xref: /linux/drivers/scsi/libsas/sas_scsi_host.c (revision fab183d632628381b466a41479489541ac0e29a0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Serial Attached SCSI (SAS) class SCSI Host glue.
4  *
5  * Copyright (C) 2005 Adaptec, Inc.  All rights reserved.
6  * Copyright (C) 2005 Luben Tuikov <luben_tuikov@adaptec.com>
7  */
8 
9 #include <linux/kthread.h>
10 #include <linux/firmware.h>
11 #include <linux/export.h>
12 #include <linux/ctype.h>
13 #include <linux/hex.h>
14 #include <linux/kernel.h>
15 
16 #include "sas_internal.h"
17 
18 #include <scsi/scsi_host.h>
19 #include <scsi/scsi_device.h>
20 #include <scsi/scsi_tcq.h>
21 #include <scsi/scsi.h>
22 #include <scsi/scsi_eh.h>
23 #include <scsi/scsi_transport.h>
24 #include <scsi/scsi_transport_sas.h>
25 #include <scsi/sas_ata.h>
26 #include "scsi_sas_internal.h"
27 #include "scsi_transport_api.h"
28 #include "scsi_priv.h"
29 
30 #include <linux/err.h>
31 #include <linux/blkdev.h>
32 #include <linux/freezer.h>
33 #include <linux/gfp.h>
34 #include <linux/scatterlist.h>
35 #include <linux/libata.h>
36 
37 /* record final status and free the task */
sas_end_task(struct scsi_cmnd * sc,struct sas_task * task)38 static void sas_end_task(struct scsi_cmnd *sc, struct sas_task *task)
39 {
40 	struct task_status_struct *ts = &task->task_status;
41 	enum scsi_host_status hs = DID_OK;
42 	enum exec_status stat = SAS_SAM_STAT_GOOD;
43 
44 	if (ts->resp == SAS_TASK_UNDELIVERED) {
45 		/* transport error */
46 		hs = DID_NO_CONNECT;
47 	} else { /* ts->resp == SAS_TASK_COMPLETE */
48 		/* task delivered, what happened afterwards? */
49 		switch (ts->stat) {
50 		case SAS_DEV_NO_RESPONSE:
51 		case SAS_INTERRUPTED:
52 		case SAS_PHY_DOWN:
53 		case SAS_NAK_R_ERR:
54 		case SAS_OPEN_TO:
55 			hs = DID_NO_CONNECT;
56 			break;
57 		case SAS_DATA_UNDERRUN:
58 			scsi_set_resid(sc, ts->residual);
59 			if (scsi_bufflen(sc) - scsi_get_resid(sc) < sc->underflow)
60 				hs = DID_ERROR;
61 			break;
62 		case SAS_DATA_OVERRUN:
63 			hs = DID_ERROR;
64 			break;
65 		case SAS_QUEUE_FULL:
66 			hs = DID_SOFT_ERROR; /* retry */
67 			break;
68 		case SAS_DEVICE_UNKNOWN:
69 			hs = DID_BAD_TARGET;
70 			break;
71 		case SAS_OPEN_REJECT:
72 			if (ts->open_rej_reason == SAS_OREJ_RSVD_RETRY)
73 				hs = DID_SOFT_ERROR; /* retry */
74 			else
75 				hs = DID_ERROR;
76 			break;
77 		case SAS_PROTO_RESPONSE:
78 			pr_notice("LLDD:%s sent SAS_PROTO_RESP for an SSP task; please report this\n",
79 				  task->dev->port->ha->sas_ha_name);
80 			break;
81 		case SAS_ABORTED_TASK:
82 			hs = DID_ABORT;
83 			break;
84 		case SAS_SAM_STAT_CHECK_CONDITION:
85 			memcpy(sc->sense_buffer, ts->buf,
86 			       min(SCSI_SENSE_BUFFERSIZE, ts->buf_valid_size));
87 			stat = SAS_SAM_STAT_CHECK_CONDITION;
88 			break;
89 		default:
90 			stat = ts->stat;
91 			break;
92 		}
93 	}
94 
95 	sc->result = (hs << 16) | stat;
96 	ASSIGN_SAS_TASK(sc, NULL);
97 	sas_free_task(task);
98 }
99 
sas_scsi_task_done(struct sas_task * task)100 static void sas_scsi_task_done(struct sas_task *task)
101 {
102 	struct scsi_cmnd *sc = task->uldd_task;
103 	struct domain_device *dev = task->dev;
104 	struct sas_ha_struct *ha = dev->port->ha;
105 	unsigned long flags;
106 
107 	spin_lock_irqsave(&dev->done_lock, flags);
108 	if (test_bit(SAS_HA_FROZEN, &ha->state))
109 		task = NULL;
110 	else
111 		ASSIGN_SAS_TASK(sc, NULL);
112 	spin_unlock_irqrestore(&dev->done_lock, flags);
113 
114 	if (unlikely(!task)) {
115 		/* task will be completed by the error handler */
116 		pr_debug("task done but aborted\n");
117 		return;
118 	}
119 
120 	if (unlikely(!sc)) {
121 		pr_debug("task_done called with non existing SCSI cmnd!\n");
122 		sas_free_task(task);
123 		return;
124 	}
125 
126 	sas_end_task(sc, task);
127 	scsi_done(sc);
128 }
129 
sas_create_task(struct scsi_cmnd * cmd,struct domain_device * dev,gfp_t gfp_flags)130 static struct sas_task *sas_create_task(struct scsi_cmnd *cmd,
131 					       struct domain_device *dev,
132 					       gfp_t gfp_flags)
133 {
134 	struct sas_task *task = sas_alloc_task(gfp_flags);
135 	struct scsi_lun lun;
136 
137 	if (!task)
138 		return NULL;
139 
140 	task->uldd_task = cmd;
141 	ASSIGN_SAS_TASK(cmd, task);
142 
143 	task->dev = dev;
144 	task->task_proto = task->dev->tproto; /* BUG_ON(!SSP) */
145 
146 	int_to_scsilun(cmd->device->lun, &lun);
147 	memcpy(task->ssp_task.LUN, &lun.scsi_lun, 8);
148 	task->ssp_task.task_attr = TASK_ATTR_SIMPLE;
149 	task->ssp_task.cmd = cmd;
150 
151 	task->scatter = scsi_sglist(cmd);
152 	task->num_scatter = scsi_sg_count(cmd);
153 	task->total_xfer_len = scsi_bufflen(cmd);
154 	task->data_dir = cmd->sc_data_direction;
155 
156 	task->task_done = sas_scsi_task_done;
157 
158 	return task;
159 }
160 
sas_queuecommand(struct Scsi_Host * host,struct scsi_cmnd * cmd)161 enum scsi_qc_status sas_queuecommand(struct Scsi_Host *host,
162 				     struct scsi_cmnd *cmd)
163 {
164 	struct sas_internal *i = to_sas_internal(host->transportt);
165 	struct domain_device *dev = cmd_to_domain_dev(cmd);
166 	struct sas_task *task;
167 	int res = 0;
168 
169 	/* If the device fell off, no sense in issuing commands */
170 	if (test_bit(SAS_DEV_GONE, &dev->state)) {
171 		cmd->result = DID_BAD_TARGET << 16;
172 		goto out_done;
173 	}
174 
175 	if (dev_is_sata(dev)) {
176 		spin_lock_irq(dev->sata_dev.ap->lock);
177 		res = ata_sas_queuecmd(cmd, dev->sata_dev.ap);
178 		spin_unlock_irq(dev->sata_dev.ap->lock);
179 		return res;
180 	}
181 
182 	task = sas_create_task(cmd, dev, GFP_ATOMIC);
183 	if (!task)
184 		return SCSI_MLQUEUE_HOST_BUSY;
185 
186 	res = i->dft->lldd_execute_task(task, GFP_ATOMIC);
187 	if (res)
188 		goto out_free_task;
189 	return 0;
190 
191 out_free_task:
192 	pr_debug("lldd_execute_task returned: %d\n", res);
193 	ASSIGN_SAS_TASK(cmd, NULL);
194 	sas_free_task(task);
195 	if (res == -SAS_QUEUE_FULL)
196 		cmd->result = DID_SOFT_ERROR << 16; /* retry */
197 	else
198 		cmd->result = DID_ERROR << 16;
199 out_done:
200 	scsi_done(cmd);
201 	return 0;
202 }
203 EXPORT_SYMBOL_GPL(sas_queuecommand);
204 
sas_eh_finish_cmd(struct scsi_cmnd * cmd)205 static void sas_eh_finish_cmd(struct scsi_cmnd *cmd)
206 {
207 	struct sas_ha_struct *sas_ha = SHOST_TO_SAS_HA(cmd->device->host);
208 	struct domain_device *dev = cmd_to_domain_dev(cmd);
209 	struct sas_task *task = TO_SAS_TASK(cmd);
210 
211 	/* At this point, we only get called following an actual abort
212 	 * of the task, so we should be guaranteed not to be racing with
213 	 * any completions from the LLD.  Task is freed after this.
214 	 */
215 	sas_end_task(cmd, task);
216 
217 	if (dev_is_sata(dev)) {
218 		/* defer commands to libata so that libata EH can
219 		 * handle ata qcs correctly
220 		 */
221 		list_move_tail(&cmd->eh_entry, &sas_ha->eh_ata_q);
222 		return;
223 	}
224 
225 	/* now finish the command and move it on to the error
226 	 * handler done list, this also takes it off the
227 	 * error handler pending list.
228 	 */
229 	scsi_eh_finish_cmd(cmd, &sas_ha->eh_done_q);
230 }
231 
sas_scsi_clear_queue_lu(struct list_head * error_q,struct scsi_cmnd * my_cmd)232 static void sas_scsi_clear_queue_lu(struct list_head *error_q, struct scsi_cmnd *my_cmd)
233 {
234 	struct scsi_cmnd *cmd, *n;
235 
236 	list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
237 		if (cmd->device->sdev_target == my_cmd->device->sdev_target &&
238 		    cmd->device->lun == my_cmd->device->lun)
239 			sas_eh_finish_cmd(cmd);
240 	}
241 }
242 
sas_scsi_clear_queue_I_T(struct list_head * error_q,struct domain_device * dev)243 static void sas_scsi_clear_queue_I_T(struct list_head *error_q,
244 				     struct domain_device *dev)
245 {
246 	struct scsi_cmnd *cmd, *n;
247 
248 	list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
249 		struct domain_device *x = cmd_to_domain_dev(cmd);
250 
251 		if (x == dev)
252 			sas_eh_finish_cmd(cmd);
253 	}
254 }
255 
sas_scsi_clear_queue_port(struct list_head * error_q,struct asd_sas_port * port)256 static void sas_scsi_clear_queue_port(struct list_head *error_q,
257 				      struct asd_sas_port *port)
258 {
259 	struct scsi_cmnd *cmd, *n;
260 
261 	list_for_each_entry_safe(cmd, n, error_q, eh_entry) {
262 		struct domain_device *dev = cmd_to_domain_dev(cmd);
263 		struct asd_sas_port *x = dev->port;
264 
265 		if (x == port)
266 			sas_eh_finish_cmd(cmd);
267 	}
268 }
269 
270 enum task_disposition {
271 	TASK_IS_DONE,
272 	TASK_IS_ABORTED,
273 	TASK_IS_AT_LU,
274 	TASK_IS_NOT_AT_LU,
275 	TASK_ABORT_FAILED,
276 };
277 
sas_scsi_find_task(struct sas_task * task)278 static enum task_disposition sas_scsi_find_task(struct sas_task *task)
279 {
280 	unsigned long flags;
281 	int i, res;
282 	struct sas_internal *si =
283 		to_sas_internal(task->dev->port->ha->shost->transportt);
284 
285 	for (i = 0; i < 5; i++) {
286 		pr_notice("%s: aborting task 0x%p\n", __func__, task);
287 		res = si->dft->lldd_abort_task(task);
288 
289 		spin_lock_irqsave(&task->task_state_lock, flags);
290 		if (task->task_state_flags & SAS_TASK_STATE_DONE) {
291 			spin_unlock_irqrestore(&task->task_state_lock, flags);
292 			pr_debug("%s: task 0x%p is done\n", __func__, task);
293 			return TASK_IS_DONE;
294 		}
295 		spin_unlock_irqrestore(&task->task_state_lock, flags);
296 
297 		if (res == TMF_RESP_FUNC_COMPLETE) {
298 			pr_notice("%s: task 0x%p is aborted\n",
299 				  __func__, task);
300 			return TASK_IS_ABORTED;
301 		} else if (si->dft->lldd_query_task) {
302 			pr_notice("%s: querying task 0x%p\n", __func__, task);
303 			res = si->dft->lldd_query_task(task);
304 			switch (res) {
305 			case TMF_RESP_FUNC_SUCC:
306 				pr_notice("%s: task 0x%p at LU\n", __func__,
307 					  task);
308 				return TASK_IS_AT_LU;
309 			case TMF_RESP_FUNC_COMPLETE:
310 				pr_notice("%s: task 0x%p not at LU\n",
311 					  __func__, task);
312 				return TASK_IS_NOT_AT_LU;
313 			case TMF_RESP_FUNC_FAILED:
314 				pr_notice("%s: task 0x%p failed to abort\n",
315 					  __func__, task);
316 				return TASK_ABORT_FAILED;
317 			default:
318 				pr_notice("%s: task 0x%p result code %d not handled\n",
319 					  __func__, task, res);
320 			}
321 		}
322 	}
323 	return TASK_ABORT_FAILED;
324 }
325 
sas_recover_lu(struct domain_device * dev,struct scsi_cmnd * cmd)326 static int sas_recover_lu(struct domain_device *dev, struct scsi_cmnd *cmd)
327 {
328 	int res = TMF_RESP_FUNC_FAILED;
329 	struct scsi_lun lun;
330 	struct sas_internal *i =
331 		to_sas_internal(dev->port->ha->shost->transportt);
332 
333 	int_to_scsilun(cmd->device->lun, &lun);
334 
335 	pr_notice("eh: device %016llx LUN 0x%llx has the task\n",
336 		  SAS_ADDR(dev->sas_addr),
337 		  cmd->device->lun);
338 
339 	if (i->dft->lldd_abort_task_set)
340 		res = i->dft->lldd_abort_task_set(dev, lun.scsi_lun);
341 
342 	if (res == TMF_RESP_FUNC_FAILED) {
343 		if (i->dft->lldd_clear_task_set)
344 			res = i->dft->lldd_clear_task_set(dev, lun.scsi_lun);
345 	}
346 
347 	if (res == TMF_RESP_FUNC_FAILED) {
348 		if (i->dft->lldd_lu_reset)
349 			res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
350 	}
351 
352 	return res;
353 }
354 
sas_recover_I_T(struct domain_device * dev)355 static int sas_recover_I_T(struct domain_device *dev)
356 {
357 	int res = TMF_RESP_FUNC_FAILED;
358 	struct sas_internal *i =
359 		to_sas_internal(dev->port->ha->shost->transportt);
360 
361 	pr_notice("I_T nexus reset for dev %016llx\n",
362 		  SAS_ADDR(dev->sas_addr));
363 
364 	if (i->dft->lldd_I_T_nexus_reset)
365 		res = i->dft->lldd_I_T_nexus_reset(dev);
366 
367 	return res;
368 }
369 
370 /* take a reference on the last known good phy for this device */
sas_get_local_phy(struct domain_device * dev)371 struct sas_phy *sas_get_local_phy(struct domain_device *dev)
372 {
373 	struct sas_ha_struct *ha = dev->port->ha;
374 	struct sas_phy *phy;
375 	unsigned long flags;
376 
377 	/* a published domain device always has a valid phy, it may be
378 	 * stale, but it is never NULL
379 	 */
380 	BUG_ON(!dev->phy);
381 
382 	spin_lock_irqsave(&ha->phy_port_lock, flags);
383 	phy = dev->phy;
384 	get_device(&phy->dev);
385 	spin_unlock_irqrestore(&ha->phy_port_lock, flags);
386 
387 	return phy;
388 }
389 EXPORT_SYMBOL_GPL(sas_get_local_phy);
390 
sas_queue_reset(struct domain_device * dev,int reset_type,u64 lun)391 static int sas_queue_reset(struct domain_device *dev, int reset_type, u64 lun)
392 {
393 	struct sas_ha_struct *ha = dev->port->ha;
394 	int scheduled = 0, tries = 100;
395 
396 	/* ata: promote lun reset to bus reset */
397 	if (dev_is_sata(dev)) {
398 		sas_ata_schedule_reset(dev);
399 		return SUCCESS;
400 	}
401 
402 	while (!scheduled && tries--) {
403 		spin_lock_irq(&ha->lock);
404 		if (!test_bit(SAS_DEV_EH_PENDING, &dev->state) &&
405 		    !test_bit(reset_type, &dev->state)) {
406 			scheduled = 1;
407 			ha->eh_active++;
408 			list_add_tail(&dev->ssp_dev.eh_list_node, &ha->eh_dev_q);
409 			set_bit(SAS_DEV_EH_PENDING, &dev->state);
410 			set_bit(reset_type, &dev->state);
411 			int_to_scsilun(lun, &dev->ssp_dev.reset_lun);
412 			scsi_schedule_eh(ha->shost);
413 		}
414 		spin_unlock_irq(&ha->lock);
415 
416 		if (scheduled)
417 			return SUCCESS;
418 	}
419 
420 	pr_warn("%s reset of %s failed\n",
421 		reset_type == SAS_DEV_LU_RESET ? "LUN" : "Bus",
422 		dev_name(&dev->rphy->dev));
423 
424 	return FAILED;
425 }
426 
sas_eh_abort_handler(struct scsi_cmnd * cmd)427 int sas_eh_abort_handler(struct scsi_cmnd *cmd)
428 {
429 	int res = TMF_RESP_FUNC_FAILED;
430 	struct sas_task *task = TO_SAS_TASK(cmd);
431 	struct Scsi_Host *host = cmd->device->host;
432 	struct domain_device *dev = cmd_to_domain_dev(cmd);
433 	struct sas_internal *i = to_sas_internal(host->transportt);
434 	unsigned long flags;
435 
436 	if (!i->dft->lldd_abort_task)
437 		return FAILED;
438 
439 	spin_lock_irqsave(host->host_lock, flags);
440 	/* We cannot do async aborts for SATA devices */
441 	if (dev_is_sata(dev) && !host->host_eh_scheduled) {
442 		spin_unlock_irqrestore(host->host_lock, flags);
443 		return FAILED;
444 	}
445 	spin_unlock_irqrestore(host->host_lock, flags);
446 
447 	if (task)
448 		res = i->dft->lldd_abort_task(task);
449 	else
450 		pr_notice("no task to abort\n");
451 	if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
452 		return SUCCESS;
453 
454 	return FAILED;
455 }
456 EXPORT_SYMBOL_GPL(sas_eh_abort_handler);
457 
458 /* Attempt to send a LUN reset message to a device */
sas_eh_device_reset_handler(struct scsi_cmnd * cmd)459 int sas_eh_device_reset_handler(struct scsi_cmnd *cmd)
460 {
461 	int res;
462 	struct scsi_lun lun;
463 	struct Scsi_Host *host = cmd->device->host;
464 	struct domain_device *dev = cmd_to_domain_dev(cmd);
465 	struct sas_internal *i = to_sas_internal(host->transportt);
466 
467 	if (current != host->ehandler)
468 		return sas_queue_reset(dev, SAS_DEV_LU_RESET, cmd->device->lun);
469 
470 	int_to_scsilun(cmd->device->lun, &lun);
471 
472 	if (!i->dft->lldd_lu_reset)
473 		return FAILED;
474 
475 	res = i->dft->lldd_lu_reset(dev, lun.scsi_lun);
476 	if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE)
477 		return SUCCESS;
478 
479 	return FAILED;
480 }
481 EXPORT_SYMBOL_GPL(sas_eh_device_reset_handler);
482 
sas_eh_target_reset_handler(struct scsi_cmnd * cmd)483 int sas_eh_target_reset_handler(struct scsi_cmnd *cmd)
484 {
485 	int res;
486 	struct Scsi_Host *host = cmd->device->host;
487 	struct domain_device *dev = cmd_to_domain_dev(cmd);
488 	struct sas_internal *i = to_sas_internal(host->transportt);
489 
490 	if (current != host->ehandler)
491 		return sas_queue_reset(dev, SAS_DEV_RESET, 0);
492 
493 	if (!i->dft->lldd_I_T_nexus_reset)
494 		return FAILED;
495 
496 	res = i->dft->lldd_I_T_nexus_reset(dev);
497 	if (res == TMF_RESP_FUNC_SUCC || res == TMF_RESP_FUNC_COMPLETE ||
498 	    res == -ENODEV)
499 		return SUCCESS;
500 
501 	return FAILED;
502 }
503 EXPORT_SYMBOL_GPL(sas_eh_target_reset_handler);
504 
505 /*
506  * Handle deferred QCs in case of a command timeout.
507  * See ata_scsi_eh_timed_out() for details.
508  */
sas_eh_timed_out(struct scsi_cmnd * cmd)509 enum scsi_timeout_action sas_eh_timed_out(struct scsi_cmnd *cmd)
510 {
511 	struct domain_device *dev = cmd_to_domain_dev(cmd);
512 
513 	if (dev_is_sata(dev))
514 		return ata_scsi_retry_deferred_qc(dev->sata_dev.ap, cmd);
515 
516 	return SCSI_EH_NOT_HANDLED;
517 }
518 EXPORT_SYMBOL_GPL(sas_eh_timed_out);
519 
520 /* Try to reset a device */
try_to_reset_cmd_device(struct scsi_cmnd * cmd)521 static int try_to_reset_cmd_device(struct scsi_cmnd *cmd)
522 {
523 	int res;
524 	struct Scsi_Host *shost = cmd->device->host;
525 
526 	if (!shost->hostt->eh_device_reset_handler)
527 		goto try_target_reset;
528 
529 	res = shost->hostt->eh_device_reset_handler(cmd);
530 	if (res == SUCCESS)
531 		return res;
532 
533 try_target_reset:
534 	if (shost->hostt->eh_target_reset_handler)
535 		return shost->hostt->eh_target_reset_handler(cmd);
536 
537 	return FAILED;
538 }
539 
sas_eh_handle_sas_errors(struct Scsi_Host * shost,struct list_head * work_q)540 static void sas_eh_handle_sas_errors(struct Scsi_Host *shost, struct list_head *work_q)
541 {
542 	struct scsi_cmnd *cmd, *n;
543 	enum task_disposition res = TASK_IS_DONE;
544 	int tmf_resp, need_reset;
545 	struct sas_internal *i = to_sas_internal(shost->transportt);
546 	unsigned long flags;
547 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
548 	LIST_HEAD(done);
549 
550 	/* clean out any commands that won the completion vs eh race */
551 	list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
552 		struct domain_device *dev = cmd_to_domain_dev(cmd);
553 		struct sas_task *task;
554 
555 		spin_lock_irqsave(&dev->done_lock, flags);
556 		/* by this point the lldd has either observed
557 		 * SAS_HA_FROZEN and is leaving the task alone, or has
558 		 * won the race with eh and decided to complete it
559 		 */
560 		task = TO_SAS_TASK(cmd);
561 		spin_unlock_irqrestore(&dev->done_lock, flags);
562 
563 		if (!task)
564 			list_move_tail(&cmd->eh_entry, &done);
565 	}
566 
567  Again:
568 	list_for_each_entry_safe(cmd, n, work_q, eh_entry) {
569 		struct sas_task *task = TO_SAS_TASK(cmd);
570 
571 		list_del_init(&cmd->eh_entry);
572 
573 		spin_lock_irqsave(&task->task_state_lock, flags);
574 		need_reset = task->task_state_flags & SAS_TASK_NEED_DEV_RESET;
575 		spin_unlock_irqrestore(&task->task_state_lock, flags);
576 
577 		if (need_reset) {
578 			pr_notice("%s: task 0x%p requests reset\n",
579 				  __func__, task);
580 			goto reset;
581 		}
582 
583 		pr_debug("trying to find task 0x%p\n", task);
584 		res = sas_scsi_find_task(task);
585 
586 		switch (res) {
587 		case TASK_IS_DONE:
588 			pr_notice("%s: task 0x%p is done\n", __func__,
589 				    task);
590 			sas_eh_finish_cmd(cmd);
591 			continue;
592 		case TASK_IS_ABORTED:
593 			pr_notice("%s: task 0x%p is aborted\n",
594 				  __func__, task);
595 			sas_eh_finish_cmd(cmd);
596 			continue;
597 		case TASK_IS_AT_LU:
598 			pr_info("task 0x%p is at LU: lu recover\n", task);
599  reset:
600 			tmf_resp = sas_recover_lu(task->dev, cmd);
601 			if (tmf_resp == TMF_RESP_FUNC_COMPLETE) {
602 				pr_notice("dev %016llx LU 0x%llx is recovered\n",
603 					  SAS_ADDR(task->dev),
604 					  cmd->device->lun);
605 				sas_eh_finish_cmd(cmd);
606 				sas_scsi_clear_queue_lu(work_q, cmd);
607 				goto Again;
608 			}
609 			fallthrough;
610 		case TASK_IS_NOT_AT_LU:
611 		case TASK_ABORT_FAILED:
612 			pr_notice("task 0x%p is not at LU: I_T recover\n",
613 				  task);
614 			tmf_resp = sas_recover_I_T(task->dev);
615 			if (tmf_resp == TMF_RESP_FUNC_COMPLETE ||
616 			    tmf_resp == -ENODEV) {
617 				struct domain_device *dev = task->dev;
618 				pr_notice("I_T %016llx recovered\n",
619 					  SAS_ADDR(task->dev->sas_addr));
620 				sas_eh_finish_cmd(cmd);
621 				sas_scsi_clear_queue_I_T(work_q, dev);
622 				goto Again;
623 			}
624 			/* Hammer time :-) */
625 			try_to_reset_cmd_device(cmd);
626 			if (i->dft->lldd_clear_nexus_port) {
627 				struct asd_sas_port *port = task->dev->port;
628 				pr_debug("clearing nexus for port:%d\n",
629 					  port->id);
630 				res = i->dft->lldd_clear_nexus_port(port);
631 				if (res == TMF_RESP_FUNC_COMPLETE) {
632 					pr_notice("clear nexus port:%d succeeded\n",
633 						  port->id);
634 					sas_eh_finish_cmd(cmd);
635 					sas_scsi_clear_queue_port(work_q,
636 								  port);
637 					goto Again;
638 				}
639 			}
640 			if (i->dft->lldd_clear_nexus_ha) {
641 				pr_debug("clear nexus ha\n");
642 				res = i->dft->lldd_clear_nexus_ha(ha);
643 				if (res == TMF_RESP_FUNC_COMPLETE) {
644 					pr_notice("clear nexus ha succeeded\n");
645 					sas_eh_finish_cmd(cmd);
646 					goto clear_q;
647 				}
648 			}
649 			/* If we are here -- this means that no amount
650 			 * of effort could recover from errors.  Quite
651 			 * possibly the HA just disappeared.
652 			 */
653 			pr_err("error from device %016llx, LUN 0x%llx couldn't be recovered in any way\n",
654 			       SAS_ADDR(task->dev->sas_addr),
655 			       cmd->device->lun);
656 
657 			sas_eh_finish_cmd(cmd);
658 			goto clear_q;
659 		}
660 	}
661  out:
662 	list_splice_tail(&done, work_q);
663 	list_splice_tail_init(&ha->eh_ata_q, work_q);
664 	return;
665 
666  clear_q:
667 	pr_debug("--- Exit %s -- clear_q\n", __func__);
668 	list_for_each_entry_safe(cmd, n, work_q, eh_entry)
669 		sas_eh_finish_cmd(cmd);
670 	goto out;
671 }
672 
sas_eh_handle_resets(struct Scsi_Host * shost)673 static void sas_eh_handle_resets(struct Scsi_Host *shost)
674 {
675 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
676 	struct sas_internal *i = to_sas_internal(shost->transportt);
677 
678 	/* handle directed resets to sas devices */
679 	spin_lock_irq(&ha->lock);
680 	while (!list_empty(&ha->eh_dev_q)) {
681 		struct domain_device *dev;
682 		struct ssp_device *ssp;
683 
684 		ssp = list_entry(ha->eh_dev_q.next, typeof(*ssp), eh_list_node);
685 		list_del_init(&ssp->eh_list_node);
686 		dev = container_of(ssp, typeof(*dev), ssp_dev);
687 		kref_get(&dev->kref);
688 		WARN_ONCE(dev_is_sata(dev), "ssp reset to ata device?\n");
689 
690 		spin_unlock_irq(&ha->lock);
691 
692 		if (test_and_clear_bit(SAS_DEV_LU_RESET, &dev->state))
693 			i->dft->lldd_lu_reset(dev, ssp->reset_lun.scsi_lun);
694 
695 		if (test_and_clear_bit(SAS_DEV_RESET, &dev->state))
696 			i->dft->lldd_I_T_nexus_reset(dev);
697 
698 		sas_put_device(dev);
699 		spin_lock_irq(&ha->lock);
700 		clear_bit(SAS_DEV_EH_PENDING, &dev->state);
701 		ha->eh_active--;
702 	}
703 	spin_unlock_irq(&ha->lock);
704 }
705 
706 
sas_scsi_recover_host(struct Scsi_Host * shost)707 void sas_scsi_recover_host(struct Scsi_Host *shost)
708 {
709 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
710 	LIST_HEAD(eh_work_q);
711 	int tries = 0;
712 	bool retry;
713 
714 retry:
715 	tries++;
716 	retry = true;
717 	spin_lock_irq(shost->host_lock);
718 	list_splice_init(&shost->eh_cmd_q, &eh_work_q);
719 	spin_unlock_irq(shost->host_lock);
720 
721 	pr_notice("Enter %s busy: %d failed: %d\n",
722 		  __func__, scsi_host_busy(shost), shost->host_failed);
723 	/*
724 	 * Deal with commands that still have SAS tasks (i.e. they didn't
725 	 * complete via the normal sas_task completion mechanism),
726 	 * SAS_HA_FROZEN gives eh dominion over all sas_task completion.
727 	 */
728 	set_bit(SAS_HA_FROZEN, &ha->state);
729 	sas_eh_handle_sas_errors(shost, &eh_work_q);
730 	clear_bit(SAS_HA_FROZEN, &ha->state);
731 	if (list_empty(&eh_work_q))
732 		goto out;
733 
734 	/*
735 	 * Now deal with SCSI commands that completed ok but have a an error
736 	 * code (and hopefully sense data) attached.  This is roughly what
737 	 * scsi_unjam_host does, but we skip scsi_eh_abort_cmds because any
738 	 * command we see here has no sas_task and is thus unknown to the HA.
739 	 */
740 	sas_ata_eh(shost, &eh_work_q);
741 	if (!scsi_eh_get_sense(&eh_work_q, &ha->eh_done_q))
742 		scsi_eh_ready_devs(shost, &eh_work_q, &ha->eh_done_q);
743 
744 out:
745 	sas_eh_handle_resets(shost);
746 
747 	/* now link into libata eh --- if we have any ata devices */
748 	sas_ata_strategy_handler(shost);
749 
750 	scsi_eh_flush_done_q(&ha->eh_done_q);
751 
752 	/* check if any new eh work was scheduled during the last run */
753 	spin_lock_irq(&ha->lock);
754 	if (ha->eh_active == 0) {
755 		shost->host_eh_scheduled = 0;
756 		retry = false;
757 	}
758 	spin_unlock_irq(&ha->lock);
759 
760 	if (retry)
761 		goto retry;
762 
763 	pr_notice("--- Exit %s: busy: %d failed: %d tries: %d\n",
764 		  __func__, scsi_host_busy(shost),
765 		  shost->host_failed, tries);
766 }
767 
sas_ioctl(struct scsi_device * sdev,unsigned int cmd,void __user * arg)768 int sas_ioctl(struct scsi_device *sdev, unsigned int cmd, void __user *arg)
769 {
770 	struct domain_device *dev = sdev_to_domain_dev(sdev);
771 
772 	if (dev_is_sata(dev))
773 		return ata_sas_scsi_ioctl(dev->sata_dev.ap, sdev, cmd, arg);
774 
775 	return -EINVAL;
776 }
777 EXPORT_SYMBOL_GPL(sas_ioctl);
778 
sas_find_dev_by_rphy(struct sas_rphy * rphy)779 struct domain_device *sas_find_dev_by_rphy(struct sas_rphy *rphy)
780 {
781 	struct Scsi_Host *shost = dev_to_shost(rphy->dev.parent);
782 	struct sas_ha_struct *ha = SHOST_TO_SAS_HA(shost);
783 	struct domain_device *found_dev = NULL;
784 	int i;
785 	unsigned long flags;
786 
787 	spin_lock_irqsave(&ha->phy_port_lock, flags);
788 	for (i = 0; i < ha->num_phys; i++) {
789 		struct asd_sas_port *port = ha->sas_port[i];
790 		struct domain_device *dev;
791 
792 		spin_lock(&port->dev_list_lock);
793 		list_for_each_entry(dev, &port->dev_list, dev_list_node) {
794 			if (rphy == dev->rphy) {
795 				found_dev = dev;
796 				spin_unlock(&port->dev_list_lock);
797 				goto found;
798 			}
799 		}
800 		spin_unlock(&port->dev_list_lock);
801 	}
802  found:
803 	spin_unlock_irqrestore(&ha->phy_port_lock, flags);
804 
805 	return found_dev;
806 }
807 
sas_target_alloc(struct scsi_target * starget)808 int sas_target_alloc(struct scsi_target *starget)
809 {
810 	struct sas_rphy *rphy = dev_to_rphy(starget->dev.parent);
811 	struct domain_device *found_dev = sas_find_dev_by_rphy(rphy);
812 
813 	if (!found_dev)
814 		return -ENODEV;
815 
816 	kref_get(&found_dev->kref);
817 	starget->hostdata = found_dev;
818 	return 0;
819 }
820 EXPORT_SYMBOL_GPL(sas_target_alloc);
821 
822 #define SAS_DEF_QD 256
823 
sas_sdev_configure(struct scsi_device * scsi_dev,struct queue_limits * lim)824 int sas_sdev_configure(struct scsi_device *scsi_dev, struct queue_limits *lim)
825 {
826 	struct domain_device *dev = sdev_to_domain_dev(scsi_dev);
827 
828 	BUG_ON(dev->rphy->identify.device_type != SAS_END_DEVICE);
829 
830 	if (dev_is_sata(dev)) {
831 		ata_sas_sdev_configure(scsi_dev, lim, dev->sata_dev.ap);
832 		return 0;
833 	}
834 
835 	sas_read_port_mode_page(scsi_dev);
836 
837 	if (scsi_dev->tagged_supported) {
838 		scsi_change_queue_depth(scsi_dev, SAS_DEF_QD);
839 	} else {
840 		pr_notice("device %016llx, LUN 0x%llx doesn't support TCQ\n",
841 			  SAS_ADDR(dev->sas_addr), scsi_dev->lun);
842 		scsi_change_queue_depth(scsi_dev, 1);
843 	}
844 
845 	scsi_dev->allow_restart = 1;
846 
847 	return 0;
848 }
849 EXPORT_SYMBOL_GPL(sas_sdev_configure);
850 
sas_change_queue_depth(struct scsi_device * sdev,int depth)851 int sas_change_queue_depth(struct scsi_device *sdev, int depth)
852 {
853 	struct domain_device *dev = sdev_to_domain_dev(sdev);
854 
855 	if (dev_is_sata(dev))
856 		return ata_change_queue_depth(dev->sata_dev.ap, sdev, depth);
857 
858 	if (!sdev->tagged_supported)
859 		depth = 1;
860 	return scsi_change_queue_depth(sdev, depth);
861 }
862 EXPORT_SYMBOL_GPL(sas_change_queue_depth);
863 
sas_bios_param(struct scsi_device * scsi_dev,struct gendisk * unused,sector_t capacity,int * hsc)864 int sas_bios_param(struct scsi_device *scsi_dev,
865 			  struct gendisk *unused,
866 			  sector_t capacity, int *hsc)
867 {
868 	hsc[0] = 255;
869 	hsc[1] = 63;
870 	sector_div(capacity, 255*63);
871 	hsc[2] = capacity;
872 
873 	return 0;
874 }
875 EXPORT_SYMBOL_GPL(sas_bios_param);
876 
sas_task_internal_done(struct sas_task * task)877 void sas_task_internal_done(struct sas_task *task)
878 {
879 	timer_delete(&task->slow_task->timer);
880 	complete(&task->slow_task->completion);
881 }
882 
sas_task_internal_timedout(struct timer_list * t)883 void sas_task_internal_timedout(struct timer_list *t)
884 {
885 	struct sas_task_slow *slow = timer_container_of(slow, t, timer);
886 	struct sas_task *task = slow->task;
887 	bool is_completed = true;
888 	unsigned long flags;
889 
890 	spin_lock_irqsave(&task->task_state_lock, flags);
891 	if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
892 		task->task_state_flags |= SAS_TASK_STATE_ABORTED;
893 		is_completed = false;
894 	}
895 	spin_unlock_irqrestore(&task->task_state_lock, flags);
896 
897 	if (!is_completed)
898 		complete(&task->slow_task->completion);
899 }
900 
901 #define TASK_TIMEOUT			(20 * HZ)
902 #define TASK_RETRY			3
903 
sas_execute_internal_abort(struct domain_device * device,enum sas_internal_abort type,u16 tag,unsigned int qid,void * data)904 static int sas_execute_internal_abort(struct domain_device *device,
905 				      enum sas_internal_abort type, u16 tag,
906 				      unsigned int qid, void *data)
907 {
908 	struct sas_ha_struct *ha = device->port->ha;
909 	struct sas_internal *i = to_sas_internal(ha->shost->transportt);
910 	struct sas_task *task = NULL;
911 	int res, retry;
912 
913 	for (retry = 0; retry < TASK_RETRY; retry++) {
914 		task = sas_alloc_slow_task(GFP_KERNEL);
915 		if (!task)
916 			return -ENOMEM;
917 
918 		task->dev = device;
919 		task->task_proto = SAS_PROTOCOL_INTERNAL_ABORT;
920 		task->task_done = sas_task_internal_done;
921 		task->slow_task->timer.function = sas_task_internal_timedout;
922 		task->slow_task->timer.expires = jiffies + TASK_TIMEOUT;
923 		add_timer(&task->slow_task->timer);
924 
925 		task->abort_task.tag = tag;
926 		task->abort_task.type = type;
927 		task->abort_task.qid = qid;
928 
929 		res = i->dft->lldd_execute_task(task, GFP_KERNEL);
930 		if (res) {
931 			timer_delete_sync(&task->slow_task->timer);
932 			pr_err("Executing internal abort failed %016llx (%d)\n",
933 			       SAS_ADDR(device->sas_addr), res);
934 			break;
935 		}
936 
937 		wait_for_completion(&task->slow_task->completion);
938 		res = TMF_RESP_FUNC_FAILED;
939 
940 		/* Even if the internal abort timed out, return direct. */
941 		if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
942 			bool quit = true;
943 
944 			if (i->dft->lldd_abort_timeout)
945 				quit = i->dft->lldd_abort_timeout(task, data);
946 			else
947 				pr_err("Internal abort: timeout %016llx\n",
948 				       SAS_ADDR(device->sas_addr));
949 			res = -EIO;
950 			if (quit)
951 				break;
952 		}
953 
954 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
955 			task->task_status.stat == SAS_SAM_STAT_GOOD) {
956 			res = TMF_RESP_FUNC_COMPLETE;
957 			break;
958 		}
959 
960 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
961 			task->task_status.stat == TMF_RESP_FUNC_SUCC) {
962 			res = TMF_RESP_FUNC_SUCC;
963 			break;
964 		}
965 
966 		pr_err("Internal abort: task to dev %016llx response: 0x%x status 0x%x\n",
967 		       SAS_ADDR(device->sas_addr), task->task_status.resp,
968 		       task->task_status.stat);
969 		sas_free_task(task);
970 		task = NULL;
971 	}
972 	BUG_ON(retry == TASK_RETRY && task != NULL);
973 	sas_free_task(task);
974 	return res;
975 }
976 
sas_execute_internal_abort_single(struct domain_device * device,u16 tag,unsigned int qid,void * data)977 int sas_execute_internal_abort_single(struct domain_device *device, u16 tag,
978 				      unsigned int qid, void *data)
979 {
980 	return sas_execute_internal_abort(device, SAS_INTERNAL_ABORT_SINGLE,
981 					  tag, qid, data);
982 }
983 EXPORT_SYMBOL_GPL(sas_execute_internal_abort_single);
984 
sas_execute_internal_abort_dev(struct domain_device * device,unsigned int qid,void * data)985 int sas_execute_internal_abort_dev(struct domain_device *device,
986 				   unsigned int qid, void *data)
987 {
988 	return sas_execute_internal_abort(device, SAS_INTERNAL_ABORT_DEV,
989 					  SCSI_NO_TAG, qid, data);
990 }
991 EXPORT_SYMBOL_GPL(sas_execute_internal_abort_dev);
992 
sas_execute_tmf(struct domain_device * device,void * parameter,int para_len,int force_phy_id,struct sas_tmf_task * tmf)993 int sas_execute_tmf(struct domain_device *device, void *parameter,
994 		    int para_len, int force_phy_id,
995 		    struct sas_tmf_task *tmf)
996 {
997 	struct sas_task *task;
998 	struct sas_internal *i =
999 		to_sas_internal(device->port->ha->shost->transportt);
1000 	int res, retry;
1001 
1002 	for (retry = 0; retry < TASK_RETRY; retry++) {
1003 		task = sas_alloc_slow_task(GFP_KERNEL);
1004 		if (!task)
1005 			return -ENOMEM;
1006 
1007 		task->dev = device;
1008 		task->task_proto = device->tproto;
1009 
1010 		if (dev_is_sata(device)) {
1011 			task->ata_task.device_control_reg_update = 1;
1012 			if (force_phy_id >= 0) {
1013 				task->ata_task.force_phy = true;
1014 				task->ata_task.force_phy_id = force_phy_id;
1015 			}
1016 			memcpy(&task->ata_task.fis, parameter, para_len);
1017 		} else {
1018 			memcpy(&task->ssp_task, parameter, para_len);
1019 		}
1020 
1021 		task->task_done = sas_task_internal_done;
1022 		task->tmf = tmf;
1023 
1024 		task->slow_task->timer.function = sas_task_internal_timedout;
1025 		task->slow_task->timer.expires = jiffies + TASK_TIMEOUT;
1026 		add_timer(&task->slow_task->timer);
1027 
1028 		res = i->dft->lldd_execute_task(task, GFP_KERNEL);
1029 		if (res) {
1030 			timer_delete_sync(&task->slow_task->timer);
1031 			pr_err("executing TMF task failed %016llx (%d)\n",
1032 			       SAS_ADDR(device->sas_addr), res);
1033 			break;
1034 		}
1035 
1036 		wait_for_completion(&task->slow_task->completion);
1037 
1038 		if (i->dft->lldd_tmf_exec_complete)
1039 			i->dft->lldd_tmf_exec_complete(device);
1040 
1041 		res = TMF_RESP_FUNC_FAILED;
1042 
1043 		if ((task->task_state_flags & SAS_TASK_STATE_ABORTED)) {
1044 			if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) {
1045 				pr_err("TMF task timeout for %016llx and not done\n",
1046 				       SAS_ADDR(device->sas_addr));
1047 				if (i->dft->lldd_tmf_aborted)
1048 					i->dft->lldd_tmf_aborted(task);
1049 				break;
1050 			}
1051 			pr_warn("TMF task timeout for %016llx and done\n",
1052 				SAS_ADDR(device->sas_addr));
1053 		}
1054 
1055 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
1056 		    task->task_status.stat == TMF_RESP_FUNC_COMPLETE) {
1057 			res = TMF_RESP_FUNC_COMPLETE;
1058 			break;
1059 		}
1060 
1061 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
1062 		    task->task_status.stat == TMF_RESP_FUNC_SUCC) {
1063 			res = TMF_RESP_FUNC_SUCC;
1064 			break;
1065 		}
1066 
1067 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
1068 		    task->task_status.stat == SAS_DATA_UNDERRUN) {
1069 			/* no error, but return the number of bytes of
1070 			 * underrun
1071 			 */
1072 			pr_warn("TMF task to dev %016llx resp: 0x%x sts 0x%x underrun\n",
1073 				SAS_ADDR(device->sas_addr),
1074 				task->task_status.resp,
1075 				task->task_status.stat);
1076 			res = task->task_status.residual;
1077 			break;
1078 		}
1079 
1080 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
1081 		    task->task_status.stat == SAS_DATA_OVERRUN) {
1082 			pr_warn("TMF task blocked task error %016llx\n",
1083 				SAS_ADDR(device->sas_addr));
1084 			res = -EMSGSIZE;
1085 			break;
1086 		}
1087 
1088 		if (task->task_status.resp == SAS_TASK_COMPLETE &&
1089 		    task->task_status.stat == SAS_OPEN_REJECT) {
1090 			pr_warn("TMF task open reject failed  %016llx\n",
1091 				SAS_ADDR(device->sas_addr));
1092 			res = -EIO;
1093 		} else {
1094 			pr_warn("TMF task to dev %016llx resp: 0x%x status 0x%x\n",
1095 				SAS_ADDR(device->sas_addr),
1096 				task->task_status.resp,
1097 				task->task_status.stat);
1098 		}
1099 		sas_free_task(task);
1100 		task = NULL;
1101 	}
1102 
1103 	if (retry == TASK_RETRY)
1104 		pr_warn("executing TMF for %016llx failed after %d attempts!\n",
1105 			SAS_ADDR(device->sas_addr), TASK_RETRY);
1106 	sas_free_task(task);
1107 
1108 	return res;
1109 }
1110 
sas_execute_ssp_tmf(struct domain_device * device,u8 * lun,struct sas_tmf_task * tmf)1111 static int sas_execute_ssp_tmf(struct domain_device *device, u8 *lun,
1112 			       struct sas_tmf_task *tmf)
1113 {
1114 	struct sas_ssp_task ssp_task;
1115 
1116 	if (!(device->tproto & SAS_PROTOCOL_SSP))
1117 		return TMF_RESP_FUNC_ESUPP;
1118 
1119 	memcpy(ssp_task.LUN, lun, 8);
1120 
1121 	return sas_execute_tmf(device, &ssp_task, sizeof(ssp_task), -1, tmf);
1122 }
1123 
sas_abort_task_set(struct domain_device * dev,u8 * lun)1124 int sas_abort_task_set(struct domain_device *dev, u8 *lun)
1125 {
1126 	struct sas_tmf_task tmf_task = {
1127 		.tmf = TMF_ABORT_TASK_SET,
1128 	};
1129 
1130 	return sas_execute_ssp_tmf(dev, lun, &tmf_task);
1131 }
1132 EXPORT_SYMBOL_GPL(sas_abort_task_set);
1133 
sas_clear_task_set(struct domain_device * dev,u8 * lun)1134 int sas_clear_task_set(struct domain_device *dev, u8 *lun)
1135 {
1136 	struct sas_tmf_task tmf_task = {
1137 		.tmf = TMF_CLEAR_TASK_SET,
1138 	};
1139 
1140 	return sas_execute_ssp_tmf(dev, lun, &tmf_task);
1141 }
1142 EXPORT_SYMBOL_GPL(sas_clear_task_set);
1143 
sas_lu_reset(struct domain_device * dev,u8 * lun)1144 int sas_lu_reset(struct domain_device *dev, u8 *lun)
1145 {
1146 	struct sas_tmf_task tmf_task = {
1147 		.tmf = TMF_LU_RESET,
1148 	};
1149 
1150 	return sas_execute_ssp_tmf(dev, lun, &tmf_task);
1151 }
1152 EXPORT_SYMBOL_GPL(sas_lu_reset);
1153 
sas_query_task(struct sas_task * task,u16 tag)1154 int sas_query_task(struct sas_task *task, u16 tag)
1155 {
1156 	struct sas_tmf_task tmf_task = {
1157 		.tmf = TMF_QUERY_TASK,
1158 		.tag_of_task_to_be_managed = tag,
1159 	};
1160 	struct scsi_cmnd *cmnd = task->uldd_task;
1161 	struct domain_device *dev = task->dev;
1162 	struct scsi_lun lun;
1163 
1164 	int_to_scsilun(cmnd->device->lun, &lun);
1165 
1166 	return sas_execute_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1167 }
1168 EXPORT_SYMBOL_GPL(sas_query_task);
1169 
sas_abort_task(struct sas_task * task,u16 tag)1170 int sas_abort_task(struct sas_task *task, u16 tag)
1171 {
1172 	struct sas_tmf_task tmf_task = {
1173 		.tmf = TMF_ABORT_TASK,
1174 		.tag_of_task_to_be_managed = tag,
1175 	};
1176 	struct scsi_cmnd *cmnd = task->uldd_task;
1177 	struct domain_device *dev = task->dev;
1178 	struct scsi_lun lun;
1179 
1180 	int_to_scsilun(cmnd->device->lun, &lun);
1181 
1182 	return sas_execute_ssp_tmf(dev, lun.scsi_lun, &tmf_task);
1183 }
1184 EXPORT_SYMBOL_GPL(sas_abort_task);
1185 
1186 /*
1187  * Tell an upper layer that it needs to initiate an abort for a given task.
1188  * This should only ever be called by an LLDD.
1189  */
sas_task_abort(struct sas_task * task)1190 void sas_task_abort(struct sas_task *task)
1191 {
1192 	struct scsi_cmnd *sc = task->uldd_task;
1193 
1194 	/* Escape for libsas internal commands */
1195 	if (!sc) {
1196 		struct sas_task_slow *slow = task->slow_task;
1197 
1198 		if (!slow)
1199 			return;
1200 		if (!timer_delete(&slow->timer))
1201 			return;
1202 		slow->timer.function(&slow->timer);
1203 		return;
1204 	}
1205 
1206 	if (dev_is_sata(task->dev))
1207 		sas_ata_task_abort(task);
1208 	else
1209 		blk_abort_request(scsi_cmd_to_rq(sc));
1210 }
1211 EXPORT_SYMBOL_GPL(sas_task_abort);
1212 
sas_sdev_init(struct scsi_device * sdev)1213 int sas_sdev_init(struct scsi_device *sdev)
1214 {
1215 	if (dev_is_sata(sdev_to_domain_dev(sdev)) && sdev->lun)
1216 		return -ENXIO;
1217 
1218 	return 0;
1219 }
1220 EXPORT_SYMBOL_GPL(sas_sdev_init);
1221 
sas_target_destroy(struct scsi_target * starget)1222 void sas_target_destroy(struct scsi_target *starget)
1223 {
1224 	struct domain_device *found_dev = starget->hostdata;
1225 
1226 	if (!found_dev)
1227 		return;
1228 
1229 	starget->hostdata = NULL;
1230 	sas_put_device(found_dev);
1231 }
1232 EXPORT_SYMBOL_GPL(sas_target_destroy);
1233 
1234 #define SAS_STRING_ADDR_SIZE	16
1235 
sas_request_addr(struct Scsi_Host * shost,u8 * addr)1236 int sas_request_addr(struct Scsi_Host *shost, u8 *addr)
1237 {
1238 	int res;
1239 	const struct firmware *fw;
1240 
1241 	res = request_firmware(&fw, "sas_addr", &shost->shost_gendev);
1242 	if (res)
1243 		return res;
1244 
1245 	if (fw->size < SAS_STRING_ADDR_SIZE) {
1246 		res = -ENODEV;
1247 		goto out;
1248 	}
1249 
1250 	res = hex2bin(addr, fw->data, strnlen(fw->data, SAS_ADDR_SIZE * 2) / 2);
1251 	if (res)
1252 		goto out;
1253 
1254 out:
1255 	release_firmware(fw);
1256 	return res;
1257 }
1258 EXPORT_SYMBOL_GPL(sas_request_addr);
1259 
1260