blob: 24cd1e250d8420c248c831c1563ada391e6e3c5b [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
41#include <linux/config.h>
42#include <linux/kernel.h>
43#include <linux/list.h>
44#include <linux/string.h>
Stefan Richter24d3bf82006-05-15 22:04:59 +020045#include <linux/stringify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/slab.h>
47#include <linux/interrupt.h>
48#include <linux/fs.h>
49#include <linux/poll.h>
50#include <linux/module.h>
51#include <linux/moduleparam.h>
52#include <linux/types.h>
53#include <linux/delay.h>
54#include <linux/sched.h>
55#include <linux/blkdev.h>
56#include <linux/smp_lock.h>
57#include <linux/init.h>
58#include <linux/pci.h>
59
60#include <asm/current.h>
61#include <asm/uaccess.h>
62#include <asm/io.h>
63#include <asm/byteorder.h>
64#include <asm/atomic.h>
65#include <asm/system.h>
66#include <asm/scatterlist.h>
67
68#include <scsi/scsi.h>
69#include <scsi/scsi_cmnd.h>
70#include <scsi/scsi_dbg.h>
71#include <scsi/scsi_device.h>
72#include <scsi/scsi_host.h>
73
74#include "csr1212.h"
75#include "ieee1394.h"
76#include "ieee1394_types.h"
77#include "ieee1394_core.h"
78#include "nodemgr.h"
79#include "hosts.h"
80#include "highlevel.h"
81#include "ieee1394_transactions.h"
82#include "sbp2.h"
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
85 * Module load parameter definitions
86 */
87
88/*
89 * Change max_speed on module load if you have a bad IEEE-1394
90 * controller that has trouble running 2KB packets at 400mb.
91 *
92 * NOTE: On certain OHCI parts I have seen short packets on async transmit
93 * (probably due to PCI latency/throughput issues with the part). You can
94 * bump down the speed if you are running into problems.
95 */
96static int max_speed = IEEE1394_SPEED_MAX;
97module_param(max_speed, int, 0644);
Jody McIntyre2bab3592005-09-30 11:59:07 -070098MODULE_PARM_DESC(max_speed, "Force max speed (3 = 800mb, 2 = 400mb, 1 = 200mb, 0 = 100mb)");
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100/*
101 * Set serialize_io to 1 if you'd like only one scsi command sent
102 * down to us at a time (debugging). This might be necessary for very
103 * badly behaved sbp2 devices.
Jody McIntyre2bab3592005-09-30 11:59:07 -0700104 *
105 * TODO: Make this configurable per device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 */
Jody McIntyre2bab3592005-09-30 11:59:07 -0700107static int serialize_io = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108module_param(serialize_io, int, 0444);
Jody McIntyre2bab3592005-09-30 11:59:07 -0700109MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers (default = 1, faster = 0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111/*
112 * Bump up max_sectors if you'd like to support very large sized
113 * transfers. Please note that some older sbp2 bridge chips are broken for
114 * transfers greater or equal to 128KB. Default is a value of 255
115 * sectors, or just under 128KB (at 512 byte sector size). I can note that
116 * the Oxsemi sbp2 chipsets have no problems supporting very large
117 * transfer sizes.
118 */
119static int max_sectors = SBP2_MAX_SECTORS;
120module_param(max_sectors, int, 0444);
Stefan Richter24d3bf82006-05-15 22:04:59 +0200121MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported (default = "
122 __stringify(SBP2_MAX_SECTORS) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124/*
125 * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
126 * do an exclusive login, as it's generally unsafe to have two hosts
127 * talking to a single sbp2 device at the same time (filesystem coherency,
128 * etc.). If you're running an sbp2 device that supports multiple logins,
129 * and you're either running read-only filesystems or some sort of special
130 * filesystem supporting multiple hosts (one such filesystem is OpenGFS,
131 * see opengfs.sourceforge.net for more info), then set exclusive_login
132 * to zero. Note: The Oxsemi OXFW911 sbp2 chipset supports up to four
133 * concurrent logins.
134 */
135static int exclusive_login = 1;
136module_param(exclusive_login, int, 0644);
137MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device (default = 1)");
138
139/*
Stefan Richter24d3bf82006-05-15 22:04:59 +0200140 * If any of the following workarounds is required for your device to work,
141 * please submit the kernel messages logged by sbp2 to the linux1394-devel
142 * mailing list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 *
Stefan Richter24d3bf82006-05-15 22:04:59 +0200144 * - 128kB max transfer
145 * Limit transfer size. Necessary for some old bridges.
146 *
147 * - 36 byte inquiry
148 * When scsi_mod probes the device, let the inquiry command look like that
149 * from MS Windows.
150 *
151 * - skip mode page 8
152 * Suppress sending of mode_sense for mode page 8 if the device pretends to
153 * support the SCSI Primary Block commands instead of Reduced Block Commands.
Stefan Richtere9a1c522006-05-15 22:06:37 +0200154 *
155 * - fix capacity
156 * Tell sd_mod to correct the last sector number reported by read_capacity.
157 * Avoids access beyond actual disk limits on devices with an off-by-one bug.
158 * Don't use this with devices which don't have this bug.
Stefan Richter679c0cd2006-05-15 22:08:09 +0200159 *
160 * - override internal blacklist
161 * Instead of adding to the built-in blacklist, use only the workarounds
162 * specified in the module load parameter.
163 * Useful if a blacklist entry interfered with a non-broken device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 */
Stefan Richter24d3bf82006-05-15 22:04:59 +0200165static int sbp2_default_workarounds;
166module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
167MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
168 ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
169 ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
170 ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
Stefan Richtere9a1c522006-05-15 22:06:37 +0200171 ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
Stefan Richter679c0cd2006-05-15 22:08:09 +0200172 ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
Stefan Richter24d3bf82006-05-15 22:04:59 +0200173 ", or a combination)");
174
175/* legacy parameter */
Ben Collins1934b8b2005-07-09 20:01:23 -0400176static int force_inquiry_hack;
Stefan Richtera80614d2006-02-14 22:04:19 -0500177module_param(force_inquiry_hack, int, 0644);
Stefan Richter24d3bf82006-05-15 22:04:59 +0200178MODULE_PARM_DESC(force_inquiry_hack, "Deprecated, use 'workarounds'");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180/*
181 * Export information about protocols/devices supported by this driver.
182 */
183static struct ieee1394_device_id sbp2_id_table[] = {
184 {
Stefan Richtera237f352005-11-07 06:31:39 -0500185 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
186 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
187 .version = SBP2_SW_VERSION_ENTRY & 0xffffff},
188 {}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189};
190
191MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
192
193/*
194 * Debug levels, configured via kernel config, or enable here.
195 */
196
Olaf Hering44456d32005-07-27 11:45:17 -0700197#define CONFIG_IEEE1394_SBP2_DEBUG 0
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198/* #define CONFIG_IEEE1394_SBP2_DEBUG_ORBS */
199/* #define CONFIG_IEEE1394_SBP2_DEBUG_DMA */
200/* #define CONFIG_IEEE1394_SBP2_DEBUG 1 */
201/* #define CONFIG_IEEE1394_SBP2_DEBUG 2 */
202/* #define CONFIG_IEEE1394_SBP2_PACKET_DUMP */
203
204#ifdef CONFIG_IEEE1394_SBP2_DEBUG_ORBS
205#define SBP2_ORB_DEBUG(fmt, args...) HPSB_ERR("sbp2(%s): "fmt, __FUNCTION__, ## args)
206static u32 global_outstanding_command_orbs = 0;
207#define outstanding_orb_incr global_outstanding_command_orbs++
208#define outstanding_orb_decr global_outstanding_command_orbs--
209#else
210#define SBP2_ORB_DEBUG(fmt, args...)
211#define outstanding_orb_incr
212#define outstanding_orb_decr
213#endif
214
215#ifdef CONFIG_IEEE1394_SBP2_DEBUG_DMA
216#define SBP2_DMA_ALLOC(fmt, args...) \
217 HPSB_ERR("sbp2(%s)alloc(%d): "fmt, __FUNCTION__, \
218 ++global_outstanding_dmas, ## args)
219#define SBP2_DMA_FREE(fmt, args...) \
220 HPSB_ERR("sbp2(%s)free(%d): "fmt, __FUNCTION__, \
221 --global_outstanding_dmas, ## args)
222static u32 global_outstanding_dmas = 0;
223#else
224#define SBP2_DMA_ALLOC(fmt, args...)
225#define SBP2_DMA_FREE(fmt, args...)
226#endif
227
228#if CONFIG_IEEE1394_SBP2_DEBUG >= 2
229#define SBP2_DEBUG(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
230#define SBP2_INFO(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
231#define SBP2_NOTICE(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
232#define SBP2_WARN(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
233#elif CONFIG_IEEE1394_SBP2_DEBUG == 1
234#define SBP2_DEBUG(fmt, args...) HPSB_DEBUG("sbp2: "fmt, ## args)
235#define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
236#define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
237#define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
238#else
239#define SBP2_DEBUG(fmt, args...)
240#define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
241#define SBP2_NOTICE(fmt, args...) HPSB_NOTICE("sbp2: "fmt, ## args)
242#define SBP2_WARN(fmt, args...) HPSB_WARN("sbp2: "fmt, ## args)
243#endif
244
245#define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
Stefan Richterd024ebc2006-03-28 20:03:55 -0500246#define SBP2_DEBUG_ENTER() SBP2_DEBUG("%s", __FUNCTION__)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248/*
249 * Globals
250 */
251
252static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
253 u32 status);
254
255static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
256 u32 scsi_status, struct scsi_cmnd *SCpnt,
257 void (*done)(struct scsi_cmnd *));
258
259static struct scsi_host_template scsi_driver_template;
260
261static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
262
263static void sbp2_host_reset(struct hpsb_host *host);
264
265static int sbp2_probe(struct device *dev);
266static int sbp2_remove(struct device *dev);
267static int sbp2_update(struct unit_directory *ud);
268
269static struct hpsb_highlevel sbp2_highlevel = {
270 .name = SBP2_DEVICE_NAME,
271 .host_reset = sbp2_host_reset,
272};
273
274static struct hpsb_address_ops sbp2_ops = {
275 .write = sbp2_handle_status_write
276};
277
278#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
279static struct hpsb_address_ops sbp2_physdma_ops = {
Stefan Richtera237f352005-11-07 06:31:39 -0500280 .read = sbp2_handle_physdma_read,
281 .write = sbp2_handle_physdma_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282};
283#endif
284
285static struct hpsb_protocol_driver sbp2_driver = {
286 .name = "SBP2 Driver",
287 .id_table = sbp2_id_table,
288 .update = sbp2_update,
289 .driver = {
290 .name = SBP2_DEVICE_NAME,
291 .bus = &ieee1394_bus_type,
292 .probe = sbp2_probe,
293 .remove = sbp2_remove,
294 },
295};
296
Stefan Richtera80614d2006-02-14 22:04:19 -0500297/*
Stefan Richter24d3bf82006-05-15 22:04:59 +0200298 * List of devices with known bugs.
299 *
300 * The firmware_revision field, masked with 0xffff00, is the best indicator
301 * for the type of bridge chip of a device. It yields a few false positives
302 * but this did not break correctly behaving devices so far.
Stefan Richtera80614d2006-02-14 22:04:19 -0500303 */
Stefan Richter24d3bf82006-05-15 22:04:59 +0200304static const struct {
305 u32 firmware_revision;
Stefan Richtere9a1c522006-05-15 22:06:37 +0200306 u32 model_id;
Stefan Richter24d3bf82006-05-15 22:04:59 +0200307 unsigned workarounds;
308} sbp2_workarounds_table[] = {
Ben Collins4b9a3342006-06-12 18:10:18 -0400309 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
Stefan Richter24d3bf82006-05-15 22:04:59 +0200310 .firmware_revision = 0x002800,
Ben Collins4b9a3342006-06-12 18:10:18 -0400311 .model_id = 0x001010,
Stefan Richter24d3bf82006-05-15 22:04:59 +0200312 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
313 SBP2_WORKAROUND_MODE_SENSE_8,
314 },
315 /* Initio bridges, actually only needed for some older ones */ {
316 .firmware_revision = 0x000200,
317 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
318 },
319 /* Symbios bridge */ {
320 .firmware_revision = 0xa0b800,
321 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
Stefan Richtere9a1c522006-05-15 22:06:37 +0200322 },
323 /*
324 * Note about the following Apple iPod blacklist entries:
325 *
326 * There are iPods (2nd gen, 3rd gen) with model_id==0. Since our
327 * matching logic treats 0 as a wildcard, we cannot match this ID
328 * without rewriting the matching routine. Fortunately these iPods
329 * do not feature the read_capacity bug according to one report.
330 * Read_capacity behaviour as well as model_id could change due to
331 * Apple-supplied firmware updates though.
332 */
333 /* iPod 4th generation */ {
334 .firmware_revision = 0x0a2700,
335 .model_id = 0x000021,
336 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
337 },
338 /* iPod mini */ {
339 .firmware_revision = 0x0a2700,
340 .model_id = 0x000023,
341 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
342 },
343 /* iPod Photo */ {
344 .firmware_revision = 0x0a2700,
345 .model_id = 0x00007e,
346 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
Stefan Richter24d3bf82006-05-15 22:04:59 +0200347 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348};
349
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350/**************************************
351 * General utility functions
352 **************************************/
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#ifndef __BIG_ENDIAN
355/*
356 * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
357 */
358static __inline__ void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
359{
360 u32 *temp = buffer;
361
362 for (length = (length >> 2); length--; )
363 temp[length] = be32_to_cpu(temp[length]);
364
365 return;
366}
367
368/*
369 * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
370 */
371static __inline__ void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
372{
373 u32 *temp = buffer;
374
375 for (length = (length >> 2); length--; )
376 temp[length] = cpu_to_be32(temp[length]);
377
378 return;
379}
380#else /* BIG_ENDIAN */
381/* Why waste the cpu cycles? */
382#define sbp2util_be32_to_cpu_buffer(x,y)
383#define sbp2util_cpu_to_be32_buffer(x,y)
384#endif
385
386#ifdef CONFIG_IEEE1394_SBP2_PACKET_DUMP
387/*
388 * Debug packet dump routine. Length is in bytes.
389 */
Stefan Richtera237f352005-11-07 06:31:39 -0500390static void sbp2util_packet_dump(void *buffer, int length, char *dump_name,
391 u32 dump_phys_addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 int i;
394 unsigned char *dump = buffer;
395
396 if (!dump || !length || !dump_name)
397 return;
398
399 if (dump_phys_addr)
400 printk("[%s, 0x%x]", dump_name, dump_phys_addr);
401 else
402 printk("[%s]", dump_name);
403 for (i = 0; i < length; i++) {
404 if (i > 0x3f) {
405 printk("\n ...");
406 break;
407 }
408 if ((i & 0x3) == 0)
409 printk(" ");
410 if ((i & 0xf) == 0)
411 printk("\n ");
Stefan Richtera237f352005-11-07 06:31:39 -0500412 printk("%02x ", (int)dump[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 }
414 printk("\n");
415
416 return;
417}
418#else
419#define sbp2util_packet_dump(w,x,y,z)
420#endif
421
422/*
423 * Goofy routine that basically does a down_timeout function.
424 */
425static int sbp2util_down_timeout(atomic_t *done, int timeout)
426{
427 int i;
428
429 for (i = timeout; (i > 0 && atomic_read(done) == 0); i-= HZ/10) {
430 if (msleep_interruptible(100)) /* 100ms */
Stefan Richtera237f352005-11-07 06:31:39 -0500431 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 }
Stefan Richtera237f352005-11-07 06:31:39 -0500433 return (i > 0) ? 0 : 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436/* Free's an allocated packet */
437static void sbp2_free_packet(struct hpsb_packet *packet)
438{
439 hpsb_free_tlabel(packet);
440 hpsb_free_packet(packet);
441}
442
443/* This is much like hpsb_node_write(), except it ignores the response
444 * subaction and returns immediately. Can be used from interrupts.
445 */
446static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
Stefan Richtera237f352005-11-07 06:31:39 -0500447 quadlet_t *buffer, size_t length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 struct hpsb_packet *packet;
450
451 packet = hpsb_make_writepacket(ne->host, ne->nodeid,
452 addr, buffer, length);
Stefan Richtera237f352005-11-07 06:31:39 -0500453 if (!packet)
454 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Stefan Richtera237f352005-11-07 06:31:39 -0500456 hpsb_set_packet_complete_task(packet,
457 (void (*)(void *))sbp2_free_packet,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 packet);
459
460 hpsb_node_fill_packet(ne, packet);
461
Stefan Richtera237f352005-11-07 06:31:39 -0500462 if (hpsb_send_packet(packet) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 sbp2_free_packet(packet);
464 return -EIO;
465 }
466
467 return 0;
468}
469
470/*
471 * This function is called to create a pool of command orbs used for
472 * command processing. It is called when a new sbp2 device is detected.
473 */
474static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id)
475{
476 struct sbp2scsi_host_info *hi = scsi_id->hi;
477 int i;
478 unsigned long flags, orbs;
479 struct sbp2_command_info *command;
480
481 orbs = serialize_io ? 2 : SBP2_MAX_CMDS;
482
483 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
484 for (i = 0; i < orbs; i++) {
Stefan Richter85511582005-11-07 06:31:45 -0500485 command = kzalloc(sizeof(*command), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (!command) {
Stefan Richtera237f352005-11-07 06:31:39 -0500487 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock,
488 flags);
489 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 command->command_orb_dma =
Stefan Richtera237f352005-11-07 06:31:39 -0500492 pci_map_single(hi->host->pdev, &command->command_orb,
493 sizeof(struct sbp2_command_orb),
494 PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 SBP2_DMA_ALLOC("single command orb DMA");
496 command->sge_dma =
Stefan Richtera237f352005-11-07 06:31:39 -0500497 pci_map_single(hi->host->pdev,
498 &command->scatter_gather_element,
499 sizeof(command->scatter_gather_element),
500 PCI_DMA_BIDIRECTIONAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 SBP2_DMA_ALLOC("scatter_gather_element");
502 INIT_LIST_HEAD(&command->list);
503 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
504 }
505 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
506 return 0;
507}
508
509/*
510 * This function is called to delete a pool of command orbs.
511 */
512static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id)
513{
514 struct hpsb_host *host = scsi_id->hi->host;
515 struct list_head *lh, *next;
516 struct sbp2_command_info *command;
517 unsigned long flags;
518
519 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
520 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
521 list_for_each_safe(lh, next, &scsi_id->sbp2_command_orb_completed) {
522 command = list_entry(lh, struct sbp2_command_info, list);
523
524 /* Release our generic DMA's */
525 pci_unmap_single(host->pdev, command->command_orb_dma,
526 sizeof(struct sbp2_command_orb),
527 PCI_DMA_BIDIRECTIONAL);
528 SBP2_DMA_FREE("single command orb DMA");
529 pci_unmap_single(host->pdev, command->sge_dma,
530 sizeof(command->scatter_gather_element),
531 PCI_DMA_BIDIRECTIONAL);
532 SBP2_DMA_FREE("scatter_gather_element");
533
534 kfree(command);
535 }
536 }
537 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
538 return;
539}
540
541/*
542 * This function finds the sbp2_command for a given outstanding command
543 * orb.Only looks at the inuse list.
544 */
545static struct sbp2_command_info *sbp2util_find_command_for_orb(
546 struct scsi_id_instance_data *scsi_id, dma_addr_t orb)
547{
548 struct sbp2_command_info *command;
549 unsigned long flags;
550
551 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
552 if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
553 list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list) {
554 if (command->command_orb_dma == orb) {
555 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Stefan Richtera237f352005-11-07 06:31:39 -0500556 return command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 }
558 }
559 }
560 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
561
562 SBP2_ORB_DEBUG("could not match command orb %x", (unsigned int)orb);
563
Stefan Richtera237f352005-11-07 06:31:39 -0500564 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565}
566
567/*
568 * This function finds the sbp2_command for a given outstanding SCpnt.
569 * Only looks at the inuse list.
Stefan Richter24c7cd02006-04-01 21:11:41 +0200570 * Must be called with scsi_id->sbp2_command_orb_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 */
Stefan Richter24c7cd02006-04-01 21:11:41 +0200572static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
573 struct scsi_id_instance_data *scsi_id, void *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 struct sbp2_command_info *command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
Stefan Richter24c7cd02006-04-01 21:11:41 +0200577 if (!list_empty(&scsi_id->sbp2_command_orb_inuse))
578 list_for_each_entry(command, &scsi_id->sbp2_command_orb_inuse, list)
579 if (command->Current_SCpnt == SCpnt)
Stefan Richtera237f352005-11-07 06:31:39 -0500580 return command;
Stefan Richtera237f352005-11-07 06:31:39 -0500581 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584/*
585 * This function allocates a command orb used to send a scsi command.
586 */
587static struct sbp2_command_info *sbp2util_allocate_command_orb(
588 struct scsi_id_instance_data *scsi_id,
589 struct scsi_cmnd *Current_SCpnt,
590 void (*Current_done)(struct scsi_cmnd *))
591{
592 struct list_head *lh;
593 struct sbp2_command_info *command = NULL;
594 unsigned long flags;
595
596 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
597 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
598 lh = scsi_id->sbp2_command_orb_completed.next;
599 list_del(lh);
600 command = list_entry(lh, struct sbp2_command_info, list);
601 command->Current_done = Current_done;
602 command->Current_SCpnt = Current_SCpnt;
603 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_inuse);
604 } else {
Stefan Richterd024ebc2006-03-28 20:03:55 -0500605 SBP2_ERR("%s: no orbs available", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Stefan Richtera237f352005-11-07 06:31:39 -0500608 return command;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611/* Free our DMA's */
612static void sbp2util_free_command_dma(struct sbp2_command_info *command)
613{
614 struct scsi_id_instance_data *scsi_id =
615 (struct scsi_id_instance_data *)command->Current_SCpnt->device->host->hostdata[0];
616 struct hpsb_host *host;
617
618 if (!scsi_id) {
Stefan Richterd024ebc2006-03-28 20:03:55 -0500619 SBP2_ERR("%s: scsi_id == NULL", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return;
621 }
622
623 host = scsi_id->ud->ne->host;
624
625 if (command->cmd_dma) {
626 if (command->dma_type == CMD_DMA_SINGLE) {
627 pci_unmap_single(host->pdev, command->cmd_dma,
628 command->dma_size, command->dma_dir);
629 SBP2_DMA_FREE("single bulk");
630 } else if (command->dma_type == CMD_DMA_PAGE) {
631 pci_unmap_page(host->pdev, command->cmd_dma,
632 command->dma_size, command->dma_dir);
633 SBP2_DMA_FREE("single page");
634 } /* XXX: Check for CMD_DMA_NONE bug */
635 command->dma_type = CMD_DMA_NONE;
636 command->cmd_dma = 0;
637 }
638
639 if (command->sge_buffer) {
640 pci_unmap_sg(host->pdev, command->sge_buffer,
641 command->dma_size, command->dma_dir);
642 SBP2_DMA_FREE("scatter list");
643 command->sge_buffer = NULL;
644 }
645}
646
647/*
648 * This function moves a command to the completed orb list.
Stefan Richter24c7cd02006-04-01 21:11:41 +0200649 * Must be called with scsi_id->sbp2_command_orb_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 */
Stefan Richter24c7cd02006-04-01 21:11:41 +0200651static void sbp2util_mark_command_completed(
652 struct scsi_id_instance_data *scsi_id,
653 struct sbp2_command_info *command)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 list_del(&command->list);
656 sbp2util_free_command_dma(command);
657 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Jody McIntyreabd559b2005-09-30 11:59:06 -0700660/*
661 * Is scsi_id valid? Is the 1394 node still present?
662 */
663static inline int sbp2util_node_is_available(struct scsi_id_instance_data *scsi_id)
664{
665 return scsi_id && scsi_id->ne && !scsi_id->ne->in_limbo;
666}
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668/*********************************************
669 * IEEE-1394 core driver stack related section
670 *********************************************/
671static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud);
672
673static int sbp2_probe(struct device *dev)
674{
675 struct unit_directory *ud;
676 struct scsi_id_instance_data *scsi_id;
677
Stefan Richterd024ebc2006-03-28 20:03:55 -0500678 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 ud = container_of(dev, struct unit_directory, device);
681
682 /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
683 * instead. */
684 if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
685 return -ENODEV;
686
Stefan Richtera237f352005-11-07 06:31:39 -0500687 scsi_id = sbp2_alloc_device(ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Stefan Richtera237f352005-11-07 06:31:39 -0500689 if (!scsi_id)
690 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
Stefan Richtera237f352005-11-07 06:31:39 -0500692 sbp2_parse_unit_directory(scsi_id, ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Stefan Richtera237f352005-11-07 06:31:39 -0500694 return sbp2_start_device(scsi_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697static int sbp2_remove(struct device *dev)
698{
699 struct unit_directory *ud;
700 struct scsi_id_instance_data *scsi_id;
Jody McIntyreabd559b2005-09-30 11:59:06 -0700701 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Stefan Richterd024ebc2006-03-28 20:03:55 -0500703 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 ud = container_of(dev, struct unit_directory, device);
706 scsi_id = ud->device.driver_data;
Jody McIntyreabd559b2005-09-30 11:59:06 -0700707 if (!scsi_id)
708 return 0;
709
Stefan Richterbf637ec2006-01-31 00:13:06 -0500710 if (scsi_id->scsi_host) {
711 /* Get rid of enqueued commands if there is no chance to
712 * send them. */
713 if (!sbp2util_node_is_available(scsi_id))
714 sbp2scsi_complete_all_commands(scsi_id, DID_NO_CONNECT);
715 /* scsi_remove_device() will trigger shutdown functions of SCSI
716 * highlevel drivers which would deadlock if blocked. */
Jody McIntyreabd559b2005-09-30 11:59:06 -0700717 scsi_unblock_requests(scsi_id->scsi_host);
Stefan Richterbf637ec2006-01-31 00:13:06 -0500718 }
Jody McIntyreabd559b2005-09-30 11:59:06 -0700719 sdev = scsi_id->sdev;
720 if (sdev) {
721 scsi_id->sdev = NULL;
722 scsi_remove_device(sdev);
723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 sbp2_logout_device(scsi_id);
726 sbp2_remove_device(scsi_id);
727
728 return 0;
729}
730
731static int sbp2_update(struct unit_directory *ud)
732{
733 struct scsi_id_instance_data *scsi_id = ud->device.driver_data;
734
Stefan Richterd024ebc2006-03-28 20:03:55 -0500735 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
737 if (sbp2_reconnect_device(scsi_id)) {
738
739 /*
740 * Ok, reconnect has failed. Perhaps we didn't
741 * reconnect fast enough. Try doing a regular login, but
742 * first do a logout just in case of any weirdness.
743 */
744 sbp2_logout_device(scsi_id);
745
746 if (sbp2_login_device(scsi_id)) {
747 /* Login failed too, just fail, and the backend
748 * will call our sbp2_remove for us */
749 SBP2_ERR("Failed to reconnect to sbp2 device!");
750 return -EBUSY;
751 }
752 }
753
754 /* Set max retries to something large on the device. */
755 sbp2_set_busy_timeout(scsi_id);
756
757 /* Do a SBP-2 fetch agent reset. */
758 sbp2_agent_reset(scsi_id, 1);
759
760 /* Get the max speed and packet size that we can use. */
761 sbp2_max_speed_and_size(scsi_id);
762
763 /* Complete any pending commands with busy (so they get
764 * retried) and remove them from our queue
765 */
766 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
767
768 /* Make sure we unblock requests (since this is likely after a bus
769 * reset). */
770 scsi_unblock_requests(scsi_id->scsi_host);
771
772 return 0;
773}
774
775/* This functions is called by the sbp2_probe, for each new device. We now
776 * allocate one scsi host for each scsi_id (unit directory). */
777static struct scsi_id_instance_data *sbp2_alloc_device(struct unit_directory *ud)
778{
779 struct sbp2scsi_host_info *hi;
780 struct Scsi_Host *scsi_host = NULL;
781 struct scsi_id_instance_data *scsi_id = NULL;
782
Stefan Richterd024ebc2006-03-28 20:03:55 -0500783 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Stefan Richter85511582005-11-07 06:31:45 -0500785 scsi_id = kzalloc(sizeof(*scsi_id), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (!scsi_id) {
787 SBP2_ERR("failed to create scsi_id");
788 goto failed_alloc;
789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 scsi_id->ne = ud->ne;
792 scsi_id->ud = ud;
793 scsi_id->speed_code = IEEE1394_SPEED_100;
794 scsi_id->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
795 atomic_set(&scsi_id->sbp2_login_complete, 0);
796 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_inuse);
797 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_completed);
798 INIT_LIST_HEAD(&scsi_id->scsi_list);
799 spin_lock_init(&scsi_id->sbp2_command_orb_lock);
Ben Collinse309fc62005-11-07 06:31:34 -0500800 scsi_id->sbp2_lun = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
802 ud->device.driver_data = scsi_id;
803
804 hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
805 if (!hi) {
806 hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host, sizeof(*hi));
807 if (!hi) {
808 SBP2_ERR("failed to allocate hostinfo");
809 goto failed_alloc;
810 }
811 SBP2_DEBUG("sbp2_alloc_device: allocated hostinfo");
812 hi->host = ud->ne->host;
813 INIT_LIST_HEAD(&hi->scsi_ids);
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
816 /* Handle data movement if physical dma is not
Stefan Richter55664052006-03-28 19:59:42 -0500817 * enabled or not supported on host controller */
818 if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
819 &sbp2_physdma_ops,
820 0x0ULL, 0xfffffffcULL)) {
821 SBP2_ERR("failed to register lower 4GB address range");
822 goto failed_alloc;
823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824#endif
825 }
826
Stefan Richter147830f2006-03-28 19:54:52 -0500827 /* Prevent unloading of the 1394 host */
828 if (!try_module_get(hi->host->driver->owner)) {
829 SBP2_ERR("failed to get a reference on 1394 host driver");
830 goto failed_alloc;
831 }
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 scsi_id->hi = hi;
834
835 list_add_tail(&scsi_id->scsi_list, &hi->scsi_ids);
836
Stefan Richter35bdddb2006-01-31 00:13:33 -0500837 /* Register the status FIFO address range. We could use the same FIFO
838 * for targets at different nodes. However we need different FIFOs per
Stefan Richtera54c9d32006-05-15 22:09:46 +0200839 * target in order to support multi-unit devices.
840 * The FIFO is located out of the local host controller's physical range
841 * but, if possible, within the posted write area. Status writes will
842 * then be performed as unified transactions. This slightly reduces
843 * bandwidth usage, and some Prolific based devices seem to require it.
844 */
Stefan Richter35bdddb2006-01-31 00:13:33 -0500845 scsi_id->status_fifo_addr = hpsb_allocate_and_register_addrspace(
846 &sbp2_highlevel, ud->ne->host, &sbp2_ops,
847 sizeof(struct sbp2_status_block), sizeof(quadlet_t),
Stefan Richtera54c9d32006-05-15 22:09:46 +0200848 0x010000000000ULL, CSR1212_ALL_SPACE_END);
Stefan Richter829a1982006-06-04 02:51:40 -0700849 if (scsi_id->status_fifo_addr == ~0ULL) {
Stefan Richter35bdddb2006-01-31 00:13:33 -0500850 SBP2_ERR("failed to allocate status FIFO address range");
851 goto failed_alloc;
852 }
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 /* Register our host with the SCSI stack. */
Alexandre Olivaa2ef79e2005-06-15 22:26:31 -0700855 scsi_host = scsi_host_alloc(&scsi_driver_template,
Stefan Richtera237f352005-11-07 06:31:39 -0500856 sizeof(unsigned long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (!scsi_host) {
858 SBP2_ERR("failed to register scsi host");
859 goto failed_alloc;
860 }
861
862 scsi_host->hostdata[0] = (unsigned long)scsi_id;
863
864 if (!scsi_add_host(scsi_host, &ud->device)) {
865 scsi_id->scsi_host = scsi_host;
866 return scsi_id;
867 }
868
869 SBP2_ERR("failed to add scsi host");
870 scsi_host_put(scsi_host);
871
872failed_alloc:
873 sbp2_remove_device(scsi_id);
874 return NULL;
875}
876
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877static void sbp2_host_reset(struct hpsb_host *host)
878{
879 struct sbp2scsi_host_info *hi;
880 struct scsi_id_instance_data *scsi_id;
881
882 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
883
884 if (hi) {
885 list_for_each_entry(scsi_id, &hi->scsi_ids, scsi_list)
886 scsi_block_requests(scsi_id->scsi_host);
887 }
888}
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890/*
891 * This function is where we first pull the node unique ids, and then
892 * allocate memory and register a SBP-2 device.
893 */
894static int sbp2_start_device(struct scsi_id_instance_data *scsi_id)
895{
896 struct sbp2scsi_host_info *hi = scsi_id->hi;
James Bottomley146f7262005-09-10 12:44:09 -0500897 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Stefan Richterd024ebc2006-03-28 20:03:55 -0500899 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900
901 /* Login FIFO DMA */
902 scsi_id->login_response =
Stefan Richtera237f352005-11-07 06:31:39 -0500903 pci_alloc_consistent(hi->host->pdev,
904 sizeof(struct sbp2_login_response),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 &scsi_id->login_response_dma);
906 if (!scsi_id->login_response)
907 goto alloc_fail;
908 SBP2_DMA_ALLOC("consistent DMA region for login FIFO");
909
910 /* Query logins ORB DMA */
911 scsi_id->query_logins_orb =
Stefan Richtera237f352005-11-07 06:31:39 -0500912 pci_alloc_consistent(hi->host->pdev,
913 sizeof(struct sbp2_query_logins_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 &scsi_id->query_logins_orb_dma);
915 if (!scsi_id->query_logins_orb)
916 goto alloc_fail;
917 SBP2_DMA_ALLOC("consistent DMA region for query logins ORB");
918
919 /* Query logins response DMA */
920 scsi_id->query_logins_response =
Stefan Richtera237f352005-11-07 06:31:39 -0500921 pci_alloc_consistent(hi->host->pdev,
922 sizeof(struct sbp2_query_logins_response),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 &scsi_id->query_logins_response_dma);
924 if (!scsi_id->query_logins_response)
925 goto alloc_fail;
926 SBP2_DMA_ALLOC("consistent DMA region for query logins response");
927
928 /* Reconnect ORB DMA */
929 scsi_id->reconnect_orb =
Stefan Richtera237f352005-11-07 06:31:39 -0500930 pci_alloc_consistent(hi->host->pdev,
931 sizeof(struct sbp2_reconnect_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 &scsi_id->reconnect_orb_dma);
933 if (!scsi_id->reconnect_orb)
934 goto alloc_fail;
935 SBP2_DMA_ALLOC("consistent DMA region for reconnect ORB");
936
937 /* Logout ORB DMA */
938 scsi_id->logout_orb =
Stefan Richtera237f352005-11-07 06:31:39 -0500939 pci_alloc_consistent(hi->host->pdev,
940 sizeof(struct sbp2_logout_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 &scsi_id->logout_orb_dma);
942 if (!scsi_id->logout_orb)
943 goto alloc_fail;
944 SBP2_DMA_ALLOC("consistent DMA region for logout ORB");
945
946 /* Login ORB DMA */
947 scsi_id->login_orb =
Stefan Richtera237f352005-11-07 06:31:39 -0500948 pci_alloc_consistent(hi->host->pdev,
949 sizeof(struct sbp2_login_orb),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 &scsi_id->login_orb_dma);
Stefan Richtereaceec72005-12-13 11:05:05 -0500951 if (!scsi_id->login_orb)
952 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 SBP2_DMA_ALLOC("consistent DMA region for login ORB");
954
955 SBP2_DEBUG("New SBP-2 device inserted, SCSI ID = %x", scsi_id->ud->id);
956
957 /*
958 * Create our command orb pool
959 */
960 if (sbp2util_create_command_orb_pool(scsi_id)) {
961 SBP2_ERR("sbp2util_create_command_orb_pool failed!");
962 sbp2_remove_device(scsi_id);
963 return -ENOMEM;
964 }
965
966 /* Schedule a timeout here. The reason is that we may be so close
967 * to a bus reset, that the device is not available for logins.
968 * This can happen when the bus reset is caused by the host
969 * connected to the sbp2 device being removed. That host would
970 * have a certain amount of time to relogin before the sbp2 device
971 * allows someone else to login instead. One second makes sense. */
972 msleep_interruptible(1000);
973 if (signal_pending(current)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 sbp2_remove_device(scsi_id);
975 return -EINTR;
976 }
Stefan Richtera237f352005-11-07 06:31:39 -0500977
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 /*
979 * Login to the sbp-2 device
980 */
981 if (sbp2_login_device(scsi_id)) {
982 /* Login failed, just remove the device. */
983 sbp2_remove_device(scsi_id);
984 return -EBUSY;
985 }
986
987 /*
988 * Set max retries to something large on the device
989 */
990 sbp2_set_busy_timeout(scsi_id);
991
992 /*
993 * Do a SBP-2 fetch agent reset
994 */
995 sbp2_agent_reset(scsi_id, 1);
996
997 /*
998 * Get the max speed and packet size that we can use
999 */
1000 sbp2_max_speed_and_size(scsi_id);
1001
1002 /* Add this device to the scsi layer now */
James Bottomley146f7262005-09-10 12:44:09 -05001003 error = scsi_add_device(scsi_id->scsi_host, 0, scsi_id->ud->id, 0);
1004 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 SBP2_ERR("scsi_add_device failed");
Stefan Richterdc3edd52005-12-12 23:03:30 -05001006 sbp2_logout_device(scsi_id);
1007 sbp2_remove_device(scsi_id);
James Bottomley146f7262005-09-10 12:44:09 -05001008 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010
1011 return 0;
Stefan Richtereaceec72005-12-13 11:05:05 -05001012
1013alloc_fail:
1014 SBP2_ERR("Could not allocate memory for scsi_id");
1015 sbp2_remove_device(scsi_id);
1016 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
1019/*
1020 * This function removes an sbp2 device from the sbp2scsi_host_info struct.
1021 */
1022static void sbp2_remove_device(struct scsi_id_instance_data *scsi_id)
1023{
1024 struct sbp2scsi_host_info *hi;
1025
Stefan Richterd024ebc2006-03-28 20:03:55 -05001026 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 if (!scsi_id)
1029 return;
1030
1031 hi = scsi_id->hi;
1032
1033 /* This will remove our scsi device aswell */
1034 if (scsi_id->scsi_host) {
1035 scsi_remove_host(scsi_id->scsi_host);
1036 scsi_host_put(scsi_id->scsi_host);
1037 }
1038
1039 sbp2util_remove_command_orb_pool(scsi_id);
1040
1041 list_del(&scsi_id->scsi_list);
1042
1043 if (scsi_id->login_response) {
1044 pci_free_consistent(hi->host->pdev,
1045 sizeof(struct sbp2_login_response),
1046 scsi_id->login_response,
1047 scsi_id->login_response_dma);
1048 SBP2_DMA_FREE("single login FIFO");
1049 }
1050
1051 if (scsi_id->login_orb) {
1052 pci_free_consistent(hi->host->pdev,
1053 sizeof(struct sbp2_login_orb),
1054 scsi_id->login_orb,
1055 scsi_id->login_orb_dma);
1056 SBP2_DMA_FREE("single login ORB");
1057 }
1058
1059 if (scsi_id->reconnect_orb) {
1060 pci_free_consistent(hi->host->pdev,
1061 sizeof(struct sbp2_reconnect_orb),
1062 scsi_id->reconnect_orb,
1063 scsi_id->reconnect_orb_dma);
1064 SBP2_DMA_FREE("single reconnect orb");
1065 }
1066
1067 if (scsi_id->logout_orb) {
1068 pci_free_consistent(hi->host->pdev,
1069 sizeof(struct sbp2_logout_orb),
1070 scsi_id->logout_orb,
1071 scsi_id->logout_orb_dma);
1072 SBP2_DMA_FREE("single logout orb");
1073 }
1074
1075 if (scsi_id->query_logins_orb) {
1076 pci_free_consistent(hi->host->pdev,
1077 sizeof(struct sbp2_query_logins_orb),
1078 scsi_id->query_logins_orb,
1079 scsi_id->query_logins_orb_dma);
1080 SBP2_DMA_FREE("single query logins orb");
1081 }
1082
1083 if (scsi_id->query_logins_response) {
1084 pci_free_consistent(hi->host->pdev,
1085 sizeof(struct sbp2_query_logins_response),
1086 scsi_id->query_logins_response,
1087 scsi_id->query_logins_response_dma);
1088 SBP2_DMA_FREE("single query logins data");
1089 }
1090
Stefan Richter35bdddb2006-01-31 00:13:33 -05001091 if (scsi_id->status_fifo_addr)
1092 hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
1093 scsi_id->status_fifo_addr);
1094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 scsi_id->ud->device.driver_data = NULL;
1096
Stefan Richter147830f2006-03-28 19:54:52 -05001097 if (hi)
1098 module_put(hi->host->driver->owner);
1099
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 SBP2_DEBUG("SBP-2 device removed, SCSI ID = %d", scsi_id->ud->id);
1101
1102 kfree(scsi_id);
1103}
1104
1105#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
1106/*
1107 * This function deals with physical dma write requests (for adapters that do not support
1108 * physical dma in hardware). Mostly just here for debugging...
1109 */
Stefan Richtera237f352005-11-07 06:31:39 -05001110static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
1111 int destid, quadlet_t *data, u64 addr,
1112 size_t length, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113{
1114
Stefan Richtera237f352005-11-07 06:31:39 -05001115 /*
1116 * Manually put the data in the right place.
1117 */
1118 memcpy(bus_to_virt((u32) addr), data, length);
1119 sbp2util_packet_dump(data, length, "sbp2 phys dma write by device",
1120 (u32) addr);
1121 return RCODE_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122}
1123
1124/*
1125 * This function deals with physical dma read requests (for adapters that do not support
1126 * physical dma in hardware). Mostly just here for debugging...
1127 */
Stefan Richtera237f352005-11-07 06:31:39 -05001128static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
1129 quadlet_t *data, u64 addr, size_t length,
1130 u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
1132
Stefan Richtera237f352005-11-07 06:31:39 -05001133 /*
1134 * Grab data from memory and send a read response.
1135 */
1136 memcpy(data, bus_to_virt((u32) addr), length);
1137 sbp2util_packet_dump(data, length, "sbp2 phys dma read by device",
1138 (u32) addr);
1139 return RCODE_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141#endif
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143/**************************************
1144 * SBP-2 protocol related section
1145 **************************************/
1146
1147/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 * This function queries the device for the maximum concurrent logins it
1149 * supports.
1150 */
1151static int sbp2_query_logins(struct scsi_id_instance_data *scsi_id)
1152{
1153 struct sbp2scsi_host_info *hi = scsi_id->hi;
1154 quadlet_t data[2];
1155 int max_logins;
1156 int active_logins;
1157
Stefan Richterd024ebc2006-03-28 20:03:55 -05001158 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
1160 scsi_id->query_logins_orb->reserved1 = 0x0;
1161 scsi_id->query_logins_orb->reserved2 = 0x0;
1162
1163 scsi_id->query_logins_orb->query_response_lo = scsi_id->query_logins_response_dma;
1164 scsi_id->query_logins_orb->query_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 scsi_id->query_logins_orb->lun_misc = ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
1167 scsi_id->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
Ben Collinse309fc62005-11-07 06:31:34 -05001168 scsi_id->query_logins_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 scsi_id->query_logins_orb->reserved_resp_length =
1171 ORB_SET_QUERY_LOGINS_RESP_LENGTH(sizeof(struct sbp2_query_logins_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Stefan Richter35bdddb2006-01-31 00:13:33 -05001173 scsi_id->query_logins_orb->status_fifo_hi =
1174 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1175 scsi_id->query_logins_orb->status_fifo_lo =
1176 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
1178 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb));
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 sbp2util_packet_dump(scsi_id->query_logins_orb, sizeof(struct sbp2_query_logins_orb),
1181 "sbp2 query logins orb", scsi_id->query_logins_orb_dma);
1182
1183 memset(scsi_id->query_logins_response, 0, sizeof(struct sbp2_query_logins_response));
1184 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1187 data[1] = scsi_id->query_logins_orb_dma;
1188 sbp2util_cpu_to_be32_buffer(data, 8);
1189
1190 atomic_set(&scsi_id->sbp2_login_complete, 0);
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, 2*HZ)) {
1195 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001196 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 }
1198
1199 if (scsi_id->status_block.ORB_offset_lo != scsi_id->query_logins_orb_dma) {
1200 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001201 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 }
1203
1204 if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1205 STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1206 STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1207
1208 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001209 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 }
1211
1212 sbp2util_cpu_to_be32_buffer(scsi_id->query_logins_response, sizeof(struct sbp2_query_logins_response));
1213
1214 SBP2_DEBUG("length_max_logins = %x",
1215 (unsigned int)scsi_id->query_logins_response->length_max_logins);
1216
1217 SBP2_DEBUG("Query logins to SBP-2 device successful");
1218
1219 max_logins = RESPONSE_GET_MAX_LOGINS(scsi_id->query_logins_response->length_max_logins);
1220 SBP2_DEBUG("Maximum concurrent logins supported: %d", max_logins);
1221
1222 active_logins = RESPONSE_GET_ACTIVE_LOGINS(scsi_id->query_logins_response->length_max_logins);
1223 SBP2_DEBUG("Number of active logins: %d", active_logins);
1224
1225 if (active_logins >= max_logins) {
Stefan Richtera237f352005-11-07 06:31:39 -05001226 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228
1229 return 0;
1230}
1231
1232/*
1233 * This function is called in order to login to a particular SBP-2 device,
1234 * after a bus reset.
1235 */
1236static int sbp2_login_device(struct scsi_id_instance_data *scsi_id)
1237{
1238 struct sbp2scsi_host_info *hi = scsi_id->hi;
1239 quadlet_t data[2];
1240
Stefan Richterd024ebc2006-03-28 20:03:55 -05001241 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 if (!scsi_id->login_orb) {
Stefan Richterd024ebc2006-03-28 20:03:55 -05001244 SBP2_DEBUG("%s: login_orb not alloc'd!", __FUNCTION__);
Stefan Richtera237f352005-11-07 06:31:39 -05001245 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
1248 if (!exclusive_login) {
1249 if (sbp2_query_logins(scsi_id)) {
1250 SBP2_INFO("Device does not support any more concurrent logins");
Stefan Richtera237f352005-11-07 06:31:39 -05001251 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 }
1253 }
1254
1255 /* Set-up login ORB, assume no password */
1256 scsi_id->login_orb->password_hi = 0;
1257 scsi_id->login_orb->password_lo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
1260 scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
1263 scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0); /* One second reconnect time */
1264 scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(exclusive_login); /* Exclusive access to device */
1265 scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1); /* Notify us of login complete */
Ben Collinse309fc62005-11-07 06:31:34 -05001266 scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267
1268 scsi_id->login_orb->passwd_resp_lengths =
1269 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Stefan Richter35bdddb2006-01-31 00:13:33 -05001271 scsi_id->login_orb->status_fifo_hi =
1272 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1273 scsi_id->login_orb->status_fifo_lo =
1274 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
1277
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 sbp2util_packet_dump(scsi_id->login_orb, sizeof(struct sbp2_login_orb),
1279 "sbp2 login orb", scsi_id->login_orb_dma);
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
1282 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1283
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1285 data[1] = scsi_id->login_orb_dma;
1286 sbp2util_cpu_to_be32_buffer(data, 8);
1287
1288 atomic_set(&scsi_id->sbp2_login_complete, 0);
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291
1292 /*
1293 * Wait for login status (up to 20 seconds)...
1294 */
1295 if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, 20*HZ)) {
1296 SBP2_ERR("Error logging into SBP-2 device - login timed-out");
Stefan Richtera237f352005-11-07 06:31:39 -05001297 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299
1300 /*
1301 * Sanity. Make sure status returned matches login orb.
1302 */
1303 if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
1304 SBP2_ERR("Error logging into SBP-2 device - login timed-out");
Stefan Richtera237f352005-11-07 06:31:39 -05001305 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
1308 /*
1309 * Check status
1310 */
1311 if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1312 STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1313 STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1314
1315 SBP2_ERR("Error logging into SBP-2 device - login failed");
Stefan Richtera237f352005-11-07 06:31:39 -05001316 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318
1319 /*
1320 * Byte swap the login response, for use when reconnecting or
1321 * logging out.
1322 */
1323 sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response));
1324
1325 /*
1326 * Grab our command block agent address from the login response.
1327 */
1328 SBP2_DEBUG("command_block_agent_hi = %x",
1329 (unsigned int)scsi_id->login_response->command_block_agent_hi);
1330 SBP2_DEBUG("command_block_agent_lo = %x",
1331 (unsigned int)scsi_id->login_response->command_block_agent_lo);
1332
1333 scsi_id->sbp2_command_block_agent_addr =
1334 ((u64)scsi_id->login_response->command_block_agent_hi) << 32;
1335 scsi_id->sbp2_command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
1336 scsi_id->sbp2_command_block_agent_addr &= 0x0000ffffffffffffULL;
1337
1338 SBP2_INFO("Logged into SBP-2 device");
1339
Stefan Richtera237f352005-11-07 06:31:39 -05001340 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
1342}
1343
1344/*
1345 * This function is called in order to logout from a particular SBP-2
1346 * device, usually called during driver unload.
1347 */
1348static int sbp2_logout_device(struct scsi_id_instance_data *scsi_id)
1349{
1350 struct sbp2scsi_host_info *hi = scsi_id->hi;
1351 quadlet_t data[2];
1352 int error;
1353
Stefan Richterd024ebc2006-03-28 20:03:55 -05001354 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355
1356 /*
1357 * Set-up logout ORB
1358 */
1359 scsi_id->logout_orb->reserved1 = 0x0;
1360 scsi_id->logout_orb->reserved2 = 0x0;
1361 scsi_id->logout_orb->reserved3 = 0x0;
1362 scsi_id->logout_orb->reserved4 = 0x0;
1363
1364 scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
1365 scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1366
1367 /* Notify us when complete */
1368 scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1369
1370 scsi_id->logout_orb->reserved5 = 0x0;
Stefan Richter35bdddb2006-01-31 00:13:33 -05001371 scsi_id->logout_orb->status_fifo_hi =
1372 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1373 scsi_id->logout_orb->status_fifo_lo =
1374 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375
1376 /*
1377 * Byte swap ORB if necessary
1378 */
1379 sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb));
1380
1381 sbp2util_packet_dump(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb),
1382 "sbp2 logout orb", scsi_id->logout_orb_dma);
1383
1384 /*
1385 * Ok, let's write to the target's management agent register
1386 */
1387 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1388 data[1] = scsi_id->logout_orb_dma;
1389 sbp2util_cpu_to_be32_buffer(data, 8);
1390
1391 atomic_set(&scsi_id->sbp2_login_complete, 0);
1392
1393 error = hpsb_node_write(scsi_id->ne,
Stefan Richtera237f352005-11-07 06:31:39 -05001394 scsi_id->sbp2_management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 if (error)
1396 return error;
1397
1398 /* Wait for device to logout...1 second. */
1399 if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, HZ))
1400 return -EIO;
1401
1402 SBP2_INFO("Logged out of SBP-2 device");
1403
Stefan Richtera237f352005-11-07 06:31:39 -05001404 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
1406}
1407
1408/*
1409 * This function is called in order to reconnect to a particular SBP-2
1410 * device, after a bus reset.
1411 */
1412static int sbp2_reconnect_device(struct scsi_id_instance_data *scsi_id)
1413{
1414 struct sbp2scsi_host_info *hi = scsi_id->hi;
1415 quadlet_t data[2];
1416 int error;
1417
Stefan Richterd024ebc2006-03-28 20:03:55 -05001418 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 /*
1421 * Set-up reconnect ORB
1422 */
1423 scsi_id->reconnect_orb->reserved1 = 0x0;
1424 scsi_id->reconnect_orb->reserved2 = 0x0;
1425 scsi_id->reconnect_orb->reserved3 = 0x0;
1426 scsi_id->reconnect_orb->reserved4 = 0x0;
1427
1428 scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
1429 scsi_id->reconnect_orb->login_ID_misc |=
1430 ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1431
1432 /* Notify us when complete */
1433 scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1434
1435 scsi_id->reconnect_orb->reserved5 = 0x0;
Stefan Richter35bdddb2006-01-31 00:13:33 -05001436 scsi_id->reconnect_orb->status_fifo_hi =
1437 ORB_SET_STATUS_FIFO_HI(scsi_id->status_fifo_addr, hi->host->node_id);
1438 scsi_id->reconnect_orb->status_fifo_lo =
1439 ORB_SET_STATUS_FIFO_LO(scsi_id->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
1441 /*
1442 * Byte swap ORB if necessary
1443 */
1444 sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
1445
1446 sbp2util_packet_dump(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb),
1447 "sbp2 reconnect orb", scsi_id->reconnect_orb_dma);
1448
1449 /*
1450 * Initialize status fifo
1451 */
1452 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1453
1454 /*
1455 * Ok, let's write to the target's management agent register
1456 */
1457 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1458 data[1] = scsi_id->reconnect_orb_dma;
1459 sbp2util_cpu_to_be32_buffer(data, 8);
1460
1461 atomic_set(&scsi_id->sbp2_login_complete, 0);
1462
1463 error = hpsb_node_write(scsi_id->ne,
Stefan Richtera237f352005-11-07 06:31:39 -05001464 scsi_id->sbp2_management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 if (error)
1466 return error;
1467
1468 /*
1469 * Wait for reconnect status (up to 1 second)...
1470 */
1471 if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, HZ)) {
1472 SBP2_ERR("Error reconnecting to SBP-2 device - reconnect timed-out");
Stefan Richtera237f352005-11-07 06:31:39 -05001473 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 }
1475
1476 /*
1477 * Sanity. Make sure status returned matches reconnect orb.
1478 */
1479 if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
1480 SBP2_ERR("Error reconnecting to SBP-2 device - reconnect timed-out");
Stefan Richtera237f352005-11-07 06:31:39 -05001481 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
1483
1484 /*
1485 * Check status
1486 */
1487 if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1488 STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1489 STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1490
1491 SBP2_ERR("Error reconnecting to SBP-2 device - reconnect failed");
Stefan Richtera237f352005-11-07 06:31:39 -05001492 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494
1495 HPSB_DEBUG("Reconnected to SBP-2 device");
1496
Stefan Richtera237f352005-11-07 06:31:39 -05001497 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498
1499}
1500
1501/*
1502 * This function is called in order to set the busy timeout (number of
1503 * retries to attempt) on the sbp2 device.
1504 */
1505static int sbp2_set_busy_timeout(struct scsi_id_instance_data *scsi_id)
1506{
1507 quadlet_t data;
1508
Stefan Richterd024ebc2006-03-28 20:03:55 -05001509 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
Stefan Richterd024ebc2006-03-28 20:03:55 -05001512 if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
1513 SBP2_ERR("%s error", __FUNCTION__);
Stefan Richtera237f352005-11-07 06:31:39 -05001514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515}
1516
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517/*
1518 * This function is called to parse sbp2 device's config rom unit
1519 * directory. Used to determine things like sbp2 management agent offset,
1520 * and command set used (SCSI or RBC).
1521 */
1522static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id,
1523 struct unit_directory *ud)
1524{
1525 struct csr1212_keyval *kv;
1526 struct csr1212_dentry *dentry;
1527 u64 management_agent_addr;
1528 u32 command_set_spec_id, command_set, unit_characteristics,
Stefan Richter24d3bf82006-05-15 22:04:59 +02001529 firmware_revision;
1530 unsigned workarounds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 int i;
1532
Stefan Richterd024ebc2006-03-28 20:03:55 -05001533 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534
1535 management_agent_addr = 0x0;
1536 command_set_spec_id = 0x0;
1537 command_set = 0x0;
1538 unit_characteristics = 0x0;
1539 firmware_revision = 0x0;
1540
1541 /* Handle different fields in the unit directory, based on keys */
1542 csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
1543 switch (kv->key.id) {
1544 case CSR1212_KV_ID_DEPENDENT_INFO:
1545 if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET) {
1546 /* Save off the management agent address */
1547 management_agent_addr =
Stefan Richtera237f352005-11-07 06:31:39 -05001548 CSR1212_REGISTER_SPACE_BASE +
1549 (kv->value.csr_offset << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550
1551 SBP2_DEBUG("sbp2_management_agent_addr = %x",
Stefan Richtera237f352005-11-07 06:31:39 -05001552 (unsigned int)management_agent_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 } else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE) {
Stefan Richtera237f352005-11-07 06:31:39 -05001554 scsi_id->sbp2_lun =
1555 ORB_SET_LUN(kv->value.immediate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
1557 break;
1558
1559 case SBP2_COMMAND_SET_SPEC_ID_KEY:
1560 /* Command spec organization */
1561 command_set_spec_id = kv->value.immediate;
1562 SBP2_DEBUG("sbp2_command_set_spec_id = %x",
Stefan Richtera237f352005-11-07 06:31:39 -05001563 (unsigned int)command_set_spec_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 break;
1565
1566 case SBP2_COMMAND_SET_KEY:
1567 /* Command set used by sbp2 device */
1568 command_set = kv->value.immediate;
1569 SBP2_DEBUG("sbp2_command_set = %x",
Stefan Richtera237f352005-11-07 06:31:39 -05001570 (unsigned int)command_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 break;
1572
1573 case SBP2_UNIT_CHARACTERISTICS_KEY:
1574 /*
1575 * Unit characterisitcs (orb related stuff
1576 * that I'm not yet paying attention to)
1577 */
1578 unit_characteristics = kv->value.immediate;
1579 SBP2_DEBUG("sbp2_unit_characteristics = %x",
Stefan Richtera237f352005-11-07 06:31:39 -05001580 (unsigned int)unit_characteristics);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 break;
1582
1583 case SBP2_FIRMWARE_REVISION_KEY:
1584 /* Firmware revision */
1585 firmware_revision = kv->value.immediate;
Stefan Richter24d3bf82006-05-15 22:04:59 +02001586 SBP2_DEBUG("sbp2_firmware_revision = %x",
1587 (unsigned int)firmware_revision);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 break;
1589
1590 default:
1591 break;
1592 }
1593 }
1594
Stefan Richter24d3bf82006-05-15 22:04:59 +02001595 workarounds = sbp2_default_workarounds;
1596 if (force_inquiry_hack) {
1597 SBP2_WARN("force_inquiry_hack is deprecated. "
1598 "Use parameter 'workarounds' instead.");
1599 workarounds |= SBP2_WORKAROUND_INQUIRY_36;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 }
1601
Stefan Richter679c0cd2006-05-15 22:08:09 +02001602 if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
1603 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
1604 if (sbp2_workarounds_table[i].firmware_revision &&
1605 sbp2_workarounds_table[i].firmware_revision !=
1606 (firmware_revision & 0xffff00))
1607 continue;
1608 if (sbp2_workarounds_table[i].model_id &&
1609 sbp2_workarounds_table[i].model_id != ud->model_id)
1610 continue;
1611 workarounds |= sbp2_workarounds_table[i].workarounds;
1612 break;
1613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Stefan Richter24d3bf82006-05-15 22:04:59 +02001615 if (workarounds)
Stefan Richtere9a1c522006-05-15 22:06:37 +02001616 SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
1617 "(firmware_revision 0x%06x, vendor_id 0x%06x,"
1618 " model_id 0x%06x)",
Stefan Richter24d3bf82006-05-15 22:04:59 +02001619 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
Stefan Richtere9a1c522006-05-15 22:06:37 +02001620 workarounds, firmware_revision,
1621 ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
1622 ud->model_id);
Stefan Richter24d3bf82006-05-15 22:04:59 +02001623
1624 /* We would need one SCSI host template for each target to adjust
1625 * max_sectors on the fly, therefore warn only. */
1626 if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
1627 (max_sectors * 512) > (128 * 1024))
1628 SBP2_WARN("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
1629 "max transfer size. WARNING: Current max_sectors "
1630 "setting is larger than 128KB (%d sectors)",
1631 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
1632 max_sectors);
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 /* If this is a logical unit directory entry, process the parent
1635 * to get the values. */
1636 if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
1637 struct unit_directory *parent_ud =
1638 container_of(ud->device.parent, struct unit_directory, device);
1639 sbp2_parse_unit_directory(scsi_id, parent_ud);
1640 } else {
1641 scsi_id->sbp2_management_agent_addr = management_agent_addr;
1642 scsi_id->sbp2_command_set_spec_id = command_set_spec_id;
1643 scsi_id->sbp2_command_set = command_set;
1644 scsi_id->sbp2_unit_characteristics = unit_characteristics;
1645 scsi_id->sbp2_firmware_revision = firmware_revision;
1646 scsi_id->workarounds = workarounds;
1647 if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
Ben Collinse309fc62005-11-07 06:31:34 -05001648 scsi_id->sbp2_lun = ORB_SET_LUN(ud->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
1650}
1651
1652/*
1653 * This function is called in order to determine the max speed and packet
1654 * size we can use in our ORBs. Note, that we (the driver and host) only
1655 * initiate the transaction. The SBP-2 device actually transfers the data
1656 * (by reading from the DMA area we tell it). This means that the SBP-2
1657 * device decides the actual maximum data it can transfer. We just tell it
1658 * the speed that it needs to use, and the max_rec the host supports, and
1659 * it takes care of the rest.
1660 */
1661static int sbp2_max_speed_and_size(struct scsi_id_instance_data *scsi_id)
1662{
1663 struct sbp2scsi_host_info *hi = scsi_id->hi;
1664
Stefan Richterd024ebc2006-03-28 20:03:55 -05001665 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667 /* Initial setting comes from the hosts speed map */
Stefan Richtera237f352005-11-07 06:31:39 -05001668 scsi_id->speed_code =
1669 hi->host->speed_map[NODEID_TO_NODE(hi->host->node_id) * 64 +
1670 NODEID_TO_NODE(scsi_id->ne->nodeid)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
1672 /* Bump down our speed if the user requested it */
1673 if (scsi_id->speed_code > max_speed) {
1674 scsi_id->speed_code = max_speed;
1675 SBP2_ERR("Forcing SBP-2 max speed down to %s",
1676 hpsb_speedto_str[scsi_id->speed_code]);
1677 }
1678
1679 /* Payload size is the lesser of what our speed supports and what
1680 * our host supports. */
Stefan Richtera237f352005-11-07 06:31:39 -05001681 scsi_id->max_payload_size =
1682 min(sbp2_speedto_max_payload[scsi_id->speed_code],
1683 (u8) (hi->host->csr.max_rec - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 HPSB_DEBUG("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
1686 NODE_BUS_ARGS(hi->host, scsi_id->ne->nodeid),
1687 hpsb_speedto_str[scsi_id->speed_code],
Stefan Richtera237f352005-11-07 06:31:39 -05001688 1 << ((u32) scsi_id->max_payload_size + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689
Stefan Richtera237f352005-11-07 06:31:39 -05001690 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691}
1692
1693/*
1694 * This function is called in order to perform a SBP-2 agent reset.
1695 */
1696static int sbp2_agent_reset(struct scsi_id_instance_data *scsi_id, int wait)
1697{
1698 quadlet_t data;
1699 u64 addr;
1700 int retval;
1701
Stefan Richterd024ebc2006-03-28 20:03:55 -05001702 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 data = ntohl(SBP2_AGENT_RESET_DATA);
1705 addr = scsi_id->sbp2_command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
1706
1707 if (wait)
1708 retval = hpsb_node_write(scsi_id->ne, addr, &data, 4);
1709 else
1710 retval = sbp2util_node_write_no_wait(scsi_id->ne, addr, &data, 4);
1711
1712 if (retval < 0) {
1713 SBP2_ERR("hpsb_node_write failed.\n");
1714 return -EIO;
1715 }
1716
1717 /*
1718 * Need to make sure orb pointer is written on next command
1719 */
1720 scsi_id->last_orb = NULL;
1721
Stefan Richtera237f352005-11-07 06:31:39 -05001722 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723}
1724
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001725static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
1726 struct sbp2scsi_host_info *hi,
1727 struct sbp2_command_info *command,
1728 unsigned int scsi_use_sg,
1729 struct scatterlist *sgpnt,
1730 u32 orb_direction,
1731 enum dma_data_direction dma_dir)
1732{
1733 command->dma_dir = dma_dir;
1734 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1735 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1736
1737 /* Special case if only one element (and less than 64KB in size) */
1738 if ((scsi_use_sg == 1) &&
1739 (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
1740
1741 SBP2_DEBUG("Only one s/g element");
1742 command->dma_size = sgpnt[0].length;
1743 command->dma_type = CMD_DMA_PAGE;
1744 command->cmd_dma = pci_map_page(hi->host->pdev,
1745 sgpnt[0].page,
1746 sgpnt[0].offset,
1747 command->dma_size,
1748 command->dma_dir);
1749 SBP2_DMA_ALLOC("single page scatter element");
1750
1751 orb->data_descriptor_lo = command->cmd_dma;
1752 orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
1753
1754 } else {
1755 struct sbp2_unrestricted_page_table *sg_element =
1756 &command->scatter_gather_element[0];
1757 u32 sg_count, sg_len;
1758 dma_addr_t sg_addr;
1759 int i, count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg,
1760 dma_dir);
1761
1762 SBP2_DMA_ALLOC("scatter list");
1763
1764 command->dma_size = scsi_use_sg;
1765 command->sge_buffer = sgpnt;
1766
1767 /* use page tables (s/g) */
1768 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1769 orb->data_descriptor_lo = command->sge_dma;
1770
1771 /*
1772 * Loop through and fill out our sbp-2 page tables
1773 * (and split up anything too large)
1774 */
1775 for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
1776 sg_len = sg_dma_len(sgpnt);
1777 sg_addr = sg_dma_address(sgpnt);
1778 while (sg_len) {
1779 sg_element[sg_count].segment_base_lo = sg_addr;
1780 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1781 sg_element[sg_count].length_segment_base_hi =
1782 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1783 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1784 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1785 } else {
1786 sg_element[sg_count].length_segment_base_hi =
1787 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1788 sg_len = 0;
1789 }
1790 sg_count++;
1791 }
1792 }
1793
1794 /* Number of page table (s/g) elements */
1795 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1796
1797 sbp2util_packet_dump(sg_element,
1798 (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
1799 "sbp2 s/g list", command->sge_dma);
1800
1801 /* Byte swap page tables if necessary */
1802 sbp2util_cpu_to_be32_buffer(sg_element,
1803 (sizeof(struct sbp2_unrestricted_page_table)) *
1804 sg_count);
1805 }
1806}
1807
1808static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
1809 struct sbp2scsi_host_info *hi,
1810 struct sbp2_command_info *command,
1811 struct scatterlist *sgpnt,
1812 u32 orb_direction,
1813 unsigned int scsi_request_bufflen,
1814 void *scsi_request_buffer,
1815 enum dma_data_direction dma_dir)
1816{
1817 command->dma_dir = dma_dir;
1818 command->dma_size = scsi_request_bufflen;
1819 command->dma_type = CMD_DMA_SINGLE;
1820 command->cmd_dma = pci_map_single(hi->host->pdev, scsi_request_buffer,
1821 command->dma_size, command->dma_dir);
1822 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1823 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1824
1825 SBP2_DMA_ALLOC("single bulk");
1826
1827 /*
1828 * Handle case where we get a command w/o s/g enabled (but
1829 * check for transfers larger than 64K)
1830 */
1831 if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1832
1833 orb->data_descriptor_lo = command->cmd_dma;
1834 orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
1835
1836 } else {
1837 struct sbp2_unrestricted_page_table *sg_element =
1838 &command->scatter_gather_element[0];
1839 u32 sg_count, sg_len;
1840 dma_addr_t sg_addr;
1841
1842 /*
1843 * Need to turn this into page tables, since the
1844 * buffer is too large.
1845 */
1846 orb->data_descriptor_lo = command->sge_dma;
1847
1848 /* Use page tables (s/g) */
1849 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1850
1851 /*
1852 * fill out our sbp-2 page tables (and split up
1853 * the large buffer)
1854 */
1855 sg_count = 0;
1856 sg_len = scsi_request_bufflen;
1857 sg_addr = command->cmd_dma;
1858 while (sg_len) {
1859 sg_element[sg_count].segment_base_lo = sg_addr;
1860 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1861 sg_element[sg_count].length_segment_base_hi =
1862 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1863 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1864 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1865 } else {
1866 sg_element[sg_count].length_segment_base_hi =
1867 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1868 sg_len = 0;
1869 }
1870 sg_count++;
1871 }
1872
1873 /* Number of page table (s/g) elements */
1874 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1875
1876 sbp2util_packet_dump(sg_element,
1877 (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
1878 "sbp2 s/g list", command->sge_dma);
1879
1880 /* Byte swap page tables if necessary */
1881 sbp2util_cpu_to_be32_buffer(sg_element,
1882 (sizeof(struct sbp2_unrestricted_page_table)) *
1883 sg_count);
1884 }
1885}
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887/*
1888 * This function is called to create the actual command orb and s/g list
1889 * out of the scsi command itself.
1890 */
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001891static void sbp2_create_command_orb(struct scsi_id_instance_data *scsi_id,
1892 struct sbp2_command_info *command,
1893 unchar *scsi_cmd,
1894 unsigned int scsi_use_sg,
1895 unsigned int scsi_request_bufflen,
1896 void *scsi_request_buffer,
1897 enum dma_data_direction dma_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898{
1899 struct sbp2scsi_host_info *hi = scsi_id->hi;
Stefan Richtera237f352005-11-07 06:31:39 -05001900 struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 struct sbp2_command_orb *command_orb = &command->command_orb;
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001902 u32 orb_direction;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 /*
1905 * Set-up our command ORB..
1906 *
1907 * NOTE: We're doing unrestricted page tables (s/g), as this is
1908 * best performance (at least with the devices I have). This means
1909 * that data_size becomes the number of s/g elements, and
1910 * page_size should be zero (for unrestricted).
1911 */
1912 command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1913 command_orb->next_ORB_lo = 0x0;
1914 command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size);
1915 command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code);
Stefan Richtera237f352005-11-07 06:31:39 -05001916 command_orb->misc |= ORB_SET_NOTIFY(1); /* Notify us when complete */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Stefan Richter43863eb2005-12-12 23:03:24 -05001918 if (dma_dir == DMA_NONE)
Stefan Richtera237f352005-11-07 06:31:39 -05001919 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
Stefan Richter43863eb2005-12-12 23:03:24 -05001920 else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
Stefan Richtera237f352005-11-07 06:31:39 -05001921 orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
Stefan Richter43863eb2005-12-12 23:03:24 -05001922 else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
Stefan Richtera237f352005-11-07 06:31:39 -05001923 orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
Stefan Richter43863eb2005-12-12 23:03:24 -05001924 else {
1925 SBP2_WARN("Falling back to DMA_NONE");
1926 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 }
1928
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001929 /* Set-up our pagetable stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 SBP2_DEBUG("No data transfer");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 command_orb->data_descriptor_hi = 0x0;
1933 command_orb->data_descriptor_lo = 0x0;
1934 command_orb->misc |= ORB_SET_DIRECTION(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 } else if (scsi_use_sg) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 SBP2_DEBUG("Use scatter/gather");
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001937 sbp2_prep_command_orb_sg(command_orb, hi, command, scsi_use_sg,
1938 sgpnt, orb_direction, dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 SBP2_DEBUG("No scatter/gather");
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001941 sbp2_prep_command_orb_no_sg(command_orb, hi, command, sgpnt,
1942 orb_direction, scsi_request_bufflen,
1943 scsi_request_buffer, dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 }
1945
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001946 /* Byte swap command ORB if necessary */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
1948
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001949 /* Put our scsi command in the command ORB */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 memset(command_orb->cdb, 0, 12);
1951 memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952}
1953
1954/*
1955 * This function is called in order to begin a regular SBP-2 command.
1956 */
1957static int sbp2_link_orb_command(struct scsi_id_instance_data *scsi_id,
1958 struct sbp2_command_info *command)
1959{
1960 struct sbp2scsi_host_info *hi = scsi_id->hi;
1961 struct sbp2_command_orb *command_orb = &command->command_orb;
1962 struct node_entry *ne = scsi_id->ne;
1963 u64 addr;
1964
1965 outstanding_orb_incr;
1966 SBP2_ORB_DEBUG("sending command orb %p, total orbs = %x",
Stefan Richtera237f352005-11-07 06:31:39 -05001967 command_orb, global_outstanding_command_orbs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968
1969 pci_dma_sync_single_for_device(hi->host->pdev, command->command_orb_dma,
1970 sizeof(struct sbp2_command_orb),
1971 PCI_DMA_BIDIRECTIONAL);
1972 pci_dma_sync_single_for_device(hi->host->pdev, command->sge_dma,
1973 sizeof(command->scatter_gather_element),
1974 PCI_DMA_BIDIRECTIONAL);
1975 /*
1976 * Check to see if there are any previous orbs to use
1977 */
1978 if (scsi_id->last_orb == NULL) {
1979 quadlet_t data[2];
1980
1981 /*
1982 * Ok, let's write to the target's management agent register
1983 */
1984 addr = scsi_id->sbp2_command_block_agent_addr + SBP2_ORB_POINTER_OFFSET;
1985 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1986 data[1] = command->command_orb_dma;
1987 sbp2util_cpu_to_be32_buffer(data, 8);
1988
1989 SBP2_ORB_DEBUG("write command agent, command orb %p", command_orb);
1990
1991 if (sbp2util_node_write_no_wait(ne, addr, data, 8) < 0) {
1992 SBP2_ERR("sbp2util_node_write_no_wait failed.\n");
1993 return -EIO;
1994 }
1995
1996 SBP2_ORB_DEBUG("write command agent complete");
1997
1998 scsi_id->last_orb = command_orb;
1999 scsi_id->last_orb_dma = command->command_orb_dma;
2000
2001 } else {
2002 quadlet_t data;
2003
2004 /*
2005 * We have an orb already sent (maybe or maybe not
2006 * processed) that we can append this orb to. So do so,
2007 * and ring the doorbell. Have to be very careful
2008 * modifying these next orb pointers, as they are accessed
2009 * both by the sbp2 device and us.
2010 */
2011 scsi_id->last_orb->next_ORB_lo =
Stefan Richtera237f352005-11-07 06:31:39 -05002012 cpu_to_be32(command->command_orb_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 /* Tells hardware that this pointer is valid */
2014 scsi_id->last_orb->next_ORB_hi = 0x0;
Stefan Richtera237f352005-11-07 06:31:39 -05002015 pci_dma_sync_single_for_device(hi->host->pdev,
2016 scsi_id->last_orb_dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 sizeof(struct sbp2_command_orb),
2018 PCI_DMA_BIDIRECTIONAL);
2019
2020 /*
2021 * Ring the doorbell
2022 */
2023 data = cpu_to_be32(command->command_orb_dma);
2024 addr = scsi_id->sbp2_command_block_agent_addr + SBP2_DOORBELL_OFFSET;
2025
2026 SBP2_ORB_DEBUG("ring doorbell, command orb %p", command_orb);
2027
2028 if (sbp2util_node_write_no_wait(ne, addr, &data, 4) < 0) {
2029 SBP2_ERR("sbp2util_node_write_no_wait failed");
Stefan Richtera237f352005-11-07 06:31:39 -05002030 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 }
2032
2033 scsi_id->last_orb = command_orb;
2034 scsi_id->last_orb_dma = command->command_orb_dma;
2035
2036 }
Stefan Richtera237f352005-11-07 06:31:39 -05002037 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038}
2039
2040/*
2041 * This function is called in order to begin a regular SBP-2 command.
2042 */
2043static int sbp2_send_command(struct scsi_id_instance_data *scsi_id,
2044 struct scsi_cmnd *SCpnt,
2045 void (*done)(struct scsi_cmnd *))
2046{
2047 unchar *cmd = (unchar *) SCpnt->cmnd;
2048 unsigned int request_bufflen = SCpnt->request_bufflen;
2049 struct sbp2_command_info *command;
2050
Stefan Richterd024ebc2006-03-28 20:03:55 -05002051 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 SBP2_DEBUG("SCSI transfer size = %x", request_bufflen);
2053 SBP2_DEBUG("SCSI s/g elements = %x", (unsigned int)SCpnt->use_sg);
2054
2055 /*
2056 * Allocate a command orb and s/g structure
2057 */
2058 command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done);
2059 if (!command) {
Stefan Richtera237f352005-11-07 06:31:39 -05002060 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
2062
2063 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 * Now actually fill in the comamnd orb and sbp2 s/g list
2065 */
2066 sbp2_create_command_orb(scsi_id, command, cmd, SCpnt->use_sg,
2067 request_bufflen, SCpnt->request_buffer,
2068 SCpnt->sc_data_direction);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070 sbp2util_packet_dump(&command->command_orb, sizeof(struct sbp2_command_orb),
2071 "sbp2 command orb", command->command_orb_dma);
2072
2073 /*
2074 * Initialize status fifo
2075 */
2076 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
2077
2078 /*
2079 * Link up the orb, and ring the doorbell if needed
2080 */
2081 sbp2_link_orb_command(scsi_id, command);
2082
Stefan Richtera237f352005-11-07 06:31:39 -05002083 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084}
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 * Translates SBP-2 status into SCSI sense data for check conditions
2088 */
2089static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
2090{
Stefan Richterd024ebc2006-03-28 20:03:55 -05002091 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 /*
2094 * Ok, it's pretty ugly... ;-)
2095 */
2096 sense_data[0] = 0x70;
2097 sense_data[1] = 0x0;
2098 sense_data[2] = sbp2_status[9];
2099 sense_data[3] = sbp2_status[12];
2100 sense_data[4] = sbp2_status[13];
2101 sense_data[5] = sbp2_status[14];
2102 sense_data[6] = sbp2_status[15];
2103 sense_data[7] = 10;
2104 sense_data[8] = sbp2_status[16];
2105 sense_data[9] = sbp2_status[17];
2106 sense_data[10] = sbp2_status[18];
2107 sense_data[11] = sbp2_status[19];
2108 sense_data[12] = sbp2_status[10];
2109 sense_data[13] = sbp2_status[11];
2110 sense_data[14] = sbp2_status[20];
2111 sense_data[15] = sbp2_status[21];
2112
Stefan Richtera237f352005-11-07 06:31:39 -05002113 return sbp2_status[8] & 0x3f; /* return scsi status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
2116/*
2117 * This function is called after a command is completed, in order to do any necessary SBP-2
2118 * response data translations for the SCSI stack
2119 */
Stefan Richtera237f352005-11-07 06:31:39 -05002120static void sbp2_check_sbp2_response(struct scsi_id_instance_data *scsi_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 struct scsi_cmnd *SCpnt)
2122{
2123 u8 *scsi_buf = SCpnt->request_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Stefan Richterd024ebc2006-03-28 20:03:55 -05002125 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126
Al Viroe30809f2006-02-08 14:09:00 -05002127 if (SCpnt->cmnd[0] == INQUIRY && (SCpnt->cmnd[1] & 3) == 0) {
Stefan Richtera237f352005-11-07 06:31:39 -05002128 /*
2129 * Make sure data length is ok. Minimum length is 36 bytes
2130 */
2131 if (scsi_buf[4] == 0) {
2132 scsi_buf[4] = 36 - 5;
2133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
Stefan Richtera237f352005-11-07 06:31:39 -05002135 /*
2136 * Fix ansi revision and response data format
2137 */
2138 scsi_buf[2] |= 2;
2139 scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
2142
2143/*
2144 * This function deals with status writes from the SBP-2 device
2145 */
2146static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid, int destid,
2147 quadlet_t *data, u64 addr, size_t length, u16 fl)
2148{
2149 struct sbp2scsi_host_info *hi;
2150 struct scsi_id_instance_data *scsi_id = NULL, *scsi_id_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 struct scsi_cmnd *SCpnt = NULL;
2152 u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
2153 struct sbp2_command_info *command;
Jody McIntyre79456192005-11-07 06:29:39 -05002154 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Stefan Richterd024ebc2006-03-28 20:03:55 -05002156 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 sbp2util_packet_dump(data, length, "sbp2 status write by device", (u32)addr);
2159
2160 if (!host) {
2161 SBP2_ERR("host is NULL - this is bad!");
Stefan Richtera237f352005-11-07 06:31:39 -05002162 return RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
2164
2165 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
2166
2167 if (!hi) {
2168 SBP2_ERR("host info is NULL - this is bad!");
Stefan Richtera237f352005-11-07 06:31:39 -05002169 return RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 }
2171
2172 /*
Stefan Richter35bdddb2006-01-31 00:13:33 -05002173 * Find our scsi_id structure by looking at the status fifo address
2174 * written to by the sbp2 device.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 list_for_each_entry(scsi_id_tmp, &hi->scsi_ids, scsi_list) {
Stefan Richter35bdddb2006-01-31 00:13:33 -05002177 if (scsi_id_tmp->ne->nodeid == nodeid &&
2178 scsi_id_tmp->status_fifo_addr == addr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 scsi_id = scsi_id_tmp;
2180 break;
2181 }
2182 }
2183
2184 if (!scsi_id) {
2185 SBP2_ERR("scsi_id is NULL - device is gone?");
Stefan Richtera237f352005-11-07 06:31:39 -05002186 return RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 }
2188
2189 /*
2190 * Put response into scsi_id status fifo...
2191 */
2192 memcpy(&scsi_id->status_block, data, length);
2193
2194 /*
2195 * Byte swap first two quadlets (8 bytes) of status for processing
2196 */
2197 sbp2util_be32_to_cpu_buffer(&scsi_id->status_block, 8);
2198
2199 /*
2200 * Handle command ORB status here if necessary. First, need to match status with command.
2201 */
2202 command = sbp2util_find_command_for_orb(scsi_id, scsi_id->status_block.ORB_offset_lo);
2203 if (command) {
2204
2205 SBP2_DEBUG("Found status for command ORB");
2206 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
2207 sizeof(struct sbp2_command_orb),
2208 PCI_DMA_BIDIRECTIONAL);
2209 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
2210 sizeof(command->scatter_gather_element),
2211 PCI_DMA_BIDIRECTIONAL);
2212
2213 SBP2_ORB_DEBUG("matched command orb %p", &command->command_orb);
2214 outstanding_orb_decr;
2215
2216 /*
2217 * Matched status with command, now grab scsi command pointers and check status
2218 */
2219 SCpnt = command->Current_SCpnt;
Stefan Richter24c7cd02006-04-01 21:11:41 +02002220 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 sbp2util_mark_command_completed(scsi_id, command);
Stefan Richter24c7cd02006-04-01 21:11:41 +02002222 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
2224 if (SCpnt) {
2225
2226 /*
2227 * See if the target stored any scsi status information
2228 */
2229 if (STATUS_GET_LENGTH(scsi_id->status_block.ORB_offset_hi_misc) > 1) {
2230 /*
2231 * Translate SBP-2 status to SCSI sense data
2232 */
2233 SBP2_DEBUG("CHECK CONDITION");
2234 scsi_status = sbp2_status_to_sense_data((unchar *)&scsi_id->status_block, SCpnt->sense_buffer);
2235 }
2236
2237 /*
2238 * Check to see if the dead bit is set. If so, we'll have to initiate
2239 * a fetch agent reset.
2240 */
2241 if (STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc)) {
2242
2243 /*
2244 * Initiate a fetch agent reset.
2245 */
2246 SBP2_DEBUG("Dead bit set - initiating fetch agent reset");
2247 sbp2_agent_reset(scsi_id, 0);
2248 }
2249
2250 SBP2_ORB_DEBUG("completing command orb %p", &command->command_orb);
2251 }
2252
2253 /*
2254 * Check here to see if there are no commands in-use. If there are none, we can
2255 * null out last orb so that next time around we write directly to the orb pointer...
2256 * Quick start saves one 1394 bus transaction.
2257 */
Jody McIntyre79456192005-11-07 06:29:39 -05002258 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 if (list_empty(&scsi_id->sbp2_command_orb_inuse)) {
2260 scsi_id->last_orb = NULL;
2261 }
Jody McIntyre79456192005-11-07 06:29:39 -05002262 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
2264 } else {
2265
2266 /*
2267 * It's probably a login/logout/reconnect status.
2268 */
2269 if ((scsi_id->login_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2270 (scsi_id->query_logins_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2271 (scsi_id->reconnect_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2272 (scsi_id->logout_orb_dma == scsi_id->status_block.ORB_offset_lo)) {
2273 atomic_set(&scsi_id->sbp2_login_complete, 1);
2274 }
2275 }
2276
2277 if (SCpnt) {
2278
2279 /* Complete the SCSI command. */
2280 SBP2_DEBUG("Completing SCSI command");
2281 sbp2scsi_complete_command(scsi_id, scsi_status, SCpnt,
2282 command->Current_done);
2283 SBP2_ORB_DEBUG("command orb completed");
2284 }
2285
Stefan Richtera237f352005-11-07 06:31:39 -05002286 return RCODE_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287}
2288
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289/**************************************
2290 * SCSI interface related section
2291 **************************************/
2292
2293/*
2294 * This routine is the main request entry routine for doing I/O. It is
2295 * called from the scsi stack directly.
2296 */
2297static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
2298 void (*done)(struct scsi_cmnd *))
2299{
2300 struct scsi_id_instance_data *scsi_id =
2301 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2302 struct sbp2scsi_host_info *hi;
Jody McIntyreabd559b2005-09-30 11:59:06 -07002303 int result = DID_NO_CONNECT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Stefan Richterd024ebc2006-03-28 20:03:55 -05002305 SBP2_DEBUG_ENTER();
2306#if (CONFIG_IEEE1394_SBP2_DEBUG >= 2) || defined(CONFIG_IEEE1394_SBP2_PACKET_DUMP)
2307 scsi_print_command(SCpnt);
2308#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Jody McIntyreabd559b2005-09-30 11:59:06 -07002310 if (!sbp2util_node_is_available(scsi_id))
2311 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
2313 hi = scsi_id->hi;
2314
2315 if (!hi) {
2316 SBP2_ERR("sbp2scsi_host_info is NULL - this is bad!");
Jody McIntyreabd559b2005-09-30 11:59:06 -07002317 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 }
2319
2320 /*
2321 * Until we handle multiple luns, just return selection time-out
2322 * to any IO directed at non-zero LUNs
2323 */
Jody McIntyreabd559b2005-09-30 11:59:06 -07002324 if (SCpnt->device->lun)
2325 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 /*
2328 * Check for request sense command, and handle it here
2329 * (autorequest sense)
2330 */
2331 if (SCpnt->cmnd[0] == REQUEST_SENSE) {
2332 SBP2_DEBUG("REQUEST_SENSE");
2333 memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen);
2334 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
2335 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done);
Jody McIntyreabd559b2005-09-30 11:59:06 -07002336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
2338
2339 /*
2340 * Check to see if we are in the middle of a bus reset.
2341 */
2342 if (!hpsb_node_entry_valid(scsi_id->ne)) {
2343 SBP2_ERR("Bus reset in progress - rejecting command");
Jody McIntyreabd559b2005-09-30 11:59:06 -07002344 result = DID_BUS_BUSY << 16;
2345 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
2347
2348 /*
Stefan Richter43863eb2005-12-12 23:03:24 -05002349 * Bidirectional commands are not yet implemented,
2350 * and unknown transfer direction not handled.
2351 */
2352 if (SCpnt->sc_data_direction == DMA_BIDIRECTIONAL) {
2353 SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
2354 result = DID_ERROR << 16;
2355 goto done;
2356 }
2357
2358 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 * Try and send our SCSI command
2360 */
2361 if (sbp2_send_command(scsi_id, SCpnt, done)) {
2362 SBP2_ERR("Error sending SCSI command");
2363 sbp2scsi_complete_command(scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
2364 SCpnt, done);
2365 }
Jody McIntyreabd559b2005-09-30 11:59:06 -07002366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Jody McIntyreabd559b2005-09-30 11:59:06 -07002368done:
2369 SCpnt->result = result;
2370 done(SCpnt);
2371 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372}
2373
2374/*
2375 * This function is called in order to complete all outstanding SBP-2
2376 * commands (in case of resets, etc.).
2377 */
2378static void sbp2scsi_complete_all_commands(struct scsi_id_instance_data *scsi_id,
2379 u32 status)
2380{
2381 struct sbp2scsi_host_info *hi = scsi_id->hi;
2382 struct list_head *lh;
2383 struct sbp2_command_info *command;
Jody McIntyre79456192005-11-07 06:29:39 -05002384 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Stefan Richterd024ebc2006-03-28 20:03:55 -05002386 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Jody McIntyre79456192005-11-07 06:29:39 -05002388 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 while (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
2390 SBP2_DEBUG("Found pending command to complete");
2391 lh = scsi_id->sbp2_command_orb_inuse.next;
2392 command = list_entry(lh, struct sbp2_command_info, list);
2393 pci_dma_sync_single_for_cpu(hi->host->pdev, command->command_orb_dma,
2394 sizeof(struct sbp2_command_orb),
2395 PCI_DMA_BIDIRECTIONAL);
2396 pci_dma_sync_single_for_cpu(hi->host->pdev, command->sge_dma,
2397 sizeof(command->scatter_gather_element),
2398 PCI_DMA_BIDIRECTIONAL);
2399 sbp2util_mark_command_completed(scsi_id, command);
2400 if (command->Current_SCpnt) {
2401 command->Current_SCpnt->result = status << 16;
2402 command->Current_done(command->Current_SCpnt);
2403 }
2404 }
Jody McIntyre79456192005-11-07 06:29:39 -05002405 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406
2407 return;
2408}
2409
2410/*
2411 * This function is called in order to complete a regular SBP-2 command.
2412 *
2413 * This can be called in interrupt context.
2414 */
2415static void sbp2scsi_complete_command(struct scsi_id_instance_data *scsi_id,
2416 u32 scsi_status, struct scsi_cmnd *SCpnt,
2417 void (*done)(struct scsi_cmnd *))
2418{
Stefan Richterd024ebc2006-03-28 20:03:55 -05002419 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
2421 /*
2422 * Sanity
2423 */
2424 if (!SCpnt) {
2425 SBP2_ERR("SCpnt is NULL");
2426 return;
2427 }
2428
2429 /*
2430 * If a bus reset is in progress and there was an error, don't
2431 * complete the command, just let it get retried at the end of the
2432 * bus reset.
2433 */
Stefan Richtera237f352005-11-07 06:31:39 -05002434 if (!hpsb_node_entry_valid(scsi_id->ne)
2435 && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 SBP2_ERR("Bus reset in progress - retry command later");
2437 return;
2438 }
Stefan Richtera237f352005-11-07 06:31:39 -05002439
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 /*
2441 * Switch on scsi status
2442 */
2443 switch (scsi_status) {
Stefan Richtera237f352005-11-07 06:31:39 -05002444 case SBP2_SCSI_STATUS_GOOD:
Stefan Richter8f0525f2006-03-28 20:03:45 -05002445 SCpnt->result = DID_OK << 16;
Stefan Richtera237f352005-11-07 06:31:39 -05002446 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447
Stefan Richtera237f352005-11-07 06:31:39 -05002448 case SBP2_SCSI_STATUS_BUSY:
2449 SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
2450 SCpnt->result = DID_BUS_BUSY << 16;
2451 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452
Stefan Richtera237f352005-11-07 06:31:39 -05002453 case SBP2_SCSI_STATUS_CHECK_CONDITION:
2454 SBP2_DEBUG("SBP2_SCSI_STATUS_CHECK_CONDITION");
Stefan Richter8f0525f2006-03-28 20:03:45 -05002455 SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456#if CONFIG_IEEE1394_SBP2_DEBUG >= 1
Stefan Richtera237f352005-11-07 06:31:39 -05002457 scsi_print_command(SCpnt);
Stefan Richterd024ebc2006-03-28 20:03:55 -05002458 scsi_print_sense(SBP2_DEVICE_NAME, SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459#endif
Stefan Richtera237f352005-11-07 06:31:39 -05002460 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
Stefan Richtera237f352005-11-07 06:31:39 -05002462 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
2463 SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
2464 SCpnt->result = DID_NO_CONNECT << 16;
2465 scsi_print_command(SCpnt);
2466 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Stefan Richtera237f352005-11-07 06:31:39 -05002468 case SBP2_SCSI_STATUS_CONDITION_MET:
2469 case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
2470 case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
2471 SBP2_ERR("Bad SCSI status = %x", scsi_status);
2472 SCpnt->result = DID_ERROR << 16;
2473 scsi_print_command(SCpnt);
2474 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Stefan Richtera237f352005-11-07 06:31:39 -05002476 default:
2477 SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
2478 SCpnt->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 }
2480
2481 /*
2482 * Take care of any sbp2 response data mucking here (RBC stuff, etc.)
2483 */
Stefan Richter8f0525f2006-03-28 20:03:45 -05002484 if (SCpnt->result == DID_OK << 16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 sbp2_check_sbp2_response(scsi_id, SCpnt);
2486 }
2487
2488 /*
2489 * If a bus reset is in progress and there was an error, complete
2490 * the command as busy so that it will get retried.
2491 */
Stefan Richtera237f352005-11-07 06:31:39 -05002492 if (!hpsb_node_entry_valid(scsi_id->ne)
2493 && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 SBP2_ERR("Completing command with busy (bus reset)");
2495 SCpnt->result = DID_BUS_BUSY << 16;
2496 }
2497
2498 /*
2499 * If a unit attention occurs, return busy status so it gets
2500 * retried... it could have happened because of a 1394 bus reset
2501 * or hot-plug...
Stefan Richter8f0525f2006-03-28 20:03:45 -05002502 * XXX DID_BUS_BUSY is actually a bad idea because it will defy
2503 * the scsi layer's retry logic.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 */
2505#if 0
2506 if ((scsi_status == SBP2_SCSI_STATUS_CHECK_CONDITION) &&
2507 (SCpnt->sense_buffer[2] == UNIT_ATTENTION)) {
2508 SBP2_DEBUG("UNIT ATTENTION - return busy");
2509 SCpnt->result = DID_BUS_BUSY << 16;
2510 }
2511#endif
2512
2513 /*
2514 * Tell scsi stack that we're done with this command
2515 */
Stefan Richtera237f352005-11-07 06:31:39 -05002516 done(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
Jody McIntyreabd559b2005-09-30 11:59:06 -07002519static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
2520{
Stefan Richtera80614d2006-02-14 22:04:19 -05002521 struct scsi_id_instance_data *scsi_id =
2522 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2523
2524 scsi_id->sdev = sdev;
2525
Stefan Richter24d3bf82006-05-15 22:04:59 +02002526 if (scsi_id->workarounds & SBP2_WORKAROUND_INQUIRY_36)
Stefan Richtera80614d2006-02-14 22:04:19 -05002527 sdev->inquiry_len = 36;
Jody McIntyreabd559b2005-09-30 11:59:06 -07002528 return 0;
2529}
2530
Jody McIntyreabd559b2005-09-30 11:59:06 -07002531static int sbp2scsi_slave_configure(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532{
Stefan Richter24d3bf82006-05-15 22:04:59 +02002533 struct scsi_id_instance_data *scsi_id =
2534 (struct scsi_id_instance_data *)sdev->host->hostdata[0];
2535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 blk_queue_dma_alignment(sdev->request_queue, (512 - 1));
Ben Collins365c7862005-11-07 06:31:24 -05002537 sdev->use_10_for_rw = 1;
2538 sdev->use_10_for_ms = 1;
Stefan Richter24d3bf82006-05-15 22:04:59 +02002539
2540 if (sdev->type == TYPE_DISK &&
2541 scsi_id->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
2542 sdev->skip_ms_page_8 = 1;
Stefan Richtere9a1c522006-05-15 22:06:37 +02002543 if (scsi_id->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
2544 sdev->fix_capacity = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 return 0;
2546}
2547
Jody McIntyreabd559b2005-09-30 11:59:06 -07002548static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
2549{
2550 ((struct scsi_id_instance_data *)sdev->host->hostdata[0])->sdev = NULL;
2551 return;
2552}
2553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554/*
2555 * Called by scsi stack when something has really gone wrong. Usually
2556 * called when a command has timed-out for some reason.
2557 */
2558static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
2559{
2560 struct scsi_id_instance_data *scsi_id =
2561 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2562 struct sbp2scsi_host_info *hi = scsi_id->hi;
2563 struct sbp2_command_info *command;
Stefan Richter24c7cd02006-04-01 21:11:41 +02002564 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
2566 SBP2_ERR("aborting sbp2 command");
2567 scsi_print_command(SCpnt);
2568
Jody McIntyreabd559b2005-09-30 11:59:06 -07002569 if (sbp2util_node_is_available(scsi_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
2571 /*
2572 * Right now, just return any matching command structures
2573 * to the free pool.
2574 */
Stefan Richter24c7cd02006-04-01 21:11:41 +02002575 spin_lock_irqsave(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
2577 if (command) {
2578 SBP2_DEBUG("Found command to abort");
2579 pci_dma_sync_single_for_cpu(hi->host->pdev,
2580 command->command_orb_dma,
2581 sizeof(struct sbp2_command_orb),
2582 PCI_DMA_BIDIRECTIONAL);
2583 pci_dma_sync_single_for_cpu(hi->host->pdev,
2584 command->sge_dma,
2585 sizeof(command->scatter_gather_element),
2586 PCI_DMA_BIDIRECTIONAL);
2587 sbp2util_mark_command_completed(scsi_id, command);
2588 if (command->Current_SCpnt) {
2589 command->Current_SCpnt->result = DID_ABORT << 16;
2590 command->Current_done(command->Current_SCpnt);
2591 }
2592 }
Stefan Richter24c7cd02006-04-01 21:11:41 +02002593 spin_unlock_irqrestore(&scsi_id->sbp2_command_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 /*
2596 * Initiate a fetch agent reset.
2597 */
2598 sbp2_agent_reset(scsi_id, 0);
2599 sbp2scsi_complete_all_commands(scsi_id, DID_BUS_BUSY);
2600 }
2601
Stefan Richtera237f352005-11-07 06:31:39 -05002602 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603}
2604
2605/*
2606 * Called by scsi stack when something has really gone wrong.
2607 */
Jody McIntyreabd559b2005-09-30 11:59:06 -07002608static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609{
2610 struct scsi_id_instance_data *scsi_id =
2611 (struct scsi_id_instance_data *)SCpnt->device->host->hostdata[0];
2612
2613 SBP2_ERR("reset requested");
2614
Jody McIntyreabd559b2005-09-30 11:59:06 -07002615 if (sbp2util_node_is_available(scsi_id)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 SBP2_ERR("Generating sbp2 fetch agent reset");
2617 sbp2_agent_reset(scsi_id, 0);
2618 }
2619
Jody McIntyreabd559b2005-09-30 11:59:06 -07002620 return SUCCESS;
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04002621}
2622
Stefan Richtera237f352005-11-07 06:31:39 -05002623static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
2624 struct device_attribute *attr,
2625 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
2627 struct scsi_device *sdev;
2628 struct scsi_id_instance_data *scsi_id;
2629 int lun;
2630
2631 if (!(sdev = to_scsi_device(dev)))
2632 return 0;
2633
2634 if (!(scsi_id = (struct scsi_id_instance_data *)sdev->host->hostdata[0]))
2635 return 0;
2636
Ben Collinse309fc62005-11-07 06:31:34 -05002637 lun = ORB_SET_LUN(scsi_id->sbp2_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
2639 return sprintf(buf, "%016Lx:%d:%d\n", (unsigned long long)scsi_id->ne->guid,
2640 scsi_id->ud->id, lun);
2641}
2642static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
2643
2644static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
2645 &dev_attr_ieee1394_id,
2646 NULL
2647};
2648
2649MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2650MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2651MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2652MODULE_LICENSE("GPL");
2653
2654/* SCSI host template */
2655static struct scsi_host_template scsi_driver_template = {
2656 .module = THIS_MODULE,
2657 .name = "SBP-2 IEEE-1394",
2658 .proc_name = SBP2_DEVICE_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 .queuecommand = sbp2scsi_queuecommand,
2660 .eh_abort_handler = sbp2scsi_abort,
2661 .eh_device_reset_handler = sbp2scsi_reset,
Jody McIntyreabd559b2005-09-30 11:59:06 -07002662 .slave_alloc = sbp2scsi_slave_alloc,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 .slave_configure = sbp2scsi_slave_configure,
Jody McIntyreabd559b2005-09-30 11:59:06 -07002664 .slave_destroy = sbp2scsi_slave_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 .this_id = -1,
2666 .sg_tablesize = SG_ALL,
2667 .use_clustering = ENABLE_CLUSTERING,
2668 .cmd_per_lun = SBP2_MAX_CMDS,
2669 .can_queue = SBP2_MAX_CMDS,
2670 .emulated = 1,
2671 .sdev_attrs = sbp2_sysfs_sdev_attrs,
2672};
2673
2674static int sbp2_module_init(void)
2675{
2676 int ret;
2677
Stefan Richterd024ebc2006-03-28 20:03:55 -05002678 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 /* Module load debug option to force one command at a time (serializing I/O) */
2681 if (serialize_io) {
Jody McIntyre2bab3592005-09-30 11:59:07 -07002682 SBP2_INFO("Driver forced to serialize I/O (serialize_io=1)");
2683 SBP2_INFO("Try serialize_io=0 for better performance");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 scsi_driver_template.can_queue = 1;
2685 scsi_driver_template.cmd_per_lun = 1;
2686 }
2687
Stefan Richter24d3bf82006-05-15 22:04:59 +02002688 if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
2689 (max_sectors * 512) > (128 * 1024))
2690 max_sectors = 128 * 1024 / 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 scsi_driver_template.max_sectors = max_sectors;
2692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 /* Register our high level driver with 1394 stack */
2694 hpsb_register_highlevel(&sbp2_highlevel);
2695
2696 ret = hpsb_register_protocol(&sbp2_driver);
2697 if (ret) {
2698 SBP2_ERR("Failed to register protocol");
2699 hpsb_unregister_highlevel(&sbp2_highlevel);
2700 return ret;
2701 }
2702
2703 return 0;
2704}
2705
2706static void __exit sbp2_module_exit(void)
2707{
Stefan Richterd024ebc2006-03-28 20:03:55 -05002708 SBP2_DEBUG_ENTER();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709
2710 hpsb_unregister_protocol(&sbp2_driver);
2711
2712 hpsb_unregister_highlevel(&sbp2_highlevel);
2713}
2714
2715module_init(sbp2_module_init);
2716module_exit(sbp2_module_exit);