0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056 #include <linux/completion.h>
0057 #include <linux/irqflags.h>
0058 #include "sas.h"
0059 #include <scsi/libsas.h>
0060 #include "remote_device.h"
0061 #include "remote_node_context.h"
0062 #include "isci.h"
0063 #include "request.h"
0064 #include "task.h"
0065 #include "host.h"
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076 static void isci_task_refuse(struct isci_host *ihost, struct sas_task *task,
0077 enum service_response response,
0078 enum exec_status status)
0079
0080 {
0081 unsigned long flags;
0082
0083
0084 dev_dbg(&ihost->pdev->dev, "%s: task = %p, response=%d, status=%d\n",
0085 __func__, task, response, status);
0086
0087 spin_lock_irqsave(&task->task_state_lock, flags);
0088
0089 task->task_status.resp = response;
0090 task->task_status.stat = status;
0091
0092
0093 task->task_state_flags |= SAS_TASK_STATE_DONE;
0094 task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
0095 task->lldd_task = NULL;
0096 spin_unlock_irqrestore(&task->task_state_lock, flags);
0097
0098 task->task_done(task);
0099 }
0100
0101 #define for_each_sas_task(num, task) \
0102 for (; num > 0; num--,\
0103 task = list_entry(task->list.next, struct sas_task, list))
0104
0105
0106 static inline int isci_device_io_ready(struct isci_remote_device *idev,
0107 struct sas_task *task)
0108 {
0109 return idev ? test_bit(IDEV_IO_READY, &idev->flags) ||
0110 (test_bit(IDEV_IO_NCQERROR, &idev->flags) &&
0111 isci_task_is_ncq_recovery(task))
0112 : 0;
0113 }
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123 int isci_task_execute_task(struct sas_task *task, gfp_t gfp_flags)
0124 {
0125 struct isci_host *ihost = dev_to_ihost(task->dev);
0126 struct isci_remote_device *idev;
0127 unsigned long flags;
0128 enum sci_status status = SCI_FAILURE;
0129 bool io_ready;
0130 u16 tag;
0131
0132 spin_lock_irqsave(&ihost->scic_lock, flags);
0133 idev = isci_lookup_device(task->dev);
0134 io_ready = isci_device_io_ready(idev, task);
0135 tag = isci_alloc_tag(ihost);
0136 spin_unlock_irqrestore(&ihost->scic_lock, flags);
0137
0138 dev_dbg(&ihost->pdev->dev,
0139 "task: %p, dev: %p idev: %p:%#lx cmd = %p\n",
0140 task, task->dev, idev, idev ? idev->flags : 0,
0141 task->uldd_task);
0142
0143 if (!idev) {
0144 isci_task_refuse(ihost, task, SAS_TASK_UNDELIVERED,
0145 SAS_DEVICE_UNKNOWN);
0146 } else if (!io_ready || tag == SCI_CONTROLLER_INVALID_IO_TAG) {
0147
0148
0149
0150 isci_task_refuse(ihost, task, SAS_TASK_COMPLETE,
0151 SAS_QUEUE_FULL);
0152 } else {
0153
0154 spin_lock_irqsave(&task->task_state_lock, flags);
0155
0156 if (task->task_state_flags & SAS_TASK_STATE_ABORTED) {
0157
0158 spin_unlock_irqrestore(&task->task_state_lock, flags);
0159
0160 isci_task_refuse(ihost, task,
0161 SAS_TASK_UNDELIVERED,
0162 SAS_SAM_STAT_TASK_ABORTED);
0163 } else {
0164 struct isci_request *ireq;
0165
0166
0167 ireq = isci_io_request_from_tag(ihost, task, tag);
0168 spin_unlock_irqrestore(&task->task_state_lock, flags);
0169
0170
0171
0172 status = isci_request_execute(ihost, idev, task, ireq);
0173
0174 if (status != SCI_SUCCESS) {
0175 if (test_bit(IDEV_GONE, &idev->flags)) {
0176
0177
0178
0179 isci_task_refuse(ihost, task,
0180 SAS_TASK_UNDELIVERED,
0181 SAS_DEVICE_UNKNOWN);
0182 } else {
0183
0184
0185
0186
0187
0188
0189
0190
0191 isci_task_refuse(ihost, task,
0192 SAS_TASK_COMPLETE,
0193 SAS_QUEUE_FULL);
0194 }
0195 }
0196 }
0197 }
0198
0199 if (status != SCI_SUCCESS && tag != SCI_CONTROLLER_INVALID_IO_TAG) {
0200 spin_lock_irqsave(&ihost->scic_lock, flags);
0201
0202
0203
0204 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
0205 spin_unlock_irqrestore(&ihost->scic_lock, flags);
0206 }
0207
0208 isci_put_device(idev);
0209 return 0;
0210 }
0211
0212 static struct isci_request *isci_task_request_build(struct isci_host *ihost,
0213 struct isci_remote_device *idev,
0214 u16 tag, struct isci_tmf *isci_tmf)
0215 {
0216 enum sci_status status = SCI_FAILURE;
0217 struct isci_request *ireq = NULL;
0218 struct domain_device *dev;
0219
0220 dev_dbg(&ihost->pdev->dev,
0221 "%s: isci_tmf = %p\n", __func__, isci_tmf);
0222
0223 dev = idev->domain_dev;
0224
0225
0226 ireq = isci_tmf_request_from_tag(ihost, isci_tmf, tag);
0227 if (!ireq)
0228 return NULL;
0229
0230
0231 status = sci_task_request_construct(ihost, idev, tag,
0232 ireq);
0233
0234 if (status != SCI_SUCCESS) {
0235 dev_warn(&ihost->pdev->dev,
0236 "%s: sci_task_request_construct failed - "
0237 "status = 0x%x\n",
0238 __func__,
0239 status);
0240 return NULL;
0241 }
0242
0243
0244 if (dev->dev_type == SAS_END_DEVICE) {
0245 isci_tmf->proto = SAS_PROTOCOL_SSP;
0246 status = sci_task_request_construct_ssp(ireq);
0247 if (status != SCI_SUCCESS)
0248 return NULL;
0249 }
0250
0251 return ireq;
0252 }
0253
0254 static int isci_task_execute_tmf(struct isci_host *ihost,
0255 struct isci_remote_device *idev,
0256 struct isci_tmf *tmf, unsigned long timeout_ms)
0257 {
0258 DECLARE_COMPLETION_ONSTACK(completion);
0259 enum sci_status status = SCI_FAILURE;
0260 struct isci_request *ireq;
0261 int ret = TMF_RESP_FUNC_FAILED;
0262 unsigned long flags;
0263 unsigned long timeleft;
0264 u16 tag;
0265
0266 spin_lock_irqsave(&ihost->scic_lock, flags);
0267 tag = isci_alloc_tag(ihost);
0268 spin_unlock_irqrestore(&ihost->scic_lock, flags);
0269
0270 if (tag == SCI_CONTROLLER_INVALID_IO_TAG)
0271 return ret;
0272
0273
0274
0275
0276 if (!idev ||
0277 (!test_bit(IDEV_IO_READY, &idev->flags) &&
0278 !test_bit(IDEV_IO_NCQERROR, &idev->flags))) {
0279 dev_dbg(&ihost->pdev->dev,
0280 "%s: idev = %p not ready (%#lx)\n",
0281 __func__,
0282 idev, idev ? idev->flags : 0);
0283 goto err_tci;
0284 } else
0285 dev_dbg(&ihost->pdev->dev,
0286 "%s: idev = %p\n",
0287 __func__, idev);
0288
0289
0290 tmf->complete = &completion;
0291 tmf->status = SCI_FAILURE_TIMEOUT;
0292
0293 ireq = isci_task_request_build(ihost, idev, tag, tmf);
0294 if (!ireq)
0295 goto err_tci;
0296
0297 spin_lock_irqsave(&ihost->scic_lock, flags);
0298
0299
0300 status = sci_controller_start_task(ihost, idev, ireq);
0301
0302 if (status != SCI_SUCCESS) {
0303 dev_dbg(&ihost->pdev->dev,
0304 "%s: start_io failed - status = 0x%x, request = %p\n",
0305 __func__,
0306 status,
0307 ireq);
0308 spin_unlock_irqrestore(&ihost->scic_lock, flags);
0309 goto err_tci;
0310 }
0311 spin_unlock_irqrestore(&ihost->scic_lock, flags);
0312
0313
0314 isci_remote_device_resume_from_abort(ihost, idev);
0315
0316
0317 timeleft = wait_for_completion_timeout(&completion,
0318 msecs_to_jiffies(timeout_ms));
0319
0320 if (timeleft == 0) {
0321
0322
0323
0324 isci_remote_device_suspend_terminate(ihost, idev, ireq);
0325 }
0326
0327 isci_print_tmf(ihost, tmf);
0328
0329 if (tmf->status == SCI_SUCCESS)
0330 ret = TMF_RESP_FUNC_COMPLETE;
0331 else if (tmf->status == SCI_FAILURE_IO_RESPONSE_VALID) {
0332 dev_dbg(&ihost->pdev->dev,
0333 "%s: tmf.status == "
0334 "SCI_FAILURE_IO_RESPONSE_VALID\n",
0335 __func__);
0336 ret = TMF_RESP_FUNC_COMPLETE;
0337 }
0338
0339
0340 dev_dbg(&ihost->pdev->dev,
0341 "%s: completed request = %p\n",
0342 __func__,
0343 ireq);
0344
0345 return ret;
0346
0347 err_tci:
0348 spin_lock_irqsave(&ihost->scic_lock, flags);
0349 isci_tci_free(ihost, ISCI_TAG_TCI(tag));
0350 spin_unlock_irqrestore(&ihost->scic_lock, flags);
0351
0352 return ret;
0353 }
0354
0355 static void isci_task_build_tmf(struct isci_tmf *tmf,
0356 enum isci_tmf_function_codes code)
0357 {
0358 memset(tmf, 0, sizeof(*tmf));
0359 tmf->tmf_code = code;
0360 }
0361
0362 static void isci_task_build_abort_task_tmf(struct isci_tmf *tmf,
0363 enum isci_tmf_function_codes code,
0364 struct isci_request *old_request)
0365 {
0366 isci_task_build_tmf(tmf, code);
0367 tmf->io_tag = old_request->io_tag;
0368 }
0369
0370
0371
0372
0373
0374
0375
0376
0377 static int isci_task_send_lu_reset_sas(
0378 struct isci_host *isci_host,
0379 struct isci_remote_device *isci_device,
0380 u8 *lun)
0381 {
0382 struct isci_tmf tmf;
0383 int ret = TMF_RESP_FUNC_FAILED;
0384
0385 dev_dbg(&isci_host->pdev->dev,
0386 "%s: isci_host = %p, isci_device = %p\n",
0387 __func__, isci_host, isci_device);
0388
0389
0390
0391
0392
0393 isci_task_build_tmf(&tmf, isci_tmf_ssp_lun_reset);
0394
0395 #define ISCI_LU_RESET_TIMEOUT_MS 2000
0396 ret = isci_task_execute_tmf(isci_host, isci_device, &tmf, ISCI_LU_RESET_TIMEOUT_MS);
0397
0398 if (ret == TMF_RESP_FUNC_COMPLETE)
0399 dev_dbg(&isci_host->pdev->dev,
0400 "%s: %p: TMF_LU_RESET passed\n",
0401 __func__, isci_device);
0402 else
0403 dev_dbg(&isci_host->pdev->dev,
0404 "%s: %p: TMF_LU_RESET failed (%x)\n",
0405 __func__, isci_device, ret);
0406
0407 return ret;
0408 }
0409
0410 int isci_task_lu_reset(struct domain_device *dev, u8 *lun)
0411 {
0412 struct isci_host *ihost = dev_to_ihost(dev);
0413 struct isci_remote_device *idev;
0414 unsigned long flags;
0415 int ret = TMF_RESP_FUNC_COMPLETE;
0416
0417 spin_lock_irqsave(&ihost->scic_lock, flags);
0418 idev = isci_get_device(dev->lldd_dev);
0419 spin_unlock_irqrestore(&ihost->scic_lock, flags);
0420
0421 dev_dbg(&ihost->pdev->dev,
0422 "%s: domain_device=%p, isci_host=%p; isci_device=%p\n",
0423 __func__, dev, ihost, idev);
0424
0425 if (!idev) {
0426
0427 dev_dbg(&ihost->pdev->dev, "%s: No dev\n", __func__);
0428
0429 ret = TMF_RESP_FUNC_FAILED;
0430 goto out;
0431 }
0432
0433
0434 if (isci_remote_device_suspend_terminate(ihost, idev, NULL)
0435 != SCI_SUCCESS) {
0436
0437 ret = TMF_RESP_FUNC_FAILED;
0438 goto out;
0439 }
0440
0441 if (!test_bit(IDEV_GONE, &idev->flags)) {
0442 if (dev_is_sata(dev))
0443 sas_ata_schedule_reset(dev);
0444 else
0445
0446 ret = isci_task_send_lu_reset_sas(ihost, idev, lun);
0447 }
0448 out:
0449 isci_put_device(idev);
0450 return ret;
0451 }
0452
0453
0454
0455 int isci_task_clear_nexus_port(struct asd_sas_port *port)
0456 {
0457 return TMF_RESP_FUNC_FAILED;
0458 }
0459
0460
0461
0462 int isci_task_clear_nexus_ha(struct sas_ha_struct *ha)
0463 {
0464 return TMF_RESP_FUNC_FAILED;
0465 }
0466
0467
0468
0469
0470
0471
0472
0473
0474
0475
0476 int isci_task_abort_task(struct sas_task *task)
0477 {
0478 struct isci_host *ihost = dev_to_ihost(task->dev);
0479 DECLARE_COMPLETION_ONSTACK(aborted_io_completion);
0480 struct isci_request *old_request = NULL;
0481 struct isci_remote_device *idev = NULL;
0482 struct isci_tmf tmf;
0483 int ret = TMF_RESP_FUNC_FAILED;
0484 unsigned long flags;
0485 int target_done_already = 0;
0486
0487
0488
0489
0490
0491
0492 spin_lock_irqsave(&ihost->scic_lock, flags);
0493 spin_lock(&task->task_state_lock);
0494
0495 old_request = task->lldd_task;
0496
0497
0498 if (!(task->task_state_flags & SAS_TASK_STATE_DONE) &&
0499 old_request) {
0500 idev = isci_get_device(task->dev->lldd_dev);
0501 target_done_already = test_bit(IREQ_COMPLETE_IN_TARGET,
0502 &old_request->flags);
0503 }
0504 spin_unlock(&task->task_state_lock);
0505 spin_unlock_irqrestore(&ihost->scic_lock, flags);
0506
0507 dev_warn(&ihost->pdev->dev,
0508 "%s: dev = %p (%s%s), task = %p, old_request == %p\n",
0509 __func__, idev,
0510 (dev_is_sata(task->dev) ? "STP/SATA"
0511 : ((dev_is_expander(task->dev->dev_type))
0512 ? "SMP"
0513 : "SSP")),
0514 ((idev) ? ((test_bit(IDEV_GONE, &idev->flags))
0515 ? " IDEV_GONE"
0516 : "")
0517 : " <NULL>"),
0518 task, old_request);
0519
0520
0521
0522
0523
0524 if (!idev || !old_request) {
0525
0526
0527
0528
0529
0530 spin_lock_irqsave(&task->task_state_lock, flags);
0531 task->task_state_flags |= SAS_TASK_STATE_DONE;
0532 task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
0533 spin_unlock_irqrestore(&task->task_state_lock, flags);
0534
0535 ret = TMF_RESP_FUNC_COMPLETE;
0536
0537 dev_warn(&ihost->pdev->dev,
0538 "%s: abort task not needed for %p\n",
0539 __func__, task);
0540 goto out;
0541 }
0542
0543 if (isci_remote_device_suspend_terminate(ihost, idev, old_request)
0544 != SCI_SUCCESS) {
0545 dev_warn(&ihost->pdev->dev,
0546 "%s: isci_remote_device_reset_terminate(dev=%p, "
0547 "req=%p, task=%p) failed\n",
0548 __func__, idev, old_request, task);
0549 ret = TMF_RESP_FUNC_FAILED;
0550 goto out;
0551 }
0552 spin_lock_irqsave(&ihost->scic_lock, flags);
0553
0554 if (task->task_proto == SAS_PROTOCOL_SMP ||
0555 sas_protocol_ata(task->task_proto) ||
0556 target_done_already ||
0557 test_bit(IDEV_GONE, &idev->flags)) {
0558
0559 spin_unlock_irqrestore(&ihost->scic_lock, flags);
0560
0561
0562 isci_remote_device_resume_from_abort(ihost, idev);
0563
0564 dev_warn(&ihost->pdev->dev,
0565 "%s: %s request"
0566 " or complete_in_target (%d), "
0567 "or IDEV_GONE (%d), thus no TMF\n",
0568 __func__,
0569 ((task->task_proto == SAS_PROTOCOL_SMP)
0570 ? "SMP"
0571 : (sas_protocol_ata(task->task_proto)
0572 ? "SATA/STP"
0573 : "<other>")
0574 ),
0575 test_bit(IREQ_COMPLETE_IN_TARGET,
0576 &old_request->flags),
0577 test_bit(IDEV_GONE, &idev->flags));
0578
0579 spin_lock_irqsave(&task->task_state_lock, flags);
0580 task->task_state_flags &= ~SAS_TASK_STATE_PENDING;
0581 task->task_state_flags |= SAS_TASK_STATE_DONE;
0582 spin_unlock_irqrestore(&task->task_state_lock, flags);
0583
0584 ret = TMF_RESP_FUNC_COMPLETE;
0585 } else {
0586
0587 isci_task_build_abort_task_tmf(&tmf, isci_tmf_ssp_task_abort,
0588 old_request);
0589
0590 spin_unlock_irqrestore(&ihost->scic_lock, flags);
0591
0592
0593 #define ISCI_ABORT_TASK_TIMEOUT_MS 500
0594 ret = isci_task_execute_tmf(ihost, idev, &tmf,
0595 ISCI_ABORT_TASK_TIMEOUT_MS);
0596 }
0597 out:
0598 dev_warn(&ihost->pdev->dev,
0599 "%s: Done; dev = %p, task = %p , old_request == %p\n",
0600 __func__, idev, task, old_request);
0601 isci_put_device(idev);
0602 return ret;
0603 }
0604
0605
0606
0607
0608
0609
0610
0611
0612
0613
0614
0615 int isci_task_abort_task_set(
0616 struct domain_device *d_device,
0617 u8 *lun)
0618 {
0619 return TMF_RESP_FUNC_FAILED;
0620 }
0621
0622
0623
0624
0625
0626
0627
0628
0629
0630
0631
0632 int isci_task_clear_task_set(
0633 struct domain_device *d_device,
0634 u8 *lun)
0635 {
0636 return TMF_RESP_FUNC_FAILED;
0637 }
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649
0650
0651 int isci_task_query_task(
0652 struct sas_task *task)
0653 {
0654
0655 if (task->task_state_flags & SAS_TASK_NEED_DEV_RESET)
0656 return TMF_RESP_FUNC_FAILED;
0657 else
0658 return TMF_RESP_FUNC_SUCC;
0659 }
0660
0661
0662
0663
0664
0665
0666
0667
0668
0669
0670
0671 void
0672 isci_task_request_complete(struct isci_host *ihost,
0673 struct isci_request *ireq,
0674 enum sci_task_status completion_status)
0675 {
0676 struct isci_tmf *tmf = isci_request_access_tmf(ireq);
0677 struct completion *tmf_complete = NULL;
0678
0679 dev_dbg(&ihost->pdev->dev,
0680 "%s: request = %p, status=%d\n",
0681 __func__, ireq, completion_status);
0682
0683 set_bit(IREQ_COMPLETE_IN_TARGET, &ireq->flags);
0684
0685 if (tmf) {
0686 tmf->status = completion_status;
0687
0688 if (tmf->proto == SAS_PROTOCOL_SSP) {
0689 memcpy(tmf->resp.rsp_buf,
0690 ireq->ssp.rsp_buf,
0691 SSP_RESP_IU_MAX_SIZE);
0692 } else if (tmf->proto == SAS_PROTOCOL_SATA) {
0693 memcpy(&tmf->resp.d2h_fis,
0694 &ireq->stp.rsp,
0695 sizeof(struct dev_to_host_fis));
0696 }
0697
0698 tmf_complete = tmf->complete;
0699 }
0700 sci_controller_complete_io(ihost, ireq->target_device, ireq);
0701
0702
0703
0704 set_bit(IREQ_TERMINATED, &ireq->flags);
0705
0706 if (test_and_clear_bit(IREQ_ABORT_PATH_ACTIVE, &ireq->flags))
0707 wake_up_all(&ihost->eventq);
0708
0709 if (!test_bit(IREQ_NO_AUTO_FREE_TAG, &ireq->flags))
0710 isci_free_tag(ihost, ireq->io_tag);
0711
0712
0713 if (tmf_complete)
0714 complete(tmf_complete);
0715 }
0716
0717 static int isci_reset_device(struct isci_host *ihost,
0718 struct domain_device *dev,
0719 struct isci_remote_device *idev)
0720 {
0721 int rc = TMF_RESP_FUNC_COMPLETE, reset_stat = -1;
0722 struct sas_phy *phy = sas_get_local_phy(dev);
0723 struct isci_port *iport = dev->port->lldd_port;
0724
0725 dev_dbg(&ihost->pdev->dev, "%s: idev %p\n", __func__, idev);
0726
0727
0728 if (isci_remote_device_suspend_terminate(ihost, idev, NULL)
0729 != SCI_SUCCESS) {
0730 rc = TMF_RESP_FUNC_FAILED;
0731 goto out;
0732 }
0733
0734
0735
0736
0737
0738
0739 if (!test_bit(IDEV_GONE, &idev->flags)) {
0740 if (scsi_is_sas_phy_local(phy)) {
0741 struct isci_phy *iphy = &ihost->phys[phy->number];
0742
0743 reset_stat = isci_port_perform_hard_reset(ihost, iport,
0744 iphy);
0745 } else
0746 reset_stat = sas_phy_reset(phy, !dev_is_sata(dev));
0747 }
0748
0749 isci_remote_device_resume_from_abort(ihost, idev);
0750
0751 dev_dbg(&ihost->pdev->dev, "%s: idev %p complete, reset_stat=%d.\n",
0752 __func__, idev, reset_stat);
0753 out:
0754 sas_put_local_phy(phy);
0755 return rc;
0756 }
0757
0758 int isci_task_I_T_nexus_reset(struct domain_device *dev)
0759 {
0760 struct isci_host *ihost = dev_to_ihost(dev);
0761 struct isci_remote_device *idev;
0762 unsigned long flags;
0763 int ret;
0764
0765 spin_lock_irqsave(&ihost->scic_lock, flags);
0766 idev = isci_get_device(dev->lldd_dev);
0767 spin_unlock_irqrestore(&ihost->scic_lock, flags);
0768
0769 if (!idev) {
0770
0771
0772
0773 ret = -ENODEV;
0774 goto out;
0775 }
0776
0777 ret = isci_reset_device(ihost, dev, idev);
0778 out:
0779 isci_put_device(idev);
0780 return ret;
0781 }