blob: 3f873cc7e247a7944d4ac27359600daf378fb1f7 [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 *
Stefan Richter2a533b12006-11-02 21:16:08 +010032 * You may access any attached SBP-2 (usually storage devices) as regular
33 * SCSI devices. E.g. mount /dev/sda1, fdisk, mkfs, etc..
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 *
Stefan Richter2a533b12006-11-02 21:16:08 +010035 * See http://www.t10.org/drafts.htm#sbp2 for the final draft of the SBP-2
36 * specification and for where to purchase the official standard.
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 *
Stefan Richter2a533b12006-11-02 21:16:08 +010038 * TODO:
39 * - look into possible improvements of the SCSI error handlers
40 * - handle Unit_Characteristics.mgt_ORB_timeout and .ORB_size
41 * - handle Logical_Unit_Number.ordered
42 * - handle src == 1 in status blocks
43 * - reimplement the DMA mapping in absence of physical DMA so that
44 * bus_to_virt is no longer required
45 * - debug the handling of absent physical DMA
46 * - replace CONFIG_IEEE1394_SBP2_PHYS_DMA by automatic detection
47 * (this is easy but depends on the previous two TODO items)
48 * - make the parameter serialize_io configurable per device
49 * - move all requests to fetch agent registers into non-atomic context,
50 * replace all usages of sbp2util_node_write_no_wait by true transactions
Stefan Richter2a533b12006-11-02 21:16:08 +010051 * Grep for inline FIXME comments below.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
53
Stefan Richter902abed2006-08-14 18:56:00 +020054#include <linux/compiler.h>
55#include <linux/delay.h>
56#include <linux/device.h>
57#include <linux/dma-mapping.h>
58#include <linux/gfp.h>
59#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <linux/kernel.h>
61#include <linux/list.h>
Andrew Mortonf84c9222007-04-23 11:50:56 -070062#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <linux/module.h>
64#include <linux/moduleparam.h>
Andrew Mortonf84c9222007-04-23 11:50:56 -070065#include <linux/sched.h>
Stefan Richter902abed2006-08-14 18:56:00 +020066#include <linux/slab.h>
67#include <linux/spinlock.h>
68#include <linux/stat.h>
69#include <linux/string.h>
70#include <linux/stringify.h>
71#include <linux/types.h>
Stefan Richtere8398bb2006-07-23 22:19:00 +020072#include <linux/wait.h>
Stefan Richter20e20082007-05-05 17:18:12 +020073#include <linux/workqueue.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#include <asm/byteorder.h>
Stefan Richter902abed2006-08-14 18:56:00 +020076#include <asm/errno.h>
77#include <asm/param.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <asm/scatterlist.h>
Stefan Richter902abed2006-08-14 18:56:00 +020079#include <asm/system.h>
80#include <asm/types.h>
81
82#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
83#include <asm/io.h> /* for bus_to_virt */
84#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#include <scsi/scsi.h>
87#include <scsi/scsi_cmnd.h>
88#include <scsi/scsi_dbg.h>
89#include <scsi/scsi_device.h>
90#include <scsi/scsi_host.h>
91
92#include "csr1212.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include "highlevel.h"
Stefan Richter902abed2006-08-14 18:56:00 +020094#include "hosts.h"
95#include "ieee1394.h"
96#include "ieee1394_core.h"
97#include "ieee1394_hotplug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include "ieee1394_transactions.h"
Stefan Richter902abed2006-08-14 18:56:00 +020099#include "ieee1394_types.h"
100#include "nodemgr.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101#include "sbp2.h"
102
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103/*
104 * Module load parameter definitions
105 */
106
107/*
108 * Change max_speed on module load if you have a bad IEEE-1394
109 * controller that has trouble running 2KB packets at 400mb.
110 *
111 * NOTE: On certain OHCI parts I have seen short packets on async transmit
112 * (probably due to PCI latency/throughput issues with the part). You can
113 * bump down the speed if you are running into problems.
114 */
Stefan Richterca0c7452006-11-02 21:16:08 +0100115static int sbp2_max_speed = IEEE1394_SPEED_MAX;
116module_param_named(max_speed, sbp2_max_speed, int, 0644);
Stefan Richter28b06672006-11-02 21:16:08 +0100117MODULE_PARM_DESC(max_speed, "Force max speed "
118 "(3 = 800Mb/s, 2 = 400Mb/s, 1 = 200Mb/s, 0 = 100Mb/s)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120/*
121 * Set serialize_io to 1 if you'd like only one scsi command sent
122 * down to us at a time (debugging). This might be necessary for very
123 * badly behaved sbp2 devices.
124 */
Stefan Richterca0c7452006-11-02 21:16:08 +0100125static int sbp2_serialize_io = 1;
126module_param_named(serialize_io, sbp2_serialize_io, int, 0444);
127MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers "
128 "(default = 1, faster = 0)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
130/*
131 * Bump up max_sectors if you'd like to support very large sized
132 * transfers. Please note that some older sbp2 bridge chips are broken for
133 * transfers greater or equal to 128KB. Default is a value of 255
134 * sectors, or just under 128KB (at 512 byte sector size). I can note that
135 * the Oxsemi sbp2 chipsets have no problems supporting very large
136 * transfer sizes.
137 */
Stefan Richterca0c7452006-11-02 21:16:08 +0100138static int sbp2_max_sectors = SBP2_MAX_SECTORS;
139module_param_named(max_sectors, sbp2_max_sectors, int, 0444);
140MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported "
141 "(default = " __stringify(SBP2_MAX_SECTORS) ")");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
143/*
144 * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
145 * do an exclusive login, as it's generally unsafe to have two hosts
146 * talking to a single sbp2 device at the same time (filesystem coherency,
147 * etc.). If you're running an sbp2 device that supports multiple logins,
148 * and you're either running read-only filesystems or some sort of special
Ben Collins20f45782006-06-12 18:13:11 -0400149 * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster
150 * File System, or Lustre, then set exclusive_login to zero.
151 *
152 * So far only bridges from Oxford Semiconductor are known to support
153 * concurrent logins. Depending on firmware, four or two concurrent logins
154 * are possible on OXFW911 and newer Oxsemi bridges.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 */
Stefan Richterca0c7452006-11-02 21:16:08 +0100156static int sbp2_exclusive_login = 1;
157module_param_named(exclusive_login, sbp2_exclusive_login, int, 0644);
158MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
159 "(default = 1)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161/*
Stefan Richter24d3bf82006-05-15 22:04:59 +0200162 * If any of the following workarounds is required for your device to work,
163 * please submit the kernel messages logged by sbp2 to the linux1394-devel
164 * mailing list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 *
Stefan Richter24d3bf82006-05-15 22:04:59 +0200166 * - 128kB max transfer
167 * Limit transfer size. Necessary for some old bridges.
168 *
169 * - 36 byte inquiry
170 * When scsi_mod probes the device, let the inquiry command look like that
171 * from MS Windows.
172 *
173 * - skip mode page 8
174 * Suppress sending of mode_sense for mode page 8 if the device pretends to
175 * support the SCSI Primary Block commands instead of Reduced Block Commands.
Stefan Richtere9a1c522006-05-15 22:06:37 +0200176 *
177 * - fix capacity
178 * Tell sd_mod to correct the last sector number reported by read_capacity.
179 * Avoids access beyond actual disk limits on devices with an off-by-one bug.
180 * Don't use this with devices which don't have this bug.
Stefan Richter679c0cd2006-05-15 22:08:09 +0200181 *
182 * - override internal blacklist
183 * Instead of adding to the built-in blacklist, use only the workarounds
184 * specified in the module load parameter.
185 * Useful if a blacklist entry interfered with a non-broken device.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 */
Stefan Richter24d3bf82006-05-15 22:04:59 +0200187static int sbp2_default_workarounds;
188module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
189MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
190 ", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
191 ", 36 byte inquiry = " __stringify(SBP2_WORKAROUND_INQUIRY_36)
192 ", skip mode page 8 = " __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
Stefan Richtere9a1c522006-05-15 22:06:37 +0200193 ", fix capacity = " __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
Stefan Richter679c0cd2006-05-15 22:08:09 +0200194 ", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
Stefan Richter24d3bf82006-05-15 22:04:59 +0200195 ", or a combination)");
196
Stefan Richterd7794c82007-05-27 13:17:15 +0200197/*
198 * This influences the format of the sysfs attribute
199 * /sys/bus/scsi/devices/.../ieee1394_id.
200 *
201 * The default format is like in older kernels: %016Lx:%d:%d
202 * It contains the target's EUI-64, a number given to the logical unit by
203 * the ieee1394 driver's nodemgr (starting at 0), and the LUN.
204 *
205 * The long format is: %016Lx:%06x:%04x
206 * It contains the target's EUI-64, the unit directory's directory_ID as per
207 * IEEE 1212 clause 7.7.19, and the LUN. This format comes closest to the
208 * format of SBP(-3) target port and logical unit identifier as per SAM (SCSI
209 * Architecture Model) rev.2 to 4 annex A. Therefore and because it is
210 * independent of the implementation of the ieee1394 nodemgr, the longer format
211 * is recommended for future use.
212 */
213static int sbp2_long_sysfs_ieee1394_id;
214module_param_named(long_ieee1394_id, sbp2_long_sysfs_ieee1394_id, bool, 0644);
215MODULE_PARM_DESC(long_ieee1394_id, "8+3+2 bytes format of ieee1394_id in sysfs "
216 "(default = backwards-compatible = N, SAM-conforming = Y)");
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Stefan Richteredf1fb22006-11-02 21:16:08 +0100219#define SBP2_INFO(fmt, args...) HPSB_INFO("sbp2: "fmt, ## args)
220#define SBP2_ERR(fmt, args...) HPSB_ERR("sbp2: "fmt, ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/*
223 * Globals
224 */
Stefan Richter138c8af2006-11-02 21:16:08 +0100225static void sbp2scsi_complete_all_commands(struct sbp2_lu *, u32);
226static void sbp2scsi_complete_command(struct sbp2_lu *, u32, struct scsi_cmnd *,
Stefan Richterea42ea02006-11-02 21:16:08 +0100227 void (*)(struct scsi_cmnd *));
Stefan Richter138c8af2006-11-02 21:16:08 +0100228static struct sbp2_lu *sbp2_alloc_device(struct unit_directory *);
229static int sbp2_start_device(struct sbp2_lu *);
230static void sbp2_remove_device(struct sbp2_lu *);
231static int sbp2_login_device(struct sbp2_lu *);
232static int sbp2_reconnect_device(struct sbp2_lu *);
233static int sbp2_logout_device(struct sbp2_lu *);
Stefan Richterea42ea02006-11-02 21:16:08 +0100234static void sbp2_host_reset(struct hpsb_host *);
235static int sbp2_handle_status_write(struct hpsb_host *, int, int, quadlet_t *,
236 u64, size_t, u16);
Stefan Richter138c8af2006-11-02 21:16:08 +0100237static int sbp2_agent_reset(struct sbp2_lu *, int);
238static void sbp2_parse_unit_directory(struct sbp2_lu *,
Stefan Richterea42ea02006-11-02 21:16:08 +0100239 struct unit_directory *);
Stefan Richter138c8af2006-11-02 21:16:08 +0100240static int sbp2_set_busy_timeout(struct sbp2_lu *);
241static int sbp2_max_speed_and_size(struct sbp2_lu *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
245
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246static struct hpsb_highlevel sbp2_highlevel = {
Stefan Richterea42ea02006-11-02 21:16:08 +0100247 .name = SBP2_DEVICE_NAME,
248 .host_reset = sbp2_host_reset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249};
250
251static struct hpsb_address_ops sbp2_ops = {
Stefan Richterea42ea02006-11-02 21:16:08 +0100252 .write = sbp2_handle_status_write
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253};
254
255#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
Stefan Richterea42ea02006-11-02 21:16:08 +0100256static int sbp2_handle_physdma_write(struct hpsb_host *, int, int, quadlet_t *,
257 u64, size_t, u16);
258static int sbp2_handle_physdma_read(struct hpsb_host *, int, quadlet_t *, u64,
259 size_t, u16);
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261static struct hpsb_address_ops sbp2_physdma_ops = {
Stefan Richterea42ea02006-11-02 21:16:08 +0100262 .read = sbp2_handle_physdma_read,
263 .write = sbp2_handle_physdma_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264};
265#endif
266
Stefan Richterea42ea02006-11-02 21:16:08 +0100267
268/*
269 * Interface to driver core and IEEE 1394 core
270 */
271static struct ieee1394_device_id sbp2_id_table[] = {
272 {
273 .match_flags = IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
274 .specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
275 .version = SBP2_SW_VERSION_ENTRY & 0xffffff},
276 {}
277};
278MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
279
280static int sbp2_probe(struct device *);
281static int sbp2_remove(struct device *);
282static int sbp2_update(struct unit_directory *);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284static struct hpsb_protocol_driver sbp2_driver = {
Ben Collinsed30c262006-11-23 13:59:48 -0500285 .name = SBP2_DEVICE_NAME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 .id_table = sbp2_id_table,
287 .update = sbp2_update,
288 .driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 .probe = sbp2_probe,
290 .remove = sbp2_remove,
291 },
292};
293
Stefan Richterea42ea02006-11-02 21:16:08 +0100294
295/*
296 * Interface to SCSI core
297 */
298static int sbp2scsi_queuecommand(struct scsi_cmnd *,
299 void (*)(struct scsi_cmnd *));
300static int sbp2scsi_abort(struct scsi_cmnd *);
301static int sbp2scsi_reset(struct scsi_cmnd *);
302static int sbp2scsi_slave_alloc(struct scsi_device *);
303static int sbp2scsi_slave_configure(struct scsi_device *);
304static void sbp2scsi_slave_destroy(struct scsi_device *);
305static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *,
306 struct device_attribute *, char *);
307
308static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
309
310static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
311 &dev_attr_ieee1394_id,
312 NULL
313};
314
Stefan Richterca0c7452006-11-02 21:16:08 +0100315static struct scsi_host_template sbp2_shost_template = {
Stefan Richterea42ea02006-11-02 21:16:08 +0100316 .module = THIS_MODULE,
317 .name = "SBP-2 IEEE-1394",
318 .proc_name = SBP2_DEVICE_NAME,
319 .queuecommand = sbp2scsi_queuecommand,
320 .eh_abort_handler = sbp2scsi_abort,
321 .eh_device_reset_handler = sbp2scsi_reset,
322 .slave_alloc = sbp2scsi_slave_alloc,
323 .slave_configure = sbp2scsi_slave_configure,
324 .slave_destroy = sbp2scsi_slave_destroy,
325 .this_id = -1,
326 .sg_tablesize = SG_ALL,
327 .use_clustering = ENABLE_CLUSTERING,
328 .cmd_per_lun = SBP2_MAX_CMDS,
329 .can_queue = SBP2_MAX_CMDS,
Stefan Richterea42ea02006-11-02 21:16:08 +0100330 .sdev_attrs = sbp2_sysfs_sdev_attrs,
331};
332
Stefan Richter4618fd32006-12-30 15:37:09 +0100333/* for match-all entries in sbp2_workarounds_table */
334#define SBP2_ROM_VALUE_WILDCARD 0x1000000
Stefan Richterea42ea02006-11-02 21:16:08 +0100335
Stefan Richtera80614d2006-02-14 22:04:19 -0500336/*
Stefan Richter24d3bf82006-05-15 22:04:59 +0200337 * List of devices with known bugs.
338 *
339 * The firmware_revision field, masked with 0xffff00, is the best indicator
340 * for the type of bridge chip of a device. It yields a few false positives
341 * but this did not break correctly behaving devices so far.
Stefan Richtera80614d2006-02-14 22:04:19 -0500342 */
Stefan Richter24d3bf82006-05-15 22:04:59 +0200343static const struct {
344 u32 firmware_revision;
Stefan Richtere9a1c522006-05-15 22:06:37 +0200345 u32 model_id;
Stefan Richter24d3bf82006-05-15 22:04:59 +0200346 unsigned workarounds;
347} sbp2_workarounds_table[] = {
Ben Collins4b9a3342006-06-12 18:10:18 -0400348 /* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
Stefan Richter24d3bf82006-05-15 22:04:59 +0200349 .firmware_revision = 0x002800,
Ben Collins4b9a3342006-06-12 18:10:18 -0400350 .model_id = 0x001010,
Stefan Richter24d3bf82006-05-15 22:04:59 +0200351 .workarounds = SBP2_WORKAROUND_INQUIRY_36 |
352 SBP2_WORKAROUND_MODE_SENSE_8,
353 },
354 /* Initio bridges, actually only needed for some older ones */ {
355 .firmware_revision = 0x000200,
Stefan Richter4618fd32006-12-30 15:37:09 +0100356 .model_id = SBP2_ROM_VALUE_WILDCARD,
Stefan Richter24d3bf82006-05-15 22:04:59 +0200357 .workarounds = SBP2_WORKAROUND_INQUIRY_36,
358 },
359 /* Symbios bridge */ {
360 .firmware_revision = 0xa0b800,
Stefan Richter4618fd32006-12-30 15:37:09 +0100361 .model_id = SBP2_ROM_VALUE_WILDCARD,
Stefan Richter24d3bf82006-05-15 22:04:59 +0200362 .workarounds = SBP2_WORKAROUND_128K_MAX_TRANS,
Stefan Richtere9a1c522006-05-15 22:06:37 +0200363 },
Stefan Richtere9a1c522006-05-15 22:06:37 +0200364 /* iPod 4th generation */ {
365 .firmware_revision = 0x0a2700,
366 .model_id = 0x000021,
367 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
368 },
369 /* iPod mini */ {
370 .firmware_revision = 0x0a2700,
371 .model_id = 0x000023,
372 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
373 },
374 /* iPod Photo */ {
375 .firmware_revision = 0x0a2700,
376 .model_id = 0x00007e,
377 .workarounds = SBP2_WORKAROUND_FIX_CAPACITY,
Stefan Richter24d3bf82006-05-15 22:04:59 +0200378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379};
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/**************************************
382 * General utility functions
383 **************************************/
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385#ifndef __BIG_ENDIAN
386/*
387 * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
388 */
Stefan Richter2b01b802006-07-03 12:02:28 -0400389static inline void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
391 u32 *temp = buffer;
392
393 for (length = (length >> 2); length--; )
394 temp[length] = be32_to_cpu(temp[length]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
397/*
398 * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
399 */
Stefan Richter2b01b802006-07-03 12:02:28 -0400400static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
402 u32 *temp = buffer;
403
404 for (length = (length >> 2); length--; )
405 temp[length] = cpu_to_be32(temp[length]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407#else /* BIG_ENDIAN */
408/* Why waste the cpu cycles? */
Stefan Richter611aa192006-08-02 18:44:00 +0200409#define sbp2util_be32_to_cpu_buffer(x,y) do {} while (0)
410#define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411#endif
412
Stefan Richterca0c7452006-11-02 21:16:08 +0100413static DECLARE_WAIT_QUEUE_HEAD(sbp2_access_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Stefan Richtere8398bb2006-07-23 22:19:00 +0200415/*
416 * Waits for completion of an SBP-2 access request.
417 * Returns nonzero if timed out or prematurely interrupted.
418 */
Stefan Richter138c8af2006-11-02 21:16:08 +0100419static int sbp2util_access_timeout(struct sbp2_lu *lu, int timeout)
Stefan Richtere8398bb2006-07-23 22:19:00 +0200420{
Stefan Richterca0c7452006-11-02 21:16:08 +0100421 long leftover;
Stefan Richtere8398bb2006-07-23 22:19:00 +0200422
Stefan Richterca0c7452006-11-02 21:16:08 +0100423 leftover = wait_event_interruptible_timeout(
Stefan Richter138c8af2006-11-02 21:16:08 +0100424 sbp2_access_wq, lu->access_complete, timeout);
425 lu->access_complete = 0;
Stefan Richtere8398bb2006-07-23 22:19:00 +0200426 return leftover <= 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427}
428
Stefan Richter138c8af2006-11-02 21:16:08 +0100429static void sbp2_free_packet(void *packet)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
431 hpsb_free_tlabel(packet);
432 hpsb_free_packet(packet);
433}
434
Stefan Richtere8ca5662006-11-02 21:16:08 +0100435/*
436 * This is much like hpsb_node_write(), except it ignores the response
437 * subaction and returns immediately. Can be used from atomic context.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 */
439static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
Stefan Richter138c8af2006-11-02 21:16:08 +0100440 quadlet_t *buf, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 struct hpsb_packet *packet;
443
Stefan Richter138c8af2006-11-02 21:16:08 +0100444 packet = hpsb_make_writepacket(ne->host, ne->nodeid, addr, buf, len);
Stefan Richtera237f352005-11-07 06:31:39 -0500445 if (!packet)
446 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Stefan Richter138c8af2006-11-02 21:16:08 +0100448 hpsb_set_packet_complete_task(packet, sbp2_free_packet, packet);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 hpsb_node_fill_packet(ne, packet);
Stefan Richtera237f352005-11-07 06:31:39 -0500450 if (hpsb_send_packet(packet) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 sbp2_free_packet(packet);
452 return -EIO;
453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return 0;
455}
456
Stefan Richter138c8af2006-11-02 21:16:08 +0100457static void sbp2util_notify_fetch_agent(struct sbp2_lu *lu, u64 offset,
458 quadlet_t *data, size_t len)
Stefan Richter09ee67a2006-08-14 18:43:00 +0200459{
Stefan Richter138c8af2006-11-02 21:16:08 +0100460 /* There is a small window after a bus reset within which the node
461 * entry's generation is current but the reconnect wasn't completed. */
462 if (unlikely(atomic_read(&lu->state) == SBP2LU_STATE_IN_RESET))
Stefan Richter09ee67a2006-08-14 18:43:00 +0200463 return;
464
Stefan Richter138c8af2006-11-02 21:16:08 +0100465 if (hpsb_node_write(lu->ne, lu->command_block_agent_addr + offset,
Stefan Richter09ee67a2006-08-14 18:43:00 +0200466 data, len))
467 SBP2_ERR("sbp2util_notify_fetch_agent failed.");
Stefan Richter138c8af2006-11-02 21:16:08 +0100468
469 /* Now accept new SCSI commands, unless a bus reset happended during
470 * hpsb_node_write. */
471 if (likely(atomic_read(&lu->state) != SBP2LU_STATE_IN_RESET))
472 scsi_unblock_requests(lu->shost);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200473}
474
David Howellsc4028952006-11-22 14:57:56 +0000475static void sbp2util_write_orb_pointer(struct work_struct *work)
Stefan Richter09ee67a2006-08-14 18:43:00 +0200476{
Stefan Richterec9b7e12006-12-07 23:23:25 +0100477 struct sbp2_lu *lu = container_of(work, struct sbp2_lu, protocol_work);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200478 quadlet_t data[2];
479
Stefan Richterec9b7e12006-12-07 23:23:25 +0100480 data[0] = ORB_SET_NODE_ID(lu->hi->host->node_id);
481 data[1] = lu->last_orb_dma;
Stefan Richter09ee67a2006-08-14 18:43:00 +0200482 sbp2util_cpu_to_be32_buffer(data, 8);
Stefan Richterec9b7e12006-12-07 23:23:25 +0100483 sbp2util_notify_fetch_agent(lu, SBP2_ORB_POINTER_OFFSET, data, 8);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200484}
485
David Howellsc4028952006-11-22 14:57:56 +0000486static void sbp2util_write_doorbell(struct work_struct *work)
Stefan Richter09ee67a2006-08-14 18:43:00 +0200487{
Stefan Richterec9b7e12006-12-07 23:23:25 +0100488 struct sbp2_lu *lu = container_of(work, struct sbp2_lu, protocol_work);
489
490 sbp2util_notify_fetch_agent(lu, SBP2_DOORBELL_OFFSET, NULL, 4);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200491}
492
Stefan Richter138c8af2006-11-02 21:16:08 +0100493static int sbp2util_create_command_orb_pool(struct sbp2_lu *lu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
Stefan Richter138c8af2006-11-02 21:16:08 +0100495 struct sbp2_fwhost_info *hi = lu->hi;
Stefan Richter138c8af2006-11-02 21:16:08 +0100496 struct sbp2_command_info *cmd;
Stefan Richter3d269cb2007-02-04 20:57:38 +0100497 int i, orbs = sbp2_serialize_io ? 2 : SBP2_MAX_CMDS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 for (i = 0; i < orbs; i++) {
Stefan Richter3d269cb2007-02-04 20:57:38 +0100500 cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
501 if (!cmd)
Stefan Richtera237f352005-11-07 06:31:39 -0500502 return -ENOMEM;
Stefan Richter97d552e2006-12-29 23:47:04 +0100503 cmd->command_orb_dma = dma_map_single(hi->host->device.parent,
Stefan Richter138c8af2006-11-02 21:16:08 +0100504 &cmd->command_orb,
505 sizeof(struct sbp2_command_orb),
Stefan Richter9b7d9c02006-11-22 21:44:34 +0100506 DMA_TO_DEVICE);
Stefan Richter97d552e2006-12-29 23:47:04 +0100507 cmd->sge_dma = dma_map_single(hi->host->device.parent,
Stefan Richter138c8af2006-11-02 21:16:08 +0100508 &cmd->scatter_gather_element,
509 sizeof(cmd->scatter_gather_element),
Stefan Richter2446a792007-02-04 20:54:57 +0100510 DMA_TO_DEVICE);
Stefan Richter138c8af2006-11-02 21:16:08 +0100511 INIT_LIST_HEAD(&cmd->list);
512 list_add_tail(&cmd->list, &lu->cmd_orb_completed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return 0;
515}
516
Stefan Richter138c8af2006-11-02 21:16:08 +0100517static void sbp2util_remove_command_orb_pool(struct sbp2_lu *lu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Stefan Richter138c8af2006-11-02 21:16:08 +0100519 struct hpsb_host *host = lu->hi->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 struct list_head *lh, *next;
Stefan Richter138c8af2006-11-02 21:16:08 +0100521 struct sbp2_command_info *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 unsigned long flags;
523
Stefan Richter138c8af2006-11-02 21:16:08 +0100524 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
525 if (!list_empty(&lu->cmd_orb_completed))
526 list_for_each_safe(lh, next, &lu->cmd_orb_completed) {
527 cmd = list_entry(lh, struct sbp2_command_info, list);
Stefan Richter97d552e2006-12-29 23:47:04 +0100528 dma_unmap_single(host->device.parent,
529 cmd->command_orb_dma,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 sizeof(struct sbp2_command_orb),
Stefan Richter9b7d9c02006-11-22 21:44:34 +0100531 DMA_TO_DEVICE);
Stefan Richter97d552e2006-12-29 23:47:04 +0100532 dma_unmap_single(host->device.parent, cmd->sge_dma,
Stefan Richter138c8af2006-11-02 21:16:08 +0100533 sizeof(cmd->scatter_gather_element),
Stefan Richter2446a792007-02-04 20:54:57 +0100534 DMA_TO_DEVICE);
Stefan Richter138c8af2006-11-02 21:16:08 +0100535 kfree(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
Stefan Richter138c8af2006-11-02 21:16:08 +0100537 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return;
539}
540
541/*
Stefan Richtere8ca5662006-11-02 21:16:08 +0100542 * Finds the sbp2_command for a given outstanding command ORB.
543 * Only looks at the in-use list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
545static struct sbp2_command_info *sbp2util_find_command_for_orb(
Stefan Richter138c8af2006-11-02 21:16:08 +0100546 struct sbp2_lu *lu, dma_addr_t orb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Stefan Richter138c8af2006-11-02 21:16:08 +0100548 struct sbp2_command_info *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 unsigned long flags;
550
Stefan Richter138c8af2006-11-02 21:16:08 +0100551 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
552 if (!list_empty(&lu->cmd_orb_inuse))
553 list_for_each_entry(cmd, &lu->cmd_orb_inuse, list)
554 if (cmd->command_orb_dma == orb) {
555 spin_unlock_irqrestore(
556 &lu->cmd_orb_lock, flags);
557 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 }
Stefan Richter138c8af2006-11-02 21:16:08 +0100559 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
Stefan Richtera237f352005-11-07 06:31:39 -0500560 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561}
562
563/*
Stefan Richtere8ca5662006-11-02 21:16:08 +0100564 * Finds the sbp2_command for a given outstanding SCpnt.
565 * Only looks at the in-use list.
Stefan Richter138c8af2006-11-02 21:16:08 +0100566 * Must be called with lu->cmd_orb_lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 */
Stefan Richter24c7cd02006-04-01 21:11:41 +0200568static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
Stefan Richter138c8af2006-11-02 21:16:08 +0100569 struct sbp2_lu *lu, void *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Stefan Richter138c8af2006-11-02 21:16:08 +0100571 struct sbp2_command_info *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Stefan Richter138c8af2006-11-02 21:16:08 +0100573 if (!list_empty(&lu->cmd_orb_inuse))
574 list_for_each_entry(cmd, &lu->cmd_orb_inuse, list)
575 if (cmd->Current_SCpnt == SCpnt)
576 return cmd;
Stefan Richtera237f352005-11-07 06:31:39 -0500577 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580static struct sbp2_command_info *sbp2util_allocate_command_orb(
Stefan Richter138c8af2006-11-02 21:16:08 +0100581 struct sbp2_lu *lu,
582 struct scsi_cmnd *Current_SCpnt,
583 void (*Current_done)(struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 struct list_head *lh;
Stefan Richter138c8af2006-11-02 21:16:08 +0100586 struct sbp2_command_info *cmd = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 unsigned long flags;
588
Stefan Richter138c8af2006-11-02 21:16:08 +0100589 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
590 if (!list_empty(&lu->cmd_orb_completed)) {
591 lh = lu->cmd_orb_completed.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 list_del(lh);
Stefan Richter138c8af2006-11-02 21:16:08 +0100593 cmd = list_entry(lh, struct sbp2_command_info, list);
594 cmd->Current_done = Current_done;
595 cmd->Current_SCpnt = Current_SCpnt;
596 list_add_tail(&cmd->list, &lu->cmd_orb_inuse);
597 } else
Stefan Richterd024ebc2006-03-28 20:03:55 -0500598 SBP2_ERR("%s: no orbs available", __FUNCTION__);
Stefan Richter138c8af2006-11-02 21:16:08 +0100599 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
600 return cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601}
602
Stefan Richter58272c12006-11-04 09:55:33 +0100603/*
604 * Unmaps the DMAs of a command and moves the command to the completed ORB list.
605 * Must be called with lu->cmd_orb_lock held.
606 */
607static void sbp2util_mark_command_completed(struct sbp2_lu *lu,
608 struct sbp2_command_info *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
Stefan Richter58272c12006-11-04 09:55:33 +0100610 struct hpsb_host *host = lu->ud->ne->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Stefan Richter138c8af2006-11-02 21:16:08 +0100612 if (cmd->cmd_dma) {
613 if (cmd->dma_type == CMD_DMA_SINGLE)
Stefan Richter97d552e2006-12-29 23:47:04 +0100614 dma_unmap_single(host->device.parent, cmd->cmd_dma,
Stefan Richter138c8af2006-11-02 21:16:08 +0100615 cmd->dma_size, cmd->dma_dir);
616 else if (cmd->dma_type == CMD_DMA_PAGE)
Stefan Richter97d552e2006-12-29 23:47:04 +0100617 dma_unmap_page(host->device.parent, cmd->cmd_dma,
Stefan Richter138c8af2006-11-02 21:16:08 +0100618 cmd->dma_size, cmd->dma_dir);
Stefan Richteredf1fb22006-11-02 21:16:08 +0100619 /* XXX: Check for CMD_DMA_NONE bug */
Stefan Richter138c8af2006-11-02 21:16:08 +0100620 cmd->dma_type = CMD_DMA_NONE;
621 cmd->cmd_dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
Stefan Richter138c8af2006-11-02 21:16:08 +0100623 if (cmd->sge_buffer) {
Stefan Richter97d552e2006-12-29 23:47:04 +0100624 dma_unmap_sg(host->device.parent, cmd->sge_buffer,
Stefan Richter138c8af2006-11-02 21:16:08 +0100625 cmd->dma_size, cmd->dma_dir);
626 cmd->sge_buffer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 }
Stefan Richtercd641f62006-11-02 21:16:08 +0100628 list_move_tail(&cmd->list, &lu->cmd_orb_completed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Jody McIntyreabd559b2005-09-30 11:59:06 -0700631/*
Stefan Richter138c8af2006-11-02 21:16:08 +0100632 * Is lu valid? Is the 1394 node still present?
Jody McIntyreabd559b2005-09-30 11:59:06 -0700633 */
Stefan Richter138c8af2006-11-02 21:16:08 +0100634static inline int sbp2util_node_is_available(struct sbp2_lu *lu)
Jody McIntyreabd559b2005-09-30 11:59:06 -0700635{
Stefan Richter138c8af2006-11-02 21:16:08 +0100636 return lu && lu->ne && !lu->ne->in_limbo;
Jody McIntyreabd559b2005-09-30 11:59:06 -0700637}
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639/*********************************************
640 * IEEE-1394 core driver stack related section
641 *********************************************/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643static int sbp2_probe(struct device *dev)
644{
645 struct unit_directory *ud;
Stefan Richter138c8af2006-11-02 21:16:08 +0100646 struct sbp2_lu *lu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 ud = container_of(dev, struct unit_directory, device);
649
650 /* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
651 * instead. */
652 if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
653 return -ENODEV;
654
Stefan Richter138c8af2006-11-02 21:16:08 +0100655 lu = sbp2_alloc_device(ud);
656 if (!lu)
Stefan Richtera237f352005-11-07 06:31:39 -0500657 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Stefan Richter138c8af2006-11-02 21:16:08 +0100659 sbp2_parse_unit_directory(lu, ud);
660 return sbp2_start_device(lu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663static int sbp2_remove(struct device *dev)
664{
665 struct unit_directory *ud;
Stefan Richter138c8af2006-11-02 21:16:08 +0100666 struct sbp2_lu *lu;
Jody McIntyreabd559b2005-09-30 11:59:06 -0700667 struct scsi_device *sdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 ud = container_of(dev, struct unit_directory, device);
Stefan Richter138c8af2006-11-02 21:16:08 +0100670 lu = ud->device.driver_data;
671 if (!lu)
Jody McIntyreabd559b2005-09-30 11:59:06 -0700672 return 0;
673
Stefan Richter138c8af2006-11-02 21:16:08 +0100674 if (lu->shost) {
Stefan Richterbf637ec2006-01-31 00:13:06 -0500675 /* Get rid of enqueued commands if there is no chance to
676 * send them. */
Stefan Richter138c8af2006-11-02 21:16:08 +0100677 if (!sbp2util_node_is_available(lu))
678 sbp2scsi_complete_all_commands(lu, DID_NO_CONNECT);
Stefan Richtere8ca5662006-11-02 21:16:08 +0100679 /* scsi_remove_device() may trigger shutdown functions of SCSI
Stefan Richterbf637ec2006-01-31 00:13:06 -0500680 * highlevel drivers which would deadlock if blocked. */
Stefan Richter138c8af2006-11-02 21:16:08 +0100681 atomic_set(&lu->state, SBP2LU_STATE_IN_SHUTDOWN);
682 scsi_unblock_requests(lu->shost);
Stefan Richterbf637ec2006-01-31 00:13:06 -0500683 }
Stefan Richter138c8af2006-11-02 21:16:08 +0100684 sdev = lu->sdev;
Jody McIntyreabd559b2005-09-30 11:59:06 -0700685 if (sdev) {
Stefan Richter138c8af2006-11-02 21:16:08 +0100686 lu->sdev = NULL;
Jody McIntyreabd559b2005-09-30 11:59:06 -0700687 scsi_remove_device(sdev);
688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Stefan Richter138c8af2006-11-02 21:16:08 +0100690 sbp2_logout_device(lu);
691 sbp2_remove_device(lu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 return 0;
694}
695
696static int sbp2_update(struct unit_directory *ud)
697{
Stefan Richter138c8af2006-11-02 21:16:08 +0100698 struct sbp2_lu *lu = ud->device.driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Stefan Richter138c8af2006-11-02 21:16:08 +0100700 if (sbp2_reconnect_device(lu)) {
Stefan Richtere8ca5662006-11-02 21:16:08 +0100701 /* Reconnect has failed. Perhaps we didn't reconnect fast
702 * enough. Try a regular login, but first log out just in
703 * case of any weirdness. */
Stefan Richter138c8af2006-11-02 21:16:08 +0100704 sbp2_logout_device(lu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Stefan Richter138c8af2006-11-02 21:16:08 +0100706 if (sbp2_login_device(lu)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 /* Login failed too, just fail, and the backend
708 * will call our sbp2_remove for us */
709 SBP2_ERR("Failed to reconnect to sbp2 device!");
710 return -EBUSY;
711 }
712 }
713
Stefan Richter138c8af2006-11-02 21:16:08 +0100714 sbp2_set_busy_timeout(lu);
715 sbp2_agent_reset(lu, 1);
716 sbp2_max_speed_and_size(lu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
Stefan Richtere8ca5662006-11-02 21:16:08 +0100718 /* Complete any pending commands with busy (so they get retried)
719 * and remove them from our queue. */
Stefan Richter138c8af2006-11-02 21:16:08 +0100720 sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Stefan Richter4fc383c2006-08-14 18:46:00 +0200722 /* Accept new commands unless there was another bus reset in the
723 * meantime. */
Stefan Richter138c8af2006-11-02 21:16:08 +0100724 if (hpsb_node_entry_valid(lu->ne)) {
725 atomic_set(&lu->state, SBP2LU_STATE_RUNNING);
726 scsi_unblock_requests(lu->shost);
Stefan Richter4fc383c2006-08-14 18:46:00 +0200727 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return 0;
729}
730
Stefan Richter138c8af2006-11-02 21:16:08 +0100731static struct sbp2_lu *sbp2_alloc_device(struct unit_directory *ud)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
Stefan Richterca0c7452006-11-02 21:16:08 +0100733 struct sbp2_fwhost_info *hi;
Stefan Richter138c8af2006-11-02 21:16:08 +0100734 struct Scsi_Host *shost = NULL;
735 struct sbp2_lu *lu = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Stefan Richter138c8af2006-11-02 21:16:08 +0100737 lu = kzalloc(sizeof(*lu), GFP_KERNEL);
738 if (!lu) {
739 SBP2_ERR("failed to create lu");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 goto failed_alloc;
741 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
Stefan Richter138c8af2006-11-02 21:16:08 +0100743 lu->ne = ud->ne;
744 lu->ud = ud;
745 lu->speed_code = IEEE1394_SPEED_100;
746 lu->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
747 lu->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
748 INIT_LIST_HEAD(&lu->cmd_orb_inuse);
749 INIT_LIST_HEAD(&lu->cmd_orb_completed);
750 INIT_LIST_HEAD(&lu->lu_list);
751 spin_lock_init(&lu->cmd_orb_lock);
752 atomic_set(&lu->state, SBP2LU_STATE_RUNNING);
753 INIT_WORK(&lu->protocol_work, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Stefan Richter138c8af2006-11-02 21:16:08 +0100755 ud->device.driver_data = lu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
758 if (!hi) {
Stefan Richter138c8af2006-11-02 21:16:08 +0100759 hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host,
760 sizeof(*hi));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 if (!hi) {
762 SBP2_ERR("failed to allocate hostinfo");
763 goto failed_alloc;
764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 hi->host = ud->ne->host;
Stefan Richter138c8af2006-11-02 21:16:08 +0100766 INIT_LIST_HEAD(&hi->logical_units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
769 /* Handle data movement if physical dma is not
Stefan Richter55664052006-03-28 19:59:42 -0500770 * enabled or not supported on host controller */
771 if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
772 &sbp2_physdma_ops,
773 0x0ULL, 0xfffffffcULL)) {
774 SBP2_ERR("failed to register lower 4GB address range");
775 goto failed_alloc;
776 }
Stefan Richter05556592007-02-04 20:25:43 +0100777#else
778 if (dma_set_mask(hi->host->device.parent, DMA_32BIT_MASK)) {
779 SBP2_ERR("failed to set 4GB DMA mask");
780 goto failed_alloc;
781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782#endif
783 }
784
Stefan Richter147830f2006-03-28 19:54:52 -0500785 /* Prevent unloading of the 1394 host */
786 if (!try_module_get(hi->host->driver->owner)) {
787 SBP2_ERR("failed to get a reference on 1394 host driver");
788 goto failed_alloc;
789 }
790
Stefan Richter138c8af2006-11-02 21:16:08 +0100791 lu->hi = hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Stefan Richter138c8af2006-11-02 21:16:08 +0100793 list_add_tail(&lu->lu_list, &hi->logical_units);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Stefan Richter35bdddb2006-01-31 00:13:33 -0500795 /* Register the status FIFO address range. We could use the same FIFO
796 * for targets at different nodes. However we need different FIFOs per
Stefan Richtera54c9d32006-05-15 22:09:46 +0200797 * target in order to support multi-unit devices.
798 * The FIFO is located out of the local host controller's physical range
799 * but, if possible, within the posted write area. Status writes will
800 * then be performed as unified transactions. This slightly reduces
801 * bandwidth usage, and some Prolific based devices seem to require it.
802 */
Stefan Richter138c8af2006-11-02 21:16:08 +0100803 lu->status_fifo_addr = hpsb_allocate_and_register_addrspace(
Stefan Richter35bdddb2006-01-31 00:13:33 -0500804 &sbp2_highlevel, ud->ne->host, &sbp2_ops,
805 sizeof(struct sbp2_status_block), sizeof(quadlet_t),
Ben Collins40ae6c52006-06-12 18:13:49 -0400806 ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
Stefan Richter138c8af2006-11-02 21:16:08 +0100807 if (lu->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
Stefan Richter35bdddb2006-01-31 00:13:33 -0500808 SBP2_ERR("failed to allocate status FIFO address range");
809 goto failed_alloc;
810 }
811
Stefan Richter138c8af2006-11-02 21:16:08 +0100812 shost = scsi_host_alloc(&sbp2_shost_template, sizeof(unsigned long));
813 if (!shost) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 SBP2_ERR("failed to register scsi host");
815 goto failed_alloc;
816 }
817
Stefan Richter138c8af2006-11-02 21:16:08 +0100818 shost->hostdata[0] = (unsigned long)lu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Stefan Richter138c8af2006-11-02 21:16:08 +0100820 if (!scsi_add_host(shost, &ud->device)) {
821 lu->shost = shost;
822 return lu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
825 SBP2_ERR("failed to add scsi host");
Stefan Richter138c8af2006-11-02 21:16:08 +0100826 scsi_host_put(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828failed_alloc:
Stefan Richter138c8af2006-11-02 21:16:08 +0100829 sbp2_remove_device(lu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return NULL;
831}
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833static void sbp2_host_reset(struct hpsb_host *host)
834{
Stefan Richterca0c7452006-11-02 21:16:08 +0100835 struct sbp2_fwhost_info *hi;
Stefan Richter138c8af2006-11-02 21:16:08 +0100836 struct sbp2_lu *lu;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
Stefan Richter2cccbb52006-08-14 18:59:00 +0200839 if (!hi)
840 return;
Stefan Richter138c8af2006-11-02 21:16:08 +0100841 list_for_each_entry(lu, &hi->logical_units, lu_list)
842 if (likely(atomic_read(&lu->state) !=
Stefan Richter2cccbb52006-08-14 18:59:00 +0200843 SBP2LU_STATE_IN_SHUTDOWN)) {
Stefan Richter138c8af2006-11-02 21:16:08 +0100844 atomic_set(&lu->state, SBP2LU_STATE_IN_RESET);
845 scsi_block_requests(lu->shost);
Stefan Richter09ee67a2006-08-14 18:43:00 +0200846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Stefan Richter138c8af2006-11-02 21:16:08 +0100849static int sbp2_start_device(struct sbp2_lu *lu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850{
Stefan Richter138c8af2006-11-02 21:16:08 +0100851 struct sbp2_fwhost_info *hi = lu->hi;
James Bottomley146f7262005-09-10 12:44:09 -0500852 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Stefan Richter97d552e2006-12-29 23:47:04 +0100854 lu->login_response = dma_alloc_coherent(hi->host->device.parent,
Stefan Richtera237f352005-11-07 06:31:39 -0500855 sizeof(struct sbp2_login_response),
Stefan Richter9b7d9c02006-11-22 21:44:34 +0100856 &lu->login_response_dma, GFP_KERNEL);
Stefan Richter138c8af2006-11-02 21:16:08 +0100857 if (!lu->login_response)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Stefan Richter97d552e2006-12-29 23:47:04 +0100860 lu->query_logins_orb = dma_alloc_coherent(hi->host->device.parent,
Stefan Richtera237f352005-11-07 06:31:39 -0500861 sizeof(struct sbp2_query_logins_orb),
Stefan Richter9b7d9c02006-11-22 21:44:34 +0100862 &lu->query_logins_orb_dma, GFP_KERNEL);
Stefan Richter138c8af2006-11-02 21:16:08 +0100863 if (!lu->query_logins_orb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
Stefan Richter97d552e2006-12-29 23:47:04 +0100866 lu->query_logins_response = dma_alloc_coherent(hi->host->device.parent,
Stefan Richtera237f352005-11-07 06:31:39 -0500867 sizeof(struct sbp2_query_logins_response),
Stefan Richter9b7d9c02006-11-22 21:44:34 +0100868 &lu->query_logins_response_dma, GFP_KERNEL);
Stefan Richter138c8af2006-11-02 21:16:08 +0100869 if (!lu->query_logins_response)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Stefan Richter97d552e2006-12-29 23:47:04 +0100872 lu->reconnect_orb = dma_alloc_coherent(hi->host->device.parent,
Stefan Richtera237f352005-11-07 06:31:39 -0500873 sizeof(struct sbp2_reconnect_orb),
Stefan Richter9b7d9c02006-11-22 21:44:34 +0100874 &lu->reconnect_orb_dma, GFP_KERNEL);
Stefan Richter138c8af2006-11-02 21:16:08 +0100875 if (!lu->reconnect_orb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Stefan Richter97d552e2006-12-29 23:47:04 +0100878 lu->logout_orb = dma_alloc_coherent(hi->host->device.parent,
Stefan Richtera237f352005-11-07 06:31:39 -0500879 sizeof(struct sbp2_logout_orb),
Stefan Richter9b7d9c02006-11-22 21:44:34 +0100880 &lu->logout_orb_dma, GFP_KERNEL);
Stefan Richter138c8af2006-11-02 21:16:08 +0100881 if (!lu->logout_orb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Stefan Richter97d552e2006-12-29 23:47:04 +0100884 lu->login_orb = dma_alloc_coherent(hi->host->device.parent,
Stefan Richtera237f352005-11-07 06:31:39 -0500885 sizeof(struct sbp2_login_orb),
Stefan Richter9b7d9c02006-11-22 21:44:34 +0100886 &lu->login_orb_dma, GFP_KERNEL);
Stefan Richter138c8af2006-11-02 21:16:08 +0100887 if (!lu->login_orb)
Stefan Richtereaceec72005-12-13 11:05:05 -0500888 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Stefan Richter3d269cb2007-02-04 20:57:38 +0100890 if (sbp2util_create_command_orb_pool(lu))
891 goto alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Stefan Richtere8ca5662006-11-02 21:16:08 +0100893 /* Wait a second before trying to log in. Previously logged in
894 * initiators need a chance to reconnect. */
Stefan Richter902abed2006-08-14 18:56:00 +0200895 if (msleep_interruptible(1000)) {
Stefan Richter138c8af2006-11-02 21:16:08 +0100896 sbp2_remove_device(lu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 return -EINTR;
898 }
Stefan Richtera237f352005-11-07 06:31:39 -0500899
Stefan Richter138c8af2006-11-02 21:16:08 +0100900 if (sbp2_login_device(lu)) {
901 sbp2_remove_device(lu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 return -EBUSY;
903 }
904
Stefan Richter138c8af2006-11-02 21:16:08 +0100905 sbp2_set_busy_timeout(lu);
906 sbp2_agent_reset(lu, 1);
907 sbp2_max_speed_and_size(lu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Stefan Richter138c8af2006-11-02 21:16:08 +0100909 error = scsi_add_device(lu->shost, 0, lu->ud->id, 0);
James Bottomley146f7262005-09-10 12:44:09 -0500910 if (error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 SBP2_ERR("scsi_add_device failed");
Stefan Richter138c8af2006-11-02 21:16:08 +0100912 sbp2_logout_device(lu);
913 sbp2_remove_device(lu);
James Bottomley146f7262005-09-10 12:44:09 -0500914 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916
917 return 0;
Stefan Richtereaceec72005-12-13 11:05:05 -0500918
919alloc_fail:
Stefan Richter138c8af2006-11-02 21:16:08 +0100920 SBP2_ERR("Could not allocate memory for lu");
921 sbp2_remove_device(lu);
Stefan Richtereaceec72005-12-13 11:05:05 -0500922 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Stefan Richter138c8af2006-11-02 21:16:08 +0100925static void sbp2_remove_device(struct sbp2_lu *lu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Stefan Richterca0c7452006-11-02 21:16:08 +0100927 struct sbp2_fwhost_info *hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
Stefan Richter138c8af2006-11-02 21:16:08 +0100929 if (!lu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 return;
931
Stefan Richter138c8af2006-11-02 21:16:08 +0100932 hi = lu->hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
Stefan Richter138c8af2006-11-02 21:16:08 +0100934 if (lu->shost) {
935 scsi_remove_host(lu->shost);
936 scsi_host_put(lu->shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Stefan Richter09ee67a2006-08-14 18:43:00 +0200938 flush_scheduled_work();
Stefan Richter138c8af2006-11-02 21:16:08 +0100939 sbp2util_remove_command_orb_pool(lu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
Stefan Richter138c8af2006-11-02 21:16:08 +0100941 list_del(&lu->lu_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Stefan Richter138c8af2006-11-02 21:16:08 +0100943 if (lu->login_response)
Stefan Richter97d552e2006-12-29 23:47:04 +0100944 dma_free_coherent(hi->host->device.parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 sizeof(struct sbp2_login_response),
Stefan Richter138c8af2006-11-02 21:16:08 +0100946 lu->login_response,
947 lu->login_response_dma);
948 if (lu->login_orb)
Stefan Richter97d552e2006-12-29 23:47:04 +0100949 dma_free_coherent(hi->host->device.parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 sizeof(struct sbp2_login_orb),
Stefan Richter138c8af2006-11-02 21:16:08 +0100951 lu->login_orb,
952 lu->login_orb_dma);
953 if (lu->reconnect_orb)
Stefan Richter97d552e2006-12-29 23:47:04 +0100954 dma_free_coherent(hi->host->device.parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 sizeof(struct sbp2_reconnect_orb),
Stefan Richter138c8af2006-11-02 21:16:08 +0100956 lu->reconnect_orb,
957 lu->reconnect_orb_dma);
958 if (lu->logout_orb)
Stefan Richter97d552e2006-12-29 23:47:04 +0100959 dma_free_coherent(hi->host->device.parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 sizeof(struct sbp2_logout_orb),
Stefan Richter138c8af2006-11-02 21:16:08 +0100961 lu->logout_orb,
962 lu->logout_orb_dma);
963 if (lu->query_logins_orb)
Stefan Richter97d552e2006-12-29 23:47:04 +0100964 dma_free_coherent(hi->host->device.parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 sizeof(struct sbp2_query_logins_orb),
Stefan Richter138c8af2006-11-02 21:16:08 +0100966 lu->query_logins_orb,
967 lu->query_logins_orb_dma);
968 if (lu->query_logins_response)
Stefan Richter97d552e2006-12-29 23:47:04 +0100969 dma_free_coherent(hi->host->device.parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 sizeof(struct sbp2_query_logins_response),
Stefan Richter138c8af2006-11-02 21:16:08 +0100971 lu->query_logins_response,
972 lu->query_logins_response_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Stefan Richter138c8af2006-11-02 21:16:08 +0100974 if (lu->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
Stefan Richter35bdddb2006-01-31 00:13:33 -0500975 hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
Stefan Richter138c8af2006-11-02 21:16:08 +0100976 lu->status_fifo_addr);
Stefan Richter35bdddb2006-01-31 00:13:33 -0500977
Stefan Richter138c8af2006-11-02 21:16:08 +0100978 lu->ud->device.driver_data = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
Stefan Richter147830f2006-03-28 19:54:52 -0500980 if (hi)
981 module_put(hi->host->driver->owner);
982
Stefan Richter138c8af2006-11-02 21:16:08 +0100983 kfree(lu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
986#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
987/*
Stefan Richtere8ca5662006-11-02 21:16:08 +0100988 * Deal with write requests on adapters which do not support physical DMA or
989 * have it switched off.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 */
Stefan Richtera237f352005-11-07 06:31:39 -0500991static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
992 int destid, quadlet_t *data, u64 addr,
993 size_t length, u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994{
Stefan Richtera237f352005-11-07 06:31:39 -0500995 memcpy(bus_to_virt((u32) addr), data, length);
Stefan Richtera237f352005-11-07 06:31:39 -0500996 return RCODE_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997}
998
999/*
Stefan Richtere8ca5662006-11-02 21:16:08 +01001000 * Deal with read requests on adapters which do not support physical DMA or
1001 * have it switched off.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 */
Stefan Richtera237f352005-11-07 06:31:39 -05001003static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
1004 quadlet_t *data, u64 addr, size_t length,
1005 u16 flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006{
Stefan Richtera237f352005-11-07 06:31:39 -05001007 memcpy(data, bus_to_virt((u32) addr), length);
Stefan Richtera237f352005-11-07 06:31:39 -05001008 return RCODE_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010#endif
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012/**************************************
1013 * SBP-2 protocol related section
1014 **************************************/
1015
Stefan Richter138c8af2006-11-02 21:16:08 +01001016static int sbp2_query_logins(struct sbp2_lu *lu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Stefan Richter138c8af2006-11-02 21:16:08 +01001018 struct sbp2_fwhost_info *hi = lu->hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 quadlet_t data[2];
1020 int max_logins;
1021 int active_logins;
1022
Stefan Richter138c8af2006-11-02 21:16:08 +01001023 lu->query_logins_orb->reserved1 = 0x0;
1024 lu->query_logins_orb->reserved2 = 0x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Stefan Richter138c8af2006-11-02 21:16:08 +01001026 lu->query_logins_orb->query_response_lo = lu->query_logins_response_dma;
1027 lu->query_logins_orb->query_response_hi =
1028 ORB_SET_NODE_ID(hi->host->node_id);
1029 lu->query_logins_orb->lun_misc =
1030 ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
1031 lu->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
1032 lu->query_logins_orb->lun_misc |= ORB_SET_LUN(lu->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Stefan Richter138c8af2006-11-02 21:16:08 +01001034 lu->query_logins_orb->reserved_resp_length =
1035 ORB_SET_QUERY_LOGINS_RESP_LENGTH(
1036 sizeof(struct sbp2_query_logins_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Stefan Richter138c8af2006-11-02 21:16:08 +01001038 lu->query_logins_orb->status_fifo_hi =
1039 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1040 lu->query_logins_orb->status_fifo_lo =
1041 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042
Stefan Richter138c8af2006-11-02 21:16:08 +01001043 sbp2util_cpu_to_be32_buffer(lu->query_logins_orb,
1044 sizeof(struct sbp2_query_logins_orb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Stefan Richter138c8af2006-11-02 21:16:08 +01001046 memset(lu->query_logins_response, 0,
1047 sizeof(struct sbp2_query_logins_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
Stefan Richter138c8af2006-11-02 21:16:08 +01001050 data[1] = lu->query_logins_orb_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 sbp2util_cpu_to_be32_buffer(data, 8);
1052
Stefan Richter138c8af2006-11-02 21:16:08 +01001053 hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Stefan Richter138c8af2006-11-02 21:16:08 +01001055 if (sbp2util_access_timeout(lu, 2*HZ)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001057 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 }
1059
Stefan Richter138c8af2006-11-02 21:16:08 +01001060 if (lu->status_block.ORB_offset_lo != lu->query_logins_orb_dma) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 SBP2_INFO("Error querying logins to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001062 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 }
1064
Stefan Richter138c8af2006-11-02 21:16:08 +01001065 if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
Stefan Richter60657722006-07-23 22:18:00 +02001066 SBP2_INFO("Error querying logins to SBP-2 device - failed");
Stefan Richtera237f352005-11-07 06:31:39 -05001067 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 }
1069
Stefan Richter138c8af2006-11-02 21:16:08 +01001070 sbp2util_cpu_to_be32_buffer(lu->query_logins_response,
1071 sizeof(struct sbp2_query_logins_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
Stefan Richter138c8af2006-11-02 21:16:08 +01001073 max_logins = RESPONSE_GET_MAX_LOGINS(
1074 lu->query_logins_response->length_max_logins);
Ben Collins20f45782006-06-12 18:13:11 -04001075 SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Stefan Richter138c8af2006-11-02 21:16:08 +01001077 active_logins = RESPONSE_GET_ACTIVE_LOGINS(
1078 lu->query_logins_response->length_max_logins);
Ben Collins20f45782006-06-12 18:13:11 -04001079 SBP2_INFO("Number of active logins: %d", active_logins);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
1081 if (active_logins >= max_logins) {
Stefan Richtera237f352005-11-07 06:31:39 -05001082 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084
1085 return 0;
1086}
1087
Stefan Richter138c8af2006-11-02 21:16:08 +01001088static int sbp2_login_device(struct sbp2_lu *lu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089{
Stefan Richter138c8af2006-11-02 21:16:08 +01001090 struct sbp2_fwhost_info *hi = lu->hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 quadlet_t data[2];
1092
Stefan Richter138c8af2006-11-02 21:16:08 +01001093 if (!lu->login_orb)
Stefan Richtera237f352005-11-07 06:31:39 -05001094 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Stefan Richter138c8af2006-11-02 21:16:08 +01001096 if (!sbp2_exclusive_login && sbp2_query_logins(lu)) {
Stefan Richterca0c7452006-11-02 21:16:08 +01001097 SBP2_INFO("Device does not support any more concurrent logins");
1098 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100
Stefan Richtere8ca5662006-11-02 21:16:08 +01001101 /* assume no password */
Stefan Richter138c8af2006-11-02 21:16:08 +01001102 lu->login_orb->password_hi = 0;
1103 lu->login_orb->password_lo = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
Stefan Richter138c8af2006-11-02 21:16:08 +01001105 lu->login_orb->login_response_lo = lu->login_response_dma;
1106 lu->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1107 lu->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
Stefan Richtere8ca5662006-11-02 21:16:08 +01001108
1109 /* one second reconnect time */
Stefan Richter138c8af2006-11-02 21:16:08 +01001110 lu->login_orb->lun_misc |= ORB_SET_RECONNECT(0);
1111 lu->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(sbp2_exclusive_login);
1112 lu->login_orb->lun_misc |= ORB_SET_NOTIFY(1);
1113 lu->login_orb->lun_misc |= ORB_SET_LUN(lu->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
Stefan Richter138c8af2006-11-02 21:16:08 +01001115 lu->login_orb->passwd_resp_lengths =
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117
Stefan Richter138c8af2006-11-02 21:16:08 +01001118 lu->login_orb->status_fifo_hi =
1119 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1120 lu->login_orb->status_fifo_lo =
1121 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Stefan Richter138c8af2006-11-02 21:16:08 +01001123 sbp2util_cpu_to_be32_buffer(lu->login_orb,
1124 sizeof(struct sbp2_login_orb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Stefan Richter138c8af2006-11-02 21:16:08 +01001126 memset(lu->login_response, 0, sizeof(struct sbp2_login_response));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
Stefan Richter138c8af2006-11-02 21:16:08 +01001129 data[1] = lu->login_orb_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 sbp2util_cpu_to_be32_buffer(data, 8);
1131
Stefan Richter138c8af2006-11-02 21:16:08 +01001132 hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Stefan Richtere8ca5662006-11-02 21:16:08 +01001134 /* wait up to 20 seconds for login status */
Stefan Richter138c8af2006-11-02 21:16:08 +01001135 if (sbp2util_access_timeout(lu, 20*HZ)) {
Stefan Richtere8398bb2006-07-23 22:19:00 +02001136 SBP2_ERR("Error logging into SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001137 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139
Stefan Richtere8ca5662006-11-02 21:16:08 +01001140 /* make sure that the returned status matches the login ORB */
Stefan Richter138c8af2006-11-02 21:16:08 +01001141 if (lu->status_block.ORB_offset_lo != lu->login_orb_dma) {
Stefan Richter60657722006-07-23 22:18:00 +02001142 SBP2_ERR("Error logging into SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001143 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
1145
Stefan Richter138c8af2006-11-02 21:16:08 +01001146 if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
Stefan Richter60657722006-07-23 22:18:00 +02001147 SBP2_ERR("Error logging into SBP-2 device - failed");
Stefan Richtera237f352005-11-07 06:31:39 -05001148 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 }
1150
Stefan Richter138c8af2006-11-02 21:16:08 +01001151 sbp2util_cpu_to_be32_buffer(lu->login_response,
1152 sizeof(struct sbp2_login_response));
1153 lu->command_block_agent_addr =
1154 ((u64)lu->login_response->command_block_agent_hi) << 32;
1155 lu->command_block_agent_addr |=
1156 ((u64)lu->login_response->command_block_agent_lo);
1157 lu->command_block_agent_addr &= 0x0000ffffffffffffULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 SBP2_INFO("Logged into SBP-2 device");
Stefan Richtera237f352005-11-07 06:31:39 -05001160 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161}
1162
Stefan Richter138c8af2006-11-02 21:16:08 +01001163static int sbp2_logout_device(struct sbp2_lu *lu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
Stefan Richter138c8af2006-11-02 21:16:08 +01001165 struct sbp2_fwhost_info *hi = lu->hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 quadlet_t data[2];
1167 int error;
1168
Stefan Richter138c8af2006-11-02 21:16:08 +01001169 lu->logout_orb->reserved1 = 0x0;
1170 lu->logout_orb->reserved2 = 0x0;
1171 lu->logout_orb->reserved3 = 0x0;
1172 lu->logout_orb->reserved4 = 0x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Stefan Richter138c8af2006-11-02 21:16:08 +01001174 lu->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
1175 lu->logout_orb->login_ID_misc |=
1176 ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
1177 lu->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
Stefan Richter138c8af2006-11-02 21:16:08 +01001179 lu->logout_orb->reserved5 = 0x0;
1180 lu->logout_orb->status_fifo_hi =
1181 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1182 lu->logout_orb->status_fifo_lo =
1183 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Stefan Richter138c8af2006-11-02 21:16:08 +01001185 sbp2util_cpu_to_be32_buffer(lu->logout_orb,
1186 sizeof(struct sbp2_logout_orb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
Stefan Richter138c8af2006-11-02 21:16:08 +01001189 data[1] = lu->logout_orb_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 sbp2util_cpu_to_be32_buffer(data, 8);
1191
Stefan Richter138c8af2006-11-02 21:16:08 +01001192 error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (error)
1194 return error;
1195
Stefan Richtere8ca5662006-11-02 21:16:08 +01001196 /* wait up to 1 second for the device to complete logout */
Stefan Richter138c8af2006-11-02 21:16:08 +01001197 if (sbp2util_access_timeout(lu, HZ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 return -EIO;
1199
1200 SBP2_INFO("Logged out of SBP-2 device");
Stefan Richtera237f352005-11-07 06:31:39 -05001201 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202}
1203
Stefan Richter138c8af2006-11-02 21:16:08 +01001204static int sbp2_reconnect_device(struct sbp2_lu *lu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Stefan Richter138c8af2006-11-02 21:16:08 +01001206 struct sbp2_fwhost_info *hi = lu->hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 quadlet_t data[2];
1208 int error;
1209
Stefan Richter138c8af2006-11-02 21:16:08 +01001210 lu->reconnect_orb->reserved1 = 0x0;
1211 lu->reconnect_orb->reserved2 = 0x0;
1212 lu->reconnect_orb->reserved3 = 0x0;
1213 lu->reconnect_orb->reserved4 = 0x0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Stefan Richter138c8af2006-11-02 21:16:08 +01001215 lu->reconnect_orb->login_ID_misc =
1216 ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
1217 lu->reconnect_orb->login_ID_misc |=
1218 ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
1219 lu->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Stefan Richter138c8af2006-11-02 21:16:08 +01001221 lu->reconnect_orb->reserved5 = 0x0;
1222 lu->reconnect_orb->status_fifo_hi =
1223 ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1224 lu->reconnect_orb->status_fifo_lo =
1225 ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Stefan Richter138c8af2006-11-02 21:16:08 +01001227 sbp2util_cpu_to_be32_buffer(lu->reconnect_orb,
1228 sizeof(struct sbp2_reconnect_orb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
Stefan Richter138c8af2006-11-02 21:16:08 +01001231 data[1] = lu->reconnect_orb_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 sbp2util_cpu_to_be32_buffer(data, 8);
1233
Stefan Richter138c8af2006-11-02 21:16:08 +01001234 error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (error)
1236 return error;
1237
Stefan Richtere8ca5662006-11-02 21:16:08 +01001238 /* wait up to 1 second for reconnect status */
Stefan Richter138c8af2006-11-02 21:16:08 +01001239 if (sbp2util_access_timeout(lu, HZ)) {
Stefan Richtere8398bb2006-07-23 22:19:00 +02001240 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001241 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 }
1243
Stefan Richtere8ca5662006-11-02 21:16:08 +01001244 /* make sure that the returned status matches the reconnect ORB */
Stefan Richter138c8af2006-11-02 21:16:08 +01001245 if (lu->status_block.ORB_offset_lo != lu->reconnect_orb_dma) {
Stefan Richter60657722006-07-23 22:18:00 +02001246 SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
Stefan Richtera237f352005-11-07 06:31:39 -05001247 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 }
1249
Stefan Richter138c8af2006-11-02 21:16:08 +01001250 if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
Stefan Richter60657722006-07-23 22:18:00 +02001251 SBP2_ERR("Error reconnecting to SBP-2 device - failed");
Stefan Richtera237f352005-11-07 06:31:39 -05001252 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
1254
Stefan Richter35644092006-11-02 21:16:08 +01001255 SBP2_INFO("Reconnected to SBP-2 device");
Stefan Richtera237f352005-11-07 06:31:39 -05001256 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
1259/*
Stefan Richtere8ca5662006-11-02 21:16:08 +01001260 * Set the target node's Single Phase Retry limit. Affects the target's retry
1261 * behaviour if our node is too busy to accept requests.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 */
Stefan Richter138c8af2006-11-02 21:16:08 +01001263static int sbp2_set_busy_timeout(struct sbp2_lu *lu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264{
1265 quadlet_t data;
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
Stefan Richter138c8af2006-11-02 21:16:08 +01001268 if (hpsb_node_write(lu->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
Stefan Richterd024ebc2006-03-28 20:03:55 -05001269 SBP2_ERR("%s error", __FUNCTION__);
Stefan Richtera237f352005-11-07 06:31:39 -05001270 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271}
1272
Stefan Richter138c8af2006-11-02 21:16:08 +01001273static void sbp2_parse_unit_directory(struct sbp2_lu *lu,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 struct unit_directory *ud)
1275{
1276 struct csr1212_keyval *kv;
1277 struct csr1212_dentry *dentry;
1278 u64 management_agent_addr;
Stefan Richter9117c6d2006-11-02 21:16:08 +01001279 u32 unit_characteristics, firmware_revision;
Stefan Richter24d3bf82006-05-15 22:04:59 +02001280 unsigned workarounds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 int i;
1282
Stefan Richter9117c6d2006-11-02 21:16:08 +01001283 management_agent_addr = 0;
1284 unit_characteristics = 0;
1285 firmware_revision = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
1288 switch (kv->key.id) {
1289 case CSR1212_KV_ID_DEPENDENT_INFO:
Stefan Richteredf1fb22006-11-02 21:16:08 +01001290 if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 management_agent_addr =
Stefan Richtera237f352005-11-07 06:31:39 -05001292 CSR1212_REGISTER_SPACE_BASE +
1293 (kv->value.csr_offset << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Stefan Richteredf1fb22006-11-02 21:16:08 +01001295 else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE)
Stefan Richter138c8af2006-11-02 21:16:08 +01001296 lu->lun = ORB_SET_LUN(kv->value.immediate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 break;
1298
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 case SBP2_UNIT_CHARACTERISTICS_KEY:
Stefan Richtere8ca5662006-11-02 21:16:08 +01001300 /* FIXME: This is ignored so far.
1301 * See SBP-2 clause 7.4.8. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 unit_characteristics = kv->value.immediate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 break;
1304
1305 case SBP2_FIRMWARE_REVISION_KEY:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 firmware_revision = kv->value.immediate;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 break;
1308
1309 default:
Stefan Richtere8ca5662006-11-02 21:16:08 +01001310 /* FIXME: Check for SBP2_DEVICE_TYPE_AND_LUN_KEY.
1311 * Its "ordered" bit has consequences for command ORB
1312 * list handling. See SBP-2 clauses 4.6, 7.4.11, 10.2 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 break;
1314 }
1315 }
1316
Stefan Richter24d3bf82006-05-15 22:04:59 +02001317 workarounds = sbp2_default_workarounds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
Stefan Richter679c0cd2006-05-15 22:08:09 +02001319 if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
1320 for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
Stefan Richter4618fd32006-12-30 15:37:09 +01001321 if (sbp2_workarounds_table[i].firmware_revision !=
1322 SBP2_ROM_VALUE_WILDCARD &&
Stefan Richter679c0cd2006-05-15 22:08:09 +02001323 sbp2_workarounds_table[i].firmware_revision !=
1324 (firmware_revision & 0xffff00))
1325 continue;
Stefan Richter4618fd32006-12-30 15:37:09 +01001326 if (sbp2_workarounds_table[i].model_id !=
1327 SBP2_ROM_VALUE_WILDCARD &&
Stefan Richter679c0cd2006-05-15 22:08:09 +02001328 sbp2_workarounds_table[i].model_id != ud->model_id)
1329 continue;
1330 workarounds |= sbp2_workarounds_table[i].workarounds;
1331 break;
1332 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
Stefan Richter24d3bf82006-05-15 22:04:59 +02001334 if (workarounds)
Stefan Richtere9a1c522006-05-15 22:06:37 +02001335 SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
1336 "(firmware_revision 0x%06x, vendor_id 0x%06x,"
1337 " model_id 0x%06x)",
Stefan Richter24d3bf82006-05-15 22:04:59 +02001338 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
Stefan Richtere9a1c522006-05-15 22:06:37 +02001339 workarounds, firmware_revision,
1340 ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
1341 ud->model_id);
Stefan Richter24d3bf82006-05-15 22:04:59 +02001342
1343 /* We would need one SCSI host template for each target to adjust
1344 * max_sectors on the fly, therefore warn only. */
1345 if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
Stefan Richterca0c7452006-11-02 21:16:08 +01001346 (sbp2_max_sectors * 512) > (128 * 1024))
Stefan Richter35644092006-11-02 21:16:08 +01001347 SBP2_INFO("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
Stefan Richter24d3bf82006-05-15 22:04:59 +02001348 "max transfer size. WARNING: Current max_sectors "
1349 "setting is larger than 128KB (%d sectors)",
1350 NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
Stefan Richterca0c7452006-11-02 21:16:08 +01001351 sbp2_max_sectors);
Stefan Richter24d3bf82006-05-15 22:04:59 +02001352
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 /* If this is a logical unit directory entry, process the parent
1354 * to get the values. */
1355 if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
Stefan Richter138c8af2006-11-02 21:16:08 +01001356 struct unit_directory *parent_ud = container_of(
1357 ud->device.parent, struct unit_directory, device);
1358 sbp2_parse_unit_directory(lu, parent_ud);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 } else {
Stefan Richter138c8af2006-11-02 21:16:08 +01001360 lu->management_agent_addr = management_agent_addr;
1361 lu->workarounds = workarounds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
Stefan Richter138c8af2006-11-02 21:16:08 +01001363 lu->lun = ORB_SET_LUN(ud->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365}
1366
Ben Collinsfd23ade2006-06-12 18:14:14 -04001367#define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369/*
1370 * This function is called in order to determine the max speed and packet
1371 * size we can use in our ORBs. Note, that we (the driver and host) only
1372 * initiate the transaction. The SBP-2 device actually transfers the data
1373 * (by reading from the DMA area we tell it). This means that the SBP-2
1374 * device decides the actual maximum data it can transfer. We just tell it
1375 * the speed that it needs to use, and the max_rec the host supports, and
1376 * it takes care of the rest.
1377 */
Stefan Richter138c8af2006-11-02 21:16:08 +01001378static int sbp2_max_speed_and_size(struct sbp2_lu *lu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Stefan Richter138c8af2006-11-02 21:16:08 +01001380 struct sbp2_fwhost_info *hi = lu->hi;
Ben Collinsfd23ade2006-06-12 18:14:14 -04001381 u8 payload;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Stefan Richter138c8af2006-11-02 21:16:08 +01001383 lu->speed_code = hi->host->speed[NODEID_TO_NODE(lu->ne->nodeid)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Stefan Richter138c8af2006-11-02 21:16:08 +01001385 if (lu->speed_code > sbp2_max_speed) {
1386 lu->speed_code = sbp2_max_speed;
Stefan Richterca0c7452006-11-02 21:16:08 +01001387 SBP2_INFO("Reducing speed to %s",
1388 hpsb_speedto_str[sbp2_max_speed]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390
1391 /* Payload size is the lesser of what our speed supports and what
1392 * our host supports. */
Stefan Richter138c8af2006-11-02 21:16:08 +01001393 payload = min(sbp2_speedto_max_payload[lu->speed_code],
Ben Collinsfd23ade2006-06-12 18:14:14 -04001394 (u8) (hi->host->csr.max_rec - 1));
1395
1396 /* If physical DMA is off, work around limitation in ohci1394:
1397 * packet size must not exceed PAGE_SIZE */
Stefan Richter138c8af2006-11-02 21:16:08 +01001398 if (lu->ne->host->low_addr_space < (1ULL << 32))
Ben Collinsfd23ade2006-06-12 18:14:14 -04001399 while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
1400 payload)
1401 payload--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
Stefan Richter35644092006-11-02 21:16:08 +01001403 SBP2_INFO("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
Stefan Richter138c8af2006-11-02 21:16:08 +01001404 NODE_BUS_ARGS(hi->host, lu->ne->nodeid),
1405 hpsb_speedto_str[lu->speed_code],
Stefan Richter35644092006-11-02 21:16:08 +01001406 SBP2_PAYLOAD_TO_BYTES(payload));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
Stefan Richter138c8af2006-11-02 21:16:08 +01001408 lu->max_payload_size = payload;
Stefan Richtera237f352005-11-07 06:31:39 -05001409 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410}
1411
Stefan Richter138c8af2006-11-02 21:16:08 +01001412static int sbp2_agent_reset(struct sbp2_lu *lu, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413{
1414 quadlet_t data;
1415 u64 addr;
1416 int retval;
Stefan Richtercc078182006-07-23 22:12:00 +02001417 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Stefan Richterec9b7e12006-12-07 23:23:25 +01001419 /* flush lu->protocol_work */
Stefan Richter09ee67a2006-08-14 18:43:00 +02001420 if (wait)
1421 flush_scheduled_work();
1422
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 data = ntohl(SBP2_AGENT_RESET_DATA);
Stefan Richter138c8af2006-11-02 21:16:08 +01001424 addr = lu->command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425
1426 if (wait)
Stefan Richter138c8af2006-11-02 21:16:08 +01001427 retval = hpsb_node_write(lu->ne, addr, &data, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 else
Stefan Richter138c8af2006-11-02 21:16:08 +01001429 retval = sbp2util_node_write_no_wait(lu->ne, addr, &data, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 if (retval < 0) {
1432 SBP2_ERR("hpsb_node_write failed.\n");
1433 return -EIO;
1434 }
1435
Stefan Richtere8ca5662006-11-02 21:16:08 +01001436 /* make sure that the ORB_POINTER is written on next command */
Stefan Richter138c8af2006-11-02 21:16:08 +01001437 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1438 lu->last_orb = NULL;
1439 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440
Stefan Richtera237f352005-11-07 06:31:39 -05001441 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442}
1443
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001444static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
Stefan Richterca0c7452006-11-02 21:16:08 +01001445 struct sbp2_fwhost_info *hi,
Stefan Richter138c8af2006-11-02 21:16:08 +01001446 struct sbp2_command_info *cmd,
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001447 unsigned int scsi_use_sg,
1448 struct scatterlist *sgpnt,
1449 u32 orb_direction,
1450 enum dma_data_direction dma_dir)
1451{
Stefan Richter138c8af2006-11-02 21:16:08 +01001452 cmd->dma_dir = dma_dir;
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001453 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1454 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1455
Stefan Richtere8ca5662006-11-02 21:16:08 +01001456 /* special case if only one element (and less than 64KB in size) */
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001457 if ((scsi_use_sg == 1) &&
1458 (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
1459
Stefan Richter138c8af2006-11-02 21:16:08 +01001460 cmd->dma_size = sgpnt[0].length;
1461 cmd->dma_type = CMD_DMA_PAGE;
Stefan Richter97d552e2006-12-29 23:47:04 +01001462 cmd->cmd_dma = dma_map_page(hi->host->device.parent,
Stefan Richter138c8af2006-11-02 21:16:08 +01001463 sgpnt[0].page, sgpnt[0].offset,
1464 cmd->dma_size, cmd->dma_dir);
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001465
Stefan Richter138c8af2006-11-02 21:16:08 +01001466 orb->data_descriptor_lo = cmd->cmd_dma;
1467 orb->misc |= ORB_SET_DATA_SIZE(cmd->dma_size);
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001468
1469 } else {
1470 struct sbp2_unrestricted_page_table *sg_element =
Stefan Richter138c8af2006-11-02 21:16:08 +01001471 &cmd->scatter_gather_element[0];
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001472 u32 sg_count, sg_len;
1473 dma_addr_t sg_addr;
Stefan Richter97d552e2006-12-29 23:47:04 +01001474 int i, count = dma_map_sg(hi->host->device.parent, sgpnt,
1475 scsi_use_sg, dma_dir);
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001476
Stefan Richter138c8af2006-11-02 21:16:08 +01001477 cmd->dma_size = scsi_use_sg;
1478 cmd->sge_buffer = sgpnt;
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001479
1480 /* use page tables (s/g) */
1481 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
Stefan Richter138c8af2006-11-02 21:16:08 +01001482 orb->data_descriptor_lo = cmd->sge_dma;
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001483
Stefan Richtere8ca5662006-11-02 21:16:08 +01001484 /* loop through and fill out our SBP-2 page tables
1485 * (and split up anything too large) */
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001486 for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
1487 sg_len = sg_dma_len(sgpnt);
1488 sg_addr = sg_dma_address(sgpnt);
1489 while (sg_len) {
1490 sg_element[sg_count].segment_base_lo = sg_addr;
1491 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1492 sg_element[sg_count].length_segment_base_hi =
1493 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1494 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1495 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1496 } else {
1497 sg_element[sg_count].length_segment_base_hi =
1498 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1499 sg_len = 0;
1500 }
1501 sg_count++;
1502 }
1503 }
1504
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001505 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1506
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001507 sbp2util_cpu_to_be32_buffer(sg_element,
Stefan Richter138c8af2006-11-02 21:16:08 +01001508 (sizeof(struct sbp2_unrestricted_page_table)) *
1509 sg_count);
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001510 }
1511}
1512
1513static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
Stefan Richterca0c7452006-11-02 21:16:08 +01001514 struct sbp2_fwhost_info *hi,
Stefan Richter138c8af2006-11-02 21:16:08 +01001515 struct sbp2_command_info *cmd,
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001516 struct scatterlist *sgpnt,
1517 u32 orb_direction,
1518 unsigned int scsi_request_bufflen,
1519 void *scsi_request_buffer,
1520 enum dma_data_direction dma_dir)
1521{
Stefan Richter138c8af2006-11-02 21:16:08 +01001522 cmd->dma_dir = dma_dir;
1523 cmd->dma_size = scsi_request_bufflen;
1524 cmd->dma_type = CMD_DMA_SINGLE;
Stefan Richter97d552e2006-12-29 23:47:04 +01001525 cmd->cmd_dma = dma_map_single(hi->host->device.parent,
1526 scsi_request_buffer,
Stefan Richter138c8af2006-11-02 21:16:08 +01001527 cmd->dma_size, cmd->dma_dir);
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001528 orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1529 orb->misc |= ORB_SET_DIRECTION(orb_direction);
1530
Stefan Richtere8ca5662006-11-02 21:16:08 +01001531 /* handle case where we get a command w/o s/g enabled
1532 * (but check for transfers larger than 64K) */
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001533 if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1534
Stefan Richter138c8af2006-11-02 21:16:08 +01001535 orb->data_descriptor_lo = cmd->cmd_dma;
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001536 orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
1537
1538 } else {
Stefan Richtere8ca5662006-11-02 21:16:08 +01001539 /* The buffer is too large. Turn this into page tables. */
1540
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001541 struct sbp2_unrestricted_page_table *sg_element =
Stefan Richter138c8af2006-11-02 21:16:08 +01001542 &cmd->scatter_gather_element[0];
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001543 u32 sg_count, sg_len;
1544 dma_addr_t sg_addr;
1545
Stefan Richter138c8af2006-11-02 21:16:08 +01001546 orb->data_descriptor_lo = cmd->sge_dma;
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001547 orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1548
Stefan Richtere8ca5662006-11-02 21:16:08 +01001549 /* fill out our SBP-2 page tables; split up the large buffer */
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001550 sg_count = 0;
1551 sg_len = scsi_request_bufflen;
Stefan Richter138c8af2006-11-02 21:16:08 +01001552 sg_addr = cmd->cmd_dma;
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001553 while (sg_len) {
1554 sg_element[sg_count].segment_base_lo = sg_addr;
1555 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1556 sg_element[sg_count].length_segment_base_hi =
1557 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1558 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1559 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1560 } else {
1561 sg_element[sg_count].length_segment_base_hi =
1562 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1563 sg_len = 0;
1564 }
1565 sg_count++;
1566 }
1567
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001568 orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1569
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001570 sbp2util_cpu_to_be32_buffer(sg_element,
Stefan Richter138c8af2006-11-02 21:16:08 +01001571 (sizeof(struct sbp2_unrestricted_page_table)) *
1572 sg_count);
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001573 }
1574}
1575
Stefan Richter138c8af2006-11-02 21:16:08 +01001576static void sbp2_create_command_orb(struct sbp2_lu *lu,
1577 struct sbp2_command_info *cmd,
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001578 unchar *scsi_cmd,
1579 unsigned int scsi_use_sg,
1580 unsigned int scsi_request_bufflen,
1581 void *scsi_request_buffer,
1582 enum dma_data_direction dma_dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Stefan Richter138c8af2006-11-02 21:16:08 +01001584 struct sbp2_fwhost_info *hi = lu->hi;
Stefan Richtera237f352005-11-07 06:31:39 -05001585 struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
Stefan Richter138c8af2006-11-02 21:16:08 +01001586 struct sbp2_command_orb *orb = &cmd->command_orb;
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001587 u32 orb_direction;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 /*
Stefan Richtere8ca5662006-11-02 21:16:08 +01001590 * Set-up our command ORB.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 *
1592 * NOTE: We're doing unrestricted page tables (s/g), as this is
1593 * best performance (at least with the devices I have). This means
1594 * that data_size becomes the number of s/g elements, and
1595 * page_size should be zero (for unrestricted).
1596 */
Stefan Richter138c8af2006-11-02 21:16:08 +01001597 orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1598 orb->next_ORB_lo = 0x0;
1599 orb->misc = ORB_SET_MAX_PAYLOAD(lu->max_payload_size);
1600 orb->misc |= ORB_SET_SPEED(lu->speed_code);
1601 orb->misc |= ORB_SET_NOTIFY(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602
Stefan Richter43863eb2005-12-12 23:03:24 -05001603 if (dma_dir == DMA_NONE)
Stefan Richtera237f352005-11-07 06:31:39 -05001604 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
Stefan Richter43863eb2005-12-12 23:03:24 -05001605 else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
Stefan Richtera237f352005-11-07 06:31:39 -05001606 orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
Stefan Richter43863eb2005-12-12 23:03:24 -05001607 else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
Stefan Richtera237f352005-11-07 06:31:39 -05001608 orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
Stefan Richter43863eb2005-12-12 23:03:24 -05001609 else {
Stefan Richter35644092006-11-02 21:16:08 +01001610 SBP2_INFO("Falling back to DMA_NONE");
Stefan Richter43863eb2005-12-12 23:03:24 -05001611 orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
1613
Stefan Richtere8ca5662006-11-02 21:16:08 +01001614 /* set up our page table stuff */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
Stefan Richter138c8af2006-11-02 21:16:08 +01001616 orb->data_descriptor_hi = 0x0;
1617 orb->data_descriptor_lo = 0x0;
1618 orb->misc |= ORB_SET_DIRECTION(1);
Stefan Richteredf1fb22006-11-02 21:16:08 +01001619 } else if (scsi_use_sg)
Stefan Richter138c8af2006-11-02 21:16:08 +01001620 sbp2_prep_command_orb_sg(orb, hi, cmd, scsi_use_sg, sgpnt,
1621 orb_direction, dma_dir);
Stefan Richteredf1fb22006-11-02 21:16:08 +01001622 else
Stefan Richter138c8af2006-11-02 21:16:08 +01001623 sbp2_prep_command_orb_no_sg(orb, hi, cmd, sgpnt, orb_direction,
1624 scsi_request_bufflen,
Stefan Richtercf8d2c02005-12-13 11:05:03 -05001625 scsi_request_buffer, dma_dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
Stefan Richter138c8af2006-11-02 21:16:08 +01001627 sbp2util_cpu_to_be32_buffer(orb, sizeof(*orb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Stefan Richter138c8af2006-11-02 21:16:08 +01001629 memset(orb->cdb, 0, 12);
1630 memcpy(orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631}
1632
Stefan Richter138c8af2006-11-02 21:16:08 +01001633static void sbp2_link_orb_command(struct sbp2_lu *lu,
1634 struct sbp2_command_info *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
Stefan Richter138c8af2006-11-02 21:16:08 +01001636 struct sbp2_fwhost_info *hi = lu->hi;
Stefan Richtercc078182006-07-23 22:12:00 +02001637 struct sbp2_command_orb *last_orb;
1638 dma_addr_t last_orb_dma;
Stefan Richter138c8af2006-11-02 21:16:08 +01001639 u64 addr = lu->command_block_agent_addr;
Stefan Richtercc078182006-07-23 22:12:00 +02001640 quadlet_t data[2];
1641 size_t length;
1642 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Stefan Richter97d552e2006-12-29 23:47:04 +01001644 dma_sync_single_for_device(hi->host->device.parent,
1645 cmd->command_orb_dma,
Stefan Richter9b7d9c02006-11-22 21:44:34 +01001646 sizeof(struct sbp2_command_orb),
1647 DMA_TO_DEVICE);
Stefan Richter97d552e2006-12-29 23:47:04 +01001648 dma_sync_single_for_device(hi->host->device.parent, cmd->sge_dma,
Stefan Richter9b7d9c02006-11-22 21:44:34 +01001649 sizeof(cmd->scatter_gather_element),
Stefan Richter2446a792007-02-04 20:54:57 +01001650 DMA_TO_DEVICE);
Stefan Richtere8ca5662006-11-02 21:16:08 +01001651
1652 /* check to see if there are any previous orbs to use */
Stefan Richter138c8af2006-11-02 21:16:08 +01001653 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1654 last_orb = lu->last_orb;
1655 last_orb_dma = lu->last_orb_dma;
Stefan Richtercc078182006-07-23 22:12:00 +02001656 if (!last_orb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 /*
Stefan Richtercc078182006-07-23 22:12:00 +02001658 * last_orb == NULL means: We know that the target's fetch agent
1659 * is not active right now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 */
Stefan Richtercc078182006-07-23 22:12:00 +02001661 addr += SBP2_ORB_POINTER_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
Stefan Richter138c8af2006-11-02 21:16:08 +01001663 data[1] = cmd->command_orb_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 sbp2util_cpu_to_be32_buffer(data, 8);
Stefan Richtercc078182006-07-23 22:12:00 +02001665 length = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 /*
Stefan Richtercc078182006-07-23 22:12:00 +02001668 * last_orb != NULL means: We know that the target's fetch agent
1669 * is (very probably) not dead or in reset state right now.
1670 * We have an ORB already sent that we can append a new one to.
1671 * The target's fetch agent may or may not have read this
1672 * previous ORB yet.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 */
Stefan Richter97d552e2006-12-29 23:47:04 +01001674 dma_sync_single_for_cpu(hi->host->device.parent, last_orb_dma,
Stefan Richter9b7d9c02006-11-22 21:44:34 +01001675 sizeof(struct sbp2_command_orb),
1676 DMA_TO_DEVICE);
Stefan Richter138c8af2006-11-02 21:16:08 +01001677 last_orb->next_ORB_lo = cpu_to_be32(cmd->command_orb_dma);
Stefan Richtercc078182006-07-23 22:12:00 +02001678 wmb();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 /* Tells hardware that this pointer is valid */
Stefan Richtercc078182006-07-23 22:12:00 +02001680 last_orb->next_ORB_hi = 0;
Stefan Richter97d552e2006-12-29 23:47:04 +01001681 dma_sync_single_for_device(hi->host->device.parent,
1682 last_orb_dma,
Stefan Richter9b7d9c02006-11-22 21:44:34 +01001683 sizeof(struct sbp2_command_orb),
1684 DMA_TO_DEVICE);
Stefan Richtercc078182006-07-23 22:12:00 +02001685 addr += SBP2_DOORBELL_OFFSET;
1686 data[0] = 0;
1687 length = 4;
1688 }
Stefan Richter138c8af2006-11-02 21:16:08 +01001689 lu->last_orb = &cmd->command_orb;
1690 lu->last_orb_dma = cmd->command_orb_dma;
1691 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Stefan Richter138c8af2006-11-02 21:16:08 +01001693 if (sbp2util_node_write_no_wait(lu->ne, addr, data, length)) {
Stefan Richter09ee67a2006-08-14 18:43:00 +02001694 /*
1695 * sbp2util_node_write_no_wait failed. We certainly ran out
1696 * of transaction labels, perhaps just because there were no
1697 * context switches which gave khpsbpkt a chance to collect
1698 * free tlabels. Try again in non-atomic context. If necessary,
1699 * the workqueue job will sleep to guaranteedly get a tlabel.
1700 * We do not accept new commands until the job is over.
1701 */
Stefan Richter138c8af2006-11-02 21:16:08 +01001702 scsi_block_requests(lu->shost);
1703 PREPARE_WORK(&lu->protocol_work,
Stefan Richter09ee67a2006-08-14 18:43:00 +02001704 last_orb ? sbp2util_write_doorbell:
Stefan Richterec9b7e12006-12-07 23:23:25 +01001705 sbp2util_write_orb_pointer);
Stefan Richter138c8af2006-11-02 21:16:08 +01001706 schedule_work(&lu->protocol_work);
Stefan Richter09ee67a2006-08-14 18:43:00 +02001707 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
Stefan Richter138c8af2006-11-02 21:16:08 +01001710static int sbp2_send_command(struct sbp2_lu *lu, struct scsi_cmnd *SCpnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 void (*done)(struct scsi_cmnd *))
1712{
Stefan Richter138c8af2006-11-02 21:16:08 +01001713 unchar *scsi_cmd = (unchar *)SCpnt->cmnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 unsigned int request_bufflen = SCpnt->request_bufflen;
Stefan Richter138c8af2006-11-02 21:16:08 +01001715 struct sbp2_command_info *cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Stefan Richter138c8af2006-11-02 21:16:08 +01001717 cmd = sbp2util_allocate_command_orb(lu, SCpnt, done);
1718 if (!cmd)
Stefan Richtera237f352005-11-07 06:31:39 -05001719 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Stefan Richter138c8af2006-11-02 21:16:08 +01001721 sbp2_create_command_orb(lu, cmd, scsi_cmd, SCpnt->use_sg,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 request_bufflen, SCpnt->request_buffer,
1723 SCpnt->sc_data_direction);
Stefan Richter138c8af2006-11-02 21:16:08 +01001724 sbp2_link_orb_command(lu, cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
Stefan Richtera237f352005-11-07 06:31:39 -05001726 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 * Translates SBP-2 status into SCSI sense data for check conditions
1731 */
Stefan Richter138c8af2006-11-02 21:16:08 +01001732static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status,
1733 unchar *sense_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Stefan Richtere8ca5662006-11-02 21:16:08 +01001735 /* OK, it's pretty ugly... ;-) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 sense_data[0] = 0x70;
1737 sense_data[1] = 0x0;
1738 sense_data[2] = sbp2_status[9];
1739 sense_data[3] = sbp2_status[12];
1740 sense_data[4] = sbp2_status[13];
1741 sense_data[5] = sbp2_status[14];
1742 sense_data[6] = sbp2_status[15];
1743 sense_data[7] = 10;
1744 sense_data[8] = sbp2_status[16];
1745 sense_data[9] = sbp2_status[17];
1746 sense_data[10] = sbp2_status[18];
1747 sense_data[11] = sbp2_status[19];
1748 sense_data[12] = sbp2_status[10];
1749 sense_data[13] = sbp2_status[11];
1750 sense_data[14] = sbp2_status[20];
1751 sense_data[15] = sbp2_status[21];
1752
Stefan Richtere8ca5662006-11-02 21:16:08 +01001753 return sbp2_status[8] & 0x3f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
Stefan Richter3e98eab42006-07-23 22:16:00 +02001756static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
1757 int destid, quadlet_t *data, u64 addr,
1758 size_t length, u16 fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Stefan Richterca0c7452006-11-02 21:16:08 +01001760 struct sbp2_fwhost_info *hi;
Stefan Richter138c8af2006-11-02 21:16:08 +01001761 struct sbp2_lu *lu = NULL, *lu_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 struct scsi_cmnd *SCpnt = NULL;
Stefan Richter3e98eab42006-07-23 22:16:00 +02001763 struct sbp2_status_block *sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
Stefan Richter138c8af2006-11-02 21:16:08 +01001765 struct sbp2_command_info *cmd;
Jody McIntyre79456192005-11-07 06:29:39 -05001766 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Stefan Richter60657722006-07-23 22:18:00 +02001768 if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
1769 SBP2_ERR("Wrong size of status block");
1770 return RCODE_ADDRESS_ERROR;
1771 }
1772 if (unlikely(!host)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 SBP2_ERR("host is NULL - this is bad!");
Stefan Richtera237f352005-11-07 06:31:39 -05001774 return RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
Stefan Richter60657722006-07-23 22:18:00 +02001777 if (unlikely(!hi)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 SBP2_ERR("host info is NULL - this is bad!");
Stefan Richtera237f352005-11-07 06:31:39 -05001779 return RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 }
Stefan Richtere8ca5662006-11-02 21:16:08 +01001781
1782 /* Find the unit which wrote the status. */
Stefan Richter138c8af2006-11-02 21:16:08 +01001783 list_for_each_entry(lu_tmp, &hi->logical_units, lu_list) {
1784 if (lu_tmp->ne->nodeid == nodeid &&
1785 lu_tmp->status_fifo_addr == addr) {
1786 lu = lu_tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 break;
1788 }
1789 }
Stefan Richter138c8af2006-11-02 21:16:08 +01001790 if (unlikely(!lu)) {
1791 SBP2_ERR("lu is NULL - device is gone?");
Stefan Richtera237f352005-11-07 06:31:39 -05001792 return RCODE_ADDRESS_ERROR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794
Stefan Richter138c8af2006-11-02 21:16:08 +01001795 /* Put response into lu status fifo buffer. The first two bytes
Stefan Richter3e98eab42006-07-23 22:16:00 +02001796 * come in big endian bit order. Often the target writes only a
1797 * truncated status block, minimally the first two quadlets. The rest
Stefan Richtere8ca5662006-11-02 21:16:08 +01001798 * is implied to be zeros. */
Stefan Richter138c8af2006-11-02 21:16:08 +01001799 sb = &lu->status_block;
Stefan Richter3e98eab42006-07-23 22:16:00 +02001800 memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
1801 memcpy(sb, data, length);
1802 sbp2util_be32_to_cpu_buffer(sb, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Stefan Richtere8ca5662006-11-02 21:16:08 +01001804 /* Ignore unsolicited status. Handle command ORB status. */
Stefan Richter60657722006-07-23 22:18:00 +02001805 if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
Stefan Richter138c8af2006-11-02 21:16:08 +01001806 cmd = NULL;
Stefan Richter60657722006-07-23 22:18:00 +02001807 else
Stefan Richter138c8af2006-11-02 21:16:08 +01001808 cmd = sbp2util_find_command_for_orb(lu, sb->ORB_offset_lo);
1809 if (cmd) {
Stefan Richter97d552e2006-12-29 23:47:04 +01001810 dma_sync_single_for_cpu(hi->host->device.parent,
1811 cmd->command_orb_dma,
Stefan Richter9b7d9c02006-11-22 21:44:34 +01001812 sizeof(struct sbp2_command_orb),
1813 DMA_TO_DEVICE);
Stefan Richter97d552e2006-12-29 23:47:04 +01001814 dma_sync_single_for_cpu(hi->host->device.parent, cmd->sge_dma,
Stefan Richter9b7d9c02006-11-22 21:44:34 +01001815 sizeof(cmd->scatter_gather_element),
Stefan Richter2446a792007-02-04 20:54:57 +01001816 DMA_TO_DEVICE);
Stefan Richtere8ca5662006-11-02 21:16:08 +01001817 /* Grab SCSI command pointers and check status. */
Stefan Richter60657722006-07-23 22:18:00 +02001818 /*
1819 * FIXME: If the src field in the status is 1, the ORB DMA must
1820 * not be reused until status for a subsequent ORB is received.
1821 */
Stefan Richter138c8af2006-11-02 21:16:08 +01001822 SCpnt = cmd->Current_SCpnt;
1823 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1824 sbp2util_mark_command_completed(lu, cmd);
1825 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
1827 if (SCpnt) {
Stefan Richterabbca102006-08-14 18:51:00 +02001828 u32 h = sb->ORB_offset_hi_misc;
1829 u32 r = STATUS_GET_RESP(h);
1830
1831 if (r != RESP_STATUS_REQUEST_COMPLETE) {
Stefan Richter35644092006-11-02 21:16:08 +01001832 SBP2_INFO("resp 0x%x, sbp_status 0x%x",
Stefan Richterabbca102006-08-14 18:51:00 +02001833 r, STATUS_GET_SBP_STATUS(h));
Stefan Richter60657722006-07-23 22:18:00 +02001834 scsi_status =
Stefan Richterabbca102006-08-14 18:51:00 +02001835 r == RESP_STATUS_TRANSPORT_FAILURE ?
1836 SBP2_SCSI_STATUS_BUSY :
Stefan Richter60657722006-07-23 22:18:00 +02001837 SBP2_SCSI_STATUS_COMMAND_TERMINATED;
Stefan Richterabbca102006-08-14 18:51:00 +02001838 }
Stefan Richtere8ca5662006-11-02 21:16:08 +01001839
Stefan Richteredf1fb22006-11-02 21:16:08 +01001840 if (STATUS_GET_LEN(h) > 1)
Stefan Richter3e98eab42006-07-23 22:16:00 +02001841 scsi_status = sbp2_status_to_sense_data(
1842 (unchar *)sb, SCpnt->sense_buffer);
Stefan Richtere8ca5662006-11-02 21:16:08 +01001843
Stefan Richteredf1fb22006-11-02 21:16:08 +01001844 if (STATUS_TEST_DEAD(h))
Stefan Richter138c8af2006-11-02 21:16:08 +01001845 sbp2_agent_reset(lu, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
1847
Stefan Richtere8ca5662006-11-02 21:16:08 +01001848 /* Check here to see if there are no commands in-use. If there
Stefan Richtercc078182006-07-23 22:12:00 +02001849 * are none, we know that the fetch agent left the active state
1850 * _and_ that we did not reactivate it yet. Therefore clear
1851 * last_orb so that next time we write directly to the
1852 * ORB_POINTER register. That way the fetch agent does not need
Stefan Richtere8ca5662006-11-02 21:16:08 +01001853 * to refetch the next_ORB. */
Stefan Richter138c8af2006-11-02 21:16:08 +01001854 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1855 if (list_empty(&lu->cmd_orb_inuse))
1856 lu->last_orb = NULL;
1857 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
1859 } else {
Stefan Richtere8ca5662006-11-02 21:16:08 +01001860 /* It's probably status after a management request. */
Stefan Richter138c8af2006-11-02 21:16:08 +01001861 if ((sb->ORB_offset_lo == lu->reconnect_orb_dma) ||
1862 (sb->ORB_offset_lo == lu->login_orb_dma) ||
1863 (sb->ORB_offset_lo == lu->query_logins_orb_dma) ||
1864 (sb->ORB_offset_lo == lu->logout_orb_dma)) {
1865 lu->access_complete = 1;
Stefan Richterca0c7452006-11-02 21:16:08 +01001866 wake_up_interruptible(&sbp2_access_wq);
Stefan Richtere8398bb2006-07-23 22:19:00 +02001867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 }
1869
Stefan Richteredf1fb22006-11-02 21:16:08 +01001870 if (SCpnt)
Stefan Richter138c8af2006-11-02 21:16:08 +01001871 sbp2scsi_complete_command(lu, scsi_status, SCpnt,
1872 cmd->Current_done);
Stefan Richtera237f352005-11-07 06:31:39 -05001873 return RCODE_COMPLETE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874}
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876/**************************************
1877 * SCSI interface related section
1878 **************************************/
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
1881 void (*done)(struct scsi_cmnd *))
1882{
Stefan Richter138c8af2006-11-02 21:16:08 +01001883 struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
Stefan Richterca0c7452006-11-02 21:16:08 +01001884 struct sbp2_fwhost_info *hi;
Jody McIntyreabd559b2005-09-30 11:59:06 -07001885 int result = DID_NO_CONNECT << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Stefan Richter138c8af2006-11-02 21:16:08 +01001887 if (unlikely(!sbp2util_node_is_available(lu)))
Jody McIntyreabd559b2005-09-30 11:59:06 -07001888 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889
Stefan Richter138c8af2006-11-02 21:16:08 +01001890 hi = lu->hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Stefan Richter5796aa72006-11-02 21:16:08 +01001892 if (unlikely(!hi)) {
Stefan Richterca0c7452006-11-02 21:16:08 +01001893 SBP2_ERR("sbp2_fwhost_info is NULL - this is bad!");
Jody McIntyreabd559b2005-09-30 11:59:06 -07001894 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896
Stefan Richtere8ca5662006-11-02 21:16:08 +01001897 /* Multiple units are currently represented to the SCSI core as separate
1898 * targets, not as one target with multiple LUs. Therefore return
1899 * selection time-out to any IO directed at non-zero LUNs. */
Stefan Richter5796aa72006-11-02 21:16:08 +01001900 if (unlikely(SCpnt->device->lun))
Jody McIntyreabd559b2005-09-30 11:59:06 -07001901 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Stefan Richter138c8af2006-11-02 21:16:08 +01001903 if (unlikely(!hpsb_node_entry_valid(lu->ne))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 SBP2_ERR("Bus reset in progress - rejecting command");
Jody McIntyreabd559b2005-09-30 11:59:06 -07001905 result = DID_BUS_BUSY << 16;
1906 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
1908
Stefan Richtere8ca5662006-11-02 21:16:08 +01001909 /* Bidirectional commands are not yet implemented,
1910 * and unknown transfer direction not handled. */
Stefan Richter5796aa72006-11-02 21:16:08 +01001911 if (unlikely(SCpnt->sc_data_direction == DMA_BIDIRECTIONAL)) {
Stefan Richter43863eb2005-12-12 23:03:24 -05001912 SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
1913 result = DID_ERROR << 16;
1914 goto done;
1915 }
1916
Stefan Richter138c8af2006-11-02 21:16:08 +01001917 if (sbp2_send_command(lu, SCpnt, done)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 SBP2_ERR("Error sending SCSI command");
Stefan Richter138c8af2006-11-02 21:16:08 +01001919 sbp2scsi_complete_command(lu,
1920 SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 SCpnt, done);
1922 }
Jody McIntyreabd559b2005-09-30 11:59:06 -07001923 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Jody McIntyreabd559b2005-09-30 11:59:06 -07001925done:
1926 SCpnt->result = result;
1927 done(SCpnt);
1928 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}
1930
Stefan Richter138c8af2006-11-02 21:16:08 +01001931static void sbp2scsi_complete_all_commands(struct sbp2_lu *lu, u32 status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932{
Stefan Richter138c8af2006-11-02 21:16:08 +01001933 struct sbp2_fwhost_info *hi = lu->hi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 struct list_head *lh;
Stefan Richter138c8af2006-11-02 21:16:08 +01001935 struct sbp2_command_info *cmd;
Jody McIntyre79456192005-11-07 06:29:39 -05001936 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Stefan Richter138c8af2006-11-02 21:16:08 +01001938 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1939 while (!list_empty(&lu->cmd_orb_inuse)) {
1940 lh = lu->cmd_orb_inuse.next;
1941 cmd = list_entry(lh, struct sbp2_command_info, list);
Stefan Richter97d552e2006-12-29 23:47:04 +01001942 dma_sync_single_for_cpu(hi->host->device.parent,
1943 cmd->command_orb_dma,
Stefan Richter9b7d9c02006-11-22 21:44:34 +01001944 sizeof(struct sbp2_command_orb),
1945 DMA_TO_DEVICE);
Stefan Richter97d552e2006-12-29 23:47:04 +01001946 dma_sync_single_for_cpu(hi->host->device.parent, cmd->sge_dma,
Stefan Richter9b7d9c02006-11-22 21:44:34 +01001947 sizeof(cmd->scatter_gather_element),
Stefan Richter2446a792007-02-04 20:54:57 +01001948 DMA_TO_DEVICE);
Stefan Richter138c8af2006-11-02 21:16:08 +01001949 sbp2util_mark_command_completed(lu, cmd);
1950 if (cmd->Current_SCpnt) {
1951 cmd->Current_SCpnt->result = status << 16;
1952 cmd->Current_done(cmd->Current_SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 }
1954 }
Stefan Richter138c8af2006-11-02 21:16:08 +01001955 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957 return;
1958}
1959
1960/*
Stefan Richtere8ca5662006-11-02 21:16:08 +01001961 * Complete a regular SCSI command. Can be called in atomic context.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 */
Stefan Richter138c8af2006-11-02 21:16:08 +01001963static void sbp2scsi_complete_command(struct sbp2_lu *lu, u32 scsi_status,
1964 struct scsi_cmnd *SCpnt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 void (*done)(struct scsi_cmnd *))
1966{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 if (!SCpnt) {
1968 SBP2_ERR("SCpnt is NULL");
1969 return;
1970 }
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 switch (scsi_status) {
Stefan Richtera237f352005-11-07 06:31:39 -05001973 case SBP2_SCSI_STATUS_GOOD:
Stefan Richter8f0525f2006-03-28 20:03:45 -05001974 SCpnt->result = DID_OK << 16;
Stefan Richtera237f352005-11-07 06:31:39 -05001975 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Stefan Richtera237f352005-11-07 06:31:39 -05001977 case SBP2_SCSI_STATUS_BUSY:
1978 SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
1979 SCpnt->result = DID_BUS_BUSY << 16;
1980 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Stefan Richtera237f352005-11-07 06:31:39 -05001982 case SBP2_SCSI_STATUS_CHECK_CONDITION:
Stefan Richter8f0525f2006-03-28 20:03:45 -05001983 SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
Stefan Richtera237f352005-11-07 06:31:39 -05001984 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
Stefan Richtera237f352005-11-07 06:31:39 -05001986 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
1987 SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
1988 SCpnt->result = DID_NO_CONNECT << 16;
1989 scsi_print_command(SCpnt);
1990 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
Stefan Richtera237f352005-11-07 06:31:39 -05001992 case SBP2_SCSI_STATUS_CONDITION_MET:
1993 case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
1994 case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
1995 SBP2_ERR("Bad SCSI status = %x", scsi_status);
1996 SCpnt->result = DID_ERROR << 16;
1997 scsi_print_command(SCpnt);
1998 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
Stefan Richtera237f352005-11-07 06:31:39 -05002000 default:
2001 SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
2002 SCpnt->result = DID_ERROR << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
2004
Stefan Richtere8ca5662006-11-02 21:16:08 +01002005 /* If a bus reset is in progress and there was an error, complete
2006 * the command as busy so that it will get retried. */
Stefan Richter138c8af2006-11-02 21:16:08 +01002007 if (!hpsb_node_entry_valid(lu->ne)
Stefan Richtera237f352005-11-07 06:31:39 -05002008 && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 SBP2_ERR("Completing command with busy (bus reset)");
2010 SCpnt->result = DID_BUS_BUSY << 16;
2011 }
2012
Stefan Richtere8ca5662006-11-02 21:16:08 +01002013 /* Tell the SCSI stack that we're done with this command. */
Stefan Richtera237f352005-11-07 06:31:39 -05002014 done(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015}
2016
Jody McIntyreabd559b2005-09-30 11:59:06 -07002017static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
2018{
Stefan Richter138c8af2006-11-02 21:16:08 +01002019 struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
Stefan Richtera80614d2006-02-14 22:04:19 -05002020
Stefan Richter138c8af2006-11-02 21:16:08 +01002021 lu->sdev = sdev;
Stefan Richterc394f1e2006-08-07 20:48:00 +02002022 sdev->allow_restart = 1;
Stefan Richtera80614d2006-02-14 22:04:19 -05002023
Stefan Richter138c8af2006-11-02 21:16:08 +01002024 if (lu->workarounds & SBP2_WORKAROUND_INQUIRY_36)
Stefan Richtera80614d2006-02-14 22:04:19 -05002025 sdev->inquiry_len = 36;
Jody McIntyreabd559b2005-09-30 11:59:06 -07002026 return 0;
2027}
2028
Jody McIntyreabd559b2005-09-30 11:59:06 -07002029static int sbp2scsi_slave_configure(struct scsi_device *sdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030{
Stefan Richter138c8af2006-11-02 21:16:08 +01002031 struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
Stefan Richter24d3bf82006-05-15 22:04:59 +02002032
Ben Collins365c7862005-11-07 06:31:24 -05002033 sdev->use_10_for_rw = 1;
Stefan Richter24d3bf82006-05-15 22:04:59 +02002034
Stefan Richter1a74bc62007-01-10 20:17:15 +01002035 if (sdev->type == TYPE_ROM)
2036 sdev->use_10_for_ms = 1;
Stefan Richter24d3bf82006-05-15 22:04:59 +02002037 if (sdev->type == TYPE_DISK &&
Stefan Richter138c8af2006-11-02 21:16:08 +01002038 lu->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
Stefan Richter24d3bf82006-05-15 22:04:59 +02002039 sdev->skip_ms_page_8 = 1;
Stefan Richter138c8af2006-11-02 21:16:08 +01002040 if (lu->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
Stefan Richtere9a1c522006-05-15 22:06:37 +02002041 sdev->fix_capacity = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return 0;
2043}
2044
Jody McIntyreabd559b2005-09-30 11:59:06 -07002045static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
2046{
Stefan Richter138c8af2006-11-02 21:16:08 +01002047 ((struct sbp2_lu *)sdev->host->hostdata[0])->sdev = NULL;
Jody McIntyreabd559b2005-09-30 11:59:06 -07002048 return;
2049}
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051/*
Stefan Richtere8ca5662006-11-02 21:16:08 +01002052 * Called by scsi stack when something has really gone wrong.
2053 * Usually called when a command has timed-out for some reason.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 */
2055static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
2056{
Stefan Richter138c8af2006-11-02 21:16:08 +01002057 struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
2058 struct sbp2_fwhost_info *hi = lu->hi;
2059 struct sbp2_command_info *cmd;
Stefan Richter24c7cd02006-04-01 21:11:41 +02002060 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061
Stefan Richter35644092006-11-02 21:16:08 +01002062 SBP2_INFO("aborting sbp2 command");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 scsi_print_command(SCpnt);
2064
Stefan Richter138c8af2006-11-02 21:16:08 +01002065 if (sbp2util_node_is_available(lu)) {
2066 sbp2_agent_reset(lu, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Stefan Richter23077f12006-09-11 20:17:14 +02002068 /* Return a matching command structure to the free pool. */
Stefan Richter138c8af2006-11-02 21:16:08 +01002069 spin_lock_irqsave(&lu->cmd_orb_lock, flags);
2070 cmd = sbp2util_find_command_for_SCpnt(lu, SCpnt);
2071 if (cmd) {
Stefan Richter97d552e2006-12-29 23:47:04 +01002072 dma_sync_single_for_cpu(hi->host->device.parent,
Stefan Richter138c8af2006-11-02 21:16:08 +01002073 cmd->command_orb_dma,
2074 sizeof(struct sbp2_command_orb),
Stefan Richter9b7d9c02006-11-22 21:44:34 +01002075 DMA_TO_DEVICE);
Stefan Richter97d552e2006-12-29 23:47:04 +01002076 dma_sync_single_for_cpu(hi->host->device.parent,
2077 cmd->sge_dma,
Stefan Richter138c8af2006-11-02 21:16:08 +01002078 sizeof(cmd->scatter_gather_element),
Stefan Richter2446a792007-02-04 20:54:57 +01002079 DMA_TO_DEVICE);
Stefan Richter138c8af2006-11-02 21:16:08 +01002080 sbp2util_mark_command_completed(lu, cmd);
2081 if (cmd->Current_SCpnt) {
2082 cmd->Current_SCpnt->result = DID_ABORT << 16;
2083 cmd->Current_done(cmd->Current_SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 }
2085 }
Stefan Richter138c8af2006-11-02 21:16:08 +01002086 spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Stefan Richter138c8af2006-11-02 21:16:08 +01002088 sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 }
2090
Stefan Richtera237f352005-11-07 06:31:39 -05002091 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092}
2093
2094/*
2095 * Called by scsi stack when something has really gone wrong.
2096 */
Jody McIntyreabd559b2005-09-30 11:59:06 -07002097static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098{
Stefan Richter138c8af2006-11-02 21:16:08 +01002099 struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Stefan Richter35644092006-11-02 21:16:08 +01002101 SBP2_INFO("reset requested");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
Stefan Richter138c8af2006-11-02 21:16:08 +01002103 if (sbp2util_node_is_available(lu)) {
Stefan Richter35644092006-11-02 21:16:08 +01002104 SBP2_INFO("generating sbp2 fetch agent reset");
Stefan Richter138c8af2006-11-02 21:16:08 +01002105 sbp2_agent_reset(lu, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
2107
Jody McIntyreabd559b2005-09-30 11:59:06 -07002108 return SUCCESS;
Jeff Garzik 94d0e7b82005-05-28 07:55:48 -04002109}
2110
Stefan Richtera237f352005-11-07 06:31:39 -05002111static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
2112 struct device_attribute *attr,
2113 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114{
2115 struct scsi_device *sdev;
Stefan Richter138c8af2006-11-02 21:16:08 +01002116 struct sbp2_lu *lu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 if (!(sdev = to_scsi_device(dev)))
2119 return 0;
2120
Stefan Richter138c8af2006-11-02 21:16:08 +01002121 if (!(lu = (struct sbp2_lu *)sdev->host->hostdata[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 return 0;
2123
Stefan Richterd7794c82007-05-27 13:17:15 +02002124 if (sbp2_long_sysfs_ieee1394_id)
2125 return sprintf(buf, "%016Lx:%06x:%04x\n",
2126 (unsigned long long)lu->ne->guid,
2127 lu->ud->directory_id, ORB_SET_LUN(lu->lun));
2128 else
2129 return sprintf(buf, "%016Lx:%d:%d\n",
2130 (unsigned long long)lu->ne->guid,
2131 lu->ud->id, ORB_SET_LUN(lu->lun));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2135MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2136MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2137MODULE_LICENSE("GPL");
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139static int sbp2_module_init(void)
2140{
2141 int ret;
2142
Stefan Richterca0c7452006-11-02 21:16:08 +01002143 if (sbp2_serialize_io) {
2144 sbp2_shost_template.can_queue = 1;
2145 sbp2_shost_template.cmd_per_lun = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
2147
Stefan Richter24d3bf82006-05-15 22:04:59 +02002148 if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
Stefan Richterca0c7452006-11-02 21:16:08 +01002149 (sbp2_max_sectors * 512) > (128 * 1024))
2150 sbp2_max_sectors = 128 * 1024 / 512;
2151 sbp2_shost_template.max_sectors = sbp2_max_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 hpsb_register_highlevel(&sbp2_highlevel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 ret = hpsb_register_protocol(&sbp2_driver);
2155 if (ret) {
2156 SBP2_ERR("Failed to register protocol");
2157 hpsb_unregister_highlevel(&sbp2_highlevel);
2158 return ret;
2159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 return 0;
2161}
2162
2163static void __exit sbp2_module_exit(void)
2164{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 hpsb_unregister_protocol(&sbp2_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 hpsb_unregister_highlevel(&sbp2_highlevel);
2167}
2168
2169module_init(sbp2_module_init);
2170module_exit(sbp2_module_exit);