blob: f1f5de616d79cae1bad61db5201571c6c3bee3f6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * sbp2.c - SBP-2 protocol driver for IEEE-1394
3 *
4 * Copyright (C) 2000 James Goodwin, Filanet Corporation (www.filanet.com)
5 * jamesg@filanet.com (JSG)
6 *
7 * Copyright (C) 2003 Ben Collins <bcollins@debian.org>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24/*
25 * Brief Description:
26 *
27 * This driver implements the Serial Bus Protocol 2 (SBP-2) over IEEE-1394
28 * under Linux. The SBP-2 driver is implemented as an IEEE-1394 high-level
29 * driver. It also registers as a SCSI lower-level driver in order to accept
30 * SCSI commands for transport using SBP-2.
31 *
32 * You may access any attached SBP-2 storage devices as if they were SCSI
33 * devices (e.g. mount /dev/sda1, fdisk, mkfs, etc.).
34 *
35 * Current Issues:
36 *
37 * - Error Handling: SCSI aborts and bus reset requests are handled somewhat
38 * but the code needs additional debugging.
39 */
40
Stefan Richter902abed2006-08-14 18:56:00 +020041#include <linux/blkdev.h>
42#include <linux/compiler.h>
43#include <linux/delay.h>
44#include <linux/device.h>
45#include <linux/dma-mapping.h>
46#include <linux/gfp.h>
47#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/kernel.h>
49#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <linux/module.h>
51#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/pci.h>
Stefan Richter902abed2006-08-14 18:56:00 +020053#include <linux/slab.h>
54#include <linux/spinlock.h>
55#include <linux/stat.h>
56#include <linux/string.h>
57#include <linux/stringify.h>
58#include <linux/types.h>
Stefan Richtere8398bb2006-07-23 22:19:00 +020059#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <asm/byteorder.h>
Stefan Richter902abed2006-08-14 18:56:00 +020062#include <asm/errno.h>
63#include <asm/param.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <asm/scatterlist.h>
Stefan Richter902abed2006-08-14 18:56:00 +020065#include <asm/system.h>
66#include <asm/types.h>
67
68#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
69#include <asm/io.h> /* for bus_to_virt */
70#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72#include <scsi/scsi.h>
73#include <scsi/scsi_cmnd.h>
74#include <scsi/scsi_dbg.h>
75#include <scsi/scsi_device.h>
76#include <scsi/scsi_host.h>
77
78#include "csr1212.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include "highlevel.h"
Stefan Richter902abed2006-08-14 18:56:00 +020080#include "hosts.h"
81#include "ieee1394.h"
82#include "ieee1394_core.h"
83#include "ieee1394_hotplug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070084#include "ieee1394_transactions.h"
Stefan Richter902abed2006-08-14 18:56:00 +020085#include "ieee1394_types.h"
86#include "nodemgr.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070087#include "sbp2.h"
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * Module load parameter definitions
91 */
92
93/*
94 * Change max_speed on module load if you have a bad IEEE-1394
95 * controller that has trouble running 2KB packets at 400mb.
96 *
97 * NOTE: On certain OHCI parts I have seen short packets on async transmit
98 * (probably due to PCI latency/throughput issues with the part). You can
99 * bump down the speed if you are running into problems.
100 */
101static int max_speed = IEEE1394_SPEED_MAX;
102module_param(max_speed, int, 0644);
Jody McIntyre2bab3592005-09-30 11:59:07 -0700103MODULE_PARM_DESC(max_speed, "Force max speed (3 = 800mb, 2 = 400mb, 1 = 200mb, 0 = 100mb)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/*
106 * Set serialize_io to 1 if you'd like only one scsi command sent
107 * down to us at a time (debugging). This might be necessary for very
108 * badly behaved sbp2 devices.
Jody McIntyre2bab3592005-09-30 11:59:07 -0700109 *
110 * TODO: Make this configurable per device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 */
Jody McIntyre2bab3592005-09-30 11:59:07 -0700112static int serialize_io = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113module_param(serialize_io, int, 0444);
Jody McIntyre2bab3592005-09-30 11:59:07 -0700114MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers (default = 1, faster = 0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
116/*
117 * Bump up max_sectors if you'd like to support very large sized
118 * transfers. Please note that some older sbp2 bridge chips are broken for
119 * transfers greater or equal to 128KB. Default is a value of 255
120 * sectors, or just under 128KB (at 512 byte sector size). I can note that
121 * the Oxsemi sbp2 chipsets have no problems supporting very large
122 * transfer sizes.
123 */
124static int max_sectors = SBP2_MAX_SECTORS;
125module_param(max_sectors, int, 0444);
Stefan Richter24d3bf82006-05-15 22:04:59 +0200126MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported (default = "
127 __stringify(SBP2_MAX_SECTORS) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129/*
130 * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
131 * do an exclusive login, as it's generally unsafe to have two hosts
132 * talking to a single sbp2 device at the same time (filesystem coherency,
133 * etc.). If you're running an sbp2 device that supports multiple logins,
134 * and you're either running read-only filesystems or some sort of special
Ben Collins20f45782006-06-12 18:13:11 -0400135 * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster
136 * File System, or Lustre, then set exclusive_login to zero.
137 *
138 * So far only bridges from Oxford Semiconductor are known to support
139 * concurrent logins. Depending on firmware, four or two concurrent logins
140 * are possible on OXFW911 and newer Oxsemi bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 */
142static int exclusive_login = 1;
143module_param(exclusive_login, int, 0644);
144MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device (default = 1)");
145
146/*
Stefan Richter24d3bf82006-05-15 22:04:59 +0200147 * If any of the following workarounds is required for your device to work,
148 * please submit the kernel messages logged by sbp2 to the linux1394-devel
149 * mailing list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 *
Stefan Richter24d3bf82006-05-15 22:04:59 +0200151 * - 128kB max transfer
152 * Limit transfer size. Necessary for some old bridges.
153 *
154 * - 36 byte inquiry
155 * When scsi_mod probes the device, let the inquiry command look like that
156 * from MS Windows.
157 *
158 * - skip mode page 8
159 * Suppress sending of mode_sense for mode page 8 if the device pretends to
160 * support the SCSI Primary Block commands instead of Reduced Block Commands.
Stefan Richtere9a1c522006-05-15 22:06:37 +0200161 *
162 * - fix capacity
163 * Tell sd_mod to correct the last sector number reported by read_capacity.
164 * Avoids access beyond actual disk limits on devices with an off-by-one bug.
165 * Don't use this with devices which don't have this bug.
Stefan Richter679c0cd2006-05-15 22:08:09 +0200166 *
167 * - override internal blacklist
168 * Instead of adding to the built-in blacklist, use only the workarounds
169 * specified in the module load parameter.
170 * Useful if a blacklist entry interfered with a non-broken device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
Stefan Richter24d3bf82006-05-15 22:04:59 +0200172static int sbp2_default_workarounds;
173module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
174MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
175 ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
176 ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
177 ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
Stefan Richtere9a1c522006-05-15 22:06:37 +0200178 ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
Stefan Richter679c0cd2006-05-15 22:08:09 +0200179 ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
Stefan Richter24d3bf82006-05-15 22:04:59 +0200180 ", or a combination)");
181
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 * Debug levels, configured via kernel config, or enable here.
184 */
185
Olaf Hering44456d32005-07-27 11:45:17 -0700186#define CONFIG_IEEE1394_SBP2_DEBUG 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187/* #define CONFIG_IEEE1394_SBP2_DEBUG_ORBS */
188/* #define CONFIG_IEEE1394_SBP2_DEBUG_DMA */
189/* #define CONFIG_IEEE1394_SBP2_DEBUG 1 */
190/* #define CONFIG_IEEE1394_SBP2_DEBUG 2 */
191/* #define CONFIG_IEEE1394_SBP2_PACKET_DUMP */
192
193#ifdef CONFIG_IEEE1394_SBP2_DEBUG_ORBS
194#define SBP2_ORB_DEBUG(fmt, args...) HPSB_ERR("sbp2(%s): "fmt, __FUNCTION__, ## args)
195static u32 global_outstanding_command_orbs = 0;
196#define outstanding_orb_incr global_outstanding_command_orbs++
197#define outstanding_orb_decr global_outstanding_command_orbs--
198#else
Stefan Richter611aa192006-08-02 18:44:00 +0200199#define SBP2_ORB_DEBUG(fmt, args...) do {} while (0)
200#define outstanding_orb_incr do {} while (0)
201#define outstanding_orb_decr do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202#endif
203
204#ifdef CONFIG_IEEE1394_SBP2_DEBUG_DMA
205#define SBP2_DMA_ALLOC(fmt, args...) \
206 HPSB_ERR("sbp2(%s)alloc(%d): "fmt, __FUNCTION__, \
207 ++global_outstanding_dmas, ## args)
208#define SBP2_DMA_FREE(fmt, args...) \
209 HPSB_ERR("sbp2(%s)free(%d): "fmt, __FUNCTION__, \
210 --global_outstanding_dmas, ## args)
211static u32 global_outstanding_dmas = 0;
212#else
Stefan Richter611aa192006-08-02 18:44:00 +0200213#define SBP2_DMA_ALLOC(fmt, args...) do {} while (0)
214#define SBP2_DMA_FREE(fmt, args...) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#endif
216
217#if CONFIG_IEEE1394_SBP2_DEBUG >= 2
218#define SBP2_DEBUG(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
219#define SBP2_INFO(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
220#define SBP2_NOTICE(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
221#define SBP2_WARN(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
222#elif CONFIG_IEEE1394_SBP2_DEBUG == 1
223#define SBP2_DEBUG(fmt, args...) HPSB_DEBUG("sbp2: "fmt, ## args)
224#define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
225#define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
226#define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
227#else
Stefan Richter611aa192006-08-02 18:44:00 +0200228#define SBP2_DEBUG(fmt, args...) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229#define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
230#define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
231#define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
232#endif
233
234#define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
Stefan Richterd024ebc2006-03-28 20:03:55 -0500235#define SBP2_DEBUG_ENTER() SBP2_DEBUG("%s", __FUNCTION__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/*
238 * Globals
239 */
Stefan Richterea42ea02006-11-02 21:16:08 +0100240static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *, u32);
241static void sbp2scsi_complete_command(struct scsi_id_instance_data *, u32,
242 struct scsi_cmnd *,
243 void (*)(struct scsi_cmnd *));
244static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *);
245static int sbp2_start_device(struct scsi_id_instance_data *);
246static void sbp2_remove_device(struct scsi_id_instance_data *);
247static int sbp2_login_device(struct scsi_id_instance_data *);
248static int sbp2_reconnect_device(struct scsi_id_instance_data *);
249static int sbp2_logout_device(struct scsi_id_instance_data *);
250static void sbp2_host_reset(struct hpsb_host *);
251static int sbp2_handle_status_write(struct hpsb_host *, int, int, quadlet_t *,
252 u64, size_t, u16);
253static int sbp2_agent_reset(struct scsi_id_instance_data *, int);
254static void sbp2_parse_unit_directory(struct scsi_id_instance_data *,
255 struct unit_directory *);
256static int sbp2_set_busy_timeout(struct scsi_id_instance_data *);
257static int sbp2_max_speed_and_size(struct scsi_id_instance_data *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
261
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262static struct hpsb_highlevel sbp2_highlevel = {
Stefan Richterea42ea02006-11-02 21:16:08 +0100263 .name = SBP2_DEVICE_NAME,
264 .host_reset = sbp2_host_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265};
266
267static struct hpsb_address_ops sbp2_ops = {
Stefan Richterea42ea02006-11-02 21:16:08 +0100268 .write = sbp2_handle_status_write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269};
270
271#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
Stefan Richterea42ea02006-11-02 21:16:08 +0100272static int sbp2_handle_physdma_write(struct hpsb_host *, int, int, quadlet_t *,
273 u64, size_t, u16);
274static int sbp2_handle_physdma_read(struct hpsb_host *, int, quadlet_t *, u64,
275 size_t, u16);
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277static struct hpsb_address_ops sbp2_physdma_ops = {
Stefan Richterea42ea02006-11-02 21:16:08 +0100278 .read = sbp2_handle_physdma_read,
279 .write = sbp2_handle_physdma_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280};
281#endif
282
Stefan Richterea42ea02006-11-02 21:16:08 +0100283
284/*
285 * Interface to driver core and IEEE 1394 core
286 */
287static struct ieee1394_device_id sbp2_id_table[] = {
288 {
289 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
290 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
291 .version = SBP2_SW_VERSION_ENTRY & 0xffffff},
292 {}
293};
294MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
295
296static int sbp2_probe(struct device *);
297static int sbp2_remove(struct device *);
298static int sbp2_update(struct unit_directory *);
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300static struct hpsb_protocol_driver sbp2_driver = {
301 .name = "SBP2 Driver",
302 .id_table = sbp2_id_table,
303 .update = sbp2_update,
304 .driver = {
305 .name = SBP2_DEVICE_NAME,
306 .bus = &ieee1394_bus_type,
307 .probe = sbp2_probe,
308 .remove = sbp2_remove,
309 },
310};
311
Stefan Richterea42ea02006-11-02 21:16:08 +0100312
313/*
314 * Interface to SCSI core
315 */
316static int sbp2scsi_queuecommand(struct scsi_cmnd *,
317 void (*)(struct scsi_cmnd *));
318static int sbp2scsi_abort(struct scsi_cmnd *);
319static int sbp2scsi_reset(struct scsi_cmnd *);
320static int sbp2scsi_slave_alloc(struct scsi_device *);
321static int sbp2scsi_slave_configure(struct scsi_device *);
322static void sbp2scsi_slave_destroy(struct scsi_device *);
323static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *,
324 struct device_attribute *, char *);
325
326static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
327
328static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
329 &dev_attr_ieee1394_id,
330 NULL
331};
332
333static struct scsi_host_template scsi_driver_template = {
334 .module = THIS_MODULE,
335 .name = "SBP-2 IEEE-1394",
336 .proc_name = SBP2_DEVICE_NAME,
337 .queuecommand = sbp2scsi_queuecommand,
338 .eh_abort_handler = sbp2scsi_abort,
339 .eh_device_reset_handler = sbp2scsi_reset,
340 .slave_alloc = sbp2scsi_slave_alloc,
341 .slave_configure = sbp2scsi_slave_configure,
342 .slave_destroy = sbp2scsi_slave_destroy,
343 .this_id = -1,
344 .sg_tablesize = SG_ALL,
345 .use_clustering = ENABLE_CLUSTERING,
346 .cmd_per_lun = SBP2_MAX_CMDS,
347 .can_queue = SBP2_MAX_CMDS,
348 .emulated = 1,
349 .sdev_attrs = sbp2_sysfs_sdev_attrs,
350};
351
352
Stefan Richtera80614d2006-02-14 22:04:19 -0500353/*
Stefan Richter24d3bf82006-05-15 22:04:59 +0200354 * List of devices with known bugs.
355 *
356 * The firmware_revision field, masked with 0xffff00, is the best indicator
357 * for the type of bridge chip of a device. It yields a few false positives
358 * but this did not break correctly behaving devices so far.
Stefan Richtera80614d2006-02-14 22:04:19 -0500359 */
Stefan Richter24d3bf82006-05-15 22:04:59 +0200360static const struct {
361 u32 firmware_revision;
Stefan Richtere9a1c522006-05-15 22:06:37 +0200362 u32 model_id;
Stefan Richter24d3bf82006-05-15 22:04:59 +0200363 unsigned workarounds;
364} sbp2_workarounds_table[] = {
Ben Collins4b9a3342006-06-12 18:10:18 -0400365 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
Stefan Richter24d3bf82006-05-15 22:04:59 +0200366 .firmware_revision = 0x002800,
Ben Collins4b9a3342006-06-12 18:10:18 -0400367 .model_id = 0x001010,
Stefan Richter24d3bf82006-05-15 22:04:59 +0200368 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
369 SBP2_WORKAROUND_MODE_SENSE_8,
370 },
371 /* Initio bridges, actually only needed for some older ones */ {
372 .firmware_revision = 0x000200,
373 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
374 },
375 /* Symbios bridge */ {
376 .firmware_revision = 0xa0b800,
377 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
Stefan Richtere9a1c522006-05-15 22:06:37 +0200378 },
379 /*
380 * Note about the following Apple iPod blacklist entries:
381 *
382 * There are iPods (2nd gen, 3rd gen) with model_id==0. Since our
383 * matching logic treats 0 as a wildcard, we cannot match this ID
384 * without rewriting the matching routine. Fortunately these iPods
385 * do not feature the read_capacity bug according to one report.
386 * Read_capacity behaviour as well as model_id could change due to
387 * Apple-supplied firmware updates though.
388 */
389 /* iPod 4th generation */ {
390 .firmware_revision = 0x0a2700,
391 .model_id = 0x000021,
392 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
393 },
394 /* iPod mini */ {
395 .firmware_revision = 0x0a2700,
396 .model_id = 0x000023,
397 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
398 },
399 /* iPod Photo */ {
400 .firmware_revision = 0x0a2700,
401 .model_id = 0x00007e,
402 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
Stefan Richter24d3bf82006-05-15 22:04:59 +0200403 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404};
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406/**************************************
407 * General utility functions
408 **************************************/
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410#ifndef __BIG_ENDIAN
411/*
412 * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
413 */
Stefan Richter2b01b802006-07-03 12:02:28 -0400414static inline void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 u32 *temp = buffer;
417
418 for (length = (length >> 2); length--; )
419 temp[length] = be32_to_cpu(temp[length]);
420
421 return;
422}
423
424/*
425 * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
426 */
Stefan Richter2b01b802006-07-03 12:02:28 -0400427static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
429 u32 *temp = buffer;
430
431 for (length = (length >> 2); length--; )
432 temp[length] = cpu_to_be32(temp[length]);
433
434 return;
435}
436#else /* BIG_ENDIAN */
437/* Why waste the cpu cycles? */
Stefan Richter611aa192006-08-02 18:44:00 +0200438#define sbp2util_be32_to_cpu_buffer(x,y) do {} while (0)
439#define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440#endif
441
442#ifdef CONFIG_IEEE1394_SBP2_PACKET_DUMP
443/*
444 * Debug packet dump routine. Length is in bytes.
445 */
Stefan Richtera237f352005-11-07 06:31:39 -0500446static void sbp2util_packet_dump(void *buffer, int length, char *dump_name,
447 u32 dump_phys_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 int i;
450 unsigned char *dump = buffer;
451
452 if (!dump || !length || !dump_name)
453 return;
454
455 if (dump_phys_addr)
456 printk("[%s, 0x%x]", dump_name, dump_phys_addr);
457 else
458 printk("[%s]", dump_name);
459 for (i = 0; i < length; i++) {
460 if (i > 0x3f) {
461 printk("\n ...");
462 break;
463 }
464 if ((i & 0x3) == 0)
465 printk(" ");
466 if ((i & 0xf) == 0)
467 printk("\n ");
Stefan Richtera237f352005-11-07 06:31:39 -0500468 printk("%02x ", (int)dump[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
470 printk("\n");
471
472 return;
473}
474#else
Stefan Richter611aa192006-08-02 18:44:00 +0200475#define sbp2util_packet_dump(w,x,y,z) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476#endif
477
Stefan Richtere8398bb2006-07-23 22:19:00 +0200478static DECLARE_WAIT_QUEUE_HEAD(access_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Stefan Richtere8398bb2006-07-23 22:19:00 +0200480/*
481 * Waits for completion of an SBP-2 access request.
482 * Returns nonzero if timed out or prematurely interrupted.
483 */
484static int sbp2util_access_timeout(struct scsi_id_instance_data *scsi_id,
485 int timeout)
486{
487 long leftover = wait_event_interruptible_timeout(
488 access_wq, scsi_id->access_complete, timeout);
489
490 scsi_id->access_complete = 0;
491 return leftover <= 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Stefan Richtere8398bb2006-07-23 22:19:00 +0200494/* Frees an allocated packet */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495static void sbp2_free_packet(struct hpsb_packet *packet)
496{
497 hpsb_free_tlabel(packet);
498 hpsb_free_packet(packet);
499}
500
501/* This is much like hpsb_node_write(), except it ignores the response
502 * subaction and returns immediately. Can be used from interrupts.
503 */
504static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
Stefan Richtera237f352005-11-07 06:31:39 -0500505 quadlet_t *buffer, size_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 struct hpsb_packet *packet;
508
509 packet = hpsb_make_writepacket(ne->host, ne->nodeid,
510 addr, buffer, length);
Stefan Richtera237f352005-11-07 06:31:39 -0500511 if (!packet)
512 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Stefan Richtera237f352005-11-07 06:31:39 -0500514 hpsb_set_packet_complete_task(packet,
515 (void (*)(void *))sbp2_free_packet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 packet);
517
518 hpsb_node_fill_packet(ne, packet);
519
Stefan Richtera237f352005-11-07 06:31:39 -0500520 if (hpsb_send_packet(packet) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 sbp2_free_packet(packet);
522 return -EIO;
523 }
524
525 return 0;
526}
527
Stefan Richter09ee67a2006-08-14 18:43:00 +0200528static void sbp2util_notify_fetch_agent(struct scsi_id_instance_data *scsi_id,
529 u64 offset, quadlet_t *data, size_t len)
530{
531 /*
532 * There is a small window after a bus reset within which the node
533 * entry's generation is current but the reconnect wasn't completed.
534 */
Stefan Richter2cccbb52006-08-14 18:59:00 +0200535 if (unlikely(atomic_read(&scsi_id->state) == SBP2LU_STATE_IN_RESET))
Stefan Richter09ee67a2006-08-14 18:43:00 +0200536 return;
537
538 if (hpsb_node_write(scsi_id->ne,
539 scsi_id->sbp2_command_block_agent_addr + offset,
540 data, len))
541 SBP2_ERR("sbp2util_notify_fetch_agent failed.");
542 /*
543 * Now accept new SCSI commands, unless a bus reset happended during
544 * hpsb_node_write.
545 */
Stefan Richter2cccbb52006-08-14 18:59:00 +0200546 if (likely(atomic_read(&scsi_id->state) != SBP2LU_STATE_IN_RESET))
Stefan Richter09ee67a2006-08-14 18:43:00 +0200547 scsi_unblock_requests(scsi_id->scsi_host);
548}
549
David Howellsc4028952006-11-22 14:57:56 +0000550static void sbp2util_write_orb_pointer(struct work_struct *work)
Stefan Richter09ee67a2006-08-14 18:43:00 +0200551{
David Howellsc4028952006-11-22 14:57:56 +0000552 struct scsi_id_instance_data *scsi_id =
553 container_of(work, struct scsi_id_instance_data,
554 protocol_work.work);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200555 quadlet_t data[2];
556
David Howellsc4028952006-11-22 14:57:56 +0000557 data[0] = ORB_SET_NODE_ID(scsi_id->hi->host->node_id);
558 data[1] = scsi_id->last_orb_dma;
Stefan Richter09ee67a2006-08-14 18:43:00 +0200559 sbp2util_cpu_to_be32_buffer(data, 8);
David Howellsc4028952006-11-22 14:57:56 +0000560 sbp2util_notify_fetch_agent(scsi_id, SBP2_ORB_POINTER_OFFSET, data, 8);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200561}
562
David Howellsc4028952006-11-22 14:57:56 +0000563static void sbp2util_write_doorbell(struct work_struct *work)
Stefan Richter09ee67a2006-08-14 18:43:00 +0200564{
David Howellsc4028952006-11-22 14:57:56 +0000565 struct scsi_id_instance_data *scsi_id =
566 container_of(work, struct scsi_id_instance_data,
567 protocol_work.work);
568 sbp2util_notify_fetch_agent(scsi_id, SBP2_DOORBELL_OFFSET, NULL, 4);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200569}
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571/*
572 * This function is called to create a pool of command orbs used for
573 * command processing. It is called when a new sbp2 device is detected.
574 */
575static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id)
576{
577 struct sbp2scsi_host_info *hi = scsi_id->hi;
578 int i;
579 unsigned long flags, orbs;
580 struct sbp2_command_info *command;
581
582 orbs = serialize_io ? 2 : SBP2_MAX_CMDS;
583
584 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
585 for (i = 0; i < orbs; i++) {
Stefan Richter85511582005-11-07 06:31:45 -0500586 command = kzalloc(sizeof(*command), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (!command) {
Stefan Richtera237f352005-11-07 06:31:39 -0500588 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock,
589 flags);
590 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 command->command_orb_dma =
Stefan Richtera237f352005-11-07 06:31:39 -0500593 pci_map_single(hi->host->pdev, &command->command_orb,
594 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +0200595 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 SBP2_DMA_ALLOC("single command orb DMA");
597 command->sge_dma =
Stefan Richtera237f352005-11-07 06:31:39 -0500598 pci_map_single(hi->host->pdev,
599 &command->scatter_gather_element,
600 sizeof(command->scatter_gather_element),
601 PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 SBP2_DMA_ALLOC("scatter_gather_element");
603 INIT_LIST_HEAD(&command->list);
604 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
605 }
606 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
607 return 0;
608}
609
610/*
611 * This function is called to delete a pool of command orbs.
612 */
613static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id)
614{
615 struct hpsb_host *host = scsi_id->hi->host;
616 struct list_head *lh, *next;
617 struct sbp2_command_info *command;
618 unsigned long flags;
619
620 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
621 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
622 list_for_each_safe(lh, next, &scsi_id->sbp2_command_orb_completed) {
623 command = list_entry(lh, struct sbp2_command_info, list);
624
625 /* Release our generic DMA's */
626 pci_unmap_single(host->pdev, command->command_orb_dma,
627 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +0200628 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 SBP2_DMA_FREE("single command orb DMA");
630 pci_unmap_single(host->pdev, command->sge_dma,
631 sizeof(command->scatter_gather_element),
632 PCI_DMA_BIDIRECTIONAL);
633 SBP2_DMA_FREE("scatter_gather_element");
634
635 kfree(command);
636 }
637 }
638 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
639 return;
640}
641
642/*
643 * This function finds the sbp2_command for a given outstanding command
644 * orb.Only looks at the inuse list.
645 */
646static struct sbp2_command_info *sbp2util_find_command_for_orb(
647 struct scsi_id_instance_data *scsi_id, dma_addr_t orb)
648{
649 struct sbp2_command_info *command;
650 unsigned long flags;
651
652 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
653 if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
654 list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list) {
655 if (command->command_orb_dma == orb) {
656 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Stefan Richtera237f352005-11-07 06:31:39 -0500657 return command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
659 }
660 }
661 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
662
663 SBP2_ORB_DEBUG("could not match command orb %x", (unsigned int)orb);
664
Stefan Richtera237f352005-11-07 06:31:39 -0500665 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
668/*
669 * This function finds the sbp2_command for a given outstanding SCpnt.
670 * Only looks at the inuse list.
Stefan Richter24c7cd02006-04-01 21:11:41 +0200671 * Must be called with scsi_id->sbp2_command_orb_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 */
Stefan Richter24c7cd02006-04-01 21:11:41 +0200673static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
674 struct scsi_id_instance_data *scsi_id, void *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
676 struct sbp2_command_info *command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Stefan Richter24c7cd02006-04-01 21:11:41 +0200678 if (!list_empty(&scsi_id->sbp2_command_orb_inuse))
679 list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list)
680 if (command->Current_SCpnt == SCpnt)
Stefan Richtera237f352005-11-07 06:31:39 -0500681 return command;
Stefan Richtera237f352005-11-07 06:31:39 -0500682 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
685/*
686 * This function allocates a command orb used to send a scsi command.
687 */
688static struct sbp2_command_info *sbp2util_allocate_command_orb(
689 struct scsi_id_instance_data *scsi_id,
690 struct scsi_cmnd *Current_SCpnt,
691 void (*Current_done)(struct scsi_cmnd *))
692{
693 struct list_head *lh;
694 struct sbp2_command_info *command = NULL;
695 unsigned long flags;
696
697 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
698 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
699 lh = scsi_id->sbp2_command_orb_completed.next;
700 list_del(lh);
701 command = list_entry(lh, struct sbp2_command_info, list);
702 command->Current_done = Current_done;
703 command->Current_SCpnt = Current_SCpnt;
704 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_inuse);
705 } else {
Stefan Richterd024ebc2006-03-28 20:03:55 -0500706 SBP2_ERR("%s: no orbs available", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
708 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Stefan Richtera237f352005-11-07 06:31:39 -0500709 return command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
712/* Free our DMA's */
713static void sbp2util_free_command_dma(struct sbp2_command_info *command)
714{
715 struct scsi_id_instance_data *scsi_id =
716 (struct scsi_id_instance_data *)command->Current_SCpnt->device->host->hostdata[0];
717 struct hpsb_host *host;
718
719 if (!scsi_id) {
Stefan Richterd024ebc2006-03-28 20:03:55 -0500720 SBP2_ERR("%s: scsi_id == NULL", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return;
722 }
723
724 host = scsi_id->ud->ne->host;
725
726 if (command->cmd_dma) {
727 if (command->dma_type == CMD_DMA_SINGLE) {
728 pci_unmap_single(host->pdev, command->cmd_dma,
729 command->dma_size, command->dma_dir);
730 SBP2_DMA_FREE("single bulk");
731 } else if (command->dma_type == CMD_DMA_PAGE) {
732 pci_unmap_page(host->pdev, command->cmd_dma,
733 command->dma_size, command->dma_dir);
734 SBP2_DMA_FREE("single page");
735 } /* XXX: Check for CMD_DMA_NONE bug */
736 command->dma_type = CMD_DMA_NONE;
737 command->cmd_dma = 0;
738 }
739
740 if (command->sge_buffer) {
741 pci_unmap_sg(host->pdev, command->sge_buffer,
742 command->dma_size, command->dma_dir);
743 SBP2_DMA_FREE("scatter list");
744 command->sge_buffer = NULL;
745 }
746}
747
748/*
749 * This function moves a command to the completed orb list.
Stefan Richter24c7cd02006-04-01 21:11:41 +0200750 * Must be called with scsi_id->sbp2_command_orb_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 */
Stefan Richter24c7cd02006-04-01 21:11:41 +0200752static void sbp2util_mark_command_completed(
753 struct scsi_id_instance_data *scsi_id,
754 struct sbp2_command_info *command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 list_del(&command->list);
757 sbp2util_free_command_dma(command);
758 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Jody McIntyreabd559b2005-09-30 11:59:06 -0700761/*
762 * Is scsi_id valid? Is the 1394 node still present?
763 */
764static inline int sbp2util_node_is_available(struct scsi_id_instance_data *scsi_id)
765{
766 return scsi_id && scsi_id->ne && !scsi_id->ne->in_limbo;
767}
768
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769/*********************************************
770 * IEEE-1394 core driver stack related section
771 *********************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
773static int sbp2_probe(struct device *dev)
774{
775 struct unit_directory *ud;
776 struct scsi_id_instance_data *scsi_id;
777
Stefan Richterd024ebc2006-03-28 20:03:55 -0500778 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 ud = container_of(dev, struct unit_directory, device);
781
782 /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
783 * instead. */
784 if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
785 return -ENODEV;
786
Stefan Richtera237f352005-11-07 06:31:39 -0500787 scsi_id = sbp2_alloc_device(ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Stefan Richtera237f352005-11-07 06:31:39 -0500789 if (!scsi_id)
790 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Stefan Richtera237f352005-11-07 06:31:39 -0500792 sbp2_parse_unit_directory(scsi_id, ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Stefan Richtera237f352005-11-07 06:31:39 -0500794 return sbp2_start_device(scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
797static int sbp2_remove(struct device *dev)
798{
799 struct unit_directory *ud;
800 struct scsi_id_instance_data *scsi_id;
Jody McIntyreabd559b2005-09-30 11:59:06 -0700801 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
Stefan Richterd024ebc2006-03-28 20:03:55 -0500803 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 ud = container_of(dev, struct unit_directory, device);
806 scsi_id = ud->device.driver_data;
Jody McIntyreabd559b2005-09-30 11:59:06 -0700807 if (!scsi_id)
808 return 0;
809
Stefan Richterbf637ec2006-01-31 00:13:06 -0500810 if (scsi_id->scsi_host) {
811 /* Get rid of enqueued commands if there is no chance to
812 * send them. */
813 if (!sbp2util_node_is_available(scsi_id))
814 sbp2scsi_complete_all_commands(scsi_id, DID_NO_CONNECT);
815 /* scsi_remove_device() will trigger shutdown functions of SCSI
816 * highlevel drivers which would deadlock if blocked. */
Stefan Richter2cccbb52006-08-14 18:59:00 +0200817 atomic_set(&scsi_id->state, SBP2LU_STATE_IN_SHUTDOWN);
Jody McIntyreabd559b2005-09-30 11:59:06 -0700818 scsi_unblock_requests(scsi_id->scsi_host);
Stefan Richterbf637ec2006-01-31 00:13:06 -0500819 }
Jody McIntyreabd559b2005-09-30 11:59:06 -0700820 sdev = scsi_id->sdev;
821 if (sdev) {
822 scsi_id->sdev = NULL;
823 scsi_remove_device(sdev);
824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 sbp2_logout_device(scsi_id);
827 sbp2_remove_device(scsi_id);
828
829 return 0;
830}
831
832static int sbp2_update(struct unit_directory *ud)
833{
834 struct scsi_id_instance_data *scsi_id = ud->device.driver_data;
835
Stefan Richterd024ebc2006-03-28 20:03:55 -0500836 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 if (sbp2_reconnect_device(scsi_id)) {
839
840 /*
841 * Ok, reconnect has failed. Perhaps we didn't
842 * reconnect fast enough. Try doing a regular login, but
843 * first do a logout just in case of any weirdness.
844 */
845 sbp2_logout_device(scsi_id);
846
847 if (sbp2_login_device(scsi_id)) {
848 /* Login failed too, just fail, and the backend
849 * will call our sbp2_remove for us */
850 SBP2_ERR("Failed to reconnect to sbp2 device!");
851 return -EBUSY;
852 }
853 }
854
855 /* Set max retries to something large on the device. */
856 sbp2_set_busy_timeout(scsi_id);
857
858 /* Do a SBP-2 fetch agent reset. */
859 sbp2_agent_reset(scsi_id, 1);
860
861 /* Get the max speed and packet size that we can use. */
862 sbp2_max_speed_and_size(scsi_id);
863
864 /* Complete any pending commands with busy (so they get
865 * retried) and remove them from our queue
866 */
867 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
868
Stefan Richter4fc383c2006-08-14 18:46:00 +0200869 /* Accept new commands unless there was another bus reset in the
870 * meantime. */
871 if (hpsb_node_entry_valid(scsi_id->ne)) {
Stefan Richter2cccbb52006-08-14 18:59:00 +0200872 atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
Stefan Richter4fc383c2006-08-14 18:46:00 +0200873 scsi_unblock_requests(scsi_id->scsi_host);
874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 return 0;
876}
877
878/* This functions is called by the sbp2_probe, for each new device. We now
879 * allocate one scsi host for each scsi_id (unit directory). */
880static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud)
881{
882 struct sbp2scsi_host_info *hi;
883 struct Scsi_Host *scsi_host = NULL;
884 struct scsi_id_instance_data *scsi_id = NULL;
885
Stefan Richterd024ebc2006-03-28 20:03:55 -0500886 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Stefan Richter85511582005-11-07 06:31:45 -0500888 scsi_id = kzalloc(sizeof(*scsi_id), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (!scsi_id) {
890 SBP2_ERR("failed to create scsi_id");
891 goto failed_alloc;
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 scsi_id->ne = ud->ne;
895 scsi_id->ud = ud;
896 scsi_id->speed_code = IEEE1394_SPEED_100;
897 scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
Ben Collins67372312006-06-12 18:15:31 -0400898 scsi_id->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_inuse);
900 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_completed);
901 INIT_LIST_HEAD(&scsi_id->scsi_list);
902 spin_lock_init(&scsi_id->sbp2_command_orb_lock);
Stefan Richter2cccbb52006-08-14 18:59:00 +0200903 atomic_set(&scsi_id->state, SBP2LU_STATE_RUNNING);
David Howellsc4028952006-11-22 14:57:56 +0000904 INIT_DELAYED_WORK(&scsi_id->protocol_work, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906 ud->device.driver_data = scsi_id;
907
908 hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
909 if (!hi) {
910 hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host, sizeof(*hi));
911 if (!hi) {
912 SBP2_ERR("failed to allocate hostinfo");
913 goto failed_alloc;
914 }
915 SBP2_DEBUG("sbp2_alloc_device: allocated hostinfo");
916 hi->host = ud->ne->host;
917 INIT_LIST_HEAD(&hi->scsi_ids);
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
920 /* Handle data movement if physical dma is not
Stefan Richter55664052006-03-28 19:59:42 -0500921 * enabled or not supported on host controller */
922 if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
923 &sbp2_physdma_ops,
924 0x0ULL, 0xfffffffcULL)) {
925 SBP2_ERR("failed to register lower 4GB address range");
926 goto failed_alloc;
927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928#endif
929 }
930
Stefan Richter147830f2006-03-28 19:54:52 -0500931 /* Prevent unloading of the 1394 host */
932 if (!try_module_get(hi->host->driver->owner)) {
933 SBP2_ERR("failed to get a reference on 1394 host driver");
934 goto failed_alloc;
935 }
936
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 scsi_id->hi = hi;
938
939 list_add_tail(&scsi_id->scsi_list, &hi->scsi_ids);
940
Stefan Richter35bdddb2006-01-31 00:13:33 -0500941 /* Register the status FIFO address range. We could use the same FIFO
942 * for targets at different nodes. However we need different FIFOs per
Stefan Richtera54c9d32006-05-15 22:09:46 +0200943 * target in order to support multi-unit devices.
944 * The FIFO is located out of the local host controller's physical range
945 * but, if possible, within the posted write area. Status writes will
946 * then be performed as unified transactions. This slightly reduces
947 * bandwidth usage, and some Prolific based devices seem to require it.
948 */
Stefan Richter35bdddb2006-01-31 00:13:33 -0500949 scsi_id->status_fifo_addr = hpsb_allocate_and_register_addrspace(
950 &sbp2_highlevel, ud->ne->host, &sbp2_ops,
951 sizeof(struct sbp2_status_block), sizeof(quadlet_t),
Ben Collins40ae6c52006-06-12 18:13:49 -0400952 ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
Ben Collins67372312006-06-12 18:15:31 -0400953 if (scsi_id->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
Stefan Richter35bdddb2006-01-31 00:13:33 -0500954 SBP2_ERR("failed to allocate status FIFO address range");
955 goto failed_alloc;
956 }
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 /* Register our host with the SCSI stack. */
Alexandre Olivaa2ef79e2005-06-15 22:26:31 -0700959 scsi_host = scsi_host_alloc(&scsi_driver_template,
Stefan Richtera237f352005-11-07 06:31:39 -0500960 sizeof(unsigned long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (!scsi_host) {
962 SBP2_ERR("failed to register scsi host");
963 goto failed_alloc;
964 }
965
966 scsi_host->hostdata[0] = (unsigned long)scsi_id;
967
968 if (!scsi_add_host(scsi_host, &ud->device)) {
969 scsi_id->scsi_host = scsi_host;
970 return scsi_id;
971 }
972
973 SBP2_ERR("failed to add scsi host");
974 scsi_host_put(scsi_host);
975
976failed_alloc:
977 sbp2_remove_device(scsi_id);
978 return NULL;
979}
980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981static void sbp2_host_reset(struct hpsb_host *host)
982{
983 struct sbp2scsi_host_info *hi;
984 struct scsi_id_instance_data *scsi_id;
985
986 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
Stefan Richter2cccbb52006-08-14 18:59:00 +0200987 if (!hi)
988 return;
989 list_for_each_entry(scsi_id, &hi->scsi_ids, scsi_list)
990 if (likely(atomic_read(&scsi_id->state) !=
991 SBP2LU_STATE_IN_SHUTDOWN)) {
992 atomic_set(&scsi_id->state, SBP2LU_STATE_IN_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 scsi_block_requests(scsi_id->scsi_host);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997/*
998 * This function is where we first pull the node unique ids, and then
999 * allocate memory and register a SBP-2 device.
1000 */
1001static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
1002{
1003 struct sbp2scsi_host_info *hi = scsi_id->hi;
James Bottomley146f7262005-09-10 12:44:09 -05001004 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005
Stefan Richterd024ebc2006-03-28 20:03:55 -05001006 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
1008 /* Login FIFO DMA */
1009 scsi_id->login_response =
Stefan Richtera237f352005-11-07 06:31:39 -05001010 pci_alloc_consistent(hi->host->pdev,
1011 sizeof(struct sbp2_login_response),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 &scsi_id->login_response_dma);
1013 if (!scsi_id->login_response)
1014 goto alloc_fail;
1015 SBP2_DMA_ALLOC("consistent DMA region for login FIFO");
1016
1017 /* Query logins ORB DMA */
1018 scsi_id->query_logins_orb =
Stefan Richtera237f352005-11-07 06:31:39 -05001019 pci_alloc_consistent(hi->host->pdev,
1020 sizeof(struct sbp2_query_logins_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 &scsi_id->query_logins_orb_dma);
1022 if (!scsi_id->query_logins_orb)
1023 goto alloc_fail;
1024 SBP2_DMA_ALLOC("consistent DMA region for query logins ORB");
1025
1026 /* Query logins response DMA */
1027 scsi_id->query_logins_response =
Stefan Richtera237f352005-11-07 06:31:39 -05001028 pci_alloc_consistent(hi->host->pdev,
1029 sizeof(struct sbp2_query_logins_response),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 &scsi_id->query_logins_response_dma);
1031 if (!scsi_id->query_logins_response)
1032 goto alloc_fail;
1033 SBP2_DMA_ALLOC("consistent DMA region for query logins response");
1034
1035 /* Reconnect ORB DMA */
1036 scsi_id->reconnect_orb =
Stefan Richtera237f352005-11-07 06:31:39 -05001037 pci_alloc_consistent(hi->host->pdev,
1038 sizeof(struct sbp2_reconnect_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 &scsi_id->reconnect_orb_dma);
1040 if (!scsi_id->reconnect_orb)
1041 goto alloc_fail;
1042 SBP2_DMA_ALLOC("consistent DMA region for reconnect ORB");
1043
1044 /* Logout ORB DMA */
1045 scsi_id->logout_orb =
Stefan Richtera237f352005-11-07 06:31:39 -05001046 pci_alloc_consistent(hi->host->pdev,
1047 sizeof(struct sbp2_logout_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 &scsi_id->logout_orb_dma);
1049 if (!scsi_id->logout_orb)
1050 goto alloc_fail;
1051 SBP2_DMA_ALLOC("consistent DMA region for logout ORB");
1052
1053 /* Login ORB DMA */
1054 scsi_id->login_orb =
Stefan Richtera237f352005-11-07 06:31:39 -05001055 pci_alloc_consistent(hi->host->pdev,
1056 sizeof(struct sbp2_login_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 &scsi_id->login_orb_dma);
Stefan Richtereaceec72005-12-13 11:05:05 -05001058 if (!scsi_id->login_orb)
1059 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 SBP2_DMA_ALLOC("consistent DMA region for login ORB");
1061
1062 SBP2_DEBUG("New SBP-2 device inserted, SCSI ID = %x", scsi_id->ud->id);
1063
1064 /*
1065 * Create our command orb pool
1066 */
1067 if (sbp2util_create_command_orb_pool(scsi_id)) {
1068 SBP2_ERR("sbp2util_create_command_orb_pool failed!");
1069 sbp2_remove_device(scsi_id);
1070 return -ENOMEM;
1071 }
1072
1073 /* Schedule a timeout here. The reason is that we may be so close
1074 * to a bus reset, that the device is not available for logins.
1075 * This can happen when the bus reset is caused by the host
1076 * connected to the sbp2 device being removed. That host would
1077 * have a certain amount of time to relogin before the sbp2 device
1078 * allows someone else to login instead. One second makes sense. */
Stefan Richter902abed2006-08-14 18:56:00 +02001079 if (msleep_interruptible(1000)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 sbp2_remove_device(scsi_id);
1081 return -EINTR;
1082 }
Stefan Richtera237f352005-11-07 06:31:39 -05001083
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 /*
1085 * Login to the sbp-2 device
1086 */
1087 if (sbp2_login_device(scsi_id)) {
1088 /* Login failed, just remove the device. */
1089 sbp2_remove_device(scsi_id);
1090 return -EBUSY;
1091 }
1092
1093 /*
1094 * Set max retries to something large on the device
1095 */
1096 sbp2_set_busy_timeout(scsi_id);
1097
1098 /*
1099 * Do a SBP-2 fetch agent reset
1100 */
1101 sbp2_agent_reset(scsi_id, 1);
1102
1103 /*
1104 * Get the max speed and packet size that we can use
1105 */
1106 sbp2_max_speed_and_size(scsi_id);
1107
1108 /* Add this device to the scsi layer now */
James Bottomley146f7262005-09-10 12:44:09 -05001109 error = scsi_add_device(scsi_id->scsi_host, 0, scsi_id->ud->id, 0);
1110 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 SBP2_ERR("scsi_add_device failed");
Stefan Richterdc3edd52005-12-12 23:03:30 -05001112 sbp2_logout_device(scsi_id);
1113 sbp2_remove_device(scsi_id);
James Bottomley146f7262005-09-10 12:44:09 -05001114 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116
1117 return 0;
Stefan Richtereaceec72005-12-13 11:05:05 -05001118
1119alloc_fail:
1120 SBP2_ERR("Could not allocate memory for scsi_id");
1121 sbp2_remove_device(scsi_id);
1122 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123}
1124
1125/*
1126 * This function removes an sbp2 device from the sbp2scsi_host_info struct.
1127 */
1128static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
1129{
1130 struct sbp2scsi_host_info *hi;
1131
Stefan Richterd024ebc2006-03-28 20:03:55 -05001132 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 if (!scsi_id)
1135 return;
1136
1137 hi = scsi_id->hi;
1138
1139 /* This will remove our scsi device aswell */
1140 if (scsi_id->scsi_host) {
1141 scsi_remove_host(scsi_id->scsi_host);
1142 scsi_host_put(scsi_id->scsi_host);
1143 }
Stefan Richter09ee67a2006-08-14 18:43:00 +02001144 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 sbp2util_remove_command_orb_pool(scsi_id);
1146
1147 list_del(&scsi_id->scsi_list);
1148
1149 if (scsi_id->login_response) {
1150 pci_free_consistent(hi->host->pdev,
1151 sizeof(struct sbp2_login_response),
1152 scsi_id->login_response,
1153 scsi_id->login_response_dma);
1154 SBP2_DMA_FREE("single login FIFO");
1155 }
1156
1157 if (scsi_id->login_orb) {
1158 pci_free_consistent(hi->host->pdev,
1159 sizeof(struct sbp2_login_orb),
1160 scsi_id->login_orb,
1161 scsi_id->login_orb_dma);
1162 SBP2_DMA_FREE("single login ORB");
1163 }
1164
1165 if (scsi_id->reconnect_orb) {
1166 pci_free_consistent(hi->host->pdev,
1167 sizeof(struct sbp2_reconnect_orb),
1168 scsi_id->reconnect_orb,
1169 scsi_id->reconnect_orb_dma);
1170 SBP2_DMA_FREE("single reconnect orb");
1171 }
1172
1173 if (scsi_id->logout_orb) {
1174 pci_free_consistent(hi->host->pdev,
1175 sizeof(struct sbp2_logout_orb),
1176 scsi_id->logout_orb,
1177 scsi_id->logout_orb_dma);
1178 SBP2_DMA_FREE("single logout orb");
1179 }
1180
1181 if (scsi_id->query_logins_orb) {
1182 pci_free_consistent(hi->host->pdev,
1183 sizeof(struct sbp2_query_logins_orb),
1184 scsi_id->query_logins_orb,
1185 scsi_id->query_logins_orb_dma);
1186 SBP2_DMA_FREE("single query logins orb");
1187 }
1188
1189 if (scsi_id->query_logins_response) {
1190 pci_free_consistent(hi->host->pdev,
1191 sizeof(struct sbp2_query_logins_response),
1192 scsi_id->query_logins_response,
1193 scsi_id->query_logins_response_dma);
1194 SBP2_DMA_FREE("single query logins data");
1195 }
1196
Ben Collins67372312006-06-12 18:15:31 -04001197 if (scsi_id->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
Stefan Richter35bdddb2006-01-31 00:13:33 -05001198 hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
Ben Collins67372312006-06-12 18:15:31 -04001199 scsi_id->status_fifo_addr);
Stefan Richter35bdddb2006-01-31 00:13:33 -05001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 scsi_id->ud->device.driver_data = NULL;
1202
Stefan Richter147830f2006-03-28 19:54:52 -05001203 if (hi)
1204 module_put(hi->host->driver->owner);
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 SBP2_DEBUG("SBP-2 device removed, SCSI ID = %d", scsi_id->ud->id);
1207
1208 kfree(scsi_id);
1209}
1210
1211#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
1212/*
1213 * This function deals with physical dma write requests (for adapters that do not support
1214 * physical dma in hardware). Mostly just here for debugging...
1215 */
Stefan Richtera237f352005-11-07 06:31:39 -05001216static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
1217 int destid, quadlet_t *data, u64 addr,
1218 size_t length, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220
Stefan Richtera237f352005-11-07 06:31:39 -05001221 /*
1222 * Manually put the data in the right place.
1223 */
1224 memcpy(bus_to_virt((u32) addr), data, length);
1225 sbp2util_packet_dump(data, length, "sbp2 phys dma write by device",
1226 (u32) addr);
1227 return RCODE_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
1230/*
1231 * This function deals with physical dma read requests (for adapters that do not support
1232 * physical dma in hardware). Mostly just here for debugging...
1233 */
Stefan Richtera237f352005-11-07 06:31:39 -05001234static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
1235 quadlet_t *data, u64 addr, size_t length,
1236 u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237{
1238
Stefan Richtera237f352005-11-07 06:31:39 -05001239 /*
1240 * Grab data from memory and send a read response.
1241 */
1242 memcpy(data, bus_to_virt((u32) addr), length);
1243 sbp2util_packet_dump(data, length, "sbp2 phys dma read by device",
1244 (u32) addr);
1245 return RCODE_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246}
1247#endif
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249/**************************************
1250 * SBP-2 protocol related section
1251 **************************************/
1252
1253/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 * This function queries the device for the maximum concurrent logins it
1255 * supports.
1256 */
1257static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
1258{
1259 struct sbp2scsi_host_info *hi = scsi_id->hi;
1260 quadlet_t data[2];
1261 int max_logins;
1262 int active_logins;
1263
Stefan Richterd024ebc2006-03-28 20:03:55 -05001264 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 scsi_id->query_logins_orb->reserved1 = 0x0;
1267 scsi_id->query_logins_orb->reserved2 = 0x0;
1268
1269 scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma;
1270 scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272 scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
1273 scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
Ben Collinse309fc62005-11-07 06:31:34 -05001274 scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 scsi_id->query_logins_orb->reserved_resp_length =
1277 ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Stefan Richter35bdddb2006-01-31 00:13:33 -05001279 scsi_id->query_logins_orb->status_fifo_hi =
1280 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1281 scsi_id->query_logins_orb->status_fifo_lo =
1282 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb));
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 sbp2util_packet_dump(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb),
1287 "sbp2 query logins orb", scsi_id->query_logins_orb_dma);
1288
1289 memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1292 data[1] = scsi_id->query_logins_orb_dma;
1293 sbp2util_cpu_to_be32_buffer(data, 8);
1294
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Stefan Richtere8398bb2006-07-23 22:19:00 +02001297 if (sbp2util_access_timeout(scsi_id, 2*HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001299 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 }
1301
1302 if (scsi_id->status_block.ORB_offset_lo != scsi_id->query_logins_orb_dma) {
1303 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001304 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 }
1306
Stefan Richter60657722006-07-23 22:18:00 +02001307 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1308 SBP2_INFO("Error querying logins to SBP-2 device - failed");
Stefan Richtera237f352005-11-07 06:31:39 -05001309 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 }
1311
1312 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_response, sizeof(struct sbp2_query_logins_response));
1313
1314 SBP2_DEBUG("length_max_logins = %x",
1315 (unsigned int)scsi_id->query_logins_response->length_max_logins);
1316
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins);
Ben Collins20f45782006-06-12 18:13:11 -04001318 SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319
1320 active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins);
Ben Collins20f45782006-06-12 18:13:11 -04001321 SBP2_INFO("Number of active logins: %d", active_logins);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 if (active_logins >= max_logins) {
Stefan Richtera237f352005-11-07 06:31:39 -05001324 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326
1327 return 0;
1328}
1329
1330/*
1331 * This function is called in order to login to a particular SBP-2 device,
1332 * after a bus reset.
1333 */
1334static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
1335{
1336 struct sbp2scsi_host_info *hi = scsi_id->hi;
1337 quadlet_t data[2];
1338
Stefan Richterd024ebc2006-03-28 20:03:55 -05001339 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
1341 if (!scsi_id->login_orb) {
Stefan Richterd024ebc2006-03-28 20:03:55 -05001342 SBP2_DEBUG("%s: login_orb not alloc'd!", __FUNCTION__);
Stefan Richtera237f352005-11-07 06:31:39 -05001343 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345
1346 if (!exclusive_login) {
1347 if (sbp2_query_logins(scsi_id)) {
1348 SBP2_INFO("Device does not support any more concurrent logins");
Stefan Richtera237f352005-11-07 06:31:39 -05001349 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
1351 }
1352
1353 /* Set-up login ORB, assume no password */
1354 scsi_id->login_orb->password_hi = 0;
1355 scsi_id->login_orb->password_lo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
1358 scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
1360 scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
1361 scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0); /* One second reconnect time */
1362 scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(exclusive_login); /* Exclusive access to device */
1363 scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1); /* Notify us of login complete */
Ben Collinse309fc62005-11-07 06:31:34 -05001364 scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 scsi_id->login_orb->passwd_resp_lengths =
1367 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Stefan Richter35bdddb2006-01-31 00:13:33 -05001369 scsi_id->login_orb->status_fifo_hi =
1370 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1371 scsi_id->login_orb->status_fifo_lo =
1372 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 sbp2util_packet_dump(scsi_id->login_orb, sizeof(struct sbp2_login_orb),
1377 "sbp2 login orb", scsi_id->login_orb_dma);
1378
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1382 data[1] = scsi_id->login_orb_dma;
1383 sbp2util_cpu_to_be32_buffer(data, 8);
1384
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 /*
1388 * Wait for login status (up to 20 seconds)...
1389 */
Stefan Richtere8398bb2006-07-23 22:19:00 +02001390 if (sbp2util_access_timeout(scsi_id, 20*HZ)) {
1391 SBP2_ERR("Error logging into SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001392 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 }
1394
1395 /*
1396 * Sanity. Make sure status returned matches login orb.
1397 */
1398 if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
Stefan Richter60657722006-07-23 22:18:00 +02001399 SBP2_ERR("Error logging into SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001400 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 }
1402
Stefan Richter60657722006-07-23 22:18:00 +02001403 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1404 SBP2_ERR("Error logging into SBP-2 device - failed");
Stefan Richtera237f352005-11-07 06:31:39 -05001405 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 }
1407
1408 /*
1409 * Byte swap the login response, for use when reconnecting or
1410 * logging out.
1411 */
1412 sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response));
1413
1414 /*
1415 * Grab our command block agent address from the login response.
1416 */
1417 SBP2_DEBUG("command_block_agent_hi = %x",
1418 (unsigned int)scsi_id->login_response->command_block_agent_hi);
1419 SBP2_DEBUG("command_block_agent_lo = %x",
1420 (unsigned int)scsi_id->login_response->command_block_agent_lo);
1421
1422 scsi_id->sbp2_command_block_agent_addr =
1423 ((u64)scsi_id->login_response->command_block_agent_hi) << 32;
1424 scsi_id->sbp2_command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
1425 scsi_id->sbp2_command_block_agent_addr &= 0x0000ffffffffffffULL;
1426
1427 SBP2_INFO("Logged into SBP-2 device");
Stefan Richtera237f352005-11-07 06:31:39 -05001428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
1431/*
1432 * This function is called in order to logout from a particular SBP-2
1433 * device, usually called during driver unload.
1434 */
1435static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
1436{
1437 struct sbp2scsi_host_info *hi = scsi_id->hi;
1438 quadlet_t data[2];
1439 int error;
1440
Stefan Richterd024ebc2006-03-28 20:03:55 -05001441 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 /*
1444 * Set-up logout ORB
1445 */
1446 scsi_id->logout_orb->reserved1 = 0x0;
1447 scsi_id->logout_orb->reserved2 = 0x0;
1448 scsi_id->logout_orb->reserved3 = 0x0;
1449 scsi_id->logout_orb->reserved4 = 0x0;
1450
1451 scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
1452 scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1453
1454 /* Notify us when complete */
1455 scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1456
1457 scsi_id->logout_orb->reserved5 = 0x0;
Stefan Richter35bdddb2006-01-31 00:13:33 -05001458 scsi_id->logout_orb->status_fifo_hi =
1459 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1460 scsi_id->logout_orb->status_fifo_lo =
1461 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 /*
1464 * Byte swap ORB if necessary
1465 */
1466 sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb));
1467
1468 sbp2util_packet_dump(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb),
1469 "sbp2 logout orb", scsi_id->logout_orb_dma);
1470
1471 /*
1472 * Ok, let's write to the target's management agent register
1473 */
1474 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1475 data[1] = scsi_id->logout_orb_dma;
1476 sbp2util_cpu_to_be32_buffer(data, 8);
1477
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 error = hpsb_node_write(scsi_id->ne,
Stefan Richtera237f352005-11-07 06:31:39 -05001479 scsi_id->sbp2_management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 if (error)
1481 return error;
1482
1483 /* Wait for device to logout...1 second. */
Stefan Richtere8398bb2006-07-23 22:19:00 +02001484 if (sbp2util_access_timeout(scsi_id, HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return -EIO;
1486
1487 SBP2_INFO("Logged out of SBP-2 device");
Stefan Richtera237f352005-11-07 06:31:39 -05001488 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489}
1490
1491/*
1492 * This function is called in order to reconnect to a particular SBP-2
1493 * device, after a bus reset.
1494 */
1495static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
1496{
1497 struct sbp2scsi_host_info *hi = scsi_id->hi;
1498 quadlet_t data[2];
1499 int error;
1500
Stefan Richterd024ebc2006-03-28 20:03:55 -05001501 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 /*
1504 * Set-up reconnect ORB
1505 */
1506 scsi_id->reconnect_orb->reserved1 = 0x0;
1507 scsi_id->reconnect_orb->reserved2 = 0x0;
1508 scsi_id->reconnect_orb->reserved3 = 0x0;
1509 scsi_id->reconnect_orb->reserved4 = 0x0;
1510
1511 scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
1512 scsi_id->reconnect_orb->login_ID_misc |=
1513 ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1514
1515 /* Notify us when complete */
1516 scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1517
1518 scsi_id->reconnect_orb->reserved5 = 0x0;
Stefan Richter35bdddb2006-01-31 00:13:33 -05001519 scsi_id->reconnect_orb->status_fifo_hi =
1520 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1521 scsi_id->reconnect_orb->status_fifo_lo =
1522 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524 /*
1525 * Byte swap ORB if necessary
1526 */
1527 sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
1528
1529 sbp2util_packet_dump(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb),
1530 "sbp2 reconnect orb", scsi_id->reconnect_orb_dma);
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1533 data[1] = scsi_id->reconnect_orb_dma;
1534 sbp2util_cpu_to_be32_buffer(data, 8);
1535
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 error = hpsb_node_write(scsi_id->ne,
Stefan Richtera237f352005-11-07 06:31:39 -05001537 scsi_id->sbp2_management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 if (error)
1539 return error;
1540
1541 /*
1542 * Wait for reconnect status (up to 1 second)...
1543 */
Stefan Richtere8398bb2006-07-23 22:19:00 +02001544 if (sbp2util_access_timeout(scsi_id, HZ)) {
1545 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001546 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
1548
1549 /*
1550 * Sanity. Make sure status returned matches reconnect orb.
1551 */
1552 if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
Stefan Richter60657722006-07-23 22:18:00 +02001553 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001554 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556
Stefan Richter60657722006-07-23 22:18:00 +02001557 if (STATUS_TEST_RDS(scsi_id->status_block.ORB_offset_hi_misc)) {
1558 SBP2_ERR("Error reconnecting to SBP-2 device - failed");
Stefan Richtera237f352005-11-07 06:31:39 -05001559 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 }
1561
1562 HPSB_DEBUG("Reconnected to SBP-2 device");
Stefan Richtera237f352005-11-07 06:31:39 -05001563 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564}
1565
1566/*
1567 * This function is called in order to set the busy timeout (number of
1568 * retries to attempt) on the sbp2 device.
1569 */
1570static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id)
1571{
1572 quadlet_t data;
1573
Stefan Richterd024ebc2006-03-28 20:03:55 -05001574 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
Stefan Richterd024ebc2006-03-28 20:03:55 -05001577 if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
1578 SBP2_ERR("%s error", __FUNCTION__);
Stefan Richtera237f352005-11-07 06:31:39 -05001579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580}
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582/*
1583 * This function is called to parse sbp2 device's config rom unit
1584 * directory. Used to determine things like sbp2 management agent offset,
1585 * and command set used (SCSI or RBC).
1586 */
1587static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
1588 struct unit_directory *ud)
1589{
1590 struct csr1212_keyval *kv;
1591 struct csr1212_dentry *dentry;
1592 u64 management_agent_addr;
1593 u32 command_set_spec_id, command_set, unit_characteristics,
Stefan Richter24d3bf82006-05-15 22:04:59 +02001594 firmware_revision;
1595 unsigned workarounds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 int i;
1597
Stefan Richterd024ebc2006-03-28 20:03:55 -05001598 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 management_agent_addr = 0x0;
1601 command_set_spec_id = 0x0;
1602 command_set = 0x0;
1603 unit_characteristics = 0x0;
1604 firmware_revision = 0x0;
1605
1606 /* Handle different fields in the unit directory, based on keys */
1607 csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
1608 switch (kv->key.id) {
1609 case CSR1212_KV_ID_DEPENDENT_INFO:
1610 if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET) {
1611 /* Save off the management agent address */
1612 management_agent_addr =
Stefan Richtera237f352005-11-07 06:31:39 -05001613 CSR1212_REGISTER_SPACE_BASE +
1614 (kv->value.csr_offset << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
1616 SBP2_DEBUG("sbp2_management_agent_addr = %x",
Stefan Richtera237f352005-11-07 06:31:39 -05001617 (unsigned int)management_agent_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 } else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
Stefan Richtera237f352005-11-07 06:31:39 -05001619 scsi_id->sbp2_lun =
1620 ORB_SET_LUN(kv->value.immediate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 }
1622 break;
1623
1624 case SBP2_COMMAND_SET_SPEC_ID_KEY:
1625 /* Command spec organization */
1626 command_set_spec_id = kv->value.immediate;
1627 SBP2_DEBUG("sbp2_command_set_spec_id = %x",
Stefan Richtera237f352005-11-07 06:31:39 -05001628 (unsigned int)command_set_spec_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 break;
1630
1631 case SBP2_COMMAND_SET_KEY:
1632 /* Command set used by sbp2 device */
1633 command_set = kv->value.immediate;
1634 SBP2_DEBUG("sbp2_command_set = %x",
Stefan Richtera237f352005-11-07 06:31:39 -05001635 (unsigned int)command_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 break;
1637
1638 case SBP2_UNIT_CHARACTERISTICS_KEY:
1639 /*
1640 * Unit characterisitcs (orb related stuff
1641 * that I'm not yet paying attention to)
1642 */
1643 unit_characteristics = kv->value.immediate;
1644 SBP2_DEBUG("sbp2_unit_characteristics = %x",
Stefan Richtera237f352005-11-07 06:31:39 -05001645 (unsigned int)unit_characteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 break;
1647
1648 case SBP2_FIRMWARE_REVISION_KEY:
1649 /* Firmware revision */
1650 firmware_revision = kv->value.immediate;
Stefan Richter24d3bf82006-05-15 22:04:59 +02001651 SBP2_DEBUG("sbp2_firmware_revision = %x",
1652 (unsigned int)firmware_revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 break;
1654
1655 default:
1656 break;
1657 }
1658 }
1659
Stefan Richter24d3bf82006-05-15 22:04:59 +02001660 workarounds = sbp2_default_workarounds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Stefan Richter679c0cd2006-05-15 22:08:09 +02001662 if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
1663 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
1664 if (sbp2_workarounds_table[i].firmware_revision &&
1665 sbp2_workarounds_table[i].firmware_revision !=
1666 (firmware_revision & 0xffff00))
1667 continue;
1668 if (sbp2_workarounds_table[i].model_id &&
1669 sbp2_workarounds_table[i].model_id != ud->model_id)
1670 continue;
1671 workarounds |= sbp2_workarounds_table[i].workarounds;
1672 break;
1673 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Stefan Richter24d3bf82006-05-15 22:04:59 +02001675 if (workarounds)
Stefan Richtere9a1c522006-05-15 22:06:37 +02001676 SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
1677 "(firmware_revision 0x%06x, vendor_id 0x%06x,"
1678 " model_id 0x%06x)",
Stefan Richter24d3bf82006-05-15 22:04:59 +02001679 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
Stefan Richtere9a1c522006-05-15 22:06:37 +02001680 workarounds, firmware_revision,
1681 ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
1682 ud->model_id);
Stefan Richter24d3bf82006-05-15 22:04:59 +02001683
1684 /* We would need one SCSI host template for each target to adjust
1685 * max_sectors on the fly, therefore warn only. */
1686 if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
1687 (max_sectors * 512) > (128 * 1024))
1688 SBP2_WARN("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
1689 "max transfer size. WARNING: Current max_sectors "
1690 "setting is larger than 128KB (%d sectors)",
1691 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
1692 max_sectors);
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 /* If this is a logical unit directory entry, process the parent
1695 * to get the values. */
1696 if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
1697 struct unit_directory *parent_ud =
1698 container_of(ud->device.parent, struct unit_directory, device);
1699 sbp2_parse_unit_directory(scsi_id, parent_ud);
1700 } else {
1701 scsi_id->sbp2_management_agent_addr = management_agent_addr;
1702 scsi_id->sbp2_command_set_spec_id = command_set_spec_id;
1703 scsi_id->sbp2_command_set = command_set;
1704 scsi_id->sbp2_unit_characteristics = unit_characteristics;
1705 scsi_id->sbp2_firmware_revision = firmware_revision;
1706 scsi_id->workarounds = workarounds;
1707 if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
Ben Collinse309fc62005-11-07 06:31:34 -05001708 scsi_id->sbp2_lun = ORB_SET_LUN(ud->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710}
1711
Ben Collinsfd23ade2006-06-12 18:14:14 -04001712#define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714/*
1715 * This function is called in order to determine the max speed and packet
1716 * size we can use in our ORBs. Note, that we (the driver and host) only
1717 * initiate the transaction. The SBP-2 device actually transfers the data
1718 * (by reading from the DMA area we tell it). This means that the SBP-2
1719 * device decides the actual maximum data it can transfer. We just tell it
1720 * the speed that it needs to use, and the max_rec the host supports, and
1721 * it takes care of the rest.
1722 */
1723static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id)
1724{
1725 struct sbp2scsi_host_info *hi = scsi_id->hi;
Ben Collinsfd23ade2006-06-12 18:14:14 -04001726 u8 payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
Stefan Richterd024ebc2006-03-28 20:03:55 -05001728 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Stefan Richtera237f352005-11-07 06:31:39 -05001730 scsi_id->speed_code =
Ben Collins647dcb52006-06-12 18:12:37 -04001731 hi->host->speed[NODEID_TO_NODE(scsi_id->ne->nodeid)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 /* Bump down our speed if the user requested it */
1734 if (scsi_id->speed_code > max_speed) {
1735 scsi_id->speed_code = max_speed;
1736 SBP2_ERR("Forcing SBP-2 max speed down to %s",
1737 hpsb_speedto_str[scsi_id->speed_code]);
1738 }
1739
1740 /* Payload size is the lesser of what our speed supports and what
1741 * our host supports. */
Ben Collinsfd23ade2006-06-12 18:14:14 -04001742 payload = min(sbp2_speedto_max_payload[scsi_id->speed_code],
1743 (u8) (hi->host->csr.max_rec - 1));
1744
1745 /* If physical DMA is off, work around limitation in ohci1394:
1746 * packet size must not exceed PAGE_SIZE */
1747 if (scsi_id->ne->host->low_addr_space < (1ULL << 32))
1748 while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
1749 payload)
1750 payload--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752 HPSB_DEBUG("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
1753 NODE_BUS_ARGS(hi->host, scsi_id->ne->nodeid),
1754 hpsb_speedto_str[scsi_id->speed_code],
Ben Collinsfd23ade2006-06-12 18:14:14 -04001755 SBP2_PAYLOAD_TO_BYTES(payload));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Ben Collinsfd23ade2006-06-12 18:14:14 -04001757 scsi_id->max_payload_size = payload;
Stefan Richtera237f352005-11-07 06:31:39 -05001758 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
1761/*
1762 * This function is called in order to perform a SBP-2 agent reset.
1763 */
1764static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait)
1765{
1766 quadlet_t data;
1767 u64 addr;
1768 int retval;
Stefan Richtercc078182006-07-23 22:12:00 +02001769 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Stefan Richterd024ebc2006-03-28 20:03:55 -05001771 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Stefan Richter09ee67a2006-08-14 18:43:00 +02001773 cancel_delayed_work(&scsi_id->protocol_work);
1774 if (wait)
1775 flush_scheduled_work();
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 data = ntohl(SBP2_AGENT_RESET_DATA);
1778 addr = scsi_id->sbp2_command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
1779
1780 if (wait)
1781 retval = hpsb_node_write(scsi_id->ne, addr, &data, 4);
1782 else
1783 retval = sbp2util_node_write_no_wait(scsi_id->ne, addr, &data, 4);
1784
1785 if (retval < 0) {
1786 SBP2_ERR("hpsb_node_write failed.\n");
1787 return -EIO;
1788 }
1789
1790 /*
1791 * Need to make sure orb pointer is written on next command
1792 */
Stefan Richtercc078182006-07-23 22:12:00 +02001793 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 scsi_id->last_orb = NULL;
Stefan Richtercc078182006-07-23 22:12:00 +02001795 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
Stefan Richtera237f352005-11-07 06:31:39 -05001797 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798}
1799
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001800static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
1801 struct sbp2scsi_host_info *hi,
1802 struct sbp2_command_info *command,
1803 unsigned int scsi_use_sg,
1804 struct scatterlist *sgpnt,
1805 u32 orb_direction,
1806 enum dma_data_direction dma_dir)
1807{
1808 command->dma_dir = dma_dir;
1809 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1810 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1811
1812 /* Special case if only one element (and less than 64KB in size) */
1813 if ((scsi_use_sg == 1) &&
1814 (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
1815
1816 SBP2_DEBUG("Only one s/g element");
1817 command->dma_size = sgpnt[0].length;
1818 command->dma_type = CMD_DMA_PAGE;
1819 command->cmd_dma = pci_map_page(hi->host->pdev,
1820 sgpnt[0].page,
1821 sgpnt[0].offset,
1822 command->dma_size,
1823 command->dma_dir);
1824 SBP2_DMA_ALLOC("single page scatter element");
1825
1826 orb->data_descriptor_lo = command->cmd_dma;
1827 orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
1828
1829 } else {
1830 struct sbp2_unrestricted_page_table *sg_element =
1831 &command->scatter_gather_element[0];
1832 u32 sg_count, sg_len;
1833 dma_addr_t sg_addr;
1834 int i, count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg,
1835 dma_dir);
1836
1837 SBP2_DMA_ALLOC("scatter list");
1838
1839 command->dma_size = scsi_use_sg;
1840 command->sge_buffer = sgpnt;
1841
1842 /* use page tables (s/g) */
1843 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1844 orb->data_descriptor_lo = command->sge_dma;
1845
1846 /*
1847 * Loop through and fill out our sbp-2 page tables
1848 * (and split up anything too large)
1849 */
1850 for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
1851 sg_len = sg_dma_len(sgpnt);
1852 sg_addr = sg_dma_address(sgpnt);
1853 while (sg_len) {
1854 sg_element[sg_count].segment_base_lo = sg_addr;
1855 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1856 sg_element[sg_count].length_segment_base_hi =
1857 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1858 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1859 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1860 } else {
1861 sg_element[sg_count].length_segment_base_hi =
1862 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1863 sg_len = 0;
1864 }
1865 sg_count++;
1866 }
1867 }
1868
1869 /* Number of page table (s/g) elements */
1870 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1871
1872 sbp2util_packet_dump(sg_element,
1873 (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
1874 "sbp2 s/g list", command->sge_dma);
1875
1876 /* Byte swap page tables if necessary */
1877 sbp2util_cpu_to_be32_buffer(sg_element,
1878 (sizeof(struct sbp2_unrestricted_page_table)) *
1879 sg_count);
1880 }
1881}
1882
1883static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
1884 struct sbp2scsi_host_info *hi,
1885 struct sbp2_command_info *command,
1886 struct scatterlist *sgpnt,
1887 u32 orb_direction,
1888 unsigned int scsi_request_bufflen,
1889 void *scsi_request_buffer,
1890 enum dma_data_direction dma_dir)
1891{
1892 command->dma_dir = dma_dir;
1893 command->dma_size = scsi_request_bufflen;
1894 command->dma_type = CMD_DMA_SINGLE;
1895 command->cmd_dma = pci_map_single(hi->host->pdev, scsi_request_buffer,
1896 command->dma_size, command->dma_dir);
1897 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1898 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1899
1900 SBP2_DMA_ALLOC("single bulk");
1901
1902 /*
1903 * Handle case where we get a command w/o s/g enabled (but
1904 * check for transfers larger than 64K)
1905 */
1906 if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1907
1908 orb->data_descriptor_lo = command->cmd_dma;
1909 orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
1910
1911 } else {
1912 struct sbp2_unrestricted_page_table *sg_element =
1913 &command->scatter_gather_element[0];
1914 u32 sg_count, sg_len;
1915 dma_addr_t sg_addr;
1916
1917 /*
1918 * Need to turn this into page tables, since the
1919 * buffer is too large.
1920 */
1921 orb->data_descriptor_lo = command->sge_dma;
1922
1923 /* Use page tables (s/g) */
1924 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1925
1926 /*
1927 * fill out our sbp-2 page tables (and split up
1928 * the large buffer)
1929 */
1930 sg_count = 0;
1931 sg_len = scsi_request_bufflen;
1932 sg_addr = command->cmd_dma;
1933 while (sg_len) {
1934 sg_element[sg_count].segment_base_lo = sg_addr;
1935 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1936 sg_element[sg_count].length_segment_base_hi =
1937 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1938 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1939 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1940 } else {
1941 sg_element[sg_count].length_segment_base_hi =
1942 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1943 sg_len = 0;
1944 }
1945 sg_count++;
1946 }
1947
1948 /* Number of page table (s/g) elements */
1949 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1950
1951 sbp2util_packet_dump(sg_element,
1952 (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
1953 "sbp2 s/g list", command->sge_dma);
1954
1955 /* Byte swap page tables if necessary */
1956 sbp2util_cpu_to_be32_buffer(sg_element,
1957 (sizeof(struct sbp2_unrestricted_page_table)) *
1958 sg_count);
1959 }
1960}
1961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962/*
1963 * This function is called to create the actual command orb and s/g list
1964 * out of the scsi command itself.
1965 */
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001966static void sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
1967 struct sbp2_command_info *command,
1968 unchar *scsi_cmd,
1969 unsigned int scsi_use_sg,
1970 unsigned int scsi_request_bufflen,
1971 void *scsi_request_buffer,
1972 enum dma_data_direction dma_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
1974 struct sbp2scsi_host_info *hi = scsi_id->hi;
Stefan Richtera237f352005-11-07 06:31:39 -05001975 struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 struct sbp2_command_orb *command_orb = &command->command_orb;
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001977 u32 orb_direction;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
1979 /*
1980 * Set-up our command ORB..
1981 *
1982 * NOTE: We're doing unrestricted page tables (s/g), as this is
1983 * best performance (at least with the devices I have). This means
1984 * that data_size becomes the number of s/g elements, and
1985 * page_size should be zero (for unrestricted).
1986 */
1987 command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1988 command_orb->next_ORB_lo = 0x0;
1989 command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size);
1990 command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code);
Stefan Richtera237f352005-11-07 06:31:39 -05001991 command_orb->misc |= ORB_SET_NOTIFY(1); /* Notify us when complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Stefan Richter43863eb2005-12-12 23:03:24 -05001993 if (dma_dir == DMA_NONE)
Stefan Richtera237f352005-11-07 06:31:39 -05001994 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
Stefan Richter43863eb2005-12-12 23:03:24 -05001995 else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
Stefan Richtera237f352005-11-07 06:31:39 -05001996 orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
Stefan Richter43863eb2005-12-12 23:03:24 -05001997 else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
Stefan Richtera237f352005-11-07 06:31:39 -05001998 orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
Stefan Richter43863eb2005-12-12 23:03:24 -05001999 else {
2000 SBP2_WARN("Falling back to DMA_NONE");
2001 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 }
2003
Stefan Richtercf8d2c02005-12-13 11:05:03 -05002004 /* Set-up our pagetable stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 SBP2_DEBUG("No data transfer");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 command_orb->data_descriptor_hi = 0x0;
2008 command_orb->data_descriptor_lo = 0x0;
2009 command_orb->misc |= ORB_SET_DIRECTION(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 } else if (scsi_use_sg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 SBP2_DEBUG("Use scatter/gather");
Stefan Richtercf8d2c02005-12-13 11:05:03 -05002012 sbp2_prep_command_orb_sg(command_orb, hi, command, scsi_use_sg,
2013 sgpnt, orb_direction, dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 SBP2_DEBUG("No scatter/gather");
Stefan Richtercf8d2c02005-12-13 11:05:03 -05002016 sbp2_prep_command_orb_no_sg(command_orb, hi, command, sgpnt,
2017 orb_direction, scsi_request_bufflen,
2018 scsi_request_buffer, dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
2020
Stefan Richtercf8d2c02005-12-13 11:05:03 -05002021 /* Byte swap command ORB if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
2023
Stefan Richtercf8d2c02005-12-13 11:05:03 -05002024 /* Put our scsi command in the command ORB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 memset(command_orb->cdb, 0, 12);
2026 memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027}
2028
2029/*
2030 * This function is called in order to begin a regular SBP-2 command.
2031 */
Stefan Richter28212762006-07-23 22:10:00 +02002032static void sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 struct sbp2_command_info *command)
2034{
2035 struct sbp2scsi_host_info *hi = scsi_id->hi;
2036 struct sbp2_command_orb *command_orb = &command->command_orb;
Stefan Richtercc078182006-07-23 22:12:00 +02002037 struct sbp2_command_orb *last_orb;
2038 dma_addr_t last_orb_dma;
2039 u64 addr = scsi_id->sbp2_command_block_agent_addr;
2040 quadlet_t data[2];
2041 size_t length;
2042 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043
2044 outstanding_orb_incr;
2045 SBP2_ORB_DEBUG("sending command orb %p, total orbs = %x",
Stefan Richtera237f352005-11-07 06:31:39 -05002046 command_orb, global_outstanding_command_orbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 pci_dma_sync_single_for_device(hi->host->pdev, command->command_orb_dma,
2049 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02002050 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 pci_dma_sync_single_for_device(hi->host->pdev, command->sge_dma,
2052 sizeof(command->scatter_gather_element),
2053 PCI_DMA_BIDIRECTIONAL);
2054 /*
2055 * Check to see if there are any previous orbs to use
2056 */
Stefan Richtercc078182006-07-23 22:12:00 +02002057 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
2058 last_orb = scsi_id->last_orb;
2059 last_orb_dma = scsi_id->last_orb_dma;
2060 if (!last_orb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 /*
Stefan Richtercc078182006-07-23 22:12:00 +02002062 * last_orb == NULL means: We know that the target's fetch agent
2063 * is not active right now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 */
Stefan Richtercc078182006-07-23 22:12:00 +02002065 addr += SBP2_ORB_POINTER_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
2067 data[1] = command->command_orb_dma;
2068 sbp2util_cpu_to_be32_buffer(data, 8);
Stefan Richtercc078182006-07-23 22:12:00 +02002069 length = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 /*
Stefan Richtercc078182006-07-23 22:12:00 +02002072 * last_orb != NULL means: We know that the target's fetch agent
2073 * is (very probably) not dead or in reset state right now.
2074 * We have an ORB already sent that we can append a new one to.
2075 * The target's fetch agent may or may not have read this
2076 * previous ORB yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 */
Stefan Richtercc078182006-07-23 22:12:00 +02002078 pci_dma_sync_single_for_cpu(hi->host->pdev, last_orb_dma,
2079 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02002080 PCI_DMA_TODEVICE);
Stefan Richtercc078182006-07-23 22:12:00 +02002081 last_orb->next_ORB_lo = cpu_to_be32(command->command_orb_dma);
2082 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 /* Tells hardware that this pointer is valid */
Stefan Richtercc078182006-07-23 22:12:00 +02002084 last_orb->next_ORB_hi = 0;
2085 pci_dma_sync_single_for_device(hi->host->pdev, last_orb_dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02002087 PCI_DMA_TODEVICE);
Stefan Richtercc078182006-07-23 22:12:00 +02002088 addr += SBP2_DOORBELL_OFFSET;
2089 data[0] = 0;
2090 length = 4;
2091 }
2092 scsi_id->last_orb = command_orb;
2093 scsi_id->last_orb_dma = command->command_orb_dma;
2094 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095
Stefan Richtercc078182006-07-23 22:12:00 +02002096 SBP2_ORB_DEBUG("write to %s register, command orb %p",
2097 last_orb ? "DOORBELL" : "ORB_POINTER", command_orb);
Stefan Richter09ee67a2006-08-14 18:43:00 +02002098 if (sbp2util_node_write_no_wait(scsi_id->ne, addr, data, length)) {
2099 /*
2100 * sbp2util_node_write_no_wait failed. We certainly ran out
2101 * of transaction labels, perhaps just because there were no
2102 * context switches which gave khpsbpkt a chance to collect
2103 * free tlabels. Try again in non-atomic context. If necessary,
2104 * the workqueue job will sleep to guaranteedly get a tlabel.
2105 * We do not accept new commands until the job is over.
2106 */
2107 scsi_block_requests(scsi_id->scsi_host);
David Howellsc4028952006-11-22 14:57:56 +00002108 PREPARE_DELAYED_WORK(&scsi_id->protocol_work,
Stefan Richter09ee67a2006-08-14 18:43:00 +02002109 last_orb ? sbp2util_write_doorbell:
David Howellsc4028952006-11-22 14:57:56 +00002110 sbp2util_write_orb_pointer);
2111 schedule_delayed_work(&scsi_id->protocol_work, 0);
Stefan Richter09ee67a2006-08-14 18:43:00 +02002112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113}
2114
2115/*
2116 * This function is called in order to begin a regular SBP-2 command.
2117 */
2118static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
2119 struct scsi_cmnd *SCpnt,
2120 void (*done)(struct scsi_cmnd *))
2121{
2122 unchar *cmd = (unchar *) SCpnt->cmnd;
2123 unsigned int request_bufflen = SCpnt->request_bufflen;
2124 struct sbp2_command_info *command;
2125
Stefan Richterd024ebc2006-03-28 20:03:55 -05002126 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 SBP2_DEBUG("SCSI transfer size = %x", request_bufflen);
2128 SBP2_DEBUG("SCSI s/g elements = %x", (unsigned int)SCpnt->use_sg);
2129
2130 /*
2131 * Allocate a command orb and s/g structure
2132 */
2133 command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done);
2134 if (!command) {
Stefan Richtera237f352005-11-07 06:31:39 -05002135 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 }
2137
2138 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 * Now actually fill in the comamnd orb and sbp2 s/g list
2140 */
2141 sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg,
2142 request_bufflen, SCpnt->request_buffer,
2143 SCpnt->sc_data_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145 sbp2util_packet_dump(&command->command_orb, sizeof(struct sbp2_command_orb),
2146 "sbp2 command orb", command->command_orb_dma);
2147
2148 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 * Link up the orb, and ring the doorbell if needed
2150 */
2151 sbp2_link_orb_command(scsi_id, command);
2152
Stefan Richtera237f352005-11-07 06:31:39 -05002153 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154}
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 * Translates SBP-2 status into SCSI sense data for check conditions
2158 */
2159static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
2160{
Stefan Richterd024ebc2006-03-28 20:03:55 -05002161 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
2163 /*
2164 * Ok, it's pretty ugly... ;-)
2165 */
2166 sense_data[0] = 0x70;
2167 sense_data[1] = 0x0;
2168 sense_data[2] = sbp2_status[9];
2169 sense_data[3] = sbp2_status[12];
2170 sense_data[4] = sbp2_status[13];
2171 sense_data[5] = sbp2_status[14];
2172 sense_data[6] = sbp2_status[15];
2173 sense_data[7] = 10;
2174 sense_data[8] = sbp2_status[16];
2175 sense_data[9] = sbp2_status[17];
2176 sense_data[10] = sbp2_status[18];
2177 sense_data[11] = sbp2_status[19];
2178 sense_data[12] = sbp2_status[10];
2179 sense_data[13] = sbp2_status[11];
2180 sense_data[14] = sbp2_status[20];
2181 sense_data[15] = sbp2_status[21];
2182
Stefan Richtera237f352005-11-07 06:31:39 -05002183 return sbp2_status[8] & 0x3f; /* return scsi status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184}
2185
2186/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 * This function deals with status writes from the SBP-2 device
2188 */
Stefan Richter3e98eab42006-07-23 22:16:00 +02002189static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
2190 int destid, quadlet_t *data, u64 addr,
2191 size_t length, u16 fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
2193 struct sbp2scsi_host_info *hi;
2194 struct scsi_id_instance_data *scsi_id = NULL, *scsi_id_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 struct scsi_cmnd *SCpnt = NULL;
Stefan Richter3e98eab42006-07-23 22:16:00 +02002196 struct sbp2_status_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
2198 struct sbp2_command_info *command;
Jody McIntyre79456192005-11-07 06:29:39 -05002199 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Stefan Richterd024ebc2006-03-28 20:03:55 -05002201 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
2203 sbp2util_packet_dump(data, length, "sbp2 status write by device", (u32)addr);
2204
Stefan Richter60657722006-07-23 22:18:00 +02002205 if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
2206 SBP2_ERR("Wrong size of status block");
2207 return RCODE_ADDRESS_ERROR;
2208 }
2209 if (unlikely(!host)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 SBP2_ERR("host is NULL - this is bad!");
Stefan Richtera237f352005-11-07 06:31:39 -05002211 return RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
Stefan Richter60657722006-07-23 22:18:00 +02002214 if (unlikely(!hi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 SBP2_ERR("host info is NULL - this is bad!");
Stefan Richtera237f352005-11-07 06:31:39 -05002216 return RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 /*
Stefan Richter35bdddb2006-01-31 00:13:33 -05002219 * Find our scsi_id structure by looking at the status fifo address
2220 * written to by the sbp2 device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 list_for_each_entry(scsi_id_tmp, &hi->scsi_ids, scsi_list) {
Stefan Richter35bdddb2006-01-31 00:13:33 -05002223 if (scsi_id_tmp->ne->nodeid == nodeid &&
2224 scsi_id_tmp->status_fifo_addr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 scsi_id = scsi_id_tmp;
2226 break;
2227 }
2228 }
Stefan Richter60657722006-07-23 22:18:00 +02002229 if (unlikely(!scsi_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 SBP2_ERR("scsi_id is NULL - device is gone?");
Stefan Richtera237f352005-11-07 06:31:39 -05002231 return RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 }
2233
2234 /*
Stefan Richter3e98eab42006-07-23 22:16:00 +02002235 * Put response into scsi_id status fifo buffer. The first two bytes
2236 * come in big endian bit order. Often the target writes only a
2237 * truncated status block, minimally the first two quadlets. The rest
2238 * is implied to be zeros.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 */
Stefan Richter3e98eab42006-07-23 22:16:00 +02002240 sb = &scsi_id->status_block;
2241 memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
2242 memcpy(sb, data, length);
2243 sbp2util_be32_to_cpu_buffer(sb, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
2245 /*
Stefan Richter60657722006-07-23 22:18:00 +02002246 * Ignore unsolicited status. Handle command ORB status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 */
Stefan Richter60657722006-07-23 22:18:00 +02002248 if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
2249 command = NULL;
2250 else
2251 command = sbp2util_find_command_for_orb(scsi_id,
2252 sb->ORB_offset_lo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 if (command) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 SBP2_DEBUG("Found status for command ORB");
2255 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
2256 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02002257 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
2259 sizeof(command->scatter_gather_element),
2260 PCI_DMA_BIDIRECTIONAL);
2261
2262 SBP2_ORB_DEBUG("matched command orb %p", &command->command_orb);
2263 outstanding_orb_decr;
2264
2265 /*
Stefan Richter3e98eab42006-07-23 22:16:00 +02002266 * Matched status with command, now grab scsi command pointers
2267 * and check status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 */
Stefan Richter60657722006-07-23 22:18:00 +02002269 /*
2270 * FIXME: If the src field in the status is 1, the ORB DMA must
2271 * not be reused until status for a subsequent ORB is received.
2272 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 SCpnt = command->Current_SCpnt;
Stefan Richter24c7cd02006-04-01 21:11:41 +02002274 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 sbp2util_mark_command_completed(scsi_id, command);
Stefan Richter24c7cd02006-04-01 21:11:41 +02002276 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 if (SCpnt) {
Stefan Richterabbca102006-08-14 18:51:00 +02002279 u32 h = sb->ORB_offset_hi_misc;
2280 u32 r = STATUS_GET_RESP(h);
2281
2282 if (r != RESP_STATUS_REQUEST_COMPLETE) {
2283 SBP2_WARN("resp 0x%x, sbp_status 0x%x",
2284 r, STATUS_GET_SBP_STATUS(h));
Stefan Richter60657722006-07-23 22:18:00 +02002285 scsi_status =
Stefan Richterabbca102006-08-14 18:51:00 +02002286 r == RESP_STATUS_TRANSPORT_FAILURE ?
2287 SBP2_SCSI_STATUS_BUSY :
Stefan Richter60657722006-07-23 22:18:00 +02002288 SBP2_SCSI_STATUS_COMMAND_TERMINATED;
Stefan Richterabbca102006-08-14 18:51:00 +02002289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 /*
Stefan Richter3e98eab42006-07-23 22:16:00 +02002291 * See if the target stored any scsi status information.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 */
Stefan Richterabbca102006-08-14 18:51:00 +02002293 if (STATUS_GET_LEN(h) > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 SBP2_DEBUG("CHECK CONDITION");
Stefan Richter3e98eab42006-07-23 22:16:00 +02002295 scsi_status = sbp2_status_to_sense_data(
2296 (unchar *)sb, SCpnt->sense_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 /*
Stefan Richter3e98eab42006-07-23 22:16:00 +02002299 * Check to see if the dead bit is set. If so, we'll
2300 * have to initiate a fetch agent reset.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 */
Stefan Richterabbca102006-08-14 18:51:00 +02002302 if (STATUS_TEST_DEAD(h)) {
Stefan Richter3e98eab42006-07-23 22:16:00 +02002303 SBP2_DEBUG("Dead bit set - "
2304 "initiating fetch agent reset");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 sbp2_agent_reset(scsi_id, 0);
2306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 SBP2_ORB_DEBUG("completing command orb %p", &command->command_orb);
2308 }
2309
2310 /*
Stefan Richtercc078182006-07-23 22:12:00 +02002311 * Check here to see if there are no commands in-use. If there
2312 * are none, we know that the fetch agent left the active state
2313 * _and_ that we did not reactivate it yet. Therefore clear
2314 * last_orb so that next time we write directly to the
2315 * ORB_POINTER register. That way the fetch agent does not need
2316 * to refetch the next_ORB.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 */
Jody McIntyre79456192005-11-07 06:29:39 -05002318 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Stefan Richtercc078182006-07-23 22:12:00 +02002319 if (list_empty(&scsi_id->sbp2_command_orb_inuse))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 scsi_id->last_orb = NULL;
Jody McIntyre79456192005-11-07 06:29:39 -05002321 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 /*
2325 * It's probably a login/logout/reconnect status.
2326 */
Stefan Richter3e98eab42006-07-23 22:16:00 +02002327 if ((sb->ORB_offset_lo == scsi_id->reconnect_orb_dma) ||
2328 (sb->ORB_offset_lo == scsi_id->login_orb_dma) ||
2329 (sb->ORB_offset_lo == scsi_id->query_logins_orb_dma) ||
Stefan Richtere8398bb2006-07-23 22:19:00 +02002330 (sb->ORB_offset_lo == scsi_id->logout_orb_dma)) {
2331 scsi_id->access_complete = 1;
2332 wake_up_interruptible(&access_wq);
2333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 }
2335
2336 if (SCpnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 SBP2_DEBUG("Completing SCSI command");
2338 sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt,
2339 command->Current_done);
2340 SBP2_ORB_DEBUG("command orb completed");
2341 }
2342
Stefan Richtera237f352005-11-07 06:31:39 -05002343 return RCODE_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344}
2345
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346/**************************************
2347 * SCSI interface related section
2348 **************************************/
2349
2350/*
2351 * This routine is the main request entry routine for doing I/O. It is
2352 * called from the scsi stack directly.
2353 */
2354static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
2355 void (*done)(struct scsi_cmnd *))
2356{
2357 struct scsi_id_instance_data *scsi_id =
2358 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2359 struct sbp2scsi_host_info *hi;
Jody McIntyreabd559b2005-09-30 11:59:06 -07002360 int result = DID_NO_CONNECT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Stefan Richterd024ebc2006-03-28 20:03:55 -05002362 SBP2_DEBUG_ENTER();
2363#if (CONFIG_IEEE1394_SBP2_DEBUG >= 2) || defined(CONFIG_IEEE1394_SBP2_PACKET_DUMP)
2364 scsi_print_command(SCpnt);
2365#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Jody McIntyreabd559b2005-09-30 11:59:06 -07002367 if (!sbp2util_node_is_available(scsi_id))
2368 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
2370 hi = scsi_id->hi;
2371
2372 if (!hi) {
2373 SBP2_ERR("sbp2scsi_host_info is NULL - this is bad!");
Jody McIntyreabd559b2005-09-30 11:59:06 -07002374 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
2376
2377 /*
2378 * Until we handle multiple luns, just return selection time-out
2379 * to any IO directed at non-zero LUNs
2380 */
Jody McIntyreabd559b2005-09-30 11:59:06 -07002381 if (SCpnt->device->lun)
2382 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384 /*
2385 * Check for request sense command, and handle it here
2386 * (autorequest sense)
2387 */
2388 if (SCpnt->cmnd[0] == REQUEST_SENSE) {
2389 SBP2_DEBUG("REQUEST_SENSE");
2390 memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen);
2391 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
2392 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done);
Jody McIntyreabd559b2005-09-30 11:59:06 -07002393 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 }
2395
2396 /*
2397 * Check to see if we are in the middle of a bus reset.
2398 */
2399 if (!hpsb_node_entry_valid(scsi_id->ne)) {
2400 SBP2_ERR("Bus reset in progress - rejecting command");
Jody McIntyreabd559b2005-09-30 11:59:06 -07002401 result = DID_BUS_BUSY << 16;
2402 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 }
2404
2405 /*
Stefan Richter43863eb2005-12-12 23:03:24 -05002406 * Bidirectional commands are not yet implemented,
2407 * and unknown transfer direction not handled.
2408 */
2409 if (SCpnt->sc_data_direction == DMA_BIDIRECTIONAL) {
2410 SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
2411 result = DID_ERROR << 16;
2412 goto done;
2413 }
2414
2415 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 * Try and send our SCSI command
2417 */
2418 if (sbp2_send_command(scsi_id, SCpnt, done)) {
2419 SBP2_ERR("Error sending SCSI command");
2420 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
2421 SCpnt, done);
2422 }
Jody McIntyreabd559b2005-09-30 11:59:06 -07002423 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Jody McIntyreabd559b2005-09-30 11:59:06 -07002425done:
2426 SCpnt->result = result;
2427 done(SCpnt);
2428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
2431/*
2432 * This function is called in order to complete all outstanding SBP-2
2433 * commands (in case of resets, etc.).
2434 */
2435static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
2436 u32 status)
2437{
2438 struct sbp2scsi_host_info *hi = scsi_id->hi;
2439 struct list_head *lh;
2440 struct sbp2_command_info *command;
Jody McIntyre79456192005-11-07 06:29:39 -05002441 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Stefan Richterd024ebc2006-03-28 20:03:55 -05002443 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Jody McIntyre79456192005-11-07 06:29:39 -05002445 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 while (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
2447 SBP2_DEBUG("Found pending command to complete");
2448 lh = scsi_id->sbp2_command_orb_inuse.next;
2449 command = list_entry(lh, struct sbp2_command_info, list);
2450 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
2451 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02002452 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
2454 sizeof(command->scatter_gather_element),
2455 PCI_DMA_BIDIRECTIONAL);
2456 sbp2util_mark_command_completed(scsi_id, command);
2457 if (command->Current_SCpnt) {
2458 command->Current_SCpnt->result = status << 16;
2459 command->Current_done(command->Current_SCpnt);
2460 }
2461 }
Jody McIntyre79456192005-11-07 06:29:39 -05002462 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 return;
2465}
2466
2467/*
2468 * This function is called in order to complete a regular SBP-2 command.
2469 *
2470 * This can be called in interrupt context.
2471 */
2472static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
2473 u32 scsi_status, struct scsi_cmnd *SCpnt,
2474 void (*done)(struct scsi_cmnd *))
2475{
Stefan Richterd024ebc2006-03-28 20:03:55 -05002476 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
2478 /*
2479 * Sanity
2480 */
2481 if (!SCpnt) {
2482 SBP2_ERR("SCpnt is NULL");
2483 return;
2484 }
2485
2486 /*
2487 * If a bus reset is in progress and there was an error, don't
2488 * complete the command, just let it get retried at the end of the
2489 * bus reset.
2490 */
Stefan Richtera237f352005-11-07 06:31:39 -05002491 if (!hpsb_node_entry_valid(scsi_id->ne)
2492 && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 SBP2_ERR("Bus reset in progress - retry command later");
2494 return;
2495 }
Stefan Richtera237f352005-11-07 06:31:39 -05002496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 /*
2498 * Switch on scsi status
2499 */
2500 switch (scsi_status) {
Stefan Richtera237f352005-11-07 06:31:39 -05002501 case SBP2_SCSI_STATUS_GOOD:
Stefan Richter8f0525f2006-03-28 20:03:45 -05002502 SCpnt->result = DID_OK << 16;
Stefan Richtera237f352005-11-07 06:31:39 -05002503 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504
Stefan Richtera237f352005-11-07 06:31:39 -05002505 case SBP2_SCSI_STATUS_BUSY:
2506 SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
2507 SCpnt->result = DID_BUS_BUSY << 16;
2508 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
Stefan Richtera237f352005-11-07 06:31:39 -05002510 case SBP2_SCSI_STATUS_CHECK_CONDITION:
2511 SBP2_DEBUG("SBP2_SCSI_STATUS_CHECK_CONDITION");
Stefan Richter8f0525f2006-03-28 20:03:45 -05002512 SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513#if CONFIG_IEEE1394_SBP2_DEBUG >= 1
Stefan Richtera237f352005-11-07 06:31:39 -05002514 scsi_print_command(SCpnt);
Stefan Richterd024ebc2006-03-28 20:03:55 -05002515 scsi_print_sense(SBP2_DEVICE_NAME, SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516#endif
Stefan Richtera237f352005-11-07 06:31:39 -05002517 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Stefan Richtera237f352005-11-07 06:31:39 -05002519 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
2520 SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
2521 SCpnt->result = DID_NO_CONNECT << 16;
2522 scsi_print_command(SCpnt);
2523 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Stefan Richtera237f352005-11-07 06:31:39 -05002525 case SBP2_SCSI_STATUS_CONDITION_MET:
2526 case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
2527 case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
2528 SBP2_ERR("Bad SCSI status = %x", scsi_status);
2529 SCpnt->result = DID_ERROR << 16;
2530 scsi_print_command(SCpnt);
2531 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532
Stefan Richtera237f352005-11-07 06:31:39 -05002533 default:
2534 SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
2535 SCpnt->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 }
2537
2538 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 * If a bus reset is in progress and there was an error, complete
2540 * the command as busy so that it will get retried.
2541 */
Stefan Richtera237f352005-11-07 06:31:39 -05002542 if (!hpsb_node_entry_valid(scsi_id->ne)
2543 && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 SBP2_ERR("Completing command with busy (bus reset)");
2545 SCpnt->result = DID_BUS_BUSY << 16;
2546 }
2547
2548 /*
2549 * If a unit attention occurs, return busy status so it gets
2550 * retried... it could have happened because of a 1394 bus reset
2551 * or hot-plug...
Stefan Richter8f0525f2006-03-28 20:03:45 -05002552 * XXX DID_BUS_BUSY is actually a bad idea because it will defy
2553 * the scsi layer's retry logic.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 */
2555#if 0
2556 if ((scsi_status == SBP2_SCSI_STATUS_CHECK_CONDITION) &&
2557 (SCpnt->sense_buffer[2] == UNIT_ATTENTION)) {
2558 SBP2_DEBUG("UNIT ATTENTION - return busy");
2559 SCpnt->result = DID_BUS_BUSY << 16;
2560 }
2561#endif
2562
2563 /*
2564 * Tell scsi stack that we're done with this command
2565 */
Stefan Richtera237f352005-11-07 06:31:39 -05002566 done(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567}
2568
Jody McIntyreabd559b2005-09-30 11:59:06 -07002569static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
2570{
Stefan Richtera80614d2006-02-14 22:04:19 -05002571 struct scsi_id_instance_data *scsi_id =
2572 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2573
2574 scsi_id->sdev = sdev;
Stefan Richterc394f1e2006-08-07 20:48:00 +02002575 sdev->allow_restart = 1;
Stefan Richtera80614d2006-02-14 22:04:19 -05002576
Stefan Richter24d3bf82006-05-15 22:04:59 +02002577 if (scsi_id->workarounds & SBP2_WORKAROUND_INQUIRY_36)
Stefan Richtera80614d2006-02-14 22:04:19 -05002578 sdev->inquiry_len = 36;
Jody McIntyreabd559b2005-09-30 11:59:06 -07002579 return 0;
2580}
2581
Jody McIntyreabd559b2005-09-30 11:59:06 -07002582static int sbp2scsi_slave_configure(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583{
Stefan Richter24d3bf82006-05-15 22:04:59 +02002584 struct scsi_id_instance_data *scsi_id =
2585 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2586
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
Ben Collins365c7862005-11-07 06:31:24 -05002588 sdev->use_10_for_rw = 1;
Stefan Richter24d3bf82006-05-15 22:04:59 +02002589
2590 if (sdev->type == TYPE_DISK &&
2591 scsi_id->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
2592 sdev->skip_ms_page_8 = 1;
Stefan Richtere9a1c522006-05-15 22:06:37 +02002593 if (scsi_id->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
2594 sdev->fix_capacity = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 return 0;
2596}
2597
Jody McIntyreabd559b2005-09-30 11:59:06 -07002598static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
2599{
2600 ((struct scsi_id_instance_data *)sdev->host->hostdata[0])->sdev = NULL;
2601 return;
2602}
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604/*
2605 * Called by scsi stack when something has really gone wrong. Usually
2606 * called when a command has timed-out for some reason.
2607 */
2608static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
2609{
2610 struct scsi_id_instance_data *scsi_id =
2611 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2612 struct sbp2scsi_host_info *hi = scsi_id->hi;
2613 struct sbp2_command_info *command;
Stefan Richter24c7cd02006-04-01 21:11:41 +02002614 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 SBP2_ERR("aborting sbp2 command");
2617 scsi_print_command(SCpnt);
2618
Jody McIntyreabd559b2005-09-30 11:59:06 -07002619 if (sbp2util_node_is_available(scsi_id)) {
Stefan Richter23077f12006-09-11 20:17:14 +02002620 sbp2_agent_reset(scsi_id, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Stefan Richter23077f12006-09-11 20:17:14 +02002622 /* Return a matching command structure to the free pool. */
Stefan Richter24c7cd02006-04-01 21:11:41 +02002623 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
2625 if (command) {
2626 SBP2_DEBUG("Found command to abort");
2627 pci_dma_sync_single_for_cpu(hi->host->pdev,
2628 command->command_orb_dma,
2629 sizeof(struct sbp2_command_orb),
Stefan Richterd4018d72006-07-23 22:57:00 +02002630 PCI_DMA_TODEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 pci_dma_sync_single_for_cpu(hi->host->pdev,
2632 command->sge_dma,
2633 sizeof(command->scatter_gather_element),
2634 PCI_DMA_BIDIRECTIONAL);
2635 sbp2util_mark_command_completed(scsi_id, command);
2636 if (command->Current_SCpnt) {
2637 command->Current_SCpnt->result = DID_ABORT << 16;
2638 command->Current_done(command->Current_SCpnt);
2639 }
2640 }
Stefan Richter24c7cd02006-04-01 21:11:41 +02002641 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
2644 }
2645
Stefan Richtera237f352005-11-07 06:31:39 -05002646 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647}
2648
2649/*
2650 * Called by scsi stack when something has really gone wrong.
2651 */
Jody McIntyreabd559b2005-09-30 11:59:06 -07002652static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653{
2654 struct scsi_id_instance_data *scsi_id =
2655 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2656
2657 SBP2_ERR("reset requested");
2658
Jody McIntyreabd559b2005-09-30 11:59:06 -07002659 if (sbp2util_node_is_available(scsi_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 SBP2_ERR("Generating sbp2 fetch agent reset");
Stefan Richter1f427e82006-08-14 18:44:00 +02002661 sbp2_agent_reset(scsi_id, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 }
2663
Jody McIntyreabd559b2005-09-30 11:59:06 -07002664 return SUCCESS;
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04002665}
2666
Stefan Richtera237f352005-11-07 06:31:39 -05002667static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
2668 struct device_attribute *attr,
2669 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670{
2671 struct scsi_device *sdev;
2672 struct scsi_id_instance_data *scsi_id;
2673 int lun;
2674
2675 if (!(sdev = to_scsi_device(dev)))
2676 return 0;
2677
2678 if (!(scsi_id = (struct scsi_id_instance_data *)sdev->host->hostdata[0]))
2679 return 0;
2680
Ben Collinse309fc62005-11-07 06:31:34 -05002681 lun = ORB_SET_LUN(scsi_id->sbp2_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682
2683 return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)scsi_id->ne->guid,
2684 scsi_id->ud->id, lun);
2685}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
2687MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2688MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2689MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2690MODULE_LICENSE("GPL");
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692static int sbp2_module_init(void)
2693{
2694 int ret;
2695
Stefan Richterd024ebc2006-03-28 20:03:55 -05002696 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 /* Module load debug option to force one command at a time (serializing I/O) */
2699 if (serialize_io) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 scsi_driver_template.can_queue = 1;
2701 scsi_driver_template.cmd_per_lun = 1;
2702 }
2703
Stefan Richter24d3bf82006-05-15 22:04:59 +02002704 if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
2705 (max_sectors * 512) > (128 * 1024))
2706 max_sectors = 128 * 1024 / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 scsi_driver_template.max_sectors = max_sectors;
2708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 /* Register our high level driver with 1394 stack */
2710 hpsb_register_highlevel(&sbp2_highlevel);
2711
2712 ret = hpsb_register_protocol(&sbp2_driver);
2713 if (ret) {
2714 SBP2_ERR("Failed to register protocol");
2715 hpsb_unregister_highlevel(&sbp2_highlevel);
2716 return ret;
2717 }
2718
2719 return 0;
2720}
2721
2722static void __exit sbp2_module_exit(void)
2723{
Stefan Richterd024ebc2006-03-28 20:03:55 -05002724 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725
2726 hpsb_unregister_protocol(&sbp2_driver);
2727
2728 hpsb_unregister_highlevel(&sbp2_highlevel);
2729}
2730
2731module_init(sbp2_module_init);
2732module_exit(sbp2_module_exit);