File: /usr/src/linux/drivers/ieee1394/sbp2.c
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
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 * Brief Description:
24 *
25 * This driver implements the Serial Bus Protocol 2 (SBP-2) over IEEE-1394
26 * under Linux. The SBP-2 driver is implemented as an IEEE-1394 high-level
27 * driver. It also registers as a SCSI lower-level driver in order to accept
28 * SCSI commands for transport using SBP-2.
29 *
30 * Driver Loading:
31 *
32 * Currently, the SBP-2 driver is supported only as a module. Because the
33 * Linux SCSI stack is not Plug-N-Play aware, module load order is
34 * important. Assuming the SCSI core drivers are either built into the
35 * kernel or already loaded as modules, you should load the IEEE-1394 modules
36 * in the following order:
37 *
38 * ieee1394 (e.g. insmod ieee1394)
39 * ohci1394 (e.g. insmod ohci1394)
40 * sbp2 (e.g. insmod sbp2)
41 *
42 * The SBP-2 driver will attempt to discover any attached SBP-2 devices when first
43 * loaded, or after any IEEE-1394 bus reset (e.g. a hot-plug). It will then print
44 * out a debug message indicating if it was able to discover a SBP-2 device.
45 *
46 * Currently, the SBP-2 driver will catch any attached SBP-2 devices during the
47 * initial scsi bus scan (when the driver is first loaded). To add or remove
48 * SBP-2 devices after this initial scan (i.e. if you plug-in or un-plug a
49 * device after the SBP-2 driver is loaded), you must either use the scsi procfs
50 * add-single-device, remove-single-device, or a shell script such as
51 * rescan-scsi-bus.sh.
52 *
53 * The easiest way to add/detect new SBP-2 devices is to run the shell script
54 * rescan-scsi-bus.sh (or re-load the SBP-2 driver). This script may be
55 * found at:
56 * http://www.garloff.de/kurt/linux/rescan-scsi-bus.sh
57 *
58 * As an alternative, you may manually add/remove SBP-2 devices via the procfs with
59 * add-single-device <h> <b> <t> <l> or remove-single-device <h> <b> <t> <l>, where:
60 * <h> = host (starting at zero for first SCSI adapter)
61 * <b> = bus (normally zero)
62 * <t> = target (starting at zero for first SBP-2 device)
63 * <l> = lun (normally zero)
64 *
65 * e.g. To manually add/detect a new SBP-2 device
66 * echo "scsi add-single-device 0 0 0 0" > /proc/scsi/scsi
67 *
68 * e.g. To manually remove a SBP-2 device after it's been unplugged
69 * echo "scsi remove-single-device 0 0 0 0" > /proc/scsi/scsi
70 *
71 * e.g. To check to see which SBP-2/SCSI devices are currently registered
72 * cat /proc/scsi/scsi
73 *
74 * After scanning for new SCSI devices (above), you may access any attached
75 * SBP-2 storage devices as if they were SCSI devices (e.g. mount /dev/sda1,
76 * fdisk, mkfs, etc.).
77 *
78 *
79 * Current Support:
80 *
81 * The SBP-2 driver is still in an early state, but supports a variety of devices.
82 * I have read/written many gigabytes of data from/to SBP-2 drives, and have seen
83 * performance of more than 16 MBytes/s on individual drives (limit of the media
84 * transfer rate).
85 *
86 * Following are the devices that have been tested successfully:
87 *
88 * - Western Digital IEEE-1394 hard drives
89 * - Maxtor IEEE-1394 hard drives
90 * - VST (SmartDisk) IEEE-1394 hard drives and Zip drives (several flavors)
91 * - LaCie IEEE-1394 hard drives (several flavors)
92 * - QPS IEEE-1394 CD-RW/DVD drives and hard drives
93 * - BusLink IEEE-1394 hard drives
94 * - Iomega IEEE-1394 Zip/Jazz drives
95 * - ClubMac IEEE-1394 hard drives
96 * - FirePower IEEE-1394 hard drives
97 * - EzQuest IEEE-1394 hard drives and CD-RW drives
98 * - Castlewood/ADS IEEE-1394 ORB drives
99 * - Evergreen IEEE-1394 hard drives and CD-RW drives
100 * - Addonics IEEE-1394 CD-RW drives
101 * - Bellstor IEEE-1394 hard drives and CD-RW drives
102 * - APDrives IEEE-1394 hard drives
103 * - Fujitsu IEEE-1394 MO drives
104 * - Sony IEEE-1394 CD-RW drives
105 * - Epson IEEE-1394 scanner
106 * - ADS IEEE-1394 memory stick and compact flash readers
107 * (e.g. "insmod sbp2 mode_sense_hack=1" for mem stick and flash readers))
108 * - SBP-2 bridge-based devices (LSI, Oxford Semiconductor, Indigita bridges)
109 * - Various other standard IEEE-1394 hard drives and enclosures
110 *
111 *
112 * Performance Issues:
113 *
114 * - Make sure you are "not" running fat/fat32 on your attached SBP-2 drives. You'll
115 * get much better performance formatting the drive ext2 (but you will lose the
116 * ability to easily move the drive between Windows/Linux).
117 *
118 *
119 * Current Issues:
120 *
121 * - Currently, all I/O from the scsi stack is serialized by default, as there
122 * are some stress issues under investigation with deserialized I/O. To enable
123 * deserialized I/O for testing, do "insmod sbp2 serialize_io=0"
124 *
125 * - Hot-Plugging: Need to add procfs support and integration with linux
126 * hot-plug support (http://linux-hotplug.sourceforge.net) for auto-mounting
127 * of drives.
128 *
129 * - Error Handling: SCSI aborts and bus reset requests are handled somewhat
130 * but the code needs additional debugging.
131 *
132 * - IEEE-1394 Bus Management: There is currently little bus management
133 * in the core IEEE-1394 stack. Because of this, the SBP-2 driver handles
134 * detection of SBP-2 devices itself. This should be moved to the core
135 * stack.
136 *
137 * - The SBP-2 driver is currently only supported as a module. It would not take
138 * much work to allow it to be compiled into the kernel, but you'd have to
139 * add some init code to the kernel to support this... and modules are much
140 * more flexible anyway. ;-)
141 *
142 * - Workaround for PPC pismo firewire chipset (enable SBP2_PPC_PISMO_WORKAROUND
143 * define below).
144 *
145 *
146 * History:
147 *
148 * 07/25/00 - Initial revision (JSG)
149 * 08/11/00 - Following changes/bug fixes were made (JSG):
150 * * Bug fix to SCSI procfs code (still needs to be synched with 2.4 kernel).
151 * * Bug fix where request sense commands were actually sent on the bus.
152 * * Changed bus reset/abort code to deal with devices that spin up quite
153 * slowly (which result in SCSI time-outs).
154 * * "More" properly pull information from device's config rom, for enumeration
155 * of SBP-2 devices, and determining SBP-2 register offsets.
156 * * Change Simplified Direct Access Device type to Direct Access Device type in
157 * returned inquiry data, in order to make the SCSI stack happy.
158 * * Modified driver to register with the SCSI stack "before" enumerating any attached
159 * SBP-2 devices. This means that you'll have to use procfs scsi-add-device or
160 * some sort of script to discover new SBP-2 devices.
161 * * Minor re-write of some code and other minor changes.
162 * 08/28/00 - Following changes/bug fixes were made (JSG):
163 * * Bug fixes to scatter/gather support (case of one s/g element)
164 * * Updated direction table for scsi commands (mostly DVD commands)
165 * * Retries when trying to detect SBP-2 devices (for slow devices)
166 * * Slightly better error handling (previously none) when commands time-out.
167 * * Misc. other bug fixes and code reorganization.
168 * 09/13/00 - Following changes/bug fixes were made (JSG)
169 * * Moved detection/enumeration code to a kernel thread which is woken up when IEEE-1394
170 * bus resets occur.
171 * * Added code to handle bus resets and hot-plugging while devices are mounted, but full
172 * hot-plug support is not quite there yet.
173 * * Now use speed map to determine speed and max payload sizes for ORBs
174 * * Clean-up of code and reorganization
175 * 09/19/00 - Added better hot-plug support and other minor changes (JSG)
176 * 10/15/00 - Fixes for latest 2.4.0 test kernel, minor fix for hot-plug race. (JSG)
177 * 12/03/00 - Created pool of request packet structures for use in sending out sbp2 command
178 * and agent reset requests. This removes the kmallocs/kfrees in the critical I/O paths,
179 * and also deals with some subtle race conditions related to allocating and freeing
180 * packets. (JSG)
181 * 12/09/00 - Improved the sbp2 device detection by actually reading the root and unit
182 * directory (khk@khk.net)
183 * 12/23/00 - Following changes/enhancements were made (JSG)
184 * * Only do SCSI to RBC command conversion for Direct Access and Simplified
185 * Direct Access Devices (this is pulled from the config rom root directory).
186 * This is needed because doing the conversion for all device types broke the
187 * Epson scanner. Still looking for a better way of determining when to convert
188 * commands (for RBC devices). Thanks to khk for helping on this!
189 * * Added ability to "emulate" physical dma support, for host adapters such as TILynx.
190 * * Determine max payload and speed by also looking at the host adapter's max_rec field.
191 * 01/19/01 - Added checks to sbp2 login and made the login time-out longer. Also fixed a compile
192 * problem for 2.4.0. (JSG)
193 * 01/24/01 - Fixed problem when individual s/g elements are 64KB or larger. Needed to break
194 * up these larger elements, since the sbp2 page table element size is only 16 bits. (JSG)
195 * 01/29/01 - Minor byteswap fix for login response (used for reconnect and log out).
196 * 03/07/01 - Following changes/enhancements were made (JSG)
197 * * Changes to allow us to catch the initial scsi bus scan (for detecting sbp2
198 * devices when first loading sbp2.o). To disable this, un-define
199 * SBP2_SUPPORT_INITIAL_BUS_SCAN.
200 * * Temporary fix to deal with many sbp2 devices that do not support individual
201 * transfers of greater than 128KB in size.
202 * * Mode sense conversion from 6 byte to 10 byte versions for CDRW/DVD devices. (Mark Burton)
203 * * Define allowing support for goofy sbp2 devices that do not support mode
204 * sense command at all, allowing them to be mounted rw (such as 1394 memory
205 * stick and compact flash readers). Define SBP2_MODE_SENSE_WRITE_PROTECT_HACK
206 * if you need this fix.
207 * 03/29/01 - Major performance enhancements and misc. other changes. Thanks to Daniel Berlin for many of
208 * changes and suggestions for change:
209 * * Now use sbp2 doorbell and link commands on the fly (instead of serializing requests)
210 * * Removed all bit fields in an attempt to run on PPC machines (still needs a little more work)
211 * * Added large request break-up/linking support for sbp2 chipsets that do not support transfers
212 * greater than 128KB in size.
213 * * Bumped up max commands per lun to two, and max total outstanding commands to eight.
214 * 04/03/01 - Minor clean-up. Write orb pointer directly if no outstanding commands (saves one 1394 bus
215 * transaction). Added module load options (bus scan, mode sense hack, max speed, serialize_io,
216 * no_large_transfers). Better bus reset handling while I/O pending. Set serialize_io to 1 by
217 * default (debugging of deserialized I/O in progress).
218 * 04/04/01 - Added workaround for PPC Pismo firewire chipset. See #define below. (Daniel Berlin)
219 * 04/20/01 - Minor clean-up. Allocate more orb structures when running with sbp2 target chipsets with
220 * 128KB max transfer limit.
221 * 06/16/01 - Converted DMA interfaces to pci_dma - Ben Collins
222 * <bcollins@debian.org
223 * 07/22/01 - Use NodeMngr to get info about the local host and
224 * attached devices. Ben Collins
225 */
226
227 /*
228 * Includes
229 */
230
231 #include <linux/config.h>
232 #include <linux/kernel.h>
233 #include <linux/list.h>
234 #include <linux/string.h>
235 #include <linux/slab.h>
236 #include <linux/fs.h>
237 #include <linux/poll.h>
238 #include <linux/module.h>
239 #include <linux/types.h>
240 #include <linux/delay.h>
241 #include <linux/sched.h>
242 #include <linux/proc_fs.h>
243 #include <linux/blk.h>
244 #include <linux/smp_lock.h>
245 #include <asm/current.h>
246 #include <asm/uaccess.h>
247 #include <asm/io.h>
248 #include <asm/byteorder.h>
249 #include <asm/system.h>
250 #include <asm/io.h>
251 #include <asm/scatterlist.h>
252
253 #include "ieee1394.h"
254 #include "ieee1394_types.h"
255 #include "ieee1394_core.h"
256 #include "hosts.h"
257 #include "nodemgr.h"
258 #include "highlevel.h"
259 #include "ieee1394_transactions.h"
260 #include "ieee1394_hotplug.h"
261 #include "../scsi/scsi.h"
262 #include "../scsi/hosts.h"
263 #include "../scsi/sd.h"
264 #include "sbp2.h"
265
266 /*
267 * PPC firewire Pismo chipset workaround!!!
268 *
269 * This is a workaround for a bug in the firewire pismo chipset. For some odd reason the status
270 * fifo address hi/lo must be byteswapped and the response address byteswapped, but no other
271 * parts of the structure. Apple's drivers seem to specifically check for the pismo and do
272 * the same workaround for sbp2. (Daniel Berlin)
273 *
274 * Please enable the following define if you're running on the PPC Pismo chipset.
275 */
276
277 #ifdef CONFIG_IEEE1394_SBP2_PISMO
278 #define SBP2_NEED_LOGIN_DESCRIPTOR_WORKAROUND
279 #endif
280
281 /*
282 * Module load parameter definitions
283 */
284
285 /*
286 * Normally the sbp2 driver tries to catch the initial scsi bus scan to pick up any
287 * attached sbp2 devices. Setting no_bus_scan to 1 tells the sbp2 driver not to catch
288 * this initial scsi bus scan on module load. You can always either add or remove devices
289 * later through the rescan-scsi-bus.sh script or scsi procfs.
290 */
291 MODULE_PARM(no_bus_scan,"i");
292 MODULE_PARM_DESC(no_bus_scan, "Skip the initial scsi bus scan during module load");
293 static int no_bus_scan = 0;
294
295 /*
296 * Set mode_sense_hack to 1 if you have some sort of unusual sbp2 device, like a 1394 memory
297 * stick reader, compact flash reader, or MO drive that does not support mode sense. Allows
298 * you to mount the media rw instead of ro.
299 */
300 MODULE_PARM(mode_sense_hack,"i");
301 MODULE_PARM_DESC(mode_sense_hack, "Emulate mode sense for devices like 1394 memory stick readers");
302 static int mode_sense_hack = 0;
303
304 /*
305 * Change max_speed on module load if you have a bad IEEE-1394 controller that has trouble running
306 * 2KB packets at 400mb.
307 *
308 * NOTE: On certain OHCI parts I have seen short packets on async transmit (probably
309 * due to PCI latency/throughput issues with the part). You can bump down the speed if
310 * you are running into problems.
311 *
312 * Valid values:
313 * max_speed = 2 (default: max speed 400mb)
314 * max_speed = 1 (max speed 200mb)
315 * max_speed = 0 (max speed 100mb)
316 */
317 MODULE_PARM(max_speed,"i");
318 MODULE_PARM_DESC(max_speed, "Force down max speed (2 = 400mb default, 1 = 200mb, 0 = 100mb)");
319 static int max_speed = SPEED_400;
320
321 /*
322 * Set serialize_io to 1 if you'd like only one scsi command sent down to us at a time (debugging).
323 */
324 MODULE_PARM(serialize_io,"i");
325 MODULE_PARM_DESC(serialize_io, "Serialize all I/O coming down from the scsi drivers (debugging)");
326 static int serialize_io = 1; /* serialize I/O until stress issues are resolved */
327
328 /*
329 * Set no_large_packets to 1 if you'd like to limit the size of requests sent down to us (normally
330 * the sbp2 driver will break up any requests to any individual devices with 128KB transfer size limits).
331 * Sets max s/g list elements to 0x1f in size and disables s/g clustering.
332 */
333 MODULE_PARM(no_large_packets,"i");
334 MODULE_PARM_DESC(no_large_packets, "Do not allow large transfers from scsi drivers (debugging)");
335 static int no_large_packets = 0;
336
337 /*
338 * Export information about protocols/devices supported by this driver
339 */
340 static struct ieee1394_device_id sbp2_id_table[] = {
341 IEEE1394_PROTOCOL(SBP2_UNIT_SPEC_ID_ENTRY, SBP2_SW_VERSION_ENTRY),
342 { }
343 };
344
345 MODULE_DEVICE_TABLE(ieee1394, sbp2_id_table);
346
347 /*
348 * Debug levels, configured via kernel config.
349 */
350
351 #ifdef CONFIG_IEEE1394_SBP2_DEBUG_ORBS
352 #define SBP2_ORB_DEBUG(fmt, args...) HPSB_ERR("sbp2("__FUNCTION__"): "fmt, ## args)
353 static u32 global_outstanding_command_orbs = 0;
354 #define outstanding_orb_incr global_outstanding_command_orbs++
355 #define outstanding_orb_decr global_outstanding_command_orbs--
356 #else
357 #define SBP2_ORB_DEBUG(fmt, args...)
358 #define outstanding_orb_incr
359 #define outstanding_orb_decr
360 #endif
361
362 #ifdef CONFIG_IEEE1394_SBP2_DEBUG_DMA
363 #define SBP2_DMA_ALLOC(fmt, args...) \
364 HPSB_ERR("sbp2("__FUNCTION__")alloc(%d): "fmt, \
365 ++global_outstanding_dmas, ## args)
366 #define SBP2_DMA_FREE(fmt, args...) \
367 HPSB_ERR("sbp2("__FUNCTION__")free(%d): "fmt, \
368 --global_outstanding_dmas, ## args)
369 static u32 global_outstanding_dmas = 0;
370 #else
371 #define SBP2_DMA_ALLOC(fmt, args...)
372 #define SBP2_DMA_FREE(fmt, args...)
373 #endif
374
375 #if CONFIG_IEEE1394_SBP2_DEBUG >= 2
376 #define SBP2_DEBUG(fmt, args...) HPSB_ERR(fmt, ## args)
377 #define SBP2_INFO(fmt, args...) HPSB_ERR(fmt, ## args)
378 #define SBP2_NOTICE(fmt, args...) HPSB_ERR(fmt, ## args)
379 #define SBP2_WARN(fmt, args...) HPSB_ERR(fmt, ## args)
380 #elif CONFIG_IEEE1394_SBP2_DEBUG == 1
381 #define SBP2_DEBUG(fmt, args...) HPSB_DEBUG(fmt, ## args)
382 #define SBP2_INFO(fmt, args...) HPSB_INFO(fmt, ## args)
383 #define SBP2_NOTICE(fmt, args...) HPSB_NOTICE(fmt, ## args)
384 #define SBP2_WARN(fmt, args...) HPSB_WARN(fmt, ## args)
385 #else
386 #define SBP2_DEBUG(fmt, args...)
387 #define SBP2_INFO(fmt, args...)
388 #define SBP2_NOTICE(fmt, args...)
389 #define SBP2_WARN(fmt, args...)
390 #endif
391
392 #define SBP2_ERR(fmt, args...) HPSB_ERR(fmt, ## args)
393
394 /*
395 * Spinlock debugging stuff. I'm playing it safe until the driver has been debugged on SMP. (JSG)
396 */
397 /* #define SBP2_USE_REAL_SPINLOCKS */
398 #ifdef SBP2_USE_REAL_SPINLOCKS
399 #define sbp2_spin_lock(lock, flags) spin_lock_irqsave(lock, flags)
400 #define sbp2_spin_unlock(lock, flags) spin_unlock_irqrestore(lock, flags);
401 #else
402 #define sbp2_spin_lock(lock, flags) do {save_flags(flags); cli();} while (0)
403 #define sbp2_spin_unlock(lock, flags) do {restore_flags(flags);} while (0)
404 #endif
405
406 /*
407 * Globals
408 */
409
410 Scsi_Host_Template *global_scsi_tpnt = NULL;
411
412 static LIST_HEAD(sbp2_host_info_list);
413 static int sbp2_host_count = 0;
414 static spinlock_t sbp2_host_info_lock = SPIN_LOCK_UNLOCKED;
415
416 static struct hpsb_highlevel *sbp2_hl_handle = NULL;
417
418 static struct hpsb_highlevel_ops sbp2_hl_ops = {
419 sbp2_add_host,
420 sbp2_remove_host,
421 sbp2_host_reset,
422 NULL,
423 NULL
424 };
425
426 static struct hpsb_address_ops sbp2_ops = {
427 write: sbp2_handle_status_write,
428 };
429
430 #if 0
431 static struct hpsb_address_ops sbp2_physdma_ops = {
432 read: sbp2_handle_physdma_read,
433 write: sbp2_handle_physdma_write,
434 };
435 #endif
436
437 /**************************************
438 * General utility functions
439 **************************************/
440
441
442 #ifndef __BIG_ENDIAN
443 /*
444 * Converts a buffer from be32 to cpu byte ordering. Length is in bytes.
445 */
446 static __inline__ void sbp2util_be32_to_cpu_buffer(void *buffer, int length)
447 {
448 u32 *temp = buffer;
449
450 for (length = (length >> 2); length--; )
451 temp[length] = be32_to_cpu(temp[length]);
452
453 return;
454 }
455
456 /*
457 * Converts a buffer from cpu to be32 byte ordering. Length is in bytes.
458 */
459 static __inline__ void sbp2util_cpu_to_be32_buffer(void *buffer, int length)
460 {
461 u32 *temp = buffer;
462
463 for (length = (length >> 2); length--; )
464 temp[length] = cpu_to_be32(temp[length]);
465
466 return;
467 }
468 #else /* BIG_ENDIAN */
469 /* Why waste the cpu cycles? */
470 #define sbp2util_be32_to_cpu_buffer(x,y)
471 #define sbp2util_cpu_to_be32_buffer(x,y)
472 #endif
473
474 /*
475 * This function does quadlet sized reads (used by detection code)
476 */
477 static int sbp2util_read_quadlet(struct sbp2scsi_host_info *hi, nodeid_t node, u64 addr,
478 quadlet_t *buffer)
479 {
480 int retval = 0;
481 int retry_count = 3;
482
483 /*
484 * Retry a couple times if needed (for slow devices)
485 */
486 do {
487
488 retval = hpsb_read(hi->host, node, addr, buffer, 4);
489
490 if (retval) {
491 SBP2_DEBUG("sbp2: sbp2util_read_quadlet data packet error");
492 current->state = TASK_INTERRUPTIBLE;
493 schedule_timeout(HZ/50); /* 20ms delay */
494 }
495
496 retry_count--;
497
498 } while (retval && retry_count);
499
500 return(retval);
501 }
502
503 /*
504 * This function returns the address of the unit directory.
505 */
506 static int sbp2util_unit_directory(struct sbp2scsi_host_info *hi, nodeid_t node_id, u64 *unit_directory_addr)
507 {
508 quadlet_t root_directory_length, current_quadlet;
509 u64 current_addr;
510 int length, i;
511
512 /*
513 * First, read the first quadlet of the root directory to determine its size
514 */
515 if (sbp2util_read_quadlet(hi, LOCAL_BUS | node_id, CONFIG_ROM_ROOT_DIR_BASE,
516 &root_directory_length)) {
517 SBP2_DEBUG("sbp2: Error reading root directory length - bad status");
518 return(-EIO);
519 }
520
521 current_addr = CONFIG_ROM_ROOT_DIR_BASE;
522 length = be32_to_cpu(root_directory_length) >> 16;
523
524 /*
525 * Step through the root directory and look for the "Unit_Directory entry", which
526 * contains the offset to the unit directory.
527 */
528 for (i=0; i < length; i++) {
529
530 current_addr += 4;
531
532 if (sbp2util_read_quadlet(hi, LOCAL_BUS | node_id, current_addr, ¤t_quadlet)) {
533 SBP2_DEBUG("sbp2: Error reading at address 0x%08x%08x - bad status",
534 (unsigned int) ((current_addr) >> 32), (unsigned int) ((current_addr) & 0xffffffff));
535 return(-EIO);
536 }
537
538 /*
539 * Check for unit directory offset tag
540 */
541 if ((be32_to_cpu(current_quadlet) >> 24) == SBP2_UNIT_DIRECTORY_OFFSET_KEY) {
542 *unit_directory_addr = current_addr + 4 * ((be32_to_cpu(current_quadlet) & 0xffffff));
543 SBP2_DEBUG("sbp2: unit_directory_addr = %lu", *unit_directory_addr);
544 }
545 }
546
547 return(0);
548 }
549
550 /*
551 * This function is called to initially create a packet pool for use in sbp2 I/O requests.
552 * This packet pool is used when sending out sbp2 command and agent reset requests, and
553 * allows us to remove all kmallocs/kfrees from the critical I/O paths.
554 */
555 static int sbp2util_create_request_packet_pool(struct sbp2scsi_host_info *hi)
556 {
557 struct hpsb_packet *packet;
558 int i;
559 unsigned long flags;
560
561 /*
562 * Create SBP2_MAX_REQUEST_PACKETS number of request packets.
563 */
564 sbp2_spin_lock(&hi->sbp2_request_packet_lock, flags);
565 for (i=0; i<SBP2_MAX_REQUEST_PACKETS; i++) {
566
567 /*
568 * Max payload of 8 bytes since the sbp2 command request uses a payload of
569 * 8 bytes, and agent reset is a quadlet write request. Bump this up if we
570 * plan on using this pool for other stuff.
571 */
572 packet = alloc_hpsb_packet(8);
573
574 if (!packet) {
575 SBP2_ERR("sbp2: sbp2util_create_request_packet_pool - packet allocation failed!");
576 sbp2_spin_unlock(&hi->sbp2_request_packet_lock, flags);
577 return(-ENOMEM);
578 }
579
580 /*
581 * Put these request packets into a free list
582 */
583 INIT_LIST_HEAD(&hi->request_packet[i].list);
584 hi->request_packet[i].packet = packet;
585 list_add_tail(&hi->request_packet[i].list, &hi->sbp2_req_free);
586
587 }
588 sbp2_spin_unlock(&hi->sbp2_request_packet_lock, flags);
589
590 return(0);
591 }
592
593 /*
594 * This function is called to remove the packet pool. It is called when the sbp2 driver is unloaded.
595 */
596 static void sbp2util_remove_request_packet_pool(struct sbp2scsi_host_info *hi)
597 {
598 struct list_head *lh;
599 struct sbp2_request_packet *request_packet;
600 unsigned long flags;
601
602 /*
603 * Go through free list releasing packets
604 */
605 sbp2_spin_lock(&hi->sbp2_request_packet_lock, flags);
606 while (!list_empty(&hi->sbp2_req_free)) {
607
608 lh = hi->sbp2_req_free.next;
609 list_del(lh);
610
611 request_packet = list_entry(lh, struct sbp2_request_packet, list);
612
613 /*
614 * Free the hpsb packets that we allocated for the pool
615 */
616 if (request_packet) {
617 free_hpsb_packet(request_packet->packet);
618 }
619
620 }
621 sbp2_spin_unlock(&hi->sbp2_request_packet_lock, flags);
622
623 return;
624 }
625
626 /*
627 * This function is called to retrieve a block write packet from our packet pool. This function is
628 * used in place of calling alloc_hpsb_packet (which costs us three kmallocs). Instead we
629 * just pull out a free request packet and re-initialize values in it. I'm sure this can still
630 * stand some more optimization.
631 */
632 static struct sbp2_request_packet *sbp2util_allocate_write_request_packet(struct sbp2scsi_host_info *hi,
633 nodeid_t node, u64 addr,
634 size_t data_size,
635 quadlet_t data) {
636 struct list_head *lh;
637 struct sbp2_request_packet *request_packet = NULL;
638 struct hpsb_packet *packet;
639 unsigned long flags;
640
641 sbp2_spin_lock(&hi->sbp2_request_packet_lock, flags);
642 if (!list_empty(&hi->sbp2_req_free)) {
643
644 /*
645 * Pull out a free request packet
646 */
647 lh = hi->sbp2_req_free.next;
648 list_del(lh);
649
650 request_packet = list_entry(lh, struct sbp2_request_packet, list);
651 packet = request_packet->packet;
652
653 /*
654 * Initialize the packet (this is really initialization the core 1394 stack should do,
655 * but I'm doing it myself to avoid the overhead).
656 */
657 packet->data_size = data_size;
658 INIT_LIST_HEAD(&packet->list);
659 sema_init(&packet->state_change, 0);
660 packet->state = unused;
661 packet->generation = get_hpsb_generation(hi->host);
662 packet->data_be = 1;
663
664 packet->host = hi->host;
665 packet->tlabel = get_tlabel(hi->host, node, 1);
666 packet->node_id = node;
667
668 if (!data_size) {
669 fill_async_writequad(packet, addr, data);
670 } else {
671 fill_async_writeblock(packet, addr, data_size);
672 }
673
674 /*
675 * Set up a task queue completion routine, which returns the packet to the free list
676 * and releases the tlabel
677 */
678 request_packet->tq.routine = (void (*)(void*))sbp2util_free_request_packet;
679 request_packet->tq.data = request_packet;
680 request_packet->hi_context = hi;
681 queue_task(&request_packet->tq, &packet->complete_tq);
682
683 /*
684 * Now, put the packet on the in-use list
685 */
686 list_add_tail(&request_packet->list, &hi->sbp2_req_inuse);
687
688 } else {
689 SBP2_ERR("sbp2: sbp2util_allocate_request_packet - no packets available!");
690 }
691 sbp2_spin_unlock(&hi->sbp2_request_packet_lock, flags);
692
693 return(request_packet);
694 }
695
696 /*
697 * This function is called to return a packet to our packet pool. It is also called as a
698 * completion routine when a request packet is completed.
699 */
700 static void sbp2util_free_request_packet(struct sbp2_request_packet *request_packet)
701 {
702 unsigned long flags;
703 struct sbp2scsi_host_info *hi = request_packet->hi_context;
704
705 /*
706 * Free the tlabel, and return the packet to the free pool
707 */
708 sbp2_spin_lock(&hi->sbp2_request_packet_lock, flags);
709 free_tlabel(hi->host, LOCAL_BUS | request_packet->packet->node_id,
710 request_packet->packet->tlabel);
711 list_del(&request_packet->list);
712 list_add_tail(&request_packet->list, &hi->sbp2_req_free);
713 sbp2_spin_unlock(&hi->sbp2_request_packet_lock, flags);
714
715 return;
716 }
717
718 /*
719 * This function is called to create a pool of command orbs used for command processing. It is called
720 * when a new sbp2 device is detected.
721 */
722 static int sbp2util_create_command_orb_pool(struct scsi_id_instance_data *scsi_id,
723 struct sbp2scsi_host_info *hi)
724 {
725 int i;
726 unsigned long flags;
727 struct sbp2_command_info *command;
728
729 sbp2_spin_lock(&scsi_id->sbp2_command_orb_lock, flags);
730 for (i = 0; i < scsi_id->sbp2_total_command_orbs; i++) {
731 command = (struct sbp2_command_info *)
732 kmalloc(sizeof(struct sbp2_command_info), GFP_KERNEL);
733 if (!command) {
734 sbp2_spin_unlock(&scsi_id->sbp2_command_orb_lock, flags);
735 return(-ENOMEM);
736 }
737 memset(command, '\0', sizeof(struct sbp2_command_info));
738 command->command_orb_dma =
739 pci_map_single (hi->host->pdev, &command->command_orb,
740 sizeof(struct sbp2_command_orb),
741 PCI_DMA_BIDIRECTIONAL);
742 SBP2_DMA_ALLOC("single command orb DMA");
743 command->sge_dma =
744 pci_map_single (hi->host->pdev, &command->scatter_gather_element,
745 sizeof(command->scatter_gather_element),
746 PCI_DMA_BIDIRECTIONAL);
747 SBP2_DMA_ALLOC("scatter_gather_element");
748 INIT_LIST_HEAD(&command->list);
749 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
750 }
751 sbp2_spin_unlock(&scsi_id->sbp2_command_orb_lock, flags);
752 return 0;
753 }
754
755 /*
756 * This function is called to delete a pool of command orbs.
757 */
758 static void sbp2util_remove_command_orb_pool(struct scsi_id_instance_data *scsi_id,
759 struct sbp2scsi_host_info *hi)
760 {
761 struct list_head *lh;
762 struct sbp2_command_info *command;
763 unsigned long flags;
764
765 sbp2_spin_lock(&scsi_id->sbp2_command_orb_lock, flags);
766 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
767 list_for_each(lh, &scsi_id->sbp2_command_orb_completed) {
768 command = list_entry(lh, struct sbp2_command_info, list);
769
770 /* Release our generic DMA's */
771 pci_unmap_single(hi->host->pdev, command->command_orb_dma,
772 sizeof(struct sbp2_command_orb),
773 PCI_DMA_BIDIRECTIONAL);
774 SBP2_DMA_FREE("single command orb DMA");
775 pci_unmap_single(hi->host->pdev, command->sge_dma,
776 sizeof(command->scatter_gather_element),
777 PCI_DMA_BIDIRECTIONAL);
778 SBP2_DMA_FREE("scatter_gather_element");
779
780 kfree(command);
781 }
782 }
783 sbp2_spin_unlock(&scsi_id->sbp2_command_orb_lock, flags);
784 return;
785 }
786
787 /*
788 * This functions finds the sbp2_command for a given outstanding
789 * command orb. Only looks at the inuse list.
790 */
791 static struct sbp2_command_info *sbp2util_find_command_for_orb(struct scsi_id_instance_data *scsi_id, dma_addr_t orb)
792 {
793 struct list_head *lh;
794 struct sbp2_command_info *command;
795 unsigned long flags;
796
797 sbp2_spin_lock(&scsi_id->sbp2_command_orb_lock, flags);
798 if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
799 list_for_each(lh, &scsi_id->sbp2_command_orb_inuse) {
800 command = list_entry(lh, struct sbp2_command_info, list);
801 if (command->command_orb_dma == orb) {
802 sbp2_spin_unlock(&scsi_id->sbp2_command_orb_lock, flags);
803 return (command);
804 }
805 }
806 }
807 sbp2_spin_unlock(&scsi_id->sbp2_command_orb_lock, flags);
808
809 SBP2_ORB_DEBUG("could not match command orb %x", (unsigned int)orb);
810
811 return(NULL);
812 }
813
814 /*
815 * This functions finds the sbp2_command for a given outstanding SCpnt. Only looks at the inuse list
816 */
817 static struct sbp2_command_info *sbp2util_find_command_for_SCpnt(struct scsi_id_instance_data *scsi_id, void *SCpnt)
818 {
819 struct list_head *lh;
820 struct sbp2_command_info *command;
821 unsigned long flags;
822
823 sbp2_spin_lock(&scsi_id->sbp2_command_orb_lock, flags);
824 if (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
825 list_for_each(lh, &scsi_id->sbp2_command_orb_inuse) {
826 command = list_entry(lh, struct sbp2_command_info, list);
827 if (command->Current_SCpnt == SCpnt) {
828 sbp2_spin_unlock(&scsi_id->sbp2_command_orb_lock, flags);
829 return (command);
830 }
831 }
832 }
833 sbp2_spin_unlock(&scsi_id->sbp2_command_orb_lock, flags);
834 return(NULL);
835 }
836
837 /*
838 * This function allocates a command orb used to send a scsi command.
839 */
840 static struct sbp2_command_info *sbp2util_allocate_command_orb(struct scsi_id_instance_data *scsi_id,
841 Scsi_Cmnd *Current_SCpnt,
842 void (*Current_done)(Scsi_Cmnd *),
843 struct sbp2scsi_host_info *hi)
844 {
845 struct list_head *lh;
846 struct sbp2_command_info *command = NULL;
847 unsigned long flags;
848
849 sbp2_spin_lock(&scsi_id->sbp2_command_orb_lock, flags);
850 if (!list_empty(&scsi_id->sbp2_command_orb_completed)) {
851 lh = scsi_id->sbp2_command_orb_completed.next;
852 list_del(lh);
853 command = list_entry(lh, struct sbp2_command_info, list);
854 command->Current_done = Current_done;
855 command->Current_SCpnt = Current_SCpnt;
856 command->linked = 0;
857 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_inuse);
858 } else {
859 SBP2_ERR("sbp2: sbp2util_allocate_command_orb - No orbs available!");
860 }
861 sbp2_spin_unlock(&scsi_id->sbp2_command_orb_lock, flags);
862 return (command);
863 }
864
865 /* Free our DMA's */
866 static void sbp2util_free_command_dma(struct sbp2_command_info *command)
867 {
868 struct sbp2scsi_host_info *hi;
869
870 hi = (struct sbp2scsi_host_info *) command->Current_SCpnt->host->hostdata[0];
871
872 if (hi == NULL) {
873 printk(KERN_ERR __FUNCTION__": hi == NULL\n");
874 return;
875 }
876
877 if (command->cmd_dma) {
878 pci_unmap_single(hi->host->pdev, command->cmd_dma,
879 command->dma_size, command->dma_dir);
880 SBP2_DMA_FREE("single bulk");
881 command->cmd_dma = 0;
882 }
883
884 if (command->sge_buffer) {
885 pci_unmap_sg(hi->host->pdev, command->sge_buffer,
886 command->dma_size, command->dma_dir);
887 SBP2_DMA_FREE("scatter list");
888 command->sge_buffer = NULL;
889 }
890 }
891
892 /*
893 * This function moves a command to the completed orb list.
894 */
895 static void sbp2util_mark_command_completed(struct scsi_id_instance_data *scsi_id, struct sbp2_command_info *command)
896 {
897 unsigned long flags;
898
899 sbp2_spin_lock(&scsi_id->sbp2_command_orb_lock, flags);
900 list_del(&command->list);
901 sbp2util_free_command_dma(command);
902 list_add_tail(&command->list, &scsi_id->sbp2_command_orb_completed);
903 sbp2_spin_unlock(&scsi_id->sbp2_command_orb_lock, flags);
904 }
905
906 /*********************************************
907 * IEEE-1394 core driver stack related section
908 *********************************************/
909
910 /*
911 * This function is called at SCSI init in order to register our driver with the
912 * IEEE-1394 stack
913 */
914 int sbp2_init(void)
915 {
916 SBP2_DEBUG("sbp2: sbp2_init");
917
918 /*
919 * Register our high level driver with 1394 stack
920 */
921 sbp2_hl_handle = hpsb_register_highlevel(SBP2_DEVICE_NAME, &sbp2_hl_ops);
922
923 if (sbp2_hl_handle == NULL) {
924 SBP2_ERR("sbp2: sbp2 failed to register with ieee1394 highlevel");
925 return(-ENOMEM);
926 }
927
928 /*
929 * Register our sbp2 status address space...
930 */
931 hpsb_register_addrspace(sbp2_hl_handle, &sbp2_ops, SBP2_STATUS_FIFO_ADDRESS,
932 SBP2_STATUS_FIFO_ADDRESS + sizeof(struct sbp2_status_block));
933
934 /*
935 * Register physical dma address space... used for
936 * adapters not supporting hardware phys dma.
937 *
938 * XXX: Disabled for now.
939 */
940 /* hpsb_register_addrspace(sbp2_hl_handle, &sbp2_physdma_ops,
941 0x0ULL, 0xfffffffcULL); */
942
943 return(0);
944 }
945
946 /*
947 * This function is called from cleanup module, or during shut-down, in order to
948 * unregister our driver
949 */
950 void sbp2_cleanup(void)
951 {
952 SBP2_DEBUG("sbp2: sbp2_cleanup");
953
954 if (sbp2_hl_handle) {
955 hpsb_unregister_highlevel(sbp2_hl_handle);
956 sbp2_hl_handle = NULL;
957 }
958 return;
959 }
960
961 /*
962 * This function is called after registering our operations in sbp2_init. We go ahead and
963 * allocate some memory for our host info structure, and init some structures.
964 */
965 static void sbp2_add_host(struct hpsb_host *host)
966 {
967 struct sbp2scsi_host_info *hi;
968 unsigned long flags;
969
970 SBP2_DEBUG("sbp2: sbp2_add_host");
971
972 /*
973 * Allocate some memory for our host info structure
974 */
975 hi = (struct sbp2scsi_host_info *)kmalloc(sizeof(struct sbp2scsi_host_info), GFP_KERNEL);
976
977 if (hi != NULL) {
978
979 /*
980 * Initialize some host stuff
981 */
982 memset(hi, 0, sizeof(struct sbp2scsi_host_info));
983 INIT_LIST_HEAD(&hi->list);
984 INIT_LIST_HEAD(&hi->sbp2_req_inuse);
985 INIT_LIST_HEAD(&hi->sbp2_req_free);
986 hi->host = host;
987 hi->sbp2_command_lock = SPIN_LOCK_UNLOCKED;
988 hi->sbp2_request_packet_lock = SPIN_LOCK_UNLOCKED;
989
990 /*
991 * Create our request packet pool (pool of packets for use in I/O)
992 */
993 if (sbp2util_create_request_packet_pool(hi)) {
994 SBP2_ERR("sbp2: sbp2util_create_request_packet_pool failed!");
995 return;
996 }
997
998 sbp2_spin_lock(&sbp2_host_info_lock, flags);
999 list_add_tail(&hi->list, &sbp2_host_info_list);
1000 sbp2_host_count++;
1001 sbp2_spin_unlock(&sbp2_host_info_lock, flags);
1002
1003 /*
1004 * Initialize us to bus reset in progress
1005 */
1006 hi->bus_reset_in_progress = 1;
1007
1008 /*
1009 * Register our host with the SCSI stack.
1010 */
1011 sbp2scsi_register_scsi_host(hi);
1012
1013 /*
1014 * Start our kernel thread to deal with sbp2 device detection
1015 */
1016 init_waitqueue_head(&hi->sbp2_detection_wait);
1017 hi->sbp2_detection_pid = 0;
1018 hi->sbp2_detection_pid = kernel_thread(sbp2_detection_thread, hi, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
1019
1020 }
1021
1022 return;
1023 }
1024
1025 /*
1026 * This fuction returns a host info structure from the host structure,
1027 * in case we have multiple hosts
1028 */
1029 static struct sbp2scsi_host_info *sbp2_find_host_info(struct hpsb_host *host)
1030 {
1031 struct list_head *lh;
1032 struct sbp2scsi_host_info *hi;
1033
1034 list_for_each (lh, &sbp2_host_info_list) {
1035 hi = list_entry(lh, struct sbp2scsi_host_info, list);
1036 if (hi->host == host) {
1037 return hi;
1038 }
1039 }
1040
1041 return(NULL);
1042 }
1043
1044 /*
1045 * This function is called when the host is removed
1046 */
1047 static void sbp2_remove_host(struct hpsb_host *host)
1048 {
1049 struct sbp2scsi_host_info *hi;
1050 int i;
1051 unsigned long flags;
1052
1053 SBP2_DEBUG("sbp2: sbp2_remove_host");
1054
1055 sbp2_spin_lock(&sbp2_host_info_lock, flags);
1056 hi = sbp2_find_host_info(host);
1057
1058 if (hi != NULL) {
1059
1060 /*
1061 * Need to remove any attached SBP-2 devices. Also make sure to logout of all devices.
1062 */
1063 for (i=0; i<SBP2SCSI_MAX_SCSI_IDS; i++) {
1064 if (hi->scsi_id[i]) {
1065 sbp2_logout_device(hi, hi->scsi_id[i]);
1066 hi->scsi_id[i]->validated = 0;
1067 }
1068 }
1069
1070 sbp2_remove_unvalidated_devices(hi);
1071
1072 list_del(&hi->list);
1073 sbp2_host_count--;
1074 }
1075 sbp2_spin_unlock(&sbp2_host_info_lock, flags);
1076
1077 if (hi == NULL) {
1078 SBP2_ERR("sbp2: attempt to remove unknown host %p", host);
1079 return;
1080 }
1081
1082 /*
1083 * Remove the packet pool (release the packets)
1084 */
1085 sbp2util_remove_request_packet_pool(hi);
1086
1087 /*
1088 * Kill our detection thread
1089 */
1090 if (hi->sbp2_detection_pid >= 0) {
1091 kill_proc(hi->sbp2_detection_pid, SIGINT, 1);
1092 }
1093
1094 /*
1095 * Give the detection thread a little time to exit
1096 */
1097 current->state = TASK_INTERRUPTIBLE;
1098 schedule_timeout(HZ); /* 1 second delay */
1099
1100 kfree(hi);
1101 hi = NULL;
1102
1103 return;
1104 }
1105
1106 /*
1107 * This is our sbp2 detection thread. It is signalled when bus resets occur
1108 * so that we can find and initialize any sbp2 devices.
1109 */
1110 static int sbp2_detection_thread(void *__hi)
1111 {
1112 struct sbp2scsi_host_info *hi = (struct sbp2scsi_host_info *)__hi;
1113
1114 SBP2_DEBUG("sbp2: sbp2_detection_thread");
1115
1116 lock_kernel();
1117
1118 /*
1119 * This thread doesn't need any user-level access,
1120 * so get rid of all our resources
1121 */
1122 #if LINUX_VERSION_CODE > 0x20300
1123 daemonize();
1124 #endif
1125
1126 /*
1127 * Set-up a nice name
1128 */
1129 strcpy(current->comm, SBP2_DEVICE_NAME);
1130
1131 unlock_kernel();
1132
1133 while ((!signal_pending(current)) && hi) {
1134
1135 /*
1136 * Process our bus reset now
1137 */
1138 if (hi) {
1139 MOD_INC_USE_COUNT;
1140 sbp2_bus_reset_handler(hi);
1141 MOD_DEC_USE_COUNT;
1142 }
1143
1144 /*
1145 * Sleep until next bus reset
1146 */
1147 if (hi) {
1148 interruptible_sleep_on(&hi->sbp2_detection_wait);
1149 }
1150 }
1151
1152 return(0);
1153 }
1154
1155 /*
1156 * This function is where we first pull the node unique ids, and then allocate memory and register
1157 * a SBP-2 device
1158 */
1159 static int sbp2_start_device(struct sbp2scsi_host_info *hi, int node_id)
1160 {
1161 u64 node_unique_id;
1162 struct scsi_id_instance_data *scsi_id = NULL;
1163 struct node_entry *ne;
1164 int i;
1165
1166 SBP2_DEBUG("sbp2: sbp2_start_device");
1167
1168 /* XXX: This will go away once we start using the nodemgr's
1169 * feature subscription API. */
1170 ne = hpsb_nodeid_get_entry(node_id|(hi->host->node_id & BUS_MASK));
1171 if (!ne) {
1172 HPSB_ERR("sbp2: Could not find device node");
1173 return -ENXIO;
1174 }
1175
1176 node_unique_id = ne->guid;
1177
1178 /*
1179 * First, we need to find out whether this is a "new" SBP-2 device plugged in, or one that already
1180 * exists and is initialized. We do this by looping through our scsi id instance data structures
1181 * looking for matching node unique ids.
1182 */
1183 for (i=0; i<SBP2SCSI_MAX_SCSI_IDS; i++) {
1184
1185 if (hi->scsi_id[i]) {
1186
1187 if (hi->scsi_id[i]->node_unique_id == node_unique_id) {
1188
1189 /*
1190 * Update our node id
1191 */
1192 hi->scsi_id[i]->node_id = node_id;
1193
1194 /*
1195 * Mark the device as validated, since it still exists on the bus
1196 */
1197 hi->scsi_id[i]->validated = 1;
1198 SBP2_DEBUG("sbp2: SBP-2 device re-validated, SCSI ID = %x", (unsigned int) i);
1199
1200 /*
1201 * Reconnect to the sbp-2 device
1202 */
1203 if (sbp2_reconnect_device(hi, hi->scsi_id[i])) {
1204
1205 /*
1206 * Ok, reconnect has failed. Perhaps we didn't reconnect fast enough. Try
1207 * doing a regular login.
1208 */
1209 if (sbp2_login_device(hi, hi->scsi_id[i])) {
1210
1211 /*
1212 * Login failed too... so, just mark him as unvalidated, so that he gets cleaned up
1213 * later
1214 */
1215 SBP2_ERR("sbp2: sbp2_reconnect_device failed!");
1216 hi->scsi_id[i]->validated = 0;
1217 }
1218 }
1219
1220 if (hi->scsi_id[i]->validated) {
1221
1222 /*
1223 * Set max retries to something large on the device
1224 */
1225 sbp2_set_busy_timeout(hi, hi->scsi_id[i]);
1226
1227 /*
1228 * Do a SBP-2 fetch agent reset
1229 */
1230 sbp2_agent_reset(hi, hi->scsi_id[i], 0);
1231
1232 /*
1233 * Get the max speed and packet size that we can use
1234 */
1235 sbp2_max_speed_and_size(hi, hi->scsi_id[i]);
1236
1237 }
1238
1239 /*
1240 * Nothing more to do, since we found the device
1241 */
1242 return(0);
1243
1244 }
1245 }
1246 }
1247
1248 /*
1249 * This really is a "new" device plugged in. Let's allocate memory for our scsi id instance data
1250 */
1251 scsi_id = (struct scsi_id_instance_data *)kmalloc(sizeof(struct scsi_id_instance_data),
1252 GFP_KERNEL);
1253 if (!scsi_id)
1254 goto alloc_fail_first;
1255 memset(scsi_id, 0, sizeof(struct scsi_id_instance_data));
1256
1257 /* Login FIFO DMA */
1258 scsi_id->login_response =
1259 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_login_response),
1260 &scsi_id->login_response_dma);
1261 if (!scsi_id->login_response)
1262 goto alloc_fail;
1263 SBP2_DMA_ALLOC("consistent DMA region for login FIFO");
1264
1265 /* Reconnect ORB DMA */
1266 scsi_id->reconnect_orb =
1267 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_reconnect_orb),
1268 &scsi_id->reconnect_orb_dma);
1269 if (!scsi_id->reconnect_orb)
1270 goto alloc_fail;
1271 SBP2_DMA_ALLOC("consistent DMA region for reconnect ORB");
1272
1273 /* Logout ORB DMA */
1274 scsi_id->logout_orb =
1275 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_logout_orb),
1276 &scsi_id->logout_orb_dma);
1277 if (!scsi_id->logout_orb)
1278 goto alloc_fail;
1279 SBP2_DMA_ALLOC("consistent DMA region for logout ORB");
1280
1281 /* Login ORB DMA */
1282 scsi_id->login_orb =
1283 pci_alloc_consistent(hi->host->pdev, sizeof(struct sbp2_login_orb),
1284 &scsi_id->login_orb_dma);
1285 if (scsi_id->login_orb == NULL) {
1286 alloc_fail:
1287 if (scsi_id->logout_orb) {
1288 pci_free_consistent(hi->host->pdev,
1289 sizeof(struct sbp2_logout_orb),
1290 scsi_id->logout_orb,
1291 scsi_id->logout_orb_dma);
1292 SBP2_DMA_FREE("logout ORB DMA");
1293 }
1294
1295 if (scsi_id->reconnect_orb) {
1296 pci_free_consistent(hi->host->pdev,
1297 sizeof(struct sbp2_reconnect_orb),
1298 scsi_id->reconnect_orb,
1299 scsi_id->reconnect_orb_dma);
1300 SBP2_DMA_FREE("reconnect ORB DMA");
1301 }
1302
1303 if (scsi_id->login_response) {
1304 pci_free_consistent(hi->host->pdev,
1305 sizeof(struct sbp2_login_response),
1306 scsi_id->login_response,
1307 scsi_id->login_response_dma);
1308 SBP2_DMA_FREE("login FIFO DMA");
1309 }
1310
1311 kfree(scsi_id);
1312 alloc_fail_first:
1313 SBP2_ERR ("sbp2: Could not allocate memory for scsi_id");
1314 return(-ENOMEM);
1315 }
1316 SBP2_DMA_ALLOC("consistent DMA region for login ORB");
1317
1318 /*
1319 * Initialize some of the fields in this structure
1320 */
1321 scsi_id->node_id = node_id;
1322 scsi_id->node_unique_id = node_unique_id;
1323 scsi_id->validated = 1;
1324 scsi_id->speed_code = SPEED_100;
1325 scsi_id->max_payload_size = MAX_PAYLOAD_S100;
1326
1327 init_waitqueue_head(&scsi_id->sbp2_login_wait);
1328
1329 /*
1330 * Initialize structures needed for the command orb pool.
1331 */
1332 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_inuse);
1333 INIT_LIST_HEAD(&scsi_id->sbp2_command_orb_completed);
1334 scsi_id->sbp2_command_orb_lock = SPIN_LOCK_UNLOCKED;
1335 scsi_id->sbp2_total_command_orbs = 0;
1336
1337 /*
1338 * Make sure that we've gotten ahold of the sbp2 management agent address. Also figure out the
1339 * command set being used (SCSI or RBC).
1340 */
1341 if (sbp2_parse_unit_directory(hi, scsi_id)) {
1342 SBP2_ERR("sbp2: Error while parsing sbp2 unit directory");
1343 hi->scsi_id[i]->validated = 0;
1344 return(-EIO);
1345 }
1346
1347 scsi_id->sbp2_total_command_orbs = SBP2_MAX_COMMAND_ORBS;
1348
1349 /*
1350 * Knock the total command orbs down if we are serializing I/O
1351 */
1352 if (serialize_io) {
1353 scsi_id->sbp2_total_command_orbs = 2; /* one extra for good measure */
1354 }
1355
1356 /*
1357 * Allocate some extra command orb structures for devices with 128KB limit
1358 */
1359 if (scsi_id->sbp2_firmware_revision == SBP2_128KB_BROKEN_FIRMWARE) {
1360 scsi_id->sbp2_total_command_orbs *= 4;
1361 }
1362
1363 /*
1364 * Create our command orb pool
1365 */
1366 if (sbp2util_create_command_orb_pool(scsi_id, hi)) {
1367 SBP2_ERR("sbp2: sbp2util_create_command_orb_pool failed!");
1368 hi->scsi_id[i]->validated = 0;
1369 return (-ENOMEM);
1370 }
1371
1372 /*
1373 * Find an empty spot to stick our scsi id instance data.
1374 */
1375 for (i=0; i<SBP2SCSI_MAX_SCSI_IDS; i++) {
1376 if (!hi->scsi_id[i]) {
1377 hi->scsi_id[i] = scsi_id;
1378 SBP2_DEBUG("sbp2: New SBP-2 device inserted, SCSI ID = %x", (unsigned int) i);
1379 break;
1380 }
1381 }
1382
1383 /*
1384 * Make sure we are not out of space
1385 */
1386 if (i >= SBP2SCSI_MAX_SCSI_IDS) {
1387 SBP2_ERR("sbp2: No slots left for SBP-2 device");
1388 hi->scsi_id[i]->validated = 0;
1389 return(-EBUSY);
1390 }
1391
1392 /*
1393 * Login to the sbp-2 device
1394 */
1395 if (sbp2_login_device(hi, hi->scsi_id[i])) {
1396
1397 /*
1398 * Login failed... so, just mark him as unvalidated, so that he gets cleaned up later
1399 */
1400 SBP2_ERR("sbp2: sbp2_login_device failed");
1401 hi->scsi_id[i]->validated = 0;
1402 }
1403
1404 if (hi->scsi_id[i]->validated) {
1405
1406 /*
1407 * Set max retries to something large on the device
1408 */
1409 sbp2_set_busy_timeout(hi, hi->scsi_id[i]);
1410
1411 /*
1412 * Do a SBP-2 fetch agent reset
1413 */
1414 sbp2_agent_reset(hi, hi->scsi_id[i], 0);
1415
1416 /*
1417 * Get the max speed and packet size that we can use
1418 */
1419 sbp2_max_speed_and_size(hi, hi->scsi_id[i]);
1420
1421 }
1422
1423 return(0);
1424 }
1425
1426 /*
1427 * This function tries to determine if a device is a valid SBP-2 device
1428 */
1429 static int sbp2_check_device(struct sbp2scsi_host_info *hi, int node_id)
1430 {
1431 quadlet_t unit_spec_id_data = 0, unit_sw_ver_data = 0;
1432 quadlet_t unit_directory_length, current_quadlet;
1433 u64 unit_directory_addr, current_addr;
1434 unsigned int i, length;
1435
1436 SBP2_DEBUG("sbp2: sbp2_check_device");
1437
1438 /*
1439 * Let's try and read the unit spec id and unit sw ver to determine if this is an SBP2 device...
1440 */
1441
1442 if (sbp2util_unit_directory(hi, LOCAL_BUS | node_id, &unit_directory_addr)) {
1443 SBP2_DEBUG("sbp2: Error reading unit directory address - bad status");
1444 return(-EIO);
1445 }
1446
1447 /*
1448 * Read the size of the unit directory
1449 */
1450 if (sbp2util_read_quadlet(hi, LOCAL_BUS | node_id, unit_directory_addr,
1451 &unit_directory_length)) {
1452 SBP2_DEBUG("sbp2: Error reading root directory length - bad status");
1453 return(-EIO);
1454 }
1455
1456 current_addr = unit_directory_addr;
1457 length = be32_to_cpu(unit_directory_length) >> 16;
1458
1459 /*
1460 * Now, step through the unit directory and look for the unit_spec_ID and the unit_sw_version
1461 */
1462 for (i=0; i < length; i++) {
1463
1464 current_addr += 4;
1465
1466 if (sbp2util_read_quadlet(hi, LOCAL_BUS | node_id, current_addr, ¤t_quadlet)) {
1467 SBP2_DEBUG("sbp2: Error reading at address 0x%08x%08x - bad status",
1468 (unsigned int) ((current_addr) >> 32), (unsigned int) ((current_addr) & 0xffffffff));
1469 return(-EIO);
1470 }
1471
1472 /*
1473 * Check for unit_spec_ID tag
1474 */
1475 if ((be32_to_cpu(current_quadlet) >> 24) == SBP2_UNIT_SPEC_ID_KEY) {
1476 unit_spec_id_data = current_quadlet;
1477 SBP2_DEBUG("sbp2: Node %x, unit spec id = %x", (LOCAL_BUS | node_id),
1478 (unsigned int) be32_to_cpu(unit_spec_id_data));
1479 }
1480
1481 /*
1482 * Check for unit_sw_version tag
1483 */
1484 if ((be32_to_cpu(current_quadlet) >> 24) == SBP2_UNIT_SW_VERSION_KEY) {
1485 unit_sw_ver_data = current_quadlet;
1486 SBP2_DEBUG("sbp2: Node %x, unit sw version = %x", (LOCAL_BUS | node_id),
1487 (unsigned int) be32_to_cpu(unit_sw_ver_data));
1488 }
1489 }
1490
1491 /*
1492 * Validate unit spec id and unit sw ver to see if this is an SBP-2 device
1493 */
1494 if ((be32_to_cpu(unit_spec_id_data) != SBP2_UNIT_SPEC_ID_ENTRY) ||
1495 (be32_to_cpu(unit_sw_ver_data) != SBP2_SW_VERSION_ENTRY)) {
1496
1497 /*
1498 * Not an sbp2 device
1499 */
1500 return(-ENXIO);
1501 }
1502
1503 /*
1504 * This device is a valid SBP-2 device
1505 */
1506 SBP2_INFO("sbp2: Node 0x%04x, Found SBP-2 device", (LOCAL_BUS | node_id));
1507 return(0);
1508 }
1509
1510 /*
1511 * This function removes (cleans-up after) any unvalidated sbp2 devices
1512 */
1513 static void sbp2_remove_unvalidated_devices(struct sbp2scsi_host_info *hi)
1514 {
1515 int i;
1516
1517 /*
1518 * Loop through and free any unvalidated scsi id instance data structures
1519 */
1520 for (i=0; i<SBP2SCSI_MAX_SCSI_IDS; i++) {
1521 if (hi->scsi_id[i]) {
1522 if (!hi->scsi_id[i]->validated) {
1523
1524 /*
1525 * Complete any pending commands with selection timeout
1526 */
1527 sbp2scsi_complete_all_commands(hi, hi->scsi_id[i], DID_NO_CONNECT);
1528
1529 /*
1530 * Clean up any other structures
1531 */
1532 if (hi->scsi_id[i]->sbp2_total_command_orbs) {
1533 sbp2util_remove_command_orb_pool(hi->scsi_id[i], hi);
1534 }
1535 if (hi->scsi_id[i]->login_response) {
1536 pci_free_consistent(hi->host->pdev,
1537 sizeof(struct sbp2_login_response),
1538 hi->scsi_id[i]->login_response,
1539 hi->scsi_id[i]->login_response_dma);
1540 SBP2_DMA_FREE("single login FIFO");
1541 }
1542
1543 if (hi->scsi_id[i]->login_orb) {
1544 pci_free_consistent(hi->host->pdev,
1545 sizeof(struct sbp2_login_orb),
1546 hi->scsi_id[i]->login_orb,
1547 hi->scsi_id[i]->login_orb_dma);
1548 SBP2_DMA_FREE("single login ORB");
1549 }
1550
1551 if (hi->scsi_id[i]->reconnect_orb) {
1552 pci_free_consistent(hi->host->pdev,
1553 sizeof(struct sbp2_reconnect_orb),
1554 hi->scsi_id[i]->reconnect_orb,
1555 hi->scsi_id[i]->reconnect_orb_dma);
1556 SBP2_DMA_FREE("single reconnect orb");
1557 }
1558
1559 if (hi->scsi_id[i]->logout_orb) {
1560 pci_free_consistent(hi->host->pdev,
1561 sizeof(struct sbp2_logout_orb),
1562 hi->scsi_id[i]->logout_orb,
1563 hi->scsi_id[i]->reconnect_orb_dma);
1564 SBP2_DMA_FREE("single logout orb");
1565 }
1566
1567 kfree(hi->scsi_id[i]);
1568 hi->scsi_id[i] = NULL;
1569 SBP2_DEBUG("sbp2: Unvalidated SBP-2 device removed, SCSI ID = %x", (unsigned int) i);
1570 }
1571 }
1572 }
1573
1574 return;
1575 }
1576
1577 /*
1578 * This function is our reset handler. It is run out of a thread, since we get
1579 * notified of a bus reset from a bh (or interrupt).
1580 */
1581 static void sbp2_bus_reset_handler(void *context)
1582 {
1583 struct sbp2scsi_host_info *hi = context;
1584 quadlet_t signature_data;
1585 int i;
1586 unsigned long flags;
1587 struct scsi_id_instance_data *scsi_id;
1588
1589 SBP2_DEBUG("sbp2: sbp2_bus_reset_handler");
1590
1591 /*
1592 * TODO. Check and keep track of generation number of all requests, in case a
1593 * bus reset occurs while trying to find and login to SBP-2 devices.
1594 */
1595
1596 /*
1597 * First thing to do. Invalidate all SBP-2 devices. This is needed so that
1598 * we stop sending down I/O requests to the device, and also so that we can
1599 * figure out which devices have disappeared after a bus reset.
1600 */
1601 for (i=0; i<SBP2SCSI_MAX_SCSI_IDS; i++) {
1602 if (hi->scsi_id[i]) {
1603 hi->scsi_id[i]->validated = 0;
1604 }
1605 }
1606
1607 /*
1608 * Give the sbp2 devices a little time to recover after the bus reset
1609 */
1610 current->state = TASK_INTERRUPTIBLE;
1611 schedule_timeout(HZ/2); /* 1/2 second delay */
1612
1613 /*
1614 * Spit out what we know from the host
1615 */
1616 SBP2_DEBUG("host: node_count = %x", (unsigned int) hi->host->node_count);
1617 SBP2_DEBUG("host: selfid_count = %x", (unsigned int) hi->host->selfid_count);
1618 SBP2_DEBUG("host: node_id = %x", (unsigned int) hi->host->node_id);
1619 SBP2_DEBUG("host: irm_id = %x", (unsigned int) hi->host->irm_id);
1620 SBP2_DEBUG("host: busmgr_id = %x", (unsigned int) hi->host->busmgr_id);
1621 SBP2_DEBUG("host: is_root = %x", (unsigned int) hi->host->is_root);
1622 SBP2_DEBUG("host: is_cycmst = %x", (unsigned int) hi->host->is_cycmst);
1623 SBP2_DEBUG("host: is_irm = %x", (unsigned int) hi->host->is_irm);
1624 SBP2_DEBUG("host: is_busmgr = %x", (unsigned int) hi->host->is_busmgr);
1625
1626 /*
1627 * Let's try and figure out which devices out there are SBP-2 devices! Loop through all
1628 * nodes out there.
1629 */
1630 for (i=0; i<hi->host->node_count; i++) {
1631
1632 /*
1633 * Don't read from ourselves!
1634 */
1635 if (i != ((hi->host->node_id) & NODE_MASK)) {
1636
1637 /*
1638 * Try and send a request for a config rom signature. This is expected to fail for
1639 * some nodes, as they might be repeater phys or not be initialized.
1640 */
1641 if (!sbp2util_read_quadlet(hi, LOCAL_BUS | i, CONFIG_ROM_SIGNATURE_ADDRESS, &signature_data)) {
1642
1643 if (be32_to_cpu(signature_data) == IEEE1394_CONFIG_ROM_SIGNATURE) {
1644
1645 /*
1646 * Hey, we've got a valid responding IEEE1394 node. Need to now see if it's an SBP-2 device
1647 */
1648 if (!sbp2_check_device(hi, i)) {
1649
1650 /*
1651 * Found an SBP-2 device. Now, actually start the device.
1652 */
1653 sbp2_start_device(hi, i);
1654 }
1655 }
1656 }
1657 }
1658 }
1659
1660 /*
1661 * This code needs protection
1662 */
1663 sbp2_spin_lock(&hi->sbp2_command_lock, flags);
1664
1665 /*
1666 * Ok, we've discovered and re-validated all SBP-2 devices out there. Let's remove structures of all
1667 * devices not re-validated (meaning they've been removed).
1668 */
1669 sbp2_remove_unvalidated_devices(hi);
1670
1671 /*
1672 * Complete any pending commands with busy (so they get retried) and remove them from our queue
1673 */
1674 for (i=0; i<SBP2SCSI_MAX_SCSI_IDS; i++) {
1675 if (hi->scsi_id[i]) {
1676 sbp2scsi_complete_all_commands(hi, hi->scsi_id[i], DID_BUS_BUSY);
1677 }
1678 }
1679
1680 /*
1681 * Now, note that the bus reset is complete (finally!)
1682 */
1683 hi->bus_reset_in_progress = 0;
1684
1685 /*
1686 * Deal with the initial scsi bus scan if needed (since we only now know if there are
1687 * any sbp2 devices attached)
1688 */
1689 if (!no_bus_scan && !hi->initial_scsi_bus_scan_complete && hi->bus_scan_SCpnt) {
1690
1691 hi->initial_scsi_bus_scan_complete = 1;
1692 scsi_id = hi->scsi_id[hi->bus_scan_SCpnt->target];
1693
1694 /*
1695 * If the sbp2 device exists, then let's now execute the command.
1696 * If not, then just complete it as a selection time-out.
1697 */
1698 if (scsi_id) {
1699 if (sbp2_send_command(hi, scsi_id, hi->bus_scan_SCpnt, hi->bus_scan_done)) {
1700 SBP2_ERR("sbp2: Error sending SCSI command");
1701 sbp2scsi_complete_command(hi, scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT,
1702 hi->bus_scan_SCpnt, hi->bus_scan_done);
1703 }
1704 } else {
1705 void (*done)(Scsi_Cmnd *) = hi->bus_scan_done;
1706 hi->bus_scan_SCpnt->result = DID_NO_CONNECT << 16;
1707 done (hi->bus_scan_SCpnt);
1708 }
1709 }
1710
1711 sbp2_spin_unlock(&hi->sbp2_command_lock, flags);
1712
1713 return;
1714 }
1715
1716
1717 /*
1718 * This is called from the host's bh when a bus reset is complete. We wake up our detection thread
1719 * to deal with the reset
1720 */
1721 static void sbp2_host_reset(struct hpsb_host *host)
1722 {
1723 unsigned long flags;
1724 struct sbp2scsi_host_info *hi;
1725 int i;
1726
1727 SBP2_INFO("sbp2: IEEE-1394 bus reset");
1728 sbp2_spin_lock(&sbp2_host_info_lock, flags);
1729 hi = sbp2_find_host_info(host);
1730
1731 if (hi != NULL) {
1732
1733 /*
1734 * Wake up our detection thread, only if it's not already handling a reset
1735 */
1736 if (!hi->bus_reset_in_progress) {
1737
1738 /*
1739 * First thing to do. Invalidate all SBP-2 devices. This is needed so that
1740 * we stop sending down I/O requests to the device, and also so that we can
1741 * figure out which devices have disappeared after a bus reset.
1742 */
1743 for (i=0; i<SBP2SCSI_MAX_SCSI_IDS; i++) {
1744 if (hi->scsi_id[i]) {
1745 hi->scsi_id[i]->validated = 0;
1746 }
1747 }
1748
1749 hi->bus_reset_in_progress = 1;
1750
1751 wake_up(&hi->sbp2_detection_wait);
1752 }
1753 }
1754 sbp2_spin_unlock(&sbp2_host_info_lock, flags);
1755 return;
1756 }
1757
1758 /* XXX: How best to handle these with DMA interface? */
1759
1760 #if 0
1761 /*
1762 * This function deals with physical dma write requests (for adapters that do not support
1763 * physical dma in hardware).
1764 */
1765 static int sbp2_handle_physdma_write(struct hpsb_host *host, int nodeid, quadlet_t *data,
1766 u64 addr, unsigned int length)
1767 {
1768
1769 /*
1770 * Manually put the data in the right place.
1771 */
1772 memcpy(bus_to_virt((u32)addr), data, length);
1773 return(RCODE_COMPLETE);
1774 }
1775
1776 /*
1777 * This function deals with physical dma read requests (for adapters that do not support
1778 * physical dma in hardware).
1779 */
1780 static int sbp2_handle_physdma_read(struct hpsb_host *host, int nodeid, quadlet_t *data,
1781 u64 addr, unsigned int length)
1782 {
1783
1784 /*
1785 * Grab data from memory and send a read response.
1786 */
1787 memcpy(data, bus_to_virt((u32)addr), length);
1788 return(RCODE_COMPLETE);
1789 }
1790 #endif
1791
1792 /**************************************
1793 * SBP-2 protocol related section
1794 **************************************/
1795
1796 /*
1797 * This function is called in order to login to a particular SBP-2 device, after a bus reset
1798 */
1799 static int sbp2_login_device(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id)
1800 {
1801 quadlet_t data[2];
1802 unsigned long flags;
1803
1804 SBP2_DEBUG("sbp2: sbp2_login_device");
1805
1806 if (!scsi_id->login_orb) {
1807 SBP2_DEBUG("sbp2: sbp2_login_device: login_orb not alloc'd!");
1808 return(-EIO);
1809 }
1810
1811 /*
1812 * Set-up login ORB
1813 */
1814 scsi_id->login_orb->password_hi = 0; /* Assume no password */
1815 scsi_id->login_orb->password_lo = 0;
1816 SBP2_DEBUG("sbp2: sbp2_login_device: password_hi/lo initialized");
1817 #ifdef SBP2_NEED_LOGIN_DESCRIPTOR_WORKAROUND
1818 scsi_id->login_orb->login_response_lo = cpu_to_le32(scsi_id->login_response_dma);
1819 scsi_id->login_orb->login_response_hi = cpu_to_le32(ORB_SET_NODE_ID(hi->host->node_id));
1820 #else
1821 scsi_id->login_orb->login_response_lo = scsi_id->login_response_dma;
1822 scsi_id->login_orb->login_response_hi = ORB_SET_NODE_ID(hi->host->node_id);
1823 #endif
1824 SBP2_DEBUG("sbp2: sbp2_login_device: login_response_hi/lo initialized");
1825 scsi_id->login_orb->lun_misc = ORB_SET_FUNCTION(LOGIN_REQUEST);
1826 scsi_id->login_orb->lun_misc |= ORB_SET_RECONNECT(0); /* One second reconnect time */
1827 scsi_id->login_orb->lun_misc |= ORB_SET_EXCLUSIVE(1); /* Exclusive access to device */
1828 scsi_id->login_orb->lun_misc |= ORB_SET_NOTIFY(1); /* Notify us of login complete */
1829 SBP2_DEBUG("sbp2: sbp2_login_device: lun_misc initialized");
1830 scsi_id->login_orb->passwd_resp_lengths = ORB_SET_LOGIN_RESP_LENGTH(sizeof(struct sbp2_login_response));
1831 SBP2_DEBUG("sbp2: sbp2_login_device: passwd_resp_lengths initialized");
1832 #ifdef SBP2_NEED_LOGIN_DESCRIPTOR_WORKAROUND
1833 scsi_id->login_orb->status_FIFO_lo = cpu_to_le32((u32)SBP2_STATUS_FIFO_ADDRESS_LO);
1834 scsi_id->login_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) | cpu_to_le16(SBP2_STATUS_FIFO_ADDRESS_HI));
1835 #else
1836 scsi_id->login_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO;
1837 scsi_id->login_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) | SBP2_STATUS_FIFO_ADDRESS_HI);
1838 #endif
1839 SBP2_DEBUG("sbp2: sbp2_login_device: status FIFO initialized");
1840
1841 /*
1842 * Byte swap ORB if necessary
1843 */
1844 sbp2util_cpu_to_be32_buffer(scsi_id->login_orb, sizeof(struct sbp2_login_orb));
1845
1846 SBP2_DEBUG("sbp2: sbp2_login_device: orb byte-swapped");
1847
1848 /*
1849 * Initialize login response and status fifo
1850 */
1851 memset(scsi_id->login_response, 0, sizeof(struct sbp2_login_response));
1852 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
1853
1854 SBP2_DEBUG("sbp2: sbp2_login_device: login_response/status FIFO memset");
1855
1856 /*
1857 * Ok, let's write to the target's management agent register
1858 */
1859 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1860 data[1] = scsi_id->login_orb_dma;
1861 sbp2util_cpu_to_be32_buffer(data, 8);
1862
1863 SBP2_DEBUG("sbp2: sbp2_login_device: prepared to write");
1864
1865 hpsb_write(hi->host, LOCAL_BUS | scsi_id->node_id, scsi_id->sbp2_management_agent_addr, data, 8);
1866
1867 /*
1868 * Wait for login status... but, only if the device has not already logged-in (some devices are fast)
1869 */
1870
1871 SBP2_DEBUG("sbp2: sbp2_login_device: written");
1872 save_flags(flags);
1873 cli();
1874 if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
1875 interruptible_sleep_on_timeout(&scsi_id->sbp2_login_wait, 10*HZ); /* 10 second timeout */
1876 }
1877 restore_flags(flags);
1878
1879 SBP2_DEBUG("sbp2: sbp2_login_device: initial check");
1880
1881 /*
1882 * Match status to the login orb. If they do not match, it's probably because the login timed-out
1883 */
1884 if (scsi_id->status_block.ORB_offset_lo != scsi_id->login_orb_dma) {
1885 SBP2_ERR("sbp2: Error logging into SBP-2 device - login timed-out");
1886 return(-EIO);
1887 }
1888
1889 SBP2_DEBUG("sbp2: sbp2_login_device: second check");
1890
1891 /*
1892 * Check status
1893 */
1894 if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
1895 STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
1896 STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
1897
1898 SBP2_ERR("sbp2: Error logging into SBP-2 device - login failed");
1899 return(-EIO);
1900 }
1901
1902 /*
1903 * Byte swap the login response, for use when reconnecting or logging out.
1904 */
1905 sbp2util_cpu_to_be32_buffer(scsi_id->login_response, sizeof(struct sbp2_login_response));
1906
1907 /*
1908 * Grab our command block agent address from the login response
1909 */
1910 SBP2_DEBUG("sbp2: command_block_agent_hi = %x", (unsigned int)scsi_id->login_response->command_block_agent_hi);
1911 SBP2_DEBUG("sbp2: command_block_agent_lo = %x", (unsigned int)scsi_id->login_response->command_block_agent_lo);
1912
1913 scsi_id->sbp2_command_block_agent_addr = ((u64)scsi_id->login_response->command_block_agent_hi) << 32;
1914 scsi_id->sbp2_command_block_agent_addr |= ((u64)scsi_id->login_response->command_block_agent_lo);
1915 scsi_id->sbp2_command_block_agent_addr &= 0x0000ffffffffffffULL;
1916
1917 SBP2_INFO("sbp2: Logged into SBP-2 device");
1918
1919 return(0);
1920
1921 }
1922
1923 /*
1924 * This function is called in order to logout from a particular SBP-2 device, usually called during driver
1925 * unload
1926 */
1927 static int sbp2_logout_device(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id)
1928 {
1929 quadlet_t data[2];
1930
1931 SBP2_DEBUG("sbp2: sbp2_logout_device");
1932
1933 /*
1934 * Set-up logout ORB
1935 */
1936 scsi_id->logout_orb->reserved1 = 0x0;
1937 scsi_id->logout_orb->reserved2 = 0x0;
1938 scsi_id->logout_orb->reserved3 = 0x0;
1939 scsi_id->logout_orb->reserved4 = 0x0;
1940 scsi_id->logout_orb->login_ID_misc = ORB_SET_FUNCTION(LOGOUT_REQUEST);
1941 scsi_id->logout_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1942 scsi_id->logout_orb->login_ID_misc |= ORB_SET_NOTIFY(1); /* Notify us when complete */
1943 scsi_id->logout_orb->reserved5 = 0x0;
1944 #ifdef SBP2_NEED_LOGIN_DESCRIPTOR_WORKAROUND
1945 scsi_id->logout_orb->status_FIFO_lo = cpu_to_le32((u32)SBP2_STATUS_FIFO_ADDRESS_LO);
1946 scsi_id->logout_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) | cpu_to_le16(SBP2_STATUS_FIFO_ADDRESS_HI));
1947 #else
1948 scsi_id->logout_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO;
1949 scsi_id->logout_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) | SBP2_STATUS_FIFO_ADDRESS_HI);
1950 #endif
1951
1952 /*
1953 * Byte swap ORB if necessary
1954 */
1955 sbp2util_cpu_to_be32_buffer(scsi_id->logout_orb, sizeof(struct sbp2_logout_orb));
1956
1957 /*
1958 * Ok, let's write to the target's management agent register
1959 */
1960 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
1961 data[1] = scsi_id->logout_orb_dma;
1962 sbp2util_cpu_to_be32_buffer(data, 8);
1963
1964 hpsb_write(hi->host, LOCAL_BUS | scsi_id->node_id, scsi_id->sbp2_management_agent_addr, data, 8);
1965
1966 /*
1967 * Wait for device to logout...
1968 */
1969 interruptible_sleep_on_timeout(&scsi_id->sbp2_login_wait, HZ); /* 1 second timeout */
1970
1971 SBP2_INFO("sbp2: Logged out of SBP-2 device");
1972
1973 return(0);
1974
1975 }
1976
1977 /*
1978 * This function is called in order to reconnect to a particular SBP-2 device, after a bus reset
1979 */
1980 static int sbp2_reconnect_device(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id)
1981 {
1982 quadlet_t data[2];
1983 unsigned long flags;
1984
1985 SBP2_DEBUG("sbp2: sbp2_reconnect_device");
1986
1987 /*
1988 * Set-up reconnect ORB
1989 */
1990 scsi_id->reconnect_orb->reserved1 = 0x0;
1991 scsi_id->reconnect_orb->reserved2 = 0x0;
1992 scsi_id->reconnect_orb->reserved3 = 0x0;
1993 scsi_id->reconnect_orb->reserved4 = 0x0;
1994 scsi_id->reconnect_orb->login_ID_misc = ORB_SET_FUNCTION(RECONNECT_REQUEST);
1995 scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_LOGIN_ID(scsi_id->login_response->length_login_ID);
1996 scsi_id->reconnect_orb->login_ID_misc |= ORB_SET_NOTIFY(1); /* Notify us when complete */
1997 scsi_id->reconnect_orb->reserved5 = 0x0;
1998 #ifdef SBP2_NEED_LOGIN_DESCRIPTOR_WORKAROUND
1999 scsi_id->reconnect_orb->status_FIFO_lo = cpu_to_le32((u32)SBP2_STATUS_FIFO_ADDRESS_LO);
2000 scsi_id->reconnect_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) | cpu_to_le16(SBP2_STATUS_FIFO_ADDRESS_HI));
2001 #else
2002 scsi_id->reconnect_orb->status_FIFO_lo = SBP2_STATUS_FIFO_ADDRESS_LO;
2003 scsi_id->reconnect_orb->status_FIFO_hi = (ORB_SET_NODE_ID(hi->host->node_id) | SBP2_STATUS_FIFO_ADDRESS_HI);
2004 #endif
2005
2006 /*
2007 * Byte swap ORB if necessary
2008 */
2009 sbp2util_cpu_to_be32_buffer(scsi_id->reconnect_orb, sizeof(struct sbp2_reconnect_orb));
2010
2011 /*
2012 * Initialize status fifo
2013 */
2014 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
2015
2016 /*
2017 * Ok, let's write to the target's management agent register
2018 */
2019 data[0] = ORB_SET_NODE_ID(hi->host->node_id);
2020 data[1] = scsi_id->reconnect_orb_dma;
2021 sbp2util_cpu_to_be32_buffer(data, 8);
2022
2023 hpsb_write(hi->host, LOCAL_BUS | scsi_id->node_id, scsi_id->sbp2_management_agent_addr, data, 8);
2024
2025 /*
2026 * Wait for reconnect status... but, only if the device has not already reconnected (some devices are fast)
2027 */
2028 save_flags(flags);
2029 cli();
2030 if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
2031 interruptible_sleep_on_timeout(&scsi_id->sbp2_login_wait, HZ); /* one second timeout */
2032 }
2033 restore_flags(flags);
2034
2035 /*
2036 * Match status to the reconnect orb. If they do not match, it's probably because the reconnect timed-out
2037 */
2038 if (scsi_id->status_block.ORB_offset_lo != scsi_id->reconnect_orb_dma) {
2039 SBP2_ERR("sbp2: Error reconnecting to SBP-2 device - reconnect timed-out");
2040 return(-EIO);
2041 }
2042
2043 /*
2044 * Check status
2045 */
2046 if (STATUS_GET_RESP(scsi_id->status_block.ORB_offset_hi_misc) ||
2047 STATUS_GET_DEAD_BIT(scsi_id->status_block.ORB_offset_hi_misc) ||
2048 STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
2049
2050 SBP2_ERR("sbp2: Error reconnecting to SBP-2 device - reconnect failed");
2051 return(-EIO);
2052 }
2053
2054 SBP2_INFO("sbp2: Reconnected to SBP-2 device");
2055
2056 return(0);
2057
2058 }
2059
2060 /*
2061 * This function is called in order to set the busy timeout (number of retries to attempt) on the sbp2 device.
2062 */
2063 static int sbp2_set_busy_timeout(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id)
2064 {
2065 quadlet_t data;
2066
2067 SBP2_DEBUG("sbp2: sbp2_set_busy_timeout");
2068
2069 /*
2070 * Ok, let's write to the target's busy timeout register
2071 */
2072 data = cpu_to_be32(SBP2_BUSY_TIMEOUT_VALUE);
2073
2074 if (hpsb_write(hi->host, LOCAL_BUS | scsi_id->node_id, SBP2_BUSY_TIMEOUT_ADDRESS, &data, 4)) {
2075 SBP2_ERR("sbp2: sbp2_set_busy_timeout error");
2076 }
2077
2078 return(0);
2079 }
2080
2081 /*
2082 * This function is called to parse sbp2 device's config rom unit directory. Used to determine
2083 * things like sbp2 management agent offset, and command set used (SCSI or RBC).
2084 */
2085 static int sbp2_parse_unit_directory(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id)
2086 {
2087 quadlet_t unit_directory_length, unit_directory_data;
2088 u64 unit_directory_addr;
2089 u32 i;
2090
2091 SBP2_DEBUG("sbp2: sbp2_parse_unit_directory");
2092
2093 if (sbp2util_unit_directory(hi, LOCAL_BUS | scsi_id->node_id, &unit_directory_addr)) {
2094 SBP2_DEBUG("sbp2: Error reading unit directory address - bad status");
2095 return(-EIO);
2096 }
2097
2098 /*
2099 * Read the size of the unit directory
2100 */
2101 if (sbp2util_read_quadlet(hi, LOCAL_BUS | scsi_id->node_id, unit_directory_addr,
2102 &unit_directory_length)) {
2103 SBP2_DEBUG("sbp2: Error reading unit directory length - bad status");
2104 return(-EIO);
2105 }
2106
2107 unit_directory_length = ((be32_to_cpu(unit_directory_length)) >> 16);
2108
2109 /*
2110 * Now, sweep through the unit directory looking for the management agent offset
2111 * Give up if we hit any error or somehow miss it...
2112 */
2113 for (i=0; i<unit_directory_length; i++) {
2114
2115 if (sbp2util_read_quadlet(hi, LOCAL_BUS | scsi_id->node_id, unit_directory_addr + (i<<2) + 4,
2116 &unit_directory_data)) {
2117 SBP2_DEBUG("sbp2: Error reading unit directory - bad status");
2118 return(-EIO);
2119 }
2120
2121 /*
2122 * Handle different fields in the unit directory, based on keys
2123 */
2124 unit_directory_data = be32_to_cpu(unit_directory_data);
2125 switch (unit_directory_data >> 24) {
2126
2127 case SBP2_CSR_OFFSET_KEY:
2128
2129 /*
2130 * Save off the management agent address
2131 */
2132 scsi_id->sbp2_management_agent_addr = CONFIG_ROM_INITIAL_MEMORY_SPACE +
2133 ((unit_directory_data & 0x00ffffff) << 2);
2134
2135 SBP2_DEBUG("sbp2: sbp2_management_agent_addr = %x", (unsigned int) scsi_id->sbp2_management_agent_addr);
2136 break;
2137
2138 case SBP2_COMMAND_SET_SPEC_ID_KEY:
2139
2140 /*
2141 * Command spec organization
2142 */
2143 scsi_id->sbp2_command_set_spec_id = unit_directory_data & 0xffffff;
2144 SBP2_DEBUG("sbp2: sbp2_command_set_spec_id = %x", (unsigned int) scsi_id->sbp2_command_set_spec_id);
2145 break;
2146
2147 case SBP2_COMMAND_SET_KEY:
2148
2149 /*
2150 * Command set used by sbp2 device
2151 */
2152 scsi_id->sbp2_command_set = unit_directory_data & 0xffffff;
2153 SBP2_DEBUG("sbp2: sbp2_command_set = %x", (unsigned int) scsi_id->sbp2_command_set);
2154 break;
2155
2156 case SBP2_UNIT_CHARACTERISTICS_KEY:
2157
2158 /*
2159 * Unit characterisitcs (orb related stuff that I'm not yet paying attention to)
2160 */
2161 scsi_id->sbp2_unit_characteristics = unit_directory_data & 0xffffff;
2162 SBP2_DEBUG("sbp2: sbp2_unit_characteristics = %x", (unsigned int) scsi_id->sbp2_unit_characteristics);
2163 break;
2164
2165 case SBP2_DEVICE_TYPE_AND_LUN_KEY:
2166
2167 /*
2168 * Device type and lun (used for detemining type of sbp2 device)
2169 */
2170 scsi_id->sbp2_device_type_and_lun = unit_directory_data & 0xffffff;
2171 SBP2_DEBUG("sbp2: sbp2_device_type_and_lun = %x", (unsigned int) scsi_id->sbp2_device_type_and_lun);
2172 break;
2173
2174 case SBP2_UNIT_SPEC_ID_KEY:
2175
2176 /*
2177 * Unit spec id (used for protocol detection)
2178 */
2179 scsi_id->sbp2_unit_spec_id = unit_directory_data & 0xffffff;
2180 SBP2_DEBUG("sbp2: sbp2_unit_spec_id = %x", (unsigned int) scsi_id->sbp2_unit_spec_id);
2181 break;
2182
2183 case SBP2_UNIT_SW_VERSION_KEY:
2184
2185 /*
2186 * Unit sw version (used for protocol detection)
2187 */
2188 scsi_id->sbp2_unit_sw_version = unit_directory_data & 0xffffff;
2189 SBP2_DEBUG("sbp2: sbp2_unit_sw_version = %x", (unsigned int) scsi_id->sbp2_unit_sw_version);
2190 break;
2191
2192 case SBP2_FIRMWARE_REVISION_KEY:
2193
2194 /*
2195 * Firmware revision (used to find broken devices). If the vendor id is 0xa0b8
2196 * (Symbios vendor id), then we have a bridge with 128KB max transfer size limitation.
2197 */
2198 scsi_id->sbp2_firmware_revision = unit_directory_data & 0xffff00;
2199 if (scsi_id->sbp2_firmware_revision == SBP2_128KB_BROKEN_FIRMWARE) {
2200 SBP2_WARN("sbp2: warning: Bridge chipset supports 128KB max transfer size");
2201 }
2202 break;
2203
2204 default:
2205 break;
2206 }
2207
2208 }
2209
2210 return(0);
2211 }
2212
2213 /*
2214 * This function is called in order to determine the max speed and packet size we can use in our ORBs.
2215 */
2216 static int sbp2_max_speed_and_size(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id)
2217 {
2218 u8 speed_code;
2219 struct node_entry *ne;
2220
2221 SBP2_DEBUG("sbp2: sbp2_max_speed_and_size");
2222
2223 /* Get this nodes information */
2224 ne = hpsb_nodeid_get_entry(hi->host->node_id);
2225
2226 if (!ne) {
2227 HPSB_ERR("sbp2: Unknown device, using S100, payload 512 bytes");
2228 scsi_id->speed_code = SPEED_100;
2229 scsi_id->max_payload_size = MAX_PAYLOAD_S100;
2230 return(0);
2231 }
2232
2233 speed_code = ne->busopt.lnkspd;
2234
2235 /* Bump down our speed if there is a module parameter forcing us slower */
2236 if (speed_code > max_speed) {
2237 speed_code = max_speed;
2238 SBP2_ERR("sbp2: Reducing SBP-2 max speed allowed (%x)", max_speed);
2239 }
2240
2241 /* Support the devices max_rec and max speed. We choose a setting
2242 * that fits both values, since they may differ. */
2243 if (speed_code >= SPEED_400 && ne->busopt.max_rec >= MAX_REC_S400) {
2244 HPSB_INFO("sbp2: SBP-2 device max speed S400 and payload 2KB");
2245 scsi_id->speed_code = SPEED_400;
2246 scsi_id->max_payload_size = MAX_PAYLOAD_S400;
2247 } else if (speed_code >= SPEED_200 && ne->busopt.max_rec >= MAX_REC_S200) {
2248 HPSB_INFO("sbp2: SBP-2 device max speed S200 and payload 1KB");
2249 scsi_id->speed_code = SPEED_200;
2250 scsi_id->max_payload_size = MAX_PAYLOAD_S200;
2251 } else {
2252 HPSB_INFO("sbp2: SBP-2 device max speed S100 and payload 512 bytes");
2253 scsi_id->speed_code = SPEED_100;
2254 scsi_id->max_payload_size = MAX_PAYLOAD_S100;
2255 }
2256
2257 return(0);
2258 }
2259
2260 /*
2261 * This function is called in order to perform a SBP-2 agent reset.
2262 */
2263 static int sbp2_agent_reset(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id, u32 flags)
2264 {
2265 struct sbp2_request_packet *agent_reset_request_packet;
2266
2267 SBP2_DEBUG("sbp2: sbp2_agent_reset");
2268
2269 /*
2270 * Ok, let's write to the target's management agent register
2271 */
2272 agent_reset_request_packet = sbp2util_allocate_write_request_packet(hi, LOCAL_BUS | scsi_id->node_id,
2273 scsi_id->sbp2_command_block_agent_addr + SBP2_AGENT_RESET_OFFSET,
2274 0, ntohl(SBP2_AGENT_RESET_DATA));
2275
2276 if (!agent_reset_request_packet) {
2277 SBP2_ERR("sbp2: sbp2util_allocate_write_request_packet failed");
2278 return(-EIO);
2279 }
2280
2281 if (!hpsb_send_packet(agent_reset_request_packet->packet)) {
2282 SBP2_ERR("sbp2: hpsb_send_packet failed");
2283 sbp2util_free_request_packet(agent_reset_request_packet);
2284 return(-EIO);
2285 }
2286
2287 if (!(flags & SBP2_SEND_NO_WAIT)) {
2288 down(&agent_reset_request_packet->packet->state_change);
2289 down(&agent_reset_request_packet->packet->state_change);
2290 }
2291
2292 /*
2293 * Need to make sure orb pointer is written on next command
2294 */
2295 scsi_id->last_orb = NULL;
2296
2297 return(0);
2298
2299 }
2300
2301 /*
2302 * This function is called to create the actual command orb and s/g list out of the
2303 * scsi command itself.
2304 */
2305 static int sbp2_create_command_orb(struct sbp2scsi_host_info *hi,
2306 struct scsi_id_instance_data *scsi_id,
2307 struct sbp2_command_info *command,
2308 unchar *scsi_cmd,
2309 unsigned int scsi_use_sg,
2310 unsigned int scsi_request_bufflen,
2311 void *scsi_request_buffer, int dma_dir)
2312 {
2313 struct scatterlist *sgpnt = (struct scatterlist *) scsi_request_buffer;
2314 struct sbp2_command_orb *command_orb = &command->command_orb;
2315 struct sbp2_unrestricted_page_table *scatter_gather_element =
2316 &command->scatter_gather_element[0];
2317 u32 sg_count, sg_len;
2318 dma_addr_t sg_addr;
2319 int i;
2320
2321 /*
2322 * Set-up our command ORB..
2323 *
2324 * NOTE: We're doing unrestricted page tables (s/g), as this is best performance
2325 * (at least with the devices I have). This means that data_size becomes the number
2326 * of s/g elements, and page_size should be zero (for unrestricted).
2327 */
2328 command_orb->next_ORB_hi = 0xffffffff;
2329 command_orb->next_ORB_lo = 0xffffffff;
2330 command_orb->misc = ORB_SET_MAX_PAYLOAD(scsi_id->max_payload_size);
2331 command_orb->misc |= ORB_SET_SPEED(scsi_id->speed_code);
2332 command_orb->misc |= ORB_SET_NOTIFY(1); /* Notify us when complete */
2333
2334 /*
2335 * Set-up our pagetable stuff... unfortunately, this has become messier than I'd like. Need to
2336 * clean this up a bit. ;-)
2337 */
2338 if (sbp2scsi_direction_table[*scsi_cmd] == ORB_DIRECTION_NO_DATA_TRANSFER) {
2339
2340 SBP2_DEBUG("sbp2: No data transfer");
2341
2342 /*
2343 * Handle no data transfer
2344 */
2345 command_orb->data_descriptor_hi = 0xffffffff;
2346 command_orb->data_descriptor_lo = 0xffffffff;
2347 command_orb->misc |= ORB_SET_DIRECTION(1);
2348
2349 } else if (scsi_use_sg) {
2350
2351 SBP2_DEBUG("sbp2: Use scatter/gather");
2352
2353 /*
2354 * Special case if only one element (and less than 64KB in size)
2355 */
2356 if ((scsi_use_sg == 1) && (sgpnt[0].length <= SBP2_MAX_SG_ELEMENT_LENGTH)) {
2357
2358 SBP2_DEBUG("sbp2: Only one s/g element");
2359 command->dma_dir = dma_dir;
2360 command->dma_size = sgpnt[0].length;
2361 command->cmd_dma = pci_map_single (hi->host->pdev, sgpnt[0].address,
2362 command->dma_size,
2363 command->dma_dir);
2364 SBP2_DMA_ALLOC("single scatter element");
2365
2366 command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
2367 command_orb->data_descriptor_lo = command->cmd_dma;
2368 command_orb->misc |= ORB_SET_DATA_SIZE(command->dma_size);
2369 command_orb->misc |= ORB_SET_DIRECTION(sbp2scsi_direction_table[*scsi_cmd]);
2370
2371 } else {
2372 int count = pci_map_sg(hi->host->pdev, sgpnt, scsi_use_sg, dma_dir);
2373 SBP2_DMA_ALLOC("scatter list");
2374
2375 command->dma_size = scsi_use_sg;
2376 command->dma_dir = dma_dir;
2377 command->sge_buffer = sgpnt;
2378
2379 /* use page tables (s/g) */
2380 command_orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1);
2381 command_orb->misc |= ORB_SET_DIRECTION(sbp2scsi_direction_table[*scsi_cmd]);
2382 command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
2383 command_orb->data_descriptor_lo = command->sge_dma;
2384
2385 /*
2386 * Loop through and fill out our sbp-2 page tables
2387 * (and split up anything too large)
2388 */
2389 for (i = 0, sg_count = 0 ; i < count; i++, sgpnt++) {
2390 sg_len = sg_dma_len(sgpnt);
2391 sg_addr = sg_dma_address(sgpnt);
2392 while (sg_len) {
2393 scatter_gather_element[sg_count].segment_base_lo = sg_addr;
2394 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
2395 scatter_gather_element[sg_count].length_segment_base_hi =
2396 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
2397 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
2398 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
2399 } else {
2400 scatter_gather_element[sg_count].length_segment_base_hi =
2401 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
2402 sg_len = 0;
2403 }
2404 sg_count++;
2405 }
2406 }
2407
2408 command_orb->misc |= ORB_SET_DATA_SIZE(sg_count); /* number of page table (s/g) elements */
2409
2410 /*
2411 * Byte swap page tables if necessary
2412 */
2413 sbp2util_cpu_to_be32_buffer(scatter_gather_element,
2414 (sizeof(struct sbp2_unrestricted_page_table)) * sg_count);
2415
2416 }
2417
2418 } else {
2419
2420 SBP2_DEBUG("sbp2: No scatter/gather");
2421
2422 command->dma_dir = dma_dir;
2423 command->dma_size = scsi_request_bufflen;
2424 command->cmd_dma = pci_map_single (hi->host->pdev, scsi_request_buffer,
2425 command->dma_size,
2426 command->dma_dir);
2427 SBP2_DMA_ALLOC("single bulk");
2428
2429 /*
2430 * Handle case where we get a command w/o s/g enabled (but check
2431 * for transfers larger than 64K)
2432 */
2433 if (scsi_request_bufflen <= SBP2_MAX_SG_ELEMENT_LENGTH) {
2434
2435 command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
2436 command_orb->data_descriptor_lo = command->cmd_dma;
2437 command_orb->misc |= ORB_SET_DATA_SIZE(scsi_request_bufflen);
2438 command_orb->misc |= ORB_SET_DIRECTION(sbp2scsi_direction_table[*scsi_cmd]);
2439
2440 /*
2441 * Sanity, in case our direction table is not up-to-date
2442 */
2443 if (!scsi_request_bufflen) {
2444 command_orb->data_descriptor_hi = 0xffffffff;
2445 command_orb->data_descriptor_lo = 0xffffffff;
2446 command_orb->misc |= ORB_SET_DIRECTION(1);
2447 }
2448
2449 } else {
2450 /*
2451 * Need to turn this into page tables, since the buffer is too large.
2452 */
2453 command_orb->data_descriptor_hi = ORB_SET_NODE_ID(hi->host->node_id);
2454 command_orb->data_descriptor_lo = command->sge_dma;
2455 command_orb->misc |= ORB_SET_PAGE_TABLE_PRESENT(0x1); /* use page tables (s/g) */
2456 command_orb->misc |= ORB_SET_DIRECTION(sbp2scsi_direction_table[*scsi_cmd]);
2457
2458 /*
2459 * fill out our sbp-2 page tables (and split up the large buffer)
2460 */
2461 sg_count = 0;
2462 sg_len = scsi_request_bufflen;
2463 sg_addr = command->cmd_dma;
2464 while (sg_len) {
2465 scatter_gather_element[sg_count].segment_base_lo = sg_addr;
2466 if (sg_len > SBP2_MAX_SG_ELEMENT_LENGTH) {
2467 scatter_gather_element[sg_count].length_segment_base_hi =
2468 PAGE_TABLE_SET_SEGMENT_LENGTH(SBP2_MAX_SG_ELEMENT_LENGTH);
2469 sg_addr += SBP2_MAX_SG_ELEMENT_LENGTH;
2470 sg_len -= SBP2_MAX_SG_ELEMENT_LENGTH;
2471 } else {
2472 scatter_gather_element[sg_count].length_segment_base_hi =
2473 PAGE_TABLE_SET_SEGMENT_LENGTH(sg_len);
2474 sg_len = 0;
2475 }
2476 sg_count++;
2477 }
2478
2479 command_orb->misc |= ORB_SET_DATA_SIZE(sg_count); /* number of page table (s/g) elements */
2480
2481 /*
2482 * Byte swap page tables if necessary
2483 */
2484 sbp2util_cpu_to_be32_buffer(scatter_gather_element,
2485 (sizeof(struct sbp2_unrestricted_page_table)) *
2486 sg_count);
2487
2488 }
2489
2490 }
2491
2492 /*
2493 * Byte swap command ORB if necessary
2494 */
2495 sbp2util_cpu_to_be32_buffer(command_orb, sizeof(struct sbp2_command_orb));
2496
2497 /*
2498 * Put our scsi command in the command ORB
2499 */
2500 memset(command_orb->cdb, 0, 12);
2501 memcpy(command_orb->cdb, scsi_cmd, COMMAND_SIZE(*scsi_cmd));
2502
2503 return(0);
2504 }
2505
2506 /*
2507 * This function is called in order to begin a regular SBP-2 command.
2508 */
2509 static int sbp2_link_orb_command(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id,
2510 struct sbp2_command_info *command)
2511 {
2512 struct sbp2_request_packet *command_request_packet;
2513 struct sbp2_command_orb *command_orb = &command->command_orb;
2514
2515 outstanding_orb_incr;
2516 SBP2_ORB_DEBUG("sending command orb %p, linked = %x, total orbs = %x",
2517 command_orb, command->linked, global_outstanding_command_orbs);
2518
2519 /*
2520 * Check to see if there are any previous orbs to use
2521 */
2522 if (scsi_id->last_orb == NULL) {
2523
2524 /*
2525 * Ok, let's write to the target's management agent register
2526 */
2527 if (!hi->bus_reset_in_progress) {
2528
2529 command_request_packet = sbp2util_allocate_write_request_packet(hi, LOCAL_BUS | scsi_id->node_id,
2530 scsi_id->sbp2_command_block_agent_addr + SBP2_ORB_POINTER_OFFSET,
2531 8, 0);
2532
2533 if (!command_request_packet) {
2534 SBP2_ERR("sbp2: sbp2util_allocate_write_request_packet failed");
2535 return(-EIO);
2536 }
2537
2538 command_request_packet->packet->data[0] = ORB_SET_NODE_ID(hi->host->node_id);
2539 command_request_packet->packet->data[1] = command->command_orb_dma;
2540 sbp2util_cpu_to_be32_buffer(command_request_packet->packet->data, 8);
2541
2542 SBP2_ORB_DEBUG("write command agent, command orb %p", command_orb);
2543
2544 if (!hpsb_send_packet(command_request_packet->packet)) {
2545 SBP2_ERR("sbp2: hpsb_send_packet failed");
2546 sbp2util_free_request_packet(command_request_packet);
2547 return(-EIO);
2548 }
2549
2550 SBP2_ORB_DEBUG("write command agent complete");
2551 }
2552
2553 scsi_id->last_orb = command_orb;
2554
2555 } else {
2556
2557 /*
2558 * We have an orb already sent (maybe or maybe not
2559 * processed) that we can append this orb to. So do so,
2560 * and ring the doorbell. Have to be very careful
2561 * modifying these next orb pointers, as they are accessed
2562 * both by the sbp2 device and us.
2563 */
2564 scsi_id->last_orb->next_ORB_lo = cpu_to_be32(command->command_orb_dma);
2565 scsi_id->last_orb->next_ORB_hi = 0x0; /* Tells hardware that this pointer is valid */
2566
2567 /*
2568 * Only ring the doorbell if we need to (first parts of linked orbs don't need this)
2569 */
2570 if (!command->linked && !hi->bus_reset_in_progress) {
2571
2572 command_request_packet = sbp2util_allocate_write_request_packet(hi,
2573 LOCAL_BUS | scsi_id->node_id,
2574 scsi_id->sbp2_command_block_agent_addr + SBP2_DOORBELL_OFFSET,
2575 0, cpu_to_be32(command->command_orb_dma));
2576
2577 if (!command_request_packet) {
2578 SBP2_ERR("sbp2: sbp2util_allocate_write_request_packet failed");
2579 return(-EIO);
2580 }
2581
2582 SBP2_ORB_DEBUG("ring doorbell, command orb %p", command_orb);
2583
2584 if (!hpsb_send_packet(command_request_packet->packet)) {
2585 SBP2_ERR("sbp2: hpsb_send_packet failed");
2586 sbp2util_free_request_packet(command_request_packet);
2587 return(-EIO);
2588 }
2589 }
2590
2591 scsi_id->last_orb = command_orb;
2592
2593 }
2594 return(0);
2595 }
2596
2597 /*
2598 * This function is called in order to begin a regular SBP-2 command.
2599 */
2600 static int sbp2_send_command(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id,
2601 Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
2602 {
2603 unchar *cmd = (unchar *) SCpnt->cmnd;
2604 u32 device_type = (scsi_id->sbp2_device_type_and_lun & 0x00ff0000) >> 16;
2605 struct sbp2_command_info *command;
2606
2607 SBP2_DEBUG("sbp2: sbp2_send_command");
2608 SBP2_DEBUG("sbp2: SCSI command = %02x", *cmd);
2609 SBP2_DEBUG("sbp2: SCSI transfer size = %x", SCpnt->request_bufflen);
2610 SBP2_DEBUG("sbp2: SCSI s/g elements = %x", (unsigned int)SCpnt->use_sg);
2611
2612 /*
2613 * Check for broken devices that can't handle greater than 128K transfers, and deal with them in a
2614 * hacked ugly way.
2615 */
2616 if ((scsi_id->sbp2_firmware_revision == SBP2_128KB_BROKEN_FIRMWARE) &&
2617 (SCpnt->request_bufflen > SBP2_BROKEN_FIRMWARE_MAX_TRANSFER) &&
2618 (device_type == TYPE_DISK) &&
2619 (SCpnt->use_sg) &&
2620 (*cmd == 0x28 || *cmd == 0x2a || *cmd == 0x0a || *cmd == 0x08)) {
2621
2622 /*
2623 * Darn, a broken device. We'll need to split up the transfer ourselves
2624 */
2625 sbp2_send_split_command(hi, scsi_id, SCpnt, done);
2626 return(0);
2627 }
2628
2629 /*
2630 * Allocate a command orb and s/g structure
2631 */
2632 command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done, hi);
2633 if (!command) {
2634 return(-EIO);
2635 }
2636
2637 /*
2638 * Now actually fill in the comamnd orb and sbp2 s/g list
2639 */
2640 sbp2_create_command_orb(hi, scsi_id, command, cmd, SCpnt->use_sg,
2641 SCpnt->request_bufflen, SCpnt->request_buffer,
2642 scsi_to_pci_dma_dir(SCpnt->sc_data_direction));
2643
2644 /*
2645 * Update our cdb if necessary (to handle sbp2 RBC command set differences).
2646 * This is where the command set hacks go! =)
2647 */
2648 if ((device_type == TYPE_DISK) ||
2649 (device_type == TYPE_SDAD) ||
2650 (device_type == TYPE_ROM)) {
2651 sbp2_check_sbp2_command(command->command_orb.cdb);
2652 }
2653
2654 /*
2655 * Initialize status fifo
2656 */
2657 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
2658
2659 /*
2660 * Link up the orb, and ring the doorbell if needed
2661 */
2662 sbp2_link_orb_command(hi, scsi_id, command);
2663
2664 return(0);
2665 }
2666
2667 /*
2668 * This function is called for broken sbp2 device, where we have to break up large transfers.
2669 */
2670 static int sbp2_send_split_command(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id,
2671 Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
2672 {
2673 unchar *cmd = (unchar *) SCpnt->cmnd;
2674 struct scatterlist *sgpnt = (struct scatterlist *) SCpnt->request_buffer;
2675 struct sbp2_command_info *command;
2676 unsigned int i, block_count, block_address, block_size;
2677 unsigned int current_sg = 0;
2678 unsigned int total_transfer = 0;
2679 unsigned int total_sg = 0;
2680 unchar new_cmd[12];
2681
2682 memset(new_cmd, 0, 12);
2683 memcpy(new_cmd, cmd, COMMAND_SIZE(*cmd));
2684
2685 /*
2686 * Turns command into 10 byte version
2687 */
2688 sbp2_check_sbp2_command(new_cmd);
2689
2690 /*
2691 * Pull block size, block address, block count from command sent down
2692 */
2693 block_count = (cmd[7] << 8) | cmd[8];
2694 block_address = (cmd[2] << 24) | (cmd[3] << 16) | (cmd[4] << 8) | cmd[5];
2695 block_size = SCpnt->request_bufflen/block_count;
2696
2697 /*
2698 * Walk the scsi s/g list to determine how much we can transfer in one pop
2699 */
2700 for (i=0; i<SCpnt->use_sg; i++) {
2701
2702 total_transfer+=sgpnt[i].length;
2703 total_sg++;
2704
2705 if (total_transfer > SBP2_BROKEN_FIRMWARE_MAX_TRANSFER) {
2706
2707 /*
2708 * Back everything up one, so that we're less than 128KB
2709 */
2710 total_transfer-=sgpnt[i].length;
2711 total_sg--;
2712 i--;
2713
2714 command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done, hi);
2715 if (!command) {
2716 return(-EIO);
2717 }
2718
2719 /*
2720 * This is not the final piece, so mark it as linked
2721 */
2722 command->linked = 1;
2723
2724 block_count = total_transfer/block_size;
2725 new_cmd[2] = (unchar) (block_address >> 24) & 0xff;
2726 new_cmd[3] = (unchar) (block_address >> 16) & 0xff;
2727 new_cmd[4] = (unchar) (block_address >> 8) & 0xff;
2728 new_cmd[5] = (unchar) block_address & 0xff;
2729 new_cmd[7] = (unchar) (block_count >> 8) & 0xff;
2730 new_cmd[8] = (unchar) block_count & 0xff;
2731 block_address+=block_count;
2732
2733 sbp2_create_command_orb(hi, scsi_id, command, new_cmd, total_sg,
2734 total_transfer, &sgpnt[current_sg],
2735 scsi_to_pci_dma_dir(SCpnt->sc_data_direction));
2736
2737 /*
2738 * Link up the orb, and ring the doorbell if needed
2739 */
2740 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
2741 sbp2_link_orb_command(hi, scsi_id, command);
2742
2743 current_sg += total_sg;
2744 total_sg = 0;
2745 total_transfer = 0;
2746
2747 }
2748
2749 }
2750
2751 /*
2752 * Get the last piece...
2753 */
2754 command = sbp2util_allocate_command_orb(scsi_id, SCpnt, done, hi);
2755 if (!command) {
2756 return(-EIO);
2757 }
2758
2759 block_count = total_transfer/block_size;
2760 new_cmd[2] = (unchar) (block_address >> 24) & 0xff;
2761 new_cmd[3] = (unchar) (block_address >> 16) & 0xff;
2762 new_cmd[4] = (unchar) (block_address >> 8) & 0xff;
2763 new_cmd[5] = (unchar) block_address & 0xff;
2764 new_cmd[7] = (unchar) (block_count >> 8) & 0xff;
2765 new_cmd[8] = (unchar) block_count & 0xff;
2766
2767 sbp2_create_command_orb(hi, scsi_id, command, new_cmd, total_sg,
2768 total_transfer, &sgpnt[current_sg],
2769 scsi_to_pci_dma_dir(SCpnt->sc_data_direction));
2770
2771 /*
2772 * Link up the orb, and ring the doorbell if needed
2773 */
2774 memset(&scsi_id->status_block, 0, sizeof(struct sbp2_status_block));
2775 sbp2_link_orb_command(hi, scsi_id, command);
2776
2777
2778 return(0);
2779 }
2780
2781 /*
2782 * This function deals with command set differences between Linux scsi command set and sbp2 RBC
2783 * command set.
2784 */
2785 static void sbp2_check_sbp2_command(unchar *cmd)
2786 {
2787 unchar new_cmd[16];
2788
2789 SBP2_DEBUG("sbp2: sbp2_check_sbp2_command");
2790
2791 switch (*cmd) {
2792
2793 case READ_6:
2794
2795 SBP2_DEBUG("sbp2: Convert READ_6 to READ_10");
2796
2797 /*
2798 * Need to turn read_6 into read_10
2799 */
2800 new_cmd[0] = 0x28;
2801 new_cmd[1] = (cmd[1] & 0xe0);
2802 new_cmd[2] = 0x0;
2803 new_cmd[3] = (cmd[1] & 0x1f);
2804 new_cmd[4] = cmd[2];
2805 new_cmd[5] = cmd[3];
2806 new_cmd[6] = 0x0;
2807 new_cmd[7] = 0x0;
2808 new_cmd[8] = cmd[4];
2809 new_cmd[9] = cmd[5];
2810
2811 memcpy(cmd, new_cmd, 10);
2812
2813 break;
2814
2815 case WRITE_6:
2816
2817 SBP2_DEBUG("sbp2: Convert WRITE_6 to WRITE_10");
2818
2819 /*
2820 * Need to turn write_6 into write_10
2821 */
2822 new_cmd[0] = 0x2a;
2823 new_cmd[1] = (cmd[1] & 0xe0);
2824 new_cmd[2] = 0x0;
2825 new_cmd[3] = (cmd[1] & 0x1f);
2826 new_cmd[4] = cmd[2];
2827 new_cmd[5] = cmd[3];
2828 new_cmd[6] = 0x0;
2829 new_cmd[7] = 0x0;
2830 new_cmd[8] = cmd[4];
2831 new_cmd[9] = cmd[5];
2832
2833 memcpy(cmd, new_cmd, 10);
2834
2835 break;
2836
2837 case MODE_SENSE:
2838
2839 SBP2_DEBUG("sbp2: Convert MODE_SENSE_6 to MOSE_SENSE_10");
2840
2841 /*
2842 * Need to turn mode_sense_6 into mode_sense_10
2843 */
2844 new_cmd[0] = 0x5a;
2845 new_cmd[1] = cmd[1];
2846 new_cmd[2] = cmd[2];
2847 new_cmd[3] = 0x0;
2848 new_cmd[4] = 0x0;
2849 new_cmd[5] = 0x0;
2850 new_cmd[6] = 0x0;
2851 new_cmd[7] = 0x0;
2852 new_cmd[8] = cmd[4];
2853 new_cmd[9] = cmd[5];
2854
2855 memcpy(cmd, new_cmd, 10);
2856
2857 break;
2858
2859 case MODE_SELECT:
2860
2861 /*
2862 * TODO. Probably need to change mode select to 10 byte version
2863 */
2864
2865 default:
2866 break;
2867 }
2868
2869 return;
2870 }
2871
2872 /*
2873 * Translates SBP-2 status into SCSI sense data for check conditions
2874 */
2875 static unsigned int sbp2_status_to_sense_data(unchar *sbp2_status, unchar *sense_data)
2876 {
2877 SBP2_DEBUG("sbp2: sbp2_status_to_sense_data");
2878
2879 /*
2880 * Ok, it's pretty ugly... ;-)
2881 */
2882 sense_data[0] = 0x70;
2883 sense_data[1] = 0x0;
2884 sense_data[2] = sbp2_status[9];
2885 sense_data[3] = sbp2_status[12];
2886 sense_data[4] = sbp2_status[13];
2887 sense_data[5] = sbp2_status[14];
2888 sense_data[6] = sbp2_status[15];
2889 sense_data[7] = 10;
2890 sense_data[8] = sbp2_status[16];
2891 sense_data[9] = sbp2_status[17];
2892 sense_data[10] = sbp2_status[18];
2893 sense_data[11] = sbp2_status[19];
2894 sense_data[12] = sbp2_status[10];
2895 sense_data[13] = sbp2_status[11];
2896 sense_data[14] = sbp2_status[20];
2897 sense_data[15] = sbp2_status[21];
2898
2899 return(sbp2_status[8] & 0x3f); /* return scsi status */
2900 }
2901
2902 /*
2903 * This function is called after a command is completed, in order to do any necessary SBP-2
2904 * response data translations for the SCSI stack
2905 */
2906 static void sbp2_check_sbp2_response(struct sbp2scsi_host_info *hi,
2907 struct scsi_id_instance_data *scsi_id,
2908 Scsi_Cmnd *SCpnt)
2909 {
2910 u8 *scsi_buf = SCpnt->request_buffer;
2911 u32 device_type = (scsi_id->sbp2_device_type_and_lun & 0x00ff0000) >> 16;
2912
2913 SBP2_DEBUG("sbp2: sbp2_check_sbp2_response");
2914
2915 switch (SCpnt->cmnd[0]) {
2916
2917 case INQUIRY:
2918
2919 SBP2_DEBUG("sbp2: Check Inquiry data");
2920
2921 /*
2922 * Check for Simple Direct Access Device and change it to TYPE_DISK
2923 */
2924 if ((scsi_buf[0] & 0x1f) == TYPE_SDAD) {
2925 SBP2_DEBUG("sbp2: Changing TYPE_SDAD to TYPE_DISK");
2926 scsi_buf[0] &= 0xe0;
2927 }
2928
2929 /*
2930 * Fix ansi revision and response data format
2931 */
2932 scsi_buf[2] |= 2;
2933 scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2;
2934
2935 break;
2936
2937 case MODE_SENSE:
2938
2939 if ((device_type == TYPE_DISK) ||
2940 (device_type == TYPE_SDAD) ||
2941 (device_type == TYPE_ROM)) {
2942
2943 SBP2_DEBUG("sbp2: Modify mode sense response (10 byte version)");
2944
2945 scsi_buf[0] = scsi_buf[1]; /* Mode data length */
2946 scsi_buf[1] = scsi_buf[2]; /* Medium type */
2947 scsi_buf[2] = scsi_buf[3]; /* Device specific parameter */
2948 scsi_buf[3] = scsi_buf[7]; /* Block descriptor length */
2949 memcpy(scsi_buf + 4, scsi_buf + 8, scsi_buf[0]);
2950
2951 }
2952
2953 break;
2954
2955 case MODE_SELECT:
2956
2957 /*
2958 * TODO. Probably need to change mode select to 10 byte version
2959 */
2960
2961 default:
2962 break;
2963 }
2964 return;
2965 }
2966
2967 /*
2968 * This function deals with status writes from the SBP-2 device
2969 */
2970 static int sbp2_handle_status_write(struct hpsb_host *host, int nodeid, int destid,
2971 quadlet_t *data, u64 addr, unsigned int length)
2972 {
2973 struct sbp2scsi_host_info *hi = NULL;
2974 struct scsi_id_instance_data *scsi_id = NULL;
2975 int i;
2976 unsigned long flags;
2977 Scsi_Cmnd *SCpnt = NULL;
2978 u32 scsi_status = SBP2_SCSI_STATUS_GOOD;
2979 struct sbp2_command_info *command;
2980
2981 SBP2_DEBUG("sbp2: sbp2_handle_status_write");
2982
2983 if (!host) {
2984 SBP2_ERR("sbp2: host is NULL - this is bad!");
2985 return(RCODE_ADDRESS_ERROR);
2986 }
2987
2988 sbp2_spin_lock(&sbp2_host_info_lock, flags);
2989 hi = sbp2_find_host_info(host);
2990 sbp2_spin_unlock(&sbp2_host_info_lock, flags);
2991
2992 if (!hi) {
2993 SBP2_ERR("sbp2: host info is NULL - this is bad!");
2994 return(RCODE_ADDRESS_ERROR);
2995 }
2996
2997 sbp2_spin_lock(&hi->sbp2_command_lock, flags);
2998
2999 /*
3000 * Find our scsi_id structure
3001 */
3002 for (i=0; i<SBP2SCSI_MAX_SCSI_IDS; i++) {
3003 if (hi->scsi_id[i]) {
3004 if (hi->scsi_id[i]->node_id == (nodeid & NODE_MASK)) {
3005 scsi_id = hi->scsi_id[i];
3006 SBP2_DEBUG("sbp2: SBP-2 status write from node %x", scsi_id->node_id);
3007 break;
3008 }
3009 }
3010 }
3011
3012 if (!scsi_id) {
3013 SBP2_ERR("sbp2: scsi_id is NULL - device is gone?");
3014 sbp2_spin_unlock(&hi->sbp2_command_lock, flags);
3015 return(RCODE_ADDRESS_ERROR);
3016 }
3017
3018 /*
3019 * Put response into scsi_id status fifo...
3020 */
3021 memcpy(&scsi_id->status_block, data, length);
3022
3023 /*
3024 * Byte swap first two quadlets (8 bytes) of status for processing
3025 */
3026 sbp2util_be32_to_cpu_buffer(&scsi_id->status_block, 8);
3027
3028 /*
3029 * Handle command ORB status here if necessary. First, need to match status with command.
3030 */
3031 command = sbp2util_find_command_for_orb(scsi_id, scsi_id->status_block.ORB_offset_lo);
3032 if (command) {
3033
3034 SBP2_DEBUG("sbp2: Found status for command ORB");
3035
3036 SBP2_ORB_DEBUG("matched command orb %p", &command->command_orb);
3037 outstanding_orb_decr;
3038
3039 /*
3040 * Matched status with command, now grab scsi command pointers and check status
3041 */
3042 SCpnt = command->Current_SCpnt;
3043 sbp2util_mark_command_completed(scsi_id, command);
3044
3045 if (SCpnt && !command->linked) {
3046
3047 /*
3048 * Handle check conditions
3049 */
3050 if (STATUS_GET_SBP_STATUS(scsi_id->status_block.ORB_offset_hi_misc)) {
3051
3052 SBP2_DEBUG("sbp2: CHECK CONDITION");
3053
3054 /*
3055 * Translate SBP-2 status to SCSI sense data
3056 */
3057 scsi_status = sbp2_status_to_sense_data((unchar *)&scsi_id->status_block, SCpnt->sense_buffer);
3058
3059 /*
3060 * Initiate a fetch agent reset.
3061 */
3062 sbp2_agent_reset(hi, scsi_id, SBP2_SEND_NO_WAIT);
3063
3064 }
3065
3066 SBP2_ORB_DEBUG("completing command orb %p", &command->command_orb);
3067
3068 /*
3069 * Complete the SCSI command
3070 */
3071 SBP2_DEBUG("sbp2: Completing SCSI command");
3072 sbp2scsi_complete_command(hi, scsi_id, scsi_status, SCpnt, command->Current_done);
3073 SBP2_ORB_DEBUG("command orb completed");
3074 }
3075
3076 /*
3077 * Check here to see if there are no commands in-use. If there are none, we can
3078 * null out last orb so that next time around we write directly to the orb pointer...
3079 * Quick start saves one 1394 bus transaction.
3080 */
3081 if (list_empty(&scsi_id->sbp2_command_orb_inuse)) {
3082 scsi_id->last_orb = NULL;
3083 }
3084
3085 }
3086
3087 sbp2_spin_unlock(&hi->sbp2_command_lock, flags);
3088 wake_up(&scsi_id->sbp2_login_wait);
3089 return(RCODE_COMPLETE);
3090 }
3091
3092
3093 /**************************************
3094 * SCSI interface related section
3095 **************************************/
3096
3097 /*
3098 * This routine is the main request entry routine for doing I/O. It is
3099 * called from the scsi stack directly.
3100 */
3101 static int sbp2scsi_queuecommand (Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
3102 {
3103 struct sbp2scsi_host_info *hi = NULL;
3104 struct scsi_id_instance_data *scsi_id = NULL;
3105 unsigned long flags;
3106
3107 SBP2_DEBUG("sbp2: sbp2scsi_queuecommand");
3108
3109 /*
3110 * Pull our host info and scsi id instance data from the scsi command
3111 */
3112 hi = (struct sbp2scsi_host_info *) SCpnt->host->hostdata[0];
3113
3114 if (!hi) {
3115 SBP2_ERR("sbp2: sbp2scsi_host_info is NULL - this is bad!");
3116 SCpnt->result = DID_NO_CONNECT << 16;
3117 done (SCpnt);
3118 return(0);
3119 }
3120
3121 scsi_id = hi->scsi_id[SCpnt->target];
3122
3123 /*
3124 * Save off the command if this is the initial bus scan... so that we can
3125 * complete it after we find all our sbp2 devices on the 1394 bus
3126 */
3127 if (!no_bus_scan && !hi->initial_scsi_bus_scan_complete) {
3128 hi->bus_scan_SCpnt = SCpnt;
3129 hi->bus_scan_done = done;
3130 return(0);
3131 }
3132
3133 /*
3134 * If scsi_id is null, it means there is no device in this slot, so we should return
3135 * selection timeout.
3136 */
3137 if (!scsi_id) {
3138 SCpnt->result = DID_NO_CONNECT << 16;
3139 done (SCpnt);
3140 return(0);
3141 }
3142
3143 /*
3144 * Until we handle multiple luns, just return selection time-out to any IO directed at non-zero LUNs
3145 */
3146 if (SCpnt->lun) {
3147 SCpnt->result = DID_NO_CONNECT << 16;
3148 done (SCpnt);
3149 return(0);
3150 }
3151
3152 /*
3153 * Check for request sense command, and handle it here (autorequest sense)
3154 */
3155 if (SCpnt->cmnd[0] == REQUEST_SENSE) {
3156 SBP2_DEBUG("sbp2: REQUEST_SENSE");
3157 memcpy(SCpnt->request_buffer, SCpnt->sense_buffer, SCpnt->request_bufflen);
3158 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
3159 sbp2scsi_complete_command(hi, scsi_id, SBP2_SCSI_STATUS_GOOD, SCpnt, done);
3160 return(0);
3161 }
3162
3163 /*
3164 * Check to see if there is a command in progress and just return busy (to be queued later)
3165 */
3166 if (hi->bus_reset_in_progress) {
3167 SBP2_ERR("sbp2: Bus reset in progress - rejecting command");
3168 SCpnt->result = DID_BUS_BUSY << 16;
3169 done (SCpnt);
3170 return(0);
3171 }
3172
3173 /*
3174 * Try and send our SCSI command
3175 */
3176 sbp2_spin_lock(&hi->sbp2_command_lock, flags);
3177 if (sbp2_send_command(hi, scsi_id, SCpnt, done)) {
3178 SBP2_ERR("sbp2: Error sending SCSI command");
3179 sbp2scsi_complete_command(hi, scsi_id, SBP2_SCSI_STATUS_SELECTION_TIMEOUT, SCpnt, done);
3180 }
3181 sbp2_spin_unlock(&hi->sbp2_command_lock, flags);
3182
3183 return(0);
3184 }
3185
3186 /*
3187 * This function is called in order to complete all outstanding SBP-2 commands (in case of resets, etc.).
3188 */
3189 static void sbp2scsi_complete_all_commands(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id,
3190 u32 status)
3191 {
3192 struct list_head *lh;
3193 struct sbp2_command_info *command;
3194
3195 SBP2_DEBUG("sbp2: sbp2_complete_all_commands");
3196
3197 while (!list_empty(&scsi_id->sbp2_command_orb_inuse)) {
3198 SBP2_DEBUG("sbp2: Found pending command to complete");
3199 lh = scsi_id->sbp2_command_orb_inuse.next;
3200 command = list_entry(lh, struct sbp2_command_info, list);
3201 sbp2util_mark_command_completed(scsi_id, command);
3202 if (command->Current_SCpnt && !command->linked) {
3203 void (*done)(Scsi_Cmnd *) = command->Current_done;
3204 command->Current_SCpnt->result = status << 16;
3205 done (command->Current_SCpnt);
3206 }
3207 }
3208
3209 return;
3210 }
3211
3212 /*
3213 * This function is called in order to complete a regular SBP-2 command.
3214 */
3215 static void sbp2scsi_complete_command(struct sbp2scsi_host_info *hi, struct scsi_id_instance_data *scsi_id, u32 scsi_status,
3216 Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
3217 {
3218 SBP2_DEBUG("sbp2: sbp2scsi_complete_command");
3219
3220 /*
3221 * Sanity
3222 */
3223 if (!SCpnt) {
3224 SBP2_ERR("sbp2: SCpnt is NULL");
3225 return;
3226 }
3227
3228 /*
3229 * If a bus reset is in progress and there was an error, don't complete the command,
3230 * just let it get retried at the end of the bus reset.
3231 */
3232 if ((hi->bus_reset_in_progress) && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
3233 SBP2_ERR("sbp2: Bus reset in progress - retry command later");
3234 return;
3235 }
3236
3237 /*
3238 * Switch on scsi status
3239 */
3240 switch (scsi_status) {
3241 case SBP2_SCSI_STATUS_GOOD:
3242 SCpnt->result = DID_OK;
3243 break;
3244
3245 case SBP2_SCSI_STATUS_BUSY:
3246 SBP2_ERR("sbp2: SBP2_SCSI_STATUS_BUSY");
3247 SCpnt->result = DID_BUS_BUSY << 16;
3248 break;
3249
3250 case SBP2_SCSI_STATUS_CHECK_CONDITION:
3251 SBP2_DEBUG("sbp2: SBP2_SCSI_STATUS_CHECK_CONDITION");
3252 SCpnt->result = CHECK_CONDITION << 1;
3253
3254 /*
3255 * Debug stuff
3256 */
3257 print_sense("bh", SCpnt);
3258
3259 break;
3260
3261 case SBP2_SCSI_STATUS_SELECTION_TIMEOUT:
3262 SBP2_ERR("sbp2: SBP2_SCSI_STATUS_SELECTION_TIMEOUT");
3263 SCpnt->result = DID_NO_CONNECT << 16;
3264 break;
3265
3266 case SBP2_SCSI_STATUS_CONDITION_MET:
3267 case SBP2_SCSI_STATUS_RESERVATION_CONFLICT:
3268 case SBP2_SCSI_STATUS_COMMAND_TERMINATED:
3269 SBP2_ERR("sbp2: Bad SCSI status = %x", scsi_status);
3270 SCpnt->result = DID_ERROR << 16;
3271 break;
3272
3273 default:
3274 SBP2_ERR("sbp2: Unsupported SCSI status = %x", scsi_status);
3275 SCpnt->result = DID_ERROR << 16;
3276 }
3277
3278 /*
3279 * Take care of any sbp2 response data mucking here (RBC stuff, etc.)
3280 */
3281 if (SCpnt->result == DID_OK) {
3282 sbp2_check_sbp2_response(hi, scsi_id, SCpnt);
3283 }
3284
3285 /*
3286 * One more quick hack (not enabled by default). Some sbp2 devices do not support
3287 * mode sense. Turn-on this hack to allow the device to pass the sd driver's
3288 * write-protect test (so that you can mount the device rw).
3289 */
3290 if (mode_sense_hack && SCpnt->result != DID_OK && SCpnt->cmnd[0] == MODE_SENSE) {
3291 SBP2_INFO("sbp2: Returning success to mode sense command");
3292 SCpnt->result = DID_OK;
3293 SCpnt->sense_buffer[0] = 0;
3294 memset (SCpnt->request_buffer, 0, 8);
3295 }
3296
3297 /*
3298 * If a bus reset is in progress and there was an error, complete the command
3299 * as busy so that it will get retried.
3300 */
3301 if ((hi->bus_reset_in_progress) && (scsi_status != SBP2_SCSI_STATUS_GOOD)) {
3302 SBP2_ERR("sbp2: Completing command with busy (bus reset)");
3303 SCpnt->result = DID_BUS_BUSY << 16;
3304 }
3305
3306 /*
3307 * If a unit attention occurs, return busy status so it gets retried... it could have happened because
3308 * of a 1394 bus reset or hot-plug...
3309 */
3310 if ((scsi_status == SBP2_SCSI_STATUS_CHECK_CONDITION) && (SCpnt->sense_buffer[2] == UNIT_ATTENTION)) {
3311 SBP2_INFO("sbp2: UNIT ATTENTION - return busy");
3312 SCpnt->result = DID_BUS_BUSY << 16;
3313 }
3314
3315 /*
3316 * Tell scsi stack that we're done with this command
3317 */
3318 done (SCpnt);
3319
3320 return;
3321 }
3322
3323 /*
3324 * Called by scsi stack when something has really gone wrong.
3325 * Usually called when a command has timed-out for some reason.
3326 */
3327 static int sbp2scsi_abort (Scsi_Cmnd *SCpnt)
3328 {
3329 struct sbp2scsi_host_info *hi = (struct sbp2scsi_host_info *) SCpnt->host->hostdata[0];
3330 struct scsi_id_instance_data *scsi_id = hi->scsi_id[SCpnt->target];
3331 struct sbp2_command_info *command;
3332 unsigned long flags;
3333
3334 SBP2_ERR("sbp2: aborting sbp2 command");
3335
3336 if (scsi_id) {
3337
3338 /*
3339 * Right now, just return any matching command structures to the free pool (there may
3340 * be more than one because of broken up/linked commands).
3341 */
3342 sbp2_spin_lock(&hi->sbp2_command_lock, flags);
3343 do {
3344 command = sbp2util_find_command_for_SCpnt(scsi_id, SCpnt);
3345 if (command) {
3346 SBP2_DEBUG("sbp2: Found command to abort");
3347 sbp2util_mark_command_completed(scsi_id, command);
3348 if (command->Current_SCpnt && !command->linked) {
3349 void (*done)(Scsi_Cmnd *) = command->Current_done;
3350 command->Current_SCpnt->result = DID_ABORT << 16;
3351 done (command->Current_SCpnt);
3352 }
3353 }
3354 } while (command);
3355
3356 /*
3357 * Initiate a fetch agent reset.
3358 */
3359 sbp2_agent_reset(hi, scsi_id, SBP2_SEND_NO_WAIT);
3360 sbp2scsi_complete_all_commands(hi, scsi_id, DID_BUS_BUSY);
3361 sbp2_spin_unlock(&hi->sbp2_command_lock, flags);
3362 }
3363
3364 return(SCSI_ABORT_SUCCESS);
3365 }
3366
3367 /*
3368 * Called by scsi stack when something has really gone wrong.
3369 */
3370 static int sbp2scsi_reset (Scsi_Cmnd *SCpnt, unsigned int reset_flags)
3371 {
3372 struct sbp2scsi_host_info *hi = (struct sbp2scsi_host_info *) SCpnt->host->hostdata[0];
3373
3374 SBP2_ERR("sbp2: reset requested");
3375
3376 if (hi) {
3377 SBP2_ERR("sbp2: generating IEEE-1394 bus reset");
3378 hpsb_reset_bus(hi->host, LONG_RESET);
3379 }
3380
3381 return(SCSI_RESET_SUCCESS);
3382 }
3383
3384 /*
3385 * Called by scsi stack to get bios parameters (used by fdisk, and at boot).
3386 */
3387 static int sbp2scsi_biosparam (Scsi_Disk *disk, kdev_t dev, int geom[])
3388 {
3389 int heads, sectors, cylinders;
3390
3391 SBP2_DEBUG("sbp2: request for bios parameters");
3392
3393 heads = 64;
3394 sectors = 32;
3395 cylinders = disk->capacity / (heads * sectors);
3396
3397 if (cylinders > 1024) {
3398 heads = 255;
3399 sectors = 63;
3400 cylinders = disk->capacity / (heads * sectors);
3401 }
3402
3403 geom[0] = heads;
3404 geom[1] = sectors;
3405 geom[2] = cylinders;
3406
3407 return(0);
3408 }
3409
3410 /*
3411 * This routine is called at setup (init) and does nothing. Not used here. =)
3412 */
3413 void sbp2scsi_setup( char *str, int *ints)
3414 {
3415 SBP2_DEBUG("sbp2: sbp2scsi_setup");
3416 return;
3417 }
3418
3419 /*
3420 * This is our detection routine, and is where we init everything.
3421 */
3422 static int sbp2scsi_detect (Scsi_Host_Template *tpnt)
3423 {
3424 SBP2_DEBUG("sbp2: sbp2scsi_detect");
3425
3426 global_scsi_tpnt = tpnt;
3427 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,26)
3428 global_scsi_tpnt->proc_name = SBP2_DEVICE_NAME;
3429 #endif
3430 /*
3431 * Module load option for force one command at a time
3432 */
3433 if (serialize_io) {
3434 SBP2_ERR("sbp2: Driver forced to serialize I/O (serialize_io = 1)");
3435 global_scsi_tpnt->can_queue = 1;
3436 global_scsi_tpnt->cmd_per_lun = 1;
3437 }
3438
3439 /*
3440 * Module load option to limit max size of requests from the scsi drivers
3441 */
3442 if (no_large_packets) {
3443 SBP2_ERR("sbp2: Driver forced to limit max transfer size (no_large_packets = 1)");
3444 global_scsi_tpnt->sg_tablesize = 0x1f;
3445 global_scsi_tpnt->use_clustering = DISABLE_CLUSTERING;
3446 }
3447
3448 if (no_bus_scan) {
3449 SBP2_ERR("sbp2: Initial scsi bus scan deferred (no_bus_scan = 1)");
3450 }
3451
3452 if (mode_sense_hack) {
3453 SBP2_ERR("sbp2: Mode sense emulation enabled (mode_sense_hack = 1)");
3454 }
3455
3456 sbp2_init();
3457
3458 if (!sbp2_host_count) {
3459 SBP2_ERR("sbp2: Please load the lower level IEEE-1394 driver (e.g. ohci1394) before sbp2...");
3460 if (sbp2_hl_handle) {
3461 hpsb_unregister_highlevel(sbp2_hl_handle);
3462 sbp2_hl_handle = NULL;
3463 }
3464 }
3465
3466 /*
3467 * Since we are returning this count, it means that sbp2 must be loaded "after" the
3468 * host adapter module...
3469 */
3470 return(sbp2_host_count);
3471 }
3472
3473 /*
3474 * This function is called from sbp2_add_host, and is where we register our scsi host
3475 */
3476 static void sbp2scsi_register_scsi_host(struct sbp2scsi_host_info *hi)
3477 {
3478 struct Scsi_Host *shpnt = NULL;
3479
3480 SBP2_DEBUG("sbp2: sbp2scsi_register_scsi_host");
3481 SBP2_DEBUG("sbp2: sbp2scsi_host_info = %p", hi);
3482
3483 /*
3484 * Let's register with the scsi stack
3485 */
3486 if (global_scsi_tpnt) {
3487
3488 shpnt = scsi_register (global_scsi_tpnt, sizeof(void *));
3489
3490 /*
3491 * If successful, save off a context (to be used when SCSI commands are received)
3492 */
3493 if (shpnt) {
3494 shpnt->hostdata[0] = (unsigned long)hi;
3495 }
3496 }
3497
3498 return;
3499 }
3500
3501 /* Called when our module is released */
3502 static int sbp2scsi_release(struct Scsi_Host *host)
3503 {
3504 SBP2_DEBUG("sbp2: sbp2scsi_release");
3505 sbp2_cleanup();
3506 return(0);
3507 }
3508
3509 /* Called for contents of procfs */
3510 static const char *sbp2scsi_info (struct Scsi_Host *host)
3511 {
3512 return "IEEE-1394 SBP-2 protocol driver";
3513 }
3514
3515 MODULE_AUTHOR("James Goodwin <jamesg@filanet.com>");
3516 MODULE_DESCRIPTION("IEEE-1394 SBP-2 protocol driver");
3517 MODULE_SUPPORTED_DEVICE(SBP2_DEVICE_NAME);
3518
3519 /* SCSI host template */
3520 static Scsi_Host_Template driver_template = {
3521 name: "IEEE1394 SBP-2",
3522 detect: sbp2scsi_detect,
3523 release: sbp2scsi_release,
3524 info: sbp2scsi_info,
3525 queuecommand: sbp2scsi_queuecommand,
3526 abort: sbp2scsi_abort,
3527 reset: sbp2scsi_reset,
3528 bios_param: sbp2scsi_biosparam,
3529 can_queue: SBP2SCSI_MAX_OUTSTANDING_CMDS,
3530 this_id: -1,
3531 sg_tablesize: SBP2_MAX_SG_ELEMENTS,
3532 cmd_per_lun: SBP2SCSI_MAX_CMDS_PER_LUN,
3533 use_clustering: SBP2_CLUSTERING,
3534 emulated: 1
3535 };
3536
3537 #include "../scsi/scsi_module.c"
3538