12908d778SJames Bottomley /* 22908d778SJames Bottomley * Serial Attached SCSI (SAS) Port class 32908d778SJames Bottomley * 42908d778SJames Bottomley * Copyright (C) 2005 Adaptec, Inc. All rights reserved. 52908d778SJames Bottomley * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com> 62908d778SJames Bottomley * 72908d778SJames Bottomley * This file is licensed under GPLv2. 82908d778SJames Bottomley * 92908d778SJames Bottomley * This program is free software; you can redistribute it and/or 102908d778SJames Bottomley * modify it under the terms of the GNU General Public License as 112908d778SJames Bottomley * published by the Free Software Foundation; either version 2 of the 122908d778SJames Bottomley * License, or (at your option) any later version. 132908d778SJames Bottomley * 142908d778SJames Bottomley * This program is distributed in the hope that it will be useful, but 152908d778SJames Bottomley * WITHOUT ANY WARRANTY; without even the implied warranty of 162908d778SJames Bottomley * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 172908d778SJames Bottomley * General Public License for more details. 182908d778SJames Bottomley * 192908d778SJames Bottomley * You should have received a copy of the GNU General Public License 202908d778SJames Bottomley * along with this program; if not, write to the Free Software 212908d778SJames Bottomley * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 222908d778SJames Bottomley * 232908d778SJames Bottomley */ 242908d778SJames Bottomley 252908d778SJames Bottomley #include "sas_internal.h" 262908d778SJames Bottomley 272908d778SJames Bottomley #include <scsi/scsi_transport.h> 282908d778SJames Bottomley #include <scsi/scsi_transport_sas.h> 292908d778SJames Bottomley #include "../scsi_sas_internal.h" 302908d778SJames Bottomley 3100f0254eSDan Williams static bool phy_is_wideport_member(struct asd_sas_port *port, struct asd_sas_phy *phy) 3200f0254eSDan Williams { 3300f0254eSDan Williams struct sas_ha_struct *sas_ha = phy->ha; 3400f0254eSDan Williams 3500f0254eSDan Williams if (memcmp(port->attached_sas_addr, phy->attached_sas_addr, 3600f0254eSDan Williams SAS_ADDR_SIZE) != 0 || (sas_ha->strict_wide_ports && 3700f0254eSDan Williams memcmp(port->sas_addr, phy->sas_addr, SAS_ADDR_SIZE) != 0)) 3800f0254eSDan Williams return false; 3900f0254eSDan Williams return true; 4000f0254eSDan Williams } 4100f0254eSDan Williams 422908d778SJames Bottomley /** 432908d778SJames Bottomley * sas_form_port -- add this phy to a port 442908d778SJames Bottomley * @phy: the phy of interest 452908d778SJames Bottomley * 462908d778SJames Bottomley * This function adds this phy to an existing port, thus creating a wide 472908d778SJames Bottomley * port, or it creates a port and adds the phy to the port. 482908d778SJames Bottomley */ 492908d778SJames Bottomley static void sas_form_port(struct asd_sas_phy *phy) 502908d778SJames Bottomley { 512908d778SJames Bottomley int i; 522908d778SJames Bottomley struct sas_ha_struct *sas_ha = phy->ha; 532908d778SJames Bottomley struct asd_sas_port *port = phy->port; 542908d778SJames Bottomley struct sas_internal *si = 552908d778SJames Bottomley to_sas_internal(sas_ha->core.shost->transportt); 56980fa2f9SDarrick J. Wong unsigned long flags; 572908d778SJames Bottomley 582908d778SJames Bottomley if (port) { 5900f0254eSDan Williams if (!phy_is_wideport_member(port, phy)) 6090f1e10dSDan Williams sas_deform_port(phy, 0); 612908d778SJames Bottomley else { 622908d778SJames Bottomley SAS_DPRINTK("%s: phy%d belongs to port%d already(%d)!\n", 63cadbd4a5SHarvey Harrison __func__, phy->id, phy->port->id, 642908d778SJames Bottomley phy->port->num_phys); 652908d778SJames Bottomley return; 662908d778SJames Bottomley } 672908d778SJames Bottomley } 682908d778SJames Bottomley 695381837fSTom Peng /* see if the phy should be part of a wide port */ 70980fa2f9SDarrick J. Wong spin_lock_irqsave(&sas_ha->phy_port_lock, flags); 712908d778SJames Bottomley for (i = 0; i < sas_ha->num_phys; i++) { 722908d778SJames Bottomley port = sas_ha->sas_port[i]; 732908d778SJames Bottomley spin_lock(&port->phy_list_lock); 742908d778SJames Bottomley if (*(u64 *) port->sas_addr && 7500f0254eSDan Williams phy_is_wideport_member(port, phy) && port->num_phys > 0) { 762908d778SJames Bottomley /* wide port */ 772908d778SJames Bottomley SAS_DPRINTK("phy%d matched wide port%d\n", phy->id, 782908d778SJames Bottomley port->id); 792908d778SJames Bottomley break; 805381837fSTom Peng } 815381837fSTom Peng spin_unlock(&port->phy_list_lock); 825381837fSTom Peng } 835381837fSTom Peng /* The phy does not match any existing port, create a new one */ 845381837fSTom Peng if (i == sas_ha->num_phys) { 855381837fSTom Peng for (i = 0; i < sas_ha->num_phys; i++) { 865381837fSTom Peng port = sas_ha->sas_port[i]; 875381837fSTom Peng spin_lock(&port->phy_list_lock); 885381837fSTom Peng if (*(u64 *)port->sas_addr == 0 895381837fSTom Peng && port->num_phys == 0) { 905381837fSTom Peng memcpy(port->sas_addr, phy->sas_addr, 915381837fSTom Peng SAS_ADDR_SIZE); 922908d778SJames Bottomley break; 932908d778SJames Bottomley } 942908d778SJames Bottomley spin_unlock(&port->phy_list_lock); 952908d778SJames Bottomley } 965381837fSTom Peng } 972908d778SJames Bottomley 982908d778SJames Bottomley if (i >= sas_ha->num_phys) { 992908d778SJames Bottomley printk(KERN_NOTICE "%s: couldn't find a free port, bug?\n", 100cadbd4a5SHarvey Harrison __func__); 101980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags); 1022908d778SJames Bottomley return; 1032908d778SJames Bottomley } 1042908d778SJames Bottomley 1052908d778SJames Bottomley /* add the phy to the port */ 1062908d778SJames Bottomley list_add_tail(&phy->port_phy_el, &port->phy_list); 1072908d778SJames Bottomley phy->port = port; 1082908d778SJames Bottomley port->num_phys++; 1092908d778SJames Bottomley port->phy_mask |= (1U << phy->id); 1102908d778SJames Bottomley 1112908d778SJames Bottomley if (*(u64 *)port->attached_sas_addr == 0) { 1122908d778SJames Bottomley port->class = phy->class; 1132908d778SJames Bottomley memcpy(port->attached_sas_addr, phy->attached_sas_addr, 1142908d778SJames Bottomley SAS_ADDR_SIZE); 1152908d778SJames Bottomley port->iproto = phy->iproto; 1162908d778SJames Bottomley port->tproto = phy->tproto; 1172908d778SJames Bottomley port->oob_mode = phy->oob_mode; 1182908d778SJames Bottomley port->linkrate = phy->linkrate; 1192908d778SJames Bottomley } else 1202908d778SJames Bottomley port->linkrate = max(port->linkrate, phy->linkrate); 1212908d778SJames Bottomley spin_unlock(&port->phy_list_lock); 122980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags); 1232908d778SJames Bottomley 1242908d778SJames Bottomley if (!port->port) { 1252908d778SJames Bottomley port->port = sas_port_alloc(phy->phy->dev.parent, port->id); 1262908d778SJames Bottomley BUG_ON(!port->port); 1272908d778SJames Bottomley sas_port_add(port->port); 1282908d778SJames Bottomley } 1292908d778SJames Bottomley sas_port_add_phy(port->port, phy->phy); 1302908d778SJames Bottomley 131a29c0515SJames Bottomley SAS_DPRINTK("%s added to %s, phy_mask:0x%x (%16llx)\n", 13271610f55SKay Sievers dev_name(&phy->phy->dev), dev_name(&port->port->dev), 133a29c0515SJames Bottomley port->phy_mask, 134a29c0515SJames Bottomley SAS_ADDR(port->attached_sas_addr)); 135a29c0515SJames Bottomley 1362908d778SJames Bottomley if (port->port_dev) 1372908d778SJames Bottomley port->port_dev->pathways = port->num_phys; 1382908d778SJames Bottomley 1392908d778SJames Bottomley /* Tell the LLDD about this port formation. */ 1402908d778SJames Bottomley if (si->dft->lldd_port_formed) 1412908d778SJames Bottomley si->dft->lldd_port_formed(phy); 1422908d778SJames Bottomley 1432908d778SJames Bottomley sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN); 1442908d778SJames Bottomley } 1452908d778SJames Bottomley 1462908d778SJames Bottomley /** 1472908d778SJames Bottomley * sas_deform_port -- remove this phy from the port it belongs to 1482908d778SJames Bottomley * @phy: the phy of interest 1492908d778SJames Bottomley * 1502908d778SJames Bottomley * This is called when the physical link to the other phy has been 1512908d778SJames Bottomley * lost (on this phy), in Event thread context. We cannot delay here. 1522908d778SJames Bottomley */ 15390f1e10dSDan Williams void sas_deform_port(struct asd_sas_phy *phy, int gone) 1542908d778SJames Bottomley { 1552908d778SJames Bottomley struct sas_ha_struct *sas_ha = phy->ha; 1562908d778SJames Bottomley struct asd_sas_port *port = phy->port; 1572908d778SJames Bottomley struct sas_internal *si = 1582908d778SJames Bottomley to_sas_internal(sas_ha->core.shost->transportt); 15990f1e10dSDan Williams struct domain_device *dev; 160980fa2f9SDarrick J. Wong unsigned long flags; 1612908d778SJames Bottomley 1622908d778SJames Bottomley if (!port) 1632908d778SJames Bottomley return; /* done by a phy event */ 1642908d778SJames Bottomley 16590f1e10dSDan Williams dev = port->port_dev; 16690f1e10dSDan Williams if (dev) 16790f1e10dSDan Williams dev->pathways--; 1682908d778SJames Bottomley 1692908d778SJames Bottomley if (port->num_phys == 1) { 1707d05919aSDan Williams sas_unregister_domain_devices(port, gone); 1712908d778SJames Bottomley sas_port_delete(port->port); 1722908d778SJames Bottomley port->port = NULL; 173f41a0c44SDan Williams } else { 1742908d778SJames Bottomley sas_port_delete_phy(port->port, phy->phy); 175f41a0c44SDan Williams sas_device_set_phy(dev, port->port); 176f41a0c44SDan Williams } 1772908d778SJames Bottomley 1782908d778SJames Bottomley if (si->dft->lldd_port_deformed) 1792908d778SJames Bottomley si->dft->lldd_port_deformed(phy); 1802908d778SJames Bottomley 181980fa2f9SDarrick J. Wong spin_lock_irqsave(&sas_ha->phy_port_lock, flags); 1822908d778SJames Bottomley spin_lock(&port->phy_list_lock); 1832908d778SJames Bottomley 1842908d778SJames Bottomley list_del_init(&phy->port_phy_el); 1852908d778SJames Bottomley phy->port = NULL; 1862908d778SJames Bottomley port->num_phys--; 1872908d778SJames Bottomley port->phy_mask &= ~(1U << phy->id); 1882908d778SJames Bottomley 1892908d778SJames Bottomley if (port->num_phys == 0) { 1902908d778SJames Bottomley INIT_LIST_HEAD(&port->phy_list); 1912908d778SJames Bottomley memset(port->sas_addr, 0, SAS_ADDR_SIZE); 1922908d778SJames Bottomley memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE); 1932908d778SJames Bottomley port->class = 0; 1942908d778SJames Bottomley port->iproto = 0; 1952908d778SJames Bottomley port->tproto = 0; 1962908d778SJames Bottomley port->oob_mode = 0; 1972908d778SJames Bottomley port->phy_mask = 0; 1982908d778SJames Bottomley } 1992908d778SJames Bottomley spin_unlock(&port->phy_list_lock); 200980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags); 2012908d778SJames Bottomley 2022908d778SJames Bottomley return; 2032908d778SJames Bottomley } 2042908d778SJames Bottomley 2052908d778SJames Bottomley /* ---------- SAS port events ---------- */ 2062908d778SJames Bottomley 207c4028958SDavid Howells void sas_porte_bytes_dmaed(struct work_struct *work) 2082908d778SJames Bottomley { 209c4028958SDavid Howells struct asd_sas_event *ev = 210c4028958SDavid Howells container_of(work, struct asd_sas_event, work); 211c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 2122908d778SJames Bottomley 213b15ebe0bSDan Williams clear_bit(PORTE_BYTES_DMAED, &phy->port_events_pending); 2142908d778SJames Bottomley 2152908d778SJames Bottomley sas_form_port(phy); 2162908d778SJames Bottomley } 2172908d778SJames Bottomley 218c4028958SDavid Howells void sas_porte_broadcast_rcvd(struct work_struct *work) 2192908d778SJames Bottomley { 220c4028958SDavid Howells struct asd_sas_event *ev = 221c4028958SDavid Howells container_of(work, struct asd_sas_event, work); 222c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 2232908d778SJames Bottomley unsigned long flags; 2242908d778SJames Bottomley u32 prim; 2252908d778SJames Bottomley 226b15ebe0bSDan Williams clear_bit(PORTE_BROADCAST_RCVD, &phy->port_events_pending); 2272908d778SJames Bottomley 2282908d778SJames Bottomley spin_lock_irqsave(&phy->sas_prim_lock, flags); 2292908d778SJames Bottomley prim = phy->sas_prim; 2302908d778SJames Bottomley spin_unlock_irqrestore(&phy->sas_prim_lock, flags); 2312908d778SJames Bottomley 2322908d778SJames Bottomley SAS_DPRINTK("broadcast received: %d\n", prim); 2332908d778SJames Bottomley sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN); 2342908d778SJames Bottomley } 2352908d778SJames Bottomley 236c4028958SDavid Howells void sas_porte_link_reset_err(struct work_struct *work) 2372908d778SJames Bottomley { 238c4028958SDavid Howells struct asd_sas_event *ev = 239c4028958SDavid Howells container_of(work, struct asd_sas_event, work); 240c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 2412908d778SJames Bottomley 242b15ebe0bSDan Williams clear_bit(PORTE_LINK_RESET_ERR, &phy->port_events_pending); 2432908d778SJames Bottomley 24490f1e10dSDan Williams sas_deform_port(phy, 1); 2452908d778SJames Bottomley } 2462908d778SJames Bottomley 247c4028958SDavid Howells void sas_porte_timer_event(struct work_struct *work) 2482908d778SJames Bottomley { 249c4028958SDavid Howells struct asd_sas_event *ev = 250c4028958SDavid Howells container_of(work, struct asd_sas_event, work); 251c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 2522908d778SJames Bottomley 253b15ebe0bSDan Williams clear_bit(PORTE_TIMER_EVENT, &phy->port_events_pending); 2542908d778SJames Bottomley 25590f1e10dSDan Williams sas_deform_port(phy, 1); 2562908d778SJames Bottomley } 2572908d778SJames Bottomley 258c4028958SDavid Howells void sas_porte_hard_reset(struct work_struct *work) 2592908d778SJames Bottomley { 260c4028958SDavid Howells struct asd_sas_event *ev = 261c4028958SDavid Howells container_of(work, struct asd_sas_event, work); 262c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 2632908d778SJames Bottomley 264b15ebe0bSDan Williams clear_bit(PORTE_HARD_RESET, &phy->port_events_pending); 2652908d778SJames Bottomley 26690f1e10dSDan Williams sas_deform_port(phy, 1); 2672908d778SJames Bottomley } 2682908d778SJames Bottomley 2692908d778SJames Bottomley /* ---------- SAS port registration ---------- */ 2702908d778SJames Bottomley 2712908d778SJames Bottomley static void sas_init_port(struct asd_sas_port *port, 2722908d778SJames Bottomley struct sas_ha_struct *sas_ha, int i) 2732908d778SJames Bottomley { 274a29c0515SJames Bottomley memset(port, 0, sizeof(*port)); 2752908d778SJames Bottomley port->id = i; 2762908d778SJames Bottomley INIT_LIST_HEAD(&port->dev_list); 27787c8331fSDan Williams INIT_LIST_HEAD(&port->disco_list); 27887c8331fSDan Williams INIT_LIST_HEAD(&port->destroy_list); 2792908d778SJames Bottomley spin_lock_init(&port->phy_list_lock); 2802908d778SJames Bottomley INIT_LIST_HEAD(&port->phy_list); 2812908d778SJames Bottomley port->ha = sas_ha; 2822908d778SJames Bottomley 2832908d778SJames Bottomley spin_lock_init(&port->dev_list_lock); 2842908d778SJames Bottomley } 2852908d778SJames Bottomley 2862908d778SJames Bottomley int sas_register_ports(struct sas_ha_struct *sas_ha) 2872908d778SJames Bottomley { 2882908d778SJames Bottomley int i; 2892908d778SJames Bottomley 2902908d778SJames Bottomley /* initialize the ports and discovery */ 2912908d778SJames Bottomley for (i = 0; i < sas_ha->num_phys; i++) { 2922908d778SJames Bottomley struct asd_sas_port *port = sas_ha->sas_port[i]; 2932908d778SJames Bottomley 2942908d778SJames Bottomley sas_init_port(port, sas_ha, i); 2952908d778SJames Bottomley sas_init_disc(&port->disc, port); 2962908d778SJames Bottomley } 2972908d778SJames Bottomley return 0; 2982908d778SJames Bottomley } 2992908d778SJames Bottomley 3002908d778SJames Bottomley void sas_unregister_ports(struct sas_ha_struct *sas_ha) 3012908d778SJames Bottomley { 3022908d778SJames Bottomley int i; 3032908d778SJames Bottomley 3042908d778SJames Bottomley for (i = 0; i < sas_ha->num_phys; i++) 3052908d778SJames Bottomley if (sas_ha->sas_phy[i]->port) 30690f1e10dSDan Williams sas_deform_port(sas_ha->sas_phy[i], 0); 3072908d778SJames Bottomley 3082908d778SJames Bottomley } 309