aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/ieee1394/sbp2.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/ieee1394/sbp2.c')
-rw-r--r--drivers/ieee1394/sbp2.c2192
1 files changed, 818 insertions, 1374 deletions
diff --git a/drivers/ieee1394/sbp2.c b/drivers/ieee1394/sbp2.c
index 6986ac188281..e68b80b7340d 100644
--- a/drivers/ieee1394/sbp2.c
+++ b/drivers/ieee1394/sbp2.c
@@ -29,13 +29,26 @@
29 * driver. It also registers as a SCSI lower-level driver in order to accept 29 * driver. It also registers as a SCSI lower-level driver in order to accept
30 * SCSI commands for transport using SBP-2. 30 * SCSI commands for transport using SBP-2.
31 * 31 *
32 * You may access any attached SBP-2 storage devices as if they were SCSI 32 * You may access any attached SBP-2 (usually storage devices) as regular
33 * devices (e.g. mount /dev/sda1, fdisk, mkfs, etc.). 33 * SCSI devices. E.g. mount /dev/sda1, fdisk, mkfs, etc..
34 * 34 *
35 * Current Issues: 35 * See http://www.t10.org/drafts.htm#sbp2 for the final draft of the SBP-2
36 * specification and for where to purchase the official standard.
36 * 37 *
37 * - Error Handling: SCSI aborts and bus reset requests are handled somewhat 38 * TODO:
38 * but the code needs additional debugging. 39 * - look into possible improvements of the SCSI error handlers
40 * - handle Unit_Characteristics.mgt_ORB_timeout and .ORB_size
41 * - handle Logical_Unit_Number.ordered
42 * - handle src == 1 in status blocks
43 * - reimplement the DMA mapping in absence of physical DMA so that
44 * bus_to_virt is no longer required
45 * - debug the handling of absent physical DMA
46 * - replace CONFIG_IEEE1394_SBP2_PHYS_DMA by automatic detection
47 * (this is easy but depends on the previous two TODO items)
48 * - make the parameter serialize_io configurable per device
49 * - move all requests to fetch agent registers into non-atomic context,
50 * replace all usages of sbp2util_node_write_no_wait by true transactions
51 * Grep for inline FIXME comments below.
39 */ 52 */
40 53
41#include <linux/blkdev.h> 54#include <linux/blkdev.h>
@@ -49,7 +62,6 @@
49#include <linux/list.h> 62#include <linux/list.h>
50#include <linux/module.h> 63#include <linux/module.h>
51#include <linux/moduleparam.h> 64#include <linux/moduleparam.h>
52#include <linux/pci.h>
53#include <linux/slab.h> 65#include <linux/slab.h>
54#include <linux/spinlock.h> 66#include <linux/spinlock.h>
55#include <linux/stat.h> 67#include <linux/stat.h>
@@ -98,20 +110,20 @@
98 * (probably due to PCI latency/throughput issues with the part). You can 110 * (probably due to PCI latency/throughput issues with the part). You can
99 * bump down the speed if you are running into problems. 111 * bump down the speed if you are running into problems.
100 */ 112 */
101static int max_speed = IEEE1394_SPEED_MAX; 113static int sbp2_max_speed = IEEE1394_SPEED_MAX;
102module_param(max_speed, int, 0644); 114module_param_named(max_speed, sbp2_max_speed, int, 0644);
103MODULE_PARM_DESC(max_speed, "Force max speed (3 = 800mb, 2 = 400mb, 1 = 200mb, 0 = 100mb)"); 115MODULE_PARM_DESC(max_speed, "Force max speed "
116 "(3 = 800Mb/s, 2 = 400Mb/s, 1 = 200Mb/s, 0 = 100Mb/s)");
104 117
105/* 118/*
106 * Set serialize_io to 1 if you'd like only one scsi command sent 119 * Set serialize_io to 1 if you'd like only one scsi command sent
107 * down to us at a time (debugging). This might be necessary for very 120 * down to us at a time (debugging). This might be necessary for very
108 * badly behaved sbp2 devices. 121 * badly behaved sbp2 devices.
109 *
110 * TODO: Make this configurable per device.
111 */ 122 */
112static int serialize_io = 1; 123static int sbp2_serialize_io = 1;
113module_param(serialize_io, int, 0444); 124module_param_named(serialize_io, sbp2_serialize_io, int, 0444);
114MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers (default = 1, faster = 0)"); 125MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers "
126 "(default = 1, faster = 0)");
115 127
116/* 128/*
117 * Bump up max_sectors if you'd like to support very large sized 129 * Bump up max_sectors if you'd like to support very large sized
@@ -121,10 +133,10 @@ MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers (default
121 * the Oxsemi sbp2 chipsets have no problems supporting very large 133 * the Oxsemi sbp2 chipsets have no problems supporting very large
122 * transfer sizes. 134 * transfer sizes.
123 */ 135 */
124static int max_sectors = SBP2_MAX_SECTORS; 136static int sbp2_max_sectors = SBP2_MAX_SECTORS;
125module_param(max_sectors, int, 0444); 137module_param_named(max_sectors, sbp2_max_sectors, int, 0444);
126MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported (default = " 138MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported "
127 __stringify(SBP2_MAX_SECTORS) ")"); 139 "(default = " __stringify(SBP2_MAX_SECTORS) ")");
128 140
129/* 141/*
130 * Exclusive login to sbp2 device? In most cases, the sbp2 driver should 142 * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
@@ -139,9 +151,10 @@ MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported (default = "
139 * concurrent logins. Depending on firmware, four or two concurrent logins 151 * concurrent logins. Depending on firmware, four or two concurrent logins
140 * are possible on OXFW911 and newer Oxsemi bridges. 152 * are possible on OXFW911 and newer Oxsemi bridges.
141 */ 153 */
142static int exclusive_login = 1; 154static int sbp2_exclusive_login = 1;
143module_param(exclusive_login, int, 0644); 155module_param_named(exclusive_login, sbp2_exclusive_login, int, 0644);
144MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device (default = 1)"); 156MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
157 "(default = 1)");
145 158
146/* 159/*
147 * If any of the following workarounds is required for your device to work, 160 * If any of the following workarounds is required for your device to work,
@@ -179,123 +192,123 @@ MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
179 ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE) 192 ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
180 ", or a combination)"); 193 ", or a combination)");
181 194
182/*
183 * Export information about protocols/devices supported by this driver.
184 */
185static struct ieee1394_device_id sbp2_id_table[] = {
186 {
187 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
188 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
189 .version = SBP2_SW_VERSION_ENTRY & 0xffffff},
190 {}
191};
192
193MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
194
195/*
196 * Debug levels, configured via kernel config, or enable here.
197 */
198
199#define CONFIG_IEEE1394_SBP2_DEBUG 0
200/* #define CONFIG_IEEE1394_SBP2_DEBUG_ORBS */
201/* #define CONFIG_IEEE1394_SBP2_DEBUG_DMA */
202/* #define CONFIG_IEEE1394_SBP2_DEBUG 1 */
203/* #define CONFIG_IEEE1394_SBP2_DEBUG 2 */
204/* #define CONFIG_IEEE1394_SBP2_PACKET_DUMP */
205
206#ifdef CONFIG_IEEE1394_SBP2_DEBUG_ORBS
207#define SBP2_ORB_DEBUG(fmt, args...) HPSB_ERR("sbp2(%s): "fmt, __FUNCTION__, ## args)
208static u32 global_outstanding_command_orbs = 0;
209#define outstanding_orb_incr global_outstanding_command_orbs++
210#define outstanding_orb_decr global_outstanding_command_orbs--
211#else
212#define SBP2_ORB_DEBUG(fmt, args...) do {} while (0)
213#define outstanding_orb_incr do {} while (0)
214#define outstanding_orb_decr do {} while (0)
215#endif
216
217#ifdef CONFIG_IEEE1394_SBP2_DEBUG_DMA
218#define SBP2_DMA_ALLOC(fmt, args...) \
219 HPSB_ERR("sbp2(%s)alloc(%d): "fmt, __FUNCTION__, \
220 ++global_outstanding_dmas, ## args)
221#define SBP2_DMA_FREE(fmt, args...) \
222 HPSB_ERR("sbp2(%s)free(%d): "fmt, __FUNCTION__, \
223 --global_outstanding_dmas, ## args)
224static u32 global_outstanding_dmas = 0;
225#else
226#define SBP2_DMA_ALLOC(fmt, args...) do {} while (0)
227#define SBP2_DMA_FREE(fmt, args...) do {} while (0)
228#endif
229 195
230#if CONFIG_IEEE1394_SBP2_DEBUG >= 2 196#define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
231#define SBP2_DEBUG(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args) 197#define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
232#define SBP2_INFO(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
233#define SBP2_NOTICE(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
234#define SBP2_WARN(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
235#elif CONFIG_IEEE1394_SBP2_DEBUG == 1
236#define SBP2_DEBUG(fmt, args...) HPSB_DEBUG("sbp2: "fmt, ## args)
237#define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
238#define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
239#define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
240#else
241#define SBP2_DEBUG(fmt, args...) do {} while (0)
242#define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
243#define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
244#define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
245#endif
246
247#define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
248#define SBP2_DEBUG_ENTER() SBP2_DEBUG("%s", __FUNCTION__)
249 198
250/* 199/*
251 * Globals 200 * Globals
252 */ 201 */
202static void sbp2scsi_complete_all_commands(struct sbp2_lu *, u32);
203static void sbp2scsi_complete_command(struct sbp2_lu *, u32, struct scsi_cmnd *,
204 void (*)(struct scsi_cmnd *));
205static struct sbp2_lu *sbp2_alloc_device(struct unit_directory *);
206static int sbp2_start_device(struct sbp2_lu *);
207static void sbp2_remove_device(struct sbp2_lu *);
208static int sbp2_login_device(struct sbp2_lu *);
209static int sbp2_reconnect_device(struct sbp2_lu *);
210static int sbp2_logout_device(struct sbp2_lu *);
211static void sbp2_host_reset(struct hpsb_host *);
212static int sbp2_handle_status_write(struct hpsb_host *, int, int, quadlet_t *,
213 u64, size_t, u16);
214static int sbp2_agent_reset(struct sbp2_lu *, int);
215static void sbp2_parse_unit_directory(struct sbp2_lu *,
216 struct unit_directory *);
217static int sbp2_set_busy_timeout(struct sbp2_lu *);
218static int sbp2_max_speed_and_size(struct sbp2_lu *);
253 219
254static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
255 u32 status);
256
257static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
258 u32 scsi_status, struct scsi_cmnd *SCpnt,
259 void (*done)(struct scsi_cmnd *));
260
261static struct scsi_host_template scsi_driver_template;
262 220
263static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC }; 221static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
264 222
265static void sbp2_host_reset(struct hpsb_host *host);
266
267static int sbp2_probe(struct device *dev);
268static int sbp2_remove(struct device *dev);
269static int sbp2_update(struct unit_directory *ud);
270
271static struct hpsb_highlevel sbp2_highlevel = { 223static struct hpsb_highlevel sbp2_highlevel = {
272 .name = SBP2_DEVICE_NAME, 224 .name = SBP2_DEVICE_NAME,
273 .host_reset = sbp2_host_reset, 225 .host_reset = sbp2_host_reset,
274}; 226};
275 227
276static struct hpsb_address_ops sbp2_ops = { 228static struct hpsb_address_ops sbp2_ops = {
277 .write = sbp2_handle_status_write 229 .write = sbp2_handle_status_write
278}; 230};
279 231
280#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA 232#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
233static int sbp2_handle_physdma_write(struct hpsb_host *, int, int, quadlet_t *,
234 u64, size_t, u16);
235static int sbp2_handle_physdma_read(struct hpsb_host *, int, quadlet_t *, u64,
236 size_t, u16);
237
281static struct hpsb_address_ops sbp2_physdma_ops = { 238static struct hpsb_address_ops sbp2_physdma_ops = {
282 .read = sbp2_handle_physdma_read, 239 .read = sbp2_handle_physdma_read,
283 .write = sbp2_handle_physdma_write, 240 .write = sbp2_handle_physdma_write,
284}; 241};
285#endif 242#endif
286 243
244
245/*
246 * Interface to driver core and IEEE 1394 core
247 */
248static struct ieee1394_device_id sbp2_id_table[] = {
249 {
250 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
251 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
252 .version = SBP2_SW_VERSION_ENTRY & 0xffffff},
253 {}
254};
255MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
256
257static int sbp2_probe(struct device *);
258static int sbp2_remove(struct device *);
259static int sbp2_update(struct unit_directory *);
260
287static struct hpsb_protocol_driver sbp2_driver = { 261static struct hpsb_protocol_driver sbp2_driver = {
288 .name = "SBP2 Driver", 262 .name = SBP2_DEVICE_NAME,
289 .id_table = sbp2_id_table, 263 .id_table = sbp2_id_table,
290 .update = sbp2_update, 264 .update = sbp2_update,
291 .driver = { 265 .driver = {
292 .name = SBP2_DEVICE_NAME,
293 .bus = &ieee1394_bus_type,
294 .probe = sbp2_probe, 266 .probe = sbp2_probe,
295 .remove = sbp2_remove, 267 .remove = sbp2_remove,
296 }, 268 },
297}; 269};
298 270
271
272/*
273 * Interface to SCSI core
274 */
275static int sbp2scsi_queuecommand(struct scsi_cmnd *,
276 void (*)(struct scsi_cmnd *));
277static int sbp2scsi_abort(struct scsi_cmnd *);
278static int sbp2scsi_reset(struct scsi_cmnd *);
279static int sbp2scsi_slave_alloc(struct scsi_device *);
280static int sbp2scsi_slave_configure(struct scsi_device *);
281static void sbp2scsi_slave_destroy(struct scsi_device *);
282static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *,
283 struct device_attribute *, char *);
284
285static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
286
287static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
288 &dev_attr_ieee1394_id,
289 NULL
290};
291
292static struct scsi_host_template sbp2_shost_template = {
293 .module = THIS_MODULE,
294 .name = "SBP-2 IEEE-1394",
295 .proc_name = SBP2_DEVICE_NAME,
296 .queuecommand = sbp2scsi_queuecommand,
297 .eh_abort_handler = sbp2scsi_abort,
298 .eh_device_reset_handler = sbp2scsi_reset,
299 .slave_alloc = sbp2scsi_slave_alloc,
300 .slave_configure = sbp2scsi_slave_configure,
301 .slave_destroy = sbp2scsi_slave_destroy,
302 .this_id = -1,
303 .sg_tablesize = SG_ALL,
304 .use_clustering = ENABLE_CLUSTERING,
305 .cmd_per_lun = SBP2_MAX_CMDS,
306 .can_queue = SBP2_MAX_CMDS,
307 .emulated = 1,
308 .sdev_attrs = sbp2_sysfs_sdev_attrs,
309};
310
311
299/* 312/*
300 * List of devices with known bugs. 313 * List of devices with known bugs.
301 * 314 *
@@ -363,8 +376,6 @@ static inline void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
363 376
364 for (length = (length >> 2); length--; ) 377 for (length = (length >> 2); length--; )
365 temp[length] = be32_to_cpu(temp[length]); 378 temp[length] = be32_to_cpu(temp[length]);
366
367 return;
368} 379}
369 380
370/* 381/*
@@ -376,8 +387,6 @@ static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
376 387
377 for (length = (length >> 2); length--; ) 388 for (length = (length >> 2); length--; )
378 temp[length] = cpu_to_be32(temp[length]); 389 temp[length] = cpu_to_be32(temp[length]);
379
380 return;
381} 390}
382#else /* BIG_ENDIAN */ 391#else /* BIG_ENDIAN */
383/* Why waste the cpu cycles? */ 392/* Why waste the cpu cycles? */
@@ -385,339 +394,246 @@ static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
385#define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0) 394#define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0)
386#endif 395#endif
387 396
388#ifdef CONFIG_IEEE1394_SBP2_PACKET_DUMP 397static DECLARE_WAIT_QUEUE_HEAD(sbp2_access_wq);
389/*
390 * Debug packet dump routine. Length is in bytes.
391 */
392static void sbp2util_packet_dump(void *buffer, int length, char *dump_name,
393 u32 dump_phys_addr)
394{
395 int i;
396 unsigned char *dump = buffer;
397
398 if (!dump || !length || !dump_name)
399 return;
400
401 if (dump_phys_addr)
402 printk("[%s, 0x%x]", dump_name, dump_phys_addr);
403 else
404 printk("[%s]", dump_name);
405 for (i = 0; i < length; i++) {
406 if (i > 0x3f) {
407 printk("\n ...");
408 break;
409 }
410 if ((i & 0x3) == 0)
411 printk(" ");
412 if ((i & 0xf) == 0)
413 printk("\n ");
414 printk("%02x ", (int)dump[i]);
415 }
416 printk("\n");
417
418 return;
419}
420#else
421#define sbp2util_packet_dump(w,x,y,z) do {} while (0)
422#endif
423
424static DECLARE_WAIT_QUEUE_HEAD(access_wq);
425 398
426/* 399/*
427 * Waits for completion of an SBP-2 access request. 400 * Waits for completion of an SBP-2 access request.
428 * Returns nonzero if timed out or prematurely interrupted. 401 * Returns nonzero if timed out or prematurely interrupted.
429 */ 402 */
430static int sbp2util_access_timeout(struct scsi_id_instance_data *scsi_id, 403static int sbp2util_access_timeout(struct sbp2_lu *lu, int timeout)
431 int timeout)
432{ 404{
433 long leftover = wait_event_interruptible_timeout( 405 long leftover;
434 access_wq, scsi_id->access_complete, timeout);
435 406
436 scsi_id->access_complete = 0; 407 leftover = wait_event_interruptible_timeout(
408 sbp2_access_wq, lu->access_complete, timeout);
409 lu->access_complete = 0;
437 return leftover <= 0; 410 return leftover <= 0;
438} 411}
439 412
440/* Frees an allocated packet */ 413static void sbp2_free_packet(void *packet)
441static void sbp2_free_packet(struct hpsb_packet *packet)
442{ 414{
443 hpsb_free_tlabel(packet); 415 hpsb_free_tlabel(packet);
444 hpsb_free_packet(packet); 416 hpsb_free_packet(packet);
445} 417}
446 418
447/* This is much like hpsb_node_write(), except it ignores the response 419/*
448 * subaction and returns immediately. Can be used from interrupts. 420 * This is much like hpsb_node_write(), except it ignores the response
421 * subaction and returns immediately. Can be used from atomic context.
449 */ 422 */
450static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr, 423static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
451 quadlet_t *buffer, size_t length) 424 quadlet_t *buf, size_t len)
452{ 425{
453 struct hpsb_packet *packet; 426 struct hpsb_packet *packet;
454 427
455 packet = hpsb_make_writepacket(ne->host, ne->nodeid, 428 packet = hpsb_make_writepacket(ne->host, ne->nodeid, addr, buf, len);
456 addr, buffer, length);
457 if (!packet) 429 if (!packet)
458 return -ENOMEM; 430 return -ENOMEM;
459 431
460 hpsb_set_packet_complete_task(packet, 432 hpsb_set_packet_complete_task(packet, sbp2_free_packet, packet);
461 (void (*)(void *))sbp2_free_packet,
462 packet);
463
464 hpsb_node_fill_packet(ne, packet); 433 hpsb_node_fill_packet(ne, packet);
465
466 if (hpsb_send_packet(packet) < 0) { 434 if (hpsb_send_packet(packet) < 0) {
467 sbp2_free_packet(packet); 435 sbp2_free_packet(packet);
468 return -EIO; 436 return -EIO;
469 } 437 }
470
471 return 0; 438 return 0;
472} 439}
473 440
474static void sbp2util_notify_fetch_agent(struct scsi_id_instance_data *scsi_id, 441static void sbp2util_notify_fetch_agent(struct sbp2_lu *lu, u64 offset,
475 u64 offset, quadlet_t *data, size_t len) 442 quadlet_t *data, size_t len)
476{ 443{
477 /* 444 /* There is a small window after a bus reset within which the node
478 * There is a small window after a bus reset within which the node 445 * entry's generation is current but the reconnect wasn't completed. */
479 * entry's generation is current but the reconnect wasn't completed. 446 if (unlikely(atomic_read(&lu->state) == SBP2LU_STATE_IN_RESET))
480 */
481 if (unlikely(atomic_read(&scsi_id->state) == SBP2LU_STATE_IN_RESET))
482 return; 447 return;
483 448
484 if (hpsb_node_write(scsi_id->ne, 449 if (hpsb_node_write(lu->ne, lu->command_block_agent_addr + offset,
485 scsi_id->sbp2_command_block_agent_addr + offset,
486 data, len)) 450 data, len))
487 SBP2_ERR("sbp2util_notify_fetch_agent failed."); 451 SBP2_ERR("sbp2util_notify_fetch_agent failed.");
488 /* 452
489 * Now accept new SCSI commands, unless a bus reset happended during 453 /* Now accept new SCSI commands, unless a bus reset happended during
490 * hpsb_node_write. 454 * hpsb_node_write. */
491 */ 455 if (likely(atomic_read(&lu->state) != SBP2LU_STATE_IN_RESET))
492 if (likely(atomic_read(&scsi_id->state) != SBP2LU_STATE_IN_RESET)) 456 scsi_unblock_requests(lu->shost);
493 scsi_unblock_requests(scsi_id->scsi_host);
494} 457}
495 458
496static void sbp2util_write_orb_pointer(void *p) 459static void sbp2util_write_orb_pointer(struct work_struct *work)
497{ 460{
461 struct sbp2_lu *lu = container_of(work, struct sbp2_lu, protocol_work);
498 quadlet_t data[2]; 462 quadlet_t data[2];
499 463
500 data[0] = ORB_SET_NODE_ID( 464 data[0] = ORB_SET_NODE_ID(lu->hi->host->node_id);
501 ((struct scsi_id_instance_data *)p)->hi->host->node_id); 465 data[1] = lu->last_orb_dma;
502 data[1] = ((struct scsi_id_instance_data *)p)->last_orb_dma;
503 sbp2util_cpu_to_be32_buffer(data, 8); 466 sbp2util_cpu_to_be32_buffer(data, 8);
504 sbp2util_notify_fetch_agent(p, SBP2_ORB_POINTER_OFFSET, data, 8); 467 sbp2util_notify_fetch_agent(lu, SBP2_ORB_POINTER_OFFSET, data, 8);
505} 468}
506 469
507static void sbp2util_write_doorbell(void *p) 470static void sbp2util_write_doorbell(struct work_struct *work)
508{ 471{
509 sbp2util_notify_fetch_agent(p, SBP2_DOORBELL_OFFSET, NULL, 4); 472 struct sbp2_lu *lu = container_of(work, struct sbp2_lu, protocol_work);
473
474 sbp2util_notify_fetch_agent(lu, SBP2_DOORBELL_OFFSET, NULL, 4);
510} 475}
511 476
512/* 477static int sbp2util_create_command_orb_pool(struct sbp2_lu *lu)
513 * This function is called to create a pool of command orbs used for
514 * command processing. It is called when a new sbp2 device is detected.
515 */
516static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id)
517{ 478{
518 struct sbp2scsi_host_info *hi = scsi_id->hi; 479 struct sbp2_fwhost_info *hi = lu->hi;
519 int i; 480 int i;
520 unsigned long flags, orbs; 481 unsigned long flags, orbs;
521 struct sbp2_command_info *command; 482 struct sbp2_command_info *cmd;
522 483
523 orbs = serialize_io ? 2 : SBP2_MAX_CMDS; 484 orbs = sbp2_serialize_io ? 2 : SBP2_MAX_CMDS;
524 485
525 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags); 486 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
526 for (i = 0; i < orbs; i++) { 487 for (i = 0; i < orbs; i++) {
527 command = kzalloc(sizeof(*command), GFP_ATOMIC); 488 cmd = kzalloc(sizeof(*cmd), GFP_ATOMIC);
528 if (!command) { 489 if (!cmd) {
529 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, 490 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
530 flags);
531 return -ENOMEM; 491 return -ENOMEM;
532 } 492 }
533 command->command_orb_dma = 493 cmd->command_orb_dma = dma_map_single(&hi->host->device,
534 pci_map_single(hi->host->pdev, &command->command_orb, 494 &cmd->command_orb,
535 sizeof(struct sbp2_command_orb), 495 sizeof(struct sbp2_command_orb),
536 PCI_DMA_TODEVICE); 496 DMA_TO_DEVICE);
537 SBP2_DMA_ALLOC("single command orb DMA"); 497 cmd->sge_dma = dma_map_single(&hi->host->device,
538 command->sge_dma = 498 &cmd->scatter_gather_element,
539 pci_map_single(hi->host->pdev, 499 sizeof(cmd->scatter_gather_element),
540 &command->scatter_gather_element, 500 DMA_BIDIRECTIONAL);
541 sizeof(command->scatter_gather_element), 501 INIT_LIST_HEAD(&cmd->list);
542 PCI_DMA_BIDIRECTIONAL); 502 list_add_tail(&cmd->list, &lu->cmd_orb_completed);
543 SBP2_DMA_ALLOC("scatter_gather_element"); 503 }
544 INIT_LIST_HEAD(&command->list); 504 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
545 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
546 }
547 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
548 return 0; 505 return 0;
549} 506}
550 507
551/* 508static void sbp2util_remove_command_orb_pool(struct sbp2_lu *lu)
552 * This function is called to delete a pool of command orbs.
553 */
554static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id)
555{ 509{
556 struct hpsb_host *host = scsi_id->hi->host; 510 struct hpsb_host *host = lu->hi->host;
557 struct list_head *lh, *next; 511 struct list_head *lh, *next;
558 struct sbp2_command_info *command; 512 struct sbp2_command_info *cmd;
559 unsigned long flags; 513 unsigned long flags;
560 514
561 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags); 515 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
562 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) { 516 if (!list_empty(&lu->cmd_orb_completed))
563 list_for_each_safe(lh, next, &scsi_id->sbp2_command_orb_completed) { 517 list_for_each_safe(lh, next, &lu->cmd_orb_completed) {
564 command = list_entry(lh, struct sbp2_command_info, list); 518 cmd = list_entry(lh, struct sbp2_command_info, list);
565 519 dma_unmap_single(&host->device, cmd->command_orb_dma,
566 /* Release our generic DMA's */
567 pci_unmap_single(host->pdev, command->command_orb_dma,
568 sizeof(struct sbp2_command_orb), 520 sizeof(struct sbp2_command_orb),
569 PCI_DMA_TODEVICE); 521 DMA_TO_DEVICE);
570 SBP2_DMA_FREE("single command orb DMA"); 522 dma_unmap_single(&host->device, cmd->sge_dma,
571 pci_unmap_single(host->pdev, command->sge_dma, 523 sizeof(cmd->scatter_gather_element),
572 sizeof(command->scatter_gather_element), 524 DMA_BIDIRECTIONAL);
573 PCI_DMA_BIDIRECTIONAL); 525 kfree(cmd);
574 SBP2_DMA_FREE("scatter_gather_element");
575
576 kfree(command);
577 } 526 }
578 } 527 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
579 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
580 return; 528 return;
581} 529}
582 530
583/* 531/*
584 * This function finds the sbp2_command for a given outstanding command 532 * Finds the sbp2_command for a given outstanding command ORB.
585 * orb.Only looks at the inuse list. 533 * Only looks at the in-use list.
586 */ 534 */
587static struct sbp2_command_info *sbp2util_find_command_for_orb( 535static struct sbp2_command_info *sbp2util_find_command_for_orb(
588 struct scsi_id_instance_data *scsi_id, dma_addr_t orb) 536 struct sbp2_lu *lu, dma_addr_t orb)
589{ 537{
590 struct sbp2_command_info *command; 538 struct sbp2_command_info *cmd;
591 unsigned long flags; 539 unsigned long flags;
592 540
593 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags); 541 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
594 if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) { 542 if (!list_empty(&lu->cmd_orb_inuse))
595 list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list) { 543 list_for_each_entry(cmd, &lu->cmd_orb_inuse, list)
596 if (command->command_orb_dma == orb) { 544 if (cmd->command_orb_dma == orb) {
597 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags); 545 spin_unlock_irqrestore(
598 return command; 546 &lu->cmd_orb_lock, flags);
547 return cmd;
599 } 548 }
600 } 549 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
601 }
602 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
603
604 SBP2_ORB_DEBUG("could not match command orb %x", (unsigned int)orb);
605
606 return NULL; 550 return NULL;
607} 551}
608 552
609/* 553/*
610 * This function finds the sbp2_command for a given outstanding SCpnt. 554 * Finds the sbp2_command for a given outstanding SCpnt.
611 * Only looks at the inuse list. 555 * Only looks at the in-use list.
612 * Must be called with scsi_id->sbp2_command_orb_lock held. 556 * Must be called with lu->cmd_orb_lock held.
613 */ 557 */
614static struct sbp2_command_info *sbp2util_find_command_for_SCpnt( 558static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
615 struct scsi_id_instance_data *scsi_id, void *SCpnt) 559 struct sbp2_lu *lu, void *SCpnt)
616{ 560{
617 struct sbp2_command_info *command; 561 struct sbp2_command_info *cmd;
618 562
619 if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) 563 if (!list_empty(&lu->cmd_orb_inuse))
620 list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list) 564 list_for_each_entry(cmd, &lu->cmd_orb_inuse, list)
621 if (command->Current_SCpnt == SCpnt) 565 if (cmd->Current_SCpnt == SCpnt)
622 return command; 566 return cmd;
623 return NULL; 567 return NULL;
624} 568}
625 569
626/*
627 * This function allocates a command orb used to send a scsi command.
628 */
629static struct sbp2_command_info *sbp2util_allocate_command_orb( 570static struct sbp2_command_info *sbp2util_allocate_command_orb(
630 struct scsi_id_instance_data *scsi_id, 571 struct sbp2_lu *lu,
631 struct scsi_cmnd *Current_SCpnt, 572 struct scsi_cmnd *Current_SCpnt,
632 void (*Current_done)(struct scsi_cmnd *)) 573 void (*Current_done)(struct scsi_cmnd *))
633{ 574{
634 struct list_head *lh; 575 struct list_head *lh;
635 struct sbp2_command_info *command = NULL; 576 struct sbp2_command_info *cmd = NULL;
636 unsigned long flags; 577 unsigned long flags;
637 578
638 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags); 579 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
639 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) { 580 if (!list_empty(&lu->cmd_orb_completed)) {
640 lh = scsi_id->sbp2_command_orb_completed.next; 581 lh = lu->cmd_orb_completed.next;
641 list_del(lh); 582 list_del(lh);
642 command = list_entry(lh, struct sbp2_command_info, list); 583 cmd = list_entry(lh, struct sbp2_command_info, list);
643 command->Current_done = Current_done; 584 cmd->Current_done = Current_done;
644 command->Current_SCpnt = Current_SCpnt; 585 cmd->Current_SCpnt = Current_SCpnt;
645 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_inuse); 586 list_add_tail(&cmd->list, &lu->cmd_orb_inuse);
646 } else { 587 } else
647 SBP2_ERR("%s: no orbs available", __FUNCTION__); 588 SBP2_ERR("%s: no orbs available", __FUNCTION__);
648 } 589 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
649 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags); 590 return cmd;
650 return command;
651}
652
653/* Free our DMA's */
654static void sbp2util_free_command_dma(struct sbp2_command_info *command)
655{
656 struct scsi_id_instance_data *scsi_id =
657 (struct scsi_id_instance_data *)command->Current_SCpnt->device->host->hostdata[0];
658 struct hpsb_host *host;
659
660 if (!scsi_id) {
661 SBP2_ERR("%s: scsi_id == NULL", __FUNCTION__);
662 return;
663 }
664
665 host = scsi_id->ud->ne->host;
666
667 if (command->cmd_dma) {
668 if (command->dma_type == CMD_DMA_SINGLE) {
669 pci_unmap_single(host->pdev, command->cmd_dma,
670 command->dma_size, command->dma_dir);
671 SBP2_DMA_FREE("single bulk");
672 } else if (command->dma_type == CMD_DMA_PAGE) {
673 pci_unmap_page(host->pdev, command->cmd_dma,
674 command->dma_size, command->dma_dir);
675 SBP2_DMA_FREE("single page");
676 } /* XXX: Check for CMD_DMA_NONE bug */
677 command->dma_type = CMD_DMA_NONE;
678 command->cmd_dma = 0;
679 }
680
681 if (command->sge_buffer) {
682 pci_unmap_sg(host->pdev, command->sge_buffer,
683 command->dma_size, command->dma_dir);
684 SBP2_DMA_FREE("scatter list");
685 command->sge_buffer = NULL;
686 }
687} 591}
688 592
689/* 593/*
690 * This function moves a command to the completed orb list. 594 * Unmaps the DMAs of a command and moves the command to the completed ORB list.
691 * Must be called with scsi_id->sbp2_command_orb_lock held. 595 * Must be called with lu->cmd_orb_lock held.
692 */ 596 */
693static void sbp2util_mark_command_completed( 597static void sbp2util_mark_command_completed(struct sbp2_lu *lu,
694 struct scsi_id_instance_data *scsi_id, 598 struct sbp2_command_info *cmd)
695 struct sbp2_command_info *command)
696{ 599{
697 list_del(&command->list); 600 struct hpsb_host *host = lu->ud->ne->host;
698 sbp2util_free_command_dma(command); 601
699 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed); 602 if (cmd->cmd_dma) {
603 if (cmd->dma_type == CMD_DMA_SINGLE)
604 dma_unmap_single(&host->device, cmd->cmd_dma,
605 cmd->dma_size, cmd->dma_dir);
606 else if (cmd->dma_type == CMD_DMA_PAGE)
607 dma_unmap_page(&host->device, cmd->cmd_dma,
608 cmd->dma_size, cmd->dma_dir);
609 /* XXX: Check for CMD_DMA_NONE bug */
610 cmd->dma_type = CMD_DMA_NONE;
611 cmd->cmd_dma = 0;
612 }
613 if (cmd->sge_buffer) {
614 dma_unmap_sg(&host->device, cmd->sge_buffer,
615 cmd->dma_size, cmd->dma_dir);
616 cmd->sge_buffer = NULL;
617 }
618 list_move_tail(&cmd->list, &lu->cmd_orb_completed);
700} 619}
701 620
702/* 621/*
703 * Is scsi_id valid? Is the 1394 node still present? 622 * Is lu valid? Is the 1394 node still present?
704 */ 623 */
705static inline int sbp2util_node_is_available(struct scsi_id_instance_data *scsi_id) 624static inline int sbp2util_node_is_available(struct sbp2_lu *lu)
706{ 625{
707 return scsi_id && scsi_id->ne && !scsi_id->ne->in_limbo; 626 return lu && lu->ne && !lu->ne->in_limbo;
708} 627}
709 628
710/********************************************* 629/*********************************************
711 * IEEE-1394 core driver stack related section 630 * IEEE-1394 core driver stack related section
712 *********************************************/ 631 *********************************************/
713static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud);
714 632
715static int sbp2_probe(struct device *dev) 633static int sbp2_probe(struct device *dev)
716{ 634{
717 struct unit_directory *ud; 635 struct unit_directory *ud;
718 struct scsi_id_instance_data *scsi_id; 636 struct sbp2_lu *lu;
719
720 SBP2_DEBUG_ENTER();
721 637
722 ud = container_of(dev, struct unit_directory, device); 638 ud = container_of(dev, struct unit_directory, device);
723 639
@@ -726,67 +642,58 @@ static int sbp2_probe(struct device *dev)
726 if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY) 642 if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
727 return -ENODEV; 643 return -ENODEV;
728 644
729 scsi_id = sbp2_alloc_device(ud); 645 lu = sbp2_alloc_device(ud);
730 646 if (!lu)
731 if (!scsi_id)
732 return -ENOMEM; 647 return -ENOMEM;
733 648
734 sbp2_parse_unit_directory(scsi_id, ud); 649 sbp2_parse_unit_directory(lu, ud);
735 650 return sbp2_start_device(lu);
736 return sbp2_start_device(scsi_id);
737} 651}
738 652
739static int sbp2_remove(struct device *dev) 653static int sbp2_remove(struct device *dev)
740{ 654{
741 struct unit_directory *ud; 655 struct unit_directory *ud;
742 struct scsi_id_instance_data *scsi_id; 656 struct sbp2_lu *lu;
743 struct scsi_device *sdev; 657 struct scsi_device *sdev;
744 658
745 SBP2_DEBUG_ENTER();
746
747 ud = container_of(dev, struct unit_directory, device); 659 ud = container_of(dev, struct unit_directory, device);
748 scsi_id = ud->device.driver_data; 660 lu = ud->device.driver_data;
749 if (!scsi_id) 661 if (!lu)
750 return 0; 662 return 0;
751 663
752 if (scsi_id->scsi_host) { 664 if (lu->shost) {
753 /* Get rid of enqueued commands if there is no chance to 665 /* Get rid of enqueued commands if there is no chance to
754 * send them. */ 666 * send them. */
755 if (!sbp2util_node_is_available(scsi_id)) 667 if (!sbp2util_node_is_available(lu))
756 sbp2scsi_complete_all_commands(scsi_id, DID_NO_CONNECT); 668 sbp2scsi_complete_all_commands(lu, DID_NO_CONNECT);
757 /* scsi_remove_device() will trigger shutdown functions of SCSI 669 /* scsi_remove_device() may trigger shutdown functions of SCSI
758 * highlevel drivers which would deadlock if blocked. */ 670 * highlevel drivers which would deadlock if blocked. */
759 atomic_set(&scsi_id->state, SBP2LU_STATE_IN_SHUTDOWN); 671 atomic_set(&lu->state, SBP2LU_STATE_IN_SHUTDOWN);
760 scsi_unblock_requests(scsi_id->scsi_host); 672 scsi_unblock_requests(lu->shost);
761 } 673 }
762 sdev = scsi_id->sdev; 674 sdev = lu->sdev;
763 if (sdev) { 675 if (sdev) {
764 scsi_id->sdev = NULL; 676 lu->sdev = NULL;
765 scsi_remove_device(sdev); 677 scsi_remove_device(sdev);
766 } 678 }
767 679
768 sbp2_logout_device(scsi_id); 680 sbp2_logout_device(lu);
769 sbp2_remove_device(scsi_id); 681 sbp2_remove_device(lu);
770 682
771 return 0; 683 return 0;
772} 684}
773 685
774static int sbp2_update(struct unit_directory *ud) 686static int sbp2_update(struct unit_directory *ud)
775{ 687{
776 struct scsi_id_instance_data *scsi_id = ud->device.driver_data; 688 struct sbp2_lu *lu = ud->device.driver_data;
777
778 SBP2_DEBUG_ENTER();
779 689
780 if (sbp2_reconnect_device(scsi_id)) { 690 if (sbp2_reconnect_device(lu)) {
691 /* Reconnect has failed. Perhaps we didn't reconnect fast
692 * enough. Try a regular login, but first log out just in
693 * case of any weirdness. */
694 sbp2_logout_device(lu);
781 695
782 /* 696 if (sbp2_login_device(lu)) {
783 * Ok, reconnect has failed. Perhaps we didn't
784 * reconnect fast enough. Try doing a regular login, but
785 * first do a logout just in case of any weirdness.
786 */
787 sbp2_logout_device(scsi_id);
788
789 if (sbp2_login_device(scsi_id)) {
790 /* Login failed too, just fail, and the backend 697 /* Login failed too, just fail, and the backend
791 * will call our sbp2_remove for us */ 698 * will call our sbp2_remove for us */
792 SBP2_ERR("Failed to reconnect to sbp2 device!"); 699 SBP2_ERR("Failed to reconnect to sbp2 device!");
@@ -794,69 +701,59 @@ static int sbp2_update(struct unit_directory *ud)
794 } 701 }
795 } 702 }
796 703
797 /* Set max retries to something large on the device. */ 704 sbp2_set_busy_timeout(lu);
798 sbp2_set_busy_timeout(scsi_id); 705 sbp2_agent_reset(lu, 1);
706 sbp2_max_speed_and_size(lu);
799 707
800 /* Do a SBP-2 fetch agent reset. */ 708 /* Complete any pending commands with busy (so they get retried)
801 sbp2_agent_reset(scsi_id, 1); 709 * and remove them from our queue. */
802 710 sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
803 /* Get the max speed and packet size that we can use. */
804 sbp2_max_speed_and_size(scsi_id);
805
806 /* Complete any pending commands with busy (so they get
807 * retried) and remove them from our queue
808 */
809 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
810 711
811 /* Accept new commands unless there was another bus reset in the 712 /* Accept new commands unless there was another bus reset in the
812 * meantime. */ 713 * meantime. */
813 if (hpsb_node_entry_valid(scsi_id->ne)) { 714 if (hpsb_node_entry_valid(lu->ne)) {
814 atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING); 715 atomic_set(&lu->state, SBP2LU_STATE_RUNNING);
815 scsi_unblock_requests(scsi_id->scsi_host); 716 scsi_unblock_requests(lu->shost);
816 } 717 }
817 return 0; 718 return 0;
818} 719}
819 720
820/* This functions is called by the sbp2_probe, for each new device. We now 721static struct sbp2_lu *sbp2_alloc_device(struct unit_directory *ud)
821 * allocate one scsi host for each scsi_id (unit directory). */
822static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud)
823{ 722{
824 struct sbp2scsi_host_info *hi; 723 struct sbp2_fwhost_info *hi;
825 struct Scsi_Host *scsi_host = NULL; 724 struct Scsi_Host *shost = NULL;
826 struct scsi_id_instance_data *scsi_id = NULL; 725 struct sbp2_lu *lu = NULL;
827 726
828 SBP2_DEBUG_ENTER(); 727 lu = kzalloc(sizeof(*lu), GFP_KERNEL);
829 728 if (!lu) {
830 scsi_id = kzalloc(sizeof(*scsi_id), GFP_KERNEL); 729 SBP2_ERR("failed to create lu");
831 if (!scsi_id) {
832 SBP2_ERR("failed to create scsi_id");
833 goto failed_alloc; 730 goto failed_alloc;
834 } 731 }
835 732
836 scsi_id->ne = ud->ne; 733 lu->ne = ud->ne;
837 scsi_id->ud = ud; 734 lu->ud = ud;
838 scsi_id->speed_code = IEEE1394_SPEED_100; 735 lu->speed_code = IEEE1394_SPEED_100;
839 scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100]; 736 lu->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
840 scsi_id->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE; 737 lu->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
841 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_inuse); 738 INIT_LIST_HEAD(&lu->cmd_orb_inuse);
842 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_completed); 739 INIT_LIST_HEAD(&lu->cmd_orb_completed);
843 INIT_LIST_HEAD(&scsi_id->scsi_list); 740 INIT_LIST_HEAD(&lu->lu_list);
844 spin_lock_init(&scsi_id->sbp2_command_orb_lock); 741 spin_lock_init(&lu->cmd_orb_lock);
845 atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING); 742 atomic_set(&lu->state, SBP2LU_STATE_RUNNING);
846 INIT_WORK(&scsi_id->protocol_work, NULL, NULL); 743 INIT_WORK(&lu->protocol_work, NULL);
847 744
848 ud->device.driver_data = scsi_id; 745 ud->device.driver_data = lu;
849 746
850 hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host); 747 hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
851 if (!hi) { 748 if (!hi) {
852 hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host, sizeof(*hi)); 749 hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host,
750 sizeof(*hi));
853 if (!hi) { 751 if (!hi) {
854 SBP2_ERR("failed to allocate hostinfo"); 752 SBP2_ERR("failed to allocate hostinfo");
855 goto failed_alloc; 753 goto failed_alloc;
856 } 754 }
857 SBP2_DEBUG("sbp2_alloc_device: allocated hostinfo");
858 hi->host = ud->ne->host; 755 hi->host = ud->ne->host;
859 INIT_LIST_HEAD(&hi->scsi_ids); 756 INIT_LIST_HEAD(&hi->logical_units);
860 757
861#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA 758#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
862 /* Handle data movement if physical dma is not 759 /* Handle data movement if physical dma is not
@@ -876,9 +773,9 @@ static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud
876 goto failed_alloc; 773 goto failed_alloc;
877 } 774 }
878 775
879 scsi_id->hi = hi; 776 lu->hi = hi;
880 777
881 list_add_tail(&scsi_id->scsi_list, &hi->scsi_ids); 778 list_add_tail(&lu->lu_list, &hi->logical_units);
882 779
883 /* Register the status FIFO address range. We could use the same FIFO 780 /* Register the status FIFO address range. We could use the same FIFO
884 * for targets at different nodes. However we need different FIFOs per 781 * for targets at different nodes. However we need different FIFOs per
@@ -888,302 +785,214 @@ static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud
888 * then be performed as unified transactions. This slightly reduces 785 * then be performed as unified transactions. This slightly reduces
889 * bandwidth usage, and some Prolific based devices seem to require it. 786 * bandwidth usage, and some Prolific based devices seem to require it.
890 */ 787 */
891 scsi_id->status_fifo_addr = hpsb_allocate_and_register_addrspace( 788 lu->status_fifo_addr = hpsb_allocate_and_register_addrspace(
892 &sbp2_highlevel, ud->ne->host, &sbp2_ops, 789 &sbp2_highlevel, ud->ne->host, &sbp2_ops,
893 sizeof(struct sbp2_status_block), sizeof(quadlet_t), 790 sizeof(struct sbp2_status_block), sizeof(quadlet_t),
894 ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END); 791 ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
895 if (scsi_id->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) { 792 if (lu->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
896 SBP2_ERR("failed to allocate status FIFO address range"); 793 SBP2_ERR("failed to allocate status FIFO address range");
897 goto failed_alloc; 794 goto failed_alloc;
898 } 795 }
899 796
900 /* Register our host with the SCSI stack. */ 797 shost = scsi_host_alloc(&sbp2_shost_template, sizeof(unsigned long));
901 scsi_host = scsi_host_alloc(&scsi_driver_template, 798 if (!shost) {
902 sizeof(unsigned long));
903 if (!scsi_host) {
904 SBP2_ERR("failed to register scsi host"); 799 SBP2_ERR("failed to register scsi host");
905 goto failed_alloc; 800 goto failed_alloc;
906 } 801 }
907 802
908 scsi_host->hostdata[0] = (unsigned long)scsi_id; 803 shost->hostdata[0] = (unsigned long)lu;
909 804
910 if (!scsi_add_host(scsi_host, &ud->device)) { 805 if (!scsi_add_host(shost, &ud->device)) {
911 scsi_id->scsi_host = scsi_host; 806 lu->shost = shost;
912 return scsi_id; 807 return lu;
913 } 808 }
914 809
915 SBP2_ERR("failed to add scsi host"); 810 SBP2_ERR("failed to add scsi host");
916 scsi_host_put(scsi_host); 811 scsi_host_put(shost);
917 812
918failed_alloc: 813failed_alloc:
919 sbp2_remove_device(scsi_id); 814 sbp2_remove_device(lu);
920 return NULL; 815 return NULL;
921} 816}
922 817
923static void sbp2_host_reset(struct hpsb_host *host) 818static void sbp2_host_reset(struct hpsb_host *host)
924{ 819{
925 struct sbp2scsi_host_info *hi; 820 struct sbp2_fwhost_info *hi;
926 struct scsi_id_instance_data *scsi_id; 821 struct sbp2_lu *lu;
927 822
928 hi = hpsb_get_hostinfo(&sbp2_highlevel, host); 823 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
929 if (!hi) 824 if (!hi)
930 return; 825 return;
931 list_for_each_entry(scsi_id, &hi->scsi_ids, scsi_list) 826 list_for_each_entry(lu, &hi->logical_units, lu_list)
932 if (likely(atomic_read(&scsi_id->state) != 827 if (likely(atomic_read(&lu->state) !=
933 SBP2LU_STATE_IN_SHUTDOWN)) { 828 SBP2LU_STATE_IN_SHUTDOWN)) {
934 atomic_set(&scsi_id->state, SBP2LU_STATE_IN_RESET); 829 atomic_set(&lu->state, SBP2LU_STATE_IN_RESET);
935 scsi_block_requests(scsi_id->scsi_host); 830 scsi_block_requests(lu->shost);
936 } 831 }
937} 832}
938 833
939/* 834static int sbp2_start_device(struct sbp2_lu *lu)
940 * This function is where we first pull the node unique ids, and then
941 * allocate memory and register a SBP-2 device.
942 */
943static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
944{ 835{
945 struct sbp2scsi_host_info *hi = scsi_id->hi; 836 struct sbp2_fwhost_info *hi = lu->hi;
946 int error; 837 int error;
947 838
948 SBP2_DEBUG_ENTER(); 839 lu->login_response = dma_alloc_coherent(&hi->host->device,
949
950 /* Login FIFO DMA */
951 scsi_id->login_response =
952 pci_alloc_consistent(hi->host->pdev,
953 sizeof(struct sbp2_login_response), 840 sizeof(struct sbp2_login_response),
954 &scsi_id->login_response_dma); 841 &lu->login_response_dma, GFP_KERNEL);
955 if (!scsi_id->login_response) 842 if (!lu->login_response)
956 goto alloc_fail; 843 goto alloc_fail;
957 SBP2_DMA_ALLOC("consistent DMA region for login FIFO");
958 844
959 /* Query logins ORB DMA */ 845 lu->query_logins_orb = dma_alloc_coherent(&hi->host->device,
960 scsi_id->query_logins_orb =
961 pci_alloc_consistent(hi->host->pdev,
962 sizeof(struct sbp2_query_logins_orb), 846 sizeof(struct sbp2_query_logins_orb),
963 &scsi_id->query_logins_orb_dma); 847 &lu->query_logins_orb_dma, GFP_KERNEL);
964 if (!scsi_id->query_logins_orb) 848 if (!lu->query_logins_orb)
965 goto alloc_fail; 849 goto alloc_fail;
966 SBP2_DMA_ALLOC("consistent DMA region for query logins ORB");
967 850
968 /* Query logins response DMA */ 851 lu->query_logins_response = dma_alloc_coherent(&hi->host->device,
969 scsi_id->query_logins_response =
970 pci_alloc_consistent(hi->host->pdev,
971 sizeof(struct sbp2_query_logins_response), 852 sizeof(struct sbp2_query_logins_response),
972 &scsi_id->query_logins_response_dma); 853 &lu->query_logins_response_dma, GFP_KERNEL);
973 if (!scsi_id->query_logins_response) 854 if (!lu->query_logins_response)
974 goto alloc_fail; 855 goto alloc_fail;
975 SBP2_DMA_ALLOC("consistent DMA region for query logins response");
976 856
977 /* Reconnect ORB DMA */ 857 lu->reconnect_orb = dma_alloc_coherent(&hi->host->device,
978 scsi_id->reconnect_orb =
979 pci_alloc_consistent(hi->host->pdev,
980 sizeof(struct sbp2_reconnect_orb), 858 sizeof(struct sbp2_reconnect_orb),
981 &scsi_id->reconnect_orb_dma); 859 &lu->reconnect_orb_dma, GFP_KERNEL);
982 if (!scsi_id->reconnect_orb) 860 if (!lu->reconnect_orb)
983 goto alloc_fail; 861 goto alloc_fail;
984 SBP2_DMA_ALLOC("consistent DMA region for reconnect ORB");
985 862
986 /* Logout ORB DMA */ 863 lu->logout_orb = dma_alloc_coherent(&hi->host->device,
987 scsi_id->logout_orb =
988 pci_alloc_consistent(hi->host->pdev,
989 sizeof(struct sbp2_logout_orb), 864 sizeof(struct sbp2_logout_orb),
990 &scsi_id->logout_orb_dma); 865 &lu->logout_orb_dma, GFP_KERNEL);
991 if (!scsi_id->logout_orb) 866 if (!lu->logout_orb)
992 goto alloc_fail; 867 goto alloc_fail;
993 SBP2_DMA_ALLOC("consistent DMA region for logout ORB");
994 868
995 /* Login ORB DMA */ 869 lu->login_orb = dma_alloc_coherent(&hi->host->device,
996 scsi_id->login_orb =
997 pci_alloc_consistent(hi->host->pdev,
998 sizeof(struct sbp2_login_orb), 870 sizeof(struct sbp2_login_orb),
999 &scsi_id->login_orb_dma); 871 &lu->login_orb_dma, GFP_KERNEL);
1000 if (!scsi_id->login_orb) 872 if (!lu->login_orb)
1001 goto alloc_fail; 873 goto alloc_fail;
1002 SBP2_DMA_ALLOC("consistent DMA region for login ORB");
1003
1004 SBP2_DEBUG("New SBP-2 device inserted, SCSI ID = %x", scsi_id->ud->id);
1005 874
1006 /* 875 if (sbp2util_create_command_orb_pool(lu)) {
1007 * Create our command orb pool
1008 */
1009 if (sbp2util_create_command_orb_pool(scsi_id)) {
1010 SBP2_ERR("sbp2util_create_command_orb_pool failed!"); 876 SBP2_ERR("sbp2util_create_command_orb_pool failed!");
1011 sbp2_remove_device(scsi_id); 877 sbp2_remove_device(lu);
1012 return -ENOMEM; 878 return -ENOMEM;
1013 } 879 }
1014 880
1015 /* Schedule a timeout here. The reason is that we may be so close 881 /* Wait a second before trying to log in. Previously logged in
1016 * to a bus reset, that the device is not available for logins. 882 * initiators need a chance to reconnect. */
1017 * This can happen when the bus reset is caused by the host
1018 * connected to the sbp2 device being removed. That host would
1019 * have a certain amount of time to relogin before the sbp2 device
1020 * allows someone else to login instead. One second makes sense. */
1021 if (msleep_interruptible(1000)) { 883 if (msleep_interruptible(1000)) {
1022 sbp2_remove_device(scsi_id); 884 sbp2_remove_device(lu);
1023 return -EINTR; 885 return -EINTR;
1024 } 886 }
1025 887
1026 /* 888 if (sbp2_login_device(lu)) {
1027 * Login to the sbp-2 device 889 sbp2_remove_device(lu);
1028 */
1029 if (sbp2_login_device(scsi_id)) {
1030 /* Login failed, just remove the device. */
1031 sbp2_remove_device(scsi_id);
1032 return -EBUSY; 890 return -EBUSY;
1033 } 891 }
1034 892
1035 /* 893 sbp2_set_busy_timeout(lu);
1036 * Set max retries to something large on the device 894 sbp2_agent_reset(lu, 1);
1037 */ 895 sbp2_max_speed_and_size(lu);
1038 sbp2_set_busy_timeout(scsi_id);
1039
1040 /*
1041 * Do a SBP-2 fetch agent reset
1042 */
1043 sbp2_agent_reset(scsi_id, 1);
1044 896
1045 /* 897 error = scsi_add_device(lu->shost, 0, lu->ud->id, 0);
1046 * Get the max speed and packet size that we can use
1047 */
1048 sbp2_max_speed_and_size(scsi_id);
1049
1050 /* Add this device to the scsi layer now */
1051 error = scsi_add_device(scsi_id->scsi_host, 0, scsi_id->ud->id, 0);
1052 if (error) { 898 if (error) {
1053 SBP2_ERR("scsi_add_device failed"); 899 SBP2_ERR("scsi_add_device failed");
1054 sbp2_logout_device(scsi_id); 900 sbp2_logout_device(lu);
1055 sbp2_remove_device(scsi_id); 901 sbp2_remove_device(lu);
1056 return error; 902 return error;
1057 } 903 }
1058 904
1059 return 0; 905 return 0;
1060 906
1061alloc_fail: 907alloc_fail:
1062 SBP2_ERR("Could not allocate memory for scsi_id"); 908 SBP2_ERR("Could not allocate memory for lu");
1063 sbp2_remove_device(scsi_id); 909 sbp2_remove_device(lu);
1064 return -ENOMEM; 910 return -ENOMEM;
1065} 911}
1066 912
1067/* 913static void sbp2_remove_device(struct sbp2_lu *lu)
1068 * This function removes an sbp2 device from the sbp2scsi_host_info struct.
1069 */
1070static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
1071{ 914{
1072 struct sbp2scsi_host_info *hi; 915 struct sbp2_fwhost_info *hi;
1073
1074 SBP2_DEBUG_ENTER();
1075 916
1076 if (!scsi_id) 917 if (!lu)
1077 return; 918 return;
1078 919
1079 hi = scsi_id->hi; 920 hi = lu->hi;
1080 921
1081 /* This will remove our scsi device aswell */ 922 if (lu->shost) {
1082 if (scsi_id->scsi_host) { 923 scsi_remove_host(lu->shost);
1083 scsi_remove_host(scsi_id->scsi_host); 924 scsi_host_put(lu->shost);
1084 scsi_host_put(scsi_id->scsi_host);
1085 } 925 }
1086 flush_scheduled_work(); 926 flush_scheduled_work();
1087 sbp2util_remove_command_orb_pool(scsi_id); 927 sbp2util_remove_command_orb_pool(lu);
1088 928
1089 list_del(&scsi_id->scsi_list); 929 list_del(&lu->lu_list);
1090 930
1091 if (scsi_id->login_response) { 931 if (lu->login_response)
1092 pci_free_consistent(hi->host->pdev, 932 dma_free_coherent(&hi->host->device,
1093 sizeof(struct sbp2_login_response), 933 sizeof(struct sbp2_login_response),
1094 scsi_id->login_response, 934 lu->login_response,
1095 scsi_id->login_response_dma); 935 lu->login_response_dma);
1096 SBP2_DMA_FREE("single login FIFO"); 936 if (lu->login_orb)
1097 } 937 dma_free_coherent(&hi->host->device,
1098
1099 if (scsi_id->login_orb) {
1100 pci_free_consistent(hi->host->pdev,
1101 sizeof(struct sbp2_login_orb), 938 sizeof(struct sbp2_login_orb),
1102 scsi_id->login_orb, 939 lu->login_orb,
1103 scsi_id->login_orb_dma); 940 lu->login_orb_dma);
1104 SBP2_DMA_FREE("single login ORB"); 941 if (lu->reconnect_orb)
1105 } 942 dma_free_coherent(&hi->host->device,
1106
1107 if (scsi_id->reconnect_orb) {
1108 pci_free_consistent(hi->host->pdev,
1109 sizeof(struct sbp2_reconnect_orb), 943 sizeof(struct sbp2_reconnect_orb),
1110 scsi_id->reconnect_orb, 944 lu->reconnect_orb,
1111 scsi_id->reconnect_orb_dma); 945 lu->reconnect_orb_dma);
1112 SBP2_DMA_FREE("single reconnect orb"); 946 if (lu->logout_orb)
1113 } 947 dma_free_coherent(&hi->host->device,
1114
1115 if (scsi_id->logout_orb) {
1116 pci_free_consistent(hi->host->pdev,
1117 sizeof(struct sbp2_logout_orb), 948 sizeof(struct sbp2_logout_orb),
1118 scsi_id->logout_orb, 949 lu->logout_orb,
1119 scsi_id->logout_orb_dma); 950 lu->logout_orb_dma);
1120 SBP2_DMA_FREE("single logout orb"); 951 if (lu->query_logins_orb)
1121 } 952 dma_free_coherent(&hi->host->device,
1122
1123 if (scsi_id->query_logins_orb) {
1124 pci_free_consistent(hi->host->pdev,
1125 sizeof(struct sbp2_query_logins_orb), 953 sizeof(struct sbp2_query_logins_orb),
1126 scsi_id->query_logins_orb, 954 lu->query_logins_orb,
1127 scsi_id->query_logins_orb_dma); 955 lu->query_logins_orb_dma);
1128 SBP2_DMA_FREE("single query logins orb"); 956 if (lu->query_logins_response)
1129 } 957 dma_free_coherent(&hi->host->device,
1130
1131 if (scsi_id->query_logins_response) {
1132 pci_free_consistent(hi->host->pdev,
1133 sizeof(struct sbp2_query_logins_response), 958 sizeof(struct sbp2_query_logins_response),
1134 scsi_id->query_logins_response, 959 lu->query_logins_response,
1135 scsi_id->query_logins_response_dma); 960 lu->query_logins_response_dma);
1136 SBP2_DMA_FREE("single query logins data");
1137 }
1138 961
1139 if (scsi_id->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE) 962 if (lu->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
1140 hpsb_unregister_addrspace(&sbp2_highlevel, hi->host, 963 hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
1141 scsi_id->status_fifo_addr); 964 lu->status_fifo_addr);
1142 965
1143 scsi_id->ud->device.driver_data = NULL; 966 lu->ud->device.driver_data = NULL;
1144 967
1145 if (hi) 968 if (hi)
1146 module_put(hi->host->driver->owner); 969 module_put(hi->host->driver->owner);
1147 970
1148 SBP2_DEBUG("SBP-2 device removed, SCSI ID = %d", scsi_id->ud->id); 971 kfree(lu);
1149
1150 kfree(scsi_id);
1151} 972}
1152 973
1153#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA 974#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
1154/* 975/*
1155 * This function deals with physical dma write requests (for adapters that do not support 976 * Deal with write requests on adapters which do not support physical DMA or
1156 * physical dma in hardware). Mostly just here for debugging... 977 * have it switched off.
1157 */ 978 */
1158static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid, 979static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
1159 int destid, quadlet_t *data, u64 addr, 980 int destid, quadlet_t *data, u64 addr,
1160 size_t length, u16 flags) 981 size_t length, u16 flags)
1161{ 982{
1162
1163 /*
1164 * Manually put the data in the right place.
1165 */
1166 memcpy(bus_to_virt((u32) addr), data, length); 983 memcpy(bus_to_virt((u32) addr), data, length);
1167 sbp2util_packet_dump(data, length, "sbp2 phys dma write by device",
1168 (u32) addr);
1169 return RCODE_COMPLETE; 984 return RCODE_COMPLETE;
1170} 985}
1171 986
1172/* 987/*
1173 * This function deals with physical dma read requests (for adapters that do not support 988 * Deal with read requests on adapters which do not support physical DMA or
1174 * physical dma in hardware). Mostly just here for debugging... 989 * have it switched off.
1175 */ 990 */
1176static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid, 991static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
1177 quadlet_t *data, u64 addr, size_t length, 992 quadlet_t *data, u64 addr, size_t length,
1178 u16 flags) 993 u16 flags)
1179{ 994{
1180
1181 /*
1182 * Grab data from memory and send a read response.
1183 */
1184 memcpy(data, bus_to_virt((u32) addr), length); 995 memcpy(data, bus_to_virt((u32) addr), length);
1185 sbp2util_packet_dump(data, length, "sbp2 phys dma read by device",
1186 (u32) addr);
1187 return RCODE_COMPLETE; 996 return RCODE_COMPLETE;
1188} 997}
1189#endif 998#endif
@@ -1192,74 +1001,69 @@ static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
1192 * SBP-2 protocol related section 1001 * SBP-2 protocol related section
1193 **************************************/ 1002 **************************************/
1194 1003
1195/* 1004static int sbp2_query_logins(struct sbp2_lu *lu)
1196 * This function queries the device for the maximum concurrent logins it
1197 * supports.
1198 */
1199static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
1200{ 1005{
1201 struct sbp2scsi_host_info *hi = scsi_id->hi; 1006 struct sbp2_fwhost_info *hi = lu->hi;
1202 quadlet_t data[2]; 1007 quadlet_t data[2];
1203 int max_logins; 1008 int max_logins;
1204 int active_logins; 1009 int active_logins;
1205 1010
1206 SBP2_DEBUG_ENTER(); 1011 lu->query_logins_orb->reserved1 = 0x0;
1207 1012 lu->query_logins_orb->reserved2 = 0x0;
1208 scsi_id->query_logins_orb->reserved1 = 0x0;
1209 scsi_id->query_logins_orb->reserved2 = 0x0;
1210
1211 scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma;
1212 scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1213 1013
1214 scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST); 1014 lu->query_logins_orb->query_response_lo = lu->query_logins_response_dma;
1215 scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1); 1015 lu->query_logins_orb->query_response_hi =
1216 scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun); 1016 ORB_SET_NODE_ID(hi->host->node_id);
1017 lu->query_logins_orb->lun_misc =
1018 ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
1019 lu->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
1020 lu->query_logins_orb->lun_misc |= ORB_SET_LUN(lu->lun);
1217 1021
1218 scsi_id->query_logins_orb->reserved_resp_length = 1022 lu->query_logins_orb->reserved_resp_length =
1219 ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response)); 1023 ORB_SET_QUERY_LOGINS_RESP_LENGTH(
1024 sizeof(struct sbp2_query_logins_response));
1220 1025
1221 scsi_id->query_logins_orb->status_fifo_hi = 1026 lu->query_logins_orb->status_fifo_hi =
1222 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id); 1027 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1223 scsi_id->query_logins_orb->status_fifo_lo = 1028 lu->query_logins_orb->status_fifo_lo =
1224 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr); 1029 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1225 1030
1226 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb)); 1031 sbp2util_cpu_to_be32_buffer(lu->query_logins_orb,
1032 sizeof(struct sbp2_query_logins_orb));
1227 1033
1228 sbp2util_packet_dump(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb), 1034 memset(lu->query_logins_response, 0,
1229 "sbp2 query logins orb", scsi_id->query_logins_orb_dma); 1035 sizeof(struct sbp2_query_logins_response));
1230
1231 memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response));
1232 1036
1233 data[0] = ORB_SET_NODE_ID(hi->host->node_id); 1037 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1234 data[1] = scsi_id->query_logins_orb_dma; 1038 data[1] = lu->query_logins_orb_dma;
1235 sbp2util_cpu_to_be32_buffer(data, 8); 1039 sbp2util_cpu_to_be32_buffer(data, 8);
1236 1040
1237 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8); 1041 hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1238 1042
1239 if (sbp2util_access_timeout(scsi_id, 2*HZ)) { 1043 if (sbp2util_access_timeout(lu, 2*HZ)) {
1240 SBP2_INFO("Error querying logins to SBP-2 device - timed out"); 1044 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
1241 return -EIO; 1045 return -EIO;
1242 } 1046 }
1243 1047
1244 if (scsi_id->status_block.ORB_offset_lo != scsi_id->query_logins_orb_dma) { 1048 if (lu->status_block.ORB_offset_lo != lu->query_logins_orb_dma) {
1245 SBP2_INFO("Error querying logins to SBP-2 device - timed out"); 1049 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
1246 return -EIO; 1050 return -EIO;
1247 } 1051 }
1248 1052
1249 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) { 1053 if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
1250 SBP2_INFO("Error querying logins to SBP-2 device - failed"); 1054 SBP2_INFO("Error querying logins to SBP-2 device - failed");
1251 return -EIO; 1055 return -EIO;
1252 } 1056 }
1253 1057
1254 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_response, sizeof(struct sbp2_query_logins_response)); 1058 sbp2util_cpu_to_be32_buffer(lu->query_logins_response,
1255 1059 sizeof(struct sbp2_query_logins_response));
1256 SBP2_DEBUG("length_max_logins = %x",
1257 (unsigned int)scsi_id->query_logins_response->length_max_logins);
1258 1060
1259 max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins); 1061 max_logins = RESPONSE_GET_MAX_LOGINS(
1062 lu->query_logins_response->length_max_logins);
1260 SBP2_INFO("Maximum concurrent logins supported: %d", max_logins); 1063 SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
1261 1064
1262 active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins); 1065 active_logins = RESPONSE_GET_ACTIVE_LOGINS(
1066 lu->query_logins_response->length_max_logins);
1263 SBP2_INFO("Number of active logins: %d", active_logins); 1067 SBP2_INFO("Number of active logins: %d", active_logins);
1264 1068
1265 if (active_logins >= max_logins) { 1069 if (active_logins >= max_logins) {
@@ -1269,332 +1073,231 @@ static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
1269 return 0; 1073 return 0;
1270} 1074}
1271 1075
1272/* 1076static int sbp2_login_device(struct sbp2_lu *lu)
1273 * This function is called in order to login to a particular SBP-2 device,
1274 * after a bus reset.
1275 */
1276static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
1277{ 1077{
1278 struct sbp2scsi_host_info *hi = scsi_id->hi; 1078 struct sbp2_fwhost_info *hi = lu->hi;
1279 quadlet_t data[2]; 1079 quadlet_t data[2];
1280 1080
1281 SBP2_DEBUG_ENTER(); 1081 if (!lu->login_orb)
1282
1283 if (!scsi_id->login_orb) {
1284 SBP2_DEBUG("%s: login_orb not alloc'd!", __FUNCTION__);
1285 return -EIO; 1082 return -EIO;
1286 }
1287 1083
1288 if (!exclusive_login) { 1084 if (!sbp2_exclusive_login && sbp2_query_logins(lu)) {
1289 if (sbp2_query_logins(scsi_id)) { 1085 SBP2_INFO("Device does not support any more concurrent logins");
1290 SBP2_INFO("Device does not support any more concurrent logins"); 1086 return -EIO;
1291 return -EIO;
1292 }
1293 } 1087 }
1294 1088
1295 /* Set-up login ORB, assume no password */ 1089 /* assume no password */
1296 scsi_id->login_orb->password_hi = 0; 1090 lu->login_orb->password_hi = 0;
1297 scsi_id->login_orb->password_lo = 0; 1091 lu->login_orb->password_lo = 0;
1298 1092
1299 scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma; 1093 lu->login_orb->login_response_lo = lu->login_response_dma;
1300 scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id); 1094 lu->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1095 lu->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
1301 1096
1302 scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST); 1097 /* one second reconnect time */
1303 scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0); /* One second reconnect time */ 1098 lu->login_orb->lun_misc |= ORB_SET_RECONNECT(0);
1304 scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(exclusive_login); /* Exclusive access to device */ 1099 lu->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(sbp2_exclusive_login);
1305 scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1); /* Notify us of login complete */ 1100 lu->login_orb->lun_misc |= ORB_SET_NOTIFY(1);
1306 scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun); 1101 lu->login_orb->lun_misc |= ORB_SET_LUN(lu->lun);
1307 1102
1308 scsi_id->login_orb->passwd_resp_lengths = 1103 lu->login_orb->passwd_resp_lengths =
1309 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response)); 1104 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
1310 1105
1311 scsi_id->login_orb->status_fifo_hi = 1106 lu->login_orb->status_fifo_hi =
1312 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id); 1107 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1313 scsi_id->login_orb->status_fifo_lo = 1108 lu->login_orb->status_fifo_lo =
1314 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr); 1109 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1315 1110
1316 sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb)); 1111 sbp2util_cpu_to_be32_buffer(lu->login_orb,
1112 sizeof(struct sbp2_login_orb));
1317 1113
1318 sbp2util_packet_dump(scsi_id->login_orb, sizeof(struct sbp2_login_orb), 1114 memset(lu->login_response, 0, sizeof(struct sbp2_login_response));
1319 "sbp2 login orb", scsi_id->login_orb_dma);
1320
1321 memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
1322 1115
1323 data[0] = ORB_SET_NODE_ID(hi->host->node_id); 1116 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1324 data[1] = scsi_id->login_orb_dma; 1117 data[1] = lu->login_orb_dma;
1325 sbp2util_cpu_to_be32_buffer(data, 8); 1118 sbp2util_cpu_to_be32_buffer(data, 8);
1326 1119
1327 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8); 1120 hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1328 1121
1329 /* 1122 /* wait up to 20 seconds for login status */
1330 * Wait for login status (up to 20 seconds)... 1123 if (sbp2util_access_timeout(lu, 20*HZ)) {
1331 */
1332 if (sbp2util_access_timeout(scsi_id, 20*HZ)) {
1333 SBP2_ERR("Error logging into SBP-2 device - timed out"); 1124 SBP2_ERR("Error logging into SBP-2 device - timed out");
1334 return -EIO; 1125 return -EIO;
1335 } 1126 }
1336 1127
1337 /* 1128 /* make sure that the returned status matches the login ORB */
1338 * Sanity. Make sure status returned matches login orb. 1129 if (lu->status_block.ORB_offset_lo != lu->login_orb_dma) {
1339 */
1340 if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
1341 SBP2_ERR("Error logging into SBP-2 device - timed out"); 1130 SBP2_ERR("Error logging into SBP-2 device - timed out");
1342 return -EIO; 1131 return -EIO;
1343 } 1132 }
1344 1133
1345 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) { 1134 if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
1346 SBP2_ERR("Error logging into SBP-2 device - failed"); 1135 SBP2_ERR("Error logging into SBP-2 device - failed");
1347 return -EIO; 1136 return -EIO;
1348 } 1137 }
1349 1138
1350 /* 1139 sbp2util_cpu_to_be32_buffer(lu->login_response,
1351 * Byte swap the login response, for use when reconnecting or 1140 sizeof(struct sbp2_login_response));
1352 * logging out. 1141 lu->command_block_agent_addr =
1353 */ 1142 ((u64)lu->login_response->command_block_agent_hi) << 32;
1354 sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response)); 1143 lu->command_block_agent_addr |=
1355 1144 ((u64)lu->login_response->command_block_agent_lo);
1356 /* 1145 lu->command_block_agent_addr &= 0x0000ffffffffffffULL;
1357 * Grab our command block agent address from the login response.
1358 */
1359 SBP2_DEBUG("command_block_agent_hi = %x",
1360 (unsigned int)scsi_id->login_response->command_block_agent_hi);
1361 SBP2_DEBUG("command_block_agent_lo = %x",
1362 (unsigned int)scsi_id->login_response->command_block_agent_lo);
1363
1364 scsi_id->sbp2_command_block_agent_addr =
1365 ((u64)scsi_id->login_response->command_block_agent_hi) << 32;
1366 scsi_id->sbp2_command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
1367 scsi_id->sbp2_command_block_agent_addr &= 0x0000ffffffffffffULL;
1368 1146
1369 SBP2_INFO("Logged into SBP-2 device"); 1147 SBP2_INFO("Logged into SBP-2 device");
1370 return 0; 1148 return 0;
1371} 1149}
1372 1150
1373/* 1151static int sbp2_logout_device(struct sbp2_lu *lu)
1374 * This function is called in order to logout from a particular SBP-2
1375 * device, usually called during driver unload.
1376 */
1377static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
1378{ 1152{
1379 struct sbp2scsi_host_info *hi = scsi_id->hi; 1153 struct sbp2_fwhost_info *hi = lu->hi;
1380 quadlet_t data[2]; 1154 quadlet_t data[2];
1381 int error; 1155 int error;
1382 1156
1383 SBP2_DEBUG_ENTER(); 1157 lu->logout_orb->reserved1 = 0x0;
1384 1158 lu->logout_orb->reserved2 = 0x0;
1385 /* 1159 lu->logout_orb->reserved3 = 0x0;
1386 * Set-up logout ORB 1160 lu->logout_orb->reserved4 = 0x0;
1387 */
1388 scsi_id->logout_orb->reserved1 = 0x0;
1389 scsi_id->logout_orb->reserved2 = 0x0;
1390 scsi_id->logout_orb->reserved3 = 0x0;
1391 scsi_id->logout_orb->reserved4 = 0x0;
1392
1393 scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
1394 scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1395
1396 /* Notify us when complete */
1397 scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1398 1161
1399 scsi_id->logout_orb->reserved5 = 0x0; 1162 lu->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
1400 scsi_id->logout_orb->status_fifo_hi = 1163 lu->logout_orb->login_ID_misc |=
1401 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id); 1164 ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
1402 scsi_id->logout_orb->status_fifo_lo = 1165 lu->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1403 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
1404 1166
1405 /* 1167 lu->logout_orb->reserved5 = 0x0;
1406 * Byte swap ORB if necessary 1168 lu->logout_orb->status_fifo_hi =
1407 */ 1169 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1408 sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb)); 1170 lu->logout_orb->status_fifo_lo =
1171 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1409 1172
1410 sbp2util_packet_dump(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb), 1173 sbp2util_cpu_to_be32_buffer(lu->logout_orb,
1411 "sbp2 logout orb", scsi_id->logout_orb_dma); 1174 sizeof(struct sbp2_logout_orb));
1412 1175
1413 /*
1414 * Ok, let's write to the target's management agent register
1415 */
1416 data[0] = ORB_SET_NODE_ID(hi->host->node_id); 1176 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1417 data[1] = scsi_id->logout_orb_dma; 1177 data[1] = lu->logout_orb_dma;
1418 sbp2util_cpu_to_be32_buffer(data, 8); 1178 sbp2util_cpu_to_be32_buffer(data, 8);
1419 1179
1420 error = hpsb_node_write(scsi_id->ne, 1180 error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1421 scsi_id->sbp2_management_agent_addr, data, 8);
1422 if (error) 1181 if (error)
1423 return error; 1182 return error;
1424 1183
1425 /* Wait for device to logout...1 second. */ 1184 /* wait up to 1 second for the device to complete logout */
1426 if (sbp2util_access_timeout(scsi_id, HZ)) 1185 if (sbp2util_access_timeout(lu, HZ))
1427 return -EIO; 1186 return -EIO;
1428 1187
1429 SBP2_INFO("Logged out of SBP-2 device"); 1188 SBP2_INFO("Logged out of SBP-2 device");
1430 return 0; 1189 return 0;
1431} 1190}
1432 1191
1433/* 1192static int sbp2_reconnect_device(struct sbp2_lu *lu)
1434 * This function is called in order to reconnect to a particular SBP-2
1435 * device, after a bus reset.
1436 */
1437static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
1438{ 1193{
1439 struct sbp2scsi_host_info *hi = scsi_id->hi; 1194 struct sbp2_fwhost_info *hi = lu->hi;
1440 quadlet_t data[2]; 1195 quadlet_t data[2];
1441 int error; 1196 int error;
1442 1197
1443 SBP2_DEBUG_ENTER(); 1198 lu->reconnect_orb->reserved1 = 0x0;
1199 lu->reconnect_orb->reserved2 = 0x0;
1200 lu->reconnect_orb->reserved3 = 0x0;
1201 lu->reconnect_orb->reserved4 = 0x0;
1444 1202
1445 /* 1203 lu->reconnect_orb->login_ID_misc =
1446 * Set-up reconnect ORB 1204 ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
1447 */ 1205 lu->reconnect_orb->login_ID_misc |=
1448 scsi_id->reconnect_orb->reserved1 = 0x0; 1206 ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
1449 scsi_id->reconnect_orb->reserved2 = 0x0; 1207 lu->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1450 scsi_id->reconnect_orb->reserved3 = 0x0;
1451 scsi_id->reconnect_orb->reserved4 = 0x0;
1452
1453 scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
1454 scsi_id->reconnect_orb->login_ID_misc |=
1455 ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1456 1208
1457 /* Notify us when complete */ 1209 lu->reconnect_orb->reserved5 = 0x0;
1458 scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1); 1210 lu->reconnect_orb->status_fifo_hi =
1211 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1212 lu->reconnect_orb->status_fifo_lo =
1213 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1459 1214
1460 scsi_id->reconnect_orb->reserved5 = 0x0; 1215 sbp2util_cpu_to_be32_buffer(lu->reconnect_orb,
1461 scsi_id->reconnect_orb->status_fifo_hi = 1216 sizeof(struct sbp2_reconnect_orb));
1462 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1463 scsi_id->reconnect_orb->status_fifo_lo =
1464 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
1465
1466 /*
1467 * Byte swap ORB if necessary
1468 */
1469 sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
1470
1471 sbp2util_packet_dump(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb),
1472 "sbp2 reconnect orb", scsi_id->reconnect_orb_dma);
1473 1217
1474 data[0] = ORB_SET_NODE_ID(hi->host->node_id); 1218 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1475 data[1] = scsi_id->reconnect_orb_dma; 1219 data[1] = lu->reconnect_orb_dma;
1476 sbp2util_cpu_to_be32_buffer(data, 8); 1220 sbp2util_cpu_to_be32_buffer(data, 8);
1477 1221
1478 error = hpsb_node_write(scsi_id->ne, 1222 error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1479 scsi_id->sbp2_management_agent_addr, data, 8);
1480 if (error) 1223 if (error)
1481 return error; 1224 return error;
1482 1225
1483 /* 1226 /* wait up to 1 second for reconnect status */
1484 * Wait for reconnect status (up to 1 second)... 1227 if (sbp2util_access_timeout(lu, HZ)) {
1485 */
1486 if (sbp2util_access_timeout(scsi_id, HZ)) {
1487 SBP2_ERR("Error reconnecting to SBP-2 device - timed out"); 1228 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
1488 return -EIO; 1229 return -EIO;
1489 } 1230 }
1490 1231
1491 /* 1232 /* make sure that the returned status matches the reconnect ORB */
1492 * Sanity. Make sure status returned matches reconnect orb. 1233 if (lu->status_block.ORB_offset_lo != lu->reconnect_orb_dma) {
1493 */
1494 if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
1495 SBP2_ERR("Error reconnecting to SBP-2 device - timed out"); 1234 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
1496 return -EIO; 1235 return -EIO;
1497 } 1236 }
1498 1237
1499 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) { 1238 if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
1500 SBP2_ERR("Error reconnecting to SBP-2 device - failed"); 1239 SBP2_ERR("Error reconnecting to SBP-2 device - failed");
1501 return -EIO; 1240 return -EIO;
1502 } 1241 }
1503 1242
1504 HPSB_DEBUG("Reconnected to SBP-2 device"); 1243 SBP2_INFO("Reconnected to SBP-2 device");
1505 return 0; 1244 return 0;
1506} 1245}
1507 1246
1508/* 1247/*
1509 * This function is called in order to set the busy timeout (number of 1248 * Set the target node's Single Phase Retry limit. Affects the target's retry
1510 * retries to attempt) on the sbp2 device. 1249 * behaviour if our node is too busy to accept requests.
1511 */ 1250 */
1512static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id) 1251static int sbp2_set_busy_timeout(struct sbp2_lu *lu)
1513{ 1252{
1514 quadlet_t data; 1253 quadlet_t data;
1515 1254
1516 SBP2_DEBUG_ENTER();
1517
1518 data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE); 1255 data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
1519 if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4)) 1256 if (hpsb_node_write(lu->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
1520 SBP2_ERR("%s error", __FUNCTION__); 1257 SBP2_ERR("%s error", __FUNCTION__);
1521 return 0; 1258 return 0;
1522} 1259}
1523 1260
1524/* 1261static void sbp2_parse_unit_directory(struct sbp2_lu *lu,
1525 * This function is called to parse sbp2 device's config rom unit
1526 * directory. Used to determine things like sbp2 management agent offset,
1527 * and command set used (SCSI or RBC).
1528 */
1529static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
1530 struct unit_directory *ud) 1262 struct unit_directory *ud)
1531{ 1263{
1532 struct csr1212_keyval *kv; 1264 struct csr1212_keyval *kv;
1533 struct csr1212_dentry *dentry; 1265 struct csr1212_dentry *dentry;
1534 u64 management_agent_addr; 1266 u64 management_agent_addr;
1535 u32 command_set_spec_id, command_set, unit_characteristics, 1267 u32 unit_characteristics, firmware_revision;
1536 firmware_revision;
1537 unsigned workarounds; 1268 unsigned workarounds;
1538 int i; 1269 int i;
1539 1270
1540 SBP2_DEBUG_ENTER(); 1271 management_agent_addr = 0;
1272 unit_characteristics = 0;
1273 firmware_revision = 0;
1541 1274
1542 management_agent_addr = 0x0;
1543 command_set_spec_id = 0x0;
1544 command_set = 0x0;
1545 unit_characteristics = 0x0;
1546 firmware_revision = 0x0;
1547
1548 /* Handle different fields in the unit directory, based on keys */
1549 csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) { 1275 csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
1550 switch (kv->key.id) { 1276 switch (kv->key.id) {
1551 case CSR1212_KV_ID_DEPENDENT_INFO: 1277 case CSR1212_KV_ID_DEPENDENT_INFO:
1552 if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET) { 1278 if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET)
1553 /* Save off the management agent address */
1554 management_agent_addr = 1279 management_agent_addr =
1555 CSR1212_REGISTER_SPACE_BASE + 1280 CSR1212_REGISTER_SPACE_BASE +
1556 (kv->value.csr_offset << 2); 1281 (kv->value.csr_offset << 2);
1557 1282
1558 SBP2_DEBUG("sbp2_management_agent_addr = %x", 1283 else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE)
1559 (unsigned int)management_agent_addr); 1284 lu->lun = ORB_SET_LUN(kv->value.immediate);
1560 } else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
1561 scsi_id->sbp2_lun =
1562 ORB_SET_LUN(kv->value.immediate);
1563 }
1564 break;
1565
1566 case SBP2_COMMAND_SET_SPEC_ID_KEY:
1567 /* Command spec organization */
1568 command_set_spec_id = kv->value.immediate;
1569 SBP2_DEBUG("sbp2_command_set_spec_id = %x",
1570 (unsigned int)command_set_spec_id);
1571 break;
1572
1573 case SBP2_COMMAND_SET_KEY:
1574 /* Command set used by sbp2 device */
1575 command_set = kv->value.immediate;
1576 SBP2_DEBUG("sbp2_command_set = %x",
1577 (unsigned int)command_set);
1578 break; 1285 break;
1579 1286
1580 case SBP2_UNIT_CHARACTERISTICS_KEY: 1287 case SBP2_UNIT_CHARACTERISTICS_KEY:
1581 /* 1288 /* FIXME: This is ignored so far.
1582 * Unit characterisitcs (orb related stuff 1289 * See SBP-2 clause 7.4.8. */
1583 * that I'm not yet paying attention to)
1584 */
1585 unit_characteristics = kv->value.immediate; 1290 unit_characteristics = kv->value.immediate;
1586 SBP2_DEBUG("sbp2_unit_characteristics = %x",
1587 (unsigned int)unit_characteristics);
1588 break; 1291 break;
1589 1292
1590 case SBP2_FIRMWARE_REVISION_KEY: 1293 case SBP2_FIRMWARE_REVISION_KEY:
1591 /* Firmware revision */
1592 firmware_revision = kv->value.immediate; 1294 firmware_revision = kv->value.immediate;
1593 SBP2_DEBUG("sbp2_firmware_revision = %x",
1594 (unsigned int)firmware_revision);
1595 break; 1295 break;
1596 1296
1597 default: 1297 default:
1298 /* FIXME: Check for SBP2_DEVICE_TYPE_AND_LUN_KEY.
1299 * Its "ordered" bit has consequences for command ORB
1300 * list handling. See SBP-2 clauses 4.6, 7.4.11, 10.2 */
1598 break; 1301 break;
1599 } 1302 }
1600 } 1303 }
@@ -1626,28 +1329,24 @@ static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
1626 /* We would need one SCSI host template for each target to adjust 1329 /* We would need one SCSI host template for each target to adjust
1627 * max_sectors on the fly, therefore warn only. */ 1330 * max_sectors on the fly, therefore warn only. */
1628 if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS && 1331 if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
1629 (max_sectors * 512) > (128 * 1024)) 1332 (sbp2_max_sectors * 512) > (128 * 1024))
1630 SBP2_WARN("Node " NODE_BUS_FMT ": Bridge only supports 128KB " 1333 SBP2_INFO("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
1631 "max transfer size. WARNING: Current max_sectors " 1334 "max transfer size. WARNING: Current max_sectors "
1632 "setting is larger than 128KB (%d sectors)", 1335 "setting is larger than 128KB (%d sectors)",
1633 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid), 1336 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
1634 max_sectors); 1337 sbp2_max_sectors);
1635 1338
1636 /* If this is a logical unit directory entry, process the parent 1339 /* If this is a logical unit directory entry, process the parent
1637 * to get the values. */ 1340 * to get the values. */
1638 if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) { 1341 if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
1639 struct unit_directory *parent_ud = 1342 struct unit_directory *parent_ud = container_of(
1640 container_of(ud->device.parent, struct unit_directory, device); 1343 ud->device.parent, struct unit_directory, device);
1641 sbp2_parse_unit_directory(scsi_id, parent_ud); 1344 sbp2_parse_unit_directory(lu, parent_ud);
1642 } else { 1345 } else {
1643 scsi_id->sbp2_management_agent_addr = management_agent_addr; 1346 lu->management_agent_addr = management_agent_addr;
1644 scsi_id->sbp2_command_set_spec_id = command_set_spec_id; 1347 lu->workarounds = workarounds;
1645 scsi_id->sbp2_command_set = command_set;
1646 scsi_id->sbp2_unit_characteristics = unit_characteristics;
1647 scsi_id->sbp2_firmware_revision = firmware_revision;
1648 scsi_id->workarounds = workarounds;
1649 if (ud->flags & UNIT_DIRECTORY_HAS_LUN) 1348 if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
1650 scsi_id->sbp2_lun = ORB_SET_LUN(ud->lun); 1349 lu->lun = ORB_SET_LUN(ud->lun);
1651 } 1350 }
1652} 1351}
1653 1352
@@ -1662,133 +1361,114 @@ static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
1662 * the speed that it needs to use, and the max_rec the host supports, and 1361 * the speed that it needs to use, and the max_rec the host supports, and
1663 * it takes care of the rest. 1362 * it takes care of the rest.
1664 */ 1363 */
1665static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id) 1364static int sbp2_max_speed_and_size(struct sbp2_lu *lu)
1666{ 1365{
1667 struct sbp2scsi_host_info *hi = scsi_id->hi; 1366 struct sbp2_fwhost_info *hi = lu->hi;
1668 u8 payload; 1367 u8 payload;
1669 1368
1670 SBP2_DEBUG_ENTER(); 1369 lu->speed_code = hi->host->speed[NODEID_TO_NODE(lu->ne->nodeid)];
1671
1672 scsi_id->speed_code =
1673 hi->host->speed[NODEID_TO_NODE(scsi_id->ne->nodeid)];
1674 1370
1675 /* Bump down our speed if the user requested it */ 1371 if (lu->speed_code > sbp2_max_speed) {
1676 if (scsi_id->speed_code > max_speed) { 1372 lu->speed_code = sbp2_max_speed;
1677 scsi_id->speed_code = max_speed; 1373 SBP2_INFO("Reducing speed to %s",
1678 SBP2_ERR("Forcing SBP-2 max speed down to %s", 1374 hpsb_speedto_str[sbp2_max_speed]);
1679 hpsb_speedto_str[scsi_id->speed_code]);
1680 } 1375 }
1681 1376
1682 /* Payload size is the lesser of what our speed supports and what 1377 /* Payload size is the lesser of what our speed supports and what
1683 * our host supports. */ 1378 * our host supports. */
1684 payload = min(sbp2_speedto_max_payload[scsi_id->speed_code], 1379 payload = min(sbp2_speedto_max_payload[lu->speed_code],
1685 (u8) (hi->host->csr.max_rec - 1)); 1380 (u8) (hi->host->csr.max_rec - 1));
1686 1381
1687 /* If physical DMA is off, work around limitation in ohci1394: 1382 /* If physical DMA is off, work around limitation in ohci1394:
1688 * packet size must not exceed PAGE_SIZE */ 1383 * packet size must not exceed PAGE_SIZE */
1689 if (scsi_id->ne->host->low_addr_space < (1ULL << 32)) 1384 if (lu->ne->host->low_addr_space < (1ULL << 32))
1690 while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE && 1385 while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
1691 payload) 1386 payload)
1692 payload--; 1387 payload--;
1693 1388
1694 HPSB_DEBUG("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]", 1389 SBP2_INFO("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
1695 NODE_BUS_ARGS(hi->host, scsi_id->ne->nodeid), 1390 NODE_BUS_ARGS(hi->host, lu->ne->nodeid),
1696 hpsb_speedto_str[scsi_id->speed_code], 1391 hpsb_speedto_str[lu->speed_code],
1697 SBP2_PAYLOAD_TO_BYTES(payload)); 1392 SBP2_PAYLOAD_TO_BYTES(payload));
1698 1393
1699 scsi_id->max_payload_size = payload; 1394 lu->max_payload_size = payload;
1700 return 0; 1395 return 0;
1701} 1396}
1702 1397
1703/* 1398static int sbp2_agent_reset(struct sbp2_lu *lu, int wait)
1704 * This function is called in order to perform a SBP-2 agent reset.
1705 */
1706static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait)
1707{ 1399{
1708 quadlet_t data; 1400 quadlet_t data;
1709 u64 addr; 1401 u64 addr;
1710 int retval; 1402 int retval;
1711 unsigned long flags; 1403 unsigned long flags;
1712 1404
1713 SBP2_DEBUG_ENTER(); 1405 /* flush lu->protocol_work */
1714
1715 cancel_delayed_work(&scsi_id->protocol_work);
1716 if (wait) 1406 if (wait)
1717 flush_scheduled_work(); 1407 flush_scheduled_work();
1718 1408
1719 data = ntohl(SBP2_AGENT_RESET_DATA); 1409 data = ntohl(SBP2_AGENT_RESET_DATA);
1720 addr = scsi_id->sbp2_command_block_agent_addr + SBP2_AGENT_RESET_OFFSET; 1410 addr = lu->command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
1721 1411
1722 if (wait) 1412 if (wait)
1723 retval = hpsb_node_write(scsi_id->ne, addr, &data, 4); 1413 retval = hpsb_node_write(lu->ne, addr, &data, 4);
1724 else 1414 else
1725 retval = sbp2util_node_write_no_wait(scsi_id->ne, addr, &data, 4); 1415 retval = sbp2util_node_write_no_wait(lu->ne, addr, &data, 4);
1726 1416
1727 if (retval < 0) { 1417 if (retval < 0) {
1728 SBP2_ERR("hpsb_node_write failed.\n"); 1418 SBP2_ERR("hpsb_node_write failed.\n");
1729 return -EIO; 1419 return -EIO;
1730 } 1420 }
1731 1421
1732 /* 1422 /* make sure that the ORB_POINTER is written on next command */
1733 * Need to make sure orb pointer is written on next command 1423 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1734 */ 1424 lu->last_orb = NULL;
1735 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags); 1425 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1736 scsi_id->last_orb = NULL;
1737 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
1738 1426
1739 return 0; 1427 return 0;
1740} 1428}
1741 1429
1742static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb, 1430static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
1743 struct sbp2scsi_host_info *hi, 1431 struct sbp2_fwhost_info *hi,
1744 struct sbp2_command_info *command, 1432 struct sbp2_command_info *cmd,
1745 unsigned int scsi_use_sg, 1433 unsigned int scsi_use_sg,
1746 struct scatterlist *sgpnt, 1434 struct scatterlist *sgpnt,
1747 u32 orb_direction, 1435 u32 orb_direction,
1748 enum dma_data_direction dma_dir) 1436 enum dma_data_direction dma_dir)
1749{ 1437{
1750 command->dma_dir = dma_dir; 1438 cmd->dma_dir = dma_dir;
1751 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id); 1439 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1752 orb->misc |= ORB_SET_DIRECTION(orb_direction); 1440 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1753 1441
1754 /* Special case if only one element (and less than 64KB in size) */ 1442 /* special case if only one element (and less than 64KB in size) */
1755 if ((scsi_use_sg == 1) && 1443 if ((scsi_use_sg == 1) &&
1756 (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) { 1444 (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
1757 1445
1758 SBP2_DEBUG("Only one s/g element"); 1446 cmd->dma_size = sgpnt[0].length;
1759 command->dma_size = sgpnt[0].length; 1447 cmd->dma_type = CMD_DMA_PAGE;
1760 command->dma_type = CMD_DMA_PAGE; 1448 cmd->cmd_dma = dma_map_page(&hi->host->device,
1761 command->cmd_dma = pci_map_page(hi->host->pdev, 1449 sgpnt[0].page, sgpnt[0].offset,
1762 sgpnt[0].page, 1450 cmd->dma_size, cmd->dma_dir);
1763 sgpnt[0].offset,
1764 command->dma_size,
1765 command->dma_dir);
1766 SBP2_DMA_ALLOC("single page scatter element");
1767 1451
1768 orb->data_descriptor_lo = command->cmd_dma; 1452 orb->data_descriptor_lo = cmd->cmd_dma;
1769 orb->misc |= ORB_SET_DATA_SIZE(command->dma_size); 1453 orb->misc |= ORB_SET_DATA_SIZE(cmd->dma_size);
1770 1454
1771 } else { 1455 } else {
1772 struct sbp2_unrestricted_page_table *sg_element = 1456 struct sbp2_unrestricted_page_table *sg_element =
1773 &command->scatter_gather_element[0]; 1457 &cmd->scatter_gather_element[0];
1774 u32 sg_count, sg_len; 1458 u32 sg_count, sg_len;
1775 dma_addr_t sg_addr; 1459 dma_addr_t sg_addr;
1776 int i, count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg, 1460 int i, count = dma_map_sg(&hi->host->device, sgpnt, scsi_use_sg,
1777 dma_dir); 1461 dma_dir);
1778 1462
1779 SBP2_DMA_ALLOC("scatter list"); 1463 cmd->dma_size = scsi_use_sg;
1780 1464 cmd->sge_buffer = sgpnt;
1781 command->dma_size = scsi_use_sg;
1782 command->sge_buffer = sgpnt;
1783 1465
1784 /* use page tables (s/g) */ 1466 /* use page tables (s/g) */
1785 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1); 1467 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1786 orb->data_descriptor_lo = command->sge_dma; 1468 orb->data_descriptor_lo = cmd->sge_dma;
1787 1469
1788 /* 1470 /* loop through and fill out our SBP-2 page tables
1789 * Loop through and fill out our sbp-2 page tables 1471 * (and split up anything too large) */
1790 * (and split up anything too large)
1791 */
1792 for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) { 1472 for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
1793 sg_len = sg_dma_len(sgpnt); 1473 sg_len = sg_dma_len(sgpnt);
1794 sg_addr = sg_dma_address(sgpnt); 1474 sg_addr = sg_dma_address(sgpnt);
@@ -1808,70 +1488,53 @@ static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
1808 } 1488 }
1809 } 1489 }
1810 1490
1811 /* Number of page table (s/g) elements */
1812 orb->misc |= ORB_SET_DATA_SIZE(sg_count); 1491 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1813 1492
1814 sbp2util_packet_dump(sg_element,
1815 (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
1816 "sbp2 s/g list", command->sge_dma);
1817
1818 /* Byte swap page tables if necessary */
1819 sbp2util_cpu_to_be32_buffer(sg_element, 1493 sbp2util_cpu_to_be32_buffer(sg_element,
1820 (sizeof(struct sbp2_unrestricted_page_table)) * 1494 (sizeof(struct sbp2_unrestricted_page_table)) *
1821 sg_count); 1495 sg_count);
1822 } 1496 }
1823} 1497}
1824 1498
1825static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb, 1499static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
1826 struct sbp2scsi_host_info *hi, 1500 struct sbp2_fwhost_info *hi,
1827 struct sbp2_command_info *command, 1501 struct sbp2_command_info *cmd,
1828 struct scatterlist *sgpnt, 1502 struct scatterlist *sgpnt,
1829 u32 orb_direction, 1503 u32 orb_direction,
1830 unsigned int scsi_request_bufflen, 1504 unsigned int scsi_request_bufflen,
1831 void *scsi_request_buffer, 1505 void *scsi_request_buffer,
1832 enum dma_data_direction dma_dir) 1506 enum dma_data_direction dma_dir)
1833{ 1507{
1834 command->dma_dir = dma_dir; 1508 cmd->dma_dir = dma_dir;
1835 command->dma_size = scsi_request_bufflen; 1509 cmd->dma_size = scsi_request_bufflen;
1836 command->dma_type = CMD_DMA_SINGLE; 1510 cmd->dma_type = CMD_DMA_SINGLE;
1837 command->cmd_dma = pci_map_single(hi->host->pdev, scsi_request_buffer, 1511 cmd->cmd_dma = dma_map_single(&hi->host->device, scsi_request_buffer,
1838 command->dma_size, command->dma_dir); 1512 cmd->dma_size, cmd->dma_dir);
1839 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id); 1513 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1840 orb->misc |= ORB_SET_DIRECTION(orb_direction); 1514 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1841 1515
1842 SBP2_DMA_ALLOC("single bulk"); 1516 /* handle case where we get a command w/o s/g enabled
1843 1517 * (but check for transfers larger than 64K) */
1844 /*
1845 * Handle case where we get a command w/o s/g enabled (but
1846 * check for transfers larger than 64K)
1847 */
1848 if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) { 1518 if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1849 1519
1850 orb->data_descriptor_lo = command->cmd_dma; 1520 orb->data_descriptor_lo = cmd->cmd_dma;
1851 orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen); 1521 orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
1852 1522
1853 } else { 1523 } else {
1524 /* The buffer is too large. Turn this into page tables. */
1525
1854 struct sbp2_unrestricted_page_table *sg_element = 1526 struct sbp2_unrestricted_page_table *sg_element =
1855 &command->scatter_gather_element[0]; 1527 &cmd->scatter_gather_element[0];
1856 u32 sg_count, sg_len; 1528 u32 sg_count, sg_len;
1857 dma_addr_t sg_addr; 1529 dma_addr_t sg_addr;
1858 1530
1859 /* 1531 orb->data_descriptor_lo = cmd->sge_dma;
1860 * Need to turn this into page tables, since the
1861 * buffer is too large.
1862 */
1863 orb->data_descriptor_lo = command->sge_dma;
1864
1865 /* Use page tables (s/g) */
1866 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1); 1532 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1867 1533
1868 /* 1534 /* fill out our SBP-2 page tables; split up the large buffer */
1869 * fill out our sbp-2 page tables (and split up
1870 * the large buffer)
1871 */
1872 sg_count = 0; 1535 sg_count = 0;
1873 sg_len = scsi_request_bufflen; 1536 sg_len = scsi_request_bufflen;
1874 sg_addr = command->cmd_dma; 1537 sg_addr = cmd->cmd_dma;
1875 while (sg_len) { 1538 while (sg_len) {
1876 sg_element[sg_count].segment_base_lo = sg_addr; 1539 sg_element[sg_count].segment_base_lo = sg_addr;
1877 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) { 1540 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
@@ -1887,50 +1550,40 @@ static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
1887 sg_count++; 1550 sg_count++;
1888 } 1551 }
1889 1552
1890 /* Number of page table (s/g) elements */
1891 orb->misc |= ORB_SET_DATA_SIZE(sg_count); 1553 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1892 1554
1893 sbp2util_packet_dump(sg_element,
1894 (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
1895 "sbp2 s/g list", command->sge_dma);
1896
1897 /* Byte swap page tables if necessary */
1898 sbp2util_cpu_to_be32_buffer(sg_element, 1555 sbp2util_cpu_to_be32_buffer(sg_element,
1899 (sizeof(struct sbp2_unrestricted_page_table)) * 1556 (sizeof(struct sbp2_unrestricted_page_table)) *
1900 sg_count); 1557 sg_count);
1901 } 1558 }
1902} 1559}
1903 1560
1904/* 1561static void sbp2_create_command_orb(struct sbp2_lu *lu,
1905 * This function is called to create the actual command orb and s/g list 1562 struct sbp2_command_info *cmd,
1906 * out of the scsi command itself.
1907 */
1908static void sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
1909 struct sbp2_command_info *command,
1910 unchar *scsi_cmd, 1563 unchar *scsi_cmd,
1911 unsigned int scsi_use_sg, 1564 unsigned int scsi_use_sg,
1912 unsigned int scsi_request_bufflen, 1565 unsigned int scsi_request_bufflen,
1913 void *scsi_request_buffer, 1566 void *scsi_request_buffer,
1914 enum dma_data_direction dma_dir) 1567 enum dma_data_direction dma_dir)
1915{ 1568{
1916 struct sbp2scsi_host_info *hi = scsi_id->hi; 1569 struct sbp2_fwhost_info *hi = lu->hi;
1917 struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer; 1570 struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
1918 struct sbp2_command_orb *command_orb = &command->command_orb; 1571 struct sbp2_command_orb *orb = &cmd->command_orb;
1919 u32 orb_direction; 1572 u32 orb_direction;
1920 1573
1921 /* 1574 /*
1922 * Set-up our command ORB.. 1575 * Set-up our command ORB.
1923 * 1576 *
1924 * NOTE: We're doing unrestricted page tables (s/g), as this is 1577 * NOTE: We're doing unrestricted page tables (s/g), as this is
1925 * best performance (at least with the devices I have). This means 1578 * best performance (at least with the devices I have). This means
1926 * that data_size becomes the number of s/g elements, and 1579 * that data_size becomes the number of s/g elements, and
1927 * page_size should be zero (for unrestricted). 1580 * page_size should be zero (for unrestricted).
1928 */ 1581 */
1929 command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1); 1582 orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1930 command_orb->next_ORB_lo = 0x0; 1583 orb->next_ORB_lo = 0x0;
1931 command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size); 1584 orb->misc = ORB_SET_MAX_PAYLOAD(lu->max_payload_size);
1932 command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code); 1585 orb->misc |= ORB_SET_SPEED(lu->speed_code);
1933 command_orb->misc |= ORB_SET_NOTIFY(1); /* Notify us when complete */ 1586 orb->misc |= ORB_SET_NOTIFY(1);
1934 1587
1935 if (dma_dir == DMA_NONE) 1588 if (dma_dir == DMA_NONE)
1936 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER; 1589 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
@@ -1939,66 +1592,51 @@ static void sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
1939 else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen) 1592 else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
1940 orb_direction = ORB_DIRECTION_READ_FROM_MEDIA; 1593 orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
1941 else { 1594 else {
1942 SBP2_WARN("Falling back to DMA_NONE"); 1595 SBP2_INFO("Falling back to DMA_NONE");
1943 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER; 1596 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1944 } 1597 }
1945 1598
1946 /* Set-up our pagetable stuff */ 1599 /* set up our page table stuff */
1947 if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) { 1600 if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
1948 SBP2_DEBUG("No data transfer"); 1601 orb->data_descriptor_hi = 0x0;
1949 command_orb->data_descriptor_hi = 0x0; 1602 orb->data_descriptor_lo = 0x0;
1950 command_orb->data_descriptor_lo = 0x0; 1603 orb->misc |= ORB_SET_DIRECTION(1);
1951 command_orb->misc |= ORB_SET_DIRECTION(1); 1604 } else if (scsi_use_sg)
1952 } else if (scsi_use_sg) { 1605 sbp2_prep_command_orb_sg(orb, hi, cmd, scsi_use_sg, sgpnt,
1953 SBP2_DEBUG("Use scatter/gather"); 1606 orb_direction, dma_dir);
1954 sbp2_prep_command_orb_sg(command_orb, hi, command, scsi_use_sg, 1607 else
1955 sgpnt, orb_direction, dma_dir); 1608 sbp2_prep_command_orb_no_sg(orb, hi, cmd, sgpnt, orb_direction,
1956 } else { 1609 scsi_request_bufflen,
1957 SBP2_DEBUG("No scatter/gather");
1958 sbp2_prep_command_orb_no_sg(command_orb, hi, command, sgpnt,
1959 orb_direction, scsi_request_bufflen,
1960 scsi_request_buffer, dma_dir); 1610 scsi_request_buffer, dma_dir);
1961 }
1962 1611
1963 /* Byte swap command ORB if necessary */ 1612 sbp2util_cpu_to_be32_buffer(orb, sizeof(*orb));
1964 sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
1965 1613
1966 /* Put our scsi command in the command ORB */ 1614 memset(orb->cdb, 0, 12);
1967 memset(command_orb->cdb, 0, 12); 1615 memcpy(orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
1968 memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
1969} 1616}
1970 1617
1971/* 1618static void sbp2_link_orb_command(struct sbp2_lu *lu,
1972 * This function is called in order to begin a regular SBP-2 command. 1619 struct sbp2_command_info *cmd)
1973 */
1974static void sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
1975 struct sbp2_command_info *command)
1976{ 1620{
1977 struct sbp2scsi_host_info *hi = scsi_id->hi; 1621 struct sbp2_fwhost_info *hi = lu->hi;
1978 struct sbp2_command_orb *command_orb = &command->command_orb;
1979 struct sbp2_command_orb *last_orb; 1622 struct sbp2_command_orb *last_orb;
1980 dma_addr_t last_orb_dma; 1623 dma_addr_t last_orb_dma;
1981 u64 addr = scsi_id->sbp2_command_block_agent_addr; 1624 u64 addr = lu->command_block_agent_addr;
1982 quadlet_t data[2]; 1625 quadlet_t data[2];
1983 size_t length; 1626 size_t length;
1984 unsigned long flags; 1627 unsigned long flags;
1985 1628
1986 outstanding_orb_incr; 1629 dma_sync_single_for_device(&hi->host->device, cmd->command_orb_dma,
1987 SBP2_ORB_DEBUG("sending command orb %p, total orbs = %x", 1630 sizeof(struct sbp2_command_orb),
1988 command_orb, global_outstanding_command_orbs); 1631 DMA_TO_DEVICE);
1989 1632 dma_sync_single_for_device(&hi->host->device, cmd->sge_dma,
1990 pci_dma_sync_single_for_device(hi->host->pdev, command->command_orb_dma, 1633 sizeof(cmd->scatter_gather_element),
1991 sizeof(struct sbp2_command_orb), 1634 DMA_BIDIRECTIONAL);
1992 PCI_DMA_TODEVICE); 1635
1993 pci_dma_sync_single_for_device(hi->host->pdev, command->sge_dma, 1636 /* check to see if there are any previous orbs to use */
1994 sizeof(command->scatter_gather_element), 1637 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1995 PCI_DMA_BIDIRECTIONAL); 1638 last_orb = lu->last_orb;
1996 /* 1639 last_orb_dma = lu->last_orb_dma;
1997 * Check to see if there are any previous orbs to use
1998 */
1999 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
2000 last_orb = scsi_id->last_orb;
2001 last_orb_dma = scsi_id->last_orb_dma;
2002 if (!last_orb) { 1640 if (!last_orb) {
2003 /* 1641 /*
2004 * last_orb == NULL means: We know that the target's fetch agent 1642 * last_orb == NULL means: We know that the target's fetch agent
@@ -2006,7 +1644,7 @@ static void sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
2006 */ 1644 */
2007 addr += SBP2_ORB_POINTER_OFFSET; 1645 addr += SBP2_ORB_POINTER_OFFSET;
2008 data[0] = ORB_SET_NODE_ID(hi->host->node_id); 1646 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
2009 data[1] = command->command_orb_dma; 1647 data[1] = cmd->command_orb_dma;
2010 sbp2util_cpu_to_be32_buffer(data, 8); 1648 sbp2util_cpu_to_be32_buffer(data, 8);
2011 length = 8; 1649 length = 8;
2012 } else { 1650 } else {
@@ -2017,27 +1655,25 @@ static void sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
2017 * The target's fetch agent may or may not have read this 1655 * The target's fetch agent may or may not have read this
2018 * previous ORB yet. 1656 * previous ORB yet.
2019 */ 1657 */
2020 pci_dma_sync_single_for_cpu(hi->host->pdev, last_orb_dma, 1658 dma_sync_single_for_cpu(&hi->host->device, last_orb_dma,
2021 sizeof(struct sbp2_command_orb), 1659 sizeof(struct sbp2_command_orb),
2022 PCI_DMA_TODEVICE); 1660 DMA_TO_DEVICE);
2023 last_orb->next_ORB_lo = cpu_to_be32(command->command_orb_dma); 1661 last_orb->next_ORB_lo = cpu_to_be32(cmd->command_orb_dma);
2024 wmb(); 1662 wmb();
2025 /* Tells hardware that this pointer is valid */ 1663 /* Tells hardware that this pointer is valid */
2026 last_orb->next_ORB_hi = 0; 1664 last_orb->next_ORB_hi = 0;
2027 pci_dma_sync_single_for_device(hi->host->pdev, last_orb_dma, 1665 dma_sync_single_for_device(&hi->host->device, last_orb_dma,
2028 sizeof(struct sbp2_command_orb), 1666 sizeof(struct sbp2_command_orb),
2029 PCI_DMA_TODEVICE); 1667 DMA_TO_DEVICE);
2030 addr += SBP2_DOORBELL_OFFSET; 1668 addr += SBP2_DOORBELL_OFFSET;
2031 data[0] = 0; 1669 data[0] = 0;
2032 length = 4; 1670 length = 4;
2033 } 1671 }
2034 scsi_id->last_orb = command_orb; 1672 lu->last_orb = &cmd->command_orb;
2035 scsi_id->last_orb_dma = command->command_orb_dma; 1673 lu->last_orb_dma = cmd->command_orb_dma;
2036 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags); 1674 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
2037 1675
2038 SBP2_ORB_DEBUG("write to %s register, command orb %p", 1676 if (sbp2util_node_write_no_wait(lu->ne, addr, data, length)) {
2039 last_orb ? "DOORBELL" : "ORB_POINTER", command_orb);
2040 if (sbp2util_node_write_no_wait(scsi_id->ne, addr, data, length)) {
2041 /* 1677 /*
2042 * sbp2util_node_write_no_wait failed. We certainly ran out 1678 * sbp2util_node_write_no_wait failed. We certainly ran out
2043 * of transaction labels, perhaps just because there were no 1679 * of transaction labels, perhaps just because there were no
@@ -2046,52 +1682,29 @@ static void sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
2046 * the workqueue job will sleep to guaranteedly get a tlabel. 1682 * the workqueue job will sleep to guaranteedly get a tlabel.
2047 * We do not accept new commands until the job is over. 1683 * We do not accept new commands until the job is over.
2048 */ 1684 */
2049 scsi_block_requests(scsi_id->scsi_host); 1685 scsi_block_requests(lu->shost);
2050 PREPARE_WORK(&scsi_id->protocol_work, 1686 PREPARE_WORK(&lu->protocol_work,
2051 last_orb ? sbp2util_write_doorbell: 1687 last_orb ? sbp2util_write_doorbell:
2052 sbp2util_write_orb_pointer, 1688 sbp2util_write_orb_pointer);
2053 scsi_id); 1689 schedule_work(&lu->protocol_work);
2054 schedule_work(&scsi_id->protocol_work);
2055 } 1690 }
2056} 1691}
2057 1692
2058/* 1693static int sbp2_send_command(struct sbp2_lu *lu, struct scsi_cmnd *SCpnt,
2059 * This function is called in order to begin a regular SBP-2 command.
2060 */
2061static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
2062 struct scsi_cmnd *SCpnt,
2063 void (*done)(struct scsi_cmnd *)) 1694 void (*done)(struct scsi_cmnd *))
2064{ 1695{
2065 unchar *cmd = (unchar *) SCpnt->cmnd; 1696 unchar *scsi_cmd = (unchar *)SCpnt->cmnd;
2066 unsigned int request_bufflen = SCpnt->request_bufflen; 1697 unsigned int request_bufflen = SCpnt->request_bufflen;
2067 struct sbp2_command_info *command; 1698 struct sbp2_command_info *cmd;
2068 1699
2069 SBP2_DEBUG_ENTER(); 1700 cmd = sbp2util_allocate_command_orb(lu, SCpnt, done);
2070 SBP2_DEBUG("SCSI transfer size = %x", request_bufflen); 1701 if (!cmd)
2071 SBP2_DEBUG("SCSI s/g elements = %x", (unsigned int)SCpnt->use_sg);
2072
2073 /*
2074 * Allocate a command orb and s/g structure
2075 */
2076 command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done);
2077 if (!command) {
2078 return -EIO; 1702 return -EIO;
2079 }
2080 1703
2081 /* 1704 sbp2_create_command_orb(lu, cmd, scsi_cmd, SCpnt->use_sg,
2082 * Now actually fill in the comamnd orb and sbp2 s/g list
2083 */
2084 sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg,
2085 request_bufflen, SCpnt->request_buffer, 1705 request_bufflen, SCpnt->request_buffer,
2086 SCpnt->sc_data_direction); 1706 SCpnt->sc_data_direction);
2087 1707 sbp2_link_orb_command(lu, cmd);
2088 sbp2util_packet_dump(&command->command_orb, sizeof(struct sbp2_command_orb),
2089 "sbp2 command orb", command->command_orb_dma);
2090
2091 /*
2092 * Link up the orb, and ring the doorbell if needed
2093 */
2094 sbp2_link_orb_command(scsi_id, command);
2095 1708
2096 return 0; 1709 return 0;
2097} 1710}
@@ -2099,13 +1712,10 @@ static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
2099/* 1712/*
2100 * Translates SBP-2 status into SCSI sense data for check conditions 1713 * Translates SBP-2 status into SCSI sense data for check conditions
2101 */ 1714 */
2102static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data) 1715static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status,
1716 unchar *sense_data)
2103{ 1717{
2104 SBP2_DEBUG_ENTER(); 1718 /* OK, it's pretty ugly... ;-) */
2105
2106 /*
2107 * Ok, it's pretty ugly... ;-)
2108 */
2109 sense_data[0] = 0x70; 1719 sense_data[0] = 0x70;
2110 sense_data[1] = 0x0; 1720 sense_data[1] = 0x0;
2111 sense_data[2] = sbp2_status[9]; 1721 sense_data[2] = sbp2_status[9];
@@ -2123,28 +1733,21 @@ static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense
2123 sense_data[14] = sbp2_status[20]; 1733 sense_data[14] = sbp2_status[20];
2124 sense_data[15] = sbp2_status[21]; 1734 sense_data[15] = sbp2_status[21];
2125 1735
2126 return sbp2_status[8] & 0x3f; /* return scsi status */ 1736 return sbp2_status[8] & 0x3f;
2127} 1737}
2128 1738
2129/*
2130 * This function deals with status writes from the SBP-2 device
2131 */
2132static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid, 1739static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
2133 int destid, quadlet_t *data, u64 addr, 1740 int destid, quadlet_t *data, u64 addr,
2134 size_t length, u16 fl) 1741 size_t length, u16 fl)
2135{ 1742{
2136 struct sbp2scsi_host_info *hi; 1743 struct sbp2_fwhost_info *hi;
2137 struct scsi_id_instance_data *scsi_id = NULL, *scsi_id_tmp; 1744 struct sbp2_lu *lu = NULL, *lu_tmp;
2138 struct scsi_cmnd *SCpnt = NULL; 1745 struct scsi_cmnd *SCpnt = NULL;
2139 struct sbp2_status_block *sb; 1746 struct sbp2_status_block *sb;
2140 u32 scsi_status = SBP2_SCSI_STATUS_GOOD; 1747 u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
2141 struct sbp2_command_info *command; 1748 struct sbp2_command_info *cmd;
2142 unsigned long flags; 1749 unsigned long flags;
2143 1750
2144 SBP2_DEBUG_ENTER();
2145
2146 sbp2util_packet_dump(data, length, "sbp2 status write by device", (u32)addr);
2147
2148 if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) { 1751 if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
2149 SBP2_ERR("Wrong size of status block"); 1752 SBP2_ERR("Wrong size of status block");
2150 return RCODE_ADDRESS_ERROR; 1753 return RCODE_ADDRESS_ERROR;
@@ -2158,131 +1761,97 @@ static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
2158 SBP2_ERR("host info is NULL - this is bad!"); 1761 SBP2_ERR("host info is NULL - this is bad!");
2159 return RCODE_ADDRESS_ERROR; 1762 return RCODE_ADDRESS_ERROR;
2160 } 1763 }
2161 /* 1764
2162 * Find our scsi_id structure by looking at the status fifo address 1765 /* Find the unit which wrote the status. */
2163 * written to by the sbp2 device. 1766 list_for_each_entry(lu_tmp, &hi->logical_units, lu_list) {
2164 */ 1767 if (lu_tmp->ne->nodeid == nodeid &&
2165 list_for_each_entry(scsi_id_tmp, &hi->scsi_ids, scsi_list) { 1768 lu_tmp->status_fifo_addr == addr) {
2166 if (scsi_id_tmp->ne->nodeid == nodeid && 1769 lu = lu_tmp;
2167 scsi_id_tmp->status_fifo_addr == addr) {
2168 scsi_id = scsi_id_tmp;
2169 break; 1770 break;
2170 } 1771 }
2171 } 1772 }
2172 if (unlikely(!scsi_id)) { 1773 if (unlikely(!lu)) {
2173 SBP2_ERR("scsi_id is NULL - device is gone?"); 1774 SBP2_ERR("lu is NULL - device is gone?");
2174 return RCODE_ADDRESS_ERROR; 1775 return RCODE_ADDRESS_ERROR;
2175 } 1776 }
2176 1777
2177 /* 1778 /* Put response into lu status fifo buffer. The first two bytes
2178 * Put response into scsi_id status fifo buffer. The first two bytes
2179 * come in big endian bit order. Often the target writes only a 1779 * come in big endian bit order. Often the target writes only a
2180 * truncated status block, minimally the first two quadlets. The rest 1780 * truncated status block, minimally the first two quadlets. The rest
2181 * is implied to be zeros. 1781 * is implied to be zeros. */
2182 */ 1782 sb = &lu->status_block;
2183 sb = &scsi_id->status_block;
2184 memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent)); 1783 memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
2185 memcpy(sb, data, length); 1784 memcpy(sb, data, length);
2186 sbp2util_be32_to_cpu_buffer(sb, 8); 1785 sbp2util_be32_to_cpu_buffer(sb, 8);
2187 1786
2188 /* 1787 /* Ignore unsolicited status. Handle command ORB status. */
2189 * Ignore unsolicited status. Handle command ORB status.
2190 */
2191 if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2)) 1788 if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
2192 command = NULL; 1789 cmd = NULL;
2193 else 1790 else
2194 command = sbp2util_find_command_for_orb(scsi_id, 1791 cmd = sbp2util_find_command_for_orb(lu, sb->ORB_offset_lo);
2195 sb->ORB_offset_lo); 1792 if (cmd) {
2196 if (command) { 1793 dma_sync_single_for_cpu(&hi->host->device, cmd->command_orb_dma,
2197 SBP2_DEBUG("Found status for command ORB"); 1794 sizeof(struct sbp2_command_orb),
2198 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma, 1795 DMA_TO_DEVICE);
2199 sizeof(struct sbp2_command_orb), 1796 dma_sync_single_for_cpu(&hi->host->device, cmd->sge_dma,
2200 PCI_DMA_TODEVICE); 1797 sizeof(cmd->scatter_gather_element),
2201 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma, 1798 DMA_BIDIRECTIONAL);
2202 sizeof(command->scatter_gather_element), 1799 /* Grab SCSI command pointers and check status. */
2203 PCI_DMA_BIDIRECTIONAL);
2204
2205 SBP2_ORB_DEBUG("matched command orb %p", &command->command_orb);
2206 outstanding_orb_decr;
2207
2208 /*
2209 * Matched status with command, now grab scsi command pointers
2210 * and check status.
2211 */
2212 /* 1800 /*
2213 * FIXME: If the src field in the status is 1, the ORB DMA must 1801 * FIXME: If the src field in the status is 1, the ORB DMA must
2214 * not be reused until status for a subsequent ORB is received. 1802 * not be reused until status for a subsequent ORB is received.
2215 */ 1803 */
2216 SCpnt = command->Current_SCpnt; 1804 SCpnt = cmd->Current_SCpnt;
2217 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags); 1805 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
2218 sbp2util_mark_command_completed(scsi_id, command); 1806 sbp2util_mark_command_completed(lu, cmd);
2219 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags); 1807 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
2220 1808
2221 if (SCpnt) { 1809 if (SCpnt) {
2222 u32 h = sb->ORB_offset_hi_misc; 1810 u32 h = sb->ORB_offset_hi_misc;
2223 u32 r = STATUS_GET_RESP(h); 1811 u32 r = STATUS_GET_RESP(h);
2224 1812
2225 if (r != RESP_STATUS_REQUEST_COMPLETE) { 1813 if (r != RESP_STATUS_REQUEST_COMPLETE) {
2226 SBP2_WARN("resp 0x%x, sbp_status 0x%x", 1814 SBP2_INFO("resp 0x%x, sbp_status 0x%x",
2227 r, STATUS_GET_SBP_STATUS(h)); 1815 r, STATUS_GET_SBP_STATUS(h));
2228 scsi_status = 1816 scsi_status =
2229 r == RESP_STATUS_TRANSPORT_FAILURE ? 1817 r == RESP_STATUS_TRANSPORT_FAILURE ?
2230 SBP2_SCSI_STATUS_BUSY : 1818 SBP2_SCSI_STATUS_BUSY :
2231 SBP2_SCSI_STATUS_COMMAND_TERMINATED; 1819 SBP2_SCSI_STATUS_COMMAND_TERMINATED;
2232 } 1820 }
2233 /* 1821
2234 * See if the target stored any scsi status information. 1822 if (STATUS_GET_LEN(h) > 1)
2235 */
2236 if (STATUS_GET_LEN(h) > 1) {
2237 SBP2_DEBUG("CHECK CONDITION");
2238 scsi_status = sbp2_status_to_sense_data( 1823 scsi_status = sbp2_status_to_sense_data(
2239 (unchar *)sb, SCpnt->sense_buffer); 1824 (unchar *)sb, SCpnt->sense_buffer);
2240 } 1825
2241 /* 1826 if (STATUS_TEST_DEAD(h))
2242 * Check to see if the dead bit is set. If so, we'll 1827 sbp2_agent_reset(lu, 0);
2243 * have to initiate a fetch agent reset.
2244 */
2245 if (STATUS_TEST_DEAD(h)) {
2246 SBP2_DEBUG("Dead bit set - "
2247 "initiating fetch agent reset");
2248 sbp2_agent_reset(scsi_id, 0);
2249 }
2250 SBP2_ORB_DEBUG("completing command orb %p", &command->command_orb);
2251 } 1828 }
2252 1829
2253 /* 1830 /* Check here to see if there are no commands in-use. If there
2254 * Check here to see if there are no commands in-use. If there
2255 * are none, we know that the fetch agent left the active state 1831 * are none, we know that the fetch agent left the active state
2256 * _and_ that we did not reactivate it yet. Therefore clear 1832 * _and_ that we did not reactivate it yet. Therefore clear
2257 * last_orb so that next time we write directly to the 1833 * last_orb so that next time we write directly to the
2258 * ORB_POINTER register. That way the fetch agent does not need 1834 * ORB_POINTER register. That way the fetch agent does not need
2259 * to refetch the next_ORB. 1835 * to refetch the next_ORB. */
2260 */ 1836 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
2261 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags); 1837 if (list_empty(&lu->cmd_orb_inuse))
2262 if (list_empty(&scsi_id->sbp2_command_orb_inuse)) 1838 lu->last_orb = NULL;
2263 scsi_id->last_orb = NULL; 1839 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
2264 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
2265 1840
2266 } else { 1841 } else {
2267 /* 1842 /* It's probably status after a management request. */
2268 * It's probably a login/logout/reconnect status. 1843 if ((sb->ORB_offset_lo == lu->reconnect_orb_dma) ||
2269 */ 1844 (sb->ORB_offset_lo == lu->login_orb_dma) ||
2270 if ((sb->ORB_offset_lo == scsi_id->reconnect_orb_dma) || 1845 (sb->ORB_offset_lo == lu->query_logins_orb_dma) ||
2271 (sb->ORB_offset_lo == scsi_id->login_orb_dma) || 1846 (sb->ORB_offset_lo == lu->logout_orb_dma)) {
2272 (sb->ORB_offset_lo == scsi_id->query_logins_orb_dma) || 1847 lu->access_complete = 1;
2273 (sb->ORB_offset_lo == scsi_id->logout_orb_dma)) { 1848 wake_up_interruptible(&sbp2_access_wq);
2274 scsi_id->access_complete = 1;
2275 wake_up_interruptible(&access_wq);
2276 } 1849 }
2277 } 1850 }
2278 1851
2279 if (SCpnt) { 1852 if (SCpnt)
2280 SBP2_DEBUG("Completing SCSI command"); 1853 sbp2scsi_complete_command(lu, scsi_status, SCpnt,
2281 sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt, 1854 cmd->Current_done);
2282 command->Current_done);
2283 SBP2_ORB_DEBUG("command orb completed");
2284 }
2285
2286 return RCODE_COMPLETE; 1855 return RCODE_COMPLETE;
2287} 1856}
2288 1857
@@ -2290,77 +1859,57 @@ static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
2290 * SCSI interface related section 1859 * SCSI interface related section
2291 **************************************/ 1860 **************************************/
2292 1861
2293/*
2294 * This routine is the main request entry routine for doing I/O. It is
2295 * called from the scsi stack directly.
2296 */
2297static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt, 1862static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
2298 void (*done)(struct scsi_cmnd *)) 1863 void (*done)(struct scsi_cmnd *))
2299{ 1864{
2300 struct scsi_id_instance_data *scsi_id = 1865 struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
2301 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0]; 1866 struct sbp2_fwhost_info *hi;
2302 struct sbp2scsi_host_info *hi;
2303 int result = DID_NO_CONNECT << 16; 1867 int result = DID_NO_CONNECT << 16;
2304 1868
2305 SBP2_DEBUG_ENTER(); 1869 if (unlikely(!sbp2util_node_is_available(lu)))
2306#if (CONFIG_IEEE1394_SBP2_DEBUG >= 2) || defined(CONFIG_IEEE1394_SBP2_PACKET_DUMP)
2307 scsi_print_command(SCpnt);
2308#endif
2309
2310 if (!sbp2util_node_is_available(scsi_id))
2311 goto done; 1870 goto done;
2312 1871
2313 hi = scsi_id->hi; 1872 hi = lu->hi;
2314 1873
2315 if (!hi) { 1874 if (unlikely(!hi)) {
2316 SBP2_ERR("sbp2scsi_host_info is NULL - this is bad!"); 1875 SBP2_ERR("sbp2_fwhost_info is NULL - this is bad!");
2317 goto done; 1876 goto done;
2318 } 1877 }
2319 1878
2320 /* 1879 /* Multiple units are currently represented to the SCSI core as separate
2321 * Until we handle multiple luns, just return selection time-out 1880 * targets, not as one target with multiple LUs. Therefore return
2322 * to any IO directed at non-zero LUNs 1881 * selection time-out to any IO directed at non-zero LUNs. */
2323 */ 1882 if (unlikely(SCpnt->device->lun))
2324 if (SCpnt->device->lun)
2325 goto done; 1883 goto done;
2326 1884
2327 /* 1885 /* handle the request sense command here (auto-request sense) */
2328 * Check for request sense command, and handle it here
2329 * (autorequest sense)
2330 */
2331 if (SCpnt->cmnd[0] == REQUEST_SENSE) { 1886 if (SCpnt->cmnd[0] == REQUEST_SENSE) {
2332 SBP2_DEBUG("REQUEST_SENSE"); 1887 memcpy(SCpnt->request_buffer, SCpnt->sense_buffer,
2333 memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen); 1888 SCpnt->request_bufflen);
2334 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer)); 1889 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
2335 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done); 1890 sbp2scsi_complete_command(lu, SBP2_SCSI_STATUS_GOOD, SCpnt,
1891 done);
2336 return 0; 1892 return 0;
2337 } 1893 }
2338 1894
2339 /* 1895 if (unlikely(!hpsb_node_entry_valid(lu->ne))) {
2340 * Check to see if we are in the middle of a bus reset.
2341 */
2342 if (!hpsb_node_entry_valid(scsi_id->ne)) {
2343 SBP2_ERR("Bus reset in progress - rejecting command"); 1896 SBP2_ERR("Bus reset in progress - rejecting command");
2344 result = DID_BUS_BUSY << 16; 1897 result = DID_BUS_BUSY << 16;
2345 goto done; 1898 goto done;
2346 } 1899 }
2347 1900
2348 /* 1901 /* Bidirectional commands are not yet implemented,
2349 * Bidirectional commands are not yet implemented, 1902 * and unknown transfer direction not handled. */
2350 * and unknown transfer direction not handled. 1903 if (unlikely(SCpnt->sc_data_direction == DMA_BIDIRECTIONAL)) {
2351 */
2352 if (SCpnt->sc_data_direction == DMA_BIDIRECTIONAL) {
2353 SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command"); 1904 SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
2354 result = DID_ERROR << 16; 1905 result = DID_ERROR << 16;
2355 goto done; 1906 goto done;
2356 } 1907 }
2357 1908
2358 /* 1909 if (sbp2_send_command(lu, SCpnt, done)) {
2359 * Try and send our SCSI command
2360 */
2361 if (sbp2_send_command(scsi_id, SCpnt, done)) {
2362 SBP2_ERR("Error sending SCSI command"); 1910 SBP2_ERR("Error sending SCSI command");
2363 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT, 1911 sbp2scsi_complete_command(lu,
1912 SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
2364 SCpnt, done); 1913 SCpnt, done);
2365 } 1914 }
2366 return 0; 1915 return 0;
@@ -2371,75 +1920,46 @@ done:
2371 return 0; 1920 return 0;
2372} 1921}
2373 1922
2374/* 1923static void sbp2scsi_complete_all_commands(struct sbp2_lu *lu, u32 status)
2375 * This function is called in order to complete all outstanding SBP-2
2376 * commands (in case of resets, etc.).
2377 */
2378static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
2379 u32 status)
2380{ 1924{
2381 struct sbp2scsi_host_info *hi = scsi_id->hi; 1925 struct sbp2_fwhost_info *hi = lu->hi;
2382 struct list_head *lh; 1926 struct list_head *lh;
2383 struct sbp2_command_info *command; 1927 struct sbp2_command_info *cmd;
2384 unsigned long flags; 1928 unsigned long flags;
2385 1929
2386 SBP2_DEBUG_ENTER(); 1930 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
2387 1931 while (!list_empty(&lu->cmd_orb_inuse)) {
2388 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags); 1932 lh = lu->cmd_orb_inuse.next;
2389 while (!list_empty(&scsi_id->sbp2_command_orb_inuse)) { 1933 cmd = list_entry(lh, struct sbp2_command_info, list);
2390 SBP2_DEBUG("Found pending command to complete"); 1934 dma_sync_single_for_cpu(&hi->host->device, cmd->command_orb_dma,
2391 lh = scsi_id->sbp2_command_orb_inuse.next; 1935 sizeof(struct sbp2_command_orb),
2392 command = list_entry(lh, struct sbp2_command_info, list); 1936 DMA_TO_DEVICE);
2393 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma, 1937 dma_sync_single_for_cpu(&hi->host->device, cmd->sge_dma,
2394 sizeof(struct sbp2_command_orb), 1938 sizeof(cmd->scatter_gather_element),
2395 PCI_DMA_TODEVICE); 1939 DMA_BIDIRECTIONAL);
2396 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma, 1940 sbp2util_mark_command_completed(lu, cmd);
2397 sizeof(command->scatter_gather_element), 1941 if (cmd->Current_SCpnt) {
2398 PCI_DMA_BIDIRECTIONAL); 1942 cmd->Current_SCpnt->result = status << 16;
2399 sbp2util_mark_command_completed(scsi_id, command); 1943 cmd->Current_done(cmd->Current_SCpnt);
2400 if (command->Current_SCpnt) {
2401 command->Current_SCpnt->result = status << 16;
2402 command->Current_done(command->Current_SCpnt);
2403 } 1944 }
2404 } 1945 }
2405 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags); 1946 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
2406 1947
2407 return; 1948 return;
2408} 1949}
2409 1950
2410/* 1951/*
2411 * This function is called in order to complete a regular SBP-2 command. 1952 * Complete a regular SCSI command. Can be called in atomic context.
2412 *
2413 * This can be called in interrupt context.
2414 */ 1953 */
2415static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id, 1954static void sbp2scsi_complete_command(struct sbp2_lu *lu, u32 scsi_status,
2416 u32 scsi_status, struct scsi_cmnd *SCpnt, 1955 struct scsi_cmnd *SCpnt,
2417 void (*done)(struct scsi_cmnd *)) 1956 void (*done)(struct scsi_cmnd *))
2418{ 1957{
2419 SBP2_DEBUG_ENTER();
2420
2421 /*
2422 * Sanity
2423 */
2424 if (!SCpnt) { 1958 if (!SCpnt) {
2425 SBP2_ERR("SCpnt is NULL"); 1959 SBP2_ERR("SCpnt is NULL");
2426 return; 1960 return;
2427 } 1961 }
2428 1962
2429 /*
2430 * If a bus reset is in progress and there was an error, don't
2431 * complete the command, just let it get retried at the end of the
2432 * bus reset.
2433 */
2434 if (!hpsb_node_entry_valid(scsi_id->ne)
2435 && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
2436 SBP2_ERR("Bus reset in progress - retry command later");
2437 return;
2438 }
2439
2440 /*
2441 * Switch on scsi status
2442 */
2443 switch (scsi_status) { 1963 switch (scsi_status) {
2444 case SBP2_SCSI_STATUS_GOOD: 1964 case SBP2_SCSI_STATUS_GOOD:
2445 SCpnt->result = DID_OK << 16; 1965 SCpnt->result = DID_OK << 16;
@@ -2451,12 +1971,7 @@ static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
2451 break; 1971 break;
2452 1972
2453 case SBP2_SCSI_STATUS_CHECK_CONDITION: 1973 case SBP2_SCSI_STATUS_CHECK_CONDITION:
2454 SBP2_DEBUG("SBP2_SCSI_STATUS_CHECK_CONDITION");
2455 SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16; 1974 SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
2456#if CONFIG_IEEE1394_SBP2_DEBUG >= 1
2457 scsi_print_command(SCpnt);
2458 scsi_print_sense(SBP2_DEVICE_NAME, SCpnt);
2459#endif
2460 break; 1975 break;
2461 1976
2462 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT: 1977 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
@@ -2478,118 +1993,88 @@ static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
2478 SCpnt->result = DID_ERROR << 16; 1993 SCpnt->result = DID_ERROR << 16;
2479 } 1994 }
2480 1995
2481 /* 1996 /* If a bus reset is in progress and there was an error, complete
2482 * If a bus reset is in progress and there was an error, complete 1997 * the command as busy so that it will get retried. */
2483 * the command as busy so that it will get retried. 1998 if (!hpsb_node_entry_valid(lu->ne)
2484 */
2485 if (!hpsb_node_entry_valid(scsi_id->ne)
2486 && (scsi_status != SBP2_SCSI_STATUS_GOOD)) { 1999 && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
2487 SBP2_ERR("Completing command with busy (bus reset)"); 2000 SBP2_ERR("Completing command with busy (bus reset)");
2488 SCpnt->result = DID_BUS_BUSY << 16; 2001 SCpnt->result = DID_BUS_BUSY << 16;
2489 } 2002 }
2490 2003
2491 /* 2004 /* Tell the SCSI stack that we're done with this command. */
2492 * If a unit attention occurs, return busy status so it gets
2493 * retried... it could have happened because of a 1394 bus reset
2494 * or hot-plug...
2495 * XXX DID_BUS_BUSY is actually a bad idea because it will defy
2496 * the scsi layer's retry logic.
2497 */
2498#if 0
2499 if ((scsi_status == SBP2_SCSI_STATUS_CHECK_CONDITION) &&
2500 (SCpnt->sense_buffer[2] == UNIT_ATTENTION)) {
2501 SBP2_DEBUG("UNIT ATTENTION - return busy");
2502 SCpnt->result = DID_BUS_BUSY << 16;
2503 }
2504#endif
2505
2506 /*
2507 * Tell scsi stack that we're done with this command
2508 */
2509 done(SCpnt); 2005 done(SCpnt);
2510} 2006}
2511 2007
2512static int sbp2scsi_slave_alloc(struct scsi_device *sdev) 2008static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
2513{ 2009{
2514 struct scsi_id_instance_data *scsi_id = 2010 struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
2515 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2516 2011
2517 scsi_id->sdev = sdev; 2012 lu->sdev = sdev;
2518 sdev->allow_restart = 1; 2013 sdev->allow_restart = 1;
2519 2014
2520 if (scsi_id->workarounds & SBP2_WORKAROUND_INQUIRY_36) 2015 if (lu->workarounds & SBP2_WORKAROUND_INQUIRY_36)
2521 sdev->inquiry_len = 36; 2016 sdev->inquiry_len = 36;
2522 return 0; 2017 return 0;
2523} 2018}
2524 2019
2525static int sbp2scsi_slave_configure(struct scsi_device *sdev) 2020static int sbp2scsi_slave_configure(struct scsi_device *sdev)
2526{ 2021{
2527 struct scsi_id_instance_data *scsi_id = 2022 struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
2528 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2529 2023
2530 blk_queue_dma_alignment(sdev->request_queue, (512 - 1)); 2024 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
2531 sdev->use_10_for_rw = 1; 2025 sdev->use_10_for_rw = 1;
2532 2026
2533 if (sdev->type == TYPE_DISK && 2027 if (sdev->type == TYPE_DISK &&
2534 scsi_id->workarounds & SBP2_WORKAROUND_MODE_SENSE_8) 2028 lu->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
2535 sdev->skip_ms_page_8 = 1; 2029 sdev->skip_ms_page_8 = 1;
2536 if (scsi_id->workarounds & SBP2_WORKAROUND_FIX_CAPACITY) 2030 if (lu->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
2537 sdev->fix_capacity = 1; 2031 sdev->fix_capacity = 1;
2538 return 0; 2032 return 0;
2539} 2033}
2540 2034
2541static void sbp2scsi_slave_destroy(struct scsi_device *sdev) 2035static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
2542{ 2036{
2543 ((struct scsi_id_instance_data *)sdev->host->hostdata[0])->sdev = NULL; 2037 ((struct sbp2_lu *)sdev->host->hostdata[0])->sdev = NULL;
2544 return; 2038 return;
2545} 2039}
2546 2040
2547/* 2041/*
2548 * Called by scsi stack when something has really gone wrong. Usually 2042 * Called by scsi stack when something has really gone wrong.
2549 * called when a command has timed-out for some reason. 2043 * Usually called when a command has timed-out for some reason.
2550 */ 2044 */
2551static int sbp2scsi_abort(struct scsi_cmnd *SCpnt) 2045static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
2552{ 2046{
2553 struct scsi_id_instance_data *scsi_id = 2047 struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
2554 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0]; 2048 struct sbp2_fwhost_info *hi = lu->hi;
2555 struct sbp2scsi_host_info *hi = scsi_id->hi; 2049 struct sbp2_command_info *cmd;
2556 struct sbp2_command_info *command;
2557 unsigned long flags; 2050 unsigned long flags;
2558 2051
2559 SBP2_ERR("aborting sbp2 command"); 2052 SBP2_INFO("aborting sbp2 command");
2560 scsi_print_command(SCpnt); 2053 scsi_print_command(SCpnt);
2561 2054
2562 if (sbp2util_node_is_available(scsi_id)) { 2055 if (sbp2util_node_is_available(lu)) {
2563 2056 sbp2_agent_reset(lu, 1);
2564 /* 2057
2565 * Right now, just return any matching command structures 2058 /* Return a matching command structure to the free pool. */
2566 * to the free pool. 2059 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
2567 */ 2060 cmd = sbp2util_find_command_for_SCpnt(lu, SCpnt);
2568 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags); 2061 if (cmd) {
2569 command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt); 2062 dma_sync_single_for_cpu(&hi->host->device,
2570 if (command) { 2063 cmd->command_orb_dma,
2571 SBP2_DEBUG("Found command to abort"); 2064 sizeof(struct sbp2_command_orb),
2572 pci_dma_sync_single_for_cpu(hi->host->pdev, 2065 DMA_TO_DEVICE);
2573 command->command_orb_dma, 2066 dma_sync_single_for_cpu(&hi->host->device, cmd->sge_dma,
2574 sizeof(struct sbp2_command_orb), 2067 sizeof(cmd->scatter_gather_element),
2575 PCI_DMA_TODEVICE); 2068 DMA_BIDIRECTIONAL);
2576 pci_dma_sync_single_for_cpu(hi->host->pdev, 2069 sbp2util_mark_command_completed(lu, cmd);
2577 command->sge_dma, 2070 if (cmd->Current_SCpnt) {
2578 sizeof(command->scatter_gather_element), 2071 cmd->Current_SCpnt->result = DID_ABORT << 16;
2579 PCI_DMA_BIDIRECTIONAL); 2072 cmd->Current_done(cmd->Current_SCpnt);
2580 sbp2util_mark_command_completed(scsi_id, command);
2581 if (command->Current_SCpnt) {
2582 command->Current_SCpnt->result = DID_ABORT << 16;
2583 command->Current_done(command->Current_SCpnt);
2584 } 2073 }
2585 } 2074 }
2586 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags); 2075 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
2587 2076
2588 /* 2077 sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
2589 * Initiate a fetch agent reset.
2590 */
2591 sbp2_agent_reset(scsi_id, 1);
2592 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
2593 } 2078 }
2594 2079
2595 return SUCCESS; 2080 return SUCCESS;
@@ -2600,14 +2085,13 @@ static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
2600 */ 2085 */
2601static int sbp2scsi_reset(struct scsi_cmnd *SCpnt) 2086static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
2602{ 2087{
2603 struct scsi_id_instance_data *scsi_id = 2088 struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
2604 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2605 2089
2606 SBP2_ERR("reset requested"); 2090 SBP2_INFO("reset requested");
2607 2091
2608 if (sbp2util_node_is_available(scsi_id)) { 2092 if (sbp2util_node_is_available(lu)) {
2609 SBP2_ERR("Generating sbp2 fetch agent reset"); 2093 SBP2_INFO("generating sbp2 fetch agent reset");
2610 sbp2_agent_reset(scsi_id, 1); 2094 sbp2_agent_reset(lu, 1);
2611 } 2095 }
2612 2096
2613 return SUCCESS; 2097 return SUCCESS;
@@ -2618,90 +2102,50 @@ static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
2618 char *buf) 2102 char *buf)
2619{ 2103{
2620 struct scsi_device *sdev; 2104 struct scsi_device *sdev;
2621 struct scsi_id_instance_data *scsi_id; 2105 struct sbp2_lu *lu;
2622 int lun;
2623 2106
2624 if (!(sdev = to_scsi_device(dev))) 2107 if (!(sdev = to_scsi_device(dev)))
2625 return 0; 2108 return 0;
2626 2109
2627 if (!(scsi_id = (struct scsi_id_instance_data *)sdev->host->hostdata[0])) 2110 if (!(lu = (struct sbp2_lu *)sdev->host->hostdata[0]))
2628 return 0; 2111 return 0;
2629 2112
2630 lun = ORB_SET_LUN(scsi_id->sbp2_lun); 2113 return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)lu->ne->guid,
2631 2114 lu->ud->id, ORB_SET_LUN(lu->lun));
2632 return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)scsi_id->ne->guid,
2633 scsi_id->ud->id, lun);
2634} 2115}
2635static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
2636
2637static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
2638 &dev_attr_ieee1394_id,
2639 NULL
2640};
2641 2116
2642MODULE_AUTHOR("Ben Collins <bcollins@debian.org>"); 2117MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2643MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver"); 2118MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2644MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME); 2119MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2645MODULE_LICENSE("GPL"); 2120MODULE_LICENSE("GPL");
2646 2121
2647/* SCSI host template */
2648static struct scsi_host_template scsi_driver_template = {
2649 .module = THIS_MODULE,
2650 .name = "SBP-2 IEEE-1394",
2651 .proc_name = SBP2_DEVICE_NAME,
2652 .queuecommand = sbp2scsi_queuecommand,
2653 .eh_abort_handler = sbp2scsi_abort,
2654 .eh_device_reset_handler = sbp2scsi_reset,
2655 .slave_alloc = sbp2scsi_slave_alloc,
2656 .slave_configure = sbp2scsi_slave_configure,
2657 .slave_destroy = sbp2scsi_slave_destroy,
2658 .this_id = -1,
2659 .sg_tablesize = SG_ALL,
2660 .use_clustering = ENABLE_CLUSTERING,
2661 .cmd_per_lun = SBP2_MAX_CMDS,
2662 .can_queue = SBP2_MAX_CMDS,
2663 .emulated = 1,
2664 .sdev_attrs = sbp2_sysfs_sdev_attrs,
2665};
2666
2667static int sbp2_module_init(void) 2122static int sbp2_module_init(void)
2668{ 2123{
2669 int ret; 2124 int ret;
2670 2125
2671 SBP2_DEBUG_ENTER(); 2126 if (sbp2_serialize_io) {
2672 2127 sbp2_shost_template.can_queue = 1;
2673 /* Module load debug option to force one command at a time (serializing I/O) */ 2128 sbp2_shost_template.cmd_per_lun = 1;
2674 if (serialize_io) {
2675 SBP2_INFO("Driver forced to serialize I/O (serialize_io=1)");
2676 SBP2_INFO("Try serialize_io=0 for better performance");
2677 scsi_driver_template.can_queue = 1;
2678 scsi_driver_template.cmd_per_lun = 1;
2679 } 2129 }
2680 2130
2681 if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS && 2131 if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
2682 (max_sectors * 512) > (128 * 1024)) 2132 (sbp2_max_sectors * 512) > (128 * 1024))
2683 max_sectors = 128 * 1024 / 512; 2133 sbp2_max_sectors = 128 * 1024 / 512;
2684 scsi_driver_template.max_sectors = max_sectors; 2134 sbp2_shost_template.max_sectors = sbp2_max_sectors;
2685 2135
2686 /* Register our high level driver with 1394 stack */
2687 hpsb_register_highlevel(&sbp2_highlevel); 2136 hpsb_register_highlevel(&sbp2_highlevel);
2688
2689 ret = hpsb_register_protocol(&sbp2_driver); 2137 ret = hpsb_register_protocol(&sbp2_driver);
2690 if (ret) { 2138 if (ret) {
2691 SBP2_ERR("Failed to register protocol"); 2139 SBP2_ERR("Failed to register protocol");
2692 hpsb_unregister_highlevel(&sbp2_highlevel); 2140 hpsb_unregister_highlevel(&sbp2_highlevel);
2693 return ret; 2141 return ret;
2694 } 2142 }
2695
2696 return 0; 2143 return 0;
2697} 2144}
2698 2145
2699static void __exit sbp2_module_exit(void) 2146static void __exit sbp2_module_exit(void)
2700{ 2147{
2701 SBP2_DEBUG_ENTER();
2702
2703 hpsb_unregister_protocol(&sbp2_driver); 2148 hpsb_unregister_protocol(&sbp2_driver);
2704
2705 hpsb_unregister_highlevel(&sbp2_highlevel); 2149 hpsb_unregister_highlevel(&sbp2_highlevel);
2706} 2150}
2707 2151