xref: /openbmc/linux/drivers/scsi/libsas/sas_port.c (revision 121246ae)
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 
42303694eeSDan Williams static void sas_resume_port(struct asd_sas_phy *phy)
43303694eeSDan Williams {
44303694eeSDan Williams 	struct domain_device *dev;
45303694eeSDan Williams 	struct asd_sas_port *port = phy->port;
46303694eeSDan Williams 	struct sas_ha_struct *sas_ha = phy->ha;
47303694eeSDan Williams 	struct sas_internal *si = to_sas_internal(sas_ha->core.shost->transportt);
48303694eeSDan Williams 
49303694eeSDan Williams 	if (si->dft->lldd_port_formed)
50303694eeSDan Williams 		si->dft->lldd_port_formed(phy);
51303694eeSDan Williams 
52303694eeSDan Williams 	if (port->suspended)
53303694eeSDan Williams 		port->suspended = 0;
54303694eeSDan Williams 	else {
55303694eeSDan Williams 		/* we only need to handle "link returned" actions once */
56303694eeSDan Williams 		return;
57303694eeSDan Williams 	}
58303694eeSDan Williams 
59303694eeSDan Williams 	/* if the port came back:
60303694eeSDan Williams 	 * 1/ presume every device came back
61303694eeSDan Williams 	 * 2/ force the next revalidation to check all expander phys
62303694eeSDan Williams 	 */
63303694eeSDan Williams 	list_for_each_entry(dev, &port->dev_list, dev_list_node) {
64303694eeSDan Williams 		int i, rc;
65303694eeSDan Williams 
66303694eeSDan Williams 		rc = sas_notify_lldd_dev_found(dev);
67303694eeSDan Williams 		if (rc) {
68303694eeSDan Williams 			sas_unregister_dev(port, dev);
690558f33cSJason Yan 			sas_destruct_devices(port);
70303694eeSDan Williams 			continue;
71303694eeSDan Williams 		}
72303694eeSDan Williams 
73aa9f8328SJames Bottomley 		if (dev->dev_type == SAS_EDGE_EXPANDER_DEVICE || dev->dev_type == SAS_FANOUT_EXPANDER_DEVICE) {
74303694eeSDan Williams 			dev->ex_dev.ex_change_count = -1;
75303694eeSDan Williams 			for (i = 0; i < dev->ex_dev.num_phys; i++) {
76303694eeSDan Williams 				struct ex_phy *phy = &dev->ex_dev.ex_phy[i];
77303694eeSDan Williams 
78303694eeSDan Williams 				phy->phy_change_count = -1;
79303694eeSDan Williams 			}
80303694eeSDan Williams 		}
81303694eeSDan Williams 	}
82303694eeSDan Williams 
83303694eeSDan Williams 	sas_discover_event(port, DISCE_RESUME);
84303694eeSDan Williams }
85303694eeSDan Williams 
862908d778SJames Bottomley /**
87121246aeSBart Van Assche  * sas_form_port - add this phy to a port
882908d778SJames Bottomley  * @phy: the phy of interest
892908d778SJames Bottomley  *
902908d778SJames Bottomley  * This function adds this phy to an existing port, thus creating a wide
912908d778SJames Bottomley  * port, or it creates a port and adds the phy to the port.
922908d778SJames Bottomley  */
932908d778SJames Bottomley static void sas_form_port(struct asd_sas_phy *phy)
942908d778SJames Bottomley {
952908d778SJames Bottomley 	int i;
962908d778SJames Bottomley 	struct sas_ha_struct *sas_ha = phy->ha;
972908d778SJames Bottomley 	struct asd_sas_port *port = phy->port;
982908d778SJames Bottomley 	struct sas_internal *si =
992908d778SJames Bottomley 		to_sas_internal(sas_ha->core.shost->transportt);
100980fa2f9SDarrick J. Wong 	unsigned long flags;
1012908d778SJames Bottomley 
1022908d778SJames Bottomley 	if (port) {
10300f0254eSDan Williams 		if (!phy_is_wideport_member(port, phy))
10490f1e10dSDan Williams 			sas_deform_port(phy, 0);
105303694eeSDan Williams 		else if (phy->suspended) {
106303694eeSDan Williams 			phy->suspended = 0;
107303694eeSDan Williams 			sas_resume_port(phy);
108303694eeSDan Williams 
109303694eeSDan Williams 			/* phy came back, try to cancel the timeout */
110303694eeSDan Williams 			wake_up(&sas_ha->eh_wait_q);
111303694eeSDan Williams 			return;
112303694eeSDan Williams 		} else {
1132908d778SJames Bottomley 			SAS_DPRINTK("%s: phy%d belongs to port%d already(%d)!\n",
114cadbd4a5SHarvey Harrison 				    __func__, phy->id, phy->port->id,
1152908d778SJames Bottomley 				    phy->port->num_phys);
1162908d778SJames Bottomley 			return;
1172908d778SJames Bottomley 		}
1182908d778SJames Bottomley 	}
1192908d778SJames Bottomley 
1205381837fSTom Peng 	/* see if the phy should be part of a wide port */
121980fa2f9SDarrick J. Wong 	spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
1222908d778SJames Bottomley 	for (i = 0; i < sas_ha->num_phys; i++) {
1232908d778SJames Bottomley 		port = sas_ha->sas_port[i];
1242908d778SJames Bottomley 		spin_lock(&port->phy_list_lock);
1252908d778SJames Bottomley 		if (*(u64 *) port->sas_addr &&
12600f0254eSDan Williams 		    phy_is_wideport_member(port, phy) && port->num_phys > 0) {
1272908d778SJames Bottomley 			/* wide port */
1282908d778SJames Bottomley 			SAS_DPRINTK("phy%d matched wide port%d\n", phy->id,
1292908d778SJames Bottomley 				    port->id);
1302908d778SJames Bottomley 			break;
1315381837fSTom Peng 		}
1325381837fSTom Peng 		spin_unlock(&port->phy_list_lock);
1335381837fSTom Peng 	}
1345381837fSTom Peng 	/* The phy does not match any existing port, create a new one */
1355381837fSTom Peng 	if (i == sas_ha->num_phys) {
1365381837fSTom Peng 		for (i = 0; i < sas_ha->num_phys; i++) {
1375381837fSTom Peng 			port = sas_ha->sas_port[i];
1385381837fSTom Peng 			spin_lock(&port->phy_list_lock);
1395381837fSTom Peng 			if (*(u64 *)port->sas_addr == 0
1405381837fSTom Peng 				&& port->num_phys == 0) {
1415381837fSTom Peng 				memcpy(port->sas_addr, phy->sas_addr,
1425381837fSTom Peng 					SAS_ADDR_SIZE);
1432908d778SJames Bottomley 				break;
1442908d778SJames Bottomley 			}
1452908d778SJames Bottomley 			spin_unlock(&port->phy_list_lock);
1462908d778SJames Bottomley 		}
1475381837fSTom Peng 	}
1482908d778SJames Bottomley 
1492908d778SJames Bottomley 	if (i >= sas_ha->num_phys) {
1502908d778SJames Bottomley 		printk(KERN_NOTICE "%s: couldn't find a free port, bug?\n",
151cadbd4a5SHarvey Harrison 		       __func__);
152980fa2f9SDarrick J. Wong 		spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
1532908d778SJames Bottomley 		return;
1542908d778SJames Bottomley 	}
1552908d778SJames Bottomley 
1562908d778SJames Bottomley 	/* add the phy to the port */
1572908d778SJames Bottomley 	list_add_tail(&phy->port_phy_el, &port->phy_list);
158899fcf40SDan Williams 	sas_phy_set_target(phy, port->port_dev);
1592908d778SJames Bottomley 	phy->port = port;
1602908d778SJames Bottomley 	port->num_phys++;
1612908d778SJames Bottomley 	port->phy_mask |= (1U << phy->id);
1622908d778SJames Bottomley 
1632908d778SJames Bottomley 	if (*(u64 *)port->attached_sas_addr == 0) {
1642908d778SJames Bottomley 		port->class = phy->class;
1652908d778SJames Bottomley 		memcpy(port->attached_sas_addr, phy->attached_sas_addr,
1662908d778SJames Bottomley 		       SAS_ADDR_SIZE);
1672908d778SJames Bottomley 		port->iproto = phy->iproto;
1682908d778SJames Bottomley 		port->tproto = phy->tproto;
1692908d778SJames Bottomley 		port->oob_mode = phy->oob_mode;
1702908d778SJames Bottomley 		port->linkrate = phy->linkrate;
1712908d778SJames Bottomley 	} else
1722908d778SJames Bottomley 		port->linkrate = max(port->linkrate, phy->linkrate);
1732908d778SJames Bottomley 	spin_unlock(&port->phy_list_lock);
174980fa2f9SDarrick J. Wong 	spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
1752908d778SJames Bottomley 
1762908d778SJames Bottomley 	if (!port->port) {
177b4698d88SDan Williams 		port->port = sas_port_alloc(phy->phy->dev.parent, port->id);
1782908d778SJames Bottomley 		BUG_ON(!port->port);
1792908d778SJames Bottomley 		sas_port_add(port->port);
1802908d778SJames Bottomley 	}
1812908d778SJames Bottomley 	sas_port_add_phy(port->port, phy->phy);
1822908d778SJames Bottomley 
183a29c0515SJames Bottomley 	SAS_DPRINTK("%s added to %s, phy_mask:0x%x (%16llx)\n",
18471610f55SKay Sievers 		    dev_name(&phy->phy->dev), dev_name(&port->port->dev),
185a29c0515SJames Bottomley 		    port->phy_mask,
186a29c0515SJames Bottomley 		    SAS_ADDR(port->attached_sas_addr));
187a29c0515SJames Bottomley 
1882908d778SJames Bottomley 	if (port->port_dev)
1892908d778SJames Bottomley 		port->port_dev->pathways = port->num_phys;
1902908d778SJames Bottomley 
1912908d778SJames Bottomley 	/* Tell the LLDD about this port formation. */
1922908d778SJames Bottomley 	if (si->dft->lldd_port_formed)
1932908d778SJames Bottomley 		si->dft->lldd_port_formed(phy);
1942908d778SJames Bottomley 
1952908d778SJames Bottomley 	sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN);
196517e5153SJason Yan 	flush_workqueue(sas_ha->disco_q);
1972908d778SJames Bottomley }
1982908d778SJames Bottomley 
1992908d778SJames Bottomley /**
200121246aeSBart Van Assche  * sas_deform_port - remove this phy from the port it belongs to
2012908d778SJames Bottomley  * @phy: the phy of interest
202121246aeSBart Van Assche  * @gone: whether or not the PHY is gone
2032908d778SJames Bottomley  *
2042908d778SJames Bottomley  * This is called when the physical link to the other phy has been
2052908d778SJames Bottomley  * lost (on this phy), in Event thread context. We cannot delay here.
2062908d778SJames Bottomley  */
20790f1e10dSDan Williams void sas_deform_port(struct asd_sas_phy *phy, int gone)
2082908d778SJames Bottomley {
2092908d778SJames Bottomley 	struct sas_ha_struct *sas_ha = phy->ha;
2102908d778SJames Bottomley 	struct asd_sas_port *port = phy->port;
2112908d778SJames Bottomley 	struct sas_internal *si =
2122908d778SJames Bottomley 		to_sas_internal(sas_ha->core.shost->transportt);
21390f1e10dSDan Williams 	struct domain_device *dev;
214980fa2f9SDarrick J. Wong 	unsigned long flags;
2152908d778SJames Bottomley 
2162908d778SJames Bottomley 	if (!port)
2172908d778SJames Bottomley 		return;		  /* done by a phy event */
2182908d778SJames Bottomley 
21990f1e10dSDan Williams 	dev = port->port_dev;
22090f1e10dSDan Williams 	if (dev)
22190f1e10dSDan Williams 		dev->pathways--;
2222908d778SJames Bottomley 
2232908d778SJames Bottomley 	if (port->num_phys == 1) {
2247d05919aSDan Williams 		sas_unregister_domain_devices(port, gone);
2250558f33cSJason Yan 		sas_destruct_devices(port);
2262908d778SJames Bottomley 		sas_port_delete(port->port);
2272908d778SJames Bottomley 		port->port = NULL;
228f41a0c44SDan Williams 	} else {
2292908d778SJames Bottomley 		sas_port_delete_phy(port->port, phy->phy);
230f41a0c44SDan Williams 		sas_device_set_phy(dev, port->port);
231f41a0c44SDan Williams 	}
2322908d778SJames Bottomley 
2332908d778SJames Bottomley 	if (si->dft->lldd_port_deformed)
2342908d778SJames Bottomley 		si->dft->lldd_port_deformed(phy);
2352908d778SJames Bottomley 
236980fa2f9SDarrick J. Wong 	spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
2372908d778SJames Bottomley 	spin_lock(&port->phy_list_lock);
2382908d778SJames Bottomley 
2392908d778SJames Bottomley 	list_del_init(&phy->port_phy_el);
240899fcf40SDan Williams 	sas_phy_set_target(phy, NULL);
2412908d778SJames Bottomley 	phy->port = NULL;
2422908d778SJames Bottomley 	port->num_phys--;
2432908d778SJames Bottomley 	port->phy_mask &= ~(1U << phy->id);
2442908d778SJames Bottomley 
2452908d778SJames Bottomley 	if (port->num_phys == 0) {
2462908d778SJames Bottomley 		INIT_LIST_HEAD(&port->phy_list);
2472908d778SJames Bottomley 		memset(port->sas_addr, 0, SAS_ADDR_SIZE);
2482908d778SJames Bottomley 		memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE);
2492908d778SJames Bottomley 		port->class = 0;
2502908d778SJames Bottomley 		port->iproto = 0;
2512908d778SJames Bottomley 		port->tproto = 0;
2522908d778SJames Bottomley 		port->oob_mode = 0;
2532908d778SJames Bottomley 		port->phy_mask = 0;
2542908d778SJames Bottomley 	}
2552908d778SJames Bottomley 	spin_unlock(&port->phy_list_lock);
256980fa2f9SDarrick J. Wong 	spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
2572908d778SJames Bottomley 
2582908d778SJames Bottomley 	return;
2592908d778SJames Bottomley }
2602908d778SJames Bottomley 
2612908d778SJames Bottomley /* ---------- SAS port events ---------- */
2622908d778SJames Bottomley 
263c4028958SDavid Howells void sas_porte_bytes_dmaed(struct work_struct *work)
2642908d778SJames Bottomley {
26522b9153fSDan Williams 	struct asd_sas_event *ev = to_asd_sas_event(work);
266c4028958SDavid Howells 	struct asd_sas_phy *phy = ev->phy;
2672908d778SJames Bottomley 
2682908d778SJames Bottomley 	sas_form_port(phy);
2692908d778SJames Bottomley }
2702908d778SJames Bottomley 
271c4028958SDavid Howells void sas_porte_broadcast_rcvd(struct work_struct *work)
2722908d778SJames Bottomley {
27322b9153fSDan Williams 	struct asd_sas_event *ev = to_asd_sas_event(work);
274c4028958SDavid Howells 	struct asd_sas_phy *phy = ev->phy;
2752908d778SJames Bottomley 	unsigned long flags;
2762908d778SJames Bottomley 	u32 prim;
2772908d778SJames Bottomley 
2782908d778SJames Bottomley 	spin_lock_irqsave(&phy->sas_prim_lock, flags);
2792908d778SJames Bottomley 	prim = phy->sas_prim;
2802908d778SJames Bottomley 	spin_unlock_irqrestore(&phy->sas_prim_lock, flags);
2812908d778SJames Bottomley 
2822908d778SJames Bottomley 	SAS_DPRINTK("broadcast received: %d\n", prim);
2832908d778SJames Bottomley 	sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN);
284517e5153SJason Yan 
285517e5153SJason Yan 	if (phy->port)
286517e5153SJason Yan 		flush_workqueue(phy->port->ha->disco_q);
2872908d778SJames Bottomley }
2882908d778SJames Bottomley 
289c4028958SDavid Howells void sas_porte_link_reset_err(struct work_struct *work)
2902908d778SJames Bottomley {
29122b9153fSDan Williams 	struct asd_sas_event *ev = to_asd_sas_event(work);
292c4028958SDavid Howells 	struct asd_sas_phy *phy = ev->phy;
2932908d778SJames Bottomley 
29490f1e10dSDan Williams 	sas_deform_port(phy, 1);
2952908d778SJames Bottomley }
2962908d778SJames Bottomley 
297c4028958SDavid Howells void sas_porte_timer_event(struct work_struct *work)
2982908d778SJames Bottomley {
29922b9153fSDan Williams 	struct asd_sas_event *ev = to_asd_sas_event(work);
300c4028958SDavid Howells 	struct asd_sas_phy *phy = ev->phy;
3012908d778SJames Bottomley 
30290f1e10dSDan Williams 	sas_deform_port(phy, 1);
3032908d778SJames Bottomley }
3042908d778SJames Bottomley 
305c4028958SDavid Howells void sas_porte_hard_reset(struct work_struct *work)
3062908d778SJames Bottomley {
30722b9153fSDan Williams 	struct asd_sas_event *ev = to_asd_sas_event(work);
308c4028958SDavid Howells 	struct asd_sas_phy *phy = ev->phy;
3092908d778SJames Bottomley 
31090f1e10dSDan Williams 	sas_deform_port(phy, 1);
3112908d778SJames Bottomley }
3122908d778SJames Bottomley 
3132908d778SJames Bottomley /* ---------- SAS port registration ---------- */
3142908d778SJames Bottomley 
3152908d778SJames Bottomley static void sas_init_port(struct asd_sas_port *port,
3162908d778SJames Bottomley 			  struct sas_ha_struct *sas_ha, int i)
3172908d778SJames Bottomley {
318a29c0515SJames Bottomley 	memset(port, 0, sizeof(*port));
3192908d778SJames Bottomley 	port->id = i;
3202908d778SJames Bottomley 	INIT_LIST_HEAD(&port->dev_list);
32187c8331fSDan Williams 	INIT_LIST_HEAD(&port->disco_list);
32287c8331fSDan Williams 	INIT_LIST_HEAD(&port->destroy_list);
3230558f33cSJason Yan 	INIT_LIST_HEAD(&port->sas_port_del_list);
3242908d778SJames Bottomley 	spin_lock_init(&port->phy_list_lock);
3252908d778SJames Bottomley 	INIT_LIST_HEAD(&port->phy_list);
3262908d778SJames Bottomley 	port->ha = sas_ha;
3272908d778SJames Bottomley 
3282908d778SJames Bottomley 	spin_lock_init(&port->dev_list_lock);
3292908d778SJames Bottomley }
3302908d778SJames Bottomley 
3312908d778SJames Bottomley int sas_register_ports(struct sas_ha_struct *sas_ha)
3322908d778SJames Bottomley {
3332908d778SJames Bottomley 	int i;
3342908d778SJames Bottomley 
3352908d778SJames Bottomley 	/* initialize the ports and discovery */
3362908d778SJames Bottomley 	for (i = 0; i < sas_ha->num_phys; i++) {
3372908d778SJames Bottomley 		struct asd_sas_port *port = sas_ha->sas_port[i];
3382908d778SJames Bottomley 
3392908d778SJames Bottomley 		sas_init_port(port, sas_ha, i);
3402908d778SJames Bottomley 		sas_init_disc(&port->disc, port);
3412908d778SJames Bottomley 	}
3422908d778SJames Bottomley 	return 0;
3432908d778SJames Bottomley }
3442908d778SJames Bottomley 
3452908d778SJames Bottomley void sas_unregister_ports(struct sas_ha_struct *sas_ha)
3462908d778SJames Bottomley {
3472908d778SJames Bottomley 	int i;
3482908d778SJames Bottomley 
3492908d778SJames Bottomley 	for (i = 0; i < sas_ha->num_phys; i++)
3502908d778SJames Bottomley 		if (sas_ha->sas_phy[i]->port)
35190f1e10dSDan Williams 			sas_deform_port(sas_ha->sas_phy[i], 0);
3522908d778SJames Bottomley 
3532908d778SJames Bottomley }
3541c393b97SJason Yan 
3551c393b97SJason Yan const work_func_t sas_port_event_fns[PORT_NUM_EVENTS] = {
3561c393b97SJason Yan 	[PORTE_BYTES_DMAED] = sas_porte_bytes_dmaed,
3571c393b97SJason Yan 	[PORTE_BROADCAST_RCVD] = sas_porte_broadcast_rcvd,
3581c393b97SJason Yan 	[PORTE_LINK_RESET_ERR] = sas_porte_link_reset_err,
3591c393b97SJason Yan 	[PORTE_TIMER_EVENT] = sas_porte_timer_event,
3601c393b97SJason Yan 	[PORTE_HARD_RESET] = sas_porte_hard_reset,
3611c393b97SJason Yan };
362