xref: /openbmc/linux/drivers/scsi/libsas/sas_port.c (revision 00f0254e)
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))
602908d778SJames Bottomley 			sas_deform_port(phy);
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);
1072908d778SJames Bottomley 	phy->port = port;
1082908d778SJames Bottomley 	port->num_phys++;
1092908d778SJames Bottomley 	port->phy_mask |= (1U << phy->id);
1102908d778SJames Bottomley 
1112908d778SJames Bottomley 	if (!port->phy)
1122908d778SJames Bottomley 		port->phy = phy->phy;
1132908d778SJames Bottomley 
1142908d778SJames Bottomley 	if (*(u64 *)port->attached_sas_addr == 0) {
1152908d778SJames Bottomley 		port->class = phy->class;
1162908d778SJames Bottomley 		memcpy(port->attached_sas_addr, phy->attached_sas_addr,
1172908d778SJames Bottomley 		       SAS_ADDR_SIZE);
1182908d778SJames Bottomley 		port->iproto = phy->iproto;
1192908d778SJames Bottomley 		port->tproto = phy->tproto;
1202908d778SJames Bottomley 		port->oob_mode = phy->oob_mode;
1212908d778SJames Bottomley 		port->linkrate = phy->linkrate;
1222908d778SJames Bottomley 	} else
1232908d778SJames Bottomley 		port->linkrate = max(port->linkrate, phy->linkrate);
1242908d778SJames Bottomley 	spin_unlock(&port->phy_list_lock);
125980fa2f9SDarrick J. Wong 	spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
1262908d778SJames Bottomley 
1272908d778SJames Bottomley 	if (!port->port) {
1282908d778SJames Bottomley 		port->port = sas_port_alloc(phy->phy->dev.parent, port->id);
1292908d778SJames Bottomley 		BUG_ON(!port->port);
1302908d778SJames Bottomley 		sas_port_add(port->port);
1312908d778SJames Bottomley 	}
1322908d778SJames Bottomley 	sas_port_add_phy(port->port, phy->phy);
1332908d778SJames Bottomley 
134a29c0515SJames Bottomley 	SAS_DPRINTK("%s added to %s, phy_mask:0x%x (%16llx)\n",
13571610f55SKay Sievers 		    dev_name(&phy->phy->dev), dev_name(&port->port->dev),
136a29c0515SJames Bottomley 		    port->phy_mask,
137a29c0515SJames Bottomley 		    SAS_ADDR(port->attached_sas_addr));
138a29c0515SJames Bottomley 
1392908d778SJames Bottomley 	if (port->port_dev)
1402908d778SJames Bottomley 		port->port_dev->pathways = port->num_phys;
1412908d778SJames Bottomley 
1422908d778SJames Bottomley 	/* Tell the LLDD about this port formation. */
1432908d778SJames Bottomley 	if (si->dft->lldd_port_formed)
1442908d778SJames Bottomley 		si->dft->lldd_port_formed(phy);
1452908d778SJames Bottomley 
1462908d778SJames Bottomley 	sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN);
1472908d778SJames Bottomley }
1482908d778SJames Bottomley 
1492908d778SJames Bottomley /**
1502908d778SJames Bottomley  * sas_deform_port -- remove this phy from the port it belongs to
1512908d778SJames Bottomley  * @phy: the phy of interest
1522908d778SJames Bottomley  *
1532908d778SJames Bottomley  * This is called when the physical link to the other phy has been
1542908d778SJames Bottomley  * lost (on this phy), in Event thread context. We cannot delay here.
1552908d778SJames Bottomley  */
1562908d778SJames Bottomley void sas_deform_port(struct asd_sas_phy *phy)
1572908d778SJames Bottomley {
1582908d778SJames Bottomley 	struct sas_ha_struct *sas_ha = phy->ha;
1592908d778SJames Bottomley 	struct asd_sas_port *port = phy->port;
1602908d778SJames Bottomley 	struct sas_internal *si =
1612908d778SJames Bottomley 		to_sas_internal(sas_ha->core.shost->transportt);
162980fa2f9SDarrick J. Wong 	unsigned long flags;
1632908d778SJames Bottomley 
1642908d778SJames Bottomley 	if (!port)
1652908d778SJames Bottomley 		return;		  /* done by a phy event */
1662908d778SJames Bottomley 
1672908d778SJames Bottomley 	if (port->port_dev)
1682908d778SJames Bottomley 		port->port_dev->pathways--;
1692908d778SJames Bottomley 
1702908d778SJames Bottomley 	if (port->num_phys == 1) {
1712908d778SJames Bottomley 		sas_unregister_domain_devices(port);
1722908d778SJames Bottomley 		sas_port_delete(port->port);
1732908d778SJames Bottomley 		port->port = NULL;
1742908d778SJames Bottomley 	} else
1752908d778SJames Bottomley 		sas_port_delete_phy(port->port, phy->phy);
1762908d778SJames Bottomley 
1772908d778SJames Bottomley 
1782908d778SJames Bottomley 	if (si->dft->lldd_port_deformed)
1792908d778SJames Bottomley 		si->dft->lldd_port_deformed(phy);
1802908d778SJames Bottomley 
181980fa2f9SDarrick J. Wong 	spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
1822908d778SJames Bottomley 	spin_lock(&port->phy_list_lock);
1832908d778SJames Bottomley 
1842908d778SJames Bottomley 	list_del_init(&phy->port_phy_el);
1852908d778SJames Bottomley 	phy->port = NULL;
1862908d778SJames Bottomley 	port->num_phys--;
1872908d778SJames Bottomley 	port->phy_mask &= ~(1U << phy->id);
1882908d778SJames Bottomley 
1892908d778SJames Bottomley 	if (port->num_phys == 0) {
1902908d778SJames Bottomley 		INIT_LIST_HEAD(&port->phy_list);
1912908d778SJames Bottomley 		memset(port->sas_addr, 0, SAS_ADDR_SIZE);
1922908d778SJames Bottomley 		memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE);
1932908d778SJames Bottomley 		port->class = 0;
1942908d778SJames Bottomley 		port->iproto = 0;
1952908d778SJames Bottomley 		port->tproto = 0;
1962908d778SJames Bottomley 		port->oob_mode = 0;
1972908d778SJames Bottomley 		port->phy_mask = 0;
1982908d778SJames Bottomley 	}
1992908d778SJames Bottomley 	spin_unlock(&port->phy_list_lock);
200980fa2f9SDarrick J. Wong 	spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
2012908d778SJames Bottomley 
2022908d778SJames Bottomley 	return;
2032908d778SJames Bottomley }
2042908d778SJames Bottomley 
2052908d778SJames Bottomley /* ---------- SAS port events ---------- */
2062908d778SJames Bottomley 
207c4028958SDavid Howells void sas_porte_bytes_dmaed(struct work_struct *work)
2082908d778SJames Bottomley {
209c4028958SDavid Howells 	struct asd_sas_event *ev =
210c4028958SDavid Howells 		container_of(work, struct asd_sas_event, work);
211c4028958SDavid Howells 	struct asd_sas_phy *phy = ev->phy;
2122908d778SJames Bottomley 
2132908d778SJames Bottomley 	sas_begin_event(PORTE_BYTES_DMAED, &phy->ha->event_lock,
2142908d778SJames Bottomley 			&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 {
221c4028958SDavid Howells 	struct asd_sas_event *ev =
222c4028958SDavid Howells 		container_of(work, struct asd_sas_event, work);
223c4028958SDavid Howells 	struct asd_sas_phy *phy = ev->phy;
2242908d778SJames Bottomley 	unsigned long flags;
2252908d778SJames Bottomley 	u32 prim;
2262908d778SJames Bottomley 
2272908d778SJames Bottomley 	sas_begin_event(PORTE_BROADCAST_RCVD, &phy->ha->event_lock,
2282908d778SJames Bottomley 			&phy->port_events_pending);
2292908d778SJames Bottomley 
2302908d778SJames Bottomley 	spin_lock_irqsave(&phy->sas_prim_lock, flags);
2312908d778SJames Bottomley 	prim = phy->sas_prim;
2322908d778SJames Bottomley 	spin_unlock_irqrestore(&phy->sas_prim_lock, flags);
2332908d778SJames Bottomley 
2342908d778SJames Bottomley 	SAS_DPRINTK("broadcast received: %d\n", prim);
2352908d778SJames Bottomley 	sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN);
2362908d778SJames Bottomley }
2372908d778SJames Bottomley 
238c4028958SDavid Howells void sas_porte_link_reset_err(struct work_struct *work)
2392908d778SJames Bottomley {
240c4028958SDavid Howells 	struct asd_sas_event *ev =
241c4028958SDavid Howells 		container_of(work, struct asd_sas_event, work);
242c4028958SDavid Howells 	struct asd_sas_phy *phy = ev->phy;
2432908d778SJames Bottomley 
2442908d778SJames Bottomley 	sas_begin_event(PORTE_LINK_RESET_ERR, &phy->ha->event_lock,
2452908d778SJames Bottomley 			&phy->port_events_pending);
2462908d778SJames Bottomley 
2472908d778SJames Bottomley 	sas_deform_port(phy);
2482908d778SJames Bottomley }
2492908d778SJames Bottomley 
250c4028958SDavid Howells void sas_porte_timer_event(struct work_struct *work)
2512908d778SJames Bottomley {
252c4028958SDavid Howells 	struct asd_sas_event *ev =
253c4028958SDavid Howells 		container_of(work, struct asd_sas_event, work);
254c4028958SDavid Howells 	struct asd_sas_phy *phy = ev->phy;
2552908d778SJames Bottomley 
2562908d778SJames Bottomley 	sas_begin_event(PORTE_TIMER_EVENT, &phy->ha->event_lock,
2572908d778SJames Bottomley 			&phy->port_events_pending);
2582908d778SJames Bottomley 
2592908d778SJames Bottomley 	sas_deform_port(phy);
2602908d778SJames Bottomley }
2612908d778SJames Bottomley 
262c4028958SDavid Howells void sas_porte_hard_reset(struct work_struct *work)
2632908d778SJames Bottomley {
264c4028958SDavid Howells 	struct asd_sas_event *ev =
265c4028958SDavid Howells 		container_of(work, struct asd_sas_event, work);
266c4028958SDavid Howells 	struct asd_sas_phy *phy = ev->phy;
2672908d778SJames Bottomley 
2682908d778SJames Bottomley 	sas_begin_event(PORTE_HARD_RESET, &phy->ha->event_lock,
2692908d778SJames Bottomley 			&phy->port_events_pending);
2702908d778SJames Bottomley 
2712908d778SJames Bottomley 	sas_deform_port(phy);
2722908d778SJames Bottomley }
2732908d778SJames Bottomley 
2742908d778SJames Bottomley /* ---------- SAS port registration ---------- */
2752908d778SJames Bottomley 
2762908d778SJames Bottomley static void sas_init_port(struct asd_sas_port *port,
2772908d778SJames Bottomley 			  struct sas_ha_struct *sas_ha, int i)
2782908d778SJames Bottomley {
279a29c0515SJames Bottomley 	memset(port, 0, sizeof(*port));
2802908d778SJames Bottomley 	port->id = i;
2812908d778SJames Bottomley 	INIT_LIST_HEAD(&port->dev_list);
2822908d778SJames Bottomley 	spin_lock_init(&port->phy_list_lock);
2832908d778SJames Bottomley 	INIT_LIST_HEAD(&port->phy_list);
2842908d778SJames Bottomley 	port->ha = sas_ha;
2852908d778SJames Bottomley 
2862908d778SJames Bottomley 	spin_lock_init(&port->dev_list_lock);
2872908d778SJames Bottomley }
2882908d778SJames Bottomley 
2892908d778SJames Bottomley int sas_register_ports(struct sas_ha_struct *sas_ha)
2902908d778SJames Bottomley {
2912908d778SJames Bottomley 	int i;
2922908d778SJames Bottomley 
2932908d778SJames Bottomley 	/* initialize the ports and discovery */
2942908d778SJames Bottomley 	for (i = 0; i < sas_ha->num_phys; i++) {
2952908d778SJames Bottomley 		struct asd_sas_port *port = sas_ha->sas_port[i];
2962908d778SJames Bottomley 
2972908d778SJames Bottomley 		sas_init_port(port, sas_ha, i);
2982908d778SJames Bottomley 		sas_init_disc(&port->disc, port);
2992908d778SJames Bottomley 	}
3002908d778SJames Bottomley 	return 0;
3012908d778SJames Bottomley }
3022908d778SJames Bottomley 
3032908d778SJames Bottomley void sas_unregister_ports(struct sas_ha_struct *sas_ha)
3042908d778SJames Bottomley {
3052908d778SJames Bottomley 	int i;
3062908d778SJames Bottomley 
3072908d778SJames Bottomley 	for (i = 0; i < sas_ha->num_phys; i++)
3082908d778SJames Bottomley 		if (sas_ha->sas_phy[i]->port)
3092908d778SJames Bottomley 			sas_deform_port(sas_ha->sas_phy[i]);
3102908d778SJames Bottomley 
3112908d778SJames Bottomley }
312