186b89cb0SChristoph Hellwig // SPDX-License-Identifier: GPL-2.0 22908d778SJames Bottomley /* 32908d778SJames Bottomley * Serial Attached SCSI (SAS) Port class 42908d778SJames Bottomley * 52908d778SJames Bottomley * Copyright (C) 2005 Adaptec, Inc. All rights reserved. 62908d778SJames Bottomley * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com> 72908d778SJames Bottomley */ 82908d778SJames Bottomley 92908d778SJames Bottomley #include "sas_internal.h" 102908d778SJames Bottomley 112908d778SJames Bottomley #include <scsi/scsi_transport.h> 122908d778SJames Bottomley #include <scsi/scsi_transport_sas.h> 132908d778SJames Bottomley #include "../scsi_sas_internal.h" 142908d778SJames Bottomley 1500f0254eSDan Williams static bool phy_is_wideport_member(struct asd_sas_port *port, struct asd_sas_phy *phy) 1600f0254eSDan Williams { 1700f0254eSDan Williams struct sas_ha_struct *sas_ha = phy->ha; 1800f0254eSDan Williams 1900f0254eSDan Williams if (memcmp(port->attached_sas_addr, phy->attached_sas_addr, 2000f0254eSDan Williams SAS_ADDR_SIZE) != 0 || (sas_ha->strict_wide_ports && 2100f0254eSDan Williams memcmp(port->sas_addr, phy->sas_addr, SAS_ADDR_SIZE) != 0)) 2200f0254eSDan Williams return false; 2300f0254eSDan Williams return true; 2400f0254eSDan Williams } 2500f0254eSDan Williams 26303694eeSDan Williams static void sas_resume_port(struct asd_sas_phy *phy) 27303694eeSDan Williams { 28303694eeSDan Williams struct domain_device *dev; 29303694eeSDan Williams struct asd_sas_port *port = phy->port; 30303694eeSDan Williams struct sas_ha_struct *sas_ha = phy->ha; 31303694eeSDan Williams struct sas_internal *si = to_sas_internal(sas_ha->core.shost->transportt); 32303694eeSDan Williams 33303694eeSDan Williams if (si->dft->lldd_port_formed) 34303694eeSDan Williams si->dft->lldd_port_formed(phy); 35303694eeSDan Williams 36303694eeSDan Williams if (port->suspended) 37303694eeSDan Williams port->suspended = 0; 38303694eeSDan Williams else { 39303694eeSDan Williams /* we only need to handle "link returned" actions once */ 40303694eeSDan Williams return; 41303694eeSDan Williams } 42303694eeSDan Williams 43303694eeSDan Williams /* if the port came back: 44303694eeSDan Williams * 1/ presume every device came back 45303694eeSDan Williams * 2/ force the next revalidation to check all expander phys 46303694eeSDan Williams */ 47303694eeSDan Williams list_for_each_entry(dev, &port->dev_list, dev_list_node) { 48303694eeSDan Williams int i, rc; 49303694eeSDan Williams 50303694eeSDan Williams rc = sas_notify_lldd_dev_found(dev); 51303694eeSDan Williams if (rc) { 52303694eeSDan Williams sas_unregister_dev(port, dev); 530558f33cSJason Yan sas_destruct_devices(port); 54303694eeSDan Williams continue; 55303694eeSDan Williams } 56303694eeSDan Williams 57aa9f8328SJames Bottomley if (dev->dev_type == SAS_EDGE_EXPANDER_DEVICE || dev->dev_type == SAS_FANOUT_EXPANDER_DEVICE) { 58303694eeSDan Williams dev->ex_dev.ex_change_count = -1; 59303694eeSDan Williams for (i = 0; i < dev->ex_dev.num_phys; i++) { 60303694eeSDan Williams struct ex_phy *phy = &dev->ex_dev.ex_phy[i]; 61303694eeSDan Williams 62303694eeSDan Williams phy->phy_change_count = -1; 63303694eeSDan Williams } 64303694eeSDan Williams } 65303694eeSDan Williams } 66303694eeSDan Williams 67303694eeSDan Williams sas_discover_event(port, DISCE_RESUME); 68303694eeSDan Williams } 69303694eeSDan Williams 702908d778SJames Bottomley /** 71121246aeSBart Van Assche * sas_form_port - add this phy to a port 722908d778SJames Bottomley * @phy: the phy of interest 732908d778SJames Bottomley * 742908d778SJames Bottomley * This function adds this phy to an existing port, thus creating a wide 752908d778SJames Bottomley * port, or it creates a port and adds the phy to the port. 762908d778SJames Bottomley */ 772908d778SJames Bottomley static void sas_form_port(struct asd_sas_phy *phy) 782908d778SJames Bottomley { 792908d778SJames Bottomley int i; 802908d778SJames Bottomley struct sas_ha_struct *sas_ha = phy->ha; 812908d778SJames Bottomley struct asd_sas_port *port = phy->port; 82085f104aSJohn Garry struct domain_device *port_dev; 832908d778SJames Bottomley struct sas_internal *si = 842908d778SJames Bottomley to_sas_internal(sas_ha->core.shost->transportt); 85980fa2f9SDarrick J. Wong unsigned long flags; 862908d778SJames Bottomley 872908d778SJames Bottomley if (port) { 8800f0254eSDan Williams if (!phy_is_wideport_member(port, phy)) 8990f1e10dSDan Williams sas_deform_port(phy, 0); 90303694eeSDan Williams else if (phy->suspended) { 91303694eeSDan Williams phy->suspended = 0; 92303694eeSDan Williams sas_resume_port(phy); 93303694eeSDan Williams 94303694eeSDan Williams /* phy came back, try to cancel the timeout */ 95303694eeSDan Williams wake_up(&sas_ha->eh_wait_q); 96303694eeSDan Williams return; 97303694eeSDan Williams } else { 9815ba7806SJohn Garry pr_info("%s: phy%d belongs to port%d already(%d)!\n", 99cadbd4a5SHarvey Harrison __func__, phy->id, phy->port->id, 1002908d778SJames Bottomley phy->port->num_phys); 1012908d778SJames Bottomley return; 1022908d778SJames Bottomley } 1032908d778SJames Bottomley } 1042908d778SJames Bottomley 1055381837fSTom Peng /* see if the phy should be part of a wide port */ 106980fa2f9SDarrick J. Wong spin_lock_irqsave(&sas_ha->phy_port_lock, flags); 1072908d778SJames Bottomley for (i = 0; i < sas_ha->num_phys; i++) { 1082908d778SJames Bottomley port = sas_ha->sas_port[i]; 1092908d778SJames Bottomley spin_lock(&port->phy_list_lock); 1102908d778SJames Bottomley if (*(u64 *) port->sas_addr && 11100f0254eSDan Williams phy_is_wideport_member(port, phy) && port->num_phys > 0) { 1122908d778SJames Bottomley /* wide port */ 11315ba7806SJohn Garry pr_debug("phy%d matched wide port%d\n", phy->id, 1142908d778SJames Bottomley port->id); 1152908d778SJames Bottomley break; 1165381837fSTom Peng } 1175381837fSTom Peng spin_unlock(&port->phy_list_lock); 1185381837fSTom Peng } 1195381837fSTom Peng /* The phy does not match any existing port, create a new one */ 1205381837fSTom Peng if (i == sas_ha->num_phys) { 1215381837fSTom Peng for (i = 0; i < sas_ha->num_phys; i++) { 1225381837fSTom Peng port = sas_ha->sas_port[i]; 1235381837fSTom Peng spin_lock(&port->phy_list_lock); 1245381837fSTom Peng if (*(u64 *)port->sas_addr == 0 1255381837fSTom Peng && port->num_phys == 0) { 1265381837fSTom Peng memcpy(port->sas_addr, phy->sas_addr, 1275381837fSTom Peng SAS_ADDR_SIZE); 1282908d778SJames Bottomley break; 1292908d778SJames Bottomley } 1302908d778SJames Bottomley spin_unlock(&port->phy_list_lock); 1312908d778SJames Bottomley } 1325381837fSTom Peng } 1332908d778SJames Bottomley 1342908d778SJames Bottomley if (i >= sas_ha->num_phys) { 13515ba7806SJohn Garry pr_err("%s: couldn't find a free port, bug?\n", __func__); 136980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags); 1372908d778SJames Bottomley return; 1382908d778SJames Bottomley } 1392908d778SJames Bottomley 1402908d778SJames Bottomley /* add the phy to the port */ 141085f104aSJohn Garry port_dev = port->port_dev; 1422908d778SJames Bottomley list_add_tail(&phy->port_phy_el, &port->phy_list); 143085f104aSJohn Garry sas_phy_set_target(phy, port_dev); 1442908d778SJames Bottomley phy->port = port; 1452908d778SJames Bottomley port->num_phys++; 1462908d778SJames Bottomley port->phy_mask |= (1U << phy->id); 1472908d778SJames Bottomley 1482908d778SJames Bottomley if (*(u64 *)port->attached_sas_addr == 0) { 1492908d778SJames Bottomley port->class = phy->class; 1502908d778SJames Bottomley memcpy(port->attached_sas_addr, phy->attached_sas_addr, 1512908d778SJames Bottomley SAS_ADDR_SIZE); 1522908d778SJames Bottomley port->iproto = phy->iproto; 1532908d778SJames Bottomley port->tproto = phy->tproto; 1542908d778SJames Bottomley port->oob_mode = phy->oob_mode; 1552908d778SJames Bottomley port->linkrate = phy->linkrate; 1562908d778SJames Bottomley } else 1572908d778SJames Bottomley port->linkrate = max(port->linkrate, phy->linkrate); 1582908d778SJames Bottomley spin_unlock(&port->phy_list_lock); 159980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags); 1602908d778SJames Bottomley 1612908d778SJames Bottomley if (!port->port) { 162b4698d88SDan Williams port->port = sas_port_alloc(phy->phy->dev.parent, port->id); 1632908d778SJames Bottomley BUG_ON(!port->port); 1642908d778SJames Bottomley sas_port_add(port->port); 1652908d778SJames Bottomley } 1662908d778SJames Bottomley sas_port_add_phy(port->port, phy->phy); 1672908d778SJames Bottomley 16815ba7806SJohn Garry pr_debug("%s added to %s, phy_mask:0x%x (%16llx)\n", 16971610f55SKay Sievers dev_name(&phy->phy->dev), dev_name(&port->port->dev), 170a29c0515SJames Bottomley port->phy_mask, 171a29c0515SJames Bottomley SAS_ADDR(port->attached_sas_addr)); 172a29c0515SJames Bottomley 173085f104aSJohn Garry if (port_dev) 174085f104aSJohn Garry port_dev->pathways = port->num_phys; 1752908d778SJames Bottomley 1762908d778SJames Bottomley /* Tell the LLDD about this port formation. */ 1772908d778SJames Bottomley if (si->dft->lldd_port_formed) 1782908d778SJames Bottomley si->dft->lldd_port_formed(phy); 1792908d778SJames Bottomley 1802908d778SJames Bottomley sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN); 181085f104aSJohn Garry /* Only insert a revalidate event after initial discovery */ 182085f104aSJohn Garry if (port_dev && sas_dev_type_is_expander(port_dev->dev_type)) { 183085f104aSJohn Garry struct expander_device *ex_dev = &port_dev->ex_dev; 184085f104aSJohn Garry 185085f104aSJohn Garry ex_dev->ex_change_count = -1; 186085f104aSJohn Garry sas_discover_event(port, DISCE_REVALIDATE_DOMAIN); 187085f104aSJohn Garry } 188517e5153SJason Yan flush_workqueue(sas_ha->disco_q); 1892908d778SJames Bottomley } 1902908d778SJames Bottomley 1912908d778SJames Bottomley /** 192121246aeSBart Van Assche * sas_deform_port - remove this phy from the port it belongs to 1932908d778SJames Bottomley * @phy: the phy of interest 194121246aeSBart Van Assche * @gone: whether or not the PHY is gone 1952908d778SJames Bottomley * 1962908d778SJames Bottomley * This is called when the physical link to the other phy has been 1972908d778SJames Bottomley * lost (on this phy), in Event thread context. We cannot delay here. 1982908d778SJames Bottomley */ 19990f1e10dSDan Williams void sas_deform_port(struct asd_sas_phy *phy, int gone) 2002908d778SJames Bottomley { 2012908d778SJames Bottomley struct sas_ha_struct *sas_ha = phy->ha; 2022908d778SJames Bottomley struct asd_sas_port *port = phy->port; 2032908d778SJames Bottomley struct sas_internal *si = 2042908d778SJames Bottomley to_sas_internal(sas_ha->core.shost->transportt); 20590f1e10dSDan Williams struct domain_device *dev; 206980fa2f9SDarrick J. Wong unsigned long flags; 2072908d778SJames Bottomley 2082908d778SJames Bottomley if (!port) 2092908d778SJames Bottomley return; /* done by a phy event */ 2102908d778SJames Bottomley 21190f1e10dSDan Williams dev = port->port_dev; 21290f1e10dSDan Williams if (dev) 21390f1e10dSDan Williams dev->pathways--; 2142908d778SJames Bottomley 2152908d778SJames Bottomley if (port->num_phys == 1) { 2167d05919aSDan Williams sas_unregister_domain_devices(port, gone); 2170558f33cSJason Yan sas_destruct_devices(port); 2182908d778SJames Bottomley sas_port_delete(port->port); 2192908d778SJames Bottomley port->port = NULL; 220f41a0c44SDan Williams } else { 2212908d778SJames Bottomley sas_port_delete_phy(port->port, phy->phy); 222f41a0c44SDan Williams sas_device_set_phy(dev, port->port); 223f41a0c44SDan Williams } 2242908d778SJames Bottomley 2252908d778SJames Bottomley if (si->dft->lldd_port_deformed) 2262908d778SJames Bottomley si->dft->lldd_port_deformed(phy); 2272908d778SJames Bottomley 228980fa2f9SDarrick J. Wong spin_lock_irqsave(&sas_ha->phy_port_lock, flags); 2292908d778SJames Bottomley spin_lock(&port->phy_list_lock); 2302908d778SJames Bottomley 2312908d778SJames Bottomley list_del_init(&phy->port_phy_el); 232899fcf40SDan Williams sas_phy_set_target(phy, NULL); 2332908d778SJames Bottomley phy->port = NULL; 2342908d778SJames Bottomley port->num_phys--; 2352908d778SJames Bottomley port->phy_mask &= ~(1U << phy->id); 2362908d778SJames Bottomley 2372908d778SJames Bottomley if (port->num_phys == 0) { 2382908d778SJames Bottomley INIT_LIST_HEAD(&port->phy_list); 2392908d778SJames Bottomley memset(port->sas_addr, 0, SAS_ADDR_SIZE); 2402908d778SJames Bottomley memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE); 2412908d778SJames Bottomley port->class = 0; 2422908d778SJames Bottomley port->iproto = 0; 2432908d778SJames Bottomley port->tproto = 0; 2442908d778SJames Bottomley port->oob_mode = 0; 2452908d778SJames Bottomley port->phy_mask = 0; 2462908d778SJames Bottomley } 2472908d778SJames Bottomley spin_unlock(&port->phy_list_lock); 248980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags); 2492908d778SJames Bottomley 250085f104aSJohn Garry /* Only insert revalidate event if the port still has members */ 251085f104aSJohn Garry if (port->port && dev && sas_dev_type_is_expander(dev->dev_type)) { 252085f104aSJohn Garry struct expander_device *ex_dev = &dev->ex_dev; 253085f104aSJohn Garry 254085f104aSJohn Garry ex_dev->ex_change_count = -1; 255085f104aSJohn Garry sas_discover_event(port, DISCE_REVALIDATE_DOMAIN); 256085f104aSJohn Garry } 257085f104aSJohn Garry flush_workqueue(sas_ha->disco_q); 258085f104aSJohn Garry 2592908d778SJames Bottomley return; 2602908d778SJames Bottomley } 2612908d778SJames Bottomley 2622908d778SJames Bottomley /* ---------- SAS port events ---------- */ 2632908d778SJames Bottomley 264c4028958SDavid Howells void sas_porte_bytes_dmaed(struct work_struct *work) 2652908d778SJames Bottomley { 26622b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work); 267c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 2682908d778SJames Bottomley 2692908d778SJames Bottomley sas_form_port(phy); 2702908d778SJames Bottomley } 2712908d778SJames Bottomley 272c4028958SDavid Howells void sas_porte_broadcast_rcvd(struct work_struct *work) 2732908d778SJames Bottomley { 27422b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work); 275c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 2762908d778SJames Bottomley unsigned long flags; 2772908d778SJames Bottomley u32 prim; 2782908d778SJames Bottomley 2792908d778SJames Bottomley spin_lock_irqsave(&phy->sas_prim_lock, flags); 2802908d778SJames Bottomley prim = phy->sas_prim; 2812908d778SJames Bottomley spin_unlock_irqrestore(&phy->sas_prim_lock, flags); 2822908d778SJames Bottomley 28315ba7806SJohn Garry pr_debug("broadcast received: %d\n", prim); 2842908d778SJames Bottomley sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN); 285517e5153SJason Yan 286517e5153SJason Yan if (phy->port) 287517e5153SJason Yan flush_workqueue(phy->port->ha->disco_q); 2882908d778SJames Bottomley } 2892908d778SJames Bottomley 290c4028958SDavid Howells void sas_porte_link_reset_err(struct work_struct *work) 2912908d778SJames Bottomley { 29222b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work); 293c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 2942908d778SJames Bottomley 29590f1e10dSDan Williams sas_deform_port(phy, 1); 2962908d778SJames Bottomley } 2972908d778SJames Bottomley 298c4028958SDavid Howells void sas_porte_timer_event(struct work_struct *work) 2992908d778SJames Bottomley { 30022b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work); 301c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 3022908d778SJames Bottomley 30390f1e10dSDan Williams sas_deform_port(phy, 1); 3042908d778SJames Bottomley } 3052908d778SJames Bottomley 306c4028958SDavid Howells void sas_porte_hard_reset(struct work_struct *work) 3072908d778SJames Bottomley { 30822b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work); 309c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 3102908d778SJames Bottomley 31190f1e10dSDan Williams sas_deform_port(phy, 1); 3122908d778SJames Bottomley } 3132908d778SJames Bottomley 3142908d778SJames Bottomley /* ---------- SAS port registration ---------- */ 3152908d778SJames Bottomley 3162908d778SJames Bottomley static void sas_init_port(struct asd_sas_port *port, 3172908d778SJames Bottomley struct sas_ha_struct *sas_ha, int i) 3182908d778SJames Bottomley { 319a29c0515SJames Bottomley memset(port, 0, sizeof(*port)); 3202908d778SJames Bottomley port->id = i; 3212908d778SJames Bottomley INIT_LIST_HEAD(&port->dev_list); 32287c8331fSDan Williams INIT_LIST_HEAD(&port->disco_list); 32387c8331fSDan Williams INIT_LIST_HEAD(&port->destroy_list); 3240558f33cSJason Yan INIT_LIST_HEAD(&port->sas_port_del_list); 3252908d778SJames Bottomley spin_lock_init(&port->phy_list_lock); 3262908d778SJames Bottomley INIT_LIST_HEAD(&port->phy_list); 3272908d778SJames Bottomley port->ha = sas_ha; 3282908d778SJames Bottomley 3292908d778SJames Bottomley spin_lock_init(&port->dev_list_lock); 3302908d778SJames Bottomley } 3312908d778SJames Bottomley 3322908d778SJames Bottomley int sas_register_ports(struct sas_ha_struct *sas_ha) 3332908d778SJames Bottomley { 3342908d778SJames Bottomley int i; 3352908d778SJames Bottomley 3362908d778SJames Bottomley /* initialize the ports and discovery */ 3372908d778SJames Bottomley for (i = 0; i < sas_ha->num_phys; i++) { 3382908d778SJames Bottomley struct asd_sas_port *port = sas_ha->sas_port[i]; 3392908d778SJames Bottomley 3402908d778SJames Bottomley sas_init_port(port, sas_ha, i); 3412908d778SJames Bottomley sas_init_disc(&port->disc, port); 3422908d778SJames Bottomley } 3432908d778SJames Bottomley return 0; 3442908d778SJames Bottomley } 3452908d778SJames Bottomley 3462908d778SJames Bottomley void sas_unregister_ports(struct sas_ha_struct *sas_ha) 3472908d778SJames Bottomley { 3482908d778SJames Bottomley int i; 3492908d778SJames Bottomley 3502908d778SJames Bottomley for (i = 0; i < sas_ha->num_phys; i++) 3512908d778SJames Bottomley if (sas_ha->sas_phy[i]->port) 35290f1e10dSDan Williams sas_deform_port(sas_ha->sas_phy[i], 0); 3532908d778SJames Bottomley 3542908d778SJames Bottomley } 3551c393b97SJason Yan 3561c393b97SJason Yan const work_func_t sas_port_event_fns[PORT_NUM_EVENTS] = { 3571c393b97SJason Yan [PORTE_BYTES_DMAED] = sas_porte_bytes_dmaed, 3581c393b97SJason Yan [PORTE_BROADCAST_RCVD] = sas_porte_broadcast_rcvd, 3591c393b97SJason Yan [PORTE_LINK_RESET_ERR] = sas_porte_link_reset_err, 3601c393b97SJason Yan [PORTE_TIMER_EVENT] = sas_porte_timer_event, 3611c393b97SJason Yan [PORTE_HARD_RESET] = sas_porte_hard_reset, 3621c393b97SJason Yan }; 363