xref: /openbmc/linux/drivers/scsi/libsas/sas_port.c (revision 71610f55)
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 
312908d778SJames Bottomley /**
322908d778SJames Bottomley  * sas_form_port -- add this phy to a port
332908d778SJames Bottomley  * @phy: the phy of interest
342908d778SJames Bottomley  *
352908d778SJames Bottomley  * This function adds this phy to an existing port, thus creating a wide
362908d778SJames Bottomley  * port, or it creates a port and adds the phy to the port.
372908d778SJames Bottomley  */
382908d778SJames Bottomley static void sas_form_port(struct asd_sas_phy *phy)
392908d778SJames Bottomley {
402908d778SJames Bottomley 	int i;
412908d778SJames Bottomley 	struct sas_ha_struct *sas_ha = phy->ha;
422908d778SJames Bottomley 	struct asd_sas_port *port = phy->port;
432908d778SJames Bottomley 	struct sas_internal *si =
442908d778SJames Bottomley 		to_sas_internal(sas_ha->core.shost->transportt);
45980fa2f9SDarrick J. Wong 	unsigned long flags;
462908d778SJames Bottomley 
472908d778SJames Bottomley 	if (port) {
482908d778SJames Bottomley 		if (memcmp(port->attached_sas_addr, phy->attached_sas_addr,
493b6e9fafSDarrick J. Wong 			   SAS_ADDR_SIZE) != 0)
502908d778SJames Bottomley 			sas_deform_port(phy);
512908d778SJames Bottomley 		else {
522908d778SJames Bottomley 			SAS_DPRINTK("%s: phy%d belongs to port%d already(%d)!\n",
53cadbd4a5SHarvey Harrison 				    __func__, phy->id, phy->port->id,
542908d778SJames Bottomley 				    phy->port->num_phys);
552908d778SJames Bottomley 			return;
562908d778SJames Bottomley 		}
572908d778SJames Bottomley 	}
582908d778SJames Bottomley 
592908d778SJames Bottomley 	/* find a port */
60980fa2f9SDarrick J. Wong 	spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
612908d778SJames Bottomley 	for (i = 0; i < sas_ha->num_phys; i++) {
622908d778SJames Bottomley 		port = sas_ha->sas_port[i];
632908d778SJames Bottomley 		spin_lock(&port->phy_list_lock);
642908d778SJames Bottomley 		if (*(u64 *) port->sas_addr &&
652908d778SJames Bottomley 		    memcmp(port->attached_sas_addr,
662908d778SJames Bottomley 			   phy->attached_sas_addr, SAS_ADDR_SIZE) == 0 &&
672908d778SJames Bottomley 		    port->num_phys > 0) {
682908d778SJames Bottomley 			/* wide port */
692908d778SJames Bottomley 			SAS_DPRINTK("phy%d matched wide port%d\n", phy->id,
702908d778SJames Bottomley 				    port->id);
712908d778SJames Bottomley 			break;
722908d778SJames Bottomley 		} else if (*(u64 *) port->sas_addr == 0 && port->num_phys==0) {
732908d778SJames Bottomley 			memcpy(port->sas_addr, phy->sas_addr, SAS_ADDR_SIZE);
742908d778SJames Bottomley 			break;
752908d778SJames Bottomley 		}
762908d778SJames Bottomley 		spin_unlock(&port->phy_list_lock);
772908d778SJames Bottomley 	}
782908d778SJames Bottomley 
792908d778SJames Bottomley 	if (i >= sas_ha->num_phys) {
802908d778SJames Bottomley 		printk(KERN_NOTICE "%s: couldn't find a free port, bug?\n",
81cadbd4a5SHarvey Harrison 		       __func__);
82980fa2f9SDarrick J. Wong 		spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
832908d778SJames Bottomley 		return;
842908d778SJames Bottomley 	}
852908d778SJames Bottomley 
862908d778SJames Bottomley 	/* add the phy to the port */
872908d778SJames Bottomley 	list_add_tail(&phy->port_phy_el, &port->phy_list);
882908d778SJames Bottomley 	phy->port = port;
892908d778SJames Bottomley 	port->num_phys++;
902908d778SJames Bottomley 	port->phy_mask |= (1U << phy->id);
912908d778SJames Bottomley 
922908d778SJames Bottomley 	if (!port->phy)
932908d778SJames Bottomley 		port->phy = phy->phy;
942908d778SJames Bottomley 
952908d778SJames Bottomley 	if (*(u64 *)port->attached_sas_addr == 0) {
962908d778SJames Bottomley 		port->class = phy->class;
972908d778SJames Bottomley 		memcpy(port->attached_sas_addr, phy->attached_sas_addr,
982908d778SJames Bottomley 		       SAS_ADDR_SIZE);
992908d778SJames Bottomley 		port->iproto = phy->iproto;
1002908d778SJames Bottomley 		port->tproto = phy->tproto;
1012908d778SJames Bottomley 		port->oob_mode = phy->oob_mode;
1022908d778SJames Bottomley 		port->linkrate = phy->linkrate;
1032908d778SJames Bottomley 	} else
1042908d778SJames Bottomley 		port->linkrate = max(port->linkrate, phy->linkrate);
1052908d778SJames Bottomley 	spin_unlock(&port->phy_list_lock);
106980fa2f9SDarrick J. Wong 	spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
1072908d778SJames Bottomley 
1082908d778SJames Bottomley 	if (!port->port) {
1092908d778SJames Bottomley 		port->port = sas_port_alloc(phy->phy->dev.parent, port->id);
1102908d778SJames Bottomley 		BUG_ON(!port->port);
1112908d778SJames Bottomley 		sas_port_add(port->port);
1122908d778SJames Bottomley 	}
1132908d778SJames Bottomley 	sas_port_add_phy(port->port, phy->phy);
1142908d778SJames Bottomley 
115a29c0515SJames Bottomley 	SAS_DPRINTK("%s added to %s, phy_mask:0x%x (%16llx)\n",
11671610f55SKay Sievers 		    dev_name(&phy->phy->dev), dev_name(&port->port->dev),
117a29c0515SJames Bottomley 		    port->phy_mask,
118a29c0515SJames Bottomley 		    SAS_ADDR(port->attached_sas_addr));
119a29c0515SJames Bottomley 
1202908d778SJames Bottomley 	if (port->port_dev)
1212908d778SJames Bottomley 		port->port_dev->pathways = port->num_phys;
1222908d778SJames Bottomley 
1232908d778SJames Bottomley 	/* Tell the LLDD about this port formation. */
1242908d778SJames Bottomley 	if (si->dft->lldd_port_formed)
1252908d778SJames Bottomley 		si->dft->lldd_port_formed(phy);
1262908d778SJames Bottomley 
1272908d778SJames Bottomley 	sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN);
1282908d778SJames Bottomley }
1292908d778SJames Bottomley 
1302908d778SJames Bottomley /**
1312908d778SJames Bottomley  * sas_deform_port -- remove this phy from the port it belongs to
1322908d778SJames Bottomley  * @phy: the phy of interest
1332908d778SJames Bottomley  *
1342908d778SJames Bottomley  * This is called when the physical link to the other phy has been
1352908d778SJames Bottomley  * lost (on this phy), in Event thread context. We cannot delay here.
1362908d778SJames Bottomley  */
1372908d778SJames Bottomley void sas_deform_port(struct asd_sas_phy *phy)
1382908d778SJames Bottomley {
1392908d778SJames Bottomley 	struct sas_ha_struct *sas_ha = phy->ha;
1402908d778SJames Bottomley 	struct asd_sas_port *port = phy->port;
1412908d778SJames Bottomley 	struct sas_internal *si =
1422908d778SJames Bottomley 		to_sas_internal(sas_ha->core.shost->transportt);
143980fa2f9SDarrick J. Wong 	unsigned long flags;
1442908d778SJames Bottomley 
1452908d778SJames Bottomley 	if (!port)
1462908d778SJames Bottomley 		return;		  /* done by a phy event */
1472908d778SJames Bottomley 
1482908d778SJames Bottomley 	if (port->port_dev)
1492908d778SJames Bottomley 		port->port_dev->pathways--;
1502908d778SJames Bottomley 
1512908d778SJames Bottomley 	if (port->num_phys == 1) {
1522908d778SJames Bottomley 		sas_unregister_domain_devices(port);
1532908d778SJames Bottomley 		sas_port_delete(port->port);
1542908d778SJames Bottomley 		port->port = NULL;
1552908d778SJames Bottomley 	} else
1562908d778SJames Bottomley 		sas_port_delete_phy(port->port, phy->phy);
1572908d778SJames Bottomley 
1582908d778SJames Bottomley 
1592908d778SJames Bottomley 	if (si->dft->lldd_port_deformed)
1602908d778SJames Bottomley 		si->dft->lldd_port_deformed(phy);
1612908d778SJames Bottomley 
162980fa2f9SDarrick J. Wong 	spin_lock_irqsave(&sas_ha->phy_port_lock, flags);
1632908d778SJames Bottomley 	spin_lock(&port->phy_list_lock);
1642908d778SJames Bottomley 
1652908d778SJames Bottomley 	list_del_init(&phy->port_phy_el);
1662908d778SJames Bottomley 	phy->port = NULL;
1672908d778SJames Bottomley 	port->num_phys--;
1682908d778SJames Bottomley 	port->phy_mask &= ~(1U << phy->id);
1692908d778SJames Bottomley 
1702908d778SJames Bottomley 	if (port->num_phys == 0) {
1712908d778SJames Bottomley 		INIT_LIST_HEAD(&port->phy_list);
1722908d778SJames Bottomley 		memset(port->sas_addr, 0, SAS_ADDR_SIZE);
1732908d778SJames Bottomley 		memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE);
1742908d778SJames Bottomley 		port->class = 0;
1752908d778SJames Bottomley 		port->iproto = 0;
1762908d778SJames Bottomley 		port->tproto = 0;
1772908d778SJames Bottomley 		port->oob_mode = 0;
1782908d778SJames Bottomley 		port->phy_mask = 0;
1792908d778SJames Bottomley 	}
1802908d778SJames Bottomley 	spin_unlock(&port->phy_list_lock);
181980fa2f9SDarrick J. Wong 	spin_unlock_irqrestore(&sas_ha->phy_port_lock, flags);
1822908d778SJames Bottomley 
1832908d778SJames Bottomley 	return;
1842908d778SJames Bottomley }
1852908d778SJames Bottomley 
1862908d778SJames Bottomley /* ---------- SAS port events ---------- */
1872908d778SJames Bottomley 
188c4028958SDavid Howells void sas_porte_bytes_dmaed(struct work_struct *work)
1892908d778SJames Bottomley {
190c4028958SDavid Howells 	struct asd_sas_event *ev =
191c4028958SDavid Howells 		container_of(work, struct asd_sas_event, work);
192c4028958SDavid Howells 	struct asd_sas_phy *phy = ev->phy;
1932908d778SJames Bottomley 
1942908d778SJames Bottomley 	sas_begin_event(PORTE_BYTES_DMAED, &phy->ha->event_lock,
1952908d778SJames Bottomley 			&phy->port_events_pending);
1962908d778SJames Bottomley 
1972908d778SJames Bottomley 	sas_form_port(phy);
1982908d778SJames Bottomley }
1992908d778SJames Bottomley 
200c4028958SDavid Howells void sas_porte_broadcast_rcvd(struct work_struct *work)
2012908d778SJames Bottomley {
202c4028958SDavid Howells 	struct asd_sas_event *ev =
203c4028958SDavid Howells 		container_of(work, struct asd_sas_event, work);
204c4028958SDavid Howells 	struct asd_sas_phy *phy = ev->phy;
2052908d778SJames Bottomley 	unsigned long flags;
2062908d778SJames Bottomley 	u32 prim;
2072908d778SJames Bottomley 
2082908d778SJames Bottomley 	sas_begin_event(PORTE_BROADCAST_RCVD, &phy->ha->event_lock,
2092908d778SJames Bottomley 			&phy->port_events_pending);
2102908d778SJames Bottomley 
2112908d778SJames Bottomley 	spin_lock_irqsave(&phy->sas_prim_lock, flags);
2122908d778SJames Bottomley 	prim = phy->sas_prim;
2132908d778SJames Bottomley 	spin_unlock_irqrestore(&phy->sas_prim_lock, flags);
2142908d778SJames Bottomley 
2152908d778SJames Bottomley 	SAS_DPRINTK("broadcast received: %d\n", prim);
2162908d778SJames Bottomley 	sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN);
2172908d778SJames Bottomley }
2182908d778SJames Bottomley 
219c4028958SDavid Howells void sas_porte_link_reset_err(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 
2252908d778SJames Bottomley 	sas_begin_event(PORTE_LINK_RESET_ERR, &phy->ha->event_lock,
2262908d778SJames Bottomley 			&phy->port_events_pending);
2272908d778SJames Bottomley 
2282908d778SJames Bottomley 	sas_deform_port(phy);
2292908d778SJames Bottomley }
2302908d778SJames Bottomley 
231c4028958SDavid Howells void sas_porte_timer_event(struct work_struct *work)
2322908d778SJames Bottomley {
233c4028958SDavid Howells 	struct asd_sas_event *ev =
234c4028958SDavid Howells 		container_of(work, struct asd_sas_event, work);
235c4028958SDavid Howells 	struct asd_sas_phy *phy = ev->phy;
2362908d778SJames Bottomley 
2372908d778SJames Bottomley 	sas_begin_event(PORTE_TIMER_EVENT, &phy->ha->event_lock,
2382908d778SJames Bottomley 			&phy->port_events_pending);
2392908d778SJames Bottomley 
2402908d778SJames Bottomley 	sas_deform_port(phy);
2412908d778SJames Bottomley }
2422908d778SJames Bottomley 
243c4028958SDavid Howells void sas_porte_hard_reset(struct work_struct *work)
2442908d778SJames Bottomley {
245c4028958SDavid Howells 	struct asd_sas_event *ev =
246c4028958SDavid Howells 		container_of(work, struct asd_sas_event, work);
247c4028958SDavid Howells 	struct asd_sas_phy *phy = ev->phy;
2482908d778SJames Bottomley 
2492908d778SJames Bottomley 	sas_begin_event(PORTE_HARD_RESET, &phy->ha->event_lock,
2502908d778SJames Bottomley 			&phy->port_events_pending);
2512908d778SJames Bottomley 
2522908d778SJames Bottomley 	sas_deform_port(phy);
2532908d778SJames Bottomley }
2542908d778SJames Bottomley 
2552908d778SJames Bottomley /* ---------- SAS port registration ---------- */
2562908d778SJames Bottomley 
2572908d778SJames Bottomley static void sas_init_port(struct asd_sas_port *port,
2582908d778SJames Bottomley 			  struct sas_ha_struct *sas_ha, int i)
2592908d778SJames Bottomley {
260a29c0515SJames Bottomley 	memset(port, 0, sizeof(*port));
2612908d778SJames Bottomley 	port->id = i;
2622908d778SJames Bottomley 	INIT_LIST_HEAD(&port->dev_list);
2632908d778SJames Bottomley 	spin_lock_init(&port->phy_list_lock);
2642908d778SJames Bottomley 	INIT_LIST_HEAD(&port->phy_list);
2652908d778SJames Bottomley 	port->ha = sas_ha;
2662908d778SJames Bottomley 
2672908d778SJames Bottomley 	spin_lock_init(&port->dev_list_lock);
2682908d778SJames Bottomley }
2692908d778SJames Bottomley 
2702908d778SJames Bottomley int sas_register_ports(struct sas_ha_struct *sas_ha)
2712908d778SJames Bottomley {
2722908d778SJames Bottomley 	int i;
2732908d778SJames Bottomley 
2742908d778SJames Bottomley 	/* initialize the ports and discovery */
2752908d778SJames Bottomley 	for (i = 0; i < sas_ha->num_phys; i++) {
2762908d778SJames Bottomley 		struct asd_sas_port *port = sas_ha->sas_port[i];
2772908d778SJames Bottomley 
2782908d778SJames Bottomley 		sas_init_port(port, sas_ha, i);
2792908d778SJames Bottomley 		sas_init_disc(&port->disc, port);
2802908d778SJames Bottomley 	}
2812908d778SJames Bottomley 	return 0;
2822908d778SJames Bottomley }
2832908d778SJames Bottomley 
2842908d778SJames Bottomley void sas_unregister_ports(struct sas_ha_struct *sas_ha)
2852908d778SJames Bottomley {
2862908d778SJames Bottomley 	int i;
2872908d778SJames Bottomley 
2882908d778SJames Bottomley 	for (i = 0; i < sas_ha->num_phys; i++)
2892908d778SJames Bottomley 		if (sas_ha->sas_phy[i]->port)
2902908d778SJames Bottomley 			sas_deform_port(sas_ha->sas_phy[i]);
2912908d778SJames Bottomley 
2922908d778SJames Bottomley }
293