xref: /linux/drivers/scsi/aic94xx/aic94xx_scb.c (revision ab93e0dd72c37d378dd936f031ffb83ff2bd87ce)
165c85c83SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22908d778SJames Bottomley /*
32908d778SJames Bottomley  * Aic94xx SAS/SATA driver SCB management.
42908d778SJames Bottomley  *
52908d778SJames Bottomley  * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
62908d778SJames Bottomley  * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
72908d778SJames Bottomley  */
82908d778SJames Bottomley 
95a0e3ad6STejun Heo #include <linux/gfp.h>
10fe4a36cfSDarrick J. Wong #include <scsi/scsi_host.h>
112908d778SJames Bottomley 
122908d778SJames Bottomley #include "aic94xx.h"
132908d778SJames Bottomley #include "aic94xx_reg.h"
142908d778SJames Bottomley #include "aic94xx_hwi.h"
152908d778SJames Bottomley #include "aic94xx_seq.h"
162908d778SJames Bottomley 
172908d778SJames Bottomley #include "aic94xx_dump.h"
182908d778SJames Bottomley 
192908d778SJames Bottomley /* ---------- EMPTY SCB ---------- */
202908d778SJames Bottomley 
212908d778SJames Bottomley #define DL_PHY_MASK      7
222908d778SJames Bottomley #define BYTES_DMAED      0
232908d778SJames Bottomley #define PRIMITIVE_RECVD  0x08
242908d778SJames Bottomley #define PHY_EVENT        0x10
252908d778SJames Bottomley #define LINK_RESET_ERROR 0x18
262908d778SJames Bottomley #define TIMER_EVENT      0x20
272908d778SJames Bottomley #define REQ_TASK_ABORT   0xF0
282908d778SJames Bottomley #define REQ_DEVICE_RESET 0xF1
292908d778SJames Bottomley #define SIGNAL_NCQ_ERROR 0xF2
302908d778SJames Bottomley #define CLEAR_NCQ_ERROR  0xF3
312908d778SJames Bottomley 
322908d778SJames Bottomley #define PHY_EVENTS_STATUS (CURRENT_LOSS_OF_SIGNAL | CURRENT_OOB_DONE   \
332908d778SJames Bottomley 			   | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
342908d778SJames Bottomley 			   | CURRENT_OOB_ERROR)
352908d778SJames Bottomley 
get_lrate_mode(struct asd_phy * phy,u8 oob_mode)3681e56dedSAdrian Bunk static void get_lrate_mode(struct asd_phy *phy, u8 oob_mode)
372908d778SJames Bottomley {
38a01e70e5SJames Bottomley 	struct sas_phy *sas_phy = phy->sas_phy.phy;
39a01e70e5SJames Bottomley 
402908d778SJames Bottomley 	switch (oob_mode & 7) {
412908d778SJames Bottomley 	case PHY_SPEED_60:
422908d778SJames Bottomley 		/* FIXME: sas transport class doesn't have this */
4388edf746SJames Bottomley 		phy->sas_phy.linkrate = SAS_LINK_RATE_6_0_GBPS;
442908d778SJames Bottomley 		phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_6_0_GBPS;
452908d778SJames Bottomley 		break;
462908d778SJames Bottomley 	case PHY_SPEED_30:
4788edf746SJames Bottomley 		phy->sas_phy.linkrate = SAS_LINK_RATE_3_0_GBPS;
482908d778SJames Bottomley 		phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
492908d778SJames Bottomley 		break;
502908d778SJames Bottomley 	case PHY_SPEED_15:
5188edf746SJames Bottomley 		phy->sas_phy.linkrate = SAS_LINK_RATE_1_5_GBPS;
522908d778SJames Bottomley 		phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
532908d778SJames Bottomley 		break;
542908d778SJames Bottomley 	}
55a01e70e5SJames Bottomley 	sas_phy->negotiated_linkrate = phy->sas_phy.linkrate;
56a01e70e5SJames Bottomley 	sas_phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
57a01e70e5SJames Bottomley 	sas_phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
58a01e70e5SJames Bottomley 	sas_phy->maximum_linkrate = phy->phy_desc->max_sas_lrate;
59a01e70e5SJames Bottomley 	sas_phy->minimum_linkrate = phy->phy_desc->min_sas_lrate;
60a01e70e5SJames Bottomley 
612908d778SJames Bottomley 	if (oob_mode & SAS_MODE)
622908d778SJames Bottomley 		phy->sas_phy.oob_mode = SAS_OOB_MODE;
632908d778SJames Bottomley 	else if (oob_mode & SATA_MODE)
642908d778SJames Bottomley 		phy->sas_phy.oob_mode = SATA_OOB_MODE;
652908d778SJames Bottomley }
662908d778SJames Bottomley 
asd_phy_event_tasklet(struct asd_ascb * ascb,struct done_list_struct * dl)6781e56dedSAdrian Bunk static void asd_phy_event_tasklet(struct asd_ascb *ascb,
682908d778SJames Bottomley 					 struct done_list_struct *dl)
692908d778SJames Bottomley {
702908d778SJames Bottomley 	struct asd_ha_struct *asd_ha = ascb->ha;
712908d778SJames Bottomley 	int phy_id = dl->status_block[0] & DL_PHY_MASK;
722908d778SJames Bottomley 	struct asd_phy *phy = &asd_ha->phys[phy_id];
732908d778SJames Bottomley 
742908d778SJames Bottomley 	u8 oob_status = dl->status_block[1] & PHY_EVENTS_STATUS;
752908d778SJames Bottomley 	u8 oob_mode   = dl->status_block[2];
762908d778SJames Bottomley 
772908d778SJames Bottomley 	switch (oob_status) {
782908d778SJames Bottomley 	case CURRENT_LOSS_OF_SIGNAL:
792908d778SJames Bottomley 		/* directly attached device was removed */
802908d778SJames Bottomley 		ASD_DPRINTK("phy%d: device unplugged\n", phy_id);
812908d778SJames Bottomley 		asd_turn_led(asd_ha, phy_id, 0);
822908d778SJames Bottomley 		sas_phy_disconnected(&phy->sas_phy);
83093289e4SAhmed S. Darwish 		sas_notify_phy_event(&phy->sas_phy, PHYE_LOSS_OF_SIGNAL,
84111d06abSAhmed S. Darwish 				     GFP_ATOMIC);
852908d778SJames Bottomley 		break;
862908d778SJames Bottomley 	case CURRENT_OOB_DONE:
872908d778SJames Bottomley 		/* hot plugged device */
882908d778SJames Bottomley 		asd_turn_led(asd_ha, phy_id, 1);
892908d778SJames Bottomley 		get_lrate_mode(phy, oob_mode);
902908d778SJames Bottomley 		ASD_DPRINTK("phy%d device plugged: lrate:0x%x, proto:0x%x\n",
912908d778SJames Bottomley 			    phy_id, phy->sas_phy.linkrate, phy->sas_phy.iproto);
92093289e4SAhmed S. Darwish 		sas_notify_phy_event(&phy->sas_phy, PHYE_OOB_DONE, GFP_ATOMIC);
932908d778SJames Bottomley 		break;
942908d778SJames Bottomley 	case CURRENT_SPINUP_HOLD:
952908d778SJames Bottomley 		/* hot plug SATA, no COMWAKE sent */
962908d778SJames Bottomley 		asd_turn_led(asd_ha, phy_id, 1);
97093289e4SAhmed S. Darwish 		sas_notify_phy_event(&phy->sas_phy, PHYE_SPINUP_HOLD,
98111d06abSAhmed S. Darwish 				     GFP_ATOMIC);
992908d778SJames Bottomley 		break;
1002908d778SJames Bottomley 	case CURRENT_GTO_TIMEOUT:
1012908d778SJames Bottomley 	case CURRENT_OOB_ERROR:
1022908d778SJames Bottomley 		ASD_DPRINTK("phy%d error while OOB: oob status:0x%x\n", phy_id,
1032908d778SJames Bottomley 			    dl->status_block[1]);
1042908d778SJames Bottomley 		asd_turn_led(asd_ha, phy_id, 0);
1052908d778SJames Bottomley 		sas_phy_disconnected(&phy->sas_phy);
106093289e4SAhmed S. Darwish 		sas_notify_phy_event(&phy->sas_phy, PHYE_OOB_ERROR, GFP_ATOMIC);
1072908d778SJames Bottomley 		break;
1082908d778SJames Bottomley 	}
1092908d778SJames Bottomley }
1102908d778SJames Bottomley 
1112908d778SJames Bottomley /* If phys are enabled sparsely, this will do the right thing. */
ord_phy(struct asd_ha_struct * asd_ha,struct asd_phy * phy)11281e56dedSAdrian Bunk static unsigned ord_phy(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
1132908d778SJames Bottomley {
1142908d778SJames Bottomley 	u8 enabled_mask = asd_ha->hw_prof.enabled_phys;
1152908d778SJames Bottomley 	int i, k = 0;
1162908d778SJames Bottomley 
1172908d778SJames Bottomley 	for_each_phy(enabled_mask, enabled_mask, i) {
1182908d778SJames Bottomley 		if (&asd_ha->phys[i] == phy)
1192908d778SJames Bottomley 			return k;
1202908d778SJames Bottomley 		k++;
1212908d778SJames Bottomley 	}
1222908d778SJames Bottomley 	return 0;
1232908d778SJames Bottomley }
1242908d778SJames Bottomley 
1252908d778SJames Bottomley /**
1262908d778SJames Bottomley  * asd_get_attached_sas_addr -- extract/generate attached SAS address
127d2e51050SLee Jones  * @phy: pointer to asd_phy
128d2e51050SLee Jones  * @sas_addr: pointer to buffer where the SAS address is to be written
1292908d778SJames Bottomley  *
1302908d778SJames Bottomley  * This function extracts the SAS address from an IDENTIFY frame
1312908d778SJames Bottomley  * received.  If OOB is SATA, then a SAS address is generated from the
1322908d778SJames Bottomley  * HA tables.
1332908d778SJames Bottomley  *
1342908d778SJames Bottomley  * LOCKING: the frame_rcvd_lock needs to be held since this parses the frame
1352908d778SJames Bottomley  * buffer.
1362908d778SJames Bottomley  */
asd_get_attached_sas_addr(struct asd_phy * phy,u8 * sas_addr)13781e56dedSAdrian Bunk static void asd_get_attached_sas_addr(struct asd_phy *phy, u8 *sas_addr)
1382908d778SJames Bottomley {
1392908d778SJames Bottomley 	if (phy->sas_phy.frame_rcvd[0] == 0x34
1402908d778SJames Bottomley 	    && phy->sas_phy.oob_mode == SATA_OOB_MODE) {
1412908d778SJames Bottomley 		struct asd_ha_struct *asd_ha = phy->sas_phy.ha->lldd_ha;
1422908d778SJames Bottomley 		/* FIS device-to-host */
1432908d778SJames Bottomley 		u64 addr = be64_to_cpu(*(__be64 *)phy->phy_desc->sas_addr);
1442908d778SJames Bottomley 
1452908d778SJames Bottomley 		addr += asd_ha->hw_prof.sata_name_base + ord_phy(asd_ha, phy);
1462908d778SJames Bottomley 		*(__be64 *)sas_addr = cpu_to_be64(addr);
1472908d778SJames Bottomley 	} else {
1482908d778SJames Bottomley 		struct sas_identify_frame *idframe =
1492908d778SJames Bottomley 			(void *) phy->sas_phy.frame_rcvd;
1502908d778SJames Bottomley 		memcpy(sas_addr, idframe->sas_addr, SAS_ADDR_SIZE);
1512908d778SJames Bottomley 	}
1522908d778SJames Bottomley }
1532908d778SJames Bottomley 
asd_form_port(struct asd_ha_struct * asd_ha,struct asd_phy * phy)1543f048109Smalahal@us.ibm.com static void asd_form_port(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
1553f048109Smalahal@us.ibm.com {
1563f048109Smalahal@us.ibm.com 	int i;
1573f048109Smalahal@us.ibm.com 	struct asd_port *free_port = NULL;
1583f048109Smalahal@us.ibm.com 	struct asd_port *port;
1593f048109Smalahal@us.ibm.com 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
1603f048109Smalahal@us.ibm.com 	unsigned long flags;
1613f048109Smalahal@us.ibm.com 
1623f048109Smalahal@us.ibm.com 	spin_lock_irqsave(&asd_ha->asd_ports_lock, flags);
1633f048109Smalahal@us.ibm.com 	if (!phy->asd_port) {
1643f048109Smalahal@us.ibm.com 		for (i = 0; i < ASD_MAX_PHYS; i++) {
1653f048109Smalahal@us.ibm.com 			port = &asd_ha->asd_ports[i];
1663f048109Smalahal@us.ibm.com 
1673f048109Smalahal@us.ibm.com 			/* Check for wide port */
1683f048109Smalahal@us.ibm.com 			if (port->num_phys > 0 &&
1693f048109Smalahal@us.ibm.com 			    memcmp(port->sas_addr, sas_phy->sas_addr,
1703f048109Smalahal@us.ibm.com 				   SAS_ADDR_SIZE) == 0 &&
1713f048109Smalahal@us.ibm.com 			    memcmp(port->attached_sas_addr,
1723f048109Smalahal@us.ibm.com 				   sas_phy->attached_sas_addr,
1733f048109Smalahal@us.ibm.com 				   SAS_ADDR_SIZE) == 0) {
1743f048109Smalahal@us.ibm.com 				break;
1753f048109Smalahal@us.ibm.com 			}
1763f048109Smalahal@us.ibm.com 
1773f048109Smalahal@us.ibm.com 			/* Find a free port */
1783f048109Smalahal@us.ibm.com 			if (port->num_phys == 0 && free_port == NULL) {
1793f048109Smalahal@us.ibm.com 				free_port = port;
1803f048109Smalahal@us.ibm.com 			}
1813f048109Smalahal@us.ibm.com 		}
1823f048109Smalahal@us.ibm.com 
1833f048109Smalahal@us.ibm.com 		/* Use a free port if this doesn't form a wide port */
1843f048109Smalahal@us.ibm.com 		if (i >= ASD_MAX_PHYS) {
1853f048109Smalahal@us.ibm.com 			port = free_port;
1863f048109Smalahal@us.ibm.com 			BUG_ON(!port);
1873f048109Smalahal@us.ibm.com 			memcpy(port->sas_addr, sas_phy->sas_addr,
1883f048109Smalahal@us.ibm.com 			       SAS_ADDR_SIZE);
1893f048109Smalahal@us.ibm.com 			memcpy(port->attached_sas_addr,
1903f048109Smalahal@us.ibm.com 			       sas_phy->attached_sas_addr,
1913f048109Smalahal@us.ibm.com 			       SAS_ADDR_SIZE);
1923f048109Smalahal@us.ibm.com 		}
1933f048109Smalahal@us.ibm.com 		port->num_phys++;
1943f048109Smalahal@us.ibm.com 		port->phy_mask |= (1U << sas_phy->id);
1953f048109Smalahal@us.ibm.com 		phy->asd_port = port;
1963f048109Smalahal@us.ibm.com 	}
1973f048109Smalahal@us.ibm.com 	ASD_DPRINTK("%s: updating phy_mask 0x%x for phy%d\n",
198cadbd4a5SHarvey Harrison 		    __func__, phy->asd_port->phy_mask, sas_phy->id);
1993f048109Smalahal@us.ibm.com 	asd_update_port_links(asd_ha, phy);
2003f048109Smalahal@us.ibm.com 	spin_unlock_irqrestore(&asd_ha->asd_ports_lock, flags);
2013f048109Smalahal@us.ibm.com }
2023f048109Smalahal@us.ibm.com 
asd_deform_port(struct asd_ha_struct * asd_ha,struct asd_phy * phy)2033f048109Smalahal@us.ibm.com static void asd_deform_port(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
2043f048109Smalahal@us.ibm.com {
2053f048109Smalahal@us.ibm.com 	struct asd_port *port = phy->asd_port;
2063f048109Smalahal@us.ibm.com 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
2073f048109Smalahal@us.ibm.com 	unsigned long flags;
2083f048109Smalahal@us.ibm.com 
2093f048109Smalahal@us.ibm.com 	spin_lock_irqsave(&asd_ha->asd_ports_lock, flags);
2103f048109Smalahal@us.ibm.com 	if (port) {
2113f048109Smalahal@us.ibm.com 		port->num_phys--;
2123f048109Smalahal@us.ibm.com 		port->phy_mask &= ~(1U << sas_phy->id);
2133f048109Smalahal@us.ibm.com 		phy->asd_port = NULL;
2143f048109Smalahal@us.ibm.com 	}
2153f048109Smalahal@us.ibm.com 	spin_unlock_irqrestore(&asd_ha->asd_ports_lock, flags);
2163f048109Smalahal@us.ibm.com }
2173f048109Smalahal@us.ibm.com 
asd_bytes_dmaed_tasklet(struct asd_ascb * ascb,struct done_list_struct * dl,int edb_id,int phy_id)21881e56dedSAdrian Bunk static void asd_bytes_dmaed_tasklet(struct asd_ascb *ascb,
2192908d778SJames Bottomley 				    struct done_list_struct *dl,
2202908d778SJames Bottomley 				    int edb_id, int phy_id)
2212908d778SJames Bottomley {
2222908d778SJames Bottomley 	unsigned long flags;
2232908d778SJames Bottomley 	int edb_el = edb_id + ascb->edb_index;
2242908d778SJames Bottomley 	struct asd_dma_tok *edb = ascb->ha->seq.edb_arr[edb_el];
2252908d778SJames Bottomley 	struct asd_phy *phy = &ascb->ha->phys[phy_id];
2262908d778SJames Bottomley 	u16 size = ((dl->status_block[3] & 7) << 8) | dl->status_block[2];
2272908d778SJames Bottomley 
2282908d778SJames Bottomley 	size = min(size, (u16) sizeof(phy->frame_rcvd));
2292908d778SJames Bottomley 
2302908d778SJames Bottomley 	spin_lock_irqsave(&phy->sas_phy.frame_rcvd_lock, flags);
2312908d778SJames Bottomley 	memcpy(phy->sas_phy.frame_rcvd, edb->vaddr, size);
2322908d778SJames Bottomley 	phy->sas_phy.frame_rcvd_size = size;
2332908d778SJames Bottomley 	asd_get_attached_sas_addr(phy, phy->sas_phy.attached_sas_addr);
2342908d778SJames Bottomley 	spin_unlock_irqrestore(&phy->sas_phy.frame_rcvd_lock, flags);
2352908d778SJames Bottomley 	asd_dump_frame_rcvd(phy, dl);
2363f048109Smalahal@us.ibm.com 	asd_form_port(ascb->ha, phy);
237093289e4SAhmed S. Darwish 	sas_notify_port_event(&phy->sas_phy, PORTE_BYTES_DMAED, GFP_ATOMIC);
2382908d778SJames Bottomley }
2392908d778SJames Bottomley 
asd_link_reset_err_tasklet(struct asd_ascb * ascb,struct done_list_struct * dl,int phy_id)24081e56dedSAdrian Bunk static void asd_link_reset_err_tasklet(struct asd_ascb *ascb,
2412908d778SJames Bottomley 				       struct done_list_struct *dl,
2422908d778SJames Bottomley 				       int phy_id)
2432908d778SJames Bottomley {
2442908d778SJames Bottomley 	struct asd_ha_struct *asd_ha = ascb->ha;
2452908d778SJames Bottomley 	struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
2462908d778SJames Bottomley 	struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
2473f048109Smalahal@us.ibm.com 	struct asd_phy *phy = &asd_ha->phys[phy_id];
2482908d778SJames Bottomley 	u8 lr_error = dl->status_block[1];
2492908d778SJames Bottomley 	u8 retries_left = dl->status_block[2];
2502908d778SJames Bottomley 
2512908d778SJames Bottomley 	switch (lr_error) {
2522908d778SJames Bottomley 	case 0:
2532908d778SJames Bottomley 		ASD_DPRINTK("phy%d: Receive ID timer expired\n", phy_id);
2542908d778SJames Bottomley 		break;
2552908d778SJames Bottomley 	case 1:
2562908d778SJames Bottomley 		ASD_DPRINTK("phy%d: Loss of signal\n", phy_id);
2572908d778SJames Bottomley 		break;
2582908d778SJames Bottomley 	case 2:
2592908d778SJames Bottomley 		ASD_DPRINTK("phy%d: Loss of dword sync\n", phy_id);
2602908d778SJames Bottomley 		break;
2612908d778SJames Bottomley 	case 3:
2622908d778SJames Bottomley 		ASD_DPRINTK("phy%d: Receive FIS timeout\n", phy_id);
2632908d778SJames Bottomley 		break;
2642908d778SJames Bottomley 	default:
2652908d778SJames Bottomley 		ASD_DPRINTK("phy%d: unknown link reset error code: 0x%x\n",
2662908d778SJames Bottomley 			    phy_id, lr_error);
2672908d778SJames Bottomley 		break;
2682908d778SJames Bottomley 	}
2692908d778SJames Bottomley 
2702908d778SJames Bottomley 	asd_turn_led(asd_ha, phy_id, 0);
2712908d778SJames Bottomley 	sas_phy_disconnected(sas_phy);
2723f048109Smalahal@us.ibm.com 	asd_deform_port(asd_ha, phy);
273093289e4SAhmed S. Darwish 	sas_notify_port_event(sas_phy, PORTE_LINK_RESET_ERR, GFP_ATOMIC);
2742908d778SJames Bottomley 
2752908d778SJames Bottomley 	if (retries_left == 0) {
2762908d778SJames Bottomley 		int num = 1;
2772908d778SJames Bottomley 		struct asd_ascb *cp = asd_ascb_alloc_list(ascb->ha, &num,
2782908d778SJames Bottomley 							  GFP_ATOMIC);
2792908d778SJames Bottomley 		if (!cp) {
280cadbd4a5SHarvey Harrison 			asd_printk("%s: out of memory\n", __func__);
2812908d778SJames Bottomley 			goto out;
2822908d778SJames Bottomley 		}
2832908d778SJames Bottomley 		ASD_DPRINTK("phy%d: retries:0 performing link reset seq\n",
2842908d778SJames Bottomley 			    phy_id);
2852908d778SJames Bottomley 		asd_build_control_phy(cp, phy_id, ENABLE_PHY);
2862908d778SJames Bottomley 		if (asd_post_ascb_list(ascb->ha, cp, 1) != 0)
2872908d778SJames Bottomley 			asd_ascb_free(cp);
2882908d778SJames Bottomley 	}
2892908d778SJames Bottomley out:
2902908d778SJames Bottomley 	;
2912908d778SJames Bottomley }
2922908d778SJames Bottomley 
asd_primitive_rcvd_tasklet(struct asd_ascb * ascb,struct done_list_struct * dl,int phy_id)29381e56dedSAdrian Bunk static void asd_primitive_rcvd_tasklet(struct asd_ascb *ascb,
2942908d778SJames Bottomley 				       struct done_list_struct *dl,
2952908d778SJames Bottomley 				       int phy_id)
2962908d778SJames Bottomley {
2972908d778SJames Bottomley 	unsigned long flags;
2982908d778SJames Bottomley 	struct sas_ha_struct *sas_ha = &ascb->ha->sas_ha;
2992908d778SJames Bottomley 	struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
3003f048109Smalahal@us.ibm.com 	struct asd_ha_struct *asd_ha = ascb->ha;
3013f048109Smalahal@us.ibm.com 	struct asd_phy *phy = &asd_ha->phys[phy_id];
3022908d778SJames Bottomley 	u8  reg  = dl->status_block[1];
3032908d778SJames Bottomley 	u32 cont = dl->status_block[2] << ((reg & 3)*8);
3042908d778SJames Bottomley 
3052908d778SJames Bottomley 	reg &= ~3;
3062908d778SJames Bottomley 	switch (reg) {
3072908d778SJames Bottomley 	case LmPRMSTAT0BYTE0:
3082908d778SJames Bottomley 		switch (cont) {
3092908d778SJames Bottomley 		case LmBROADCH:
3102908d778SJames Bottomley 		case LmBROADRVCH0:
3112908d778SJames Bottomley 		case LmBROADRVCH1:
3122908d778SJames Bottomley 		case LmBROADSES:
3132908d778SJames Bottomley 			ASD_DPRINTK("phy%d: BROADCAST change received:%d\n",
3142908d778SJames Bottomley 				    phy_id, cont);
3152908d778SJames Bottomley 			spin_lock_irqsave(&sas_phy->sas_prim_lock, flags);
3162908d778SJames Bottomley 			sas_phy->sas_prim = ffs(cont);
3172908d778SJames Bottomley 			spin_unlock_irqrestore(&sas_phy->sas_prim_lock, flags);
318093289e4SAhmed S. Darwish 			sas_notify_port_event(sas_phy, PORTE_BROADCAST_RCVD,
319111d06abSAhmed S. Darwish 					      GFP_ATOMIC);
3202908d778SJames Bottomley 			break;
3212908d778SJames Bottomley 
3222908d778SJames Bottomley 		case LmUNKNOWNP:
3232908d778SJames Bottomley 			ASD_DPRINTK("phy%d: unknown BREAK\n", phy_id);
3242908d778SJames Bottomley 			break;
3252908d778SJames Bottomley 
3262908d778SJames Bottomley 		default:
3272908d778SJames Bottomley 			ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
3282908d778SJames Bottomley 				    phy_id, reg, cont);
3292908d778SJames Bottomley 			break;
3302908d778SJames Bottomley 		}
3312908d778SJames Bottomley 		break;
3322908d778SJames Bottomley 	case LmPRMSTAT1BYTE0:
3332908d778SJames Bottomley 		switch (cont) {
3342908d778SJames Bottomley 		case LmHARDRST:
3352908d778SJames Bottomley 			ASD_DPRINTK("phy%d: HARD_RESET primitive rcvd\n",
3362908d778SJames Bottomley 				    phy_id);
3372908d778SJames Bottomley 			/* The sequencer disables all phys on that port.
3382908d778SJames Bottomley 			 * We have to re-enable the phys ourselves. */
3393f048109Smalahal@us.ibm.com 			asd_deform_port(asd_ha, phy);
340093289e4SAhmed S. Darwish 			sas_notify_port_event(sas_phy, PORTE_HARD_RESET,
341111d06abSAhmed S. Darwish 					      GFP_ATOMIC);
3422908d778SJames Bottomley 			break;
3432908d778SJames Bottomley 
3442908d778SJames Bottomley 		default:
3452908d778SJames Bottomley 			ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
3462908d778SJames Bottomley 				    phy_id, reg, cont);
3472908d778SJames Bottomley 			break;
3482908d778SJames Bottomley 		}
3492908d778SJames Bottomley 		break;
3502908d778SJames Bottomley 	default:
3512908d778SJames Bottomley 		ASD_DPRINTK("unknown primitive register:0x%x\n",
3522908d778SJames Bottomley 			    dl->status_block[1]);
3532908d778SJames Bottomley 		break;
3542908d778SJames Bottomley 	}
3552908d778SJames Bottomley }
3562908d778SJames Bottomley 
3572908d778SJames Bottomley /**
3582908d778SJames Bottomley  * asd_invalidate_edb -- invalidate an EDB and if necessary post the ESCB
3592908d778SJames Bottomley  * @ascb: pointer to Empty SCB
3602908d778SJames Bottomley  * @edb_id: index [0,6] to the empty data buffer which is to be invalidated
3612908d778SJames Bottomley  *
3622908d778SJames Bottomley  * After an EDB has been invalidated, if all EDBs in this ESCB have been
3632908d778SJames Bottomley  * invalidated, the ESCB is posted back to the sequencer.
3642908d778SJames Bottomley  * Context is tasklet/IRQ.
3652908d778SJames Bottomley  */
asd_invalidate_edb(struct asd_ascb * ascb,int edb_id)3662908d778SJames Bottomley void asd_invalidate_edb(struct asd_ascb *ascb, int edb_id)
3672908d778SJames Bottomley {
3682908d778SJames Bottomley 	struct asd_seq_data *seq = &ascb->ha->seq;
3692908d778SJames Bottomley 	struct empty_scb *escb = &ascb->scb->escb;
3702908d778SJames Bottomley 	struct sg_el     *eb   = &escb->eb[edb_id];
3712908d778SJames Bottomley 	struct asd_dma_tok *edb = seq->edb_arr[ascb->edb_index + edb_id];
3722908d778SJames Bottomley 
3732908d778SJames Bottomley 	memset(edb->vaddr, 0, ASD_EDB_SIZE);
3742908d778SJames Bottomley 	eb->flags |= ELEMENT_NOT_VALID;
3752908d778SJames Bottomley 	escb->num_valid--;
3762908d778SJames Bottomley 
3772908d778SJames Bottomley 	if (escb->num_valid == 0) {
3782908d778SJames Bottomley 		int i;
3792908d778SJames Bottomley 		/* ASD_DPRINTK("reposting escb: vaddr: 0x%p, "
3802908d778SJames Bottomley 			    "dma_handle: 0x%08llx, next: 0x%08llx, "
3812908d778SJames Bottomley 			    "index:%d, opcode:0x%02x\n",
3822908d778SJames Bottomley 			    ascb->dma_scb.vaddr,
3832908d778SJames Bottomley 			    (u64)ascb->dma_scb.dma_handle,
3842908d778SJames Bottomley 			    le64_to_cpu(ascb->scb->header.next_scb),
3852908d778SJames Bottomley 			    le16_to_cpu(ascb->scb->header.index),
3862908d778SJames Bottomley 			    ascb->scb->header.opcode);
3872908d778SJames Bottomley 		*/
3882908d778SJames Bottomley 		escb->num_valid = ASD_EDBS_PER_SCB;
3892908d778SJames Bottomley 		for (i = 0; i < ASD_EDBS_PER_SCB; i++)
3902908d778SJames Bottomley 			escb->eb[i].flags = 0;
3912908d778SJames Bottomley 		if (!list_empty(&ascb->list))
3922908d778SJames Bottomley 			list_del_init(&ascb->list);
3932908d778SJames Bottomley 		i = asd_post_escb_list(ascb->ha, ascb, 1);
3942908d778SJames Bottomley 		if (i)
3952908d778SJames Bottomley 			asd_printk("couldn't post escb, err:%d\n", i);
3962908d778SJames Bottomley 	}
3972908d778SJames Bottomley }
3982908d778SJames Bottomley 
escb_tasklet_complete(struct asd_ascb * ascb,struct done_list_struct * dl)3992908d778SJames Bottomley static void escb_tasklet_complete(struct asd_ascb *ascb,
4002908d778SJames Bottomley 				  struct done_list_struct *dl)
4012908d778SJames Bottomley {
4022908d778SJames Bottomley 	struct asd_ha_struct *asd_ha = ascb->ha;
4032908d778SJames Bottomley 	struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
4042908d778SJames Bottomley 	int edb = (dl->opcode & DL_PHY_MASK) - 1; /* [0xc1,0xc7] -> [0,6] */
4052908d778SJames Bottomley 	u8  sb_opcode = dl->status_block[0];
4062908d778SJames Bottomley 	int phy_id = sb_opcode & DL_PHY_MASK;
4072908d778SJames Bottomley 	struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
4083f048109Smalahal@us.ibm.com 	struct asd_phy *phy = &asd_ha->phys[phy_id];
4092908d778SJames Bottomley 
4102908d778SJames Bottomley 	if (edb > 6 || edb < 0) {
4112908d778SJames Bottomley 		ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
4122908d778SJames Bottomley 			    edb, dl->opcode);
4132908d778SJames Bottomley 		ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
4142908d778SJames Bottomley 			    sb_opcode, phy_id);
4152908d778SJames Bottomley 		ASD_DPRINTK("escb: vaddr: 0x%p, "
4162908d778SJames Bottomley 			    "dma_handle: 0x%llx, next: 0x%llx, "
4172908d778SJames Bottomley 			    "index:%d, opcode:0x%02x\n",
4182908d778SJames Bottomley 			    ascb->dma_scb.vaddr,
4192908d778SJames Bottomley 			    (unsigned long long)ascb->dma_scb.dma_handle,
4202908d778SJames Bottomley 			    (unsigned long long)
4212908d778SJames Bottomley 			    le64_to_cpu(ascb->scb->header.next_scb),
4222908d778SJames Bottomley 			    le16_to_cpu(ascb->scb->header.index),
4232908d778SJames Bottomley 			    ascb->scb->header.opcode);
4242908d778SJames Bottomley 	}
4252908d778SJames Bottomley 
426fe4a36cfSDarrick J. Wong 	/* Catch these before we mask off the sb_opcode bits */
427fe4a36cfSDarrick J. Wong 	switch (sb_opcode) {
428fe4a36cfSDarrick J. Wong 	case REQ_TASK_ABORT: {
429fe4a36cfSDarrick J. Wong 		struct asd_ascb *a, *b;
430fe4a36cfSDarrick J. Wong 		u16 tc_abort;
4313cd041fbSDarrick J. Wong 		struct domain_device *failed_dev = NULL;
432fe4a36cfSDarrick J. Wong 
433fe4a36cfSDarrick J. Wong 		ASD_DPRINTK("%s: REQ_TASK_ABORT, reason=0x%X\n",
434cadbd4a5SHarvey Harrison 			    __func__, dl->status_block[3]);
435fe4a36cfSDarrick J. Wong 
4363cd041fbSDarrick J. Wong 		/*
4373cd041fbSDarrick J. Wong 		 * Find the task that caused the abort and abort it first.
4383cd041fbSDarrick J. Wong 		 * The sequencer won't put anything on the done list until
4393cd041fbSDarrick J. Wong 		 * that happens.
4403cd041fbSDarrick J. Wong 		 */
4413cd041fbSDarrick J. Wong 		tc_abort = *((u16*)(&dl->status_block[1]));
4423cd041fbSDarrick J. Wong 		tc_abort = le16_to_cpu(tc_abort);
4433cd041fbSDarrick J. Wong 
4443cd041fbSDarrick J. Wong 		list_for_each_entry_safe(a, b, &asd_ha->seq.pend_q, list) {
445cb84e2d2SJames Bottomley 			struct sas_task *task = a->uldd_task;
4463cd041fbSDarrick J. Wong 
447cb84e2d2SJames Bottomley 			if (a->tc_index != tc_abort)
448cb84e2d2SJames Bottomley 				continue;
449cb84e2d2SJames Bottomley 
450cb84e2d2SJames Bottomley 			if (task) {
4513cd041fbSDarrick J. Wong 				failed_dev = task->dev;
4523cd041fbSDarrick J. Wong 				sas_task_abort(task);
453cb84e2d2SJames Bottomley 			} else {
454cb84e2d2SJames Bottomley 				ASD_DPRINTK("R_T_A for non TASK scb 0x%x\n",
455cb84e2d2SJames Bottomley 					    a->scb->header.opcode);
456fe4a36cfSDarrick J. Wong 			}
457cb84e2d2SJames Bottomley 			break;
4583cd041fbSDarrick J. Wong 		}
4593cd041fbSDarrick J. Wong 
4603cd041fbSDarrick J. Wong 		if (!failed_dev) {
4613cd041fbSDarrick J. Wong 			ASD_DPRINTK("%s: Can't find task (tc=%d) to abort!\n",
462cadbd4a5SHarvey Harrison 				    __func__, tc_abort);
4633cd041fbSDarrick J. Wong 			goto out;
4643cd041fbSDarrick J. Wong 		}
4653cd041fbSDarrick J. Wong 
4663cd041fbSDarrick J. Wong 		/*
4673cd041fbSDarrick J. Wong 		 * Now abort everything else for that device (hba?) so
4683cd041fbSDarrick J. Wong 		 * that the EH will wake up and do something.
4693cd041fbSDarrick J. Wong 		 */
4703cd041fbSDarrick J. Wong 		list_for_each_entry_safe(a, b, &asd_ha->seq.pend_q, list) {
471cb84e2d2SJames Bottomley 			struct sas_task *task = a->uldd_task;
4723cd041fbSDarrick J. Wong 
4733cd041fbSDarrick J. Wong 			if (task &&
4743cd041fbSDarrick J. Wong 			    task->dev == failed_dev &&
4753cd041fbSDarrick J. Wong 			    a->tc_index != tc_abort)
4763cd041fbSDarrick J. Wong 				sas_task_abort(task);
4773cd041fbSDarrick J. Wong 		}
4783cd041fbSDarrick J. Wong 
479fe4a36cfSDarrick J. Wong 		goto out;
480fe4a36cfSDarrick J. Wong 	}
481fe4a36cfSDarrick J. Wong 	case REQ_DEVICE_RESET: {
482dea22214SDarrick J. Wong 		struct asd_ascb *a;
483fe4a36cfSDarrick J. Wong 		u16 conn_handle;
4843cd041fbSDarrick J. Wong 		unsigned long flags;
4853cd041fbSDarrick J. Wong 		struct sas_task *last_dev_task = NULL;
486fe4a36cfSDarrick J. Wong 
487fe4a36cfSDarrick J. Wong 		conn_handle = *((u16*)(&dl->status_block[1]));
488fe4a36cfSDarrick J. Wong 		conn_handle = le16_to_cpu(conn_handle);
489fe4a36cfSDarrick J. Wong 
490cadbd4a5SHarvey Harrison 		ASD_DPRINTK("%s: REQ_DEVICE_RESET, reason=0x%X\n", __func__,
491fe4a36cfSDarrick J. Wong 			    dl->status_block[3]);
492fe4a36cfSDarrick J. Wong 
4933cd041fbSDarrick J. Wong 		/* Find the last pending task for the device... */
494dea22214SDarrick J. Wong 		list_for_each_entry(a, &asd_ha->seq.pend_q, list) {
495fe4a36cfSDarrick J. Wong 			u16 x;
4963cd041fbSDarrick J. Wong 			struct domain_device *dev;
4973cd041fbSDarrick J. Wong 			struct sas_task *task = a->uldd_task;
498fe4a36cfSDarrick J. Wong 
499dea22214SDarrick J. Wong 			if (!task)
500dea22214SDarrick J. Wong 				continue;
501dea22214SDarrick J. Wong 			dev = task->dev;
502dea22214SDarrick J. Wong 
503e138a5d2SJames Bottomley 			x = (unsigned long)dev->lldd_dev;
5043cd041fbSDarrick J. Wong 			if (x == conn_handle)
5053cd041fbSDarrick J. Wong 				last_dev_task = task;
506dea22214SDarrick J. Wong 		}
507fe4a36cfSDarrick J. Wong 
5083cd041fbSDarrick J. Wong 		if (!last_dev_task) {
5093cd041fbSDarrick J. Wong 			ASD_DPRINTK("%s: Device reset for idle device %d?\n",
510cadbd4a5SHarvey Harrison 				    __func__, conn_handle);
511dea22214SDarrick J. Wong 			goto out;
512dea22214SDarrick J. Wong 		}
5133cd041fbSDarrick J. Wong 
5143cd041fbSDarrick J. Wong 		/* ...and set the reset flag */
5153cd041fbSDarrick J. Wong 		spin_lock_irqsave(&last_dev_task->task_state_lock, flags);
5163cd041fbSDarrick J. Wong 		last_dev_task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
5173cd041fbSDarrick J. Wong 		spin_unlock_irqrestore(&last_dev_task->task_state_lock, flags);
5183cd041fbSDarrick J. Wong 
5193cd041fbSDarrick J. Wong 		/* Kill all pending tasks for the device */
5203cd041fbSDarrick J. Wong 		list_for_each_entry(a, &asd_ha->seq.pend_q, list) {
5213cd041fbSDarrick J. Wong 			u16 x;
5223cd041fbSDarrick J. Wong 			struct domain_device *dev;
5233cd041fbSDarrick J. Wong 			struct sas_task *task = a->uldd_task;
5243cd041fbSDarrick J. Wong 
5253cd041fbSDarrick J. Wong 			if (!task)
5263cd041fbSDarrick J. Wong 				continue;
5273cd041fbSDarrick J. Wong 			dev = task->dev;
5283cd041fbSDarrick J. Wong 
5293cd041fbSDarrick J. Wong 			x = (unsigned long)dev->lldd_dev;
5303cd041fbSDarrick J. Wong 			if (x == conn_handle)
5313cd041fbSDarrick J. Wong 				sas_task_abort(task);
5323cd041fbSDarrick J. Wong 		}
5333cd041fbSDarrick J. Wong 
534fe4a36cfSDarrick J. Wong 		goto out;
535fe4a36cfSDarrick J. Wong 	}
536fe4a36cfSDarrick J. Wong 	case SIGNAL_NCQ_ERROR:
537cadbd4a5SHarvey Harrison 		ASD_DPRINTK("%s: SIGNAL_NCQ_ERROR\n", __func__);
538fe4a36cfSDarrick J. Wong 		goto out;
539fe4a36cfSDarrick J. Wong 	case CLEAR_NCQ_ERROR:
540cadbd4a5SHarvey Harrison 		ASD_DPRINTK("%s: CLEAR_NCQ_ERROR\n", __func__);
541fe4a36cfSDarrick J. Wong 		goto out;
542fe4a36cfSDarrick J. Wong 	}
543fe4a36cfSDarrick J. Wong 
5442908d778SJames Bottomley 	sb_opcode &= ~DL_PHY_MASK;
5452908d778SJames Bottomley 
5462908d778SJames Bottomley 	switch (sb_opcode) {
5472908d778SJames Bottomley 	case BYTES_DMAED:
548cadbd4a5SHarvey Harrison 		ASD_DPRINTK("%s: phy%d: BYTES_DMAED\n", __func__, phy_id);
5492908d778SJames Bottomley 		asd_bytes_dmaed_tasklet(ascb, dl, edb, phy_id);
5502908d778SJames Bottomley 		break;
5512908d778SJames Bottomley 	case PRIMITIVE_RECVD:
552cadbd4a5SHarvey Harrison 		ASD_DPRINTK("%s: phy%d: PRIMITIVE_RECVD\n", __func__,
5532908d778SJames Bottomley 			    phy_id);
5542908d778SJames Bottomley 		asd_primitive_rcvd_tasklet(ascb, dl, phy_id);
5552908d778SJames Bottomley 		break;
5562908d778SJames Bottomley 	case PHY_EVENT:
557cadbd4a5SHarvey Harrison 		ASD_DPRINTK("%s: phy%d: PHY_EVENT\n", __func__, phy_id);
5582908d778SJames Bottomley 		asd_phy_event_tasklet(ascb, dl);
5592908d778SJames Bottomley 		break;
5602908d778SJames Bottomley 	case LINK_RESET_ERROR:
561cadbd4a5SHarvey Harrison 		ASD_DPRINTK("%s: phy%d: LINK_RESET_ERROR\n", __func__,
5622908d778SJames Bottomley 			    phy_id);
5632908d778SJames Bottomley 		asd_link_reset_err_tasklet(ascb, dl, phy_id);
5642908d778SJames Bottomley 		break;
5652908d778SJames Bottomley 	case TIMER_EVENT:
5662908d778SJames Bottomley 		ASD_DPRINTK("%s: phy%d: TIMER_EVENT, lost dw sync\n",
567cadbd4a5SHarvey Harrison 			    __func__, phy_id);
5682908d778SJames Bottomley 		asd_turn_led(asd_ha, phy_id, 0);
5692908d778SJames Bottomley 		/* the device is gone */
5702908d778SJames Bottomley 		sas_phy_disconnected(sas_phy);
5713f048109Smalahal@us.ibm.com 		asd_deform_port(asd_ha, phy);
572093289e4SAhmed S. Darwish 		sas_notify_port_event(sas_phy, PORTE_TIMER_EVENT, GFP_ATOMIC);
5732908d778SJames Bottomley 		break;
5742908d778SJames Bottomley 	default:
575cadbd4a5SHarvey Harrison 		ASD_DPRINTK("%s: phy%d: unknown event:0x%x\n", __func__,
5762908d778SJames Bottomley 			    phy_id, sb_opcode);
5772908d778SJames Bottomley 		ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
5782908d778SJames Bottomley 			    edb, dl->opcode);
5792908d778SJames Bottomley 		ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
5802908d778SJames Bottomley 			    sb_opcode, phy_id);
5812908d778SJames Bottomley 		ASD_DPRINTK("escb: vaddr: 0x%p, "
5822908d778SJames Bottomley 			    "dma_handle: 0x%llx, next: 0x%llx, "
5832908d778SJames Bottomley 			    "index:%d, opcode:0x%02x\n",
5842908d778SJames Bottomley 			    ascb->dma_scb.vaddr,
5852908d778SJames Bottomley 			    (unsigned long long)ascb->dma_scb.dma_handle,
5862908d778SJames Bottomley 			    (unsigned long long)
5872908d778SJames Bottomley 			    le64_to_cpu(ascb->scb->header.next_scb),
5882908d778SJames Bottomley 			    le16_to_cpu(ascb->scb->header.index),
5892908d778SJames Bottomley 			    ascb->scb->header.opcode);
5902908d778SJames Bottomley 
5912908d778SJames Bottomley 		break;
5922908d778SJames Bottomley 	}
593fe4a36cfSDarrick J. Wong out:
5942908d778SJames Bottomley 	asd_invalidate_edb(ascb, edb);
5952908d778SJames Bottomley }
5962908d778SJames Bottomley 
asd_init_post_escbs(struct asd_ha_struct * asd_ha)5972908d778SJames Bottomley int asd_init_post_escbs(struct asd_ha_struct *asd_ha)
5982908d778SJames Bottomley {
5992908d778SJames Bottomley 	struct asd_seq_data *seq = &asd_ha->seq;
6002908d778SJames Bottomley 	int i;
6012908d778SJames Bottomley 
6022908d778SJames Bottomley 	for (i = 0; i < seq->num_escbs; i++)
6032908d778SJames Bottomley 		seq->escb_arr[i]->tasklet_complete = escb_tasklet_complete;
6042908d778SJames Bottomley 
6052908d778SJames Bottomley 	ASD_DPRINTK("posting %d escbs\n", i);
6062908d778SJames Bottomley 	return asd_post_escb_list(asd_ha, seq->escb_arr[0], seq->num_escbs);
6072908d778SJames Bottomley }
6082908d778SJames Bottomley 
6092908d778SJames Bottomley /* ---------- CONTROL PHY ---------- */
6102908d778SJames Bottomley 
6112908d778SJames Bottomley #define CONTROL_PHY_STATUS (CURRENT_DEVICE_PRESENT | CURRENT_OOB_DONE   \
6122908d778SJames Bottomley 			    | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
6132908d778SJames Bottomley 			    | CURRENT_OOB_ERROR)
6142908d778SJames Bottomley 
6152908d778SJames Bottomley /**
6162908d778SJames Bottomley  * control_phy_tasklet_complete -- tasklet complete for CONTROL PHY ascb
6172908d778SJames Bottomley  * @ascb: pointer to an ascb
6182908d778SJames Bottomley  * @dl: pointer to the done list entry
6192908d778SJames Bottomley  *
6202908d778SJames Bottomley  * This function completes a CONTROL PHY scb and frees the ascb.
6212908d778SJames Bottomley  * A note on LEDs:
6222908d778SJames Bottomley  *  - an LED blinks if there is IO though it,
6232908d778SJames Bottomley  *  - if a device is connected to the LED, it is lit,
6242908d778SJames Bottomley  *  - if no device is connected to the LED, is is dimmed (off).
6252908d778SJames Bottomley  */
control_phy_tasklet_complete(struct asd_ascb * ascb,struct done_list_struct * dl)6262908d778SJames Bottomley static void control_phy_tasklet_complete(struct asd_ascb *ascb,
6272908d778SJames Bottomley 					 struct done_list_struct *dl)
6282908d778SJames Bottomley {
6292908d778SJames Bottomley 	struct asd_ha_struct *asd_ha = ascb->ha;
6302908d778SJames Bottomley 	struct scb *scb = ascb->scb;
6312908d778SJames Bottomley 	struct control_phy *control_phy = &scb->control_phy;
6322908d778SJames Bottomley 	u8 phy_id = control_phy->phy_id;
6332908d778SJames Bottomley 	struct asd_phy *phy = &ascb->ha->phys[phy_id];
6342908d778SJames Bottomley 
6352908d778SJames Bottomley 	u8 status     = dl->status_block[0];
6362908d778SJames Bottomley 	u8 oob_status = dl->status_block[1];
6372908d778SJames Bottomley 	u8 oob_mode   = dl->status_block[2];
6382908d778SJames Bottomley 	/* u8 oob_signals= dl->status_block[3]; */
6392908d778SJames Bottomley 
6402908d778SJames Bottomley 	if (status != 0) {
6412908d778SJames Bottomley 		ASD_DPRINTK("%s: phy%d status block opcode:0x%x\n",
642cadbd4a5SHarvey Harrison 			    __func__, phy_id, status);
6432908d778SJames Bottomley 		goto out;
6442908d778SJames Bottomley 	}
6452908d778SJames Bottomley 
6462908d778SJames Bottomley 	switch (control_phy->sub_func) {
6472908d778SJames Bottomley 	case DISABLE_PHY:
6482908d778SJames Bottomley 		asd_ha->hw_prof.enabled_phys &= ~(1 << phy_id);
6492908d778SJames Bottomley 		asd_turn_led(asd_ha, phy_id, 0);
6502908d778SJames Bottomley 		asd_control_led(asd_ha, phy_id, 0);
651cadbd4a5SHarvey Harrison 		ASD_DPRINTK("%s: disable phy%d\n", __func__, phy_id);
6522908d778SJames Bottomley 		break;
6532908d778SJames Bottomley 
6542908d778SJames Bottomley 	case ENABLE_PHY:
6552908d778SJames Bottomley 		asd_control_led(asd_ha, phy_id, 1);
6562908d778SJames Bottomley 		if (oob_status & CURRENT_OOB_DONE) {
6572908d778SJames Bottomley 			asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
6582908d778SJames Bottomley 			get_lrate_mode(phy, oob_mode);
6592908d778SJames Bottomley 			asd_turn_led(asd_ha, phy_id, 1);
6602908d778SJames Bottomley 			ASD_DPRINTK("%s: phy%d, lrate:0x%x, proto:0x%x\n",
661cadbd4a5SHarvey Harrison 				    __func__, phy_id,phy->sas_phy.linkrate,
6622908d778SJames Bottomley 				    phy->sas_phy.iproto);
6632908d778SJames Bottomley 		} else if (oob_status & CURRENT_SPINUP_HOLD) {
6642908d778SJames Bottomley 			asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
6652908d778SJames Bottomley 			asd_turn_led(asd_ha, phy_id, 1);
666cadbd4a5SHarvey Harrison 			ASD_DPRINTK("%s: phy%d, spinup hold\n", __func__,
6672908d778SJames Bottomley 				    phy_id);
6682908d778SJames Bottomley 		} else if (oob_status & CURRENT_ERR_MASK) {
6692908d778SJames Bottomley 			asd_turn_led(asd_ha, phy_id, 0);
6702908d778SJames Bottomley 			ASD_DPRINTK("%s: phy%d: error: oob status:0x%02x\n",
671cadbd4a5SHarvey Harrison 				    __func__, phy_id, oob_status);
6722908d778SJames Bottomley 		} else if (oob_status & (CURRENT_HOT_PLUG_CNCT
6732908d778SJames Bottomley 					 | CURRENT_DEVICE_PRESENT))  {
6742908d778SJames Bottomley 			asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
6752908d778SJames Bottomley 			asd_turn_led(asd_ha, phy_id, 1);
6762908d778SJames Bottomley 			ASD_DPRINTK("%s: phy%d: hot plug or device present\n",
677cadbd4a5SHarvey Harrison 				    __func__, phy_id);
6782908d778SJames Bottomley 		} else {
6792908d778SJames Bottomley 			asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
6802908d778SJames Bottomley 			asd_turn_led(asd_ha, phy_id, 0);
6812908d778SJames Bottomley 			ASD_DPRINTK("%s: phy%d: no device present: "
6822908d778SJames Bottomley 				    "oob_status:0x%x\n",
683cadbd4a5SHarvey Harrison 				    __func__, phy_id, oob_status);
6842908d778SJames Bottomley 		}
6852908d778SJames Bottomley 		break;
6862908d778SJames Bottomley 	case RELEASE_SPINUP_HOLD:
6872908d778SJames Bottomley 	case PHY_NO_OP:
6882908d778SJames Bottomley 	case EXECUTE_HARD_RESET:
689cadbd4a5SHarvey Harrison 		ASD_DPRINTK("%s: phy%d: sub_func:0x%x\n", __func__,
6902908d778SJames Bottomley 			    phy_id, control_phy->sub_func);
6912908d778SJames Bottomley 		/* XXX finish */
6922908d778SJames Bottomley 		break;
6932908d778SJames Bottomley 	default:
694cadbd4a5SHarvey Harrison 		ASD_DPRINTK("%s: phy%d: sub_func:0x%x?\n", __func__,
6952908d778SJames Bottomley 			    phy_id, control_phy->sub_func);
6962908d778SJames Bottomley 		break;
6972908d778SJames Bottomley 	}
6982908d778SJames Bottomley out:
6992908d778SJames Bottomley 	asd_ascb_free(ascb);
7002908d778SJames Bottomley }
7012908d778SJames Bottomley 
set_speed_mask(u8 * speed_mask,struct asd_phy_desc * pd)70281e56dedSAdrian Bunk static void set_speed_mask(u8 *speed_mask, struct asd_phy_desc *pd)
7032908d778SJames Bottomley {
7042908d778SJames Bottomley 	/* disable all speeds, then enable defaults */
7052908d778SJames Bottomley 	*speed_mask = SAS_SPEED_60_DIS | SAS_SPEED_30_DIS | SAS_SPEED_15_DIS
7062908d778SJames Bottomley 		| SATA_SPEED_30_DIS | SATA_SPEED_15_DIS;
7072908d778SJames Bottomley 
7082908d778SJames Bottomley 	switch (pd->max_sas_lrate) {
70988edf746SJames Bottomley 	case SAS_LINK_RATE_6_0_GBPS:
7102908d778SJames Bottomley 		*speed_mask &= ~SAS_SPEED_60_DIS;
711df561f66SGustavo A. R. Silva 		fallthrough;
7122908d778SJames Bottomley 	default:
71388edf746SJames Bottomley 	case SAS_LINK_RATE_3_0_GBPS:
7142908d778SJames Bottomley 		*speed_mask &= ~SAS_SPEED_30_DIS;
715df561f66SGustavo A. R. Silva 		fallthrough;
71688edf746SJames Bottomley 	case SAS_LINK_RATE_1_5_GBPS:
7172908d778SJames Bottomley 		*speed_mask &= ~SAS_SPEED_15_DIS;
7182908d778SJames Bottomley 	}
7192908d778SJames Bottomley 
7202908d778SJames Bottomley 	switch (pd->min_sas_lrate) {
72188edf746SJames Bottomley 	case SAS_LINK_RATE_6_0_GBPS:
7222908d778SJames Bottomley 		*speed_mask |= SAS_SPEED_30_DIS;
723df561f66SGustavo A. R. Silva 		fallthrough;
72488edf746SJames Bottomley 	case SAS_LINK_RATE_3_0_GBPS:
7252908d778SJames Bottomley 		*speed_mask |= SAS_SPEED_15_DIS;
7264886dd0aSGustavo A. R. Silva 		fallthrough;
7272908d778SJames Bottomley 	default:
72888edf746SJames Bottomley 	case SAS_LINK_RATE_1_5_GBPS:
7292908d778SJames Bottomley 		/* nothing to do */
7302908d778SJames Bottomley 		;
7312908d778SJames Bottomley 	}
7322908d778SJames Bottomley 
7332908d778SJames Bottomley 	switch (pd->max_sata_lrate) {
73488edf746SJames Bottomley 	case SAS_LINK_RATE_3_0_GBPS:
7352908d778SJames Bottomley 		*speed_mask &= ~SATA_SPEED_30_DIS;
736df561f66SGustavo A. R. Silva 		fallthrough;
7372908d778SJames Bottomley 	default:
73888edf746SJames Bottomley 	case SAS_LINK_RATE_1_5_GBPS:
7392908d778SJames Bottomley 		*speed_mask &= ~SATA_SPEED_15_DIS;
7402908d778SJames Bottomley 	}
7412908d778SJames Bottomley 
7422908d778SJames Bottomley 	switch (pd->min_sata_lrate) {
74388edf746SJames Bottomley 	case SAS_LINK_RATE_3_0_GBPS:
7442908d778SJames Bottomley 		*speed_mask |= SATA_SPEED_15_DIS;
7454886dd0aSGustavo A. R. Silva 		fallthrough;
7462908d778SJames Bottomley 	default:
74788edf746SJames Bottomley 	case SAS_LINK_RATE_1_5_GBPS:
7482908d778SJames Bottomley 		/* nothing to do */
7492908d778SJames Bottomley 		;
7502908d778SJames Bottomley 	}
7512908d778SJames Bottomley }
7522908d778SJames Bottomley 
7532908d778SJames Bottomley /**
7542908d778SJames Bottomley  * asd_build_control_phy -- build a CONTROL PHY SCB
7552908d778SJames Bottomley  * @ascb: pointer to an ascb
7562908d778SJames Bottomley  * @phy_id: phy id to control, integer
7572908d778SJames Bottomley  * @subfunc: subfunction, what to actually to do the phy
7582908d778SJames Bottomley  *
7592908d778SJames Bottomley  * This function builds a CONTROL PHY scb.  No allocation of any kind
7602908d778SJames Bottomley  * is performed. @ascb is allocated with the list function.
7612908d778SJames Bottomley  * The caller can override the ascb->tasklet_complete to point
7622908d778SJames Bottomley  * to its own callback function.  It must call asd_ascb_free()
7632908d778SJames Bottomley  * at its tasklet complete function.
7642908d778SJames Bottomley  * See the default implementation.
7652908d778SJames Bottomley  */
asd_build_control_phy(struct asd_ascb * ascb,int phy_id,u8 subfunc)7662908d778SJames Bottomley void asd_build_control_phy(struct asd_ascb *ascb, int phy_id, u8 subfunc)
7672908d778SJames Bottomley {
7682908d778SJames Bottomley 	struct asd_phy *phy = &ascb->ha->phys[phy_id];
7692908d778SJames Bottomley 	struct scb *scb = ascb->scb;
7702908d778SJames Bottomley 	struct control_phy *control_phy = &scb->control_phy;
7712908d778SJames Bottomley 
7722908d778SJames Bottomley 	scb->header.opcode = CONTROL_PHY;
7732908d778SJames Bottomley 	control_phy->phy_id = (u8) phy_id;
7742908d778SJames Bottomley 	control_phy->sub_func = subfunc;
7752908d778SJames Bottomley 
7762908d778SJames Bottomley 	switch (subfunc) {
7772908d778SJames Bottomley 	case EXECUTE_HARD_RESET:  /* 0x81 */
7782908d778SJames Bottomley 	case ENABLE_PHY:          /* 0x01 */
7792908d778SJames Bottomley 		/* decide hot plug delay */
7802908d778SJames Bottomley 		control_phy->hot_plug_delay = HOTPLUG_DELAY_TIMEOUT;
7812908d778SJames Bottomley 
7822908d778SJames Bottomley 		/* decide speed mask */
7832908d778SJames Bottomley 		set_speed_mask(&control_phy->speed_mask, phy->phy_desc);
7842908d778SJames Bottomley 
7852908d778SJames Bottomley 		/* initiator port settings are in the hi nibble */
7862908d778SJames Bottomley 		if (phy->sas_phy.role == PHY_ROLE_INITIATOR)
7875929faf3SDarrick J. Wong 			control_phy->port_type = SAS_PROTOCOL_ALL << 4;
7882908d778SJames Bottomley 		else if (phy->sas_phy.role == PHY_ROLE_TARGET)
7895929faf3SDarrick J. Wong 			control_phy->port_type = SAS_PROTOCOL_ALL;
7902908d778SJames Bottomley 		else
7912908d778SJames Bottomley 			control_phy->port_type =
7925929faf3SDarrick J. Wong 				(SAS_PROTOCOL_ALL << 4) | SAS_PROTOCOL_ALL;
7932908d778SJames Bottomley 
7942908d778SJames Bottomley 		/* link reset retries, this should be nominal */
7952908d778SJames Bottomley 		control_phy->link_reset_retries = 10;
796df561f66SGustavo A. R. Silva 		fallthrough;
7972908d778SJames Bottomley 
7982908d778SJames Bottomley 	case RELEASE_SPINUP_HOLD: /* 0x02 */
7992908d778SJames Bottomley 		/* decide the func_mask */
8002908d778SJames Bottomley 		control_phy->func_mask = FUNCTION_MASK_DEFAULT;
8012908d778SJames Bottomley 		if (phy->phy_desc->flags & ASD_SATA_SPINUP_HOLD)
8022908d778SJames Bottomley 			control_phy->func_mask &= ~SPINUP_HOLD_DIS;
8032908d778SJames Bottomley 		else
8042908d778SJames Bottomley 			control_phy->func_mask |= SPINUP_HOLD_DIS;
8052908d778SJames Bottomley 	}
8062908d778SJames Bottomley 
8072908d778SJames Bottomley 	control_phy->conn_handle = cpu_to_le16(0xFFFF);
8082908d778SJames Bottomley 
8092908d778SJames Bottomley 	ascb->tasklet_complete = control_phy_tasklet_complete;
8102908d778SJames Bottomley }
8112908d778SJames Bottomley 
8122908d778SJames Bottomley /* ---------- INITIATE LINK ADM TASK ---------- */
8132908d778SJames Bottomley 
81481e56dedSAdrian Bunk #if 0
81581e56dedSAdrian Bunk 
8162908d778SJames Bottomley static void link_adm_tasklet_complete(struct asd_ascb *ascb,
8172908d778SJames Bottomley 				      struct done_list_struct *dl)
8182908d778SJames Bottomley {
8192908d778SJames Bottomley 	u8 opcode = dl->opcode;
8202908d778SJames Bottomley 	struct initiate_link_adm *link_adm = &ascb->scb->link_adm;
8212908d778SJames Bottomley 	u8 phy_id = link_adm->phy_id;
8222908d778SJames Bottomley 
8232908d778SJames Bottomley 	if (opcode != TC_NO_ERROR) {
8242908d778SJames Bottomley 		asd_printk("phy%d: link adm task 0x%x completed with error "
8252908d778SJames Bottomley 			   "0x%x\n", phy_id, link_adm->sub_func, opcode);
8262908d778SJames Bottomley 	}
8272908d778SJames Bottomley 	ASD_DPRINTK("phy%d: link adm task 0x%x: 0x%x\n",
8282908d778SJames Bottomley 		    phy_id, link_adm->sub_func, opcode);
8292908d778SJames Bottomley 
8302908d778SJames Bottomley 	asd_ascb_free(ascb);
8312908d778SJames Bottomley }
8322908d778SJames Bottomley 
8332908d778SJames Bottomley void asd_build_initiate_link_adm_task(struct asd_ascb *ascb, int phy_id,
8342908d778SJames Bottomley 				      u8 subfunc)
8352908d778SJames Bottomley {
8362908d778SJames Bottomley 	struct scb *scb = ascb->scb;
8372908d778SJames Bottomley 	struct initiate_link_adm *link_adm = &scb->link_adm;
8382908d778SJames Bottomley 
8392908d778SJames Bottomley 	scb->header.opcode = INITIATE_LINK_ADM_TASK;
8402908d778SJames Bottomley 
8412908d778SJames Bottomley 	link_adm->phy_id = phy_id;
8422908d778SJames Bottomley 	link_adm->sub_func = subfunc;
8432908d778SJames Bottomley 	link_adm->conn_handle = cpu_to_le16(0xFFFF);
8442908d778SJames Bottomley 
8452908d778SJames Bottomley 	ascb->tasklet_complete = link_adm_tasklet_complete;
8462908d778SJames Bottomley }
8472908d778SJames Bottomley 
84881e56dedSAdrian Bunk #endif  /*  0  */
84981e56dedSAdrian Bunk 
8502908d778SJames Bottomley /* ---------- SCB timer ---------- */
8512908d778SJames Bottomley 
8522908d778SJames Bottomley /**
8532908d778SJames Bottomley  * asd_ascb_timedout -- called when a pending SCB's timer has expired
854d2e51050SLee Jones  * @t: Timer context used to fetch the SCB
8552908d778SJames Bottomley  *
8562908d778SJames Bottomley  * This is the default timeout function which does the most necessary.
8572908d778SJames Bottomley  * Upper layers can implement their own timeout function, say to free
8582908d778SJames Bottomley  * resources they have with this SCB, and then call this one at the
8592908d778SJames Bottomley  * end of their timeout function.  To do this, one should initialize
86030199beeSKees Cook  * the ascb->timer.{function, expires} prior to calling the post
8618d814961SAndrea Gelmini  * function. The timer is started by the post function.
8622908d778SJames Bottomley  */
asd_ascb_timedout(struct timer_list * t)86330199beeSKees Cook void asd_ascb_timedout(struct timer_list *t)
8642908d778SJames Bottomley {
865*41cb0855SIngo Molnar 	struct asd_ascb *ascb = timer_container_of(ascb, t, timer);
8662908d778SJames Bottomley 	struct asd_seq_data *seq = &ascb->ha->seq;
8672908d778SJames Bottomley 	unsigned long flags;
8682908d778SJames Bottomley 
8692908d778SJames Bottomley 	ASD_DPRINTK("scb:0x%x timed out\n", ascb->scb->header.opcode);
8702908d778SJames Bottomley 
8712908d778SJames Bottomley 	spin_lock_irqsave(&seq->pend_q_lock, flags);
8722908d778SJames Bottomley 	seq->pending--;
8732908d778SJames Bottomley 	list_del_init(&ascb->list);
8742908d778SJames Bottomley 	spin_unlock_irqrestore(&seq->pend_q_lock, flags);
8752908d778SJames Bottomley 
8762908d778SJames Bottomley 	asd_ascb_free(ascb);
8772908d778SJames Bottomley }
8782908d778SJames Bottomley 
8792908d778SJames Bottomley /* ---------- CONTROL PHY ---------- */
8802908d778SJames Bottomley 
8812908d778SJames Bottomley /* Given the spec value, return a driver value. */
8822908d778SJames Bottomley static const int phy_func_table[] = {
8832908d778SJames Bottomley 	[PHY_FUNC_NOP]        = PHY_NO_OP,
8842908d778SJames Bottomley 	[PHY_FUNC_LINK_RESET] = ENABLE_PHY,
8852908d778SJames Bottomley 	[PHY_FUNC_HARD_RESET] = EXECUTE_HARD_RESET,
8862908d778SJames Bottomley 	[PHY_FUNC_DISABLE]    = DISABLE_PHY,
8872908d778SJames Bottomley 	[PHY_FUNC_RELEASE_SPINUP_HOLD] = RELEASE_SPINUP_HOLD,
8882908d778SJames Bottomley };
8892908d778SJames Bottomley 
asd_control_phy(struct asd_sas_phy * phy,enum phy_func func,void * arg)890a01e70e5SJames Bottomley int asd_control_phy(struct asd_sas_phy *phy, enum phy_func func, void *arg)
8912908d778SJames Bottomley {
8922908d778SJames Bottomley 	struct asd_ha_struct *asd_ha = phy->ha->lldd_ha;
893a01e70e5SJames Bottomley 	struct asd_phy_desc *pd = asd_ha->phys[phy->id].phy_desc;
8942908d778SJames Bottomley 	struct asd_ascb *ascb;
895a01e70e5SJames Bottomley 	struct sas_phy_linkrates *rates;
8962908d778SJames Bottomley 	int res = 1;
8972908d778SJames Bottomley 
898a01e70e5SJames Bottomley 	switch (func) {
899a01e70e5SJames Bottomley 	case PHY_FUNC_CLEAR_ERROR_LOG:
900ac013ed1SDan Williams 	case PHY_FUNC_GET_EVENTS:
9012908d778SJames Bottomley 		return -ENOSYS;
902a01e70e5SJames Bottomley 	case PHY_FUNC_SET_LINK_RATE:
903a01e70e5SJames Bottomley 		rates = arg;
904a01e70e5SJames Bottomley 		if (rates->minimum_linkrate) {
905a01e70e5SJames Bottomley 			pd->min_sas_lrate = rates->minimum_linkrate;
906a01e70e5SJames Bottomley 			pd->min_sata_lrate = rates->minimum_linkrate;
907a01e70e5SJames Bottomley 		}
908a01e70e5SJames Bottomley 		if (rates->maximum_linkrate) {
909a01e70e5SJames Bottomley 			pd->max_sas_lrate = rates->maximum_linkrate;
910a01e70e5SJames Bottomley 			pd->max_sata_lrate = rates->maximum_linkrate;
911a01e70e5SJames Bottomley 		}
912a01e70e5SJames Bottomley 		func = PHY_FUNC_LINK_RESET;
913a01e70e5SJames Bottomley 		break;
914a01e70e5SJames Bottomley 	default:
915a01e70e5SJames Bottomley 		break;
916a01e70e5SJames Bottomley 	}
9172908d778SJames Bottomley 
9182908d778SJames Bottomley 	ascb = asd_ascb_alloc_list(asd_ha, &res, GFP_KERNEL);
9192908d778SJames Bottomley 	if (!ascb)
9202908d778SJames Bottomley 		return -ENOMEM;
9212908d778SJames Bottomley 
9222908d778SJames Bottomley 	asd_build_control_phy(ascb, phy->id, phy_func_table[func]);
9232908d778SJames Bottomley 	res = asd_post_ascb_list(asd_ha, ascb , 1);
9242908d778SJames Bottomley 	if (res)
9252908d778SJames Bottomley 		asd_ascb_free(ascb);
9262908d778SJames Bottomley 
9272908d778SJames Bottomley 	return res;
9282908d778SJames Bottomley }
929