blob: 6bd26a91111eed70068ffc76a797e55a4699ba8b [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
Stefan Richtere8398bb2006-07-23 22:19:00 +0200407/* Frees an allocated packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408static void sbp2_free_packet(struct hpsb_packet *packet)
409{
410 hpsb_free_tlabel(packet);
411 hpsb_free_packet(packet);
412}
413
414/* This is much like hpsb_node_write(), except it ignores the response
415 * subaction and returns immediately. Can be used from interrupts.
416 */
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{
David Howellsc4028952006-11-22 14:57:56 +0000465 struct scsi_id_instance_data *scsi_id =
466 container_of(work, struct scsi_id_instance_data,
467 protocol_work.work);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200468 quadlet_t data[2];
469
David Howellsc4028952006-11-22 14:57:56 +0000470 data[0] = ORB_SET_NODE_ID(scsi_id->hi->host->node_id);
471 data[1] = scsi_id->last_orb_dma;
Stefan Richter09ee67a2006-08-14 18:43:00 +0200472 sbp2util_cpu_to_be32_buffer(data, 8);
David Howellsc4028952006-11-22 14:57:56 +0000473 sbp2util_notify_fetch_agent(scsi_id, SBP2_ORB_POINTER_OFFSET, data, 8);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200474}
475
David Howellsc4028952006-11-22 14:57:56 +0000476static void sbp2util_write_doorbell(struct work_struct *work)
Stefan Richter09ee67a2006-08-14 18:43:00 +0200477{
David Howellsc4028952006-11-22 14:57:56 +0000478 struct scsi_id_instance_data *scsi_id =
479 container_of(work, struct scsi_id_instance_data,
480 protocol_work.work);
481 sbp2util_notify_fetch_agent(scsi_id, SBP2_DOORBELL_OFFSET, NULL, 4);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200482}
483
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484/*
485 * This function is called to create a pool of command orbs used for
486 * command processing. It is called when a new sbp2 device is detected.
487 */
488static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id)
489{
490 struct sbp2scsi_host_info *hi = scsi_id->hi;
491 int i;
492 unsigned long flags, orbs;
493 struct sbp2_command_info *command;
494
495 orbs = serialize_io ? 2 : SBP2_MAX_CMDS;
496
497 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
498 for (i = 0; i < orbs; i++) {
Stefan Richter85511582005-11-07 06:31:45 -0500499 command = kzalloc(sizeof(*command), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 if (!command) {
Stefan Richtera237f352005-11-07 06:31:39 -0500501 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock,
502 flags);
503 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 command->command_orb_dma =
Stefan Richtera237f352005-11-07 06:31:39 -0500506 pci_map_single(hi->host->pdev, &command->command_orb,
507 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +0200508 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 command->sge_dma =
Stefan Richtera237f352005-11-07 06:31:39 -0500510 pci_map_single(hi->host->pdev,
511 &command->scatter_gather_element,
512 sizeof(command->scatter_gather_element),
513 PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 INIT_LIST_HEAD(&command->list);
515 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
516 }
517 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
518 return 0;
519}
520
521/*
522 * This function is called to delete a pool of command orbs.
523 */
524static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id)
525{
526 struct hpsb_host *host = scsi_id->hi->host;
527 struct list_head *lh, *next;
528 struct sbp2_command_info *command;
529 unsigned long flags;
530
531 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
532 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
533 list_for_each_safe(lh, next, &scsi_id->sbp2_command_orb_completed) {
534 command = list_entry(lh, struct sbp2_command_info, list);
535
536 /* Release our generic DMA's */
537 pci_unmap_single(host->pdev, command->command_orb_dma,
538 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +0200539 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 pci_unmap_single(host->pdev, command->sge_dma,
541 sizeof(command->scatter_gather_element),
542 PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 kfree(command);
544 }
545 }
546 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
547 return;
548}
549
550/*
551 * This function finds the sbp2_command for a given outstanding command
552 * orb.Only looks at the inuse list.
553 */
554static struct sbp2_command_info *sbp2util_find_command_for_orb(
555 struct scsi_id_instance_data *scsi_id, dma_addr_t orb)
556{
557 struct sbp2_command_info *command;
558 unsigned long flags;
559
560 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
561 if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
562 list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list) {
563 if (command->command_orb_dma == orb) {
564 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Stefan Richtera237f352005-11-07 06:31:39 -0500565 return command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567 }
568 }
569 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Stefan Richtera237f352005-11-07 06:31:39 -0500570 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
573/*
574 * This function finds the sbp2_command for a given outstanding SCpnt.
575 * Only looks at the inuse list.
Stefan Richter24c7cd02006-04-01 21:11:41 +0200576 * Must be called with scsi_id->sbp2_command_orb_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
Stefan Richter24c7cd02006-04-01 21:11:41 +0200578static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
579 struct scsi_id_instance_data *scsi_id, void *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580{
581 struct sbp2_command_info *command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Stefan Richter24c7cd02006-04-01 21:11:41 +0200583 if (!list_empty(&scsi_id->sbp2_command_orb_inuse))
584 list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list)
585 if (command->Current_SCpnt == SCpnt)
Stefan Richtera237f352005-11-07 06:31:39 -0500586 return command;
Stefan Richtera237f352005-11-07 06:31:39 -0500587 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588}
589
590/*
591 * This function allocates a command orb used to send a scsi command.
592 */
593static struct sbp2_command_info *sbp2util_allocate_command_orb(
594 struct scsi_id_instance_data *scsi_id,
595 struct scsi_cmnd *Current_SCpnt,
596 void (*Current_done)(struct scsi_cmnd *))
597{
598 struct list_head *lh;
599 struct sbp2_command_info *command = NULL;
600 unsigned long flags;
601
602 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
603 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
604 lh = scsi_id->sbp2_command_orb_completed.next;
605 list_del(lh);
606 command = list_entry(lh, struct sbp2_command_info, list);
607 command->Current_done = Current_done;
608 command->Current_SCpnt = Current_SCpnt;
609 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_inuse);
610 } else {
Stefan Richterd024ebc2006-03-28 20:03:55 -0500611 SBP2_ERR("%s: no orbs available", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
613 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Stefan Richtera237f352005-11-07 06:31:39 -0500614 return command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
617/* Free our DMA's */
618static void sbp2util_free_command_dma(struct sbp2_command_info *command)
619{
620 struct scsi_id_instance_data *scsi_id =
621 (struct scsi_id_instance_data *)command->Current_SCpnt->device->host->hostdata[0];
622 struct hpsb_host *host;
623
624 if (!scsi_id) {
Stefan Richterd024ebc2006-03-28 20:03:55 -0500625 SBP2_ERR("%s: scsi_id == NULL", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 return;
627 }
628
629 host = scsi_id->ud->ne->host;
630
631 if (command->cmd_dma) {
Stefan Richteredf1fb22006-11-02 21:16:08 +0100632 if (command->dma_type == CMD_DMA_SINGLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 pci_unmap_single(host->pdev, command->cmd_dma,
634 command->dma_size, command->dma_dir);
Stefan Richteredf1fb22006-11-02 21:16:08 +0100635 else if (command->dma_type == CMD_DMA_PAGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 pci_unmap_page(host->pdev, command->cmd_dma,
637 command->dma_size, command->dma_dir);
Stefan Richteredf1fb22006-11-02 21:16:08 +0100638 /* XXX: Check for CMD_DMA_NONE bug */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 command->dma_type = CMD_DMA_NONE;
640 command->cmd_dma = 0;
641 }
642
643 if (command->sge_buffer) {
644 pci_unmap_sg(host->pdev, command->sge_buffer,
645 command->dma_size, command->dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 command->sge_buffer = NULL;
647 }
648}
649
650/*
651 * This function moves a command to the completed orb list.
Stefan Richter24c7cd02006-04-01 21:11:41 +0200652 * Must be called with scsi_id->sbp2_command_orb_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 */
Stefan Richter24c7cd02006-04-01 21:11:41 +0200654static void sbp2util_mark_command_completed(
655 struct scsi_id_instance_data *scsi_id,
656 struct sbp2_command_info *command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 list_del(&command->list);
659 sbp2util_free_command_dma(command);
660 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
Jody McIntyreabd559b2005-09-30 11:59:06 -0700663/*
664 * Is scsi_id valid? Is the 1394 node still present?
665 */
666static inline int sbp2util_node_is_available(struct scsi_id_instance_data *scsi_id)
667{
668 return scsi_id && scsi_id->ne && !scsi_id->ne->in_limbo;
669}
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671/*********************************************
672 * IEEE-1394 core driver stack related section
673 *********************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675static int sbp2_probe(struct device *dev)
676{
677 struct unit_directory *ud;
678 struct scsi_id_instance_data *scsi_id;
679
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 ud = container_of(dev, struct unit_directory, device);
681
682 /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
683 * instead. */
684 if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
685 return -ENODEV;
686
Stefan Richtera237f352005-11-07 06:31:39 -0500687 scsi_id = sbp2_alloc_device(ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Stefan Richtera237f352005-11-07 06:31:39 -0500689 if (!scsi_id)
690 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Stefan Richtera237f352005-11-07 06:31:39 -0500692 sbp2_parse_unit_directory(scsi_id, ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Stefan Richtera237f352005-11-07 06:31:39 -0500694 return sbp2_start_device(scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697static int sbp2_remove(struct device *dev)
698{
699 struct unit_directory *ud;
700 struct scsi_id_instance_data *scsi_id;
Jody McIntyreabd559b2005-09-30 11:59:06 -0700701 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 ud = container_of(dev, struct unit_directory, device);
704 scsi_id = ud->device.driver_data;
Jody McIntyreabd559b2005-09-30 11:59:06 -0700705 if (!scsi_id)
706 return 0;
707
Stefan Richterbf637ec2006-01-31 00:13:06 -0500708 if (scsi_id->scsi_host) {
709 /* Get rid of enqueued commands if there is no chance to
710 * send them. */
711 if (!sbp2util_node_is_available(scsi_id))
712 sbp2scsi_complete_all_commands(scsi_id, DID_NO_CONNECT);
713 /* scsi_remove_device() will trigger shutdown functions of SCSI
714 * highlevel drivers which would deadlock if blocked. */
Stefan Richter2cccbb52006-08-14 18:59:00 +0200715 atomic_set(&scsi_id->state, SBP2LU_STATE_IN_SHUTDOWN);
Jody McIntyreabd559b2005-09-30 11:59:06 -0700716 scsi_unblock_requests(scsi_id->scsi_host);
Stefan Richterbf637ec2006-01-31 00:13:06 -0500717 }
Jody McIntyreabd559b2005-09-30 11:59:06 -0700718 sdev = scsi_id->sdev;
719 if (sdev) {
720 scsi_id->sdev = NULL;
721 scsi_remove_device(sdev);
722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 sbp2_logout_device(scsi_id);
725 sbp2_remove_device(scsi_id);
726
727 return 0;
728}
729
730static int sbp2_update(struct unit_directory *ud)
731{
732 struct scsi_id_instance_data *scsi_id = ud->device.driver_data;
733
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 if (sbp2_reconnect_device(scsi_id)) {
735
736 /*
737 * Ok, reconnect has failed. Perhaps we didn't
738 * reconnect fast enough. Try doing a regular login, but
739 * first do a logout just in case of any weirdness.
740 */
741 sbp2_logout_device(scsi_id);
742
743 if (sbp2_login_device(scsi_id)) {
744 /* Login failed too, just fail, and the backend
745 * will call our sbp2_remove for us */
746 SBP2_ERR("Failed to reconnect to sbp2 device!");
747 return -EBUSY;
748 }
749 }
750
751 /* Set max retries to something large on the device. */
752 sbp2_set_busy_timeout(scsi_id);
753
754 /* Do a SBP-2 fetch agent reset. */
755 sbp2_agent_reset(scsi_id, 1);
756
757 /* Get the max speed and packet size that we can use. */
758 sbp2_max_speed_and_size(scsi_id);
759
760 /* Complete any pending commands with busy (so they get
761 * retried) and remove them from our queue
762 */
763 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
764
Stefan Richter4fc383c2006-08-14 18:46:00 +0200765 /* Accept new commands unless there was another bus reset in the
766 * meantime. */
767 if (hpsb_node_entry_valid(scsi_id->ne)) {
Stefan Richter2cccbb52006-08-14 18:59:00 +0200768 atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
Stefan Richter4fc383c2006-08-14 18:46:00 +0200769 scsi_unblock_requests(scsi_id->scsi_host);
770 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return 0;
772}
773
774/* This functions is called by the sbp2_probe, for each new device. We now
775 * allocate one scsi host for each scsi_id (unit directory). */
776static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud)
777{
778 struct sbp2scsi_host_info *hi;
779 struct Scsi_Host *scsi_host = NULL;
780 struct scsi_id_instance_data *scsi_id = NULL;
781
Stefan Richter85511582005-11-07 06:31:45 -0500782 scsi_id = kzalloc(sizeof(*scsi_id), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 if (!scsi_id) {
784 SBP2_ERR("failed to create scsi_id");
785 goto failed_alloc;
786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
788 scsi_id->ne = ud->ne;
789 scsi_id->ud = ud;
790 scsi_id->speed_code = IEEE1394_SPEED_100;
791 scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
Ben Collins67372312006-06-12 18:15:31 -0400792 scsi_id->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_inuse);
794 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_completed);
795 INIT_LIST_HEAD(&scsi_id->scsi_list);
796 spin_lock_init(&scsi_id->sbp2_command_orb_lock);
Stefan Richter2cccbb52006-08-14 18:59:00 +0200797 atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
David Howellsc4028952006-11-22 14:57:56 +0000798 INIT_DELAYED_WORK(&scsi_id->protocol_work, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 ud->device.driver_data = scsi_id;
801
802 hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
803 if (!hi) {
804 hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host, sizeof(*hi));
805 if (!hi) {
806 SBP2_ERR("failed to allocate hostinfo");
807 goto failed_alloc;
808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 hi->host = ud->ne->host;
810 INIT_LIST_HEAD(&hi->scsi_ids);
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
813 /* Handle data movement if physical dma is not
Stefan Richter55664052006-03-28 19:59:42 -0500814 * enabled or not supported on host controller */
815 if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
816 &sbp2_physdma_ops,
817 0x0ULL, 0xfffffffcULL)) {
818 SBP2_ERR("failed to register lower 4GB address range");
819 goto failed_alloc;
820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821#endif
822 }
823
Stefan Richter147830f2006-03-28 19:54:52 -0500824 /* Prevent unloading of the 1394 host */
825 if (!try_module_get(hi->host->driver->owner)) {
826 SBP2_ERR("failed to get a reference on 1394 host driver");
827 goto failed_alloc;
828 }
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 scsi_id->hi = hi;
831
832 list_add_tail(&scsi_id->scsi_list, &hi->scsi_ids);
833
Stefan Richter35bdddb2006-01-31 00:13:33 -0500834 /* Register the status FIFO address range. We could use the same FIFO
835 * for targets at different nodes. However we need different FIFOs per
Stefan Richtera54c9d32006-05-15 22:09:46 +0200836 * target in order to support multi-unit devices.
837 * The FIFO is located out of the local host controller's physical range
838 * but, if possible, within the posted write area. Status writes will
839 * then be performed as unified transactions. This slightly reduces
840 * bandwidth usage, and some Prolific based devices seem to require it.
841 */
Stefan Richter35bdddb2006-01-31 00:13:33 -0500842 scsi_id->status_fifo_addr = hpsb_allocate_and_register_addrspace(
843 &sbp2_highlevel, ud->ne->host, &sbp2_ops,
844 sizeof(struct sbp2_status_block), sizeof(quadlet_t),
Ben Collins40ae6c52006-06-12 18:13:49 -0400845 ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
Ben Collins67372312006-06-12 18:15:31 -0400846 if (scsi_id->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
Stefan Richter35bdddb2006-01-31 00:13:33 -0500847 SBP2_ERR("failed to allocate status FIFO address range");
848 goto failed_alloc;
849 }
850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 /* Register our host with the SCSI stack. */
Alexandre Olivaa2ef79e2005-06-15 22:26:31 -0700852 scsi_host = scsi_host_alloc(&scsi_driver_template,
Stefan Richtera237f352005-11-07 06:31:39 -0500853 sizeof(unsigned long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (!scsi_host) {
855 SBP2_ERR("failed to register scsi host");
856 goto failed_alloc;
857 }
858
859 scsi_host->hostdata[0] = (unsigned long)scsi_id;
860
861 if (!scsi_add_host(scsi_host, &ud->device)) {
862 scsi_id->scsi_host = scsi_host;
863 return scsi_id;
864 }
865
866 SBP2_ERR("failed to add scsi host");
867 scsi_host_put(scsi_host);
868
869failed_alloc:
870 sbp2_remove_device(scsi_id);
871 return NULL;
872}
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874static void sbp2_host_reset(struct hpsb_host *host)
875{
876 struct sbp2scsi_host_info *hi;
877 struct scsi_id_instance_data *scsi_id;
878
879 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
Stefan Richter2cccbb52006-08-14 18:59:00 +0200880 if (!hi)
881 return;
882 list_for_each_entry(scsi_id, &hi->scsi_ids, scsi_list)
883 if (likely(atomic_read(&scsi_id->state) !=
884 SBP2LU_STATE_IN_SHUTDOWN)) {
885 atomic_set(&scsi_id->state, SBP2LU_STATE_IN_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 scsi_block_requests(scsi_id->scsi_host);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888}
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890/*
891 * This function is where we first pull the node unique ids, and then
892 * allocate memory and register a SBP-2 device.
893 */
894static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
895{
896 struct sbp2scsi_host_info *hi = scsi_id->hi;
James Bottomley146f7262005-09-10 12:44:09 -0500897 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 /* Login FIFO DMA */
900 scsi_id->login_response =
Stefan Richtera237f352005-11-07 06:31:39 -0500901 pci_alloc_consistent(hi->host->pdev,
902 sizeof(struct sbp2_login_response),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 &scsi_id->login_response_dma);
904 if (!scsi_id->login_response)
905 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
907 /* Query logins ORB DMA */
908 scsi_id->query_logins_orb =
Stefan Richtera237f352005-11-07 06:31:39 -0500909 pci_alloc_consistent(hi->host->pdev,
910 sizeof(struct sbp2_query_logins_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 &scsi_id->query_logins_orb_dma);
912 if (!scsi_id->query_logins_orb)
913 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
915 /* Query logins response DMA */
916 scsi_id->query_logins_response =
Stefan Richtera237f352005-11-07 06:31:39 -0500917 pci_alloc_consistent(hi->host->pdev,
918 sizeof(struct sbp2_query_logins_response),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 &scsi_id->query_logins_response_dma);
920 if (!scsi_id->query_logins_response)
921 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 /* Reconnect ORB DMA */
924 scsi_id->reconnect_orb =
Stefan Richtera237f352005-11-07 06:31:39 -0500925 pci_alloc_consistent(hi->host->pdev,
926 sizeof(struct sbp2_reconnect_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 &scsi_id->reconnect_orb_dma);
928 if (!scsi_id->reconnect_orb)
929 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 /* Logout ORB DMA */
932 scsi_id->logout_orb =
Stefan Richtera237f352005-11-07 06:31:39 -0500933 pci_alloc_consistent(hi->host->pdev,
934 sizeof(struct sbp2_logout_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 &scsi_id->logout_orb_dma);
936 if (!scsi_id->logout_orb)
937 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
939 /* Login ORB DMA */
940 scsi_id->login_orb =
Stefan Richtera237f352005-11-07 06:31:39 -0500941 pci_alloc_consistent(hi->host->pdev,
942 sizeof(struct sbp2_login_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 &scsi_id->login_orb_dma);
Stefan Richtereaceec72005-12-13 11:05:05 -0500944 if (!scsi_id->login_orb)
945 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 /*
948 * Create our command orb pool
949 */
950 if (sbp2util_create_command_orb_pool(scsi_id)) {
951 SBP2_ERR("sbp2util_create_command_orb_pool failed!");
952 sbp2_remove_device(scsi_id);
953 return -ENOMEM;
954 }
955
956 /* Schedule a timeout here. The reason is that we may be so close
957 * to a bus reset, that the device is not available for logins.
958 * This can happen when the bus reset is caused by the host
959 * connected to the sbp2 device being removed. That host would
960 * have a certain amount of time to relogin before the sbp2 device
961 * allows someone else to login instead. One second makes sense. */
Stefan Richter902abed2006-08-14 18:56:00 +0200962 if (msleep_interruptible(1000)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 sbp2_remove_device(scsi_id);
964 return -EINTR;
965 }
Stefan Richtera237f352005-11-07 06:31:39 -0500966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 /*
968 * Login to the sbp-2 device
969 */
970 if (sbp2_login_device(scsi_id)) {
971 /* Login failed, just remove the device. */
972 sbp2_remove_device(scsi_id);
973 return -EBUSY;
974 }
975
976 /*
977 * Set max retries to something large on the device
978 */
979 sbp2_set_busy_timeout(scsi_id);
980
981 /*
982 * Do a SBP-2 fetch agent reset
983 */
984 sbp2_agent_reset(scsi_id, 1);
985
986 /*
987 * Get the max speed and packet size that we can use
988 */
989 sbp2_max_speed_and_size(scsi_id);
990
991 /* Add this device to the scsi layer now */
James Bottomley146f7262005-09-10 12:44:09 -0500992 error = scsi_add_device(scsi_id->scsi_host, 0, scsi_id->ud->id, 0);
993 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 SBP2_ERR("scsi_add_device failed");
Stefan Richterdc3edd52005-12-12 23:03:30 -0500995 sbp2_logout_device(scsi_id);
996 sbp2_remove_device(scsi_id);
James Bottomley146f7262005-09-10 12:44:09 -0500997 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
999
1000 return 0;
Stefan Richtereaceec72005-12-13 11:05:05 -05001001
1002alloc_fail:
1003 SBP2_ERR("Could not allocate memory for scsi_id");
1004 sbp2_remove_device(scsi_id);
1005 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
1008/*
1009 * This function removes an sbp2 device from the sbp2scsi_host_info struct.
1010 */
1011static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
1012{
1013 struct sbp2scsi_host_info *hi;
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 if (!scsi_id)
1016 return;
1017
1018 hi = scsi_id->hi;
1019
1020 /* This will remove our scsi device aswell */
1021 if (scsi_id->scsi_host) {
1022 scsi_remove_host(scsi_id->scsi_host);
1023 scsi_host_put(scsi_id->scsi_host);
1024 }
Stefan Richter09ee67a2006-08-14 18:43:00 +02001025 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 sbp2util_remove_command_orb_pool(scsi_id);
1027
1028 list_del(&scsi_id->scsi_list);
1029
Stefan Richteredf1fb22006-11-02 21:16:08 +01001030 if (scsi_id->login_response)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 pci_free_consistent(hi->host->pdev,
1032 sizeof(struct sbp2_login_response),
1033 scsi_id->login_response,
1034 scsi_id->login_response_dma);
Stefan Richteredf1fb22006-11-02 21:16:08 +01001035 if (scsi_id->login_orb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 pci_free_consistent(hi->host->pdev,
1037 sizeof(struct sbp2_login_orb),
1038 scsi_id->login_orb,
1039 scsi_id->login_orb_dma);
Stefan Richteredf1fb22006-11-02 21:16:08 +01001040 if (scsi_id->reconnect_orb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 pci_free_consistent(hi->host->pdev,
1042 sizeof(struct sbp2_reconnect_orb),
1043 scsi_id->reconnect_orb,
1044 scsi_id->reconnect_orb_dma);
Stefan Richteredf1fb22006-11-02 21:16:08 +01001045 if (scsi_id->logout_orb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 pci_free_consistent(hi->host->pdev,
1047 sizeof(struct sbp2_logout_orb),
1048 scsi_id->logout_orb,
1049 scsi_id->logout_orb_dma);
Stefan Richteredf1fb22006-11-02 21:16:08 +01001050 if (scsi_id->query_logins_orb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 pci_free_consistent(hi->host->pdev,
1052 sizeof(struct sbp2_query_logins_orb),
1053 scsi_id->query_logins_orb,
1054 scsi_id->query_logins_orb_dma);
Stefan Richteredf1fb22006-11-02 21:16:08 +01001055 if (scsi_id->query_logins_response)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 pci_free_consistent(hi->host->pdev,
1057 sizeof(struct sbp2_query_logins_response),
1058 scsi_id->query_logins_response,
1059 scsi_id->query_logins_response_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
Ben Collins67372312006-06-12 18:15:31 -04001061 if (scsi_id->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
Stefan Richter35bdddb2006-01-31 00:13:33 -05001062 hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
Ben Collins67372312006-06-12 18:15:31 -04001063 scsi_id->status_fifo_addr);
Stefan Richter35bdddb2006-01-31 00:13:33 -05001064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 scsi_id->ud->device.driver_data = NULL;
1066
Stefan Richter147830f2006-03-28 19:54:52 -05001067 if (hi)
1068 module_put(hi->host->driver->owner);
1069
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 kfree(scsi_id);
1071}
1072
1073#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
1074/*
1075 * This function deals with physical dma write requests (for adapters that do not support
1076 * physical dma in hardware). Mostly just here for debugging...
1077 */
Stefan Richtera237f352005-11-07 06:31:39 -05001078static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
1079 int destid, quadlet_t *data, u64 addr,
1080 size_t length, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081{
1082
Stefan Richtera237f352005-11-07 06:31:39 -05001083 /*
1084 * Manually put the data in the right place.
1085 */
1086 memcpy(bus_to_virt((u32) addr), data, length);
Stefan Richtera237f352005-11-07 06:31:39 -05001087 return RCODE_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
1090/*
1091 * This function deals with physical dma read requests (for adapters that do not support
1092 * physical dma in hardware). Mostly just here for debugging...
1093 */
Stefan Richtera237f352005-11-07 06:31:39 -05001094static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
1095 quadlet_t *data, u64 addr, size_t length,
1096 u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098
Stefan Richtera237f352005-11-07 06:31:39 -05001099 /*
1100 * Grab data from memory and send a read response.
1101 */
1102 memcpy(data, bus_to_virt((u32) addr), length);
Stefan Richtera237f352005-11-07 06:31:39 -05001103 return RCODE_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104}
1105#endif
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107/**************************************
1108 * SBP-2 protocol related section
1109 **************************************/
1110
1111/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 * This function queries the device for the maximum concurrent logins it
1113 * supports.
1114 */
1115static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
1116{
1117 struct sbp2scsi_host_info *hi = scsi_id->hi;
1118 quadlet_t data[2];
1119 int max_logins;
1120 int active_logins;
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 scsi_id->query_logins_orb->reserved1 = 0x0;
1123 scsi_id->query_logins_orb->reserved2 = 0x0;
1124
1125 scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma;
1126 scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
1128 scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
1129 scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
Ben Collinse309fc62005-11-07 06:31:34 -05001130 scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 scsi_id->query_logins_orb->reserved_resp_length =
1133 ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Stefan Richter35bdddb2006-01-31 00:13:33 -05001135 scsi_id->query_logins_orb->status_fifo_hi =
1136 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1137 scsi_id->query_logins_orb->status_fifo_lo =
1138 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139
1140 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb));
1141
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1145 data[1] = scsi_id->query_logins_orb_dma;
1146 sbp2util_cpu_to_be32_buffer(data, 8);
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Stefan Richtere8398bb2006-07-23 22:19:00 +02001150 if (sbp2util_access_timeout(scsi_id, 2*HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001152 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154
1155 if (scsi_id->status_block.ORB_offset_lo != scsi_id->query_logins_orb_dma) {
1156 SBP2_INFO("Error querying logins to 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_INFO("Error querying logins to SBP-2 device - failed");
Stefan Richtera237f352005-11-07 06:31:39 -05001162 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
1164
1165 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_response, sizeof(struct sbp2_query_logins_response));
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins);
Ben Collins20f45782006-06-12 18:13:11 -04001168 SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins);
Ben Collins20f45782006-06-12 18:13:11 -04001171 SBP2_INFO("Number of active logins: %d", active_logins);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
1173 if (active_logins >= max_logins) {
Stefan Richtera237f352005-11-07 06:31:39 -05001174 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176
1177 return 0;
1178}
1179
1180/*
1181 * This function is called in order to login to a particular SBP-2 device,
1182 * after a bus reset.
1183 */
1184static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
1185{
1186 struct sbp2scsi_host_info *hi = scsi_id->hi;
1187 quadlet_t data[2];
1188
Stefan Richteredf1fb22006-11-02 21:16:08 +01001189 if (!scsi_id->login_orb)
Stefan Richtera237f352005-11-07 06:31:39 -05001190 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
1192 if (!exclusive_login) {
1193 if (sbp2_query_logins(scsi_id)) {
1194 SBP2_INFO("Device does not support any more concurrent logins");
Stefan Richtera237f352005-11-07 06:31:39 -05001195 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
1197 }
1198
1199 /* Set-up login ORB, assume no password */
1200 scsi_id->login_orb->password_hi = 0;
1201 scsi_id->login_orb->password_lo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
1203 scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
1204 scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
1207 scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0); /* One second reconnect time */
1208 scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(exclusive_login); /* Exclusive access to device */
1209 scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1); /* Notify us of login complete */
Ben Collinse309fc62005-11-07 06:31:34 -05001210 scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 scsi_id->login_orb->passwd_resp_lengths =
1213 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Stefan Richter35bdddb2006-01-31 00:13:33 -05001215 scsi_id->login_orb->status_fifo_hi =
1216 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1217 scsi_id->login_orb->status_fifo_lo =
1218 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1225 data[1] = scsi_id->login_orb_dma;
1226 sbp2util_cpu_to_be32_buffer(data, 8);
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 /*
1231 * Wait for login status (up to 20 seconds)...
1232 */
Stefan Richtere8398bb2006-07-23 22:19:00 +02001233 if (sbp2util_access_timeout(scsi_id, 20*HZ)) {
1234 SBP2_ERR("Error logging into SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001235 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237
1238 /*
1239 * Sanity. Make sure status returned matches login orb.
1240 */
1241 if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
Stefan Richter60657722006-07-23 22:18:00 +02001242 SBP2_ERR("Error logging into SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001243 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 }
1245
Stefan Richter60657722006-07-23 22:18:00 +02001246 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1247 SBP2_ERR("Error logging into SBP-2 device - failed");
Stefan Richtera237f352005-11-07 06:31:39 -05001248 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 }
1250
1251 /*
1252 * Byte swap the login response, for use when reconnecting or
1253 * logging out.
1254 */
1255 sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response));
1256
1257 /*
1258 * Grab our command block agent address from the login response.
1259 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 scsi_id->sbp2_command_block_agent_addr =
1261 ((u64)scsi_id->login_response->command_block_agent_hi) << 32;
1262 scsi_id->sbp2_command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
1263 scsi_id->sbp2_command_block_agent_addr &= 0x0000ffffffffffffULL;
1264
1265 SBP2_INFO("Logged into SBP-2 device");
Stefan Richtera237f352005-11-07 06:31:39 -05001266 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
1269/*
1270 * This function is called in order to logout from a particular SBP-2
1271 * device, usually called during driver unload.
1272 */
1273static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
1274{
1275 struct sbp2scsi_host_info *hi = scsi_id->hi;
1276 quadlet_t data[2];
1277 int error;
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 /*
1280 * Set-up logout ORB
1281 */
1282 scsi_id->logout_orb->reserved1 = 0x0;
1283 scsi_id->logout_orb->reserved2 = 0x0;
1284 scsi_id->logout_orb->reserved3 = 0x0;
1285 scsi_id->logout_orb->reserved4 = 0x0;
1286
1287 scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
1288 scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1289
1290 /* Notify us when complete */
1291 scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1292
1293 scsi_id->logout_orb->reserved5 = 0x0;
Stefan Richter35bdddb2006-01-31 00:13:33 -05001294 scsi_id->logout_orb->status_fifo_hi =
1295 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1296 scsi_id->logout_orb->status_fifo_lo =
1297 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 /*
1300 * Byte swap ORB if necessary
1301 */
1302 sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb));
1303
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 /*
1305 * Ok, let's write to the target's management agent register
1306 */
1307 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1308 data[1] = scsi_id->logout_orb_dma;
1309 sbp2util_cpu_to_be32_buffer(data, 8);
1310
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 error = hpsb_node_write(scsi_id->ne,
Stefan Richtera237f352005-11-07 06:31:39 -05001312 scsi_id->sbp2_management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (error)
1314 return error;
1315
1316 /* Wait for device to logout...1 second. */
Stefan Richtere8398bb2006-07-23 22:19:00 +02001317 if (sbp2util_access_timeout(scsi_id, HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 return -EIO;
1319
1320 SBP2_INFO("Logged out of SBP-2 device");
Stefan Richtera237f352005-11-07 06:31:39 -05001321 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322}
1323
1324/*
1325 * This function is called in order to reconnect to a particular SBP-2
1326 * device, after a bus reset.
1327 */
1328static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
1329{
1330 struct sbp2scsi_host_info *hi = scsi_id->hi;
1331 quadlet_t data[2];
1332 int error;
1333
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 /*
1335 * Set-up reconnect ORB
1336 */
1337 scsi_id->reconnect_orb->reserved1 = 0x0;
1338 scsi_id->reconnect_orb->reserved2 = 0x0;
1339 scsi_id->reconnect_orb->reserved3 = 0x0;
1340 scsi_id->reconnect_orb->reserved4 = 0x0;
1341
1342 scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
1343 scsi_id->reconnect_orb->login_ID_misc |=
1344 ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1345
1346 /* Notify us when complete */
1347 scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1348
1349 scsi_id->reconnect_orb->reserved5 = 0x0;
Stefan Richter35bdddb2006-01-31 00:13:33 -05001350 scsi_id->reconnect_orb->status_fifo_hi =
1351 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1352 scsi_id->reconnect_orb->status_fifo_lo =
1353 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 /*
1356 * Byte swap ORB if necessary
1357 */
1358 sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
1359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1361 data[1] = scsi_id->reconnect_orb_dma;
1362 sbp2util_cpu_to_be32_buffer(data, 8);
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 error = hpsb_node_write(scsi_id->ne,
Stefan Richtera237f352005-11-07 06:31:39 -05001365 scsi_id->sbp2_management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (error)
1367 return error;
1368
1369 /*
1370 * Wait for reconnect status (up to 1 second)...
1371 */
Stefan Richtere8398bb2006-07-23 22:19:00 +02001372 if (sbp2util_access_timeout(scsi_id, HZ)) {
1373 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001374 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 }
1376
1377 /*
1378 * Sanity. Make sure status returned matches reconnect orb.
1379 */
1380 if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
Stefan Richter60657722006-07-23 22:18:00 +02001381 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001382 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
1384
Stefan Richter60657722006-07-23 22:18:00 +02001385 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1386 SBP2_ERR("Error reconnecting to SBP-2 device - failed");
Stefan Richtera237f352005-11-07 06:31:39 -05001387 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389
Stefan Richter35644092006-11-02 21:16:08 +01001390 SBP2_INFO("Reconnected to SBP-2 device");
Stefan Richtera237f352005-11-07 06:31:39 -05001391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392}
1393
1394/*
1395 * This function is called in order to set the busy timeout (number of
1396 * retries to attempt) on the sbp2 device.
1397 */
1398static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id)
1399{
1400 quadlet_t data;
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
Stefan Richterd024ebc2006-03-28 20:03:55 -05001403 if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
1404 SBP2_ERR("%s error", __FUNCTION__);
Stefan Richtera237f352005-11-07 06:31:39 -05001405 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406}
1407
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408/*
1409 * This function is called to parse sbp2 device's config rom unit
1410 * directory. Used to determine things like sbp2 management agent offset,
1411 * and command set used (SCSI or RBC).
1412 */
1413static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
1414 struct unit_directory *ud)
1415{
1416 struct csr1212_keyval *kv;
1417 struct csr1212_dentry *dentry;
1418 u64 management_agent_addr;
1419 u32 command_set_spec_id, command_set, unit_characteristics,
Stefan Richter24d3bf82006-05-15 22:04:59 +02001420 firmware_revision;
1421 unsigned workarounds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 int i;
1423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 management_agent_addr = 0x0;
1425 command_set_spec_id = 0x0;
1426 command_set = 0x0;
1427 unit_characteristics = 0x0;
1428 firmware_revision = 0x0;
1429
1430 /* Handle different fields in the unit directory, based on keys */
1431 csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
1432 switch (kv->key.id) {
1433 case CSR1212_KV_ID_DEPENDENT_INFO:
Stefan Richteredf1fb22006-11-02 21:16:08 +01001434 if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 /* Save off the management agent address */
1436 management_agent_addr =
Stefan Richtera237f352005-11-07 06:31:39 -05001437 CSR1212_REGISTER_SPACE_BASE +
1438 (kv->value.csr_offset << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Stefan Richteredf1fb22006-11-02 21:16:08 +01001440 else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE)
Stefan Richtera237f352005-11-07 06:31:39 -05001441 scsi_id->sbp2_lun =
1442 ORB_SET_LUN(kv->value.immediate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 break;
1444
1445 case SBP2_COMMAND_SET_SPEC_ID_KEY:
1446 /* Command spec organization */
1447 command_set_spec_id = kv->value.immediate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 break;
1449
1450 case SBP2_COMMAND_SET_KEY:
1451 /* Command set used by sbp2 device */
1452 command_set = kv->value.immediate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 break;
1454
1455 case SBP2_UNIT_CHARACTERISTICS_KEY:
1456 /*
1457 * Unit characterisitcs (orb related stuff
1458 * that I'm not yet paying attention to)
1459 */
1460 unit_characteristics = kv->value.immediate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 break;
1462
1463 case SBP2_FIRMWARE_REVISION_KEY:
1464 /* Firmware revision */
1465 firmware_revision = kv->value.immediate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 break;
1467
1468 default:
1469 break;
1470 }
1471 }
1472
Stefan Richter24d3bf82006-05-15 22:04:59 +02001473 workarounds = sbp2_default_workarounds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Stefan Richter679c0cd2006-05-15 22:08:09 +02001475 if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
1476 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
1477 if (sbp2_workarounds_table[i].firmware_revision &&
1478 sbp2_workarounds_table[i].firmware_revision !=
1479 (firmware_revision & 0xffff00))
1480 continue;
1481 if (sbp2_workarounds_table[i].model_id &&
1482 sbp2_workarounds_table[i].model_id != ud->model_id)
1483 continue;
1484 workarounds |= sbp2_workarounds_table[i].workarounds;
1485 break;
1486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Stefan Richter24d3bf82006-05-15 22:04:59 +02001488 if (workarounds)
Stefan Richtere9a1c522006-05-15 22:06:37 +02001489 SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
1490 "(firmware_revision 0x%06x, vendor_id 0x%06x,"
1491 " model_id 0x%06x)",
Stefan Richter24d3bf82006-05-15 22:04:59 +02001492 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
Stefan Richtere9a1c522006-05-15 22:06:37 +02001493 workarounds, firmware_revision,
1494 ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
1495 ud->model_id);
Stefan Richter24d3bf82006-05-15 22:04:59 +02001496
1497 /* We would need one SCSI host template for each target to adjust
1498 * max_sectors on the fly, therefore warn only. */
1499 if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
1500 (max_sectors * 512) > (128 * 1024))
Stefan Richter35644092006-11-02 21:16:08 +01001501 SBP2_INFO("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
Stefan Richter24d3bf82006-05-15 22:04:59 +02001502 "max transfer size. WARNING: Current max_sectors "
1503 "setting is larger than 128KB (%d sectors)",
1504 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
1505 max_sectors);
1506
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 /* If this is a logical unit directory entry, process the parent
1508 * to get the values. */
1509 if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
1510 struct unit_directory *parent_ud =
1511 container_of(ud->device.parent, struct unit_directory, device);
1512 sbp2_parse_unit_directory(scsi_id, parent_ud);
1513 } else {
1514 scsi_id->sbp2_management_agent_addr = management_agent_addr;
1515 scsi_id->sbp2_command_set_spec_id = command_set_spec_id;
1516 scsi_id->sbp2_command_set = command_set;
1517 scsi_id->sbp2_unit_characteristics = unit_characteristics;
1518 scsi_id->sbp2_firmware_revision = firmware_revision;
1519 scsi_id->workarounds = workarounds;
1520 if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
Ben Collinse309fc62005-11-07 06:31:34 -05001521 scsi_id->sbp2_lun = ORB_SET_LUN(ud->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523}
1524
Ben Collinsfd23ade2006-06-12 18:14:14 -04001525#define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
1526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527/*
1528 * This function is called in order to determine the max speed and packet
1529 * size we can use in our ORBs. Note, that we (the driver and host) only
1530 * initiate the transaction. The SBP-2 device actually transfers the data
1531 * (by reading from the DMA area we tell it). This means that the SBP-2
1532 * device decides the actual maximum data it can transfer. We just tell it
1533 * the speed that it needs to use, and the max_rec the host supports, and
1534 * it takes care of the rest.
1535 */
1536static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id)
1537{
1538 struct sbp2scsi_host_info *hi = scsi_id->hi;
Ben Collinsfd23ade2006-06-12 18:14:14 -04001539 u8 payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Stefan Richtera237f352005-11-07 06:31:39 -05001541 scsi_id->speed_code =
Ben Collins647dcb52006-06-12 18:12:37 -04001542 hi->host->speed[NODEID_TO_NODE(scsi_id->ne->nodeid)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
1544 /* Bump down our speed if the user requested it */
1545 if (scsi_id->speed_code > max_speed) {
1546 scsi_id->speed_code = max_speed;
Stefan Richter35644092006-11-02 21:16:08 +01001547 SBP2_INFO("Reducing speed to %s", hpsb_speedto_str[max_speed]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 }
1549
1550 /* Payload size is the lesser of what our speed supports and what
1551 * our host supports. */
Ben Collinsfd23ade2006-06-12 18:14:14 -04001552 payload = min(sbp2_speedto_max_payload[scsi_id->speed_code],
1553 (u8) (hi->host->csr.max_rec - 1));
1554
1555 /* If physical DMA is off, work around limitation in ohci1394:
1556 * packet size must not exceed PAGE_SIZE */
1557 if (scsi_id->ne->host->low_addr_space < (1ULL << 32))
1558 while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
1559 payload)
1560 payload--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
Stefan Richter35644092006-11-02 21:16:08 +01001562 SBP2_INFO("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
1563 NODE_BUS_ARGS(hi->host, scsi_id->ne->nodeid),
1564 hpsb_speedto_str[scsi_id->speed_code],
1565 SBP2_PAYLOAD_TO_BYTES(payload));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Ben Collinsfd23ade2006-06-12 18:14:14 -04001567 scsi_id->max_payload_size = payload;
Stefan Richtera237f352005-11-07 06:31:39 -05001568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
1571/*
1572 * This function is called in order to perform a SBP-2 agent reset.
1573 */
1574static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait)
1575{
1576 quadlet_t data;
1577 u64 addr;
1578 int retval;
Stefan Richtercc078182006-07-23 22:12:00 +02001579 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Stefan Richter09ee67a2006-08-14 18:43:00 +02001581 cancel_delayed_work(&scsi_id->protocol_work);
1582 if (wait)
1583 flush_scheduled_work();
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 data = ntohl(SBP2_AGENT_RESET_DATA);
1586 addr = scsi_id->sbp2_command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
1587
1588 if (wait)
1589 retval = hpsb_node_write(scsi_id->ne, addr, &data, 4);
1590 else
1591 retval = sbp2util_node_write_no_wait(scsi_id->ne, addr, &data, 4);
1592
1593 if (retval < 0) {
1594 SBP2_ERR("hpsb_node_write failed.\n");
1595 return -EIO;
1596 }
1597
1598 /*
1599 * Need to make sure orb pointer is written on next command
1600 */
Stefan Richtercc078182006-07-23 22:12:00 +02001601 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 scsi_id->last_orb = NULL;
Stefan Richtercc078182006-07-23 22:12:00 +02001603 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Stefan Richtera237f352005-11-07 06:31:39 -05001605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001608static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
1609 struct sbp2scsi_host_info *hi,
1610 struct sbp2_command_info *command,
1611 unsigned int scsi_use_sg,
1612 struct scatterlist *sgpnt,
1613 u32 orb_direction,
1614 enum dma_data_direction dma_dir)
1615{
1616 command->dma_dir = dma_dir;
1617 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1618 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1619
1620 /* Special case if only one element (and less than 64KB in size) */
1621 if ((scsi_use_sg == 1) &&
1622 (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
1623
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001624 command->dma_size = sgpnt[0].length;
1625 command->dma_type = CMD_DMA_PAGE;
1626 command->cmd_dma = pci_map_page(hi->host->pdev,
1627 sgpnt[0].page,
1628 sgpnt[0].offset,
1629 command->dma_size,
1630 command->dma_dir);
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001631
1632 orb->data_descriptor_lo = command->cmd_dma;
1633 orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
1634
1635 } else {
1636 struct sbp2_unrestricted_page_table *sg_element =
1637 &command->scatter_gather_element[0];
1638 u32 sg_count, sg_len;
1639 dma_addr_t sg_addr;
1640 int i, count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg,
1641 dma_dir);
1642
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001643 command->dma_size = scsi_use_sg;
1644 command->sge_buffer = sgpnt;
1645
1646 /* use page tables (s/g) */
1647 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1648 orb->data_descriptor_lo = command->sge_dma;
1649
1650 /*
1651 * Loop through and fill out our sbp-2 page tables
1652 * (and split up anything too large)
1653 */
1654 for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
1655 sg_len = sg_dma_len(sgpnt);
1656 sg_addr = sg_dma_address(sgpnt);
1657 while (sg_len) {
1658 sg_element[sg_count].segment_base_lo = sg_addr;
1659 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1660 sg_element[sg_count].length_segment_base_hi =
1661 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1662 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1663 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1664 } else {
1665 sg_element[sg_count].length_segment_base_hi =
1666 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1667 sg_len = 0;
1668 }
1669 sg_count++;
1670 }
1671 }
1672
1673 /* Number of page table (s/g) elements */
1674 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1675
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001676 /* Byte swap page tables if necessary */
1677 sbp2util_cpu_to_be32_buffer(sg_element,
1678 (sizeof(struct sbp2_unrestricted_page_table)) *
1679 sg_count);
1680 }
1681}
1682
1683static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
1684 struct sbp2scsi_host_info *hi,
1685 struct sbp2_command_info *command,
1686 struct scatterlist *sgpnt,
1687 u32 orb_direction,
1688 unsigned int scsi_request_bufflen,
1689 void *scsi_request_buffer,
1690 enum dma_data_direction dma_dir)
1691{
1692 command->dma_dir = dma_dir;
1693 command->dma_size = scsi_request_bufflen;
1694 command->dma_type = CMD_DMA_SINGLE;
1695 command->cmd_dma = pci_map_single(hi->host->pdev, scsi_request_buffer,
1696 command->dma_size, command->dma_dir);
1697 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1698 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1699
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001700 /*
1701 * Handle case where we get a command w/o s/g enabled (but
1702 * check for transfers larger than 64K)
1703 */
1704 if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1705
1706 orb->data_descriptor_lo = command->cmd_dma;
1707 orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
1708
1709 } else {
1710 struct sbp2_unrestricted_page_table *sg_element =
1711 &command->scatter_gather_element[0];
1712 u32 sg_count, sg_len;
1713 dma_addr_t sg_addr;
1714
1715 /*
1716 * Need to turn this into page tables, since the
1717 * buffer is too large.
1718 */
1719 orb->data_descriptor_lo = command->sge_dma;
1720
1721 /* Use page tables (s/g) */
1722 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1723
1724 /*
1725 * fill out our sbp-2 page tables (and split up
1726 * the large buffer)
1727 */
1728 sg_count = 0;
1729 sg_len = scsi_request_bufflen;
1730 sg_addr = command->cmd_dma;
1731 while (sg_len) {
1732 sg_element[sg_count].segment_base_lo = sg_addr;
1733 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1734 sg_element[sg_count].length_segment_base_hi =
1735 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1736 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1737 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1738 } else {
1739 sg_element[sg_count].length_segment_base_hi =
1740 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1741 sg_len = 0;
1742 }
1743 sg_count++;
1744 }
1745
1746 /* Number of page table (s/g) elements */
1747 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1748
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001749 /* Byte swap page tables if necessary */
1750 sbp2util_cpu_to_be32_buffer(sg_element,
1751 (sizeof(struct sbp2_unrestricted_page_table)) *
1752 sg_count);
1753 }
1754}
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756/*
1757 * This function is called to create the actual command orb and s/g list
1758 * out of the scsi command itself.
1759 */
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001760static void sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
1761 struct sbp2_command_info *command,
1762 unchar *scsi_cmd,
1763 unsigned int scsi_use_sg,
1764 unsigned int scsi_request_bufflen,
1765 void *scsi_request_buffer,
1766 enum dma_data_direction dma_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767{
1768 struct sbp2scsi_host_info *hi = scsi_id->hi;
Stefan Richtera237f352005-11-07 06:31:39 -05001769 struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 struct sbp2_command_orb *command_orb = &command->command_orb;
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001771 u32 orb_direction;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773 /*
1774 * Set-up our command ORB..
1775 *
1776 * NOTE: We're doing unrestricted page tables (s/g), as this is
1777 * best performance (at least with the devices I have). This means
1778 * that data_size becomes the number of s/g elements, and
1779 * page_size should be zero (for unrestricted).
1780 */
1781 command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1782 command_orb->next_ORB_lo = 0x0;
1783 command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size);
1784 command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code);
Stefan Richtera237f352005-11-07 06:31:39 -05001785 command_orb->misc |= ORB_SET_NOTIFY(1); /* Notify us when complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Stefan Richter43863eb2005-12-12 23:03:24 -05001787 if (dma_dir == DMA_NONE)
Stefan Richtera237f352005-11-07 06:31:39 -05001788 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
Stefan Richter43863eb2005-12-12 23:03:24 -05001789 else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
Stefan Richtera237f352005-11-07 06:31:39 -05001790 orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
Stefan Richter43863eb2005-12-12 23:03:24 -05001791 else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
Stefan Richtera237f352005-11-07 06:31:39 -05001792 orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
Stefan Richter43863eb2005-12-12 23:03:24 -05001793 else {
Stefan Richter35644092006-11-02 21:16:08 +01001794 SBP2_INFO("Falling back to DMA_NONE");
Stefan Richter43863eb2005-12-12 23:03:24 -05001795 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
1797
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001798 /* Set-up our pagetable stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 command_orb->data_descriptor_hi = 0x0;
1801 command_orb->data_descriptor_lo = 0x0;
1802 command_orb->misc |= ORB_SET_DIRECTION(1);
Stefan Richteredf1fb22006-11-02 21:16:08 +01001803 } else if (scsi_use_sg)
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001804 sbp2_prep_command_orb_sg(command_orb, hi, command, scsi_use_sg,
1805 sgpnt, orb_direction, dma_dir);
Stefan Richteredf1fb22006-11-02 21:16:08 +01001806 else
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001807 sbp2_prep_command_orb_no_sg(command_orb, hi, command, sgpnt,
1808 orb_direction, scsi_request_bufflen,
1809 scsi_request_buffer, dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001811 /* Byte swap command ORB if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
1813
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001814 /* Put our scsi command in the command ORB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 memset(command_orb->cdb, 0, 12);
1816 memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817}
1818
1819/*
1820 * This function is called in order to begin a regular SBP-2 command.
1821 */
Stefan Richter28212762006-07-23 22:10:00 +02001822static void sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 struct sbp2_command_info *command)
1824{
1825 struct sbp2scsi_host_info *hi = scsi_id->hi;
1826 struct sbp2_command_orb *command_orb = &command->command_orb;
Stefan Richtercc078182006-07-23 22:12:00 +02001827 struct sbp2_command_orb *last_orb;
1828 dma_addr_t last_orb_dma;
1829 u64 addr = scsi_id->sbp2_command_block_agent_addr;
1830 quadlet_t data[2];
1831 size_t length;
1832 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 pci_dma_sync_single_for_device(hi->host->pdev, command->command_orb_dma,
1835 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02001836 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 pci_dma_sync_single_for_device(hi->host->pdev, command->sge_dma,
1838 sizeof(command->scatter_gather_element),
1839 PCI_DMA_BIDIRECTIONAL);
1840 /*
1841 * Check to see if there are any previous orbs to use
1842 */
Stefan Richtercc078182006-07-23 22:12:00 +02001843 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
1844 last_orb = scsi_id->last_orb;
1845 last_orb_dma = scsi_id->last_orb_dma;
1846 if (!last_orb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 /*
Stefan Richtercc078182006-07-23 22:12:00 +02001848 * last_orb == NULL means: We know that the target's fetch agent
1849 * is not active right now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 */
Stefan Richtercc078182006-07-23 22:12:00 +02001851 addr += SBP2_ORB_POINTER_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1853 data[1] = command->command_orb_dma;
1854 sbp2util_cpu_to_be32_buffer(data, 8);
Stefan Richtercc078182006-07-23 22:12:00 +02001855 length = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 /*
Stefan Richtercc078182006-07-23 22:12:00 +02001858 * last_orb != NULL means: We know that the target's fetch agent
1859 * is (very probably) not dead or in reset state right now.
1860 * We have an ORB already sent that we can append a new one to.
1861 * The target's fetch agent may or may not have read this
1862 * previous ORB yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 */
Stefan Richtercc078182006-07-23 22:12:00 +02001864 pci_dma_sync_single_for_cpu(hi->host->pdev, last_orb_dma,
1865 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02001866 PCI_DMA_TODEVICE);
Stefan Richtercc078182006-07-23 22:12:00 +02001867 last_orb->next_ORB_lo = cpu_to_be32(command->command_orb_dma);
1868 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /* Tells hardware that this pointer is valid */
Stefan Richtercc078182006-07-23 22:12:00 +02001870 last_orb->next_ORB_hi = 0;
1871 pci_dma_sync_single_for_device(hi->host->pdev, last_orb_dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02001873 PCI_DMA_TODEVICE);
Stefan Richtercc078182006-07-23 22:12:00 +02001874 addr += SBP2_DOORBELL_OFFSET;
1875 data[0] = 0;
1876 length = 4;
1877 }
1878 scsi_id->last_orb = command_orb;
1879 scsi_id->last_orb_dma = command->command_orb_dma;
1880 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Stefan Richter09ee67a2006-08-14 18:43:00 +02001882 if (sbp2util_node_write_no_wait(scsi_id->ne, addr, data, length)) {
1883 /*
1884 * sbp2util_node_write_no_wait failed. We certainly ran out
1885 * of transaction labels, perhaps just because there were no
1886 * context switches which gave khpsbpkt a chance to collect
1887 * free tlabels. Try again in non-atomic context. If necessary,
1888 * the workqueue job will sleep to guaranteedly get a tlabel.
1889 * We do not accept new commands until the job is over.
1890 */
1891 scsi_block_requests(scsi_id->scsi_host);
David Howellsc4028952006-11-22 14:57:56 +00001892 PREPARE_DELAYED_WORK(&scsi_id->protocol_work,
Stefan Richter09ee67a2006-08-14 18:43:00 +02001893 last_orb ? sbp2util_write_doorbell:
David Howellsc4028952006-11-22 14:57:56 +00001894 sbp2util_write_orb_pointer);
1895 schedule_delayed_work(&scsi_id->protocol_work, 0);
Stefan Richter09ee67a2006-08-14 18:43:00 +02001896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897}
1898
1899/*
1900 * This function is called in order to begin a regular SBP-2 command.
1901 */
1902static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
1903 struct scsi_cmnd *SCpnt,
1904 void (*done)(struct scsi_cmnd *))
1905{
1906 unchar *cmd = (unchar *) SCpnt->cmnd;
1907 unsigned int request_bufflen = SCpnt->request_bufflen;
1908 struct sbp2_command_info *command;
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 /*
1911 * Allocate a command orb and s/g structure
1912 */
1913 command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done);
Stefan Richteredf1fb22006-11-02 21:16:08 +01001914 if (!command)
Stefan Richtera237f352005-11-07 06:31:39 -05001915 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916
1917 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 * Now actually fill in the comamnd orb and sbp2 s/g list
1919 */
1920 sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg,
1921 request_bufflen, SCpnt->request_buffer,
1922 SCpnt->sc_data_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 * Link up the orb, and ring the doorbell if needed
1926 */
1927 sbp2_link_orb_command(scsi_id, command);
1928
Stefan Richtera237f352005-11-07 06:31:39 -05001929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}
1931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 * Translates SBP-2 status into SCSI sense data for check conditions
1934 */
1935static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
1936{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 /*
1938 * Ok, it's pretty ugly... ;-)
1939 */
1940 sense_data[0] = 0x70;
1941 sense_data[1] = 0x0;
1942 sense_data[2] = sbp2_status[9];
1943 sense_data[3] = sbp2_status[12];
1944 sense_data[4] = sbp2_status[13];
1945 sense_data[5] = sbp2_status[14];
1946 sense_data[6] = sbp2_status[15];
1947 sense_data[7] = 10;
1948 sense_data[8] = sbp2_status[16];
1949 sense_data[9] = sbp2_status[17];
1950 sense_data[10] = sbp2_status[18];
1951 sense_data[11] = sbp2_status[19];
1952 sense_data[12] = sbp2_status[10];
1953 sense_data[13] = sbp2_status[11];
1954 sense_data[14] = sbp2_status[20];
1955 sense_data[15] = sbp2_status[21];
1956
Stefan Richtera237f352005-11-07 06:31:39 -05001957 return sbp2_status[8] & 0x3f; /* return scsi status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958}
1959
1960/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 * This function deals with status writes from the SBP-2 device
1962 */
Stefan Richter3e98eab42006-07-23 22:16:00 +02001963static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
1964 int destid, quadlet_t *data, u64 addr,
1965 size_t length, u16 fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966{
1967 struct sbp2scsi_host_info *hi;
1968 struct scsi_id_instance_data *scsi_id = NULL, *scsi_id_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 struct scsi_cmnd *SCpnt = NULL;
Stefan Richter3e98eab42006-07-23 22:16:00 +02001970 struct sbp2_status_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
1972 struct sbp2_command_info *command;
Jody McIntyre79456192005-11-07 06:29:39 -05001973 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
Stefan Richter60657722006-07-23 22:18:00 +02001975 if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
1976 SBP2_ERR("Wrong size of status block");
1977 return RCODE_ADDRESS_ERROR;
1978 }
1979 if (unlikely(!host)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 SBP2_ERR("host is NULL - this is bad!");
Stefan Richtera237f352005-11-07 06:31:39 -05001981 return RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
Stefan Richter60657722006-07-23 22:18:00 +02001984 if (unlikely(!hi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 SBP2_ERR("host info is NULL - this is bad!");
Stefan Richtera237f352005-11-07 06:31:39 -05001986 return RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 /*
Stefan Richter35bdddb2006-01-31 00:13:33 -05001989 * Find our scsi_id structure by looking at the status fifo address
1990 * written to by the sbp2 device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 list_for_each_entry(scsi_id_tmp, &hi->scsi_ids, scsi_list) {
Stefan Richter35bdddb2006-01-31 00:13:33 -05001993 if (scsi_id_tmp->ne->nodeid == nodeid &&
1994 scsi_id_tmp->status_fifo_addr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 scsi_id = scsi_id_tmp;
1996 break;
1997 }
1998 }
Stefan Richter60657722006-07-23 22:18:00 +02001999 if (unlikely(!scsi_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 SBP2_ERR("scsi_id is NULL - device is gone?");
Stefan Richtera237f352005-11-07 06:31:39 -05002001 return RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 }
2003
2004 /*
Stefan Richter3e98eab42006-07-23 22:16:00 +02002005 * Put response into scsi_id status fifo buffer. The first two bytes
2006 * come in big endian bit order. Often the target writes only a
2007 * truncated status block, minimally the first two quadlets. The rest
2008 * is implied to be zeros.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 */
Stefan Richter3e98eab42006-07-23 22:16:00 +02002010 sb = &scsi_id->status_block;
2011 memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
2012 memcpy(sb, data, length);
2013 sbp2util_be32_to_cpu_buffer(sb, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
2015 /*
Stefan Richter60657722006-07-23 22:18:00 +02002016 * Ignore unsolicited status. Handle command ORB status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 */
Stefan Richter60657722006-07-23 22:18:00 +02002018 if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
2019 command = NULL;
2020 else
2021 command = sbp2util_find_command_for_orb(scsi_id,
2022 sb->ORB_offset_lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if (command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
2025 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02002026 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
2028 sizeof(command->scatter_gather_element),
2029 PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 /*
Stefan Richter3e98eab42006-07-23 22:16:00 +02002031 * Matched status with command, now grab scsi command pointers
2032 * and check status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 */
Stefan Richter60657722006-07-23 22:18:00 +02002034 /*
2035 * FIXME: If the src field in the status is 1, the ORB DMA must
2036 * not be reused until status for a subsequent ORB is received.
2037 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 SCpnt = command->Current_SCpnt;
Stefan Richter24c7cd02006-04-01 21:11:41 +02002039 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 sbp2util_mark_command_completed(scsi_id, command);
Stefan Richter24c7cd02006-04-01 21:11:41 +02002041 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 if (SCpnt) {
Stefan Richterabbca102006-08-14 18:51:00 +02002044 u32 h = sb->ORB_offset_hi_misc;
2045 u32 r = STATUS_GET_RESP(h);
2046
2047 if (r != RESP_STATUS_REQUEST_COMPLETE) {
Stefan Richter35644092006-11-02 21:16:08 +01002048 SBP2_INFO("resp 0x%x, sbp_status 0x%x",
Stefan Richterabbca102006-08-14 18:51:00 +02002049 r, STATUS_GET_SBP_STATUS(h));
Stefan Richter60657722006-07-23 22:18:00 +02002050 scsi_status =
Stefan Richterabbca102006-08-14 18:51:00 +02002051 r == RESP_STATUS_TRANSPORT_FAILURE ?
2052 SBP2_SCSI_STATUS_BUSY :
Stefan Richter60657722006-07-23 22:18:00 +02002053 SBP2_SCSI_STATUS_COMMAND_TERMINATED;
Stefan Richterabbca102006-08-14 18:51:00 +02002054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 /*
Stefan Richter3e98eab42006-07-23 22:16:00 +02002056 * See if the target stored any scsi status information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 */
Stefan Richteredf1fb22006-11-02 21:16:08 +01002058 if (STATUS_GET_LEN(h) > 1)
Stefan Richter3e98eab42006-07-23 22:16:00 +02002059 scsi_status = sbp2_status_to_sense_data(
2060 (unchar *)sb, SCpnt->sense_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 /*
Stefan Richter3e98eab42006-07-23 22:16:00 +02002062 * Check to see if the dead bit is set. If so, we'll
2063 * have to initiate a fetch agent reset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 */
Stefan Richteredf1fb22006-11-02 21:16:08 +01002065 if (STATUS_TEST_DEAD(h))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 sbp2_agent_reset(scsi_id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 }
2068
2069 /*
Stefan Richtercc078182006-07-23 22:12:00 +02002070 * Check here to see if there are no commands in-use. If there
2071 * are none, we know that the fetch agent left the active state
2072 * _and_ that we did not reactivate it yet. Therefore clear
2073 * last_orb so that next time we write directly to the
2074 * ORB_POINTER register. That way the fetch agent does not need
2075 * to refetch the next_ORB.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 */
Jody McIntyre79456192005-11-07 06:29:39 -05002077 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Stefan Richtercc078182006-07-23 22:12:00 +02002078 if (list_empty(&scsi_id->sbp2_command_orb_inuse))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 scsi_id->last_orb = NULL;
Jody McIntyre79456192005-11-07 06:29:39 -05002080 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 /*
2084 * It's probably a login/logout/reconnect status.
2085 */
Stefan Richter3e98eab42006-07-23 22:16:00 +02002086 if ((sb->ORB_offset_lo == scsi_id->reconnect_orb_dma) ||
2087 (sb->ORB_offset_lo == scsi_id->login_orb_dma) ||
2088 (sb->ORB_offset_lo == scsi_id->query_logins_orb_dma) ||
Stefan Richtere8398bb2006-07-23 22:19:00 +02002089 (sb->ORB_offset_lo == scsi_id->logout_orb_dma)) {
2090 scsi_id->access_complete = 1;
2091 wake_up_interruptible(&access_wq);
2092 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
2094
Stefan Richteredf1fb22006-11-02 21:16:08 +01002095 if (SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt,
2097 command->Current_done);
Stefan Richtera237f352005-11-07 06:31:39 -05002098 return RCODE_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101/**************************************
2102 * SCSI interface related section
2103 **************************************/
2104
2105/*
2106 * This routine is the main request entry routine for doing I/O. It is
2107 * called from the scsi stack directly.
2108 */
2109static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
2110 void (*done)(struct scsi_cmnd *))
2111{
2112 struct scsi_id_instance_data *scsi_id =
2113 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2114 struct sbp2scsi_host_info *hi;
Jody McIntyreabd559b2005-09-30 11:59:06 -07002115 int result = DID_NO_CONNECT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
Jody McIntyreabd559b2005-09-30 11:59:06 -07002117 if (!sbp2util_node_is_available(scsi_id))
2118 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 hi = scsi_id->hi;
2121
2122 if (!hi) {
2123 SBP2_ERR("sbp2scsi_host_info is NULL - this is bad!");
Jody McIntyreabd559b2005-09-30 11:59:06 -07002124 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
2126
2127 /*
2128 * Until we handle multiple luns, just return selection time-out
2129 * to any IO directed at non-zero LUNs
2130 */
Jody McIntyreabd559b2005-09-30 11:59:06 -07002131 if (SCpnt->device->lun)
2132 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 /*
2135 * Check for request sense command, and handle it here
2136 * (autorequest sense)
2137 */
2138 if (SCpnt->cmnd[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen);
2140 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
2141 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done);
Jody McIntyreabd559b2005-09-30 11:59:06 -07002142 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 }
2144
2145 /*
2146 * Check to see if we are in the middle of a bus reset.
2147 */
2148 if (!hpsb_node_entry_valid(scsi_id->ne)) {
2149 SBP2_ERR("Bus reset in progress - rejecting command");
Jody McIntyreabd559b2005-09-30 11:59:06 -07002150 result = DID_BUS_BUSY << 16;
2151 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
2153
2154 /*
Stefan Richter43863eb2005-12-12 23:03:24 -05002155 * Bidirectional commands are not yet implemented,
2156 * and unknown transfer direction not handled.
2157 */
2158 if (SCpnt->sc_data_direction == DMA_BIDIRECTIONAL) {
2159 SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
2160 result = DID_ERROR << 16;
2161 goto done;
2162 }
2163
2164 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 * Try and send our SCSI command
2166 */
2167 if (sbp2_send_command(scsi_id, SCpnt, done)) {
2168 SBP2_ERR("Error sending SCSI command");
2169 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
2170 SCpnt, done);
2171 }
Jody McIntyreabd559b2005-09-30 11:59:06 -07002172 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173
Jody McIntyreabd559b2005-09-30 11:59:06 -07002174done:
2175 SCpnt->result = result;
2176 done(SCpnt);
2177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178}
2179
2180/*
2181 * This function is called in order to complete all outstanding SBP-2
2182 * commands (in case of resets, etc.).
2183 */
2184static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
2185 u32 status)
2186{
2187 struct sbp2scsi_host_info *hi = scsi_id->hi;
2188 struct list_head *lh;
2189 struct sbp2_command_info *command;
Jody McIntyre79456192005-11-07 06:29:39 -05002190 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Jody McIntyre79456192005-11-07 06:29:39 -05002192 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 while (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 lh = scsi_id->sbp2_command_orb_inuse.next;
2195 command = list_entry(lh, struct sbp2_command_info, list);
2196 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
2197 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02002198 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
2200 sizeof(command->scatter_gather_element),
2201 PCI_DMA_BIDIRECTIONAL);
2202 sbp2util_mark_command_completed(scsi_id, command);
2203 if (command->Current_SCpnt) {
2204 command->Current_SCpnt->result = status << 16;
2205 command->Current_done(command->Current_SCpnt);
2206 }
2207 }
Jody McIntyre79456192005-11-07 06:29:39 -05002208 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 return;
2211}
2212
2213/*
2214 * This function is called in order to complete a regular SBP-2 command.
2215 *
2216 * This can be called in interrupt context.
2217 */
2218static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
2219 u32 scsi_status, struct scsi_cmnd *SCpnt,
2220 void (*done)(struct scsi_cmnd *))
2221{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 /*
2223 * Sanity
2224 */
2225 if (!SCpnt) {
2226 SBP2_ERR("SCpnt is NULL");
2227 return;
2228 }
2229
2230 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 * Switch on scsi status
2232 */
2233 switch (scsi_status) {
Stefan Richtera237f352005-11-07 06:31:39 -05002234 case SBP2_SCSI_STATUS_GOOD:
Stefan Richter8f0525f2006-03-28 20:03:45 -05002235 SCpnt->result = DID_OK << 16;
Stefan Richtera237f352005-11-07 06:31:39 -05002236 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Stefan Richtera237f352005-11-07 06:31:39 -05002238 case SBP2_SCSI_STATUS_BUSY:
2239 SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
2240 SCpnt->result = DID_BUS_BUSY << 16;
2241 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242
Stefan Richtera237f352005-11-07 06:31:39 -05002243 case SBP2_SCSI_STATUS_CHECK_CONDITION:
Stefan Richter8f0525f2006-03-28 20:03:45 -05002244 SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
Stefan Richtera237f352005-11-07 06:31:39 -05002245 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Stefan Richtera237f352005-11-07 06:31:39 -05002247 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
2248 SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
2249 SCpnt->result = DID_NO_CONNECT << 16;
2250 scsi_print_command(SCpnt);
2251 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Stefan Richtera237f352005-11-07 06:31:39 -05002253 case SBP2_SCSI_STATUS_CONDITION_MET:
2254 case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
2255 case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
2256 SBP2_ERR("Bad SCSI status = %x", scsi_status);
2257 SCpnt->result = DID_ERROR << 16;
2258 scsi_print_command(SCpnt);
2259 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Stefan Richtera237f352005-11-07 06:31:39 -05002261 default:
2262 SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
2263 SCpnt->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 }
2265
2266 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 * If a bus reset is in progress and there was an error, complete
2268 * the command as busy so that it will get retried.
2269 */
Stefan Richtera237f352005-11-07 06:31:39 -05002270 if (!hpsb_node_entry_valid(scsi_id->ne)
2271 && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 SBP2_ERR("Completing command with busy (bus reset)");
2273 SCpnt->result = DID_BUS_BUSY << 16;
2274 }
2275
2276 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 * Tell scsi stack that we're done with this command
2278 */
Stefan Richtera237f352005-11-07 06:31:39 -05002279 done(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280}
2281
Jody McIntyreabd559b2005-09-30 11:59:06 -07002282static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
2283{
Stefan Richtera80614d2006-02-14 22:04:19 -05002284 struct scsi_id_instance_data *scsi_id =
2285 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2286
2287 scsi_id->sdev = sdev;
Stefan Richterc394f1e2006-08-07 20:48:00 +02002288 sdev->allow_restart = 1;
Stefan Richtera80614d2006-02-14 22:04:19 -05002289
Stefan Richter24d3bf82006-05-15 22:04:59 +02002290 if (scsi_id->workarounds & SBP2_WORKAROUND_INQUIRY_36)
Stefan Richtera80614d2006-02-14 22:04:19 -05002291 sdev->inquiry_len = 36;
Jody McIntyreabd559b2005-09-30 11:59:06 -07002292 return 0;
2293}
2294
Jody McIntyreabd559b2005-09-30 11:59:06 -07002295static int sbp2scsi_slave_configure(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
Stefan Richter24d3bf82006-05-15 22:04:59 +02002297 struct scsi_id_instance_data *scsi_id =
2298 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2299
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
Ben Collins365c7862005-11-07 06:31:24 -05002301 sdev->use_10_for_rw = 1;
Stefan Richter24d3bf82006-05-15 22:04:59 +02002302
2303 if (sdev->type == TYPE_DISK &&
2304 scsi_id->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
2305 sdev->skip_ms_page_8 = 1;
Stefan Richtere9a1c522006-05-15 22:06:37 +02002306 if (scsi_id->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
2307 sdev->fix_capacity = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 return 0;
2309}
2310
Jody McIntyreabd559b2005-09-30 11:59:06 -07002311static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
2312{
2313 ((struct scsi_id_instance_data *)sdev->host->hostdata[0])->sdev = NULL;
2314 return;
2315}
2316
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317/*
2318 * Called by scsi stack when something has really gone wrong. Usually
2319 * called when a command has timed-out for some reason.
2320 */
2321static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
2322{
2323 struct scsi_id_instance_data *scsi_id =
2324 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2325 struct sbp2scsi_host_info *hi = scsi_id->hi;
2326 struct sbp2_command_info *command;
Stefan Richter24c7cd02006-04-01 21:11:41 +02002327 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Stefan Richter35644092006-11-02 21:16:08 +01002329 SBP2_INFO("aborting sbp2 command");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 scsi_print_command(SCpnt);
2331
Jody McIntyreabd559b2005-09-30 11:59:06 -07002332 if (sbp2util_node_is_available(scsi_id)) {
Stefan Richter23077f12006-09-11 20:17:14 +02002333 sbp2_agent_reset(scsi_id, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Stefan Richter23077f12006-09-11 20:17:14 +02002335 /* Return a matching command structure to the free pool. */
Stefan Richter24c7cd02006-04-01 21:11:41 +02002336 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
2338 if (command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 pci_dma_sync_single_for_cpu(hi->host->pdev,
2340 command->command_orb_dma,
2341 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02002342 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 pci_dma_sync_single_for_cpu(hi->host->pdev,
2344 command->sge_dma,
2345 sizeof(command->scatter_gather_element),
2346 PCI_DMA_BIDIRECTIONAL);
2347 sbp2util_mark_command_completed(scsi_id, command);
2348 if (command->Current_SCpnt) {
2349 command->Current_SCpnt->result = DID_ABORT << 16;
2350 command->Current_done(command->Current_SCpnt);
2351 }
2352 }
Stefan Richter24c7cd02006-04-01 21:11:41 +02002353 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
2356 }
2357
Stefan Richtera237f352005-11-07 06:31:39 -05002358 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359}
2360
2361/*
2362 * Called by scsi stack when something has really gone wrong.
2363 */
Jody McIntyreabd559b2005-09-30 11:59:06 -07002364static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
2366 struct scsi_id_instance_data *scsi_id =
2367 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2368
Stefan Richter35644092006-11-02 21:16:08 +01002369 SBP2_INFO("reset requested");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Jody McIntyreabd559b2005-09-30 11:59:06 -07002371 if (sbp2util_node_is_available(scsi_id)) {
Stefan Richter35644092006-11-02 21:16:08 +01002372 SBP2_INFO("generating sbp2 fetch agent reset");
Stefan Richter1f427e82006-08-14 18:44:00 +02002373 sbp2_agent_reset(scsi_id, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
2375
Jody McIntyreabd559b2005-09-30 11:59:06 -07002376 return SUCCESS;
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04002377}
2378
Stefan Richtera237f352005-11-07 06:31:39 -05002379static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
2380 struct device_attribute *attr,
2381 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
2383 struct scsi_device *sdev;
2384 struct scsi_id_instance_data *scsi_id;
2385 int lun;
2386
2387 if (!(sdev = to_scsi_device(dev)))
2388 return 0;
2389
2390 if (!(scsi_id = (struct scsi_id_instance_data *)sdev->host->hostdata[0]))
2391 return 0;
2392
Ben Collinse309fc62005-11-07 06:31:34 -05002393 lun = ORB_SET_LUN(scsi_id->sbp2_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
2395 return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)scsi_id->ne->guid,
2396 scsi_id->ud->id, lun);
2397}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
2399MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2400MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2401MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2402MODULE_LICENSE("GPL");
2403
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404static int sbp2_module_init(void)
2405{
2406 int ret;
2407
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 /* Module load debug option to force one command at a time (serializing I/O) */
2409 if (serialize_io) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 scsi_driver_template.can_queue = 1;
2411 scsi_driver_template.cmd_per_lun = 1;
2412 }
2413
Stefan Richter24d3bf82006-05-15 22:04:59 +02002414 if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
2415 (max_sectors * 512) > (128 * 1024))
2416 max_sectors = 128 * 1024 / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 scsi_driver_template.max_sectors = max_sectors;
2418
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 /* Register our high level driver with 1394 stack */
2420 hpsb_register_highlevel(&sbp2_highlevel);
2421
2422 ret = hpsb_register_protocol(&sbp2_driver);
2423 if (ret) {
2424 SBP2_ERR("Failed to register protocol");
2425 hpsb_unregister_highlevel(&sbp2_highlevel);
2426 return ret;
2427 }
2428
2429 return 0;
2430}
2431
2432static void __exit sbp2_module_exit(void)
2433{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 hpsb_unregister_protocol(&sbp2_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 hpsb_unregister_highlevel(&sbp2_highlevel);
2436}
2437
2438module_init(sbp2_module_init);
2439module_exit(sbp2_module_exit);