diff options
author | James Bottomley <James.Bottomley@steeleye.com> | 2007-07-22 14:15:55 -0400 |
---|---|---|
committer | James Bottomley <jejb@mulgrave.localdomain> | 2007-07-22 14:23:13 -0400 |
commit | b91421749a1840148d8c81637c03c0ace3f35269 (patch) | |
tree | e3e2f59f9a14051c8277793f214db094380d3ebb /drivers/scsi/libsas/sas_ata.c | |
parent | 41e1703b9b88cf9b5e91cdd2f7dcded3ec3917cb (diff) |
[SCSI] libsas: make ATA functions selectable by a config option
Not everyone wants libsas automatically to pull in libata. This patch
makes the behaviour configurable, so you can build libsas with or
without ATA support.
Signed-off-by: James Bottomley <James.Bottomley@SteelEye.com>
Diffstat (limited to 'drivers/scsi/libsas/sas_ata.c')
-rw-r--r-- | drivers/scsi/libsas/sas_ata.c | 397 |
1 files changed, 397 insertions, 0 deletions
diff --git a/drivers/scsi/libsas/sas_ata.c b/drivers/scsi/libsas/sas_ata.c index 359391f5735f..ced2de32c511 100644 --- a/drivers/scsi/libsas/sas_ata.c +++ b/drivers/scsi/libsas/sas_ata.c | |||
@@ -21,6 +21,8 @@ | |||
21 | * USA | 21 | * USA |
22 | */ | 22 | */ |
23 | 23 | ||
24 | #include <linux/scatterlist.h> | ||
25 | |||
24 | #include <scsi/sas_ata.h> | 26 | #include <scsi/sas_ata.h> |
25 | #include "sas_internal.h" | 27 | #include "sas_internal.h" |
26 | #include <scsi/scsi_host.h> | 28 | #include <scsi/scsi_host.h> |
@@ -418,3 +420,398 @@ void sas_ata_task_abort(struct sas_task *task) | |||
418 | waiting = qc->private_data; | 420 | waiting = qc->private_data; |
419 | complete(waiting); | 421 | complete(waiting); |
420 | } | 422 | } |
423 | |||
424 | static void sas_task_timedout(unsigned long _task) | ||
425 | { | ||
426 | struct sas_task *task = (void *) _task; | ||
427 | unsigned long flags; | ||
428 | |||
429 | spin_lock_irqsave(&task->task_state_lock, flags); | ||
430 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) | ||
431 | task->task_state_flags |= SAS_TASK_STATE_ABORTED; | ||
432 | spin_unlock_irqrestore(&task->task_state_lock, flags); | ||
433 | |||
434 | complete(&task->completion); | ||
435 | } | ||
436 | |||
437 | static void sas_disc_task_done(struct sas_task *task) | ||
438 | { | ||
439 | if (!del_timer(&task->timer)) | ||
440 | return; | ||
441 | complete(&task->completion); | ||
442 | } | ||
443 | |||
444 | #define SAS_DEV_TIMEOUT 10 | ||
445 | |||
446 | /** | ||
447 | * sas_execute_task -- Basic task processing for discovery | ||
448 | * @task: the task to be executed | ||
449 | * @buffer: pointer to buffer to do I/O | ||
450 | * @size: size of @buffer | ||
451 | * @pci_dma_dir: PCI_DMA_... | ||
452 | */ | ||
453 | static int sas_execute_task(struct sas_task *task, void *buffer, int size, | ||
454 | int pci_dma_dir) | ||
455 | { | ||
456 | int res = 0; | ||
457 | struct scatterlist *scatter = NULL; | ||
458 | struct task_status_struct *ts = &task->task_status; | ||
459 | int num_scatter = 0; | ||
460 | int retries = 0; | ||
461 | struct sas_internal *i = | ||
462 | to_sas_internal(task->dev->port->ha->core.shost->transportt); | ||
463 | |||
464 | if (pci_dma_dir != PCI_DMA_NONE) { | ||
465 | scatter = kzalloc(sizeof(*scatter), GFP_KERNEL); | ||
466 | if (!scatter) | ||
467 | goto out; | ||
468 | |||
469 | sg_init_one(scatter, buffer, size); | ||
470 | num_scatter = 1; | ||
471 | } | ||
472 | |||
473 | task->task_proto = task->dev->tproto; | ||
474 | task->scatter = scatter; | ||
475 | task->num_scatter = num_scatter; | ||
476 | task->total_xfer_len = size; | ||
477 | task->data_dir = pci_dma_dir; | ||
478 | task->task_done = sas_disc_task_done; | ||
479 | if (pci_dma_dir != PCI_DMA_NONE && | ||
480 | sas_protocol_ata(task->task_proto)) { | ||
481 | task->num_scatter = pci_map_sg(task->dev->port->ha->pcidev, | ||
482 | task->scatter, | ||
483 | task->num_scatter, | ||
484 | task->data_dir); | ||
485 | } | ||
486 | |||
487 | for (retries = 0; retries < 5; retries++) { | ||
488 | task->task_state_flags = SAS_TASK_STATE_PENDING; | ||
489 | init_completion(&task->completion); | ||
490 | |||
491 | task->timer.data = (unsigned long) task; | ||
492 | task->timer.function = sas_task_timedout; | ||
493 | task->timer.expires = jiffies + SAS_DEV_TIMEOUT*HZ; | ||
494 | add_timer(&task->timer); | ||
495 | |||
496 | res = i->dft->lldd_execute_task(task, 1, GFP_KERNEL); | ||
497 | if (res) { | ||
498 | del_timer(&task->timer); | ||
499 | SAS_DPRINTK("executing SAS discovery task failed:%d\n", | ||
500 | res); | ||
501 | goto ex_err; | ||
502 | } | ||
503 | wait_for_completion(&task->completion); | ||
504 | res = -ETASK; | ||
505 | if (task->task_state_flags & SAS_TASK_STATE_ABORTED) { | ||
506 | int res2; | ||
507 | SAS_DPRINTK("task aborted, flags:0x%x\n", | ||
508 | task->task_state_flags); | ||
509 | res2 = i->dft->lldd_abort_task(task); | ||
510 | SAS_DPRINTK("came back from abort task\n"); | ||
511 | if (!(task->task_state_flags & SAS_TASK_STATE_DONE)) { | ||
512 | if (res2 == TMF_RESP_FUNC_COMPLETE) | ||
513 | continue; /* Retry the task */ | ||
514 | else | ||
515 | goto ex_err; | ||
516 | } | ||
517 | } | ||
518 | if (task->task_status.stat == SAM_BUSY || | ||
519 | task->task_status.stat == SAM_TASK_SET_FULL || | ||
520 | task->task_status.stat == SAS_QUEUE_FULL) { | ||
521 | SAS_DPRINTK("task: q busy, sleeping...\n"); | ||
522 | schedule_timeout_interruptible(HZ); | ||
523 | } else if (task->task_status.stat == SAM_CHECK_COND) { | ||
524 | struct scsi_sense_hdr shdr; | ||
525 | |||
526 | if (!scsi_normalize_sense(ts->buf, ts->buf_valid_size, | ||
527 | &shdr)) { | ||
528 | SAS_DPRINTK("couldn't normalize sense\n"); | ||
529 | continue; | ||
530 | } | ||
531 | if ((shdr.sense_key == 6 && shdr.asc == 0x29) || | ||
532 | (shdr.sense_key == 2 && shdr.asc == 4 && | ||
533 | shdr.ascq == 1)) { | ||
534 | SAS_DPRINTK("device %016llx LUN: %016llx " | ||
535 | "powering up or not ready yet, " | ||
536 | "sleeping...\n", | ||
537 | SAS_ADDR(task->dev->sas_addr), | ||
538 | SAS_ADDR(task->ssp_task.LUN)); | ||
539 | |||
540 | schedule_timeout_interruptible(5*HZ); | ||
541 | } else if (shdr.sense_key == 1) { | ||
542 | res = 0; | ||
543 | break; | ||
544 | } else if (shdr.sense_key == 5) { | ||
545 | break; | ||
546 | } else { | ||
547 | SAS_DPRINTK("dev %016llx LUN: %016llx " | ||
548 | "sense key:0x%x ASC:0x%x ASCQ:0x%x" | ||
549 | "\n", | ||
550 | SAS_ADDR(task->dev->sas_addr), | ||
551 | SAS_ADDR(task->ssp_task.LUN), | ||
552 | shdr.sense_key, | ||
553 | shdr.asc, shdr.ascq); | ||
554 | } | ||
555 | } else if (task->task_status.resp != SAS_TASK_COMPLETE || | ||
556 | task->task_status.stat != SAM_GOOD) { | ||
557 | SAS_DPRINTK("task finished with resp:0x%x, " | ||
558 | "stat:0x%x\n", | ||
559 | task->task_status.resp, | ||
560 | task->task_status.stat); | ||
561 | goto ex_err; | ||
562 | } else { | ||
563 | res = 0; | ||
564 | break; | ||
565 | } | ||
566 | } | ||
567 | ex_err: | ||
568 | if (pci_dma_dir != PCI_DMA_NONE) { | ||
569 | if (sas_protocol_ata(task->task_proto)) | ||
570 | pci_unmap_sg(task->dev->port->ha->pcidev, | ||
571 | task->scatter, task->num_scatter, | ||
572 | task->data_dir); | ||
573 | kfree(scatter); | ||
574 | } | ||
575 | out: | ||
576 | return res; | ||
577 | } | ||
578 | |||
579 | /* ---------- SATA ---------- */ | ||
580 | |||
581 | static void sas_get_ata_command_set(struct domain_device *dev) | ||
582 | { | ||
583 | struct dev_to_host_fis *fis = | ||
584 | (struct dev_to_host_fis *) dev->frame_rcvd; | ||
585 | |||
586 | if ((fis->sector_count == 1 && /* ATA */ | ||
587 | fis->lbal == 1 && | ||
588 | fis->lbam == 0 && | ||
589 | fis->lbah == 0 && | ||
590 | fis->device == 0) | ||
591 | || | ||
592 | (fis->sector_count == 0 && /* CE-ATA (mATA) */ | ||
593 | fis->lbal == 0 && | ||
594 | fis->lbam == 0xCE && | ||
595 | fis->lbah == 0xAA && | ||
596 | (fis->device & ~0x10) == 0)) | ||
597 | |||
598 | dev->sata_dev.command_set = ATA_COMMAND_SET; | ||
599 | |||
600 | else if ((fis->interrupt_reason == 1 && /* ATAPI */ | ||
601 | fis->lbal == 1 && | ||
602 | fis->byte_count_low == 0x14 && | ||
603 | fis->byte_count_high == 0xEB && | ||
604 | (fis->device & ~0x10) == 0)) | ||
605 | |||
606 | dev->sata_dev.command_set = ATAPI_COMMAND_SET; | ||
607 | |||
608 | else if ((fis->sector_count == 1 && /* SEMB */ | ||
609 | fis->lbal == 1 && | ||
610 | fis->lbam == 0x3C && | ||
611 | fis->lbah == 0xC3 && | ||
612 | fis->device == 0) | ||
613 | || | ||
614 | (fis->interrupt_reason == 1 && /* SATA PM */ | ||
615 | fis->lbal == 1 && | ||
616 | fis->byte_count_low == 0x69 && | ||
617 | fis->byte_count_high == 0x96 && | ||
618 | (fis->device & ~0x10) == 0)) | ||
619 | |||
620 | /* Treat it as a superset? */ | ||
621 | dev->sata_dev.command_set = ATAPI_COMMAND_SET; | ||
622 | } | ||
623 | |||
624 | /** | ||
625 | * sas_issue_ata_cmd -- Basic SATA command processing for discovery | ||
626 | * @dev: the device to send the command to | ||
627 | * @command: the command register | ||
628 | * @features: the features register | ||
629 | * @buffer: pointer to buffer to do I/O | ||
630 | * @size: size of @buffer | ||
631 | * @pci_dma_dir: PCI_DMA_... | ||
632 | */ | ||
633 | static int sas_issue_ata_cmd(struct domain_device *dev, u8 command, | ||
634 | u8 features, void *buffer, int size, | ||
635 | int pci_dma_dir) | ||
636 | { | ||
637 | int res = 0; | ||
638 | struct sas_task *task; | ||
639 | struct dev_to_host_fis *d2h_fis = (struct dev_to_host_fis *) | ||
640 | &dev->frame_rcvd[0]; | ||
641 | |||
642 | res = -ENOMEM; | ||
643 | task = sas_alloc_task(GFP_KERNEL); | ||
644 | if (!task) | ||
645 | goto out; | ||
646 | |||
647 | task->dev = dev; | ||
648 | |||
649 | task->ata_task.fis.fis_type = 0x27; | ||
650 | task->ata_task.fis.command = command; | ||
651 | task->ata_task.fis.features = features; | ||
652 | task->ata_task.fis.device = d2h_fis->device; | ||
653 | task->ata_task.retry_count = 1; | ||
654 | |||
655 | res = sas_execute_task(task, buffer, size, pci_dma_dir); | ||
656 | |||
657 | sas_free_task(task); | ||
658 | out: | ||
659 | return res; | ||
660 | } | ||
661 | |||
662 | static void sas_sata_propagate_sas_addr(struct domain_device *dev) | ||
663 | { | ||
664 | unsigned long flags; | ||
665 | struct asd_sas_port *port = dev->port; | ||
666 | struct asd_sas_phy *phy; | ||
667 | |||
668 | BUG_ON(dev->parent); | ||
669 | |||
670 | memcpy(port->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE); | ||
671 | spin_lock_irqsave(&port->phy_list_lock, flags); | ||
672 | list_for_each_entry(phy, &port->phy_list, port_phy_el) | ||
673 | memcpy(phy->attached_sas_addr, dev->sas_addr, SAS_ADDR_SIZE); | ||
674 | spin_unlock_irqrestore(&port->phy_list_lock, flags); | ||
675 | } | ||
676 | |||
677 | #define ATA_IDENTIFY_DEV 0xEC | ||
678 | #define ATA_IDENTIFY_PACKET_DEV 0xA1 | ||
679 | #define ATA_SET_FEATURES 0xEF | ||
680 | #define ATA_FEATURE_PUP_STBY_SPIN_UP 0x07 | ||
681 | |||
682 | /** | ||
683 | * sas_discover_sata_dev -- discover a STP/SATA device (SATA_DEV) | ||
684 | * @dev: STP/SATA device of interest (ATA/ATAPI) | ||
685 | * | ||
686 | * The LLDD has already been notified of this device, so that we can | ||
687 | * send FISes to it. Here we try to get IDENTIFY DEVICE or IDENTIFY | ||
688 | * PACKET DEVICE, if ATAPI device, so that the LLDD can fine-tune its | ||
689 | * performance for this device. | ||
690 | */ | ||
691 | static int sas_discover_sata_dev(struct domain_device *dev) | ||
692 | { | ||
693 | int res; | ||
694 | __le16 *identify_x; | ||
695 | u8 command; | ||
696 | |||
697 | identify_x = kzalloc(512, GFP_KERNEL); | ||
698 | if (!identify_x) | ||
699 | return -ENOMEM; | ||
700 | |||
701 | if (dev->sata_dev.command_set == ATA_COMMAND_SET) { | ||
702 | dev->sata_dev.identify_device = identify_x; | ||
703 | command = ATA_IDENTIFY_DEV; | ||
704 | } else { | ||
705 | dev->sata_dev.identify_packet_device = identify_x; | ||
706 | command = ATA_IDENTIFY_PACKET_DEV; | ||
707 | } | ||
708 | |||
709 | res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512, | ||
710 | PCI_DMA_FROMDEVICE); | ||
711 | if (res) | ||
712 | goto out_err; | ||
713 | |||
714 | /* lives on the media? */ | ||
715 | if (le16_to_cpu(identify_x[0]) & 4) { | ||
716 | /* incomplete response */ | ||
717 | SAS_DPRINTK("sending SET FEATURE/PUP_STBY_SPIN_UP to " | ||
718 | "dev %llx\n", SAS_ADDR(dev->sas_addr)); | ||
719 | if (!le16_to_cpu(identify_x[83] & (1<<6))) | ||
720 | goto cont1; | ||
721 | res = sas_issue_ata_cmd(dev, ATA_SET_FEATURES, | ||
722 | ATA_FEATURE_PUP_STBY_SPIN_UP, | ||
723 | NULL, 0, PCI_DMA_NONE); | ||
724 | if (res) | ||
725 | goto cont1; | ||
726 | |||
727 | schedule_timeout_interruptible(5*HZ); /* More time? */ | ||
728 | res = sas_issue_ata_cmd(dev, command, 0, identify_x, 512, | ||
729 | PCI_DMA_FROMDEVICE); | ||
730 | if (res) | ||
731 | goto out_err; | ||
732 | } | ||
733 | cont1: | ||
734 | /* Get WWN */ | ||
735 | if (dev->port->oob_mode != SATA_OOB_MODE) { | ||
736 | memcpy(dev->sas_addr, dev->sata_dev.rps_resp.rps.stp_sas_addr, | ||
737 | SAS_ADDR_SIZE); | ||
738 | } else if (dev->sata_dev.command_set == ATA_COMMAND_SET && | ||
739 | (le16_to_cpu(dev->sata_dev.identify_device[108]) & 0xF000) | ||
740 | == 0x5000) { | ||
741 | int i; | ||
742 | |||
743 | for (i = 0; i < 4; i++) { | ||
744 | dev->sas_addr[2*i] = | ||
745 | (le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0xFF00) >> 8; | ||
746 | dev->sas_addr[2*i+1] = | ||
747 | le16_to_cpu(dev->sata_dev.identify_device[108+i]) & 0x00FF; | ||
748 | } | ||
749 | } | ||
750 | sas_hash_addr(dev->hashed_sas_addr, dev->sas_addr); | ||
751 | if (!dev->parent) | ||
752 | sas_sata_propagate_sas_addr(dev); | ||
753 | |||
754 | /* XXX Hint: register this SATA device with SATL. | ||
755 | When this returns, dev->sata_dev->lu is alive and | ||
756 | present. | ||
757 | sas_satl_register_dev(dev); | ||
758 | */ | ||
759 | |||
760 | sas_fill_in_rphy(dev, dev->rphy); | ||
761 | |||
762 | return 0; | ||
763 | out_err: | ||
764 | dev->sata_dev.identify_packet_device = NULL; | ||
765 | dev->sata_dev.identify_device = NULL; | ||
766 | kfree(identify_x); | ||
767 | return res; | ||
768 | } | ||
769 | |||
770 | static int sas_discover_sata_pm(struct domain_device *dev) | ||
771 | { | ||
772 | return -ENODEV; | ||
773 | } | ||
774 | |||
775 | /** | ||
776 | * sas_discover_sata -- discover an STP/SATA domain device | ||
777 | * @dev: pointer to struct domain_device of interest | ||
778 | * | ||
779 | * First we notify the LLDD of this device, so we can send frames to | ||
780 | * it. Then depending on the type of device we call the appropriate | ||
781 | * discover functions. Once device discover is done, we notify the | ||
782 | * LLDD so that it can fine-tune its parameters for the device, by | ||
783 | * removing it and then adding it. That is, the second time around, | ||
784 | * the driver would have certain fields, that it is looking at, set. | ||
785 | * Finally we initialize the kobj so that the device can be added to | ||
786 | * the system at registration time. Devices directly attached to a HA | ||
787 | * port, have no parents. All other devices do, and should have their | ||
788 | * "parent" pointer set appropriately before calling this function. | ||
789 | */ | ||
790 | int sas_discover_sata(struct domain_device *dev) | ||
791 | { | ||
792 | int res; | ||
793 | |||
794 | sas_get_ata_command_set(dev); | ||
795 | |||
796 | res = sas_notify_lldd_dev_found(dev); | ||
797 | if (res) | ||
798 | return res; | ||
799 | |||
800 | switch (dev->dev_type) { | ||
801 | case SATA_DEV: | ||
802 | res = sas_discover_sata_dev(dev); | ||
803 | break; | ||
804 | case SATA_PM: | ||
805 | res = sas_discover_sata_pm(dev); | ||
806 | break; | ||
807 | default: | ||
808 | break; | ||
809 | } | ||
810 | sas_notify_lldd_dev_gone(dev); | ||
811 | if (!res) { | ||
812 | sas_notify_lldd_dev_found(dev); | ||
813 | res = sas_rphy_add(dev->rphy); | ||
814 | } | ||
815 | |||
816 | return res; | ||
817 | } | ||