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