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