ieee1394: remove unused code
[linux-2.6-block.git] / drivers / ieee1394 / sbp2.c
CommitLineData
1da177e4
LT
1/*
2 * sbp2.c - SBP-2 protocol driver for IEEE-1394
3 *
4 * Copyright (C) 2000 James Goodwin, Filanet Corporation (www.filanet.com)
5 * jamesg@filanet.com (JSG)
6 *
7 * Copyright (C) 2003 Ben Collins <bcollins@debian.org>
8 *
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.
13 *
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.
18 *
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.
22 */
23
24/*
25 * Brief Description:
26 *
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.
31 *
2a533b17
SR
32 * You may access any attached SBP-2 (usually storage devices) as regular
33 * SCSI devices. E.g. mount /dev/sda1, fdisk, mkfs, etc..
1da177e4 34 *
2a533b17
SR
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.
1da177e4 37 *
2a533b17
SR
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
2a533b17 51 * Grep for inline FIXME comments below.
1da177e4
LT
52 */
53
902abed1
SR
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>
1da177e4
LT
60#include <linux/kernel.h>
61#include <linux/list.h>
f84c922b 62#include <linux/mm.h>
1da177e4
LT
63#include <linux/module.h>
64#include <linux/moduleparam.h>
f84c922b 65#include <linux/sched.h>
902abed1
SR
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>
e8398bb7 72#include <linux/wait.h>
20e2008e 73#include <linux/workqueue.h>
87ae9afd 74#include <linux/scatterlist.h>
1da177e4 75
1da177e4 76#include <asm/byteorder.h>
902abed1
SR
77#include <asm/errno.h>
78#include <asm/param.h>
902abed1
SR
79#include <asm/system.h>
80#include <asm/types.h>
81
82#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
83#include <asm/io.h> /* for bus_to_virt */
84#endif
1da177e4
LT
85
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>
91
92#include "csr1212.h"
902abed1
SR
93#include "highlevel.h"
94#include "hosts.h"
1da177e4 95#include "ieee1394.h"
1da177e4 96#include "ieee1394_core.h"
902abed1 97#include "ieee1394_hotplug.h"
1da177e4 98#include "ieee1394_transactions.h"
902abed1
SR
99#include "ieee1394_types.h"
100#include "nodemgr.h"
1da177e4
LT
101#include "sbp2.h"
102
1da177e4
LT
103/*
104 * Module load parameter definitions
105 */
106
107/*
108 * Change max_speed on module load if you have a bad IEEE-1394
109 * controller that has trouble running 2KB packets at 400mb.
110 *
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.
114 */
ca0c7453
SR
115static int sbp2_max_speed = IEEE1394_SPEED_MAX;
116module_param_named(max_speed, sbp2_max_speed, int, 0644);
28b06679
SR
117MODULE_PARM_DESC(max_speed, "Force max speed "
118 "(3 = 800Mb/s, 2 = 400Mb/s, 1 = 200Mb/s, 0 = 100Mb/s)");
1da177e4
LT
119
120/*
77bba7ae
SR
121 * Set serialize_io to 0 or N to use dynamically appended lists of command ORBs.
122 * This is and always has been buggy in multiple subtle ways. See above TODOs.
1da177e4 123 */
ca0c7453 124static int sbp2_serialize_io = 1;
77bba7ae
SR
125module_param_named(serialize_io, sbp2_serialize_io, bool, 0444);
126MODULE_PARM_DESC(serialize_io, "Serialize requests coming from SCSI drivers "
127 "(default = Y, faster but buggy = N)");
1da177e4
LT
128
129/*
130 * Bump up max_sectors if you'd like to support very large sized
131 * transfers. Please note that some older sbp2 bridge chips are broken for
132 * transfers greater or equal to 128KB. Default is a value of 255
133 * sectors, or just under 128KB (at 512 byte sector size). I can note that
134 * the Oxsemi sbp2 chipsets have no problems supporting very large
135 * transfer sizes.
136 */
ca0c7453
SR
137static int sbp2_max_sectors = SBP2_MAX_SECTORS;
138module_param_named(max_sectors, sbp2_max_sectors, int, 0444);
139MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported "
140 "(default = " __stringify(SBP2_MAX_SECTORS) ")");
1da177e4
LT
141
142/*
143 * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
144 * do an exclusive login, as it's generally unsafe to have two hosts
145 * talking to a single sbp2 device at the same time (filesystem coherency,
146 * etc.). If you're running an sbp2 device that supports multiple logins,
147 * and you're either running read-only filesystems or some sort of special
20f45781
BC
148 * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster
149 * File System, or Lustre, then set exclusive_login to zero.
150 *
151 * So far only bridges from Oxford Semiconductor are known to support
152 * concurrent logins. Depending on firmware, four or two concurrent logins
153 * are possible on OXFW911 and newer Oxsemi bridges.
1da177e4 154 */
ca0c7453 155static int sbp2_exclusive_login = 1;
77bba7ae 156module_param_named(exclusive_login, sbp2_exclusive_login, bool, 0644);
ca0c7453 157MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
77bba7ae 158 "(default = Y, use N for concurrent initiators)");
1da177e4
LT
159
160/*
24d3bf88
SR
161 * If any of the following workarounds is required for your device to work,
162 * please submit the kernel messages logged by sbp2 to the linux1394-devel
163 * mailing list.
1da177e4 164 *
24d3bf88
SR
165 * - 128kB max transfer
166 * Limit transfer size. Necessary for some old bridges.
167 *
168 * - 36 byte inquiry
169 * When scsi_mod probes the device, let the inquiry command look like that
170 * from MS Windows.
171 *
172 * - skip mode page 8
173 * Suppress sending of mode_sense for mode page 8 if the device pretends to
174 * support the SCSI Primary Block commands instead of Reduced Block Commands.
e9a1c52c
SR
175 *
176 * - fix capacity
177 * Tell sd_mod to correct the last sector number reported by read_capacity.
178 * Avoids access beyond actual disk limits on devices with an off-by-one bug.
179 * Don't use this with devices which don't have this bug.
679c0cd2
SR
180 *
181 * - override internal blacklist
182 * Instead of adding to the built-in blacklist, use only the workarounds
183 * specified in the module load parameter.
184 * Useful if a blacklist entry interfered with a non-broken device.
1da177e4 185 */
24d3bf88
SR
186static int sbp2_default_workarounds;
187module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
188MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
189 ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
190 ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
191 ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
e9a1c52c 192 ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
679c0cd2 193 ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
24d3bf88
SR
194 ", or a combination)");
195
d7794c86
SR
196/*
197 * This influences the format of the sysfs attribute
198 * /sys/bus/scsi/devices/.../ieee1394_id.
199 *
200 * The default format is like in older kernels: %016Lx:%d:%d
201 * It contains the target's EUI-64, a number given to the logical unit by
202 * the ieee1394 driver's nodemgr (starting at 0), and the LUN.
203 *
204 * The long format is: %016Lx:%06x:%04x
205 * It contains the target's EUI-64, the unit directory's directory_ID as per
206 * IEEE 1212 clause 7.7.19, and the LUN. This format comes closest to the
207 * format of SBP(-3) target port and logical unit identifier as per SAM (SCSI
208 * Architecture Model) rev.2 to 4 annex A. Therefore and because it is
209 * independent of the implementation of the ieee1394 nodemgr, the longer format
210 * is recommended for future use.
211 */
212static int sbp2_long_sysfs_ieee1394_id;
213module_param_named(long_ieee1394_id, sbp2_long_sysfs_ieee1394_id, bool, 0644);
214MODULE_PARM_DESC(long_ieee1394_id, "8+3+2 bytes format of ieee1394_id in sysfs "
215 "(default = backwards-compatible = N, SAM-conforming = Y)");
216
1da177e4 217
edf1fb21
SR
218#define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
219#define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
1da177e4 220
1da177e4
LT
221/*
222 * Globals
223 */
138c8af8
SR
224static void sbp2scsi_complete_all_commands(struct sbp2_lu *, u32);
225static void sbp2scsi_complete_command(struct sbp2_lu *, u32, struct scsi_cmnd *,
ea42ea0f 226 void (*)(struct scsi_cmnd *));
138c8af8
SR
227static struct sbp2_lu *sbp2_alloc_device(struct unit_directory *);
228static int sbp2_start_device(struct sbp2_lu *);
229static void sbp2_remove_device(struct sbp2_lu *);
230static int sbp2_login_device(struct sbp2_lu *);
231static int sbp2_reconnect_device(struct sbp2_lu *);
232static int sbp2_logout_device(struct sbp2_lu *);
ea42ea0f
SR
233static void sbp2_host_reset(struct hpsb_host *);
234static int sbp2_handle_status_write(struct hpsb_host *, int, int, quadlet_t *,
235 u64, size_t, u16);
138c8af8
SR
236static int sbp2_agent_reset(struct sbp2_lu *, int);
237static void sbp2_parse_unit_directory(struct sbp2_lu *,
ea42ea0f 238 struct unit_directory *);
138c8af8
SR
239static int sbp2_set_busy_timeout(struct sbp2_lu *);
240static int sbp2_max_speed_and_size(struct sbp2_lu *);
1da177e4 241
1da177e4
LT
242
243static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
244
261b5f66
SR
245static DEFINE_RWLOCK(sbp2_hi_logical_units_lock);
246
1da177e4 247static struct hpsb_highlevel sbp2_highlevel = {
ea42ea0f
SR
248 .name = SBP2_DEVICE_NAME,
249 .host_reset = sbp2_host_reset,
1da177e4
LT
250};
251
252static struct hpsb_address_ops sbp2_ops = {
ea42ea0f 253 .write = sbp2_handle_status_write
1da177e4
LT
254};
255
256#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
ea42ea0f
SR
257static int sbp2_handle_physdma_write(struct hpsb_host *, int, int, quadlet_t *,
258 u64, size_t, u16);
259static int sbp2_handle_physdma_read(struct hpsb_host *, int, quadlet_t *, u64,
260 size_t, u16);
261
1da177e4 262static struct hpsb_address_ops sbp2_physdma_ops = {
ea42ea0f
SR
263 .read = sbp2_handle_physdma_read,
264 .write = sbp2_handle_physdma_write,
1da177e4
LT
265};
266#endif
267
ea42ea0f
SR
268
269/*
270 * Interface to driver core and IEEE 1394 core
271 */
272static struct ieee1394_device_id sbp2_id_table[] = {
273 {
274 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
275 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
276 .version = SBP2_SW_VERSION_ENTRY & 0xffffff},
277 {}
278};
279MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
280
281static int sbp2_probe(struct device *);
282static int sbp2_remove(struct device *);
283static int sbp2_update(struct unit_directory *);
284
1da177e4 285static struct hpsb_protocol_driver sbp2_driver = {
ed30c26e 286 .name = SBP2_DEVICE_NAME,
1da177e4
LT
287 .id_table = sbp2_id_table,
288 .update = sbp2_update,
289 .driver = {
1da177e4
LT
290 .probe = sbp2_probe,
291 .remove = sbp2_remove,
292 },
293};
294
ea42ea0f
SR
295
296/*
297 * Interface to SCSI core
298 */
299static int sbp2scsi_queuecommand(struct scsi_cmnd *,
300 void (*)(struct scsi_cmnd *));
301static int sbp2scsi_abort(struct scsi_cmnd *);
302static int sbp2scsi_reset(struct scsi_cmnd *);
303static int sbp2scsi_slave_alloc(struct scsi_device *);
304static int sbp2scsi_slave_configure(struct scsi_device *);
305static void sbp2scsi_slave_destroy(struct scsi_device *);
306static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *,
307 struct device_attribute *, char *);
308
309static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
310
311static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
312 &dev_attr_ieee1394_id,
313 NULL
314};
315
ca0c7453 316static struct scsi_host_template sbp2_shost_template = {
ea42ea0f
SR
317 .module = THIS_MODULE,
318 .name = "SBP-2 IEEE-1394",
319 .proc_name = SBP2_DEVICE_NAME,
320 .queuecommand = sbp2scsi_queuecommand,
321 .eh_abort_handler = sbp2scsi_abort,
322 .eh_device_reset_handler = sbp2scsi_reset,
323 .slave_alloc = sbp2scsi_slave_alloc,
324 .slave_configure = sbp2scsi_slave_configure,
325 .slave_destroy = sbp2scsi_slave_destroy,
326 .this_id = -1,
327 .sg_tablesize = SG_ALL,
328 .use_clustering = ENABLE_CLUSTERING,
329 .cmd_per_lun = SBP2_MAX_CMDS,
330 .can_queue = SBP2_MAX_CMDS,
ea42ea0f
SR
331 .sdev_attrs = sbp2_sysfs_sdev_attrs,
332};
333
4618fd30
SR
334/* for match-all entries in sbp2_workarounds_table */
335#define SBP2_ROM_VALUE_WILDCARD 0x1000000
ea42ea0f 336
a80614d1 337/*
24d3bf88
SR
338 * List of devices with known bugs.
339 *
340 * The firmware_revision field, masked with 0xffff00, is the best indicator
341 * for the type of bridge chip of a device. It yields a few false positives
342 * but this did not break correctly behaving devices so far.
a80614d1 343 */
24d3bf88
SR
344static const struct {
345 u32 firmware_revision;
e9a1c52c 346 u32 model_id;
24d3bf88
SR
347 unsigned workarounds;
348} sbp2_workarounds_table[] = {
4b9a3347 349 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
24d3bf88 350 .firmware_revision = 0x002800,
4b9a3347 351 .model_id = 0x001010,
24d3bf88
SR
352 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
353 SBP2_WORKAROUND_MODE_SENSE_8,
354 },
355 /* Initio bridges, actually only needed for some older ones */ {
356 .firmware_revision = 0x000200,
4618fd30 357 .model_id = SBP2_ROM_VALUE_WILDCARD,
24d3bf88
SR
358 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
359 },
360 /* Symbios bridge */ {
361 .firmware_revision = 0xa0b800,
4618fd30 362 .model_id = SBP2_ROM_VALUE_WILDCARD,
24d3bf88 363 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
e9a1c52c 364 },
e9a1c52c
SR
365 /* iPod 4th generation */ {
366 .firmware_revision = 0x0a2700,
367 .model_id = 0x000021,
368 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
369 },
370 /* iPod mini */ {
371 .firmware_revision = 0x0a2700,
372 .model_id = 0x000023,
373 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
374 },
375 /* iPod Photo */ {
376 .firmware_revision = 0x0a2700,
377 .model_id = 0x00007e,
378 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
24d3bf88 379 }
1da177e4
LT
380};
381
1da177e4
LT
382/**************************************
383 * General utility functions
384 **************************************/
385
1da177e4
LT
386#ifndef __BIG_ENDIAN
387/*
388 * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
389 */
2b01b80b 390static inline void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
1da177e4
LT
391{
392 u32 *temp = buffer;
393
394 for (length = (length >> 2); length--; )
395 temp[length] = be32_to_cpu(temp[length]);
1da177e4
LT
396}
397
398/*
399 * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
400 */
2b01b80b 401static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
1da177e4
LT
402{
403 u32 *temp = buffer;
404
405 for (length = (length >> 2); length--; )
406 temp[length] = cpu_to_be32(temp[length]);
1da177e4
LT
407}
408#else /* BIG_ENDIAN */
409/* Why waste the cpu cycles? */
611aa19f
SR
410#define sbp2util_be32_to_cpu_buffer(x,y) do {} while (0)
411#define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0)
1da177e4
LT
412#endif
413
ca0c7453 414static DECLARE_WAIT_QUEUE_HEAD(sbp2_access_wq);
e8398bb7 415
1da177e4 416/*
e8398bb7
SR
417 * Waits for completion of an SBP-2 access request.
418 * Returns nonzero if timed out or prematurely interrupted.
1da177e4 419 */
138c8af8 420static int sbp2util_access_timeout(struct sbp2_lu *lu, int timeout)
1da177e4 421{
ca0c7453 422 long leftover;
1da177e4 423
ca0c7453 424 leftover = wait_event_interruptible_timeout(
138c8af8
SR
425 sbp2_access_wq, lu->access_complete, timeout);
426 lu->access_complete = 0;
e8398bb7 427 return leftover <= 0;
1da177e4
LT
428}
429
138c8af8 430static void sbp2_free_packet(void *packet)
1da177e4
LT
431{
432 hpsb_free_tlabel(packet);
433 hpsb_free_packet(packet);
434}
435
e8ca5668
SR
436/*
437 * This is much like hpsb_node_write(), except it ignores the response
438 * subaction and returns immediately. Can be used from atomic context.
1da177e4
LT
439 */
440static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
138c8af8 441 quadlet_t *buf, size_t len)
1da177e4
LT
442{
443 struct hpsb_packet *packet;
444
138c8af8 445 packet = hpsb_make_writepacket(ne->host, ne->nodeid, addr, buf, len);
a237f35f
SR
446 if (!packet)
447 return -ENOMEM;
1da177e4 448
138c8af8 449 hpsb_set_packet_complete_task(packet, sbp2_free_packet, packet);
1da177e4 450 hpsb_node_fill_packet(ne, packet);
a237f35f 451 if (hpsb_send_packet(packet) < 0) {
1da177e4
LT
452 sbp2_free_packet(packet);
453 return -EIO;
454 }
1da177e4
LT
455 return 0;
456}
457
138c8af8
SR
458static void sbp2util_notify_fetch_agent(struct sbp2_lu *lu, u64 offset,
459 quadlet_t *data, size_t len)
09ee67ab 460{
138c8af8
SR
461 /* There is a small window after a bus reset within which the node
462 * entry's generation is current but the reconnect wasn't completed. */
463 if (unlikely(atomic_read(&lu->state) == SBP2LU_STATE_IN_RESET))
09ee67ab
SR
464 return;
465
138c8af8 466 if (hpsb_node_write(lu->ne, lu->command_block_agent_addr + offset,
09ee67ab
SR
467 data, len))
468 SBP2_ERR("sbp2util_notify_fetch_agent failed.");
138c8af8
SR
469
470 /* Now accept new SCSI commands, unless a bus reset happended during
471 * hpsb_node_write. */
472 if (likely(atomic_read(&lu->state) != SBP2LU_STATE_IN_RESET))
473 scsi_unblock_requests(lu->shost);
09ee67ab
SR
474}
475
c4028958 476static void sbp2util_write_orb_pointer(struct work_struct *work)
09ee67ab 477{
ec9b7e10 478 struct sbp2_lu *lu = container_of(work, struct sbp2_lu, protocol_work);
09ee67ab
SR
479 quadlet_t data[2];
480
ec9b7e10
SR
481 data[0] = ORB_SET_NODE_ID(lu->hi->host->node_id);
482 data[1] = lu->last_orb_dma;
09ee67ab 483 sbp2util_cpu_to_be32_buffer(data, 8);
ec9b7e10 484 sbp2util_notify_fetch_agent(lu, SBP2_ORB_POINTER_OFFSET, data, 8);
09ee67ab
SR
485}
486
c4028958 487static void sbp2util_write_doorbell(struct work_struct *work)
09ee67ab 488{
ec9b7e10
SR
489 struct sbp2_lu *lu = container_of(work, struct sbp2_lu, protocol_work);
490
491 sbp2util_notify_fetch_agent(lu, SBP2_DOORBELL_OFFSET, NULL, 4);
09ee67ab
SR
492}
493
138c8af8 494static int sbp2util_create_command_orb_pool(struct sbp2_lu *lu)
1da177e4 495{
138c8af8 496 struct sbp2_fwhost_info *hi = lu->hi;
138c8af8 497 struct sbp2_command_info *cmd;
3d269cb5 498 int i, orbs = sbp2_serialize_io ? 2 : SBP2_MAX_CMDS;
1da177e4 499
1da177e4 500 for (i = 0; i < orbs; i++) {
3d269cb5
SR
501 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
502 if (!cmd)
a237f35f 503 return -ENOMEM;
97d552e3 504 cmd->command_orb_dma = dma_map_single(hi->host->device.parent,
138c8af8
SR
505 &cmd->command_orb,
506 sizeof(struct sbp2_command_orb),
9b7d9c09 507 DMA_TO_DEVICE);
97d552e3 508 cmd->sge_dma = dma_map_single(hi->host->device.parent,
138c8af8
SR
509 &cmd->scatter_gather_element,
510 sizeof(cmd->scatter_gather_element),
2446a79f 511 DMA_TO_DEVICE);
138c8af8
SR
512 INIT_LIST_HEAD(&cmd->list);
513 list_add_tail(&cmd->list, &lu->cmd_orb_completed);
1da177e4 514 }
1da177e4
LT
515 return 0;
516}
517
a2ee3f9b
SR
518static void sbp2util_remove_command_orb_pool(struct sbp2_lu *lu,
519 struct hpsb_host *host)
1da177e4 520{
1da177e4 521 struct list_head *lh, *next;
138c8af8 522 struct sbp2_command_info *cmd;
1da177e4
LT
523 unsigned long flags;
524
138c8af8
SR
525 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
526 if (!list_empty(&lu->cmd_orb_completed))
527 list_for_each_safe(lh, next, &lu->cmd_orb_completed) {
528 cmd = list_entry(lh, struct sbp2_command_info, list);
97d552e3
SR
529 dma_unmap_single(host->device.parent,
530 cmd->command_orb_dma,
1da177e4 531 sizeof(struct sbp2_command_orb),
9b7d9c09 532 DMA_TO_DEVICE);
97d552e3 533 dma_unmap_single(host->device.parent, cmd->sge_dma,
138c8af8 534 sizeof(cmd->scatter_gather_element),
2446a79f 535 DMA_TO_DEVICE);
138c8af8 536 kfree(cmd);
1da177e4 537 }
138c8af8 538 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1da177e4
LT
539 return;
540}
541
542/*
e8ca5668
SR
543 * Finds the sbp2_command for a given outstanding command ORB.
544 * Only looks at the in-use list.
1da177e4
LT
545 */
546static struct sbp2_command_info *sbp2util_find_command_for_orb(
138c8af8 547 struct sbp2_lu *lu, dma_addr_t orb)
1da177e4 548{
138c8af8 549 struct sbp2_command_info *cmd;
1da177e4
LT
550 unsigned long flags;
551
138c8af8
SR
552 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
553 if (!list_empty(&lu->cmd_orb_inuse))
554 list_for_each_entry(cmd, &lu->cmd_orb_inuse, list)
555 if (cmd->command_orb_dma == orb) {
556 spin_unlock_irqrestore(
557 &lu->cmd_orb_lock, flags);
558 return cmd;
1da177e4 559 }
138c8af8 560 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
a237f35f 561 return NULL;
1da177e4
LT
562}
563
564/*
e8ca5668
SR
565 * Finds the sbp2_command for a given outstanding SCpnt.
566 * Only looks at the in-use list.
138c8af8 567 * Must be called with lu->cmd_orb_lock held.
1da177e4 568 */
24c7cd06 569static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
138c8af8 570 struct sbp2_lu *lu, void *SCpnt)
1da177e4 571{
138c8af8 572 struct sbp2_command_info *cmd;
1da177e4 573
138c8af8
SR
574 if (!list_empty(&lu->cmd_orb_inuse))
575 list_for_each_entry(cmd, &lu->cmd_orb_inuse, list)
576 if (cmd->Current_SCpnt == SCpnt)
577 return cmd;
a237f35f 578 return NULL;
1da177e4
LT
579}
580
1da177e4 581static struct sbp2_command_info *sbp2util_allocate_command_orb(
138c8af8
SR
582 struct sbp2_lu *lu,
583 struct scsi_cmnd *Current_SCpnt,
584 void (*Current_done)(struct scsi_cmnd *))
1da177e4
LT
585{
586 struct list_head *lh;
138c8af8 587 struct sbp2_command_info *cmd = NULL;
1da177e4
LT
588 unsigned long flags;
589
138c8af8
SR
590 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
591 if (!list_empty(&lu->cmd_orb_completed)) {
592 lh = lu->cmd_orb_completed.next;
1da177e4 593 list_del(lh);
138c8af8
SR
594 cmd = list_entry(lh, struct sbp2_command_info, list);
595 cmd->Current_done = Current_done;
596 cmd->Current_SCpnt = Current_SCpnt;
597 list_add_tail(&cmd->list, &lu->cmd_orb_inuse);
598 } else
d024ebc6 599 SBP2_ERR("%s: no orbs available", __FUNCTION__);
138c8af8
SR
600 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
601 return cmd;
1da177e4
LT
602}
603
58272c1c
SR
604/*
605 * Unmaps the DMAs of a command and moves the command to the completed ORB list.
606 * Must be called with lu->cmd_orb_lock held.
607 */
608static void sbp2util_mark_command_completed(struct sbp2_lu *lu,
609 struct sbp2_command_info *cmd)
1da177e4 610{
58272c1c 611 struct hpsb_host *host = lu->ud->ne->host;
1da177e4 612
138c8af8
SR
613 if (cmd->cmd_dma) {
614 if (cmd->dma_type == CMD_DMA_SINGLE)
97d552e3 615 dma_unmap_single(host->device.parent, cmd->cmd_dma,
138c8af8
SR
616 cmd->dma_size, cmd->dma_dir);
617 else if (cmd->dma_type == CMD_DMA_PAGE)
97d552e3 618 dma_unmap_page(host->device.parent, cmd->cmd_dma,
138c8af8 619 cmd->dma_size, cmd->dma_dir);
edf1fb21 620 /* XXX: Check for CMD_DMA_NONE bug */
138c8af8
SR
621 cmd->dma_type = CMD_DMA_NONE;
622 cmd->cmd_dma = 0;
1da177e4 623 }
138c8af8 624 if (cmd->sge_buffer) {
97d552e3 625 dma_unmap_sg(host->device.parent, cmd->sge_buffer,
138c8af8
SR
626 cmd->dma_size, cmd->dma_dir);
627 cmd->sge_buffer = NULL;
1da177e4 628 }
cd641f68 629 list_move_tail(&cmd->list, &lu->cmd_orb_completed);
1da177e4
LT
630}
631
abd559b1 632/*
138c8af8 633 * Is lu valid? Is the 1394 node still present?
abd559b1 634 */
138c8af8 635static inline int sbp2util_node_is_available(struct sbp2_lu *lu)
abd559b1 636{
138c8af8 637 return lu && lu->ne && !lu->ne->in_limbo;
abd559b1
JM
638}
639
1da177e4
LT
640/*********************************************
641 * IEEE-1394 core driver stack related section
642 *********************************************/
1da177e4
LT
643
644static int sbp2_probe(struct device *dev)
645{
646 struct unit_directory *ud;
138c8af8 647 struct sbp2_lu *lu;
1da177e4 648
1da177e4
LT
649 ud = container_of(dev, struct unit_directory, device);
650
651 /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
652 * instead. */
653 if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
654 return -ENODEV;
655
138c8af8
SR
656 lu = sbp2_alloc_device(ud);
657 if (!lu)
a237f35f 658 return -ENOMEM;
1da177e4 659
138c8af8
SR
660 sbp2_parse_unit_directory(lu, ud);
661 return sbp2_start_device(lu);
1da177e4
LT
662}
663
664static int sbp2_remove(struct device *dev)
665{
666 struct unit_directory *ud;
138c8af8 667 struct sbp2_lu *lu;
abd559b1 668 struct scsi_device *sdev;
1da177e4 669
1da177e4 670 ud = container_of(dev, struct unit_directory, device);
138c8af8
SR
671 lu = ud->device.driver_data;
672 if (!lu)
abd559b1
JM
673 return 0;
674
138c8af8 675 if (lu->shost) {
bf637ec3
SR
676 /* Get rid of enqueued commands if there is no chance to
677 * send them. */
138c8af8
SR
678 if (!sbp2util_node_is_available(lu))
679 sbp2scsi_complete_all_commands(lu, DID_NO_CONNECT);
e8ca5668 680 /* scsi_remove_device() may trigger shutdown functions of SCSI
bf637ec3 681 * highlevel drivers which would deadlock if blocked. */
138c8af8
SR
682 atomic_set(&lu->state, SBP2LU_STATE_IN_SHUTDOWN);
683 scsi_unblock_requests(lu->shost);
bf637ec3 684 }
138c8af8 685 sdev = lu->sdev;
abd559b1 686 if (sdev) {
138c8af8 687 lu->sdev = NULL;
abd559b1
JM
688 scsi_remove_device(sdev);
689 }
1da177e4 690
138c8af8
SR
691 sbp2_logout_device(lu);
692 sbp2_remove_device(lu);
1da177e4
LT
693
694 return 0;
695}
696
697static int sbp2_update(struct unit_directory *ud)
698{
138c8af8 699 struct sbp2_lu *lu = ud->device.driver_data;
1da177e4 700
138c8af8 701 if (sbp2_reconnect_device(lu)) {
e8ca5668
SR
702 /* Reconnect has failed. Perhaps we didn't reconnect fast
703 * enough. Try a regular login, but first log out just in
704 * case of any weirdness. */
138c8af8 705 sbp2_logout_device(lu);
1da177e4 706
138c8af8 707 if (sbp2_login_device(lu)) {
1da177e4
LT
708 /* Login failed too, just fail, and the backend
709 * will call our sbp2_remove for us */
710 SBP2_ERR("Failed to reconnect to sbp2 device!");
711 return -EBUSY;
712 }
713 }
714
138c8af8
SR
715 sbp2_set_busy_timeout(lu);
716 sbp2_agent_reset(lu, 1);
717 sbp2_max_speed_and_size(lu);
1da177e4 718
e8ca5668
SR
719 /* Complete any pending commands with busy (so they get retried)
720 * and remove them from our queue. */
138c8af8 721 sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
1da177e4 722
4fc383c0
SR
723 /* Accept new commands unless there was another bus reset in the
724 * meantime. */
138c8af8
SR
725 if (hpsb_node_entry_valid(lu->ne)) {
726 atomic_set(&lu->state, SBP2LU_STATE_RUNNING);
727 scsi_unblock_requests(lu->shost);
4fc383c0 728 }
1da177e4
LT
729 return 0;
730}
731
138c8af8 732static struct sbp2_lu *sbp2_alloc_device(struct unit_directory *ud)
1da177e4 733{
ca0c7453 734 struct sbp2_fwhost_info *hi;
138c8af8
SR
735 struct Scsi_Host *shost = NULL;
736 struct sbp2_lu *lu = NULL;
261b5f66 737 unsigned long flags;
1da177e4 738
138c8af8
SR
739 lu = kzalloc(sizeof(*lu), GFP_KERNEL);
740 if (!lu) {
741 SBP2_ERR("failed to create lu");
1da177e4
LT
742 goto failed_alloc;
743 }
1da177e4 744
138c8af8
SR
745 lu->ne = ud->ne;
746 lu->ud = ud;
747 lu->speed_code = IEEE1394_SPEED_100;
748 lu->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
749 lu->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
750 INIT_LIST_HEAD(&lu->cmd_orb_inuse);
751 INIT_LIST_HEAD(&lu->cmd_orb_completed);
752 INIT_LIST_HEAD(&lu->lu_list);
753 spin_lock_init(&lu->cmd_orb_lock);
754 atomic_set(&lu->state, SBP2LU_STATE_RUNNING);
755 INIT_WORK(&lu->protocol_work, NULL);
1da177e4 756
138c8af8 757 ud->device.driver_data = lu;
1da177e4
LT
758
759 hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
760 if (!hi) {
138c8af8
SR
761 hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host,
762 sizeof(*hi));
1da177e4
LT
763 if (!hi) {
764 SBP2_ERR("failed to allocate hostinfo");
765 goto failed_alloc;
766 }
1da177e4 767 hi->host = ud->ne->host;
138c8af8 768 INIT_LIST_HEAD(&hi->logical_units);
1da177e4 769
1da177e4
LT
770#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
771 /* Handle data movement if physical dma is not
55664051
SR
772 * enabled or not supported on host controller */
773 if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
774 &sbp2_physdma_ops,
775 0x0ULL, 0xfffffffcULL)) {
776 SBP2_ERR("failed to register lower 4GB address range");
777 goto failed_alloc;
778 }
1da177e4
LT
779#endif
780 }
781
147830f2
SR
782 /* Prevent unloading of the 1394 host */
783 if (!try_module_get(hi->host->driver->owner)) {
784 SBP2_ERR("failed to get a reference on 1394 host driver");
785 goto failed_alloc;
786 }
787
138c8af8 788 lu->hi = hi;
1da177e4 789
261b5f66 790 write_lock_irqsave(&sbp2_hi_logical_units_lock, flags);
138c8af8 791 list_add_tail(&lu->lu_list, &hi->logical_units);
261b5f66 792 write_unlock_irqrestore(&sbp2_hi_logical_units_lock, flags);
1da177e4 793
35bdddb8
SR
794 /* Register the status FIFO address range. We could use the same FIFO
795 * for targets at different nodes. However we need different FIFOs per
a54c9d30
SR
796 * target in order to support multi-unit devices.
797 * The FIFO is located out of the local host controller's physical range
798 * but, if possible, within the posted write area. Status writes will
799 * then be performed as unified transactions. This slightly reduces
800 * bandwidth usage, and some Prolific based devices seem to require it.
801 */
138c8af8 802 lu->status_fifo_addr = hpsb_allocate_and_register_addrspace(
35bdddb8
SR
803 &sbp2_highlevel, ud->ne->host, &sbp2_ops,
804 sizeof(struct sbp2_status_block), sizeof(quadlet_t),
40ae6c5e 805 ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
138c8af8 806 if (lu->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
35bdddb8
SR
807 SBP2_ERR("failed to allocate status FIFO address range");
808 goto failed_alloc;
809 }
810
138c8af8
SR
811 shost = scsi_host_alloc(&sbp2_shost_template, sizeof(unsigned long));
812 if (!shost) {
1da177e4
LT
813 SBP2_ERR("failed to register scsi host");
814 goto failed_alloc;
815 }
816
138c8af8 817 shost->hostdata[0] = (unsigned long)lu;
1da177e4 818
138c8af8
SR
819 if (!scsi_add_host(shost, &ud->device)) {
820 lu->shost = shost;
821 return lu;
1da177e4
LT
822 }
823
824 SBP2_ERR("failed to add scsi host");
138c8af8 825 scsi_host_put(shost);
1da177e4
LT
826
827failed_alloc:
138c8af8 828 sbp2_remove_device(lu);
1da177e4
LT
829 return NULL;
830}
831
1da177e4
LT
832static void sbp2_host_reset(struct hpsb_host *host)
833{
ca0c7453 834 struct sbp2_fwhost_info *hi;
138c8af8 835 struct sbp2_lu *lu;
261b5f66 836 unsigned long flags;
1da177e4
LT
837
838 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
2cccbb55
SR
839 if (!hi)
840 return;
261b5f66
SR
841
842 read_lock_irqsave(&sbp2_hi_logical_units_lock, flags);
138c8af8
SR
843 list_for_each_entry(lu, &hi->logical_units, lu_list)
844 if (likely(atomic_read(&lu->state) !=
2cccbb55 845 SBP2LU_STATE_IN_SHUTDOWN)) {
138c8af8
SR
846 atomic_set(&lu->state, SBP2LU_STATE_IN_RESET);
847 scsi_block_requests(lu->shost);
09ee67ab 848 }
261b5f66 849 read_unlock_irqrestore(&sbp2_hi_logical_units_lock, flags);
1da177e4
LT
850}
851
138c8af8 852static int sbp2_start_device(struct sbp2_lu *lu)
1da177e4 853{
138c8af8 854 struct sbp2_fwhost_info *hi = lu->hi;
146f7262 855 int error;
1da177e4 856
97d552e3 857 lu->login_response = dma_alloc_coherent(hi->host->device.parent,
a237f35f 858 sizeof(struct sbp2_login_response),
9b7d9c09 859 &lu->login_response_dma, GFP_KERNEL);
138c8af8 860 if (!lu->login_response)
1da177e4 861 goto alloc_fail;
1da177e4 862
97d552e3 863 lu->query_logins_orb = dma_alloc_coherent(hi->host->device.parent,
a237f35f 864 sizeof(struct sbp2_query_logins_orb),
9b7d9c09 865 &lu->query_logins_orb_dma, GFP_KERNEL);
138c8af8 866 if (!lu->query_logins_orb)
1da177e4 867 goto alloc_fail;
1da177e4 868
97d552e3 869 lu->query_logins_response = dma_alloc_coherent(hi->host->device.parent,
a237f35f 870 sizeof(struct sbp2_query_logins_response),
9b7d9c09 871 &lu->query_logins_response_dma, GFP_KERNEL);
138c8af8 872 if (!lu->query_logins_response)
1da177e4 873 goto alloc_fail;
1da177e4 874
97d552e3 875 lu->reconnect_orb = dma_alloc_coherent(hi->host->device.parent,
a237f35f 876 sizeof(struct sbp2_reconnect_orb),
9b7d9c09 877 &lu->reconnect_orb_dma, GFP_KERNEL);
138c8af8 878 if (!lu->reconnect_orb)
1da177e4 879 goto alloc_fail;
1da177e4 880
97d552e3 881 lu->logout_orb = dma_alloc_coherent(hi->host->device.parent,
a237f35f 882 sizeof(struct sbp2_logout_orb),
9b7d9c09 883 &lu->logout_orb_dma, GFP_KERNEL);
138c8af8 884 if (!lu->logout_orb)
1da177e4 885 goto alloc_fail;
1da177e4 886
97d552e3 887 lu->login_orb = dma_alloc_coherent(hi->host->device.parent,
a237f35f 888 sizeof(struct sbp2_login_orb),
9b7d9c09 889 &lu->login_orb_dma, GFP_KERNEL);
138c8af8 890 if (!lu->login_orb)
eaceec7f 891 goto alloc_fail;
1da177e4 892
3d269cb5
SR
893 if (sbp2util_create_command_orb_pool(lu))
894 goto alloc_fail;
1da177e4 895
e8ca5668
SR
896 /* Wait a second before trying to log in. Previously logged in
897 * initiators need a chance to reconnect. */
902abed1 898 if (msleep_interruptible(1000)) {
138c8af8 899 sbp2_remove_device(lu);
1da177e4
LT
900 return -EINTR;
901 }
a237f35f 902
138c8af8
SR
903 if (sbp2_login_device(lu)) {
904 sbp2_remove_device(lu);
1da177e4
LT
905 return -EBUSY;
906 }
907
138c8af8
SR
908 sbp2_set_busy_timeout(lu);
909 sbp2_agent_reset(lu, 1);
910 sbp2_max_speed_and_size(lu);
1da177e4 911
138c8af8 912 error = scsi_add_device(lu->shost, 0, lu->ud->id, 0);
146f7262 913 if (error) {
1da177e4 914 SBP2_ERR("scsi_add_device failed");
138c8af8
SR
915 sbp2_logout_device(lu);
916 sbp2_remove_device(lu);
146f7262 917 return error;
1da177e4
LT
918 }
919
920 return 0;
eaceec7f
SR
921
922alloc_fail:
138c8af8
SR
923 SBP2_ERR("Could not allocate memory for lu");
924 sbp2_remove_device(lu);
eaceec7f 925 return -ENOMEM;
1da177e4
LT
926}
927
138c8af8 928static void sbp2_remove_device(struct sbp2_lu *lu)
1da177e4 929{
ca0c7453 930 struct sbp2_fwhost_info *hi;
261b5f66 931 unsigned long flags;
1da177e4 932
138c8af8 933 if (!lu)
1da177e4 934 return;
138c8af8 935 hi = lu->hi;
a2ee3f9b
SR
936 if (!hi)
937 goto no_hi;
1da177e4 938
138c8af8
SR
939 if (lu->shost) {
940 scsi_remove_host(lu->shost);
941 scsi_host_put(lu->shost);
1da177e4 942 }
09ee67ab 943 flush_scheduled_work();
a2ee3f9b 944 sbp2util_remove_command_orb_pool(lu, hi->host);
1da177e4 945
261b5f66 946 write_lock_irqsave(&sbp2_hi_logical_units_lock, flags);
138c8af8 947 list_del(&lu->lu_list);
261b5f66 948 write_unlock_irqrestore(&sbp2_hi_logical_units_lock, flags);
1da177e4 949
138c8af8 950 if (lu->login_response)
97d552e3 951 dma_free_coherent(hi->host->device.parent,
1da177e4 952 sizeof(struct sbp2_login_response),
138c8af8
SR
953 lu->login_response,
954 lu->login_response_dma);
955 if (lu->login_orb)
97d552e3 956 dma_free_coherent(hi->host->device.parent,
1da177e4 957 sizeof(struct sbp2_login_orb),
138c8af8
SR
958 lu->login_orb,
959 lu->login_orb_dma);
960 if (lu->reconnect_orb)
97d552e3 961 dma_free_coherent(hi->host->device.parent,
1da177e4 962 sizeof(struct sbp2_reconnect_orb),
138c8af8
SR
963 lu->reconnect_orb,
964 lu->reconnect_orb_dma);
965 if (lu->logout_orb)
97d552e3 966 dma_free_coherent(hi->host->device.parent,
1da177e4 967 sizeof(struct sbp2_logout_orb),
138c8af8
SR
968 lu->logout_orb,
969 lu->logout_orb_dma);
970 if (lu->query_logins_orb)
97d552e3 971 dma_free_coherent(hi->host->device.parent,
1da177e4 972 sizeof(struct sbp2_query_logins_orb),
138c8af8
SR
973 lu->query_logins_orb,
974 lu->query_logins_orb_dma);
975 if (lu->query_logins_response)
97d552e3 976 dma_free_coherent(hi->host->device.parent,
1da177e4 977 sizeof(struct sbp2_query_logins_response),
138c8af8
SR
978 lu->query_logins_response,
979 lu->query_logins_response_dma);
1da177e4 980
138c8af8 981 if (lu->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
35bdddb8 982 hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
138c8af8 983 lu->status_fifo_addr);
35bdddb8 984
138c8af8 985 lu->ud->device.driver_data = NULL;
1da177e4 986
a2ee3f9b
SR
987 module_put(hi->host->driver->owner);
988no_hi:
138c8af8 989 kfree(lu);
1da177e4
LT
990}
991
992#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
993/*
e8ca5668
SR
994 * Deal with write requests on adapters which do not support physical DMA or
995 * have it switched off.
1da177e4 996 */
a237f35f
SR
997static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
998 int destid, quadlet_t *data, u64 addr,
999 size_t length, u16 flags)
1da177e4 1000{
a237f35f 1001 memcpy(bus_to_virt((u32) addr), data, length);
a237f35f 1002 return RCODE_COMPLETE;
1da177e4
LT
1003}
1004
1005/*
e8ca5668
SR
1006 * Deal with read requests on adapters which do not support physical DMA or
1007 * have it switched off.
1da177e4 1008 */
a237f35f
SR
1009static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
1010 quadlet_t *data, u64 addr, size_t length,
1011 u16 flags)
1da177e4 1012{
a237f35f 1013 memcpy(data, bus_to_virt((u32) addr), length);
a237f35f 1014 return RCODE_COMPLETE;
1da177e4
LT
1015}
1016#endif
1017
1da177e4
LT
1018/**************************************
1019 * SBP-2 protocol related section
1020 **************************************/
1021
138c8af8 1022static int sbp2_query_logins(struct sbp2_lu *lu)
1da177e4 1023{
138c8af8 1024 struct sbp2_fwhost_info *hi = lu->hi;
1da177e4
LT
1025 quadlet_t data[2];
1026 int max_logins;
1027 int active_logins;
1028
138c8af8
SR
1029 lu->query_logins_orb->reserved1 = 0x0;
1030 lu->query_logins_orb->reserved2 = 0x0;
1da177e4 1031
138c8af8
SR
1032 lu->query_logins_orb->query_response_lo = lu->query_logins_response_dma;
1033 lu->query_logins_orb->query_response_hi =
1034 ORB_SET_NODE_ID(hi->host->node_id);
1035 lu->query_logins_orb->lun_misc =
1036 ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
1037 lu->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
1038 lu->query_logins_orb->lun_misc |= ORB_SET_LUN(lu->lun);
1da177e4 1039
138c8af8
SR
1040 lu->query_logins_orb->reserved_resp_length =
1041 ORB_SET_QUERY_LOGINS_RESP_LENGTH(
1042 sizeof(struct sbp2_query_logins_response));
1da177e4 1043
138c8af8
SR
1044 lu->query_logins_orb->status_fifo_hi =
1045 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1046 lu->query_logins_orb->status_fifo_lo =
1047 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1da177e4 1048
138c8af8
SR
1049 sbp2util_cpu_to_be32_buffer(lu->query_logins_orb,
1050 sizeof(struct sbp2_query_logins_orb));
1da177e4 1051
138c8af8
SR
1052 memset(lu->query_logins_response, 0,
1053 sizeof(struct sbp2_query_logins_response));
1da177e4 1054
1da177e4 1055 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
138c8af8 1056 data[1] = lu->query_logins_orb_dma;
1da177e4
LT
1057 sbp2util_cpu_to_be32_buffer(data, 8);
1058
138c8af8 1059 hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1da177e4 1060
138c8af8 1061 if (sbp2util_access_timeout(lu, 2*HZ)) {
1da177e4 1062 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
a237f35f 1063 return -EIO;
1da177e4
LT
1064 }
1065
138c8af8 1066 if (lu->status_block.ORB_offset_lo != lu->query_logins_orb_dma) {
1da177e4 1067 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
a237f35f 1068 return -EIO;
1da177e4
LT
1069 }
1070
138c8af8 1071 if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
6065772d 1072 SBP2_INFO("Error querying logins to SBP-2 device - failed");
a237f35f 1073 return -EIO;
1da177e4
LT
1074 }
1075
138c8af8
SR
1076 sbp2util_cpu_to_be32_buffer(lu->query_logins_response,
1077 sizeof(struct sbp2_query_logins_response));
1da177e4 1078
138c8af8
SR
1079 max_logins = RESPONSE_GET_MAX_LOGINS(
1080 lu->query_logins_response->length_max_logins);
20f45781 1081 SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
1da177e4 1082
138c8af8
SR
1083 active_logins = RESPONSE_GET_ACTIVE_LOGINS(
1084 lu->query_logins_response->length_max_logins);
20f45781 1085 SBP2_INFO("Number of active logins: %d", active_logins);
1da177e4
LT
1086
1087 if (active_logins >= max_logins) {
a237f35f 1088 return -EIO;
1da177e4
LT
1089 }
1090
1091 return 0;
1092}
1093
138c8af8 1094static int sbp2_login_device(struct sbp2_lu *lu)
1da177e4 1095{
138c8af8 1096 struct sbp2_fwhost_info *hi = lu->hi;
1da177e4
LT
1097 quadlet_t data[2];
1098
138c8af8 1099 if (!lu->login_orb)
a237f35f 1100 return -EIO;
1da177e4 1101
138c8af8 1102 if (!sbp2_exclusive_login && sbp2_query_logins(lu)) {
ca0c7453
SR
1103 SBP2_INFO("Device does not support any more concurrent logins");
1104 return -EIO;
1da177e4
LT
1105 }
1106
e8ca5668 1107 /* assume no password */
138c8af8
SR
1108 lu->login_orb->password_hi = 0;
1109 lu->login_orb->password_lo = 0;
1da177e4 1110
138c8af8
SR
1111 lu->login_orb->login_response_lo = lu->login_response_dma;
1112 lu->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1113 lu->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
e8ca5668
SR
1114
1115 /* one second reconnect time */
138c8af8
SR
1116 lu->login_orb->lun_misc |= ORB_SET_RECONNECT(0);
1117 lu->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(sbp2_exclusive_login);
1118 lu->login_orb->lun_misc |= ORB_SET_NOTIFY(1);
1119 lu->login_orb->lun_misc |= ORB_SET_LUN(lu->lun);
1da177e4 1120
138c8af8 1121 lu->login_orb->passwd_resp_lengths =
1da177e4 1122 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
1da177e4 1123
138c8af8
SR
1124 lu->login_orb->status_fifo_hi =
1125 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1126 lu->login_orb->status_fifo_lo =
1127 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1da177e4 1128
138c8af8
SR
1129 sbp2util_cpu_to_be32_buffer(lu->login_orb,
1130 sizeof(struct sbp2_login_orb));
1da177e4 1131
138c8af8 1132 memset(lu->login_response, 0, sizeof(struct sbp2_login_response));
1da177e4 1133
1da177e4 1134 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
138c8af8 1135 data[1] = lu->login_orb_dma;
1da177e4
LT
1136 sbp2util_cpu_to_be32_buffer(data, 8);
1137
138c8af8 1138 hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1da177e4 1139
e8ca5668 1140 /* wait up to 20 seconds for login status */
138c8af8 1141 if (sbp2util_access_timeout(lu, 20*HZ)) {
e8398bb7 1142 SBP2_ERR("Error logging into SBP-2 device - timed out");
a237f35f 1143 return -EIO;
1da177e4
LT
1144 }
1145
e8ca5668 1146 /* make sure that the returned status matches the login ORB */
138c8af8 1147 if (lu->status_block.ORB_offset_lo != lu->login_orb_dma) {
6065772d 1148 SBP2_ERR("Error logging into SBP-2 device - timed out");
a237f35f 1149 return -EIO;
1da177e4
LT
1150 }
1151
138c8af8 1152 if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
6065772d 1153 SBP2_ERR("Error logging into SBP-2 device - failed");
a237f35f 1154 return -EIO;
1da177e4
LT
1155 }
1156
138c8af8
SR
1157 sbp2util_cpu_to_be32_buffer(lu->login_response,
1158 sizeof(struct sbp2_login_response));
1159 lu->command_block_agent_addr =
1160 ((u64)lu->login_response->command_block_agent_hi) << 32;
1161 lu->command_block_agent_addr |=
1162 ((u64)lu->login_response->command_block_agent_lo);
1163 lu->command_block_agent_addr &= 0x0000ffffffffffffULL;
1da177e4
LT
1164
1165 SBP2_INFO("Logged into SBP-2 device");
a237f35f 1166 return 0;
1da177e4
LT
1167}
1168
138c8af8 1169static int sbp2_logout_device(struct sbp2_lu *lu)
1da177e4 1170{
138c8af8 1171 struct sbp2_fwhost_info *hi = lu->hi;
1da177e4
LT
1172 quadlet_t data[2];
1173 int error;
1174
138c8af8
SR
1175 lu->logout_orb->reserved1 = 0x0;
1176 lu->logout_orb->reserved2 = 0x0;
1177 lu->logout_orb->reserved3 = 0x0;
1178 lu->logout_orb->reserved4 = 0x0;
1da177e4 1179
138c8af8
SR
1180 lu->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
1181 lu->logout_orb->login_ID_misc |=
1182 ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
1183 lu->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1da177e4 1184
138c8af8
SR
1185 lu->logout_orb->reserved5 = 0x0;
1186 lu->logout_orb->status_fifo_hi =
1187 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1188 lu->logout_orb->status_fifo_lo =
1189 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1da177e4 1190
138c8af8
SR
1191 sbp2util_cpu_to_be32_buffer(lu->logout_orb,
1192 sizeof(struct sbp2_logout_orb));
1da177e4 1193
1da177e4 1194 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
138c8af8 1195 data[1] = lu->logout_orb_dma;
1da177e4
LT
1196 sbp2util_cpu_to_be32_buffer(data, 8);
1197
138c8af8 1198 error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1da177e4
LT
1199 if (error)
1200 return error;
1201
e8ca5668 1202 /* wait up to 1 second for the device to complete logout */
138c8af8 1203 if (sbp2util_access_timeout(lu, HZ))
1da177e4
LT
1204 return -EIO;
1205
1206 SBP2_INFO("Logged out of SBP-2 device");
a237f35f 1207 return 0;
1da177e4
LT
1208}
1209
138c8af8 1210static int sbp2_reconnect_device(struct sbp2_lu *lu)
1da177e4 1211{
138c8af8 1212 struct sbp2_fwhost_info *hi = lu->hi;
1da177e4
LT
1213 quadlet_t data[2];
1214 int error;
1215
138c8af8
SR
1216 lu->reconnect_orb->reserved1 = 0x0;
1217 lu->reconnect_orb->reserved2 = 0x0;
1218 lu->reconnect_orb->reserved3 = 0x0;
1219 lu->reconnect_orb->reserved4 = 0x0;
1da177e4 1220
138c8af8
SR
1221 lu->reconnect_orb->login_ID_misc =
1222 ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
1223 lu->reconnect_orb->login_ID_misc |=
1224 ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
1225 lu->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1da177e4 1226
138c8af8
SR
1227 lu->reconnect_orb->reserved5 = 0x0;
1228 lu->reconnect_orb->status_fifo_hi =
1229 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1230 lu->reconnect_orb->status_fifo_lo =
1231 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1da177e4 1232
138c8af8
SR
1233 sbp2util_cpu_to_be32_buffer(lu->reconnect_orb,
1234 sizeof(struct sbp2_reconnect_orb));
1da177e4 1235
1da177e4 1236 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
138c8af8 1237 data[1] = lu->reconnect_orb_dma;
1da177e4
LT
1238 sbp2util_cpu_to_be32_buffer(data, 8);
1239
138c8af8 1240 error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1da177e4
LT
1241 if (error)
1242 return error;
1243
e8ca5668 1244 /* wait up to 1 second for reconnect status */
138c8af8 1245 if (sbp2util_access_timeout(lu, HZ)) {
e8398bb7 1246 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
a237f35f 1247 return -EIO;
1da177e4
LT
1248 }
1249
e8ca5668 1250 /* make sure that the returned status matches the reconnect ORB */
138c8af8 1251 if (lu->status_block.ORB_offset_lo != lu->reconnect_orb_dma) {
6065772d 1252 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
a237f35f 1253 return -EIO;
1da177e4
LT
1254 }
1255
138c8af8 1256 if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
6065772d 1257 SBP2_ERR("Error reconnecting to SBP-2 device - failed");
a237f35f 1258 return -EIO;
1da177e4
LT
1259 }
1260
35644090 1261 SBP2_INFO("Reconnected to SBP-2 device");
a237f35f 1262 return 0;
1da177e4
LT
1263}
1264
1265/*
e8ca5668
SR
1266 * Set the target node's Single Phase Retry limit. Affects the target's retry
1267 * behaviour if our node is too busy to accept requests.
1da177e4 1268 */
138c8af8 1269static int sbp2_set_busy_timeout(struct sbp2_lu *lu)
1da177e4
LT
1270{
1271 quadlet_t data;
1272
1da177e4 1273 data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
138c8af8 1274 if (hpsb_node_write(lu->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
d024ebc6 1275 SBP2_ERR("%s error", __FUNCTION__);
a237f35f 1276 return 0;
1da177e4
LT
1277}
1278
138c8af8 1279static void sbp2_parse_unit_directory(struct sbp2_lu *lu,
1da177e4
LT
1280 struct unit_directory *ud)
1281{
1282 struct csr1212_keyval *kv;
1283 struct csr1212_dentry *dentry;
1284 u64 management_agent_addr;
9117c6dc 1285 u32 unit_characteristics, firmware_revision;
24d3bf88 1286 unsigned workarounds;
1da177e4
LT
1287 int i;
1288
9117c6dc
SR
1289 management_agent_addr = 0;
1290 unit_characteristics = 0;
1291 firmware_revision = 0;
1da177e4 1292
1da177e4
LT
1293 csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
1294 switch (kv->key.id) {
1295 case CSR1212_KV_ID_DEPENDENT_INFO:
edf1fb21 1296 if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET)
1da177e4 1297 management_agent_addr =
a237f35f
SR
1298 CSR1212_REGISTER_SPACE_BASE +
1299 (kv->value.csr_offset << 2);
1da177e4 1300
edf1fb21 1301 else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE)
138c8af8 1302 lu->lun = ORB_SET_LUN(kv->value.immediate);
1da177e4
LT
1303 break;
1304
1da177e4 1305 case SBP2_UNIT_CHARACTERISTICS_KEY:
e8ca5668
SR
1306 /* FIXME: This is ignored so far.
1307 * See SBP-2 clause 7.4.8. */
1da177e4 1308 unit_characteristics = kv->value.immediate;
1da177e4
LT
1309 break;
1310
1311 case SBP2_FIRMWARE_REVISION_KEY:
1da177e4 1312 firmware_revision = kv->value.immediate;
1da177e4
LT
1313 break;
1314
1315 default:
e8ca5668
SR
1316 /* FIXME: Check for SBP2_DEVICE_TYPE_AND_LUN_KEY.
1317 * Its "ordered" bit has consequences for command ORB
1318 * list handling. See SBP-2 clauses 4.6, 7.4.11, 10.2 */
1da177e4
LT
1319 break;
1320 }
1321 }
1322
24d3bf88 1323 workarounds = sbp2_default_workarounds;
1da177e4 1324
679c0cd2
SR
1325 if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
1326 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
4618fd30
SR
1327 if (sbp2_workarounds_table[i].firmware_revision !=
1328 SBP2_ROM_VALUE_WILDCARD &&
679c0cd2
SR
1329 sbp2_workarounds_table[i].firmware_revision !=
1330 (firmware_revision & 0xffff00))
1331 continue;
4618fd30
SR
1332 if (sbp2_workarounds_table[i].model_id !=
1333 SBP2_ROM_VALUE_WILDCARD &&
679c0cd2
SR
1334 sbp2_workarounds_table[i].model_id != ud->model_id)
1335 continue;
1336 workarounds |= sbp2_workarounds_table[i].workarounds;
1337 break;
1338 }
1da177e4 1339
24d3bf88 1340 if (workarounds)
e9a1c52c
SR
1341 SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
1342 "(firmware_revision 0x%06x, vendor_id 0x%06x,"
1343 " model_id 0x%06x)",
24d3bf88 1344 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
e9a1c52c
SR
1345 workarounds, firmware_revision,
1346 ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
1347 ud->model_id);
24d3bf88
SR
1348
1349 /* We would need one SCSI host template for each target to adjust
1350 * max_sectors on the fly, therefore warn only. */
1351 if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
ca0c7453 1352 (sbp2_max_sectors * 512) > (128 * 1024))
35644090 1353 SBP2_INFO("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
24d3bf88
SR
1354 "max transfer size. WARNING: Current max_sectors "
1355 "setting is larger than 128KB (%d sectors)",
1356 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
ca0c7453 1357 sbp2_max_sectors);
24d3bf88 1358
1da177e4
LT
1359 /* If this is a logical unit directory entry, process the parent
1360 * to get the values. */
1361 if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
138c8af8
SR
1362 struct unit_directory *parent_ud = container_of(
1363 ud->device.parent, struct unit_directory, device);
1364 sbp2_parse_unit_directory(lu, parent_ud);
1da177e4 1365 } else {
138c8af8
SR
1366 lu->management_agent_addr = management_agent_addr;
1367 lu->workarounds = workarounds;
1da177e4 1368 if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
138c8af8 1369 lu->lun = ORB_SET_LUN(ud->lun);
1da177e4
LT
1370 }
1371}
1372
fd23ade8
BC
1373#define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
1374
1da177e4
LT
1375/*
1376 * This function is called in order to determine the max speed and packet
1377 * size we can use in our ORBs. Note, that we (the driver and host) only
1378 * initiate the transaction. The SBP-2 device actually transfers the data
1379 * (by reading from the DMA area we tell it). This means that the SBP-2
1380 * device decides the actual maximum data it can transfer. We just tell it
1381 * the speed that it needs to use, and the max_rec the host supports, and
1382 * it takes care of the rest.
1383 */
138c8af8 1384static int sbp2_max_speed_and_size(struct sbp2_lu *lu)
1da177e4 1385{
138c8af8 1386 struct sbp2_fwhost_info *hi = lu->hi;
fd23ade8 1387 u8 payload;
1da177e4 1388
138c8af8 1389 lu->speed_code = hi->host->speed[NODEID_TO_NODE(lu->ne->nodeid)];
1da177e4 1390
138c8af8
SR
1391 if (lu->speed_code > sbp2_max_speed) {
1392 lu->speed_code = sbp2_max_speed;
ca0c7453
SR
1393 SBP2_INFO("Reducing speed to %s",
1394 hpsb_speedto_str[sbp2_max_speed]);
1da177e4
LT
1395 }
1396
1397 /* Payload size is the lesser of what our speed supports and what
1398 * our host supports. */
138c8af8 1399 payload = min(sbp2_speedto_max_payload[lu->speed_code],
fd23ade8
BC
1400 (u8) (hi->host->csr.max_rec - 1));
1401
1402 /* If physical DMA is off, work around limitation in ohci1394:
1403 * packet size must not exceed PAGE_SIZE */
138c8af8 1404 if (lu->ne->host->low_addr_space < (1ULL << 32))
fd23ade8
BC
1405 while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
1406 payload)
1407 payload--;
1da177e4 1408
35644090 1409 SBP2_INFO("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
138c8af8
SR
1410 NODE_BUS_ARGS(hi->host, lu->ne->nodeid),
1411 hpsb_speedto_str[lu->speed_code],
35644090 1412 SBP2_PAYLOAD_TO_BYTES(payload));
1da177e4 1413
138c8af8 1414 lu->max_payload_size = payload;
a237f35f 1415 return 0;
1da177e4
LT
1416}
1417
138c8af8 1418static int sbp2_agent_reset(struct sbp2_lu *lu, int wait)
1da177e4
LT
1419{
1420 quadlet_t data;
1421 u64 addr;
1422 int retval;
cc078189 1423 unsigned long flags;
1da177e4 1424
ec9b7e10 1425 /* flush lu->protocol_work */
09ee67ab
SR
1426 if (wait)
1427 flush_scheduled_work();
1428
1da177e4 1429 data = ntohl(SBP2_AGENT_RESET_DATA);
138c8af8 1430 addr = lu->command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
1da177e4
LT
1431
1432 if (wait)
138c8af8 1433 retval = hpsb_node_write(lu->ne, addr, &data, 4);
1da177e4 1434 else
138c8af8 1435 retval = sbp2util_node_write_no_wait(lu->ne, addr, &data, 4);
1da177e4
LT
1436
1437 if (retval < 0) {
1438 SBP2_ERR("hpsb_node_write failed.\n");
1439 return -EIO;
1440 }
1441
e8ca5668 1442 /* make sure that the ORB_POINTER is written on next command */
138c8af8
SR
1443 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1444 lu->last_orb = NULL;
1445 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1da177e4 1446
a237f35f 1447 return 0;
1da177e4
LT
1448}
1449
cf8d2c09 1450static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
ca0c7453 1451 struct sbp2_fwhost_info *hi,
138c8af8 1452 struct sbp2_command_info *cmd,
cf8d2c09 1453 unsigned int scsi_use_sg,
825f1df5 1454 struct scatterlist *sg,
cf8d2c09
SR
1455 u32 orb_direction,
1456 enum dma_data_direction dma_dir)
1457{
138c8af8 1458 cmd->dma_dir = dma_dir;
cf8d2c09
SR
1459 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1460 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1461
e8ca5668 1462 /* special case if only one element (and less than 64KB in size) */
cf8d2c09 1463 if ((scsi_use_sg == 1) &&
825f1df5 1464 (sg_dma_len(sg) <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
cf8d2c09 1465
825f1df5 1466 cmd->dma_size = sg_dma_len(sg);
138c8af8 1467 cmd->dma_type = CMD_DMA_PAGE;
97d552e3 1468 cmd->cmd_dma = dma_map_page(hi->host->device.parent,
825f1df5 1469 sg_page(sg), sg->offset,
138c8af8 1470 cmd->dma_size, cmd->dma_dir);
cf8d2c09 1471
138c8af8
SR
1472 orb->data_descriptor_lo = cmd->cmd_dma;
1473 orb->misc |= ORB_SET_DATA_SIZE(cmd->dma_size);
cf8d2c09
SR
1474
1475 } else {
1476 struct sbp2_unrestricted_page_table *sg_element =
138c8af8 1477 &cmd->scatter_gather_element[0];
cf8d2c09
SR
1478 u32 sg_count, sg_len;
1479 dma_addr_t sg_addr;
825f1df5 1480 int i, count = dma_map_sg(hi->host->device.parent, sg,
97d552e3 1481 scsi_use_sg, dma_dir);
cf8d2c09 1482
138c8af8 1483 cmd->dma_size = scsi_use_sg;
825f1df5 1484 cmd->sge_buffer = sg;
cf8d2c09
SR
1485
1486 /* use page tables (s/g) */
1487 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
138c8af8 1488 orb->data_descriptor_lo = cmd->sge_dma;
cf8d2c09 1489
e8ca5668
SR
1490 /* loop through and fill out our SBP-2 page tables
1491 * (and split up anything too large) */
825f1df5
SR
1492 for (i = 0, sg_count = 0; i < count; i++, sg = sg_next(sg)) {
1493 sg_len = sg_dma_len(sg);
1494 sg_addr = sg_dma_address(sg);
cf8d2c09
SR
1495 while (sg_len) {
1496 sg_element[sg_count].segment_base_lo = sg_addr;
1497 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1498 sg_element[sg_count].length_segment_base_hi =
1499 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1500 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1501 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1502 } else {
1503 sg_element[sg_count].length_segment_base_hi =
1504 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1505 sg_len = 0;
1506 }
1507 sg_count++;
1508 }
1509 }
1510
cf8d2c09
SR
1511 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1512
cf8d2c09 1513 sbp2util_cpu_to_be32_buffer(sg_element,
138c8af8
SR
1514 (sizeof(struct sbp2_unrestricted_page_table)) *
1515 sg_count);
cf8d2c09
SR
1516 }
1517}
1518
138c8af8
SR
1519static void sbp2_create_command_orb(struct sbp2_lu *lu,
1520 struct sbp2_command_info *cmd,
cf8d2c09
SR
1521 unchar *scsi_cmd,
1522 unsigned int scsi_use_sg,
1523 unsigned int scsi_request_bufflen,
825f1df5 1524 struct scatterlist *sg,
cf8d2c09 1525 enum dma_data_direction dma_dir)
1da177e4 1526{
138c8af8 1527 struct sbp2_fwhost_info *hi = lu->hi;
138c8af8 1528 struct sbp2_command_orb *orb = &cmd->command_orb;
cf8d2c09 1529 u32 orb_direction;
1da177e4
LT
1530
1531 /*
e8ca5668 1532 * Set-up our command ORB.
1da177e4
LT
1533 *
1534 * NOTE: We're doing unrestricted page tables (s/g), as this is
1535 * best performance (at least with the devices I have). This means
1536 * that data_size becomes the number of s/g elements, and
1537 * page_size should be zero (for unrestricted).
1538 */
138c8af8
SR
1539 orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1540 orb->next_ORB_lo = 0x0;
1541 orb->misc = ORB_SET_MAX_PAYLOAD(lu->max_payload_size);
1542 orb->misc |= ORB_SET_SPEED(lu->speed_code);
1543 orb->misc |= ORB_SET_NOTIFY(1);
1da177e4 1544
43863eba 1545 if (dma_dir == DMA_NONE)
a237f35f 1546 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
43863eba 1547 else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
a237f35f 1548 orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
43863eba 1549 else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
a237f35f 1550 orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
43863eba 1551 else {
35644090 1552 SBP2_INFO("Falling back to DMA_NONE");
43863eba 1553 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1da177e4
LT
1554 }
1555
e8ca5668 1556 /* set up our page table stuff */
1da177e4 1557 if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
138c8af8
SR
1558 orb->data_descriptor_hi = 0x0;
1559 orb->data_descriptor_lo = 0x0;
1560 orb->misc |= ORB_SET_DIRECTION(1);
d7dea2cf 1561 } else
825f1df5 1562 sbp2_prep_command_orb_sg(orb, hi, cmd, scsi_use_sg, sg,
138c8af8 1563 orb_direction, dma_dir);
1da177e4 1564
138c8af8 1565 sbp2util_cpu_to_be32_buffer(orb, sizeof(*orb));
1da177e4 1566
138c8af8
SR
1567 memset(orb->cdb, 0, 12);
1568 memcpy(orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
1da177e4
LT
1569}
1570
138c8af8
SR
1571static void sbp2_link_orb_command(struct sbp2_lu *lu,
1572 struct sbp2_command_info *cmd)
1da177e4 1573{
138c8af8 1574 struct sbp2_fwhost_info *hi = lu->hi;
cc078189
SR
1575 struct sbp2_command_orb *last_orb;
1576 dma_addr_t last_orb_dma;
138c8af8 1577 u64 addr = lu->command_block_agent_addr;
cc078189
SR
1578 quadlet_t data[2];
1579 size_t length;
1580 unsigned long flags;
1da177e4 1581
97d552e3
SR
1582 dma_sync_single_for_device(hi->host->device.parent,
1583 cmd->command_orb_dma,
9b7d9c09
SR
1584 sizeof(struct sbp2_command_orb),
1585 DMA_TO_DEVICE);
97d552e3 1586 dma_sync_single_for_device(hi->host->device.parent, cmd->sge_dma,
9b7d9c09 1587 sizeof(cmd->scatter_gather_element),
2446a79f 1588 DMA_TO_DEVICE);
e8ca5668
SR
1589
1590 /* check to see if there are any previous orbs to use */
138c8af8
SR
1591 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1592 last_orb = lu->last_orb;
1593 last_orb_dma = lu->last_orb_dma;
cc078189 1594 if (!last_orb) {
1da177e4 1595 /*
cc078189
SR
1596 * last_orb == NULL means: We know that the target's fetch agent
1597 * is not active right now.
1da177e4 1598 */
cc078189 1599 addr += SBP2_ORB_POINTER_OFFSET;
1da177e4 1600 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
138c8af8 1601 data[1] = cmd->command_orb_dma;
1da177e4 1602 sbp2util_cpu_to_be32_buffer(data, 8);
cc078189 1603 length = 8;
1da177e4 1604 } else {
1da177e4 1605 /*
cc078189
SR
1606 * last_orb != NULL means: We know that the target's fetch agent
1607 * is (very probably) not dead or in reset state right now.
1608 * We have an ORB already sent that we can append a new one to.
1609 * The target's fetch agent may or may not have read this
1610 * previous ORB yet.
1da177e4 1611 */
97d552e3 1612 dma_sync_single_for_cpu(hi->host->device.parent, last_orb_dma,
9b7d9c09
SR
1613 sizeof(struct sbp2_command_orb),
1614 DMA_TO_DEVICE);
138c8af8 1615 last_orb->next_ORB_lo = cpu_to_be32(cmd->command_orb_dma);
cc078189 1616 wmb();
1da177e4 1617 /* Tells hardware that this pointer is valid */
cc078189 1618 last_orb->next_ORB_hi = 0;
97d552e3
SR
1619 dma_sync_single_for_device(hi->host->device.parent,
1620 last_orb_dma,
9b7d9c09
SR
1621 sizeof(struct sbp2_command_orb),
1622 DMA_TO_DEVICE);
cc078189
SR
1623 addr += SBP2_DOORBELL_OFFSET;
1624 data[0] = 0;
1625 length = 4;
1626 }
138c8af8
SR
1627 lu->last_orb = &cmd->command_orb;
1628 lu->last_orb_dma = cmd->command_orb_dma;
1629 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1da177e4 1630
138c8af8 1631 if (sbp2util_node_write_no_wait(lu->ne, addr, data, length)) {
09ee67ab
SR
1632 /*
1633 * sbp2util_node_write_no_wait failed. We certainly ran out
1634 * of transaction labels, perhaps just because there were no
1635 * context switches which gave khpsbpkt a chance to collect
1636 * free tlabels. Try again in non-atomic context. If necessary,
1637 * the workqueue job will sleep to guaranteedly get a tlabel.
1638 * We do not accept new commands until the job is over.
1639 */
138c8af8
SR
1640 scsi_block_requests(lu->shost);
1641 PREPARE_WORK(&lu->protocol_work,
09ee67ab 1642 last_orb ? sbp2util_write_doorbell:
ec9b7e10 1643 sbp2util_write_orb_pointer);
138c8af8 1644 schedule_work(&lu->protocol_work);
09ee67ab 1645 }
1da177e4
LT
1646}
1647
138c8af8 1648static int sbp2_send_command(struct sbp2_lu *lu, struct scsi_cmnd *SCpnt,
1da177e4
LT
1649 void (*done)(struct scsi_cmnd *))
1650{
138c8af8 1651 unchar *scsi_cmd = (unchar *)SCpnt->cmnd;
138c8af8 1652 struct sbp2_command_info *cmd;
1da177e4 1653
138c8af8
SR
1654 cmd = sbp2util_allocate_command_orb(lu, SCpnt, done);
1655 if (!cmd)
a237f35f 1656 return -EIO;
1da177e4 1657
d7dea2cf 1658 sbp2_create_command_orb(lu, cmd, scsi_cmd, scsi_sg_count(SCpnt),
825f1df5 1659 scsi_bufflen(SCpnt), scsi_sglist(SCpnt),
1da177e4 1660 SCpnt->sc_data_direction);
138c8af8 1661 sbp2_link_orb_command(lu, cmd);
1da177e4 1662
a237f35f 1663 return 0;
1da177e4
LT
1664}
1665
1da177e4
LT
1666/*
1667 * Translates SBP-2 status into SCSI sense data for check conditions
1668 */
138c8af8
SR
1669static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status,
1670 unchar *sense_data)
1da177e4 1671{
e8ca5668 1672 /* OK, it's pretty ugly... ;-) */
1da177e4
LT
1673 sense_data[0] = 0x70;
1674 sense_data[1] = 0x0;
1675 sense_data[2] = sbp2_status[9];
1676 sense_data[3] = sbp2_status[12];
1677 sense_data[4] = sbp2_status[13];
1678 sense_data[5] = sbp2_status[14];
1679 sense_data[6] = sbp2_status[15];
1680 sense_data[7] = 10;
1681 sense_data[8] = sbp2_status[16];
1682 sense_data[9] = sbp2_status[17];
1683 sense_data[10] = sbp2_status[18];
1684 sense_data[11] = sbp2_status[19];
1685 sense_data[12] = sbp2_status[10];
1686 sense_data[13] = sbp2_status[11];
1687 sense_data[14] = sbp2_status[20];
1688 sense_data[15] = sbp2_status[21];
1689
e8ca5668 1690 return sbp2_status[8] & 0x3f;
1da177e4
LT
1691}
1692
3e98eab4
SR
1693static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
1694 int destid, quadlet_t *data, u64 addr,
1695 size_t length, u16 fl)
1da177e4 1696{
ca0c7453 1697 struct sbp2_fwhost_info *hi;
138c8af8 1698 struct sbp2_lu *lu = NULL, *lu_tmp;
1da177e4 1699 struct scsi_cmnd *SCpnt = NULL;
3e98eab4 1700 struct sbp2_status_block *sb;
1da177e4 1701 u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
138c8af8 1702 struct sbp2_command_info *cmd;
79456197 1703 unsigned long flags;
1da177e4 1704
6065772d
SR
1705 if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
1706 SBP2_ERR("Wrong size of status block");
1707 return RCODE_ADDRESS_ERROR;
1708 }
1709 if (unlikely(!host)) {
1da177e4 1710 SBP2_ERR("host is NULL - this is bad!");
a237f35f 1711 return RCODE_ADDRESS_ERROR;
1da177e4 1712 }
1da177e4 1713 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
6065772d 1714 if (unlikely(!hi)) {
1da177e4 1715 SBP2_ERR("host info is NULL - this is bad!");
a237f35f 1716 return RCODE_ADDRESS_ERROR;
1da177e4 1717 }
e8ca5668
SR
1718
1719 /* Find the unit which wrote the status. */
261b5f66 1720 read_lock_irqsave(&sbp2_hi_logical_units_lock, flags);
138c8af8
SR
1721 list_for_each_entry(lu_tmp, &hi->logical_units, lu_list) {
1722 if (lu_tmp->ne->nodeid == nodeid &&
1723 lu_tmp->status_fifo_addr == addr) {
1724 lu = lu_tmp;
1da177e4
LT
1725 break;
1726 }
1727 }
261b5f66
SR
1728 read_unlock_irqrestore(&sbp2_hi_logical_units_lock, flags);
1729
138c8af8
SR
1730 if (unlikely(!lu)) {
1731 SBP2_ERR("lu is NULL - device is gone?");
a237f35f 1732 return RCODE_ADDRESS_ERROR;
1da177e4
LT
1733 }
1734
138c8af8 1735 /* Put response into lu status fifo buffer. The first two bytes
3e98eab4
SR
1736 * come in big endian bit order. Often the target writes only a
1737 * truncated status block, minimally the first two quadlets. The rest
e8ca5668 1738 * is implied to be zeros. */
138c8af8 1739 sb = &lu->status_block;
3e98eab4
SR
1740 memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
1741 memcpy(sb, data, length);
1742 sbp2util_be32_to_cpu_buffer(sb, 8);
1da177e4 1743
e8ca5668 1744 /* Ignore unsolicited status. Handle command ORB status. */
6065772d 1745 if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
138c8af8 1746 cmd = NULL;
6065772d 1747 else
138c8af8
SR
1748 cmd = sbp2util_find_command_for_orb(lu, sb->ORB_offset_lo);
1749 if (cmd) {
97d552e3
SR
1750 dma_sync_single_for_cpu(hi->host->device.parent,
1751 cmd->command_orb_dma,
9b7d9c09
SR
1752 sizeof(struct sbp2_command_orb),
1753 DMA_TO_DEVICE);
97d552e3 1754 dma_sync_single_for_cpu(hi->host->device.parent, cmd->sge_dma,
9b7d9c09 1755 sizeof(cmd->scatter_gather_element),
2446a79f 1756 DMA_TO_DEVICE);
e8ca5668 1757 /* Grab SCSI command pointers and check status. */
6065772d
SR
1758 /*
1759 * FIXME: If the src field in the status is 1, the ORB DMA must
1760 * not be reused until status for a subsequent ORB is received.
1761 */
138c8af8
SR
1762 SCpnt = cmd->Current_SCpnt;
1763 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1764 sbp2util_mark_command_completed(lu, cmd);
1765 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1da177e4
LT
1766
1767 if (SCpnt) {
abbca103
SR
1768 u32 h = sb->ORB_offset_hi_misc;
1769 u32 r = STATUS_GET_RESP(h);
1770
1771 if (r != RESP_STATUS_REQUEST_COMPLETE) {
35644090 1772 SBP2_INFO("resp 0x%x, sbp_status 0x%x",
abbca103 1773 r, STATUS_GET_SBP_STATUS(h));
6065772d 1774 scsi_status =
abbca103
SR
1775 r == RESP_STATUS_TRANSPORT_FAILURE ?
1776 SBP2_SCSI_STATUS_BUSY :
6065772d 1777 SBP2_SCSI_STATUS_COMMAND_TERMINATED;
abbca103 1778 }
e8ca5668 1779
edf1fb21 1780 if (STATUS_GET_LEN(h) > 1)
3e98eab4
SR
1781 scsi_status = sbp2_status_to_sense_data(
1782 (unchar *)sb, SCpnt->sense_buffer);
e8ca5668 1783
edf1fb21 1784 if (STATUS_TEST_DEAD(h))
138c8af8 1785 sbp2_agent_reset(lu, 0);
1da177e4
LT
1786 }
1787
e8ca5668 1788 /* Check here to see if there are no commands in-use. If there
cc078189
SR
1789 * are none, we know that the fetch agent left the active state
1790 * _and_ that we did not reactivate it yet. Therefore clear
1791 * last_orb so that next time we write directly to the
1792 * ORB_POINTER register. That way the fetch agent does not need
e8ca5668 1793 * to refetch the next_ORB. */
138c8af8
SR
1794 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1795 if (list_empty(&lu->cmd_orb_inuse))
1796 lu->last_orb = NULL;
1797 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1da177e4
LT
1798
1799 } else {
e8ca5668 1800 /* It's probably status after a management request. */
138c8af8
SR
1801 if ((sb->ORB_offset_lo == lu->reconnect_orb_dma) ||
1802 (sb->ORB_offset_lo == lu->login_orb_dma) ||
1803 (sb->ORB_offset_lo == lu->query_logins_orb_dma) ||
1804 (sb->ORB_offset_lo == lu->logout_orb_dma)) {
1805 lu->access_complete = 1;
ca0c7453 1806 wake_up_interruptible(&sbp2_access_wq);
e8398bb7 1807 }
1da177e4
LT
1808 }
1809
edf1fb21 1810 if (SCpnt)
138c8af8
SR
1811 sbp2scsi_complete_command(lu, scsi_status, SCpnt,
1812 cmd->Current_done);
a237f35f 1813 return RCODE_COMPLETE;
1da177e4
LT
1814}
1815
1da177e4
LT
1816/**************************************
1817 * SCSI interface related section
1818 **************************************/
1819
1da177e4
LT
1820static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
1821 void (*done)(struct scsi_cmnd *))
1822{
138c8af8 1823 struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
ca0c7453 1824 struct sbp2_fwhost_info *hi;
abd559b1 1825 int result = DID_NO_CONNECT << 16;
1da177e4 1826
138c8af8 1827 if (unlikely(!sbp2util_node_is_available(lu)))
abd559b1 1828 goto done;
1da177e4 1829
138c8af8 1830 hi = lu->hi;
1da177e4 1831
5796aa7b 1832 if (unlikely(!hi)) {
ca0c7453 1833 SBP2_ERR("sbp2_fwhost_info is NULL - this is bad!");
abd559b1 1834 goto done;
1da177e4
LT
1835 }
1836
e8ca5668
SR
1837 /* Multiple units are currently represented to the SCSI core as separate
1838 * targets, not as one target with multiple LUs. Therefore return
1839 * selection time-out to any IO directed at non-zero LUNs. */
5796aa7b 1840 if (unlikely(SCpnt->device->lun))
abd559b1 1841 goto done;
1da177e4 1842
138c8af8 1843 if (unlikely(!hpsb_node_entry_valid(lu->ne))) {
1da177e4 1844 SBP2_ERR("Bus reset in progress - rejecting command");
abd559b1
JM
1845 result = DID_BUS_BUSY << 16;
1846 goto done;
1da177e4
LT
1847 }
1848
e8ca5668
SR
1849 /* Bidirectional commands are not yet implemented,
1850 * and unknown transfer direction not handled. */
5796aa7b 1851 if (unlikely(SCpnt->sc_data_direction == DMA_BIDIRECTIONAL)) {
43863eba
SR
1852 SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
1853 result = DID_ERROR << 16;
1854 goto done;
1855 }
1856
138c8af8 1857 if (sbp2_send_command(lu, SCpnt, done)) {
1da177e4 1858 SBP2_ERR("Error sending SCSI command");
138c8af8
SR
1859 sbp2scsi_complete_command(lu,
1860 SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
1da177e4
LT
1861 SCpnt, done);
1862 }
abd559b1 1863 return 0;
1da177e4 1864
abd559b1
JM
1865done:
1866 SCpnt->result = result;
1867 done(SCpnt);
1868 return 0;
1da177e4
LT
1869}
1870
138c8af8 1871static void sbp2scsi_complete_all_commands(struct sbp2_lu *lu, u32 status)
1da177e4 1872{
138c8af8 1873 struct sbp2_fwhost_info *hi = lu->hi;
1da177e4 1874 struct list_head *lh;
138c8af8 1875 struct sbp2_command_info *cmd;
79456197 1876 unsigned long flags;
1da177e4 1877
138c8af8
SR
1878 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1879 while (!list_empty(&lu->cmd_orb_inuse)) {
1880 lh = lu->cmd_orb_inuse.next;
1881 cmd = list_entry(lh, struct sbp2_command_info, list);
97d552e3
SR
1882 dma_sync_single_for_cpu(hi->host->device.parent,
1883 cmd->command_orb_dma,
9b7d9c09
SR
1884 sizeof(struct sbp2_command_orb),
1885 DMA_TO_DEVICE);
97d552e3 1886 dma_sync_single_for_cpu(hi->host->device.parent, cmd->sge_dma,
9b7d9c09 1887 sizeof(cmd->scatter_gather_element),
2446a79f 1888 DMA_TO_DEVICE);
138c8af8
SR
1889 sbp2util_mark_command_completed(lu, cmd);
1890 if (cmd->Current_SCpnt) {
1891 cmd->Current_SCpnt->result = status << 16;
1892 cmd->Current_done(cmd->Current_SCpnt);
1da177e4
LT
1893 }
1894 }
138c8af8 1895 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1da177e4
LT
1896
1897 return;
1898}
1899
1900/*
e8ca5668 1901 * Complete a regular SCSI command. Can be called in atomic context.
1da177e4 1902 */
138c8af8
SR
1903static void sbp2scsi_complete_command(struct sbp2_lu *lu, u32 scsi_status,
1904 struct scsi_cmnd *SCpnt,
1da177e4
LT
1905 void (*done)(struct scsi_cmnd *))
1906{
1da177e4
LT
1907 if (!SCpnt) {
1908 SBP2_ERR("SCpnt is NULL");
1909 return;
1910 }
1911
1da177e4 1912 switch (scsi_status) {
a237f35f 1913 case SBP2_SCSI_STATUS_GOOD:
8f0525ff 1914 SCpnt->result = DID_OK << 16;
a237f35f 1915 break;
1da177e4 1916
a237f35f
SR
1917 case SBP2_SCSI_STATUS_BUSY:
1918 SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
1919 SCpnt->result = DID_BUS_BUSY << 16;
1920 break;
1da177e4 1921
a237f35f 1922 case SBP2_SCSI_STATUS_CHECK_CONDITION:
8f0525ff 1923 SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
a237f35f 1924 break;
1da177e4 1925
a237f35f
SR
1926 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
1927 SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
1928 SCpnt->result = DID_NO_CONNECT << 16;
1929 scsi_print_command(SCpnt);
1930 break;
1da177e4 1931
a237f35f
SR
1932 case SBP2_SCSI_STATUS_CONDITION_MET:
1933 case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
1934 case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
1935 SBP2_ERR("Bad SCSI status = %x", scsi_status);
1936 SCpnt->result = DID_ERROR << 16;
1937 scsi_print_command(SCpnt);
1938 break;
1da177e4 1939
a237f35f
SR
1940 default:
1941 SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
1942 SCpnt->result = DID_ERROR << 16;
1da177e4
LT
1943 }
1944
e8ca5668
SR
1945 /* If a bus reset is in progress and there was an error, complete
1946 * the command as busy so that it will get retried. */
138c8af8 1947 if (!hpsb_node_entry_valid(lu->ne)
a237f35f 1948 && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
1da177e4
LT
1949 SBP2_ERR("Completing command with busy (bus reset)");
1950 SCpnt->result = DID_BUS_BUSY << 16;
1951 }
1952
e8ca5668 1953 /* Tell the SCSI stack that we're done with this command. */
a237f35f 1954 done(SCpnt);
1da177e4
LT
1955}
1956
abd559b1 1957static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
1da177e4 1958{
138c8af8 1959 struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
a80614d1 1960
138c8af8 1961 lu->sdev = sdev;
c394f1ea 1962 sdev->allow_restart = 1;
a80614d1 1963
465ff318
JB
1964 /*
1965 * Update the dma alignment (minimum alignment requirements for
1966 * start and end of DMA transfers) to be a sector
1967 */
1968 blk_queue_update_dma_alignment(sdev->request_queue, 511);
1969
138c8af8 1970 if (lu->workarounds & SBP2_WORKAROUND_INQUIRY_36)
a80614d1 1971 sdev->inquiry_len = 36;
abd559b1
JM
1972 return 0;
1973}
1974
abd559b1
JM
1975static int sbp2scsi_slave_configure(struct scsi_device *sdev)
1976{
138c8af8 1977 struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
24d3bf88 1978
365c786f 1979 sdev->use_10_for_rw = 1;
24d3bf88 1980
1a74bc68
SR
1981 if (sdev->type == TYPE_ROM)
1982 sdev->use_10_for_ms = 1;
24d3bf88 1983 if (sdev->type == TYPE_DISK &&
138c8af8 1984 lu->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
24d3bf88 1985 sdev->skip_ms_page_8 = 1;
138c8af8 1986 if (lu->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
e9a1c52c 1987 sdev->fix_capacity = 1;
1da177e4
LT
1988 return 0;
1989}
1990
abd559b1
JM
1991static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
1992{
138c8af8 1993 ((struct sbp2_lu *)sdev->host->hostdata[0])->sdev = NULL;
abd559b1
JM
1994 return;
1995}
1996
1da177e4 1997/*
e8ca5668
SR
1998 * Called by scsi stack when something has really gone wrong.
1999 * Usually called when a command has timed-out for some reason.
1da177e4
LT
2000 */
2001static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
2002{
138c8af8
SR
2003 struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
2004 struct sbp2_fwhost_info *hi = lu->hi;
2005 struct sbp2_command_info *cmd;
24c7cd06 2006 unsigned long flags;
1da177e4 2007
35644090 2008 SBP2_INFO("aborting sbp2 command");
1da177e4
LT
2009 scsi_print_command(SCpnt);
2010
138c8af8
SR
2011 if (sbp2util_node_is_available(lu)) {
2012 sbp2_agent_reset(lu, 1);
1da177e4 2013
23077f1d 2014 /* Return a matching command structure to the free pool. */
138c8af8
SR
2015 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
2016 cmd = sbp2util_find_command_for_SCpnt(lu, SCpnt);
2017 if (cmd) {
97d552e3 2018 dma_sync_single_for_cpu(hi->host->device.parent,
138c8af8
SR
2019 cmd->command_orb_dma,
2020 sizeof(struct sbp2_command_orb),
9b7d9c09 2021 DMA_TO_DEVICE);
97d552e3
SR
2022 dma_sync_single_for_cpu(hi->host->device.parent,
2023 cmd->sge_dma,
138c8af8 2024 sizeof(cmd->scatter_gather_element),
2446a79f 2025 DMA_TO_DEVICE);
138c8af8
SR
2026 sbp2util_mark_command_completed(lu, cmd);
2027 if (cmd->Current_SCpnt) {
2028 cmd->Current_SCpnt->result = DID_ABORT << 16;
2029 cmd->Current_done(cmd->Current_SCpnt);
1da177e4
LT
2030 }
2031 }
138c8af8 2032 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1da177e4 2033
138c8af8 2034 sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
1da177e4
LT
2035 }
2036
a237f35f 2037 return SUCCESS;
1da177e4
LT
2038}
2039
2040/*
2041 * Called by scsi stack when something has really gone wrong.
2042 */
abd559b1 2043static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
1da177e4 2044{
138c8af8 2045 struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
1da177e4 2046
35644090 2047 SBP2_INFO("reset requested");
1da177e4 2048
138c8af8 2049 if (sbp2util_node_is_available(lu)) {
35644090 2050 SBP2_INFO("generating sbp2 fetch agent reset");
138c8af8 2051 sbp2_agent_reset(lu, 1);
1da177e4
LT
2052 }
2053
abd559b1 2054 return SUCCESS;
94d0e7b8
JG
2055}
2056
a237f35f
SR
2057static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
2058 struct device_attribute *attr,
2059 char *buf)
1da177e4
LT
2060{
2061 struct scsi_device *sdev;
138c8af8 2062 struct sbp2_lu *lu;
1da177e4
LT
2063
2064 if (!(sdev = to_scsi_device(dev)))
2065 return 0;
2066
138c8af8 2067 if (!(lu = (struct sbp2_lu *)sdev->host->hostdata[0]))
1da177e4
LT
2068 return 0;
2069
d7794c86
SR
2070 if (sbp2_long_sysfs_ieee1394_id)
2071 return sprintf(buf, "%016Lx:%06x:%04x\n",
2072 (unsigned long long)lu->ne->guid,
2073 lu->ud->directory_id, ORB_SET_LUN(lu->lun));
2074 else
2075 return sprintf(buf, "%016Lx:%d:%d\n",
2076 (unsigned long long)lu->ne->guid,
2077 lu->ud->id, ORB_SET_LUN(lu->lun));
1da177e4 2078}
1da177e4
LT
2079
2080MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2081MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2082MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2083MODULE_LICENSE("GPL");
2084
1da177e4
LT
2085static int sbp2_module_init(void)
2086{
2087 int ret;
2088
ca0c7453
SR
2089 if (sbp2_serialize_io) {
2090 sbp2_shost_template.can_queue = 1;
2091 sbp2_shost_template.cmd_per_lun = 1;
1da177e4
LT
2092 }
2093
24d3bf88 2094 if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
ca0c7453
SR
2095 (sbp2_max_sectors * 512) > (128 * 1024))
2096 sbp2_max_sectors = 128 * 1024 / 512;
2097 sbp2_shost_template.max_sectors = sbp2_max_sectors;
1da177e4 2098
1da177e4 2099 hpsb_register_highlevel(&sbp2_highlevel);
1da177e4
LT
2100 ret = hpsb_register_protocol(&sbp2_driver);
2101 if (ret) {
2102 SBP2_ERR("Failed to register protocol");
2103 hpsb_unregister_highlevel(&sbp2_highlevel);
2104 return ret;
2105 }
1da177e4
LT
2106 return 0;
2107}
2108
2109static void __exit sbp2_module_exit(void)
2110{
1da177e4 2111 hpsb_unregister_protocol(&sbp2_driver);
1da177e4
LT
2112 hpsb_unregister_highlevel(&sbp2_highlevel);
2113}
2114
2115module_init(sbp2_module_init);
2116module_exit(sbp2_module_exit);