Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / ieee1394 / sbp2.c
blobe70f4908f0e699e2994dcef436728e2198b8887d
1 /*
2 * sbp2.c - SBP-2 protocol driver for IEEE-1394
4 * Copyright (C) 2000 James Goodwin, Filanet Corporation (www.filanet.com)
5 * jamesg@filanet.com (JSG)
7 * Copyright (C) 2003 Ben Collins <bcollins@debian.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Brief Description:
27 * This driver implements the Serial Bus Protocol 2 (SBP-2) over IEEE-1394
28 * under Linux. The SBP-2 driver is implemented as an IEEE-1394 high-level
29 * driver. It also registers as a SCSI lower-level driver in order to accept
30 * SCSI commands for transport using SBP-2.
32 * You may access any attached SBP-2 (usually storage devices) as regular
33 * SCSI devices. E.g. mount /dev/sda1, fdisk, mkfs, etc..
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.
38 * TODO:
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.
54 #include <linux/blkdev.h>
55 #include <linux/compiler.h>
56 #include <linux/delay.h>
57 #include <linux/device.h>
58 #include <linux/dma-mapping.h>
59 #include <linux/gfp.h>
60 #include <linux/init.h>
61 #include <linux/kernel.h>
62 #include <linux/list.h>
63 #include <linux/mm.h>
64 #include <linux/module.h>
65 #include <linux/moduleparam.h>
66 #include <linux/sched.h>
67 #include <linux/slab.h>
68 #include <linux/spinlock.h>
69 #include <linux/stat.h>
70 #include <linux/string.h>
71 #include <linux/stringify.h>
72 #include <linux/types.h>
73 #include <linux/wait.h>
74 #include <linux/workqueue.h>
75 #include <linux/scatterlist.h>
77 #include <asm/byteorder.h>
78 #include <asm/errno.h>
79 #include <asm/param.h>
80 #include <asm/system.h>
81 #include <asm/types.h>
83 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
84 #include <asm/io.h> /* for bus_to_virt */
85 #endif
87 #include <scsi/scsi.h>
88 #include <scsi/scsi_cmnd.h>
89 #include <scsi/scsi_dbg.h>
90 #include <scsi/scsi_device.h>
91 #include <scsi/scsi_host.h>
93 #include "csr1212.h"
94 #include "highlevel.h"
95 #include "hosts.h"
96 #include "ieee1394.h"
97 #include "ieee1394_core.h"
98 #include "ieee1394_hotplug.h"
99 #include "ieee1394_transactions.h"
100 #include "ieee1394_types.h"
101 #include "nodemgr.h"
102 #include "sbp2.h"
105 * Module load parameter definitions
109 * Change max_speed on module load if you have a bad IEEE-1394
110 * controller that has trouble running 2KB packets at 400mb.
112 * NOTE: On certain OHCI parts I have seen short packets on async transmit
113 * (probably due to PCI latency/throughput issues with the part). You can
114 * bump down the speed if you are running into problems.
116 static int sbp2_max_speed = IEEE1394_SPEED_MAX;
117 module_param_named(max_speed, sbp2_max_speed, int, 0644);
118 MODULE_PARM_DESC(max_speed, "Force max speed "
119 "(3 = 800Mb/s, 2 = 400Mb/s, 1 = 200Mb/s, 0 = 100Mb/s)");
122 * Set serialize_io to 0 or N to use dynamically appended lists of command ORBs.
123 * This is and always has been buggy in multiple subtle ways. See above TODOs.
125 static int sbp2_serialize_io = 1;
126 module_param_named(serialize_io, sbp2_serialize_io, bool, 0444);
127 MODULE_PARM_DESC(serialize_io, "Serialize requests coming from SCSI drivers "
128 "(default = Y, faster but buggy = N)");
131 * Adjust max_sectors if you'd like to influence how many sectors each SCSI
132 * command can transfer at most. Please note that some older SBP-2 bridge
133 * chips are broken for transfers greater or equal to 128KB, therefore
134 * max_sectors used to be a safe 255 sectors for many years. We now have a
135 * default of 0 here which means that we let the SCSI stack choose a limit.
137 * The SBP2_WORKAROUND_128K_MAX_TRANS flag, if set either in the workarounds
138 * module parameter or in the sbp2_workarounds_table[], will override the
139 * value of max_sectors. We should use sbp2_workarounds_table[] to cover any
140 * bridge chip which becomes known to need the 255 sectors limit.
142 static int sbp2_max_sectors;
143 module_param_named(max_sectors, sbp2_max_sectors, int, 0444);
144 MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported "
145 "(default = 0 = use SCSI stack's default)");
148 * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
149 * do an exclusive login, as it's generally unsafe to have two hosts
150 * talking to a single sbp2 device at the same time (filesystem coherency,
151 * etc.). If you're running an sbp2 device that supports multiple logins,
152 * and you're either running read-only filesystems or some sort of special
153 * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster
154 * File System, or Lustre, then set exclusive_login to zero.
156 * So far only bridges from Oxford Semiconductor are known to support
157 * concurrent logins. Depending on firmware, four or two concurrent logins
158 * are possible on OXFW911 and newer Oxsemi bridges.
160 static int sbp2_exclusive_login = 1;
161 module_param_named(exclusive_login, sbp2_exclusive_login, bool, 0644);
162 MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
163 "(default = Y, use N for concurrent initiators)");
166 * If any of the following workarounds is required for your device to work,
167 * please submit the kernel messages logged by sbp2 to the linux1394-devel
168 * mailing list.
170 * - 128kB max transfer
171 * Limit transfer size. Necessary for some old bridges.
173 * - 36 byte inquiry
174 * When scsi_mod probes the device, let the inquiry command look like that
175 * from MS Windows.
177 * - skip mode page 8
178 * Suppress sending of mode_sense for mode page 8 if the device pretends to
179 * support the SCSI Primary Block commands instead of Reduced Block Commands.
181 * - fix capacity
182 * Tell sd_mod to correct the last sector number reported by read_capacity.
183 * Avoids access beyond actual disk limits on devices with an off-by-one bug.
184 * Don't use this with devices which don't have this bug.
186 <<<<<<< HEAD:drivers/ieee1394/sbp2.c
187 =======
188 * - delay inquiry
189 * Wait extra SBP2_INQUIRY_DELAY seconds after login before SCSI inquiry.
191 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ieee1394/sbp2.c
192 * - override internal blacklist
193 * Instead of adding to the built-in blacklist, use only the workarounds
194 * specified in the module load parameter.
195 * Useful if a blacklist entry interfered with a non-broken device.
197 static int sbp2_default_workarounds;
198 module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
199 MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
200 ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
201 ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
202 ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
203 ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
204 <<<<<<< HEAD:drivers/ieee1394/sbp2.c
205 =======
206 ", delay inquiry = " __stringify(SBP2_WORKAROUND_DELAY_INQUIRY)
207 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ieee1394/sbp2.c
208 ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
209 ", or a combination)");
212 * This influences the format of the sysfs attribute
213 * /sys/bus/scsi/devices/.../ieee1394_id.
215 * The default format is like in older kernels: %016Lx:%d:%d
216 * It contains the target's EUI-64, a number given to the logical unit by
217 * the ieee1394 driver's nodemgr (starting at 0), and the LUN.
219 * The long format is: %016Lx:%06x:%04x
220 * It contains the target's EUI-64, the unit directory's directory_ID as per
221 * IEEE 1212 clause 7.7.19, and the LUN. This format comes closest to the
222 * format of SBP(-3) target port and logical unit identifier as per SAM (SCSI
223 * Architecture Model) rev.2 to 4 annex A. Therefore and because it is
224 * independent of the implementation of the ieee1394 nodemgr, the longer format
225 * is recommended for future use.
227 static int sbp2_long_sysfs_ieee1394_id;
228 module_param_named(long_ieee1394_id, sbp2_long_sysfs_ieee1394_id, bool, 0644);
229 MODULE_PARM_DESC(long_ieee1394_id, "8+3+2 bytes format of ieee1394_id in sysfs "
230 "(default = backwards-compatible = N, SAM-conforming = Y)");
233 #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
234 #define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
237 * Globals
239 static void sbp2scsi_complete_all_commands(struct sbp2_lu *, u32);
240 static void sbp2scsi_complete_command(struct sbp2_lu *, u32, struct scsi_cmnd *,
241 void (*)(struct scsi_cmnd *));
242 static struct sbp2_lu *sbp2_alloc_device(struct unit_directory *);
243 static int sbp2_start_device(struct sbp2_lu *);
244 static void sbp2_remove_device(struct sbp2_lu *);
245 static int sbp2_login_device(struct sbp2_lu *);
246 static int sbp2_reconnect_device(struct sbp2_lu *);
247 static int sbp2_logout_device(struct sbp2_lu *);
248 static void sbp2_host_reset(struct hpsb_host *);
249 static int sbp2_handle_status_write(struct hpsb_host *, int, int, quadlet_t *,
250 u64, size_t, u16);
251 static int sbp2_agent_reset(struct sbp2_lu *, int);
252 static void sbp2_parse_unit_directory(struct sbp2_lu *,
253 struct unit_directory *);
254 static int sbp2_set_busy_timeout(struct sbp2_lu *);
255 static int sbp2_max_speed_and_size(struct sbp2_lu *);
258 static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
260 static DEFINE_RWLOCK(sbp2_hi_logical_units_lock);
262 static struct hpsb_highlevel sbp2_highlevel = {
263 .name = SBP2_DEVICE_NAME,
264 .host_reset = sbp2_host_reset,
267 static struct hpsb_address_ops sbp2_ops = {
268 .write = sbp2_handle_status_write
271 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
272 static int sbp2_handle_physdma_write(struct hpsb_host *, int, int, quadlet_t *,
273 u64, size_t, u16);
274 static int sbp2_handle_physdma_read(struct hpsb_host *, int, quadlet_t *, u64,
275 size_t, u16);
277 static struct hpsb_address_ops sbp2_physdma_ops = {
278 .read = sbp2_handle_physdma_read,
279 .write = sbp2_handle_physdma_write,
281 #endif
285 * Interface to driver core and IEEE 1394 core
287 static struct ieee1394_device_id sbp2_id_table[] = {
289 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
290 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
291 .version = SBP2_SW_VERSION_ENTRY & 0xffffff},
294 MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
296 static int sbp2_probe(struct device *);
297 static int sbp2_remove(struct device *);
298 static int sbp2_update(struct unit_directory *);
300 static struct hpsb_protocol_driver sbp2_driver = {
301 .name = SBP2_DEVICE_NAME,
302 .id_table = sbp2_id_table,
303 .update = sbp2_update,
304 .driver = {
305 .probe = sbp2_probe,
306 .remove = sbp2_remove,
312 * Interface to SCSI core
314 static int sbp2scsi_queuecommand(struct scsi_cmnd *,
315 void (*)(struct scsi_cmnd *));
316 static int sbp2scsi_abort(struct scsi_cmnd *);
317 static int sbp2scsi_reset(struct scsi_cmnd *);
318 static int sbp2scsi_slave_alloc(struct scsi_device *);
319 static int sbp2scsi_slave_configure(struct scsi_device *);
320 static void sbp2scsi_slave_destroy(struct scsi_device *);
321 static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *,
322 struct device_attribute *, char *);
324 static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
326 static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
327 &dev_attr_ieee1394_id,
328 NULL
331 static struct scsi_host_template sbp2_shost_template = {
332 .module = THIS_MODULE,
333 .name = "SBP-2 IEEE-1394",
334 .proc_name = SBP2_DEVICE_NAME,
335 .queuecommand = sbp2scsi_queuecommand,
336 .eh_abort_handler = sbp2scsi_abort,
337 .eh_device_reset_handler = sbp2scsi_reset,
338 .slave_alloc = sbp2scsi_slave_alloc,
339 .slave_configure = sbp2scsi_slave_configure,
340 .slave_destroy = sbp2scsi_slave_destroy,
341 .this_id = -1,
342 .sg_tablesize = SG_ALL,
343 .use_clustering = ENABLE_CLUSTERING,
344 .cmd_per_lun = SBP2_MAX_CMDS,
345 .can_queue = SBP2_MAX_CMDS,
346 .sdev_attrs = sbp2_sysfs_sdev_attrs,
349 /* for match-all entries in sbp2_workarounds_table */
350 #define SBP2_ROM_VALUE_WILDCARD 0x1000000
353 * List of devices with known bugs.
355 * The firmware_revision field, masked with 0xffff00, is the best indicator
356 * for the type of bridge chip of a device. It yields a few false positives
357 * but this did not break correctly behaving devices so far.
359 static const struct {
360 u32 firmware_revision;
361 u32 model_id;
362 unsigned workarounds;
363 } sbp2_workarounds_table[] = {
364 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
365 .firmware_revision = 0x002800,
366 .model_id = 0x001010,
367 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
368 SBP2_WORKAROUND_MODE_SENSE_8,
370 <<<<<<< HEAD:drivers/ieee1394/sbp2.c
371 =======
372 /* DViCO Momobay FX-3A with TSB42AA9A bridge */ {
373 .firmware_revision = 0x002800,
374 .model_id = 0x000000,
375 .workarounds = SBP2_WORKAROUND_DELAY_INQUIRY,
377 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ieee1394/sbp2.c
378 /* Initio bridges, actually only needed for some older ones */ {
379 .firmware_revision = 0x000200,
380 .model_id = SBP2_ROM_VALUE_WILDCARD,
381 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
383 /* Symbios bridge */ {
384 .firmware_revision = 0xa0b800,
385 .model_id = SBP2_ROM_VALUE_WILDCARD,
386 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
388 <<<<<<< HEAD:drivers/ieee1394/sbp2.c
389 =======
390 /* Datafab MD2-FW2 with Symbios/LSILogic SYM13FW500 bridge */ {
391 .firmware_revision = 0x002600,
392 .model_id = SBP2_ROM_VALUE_WILDCARD,
393 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
395 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ieee1394/sbp2.c
396 /* iPod 4th generation */ {
397 .firmware_revision = 0x0a2700,
398 .model_id = 0x000021,
399 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
401 /* iPod mini */ {
402 .firmware_revision = 0x0a2700,
403 .model_id = 0x000023,
404 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
406 /* iPod Photo */ {
407 .firmware_revision = 0x0a2700,
408 .model_id = 0x00007e,
409 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
413 /**************************************
414 * General utility functions
415 **************************************/
417 #ifndef __BIG_ENDIAN
419 * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
421 static inline void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
423 u32 *temp = buffer;
425 for (length = (length >> 2); length--; )
426 temp[length] = be32_to_cpu(temp[length]);
430 * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
432 static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
434 u32 *temp = buffer;
436 for (length = (length >> 2); length--; )
437 temp[length] = cpu_to_be32(temp[length]);
439 #else /* BIG_ENDIAN */
440 /* Why waste the cpu cycles? */
441 #define sbp2util_be32_to_cpu_buffer(x,y) do {} while (0)
442 #define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0)
443 #endif
445 static DECLARE_WAIT_QUEUE_HEAD(sbp2_access_wq);
448 * Waits for completion of an SBP-2 access request.
449 * Returns nonzero if timed out or prematurely interrupted.
451 static int sbp2util_access_timeout(struct sbp2_lu *lu, int timeout)
453 long leftover;
455 leftover = wait_event_interruptible_timeout(
456 sbp2_access_wq, lu->access_complete, timeout);
457 lu->access_complete = 0;
458 return leftover <= 0;
461 static void sbp2_free_packet(void *packet)
463 hpsb_free_tlabel(packet);
464 hpsb_free_packet(packet);
468 * This is much like hpsb_node_write(), except it ignores the response
469 * subaction and returns immediately. Can be used from atomic context.
471 static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
472 quadlet_t *buf, size_t len)
474 struct hpsb_packet *packet;
476 packet = hpsb_make_writepacket(ne->host, ne->nodeid, addr, buf, len);
477 if (!packet)
478 return -ENOMEM;
480 hpsb_set_packet_complete_task(packet, sbp2_free_packet, packet);
481 hpsb_node_fill_packet(ne, packet);
482 if (hpsb_send_packet(packet) < 0) {
483 sbp2_free_packet(packet);
484 return -EIO;
486 return 0;
489 static void sbp2util_notify_fetch_agent(struct sbp2_lu *lu, u64 offset,
490 quadlet_t *data, size_t len)
492 /* There is a small window after a bus reset within which the node
493 * entry's generation is current but the reconnect wasn't completed. */
494 if (unlikely(atomic_read(&lu->state) == SBP2LU_STATE_IN_RESET))
495 return;
497 if (hpsb_node_write(lu->ne, lu->command_block_agent_addr + offset,
498 data, len))
499 SBP2_ERR("sbp2util_notify_fetch_agent failed.");
501 /* Now accept new SCSI commands, unless a bus reset happended during
502 * hpsb_node_write. */
503 if (likely(atomic_read(&lu->state) != SBP2LU_STATE_IN_RESET))
504 scsi_unblock_requests(lu->shost);
507 static void sbp2util_write_orb_pointer(struct work_struct *work)
509 struct sbp2_lu *lu = container_of(work, struct sbp2_lu, protocol_work);
510 quadlet_t data[2];
512 data[0] = ORB_SET_NODE_ID(lu->hi->host->node_id);
513 data[1] = lu->last_orb_dma;
514 sbp2util_cpu_to_be32_buffer(data, 8);
515 sbp2util_notify_fetch_agent(lu, SBP2_ORB_POINTER_OFFSET, data, 8);
518 static void sbp2util_write_doorbell(struct work_struct *work)
520 struct sbp2_lu *lu = container_of(work, struct sbp2_lu, protocol_work);
522 sbp2util_notify_fetch_agent(lu, SBP2_DOORBELL_OFFSET, NULL, 4);
525 static int sbp2util_create_command_orb_pool(struct sbp2_lu *lu)
527 struct sbp2_fwhost_info *hi = lu->hi;
528 struct sbp2_command_info *cmd;
529 int i, orbs = sbp2_serialize_io ? 2 : SBP2_MAX_CMDS;
531 for (i = 0; i < orbs; i++) {
532 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
533 if (!cmd)
534 return -ENOMEM;
535 cmd->command_orb_dma = dma_map_single(hi->host->device.parent,
536 &cmd->command_orb,
537 sizeof(struct sbp2_command_orb),
538 DMA_TO_DEVICE);
539 cmd->sge_dma = dma_map_single(hi->host->device.parent,
540 &cmd->scatter_gather_element,
541 sizeof(cmd->scatter_gather_element),
542 DMA_TO_DEVICE);
543 INIT_LIST_HEAD(&cmd->list);
544 list_add_tail(&cmd->list, &lu->cmd_orb_completed);
546 return 0;
549 static void sbp2util_remove_command_orb_pool(struct sbp2_lu *lu,
550 struct hpsb_host *host)
552 struct list_head *lh, *next;
553 struct sbp2_command_info *cmd;
554 unsigned long flags;
556 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
557 if (!list_empty(&lu->cmd_orb_completed))
558 list_for_each_safe(lh, next, &lu->cmd_orb_completed) {
559 cmd = list_entry(lh, struct sbp2_command_info, list);
560 dma_unmap_single(host->device.parent,
561 cmd->command_orb_dma,
562 sizeof(struct sbp2_command_orb),
563 DMA_TO_DEVICE);
564 dma_unmap_single(host->device.parent, cmd->sge_dma,
565 sizeof(cmd->scatter_gather_element),
566 DMA_TO_DEVICE);
567 kfree(cmd);
569 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
570 return;
574 * Finds the sbp2_command for a given outstanding command ORB.
575 * Only looks at the in-use list.
577 static struct sbp2_command_info *sbp2util_find_command_for_orb(
578 struct sbp2_lu *lu, dma_addr_t orb)
580 struct sbp2_command_info *cmd;
581 unsigned long flags;
583 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
584 if (!list_empty(&lu->cmd_orb_inuse))
585 list_for_each_entry(cmd, &lu->cmd_orb_inuse, list)
586 if (cmd->command_orb_dma == orb) {
587 spin_unlock_irqrestore(
588 &lu->cmd_orb_lock, flags);
589 return cmd;
591 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
592 return NULL;
596 * Finds the sbp2_command for a given outstanding SCpnt.
597 * Only looks at the in-use list.
598 * Must be called with lu->cmd_orb_lock held.
600 static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
601 struct sbp2_lu *lu, void *SCpnt)
603 struct sbp2_command_info *cmd;
605 if (!list_empty(&lu->cmd_orb_inuse))
606 list_for_each_entry(cmd, &lu->cmd_orb_inuse, list)
607 if (cmd->Current_SCpnt == SCpnt)
608 return cmd;
609 return NULL;
612 static struct sbp2_command_info *sbp2util_allocate_command_orb(
613 struct sbp2_lu *lu,
614 struct scsi_cmnd *Current_SCpnt,
615 void (*Current_done)(struct scsi_cmnd *))
617 struct list_head *lh;
618 struct sbp2_command_info *cmd = NULL;
619 unsigned long flags;
621 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
622 if (!list_empty(&lu->cmd_orb_completed)) {
623 lh = lu->cmd_orb_completed.next;
624 list_del(lh);
625 cmd = list_entry(lh, struct sbp2_command_info, list);
626 cmd->Current_done = Current_done;
627 cmd->Current_SCpnt = Current_SCpnt;
628 list_add_tail(&cmd->list, &lu->cmd_orb_inuse);
629 } else
630 SBP2_ERR("%s: no orbs available", __FUNCTION__);
631 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
632 return cmd;
636 * Unmaps the DMAs of a command and moves the command to the completed ORB list.
637 * Must be called with lu->cmd_orb_lock held.
639 static void sbp2util_mark_command_completed(struct sbp2_lu *lu,
640 struct sbp2_command_info *cmd)
642 struct hpsb_host *host = lu->ud->ne->host;
644 if (cmd->cmd_dma) {
645 if (cmd->dma_type == CMD_DMA_SINGLE)
646 dma_unmap_single(host->device.parent, cmd->cmd_dma,
647 cmd->dma_size, cmd->dma_dir);
648 else if (cmd->dma_type == CMD_DMA_PAGE)
649 dma_unmap_page(host->device.parent, cmd->cmd_dma,
650 cmd->dma_size, cmd->dma_dir);
651 /* XXX: Check for CMD_DMA_NONE bug */
652 cmd->dma_type = CMD_DMA_NONE;
653 cmd->cmd_dma = 0;
655 if (cmd->sge_buffer) {
656 dma_unmap_sg(host->device.parent, cmd->sge_buffer,
657 cmd->dma_size, cmd->dma_dir);
658 cmd->sge_buffer = NULL;
660 list_move_tail(&cmd->list, &lu->cmd_orb_completed);
664 * Is lu valid? Is the 1394 node still present?
666 static inline int sbp2util_node_is_available(struct sbp2_lu *lu)
668 return lu && lu->ne && !lu->ne->in_limbo;
671 /*********************************************
672 * IEEE-1394 core driver stack related section
673 *********************************************/
675 static int sbp2_probe(struct device *dev)
677 struct unit_directory *ud;
678 struct sbp2_lu *lu;
680 ud = container_of(dev, struct unit_directory, device);
682 /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
683 * instead. */
684 if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
685 return -ENODEV;
687 lu = sbp2_alloc_device(ud);
688 if (!lu)
689 return -ENOMEM;
691 sbp2_parse_unit_directory(lu, ud);
692 return sbp2_start_device(lu);
695 static int sbp2_remove(struct device *dev)
697 struct unit_directory *ud;
698 struct sbp2_lu *lu;
699 struct scsi_device *sdev;
701 ud = container_of(dev, struct unit_directory, device);
702 lu = ud->device.driver_data;
703 if (!lu)
704 return 0;
706 if (lu->shost) {
707 /* Get rid of enqueued commands if there is no chance to
708 * send them. */
709 if (!sbp2util_node_is_available(lu))
710 sbp2scsi_complete_all_commands(lu, DID_NO_CONNECT);
711 /* scsi_remove_device() may trigger shutdown functions of SCSI
712 * highlevel drivers which would deadlock if blocked. */
713 atomic_set(&lu->state, SBP2LU_STATE_IN_SHUTDOWN);
714 scsi_unblock_requests(lu->shost);
716 sdev = lu->sdev;
717 if (sdev) {
718 lu->sdev = NULL;
719 scsi_remove_device(sdev);
722 sbp2_logout_device(lu);
723 sbp2_remove_device(lu);
725 return 0;
728 static int sbp2_update(struct unit_directory *ud)
730 struct sbp2_lu *lu = ud->device.driver_data;
732 if (sbp2_reconnect_device(lu)) {
733 /* Reconnect has failed. Perhaps we didn't reconnect fast
734 * enough. Try a regular login, but first log out just in
735 * case of any weirdness. */
736 sbp2_logout_device(lu);
738 if (sbp2_login_device(lu)) {
739 /* Login failed too, just fail, and the backend
740 * will call our sbp2_remove for us */
741 SBP2_ERR("Failed to reconnect to sbp2 device!");
742 return -EBUSY;
746 sbp2_set_busy_timeout(lu);
747 sbp2_agent_reset(lu, 1);
748 sbp2_max_speed_and_size(lu);
750 /* Complete any pending commands with busy (so they get retried)
751 * and remove them from our queue. */
752 sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
754 /* Accept new commands unless there was another bus reset in the
755 * meantime. */
756 if (hpsb_node_entry_valid(lu->ne)) {
757 atomic_set(&lu->state, SBP2LU_STATE_RUNNING);
758 scsi_unblock_requests(lu->shost);
760 return 0;
763 static struct sbp2_lu *sbp2_alloc_device(struct unit_directory *ud)
765 struct sbp2_fwhost_info *hi;
766 struct Scsi_Host *shost = NULL;
767 struct sbp2_lu *lu = NULL;
768 unsigned long flags;
770 lu = kzalloc(sizeof(*lu), GFP_KERNEL);
771 if (!lu) {
772 SBP2_ERR("failed to create lu");
773 goto failed_alloc;
776 lu->ne = ud->ne;
777 lu->ud = ud;
778 lu->speed_code = IEEE1394_SPEED_100;
779 lu->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
780 lu->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
781 INIT_LIST_HEAD(&lu->cmd_orb_inuse);
782 INIT_LIST_HEAD(&lu->cmd_orb_completed);
783 INIT_LIST_HEAD(&lu->lu_list);
784 spin_lock_init(&lu->cmd_orb_lock);
785 atomic_set(&lu->state, SBP2LU_STATE_RUNNING);
786 INIT_WORK(&lu->protocol_work, NULL);
788 ud->device.driver_data = lu;
790 hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
791 if (!hi) {
792 hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host,
793 sizeof(*hi));
794 if (!hi) {
795 SBP2_ERR("failed to allocate hostinfo");
796 goto failed_alloc;
798 hi->host = ud->ne->host;
799 INIT_LIST_HEAD(&hi->logical_units);
801 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
802 /* Handle data movement if physical dma is not
803 * enabled or not supported on host controller */
804 if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
805 &sbp2_physdma_ops,
806 0x0ULL, 0xfffffffcULL)) {
807 SBP2_ERR("failed to register lower 4GB address range");
808 goto failed_alloc;
810 #endif
813 /* Prevent unloading of the 1394 host */
814 if (!try_module_get(hi->host->driver->owner)) {
815 SBP2_ERR("failed to get a reference on 1394 host driver");
816 goto failed_alloc;
819 lu->hi = hi;
821 write_lock_irqsave(&sbp2_hi_logical_units_lock, flags);
822 list_add_tail(&lu->lu_list, &hi->logical_units);
823 write_unlock_irqrestore(&sbp2_hi_logical_units_lock, flags);
825 /* Register the status FIFO address range. We could use the same FIFO
826 * for targets at different nodes. However we need different FIFOs per
827 * target in order to support multi-unit devices.
828 * The FIFO is located out of the local host controller's physical range
829 * but, if possible, within the posted write area. Status writes will
830 * then be performed as unified transactions. This slightly reduces
831 * bandwidth usage, and some Prolific based devices seem to require it.
833 lu->status_fifo_addr = hpsb_allocate_and_register_addrspace(
834 &sbp2_highlevel, ud->ne->host, &sbp2_ops,
835 sizeof(struct sbp2_status_block), sizeof(quadlet_t),
836 ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
837 if (lu->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
838 SBP2_ERR("failed to allocate status FIFO address range");
839 goto failed_alloc;
842 shost = scsi_host_alloc(&sbp2_shost_template, sizeof(unsigned long));
843 if (!shost) {
844 SBP2_ERR("failed to register scsi host");
845 goto failed_alloc;
848 shost->hostdata[0] = (unsigned long)lu;
850 if (!scsi_add_host(shost, &ud->device)) {
851 lu->shost = shost;
852 return lu;
855 SBP2_ERR("failed to add scsi host");
856 scsi_host_put(shost);
858 failed_alloc:
859 sbp2_remove_device(lu);
860 return NULL;
863 static void sbp2_host_reset(struct hpsb_host *host)
865 struct sbp2_fwhost_info *hi;
866 struct sbp2_lu *lu;
867 unsigned long flags;
869 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
870 if (!hi)
871 return;
873 read_lock_irqsave(&sbp2_hi_logical_units_lock, flags);
874 list_for_each_entry(lu, &hi->logical_units, lu_list)
875 if (likely(atomic_read(&lu->state) !=
876 SBP2LU_STATE_IN_SHUTDOWN)) {
877 atomic_set(&lu->state, SBP2LU_STATE_IN_RESET);
878 scsi_block_requests(lu->shost);
880 read_unlock_irqrestore(&sbp2_hi_logical_units_lock, flags);
883 static int sbp2_start_device(struct sbp2_lu *lu)
885 struct sbp2_fwhost_info *hi = lu->hi;
886 int error;
888 lu->login_response = dma_alloc_coherent(hi->host->device.parent,
889 sizeof(struct sbp2_login_response),
890 &lu->login_response_dma, GFP_KERNEL);
891 if (!lu->login_response)
892 goto alloc_fail;
894 lu->query_logins_orb = dma_alloc_coherent(hi->host->device.parent,
895 sizeof(struct sbp2_query_logins_orb),
896 &lu->query_logins_orb_dma, GFP_KERNEL);
897 if (!lu->query_logins_orb)
898 goto alloc_fail;
900 lu->query_logins_response = dma_alloc_coherent(hi->host->device.parent,
901 sizeof(struct sbp2_query_logins_response),
902 &lu->query_logins_response_dma, GFP_KERNEL);
903 if (!lu->query_logins_response)
904 goto alloc_fail;
906 lu->reconnect_orb = dma_alloc_coherent(hi->host->device.parent,
907 sizeof(struct sbp2_reconnect_orb),
908 &lu->reconnect_orb_dma, GFP_KERNEL);
909 if (!lu->reconnect_orb)
910 goto alloc_fail;
912 lu->logout_orb = dma_alloc_coherent(hi->host->device.parent,
913 sizeof(struct sbp2_logout_orb),
914 &lu->logout_orb_dma, GFP_KERNEL);
915 if (!lu->logout_orb)
916 goto alloc_fail;
918 lu->login_orb = dma_alloc_coherent(hi->host->device.parent,
919 sizeof(struct sbp2_login_orb),
920 &lu->login_orb_dma, GFP_KERNEL);
921 if (!lu->login_orb)
922 goto alloc_fail;
924 if (sbp2util_create_command_orb_pool(lu))
925 goto alloc_fail;
927 /* Wait a second before trying to log in. Previously logged in
928 * initiators need a chance to reconnect. */
929 if (msleep_interruptible(1000)) {
930 sbp2_remove_device(lu);
931 return -EINTR;
934 if (sbp2_login_device(lu)) {
935 sbp2_remove_device(lu);
936 return -EBUSY;
939 sbp2_set_busy_timeout(lu);
940 sbp2_agent_reset(lu, 1);
941 sbp2_max_speed_and_size(lu);
943 <<<<<<< HEAD:drivers/ieee1394/sbp2.c
944 =======
945 if (lu->workarounds & SBP2_WORKAROUND_DELAY_INQUIRY)
946 ssleep(SBP2_INQUIRY_DELAY);
948 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ieee1394/sbp2.c
949 error = scsi_add_device(lu->shost, 0, lu->ud->id, 0);
950 if (error) {
951 SBP2_ERR("scsi_add_device failed");
952 sbp2_logout_device(lu);
953 sbp2_remove_device(lu);
954 return error;
957 return 0;
959 alloc_fail:
960 SBP2_ERR("Could not allocate memory for lu");
961 sbp2_remove_device(lu);
962 return -ENOMEM;
965 static void sbp2_remove_device(struct sbp2_lu *lu)
967 struct sbp2_fwhost_info *hi;
968 unsigned long flags;
970 if (!lu)
971 return;
972 hi = lu->hi;
973 if (!hi)
974 goto no_hi;
976 if (lu->shost) {
977 scsi_remove_host(lu->shost);
978 scsi_host_put(lu->shost);
980 flush_scheduled_work();
981 sbp2util_remove_command_orb_pool(lu, hi->host);
983 write_lock_irqsave(&sbp2_hi_logical_units_lock, flags);
984 list_del(&lu->lu_list);
985 write_unlock_irqrestore(&sbp2_hi_logical_units_lock, flags);
987 if (lu->login_response)
988 dma_free_coherent(hi->host->device.parent,
989 sizeof(struct sbp2_login_response),
990 lu->login_response,
991 lu->login_response_dma);
992 if (lu->login_orb)
993 dma_free_coherent(hi->host->device.parent,
994 sizeof(struct sbp2_login_orb),
995 lu->login_orb,
996 lu->login_orb_dma);
997 if (lu->reconnect_orb)
998 dma_free_coherent(hi->host->device.parent,
999 sizeof(struct sbp2_reconnect_orb),
1000 lu->reconnect_orb,
1001 lu->reconnect_orb_dma);
1002 if (lu->logout_orb)
1003 dma_free_coherent(hi->host->device.parent,
1004 sizeof(struct sbp2_logout_orb),
1005 lu->logout_orb,
1006 lu->logout_orb_dma);
1007 if (lu->query_logins_orb)
1008 dma_free_coherent(hi->host->device.parent,
1009 sizeof(struct sbp2_query_logins_orb),
1010 lu->query_logins_orb,
1011 lu->query_logins_orb_dma);
1012 if (lu->query_logins_response)
1013 dma_free_coherent(hi->host->device.parent,
1014 sizeof(struct sbp2_query_logins_response),
1015 lu->query_logins_response,
1016 lu->query_logins_response_dma);
1018 if (lu->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
1019 hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
1020 lu->status_fifo_addr);
1022 lu->ud->device.driver_data = NULL;
1024 module_put(hi->host->driver->owner);
1025 no_hi:
1026 kfree(lu);
1029 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
1031 * Deal with write requests on adapters which do not support physical DMA or
1032 * have it switched off.
1034 static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
1035 int destid, quadlet_t *data, u64 addr,
1036 size_t length, u16 flags)
1038 memcpy(bus_to_virt((u32) addr), data, length);
1039 return RCODE_COMPLETE;
1043 * Deal with read requests on adapters which do not support physical DMA or
1044 * have it switched off.
1046 static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
1047 quadlet_t *data, u64 addr, size_t length,
1048 u16 flags)
1050 memcpy(data, bus_to_virt((u32) addr), length);
1051 return RCODE_COMPLETE;
1053 #endif
1055 /**************************************
1056 * SBP-2 protocol related section
1057 **************************************/
1059 static int sbp2_query_logins(struct sbp2_lu *lu)
1061 struct sbp2_fwhost_info *hi = lu->hi;
1062 quadlet_t data[2];
1063 int max_logins;
1064 int active_logins;
1066 lu->query_logins_orb->reserved1 = 0x0;
1067 lu->query_logins_orb->reserved2 = 0x0;
1069 lu->query_logins_orb->query_response_lo = lu->query_logins_response_dma;
1070 lu->query_logins_orb->query_response_hi =
1071 ORB_SET_NODE_ID(hi->host->node_id);
1072 lu->query_logins_orb->lun_misc =
1073 ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
1074 lu->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
1075 lu->query_logins_orb->lun_misc |= ORB_SET_LUN(lu->lun);
1077 lu->query_logins_orb->reserved_resp_length =
1078 ORB_SET_QUERY_LOGINS_RESP_LENGTH(
1079 sizeof(struct sbp2_query_logins_response));
1081 lu->query_logins_orb->status_fifo_hi =
1082 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1083 lu->query_logins_orb->status_fifo_lo =
1084 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1086 sbp2util_cpu_to_be32_buffer(lu->query_logins_orb,
1087 sizeof(struct sbp2_query_logins_orb));
1089 memset(lu->query_logins_response, 0,
1090 sizeof(struct sbp2_query_logins_response));
1092 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1093 data[1] = lu->query_logins_orb_dma;
1094 sbp2util_cpu_to_be32_buffer(data, 8);
1096 hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1098 if (sbp2util_access_timeout(lu, 2*HZ)) {
1099 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
1100 return -EIO;
1103 if (lu->status_block.ORB_offset_lo != lu->query_logins_orb_dma) {
1104 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
1105 return -EIO;
1108 if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
1109 SBP2_INFO("Error querying logins to SBP-2 device - failed");
1110 return -EIO;
1113 sbp2util_cpu_to_be32_buffer(lu->query_logins_response,
1114 sizeof(struct sbp2_query_logins_response));
1116 max_logins = RESPONSE_GET_MAX_LOGINS(
1117 lu->query_logins_response->length_max_logins);
1118 SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
1120 active_logins = RESPONSE_GET_ACTIVE_LOGINS(
1121 lu->query_logins_response->length_max_logins);
1122 SBP2_INFO("Number of active logins: %d", active_logins);
1124 if (active_logins >= max_logins) {
1125 return -EIO;
1128 return 0;
1131 static int sbp2_login_device(struct sbp2_lu *lu)
1133 struct sbp2_fwhost_info *hi = lu->hi;
1134 quadlet_t data[2];
1136 if (!lu->login_orb)
1137 return -EIO;
1139 if (!sbp2_exclusive_login && sbp2_query_logins(lu)) {
1140 SBP2_INFO("Device does not support any more concurrent logins");
1141 return -EIO;
1144 /* assume no password */
1145 lu->login_orb->password_hi = 0;
1146 lu->login_orb->password_lo = 0;
1148 lu->login_orb->login_response_lo = lu->login_response_dma;
1149 lu->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1150 lu->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
1152 /* one second reconnect time */
1153 lu->login_orb->lun_misc |= ORB_SET_RECONNECT(0);
1154 lu->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(sbp2_exclusive_login);
1155 lu->login_orb->lun_misc |= ORB_SET_NOTIFY(1);
1156 lu->login_orb->lun_misc |= ORB_SET_LUN(lu->lun);
1158 lu->login_orb->passwd_resp_lengths =
1159 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
1161 lu->login_orb->status_fifo_hi =
1162 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1163 lu->login_orb->status_fifo_lo =
1164 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1166 sbp2util_cpu_to_be32_buffer(lu->login_orb,
1167 sizeof(struct sbp2_login_orb));
1169 memset(lu->login_response, 0, sizeof(struct sbp2_login_response));
1171 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1172 data[1] = lu->login_orb_dma;
1173 sbp2util_cpu_to_be32_buffer(data, 8);
1175 hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1177 /* wait up to 20 seconds for login status */
1178 if (sbp2util_access_timeout(lu, 20*HZ)) {
1179 SBP2_ERR("Error logging into SBP-2 device - timed out");
1180 return -EIO;
1183 /* make sure that the returned status matches the login ORB */
1184 if (lu->status_block.ORB_offset_lo != lu->login_orb_dma) {
1185 SBP2_ERR("Error logging into SBP-2 device - timed out");
1186 return -EIO;
1189 if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
1190 SBP2_ERR("Error logging into SBP-2 device - failed");
1191 return -EIO;
1194 sbp2util_cpu_to_be32_buffer(lu->login_response,
1195 sizeof(struct sbp2_login_response));
1196 lu->command_block_agent_addr =
1197 ((u64)lu->login_response->command_block_agent_hi) << 32;
1198 lu->command_block_agent_addr |=
1199 ((u64)lu->login_response->command_block_agent_lo);
1200 lu->command_block_agent_addr &= 0x0000ffffffffffffULL;
1202 SBP2_INFO("Logged into SBP-2 device");
1203 return 0;
1206 static int sbp2_logout_device(struct sbp2_lu *lu)
1208 struct sbp2_fwhost_info *hi = lu->hi;
1209 quadlet_t data[2];
1210 int error;
1212 lu->logout_orb->reserved1 = 0x0;
1213 lu->logout_orb->reserved2 = 0x0;
1214 lu->logout_orb->reserved3 = 0x0;
1215 lu->logout_orb->reserved4 = 0x0;
1217 lu->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
1218 lu->logout_orb->login_ID_misc |=
1219 ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
1220 lu->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1222 lu->logout_orb->reserved5 = 0x0;
1223 lu->logout_orb->status_fifo_hi =
1224 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1225 lu->logout_orb->status_fifo_lo =
1226 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1228 sbp2util_cpu_to_be32_buffer(lu->logout_orb,
1229 sizeof(struct sbp2_logout_orb));
1231 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1232 data[1] = lu->logout_orb_dma;
1233 sbp2util_cpu_to_be32_buffer(data, 8);
1235 error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1236 if (error)
1237 return error;
1239 /* wait up to 1 second for the device to complete logout */
1240 if (sbp2util_access_timeout(lu, HZ))
1241 return -EIO;
1243 SBP2_INFO("Logged out of SBP-2 device");
1244 return 0;
1247 static int sbp2_reconnect_device(struct sbp2_lu *lu)
1249 struct sbp2_fwhost_info *hi = lu->hi;
1250 quadlet_t data[2];
1251 int error;
1253 lu->reconnect_orb->reserved1 = 0x0;
1254 lu->reconnect_orb->reserved2 = 0x0;
1255 lu->reconnect_orb->reserved3 = 0x0;
1256 lu->reconnect_orb->reserved4 = 0x0;
1258 lu->reconnect_orb->login_ID_misc =
1259 ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
1260 lu->reconnect_orb->login_ID_misc |=
1261 ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
1262 lu->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1264 lu->reconnect_orb->reserved5 = 0x0;
1265 lu->reconnect_orb->status_fifo_hi =
1266 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1267 lu->reconnect_orb->status_fifo_lo =
1268 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1270 sbp2util_cpu_to_be32_buffer(lu->reconnect_orb,
1271 sizeof(struct sbp2_reconnect_orb));
1273 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1274 data[1] = lu->reconnect_orb_dma;
1275 sbp2util_cpu_to_be32_buffer(data, 8);
1277 error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1278 if (error)
1279 return error;
1281 /* wait up to 1 second for reconnect status */
1282 if (sbp2util_access_timeout(lu, HZ)) {
1283 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
1284 return -EIO;
1287 /* make sure that the returned status matches the reconnect ORB */
1288 if (lu->status_block.ORB_offset_lo != lu->reconnect_orb_dma) {
1289 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
1290 return -EIO;
1293 if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
1294 SBP2_ERR("Error reconnecting to SBP-2 device - failed");
1295 return -EIO;
1298 SBP2_INFO("Reconnected to SBP-2 device");
1299 return 0;
1303 * Set the target node's Single Phase Retry limit. Affects the target's retry
1304 * behaviour if our node is too busy to accept requests.
1306 static int sbp2_set_busy_timeout(struct sbp2_lu *lu)
1308 quadlet_t data;
1310 data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
1311 if (hpsb_node_write(lu->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
1312 SBP2_ERR("%s error", __FUNCTION__);
1313 return 0;
1316 static void sbp2_parse_unit_directory(struct sbp2_lu *lu,
1317 struct unit_directory *ud)
1319 struct csr1212_keyval *kv;
1320 struct csr1212_dentry *dentry;
1321 u64 management_agent_addr;
1322 u32 unit_characteristics, firmware_revision;
1323 unsigned workarounds;
1324 int i;
1326 management_agent_addr = 0;
1327 unit_characteristics = 0;
1328 firmware_revision = 0;
1330 csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
1331 switch (kv->key.id) {
1332 case CSR1212_KV_ID_DEPENDENT_INFO:
1333 if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET)
1334 management_agent_addr =
1335 CSR1212_REGISTER_SPACE_BASE +
1336 (kv->value.csr_offset << 2);
1338 else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE)
1339 lu->lun = ORB_SET_LUN(kv->value.immediate);
1340 break;
1342 case SBP2_UNIT_CHARACTERISTICS_KEY:
1343 /* FIXME: This is ignored so far.
1344 * See SBP-2 clause 7.4.8. */
1345 unit_characteristics = kv->value.immediate;
1346 break;
1348 case SBP2_FIRMWARE_REVISION_KEY:
1349 firmware_revision = kv->value.immediate;
1350 break;
1352 default:
1353 /* FIXME: Check for SBP2_DEVICE_TYPE_AND_LUN_KEY.
1354 * Its "ordered" bit has consequences for command ORB
1355 * list handling. See SBP-2 clauses 4.6, 7.4.11, 10.2 */
1356 break;
1360 workarounds = sbp2_default_workarounds;
1362 if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
1363 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
1364 if (sbp2_workarounds_table[i].firmware_revision !=
1365 SBP2_ROM_VALUE_WILDCARD &&
1366 sbp2_workarounds_table[i].firmware_revision !=
1367 (firmware_revision & 0xffff00))
1368 continue;
1369 if (sbp2_workarounds_table[i].model_id !=
1370 SBP2_ROM_VALUE_WILDCARD &&
1371 sbp2_workarounds_table[i].model_id != ud->model_id)
1372 continue;
1373 workarounds |= sbp2_workarounds_table[i].workarounds;
1374 break;
1377 if (workarounds)
1378 SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
1379 "(firmware_revision 0x%06x, vendor_id 0x%06x,"
1380 " model_id 0x%06x)",
1381 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
1382 workarounds, firmware_revision,
1383 ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
1384 ud->model_id);
1386 /* We would need one SCSI host template for each target to adjust
1387 * max_sectors on the fly, therefore warn only. */
1388 if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
1389 (sbp2_max_sectors * 512) > (128 * 1024))
1390 SBP2_INFO("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
1391 "max transfer size. WARNING: Current max_sectors "
1392 "setting is larger than 128KB (%d sectors)",
1393 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
1394 sbp2_max_sectors);
1396 /* If this is a logical unit directory entry, process the parent
1397 * to get the values. */
1398 if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
1399 struct unit_directory *parent_ud = container_of(
1400 ud->device.parent, struct unit_directory, device);
1401 sbp2_parse_unit_directory(lu, parent_ud);
1402 } else {
1403 lu->management_agent_addr = management_agent_addr;
1404 lu->workarounds = workarounds;
1405 if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
1406 lu->lun = ORB_SET_LUN(ud->lun);
1410 #define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
1413 * This function is called in order to determine the max speed and packet
1414 * size we can use in our ORBs. Note, that we (the driver and host) only
1415 * initiate the transaction. The SBP-2 device actually transfers the data
1416 * (by reading from the DMA area we tell it). This means that the SBP-2
1417 * device decides the actual maximum data it can transfer. We just tell it
1418 * the speed that it needs to use, and the max_rec the host supports, and
1419 * it takes care of the rest.
1421 static int sbp2_max_speed_and_size(struct sbp2_lu *lu)
1423 struct sbp2_fwhost_info *hi = lu->hi;
1424 u8 payload;
1426 lu->speed_code = hi->host->speed[NODEID_TO_NODE(lu->ne->nodeid)];
1428 if (lu->speed_code > sbp2_max_speed) {
1429 lu->speed_code = sbp2_max_speed;
1430 SBP2_INFO("Reducing speed to %s",
1431 hpsb_speedto_str[sbp2_max_speed]);
1434 /* Payload size is the lesser of what our speed supports and what
1435 * our host supports. */
1436 payload = min(sbp2_speedto_max_payload[lu->speed_code],
1437 (u8) (hi->host->csr.max_rec - 1));
1439 /* If physical DMA is off, work around limitation in ohci1394:
1440 * packet size must not exceed PAGE_SIZE */
1441 if (lu->ne->host->low_addr_space < (1ULL << 32))
1442 while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
1443 payload)
1444 payload--;
1446 SBP2_INFO("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
1447 NODE_BUS_ARGS(hi->host, lu->ne->nodeid),
1448 hpsb_speedto_str[lu->speed_code],
1449 SBP2_PAYLOAD_TO_BYTES(payload));
1451 lu->max_payload_size = payload;
1452 return 0;
1455 static int sbp2_agent_reset(struct sbp2_lu *lu, int wait)
1457 quadlet_t data;
1458 u64 addr;
1459 int retval;
1460 unsigned long flags;
1462 /* flush lu->protocol_work */
1463 if (wait)
1464 flush_scheduled_work();
1466 data = ntohl(SBP2_AGENT_RESET_DATA);
1467 addr = lu->command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
1469 if (wait)
1470 retval = hpsb_node_write(lu->ne, addr, &data, 4);
1471 else
1472 retval = sbp2util_node_write_no_wait(lu->ne, addr, &data, 4);
1474 if (retval < 0) {
1475 SBP2_ERR("hpsb_node_write failed.\n");
1476 return -EIO;
1479 /* make sure that the ORB_POINTER is written on next command */
1480 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1481 lu->last_orb = NULL;
1482 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1484 return 0;
1487 static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
1488 struct sbp2_fwhost_info *hi,
1489 struct sbp2_command_info *cmd,
1490 unsigned int scsi_use_sg,
1491 struct scatterlist *sg,
1492 u32 orb_direction,
1493 enum dma_data_direction dma_dir)
1495 cmd->dma_dir = dma_dir;
1496 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1497 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1499 /* special case if only one element (and less than 64KB in size) */
1500 if (scsi_use_sg == 1 && sg->length <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1502 cmd->dma_size = sg->length;
1503 cmd->dma_type = CMD_DMA_PAGE;
1504 cmd->cmd_dma = dma_map_page(hi->host->device.parent,
1505 sg_page(sg), sg->offset,
1506 cmd->dma_size, cmd->dma_dir);
1508 orb->data_descriptor_lo = cmd->cmd_dma;
1509 orb->misc |= ORB_SET_DATA_SIZE(cmd->dma_size);
1511 } else {
1512 struct sbp2_unrestricted_page_table *sg_element =
1513 &cmd->scatter_gather_element[0];
1514 u32 sg_count, sg_len;
1515 dma_addr_t sg_addr;
1516 int i, count = dma_map_sg(hi->host->device.parent, sg,
1517 scsi_use_sg, dma_dir);
1519 cmd->dma_size = scsi_use_sg;
1520 cmd->sge_buffer = sg;
1522 /* use page tables (s/g) */
1523 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1524 orb->data_descriptor_lo = cmd->sge_dma;
1526 /* loop through and fill out our SBP-2 page tables
1527 * (and split up anything too large) */
1528 for (i = 0, sg_count = 0; i < count; i++, sg = sg_next(sg)) {
1529 sg_len = sg_dma_len(sg);
1530 sg_addr = sg_dma_address(sg);
1531 while (sg_len) {
1532 sg_element[sg_count].segment_base_lo = sg_addr;
1533 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1534 sg_element[sg_count].length_segment_base_hi =
1535 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1536 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1537 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1538 } else {
1539 sg_element[sg_count].length_segment_base_hi =
1540 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1541 sg_len = 0;
1543 sg_count++;
1547 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1549 sbp2util_cpu_to_be32_buffer(sg_element,
1550 (sizeof(struct sbp2_unrestricted_page_table)) *
1551 sg_count);
1555 static void sbp2_create_command_orb(struct sbp2_lu *lu,
1556 struct sbp2_command_info *cmd,
1557 unchar *scsi_cmd,
1558 unsigned int scsi_use_sg,
1559 unsigned int scsi_request_bufflen,
1560 struct scatterlist *sg,
1561 enum dma_data_direction dma_dir)
1563 struct sbp2_fwhost_info *hi = lu->hi;
1564 struct sbp2_command_orb *orb = &cmd->command_orb;
1565 u32 orb_direction;
1568 * Set-up our command ORB.
1570 * NOTE: We're doing unrestricted page tables (s/g), as this is
1571 * best performance (at least with the devices I have). This means
1572 * that data_size becomes the number of s/g elements, and
1573 * page_size should be zero (for unrestricted).
1575 orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1576 orb->next_ORB_lo = 0x0;
1577 orb->misc = ORB_SET_MAX_PAYLOAD(lu->max_payload_size);
1578 orb->misc |= ORB_SET_SPEED(lu->speed_code);
1579 orb->misc |= ORB_SET_NOTIFY(1);
1581 if (dma_dir == DMA_NONE)
1582 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1583 else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
1584 orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
1585 else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
1586 orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
1587 else {
1588 SBP2_INFO("Falling back to DMA_NONE");
1589 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1592 /* set up our page table stuff */
1593 if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
1594 orb->data_descriptor_hi = 0x0;
1595 orb->data_descriptor_lo = 0x0;
1596 orb->misc |= ORB_SET_DIRECTION(1);
1597 } else
1598 sbp2_prep_command_orb_sg(orb, hi, cmd, scsi_use_sg, sg,
1599 orb_direction, dma_dir);
1601 sbp2util_cpu_to_be32_buffer(orb, sizeof(*orb));
1603 memset(orb->cdb, 0, 12);
1604 memcpy(orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
1607 static void sbp2_link_orb_command(struct sbp2_lu *lu,
1608 struct sbp2_command_info *cmd)
1610 struct sbp2_fwhost_info *hi = lu->hi;
1611 struct sbp2_command_orb *last_orb;
1612 dma_addr_t last_orb_dma;
1613 u64 addr = lu->command_block_agent_addr;
1614 quadlet_t data[2];
1615 size_t length;
1616 unsigned long flags;
1618 dma_sync_single_for_device(hi->host->device.parent,
1619 cmd->command_orb_dma,
1620 sizeof(struct sbp2_command_orb),
1621 DMA_TO_DEVICE);
1622 dma_sync_single_for_device(hi->host->device.parent, cmd->sge_dma,
1623 sizeof(cmd->scatter_gather_element),
1624 DMA_TO_DEVICE);
1626 /* check to see if there are any previous orbs to use */
1627 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1628 last_orb = lu->last_orb;
1629 last_orb_dma = lu->last_orb_dma;
1630 if (!last_orb) {
1632 * last_orb == NULL means: We know that the target's fetch agent
1633 * is not active right now.
1635 addr += SBP2_ORB_POINTER_OFFSET;
1636 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1637 data[1] = cmd->command_orb_dma;
1638 sbp2util_cpu_to_be32_buffer(data, 8);
1639 length = 8;
1640 } else {
1642 * last_orb != NULL means: We know that the target's fetch agent
1643 * is (very probably) not dead or in reset state right now.
1644 * We have an ORB already sent that we can append a new one to.
1645 * The target's fetch agent may or may not have read this
1646 * previous ORB yet.
1648 dma_sync_single_for_cpu(hi->host->device.parent, last_orb_dma,
1649 sizeof(struct sbp2_command_orb),
1650 DMA_TO_DEVICE);
1651 last_orb->next_ORB_lo = cpu_to_be32(cmd->command_orb_dma);
1652 wmb();
1653 /* Tells hardware that this pointer is valid */
1654 last_orb->next_ORB_hi = 0;
1655 dma_sync_single_for_device(hi->host->device.parent,
1656 last_orb_dma,
1657 sizeof(struct sbp2_command_orb),
1658 DMA_TO_DEVICE);
1659 addr += SBP2_DOORBELL_OFFSET;
1660 data[0] = 0;
1661 length = 4;
1663 lu->last_orb = &cmd->command_orb;
1664 lu->last_orb_dma = cmd->command_orb_dma;
1665 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1667 if (sbp2util_node_write_no_wait(lu->ne, addr, data, length)) {
1669 * sbp2util_node_write_no_wait failed. We certainly ran out
1670 * of transaction labels, perhaps just because there were no
1671 * context switches which gave khpsbpkt a chance to collect
1672 * free tlabels. Try again in non-atomic context. If necessary,
1673 * the workqueue job will sleep to guaranteedly get a tlabel.
1674 * We do not accept new commands until the job is over.
1676 scsi_block_requests(lu->shost);
1677 PREPARE_WORK(&lu->protocol_work,
1678 last_orb ? sbp2util_write_doorbell:
1679 sbp2util_write_orb_pointer);
1680 schedule_work(&lu->protocol_work);
1684 static int sbp2_send_command(struct sbp2_lu *lu, struct scsi_cmnd *SCpnt,
1685 void (*done)(struct scsi_cmnd *))
1687 unchar *scsi_cmd = (unchar *)SCpnt->cmnd;
1688 struct sbp2_command_info *cmd;
1690 cmd = sbp2util_allocate_command_orb(lu, SCpnt, done);
1691 if (!cmd)
1692 return -EIO;
1694 sbp2_create_command_orb(lu, cmd, scsi_cmd, scsi_sg_count(SCpnt),
1695 scsi_bufflen(SCpnt), scsi_sglist(SCpnt),
1696 SCpnt->sc_data_direction);
1697 sbp2_link_orb_command(lu, cmd);
1699 return 0;
1703 * Translates SBP-2 status into SCSI sense data for check conditions
1705 static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status,
1706 unchar *sense_data)
1708 /* OK, it's pretty ugly... ;-) */
1709 sense_data[0] = 0x70;
1710 sense_data[1] = 0x0;
1711 sense_data[2] = sbp2_status[9];
1712 sense_data[3] = sbp2_status[12];
1713 sense_data[4] = sbp2_status[13];
1714 sense_data[5] = sbp2_status[14];
1715 sense_data[6] = sbp2_status[15];
1716 sense_data[7] = 10;
1717 sense_data[8] = sbp2_status[16];
1718 sense_data[9] = sbp2_status[17];
1719 sense_data[10] = sbp2_status[18];
1720 sense_data[11] = sbp2_status[19];
1721 sense_data[12] = sbp2_status[10];
1722 sense_data[13] = sbp2_status[11];
1723 sense_data[14] = sbp2_status[20];
1724 sense_data[15] = sbp2_status[21];
1726 return sbp2_status[8] & 0x3f;
1729 static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
1730 int destid, quadlet_t *data, u64 addr,
1731 size_t length, u16 fl)
1733 struct sbp2_fwhost_info *hi;
1734 struct sbp2_lu *lu = NULL, *lu_tmp;
1735 struct scsi_cmnd *SCpnt = NULL;
1736 struct sbp2_status_block *sb;
1737 u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
1738 struct sbp2_command_info *cmd;
1739 unsigned long flags;
1741 if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
1742 SBP2_ERR("Wrong size of status block");
1743 return RCODE_ADDRESS_ERROR;
1745 if (unlikely(!host)) {
1746 SBP2_ERR("host is NULL - this is bad!");
1747 return RCODE_ADDRESS_ERROR;
1749 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
1750 if (unlikely(!hi)) {
1751 SBP2_ERR("host info is NULL - this is bad!");
1752 return RCODE_ADDRESS_ERROR;
1755 /* Find the unit which wrote the status. */
1756 read_lock_irqsave(&sbp2_hi_logical_units_lock, flags);
1757 list_for_each_entry(lu_tmp, &hi->logical_units, lu_list) {
1758 if (lu_tmp->ne->nodeid == nodeid &&
1759 lu_tmp->status_fifo_addr == addr) {
1760 lu = lu_tmp;
1761 break;
1764 read_unlock_irqrestore(&sbp2_hi_logical_units_lock, flags);
1766 if (unlikely(!lu)) {
1767 SBP2_ERR("lu is NULL - device is gone?");
1768 return RCODE_ADDRESS_ERROR;
1771 /* Put response into lu status fifo buffer. The first two bytes
1772 * come in big endian bit order. Often the target writes only a
1773 * truncated status block, minimally the first two quadlets. The rest
1774 * is implied to be zeros. */
1775 sb = &lu->status_block;
1776 memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
1777 memcpy(sb, data, length);
1778 sbp2util_be32_to_cpu_buffer(sb, 8);
1780 /* Ignore unsolicited status. Handle command ORB status. */
1781 if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
1782 cmd = NULL;
1783 else
1784 cmd = sbp2util_find_command_for_orb(lu, sb->ORB_offset_lo);
1785 if (cmd) {
1786 dma_sync_single_for_cpu(hi->host->device.parent,
1787 cmd->command_orb_dma,
1788 sizeof(struct sbp2_command_orb),
1789 DMA_TO_DEVICE);
1790 dma_sync_single_for_cpu(hi->host->device.parent, cmd->sge_dma,
1791 sizeof(cmd->scatter_gather_element),
1792 DMA_TO_DEVICE);
1793 /* Grab SCSI command pointers and check status. */
1795 * FIXME: If the src field in the status is 1, the ORB DMA must
1796 * not be reused until status for a subsequent ORB is received.
1798 SCpnt = cmd->Current_SCpnt;
1799 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1800 sbp2util_mark_command_completed(lu, cmd);
1801 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1803 if (SCpnt) {
1804 u32 h = sb->ORB_offset_hi_misc;
1805 u32 r = STATUS_GET_RESP(h);
1807 if (r != RESP_STATUS_REQUEST_COMPLETE) {
1808 SBP2_INFO("resp 0x%x, sbp_status 0x%x",
1809 r, STATUS_GET_SBP_STATUS(h));
1810 scsi_status =
1811 r == RESP_STATUS_TRANSPORT_FAILURE ?
1812 SBP2_SCSI_STATUS_BUSY :
1813 SBP2_SCSI_STATUS_COMMAND_TERMINATED;
1816 if (STATUS_GET_LEN(h) > 1)
1817 scsi_status = sbp2_status_to_sense_data(
1818 (unchar *)sb, SCpnt->sense_buffer);
1820 if (STATUS_TEST_DEAD(h))
1821 sbp2_agent_reset(lu, 0);
1824 /* Check here to see if there are no commands in-use. If there
1825 * are none, we know that the fetch agent left the active state
1826 * _and_ that we did not reactivate it yet. Therefore clear
1827 * last_orb so that next time we write directly to the
1828 * ORB_POINTER register. That way the fetch agent does not need
1829 * to refetch the next_ORB. */
1830 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1831 if (list_empty(&lu->cmd_orb_inuse))
1832 lu->last_orb = NULL;
1833 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1835 } else {
1836 /* It's probably status after a management request. */
1837 if ((sb->ORB_offset_lo == lu->reconnect_orb_dma) ||
1838 (sb->ORB_offset_lo == lu->login_orb_dma) ||
1839 (sb->ORB_offset_lo == lu->query_logins_orb_dma) ||
1840 (sb->ORB_offset_lo == lu->logout_orb_dma)) {
1841 lu->access_complete = 1;
1842 wake_up_interruptible(&sbp2_access_wq);
1846 if (SCpnt)
1847 sbp2scsi_complete_command(lu, scsi_status, SCpnt,
1848 cmd->Current_done);
1849 return RCODE_COMPLETE;
1852 /**************************************
1853 * SCSI interface related section
1854 **************************************/
1856 static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
1857 void (*done)(struct scsi_cmnd *))
1859 struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
1860 struct sbp2_fwhost_info *hi;
1861 int result = DID_NO_CONNECT << 16;
1863 if (unlikely(!sbp2util_node_is_available(lu)))
1864 goto done;
1866 hi = lu->hi;
1868 if (unlikely(!hi)) {
1869 SBP2_ERR("sbp2_fwhost_info is NULL - this is bad!");
1870 goto done;
1873 /* Multiple units are currently represented to the SCSI core as separate
1874 * targets, not as one target with multiple LUs. Therefore return
1875 * selection time-out to any IO directed at non-zero LUNs. */
1876 if (unlikely(SCpnt->device->lun))
1877 goto done;
1879 if (unlikely(!hpsb_node_entry_valid(lu->ne))) {
1880 SBP2_ERR("Bus reset in progress - rejecting command");
1881 result = DID_BUS_BUSY << 16;
1882 goto done;
1885 /* Bidirectional commands are not yet implemented,
1886 * and unknown transfer direction not handled. */
1887 if (unlikely(SCpnt->sc_data_direction == DMA_BIDIRECTIONAL)) {
1888 SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
1889 result = DID_ERROR << 16;
1890 goto done;
1893 if (sbp2_send_command(lu, SCpnt, done)) {
1894 SBP2_ERR("Error sending SCSI command");
1895 sbp2scsi_complete_command(lu,
1896 SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
1897 SCpnt, done);
1899 return 0;
1901 done:
1902 SCpnt->result = result;
1903 done(SCpnt);
1904 return 0;
1907 static void sbp2scsi_complete_all_commands(struct sbp2_lu *lu, u32 status)
1909 struct sbp2_fwhost_info *hi = lu->hi;
1910 struct list_head *lh;
1911 struct sbp2_command_info *cmd;
1912 unsigned long flags;
1914 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1915 while (!list_empty(&lu->cmd_orb_inuse)) {
1916 lh = lu->cmd_orb_inuse.next;
1917 cmd = list_entry(lh, struct sbp2_command_info, list);
1918 dma_sync_single_for_cpu(hi->host->device.parent,
1919 cmd->command_orb_dma,
1920 sizeof(struct sbp2_command_orb),
1921 DMA_TO_DEVICE);
1922 dma_sync_single_for_cpu(hi->host->device.parent, cmd->sge_dma,
1923 sizeof(cmd->scatter_gather_element),
1924 DMA_TO_DEVICE);
1925 sbp2util_mark_command_completed(lu, cmd);
1926 if (cmd->Current_SCpnt) {
1927 cmd->Current_SCpnt->result = status << 16;
1928 cmd->Current_done(cmd->Current_SCpnt);
1931 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1933 return;
1937 * Complete a regular SCSI command. Can be called in atomic context.
1939 static void sbp2scsi_complete_command(struct sbp2_lu *lu, u32 scsi_status,
1940 struct scsi_cmnd *SCpnt,
1941 void (*done)(struct scsi_cmnd *))
1943 if (!SCpnt) {
1944 SBP2_ERR("SCpnt is NULL");
1945 return;
1948 switch (scsi_status) {
1949 case SBP2_SCSI_STATUS_GOOD:
1950 SCpnt->result = DID_OK << 16;
1951 break;
1953 case SBP2_SCSI_STATUS_BUSY:
1954 SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
1955 SCpnt->result = DID_BUS_BUSY << 16;
1956 break;
1958 case SBP2_SCSI_STATUS_CHECK_CONDITION:
1959 SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
1960 break;
1962 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
1963 SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
1964 SCpnt->result = DID_NO_CONNECT << 16;
1965 scsi_print_command(SCpnt);
1966 break;
1968 case SBP2_SCSI_STATUS_CONDITION_MET:
1969 case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
1970 case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
1971 SBP2_ERR("Bad SCSI status = %x", scsi_status);
1972 SCpnt->result = DID_ERROR << 16;
1973 scsi_print_command(SCpnt);
1974 break;
1976 default:
1977 SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
1978 SCpnt->result = DID_ERROR << 16;
1981 /* If a bus reset is in progress and there was an error, complete
1982 * the command as busy so that it will get retried. */
1983 if (!hpsb_node_entry_valid(lu->ne)
1984 && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
1985 SBP2_ERR("Completing command with busy (bus reset)");
1986 SCpnt->result = DID_BUS_BUSY << 16;
1989 /* Tell the SCSI stack that we're done with this command. */
1990 done(SCpnt);
1993 static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
1995 struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
1997 <<<<<<< HEAD:drivers/ieee1394/sbp2.c
1998 =======
1999 if (sdev->lun != 0 || sdev->id != lu->ud->id || sdev->channel != 0)
2000 return -ENODEV;
2002 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ieee1394/sbp2.c
2003 lu->sdev = sdev;
2004 sdev->allow_restart = 1;
2007 * Update the dma alignment (minimum alignment requirements for
2008 * start and end of DMA transfers) to be a sector
2010 blk_queue_update_dma_alignment(sdev->request_queue, 511);
2012 if (lu->workarounds & SBP2_WORKAROUND_INQUIRY_36)
2013 sdev->inquiry_len = 36;
2014 return 0;
2017 static int sbp2scsi_slave_configure(struct scsi_device *sdev)
2019 struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
2021 sdev->use_10_for_rw = 1;
2023 if (sdev->type == TYPE_ROM)
2024 sdev->use_10_for_ms = 1;
2025 if (sdev->type == TYPE_DISK &&
2026 lu->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
2027 sdev->skip_ms_page_8 = 1;
2028 if (lu->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
2029 sdev->fix_capacity = 1;
2030 if (lu->workarounds & SBP2_WORKAROUND_128K_MAX_TRANS)
2031 blk_queue_max_sectors(sdev->request_queue, 128 * 1024 / 512);
2032 return 0;
2035 static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
2037 ((struct sbp2_lu *)sdev->host->hostdata[0])->sdev = NULL;
2038 return;
2042 * Called by scsi stack when something has really gone wrong.
2043 * Usually called when a command has timed-out for some reason.
2045 static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
2047 struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
2048 struct sbp2_fwhost_info *hi = lu->hi;
2049 struct sbp2_command_info *cmd;
2050 unsigned long flags;
2052 SBP2_INFO("aborting sbp2 command");
2053 scsi_print_command(SCpnt);
2055 if (sbp2util_node_is_available(lu)) {
2056 sbp2_agent_reset(lu, 1);
2058 /* Return a matching command structure to the free pool. */
2059 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
2060 cmd = sbp2util_find_command_for_SCpnt(lu, SCpnt);
2061 if (cmd) {
2062 dma_sync_single_for_cpu(hi->host->device.parent,
2063 cmd->command_orb_dma,
2064 sizeof(struct sbp2_command_orb),
2065 DMA_TO_DEVICE);
2066 dma_sync_single_for_cpu(hi->host->device.parent,
2067 cmd->sge_dma,
2068 sizeof(cmd->scatter_gather_element),
2069 DMA_TO_DEVICE);
2070 sbp2util_mark_command_completed(lu, cmd);
2071 if (cmd->Current_SCpnt) {
2072 cmd->Current_SCpnt->result = DID_ABORT << 16;
2073 cmd->Current_done(cmd->Current_SCpnt);
2076 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
2078 sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
2081 return SUCCESS;
2085 * Called by scsi stack when something has really gone wrong.
2087 static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
2089 struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
2091 SBP2_INFO("reset requested");
2093 if (sbp2util_node_is_available(lu)) {
2094 SBP2_INFO("generating sbp2 fetch agent reset");
2095 sbp2_agent_reset(lu, 1);
2098 return SUCCESS;
2101 static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
2102 struct device_attribute *attr,
2103 char *buf)
2105 struct scsi_device *sdev;
2106 struct sbp2_lu *lu;
2108 if (!(sdev = to_scsi_device(dev)))
2109 return 0;
2111 if (!(lu = (struct sbp2_lu *)sdev->host->hostdata[0]))
2112 return 0;
2114 if (sbp2_long_sysfs_ieee1394_id)
2115 return sprintf(buf, "%016Lx:%06x:%04x\n",
2116 (unsigned long long)lu->ne->guid,
2117 lu->ud->directory_id, ORB_SET_LUN(lu->lun));
2118 else
2119 return sprintf(buf, "%016Lx:%d:%d\n",
2120 (unsigned long long)lu->ne->guid,
2121 lu->ud->id, ORB_SET_LUN(lu->lun));
2124 MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2125 MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2126 MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2127 MODULE_LICENSE("GPL");
2129 static int sbp2_module_init(void)
2131 int ret;
2133 if (sbp2_serialize_io) {
2134 sbp2_shost_template.can_queue = 1;
2135 sbp2_shost_template.cmd_per_lun = 1;
2138 sbp2_shost_template.max_sectors = sbp2_max_sectors;
2140 hpsb_register_highlevel(&sbp2_highlevel);
2141 ret = hpsb_register_protocol(&sbp2_driver);
2142 if (ret) {
2143 SBP2_ERR("Failed to register protocol");
2144 hpsb_unregister_highlevel(&sbp2_highlevel);
2145 return ret;
2147 return 0;
2150 static void __exit sbp2_module_exit(void)
2152 hpsb_unregister_protocol(&sbp2_driver);
2153 hpsb_unregister_highlevel(&sbp2_highlevel);
2156 module_init(sbp2_module_init);
2157 module_exit(sbp2_module_exit);