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 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software Foundation,
19 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 */
21
22
23
24/*
25 * Includes
26 */
27
28#include <linux/config.h>
29#include <linux/kernel.h>
30#include <linux/list.h>
31#include <linux/string.h>
32#include <linux/slab.h>
33#include <linux/fs.h>
34#include <linux/poll.h>
35#include <linux/module.h>
36#include <linux/types.h>
37#include <linux/delay.h>
38#include <linux/sched.h>
39#include <linux/proc_fs.h>
40#include <linux/blk.h>
41#include <linux/smp_lock.h>
42#include <linux/init.h>
43#include <linux/blk.h>
44#include <asm/current.h>
45#include <asm/uaccess.h>
46#include <asm/io.h>
47#include <asm/byteorder.h>
48#include <asm/atomic.h>
49#include <asm/system.h>
50#include <asm/io.h>
51#include <asm/scatterlist.h>
52
53#ifdef CONFIG_KBUILD_2_5
54#include <scsi.h>
55#include <hosts.h>
56#include <sd.h>
57#else
58#include "../scsi/scsi.h"
59#include "../scsi/hosts.h"
60#include "../scsi/sd.h"
61#endif
62
63#include "ieee1394.h"
64#include "ieee1394_types.h"
65#include "ieee1394_core.h"
66#include "hosts.h"
67#include "nodemgr.h"
68#include "highlevel.h"
69#include "ieee1394_transactions.h"
70#include "ieee1394_hotplug.h"
71#include "sbp2.h"
72
73static char version[] __devinitdata =
74	"$Rev: 584 $ James Goodwin <jamesg@filanet.com>";
75
76/*
77 * Module load parameter definitions
78 */
79
80/*
81 * Change sbp2_max_speed on module load if you have a bad IEEE-1394
82 * controller that has trouble running 2KB packets at 400mb.
83 *
84 * NOTE: On certain OHCI parts I have seen short packets on async transmit
85 * (probably due to PCI latency/throughput issues with the part). You can
86 * bump down the speed if you are running into problems.
87 *
88 * Valid values:
89 * sbp2_max_speed = 2 (default: max speed 400mb)
90 * sbp2_max_speed = 1 (max speed 200mb)
91 * sbp2_max_speed = 0 (max speed 100mb)
92 */
93MODULE_PARM(sbp2_max_speed,"i");
94MODULE_PARM_DESC(sbp2_max_speed, "Force max speed (2 = 400mb default, 1 = 200mb, 0 = 100mb)");
95static int sbp2_max_speed = SPEED_400;
96
97/*
98 * Set sbp2_serialize_io to 1 if you'd like only one scsi command sent
99 * down to us at a time (debugging). This might be necessary for very
100 * badly behaved sbp2 devices.
101 */
102MODULE_PARM(sbp2_serialize_io,"i");
103MODULE_PARM_DESC(sbp2_serialize_io, "Serialize all I/O coming down from the scsi drivers (default = 0)");
104static int sbp2_serialize_io = 0;	/* serialize I/O - available for debugging purposes */
105
106/*
107 * Bump up sbp2_max_sectors if you'd like to support very large sized
108 * transfers. Please note that some older sbp2 bridge chips are broken for
109 * transfers greater or equal to 128KB.  Default is a value of 255
110 * sectors, or just under 128KB (at 512 byte sector size). I can note that
111 * the Oxsemi sbp2 chipsets have no problems supporting very large
112 * transfer sizes.
113 */
114MODULE_PARM(sbp2_max_sectors,"i");
115MODULE_PARM_DESC(sbp2_max_sectors, "Change max sectors per I/O supported (default = 255)");
116static int sbp2_max_sectors = SBP2_MAX_SECTORS;
117
118/*
119 * Adjust sbp2_max_outstanding_cmds to tune performance if you have many
120 * sbp2 devices attached (or if you need to do some debugging).
121 */
122MODULE_PARM(sbp2_max_outstanding_cmds,"i");
123MODULE_PARM_DESC(sbp2_max_outstanding_cmds, "Change max outstanding concurrent commands (default = 8)");
124static int sbp2_max_outstanding_cmds = SBP2SCSI_MAX_OUTSTANDING_CMDS;
125
126/*
127 * Adjust sbp2_max_cmds_per_lun to tune performance. Enabling more than
128 * one concurrent/linked command per sbp2 device may allow some
129 * performance gains, but some older sbp2 devices have firmware bugs
130 * resulting in problems when linking commands... so, enable this with
131 * care.  I can note that the Oxsemi OXFW911 sbp2 chipset works very well
132 * with large numbers of concurrent/linked commands.  =)
133 */
134MODULE_PARM(sbp2_max_cmds_per_lun,"i");
135MODULE_PARM_DESC(sbp2_max_cmds_per_lun, "Change max concurrent commands per sbp2 device (default = 1)");
136static int sbp2_max_cmds_per_lun = SBP2SCSI_MAX_CMDS_PER_LUN;
137
138/*
139 * Exclusive login to sbp2 device? In most cases, the sbp2 driver should
140 * do an exclusive login, as it's generally unsafe to have two hosts
141 * talking to a single sbp2 device at the same time (filesystem coherency,
142 * etc.). If you're running an sbp2 device that supports multiple logins,
143 * and you're either running read-only filesystems or some sort of special
144 * filesystem supporting multiple hosts, then set sbp2_exclusive_login to
145 * zero. Note: The Oxsemi OXFW911 sbp2 chipset supports up to four
146 * concurrent logins.
147 */
148MODULE_PARM(sbp2_exclusive_login,"i");
149MODULE_PARM_DESC(sbp2_exclusive_login, "Exclusive login to sbp2 device (default = 1)");
150static int sbp2_exclusive_login = 1;
151
152/*
153 * SCSI inquiry hack for really badly behaved sbp2 devices. Turn this on
154 * if your sbp2 device is not properly handling the SCSI inquiry command.
155 * This hack makes the inquiry look more like a typical MS Windows
156 * inquiry.
157 *
158 * If sbp2_force_inquiry_hack=1 is required for your device to work,
159 * please submit the logged sbp2_firmware_revision value of this device to
160 * the linux1394-devel mailing list.
161 */
162MODULE_PARM(sbp2_force_inquiry_hack,"i");
163MODULE_PARM_DESC(sbp2_force_inquiry_hack, "Force SCSI inquiry hack (default = 0)");
164static int sbp2_force_inquiry_hack = 0;
165
166
167/*
168 * Export information about protocols/devices supported by this driver.
169 */
170static struct ieee1394_device_id sbp2_id_table[] = {
171	{
172		.match_flags =IEEE1394_MATCH_SPECIFIER_ID |
173		              IEEE1394_MATCH_VERSION,
174		.specifier_id = SBP2_UNIT_SPEC_ID_ENTRY & 0xffffff,
175		.version =    SBP2_SW_VERSION_ENTRY & 0xffffff
176	},
177	{ }
178};
179
180MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
181
182/*
183 * Debug levels, configured via kernel config, or enable here.
184 */
185
186/* #define CONFIG_IEEE1394_SBP2_DEBUG_ORBS */
187/* #define CONFIG_IEEE1394_SBP2_DEBUG_DMA */
188/* #define CONFIG_IEEE1394_SBP2_DEBUG 1 */
189/* #define CONFIG_IEEE1394_SBP2_DEBUG 2 */
190/* #define CONFIG_IEEE1394_SBP2_PACKET_DUMP */
191
192#ifdef CONFIG_IEEE1394_SBP2_DEBUG_ORBS
193#define SBP2_ORB_DEBUG(fmt, args...)	HPSB_ERR("sbp2(%s): "fmt, __FUNCTION__, ## args)
194static u32 global_outstanding_command_orbs = 0;
195#define outstanding_orb_incr global_outstanding_command_orbs++
196#define outstanding_orb_decr global_outstanding_command_orbs--
197#else
198#define SBP2_ORB_DEBUG(fmt, args...)
199#define outstanding_orb_incr
200#define outstanding_orb_decr
201#endif
202
203#ifdef CONFIG_IEEE1394_SBP2_DEBUG_DMA
204#define SBP2_DMA_ALLOC(fmt, args...) \
205	HPSB_ERR("sbp2(%s)alloc(%d): "fmt, __FUNCTION__, \
206		 ++global_outstanding_dmas, ## args)
207#define SBP2_DMA_FREE(fmt, args...) \
208	HPSB_ERR("sbp2(%s)free(%d): "fmt, __FUNCTION__, \
209		 --global_outstanding_dmas, ## args)
210static u32 global_outstanding_dmas = 0;
211#else
212#define SBP2_DMA_ALLOC(fmt, args...)
213#define SBP2_DMA_FREE(fmt, args...)
214#endif
215
216#if CONFIG_IEEE1394_SBP2_DEBUG >= 2
217#define SBP2_DEBUG(fmt, args...)	HPSB_ERR("sbp2: "fmt, ## args)
218#define SBP2_INFO(fmt, args...)		HPSB_ERR("sbp2: "fmt, ## args)
219#define SBP2_NOTICE(fmt, args...)	HPSB_ERR("sbp2: "fmt, ## args)
220#define SBP2_WARN(fmt, args...)		HPSB_ERR("sbp2: "fmt, ## args)
221#elif CONFIG_IEEE1394_SBP2_DEBUG == 1
222#define SBP2_DEBUG(fmt, args...)	HPSB_DEBUG("sbp2: "fmt, ## args)
223#define SBP2_INFO(fmt, args...)		HPSB_INFO("sbp2: "fmt, ## args)
224#define SBP2_NOTICE(fmt, args...)	HPSB_NOTICE("sbp2: "fmt, ## args)
225#define SBP2_WARN(fmt, args...)		HPSB_WARN("sbp2: "fmt, ## args)
226#else
227#define SBP2_DEBUG(fmt, args...)
228#define SBP2_INFO(fmt, args...)		HPSB_INFO("sbp2: "fmt, ## args)
229#define SBP2_NOTICE(fmt, args...)       HPSB_NOTICE("sbp2: "fmt, ## args)
230#define SBP2_WARN(fmt, args...)         HPSB_WARN("sbp2: "fmt, ## args)
231#endif
232
233#define SBP2_ERR(fmt, args...)		HPSB_ERR("sbp2: "fmt, ## args)
234
235/*
236 * Spinlock debugging stuff.
237 */
238#define SBP2_USE_REAL_SPINLOCKS
239
240#ifdef SBP2_USE_REAL_SPINLOCKS
241#define sbp2_spin_lock(lock, flags)	spin_lock_irqsave(lock, flags)
242#define sbp2_spin_unlock(lock, flags)	spin_unlock_irqrestore(lock, flags);
243static spinlock_t sbp2_host_info_lock = SPIN_LOCK_UNLOCKED;
244#else
245#define sbp2_spin_lock(lock, flags)	do {save_flags(flags); cli();} while (0)
246#define sbp2_spin_unlock(lock, flags)	do {restore_flags(flags);} while (0)
247#endif
248
249/*
250 * Globals
251 */
252
253static Scsi_Host_Template scsi_driver_template;
254
255static u8 sbp2_speedto_maxrec[] = { 0x7, 0x8, 0x9 };
256
257static LIST_HEAD(sbp2_host_info_list);
258
259static struct hpsb_highlevel *sbp2_hl_handle = NULL;
260
261static struct hpsb_highlevel_ops sbp2_hl_ops = {
262	.add_host =	sbp2_add_host,
263	.remove_host =	sbp2_remove_host,
264};
265
266static struct hpsb_address_ops sbp2_ops = {
267	.write = sbp2_handle_status_write
268};
269
270#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
271static struct hpsb_address_ops sbp2_physdma_ops = {
272        .read = sbp2_handle_physdma_read,
273        .write = sbp2_handle_physdma_write,
274};
275#endif
276
277static struct hpsb_protocol_driver sbp2_driver = {
278	.name =		"SBP2 Driver",
279	.id_table = 	sbp2_id_table,
280	.probe = 		sbp2_probe,
281	.disconnect = 	sbp2_disconnect,
282	.update = 	sbp2_update
283};
284
285/* List of device firmware's that require a forced 36 byte inquiry.  */
286static u32 sbp2_broken_inquiry_list[] = {
287	0x00002800,	/* Stefan Richter <richtest@bauwesen.tu-cottbus.de> */
288			/* DViCO Momobay CX-1 */
289	0x00000200	/* Andreas Plesch <plesch@fas.harvard.edu> */
290			/* QPS Fire DVDBurner */
291};
292
293#define NUM_BROKEN_INQUIRY_DEVS \
294	(sizeof(sbp2_broken_inquiry_list)/sizeof(*sbp2_broken_inquiry_list))
295
296/**************************************
297 * General utility functions
298 **************************************/
299
300
301#ifndef __BIG_ENDIAN
302/*
303 * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
304 */
305static __inline__ void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
306{
307	u32 *temp = buffer;
308
309	for (length = (length >> 2); length--; )
310		temp[length] = be32_to_cpu(temp[length]);
311
312	return;
313}
314
315/*
316 * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
317 */
318static __inline__ void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
319{
320	u32 *temp = buffer;
321
322	for (length = (length >> 2); length--; )
323		temp[length] = cpu_to_be32(temp[length]);
324
325	return;
326}
327#else /* BIG_ENDIAN */
328/* Why waste the cpu cycles? */
329#define sbp2util_be32_to_cpu_buffer(x,y)
330#define sbp2util_cpu_to_be32_buffer(x,y)
331#endif
332
333#ifdef CONFIG_IEEE1394_SBP2_PACKET_DUMP
334/*
335 * Debug packet dump routine. Length is in bytes.
336 */
337static void sbp2util_packet_dump(void *buffer, int length, char *dump_name, u32 dump_phys_addr)
338{
339	int i;
340	unsigned char *dump = buffer;
341
342	if (!dump || !length || !dump_name)
343		return;
344
345	if (dump_phys_addr)
346		printk("[%s, 0x%x]", dump_name, dump_phys_addr);
347	else
348		printk("[%s]", dump_name);
349	for (i = 0; i < length; i++) {
350		if (i > 0x3f) {
351			printk("\n   ...");
352			break;
353		}
354		if ((i & 0x3) == 0)
355			printk("  ");
356		if ((i & 0xf) == 0)
357			printk("\n   ");
358		printk("%02x ", (int) dump[i]);
359	}
360	printk("\n");
361
362	return;
363}
364#else
365#define sbp2util_packet_dump(w,x,y,z)
366#endif
367
368/*
369 * Goofy routine that basically does a down_timeout function.
370 */
371static int sbp2util_down_timeout(atomic_t *done, int timeout)
372{
373	int i;
374
375	for (i = timeout; (i > 0 && atomic_read(done) == 0); i-= HZ/10) {
376		set_current_state(TASK_INTERRUPTIBLE);
377		if (schedule_timeout(HZ/10))	/* 100ms */
378			return(1);
379	}
380	return ((i > 0) ? 0:1);
381}
382
383/*
384 * This function is called to initially create a packet pool for use in
385 * sbp2 I/O requests. This packet pool is used when sending out sbp2
386 * command and agent reset requests, and allows us to remove all
387 * kmallocs/kfrees from the critical I/O paths.
388 */
389static int sbp2util_create_request_packet_pool(struct sbp2scsi_host_info *hi)
390{
391	struct hpsb_packet *packet;
392	int i;
393
394	hi->request_packet = kmalloc(sizeof(struct sbp2_request_packet) * SBP2_MAX_REQUEST_PACKETS,
395				     GFP_KERNEL);
396
397	if (!hi->request_packet) {
398		SBP2_ERR("sbp2util_create_request_packet_pool - packet allocation failed!");
399		return(-ENOMEM);
400	}
401	memset(hi->request_packet, 0, sizeof(struct sbp2_request_packet) * SBP2_MAX_REQUEST_PACKETS);
402
403	/*
404	 * Create a pool of request packets. Just take the max supported
405	 * concurrent commands and multiply by two to be safe...
406	 */
407	for (i=0; i<SBP2_MAX_REQUEST_PACKETS; i++) {
408
409		/*
410		 * Max payload of 8 bytes since the sbp2 command request
411		 * uses a payload of 8 bytes, and agent reset is a quadlet
412		 * write request. Bump this up if we plan on using this
413		 * pool for other stuff.
414		 */
415		packet = alloc_hpsb_packet(8);
416
417		if (!packet) {
418			SBP2_ERR("sbp2util_create_request_packet_pool - packet allocation failed!");
419			return(-ENOMEM);
420		}
421
422		/*
423		 * Put these request packets into a free list
424		 */
425		INIT_LIST_HEAD(&hi->request_packet[i].list);
426		hi->request_packet[i].packet = packet;
427		list_add_tail(&hi->request_packet[i].list, &hi->sbp2_req_free);
428
429	}
430
431	return(0);
432}
433
434/*
435 * This function is called to remove the packet pool. It is called when
436 * the sbp2 driver is unloaded.
437 */
438static void sbp2util_remove_request_packet_pool(struct sbp2scsi_host_info *hi)
439{
440	struct list_head *lh;
441	struct sbp2_request_packet *request_packet;
442	unsigned long flags;
443
444	/*
445	 * Go through free list releasing packets
446	 */
447	sbp2_spin_lock(&hi->sbp2_request_packet_lock, flags);
448	while (!list_empty(&hi->sbp2_req_free)) {
449
450		lh = hi->sbp2_req_free.next;
451		list_del(lh);
452
453		request_packet = list_entry(lh, struct sbp2_request_packet, list);
454
455		/*
456		 * Free the hpsb packets that we allocated for the pool
457		 */
458		if (request_packet) {
459			free_hpsb_packet(request_packet->packet);
460		}
461
462	}
463	kfree(hi->request_packet);
464	sbp2_spin_unlock(&hi->sbp2_request_packet_lock, flags);
465
466	return;
467}
468
469/*
470 * This function is called to retrieve a block write packet from our
471 * packet pool. This function is used in place of calling
472 * alloc_hpsb_packet (which costs us three kmallocs). Instead we just pull
473 * out a free request packet and re-initialize values in it. I'm sure this
474 * can still stand some more optimization.
475 */
476static struct sbp2_request_packet *
477sbp2util_allocate_write_request_packet(struct sbp2scsi_host_info *hi,
478				       struct node_entry *ne, u64 addr,
479				       size_t data_size,
480				       quadlet_t data) {
481	struct list_head *lh;
482	struct sbp2_request_packet *request_packet = NULL;
483	struct hpsb_packet *packet;
484	unsigned long flags;
485
486	sbp2_spin_lock(&hi->sbp2_request_packet_lock, flags);
487	if (!list_empty(&hi->sbp2_req_free)) {
488
489		/*
490		 * Pull out a free request packet
491		 */
492		lh = hi->sbp2_req_free.next;
493		list_del(lh);
494
495		request_packet = list_entry(lh, struct sbp2_request_packet, list);
496		packet = request_packet->packet;
497
498		/*
499		 * Initialize the packet (this is really initialization
500		 * the core 1394 stack should do, but I'm doing it myself
501		 * to avoid the overhead).
502		 */
503		packet->data_size = data_size;
504		INIT_LIST_HEAD(&packet->list);
505		sema_init(&packet->state_change, 0);
506		packet->state = hpsb_unused;
507		packet->data_be = 1;
508
509		hpsb_node_fill_packet(ne, packet);
510
511		packet->tlabel = get_tlabel(hi->host, packet->node_id, 0);
512
513		if (!data_size) {
514			fill_async_writequad(packet, addr, data);
515		} else {
516			fill_async_writeblock(packet, addr, data_size);
517		}
518
519		/*
520		 * Set up a task queue completion routine, which returns
521		 * the packet to the free list and releases the tlabel.
522		 */
523		request_packet->tq.routine = (void (*)(void*))sbp2util_free_request_packet;
524		request_packet->tq.data = request_packet;
525		request_packet->hi_context = hi;
526		hpsb_add_packet_complete_task(packet, &request_packet->tq);
527
528		/*
529		 * Now, put the packet on the in-use list.
530		 */
531		list_add_tail(&request_packet->list, &hi->sbp2_req_inuse);
532
533	} else {
534		SBP2_ERR("sbp2util_allocate_request_packet - no packets available!");
535	}
536	sbp2_spin_unlock(&hi->sbp2_request_packet_lock, flags);
537
538	return(request_packet);
539}
540
541/*
542 * This function is called to return a packet to our packet pool. It is
543 * also called as a completion routine when a request packet is completed.
544 */
545static void sbp2util_free_request_packet(struct sbp2_request_packet *request_packet)
546{
547	unsigned long flags;
548	struct sbp2scsi_host_info *hi = request_packet->hi_context;
549
550	/*
551	 * Free the tlabel, and return the packet to the free pool.
552	 */
553	sbp2_spin_lock(&hi->sbp2_request_packet_lock, flags);
554	free_tlabel(hi->host, LOCAL_BUS | request_packet->packet->node_id,
555		    request_packet->packet->tlabel);
556	list_del(&request_packet->list);
557	list_add_tail(&request_packet->list, &hi->sbp2_req_free);
558	sbp2_spin_unlock(&hi->sbp2_request_packet_lock, flags);
559
560	return;
561}
562
563/*
564 * This function is called to create a pool of command orbs used for
565 * command processing. It is called when a new sbp2 device is detected.
566 */
567static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id,
568					    struct sbp2scsi_host_info *hi)
569{
570	int i;
571	unsigned long flags;
572	struct sbp2_command_info *command;
573
574	sbp2_spin_lock(&scsi_id->sbp2_command_orb_lock, flags);
575	for (i = 0; i < scsi_id->sbp2_total_command_orbs; i++) {
576		command = (struct sbp2_command_info *)
577		    kmalloc(sizeof(struct sbp2_command_info), GFP_KERNEL);
578		if (!command) {
579			sbp2_spin_unlock(&scsi_id->sbp2_command_orb_lock, flags);
580			return(-ENOMEM);
581		}
582		memset(command, '\0', sizeof(struct sbp2_command_info));
583		command->command_orb_dma =
584			pci_map_single (hi->host->pdev, &command->command_orb,
585					sizeof(struct sbp2_command_orb),
586					PCI_DMA_BIDIRECTIONAL);
587		SBP2_DMA_ALLOC("single command orb DMA");
588		command->sge_dma =
589			pci_map_single (hi->host->pdev, &command->scatter_gather_element,
590					sizeof(command->scatter_gather_element),
591					PCI_DMA_BIDIRECTIONAL);
592		SBP2_DMA_ALLOC("scatter_gather_element");
593		INIT_LIST_HEAD(&command->list);
594		list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
595	}
596	sbp2_spin_unlock(&scsi_id->sbp2_command_orb_lock, flags);
597	return 0;
598}
599
600/*
601 * This function is called to delete a pool of command orbs.
602 */
603static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id,
604					     struct sbp2scsi_host_info *hi)
605{
606	struct list_head *lh, *next;
607	struct sbp2_command_info *command;
608	unsigned long flags;
609
610	sbp2_spin_lock(&scsi_id->sbp2_command_orb_lock, flags);
611	if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
612		list_for_each_safe(lh, next, &scsi_id->sbp2_command_orb_completed) {
613			command = list_entry(lh, struct sbp2_command_info, list);
614
615			/* Release our generic DMA's */
616			pci_unmap_single(hi->host->pdev, command->command_orb_dma,
617					 sizeof(struct sbp2_command_orb),
618					 PCI_DMA_BIDIRECTIONAL);
619			SBP2_DMA_FREE("single command orb DMA");
620			pci_unmap_single(hi->host->pdev, command->sge_dma,
621					 sizeof(command->scatter_gather_element),
622					 PCI_DMA_BIDIRECTIONAL);
623			SBP2_DMA_FREE("scatter_gather_element");
624
625			kfree(command);
626		}
627	}
628	sbp2_spin_unlock(&scsi_id->sbp2_command_orb_lock, flags);
629	return;
630}
631
632/*
633 * This function finds the sbp2_command for a given outstanding command
634 * orb.Only looks at the inuse list.
635 */
636static struct sbp2_command_info *sbp2util_find_command_for_orb(
637		struct scsi_id_instance_data *scsi_id, dma_addr_t orb)
638{
639	struct list_head *lh;
640	struct sbp2_command_info *command;
641	unsigned long flags;
642
643	sbp2_spin_lock(&scsi_id->sbp2_command_orb_lock, flags);
644	if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
645		list_for_each(lh, &scsi_id->sbp2_command_orb_inuse) {
646			command = list_entry(lh, struct sbp2_command_info, list);
647			if (command->command_orb_dma == orb) {
648				sbp2_spin_unlock(&scsi_id->sbp2_command_orb_lock, flags);
649				return (command);
650			}
651		}
652	}
653	sbp2_spin_unlock(&scsi_id->sbp2_command_orb_lock, flags);
654
655	SBP2_ORB_DEBUG("could not match command orb %x", (unsigned int)orb);
656
657	return(NULL);
658}
659
660/*
661 * This function finds the sbp2_command for a given outstanding SCpnt.
662 * Only looks at the inuse list.
663 */
664static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(struct scsi_id_instance_data *scsi_id, void *SCpnt)
665{
666	struct list_head *lh;
667	struct sbp2_command_info *command;
668	unsigned long flags;
669
670	sbp2_spin_lock(&scsi_id->sbp2_command_orb_lock, flags);
671	if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
672		list_for_each(lh, &scsi_id->sbp2_command_orb_inuse) {
673			command = list_entry(lh, struct sbp2_command_info, list);
674			if (command->Current_SCpnt == SCpnt) {
675				sbp2_spin_unlock(&scsi_id->sbp2_command_orb_lock, flags);
676				return (command);
677			}
678		}
679	}
680	sbp2_spin_unlock(&scsi_id->sbp2_command_orb_lock, flags);
681	return(NULL);
682}
683
684/*
685 * This function allocates a command orb used to send a scsi command.
686 */
687static struct sbp2_command_info *sbp2util_allocate_command_orb(
688		struct scsi_id_instance_data *scsi_id,
689		Scsi_Cmnd *Current_SCpnt,
690		void (*Current_done)(Scsi_Cmnd *),
691		struct sbp2scsi_host_info *hi)
692{
693	struct list_head *lh;
694	struct sbp2_command_info *command = NULL;
695	unsigned long flags;
696
697	sbp2_spin_lock(&scsi_id->sbp2_command_orb_lock, flags);
698	if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
699		lh = scsi_id->sbp2_command_orb_completed.next;
700		list_del(lh);
701		command = list_entry(lh, struct sbp2_command_info, list);
702		command->Current_done = Current_done;
703		command->Current_SCpnt = Current_SCpnt;
704		list_add_tail(&command->list, &scsi_id->sbp2_command_orb_inuse);
705	} else {
706		SBP2_ERR("sbp2util_allocate_command_orb - No orbs available!");
707	}
708	sbp2_spin_unlock(&scsi_id->sbp2_command_orb_lock, flags);
709	return (command);
710}
711
712/* Free our DMA's */
713static void sbp2util_free_command_dma(struct sbp2_command_info *command)
714{
715	struct sbp2scsi_host_info *hi;
716
717	hi = (struct sbp2scsi_host_info *) command->Current_SCpnt->host->hostdata[0];
718
719	if (hi == NULL) {
720		printk(KERN_ERR "%s: hi == NULL\n", __FUNCTION__);
721		return;
722	}
723
724	if (command->cmd_dma) {
725		if (command->dma_type == CMD_DMA_SINGLE) {
726			pci_unmap_single(hi->host->pdev, command->cmd_dma,
727					 command->dma_size, command->dma_dir);
728			SBP2_DMA_FREE("single bulk");
729		} else if (command->dma_type == CMD_DMA_PAGE) {
730#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,13)
731			pci_unmap_single(hi->host->pdev, command->cmd_dma,
732					 command->dma_size, command->dma_dir);
733#else
734			pci_unmap_page(hi->host->pdev, command->cmd_dma,
735				       command->dma_size, command->dma_dir);
736#endif /* Linux version < 2.4.13 */
737			SBP2_DMA_FREE("single page");
738		}
739		command->dma_type = CMD_DMA_NONE;
740		command->cmd_dma = 0;
741	}
742
743	if (command->sge_buffer) {
744		pci_unmap_sg(hi->host->pdev, command->sge_buffer,
745			     command->dma_size, command->dma_dir);
746		SBP2_DMA_FREE("scatter list");
747		command->sge_buffer = NULL;
748	}
749}
750
751/*
752 * This function moves a command to the completed orb list.
753 */
754static void sbp2util_mark_command_completed(struct scsi_id_instance_data *scsi_id, struct sbp2_command_info *command)
755{
756	unsigned long flags;
757
758	sbp2_spin_lock(&scsi_id->sbp2_command_orb_lock, flags);
759	list_del(&command->list);
760	sbp2util_free_command_dma(command);
761	list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
762	sbp2_spin_unlock(&scsi_id->sbp2_command_orb_lock, flags);
763}
764
765
766
767/*********************************************
768 * IEEE-1394 core driver stack related section
769 *********************************************/
770
771/*
772 * This function is called at SCSI init in order to register our driver
773 * with the IEEE-1394 stack.
774 */
775int sbp2_init(void)
776{
777	SBP2_DEBUG("sbp2_init");
778
779	/*
780	 * Register our high level driver with 1394 stack
781	 */
782	sbp2_hl_handle = hpsb_register_highlevel(SBP2_DEVICE_NAME, &sbp2_hl_ops);
783
784	if (sbp2_hl_handle == NULL) {
785		SBP2_ERR("sbp2 failed to register with ieee1394 highlevel");
786		return(-ENOMEM);
787	}
788
789	/*
790	 * Register our sbp2 status address space...
791	 */
792	hpsb_register_addrspace(sbp2_hl_handle, &sbp2_ops, SBP2_STATUS_FIFO_ADDRESS,
793				SBP2_STATUS_FIFO_ADDRESS +
794				SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(SBP2SCSI_MAX_SCSI_IDS+1));
795
796	/*
797	 * Handle data movement if physical dma is not enabled/supported on host controller
798	 */
799#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
800	hpsb_register_addrspace(sbp2_hl_handle, &sbp2_physdma_ops, 0x0ULL, 0xfffffffcULL);
801#endif
802
803	hpsb_register_protocol(&sbp2_driver);
804
805	return 0;
806}
807
808/*
809 * This function is called from cleanup module, or during shut-down, in
810 * order to unregister our driver.
811 */
812void sbp2_cleanup(void)
813{
814	SBP2_DEBUG("sbp2_cleanup");
815
816	hpsb_unregister_protocol(&sbp2_driver);
817
818	if (sbp2_hl_handle) {
819		hpsb_unregister_highlevel(sbp2_hl_handle);
820		sbp2_hl_handle = NULL;
821	}
822}
823
824static int sbp2_probe(struct unit_directory *ud)
825{
826	struct sbp2scsi_host_info *hi;
827
828	SBP2_DEBUG("sbp2_probe");
829	hi = sbp2_find_host_info(ud->ne->host);
830
831	return sbp2_start_device(hi, ud);
832}
833
834static void sbp2_disconnect(struct unit_directory *ud)
835{
836	struct sbp2scsi_host_info *hi;
837	struct scsi_id_instance_data *scsi_id = ud->driver_data;
838
839	SBP2_DEBUG("sbp2_disconnect");
840	hi = sbp2_find_host_info(ud->ne->host);
841
842	if (hi != NULL) {
843		sbp2_logout_device(hi, scsi_id);
844 		sbp2_remove_device(hi, scsi_id);
845	}
846}
847
848static void sbp2_update(struct unit_directory *ud)
849{
850	struct sbp2scsi_host_info *hi;
851	struct scsi_id_instance_data *scsi_id = ud->driver_data;
852	unsigned long flags;
853
854	SBP2_DEBUG("sbp2_update");
855	hi = sbp2_find_host_info(ud->ne->host);
856
857	if (sbp2_reconnect_device(hi, scsi_id)) {
858
859		/*
860		 * Ok, reconnect has failed. Perhaps we didn't
861		 * reconnect fast enough. Try doing a regular login.
862		 */
863		if (sbp2_login_device(hi, scsi_id)) {
864
865			/* Login failed too, just remove the device. */
866			SBP2_ERR("sbp2_reconnect_device failed!");
867			sbp2_remove_device(hi, scsi_id);
868			hpsb_release_unit_directory(ud);
869			return;
870		}
871	}
872
873	/* Set max retries to something large on the device. */
874	sbp2_set_busy_timeout(hi, scsi_id);
875
876	/* Do a SBP-2 fetch agent reset. */
877	sbp2_agent_reset(hi, scsi_id, 0);
878
879	/* Get the max speed and packet size that we can use. */
880	sbp2_max_speed_and_size(hi, scsi_id);
881
882	/* Complete any pending commands with busy (so they get
883	 * retried) and remove them from our queue
884	 */
885	sbp2_spin_lock(&hi->sbp2_command_lock, flags);
886	sbp2scsi_complete_all_commands(hi, scsi_id, DID_BUS_BUSY);
887	sbp2_spin_unlock(&hi->sbp2_command_lock, flags);
888}
889
890/*
891 * This function is called after registering our operations in sbp2_init.
892 * We go ahead and allocate some memory for our host info structure, and
893 * init some structures.
894 */
895static void sbp2_add_host(struct hpsb_host *host)
896{
897	struct sbp2scsi_host_info *hi;
898	unsigned long flags;
899
900	SBP2_DEBUG("sbp2_add_host");
901
902	/* Allocate some memory for our host info structure */
903	hi = (struct sbp2scsi_host_info *)kmalloc(sizeof(struct sbp2scsi_host_info),
904						  GFP_KERNEL);
905
906	if (hi == NULL) {
907		SBP2_ERR("out of memory in sbp2_add_host");
908		return;
909	}
910
911	/* Initialize some host stuff */
912	memset(hi, 0, sizeof(struct sbp2scsi_host_info));
913	INIT_LIST_HEAD(&hi->list);
914	INIT_LIST_HEAD(&hi->sbp2_req_inuse);
915	INIT_LIST_HEAD(&hi->sbp2_req_free);
916	hi->host = host;
917	hi->sbp2_command_lock = SPIN_LOCK_UNLOCKED;
918	hi->sbp2_request_packet_lock = SPIN_LOCK_UNLOCKED;
919
920	/* Create our request packet pool (pool of packets for use in I/O) */
921	if (sbp2util_create_request_packet_pool(hi)) {
922		SBP2_ERR("sbp2util_create_request_packet_pool failed!");
923		return;
924	}
925
926	sbp2_spin_lock(&sbp2_host_info_lock, flags);
927	list_add_tail(&hi->list, &sbp2_host_info_list);
928	sbp2_spin_unlock(&sbp2_host_info_lock, flags);
929
930	/* Register our host with the SCSI stack. */
931	hi->scsi_host = scsi_register (&scsi_driver_template, sizeof(void *));
932	if (hi->scsi_host) {
933		hi->scsi_host->hostdata[0] = (unsigned long)hi;
934		hi->scsi_host->max_id = SBP2SCSI_MAX_SCSI_IDS;
935	}
936	scsi_driver_template.present++;
937
938	return;
939}
940
941/*
942 * This fuction returns a host info structure from the host structure, in
943 * case we have multiple hosts.
944 */
945static struct sbp2scsi_host_info *sbp2_find_host_info(struct hpsb_host *host)
946{
947	struct list_head *lh;
948	struct sbp2scsi_host_info *hi;
949
950	list_for_each (lh, &sbp2_host_info_list) {
951		hi = list_entry(lh, struct sbp2scsi_host_info, list);
952		if (hi->host == host)
953			return hi;
954	}
955
956	return NULL;
957}
958
959/*
960 * This function returns a host info structure for a given Scsi_Host
961 * struct.
962 */
963static struct sbp2scsi_host_info *sbp2_find_host_info_scsi(struct Scsi_Host *host)
964{
965	struct list_head *lh;
966	struct sbp2scsi_host_info *hi;
967
968	list_for_each (lh, &sbp2_host_info_list) {
969		hi = list_entry(lh, struct sbp2scsi_host_info, list);
970		if (hi->scsi_host == host)
971			return hi;
972	}
973
974	return NULL;
975}
976
977/*
978 * This function is called when a host is removed.
979 */
980static void sbp2_remove_host(struct hpsb_host *host)
981{
982	struct sbp2scsi_host_info *hi;
983	unsigned long flags;
984
985	SBP2_DEBUG("sbp2_remove_host");
986
987	sbp2_spin_lock(&sbp2_host_info_lock, flags);
988
989	hi = sbp2_find_host_info(host);
990	if (hi != NULL) {
991		sbp2util_remove_request_packet_pool(hi);
992		list_del(&hi->list);
993		kfree(hi);
994	}
995	else
996		SBP2_ERR("attempt to remove unknown host %p", host);
997
998	sbp2_spin_unlock(&sbp2_host_info_lock, flags);
999}
1000
1001/*
1002 * This function is where we first pull the node unique ids, and then
1003 * allocate memory and register a SBP-2 device.
1004 */
1005static int sbp2_start_device(struct sbp2scsi_host_info *hi, struct unit_directory *ud)
1006{
1007	struct scsi_id_instance_data *scsi_id = NULL;
1008	struct node_entry *ne;
1009	int i;
1010
1011	SBP2_DEBUG("sbp2_start_device");
1012	ne = ud->ne;
1013
1014	/*
1015	 * This really is a "new" device plugged in. Let's allocate memory
1016	 * for our scsi id instance data.
1017	 */
1018	scsi_id = (struct scsi_id_instance_data *)kmalloc(sizeof(struct scsi_id_instance_data),
1019							  GFP_KERNEL);
1020	if (!scsi_id)
1021		goto alloc_fail_first;
1022	memset(scsi_id, 0, sizeof(struct scsi_id_instance_data));
1023
1024	/* Login FIFO DMA */
1025	scsi_id->login_response =
1026		pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_login_response),
1027				     &scsi_id->login_response_dma);
1028	if (!scsi_id->login_response)
1029		goto alloc_fail;
1030	SBP2_DMA_ALLOC("consistent DMA region for login FIFO");
1031
1032	/* Reconnect ORB DMA */
1033	scsi_id->reconnect_orb =
1034		pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_reconnect_orb),
1035				     &scsi_id->reconnect_orb_dma);
1036	if (!scsi_id->reconnect_orb)
1037		goto alloc_fail;
1038	SBP2_DMA_ALLOC("consistent DMA region for reconnect ORB");
1039
1040	/* Logout ORB DMA */
1041	scsi_id->logout_orb =
1042		pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_logout_orb),
1043				     &scsi_id->logout_orb_dma);
1044	if (!scsi_id->logout_orb)
1045		goto alloc_fail;
1046	SBP2_DMA_ALLOC("consistent DMA region for logout ORB");
1047
1048	/* Login ORB DMA */
1049	scsi_id->login_orb =
1050		pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_login_orb),
1051				     &scsi_id->login_orb_dma);
1052	if (scsi_id->login_orb == NULL) {
1053alloc_fail:
1054		if (scsi_id->logout_orb) {
1055			pci_free_consistent(hi->host->pdev,
1056					sizeof(struct sbp2_logout_orb),
1057					scsi_id->logout_orb,
1058					scsi_id->logout_orb_dma);
1059			SBP2_DMA_FREE("logout ORB DMA");
1060		}
1061
1062		if (scsi_id->reconnect_orb) {
1063			pci_free_consistent(hi->host->pdev,
1064					sizeof(struct sbp2_reconnect_orb),
1065					scsi_id->reconnect_orb,
1066					scsi_id->reconnect_orb_dma);
1067			SBP2_DMA_FREE("reconnect ORB DMA");
1068		}
1069
1070		if (scsi_id->login_response) {
1071			pci_free_consistent(hi->host->pdev,
1072					sizeof(struct sbp2_login_response),
1073					scsi_id->login_response,
1074					scsi_id->login_response_dma);
1075			SBP2_DMA_FREE("login FIFO DMA");
1076		}
1077
1078		kfree(scsi_id);
1079alloc_fail_first:
1080		SBP2_ERR ("Could not allocate memory for scsi_id");
1081		return(-ENOMEM);
1082	}
1083	SBP2_DMA_ALLOC("consistent DMA region for login ORB");
1084
1085	/*
1086	 * Initialize some of the fields in this structure
1087	 */
1088	scsi_id->ne = ne;
1089	scsi_id->ud = ud;
1090	scsi_id->speed_code = SPEED_100;
1091	scsi_id->max_payload_size = sbp2_speedto_maxrec[SPEED_100];
1092	ud->driver_data = scsi_id;
1093
1094	atomic_set(&scsi_id->sbp2_login_complete, 0);
1095
1096	/*
1097	 * Initialize structures needed for the command orb pool.
1098	 */
1099	INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_inuse);
1100	INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_completed);
1101	scsi_id->sbp2_command_orb_lock = SPIN_LOCK_UNLOCKED;
1102	scsi_id->sbp2_total_command_orbs = 0;
1103
1104	/*
1105	 * Make sure that we've gotten ahold of the sbp2 management agent
1106	 * address. Also figure out the command set being used (SCSI or
1107	 * RBC).
1108	 */
1109	sbp2_parse_unit_directory(scsi_id);
1110
1111	scsi_id->sbp2_total_command_orbs = SBP2_MAX_COMMAND_ORBS;
1112
1113	/*
1114	 * Knock the total command orbs down if we are serializing I/O
1115	 */
1116	if (sbp2_serialize_io) {
1117		scsi_id->sbp2_total_command_orbs = 2;	/* one extra for good measure */
1118	}
1119
1120	/*
1121	 * Find an empty spot to stick our scsi id instance data.
1122	 */
1123	for (i = 0; i < SBP2SCSI_MAX_SCSI_IDS; i++) {
1124		if (!hi->scsi_id[i]) {
1125			hi->scsi_id[i] = scsi_id;
1126			scsi_id->id = i;
1127			SBP2_DEBUG("New SBP-2 device inserted, SCSI ID = %x", (unsigned int) i);
1128			break;
1129		}
1130	}
1131
1132	/*
1133	 * Create our command orb pool
1134	 */
1135	if (sbp2util_create_command_orb_pool(scsi_id, hi)) {
1136		SBP2_ERR("sbp2util_create_command_orb_pool failed!");
1137		sbp2_remove_device(hi, scsi_id);
1138		return -ENOMEM;
1139	}
1140
1141	/*
1142	 * Make sure we are not out of space
1143	 */
1144	if (i == SBP2SCSI_MAX_SCSI_IDS) {
1145		SBP2_ERR("No slots left for SBP-2 device");
1146		sbp2_remove_device(hi, scsi_id);
1147		return -EBUSY;
1148	}
1149
1150	/*
1151	 * Login to the sbp-2 device
1152	 */
1153	if (sbp2_login_device(hi, scsi_id)) {
1154
1155		/* Login failed, just remove the device. */
1156		SBP2_ERR("sbp2_login_device failed");
1157		sbp2_remove_device(hi, scsi_id);
1158		return -EBUSY;
1159	}
1160
1161	/*
1162	 * Set max retries to something large on the device
1163	 */
1164	sbp2_set_busy_timeout(hi, scsi_id);
1165
1166	/*
1167	 * Do a SBP-2 fetch agent reset
1168	 */
1169	sbp2_agent_reset(hi, scsi_id, 0);
1170
1171	/*
1172	 * Get the max speed and packet size that we can use
1173	 */
1174	sbp2_max_speed_and_size(hi, scsi_id);
1175
1176	return 0;
1177}
1178
1179/*
1180 * This function removes an sbp2 device from the sbp2scsi_host_info struct.
1181 */
1182static void sbp2_remove_device(struct sbp2scsi_host_info *hi,
1183			       struct scsi_id_instance_data *scsi_id)
1184{
1185	SBP2_DEBUG("sbp2_remove_device");
1186
1187	/* Complete any pending commands with selection timeout */
1188	sbp2scsi_complete_all_commands(hi, scsi_id, DID_NO_CONNECT);
1189
1190	/* Clean up any other structures */
1191	if (scsi_id->sbp2_total_command_orbs) {
1192		sbp2util_remove_command_orb_pool(scsi_id, hi);
1193	}
1194
1195	if (scsi_id->login_response) {
1196		pci_free_consistent(hi->host->pdev,
1197				    sizeof(struct sbp2_login_response),
1198				    scsi_id->login_response,
1199				    scsi_id->login_response_dma);
1200		SBP2_DMA_FREE("single login FIFO");
1201	}
1202
1203	if (scsi_id->login_orb) {
1204		pci_free_consistent(hi->host->pdev,
1205				    sizeof(struct sbp2_login_orb),
1206				    scsi_id->login_orb,
1207				    scsi_id->login_orb_dma);
1208		SBP2_DMA_FREE("single login ORB");
1209	}
1210
1211	if (scsi_id->reconnect_orb) {
1212		pci_free_consistent(hi->host->pdev,
1213				    sizeof(struct sbp2_reconnect_orb),
1214				    scsi_id->reconnect_orb,
1215				    scsi_id->reconnect_orb_dma);
1216		SBP2_DMA_FREE("single reconnect orb");
1217	}
1218
1219	if (scsi_id->logout_orb) {
1220		pci_free_consistent(hi->host->pdev,
1221				    sizeof(struct sbp2_logout_orb),
1222				    scsi_id->logout_orb,
1223				    scsi_id->reconnect_orb_dma);
1224		SBP2_DMA_FREE("single logout orb");
1225	}
1226
1227	SBP2_DEBUG("SBP-2 device removed, SCSI ID = %d", scsi_id->id);
1228	hi->scsi_id[scsi_id->id] = NULL;
1229	kfree(scsi_id);
1230}
1231
1232#ifdef CONFIG_IEEE1394_SBP2_PHYS_DMA
1233/*
1234 * This function deals with physical dma write requests (for adapters that do not support
1235 * physical dma in hardware). Mostly just here for debugging...
1236 */
1237static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid, int destid, quadlet_t *data,
1238                                     u64 addr, unsigned int length)
1239{
1240
1241        /*
1242         * Manually put the data in the right place.
1243         */
1244        memcpy(bus_to_virt((u32)addr), data, length);
1245	sbp2util_packet_dump(data, length, "sbp2 phys dma write by device", (u32)addr);
1246        return(RCODE_COMPLETE);
1247}
1248
1249/*
1250 * This function deals with physical dma read requests (for adapters that do not support
1251 * physical dma in hardware). Mostly just here for debugging...
1252 */
1253static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid, quadlet_t *data,
1254                                    u64 addr, unsigned int length)
1255{
1256
1257        /*
1258         * Grab data from memory and send a read response.
1259         */
1260        memcpy(data, bus_to_virt((u32)addr), length);
1261	sbp2util_packet_dump(data, length, "sbp2 phys dma read by device", (u32)addr);
1262        return(RCODE_COMPLETE);
1263}
1264#endif
1265
1266
1267/**************************************
1268 * SBP-2 protocol related section
1269 **************************************/
1270
1271/*
1272 * This function determines if we should convert scsi commands for a particular sbp2 device type
1273 */
1274static __inline__ int sbp2_command_conversion_device_type(u8 device_type)
1275{
1276	return (((device_type == TYPE_DISK) ||
1277		 (device_type == TYPE_SDAD) ||
1278		 (device_type == TYPE_ROM)) ? 1:0);
1279}
1280
1281/*
1282 * This function is called in order to login to a particular SBP-2 device,
1283 * after a bus reset.
1284 */
1285static int sbp2_login_device(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id)
1286{
1287	quadlet_t data[2];
1288
1289	SBP2_DEBUG("sbp2_login_device");
1290
1291	if (!scsi_id->login_orb) {
1292		SBP2_DEBUG("sbp2_login_device: login_orb not alloc'd!");
1293		return(-EIO);
1294	}
1295
1296	/* Set-up login ORB, assume no password */
1297	scsi_id->login_orb->password_hi = 0;
1298	scsi_id->login_orb->password_lo = 0;
1299	SBP2_DEBUG("sbp2_login_device: password_hi/lo initialized");
1300
1301	scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
1302	scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1303	SBP2_DEBUG("sbp2_login_device: login_response_hi/lo initialized");
1304
1305	scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(LOGIN_REQUEST);
1306	scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0);	/* One second reconnect time */
1307	scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(sbp2_exclusive_login);	/* Exclusive access to device */
1308	scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1);	/* Notify us of login complete */
1309	/* Set the lun if we were able to pull it from the device's unit directory */
1310	if (scsi_id->sbp2_device_type_and_lun != SBP2_DEVICE_TYPE_LUN_UNINITIALIZED) {
1311		scsi_id->login_orb->lun_misc |= ORB_SET_LUN(scsi_id->sbp2_device_type_and_lun);
1312	}
1313	SBP2_DEBUG("sbp2_login_device: lun_misc initialized");
1314
1315	scsi_id->login_orb->passwd_resp_lengths =
1316		ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
1317	SBP2_DEBUG("sbp2_login_device: passwd_resp_lengths initialized");
1318
1319	scsi_id->login_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
1320					     SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->id);
1321	scsi_id->login_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) |
1322					      SBP2_STATUS_FIFO_ADDRESS_HI);
1323	SBP2_DEBUG("sbp2_login_device: status FIFO initialized");
1324
1325	/*
1326	 * Byte swap ORB if necessary
1327	 */
1328	sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
1329
1330	SBP2_DEBUG("sbp2_login_device: orb byte-swapped");
1331
1332	sbp2util_packet_dump(scsi_id->login_orb, sizeof(struct sbp2_login_orb),
1333			     "sbp2 login orb", scsi_id->login_orb_dma);
1334
1335	/*
1336	 * Initialize login response and status fifo
1337	 */
1338	memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
1339	memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1340
1341	SBP2_DEBUG("sbp2_login_device: login_response/status FIFO memset");
1342
1343	/*
1344	 * Ok, let's write to the target's management agent register
1345	 */
1346	data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1347	data[1] = scsi_id->login_orb_dma;
1348	sbp2util_cpu_to_be32_buffer(data, 8);
1349
1350	atomic_set(&scsi_id->sbp2_login_complete, 0);
1351
1352	SBP2_DEBUG("sbp2_login_device: prepared to write");
1353	hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1354	SBP2_DEBUG("sbp2_login_device: written");
1355
1356	/*
1357	 * Wait for login status (up to 20 seconds)...
1358	 */
1359	if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, 20*HZ)) {
1360		SBP2_ERR("Error logging into SBP-2 device - login timed-out");
1361		return(-EIO);
1362	}
1363
1364	/*
1365	 * Sanity. Make sure status returned matches login orb.
1366	 */
1367	if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
1368		SBP2_ERR("Error logging into SBP-2 device - login timed-out");
1369		return(-EIO);
1370	}
1371
1372	/*
1373	 * Check status
1374	 */
1375	if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1376	    STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1377	    STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1378
1379		SBP2_ERR("Error logging into SBP-2 device - login failed");
1380		return(-EIO);
1381	}
1382
1383	/*
1384	 * Byte swap the login response, for use when reconnecting or
1385	 * logging out.
1386	 */
1387	sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response));
1388
1389	/*
1390	 * Grab our command block agent address from the login response.
1391	 */
1392	SBP2_DEBUG("command_block_agent_hi = %x",
1393		   (unsigned int)scsi_id->login_response->command_block_agent_hi);
1394	SBP2_DEBUG("command_block_agent_lo = %x",
1395		   (unsigned int)scsi_id->login_response->command_block_agent_lo);
1396
1397	scsi_id->sbp2_command_block_agent_addr =
1398		((u64)scsi_id->login_response->command_block_agent_hi) << 32;
1399	scsi_id->sbp2_command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
1400	scsi_id->sbp2_command_block_agent_addr &= 0x0000ffffffffffffULL;
1401
1402	SBP2_INFO("Logged into SBP-2 device");
1403
1404	return(0);
1405
1406}
1407
1408/*
1409 * This function is called in order to logout from a particular SBP-2
1410 * device, usually called during driver unload.
1411 */
1412static int sbp2_logout_device(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id)
1413{
1414	quadlet_t data[2];
1415
1416	SBP2_DEBUG("sbp2_logout_device");
1417
1418	/*
1419	 * Set-up logout ORB
1420	 */
1421	scsi_id->logout_orb->reserved1 = 0x0;
1422	scsi_id->logout_orb->reserved2 = 0x0;
1423	scsi_id->logout_orb->reserved3 = 0x0;
1424	scsi_id->logout_orb->reserved4 = 0x0;
1425
1426	scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(LOGOUT_REQUEST);
1427	scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1428
1429	/* Notify us when complete */
1430	scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1431
1432	scsi_id->logout_orb->reserved5 = 0x0;
1433	scsi_id->logout_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
1434					      SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->id);
1435	scsi_id->logout_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) |
1436					       SBP2_STATUS_FIFO_ADDRESS_HI);
1437
1438	/*
1439	 * Byte swap ORB if necessary
1440	 */
1441	sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb));
1442
1443	sbp2util_packet_dump(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb),
1444			     "sbp2 logout orb", scsi_id->logout_orb_dma);
1445
1446	/*
1447	 * Ok, let's write to the target's management agent register
1448	 */
1449	data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1450	data[1] = scsi_id->logout_orb_dma;
1451	sbp2util_cpu_to_be32_buffer(data, 8);
1452
1453	atomic_set(&scsi_id->sbp2_login_complete, 0);
1454
1455	hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1456
1457	/* Wait for device to logout...1 second. */
1458	sbp2util_down_timeout(&scsi_id->sbp2_login_complete, HZ);
1459
1460	SBP2_INFO("Logged out of SBP-2 device");
1461
1462	return(0);
1463
1464}
1465
1466/*
1467 * This function is called in order to reconnect to a particular SBP-2
1468 * device, after a bus reset.
1469 */
1470static int sbp2_reconnect_device(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id)
1471{
1472	quadlet_t data[2];
1473
1474	SBP2_DEBUG("sbp2_reconnect_device");
1475
1476	/*
1477	 * Set-up reconnect ORB
1478	 */
1479	scsi_id->reconnect_orb->reserved1 = 0x0;
1480	scsi_id->reconnect_orb->reserved2 = 0x0;
1481	scsi_id->reconnect_orb->reserved3 = 0x0;
1482	scsi_id->reconnect_orb->reserved4 = 0x0;
1483
1484	scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(RECONNECT_REQUEST);
1485	scsi_id->reconnect_orb->login_ID_misc |=
1486		ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1487
1488	/* Notify us when complete */
1489	scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1);
1490
1491	scsi_id->reconnect_orb->reserved5 = 0x0;
1492	scsi_id->reconnect_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO +
1493						 SBP2_STATUS_FIFO_ENTRY_TO_OFFSET(scsi_id->id);
1494	scsi_id->reconnect_orb->status_FIFO_hi =
1495		(ORB_SET_NODE_ID(hi->host->node_id) | SBP2_STATUS_FIFO_ADDRESS_HI);
1496
1497	/*
1498	 * Byte swap ORB if necessary
1499	 */
1500	sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
1501
1502	sbp2util_packet_dump(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb),
1503			     "sbp2 reconnect orb", scsi_id->reconnect_orb_dma);
1504
1505	/*
1506	 * Initialize status fifo
1507	 */
1508	memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1509
1510	/*
1511	 * Ok, let's write to the target's management agent register
1512	 */
1513	data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1514	data[1] = scsi_id->reconnect_orb_dma;
1515	sbp2util_cpu_to_be32_buffer(data, 8);
1516
1517	atomic_set(&scsi_id->sbp2_login_complete, 0);
1518
1519	hpsb_node_write(scsi_id->ne, scsi_id->sbp2_management_agent_addr, data, 8);
1520
1521	/*
1522	 * Wait for reconnect status (up to 1 second)...
1523	 */
1524	if (sbp2util_down_timeout(&scsi_id->sbp2_login_complete, HZ)) {
1525		SBP2_ERR("Error reconnecting to SBP-2 device - reconnect timed-out");
1526		return(-EIO);
1527	}
1528
1529	/*
1530	 * Sanity. Make sure status returned matches reconnect orb.
1531	 */
1532	if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
1533		SBP2_ERR("Error reconnecting to SBP-2 device - reconnect timed-out");
1534		return(-EIO);
1535	}
1536
1537	/*
1538	 * Check status
1539	 */
1540	if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1541	    STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1542	    STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1543
1544		SBP2_ERR("Error reconnecting to SBP-2 device - reconnect failed");
1545		return(-EIO);
1546	}
1547
1548	SBP2_INFO("Reconnected to SBP-2 device");
1549
1550	return(0);
1551
1552}
1553
1554/*
1555 * This function is called in order to set the busy timeout (number of
1556 * retries to attempt) on the sbp2 device.
1557 */
1558static int sbp2_set_busy_timeout(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id)
1559{
1560	quadlet_t data;
1561
1562	SBP2_DEBUG("sbp2_set_busy_timeout");
1563
1564	/*
1565	 * Ok, let's write to the target's busy timeout register
1566	 */
1567	data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
1568
1569	if (hpsb_node_write(scsi_id->ne, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4)) {
1570		SBP2_ERR("sbp2_set_busy_timeout error");
1571	}
1572
1573	return(0);
1574}
1575
1576/*
1577 * This function is called to parse sbp2 device's config rom unit
1578 * directory. Used to determine things like sbp2 management agent offset,
1579 * and command set used (SCSI or RBC).
1580 */
1581static void sbp2_parse_unit_directory(struct scsi_id_instance_data *scsi_id)
1582{
1583	struct unit_directory *ud;
1584	int i;
1585
1586	SBP2_DEBUG("sbp2_parse_unit_directory");
1587
1588	/* Initialize some fields, in case an entry does not exist */
1589	scsi_id->sbp2_device_type_and_lun = SBP2_DEVICE_TYPE_LUN_UNINITIALIZED;
1590	scsi_id->sbp2_management_agent_addr = 0x0;
1591	scsi_id->sbp2_command_set_spec_id = 0x0;
1592	scsi_id->sbp2_command_set = 0x0;
1593	scsi_id->sbp2_unit_characteristics = 0x0;
1594	scsi_id->sbp2_firmware_revision = 0x0;
1595
1596	ud = scsi_id->ud;
1597
1598	/* Handle different fields in the unit directory, based on keys */
1599	for (i = 0; i < ud->count; i++) {
1600		switch (CONFIG_ROM_KEY(ud->quadlets[i])) {
1601		case SBP2_CSR_OFFSET_KEY:
1602			/* Save off the management agent address */
1603			scsi_id->sbp2_management_agent_addr =
1604				CSR_REGISTER_BASE +
1605				(CONFIG_ROM_VALUE(ud->quadlets[i]) << 2);
1606
1607			SBP2_DEBUG("sbp2_management_agent_addr = %x",
1608				   (unsigned int) scsi_id->sbp2_management_agent_addr);
1609			break;
1610
1611		case SBP2_COMMAND_SET_SPEC_ID_KEY:
1612			/* Command spec organization */
1613			scsi_id->sbp2_command_set_spec_id
1614				= CONFIG_ROM_VALUE(ud->quadlets[i]);
1615			SBP2_DEBUG("sbp2_command_set_spec_id = %x",
1616				   (unsigned int) scsi_id->sbp2_command_set_spec_id);
1617			break;
1618
1619		case SBP2_COMMAND_SET_KEY:
1620			/* Command set used by sbp2 device */
1621			scsi_id->sbp2_command_set
1622				= CONFIG_ROM_VALUE(ud->quadlets[i]);
1623			SBP2_DEBUG("sbp2_command_set = %x",
1624				   (unsigned int) scsi_id->sbp2_command_set);
1625			break;
1626
1627		case SBP2_UNIT_CHARACTERISTICS_KEY:
1628			/*
1629			 * Unit characterisitcs (orb related stuff
1630			 * that I'm not yet paying attention to)
1631			 */
1632			scsi_id->sbp2_unit_characteristics
1633				= CONFIG_ROM_VALUE(ud->quadlets[i]);
1634			SBP2_DEBUG("sbp2_unit_characteristics = %x",
1635				   (unsigned int) scsi_id->sbp2_unit_characteristics);
1636			break;
1637
1638		case SBP2_DEVICE_TYPE_AND_LUN_KEY:
1639			/*
1640			 * Device type and lun (used for
1641			 * detemining type of sbp2 device)
1642			 */
1643			scsi_id->sbp2_device_type_and_lun
1644				= CONFIG_ROM_VALUE(ud->quadlets[i]);
1645			SBP2_DEBUG("sbp2_device_type_and_lun = %x",
1646				   (unsigned int) scsi_id->sbp2_device_type_and_lun);
1647			break;
1648
1649		case SBP2_FIRMWARE_REVISION_KEY:
1650			/* Firmware revision */
1651			scsi_id->sbp2_firmware_revision
1652				= CONFIG_ROM_VALUE(ud->quadlets[i]);
1653			if (sbp2_force_inquiry_hack)
1654				SBP2_INFO("sbp2_firmware_revision = %x",
1655				   (unsigned int) scsi_id->sbp2_firmware_revision);
1656			else	SBP2_DEBUG("sbp2_firmware_revision = %x",
1657				   (unsigned int) scsi_id->sbp2_firmware_revision);
1658			break;
1659
1660		default:
1661			break;
1662		}
1663	}
1664
1665	/* This is the start of our broken device checking. We try to hack
1666	 * around oddities and known defects.  */
1667	scsi_id->workarounds = 0x0;
1668
1669	/* If the vendor id is 0xa0b8 (Symbios vendor id), then we have a
1670	 * bridge with 128KB max transfer size limitation. For sanity, we
1671	 * only voice this when the current sbp2_max_sectors setting
1672	 * exceeds the 128k limit. By default, that is not the case.
1673	 *
1674	 * It would be really nice if we could detect this before the scsi
1675	 * host gets initialized. That way we can down-force the
1676	 * sbp2_max_sectors to account for it. That is not currently
1677	 * possible.  */
1678	if ((scsi_id->sbp2_firmware_revision & 0xffff00) ==
1679			SBP2_128KB_BROKEN_FIRMWARE &&
1680			(sbp2_max_sectors * 512) > (128 * 1024)) {
1681		SBP2_WARN("Node " NODE_BUS_FMT ": Bridge only supports 128KB max transfer size.",
1682				NODE_BUS_ARGS(scsi_id->ne->nodeid));
1683		SBP2_WARN("WARNING: Current sbp2_max_sectors setting is larger than 128KB (%d sectors)!",
1684				sbp2_max_sectors);
1685		scsi_id->workarounds |= SBP2_BREAKAGE_128K_MAX_TRANSFER;
1686	}
1687
1688	/* Check for a blacklisted set of devices that require us to force
1689	 * a 36 byte host inquiry. This can be overriden as a module param
1690	 * (to force all hosts).  */
1691	for (i = 0; i < NUM_BROKEN_INQUIRY_DEVS; i++) {
1692		if ((scsi_id->sbp2_firmware_revision & 0xffff00) ==
1693				sbp2_broken_inquiry_list[i]) {
1694			SBP2_WARN("Node " NODE_BUS_FMT ": Using 36byte inquiry workaround",
1695					NODE_BUS_ARGS(scsi_id->ne->nodeid));
1696			scsi_id->workarounds |= SBP2_BREAKAGE_INQUIRY_HACK;
1697			break; /* No need to continue. */
1698		}
1699	}
1700}
1701
1702/*
1703 * This function is called in order to determine the max speed and packet
1704 * size we can use in our ORBs. Note, that we (the driver and host) only
1705 * initiate the transaction. The SBP-2 device actually transfers the data
1706 * (by reading from the DMA area we tell it). This means that the SBP-2
1707 * device decides the actual maximum data it can transfer. We just tell it
1708 * the speed that it needs to use, and the max_rec the host supports, and
1709 * it takes care of the rest.
1710 */
1711static int sbp2_max_speed_and_size(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id)
1712{
1713	SBP2_DEBUG("sbp2_max_speed_and_size");
1714
1715	/* Initial setting comes from the hosts speed map */
1716	scsi_id->speed_code = hi->host->speed_map[(hi->host->node_id & NODE_MASK) * 64
1717						  + (scsi_id->ne->nodeid & NODE_MASK)];
1718
1719	/* Bump down our speed if the user requested it */
1720	if (scsi_id->speed_code > sbp2_max_speed) {
1721		scsi_id->speed_code = sbp2_max_speed;
1722		SBP2_ERR("Forcing SBP-2 max speed down to %s",
1723			 hpsb_speedto_str[scsi_id->speed_code]);
1724	}
1725
1726	/* Payload size is the lesser of what our speed supports and what
1727	 * our host supports.  */
1728	scsi_id->max_payload_size = min(sbp2_speedto_maxrec[scsi_id->speed_code],
1729					(u8)(((be32_to_cpu(hi->host->csr.rom[2]) >> 12) & 0xf) - 1));
1730
1731	SBP2_ERR("Node[" NODE_BUS_FMT "]: Max speed [%s] - Max payload [%u]",
1732		 NODE_BUS_ARGS(scsi_id->ne->nodeid), hpsb_speedto_str[scsi_id->speed_code],
1733		 1 << ((u32)scsi_id->max_payload_size + 2));
1734
1735	return(0);
1736}
1737
1738/*
1739 * This function is called in order to perform a SBP-2 agent reset.
1740 */
1741static int sbp2_agent_reset(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id, u32 flags)
1742{
1743	struct sbp2_request_packet *agent_reset_request_packet;
1744
1745	SBP2_DEBUG("sbp2_agent_reset");
1746
1747	/*
1748	 * Ok, let's write to the target's management agent register
1749	 */
1750	agent_reset_request_packet =
1751		sbp2util_allocate_write_request_packet(hi, scsi_id->ne,
1752						       scsi_id->sbp2_command_block_agent_addr +
1753						       SBP2_AGENT_RESET_OFFSET,
1754						       0, ntohl(SBP2_AGENT_RESET_DATA));
1755
1756	if (!agent_reset_request_packet) {
1757		SBP2_ERR("sbp2util_allocate_write_request_packet failed");
1758		return(-EIO);
1759	}
1760
1761	if (!hpsb_send_packet(agent_reset_request_packet->packet)) {
1762		SBP2_ERR("hpsb_send_packet failed");
1763		sbp2util_free_request_packet(agent_reset_request_packet);
1764		return(-EIO);
1765	}
1766
1767	if (!(flags & SBP2_SEND_NO_WAIT)) {
1768		down(&agent_reset_request_packet->packet->state_change);
1769		down(&agent_reset_request_packet->packet->state_change);
1770	}
1771
1772	/*
1773	 * Need to make sure orb pointer is written on next command
1774	 */
1775	scsi_id->last_orb = NULL;
1776
1777	return(0);
1778
1779}
1780
1781/*
1782 * This function is called to create the actual command orb and s/g list
1783 * out of the scsi command itself.
1784 */
1785static int sbp2_create_command_orb(struct sbp2scsi_host_info *hi,
1786				   struct scsi_id_instance_data *scsi_id,
1787				   struct sbp2_command_info *command,
1788				   unchar *scsi_cmd,
1789				   unsigned int scsi_use_sg,
1790				   unsigned int scsi_request_bufflen,
1791				   void *scsi_request_buffer,
1792				   unsigned char scsi_dir)
1793{
1794	struct scatterlist *sgpnt = (struct scatterlist *) scsi_request_buffer;
1795	struct sbp2_command_orb *command_orb = &command->command_orb;
1796	struct sbp2_unrestricted_page_table *scatter_gather_element =
1797		&command->scatter_gather_element[0];
1798	int dma_dir = scsi_to_pci_dma_dir (scsi_dir);
1799	u32 sg_count, sg_len, orb_direction;
1800	dma_addr_t sg_addr;
1801	int i;
1802
1803	/*
1804	 * Set-up our command ORB..
1805	 *
1806	 * NOTE: We're doing unrestricted page tables (s/g), as this is
1807	 * best performance (at least with the devices I have). This means
1808	 * that data_size becomes the number of s/g elements, and
1809	 * page_size should be zero (for unrestricted).
1810	 */
1811	command_orb->next_ORB_hi = ORB_SET_NULL_PTR(1);
1812	command_orb->next_ORB_lo = 0x0;
1813	command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size);
1814	command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code);
1815	command_orb->misc |= ORB_SET_NOTIFY(1);		/* Notify us when complete */
1816
1817	/*
1818	 * Get the direction of the transfer. If the direction is unknown, then use our
1819	 * goofy table as a back-up.
1820	 */
1821	switch (scsi_dir) {
1822		case SCSI_DATA_NONE:
1823			orb_direction = ORB_DIRECTION_NO_DATA_TRANSFER;
1824			break;
1825		case SCSI_DATA_WRITE:
1826			orb_direction = ORB_DIRECTION_WRITE_TO_MEDIA;
1827			break;
1828		case SCSI_DATA_READ:
1829			orb_direction = ORB_DIRECTION_READ_FROM_MEDIA;
1830			break;
1831		case SCSI_DATA_UNKNOWN:
1832		default:
1833			SBP2_ERR("SCSI data transfer direction not specified. "
1834				 "Update the SBP2 direction table in sbp2.h if "
1835				 "necessary for your application");
1836			print_command (scsi_cmd);
1837			orb_direction = sbp2scsi_direction_table[*scsi_cmd];
1838			break;
1839	}
1840
1841	/*
1842	 * Set-up our pagetable stuff... unfortunately, this has become
1843	 * messier than I'd like. Need to clean this up a bit.   ;-)
1844	 */
1845	if (orb_direction == ORB_DIRECTION_NO_DATA_TRANSFER) {
1846
1847		SBP2_DEBUG("No data transfer");
1848
1849		/*
1850		 * Handle no data transfer
1851		 */
1852		command_orb->data_descriptor_hi = 0x0;
1853		command_orb->data_descriptor_lo = 0x0;
1854		command_orb->misc |= ORB_SET_DIRECTION(1);
1855
1856	} else if (scsi_use_sg) {
1857
1858		SBP2_DEBUG("Use scatter/gather");
1859
1860		/*
1861		 * Special case if only one element (and less than 64KB in size)
1862		 */
1863		if ((scsi_use_sg == 1) && (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
1864
1865			SBP2_DEBUG("Only one s/g element");
1866			command->dma_dir = dma_dir;
1867			command->dma_size = sgpnt[0].length;
1868			command->dma_type = CMD_DMA_PAGE;
1869#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,13)
1870			command->cmd_dma = pci_map_single (hi->host->pdev, sgpnt[0].address,
1871							   command->dma_size,
1872							   command->dma_dir);
1873#else
1874			command->cmd_dma = pci_map_page(hi->host->pdev,
1875							sgpnt[0].page,
1876							sgpnt[0].offset,
1877							command->dma_size,
1878							command->dma_dir);
1879#endif /* Linux version < 2.4.13 */
1880			SBP2_DMA_ALLOC("single page scatter element");
1881
1882			command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1883			command_orb->data_descriptor_lo = command->cmd_dma;
1884			command_orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
1885			command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1886
1887		} else {
1888			int count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg, dma_dir);
1889			SBP2_DMA_ALLOC("scatter list");
1890
1891			command->dma_size = scsi_use_sg;
1892			command->dma_dir = dma_dir;
1893			command->sge_buffer = sgpnt;
1894
1895			/* use page tables (s/g) */
1896			command_orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1897			command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1898			command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1899			command_orb->data_descriptor_lo = command->sge_dma;
1900
1901			/*
1902			 * Loop through and fill out our sbp-2 page tables
1903			 * (and split up anything too large)
1904			 */
1905			for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
1906				sg_len = sg_dma_len(sgpnt);
1907				sg_addr = sg_dma_address(sgpnt);
1908				while (sg_len) {
1909					scatter_gather_element[sg_count].segment_base_lo = sg_addr;
1910					if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1911						scatter_gather_element[sg_count].length_segment_base_hi =
1912							PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1913						sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1914						sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1915					} else {
1916						scatter_gather_element[sg_count].length_segment_base_hi =
1917							PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
1918						sg_len = 0;
1919					}
1920					sg_count++;
1921				}
1922			}
1923
1924			/* Number of page table (s/g) elements */
1925			command_orb->misc |= ORB_SET_DATA_SIZE(sg_count);
1926
1927			sbp2util_packet_dump(scatter_gather_element,
1928					     (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
1929					     "sbp2 s/g list", command->sge_dma);
1930
1931			/*
1932			 * Byte swap page tables if necessary
1933			 */
1934			sbp2util_cpu_to_be32_buffer(scatter_gather_element,
1935						    (sizeof(struct sbp2_unrestricted_page_table)) *
1936						    sg_count);
1937
1938		}
1939
1940	} else {
1941
1942		SBP2_DEBUG("No scatter/gather");
1943
1944		command->dma_dir = dma_dir;
1945		command->dma_size = scsi_request_bufflen;
1946		command->dma_type = CMD_DMA_SINGLE;
1947		command->cmd_dma = pci_map_single (hi->host->pdev, scsi_request_buffer,
1948						   command->dma_size,
1949						   command->dma_dir);
1950		SBP2_DMA_ALLOC("single bulk");
1951
1952		/*
1953		 * Handle case where we get a command w/o s/g enabled (but
1954		 * check for transfers larger than 64K)
1955		 */
1956		if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
1957
1958			command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1959			command_orb->data_descriptor_lo = command->cmd_dma;
1960			command_orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
1961			command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1962
1963			/*
1964			 * Sanity, in case our direction table is not
1965			 * up-to-date
1966			 */
1967			if (!scsi_request_bufflen) {
1968				command_orb->data_descriptor_hi = 0x0;
1969				command_orb->data_descriptor_lo = 0x0;
1970				command_orb->misc |= ORB_SET_DIRECTION(1);
1971			}
1972
1973		} else {
1974			/*
1975			 * Need to turn this into page tables, since the
1976			 * buffer is too large.
1977			 */
1978			command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
1979			command_orb->data_descriptor_lo = command->sge_dma;
1980
1981			/* Use page tables (s/g) */
1982			command_orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
1983			command_orb->misc |= ORB_SET_DIRECTION(orb_direction);
1984
1985			/*
1986			 * fill out our sbp-2 page tables (and split up
1987			 * the large buffer)
1988			 */
1989			sg_count = 0;
1990			sg_len = scsi_request_bufflen;
1991			sg_addr = command->cmd_dma;
1992			while (sg_len) {
1993				scatter_gather_element[sg_count].segment_base_lo = sg_addr;
1994				if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
1995					scatter_gather_element[sg_count].length_segment_base_hi =
1996						PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
1997					sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
1998					sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
1999				} else {
2000					scatter_gather_element[sg_count].length_segment_base_hi =
2001						PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
2002					sg_len = 0;
2003				}
2004				sg_count++;
2005			}
2006
2007			/* Number of page table (s/g) elements */
2008			command_orb->misc |= ORB_SET_DATA_SIZE(sg_count);
2009
2010			sbp2util_packet_dump(scatter_gather_element,
2011					     (sizeof(struct sbp2_unrestricted_page_table)) * sg_count,
2012					     "sbp2 s/g list", command->sge_dma);
2013
2014			/*
2015			 * Byte swap page tables if necessary
2016			 */
2017			sbp2util_cpu_to_be32_buffer(scatter_gather_element,
2018						    (sizeof(struct sbp2_unrestricted_page_table)) *
2019						     sg_count);
2020
2021		}
2022
2023	}
2024
2025	/*
2026	 * Byte swap command ORB if necessary
2027	 */
2028	sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
2029
2030	/*
2031	 * Put our scsi command in the command ORB
2032	 */
2033	memset(command_orb->cdb, 0, 12);
2034	memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
2035
2036	return(0);
2037}
2038
2039/*
2040 * This function is called in order to begin a regular SBP-2 command.
2041 */
2042static int sbp2_link_orb_command(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id,
2043				 struct sbp2_command_info *command)
2044{
2045        struct sbp2_request_packet *command_request_packet;
2046	struct sbp2_command_orb *command_orb = &command->command_orb;
2047
2048	outstanding_orb_incr;
2049	SBP2_ORB_DEBUG("sending command orb %p, total orbs = %x",
2050			command_orb, global_outstanding_command_orbs);
2051
2052	pci_dma_sync_single(hi->host->pdev, command->command_orb_dma,
2053			    sizeof(struct sbp2_command_orb),
2054			    PCI_DMA_BIDIRECTIONAL);
2055	pci_dma_sync_single(hi->host->pdev, command->sge_dma,
2056			    sizeof(command->scatter_gather_element),
2057			    PCI_DMA_BIDIRECTIONAL);
2058	/*
2059	 * Check to see if there are any previous orbs to use
2060	 */
2061	if (scsi_id->last_orb == NULL) {
2062
2063		/*
2064		 * Ok, let's write to the target's management agent register
2065		 */
2066		if (hpsb_node_entry_valid(scsi_id->ne)) {
2067
2068			command_request_packet =
2069				sbp2util_allocate_write_request_packet(hi, scsi_id->ne,
2070								scsi_id->sbp2_command_block_agent_addr +
2071								SBP2_ORB_POINTER_OFFSET, 8, 0);
2072
2073			if (!command_request_packet) {
2074				SBP2_ERR("sbp2util_allocate_write_request_packet failed");
2075				return(-EIO);
2076			}
2077
2078			command_request_packet->packet->data[0] = ORB_SET_NODE_ID(hi->host->node_id);
2079			command_request_packet->packet->data[1] = command->command_orb_dma;
2080			sbp2util_cpu_to_be32_buffer(command_request_packet->packet->data, 8);
2081
2082			SBP2_ORB_DEBUG("write command agent, command orb %p", command_orb);
2083
2084			if (!hpsb_send_packet(command_request_packet->packet)) {
2085				SBP2_ERR("hpsb_send_packet failed");
2086				sbp2util_free_request_packet(command_request_packet);
2087				return(-EIO);
2088			}
2089
2090			SBP2_ORB_DEBUG("write command agent complete");
2091		}
2092
2093		scsi_id->last_orb = command_orb;
2094		scsi_id->last_orb_dma = command->command_orb_dma;
2095
2096	} else {
2097
2098		/*
2099		 * We have an orb already sent (maybe or maybe not
2100		 * processed) that we can append this orb to. So do so,
2101		 * and ring the doorbell. Have to be very careful
2102		 * modifying these next orb pointers, as they are accessed
2103		 * both by the sbp2 device and us.
2104		 */
2105		scsi_id->last_orb->next_ORB_lo =
2106			cpu_to_be32(command->command_orb_dma);
2107		/* Tells hardware that this pointer is valid */
2108		scsi_id->last_orb->next_ORB_hi = 0x0;
2109		pci_dma_sync_single(hi->host->pdev, scsi_id->last_orb_dma,
2110				    sizeof(struct sbp2_command_orb),
2111				    PCI_DMA_BIDIRECTIONAL);
2112
2113		/*
2114		 * Ring the doorbell
2115		 */
2116		if (hpsb_node_entry_valid(scsi_id->ne)) {
2117
2118			command_request_packet = sbp2util_allocate_write_request_packet(hi,
2119				scsi_id->ne,
2120				scsi_id->sbp2_command_block_agent_addr + SBP2_DOORBELL_OFFSET,
2121				0, cpu_to_be32(command->command_orb_dma));
2122
2123			if (!command_request_packet) {
2124				SBP2_ERR("sbp2util_allocate_write_request_packet failed");
2125				return(-EIO);
2126			}
2127
2128			SBP2_ORB_DEBUG("ring doorbell, command orb %p", command_orb);
2129
2130			if (!hpsb_send_packet(command_request_packet->packet)) {
2131				SBP2_ERR("hpsb_send_packet failed");
2132				sbp2util_free_request_packet(command_request_packet);
2133				return(-EIO);
2134			}
2135		}
2136
2137		scsi_id->last_orb = command_orb;
2138		scsi_id->last_orb_dma = command->command_orb_dma;
2139
2140	}
2141       	return(0);
2142}
2143
2144/*
2145 * This function is called in order to begin a regular SBP-2 command.
2146 */
2147static int sbp2_send_command(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id,
2148			     Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
2149{
2150	unchar *cmd = (unchar *) SCpnt->cmnd;
2151	unsigned int request_bufflen = SCpnt->request_bufflen;
2152	struct sbp2_command_info *command;
2153
2154	SBP2_DEBUG("sbp2_send_command");
2155#if (CONFIG_IEEE1394_SBP2_DEBUG >= 2) || defined(CONFIG_IEEE1394_SBP2_PACKET_DUMP)
2156	printk("[scsi command]\n   ");
2157	print_command (cmd);
2158#endif
2159	SBP2_DEBUG("SCSI transfer size = %x", request_bufflen);
2160	SBP2_DEBUG("SCSI s/g elements = %x", (unsigned int)SCpnt->use_sg);
2161
2162	/*
2163	 * Allocate a command orb and s/g structure
2164	 */
2165	command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done, hi);
2166	if (!command) {
2167		return(-EIO);
2168	}
2169
2170	/*
2171	 * The scsi stack sends down a request_bufflen which does not match the
2172	 * length field in the scsi cdb. This causes some sbp2 devices to
2173	 * reject this inquiry command. Fix the request_bufflen.
2174	 */
2175	if (*cmd == INQUIRY) {
2176		if (sbp2_force_inquiry_hack || scsi_id->workarounds & SBP2_BREAKAGE_INQUIRY_HACK)
2177			request_bufflen = cmd[4] = 0x24;
2178		else
2179			request_bufflen = cmd[4];
2180	}
2181
2182	/*
2183	 * Now actually fill in the comamnd orb and sbp2 s/g list
2184	 */
2185	sbp2_create_command_orb(hi, scsi_id, command, cmd, SCpnt->use_sg,
2186				request_bufflen, SCpnt->request_buffer,
2187				SCpnt->sc_data_direction);
2188	/*
2189	 * Update our cdb if necessary (to handle sbp2 RBC command set
2190	 * differences). This is where the command set hacks go!   =)
2191	 */
2192	sbp2_check_sbp2_command(scsi_id, command->command_orb.cdb);
2193
2194	sbp2util_packet_dump(&command->command_orb, sizeof(struct sbp2_command_orb),
2195			     "sbp2 command orb", command->command_orb_dma);
2196
2197	/*
2198	 * Initialize status fifo
2199	 */
2200	memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
2201
2202	/*
2203	 * Link up the orb, and ring the doorbell if needed
2204	 */
2205	sbp2_link_orb_command(hi, scsi_id, command);
2206
2207	return(0);
2208}
2209
2210
2211/*
2212 * This function deals with command set differences between Linux scsi
2213 * command set and sbp2 RBC command set.
2214 */
2215static void sbp2_check_sbp2_command(struct scsi_id_instance_data *scsi_id, unchar *cmd)
2216{
2217	unchar new_cmd[16];
2218	u8 device_type = SBP2_DEVICE_TYPE (scsi_id->sbp2_device_type_and_lun);
2219
2220	SBP2_DEBUG("sbp2_check_sbp2_command");
2221
2222	switch (*cmd) {
2223
2224		case READ_6:
2225
2226			if (sbp2_command_conversion_device_type(device_type)) {
2227
2228				SBP2_DEBUG("Convert READ_6 to READ_10");
2229
2230				/*
2231				 * Need to turn read_6 into read_10
2232				 */
2233				new_cmd[0] = 0x28;
2234				new_cmd[1] = (cmd[1] & 0xe0);
2235				new_cmd[2] = 0x0;
2236				new_cmd[3] = (cmd[1] & 0x1f);
2237				new_cmd[4] = cmd[2];
2238				new_cmd[5] = cmd[3];
2239				new_cmd[6] = 0x0;
2240				new_cmd[7] = 0x0;
2241				new_cmd[8] = cmd[4];
2242				new_cmd[9] = cmd[5];
2243
2244				memcpy(cmd, new_cmd, 10);
2245
2246			}
2247
2248			break;
2249
2250		case WRITE_6:
2251
2252			if (sbp2_command_conversion_device_type(device_type)) {
2253
2254				SBP2_DEBUG("Convert WRITE_6 to WRITE_10");
2255
2256				/*
2257				 * Need to turn write_6 into write_10
2258				 */
2259				new_cmd[0] = 0x2a;
2260				new_cmd[1] = (cmd[1] & 0xe0);
2261				new_cmd[2] = 0x0;
2262				new_cmd[3] = (cmd[1] & 0x1f);
2263				new_cmd[4] = cmd[2];
2264				new_cmd[5] = cmd[3];
2265				new_cmd[6] = 0x0;
2266				new_cmd[7] = 0x0;
2267				new_cmd[8] = cmd[4];
2268				new_cmd[9] = cmd[5];
2269
2270				memcpy(cmd, new_cmd, 10);
2271
2272			}
2273
2274			break;
2275
2276		case MODE_SENSE:
2277
2278			if (sbp2_command_conversion_device_type(device_type)) {
2279
2280				SBP2_DEBUG("Convert MODE_SENSE_6 to MODE_SENSE_10");
2281
2282				/*
2283				 * Need to turn mode_sense_6 into mode_sense_10
2284				 */
2285				new_cmd[0] = 0x5a;
2286				new_cmd[1] = cmd[1];
2287				new_cmd[2] = cmd[2];
2288				new_cmd[3] = 0x0;
2289				new_cmd[4] = 0x0;
2290				new_cmd[5] = 0x0;
2291				new_cmd[6] = 0x0;
2292				new_cmd[7] = 0x0;
2293				new_cmd[8] = cmd[4];
2294				new_cmd[9] = cmd[5];
2295
2296				memcpy(cmd, new_cmd, 10);
2297
2298			}
2299
2300			break;
2301
2302		case MODE_SELECT:
2303
2304			/*
2305			 * TODO. Probably need to change mode select to 10 byte version
2306			 */
2307
2308		default:
2309			break;
2310	}
2311
2312	return;
2313}
2314
2315/*
2316 * Translates SBP-2 status into SCSI sense data for check conditions
2317 */
2318static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
2319{
2320	SBP2_DEBUG("sbp2_status_to_sense_data");
2321
2322	/*
2323	 * Ok, it's pretty ugly...   ;-)
2324	 */
2325	sense_data[0] = 0x70;
2326	sense_data[1] = 0x0;
2327	sense_data[2] = sbp2_status[9];
2328	sense_data[3] = sbp2_status[12];
2329	sense_data[4] = sbp2_status[13];
2330	sense_data[5] = sbp2_status[14];
2331	sense_data[6] = sbp2_status[15];
2332	sense_data[7] = 10;
2333	sense_data[8] = sbp2_status[16];
2334	sense_data[9] = sbp2_status[17];
2335	sense_data[10] = sbp2_status[18];
2336	sense_data[11] = sbp2_status[19];
2337	sense_data[12] = sbp2_status[10];
2338	sense_data[13] = sbp2_status[11];
2339	sense_data[14] = sbp2_status[20];
2340	sense_data[15] = sbp2_status[21];
2341
2342	return(sbp2_status[8] & 0x3f);	/* return scsi status */
2343}
2344
2345/*
2346 * This function is called after a command is completed, in order to do any necessary SBP-2
2347 * response data translations for the SCSI stack
2348 */
2349static void sbp2_check_sbp2_response(struct scsi_id_instance_data *scsi_id,
2350				     Scsi_Cmnd *SCpnt)
2351{
2352	u8 *scsi_buf = SCpnt->request_buffer;
2353	u8 device_type = SBP2_DEVICE_TYPE (scsi_id->sbp2_device_type_and_lun);
2354
2355	SBP2_DEBUG("sbp2_check_sbp2_response");
2356
2357	switch (SCpnt->cmnd[0]) {
2358
2359		case INQUIRY:
2360
2361			/*
2362			 * If scsi_id->sbp2_device_type_and_lun is uninitialized, then fill
2363			 * this information in from the inquiry response data. Lun is set to zero.
2364			 */
2365			if (scsi_id->sbp2_device_type_and_lun == SBP2_DEVICE_TYPE_LUN_UNINITIALIZED) {
2366				SBP2_DEBUG("Creating sbp2_device_type_and_lun from scsi inquiry data");
2367				scsi_id->sbp2_device_type_and_lun = (scsi_buf[0] & 0x1f) << 16;
2368			}
2369
2370			/*
2371			 * Make sure data length is ok. Minimum length is 36 bytes
2372			 */
2373			if (scsi_buf[4] == 0) {
2374				scsi_buf[4] = 36 - 5;
2375			}
2376
2377			/*
2378			 * Check for Simple Direct Access Device and change it to TYPE_DISK
2379			 */
2380			if ((scsi_buf[0] & 0x1f) == TYPE_SDAD) {
2381				SBP2_DEBUG("Changing TYPE_SDAD to TYPE_DISK");
2382				scsi_buf[0] &= 0xe0;
2383			}
2384
2385			/*
2386			 * Fix ansi revision and response data format
2387			 */
2388			scsi_buf[2] |= 2;
2389			scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2;
2390
2391			break;
2392
2393		case MODE_SENSE:
2394
2395			if (sbp2_command_conversion_device_type(device_type)) {
2396
2397				SBP2_DEBUG("Modify mode sense response (10 byte version)");
2398
2399				scsi_buf[0] = scsi_buf[1];	/* Mode data length */
2400				scsi_buf[1] = scsi_buf[2];	/* Medium type */
2401				scsi_buf[2] = scsi_buf[3];	/* Device specific parameter */
2402				scsi_buf[3] = scsi_buf[7];	/* Block descriptor length */
2403				memcpy(scsi_buf + 4, scsi_buf + 8, scsi_buf[0]);
2404
2405			}
2406
2407			break;
2408
2409		case MODE_SELECT:
2410
2411			/*
2412			 * TODO. Probably need to change mode select to 10 byte version
2413			 */
2414
2415		default:
2416			break;
2417	}
2418	return;
2419}
2420
2421/*
2422 * This function deals with status writes from the SBP-2 device
2423 */
2424static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid, int destid,
2425				    quadlet_t *data, u64 addr, unsigned int length)
2426{
2427	struct sbp2scsi_host_info *hi = NULL;
2428	struct scsi_id_instance_data *scsi_id = NULL;
2429	u32 id;
2430	unsigned long flags;
2431	Scsi_Cmnd *SCpnt = NULL;
2432	u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
2433	struct sbp2_command_info *command;
2434
2435	SBP2_DEBUG("sbp2_handle_status_write");
2436
2437	sbp2util_packet_dump(data, length, "sbp2 status write by device", (u32)addr);
2438
2439	if (!host) {
2440		SBP2_ERR("host is NULL - this is bad!");
2441		return(RCODE_ADDRESS_ERROR);
2442	}
2443
2444	sbp2_spin_lock(&sbp2_host_info_lock, flags);
2445	hi = sbp2_find_host_info(host);
2446	sbp2_spin_unlock(&sbp2_host_info_lock, flags);
2447
2448	if (!hi) {
2449		SBP2_ERR("host info is NULL - this is bad!");
2450		return(RCODE_ADDRESS_ERROR);
2451	}
2452
2453	sbp2_spin_lock(&hi->sbp2_command_lock, flags);
2454
2455	/*
2456	 * Find our scsi_id structure by looking at the status fifo address written to by
2457	 * the sbp2 device.
2458	 */
2459	id = SBP2_STATUS_FIFO_OFFSET_TO_ENTRY((u32)(addr - SBP2_STATUS_FIFO_ADDRESS));
2460	scsi_id = hi->scsi_id[id];
2461
2462	if (!scsi_id) {
2463		SBP2_ERR("scsi_id is NULL - device is gone?");
2464		sbp2_spin_unlock(&hi->sbp2_command_lock, flags);
2465		return(RCODE_ADDRESS_ERROR);
2466	}
2467
2468	/*
2469	 * Put response into scsi_id status fifo...
2470	 */
2471	memcpy(&scsi_id->status_block, data, length);
2472
2473	/*
2474	 * Byte swap first two quadlets (8 bytes) of status for processing
2475	 */
2476	sbp2util_be32_to_cpu_buffer(&scsi_id->status_block, 8);
2477
2478	/*
2479	 * Handle command ORB status here if necessary. First, need to match status with command.
2480	 */
2481	command = sbp2util_find_command_for_orb(scsi_id, scsi_id->status_block.ORB_offset_lo);
2482	if (command) {
2483
2484		SBP2_DEBUG("Found status for command ORB");
2485		pci_dma_sync_single(hi->host->pdev, command->command_orb_dma,
2486				    sizeof(struct sbp2_command_orb),
2487				    PCI_DMA_BIDIRECTIONAL);
2488		pci_dma_sync_single(hi->host->pdev, command->sge_dma,
2489				    sizeof(command->scatter_gather_element),
2490				    PCI_DMA_BIDIRECTIONAL);
2491
2492		SBP2_ORB_DEBUG("matched command orb %p", &command->command_orb);
2493		outstanding_orb_decr;
2494
2495		/*
2496		 * Matched status with command, now grab scsi command pointers and check status
2497		 */
2498		SCpnt = command->Current_SCpnt;
2499		sbp2util_mark_command_completed(scsi_id, command);
2500
2501		if (SCpnt) {
2502
2503			/*
2504			 * See if the target stored any scsi status information
2505			 */
2506			if (STATUS_GET_LENGTH(scsi_id->status_block.ORB_offset_hi_misc) > 1) {
2507				/*
2508				 * Translate SBP-2 status to SCSI sense data
2509				 */
2510				SBP2_DEBUG("CHECK CONDITION");
2511				scsi_status = sbp2_status_to_sense_data((unchar *)&scsi_id->status_block, SCpnt->sense_buffer);
2512			}
2513
2514			/*
2515			 * Check to see if the dead bit is set. If so, we'll have to initiate
2516			 * a fetch agent reset.
2517			 */
2518			if (STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc)) {
2519
2520				/*
2521				 * Initiate a fetch agent reset.
2522				 */
2523				SBP2_DEBUG("Dead bit set - initiating fetch agent reset");
2524                                sbp2_agent_reset(hi, scsi_id, SBP2_SEND_NO_WAIT);
2525			}
2526
2527			SBP2_ORB_DEBUG("completing command orb %p", &command->command_orb);
2528
2529			/*
2530			 * Complete the SCSI command
2531			 */
2532			SBP2_DEBUG("Completing SCSI command");
2533			sbp2scsi_complete_command(hi, scsi_id, scsi_status, SCpnt, command->Current_done);
2534			SBP2_ORB_DEBUG("command orb completed");
2535		}
2536
2537		/*
2538		 * Check here to see if there are no commands in-use. If there are none, we can
2539		 * null out last orb so that next time around we write directly to the orb pointer...
2540		 * Quick start saves one 1394 bus transaction.
2541		 */
2542		if (list_empty(&scsi_id->sbp2_command_orb_inuse)) {
2543			scsi_id->last_orb = NULL;
2544		}
2545
2546	} else {
2547
2548		/*
2549		 * It's probably a login/logout/reconnect status.
2550		 */
2551		if ((scsi_id->login_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2552		    (scsi_id->reconnect_orb_dma == scsi_id->status_block.ORB_offset_lo) ||
2553		    (scsi_id->logout_orb_dma == scsi_id->status_block.ORB_offset_lo)) {
2554			atomic_set(&scsi_id->sbp2_login_complete, 1);
2555		}
2556	}
2557
2558	sbp2_spin_unlock(&hi->sbp2_command_lock, flags);
2559	return(RCODE_COMPLETE);
2560}
2561
2562
2563/**************************************
2564 * SCSI interface related section
2565 **************************************/
2566
2567/*
2568 * This routine is the main request entry routine for doing I/O. It is
2569 * called from the scsi stack directly.
2570 */
2571static int sbp2scsi_queuecommand (Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
2572{
2573	struct sbp2scsi_host_info *hi = NULL;
2574	struct scsi_id_instance_data *scsi_id = NULL;
2575	unsigned long flags;
2576
2577	SBP2_DEBUG("sbp2scsi_queuecommand");
2578
2579	/*
2580	 * Pull our host info and scsi id instance data from the scsi command
2581	 */
2582	hi = (struct sbp2scsi_host_info *) SCpnt->host->hostdata[0];
2583
2584	if (!hi) {
2585		SBP2_ERR("sbp2scsi_host_info is NULL - this is bad!");
2586		SCpnt->result = DID_NO_CONNECT << 16;
2587		done (SCpnt);
2588		return(0);
2589	}
2590
2591	scsi_id = hi->scsi_id[SCpnt->target];
2592
2593	/*
2594	 * If scsi_id is null, it means there is no device in this slot,
2595	 * so we should return selection timeout.
2596	 */
2597	if (!scsi_id) {
2598		SCpnt->result = DID_NO_CONNECT << 16;
2599		done (SCpnt);
2600		return(0);
2601	}
2602
2603	/*
2604	 * Until we handle multiple luns, just return selection time-out
2605	 * to any IO directed at non-zero LUNs
2606	 */
2607	if (SCpnt->lun) {
2608		SCpnt->result = DID_NO_CONNECT << 16;
2609		done (SCpnt);
2610		return(0);
2611	}
2612
2613	/*
2614	 * Check for request sense command, and handle it here
2615	 * (autorequest sense)
2616	 */
2617	if (SCpnt->cmnd[0] == REQUEST_SENSE) {
2618		SBP2_DEBUG("REQUEST_SENSE");
2619		memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen);
2620		memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
2621		sbp2scsi_complete_command(hi, scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done);
2622		return(0);
2623	}
2624
2625	/*
2626	 * Check to see if we are in the middle of a bus reset.
2627	 */
2628	if (!hpsb_node_entry_valid(scsi_id->ne)) {
2629		SBP2_ERR("Bus reset in progress - rejecting command");
2630		SCpnt->result = DID_BUS_BUSY << 16;
2631		done (SCpnt);
2632		return(0);
2633	}
2634
2635	/*
2636	 * Try and send our SCSI command
2637	 */
2638	sbp2_spin_lock(&hi->sbp2_command_lock, flags);
2639	if (sbp2_send_command(hi, scsi_id, SCpnt, done)) {
2640		SBP2_ERR("Error sending SCSI command");
2641		sbp2scsi_complete_command(hi, scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT, SCpnt, done);
2642	}
2643	sbp2_spin_unlock(&hi->sbp2_command_lock, flags);
2644
2645	return(0);
2646}
2647
2648/*
2649 * This function is called in order to complete all outstanding SBP-2
2650 * commands (in case of resets, etc.).
2651 */
2652static void sbp2scsi_complete_all_commands(struct sbp2scsi_host_info *hi,
2653					   struct scsi_id_instance_data *scsi_id,
2654					   u32 status)
2655{
2656	struct list_head *lh;
2657	struct sbp2_command_info *command;
2658
2659	SBP2_DEBUG("sbp2_complete_all_commands");
2660
2661	while (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
2662		SBP2_DEBUG("Found pending command to complete");
2663		lh = scsi_id->sbp2_command_orb_inuse.next;
2664		command = list_entry(lh, struct sbp2_command_info, list);
2665		pci_dma_sync_single(hi->host->pdev, command->command_orb_dma,
2666				    sizeof(struct sbp2_command_orb),
2667				    PCI_DMA_BIDIRECTIONAL);
2668		pci_dma_sync_single(hi->host->pdev, command->sge_dma,
2669				    sizeof(command->scatter_gather_element),
2670				    PCI_DMA_BIDIRECTIONAL);
2671		sbp2util_mark_command_completed(scsi_id, command);
2672		if (command->Current_SCpnt) {
2673			void (*done)(Scsi_Cmnd *) = command->Current_done;
2674			command->Current_SCpnt->result = status << 16;
2675			done (command->Current_SCpnt);
2676		}
2677	}
2678
2679	return;
2680}
2681
2682/*
2683 * This function is called in order to complete a regular SBP-2 command.
2684 */
2685static void sbp2scsi_complete_command(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id, u32 scsi_status,
2686				      Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
2687{
2688	SBP2_DEBUG("sbp2scsi_complete_command");
2689
2690	/*
2691	 * Sanity
2692	 */
2693	if (!SCpnt) {
2694		SBP2_ERR("SCpnt is NULL");
2695		return;
2696	}
2697
2698	/*
2699	 * If a bus reset is in progress and there was an error, don't
2700	 * complete the command, just let it get retried at the end of the
2701	 * bus reset.
2702	 */
2703	if (!hpsb_node_entry_valid(scsi_id->ne) && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
2704		SBP2_ERR("Bus reset in progress - retry command later");
2705		return;
2706	}
2707
2708	/*
2709	 * Switch on scsi status
2710	 */
2711	switch (scsi_status) {
2712		case SBP2_SCSI_STATUS_GOOD:
2713			SCpnt->result = DID_OK;
2714			break;
2715
2716		case SBP2_SCSI_STATUS_BUSY:
2717			SBP2_ERR("SBP2_SCSI_STATUS_BUSY");
2718			SCpnt->result = DID_BUS_BUSY << 16;
2719			break;
2720
2721		case SBP2_SCSI_STATUS_CHECK_CONDITION:
2722			SBP2_DEBUG("SBP2_SCSI_STATUS_CHECK_CONDITION");
2723			SCpnt->result = CHECK_CONDITION << 1;
2724
2725			/*
2726			 * Debug stuff
2727			 */
2728#if CONFIG_IEEE1394_SBP2_DEBUG >= 1
2729			print_command (SCpnt->cmnd);
2730			print_sense("bh", SCpnt);
2731#endif
2732
2733			break;
2734
2735		case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
2736			SBP2_ERR("SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
2737			SCpnt->result = DID_NO_CONNECT << 16;
2738			print_command (SCpnt->cmnd);
2739			break;
2740
2741		case SBP2_SCSI_STATUS_CONDITION_MET:
2742		case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
2743		case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
2744			SBP2_ERR("Bad SCSI status = %x", scsi_status);
2745			SCpnt->result = DID_ERROR << 16;
2746			print_command (SCpnt->cmnd);
2747			break;
2748
2749		default:
2750			SBP2_ERR("Unsupported SCSI status = %x", scsi_status);
2751			SCpnt->result = DID_ERROR << 16;
2752	}
2753
2754	/*
2755	 * Take care of any sbp2 response data mucking here (RBC stuff, etc.)
2756	 */
2757	if (SCpnt->result == DID_OK) {
2758		sbp2_check_sbp2_response(scsi_id, SCpnt);
2759	}
2760
2761	/*
2762	 * If a bus reset is in progress and there was an error, complete
2763	 * the command as busy so that it will get retried.
2764	 */
2765	if (!hpsb_node_entry_valid(scsi_id->ne) && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
2766		SBP2_ERR("Completing command with busy (bus reset)");
2767		SCpnt->result = DID_BUS_BUSY << 16;
2768	}
2769
2770	/*
2771	 * If a unit attention occurs, return busy status so it gets
2772	 * retried... it could have happened because of a 1394 bus reset
2773	 * or hot-plug...
2774	 */
2775
2776	/*
2777	 * Tell scsi stack that we're done with this command
2778	 */
2779#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
2780	spin_lock_irq(&io_request_lock);
2781	done (SCpnt);
2782	spin_unlock_irq(&io_request_lock);
2783#else
2784	spin_lock_irq(hi->scsi_host->host_lock);
2785	done (SCpnt);
2786	spin_unlock_irq(hi->scsi_host->host_lock);
2787#endif
2788
2789	return;
2790}
2791
2792/*
2793 * Called by scsi stack when something has really gone wrong.  Usually
2794 * called when a command has timed-out for some reason.
2795 */
2796static int sbp2scsi_abort (Scsi_Cmnd *SCpnt)
2797{
2798	struct sbp2scsi_host_info *hi = (struct sbp2scsi_host_info *) SCpnt->host->hostdata[0];
2799	struct scsi_id_instance_data *scsi_id = hi->scsi_id[SCpnt->target];
2800	struct sbp2_command_info *command;
2801	unsigned long flags;
2802
2803	SBP2_ERR("aborting sbp2 command");
2804	print_command (SCpnt->cmnd);
2805
2806	if (scsi_id) {
2807
2808		/*
2809		 * Right now, just return any matching command structures
2810		 * to the free pool.
2811		 */
2812		sbp2_spin_lock(&hi->sbp2_command_lock, flags);
2813		command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
2814		if (command) {
2815			SBP2_DEBUG("Found command to abort");
2816			pci_dma_sync_single(hi->host->pdev,
2817					    command->command_orb_dma,
2818					    sizeof(struct sbp2_command_orb),
2819					    PCI_DMA_BIDIRECTIONAL);
2820			pci_dma_sync_single(hi->host->pdev,
2821					    command->sge_dma,
2822					    sizeof(command->scatter_gather_element),
2823					    PCI_DMA_BIDIRECTIONAL);
2824			sbp2util_mark_command_completed(scsi_id, command);
2825			if (command->Current_SCpnt) {
2826				void (*done)(Scsi_Cmnd *) = command->Current_done;
2827				command->Current_SCpnt->result = DID_ABORT << 16;
2828				done (command->Current_SCpnt);
2829			}
2830		}
2831
2832		/*
2833		 * Initiate a fetch agent reset.
2834		 */
2835		sbp2_agent_reset(hi, scsi_id, SBP2_SEND_NO_WAIT);
2836		sbp2scsi_complete_all_commands(hi, scsi_id, DID_BUS_BUSY);
2837		sbp2_spin_unlock(&hi->sbp2_command_lock, flags);
2838	}
2839
2840	return(SUCCESS);
2841}
2842
2843/*
2844 * Called by scsi stack when something has really gone wrong.
2845 */
2846static int sbp2scsi_reset (Scsi_Cmnd *SCpnt)
2847{
2848	struct sbp2scsi_host_info *hi = (struct sbp2scsi_host_info *) SCpnt->host->hostdata[0];
2849	struct scsi_id_instance_data *scsi_id = hi->scsi_id[SCpnt->target];
2850
2851	SBP2_ERR("reset requested");
2852
2853	if (scsi_id) {
2854		SBP2_ERR("Generating sbp2 fetch agent reset");
2855		sbp2_agent_reset(hi, scsi_id, SBP2_SEND_NO_WAIT);
2856	}
2857
2858	return(SUCCESS);
2859}
2860
2861/*
2862 * Called by scsi stack to get bios parameters (used by fdisk, and at boot).
2863 */
2864#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,28)
2865static int sbp2scsi_biosparam (Scsi_Disk *disk, kdev_t dev, int geom[])
2866#else
2867static int sbp2scsi_biosparam (Scsi_Disk *disk, struct block_device *dev, int geom[])
2868#endif
2869{
2870	int heads, sectors, cylinders;
2871
2872	SBP2_DEBUG("Request for bios parameters");
2873
2874	heads = 64;
2875	sectors = 32;
2876	cylinders = disk->capacity / (heads * sectors);
2877
2878	if (cylinders > 1024) {
2879		heads = 255;
2880		sectors = 63;
2881		cylinders = disk->capacity / (heads * sectors);
2882	}
2883
2884	geom[0] = heads;
2885	geom[1] = sectors;
2886	geom[2] = cylinders;
2887
2888	return(0);
2889}
2890
2891/*
2892 * Called by scsi stack after scsi driver is registered
2893 */
2894static int sbp2scsi_detect (Scsi_Host_Template *tpnt)
2895{
2896	SBP2_DEBUG("sbp2scsi_detect");
2897
2898	/*
2899	 * Call sbp2_init to register with the ieee1394 stack. This
2900	 * results in a callback to sbp2_add_host for each ieee1394
2901	 * host controller currently registered, and for each of those
2902	 * we register a scsi host with the scsi stack.
2903	 */
2904
2905#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
2906	spin_unlock_irq(&io_request_lock);
2907	sbp2_init();
2908	spin_lock_irq(&io_request_lock);
2909#else
2910	sbp2_init();
2911#endif
2912
2913	/* We return the number of hosts registered. */
2914	return scsi_driver_template.present;
2915}
2916
2917
2918/*
2919 * Called for contents of procfs
2920 */
2921static const char *sbp2scsi_info (struct Scsi_Host *host)
2922{
2923	struct sbp2scsi_host_info *hi = sbp2_find_host_info_scsi(host);
2924	static char info[1024];
2925
2926	if (!hi) /* shouldn't happen, but... */
2927        	return "IEEE-1394 SBP-2 protocol driver";
2928
2929	sprintf(info, "IEEE-1394 SBP-2 protocol driver (host: %s)\n%s\n"
2930		"SBP-2 module load options:\n"
2931		"- Max speed supported: %s\n"
2932		"- Max sectors per I/O supported: %d\n"
2933		"- Max outstanding commands supported: %d\n"
2934		"- Max outstanding commands per lun supported: %d\n"
2935                "- Serialized I/O (debug): %s\n"
2936		"- Exclusive login: %s",
2937		hi->host->driver->name,
2938		version,
2939		hpsb_speedto_str[sbp2_max_speed],
2940		sbp2_max_sectors,
2941		sbp2_max_outstanding_cmds,
2942		sbp2_max_cmds_per_lun,
2943		sbp2_serialize_io ? "yes" : "no",
2944		sbp2_exclusive_login ? "yes" : "no");
2945
2946	return info;
2947}
2948
2949
2950MODULE_AUTHOR("James Goodwin <jamesg@filanet.com>");
2951MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
2952MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
2953MODULE_LICENSE("GPL");
2954
2955/* SCSI host template */
2956static Scsi_Host_Template scsi_driver_template = {
2957	.name =			"IEEE-1394 SBP-2 protocol driver",
2958	.info =			sbp2scsi_info,
2959	.detect =		sbp2scsi_detect,
2960	.queuecommand =		sbp2scsi_queuecommand,
2961	.eh_abort_handler =	sbp2scsi_abort,
2962	.eh_device_reset_handler =sbp2scsi_reset,
2963	.eh_bus_reset_handler =	sbp2scsi_reset,
2964	.eh_host_reset_handler =sbp2scsi_reset,
2965	.bios_param =		sbp2scsi_biosparam,
2966	.this_id =		-1,
2967	.sg_tablesize =		SBP2_MAX_SG_ELEMENTS,
2968	.use_clustering =	SBP2_CLUSTERING,
2969#if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
2970	.use_new_eh_code =	TRUE,
2971#endif
2972	.emulated =		1,
2973	.proc_name =	SBP2_DEVICE_NAME,
2974};
2975
2976static int sbp2_module_init(void)
2977{
2978	SBP2_DEBUG("sbp2_module_init");
2979
2980	/*
2981	 * Module load debug option to force one command at a time (serializing I/O)
2982	 */
2983	if (sbp2_serialize_io) {
2984		SBP2_ERR("Driver forced to serialize I/O (serialize_io = 1)");
2985		scsi_driver_template.can_queue = 1;
2986		scsi_driver_template.cmd_per_lun = 1;
2987	} else {
2988		scsi_driver_template.can_queue = sbp2_max_outstanding_cmds;
2989		scsi_driver_template.cmd_per_lun = sbp2_max_cmds_per_lun;
2990	}
2991
2992	/*
2993	 * Set max sectors (module load option). Default is 255 sectors.
2994	 */
2995	scsi_driver_template.max_sectors = sbp2_max_sectors;
2996
2997	/*
2998	 * Ideally we would register our scsi_driver_template with the
2999	 * scsi stack and after that register with the ieee1394 stack
3000	 * and process the add_host callbacks. However, the detect
3001	 * function in the scsi host template requires that we find at
3002	 * least one host, so we "nest" the registrations by calling
3003	 * sbp2_init from the detect function.
3004	 */
3005	scsi_driver_template.module = THIS_MODULE;
3006	if (SCSI_REGISTER_HOST(&scsi_driver_template) ||
3007	    !scsi_driver_template.present) {
3008		SBP2_ERR("Please load the lower level IEEE-1394 driver "
3009			 "(e.g. ohci1394) before sbp2...");
3010		sbp2_cleanup();
3011		return -ENODEV;
3012	}
3013
3014	return 0;
3015}
3016
3017static void __exit sbp2_module_exit(void)
3018{
3019	SBP2_DEBUG("sbp2_module_exit");
3020
3021	/*
3022	 * On module unload we unregister with the ieee1394 stack
3023	 * which results in remove_host callbacks for all ieee1394
3024	 * host controllers.  In the callbacks we unregister the
3025	 * corresponding scsi hosts.
3026	 */
3027	sbp2_cleanup();
3028
3029	if (SCSI_UNREGISTER_HOST(&scsi_driver_template))
3030		SBP2_ERR("sbp2_module_exit: couldn't unregister scsi driver");
3031
3032}
3033
3034module_init(sbp2_module_init);
3035module_exit(sbp2_module_exit);
3036