xref: /openbmc/linux/drivers/scsi/libsas/sas_port.c (revision 22b9153f)
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);
107899fcf40SDan Williams 	sas_phy_set_target(phy, port->port_dev);
1082908d778SJames Bottomley 	phy->port = port;
1092908d778SJames Bottomley 	port->num_phys++;
1102908d778SJames Bottomley 	port->phy_mask |= (1U << phy->id);
1112908d778SJames Bottomley 
1122908d778SJames Bottomley 	if (*(u64 *)port->attached_sas_addr == 0) {
1132908d778SJames Bottomley 		port->class = phy->class;
1142908d778SJames Bottomley 		memcpy(port->attached_sas_addr, phy->attached_sas_addr,
1152908d778SJames Bottomley 		       SAS_ADDR_SIZE);
1162908d778SJames Bottomley 		port->iproto = phy->iproto;
1172908d778SJames Bottomley 		port->tproto = phy->tproto;
1182908d778SJames Bottomley 		port->oob_mode = phy->oob_mode;
1192908d778SJames Bottomley 		port->linkrate = phy->linkrate;
1202908d778SJames Bottomley 	} else
1212908d778SJames Bottomley 		port->linkrate = max(port->linkrate, phy->linkrate);
1222908d778SJames Bottomley 	spin_unlock(&port->phy_list_lock);
123980fa2f9SDarrick J. Wong 	spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
1242908d778SJames Bottomley 
1252908d778SJames Bottomley 	if (!port->port) {
126a692b0eeSDan Williams 		port->port = sas_port_alloc(phy->phy->dev.parent, phy->id);
1272908d778SJames Bottomley 		BUG_ON(!port->port);
1282908d778SJames Bottomley 		sas_port_add(port->port);
1292908d778SJames Bottomley 	}
1302908d778SJames Bottomley 	sas_port_add_phy(port->port, phy->phy);
1312908d778SJames Bottomley 
132a29c0515SJames Bottomley 	SAS_DPRINTK("%s added to %s, phy_mask:0x%x (%16llx)\n",
13371610f55SKay Sievers 		    dev_name(&phy->phy->dev), dev_name(&port->port->dev),
134a29c0515SJames Bottomley 		    port->phy_mask,
135a29c0515SJames Bottomley 		    SAS_ADDR(port->attached_sas_addr));
136a29c0515SJames Bottomley 
1372908d778SJames Bottomley 	if (port->port_dev)
1382908d778SJames Bottomley 		port->port_dev->pathways = port->num_phys;
1392908d778SJames Bottomley 
1402908d778SJames Bottomley 	/* Tell the LLDD about this port formation. */
1412908d778SJames Bottomley 	if (si->dft->lldd_port_formed)
1422908d778SJames Bottomley 		si->dft->lldd_port_formed(phy);
1432908d778SJames Bottomley 
1442908d778SJames Bottomley 	sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN);
1452908d778SJames Bottomley }
1462908d778SJames Bottomley 
1472908d778SJames Bottomley /**
1482908d778SJames Bottomley  * sas_deform_port -- remove this phy from the port it belongs to
1492908d778SJames Bottomley  * @phy: the phy of interest
1502908d778SJames Bottomley  *
1512908d778SJames Bottomley  * This is called when the physical link to the other phy has been
1522908d778SJames Bottomley  * lost (on this phy), in Event thread context. We cannot delay here.
1532908d778SJames Bottomley  */
15490f1e10dSDan Williams void sas_deform_port(struct asd_sas_phy *phy, int gone)
1552908d778SJames Bottomley {
1562908d778SJames Bottomley 	struct sas_ha_struct *sas_ha = phy->ha;
1572908d778SJames Bottomley 	struct asd_sas_port *port = phy->port;
1582908d778SJames Bottomley 	struct sas_internal *si =
1592908d778SJames Bottomley 		to_sas_internal(sas_ha->core.shost->transportt);
16090f1e10dSDan Williams 	struct domain_device *dev;
161980fa2f9SDarrick J. Wong 	unsigned long flags;
1622908d778SJames Bottomley 
1632908d778SJames Bottomley 	if (!port)
1642908d778SJames Bottomley 		return;		  /* done by a phy event */
1652908d778SJames Bottomley 
16690f1e10dSDan Williams 	dev = port->port_dev;
16790f1e10dSDan Williams 	if (dev)
16890f1e10dSDan Williams 		dev->pathways--;
1692908d778SJames Bottomley 
1702908d778SJames Bottomley 	if (port->num_phys == 1) {
1717d05919aSDan Williams 		sas_unregister_domain_devices(port, gone);
1722908d778SJames Bottomley 		sas_port_delete(port->port);
1732908d778SJames Bottomley 		port->port = NULL;
174f41a0c44SDan Williams 	} else {
1752908d778SJames Bottomley 		sas_port_delete_phy(port->port, phy->phy);
176f41a0c44SDan Williams 		sas_device_set_phy(dev, port->port);
177f41a0c44SDan Williams 	}
1782908d778SJames Bottomley 
1792908d778SJames Bottomley 	if (si->dft->lldd_port_deformed)
1802908d778SJames Bottomley 		si->dft->lldd_port_deformed(phy);
1812908d778SJames Bottomley 
182980fa2f9SDarrick J. Wong 	spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
1832908d778SJames Bottomley 	spin_lock(&port->phy_list_lock);
1842908d778SJames Bottomley 
1852908d778SJames Bottomley 	list_del_init(&phy->port_phy_el);
186899fcf40SDan Williams 	sas_phy_set_target(phy, NULL);
1872908d778SJames Bottomley 	phy->port = NULL;
1882908d778SJames Bottomley 	port->num_phys--;
1892908d778SJames Bottomley 	port->phy_mask &= ~(1U << phy->id);
1902908d778SJames Bottomley 
1912908d778SJames Bottomley 	if (port->num_phys == 0) {
1922908d778SJames Bottomley 		INIT_LIST_HEAD(&port->phy_list);
1932908d778SJames Bottomley 		memset(port->sas_addr, 0, SAS_ADDR_SIZE);
1942908d778SJames Bottomley 		memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE);
1952908d778SJames Bottomley 		port->class = 0;
1962908d778SJames Bottomley 		port->iproto = 0;
1972908d778SJames Bottomley 		port->tproto = 0;
1982908d778SJames Bottomley 		port->oob_mode = 0;
1992908d778SJames Bottomley 		port->phy_mask = 0;
2002908d778SJames Bottomley 	}
2012908d778SJames Bottomley 	spin_unlock(&port->phy_list_lock);
202980fa2f9SDarrick J. Wong 	spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
2032908d778SJames Bottomley 
2042908d778SJames Bottomley 	return;
2052908d778SJames Bottomley }
2062908d778SJames Bottomley 
2072908d778SJames Bottomley /* ---------- SAS port events ---------- */
2082908d778SJames Bottomley 
209c4028958SDavid Howells void sas_porte_bytes_dmaed(struct work_struct *work)
2102908d778SJames Bottomley {
21122b9153fSDan Williams 	struct asd_sas_event *ev = to_asd_sas_event(work);
212c4028958SDavid Howells 	struct asd_sas_phy *phy = ev->phy;
2132908d778SJames Bottomley 
214b15ebe0bSDan Williams 	clear_bit(PORTE_BYTES_DMAED, &phy->port_events_pending);
2152908d778SJames Bottomley 
2162908d778SJames Bottomley 	sas_form_port(phy);
2172908d778SJames Bottomley }
2182908d778SJames Bottomley 
219c4028958SDavid Howells void sas_porte_broadcast_rcvd(struct work_struct *work)
2202908d778SJames Bottomley {
22122b9153fSDan Williams 	struct asd_sas_event *ev = to_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 {
23822b9153fSDan Williams 	struct asd_sas_event *ev = to_asd_sas_event(work);
239c4028958SDavid Howells 	struct asd_sas_phy *phy = ev->phy;
2402908d778SJames Bottomley 
241b15ebe0bSDan Williams 	clear_bit(PORTE_LINK_RESET_ERR, &phy->port_events_pending);
2422908d778SJames Bottomley 
24390f1e10dSDan Williams 	sas_deform_port(phy, 1);
2442908d778SJames Bottomley }
2452908d778SJames Bottomley 
246c4028958SDavid Howells void sas_porte_timer_event(struct work_struct *work)
2472908d778SJames Bottomley {
24822b9153fSDan Williams 	struct asd_sas_event *ev = to_asd_sas_event(work);
249c4028958SDavid Howells 	struct asd_sas_phy *phy = ev->phy;
2502908d778SJames Bottomley 
251b15ebe0bSDan Williams 	clear_bit(PORTE_TIMER_EVENT, &phy->port_events_pending);
2522908d778SJames Bottomley 
25390f1e10dSDan Williams 	sas_deform_port(phy, 1);
2542908d778SJames Bottomley }
2552908d778SJames Bottomley 
256c4028958SDavid Howells void sas_porte_hard_reset(struct work_struct *work)
2572908d778SJames Bottomley {
25822b9153fSDan Williams 	struct asd_sas_event *ev = to_asd_sas_event(work);
259c4028958SDavid Howells 	struct asd_sas_phy *phy = ev->phy;
2602908d778SJames Bottomley 
261b15ebe0bSDan Williams 	clear_bit(PORTE_HARD_RESET, &phy->port_events_pending);
2622908d778SJames Bottomley 
26390f1e10dSDan Williams 	sas_deform_port(phy, 1);
2642908d778SJames Bottomley }
2652908d778SJames Bottomley 
2662908d778SJames Bottomley /* ---------- SAS port registration ---------- */
2672908d778SJames Bottomley 
2682908d778SJames Bottomley static void sas_init_port(struct asd_sas_port *port,
2692908d778SJames Bottomley 			  struct sas_ha_struct *sas_ha, int i)
2702908d778SJames Bottomley {
271a29c0515SJames Bottomley 	memset(port, 0, sizeof(*port));
2722908d778SJames Bottomley 	port->id = i;
2732908d778SJames Bottomley 	INIT_LIST_HEAD(&port->dev_list);
27487c8331fSDan Williams 	INIT_LIST_HEAD(&port->disco_list);
27587c8331fSDan Williams 	INIT_LIST_HEAD(&port->destroy_list);
2762908d778SJames Bottomley 	spin_lock_init(&port->phy_list_lock);
2772908d778SJames Bottomley 	INIT_LIST_HEAD(&port->phy_list);
2782908d778SJames Bottomley 	port->ha = sas_ha;
2792908d778SJames Bottomley 
2802908d778SJames Bottomley 	spin_lock_init(&port->dev_list_lock);
2812908d778SJames Bottomley }
2822908d778SJames Bottomley 
2832908d778SJames Bottomley int sas_register_ports(struct sas_ha_struct *sas_ha)
2842908d778SJames Bottomley {
2852908d778SJames Bottomley 	int i;
2862908d778SJames Bottomley 
2872908d778SJames Bottomley 	/* initialize the ports and discovery */
2882908d778SJames Bottomley 	for (i = 0; i < sas_ha->num_phys; i++) {
2892908d778SJames Bottomley 		struct asd_sas_port *port = sas_ha->sas_port[i];
2902908d778SJames Bottomley 
2912908d778SJames Bottomley 		sas_init_port(port, sas_ha, i);
2922908d778SJames Bottomley 		sas_init_disc(&port->disc, port);
2932908d778SJames Bottomley 	}
2942908d778SJames Bottomley 	return 0;
2952908d778SJames Bottomley }
2962908d778SJames Bottomley 
2972908d778SJames Bottomley void sas_unregister_ports(struct sas_ha_struct *sas_ha)
2982908d778SJames Bottomley {
2992908d778SJames Bottomley 	int i;
3002908d778SJames Bottomley 
3012908d778SJames Bottomley 	for (i = 0; i < sas_ha->num_phys; i++)
3022908d778SJames Bottomley 		if (sas_ha->sas_phy[i]->port)
30390f1e10dSDan Williams 			sas_deform_port(sas_ha->sas_phy[i], 0);
3042908d778SJames Bottomley 
3052908d778SJames Bottomley }
306