aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/firewire
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/firewire')
-rw-r--r--drivers/firewire/fw-cdev.c52
-rw-r--r--drivers/firewire/fw-device.h5
-rw-r--r--drivers/firewire/fw-ohci.c41
-rw-r--r--drivers/firewire/fw-ohci.h2
-rw-r--r--drivers/firewire/fw-sbp2.c647
-rw-r--r--drivers/firewire/fw-topology.c10
-rw-r--r--drivers/firewire/fw-topology.h6
-rw-r--r--drivers/firewire/fw-transaction.c12
8 files changed, 472 insertions, 303 deletions
diff --git a/drivers/firewire/fw-cdev.c b/drivers/firewire/fw-cdev.c
index 06471302200f..60f1a8924a95 100644
--- a/drivers/firewire/fw-cdev.c
+++ b/drivers/firewire/fw-cdev.c
@@ -25,11 +25,14 @@
25#include <linux/device.h> 25#include <linux/device.h>
26#include <linux/vmalloc.h> 26#include <linux/vmalloc.h>
27#include <linux/poll.h> 27#include <linux/poll.h>
28#include <linux/preempt.h>
29#include <linux/time.h>
28#include <linux/delay.h> 30#include <linux/delay.h>
29#include <linux/mm.h> 31#include <linux/mm.h>
30#include <linux/idr.h> 32#include <linux/idr.h>
31#include <linux/compat.h> 33#include <linux/compat.h>
32#include <linux/firewire-cdev.h> 34#include <linux/firewire-cdev.h>
35#include <asm/system.h>
33#include <asm/uaccess.h> 36#include <asm/uaccess.h>
34#include "fw-transaction.h" 37#include "fw-transaction.h"
35#include "fw-topology.h" 38#include "fw-topology.h"
@@ -140,11 +143,10 @@ static void queue_event(struct client *client, struct event *event,
140 event->v[1].size = size1; 143 event->v[1].size = size1;
141 144
142 spin_lock_irqsave(&client->lock, flags); 145 spin_lock_irqsave(&client->lock, flags);
143
144 list_add_tail(&event->link, &client->event_list); 146 list_add_tail(&event->link, &client->event_list);
145 wake_up_interruptible(&client->wait);
146
147 spin_unlock_irqrestore(&client->lock, flags); 147 spin_unlock_irqrestore(&client->lock, flags);
148
149 wake_up_interruptible(&client->wait);
148} 150}
149 151
150static int 152static int
@@ -621,20 +623,19 @@ iso_callback(struct fw_iso_context *context, u32 cycle,
621 size_t header_length, void *header, void *data) 623 size_t header_length, void *header, void *data)
622{ 624{
623 struct client *client = data; 625 struct client *client = data;
624 struct iso_interrupt *interrupt; 626 struct iso_interrupt *irq;
625 627
626 interrupt = kzalloc(sizeof(*interrupt) + header_length, GFP_ATOMIC); 628 irq = kzalloc(sizeof(*irq) + header_length, GFP_ATOMIC);
627 if (interrupt == NULL) 629 if (irq == NULL)
628 return; 630 return;
629 631
630 interrupt->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT; 632 irq->interrupt.type = FW_CDEV_EVENT_ISO_INTERRUPT;
631 interrupt->interrupt.closure = client->iso_closure; 633 irq->interrupt.closure = client->iso_closure;
632 interrupt->interrupt.cycle = cycle; 634 irq->interrupt.cycle = cycle;
633 interrupt->interrupt.header_length = header_length; 635 irq->interrupt.header_length = header_length;
634 memcpy(interrupt->interrupt.header, header, header_length); 636 memcpy(irq->interrupt.header, header, header_length);
635 queue_event(client, &interrupt->event, 637 queue_event(client, &irq->event, &irq->interrupt,
636 &interrupt->interrupt, 638 sizeof(irq->interrupt) + header_length, NULL, 0);
637 sizeof(interrupt->interrupt) + header_length, NULL, 0);
638} 639}
639 640
640static int ioctl_create_iso_context(struct client *client, void *buffer) 641static int ioctl_create_iso_context(struct client *client, void *buffer)
@@ -812,6 +813,28 @@ static int ioctl_stop_iso(struct client *client, void *buffer)
812 return fw_iso_context_stop(client->iso_context); 813 return fw_iso_context_stop(client->iso_context);
813} 814}
814 815
816static int ioctl_get_cycle_timer(struct client *client, void *buffer)
817{
818 struct fw_cdev_get_cycle_timer *request = buffer;
819 struct fw_card *card = client->device->card;
820 unsigned long long bus_time;
821 struct timeval tv;
822 unsigned long flags;
823
824 preempt_disable();
825 local_irq_save(flags);
826
827 bus_time = card->driver->get_bus_time(card);
828 do_gettimeofday(&tv);
829
830 local_irq_restore(flags);
831 preempt_enable();
832
833 request->local_time = tv.tv_sec * 1000000ULL + tv.tv_usec;
834 request->cycle_timer = bus_time & 0xffffffff;
835 return 0;
836}
837
815static int (* const ioctl_handlers[])(struct client *client, void *buffer) = { 838static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
816 ioctl_get_info, 839 ioctl_get_info,
817 ioctl_send_request, 840 ioctl_send_request,
@@ -825,6 +848,7 @@ static int (* const ioctl_handlers[])(struct client *client, void *buffer) = {
825 ioctl_queue_iso, 848 ioctl_queue_iso,
826 ioctl_start_iso, 849 ioctl_start_iso,
827 ioctl_stop_iso, 850 ioctl_stop_iso,
851 ioctl_get_cycle_timer,
828}; 852};
829 853
830static int 854static int
diff --git a/drivers/firewire/fw-device.h b/drivers/firewire/fw-device.h
index d13e6a69707f..894d4a92a18e 100644
--- a/drivers/firewire/fw-device.h
+++ b/drivers/firewire/fw-device.h
@@ -102,11 +102,6 @@ fw_unit(struct device *dev)
102#define CSR_INSTANCE 0x18 102#define CSR_INSTANCE 0x18
103#define CSR_DIRECTORY_ID 0x20 103#define CSR_DIRECTORY_ID 0x20
104 104
105#define SBP2_COMMAND_SET_SPECIFIER 0x38
106#define SBP2_COMMAND_SET 0x39
107#define SBP2_COMMAND_SET_REVISION 0x3b
108#define SBP2_FIRMWARE_REVISION 0x3c
109
110struct fw_csr_iterator { 105struct fw_csr_iterator {
111 u32 *p; 106 u32 *p;
112 u32 *end; 107 u32 *end;
diff --git a/drivers/firewire/fw-ohci.c b/drivers/firewire/fw-ohci.c
index e14c1ca7813e..2f307c4df335 100644
--- a/drivers/firewire/fw-ohci.c
+++ b/drivers/firewire/fw-ohci.c
@@ -18,21 +18,23 @@
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */ 19 */
20 20
21#include <linux/kernel.h> 21#include <linux/compiler.h>
22#include <linux/module.h>
23#include <linux/init.h>
24#include <linux/interrupt.h>
25#include <linux/pci.h>
26#include <linux/delay.h> 22#include <linux/delay.h>
27#include <linux/poll.h>
28#include <linux/dma-mapping.h> 23#include <linux/dma-mapping.h>
24#include <linux/gfp.h>
25#include <linux/init.h>
26#include <linux/interrupt.h>
27#include <linux/kernel.h>
29#include <linux/mm.h> 28#include <linux/mm.h>
29#include <linux/module.h>
30#include <linux/pci.h>
31#include <linux/spinlock.h>
30 32
31#include <asm/uaccess.h> 33#include <asm/page.h>
32#include <asm/semaphore.h> 34#include <asm/system.h>
33 35
34#include "fw-transaction.h"
35#include "fw-ohci.h" 36#include "fw-ohci.h"
37#include "fw-transaction.h"
36 38
37#define DESCRIPTOR_OUTPUT_MORE 0 39#define DESCRIPTOR_OUTPUT_MORE 0
38#define DESCRIPTOR_OUTPUT_LAST (1 << 12) 40#define DESCRIPTOR_OUTPUT_LAST (1 << 12)
@@ -678,6 +680,9 @@ at_context_queue_packet(struct context *ctx, struct fw_packet *packet)
678 680
679 /* FIXME: Document how the locking works. */ 681 /* FIXME: Document how the locking works. */
680 if (ohci->generation != packet->generation) { 682 if (ohci->generation != packet->generation) {
683 if (packet->payload_length > 0)
684 dma_unmap_single(ohci->card.device, payload_bus,
685 packet->payload_length, DMA_TO_DEVICE);
681 packet->ack = RCODE_GENERATION; 686 packet->ack = RCODE_GENERATION;
682 return -1; 687 return -1;
683 } 688 }
@@ -912,10 +917,15 @@ static void bus_reset_tasklet(unsigned long data)
912 917
913 reg = reg_read(ohci, OHCI1394_NodeID); 918 reg = reg_read(ohci, OHCI1394_NodeID);
914 if (!(reg & OHCI1394_NodeID_idValid)) { 919 if (!(reg & OHCI1394_NodeID_idValid)) {
915 fw_error("node ID not valid, new bus reset in progress\n"); 920 fw_notify("node ID not valid, new bus reset in progress\n");
916 return; 921 return;
917 } 922 }
918 ohci->node_id = reg & 0xffff; 923 if ((reg & OHCI1394_NodeID_nodeNumber) == 63) {
924 fw_notify("malconfigured bus\n");
925 return;
926 }
927 ohci->node_id = reg & (OHCI1394_NodeID_busNumber |
928 OHCI1394_NodeID_nodeNumber);
919 929
920 /* 930 /*
921 * The count in the SelfIDCount register is the number of 931 * The count in the SelfIDCount register is the number of
@@ -926,12 +936,14 @@ static void bus_reset_tasklet(unsigned long data)
926 936
927 self_id_count = (reg_read(ohci, OHCI1394_SelfIDCount) >> 3) & 0x3ff; 937 self_id_count = (reg_read(ohci, OHCI1394_SelfIDCount) >> 3) & 0x3ff;
928 generation = (le32_to_cpu(ohci->self_id_cpu[0]) >> 16) & 0xff; 938 generation = (le32_to_cpu(ohci->self_id_cpu[0]) >> 16) & 0xff;
939 rmb();
929 940
930 for (i = 1, j = 0; j < self_id_count; i += 2, j++) { 941 for (i = 1, j = 0; j < self_id_count; i += 2, j++) {
931 if (ohci->self_id_cpu[i] != ~ohci->self_id_cpu[i + 1]) 942 if (ohci->self_id_cpu[i] != ~ohci->self_id_cpu[i + 1])
932 fw_error("inconsistent self IDs\n"); 943 fw_error("inconsistent self IDs\n");
933 ohci->self_id_buffer[j] = le32_to_cpu(ohci->self_id_cpu[i]); 944 ohci->self_id_buffer[j] = le32_to_cpu(ohci->self_id_cpu[i]);
934 } 945 }
946 rmb();
935 947
936 /* 948 /*
937 * Check the consistency of the self IDs we just read. The 949 * Check the consistency of the self IDs we just read. The
@@ -1046,6 +1058,9 @@ static irqreturn_t irq_handler(int irq, void *data)
1046 iso_event &= ~(1 << i); 1058 iso_event &= ~(1 << i);
1047 } 1059 }
1048 1060
1061 if (unlikely(event & OHCI1394_postedWriteErr))
1062 fw_error("PCI posted write error\n");
1063
1049 if (event & OHCI1394_cycle64Seconds) { 1064 if (event & OHCI1394_cycle64Seconds) {
1050 cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer); 1065 cycle_time = reg_read(ohci, OHCI1394_IsochronousCycleTimer);
1051 if ((cycle_time & 0x80000000) == 0) 1066 if ((cycle_time & 0x80000000) == 0)
@@ -1119,8 +1134,8 @@ static int ohci_enable(struct fw_card *card, u32 *config_rom, size_t length)
1119 OHCI1394_RQPkt | OHCI1394_RSPkt | 1134 OHCI1394_RQPkt | OHCI1394_RSPkt |
1120 OHCI1394_reqTxComplete | OHCI1394_respTxComplete | 1135 OHCI1394_reqTxComplete | OHCI1394_respTxComplete |
1121 OHCI1394_isochRx | OHCI1394_isochTx | 1136 OHCI1394_isochRx | OHCI1394_isochTx |
1122 OHCI1394_masterIntEnable | 1137 OHCI1394_postedWriteErr | OHCI1394_cycle64Seconds |
1123 OHCI1394_cycle64Seconds); 1138 OHCI1394_masterIntEnable);
1124 1139
1125 /* Activate link_on bit and contender bit in our self ID packets.*/ 1140 /* Activate link_on bit and contender bit in our self ID packets.*/
1126 if (ohci_update_phy_reg(card, 4, 0, 1141 if (ohci_update_phy_reg(card, 4, 0,
diff --git a/drivers/firewire/fw-ohci.h b/drivers/firewire/fw-ohci.h
index fa15706397d7..dec4f04e6b24 100644
--- a/drivers/firewire/fw-ohci.h
+++ b/drivers/firewire/fw-ohci.h
@@ -59,6 +59,8 @@
59#define OHCI1394_LinkControl_cycleSource (1 << 22) 59#define OHCI1394_LinkControl_cycleSource (1 << 22)
60#define OHCI1394_NodeID 0x0E8 60#define OHCI1394_NodeID 0x0E8
61#define OHCI1394_NodeID_idValid 0x80000000 61#define OHCI1394_NodeID_idValid 0x80000000
62#define OHCI1394_NodeID_nodeNumber 0x0000003f
63#define OHCI1394_NodeID_busNumber 0x0000ffc0
62#define OHCI1394_PhyControl 0x0EC 64#define OHCI1394_PhyControl 0x0EC
63#define OHCI1394_PhyControl_Read(addr) (((addr) << 8) | 0x00008000) 65#define OHCI1394_PhyControl_Read(addr) (((addr) << 8) | 0x00008000)
64#define OHCI1394_PhyControl_ReadDone 0x80000000 66#define OHCI1394_PhyControl_ReadDone 0x80000000
diff --git a/drivers/firewire/fw-sbp2.c b/drivers/firewire/fw-sbp2.c
index 238730f75db1..5596df65c8ed 100644
--- a/drivers/firewire/fw-sbp2.c
+++ b/drivers/firewire/fw-sbp2.c
@@ -37,11 +37,12 @@
37#include <linux/dma-mapping.h> 37#include <linux/dma-mapping.h>
38#include <linux/blkdev.h> 38#include <linux/blkdev.h>
39#include <linux/string.h> 39#include <linux/string.h>
40#include <linux/stringify.h>
40#include <linux/timer.h> 41#include <linux/timer.h>
42#include <linux/workqueue.h>
41 43
42#include <scsi/scsi.h> 44#include <scsi/scsi.h>
43#include <scsi/scsi_cmnd.h> 45#include <scsi/scsi_cmnd.h>
44#include <scsi/scsi_dbg.h>
45#include <scsi/scsi_device.h> 46#include <scsi/scsi_device.h>
46#include <scsi/scsi_host.h> 47#include <scsi/scsi_host.h>
47 48
@@ -61,36 +62,94 @@ module_param_named(exclusive_login, sbp2_param_exclusive_login, bool, 0644);
61MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device " 62MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
62 "(default = Y, use N for concurrent initiators)"); 63 "(default = Y, use N for concurrent initiators)");
63 64
65/*
66 * Flags for firmware oddities
67 *
68 * - 128kB max transfer
69 * Limit transfer size. Necessary for some old bridges.
70 *
71 * - 36 byte inquiry
72 * When scsi_mod probes the device, let the inquiry command look like that
73 * from MS Windows.
74 *
75 * - skip mode page 8
76 * Suppress sending of mode_sense for mode page 8 if the device pretends to
77 * support the SCSI Primary Block commands instead of Reduced Block Commands.
78 *
79 * - fix capacity
80 * Tell sd_mod to correct the last sector number reported by read_capacity.
81 * Avoids access beyond actual disk limits on devices with an off-by-one bug.
82 * Don't use this with devices which don't have this bug.
83 *
84 * - override internal blacklist
85 * Instead of adding to the built-in blacklist, use only the workarounds
86 * specified in the module load parameter.
87 * Useful if a blacklist entry interfered with a non-broken device.
88 */
89#define SBP2_WORKAROUND_128K_MAX_TRANS 0x1
90#define SBP2_WORKAROUND_INQUIRY_36 0x2
91#define SBP2_WORKAROUND_MODE_SENSE_8 0x4
92#define SBP2_WORKAROUND_FIX_CAPACITY 0x8
93#define SBP2_WORKAROUND_OVERRIDE 0x100
94
95static int sbp2_param_workarounds;
96module_param_named(workarounds, sbp2_param_workarounds, int, 0644);
97MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
98 ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
99 ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
100 ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
101 ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
102 ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
103 ", or a combination)");
104
64/* I don't know why the SCSI stack doesn't define something like this... */ 105/* I don't know why the SCSI stack doesn't define something like this... */
65typedef void (*scsi_done_fn_t)(struct scsi_cmnd *); 106typedef void (*scsi_done_fn_t)(struct scsi_cmnd *);
66 107
67static const char sbp2_driver_name[] = "sbp2"; 108static const char sbp2_driver_name[] = "sbp2";
68 109
69struct sbp2_device { 110/*
70 struct kref kref; 111 * We create one struct sbp2_logical_unit per SBP-2 Logical Unit Number Entry
71 struct fw_unit *unit; 112 * and one struct scsi_device per sbp2_logical_unit.
113 */
114struct sbp2_logical_unit {
115 struct sbp2_target *tgt;
116 struct list_head link;
117 struct scsi_device *sdev;
72 struct fw_address_handler address_handler; 118 struct fw_address_handler address_handler;
73 struct list_head orb_list; 119 struct list_head orb_list;
74 u64 management_agent_address; 120
75 u64 command_block_agent_address; 121 u64 command_block_agent_address;
76 u32 workarounds; 122 u16 lun;
77 int login_id; 123 int login_id;
78 124
79 /* 125 /*
80 * We cache these addresses and only update them once we've 126 * The generation is updated once we've logged in or reconnected
81 * logged in or reconnected to the sbp2 device. That way, any 127 * to the logical unit. Thus, I/O to the device will automatically
82 * IO to the device will automatically fail and get retried if 128 * fail and get retried if it happens in a window where the device
83 * it happens in a window where the device is not ready to 129 * is not ready, e.g. after a bus reset but before we reconnect.
84 * handle it (e.g. after a bus reset but before we reconnect).
85 */ 130 */
86 int node_id;
87 int address_high;
88 int generation; 131 int generation;
89
90 int retries; 132 int retries;
91 struct delayed_work work; 133 struct delayed_work work;
92}; 134};
93 135
136/*
137 * We create one struct sbp2_target per IEEE 1212 Unit Directory
138 * and one struct Scsi_Host per sbp2_target.
139 */
140struct sbp2_target {
141 struct kref kref;
142 struct fw_unit *unit;
143
144 u64 management_agent_address;
145 int directory_id;
146 int node_id;
147 int address_high;
148
149 unsigned workarounds;
150 struct list_head lu_list;
151};
152
94#define SBP2_MAX_SG_ELEMENT_LENGTH 0xf000 153#define SBP2_MAX_SG_ELEMENT_LENGTH 0xf000
95#define SBP2_MAX_SECTORS 255 /* Max sectors supported */ 154#define SBP2_MAX_SECTORS 255 /* Max sectors supported */
96#define SBP2_ORB_TIMEOUT 2000 /* Timeout in ms */ 155#define SBP2_ORB_TIMEOUT 2000 /* Timeout in ms */
@@ -101,17 +160,9 @@ struct sbp2_device {
101#define SBP2_DIRECTION_FROM_MEDIA 0x1 160#define SBP2_DIRECTION_FROM_MEDIA 0x1
102 161
103/* Unit directory keys */ 162/* Unit directory keys */
104#define SBP2_COMMAND_SET_SPECIFIER 0x38 163#define SBP2_CSR_FIRMWARE_REVISION 0x3c
105#define SBP2_COMMAND_SET 0x39 164#define SBP2_CSR_LOGICAL_UNIT_NUMBER 0x14
106#define SBP2_COMMAND_SET_REVISION 0x3b 165#define SBP2_CSR_LOGICAL_UNIT_DIRECTORY 0xd4
107#define SBP2_FIRMWARE_REVISION 0x3c
108
109/* Flags for detected oddities and brokeness */
110#define SBP2_WORKAROUND_128K_MAX_TRANS 0x1
111#define SBP2_WORKAROUND_INQUIRY_36 0x2
112#define SBP2_WORKAROUND_MODE_SENSE_8 0x4
113#define SBP2_WORKAROUND_FIX_CAPACITY 0x8
114#define SBP2_WORKAROUND_OVERRIDE 0x100
115 166
116/* Management orb opcodes */ 167/* Management orb opcodes */
117#define SBP2_LOGIN_REQUEST 0x0 168#define SBP2_LOGIN_REQUEST 0x0
@@ -219,7 +270,7 @@ struct sbp2_command_orb {
219 } request; 270 } request;
220 struct scsi_cmnd *cmd; 271 struct scsi_cmnd *cmd;
221 scsi_done_fn_t done; 272 scsi_done_fn_t done;
222 struct fw_unit *unit; 273 struct sbp2_logical_unit *lu;
223 274
224 struct sbp2_pointer page_table[SG_ALL] __attribute__((aligned(8))); 275 struct sbp2_pointer page_table[SG_ALL] __attribute__((aligned(8)));
225 dma_addr_t page_table_bus; 276 dma_addr_t page_table_bus;
@@ -295,7 +346,7 @@ sbp2_status_write(struct fw_card *card, struct fw_request *request,
295 unsigned long long offset, 346 unsigned long long offset,
296 void *payload, size_t length, void *callback_data) 347 void *payload, size_t length, void *callback_data)
297{ 348{
298 struct sbp2_device *sd = callback_data; 349 struct sbp2_logical_unit *lu = callback_data;
299 struct sbp2_orb *orb; 350 struct sbp2_orb *orb;
300 struct sbp2_status status; 351 struct sbp2_status status;
301 size_t header_size; 352 size_t header_size;
@@ -319,7 +370,7 @@ sbp2_status_write(struct fw_card *card, struct fw_request *request,
319 370
320 /* Lookup the orb corresponding to this status write. */ 371 /* Lookup the orb corresponding to this status write. */
321 spin_lock_irqsave(&card->lock, flags); 372 spin_lock_irqsave(&card->lock, flags);
322 list_for_each_entry(orb, &sd->orb_list, link) { 373 list_for_each_entry(orb, &lu->orb_list, link) {
323 if (STATUS_GET_ORB_HIGH(status) == 0 && 374 if (STATUS_GET_ORB_HIGH(status) == 0 &&
324 STATUS_GET_ORB_LOW(status) == orb->request_bus) { 375 STATUS_GET_ORB_LOW(status) == orb->request_bus) {
325 orb->rcode = RCODE_COMPLETE; 376 orb->rcode = RCODE_COMPLETE;
@@ -329,7 +380,7 @@ sbp2_status_write(struct fw_card *card, struct fw_request *request,
329 } 380 }
330 spin_unlock_irqrestore(&card->lock, flags); 381 spin_unlock_irqrestore(&card->lock, flags);
331 382
332 if (&orb->link != &sd->orb_list) 383 if (&orb->link != &lu->orb_list)
333 orb->callback(orb, &status); 384 orb->callback(orb, &status);
334 else 385 else
335 fw_error("status write for unknown orb\n"); 386 fw_error("status write for unknown orb\n");
@@ -361,20 +412,20 @@ complete_transaction(struct fw_card *card, int rcode,
361 orb->rcode = rcode; 412 orb->rcode = rcode;
362 if (orb->rcode != RCODE_COMPLETE) { 413 if (orb->rcode != RCODE_COMPLETE) {
363 list_del(&orb->link); 414 list_del(&orb->link);
415 spin_unlock_irqrestore(&card->lock, flags);
364 orb->callback(orb, NULL); 416 orb->callback(orb, NULL);
417 } else {
418 spin_unlock_irqrestore(&card->lock, flags);
365 } 419 }
366 420
367 spin_unlock_irqrestore(&card->lock, flags);
368
369 kref_put(&orb->kref, free_orb); 421 kref_put(&orb->kref, free_orb);
370} 422}
371 423
372static void 424static void
373sbp2_send_orb(struct sbp2_orb *orb, struct fw_unit *unit, 425sbp2_send_orb(struct sbp2_orb *orb, struct sbp2_logical_unit *lu,
374 int node_id, int generation, u64 offset) 426 int node_id, int generation, u64 offset)
375{ 427{
376 struct fw_device *device = fw_device(unit->device.parent); 428 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
377 struct sbp2_device *sd = unit->device.driver_data;
378 unsigned long flags; 429 unsigned long flags;
379 430
380 orb->pointer.high = 0; 431 orb->pointer.high = 0;
@@ -382,7 +433,7 @@ sbp2_send_orb(struct sbp2_orb *orb, struct fw_unit *unit,
382 fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof(orb->pointer)); 433 fw_memcpy_to_be32(&orb->pointer, &orb->pointer, sizeof(orb->pointer));
383 434
384 spin_lock_irqsave(&device->card->lock, flags); 435 spin_lock_irqsave(&device->card->lock, flags);
385 list_add_tail(&orb->link, &sd->orb_list); 436 list_add_tail(&orb->link, &lu->orb_list);
386 spin_unlock_irqrestore(&device->card->lock, flags); 437 spin_unlock_irqrestore(&device->card->lock, flags);
387 438
388 /* Take a ref for the orb list and for the transaction callback. */ 439 /* Take a ref for the orb list and for the transaction callback. */
@@ -395,10 +446,9 @@ sbp2_send_orb(struct sbp2_orb *orb, struct fw_unit *unit,
395 complete_transaction, orb); 446 complete_transaction, orb);
396} 447}
397 448
398static int sbp2_cancel_orbs(struct fw_unit *unit) 449static int sbp2_cancel_orbs(struct sbp2_logical_unit *lu)
399{ 450{
400 struct fw_device *device = fw_device(unit->device.parent); 451 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
401 struct sbp2_device *sd = unit->device.driver_data;
402 struct sbp2_orb *orb, *next; 452 struct sbp2_orb *orb, *next;
403 struct list_head list; 453 struct list_head list;
404 unsigned long flags; 454 unsigned long flags;
@@ -406,7 +456,7 @@ static int sbp2_cancel_orbs(struct fw_unit *unit)
406 456
407 INIT_LIST_HEAD(&list); 457 INIT_LIST_HEAD(&list);
408 spin_lock_irqsave(&device->card->lock, flags); 458 spin_lock_irqsave(&device->card->lock, flags);
409 list_splice_init(&sd->orb_list, &list); 459 list_splice_init(&lu->orb_list, &list);
410 spin_unlock_irqrestore(&device->card->lock, flags); 460 spin_unlock_irqrestore(&device->card->lock, flags);
411 461
412 list_for_each_entry_safe(orb, next, &list, link) { 462 list_for_each_entry_safe(orb, next, &list, link) {
@@ -433,11 +483,11 @@ complete_management_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
433} 483}
434 484
435static int 485static int
436sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation, 486sbp2_send_management_orb(struct sbp2_logical_unit *lu, int node_id,
437 int function, int lun, void *response) 487 int generation, int function, int lun_or_login_id,
488 void *response)
438{ 489{
439 struct fw_device *device = fw_device(unit->device.parent); 490 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
440 struct sbp2_device *sd = unit->device.driver_data;
441 struct sbp2_management_orb *orb; 491 struct sbp2_management_orb *orb;
442 int retval = -ENOMEM; 492 int retval = -ENOMEM;
443 493
@@ -458,12 +508,12 @@ sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation,
458 orb->request.misc = 508 orb->request.misc =
459 MANAGEMENT_ORB_NOTIFY | 509 MANAGEMENT_ORB_NOTIFY |
460 MANAGEMENT_ORB_FUNCTION(function) | 510 MANAGEMENT_ORB_FUNCTION(function) |
461 MANAGEMENT_ORB_LUN(lun); 511 MANAGEMENT_ORB_LUN(lun_or_login_id);
462 orb->request.length = 512 orb->request.length =
463 MANAGEMENT_ORB_RESPONSE_LENGTH(sizeof(orb->response)); 513 MANAGEMENT_ORB_RESPONSE_LENGTH(sizeof(orb->response));
464 514
465 orb->request.status_fifo.high = sd->address_handler.offset >> 32; 515 orb->request.status_fifo.high = lu->address_handler.offset >> 32;
466 orb->request.status_fifo.low = sd->address_handler.offset; 516 orb->request.status_fifo.low = lu->address_handler.offset;
467 517
468 if (function == SBP2_LOGIN_REQUEST) { 518 if (function == SBP2_LOGIN_REQUEST) {
469 orb->request.misc |= 519 orb->request.misc |=
@@ -482,14 +532,14 @@ sbp2_send_management_orb(struct fw_unit *unit, int node_id, int generation,
482 if (dma_mapping_error(orb->base.request_bus)) 532 if (dma_mapping_error(orb->base.request_bus))
483 goto fail_mapping_request; 533 goto fail_mapping_request;
484 534
485 sbp2_send_orb(&orb->base, unit, 535 sbp2_send_orb(&orb->base, lu, node_id, generation,
486 node_id, generation, sd->management_agent_address); 536 lu->tgt->management_agent_address);
487 537
488 wait_for_completion_timeout(&orb->done, 538 wait_for_completion_timeout(&orb->done,
489 msecs_to_jiffies(SBP2_ORB_TIMEOUT)); 539 msecs_to_jiffies(SBP2_ORB_TIMEOUT));
490 540
491 retval = -EIO; 541 retval = -EIO;
492 if (sbp2_cancel_orbs(unit) == 0) { 542 if (sbp2_cancel_orbs(lu) == 0) {
493 fw_error("orb reply timed out, rcode=0x%02x\n", 543 fw_error("orb reply timed out, rcode=0x%02x\n",
494 orb->base.rcode); 544 orb->base.rcode);
495 goto out; 545 goto out;
@@ -534,10 +584,9 @@ complete_agent_reset_write(struct fw_card *card, int rcode,
534 kfree(t); 584 kfree(t);
535} 585}
536 586
537static int sbp2_agent_reset(struct fw_unit *unit) 587static int sbp2_agent_reset(struct sbp2_logical_unit *lu)
538{ 588{
539 struct fw_device *device = fw_device(unit->device.parent); 589 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
540 struct sbp2_device *sd = unit->device.driver_data;
541 struct fw_transaction *t; 590 struct fw_transaction *t;
542 static u32 zero; 591 static u32 zero;
543 592
@@ -546,181 +595,272 @@ static int sbp2_agent_reset(struct fw_unit *unit)
546 return -ENOMEM; 595 return -ENOMEM;
547 596
548 fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST, 597 fw_send_request(device->card, t, TCODE_WRITE_QUADLET_REQUEST,
549 sd->node_id, sd->generation, device->max_speed, 598 lu->tgt->node_id, lu->generation, device->max_speed,
550 sd->command_block_agent_address + SBP2_AGENT_RESET, 599 lu->command_block_agent_address + SBP2_AGENT_RESET,
551 &zero, sizeof(zero), complete_agent_reset_write, t); 600 &zero, sizeof(zero), complete_agent_reset_write, t);
552 601
553 return 0; 602 return 0;
554} 603}
555 604
556static void sbp2_reconnect(struct work_struct *work); 605static void sbp2_release_target(struct kref *kref)
557static struct scsi_host_template scsi_driver_template;
558
559static void release_sbp2_device(struct kref *kref)
560{ 606{
561 struct sbp2_device *sd = container_of(kref, struct sbp2_device, kref); 607 struct sbp2_target *tgt = container_of(kref, struct sbp2_target, kref);
562 struct Scsi_Host *host = 608 struct sbp2_logical_unit *lu, *next;
563 container_of((void *)sd, struct Scsi_Host, hostdata[0]); 609 struct Scsi_Host *shost =
564 610 container_of((void *)tgt, struct Scsi_Host, hostdata[0]);
565 scsi_remove_host(host); 611
566 sbp2_send_management_orb(sd->unit, sd->node_id, sd->generation, 612 list_for_each_entry_safe(lu, next, &tgt->lu_list, link) {
567 SBP2_LOGOUT_REQUEST, sd->login_id, NULL); 613 if (lu->sdev)
568 fw_core_remove_address_handler(&sd->address_handler); 614 scsi_remove_device(lu->sdev);
569 fw_notify("removed sbp2 unit %s\n", sd->unit->device.bus_id); 615
570 put_device(&sd->unit->device); 616 sbp2_send_management_orb(lu, tgt->node_id, lu->generation,
571 scsi_host_put(host); 617 SBP2_LOGOUT_REQUEST, lu->login_id, NULL);
618 fw_core_remove_address_handler(&lu->address_handler);
619 list_del(&lu->link);
620 kfree(lu);
621 }
622 scsi_remove_host(shost);
623 fw_notify("released %s\n", tgt->unit->device.bus_id);
624
625 put_device(&tgt->unit->device);
626 scsi_host_put(shost);
572} 627}
573 628
629static struct workqueue_struct *sbp2_wq;
630
631static void sbp2_reconnect(struct work_struct *work);
632
574static void sbp2_login(struct work_struct *work) 633static void sbp2_login(struct work_struct *work)
575{ 634{
576 struct sbp2_device *sd = 635 struct sbp2_logical_unit *lu =
577 container_of(work, struct sbp2_device, work.work); 636 container_of(work, struct sbp2_logical_unit, work.work);
578 struct Scsi_Host *host = 637 struct Scsi_Host *shost =
579 container_of((void *)sd, struct Scsi_Host, hostdata[0]); 638 container_of((void *)lu->tgt, struct Scsi_Host, hostdata[0]);
580 struct fw_unit *unit = sd->unit; 639 struct scsi_device *sdev;
640 struct scsi_lun eight_bytes_lun;
641 struct fw_unit *unit = lu->tgt->unit;
581 struct fw_device *device = fw_device(unit->device.parent); 642 struct fw_device *device = fw_device(unit->device.parent);
582 struct sbp2_login_response response; 643 struct sbp2_login_response response;
583 int generation, node_id, local_node_id, lun, retval; 644 int generation, node_id, local_node_id;
584
585 /* FIXME: Make this work for multi-lun devices. */
586 lun = 0;
587 645
588 generation = device->card->generation; 646 generation = device->card->generation;
589 node_id = device->node->node_id; 647 node_id = device->node->node_id;
590 local_node_id = device->card->local_node->node_id; 648 local_node_id = device->card->local_node->node_id;
591 649
592 if (sbp2_send_management_orb(unit, node_id, generation, 650 if (sbp2_send_management_orb(lu, node_id, generation,
593 SBP2_LOGIN_REQUEST, lun, &response) < 0) { 651 SBP2_LOGIN_REQUEST, lu->lun, &response) < 0) {
594 if (sd->retries++ < 5) { 652 if (lu->retries++ < 5) {
595 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5)); 653 queue_delayed_work(sbp2_wq, &lu->work,
654 DIV_ROUND_UP(HZ, 5));
596 } else { 655 } else {
597 fw_error("failed to login to %s\n", 656 fw_error("failed to login to %s LUN %04x\n",
598 unit->device.bus_id); 657 unit->device.bus_id, lu->lun);
599 kref_put(&sd->kref, release_sbp2_device); 658 kref_put(&lu->tgt->kref, sbp2_release_target);
600 } 659 }
601 return; 660 return;
602 } 661 }
603 662
604 sd->generation = generation; 663 lu->generation = generation;
605 sd->node_id = node_id; 664 lu->tgt->node_id = node_id;
606 sd->address_high = local_node_id << 16; 665 lu->tgt->address_high = local_node_id << 16;
607 666
608 /* Get command block agent offset and login id. */ 667 /* Get command block agent offset and login id. */
609 sd->command_block_agent_address = 668 lu->command_block_agent_address =
610 ((u64) (response.command_block_agent.high & 0xffff) << 32) | 669 ((u64) (response.command_block_agent.high & 0xffff) << 32) |
611 response.command_block_agent.low; 670 response.command_block_agent.low;
612 sd->login_id = LOGIN_RESPONSE_GET_LOGIN_ID(response); 671 lu->login_id = LOGIN_RESPONSE_GET_LOGIN_ID(response);
613 672
614 fw_notify("logged in to sbp2 unit %s (%d retries)\n", 673 fw_notify("logged in to %s LUN %04x (%d retries)\n",
615 unit->device.bus_id, sd->retries); 674 unit->device.bus_id, lu->lun, lu->retries);
616 fw_notify(" - management_agent_address: 0x%012llx\n",
617 (unsigned long long) sd->management_agent_address);
618 fw_notify(" - command_block_agent_address: 0x%012llx\n",
619 (unsigned long long) sd->command_block_agent_address);
620 fw_notify(" - status write address: 0x%012llx\n",
621 (unsigned long long) sd->address_handler.offset);
622 675
623#if 0 676#if 0
624 /* FIXME: The linux1394 sbp2 does this last step. */ 677 /* FIXME: The linux1394 sbp2 does this last step. */
625 sbp2_set_busy_timeout(scsi_id); 678 sbp2_set_busy_timeout(scsi_id);
626#endif 679#endif
627 680
628 PREPARE_DELAYED_WORK(&sd->work, sbp2_reconnect); 681 PREPARE_DELAYED_WORK(&lu->work, sbp2_reconnect);
629 sbp2_agent_reset(unit); 682 sbp2_agent_reset(lu);
683
684 memset(&eight_bytes_lun, 0, sizeof(eight_bytes_lun));
685 eight_bytes_lun.scsi_lun[0] = (lu->lun >> 8) & 0xff;
686 eight_bytes_lun.scsi_lun[1] = lu->lun & 0xff;
630 687
631 /* FIXME: Loop over luns here. */ 688 sdev = __scsi_add_device(shost, 0, 0,
632 lun = 0; 689 scsilun_to_int(&eight_bytes_lun), lu);
633 retval = scsi_add_device(host, 0, 0, lun); 690 if (IS_ERR(sdev)) {
634 if (retval < 0) { 691 sbp2_send_management_orb(lu, node_id, generation,
635 sbp2_send_management_orb(unit, sd->node_id, sd->generation, 692 SBP2_LOGOUT_REQUEST, lu->login_id, NULL);
636 SBP2_LOGOUT_REQUEST, sd->login_id,
637 NULL);
638 /* 693 /*
639 * Set this back to sbp2_login so we fall back and 694 * Set this back to sbp2_login so we fall back and
640 * retry login on bus reset. 695 * retry login on bus reset.
641 */ 696 */
642 PREPARE_DELAYED_WORK(&sd->work, sbp2_login); 697 PREPARE_DELAYED_WORK(&lu->work, sbp2_login);
698 } else {
699 lu->sdev = sdev;
700 scsi_device_put(sdev);
643 } 701 }
644 kref_put(&sd->kref, release_sbp2_device); 702 kref_put(&lu->tgt->kref, sbp2_release_target);
645} 703}
646 704
647static int sbp2_probe(struct device *dev) 705static int sbp2_add_logical_unit(struct sbp2_target *tgt, int lun_entry)
648{ 706{
649 struct fw_unit *unit = fw_unit(dev); 707 struct sbp2_logical_unit *lu;
650 struct fw_device *device = fw_device(unit->device.parent);
651 struct sbp2_device *sd;
652 struct fw_csr_iterator ci;
653 struct Scsi_Host *host;
654 int i, key, value, err;
655 u32 model, firmware_revision;
656 708
657 err = -ENOMEM; 709 lu = kmalloc(sizeof(*lu), GFP_KERNEL);
658 host = scsi_host_alloc(&scsi_driver_template, sizeof(*sd)); 710 if (!lu)
659 if (host == NULL) 711 return -ENOMEM;
660 goto fail;
661 712
662 sd = (struct sbp2_device *) host->hostdata; 713 lu->address_handler.length = 0x100;
663 unit->device.driver_data = sd; 714 lu->address_handler.address_callback = sbp2_status_write;
664 sd->unit = unit; 715 lu->address_handler.callback_data = lu;
665 INIT_LIST_HEAD(&sd->orb_list);
666 kref_init(&sd->kref);
667 716
668 sd->address_handler.length = 0x100; 717 if (fw_core_add_address_handler(&lu->address_handler,
669 sd->address_handler.address_callback = sbp2_status_write; 718 &fw_high_memory_region) < 0) {
670 sd->address_handler.callback_data = sd; 719 kfree(lu);
720 return -ENOMEM;
721 }
671 722
672 err = fw_core_add_address_handler(&sd->address_handler, 723 lu->tgt = tgt;
673 &fw_high_memory_region); 724 lu->sdev = NULL;
674 if (err < 0) 725 lu->lun = lun_entry & 0xffff;
675 goto fail_host; 726 lu->retries = 0;
727 INIT_LIST_HEAD(&lu->orb_list);
728 INIT_DELAYED_WORK(&lu->work, sbp2_login);
676 729
677 err = fw_device_enable_phys_dma(device); 730 list_add_tail(&lu->link, &tgt->lu_list);
678 if (err < 0) 731 return 0;
679 goto fail_address_handler; 732}
680 733
681 err = scsi_add_host(host, &unit->device); 734static int sbp2_scan_logical_unit_dir(struct sbp2_target *tgt, u32 *directory)
682 if (err < 0) 735{
683 goto fail_address_handler; 736 struct fw_csr_iterator ci;
737 int key, value;
684 738
685 /* 739 fw_csr_iterator_init(&ci, directory);
686 * Scan unit directory to get management agent address, 740 while (fw_csr_iterator_next(&ci, &key, &value))
687 * firmware revison and model. Initialize firmware_revision 741 if (key == SBP2_CSR_LOGICAL_UNIT_NUMBER &&
688 * and model to values that wont match anything in our table. 742 sbp2_add_logical_unit(tgt, value) < 0)
689 */ 743 return -ENOMEM;
690 firmware_revision = 0xff000000; 744 return 0;
691 model = 0xff000000; 745}
692 fw_csr_iterator_init(&ci, unit->directory); 746
747static int sbp2_scan_unit_dir(struct sbp2_target *tgt, u32 *directory,
748 u32 *model, u32 *firmware_revision)
749{
750 struct fw_csr_iterator ci;
751 int key, value;
752
753 fw_csr_iterator_init(&ci, directory);
693 while (fw_csr_iterator_next(&ci, &key, &value)) { 754 while (fw_csr_iterator_next(&ci, &key, &value)) {
694 switch (key) { 755 switch (key) {
756
695 case CSR_DEPENDENT_INFO | CSR_OFFSET: 757 case CSR_DEPENDENT_INFO | CSR_OFFSET:
696 sd->management_agent_address = 758 tgt->management_agent_address =
697 0xfffff0000000ULL + 4 * value; 759 CSR_REGISTER_BASE + 4 * value;
698 break; 760 break;
699 case SBP2_FIRMWARE_REVISION: 761
700 firmware_revision = value; 762 case CSR_DIRECTORY_ID:
763 tgt->directory_id = value;
701 break; 764 break;
765
702 case CSR_MODEL: 766 case CSR_MODEL:
703 model = value; 767 *model = value;
768 break;
769
770 case SBP2_CSR_FIRMWARE_REVISION:
771 *firmware_revision = value;
772 break;
773
774 case SBP2_CSR_LOGICAL_UNIT_NUMBER:
775 if (sbp2_add_logical_unit(tgt, value) < 0)
776 return -ENOMEM;
777 break;
778
779 case SBP2_CSR_LOGICAL_UNIT_DIRECTORY:
780 if (sbp2_scan_logical_unit_dir(tgt, ci.p + value) < 0)
781 return -ENOMEM;
704 break; 782 break;
705 } 783 }
706 } 784 }
785 return 0;
786}
787
788static void sbp2_init_workarounds(struct sbp2_target *tgt, u32 model,
789 u32 firmware_revision)
790{
791 int i;
792 unsigned w = sbp2_param_workarounds;
793
794 if (w)
795 fw_notify("Please notify linux1394-devel@lists.sourceforge.net "
796 "if you need the workarounds parameter for %s\n",
797 tgt->unit->device.bus_id);
798
799 if (w & SBP2_WORKAROUND_OVERRIDE)
800 goto out;
707 801
708 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) { 802 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
803
709 if (sbp2_workarounds_table[i].firmware_revision != 804 if (sbp2_workarounds_table[i].firmware_revision !=
710 (firmware_revision & 0xffffff00)) 805 (firmware_revision & 0xffffff00))
711 continue; 806 continue;
807
712 if (sbp2_workarounds_table[i].model != model && 808 if (sbp2_workarounds_table[i].model != model &&
713 sbp2_workarounds_table[i].model != ~0) 809 sbp2_workarounds_table[i].model != ~0)
714 continue; 810 continue;
715 sd->workarounds |= sbp2_workarounds_table[i].workarounds; 811
812 w |= sbp2_workarounds_table[i].workarounds;
716 break; 813 break;
717 } 814 }
718 815 out:
719 if (sd->workarounds) 816 if (w)
720 fw_notify("Workarounds for node %s: 0x%x " 817 fw_notify("Workarounds for %s: 0x%x "
721 "(firmware_revision 0x%06x, model_id 0x%06x)\n", 818 "(firmware_revision 0x%06x, model_id 0x%06x)\n",
722 unit->device.bus_id, 819 tgt->unit->device.bus_id,
723 sd->workarounds, firmware_revision, model); 820 w, firmware_revision, model);
821 tgt->workarounds = w;
822}
823
824static struct scsi_host_template scsi_driver_template;
825
826static int sbp2_probe(struct device *dev)
827{
828 struct fw_unit *unit = fw_unit(dev);
829 struct fw_device *device = fw_device(unit->device.parent);
830 struct sbp2_target *tgt;
831 struct sbp2_logical_unit *lu;
832 struct Scsi_Host *shost;
833 u32 model, firmware_revision;
834
835 shost = scsi_host_alloc(&scsi_driver_template, sizeof(*tgt));
836 if (shost == NULL)
837 return -ENOMEM;
838
839 tgt = (struct sbp2_target *)shost->hostdata;
840 unit->device.driver_data = tgt;
841 tgt->unit = unit;
842 kref_init(&tgt->kref);
843 INIT_LIST_HEAD(&tgt->lu_list);
844
845 if (fw_device_enable_phys_dma(device) < 0)
846 goto fail_shost_put;
847
848 if (scsi_add_host(shost, &unit->device) < 0)
849 goto fail_shost_put;
850
851 /* Initialize to values that won't match anything in our table. */
852 firmware_revision = 0xff000000;
853 model = 0xff000000;
854
855 /* implicit directory ID */
856 tgt->directory_id = ((unit->directory - device->config_rom) * 4
857 + CSR_CONFIG_ROM) & 0xffffff;
858
859 if (sbp2_scan_unit_dir(tgt, unit->directory, &model,
860 &firmware_revision) < 0)
861 goto fail_tgt_put;
862
863 sbp2_init_workarounds(tgt, model, firmware_revision);
724 864
725 get_device(&unit->device); 865 get_device(&unit->device);
726 866
@@ -729,35 +869,34 @@ static int sbp2_probe(struct device *dev)
729 * reschedule retries. Always get the ref before scheduling 869 * reschedule retries. Always get the ref before scheduling
730 * work. 870 * work.
731 */ 871 */
732 INIT_DELAYED_WORK(&sd->work, sbp2_login); 872 list_for_each_entry(lu, &tgt->lu_list, link)
733 if (schedule_delayed_work(&sd->work, 0)) 873 if (queue_delayed_work(sbp2_wq, &lu->work, 0))
734 kref_get(&sd->kref); 874 kref_get(&tgt->kref);
735
736 return 0; 875 return 0;
737 876
738 fail_address_handler: 877 fail_tgt_put:
739 fw_core_remove_address_handler(&sd->address_handler); 878 kref_put(&tgt->kref, sbp2_release_target);
740 fail_host: 879 return -ENOMEM;
741 scsi_host_put(host); 880
742 fail: 881 fail_shost_put:
743 return err; 882 scsi_host_put(shost);
883 return -ENOMEM;
744} 884}
745 885
746static int sbp2_remove(struct device *dev) 886static int sbp2_remove(struct device *dev)
747{ 887{
748 struct fw_unit *unit = fw_unit(dev); 888 struct fw_unit *unit = fw_unit(dev);
749 struct sbp2_device *sd = unit->device.driver_data; 889 struct sbp2_target *tgt = unit->device.driver_data;
750
751 kref_put(&sd->kref, release_sbp2_device);
752 890
891 kref_put(&tgt->kref, sbp2_release_target);
753 return 0; 892 return 0;
754} 893}
755 894
756static void sbp2_reconnect(struct work_struct *work) 895static void sbp2_reconnect(struct work_struct *work)
757{ 896{
758 struct sbp2_device *sd = 897 struct sbp2_logical_unit *lu =
759 container_of(work, struct sbp2_device, work.work); 898 container_of(work, struct sbp2_logical_unit, work.work);
760 struct fw_unit *unit = sd->unit; 899 struct fw_unit *unit = lu->tgt->unit;
761 struct fw_device *device = fw_device(unit->device.parent); 900 struct fw_device *device = fw_device(unit->device.parent);
762 int generation, node_id, local_node_id; 901 int generation, node_id, local_node_id;
763 902
@@ -765,40 +904,49 @@ static void sbp2_reconnect(struct work_struct *work)
765 node_id = device->node->node_id; 904 node_id = device->node->node_id;
766 local_node_id = device->card->local_node->node_id; 905 local_node_id = device->card->local_node->node_id;
767 906
768 if (sbp2_send_management_orb(unit, node_id, generation, 907 if (sbp2_send_management_orb(lu, node_id, generation,
769 SBP2_RECONNECT_REQUEST, 908 SBP2_RECONNECT_REQUEST,
770 sd->login_id, NULL) < 0) { 909 lu->login_id, NULL) < 0) {
771 if (sd->retries++ >= 5) { 910 if (lu->retries++ >= 5) {
772 fw_error("failed to reconnect to %s\n", 911 fw_error("failed to reconnect to %s\n",
773 unit->device.bus_id); 912 unit->device.bus_id);
774 /* Fall back and try to log in again. */ 913 /* Fall back and try to log in again. */
775 sd->retries = 0; 914 lu->retries = 0;
776 PREPARE_DELAYED_WORK(&sd->work, sbp2_login); 915 PREPARE_DELAYED_WORK(&lu->work, sbp2_login);
777 } 916 }
778 schedule_delayed_work(&sd->work, DIV_ROUND_UP(HZ, 5)); 917 queue_delayed_work(sbp2_wq, &lu->work, DIV_ROUND_UP(HZ, 5));
779 return; 918 return;
780 } 919 }
781 920
782 sd->generation = generation; 921 lu->generation = generation;
783 sd->node_id = node_id; 922 lu->tgt->node_id = node_id;
784 sd->address_high = local_node_id << 16; 923 lu->tgt->address_high = local_node_id << 16;
785 924
786 fw_notify("reconnected to unit %s (%d retries)\n", 925 fw_notify("reconnected to %s LUN %04x (%d retries)\n",
787 unit->device.bus_id, sd->retries); 926 unit->device.bus_id, lu->lun, lu->retries);
788 sbp2_agent_reset(unit); 927
789 sbp2_cancel_orbs(unit); 928 sbp2_agent_reset(lu);
790 kref_put(&sd->kref, release_sbp2_device); 929 sbp2_cancel_orbs(lu);
930
931 kref_put(&lu->tgt->kref, sbp2_release_target);
791} 932}
792 933
793static void sbp2_update(struct fw_unit *unit) 934static void sbp2_update(struct fw_unit *unit)
794{ 935{
795 struct fw_device *device = fw_device(unit->device.parent); 936 struct sbp2_target *tgt = unit->device.driver_data;
796 struct sbp2_device *sd = unit->device.driver_data; 937 struct sbp2_logical_unit *lu;
797 938
798 sd->retries = 0; 939 fw_device_enable_phys_dma(fw_device(unit->device.parent));
799 fw_device_enable_phys_dma(device); 940
800 if (schedule_delayed_work(&sd->work, 0)) 941 /*
801 kref_get(&sd->kref); 942 * Fw-core serializes sbp2_update() against sbp2_remove().
943 * Iteration over tgt->lu_list is therefore safe here.
944 */
945 list_for_each_entry(lu, &tgt->lu_list, link) {
946 lu->retries = 0;
947 if (queue_delayed_work(sbp2_wq, &lu->work, 0))
948 kref_get(&tgt->kref);
949 }
802} 950}
803 951
804#define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e 952#define SBP2_UNIT_SPEC_ID_ENTRY 0x0000609e
@@ -868,13 +1016,12 @@ complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
868{ 1016{
869 struct sbp2_command_orb *orb = 1017 struct sbp2_command_orb *orb =
870 container_of(base_orb, struct sbp2_command_orb, base); 1018 container_of(base_orb, struct sbp2_command_orb, base);
871 struct fw_unit *unit = orb->unit; 1019 struct fw_device *device = fw_device(orb->lu->tgt->unit->device.parent);
872 struct fw_device *device = fw_device(unit->device.parent);
873 int result; 1020 int result;
874 1021
875 if (status != NULL) { 1022 if (status != NULL) {
876 if (STATUS_GET_DEAD(*status)) 1023 if (STATUS_GET_DEAD(*status))
877 sbp2_agent_reset(unit); 1024 sbp2_agent_reset(orb->lu);
878 1025
879 switch (STATUS_GET_RESPONSE(*status)) { 1026 switch (STATUS_GET_RESPONSE(*status)) {
880 case SBP2_STATUS_REQUEST_COMPLETE: 1027 case SBP2_STATUS_REQUEST_COMPLETE:
@@ -918,12 +1065,10 @@ complete_command_orb(struct sbp2_orb *base_orb, struct sbp2_status *status)
918 orb->done(orb->cmd); 1065 orb->done(orb->cmd);
919} 1066}
920 1067
921static int sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb) 1068static int
1069sbp2_map_scatterlist(struct sbp2_command_orb *orb, struct fw_device *device,
1070 struct sbp2_logical_unit *lu)
922{ 1071{
923 struct sbp2_device *sd =
924 (struct sbp2_device *)orb->cmd->device->host->hostdata;
925 struct fw_unit *unit = sd->unit;
926 struct fw_device *device = fw_device(unit->device.parent);
927 struct scatterlist *sg; 1072 struct scatterlist *sg;
928 int sg_len, l, i, j, count; 1073 int sg_len, l, i, j, count;
929 dma_addr_t sg_addr; 1074 dma_addr_t sg_addr;
@@ -942,10 +1087,9 @@ static int sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
942 * tables. 1087 * tables.
943 */ 1088 */
944 if (count == 1 && sg_dma_len(sg) < SBP2_MAX_SG_ELEMENT_LENGTH) { 1089 if (count == 1 && sg_dma_len(sg) < SBP2_MAX_SG_ELEMENT_LENGTH) {
945 orb->request.data_descriptor.high = sd->address_high; 1090 orb->request.data_descriptor.high = lu->tgt->address_high;
946 orb->request.data_descriptor.low = sg_dma_address(sg); 1091 orb->request.data_descriptor.low = sg_dma_address(sg);
947 orb->request.misc |= 1092 orb->request.misc |= COMMAND_ORB_DATA_SIZE(sg_dma_len(sg));
948 COMMAND_ORB_DATA_SIZE(sg_dma_len(sg));
949 return 0; 1093 return 0;
950 } 1094 }
951 1095
@@ -989,7 +1133,7 @@ static int sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
989 * initiator (i.e. us), but data_descriptor can refer to data 1133 * initiator (i.e. us), but data_descriptor can refer to data
990 * on other nodes so we need to put our ID in descriptor.high. 1134 * on other nodes so we need to put our ID in descriptor.high.
991 */ 1135 */
992 orb->request.data_descriptor.high = sd->address_high; 1136 orb->request.data_descriptor.high = lu->tgt->address_high;
993 orb->request.data_descriptor.low = orb->page_table_bus; 1137 orb->request.data_descriptor.low = orb->page_table_bus;
994 orb->request.misc |= 1138 orb->request.misc |=
995 COMMAND_ORB_PAGE_TABLE_PRESENT | 1139 COMMAND_ORB_PAGE_TABLE_PRESENT |
@@ -1008,12 +1152,11 @@ static int sbp2_command_orb_map_scatterlist(struct sbp2_command_orb *orb)
1008 1152
1009static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done) 1153static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
1010{ 1154{
1011 struct sbp2_device *sd = 1155 struct sbp2_logical_unit *lu = cmd->device->hostdata;
1012 (struct sbp2_device *)cmd->device->host->hostdata; 1156 struct fw_device *device = fw_device(lu->tgt->unit->device.parent);
1013 struct fw_unit *unit = sd->unit;
1014 struct fw_device *device = fw_device(unit->device.parent);
1015 struct sbp2_command_orb *orb; 1157 struct sbp2_command_orb *orb;
1016 unsigned max_payload; 1158 unsigned max_payload;
1159 int retval = SCSI_MLQUEUE_HOST_BUSY;
1017 1160
1018 /* 1161 /*
1019 * Bidirectional commands are not yet implemented, and unknown 1162 * Bidirectional commands are not yet implemented, and unknown
@@ -1029,14 +1172,14 @@ static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
1029 orb = kzalloc(sizeof(*orb), GFP_ATOMIC); 1172 orb = kzalloc(sizeof(*orb), GFP_ATOMIC);
1030 if (orb == NULL) { 1173 if (orb == NULL) {
1031 fw_notify("failed to alloc orb\n"); 1174 fw_notify("failed to alloc orb\n");
1032 goto fail_alloc; 1175 return SCSI_MLQUEUE_HOST_BUSY;
1033 } 1176 }
1034 1177
1035 /* Initialize rcode to something not RCODE_COMPLETE. */ 1178 /* Initialize rcode to something not RCODE_COMPLETE. */
1036 orb->base.rcode = -1; 1179 orb->base.rcode = -1;
1037 kref_init(&orb->base.kref); 1180 kref_init(&orb->base.kref);
1038 1181
1039 orb->unit = unit; 1182 orb->lu = lu;
1040 orb->done = done; 1183 orb->done = done;
1041 orb->cmd = cmd; 1184 orb->cmd = cmd;
1042 1185
@@ -1062,8 +1205,8 @@ static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
1062 orb->request.misc |= 1205 orb->request.misc |=
1063 COMMAND_ORB_DIRECTION(SBP2_DIRECTION_TO_MEDIA); 1206 COMMAND_ORB_DIRECTION(SBP2_DIRECTION_TO_MEDIA);
1064 1207
1065 if (scsi_sg_count(cmd) && sbp2_command_orb_map_scatterlist(orb) < 0) 1208 if (scsi_sg_count(cmd) && sbp2_map_scatterlist(orb, device, lu) < 0)
1066 goto fail_mapping; 1209 goto out;
1067 1210
1068 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request)); 1211 fw_memcpy_to_be32(&orb->request, &orb->request, sizeof(orb->request));
1069 1212
@@ -1076,49 +1219,47 @@ static int sbp2_scsi_queuecommand(struct scsi_cmnd *cmd, scsi_done_fn_t done)
1076 dma_map_single(device->card->device, &orb->request, 1219 dma_map_single(device->card->device, &orb->request,
1077 sizeof(orb->request), DMA_TO_DEVICE); 1220 sizeof(orb->request), DMA_TO_DEVICE);
1078 if (dma_mapping_error(orb->base.request_bus)) 1221 if (dma_mapping_error(orb->base.request_bus))
1079 goto fail_mapping; 1222 goto out;
1080
1081 sbp2_send_orb(&orb->base, unit, sd->node_id, sd->generation,
1082 sd->command_block_agent_address + SBP2_ORB_POINTER);
1083
1084 kref_put(&orb->base.kref, free_orb);
1085 return 0;
1086 1223
1087 fail_mapping: 1224 sbp2_send_orb(&orb->base, lu, lu->tgt->node_id, lu->generation,
1225 lu->command_block_agent_address + SBP2_ORB_POINTER);
1226 retval = 0;
1227 out:
1088 kref_put(&orb->base.kref, free_orb); 1228 kref_put(&orb->base.kref, free_orb);
1089 fail_alloc: 1229 return retval;
1090 return SCSI_MLQUEUE_HOST_BUSY;
1091} 1230}
1092 1231
1093static int sbp2_scsi_slave_alloc(struct scsi_device *sdev) 1232static int sbp2_scsi_slave_alloc(struct scsi_device *sdev)
1094{ 1233{
1095 struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata; 1234 struct sbp2_logical_unit *lu = sdev->hostdata;
1096 1235
1097 sdev->allow_restart = 1; 1236 sdev->allow_restart = 1;
1098 1237
1099 if (sd->workarounds & SBP2_WORKAROUND_INQUIRY_36) 1238 if (lu->tgt->workarounds & SBP2_WORKAROUND_INQUIRY_36)
1100 sdev->inquiry_len = 36; 1239 sdev->inquiry_len = 36;
1240
1101 return 0; 1241 return 0;
1102} 1242}
1103 1243
1104static int sbp2_scsi_slave_configure(struct scsi_device *sdev) 1244static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
1105{ 1245{
1106 struct sbp2_device *sd = (struct sbp2_device *)sdev->host->hostdata; 1246 struct sbp2_logical_unit *lu = sdev->hostdata;
1107 struct fw_unit *unit = sd->unit;
1108 1247
1109 sdev->use_10_for_rw = 1; 1248 sdev->use_10_for_rw = 1;
1110 1249
1111 if (sdev->type == TYPE_ROM) 1250 if (sdev->type == TYPE_ROM)
1112 sdev->use_10_for_ms = 1; 1251 sdev->use_10_for_ms = 1;
1252
1113 if (sdev->type == TYPE_DISK && 1253 if (sdev->type == TYPE_DISK &&
1114 sd->workarounds & SBP2_WORKAROUND_MODE_SENSE_8) 1254 lu->tgt->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
1115 sdev->skip_ms_page_8 = 1; 1255 sdev->skip_ms_page_8 = 1;
1116 if (sd->workarounds & SBP2_WORKAROUND_FIX_CAPACITY) { 1256
1117 fw_notify("setting fix_capacity for %s\n", unit->device.bus_id); 1257 if (lu->tgt->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
1118 sdev->fix_capacity = 1; 1258 sdev->fix_capacity = 1;
1119 } 1259
1120 if (sd->workarounds & SBP2_WORKAROUND_128K_MAX_TRANS) 1260 if (lu->tgt->workarounds & SBP2_WORKAROUND_128K_MAX_TRANS)
1121 blk_queue_max_sectors(sdev->request_queue, 128 * 1024 / 512); 1261 blk_queue_max_sectors(sdev->request_queue, 128 * 1024 / 512);
1262
1122 return 0; 1263 return 0;
1123} 1264}
1124 1265
@@ -1128,13 +1269,11 @@ static int sbp2_scsi_slave_configure(struct scsi_device *sdev)
1128 */ 1269 */
1129static int sbp2_scsi_abort(struct scsi_cmnd *cmd) 1270static int sbp2_scsi_abort(struct scsi_cmnd *cmd)
1130{ 1271{
1131 struct sbp2_device *sd = 1272 struct sbp2_logical_unit *lu = cmd->device->hostdata;
1132 (struct sbp2_device *)cmd->device->host->hostdata;
1133 struct fw_unit *unit = sd->unit;
1134 1273
1135 fw_notify("sbp2_scsi_abort\n"); 1274 fw_notify("sbp2_scsi_abort\n");
1136 sbp2_agent_reset(unit); 1275 sbp2_agent_reset(lu);
1137 sbp2_cancel_orbs(unit); 1276 sbp2_cancel_orbs(lu);
1138 1277
1139 return SUCCESS; 1278 return SUCCESS;
1140} 1279}
@@ -1151,37 +1290,18 @@ sbp2_sysfs_ieee1394_id_show(struct device *dev, struct device_attribute *attr,
1151 char *buf) 1290 char *buf)
1152{ 1291{
1153 struct scsi_device *sdev = to_scsi_device(dev); 1292 struct scsi_device *sdev = to_scsi_device(dev);
1154 struct sbp2_device *sd; 1293 struct sbp2_logical_unit *lu;
1155 struct fw_unit *unit;
1156 struct fw_device *device; 1294 struct fw_device *device;
1157 u32 directory_id;
1158 struct fw_csr_iterator ci;
1159 int key, value, lun;
1160 1295
1161 if (!sdev) 1296 if (!sdev)
1162 return 0; 1297 return 0;
1163 sd = (struct sbp2_device *)sdev->host->hostdata;
1164 unit = sd->unit;
1165 device = fw_device(unit->device.parent);
1166
1167 /* implicit directory ID */
1168 directory_id = ((unit->directory - device->config_rom) * 4
1169 + CSR_CONFIG_ROM) & 0xffffff;
1170
1171 /* explicit directory ID, overrides implicit ID if present */
1172 fw_csr_iterator_init(&ci, unit->directory);
1173 while (fw_csr_iterator_next(&ci, &key, &value))
1174 if (key == CSR_DIRECTORY_ID) {
1175 directory_id = value;
1176 break;
1177 }
1178 1298
1179 /* FIXME: Make this work for multi-lun devices. */ 1299 lu = sdev->hostdata;
1180 lun = 0; 1300 device = fw_device(lu->tgt->unit->device.parent);
1181 1301
1182 return sprintf(buf, "%08x%08x:%06x:%04x\n", 1302 return sprintf(buf, "%08x%08x:%06x:%04x\n",
1183 device->config_rom[3], device->config_rom[4], 1303 device->config_rom[3], device->config_rom[4],
1184 directory_id, lun); 1304 lu->tgt->directory_id, lu->lun);
1185} 1305}
1186 1306
1187static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL); 1307static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
@@ -1219,12 +1339,17 @@ MODULE_ALIAS("sbp2");
1219 1339
1220static int __init sbp2_init(void) 1340static int __init sbp2_init(void)
1221{ 1341{
1342 sbp2_wq = create_singlethread_workqueue(KBUILD_MODNAME);
1343 if (!sbp2_wq)
1344 return -ENOMEM;
1345
1222 return driver_register(&sbp2_driver.driver); 1346 return driver_register(&sbp2_driver.driver);
1223} 1347}
1224 1348
1225static void __exit sbp2_cleanup(void) 1349static void __exit sbp2_cleanup(void)
1226{ 1350{
1227 driver_unregister(&sbp2_driver.driver); 1351 driver_unregister(&sbp2_driver.driver);
1352 destroy_workqueue(sbp2_wq);
1228} 1353}
1229 1354
1230module_init(sbp2_init); 1355module_init(sbp2_init);
diff --git a/drivers/firewire/fw-topology.c b/drivers/firewire/fw-topology.c
index 39e5cd12aa52..0fc9b000e99d 100644
--- a/drivers/firewire/fw-topology.c
+++ b/drivers/firewire/fw-topology.c
@@ -152,6 +152,10 @@ static void update_hop_count(struct fw_node *node)
152 node->max_hops = max(max_child_hops, depths[0] + depths[1] + 2); 152 node->max_hops = max(max_child_hops, depths[0] + depths[1] + 2);
153} 153}
154 154
155static inline struct fw_node *fw_node(struct list_head *l)
156{
157 return list_entry(l, struct fw_node, link);
158}
155 159
156/** 160/**
157 * build_tree - Build the tree representation of the topology 161 * build_tree - Build the tree representation of the topology
@@ -162,7 +166,7 @@ static void update_hop_count(struct fw_node *node)
162 * This function builds the tree representation of the topology given 166 * This function builds the tree representation of the topology given
163 * by the self IDs from the latest bus reset. During the construction 167 * by the self IDs from the latest bus reset. During the construction
164 * of the tree, the function checks that the self IDs are valid and 168 * of the tree, the function checks that the self IDs are valid and
165 * internally consistent. On succcess this funtions returns the 169 * internally consistent. On succcess this function returns the
166 * fw_node corresponding to the local card otherwise NULL. 170 * fw_node corresponding to the local card otherwise NULL.
167 */ 171 */
168static struct fw_node *build_tree(struct fw_card *card, 172static struct fw_node *build_tree(struct fw_card *card,
@@ -211,6 +215,10 @@ static struct fw_node *build_tree(struct fw_card *card,
211 */ 215 */
212 for (i = 0, h = &stack; i < child_port_count; i++) 216 for (i = 0, h = &stack; i < child_port_count; i++)
213 h = h->prev; 217 h = h->prev;
218 /*
219 * When the stack is empty, this yields an invalid value,
220 * but that pointer will never be dereferenced.
221 */
214 child = fw_node(h); 222 child = fw_node(h);
215 223
216 node = fw_node_create(q, port_count, card->color); 224 node = fw_node_create(q, port_count, card->color);
diff --git a/drivers/firewire/fw-topology.h b/drivers/firewire/fw-topology.h
index 1b56b4ac7fb2..cedc1ec906e9 100644
--- a/drivers/firewire/fw-topology.h
+++ b/drivers/firewire/fw-topology.h
@@ -51,12 +51,6 @@ struct fw_node {
51}; 51};
52 52
53static inline struct fw_node * 53static inline struct fw_node *
54fw_node(struct list_head *l)
55{
56 return list_entry(l, struct fw_node, link);
57}
58
59static inline struct fw_node *
60fw_node_get(struct fw_node *node) 54fw_node_get(struct fw_node *node)
61{ 55{
62 atomic_inc(&node->ref_count); 56 atomic_inc(&node->ref_count);
diff --git a/drivers/firewire/fw-transaction.c b/drivers/firewire/fw-transaction.c
index 3e1cb12e43cd..9959b799dbe2 100644
--- a/drivers/firewire/fw-transaction.c
+++ b/drivers/firewire/fw-transaction.c
@@ -410,7 +410,12 @@ EXPORT_SYMBOL(fw_unit_space_region);
410 * controller. When a request is received that falls within the 410 * controller. When a request is received that falls within the
411 * specified address range, the specified callback is invoked. The 411 * specified address range, the specified callback is invoked. The
412 * parameters passed to the callback give the details of the 412 * parameters passed to the callback give the details of the
413 * particular request 413 * particular request.
414 *
415 * Return value: 0 on success, non-zero otherwise.
416 * The start offset of the handler's address region is determined by
417 * fw_core_add_address_handler() and is returned in handler->offset.
418 * The offset is quadlet-aligned.
414 */ 419 */
415int 420int
416fw_core_add_address_handler(struct fw_address_handler *handler, 421fw_core_add_address_handler(struct fw_address_handler *handler,
@@ -422,14 +427,15 @@ fw_core_add_address_handler(struct fw_address_handler *handler,
422 427
423 spin_lock_irqsave(&address_handler_lock, flags); 428 spin_lock_irqsave(&address_handler_lock, flags);
424 429
425 handler->offset = region->start; 430 handler->offset = roundup(region->start, 4);
426 while (handler->offset + handler->length <= region->end) { 431 while (handler->offset + handler->length <= region->end) {
427 other = 432 other =
428 lookup_overlapping_address_handler(&address_handler_list, 433 lookup_overlapping_address_handler(&address_handler_list,
429 handler->offset, 434 handler->offset,
430 handler->length); 435 handler->length);
431 if (other != NULL) { 436 if (other != NULL) {
432 handler->offset += other->length; 437 handler->offset =
438 roundup(other->offset + other->length, 4);
433 } else { 439 } else {
434 list_add_tail(&handler->link, &address_handler_list); 440 list_add_tail(&handler->link, &address_handler_list);
435 ret = 0; 441 ret = 0;