[ARM] Fix 4417/1: Serial: Fix AMBA drivers locking
[linux-2.6/x86.git] / drivers / ieee1394 / sbp2.c
blob3f873cc7e247a7944d4ac27359600daf378fb1f7
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/compiler.h>
55 #include <linux/delay.h>
56 #include <linux/device.h>
57 #include <linux/dma-mapping.h>
58 #include <linux/gfp.h>
59 #include <linux/init.h>
60 #include <linux/kernel.h>
61 #include <linux/list.h>
62 #include <linux/mm.h>
63 #include <linux/module.h>
64 #include <linux/moduleparam.h>
65 #include <linux/sched.h>
66 #include <linux/slab.h>
67 #include <linux/spinlock.h>
68 #include <linux/stat.h>
69 #include <linux/string.h>
70 #include <linux/stringify.h>
71 #include <linux/types.h>
72 #include <linux/wait.h>
73 #include <linux/workqueue.h>
75 #include <asm/byteorder.h>
76 #include <asm/errno.h>
77 #include <asm/param.h>
78 #include <asm/scatterlist.h>
79 #include <asm/system.h>
80 #include <asm/types.h>
82 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
83 #include <asm/io.h> /* for bus_to_virt */
84 #endif
86 #include <scsi/scsi.h>
87 #include <scsi/scsi_cmnd.h>
88 #include <scsi/scsi_dbg.h>
89 #include <scsi/scsi_device.h>
90 #include <scsi/scsi_host.h>
92 #include "csr1212.h"
93 #include "highlevel.h"
94 #include "hosts.h"
95 #include "ieee1394.h"
96 #include "ieee1394_core.h"
97 #include "ieee1394_hotplug.h"
98 #include "ieee1394_transactions.h"
99 #include "ieee1394_types.h"
100 #include "nodemgr.h"
101 #include "sbp2.h"
104 * Module load parameter definitions
108 * Change max_speed on module load if you have a bad IEEE-1394
109 * controller that has trouble running 2KB packets at 400mb.
111 * NOTE: On certain OHCI parts I have seen short packets on async transmit
112 * (probably due to PCI latency/throughput issues with the part). You can
113 * bump down the speed if you are running into problems.
115 static int sbp2_max_speed = IEEE1394_SPEED_MAX;
116 module_param_named(max_speed, sbp2_max_speed, int, 0644);
117 MODULE_PARM_DESC(max_speed, "Force max speed "
118 "(3 = 800Mb/s, 2 = 400Mb/s, 1 = 200Mb/s, 0 = 100Mb/s)");
121 * Set serialize_io to 1 if you'd like only one scsi command sent
122 * down to us at a time (debugging). This might be necessary for very
123 * badly behaved sbp2 devices.
125 static int sbp2_serialize_io = 1;
126 module_param_named(serialize_io, sbp2_serialize_io, int, 0444);
127 MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers "
128 "(default = 1, faster = 0)");
131 * Bump up max_sectors if you'd like to support very large sized
132 * transfers. Please note that some older sbp2 bridge chips are broken for
133 * transfers greater or equal to 128KB. Default is a value of 255
134 * sectors, or just under 128KB (at 512 byte sector size). I can note that
135 * the Oxsemi sbp2 chipsets have no problems supporting very large
136 * transfer sizes.
138 static int sbp2_max_sectors = SBP2_MAX_SECTORS;
139 module_param_named(max_sectors, sbp2_max_sectors, int, 0444);
140 MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported "
141 "(default = " __stringify(SBP2_MAX_SECTORS) ")");
144 * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
145 * do an exclusive login, as it's generally unsafe to have two hosts
146 * talking to a single sbp2 device at the same time (filesystem coherency,
147 * etc.). If you're running an sbp2 device that supports multiple logins,
148 * and you're either running read-only filesystems or some sort of special
149 * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster
150 * File System, or Lustre, then set exclusive_login to zero.
152 * So far only bridges from Oxford Semiconductor are known to support
153 * concurrent logins. Depending on firmware, four or two concurrent logins
154 * are possible on OXFW911 and newer Oxsemi bridges.
156 static int sbp2_exclusive_login = 1;
157 module_param_named(exclusive_login, sbp2_exclusive_login, int, 0644);
158 MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
159 "(default = 1)");
162 * If any of the following workarounds is required for your device to work,
163 * please submit the kernel messages logged by sbp2 to the linux1394-devel
164 * mailing list.
166 * - 128kB max transfer
167 * Limit transfer size. Necessary for some old bridges.
169 * - 36 byte inquiry
170 * When scsi_mod probes the device, let the inquiry command look like that
171 * from MS Windows.
173 * - skip mode page 8
174 * Suppress sending of mode_sense for mode page 8 if the device pretends to
175 * support the SCSI Primary Block commands instead of Reduced Block Commands.
177 * - fix capacity
178 * Tell sd_mod to correct the last sector number reported by read_capacity.
179 * Avoids access beyond actual disk limits on devices with an off-by-one bug.
180 * Don't use this with devices which don't have this bug.
182 * - override internal blacklist
183 * Instead of adding to the built-in blacklist, use only the workarounds
184 * specified in the module load parameter.
185 * Useful if a blacklist entry interfered with a non-broken device.
187 static int sbp2_default_workarounds;
188 module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
189 MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
190 ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
191 ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
192 ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
193 ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
194 ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
195 ", or a combination)");
198 * This influences the format of the sysfs attribute
199 * /sys/bus/scsi/devices/.../ieee1394_id.
201 * The default format is like in older kernels: %016Lx:%d:%d
202 * It contains the target's EUI-64, a number given to the logical unit by
203 * the ieee1394 driver's nodemgr (starting at 0), and the LUN.
205 * The long format is: %016Lx:%06x:%04x
206 * It contains the target's EUI-64, the unit directory's directory_ID as per
207 * IEEE 1212 clause 7.7.19, and the LUN. This format comes closest to the
208 * format of SBP(-3) target port and logical unit identifier as per SAM (SCSI
209 * Architecture Model) rev.2 to 4 annex A. Therefore and because it is
210 * independent of the implementation of the ieee1394 nodemgr, the longer format
211 * is recommended for future use.
213 static int sbp2_long_sysfs_ieee1394_id;
214 module_param_named(long_ieee1394_id, sbp2_long_sysfs_ieee1394_id, bool, 0644);
215 MODULE_PARM_DESC(long_ieee1394_id, "8+3+2 bytes format of ieee1394_id in sysfs "
216 "(default = backwards-compatible = N, SAM-conforming = Y)");
219 #define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
220 #define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
223 * Globals
225 static void sbp2scsi_complete_all_commands(struct sbp2_lu *, u32);
226 static void sbp2scsi_complete_command(struct sbp2_lu *, u32, struct scsi_cmnd *,
227 void (*)(struct scsi_cmnd *));
228 static struct sbp2_lu *sbp2_alloc_device(struct unit_directory *);
229 static int sbp2_start_device(struct sbp2_lu *);
230 static void sbp2_remove_device(struct sbp2_lu *);
231 static int sbp2_login_device(struct sbp2_lu *);
232 static int sbp2_reconnect_device(struct sbp2_lu *);
233 static int sbp2_logout_device(struct sbp2_lu *);
234 static void sbp2_host_reset(struct hpsb_host *);
235 static int sbp2_handle_status_write(struct hpsb_host *, int, int, quadlet_t *,
236 u64, size_t, u16);
237 static int sbp2_agent_reset(struct sbp2_lu *, int);
238 static void sbp2_parse_unit_directory(struct sbp2_lu *,
239 struct unit_directory *);
240 static int sbp2_set_busy_timeout(struct sbp2_lu *);
241 static int sbp2_max_speed_and_size(struct sbp2_lu *);
244 static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
246 static struct hpsb_highlevel sbp2_highlevel = {
247 .name = SBP2_DEVICE_NAME,
248 .host_reset = sbp2_host_reset,
251 static struct hpsb_address_ops sbp2_ops = {
252 .write = sbp2_handle_status_write
255 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
256 static int sbp2_handle_physdma_write(struct hpsb_host *, int, int, quadlet_t *,
257 u64, size_t, u16);
258 static int sbp2_handle_physdma_read(struct hpsb_host *, int, quadlet_t *, u64,
259 size_t, u16);
261 static struct hpsb_address_ops sbp2_physdma_ops = {
262 .read = sbp2_handle_physdma_read,
263 .write = sbp2_handle_physdma_write,
265 #endif
269 * Interface to driver core and IEEE 1394 core
271 static struct ieee1394_device_id sbp2_id_table[] = {
273 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
274 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
275 .version = SBP2_SW_VERSION_ENTRY & 0xffffff},
278 MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
280 static int sbp2_probe(struct device *);
281 static int sbp2_remove(struct device *);
282 static int sbp2_update(struct unit_directory *);
284 static struct hpsb_protocol_driver sbp2_driver = {
285 .name = SBP2_DEVICE_NAME,
286 .id_table = sbp2_id_table,
287 .update = sbp2_update,
288 .driver = {
289 .probe = sbp2_probe,
290 .remove = sbp2_remove,
296 * Interface to SCSI core
298 static int sbp2scsi_queuecommand(struct scsi_cmnd *,
299 void (*)(struct scsi_cmnd *));
300 static int sbp2scsi_abort(struct scsi_cmnd *);
301 static int sbp2scsi_reset(struct scsi_cmnd *);
302 static int sbp2scsi_slave_alloc(struct scsi_device *);
303 static int sbp2scsi_slave_configure(struct scsi_device *);
304 static void sbp2scsi_slave_destroy(struct scsi_device *);
305 static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *,
306 struct device_attribute *, char *);
308 static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
310 static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
311 &dev_attr_ieee1394_id,
312 NULL
315 static struct scsi_host_template sbp2_shost_template = {
316 .module = THIS_MODULE,
317 .name = "SBP-2 IEEE-1394",
318 .proc_name = SBP2_DEVICE_NAME,
319 .queuecommand = sbp2scsi_queuecommand,
320 .eh_abort_handler = sbp2scsi_abort,
321 .eh_device_reset_handler = sbp2scsi_reset,
322 .slave_alloc = sbp2scsi_slave_alloc,
323 .slave_configure = sbp2scsi_slave_configure,
324 .slave_destroy = sbp2scsi_slave_destroy,
325 .this_id = -1,
326 .sg_tablesize = SG_ALL,
327 .use_clustering = ENABLE_CLUSTERING,
328 .cmd_per_lun = SBP2_MAX_CMDS,
329 .can_queue = SBP2_MAX_CMDS,
330 .sdev_attrs = sbp2_sysfs_sdev_attrs,
333 /* for match-all entries in sbp2_workarounds_table */
334 #define SBP2_ROM_VALUE_WILDCARD 0x1000000
337 * List of devices with known bugs.
339 * The firmware_revision field, masked with 0xffff00, is the best indicator
340 * for the type of bridge chip of a device. It yields a few false positives
341 * but this did not break correctly behaving devices so far.
343 static const struct {
344 u32 firmware_revision;
345 u32 model_id;
346 unsigned workarounds;
347 } sbp2_workarounds_table[] = {
348 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
349 .firmware_revision = 0x002800,
350 .model_id = 0x001010,
351 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
352 SBP2_WORKAROUND_MODE_SENSE_8,
354 /* Initio bridges, actually only needed for some older ones */ {
355 .firmware_revision = 0x000200,
356 .model_id = SBP2_ROM_VALUE_WILDCARD,
357 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
359 /* Symbios bridge */ {
360 .firmware_revision = 0xa0b800,
361 .model_id = SBP2_ROM_VALUE_WILDCARD,
362 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
364 /* iPod 4th generation */ {
365 .firmware_revision = 0x0a2700,
366 .model_id = 0x000021,
367 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
369 /* iPod mini */ {
370 .firmware_revision = 0x0a2700,
371 .model_id = 0x000023,
372 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
374 /* iPod Photo */ {
375 .firmware_revision = 0x0a2700,
376 .model_id = 0x00007e,
377 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
381 /**************************************
382 * General utility functions
383 **************************************/
385 #ifndef __BIG_ENDIAN
387 * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
389 static inline void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
391 u32 *temp = buffer;
393 for (length = (length >> 2); length--; )
394 temp[length] = be32_to_cpu(temp[length]);
398 * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
400 static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
402 u32 *temp = buffer;
404 for (length = (length >> 2); length--; )
405 temp[length] = cpu_to_be32(temp[length]);
407 #else /* BIG_ENDIAN */
408 /* Why waste the cpu cycles? */
409 #define sbp2util_be32_to_cpu_buffer(x,y) do {} while (0)
410 #define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0)
411 #endif
413 static DECLARE_WAIT_QUEUE_HEAD(sbp2_access_wq);
416 * Waits for completion of an SBP-2 access request.
417 * Returns nonzero if timed out or prematurely interrupted.
419 static int sbp2util_access_timeout(struct sbp2_lu *lu, int timeout)
421 long leftover;
423 leftover = wait_event_interruptible_timeout(
424 sbp2_access_wq, lu->access_complete, timeout);
425 lu->access_complete = 0;
426 return leftover <= 0;
429 static void sbp2_free_packet(void *packet)
431 hpsb_free_tlabel(packet);
432 hpsb_free_packet(packet);
436 * This is much like hpsb_node_write(), except it ignores the response
437 * subaction and returns immediately. Can be used from atomic context.
439 static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
440 quadlet_t *buf, size_t len)
442 struct hpsb_packet *packet;
444 packet = hpsb_make_writepacket(ne->host, ne->nodeid, addr, buf, len);
445 if (!packet)
446 return -ENOMEM;
448 hpsb_set_packet_complete_task(packet, sbp2_free_packet, packet);
449 hpsb_node_fill_packet(ne, packet);
450 if (hpsb_send_packet(packet) < 0) {
451 sbp2_free_packet(packet);
452 return -EIO;
454 return 0;
457 static void sbp2util_notify_fetch_agent(struct sbp2_lu *lu, u64 offset,
458 quadlet_t *data, size_t len)
460 /* There is a small window after a bus reset within which the node
461 * entry's generation is current but the reconnect wasn't completed. */
462 if (unlikely(atomic_read(&lu->state) == SBP2LU_STATE_IN_RESET))
463 return;
465 if (hpsb_node_write(lu->ne, lu->command_block_agent_addr + offset,
466 data, len))
467 SBP2_ERR("sbp2util_notify_fetch_agent failed.");
469 /* Now accept new SCSI commands, unless a bus reset happended during
470 * hpsb_node_write. */
471 if (likely(atomic_read(&lu->state) != SBP2LU_STATE_IN_RESET))
472 scsi_unblock_requests(lu->shost);
475 static void sbp2util_write_orb_pointer(struct work_struct *work)
477 struct sbp2_lu *lu = container_of(work, struct sbp2_lu, protocol_work);
478 quadlet_t data[2];
480 data[0] = ORB_SET_NODE_ID(lu->hi->host->node_id);
481 data[1] = lu->last_orb_dma;
482 sbp2util_cpu_to_be32_buffer(data, 8);
483 sbp2util_notify_fetch_agent(lu, SBP2_ORB_POINTER_OFFSET, data, 8);
486 static void sbp2util_write_doorbell(struct work_struct *work)
488 struct sbp2_lu *lu = container_of(work, struct sbp2_lu, protocol_work);
490 sbp2util_notify_fetch_agent(lu, SBP2_DOORBELL_OFFSET, NULL, 4);
493 static int sbp2util_create_command_orb_pool(struct sbp2_lu *lu)
495 struct sbp2_fwhost_info *hi = lu->hi;
496 struct sbp2_command_info *cmd;
497 int i, orbs = sbp2_serialize_io ? 2 : SBP2_MAX_CMDS;
499 for (i = 0; i < orbs; i++) {
500 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
501 if (!cmd)
502 return -ENOMEM;
503 cmd->command_orb_dma = dma_map_single(hi->host->device.parent,
504 &cmd->command_orb,
505 sizeof(struct sbp2_command_orb),
506 DMA_TO_DEVICE);
507 cmd->sge_dma = dma_map_single(hi->host->device.parent,
508 &cmd->scatter_gather_element,
509 sizeof(cmd->scatter_gather_element),
510 DMA_TO_DEVICE);
511 INIT_LIST_HEAD(&cmd->list);
512 list_add_tail(&cmd->list, &lu->cmd_orb_completed);
514 return 0;
517 static void sbp2util_remove_command_orb_pool(struct sbp2_lu *lu)
519 struct hpsb_host *host = lu->hi->host;
520 struct list_head *lh, *next;
521 struct sbp2_command_info *cmd;
522 unsigned long flags;
524 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
525 if (!list_empty(&lu->cmd_orb_completed))
526 list_for_each_safe(lh, next, &lu->cmd_orb_completed) {
527 cmd = list_entry(lh, struct sbp2_command_info, list);
528 dma_unmap_single(host->device.parent,
529 cmd->command_orb_dma,
530 sizeof(struct sbp2_command_orb),
531 DMA_TO_DEVICE);
532 dma_unmap_single(host->device.parent, cmd->sge_dma,
533 sizeof(cmd->scatter_gather_element),
534 DMA_TO_DEVICE);
535 kfree(cmd);
537 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
538 return;
542 * Finds the sbp2_command for a given outstanding command ORB.
543 * Only looks at the in-use list.
545 static struct sbp2_command_info *sbp2util_find_command_for_orb(
546 struct sbp2_lu *lu, dma_addr_t orb)
548 struct sbp2_command_info *cmd;
549 unsigned long flags;
551 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
552 if (!list_empty(&lu->cmd_orb_inuse))
553 list_for_each_entry(cmd, &lu->cmd_orb_inuse, list)
554 if (cmd->command_orb_dma == orb) {
555 spin_unlock_irqrestore(
556 &lu->cmd_orb_lock, flags);
557 return cmd;
559 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
560 return NULL;
564 * Finds the sbp2_command for a given outstanding SCpnt.
565 * Only looks at the in-use list.
566 * Must be called with lu->cmd_orb_lock held.
568 static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
569 struct sbp2_lu *lu, void *SCpnt)
571 struct sbp2_command_info *cmd;
573 if (!list_empty(&lu->cmd_orb_inuse))
574 list_for_each_entry(cmd, &lu->cmd_orb_inuse, list)
575 if (cmd->Current_SCpnt == SCpnt)
576 return cmd;
577 return NULL;
580 static struct sbp2_command_info *sbp2util_allocate_command_orb(
581 struct sbp2_lu *lu,
582 struct scsi_cmnd *Current_SCpnt,
583 void (*Current_done)(struct scsi_cmnd *))
585 struct list_head *lh;
586 struct sbp2_command_info *cmd = NULL;
587 unsigned long flags;
589 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
590 if (!list_empty(&lu->cmd_orb_completed)) {
591 lh = lu->cmd_orb_completed.next;
592 list_del(lh);
593 cmd = list_entry(lh, struct sbp2_command_info, list);
594 cmd->Current_done = Current_done;
595 cmd->Current_SCpnt = Current_SCpnt;
596 list_add_tail(&cmd->list, &lu->cmd_orb_inuse);
597 } else
598 SBP2_ERR("%s: no orbs available", __FUNCTION__);
599 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
600 return cmd;
604 * Unmaps the DMAs of a command and moves the command to the completed ORB list.
605 * Must be called with lu->cmd_orb_lock held.
607 static void sbp2util_mark_command_completed(struct sbp2_lu *lu,
608 struct sbp2_command_info *cmd)
610 struct hpsb_host *host = lu->ud->ne->host;
612 if (cmd->cmd_dma) {
613 if (cmd->dma_type == CMD_DMA_SINGLE)
614 dma_unmap_single(host->device.parent, cmd->cmd_dma,
615 cmd->dma_size, cmd->dma_dir);
616 else if (cmd->dma_type == CMD_DMA_PAGE)
617 dma_unmap_page(host->device.parent, cmd->cmd_dma,
618 cmd->dma_size, cmd->dma_dir);
619 /* XXX: Check for CMD_DMA_NONE bug */
620 cmd->dma_type = CMD_DMA_NONE;
621 cmd->cmd_dma = 0;
623 if (cmd->sge_buffer) {
624 dma_unmap_sg(host->device.parent, cmd->sge_buffer,
625 cmd->dma_size, cmd->dma_dir);
626 cmd->sge_buffer = NULL;
628 list_move_tail(&cmd->list, &lu->cmd_orb_completed);
632 * Is lu valid? Is the 1394 node still present?
634 static inline int sbp2util_node_is_available(struct sbp2_lu *lu)
636 return lu && lu->ne && !lu->ne->in_limbo;
639 /*********************************************
640 * IEEE-1394 core driver stack related section
641 *********************************************/
643 static int sbp2_probe(struct device *dev)
645 struct unit_directory *ud;
646 struct sbp2_lu *lu;
648 ud = container_of(dev, struct unit_directory, device);
650 /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
651 * instead. */
652 if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
653 return -ENODEV;
655 lu = sbp2_alloc_device(ud);
656 if (!lu)
657 return -ENOMEM;
659 sbp2_parse_unit_directory(lu, ud);
660 return sbp2_start_device(lu);
663 static int sbp2_remove(struct device *dev)
665 struct unit_directory *ud;
666 struct sbp2_lu *lu;
667 struct scsi_device *sdev;
669 ud = container_of(dev, struct unit_directory, device);
670 lu = ud->device.driver_data;
671 if (!lu)
672 return 0;
674 if (lu->shost) {
675 /* Get rid of enqueued commands if there is no chance to
676 * send them. */
677 if (!sbp2util_node_is_available(lu))
678 sbp2scsi_complete_all_commands(lu, DID_NO_CONNECT);
679 /* scsi_remove_device() may trigger shutdown functions of SCSI
680 * highlevel drivers which would deadlock if blocked. */
681 atomic_set(&lu->state, SBP2LU_STATE_IN_SHUTDOWN);
682 scsi_unblock_requests(lu->shost);
684 sdev = lu->sdev;
685 if (sdev) {
686 lu->sdev = NULL;
687 scsi_remove_device(sdev);
690 sbp2_logout_device(lu);
691 sbp2_remove_device(lu);
693 return 0;
696 static int sbp2_update(struct unit_directory *ud)
698 struct sbp2_lu *lu = ud->device.driver_data;
700 if (sbp2_reconnect_device(lu)) {
701 /* Reconnect has failed. Perhaps we didn't reconnect fast
702 * enough. Try a regular login, but first log out just in
703 * case of any weirdness. */
704 sbp2_logout_device(lu);
706 if (sbp2_login_device(lu)) {
707 /* Login failed too, just fail, and the backend
708 * will call our sbp2_remove for us */
709 SBP2_ERR("Failed to reconnect to sbp2 device!");
710 return -EBUSY;
714 sbp2_set_busy_timeout(lu);
715 sbp2_agent_reset(lu, 1);
716 sbp2_max_speed_and_size(lu);
718 /* Complete any pending commands with busy (so they get retried)
719 * and remove them from our queue. */
720 sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
722 /* Accept new commands unless there was another bus reset in the
723 * meantime. */
724 if (hpsb_node_entry_valid(lu->ne)) {
725 atomic_set(&lu->state, SBP2LU_STATE_RUNNING);
726 scsi_unblock_requests(lu->shost);
728 return 0;
731 static struct sbp2_lu *sbp2_alloc_device(struct unit_directory *ud)
733 struct sbp2_fwhost_info *hi;
734 struct Scsi_Host *shost = NULL;
735 struct sbp2_lu *lu = NULL;
737 lu = kzalloc(sizeof(*lu), GFP_KERNEL);
738 if (!lu) {
739 SBP2_ERR("failed to create lu");
740 goto failed_alloc;
743 lu->ne = ud->ne;
744 lu->ud = ud;
745 lu->speed_code = IEEE1394_SPEED_100;
746 lu->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
747 lu->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
748 INIT_LIST_HEAD(&lu->cmd_orb_inuse);
749 INIT_LIST_HEAD(&lu->cmd_orb_completed);
750 INIT_LIST_HEAD(&lu->lu_list);
751 spin_lock_init(&lu->cmd_orb_lock);
752 atomic_set(&lu->state, SBP2LU_STATE_RUNNING);
753 INIT_WORK(&lu->protocol_work, NULL);
755 ud->device.driver_data = lu;
757 hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
758 if (!hi) {
759 hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host,
760 sizeof(*hi));
761 if (!hi) {
762 SBP2_ERR("failed to allocate hostinfo");
763 goto failed_alloc;
765 hi->host = ud->ne->host;
766 INIT_LIST_HEAD(&hi->logical_units);
768 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
769 /* Handle data movement if physical dma is not
770 * enabled or not supported on host controller */
771 if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
772 &sbp2_physdma_ops,
773 0x0ULL, 0xfffffffcULL)) {
774 SBP2_ERR("failed to register lower 4GB address range");
775 goto failed_alloc;
777 #else
778 if (dma_set_mask(hi->host->device.parent, DMA_32BIT_MASK)) {
779 SBP2_ERR("failed to set 4GB DMA mask");
780 goto failed_alloc;
782 #endif
785 /* Prevent unloading of the 1394 host */
786 if (!try_module_get(hi->host->driver->owner)) {
787 SBP2_ERR("failed to get a reference on 1394 host driver");
788 goto failed_alloc;
791 lu->hi = hi;
793 list_add_tail(&lu->lu_list, &hi->logical_units);
795 /* Register the status FIFO address range. We could use the same FIFO
796 * for targets at different nodes. However we need different FIFOs per
797 * target in order to support multi-unit devices.
798 * The FIFO is located out of the local host controller's physical range
799 * but, if possible, within the posted write area. Status writes will
800 * then be performed as unified transactions. This slightly reduces
801 * bandwidth usage, and some Prolific based devices seem to require it.
803 lu->status_fifo_addr = hpsb_allocate_and_register_addrspace(
804 &sbp2_highlevel, ud->ne->host, &sbp2_ops,
805 sizeof(struct sbp2_status_block), sizeof(quadlet_t),
806 ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
807 if (lu->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
808 SBP2_ERR("failed to allocate status FIFO address range");
809 goto failed_alloc;
812 shost = scsi_host_alloc(&sbp2_shost_template, sizeof(unsigned long));
813 if (!shost) {
814 SBP2_ERR("failed to register scsi host");
815 goto failed_alloc;
818 shost->hostdata[0] = (unsigned long)lu;
820 if (!scsi_add_host(shost, &ud->device)) {
821 lu->shost = shost;
822 return lu;
825 SBP2_ERR("failed to add scsi host");
826 scsi_host_put(shost);
828 failed_alloc:
829 sbp2_remove_device(lu);
830 return NULL;
833 static void sbp2_host_reset(struct hpsb_host *host)
835 struct sbp2_fwhost_info *hi;
836 struct sbp2_lu *lu;
838 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
839 if (!hi)
840 return;
841 list_for_each_entry(lu, &hi->logical_units, lu_list)
842 if (likely(atomic_read(&lu->state) !=
843 SBP2LU_STATE_IN_SHUTDOWN)) {
844 atomic_set(&lu->state, SBP2LU_STATE_IN_RESET);
845 scsi_block_requests(lu->shost);
849 static int sbp2_start_device(struct sbp2_lu *lu)
851 struct sbp2_fwhost_info *hi = lu->hi;
852 int error;
854 lu->login_response = dma_alloc_coherent(hi->host->device.parent,
855 sizeof(struct sbp2_login_response),
856 &lu->login_response_dma, GFP_KERNEL);
857 if (!lu->login_response)
858 goto alloc_fail;
860 lu->query_logins_orb = dma_alloc_coherent(hi->host->device.parent,
861 sizeof(struct sbp2_query_logins_orb),
862 &lu->query_logins_orb_dma, GFP_KERNEL);
863 if (!lu->query_logins_orb)
864 goto alloc_fail;
866 lu->query_logins_response = dma_alloc_coherent(hi->host->device.parent,
867 sizeof(struct sbp2_query_logins_response),
868 &lu->query_logins_response_dma, GFP_KERNEL);
869 if (!lu->query_logins_response)
870 goto alloc_fail;
872 lu->reconnect_orb = dma_alloc_coherent(hi->host->device.parent,
873 sizeof(struct sbp2_reconnect_orb),
874 &lu->reconnect_orb_dma, GFP_KERNEL);
875 if (!lu->reconnect_orb)
876 goto alloc_fail;
878 lu->logout_orb = dma_alloc_coherent(hi->host->device.parent,
879 sizeof(struct sbp2_logout_orb),
880 &lu->logout_orb_dma, GFP_KERNEL);
881 if (!lu->logout_orb)
882 goto alloc_fail;
884 lu->login_orb = dma_alloc_coherent(hi->host->device.parent,
885 sizeof(struct sbp2_login_orb),
886 &lu->login_orb_dma, GFP_KERNEL);
887 if (!lu->login_orb)
888 goto alloc_fail;
890 if (sbp2util_create_command_orb_pool(lu))
891 goto alloc_fail;
893 /* Wait a second before trying to log in. Previously logged in
894 * initiators need a chance to reconnect. */
895 if (msleep_interruptible(1000)) {
896 sbp2_remove_device(lu);
897 return -EINTR;
900 if (sbp2_login_device(lu)) {
901 sbp2_remove_device(lu);
902 return -EBUSY;
905 sbp2_set_busy_timeout(lu);
906 sbp2_agent_reset(lu, 1);
907 sbp2_max_speed_and_size(lu);
909 error = scsi_add_device(lu->shost, 0, lu->ud->id, 0);
910 if (error) {
911 SBP2_ERR("scsi_add_device failed");
912 sbp2_logout_device(lu);
913 sbp2_remove_device(lu);
914 return error;
917 return 0;
919 alloc_fail:
920 SBP2_ERR("Could not allocate memory for lu");
921 sbp2_remove_device(lu);
922 return -ENOMEM;
925 static void sbp2_remove_device(struct sbp2_lu *lu)
927 struct sbp2_fwhost_info *hi;
929 if (!lu)
930 return;
932 hi = lu->hi;
934 if (lu->shost) {
935 scsi_remove_host(lu->shost);
936 scsi_host_put(lu->shost);
938 flush_scheduled_work();
939 sbp2util_remove_command_orb_pool(lu);
941 list_del(&lu->lu_list);
943 if (lu->login_response)
944 dma_free_coherent(hi->host->device.parent,
945 sizeof(struct sbp2_login_response),
946 lu->login_response,
947 lu->login_response_dma);
948 if (lu->login_orb)
949 dma_free_coherent(hi->host->device.parent,
950 sizeof(struct sbp2_login_orb),
951 lu->login_orb,
952 lu->login_orb_dma);
953 if (lu->reconnect_orb)
954 dma_free_coherent(hi->host->device.parent,
955 sizeof(struct sbp2_reconnect_orb),
956 lu->reconnect_orb,
957 lu->reconnect_orb_dma);
958 if (lu->logout_orb)
959 dma_free_coherent(hi->host->device.parent,
960 sizeof(struct sbp2_logout_orb),
961 lu->logout_orb,
962 lu->logout_orb_dma);
963 if (lu->query_logins_orb)
964 dma_free_coherent(hi->host->device.parent,
965 sizeof(struct sbp2_query_logins_orb),
966 lu->query_logins_orb,
967 lu->query_logins_orb_dma);
968 if (lu->query_logins_response)
969 dma_free_coherent(hi->host->device.parent,
970 sizeof(struct sbp2_query_logins_response),
971 lu->query_logins_response,
972 lu->query_logins_response_dma);
974 if (lu->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
975 hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
976 lu->status_fifo_addr);
978 lu->ud->device.driver_data = NULL;
980 if (hi)
981 module_put(hi->host->driver->owner);
983 kfree(lu);
986 #ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
988 * Deal with write requests on adapters which do not support physical DMA or
989 * have it switched off.
991 static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
992 int destid, quadlet_t *data, u64 addr,
993 size_t length, u16 flags)
995 memcpy(bus_to_virt((u32) addr), data, length);
996 return RCODE_COMPLETE;
1000 * Deal with read requests on adapters which do not support physical DMA or
1001 * have it switched off.
1003 static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
1004 quadlet_t *data, u64 addr, size_t length,
1005 u16 flags)
1007 memcpy(data, bus_to_virt((u32) addr), length);
1008 return RCODE_COMPLETE;
1010 #endif
1012 /**************************************
1013 * SBP-2 protocol related section
1014 **************************************/
1016 static int sbp2_query_logins(struct sbp2_lu *lu)
1018 struct sbp2_fwhost_info *hi = lu->hi;
1019 quadlet_t data[2];
1020 int max_logins;
1021 int active_logins;
1023 lu->query_logins_orb->reserved1 = 0x0;
1024 lu->query_logins_orb->reserved2 = 0x0;
1026 lu->query_logins_orb->query_response_lo = lu->query_logins_response_dma;
1027 lu->query_logins_orb->query_response_hi =
1028 ORB_SET_NODE_ID(hi->host->node_id);
1029 lu->query_logins_orb->lun_misc =
1030 ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
1031 lu->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
1032 lu->query_logins_orb->lun_misc |= ORB_SET_LUN(lu->lun);
1034 lu->query_logins_orb->reserved_resp_length =
1035 ORB_SET_QUERY_LOGINS_RESP_LENGTH(
1036 sizeof(struct sbp2_query_logins_response));
1038 lu->query_logins_orb->status_fifo_hi =
1039 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1040 lu->query_logins_orb->status_fifo_lo =
1041 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1043 sbp2util_cpu_to_be32_buffer(lu->query_logins_orb,
1044 sizeof(struct sbp2_query_logins_orb));
1046 memset(lu->query_logins_response, 0,
1047 sizeof(struct sbp2_query_logins_response));
1049 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1050 data[1] = lu->query_logins_orb_dma;
1051 sbp2util_cpu_to_be32_buffer(data, 8);
1053 hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1055 if (sbp2util_access_timeout(lu, 2*HZ)) {
1056 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
1057 return -EIO;
1060 if (lu->status_block.ORB_offset_lo != lu->query_logins_orb_dma) {
1061 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
1062 return -EIO;
1065 if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
1066 SBP2_INFO("Error querying logins to SBP-2 device - failed");
1067 return -EIO;
1070 sbp2util_cpu_to_be32_buffer(lu->query_logins_response,
1071 sizeof(struct sbp2_query_logins_response));
1073 max_logins = RESPONSE_GET_MAX_LOGINS(
1074 lu->query_logins_response->length_max_logins);
1075 SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
1077 active_logins = RESPONSE_GET_ACTIVE_LOGINS(
1078 lu->query_logins_response->length_max_logins);
1079 SBP2_INFO("Number of active logins: %d", active_logins);
1081 if (active_logins >= max_logins) {
1082 return -EIO;
1085 return 0;
1088 static int sbp2_login_device(struct sbp2_lu *lu)
1090 struct sbp2_fwhost_info *hi = lu->hi;
1091 quadlet_t data[2];
1093 if (!lu->login_orb)
1094 return -EIO;
1096 if (!sbp2_exclusive_login && sbp2_query_logins(lu)) {
1097 SBP2_INFO("Device does not support any more concurrent logins");
1098 return -EIO;
1101 /* assume no password */
1102 lu->login_orb->password_hi = 0;
1103 lu->login_orb->password_lo = 0;
1105 lu->login_orb->login_response_lo = lu->login_response_dma;
1106 lu->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1107 lu->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
1109 /* one second reconnect time */
1110 lu->login_orb->lun_misc |= ORB_SET_RECONNECT(0);
1111 lu->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(sbp2_exclusive_login);
1112 lu->login_orb->lun_misc |= ORB_SET_NOTIFY(1);
1113 lu->login_orb->lun_misc |= ORB_SET_LUN(lu->lun);
1115 lu->login_orb->passwd_resp_lengths =
1116 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
1118 lu->login_orb->status_fifo_hi =
1119 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1120 lu->login_orb->status_fifo_lo =
1121 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1123 sbp2util_cpu_to_be32_buffer(lu->login_orb,
1124 sizeof(struct sbp2_login_orb));
1126 memset(lu->login_response, 0, sizeof(struct sbp2_login_response));
1128 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1129 data[1] = lu->login_orb_dma;
1130 sbp2util_cpu_to_be32_buffer(data, 8);
1132 hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1134 /* wait up to 20 seconds for login status */
1135 if (sbp2util_access_timeout(lu, 20*HZ)) {
1136 SBP2_ERR("Error logging into SBP-2 device - timed out");
1137 return -EIO;
1140 /* make sure that the returned status matches the login ORB */
1141 if (lu->status_block.ORB_offset_lo != lu->login_orb_dma) {
1142 SBP2_ERR("Error logging into SBP-2 device - timed out");
1143 return -EIO;
1146 if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
1147 SBP2_ERR("Error logging into SBP-2 device - failed");
1148 return -EIO;
1151 sbp2util_cpu_to_be32_buffer(lu->login_response,
1152 sizeof(struct sbp2_login_response));
1153 lu->command_block_agent_addr =
1154 ((u64)lu->login_response->command_block_agent_hi) << 32;
1155 lu->command_block_agent_addr |=
1156 ((u64)lu->login_response->command_block_agent_lo);
1157 lu->command_block_agent_addr &= 0x0000ffffffffffffULL;
1159 SBP2_INFO("Logged into SBP-2 device");
1160 return 0;
1163 static int sbp2_logout_device(struct sbp2_lu *lu)
1165 struct sbp2_fwhost_info *hi = lu->hi;
1166 quadlet_t data[2];
1167 int error;
1169 lu->logout_orb->reserved1 = 0x0;
1170 lu->logout_orb->reserved2 = 0x0;
1171 lu->logout_orb->reserved3 = 0x0;
1172 lu->logout_orb->reserved4 = 0x0;
1174 lu->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
1175 lu->logout_orb->login_ID_misc |=
1176 ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
1177 lu->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1179 lu->logout_orb->reserved5 = 0x0;
1180 lu->logout_orb->status_fifo_hi =
1181 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1182 lu->logout_orb->status_fifo_lo =
1183 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1185 sbp2util_cpu_to_be32_buffer(lu->logout_orb,
1186 sizeof(struct sbp2_logout_orb));
1188 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1189 data[1] = lu->logout_orb_dma;
1190 sbp2util_cpu_to_be32_buffer(data, 8);
1192 error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1193 if (error)
1194 return error;
1196 /* wait up to 1 second for the device to complete logout */
1197 if (sbp2util_access_timeout(lu, HZ))
1198 return -EIO;
1200 SBP2_INFO("Logged out of SBP-2 device");
1201 return 0;
1204 static int sbp2_reconnect_device(struct sbp2_lu *lu)
1206 struct sbp2_fwhost_info *hi = lu->hi;
1207 quadlet_t data[2];
1208 int error;
1210 lu->reconnect_orb->reserved1 = 0x0;
1211 lu->reconnect_orb->reserved2 = 0x0;
1212 lu->reconnect_orb->reserved3 = 0x0;
1213 lu->reconnect_orb->reserved4 = 0x0;
1215 lu->reconnect_orb->login_ID_misc =
1216 ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
1217 lu->reconnect_orb->login_ID_misc |=
1218 ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
1219 lu->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1221 lu->reconnect_orb->reserved5 = 0x0;
1222 lu->reconnect_orb->status_fifo_hi =
1223 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1224 lu->reconnect_orb->status_fifo_lo =
1225 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1227 sbp2util_cpu_to_be32_buffer(lu->reconnect_orb,
1228 sizeof(struct sbp2_reconnect_orb));
1230 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1231 data[1] = lu->reconnect_orb_dma;
1232 sbp2util_cpu_to_be32_buffer(data, 8);
1234 error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1235 if (error)
1236 return error;
1238 /* wait up to 1 second for reconnect status */
1239 if (sbp2util_access_timeout(lu, HZ)) {
1240 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
1241 return -EIO;
1244 /* make sure that the returned status matches the reconnect ORB */
1245 if (lu->status_block.ORB_offset_lo != lu->reconnect_orb_dma) {
1246 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
1247 return -EIO;
1250 if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
1251 SBP2_ERR("Error reconnecting to SBP-2 device - failed");
1252 return -EIO;
1255 SBP2_INFO("Reconnected to SBP-2 device");
1256 return 0;
1260 * Set the target node's Single Phase Retry limit. Affects the target's retry
1261 * behaviour if our node is too busy to accept requests.
1263 static int sbp2_set_busy_timeout(struct sbp2_lu *lu)
1265 quadlet_t data;
1267 data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
1268 if (hpsb_node_write(lu->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
1269 SBP2_ERR("%s error", __FUNCTION__);
1270 return 0;
1273 static void sbp2_parse_unit_directory(struct sbp2_lu *lu,
1274 struct unit_directory *ud)
1276 struct csr1212_keyval *kv;
1277 struct csr1212_dentry *dentry;
1278 u64 management_agent_addr;
1279 u32 unit_characteristics, firmware_revision;
1280 unsigned workarounds;
1281 int i;
1283 management_agent_addr = 0;
1284 unit_characteristics = 0;
1285 firmware_revision = 0;
1287 csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
1288 switch (kv->key.id) {
1289 case CSR1212_KV_ID_DEPENDENT_INFO:
1290 if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET)
1291 management_agent_addr =
1292 CSR1212_REGISTER_SPACE_BASE +
1293 (kv->value.csr_offset << 2);
1295 else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE)
1296 lu->lun = ORB_SET_LUN(kv->value.immediate);
1297 break;
1299 case SBP2_UNIT_CHARACTERISTICS_KEY:
1300 /* FIXME: This is ignored so far.
1301 * See SBP-2 clause 7.4.8. */
1302 unit_characteristics = kv->value.immediate;
1303 break;
1305 case SBP2_FIRMWARE_REVISION_KEY:
1306 firmware_revision = kv->value.immediate;
1307 break;
1309 default:
1310 /* FIXME: Check for SBP2_DEVICE_TYPE_AND_LUN_KEY.
1311 * Its "ordered" bit has consequences for command ORB
1312 * list handling. See SBP-2 clauses 4.6, 7.4.11, 10.2 */
1313 break;
1317 workarounds = sbp2_default_workarounds;
1319 if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
1320 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
1321 if (sbp2_workarounds_table[i].firmware_revision !=
1322 SBP2_ROM_VALUE_WILDCARD &&
1323 sbp2_workarounds_table[i].firmware_revision !=
1324 (firmware_revision & 0xffff00))
1325 continue;
1326 if (sbp2_workarounds_table[i].model_id !=
1327 SBP2_ROM_VALUE_WILDCARD &&
1328 sbp2_workarounds_table[i].model_id != ud->model_id)
1329 continue;
1330 workarounds |= sbp2_workarounds_table[i].workarounds;
1331 break;
1334 if (workarounds)
1335 SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
1336 "(firmware_revision 0x%06x, vendor_id 0x%06x,"
1337 " model_id 0x%06x)",
1338 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
1339 workarounds, firmware_revision,
1340 ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
1341 ud->model_id);
1343 /* We would need one SCSI host template for each target to adjust
1344 * max_sectors on the fly, therefore warn only. */
1345 if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
1346 (sbp2_max_sectors * 512) > (128 * 1024))
1347 SBP2_INFO("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
1348 "max transfer size. WARNING: Current max_sectors "
1349 "setting is larger than 128KB (%d sectors)",
1350 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
1351 sbp2_max_sectors);
1353 /* If this is a logical unit directory entry, process the parent
1354 * to get the values. */
1355 if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
1356 struct unit_directory *parent_ud = container_of(
1357 ud->device.parent, struct unit_directory, device);
1358 sbp2_parse_unit_directory(lu, parent_ud);
1359 } else {
1360 lu->management_agent_addr = management_agent_addr;
1361 lu->workarounds = workarounds;
1362 if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
1363 lu->lun = ORB_SET_LUN(ud->lun);
1367 #define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
1370 * This function is called in order to determine the max speed and packet
1371 * size we can use in our ORBs. Note, that we (the driver and host) only
1372 * initiate the transaction. The SBP-2 device actually transfers the data
1373 * (by reading from the DMA area we tell it). This means that the SBP-2
1374 * device decides the actual maximum data it can transfer. We just tell it
1375 * the speed that it needs to use, and the max_rec the host supports, and
1376 * it takes care of the rest.
1378 static int sbp2_max_speed_and_size(struct sbp2_lu *lu)
1380 struct sbp2_fwhost_info *hi = lu->hi;
1381 u8 payload;
1383 lu->speed_code = hi->host->speed[NODEID_TO_NODE(lu->ne->nodeid)];
1385 if (lu->speed_code > sbp2_max_speed) {
1386 lu->speed_code = sbp2_max_speed;
1387 SBP2_INFO("Reducing speed to %s",
1388 hpsb_speedto_str[sbp2_max_speed]);
1391 /* Payload size is the lesser of what our speed supports and what
1392 * our host supports. */
1393 payload = min(sbp2_speedto_max_payload[lu->speed_code],
1394 (u8) (hi->host->csr.max_rec - 1));
1396 /* If physical DMA is off, work around limitation in ohci1394:
1397 * packet size must not exceed PAGE_SIZE */
1398 if (lu->ne->host->low_addr_space < (1ULL << 32))
1399 while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
1400 payload)
1401 payload--;
1403 SBP2_INFO("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
1404 NODE_BUS_ARGS(hi->host, lu->ne->nodeid),
1405 hpsb_speedto_str[lu->speed_code],
1406 SBP2_PAYLOAD_TO_BYTES(payload));
1408 lu->max_payload_size = payload;
1409 return 0;
1412 static int sbp2_agent_reset(struct sbp2_lu *lu, int wait)
1414 quadlet_t data;
1415 u64 addr;
1416 int retval;
1417 unsigned long flags;
1419 /* flush lu->protocol_work */
1420 if (wait)
1421 flush_scheduled_work();
1423 data = ntohl(SBP2_AGENT_RESET_DATA);
1424 addr = lu->command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
1426 if (wait)
1427 retval = hpsb_node_write(lu->ne, addr, &data, 4);
1428 else
1429 retval = sbp2util_node_write_no_wait(lu->ne, addr, &data, 4);
1431 if (retval < 0) {
1432 SBP2_ERR("hpsb_node_write failed.\n");
1433 return -EIO;
1436 /* make sure that the ORB_POINTER is written on next command */
1437 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1438 lu->last_orb = NULL;
1439 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1441 return 0;
1444 static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
1445 struct sbp2_fwhost_info *hi,
1446 struct sbp2_command_info *cmd,
1447 unsigned int scsi_use_sg,
1448 struct scatterlist *sgpnt,
1449 u32 orb_direction,
1450 enum dma_data_direction dma_dir)
1452 cmd->dma_dir = dma_dir;
1453 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1454 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1456 /* special case if only one element (and less than 64KB in size) */
1457 if ((scsi_use_sg == 1) &&
1458 (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
1460 cmd->dma_size = sgpnt[0].length;
1461 cmd->dma_type = CMD_DMA_PAGE;
1462 cmd->cmd_dma = dma_map_page(hi->host->device.parent,
1463 sgpnt[0].page, sgpnt[0].offset,
1464 cmd->dma_size, cmd->dma_dir);
1466 orb->data_descriptor_lo = cmd->cmd_dma;
1467 orb->misc |= ORB_SET_DATA_SIZE(cmd->dma_size);
1469 } else {
1470 struct sbp2_unrestricted_page_table *sg_element =
1471 &cmd->scatter_gather_element[0];
1472 u32 sg_count, sg_len;
1473 dma_addr_t sg_addr;
1474 int i, count = dma_map_sg(hi->host->device.parent, sgpnt,
1475 scsi_use_sg, dma_dir);
1477 cmd->dma_size = scsi_use_sg;
1478 cmd->sge_buffer = sgpnt;
1480 /* use page tables (s/g) */
1481 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1482 orb->data_descriptor_lo = cmd->sge_dma;
1484 /* loop through and fill out our SBP-2 page tables
1485 * (and split up anything too large) */
1486 for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
1487 sg_len = sg_dma_len(sgpnt);
1488 sg_addr = sg_dma_address(sgpnt);
1489 while (sg_len) {
1490 sg_element[sg_count].segment_base_lo = sg_addr;
1491 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1492 sg_element[sg_count].length_segment_base_hi =
1493 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1494 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1495 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1496 } else {
1497 sg_element[sg_count].length_segment_base_hi =
1498 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1499 sg_len = 0;
1501 sg_count++;
1505 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1507 sbp2util_cpu_to_be32_buffer(sg_element,
1508 (sizeof(struct sbp2_unrestricted_page_table)) *
1509 sg_count);
1513 static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
1514 struct sbp2_fwhost_info *hi,
1515 struct sbp2_command_info *cmd,
1516 struct scatterlist *sgpnt,
1517 u32 orb_direction,
1518 unsigned int scsi_request_bufflen,
1519 void *scsi_request_buffer,
1520 enum dma_data_direction dma_dir)
1522 cmd->dma_dir = dma_dir;
1523 cmd->dma_size = scsi_request_bufflen;
1524 cmd->dma_type = CMD_DMA_SINGLE;
1525 cmd->cmd_dma = dma_map_single(hi->host->device.parent,
1526 scsi_request_buffer,
1527 cmd->dma_size, cmd->dma_dir);
1528 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1529 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1531 /* handle case where we get a command w/o s/g enabled
1532 * (but check for transfers larger than 64K) */
1533 if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1535 orb->data_descriptor_lo = cmd->cmd_dma;
1536 orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
1538 } else {
1539 /* The buffer is too large. Turn this into page tables. */
1541 struct sbp2_unrestricted_page_table *sg_element =
1542 &cmd->scatter_gather_element[0];
1543 u32 sg_count, sg_len;
1544 dma_addr_t sg_addr;
1546 orb->data_descriptor_lo = cmd->sge_dma;
1547 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1549 /* fill out our SBP-2 page tables; split up the large buffer */
1550 sg_count = 0;
1551 sg_len = scsi_request_bufflen;
1552 sg_addr = cmd->cmd_dma;
1553 while (sg_len) {
1554 sg_element[sg_count].segment_base_lo = sg_addr;
1555 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1556 sg_element[sg_count].length_segment_base_hi =
1557 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1558 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1559 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1560 } else {
1561 sg_element[sg_count].length_segment_base_hi =
1562 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1563 sg_len = 0;
1565 sg_count++;
1568 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1570 sbp2util_cpu_to_be32_buffer(sg_element,
1571 (sizeof(struct sbp2_unrestricted_page_table)) *
1572 sg_count);
1576 static void sbp2_create_command_orb(struct sbp2_lu *lu,
1577 struct sbp2_command_info *cmd,
1578 unchar *scsi_cmd,
1579 unsigned int scsi_use_sg,
1580 unsigned int scsi_request_bufflen,
1581 void *scsi_request_buffer,
1582 enum dma_data_direction dma_dir)
1584 struct sbp2_fwhost_info *hi = lu->hi;
1585 struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
1586 struct sbp2_command_orb *orb = &cmd->command_orb;
1587 u32 orb_direction;
1590 * Set-up our command ORB.
1592 * NOTE: We're doing unrestricted page tables (s/g), as this is
1593 * best performance (at least with the devices I have). This means
1594 * that data_size becomes the number of s/g elements, and
1595 * page_size should be zero (for unrestricted).
1597 orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1598 orb->next_ORB_lo = 0x0;
1599 orb->misc = ORB_SET_MAX_PAYLOAD(lu->max_payload_size);
1600 orb->misc |= ORB_SET_SPEED(lu->speed_code);
1601 orb->misc |= ORB_SET_NOTIFY(1);
1603 if (dma_dir == DMA_NONE)
1604 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1605 else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
1606 orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
1607 else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
1608 orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
1609 else {
1610 SBP2_INFO("Falling back to DMA_NONE");
1611 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1614 /* set up our page table stuff */
1615 if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
1616 orb->data_descriptor_hi = 0x0;
1617 orb->data_descriptor_lo = 0x0;
1618 orb->misc |= ORB_SET_DIRECTION(1);
1619 } else if (scsi_use_sg)
1620 sbp2_prep_command_orb_sg(orb, hi, cmd, scsi_use_sg, sgpnt,
1621 orb_direction, dma_dir);
1622 else
1623 sbp2_prep_command_orb_no_sg(orb, hi, cmd, sgpnt, orb_direction,
1624 scsi_request_bufflen,
1625 scsi_request_buffer, dma_dir);
1627 sbp2util_cpu_to_be32_buffer(orb, sizeof(*orb));
1629 memset(orb->cdb, 0, 12);
1630 memcpy(orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
1633 static void sbp2_link_orb_command(struct sbp2_lu *lu,
1634 struct sbp2_command_info *cmd)
1636 struct sbp2_fwhost_info *hi = lu->hi;
1637 struct sbp2_command_orb *last_orb;
1638 dma_addr_t last_orb_dma;
1639 u64 addr = lu->command_block_agent_addr;
1640 quadlet_t data[2];
1641 size_t length;
1642 unsigned long flags;
1644 dma_sync_single_for_device(hi->host->device.parent,
1645 cmd->command_orb_dma,
1646 sizeof(struct sbp2_command_orb),
1647 DMA_TO_DEVICE);
1648 dma_sync_single_for_device(hi->host->device.parent, cmd->sge_dma,
1649 sizeof(cmd->scatter_gather_element),
1650 DMA_TO_DEVICE);
1652 /* check to see if there are any previous orbs to use */
1653 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1654 last_orb = lu->last_orb;
1655 last_orb_dma = lu->last_orb_dma;
1656 if (!last_orb) {
1658 * last_orb == NULL means: We know that the target's fetch agent
1659 * is not active right now.
1661 addr += SBP2_ORB_POINTER_OFFSET;
1662 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1663 data[1] = cmd->command_orb_dma;
1664 sbp2util_cpu_to_be32_buffer(data, 8);
1665 length = 8;
1666 } else {
1668 * last_orb != NULL means: We know that the target's fetch agent
1669 * is (very probably) not dead or in reset state right now.
1670 * We have an ORB already sent that we can append a new one to.
1671 * The target's fetch agent may or may not have read this
1672 * previous ORB yet.
1674 dma_sync_single_for_cpu(hi->host->device.parent, last_orb_dma,
1675 sizeof(struct sbp2_command_orb),
1676 DMA_TO_DEVICE);
1677 last_orb->next_ORB_lo = cpu_to_be32(cmd->command_orb_dma);
1678 wmb();
1679 /* Tells hardware that this pointer is valid */
1680 last_orb->next_ORB_hi = 0;
1681 dma_sync_single_for_device(hi->host->device.parent,
1682 last_orb_dma,
1683 sizeof(struct sbp2_command_orb),
1684 DMA_TO_DEVICE);
1685 addr += SBP2_DOORBELL_OFFSET;
1686 data[0] = 0;
1687 length = 4;
1689 lu->last_orb = &cmd->command_orb;
1690 lu->last_orb_dma = cmd->command_orb_dma;
1691 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1693 if (sbp2util_node_write_no_wait(lu->ne, addr, data, length)) {
1695 * sbp2util_node_write_no_wait failed. We certainly ran out
1696 * of transaction labels, perhaps just because there were no
1697 * context switches which gave khpsbpkt a chance to collect
1698 * free tlabels. Try again in non-atomic context. If necessary,
1699 * the workqueue job will sleep to guaranteedly get a tlabel.
1700 * We do not accept new commands until the job is over.
1702 scsi_block_requests(lu->shost);
1703 PREPARE_WORK(&lu->protocol_work,
1704 last_orb ? sbp2util_write_doorbell:
1705 sbp2util_write_orb_pointer);
1706 schedule_work(&lu->protocol_work);
1710 static int sbp2_send_command(struct sbp2_lu *lu, struct scsi_cmnd *SCpnt,
1711 void (*done)(struct scsi_cmnd *))
1713 unchar *scsi_cmd = (unchar *)SCpnt->cmnd;
1714 unsigned int request_bufflen = SCpnt->request_bufflen;
1715 struct sbp2_command_info *cmd;
1717 cmd = sbp2util_allocate_command_orb(lu, SCpnt, done);
1718 if (!cmd)
1719 return -EIO;
1721 sbp2_create_command_orb(lu, cmd, scsi_cmd, SCpnt->use_sg,
1722 request_bufflen, SCpnt->request_buffer,
1723 SCpnt->sc_data_direction);
1724 sbp2_link_orb_command(lu, cmd);
1726 return 0;
1730 * Translates SBP-2 status into SCSI sense data for check conditions
1732 static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status,
1733 unchar *sense_data)
1735 /* OK, it's pretty ugly... ;-) */
1736 sense_data[0] = 0x70;
1737 sense_data[1] = 0x0;
1738 sense_data[2] = sbp2_status[9];
1739 sense_data[3] = sbp2_status[12];
1740 sense_data[4] = sbp2_status[13];
1741 sense_data[5] = sbp2_status[14];
1742 sense_data[6] = sbp2_status[15];
1743 sense_data[7] = 10;
1744 sense_data[8] = sbp2_status[16];
1745 sense_data[9] = sbp2_status[17];
1746 sense_data[10] = sbp2_status[18];
1747 sense_data[11] = sbp2_status[19];
1748 sense_data[12] = sbp2_status[10];
1749 sense_data[13] = sbp2_status[11];
1750 sense_data[14] = sbp2_status[20];
1751 sense_data[15] = sbp2_status[21];
1753 return sbp2_status[8] & 0x3f;
1756 static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
1757 int destid, quadlet_t *data, u64 addr,
1758 size_t length, u16 fl)
1760 struct sbp2_fwhost_info *hi;
1761 struct sbp2_lu *lu = NULL, *lu_tmp;
1762 struct scsi_cmnd *SCpnt = NULL;
1763 struct sbp2_status_block *sb;
1764 u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
1765 struct sbp2_command_info *cmd;
1766 unsigned long flags;
1768 if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
1769 SBP2_ERR("Wrong size of status block");
1770 return RCODE_ADDRESS_ERROR;
1772 if (unlikely(!host)) {
1773 SBP2_ERR("host is NULL - this is bad!");
1774 return RCODE_ADDRESS_ERROR;
1776 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
1777 if (unlikely(!hi)) {
1778 SBP2_ERR("host info is NULL - this is bad!");
1779 return RCODE_ADDRESS_ERROR;
1782 /* Find the unit which wrote the status. */
1783 list_for_each_entry(lu_tmp, &hi->logical_units, lu_list) {
1784 if (lu_tmp->ne->nodeid == nodeid &&
1785 lu_tmp->status_fifo_addr == addr) {
1786 lu = lu_tmp;
1787 break;
1790 if (unlikely(!lu)) {
1791 SBP2_ERR("lu is NULL - device is gone?");
1792 return RCODE_ADDRESS_ERROR;
1795 /* Put response into lu status fifo buffer. The first two bytes
1796 * come in big endian bit order. Often the target writes only a
1797 * truncated status block, minimally the first two quadlets. The rest
1798 * is implied to be zeros. */
1799 sb = &lu->status_block;
1800 memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
1801 memcpy(sb, data, length);
1802 sbp2util_be32_to_cpu_buffer(sb, 8);
1804 /* Ignore unsolicited status. Handle command ORB status. */
1805 if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
1806 cmd = NULL;
1807 else
1808 cmd = sbp2util_find_command_for_orb(lu, sb->ORB_offset_lo);
1809 if (cmd) {
1810 dma_sync_single_for_cpu(hi->host->device.parent,
1811 cmd->command_orb_dma,
1812 sizeof(struct sbp2_command_orb),
1813 DMA_TO_DEVICE);
1814 dma_sync_single_for_cpu(hi->host->device.parent, cmd->sge_dma,
1815 sizeof(cmd->scatter_gather_element),
1816 DMA_TO_DEVICE);
1817 /* Grab SCSI command pointers and check status. */
1819 * FIXME: If the src field in the status is 1, the ORB DMA must
1820 * not be reused until status for a subsequent ORB is received.
1822 SCpnt = cmd->Current_SCpnt;
1823 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1824 sbp2util_mark_command_completed(lu, cmd);
1825 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1827 if (SCpnt) {
1828 u32 h = sb->ORB_offset_hi_misc;
1829 u32 r = STATUS_GET_RESP(h);
1831 if (r != RESP_STATUS_REQUEST_COMPLETE) {
1832 SBP2_INFO("resp 0x%x, sbp_status 0x%x",
1833 r, STATUS_GET_SBP_STATUS(h));
1834 scsi_status =
1835 r == RESP_STATUS_TRANSPORT_FAILURE ?
1836 SBP2_SCSI_STATUS_BUSY :
1837 SBP2_SCSI_STATUS_COMMAND_TERMINATED;
1840 if (STATUS_GET_LEN(h) > 1)
1841 scsi_status = sbp2_status_to_sense_data(
1842 (unchar *)sb, SCpnt->sense_buffer);
1844 if (STATUS_TEST_DEAD(h))
1845 sbp2_agent_reset(lu, 0);
1848 /* Check here to see if there are no commands in-use. If there
1849 * are none, we know that the fetch agent left the active state
1850 * _and_ that we did not reactivate it yet. Therefore clear
1851 * last_orb so that next time we write directly to the
1852 * ORB_POINTER register. That way the fetch agent does not need
1853 * to refetch the next_ORB. */
1854 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1855 if (list_empty(&lu->cmd_orb_inuse))
1856 lu->last_orb = NULL;
1857 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1859 } else {
1860 /* It's probably status after a management request. */
1861 if ((sb->ORB_offset_lo == lu->reconnect_orb_dma) ||
1862 (sb->ORB_offset_lo == lu->login_orb_dma) ||
1863 (sb->ORB_offset_lo == lu->query_logins_orb_dma) ||
1864 (sb->ORB_offset_lo == lu->logout_orb_dma)) {
1865 lu->access_complete = 1;
1866 wake_up_interruptible(&sbp2_access_wq);
1870 if (SCpnt)
1871 sbp2scsi_complete_command(lu, scsi_status, SCpnt,
1872 cmd->Current_done);
1873 return RCODE_COMPLETE;
1876 /**************************************
1877 * SCSI interface related section
1878 **************************************/
1880 static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
1881 void (*done)(struct scsi_cmnd *))
1883 struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
1884 struct sbp2_fwhost_info *hi;
1885 int result = DID_NO_CONNECT << 16;
1887 if (unlikely(!sbp2util_node_is_available(lu)))
1888 goto done;
1890 hi = lu->hi;
1892 if (unlikely(!hi)) {
1893 SBP2_ERR("sbp2_fwhost_info is NULL - this is bad!");
1894 goto done;
1897 /* Multiple units are currently represented to the SCSI core as separate
1898 * targets, not as one target with multiple LUs. Therefore return
1899 * selection time-out to any IO directed at non-zero LUNs. */
1900 if (unlikely(SCpnt->device->lun))
1901 goto done;
1903 if (unlikely(!hpsb_node_entry_valid(lu->ne))) {
1904 SBP2_ERR("Bus reset in progress - rejecting command");
1905 result = DID_BUS_BUSY << 16;
1906 goto done;
1909 /* Bidirectional commands are not yet implemented,
1910 * and unknown transfer direction not handled. */
1911 if (unlikely(SCpnt->sc_data_direction == DMA_BIDIRECTIONAL)) {
1912 SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
1913 result = DID_ERROR << 16;
1914 goto done;
1917 if (sbp2_send_command(lu, SCpnt, done)) {
1918 SBP2_ERR("Error sending SCSI command");
1919 sbp2scsi_complete_command(lu,
1920 SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
1921 SCpnt, done);
1923 return 0;
1925 done:
1926 SCpnt->result = result;
1927 done(SCpnt);
1928 return 0;
1931 static void sbp2scsi_complete_all_commands(struct sbp2_lu *lu, u32 status)
1933 struct sbp2_fwhost_info *hi = lu->hi;
1934 struct list_head *lh;
1935 struct sbp2_command_info *cmd;
1936 unsigned long flags;
1938 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1939 while (!list_empty(&lu->cmd_orb_inuse)) {
1940 lh = lu->cmd_orb_inuse.next;
1941 cmd = list_entry(lh, struct sbp2_command_info, list);
1942 dma_sync_single_for_cpu(hi->host->device.parent,
1943 cmd->command_orb_dma,
1944 sizeof(struct sbp2_command_orb),
1945 DMA_TO_DEVICE);
1946 dma_sync_single_for_cpu(hi->host->device.parent, cmd->sge_dma,
1947 sizeof(cmd->scatter_gather_element),
1948 DMA_TO_DEVICE);
1949 sbp2util_mark_command_completed(lu, cmd);
1950 if (cmd->Current_SCpnt) {
1951 cmd->Current_SCpnt->result = status << 16;
1952 cmd->Current_done(cmd->Current_SCpnt);
1955 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1957 return;
1961 * Complete a regular SCSI command. Can be called in atomic context.
1963 static void sbp2scsi_complete_command(struct sbp2_lu *lu, u32 scsi_status,
1964 struct scsi_cmnd *SCpnt,
1965 void (*done)(struct scsi_cmnd *))
1967 if (!SCpnt) {
1968 SBP2_ERR("SCpnt is NULL");
1969 return;
1972 switch (scsi_status) {
1973 case SBP2_SCSI_STATUS_GOOD:
1974 SCpnt->result = DID_OK << 16;
1975 break;
1977 case SBP2_SCSI_STATUS_BUSY:
1978 SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
1979 SCpnt->result = DID_BUS_BUSY << 16;
1980 break;
1982 case SBP2_SCSI_STATUS_CHECK_CONDITION:
1983 SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
1984 break;
1986 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
1987 SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
1988 SCpnt->result = DID_NO_CONNECT << 16;
1989 scsi_print_command(SCpnt);
1990 break;
1992 case SBP2_SCSI_STATUS_CONDITION_MET:
1993 case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
1994 case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
1995 SBP2_ERR("Bad SCSI status = %x", scsi_status);
1996 SCpnt->result = DID_ERROR << 16;
1997 scsi_print_command(SCpnt);
1998 break;
2000 default:
2001 SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
2002 SCpnt->result = DID_ERROR << 16;
2005 /* If a bus reset is in progress and there was an error, complete
2006 * the command as busy so that it will get retried. */
2007 if (!hpsb_node_entry_valid(lu->ne)
2008 && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
2009 SBP2_ERR("Completing command with busy (bus reset)");
2010 SCpnt->result = DID_BUS_BUSY << 16;
2013 /* Tell the SCSI stack that we're done with this command. */
2014 done(SCpnt);
2017 static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
2019 struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
2021 lu->sdev = sdev;
2022 sdev->allow_restart = 1;
2024 if (lu->workarounds & SBP2_WORKAROUND_INQUIRY_36)
2025 sdev->inquiry_len = 36;
2026 return 0;
2029 static int sbp2scsi_slave_configure(struct scsi_device *sdev)
2031 struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
2033 sdev->use_10_for_rw = 1;
2035 if (sdev->type == TYPE_ROM)
2036 sdev->use_10_for_ms = 1;
2037 if (sdev->type == TYPE_DISK &&
2038 lu->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
2039 sdev->skip_ms_page_8 = 1;
2040 if (lu->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
2041 sdev->fix_capacity = 1;
2042 return 0;
2045 static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
2047 ((struct sbp2_lu *)sdev->host->hostdata[0])->sdev = NULL;
2048 return;
2052 * Called by scsi stack when something has really gone wrong.
2053 * Usually called when a command has timed-out for some reason.
2055 static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
2057 struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
2058 struct sbp2_fwhost_info *hi = lu->hi;
2059 struct sbp2_command_info *cmd;
2060 unsigned long flags;
2062 SBP2_INFO("aborting sbp2 command");
2063 scsi_print_command(SCpnt);
2065 if (sbp2util_node_is_available(lu)) {
2066 sbp2_agent_reset(lu, 1);
2068 /* Return a matching command structure to the free pool. */
2069 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
2070 cmd = sbp2util_find_command_for_SCpnt(lu, SCpnt);
2071 if (cmd) {
2072 dma_sync_single_for_cpu(hi->host->device.parent,
2073 cmd->command_orb_dma,
2074 sizeof(struct sbp2_command_orb),
2075 DMA_TO_DEVICE);
2076 dma_sync_single_for_cpu(hi->host->device.parent,
2077 cmd->sge_dma,
2078 sizeof(cmd->scatter_gather_element),
2079 DMA_TO_DEVICE);
2080 sbp2util_mark_command_completed(lu, cmd);
2081 if (cmd->Current_SCpnt) {
2082 cmd->Current_SCpnt->result = DID_ABORT << 16;
2083 cmd->Current_done(cmd->Current_SCpnt);
2086 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
2088 sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
2091 return SUCCESS;
2095 * Called by scsi stack when something has really gone wrong.
2097 static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
2099 struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
2101 SBP2_INFO("reset requested");
2103 if (sbp2util_node_is_available(lu)) {
2104 SBP2_INFO("generating sbp2 fetch agent reset");
2105 sbp2_agent_reset(lu, 1);
2108 return SUCCESS;
2111 static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
2112 struct device_attribute *attr,
2113 char *buf)
2115 struct scsi_device *sdev;
2116 struct sbp2_lu *lu;
2118 if (!(sdev = to_scsi_device(dev)))
2119 return 0;
2121 if (!(lu = (struct sbp2_lu *)sdev->host->hostdata[0]))
2122 return 0;
2124 if (sbp2_long_sysfs_ieee1394_id)
2125 return sprintf(buf, "%016Lx:%06x:%04x\n",
2126 (unsigned long long)lu->ne->guid,
2127 lu->ud->directory_id, ORB_SET_LUN(lu->lun));
2128 else
2129 return sprintf(buf, "%016Lx:%d:%d\n",
2130 (unsigned long long)lu->ne->guid,
2131 lu->ud->id, ORB_SET_LUN(lu->lun));
2134 MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2135 MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2136 MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2137 MODULE_LICENSE("GPL");
2139 static int sbp2_module_init(void)
2141 int ret;
2143 if (sbp2_serialize_io) {
2144 sbp2_shost_template.can_queue = 1;
2145 sbp2_shost_template.cmd_per_lun = 1;
2148 if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
2149 (sbp2_max_sectors * 512) > (128 * 1024))
2150 sbp2_max_sectors = 128 * 1024 / 512;
2151 sbp2_shost_template.max_sectors = sbp2_max_sectors;
2153 hpsb_register_highlevel(&sbp2_highlevel);
2154 ret = hpsb_register_protocol(&sbp2_driver);
2155 if (ret) {
2156 SBP2_ERR("Failed to register protocol");
2157 hpsb_unregister_highlevel(&sbp2_highlevel);
2158 return ret;
2160 return 0;
2163 static void __exit sbp2_module_exit(void)
2165 hpsb_unregister_protocol(&sbp2_driver);
2166 hpsb_unregister_highlevel(&sbp2_highlevel);
2169 module_init(sbp2_module_init);
2170 module_exit(sbp2_module_exit);