xref: /openbmc/linux/drivers/scsi/libsas/sas_port.c (revision 2908d778)
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);
452908d778SJames Bottomley 
462908d778SJames Bottomley 	if (port) {
472908d778SJames Bottomley 		if (memcmp(port->attached_sas_addr, phy->attached_sas_addr,
482908d778SJames Bottomley 			   SAS_ADDR_SIZE) == 0)
492908d778SJames Bottomley 			sas_deform_port(phy);
502908d778SJames Bottomley 		else {
512908d778SJames Bottomley 			SAS_DPRINTK("%s: phy%d belongs to port%d already(%d)!\n",
522908d778SJames Bottomley 				    __FUNCTION__, phy->id, phy->port->id,
532908d778SJames Bottomley 				    phy->port->num_phys);
542908d778SJames Bottomley 			return;
552908d778SJames Bottomley 		}
562908d778SJames Bottomley 	}
572908d778SJames Bottomley 
582908d778SJames Bottomley 	/* find a port */
592908d778SJames Bottomley 	spin_lock(&sas_ha->phy_port_lock);
602908d778SJames Bottomley 	for (i = 0; i < sas_ha->num_phys; i++) {
612908d778SJames Bottomley 		port = sas_ha->sas_port[i];
622908d778SJames Bottomley 		spin_lock(&port->phy_list_lock);
632908d778SJames Bottomley 		if (*(u64 *) port->sas_addr &&
642908d778SJames Bottomley 		    memcmp(port->attached_sas_addr,
652908d778SJames Bottomley 			   phy->attached_sas_addr, SAS_ADDR_SIZE) == 0 &&
662908d778SJames Bottomley 		    port->num_phys > 0) {
672908d778SJames Bottomley 			/* wide port */
682908d778SJames Bottomley 			SAS_DPRINTK("phy%d matched wide port%d\n", phy->id,
692908d778SJames Bottomley 				    port->id);
702908d778SJames Bottomley 			break;
712908d778SJames Bottomley 		} else if (*(u64 *) port->sas_addr == 0 && port->num_phys==0) {
722908d778SJames Bottomley 			memcpy(port->sas_addr, phy->sas_addr, SAS_ADDR_SIZE);
732908d778SJames Bottomley 			break;
742908d778SJames Bottomley 		}
752908d778SJames Bottomley 		spin_unlock(&port->phy_list_lock);
762908d778SJames Bottomley 	}
772908d778SJames Bottomley 
782908d778SJames Bottomley 	if (i >= sas_ha->num_phys) {
792908d778SJames Bottomley 		printk(KERN_NOTICE "%s: couldn't find a free port, bug?\n",
802908d778SJames Bottomley 		       __FUNCTION__);
812908d778SJames Bottomley 		spin_unlock(&sas_ha->phy_port_lock);
822908d778SJames Bottomley 		return;
832908d778SJames Bottomley 	}
842908d778SJames Bottomley 
852908d778SJames Bottomley 	/* add the phy to the port */
862908d778SJames Bottomley 	list_add_tail(&phy->port_phy_el, &port->phy_list);
872908d778SJames Bottomley 	phy->port = port;
882908d778SJames Bottomley 	port->num_phys++;
892908d778SJames Bottomley 	port->phy_mask |= (1U << phy->id);
902908d778SJames Bottomley 
912908d778SJames Bottomley 	if (!port->phy)
922908d778SJames Bottomley 		port->phy = phy->phy;
932908d778SJames Bottomley 
942908d778SJames Bottomley 	SAS_DPRINTK("phy%d added to port%d, phy_mask:0x%x\n", phy->id,
952908d778SJames Bottomley 		    port->id, port->phy_mask);
962908d778SJames Bottomley 
972908d778SJames Bottomley 	if (*(u64 *)port->attached_sas_addr == 0) {
982908d778SJames Bottomley 		port->class = phy->class;
992908d778SJames Bottomley 		memcpy(port->attached_sas_addr, phy->attached_sas_addr,
1002908d778SJames Bottomley 		       SAS_ADDR_SIZE);
1012908d778SJames Bottomley 		port->iproto = phy->iproto;
1022908d778SJames Bottomley 		port->tproto = phy->tproto;
1032908d778SJames Bottomley 		port->oob_mode = phy->oob_mode;
1042908d778SJames Bottomley 		port->linkrate = phy->linkrate;
1052908d778SJames Bottomley 	} else
1062908d778SJames Bottomley 		port->linkrate = max(port->linkrate, phy->linkrate);
1072908d778SJames Bottomley 	spin_unlock(&port->phy_list_lock);
1082908d778SJames Bottomley 	spin_unlock(&sas_ha->phy_port_lock);
1092908d778SJames Bottomley 
1102908d778SJames Bottomley 	if (!port->port) {
1112908d778SJames Bottomley 		port->port = sas_port_alloc(phy->phy->dev.parent, port->id);
1122908d778SJames Bottomley 		BUG_ON(!port->port);
1132908d778SJames Bottomley 		sas_port_add(port->port);
1142908d778SJames Bottomley 	}
1152908d778SJames Bottomley 	sas_port_add_phy(port->port, phy->phy);
1162908d778SJames Bottomley 
1172908d778SJames Bottomley 	if (port->port_dev)
1182908d778SJames Bottomley 		port->port_dev->pathways = port->num_phys;
1192908d778SJames Bottomley 
1202908d778SJames Bottomley 	/* Tell the LLDD about this port formation. */
1212908d778SJames Bottomley 	if (si->dft->lldd_port_formed)
1222908d778SJames Bottomley 		si->dft->lldd_port_formed(phy);
1232908d778SJames Bottomley 
1242908d778SJames Bottomley 	sas_discover_event(phy->port, DISCE_DISCOVER_DOMAIN);
1252908d778SJames Bottomley }
1262908d778SJames Bottomley 
1272908d778SJames Bottomley /**
1282908d778SJames Bottomley  * sas_deform_port -- remove this phy from the port it belongs to
1292908d778SJames Bottomley  * @phy: the phy of interest
1302908d778SJames Bottomley  *
1312908d778SJames Bottomley  * This is called when the physical link to the other phy has been
1322908d778SJames Bottomley  * lost (on this phy), in Event thread context. We cannot delay here.
1332908d778SJames Bottomley  */
1342908d778SJames Bottomley void sas_deform_port(struct asd_sas_phy *phy)
1352908d778SJames Bottomley {
1362908d778SJames Bottomley 	struct sas_ha_struct *sas_ha = phy->ha;
1372908d778SJames Bottomley 	struct asd_sas_port *port = phy->port;
1382908d778SJames Bottomley 	struct sas_internal *si =
1392908d778SJames Bottomley 		to_sas_internal(sas_ha->core.shost->transportt);
1402908d778SJames Bottomley 
1412908d778SJames Bottomley 	if (!port)
1422908d778SJames Bottomley 		return;		  /* done by a phy event */
1432908d778SJames Bottomley 
1442908d778SJames Bottomley 	if (port->port_dev)
1452908d778SJames Bottomley 		port->port_dev->pathways--;
1462908d778SJames Bottomley 
1472908d778SJames Bottomley 	if (port->num_phys == 1) {
1482908d778SJames Bottomley 		sas_unregister_domain_devices(port);
1492908d778SJames Bottomley 		sas_port_delete(port->port);
1502908d778SJames Bottomley 		port->port = NULL;
1512908d778SJames Bottomley 	} else
1522908d778SJames Bottomley 		sas_port_delete_phy(port->port, phy->phy);
1532908d778SJames Bottomley 
1542908d778SJames Bottomley 
1552908d778SJames Bottomley 	if (si->dft->lldd_port_deformed)
1562908d778SJames Bottomley 		si->dft->lldd_port_deformed(phy);
1572908d778SJames Bottomley 
1582908d778SJames Bottomley 	spin_lock(&sas_ha->phy_port_lock);
1592908d778SJames Bottomley 	spin_lock(&port->phy_list_lock);
1602908d778SJames Bottomley 
1612908d778SJames Bottomley 	list_del_init(&phy->port_phy_el);
1622908d778SJames Bottomley 	phy->port = NULL;
1632908d778SJames Bottomley 	port->num_phys--;
1642908d778SJames Bottomley 	port->phy_mask &= ~(1U << phy->id);
1652908d778SJames Bottomley 
1662908d778SJames Bottomley 	if (port->num_phys == 0) {
1672908d778SJames Bottomley 		INIT_LIST_HEAD(&port->phy_list);
1682908d778SJames Bottomley 		memset(port->sas_addr, 0, SAS_ADDR_SIZE);
1692908d778SJames Bottomley 		memset(port->attached_sas_addr, 0, SAS_ADDR_SIZE);
1702908d778SJames Bottomley 		port->class = 0;
1712908d778SJames Bottomley 		port->iproto = 0;
1722908d778SJames Bottomley 		port->tproto = 0;
1732908d778SJames Bottomley 		port->oob_mode = 0;
1742908d778SJames Bottomley 		port->phy_mask = 0;
1752908d778SJames Bottomley 	}
1762908d778SJames Bottomley 	spin_unlock(&port->phy_list_lock);
1772908d778SJames Bottomley 	spin_unlock(&sas_ha->phy_port_lock);
1782908d778SJames Bottomley 
1792908d778SJames Bottomley 	return;
1802908d778SJames Bottomley }
1812908d778SJames Bottomley 
1822908d778SJames Bottomley /* ---------- SAS port events ---------- */
1832908d778SJames Bottomley 
1842908d778SJames Bottomley void sas_porte_bytes_dmaed(void *data)
1852908d778SJames Bottomley {
1862908d778SJames Bottomley 	struct asd_sas_phy *phy = data;
1872908d778SJames Bottomley 
1882908d778SJames Bottomley 	sas_begin_event(PORTE_BYTES_DMAED, &phy->ha->event_lock,
1892908d778SJames Bottomley 			&phy->port_events_pending);
1902908d778SJames Bottomley 
1912908d778SJames Bottomley 	sas_form_port(phy);
1922908d778SJames Bottomley }
1932908d778SJames Bottomley 
1942908d778SJames Bottomley void sas_porte_broadcast_rcvd(void *data)
1952908d778SJames Bottomley {
1962908d778SJames Bottomley 	unsigned long flags;
1972908d778SJames Bottomley 	u32 prim;
1982908d778SJames Bottomley 	struct asd_sas_phy *phy = data;
1992908d778SJames Bottomley 
2002908d778SJames Bottomley 	sas_begin_event(PORTE_BROADCAST_RCVD, &phy->ha->event_lock,
2012908d778SJames Bottomley 			&phy->port_events_pending);
2022908d778SJames Bottomley 
2032908d778SJames Bottomley 	spin_lock_irqsave(&phy->sas_prim_lock, flags);
2042908d778SJames Bottomley 	prim = phy->sas_prim;
2052908d778SJames Bottomley 	spin_unlock_irqrestore(&phy->sas_prim_lock, flags);
2062908d778SJames Bottomley 
2072908d778SJames Bottomley 	SAS_DPRINTK("broadcast received: %d\n", prim);
2082908d778SJames Bottomley 	sas_discover_event(phy->port, DISCE_REVALIDATE_DOMAIN);
2092908d778SJames Bottomley }
2102908d778SJames Bottomley 
2112908d778SJames Bottomley void sas_porte_link_reset_err(void *data)
2122908d778SJames Bottomley {
2132908d778SJames Bottomley 	struct asd_sas_phy *phy = data;
2142908d778SJames Bottomley 
2152908d778SJames Bottomley 	sas_begin_event(PORTE_LINK_RESET_ERR, &phy->ha->event_lock,
2162908d778SJames Bottomley 			&phy->port_events_pending);
2172908d778SJames Bottomley 
2182908d778SJames Bottomley 	sas_deform_port(phy);
2192908d778SJames Bottomley }
2202908d778SJames Bottomley 
2212908d778SJames Bottomley void sas_porte_timer_event(void *data)
2222908d778SJames Bottomley {
2232908d778SJames Bottomley 	struct asd_sas_phy *phy = data;
2242908d778SJames Bottomley 
2252908d778SJames Bottomley 	sas_begin_event(PORTE_TIMER_EVENT, &phy->ha->event_lock,
2262908d778SJames Bottomley 			&phy->port_events_pending);
2272908d778SJames Bottomley 
2282908d778SJames Bottomley 	sas_deform_port(phy);
2292908d778SJames Bottomley }
2302908d778SJames Bottomley 
2312908d778SJames Bottomley void sas_porte_hard_reset(void *data)
2322908d778SJames Bottomley {
2332908d778SJames Bottomley 	struct asd_sas_phy *phy = data;
2342908d778SJames Bottomley 
2352908d778SJames Bottomley 	sas_begin_event(PORTE_HARD_RESET, &phy->ha->event_lock,
2362908d778SJames Bottomley 			&phy->port_events_pending);
2372908d778SJames Bottomley 
2382908d778SJames Bottomley 	sas_deform_port(phy);
2392908d778SJames Bottomley }
2402908d778SJames Bottomley 
2412908d778SJames Bottomley /* ---------- SAS port registration ---------- */
2422908d778SJames Bottomley 
2432908d778SJames Bottomley static void sas_init_port(struct asd_sas_port *port,
2442908d778SJames Bottomley 			  struct sas_ha_struct *sas_ha, int i)
2452908d778SJames Bottomley {
2462908d778SJames Bottomley 	port->id = i;
2472908d778SJames Bottomley 	INIT_LIST_HEAD(&port->dev_list);
2482908d778SJames Bottomley 	spin_lock_init(&port->phy_list_lock);
2492908d778SJames Bottomley 	INIT_LIST_HEAD(&port->phy_list);
2502908d778SJames Bottomley 	port->num_phys = 0;
2512908d778SJames Bottomley 	port->phy_mask = 0;
2522908d778SJames Bottomley 	port->ha = sas_ha;
2532908d778SJames Bottomley 
2542908d778SJames Bottomley 	spin_lock_init(&port->dev_list_lock);
2552908d778SJames Bottomley }
2562908d778SJames Bottomley 
2572908d778SJames Bottomley int sas_register_ports(struct sas_ha_struct *sas_ha)
2582908d778SJames Bottomley {
2592908d778SJames Bottomley 	int i;
2602908d778SJames Bottomley 
2612908d778SJames Bottomley 	/* initialize the ports and discovery */
2622908d778SJames Bottomley 	for (i = 0; i < sas_ha->num_phys; i++) {
2632908d778SJames Bottomley 		struct asd_sas_port *port = sas_ha->sas_port[i];
2642908d778SJames Bottomley 
2652908d778SJames Bottomley 		sas_init_port(port, sas_ha, i);
2662908d778SJames Bottomley 		sas_init_disc(&port->disc, port);
2672908d778SJames Bottomley 	}
2682908d778SJames Bottomley 	return 0;
2692908d778SJames Bottomley }
2702908d778SJames Bottomley 
2712908d778SJames Bottomley void sas_unregister_ports(struct sas_ha_struct *sas_ha)
2722908d778SJames Bottomley {
2732908d778SJames Bottomley 	int i;
2742908d778SJames Bottomley 
2752908d778SJames Bottomley 	for (i = 0; i < sas_ha->num_phys; i++)
2762908d778SJames Bottomley 		if (sas_ha->sas_phy[i]->port)
2772908d778SJames Bottomley 			sas_deform_port(sas_ha->sas_phy[i]);
2782908d778SJames Bottomley 
2792908d778SJames Bottomley }
280