xref: /linux/drivers/scsi/aic94xx/aic94xx_scb.c (revision c537b994505099b7197e7d3125b942ecbcc51eb6)
1 /*
2  * Aic94xx SAS/SATA driver SCB management.
3  *
4  * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
5  * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
6  *
7  * This file is licensed under GPLv2.
8  *
9  * This file is part of the aic94xx driver.
10  *
11  * The aic94xx driver is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; version 2 of the
14  * License.
15  *
16  * The aic94xx driver is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with the aic94xx driver; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
24  *
25  */
26 
27 #include <linux/pci.h>
28 #include <scsi/scsi_host.h>
29 
30 #include "aic94xx.h"
31 #include "aic94xx_reg.h"
32 #include "aic94xx_hwi.h"
33 #include "aic94xx_seq.h"
34 
35 #include "aic94xx_dump.h"
36 
37 /* ---------- EMPTY SCB ---------- */
38 
39 #define DL_PHY_MASK      7
40 #define BYTES_DMAED      0
41 #define PRIMITIVE_RECVD  0x08
42 #define PHY_EVENT        0x10
43 #define LINK_RESET_ERROR 0x18
44 #define TIMER_EVENT      0x20
45 #define REQ_TASK_ABORT   0xF0
46 #define REQ_DEVICE_RESET 0xF1
47 #define SIGNAL_NCQ_ERROR 0xF2
48 #define CLEAR_NCQ_ERROR  0xF3
49 
50 #define PHY_EVENTS_STATUS (CURRENT_LOSS_OF_SIGNAL | CURRENT_OOB_DONE   \
51 			   | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
52 			   | CURRENT_OOB_ERROR)
53 
54 static inline void get_lrate_mode(struct asd_phy *phy, u8 oob_mode)
55 {
56 	struct sas_phy *sas_phy = phy->sas_phy.phy;
57 
58 	switch (oob_mode & 7) {
59 	case PHY_SPEED_60:
60 		/* FIXME: sas transport class doesn't have this */
61 		phy->sas_phy.linkrate = SAS_LINK_RATE_6_0_GBPS;
62 		phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_6_0_GBPS;
63 		break;
64 	case PHY_SPEED_30:
65 		phy->sas_phy.linkrate = SAS_LINK_RATE_3_0_GBPS;
66 		phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_3_0_GBPS;
67 		break;
68 	case PHY_SPEED_15:
69 		phy->sas_phy.linkrate = SAS_LINK_RATE_1_5_GBPS;
70 		phy->sas_phy.phy->negotiated_linkrate = SAS_LINK_RATE_1_5_GBPS;
71 		break;
72 	}
73 	sas_phy->negotiated_linkrate = phy->sas_phy.linkrate;
74 	sas_phy->maximum_linkrate_hw = SAS_LINK_RATE_3_0_GBPS;
75 	sas_phy->minimum_linkrate_hw = SAS_LINK_RATE_1_5_GBPS;
76 	sas_phy->maximum_linkrate = phy->phy_desc->max_sas_lrate;
77 	sas_phy->minimum_linkrate = phy->phy_desc->min_sas_lrate;
78 
79 	if (oob_mode & SAS_MODE)
80 		phy->sas_phy.oob_mode = SAS_OOB_MODE;
81 	else if (oob_mode & SATA_MODE)
82 		phy->sas_phy.oob_mode = SATA_OOB_MODE;
83 }
84 
85 static inline void asd_phy_event_tasklet(struct asd_ascb *ascb,
86 					 struct done_list_struct *dl)
87 {
88 	struct asd_ha_struct *asd_ha = ascb->ha;
89 	struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
90 	int phy_id = dl->status_block[0] & DL_PHY_MASK;
91 	struct asd_phy *phy = &asd_ha->phys[phy_id];
92 
93 	u8 oob_status = dl->status_block[1] & PHY_EVENTS_STATUS;
94 	u8 oob_mode   = dl->status_block[2];
95 
96 	switch (oob_status) {
97 	case CURRENT_LOSS_OF_SIGNAL:
98 		/* directly attached device was removed */
99 		ASD_DPRINTK("phy%d: device unplugged\n", phy_id);
100 		asd_turn_led(asd_ha, phy_id, 0);
101 		sas_phy_disconnected(&phy->sas_phy);
102 		sas_ha->notify_phy_event(&phy->sas_phy, PHYE_LOSS_OF_SIGNAL);
103 		break;
104 	case CURRENT_OOB_DONE:
105 		/* hot plugged device */
106 		asd_turn_led(asd_ha, phy_id, 1);
107 		get_lrate_mode(phy, oob_mode);
108 		ASD_DPRINTK("phy%d device plugged: lrate:0x%x, proto:0x%x\n",
109 			    phy_id, phy->sas_phy.linkrate, phy->sas_phy.iproto);
110 		sas_ha->notify_phy_event(&phy->sas_phy, PHYE_OOB_DONE);
111 		break;
112 	case CURRENT_SPINUP_HOLD:
113 		/* hot plug SATA, no COMWAKE sent */
114 		asd_turn_led(asd_ha, phy_id, 1);
115 		sas_ha->notify_phy_event(&phy->sas_phy, PHYE_SPINUP_HOLD);
116 		break;
117 	case CURRENT_GTO_TIMEOUT:
118 	case CURRENT_OOB_ERROR:
119 		ASD_DPRINTK("phy%d error while OOB: oob status:0x%x\n", phy_id,
120 			    dl->status_block[1]);
121 		asd_turn_led(asd_ha, phy_id, 0);
122 		sas_phy_disconnected(&phy->sas_phy);
123 		sas_ha->notify_phy_event(&phy->sas_phy, PHYE_OOB_ERROR);
124 		break;
125 	}
126 }
127 
128 /* If phys are enabled sparsely, this will do the right thing. */
129 static inline unsigned ord_phy(struct asd_ha_struct *asd_ha,
130 			       struct asd_phy *phy)
131 {
132 	u8 enabled_mask = asd_ha->hw_prof.enabled_phys;
133 	int i, k = 0;
134 
135 	for_each_phy(enabled_mask, enabled_mask, i) {
136 		if (&asd_ha->phys[i] == phy)
137 			return k;
138 		k++;
139 	}
140 	return 0;
141 }
142 
143 /**
144  * asd_get_attached_sas_addr -- extract/generate attached SAS address
145  * phy: pointer to asd_phy
146  * sas_addr: pointer to buffer where the SAS address is to be written
147  *
148  * This function extracts the SAS address from an IDENTIFY frame
149  * received.  If OOB is SATA, then a SAS address is generated from the
150  * HA tables.
151  *
152  * LOCKING: the frame_rcvd_lock needs to be held since this parses the frame
153  * buffer.
154  */
155 static inline void asd_get_attached_sas_addr(struct asd_phy *phy, u8 *sas_addr)
156 {
157 	if (phy->sas_phy.frame_rcvd[0] == 0x34
158 	    && phy->sas_phy.oob_mode == SATA_OOB_MODE) {
159 		struct asd_ha_struct *asd_ha = phy->sas_phy.ha->lldd_ha;
160 		/* FIS device-to-host */
161 		u64 addr = be64_to_cpu(*(__be64 *)phy->phy_desc->sas_addr);
162 
163 		addr += asd_ha->hw_prof.sata_name_base + ord_phy(asd_ha, phy);
164 		*(__be64 *)sas_addr = cpu_to_be64(addr);
165 	} else {
166 		struct sas_identify_frame *idframe =
167 			(void *) phy->sas_phy.frame_rcvd;
168 		memcpy(sas_addr, idframe->sas_addr, SAS_ADDR_SIZE);
169 	}
170 }
171 
172 static void asd_form_port(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
173 {
174 	int i;
175 	struct asd_port *free_port = NULL;
176 	struct asd_port *port;
177 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
178 	unsigned long flags;
179 
180 	spin_lock_irqsave(&asd_ha->asd_ports_lock, flags);
181 	if (!phy->asd_port) {
182 		for (i = 0; i < ASD_MAX_PHYS; i++) {
183 			port = &asd_ha->asd_ports[i];
184 
185 			/* Check for wide port */
186 			if (port->num_phys > 0 &&
187 			    memcmp(port->sas_addr, sas_phy->sas_addr,
188 				   SAS_ADDR_SIZE) == 0 &&
189 			    memcmp(port->attached_sas_addr,
190 				   sas_phy->attached_sas_addr,
191 				   SAS_ADDR_SIZE) == 0) {
192 				break;
193 			}
194 
195 			/* Find a free port */
196 			if (port->num_phys == 0 && free_port == NULL) {
197 				free_port = port;
198 			}
199 		}
200 
201 		/* Use a free port if this doesn't form a wide port */
202 		if (i >= ASD_MAX_PHYS) {
203 			port = free_port;
204 			BUG_ON(!port);
205 			memcpy(port->sas_addr, sas_phy->sas_addr,
206 			       SAS_ADDR_SIZE);
207 			memcpy(port->attached_sas_addr,
208 			       sas_phy->attached_sas_addr,
209 			       SAS_ADDR_SIZE);
210 		}
211 		port->num_phys++;
212 		port->phy_mask |= (1U << sas_phy->id);
213 		phy->asd_port = port;
214 	}
215 	ASD_DPRINTK("%s: updating phy_mask 0x%x for phy%d\n",
216 		    __FUNCTION__, phy->asd_port->phy_mask, sas_phy->id);
217 	asd_update_port_links(asd_ha, phy);
218 	spin_unlock_irqrestore(&asd_ha->asd_ports_lock, flags);
219 }
220 
221 static void asd_deform_port(struct asd_ha_struct *asd_ha, struct asd_phy *phy)
222 {
223 	struct asd_port *port = phy->asd_port;
224 	struct asd_sas_phy *sas_phy = &phy->sas_phy;
225 	unsigned long flags;
226 
227 	spin_lock_irqsave(&asd_ha->asd_ports_lock, flags);
228 	if (port) {
229 		port->num_phys--;
230 		port->phy_mask &= ~(1U << sas_phy->id);
231 		phy->asd_port = NULL;
232 	}
233 	spin_unlock_irqrestore(&asd_ha->asd_ports_lock, flags);
234 }
235 
236 static inline void asd_bytes_dmaed_tasklet(struct asd_ascb *ascb,
237 					   struct done_list_struct *dl,
238 					   int edb_id, int phy_id)
239 {
240 	unsigned long flags;
241 	int edb_el = edb_id + ascb->edb_index;
242 	struct asd_dma_tok *edb = ascb->ha->seq.edb_arr[edb_el];
243 	struct asd_phy *phy = &ascb->ha->phys[phy_id];
244 	struct sas_ha_struct *sas_ha = phy->sas_phy.ha;
245 	u16 size = ((dl->status_block[3] & 7) << 8) | dl->status_block[2];
246 
247 	size = min(size, (u16) sizeof(phy->frame_rcvd));
248 
249 	spin_lock_irqsave(&phy->sas_phy.frame_rcvd_lock, flags);
250 	memcpy(phy->sas_phy.frame_rcvd, edb->vaddr, size);
251 	phy->sas_phy.frame_rcvd_size = size;
252 	asd_get_attached_sas_addr(phy, phy->sas_phy.attached_sas_addr);
253 	spin_unlock_irqrestore(&phy->sas_phy.frame_rcvd_lock, flags);
254 	asd_dump_frame_rcvd(phy, dl);
255 	asd_form_port(ascb->ha, phy);
256 	sas_ha->notify_port_event(&phy->sas_phy, PORTE_BYTES_DMAED);
257 }
258 
259 static inline void asd_link_reset_err_tasklet(struct asd_ascb *ascb,
260 					      struct done_list_struct *dl,
261 					      int phy_id)
262 {
263 	struct asd_ha_struct *asd_ha = ascb->ha;
264 	struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
265 	struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
266 	struct asd_phy *phy = &asd_ha->phys[phy_id];
267 	u8 lr_error = dl->status_block[1];
268 	u8 retries_left = dl->status_block[2];
269 
270 	switch (lr_error) {
271 	case 0:
272 		ASD_DPRINTK("phy%d: Receive ID timer expired\n", phy_id);
273 		break;
274 	case 1:
275 		ASD_DPRINTK("phy%d: Loss of signal\n", phy_id);
276 		break;
277 	case 2:
278 		ASD_DPRINTK("phy%d: Loss of dword sync\n", phy_id);
279 		break;
280 	case 3:
281 		ASD_DPRINTK("phy%d: Receive FIS timeout\n", phy_id);
282 		break;
283 	default:
284 		ASD_DPRINTK("phy%d: unknown link reset error code: 0x%x\n",
285 			    phy_id, lr_error);
286 		break;
287 	}
288 
289 	asd_turn_led(asd_ha, phy_id, 0);
290 	sas_phy_disconnected(sas_phy);
291 	asd_deform_port(asd_ha, phy);
292 	sas_ha->notify_port_event(sas_phy, PORTE_LINK_RESET_ERR);
293 
294 	if (retries_left == 0) {
295 		int num = 1;
296 		struct asd_ascb *cp = asd_ascb_alloc_list(ascb->ha, &num,
297 							  GFP_ATOMIC);
298 		if (!cp) {
299 			asd_printk("%s: out of memory\n", __FUNCTION__);
300 			goto out;
301 		}
302 		ASD_DPRINTK("phy%d: retries:0 performing link reset seq\n",
303 			    phy_id);
304 		asd_build_control_phy(cp, phy_id, ENABLE_PHY);
305 		if (asd_post_ascb_list(ascb->ha, cp, 1) != 0)
306 			asd_ascb_free(cp);
307 	}
308 out:
309 	;
310 }
311 
312 static inline void asd_primitive_rcvd_tasklet(struct asd_ascb *ascb,
313 					      struct done_list_struct *dl,
314 					      int phy_id)
315 {
316 	unsigned long flags;
317 	struct sas_ha_struct *sas_ha = &ascb->ha->sas_ha;
318 	struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
319 	struct asd_ha_struct *asd_ha = ascb->ha;
320 	struct asd_phy *phy = &asd_ha->phys[phy_id];
321 	u8  reg  = dl->status_block[1];
322 	u32 cont = dl->status_block[2] << ((reg & 3)*8);
323 
324 	reg &= ~3;
325 	switch (reg) {
326 	case LmPRMSTAT0BYTE0:
327 		switch (cont) {
328 		case LmBROADCH:
329 		case LmBROADRVCH0:
330 		case LmBROADRVCH1:
331 		case LmBROADSES:
332 			ASD_DPRINTK("phy%d: BROADCAST change received:%d\n",
333 				    phy_id, cont);
334 			spin_lock_irqsave(&sas_phy->sas_prim_lock, flags);
335 			sas_phy->sas_prim = ffs(cont);
336 			spin_unlock_irqrestore(&sas_phy->sas_prim_lock, flags);
337 			sas_ha->notify_port_event(sas_phy,PORTE_BROADCAST_RCVD);
338 			break;
339 
340 		case LmUNKNOWNP:
341 			ASD_DPRINTK("phy%d: unknown BREAK\n", phy_id);
342 			break;
343 
344 		default:
345 			ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
346 				    phy_id, reg, cont);
347 			break;
348 		}
349 		break;
350 	case LmPRMSTAT1BYTE0:
351 		switch (cont) {
352 		case LmHARDRST:
353 			ASD_DPRINTK("phy%d: HARD_RESET primitive rcvd\n",
354 				    phy_id);
355 			/* The sequencer disables all phys on that port.
356 			 * We have to re-enable the phys ourselves. */
357 			asd_deform_port(asd_ha, phy);
358 			sas_ha->notify_port_event(sas_phy, PORTE_HARD_RESET);
359 			break;
360 
361 		default:
362 			ASD_DPRINTK("phy%d: primitive reg:0x%x, cont:0x%04x\n",
363 				    phy_id, reg, cont);
364 			break;
365 		}
366 		break;
367 	default:
368 		ASD_DPRINTK("unknown primitive register:0x%x\n",
369 			    dl->status_block[1]);
370 		break;
371 	}
372 }
373 
374 /**
375  * asd_invalidate_edb -- invalidate an EDB and if necessary post the ESCB
376  * @ascb: pointer to Empty SCB
377  * @edb_id: index [0,6] to the empty data buffer which is to be invalidated
378  *
379  * After an EDB has been invalidated, if all EDBs in this ESCB have been
380  * invalidated, the ESCB is posted back to the sequencer.
381  * Context is tasklet/IRQ.
382  */
383 void asd_invalidate_edb(struct asd_ascb *ascb, int edb_id)
384 {
385 	struct asd_seq_data *seq = &ascb->ha->seq;
386 	struct empty_scb *escb = &ascb->scb->escb;
387 	struct sg_el     *eb   = &escb->eb[edb_id];
388 	struct asd_dma_tok *edb = seq->edb_arr[ascb->edb_index + edb_id];
389 
390 	memset(edb->vaddr, 0, ASD_EDB_SIZE);
391 	eb->flags |= ELEMENT_NOT_VALID;
392 	escb->num_valid--;
393 
394 	if (escb->num_valid == 0) {
395 		int i;
396 		/* ASD_DPRINTK("reposting escb: vaddr: 0x%p, "
397 			    "dma_handle: 0x%08llx, next: 0x%08llx, "
398 			    "index:%d, opcode:0x%02x\n",
399 			    ascb->dma_scb.vaddr,
400 			    (u64)ascb->dma_scb.dma_handle,
401 			    le64_to_cpu(ascb->scb->header.next_scb),
402 			    le16_to_cpu(ascb->scb->header.index),
403 			    ascb->scb->header.opcode);
404 		*/
405 		escb->num_valid = ASD_EDBS_PER_SCB;
406 		for (i = 0; i < ASD_EDBS_PER_SCB; i++)
407 			escb->eb[i].flags = 0;
408 		if (!list_empty(&ascb->list))
409 			list_del_init(&ascb->list);
410 		i = asd_post_escb_list(ascb->ha, ascb, 1);
411 		if (i)
412 			asd_printk("couldn't post escb, err:%d\n", i);
413 	}
414 }
415 
416 static void escb_tasklet_complete(struct asd_ascb *ascb,
417 				  struct done_list_struct *dl)
418 {
419 	struct asd_ha_struct *asd_ha = ascb->ha;
420 	struct sas_ha_struct *sas_ha = &asd_ha->sas_ha;
421 	int edb = (dl->opcode & DL_PHY_MASK) - 1; /* [0xc1,0xc7] -> [0,6] */
422 	u8  sb_opcode = dl->status_block[0];
423 	int phy_id = sb_opcode & DL_PHY_MASK;
424 	struct asd_sas_phy *sas_phy = sas_ha->sas_phy[phy_id];
425 	struct asd_phy *phy = &asd_ha->phys[phy_id];
426 
427 	if (edb > 6 || edb < 0) {
428 		ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
429 			    edb, dl->opcode);
430 		ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
431 			    sb_opcode, phy_id);
432 		ASD_DPRINTK("escb: vaddr: 0x%p, "
433 			    "dma_handle: 0x%llx, next: 0x%llx, "
434 			    "index:%d, opcode:0x%02x\n",
435 			    ascb->dma_scb.vaddr,
436 			    (unsigned long long)ascb->dma_scb.dma_handle,
437 			    (unsigned long long)
438 			    le64_to_cpu(ascb->scb->header.next_scb),
439 			    le16_to_cpu(ascb->scb->header.index),
440 			    ascb->scb->header.opcode);
441 	}
442 
443 	/* Catch these before we mask off the sb_opcode bits */
444 	switch (sb_opcode) {
445 	case REQ_TASK_ABORT: {
446 		struct asd_ascb *a, *b;
447 		u16 tc_abort;
448 		struct domain_device *failed_dev = NULL;
449 
450 		ASD_DPRINTK("%s: REQ_TASK_ABORT, reason=0x%X\n",
451 			    __FUNCTION__, dl->status_block[3]);
452 
453 		/*
454 		 * Find the task that caused the abort and abort it first.
455 		 * The sequencer won't put anything on the done list until
456 		 * that happens.
457 		 */
458 		tc_abort = *((u16*)(&dl->status_block[1]));
459 		tc_abort = le16_to_cpu(tc_abort);
460 
461 		list_for_each_entry_safe(a, b, &asd_ha->seq.pend_q, list) {
462 			struct sas_task *task = ascb->uldd_task;
463 
464 			if (task && a->tc_index == tc_abort) {
465 				failed_dev = task->dev;
466 				sas_task_abort(task);
467 				break;
468 			}
469 		}
470 
471 		if (!failed_dev) {
472 			ASD_DPRINTK("%s: Can't find task (tc=%d) to abort!\n",
473 				    __FUNCTION__, tc_abort);
474 			goto out;
475 		}
476 
477 		/*
478 		 * Now abort everything else for that device (hba?) so
479 		 * that the EH will wake up and do something.
480 		 */
481 		list_for_each_entry_safe(a, b, &asd_ha->seq.pend_q, list) {
482 			struct sas_task *task = ascb->uldd_task;
483 
484 			if (task &&
485 			    task->dev == failed_dev &&
486 			    a->tc_index != tc_abort)
487 				sas_task_abort(task);
488 		}
489 
490 		goto out;
491 	}
492 	case REQ_DEVICE_RESET: {
493 		struct asd_ascb *a;
494 		u16 conn_handle;
495 		unsigned long flags;
496 		struct sas_task *last_dev_task = NULL;
497 
498 		conn_handle = *((u16*)(&dl->status_block[1]));
499 		conn_handle = le16_to_cpu(conn_handle);
500 
501 		ASD_DPRINTK("%s: REQ_DEVICE_RESET, reason=0x%X\n", __FUNCTION__,
502 			    dl->status_block[3]);
503 
504 		/* Find the last pending task for the device... */
505 		list_for_each_entry(a, &asd_ha->seq.pend_q, list) {
506 			u16 x;
507 			struct domain_device *dev;
508 			struct sas_task *task = a->uldd_task;
509 
510 			if (!task)
511 				continue;
512 			dev = task->dev;
513 
514 			x = (unsigned long)dev->lldd_dev;
515 			if (x == conn_handle)
516 				last_dev_task = task;
517 		}
518 
519 		if (!last_dev_task) {
520 			ASD_DPRINTK("%s: Device reset for idle device %d?\n",
521 				    __FUNCTION__, conn_handle);
522 			goto out;
523 		}
524 
525 		/* ...and set the reset flag */
526 		spin_lock_irqsave(&last_dev_task->task_state_lock, flags);
527 		last_dev_task->task_state_flags |= SAS_TASK_NEED_DEV_RESET;
528 		spin_unlock_irqrestore(&last_dev_task->task_state_lock, flags);
529 
530 		/* Kill all pending tasks for the device */
531 		list_for_each_entry(a, &asd_ha->seq.pend_q, list) {
532 			u16 x;
533 			struct domain_device *dev;
534 			struct sas_task *task = a->uldd_task;
535 
536 			if (!task)
537 				continue;
538 			dev = task->dev;
539 
540 			x = (unsigned long)dev->lldd_dev;
541 			if (x == conn_handle)
542 				sas_task_abort(task);
543 		}
544 
545 		goto out;
546 	}
547 	case SIGNAL_NCQ_ERROR:
548 		ASD_DPRINTK("%s: SIGNAL_NCQ_ERROR\n", __FUNCTION__);
549 		goto out;
550 	case CLEAR_NCQ_ERROR:
551 		ASD_DPRINTK("%s: CLEAR_NCQ_ERROR\n", __FUNCTION__);
552 		goto out;
553 	}
554 
555 	sb_opcode &= ~DL_PHY_MASK;
556 
557 	switch (sb_opcode) {
558 	case BYTES_DMAED:
559 		ASD_DPRINTK("%s: phy%d: BYTES_DMAED\n", __FUNCTION__, phy_id);
560 		asd_bytes_dmaed_tasklet(ascb, dl, edb, phy_id);
561 		break;
562 	case PRIMITIVE_RECVD:
563 		ASD_DPRINTK("%s: phy%d: PRIMITIVE_RECVD\n", __FUNCTION__,
564 			    phy_id);
565 		asd_primitive_rcvd_tasklet(ascb, dl, phy_id);
566 		break;
567 	case PHY_EVENT:
568 		ASD_DPRINTK("%s: phy%d: PHY_EVENT\n", __FUNCTION__, phy_id);
569 		asd_phy_event_tasklet(ascb, dl);
570 		break;
571 	case LINK_RESET_ERROR:
572 		ASD_DPRINTK("%s: phy%d: LINK_RESET_ERROR\n", __FUNCTION__,
573 			    phy_id);
574 		asd_link_reset_err_tasklet(ascb, dl, phy_id);
575 		break;
576 	case TIMER_EVENT:
577 		ASD_DPRINTK("%s: phy%d: TIMER_EVENT, lost dw sync\n",
578 			    __FUNCTION__, phy_id);
579 		asd_turn_led(asd_ha, phy_id, 0);
580 		/* the device is gone */
581 		sas_phy_disconnected(sas_phy);
582 		asd_deform_port(asd_ha, phy);
583 		sas_ha->notify_port_event(sas_phy, PORTE_TIMER_EVENT);
584 		break;
585 	default:
586 		ASD_DPRINTK("%s: phy%d: unknown event:0x%x\n", __FUNCTION__,
587 			    phy_id, sb_opcode);
588 		ASD_DPRINTK("edb is 0x%x! dl->opcode is 0x%x\n",
589 			    edb, dl->opcode);
590 		ASD_DPRINTK("sb_opcode : 0x%x, phy_id: 0x%x\n",
591 			    sb_opcode, phy_id);
592 		ASD_DPRINTK("escb: vaddr: 0x%p, "
593 			    "dma_handle: 0x%llx, next: 0x%llx, "
594 			    "index:%d, opcode:0x%02x\n",
595 			    ascb->dma_scb.vaddr,
596 			    (unsigned long long)ascb->dma_scb.dma_handle,
597 			    (unsigned long long)
598 			    le64_to_cpu(ascb->scb->header.next_scb),
599 			    le16_to_cpu(ascb->scb->header.index),
600 			    ascb->scb->header.opcode);
601 
602 		break;
603 	}
604 out:
605 	asd_invalidate_edb(ascb, edb);
606 }
607 
608 int asd_init_post_escbs(struct asd_ha_struct *asd_ha)
609 {
610 	struct asd_seq_data *seq = &asd_ha->seq;
611 	int i;
612 
613 	for (i = 0; i < seq->num_escbs; i++)
614 		seq->escb_arr[i]->tasklet_complete = escb_tasklet_complete;
615 
616 	ASD_DPRINTK("posting %d escbs\n", i);
617 	return asd_post_escb_list(asd_ha, seq->escb_arr[0], seq->num_escbs);
618 }
619 
620 /* ---------- CONTROL PHY ---------- */
621 
622 #define CONTROL_PHY_STATUS (CURRENT_DEVICE_PRESENT | CURRENT_OOB_DONE   \
623 			    | CURRENT_SPINUP_HOLD | CURRENT_GTO_TIMEOUT \
624 			    | CURRENT_OOB_ERROR)
625 
626 /**
627  * control_phy_tasklet_complete -- tasklet complete for CONTROL PHY ascb
628  * @ascb: pointer to an ascb
629  * @dl: pointer to the done list entry
630  *
631  * This function completes a CONTROL PHY scb and frees the ascb.
632  * A note on LEDs:
633  *  - an LED blinks if there is IO though it,
634  *  - if a device is connected to the LED, it is lit,
635  *  - if no device is connected to the LED, is is dimmed (off).
636  */
637 static void control_phy_tasklet_complete(struct asd_ascb *ascb,
638 					 struct done_list_struct *dl)
639 {
640 	struct asd_ha_struct *asd_ha = ascb->ha;
641 	struct scb *scb = ascb->scb;
642 	struct control_phy *control_phy = &scb->control_phy;
643 	u8 phy_id = control_phy->phy_id;
644 	struct asd_phy *phy = &ascb->ha->phys[phy_id];
645 
646 	u8 status     = dl->status_block[0];
647 	u8 oob_status = dl->status_block[1];
648 	u8 oob_mode   = dl->status_block[2];
649 	/* u8 oob_signals= dl->status_block[3]; */
650 
651 	if (status != 0) {
652 		ASD_DPRINTK("%s: phy%d status block opcode:0x%x\n",
653 			    __FUNCTION__, phy_id, status);
654 		goto out;
655 	}
656 
657 	switch (control_phy->sub_func) {
658 	case DISABLE_PHY:
659 		asd_ha->hw_prof.enabled_phys &= ~(1 << phy_id);
660 		asd_turn_led(asd_ha, phy_id, 0);
661 		asd_control_led(asd_ha, phy_id, 0);
662 		ASD_DPRINTK("%s: disable phy%d\n", __FUNCTION__, phy_id);
663 		break;
664 
665 	case ENABLE_PHY:
666 		asd_control_led(asd_ha, phy_id, 1);
667 		if (oob_status & CURRENT_OOB_DONE) {
668 			asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
669 			get_lrate_mode(phy, oob_mode);
670 			asd_turn_led(asd_ha, phy_id, 1);
671 			ASD_DPRINTK("%s: phy%d, lrate:0x%x, proto:0x%x\n",
672 				    __FUNCTION__, phy_id,phy->sas_phy.linkrate,
673 				    phy->sas_phy.iproto);
674 		} else if (oob_status & CURRENT_SPINUP_HOLD) {
675 			asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
676 			asd_turn_led(asd_ha, phy_id, 1);
677 			ASD_DPRINTK("%s: phy%d, spinup hold\n", __FUNCTION__,
678 				    phy_id);
679 		} else if (oob_status & CURRENT_ERR_MASK) {
680 			asd_turn_led(asd_ha, phy_id, 0);
681 			ASD_DPRINTK("%s: phy%d: error: oob status:0x%02x\n",
682 				    __FUNCTION__, phy_id, oob_status);
683 		} else if (oob_status & (CURRENT_HOT_PLUG_CNCT
684 					 | CURRENT_DEVICE_PRESENT))  {
685 			asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
686 			asd_turn_led(asd_ha, phy_id, 1);
687 			ASD_DPRINTK("%s: phy%d: hot plug or device present\n",
688 				    __FUNCTION__, phy_id);
689 		} else {
690 			asd_ha->hw_prof.enabled_phys |= (1 << phy_id);
691 			asd_turn_led(asd_ha, phy_id, 0);
692 			ASD_DPRINTK("%s: phy%d: no device present: "
693 				    "oob_status:0x%x\n",
694 				    __FUNCTION__, phy_id, oob_status);
695 		}
696 		break;
697 	case RELEASE_SPINUP_HOLD:
698 	case PHY_NO_OP:
699 	case EXECUTE_HARD_RESET:
700 		ASD_DPRINTK("%s: phy%d: sub_func:0x%x\n", __FUNCTION__,
701 			    phy_id, control_phy->sub_func);
702 		/* XXX finish */
703 		break;
704 	default:
705 		ASD_DPRINTK("%s: phy%d: sub_func:0x%x?\n", __FUNCTION__,
706 			    phy_id, control_phy->sub_func);
707 		break;
708 	}
709 out:
710 	asd_ascb_free(ascb);
711 }
712 
713 static inline void set_speed_mask(u8 *speed_mask, struct asd_phy_desc *pd)
714 {
715 	/* disable all speeds, then enable defaults */
716 	*speed_mask = SAS_SPEED_60_DIS | SAS_SPEED_30_DIS | SAS_SPEED_15_DIS
717 		| SATA_SPEED_30_DIS | SATA_SPEED_15_DIS;
718 
719 	switch (pd->max_sas_lrate) {
720 	case SAS_LINK_RATE_6_0_GBPS:
721 		*speed_mask &= ~SAS_SPEED_60_DIS;
722 	default:
723 	case SAS_LINK_RATE_3_0_GBPS:
724 		*speed_mask &= ~SAS_SPEED_30_DIS;
725 	case SAS_LINK_RATE_1_5_GBPS:
726 		*speed_mask &= ~SAS_SPEED_15_DIS;
727 	}
728 
729 	switch (pd->min_sas_lrate) {
730 	case SAS_LINK_RATE_6_0_GBPS:
731 		*speed_mask |= SAS_SPEED_30_DIS;
732 	case SAS_LINK_RATE_3_0_GBPS:
733 		*speed_mask |= SAS_SPEED_15_DIS;
734 	default:
735 	case SAS_LINK_RATE_1_5_GBPS:
736 		/* nothing to do */
737 		;
738 	}
739 
740 	switch (pd->max_sata_lrate) {
741 	case SAS_LINK_RATE_3_0_GBPS:
742 		*speed_mask &= ~SATA_SPEED_30_DIS;
743 	default:
744 	case SAS_LINK_RATE_1_5_GBPS:
745 		*speed_mask &= ~SATA_SPEED_15_DIS;
746 	}
747 
748 	switch (pd->min_sata_lrate) {
749 	case SAS_LINK_RATE_3_0_GBPS:
750 		*speed_mask |= SATA_SPEED_15_DIS;
751 	default:
752 	case SAS_LINK_RATE_1_5_GBPS:
753 		/* nothing to do */
754 		;
755 	}
756 }
757 
758 /**
759  * asd_build_control_phy -- build a CONTROL PHY SCB
760  * @ascb: pointer to an ascb
761  * @phy_id: phy id to control, integer
762  * @subfunc: subfunction, what to actually to do the phy
763  *
764  * This function builds a CONTROL PHY scb.  No allocation of any kind
765  * is performed. @ascb is allocated with the list function.
766  * The caller can override the ascb->tasklet_complete to point
767  * to its own callback function.  It must call asd_ascb_free()
768  * at its tasklet complete function.
769  * See the default implementation.
770  */
771 void asd_build_control_phy(struct asd_ascb *ascb, int phy_id, u8 subfunc)
772 {
773 	struct asd_phy *phy = &ascb->ha->phys[phy_id];
774 	struct scb *scb = ascb->scb;
775 	struct control_phy *control_phy = &scb->control_phy;
776 
777 	scb->header.opcode = CONTROL_PHY;
778 	control_phy->phy_id = (u8) phy_id;
779 	control_phy->sub_func = subfunc;
780 
781 	switch (subfunc) {
782 	case EXECUTE_HARD_RESET:  /* 0x81 */
783 	case ENABLE_PHY:          /* 0x01 */
784 		/* decide hot plug delay */
785 		control_phy->hot_plug_delay = HOTPLUG_DELAY_TIMEOUT;
786 
787 		/* decide speed mask */
788 		set_speed_mask(&control_phy->speed_mask, phy->phy_desc);
789 
790 		/* initiator port settings are in the hi nibble */
791 		if (phy->sas_phy.role == PHY_ROLE_INITIATOR)
792 			control_phy->port_type = SAS_PROTO_ALL << 4;
793 		else if (phy->sas_phy.role == PHY_ROLE_TARGET)
794 			control_phy->port_type = SAS_PROTO_ALL;
795 		else
796 			control_phy->port_type =
797 				(SAS_PROTO_ALL << 4) | SAS_PROTO_ALL;
798 
799 		/* link reset retries, this should be nominal */
800 		control_phy->link_reset_retries = 10;
801 
802 	case RELEASE_SPINUP_HOLD: /* 0x02 */
803 		/* decide the func_mask */
804 		control_phy->func_mask = FUNCTION_MASK_DEFAULT;
805 		if (phy->phy_desc->flags & ASD_SATA_SPINUP_HOLD)
806 			control_phy->func_mask &= ~SPINUP_HOLD_DIS;
807 		else
808 			control_phy->func_mask |= SPINUP_HOLD_DIS;
809 	}
810 
811 	control_phy->conn_handle = cpu_to_le16(0xFFFF);
812 
813 	ascb->tasklet_complete = control_phy_tasklet_complete;
814 }
815 
816 /* ---------- INITIATE LINK ADM TASK ---------- */
817 
818 static void link_adm_tasklet_complete(struct asd_ascb *ascb,
819 				      struct done_list_struct *dl)
820 {
821 	u8 opcode = dl->opcode;
822 	struct initiate_link_adm *link_adm = &ascb->scb->link_adm;
823 	u8 phy_id = link_adm->phy_id;
824 
825 	if (opcode != TC_NO_ERROR) {
826 		asd_printk("phy%d: link adm task 0x%x completed with error "
827 			   "0x%x\n", phy_id, link_adm->sub_func, opcode);
828 	}
829 	ASD_DPRINTK("phy%d: link adm task 0x%x: 0x%x\n",
830 		    phy_id, link_adm->sub_func, opcode);
831 
832 	asd_ascb_free(ascb);
833 }
834 
835 void asd_build_initiate_link_adm_task(struct asd_ascb *ascb, int phy_id,
836 				      u8 subfunc)
837 {
838 	struct scb *scb = ascb->scb;
839 	struct initiate_link_adm *link_adm = &scb->link_adm;
840 
841 	scb->header.opcode = INITIATE_LINK_ADM_TASK;
842 
843 	link_adm->phy_id = phy_id;
844 	link_adm->sub_func = subfunc;
845 	link_adm->conn_handle = cpu_to_le16(0xFFFF);
846 
847 	ascb->tasklet_complete = link_adm_tasklet_complete;
848 }
849 
850 /* ---------- SCB timer ---------- */
851 
852 /**
853  * asd_ascb_timedout -- called when a pending SCB's timer has expired
854  * @data: unsigned long, a pointer to the ascb in question
855  *
856  * This is the default timeout function which does the most necessary.
857  * Upper layers can implement their own timeout function, say to free
858  * resources they have with this SCB, and then call this one at the
859  * end of their timeout function.  To do this, one should initialize
860  * the ascb->timer.{function, data, expires} prior to calling the post
861  * funcion.  The timer is started by the post function.
862  */
863 void asd_ascb_timedout(unsigned long data)
864 {
865 	struct asd_ascb *ascb = (void *) data;
866 	struct asd_seq_data *seq = &ascb->ha->seq;
867 	unsigned long flags;
868 
869 	ASD_DPRINTK("scb:0x%x timed out\n", ascb->scb->header.opcode);
870 
871 	spin_lock_irqsave(&seq->pend_q_lock, flags);
872 	seq->pending--;
873 	list_del_init(&ascb->list);
874 	spin_unlock_irqrestore(&seq->pend_q_lock, flags);
875 
876 	asd_ascb_free(ascb);
877 }
878 
879 /* ---------- CONTROL PHY ---------- */
880 
881 /* Given the spec value, return a driver value. */
882 static const int phy_func_table[] = {
883 	[PHY_FUNC_NOP]        = PHY_NO_OP,
884 	[PHY_FUNC_LINK_RESET] = ENABLE_PHY,
885 	[PHY_FUNC_HARD_RESET] = EXECUTE_HARD_RESET,
886 	[PHY_FUNC_DISABLE]    = DISABLE_PHY,
887 	[PHY_FUNC_RELEASE_SPINUP_HOLD] = RELEASE_SPINUP_HOLD,
888 };
889 
890 int asd_control_phy(struct asd_sas_phy *phy, enum phy_func func, void *arg)
891 {
892 	struct asd_ha_struct *asd_ha = phy->ha->lldd_ha;
893 	struct asd_phy_desc *pd = asd_ha->phys[phy->id].phy_desc;
894 	struct asd_ascb *ascb;
895 	struct sas_phy_linkrates *rates;
896 	int res = 1;
897 
898 	switch (func) {
899 	case PHY_FUNC_CLEAR_ERROR_LOG:
900 		return -ENOSYS;
901 	case PHY_FUNC_SET_LINK_RATE:
902 		rates = arg;
903 		if (rates->minimum_linkrate) {
904 			pd->min_sas_lrate = rates->minimum_linkrate;
905 			pd->min_sata_lrate = rates->minimum_linkrate;
906 		}
907 		if (rates->maximum_linkrate) {
908 			pd->max_sas_lrate = rates->maximum_linkrate;
909 			pd->max_sata_lrate = rates->maximum_linkrate;
910 		}
911 		func = PHY_FUNC_LINK_RESET;
912 		break;
913 	default:
914 		break;
915 	}
916 
917 	ascb = asd_ascb_alloc_list(asd_ha, &res, GFP_KERNEL);
918 	if (!ascb)
919 		return -ENOMEM;
920 
921 	asd_build_control_phy(ascb, phy->id, phy_func_table[func]);
922 	res = asd_post_ascb_list(asd_ha, ascb , 1);
923 	if (res)
924 		asd_ascb_free(ascb);
925 
926 	return res;
927 }
928