OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [ieee1394/] [sbp2.c] - Blame information for rev 1774

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.