aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/firewire/fw-sbp2.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/firewire/fw-sbp2.c')
-rw-r--r--drivers/firewire/fw-sbp2.c428
1 files changed, 335 insertions, 93 deletions
diff --git a/drivers/firewire/fw-sbp2.c b/drivers/firewire/fw-sbp2.c
index 19ece9b6d742..62b4e47d0cc0 100644
--- a/drivers/firewire/fw-sbp2.c
+++ b/drivers/firewire/fw-sbp2.c
@@ -28,14 +28,15 @@
28 * and many others. 28 * and many others.
29 */ 29 */
30 30
31#include <linux/blkdev.h>
32#include <linux/delay.h>
33#include <linux/device.h>
34#include <linux/dma-mapping.h>
31#include <linux/kernel.h> 35#include <linux/kernel.h>
36#include <linux/mod_devicetable.h>
32#include <linux/module.h> 37#include <linux/module.h>
33#include <linux/moduleparam.h> 38#include <linux/moduleparam.h>
34#include <linux/mod_devicetable.h>
35#include <linux/device.h>
36#include <linux/scatterlist.h> 39#include <linux/scatterlist.h>
37#include <linux/dma-mapping.h>
38#include <linux/blkdev.h>
39#include <linux/string.h> 40#include <linux/string.h>
40#include <linux/stringify.h> 41#include <linux/stringify.h>
41#include <linux/timer.h> 42#include <linux/timer.h>
@@ -47,9 +48,9 @@
47#include <scsi/scsi_device.h> 48#include <scsi/scsi_device.h>
48#include <scsi/scsi_host.h> 49#include <scsi/scsi_host.h>
49 50
50#include "fw-transaction.h"
51#include "fw-topology.h"
52#include "fw-device.h" 51#include "fw-device.h"
52#include "fw-topology.h"
53#include "fw-transaction.h"
53 54
54/* 55/*
55 * So far only bridges from Oxford Semiconductor are known to support 56 * So far only bridges from Oxford Semiconductor are known to support
@@ -82,6 +83,9 @@ MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
82 * Avoids access beyond actual disk limits on devices with an off-by-one bug. 83 * Avoids access beyond actual disk limits on devices with an off-by-one bug.
83 * Don't use this with devices which don't have this bug. 84 * Don't use this with devices which don't have this bug.
84 * 85 *
86 * - delay inquiry
87 * Wait extra SBP2_INQUIRY_DELAY seconds after login before SCSI inquiry.
88 *
85 * - override internal blacklist 89 * - override internal blacklist
86 * Instead of adding to the built-in blacklist, use only the workarounds 90 * Instead of adding to the built-in blacklist, use only the workarounds
87 * specified in the module load parameter. 91 * specified in the module load parameter.
@@ -91,6 +95,8 @@ MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
91#define SBP2_WORKAROUND_INQUIRY_36 0x2 95#define SBP2_WORKAROUND_INQUIRY_36 0x2
92#define SBP2_WORKAROUND_MODE_SENSE_8 0x4 96#define SBP2_WORKAROUND_MODE_SENSE_8 0x4
93#define SBP2_WORKAROUND_FIX_CAPACITY 0x8 97#define SBP2_WORKAROUND_FIX_CAPACITY 0x8
98#define SBP2_WORKAROUND_DELAY_INQUIRY 0x10
99#define SBP2_INQUIRY_DELAY 12
94#define SBP2_WORKAROUND_OVERRIDE 0x100 100#define SBP2_WORKAROUND_OVERRIDE 0x100
95 101
96static int sbp2_param_workarounds; 102static int sbp2_param_workarounds;
@@ -100,6 +106,7 @@ MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
100 ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36) 106 ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
101 ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8) 107 ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
102 ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY) 108 ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
109 ", delay inquiry = " __stringify(SBP2_WORKAROUND_DELAY_INQUIRY)
103 ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE) 110 ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
104 ", or a combination)"); 111 ", or a combination)");
105 112
@@ -115,7 +122,6 @@ static const char sbp2_driver_name[] = "sbp2";
115struct sbp2_logical_unit { 122struct sbp2_logical_unit {
116 struct sbp2_target *tgt; 123 struct sbp2_target *tgt;
117 struct list_head link; 124 struct list_head link;
118 struct scsi_device *sdev;
119 struct fw_address_handler address_handler; 125 struct fw_address_handler address_handler;
120 struct list_head orb_list; 126 struct list_head orb_list;
121 127
@@ -132,6 +138,8 @@ struct sbp2_logical_unit {
132 int generation; 138 int generation;
133 int retries; 139 int retries;
134 struct delayed_work work; 140 struct delayed_work work;
141 bool has_sdev;
142 bool blocked;
135}; 143};
136 144
137/* 145/*
@@ -141,16 +149,18 @@ struct sbp2_logical_unit {
141struct sbp2_target { 149struct sbp2_target {
142 struct kref kref; 150 struct kref kref;
143 struct fw_unit *unit; 151 struct fw_unit *unit;
152 const char *bus_id;
153 struct list_head lu_list;
144 154
145 u64 management_agent_address; 155 u64 management_agent_address;
146 int directory_id; 156 int directory_id;
147 int node_id; 157 int node_id;
148 int address_high; 158 int address_high;
149 159 unsigned int workarounds;
150 unsigned workarounds;
151 struct list_head lu_list;
152
153 unsigned int mgt_orb_timeout; 160 unsigned int mgt_orb_timeout;
161
162 int dont_block; /* counter for each logical unit */
163 int blocked; /* ditto */
154}; 164};
155 165
156/* 166/*
@@ -160,9 +170,10 @@ struct sbp2_target {
160 */ 170 */
161#define SBP2_MIN_LOGIN_ORB_TIMEOUT 5000U /* Timeout in ms */ 171#define SBP2_MIN_LOGIN_ORB_TIMEOUT 5000U /* Timeout in ms */
162#define SBP2_MAX_LOGIN_ORB_TIMEOUT 40000U /* Timeout in ms */ 172#define SBP2_MAX_LOGIN_ORB_TIMEOUT 40000U /* Timeout in ms */
163#define SBP2_ORB_TIMEOUT 2000 /* Timeout in ms */ 173#define SBP2_ORB_TIMEOUT 2000U /* Timeout in ms */
164#define SBP2_ORB_NULL 0x80000000 174#define SBP2_ORB_NULL 0x80000000
165#define SBP2_MAX_SG_ELEMENT_LENGTH 0xf000 175#define SBP2_MAX_SG_ELEMENT_LENGTH 0xf000
176#define SBP2_RETRY_LIMIT 0xf /* 15 retries */
166 177
167#define SBP2_DIRECTION_TO_MEDIA 0x0 178#define SBP2_DIRECTION_TO_MEDIA 0x0
168#define SBP2_DIRECTION_FROM_MEDIA 0x1 179#define SBP2_DIRECTION_FROM_MEDIA 0x1
@@ -297,7 +308,7 @@ struct sbp2_command_orb {
297static const struct { 308static const struct {
298 u32 firmware_revision; 309 u32 firmware_revision;
299 u32 model; 310 u32 model;
300 unsigned workarounds; 311 unsigned int workarounds;
301} sbp2_workarounds_table[] = { 312} sbp2_workarounds_table[] = {
302 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ { 313 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
303 .firmware_revision = 0x002800, 314 .firmware_revision = 0x002800,
@@ -305,6 +316,11 @@ static const struct {
305 .workarounds = SBP2_WORKAROUND_INQUIRY_36 | 316 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
306 SBP2_WORKAROUND_MODE_SENSE_8, 317 SBP2_WORKAROUND_MODE_SENSE_8,
307 }, 318 },
319 /* DViCO Momobay FX-3A with TSB42AA9A bridge */ {
320 .firmware_revision = 0x002800,
321 .model = 0x000000,
322 .workarounds = SBP2_WORKAROUND_DELAY_INQUIRY,
323 },
308 /* Initio bridges, actually only needed for some older ones */ { 324 /* Initio bridges, actually only needed for some older ones */ {
309 .firmware_revision = 0x000200, 325 .firmware_revision = 0x000200,
310 .model = ~0, 326 .model = ~0,
@@ -315,6 +331,11 @@ static const struct {
315 .model = ~0, 331 .model = ~0,
316 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS, 332 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
317 }, 333 },
334 /* Datafab MD2-FW2 with Symbios/LSILogic SYM13FW500 bridge */ {
335 .firmware_revision = 0x002600,
336 .model = ~0,
337 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
338 },
318 339
319 /* 340 /*
320 * There are iPods (2nd gen, 3rd gen) with model_id == 0, but 341 * There are iPods (2nd gen, 3rd gen) with model_id == 0, but
@@ -501,6 +522,9 @@ sbp2_send_management_orb(struct sbp2_logical_unit *lu, int node_id,
501 unsigned int timeout; 522 unsigned int timeout;
502 int retval = -ENOMEM; 523 int retval = -ENOMEM;
503 524
525 if (function == SBP2_LOGOUT_REQUEST && fw_device_is_shutdown(device))
526 return 0;
527
504 orb = kzalloc(sizeof(*orb), GFP_ATOMIC); 528 orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
505 if (orb == NULL) 529 if (orb == NULL)
506 return -ENOMEM; 530 return -ENOMEM;
@@ -553,20 +577,20 @@ sbp2_send_management_orb(struct sbp2_logical_unit *lu, int node_id,
553 577
554 retval = -EIO; 578 retval = -EIO;
555 if (sbp2_cancel_orbs(lu) == 0) { 579 if (sbp2_cancel_orbs(lu) == 0) {
556 fw_error("orb reply timed out, rcode=0x%02x\n", 580 fw_error("%s: orb reply timed out, rcode=0x%02x\n",
557 orb->base.rcode); 581 lu->tgt->bus_id, orb->base.rcode);
558 goto out; 582 goto out;
559 } 583 }
560 584
561 if (orb->base.rcode != RCODE_COMPLETE) { 585 if (orb->base.rcode != RCODE_COMPLETE) {
562 fw_error("management write failed, rcode 0x%02x\n", 586 fw_error("%s: management write failed, rcode 0x%02x\n",
563 orb->base.rcode); 587 lu->tgt->bus_id, orb->base.rcode);
564 goto out; 588 goto out;
565 } 589 }
566 590
567 if (STATUS_GET_RESPONSE(orb->status) != 0 || 591 if (STATUS_GET_RESPONSE(orb->status) != 0 ||
568 STATUS_GET_SBP_STATUS(orb->status) != 0) { 592 STATUS_GET_SBP_STATUS(orb->status) != 0) {
569 fw_error("error status: %d:%d\n", 593 fw_error("%s: error status: %d:%d\n", lu->tgt->bus_id,
570 STATUS_GET_RESPONSE(orb->status), 594 STATUS_GET_RESPONSE(orb->status),
571 STATUS_GET_SBP_STATUS(orb->status)); 595 STATUS_GET_SBP_STATUS(orb->status));
572 goto out; 596 goto out;
@@ -590,29 +614,158 @@ sbp2_send_management_orb(struct sbp2_logical_unit *lu, int node_id,
590 614
591static void 615static void
592complete_agent_reset_write(struct fw_card *card, int rcode, 616complete_agent_reset_write(struct fw_card *card, int rcode,
593 void *payload, size_t length, void *data) 617 void *payload, size_t length, void *done)
594{ 618{
595 struct fw_transaction *t = data; 619 complete(done);
620}
596 621
597 kfree(t); 622static void sbp2_agent_reset(struct sbp2_logical_unit *lu)
623{
624 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
625 DECLARE_COMPLETION_ONSTACK(done);
626 struct fw_transaction t;
627 static u32 z;
628
629 fw_send_request(device->card, &t, TCODE_WRITE_QUADLET_REQUEST,
630 lu->tgt->node_id, lu->generation, device->max_speed,
631 lu->command_block_agent_address + SBP2_AGENT_RESET,
632 &z, sizeof(z), complete_agent_reset_write, &done);
633 wait_for_completion(&done);
598} 634}
599 635
600static int sbp2_agent_reset(struct sbp2_logical_unit *lu) 636static void
637complete_agent_reset_write_no_wait(struct fw_card *card, int rcode,
638 void *payload, size_t length, void *data)
639{
640 kfree(data);
641}
642
643static void sbp2_agent_reset_no_wait(struct sbp2_logical_unit *lu)
601{ 644{
602 struct fw_device *device = fw_device(lu->tgt->unit->device.parent); 645 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
603 struct fw_transaction *t; 646 struct fw_transaction *t;
604 static u32 zero; 647 static u32 z;
605 648
606 t = kzalloc(sizeof(*t), GFP_ATOMIC); 649 t = kmalloc(sizeof(*t), GFP_ATOMIC);
607 if (t == NULL) 650 if (t == NULL)
608 return -ENOMEM; 651 return;
609 652
610 fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST, 653 fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
611 lu->tgt->node_id, lu->generation, device->max_speed, 654 lu->tgt->node_id, lu->generation, device->max_speed,
612 lu->command_block_agent_address + SBP2_AGENT_RESET, 655 lu->command_block_agent_address + SBP2_AGENT_RESET,
613 &zero, sizeof(zero), complete_agent_reset_write, t); 656 &z, sizeof(z), complete_agent_reset_write_no_wait, t);
657}
614 658
615 return 0; 659static void sbp2_set_generation(struct sbp2_logical_unit *lu, int generation)
660{
661 struct fw_card *card = fw_device(lu->tgt->unit->device.parent)->card;
662 unsigned long flags;
663
664 /* serialize with comparisons of lu->generation and card->generation */
665 spin_lock_irqsave(&card->lock, flags);
666 lu->generation = generation;
667 spin_unlock_irqrestore(&card->lock, flags);
668}
669
670static inline void sbp2_allow_block(struct sbp2_logical_unit *lu)
671{
672 /*
673 * We may access dont_block without taking card->lock here:
674 * All callers of sbp2_allow_block() and all callers of sbp2_unblock()
675 * are currently serialized against each other.
676 * And a wrong result in sbp2_conditionally_block()'s access of
677 * dont_block is rather harmless, it simply misses its first chance.
678 */
679 --lu->tgt->dont_block;
680}
681
682/*
683 * Blocks lu->tgt if all of the following conditions are met:
684 * - Login, INQUIRY, and high-level SCSI setup of all of the target's
685 * logical units have been finished (indicated by dont_block == 0).
686 * - lu->generation is stale.
687 *
688 * Note, scsi_block_requests() must be called while holding card->lock,
689 * otherwise it might foil sbp2_[conditionally_]unblock()'s attempt to
690 * unblock the target.
691 */
692static void sbp2_conditionally_block(struct sbp2_logical_unit *lu)
693{
694 struct sbp2_target *tgt = lu->tgt;
695 struct fw_card *card = fw_device(tgt->unit->device.parent)->card;
696 struct Scsi_Host *shost =
697 container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
698 unsigned long flags;
699
700 spin_lock_irqsave(&card->lock, flags);
701 if (!tgt->dont_block && !lu->blocked &&
702 lu->generation != card->generation) {
703 lu->blocked = true;
704 if (++tgt->blocked == 1) {
705 scsi_block_requests(shost);
706 fw_notify("blocked %s\n", lu->tgt->bus_id);
707 }
708 }
709 spin_unlock_irqrestore(&card->lock, flags);
710}
711
712/*
713 * Unblocks lu->tgt as soon as all its logical units can be unblocked.
714 * Note, it is harmless to run scsi_unblock_requests() outside the
715 * card->lock protected section. On the other hand, running it inside
716 * the section might clash with shost->host_lock.
717 */
718static void sbp2_conditionally_unblock(struct sbp2_logical_unit *lu)
719{
720 struct sbp2_target *tgt = lu->tgt;
721 struct fw_card *card = fw_device(tgt->unit->device.parent)->card;
722 struct Scsi_Host *shost =
723 container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
724 unsigned long flags;
725 bool unblock = false;
726
727 spin_lock_irqsave(&card->lock, flags);
728 if (lu->blocked && lu->generation == card->generation) {
729 lu->blocked = false;
730 unblock = --tgt->blocked == 0;
731 }
732 spin_unlock_irqrestore(&card->lock, flags);
733
734 if (unblock) {
735 scsi_unblock_requests(shost);
736 fw_notify("unblocked %s\n", lu->tgt->bus_id);
737 }
738}
739
740/*
741 * Prevents future blocking of tgt and unblocks it.
742 * Note, it is harmless to run scsi_unblock_requests() outside the
743 * card->lock protected section. On the other hand, running it inside
744 * the section might clash with shost->host_lock.
745 */
746static void sbp2_unblock(struct sbp2_target *tgt)
747{
748 struct fw_card *card = fw_device(tgt->unit->device.parent)->card;
749 struct Scsi_Host *shost =
750 container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
751 unsigned long flags;
752
753 spin_lock_irqsave(&card->lock, flags);
754 ++tgt->dont_block;
755 spin_unlock_irqrestore(&card->lock, flags);
756
757 scsi_unblock_requests(shost);
758}
759
760static int sbp2_lun2int(u16 lun)
761{
762 struct scsi_lun eight_bytes_lun;
763
764 memset(&eight_bytes_lun, 0, sizeof(eight_bytes_lun));
765 eight_bytes_lun.scsi_lun[0] = (lun >> 8) & 0xff;
766 eight_bytes_lun.scsi_lun[1] = lun & 0xff;
767
768 return scsilun_to_int(&eight_bytes_lun);
616} 769}
617 770
618static void sbp2_release_target(struct kref *kref) 771static void sbp2_release_target(struct kref *kref)
@@ -621,26 +774,31 @@ static void sbp2_release_target(struct kref *kref)
621 struct sbp2_logical_unit *lu, *next; 774 struct sbp2_logical_unit *lu, *next;
622 struct Scsi_Host *shost = 775 struct Scsi_Host *shost =
623 container_of((void *)tgt, struct Scsi_Host, hostdata[0]); 776 container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
777 struct scsi_device *sdev;
624 struct fw_device *device = fw_device(tgt->unit->device.parent); 778 struct fw_device *device = fw_device(tgt->unit->device.parent);
625 779
626 list_for_each_entry_safe(lu, next, &tgt->lu_list, link) { 780 /* prevent deadlocks */
627 if (lu->sdev) 781 sbp2_unblock(tgt);
628 scsi_remove_device(lu->sdev);
629 782
630 if (!fw_device_is_shutdown(device)) 783 list_for_each_entry_safe(lu, next, &tgt->lu_list, link) {
631 sbp2_send_management_orb(lu, tgt->node_id, 784 sdev = scsi_device_lookup(shost, 0, 0, sbp2_lun2int(lu->lun));
632 lu->generation, SBP2_LOGOUT_REQUEST, 785 if (sdev) {
633 lu->login_id, NULL); 786 scsi_remove_device(sdev);
787 scsi_device_put(sdev);
788 }
789 sbp2_send_management_orb(lu, tgt->node_id, lu->generation,
790 SBP2_LOGOUT_REQUEST, lu->login_id, NULL);
634 791
635 fw_core_remove_address_handler(&lu->address_handler); 792 fw_core_remove_address_handler(&lu->address_handler);
636 list_del(&lu->link); 793 list_del(&lu->link);
637 kfree(lu); 794 kfree(lu);
638 } 795 }
639 scsi_remove_host(shost); 796 scsi_remove_host(shost);
640 fw_notify("released %s\n", tgt->unit->device.bus_id); 797 fw_notify("released %s\n", tgt->bus_id);
641 798
642 put_device(&tgt->unit->device); 799 put_device(&tgt->unit->device);
643 scsi_host_put(shost); 800 scsi_host_put(shost);
801 fw_device_put(device);
644} 802}
645 803
646static struct workqueue_struct *sbp2_wq; 804static struct workqueue_struct *sbp2_wq;
@@ -660,39 +818,72 @@ static void sbp2_target_put(struct sbp2_target *tgt)
660 kref_put(&tgt->kref, sbp2_release_target); 818 kref_put(&tgt->kref, sbp2_release_target);
661} 819}
662 820
821static void
822complete_set_busy_timeout(struct fw_card *card, int rcode,
823 void *payload, size_t length, void *done)
824{
825 complete(done);
826}
827
828static void sbp2_set_busy_timeout(struct sbp2_logical_unit *lu)
829{
830 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
831 DECLARE_COMPLETION_ONSTACK(done);
832 struct fw_transaction t;
833 static __be32 busy_timeout;
834
835 /* FIXME: we should try to set dual-phase cycle_limit too */
836 busy_timeout = cpu_to_be32(SBP2_RETRY_LIMIT);
837
838 fw_send_request(device->card, &t, TCODE_WRITE_QUADLET_REQUEST,
839 lu->tgt->node_id, lu->generation, device->max_speed,
840 CSR_REGISTER_BASE + CSR_BUSY_TIMEOUT, &busy_timeout,
841 sizeof(busy_timeout), complete_set_busy_timeout, &done);
842 wait_for_completion(&done);
843}
844
663static void sbp2_reconnect(struct work_struct *work); 845static void sbp2_reconnect(struct work_struct *work);
664 846
665static void sbp2_login(struct work_struct *work) 847static void sbp2_login(struct work_struct *work)
666{ 848{
667 struct sbp2_logical_unit *lu = 849 struct sbp2_logical_unit *lu =
668 container_of(work, struct sbp2_logical_unit, work.work); 850 container_of(work, struct sbp2_logical_unit, work.work);
669 struct Scsi_Host *shost = 851 struct sbp2_target *tgt = lu->tgt;
670 container_of((void *)lu->tgt, struct Scsi_Host, hostdata[0]); 852 struct fw_device *device = fw_device(tgt->unit->device.parent);
853 struct Scsi_Host *shost;
671 struct scsi_device *sdev; 854 struct scsi_device *sdev;
672 struct scsi_lun eight_bytes_lun;
673 struct fw_unit *unit = lu->tgt->unit;
674 struct fw_device *device = fw_device(unit->device.parent);
675 struct sbp2_login_response response; 855 struct sbp2_login_response response;
676 int generation, node_id, local_node_id; 856 int generation, node_id, local_node_id;
677 857
858 if (fw_device_is_shutdown(device))
859 goto out;
860
678 generation = device->generation; 861 generation = device->generation;
679 smp_rmb(); /* node_id must not be older than generation */ 862 smp_rmb(); /* node_id must not be older than generation */
680 node_id = device->node_id; 863 node_id = device->node_id;
681 local_node_id = device->card->node_id; 864 local_node_id = device->card->node_id;
682 865
866 /* If this is a re-login attempt, log out, or we might be rejected. */
867 if (lu->has_sdev)
868 sbp2_send_management_orb(lu, device->node_id, generation,
869 SBP2_LOGOUT_REQUEST, lu->login_id, NULL);
870
683 if (sbp2_send_management_orb(lu, node_id, generation, 871 if (sbp2_send_management_orb(lu, node_id, generation,
684 SBP2_LOGIN_REQUEST, lu->lun, &response) < 0) { 872 SBP2_LOGIN_REQUEST, lu->lun, &response) < 0) {
685 if (lu->retries++ < 5) 873 if (lu->retries++ < 5) {
686 sbp2_queue_work(lu, DIV_ROUND_UP(HZ, 5)); 874 sbp2_queue_work(lu, DIV_ROUND_UP(HZ, 5));
687 else 875 } else {
688 fw_error("failed to login to %s LUN %04x\n", 876 fw_error("%s: failed to login to LUN %04x\n",
689 unit->device.bus_id, lu->lun); 877 tgt->bus_id, lu->lun);
878 /* Let any waiting I/O fail from now on. */
879 sbp2_unblock(lu->tgt);
880 }
690 goto out; 881 goto out;
691 } 882 }
692 883
693 lu->generation = generation; 884 tgt->node_id = node_id;
694 lu->tgt->node_id = node_id; 885 tgt->address_high = local_node_id << 16;
695 lu->tgt->address_high = local_node_id << 16; 886 sbp2_set_generation(lu, generation);
696 887
697 /* Get command block agent offset and login id. */ 888 /* Get command block agent offset and login id. */
698 lu->command_block_agent_address = 889 lu->command_block_agent_address =
@@ -700,37 +891,67 @@ static void sbp2_login(struct work_struct *work)
700 response.command_block_agent.low; 891 response.command_block_agent.low;
701 lu->login_id = LOGIN_RESPONSE_GET_LOGIN_ID(response); 892 lu->login_id = LOGIN_RESPONSE_GET_LOGIN_ID(response);
702 893
703 fw_notify("logged in to %s LUN %04x (%d retries)\n", 894 fw_notify("%s: logged in to LUN %04x (%d retries)\n",
704 unit->device.bus_id, lu->lun, lu->retries); 895 tgt->bus_id, lu->lun, lu->retries);
705 896
706#if 0 897 /* set appropriate retry limit(s) in BUSY_TIMEOUT register */
707 /* FIXME: The linux1394 sbp2 does this last step. */ 898 sbp2_set_busy_timeout(lu);
708 sbp2_set_busy_timeout(scsi_id);
709#endif
710 899
711 PREPARE_DELAYED_WORK(&lu->work, sbp2_reconnect); 900 PREPARE_DELAYED_WORK(&lu->work, sbp2_reconnect);
712 sbp2_agent_reset(lu); 901 sbp2_agent_reset(lu);
713 902
714 memset(&eight_bytes_lun, 0, sizeof(eight_bytes_lun)); 903 /* This was a re-login. */
715 eight_bytes_lun.scsi_lun[0] = (lu->lun >> 8) & 0xff; 904 if (lu->has_sdev) {
716 eight_bytes_lun.scsi_lun[1] = lu->lun & 0xff; 905 sbp2_cancel_orbs(lu);
906 sbp2_conditionally_unblock(lu);
907 goto out;
908 }
717 909
718 sdev = __scsi_add_device(shost, 0, 0, 910 if (lu->tgt->workarounds & SBP2_WORKAROUND_DELAY_INQUIRY)
719 scsilun_to_int(&eight_bytes_lun), lu); 911 ssleep(SBP2_INQUIRY_DELAY);
720 if (IS_ERR(sdev)) { 912
721 sbp2_send_management_orb(lu, node_id, generation, 913 shost = container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
722 SBP2_LOGOUT_REQUEST, lu->login_id, NULL); 914 sdev = __scsi_add_device(shost, 0, 0, sbp2_lun2int(lu->lun), lu);
723 /* 915 /*
724 * Set this back to sbp2_login so we fall back and 916 * FIXME: We are unable to perform reconnects while in sbp2_login().
725 * retry login on bus reset. 917 * Therefore __scsi_add_device() will get into trouble if a bus reset
726 */ 918 * happens in parallel. It will either fail or leave us with an
727 PREPARE_DELAYED_WORK(&lu->work, sbp2_login); 919 * unusable sdev. As a workaround we check for this and retry the
728 } else { 920 * whole login and SCSI probing.
729 lu->sdev = sdev; 921 */
922
923 /* Reported error during __scsi_add_device() */
924 if (IS_ERR(sdev))
925 goto out_logout_login;
926
927 /* Unreported error during __scsi_add_device() */
928 smp_rmb(); /* get current card generation */
929 if (generation != device->card->generation) {
930 scsi_remove_device(sdev);
730 scsi_device_put(sdev); 931 scsi_device_put(sdev);
932 goto out_logout_login;
731 } 933 }
934
935 /* No error during __scsi_add_device() */
936 lu->has_sdev = true;
937 scsi_device_put(sdev);
938 sbp2_allow_block(lu);
939 goto out;
940
941 out_logout_login:
942 smp_rmb(); /* generation may have changed */
943 generation = device->generation;
944 smp_rmb(); /* node_id must not be older than generation */
945
946 sbp2_send_management_orb(lu, device->node_id, generation,
947 SBP2_LOGOUT_REQUEST, lu->login_id, NULL);
948 /*
949 * If a bus reset happened, sbp2_update will have requeued
950 * lu->work already. Reset the work from reconnect to login.
951 */
952 PREPARE_DELAYED_WORK(&lu->work, sbp2_login);
732 out: 953 out:
733 sbp2_target_put(lu->tgt); 954 sbp2_target_put(tgt);
734} 955}
735 956
736static int sbp2_add_logical_unit(struct sbp2_target *tgt, int lun_entry) 957static int sbp2_add_logical_unit(struct sbp2_target *tgt, int lun_entry)
@@ -751,10 +972,12 @@ static int sbp2_add_logical_unit(struct sbp2_target *tgt, int lun_entry)
751 return -ENOMEM; 972 return -ENOMEM;
752 } 973 }
753 974
754 lu->tgt = tgt; 975 lu->tgt = tgt;
755 lu->sdev = NULL; 976 lu->lun = lun_entry & 0xffff;
756 lu->lun = lun_entry & 0xffff; 977 lu->retries = 0;
757 lu->retries = 0; 978 lu->has_sdev = false;
979 lu->blocked = false;
980 ++tgt->dont_block;
758 INIT_LIST_HEAD(&lu->orb_list); 981 INIT_LIST_HEAD(&lu->orb_list);
759 INIT_DELAYED_WORK(&lu->work, sbp2_login); 982 INIT_DELAYED_WORK(&lu->work, sbp2_login);
760 983
@@ -813,7 +1036,7 @@ static int sbp2_scan_unit_dir(struct sbp2_target *tgt, u32 *directory,
813 if (timeout > tgt->mgt_orb_timeout) 1036 if (timeout > tgt->mgt_orb_timeout)
814 fw_notify("%s: config rom contains %ds " 1037 fw_notify("%s: config rom contains %ds "
815 "management ORB timeout, limiting " 1038 "management ORB timeout, limiting "
816 "to %ds\n", tgt->unit->device.bus_id, 1039 "to %ds\n", tgt->bus_id,
817 timeout / 1000, 1040 timeout / 1000,
818 tgt->mgt_orb_timeout / 1000); 1041 tgt->mgt_orb_timeout / 1000);
819 break; 1042 break;
@@ -836,12 +1059,12 @@ static void sbp2_init_workarounds(struct sbp2_target *tgt, u32 model,
836 u32 firmware_revision) 1059 u32 firmware_revision)
837{ 1060{
838 int i; 1061 int i;
839 unsigned w = sbp2_param_workarounds; 1062 unsigned int w = sbp2_param_workarounds;
840 1063
841 if (w) 1064 if (w)
842 fw_notify("Please notify linux1394-devel@lists.sourceforge.net " 1065 fw_notify("Please notify linux1394-devel@lists.sourceforge.net "
843 "if you need the workarounds parameter for %s\n", 1066 "if you need the workarounds parameter for %s\n",
844 tgt->unit->device.bus_id); 1067 tgt->bus_id);
845 1068
846 if (w & SBP2_WORKAROUND_OVERRIDE) 1069 if (w & SBP2_WORKAROUND_OVERRIDE)
847 goto out; 1070 goto out;
@@ -863,8 +1086,7 @@ static void sbp2_init_workarounds(struct sbp2_target *tgt, u32 model,
863 if (w) 1086 if (w)
864 fw_notify("Workarounds for %s: 0x%x " 1087 fw_notify("Workarounds for %s: 0x%x "
865 "(firmware_revision 0x%06x, model_id 0x%06x)\n", 1088 "(firmware_revision 0x%06x, model_id 0x%06x)\n",
866 tgt->unit->device.bus_id, 1089 tgt->bus_id, w, firmware_revision, model);
867 w, firmware_revision, model);
868 tgt->workarounds = w; 1090 tgt->workarounds = w;
869} 1091}
870 1092
@@ -888,6 +1110,7 @@ static int sbp2_probe(struct device *dev)
888 tgt->unit = unit; 1110 tgt->unit = unit;
889 kref_init(&tgt->kref); 1111 kref_init(&tgt->kref);
890 INIT_LIST_HEAD(&tgt->lu_list); 1112 INIT_LIST_HEAD(&tgt->lu_list);
1113 tgt->bus_id = unit->device.bus_id;
891 1114
892 if (fw_device_enable_phys_dma(device) < 0) 1115 if (fw_device_enable_phys_dma(device) < 0)
893 goto fail_shost_put; 1116 goto fail_shost_put;
@@ -895,6 +1118,8 @@ static int sbp2_probe(struct device *dev)
895 if (scsi_add_host(shost, &unit->device) < 0) 1118 if (scsi_add_host(shost, &unit->device) < 0)
896 goto fail_shost_put; 1119 goto fail_shost_put;
897 1120
1121 fw_device_get(device);
1122
898 /* Initialize to values that won't match anything in our table. */ 1123 /* Initialize to values that won't match anything in our table. */
899 firmware_revision = 0xff000000; 1124 firmware_revision = 0xff000000;
900 model = 0xff000000; 1125 model = 0xff000000;
@@ -938,10 +1163,13 @@ static void sbp2_reconnect(struct work_struct *work)
938{ 1163{
939 struct sbp2_logical_unit *lu = 1164 struct sbp2_logical_unit *lu =
940 container_of(work, struct sbp2_logical_unit, work.work); 1165 container_of(work, struct sbp2_logical_unit, work.work);
941 struct fw_unit *unit = lu->tgt->unit; 1166 struct sbp2_target *tgt = lu->tgt;
942 struct fw_device *device = fw_device(unit->device.parent); 1167 struct fw_device *device = fw_device(tgt->unit->device.parent);
943 int generation, node_id, local_node_id; 1168 int generation, node_id, local_node_id;
944 1169
1170 if (fw_device_is_shutdown(device))
1171 goto out;
1172
945 generation = device->generation; 1173 generation = device->generation;
946 smp_rmb(); /* node_id must not be older than generation */ 1174 smp_rmb(); /* node_id must not be older than generation */
947 node_id = device->node_id; 1175 node_id = device->node_id;
@@ -950,10 +1178,17 @@ static void sbp2_reconnect(struct work_struct *work)
950 if (sbp2_send_management_orb(lu, node_id, generation, 1178 if (sbp2_send_management_orb(lu, node_id, generation,
951 SBP2_RECONNECT_REQUEST, 1179 SBP2_RECONNECT_REQUEST,
952 lu->login_id, NULL) < 0) { 1180 lu->login_id, NULL) < 0) {
953 if (lu->retries++ >= 5) { 1181 /*
954 fw_error("failed to reconnect to %s\n", 1182 * If reconnect was impossible even though we are in the
955 unit->device.bus_id); 1183 * current generation, fall back and try to log in again.
956 /* Fall back and try to log in again. */ 1184 *
1185 * We could check for "Function rejected" status, but
1186 * looking at the bus generation as simpler and more general.
1187 */
1188 smp_rmb(); /* get current card generation */
1189 if (generation == device->card->generation ||
1190 lu->retries++ >= 5) {
1191 fw_error("%s: failed to reconnect\n", tgt->bus_id);
957 lu->retries = 0; 1192 lu->retries = 0;
958 PREPARE_DELAYED_WORK(&lu->work, sbp2_login); 1193 PREPARE_DELAYED_WORK(&lu->work, sbp2_login);
959 } 1194 }
@@ -961,17 +1196,18 @@ static void sbp2_reconnect(struct work_struct *work)
961 goto out; 1196 goto out;
962 } 1197 }
963 1198
964 lu->generation = generation; 1199 tgt->node_id = node_id;
965 lu->tgt->node_id = node_id; 1200 tgt->address_high = local_node_id << 16;
966 lu->tgt->address_high = local_node_id << 16; 1201 sbp2_set_generation(lu, generation);
967 1202
968 fw_notify("reconnected to %s LUN %04x (%d retries)\n", 1203 fw_notify("%s: reconnected to LUN %04x (%d retries)\n",
969 unit->device.bus_id, lu->lun, lu->retries); 1204 tgt->bus_id, lu->lun, lu->retries);
970 1205
971 sbp2_agent_reset(lu); 1206 sbp2_agent_reset(lu);
972 sbp2_cancel_orbs(lu); 1207 sbp2_cancel_orbs(lu);
1208 sbp2_conditionally_unblock(lu);
973 out: 1209 out:
974 sbp2_target_put(lu->tgt); 1210 sbp2_target_put(tgt);
975} 1211}
976 1212
977static void sbp2_update(struct fw_unit *unit) 1213static void sbp2_update(struct fw_unit *unit)
@@ -986,6 +1222,7 @@ static void sbp2_update(struct fw_unit *unit)
986 * Iteration over tgt->lu_list is therefore safe here. 1222 * Iteration over tgt->lu_list is therefore safe here.
987 */ 1223 */
988 list_for_each_entry(lu, &tgt->lu_list, link) { 1224 list_for_each_entry(lu, &tgt->lu_list, link) {
1225 sbp2_conditionally_block(lu);
989 lu->retries = 0; 1226 lu->retries = 0;
990 sbp2_queue_work(lu, 0); 1227 sbp2_queue_work(lu, 0);
991 } 1228 }
@@ -1063,7 +1300,7 @@ complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
1063 1300
1064 if (status != NULL) { 1301 if (status != NULL) {
1065 if (STATUS_GET_DEAD(*status)) 1302 if (STATUS_GET_DEAD(*status))
1066 sbp2_agent_reset(orb->lu); 1303 sbp2_agent_reset_no_wait(orb->lu);
1067 1304
1068 switch (STATUS_GET_RESPONSE(*status)) { 1305 switch (STATUS_GET_RESPONSE(*status)) {
1069 case SBP2_STATUS_REQUEST_COMPLETE: 1306 case SBP2_STATUS_REQUEST_COMPLETE:
@@ -1089,6 +1326,7 @@ complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
1089 * or when sending the write (less likely). 1326 * or when sending the write (less likely).
1090 */ 1327 */
1091 result = DID_BUS_BUSY << 16; 1328 result = DID_BUS_BUSY << 16;
1329 sbp2_conditionally_block(orb->lu);
1092 } 1330 }
1093 1331
1094 dma_unmap_single(device->card->device, orb->base.request_bus, 1332 dma_unmap_single(device->card->device, orb->base.request_bus,
@@ -1197,7 +1435,7 @@ static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
1197 struct sbp2_logical_unit *lu = cmd->device->hostdata; 1435 struct sbp2_logical_unit *lu = cmd->device->hostdata;
1198 struct fw_device *device = fw_device(lu->tgt->unit->device.parent); 1436 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
1199 struct sbp2_command_orb *orb; 1437 struct sbp2_command_orb *orb;
1200 unsigned max_payload; 1438 unsigned int max_payload;
1201 int retval = SCSI_MLQUEUE_HOST_BUSY; 1439 int retval = SCSI_MLQUEUE_HOST_BUSY;
1202 1440
1203 /* 1441 /*
@@ -1275,6 +1513,10 @@ static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
1275{ 1513{
1276 struct sbp2_logical_unit *lu = sdev->hostdata; 1514 struct sbp2_logical_unit *lu = sdev->hostdata;
1277 1515
1516 /* (Re-)Adding logical units via the SCSI stack is not supported. */
1517 if (!lu)
1518 return -ENOSYS;
1519
1278 sdev->allow_restart = 1; 1520 sdev->allow_restart = 1;
1279 1521
1280 /* 1522 /*
@@ -1319,7 +1561,7 @@ static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
1319{ 1561{
1320 struct sbp2_logical_unit *lu = cmd->device->hostdata; 1562 struct sbp2_logical_unit *lu = cmd->device->hostdata;
1321 1563
1322 fw_notify("sbp2_scsi_abort\n"); 1564 fw_notify("%s: sbp2_scsi_abort\n", lu->tgt->bus_id);
1323 sbp2_agent_reset(lu); 1565 sbp2_agent_reset(lu);
1324 sbp2_cancel_orbs(lu); 1566 sbp2_cancel_orbs(lu);
1325 1567