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> 13e15f669cSJason Yan #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 { 288c7e7b84SDan Carpenter struct domain_device *dev, *n; 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 */ 478c7e7b84SDan Carpenter list_for_each_entry_safe(dev, n, &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 57924a3541SJohn Garry if (dev_is_expander(dev->dev_type)) { 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 70*32698c95SDamien Le Moal static void sas_form_port_add_phy(struct asd_sas_port *port, 71*32698c95SDamien Le Moal struct asd_sas_phy *phy, bool wideport) 72*32698c95SDamien Le Moal { 73*32698c95SDamien Le Moal list_add_tail(&phy->port_phy_el, &port->phy_list); 74*32698c95SDamien Le Moal sas_phy_set_target(phy, port->port_dev); 75*32698c95SDamien Le Moal phy->port = port; 76*32698c95SDamien Le Moal port->num_phys++; 77*32698c95SDamien Le Moal port->phy_mask |= (1U << phy->id); 78*32698c95SDamien Le Moal 79*32698c95SDamien Le Moal if (wideport) 80*32698c95SDamien Le Moal pr_debug("phy%d matched wide port%d\n", phy->id, 81*32698c95SDamien Le Moal port->id); 82*32698c95SDamien Le Moal else 83*32698c95SDamien Le Moal memcpy(port->sas_addr, phy->sas_addr, SAS_ADDR_SIZE); 84*32698c95SDamien Le Moal 85*32698c95SDamien Le Moal if (*(u64 *)port->attached_sas_addr == 0) { 86*32698c95SDamien Le Moal port->class = phy->class; 87*32698c95SDamien Le Moal memcpy(port->attached_sas_addr, phy->attached_sas_addr, 88*32698c95SDamien Le Moal SAS_ADDR_SIZE); 89*32698c95SDamien Le Moal port->iproto = phy->iproto; 90*32698c95SDamien Le Moal port->tproto = phy->tproto; 91*32698c95SDamien Le Moal port->oob_mode = phy->oob_mode; 92*32698c95SDamien Le Moal port->linkrate = phy->linkrate; 93*32698c95SDamien Le Moal } else { 94*32698c95SDamien Le Moal port->linkrate = max(port->linkrate, phy->linkrate); 95*32698c95SDamien Le Moal } 96*32698c95SDamien Le Moal } 97*32698c95SDamien Le Moal 982908d778SJames Bottomley /** 99121246aeSBart Van Assche * sas_form_port - add this phy to a port 1002908d778SJames Bottomley * @phy: the phy of interest 1012908d778SJames Bottomley * 1022908d778SJames Bottomley * This function adds this phy to an existing port, thus creating a wide 1032908d778SJames Bottomley * port, or it creates a port and adds the phy to the port. 1042908d778SJames Bottomley */ 1052908d778SJames Bottomley static void sas_form_port(struct asd_sas_phy *phy) 1062908d778SJames Bottomley { 1072908d778SJames Bottomley int i; 1082908d778SJames Bottomley struct sas_ha_struct *sas_ha = phy->ha; 1092908d778SJames Bottomley struct asd_sas_port *port = phy->port; 110*32698c95SDamien Le Moal struct domain_device *port_dev = NULL; 1112908d778SJames Bottomley struct sas_internal *si = 1122908d778SJames Bottomley to_sas_internal(sas_ha->core.shost->transportt); 113980fa2f9SDarrick J. Wong unsigned long flags; 1142908d778SJames Bottomley 1152908d778SJames Bottomley if (port) { 11600f0254eSDan Williams if (!phy_is_wideport_member(port, phy)) 11790f1e10dSDan Williams sas_deform_port(phy, 0); 118303694eeSDan Williams else if (phy->suspended) { 119303694eeSDan Williams phy->suspended = 0; 120303694eeSDan Williams sas_resume_port(phy); 121303694eeSDan Williams 122303694eeSDan Williams /* phy came back, try to cancel the timeout */ 123303694eeSDan Williams wake_up(&sas_ha->eh_wait_q); 124303694eeSDan Williams return; 125303694eeSDan Williams } else { 12615ba7806SJohn Garry pr_info("%s: phy%d belongs to port%d already(%d)!\n", 127cadbd4a5SHarvey Harrison __func__, phy->id, phy->port->id, 1282908d778SJames Bottomley phy->port->num_phys); 1292908d778SJames Bottomley return; 1302908d778SJames Bottomley } 1312908d778SJames Bottomley } 1322908d778SJames Bottomley 1335381837fSTom Peng /* see if the phy should be part of a wide port */ 134980fa2f9SDarrick J. Wong spin_lock_irqsave(&sas_ha->phy_port_lock, flags); 1352908d778SJames Bottomley for (i = 0; i < sas_ha->num_phys; i++) { 1362908d778SJames Bottomley port = sas_ha->sas_port[i]; 1372908d778SJames Bottomley spin_lock(&port->phy_list_lock); 1382908d778SJames Bottomley if (*(u64 *) port->sas_addr && 13900f0254eSDan Williams phy_is_wideport_member(port, phy) && port->num_phys > 0) { 1402908d778SJames Bottomley /* wide port */ 141*32698c95SDamien Le Moal port_dev = port->port_dev; 142*32698c95SDamien Le Moal sas_form_port_add_phy(port, phy, true); 143*32698c95SDamien Le Moal spin_unlock(&port->phy_list_lock); 1442908d778SJames Bottomley break; 1455381837fSTom Peng } 1465381837fSTom Peng spin_unlock(&port->phy_list_lock); 1475381837fSTom Peng } 1485381837fSTom Peng /* The phy does not match any existing port, create a new one */ 1495381837fSTom Peng if (i == sas_ha->num_phys) { 1505381837fSTom Peng for (i = 0; i < sas_ha->num_phys; i++) { 1515381837fSTom Peng port = sas_ha->sas_port[i]; 1525381837fSTom Peng spin_lock(&port->phy_list_lock); 1535381837fSTom Peng if (*(u64 *)port->sas_addr == 0 1545381837fSTom Peng && port->num_phys == 0) { 155*32698c95SDamien Le Moal port_dev = port->port_dev; 156*32698c95SDamien Le Moal sas_form_port_add_phy(port, phy, false); 157*32698c95SDamien Le Moal spin_unlock(&port->phy_list_lock); 1582908d778SJames Bottomley break; 1592908d778SJames Bottomley } 1602908d778SJames Bottomley spin_unlock(&port->phy_list_lock); 1612908d778SJames Bottomley } 1622908d778SJames Bottomley 1632908d778SJames Bottomley if (i >= sas_ha->num_phys) { 164*32698c95SDamien Le Moal pr_err("%s: couldn't find a free port, bug?\n", 165*32698c95SDamien Le Moal __func__); 166980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags); 1672908d778SJames Bottomley return; 1682908d778SJames Bottomley } 169*32698c95SDamien Le Moal } 170980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags); 1712908d778SJames Bottomley 1722908d778SJames Bottomley if (!port->port) { 173b4698d88SDan Williams port->port = sas_port_alloc(phy->phy->dev.parent, port->id); 1742908d778SJames Bottomley BUG_ON(!port->port); 1752908d778SJames Bottomley sas_port_add(port->port); 1762908d778SJames Bottomley } 1772908d778SJames Bottomley sas_port_add_phy(port->port, phy->phy); 1782908d778SJames Bottomley 179b3e3d4c6SJohn Garry pr_debug("%s added to %s, phy_mask:0x%x (%016llx)\n", 18071610f55SKay Sievers dev_name(&phy->phy->dev), dev_name(&port->port->dev), 181a29c0515SJames Bottomley port->phy_mask, 182a29c0515SJames Bottomley SAS_ADDR(port->attached_sas_addr)); 183a29c0515SJames Bottomley 184085f104aSJohn Garry if (port_dev) 185085f104aSJohn Garry port_dev->pathways = port->num_phys; 1862908d778SJames Bottomley 1872908d778SJames Bottomley /* Tell the LLDD about this port formation. */ 1882908d778SJames Bottomley if (si->dft->lldd_port_formed) 1892908d778SJames Bottomley si->dft->lldd_port_formed(phy); 1902908d778SJames Bottomley 1912908d778SJames Bottomley sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN); 192085f104aSJohn Garry /* Only insert a revalidate event after initial discovery */ 193924a3541SJohn Garry if (port_dev && dev_is_expander(port_dev->dev_type)) { 194085f104aSJohn Garry struct expander_device *ex_dev = &port_dev->ex_dev; 195085f104aSJohn Garry 196085f104aSJohn Garry ex_dev->ex_change_count = -1; 197085f104aSJohn Garry sas_discover_event(port, DISCE_REVALIDATE_DOMAIN); 198085f104aSJohn Garry } 199517e5153SJason Yan flush_workqueue(sas_ha->disco_q); 2002908d778SJames Bottomley } 2012908d778SJames Bottomley 2022908d778SJames Bottomley /** 203121246aeSBart Van Assche * sas_deform_port - remove this phy from the port it belongs to 2042908d778SJames Bottomley * @phy: the phy of interest 205121246aeSBart Van Assche * @gone: whether or not the PHY is gone 2062908d778SJames Bottomley * 2072908d778SJames Bottomley * This is called when the physical link to the other phy has been 2082908d778SJames Bottomley * lost (on this phy), in Event thread context. We cannot delay here. 2092908d778SJames Bottomley */ 21090f1e10dSDan Williams void sas_deform_port(struct asd_sas_phy *phy, int gone) 2112908d778SJames Bottomley { 2122908d778SJames Bottomley struct sas_ha_struct *sas_ha = phy->ha; 2132908d778SJames Bottomley struct asd_sas_port *port = phy->port; 2142908d778SJames Bottomley struct sas_internal *si = 2152908d778SJames Bottomley to_sas_internal(sas_ha->core.shost->transportt); 21690f1e10dSDan Williams struct domain_device *dev; 217980fa2f9SDarrick J. Wong unsigned long flags; 2182908d778SJames Bottomley 2192908d778SJames Bottomley if (!port) 2202908d778SJames Bottomley return; /* done by a phy event */ 2212908d778SJames Bottomley 22290f1e10dSDan Williams dev = port->port_dev; 22390f1e10dSDan Williams if (dev) 22490f1e10dSDan Williams dev->pathways--; 2252908d778SJames Bottomley 2262908d778SJames Bottomley if (port->num_phys == 1) { 2277d05919aSDan Williams sas_unregister_domain_devices(port, gone); 2280558f33cSJason Yan sas_destruct_devices(port); 2292908d778SJames Bottomley sas_port_delete(port->port); 2302908d778SJames Bottomley port->port = NULL; 231f41a0c44SDan Williams } else { 2322908d778SJames Bottomley sas_port_delete_phy(port->port, phy->phy); 233f41a0c44SDan Williams sas_device_set_phy(dev, port->port); 234f41a0c44SDan Williams } 2352908d778SJames Bottomley 2362908d778SJames Bottomley if (si->dft->lldd_port_deformed) 2372908d778SJames Bottomley si->dft->lldd_port_deformed(phy); 2382908d778SJames Bottomley 239980fa2f9SDarrick J. Wong spin_lock_irqsave(&sas_ha->phy_port_lock, flags); 2402908d778SJames Bottomley spin_lock(&port->phy_list_lock); 2412908d778SJames Bottomley 2422908d778SJames Bottomley list_del_init(&phy->port_phy_el); 243899fcf40SDan Williams sas_phy_set_target(phy, NULL); 2442908d778SJames Bottomley phy->port = NULL; 2452908d778SJames Bottomley port->num_phys--; 2462908d778SJames Bottomley port->phy_mask &= ~(1U << phy->id); 2472908d778SJames Bottomley 2482908d778SJames Bottomley if (port->num_phys == 0) { 2492908d778SJames Bottomley INIT_LIST_HEAD(&port->phy_list); 2502908d778SJames Bottomley memset(port->sas_addr, 0, SAS_ADDR_SIZE); 2512908d778SJames Bottomley memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE); 2522908d778SJames Bottomley port->class = 0; 2532908d778SJames Bottomley port->iproto = 0; 2542908d778SJames Bottomley port->tproto = 0; 2552908d778SJames Bottomley port->oob_mode = 0; 2562908d778SJames Bottomley port->phy_mask = 0; 2572908d778SJames Bottomley } 2582908d778SJames Bottomley spin_unlock(&port->phy_list_lock); 259980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags); 2602908d778SJames Bottomley 261085f104aSJohn Garry /* Only insert revalidate event if the port still has members */ 262924a3541SJohn Garry if (port->port && dev && dev_is_expander(dev->dev_type)) { 263085f104aSJohn Garry struct expander_device *ex_dev = &dev->ex_dev; 264085f104aSJohn Garry 265085f104aSJohn Garry ex_dev->ex_change_count = -1; 266085f104aSJohn Garry sas_discover_event(port, DISCE_REVALIDATE_DOMAIN); 267085f104aSJohn Garry } 268085f104aSJohn Garry flush_workqueue(sas_ha->disco_q); 269085f104aSJohn Garry 2702908d778SJames Bottomley return; 2712908d778SJames Bottomley } 2722908d778SJames Bottomley 2732908d778SJames Bottomley /* ---------- SAS port events ---------- */ 2742908d778SJames Bottomley 275c4028958SDavid Howells void sas_porte_bytes_dmaed(struct work_struct *work) 2762908d778SJames Bottomley { 27722b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work); 278c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 2792908d778SJames Bottomley 2802908d778SJames Bottomley sas_form_port(phy); 2812908d778SJames Bottomley } 2822908d778SJames Bottomley 283c4028958SDavid Howells void sas_porte_broadcast_rcvd(struct work_struct *work) 2842908d778SJames Bottomley { 28522b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work); 286c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 2872908d778SJames Bottomley unsigned long flags; 2882908d778SJames Bottomley u32 prim; 2892908d778SJames Bottomley 2902908d778SJames Bottomley spin_lock_irqsave(&phy->sas_prim_lock, flags); 2912908d778SJames Bottomley prim = phy->sas_prim; 2922908d778SJames Bottomley spin_unlock_irqrestore(&phy->sas_prim_lock, flags); 2932908d778SJames Bottomley 29415ba7806SJohn Garry pr_debug("broadcast received: %d\n", prim); 2952908d778SJames Bottomley sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN); 296517e5153SJason Yan 297517e5153SJason Yan if (phy->port) 298517e5153SJason Yan flush_workqueue(phy->port->ha->disco_q); 2992908d778SJames Bottomley } 3002908d778SJames Bottomley 301c4028958SDavid Howells void sas_porte_link_reset_err(struct work_struct *work) 3022908d778SJames Bottomley { 30322b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work); 304c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 3052908d778SJames Bottomley 30690f1e10dSDan Williams sas_deform_port(phy, 1); 3072908d778SJames Bottomley } 3082908d778SJames Bottomley 309c4028958SDavid Howells void sas_porte_timer_event(struct work_struct *work) 3102908d778SJames Bottomley { 31122b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work); 312c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 3132908d778SJames Bottomley 31490f1e10dSDan Williams sas_deform_port(phy, 1); 3152908d778SJames Bottomley } 3162908d778SJames Bottomley 317c4028958SDavid Howells void sas_porte_hard_reset(struct work_struct *work) 3182908d778SJames Bottomley { 31922b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work); 320c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy; 3212908d778SJames Bottomley 32290f1e10dSDan Williams sas_deform_port(phy, 1); 3232908d778SJames Bottomley } 3242908d778SJames Bottomley 3252908d778SJames Bottomley /* ---------- SAS port registration ---------- */ 3262908d778SJames Bottomley 3272908d778SJames Bottomley static void sas_init_port(struct asd_sas_port *port, 3282908d778SJames Bottomley struct sas_ha_struct *sas_ha, int i) 3292908d778SJames Bottomley { 330a29c0515SJames Bottomley memset(port, 0, sizeof(*port)); 3312908d778SJames Bottomley port->id = i; 3322908d778SJames Bottomley INIT_LIST_HEAD(&port->dev_list); 33387c8331fSDan Williams INIT_LIST_HEAD(&port->disco_list); 33487c8331fSDan Williams INIT_LIST_HEAD(&port->destroy_list); 3350558f33cSJason Yan INIT_LIST_HEAD(&port->sas_port_del_list); 3362908d778SJames Bottomley spin_lock_init(&port->phy_list_lock); 3372908d778SJames Bottomley INIT_LIST_HEAD(&port->phy_list); 3382908d778SJames Bottomley port->ha = sas_ha; 3392908d778SJames Bottomley 3402908d778SJames Bottomley spin_lock_init(&port->dev_list_lock); 3412908d778SJames Bottomley } 3422908d778SJames Bottomley 3432908d778SJames Bottomley int sas_register_ports(struct sas_ha_struct *sas_ha) 3442908d778SJames Bottomley { 3452908d778SJames Bottomley int i; 3462908d778SJames Bottomley 3472908d778SJames Bottomley /* initialize the ports and discovery */ 3482908d778SJames Bottomley for (i = 0; i < sas_ha->num_phys; i++) { 3492908d778SJames Bottomley struct asd_sas_port *port = sas_ha->sas_port[i]; 3502908d778SJames Bottomley 3512908d778SJames Bottomley sas_init_port(port, sas_ha, i); 3522908d778SJames Bottomley sas_init_disc(&port->disc, port); 3532908d778SJames Bottomley } 3542908d778SJames Bottomley return 0; 3552908d778SJames Bottomley } 3562908d778SJames Bottomley 3572908d778SJames Bottomley void sas_unregister_ports(struct sas_ha_struct *sas_ha) 3582908d778SJames Bottomley { 3592908d778SJames Bottomley int i; 3602908d778SJames Bottomley 3612908d778SJames Bottomley for (i = 0; i < sas_ha->num_phys; i++) 3622908d778SJames Bottomley if (sas_ha->sas_phy[i]->port) 36390f1e10dSDan Williams sas_deform_port(sas_ha->sas_phy[i], 0); 3642908d778SJames Bottomley 3652908d778SJames Bottomley } 3661c393b97SJason Yan 3671c393b97SJason Yan const work_func_t sas_port_event_fns[PORT_NUM_EVENTS] = { 3681c393b97SJason Yan [PORTE_BYTES_DMAED] = sas_porte_bytes_dmaed, 3691c393b97SJason Yan [PORTE_BROADCAST_RCVD] = sas_porte_broadcast_rcvd, 3701c393b97SJason Yan [PORTE_LINK_RESET_ERR] = sas_porte_link_reset_err, 3711c393b97SJason Yan [PORTE_TIMER_EVENT] = sas_porte_timer_event, 3721c393b97SJason Yan [PORTE_HARD_RESET] = sas_porte_hard_reset, 3731c393b97SJason Yan }; 374