1/*
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#include <linux/compiler.h>
26#include <linux/delay.h>
27#include <linux/device.h>
28#include <linux/dma-mapping.h>
29#include <linux/gfp.h>
30#include <linux/init.h>
31#include <linux/kernel.h>
32#include <linux/list.h>
33#include <linux/mm.h>
34#include <linux/module.h>
35#include <linux/moduleparam.h>
36#include <linux/sched.h>
37#include <linux/slab.h>
38#include <linux/spinlock.h>
39#include <linux/stat.h>
40#include <linux/string.h>
41#include <linux/stringify.h>
42#include <linux/types.h>
43#include <linux/wait.h>
44#include <linux/workqueue.h>
45
46#include <asm/byteorder.h>
47#include <asm/errno.h>
48#include <asm/param.h>
49#include <asm/scatterlist.h>
50#include <asm/system.h>
51#include <asm/types.h>
52
53#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
54#include <asm/io.h> /* for bus_to_virt */
55#endif
56
57#include <scsi/scsi.h>
58#include <scsi/scsi_cmnd.h>
59#include <scsi/scsi_dbg.h>
60#include <scsi/scsi_device.h>
61#include <scsi/scsi_host.h>
62
63#include "csr1212.h"
64#include "highlevel.h"
65#include "hosts.h"
66#include "ieee1394.h"
67#include "ieee1394_core.h"
68#include "ieee1394_hotplug.h"
69#include "ieee1394_transactions.h"
70#include "ieee1394_types.h"
71#include "nodemgr.h"
72#include "sbp2.h"
73
74/*
75 * Module load parameter definitions
76 */
77
78/*
79 * Change max_speed on module load if you have a bad IEEE-1394
80 * controller that has trouble running 2KB packets at 400mb.
81 *
82 * NOTE: On certain OHCI parts I have seen short packets on async transmit
83 * (probably due to PCI latency/throughput issues with the part). You can
84 * bump down the speed if you are running into problems.
85 */
86static int sbp2_max_speed = IEEE1394_SPEED_MAX;
87module_param_named(max_speed, sbp2_max_speed, int, 0644);
88MODULE_PARM_DESC(max_speed, "Force max speed "
89		 "(3 = 800Mb/s, 2 = 400Mb/s, 1 = 200Mb/s, 0 = 100Mb/s)");
90
91/*
92 * Set serialize_io to 1 if you'd like only one scsi command sent
93 * down to us at a time (debugging). This might be necessary for very
94 * badly behaved sbp2 devices.
95 */
96static int sbp2_serialize_io = 1;
97module_param_named(serialize_io, sbp2_serialize_io, int, 0444);
98MODULE_PARM_DESC(serialize_io, "Serialize I/O coming from scsi drivers "
99		 "(default = 1, faster = 0)");
100
101/*
102 * Bump up max_sectors if you'd like to support very large sized
103 * transfers. Please note that some older sbp2 bridge chips are broken for
104 * transfers greater or equal to 128KB.  Default is a value of 255
105 * sectors, or just under 128KB (at 512 byte sector size). I can note that
106 * the Oxsemi sbp2 chipsets have no problems supporting very large
107 * transfer sizes.
108 */
109static int sbp2_max_sectors = SBP2_MAX_SECTORS;
110module_param_named(max_sectors, sbp2_max_sectors, int, 0444);
111MODULE_PARM_DESC(max_sectors, "Change max sectors per I/O supported "
112		 "(default = " __stringify(SBP2_MAX_SECTORS) ")");
113
114/*
115 * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
116 * do an exclusive login, as it's generally unsafe to have two hosts
117 * talking to a single sbp2 device at the same time (filesystem coherency,
118 * etc.). If you're running an sbp2 device that supports multiple logins,
119 * and you're either running read-only filesystems or some sort of special
120 * filesystem supporting multiple hosts, e.g. OpenGFS, Oracle Cluster
121 * File System, or Lustre, then set exclusive_login to zero.
122 *
123 * So far only bridges from Oxford Semiconductor are known to support
124 * concurrent logins. Depending on firmware, four or two concurrent logins
125 * are possible on OXFW911 and newer Oxsemi bridges.
126 */
127static int sbp2_exclusive_login = 1;
128module_param_named(exclusive_login, sbp2_exclusive_login, int, 0644);
129MODULE_PARM_DESC(exclusive_login, "Exclusive login to sbp2 device "
130		 "(default = 1)");
131
132/*
133 * If any of the following workarounds is required for your device to work,
134 * please submit the kernel messages logged by sbp2 to the linux1394-devel
135 * mailing list.
136 *
137 * - 128kB max transfer
138 *   Limit transfer size. Necessary for some old bridges.
139 *
140 * - 36 byte inquiry
141 *   When scsi_mod probes the device, let the inquiry command look like that
142 *   from MS Windows.
143 *
144 * - skip mode page 8
145 *   Suppress sending of mode_sense for mode page 8 if the device pretends to
146 *   support the SCSI Primary Block commands instead of Reduced Block Commands.
147 *
148 * - fix capacity
149 *   Tell sd_mod to correct the last sector number reported by read_capacity.
150 *   Avoids access beyond actual disk limits on devices with an off-by-one bug.
151 *   Don't use this with devices which don't have this bug.
152 *
153 * - override internal blacklist
154 *   Instead of adding to the built-in blacklist, use only the workarounds
155 *   specified in the module load parameter.
156 *   Useful if a blacklist entry interfered with a non-broken device.
157 */
158static int sbp2_default_workarounds;
159module_param_named(workarounds, sbp2_default_workarounds, int, 0644);
160MODULE_PARM_DESC(workarounds, "Work around device bugs (default = 0"
161	", 128kB max transfer = " __stringify(SBP2_WORKAROUND_128K_MAX_TRANS)
162	", 36 byte inquiry = "    __stringify(SBP2_WORKAROUND_INQUIRY_36)
163	", skip mode page 8 = "   __stringify(SBP2_WORKAROUND_MODE_SENSE_8)
164	", fix capacity = "       __stringify(SBP2_WORKAROUND_FIX_CAPACITY)
165	", override internal blacklist = " __stringify(SBP2_WORKAROUND_OVERRIDE)
166	", or a combination)");
167
168/*
169 * This influences the format of the sysfs attribute
170 * /sys/bus/scsi/devices/.../ieee1394_id.
171 *
172 * The default format is like in older kernels:  %016Lx:%d:%d
173 * It contains the target's EUI-64, a number given to the logical unit by
174 * the ieee1394 driver's nodemgr (starting at 0), and the LUN.
175 *
176 * The long format is:  %016Lx:%06x:%04x
177 * It contains the target's EUI-64, the unit directory's directory_ID as per
178 * IEEE 1212 clause 7.7.19, and the LUN.  This format comes closest to the
179 * format of SBP(-3) target port and logical unit identifier as per SAM (SCSI
180 * Architecture Model) rev.2 to 4 annex A.  Therefore and because it is
181 * independent of the implementation of the ieee1394 nodemgr, the longer format
182 * is recommended for future use.
183 */
184static int sbp2_long_sysfs_ieee1394_id;
185module_param_named(long_ieee1394_id, sbp2_long_sysfs_ieee1394_id, bool, 0644);
186MODULE_PARM_DESC(long_ieee1394_id, "8+3+2 bytes format of ieee1394_id in sysfs "
187		 "(default = backwards-compatible = N, SAM-conforming = Y)");
188
189
190#define SBP2_INFO(fmt, args...)	HPSB_INFO("sbp2: "fmt, ## args)
191#define SBP2_ERR(fmt, args...)	HPSB_ERR("sbp2: "fmt, ## args)
192
193/*
194 * Globals
195 */
196static void sbp2scsi_complete_all_commands(struct sbp2_lu *, u32);
197static void sbp2scsi_complete_command(struct sbp2_lu *, u32, struct scsi_cmnd *,
198				      void (*)(struct scsi_cmnd *));
199static struct sbp2_lu *sbp2_alloc_device(struct unit_directory *);
200static int sbp2_start_device(struct sbp2_lu *);
201static void sbp2_remove_device(struct sbp2_lu *);
202static int sbp2_login_device(struct sbp2_lu *);
203static int sbp2_reconnect_device(struct sbp2_lu *);
204static int sbp2_logout_device(struct sbp2_lu *);
205static void sbp2_host_reset(struct hpsb_host *);
206static int sbp2_handle_status_write(struct hpsb_host *, int, int, quadlet_t *,
207				    u64, size_t, u16);
208static int sbp2_agent_reset(struct sbp2_lu *, int);
209static void sbp2_parse_unit_directory(struct sbp2_lu *,
210				      struct unit_directory *);
211static int sbp2_set_busy_timeout(struct sbp2_lu *);
212static int sbp2_max_speed_and_size(struct sbp2_lu *);
213
214
215static const u8 sbp2_speedto_max_payload[] = { 0x7, 0x8, 0x9, 0xA, 0xB, 0xC };
216
217static struct hpsb_highlevel sbp2_highlevel = {
218	.name		= SBP2_DEVICE_NAME,
219	.host_reset	= sbp2_host_reset,
220};
221
222static struct hpsb_address_ops sbp2_ops = {
223	.write		= sbp2_handle_status_write
224};
225
226#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
227static int sbp2_handle_physdma_write(struct hpsb_host *, int, int, quadlet_t *,
228				     u64, size_t, u16);
229static int sbp2_handle_physdma_read(struct hpsb_host *, int, quadlet_t *, u64,
230				    size_t, u16);
231
232static struct hpsb_address_ops sbp2_physdma_ops = {
233	.read		= sbp2_handle_physdma_read,
234	.write		= sbp2_handle_physdma_write,
235};
236#endif
237
238
239/*
240 * Interface to driver core and IEEE 1394 core
241 */
242static struct ieee1394_device_id sbp2_id_table[] = {
243	{
244	 .match_flags	= IEEE1394_MATCH_SPECIFIER_ID | IEEE1394_MATCH_VERSION,
245	 .specifier_id	= SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
246	 .version	= SBP2_SW_VERSION_ENTRY & 0xffffff},
247	{}
248};
249MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
250
251static int sbp2_probe(struct device *);
252static int sbp2_remove(struct device *);
253static int sbp2_update(struct unit_directory *);
254
255static struct hpsb_protocol_driver sbp2_driver = {
256	.name		= SBP2_DEVICE_NAME,
257	.id_table	= sbp2_id_table,
258	.update		= sbp2_update,
259	.driver		= {
260		.probe		= sbp2_probe,
261		.remove		= sbp2_remove,
262	},
263};
264
265
266/*
267 * Interface to SCSI core
268 */
269static int sbp2scsi_queuecommand(struct scsi_cmnd *,
270				 void (*)(struct scsi_cmnd *));
271static int sbp2scsi_abort(struct scsi_cmnd *);
272static int sbp2scsi_reset(struct scsi_cmnd *);
273static int sbp2scsi_slave_alloc(struct scsi_device *);
274static int sbp2scsi_slave_configure(struct scsi_device *);
275static void sbp2scsi_slave_destroy(struct scsi_device *);
276static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *,
277					   struct device_attribute *, char *);
278
279static DEVICE_ATTR(ieee1394_id, S_IRUGO, sbp2_sysfs_ieee1394_id_show, NULL);
280
281static struct device_attribute *sbp2_sysfs_sdev_attrs[] = {
282	&dev_attr_ieee1394_id,
283	NULL
284};
285
286static struct scsi_host_template sbp2_shost_template = {
287	.module			 = THIS_MODULE,
288	.name			 = "SBP-2 IEEE-1394",
289	.proc_name		 = SBP2_DEVICE_NAME,
290	.queuecommand		 = sbp2scsi_queuecommand,
291	.eh_abort_handler	 = sbp2scsi_abort,
292	.eh_device_reset_handler = sbp2scsi_reset,
293	.slave_alloc		 = sbp2scsi_slave_alloc,
294	.slave_configure	 = sbp2scsi_slave_configure,
295	.slave_destroy		 = sbp2scsi_slave_destroy,
296	.this_id		 = -1,
297	.sg_tablesize		 = SG_ALL,
298	.use_clustering		 = ENABLE_CLUSTERING,
299	.cmd_per_lun		 = SBP2_MAX_CMDS,
300	.can_queue		 = SBP2_MAX_CMDS,
301	.sdev_attrs		 = sbp2_sysfs_sdev_attrs,
302};
303
304/* for match-all entries in sbp2_workarounds_table */
305#define SBP2_ROM_VALUE_WILDCARD 0x1000000
306
307/*
308 * List of devices with known bugs.
309 *
310 * The firmware_revision field, masked with 0xffff00, is the best indicator
311 * for the type of bridge chip of a device.  It yields a few false positives
312 * but this did not break correctly behaving devices so far.
313 */
314static const struct {
315	u32 firmware_revision;
316	u32 model_id;
317	unsigned workarounds;
318} sbp2_workarounds_table[] = {
319	/* DViCO Momobay CX-1 with TSB42AA9 bridge */ {
320		.firmware_revision	= 0x002800,
321		.model_id		= 0x001010,
322		.workarounds		= SBP2_WORKAROUND_INQUIRY_36 |
323					  SBP2_WORKAROUND_MODE_SENSE_8,
324	},
325	/* Initio bridges, actually only needed for some older ones */ {
326		.firmware_revision	= 0x000200,
327		.model_id		= SBP2_ROM_VALUE_WILDCARD,
328		.workarounds		= SBP2_WORKAROUND_INQUIRY_36,
329	},
330	/* Symbios bridge */ {
331		.firmware_revision	= 0xa0b800,
332		.model_id		= SBP2_ROM_VALUE_WILDCARD,
333		.workarounds		= SBP2_WORKAROUND_128K_MAX_TRANS,
334	},
335	/* iPod 4th generation */ {
336		.firmware_revision	= 0x0a2700,
337		.model_id		= 0x000021,
338		.workarounds		= SBP2_WORKAROUND_FIX_CAPACITY,
339	},
340	/* iPod mini */ {
341		.firmware_revision	= 0x0a2700,
342		.model_id		= 0x000023,
343		.workarounds		= SBP2_WORKAROUND_FIX_CAPACITY,
344	},
345	/* iPod Photo */ {
346		.firmware_revision	= 0x0a2700,
347		.model_id		= 0x00007e,
348		.workarounds		= SBP2_WORKAROUND_FIX_CAPACITY,
349	}
350};
351
352/**************************************
353 * General utility functions
354 **************************************/
355
356#ifndef __BIG_ENDIAN
357/*
358 * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
359 */
360static inline void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
361{
362	u32 *temp = buffer;
363
364	for (length = (length >> 2); length--; )
365		temp[length] = be32_to_cpu(temp[length]);
366}
367
368/*
369 * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
370 */
371static inline void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
372{
373	u32 *temp = buffer;
374
375	for (length = (length >> 2); length--; )
376		temp[length] = cpu_to_be32(temp[length]);
377}
378#else /* BIG_ENDIAN */
379/* Why waste the cpu cycles? */
380#define sbp2util_be32_to_cpu_buffer(x,y) do {} while (0)
381#define sbp2util_cpu_to_be32_buffer(x,y) do {} while (0)
382#endif
383
384static DECLARE_WAIT_QUEUE_HEAD(sbp2_access_wq);
385
386/*
387 * Waits for completion of an SBP-2 access request.
388 * Returns nonzero if timed out or prematurely interrupted.
389 */
390static int sbp2util_access_timeout(struct sbp2_lu *lu, int timeout)
391{
392	long leftover;
393
394	leftover = wait_event_interruptible_timeout(
395			sbp2_access_wq, lu->access_complete, timeout);
396	lu->access_complete = 0;
397	return leftover <= 0;
398}
399
400static void sbp2_free_packet(void *packet)
401{
402	hpsb_free_tlabel(packet);
403	hpsb_free_packet(packet);
404}
405
406/*
407 * This is much like hpsb_node_write(), except it ignores the response
408 * subaction and returns immediately. Can be used from atomic context.
409 */
410static int sbp2util_node_write_no_wait(struct node_entry *ne, u64 addr,
411				       quadlet_t *buf, size_t len)
412{
413	struct hpsb_packet *packet;
414
415	packet = hpsb_make_writepacket(ne->host, ne->nodeid, addr, buf, len);
416	if (!packet)
417		return -ENOMEM;
418
419	hpsb_set_packet_complete_task(packet, sbp2_free_packet, packet);
420	hpsb_node_fill_packet(ne, packet);
421	if (hpsb_send_packet(packet) < 0) {
422		sbp2_free_packet(packet);
423		return -EIO;
424	}
425	return 0;
426}
427
428static void sbp2util_notify_fetch_agent(struct sbp2_lu *lu, u64 offset,
429					quadlet_t *data, size_t len)
430{
431	/* There is a small window after a bus reset within which the node
432	 * entry's generation is current but the reconnect wasn't completed. */
433	if (unlikely(atomic_read(&lu->state) == SBP2LU_STATE_IN_RESET))
434		return;
435
436	if (hpsb_node_write(lu->ne, lu->command_block_agent_addr + offset,
437			    data, len))
438		SBP2_ERR("sbp2util_notify_fetch_agent failed.");
439
440	/* Now accept new SCSI commands, unless a bus reset happended during
441	 * hpsb_node_write. */
442	if (likely(atomic_read(&lu->state) != SBP2LU_STATE_IN_RESET))
443		scsi_unblock_requests(lu->shost);
444}
445
446static void sbp2util_write_orb_pointer(struct work_struct *work)
447{
448	struct sbp2_lu *lu = container_of(work, struct sbp2_lu, protocol_work);
449	quadlet_t data[2];
450
451	data[0] = ORB_SET_NODE_ID(lu->hi->host->node_id);
452	data[1] = lu->last_orb_dma;
453	sbp2util_cpu_to_be32_buffer(data, 8);
454	sbp2util_notify_fetch_agent(lu, SBP2_ORB_POINTER_OFFSET, data, 8);
455}
456
457static void sbp2util_write_doorbell(struct work_struct *work)
458{
459	struct sbp2_lu *lu = container_of(work, struct sbp2_lu, protocol_work);
460
461	sbp2util_notify_fetch_agent(lu, SBP2_DOORBELL_OFFSET, NULL, 4);
462}
463
464static int sbp2util_create_command_orb_pool(struct sbp2_lu *lu)
465{
466	struct sbp2_fwhost_info *hi = lu->hi;
467	struct sbp2_command_info *cmd;
468	int i, orbs = sbp2_serialize_io ? 2 : SBP2_MAX_CMDS;
469
470	for (i = 0; i < orbs; i++) {
471		cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
472		if (!cmd)
473			return -ENOMEM;
474		cmd->command_orb_dma = dma_map_single(hi->host->device.parent,
475						&cmd->command_orb,
476						sizeof(struct sbp2_command_orb),
477						DMA_TO_DEVICE);
478		cmd->sge_dma = dma_map_single(hi->host->device.parent,
479					&cmd->scatter_gather_element,
480					sizeof(cmd->scatter_gather_element),
481					DMA_TO_DEVICE);
482		INIT_LIST_HEAD(&cmd->list);
483		list_add_tail(&cmd->list, &lu->cmd_orb_completed);
484	}
485	return 0;
486}
487
488static void sbp2util_remove_command_orb_pool(struct sbp2_lu *lu)
489{
490	struct hpsb_host *host = lu->hi->host;
491	struct list_head *lh, *next;
492	struct sbp2_command_info *cmd;
493	unsigned long flags;
494
495	spin_lock_irqsave(&lu->cmd_orb_lock, flags);
496	if (!list_empty(&lu->cmd_orb_completed))
497		list_for_each_safe(lh, next, &lu->cmd_orb_completed) {
498			cmd = list_entry(lh, struct sbp2_command_info, list);
499			dma_unmap_single(host->device.parent,
500					 cmd->command_orb_dma,
501					 sizeof(struct sbp2_command_orb),
502					 DMA_TO_DEVICE);
503			dma_unmap_single(host->device.parent, cmd->sge_dma,
504					 sizeof(cmd->scatter_gather_element),
505					 DMA_TO_DEVICE);
506			kfree(cmd);
507		}
508	spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
509	return;
510}
511
512/*
513 * Finds the sbp2_command for a given outstanding command ORB.
514 * Only looks at the in-use list.
515 */
516static struct sbp2_command_info *sbp2util_find_command_for_orb(
517				struct sbp2_lu *lu, dma_addr_t orb)
518{
519	struct sbp2_command_info *cmd;
520	unsigned long flags;
521
522	spin_lock_irqsave(&lu->cmd_orb_lock, flags);
523	if (!list_empty(&lu->cmd_orb_inuse))
524		list_for_each_entry(cmd, &lu->cmd_orb_inuse, list)
525			if (cmd->command_orb_dma == orb) {
526				spin_unlock_irqrestore(
527						&lu->cmd_orb_lock, flags);
528				return cmd;
529			}
530	spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
531	return NULL;
532}
533
534/*
535 * Finds the sbp2_command for a given outstanding SCpnt.
536 * Only looks at the in-use list.
537 * Must be called with lu->cmd_orb_lock held.
538 */
539static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(
540				struct sbp2_lu *lu, void *SCpnt)
541{
542	struct sbp2_command_info *cmd;
543
544	if (!list_empty(&lu->cmd_orb_inuse))
545		list_for_each_entry(cmd, &lu->cmd_orb_inuse, list)
546			if (cmd->Current_SCpnt == SCpnt)
547				return cmd;
548	return NULL;
549}
550
551static struct sbp2_command_info *sbp2util_allocate_command_orb(
552				struct sbp2_lu *lu,
553				struct scsi_cmnd *Current_SCpnt,
554				void (*Current_done)(struct scsi_cmnd *))
555{
556	struct list_head *lh;
557	struct sbp2_command_info *cmd = NULL;
558	unsigned long flags;
559
560	spin_lock_irqsave(&lu->cmd_orb_lock, flags);
561	if (!list_empty(&lu->cmd_orb_completed)) {
562		lh = lu->cmd_orb_completed.next;
563		list_del(lh);
564		cmd = list_entry(lh, struct sbp2_command_info, list);
565		cmd->Current_done = Current_done;
566		cmd->Current_SCpnt = Current_SCpnt;
567		list_add_tail(&cmd->list, &lu->cmd_orb_inuse);
568	} else
569		SBP2_ERR("%s: no orbs available", __FUNCTION__);
570	spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
571	return cmd;
572}
573
574/*
575 * Unmaps the DMAs of a command and moves the command to the completed ORB list.
576 * Must be called with lu->cmd_orb_lock held.
577 */
578static void sbp2util_mark_command_completed(struct sbp2_lu *lu,
579					    struct sbp2_command_info *cmd)
580{
581	struct hpsb_host *host = lu->ud->ne->host;
582
583	if (cmd->cmd_dma) {
584		if (cmd->dma_type == CMD_DMA_SINGLE)
585			dma_unmap_single(host->device.parent, cmd->cmd_dma,
586					 cmd->dma_size, cmd->dma_dir);
587		else if (cmd->dma_type == CMD_DMA_PAGE)
588			dma_unmap_page(host->device.parent, cmd->cmd_dma,
589				       cmd->dma_size, cmd->dma_dir);
590		cmd->dma_type = CMD_DMA_NONE;
591		cmd->cmd_dma = 0;
592	}
593	if (cmd->sge_buffer) {
594		dma_unmap_sg(host->device.parent, cmd->sge_buffer,
595			     cmd->dma_size, cmd->dma_dir);
596		cmd->sge_buffer = NULL;
597	}
598	list_move_tail(&cmd->list, &lu->cmd_orb_completed);
599}
600
601/*
602 * Is lu valid? Is the 1394 node still present?
603 */
604static inline int sbp2util_node_is_available(struct sbp2_lu *lu)
605{
606	return lu && lu->ne && !lu->ne->in_limbo;
607}
608
609/*********************************************
610 * IEEE-1394 core driver stack related section
611 *********************************************/
612
613static int sbp2_probe(struct device *dev)
614{
615	struct unit_directory *ud;
616	struct sbp2_lu *lu;
617
618	ud = container_of(dev, struct unit_directory, device);
619
620	/* Don't probe UD's that have the LUN flag. We'll probe the LUN(s)
621	 * instead. */
622	if (ud->flags & UNIT_DIRECTORY_HAS_LUN_DIRECTORY)
623		return -ENODEV;
624
625	lu = sbp2_alloc_device(ud);
626	if (!lu)
627		return -ENOMEM;
628
629	sbp2_parse_unit_directory(lu, ud);
630	return sbp2_start_device(lu);
631}
632
633static int sbp2_remove(struct device *dev)
634{
635	struct unit_directory *ud;
636	struct sbp2_lu *lu;
637	struct scsi_device *sdev;
638
639	ud = container_of(dev, struct unit_directory, device);
640	lu = ud->device.driver_data;
641	if (!lu)
642		return 0;
643
644	if (lu->shost) {
645		/* Get rid of enqueued commands if there is no chance to
646		 * send them. */
647		if (!sbp2util_node_is_available(lu))
648			sbp2scsi_complete_all_commands(lu, DID_NO_CONNECT);
649		/* scsi_remove_device() may trigger shutdown functions of SCSI
650		 * highlevel drivers which would deadlock if blocked. */
651		atomic_set(&lu->state, SBP2LU_STATE_IN_SHUTDOWN);
652		scsi_unblock_requests(lu->shost);
653	}
654	sdev = lu->sdev;
655	if (sdev) {
656		lu->sdev = NULL;
657		scsi_remove_device(sdev);
658	}
659
660	sbp2_logout_device(lu);
661	sbp2_remove_device(lu);
662
663	return 0;
664}
665
666static int sbp2_update(struct unit_directory *ud)
667{
668	struct sbp2_lu *lu = ud->device.driver_data;
669
670	if (sbp2_reconnect_device(lu)) {
671		/* Reconnect has failed. Perhaps we didn't reconnect fast
672		 * enough. Try a regular login, but first log out just in
673		 * case of any weirdness. */
674		sbp2_logout_device(lu);
675
676		if (sbp2_login_device(lu)) {
677			/* Login failed too, just fail, and the backend
678			 * will call our sbp2_remove for us */
679			SBP2_ERR("Failed to reconnect to sbp2 device!");
680			return -EBUSY;
681		}
682	}
683
684	sbp2_set_busy_timeout(lu);
685	sbp2_agent_reset(lu, 1);
686	sbp2_max_speed_and_size(lu);
687
688	/* Complete any pending commands with busy (so they get retried)
689	 * and remove them from our queue. */
690	sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
691
692	/* Accept new commands unless there was another bus reset in the
693	 * meantime. */
694	if (hpsb_node_entry_valid(lu->ne)) {
695		atomic_set(&lu->state, SBP2LU_STATE_RUNNING);
696		scsi_unblock_requests(lu->shost);
697	}
698	return 0;
699}
700
701static struct sbp2_lu *sbp2_alloc_device(struct unit_directory *ud)
702{
703	struct sbp2_fwhost_info *hi;
704	struct Scsi_Host *shost = NULL;
705	struct sbp2_lu *lu = NULL;
706
707	lu = kzalloc(sizeof(*lu), GFP_KERNEL);
708	if (!lu) {
709		SBP2_ERR("failed to create lu");
710		goto failed_alloc;
711	}
712
713	lu->ne = ud->ne;
714	lu->ud = ud;
715	lu->speed_code = IEEE1394_SPEED_100;
716	lu->max_payload_size = sbp2_speedto_max_payload[IEEE1394_SPEED_100];
717	lu->status_fifo_addr = CSR1212_INVALID_ADDR_SPACE;
718	INIT_LIST_HEAD(&lu->cmd_orb_inuse);
719	INIT_LIST_HEAD(&lu->cmd_orb_completed);
720	INIT_LIST_HEAD(&lu->lu_list);
721	spin_lock_init(&lu->cmd_orb_lock);
722	atomic_set(&lu->state, SBP2LU_STATE_RUNNING);
723	INIT_WORK(&lu->protocol_work, NULL);
724
725	ud->device.driver_data = lu;
726
727	hi = hpsb_get_hostinfo(&sbp2_highlevel, ud->ne->host);
728	if (!hi) {
729		hi = hpsb_create_hostinfo(&sbp2_highlevel, ud->ne->host,
730					  sizeof(*hi));
731		if (!hi) {
732			SBP2_ERR("failed to allocate hostinfo");
733			goto failed_alloc;
734		}
735		hi->host = ud->ne->host;
736		INIT_LIST_HEAD(&hi->logical_units);
737
738#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
739		/* Handle data movement if physical dma is not
740		 * enabled or not supported on host controller */
741		if (!hpsb_register_addrspace(&sbp2_highlevel, ud->ne->host,
742					     &sbp2_physdma_ops,
743					     0x0ULL, 0xfffffffcULL)) {
744			SBP2_ERR("failed to register lower 4GB address range");
745			goto failed_alloc;
746		}
747#else
748		if (dma_set_mask(hi->host->device.parent, DMA_32BIT_MASK)) {
749			SBP2_ERR("failed to set 4GB DMA mask");
750			goto failed_alloc;
751		}
752#endif
753	}
754
755	/* Prevent unloading of the 1394 host */
756	if (!try_module_get(hi->host->driver->owner)) {
757		SBP2_ERR("failed to get a reference on 1394 host driver");
758		goto failed_alloc;
759	}
760
761	lu->hi = hi;
762
763	list_add_tail(&lu->lu_list, &hi->logical_units);
764
765	/* Register the status FIFO address range. We could use the same FIFO
766	 * for targets at different nodes. However we need different FIFOs per
767	 * target in order to support multi-unit devices.
768	 * The FIFO is located out of the local host controller's physical range
769	 * but, if possible, within the posted write area. Status writes will
770	 * then be performed as unified transactions. This slightly reduces
771	 * bandwidth usage, and some Prolific based devices seem to require it.
772	 */
773	lu->status_fifo_addr = hpsb_allocate_and_register_addrspace(
774			&sbp2_highlevel, ud->ne->host, &sbp2_ops,
775			sizeof(struct sbp2_status_block), sizeof(quadlet_t),
776			ud->ne->host->low_addr_space, CSR1212_ALL_SPACE_END);
777	if (lu->status_fifo_addr == CSR1212_INVALID_ADDR_SPACE) {
778		SBP2_ERR("failed to allocate status FIFO address range");
779		goto failed_alloc;
780	}
781
782	shost = scsi_host_alloc(&sbp2_shost_template, sizeof(unsigned long));
783	if (!shost) {
784		SBP2_ERR("failed to register scsi host");
785		goto failed_alloc;
786	}
787
788	shost->hostdata[0] = (unsigned long)lu;
789
790	if (!scsi_add_host(shost, &ud->device)) {
791		lu->shost = shost;
792		return lu;
793	}
794
795	SBP2_ERR("failed to add scsi host");
796	scsi_host_put(shost);
797
798failed_alloc:
799	sbp2_remove_device(lu);
800	return NULL;
801}
802
803static void sbp2_host_reset(struct hpsb_host *host)
804{
805	struct sbp2_fwhost_info *hi;
806	struct sbp2_lu *lu;
807
808	hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
809	if (!hi)
810		return;
811	list_for_each_entry(lu, &hi->logical_units, lu_list)
812		if (likely(atomic_read(&lu->state) !=
813			   SBP2LU_STATE_IN_SHUTDOWN)) {
814			atomic_set(&lu->state, SBP2LU_STATE_IN_RESET);
815			scsi_block_requests(lu->shost);
816		}
817}
818
819static int sbp2_start_device(struct sbp2_lu *lu)
820{
821	struct sbp2_fwhost_info *hi = lu->hi;
822	int error;
823
824	lu->login_response = dma_alloc_coherent(hi->host->device.parent,
825				     sizeof(struct sbp2_login_response),
826				     &lu->login_response_dma, GFP_KERNEL);
827	if (!lu->login_response)
828		goto alloc_fail;
829
830	lu->query_logins_orb = dma_alloc_coherent(hi->host->device.parent,
831				     sizeof(struct sbp2_query_logins_orb),
832				     &lu->query_logins_orb_dma, GFP_KERNEL);
833	if (!lu->query_logins_orb)
834		goto alloc_fail;
835
836	lu->query_logins_response = dma_alloc_coherent(hi->host->device.parent,
837				     sizeof(struct sbp2_query_logins_response),
838				     &lu->query_logins_response_dma, GFP_KERNEL);
839	if (!lu->query_logins_response)
840		goto alloc_fail;
841
842	lu->reconnect_orb = dma_alloc_coherent(hi->host->device.parent,
843				     sizeof(struct sbp2_reconnect_orb),
844				     &lu->reconnect_orb_dma, GFP_KERNEL);
845	if (!lu->reconnect_orb)
846		goto alloc_fail;
847
848	lu->logout_orb = dma_alloc_coherent(hi->host->device.parent,
849				     sizeof(struct sbp2_logout_orb),
850				     &lu->logout_orb_dma, GFP_KERNEL);
851	if (!lu->logout_orb)
852		goto alloc_fail;
853
854	lu->login_orb = dma_alloc_coherent(hi->host->device.parent,
855				     sizeof(struct sbp2_login_orb),
856				     &lu->login_orb_dma, GFP_KERNEL);
857	if (!lu->login_orb)
858		goto alloc_fail;
859
860	if (sbp2util_create_command_orb_pool(lu))
861		goto alloc_fail;
862
863	/* Wait a second before trying to log in. Previously logged in
864	 * initiators need a chance to reconnect. */
865	if (msleep_interruptible(1000)) {
866		sbp2_remove_device(lu);
867		return -EINTR;
868	}
869
870	if (sbp2_login_device(lu)) {
871		sbp2_remove_device(lu);
872		return -EBUSY;
873	}
874
875	sbp2_set_busy_timeout(lu);
876	sbp2_agent_reset(lu, 1);
877	sbp2_max_speed_and_size(lu);
878
879	error = scsi_add_device(lu->shost, 0, lu->ud->id, 0);
880	if (error) {
881		SBP2_ERR("scsi_add_device failed");
882		sbp2_logout_device(lu);
883		sbp2_remove_device(lu);
884		return error;
885	}
886
887	return 0;
888
889alloc_fail:
890	SBP2_ERR("Could not allocate memory for lu");
891	sbp2_remove_device(lu);
892	return -ENOMEM;
893}
894
895static void sbp2_remove_device(struct sbp2_lu *lu)
896{
897	struct sbp2_fwhost_info *hi;
898
899	if (!lu)
900		return;
901
902	hi = lu->hi;
903
904	if (lu->shost) {
905		scsi_remove_host(lu->shost);
906		scsi_host_put(lu->shost);
907	}
908	flush_scheduled_work();
909	sbp2util_remove_command_orb_pool(lu);
910
911	list_del(&lu->lu_list);
912
913	if (lu->login_response)
914		dma_free_coherent(hi->host->device.parent,
915				    sizeof(struct sbp2_login_response),
916				    lu->login_response,
917				    lu->login_response_dma);
918	if (lu->login_orb)
919		dma_free_coherent(hi->host->device.parent,
920				    sizeof(struct sbp2_login_orb),
921				    lu->login_orb,
922				    lu->login_orb_dma);
923	if (lu->reconnect_orb)
924		dma_free_coherent(hi->host->device.parent,
925				    sizeof(struct sbp2_reconnect_orb),
926				    lu->reconnect_orb,
927				    lu->reconnect_orb_dma);
928	if (lu->logout_orb)
929		dma_free_coherent(hi->host->device.parent,
930				    sizeof(struct sbp2_logout_orb),
931				    lu->logout_orb,
932				    lu->logout_orb_dma);
933	if (lu->query_logins_orb)
934		dma_free_coherent(hi->host->device.parent,
935				    sizeof(struct sbp2_query_logins_orb),
936				    lu->query_logins_orb,
937				    lu->query_logins_orb_dma);
938	if (lu->query_logins_response)
939		dma_free_coherent(hi->host->device.parent,
940				    sizeof(struct sbp2_query_logins_response),
941				    lu->query_logins_response,
942				    lu->query_logins_response_dma);
943
944	if (lu->status_fifo_addr != CSR1212_INVALID_ADDR_SPACE)
945		hpsb_unregister_addrspace(&sbp2_highlevel, hi->host,
946					  lu->status_fifo_addr);
947
948	lu->ud->device.driver_data = NULL;
949
950	if (hi)
951		module_put(hi->host->driver->owner);
952
953	kfree(lu);
954}
955
956#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
957/*
958 * Deal with write requests on adapters which do not support physical DMA or
959 * have it switched off.
960 */
961static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid,
962				     int destid, quadlet_t *data, u64 addr,
963				     size_t length, u16 flags)
964{
965	memcpy(bus_to_virt((u32) addr), data, length);
966	return RCODE_COMPLETE;
967}
968
969/*
970 * Deal with read requests on adapters which do not support physical DMA or
971 * have it switched off.
972 */
973static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid,
974				    quadlet_t *data, u64 addr, size_t length,
975				    u16 flags)
976{
977	memcpy(data, bus_to_virt((u32) addr), length);
978	return RCODE_COMPLETE;
979}
980#endif
981
982/**************************************
983 * SBP-2 protocol related section
984 **************************************/
985
986static int sbp2_query_logins(struct sbp2_lu *lu)
987{
988	struct sbp2_fwhost_info *hi = lu->hi;
989	quadlet_t data[2];
990	int max_logins;
991	int active_logins;
992
993	lu->query_logins_orb->reserved1 = 0x0;
994	lu->query_logins_orb->reserved2 = 0x0;
995
996	lu->query_logins_orb->query_response_lo = lu->query_logins_response_dma;
997	lu->query_logins_orb->query_response_hi =
998			ORB_SET_NODE_ID(hi->host->node_id);
999	lu->query_logins_orb->lun_misc =
1000			ORB_SET_FUNCTION(SBP2_QUERY_LOGINS_REQUEST);
1001	lu->query_logins_orb->lun_misc |= ORB_SET_NOTIFY(1);
1002	lu->query_logins_orb->lun_misc |= ORB_SET_LUN(lu->lun);
1003
1004	lu->query_logins_orb->reserved_resp_length =
1005		ORB_SET_QUERY_LOGINS_RESP_LENGTH(
1006			sizeof(struct sbp2_query_logins_response));
1007
1008	lu->query_logins_orb->status_fifo_hi =
1009		ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1010	lu->query_logins_orb->status_fifo_lo =
1011		ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1012
1013	sbp2util_cpu_to_be32_buffer(lu->query_logins_orb,
1014				    sizeof(struct sbp2_query_logins_orb));
1015
1016	memset(lu->query_logins_response, 0,
1017	       sizeof(struct sbp2_query_logins_response));
1018
1019	data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1020	data[1] = lu->query_logins_orb_dma;
1021	sbp2util_cpu_to_be32_buffer(data, 8);
1022
1023	hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1024
1025	if (sbp2util_access_timeout(lu, 2*HZ)) {
1026		SBP2_INFO("Error querying logins to SBP-2 device - timed out");
1027		return -EIO;
1028	}
1029
1030	if (lu->status_block.ORB_offset_lo != lu->query_logins_orb_dma) {
1031		SBP2_INFO("Error querying logins to SBP-2 device - timed out");
1032		return -EIO;
1033	}
1034
1035	if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
1036		SBP2_INFO("Error querying logins to SBP-2 device - failed");
1037		return -EIO;
1038	}
1039
1040	sbp2util_cpu_to_be32_buffer(lu->query_logins_response,
1041				    sizeof(struct sbp2_query_logins_response));
1042
1043	max_logins = RESPONSE_GET_MAX_LOGINS(
1044			lu->query_logins_response->length_max_logins);
1045	SBP2_INFO("Maximum concurrent logins supported: %d", max_logins);
1046
1047	active_logins = RESPONSE_GET_ACTIVE_LOGINS(
1048			lu->query_logins_response->length_max_logins);
1049	SBP2_INFO("Number of active logins: %d", active_logins);
1050
1051	if (active_logins >= max_logins) {
1052		return -EIO;
1053	}
1054
1055	return 0;
1056}
1057
1058static int sbp2_login_device(struct sbp2_lu *lu)
1059{
1060	struct sbp2_fwhost_info *hi = lu->hi;
1061	quadlet_t data[2];
1062
1063	if (!lu->login_orb)
1064		return -EIO;
1065
1066	if (!sbp2_exclusive_login && sbp2_query_logins(lu)) {
1067		SBP2_INFO("Device does not support any more concurrent logins");
1068		return -EIO;
1069	}
1070
1071	/* assume no password */
1072	lu->login_orb->password_hi = 0;
1073	lu->login_orb->password_lo = 0;
1074
1075	lu->login_orb->login_response_lo = lu->login_response_dma;
1076	lu->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1077	lu->login_orb->lun_misc = ORB_SET_FUNCTION(SBP2_LOGIN_REQUEST);
1078
1079	/* one second reconnect time */
1080	lu->login_orb->lun_misc |= ORB_SET_RECONNECT(0);
1081	lu->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(sbp2_exclusive_login);
1082	lu->login_orb->lun_misc |= ORB_SET_NOTIFY(1);
1083	lu->login_orb->lun_misc |= ORB_SET_LUN(lu->lun);
1084
1085	lu->login_orb->passwd_resp_lengths =
1086		ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
1087
1088	lu->login_orb->status_fifo_hi =
1089		ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1090	lu->login_orb->status_fifo_lo =
1091		ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1092
1093	sbp2util_cpu_to_be32_buffer(lu->login_orb,
1094				    sizeof(struct sbp2_login_orb));
1095
1096	memset(lu->login_response, 0, sizeof(struct sbp2_login_response));
1097
1098	data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1099	data[1] = lu->login_orb_dma;
1100	sbp2util_cpu_to_be32_buffer(data, 8);
1101
1102	hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1103
1104	/* wait up to 20 seconds for login status */
1105	if (sbp2util_access_timeout(lu, 20*HZ)) {
1106		SBP2_ERR("Error logging into SBP-2 device - timed out");
1107		return -EIO;
1108	}
1109
1110	/* make sure that the returned status matches the login ORB */
1111	if (lu->status_block.ORB_offset_lo != lu->login_orb_dma) {
1112		SBP2_ERR("Error logging into SBP-2 device - timed out");
1113		return -EIO;
1114	}
1115
1116	if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
1117		SBP2_ERR("Error logging into SBP-2 device - failed");
1118		return -EIO;
1119	}
1120
1121	sbp2util_cpu_to_be32_buffer(lu->login_response,
1122				    sizeof(struct sbp2_login_response));
1123	lu->command_block_agent_addr =
1124			((u64)lu->login_response->command_block_agent_hi) << 32;
1125	lu->command_block_agent_addr |=
1126			((u64)lu->login_response->command_block_agent_lo);
1127	lu->command_block_agent_addr &= 0x0000ffffffffffffULL;
1128
1129	SBP2_INFO("Logged into SBP-2 device");
1130	return 0;
1131}
1132
1133static int sbp2_logout_device(struct sbp2_lu *lu)
1134{
1135	struct sbp2_fwhost_info *hi = lu->hi;
1136	quadlet_t data[2];
1137	int error;
1138
1139	lu->logout_orb->reserved1 = 0x0;
1140	lu->logout_orb->reserved2 = 0x0;
1141	lu->logout_orb->reserved3 = 0x0;
1142	lu->logout_orb->reserved4 = 0x0;
1143
1144	lu->logout_orb->login_ID_misc = ORB_SET_FUNCTION(SBP2_LOGOUT_REQUEST);
1145	lu->logout_orb->login_ID_misc |=
1146			ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
1147	lu->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1148
1149	lu->logout_orb->reserved5 = 0x0;
1150	lu->logout_orb->status_fifo_hi =
1151		ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1152	lu->logout_orb->status_fifo_lo =
1153		ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1154
1155	sbp2util_cpu_to_be32_buffer(lu->logout_orb,
1156				    sizeof(struct sbp2_logout_orb));
1157
1158	data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1159	data[1] = lu->logout_orb_dma;
1160	sbp2util_cpu_to_be32_buffer(data, 8);
1161
1162	error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1163	if (error)
1164		return error;
1165
1166	/* wait up to 1 second for the device to complete logout */
1167	if (sbp2util_access_timeout(lu, HZ))
1168		return -EIO;
1169
1170	SBP2_INFO("Logged out of SBP-2 device");
1171	return 0;
1172}
1173
1174static int sbp2_reconnect_device(struct sbp2_lu *lu)
1175{
1176	struct sbp2_fwhost_info *hi = lu->hi;
1177	quadlet_t data[2];
1178	int error;
1179
1180	lu->reconnect_orb->reserved1 = 0x0;
1181	lu->reconnect_orb->reserved2 = 0x0;
1182	lu->reconnect_orb->reserved3 = 0x0;
1183	lu->reconnect_orb->reserved4 = 0x0;
1184
1185	lu->reconnect_orb->login_ID_misc =
1186			ORB_SET_FUNCTION(SBP2_RECONNECT_REQUEST);
1187	lu->reconnect_orb->login_ID_misc |=
1188			ORB_SET_LOGIN_ID(lu->login_response->length_login_ID);
1189	lu->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1190
1191	lu->reconnect_orb->reserved5 = 0x0;
1192	lu->reconnect_orb->status_fifo_hi =
1193		ORB_SET_STATUS_FIFO_HI(lu->status_fifo_addr, hi->host->node_id);
1194	lu->reconnect_orb->status_fifo_lo =
1195		ORB_SET_STATUS_FIFO_LO(lu->status_fifo_addr);
1196
1197	sbp2util_cpu_to_be32_buffer(lu->reconnect_orb,
1198				    sizeof(struct sbp2_reconnect_orb));
1199
1200	data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1201	data[1] = lu->reconnect_orb_dma;
1202	sbp2util_cpu_to_be32_buffer(data, 8);
1203
1204	error = hpsb_node_write(lu->ne, lu->management_agent_addr, data, 8);
1205	if (error)
1206		return error;
1207
1208	/* wait up to 1 second for reconnect status */
1209	if (sbp2util_access_timeout(lu, HZ)) {
1210		SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
1211		return -EIO;
1212	}
1213
1214	/* make sure that the returned status matches the reconnect ORB */
1215	if (lu->status_block.ORB_offset_lo != lu->reconnect_orb_dma) {
1216		SBP2_ERR("Error reconnecting to SBP-2 device - timed out");
1217		return -EIO;
1218	}
1219
1220	if (STATUS_TEST_RDS(lu->status_block.ORB_offset_hi_misc)) {
1221		SBP2_ERR("Error reconnecting to SBP-2 device - failed");
1222		return -EIO;
1223	}
1224
1225	SBP2_INFO("Reconnected to SBP-2 device");
1226	return 0;
1227}
1228
1229/*
1230 * Set the target node's Single Phase Retry limit. Affects the target's retry
1231 * behaviour if our node is too busy to accept requests.
1232 */
1233static int sbp2_set_busy_timeout(struct sbp2_lu *lu)
1234{
1235	quadlet_t data;
1236
1237	data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
1238	if (hpsb_node_write(lu->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4))
1239		SBP2_ERR("%s error", __FUNCTION__);
1240	return 0;
1241}
1242
1243static void sbp2_parse_unit_directory(struct sbp2_lu *lu,
1244				      struct unit_directory *ud)
1245{
1246	struct csr1212_keyval *kv;
1247	struct csr1212_dentry *dentry;
1248	u64 management_agent_addr;
1249	u32 unit_characteristics, firmware_revision;
1250	unsigned workarounds;
1251	int i;
1252
1253	management_agent_addr = 0;
1254	unit_characteristics = 0;
1255	firmware_revision = 0;
1256
1257	csr1212_for_each_dir_entry(ud->ne->csr, kv, ud->ud_kv, dentry) {
1258		switch (kv->key.id) {
1259		case CSR1212_KV_ID_DEPENDENT_INFO:
1260			if (kv->key.type == CSR1212_KV_TYPE_CSR_OFFSET)
1261				management_agent_addr =
1262				    CSR1212_REGISTER_SPACE_BASE +
1263				    (kv->value.csr_offset << 2);
1264
1265			else if (kv->key.type == CSR1212_KV_TYPE_IMMEDIATE)
1266				lu->lun = ORB_SET_LUN(kv->value.immediate);
1267			break;
1268
1269		case SBP2_UNIT_CHARACTERISTICS_KEY:
1270			unit_characteristics = kv->value.immediate;
1271			break;
1272
1273		case SBP2_FIRMWARE_REVISION_KEY:
1274			firmware_revision = kv->value.immediate;
1275			break;
1276
1277		default:
1278			break;
1279		}
1280	}
1281
1282	workarounds = sbp2_default_workarounds;
1283
1284	if (!(workarounds & SBP2_WORKAROUND_OVERRIDE))
1285		for (i = 0; i < ARRAY_SIZE(sbp2_workarounds_table); i++) {
1286			if (sbp2_workarounds_table[i].firmware_revision !=
1287			    SBP2_ROM_VALUE_WILDCARD &&
1288			    sbp2_workarounds_table[i].firmware_revision !=
1289			    (firmware_revision & 0xffff00))
1290				continue;
1291			if (sbp2_workarounds_table[i].model_id !=
1292			    SBP2_ROM_VALUE_WILDCARD &&
1293			    sbp2_workarounds_table[i].model_id != ud->model_id)
1294				continue;
1295			workarounds |= sbp2_workarounds_table[i].workarounds;
1296			break;
1297		}
1298
1299	if (workarounds)
1300		SBP2_INFO("Workarounds for node " NODE_BUS_FMT ": 0x%x "
1301			  "(firmware_revision 0x%06x, vendor_id 0x%06x,"
1302			  " model_id 0x%06x)",
1303			  NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
1304			  workarounds, firmware_revision,
1305			  ud->vendor_id ? ud->vendor_id : ud->ne->vendor_id,
1306			  ud->model_id);
1307
1308	/* We would need one SCSI host template for each target to adjust
1309	 * max_sectors on the fly, therefore warn only. */
1310	if (workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
1311	    (sbp2_max_sectors * 512) > (128 * 1024))
1312		SBP2_INFO("Node " NODE_BUS_FMT ": Bridge only supports 128KB "
1313			  "max transfer size. WARNING: Current max_sectors "
1314			  "setting is larger than 128KB (%d sectors)",
1315			  NODE_BUS_ARGS(ud->ne->host, ud->ne->nodeid),
1316			  sbp2_max_sectors);
1317
1318	/* If this is a logical unit directory entry, process the parent
1319	 * to get the values. */
1320	if (ud->flags & UNIT_DIRECTORY_LUN_DIRECTORY) {
1321		struct unit_directory *parent_ud = container_of(
1322			ud->device.parent, struct unit_directory, device);
1323		sbp2_parse_unit_directory(lu, parent_ud);
1324	} else {
1325		lu->management_agent_addr = management_agent_addr;
1326		lu->workarounds = workarounds;
1327		if (ud->flags & UNIT_DIRECTORY_HAS_LUN)
1328			lu->lun = ORB_SET_LUN(ud->lun);
1329	}
1330}
1331
1332#define SBP2_PAYLOAD_TO_BYTES(p) (1 << ((p) + 2))
1333
1334/*
1335 * This function is called in order to determine the max speed and packet
1336 * size we can use in our ORBs. Note, that we (the driver and host) only
1337 * initiate the transaction. The SBP-2 device actually transfers the data
1338 * (by reading from the DMA area we tell it). This means that the SBP-2
1339 * device decides the actual maximum data it can transfer. We just tell it
1340 * the speed that it needs to use, and the max_rec the host supports, and
1341 * it takes care of the rest.
1342 */
1343static int sbp2_max_speed_and_size(struct sbp2_lu *lu)
1344{
1345	struct sbp2_fwhost_info *hi = lu->hi;
1346	u8 payload;
1347
1348	lu->speed_code = hi->host->speed[NODEID_TO_NODE(lu->ne->nodeid)];
1349
1350	if (lu->speed_code > sbp2_max_speed) {
1351		lu->speed_code = sbp2_max_speed;
1352		SBP2_INFO("Reducing speed to %s",
1353			  hpsb_speedto_str[sbp2_max_speed]);
1354	}
1355
1356	/* Payload size is the lesser of what our speed supports and what
1357	 * our host supports.  */
1358	payload = min(sbp2_speedto_max_payload[lu->speed_code],
1359		      (u8) (hi->host->csr.max_rec - 1));
1360
1361	if (lu->ne->host->low_addr_space < (1ULL << 32))
1362		while (SBP2_PAYLOAD_TO_BYTES(payload) + 24 > PAGE_SIZE &&
1363		       payload)
1364			payload--;
1365
1366	SBP2_INFO("Node " NODE_BUS_FMT ": Max speed [%s] - Max payload [%u]",
1367		  NODE_BUS_ARGS(hi->host, lu->ne->nodeid),
1368		  hpsb_speedto_str[lu->speed_code],
1369		  SBP2_PAYLOAD_TO_BYTES(payload));
1370
1371	lu->max_payload_size = payload;
1372	return 0;
1373}
1374
1375static int sbp2_agent_reset(struct sbp2_lu *lu, int wait)
1376{
1377	quadlet_t data;
1378	u64 addr;
1379	int retval;
1380	unsigned long flags;
1381
1382	/* flush lu->protocol_work */
1383	if (wait)
1384		flush_scheduled_work();
1385
1386	data = ntohl(SBP2_AGENT_RESET_DATA);
1387	addr = lu->command_block_agent_addr + SBP2_AGENT_RESET_OFFSET;
1388
1389	if (wait)
1390		retval = hpsb_node_write(lu->ne, addr, &data, 4);
1391	else
1392		retval = sbp2util_node_write_no_wait(lu->ne, addr, &data, 4);
1393
1394	if (retval < 0) {
1395		SBP2_ERR("hpsb_node_write failed.\n");
1396		return -EIO;
1397	}
1398
1399	/* make sure that the ORB_POINTER is written on next command */
1400	spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1401	lu->last_orb = NULL;
1402	spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1403
1404	return 0;
1405}
1406
1407static void sbp2_prep_command_orb_sg(struct sbp2_command_orb *orb,
1408				     struct sbp2_fwhost_info *hi,
1409				     struct sbp2_command_info *cmd,
1410				     unsigned int scsi_use_sg,
1411				     struct scatterlist *sgpnt,
1412				     u32 orb_direction,
1413				     enum dma_data_direction dma_dir)
1414{
1415	cmd->dma_dir = dma_dir;
1416	orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1417	orb->misc |= ORB_SET_DIRECTION(orb_direction);
1418
1419	/* special case if only one element (and less than 64KB in size) */
1420	if ((scsi_use_sg == 1) &&
1421	    (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
1422
1423		cmd->dma_size = sgpnt[0].length;
1424		cmd->dma_type = CMD_DMA_PAGE;
1425		cmd->cmd_dma = dma_map_page(hi->host->device.parent,
1426					    sgpnt[0].page, sgpnt[0].offset,
1427					    cmd->dma_size, cmd->dma_dir);
1428
1429		orb->data_descriptor_lo = cmd->cmd_dma;
1430		orb->misc |= ORB_SET_DATA_SIZE(cmd->dma_size);
1431
1432	} else {
1433		struct sbp2_unrestricted_page_table *sg_element =
1434						&cmd->scatter_gather_element[0];
1435		u32 sg_count, sg_len;
1436		dma_addr_t sg_addr;
1437		int i, count = dma_map_sg(hi->host->device.parent, sgpnt,
1438					  scsi_use_sg, dma_dir);
1439
1440		cmd->dma_size = scsi_use_sg;
1441		cmd->sge_buffer = sgpnt;
1442
1443		/* use page tables (s/g) */
1444		orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1445		orb->data_descriptor_lo = cmd->sge_dma;
1446
1447		/* loop through and fill out our SBP-2 page tables
1448		 * (and split up anything too large) */
1449		for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
1450			sg_len = sg_dma_len(sgpnt);
1451			sg_addr = sg_dma_address(sgpnt);
1452			while (sg_len) {
1453				sg_element[sg_count].segment_base_lo = sg_addr;
1454				if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1455					sg_element[sg_count].length_segment_base_hi =
1456						PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1457					sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1458					sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1459				} else {
1460					sg_element[sg_count].length_segment_base_hi =
1461						PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1462					sg_len = 0;
1463				}
1464				sg_count++;
1465			}
1466		}
1467
1468		orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1469
1470		sbp2util_cpu_to_be32_buffer(sg_element,
1471				(sizeof(struct sbp2_unrestricted_page_table)) *
1472				sg_count);
1473	}
1474}
1475
1476static void sbp2_prep_command_orb_no_sg(struct sbp2_command_orb *orb,
1477					struct sbp2_fwhost_info *hi,
1478					struct sbp2_command_info *cmd,
1479					struct scatterlist *sgpnt,
1480					u32 orb_direction,
1481					unsigned int scsi_request_bufflen,
1482					void *scsi_request_buffer,
1483					enum dma_data_direction dma_dir)
1484{
1485	cmd->dma_dir = dma_dir;
1486	cmd->dma_size = scsi_request_bufflen;
1487	cmd->dma_type = CMD_DMA_SINGLE;
1488	cmd->cmd_dma = dma_map_single(hi->host->device.parent,
1489				      scsi_request_buffer,
1490				      cmd->dma_size, cmd->dma_dir);
1491	orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1492	orb->misc |= ORB_SET_DIRECTION(orb_direction);
1493
1494	/* handle case where we get a command w/o s/g enabled
1495	 * (but check for transfers larger than 64K) */
1496	if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1497
1498		orb->data_descriptor_lo = cmd->cmd_dma;
1499		orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
1500
1501	} else {
1502		/* The buffer is too large. Turn this into page tables. */
1503
1504		struct sbp2_unrestricted_page_table *sg_element =
1505						&cmd->scatter_gather_element[0];
1506		u32 sg_count, sg_len;
1507		dma_addr_t sg_addr;
1508
1509		orb->data_descriptor_lo = cmd->sge_dma;
1510		orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1511
1512		/* fill out our SBP-2 page tables; split up the large buffer */
1513		sg_count = 0;
1514		sg_len = scsi_request_bufflen;
1515		sg_addr = cmd->cmd_dma;
1516		while (sg_len) {
1517			sg_element[sg_count].segment_base_lo = sg_addr;
1518			if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1519				sg_element[sg_count].length_segment_base_hi =
1520					PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1521				sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1522				sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1523			} else {
1524				sg_element[sg_count].length_segment_base_hi =
1525					PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1526				sg_len = 0;
1527			}
1528			sg_count++;
1529		}
1530
1531		orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1532
1533		sbp2util_cpu_to_be32_buffer(sg_element,
1534				(sizeof(struct sbp2_unrestricted_page_table)) *
1535				sg_count);
1536	}
1537}
1538
1539static void sbp2_create_command_orb(struct sbp2_lu *lu,
1540				    struct sbp2_command_info *cmd,
1541				    unchar *scsi_cmd,
1542				    unsigned int scsi_use_sg,
1543				    unsigned int scsi_request_bufflen,
1544				    void *scsi_request_buffer,
1545				    enum dma_data_direction dma_dir)
1546{
1547	struct sbp2_fwhost_info *hi = lu->hi;
1548	struct scatterlist *sgpnt = (struct scatterlist *)scsi_request_buffer;
1549	struct sbp2_command_orb *orb = &cmd->command_orb;
1550	u32 orb_direction;
1551
1552	/*
1553	 * Set-up our command ORB.
1554	 *
1555	 * NOTE: We're doing unrestricted page tables (s/g), as this is
1556	 * best performance (at least with the devices I have). This means
1557	 * that data_size becomes the number of s/g elements, and
1558	 * page_size should be zero (for unrestricted).
1559	 */
1560	orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1561	orb->next_ORB_lo = 0x0;
1562	orb->misc = ORB_SET_MAX_PAYLOAD(lu->max_payload_size);
1563	orb->misc |= ORB_SET_SPEED(lu->speed_code);
1564	orb->misc |= ORB_SET_NOTIFY(1);
1565
1566	if (dma_dir == DMA_NONE)
1567		orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1568	else if (dma_dir == DMA_TO_DEVICE && scsi_request_bufflen)
1569		orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
1570	else if (dma_dir == DMA_FROM_DEVICE && scsi_request_bufflen)
1571		orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
1572	else {
1573		SBP2_INFO("Falling back to DMA_NONE");
1574		orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1575	}
1576
1577	/* set up our page table stuff */
1578	if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
1579		orb->data_descriptor_hi = 0x0;
1580		orb->data_descriptor_lo = 0x0;
1581		orb->misc |= ORB_SET_DIRECTION(1);
1582	} else if (scsi_use_sg)
1583		sbp2_prep_command_orb_sg(orb, hi, cmd, scsi_use_sg, sgpnt,
1584					 orb_direction, dma_dir);
1585	else
1586		sbp2_prep_command_orb_no_sg(orb, hi, cmd, sgpnt, orb_direction,
1587					    scsi_request_bufflen,
1588					    scsi_request_buffer, dma_dir);
1589
1590	sbp2util_cpu_to_be32_buffer(orb, sizeof(*orb));
1591
1592	memset(orb->cdb, 0, 12);
1593	memcpy(orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
1594}
1595
1596static void sbp2_link_orb_command(struct sbp2_lu *lu,
1597				  struct sbp2_command_info *cmd)
1598{
1599	struct sbp2_fwhost_info *hi = lu->hi;
1600	struct sbp2_command_orb *last_orb;
1601	dma_addr_t last_orb_dma;
1602	u64 addr = lu->command_block_agent_addr;
1603	quadlet_t data[2];
1604	size_t length;
1605	unsigned long flags;
1606
1607	dma_sync_single_for_device(hi->host->device.parent,
1608				   cmd->command_orb_dma,
1609				   sizeof(struct sbp2_command_orb),
1610				   DMA_TO_DEVICE);
1611	dma_sync_single_for_device(hi->host->device.parent, cmd->sge_dma,
1612				   sizeof(cmd->scatter_gather_element),
1613				   DMA_TO_DEVICE);
1614
1615	/* check to see if there are any previous orbs to use */
1616	spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1617	last_orb = lu->last_orb;
1618	last_orb_dma = lu->last_orb_dma;
1619	if (!last_orb) {
1620		/*
1621		 * last_orb == NULL means: We know that the target's fetch agent
1622		 * is not active right now.
1623		 */
1624		addr += SBP2_ORB_POINTER_OFFSET;
1625		data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1626		data[1] = cmd->command_orb_dma;
1627		sbp2util_cpu_to_be32_buffer(data, 8);
1628		length = 8;
1629	} else {
1630		/*
1631		 * last_orb != NULL means: We know that the target's fetch agent
1632		 * is (very probably) not dead or in reset state right now.
1633		 * We have an ORB already sent that we can append a new one to.
1634		 * The target's fetch agent may or may not have read this
1635		 * previous ORB yet.
1636		 */
1637		dma_sync_single_for_cpu(hi->host->device.parent, last_orb_dma,
1638					sizeof(struct sbp2_command_orb),
1639					DMA_TO_DEVICE);
1640		last_orb->next_ORB_lo = cpu_to_be32(cmd->command_orb_dma);
1641		wmb();
1642		/* Tells hardware that this pointer is valid */
1643		last_orb->next_ORB_hi = 0;
1644		dma_sync_single_for_device(hi->host->device.parent,
1645					   last_orb_dma,
1646					   sizeof(struct sbp2_command_orb),
1647					   DMA_TO_DEVICE);
1648		addr += SBP2_DOORBELL_OFFSET;
1649		data[0] = 0;
1650		length = 4;
1651	}
1652	lu->last_orb = &cmd->command_orb;
1653	lu->last_orb_dma = cmd->command_orb_dma;
1654	spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1655
1656	if (sbp2util_node_write_no_wait(lu->ne, addr, data, length)) {
1657		/*
1658		 * sbp2util_node_write_no_wait failed. We certainly ran out
1659		 * of transaction labels, perhaps just because there were no
1660		 * context switches which gave khpsbpkt a chance to collect
1661		 * free tlabels. Try again in non-atomic context. If necessary,
1662		 * the workqueue job will sleep to guaranteedly get a tlabel.
1663		 * We do not accept new commands until the job is over.
1664		 */
1665		scsi_block_requests(lu->shost);
1666		PREPARE_WORK(&lu->protocol_work,
1667			     last_orb ? sbp2util_write_doorbell:
1668					sbp2util_write_orb_pointer);
1669		schedule_work(&lu->protocol_work);
1670	}
1671}
1672
1673static int sbp2_send_command(struct sbp2_lu *lu, struct scsi_cmnd *SCpnt,
1674			     void (*done)(struct scsi_cmnd *))
1675{
1676	unchar *scsi_cmd = (unchar *)SCpnt->cmnd;
1677	unsigned int request_bufflen = SCpnt->request_bufflen;
1678	struct sbp2_command_info *cmd;
1679
1680	cmd = sbp2util_allocate_command_orb(lu, SCpnt, done);
1681	if (!cmd)
1682		return -EIO;
1683
1684	sbp2_create_command_orb(lu, cmd, scsi_cmd, SCpnt->use_sg,
1685				request_bufflen, SCpnt->request_buffer,
1686				SCpnt->sc_data_direction);
1687	sbp2_link_orb_command(lu, cmd);
1688
1689	return 0;
1690}
1691
1692/*
1693 * Translates SBP-2 status into SCSI sense data for check conditions
1694 */
1695static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status,
1696					      unchar *sense_data)
1697{
1698	/* OK, it's pretty ugly... ;-) */
1699	sense_data[0] = 0x70;
1700	sense_data[1] = 0x0;
1701	sense_data[2] = sbp2_status[9];
1702	sense_data[3] = sbp2_status[12];
1703	sense_data[4] = sbp2_status[13];
1704	sense_data[5] = sbp2_status[14];
1705	sense_data[6] = sbp2_status[15];
1706	sense_data[7] = 10;
1707	sense_data[8] = sbp2_status[16];
1708	sense_data[9] = sbp2_status[17];
1709	sense_data[10] = sbp2_status[18];
1710	sense_data[11] = sbp2_status[19];
1711	sense_data[12] = sbp2_status[10];
1712	sense_data[13] = sbp2_status[11];
1713	sense_data[14] = sbp2_status[20];
1714	sense_data[15] = sbp2_status[21];
1715
1716	return sbp2_status[8] & 0x3f;
1717}
1718
1719static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid,
1720				    int destid, quadlet_t *data, u64 addr,
1721				    size_t length, u16 fl)
1722{
1723	struct sbp2_fwhost_info *hi;
1724	struct sbp2_lu *lu = NULL, *lu_tmp;
1725	struct scsi_cmnd *SCpnt = NULL;
1726	struct sbp2_status_block *sb;
1727	u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
1728	struct sbp2_command_info *cmd;
1729	unsigned long flags;
1730
1731	if (unlikely(length < 8 || length > sizeof(struct sbp2_status_block))) {
1732		SBP2_ERR("Wrong size of status block");
1733		return RCODE_ADDRESS_ERROR;
1734	}
1735	if (unlikely(!host)) {
1736		SBP2_ERR("host is NULL - this is bad!");
1737		return RCODE_ADDRESS_ERROR;
1738	}
1739	hi = hpsb_get_hostinfo(&sbp2_highlevel, host);
1740	if (unlikely(!hi)) {
1741		SBP2_ERR("host info is NULL - this is bad!");
1742		return RCODE_ADDRESS_ERROR;
1743	}
1744
1745	/* Find the unit which wrote the status. */
1746	list_for_each_entry(lu_tmp, &hi->logical_units, lu_list) {
1747		if (lu_tmp->ne->nodeid == nodeid &&
1748		    lu_tmp->status_fifo_addr == addr) {
1749			lu = lu_tmp;
1750			break;
1751		}
1752	}
1753	if (unlikely(!lu)) {
1754		SBP2_ERR("lu is NULL - device is gone?");
1755		return RCODE_ADDRESS_ERROR;
1756	}
1757
1758	/* Put response into lu status fifo buffer. The first two bytes
1759	 * come in big endian bit order. Often the target writes only a
1760	 * truncated status block, minimally the first two quadlets. The rest
1761	 * is implied to be zeros. */
1762	sb = &lu->status_block;
1763	memset(sb->command_set_dependent, 0, sizeof(sb->command_set_dependent));
1764	memcpy(sb, data, length);
1765	sbp2util_be32_to_cpu_buffer(sb, 8);
1766
1767	/* Ignore unsolicited status. Handle command ORB status. */
1768	if (unlikely(STATUS_GET_SRC(sb->ORB_offset_hi_misc) == 2))
1769		cmd = NULL;
1770	else
1771		cmd = sbp2util_find_command_for_orb(lu, sb->ORB_offset_lo);
1772	if (cmd) {
1773		dma_sync_single_for_cpu(hi->host->device.parent,
1774					cmd->command_orb_dma,
1775					sizeof(struct sbp2_command_orb),
1776					DMA_TO_DEVICE);
1777		dma_sync_single_for_cpu(hi->host->device.parent, cmd->sge_dma,
1778					sizeof(cmd->scatter_gather_element),
1779					DMA_TO_DEVICE);
1780		/* Grab SCSI command pointers and check status. */
1781		SCpnt = cmd->Current_SCpnt;
1782		spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1783		sbp2util_mark_command_completed(lu, cmd);
1784		spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1785
1786		if (SCpnt) {
1787			u32 h = sb->ORB_offset_hi_misc;
1788			u32 r = STATUS_GET_RESP(h);
1789
1790			if (r != RESP_STATUS_REQUEST_COMPLETE) {
1791				SBP2_INFO("resp 0x%x, sbp_status 0x%x",
1792					  r, STATUS_GET_SBP_STATUS(h));
1793				scsi_status =
1794					r == RESP_STATUS_TRANSPORT_FAILURE ?
1795					SBP2_SCSI_STATUS_BUSY :
1796					SBP2_SCSI_STATUS_COMMAND_TERMINATED;
1797			}
1798
1799			if (STATUS_GET_LEN(h) > 1)
1800				scsi_status = sbp2_status_to_sense_data(
1801					(unchar *)sb, SCpnt->sense_buffer);
1802
1803			if (STATUS_TEST_DEAD(h))
1804                                sbp2_agent_reset(lu, 0);
1805		}
1806
1807		/* Check here to see if there are no commands in-use. If there
1808		 * are none, we know that the fetch agent left the active state
1809		 * _and_ that we did not reactivate it yet. Therefore clear
1810		 * last_orb so that next time we write directly to the
1811		 * ORB_POINTER register. That way the fetch agent does not need
1812		 * to refetch the next_ORB. */
1813		spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1814		if (list_empty(&lu->cmd_orb_inuse))
1815			lu->last_orb = NULL;
1816		spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1817
1818	} else {
1819		/* It's probably status after a management request. */
1820		if ((sb->ORB_offset_lo == lu->reconnect_orb_dma) ||
1821		    (sb->ORB_offset_lo == lu->login_orb_dma) ||
1822		    (sb->ORB_offset_lo == lu->query_logins_orb_dma) ||
1823		    (sb->ORB_offset_lo == lu->logout_orb_dma)) {
1824			lu->access_complete = 1;
1825			wake_up_interruptible(&sbp2_access_wq);
1826		}
1827	}
1828
1829	if (SCpnt)
1830		sbp2scsi_complete_command(lu, scsi_status, SCpnt,
1831					  cmd->Current_done);
1832	return RCODE_COMPLETE;
1833}
1834
1835/**************************************
1836 * SCSI interface related section
1837 **************************************/
1838
1839static int sbp2scsi_queuecommand(struct scsi_cmnd *SCpnt,
1840				 void (*done)(struct scsi_cmnd *))
1841{
1842	struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
1843	struct sbp2_fwhost_info *hi;
1844	int result = DID_NO_CONNECT << 16;
1845
1846	if (unlikely(!sbp2util_node_is_available(lu)))
1847		goto done;
1848
1849	hi = lu->hi;
1850
1851	if (unlikely(!hi)) {
1852		SBP2_ERR("sbp2_fwhost_info is NULL - this is bad!");
1853		goto done;
1854	}
1855
1856	/* Multiple units are currently represented to the SCSI core as separate
1857	 * targets, not as one target with multiple LUs. Therefore return
1858	 * selection time-out to any IO directed at non-zero LUNs. */
1859	if (unlikely(SCpnt->device->lun))
1860		goto done;
1861
1862	if (unlikely(!hpsb_node_entry_valid(lu->ne))) {
1863		SBP2_ERR("Bus reset in progress - rejecting command");
1864		result = DID_BUS_BUSY << 16;
1865		goto done;
1866	}
1867
1868	/* Bidirectional commands are not yet implemented,
1869	 * and unknown transfer direction not handled. */
1870	if (unlikely(SCpnt->sc_data_direction == DMA_BIDIRECTIONAL)) {
1871		SBP2_ERR("Cannot handle DMA_BIDIRECTIONAL - rejecting command");
1872		result = DID_ERROR << 16;
1873		goto done;
1874	}
1875
1876	if (sbp2_send_command(lu, SCpnt, done)) {
1877		SBP2_ERR("Error sending SCSI command");
1878		sbp2scsi_complete_command(lu,
1879					  SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
1880					  SCpnt, done);
1881	}
1882	return 0;
1883
1884done:
1885	SCpnt->result = result;
1886	done(SCpnt);
1887	return 0;
1888}
1889
1890static void sbp2scsi_complete_all_commands(struct sbp2_lu *lu, u32 status)
1891{
1892	struct sbp2_fwhost_info *hi = lu->hi;
1893	struct list_head *lh;
1894	struct sbp2_command_info *cmd;
1895	unsigned long flags;
1896
1897	spin_lock_irqsave(&lu->cmd_orb_lock, flags);
1898	while (!list_empty(&lu->cmd_orb_inuse)) {
1899		lh = lu->cmd_orb_inuse.next;
1900		cmd = list_entry(lh, struct sbp2_command_info, list);
1901		dma_sync_single_for_cpu(hi->host->device.parent,
1902				        cmd->command_orb_dma,
1903					sizeof(struct sbp2_command_orb),
1904					DMA_TO_DEVICE);
1905		dma_sync_single_for_cpu(hi->host->device.parent, cmd->sge_dma,
1906					sizeof(cmd->scatter_gather_element),
1907					DMA_TO_DEVICE);
1908		sbp2util_mark_command_completed(lu, cmd);
1909		if (cmd->Current_SCpnt) {
1910			cmd->Current_SCpnt->result = status << 16;
1911			cmd->Current_done(cmd->Current_SCpnt);
1912		}
1913	}
1914	spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
1915
1916	return;
1917}
1918
1919/*
1920 * Complete a regular SCSI command. Can be called in atomic context.
1921 */
1922static void sbp2scsi_complete_command(struct sbp2_lu *lu, u32 scsi_status,
1923				      struct scsi_cmnd *SCpnt,
1924				      void (*done)(struct scsi_cmnd *))
1925{
1926	if (!SCpnt) {
1927		SBP2_ERR("SCpnt is NULL");
1928		return;
1929	}
1930
1931	switch (scsi_status) {
1932	case SBP2_SCSI_STATUS_GOOD:
1933		SCpnt->result = DID_OK << 16;
1934		break;
1935
1936	case SBP2_SCSI_STATUS_BUSY:
1937		SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
1938		SCpnt->result = DID_BUS_BUSY << 16;
1939		break;
1940
1941	case SBP2_SCSI_STATUS_CHECK_CONDITION:
1942		SCpnt->result = CHECK_CONDITION << 1 | DID_OK << 16;
1943		break;
1944
1945	case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
1946		SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
1947		SCpnt->result = DID_NO_CONNECT << 16;
1948		scsi_print_command(SCpnt);
1949		break;
1950
1951	case SBP2_SCSI_STATUS_CONDITION_MET:
1952	case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
1953	case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
1954		SBP2_ERR("Bad SCSI status = %x", scsi_status);
1955		SCpnt->result = DID_ERROR << 16;
1956		scsi_print_command(SCpnt);
1957		break;
1958
1959	default:
1960		SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
1961		SCpnt->result = DID_ERROR << 16;
1962	}
1963
1964	/* If a bus reset is in progress and there was an error, complete
1965	 * the command as busy so that it will get retried. */
1966	if (!hpsb_node_entry_valid(lu->ne)
1967	    && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
1968		SBP2_ERR("Completing command with busy (bus reset)");
1969		SCpnt->result = DID_BUS_BUSY << 16;
1970	}
1971
1972	/* Tell the SCSI stack that we're done with this command. */
1973	done(SCpnt);
1974}
1975
1976static int sbp2scsi_slave_alloc(struct scsi_device *sdev)
1977{
1978	struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
1979
1980	lu->sdev = sdev;
1981	sdev->allow_restart = 1;
1982
1983	if (lu->workarounds & SBP2_WORKAROUND_INQUIRY_36)
1984		sdev->inquiry_len = 36;
1985	return 0;
1986}
1987
1988static int sbp2scsi_slave_configure(struct scsi_device *sdev)
1989{
1990	struct sbp2_lu *lu = (struct sbp2_lu *)sdev->host->hostdata[0];
1991
1992	sdev->use_10_for_rw = 1;
1993
1994	if (sdev->type == TYPE_ROM)
1995		sdev->use_10_for_ms = 1;
1996	if (sdev->type == TYPE_DISK &&
1997	    lu->workarounds & SBP2_WORKAROUND_MODE_SENSE_8)
1998		sdev->skip_ms_page_8 = 1;
1999	if (lu->workarounds & SBP2_WORKAROUND_FIX_CAPACITY)
2000		sdev->fix_capacity = 1;
2001	return 0;
2002}
2003
2004static void sbp2scsi_slave_destroy(struct scsi_device *sdev)
2005{
2006	((struct sbp2_lu *)sdev->host->hostdata[0])->sdev = NULL;
2007	return;
2008}
2009
2010/*
2011 * Called by scsi stack when something has really gone wrong.
2012 * Usually called when a command has timed-out for some reason.
2013 */
2014static int sbp2scsi_abort(struct scsi_cmnd *SCpnt)
2015{
2016	struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
2017	struct sbp2_fwhost_info *hi = lu->hi;
2018	struct sbp2_command_info *cmd;
2019	unsigned long flags;
2020
2021	SBP2_INFO("aborting sbp2 command");
2022	scsi_print_command(SCpnt);
2023
2024	if (sbp2util_node_is_available(lu)) {
2025		sbp2_agent_reset(lu, 1);
2026
2027		/* Return a matching command structure to the free pool. */
2028		spin_lock_irqsave(&lu->cmd_orb_lock, flags);
2029		cmd = sbp2util_find_command_for_SCpnt(lu, SCpnt);
2030		if (cmd) {
2031			dma_sync_single_for_cpu(hi->host->device.parent,
2032					cmd->command_orb_dma,
2033					sizeof(struct sbp2_command_orb),
2034					DMA_TO_DEVICE);
2035			dma_sync_single_for_cpu(hi->host->device.parent,
2036					cmd->sge_dma,
2037					sizeof(cmd->scatter_gather_element),
2038					DMA_TO_DEVICE);
2039			sbp2util_mark_command_completed(lu, cmd);
2040			if (cmd->Current_SCpnt) {
2041				cmd->Current_SCpnt->result = DID_ABORT << 16;
2042				cmd->Current_done(cmd->Current_SCpnt);
2043			}
2044		}
2045		spin_unlock_irqrestore(&lu->cmd_orb_lock, flags);
2046
2047		sbp2scsi_complete_all_commands(lu, DID_BUS_BUSY);
2048	}
2049
2050	return SUCCESS;
2051}
2052
2053/*
2054 * Called by scsi stack when something has really gone wrong.
2055 */
2056static int sbp2scsi_reset(struct scsi_cmnd *SCpnt)
2057{
2058	struct sbp2_lu *lu = (struct sbp2_lu *)SCpnt->device->host->hostdata[0];
2059
2060	SBP2_INFO("reset requested");
2061
2062	if (sbp2util_node_is_available(lu)) {
2063		SBP2_INFO("generating sbp2 fetch agent reset");
2064		sbp2_agent_reset(lu, 1);
2065	}
2066
2067	return SUCCESS;
2068}
2069
2070static ssize_t sbp2_sysfs_ieee1394_id_show(struct device *dev,
2071					   struct device_attribute *attr,
2072					   char *buf)
2073{
2074	struct scsi_device *sdev;
2075	struct sbp2_lu *lu;
2076
2077	if (!(sdev = to_scsi_device(dev)))
2078		return 0;
2079
2080	if (!(lu = (struct sbp2_lu *)sdev->host->hostdata[0]))
2081		return 0;
2082
2083	if (sbp2_long_sysfs_ieee1394_id)
2084		return sprintf(buf, "%016Lx:%06x:%04x\n",
2085				(unsigned long long)lu->ne->guid,
2086				lu->ud->directory_id, ORB_SET_LUN(lu->lun));
2087	else
2088		return sprintf(buf, "%016Lx:%d:%d\n",
2089				(unsigned long long)lu->ne->guid,
2090				lu->ud->id, ORB_SET_LUN(lu->lun));
2091}
2092
2093MODULE_AUTHOR("Ben Collins <bcollins@debian.org>");
2094MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2095MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2096MODULE_LICENSE("GPL");
2097
2098static int sbp2_module_init(void)
2099{
2100	int ret;
2101
2102	if (sbp2_serialize_io) {
2103		sbp2_shost_template.can_queue = 1;
2104		sbp2_shost_template.cmd_per_lun = 1;
2105	}
2106
2107	if (sbp2_default_workarounds & SBP2_WORKAROUND_128K_MAX_TRANS &&
2108	    (sbp2_max_sectors * 512) > (128 * 1024))
2109		sbp2_max_sectors = 128 * 1024 / 512;
2110	sbp2_shost_template.max_sectors = sbp2_max_sectors;
2111
2112	hpsb_register_highlevel(&sbp2_highlevel);
2113	ret = hpsb_register_protocol(&sbp2_driver);
2114	if (ret) {
2115		SBP2_ERR("Failed to register protocol");
2116		hpsb_unregister_highlevel(&sbp2_highlevel);
2117		return ret;
2118	}
2119	return 0;
2120}
2121
2122static void __exit sbp2_module_exit(void)
2123{
2124	hpsb_unregister_protocol(&sbp2_driver);
2125	hpsb_unregister_highlevel(&sbp2_highlevel);
2126}
2127
2128module_init(sbp2_module_init);
2129module_exit(sbp2_module_exit);
2130