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.c392
1 files changed, 303 insertions, 89 deletions
diff --git a/drivers/firewire/fw-sbp2.c b/drivers/firewire/fw-sbp2.c
index 19ece9b6d742..03069a454c07 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,7 +170,7 @@ 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
166 176
@@ -297,7 +307,7 @@ struct sbp2_command_orb {
297static const struct { 307static const struct {
298 u32 firmware_revision; 308 u32 firmware_revision;
299 u32 model; 309 u32 model;
300 unsigned workarounds; 310 unsigned int workarounds;
301} sbp2_workarounds_table[] = { 311} sbp2_workarounds_table[] = {
302 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ { 312 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
303 .firmware_revision = 0x002800, 313 .firmware_revision = 0x002800,
@@ -305,6 +315,11 @@ static const struct {
305 .workarounds = SBP2_WORKAROUND_INQUIRY_36 | 315 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
306 SBP2_WORKAROUND_MODE_SENSE_8, 316 SBP2_WORKAROUND_MODE_SENSE_8,
307 }, 317 },
318 /* DViCO Momobay FX-3A with TSB42AA9A bridge */ {
319 .firmware_revision = 0x002800,
320 .model = 0x000000,
321 .workarounds = SBP2_WORKAROUND_DELAY_INQUIRY,
322 },
308 /* Initio bridges, actually only needed for some older ones */ { 323 /* Initio bridges, actually only needed for some older ones */ {
309 .firmware_revision = 0x000200, 324 .firmware_revision = 0x000200,
310 .model = ~0, 325 .model = ~0,
@@ -501,6 +516,9 @@ sbp2_send_management_orb(struct sbp2_logical_unit *lu, int node_id,
501 unsigned int timeout; 516 unsigned int timeout;
502 int retval = -ENOMEM; 517 int retval = -ENOMEM;
503 518
519 if (function == SBP2_LOGOUT_REQUEST && fw_device_is_shutdown(device))
520 return 0;
521
504 orb = kzalloc(sizeof(*orb), GFP_ATOMIC); 522 orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
505 if (orb == NULL) 523 if (orb == NULL)
506 return -ENOMEM; 524 return -ENOMEM;
@@ -553,20 +571,20 @@ sbp2_send_management_orb(struct sbp2_logical_unit *lu, int node_id,
553 571
554 retval = -EIO; 572 retval = -EIO;
555 if (sbp2_cancel_orbs(lu) == 0) { 573 if (sbp2_cancel_orbs(lu) == 0) {
556 fw_error("orb reply timed out, rcode=0x%02x\n", 574 fw_error("%s: orb reply timed out, rcode=0x%02x\n",
557 orb->base.rcode); 575 lu->tgt->bus_id, orb->base.rcode);
558 goto out; 576 goto out;
559 } 577 }
560 578
561 if (orb->base.rcode != RCODE_COMPLETE) { 579 if (orb->base.rcode != RCODE_COMPLETE) {
562 fw_error("management write failed, rcode 0x%02x\n", 580 fw_error("%s: management write failed, rcode 0x%02x\n",
563 orb->base.rcode); 581 lu->tgt->bus_id, orb->base.rcode);
564 goto out; 582 goto out;
565 } 583 }
566 584
567 if (STATUS_GET_RESPONSE(orb->status) != 0 || 585 if (STATUS_GET_RESPONSE(orb->status) != 0 ||
568 STATUS_GET_SBP_STATUS(orb->status) != 0) { 586 STATUS_GET_SBP_STATUS(orb->status) != 0) {
569 fw_error("error status: %d:%d\n", 587 fw_error("%s: error status: %d:%d\n", lu->tgt->bus_id,
570 STATUS_GET_RESPONSE(orb->status), 588 STATUS_GET_RESPONSE(orb->status),
571 STATUS_GET_SBP_STATUS(orb->status)); 589 STATUS_GET_SBP_STATUS(orb->status));
572 goto out; 590 goto out;
@@ -590,29 +608,158 @@ sbp2_send_management_orb(struct sbp2_logical_unit *lu, int node_id,
590 608
591static void 609static void
592complete_agent_reset_write(struct fw_card *card, int rcode, 610complete_agent_reset_write(struct fw_card *card, int rcode,
593 void *payload, size_t length, void *data) 611 void *payload, size_t length, void *done)
594{ 612{
595 struct fw_transaction *t = data; 613 complete(done);
614}
596 615
597 kfree(t); 616static void sbp2_agent_reset(struct sbp2_logical_unit *lu)
617{
618 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
619 DECLARE_COMPLETION_ONSTACK(done);
620 struct fw_transaction t;
621 static u32 z;
622
623 fw_send_request(device->card, &t, TCODE_WRITE_QUADLET_REQUEST,
624 lu->tgt->node_id, lu->generation, device->max_speed,
625 lu->command_block_agent_address + SBP2_AGENT_RESET,
626 &z, sizeof(z), complete_agent_reset_write, &done);
627 wait_for_completion(&done);
628}
629
630static void
631complete_agent_reset_write_no_wait(struct fw_card *card, int rcode,
632 void *payload, size_t length, void *data)
633{
634 kfree(data);
598} 635}
599 636
600static int sbp2_agent_reset(struct sbp2_logical_unit *lu) 637static void sbp2_agent_reset_no_wait(struct sbp2_logical_unit *lu)
601{ 638{
602 struct fw_device *device = fw_device(lu->tgt->unit->device.parent); 639 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
603 struct fw_transaction *t; 640 struct fw_transaction *t;
604 static u32 zero; 641 static u32 z;
605 642
606 t = kzalloc(sizeof(*t), GFP_ATOMIC); 643 t = kmalloc(sizeof(*t), GFP_ATOMIC);
607 if (t == NULL) 644 if (t == NULL)
608 return -ENOMEM; 645 return;
609 646
610 fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST, 647 fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
611 lu->tgt->node_id, lu->generation, device->max_speed, 648 lu->tgt->node_id, lu->generation, device->max_speed,
612 lu->command_block_agent_address + SBP2_AGENT_RESET, 649 lu->command_block_agent_address + SBP2_AGENT_RESET,
613 &zero, sizeof(zero), complete_agent_reset_write, t); 650 &z, sizeof(z), complete_agent_reset_write_no_wait, t);
651}
614 652
615 return 0; 653static void sbp2_set_generation(struct sbp2_logical_unit *lu, int generation)
654{
655 struct fw_card *card = fw_device(lu->tgt->unit->device.parent)->card;
656 unsigned long flags;
657
658 /* serialize with comparisons of lu->generation and card->generation */
659 spin_lock_irqsave(&card->lock, flags);
660 lu->generation = generation;
661 spin_unlock_irqrestore(&card->lock, flags);
662}
663
664static inline void sbp2_allow_block(struct sbp2_logical_unit *lu)
665{
666 /*
667 * We may access dont_block without taking card->lock here:
668 * All callers of sbp2_allow_block() and all callers of sbp2_unblock()
669 * are currently serialized against each other.
670 * And a wrong result in sbp2_conditionally_block()'s access of
671 * dont_block is rather harmless, it simply misses its first chance.
672 */
673 --lu->tgt->dont_block;
674}
675
676/*
677 * Blocks lu->tgt if all of the following conditions are met:
678 * - Login, INQUIRY, and high-level SCSI setup of all of the target's
679 * logical units have been finished (indicated by dont_block == 0).
680 * - lu->generation is stale.
681 *
682 * Note, scsi_block_requests() must be called while holding card->lock,
683 * otherwise it might foil sbp2_[conditionally_]unblock()'s attempt to
684 * unblock the target.
685 */
686static void sbp2_conditionally_block(struct sbp2_logical_unit *lu)
687{
688 struct sbp2_target *tgt = lu->tgt;
689 struct fw_card *card = fw_device(tgt->unit->device.parent)->card;
690 struct Scsi_Host *shost =
691 container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
692 unsigned long flags;
693
694 spin_lock_irqsave(&card->lock, flags);
695 if (!tgt->dont_block && !lu->blocked &&
696 lu->generation != card->generation) {
697 lu->blocked = true;
698 if (++tgt->blocked == 1) {
699 scsi_block_requests(shost);
700 fw_notify("blocked %s\n", lu->tgt->bus_id);
701 }
702 }
703 spin_unlock_irqrestore(&card->lock, flags);
704}
705
706/*
707 * Unblocks lu->tgt as soon as all its logical units can be unblocked.
708 * Note, it is harmless to run scsi_unblock_requests() outside the
709 * card->lock protected section. On the other hand, running it inside
710 * the section might clash with shost->host_lock.
711 */
712static void sbp2_conditionally_unblock(struct sbp2_logical_unit *lu)
713{
714 struct sbp2_target *tgt = lu->tgt;
715 struct fw_card *card = fw_device(tgt->unit->device.parent)->card;
716 struct Scsi_Host *shost =
717 container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
718 unsigned long flags;
719 bool unblock = false;
720
721 spin_lock_irqsave(&card->lock, flags);
722 if (lu->blocked && lu->generation == card->generation) {
723 lu->blocked = false;
724 unblock = --tgt->blocked == 0;
725 }
726 spin_unlock_irqrestore(&card->lock, flags);
727
728 if (unblock) {
729 scsi_unblock_requests(shost);
730 fw_notify("unblocked %s\n", lu->tgt->bus_id);
731 }
732}
733
734/*
735 * Prevents future blocking of tgt and unblocks it.
736 * Note, it is harmless to run scsi_unblock_requests() outside the
737 * card->lock protected section. On the other hand, running it inside
738 * the section might clash with shost->host_lock.
739 */
740static void sbp2_unblock(struct sbp2_target *tgt)
741{
742 struct fw_card *card = fw_device(tgt->unit->device.parent)->card;
743 struct Scsi_Host *shost =
744 container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
745 unsigned long flags;
746
747 spin_lock_irqsave(&card->lock, flags);
748 ++tgt->dont_block;
749 spin_unlock_irqrestore(&card->lock, flags);
750
751 scsi_unblock_requests(shost);
752}
753
754static int sbp2_lun2int(u16 lun)
755{
756 struct scsi_lun eight_bytes_lun;
757
758 memset(&eight_bytes_lun, 0, sizeof(eight_bytes_lun));
759 eight_bytes_lun.scsi_lun[0] = (lun >> 8) & 0xff;
760 eight_bytes_lun.scsi_lun[1] = lun & 0xff;
761
762 return scsilun_to_int(&eight_bytes_lun);
616} 763}
617 764
618static void sbp2_release_target(struct kref *kref) 765static void sbp2_release_target(struct kref *kref)
@@ -621,26 +768,31 @@ static void sbp2_release_target(struct kref *kref)
621 struct sbp2_logical_unit *lu, *next; 768 struct sbp2_logical_unit *lu, *next;
622 struct Scsi_Host *shost = 769 struct Scsi_Host *shost =
623 container_of((void *)tgt, struct Scsi_Host, hostdata[0]); 770 container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
771 struct scsi_device *sdev;
624 struct fw_device *device = fw_device(tgt->unit->device.parent); 772 struct fw_device *device = fw_device(tgt->unit->device.parent);
625 773
626 list_for_each_entry_safe(lu, next, &tgt->lu_list, link) { 774 /* prevent deadlocks */
627 if (lu->sdev) 775 sbp2_unblock(tgt);
628 scsi_remove_device(lu->sdev);
629 776
630 if (!fw_device_is_shutdown(device)) 777 list_for_each_entry_safe(lu, next, &tgt->lu_list, link) {
631 sbp2_send_management_orb(lu, tgt->node_id, 778 sdev = scsi_device_lookup(shost, 0, 0, sbp2_lun2int(lu->lun));
632 lu->generation, SBP2_LOGOUT_REQUEST, 779 if (sdev) {
633 lu->login_id, NULL); 780 scsi_remove_device(sdev);
781 scsi_device_put(sdev);
782 }
783 sbp2_send_management_orb(lu, tgt->node_id, lu->generation,
784 SBP2_LOGOUT_REQUEST, lu->login_id, NULL);
634 785
635 fw_core_remove_address_handler(&lu->address_handler); 786 fw_core_remove_address_handler(&lu->address_handler);
636 list_del(&lu->link); 787 list_del(&lu->link);
637 kfree(lu); 788 kfree(lu);
638 } 789 }
639 scsi_remove_host(shost); 790 scsi_remove_host(shost);
640 fw_notify("released %s\n", tgt->unit->device.bus_id); 791 fw_notify("released %s\n", tgt->bus_id);
641 792
642 put_device(&tgt->unit->device); 793 put_device(&tgt->unit->device);
643 scsi_host_put(shost); 794 scsi_host_put(shost);
795 fw_device_put(device);
644} 796}
645 797
646static struct workqueue_struct *sbp2_wq; 798static struct workqueue_struct *sbp2_wq;
@@ -666,33 +818,42 @@ static void sbp2_login(struct work_struct *work)
666{ 818{
667 struct sbp2_logical_unit *lu = 819 struct sbp2_logical_unit *lu =
668 container_of(work, struct sbp2_logical_unit, work.work); 820 container_of(work, struct sbp2_logical_unit, work.work);
669 struct Scsi_Host *shost = 821 struct sbp2_target *tgt = lu->tgt;
670 container_of((void *)lu->tgt, struct Scsi_Host, hostdata[0]); 822 struct fw_device *device = fw_device(tgt->unit->device.parent);
823 struct Scsi_Host *shost;
671 struct scsi_device *sdev; 824 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; 825 struct sbp2_login_response response;
676 int generation, node_id, local_node_id; 826 int generation, node_id, local_node_id;
677 827
828 if (fw_device_is_shutdown(device))
829 goto out;
830
678 generation = device->generation; 831 generation = device->generation;
679 smp_rmb(); /* node_id must not be older than generation */ 832 smp_rmb(); /* node_id must not be older than generation */
680 node_id = device->node_id; 833 node_id = device->node_id;
681 local_node_id = device->card->node_id; 834 local_node_id = device->card->node_id;
682 835
836 /* If this is a re-login attempt, log out, or we might be rejected. */
837 if (lu->has_sdev)
838 sbp2_send_management_orb(lu, device->node_id, generation,
839 SBP2_LOGOUT_REQUEST, lu->login_id, NULL);
840
683 if (sbp2_send_management_orb(lu, node_id, generation, 841 if (sbp2_send_management_orb(lu, node_id, generation,
684 SBP2_LOGIN_REQUEST, lu->lun, &response) < 0) { 842 SBP2_LOGIN_REQUEST, lu->lun, &response) < 0) {
685 if (lu->retries++ < 5) 843 if (lu->retries++ < 5) {
686 sbp2_queue_work(lu, DIV_ROUND_UP(HZ, 5)); 844 sbp2_queue_work(lu, DIV_ROUND_UP(HZ, 5));
687 else 845 } else {
688 fw_error("failed to login to %s LUN %04x\n", 846 fw_error("%s: failed to login to LUN %04x\n",
689 unit->device.bus_id, lu->lun); 847 tgt->bus_id, lu->lun);
848 /* Let any waiting I/O fail from now on. */
849 sbp2_unblock(lu->tgt);
850 }
690 goto out; 851 goto out;
691 } 852 }
692 853
693 lu->generation = generation; 854 tgt->node_id = node_id;
694 lu->tgt->node_id = node_id; 855 tgt->address_high = local_node_id << 16;
695 lu->tgt->address_high = local_node_id << 16; 856 sbp2_set_generation(lu, generation);
696 857
697 /* Get command block agent offset and login id. */ 858 /* Get command block agent offset and login id. */
698 lu->command_block_agent_address = 859 lu->command_block_agent_address =
@@ -700,8 +861,8 @@ static void sbp2_login(struct work_struct *work)
700 response.command_block_agent.low; 861 response.command_block_agent.low;
701 lu->login_id = LOGIN_RESPONSE_GET_LOGIN_ID(response); 862 lu->login_id = LOGIN_RESPONSE_GET_LOGIN_ID(response);
702 863
703 fw_notify("logged in to %s LUN %04x (%d retries)\n", 864 fw_notify("%s: logged in to LUN %04x (%d retries)\n",
704 unit->device.bus_id, lu->lun, lu->retries); 865 tgt->bus_id, lu->lun, lu->retries);
705 866
706#if 0 867#if 0
707 /* FIXME: The linux1394 sbp2 does this last step. */ 868 /* FIXME: The linux1394 sbp2 does this last step. */
@@ -711,26 +872,58 @@ static void sbp2_login(struct work_struct *work)
711 PREPARE_DELAYED_WORK(&lu->work, sbp2_reconnect); 872 PREPARE_DELAYED_WORK(&lu->work, sbp2_reconnect);
712 sbp2_agent_reset(lu); 873 sbp2_agent_reset(lu);
713 874
714 memset(&eight_bytes_lun, 0, sizeof(eight_bytes_lun)); 875 /* This was a re-login. */
715 eight_bytes_lun.scsi_lun[0] = (lu->lun >> 8) & 0xff; 876 if (lu->has_sdev) {
716 eight_bytes_lun.scsi_lun[1] = lu->lun & 0xff; 877 sbp2_cancel_orbs(lu);
878 sbp2_conditionally_unblock(lu);
879 goto out;
880 }
717 881
718 sdev = __scsi_add_device(shost, 0, 0, 882 if (lu->tgt->workarounds & SBP2_WORKAROUND_DELAY_INQUIRY)
719 scsilun_to_int(&eight_bytes_lun), lu); 883 ssleep(SBP2_INQUIRY_DELAY);
720 if (IS_ERR(sdev)) { 884
721 sbp2_send_management_orb(lu, node_id, generation, 885 shost = container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
722 SBP2_LOGOUT_REQUEST, lu->login_id, NULL); 886 sdev = __scsi_add_device(shost, 0, 0, sbp2_lun2int(lu->lun), lu);
723 /* 887 /*
724 * Set this back to sbp2_login so we fall back and 888 * FIXME: We are unable to perform reconnects while in sbp2_login().
725 * retry login on bus reset. 889 * Therefore __scsi_add_device() will get into trouble if a bus reset
726 */ 890 * happens in parallel. It will either fail or leave us with an
727 PREPARE_DELAYED_WORK(&lu->work, sbp2_login); 891 * unusable sdev. As a workaround we check for this and retry the
728 } else { 892 * whole login and SCSI probing.
729 lu->sdev = sdev; 893 */
894
895 /* Reported error during __scsi_add_device() */
896 if (IS_ERR(sdev))
897 goto out_logout_login;
898
899 /* Unreported error during __scsi_add_device() */
900 smp_rmb(); /* get current card generation */
901 if (generation != device->card->generation) {
902 scsi_remove_device(sdev);
730 scsi_device_put(sdev); 903 scsi_device_put(sdev);
904 goto out_logout_login;
731 } 905 }
906
907 /* No error during __scsi_add_device() */
908 lu->has_sdev = true;
909 scsi_device_put(sdev);
910 sbp2_allow_block(lu);
911 goto out;
912
913 out_logout_login:
914 smp_rmb(); /* generation may have changed */
915 generation = device->generation;
916 smp_rmb(); /* node_id must not be older than generation */
917
918 sbp2_send_management_orb(lu, device->node_id, generation,
919 SBP2_LOGOUT_REQUEST, lu->login_id, NULL);
920 /*
921 * If a bus reset happened, sbp2_update will have requeued
922 * lu->work already. Reset the work from reconnect to login.
923 */
924 PREPARE_DELAYED_WORK(&lu->work, sbp2_login);
732 out: 925 out:
733 sbp2_target_put(lu->tgt); 926 sbp2_target_put(tgt);
734} 927}
735 928
736static int sbp2_add_logical_unit(struct sbp2_target *tgt, int lun_entry) 929static int sbp2_add_logical_unit(struct sbp2_target *tgt, int lun_entry)
@@ -751,10 +944,12 @@ static int sbp2_add_logical_unit(struct sbp2_target *tgt, int lun_entry)
751 return -ENOMEM; 944 return -ENOMEM;
752 } 945 }
753 946
754 lu->tgt = tgt; 947 lu->tgt = tgt;
755 lu->sdev = NULL; 948 lu->lun = lun_entry & 0xffff;
756 lu->lun = lun_entry & 0xffff; 949 lu->retries = 0;
757 lu->retries = 0; 950 lu->has_sdev = false;
951 lu->blocked = false;
952 ++tgt->dont_block;
758 INIT_LIST_HEAD(&lu->orb_list); 953 INIT_LIST_HEAD(&lu->orb_list);
759 INIT_DELAYED_WORK(&lu->work, sbp2_login); 954 INIT_DELAYED_WORK(&lu->work, sbp2_login);
760 955
@@ -813,7 +1008,7 @@ static int sbp2_scan_unit_dir(struct sbp2_target *tgt, u32 *directory,
813 if (timeout > tgt->mgt_orb_timeout) 1008 if (timeout > tgt->mgt_orb_timeout)
814 fw_notify("%s: config rom contains %ds " 1009 fw_notify("%s: config rom contains %ds "
815 "management ORB timeout, limiting " 1010 "management ORB timeout, limiting "
816 "to %ds\n", tgt->unit->device.bus_id, 1011 "to %ds\n", tgt->bus_id,
817 timeout / 1000, 1012 timeout / 1000,
818 tgt->mgt_orb_timeout / 1000); 1013 tgt->mgt_orb_timeout / 1000);
819 break; 1014 break;
@@ -836,12 +1031,12 @@ static void sbp2_init_workarounds(struct sbp2_target *tgt, u32 model,
836 u32 firmware_revision) 1031 u32 firmware_revision)
837{ 1032{
838 int i; 1033 int i;
839 unsigned w = sbp2_param_workarounds; 1034 unsigned int w = sbp2_param_workarounds;
840 1035
841 if (w) 1036 if (w)
842 fw_notify("Please notify linux1394-devel@lists.sourceforge.net " 1037 fw_notify("Please notify linux1394-devel@lists.sourceforge.net "
843 "if you need the workarounds parameter for %s\n", 1038 "if you need the workarounds parameter for %s\n",
844 tgt->unit->device.bus_id); 1039 tgt->bus_id);
845 1040
846 if (w & SBP2_WORKAROUND_OVERRIDE) 1041 if (w & SBP2_WORKAROUND_OVERRIDE)
847 goto out; 1042 goto out;
@@ -863,8 +1058,7 @@ static void sbp2_init_workarounds(struct sbp2_target *tgt, u32 model,
863 if (w) 1058 if (w)
864 fw_notify("Workarounds for %s: 0x%x " 1059 fw_notify("Workarounds for %s: 0x%x "
865 "(firmware_revision 0x%06x, model_id 0x%06x)\n", 1060 "(firmware_revision 0x%06x, model_id 0x%06x)\n",
866 tgt->unit->device.bus_id, 1061 tgt->bus_id, w, firmware_revision, model);
867 w, firmware_revision, model);
868 tgt->workarounds = w; 1062 tgt->workarounds = w;
869} 1063}
870 1064
@@ -888,6 +1082,7 @@ static int sbp2_probe(struct device *dev)
888 tgt->unit = unit; 1082 tgt->unit = unit;
889 kref_init(&tgt->kref); 1083 kref_init(&tgt->kref);
890 INIT_LIST_HEAD(&tgt->lu_list); 1084 INIT_LIST_HEAD(&tgt->lu_list);
1085 tgt->bus_id = unit->device.bus_id;
891 1086
892 if (fw_device_enable_phys_dma(device) < 0) 1087 if (fw_device_enable_phys_dma(device) < 0)
893 goto fail_shost_put; 1088 goto fail_shost_put;
@@ -895,6 +1090,8 @@ static int sbp2_probe(struct device *dev)
895 if (scsi_add_host(shost, &unit->device) < 0) 1090 if (scsi_add_host(shost, &unit->device) < 0)
896 goto fail_shost_put; 1091 goto fail_shost_put;
897 1092
1093 fw_device_get(device);
1094
898 /* Initialize to values that won't match anything in our table. */ 1095 /* Initialize to values that won't match anything in our table. */
899 firmware_revision = 0xff000000; 1096 firmware_revision = 0xff000000;
900 model = 0xff000000; 1097 model = 0xff000000;
@@ -938,10 +1135,13 @@ static void sbp2_reconnect(struct work_struct *work)
938{ 1135{
939 struct sbp2_logical_unit *lu = 1136 struct sbp2_logical_unit *lu =
940 container_of(work, struct sbp2_logical_unit, work.work); 1137 container_of(work, struct sbp2_logical_unit, work.work);
941 struct fw_unit *unit = lu->tgt->unit; 1138 struct sbp2_target *tgt = lu->tgt;
942 struct fw_device *device = fw_device(unit->device.parent); 1139 struct fw_device *device = fw_device(tgt->unit->device.parent);
943 int generation, node_id, local_node_id; 1140 int generation, node_id, local_node_id;
944 1141
1142 if (fw_device_is_shutdown(device))
1143 goto out;
1144
945 generation = device->generation; 1145 generation = device->generation;
946 smp_rmb(); /* node_id must not be older than generation */ 1146 smp_rmb(); /* node_id must not be older than generation */
947 node_id = device->node_id; 1147 node_id = device->node_id;
@@ -950,10 +1150,17 @@ static void sbp2_reconnect(struct work_struct *work)
950 if (sbp2_send_management_orb(lu, node_id, generation, 1150 if (sbp2_send_management_orb(lu, node_id, generation,
951 SBP2_RECONNECT_REQUEST, 1151 SBP2_RECONNECT_REQUEST,
952 lu->login_id, NULL) < 0) { 1152 lu->login_id, NULL) < 0) {
953 if (lu->retries++ >= 5) { 1153 /*
954 fw_error("failed to reconnect to %s\n", 1154 * If reconnect was impossible even though we are in the
955 unit->device.bus_id); 1155 * current generation, fall back and try to log in again.
956 /* Fall back and try to log in again. */ 1156 *
1157 * We could check for "Function rejected" status, but
1158 * looking at the bus generation as simpler and more general.
1159 */
1160 smp_rmb(); /* get current card generation */
1161 if (generation == device->card->generation ||
1162 lu->retries++ >= 5) {
1163 fw_error("%s: failed to reconnect\n", tgt->bus_id);
957 lu->retries = 0; 1164 lu->retries = 0;
958 PREPARE_DELAYED_WORK(&lu->work, sbp2_login); 1165 PREPARE_DELAYED_WORK(&lu->work, sbp2_login);
959 } 1166 }
@@ -961,17 +1168,18 @@ static void sbp2_reconnect(struct work_struct *work)
961 goto out; 1168 goto out;
962 } 1169 }
963 1170
964 lu->generation = generation; 1171 tgt->node_id = node_id;
965 lu->tgt->node_id = node_id; 1172 tgt->address_high = local_node_id << 16;
966 lu->tgt->address_high = local_node_id << 16; 1173 sbp2_set_generation(lu, generation);
967 1174
968 fw_notify("reconnected to %s LUN %04x (%d retries)\n", 1175 fw_notify("%s: reconnected to LUN %04x (%d retries)\n",
969 unit->device.bus_id, lu->lun, lu->retries); 1176 tgt->bus_id, lu->lun, lu->retries);
970 1177
971 sbp2_agent_reset(lu); 1178 sbp2_agent_reset(lu);
972 sbp2_cancel_orbs(lu); 1179 sbp2_cancel_orbs(lu);
1180 sbp2_conditionally_unblock(lu);
973 out: 1181 out:
974 sbp2_target_put(lu->tgt); 1182 sbp2_target_put(tgt);
975} 1183}
976 1184
977static void sbp2_update(struct fw_unit *unit) 1185static void sbp2_update(struct fw_unit *unit)
@@ -986,6 +1194,7 @@ static void sbp2_update(struct fw_unit *unit)
986 * Iteration over tgt->lu_list is therefore safe here. 1194 * Iteration over tgt->lu_list is therefore safe here.
987 */ 1195 */
988 list_for_each_entry(lu, &tgt->lu_list, link) { 1196 list_for_each_entry(lu, &tgt->lu_list, link) {
1197 sbp2_conditionally_block(lu);
989 lu->retries = 0; 1198 lu->retries = 0;
990 sbp2_queue_work(lu, 0); 1199 sbp2_queue_work(lu, 0);
991 } 1200 }
@@ -1063,7 +1272,7 @@ complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
1063 1272
1064 if (status != NULL) { 1273 if (status != NULL) {
1065 if (STATUS_GET_DEAD(*status)) 1274 if (STATUS_GET_DEAD(*status))
1066 sbp2_agent_reset(orb->lu); 1275 sbp2_agent_reset_no_wait(orb->lu);
1067 1276
1068 switch (STATUS_GET_RESPONSE(*status)) { 1277 switch (STATUS_GET_RESPONSE(*status)) {
1069 case SBP2_STATUS_REQUEST_COMPLETE: 1278 case SBP2_STATUS_REQUEST_COMPLETE:
@@ -1089,6 +1298,7 @@ complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
1089 * or when sending the write (less likely). 1298 * or when sending the write (less likely).
1090 */ 1299 */
1091 result = DID_BUS_BUSY << 16; 1300 result = DID_BUS_BUSY << 16;
1301 sbp2_conditionally_block(orb->lu);
1092 } 1302 }
1093 1303
1094 dma_unmap_single(device->card->device, orb->base.request_bus, 1304 dma_unmap_single(device->card->device, orb->base.request_bus,
@@ -1197,7 +1407,7 @@ static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
1197 struct sbp2_logical_unit *lu = cmd->device->hostdata; 1407 struct sbp2_logical_unit *lu = cmd->device->hostdata;
1198 struct fw_device *device = fw_device(lu->tgt->unit->device.parent); 1408 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
1199 struct sbp2_command_orb *orb; 1409 struct sbp2_command_orb *orb;
1200 unsigned max_payload; 1410 unsigned int max_payload;
1201 int retval = SCSI_MLQUEUE_HOST_BUSY; 1411 int retval = SCSI_MLQUEUE_HOST_BUSY;
1202 1412
1203 /* 1413 /*
@@ -1275,6 +1485,10 @@ static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
1275{ 1485{
1276 struct sbp2_logical_unit *lu = sdev->hostdata; 1486 struct sbp2_logical_unit *lu = sdev->hostdata;
1277 1487
1488 /* (Re-)Adding logical units via the SCSI stack is not supported. */
1489 if (!lu)
1490 return -ENOSYS;
1491
1278 sdev->allow_restart = 1; 1492 sdev->allow_restart = 1;
1279 1493
1280 /* 1494 /*
@@ -1319,7 +1533,7 @@ static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
1319{ 1533{
1320 struct sbp2_logical_unit *lu = cmd->device->hostdata; 1534 struct sbp2_logical_unit *lu = cmd->device->hostdata;
1321 1535
1322 fw_notify("sbp2_scsi_abort\n"); 1536 fw_notify("%s: sbp2_scsi_abort\n", lu->tgt->bus_id);
1323 sbp2_agent_reset(lu); 1537 sbp2_agent_reset(lu);
1324 sbp2_cancel_orbs(lu); 1538 sbp2_cancel_orbs(lu);
1325 1539