blob: 6737af26c860c34f30407c85933bea7b8dfc0bf8 [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 */
Stefan Richterca0c7452006-11-02 21:16:08 +0100101static int sbp2_max_speed = IEEE1394_SPEED_MAX;
102module_param_named(max_speed, sbp2_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 */
Stefan Richterca0c7452006-11-02 21:16:08 +0100112static int sbp2_serialize_io = 1;
113module_param_named(serialize_io, sbp2_serialize_io, int, 0444);
114MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers "
115 "(default = 1, faster = 0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117/*
118 * Bump up max_sectors if you'd like to support very large sized
119 * transfers. Please note that some older sbp2 bridge chips are broken for
120 * transfers greater or equal to 128KB. Default is a value of 255
121 * sectors, or just under 128KB (at 512 byte sector size). I can note that
122 * the Oxsemi sbp2 chipsets have no problems supporting very large
123 * transfer sizes.
124 */
Stefan Richterca0c7452006-11-02 21:16:08 +0100125static int sbp2_max_sectors = SBP2_MAX_SECTORS;
126module_param_named(max_sectors, sbp2_max_sectors, int, 0444);
127MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported "
128 "(default = " __stringify(SBP2_MAX_SECTORS) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/*
131 * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
132 * do an exclusive login, as it's generally unsafe to have two hosts
133 * talking to a single sbp2 device at the same time (filesystem coherency,
134 * etc.). If you're running an sbp2 device that supports multiple logins,
135 * and you're either running read-only filesystems or some sort of special
Ben Collins20f45782006-06-12 18:13:11 -0400136 * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster
137 * File System, or Lustre, then set exclusive_login to zero.
138 *
139 * So far only bridges from Oxford Semiconductor are known to support
140 * concurrent logins. Depending on firmware, four or two concurrent logins
141 * are possible on OXFW911 and newer Oxsemi bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 */
Stefan Richterca0c7452006-11-02 21:16:08 +0100143static int sbp2_exclusive_login = 1;
144module_param_named(exclusive_login, sbp2_exclusive_login, int, 0644);
145MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
146 "(default = 1)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148/*
Stefan Richter24d3bf82006-05-15 22:04:59 +0200149 * If any of the following workarounds is required for your device to work,
150 * please submit the kernel messages logged by sbp2 to the linux1394-devel
151 * mailing list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 *
Stefan Richter24d3bf82006-05-15 22:04:59 +0200153 * - 128kB max transfer
154 * Limit transfer size. Necessary for some old bridges.
155 *
156 * - 36 byte inquiry
157 * When scsi_mod probes the device, let the inquiry command look like that
158 * from MS Windows.
159 *
160 * - skip mode page 8
161 * Suppress sending of mode_sense for mode page 8 if the device pretends to
162 * support the SCSI Primary Block commands instead of Reduced Block Commands.
Stefan Richtere9a1c522006-05-15 22:06:37 +0200163 *
164 * - fix capacity
165 * Tell sd_mod to correct the last sector number reported by read_capacity.
166 * Avoids access beyond actual disk limits on devices with an off-by-one bug.
167 * Don't use this with devices which don't have this bug.
Stefan Richter679c0cd2006-05-15 22:08:09 +0200168 *
169 * - override internal blacklist
170 * Instead of adding to the built-in blacklist, use only the workarounds
171 * specified in the module load parameter.
172 * Useful if a blacklist entry interfered with a non-broken device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 */
Stefan Richter24d3bf82006-05-15 22:04:59 +0200174static int sbp2_default_workarounds;
175module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
176MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
177 ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
178 ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
179 ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
Stefan Richtere9a1c522006-05-15 22:06:37 +0200180 ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
Stefan Richter679c0cd2006-05-15 22:08:09 +0200181 ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
Stefan Richter24d3bf82006-05-15 22:04:59 +0200182 ", or a combination)");
183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Stefan Richteredf1fb22006-11-02 21:16:08 +0100185#define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
186#define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/*
189 * Globals
190 */
Stefan Richterea42ea02006-11-02 21:16:08 +0100191static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *, u32);
192static void sbp2scsi_complete_command(struct scsi_id_instance_data *, u32,
193 struct scsi_cmnd *,
194 void (*)(struct scsi_cmnd *));
195static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *);
196static int sbp2_start_device(struct scsi_id_instance_data *);
197static void sbp2_remove_device(struct scsi_id_instance_data *);
198static int sbp2_login_device(struct scsi_id_instance_data *);
199static int sbp2_reconnect_device(struct scsi_id_instance_data *);
200static int sbp2_logout_device(struct scsi_id_instance_data *);
201static void sbp2_host_reset(struct hpsb_host *);
202static int sbp2_handle_status_write(struct hpsb_host *, int, int, quadlet_t *,
203 u64, size_t, u16);
204static int sbp2_agent_reset(struct scsi_id_instance_data *, int);
205static void sbp2_parse_unit_directory(struct scsi_id_instance_data *,
206 struct unit_directory *);
207static int sbp2_set_busy_timeout(struct scsi_id_instance_data *);
208static int sbp2_max_speed_and_size(struct scsi_id_instance_data *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213static struct hpsb_highlevel sbp2_highlevel = {
Stefan Richterea42ea02006-11-02 21:16:08 +0100214 .name = SBP2_DEVICE_NAME,
215 .host_reset = sbp2_host_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216};
217
218static struct hpsb_address_ops sbp2_ops = {
Stefan Richterea42ea02006-11-02 21:16:08 +0100219 .write = sbp2_handle_status_write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220};
221
222#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
Stefan Richterea42ea02006-11-02 21:16:08 +0100223static int sbp2_handle_physdma_write(struct hpsb_host *, int, int, quadlet_t *,
224 u64, size_t, u16);
225static int sbp2_handle_physdma_read(struct hpsb_host *, int, quadlet_t *, u64,
226 size_t, u16);
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static struct hpsb_address_ops sbp2_physdma_ops = {
Stefan Richterea42ea02006-11-02 21:16:08 +0100229 .read = sbp2_handle_physdma_read,
230 .write = sbp2_handle_physdma_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231};
232#endif
233
Stefan Richterea42ea02006-11-02 21:16:08 +0100234
235/*
236 * Interface to driver core and IEEE 1394 core
237 */
238static struct ieee1394_device_id sbp2_id_table[] = {
239 {
240 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
241 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
242 .version = SBP2_SW_VERSION_ENTRY & 0xffffff},
243 {}
244};
245MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
246
247static int sbp2_probe(struct device *);
248static int sbp2_remove(struct device *);
249static int sbp2_update(struct unit_directory *);
250
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251static struct hpsb_protocol_driver sbp2_driver = {
252 .name = "SBP2 Driver",
253 .id_table = sbp2_id_table,
254 .update = sbp2_update,
255 .driver = {
256 .name = SBP2_DEVICE_NAME,
257 .bus = &ieee1394_bus_type,
258 .probe = sbp2_probe,
259 .remove = sbp2_remove,
260 },
261};
262
Stefan Richterea42ea02006-11-02 21:16:08 +0100263
264/*
265 * Interface to SCSI core
266 */
267static int sbp2scsi_queuecommand(struct scsi_cmnd *,
268 void (*)(struct scsi_cmnd *));
269static int sbp2scsi_abort(struct scsi_cmnd *);
270static int sbp2scsi_reset(struct scsi_cmnd *);
271static int sbp2scsi_slave_alloc(struct scsi_device *);
272static int sbp2scsi_slave_configure(struct scsi_device *);
273static void sbp2scsi_slave_destroy(struct scsi_device *);
274static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *,
275 struct device_attribute *, char *);
276
277static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
278
279static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
280 &dev_attr_ieee1394_id,
281 NULL
282};
283
Stefan Richterca0c7452006-11-02 21:16:08 +0100284static struct scsi_host_template sbp2_shost_template = {
Stefan Richterea42ea02006-11-02 21:16:08 +0100285 .module = THIS_MODULE,
286 .name = "SBP-2 IEEE-1394",
287 .proc_name = SBP2_DEVICE_NAME,
288 .queuecommand = sbp2scsi_queuecommand,
289 .eh_abort_handler = sbp2scsi_abort,
290 .eh_device_reset_handler = sbp2scsi_reset,
291 .slave_alloc = sbp2scsi_slave_alloc,
292 .slave_configure = sbp2scsi_slave_configure,
293 .slave_destroy = sbp2scsi_slave_destroy,
294 .this_id = -1,
295 .sg_tablesize = SG_ALL,
296 .use_clustering = ENABLE_CLUSTERING,
297 .cmd_per_lun = SBP2_MAX_CMDS,
298 .can_queue = SBP2_MAX_CMDS,
299 .emulated = 1,
300 .sdev_attrs = sbp2_sysfs_sdev_attrs,
301};
302
303
Stefan Richtera80614d2006-02-14 22:04:19 -0500304/*
Stefan Richter24d3bf82006-05-15 22:04:59 +0200305 * List of devices with known bugs.
306 *
307 * The firmware_revision field, masked with 0xffff00, is the best indicator
308 * for the type of bridge chip of a device. It yields a few false positives
309 * but this did not break correctly behaving devices so far.
Stefan Richtera80614d2006-02-14 22:04:19 -0500310 */
Stefan Richter24d3bf82006-05-15 22:04:59 +0200311static const struct {
312 u32 firmware_revision;
Stefan Richtere9a1c522006-05-15 22:06:37 +0200313 u32 model_id;
Stefan Richter24d3bf82006-05-15 22:04:59 +0200314 unsigned workarounds;
315} sbp2_workarounds_table[] = {
Ben Collins4b9a3342006-06-12 18:10:18 -0400316 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
Stefan Richter24d3bf82006-05-15 22:04:59 +0200317 .firmware_revision = 0x002800,
Ben Collins4b9a3342006-06-12 18:10:18 -0400318 .model_id = 0x001010,
Stefan Richter24d3bf82006-05-15 22:04:59 +0200319 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
320 SBP2_WORKAROUND_MODE_SENSE_8,
321 },
322 /* Initio bridges, actually only needed for some older ones */ {
323 .firmware_revision = 0x000200,
324 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
325 },
326 /* Symbios bridge */ {
327 .firmware_revision = 0xa0b800,
328 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
Stefan Richtere9a1c522006-05-15 22:06:37 +0200329 },
330 /*
331 * Note about the following Apple iPod blacklist entries:
332 *
333 * There are iPods (2nd gen, 3rd gen) with model_id==0. Since our
334 * matching logic treats 0 as a wildcard, we cannot match this ID
335 * without rewriting the matching routine. Fortunately these iPods
336 * do not feature the read_capacity bug according to one report.
337 * Read_capacity behaviour as well as model_id could change due to
338 * Apple-supplied firmware updates though.
339 */
340 /* iPod 4th generation */ {
341 .firmware_revision = 0x0a2700,
342 .model_id = 0x000021,
343 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
344 },
345 /* iPod mini */ {
346 .firmware_revision = 0x0a2700,
347 .model_id = 0x000023,
348 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
349 },
350 /* iPod Photo */ {
351 .firmware_revision = 0x0a2700,
352 .model_id = 0x00007e,
353 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
Stefan Richter24d3bf82006-05-15 22:04:59 +0200354 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355};
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/**************************************
358 * General utility functions
359 **************************************/
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#ifndef __BIG_ENDIAN
362/*
363 * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
364 */
Stefan Richter2b01b802006-07-03 12:02:28 -0400365static inline void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 u32 *temp = buffer;
368
369 for (length = (length >> 2); length--; )
370 temp[length] = be32_to_cpu(temp[length]);
371
372 return;
373}
374
375/*
376 * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
377 */
Stefan Richter2b01b802006-07-03 12:02:28 -0400378static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 u32 *temp = buffer;
381
382 for (length = (length >> 2); length--; )
383 temp[length] = cpu_to_be32(temp[length]);
384
385 return;
386}
387#else /* BIG_ENDIAN */
388/* Why waste the cpu cycles? */
Stefan Richter611aa192006-08-02 18:44:00 +0200389#define sbp2util_be32_to_cpu_buffer(x,y) do {} while (0)
390#define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391#endif
392
Stefan Richterca0c7452006-11-02 21:16:08 +0100393static DECLARE_WAIT_QUEUE_HEAD(sbp2_access_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Stefan Richtere8398bb2006-07-23 22:19:00 +0200395/*
396 * Waits for completion of an SBP-2 access request.
397 * Returns nonzero if timed out or prematurely interrupted.
398 */
399static int sbp2util_access_timeout(struct scsi_id_instance_data *scsi_id,
400 int timeout)
401{
Stefan Richterca0c7452006-11-02 21:16:08 +0100402 long leftover;
Stefan Richtere8398bb2006-07-23 22:19:00 +0200403
Stefan Richterca0c7452006-11-02 21:16:08 +0100404 leftover = wait_event_interruptible_timeout(
405 sbp2_access_wq, scsi_id->access_complete, timeout);
Stefan Richtere8398bb2006-07-23 22:19:00 +0200406 scsi_id->access_complete = 0;
407 return leftover <= 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410static void sbp2_free_packet(struct hpsb_packet *packet)
411{
412 hpsb_free_tlabel(packet);
413 hpsb_free_packet(packet);
414}
415
Stefan Richtere8ca5662006-11-02 21:16:08 +0100416/*
417 * This is much like hpsb_node_write(), except it ignores the response
418 * subaction and returns immediately. Can be used from atomic context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 */
420static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
Stefan Richtera237f352005-11-07 06:31:39 -0500421 quadlet_t *buffer, size_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 struct hpsb_packet *packet;
424
425 packet = hpsb_make_writepacket(ne->host, ne->nodeid,
426 addr, buffer, length);
Stefan Richtera237f352005-11-07 06:31:39 -0500427 if (!packet)
428 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Stefan Richtera237f352005-11-07 06:31:39 -0500430 hpsb_set_packet_complete_task(packet,
431 (void (*)(void *))sbp2_free_packet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 packet);
433
434 hpsb_node_fill_packet(ne, packet);
435
Stefan Richtera237f352005-11-07 06:31:39 -0500436 if (hpsb_send_packet(packet) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 sbp2_free_packet(packet);
438 return -EIO;
439 }
440
441 return 0;
442}
443
Stefan Richter09ee67a2006-08-14 18:43:00 +0200444static void sbp2util_notify_fetch_agent(struct scsi_id_instance_data *scsi_id,
445 u64 offset, quadlet_t *data, size_t len)
446{
447 /*
448 * There is a small window after a bus reset within which the node
449 * entry's generation is current but the reconnect wasn't completed.
450 */
Stefan Richter2cccbb52006-08-14 18:59:00 +0200451 if (unlikely(atomic_read(&scsi_id->state) == SBP2LU_STATE_IN_RESET))
Stefan Richter09ee67a2006-08-14 18:43:00 +0200452 return;
453
454 if (hpsb_node_write(scsi_id->ne,
Stefan Richterca0c7452006-11-02 21:16:08 +0100455 scsi_id->command_block_agent_addr + offset,
Stefan Richter09ee67a2006-08-14 18:43:00 +0200456 data, len))
457 SBP2_ERR("sbp2util_notify_fetch_agent failed.");
458 /*
459 * Now accept new SCSI commands, unless a bus reset happended during
460 * hpsb_node_write.
461 */
Stefan Richter2cccbb52006-08-14 18:59:00 +0200462 if (likely(atomic_read(&scsi_id->state) != SBP2LU_STATE_IN_RESET))
Stefan Richter09ee67a2006-08-14 18:43:00 +0200463 scsi_unblock_requests(scsi_id->scsi_host);
464}
465
David Howellsc4028952006-11-22 14:57:56 +0000466static void sbp2util_write_orb_pointer(struct work_struct *work)
Stefan Richter09ee67a2006-08-14 18:43:00 +0200467{
468 quadlet_t data[2];
469
Stefan Richterd19c7762006-12-07 22:40:33 +0100470 data[0] = ORB_SET_NODE_ID(
471 (container_of(work, struct scsi_id_instance_data, protocol_work))->hi->host->node_id);
472 data[1] = (container_of(work, struct scsi_id_instance_data, protocol_work))->last_orb_dma;
Stefan Richter09ee67a2006-08-14 18:43:00 +0200473 sbp2util_cpu_to_be32_buffer(data, 8);
Stefan Richterd19c7762006-12-07 22:40:33 +0100474 sbp2util_notify_fetch_agent(container_of(work, struct scsi_id_instance_data, protocol_work), SBP2_ORB_POINTER_OFFSET, data, 8);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200475}
476
David Howellsc4028952006-11-22 14:57:56 +0000477static void sbp2util_write_doorbell(struct work_struct *work)
Stefan Richter09ee67a2006-08-14 18:43:00 +0200478{
Stefan Richterd19c7762006-12-07 22:40:33 +0100479 sbp2util_notify_fetch_agent(container_of(work, struct scsi_id_instance_data, protocol_work), SBP2_DOORBELL_OFFSET, NULL, 4);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200480}
481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id)
483{
Stefan Richterca0c7452006-11-02 21:16:08 +0100484 struct sbp2_fwhost_info *hi = scsi_id->hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int i;
486 unsigned long flags, orbs;
487 struct sbp2_command_info *command;
488
Stefan Richterca0c7452006-11-02 21:16:08 +0100489 orbs = sbp2_serialize_io ? 2 : SBP2_MAX_CMDS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Stefan Richterca0c7452006-11-02 21:16:08 +0100491 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 for (i = 0; i < orbs; i++) {
Stefan Richter85511582005-11-07 06:31:45 -0500493 command = kzalloc(sizeof(*command), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 if (!command) {
Stefan Richterca0c7452006-11-02 21:16:08 +0100495 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock,
Stefan Richtera237f352005-11-07 06:31:39 -0500496 flags);
497 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 command->command_orb_dma =
Stefan Richtera237f352005-11-07 06:31:39 -0500500 pci_map_single(hi->host->pdev, &command->command_orb,
501 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +0200502 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 command->sge_dma =
Stefan Richtera237f352005-11-07 06:31:39 -0500504 pci_map_single(hi->host->pdev,
505 &command->scatter_gather_element,
506 sizeof(command->scatter_gather_element),
507 PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 INIT_LIST_HEAD(&command->list);
Stefan Richterca0c7452006-11-02 21:16:08 +0100509 list_add_tail(&command->list, &scsi_id->cmd_orb_completed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Stefan Richterca0c7452006-11-02 21:16:08 +0100511 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return 0;
513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id)
516{
517 struct hpsb_host *host = scsi_id->hi->host;
518 struct list_head *lh, *next;
519 struct sbp2_command_info *command;
520 unsigned long flags;
521
Stefan Richterca0c7452006-11-02 21:16:08 +0100522 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
523 if (!list_empty(&scsi_id->cmd_orb_completed)) {
524 list_for_each_safe(lh, next, &scsi_id->cmd_orb_completed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 command = list_entry(lh, struct sbp2_command_info, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 pci_unmap_single(host->pdev, command->command_orb_dma,
527 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +0200528 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 pci_unmap_single(host->pdev, command->sge_dma,
530 sizeof(command->scatter_gather_element),
531 PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 kfree(command);
533 }
534 }
Stefan Richterca0c7452006-11-02 21:16:08 +0100535 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return;
537}
538
539/*
Stefan Richtere8ca5662006-11-02 21:16:08 +0100540 * Finds the sbp2_command for a given outstanding command ORB.
541 * Only looks at the in-use list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 */
543static struct sbp2_command_info *sbp2util_find_command_for_orb(
544 struct scsi_id_instance_data *scsi_id, dma_addr_t orb)
545{
546 struct sbp2_command_info *command;
547 unsigned long flags;
548
Stefan Richterca0c7452006-11-02 21:16:08 +0100549 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
550 if (!list_empty(&scsi_id->cmd_orb_inuse)) {
551 list_for_each_entry(command, &scsi_id->cmd_orb_inuse, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (command->command_orb_dma == orb) {
Stefan Richterca0c7452006-11-02 21:16:08 +0100553 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
Stefan Richtera237f352005-11-07 06:31:39 -0500554 return command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
556 }
557 }
Stefan Richterca0c7452006-11-02 21:16:08 +0100558 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
Stefan Richtera237f352005-11-07 06:31:39 -0500559 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
562/*
Stefan Richtere8ca5662006-11-02 21:16:08 +0100563 * Finds the sbp2_command for a given outstanding SCpnt.
564 * Only looks at the in-use list.
Stefan Richterca0c7452006-11-02 21:16:08 +0100565 * Must be called with scsi_id->cmd_orb_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 */
Stefan Richter24c7cd02006-04-01 21:11:41 +0200567static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
568 struct scsi_id_instance_data *scsi_id, void *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 struct sbp2_command_info *command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Stefan Richterca0c7452006-11-02 21:16:08 +0100572 if (!list_empty(&scsi_id->cmd_orb_inuse))
573 list_for_each_entry(command, &scsi_id->cmd_orb_inuse, list)
Stefan Richter24c7cd02006-04-01 21:11:41 +0200574 if (command->Current_SCpnt == SCpnt)
Stefan Richtera237f352005-11-07 06:31:39 -0500575 return command;
Stefan Richtera237f352005-11-07 06:31:39 -0500576 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577}
578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579static struct sbp2_command_info *sbp2util_allocate_command_orb(
580 struct scsi_id_instance_data *scsi_id,
581 struct scsi_cmnd *Current_SCpnt,
582 void (*Current_done)(struct scsi_cmnd *))
583{
584 struct list_head *lh;
585 struct sbp2_command_info *command = NULL;
586 unsigned long flags;
587
Stefan Richterca0c7452006-11-02 21:16:08 +0100588 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
589 if (!list_empty(&scsi_id->cmd_orb_completed)) {
590 lh = scsi_id->cmd_orb_completed.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 list_del(lh);
592 command = list_entry(lh, struct sbp2_command_info, list);
593 command->Current_done = Current_done;
594 command->Current_SCpnt = Current_SCpnt;
Stefan Richterca0c7452006-11-02 21:16:08 +0100595 list_add_tail(&command->list, &scsi_id->cmd_orb_inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 } else {
Stefan Richterd024ebc2006-03-28 20:03:55 -0500597 SBP2_ERR("%s: no orbs available", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 }
Stefan Richterca0c7452006-11-02 21:16:08 +0100599 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
Stefan Richtera237f352005-11-07 06:31:39 -0500600 return command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603static void sbp2util_free_command_dma(struct sbp2_command_info *command)
604{
605 struct scsi_id_instance_data *scsi_id =
606 (struct scsi_id_instance_data *)command->Current_SCpnt->device->host->hostdata[0];
607 struct hpsb_host *host;
608
609 if (!scsi_id) {
Stefan Richterd024ebc2006-03-28 20:03:55 -0500610 SBP2_ERR("%s: scsi_id == NULL", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return;
612 }
613
614 host = scsi_id->ud->ne->host;
615
616 if (command->cmd_dma) {
Stefan Richteredf1fb22006-11-02 21:16:08 +0100617 if (command->dma_type == CMD_DMA_SINGLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 pci_unmap_single(host->pdev, command->cmd_dma,
619 command->dma_size, command->dma_dir);
Stefan Richteredf1fb22006-11-02 21:16:08 +0100620 else if (command->dma_type == CMD_DMA_PAGE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 pci_unmap_page(host->pdev, command->cmd_dma,
622 command->dma_size, command->dma_dir);
Stefan Richteredf1fb22006-11-02 21:16:08 +0100623 /* XXX: Check for CMD_DMA_NONE bug */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 command->dma_type = CMD_DMA_NONE;
625 command->cmd_dma = 0;
626 }
627
628 if (command->sge_buffer) {
629 pci_unmap_sg(host->pdev, command->sge_buffer,
630 command->dma_size, command->dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 command->sge_buffer = NULL;
632 }
633}
634
635/*
636 * This function moves a command to the completed orb list.
Stefan Richterca0c7452006-11-02 21:16:08 +0100637 * Must be called with scsi_id->cmd_orb_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 */
Stefan Richter24c7cd02006-04-01 21:11:41 +0200639static void sbp2util_mark_command_completed(
640 struct scsi_id_instance_data *scsi_id,
641 struct sbp2_command_info *command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 list_del(&command->list);
644 sbp2util_free_command_dma(command);
Stefan Richterca0c7452006-11-02 21:16:08 +0100645 list_add_tail(&command->list, &scsi_id->cmd_orb_completed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Jody McIntyreabd559b2005-09-30 11:59:06 -0700648/*
649 * Is scsi_id valid? Is the 1394 node still present?
650 */
651static inline int sbp2util_node_is_available(struct scsi_id_instance_data *scsi_id)
652{
653 return scsi_id && scsi_id->ne && !scsi_id->ne->in_limbo;
654}
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656/*********************************************
657 * IEEE-1394 core driver stack related section
658 *********************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659
660static int sbp2_probe(struct device *dev)
661{
662 struct unit_directory *ud;
663 struct scsi_id_instance_data *scsi_id;
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 ud = container_of(dev, struct unit_directory, device);
666
667 /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
668 * instead. */
669 if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
670 return -ENODEV;
671
Stefan Richtera237f352005-11-07 06:31:39 -0500672 scsi_id = sbp2_alloc_device(ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
Stefan Richtera237f352005-11-07 06:31:39 -0500674 if (!scsi_id)
675 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Stefan Richtera237f352005-11-07 06:31:39 -0500677 sbp2_parse_unit_directory(scsi_id, ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Stefan Richtera237f352005-11-07 06:31:39 -0500679 return sbp2_start_device(scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
682static int sbp2_remove(struct device *dev)
683{
684 struct unit_directory *ud;
685 struct scsi_id_instance_data *scsi_id;
Jody McIntyreabd559b2005-09-30 11:59:06 -0700686 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 ud = container_of(dev, struct unit_directory, device);
689 scsi_id = ud->device.driver_data;
Jody McIntyreabd559b2005-09-30 11:59:06 -0700690 if (!scsi_id)
691 return 0;
692
Stefan Richterbf637ec2006-01-31 00:13:06 -0500693 if (scsi_id->scsi_host) {
694 /* Get rid of enqueued commands if there is no chance to
695 * send them. */
696 if (!sbp2util_node_is_available(scsi_id))
697 sbp2scsi_complete_all_commands(scsi_id, DID_NO_CONNECT);
Stefan Richtere8ca5662006-11-02 21:16:08 +0100698 /* scsi_remove_device() may trigger shutdown functions of SCSI
Stefan Richterbf637ec2006-01-31 00:13:06 -0500699 * highlevel drivers which would deadlock if blocked. */
Stefan Richter2cccbb52006-08-14 18:59:00 +0200700 atomic_set(&scsi_id->state, SBP2LU_STATE_IN_SHUTDOWN);
Jody McIntyreabd559b2005-09-30 11:59:06 -0700701 scsi_unblock_requests(scsi_id->scsi_host);
Stefan Richterbf637ec2006-01-31 00:13:06 -0500702 }
Jody McIntyreabd559b2005-09-30 11:59:06 -0700703 sdev = scsi_id->sdev;
704 if (sdev) {
705 scsi_id->sdev = NULL;
706 scsi_remove_device(sdev);
707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 sbp2_logout_device(scsi_id);
710 sbp2_remove_device(scsi_id);
711
712 return 0;
713}
714
715static int sbp2_update(struct unit_directory *ud)
716{
717 struct scsi_id_instance_data *scsi_id = ud->device.driver_data;
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 if (sbp2_reconnect_device(scsi_id)) {
Stefan Richtere8ca5662006-11-02 21:16:08 +0100720 /* Reconnect has failed. Perhaps we didn't reconnect fast
721 * enough. Try a regular login, but first log out just in
722 * case of any weirdness. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 sbp2_logout_device(scsi_id);
724
725 if (sbp2_login_device(scsi_id)) {
726 /* Login failed too, just fail, and the backend
727 * will call our sbp2_remove for us */
728 SBP2_ERR("Failed to reconnect to sbp2 device!");
729 return -EBUSY;
730 }
731 }
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 sbp2_set_busy_timeout(scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 sbp2_agent_reset(scsi_id, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 sbp2_max_speed_and_size(scsi_id);
736
Stefan Richtere8ca5662006-11-02 21:16:08 +0100737 /* Complete any pending commands with busy (so they get retried)
738 * and remove them from our queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
740
Stefan Richter4fc383c2006-08-14 18:46:00 +0200741 /* Accept new commands unless there was another bus reset in the
742 * meantime. */
743 if (hpsb_node_entry_valid(scsi_id->ne)) {
Stefan Richter2cccbb52006-08-14 18:59:00 +0200744 atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
Stefan Richter4fc383c2006-08-14 18:46:00 +0200745 scsi_unblock_requests(scsi_id->scsi_host);
746 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 return 0;
748}
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud)
751{
Stefan Richterca0c7452006-11-02 21:16:08 +0100752 struct sbp2_fwhost_info *hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 struct Scsi_Host *scsi_host = NULL;
754 struct scsi_id_instance_data *scsi_id = NULL;
755
Stefan Richter85511582005-11-07 06:31:45 -0500756 scsi_id = kzalloc(sizeof(*scsi_id), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (!scsi_id) {
758 SBP2_ERR("failed to create scsi_id");
759 goto failed_alloc;
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 scsi_id->ne = ud->ne;
763 scsi_id->ud = ud;
764 scsi_id->speed_code = IEEE1394_SPEED_100;
765 scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
Ben Collins67372312006-06-12 18:15:31 -0400766 scsi_id->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
Stefan Richterca0c7452006-11-02 21:16:08 +0100767 INIT_LIST_HEAD(&scsi_id->cmd_orb_inuse);
768 INIT_LIST_HEAD(&scsi_id->cmd_orb_completed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 INIT_LIST_HEAD(&scsi_id->scsi_list);
Stefan Richterca0c7452006-11-02 21:16:08 +0100770 spin_lock_init(&scsi_id->cmd_orb_lock);
Stefan Richter2cccbb52006-08-14 18:59:00 +0200771 atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
Stefan Richterd19c7762006-12-07 22:40:33 +0100772 INIT_WORK(&scsi_id->protocol_work, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 ud->device.driver_data = scsi_id;
775
776 hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
777 if (!hi) {
778 hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host, sizeof(*hi));
779 if (!hi) {
780 SBP2_ERR("failed to allocate hostinfo");
781 goto failed_alloc;
782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 hi->host = ud->ne->host;
784 INIT_LIST_HEAD(&hi->scsi_ids);
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
787 /* Handle data movement if physical dma is not
Stefan Richter55664052006-03-28 19:59:42 -0500788 * enabled or not supported on host controller */
789 if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
790 &sbp2_physdma_ops,
791 0x0ULL, 0xfffffffcULL)) {
792 SBP2_ERR("failed to register lower 4GB address range");
793 goto failed_alloc;
794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795#endif
796 }
797
Stefan Richter147830f2006-03-28 19:54:52 -0500798 /* Prevent unloading of the 1394 host */
799 if (!try_module_get(hi->host->driver->owner)) {
800 SBP2_ERR("failed to get a reference on 1394 host driver");
801 goto failed_alloc;
802 }
803
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 scsi_id->hi = hi;
805
806 list_add_tail(&scsi_id->scsi_list, &hi->scsi_ids);
807
Stefan Richter35bdddb2006-01-31 00:13:33 -0500808 /* Register the status FIFO address range. We could use the same FIFO
809 * for targets at different nodes. However we need different FIFOs per
Stefan Richtera54c9d32006-05-15 22:09:46 +0200810 * target in order to support multi-unit devices.
811 * The FIFO is located out of the local host controller's physical range
812 * but, if possible, within the posted write area. Status writes will
813 * then be performed as unified transactions. This slightly reduces
814 * bandwidth usage, and some Prolific based devices seem to require it.
815 */
Stefan Richter35bdddb2006-01-31 00:13:33 -0500816 scsi_id->status_fifo_addr = hpsb_allocate_and_register_addrspace(
817 &sbp2_highlevel, ud->ne->host, &sbp2_ops,
818 sizeof(struct sbp2_status_block), sizeof(quadlet_t),
Ben Collins40ae6c52006-06-12 18:13:49 -0400819 ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
Ben Collins67372312006-06-12 18:15:31 -0400820 if (scsi_id->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
Stefan Richter35bdddb2006-01-31 00:13:33 -0500821 SBP2_ERR("failed to allocate status FIFO address range");
822 goto failed_alloc;
823 }
824
Stefan Richterca0c7452006-11-02 21:16:08 +0100825 scsi_host = scsi_host_alloc(&sbp2_shost_template,
Stefan Richtera237f352005-11-07 06:31:39 -0500826 sizeof(unsigned long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (!scsi_host) {
828 SBP2_ERR("failed to register scsi host");
829 goto failed_alloc;
830 }
831
832 scsi_host->hostdata[0] = (unsigned long)scsi_id;
833
834 if (!scsi_add_host(scsi_host, &ud->device)) {
835 scsi_id->scsi_host = scsi_host;
836 return scsi_id;
837 }
838
839 SBP2_ERR("failed to add scsi host");
840 scsi_host_put(scsi_host);
841
842failed_alloc:
843 sbp2_remove_device(scsi_id);
844 return NULL;
845}
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847static void sbp2_host_reset(struct hpsb_host *host)
848{
Stefan Richterca0c7452006-11-02 21:16:08 +0100849 struct sbp2_fwhost_info *hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 struct scsi_id_instance_data *scsi_id;
851
852 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
Stefan Richter2cccbb52006-08-14 18:59:00 +0200853 if (!hi)
854 return;
855 list_for_each_entry(scsi_id, &hi->scsi_ids, scsi_list)
856 if (likely(atomic_read(&scsi_id->state) !=
857 SBP2LU_STATE_IN_SHUTDOWN)) {
858 atomic_set(&scsi_id->state, SBP2LU_STATE_IN_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 scsi_block_requests(scsi_id->scsi_host);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
864{
Stefan Richterca0c7452006-11-02 21:16:08 +0100865 struct sbp2_fwhost_info *hi = scsi_id->hi;
James Bottomley146f7262005-09-10 12:44:09 -0500866 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 scsi_id->login_response =
Stefan Richtera237f352005-11-07 06:31:39 -0500869 pci_alloc_consistent(hi->host->pdev,
870 sizeof(struct sbp2_login_response),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 &scsi_id->login_response_dma);
872 if (!scsi_id->login_response)
873 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 scsi_id->query_logins_orb =
Stefan Richtera237f352005-11-07 06:31:39 -0500876 pci_alloc_consistent(hi->host->pdev,
877 sizeof(struct sbp2_query_logins_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 &scsi_id->query_logins_orb_dma);
879 if (!scsi_id->query_logins_orb)
880 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 scsi_id->query_logins_response =
Stefan Richtera237f352005-11-07 06:31:39 -0500883 pci_alloc_consistent(hi->host->pdev,
884 sizeof(struct sbp2_query_logins_response),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 &scsi_id->query_logins_response_dma);
886 if (!scsi_id->query_logins_response)
887 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 scsi_id->reconnect_orb =
Stefan Richtera237f352005-11-07 06:31:39 -0500890 pci_alloc_consistent(hi->host->pdev,
891 sizeof(struct sbp2_reconnect_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 &scsi_id->reconnect_orb_dma);
893 if (!scsi_id->reconnect_orb)
894 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 scsi_id->logout_orb =
Stefan Richtera237f352005-11-07 06:31:39 -0500897 pci_alloc_consistent(hi->host->pdev,
898 sizeof(struct sbp2_logout_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 &scsi_id->logout_orb_dma);
900 if (!scsi_id->logout_orb)
901 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 scsi_id->login_orb =
Stefan Richtera237f352005-11-07 06:31:39 -0500904 pci_alloc_consistent(hi->host->pdev,
905 sizeof(struct sbp2_login_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 &scsi_id->login_orb_dma);
Stefan Richtereaceec72005-12-13 11:05:05 -0500907 if (!scsi_id->login_orb)
908 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (sbp2util_create_command_orb_pool(scsi_id)) {
911 SBP2_ERR("sbp2util_create_command_orb_pool failed!");
912 sbp2_remove_device(scsi_id);
913 return -ENOMEM;
914 }
915
Stefan Richtere8ca5662006-11-02 21:16:08 +0100916 /* Wait a second before trying to log in. Previously logged in
917 * initiators need a chance to reconnect. */
Stefan Richter902abed2006-08-14 18:56:00 +0200918 if (msleep_interruptible(1000)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 sbp2_remove_device(scsi_id);
920 return -EINTR;
921 }
Stefan Richtera237f352005-11-07 06:31:39 -0500922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (sbp2_login_device(scsi_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 sbp2_remove_device(scsi_id);
925 return -EBUSY;
926 }
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 sbp2_set_busy_timeout(scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 sbp2_agent_reset(scsi_id, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 sbp2_max_speed_and_size(scsi_id);
931
James Bottomley146f7262005-09-10 12:44:09 -0500932 error = scsi_add_device(scsi_id->scsi_host, 0, scsi_id->ud->id, 0);
933 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 SBP2_ERR("scsi_add_device failed");
Stefan Richterdc3edd52005-12-12 23:03:30 -0500935 sbp2_logout_device(scsi_id);
936 sbp2_remove_device(scsi_id);
James Bottomley146f7262005-09-10 12:44:09 -0500937 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
939
940 return 0;
Stefan Richtereaceec72005-12-13 11:05:05 -0500941
942alloc_fail:
943 SBP2_ERR("Could not allocate memory for scsi_id");
944 sbp2_remove_device(scsi_id);
945 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946}
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
949{
Stefan Richterca0c7452006-11-02 21:16:08 +0100950 struct sbp2_fwhost_info *hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 if (!scsi_id)
953 return;
954
955 hi = scsi_id->hi;
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (scsi_id->scsi_host) {
958 scsi_remove_host(scsi_id->scsi_host);
959 scsi_host_put(scsi_id->scsi_host);
960 }
Stefan Richter09ee67a2006-08-14 18:43:00 +0200961 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 sbp2util_remove_command_orb_pool(scsi_id);
963
964 list_del(&scsi_id->scsi_list);
965
Stefan Richteredf1fb22006-11-02 21:16:08 +0100966 if (scsi_id->login_response)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 pci_free_consistent(hi->host->pdev,
968 sizeof(struct sbp2_login_response),
969 scsi_id->login_response,
970 scsi_id->login_response_dma);
Stefan Richteredf1fb22006-11-02 21:16:08 +0100971 if (scsi_id->login_orb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 pci_free_consistent(hi->host->pdev,
973 sizeof(struct sbp2_login_orb),
974 scsi_id->login_orb,
975 scsi_id->login_orb_dma);
Stefan Richteredf1fb22006-11-02 21:16:08 +0100976 if (scsi_id->reconnect_orb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 pci_free_consistent(hi->host->pdev,
978 sizeof(struct sbp2_reconnect_orb),
979 scsi_id->reconnect_orb,
980 scsi_id->reconnect_orb_dma);
Stefan Richteredf1fb22006-11-02 21:16:08 +0100981 if (scsi_id->logout_orb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 pci_free_consistent(hi->host->pdev,
983 sizeof(struct sbp2_logout_orb),
984 scsi_id->logout_orb,
985 scsi_id->logout_orb_dma);
Stefan Richteredf1fb22006-11-02 21:16:08 +0100986 if (scsi_id->query_logins_orb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 pci_free_consistent(hi->host->pdev,
988 sizeof(struct sbp2_query_logins_orb),
989 scsi_id->query_logins_orb,
990 scsi_id->query_logins_orb_dma);
Stefan Richteredf1fb22006-11-02 21:16:08 +0100991 if (scsi_id->query_logins_response)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 pci_free_consistent(hi->host->pdev,
993 sizeof(struct sbp2_query_logins_response),
994 scsi_id->query_logins_response,
995 scsi_id->query_logins_response_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Ben Collins67372312006-06-12 18:15:31 -0400997 if (scsi_id->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
Stefan Richter35bdddb2006-01-31 00:13:33 -0500998 hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
Ben Collins67372312006-06-12 18:15:31 -0400999 scsi_id->status_fifo_addr);
Stefan Richter35bdddb2006-01-31 00:13:33 -05001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 scsi_id->ud->device.driver_data = NULL;
1002
Stefan Richter147830f2006-03-28 19:54:52 -05001003 if (hi)
1004 module_put(hi->host->driver->owner);
1005
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 kfree(scsi_id);
1007}
1008
1009#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
1010/*
Stefan Richtere8ca5662006-11-02 21:16:08 +01001011 * Deal with write requests on adapters which do not support physical DMA or
1012 * have it switched off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 */
Stefan Richtera237f352005-11-07 06:31:39 -05001014static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
1015 int destid, quadlet_t *data, u64 addr,
1016 size_t length, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Stefan Richtera237f352005-11-07 06:31:39 -05001018 memcpy(bus_to_virt((u32) addr), data, length);
Stefan Richtera237f352005-11-07 06:31:39 -05001019 return RCODE_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
1022/*
Stefan Richtere8ca5662006-11-02 21:16:08 +01001023 * Deal with read requests on adapters which do not support physical DMA or
1024 * have it switched off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 */
Stefan Richtera237f352005-11-07 06:31:39 -05001026static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
1027 quadlet_t *data, u64 addr, size_t length,
1028 u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029{
Stefan Richtera237f352005-11-07 06:31:39 -05001030 memcpy(data, bus_to_virt((u32) addr), length);
Stefan Richtera237f352005-11-07 06:31:39 -05001031 return RCODE_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032}
1033#endif
1034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035/**************************************
1036 * SBP-2 protocol related section
1037 **************************************/
1038
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
1040{
Stefan Richterca0c7452006-11-02 21:16:08 +01001041 struct sbp2_fwhost_info *hi = scsi_id->hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 quadlet_t data[2];
1043 int max_logins;
1044 int active_logins;
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 scsi_id->query_logins_orb->reserved1 = 0x0;
1047 scsi_id->query_logins_orb->reserved2 = 0x0;
1048
1049 scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma;
1050 scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
1052 scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
1053 scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
Stefan Richterca0c7452006-11-02 21:16:08 +01001054 scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 scsi_id->query_logins_orb->reserved_resp_length =
1057 ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Stefan Richter35bdddb2006-01-31 00:13:33 -05001059 scsi_id->query_logins_orb->status_fifo_hi =
1060 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1061 scsi_id->query_logins_orb->status_fifo_lo =
1062 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063
1064 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb));
1065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1069 data[1] = scsi_id->query_logins_orb_dma;
1070 sbp2util_cpu_to_be32_buffer(data, 8);
1071
Stefan Richterca0c7452006-11-02 21:16:08 +01001072 hpsb_node_write(scsi_id->ne, scsi_id->management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Stefan Richtere8398bb2006-07-23 22:19:00 +02001074 if (sbp2util_access_timeout(scsi_id, 2*HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001076 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078
1079 if (scsi_id->status_block.ORB_offset_lo != scsi_id->query_logins_orb_dma) {
1080 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001081 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 }
1083
Stefan Richter60657722006-07-23 22:18:00 +02001084 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1085 SBP2_INFO("Error querying logins to SBP-2 device - failed");
Stefan Richtera237f352005-11-07 06:31:39 -05001086 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
1089 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_response, sizeof(struct sbp2_query_logins_response));
1090
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins);
Ben Collins20f45782006-06-12 18:13:11 -04001092 SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094 active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins);
Ben Collins20f45782006-06-12 18:13:11 -04001095 SBP2_INFO("Number of active logins: %d", active_logins);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 if (active_logins >= max_logins) {
Stefan Richtera237f352005-11-07 06:31:39 -05001098 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100
1101 return 0;
1102}
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
1105{
Stefan Richterca0c7452006-11-02 21:16:08 +01001106 struct sbp2_fwhost_info *hi = scsi_id->hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 quadlet_t data[2];
1108
Stefan Richteredf1fb22006-11-02 21:16:08 +01001109 if (!scsi_id->login_orb)
Stefan Richtera237f352005-11-07 06:31:39 -05001110 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Stefan Richterca0c7452006-11-02 21:16:08 +01001112 if (!sbp2_exclusive_login && sbp2_query_logins(scsi_id)) {
1113 SBP2_INFO("Device does not support any more concurrent logins");
1114 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116
Stefan Richtere8ca5662006-11-02 21:16:08 +01001117 /* assume no password */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 scsi_id->login_orb->password_hi = 0;
1119 scsi_id->login_orb->password_lo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
1121 scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
1122 scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
Stefan Richtere8ca5662006-11-02 21:16:08 +01001124
1125 /* one second reconnect time */
1126 scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0);
Stefan Richterca0c7452006-11-02 21:16:08 +01001127 scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(sbp2_exclusive_login);
Stefan Richtere8ca5662006-11-02 21:16:08 +01001128 scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1);
Stefan Richterca0c7452006-11-02 21:16:08 +01001129 scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
1131 scsi_id->login_orb->passwd_resp_lengths =
1132 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Stefan Richter35bdddb2006-01-31 00:13:33 -05001134 scsi_id->login_orb->status_fifo_hi =
1135 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1136 scsi_id->login_orb->status_fifo_lo =
1137 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1144 data[1] = scsi_id->login_orb_dma;
1145 sbp2util_cpu_to_be32_buffer(data, 8);
1146
Stefan Richterca0c7452006-11-02 21:16:08 +01001147 hpsb_node_write(scsi_id->ne, scsi_id->management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Stefan Richtere8ca5662006-11-02 21:16:08 +01001149 /* wait up to 20 seconds for login status */
Stefan Richtere8398bb2006-07-23 22:19:00 +02001150 if (sbp2util_access_timeout(scsi_id, 20*HZ)) {
1151 SBP2_ERR("Error logging into SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001152 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154
Stefan Richtere8ca5662006-11-02 21:16:08 +01001155 /* make sure that the returned status matches the login ORB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
Stefan Richter60657722006-07-23 22:18:00 +02001157 SBP2_ERR("Error logging into SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001158 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 }
1160
Stefan Richter60657722006-07-23 22:18:00 +02001161 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1162 SBP2_ERR("Error logging into SBP-2 device - failed");
Stefan Richtera237f352005-11-07 06:31:39 -05001163 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response));
Stefan Richterca0c7452006-11-02 21:16:08 +01001167 scsi_id->command_block_agent_addr =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 ((u64)scsi_id->login_response->command_block_agent_hi) << 32;
Stefan Richterca0c7452006-11-02 21:16:08 +01001169 scsi_id->command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
1170 scsi_id->command_block_agent_addr &= 0x0000ffffffffffffULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 SBP2_INFO("Logged into SBP-2 device");
Stefan Richtera237f352005-11-07 06:31:39 -05001173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174}
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
1177{
Stefan Richterca0c7452006-11-02 21:16:08 +01001178 struct sbp2_fwhost_info *hi = scsi_id->hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 quadlet_t data[2];
1180 int error;
1181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 scsi_id->logout_orb->reserved1 = 0x0;
1183 scsi_id->logout_orb->reserved2 = 0x0;
1184 scsi_id->logout_orb->reserved3 = 0x0;
1185 scsi_id->logout_orb->reserved4 = 0x0;
1186
1187 scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
1188 scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1190
1191 scsi_id->logout_orb->reserved5 = 0x0;
Stefan Richter35bdddb2006-01-31 00:13:33 -05001192 scsi_id->logout_orb->status_fifo_hi =
1193 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1194 scsi_id->logout_orb->status_fifo_lo =
1195 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb));
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1200 data[1] = scsi_id->logout_orb_dma;
1201 sbp2util_cpu_to_be32_buffer(data, 8);
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 error = hpsb_node_write(scsi_id->ne,
Stefan Richterca0c7452006-11-02 21:16:08 +01001204 scsi_id->management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 if (error)
1206 return error;
1207
Stefan Richtere8ca5662006-11-02 21:16:08 +01001208 /* wait up to 1 second for the device to complete logout */
Stefan Richtere8398bb2006-07-23 22:19:00 +02001209 if (sbp2util_access_timeout(scsi_id, HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 return -EIO;
1211
1212 SBP2_INFO("Logged out of SBP-2 device");
Stefan Richtera237f352005-11-07 06:31:39 -05001213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214}
1215
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
1217{
Stefan Richterca0c7452006-11-02 21:16:08 +01001218 struct sbp2_fwhost_info *hi = scsi_id->hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 quadlet_t data[2];
1220 int error;
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 scsi_id->reconnect_orb->reserved1 = 0x0;
1223 scsi_id->reconnect_orb->reserved2 = 0x0;
1224 scsi_id->reconnect_orb->reserved3 = 0x0;
1225 scsi_id->reconnect_orb->reserved4 = 0x0;
1226
1227 scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
1228 scsi_id->reconnect_orb->login_ID_misc |=
1229 ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1231
1232 scsi_id->reconnect_orb->reserved5 = 0x0;
Stefan Richter35bdddb2006-01-31 00:13:33 -05001233 scsi_id->reconnect_orb->status_fifo_hi =
1234 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1235 scsi_id->reconnect_orb->status_fifo_lo =
1236 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
1239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1241 data[1] = scsi_id->reconnect_orb_dma;
1242 sbp2util_cpu_to_be32_buffer(data, 8);
1243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 error = hpsb_node_write(scsi_id->ne,
Stefan Richterca0c7452006-11-02 21:16:08 +01001245 scsi_id->management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 if (error)
1247 return error;
1248
Stefan Richtere8ca5662006-11-02 21:16:08 +01001249 /* wait up to 1 second for reconnect status */
Stefan Richtere8398bb2006-07-23 22:19:00 +02001250 if (sbp2util_access_timeout(scsi_id, HZ)) {
1251 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001252 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254
Stefan Richtere8ca5662006-11-02 21:16:08 +01001255 /* make sure that the returned status matches the reconnect ORB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
Stefan Richter60657722006-07-23 22:18:00 +02001257 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001258 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260
Stefan Richter60657722006-07-23 22:18:00 +02001261 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1262 SBP2_ERR("Error reconnecting to SBP-2 device - failed");
Stefan Richtera237f352005-11-07 06:31:39 -05001263 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 }
1265
Stefan Richter35644092006-11-02 21:16:08 +01001266 SBP2_INFO("Reconnected to SBP-2 device");
Stefan Richtera237f352005-11-07 06:31:39 -05001267 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268}
1269
1270/*
Stefan Richtere8ca5662006-11-02 21:16:08 +01001271 * Set the target node's Single Phase Retry limit. Affects the target's retry
1272 * behaviour if our node is too busy to accept requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 */
1274static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id)
1275{
1276 quadlet_t data;
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
Stefan Richterd024ebc2006-03-28 20:03:55 -05001279 if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
1280 SBP2_ERR("%s error", __FUNCTION__);
Stefan Richtera237f352005-11-07 06:31:39 -05001281 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282}
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
1285 struct unit_directory *ud)
1286{
1287 struct csr1212_keyval *kv;
1288 struct csr1212_dentry *dentry;
1289 u64 management_agent_addr;
1290 u32 command_set_spec_id, command_set, unit_characteristics,
Stefan Richter24d3bf82006-05-15 22:04:59 +02001291 firmware_revision;
1292 unsigned workarounds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 int i;
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 management_agent_addr = 0x0;
1296 command_set_spec_id = 0x0;
1297 command_set = 0x0;
1298 unit_characteristics = 0x0;
1299 firmware_revision = 0x0;
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
1302 switch (kv->key.id) {
1303 case CSR1212_KV_ID_DEPENDENT_INFO:
Stefan Richteredf1fb22006-11-02 21:16:08 +01001304 if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 management_agent_addr =
Stefan Richtera237f352005-11-07 06:31:39 -05001306 CSR1212_REGISTER_SPACE_BASE +
1307 (kv->value.csr_offset << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308
Stefan Richteredf1fb22006-11-02 21:16:08 +01001309 else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE)
Stefan Richterca0c7452006-11-02 21:16:08 +01001310 scsi_id->lun =
Stefan Richtera237f352005-11-07 06:31:39 -05001311 ORB_SET_LUN(kv->value.immediate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 break;
1313
1314 case SBP2_COMMAND_SET_SPEC_ID_KEY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 command_set_spec_id = kv->value.immediate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 break;
1317
1318 case SBP2_COMMAND_SET_KEY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 command_set = kv->value.immediate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 break;
1321
1322 case SBP2_UNIT_CHARACTERISTICS_KEY:
Stefan Richtere8ca5662006-11-02 21:16:08 +01001323 /* FIXME: This is ignored so far.
1324 * See SBP-2 clause 7.4.8. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 unit_characteristics = kv->value.immediate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 break;
1327
1328 case SBP2_FIRMWARE_REVISION_KEY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 firmware_revision = kv->value.immediate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 break;
1331
1332 default:
Stefan Richtere8ca5662006-11-02 21:16:08 +01001333 /* FIXME: Check for SBP2_DEVICE_TYPE_AND_LUN_KEY.
1334 * Its "ordered" bit has consequences for command ORB
1335 * list handling. See SBP-2 clauses 4.6, 7.4.11, 10.2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 break;
1337 }
1338 }
1339
Stefan Richter24d3bf82006-05-15 22:04:59 +02001340 workarounds = sbp2_default_workarounds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Stefan Richter679c0cd2006-05-15 22:08:09 +02001342 if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
1343 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
1344 if (sbp2_workarounds_table[i].firmware_revision &&
1345 sbp2_workarounds_table[i].firmware_revision !=
1346 (firmware_revision & 0xffff00))
1347 continue;
1348 if (sbp2_workarounds_table[i].model_id &&
1349 sbp2_workarounds_table[i].model_id != ud->model_id)
1350 continue;
1351 workarounds |= sbp2_workarounds_table[i].workarounds;
1352 break;
1353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Stefan Richter24d3bf82006-05-15 22:04:59 +02001355 if (workarounds)
Stefan Richtere9a1c522006-05-15 22:06:37 +02001356 SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
1357 "(firmware_revision 0x%06x, vendor_id 0x%06x,"
1358 " model_id 0x%06x)",
Stefan Richter24d3bf82006-05-15 22:04:59 +02001359 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
Stefan Richtere9a1c522006-05-15 22:06:37 +02001360 workarounds, firmware_revision,
1361 ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
1362 ud->model_id);
Stefan Richter24d3bf82006-05-15 22:04:59 +02001363
1364 /* We would need one SCSI host template for each target to adjust
1365 * max_sectors on the fly, therefore warn only. */
1366 if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
Stefan Richterca0c7452006-11-02 21:16:08 +01001367 (sbp2_max_sectors * 512) > (128 * 1024))
Stefan Richter35644092006-11-02 21:16:08 +01001368 SBP2_INFO("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
Stefan Richter24d3bf82006-05-15 22:04:59 +02001369 "max transfer size. WARNING: Current max_sectors "
1370 "setting is larger than 128KB (%d sectors)",
1371 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
Stefan Richterca0c7452006-11-02 21:16:08 +01001372 sbp2_max_sectors);
Stefan Richter24d3bf82006-05-15 22:04:59 +02001373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 /* If this is a logical unit directory entry, process the parent
1375 * to get the values. */
1376 if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
1377 struct unit_directory *parent_ud =
1378 container_of(ud->device.parent, struct unit_directory, device);
1379 sbp2_parse_unit_directory(scsi_id, parent_ud);
1380 } else {
Stefan Richterca0c7452006-11-02 21:16:08 +01001381 scsi_id->management_agent_addr = management_agent_addr;
1382 scsi_id->command_set_spec_id = command_set_spec_id;
1383 scsi_id->command_set = command_set;
1384 scsi_id->unit_characteristics = unit_characteristics;
1385 scsi_id->firmware_revision = firmware_revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 scsi_id->workarounds = workarounds;
1387 if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
Stefan Richterca0c7452006-11-02 21:16:08 +01001388 scsi_id->lun = ORB_SET_LUN(ud->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390}
1391
Ben Collinsfd23ade2006-06-12 18:14:14 -04001392#define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
1393
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394/*
1395 * This function is called in order to determine the max speed and packet
1396 * size we can use in our ORBs. Note, that we (the driver and host) only
1397 * initiate the transaction. The SBP-2 device actually transfers the data
1398 * (by reading from the DMA area we tell it). This means that the SBP-2
1399 * device decides the actual maximum data it can transfer. We just tell it
1400 * the speed that it needs to use, and the max_rec the host supports, and
1401 * it takes care of the rest.
1402 */
1403static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id)
1404{
Stefan Richterca0c7452006-11-02 21:16:08 +01001405 struct sbp2_fwhost_info *hi = scsi_id->hi;
Ben Collinsfd23ade2006-06-12 18:14:14 -04001406 u8 payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Stefan Richtera237f352005-11-07 06:31:39 -05001408 scsi_id->speed_code =
Ben Collins647dcb52006-06-12 18:12:37 -04001409 hi->host->speed[NODEID_TO_NODE(scsi_id->ne->nodeid)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Stefan Richterca0c7452006-11-02 21:16:08 +01001411 if (scsi_id->speed_code > sbp2_max_speed) {
1412 scsi_id->speed_code = sbp2_max_speed;
1413 SBP2_INFO("Reducing speed to %s",
1414 hpsb_speedto_str[sbp2_max_speed]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 }
1416
1417 /* Payload size is the lesser of what our speed supports and what
1418 * our host supports. */
Ben Collinsfd23ade2006-06-12 18:14:14 -04001419 payload = min(sbp2_speedto_max_payload[scsi_id->speed_code],
1420 (u8) (hi->host->csr.max_rec - 1));
1421
1422 /* If physical DMA is off, work around limitation in ohci1394:
1423 * packet size must not exceed PAGE_SIZE */
1424 if (scsi_id->ne->host->low_addr_space < (1ULL << 32))
1425 while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
1426 payload)
1427 payload--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428
Stefan Richter35644092006-11-02 21:16:08 +01001429 SBP2_INFO("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
1430 NODE_BUS_ARGS(hi->host, scsi_id->ne->nodeid),
1431 hpsb_speedto_str[scsi_id->speed_code],
1432 SBP2_PAYLOAD_TO_BYTES(payload));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Ben Collinsfd23ade2006-06-12 18:14:14 -04001434 scsi_id->max_payload_size = payload;
Stefan Richtera237f352005-11-07 06:31:39 -05001435 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436}
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait)
1439{
1440 quadlet_t data;
1441 u64 addr;
1442 int retval;
Stefan Richtercc078182006-07-23 22:12:00 +02001443 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Stefan Richterd19c7762006-12-07 22:40:33 +01001445 /* cancel_delayed_work(&scsi_id->protocol_work); */
Stefan Richter09ee67a2006-08-14 18:43:00 +02001446 if (wait)
1447 flush_scheduled_work();
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 data = ntohl(SBP2_AGENT_RESET_DATA);
Stefan Richterca0c7452006-11-02 21:16:08 +01001450 addr = scsi_id->command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
1452 if (wait)
1453 retval = hpsb_node_write(scsi_id->ne, addr, &data, 4);
1454 else
1455 retval = sbp2util_node_write_no_wait(scsi_id->ne, addr, &data, 4);
1456
1457 if (retval < 0) {
1458 SBP2_ERR("hpsb_node_write failed.\n");
1459 return -EIO;
1460 }
1461
Stefan Richtere8ca5662006-11-02 21:16:08 +01001462 /* make sure that the ORB_POINTER is written on next command */
Stefan Richterca0c7452006-11-02 21:16:08 +01001463 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 scsi_id->last_orb = NULL;
Stefan Richterca0c7452006-11-02 21:16:08 +01001465 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466
Stefan Richtera237f352005-11-07 06:31:39 -05001467 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468}
1469
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001470static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
Stefan Richterca0c7452006-11-02 21:16:08 +01001471 struct sbp2_fwhost_info *hi,
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001472 struct sbp2_command_info *command,
1473 unsigned int scsi_use_sg,
1474 struct scatterlist *sgpnt,
1475 u32 orb_direction,
1476 enum dma_data_direction dma_dir)
1477{
1478 command->dma_dir = dma_dir;
1479 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1480 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1481
Stefan Richtere8ca5662006-11-02 21:16:08 +01001482 /* special case if only one element (and less than 64KB in size) */
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001483 if ((scsi_use_sg == 1) &&
1484 (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
1485
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001486 command->dma_size = sgpnt[0].length;
1487 command->dma_type = CMD_DMA_PAGE;
1488 command->cmd_dma = pci_map_page(hi->host->pdev,
1489 sgpnt[0].page,
1490 sgpnt[0].offset,
1491 command->dma_size,
1492 command->dma_dir);
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001493
1494 orb->data_descriptor_lo = command->cmd_dma;
1495 orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
1496
1497 } else {
1498 struct sbp2_unrestricted_page_table *sg_element =
1499 &command->scatter_gather_element[0];
1500 u32 sg_count, sg_len;
1501 dma_addr_t sg_addr;
1502 int i, count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg,
1503 dma_dir);
1504
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001505 command->dma_size = scsi_use_sg;
1506 command->sge_buffer = sgpnt;
1507
1508 /* use page tables (s/g) */
1509 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1510 orb->data_descriptor_lo = command->sge_dma;
1511
Stefan Richtere8ca5662006-11-02 21:16:08 +01001512 /* loop through and fill out our SBP-2 page tables
1513 * (and split up anything too large) */
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001514 for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
1515 sg_len = sg_dma_len(sgpnt);
1516 sg_addr = sg_dma_address(sgpnt);
1517 while (sg_len) {
1518 sg_element[sg_count].segment_base_lo = sg_addr;
1519 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1520 sg_element[sg_count].length_segment_base_hi =
1521 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1522 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1523 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1524 } else {
1525 sg_element[sg_count].length_segment_base_hi =
1526 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1527 sg_len = 0;
1528 }
1529 sg_count++;
1530 }
1531 }
1532
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001533 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1534
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001535 sbp2util_cpu_to_be32_buffer(sg_element,
1536 (sizeof(struct sbp2_unrestricted_page_table)) *
1537 sg_count);
1538 }
1539}
1540
1541static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
Stefan Richterca0c7452006-11-02 21:16:08 +01001542 struct sbp2_fwhost_info *hi,
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001543 struct sbp2_command_info *command,
1544 struct scatterlist *sgpnt,
1545 u32 orb_direction,
1546 unsigned int scsi_request_bufflen,
1547 void *scsi_request_buffer,
1548 enum dma_data_direction dma_dir)
1549{
1550 command->dma_dir = dma_dir;
1551 command->dma_size = scsi_request_bufflen;
1552 command->dma_type = CMD_DMA_SINGLE;
1553 command->cmd_dma = pci_map_single(hi->host->pdev, scsi_request_buffer,
1554 command->dma_size, command->dma_dir);
1555 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1556 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1557
Stefan Richtere8ca5662006-11-02 21:16:08 +01001558 /* handle case where we get a command w/o s/g enabled
1559 * (but check for transfers larger than 64K) */
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001560 if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1561
1562 orb->data_descriptor_lo = command->cmd_dma;
1563 orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
1564
1565 } else {
Stefan Richtere8ca5662006-11-02 21:16:08 +01001566 /* The buffer is too large. Turn this into page tables. */
1567
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001568 struct sbp2_unrestricted_page_table *sg_element =
1569 &command->scatter_gather_element[0];
1570 u32 sg_count, sg_len;
1571 dma_addr_t sg_addr;
1572
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001573 orb->data_descriptor_lo = command->sge_dma;
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001574 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1575
Stefan Richtere8ca5662006-11-02 21:16:08 +01001576 /* fill out our SBP-2 page tables; split up the large buffer */
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001577 sg_count = 0;
1578 sg_len = scsi_request_bufflen;
1579 sg_addr = command->cmd_dma;
1580 while (sg_len) {
1581 sg_element[sg_count].segment_base_lo = sg_addr;
1582 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1583 sg_element[sg_count].length_segment_base_hi =
1584 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1585 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1586 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1587 } else {
1588 sg_element[sg_count].length_segment_base_hi =
1589 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1590 sg_len = 0;
1591 }
1592 sg_count++;
1593 }
1594
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001595 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1596
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001597 sbp2util_cpu_to_be32_buffer(sg_element,
1598 (sizeof(struct sbp2_unrestricted_page_table)) *
1599 sg_count);
1600 }
1601}
1602
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001603static void sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
1604 struct sbp2_command_info *command,
1605 unchar *scsi_cmd,
1606 unsigned int scsi_use_sg,
1607 unsigned int scsi_request_bufflen,
1608 void *scsi_request_buffer,
1609 enum dma_data_direction dma_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610{
Stefan Richterca0c7452006-11-02 21:16:08 +01001611 struct sbp2_fwhost_info *hi = scsi_id->hi;
Stefan Richtera237f352005-11-07 06:31:39 -05001612 struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 struct sbp2_command_orb *command_orb = &command->command_orb;
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001614 u32 orb_direction;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 /*
Stefan Richtere8ca5662006-11-02 21:16:08 +01001617 * Set-up our command ORB.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 *
1619 * NOTE: We're doing unrestricted page tables (s/g), as this is
1620 * best performance (at least with the devices I have). This means
1621 * that data_size becomes the number of s/g elements, and
1622 * page_size should be zero (for unrestricted).
1623 */
1624 command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1625 command_orb->next_ORB_lo = 0x0;
1626 command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size);
1627 command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code);
Stefan Richtere8ca5662006-11-02 21:16:08 +01001628 command_orb->misc |= ORB_SET_NOTIFY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Stefan Richter43863eb2005-12-12 23:03:24 -05001630 if (dma_dir == DMA_NONE)
Stefan Richtera237f352005-11-07 06:31:39 -05001631 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
Stefan Richter43863eb2005-12-12 23:03:24 -05001632 else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
Stefan Richtera237f352005-11-07 06:31:39 -05001633 orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
Stefan Richter43863eb2005-12-12 23:03:24 -05001634 else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
Stefan Richtera237f352005-11-07 06:31:39 -05001635 orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
Stefan Richter43863eb2005-12-12 23:03:24 -05001636 else {
Stefan Richter35644092006-11-02 21:16:08 +01001637 SBP2_INFO("Falling back to DMA_NONE");
Stefan Richter43863eb2005-12-12 23:03:24 -05001638 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 }
1640
Stefan Richtere8ca5662006-11-02 21:16:08 +01001641 /* set up our page table stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 command_orb->data_descriptor_hi = 0x0;
1644 command_orb->data_descriptor_lo = 0x0;
1645 command_orb->misc |= ORB_SET_DIRECTION(1);
Stefan Richteredf1fb22006-11-02 21:16:08 +01001646 } else if (scsi_use_sg)
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001647 sbp2_prep_command_orb_sg(command_orb, hi, command, scsi_use_sg,
1648 sgpnt, orb_direction, dma_dir);
Stefan Richteredf1fb22006-11-02 21:16:08 +01001649 else
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001650 sbp2_prep_command_orb_no_sg(command_orb, hi, command, sgpnt,
1651 orb_direction, scsi_request_bufflen,
1652 scsi_request_buffer, dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 memset(command_orb->cdb, 0, 12);
1657 memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658}
1659
Stefan Richter28212762006-07-23 22:10:00 +02001660static void sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 struct sbp2_command_info *command)
1662{
Stefan Richterca0c7452006-11-02 21:16:08 +01001663 struct sbp2_fwhost_info *hi = scsi_id->hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 struct sbp2_command_orb *command_orb = &command->command_orb;
Stefan Richtercc078182006-07-23 22:12:00 +02001665 struct sbp2_command_orb *last_orb;
1666 dma_addr_t last_orb_dma;
Stefan Richterca0c7452006-11-02 21:16:08 +01001667 u64 addr = scsi_id->command_block_agent_addr;
Stefan Richtercc078182006-07-23 22:12:00 +02001668 quadlet_t data[2];
1669 size_t length;
1670 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 pci_dma_sync_single_for_device(hi->host->pdev, command->command_orb_dma,
1673 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02001674 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 pci_dma_sync_single_for_device(hi->host->pdev, command->sge_dma,
1676 sizeof(command->scatter_gather_element),
1677 PCI_DMA_BIDIRECTIONAL);
Stefan Richtere8ca5662006-11-02 21:16:08 +01001678
1679 /* check to see if there are any previous orbs to use */
Stefan Richterca0c7452006-11-02 21:16:08 +01001680 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
Stefan Richtercc078182006-07-23 22:12:00 +02001681 last_orb = scsi_id->last_orb;
1682 last_orb_dma = scsi_id->last_orb_dma;
1683 if (!last_orb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 /*
Stefan Richtercc078182006-07-23 22:12:00 +02001685 * last_orb == NULL means: We know that the target's fetch agent
1686 * is not active right now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 */
Stefan Richtercc078182006-07-23 22:12:00 +02001688 addr += SBP2_ORB_POINTER_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1690 data[1] = command->command_orb_dma;
1691 sbp2util_cpu_to_be32_buffer(data, 8);
Stefan Richtercc078182006-07-23 22:12:00 +02001692 length = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 /*
Stefan Richtercc078182006-07-23 22:12:00 +02001695 * last_orb != NULL means: We know that the target's fetch agent
1696 * is (very probably) not dead or in reset state right now.
1697 * We have an ORB already sent that we can append a new one to.
1698 * The target's fetch agent may or may not have read this
1699 * previous ORB yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 */
Stefan Richtercc078182006-07-23 22:12:00 +02001701 pci_dma_sync_single_for_cpu(hi->host->pdev, last_orb_dma,
1702 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02001703 PCI_DMA_TODEVICE);
Stefan Richtercc078182006-07-23 22:12:00 +02001704 last_orb->next_ORB_lo = cpu_to_be32(command->command_orb_dma);
1705 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 /* Tells hardware that this pointer is valid */
Stefan Richtercc078182006-07-23 22:12:00 +02001707 last_orb->next_ORB_hi = 0;
1708 pci_dma_sync_single_for_device(hi->host->pdev, last_orb_dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02001710 PCI_DMA_TODEVICE);
Stefan Richtercc078182006-07-23 22:12:00 +02001711 addr += SBP2_DOORBELL_OFFSET;
1712 data[0] = 0;
1713 length = 4;
1714 }
1715 scsi_id->last_orb = command_orb;
1716 scsi_id->last_orb_dma = command->command_orb_dma;
Stefan Richterca0c7452006-11-02 21:16:08 +01001717 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Stefan Richter09ee67a2006-08-14 18:43:00 +02001719 if (sbp2util_node_write_no_wait(scsi_id->ne, addr, data, length)) {
1720 /*
1721 * sbp2util_node_write_no_wait failed. We certainly ran out
1722 * of transaction labels, perhaps just because there were no
1723 * context switches which gave khpsbpkt a chance to collect
1724 * free tlabels. Try again in non-atomic context. If necessary,
1725 * the workqueue job will sleep to guaranteedly get a tlabel.
1726 * We do not accept new commands until the job is over.
1727 */
1728 scsi_block_requests(scsi_id->scsi_host);
Stefan Richterd19c7762006-12-07 22:40:33 +01001729 PREPARE_WORK(&scsi_id->protocol_work,
Stefan Richter09ee67a2006-08-14 18:43:00 +02001730 last_orb ? sbp2util_write_doorbell:
Stefan Richterd19c7762006-12-07 22:40:33 +01001731 sbp2util_write_orb_pointer
1732 /* */);
1733 schedule_work(&scsi_id->protocol_work);
Stefan Richter09ee67a2006-08-14 18:43:00 +02001734 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
1738 struct scsi_cmnd *SCpnt,
1739 void (*done)(struct scsi_cmnd *))
1740{
1741 unchar *cmd = (unchar *) SCpnt->cmnd;
1742 unsigned int request_bufflen = SCpnt->request_bufflen;
1743 struct sbp2_command_info *command;
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done);
Stefan Richteredf1fb22006-11-02 21:16:08 +01001746 if (!command)
Stefan Richtera237f352005-11-07 06:31:39 -05001747 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg,
1750 request_bufflen, SCpnt->request_buffer,
1751 SCpnt->sc_data_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 sbp2_link_orb_command(scsi_id, command);
1753
Stefan Richtera237f352005-11-07 06:31:39 -05001754 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755}
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 * Translates SBP-2 status into SCSI sense data for check conditions
1759 */
1760static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
1761{
Stefan Richtere8ca5662006-11-02 21:16:08 +01001762 /* OK, it's pretty ugly... ;-) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 sense_data[0] = 0x70;
1764 sense_data[1] = 0x0;
1765 sense_data[2] = sbp2_status[9];
1766 sense_data[3] = sbp2_status[12];
1767 sense_data[4] = sbp2_status[13];
1768 sense_data[5] = sbp2_status[14];
1769 sense_data[6] = sbp2_status[15];
1770 sense_data[7] = 10;
1771 sense_data[8] = sbp2_status[16];
1772 sense_data[9] = sbp2_status[17];
1773 sense_data[10] = sbp2_status[18];
1774 sense_data[11] = sbp2_status[19];
1775 sense_data[12] = sbp2_status[10];
1776 sense_data[13] = sbp2_status[11];
1777 sense_data[14] = sbp2_status[20];
1778 sense_data[15] = sbp2_status[21];
1779
Stefan Richtere8ca5662006-11-02 21:16:08 +01001780 return sbp2_status[8] & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
Stefan Richter3e98eab42006-07-23 22:16:00 +02001783static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
1784 int destid, quadlet_t *data, u64 addr,
1785 size_t length, u16 fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Stefan Richterca0c7452006-11-02 21:16:08 +01001787 struct sbp2_fwhost_info *hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 struct scsi_id_instance_data *scsi_id = NULL, *scsi_id_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 struct scsi_cmnd *SCpnt = NULL;
Stefan Richter3e98eab42006-07-23 22:16:00 +02001790 struct sbp2_status_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
1792 struct sbp2_command_info *command;
Jody McIntyre79456192005-11-07 06:29:39 -05001793 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
Stefan Richter60657722006-07-23 22:18:00 +02001795 if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
1796 SBP2_ERR("Wrong size of status block");
1797 return RCODE_ADDRESS_ERROR;
1798 }
1799 if (unlikely(!host)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 SBP2_ERR("host is NULL - this is bad!");
Stefan Richtera237f352005-11-07 06:31:39 -05001801 return RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
Stefan Richter60657722006-07-23 22:18:00 +02001804 if (unlikely(!hi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 SBP2_ERR("host info is NULL - this is bad!");
Stefan Richtera237f352005-11-07 06:31:39 -05001806 return RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
Stefan Richtere8ca5662006-11-02 21:16:08 +01001808
1809 /* Find the unit which wrote the status. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 list_for_each_entry(scsi_id_tmp, &hi->scsi_ids, scsi_list) {
Stefan Richter35bdddb2006-01-31 00:13:33 -05001811 if (scsi_id_tmp->ne->nodeid == nodeid &&
1812 scsi_id_tmp->status_fifo_addr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 scsi_id = scsi_id_tmp;
1814 break;
1815 }
1816 }
Stefan Richter60657722006-07-23 22:18:00 +02001817 if (unlikely(!scsi_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 SBP2_ERR("scsi_id is NULL - device is gone?");
Stefan Richtera237f352005-11-07 06:31:39 -05001819 return RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
1821
Stefan Richtere8ca5662006-11-02 21:16:08 +01001822 /* Put response into scsi_id status fifo buffer. The first two bytes
Stefan Richter3e98eab42006-07-23 22:16:00 +02001823 * come in big endian bit order. Often the target writes only a
1824 * truncated status block, minimally the first two quadlets. The rest
Stefan Richtere8ca5662006-11-02 21:16:08 +01001825 * is implied to be zeros. */
Stefan Richter3e98eab42006-07-23 22:16:00 +02001826 sb = &scsi_id->status_block;
1827 memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
1828 memcpy(sb, data, length);
1829 sbp2util_be32_to_cpu_buffer(sb, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
Stefan Richtere8ca5662006-11-02 21:16:08 +01001831 /* Ignore unsolicited status. Handle command ORB status. */
Stefan Richter60657722006-07-23 22:18:00 +02001832 if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
1833 command = NULL;
1834 else
1835 command = sbp2util_find_command_for_orb(scsi_id,
1836 sb->ORB_offset_lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
1839 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02001840 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
1842 sizeof(command->scatter_gather_element),
1843 PCI_DMA_BIDIRECTIONAL);
Stefan Richtere8ca5662006-11-02 21:16:08 +01001844 /* Grab SCSI command pointers and check status. */
Stefan Richter60657722006-07-23 22:18:00 +02001845 /*
1846 * FIXME: If the src field in the status is 1, the ORB DMA must
1847 * not be reused until status for a subsequent ORB is received.
1848 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 SCpnt = command->Current_SCpnt;
Stefan Richterca0c7452006-11-02 21:16:08 +01001850 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 sbp2util_mark_command_completed(scsi_id, command);
Stefan Richterca0c7452006-11-02 21:16:08 +01001852 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854 if (SCpnt) {
Stefan Richterabbca102006-08-14 18:51:00 +02001855 u32 h = sb->ORB_offset_hi_misc;
1856 u32 r = STATUS_GET_RESP(h);
1857
1858 if (r != RESP_STATUS_REQUEST_COMPLETE) {
Stefan Richter35644092006-11-02 21:16:08 +01001859 SBP2_INFO("resp 0x%x, sbp_status 0x%x",
Stefan Richterabbca102006-08-14 18:51:00 +02001860 r, STATUS_GET_SBP_STATUS(h));
Stefan Richter60657722006-07-23 22:18:00 +02001861 scsi_status =
Stefan Richterabbca102006-08-14 18:51:00 +02001862 r == RESP_STATUS_TRANSPORT_FAILURE ?
1863 SBP2_SCSI_STATUS_BUSY :
Stefan Richter60657722006-07-23 22:18:00 +02001864 SBP2_SCSI_STATUS_COMMAND_TERMINATED;
Stefan Richterabbca102006-08-14 18:51:00 +02001865 }
Stefan Richtere8ca5662006-11-02 21:16:08 +01001866
Stefan Richteredf1fb22006-11-02 21:16:08 +01001867 if (STATUS_GET_LEN(h) > 1)
Stefan Richter3e98eab42006-07-23 22:16:00 +02001868 scsi_status = sbp2_status_to_sense_data(
1869 (unchar *)sb, SCpnt->sense_buffer);
Stefan Richtere8ca5662006-11-02 21:16:08 +01001870
Stefan Richteredf1fb22006-11-02 21:16:08 +01001871 if (STATUS_TEST_DEAD(h))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 sbp2_agent_reset(scsi_id, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 }
1874
Stefan Richtere8ca5662006-11-02 21:16:08 +01001875 /* Check here to see if there are no commands in-use. If there
Stefan Richtercc078182006-07-23 22:12:00 +02001876 * are none, we know that the fetch agent left the active state
1877 * _and_ that we did not reactivate it yet. Therefore clear
1878 * last_orb so that next time we write directly to the
1879 * ORB_POINTER register. That way the fetch agent does not need
Stefan Richtere8ca5662006-11-02 21:16:08 +01001880 * to refetch the next_ORB. */
Stefan Richterca0c7452006-11-02 21:16:08 +01001881 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
1882 if (list_empty(&scsi_id->cmd_orb_inuse))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 scsi_id->last_orb = NULL;
Stefan Richterca0c7452006-11-02 21:16:08 +01001884 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 } else {
Stefan Richtere8ca5662006-11-02 21:16:08 +01001887 /* It's probably status after a management request. */
Stefan Richter3e98eab42006-07-23 22:16:00 +02001888 if ((sb->ORB_offset_lo == scsi_id->reconnect_orb_dma) ||
1889 (sb->ORB_offset_lo == scsi_id->login_orb_dma) ||
1890 (sb->ORB_offset_lo == scsi_id->query_logins_orb_dma) ||
Stefan Richtere8398bb2006-07-23 22:19:00 +02001891 (sb->ORB_offset_lo == scsi_id->logout_orb_dma)) {
1892 scsi_id->access_complete = 1;
Stefan Richterca0c7452006-11-02 21:16:08 +01001893 wake_up_interruptible(&sbp2_access_wq);
Stefan Richtere8398bb2006-07-23 22:19:00 +02001894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896
Stefan Richteredf1fb22006-11-02 21:16:08 +01001897 if (SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt,
1899 command->Current_done);
Stefan Richtera237f352005-11-07 06:31:39 -05001900 return RCODE_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901}
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903/**************************************
1904 * SCSI interface related section
1905 **************************************/
1906
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
1908 void (*done)(struct scsi_cmnd *))
1909{
1910 struct scsi_id_instance_data *scsi_id =
1911 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
Stefan Richterca0c7452006-11-02 21:16:08 +01001912 struct sbp2_fwhost_info *hi;
Jody McIntyreabd559b2005-09-30 11:59:06 -07001913 int result = DID_NO_CONNECT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Stefan Richter5796aa72006-11-02 21:16:08 +01001915 if (unlikely(!sbp2util_node_is_available(scsi_id)))
Jody McIntyreabd559b2005-09-30 11:59:06 -07001916 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 hi = scsi_id->hi;
1919
Stefan Richter5796aa72006-11-02 21:16:08 +01001920 if (unlikely(!hi)) {
Stefan Richterca0c7452006-11-02 21:16:08 +01001921 SBP2_ERR("sbp2_fwhost_info is NULL - this is bad!");
Jody McIntyreabd559b2005-09-30 11:59:06 -07001922 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 }
1924
Stefan Richtere8ca5662006-11-02 21:16:08 +01001925 /* Multiple units are currently represented to the SCSI core as separate
1926 * targets, not as one target with multiple LUs. Therefore return
1927 * selection time-out to any IO directed at non-zero LUNs. */
Stefan Richter5796aa72006-11-02 21:16:08 +01001928 if (unlikely(SCpnt->device->lun))
Jody McIntyreabd559b2005-09-30 11:59:06 -07001929 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Stefan Richtere8ca5662006-11-02 21:16:08 +01001931 /* handle the request sense command here (auto-request sense) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 if (SCpnt->cmnd[0] == REQUEST_SENSE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen);
1934 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
1935 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done);
Jody McIntyreabd559b2005-09-30 11:59:06 -07001936 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 }
1938
Stefan Richter5796aa72006-11-02 21:16:08 +01001939 if (unlikely(!hpsb_node_entry_valid(scsi_id->ne))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 SBP2_ERR("Bus reset in progress - rejecting command");
Jody McIntyreabd559b2005-09-30 11:59:06 -07001941 result = DID_BUS_BUSY << 16;
1942 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944
Stefan Richtere8ca5662006-11-02 21:16:08 +01001945 /* Bidirectional commands are not yet implemented,
1946 * and unknown transfer direction not handled. */
Stefan Richter5796aa72006-11-02 21:16:08 +01001947 if (unlikely(SCpnt->sc_data_direction == DMA_BIDIRECTIONAL)) {
Stefan Richter43863eb2005-12-12 23:03:24 -05001948 SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
1949 result = DID_ERROR << 16;
1950 goto done;
1951 }
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (sbp2_send_command(scsi_id, SCpnt, done)) {
1954 SBP2_ERR("Error sending SCSI command");
1955 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
1956 SCpnt, done);
1957 }
Jody McIntyreabd559b2005-09-30 11:59:06 -07001958 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
Jody McIntyreabd559b2005-09-30 11:59:06 -07001960done:
1961 SCpnt->result = result;
1962 done(SCpnt);
1963 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964}
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
1967 u32 status)
1968{
Stefan Richterca0c7452006-11-02 21:16:08 +01001969 struct sbp2_fwhost_info *hi = scsi_id->hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 struct list_head *lh;
1971 struct sbp2_command_info *command;
Jody McIntyre79456192005-11-07 06:29:39 -05001972 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Stefan Richterca0c7452006-11-02 21:16:08 +01001974 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
1975 while (!list_empty(&scsi_id->cmd_orb_inuse)) {
1976 lh = scsi_id->cmd_orb_inuse.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 command = list_entry(lh, struct sbp2_command_info, list);
1978 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
1979 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02001980 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
1982 sizeof(command->scatter_gather_element),
1983 PCI_DMA_BIDIRECTIONAL);
1984 sbp2util_mark_command_completed(scsi_id, command);
1985 if (command->Current_SCpnt) {
1986 command->Current_SCpnt->result = status << 16;
1987 command->Current_done(command->Current_SCpnt);
1988 }
1989 }
Stefan Richterca0c7452006-11-02 21:16:08 +01001990 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 return;
1993}
1994
1995/*
Stefan Richtere8ca5662006-11-02 21:16:08 +01001996 * Complete a regular SCSI command. Can be called in atomic context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 */
1998static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
1999 u32 scsi_status, struct scsi_cmnd *SCpnt,
2000 void (*done)(struct scsi_cmnd *))
2001{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (!SCpnt) {
2003 SBP2_ERR("SCpnt is NULL");
2004 return;
2005 }
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 switch (scsi_status) {
Stefan Richtera237f352005-11-07 06:31:39 -05002008 case SBP2_SCSI_STATUS_GOOD:
Stefan Richter8f0525f2006-03-28 20:03:45 -05002009 SCpnt->result = DID_OK << 16;
Stefan Richtera237f352005-11-07 06:31:39 -05002010 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Stefan Richtera237f352005-11-07 06:31:39 -05002012 case SBP2_SCSI_STATUS_BUSY:
2013 SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
2014 SCpnt->result = DID_BUS_BUSY << 16;
2015 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
Stefan Richtera237f352005-11-07 06:31:39 -05002017 case SBP2_SCSI_STATUS_CHECK_CONDITION:
Stefan Richter8f0525f2006-03-28 20:03:45 -05002018 SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
Stefan Richtera237f352005-11-07 06:31:39 -05002019 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
Stefan Richtera237f352005-11-07 06:31:39 -05002021 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
2022 SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
2023 SCpnt->result = DID_NO_CONNECT << 16;
2024 scsi_print_command(SCpnt);
2025 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026
Stefan Richtera237f352005-11-07 06:31:39 -05002027 case SBP2_SCSI_STATUS_CONDITION_MET:
2028 case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
2029 case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
2030 SBP2_ERR("Bad SCSI status = %x", scsi_status);
2031 SCpnt->result = DID_ERROR << 16;
2032 scsi_print_command(SCpnt);
2033 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Stefan Richtera237f352005-11-07 06:31:39 -05002035 default:
2036 SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
2037 SCpnt->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 }
2039
Stefan Richtere8ca5662006-11-02 21:16:08 +01002040 /* If a bus reset is in progress and there was an error, complete
2041 * the command as busy so that it will get retried. */
Stefan Richtera237f352005-11-07 06:31:39 -05002042 if (!hpsb_node_entry_valid(scsi_id->ne)
2043 && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 SBP2_ERR("Completing command with busy (bus reset)");
2045 SCpnt->result = DID_BUS_BUSY << 16;
2046 }
2047
Stefan Richtere8ca5662006-11-02 21:16:08 +01002048 /* Tell the SCSI stack that we're done with this command. */
Stefan Richtera237f352005-11-07 06:31:39 -05002049 done(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050}
2051
Jody McIntyreabd559b2005-09-30 11:59:06 -07002052static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
2053{
Stefan Richtera80614d2006-02-14 22:04:19 -05002054 struct scsi_id_instance_data *scsi_id =
2055 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2056
2057 scsi_id->sdev = sdev;
Stefan Richterc394f1e2006-08-07 20:48:00 +02002058 sdev->allow_restart = 1;
Stefan Richtera80614d2006-02-14 22:04:19 -05002059
Stefan Richter24d3bf82006-05-15 22:04:59 +02002060 if (scsi_id->workarounds & SBP2_WORKAROUND_INQUIRY_36)
Stefan Richtera80614d2006-02-14 22:04:19 -05002061 sdev->inquiry_len = 36;
Jody McIntyreabd559b2005-09-30 11:59:06 -07002062 return 0;
2063}
2064
Jody McIntyreabd559b2005-09-30 11:59:06 -07002065static int sbp2scsi_slave_configure(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
Stefan Richter24d3bf82006-05-15 22:04:59 +02002067 struct scsi_id_instance_data *scsi_id =
2068 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2069
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
Ben Collins365c7862005-11-07 06:31:24 -05002071 sdev->use_10_for_rw = 1;
Stefan Richter24d3bf82006-05-15 22:04:59 +02002072
2073 if (sdev->type == TYPE_DISK &&
2074 scsi_id->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
2075 sdev->skip_ms_page_8 = 1;
Stefan Richtere9a1c522006-05-15 22:06:37 +02002076 if (scsi_id->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
2077 sdev->fix_capacity = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return 0;
2079}
2080
Jody McIntyreabd559b2005-09-30 11:59:06 -07002081static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
2082{
2083 ((struct scsi_id_instance_data *)sdev->host->hostdata[0])->sdev = NULL;
2084 return;
2085}
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087/*
Stefan Richtere8ca5662006-11-02 21:16:08 +01002088 * Called by scsi stack when something has really gone wrong.
2089 * Usually called when a command has timed-out for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 */
2091static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
2092{
2093 struct scsi_id_instance_data *scsi_id =
2094 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
Stefan Richterca0c7452006-11-02 21:16:08 +01002095 struct sbp2_fwhost_info *hi = scsi_id->hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 struct sbp2_command_info *command;
Stefan Richter24c7cd02006-04-01 21:11:41 +02002097 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Stefan Richter35644092006-11-02 21:16:08 +01002099 SBP2_INFO("aborting sbp2 command");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 scsi_print_command(SCpnt);
2101
Jody McIntyreabd559b2005-09-30 11:59:06 -07002102 if (sbp2util_node_is_available(scsi_id)) {
Stefan Richter23077f12006-09-11 20:17:14 +02002103 sbp2_agent_reset(scsi_id, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Stefan Richter23077f12006-09-11 20:17:14 +02002105 /* Return a matching command structure to the free pool. */
Stefan Richterca0c7452006-11-02 21:16:08 +01002106 spin_lock_irqsave(&scsi_id->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
2108 if (command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 pci_dma_sync_single_for_cpu(hi->host->pdev,
2110 command->command_orb_dma,
2111 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02002112 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 pci_dma_sync_single_for_cpu(hi->host->pdev,
2114 command->sge_dma,
2115 sizeof(command->scatter_gather_element),
2116 PCI_DMA_BIDIRECTIONAL);
2117 sbp2util_mark_command_completed(scsi_id, command);
2118 if (command->Current_SCpnt) {
2119 command->Current_SCpnt->result = DID_ABORT << 16;
2120 command->Current_done(command->Current_SCpnt);
2121 }
2122 }
Stefan Richterca0c7452006-11-02 21:16:08 +01002123 spin_unlock_irqrestore(&scsi_id->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
2126 }
2127
Stefan Richtera237f352005-11-07 06:31:39 -05002128 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129}
2130
2131/*
2132 * Called by scsi stack when something has really gone wrong.
2133 */
Jody McIntyreabd559b2005-09-30 11:59:06 -07002134static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
2136 struct scsi_id_instance_data *scsi_id =
2137 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2138
Stefan Richter35644092006-11-02 21:16:08 +01002139 SBP2_INFO("reset requested");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Jody McIntyreabd559b2005-09-30 11:59:06 -07002141 if (sbp2util_node_is_available(scsi_id)) {
Stefan Richter35644092006-11-02 21:16:08 +01002142 SBP2_INFO("generating sbp2 fetch agent reset");
Stefan Richter1f427e82006-08-14 18:44:00 +02002143 sbp2_agent_reset(scsi_id, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145
Jody McIntyreabd559b2005-09-30 11:59:06 -07002146 return SUCCESS;
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04002147}
2148
Stefan Richtera237f352005-11-07 06:31:39 -05002149static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
2150 struct device_attribute *attr,
2151 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152{
2153 struct scsi_device *sdev;
2154 struct scsi_id_instance_data *scsi_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 if (!(sdev = to_scsi_device(dev)))
2157 return 0;
2158
2159 if (!(scsi_id = (struct scsi_id_instance_data *)sdev->host->hostdata[0]))
2160 return 0;
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)scsi_id->ne->guid,
Stefan Richterca0c7452006-11-02 21:16:08 +01002163 scsi_id->ud->id, ORB_SET_LUN(scsi_id->lun));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
2166MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2167MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2168MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2169MODULE_LICENSE("GPL");
2170
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171static int sbp2_module_init(void)
2172{
2173 int ret;
2174
Stefan Richterca0c7452006-11-02 21:16:08 +01002175 if (sbp2_serialize_io) {
2176 sbp2_shost_template.can_queue = 1;
2177 sbp2_shost_template.cmd_per_lun = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 }
2179
Stefan Richter24d3bf82006-05-15 22:04:59 +02002180 if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
Stefan Richterca0c7452006-11-02 21:16:08 +01002181 (sbp2_max_sectors * 512) > (128 * 1024))
2182 sbp2_max_sectors = 128 * 1024 / 512;
2183 sbp2_shost_template.max_sectors = sbp2_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 hpsb_register_highlevel(&sbp2_highlevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 ret = hpsb_register_protocol(&sbp2_driver);
2187 if (ret) {
2188 SBP2_ERR("Failed to register protocol");
2189 hpsb_unregister_highlevel(&sbp2_highlevel);
2190 return ret;
2191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 return 0;
2193}
2194
2195static void __exit sbp2_module_exit(void)
2196{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 hpsb_unregister_protocol(&sbp2_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 hpsb_unregister_highlevel(&sbp2_highlevel);
2199}
2200
2201module_init(sbp2_module_init);
2202module_exit(sbp2_module_exit);