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
phy_is_wideport_member(struct asd_sas_port * port,struct asd_sas_phy * phy)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
sas_resume_port(struct asd_sas_phy * phy)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;
31*1136a022SJohn Garry struct sas_internal *si = to_sas_internal(sas_ha->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
sas_form_port_add_phy(struct asd_sas_port * port,struct asd_sas_phy * phy,bool wideport)7032698c95SDamien Le Moal static void sas_form_port_add_phy(struct asd_sas_port *port,
7132698c95SDamien Le Moal struct asd_sas_phy *phy, bool wideport)
7232698c95SDamien Le Moal {
7332698c95SDamien Le Moal list_add_tail(&phy->port_phy_el, &port->phy_list);
7432698c95SDamien Le Moal sas_phy_set_target(phy, port->port_dev);
7532698c95SDamien Le Moal phy->port = port;
7632698c95SDamien Le Moal port->num_phys++;
7732698c95SDamien Le Moal port->phy_mask |= (1U << phy->id);
7832698c95SDamien Le Moal
7932698c95SDamien Le Moal if (wideport)
8032698c95SDamien Le Moal pr_debug("phy%d matched wide port%d\n", phy->id,
8132698c95SDamien Le Moal port->id);
8232698c95SDamien Le Moal else
8332698c95SDamien Le Moal memcpy(port->sas_addr, phy->sas_addr, SAS_ADDR_SIZE);
8432698c95SDamien Le Moal
8532698c95SDamien Le Moal if (*(u64 *)port->attached_sas_addr == 0) {
8632698c95SDamien Le Moal memcpy(port->attached_sas_addr, phy->attached_sas_addr,
8732698c95SDamien Le Moal SAS_ADDR_SIZE);
8832698c95SDamien Le Moal port->iproto = phy->iproto;
8932698c95SDamien Le Moal port->tproto = phy->tproto;
9032698c95SDamien Le Moal port->oob_mode = phy->oob_mode;
9132698c95SDamien Le Moal port->linkrate = phy->linkrate;
9232698c95SDamien Le Moal } else {
9332698c95SDamien Le Moal port->linkrate = max(port->linkrate, phy->linkrate);
9432698c95SDamien Le Moal }
9532698c95SDamien Le Moal }
9632698c95SDamien Le Moal
972908d778SJames Bottomley /**
98121246aeSBart Van Assche * sas_form_port - add this phy to a port
992908d778SJames Bottomley * @phy: the phy of interest
1002908d778SJames Bottomley *
1012908d778SJames Bottomley * This function adds this phy to an existing port, thus creating a wide
1022908d778SJames Bottomley * port, or it creates a port and adds the phy to the port.
1032908d778SJames Bottomley */
sas_form_port(struct asd_sas_phy * phy)1042908d778SJames Bottomley static void sas_form_port(struct asd_sas_phy *phy)
1052908d778SJames Bottomley {
1062908d778SJames Bottomley int i;
1072908d778SJames Bottomley struct sas_ha_struct *sas_ha = phy->ha;
1082908d778SJames Bottomley struct asd_sas_port *port = phy->port;
10932698c95SDamien Le Moal struct domain_device *port_dev = NULL;
1102908d778SJames Bottomley struct sas_internal *si =
111*1136a022SJohn Garry to_sas_internal(sas_ha->shost->transportt);
112980fa2f9SDarrick J. Wong unsigned long flags;
1132908d778SJames Bottomley
1142908d778SJames Bottomley if (port) {
11500f0254eSDan Williams if (!phy_is_wideport_member(port, phy))
11690f1e10dSDan Williams sas_deform_port(phy, 0);
117303694eeSDan Williams else if (phy->suspended) {
118303694eeSDan Williams phy->suspended = 0;
119303694eeSDan Williams sas_resume_port(phy);
120303694eeSDan Williams
121303694eeSDan Williams /* phy came back, try to cancel the timeout */
122303694eeSDan Williams wake_up(&sas_ha->eh_wait_q);
123303694eeSDan Williams return;
124303694eeSDan Williams } else {
12515ba7806SJohn Garry pr_info("%s: phy%d belongs to port%d already(%d)!\n",
126cadbd4a5SHarvey Harrison __func__, phy->id, phy->port->id,
1272908d778SJames Bottomley phy->port->num_phys);
1282908d778SJames Bottomley return;
1292908d778SJames Bottomley }
1302908d778SJames Bottomley }
1312908d778SJames Bottomley
1325381837fSTom Peng /* see if the phy should be part of a wide port */
133980fa2f9SDarrick J. Wong spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
1342908d778SJames Bottomley for (i = 0; i < sas_ha->num_phys; i++) {
1352908d778SJames Bottomley port = sas_ha->sas_port[i];
1362908d778SJames Bottomley spin_lock(&port->phy_list_lock);
1372908d778SJames Bottomley if (*(u64 *) port->sas_addr &&
13800f0254eSDan Williams phy_is_wideport_member(port, phy) && port->num_phys > 0) {
1392908d778SJames Bottomley /* wide port */
14032698c95SDamien Le Moal port_dev = port->port_dev;
14132698c95SDamien Le Moal sas_form_port_add_phy(port, phy, true);
14232698c95SDamien Le Moal spin_unlock(&port->phy_list_lock);
1432908d778SJames Bottomley break;
1445381837fSTom Peng }
1455381837fSTom Peng spin_unlock(&port->phy_list_lock);
1465381837fSTom Peng }
1475381837fSTom Peng /* The phy does not match any existing port, create a new one */
1485381837fSTom Peng if (i == sas_ha->num_phys) {
1495381837fSTom Peng for (i = 0; i < sas_ha->num_phys; i++) {
1505381837fSTom Peng port = sas_ha->sas_port[i];
1515381837fSTom Peng spin_lock(&port->phy_list_lock);
1525381837fSTom Peng if (*(u64 *)port->sas_addr == 0
1535381837fSTom Peng && port->num_phys == 0) {
15432698c95SDamien Le Moal port_dev = port->port_dev;
15532698c95SDamien Le Moal sas_form_port_add_phy(port, phy, false);
15632698c95SDamien Le Moal spin_unlock(&port->phy_list_lock);
1572908d778SJames Bottomley break;
1582908d778SJames Bottomley }
1592908d778SJames Bottomley spin_unlock(&port->phy_list_lock);
1602908d778SJames Bottomley }
1612908d778SJames Bottomley
1622908d778SJames Bottomley if (i >= sas_ha->num_phys) {
16332698c95SDamien Le Moal pr_err("%s: couldn't find a free port, bug?\n",
16432698c95SDamien Le Moal __func__);
165980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
1662908d778SJames Bottomley return;
1672908d778SJames Bottomley }
16832698c95SDamien Le Moal }
169980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
1702908d778SJames Bottomley
1712908d778SJames Bottomley if (!port->port) {
172b4698d88SDan Williams port->port = sas_port_alloc(phy->phy->dev.parent, port->id);
1732908d778SJames Bottomley BUG_ON(!port->port);
1742908d778SJames Bottomley sas_port_add(port->port);
1752908d778SJames Bottomley }
1762908d778SJames Bottomley sas_port_add_phy(port->port, phy->phy);
1772908d778SJames Bottomley
178b3e3d4c6SJohn Garry pr_debug("%s added to %s, phy_mask:0x%x (%016llx)\n",
17971610f55SKay Sievers dev_name(&phy->phy->dev), dev_name(&port->port->dev),
180a29c0515SJames Bottomley port->phy_mask,
181a29c0515SJames Bottomley SAS_ADDR(port->attached_sas_addr));
182a29c0515SJames Bottomley
183085f104aSJohn Garry if (port_dev)
184085f104aSJohn Garry port_dev->pathways = port->num_phys;
1852908d778SJames Bottomley
1862908d778SJames Bottomley /* Tell the LLDD about this port formation. */
1872908d778SJames Bottomley if (si->dft->lldd_port_formed)
1882908d778SJames Bottomley si->dft->lldd_port_formed(phy);
1892908d778SJames Bottomley
1902908d778SJames Bottomley sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN);
191085f104aSJohn Garry /* Only insert a revalidate event after initial discovery */
192924a3541SJohn Garry if (port_dev && dev_is_expander(port_dev->dev_type)) {
193085f104aSJohn Garry struct expander_device *ex_dev = &port_dev->ex_dev;
194085f104aSJohn Garry
195085f104aSJohn Garry ex_dev->ex_change_count = -1;
196085f104aSJohn Garry sas_discover_event(port, DISCE_REVALIDATE_DOMAIN);
197085f104aSJohn Garry }
198517e5153SJason Yan flush_workqueue(sas_ha->disco_q);
1992908d778SJames Bottomley }
2002908d778SJames Bottomley
2012908d778SJames Bottomley /**
202121246aeSBart Van Assche * sas_deform_port - remove this phy from the port it belongs to
2032908d778SJames Bottomley * @phy: the phy of interest
204121246aeSBart Van Assche * @gone: whether or not the PHY is gone
2052908d778SJames Bottomley *
2062908d778SJames Bottomley * This is called when the physical link to the other phy has been
2072908d778SJames Bottomley * lost (on this phy), in Event thread context. We cannot delay here.
2082908d778SJames Bottomley */
sas_deform_port(struct asd_sas_phy * phy,int gone)20990f1e10dSDan Williams void sas_deform_port(struct asd_sas_phy *phy, int gone)
2102908d778SJames Bottomley {
2112908d778SJames Bottomley struct sas_ha_struct *sas_ha = phy->ha;
2122908d778SJames Bottomley struct asd_sas_port *port = phy->port;
2132908d778SJames Bottomley struct sas_internal *si =
214*1136a022SJohn Garry to_sas_internal(sas_ha->shost->transportt);
21590f1e10dSDan Williams struct domain_device *dev;
216980fa2f9SDarrick J. Wong unsigned long flags;
2172908d778SJames Bottomley
2182908d778SJames Bottomley if (!port)
2192908d778SJames Bottomley return; /* done by a phy event */
2202908d778SJames Bottomley
22190f1e10dSDan Williams dev = port->port_dev;
22290f1e10dSDan Williams if (dev)
22390f1e10dSDan Williams dev->pathways--;
2242908d778SJames Bottomley
2252908d778SJames Bottomley if (port->num_phys == 1) {
2267d05919aSDan Williams sas_unregister_domain_devices(port, gone);
2270558f33cSJason Yan sas_destruct_devices(port);
2282908d778SJames Bottomley sas_port_delete(port->port);
2292908d778SJames Bottomley port->port = NULL;
230f41a0c44SDan Williams } else {
2312908d778SJames Bottomley sas_port_delete_phy(port->port, phy->phy);
232f41a0c44SDan Williams sas_device_set_phy(dev, port->port);
233f41a0c44SDan Williams }
2342908d778SJames Bottomley
2352908d778SJames Bottomley if (si->dft->lldd_port_deformed)
2362908d778SJames Bottomley si->dft->lldd_port_deformed(phy);
2372908d778SJames Bottomley
238980fa2f9SDarrick J. Wong spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
2392908d778SJames Bottomley spin_lock(&port->phy_list_lock);
2402908d778SJames Bottomley
2412908d778SJames Bottomley list_del_init(&phy->port_phy_el);
242899fcf40SDan Williams sas_phy_set_target(phy, NULL);
2432908d778SJames Bottomley phy->port = NULL;
2442908d778SJames Bottomley port->num_phys--;
2452908d778SJames Bottomley port->phy_mask &= ~(1U << phy->id);
2462908d778SJames Bottomley
2472908d778SJames Bottomley if (port->num_phys == 0) {
2482908d778SJames Bottomley INIT_LIST_HEAD(&port->phy_list);
2492908d778SJames Bottomley memset(port->sas_addr, 0, SAS_ADDR_SIZE);
2502908d778SJames Bottomley memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE);
2512908d778SJames Bottomley port->iproto = 0;
2522908d778SJames Bottomley port->tproto = 0;
2532908d778SJames Bottomley port->oob_mode = 0;
2542908d778SJames Bottomley port->phy_mask = 0;
2552908d778SJames Bottomley }
2562908d778SJames Bottomley spin_unlock(&port->phy_list_lock);
257980fa2f9SDarrick J. Wong spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
2582908d778SJames Bottomley
259085f104aSJohn Garry /* Only insert revalidate event if the port still has members */
260924a3541SJohn Garry if (port->port && dev && dev_is_expander(dev->dev_type)) {
261085f104aSJohn Garry struct expander_device *ex_dev = &dev->ex_dev;
262085f104aSJohn Garry
263085f104aSJohn Garry ex_dev->ex_change_count = -1;
264085f104aSJohn Garry sas_discover_event(port, DISCE_REVALIDATE_DOMAIN);
265085f104aSJohn Garry }
266085f104aSJohn Garry flush_workqueue(sas_ha->disco_q);
267085f104aSJohn Garry
2682908d778SJames Bottomley return;
2692908d778SJames Bottomley }
2702908d778SJames Bottomley
2712908d778SJames Bottomley /* ---------- SAS port events ---------- */
2722908d778SJames Bottomley
sas_porte_bytes_dmaed(struct work_struct * work)273c4028958SDavid Howells void sas_porte_bytes_dmaed(struct work_struct *work)
2742908d778SJames Bottomley {
27522b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work);
276c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy;
2772908d778SJames Bottomley
2782908d778SJames Bottomley sas_form_port(phy);
2792908d778SJames Bottomley }
2802908d778SJames Bottomley
sas_porte_broadcast_rcvd(struct work_struct * work)281c4028958SDavid Howells void sas_porte_broadcast_rcvd(struct work_struct *work)
2822908d778SJames Bottomley {
28322b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work);
284c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy;
2852908d778SJames Bottomley unsigned long flags;
2862908d778SJames Bottomley u32 prim;
2872908d778SJames Bottomley
2882908d778SJames Bottomley spin_lock_irqsave(&phy->sas_prim_lock, flags);
2892908d778SJames Bottomley prim = phy->sas_prim;
2902908d778SJames Bottomley spin_unlock_irqrestore(&phy->sas_prim_lock, flags);
2912908d778SJames Bottomley
29215ba7806SJohn Garry pr_debug("broadcast received: %d\n", prim);
2932908d778SJames Bottomley sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN);
294517e5153SJason Yan
295517e5153SJason Yan if (phy->port)
296517e5153SJason Yan flush_workqueue(phy->port->ha->disco_q);
2972908d778SJames Bottomley }
2982908d778SJames Bottomley
sas_porte_link_reset_err(struct work_struct * work)299c4028958SDavid Howells void sas_porte_link_reset_err(struct work_struct *work)
3002908d778SJames Bottomley {
30122b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work);
302c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy;
3032908d778SJames Bottomley
30490f1e10dSDan Williams sas_deform_port(phy, 1);
3052908d778SJames Bottomley }
3062908d778SJames Bottomley
sas_porte_timer_event(struct work_struct * work)307c4028958SDavid Howells void sas_porte_timer_event(struct work_struct *work)
3082908d778SJames Bottomley {
30922b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work);
310c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy;
3112908d778SJames Bottomley
31290f1e10dSDan Williams sas_deform_port(phy, 1);
3132908d778SJames Bottomley }
3142908d778SJames Bottomley
sas_porte_hard_reset(struct work_struct * work)315c4028958SDavid Howells void sas_porte_hard_reset(struct work_struct *work)
3162908d778SJames Bottomley {
31722b9153fSDan Williams struct asd_sas_event *ev = to_asd_sas_event(work);
318c4028958SDavid Howells struct asd_sas_phy *phy = ev->phy;
3192908d778SJames Bottomley
32090f1e10dSDan Williams sas_deform_port(phy, 1);
3212908d778SJames Bottomley }
3222908d778SJames Bottomley
3232908d778SJames Bottomley /* ---------- SAS port registration ---------- */
3242908d778SJames Bottomley
sas_init_port(struct asd_sas_port * port,struct sas_ha_struct * sas_ha,int i)3252908d778SJames Bottomley static void sas_init_port(struct asd_sas_port *port,
3262908d778SJames Bottomley struct sas_ha_struct *sas_ha, int i)
3272908d778SJames Bottomley {
328a29c0515SJames Bottomley memset(port, 0, sizeof(*port));
3292908d778SJames Bottomley port->id = i;
3302908d778SJames Bottomley INIT_LIST_HEAD(&port->dev_list);
33187c8331fSDan Williams INIT_LIST_HEAD(&port->disco_list);
33287c8331fSDan Williams INIT_LIST_HEAD(&port->destroy_list);
3330558f33cSJason Yan INIT_LIST_HEAD(&port->sas_port_del_list);
3342908d778SJames Bottomley spin_lock_init(&port->phy_list_lock);
3352908d778SJames Bottomley INIT_LIST_HEAD(&port->phy_list);
3362908d778SJames Bottomley port->ha = sas_ha;
3372908d778SJames Bottomley
3382908d778SJames Bottomley spin_lock_init(&port->dev_list_lock);
3392908d778SJames Bottomley }
3402908d778SJames Bottomley
sas_register_ports(struct sas_ha_struct * sas_ha)3412908d778SJames Bottomley int sas_register_ports(struct sas_ha_struct *sas_ha)
3422908d778SJames Bottomley {
3432908d778SJames Bottomley int i;
3442908d778SJames Bottomley
3452908d778SJames Bottomley /* initialize the ports and discovery */
3462908d778SJames Bottomley for (i = 0; i < sas_ha->num_phys; i++) {
3472908d778SJames Bottomley struct asd_sas_port *port = sas_ha->sas_port[i];
3482908d778SJames Bottomley
3492908d778SJames Bottomley sas_init_port(port, sas_ha, i);
3502908d778SJames Bottomley sas_init_disc(&port->disc, port);
3512908d778SJames Bottomley }
3522908d778SJames Bottomley return 0;
3532908d778SJames Bottomley }
3542908d778SJames Bottomley
sas_unregister_ports(struct sas_ha_struct * sas_ha)3552908d778SJames Bottomley void sas_unregister_ports(struct sas_ha_struct *sas_ha)
3562908d778SJames Bottomley {
3572908d778SJames Bottomley int i;
3582908d778SJames Bottomley
3592908d778SJames Bottomley for (i = 0; i < sas_ha->num_phys; i++)
3602908d778SJames Bottomley if (sas_ha->sas_phy[i]->port)
36190f1e10dSDan Williams sas_deform_port(sas_ha->sas_phy[i], 0);
3622908d778SJames Bottomley
3632908d778SJames Bottomley }
3641c393b97SJason Yan
3651c393b97SJason Yan const work_func_t sas_port_event_fns[PORT_NUM_EVENTS] = {
3661c393b97SJason Yan [PORTE_BYTES_DMAED] = sas_porte_bytes_dmaed,
3671c393b97SJason Yan [PORTE_BROADCAST_RCVD] = sas_porte_broadcast_rcvd,
3681c393b97SJason Yan [PORTE_LINK_RESET_ERR] = sas_porte_link_reset_err,
3691c393b97SJason Yan [PORTE_TIMER_EVENT] = sas_porte_timer_event,
3701c393b97SJason Yan [PORTE_HARD_RESET] = sas_porte_hard_reset,
3711c393b97SJason Yan };
372