blob: 96a743d9b5d5df39874cba4c65a57f532a45ba2a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
32 * You may access any attached SBP-2 storage devices as if they were SCSI
33 * devices (e.g. mount /dev/sda1, fdisk, mkfs, etc.).
34 *
35 * Current Issues:
36 *
37 * - Error Handling: SCSI aborts and bus reset requests are handled somewhat
38 * but the code needs additional debugging.
39 */
40
Stefan Richter902abed2006-08-14 18:56:00 +020041#include <linux/blkdev.h>
42#include <linux/compiler.h>
43#include <linux/delay.h>
44#include <linux/device.h>
45#include <linux/dma-mapping.h>
46#include <linux/gfp.h>
47#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/kernel.h>
49#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/module.h>
51#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/pci.h>
Stefan Richter902abed2006-08-14 18:56:00 +020053#include <linux/slab.h>
54#include <linux/spinlock.h>
55#include <linux/stat.h>
56#include <linux/string.h>
57#include <linux/stringify.h>
58#include <linux/types.h>
Stefan Richtere8398bb2006-07-23 22:19:00 +020059#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/byteorder.h>
Stefan Richter902abed2006-08-14 18:56:00 +020062#include <asm/errno.h>
63#include <asm/param.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <asm/scatterlist.h>
Stefan Richter902abed2006-08-14 18:56:00 +020065#include <asm/system.h>
66#include <asm/types.h>
67
68#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
69#include <asm/io.h> /* for bus_to_virt */
70#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#include <scsi/scsi.h>
73#include <scsi/scsi_cmnd.h>
74#include <scsi/scsi_dbg.h>
75#include <scsi/scsi_device.h>
76#include <scsi/scsi_host.h>
77
78#include "csr1212.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include "highlevel.h"
Stefan Richter902abed2006-08-14 18:56:00 +020080#include "hosts.h"
81#include "ieee1394.h"
82#include "ieee1394_core.h"
83#include "ieee1394_hotplug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include "ieee1394_transactions.h"
Stefan Richter902abed2006-08-14 18:56:00 +020085#include "ieee1394_types.h"
86#include "nodemgr.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#include "sbp2.h"
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * Module load parameter definitions
91 */
92
93/*
94 * Change max_speed on module load if you have a bad IEEE-1394
95 * controller that has trouble running 2KB packets at 400mb.
96 *
97 * NOTE: On certain OHCI parts I have seen short packets on async transmit
98 * (probably due to PCI latency/throughput issues with the part). You can
99 * bump down the speed if you are running into problems.
100 */
101static int max_speed = IEEE1394_SPEED_MAX;
102module_param(max_speed, int, 0644);
Jody McIntyre2bab3592005-09-30 11:59:07 -0700103MODULE_PARM_DESC(max_speed, "Force max speed (3 = 800mb, 2 = 400mb, 1 = 200mb, 0 = 100mb)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/*
106 * Set serialize_io to 1 if you'd like only one scsi command sent
107 * down to us at a time (debugging). This might be necessary for very
108 * badly behaved sbp2 devices.
Jody McIntyre2bab3592005-09-30 11:59:07 -0700109 *
110 * TODO: Make this configurable per device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
Jody McIntyre2bab3592005-09-30 11:59:07 -0700112static int serialize_io = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113module_param(serialize_io, int, 0444);
Jody McIntyre2bab3592005-09-30 11:59:07 -0700114MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers (default = 1, faster = 0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/*
117 * Bump up max_sectors if you'd like to support very large sized
118 * transfers. Please note that some older sbp2 bridge chips are broken for
119 * transfers greater or equal to 128KB. Default is a value of 255
120 * sectors, or just under 128KB (at 512 byte sector size). I can note that
121 * the Oxsemi sbp2 chipsets have no problems supporting very large
122 * transfer sizes.
123 */
124static int max_sectors = SBP2_MAX_SECTORS;
125module_param(max_sectors, int, 0444);
Stefan Richter24d3bf82006-05-15 22:04:59 +0200126MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported (default = "
127 __stringify(SBP2_MAX_SECTORS) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/*
130 * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
131 * do an exclusive login, as it's generally unsafe to have two hosts
132 * talking to a single sbp2 device at the same time (filesystem coherency,
133 * etc.). If you're running an sbp2 device that supports multiple logins,
134 * and you're either running read-only filesystems or some sort of special
Ben Collins20f45782006-06-12 18:13:11 -0400135 * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster
136 * File System, or Lustre, then set exclusive_login to zero.
137 *
138 * So far only bridges from Oxford Semiconductor are known to support
139 * concurrent logins. Depending on firmware, four or two concurrent logins
140 * are possible on OXFW911 and newer Oxsemi bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 */
142static int exclusive_login = 1;
143module_param(exclusive_login, int, 0644);
144MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device (default = 1)");
145
146/*
Stefan Richter24d3bf82006-05-15 22:04:59 +0200147 * If any of the following workarounds is required for your device to work,
148 * please submit the kernel messages logged by sbp2 to the linux1394-devel
149 * mailing list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
Stefan Richter24d3bf82006-05-15 22:04:59 +0200151 * - 128kB max transfer
152 * Limit transfer size. Necessary for some old bridges.
153 *
154 * - 36 byte inquiry
155 * When scsi_mod probes the device, let the inquiry command look like that
156 * from MS Windows.
157 *
158 * - skip mode page 8
159 * Suppress sending of mode_sense for mode page 8 if the device pretends to
160 * support the SCSI Primary Block commands instead of Reduced Block Commands.
Stefan Richtere9a1c522006-05-15 22:06:37 +0200161 *
162 * - fix capacity
163 * Tell sd_mod to correct the last sector number reported by read_capacity.
164 * Avoids access beyond actual disk limits on devices with an off-by-one bug.
165 * Don't use this with devices which don't have this bug.
Stefan Richter679c0cd2006-05-15 22:08:09 +0200166 *
167 * - override internal blacklist
168 * Instead of adding to the built-in blacklist, use only the workarounds
169 * specified in the module load parameter.
170 * Useful if a blacklist entry interfered with a non-broken device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
Stefan Richter24d3bf82006-05-15 22:04:59 +0200172static int sbp2_default_workarounds;
173module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
174MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
175 ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
176 ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
177 ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
Stefan Richtere9a1c522006-05-15 22:06:37 +0200178 ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
Stefan Richter679c0cd2006-05-15 22:08:09 +0200179 ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
Stefan Richter24d3bf82006-05-15 22:04:59 +0200180 ", or a combination)");
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Stefan Richteredf1fb22006-11-02 21:16:08 +0100183#define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
184#define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186/*
187 * Globals
188 */
Stefan Richterea42ea02006-11-02 21:16:08 +0100189static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *, u32);
190static void sbp2scsi_complete_command(struct scsi_id_instance_data *, u32,
191 struct scsi_cmnd *,
192 void (*)(struct scsi_cmnd *));
193static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *);
194static int sbp2_start_device(struct scsi_id_instance_data *);
195static void sbp2_remove_device(struct scsi_id_instance_data *);
196static int sbp2_login_device(struct scsi_id_instance_data *);
197static int sbp2_reconnect_device(struct scsi_id_instance_data *);
198static int sbp2_logout_device(struct scsi_id_instance_data *);
199static void sbp2_host_reset(struct hpsb_host *);
200static int sbp2_handle_status_write(struct hpsb_host *, int, int, quadlet_t *,
201 u64, size_t, u16);
202static int sbp2_agent_reset(struct scsi_id_instance_data *, int);
203static void sbp2_parse_unit_directory(struct scsi_id_instance_data *,
204 struct unit_directory *);
205static int sbp2_set_busy_timeout(struct scsi_id_instance_data *);
206static int sbp2_max_speed_and_size(struct scsi_id_instance_data *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211static struct hpsb_highlevel sbp2_highlevel = {
Stefan Richterea42ea02006-11-02 21:16:08 +0100212 .name = SBP2_DEVICE_NAME,
213 .host_reset = sbp2_host_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214};
215
216static struct hpsb_address_ops sbp2_ops = {
Stefan Richterea42ea02006-11-02 21:16:08 +0100217 .write = sbp2_handle_status_write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218};
219
220#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
Stefan Richterea42ea02006-11-02 21:16:08 +0100221static int sbp2_handle_physdma_write(struct hpsb_host *, int, int, quadlet_t *,
222 u64, size_t, u16);
223static int sbp2_handle_physdma_read(struct hpsb_host *, int, quadlet_t *, u64,
224 size_t, u16);
225
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226static struct hpsb_address_ops sbp2_physdma_ops = {
Stefan Richterea42ea02006-11-02 21:16:08 +0100227 .read = sbp2_handle_physdma_read,
228 .write = sbp2_handle_physdma_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229};
230#endif
231
Stefan Richterea42ea02006-11-02 21:16:08 +0100232
233/*
234 * Interface to driver core and IEEE 1394 core
235 */
236static struct ieee1394_device_id sbp2_id_table[] = {
237 {
238 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
239 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
240 .version = SBP2_SW_VERSION_ENTRY & 0xffffff},
241 {}
242};
243MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
244
245static int sbp2_probe(struct device *);
246static int sbp2_remove(struct device *);
247static int sbp2_update(struct unit_directory *);
248
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249static struct hpsb_protocol_driver sbp2_driver = {
250 .name = "SBP2 Driver",
251 .id_table = sbp2_id_table,
252 .update = sbp2_update,
253 .driver = {
254 .name = SBP2_DEVICE_NAME,
255 .bus = &ieee1394_bus_type,
256 .probe = sbp2_probe,
257 .remove = sbp2_remove,
258 },
259};
260
Stefan Richterea42ea02006-11-02 21:16:08 +0100261
262/*
263 * Interface to SCSI core
264 */
265static int sbp2scsi_queuecommand(struct scsi_cmnd *,
266 void (*)(struct scsi_cmnd *));
267static int sbp2scsi_abort(struct scsi_cmnd *);
268static int sbp2scsi_reset(struct scsi_cmnd *);
269static int sbp2scsi_slave_alloc(struct scsi_device *);
270static int sbp2scsi_slave_configure(struct scsi_device *);
271static void sbp2scsi_slave_destroy(struct scsi_device *);
272static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *,
273 struct device_attribute *, char *);
274
275static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
276
277static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
278 &dev_attr_ieee1394_id,
279 NULL
280};
281
282static struct scsi_host_template scsi_driver_template = {
283 .module = THIS_MODULE,
284 .name = "SBP-2 IEEE-1394",
285 .proc_name = SBP2_DEVICE_NAME,
286 .queuecommand = sbp2scsi_queuecommand,
287 .eh_abort_handler = sbp2scsi_abort,
288 .eh_device_reset_handler = sbp2scsi_reset,
289 .slave_alloc = sbp2scsi_slave_alloc,
290 .slave_configure = sbp2scsi_slave_configure,
291 .slave_destroy = sbp2scsi_slave_destroy,
292 .this_id = -1,
293 .sg_tablesize = SG_ALL,
294 .use_clustering = ENABLE_CLUSTERING,
295 .cmd_per_lun = SBP2_MAX_CMDS,
296 .can_queue = SBP2_MAX_CMDS,
297 .emulated = 1,
298 .sdev_attrs = sbp2_sysfs_sdev_attrs,
299};
300
301
Stefan Richtera80614d2006-02-14 22:04:19 -0500302/*
Stefan Richter24d3bf82006-05-15 22:04:59 +0200303 * List of devices with known bugs.
304 *
305 * The firmware_revision field, masked with 0xffff00, is the best indicator
306 * for the type of bridge chip of a device. It yields a few false positives
307 * but this did not break correctly behaving devices so far.
Stefan Richtera80614d2006-02-14 22:04:19 -0500308 */
Stefan Richter24d3bf82006-05-15 22:04:59 +0200309static const struct {
310 u32 firmware_revision;
Stefan Richtere9a1c522006-05-15 22:06:37 +0200311 u32 model_id;
Stefan Richter24d3bf82006-05-15 22:04:59 +0200312 unsigned workarounds;
313} sbp2_workarounds_table[] = {
Ben Collins4b9a3342006-06-12 18:10:18 -0400314 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
Stefan Richter24d3bf82006-05-15 22:04:59 +0200315 .firmware_revision = 0x002800,
Ben Collins4b9a3342006-06-12 18:10:18 -0400316 .model_id = 0x001010,
Stefan Richter24d3bf82006-05-15 22:04:59 +0200317 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
318 SBP2_WORKAROUND_MODE_SENSE_8,
319 },
320 /* Initio bridges, actually only needed for some older ones */ {
321 .firmware_revision = 0x000200,
322 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
323 },
324 /* Symbios bridge */ {
325 .firmware_revision = 0xa0b800,
326 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
Stefan Richtere9a1c522006-05-15 22:06:37 +0200327 },
328 /*
329 * Note about the following Apple iPod blacklist entries:
330 *
331 * There are iPods (2nd gen, 3rd gen) with model_id==0. Since our
332 * matching logic treats 0 as a wildcard, we cannot match this ID
333 * without rewriting the matching routine. Fortunately these iPods
334 * do not feature the read_capacity bug according to one report.
335 * Read_capacity behaviour as well as model_id could change due to
336 * Apple-supplied firmware updates though.
337 */
338 /* iPod 4th generation */ {
339 .firmware_revision = 0x0a2700,
340 .model_id = 0x000021,
341 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
342 },
343 /* iPod mini */ {
344 .firmware_revision = 0x0a2700,
345 .model_id = 0x000023,
346 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
347 },
348 /* iPod Photo */ {
349 .firmware_revision = 0x0a2700,
350 .model_id = 0x00007e,
351 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
Stefan Richter24d3bf82006-05-15 22:04:59 +0200352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353};
354
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355/**************************************
356 * General utility functions
357 **************************************/
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359#ifndef __BIG_ENDIAN
360/*
361 * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
362 */
Stefan Richter2b01b802006-07-03 12:02:28 -0400363static inline void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
365 u32 *temp = buffer;
366
367 for (length = (length >> 2); length--; )
368 temp[length] = be32_to_cpu(temp[length]);
369
370 return;
371}
372
373/*
374 * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
375 */
Stefan Richter2b01b802006-07-03 12:02:28 -0400376static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 u32 *temp = buffer;
379
380 for (length = (length >> 2); length--; )
381 temp[length] = cpu_to_be32(temp[length]);
382
383 return;
384}
385#else /* BIG_ENDIAN */
386/* Why waste the cpu cycles? */
Stefan Richter611aa192006-08-02 18:44:00 +0200387#define sbp2util_be32_to_cpu_buffer(x,y) do {} while (0)
388#define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389#endif
390
Stefan Richtere8398bb2006-07-23 22:19:00 +0200391static DECLARE_WAIT_QUEUE_HEAD(access_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Stefan Richtere8398bb2006-07-23 22:19:00 +0200393/*
394 * Waits for completion of an SBP-2 access request.
395 * Returns nonzero if timed out or prematurely interrupted.
396 */
397static int sbp2util_access_timeout(struct scsi_id_instance_data *scsi_id,
398 int timeout)
399{
400 long leftover = wait_event_interruptible_timeout(
401 access_wq, scsi_id->access_complete, timeout);
402
403 scsi_id->access_complete = 0;
404 return leftover <= 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407static void sbp2_free_packet(struct hpsb_packet *packet)
408{
409 hpsb_free_tlabel(packet);
410 hpsb_free_packet(packet);
411}
412
Stefan Richtere8ca5662006-11-02 21:16:08 +0100413/*
414 * This is much like hpsb_node_write(), except it ignores the response
415 * subaction and returns immediately. Can be used from atomic context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 */
417static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
Stefan Richtera237f352005-11-07 06:31:39 -0500418 quadlet_t *buffer, size_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 struct hpsb_packet *packet;
421
422 packet = hpsb_make_writepacket(ne->host, ne->nodeid,
423 addr, buffer, length);
Stefan Richtera237f352005-11-07 06:31:39 -0500424 if (!packet)
425 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Stefan Richtera237f352005-11-07 06:31:39 -0500427 hpsb_set_packet_complete_task(packet,
428 (void (*)(void *))sbp2_free_packet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 packet);
430
431 hpsb_node_fill_packet(ne, packet);
432
Stefan Richtera237f352005-11-07 06:31:39 -0500433 if (hpsb_send_packet(packet) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 sbp2_free_packet(packet);
435 return -EIO;
436 }
437
438 return 0;
439}
440
Stefan Richter09ee67a2006-08-14 18:43:00 +0200441static void sbp2util_notify_fetch_agent(struct scsi_id_instance_data *scsi_id,
442 u64 offset, quadlet_t *data, size_t len)
443{
444 /*
445 * There is a small window after a bus reset within which the node
446 * entry's generation is current but the reconnect wasn't completed.
447 */
Stefan Richter2cccbb52006-08-14 18:59:00 +0200448 if (unlikely(atomic_read(&scsi_id->state) == SBP2LU_STATE_IN_RESET))
Stefan Richter09ee67a2006-08-14 18:43:00 +0200449 return;
450
451 if (hpsb_node_write(scsi_id->ne,
452 scsi_id->sbp2_command_block_agent_addr + offset,
453 data, len))
454 SBP2_ERR("sbp2util_notify_fetch_agent failed.");
455 /*
456 * Now accept new SCSI commands, unless a bus reset happended during
457 * hpsb_node_write.
458 */
Stefan Richter2cccbb52006-08-14 18:59:00 +0200459 if (likely(atomic_read(&scsi_id->state) != SBP2LU_STATE_IN_RESET))
Stefan Richter09ee67a2006-08-14 18:43:00 +0200460 scsi_unblock_requests(scsi_id->scsi_host);
461}
462
David Howellsc4028952006-11-22 14:57:56 +0000463static void sbp2util_write_orb_pointer(struct work_struct *work)
Stefan Richter09ee67a2006-08-14 18:43:00 +0200464{
465 quadlet_t data[2];
466
Stefan Richterd19c7762006-12-07 22:40:33 +0100467 data[0] = ORB_SET_NODE_ID(
468 (container_of(work, struct scsi_id_instance_data, protocol_work))->hi->host->node_id);
469 data[1] = (container_of(work, struct scsi_id_instance_data, protocol_work))->last_orb_dma;
Stefan Richter09ee67a2006-08-14 18:43:00 +0200470 sbp2util_cpu_to_be32_buffer(data, 8);
Stefan Richterd19c7762006-12-07 22:40:33 +0100471 sbp2util_notify_fetch_agent(container_of(work, struct scsi_id_instance_data, protocol_work), SBP2_ORB_POINTER_OFFSET, data, 8);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200472}
473
David Howellsc4028952006-11-22 14:57:56 +0000474static void sbp2util_write_doorbell(struct work_struct *work)
Stefan Richter09ee67a2006-08-14 18:43:00 +0200475{
Stefan Richterd19c7762006-12-07 22:40:33 +0100476 sbp2util_notify_fetch_agent(container_of(work, struct scsi_id_instance_data, protocol_work), SBP2_DOORBELL_OFFSET, NULL, 4);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id)
480{
481 struct sbp2scsi_host_info *hi = scsi_id->hi;
482 int i;
483 unsigned long flags, orbs;
484 struct sbp2_command_info *command;
485
486 orbs = serialize_io ? 2 : SBP2_MAX_CMDS;
487
488 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
489 for (i = 0; i < orbs; i++) {
Stefan Richter85511582005-11-07 06:31:45 -0500490 command = kzalloc(sizeof(*command), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 if (!command) {
Stefan Richtera237f352005-11-07 06:31:39 -0500492 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock,
493 flags);
494 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 command->command_orb_dma =
Stefan Richtera237f352005-11-07 06:31:39 -0500497 pci_map_single(hi->host->pdev, &command->command_orb,
498 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +0200499 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 command->sge_dma =
Stefan Richtera237f352005-11-07 06:31:39 -0500501 pci_map_single(hi->host->pdev,
502 &command->scatter_gather_element,
503 sizeof(command->scatter_gather_element),
504 PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 INIT_LIST_HEAD(&command->list);
506 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
507 }
508 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
509 return 0;
510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id)
513{
514 struct hpsb_host *host = scsi_id->hi->host;
515 struct list_head *lh, *next;
516 struct sbp2_command_info *command;
517 unsigned long flags;
518
519 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
520 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
521 list_for_each_safe(lh, next, &scsi_id->sbp2_command_orb_completed) {
522 command = list_entry(lh, struct sbp2_command_info, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 pci_unmap_single(host->pdev, command->command_orb_dma,
524 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +0200525 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 pci_unmap_single(host->pdev, command->sge_dma,
527 sizeof(command->scatter_gather_element),
528 PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 kfree(command);
530 }
531 }
532 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
533 return;
534}
535
536/*
Stefan Richtere8ca5662006-11-02 21:16:08 +0100537 * Finds the sbp2_command for a given outstanding command ORB.
538 * Only looks at the in-use list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 */
540static struct sbp2_command_info *sbp2util_find_command_for_orb(
541 struct scsi_id_instance_data *scsi_id, dma_addr_t orb)
542{
543 struct sbp2_command_info *command;
544 unsigned long flags;
545
546 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
547 if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
548 list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list) {
549 if (command->command_orb_dma == orb) {
550 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Stefan Richtera237f352005-11-07 06:31:39 -0500551 return command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
553 }
554 }
555 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Stefan Richtera237f352005-11-07 06:31:39 -0500556 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
558
559/*
Stefan Richtere8ca5662006-11-02 21:16:08 +0100560 * Finds the sbp2_command for a given outstanding SCpnt.
561 * Only looks at the in-use list.
Stefan Richter24c7cd02006-04-01 21:11:41 +0200562 * Must be called with scsi_id->sbp2_command_orb_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 */
Stefan Richter24c7cd02006-04-01 21:11:41 +0200564static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
565 struct scsi_id_instance_data *scsi_id, void *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
567 struct sbp2_command_info *command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Stefan Richter24c7cd02006-04-01 21:11:41 +0200569 if (!list_empty(&scsi_id->sbp2_command_orb_inuse))
570 list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list)
571 if (command->Current_SCpnt == SCpnt)
Stefan Richtera237f352005-11-07 06:31:39 -0500572 return command;
Stefan Richtera237f352005-11-07 06:31:39 -0500573 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574}
575
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576static struct sbp2_command_info *sbp2util_allocate_command_orb(
577 struct scsi_id_instance_data *scsi_id,
578 struct scsi_cmnd *Current_SCpnt,
579 void (*Current_done)(struct scsi_cmnd *))
580{
581 struct list_head *lh;
582 struct sbp2_command_info *command = NULL;
583 unsigned long flags;
584
585 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
586 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
587 lh = scsi_id->sbp2_command_orb_completed.next;
588 list_del(lh);
589 command = list_entry(lh, struct sbp2_command_info, list);
590 command->Current_done = Current_done;
591 command->Current_SCpnt = Current_SCpnt;
592 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_inuse);
593 } else {
Stefan Richterd024ebc2006-03-28 20:03:55 -0500594 SBP2_ERR("%s: no orbs available", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 }
596 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Stefan Richtera237f352005-11-07 06:31:39 -0500597 return command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598}
599
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600static void sbp2util_free_command_dma(struct sbp2_command_info *command)
601{
602 struct scsi_id_instance_data *scsi_id =
603 (struct scsi_id_instance_data *)command->Current_SCpnt->device->host->hostdata[0];
604 struct hpsb_host *host;
605
606 if (!scsi_id) {
Stefan Richterd024ebc2006-03-28 20:03:55 -0500607 SBP2_ERR("%s: scsi_id == NULL", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 return;
609 }
610
611 host = scsi_id->ud->ne->host;
612
613 if (command->cmd_dma) {
Stefan Richteredf1fb22006-11-02 21:16:08 +0100614 if (command->dma_type == CMD_DMA_SINGLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 pci_unmap_single(host->pdev, command->cmd_dma,
616 command->dma_size, command->dma_dir);
Stefan Richteredf1fb22006-11-02 21:16:08 +0100617 else if (command->dma_type == CMD_DMA_PAGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 pci_unmap_page(host->pdev, command->cmd_dma,
619 command->dma_size, command->dma_dir);
Stefan Richteredf1fb22006-11-02 21:16:08 +0100620 /* XXX: Check for CMD_DMA_NONE bug */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 command->dma_type = CMD_DMA_NONE;
622 command->cmd_dma = 0;
623 }
624
625 if (command->sge_buffer) {
626 pci_unmap_sg(host->pdev, command->sge_buffer,
627 command->dma_size, command->dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 command->sge_buffer = NULL;
629 }
630}
631
632/*
633 * This function moves a command to the completed orb list.
Stefan Richter24c7cd02006-04-01 21:11:41 +0200634 * Must be called with scsi_id->sbp2_command_orb_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 */
Stefan Richter24c7cd02006-04-01 21:11:41 +0200636static void sbp2util_mark_command_completed(
637 struct scsi_id_instance_data *scsi_id,
638 struct sbp2_command_info *command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 list_del(&command->list);
641 sbp2util_free_command_dma(command);
642 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
Jody McIntyreabd559b2005-09-30 11:59:06 -0700645/*
646 * Is scsi_id valid? Is the 1394 node still present?
647 */
648static inline int sbp2util_node_is_available(struct scsi_id_instance_data *scsi_id)
649{
650 return scsi_id && scsi_id->ne && !scsi_id->ne->in_limbo;
651}
652
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653/*********************************************
654 * IEEE-1394 core driver stack related section
655 *********************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
657static int sbp2_probe(struct device *dev)
658{
659 struct unit_directory *ud;
660 struct scsi_id_instance_data *scsi_id;
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 ud = container_of(dev, struct unit_directory, device);
663
664 /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
665 * instead. */
666 if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
667 return -ENODEV;
668
Stefan Richtera237f352005-11-07 06:31:39 -0500669 scsi_id = sbp2_alloc_device(ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Stefan Richtera237f352005-11-07 06:31:39 -0500671 if (!scsi_id)
672 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Stefan Richtera237f352005-11-07 06:31:39 -0500674 sbp2_parse_unit_directory(scsi_id, ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Stefan Richtera237f352005-11-07 06:31:39 -0500676 return sbp2_start_device(scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679static int sbp2_remove(struct device *dev)
680{
681 struct unit_directory *ud;
682 struct scsi_id_instance_data *scsi_id;
Jody McIntyreabd559b2005-09-30 11:59:06 -0700683 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 ud = container_of(dev, struct unit_directory, device);
686 scsi_id = ud->device.driver_data;
Jody McIntyreabd559b2005-09-30 11:59:06 -0700687 if (!scsi_id)
688 return 0;
689
Stefan Richterbf637ec2006-01-31 00:13:06 -0500690 if (scsi_id->scsi_host) {
691 /* Get rid of enqueued commands if there is no chance to
692 * send them. */
693 if (!sbp2util_node_is_available(scsi_id))
694 sbp2scsi_complete_all_commands(scsi_id, DID_NO_CONNECT);
Stefan Richtere8ca5662006-11-02 21:16:08 +0100695 /* scsi_remove_device() may trigger shutdown functions of SCSI
Stefan Richterbf637ec2006-01-31 00:13:06 -0500696 * highlevel drivers which would deadlock if blocked. */
Stefan Richter2cccbb52006-08-14 18:59:00 +0200697 atomic_set(&scsi_id->state, SBP2LU_STATE_IN_SHUTDOWN);
Jody McIntyreabd559b2005-09-30 11:59:06 -0700698 scsi_unblock_requests(scsi_id->scsi_host);
Stefan Richterbf637ec2006-01-31 00:13:06 -0500699 }
Jody McIntyreabd559b2005-09-30 11:59:06 -0700700 sdev = scsi_id->sdev;
701 if (sdev) {
702 scsi_id->sdev = NULL;
703 scsi_remove_device(sdev);
704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706 sbp2_logout_device(scsi_id);
707 sbp2_remove_device(scsi_id);
708
709 return 0;
710}
711
712static int sbp2_update(struct unit_directory *ud)
713{
714 struct scsi_id_instance_data *scsi_id = ud->device.driver_data;
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (sbp2_reconnect_device(scsi_id)) {
Stefan Richtere8ca5662006-11-02 21:16:08 +0100717 /* Reconnect has failed. Perhaps we didn't reconnect fast
718 * enough. Try a regular login, but first log out just in
719 * case of any weirdness. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 sbp2_logout_device(scsi_id);
721
722 if (sbp2_login_device(scsi_id)) {
723 /* Login failed too, just fail, and the backend
724 * will call our sbp2_remove for us */
725 SBP2_ERR("Failed to reconnect to sbp2 device!");
726 return -EBUSY;
727 }
728 }
729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 sbp2_set_busy_timeout(scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 sbp2_agent_reset(scsi_id, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 sbp2_max_speed_and_size(scsi_id);
733
Stefan Richtere8ca5662006-11-02 21:16:08 +0100734 /* Complete any pending commands with busy (so they get retried)
735 * and remove them from our queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
737
Stefan Richter4fc383c2006-08-14 18:46:00 +0200738 /* Accept new commands unless there was another bus reset in the
739 * meantime. */
740 if (hpsb_node_entry_valid(scsi_id->ne)) {
Stefan Richter2cccbb52006-08-14 18:59:00 +0200741 atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
Stefan Richter4fc383c2006-08-14 18:46:00 +0200742 scsi_unblock_requests(scsi_id->scsi_host);
743 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return 0;
745}
746
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud)
748{
749 struct sbp2scsi_host_info *hi;
750 struct Scsi_Host *scsi_host = NULL;
751 struct scsi_id_instance_data *scsi_id = NULL;
752
Stefan Richter85511582005-11-07 06:31:45 -0500753 scsi_id = kzalloc(sizeof(*scsi_id), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (!scsi_id) {
755 SBP2_ERR("failed to create scsi_id");
756 goto failed_alloc;
757 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 scsi_id->ne = ud->ne;
760 scsi_id->ud = ud;
761 scsi_id->speed_code = IEEE1394_SPEED_100;
762 scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
Ben Collins67372312006-06-12 18:15:31 -0400763 scsi_id->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_inuse);
765 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_completed);
766 INIT_LIST_HEAD(&scsi_id->scsi_list);
767 spin_lock_init(&scsi_id->sbp2_command_orb_lock);
Stefan Richter2cccbb52006-08-14 18:59:00 +0200768 atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
Stefan Richterd19c7762006-12-07 22:40:33 +0100769 INIT_WORK(&scsi_id->protocol_work, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 ud->device.driver_data = scsi_id;
772
773 hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
774 if (!hi) {
775 hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host, sizeof(*hi));
776 if (!hi) {
777 SBP2_ERR("failed to allocate hostinfo");
778 goto failed_alloc;
779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 hi->host = ud->ne->host;
781 INIT_LIST_HEAD(&hi->scsi_ids);
782
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
784 /* Handle data movement if physical dma is not
Stefan Richter55664052006-03-28 19:59:42 -0500785 * enabled or not supported on host controller */
786 if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
787 &sbp2_physdma_ops,
788 0x0ULL, 0xfffffffcULL)) {
789 SBP2_ERR("failed to register lower 4GB address range");
790 goto failed_alloc;
791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792#endif
793 }
794
Stefan Richter147830f2006-03-28 19:54:52 -0500795 /* Prevent unloading of the 1394 host */
796 if (!try_module_get(hi->host->driver->owner)) {
797 SBP2_ERR("failed to get a reference on 1394 host driver");
798 goto failed_alloc;
799 }
800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 scsi_id->hi = hi;
802
803 list_add_tail(&scsi_id->scsi_list, &hi->scsi_ids);
804
Stefan Richter35bdddb2006-01-31 00:13:33 -0500805 /* Register the status FIFO address range. We could use the same FIFO
806 * for targets at different nodes. However we need different FIFOs per
Stefan Richtera54c9d32006-05-15 22:09:46 +0200807 * target in order to support multi-unit devices.
808 * The FIFO is located out of the local host controller's physical range
809 * but, if possible, within the posted write area. Status writes will
810 * then be performed as unified transactions. This slightly reduces
811 * bandwidth usage, and some Prolific based devices seem to require it.
812 */
Stefan Richter35bdddb2006-01-31 00:13:33 -0500813 scsi_id->status_fifo_addr = hpsb_allocate_and_register_addrspace(
814 &sbp2_highlevel, ud->ne->host, &sbp2_ops,
815 sizeof(struct sbp2_status_block), sizeof(quadlet_t),
Ben Collins40ae6c52006-06-12 18:13:49 -0400816 ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
Ben Collins67372312006-06-12 18:15:31 -0400817 if (scsi_id->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
Stefan Richter35bdddb2006-01-31 00:13:33 -0500818 SBP2_ERR("failed to allocate status FIFO address range");
819 goto failed_alloc;
820 }
821
Alexandre Olivaa2ef79e2005-06-15 22:26:31 -0700822 scsi_host = scsi_host_alloc(&scsi_driver_template,
Stefan Richtera237f352005-11-07 06:31:39 -0500823 sizeof(unsigned long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (!scsi_host) {
825 SBP2_ERR("failed to register scsi host");
826 goto failed_alloc;
827 }
828
829 scsi_host->hostdata[0] = (unsigned long)scsi_id;
830
831 if (!scsi_add_host(scsi_host, &ud->device)) {
832 scsi_id->scsi_host = scsi_host;
833 return scsi_id;
834 }
835
836 SBP2_ERR("failed to add scsi host");
837 scsi_host_put(scsi_host);
838
839failed_alloc:
840 sbp2_remove_device(scsi_id);
841 return NULL;
842}
843
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844static void sbp2_host_reset(struct hpsb_host *host)
845{
846 struct sbp2scsi_host_info *hi;
847 struct scsi_id_instance_data *scsi_id;
848
849 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
Stefan Richter2cccbb52006-08-14 18:59:00 +0200850 if (!hi)
851 return;
852 list_for_each_entry(scsi_id, &hi->scsi_ids, scsi_list)
853 if (likely(atomic_read(&scsi_id->state) !=
854 SBP2LU_STATE_IN_SHUTDOWN)) {
855 atomic_set(&scsi_id->state, SBP2LU_STATE_IN_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 scsi_block_requests(scsi_id->scsi_host);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858}
859
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
861{
862 struct sbp2scsi_host_info *hi = scsi_id->hi;
James Bottomley146f7262005-09-10 12:44:09 -0500863 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 scsi_id->login_response =
Stefan Richtera237f352005-11-07 06:31:39 -0500866 pci_alloc_consistent(hi->host->pdev,
867 sizeof(struct sbp2_login_response),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 &scsi_id->login_response_dma);
869 if (!scsi_id->login_response)
870 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 scsi_id->query_logins_orb =
Stefan Richtera237f352005-11-07 06:31:39 -0500873 pci_alloc_consistent(hi->host->pdev,
874 sizeof(struct sbp2_query_logins_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 &scsi_id->query_logins_orb_dma);
876 if (!scsi_id->query_logins_orb)
877 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 scsi_id->query_logins_response =
Stefan Richtera237f352005-11-07 06:31:39 -0500880 pci_alloc_consistent(hi->host->pdev,
881 sizeof(struct sbp2_query_logins_response),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 &scsi_id->query_logins_response_dma);
883 if (!scsi_id->query_logins_response)
884 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 scsi_id->reconnect_orb =
Stefan Richtera237f352005-11-07 06:31:39 -0500887 pci_alloc_consistent(hi->host->pdev,
888 sizeof(struct sbp2_reconnect_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 &scsi_id->reconnect_orb_dma);
890 if (!scsi_id->reconnect_orb)
891 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 scsi_id->logout_orb =
Stefan Richtera237f352005-11-07 06:31:39 -0500894 pci_alloc_consistent(hi->host->pdev,
895 sizeof(struct sbp2_logout_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 &scsi_id->logout_orb_dma);
897 if (!scsi_id->logout_orb)
898 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 scsi_id->login_orb =
Stefan Richtera237f352005-11-07 06:31:39 -0500901 pci_alloc_consistent(hi->host->pdev,
902 sizeof(struct sbp2_login_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 &scsi_id->login_orb_dma);
Stefan Richtereaceec72005-12-13 11:05:05 -0500904 if (!scsi_id->login_orb)
905 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (sbp2util_create_command_orb_pool(scsi_id)) {
908 SBP2_ERR("sbp2util_create_command_orb_pool failed!");
909 sbp2_remove_device(scsi_id);
910 return -ENOMEM;
911 }
912
Stefan Richtere8ca5662006-11-02 21:16:08 +0100913 /* Wait a second before trying to log in. Previously logged in
914 * initiators need a chance to reconnect. */
Stefan Richter902abed2006-08-14 18:56:00 +0200915 if (msleep_interruptible(1000)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 sbp2_remove_device(scsi_id);
917 return -EINTR;
918 }
Stefan Richtera237f352005-11-07 06:31:39 -0500919
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (sbp2_login_device(scsi_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 sbp2_remove_device(scsi_id);
922 return -EBUSY;
923 }
924
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 sbp2_set_busy_timeout(scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 sbp2_agent_reset(scsi_id, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 sbp2_max_speed_and_size(scsi_id);
928
James Bottomley146f7262005-09-10 12:44:09 -0500929 error = scsi_add_device(scsi_id->scsi_host, 0, scsi_id->ud->id, 0);
930 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 SBP2_ERR("scsi_add_device failed");
Stefan Richterdc3edd52005-12-12 23:03:30 -0500932 sbp2_logout_device(scsi_id);
933 sbp2_remove_device(scsi_id);
James Bottomley146f7262005-09-10 12:44:09 -0500934 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936
937 return 0;
Stefan Richtereaceec72005-12-13 11:05:05 -0500938
939alloc_fail:
940 SBP2_ERR("Could not allocate memory for scsi_id");
941 sbp2_remove_device(scsi_id);
942 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943}
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
946{
947 struct sbp2scsi_host_info *hi;
948
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (!scsi_id)
950 return;
951
952 hi = scsi_id->hi;
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 if (scsi_id->scsi_host) {
955 scsi_remove_host(scsi_id->scsi_host);
956 scsi_host_put(scsi_id->scsi_host);
957 }
Stefan Richter09ee67a2006-08-14 18:43:00 +0200958 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 sbp2util_remove_command_orb_pool(scsi_id);
960
961 list_del(&scsi_id->scsi_list);
962
Stefan Richteredf1fb22006-11-02 21:16:08 +0100963 if (scsi_id->login_response)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 pci_free_consistent(hi->host->pdev,
965 sizeof(struct sbp2_login_response),
966 scsi_id->login_response,
967 scsi_id->login_response_dma);
Stefan Richteredf1fb22006-11-02 21:16:08 +0100968 if (scsi_id->login_orb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 pci_free_consistent(hi->host->pdev,
970 sizeof(struct sbp2_login_orb),
971 scsi_id->login_orb,
972 scsi_id->login_orb_dma);
Stefan Richteredf1fb22006-11-02 21:16:08 +0100973 if (scsi_id->reconnect_orb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 pci_free_consistent(hi->host->pdev,
975 sizeof(struct sbp2_reconnect_orb),
976 scsi_id->reconnect_orb,
977 scsi_id->reconnect_orb_dma);
Stefan Richteredf1fb22006-11-02 21:16:08 +0100978 if (scsi_id->logout_orb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 pci_free_consistent(hi->host->pdev,
980 sizeof(struct sbp2_logout_orb),
981 scsi_id->logout_orb,
982 scsi_id->logout_orb_dma);
Stefan Richteredf1fb22006-11-02 21:16:08 +0100983 if (scsi_id->query_logins_orb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 pci_free_consistent(hi->host->pdev,
985 sizeof(struct sbp2_query_logins_orb),
986 scsi_id->query_logins_orb,
987 scsi_id->query_logins_orb_dma);
Stefan Richteredf1fb22006-11-02 21:16:08 +0100988 if (scsi_id->query_logins_response)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 pci_free_consistent(hi->host->pdev,
990 sizeof(struct sbp2_query_logins_response),
991 scsi_id->query_logins_response,
992 scsi_id->query_logins_response_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Ben Collins67372312006-06-12 18:15:31 -0400994 if (scsi_id->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
Stefan Richter35bdddb2006-01-31 00:13:33 -0500995 hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
Ben Collins67372312006-06-12 18:15:31 -0400996 scsi_id->status_fifo_addr);
Stefan Richter35bdddb2006-01-31 00:13:33 -0500997
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 scsi_id->ud->device.driver_data = NULL;
999
Stefan Richter147830f2006-03-28 19:54:52 -05001000 if (hi)
1001 module_put(hi->host->driver->owner);
1002
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 kfree(scsi_id);
1004}
1005
1006#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
1007/*
Stefan Richtere8ca5662006-11-02 21:16:08 +01001008 * Deal with write requests on adapters which do not support physical DMA or
1009 * have it switched off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 */
Stefan Richtera237f352005-11-07 06:31:39 -05001011static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
1012 int destid, quadlet_t *data, u64 addr,
1013 size_t length, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
Stefan Richtera237f352005-11-07 06:31:39 -05001015 memcpy(bus_to_virt((u32) addr), data, length);
Stefan Richtera237f352005-11-07 06:31:39 -05001016 return RCODE_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019/*
Stefan Richtere8ca5662006-11-02 21:16:08 +01001020 * Deal with read requests on adapters which do not support physical DMA or
1021 * have it switched off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 */
Stefan Richtera237f352005-11-07 06:31:39 -05001023static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
1024 quadlet_t *data, u64 addr, size_t length,
1025 u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Stefan Richtera237f352005-11-07 06:31:39 -05001027 memcpy(data, bus_to_virt((u32) addr), length);
Stefan Richtera237f352005-11-07 06:31:39 -05001028 return RCODE_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030#endif
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032/**************************************
1033 * SBP-2 protocol related section
1034 **************************************/
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
1037{
1038 struct sbp2scsi_host_info *hi = scsi_id->hi;
1039 quadlet_t data[2];
1040 int max_logins;
1041 int active_logins;
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 scsi_id->query_logins_orb->reserved1 = 0x0;
1044 scsi_id->query_logins_orb->reserved2 = 0x0;
1045
1046 scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma;
1047 scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
1049 scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
1050 scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
Ben Collinse309fc62005-11-07 06:31:34 -05001051 scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 scsi_id->query_logins_orb->reserved_resp_length =
1054 ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Stefan Richter35bdddb2006-01-31 00:13:33 -05001056 scsi_id->query_logins_orb->status_fifo_hi =
1057 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1058 scsi_id->query_logins_orb->status_fifo_lo =
1059 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb));
1062
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1066 data[1] = scsi_id->query_logins_orb_dma;
1067 sbp2util_cpu_to_be32_buffer(data, 8);
1068
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Stefan Richtere8398bb2006-07-23 22:19:00 +02001071 if (sbp2util_access_timeout(scsi_id, 2*HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001073 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 }
1075
1076 if (scsi_id->status_block.ORB_offset_lo != scsi_id->query_logins_orb_dma) {
1077 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001078 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080
Stefan Richter60657722006-07-23 22:18:00 +02001081 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1082 SBP2_INFO("Error querying logins to SBP-2 device - failed");
Stefan Richtera237f352005-11-07 06:31:39 -05001083 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 }
1085
1086 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_response, sizeof(struct sbp2_query_logins_response));
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins);
Ben Collins20f45782006-06-12 18:13:11 -04001089 SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090
1091 active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins);
Ben Collins20f45782006-06-12 18:13:11 -04001092 SBP2_INFO("Number of active logins: %d", active_logins);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 if (active_logins >= max_logins) {
Stefan Richtera237f352005-11-07 06:31:39 -05001095 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 }
1097
1098 return 0;
1099}
1100
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
1102{
1103 struct sbp2scsi_host_info *hi = scsi_id->hi;
1104 quadlet_t data[2];
1105
Stefan Richteredf1fb22006-11-02 21:16:08 +01001106 if (!scsi_id->login_orb)
Stefan Richtera237f352005-11-07 06:31:39 -05001107 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108
1109 if (!exclusive_login) {
1110 if (sbp2_query_logins(scsi_id)) {
1111 SBP2_INFO("Device does not support any more concurrent logins");
Stefan Richtera237f352005-11-07 06:31:39 -05001112 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
1114 }
1115
Stefan Richtere8ca5662006-11-02 21:16:08 +01001116 /* assume no password */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 scsi_id->login_orb->password_hi = 0;
1118 scsi_id->login_orb->password_lo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
1120 scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
1121 scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
Stefan Richtere8ca5662006-11-02 21:16:08 +01001123
1124 /* one second reconnect time */
1125 scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0);
1126 scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(exclusive_login);
1127 scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1);
Ben Collinse309fc62005-11-07 06:31:34 -05001128 scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 scsi_id->login_orb->passwd_resp_lengths =
1131 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
Stefan Richter35bdddb2006-01-31 00:13:33 -05001133 scsi_id->login_orb->status_fifo_hi =
1134 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1135 scsi_id->login_orb->status_fifo_lo =
1136 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1143 data[1] = scsi_id->login_orb_dma;
1144 sbp2util_cpu_to_be32_buffer(data, 8);
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Stefan Richtere8ca5662006-11-02 21:16:08 +01001148 /* wait up to 20 seconds for login status */
Stefan Richtere8398bb2006-07-23 22:19:00 +02001149 if (sbp2util_access_timeout(scsi_id, 20*HZ)) {
1150 SBP2_ERR("Error logging into SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001151 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 }
1153
Stefan Richtere8ca5662006-11-02 21:16:08 +01001154 /* make sure that the returned status matches the login ORB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
Stefan Richter60657722006-07-23 22:18:00 +02001156 SBP2_ERR("Error logging into SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001157 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159
Stefan Richter60657722006-07-23 22:18:00 +02001160 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1161 SBP2_ERR("Error logging into SBP-2 device - failed");
Stefan Richtera237f352005-11-07 06:31:39 -05001162 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
1164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 scsi_id->sbp2_command_block_agent_addr =
1167 ((u64)scsi_id->login_response->command_block_agent_hi) << 32;
1168 scsi_id->sbp2_command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
1169 scsi_id->sbp2_command_block_agent_addr &= 0x0000ffffffffffffULL;
1170
1171 SBP2_INFO("Logged into SBP-2 device");
Stefan Richtera237f352005-11-07 06:31:39 -05001172 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
1176{
1177 struct sbp2scsi_host_info *hi = scsi_id->hi;
1178 quadlet_t data[2];
1179 int error;
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 scsi_id->logout_orb->reserved1 = 0x0;
1182 scsi_id->logout_orb->reserved2 = 0x0;
1183 scsi_id->logout_orb->reserved3 = 0x0;
1184 scsi_id->logout_orb->reserved4 = 0x0;
1185
1186 scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
1187 scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1189
1190 scsi_id->logout_orb->reserved5 = 0x0;
Stefan Richter35bdddb2006-01-31 00:13:33 -05001191 scsi_id->logout_orb->status_fifo_hi =
1192 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1193 scsi_id->logout_orb->status_fifo_lo =
1194 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb));
1197
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1199 data[1] = scsi_id->logout_orb_dma;
1200 sbp2util_cpu_to_be32_buffer(data, 8);
1201
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 error = hpsb_node_write(scsi_id->ne,
Stefan Richtera237f352005-11-07 06:31:39 -05001203 scsi_id->sbp2_management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 if (error)
1205 return error;
1206
Stefan Richtere8ca5662006-11-02 21:16:08 +01001207 /* wait up to 1 second for the device to complete logout */
Stefan Richtere8398bb2006-07-23 22:19:00 +02001208 if (sbp2util_access_timeout(scsi_id, HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 return -EIO;
1210
1211 SBP2_INFO("Logged out of SBP-2 device");
Stefan Richtera237f352005-11-07 06:31:39 -05001212 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213}
1214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
1216{
1217 struct sbp2scsi_host_info *hi = scsi_id->hi;
1218 quadlet_t data[2];
1219 int error;
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 scsi_id->reconnect_orb->reserved1 = 0x0;
1222 scsi_id->reconnect_orb->reserved2 = 0x0;
1223 scsi_id->reconnect_orb->reserved3 = 0x0;
1224 scsi_id->reconnect_orb->reserved4 = 0x0;
1225
1226 scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
1227 scsi_id->reconnect_orb->login_ID_misc |=
1228 ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1230
1231 scsi_id->reconnect_orb->reserved5 = 0x0;
Stefan Richter35bdddb2006-01-31 00:13:33 -05001232 scsi_id->reconnect_orb->status_fifo_hi =
1233 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1234 scsi_id->reconnect_orb->status_fifo_lo =
1235 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
1238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1240 data[1] = scsi_id->reconnect_orb_dma;
1241 sbp2util_cpu_to_be32_buffer(data, 8);
1242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 error = hpsb_node_write(scsi_id->ne,
Stefan Richtera237f352005-11-07 06:31:39 -05001244 scsi_id->sbp2_management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 if (error)
1246 return error;
1247
Stefan Richtere8ca5662006-11-02 21:16:08 +01001248 /* wait up to 1 second for reconnect status */
Stefan Richtere8398bb2006-07-23 22:19:00 +02001249 if (sbp2util_access_timeout(scsi_id, HZ)) {
1250 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001251 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253
Stefan Richtere8ca5662006-11-02 21:16:08 +01001254 /* make sure that the returned status matches the reconnect ORB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
Stefan Richter60657722006-07-23 22:18:00 +02001256 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001257 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 }
1259
Stefan Richter60657722006-07-23 22:18:00 +02001260 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1261 SBP2_ERR("Error reconnecting to SBP-2 device - failed");
Stefan Richtera237f352005-11-07 06:31:39 -05001262 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
1264
Stefan Richter35644092006-11-02 21:16:08 +01001265 SBP2_INFO("Reconnected to SBP-2 device");
Stefan Richtera237f352005-11-07 06:31:39 -05001266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
1269/*
Stefan Richtere8ca5662006-11-02 21:16:08 +01001270 * Set the target node's Single Phase Retry limit. Affects the target's retry
1271 * behaviour if our node is too busy to accept requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 */
1273static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id)
1274{
1275 quadlet_t data;
1276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
Stefan Richterd024ebc2006-03-28 20:03:55 -05001278 if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
1279 SBP2_ERR("%s error", __FUNCTION__);
Stefan Richtera237f352005-11-07 06:31:39 -05001280 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281}
1282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
1284 struct unit_directory *ud)
1285{
1286 struct csr1212_keyval *kv;
1287 struct csr1212_dentry *dentry;
1288 u64 management_agent_addr;
1289 u32 command_set_spec_id, command_set, unit_characteristics,
Stefan Richter24d3bf82006-05-15 22:04:59 +02001290 firmware_revision;
1291 unsigned workarounds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 int i;
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 management_agent_addr = 0x0;
1295 command_set_spec_id = 0x0;
1296 command_set = 0x0;
1297 unit_characteristics = 0x0;
1298 firmware_revision = 0x0;
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
1301 switch (kv->key.id) {
1302 case CSR1212_KV_ID_DEPENDENT_INFO:
Stefan Richteredf1fb22006-11-02 21:16:08 +01001303 if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 management_agent_addr =
Stefan Richtera237f352005-11-07 06:31:39 -05001305 CSR1212_REGISTER_SPACE_BASE +
1306 (kv->value.csr_offset << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Stefan Richteredf1fb22006-11-02 21:16:08 +01001308 else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE)
Stefan Richtera237f352005-11-07 06:31:39 -05001309 scsi_id->sbp2_lun =
1310 ORB_SET_LUN(kv->value.immediate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 break;
1312
1313 case SBP2_COMMAND_SET_SPEC_ID_KEY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 command_set_spec_id = kv->value.immediate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 break;
1316
1317 case SBP2_COMMAND_SET_KEY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 command_set = kv->value.immediate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 break;
1320
1321 case SBP2_UNIT_CHARACTERISTICS_KEY:
Stefan Richtere8ca5662006-11-02 21:16:08 +01001322 /* FIXME: This is ignored so far.
1323 * See SBP-2 clause 7.4.8. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 unit_characteristics = kv->value.immediate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 break;
1326
1327 case SBP2_FIRMWARE_REVISION_KEY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 firmware_revision = kv->value.immediate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 break;
1330
1331 default:
Stefan Richtere8ca5662006-11-02 21:16:08 +01001332 /* FIXME: Check for SBP2_DEVICE_TYPE_AND_LUN_KEY.
1333 * Its "ordered" bit has consequences for command ORB
1334 * list handling. See SBP-2 clauses 4.6, 7.4.11, 10.2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 break;
1336 }
1337 }
1338
Stefan Richter24d3bf82006-05-15 22:04:59 +02001339 workarounds = sbp2_default_workarounds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
Stefan Richter679c0cd2006-05-15 22:08:09 +02001341 if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
1342 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
1343 if (sbp2_workarounds_table[i].firmware_revision &&
1344 sbp2_workarounds_table[i].firmware_revision !=
1345 (firmware_revision & 0xffff00))
1346 continue;
1347 if (sbp2_workarounds_table[i].model_id &&
1348 sbp2_workarounds_table[i].model_id != ud->model_id)
1349 continue;
1350 workarounds |= sbp2_workarounds_table[i].workarounds;
1351 break;
1352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Stefan Richter24d3bf82006-05-15 22:04:59 +02001354 if (workarounds)
Stefan Richtere9a1c522006-05-15 22:06:37 +02001355 SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
1356 "(firmware_revision 0x%06x, vendor_id 0x%06x,"
1357 " model_id 0x%06x)",
Stefan Richter24d3bf82006-05-15 22:04:59 +02001358 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
Stefan Richtere9a1c522006-05-15 22:06:37 +02001359 workarounds, firmware_revision,
1360 ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
1361 ud->model_id);
Stefan Richter24d3bf82006-05-15 22:04:59 +02001362
1363 /* We would need one SCSI host template for each target to adjust
1364 * max_sectors on the fly, therefore warn only. */
1365 if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
1366 (max_sectors * 512) > (128 * 1024))
Stefan Richter35644092006-11-02 21:16:08 +01001367 SBP2_INFO("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
Stefan Richter24d3bf82006-05-15 22:04:59 +02001368 "max transfer size. WARNING: Current max_sectors "
1369 "setting is larger than 128KB (%d sectors)",
1370 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
1371 max_sectors);
1372
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 /* If this is a logical unit directory entry, process the parent
1374 * to get the values. */
1375 if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
1376 struct unit_directory *parent_ud =
1377 container_of(ud->device.parent, struct unit_directory, device);
1378 sbp2_parse_unit_directory(scsi_id, parent_ud);
1379 } else {
1380 scsi_id->sbp2_management_agent_addr = management_agent_addr;
1381 scsi_id->sbp2_command_set_spec_id = command_set_spec_id;
1382 scsi_id->sbp2_command_set = command_set;
1383 scsi_id->sbp2_unit_characteristics = unit_characteristics;
1384 scsi_id->sbp2_firmware_revision = firmware_revision;
1385 scsi_id->workarounds = workarounds;
1386 if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
Ben Collinse309fc62005-11-07 06:31:34 -05001387 scsi_id->sbp2_lun = ORB_SET_LUN(ud->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389}
1390
Ben Collinsfd23ade2006-06-12 18:14:14 -04001391#define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393/*
1394 * This function is called in order to determine the max speed and packet
1395 * size we can use in our ORBs. Note, that we (the driver and host) only
1396 * initiate the transaction. The SBP-2 device actually transfers the data
1397 * (by reading from the DMA area we tell it). This means that the SBP-2
1398 * device decides the actual maximum data it can transfer. We just tell it
1399 * the speed that it needs to use, and the max_rec the host supports, and
1400 * it takes care of the rest.
1401 */
1402static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id)
1403{
1404 struct sbp2scsi_host_info *hi = scsi_id->hi;
Ben Collinsfd23ade2006-06-12 18:14:14 -04001405 u8 payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Stefan Richtera237f352005-11-07 06:31:39 -05001407 scsi_id->speed_code =
Ben Collins647dcb52006-06-12 18:12:37 -04001408 hi->host->speed[NODEID_TO_NODE(scsi_id->ne->nodeid)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (scsi_id->speed_code > max_speed) {
1411 scsi_id->speed_code = max_speed;
Stefan Richter35644092006-11-02 21:16:08 +01001412 SBP2_INFO("Reducing speed to %s", hpsb_speedto_str[max_speed]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414
1415 /* Payload size is the lesser of what our speed supports and what
1416 * our host supports. */
Ben Collinsfd23ade2006-06-12 18:14:14 -04001417 payload = min(sbp2_speedto_max_payload[scsi_id->speed_code],
1418 (u8) (hi->host->csr.max_rec - 1));
1419
1420 /* If physical DMA is off, work around limitation in ohci1394:
1421 * packet size must not exceed PAGE_SIZE */
1422 if (scsi_id->ne->host->low_addr_space < (1ULL << 32))
1423 while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
1424 payload)
1425 payload--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Stefan Richter35644092006-11-02 21:16:08 +01001427 SBP2_INFO("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
1428 NODE_BUS_ARGS(hi->host, scsi_id->ne->nodeid),
1429 hpsb_speedto_str[scsi_id->speed_code],
1430 SBP2_PAYLOAD_TO_BYTES(payload));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431
Ben Collinsfd23ade2006-06-12 18:14:14 -04001432 scsi_id->max_payload_size = payload;
Stefan Richtera237f352005-11-07 06:31:39 -05001433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434}
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait)
1437{
1438 quadlet_t data;
1439 u64 addr;
1440 int retval;
Stefan Richtercc078182006-07-23 22:12:00 +02001441 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Stefan Richterd19c7762006-12-07 22:40:33 +01001443 /* cancel_delayed_work(&scsi_id->protocol_work); */
Stefan Richter09ee67a2006-08-14 18:43:00 +02001444 if (wait)
1445 flush_scheduled_work();
1446
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 data = ntohl(SBP2_AGENT_RESET_DATA);
1448 addr = scsi_id->sbp2_command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
1449
1450 if (wait)
1451 retval = hpsb_node_write(scsi_id->ne, addr, &data, 4);
1452 else
1453 retval = sbp2util_node_write_no_wait(scsi_id->ne, addr, &data, 4);
1454
1455 if (retval < 0) {
1456 SBP2_ERR("hpsb_node_write failed.\n");
1457 return -EIO;
1458 }
1459
Stefan Richtere8ca5662006-11-02 21:16:08 +01001460 /* make sure that the ORB_POINTER is written on next command */
Stefan Richtercc078182006-07-23 22:12:00 +02001461 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 scsi_id->last_orb = NULL;
Stefan Richtercc078182006-07-23 22:12:00 +02001463 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
Stefan Richtera237f352005-11-07 06:31:39 -05001465 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466}
1467
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001468static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
1469 struct sbp2scsi_host_info *hi,
1470 struct sbp2_command_info *command,
1471 unsigned int scsi_use_sg,
1472 struct scatterlist *sgpnt,
1473 u32 orb_direction,
1474 enum dma_data_direction dma_dir)
1475{
1476 command->dma_dir = dma_dir;
1477 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1478 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1479
Stefan Richtere8ca5662006-11-02 21:16:08 +01001480 /* special case if only one element (and less than 64KB in size) */
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001481 if ((scsi_use_sg == 1) &&
1482 (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
1483
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001484 command->dma_size = sgpnt[0].length;
1485 command->dma_type = CMD_DMA_PAGE;
1486 command->cmd_dma = pci_map_page(hi->host->pdev,
1487 sgpnt[0].page,
1488 sgpnt[0].offset,
1489 command->dma_size,
1490 command->dma_dir);
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001491
1492 orb->data_descriptor_lo = command->cmd_dma;
1493 orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
1494
1495 } else {
1496 struct sbp2_unrestricted_page_table *sg_element =
1497 &command->scatter_gather_element[0];
1498 u32 sg_count, sg_len;
1499 dma_addr_t sg_addr;
1500 int i, count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg,
1501 dma_dir);
1502
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001503 command->dma_size = scsi_use_sg;
1504 command->sge_buffer = sgpnt;
1505
1506 /* use page tables (s/g) */
1507 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1508 orb->data_descriptor_lo = command->sge_dma;
1509
Stefan Richtere8ca5662006-11-02 21:16:08 +01001510 /* loop through and fill out our SBP-2 page tables
1511 * (and split up anything too large) */
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001512 for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
1513 sg_len = sg_dma_len(sgpnt);
1514 sg_addr = sg_dma_address(sgpnt);
1515 while (sg_len) {
1516 sg_element[sg_count].segment_base_lo = sg_addr;
1517 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1518 sg_element[sg_count].length_segment_base_hi =
1519 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1520 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1521 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1522 } else {
1523 sg_element[sg_count].length_segment_base_hi =
1524 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1525 sg_len = 0;
1526 }
1527 sg_count++;
1528 }
1529 }
1530
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001531 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1532
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001533 sbp2util_cpu_to_be32_buffer(sg_element,
1534 (sizeof(struct sbp2_unrestricted_page_table)) *
1535 sg_count);
1536 }
1537}
1538
1539static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
1540 struct sbp2scsi_host_info *hi,
1541 struct sbp2_command_info *command,
1542 struct scatterlist *sgpnt,
1543 u32 orb_direction,
1544 unsigned int scsi_request_bufflen,
1545 void *scsi_request_buffer,
1546 enum dma_data_direction dma_dir)
1547{
1548 command->dma_dir = dma_dir;
1549 command->dma_size = scsi_request_bufflen;
1550 command->dma_type = CMD_DMA_SINGLE;
1551 command->cmd_dma = pci_map_single(hi->host->pdev, scsi_request_buffer,
1552 command->dma_size, command->dma_dir);
1553 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1554 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1555
Stefan Richtere8ca5662006-11-02 21:16:08 +01001556 /* handle case where we get a command w/o s/g enabled
1557 * (but check for transfers larger than 64K) */
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001558 if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1559
1560 orb->data_descriptor_lo = command->cmd_dma;
1561 orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
1562
1563 } else {
Stefan Richtere8ca5662006-11-02 21:16:08 +01001564 /* The buffer is too large. Turn this into page tables. */
1565
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001566 struct sbp2_unrestricted_page_table *sg_element =
1567 &command->scatter_gather_element[0];
1568 u32 sg_count, sg_len;
1569 dma_addr_t sg_addr;
1570
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001571 orb->data_descriptor_lo = command->sge_dma;
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001572 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1573
Stefan Richtere8ca5662006-11-02 21:16:08 +01001574 /* fill out our SBP-2 page tables; split up the large buffer */
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001575 sg_count = 0;
1576 sg_len = scsi_request_bufflen;
1577 sg_addr = command->cmd_dma;
1578 while (sg_len) {
1579 sg_element[sg_count].segment_base_lo = sg_addr;
1580 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1581 sg_element[sg_count].length_segment_base_hi =
1582 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1583 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1584 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1585 } else {
1586 sg_element[sg_count].length_segment_base_hi =
1587 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1588 sg_len = 0;
1589 }
1590 sg_count++;
1591 }
1592
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001593 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1594
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001595 sbp2util_cpu_to_be32_buffer(sg_element,
1596 (sizeof(struct sbp2_unrestricted_page_table)) *
1597 sg_count);
1598 }
1599}
1600
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001601static void sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
1602 struct sbp2_command_info *command,
1603 unchar *scsi_cmd,
1604 unsigned int scsi_use_sg,
1605 unsigned int scsi_request_bufflen,
1606 void *scsi_request_buffer,
1607 enum dma_data_direction dma_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 struct sbp2scsi_host_info *hi = scsi_id->hi;
Stefan Richtera237f352005-11-07 06:31:39 -05001610 struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 struct sbp2_command_orb *command_orb = &command->command_orb;
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001612 u32 orb_direction;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613
1614 /*
Stefan Richtere8ca5662006-11-02 21:16:08 +01001615 * Set-up our command ORB.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 *
1617 * NOTE: We're doing unrestricted page tables (s/g), as this is
1618 * best performance (at least with the devices I have). This means
1619 * that data_size becomes the number of s/g elements, and
1620 * page_size should be zero (for unrestricted).
1621 */
1622 command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1623 command_orb->next_ORB_lo = 0x0;
1624 command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size);
1625 command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code);
Stefan Richtere8ca5662006-11-02 21:16:08 +01001626 command_orb->misc |= ORB_SET_NOTIFY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
Stefan Richter43863eb2005-12-12 23:03:24 -05001628 if (dma_dir == DMA_NONE)
Stefan Richtera237f352005-11-07 06:31:39 -05001629 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
Stefan Richter43863eb2005-12-12 23:03:24 -05001630 else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
Stefan Richtera237f352005-11-07 06:31:39 -05001631 orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
Stefan Richter43863eb2005-12-12 23:03:24 -05001632 else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
Stefan Richtera237f352005-11-07 06:31:39 -05001633 orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
Stefan Richter43863eb2005-12-12 23:03:24 -05001634 else {
Stefan Richter35644092006-11-02 21:16:08 +01001635 SBP2_INFO("Falling back to DMA_NONE");
Stefan Richter43863eb2005-12-12 23:03:24 -05001636 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 }
1638
Stefan Richtere8ca5662006-11-02 21:16:08 +01001639 /* set up our page table stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 command_orb->data_descriptor_hi = 0x0;
1642 command_orb->data_descriptor_lo = 0x0;
1643 command_orb->misc |= ORB_SET_DIRECTION(1);
Stefan Richteredf1fb22006-11-02 21:16:08 +01001644 } else if (scsi_use_sg)
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001645 sbp2_prep_command_orb_sg(command_orb, hi, command, scsi_use_sg,
1646 sgpnt, orb_direction, dma_dir);
Stefan Richteredf1fb22006-11-02 21:16:08 +01001647 else
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001648 sbp2_prep_command_orb_no_sg(command_orb, hi, command, sgpnt,
1649 orb_direction, scsi_request_bufflen,
1650 scsi_request_buffer, dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
1653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 memset(command_orb->cdb, 0, 12);
1655 memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
Stefan Richter28212762006-07-23 22:10:00 +02001658static void sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 struct sbp2_command_info *command)
1660{
1661 struct sbp2scsi_host_info *hi = scsi_id->hi;
1662 struct sbp2_command_orb *command_orb = &command->command_orb;
Stefan Richtercc078182006-07-23 22:12:00 +02001663 struct sbp2_command_orb *last_orb;
1664 dma_addr_t last_orb_dma;
1665 u64 addr = scsi_id->sbp2_command_block_agent_addr;
1666 quadlet_t data[2];
1667 size_t length;
1668 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 pci_dma_sync_single_for_device(hi->host->pdev, command->command_orb_dma,
1671 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02001672 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 pci_dma_sync_single_for_device(hi->host->pdev, command->sge_dma,
1674 sizeof(command->scatter_gather_element),
1675 PCI_DMA_BIDIRECTIONAL);
Stefan Richtere8ca5662006-11-02 21:16:08 +01001676
1677 /* check to see if there are any previous orbs to use */
Stefan Richtercc078182006-07-23 22:12:00 +02001678 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
1679 last_orb = scsi_id->last_orb;
1680 last_orb_dma = scsi_id->last_orb_dma;
1681 if (!last_orb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 /*
Stefan Richtercc078182006-07-23 22:12:00 +02001683 * last_orb == NULL means: We know that the target's fetch agent
1684 * is not active right now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 */
Stefan Richtercc078182006-07-23 22:12:00 +02001686 addr += SBP2_ORB_POINTER_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1688 data[1] = command->command_orb_dma;
1689 sbp2util_cpu_to_be32_buffer(data, 8);
Stefan Richtercc078182006-07-23 22:12:00 +02001690 length = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 /*
Stefan Richtercc078182006-07-23 22:12:00 +02001693 * last_orb != NULL means: We know that the target's fetch agent
1694 * is (very probably) not dead or in reset state right now.
1695 * We have an ORB already sent that we can append a new one to.
1696 * The target's fetch agent may or may not have read this
1697 * previous ORB yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 */
Stefan Richtercc078182006-07-23 22:12:00 +02001699 pci_dma_sync_single_for_cpu(hi->host->pdev, last_orb_dma,
1700 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02001701 PCI_DMA_TODEVICE);
Stefan Richtercc078182006-07-23 22:12:00 +02001702 last_orb->next_ORB_lo = cpu_to_be32(command->command_orb_dma);
1703 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 /* Tells hardware that this pointer is valid */
Stefan Richtercc078182006-07-23 22:12:00 +02001705 last_orb->next_ORB_hi = 0;
1706 pci_dma_sync_single_for_device(hi->host->pdev, last_orb_dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02001708 PCI_DMA_TODEVICE);
Stefan Richtercc078182006-07-23 22:12:00 +02001709 addr += SBP2_DOORBELL_OFFSET;
1710 data[0] = 0;
1711 length = 4;
1712 }
1713 scsi_id->last_orb = command_orb;
1714 scsi_id->last_orb_dma = command->command_orb_dma;
1715 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Stefan Richter09ee67a2006-08-14 18:43:00 +02001717 if (sbp2util_node_write_no_wait(scsi_id->ne, addr, data, length)) {
1718 /*
1719 * sbp2util_node_write_no_wait failed. We certainly ran out
1720 * of transaction labels, perhaps just because there were no
1721 * context switches which gave khpsbpkt a chance to collect
1722 * free tlabels. Try again in non-atomic context. If necessary,
1723 * the workqueue job will sleep to guaranteedly get a tlabel.
1724 * We do not accept new commands until the job is over.
1725 */
1726 scsi_block_requests(scsi_id->scsi_host);
Stefan Richterd19c7762006-12-07 22:40:33 +01001727 PREPARE_WORK(&scsi_id->protocol_work,
Stefan Richter09ee67a2006-08-14 18:43:00 +02001728 last_orb ? sbp2util_write_doorbell:
Stefan Richterd19c7762006-12-07 22:40:33 +01001729 sbp2util_write_orb_pointer
1730 /* */);
1731 schedule_work(&scsi_id->protocol_work);
Stefan Richter09ee67a2006-08-14 18:43:00 +02001732 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733}
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
1736 struct scsi_cmnd *SCpnt,
1737 void (*done)(struct scsi_cmnd *))
1738{
1739 unchar *cmd = (unchar *) SCpnt->cmnd;
1740 unsigned int request_bufflen = SCpnt->request_bufflen;
1741 struct sbp2_command_info *command;
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done);
Stefan Richteredf1fb22006-11-02 21:16:08 +01001744 if (!command)
Stefan Richtera237f352005-11-07 06:31:39 -05001745 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg,
1748 request_bufflen, SCpnt->request_buffer,
1749 SCpnt->sc_data_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 sbp2_link_orb_command(scsi_id, command);
1751
Stefan Richtera237f352005-11-07 06:31:39 -05001752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753}
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 * Translates SBP-2 status into SCSI sense data for check conditions
1757 */
1758static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
1759{
Stefan Richtere8ca5662006-11-02 21:16:08 +01001760 /* OK, it's pretty ugly... ;-) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 sense_data[0] = 0x70;
1762 sense_data[1] = 0x0;
1763 sense_data[2] = sbp2_status[9];
1764 sense_data[3] = sbp2_status[12];
1765 sense_data[4] = sbp2_status[13];
1766 sense_data[5] = sbp2_status[14];
1767 sense_data[6] = sbp2_status[15];
1768 sense_data[7] = 10;
1769 sense_data[8] = sbp2_status[16];
1770 sense_data[9] = sbp2_status[17];
1771 sense_data[10] = sbp2_status[18];
1772 sense_data[11] = sbp2_status[19];
1773 sense_data[12] = sbp2_status[10];
1774 sense_data[13] = sbp2_status[11];
1775 sense_data[14] = sbp2_status[20];
1776 sense_data[15] = sbp2_status[21];
1777
Stefan Richtere8ca5662006-11-02 21:16:08 +01001778 return sbp2_status[8] & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779}
1780
Stefan Richter3e98eab42006-07-23 22:16:00 +02001781static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
1782 int destid, quadlet_t *data, u64 addr,
1783 size_t length, u16 fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784{
1785 struct sbp2scsi_host_info *hi;
1786 struct scsi_id_instance_data *scsi_id = NULL, *scsi_id_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 struct scsi_cmnd *SCpnt = NULL;
Stefan Richter3e98eab42006-07-23 22:16:00 +02001788 struct sbp2_status_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
1790 struct sbp2_command_info *command;
Jody McIntyre79456192005-11-07 06:29:39 -05001791 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Stefan Richter60657722006-07-23 22:18:00 +02001793 if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
1794 SBP2_ERR("Wrong size of status block");
1795 return RCODE_ADDRESS_ERROR;
1796 }
1797 if (unlikely(!host)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 SBP2_ERR("host is NULL - this is bad!");
Stefan Richtera237f352005-11-07 06:31:39 -05001799 return RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
Stefan Richter60657722006-07-23 22:18:00 +02001802 if (unlikely(!hi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 SBP2_ERR("host info is NULL - this is bad!");
Stefan Richtera237f352005-11-07 06:31:39 -05001804 return RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 }
Stefan Richtere8ca5662006-11-02 21:16:08 +01001806
1807 /* Find the unit which wrote the status. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 list_for_each_entry(scsi_id_tmp, &hi->scsi_ids, scsi_list) {
Stefan Richter35bdddb2006-01-31 00:13:33 -05001809 if (scsi_id_tmp->ne->nodeid == nodeid &&
1810 scsi_id_tmp->status_fifo_addr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 scsi_id = scsi_id_tmp;
1812 break;
1813 }
1814 }
Stefan Richter60657722006-07-23 22:18:00 +02001815 if (unlikely(!scsi_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 SBP2_ERR("scsi_id is NULL - device is gone?");
Stefan Richtera237f352005-11-07 06:31:39 -05001817 return RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819
Stefan Richtere8ca5662006-11-02 21:16:08 +01001820 /* Put response into scsi_id status fifo buffer. The first two bytes
Stefan Richter3e98eab42006-07-23 22:16:00 +02001821 * come in big endian bit order. Often the target writes only a
1822 * truncated status block, minimally the first two quadlets. The rest
Stefan Richtere8ca5662006-11-02 21:16:08 +01001823 * is implied to be zeros. */
Stefan Richter3e98eab42006-07-23 22:16:00 +02001824 sb = &scsi_id->status_block;
1825 memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
1826 memcpy(sb, data, length);
1827 sbp2util_be32_to_cpu_buffer(sb, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
Stefan Richtere8ca5662006-11-02 21:16:08 +01001829 /* Ignore unsolicited status. Handle command ORB status. */
Stefan Richter60657722006-07-23 22:18:00 +02001830 if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
1831 command = NULL;
1832 else
1833 command = sbp2util_find_command_for_orb(scsi_id,
1834 sb->ORB_offset_lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
1837 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02001838 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
1840 sizeof(command->scatter_gather_element),
1841 PCI_DMA_BIDIRECTIONAL);
Stefan Richtere8ca5662006-11-02 21:16:08 +01001842 /* Grab SCSI command pointers and check status. */
Stefan Richter60657722006-07-23 22:18:00 +02001843 /*
1844 * FIXME: If the src field in the status is 1, the ORB DMA must
1845 * not be reused until status for a subsequent ORB is received.
1846 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 SCpnt = command->Current_SCpnt;
Stefan Richter24c7cd02006-04-01 21:11:41 +02001848 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 sbp2util_mark_command_completed(scsi_id, command);
Stefan Richter24c7cd02006-04-01 21:11:41 +02001850 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
1852 if (SCpnt) {
Stefan Richterabbca102006-08-14 18:51:00 +02001853 u32 h = sb->ORB_offset_hi_misc;
1854 u32 r = STATUS_GET_RESP(h);
1855
1856 if (r != RESP_STATUS_REQUEST_COMPLETE) {
Stefan Richter35644092006-11-02 21:16:08 +01001857 SBP2_INFO("resp 0x%x, sbp_status 0x%x",
Stefan Richterabbca102006-08-14 18:51:00 +02001858 r, STATUS_GET_SBP_STATUS(h));
Stefan Richter60657722006-07-23 22:18:00 +02001859 scsi_status =
Stefan Richterabbca102006-08-14 18:51:00 +02001860 r == RESP_STATUS_TRANSPORT_FAILURE ?
1861 SBP2_SCSI_STATUS_BUSY :
Stefan Richter60657722006-07-23 22:18:00 +02001862 SBP2_SCSI_STATUS_COMMAND_TERMINATED;
Stefan Richterabbca102006-08-14 18:51:00 +02001863 }
Stefan Richtere8ca5662006-11-02 21:16:08 +01001864
Stefan Richteredf1fb22006-11-02 21:16:08 +01001865 if (STATUS_GET_LEN(h) > 1)
Stefan Richter3e98eab42006-07-23 22:16:00 +02001866 scsi_status = sbp2_status_to_sense_data(
1867 (unchar *)sb, SCpnt->sense_buffer);
Stefan Richtere8ca5662006-11-02 21:16:08 +01001868
Stefan Richteredf1fb22006-11-02 21:16:08 +01001869 if (STATUS_TEST_DEAD(h))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 sbp2_agent_reset(scsi_id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
1872
Stefan Richtere8ca5662006-11-02 21:16:08 +01001873 /* Check here to see if there are no commands in-use. If there
Stefan Richtercc078182006-07-23 22:12:00 +02001874 * are none, we know that the fetch agent left the active state
1875 * _and_ that we did not reactivate it yet. Therefore clear
1876 * last_orb so that next time we write directly to the
1877 * ORB_POINTER register. That way the fetch agent does not need
Stefan Richtere8ca5662006-11-02 21:16:08 +01001878 * to refetch the next_ORB. */
Jody McIntyre79456192005-11-07 06:29:39 -05001879 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Stefan Richtercc078182006-07-23 22:12:00 +02001880 if (list_empty(&scsi_id->sbp2_command_orb_inuse))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 scsi_id->last_orb = NULL;
Jody McIntyre79456192005-11-07 06:29:39 -05001882 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
1884 } else {
Stefan Richtere8ca5662006-11-02 21:16:08 +01001885 /* It's probably status after a management request. */
Stefan Richter3e98eab42006-07-23 22:16:00 +02001886 if ((sb->ORB_offset_lo == scsi_id->reconnect_orb_dma) ||
1887 (sb->ORB_offset_lo == scsi_id->login_orb_dma) ||
1888 (sb->ORB_offset_lo == scsi_id->query_logins_orb_dma) ||
Stefan Richtere8398bb2006-07-23 22:19:00 +02001889 (sb->ORB_offset_lo == scsi_id->logout_orb_dma)) {
1890 scsi_id->access_complete = 1;
1891 wake_up_interruptible(&access_wq);
1892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
1894
Stefan Richteredf1fb22006-11-02 21:16:08 +01001895 if (SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt,
1897 command->Current_done);
Stefan Richtera237f352005-11-07 06:31:39 -05001898 return RCODE_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901/**************************************
1902 * SCSI interface related section
1903 **************************************/
1904
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
1906 void (*done)(struct scsi_cmnd *))
1907{
1908 struct scsi_id_instance_data *scsi_id =
1909 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
1910 struct sbp2scsi_host_info *hi;
Jody McIntyreabd559b2005-09-30 11:59:06 -07001911 int result = DID_NO_CONNECT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Stefan Richter5796aa72006-11-02 21:16:08 +01001913 if (unlikely(!sbp2util_node_is_available(scsi_id)))
Jody McIntyreabd559b2005-09-30 11:59:06 -07001914 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 hi = scsi_id->hi;
1917
Stefan Richter5796aa72006-11-02 21:16:08 +01001918 if (unlikely(!hi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 SBP2_ERR("sbp2scsi_host_info is NULL - this is bad!");
Jody McIntyreabd559b2005-09-30 11:59:06 -07001920 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 }
1922
Stefan Richtere8ca5662006-11-02 21:16:08 +01001923 /* Multiple units are currently represented to the SCSI core as separate
1924 * targets, not as one target with multiple LUs. Therefore return
1925 * selection time-out to any IO directed at non-zero LUNs. */
Stefan Richter5796aa72006-11-02 21:16:08 +01001926 if (unlikely(SCpnt->device->lun))
Jody McIntyreabd559b2005-09-30 11:59:06 -07001927 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Stefan Richtere8ca5662006-11-02 21:16:08 +01001929 /* handle the request sense command here (auto-request sense) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (SCpnt->cmnd[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen);
1932 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
1933 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done);
Jody McIntyreabd559b2005-09-30 11:59:06 -07001934 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 }
1936
Stefan Richter5796aa72006-11-02 21:16:08 +01001937 if (unlikely(!hpsb_node_entry_valid(scsi_id->ne))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 SBP2_ERR("Bus reset in progress - rejecting command");
Jody McIntyreabd559b2005-09-30 11:59:06 -07001939 result = DID_BUS_BUSY << 16;
1940 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942
Stefan Richtere8ca5662006-11-02 21:16:08 +01001943 /* Bidirectional commands are not yet implemented,
1944 * and unknown transfer direction not handled. */
Stefan Richter5796aa72006-11-02 21:16:08 +01001945 if (unlikely(SCpnt->sc_data_direction == DMA_BIDIRECTIONAL)) {
Stefan Richter43863eb2005-12-12 23:03:24 -05001946 SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
1947 result = DID_ERROR << 16;
1948 goto done;
1949 }
1950
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 if (sbp2_send_command(scsi_id, SCpnt, done)) {
1952 SBP2_ERR("Error sending SCSI command");
1953 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
1954 SCpnt, done);
1955 }
Jody McIntyreabd559b2005-09-30 11:59:06 -07001956 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
Jody McIntyreabd559b2005-09-30 11:59:06 -07001958done:
1959 SCpnt->result = result;
1960 done(SCpnt);
1961 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962}
1963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
1965 u32 status)
1966{
1967 struct sbp2scsi_host_info *hi = scsi_id->hi;
1968 struct list_head *lh;
1969 struct sbp2_command_info *command;
Jody McIntyre79456192005-11-07 06:29:39 -05001970 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971
Jody McIntyre79456192005-11-07 06:29:39 -05001972 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 while (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 lh = scsi_id->sbp2_command_orb_inuse.next;
1975 command = list_entry(lh, struct sbp2_command_info, list);
1976 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
1977 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02001978 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
1980 sizeof(command->scatter_gather_element),
1981 PCI_DMA_BIDIRECTIONAL);
1982 sbp2util_mark_command_completed(scsi_id, command);
1983 if (command->Current_SCpnt) {
1984 command->Current_SCpnt->result = status << 16;
1985 command->Current_done(command->Current_SCpnt);
1986 }
1987 }
Jody McIntyre79456192005-11-07 06:29:39 -05001988 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
1990 return;
1991}
1992
1993/*
Stefan Richtere8ca5662006-11-02 21:16:08 +01001994 * Complete a regular SCSI command. Can be called in atomic context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 */
1996static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
1997 u32 scsi_status, struct scsi_cmnd *SCpnt,
1998 void (*done)(struct scsi_cmnd *))
1999{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 if (!SCpnt) {
2001 SBP2_ERR("SCpnt is NULL");
2002 return;
2003 }
2004
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 switch (scsi_status) {
Stefan Richtera237f352005-11-07 06:31:39 -05002006 case SBP2_SCSI_STATUS_GOOD:
Stefan Richter8f0525f2006-03-28 20:03:45 -05002007 SCpnt->result = DID_OK << 16;
Stefan Richtera237f352005-11-07 06:31:39 -05002008 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
Stefan Richtera237f352005-11-07 06:31:39 -05002010 case SBP2_SCSI_STATUS_BUSY:
2011 SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
2012 SCpnt->result = DID_BUS_BUSY << 16;
2013 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Stefan Richtera237f352005-11-07 06:31:39 -05002015 case SBP2_SCSI_STATUS_CHECK_CONDITION:
Stefan Richter8f0525f2006-03-28 20:03:45 -05002016 SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
Stefan Richtera237f352005-11-07 06:31:39 -05002017 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Stefan Richtera237f352005-11-07 06:31:39 -05002019 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
2020 SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
2021 SCpnt->result = DID_NO_CONNECT << 16;
2022 scsi_print_command(SCpnt);
2023 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Stefan Richtera237f352005-11-07 06:31:39 -05002025 case SBP2_SCSI_STATUS_CONDITION_MET:
2026 case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
2027 case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
2028 SBP2_ERR("Bad SCSI status = %x", scsi_status);
2029 SCpnt->result = DID_ERROR << 16;
2030 scsi_print_command(SCpnt);
2031 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032
Stefan Richtera237f352005-11-07 06:31:39 -05002033 default:
2034 SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
2035 SCpnt->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
2037
Stefan Richtere8ca5662006-11-02 21:16:08 +01002038 /* If a bus reset is in progress and there was an error, complete
2039 * the command as busy so that it will get retried. */
Stefan Richtera237f352005-11-07 06:31:39 -05002040 if (!hpsb_node_entry_valid(scsi_id->ne)
2041 && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 SBP2_ERR("Completing command with busy (bus reset)");
2043 SCpnt->result = DID_BUS_BUSY << 16;
2044 }
2045
Stefan Richtere8ca5662006-11-02 21:16:08 +01002046 /* Tell the SCSI stack that we're done with this command. */
Stefan Richtera237f352005-11-07 06:31:39 -05002047 done(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048}
2049
Jody McIntyreabd559b2005-09-30 11:59:06 -07002050static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
2051{
Stefan Richtera80614d2006-02-14 22:04:19 -05002052 struct scsi_id_instance_data *scsi_id =
2053 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2054
2055 scsi_id->sdev = sdev;
Stefan Richterc394f1e2006-08-07 20:48:00 +02002056 sdev->allow_restart = 1;
Stefan Richtera80614d2006-02-14 22:04:19 -05002057
Stefan Richter24d3bf82006-05-15 22:04:59 +02002058 if (scsi_id->workarounds & SBP2_WORKAROUND_INQUIRY_36)
Stefan Richtera80614d2006-02-14 22:04:19 -05002059 sdev->inquiry_len = 36;
Jody McIntyreabd559b2005-09-30 11:59:06 -07002060 return 0;
2061}
2062
Jody McIntyreabd559b2005-09-30 11:59:06 -07002063static int sbp2scsi_slave_configure(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
Stefan Richter24d3bf82006-05-15 22:04:59 +02002065 struct scsi_id_instance_data *scsi_id =
2066 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
Ben Collins365c7862005-11-07 06:31:24 -05002069 sdev->use_10_for_rw = 1;
Stefan Richter24d3bf82006-05-15 22:04:59 +02002070
2071 if (sdev->type == TYPE_DISK &&
2072 scsi_id->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
2073 sdev->skip_ms_page_8 = 1;
Stefan Richtere9a1c522006-05-15 22:06:37 +02002074 if (scsi_id->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
2075 sdev->fix_capacity = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 return 0;
2077}
2078
Jody McIntyreabd559b2005-09-30 11:59:06 -07002079static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
2080{
2081 ((struct scsi_id_instance_data *)sdev->host->hostdata[0])->sdev = NULL;
2082 return;
2083}
2084
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085/*
Stefan Richtere8ca5662006-11-02 21:16:08 +01002086 * Called by scsi stack when something has really gone wrong.
2087 * Usually called when a command has timed-out for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 */
2089static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
2090{
2091 struct scsi_id_instance_data *scsi_id =
2092 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2093 struct sbp2scsi_host_info *hi = scsi_id->hi;
2094 struct sbp2_command_info *command;
Stefan Richter24c7cd02006-04-01 21:11:41 +02002095 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Stefan Richter35644092006-11-02 21:16:08 +01002097 SBP2_INFO("aborting sbp2 command");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 scsi_print_command(SCpnt);
2099
Jody McIntyreabd559b2005-09-30 11:59:06 -07002100 if (sbp2util_node_is_available(scsi_id)) {
Stefan Richter23077f12006-09-11 20:17:14 +02002101 sbp2_agent_reset(scsi_id, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Stefan Richter23077f12006-09-11 20:17:14 +02002103 /* Return a matching command structure to the free pool. */
Stefan Richter24c7cd02006-04-01 21:11:41 +02002104 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
2106 if (command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 pci_dma_sync_single_for_cpu(hi->host->pdev,
2108 command->command_orb_dma,
2109 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02002110 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 pci_dma_sync_single_for_cpu(hi->host->pdev,
2112 command->sge_dma,
2113 sizeof(command->scatter_gather_element),
2114 PCI_DMA_BIDIRECTIONAL);
2115 sbp2util_mark_command_completed(scsi_id, command);
2116 if (command->Current_SCpnt) {
2117 command->Current_SCpnt->result = DID_ABORT << 16;
2118 command->Current_done(command->Current_SCpnt);
2119 }
2120 }
Stefan Richter24c7cd02006-04-01 21:11:41 +02002121 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
2124 }
2125
Stefan Richtera237f352005-11-07 06:31:39 -05002126 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127}
2128
2129/*
2130 * Called by scsi stack when something has really gone wrong.
2131 */
Jody McIntyreabd559b2005-09-30 11:59:06 -07002132static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
2134 struct scsi_id_instance_data *scsi_id =
2135 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2136
Stefan Richter35644092006-11-02 21:16:08 +01002137 SBP2_INFO("reset requested");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Jody McIntyreabd559b2005-09-30 11:59:06 -07002139 if (sbp2util_node_is_available(scsi_id)) {
Stefan Richter35644092006-11-02 21:16:08 +01002140 SBP2_INFO("generating sbp2 fetch agent reset");
Stefan Richter1f427e82006-08-14 18:44:00 +02002141 sbp2_agent_reset(scsi_id, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143
Jody McIntyreabd559b2005-09-30 11:59:06 -07002144 return SUCCESS;
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04002145}
2146
Stefan Richtera237f352005-11-07 06:31:39 -05002147static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
2148 struct device_attribute *attr,
2149 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
2151 struct scsi_device *sdev;
2152 struct scsi_id_instance_data *scsi_id;
2153 int lun;
2154
2155 if (!(sdev = to_scsi_device(dev)))
2156 return 0;
2157
2158 if (!(scsi_id = (struct scsi_id_instance_data *)sdev->host->hostdata[0]))
2159 return 0;
2160
Ben Collinse309fc62005-11-07 06:31:34 -05002161 lun = ORB_SET_LUN(scsi_id->sbp2_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)scsi_id->ne->guid,
2164 scsi_id->ud->id, lun);
2165}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
2167MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2168MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2169MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2170MODULE_LICENSE("GPL");
2171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172static int sbp2_module_init(void)
2173{
2174 int ret;
2175
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 if (serialize_io) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 scsi_driver_template.can_queue = 1;
2178 scsi_driver_template.cmd_per_lun = 1;
2179 }
2180
Stefan Richter24d3bf82006-05-15 22:04:59 +02002181 if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
2182 (max_sectors * 512) > (128 * 1024))
2183 max_sectors = 128 * 1024 / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 scsi_driver_template.max_sectors = max_sectors;
2185
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 hpsb_register_highlevel(&sbp2_highlevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 ret = hpsb_register_protocol(&sbp2_driver);
2188 if (ret) {
2189 SBP2_ERR("Failed to register protocol");
2190 hpsb_unregister_highlevel(&sbp2_highlevel);
2191 return ret;
2192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return 0;
2194}
2195
2196static void __exit sbp2_module_exit(void)
2197{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 hpsb_unregister_protocol(&sbp2_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 hpsb_unregister_highlevel(&sbp2_highlevel);
2200}
2201
2202module_init(sbp2_module_init);
2203module_exit(sbp2_module_exit);