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