1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56#include <linux/completion.h>
57#include <linux/irqflags.h>
58#include "sas.h"
59#include <scsi/libsas.h>
60#include "remote_device.h"
61#include "remote_node_context.h"
62#include "isci.h"
63#include "request.h"
64#include "task.h"
65#include "host.h"
66
67
68
69
70
71
72
73
74
75
76static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
77 enum service_response response,
78 enum exec_status status)
79
80{
81 unsigned long flags;
82
83
84 dev_dbg(&ihost->pdev->dev, "%s: task = %p, response=%d, status=%d\n",
85 __func__, task, response, status);
86
87 spin_lock_irqsave(&task->task_state_lock, flags);
88
89 task->task_status.resp = response;
90 task->task_status.stat = status;
91
92
93 task->task_state_flags |= SAS_TASK_STATE_DONE;
94 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
95 SAS_TASK_STATE_PENDING);
96 task->lldd_task = NULL;
97 spin_unlock_irqrestore(&task->task_state_lock, flags);
98
99 task->task_done(task);
100}
101
102#define for_each_sas_task(num, task) \
103 for (; num > 0; num--,\
104 task = list_entry(task->list.next, struct sas_task, list))
105
106
107static inline int isci_device_io_ready(struct isci_remote_device *idev,
108 struct sas_task *task)
109{
110 return idev ? test_bit(IDEV_IO_READY, &idev->flags) ||
111 (test_bit(IDEV_IO_NCQERROR, &idev->flags) &&
112 isci_task_is_ncq_recovery(task))
113 : 0;
114}
115
116
117
118
119
120
121
122
123
124
125int isci_task_execute_task(struct sas_task *task, int num, gfp_t gfp_flags)
126{
127 struct isci_host *ihost = dev_to_ihost(task->dev);
128 struct isci_remote_device *idev;
129 unsigned long flags;
130 bool io_ready;
131 u16 tag;
132
133 dev_dbg(&ihost->pdev->dev, "%s: num=%d\n", __func__, num);
134
135 for_each_sas_task(num, task) {
136 enum sci_status status = SCI_FAILURE;
137
138 spin_lock_irqsave(&ihost->scic_lock, flags);
139 idev = isci_lookup_device(task->dev);
140 io_ready = isci_device_io_ready(idev, task);
141 tag = isci_alloc_tag(ihost);
142 spin_unlock_irqrestore(&ihost->scic_lock, flags);
143
144 dev_dbg(&ihost->pdev->dev,
145 "task: %p, num: %d dev: %p idev: %p:%#lx cmd = %p\n",
146 task, num, task->dev, idev, idev ? idev->flags : 0,
147 task->uldd_task);
148
149 if (!idev) {
150 isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
151 SAS_DEVICE_UNKNOWN);
152 } else if (!io_ready || tag == SCI_CONTROLLER_INVALID_IO_TAG) {
153
154
155
156 isci_task_refuse(ihost, task, SAS_TASK_COMPLETE,
157 SAS_QUEUE_FULL);
158 } else {
159
160 spin_lock_irqsave(&task->task_state_lock, flags);
161
162 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
163
164 spin_unlock_irqrestore(&task->task_state_lock,
165 flags);
166
167 isci_task_refuse(ihost, task,
168 SAS_TASK_UNDELIVERED,
169 SAM_STAT_TASK_ABORTED);
170 } else {
171 task->task_state_flags |= SAS_TASK_AT_INITIATOR;
172 spin_unlock_irqrestore(&task->task_state_lock, flags);
173
174
175 status = isci_request_execute(ihost, idev, task, tag);
176
177 if (status != SCI_SUCCESS) {
178
179 spin_lock_irqsave(&task->task_state_lock, flags);
180
181 task->task_state_flags &= ~SAS_TASK_AT_INITIATOR;
182 spin_unlock_irqrestore(&task->task_state_lock, flags);
183
184 if (test_bit(IDEV_GONE, &idev->flags)) {
185
186
187
188
189 isci_task_refuse(ihost, task,
190 SAS_TASK_UNDELIVERED,
191 SAS_DEVICE_UNKNOWN);
192 } else {
193
194
195
196
197
198
199
200
201 isci_task_refuse(ihost, task,
202 SAS_TASK_COMPLETE,
203 SAS_QUEUE_FULL);
204 }
205 }
206 }
207 }
208 if (status != SCI_SUCCESS && tag != SCI_CONTROLLER_INVALID_IO_TAG) {
209 spin_lock_irqsave(&ihost->scic_lock, flags);
210
211
212
213 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
214 spin_unlock_irqrestore(&ihost->scic_lock, flags);
215 }
216 isci_put_device(idev);
217 }
218 return 0;
219}
220
221static struct isci_request *isci_task_request_build(struct isci_host *ihost,
222 struct isci_remote_device *idev,
223 u16 tag, struct isci_tmf *isci_tmf)
224{
225 enum sci_status status = SCI_FAILURE;
226 struct isci_request *ireq = NULL;
227 struct domain_device *dev;
228
229 dev_dbg(&ihost->pdev->dev,
230 "%s: isci_tmf = %p\n", __func__, isci_tmf);
231
232 dev = idev->domain_dev;
233
234
235 ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
236 if (!ireq)
237 return NULL;
238
239
240 status = sci_task_request_construct(ihost, idev, tag,
241 ireq);
242
243 if (status != SCI_SUCCESS) {
244 dev_warn(&ihost->pdev->dev,
245 "%s: sci_task_request_construct failed - "
246 "status = 0x%x\n",
247 __func__,
248 status);
249 return NULL;
250 }
251
252
253 if (dev->dev_type == SAS_END_DEVICE) {
254 isci_tmf->proto = SAS_PROTOCOL_SSP;
255 status = sci_task_request_construct_ssp(ireq);
256 if (status != SCI_SUCCESS)
257 return NULL;
258 }
259
260 return ireq;
261}
262
263static int isci_task_execute_tmf(struct isci_host *ihost,
264 struct isci_remote_device *idev,
265 struct isci_tmf *tmf, unsigned long timeout_ms)
266{
267 DECLARE_COMPLETION_ONSTACK(completion);
268 enum sci_task_status status = SCI_TASK_FAILURE;
269 struct isci_request *ireq;
270 int ret = TMF_RESP_FUNC_FAILED;
271 unsigned long flags;
272 unsigned long timeleft;
273 u16 tag;
274
275 spin_lock_irqsave(&ihost->scic_lock, flags);
276 tag = isci_alloc_tag(ihost);
277 spin_unlock_irqrestore(&ihost->scic_lock, flags);
278
279 if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
280 return ret;
281
282
283
284
285 if (!idev ||
286 (!test_bit(IDEV_IO_READY, &idev->flags) &&
287 !test_bit(IDEV_IO_NCQERROR, &idev->flags))) {
288 dev_dbg(&ihost->pdev->dev,
289 "%s: idev = %p not ready (%#lx)\n",
290 __func__,
291 idev, idev ? idev->flags : 0);
292 goto err_tci;
293 } else
294 dev_dbg(&ihost->pdev->dev,
295 "%s: idev = %p\n",
296 __func__, idev);
297
298
299 tmf->complete = &completion;
300 tmf->status = SCI_FAILURE_TIMEOUT;
301
302 ireq = isci_task_request_build(ihost, idev, tag, tmf);
303 if (!ireq)
304 goto err_tci;
305
306 spin_lock_irqsave(&ihost->scic_lock, flags);
307
308
309 status = sci_controller_start_task(ihost, idev, ireq);
310
311 if (status != SCI_TASK_SUCCESS) {
312 dev_dbg(&ihost->pdev->dev,
313 "%s: start_io failed - status = 0x%x, request = %p\n",
314 __func__,
315 status,
316 ireq);
317 spin_unlock_irqrestore(&ihost->scic_lock, flags);
318 goto err_tci;
319 }
320 spin_unlock_irqrestore(&ihost->scic_lock, flags);
321
322
323 isci_remote_device_resume_from_abort(ihost, idev);
324
325
326 timeleft = wait_for_completion_timeout(&completion,
327 msecs_to_jiffies(timeout_ms));
328
329 if (timeleft == 0) {
330
331
332
333 isci_remote_device_suspend_terminate(ihost, idev, ireq);
334 }
335
336 isci_print_tmf(ihost, tmf);
337
338 if (tmf->status == SCI_SUCCESS)
339 ret = TMF_RESP_FUNC_COMPLETE;
340 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
341 dev_dbg(&ihost->pdev->dev,
342 "%s: tmf.status == "
343 "SCI_FAILURE_IO_RESPONSE_VALID\n",
344 __func__);
345 ret = TMF_RESP_FUNC_COMPLETE;
346 }
347
348
349 dev_dbg(&ihost->pdev->dev,
350 "%s: completed request = %p\n",
351 __func__,
352 ireq);
353
354 return ret;
355
356 err_tci:
357 spin_lock_irqsave(&ihost->scic_lock, flags);
358 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
359 spin_unlock_irqrestore(&ihost->scic_lock, flags);
360
361 return ret;
362}
363
364static void isci_task_build_tmf(struct isci_tmf *tmf,
365 enum isci_tmf_function_codes code)
366{
367 memset(tmf, 0, sizeof(*tmf));
368 tmf->tmf_code = code;
369}
370
371static void isci_task_build_abort_task_tmf(struct isci_tmf *tmf,
372 enum isci_tmf_function_codes code,
373 struct isci_request *old_request)
374{
375 isci_task_build_tmf(tmf, code);
376 tmf->io_tag = old_request->io_tag;
377}
378
379
380
381
382
383
384
385
386static int isci_task_send_lu_reset_sas(
387 struct isci_host *isci_host,
388 struct isci_remote_device *isci_device,
389 u8 *lun)
390{
391 struct isci_tmf tmf;
392 int ret = TMF_RESP_FUNC_FAILED;
393
394 dev_dbg(&isci_host->pdev->dev,
395 "%s: isci_host = %p, isci_device = %p\n",
396 __func__, isci_host, isci_device);
397
398
399
400
401
402 isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset);
403
404 #define ISCI_LU_RESET_TIMEOUT_MS 2000
405 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
406
407 if (ret == TMF_RESP_FUNC_COMPLETE)
408 dev_dbg(&isci_host->pdev->dev,
409 "%s: %p: TMF_LU_RESET passed\n",
410 __func__, isci_device);
411 else
412 dev_dbg(&isci_host->pdev->dev,
413 "%s: %p: TMF_LU_RESET failed (%x)\n",
414 __func__, isci_device, ret);
415
416 return ret;
417}
418
419int isci_task_lu_reset(struct domain_device *dev, u8 *lun)
420{
421 struct isci_host *ihost = dev_to_ihost(dev);
422 struct isci_remote_device *idev;
423 unsigned long flags;
424 int ret = TMF_RESP_FUNC_COMPLETE;
425
426 spin_lock_irqsave(&ihost->scic_lock, flags);
427 idev = isci_get_device(dev->lldd_dev);
428 spin_unlock_irqrestore(&ihost->scic_lock, flags);
429
430 dev_dbg(&ihost->pdev->dev,
431 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
432 __func__, dev, ihost, idev);
433
434 if (!idev) {
435
436 dev_dbg(&ihost->pdev->dev, "%s: No dev\n", __func__);
437
438 ret = TMF_RESP_FUNC_FAILED;
439 goto out;
440 }
441
442
443 if (isci_remote_device_suspend_terminate(ihost, idev, NULL)
444 != SCI_SUCCESS) {
445
446 ret = TMF_RESP_FUNC_FAILED;
447 goto out;
448 }
449
450 if (!test_bit(IDEV_GONE, &idev->flags)) {
451 if (dev_is_sata(dev))
452 sas_ata_schedule_reset(dev);
453 else
454
455 ret = isci_task_send_lu_reset_sas(ihost, idev, lun);
456 }
457 out:
458 isci_put_device(idev);
459 return ret;
460}
461
462
463
464int isci_task_clear_nexus_port(struct asd_sas_port *port)
465{
466 return TMF_RESP_FUNC_FAILED;
467}
468
469
470
471int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
472{
473 return TMF_RESP_FUNC_FAILED;
474}
475
476
477
478
479
480
481
482
483
484
485int isci_task_abort_task(struct sas_task *task)
486{
487 struct isci_host *ihost = dev_to_ihost(task->dev);
488 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
489 struct isci_request *old_request = NULL;
490 struct isci_remote_device *idev = NULL;
491 struct isci_tmf tmf;
492 int ret = TMF_RESP_FUNC_FAILED;
493 unsigned long flags;
494 int target_done_already = 0;
495
496
497
498
499
500
501 spin_lock_irqsave(&ihost->scic_lock, flags);
502 spin_lock(&task->task_state_lock);
503
504 old_request = task->lldd_task;
505
506
507 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
508 (task->task_state_flags & SAS_TASK_AT_INITIATOR) &&
509 old_request) {
510 idev = isci_get_device(task->dev->lldd_dev);
511 target_done_already = test_bit(IREQ_COMPLETE_IN_TARGET,
512 &old_request->flags);
513 }
514 spin_unlock(&task->task_state_lock);
515 spin_unlock_irqrestore(&ihost->scic_lock, flags);
516
517 dev_warn(&ihost->pdev->dev,
518 "%s: dev = %p (%s%s), task = %p, old_request == %p\n",
519 __func__, idev,
520 (dev_is_sata(task->dev) ? "STP/SATA"
521 : ((dev_is_expander(task->dev))
522 ? "SMP"
523 : "SSP")),
524 ((idev) ? ((test_bit(IDEV_GONE, &idev->flags))
525 ? " IDEV_GONE"
526 : "")
527 : " <NULL>"),
528 task, old_request);
529
530
531
532
533
534 if (!idev || !old_request) {
535
536
537
538
539
540 spin_lock_irqsave(&task->task_state_lock, flags);
541 task->task_state_flags |= SAS_TASK_STATE_DONE;
542 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
543 SAS_TASK_STATE_PENDING);
544 spin_unlock_irqrestore(&task->task_state_lock, flags);
545
546 ret = TMF_RESP_FUNC_COMPLETE;
547
548 dev_warn(&ihost->pdev->dev,
549 "%s: abort task not needed for %p\n",
550 __func__, task);
551 goto out;
552 }
553
554 if (isci_remote_device_suspend_terminate(ihost, idev, old_request)
555 != SCI_SUCCESS) {
556 dev_warn(&ihost->pdev->dev,
557 "%s: isci_remote_device_reset_terminate(dev=%p, "
558 "req=%p, task=%p) failed\n",
559 __func__, idev, old_request, task);
560 ret = TMF_RESP_FUNC_FAILED;
561 goto out;
562 }
563 spin_lock_irqsave(&ihost->scic_lock, flags);
564
565 if (task->task_proto == SAS_PROTOCOL_SMP ||
566 sas_protocol_ata(task->task_proto) ||
567 target_done_already ||
568 test_bit(IDEV_GONE, &idev->flags)) {
569
570 spin_unlock_irqrestore(&ihost->scic_lock, flags);
571
572
573 isci_remote_device_resume_from_abort(ihost, idev);
574
575 dev_warn(&ihost->pdev->dev,
576 "%s: %s request"
577 " or complete_in_target (%d), "
578 "or IDEV_GONE (%d), thus no TMF\n",
579 __func__,
580 ((task->task_proto == SAS_PROTOCOL_SMP)
581 ? "SMP"
582 : (sas_protocol_ata(task->task_proto)
583 ? "SATA/STP"
584 : "<other>")
585 ),
586 test_bit(IREQ_COMPLETE_IN_TARGET,
587 &old_request->flags),
588 test_bit(IDEV_GONE, &idev->flags));
589
590 spin_lock_irqsave(&task->task_state_lock, flags);
591 task->task_state_flags &= ~(SAS_TASK_AT_INITIATOR |
592 SAS_TASK_STATE_PENDING);
593 task->task_state_flags |= SAS_TASK_STATE_DONE;
594 spin_unlock_irqrestore(&task->task_state_lock, flags);
595
596 ret = TMF_RESP_FUNC_COMPLETE;
597 } else {
598
599 isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
600 old_request);
601
602 spin_unlock_irqrestore(&ihost->scic_lock, flags);
603
604
605 #define ISCI_ABORT_TASK_TIMEOUT_MS 500
606 ret = isci_task_execute_tmf(ihost, idev, &tmf,
607 ISCI_ABORT_TASK_TIMEOUT_MS);
608 }
609out:
610 dev_warn(&ihost->pdev->dev,
611 "%s: Done; dev = %p, task = %p , old_request == %p\n",
612 __func__, idev, task, old_request);
613 isci_put_device(idev);
614 return ret;
615}
616
617
618
619
620
621
622
623
624
625
626
627int isci_task_abort_task_set(
628 struct domain_device *d_device,
629 u8 *lun)
630{
631 return TMF_RESP_FUNC_FAILED;
632}
633
634
635
636
637
638
639
640
641
642
643
644int isci_task_clear_aca(
645 struct domain_device *d_device,
646 u8 *lun)
647{
648 return TMF_RESP_FUNC_FAILED;
649}
650
651
652
653
654
655
656
657
658
659
660
661
662int isci_task_clear_task_set(
663 struct domain_device *d_device,
664 u8 *lun)
665{
666 return TMF_RESP_FUNC_FAILED;
667}
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682int isci_task_query_task(
683 struct sas_task *task)
684{
685
686 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
687 return TMF_RESP_FUNC_FAILED;
688 else
689 return TMF_RESP_FUNC_SUCC;
690}
691
692
693
694
695
696
697
698
699
700
701
702void
703isci_task_request_complete(struct isci_host *ihost,
704 struct isci_request *ireq,
705 enum sci_task_status completion_status)
706{
707 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
708 struct completion *tmf_complete = NULL;
709
710 dev_dbg(&ihost->pdev->dev,
711 "%s: request = %p, status=%d\n",
712 __func__, ireq, completion_status);
713
714 set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
715
716 if (tmf) {
717 tmf->status = completion_status;
718
719 if (tmf->proto == SAS_PROTOCOL_SSP) {
720 memcpy(&tmf->resp.resp_iu,
721 &ireq->ssp.rsp,
722 SSP_RESP_IU_MAX_SIZE);
723 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
724 memcpy(&tmf->resp.d2h_fis,
725 &ireq->stp.rsp,
726 sizeof(struct dev_to_host_fis));
727 }
728
729 tmf_complete = tmf->complete;
730 }
731 sci_controller_complete_io(ihost, ireq->target_device, ireq);
732
733
734
735 set_bit(IREQ_TERMINATED, &ireq->flags);
736
737 if (test_and_clear_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags))
738 wake_up_all(&ihost->eventq);
739
740 if (!test_bit(IREQ_NO_AUTO_FREE_TAG, &ireq->flags))
741 isci_free_tag(ihost, ireq->io_tag);
742
743
744 if (tmf_complete)
745 complete(tmf_complete);
746}
747
748static int isci_reset_device(struct isci_host *ihost,
749 struct domain_device *dev,
750 struct isci_remote_device *idev)
751{
752 int rc = TMF_RESP_FUNC_COMPLETE, reset_stat = -1;
753 struct sas_phy *phy = sas_get_local_phy(dev);
754 struct isci_port *iport = dev->port->lldd_port;
755
756 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
757
758
759 if (isci_remote_device_suspend_terminate(ihost, idev, NULL)
760 != SCI_SUCCESS) {
761 rc = TMF_RESP_FUNC_FAILED;
762 goto out;
763 }
764
765
766
767
768
769
770 if (!test_bit(IDEV_GONE, &idev->flags)) {
771 if (scsi_is_sas_phy_local(phy)) {
772 struct isci_phy *iphy = &ihost->phys[phy->number];
773
774 reset_stat = isci_port_perform_hard_reset(ihost, iport,
775 iphy);
776 } else
777 reset_stat = sas_phy_reset(phy, !dev_is_sata(dev));
778 }
779
780 isci_remote_device_resume_from_abort(ihost, idev);
781
782 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete, reset_stat=%d.\n",
783 __func__, idev, reset_stat);
784 out:
785 sas_put_local_phy(phy);
786 return rc;
787}
788
789int isci_task_I_T_nexus_reset(struct domain_device *dev)
790{
791 struct isci_host *ihost = dev_to_ihost(dev);
792 struct isci_remote_device *idev;
793 unsigned long flags;
794 int ret;
795
796 spin_lock_irqsave(&ihost->scic_lock, flags);
797 idev = isci_get_device(dev->lldd_dev);
798 spin_unlock_irqrestore(&ihost->scic_lock, flags);
799
800 if (!idev) {
801
802
803
804 ret = -ENODEV;
805 goto out;
806 }
807
808 ret = isci_reset_device(ihost, dev, idev);
809 out:
810 isci_put_device(idev);
811 return ret;
812}
813