1 /*
2  * Serial Attached SCSI (SAS) class SCSI Host glue.
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 program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but
15  * WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22  * USA
23  *
24  */
25 
26 #include <linux/kthread.h>
27 #include <linux/firmware.h>
28 #include <linux/export.h>
29 #include <linux/ctype.h>
30 
31 #include "sas_internal.h"
32 
33 #include <scsi/scsi_host.h>
34 #include <scsi/scsi_device.h>
35 #include <scsi/scsi_tcq.h>
36 #include <scsi/scsi.h>
37 #include <scsi/scsi_eh.h>
38 #include <scsi/scsi_transport.h>
39 #include <scsi/scsi_transport_sas.h>
40 #include <scsi/sas_ata.h>
41 #include "../scsi_sas_internal.h"
42 #include "../scsi_transport_api.h"
43 #include "../scsi_priv.h"
44 
45 #include <linux/err.h>
46 #include <linux/blkdev.h>
47 #include <linux/freezer.h>
48 #include <linux/gfp.h>
49 #include <linux/scatterlist.h>
50 #include <linux/libata.h>
51 
52 /* record final status and free the task */
53 static void sas_end_task(struct scsi_cmnd *sc, struct sas_task *task)
54 {
55 	struct task_status_struct *ts = &task->task_status;
56 	int hs = 0, stat = 0;
57 
58 	if (ts->resp == SAS_TASK_UNDELIVERED) {
59 		/* transport error */
60 		hs = DID_NO_CONNECT;
61 	} else { /* ts->resp == SAS_TASK_COMPLETE */
62 		/* task delivered, what happened afterwards? */
63 		switch (ts->stat) {
64 		case SAS_DEV_NO_RESPONSE:
65 		case SAS_INTERRUPTED:
66 		case SAS_PHY_DOWN:
67 		case SAS_NAK_R_ERR:
68 		case SAS_OPEN_TO:
69 			hs = DID_NO_CONNECT;
70 			break;
71 		case SAS_DATA_UNDERRUN:
72 			scsi_set_resid(sc, ts->residual);
73 			if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
74 				hs = DID_ERROR;
75 			break;
76 		case SAS_DATA_OVERRUN:
77 			hs = DID_ERROR;
78 			break;
79 		case SAS_QUEUE_FULL:
80 			hs = DID_SOFT_ERROR; /* retry */
81 			break;
82 		case SAS_DEVICE_UNKNOWN:
83 			hs = DID_BAD_TARGET;
84 			break;
85 		case SAS_SG_ERR:
86 			hs = DID_PARITY;
87 			break;
88 		case SAS_OPEN_REJECT:
89 			if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
90 				hs = DID_SOFT_ERROR; /* retry */
91 			else
92 				hs = DID_ERROR;
93 			break;
94 		case SAS_PROTO_RESPONSE:
95 			SAS_DPRINTK("LLDD:%s sent SAS_PROTO_RESP for an SSP "
96 				    "task; please report this\n",
97 				    task->dev->port->ha->sas_ha_name);
98 			break;
99 		case SAS_ABORTED_TASK:
100 			hs = DID_ABORT;
101 			break;
102 		case SAM_STAT_CHECK_CONDITION:
103 			memcpy(sc->sense_buffer, ts->buf,
104 			       min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
105 			stat = SAM_STAT_CHECK_CONDITION;
106 			break;
107 		default:
108 			stat = ts->stat;
109 			break;
110 		}
111 	}
112 
113 	sc->result = (hs << 16) | stat;
114 	ASSIGN_SAS_TASK(sc, NULL);
115 	sas_free_task(task);
116 }
117 
118 static void sas_scsi_task_done(struct sas_task *task)
119 {
120 	struct scsi_cmnd *sc = task->uldd_task;
121 	struct domain_device *dev = task->dev;
122 	struct sas_ha_struct *ha = dev->port->ha;
123 	unsigned long flags;
124 
125 	spin_lock_irqsave(&dev->done_lock, flags);
126 	if (test_bit(SAS_HA_FROZEN, &ha->state))
127 		task = NULL;
128 	else
129 		ASSIGN_SAS_TASK(sc, NULL);
130 	spin_unlock_irqrestore(&dev->done_lock, flags);
131 
132 	if (unlikely(!task)) {
133 		/* task will be completed by the error handler */
134 		SAS_DPRINTK("task done but aborted\n");
135 		return;
136 	}
137 
138 	if (unlikely(!sc)) {
139 		SAS_DPRINTK("task_done called with non existing SCSI cmnd!\n");
140 		sas_free_task(task);
141 		return;
142 	}
143 
144 	sas_end_task(sc, task);
145 	sc->scsi_done(sc);
146 }
147 
148 static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
149 					       struct domain_device *dev,
150 					       gfp_t gfp_flags)
151 {
152 	struct sas_task *task = sas_alloc_task(gfp_flags);
153 	struct scsi_lun lun;
154 
155 	if (!task)
156 		return NULL;
157 
158 	task->uldd_task = cmd;
159 	ASSIGN_SAS_TASK(cmd, task);
160 
161 	task->dev = dev;
162 	task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
163 
164 	task->ssp_task.retry_count = 1;
165 	int_to_scsilun(cmd->device->lun, &lun);
166 	memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
167 	task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
168 	task->ssp_task.cmd = cmd;
169 
170 	task->scatter = scsi_sglist(cmd);
171 	task->num_scatter = scsi_sg_count(cmd);
172 	task->total_xfer_len = scsi_bufflen(cmd);
173 	task->data_dir = cmd->sc_data_direction;
174 
175 	task->task_done = sas_scsi_task_done;
176 
177 	return task;
178 }
179 
180 int sas_queuecommand(struct Scsi_Host *host, struct scsi_cmnd *cmd)
181 {
182 	struct sas_internal *i = to_sas_internal(host->transportt);
183 	struct domain_device *dev = cmd_to_domain_dev(cmd);
184 	struct sas_task *task;
185 	int res = 0;
186 
187 	/* If the device fell off, no sense in issuing commands */
188 	if (test_bit(SAS_DEV_GONE, &dev->state)) {
189 		cmd->result = DID_BAD_TARGET << 16;
190 		goto out_done;
191 	}
192 
193 	if (dev_is_sata(dev)) {
194 		spin_lock_irq(dev->sata_dev.ap->lock);
195 		res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
196 		spin_unlock_irq(dev->sata_dev.ap->lock);
197 		return res;
198 	}
199 
200 	task = sas_create_task(cmd, dev, GFP_ATOMIC);
201 	if (!task)
202 		return SCSI_MLQUEUE_HOST_BUSY;
203 
204 	res = i->dft->lldd_execute_task(task, GFP_ATOMIC);
205 	if (res)
206 		goto out_free_task;
207 	return 0;
208 
209 out_free_task:
210 	SAS_DPRINTK("lldd_execute_task returned: %d\n", res);
211 	ASSIGN_SAS_TASK(cmd, NULL);
212 	sas_free_task(task);
213 	if (res == -SAS_QUEUE_FULL)
214 		cmd->result = DID_SOFT_ERROR << 16; /* retry */
215 	else
216 		cmd->result = DID_ERROR << 16;
217 out_done:
218 	cmd->scsi_done(cmd);
219 	return 0;
220 }
221 
222 static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
223 {
224 	struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
225 	struct sas_task *task = TO_SAS_TASK(cmd);
226 
227 	/* At this point, we only get called following an actual abort
228 	 * of the task, so we should be guaranteed not to be racing with
229 	 * any completions from the LLD.  Task is freed after this.
230 	 */
231 	sas_end_task(cmd, task);
232 
233 	/* now finish the command and move it on to the error
234 	 * handler done list, this also takes it off the
235 	 * error handler pending list.
236 	 */
237 	scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
238 }
239 
240 static void sas_eh_defer_cmd(struct scsi_cmnd *cmd)
241 {
242 	struct domain_device *dev = cmd_to_domain_dev(cmd);
243 	struct sas_ha_struct *ha = dev->port->ha;
244 	struct sas_task *task = TO_SAS_TASK(cmd);
245 
246 	if (!dev_is_sata(dev)) {
247 		sas_eh_finish_cmd(cmd);
248 		return;
249 	}
250 
251 	/* report the timeout to libata */
252 	sas_end_task(cmd, task);
253 	list_move_tail(&cmd->eh_entry, &ha->eh_ata_q);
254 }
255 
256 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
257 {
258 	struct scsi_cmnd *cmd, *n;
259 
260 	list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
261 		if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
262 		    cmd->device->lun == my_cmd->device->lun)
263 			sas_eh_defer_cmd(cmd);
264 	}
265 }
266 
267 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
268 				     struct domain_device *dev)
269 {
270 	struct scsi_cmnd *cmd, *n;
271 
272 	list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
273 		struct domain_device *x = cmd_to_domain_dev(cmd);
274 
275 		if (x == dev)
276 			sas_eh_finish_cmd(cmd);
277 	}
278 }
279 
280 static void sas_scsi_clear_queue_port(struct list_head *error_q,
281 				      struct asd_sas_port *port)
282 {
283 	struct scsi_cmnd *cmd, *n;
284 
285 	list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
286 		struct domain_device *dev = cmd_to_domain_dev(cmd);
287 		struct asd_sas_port *x = dev->port;
288 
289 		if (x == port)
290 			sas_eh_finish_cmd(cmd);
291 	}
292 }
293 
294 enum task_disposition {
295 	TASK_IS_DONE,
296 	TASK_IS_ABORTED,
297 	TASK_IS_AT_LU,
298 	TASK_IS_NOT_AT_LU,
299 	TASK_ABORT_FAILED,
300 };
301 
302 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
303 {
304 	unsigned long flags;
305 	int i, res;
306 	struct sas_internal *si =
307 		to_sas_internal(task->dev->port->ha->core.shost->transportt);
308 
309 	for (i = 0; i < 5; i++) {
310 		SAS_DPRINTK("%s: aborting task 0x%p\n", __func__, task);
311 		res = si->dft->lldd_abort_task(task);
312 
313 		spin_lock_irqsave(&task->task_state_lock, flags);
314 		if (task->task_state_flags & SAS_TASK_STATE_DONE) {
315 			spin_unlock_irqrestore(&task->task_state_lock, flags);
316 			SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
317 				    task);
318 			return TASK_IS_DONE;
319 		}
320 		spin_unlock_irqrestore(&task->task_state_lock, flags);
321 
322 		if (res == TMF_RESP_FUNC_COMPLETE) {
323 			SAS_DPRINTK("%s: task 0x%p is aborted\n",
324 				    __func__, task);
325 			return TASK_IS_ABORTED;
326 		} else if (si->dft->lldd_query_task) {
327 			SAS_DPRINTK("%s: querying task 0x%p\n",
328 				    __func__, task);
329 			res = si->dft->lldd_query_task(task);
330 			switch (res) {
331 			case TMF_RESP_FUNC_SUCC:
332 				SAS_DPRINTK("%s: task 0x%p at LU\n",
333 					    __func__, task);
334 				return TASK_IS_AT_LU;
335 			case TMF_RESP_FUNC_COMPLETE:
336 				SAS_DPRINTK("%s: task 0x%p not at LU\n",
337 					    __func__, task);
338 				return TASK_IS_NOT_AT_LU;
339 			case TMF_RESP_FUNC_FAILED:
340                                 SAS_DPRINTK("%s: task 0x%p failed to abort\n",
341                                                 __func__, task);
342                                 return TASK_ABORT_FAILED;
343                         }
344 
345 		}
346 	}
347 	return res;
348 }
349 
350 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
351 {
352 	int res = TMF_RESP_FUNC_FAILED;
353 	struct scsi_lun lun;
354 	struct sas_internal *i =
355 		to_sas_internal(dev->port->ha->core.shost->transportt);
356 
357 	int_to_scsilun(cmd->device->lun, &lun);
358 
359 	SAS_DPRINTK("eh: device %llx LUN %llx has the task\n",
360 		    SAS_ADDR(dev->sas_addr),
361 		    cmd->device->lun);
362 
363 	if (i->dft->lldd_abort_task_set)
364 		res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
365 
366 	if (res == TMF_RESP_FUNC_FAILED) {
367 		if (i->dft->lldd_clear_task_set)
368 			res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
369 	}
370 
371 	if (res == TMF_RESP_FUNC_FAILED) {
372 		if (i->dft->lldd_lu_reset)
373 			res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
374 	}
375 
376 	return res;
377 }
378 
379 static int sas_recover_I_T(struct domain_device *dev)
380 {
381 	int res = TMF_RESP_FUNC_FAILED;
382 	struct sas_internal *i =
383 		to_sas_internal(dev->port->ha->core.shost->transportt);
384 
385 	SAS_DPRINTK("I_T nexus reset for dev %016llx\n",
386 		    SAS_ADDR(dev->sas_addr));
387 
388 	if (i->dft->lldd_I_T_nexus_reset)
389 		res = i->dft->lldd_I_T_nexus_reset(dev);
390 
391 	return res;
392 }
393 
394 /* take a reference on the last known good phy for this device */
395 struct sas_phy *sas_get_local_phy(struct domain_device *dev)
396 {
397 	struct sas_ha_struct *ha = dev->port->ha;
398 	struct sas_phy *phy;
399 	unsigned long flags;
400 
401 	/* a published domain device always has a valid phy, it may be
402 	 * stale, but it is never NULL
403 	 */
404 	BUG_ON(!dev->phy);
405 
406 	spin_lock_irqsave(&ha->phy_port_lock, flags);
407 	phy = dev->phy;
408 	get_device(&phy->dev);
409 	spin_unlock_irqrestore(&ha->phy_port_lock, flags);
410 
411 	return phy;
412 }
413 EXPORT_SYMBOL_GPL(sas_get_local_phy);
414 
415 static void sas_wait_eh(struct domain_device *dev)
416 {
417 	struct sas_ha_struct *ha = dev->port->ha;
418 	DEFINE_WAIT(wait);
419 
420 	if (dev_is_sata(dev)) {
421 		ata_port_wait_eh(dev->sata_dev.ap);
422 		return;
423 	}
424  retry:
425 	spin_lock_irq(&ha->lock);
426 
427 	while (test_bit(SAS_DEV_EH_PENDING, &dev->state)) {
428 		prepare_to_wait(&ha->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
429 		spin_unlock_irq(&ha->lock);
430 		schedule();
431 		spin_lock_irq(&ha->lock);
432 	}
433 	finish_wait(&ha->eh_wait_q, &wait);
434 
435 	spin_unlock_irq(&ha->lock);
436 
437 	/* make sure SCSI EH is complete */
438 	if (scsi_host_in_recovery(ha->core.shost)) {
439 		msleep(10);
440 		goto retry;
441 	}
442 }
443 EXPORT_SYMBOL(sas_wait_eh);
444 
445 static int sas_queue_reset(struct domain_device *dev, int reset_type,
446 			   u64 lun, int wait)
447 {
448 	struct sas_ha_struct *ha = dev->port->ha;
449 	int scheduled = 0, tries = 100;
450 
451 	/* ata: promote lun reset to bus reset */
452 	if (dev_is_sata(dev)) {
453 		sas_ata_schedule_reset(dev);
454 		if (wait)
455 			sas_ata_wait_eh(dev);
456 		return SUCCESS;
457 	}
458 
459 	while (!scheduled && tries--) {
460 		spin_lock_irq(&ha->lock);
461 		if (!test_bit(SAS_DEV_EH_PENDING, &dev->state) &&
462 		    !test_bit(reset_type, &dev->state)) {
463 			scheduled = 1;
464 			ha->eh_active++;
465 			list_add_tail(&dev->ssp_dev.eh_list_node, &ha->eh_dev_q);
466 			set_bit(SAS_DEV_EH_PENDING, &dev->state);
467 			set_bit(reset_type, &dev->state);
468 			int_to_scsilun(lun, &dev->ssp_dev.reset_lun);
469 			scsi_schedule_eh(ha->core.shost);
470 		}
471 		spin_unlock_irq(&ha->lock);
472 
473 		if (wait)
474 			sas_wait_eh(dev);
475 
476 		if (scheduled)
477 			return SUCCESS;
478 	}
479 
480 	SAS_DPRINTK("%s reset of %s failed\n",
481 		    reset_type == SAS_DEV_LU_RESET ? "LUN" : "Bus",
482 		    dev_name(&dev->rphy->dev));
483 
484 	return FAILED;
485 }
486 
487 int sas_eh_abort_handler(struct scsi_cmnd *cmd)
488 {
489 	int res;
490 	struct sas_task *task = TO_SAS_TASK(cmd);
491 	struct Scsi_Host *host = cmd->device->host;
492 	struct sas_internal *i = to_sas_internal(host->transportt);
493 
494 	if (!i->dft->lldd_abort_task)
495 		return FAILED;
496 
497 	res = i->dft->lldd_abort_task(task);
498 	if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
499 		return SUCCESS;
500 
501 	return FAILED;
502 }
503 EXPORT_SYMBOL_GPL(sas_eh_abort_handler);
504 
505 /* Attempt to send a LUN reset message to a device */
506 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
507 {
508 	int res;
509 	struct scsi_lun lun;
510 	struct Scsi_Host *host = cmd->device->host;
511 	struct domain_device *dev = cmd_to_domain_dev(cmd);
512 	struct sas_internal *i = to_sas_internal(host->transportt);
513 
514 	if (current != host->ehandler)
515 		return sas_queue_reset(dev, SAS_DEV_LU_RESET, cmd->device->lun, 0);
516 
517 	int_to_scsilun(cmd->device->lun, &lun);
518 
519 	if (!i->dft->lldd_lu_reset)
520 		return FAILED;
521 
522 	res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
523 	if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
524 		return SUCCESS;
525 
526 	return FAILED;
527 }
528 
529 int sas_eh_target_reset_handler(struct scsi_cmnd *cmd)
530 {
531 	int res;
532 	struct Scsi_Host *host = cmd->device->host;
533 	struct domain_device *dev = cmd_to_domain_dev(cmd);
534 	struct sas_internal *i = to_sas_internal(host->transportt);
535 
536 	if (current != host->ehandler)
537 		return sas_queue_reset(dev, SAS_DEV_RESET, 0, 0);
538 
539 	if (!i->dft->lldd_I_T_nexus_reset)
540 		return FAILED;
541 
542 	res = i->dft->lldd_I_T_nexus_reset(dev);
543 	if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE ||
544 	    res == -ENODEV)
545 		return SUCCESS;
546 
547 	return FAILED;
548 }
549 
550 /* Try to reset a device */
551 static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
552 {
553 	int res;
554 	struct Scsi_Host *shost = cmd->device->host;
555 
556 	if (!shost->hostt->eh_device_reset_handler)
557 		goto try_target_reset;
558 
559 	res = shost->hostt->eh_device_reset_handler(cmd);
560 	if (res == SUCCESS)
561 		return res;
562 
563 try_target_reset:
564 	if (shost->hostt->eh_target_reset_handler)
565 		return shost->hostt->eh_target_reset_handler(cmd);
566 
567 	return FAILED;
568 }
569 
570 static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q)
571 {
572 	struct scsi_cmnd *cmd, *n;
573 	enum task_disposition res = TASK_IS_DONE;
574 	int tmf_resp, need_reset;
575 	struct sas_internal *i = to_sas_internal(shost->transportt);
576 	unsigned long flags;
577 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
578 	LIST_HEAD(done);
579 
580 	/* clean out any commands that won the completion vs eh race */
581 	list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
582 		struct domain_device *dev = cmd_to_domain_dev(cmd);
583 		struct sas_task *task;
584 
585 		spin_lock_irqsave(&dev->done_lock, flags);
586 		/* by this point the lldd has either observed
587 		 * SAS_HA_FROZEN and is leaving the task alone, or has
588 		 * won the race with eh and decided to complete it
589 		 */
590 		task = TO_SAS_TASK(cmd);
591 		spin_unlock_irqrestore(&dev->done_lock, flags);
592 
593 		if (!task)
594 			list_move_tail(&cmd->eh_entry, &done);
595 	}
596 
597  Again:
598 	list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
599 		struct sas_task *task = TO_SAS_TASK(cmd);
600 
601 		list_del_init(&cmd->eh_entry);
602 
603 		spin_lock_irqsave(&task->task_state_lock, flags);
604 		need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
605 		spin_unlock_irqrestore(&task->task_state_lock, flags);
606 
607 		if (need_reset) {
608 			SAS_DPRINTK("%s: task 0x%p requests reset\n",
609 				    __func__, task);
610 			goto reset;
611 		}
612 
613 		SAS_DPRINTK("trying to find task 0x%p\n", task);
614 		res = sas_scsi_find_task(task);
615 
616 		switch (res) {
617 		case TASK_IS_DONE:
618 			SAS_DPRINTK("%s: task 0x%p is done\n", __func__,
619 				    task);
620 			sas_eh_defer_cmd(cmd);
621 			continue;
622 		case TASK_IS_ABORTED:
623 			SAS_DPRINTK("%s: task 0x%p is aborted\n",
624 				    __func__, task);
625 			sas_eh_defer_cmd(cmd);
626 			continue;
627 		case TASK_IS_AT_LU:
628 			SAS_DPRINTK("task 0x%p is at LU: lu recover\n", task);
629  reset:
630 			tmf_resp = sas_recover_lu(task->dev, cmd);
631 			if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
632 				SAS_DPRINTK("dev %016llx LU %llx is "
633 					    "recovered\n",
634 					    SAS_ADDR(task->dev),
635 					    cmd->device->lun);
636 				sas_eh_defer_cmd(cmd);
637 				sas_scsi_clear_queue_lu(work_q, cmd);
638 				goto Again;
639 			}
640 			/* fallthrough */
641 		case TASK_IS_NOT_AT_LU:
642 		case TASK_ABORT_FAILED:
643 			SAS_DPRINTK("task 0x%p is not at LU: I_T recover\n",
644 				    task);
645 			tmf_resp = sas_recover_I_T(task->dev);
646 			if (tmf_resp == TMF_RESP_FUNC_COMPLETE ||
647 			    tmf_resp == -ENODEV) {
648 				struct domain_device *dev = task->dev;
649 				SAS_DPRINTK("I_T %016llx recovered\n",
650 					    SAS_ADDR(task->dev->sas_addr));
651 				sas_eh_finish_cmd(cmd);
652 				sas_scsi_clear_queue_I_T(work_q, dev);
653 				goto Again;
654 			}
655 			/* Hammer time :-) */
656 			try_to_reset_cmd_device(cmd);
657 			if (i->dft->lldd_clear_nexus_port) {
658 				struct asd_sas_port *port = task->dev->port;
659 				SAS_DPRINTK("clearing nexus for port:%d\n",
660 					    port->id);
661 				res = i->dft->lldd_clear_nexus_port(port);
662 				if (res == TMF_RESP_FUNC_COMPLETE) {
663 					SAS_DPRINTK("clear nexus port:%d "
664 						    "succeeded\n", port->id);
665 					sas_eh_finish_cmd(cmd);
666 					sas_scsi_clear_queue_port(work_q,
667 								  port);
668 					goto Again;
669 				}
670 			}
671 			if (i->dft->lldd_clear_nexus_ha) {
672 				SAS_DPRINTK("clear nexus ha\n");
673 				res = i->dft->lldd_clear_nexus_ha(ha);
674 				if (res == TMF_RESP_FUNC_COMPLETE) {
675 					SAS_DPRINTK("clear nexus ha "
676 						    "succeeded\n");
677 					sas_eh_finish_cmd(cmd);
678 					goto clear_q;
679 				}
680 			}
681 			/* If we are here -- this means that no amount
682 			 * of effort could recover from errors.  Quite
683 			 * possibly the HA just disappeared.
684 			 */
685 			SAS_DPRINTK("error from  device %llx, LUN %llx "
686 				    "couldn't be recovered in any way\n",
687 				    SAS_ADDR(task->dev->sas_addr),
688 				    cmd->device->lun);
689 
690 			sas_eh_finish_cmd(cmd);
691 			goto clear_q;
692 		}
693 	}
694  out:
695 	list_splice_tail(&done, work_q);
696 	list_splice_tail_init(&ha->eh_ata_q, work_q);
697 	return;
698 
699  clear_q:
700 	SAS_DPRINTK("--- Exit %s -- clear_q\n", __func__);
701 	list_for_each_entry_safe(cmd, n, work_q, eh_entry)
702 		sas_eh_finish_cmd(cmd);
703 	goto out;
704 }
705 
706 static void sas_eh_handle_resets(struct Scsi_Host *shost)
707 {
708 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
709 	struct sas_internal *i = to_sas_internal(shost->transportt);
710 
711 	/* handle directed resets to sas devices */
712 	spin_lock_irq(&ha->lock);
713 	while (!list_empty(&ha->eh_dev_q)) {
714 		struct domain_device *dev;
715 		struct ssp_device *ssp;
716 
717 		ssp = list_entry(ha->eh_dev_q.next, typeof(*ssp), eh_list_node);
718 		list_del_init(&ssp->eh_list_node);
719 		dev = container_of(ssp, typeof(*dev), ssp_dev);
720 		kref_get(&dev->kref);
721 		WARN_ONCE(dev_is_sata(dev), "ssp reset to ata device?\n");
722 
723 		spin_unlock_irq(&ha->lock);
724 
725 		if (test_and_clear_bit(SAS_DEV_LU_RESET, &dev->state))
726 			i->dft->lldd_lu_reset(dev, ssp->reset_lun.scsi_lun);
727 
728 		if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
729 			i->dft->lldd_I_T_nexus_reset(dev);
730 
731 		sas_put_device(dev);
732 		spin_lock_irq(&ha->lock);
733 		clear_bit(SAS_DEV_EH_PENDING, &dev->state);
734 		ha->eh_active--;
735 	}
736 	spin_unlock_irq(&ha->lock);
737 }
738 
739 
740 void sas_scsi_recover_host(struct Scsi_Host *shost)
741 {
742 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
743 	LIST_HEAD(eh_work_q);
744 	int tries = 0;
745 	bool retry;
746 
747 retry:
748 	tries++;
749 	retry = true;
750 	spin_lock_irq(shost->host_lock);
751 	list_splice_init(&shost->eh_cmd_q, &eh_work_q);
752 	spin_unlock_irq(shost->host_lock);
753 
754 	SAS_DPRINTK("Enter %s busy: %d failed: %d\n",
755 		    __func__, atomic_read(&shost->host_busy), shost->host_failed);
756 	/*
757 	 * Deal with commands that still have SAS tasks (i.e. they didn't
758 	 * complete via the normal sas_task completion mechanism),
759 	 * SAS_HA_FROZEN gives eh dominion over all sas_task completion.
760 	 */
761 	set_bit(SAS_HA_FROZEN, &ha->state);
762 	sas_eh_handle_sas_errors(shost, &eh_work_q);
763 	clear_bit(SAS_HA_FROZEN, &ha->state);
764 	if (list_empty(&eh_work_q))
765 		goto out;
766 
767 	/*
768 	 * Now deal with SCSI commands that completed ok but have a an error
769 	 * code (and hopefully sense data) attached.  This is roughly what
770 	 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
771 	 * command we see here has no sas_task and is thus unknown to the HA.
772 	 */
773 	sas_ata_eh(shost, &eh_work_q, &ha->eh_done_q);
774 	if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
775 		scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
776 
777 out:
778 	sas_eh_handle_resets(shost);
779 
780 	/* now link into libata eh --- if we have any ata devices */
781 	sas_ata_strategy_handler(shost);
782 
783 	scsi_eh_flush_done_q(&ha->eh_done_q);
784 
785 	/* check if any new eh work was scheduled during the last run */
786 	spin_lock_irq(&ha->lock);
787 	if (ha->eh_active == 0) {
788 		shost->host_eh_scheduled = 0;
789 		retry = false;
790 	}
791 	spin_unlock_irq(&ha->lock);
792 
793 	if (retry)
794 		goto retry;
795 
796 	SAS_DPRINTK("--- Exit %s: busy: %d failed: %d tries: %d\n",
797 		    __func__, atomic_read(&shost->host_busy),
798 		    shost->host_failed, tries);
799 }
800 
801 int sas_ioctl(struct scsi_device *sdev, int cmd, void __user *arg)
802 {
803 	struct domain_device *dev = sdev_to_domain_dev(sdev);
804 
805 	if (dev_is_sata(dev))
806 		return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
807 
808 	return -EINVAL;
809 }
810 
811 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
812 {
813 	struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
814 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
815 	struct domain_device *found_dev = NULL;
816 	int i;
817 	unsigned long flags;
818 
819 	spin_lock_irqsave(&ha->phy_port_lock, flags);
820 	for (i = 0; i < ha->num_phys; i++) {
821 		struct asd_sas_port *port = ha->sas_port[i];
822 		struct domain_device *dev;
823 
824 		spin_lock(&port->dev_list_lock);
825 		list_for_each_entry(dev, &port->dev_list, dev_list_node) {
826 			if (rphy == dev->rphy) {
827 				found_dev = dev;
828 				spin_unlock(&port->dev_list_lock);
829 				goto found;
830 			}
831 		}
832 		spin_unlock(&port->dev_list_lock);
833 	}
834  found:
835 	spin_unlock_irqrestore(&ha->phy_port_lock, flags);
836 
837 	return found_dev;
838 }
839 
840 int sas_target_alloc(struct scsi_target *starget)
841 {
842 	struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
843 	struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
844 
845 	if (!found_dev)
846 		return -ENODEV;
847 
848 	kref_get(&found_dev->kref);
849 	starget->hostdata = found_dev;
850 	return 0;
851 }
852 
853 #define SAS_DEF_QD 256
854 
855 int sas_slave_configure(struct scsi_device *scsi_dev)
856 {
857 	struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
858 
859 	BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
860 
861 	if (dev_is_sata(dev)) {
862 		ata_sas_slave_configure(scsi_dev, dev->sata_dev.ap);
863 		return 0;
864 	}
865 
866 	sas_read_port_mode_page(scsi_dev);
867 
868 	if (scsi_dev->tagged_supported) {
869 		scsi_change_queue_depth(scsi_dev, SAS_DEF_QD);
870 	} else {
871 		SAS_DPRINTK("device %llx, LUN %llx doesn't support "
872 			    "TCQ\n", SAS_ADDR(dev->sas_addr),
873 			    scsi_dev->lun);
874 		scsi_change_queue_depth(scsi_dev, 1);
875 	}
876 
877 	scsi_dev->allow_restart = 1;
878 
879 	return 0;
880 }
881 
882 int sas_change_queue_depth(struct scsi_device *sdev, int depth)
883 {
884 	struct domain_device *dev = sdev_to_domain_dev(sdev);
885 
886 	if (dev_is_sata(dev))
887 		return __ata_change_queue_depth(dev->sata_dev.ap, sdev, depth);
888 
889 	if (!sdev->tagged_supported)
890 		depth = 1;
891 	return scsi_change_queue_depth(sdev, depth);
892 }
893 
894 int sas_bios_param(struct scsi_device *scsi_dev,
895 			  struct block_device *bdev,
896 			  sector_t capacity, int *hsc)
897 {
898 	hsc[0] = 255;
899 	hsc[1] = 63;
900 	sector_div(capacity, 255*63);
901 	hsc[2] = capacity;
902 
903 	return 0;
904 }
905 
906 /*
907  * Tell an upper layer that it needs to initiate an abort for a given task.
908  * This should only ever be called by an LLDD.
909  */
910 void sas_task_abort(struct sas_task *task)
911 {
912 	struct scsi_cmnd *sc = task->uldd_task;
913 
914 	/* Escape for libsas internal commands */
915 	if (!sc) {
916 		struct sas_task_slow *slow = task->slow_task;
917 
918 		if (!slow)
919 			return;
920 		if (!del_timer(&slow->timer))
921 			return;
922 		slow->timer.function(slow->timer.data);
923 		return;
924 	}
925 
926 	if (dev_is_sata(task->dev)) {
927 		sas_ata_task_abort(task);
928 	} else {
929 		struct request_queue *q = sc->device->request_queue;
930 		unsigned long flags;
931 
932 		spin_lock_irqsave(q->queue_lock, flags);
933 		blk_abort_request(sc->request);
934 		spin_unlock_irqrestore(q->queue_lock, flags);
935 	}
936 }
937 
938 void sas_target_destroy(struct scsi_target *starget)
939 {
940 	struct domain_device *found_dev = starget->hostdata;
941 
942 	if (!found_dev)
943 		return;
944 
945 	starget->hostdata = NULL;
946 	sas_put_device(found_dev);
947 }
948 
949 static void sas_parse_addr(u8 *sas_addr, const char *p)
950 {
951 	int i;
952 	for (i = 0; i < SAS_ADDR_SIZE; i++) {
953 		u8 h, l;
954 		if (!*p)
955 			break;
956 		h = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
957 		p++;
958 		l = isdigit(*p) ? *p-'0' : toupper(*p)-'A'+10;
959 		p++;
960 		sas_addr[i] = (h<<4) | l;
961 	}
962 }
963 
964 #define SAS_STRING_ADDR_SIZE	16
965 
966 int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
967 {
968 	int res;
969 	const struct firmware *fw;
970 
971 	res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
972 	if (res)
973 		return res;
974 
975 	if (fw->size < SAS_STRING_ADDR_SIZE) {
976 		res = -ENODEV;
977 		goto out;
978 	}
979 
980 	sas_parse_addr(addr, fw->data);
981 
982 out:
983 	release_firmware(fw);
984 	return res;
985 }
986 EXPORT_SYMBOL_GPL(sas_request_addr);
987 
988 EXPORT_SYMBOL_GPL(sas_queuecommand);
989 EXPORT_SYMBOL_GPL(sas_target_alloc);
990 EXPORT_SYMBOL_GPL(sas_slave_configure);
991 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
992 EXPORT_SYMBOL_GPL(sas_bios_param);
993 EXPORT_SYMBOL_GPL(sas_task_abort);
994 EXPORT_SYMBOL_GPL(sas_phy_reset);
995 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
996 EXPORT_SYMBOL_GPL(sas_eh_target_reset_handler);
997 EXPORT_SYMBOL_GPL(sas_target_destroy);
998 EXPORT_SYMBOL_GPL(sas_ioctl);
999