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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [scsi/] [dpt_i2o.c] - Blame information for rev 1780

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

Line No. Rev Author Line
1 1275 phoenix
/***************************************************************************
2
                          dpti.c  -  description
3
                             -------------------
4
    begin                : Thu Sep 7 2000
5
    copyright            : (C) 2000 by Adaptec
6
    email                : deanna_bonds@adaptec.com
7
 
8
                           July 30, 2001 First version being submitted
9
                           for inclusion in the kernel.  V2.4
10
 
11
    See README.dpti for history, notes, license info, and credits
12
 ***************************************************************************/
13
 
14
/***************************************************************************
15
 *                                                                         *
16
 *   This program is free software; you can redistribute it and/or modify  *
17
 *   it under the terms of the GNU General Public License as published by  *
18
 *   the Free Software Foundation; either version 2 of the License, or     *
19
 *   (at your option) any later version.                                   *
20
 *                                                                         *
21
 ***************************************************************************/
22
 
23
//#define DEBUG 1
24
//#define UARTDELAY 1
25
 
26
// On the real kernel ADDR32 should always be zero for 2.4. GFP_HIGH allocates
27
// high pages. Keep the macro around because of the broken unmerged ia64 tree
28
 
29
#define ADDR32 (0)
30
 
31
#include <linux/version.h>
32
#include <linux/module.h>
33
 
34
MODULE_AUTHOR("Deanna Bonds, with _lots_ of help from Mark Salyzyn");
35
MODULE_DESCRIPTION("Adaptec I2O RAID Driver");
36
 
37
////////////////////////////////////////////////////////////////
38
 
39
#include <linux/ioctl.h>        /* For SCSI-Passthrough */
40
#include <asm/uaccess.h>
41
 
42
#include <linux/stat.h>
43
#include <linux/slab.h>         /* for kmalloc() */
44
#include <linux/config.h>       /* for CONFIG_PCI */
45
#include <linux/pci.h>          /* for PCI support */
46
#include <linux/proc_fs.h>
47
#include <linux/blk.h>
48
#include <linux/delay.h>        /* for udelay */
49
#include <linux/tqueue.h>
50
#include <linux/interrupt.h>
51
#include <linux/kernel.h>       /* for printk */
52
#include <linux/sched.h>
53
#include <linux/reboot.h>
54
#include <linux/smp_lock.h>
55
 
56
#include <linux/timer.h>
57
#include <linux/string.h>
58
#include <linux/ioport.h>
59
#include <linux/stat.h>
60
 
61
#include <asm/processor.h>      /* for boot_cpu_data */
62
#include <asm/pgtable.h>
63
#include <asm/io.h>             /* for virt_to_bus, etc. */
64
 
65
#include "scsi.h"
66
#include "hosts.h"
67
#include "sd.h"
68
 
69
#include "dpt/dptsig.h"
70
#include "dpti.h"
71
 
72
/*============================================================================
73
 * Create a binary signature - this is read by dptsig
74
 * Needed for our management apps
75
 *============================================================================
76
 */
77
static dpt_sig_S DPTI_sig = {
78
        {'d', 'P', 't', 'S', 'i', 'G'}, SIG_VERSION,
79
#ifdef __i386__
80
        PROC_INTEL, PROC_386 | PROC_486 | PROC_PENTIUM | PROC_SEXIUM,
81
#elif defined(__ia64__)
82
        PROC_INTEL, PROC_IA64,
83
#elif defined(__sparc__)
84
        PROC_ULTRASPARC,
85
#elif defined(__alpha__)
86
        PROC_ALPHA ,
87
#else
88
        (-1),(-1)
89
#endif
90
         FT_HBADRVR, 0, OEM_DPT, OS_LINUX, CAP_OVERLAP, DEV_ALL,
91
        ADF_ALL_SC5, 0, 0, DPT_VERSION, DPT_REVISION, DPT_SUBREVISION,
92
        DPT_MONTH, DPT_DAY, DPT_YEAR, "Adaptec Linux I2O RAID Driver"
93
};
94
 
95
 
96
 
97
 
98
/*============================================================================
99
 * Globals
100
 *============================================================================
101
 */
102
 
103
DECLARE_MUTEX(adpt_configuration_lock);
104
 
105
static struct i2o_sys_tbl *sys_tbl = NULL;
106
static int sys_tbl_ind = 0;
107
static int sys_tbl_len = 0;
108
 
109
static adpt_hba* hbas[DPTI_MAX_HBA];
110
static adpt_hba* hba_chain = NULL;
111
static int hba_count = 0;
112
 
113
static struct file_operations adpt_fops = {
114
        ioctl: adpt_ioctl,
115
        open: adpt_open,
116
        release: adpt_close
117
};
118
 
119
#ifdef REBOOT_NOTIFIER
120
static struct notifier_block adpt_reboot_notifier =
121
{
122
         adpt_reboot_event,
123
         NULL,
124
 
125
};
126
#endif
127
 
128
/* Structures and definitions for synchronous message posting.
129
 * See adpt_i2o_post_wait() for description
130
 * */
131
struct adpt_i2o_post_wait_data
132
{
133
        int status;
134
        u32 id;
135
        adpt_wait_queue_head_t *wq;
136
        struct adpt_i2o_post_wait_data *next;
137
};
138
 
139
static struct adpt_i2o_post_wait_data *adpt_post_wait_queue = NULL;
140
static u32 adpt_post_wait_id = 0;
141
static spinlock_t adpt_post_wait_lock = SPIN_LOCK_UNLOCKED;
142
 
143
 
144
/*============================================================================
145
 *                              Functions
146
 *============================================================================
147
 */
148
 
149
static u8 adpt_read_blink_led(adpt_hba* host)
150
{
151
        if(host->FwDebugBLEDflag_P != 0) {
152
                if( readb(host->FwDebugBLEDflag_P) == 0xbc ){
153
                        return readb(host->FwDebugBLEDvalue_P);
154
                }
155
        }
156
        return 0;
157
}
158
 
159
/*============================================================================
160
 * Scsi host template interface functions
161
 *============================================================================
162
 */
163
 
164
static struct pci_device_id dptids[] = {
165
        { PCI_DPT_VENDOR_ID, PCI_DPT_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
166
        { PCI_DPT_VENDOR_ID, PCI_DPT_RAPTOR_DEVICE_ID, PCI_ANY_ID, PCI_ANY_ID,},
167
        { 0, }
168
};
169
MODULE_DEVICE_TABLE(pci,dptids);
170
 
171
static int adpt_detect(Scsi_Host_Template* sht)
172
{
173
        struct pci_dev *pDev = NULL;
174
        adpt_hba* pHba;
175
 
176
        adpt_init();
177
        sht->use_new_eh_code = 1;
178
 
179
        PINFO("Detecting Adaptec I2O RAID controllers...\n");
180
 
181
        /* search for all Adatpec I2O RAID cards */
182
        while ((pDev = pci_find_device( PCI_DPT_VENDOR_ID, PCI_ANY_ID, pDev))) {
183
                if(pDev->device == PCI_DPT_DEVICE_ID ||
184
                   pDev->device == PCI_DPT_RAPTOR_DEVICE_ID){
185
                        if(adpt_install_hba(sht, pDev) ){
186
                                PERROR("Could not Init an I2O RAID device\n");
187
                                PERROR("Will not try to detect others.\n");
188
                                return hba_count-1;
189
                        }
190
                }
191
        }
192
 
193
        /* In INIT state, Activate IOPs */
194
        for (pHba = hba_chain; pHba; pHba = pHba->next) {
195
                // Activate does get status , init outbound, and get hrt
196
                if (adpt_i2o_activate_hba(pHba) < 0) {
197
                        adpt_i2o_delete_hba(pHba);
198
                }
199
        }
200
 
201
 
202
        /* Active IOPs in HOLD state */
203
 
204
rebuild_sys_tab:
205
        if (hba_chain == NULL)
206
                return 0;
207
 
208
        /*
209
         * If build_sys_table fails, we kill everything and bail
210
         * as we can't init the IOPs w/o a system table
211
         */
212
        if (adpt_i2o_build_sys_table() < 0) {
213
                adpt_i2o_sys_shutdown();
214
                return 0;
215
        }
216
 
217
        PDEBUG("HBA's in HOLD state\n");
218
 
219
        /* If IOP don't get online, we need to rebuild the System table */
220
        for (pHba = hba_chain; pHba; pHba = pHba->next) {
221
                if (adpt_i2o_online_hba(pHba) < 0) {
222
                        adpt_i2o_delete_hba(pHba);
223
                        goto rebuild_sys_tab;
224
                }
225
        }
226
 
227
        /* Active IOPs now in OPERATIONAL state */
228
        PDEBUG("HBA's in OPERATIONAL state\n");
229
 
230
        printk(KERN_INFO"dpti: If you have a lot of devices this could take a few minutes.\n");
231
        for (pHba = hba_chain; pHba; pHba = pHba->next) {
232
                printk(KERN_INFO"%s: Reading the hardware resource table.\n", pHba->name);
233
                if (adpt_i2o_lct_get(pHba) < 0){
234
                        adpt_i2o_delete_hba(pHba);
235
                        continue;
236
                }
237
 
238
                if (adpt_i2o_parse_lct(pHba) < 0){
239
                        adpt_i2o_delete_hba(pHba);
240
                        continue;
241
                }
242
                adpt_inquiry(pHba);
243
        }
244
 
245
        for (pHba = hba_chain; pHba; pHba = pHba->next) {
246
                if( adpt_scsi_register(pHba,sht) < 0){
247
                        adpt_i2o_delete_hba(pHba);
248
                        continue;
249
                }
250
                pHba->initialized = TRUE;
251
                pHba->state &= ~DPTI_STATE_RESET;
252
        }
253
 
254
        // Register our control device node
255
        // nodes will need to be created in /dev to access this
256
        // the nodes can not be created from within the driver
257
        if (hba_count && register_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER, &adpt_fops)) {
258
                adpt_i2o_sys_shutdown();
259
                return 0;
260
        }
261
        return hba_count;
262
}
263
 
264
 
265
/*
266
 * scsi_unregister will be called AFTER we return.
267
 */
268
static int adpt_release(struct Scsi_Host *host)
269
{
270
        adpt_hba* pHba = (adpt_hba*) host->hostdata[0];
271
//      adpt_i2o_quiesce_hba(pHba);
272
        adpt_i2o_delete_hba(pHba);
273
        return 0;
274
}
275
 
276
 
277
static void adpt_inquiry(adpt_hba* pHba)
278
{
279
        u32 msg[14];
280
        u32 *mptr;
281
        u32 *lenptr;
282
        int direction;
283
        int scsidir;
284
        u32 len;
285
        u32 reqlen;
286
        u8* buf;
287
        u8  scb[16];
288
        s32 rcode;
289
 
290
        memset(msg, 0, sizeof(msg));
291
        buf = (u8*)kmalloc(80,GFP_KERNEL|ADDR32);
292
        if(!buf){
293
                printk(KERN_ERR"%s: Could not allocate buffer\n",pHba->name);
294
                return;
295
        }
296
        memset((void*)buf, 0, 36);
297
 
298
        len = 36;
299
        direction = 0x00000000;
300
        scsidir  =0x40000000;   // DATA IN  (iop<--dev)
301
 
302
        reqlen = 14;            // SINGLE SGE
303
        /* Stick the headers on */
304
        msg[0] = reqlen<<16 | SGL_OFFSET_12;
305
        msg[1] = (0xff<<24|HOST_TID<<12|ADAPTER_TID);
306
        msg[2] = 0;
307
        msg[3]  = 0;
308
        // Adaptec/DPT Private stuff 
309
        msg[4] = I2O_CMD_SCSI_EXEC|DPT_ORGANIZATION_ID<<16;
310
        msg[5] = ADAPTER_TID | 1<<16 /* Interpret*/;
311
        /* Direction, disconnect ok | sense data | simple queue , CDBLen */
312
        // I2O_SCB_FLAG_ENABLE_DISCONNECT | 
313
        // I2O_SCB_FLAG_SIMPLE_QUEUE_TAG | 
314
        // I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
315
        msg[6] = scsidir|0x20a00000| 6 /* cmd len*/;
316
 
317
        mptr=msg+7;
318
 
319
        memset(scb, 0, sizeof(scb));
320
        // Write SCSI command into the message - always 16 byte block 
321
        scb[0] = INQUIRY;
322
        scb[1] = 0;
323
        scb[2] = 0;
324
        scb[3] = 0;
325
        scb[4] = 36;
326
        scb[5] = 0;
327
        // Don't care about the rest of scb
328
 
329
        memcpy(mptr, scb, sizeof(scb));
330
        mptr+=4;
331
        lenptr=mptr++;          /* Remember me - fill in when we know */
332
 
333
        /* Now fill in the SGList and command */
334
        *lenptr = len;
335
        *mptr++ = 0xD0000000|direction|len;
336
        *mptr++ = virt_to_bus(buf);
337
 
338
        // Send it on it's way
339
        rcode = adpt_i2o_post_wait(pHba, msg, reqlen<<2, 120);
340
        if (rcode != 0) {
341
                sprintf(pHba->detail, "Adaptec I2O RAID");
342
                printk(KERN_INFO "%s: Inquiry Error (%d)\n",pHba->name,rcode);
343
        } else {
344
                memset(pHba->detail, 0, sizeof(pHba->detail));
345
                memcpy(&(pHba->detail), "Vendor: Adaptec ", 16);
346
                memcpy(&(pHba->detail[16]), " Model: ", 8);
347
                memcpy(&(pHba->detail[24]), (u8*) &buf[16], 16);
348
                memcpy(&(pHba->detail[40]), " FW: ", 4);
349
                memcpy(&(pHba->detail[44]), (u8*) &buf[32], 4);
350
                pHba->detail[48] = '\0';        /* precautionary */
351
        }
352
        kfree(buf);
353
        adpt_i2o_status_get(pHba);
354
        return ;
355
}
356
 
357
 
358
static void adpt_select_queue_depths(struct Scsi_Host *host, Scsi_Device * devicelist)
359
{
360
        Scsi_Device *device;    /* scsi layer per device information */
361
        adpt_hba* pHba;
362
 
363
        pHba = (adpt_hba *) host->hostdata[0];
364
 
365
        for (device = devicelist; device != NULL; device = device->next) {
366
                if (device->host != host) {
367
                        continue;
368
                }
369
                if (host->can_queue) {
370
                        device->queue_depth =  host->can_queue - 1;
371
                } else {
372
                        device->queue_depth = 1;
373
                }
374
        }
375
}
376
 
377
static int adpt_queue(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
378
{
379
        adpt_hba* pHba = NULL;
380
        struct adpt_device* pDev = NULL;        /* dpt per device information */
381
        ulong timeout = jiffies + (TMOUT_SCSI*HZ);
382
 
383
        cmd->scsi_done = done;
384
        /*
385
         * SCSI REQUEST_SENSE commands will be executed automatically by the
386
         * Host Adapter for any errors, so they should not be executed
387
         * explicitly unless the Sense Data is zero indicating that no error
388
         * occurred.
389
         */
390
 
391
        if ((cmd->cmnd[0] == REQUEST_SENSE) && (cmd->sense_buffer[0] != 0)) {
392
                cmd->result = (DID_OK << 16);
393
                cmd->scsi_done(cmd);
394
                return 0;
395
        }
396
 
397
        pHba = (adpt_hba*)cmd->host->hostdata[0];
398
        if (!pHba) {
399
                return FAILED;
400
        }
401
 
402
        rmb();
403
        /*
404
         * TODO: I need to block here if I am processing ioctl cmds
405
         * but if the outstanding cmds all finish before the ioctl,
406
         * the scsi-core will not know to start sending cmds to me again.
407
         * I need to a way to restart the scsi-cores queues or should I block
408
         * calling scsi_done on the outstanding cmds instead
409
         * for now we don't set the IOCTL state
410
         */
411
        if(((pHba->state) & DPTI_STATE_IOCTL) || ((pHba->state) & DPTI_STATE_RESET)) {
412
                pHba->host->last_reset = jiffies;
413
                pHba->host->resetting = 1;
414
                return 1;
415
        }
416
 
417
        if(cmd->eh_state != SCSI_STATE_QUEUED){
418
                // If we are not doing error recovery
419
                mod_timer(&cmd->eh_timeout, timeout);
420
        }
421
 
422
        // TODO if the cmd->device if offline then I may need to issue a bus rescan
423
        // followed by a get_lct to see if the device is there anymore
424
        if((pDev = (struct adpt_device*) (cmd->device->hostdata)) == NULL) {
425
                /*
426
                 * First command request for this device.  Set up a pointer
427
                 * to the device structure.  This should be a TEST_UNIT_READY
428
                 * command from scan_scsis_single.
429
                 */
430
                if ((pDev = adpt_find_device(pHba, (u32)cmd->channel, (u32)cmd->target, (u32)cmd-> lun)) == NULL) {
431
                        // TODO: if any luns are at this bus, scsi id then fake a TEST_UNIT_READY and INQUIRY response 
432
                        // with type 7F (for all luns less than the max for this bus,id) so the lun scan will continue.
433
                        cmd->result = (DID_NO_CONNECT << 16);
434
                        cmd->scsi_done(cmd);
435
                        return 0;
436
                }
437
                (struct adpt_device*)(cmd->device->hostdata) = pDev;
438
        }
439
        pDev->pScsi_dev = cmd->device;
440
 
441
        /*
442
         * If we are being called from when the device is being reset,
443
         * delay processing of the command until later.
444
         */
445
        if (pDev->state & DPTI_DEV_RESET ) {
446
                return FAILED;
447
        }
448
        return adpt_scsi_to_i2o(pHba, cmd, pDev);
449
}
450
 
451
static int adpt_bios_param(Disk* disk, kdev_t dev, int geom[])
452
{
453
        int heads=-1;
454
        int sectors=-1;
455
        int cylinders=-1;
456
 
457
        // *** First lets set the default geometry ****
458
 
459
        // If the capacity is less than ox2000
460
        if (disk->capacity < 0x2000 ) { // floppy
461
                heads = 18;
462
                sectors = 2;
463
        }
464
        // else if between 0x2000 and 0x20000
465
        else if (disk->capacity < 0x20000) {
466
                heads = 64;
467
                sectors = 32;
468
        }
469
        // else if between 0x20000 and 0x40000
470
        else if (disk->capacity < 0x40000) {
471
                heads = 65;
472
                sectors = 63;
473
        }
474
        // else if between 0x4000 and 0x80000
475
        else if (disk->capacity < 0x80000) {
476
                heads = 128;
477
                sectors = 63;
478
        }
479
        // else if greater than 0x80000
480
        else {
481
                heads = 255;
482
                sectors = 63;
483
        }
484
        cylinders = disk->capacity / (heads * sectors);
485
 
486
        // Special case if CDROM
487
        if(disk->device->type == 5) {  // CDROM
488
                heads = 252;
489
                sectors = 63;
490
                cylinders = 1111;
491
        }
492
 
493
        geom[0] = heads;
494
        geom[1] = sectors;
495
        geom[2] = cylinders;
496
 
497
        PDEBUG("adpt_bios_param: exit\n");
498
        return 0;
499
}
500
 
501
 
502
static const char *adpt_info(struct Scsi_Host *host)
503
{
504
        adpt_hba* pHba;
505
 
506
        pHba = (adpt_hba *) host->hostdata[0];
507
        return (char *) (pHba->detail);
508
}
509
 
510
static int adpt_proc_info(char *buffer, char **start, off_t offset,
511
                  int length, int hostno, int inout)
512
{
513
        struct adpt_device* d;
514
        int id;
515
        int chan;
516
        int len = 0;
517
        int begin = 0;
518
        int pos = 0;
519
        adpt_hba* pHba;
520
        struct Scsi_Host *host;
521
        int unit;
522
 
523
        *start = buffer;
524
        if (inout == TRUE) {
525
                /*
526
                 * The user has done a write and wants us to take the
527
                 * data in the buffer and do something with it.
528
                 * proc_scsiwrite calls us with inout = 1
529
                 *
530
                 * Read data from buffer (writing to us) - NOT SUPPORTED
531
                 */
532
                return -EINVAL;
533
        }
534
 
535
        /*
536
         * inout = 0 means the user has done a read and wants information
537
         * returned, so we write information about the cards into the buffer
538
         * proc_scsiread() calls us with inout = 0
539
         */
540
 
541
        // Find HBA (host bus adapter) we are looking for
542
        down(&adpt_configuration_lock);
543
        for (pHba = hba_chain; pHba; pHba = pHba->next) {
544
                if (pHba->host->host_no == hostno) {
545
                        break;  /* found adapter */
546
                }
547
        }
548
        up(&adpt_configuration_lock);
549
        if (pHba == NULL) {
550
                return 0;
551
        }
552
        host = pHba->host;
553
 
554
        len  = sprintf(buffer    , "Adaptec I2O RAID Driver Version: %s\n\n", DPT_I2O_VERSION);
555
        len += sprintf(buffer+len, "%s\n", pHba->detail);
556
        len += sprintf(buffer+len, "SCSI Host=scsi%d  Control Node=/dev/%s  irq=%d\n",
557
                        pHba->host->host_no, pHba->name, host->irq);
558
        len += sprintf(buffer+len, "\tpost fifo size  = %d\n\treply fifo size = %d\n\tsg table size   = %d\n\n",
559
                        host->can_queue, (int) pHba->reply_fifo_size , host->sg_tablesize);
560
 
561
        pos = begin + len;
562
 
563
        /* CHECKPOINT */
564
        if(pos > offset + length) {
565
                goto stop_output;
566
        }
567
        if(pos <= offset) {
568
                /*
569
                 * If we haven't even written to where we last left
570
                 * off (the last time we were called), reset the
571
                 * beginning pointer.
572
                 */
573
                len = 0;
574
                begin = pos;
575
        }
576
        len +=  sprintf(buffer+len, "Devices:\n");
577
        for(chan = 0; chan < MAX_CHANNEL; chan++) {
578
                for(id = 0; id < MAX_ID; id++) {
579
                        d = pHba->channel[chan].device[id];
580
                        while(d){
581
                                len += sprintf(buffer+len,"\t%-24.24s", d->pScsi_dev->vendor);
582
                                len += sprintf(buffer+len," Rev: %-8.8s\n", d->pScsi_dev->rev);
583
                                pos = begin + len;
584
 
585
 
586
                                /* CHECKPOINT */
587
                                if(pos > offset + length) {
588
                                        goto stop_output;
589
                                }
590
                                if(pos <= offset) {
591
                                        len = 0;
592
                                        begin = pos;
593
                                }
594
 
595
                                unit = d->pI2o_dev->lct_data.tid;
596
                                len += sprintf(buffer+len, "\tTID=%d, (Channel=%d, Target=%d, Lun=%d)  (%s)\n\n",
597
                                               unit, (int)d->scsi_channel, (int)d->scsi_id, (int)d->scsi_lun,
598
                                               d->pScsi_dev->online? "online":"offline");
599
                                pos = begin + len;
600
 
601
                                /* CHECKPOINT */
602
                                if(pos > offset + length) {
603
                                        goto stop_output;
604
                                }
605
                                if(pos <= offset) {
606
                                        len = 0;
607
                                        begin = pos;
608
                                }
609
 
610
                                d = d->next_lun;
611
                        }
612
                }
613
        }
614
 
615
        /*
616
         * begin is where we last checked our position with regards to offset
617
         * begin is always less than offset.  len is relative to begin.  It
618
         * is the number of bytes written past begin
619
         *
620
         */
621
stop_output:
622
        /* stop the output and calculate the correct length */
623
        *(buffer + len) = '\0';
624
 
625
        *start = buffer + (offset - begin);     /* Start of wanted data */
626
        len -= (offset - begin);
627
        if(len > length) {
628
                len = length;
629
        } else if(len < 0){
630
                len = 0;
631
                **start = '\0';
632
        }
633
        return len;
634
}
635
 
636
 
637
/*===========================================================================
638
 * Error Handling routines
639
 *===========================================================================
640
 */
641
 
642
static int adpt_abort(Scsi_Cmnd * cmd)
643
{
644
        adpt_hba* pHba = NULL;  /* host bus adapter structure */
645
        struct adpt_device* dptdevice;  /* dpt per device information */
646
        u32 msg[5];
647
        int rcode;
648
 
649
        if(cmd->serial_number == 0){
650
                return FAILED;
651
        }
652
        pHba = (adpt_hba*) cmd->host->hostdata[0];
653
        printk(KERN_INFO"%s: Trying to Abort cmd=%ld\n",pHba->name, cmd->serial_number);
654
        if ((dptdevice = (void*) (cmd->device->hostdata)) == NULL) {
655
                printk(KERN_ERR "%s: Unable to abort: No device in cmnd\n",pHba->name);
656
                return FAILED;
657
        }
658
 
659
        memset(msg, 0, sizeof(msg));
660
        msg[0] = FIVE_WORD_MSG_SIZE|SGL_OFFSET_0;
661
        msg[1] = I2O_CMD_SCSI_ABORT<<24|HOST_TID<<12|dptdevice->tid;
662
        msg[2] = 0;
663
        msg[3]= 0;
664
        msg[4] = (u32)cmd;
665
        if( (rcode = adpt_i2o_post_wait(pHba, msg, sizeof(msg), FOREVER)) != 0){
666
                if(rcode == -EOPNOTSUPP ){
667
                        printk(KERN_INFO"%s: Abort cmd not supported\n",pHba->name);
668
                        return FAILED;
669
                }
670
                printk(KERN_INFO"%s: Abort cmd=%ld failed.\n",pHba->name, cmd->serial_number);
671
                return FAILED;
672
        }
673
        printk(KERN_INFO"%s: Abort cmd=%ld complete.\n",pHba->name, cmd->serial_number);
674
        return SUCCESS;
675
}
676
 
677
 
678
#define I2O_DEVICE_RESET 0x27
679
// This is the same for BLK and SCSI devices
680
// NOTE this is wrong in the i2o.h definitions
681
// This is not currently supported by our adapter but we issue it anyway
682
static int adpt_device_reset(Scsi_Cmnd* cmd)
683
{
684
        adpt_hba* pHba;
685
        u32 msg[4];
686
        u32 rcode;
687
        int old_state;
688
        struct adpt_device* d = (void*) cmd->device->hostdata;
689
 
690
        pHba = (void*) cmd->host->hostdata[0];
691
        printk(KERN_INFO"%s: Trying to reset device\n",pHba->name);
692
        if (!d) {
693
                printk(KERN_INFO"%s: Reset Device: Device Not found\n",pHba->name);
694
                return FAILED;
695
        }
696
        memset(msg, 0, sizeof(msg));
697
        msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
698
        msg[1] = (I2O_DEVICE_RESET<<24|HOST_TID<<12|d->tid);
699
        msg[2] = 0;
700
        msg[3] = 0;
701
 
702
        old_state = d->state;
703
        d->state |= DPTI_DEV_RESET;
704
        if( (rcode = adpt_i2o_post_wait(pHba, (void*)msg,sizeof(msg), FOREVER)) ){
705
                d->state = old_state;
706
                if(rcode == -EOPNOTSUPP ){
707
                        printk(KERN_INFO"%s: Device reset not supported\n",pHba->name);
708
                        return FAILED;
709
                }
710
                printk(KERN_INFO"%s: Device reset failed\n",pHba->name);
711
                return FAILED;
712
        } else {
713
                d->state = old_state;
714
                printk(KERN_INFO"%s: Device reset successful\n",pHba->name);
715
                return SUCCESS;
716
        }
717
}
718
 
719
 
720
#define I2O_HBA_BUS_RESET 0x87
721
// This version of bus reset is called by the eh_error handler
722
static int adpt_bus_reset(Scsi_Cmnd* cmd)
723
{
724
        adpt_hba* pHba;
725
        u32 msg[4];
726
 
727
        pHba = (adpt_hba*)cmd->host->hostdata[0];
728
        memset(msg, 0, sizeof(msg));
729
        printk(KERN_WARNING"%s: Bus reset: SCSI Bus %d: tid: %d\n",pHba->name, cmd->channel,pHba->channel[cmd->channel].tid );
730
        msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
731
        msg[1] = (I2O_HBA_BUS_RESET<<24|HOST_TID<<12|pHba->channel[cmd->channel].tid);
732
        msg[2] = 0;
733
        msg[3] = 0;
734
        if(adpt_i2o_post_wait(pHba, (void*)msg,sizeof(msg), FOREVER) ){
735
                printk(KERN_WARNING"%s: Bus reset failed.\n",pHba->name);
736
                return FAILED;
737
        } else {
738
                printk(KERN_WARNING"%s: Bus reset success.\n",pHba->name);
739
                return SUCCESS;
740
        }
741
}
742
 
743
// This version of reset is called by the eh_error_handler
744
static int adpt_reset(Scsi_Cmnd* cmd)
745
{
746
        adpt_hba* pHba;
747
        int rcode;
748
        pHba = (adpt_hba*)cmd->host->hostdata[0];
749
        printk(KERN_WARNING"%s: Hba Reset: scsi id %d: tid: %d\n",pHba->name,cmd->channel,pHba->channel[cmd->channel].tid );
750
        rcode =  adpt_hba_reset(pHba);
751
        if(rcode == 0){
752
                printk(KERN_WARNING"%s: HBA reset complete\n",pHba->name);
753
                return SUCCESS;
754
        } else {
755
                printk(KERN_WARNING"%s: HBA reset failed (%x)\n",pHba->name, rcode);
756
                return FAILED;
757
        }
758
}
759
 
760
// This version of reset is called by the ioctls and indirectly from eh_error_handler via adpt_reset
761
static int adpt_hba_reset(adpt_hba* pHba)
762
{
763
        int rcode;
764
 
765
        pHba->state |= DPTI_STATE_RESET;
766
 
767
        // Activate does get status , init outbound, and get hrt
768
        if ((rcode=adpt_i2o_activate_hba(pHba)) < 0) {
769
                printk(KERN_ERR "%s: Could not activate\n", pHba->name);
770
                adpt_i2o_delete_hba(pHba);
771
                return rcode;
772
        }
773
 
774
        if ((rcode=adpt_i2o_build_sys_table()) < 0) {
775
                adpt_i2o_delete_hba(pHba);
776
                return rcode;
777
        }
778
        PDEBUG("%s: in HOLD state\n",pHba->name);
779
 
780
        if ((rcode=adpt_i2o_online_hba(pHba)) < 0) {
781
                adpt_i2o_delete_hba(pHba);
782
                return rcode;
783
        }
784
        PDEBUG("%s: in OPERATIONAL state\n",pHba->name);
785
 
786
        if ((rcode=adpt_i2o_lct_get(pHba)) < 0){
787
                adpt_i2o_delete_hba(pHba);
788
                return rcode;
789
        }
790
 
791
        if ((rcode=adpt_i2o_reparse_lct(pHba)) < 0){
792
                adpt_i2o_delete_hba(pHba);
793
                return rcode;
794
        }
795
        pHba->state &= ~DPTI_STATE_RESET;
796
 
797
        adpt_fail_posted_scbs(pHba);
798
        return 0;        /* return success */
799
}
800
 
801
/*===========================================================================
802
 *
803
 *===========================================================================
804
 */
805
 
806
 
807
static void adpt_i2o_sys_shutdown(void)
808
{
809
        adpt_hba *pHba, *pNext;
810
        struct adpt_i2o_post_wait_data *p1, *p2;
811
 
812
         printk(KERN_INFO"Shutting down Adaptec I2O controllers.\n");
813
         printk(KERN_INFO"   This could take a few minutes if there are many devices attached\n");
814
        /* Delete all IOPs from the controller chain */
815
        /* They should have already been released by the
816
         * scsi-core
817
         */
818
        for (pHba = hba_chain; pHba; pHba = pNext) {
819
                pNext = pHba->next;
820
                adpt_i2o_delete_hba(pHba);
821
        }
822
 
823
        /* Remove any timedout entries from the wait queue.  */
824
        p2 = NULL;
825
//      spin_lock_irqsave(&adpt_post_wait_lock, flags);
826
        /* Nothing should be outstanding at this point so just
827
         * free them
828
         */
829
        for(p1 = adpt_post_wait_queue; p1; p2 = p1, p1 = p2->next) {
830
                kfree(p1);
831
        }
832
//      spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
833
        adpt_post_wait_queue = 0;
834
 
835
         printk(KERN_INFO "Adaptec I2O controllers down.\n");
836
}
837
 
838
/*
839
 * reboot/shutdown notification.
840
 *
841
 * - Quiesce each IOP in the system
842
 *
843
 */
844
 
845
#ifdef REBOOT_NOTIFIER
846
static int adpt_reboot_event(struct notifier_block *n, ulong code, void *p)
847
{
848
 
849
         if(code != SYS_RESTART && code != SYS_HALT && code != SYS_POWER_OFF)
850
                  return NOTIFY_DONE;
851
 
852
         adpt_i2o_sys_shutdown();
853
 
854
         return NOTIFY_DONE;
855
}
856
#endif
857
 
858
 
859
static int adpt_install_hba(Scsi_Host_Template* sht, struct pci_dev* pDev)
860
{
861
 
862
        adpt_hba* pHba = NULL;
863
        adpt_hba* p = NULL;
864
        ulong base_addr0_phys = 0;
865
        ulong base_addr1_phys = 0;
866
        u32 hba_map0_area_size = 0;
867
        u32 hba_map1_area_size = 0;
868
        ulong base_addr_virt = 0;
869
        ulong msg_addr_virt = 0;
870
 
871
        int raptorFlag = FALSE;
872
        int i;
873
 
874
        if(pci_enable_device(pDev)) {
875
                return -EINVAL;
876
        }
877
        pci_set_master(pDev);
878
 
879
        base_addr0_phys = pci_resource_start(pDev,0);
880
        hba_map0_area_size = pci_resource_len(pDev,0);
881
 
882
        // Check if standard PCI card or single BAR Raptor
883
        if(pDev->device == PCI_DPT_DEVICE_ID){
884
                if(pDev->subsystem_device >=0xc032 && pDev->subsystem_device <= 0xc03b){
885
                        // Raptor card with this device id needs 4M
886
                        hba_map0_area_size = 0x400000;
887
                } else { // Not Raptor - it is a PCI card
888
                        if(hba_map0_area_size > 0x100000 ){
889
                                hba_map0_area_size = 0x100000;
890
                        }
891
                }
892
        } else {// Raptor split BAR config
893
                // Use BAR1 in this configuration
894
                base_addr1_phys = pci_resource_start(pDev,1);
895
                hba_map1_area_size = pci_resource_len(pDev,1);
896
                raptorFlag = TRUE;
897
        }
898
 
899
 
900
        base_addr_virt = (ulong)ioremap(base_addr0_phys,hba_map0_area_size);
901
        if(base_addr_virt == 0) {
902
                PERROR("dpti: adpt_config_hba: io remap failed\n");
903
                return -EINVAL;
904
        }
905
 
906
        if(raptorFlag == TRUE) {
907
                msg_addr_virt = (ulong)ioremap(base_addr1_phys, hba_map1_area_size );
908
                if(msg_addr_virt == 0) {
909
                        PERROR("dpti: adpt_config_hba: io remap failed on BAR1\n");
910
                        iounmap((void*)base_addr_virt);
911
                        return -EINVAL;
912
                }
913
        } else {
914
                msg_addr_virt = base_addr_virt;
915
        }
916
 
917
        // Allocate and zero the data structure
918
        pHba = kmalloc(sizeof(adpt_hba), GFP_KERNEL);
919
        if( pHba == NULL) {
920
                if(msg_addr_virt != base_addr_virt){
921
                        iounmap((void*)msg_addr_virt);
922
                }
923
                iounmap((void*)base_addr_virt);
924
                return -ENOMEM;
925
        }
926
        memset(pHba, 0, sizeof(adpt_hba));
927
 
928
        down(&adpt_configuration_lock);
929
        for(i=0;i<DPTI_MAX_HBA;i++) {
930
                if(hbas[i]==NULL) {
931
                        hbas[i]=pHba;
932
                        break;
933
                }
934
        }
935
 
936
        if(hba_chain != NULL){
937
                for(p = hba_chain; p->next; p = p->next);
938
                p->next = pHba;
939
        } else {
940
                hba_chain = pHba;
941
        }
942
        pHba->next = NULL;
943
        pHba->unit = hba_count;
944
        sprintf(pHba->name, "dpti%d", i);
945
        hba_count++;
946
 
947
        up(&adpt_configuration_lock);
948
 
949
        pHba->pDev = pDev;
950
        pHba->base_addr_phys = base_addr0_phys;
951
 
952
        // Set up the Virtual Base Address of the I2O Device
953
        pHba->base_addr_virt = base_addr_virt;
954
        pHba->msg_addr_virt = msg_addr_virt;
955
        pHba->irq_mask = (ulong)(base_addr_virt+0x30);
956
        pHba->post_port = (ulong)(base_addr_virt+0x40);
957
        pHba->reply_port = (ulong)(base_addr_virt+0x44);
958
 
959
        pHba->hrt = NULL;
960
        pHba->lct = NULL;
961
        pHba->lct_size = 0;
962
        pHba->status_block = NULL;
963
        pHba->post_count = 0;
964
        pHba->state = DPTI_STATE_RESET;
965
        pHba->pDev = pDev;
966
        pHba->devices = NULL;
967
 
968
        // Initializing the spinlocks
969
        spin_lock_init(&pHba->state_lock);
970
 
971
        if(raptorFlag == 0){
972
                printk(KERN_INFO"Adaptec I2O RAID controller %d at %lx size=%x irq=%d\n",
973
                        hba_count-1, base_addr_virt, hba_map0_area_size, pDev->irq);
974
        } else {
975
                printk(KERN_INFO"Adaptec I2O RAID controller %d irq=%d\n",hba_count-1, pDev->irq);
976
                printk(KERN_INFO"     BAR0 %lx - size= %x\n",base_addr_virt,hba_map0_area_size);
977
                printk(KERN_INFO"     BAR1 %lx - size= %x\n",msg_addr_virt,hba_map1_area_size);
978
        }
979
 
980
        if (request_irq (pDev->irq, adpt_isr, SA_SHIRQ, pHba->name, pHba)) {
981
                printk(KERN_ERR"%s: Couldn't register IRQ %d\n", pHba->name, pDev->irq);
982
                adpt_i2o_delete_hba(pHba);
983
                return -EINVAL;
984
        }
985
 
986
        return 0;
987
}
988
 
989
 
990
static void adpt_i2o_delete_hba(adpt_hba* pHba)
991
{
992
        adpt_hba* p1;
993
        adpt_hba* p2;
994
        struct i2o_device* d;
995
        struct i2o_device* next;
996
        int i;
997
        int j;
998
        struct adpt_device* pDev;
999
        struct adpt_device* pNext;
1000
 
1001
 
1002
        down(&adpt_configuration_lock);
1003
        // scsi_unregister calls our adpt_release which
1004
        // does a quiese
1005
        if(pHba->host){
1006
                free_irq(pHba->host->irq, pHba);
1007
        }
1008
        for(i=0;i<DPTI_MAX_HBA;i++) {
1009
                if(hbas[i]==pHba) {
1010
                        hbas[i] = NULL;
1011
                }
1012
        }
1013
        p2 = NULL;
1014
        for( p1 = hba_chain; p1; p2 = p1,p1=p1->next){
1015
                if(p1 == pHba) {
1016
                        if(p2) {
1017
                                p2->next = p1->next;
1018
                        } else {
1019
                                hba_chain = p1->next;
1020
                        }
1021
                        break;
1022
                }
1023
        }
1024
 
1025
        hba_count--;
1026
        up(&adpt_configuration_lock);
1027
 
1028
        iounmap((void*)pHba->base_addr_virt);
1029
        if(pHba->msg_addr_virt != pHba->base_addr_virt){
1030
                iounmap((void*)pHba->msg_addr_virt);
1031
        }
1032
        if(pHba->hrt) {
1033
                kfree(pHba->hrt);
1034
        }
1035
        if(pHba->lct){
1036
                kfree(pHba->lct);
1037
        }
1038
        if(pHba->status_block) {
1039
                kfree(pHba->status_block);
1040
        }
1041
        if(pHba->reply_pool){
1042
                kfree(pHba->reply_pool);
1043
        }
1044
 
1045
        for(d = pHba->devices; d ; d = next){
1046
                next = d->next;
1047
                kfree(d);
1048
        }
1049
        for(i = 0 ; i < pHba->top_scsi_channel ; i++){
1050
                for(j = 0; j < MAX_ID; j++){
1051
                        if(pHba->channel[i].device[j] != NULL){
1052
                                for(pDev = pHba->channel[i].device[j]; pDev; pDev = pNext){
1053
                                        pNext = pDev->next_lun;
1054
                                        kfree(pDev);
1055
                                }
1056
                        }
1057
                }
1058
        }
1059
        kfree(pHba);
1060
 
1061
        if(hba_count <= 0){
1062
                unregister_chrdev(DPTI_I2O_MAJOR, DPT_DRIVER);
1063
        }
1064
}
1065
 
1066
 
1067
static int adpt_init(void)
1068
{
1069
        int i;
1070
 
1071
        printk(KERN_INFO"Loading Adaptec I2O RAID: Version " DPT_I2O_VERSION "\n");
1072
        for (i = 0; i < DPTI_MAX_HBA; i++) {
1073
                hbas[i] = NULL;
1074
        }
1075
#ifdef REBOOT_NOTIFIER
1076
        register_reboot_notifier(&adpt_reboot_notifier);
1077
#endif
1078
 
1079
        return 0;
1080
}
1081
 
1082
 
1083
static struct adpt_device* adpt_find_device(adpt_hba* pHba, u32 chan, u32 id, u32 lun)
1084
{
1085
        struct adpt_device* d;
1086
 
1087
        if(chan < 0 || chan >= MAX_CHANNEL)
1088
                return NULL;
1089
 
1090
        if( pHba->channel[chan].device == NULL){
1091
                printk(KERN_DEBUG"Adaptec I2O RAID: Trying to find device before they are allocated\n");
1092
                return NULL;
1093
        }
1094
 
1095
        d = pHba->channel[chan].device[id];
1096
        if(!d || d->tid == 0) {
1097
                return NULL;
1098
        }
1099
 
1100
        /* If it is the only lun at that address then this should match*/
1101
        if(d->scsi_lun == lun){
1102
                return d;
1103
        }
1104
 
1105
        /* else we need to look through all the luns */
1106
        for(d=d->next_lun ; d ; d = d->next_lun){
1107
                if(d->scsi_lun == lun){
1108
                        return d;
1109
                }
1110
        }
1111
        return NULL;
1112
}
1113
 
1114
 
1115
static int adpt_i2o_post_wait(adpt_hba* pHba, u32* msg, int len, int timeout)
1116
{
1117
        // I used my own version of the WAIT_QUEUE_HEAD
1118
        // to handle some version differences
1119
        // When embedded in the kernel this could go back to the vanilla one
1120
        ADPT_DECLARE_WAIT_QUEUE_HEAD(adpt_wq_i2o_post);
1121
        int status = 0;
1122
        ulong flags = 0;
1123
        struct adpt_i2o_post_wait_data *p1, *p2;
1124
        struct adpt_i2o_post_wait_data *wait_data =
1125
                kmalloc(sizeof(struct adpt_i2o_post_wait_data),GFP_KERNEL);
1126
        adpt_wait_queue_t wait;
1127
 
1128
        if(!wait_data){
1129
                return -ENOMEM;
1130
        }
1131
        /*
1132
         * The spin locking is needed to keep anyone from playing
1133
         * with the queue pointers and id while we do the same
1134
         */
1135
        spin_lock_irqsave(&adpt_post_wait_lock, flags);
1136
       // TODO we need a MORE unique way of getting ids
1137
       // to support async LCT get
1138
        wait_data->next = adpt_post_wait_queue;
1139
        adpt_post_wait_queue = wait_data;
1140
        adpt_post_wait_id++;
1141
        adpt_post_wait_id = (adpt_post_wait_id & 0x7fff);
1142
        wait_data->id =  adpt_post_wait_id;
1143
        spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
1144
 
1145
        wait_data->wq = &adpt_wq_i2o_post;
1146
        wait_data->status = -ETIMEDOUT;
1147
 
1148
        // this code is taken from kernel/sched.c:interruptible_sleep_on_timeout
1149
        wait.task = current;
1150
        init_waitqueue_entry(&wait, current);
1151
        wq_write_lock_irqsave(&adpt_wq_i2o_post.lock,flags);
1152
        __add_wait_queue(&adpt_wq_i2o_post, &wait);
1153
        wq_write_unlock(&adpt_wq_i2o_post.lock);
1154
 
1155
        msg[2] |= 0x80000000 | ((u32)wait_data->id);
1156
        timeout *= HZ;
1157
        if((status = adpt_i2o_post_this(pHba, msg, len)) == 0){
1158
                if(!timeout){
1159
                        set_current_state(TASK_INTERRUPTIBLE);
1160
                        spin_unlock_irq(&io_request_lock);
1161
                        schedule();
1162
                        spin_lock_irq(&io_request_lock);
1163
                } else {
1164
                        set_current_state(TASK_INTERRUPTIBLE);
1165
                        spin_unlock_irq(&io_request_lock);
1166
                        schedule_timeout(timeout*HZ);
1167
                        spin_lock_irq(&io_request_lock);
1168
                }
1169
        }
1170
        wq_write_lock_irq(&adpt_wq_i2o_post.lock);
1171
        __remove_wait_queue(&adpt_wq_i2o_post, &wait);
1172
        wq_write_unlock_irqrestore(&adpt_wq_i2o_post.lock,flags);
1173
 
1174
        if(status == -ETIMEDOUT){
1175
                printk(KERN_INFO"dpti%d: POST WAIT TIMEOUT\n",pHba->unit);
1176
                // We will have to free the wait_data memory during shutdown
1177
                return status;
1178
        }
1179
 
1180
        /* Remove the entry from the queue.  */
1181
        p2 = NULL;
1182
        spin_lock_irqsave(&adpt_post_wait_lock, flags);
1183
        for(p1 = adpt_post_wait_queue; p1; p2 = p1, p1 = p1->next) {
1184
                if(p1 == wait_data) {
1185
                        if(p1->status == I2O_DETAIL_STATUS_UNSUPPORTED_FUNCTION ) {
1186
                                status = -EOPNOTSUPP;
1187
                        }
1188
                        if(p2) {
1189
                                p2->next = p1->next;
1190
                        } else {
1191
                                adpt_post_wait_queue = p1->next;
1192
                        }
1193
                        break;
1194
                }
1195
        }
1196
        spin_unlock_irqrestore(&adpt_post_wait_lock, flags);
1197
 
1198
        kfree(wait_data);
1199
 
1200
        return status;
1201
}
1202
 
1203
 
1204
static s32 adpt_i2o_post_this(adpt_hba* pHba, u32* data, int len)
1205
{
1206
 
1207
        u32 m = EMPTY_QUEUE;
1208
        u32 *msg;
1209
        ulong timeout = jiffies + 30*HZ;
1210
        do {
1211
                rmb();
1212
                m = readl(pHba->post_port);
1213
                if (m != EMPTY_QUEUE) {
1214
                        break;
1215
                }
1216
                if(time_after(jiffies,timeout)){
1217
                        printk(KERN_WARNING"dpti%d: Timeout waiting for message frame!\n", pHba->unit);
1218
                        return -ETIMEDOUT;
1219
                }
1220
        } while(m == EMPTY_QUEUE);
1221
 
1222
        msg = (u32*) (pHba->msg_addr_virt + m);
1223
        memcpy_toio(msg, data, len);
1224
        wmb();
1225
 
1226
        //post message
1227
        writel(m, pHba->post_port);
1228
        wmb();
1229
 
1230
        return 0;
1231
}
1232
 
1233
 
1234
static void adpt_i2o_post_wait_complete(u32 context, int status)
1235
{
1236
        struct adpt_i2o_post_wait_data *p1 = NULL;
1237
        /*
1238
         * We need to search through the adpt_post_wait
1239
         * queue to see if the given message is still
1240
         * outstanding.  If not, it means that the IOP
1241
         * took longer to respond to the message than we
1242
         * had allowed and timer has already expired.
1243
         * Not much we can do about that except log
1244
         * it for debug purposes, increase timeout, and recompile
1245
         *
1246
         * Lock needed to keep anyone from moving queue pointers
1247
         * around while we're looking through them.
1248
         */
1249
 
1250
        context &= 0x7fff;
1251
 
1252
        spin_lock(&adpt_post_wait_lock);
1253
        for(p1 = adpt_post_wait_queue; p1; p1 = p1->next) {
1254
                if(p1->id == context) {
1255
                        p1->status = status;
1256
                        spin_unlock(&adpt_post_wait_lock);
1257
                        wake_up_interruptible(p1->wq);
1258
                        return;
1259
                }
1260
        }
1261
        spin_unlock(&adpt_post_wait_lock);
1262
        // If this happens we loose commands that probably really completed
1263
        printk(KERN_DEBUG"dpti: Could Not find task %d in wait queue\n",context);
1264
        printk(KERN_DEBUG"      Tasks in wait queue:\n");
1265
        for(p1 = adpt_post_wait_queue; p1; p1 = p1->next) {
1266
                printk(KERN_DEBUG"           %d\n",p1->id);
1267
        }
1268
        return;
1269
}
1270
 
1271
static s32 adpt_i2o_reset_hba(adpt_hba* pHba)
1272
{
1273
        u32 msg[8];
1274
        u8* status;
1275
        u32 m = EMPTY_QUEUE ;
1276
        ulong timeout = jiffies + (TMOUT_IOPRESET*HZ);
1277
 
1278
        if(pHba->initialized  == FALSE) {       // First time reset should be quick
1279
                timeout = jiffies + (25*HZ);
1280
        } else {
1281
                adpt_i2o_quiesce_hba(pHba);
1282
        }
1283
 
1284
        do {
1285
                rmb();
1286
                m = readl(pHba->post_port);
1287
                if (m != EMPTY_QUEUE) {
1288
                        break;
1289
                }
1290
                if(time_after(jiffies,timeout)){
1291
                        printk(KERN_WARNING"Timeout waiting for message!\n");
1292
                        return -ETIMEDOUT;
1293
                }
1294
        } while (m == EMPTY_QUEUE);
1295
 
1296
        status = (u8*)kmalloc(4, GFP_KERNEL|ADDR32);
1297
        if(status == NULL) {
1298
                adpt_send_nop(pHba, m);
1299
                printk(KERN_ERR"IOP reset failed - no free memory.\n");
1300
                return -ENOMEM;
1301
        }
1302
        memset(status,0,4);
1303
 
1304
        msg[0]=EIGHT_WORD_MSG_SIZE|SGL_OFFSET_0;
1305
        msg[1]=I2O_CMD_ADAPTER_RESET<<24|HOST_TID<<12|ADAPTER_TID;
1306
        msg[2]=0;
1307
        msg[3]=0;
1308
        msg[4]=0;
1309
        msg[5]=0;
1310
        msg[6]=virt_to_bus(status);
1311
        msg[7]=0;
1312
 
1313
        memcpy_toio(pHba->msg_addr_virt+m, msg, sizeof(msg));
1314
        wmb();
1315
        writel(m, pHba->post_port);
1316
        wmb();
1317
 
1318
        while(*status == 0){
1319
                if(time_after(jiffies,timeout)){
1320
                        printk(KERN_WARNING"%s: IOP Reset Timeout\n",pHba->name);
1321
                        /* We loose 4 bytes of "status" here, but we cannot
1322
                           free these because controller may awake and corrupt
1323
                           those bytes at any time */
1324
                        return -ETIMEDOUT;
1325
                }
1326
                rmb();
1327
        }
1328
 
1329
        if(*status == 0x01 /*I2O_EXEC_IOP_RESET_IN_PROGRESS*/) {
1330
                PDEBUG("%s: Reset in progress...\n", pHba->name);
1331
                // Here we wait for message frame to become available
1332
                // indicated that reset has finished
1333
                do {
1334
                        rmb();
1335
                        m = readl(pHba->post_port);
1336
                        if (m != EMPTY_QUEUE) {
1337
                                break;
1338
                        }
1339
                        if(time_after(jiffies,timeout)){
1340
                                printk(KERN_ERR "%s:Timeout waiting for IOP Reset.\n",pHba->name);
1341
                        /* We loose 4 bytes of "status" here, but we cannot
1342
                           free these because controller may awake and corrupt
1343
                           those bytes at any time */
1344
                                return -ETIMEDOUT;
1345
                        }
1346
                } while (m == EMPTY_QUEUE);
1347
                // Flush the offset
1348
                adpt_send_nop(pHba, m);
1349
        }
1350
        adpt_i2o_status_get(pHba);
1351
        if(*status == 0x02 ||
1352
                        pHba->status_block->iop_state != ADAPTER_STATE_RESET) {
1353
                printk(KERN_WARNING"%s: Reset reject, trying to clear\n",
1354
                                pHba->name);
1355
        } else {
1356
                PDEBUG("%s: Reset completed.\n", pHba->name);
1357
        }
1358
 
1359
        kfree(status);
1360
#ifdef UARTDELAY
1361
        // This delay is to allow someone attached to the card through the debug UART to 
1362
        // set up the dump levels that they want before the rest of the initialization sequence
1363
        adpt_delay(20000);
1364
#endif
1365
        return 0;
1366
}
1367
 
1368
 
1369
static int adpt_i2o_parse_lct(adpt_hba* pHba)
1370
{
1371
        int i;
1372
        int max;
1373
        int tid;
1374
        struct i2o_device *d;
1375
        i2o_lct *lct = pHba->lct;
1376
        u8 bus_no = 0;
1377
        s16 scsi_id;
1378
        s16 scsi_lun;
1379
        u32 buf[10]; // larger than 7, or 8 ...
1380
        struct adpt_device* pDev;
1381
 
1382
        if (lct == NULL) {
1383
                printk(KERN_ERR "%s: LCT is empty???\n",pHba->name);
1384
                return -1;
1385
        }
1386
 
1387
        max = lct->table_size;
1388
        max -= 3;
1389
        max /= 9;
1390
 
1391
        for(i=0;i<max;i++) {
1392
                if( lct->lct_entry[i].user_tid != 0xfff){
1393
                        /*
1394
                         * If we have hidden devices, we need to inform the upper layers about
1395
                         * the possible maximum id reference to handle device access when
1396
                         * an array is disassembled. This code has no other purpose but to
1397
                         * allow us future access to devices that are currently hidden
1398
                         * behind arrays, hotspares or have not been configured (JBOD mode).
1399
                         */
1400
                        if( lct->lct_entry[i].class_id != I2O_CLASS_RANDOM_BLOCK_STORAGE &&
1401
                            lct->lct_entry[i].class_id != I2O_CLASS_SCSI_PERIPHERAL &&
1402
                            lct->lct_entry[i].class_id != I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
1403
                                continue;
1404
                        }
1405
                        tid = lct->lct_entry[i].tid;
1406
                        // I2O_DPT_DEVICE_INFO_GROUP_NO;
1407
                        if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)<0) {
1408
                                continue;
1409
                        }
1410
                        bus_no = buf[0]>>16;
1411
                        scsi_id = buf[1];
1412
                        scsi_lun = (buf[2]>>8 )&0xff;
1413
                        if(bus_no >= MAX_CHANNEL) {     // Something wrong skip it
1414
                                printk(KERN_WARNING"%s: Channel number %d out of range \n", pHba->name, bus_no);
1415
                                continue;
1416
                        }
1417
                        if(scsi_id > MAX_ID){
1418
                                printk(KERN_WARNING"%s: SCSI ID %d out of range \n", pHba->name, bus_no);
1419
                                continue;
1420
                        }
1421
                        if(bus_no > pHba->top_scsi_channel){
1422
                                pHba->top_scsi_channel = bus_no;
1423
                        }
1424
                        if(scsi_id > pHba->top_scsi_id){
1425
                                pHba->top_scsi_id = scsi_id;
1426
                        }
1427
                        if(scsi_lun > pHba->top_scsi_lun){
1428
                                pHba->top_scsi_lun = scsi_lun;
1429
                        }
1430
                        continue;
1431
                }
1432
                d = (struct i2o_device *)kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
1433
                if(d==NULL)
1434
                {
1435
                        printk(KERN_CRIT"%s: Out of memory for I2O device data.\n",pHba->name);
1436
                        return -ENOMEM;
1437
                }
1438
 
1439
                d->controller = (void*)pHba;
1440
                d->next = NULL;
1441
 
1442
                memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
1443
 
1444
                d->flags = 0;
1445
                tid = d->lct_data.tid;
1446
                adpt_i2o_report_hba_unit(pHba, d);
1447
                adpt_i2o_install_device(pHba, d);
1448
        }
1449
        bus_no = 0;
1450
        for(d = pHba->devices; d ; d = d->next) {
1451
                if(d->lct_data.class_id  == I2O_CLASS_BUS_ADAPTER_PORT ||
1452
                   d->lct_data.class_id  == I2O_CLASS_FIBRE_CHANNEL_PORT){
1453
                        tid = d->lct_data.tid;
1454
                        // TODO get the bus_no from hrt-but for now they are in order
1455
                        //bus_no = 
1456
                        if(bus_no > pHba->top_scsi_channel){
1457
                                pHba->top_scsi_channel = bus_no;
1458
                        }
1459
                        pHba->channel[bus_no].type = d->lct_data.class_id;
1460
                        pHba->channel[bus_no].tid = tid;
1461
                        if(adpt_i2o_query_scalar(pHba, tid, 0x0200, -1, buf, 28)>=0)
1462
                        {
1463
                                pHba->channel[bus_no].scsi_id = buf[1];
1464
                                PDEBUG("Bus %d - SCSI ID %d.\n", bus_no, buf[1]);
1465
                        }
1466
                        // TODO remove - this is just until we get from hrt
1467
                        bus_no++;
1468
                        if(bus_no >= MAX_CHANNEL) {     // Something wrong skip it
1469
                                printk(KERN_WARNING"%s: Channel number %d out of range - LCT\n", pHba->name, bus_no);
1470
                                break;
1471
                        }
1472
                }
1473
        }
1474
 
1475
        // Setup adpt_device table
1476
        for(d = pHba->devices; d ; d = d->next) {
1477
                if(d->lct_data.class_id  == I2O_CLASS_RANDOM_BLOCK_STORAGE ||
1478
                   d->lct_data.class_id  == I2O_CLASS_SCSI_PERIPHERAL ||
1479
                   d->lct_data.class_id  == I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
1480
 
1481
                        tid = d->lct_data.tid;
1482
                        scsi_id = -1;
1483
                        // I2O_DPT_DEVICE_INFO_GROUP_NO;
1484
                        if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)>=0) {
1485
                                bus_no = buf[0]>>16;
1486
                                scsi_id = buf[1];
1487
                                scsi_lun = (buf[2]>>8 )&0xff;
1488
                                if(bus_no >= MAX_CHANNEL) {     // Something wrong skip it
1489
                                        continue;
1490
                                }
1491
                                if(scsi_id > MAX_ID){
1492
                                        continue;
1493
                                }
1494
                                if( pHba->channel[bus_no].device[scsi_id] == NULL){
1495
                                        pDev =  kmalloc(sizeof(struct adpt_device),GFP_KERNEL);
1496
                                        if(pDev == NULL) {
1497
                                                return -ENOMEM;
1498
                                        }
1499
                                        pHba->channel[bus_no].device[scsi_id] = pDev;
1500
                                        memset(pDev,0,sizeof(struct adpt_device));
1501
                                } else {
1502
                                        for( pDev = pHba->channel[bus_no].device[scsi_id];
1503
                                                        pDev->next_lun; pDev = pDev->next_lun){
1504
                                        }
1505
                                        pDev->next_lun = kmalloc(sizeof(struct adpt_device),GFP_KERNEL);
1506
                                        if(pDev->next_lun == NULL) {
1507
                                                return -ENOMEM;
1508
                                        }
1509
                                        memset(pDev->next_lun,0,sizeof(struct adpt_device));
1510
                                        pDev = pDev->next_lun;
1511
                                }
1512
                                pDev->tid = tid;
1513
                                pDev->scsi_channel = bus_no;
1514
                                pDev->scsi_id = scsi_id;
1515
                                pDev->scsi_lun = scsi_lun;
1516
                                pDev->pI2o_dev = d;
1517
                                d->owner = pDev;
1518
                                pDev->type = (buf[0])&0xff;
1519
                                pDev->flags = (buf[0]>>8)&0xff;
1520
                                if(scsi_id > pHba->top_scsi_id){
1521
                                        pHba->top_scsi_id = scsi_id;
1522
                                }
1523
                                if(scsi_lun > pHba->top_scsi_lun){
1524
                                        pHba->top_scsi_lun = scsi_lun;
1525
                                }
1526
                        }
1527
                        if(scsi_id == -1){
1528
                                printk(KERN_WARNING"Could not find SCSI ID for %s\n",
1529
                                                d->lct_data.identity_tag);
1530
                        }
1531
                }
1532
        }
1533
        return 0;
1534
}
1535
 
1536
 
1537
/*
1538
 *      Each I2O controller has a chain of devices on it - these match
1539
 *      the useful parts of the LCT of the board.
1540
 */
1541
 
1542
static int adpt_i2o_install_device(adpt_hba* pHba, struct i2o_device *d)
1543
{
1544
        down(&adpt_configuration_lock);
1545
        d->controller=pHba;
1546
        d->owner=NULL;
1547
        d->next=pHba->devices;
1548
        d->prev=NULL;
1549
        if (pHba->devices != NULL){
1550
                pHba->devices->prev=d;
1551
        }
1552
        pHba->devices=d;
1553
        *d->dev_name = 0;
1554
 
1555
        up(&adpt_configuration_lock);
1556
        return 0;
1557
}
1558
 
1559
static int adpt_open(struct inode *inode, struct file *file)
1560
{
1561
        int minor;
1562
        adpt_hba* pHba;
1563
 
1564
        //TODO check for root access
1565
        //
1566
        minor = MINOR(inode->i_rdev);
1567
        if (minor >= hba_count) {
1568
                return -ENXIO;
1569
        }
1570
        down(&adpt_configuration_lock);
1571
        for (pHba = hba_chain; pHba; pHba = pHba->next) {
1572
                if (pHba->unit == minor) {
1573
                        break;  /* found adapter */
1574
                }
1575
        }
1576
        if (pHba == NULL) {
1577
                up(&adpt_configuration_lock);
1578
                return -ENXIO;
1579
        }
1580
 
1581
//      if(pHba->in_use){
1582
        //      up(&adpt_configuration_lock);
1583
//              return -EBUSY;
1584
//      }
1585
 
1586
        pHba->in_use = 1;
1587
        up(&adpt_configuration_lock);
1588
 
1589
        return 0;
1590
}
1591
 
1592
static int adpt_close(struct inode *inode, struct file *file)
1593
{
1594
        int minor;
1595
        adpt_hba* pHba;
1596
 
1597
        minor = MINOR(inode->i_rdev);
1598
        if (minor >= hba_count) {
1599
                return -ENXIO;
1600
        }
1601
        down(&adpt_configuration_lock);
1602
        for (pHba = hba_chain; pHba; pHba = pHba->next) {
1603
                if (pHba->unit == minor) {
1604
                        break;  /* found adapter */
1605
                }
1606
        }
1607
        up(&adpt_configuration_lock);
1608
        if (pHba == NULL) {
1609
                return -ENXIO;
1610
        }
1611
 
1612
        pHba->in_use = 0;
1613
 
1614
        return 0;
1615
}
1616
 
1617
 
1618
static int adpt_i2o_passthru(adpt_hba* pHba, u32* arg)
1619
{
1620
        u32 msg[MAX_MESSAGE_SIZE];
1621
        u32* reply = NULL;
1622
        u32 size = 0;
1623
        u32 reply_size = 0;
1624
        u32* user_msg = (u32*)arg;
1625
        u32* user_reply = NULL;
1626
        ulong sg_list[pHba->sg_tablesize];
1627
        u32 sg_offset = 0;
1628
        u32 sg_count = 0;
1629
        int sg_index = 0;
1630
        u32 i = 0;
1631
        u32 rcode = 0;
1632
        ulong p = 0;
1633
        ulong flags = 0;
1634
 
1635
        memset(&msg, 0, MAX_MESSAGE_SIZE*4);
1636
        // get user msg size in u32s 
1637
        if(get_user(size, &user_msg[0])){
1638
                return -EFAULT;
1639
        }
1640
        size = size>>16;
1641
 
1642
        user_reply = &user_msg[size];
1643
        if(size > MAX_MESSAGE_SIZE){
1644
                return -EFAULT;
1645
        }
1646
        size *= 4; // Convert to bytes
1647
 
1648
        /* Copy in the user's I2O command */
1649
        if(copy_from_user((void*)msg, (void*)user_msg, size)) {
1650
                return -EFAULT;
1651
        }
1652
        get_user(reply_size, &user_reply[0]);
1653
        reply_size = reply_size>>16;
1654
        if(reply_size > REPLY_FRAME_SIZE){
1655
                reply_size = REPLY_FRAME_SIZE;
1656
        }
1657
        reply_size *= 4;
1658
        reply = kmalloc(REPLY_FRAME_SIZE*4, GFP_KERNEL);
1659
        if(reply == NULL) {
1660
                printk(KERN_WARNING"%s: Could not allocate reply buffer\n",pHba->name);
1661
                return -ENOMEM;
1662
        }
1663
        memset(reply,0,REPLY_FRAME_SIZE*4);
1664
        sg_offset = (msg[0]>>4)&0xf;
1665
        msg[2] = 0x40000000; // IOCTL context
1666
        msg[3] = (u32)reply;
1667
        memset(sg_list,0, sizeof(sg_list[0])*pHba->sg_tablesize);
1668
        if(sg_offset) {
1669
                // TODO 64bit fix
1670
                struct sg_simple_element *sg =  (struct sg_simple_element*) (msg+sg_offset);
1671
                sg_count = (size - sg_offset*4) / sizeof(struct sg_simple_element);
1672
                if (sg_count > pHba->sg_tablesize){
1673
                        printk(KERN_DEBUG"%s:IOCTL SG List too large (%u)\n", pHba->name,sg_count);
1674
                        kfree (reply);
1675
                        return -EINVAL;
1676
                }
1677
 
1678
                for(i = 0; i < sg_count; i++) {
1679
                        int sg_size;
1680
 
1681
                        if (!(sg[i].flag_count & 0x10000000 /*I2O_SGL_FLAGS_SIMPLE_ADDRESS_ELEMENT*/)) {
1682
                                printk(KERN_DEBUG"%s:Bad SG element %d - not simple (%x)\n",pHba->name,i,  sg[i].flag_count);
1683
                                rcode = -EINVAL;
1684
                                goto cleanup;
1685
                        }
1686
                        sg_size = sg[i].flag_count & 0xffffff;
1687
                        /* Allocate memory for the transfer */
1688
                        p = (ulong)kmalloc(sg_size, GFP_KERNEL|ADDR32);
1689
                        if(p == 0) {
1690
                                printk(KERN_DEBUG"%s: Could not allocate SG buffer - size = %d buffer number %d of %d\n",
1691
                                                pHba->name,sg_size,i,sg_count);
1692
                                rcode = -ENOMEM;
1693
                                goto cleanup;
1694
                        }
1695
                        sg_list[sg_index++] = p; // sglist indexed with input frame, not our internal frame.
1696
                        /* Copy in the user's SG buffer if necessary */
1697
                        if(sg[i].flag_count & 0x04000000 /*I2O_SGL_FLAGS_DIR*/) {
1698
                                // TODO 64bit fix
1699
                                if (copy_from_user((void*)p,(void*)sg[i].addr_bus, sg_size)) {
1700
                                        printk(KERN_DEBUG"%s: Could not copy SG buf %d FROM user\n",pHba->name,i);
1701
                                        rcode = -EFAULT;
1702
                                        goto cleanup;
1703
                                }
1704
                        }
1705
                        //TODO 64bit fix
1706
                        sg[i].addr_bus = (u32)virt_to_bus((void*)p);
1707
                }
1708
        }
1709
 
1710
        do {
1711
                spin_lock_irqsave(&io_request_lock, flags);
1712
                // This state stops any new commands from enterring the
1713
                // controller while processing the ioctl
1714
//              pHba->state |= DPTI_STATE_IOCTL;
1715
//              We can't set this now - The scsi subsystem sets host_blocked and
1716
//              the queue empties and stops.  We need a way to restart the queue
1717
                rcode = adpt_i2o_post_wait(pHba, msg, size, FOREVER);
1718
//              pHba->state &= ~DPTI_STATE_IOCTL;
1719
                spin_unlock_irqrestore(&io_request_lock, flags);
1720
        } while(rcode == -ETIMEDOUT);
1721
 
1722
        if(rcode){
1723
                goto cleanup;
1724
        }
1725
 
1726
        if(sg_offset) {
1727
        /* Copy back the Scatter Gather buffers back to user space */
1728
                u32 j;
1729
                // TODO 64bit fix
1730
                struct sg_simple_element* sg;
1731
                int sg_size;
1732
 
1733
                // re-acquire the original message to handle correctly the sg copy operation
1734
                memset(&msg, 0, MAX_MESSAGE_SIZE*4);
1735
                // get user msg size in u32s 
1736
                if(get_user(size, &user_msg[0])){
1737
                        rcode = -EFAULT;
1738
                        goto cleanup;
1739
                }
1740
                size = size>>16;
1741
                size *= 4;
1742
                /* Copy in the user's I2O command */
1743
                if (copy_from_user ((void*)msg, (void*)user_msg, size)) {
1744
                        rcode = -EFAULT;
1745
                        goto cleanup;
1746
                }
1747
                sg_count = (size - sg_offset*4) / sizeof(struct sg_simple_element);
1748
 
1749
                // TODO 64bit fix
1750
                sg       = (struct sg_simple_element*)(msg + sg_offset);
1751
                for (j = 0; j < sg_count; j++) {
1752
                        /* Copy out the SG list to user's buffer if necessary */
1753
                        if(! (sg[j].flag_count & 0x4000000 /*I2O_SGL_FLAGS_DIR*/)) {
1754
                                sg_size = sg[j].flag_count & 0xffffff;
1755
                                // TODO 64bit fix
1756
                                if (copy_to_user((void*)sg[j].addr_bus,(void*)sg_list[j], sg_size)) {
1757
                                        printk(KERN_WARNING"%s: Could not copy %lx TO user %x\n",pHba->name, sg_list[j], sg[j].addr_bus);
1758
                                        rcode = -EFAULT;
1759
                                        goto cleanup;
1760
                                }
1761
                        }
1762
                }
1763
        }
1764
 
1765
        /* Copy back the reply to user space */
1766
        if (reply_size) {
1767
                // we wrote our own values for context - now restore the user supplied ones
1768
                if(copy_from_user(reply+2, user_msg+2, sizeof(u32)*2)) {
1769
                        printk(KERN_WARNING"%s: Could not copy message context FROM user\n",pHba->name);
1770
                        rcode = -EFAULT;
1771
                }
1772
                if(copy_to_user(user_reply, reply, reply_size)) {
1773
                        printk(KERN_WARNING"%s: Could not copy reply TO user\n",pHba->name);
1774
                        rcode = -EFAULT;
1775
                }
1776
        }
1777
 
1778
 
1779
cleanup:
1780
        kfree (reply);
1781
        while(sg_index) {
1782
                if(sg_list[--sg_index]) {
1783
                        kfree((void*)(sg_list[sg_index]));
1784
                }
1785
        }
1786
        return rcode;
1787
}
1788
 
1789
 
1790
/*
1791
 * This routine returns information about the system.  This does not effect
1792
 * any logic and if the info is wrong - it doesn't matter.
1793
 */
1794
 
1795
/* Get all the info we can not get from kernel services */
1796
static int adpt_system_info(void *buffer)
1797
{
1798
        sysInfo_S si;
1799
 
1800
        memset(&si, 0, sizeof(si));
1801
 
1802
        si.osType = OS_LINUX;
1803
        si.osMajorVersion = (u8) (LINUX_VERSION_CODE >> 16);
1804
        si.osMinorVersion = (u8) (LINUX_VERSION_CODE >> 8 & 0x0ff);
1805
        si.osRevision =     (u8) (LINUX_VERSION_CODE & 0x0ff);
1806
        si.busType = SI_PCI_BUS;
1807
        si.processorFamily = DPTI_sig.dsProcessorFamily;
1808
 
1809
#if defined __i386__ 
1810
        adpt_i386_info(&si);
1811
#elif defined (__ia64__)
1812
        adpt_ia64_info(&si);
1813
#elif defined(__sparc__)
1814
        adpt_sparc_info(&si);
1815
#elif defined (__alpha__)
1816
        adpt_alpha_info(&si);
1817
#else
1818
        si.processorType = 0xff ;
1819
#endif
1820
        if(copy_to_user(buffer, &si, sizeof(si))){
1821
                printk(KERN_WARNING"dpti: Could not copy buffer TO user\n");
1822
                return -EFAULT;
1823
        }
1824
 
1825
        return 0;
1826
}
1827
 
1828
#if defined __ia64__ 
1829
static void adpt_ia64_info(sysInfo_S* si)
1830
{
1831
        // This is all the info we need for now
1832
        // We will add more info as our new
1833
        // managmenent utility requires it
1834
        si->processorType = PROC_IA64;
1835
}
1836
#endif
1837
 
1838
 
1839
#if defined __sparc__ 
1840
static void adpt_sparc_info(sysInfo_S* si)
1841
{
1842
        // This is all the info we need for now
1843
        // We will add more info as our new
1844
        // managmenent utility requires it
1845
        si->processorType = PROC_ULTRASPARC;
1846
}
1847
#endif
1848
 
1849
#if defined __alpha__ 
1850
static void adpt_alpha_info(sysInfo_S* si)
1851
{
1852
        // This is all the info we need for now
1853
        // We will add more info as our new
1854
        // managmenent utility requires it
1855
        si->processorType = PROC_ALPHA;
1856
}
1857
#endif
1858
 
1859
#if defined __i386__
1860
 
1861
static void adpt_i386_info(sysInfo_S* si)
1862
{
1863
        // This is all the info we need for now
1864
        // We will add more info as our new
1865
        // managmenent utility requires it
1866
        switch (boot_cpu_data.x86) {
1867
        case CPU_386:
1868
                si->processorType = PROC_386;
1869
                break;
1870
        case CPU_486:
1871
                si->processorType = PROC_486;
1872
                break;
1873
        case CPU_586:
1874
                si->processorType = PROC_PENTIUM;
1875
                break;
1876
        default:  // Just in case 
1877
                si->processorType = PROC_PENTIUM;
1878
                break;
1879
        }
1880
}
1881
 
1882
#endif
1883
 
1884
 
1885
static int adpt_ioctl(struct inode *inode, struct file *file, uint cmd,
1886
              ulong arg)
1887
{
1888
        int minor;
1889
        int error = 0;
1890
        adpt_hba* pHba;
1891
        ulong flags;
1892
 
1893
        minor = MINOR(inode->i_rdev);
1894
        if (minor >= DPTI_MAX_HBA){
1895
                return -ENXIO;
1896
        }
1897
        down(&adpt_configuration_lock);
1898
        for (pHba = hba_chain; pHba; pHba = pHba->next) {
1899
                if (pHba->unit == minor) {
1900
                        break;  /* found adapter */
1901
                }
1902
        }
1903
        up(&adpt_configuration_lock);
1904
        if(pHba == NULL){
1905
                return -ENXIO;
1906
        }
1907
 
1908
        while((volatile u32) pHba->state & DPTI_STATE_RESET ) {
1909
                set_task_state(current,TASK_UNINTERRUPTIBLE);
1910
                schedule_timeout(2);
1911
 
1912
        }
1913
 
1914
        switch (cmd) {
1915
        // TODO: handle 3 cases
1916
        case DPT_SIGNATURE:
1917
                if (copy_to_user((char*)arg, &DPTI_sig, sizeof(DPTI_sig))) {
1918
                        return -EFAULT;
1919
                }
1920
                break;
1921
        case I2OUSRCMD:
1922
                return  adpt_i2o_passthru(pHba,(u32*)arg);
1923
                break;
1924
 
1925
        case DPT_CTRLINFO:{
1926
                drvrHBAinfo_S HbaInfo;
1927
 
1928
#define FLG_OSD_PCI_VALID 0x0001
1929
#define FLG_OSD_DMA       0x0002
1930
#define FLG_OSD_I2O       0x0004
1931
                memset(&HbaInfo, 0, sizeof(HbaInfo));
1932
                HbaInfo.drvrHBAnum = pHba->unit;
1933
                HbaInfo.baseAddr = (ulong) pHba->base_addr_phys;
1934
                HbaInfo.blinkState = adpt_read_blink_led(pHba);
1935
                HbaInfo.pciBusNum =  pHba->pDev->bus->number;
1936
                HbaInfo.pciDeviceNum=PCI_SLOT(pHba->pDev->devfn);
1937
                HbaInfo.Interrupt = pHba->pDev->irq;
1938
                HbaInfo.hbaFlags = FLG_OSD_PCI_VALID | FLG_OSD_DMA | FLG_OSD_I2O;
1939
                if(copy_to_user((void *) arg, &HbaInfo, sizeof(HbaInfo))){
1940
                        printk(KERN_WARNING"%s: Could not copy HbaInfo TO user\n",pHba->name);
1941
                        return -EFAULT;
1942
                }
1943
                break;
1944
                }
1945
        case DPT_SYSINFO:
1946
                return adpt_system_info((void*)arg);
1947
                break;
1948
        case DPT_BLINKLED:{
1949
                u32 value;
1950
                value = (u32)adpt_read_blink_led(pHba);
1951
                if (copy_to_user((char*)arg, &value, sizeof(value))) {
1952
                        return -EFAULT;
1953
                }
1954
                break;
1955
                }
1956
        case I2ORESETCMD:
1957
                spin_lock_irqsave(&io_request_lock, flags);
1958
                adpt_hba_reset(pHba);
1959
                spin_unlock_irqrestore(&io_request_lock, flags);
1960
                break;
1961
        case I2ORESCANCMD:
1962
                adpt_rescan(pHba);
1963
                break;
1964
        case DPT_TARGET_BUSY & 0xFFFF:
1965
        case DPT_TARGET_BUSY:
1966
        {
1967
                TARGET_BUSY_T busy;
1968
                struct adpt_device* d;
1969
 
1970
                if (copy_from_user((void*)&busy, (void*)arg, sizeof(TARGET_BUSY_T))) {
1971
                        return -EFAULT;
1972
                }
1973
 
1974
                d = adpt_find_device(pHba, busy.channel, busy.id, busy.lun);
1975
                if(d == NULL){
1976
                        return -ENODEV;
1977
                }
1978
                busy.isBusy = ((d->pScsi_dev) && (0 != d->pScsi_dev->access_count)) ? 1 : 0;
1979
                if (copy_to_user ((char*)arg, &busy, sizeof(busy))) {
1980
                        return -EFAULT;
1981
                }
1982
                break;
1983
        }
1984
        default:
1985
                return -EINVAL;
1986
        }
1987
 
1988
        return error;
1989
}
1990
 
1991
 
1992
static void adpt_isr(int irq, void *dev_id, struct pt_regs *regs)
1993
{
1994
        Scsi_Cmnd* cmd;
1995
        adpt_hba* pHba=NULL;
1996
        u32 m;
1997
        ulong reply;
1998
        u32 status=0;
1999
        u32 context;
2000
        ulong flags = 0;
2001
 
2002
        pHba = dev_id;
2003
        if (pHba == NULL ){
2004
                printk(KERN_WARNING"adpt_isr: NULL dev_id\n");
2005
                return;
2006
        }
2007
        spin_lock_irqsave(&io_request_lock, flags);
2008
        while( readl(pHba->irq_mask) & I2O_INTERRUPT_PENDING_B) {
2009
                m = readl(pHba->reply_port);
2010
                if(m == EMPTY_QUEUE){
2011
                        // Try twice then give up
2012
                        rmb();
2013
                        m = readl(pHba->reply_port);
2014
                        if(m == EMPTY_QUEUE){
2015
                                // This really should not happen
2016
                                printk(KERN_ERR"dpti: Could not get reply frame\n");
2017
                                spin_unlock_irqrestore(&io_request_lock,flags);
2018
                                return;
2019
                        }
2020
                }
2021
                reply = (ulong)bus_to_virt(m);
2022
 
2023
                if (readl(reply) & MSG_FAIL) {
2024
                        u32 old_m = readl(reply+28);
2025
                        ulong msg;
2026
                        u32 old_context;
2027
                        PDEBUG("%s: Failed message\n",pHba->name);
2028
                        if(old_m >= 0x100000){
2029
                                printk(KERN_ERR"%s: Bad preserved MFA (%x)- dropping frame\n",pHba->name,old_m);
2030
                                writel(m,pHba->reply_port);
2031
                                continue;
2032
                        }
2033
                        // Transaction context is 0 in failed reply frame
2034
                        msg = (ulong)(pHba->msg_addr_virt + old_m);
2035
                        old_context = readl(msg+12);
2036
                        writel(old_context, reply+12);
2037
                        adpt_send_nop(pHba, old_m);
2038
                }
2039
                context = readl(reply+8);
2040
                if(context & 0x40000000){ // IOCTL
2041
                        ulong p = (ulong)(readl(reply+12));
2042
                        if( p != 0) {
2043
                                memcpy((void*)p, (void*)reply, REPLY_FRAME_SIZE * 4);
2044
                        }
2045
                        // All IOCTLs will also be post wait
2046
                }
2047
                if(context & 0x80000000){ // Post wait message
2048
                        status = readl(reply+16);
2049
                        if(status  >> 24){
2050
                                status &=  0xffff; /* Get detail status */
2051
                        } else {
2052
                                status = I2O_POST_WAIT_OK;
2053
                        }
2054
                        if(!(context & 0x40000000)) {
2055
                                cmd = (Scsi_Cmnd*) readl(reply+12);
2056
                                if(cmd != NULL) {
2057
                                        printk(KERN_WARNING"%s: Apparent SCSI cmd in Post Wait Context - cmd=%p context=%x\n", pHba->name, cmd, context);
2058
                                }
2059
                        }
2060
                        adpt_i2o_post_wait_complete(context, status);
2061
                } else { // SCSI message
2062
                        cmd = (Scsi_Cmnd*) readl(reply+12);
2063
                        if(cmd != NULL){
2064
                                if(cmd->serial_number != 0) { // If not timedout
2065
                                        adpt_i2o_to_scsi(reply, cmd);
2066
                                }
2067
                        }
2068
                }
2069
                writel(m, pHba->reply_port);
2070
                wmb();
2071
                rmb();
2072
        }
2073
        spin_unlock_irqrestore(&io_request_lock, flags);
2074
        return;
2075
 
2076
}
2077
 
2078
static s32 adpt_scsi_to_i2o(adpt_hba* pHba, Scsi_Cmnd* cmd, struct adpt_device* d)
2079
{
2080
        int i;
2081
        u32 msg[MAX_MESSAGE_SIZE];
2082
        u32* mptr;
2083
        u32 *lenptr;
2084
        int direction;
2085
        int scsidir;
2086
        u32 len;
2087
        u32 reqlen;
2088
        s32 rcode;
2089
 
2090
        memset(msg, 0 , sizeof(msg));
2091
        len = cmd->request_bufflen;
2092
        direction = 0x00000000;
2093
 
2094
        scsidir = 0x00000000;                   // DATA NO XFER
2095
        if(len) {
2096
                /*
2097
                 * Set SCBFlags to indicate if data is being transferred
2098
                 * in or out, or no data transfer
2099
                 * Note:  Do not have to verify index is less than 0 since
2100
                 * cmd->cmnd[0] is an unsigned char
2101
                 */
2102
                switch(cmd->sc_data_direction){
2103
                case SCSI_DATA_READ:
2104
                        scsidir  =0x40000000;   // DATA IN  (iop<--dev)
2105
                        break;
2106
                case SCSI_DATA_WRITE:
2107
                        direction=0x04000000;   // SGL OUT
2108
                        scsidir  =0x80000000;   // DATA OUT (iop-->dev)
2109
                        break;
2110
                case SCSI_DATA_NONE:
2111
                        break;
2112
                case SCSI_DATA_UNKNOWN:
2113
                        scsidir  =0x40000000;   // DATA IN  (iop<--dev)
2114
                        // Assume In - and continue;
2115
                        break;
2116
                default:
2117
                        printk(KERN_WARNING"%s: scsi opcode 0x%x not supported.\n",
2118
                             pHba->name, cmd->cmnd[0]);
2119
                        cmd->result = (DID_OK <<16) | (INITIATOR_ERROR << 8);
2120
                        cmd->scsi_done(cmd);
2121
                        return  0;
2122
                }
2123
        }
2124
        // msg[0] is set later
2125
        // I2O_CMD_SCSI_EXEC
2126
        msg[1] = ((0xff<<24)|(HOST_TID<<12)|d->tid);
2127
        msg[2] = 0;
2128
        msg[3] = (u32)cmd;      /* We want the SCSI control block back */
2129
        // Our cards use the transaction context as the tag for queueing
2130
        // Adaptec/DPT Private stuff 
2131
        msg[4] = I2O_CMD_SCSI_EXEC|(DPT_ORGANIZATION_ID<<16);
2132
        msg[5] = d->tid;
2133
        /* Direction, disconnect ok | sense data | simple queue , CDBLen */
2134
        // I2O_SCB_FLAG_ENABLE_DISCONNECT | 
2135
        // I2O_SCB_FLAG_SIMPLE_QUEUE_TAG | 
2136
        // I2O_SCB_FLAG_SENSE_DATA_IN_MESSAGE;
2137
        msg[6] = scsidir|0x20a00000|cmd->cmd_len;
2138
 
2139
        mptr=msg+7;
2140
 
2141
        // Write SCSI command into the message - always 16 byte block 
2142
        memset(mptr, 0,  16);
2143
        memcpy(mptr, cmd->cmnd, cmd->cmd_len);
2144
        mptr+=4;
2145
        lenptr=mptr++;          /* Remember me - fill in when we know */
2146
        reqlen = 14;            // SINGLE SGE
2147
        /* Now fill in the SGList and command */
2148
        if(cmd->use_sg) {
2149
                struct scatterlist *sg = (struct scatterlist *)cmd->request_buffer;
2150
                len = 0;
2151
                for(i = 0 ; i < cmd->use_sg; i++) {
2152
                        *mptr++ = direction|0x10000000|sg->length;
2153
                        len+=sg->length;
2154
                        *mptr++ = virt_to_bus(sg->address);
2155
                        sg++;
2156
                }
2157
                /* Make this an end of list */
2158
                mptr[-2] = direction|0xD0000000|(sg-1)->length;
2159
                reqlen = mptr - msg;
2160
                *lenptr = len;
2161
 
2162
                if(cmd->underflow && len != cmd->underflow){
2163
                        printk(KERN_WARNING"Cmd len %08X Cmd underflow %08X\n",
2164
                                len, cmd->underflow);
2165
                }
2166
        } else {
2167
                *lenptr = len = cmd->request_bufflen;
2168
                if(len == 0) {
2169
                        reqlen = 12;
2170
                } else {
2171
                        *mptr++ = 0xD0000000|direction|cmd->request_bufflen;
2172
                        *mptr++ = virt_to_bus(cmd->request_buffer);
2173
                }
2174
        }
2175
 
2176
        /* Stick the headers on */
2177
        msg[0] = reqlen<<16 | ((reqlen > 12) ? SGL_OFFSET_12 : SGL_OFFSET_0);
2178
 
2179
        // Send it on it's way
2180
        rcode = adpt_i2o_post_this(pHba, msg, reqlen<<2);
2181
        if (rcode == 0) {
2182
                return 0;
2183
        }
2184
        return rcode;
2185
}
2186
 
2187
 
2188
static s32 adpt_scsi_register(adpt_hba* pHba,Scsi_Host_Template * sht)
2189
{
2190
        struct Scsi_Host *host = NULL;
2191
 
2192
        host = scsi_register(sht, sizeof(adpt_hba*));
2193
        if (host == NULL) {
2194
                printk ("%s: scsi_register returned NULL\n",pHba->name);
2195
                return -1;
2196
        }
2197
        (adpt_hba*)(host->hostdata[0]) = pHba;
2198
        pHba->host = host;
2199
 
2200
        host->irq = pHba->pDev->irq;;
2201
        /* no IO ports, so don't have to set host->io_port and
2202
         * host->n_io_port
2203
         */
2204
        host->io_port = 0;
2205
        host->n_io_port = 0;
2206
                                /* see comments in hosts.h */
2207
        host->max_id = 16;
2208
        host->max_lun = 256;
2209
        host->max_channel = pHba->top_scsi_channel + 1;
2210
        host->cmd_per_lun = 256;
2211
        host->unique_id = (uint) pHba;
2212
        host->sg_tablesize = pHba->sg_tablesize;
2213
        host->can_queue = pHba->post_fifo_size;
2214
        host->select_queue_depths = adpt_select_queue_depths;
2215
 
2216
        return 0;
2217
}
2218
 
2219
 
2220
static s32 adpt_i2o_to_scsi(ulong reply, Scsi_Cmnd* cmd)
2221
{
2222
        adpt_hba* pHba;
2223
        u32 hba_status;
2224
        u32 dev_status;
2225
        u32 reply_flags = readl(reply) & 0xff00; // Leave it shifted up 8 bits 
2226
        // I know this would look cleaner if I just read bytes
2227
        // but the model I have been using for all the rest of the
2228
        // io is in 4 byte words - so I keep that model
2229
        u16 detailed_status = readl(reply+16) &0xffff;
2230
        dev_status = (detailed_status & 0xff);
2231
        hba_status = detailed_status >> 8;
2232
 
2233
        // calculate resid for sg 
2234
        cmd->resid = cmd->request_bufflen - readl(reply+5);
2235
 
2236
        pHba = (adpt_hba*) cmd->host->hostdata[0];
2237
 
2238
        cmd->sense_buffer[0] = '\0';  // initialize sense valid flag to false
2239
 
2240
        if(!(reply_flags & MSG_FAIL)) {
2241
                switch(detailed_status & I2O_SCSI_DSC_MASK) {
2242
                case I2O_SCSI_DSC_SUCCESS:
2243
                        cmd->result = (DID_OK << 16);
2244
                        // handle underflow
2245
                        if(readl(reply+5) < cmd->underflow ) {
2246
                                cmd->result = (DID_ERROR <<16);
2247
                                printk(KERN_WARNING"%s: SCSI CMD underflow\n",pHba->name);
2248
                        }
2249
                        break;
2250
                case I2O_SCSI_DSC_REQUEST_ABORTED:
2251
                        cmd->result = (DID_ABORT << 16);
2252
                        break;
2253
                case I2O_SCSI_DSC_PATH_INVALID:
2254
                case I2O_SCSI_DSC_DEVICE_NOT_PRESENT:
2255
                case I2O_SCSI_DSC_SELECTION_TIMEOUT:
2256
                case I2O_SCSI_DSC_COMMAND_TIMEOUT:
2257
                case I2O_SCSI_DSC_NO_ADAPTER:
2258
                case I2O_SCSI_DSC_RESOURCE_UNAVAILABLE:
2259
                        printk(KERN_WARNING"%s: SCSI Timeout-Device (%d,%d,%d) hba status=0x%x, dev status=0x%x, cmd=0x%x\n",
2260
                                pHba->name, (u32)cmd->channel, (u32)cmd->target, (u32)cmd->lun, hba_status, dev_status, cmd->cmnd[0]);
2261
                        cmd->result = (DID_TIME_OUT << 16);
2262
                        break;
2263
                case I2O_SCSI_DSC_ADAPTER_BUSY:
2264
                case I2O_SCSI_DSC_BUS_BUSY:
2265
                        cmd->result = (DID_BUS_BUSY << 16);
2266
                        break;
2267
                case I2O_SCSI_DSC_SCSI_BUS_RESET:
2268
                case I2O_SCSI_DSC_BDR_MESSAGE_SENT:
2269
                        cmd->result = (DID_RESET << 16);
2270
                        break;
2271
                case I2O_SCSI_DSC_PARITY_ERROR_FAILURE:
2272
                        printk(KERN_WARNING"%s: SCSI CMD parity error\n",pHba->name);
2273
                        cmd->result = (DID_PARITY << 16);
2274
                        break;
2275
                case I2O_SCSI_DSC_UNABLE_TO_ABORT:
2276
                case I2O_SCSI_DSC_COMPLETE_WITH_ERROR:
2277
                case I2O_SCSI_DSC_UNABLE_TO_TERMINATE:
2278
                case I2O_SCSI_DSC_MR_MESSAGE_RECEIVED:
2279
                case I2O_SCSI_DSC_AUTOSENSE_FAILED:
2280
                case I2O_SCSI_DSC_DATA_OVERRUN:
2281
                case I2O_SCSI_DSC_UNEXPECTED_BUS_FREE:
2282
                case I2O_SCSI_DSC_SEQUENCE_FAILURE:
2283
                case I2O_SCSI_DSC_REQUEST_LENGTH_ERROR:
2284
                case I2O_SCSI_DSC_PROVIDE_FAILURE:
2285
                case I2O_SCSI_DSC_REQUEST_TERMINATED:
2286
                case I2O_SCSI_DSC_IDE_MESSAGE_SENT:
2287
                case I2O_SCSI_DSC_UNACKNOWLEDGED_EVENT:
2288
                case I2O_SCSI_DSC_MESSAGE_RECEIVED:
2289
                case I2O_SCSI_DSC_INVALID_CDB:
2290
                case I2O_SCSI_DSC_LUN_INVALID:
2291
                case I2O_SCSI_DSC_SCSI_TID_INVALID:
2292
                case I2O_SCSI_DSC_FUNCTION_UNAVAILABLE:
2293
                case I2O_SCSI_DSC_NO_NEXUS:
2294
                case I2O_SCSI_DSC_CDB_RECEIVED:
2295
                case I2O_SCSI_DSC_LUN_ALREADY_ENABLED:
2296
                case I2O_SCSI_DSC_QUEUE_FROZEN:
2297
                case I2O_SCSI_DSC_REQUEST_INVALID:
2298
                default:
2299
                        printk(KERN_WARNING"%s: SCSI error %0x-Device(%d,%d,%d) hba_status=0x%x, dev_status=0x%x, cmd=0x%x\n",
2300
                                pHba->name, detailed_status & I2O_SCSI_DSC_MASK, (u32)cmd->channel, (u32)cmd->target, (u32)cmd-> lun,
2301
                               hba_status, dev_status, cmd->cmnd[0]);
2302
                        cmd->result = (DID_ERROR << 16);
2303
                        break;
2304
                }
2305
 
2306
                // copy over the request sense data if it was a check
2307
                // condition status
2308
                if(dev_status == 0x02 /*CHECK_CONDITION*/) {
2309
                        u32 len = sizeof(cmd->sense_buffer);
2310
                        len = (len > 40) ?  40 : len;
2311
                        // Copy over the sense data
2312
                        memcpy(cmd->sense_buffer, (void*)(reply+28) , len);
2313
                        if(cmd->sense_buffer[0] == 0x70 /* class 7 */ &&
2314
                           cmd->sense_buffer[2] == DATA_PROTECT ){
2315
                                /* This is to handle an array failed */
2316
                                cmd->result = (DID_TIME_OUT << 16);
2317
                                printk(KERN_WARNING"%s: SCSI Data Protect-Device (%d,%d,%d) hba_status=0x%x, dev_status=0x%x, cmd=0x%x\n",
2318
                                        pHba->name, (u32)cmd->channel, (u32)cmd->target, (u32)cmd->lun,
2319
                                        hba_status, dev_status, cmd->cmnd[0]);
2320
 
2321
                        }
2322
                }
2323
        } else {
2324
                /* In this condtion we could not talk to the tid
2325
                 * the card rejected it.  We should signal a retry
2326
                 * for a limitted number of retries.
2327
                 */
2328
                cmd->result = (DID_TIME_OUT << 16);
2329
                printk(KERN_WARNING"%s: I2O MSG_FAIL - Device (%d,%d,%d) tid=%d, cmd=0x%x\n",
2330
                        pHba->name, (u32)cmd->channel, (u32)cmd->target, (u32)cmd-> lun,
2331
                        ((struct adpt_device*)(cmd->device->hostdata))->tid, cmd->cmnd[0]);
2332
        }
2333
 
2334
        cmd->result |= (dev_status);
2335
 
2336
        if(cmd->scsi_done != NULL){
2337
                cmd->scsi_done(cmd);
2338
        }
2339
        return cmd->result;
2340
}
2341
 
2342
 
2343
static s32 adpt_rescan(adpt_hba* pHba)
2344
{
2345
        s32 rcode;
2346
        ulong flags;
2347
 
2348
        spin_lock_irqsave(&io_request_lock, flags);
2349
        if ((rcode=adpt_i2o_lct_get(pHba)) < 0){
2350
                spin_unlock_irqrestore(&io_request_lock, flags);
2351
                return rcode;
2352
        }
2353
 
2354
        if ((rcode=adpt_i2o_reparse_lct(pHba)) < 0){
2355
                spin_unlock_irqrestore(&io_request_lock, flags);
2356
                return rcode;
2357
        }
2358
        spin_unlock_irqrestore(&io_request_lock, flags);
2359
        return 0;
2360
}
2361
 
2362
 
2363
static s32 adpt_i2o_reparse_lct(adpt_hba* pHba)
2364
{
2365
        int i;
2366
        int max;
2367
        int tid;
2368
        struct i2o_device *d;
2369
        i2o_lct *lct = pHba->lct;
2370
        u8 bus_no = 0;
2371
        s16 scsi_id;
2372
        s16 scsi_lun;
2373
        u32 buf[10]; // at least 8 u32's
2374
        struct adpt_device* pDev = NULL;
2375
        struct i2o_device* pI2o_dev = NULL;
2376
 
2377
        if (lct == NULL) {
2378
                printk(KERN_ERR "%s: LCT is empty???\n",pHba->name);
2379
                return -1;
2380
        }
2381
 
2382
        max = lct->table_size;
2383
        max -= 3;
2384
        max /= 9;
2385
 
2386
        // Mark each drive as unscanned
2387
        for (d = pHba->devices; d; d = d->next) {
2388
                pDev =(struct adpt_device*) d->owner;
2389
                if(!pDev){
2390
                        continue;
2391
                }
2392
                pDev->state |= DPTI_DEV_UNSCANNED;
2393
        }
2394
 
2395
        printk(KERN_INFO "%s: LCT has %d entries.\n", pHba->name,max);
2396
 
2397
        for(i=0;i<max;i++) {
2398
                if( lct->lct_entry[i].user_tid != 0xfff){
2399
                        continue;
2400
                }
2401
 
2402
                if( lct->lct_entry[i].class_id == I2O_CLASS_RANDOM_BLOCK_STORAGE ||
2403
                    lct->lct_entry[i].class_id == I2O_CLASS_SCSI_PERIPHERAL ||
2404
                    lct->lct_entry[i].class_id == I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL ){
2405
                        tid = lct->lct_entry[i].tid;
2406
                        if(adpt_i2o_query_scalar(pHba, tid, 0x8000, -1, buf, 32)<0) {
2407
                                printk(KERN_ERR"%s: Could not query device\n",pHba->name);
2408
                                continue;
2409
                        }
2410
                        bus_no = buf[0]>>16;
2411
                        scsi_id = buf[1];
2412
                        scsi_lun = (buf[2]>>8 )&0xff;
2413
                        pDev = pHba->channel[bus_no].device[scsi_id];
2414
                        /* da lun */
2415
                        while(pDev) {
2416
                                if(pDev->scsi_lun == scsi_lun) {
2417
                                        break;
2418
                                }
2419
                                pDev = pDev->next_lun;
2420
                        }
2421
                        if(!pDev ) { // Something new add it
2422
                                d = (struct i2o_device *)kmalloc(sizeof(struct i2o_device), GFP_KERNEL);
2423
                                if(d==NULL)
2424
                                {
2425
                                        printk(KERN_CRIT "Out of memory for I2O device data.\n");
2426
                                        return -ENOMEM;
2427
                                }
2428
 
2429
                                d->controller = (void*)pHba;
2430
                                d->next = NULL;
2431
 
2432
                                memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
2433
 
2434
                                d->flags = 0;
2435
                                adpt_i2o_report_hba_unit(pHba, d);
2436
                                adpt_i2o_install_device(pHba, d);
2437
 
2438
                                if(bus_no >= MAX_CHANNEL) {     // Something wrong skip it
2439
                                        printk(KERN_WARNING"%s: Channel number %d out of range \n", pHba->name, bus_no);
2440
                                        continue;
2441
                                }
2442
                                pDev = pHba->channel[bus_no].device[scsi_id];
2443
                                if( pDev == NULL){
2444
                                        pDev =  kmalloc(sizeof(struct adpt_device),GFP_KERNEL);
2445
                                        if(pDev == NULL) {
2446
                                                return -ENOMEM;
2447
                                        }
2448
                                        pHba->channel[bus_no].device[scsi_id] = pDev;
2449
                                } else {
2450
                                        while (pDev->next_lun) {
2451
                                                pDev = pDev->next_lun;
2452
                                        }
2453
                                        pDev = pDev->next_lun = kmalloc(sizeof(struct adpt_device),GFP_KERNEL);
2454
                                        if(pDev == NULL) {
2455
                                                return -ENOMEM;
2456
                                        }
2457
                                }
2458
                                memset(pDev,0,sizeof(struct adpt_device));
2459
                                pDev->tid = d->lct_data.tid;
2460
                                pDev->scsi_channel = bus_no;
2461
                                pDev->scsi_id = scsi_id;
2462
                                pDev->scsi_lun = scsi_lun;
2463
                                pDev->pI2o_dev = d;
2464
                                d->owner = pDev;
2465
                                pDev->type = (buf[0])&0xff;
2466
                                pDev->flags = (buf[0]>>8)&0xff;
2467
                                // Too late, SCSI system has made up it's mind, but what the hey ...
2468
                                if(scsi_id > pHba->top_scsi_id){
2469
                                        pHba->top_scsi_id = scsi_id;
2470
                                }
2471
                                if(scsi_lun > pHba->top_scsi_lun){
2472
                                        pHba->top_scsi_lun = scsi_lun;
2473
                                }
2474
                                continue;
2475
                        } // end of new i2o device
2476
 
2477
                        // We found an old device - check it
2478
                        while(pDev) {
2479
                                if(pDev->scsi_lun == scsi_lun) {
2480
                                        if(pDev->pScsi_dev->online == FALSE) {
2481
                                                printk(KERN_WARNING"%s: Setting device (%d,%d,%d) back online\n",
2482
                                                                pHba->name,bus_no,scsi_id,scsi_lun);
2483
                                                if (pDev->pScsi_dev) {
2484
                                                        pDev->pScsi_dev->online = TRUE;
2485
                                                }
2486
                                        }
2487
                                        d = pDev->pI2o_dev;
2488
                                        if(d->lct_data.tid != tid) { // something changed
2489
                                                pDev->tid = tid;
2490
                                                memcpy(&d->lct_data, &lct->lct_entry[i], sizeof(i2o_lct_entry));
2491
                                                if (pDev->pScsi_dev) {
2492
                                                        pDev->pScsi_dev->changed = TRUE;
2493
                                                        pDev->pScsi_dev->removable = TRUE;
2494
                                                }
2495
                                        }
2496
                                        // Found it - mark it scanned
2497
                                        pDev->state = DPTI_DEV_ONLINE;
2498
                                        break;
2499
                                }
2500
                                pDev = pDev->next_lun;
2501
                        }
2502
                }
2503
        }
2504
        for (pI2o_dev = pHba->devices; pI2o_dev; pI2o_dev = pI2o_dev->next) {
2505
                pDev =(struct adpt_device*) pI2o_dev->owner;
2506
                if(!pDev){
2507
                        continue;
2508
                }
2509
                // Drive offline drives that previously existed but could not be found
2510
                // in the LCT table
2511
                if (pDev->state & DPTI_DEV_UNSCANNED){
2512
                        pDev->state = DPTI_DEV_OFFLINE;
2513
                        printk(KERN_WARNING"%s: Device (%d,%d,%d) offline\n",pHba->name,pDev->scsi_channel,pDev->scsi_id,pDev->scsi_lun);
2514
                        if (pDev->pScsi_dev) {
2515
                                pDev->pScsi_dev->online = FALSE;
2516
                                if (pDev->pScsi_dev->access_count) {
2517
                                        // A drive that was mounted is no longer there... bad!
2518
                                        SCSI_LOG_ERROR_RECOVERY(1, printk ("%s:Rescan: Previously "
2519
                                                                 "mounted drive not found!\n",pHba->name));
2520
                                        printk(KERN_WARNING"%s:Mounted drive taken offline\n",pHba->name);
2521
                                }
2522
                        }
2523
                }
2524
        }
2525
        return 0;
2526
}
2527
 
2528
static void adpt_fail_posted_scbs(adpt_hba* pHba)
2529
{
2530
        Scsi_Cmnd*      cmd = NULL;
2531
        Scsi_Device*    d = NULL;
2532
 
2533
        if( pHba->host->host_queue != NULL ) {
2534
                d = pHba->host->host_queue;
2535
                if(!d){
2536
                        return;
2537
                }
2538
                while( d->next != NULL ){
2539
                        for(cmd = d->device_queue; cmd ; cmd = cmd->next){
2540
                                if(cmd->serial_number == 0){
2541
                                        continue;
2542
                                }
2543
                                cmd->result = (DID_OK << 16) | (QUEUE_FULL <<1);
2544
                                cmd->scsi_done(cmd);
2545
                        }
2546
                        d = d->next;
2547
                }
2548
        }
2549
}
2550
 
2551
 
2552
/*============================================================================
2553
 *  Routines from i2o subsystem
2554
 *============================================================================
2555
 */
2556
 
2557
 
2558
 
2559
/*
2560
 *      Bring an I2O controller into HOLD state. See the spec.
2561
 */
2562
static int adpt_i2o_activate_hba(adpt_hba* pHba)
2563
{
2564
        int rcode;
2565
 
2566
        if(pHba->initialized ) {
2567
                if (adpt_i2o_status_get(pHba) < 0) {
2568
                        if((rcode = adpt_i2o_reset_hba(pHba)) != 0){
2569
                                printk(KERN_WARNING"%s: Could NOT reset.\n", pHba->name);
2570
                                return rcode;
2571
                        }
2572
                        if (adpt_i2o_status_get(pHba) < 0) {
2573
                                printk(KERN_INFO "HBA not responding.\n");
2574
                                return -1;
2575
                        }
2576
                }
2577
 
2578
                if(pHba->status_block->iop_state == ADAPTER_STATE_FAULTED) {
2579
                        printk(KERN_CRIT "%s: hardware fault\n", pHba->name);
2580
                        return -1;
2581
                }
2582
 
2583
                if (pHba->status_block->iop_state == ADAPTER_STATE_READY ||
2584
                    pHba->status_block->iop_state == ADAPTER_STATE_OPERATIONAL ||
2585
                    pHba->status_block->iop_state == ADAPTER_STATE_HOLD ||
2586
                    pHba->status_block->iop_state == ADAPTER_STATE_FAILED) {
2587
                        adpt_i2o_reset_hba(pHba);
2588
                        if (adpt_i2o_status_get(pHba) < 0 || pHba->status_block->iop_state != ADAPTER_STATE_RESET) {
2589
                                printk(KERN_ERR "%s: Failed to initialize.\n", pHba->name);
2590
                                return -1;
2591
                        }
2592
                }
2593
        } else {
2594
                if((rcode = adpt_i2o_reset_hba(pHba)) != 0){
2595
                        printk(KERN_WARNING"%s: Could NOT reset.\n", pHba->name);
2596
                        return rcode;
2597
                }
2598
 
2599
        }
2600
 
2601
        if (adpt_i2o_init_outbound_q(pHba) < 0) {
2602
                return -1;
2603
        }
2604
 
2605
        /* In HOLD state */
2606
 
2607
        if (adpt_i2o_hrt_get(pHba) < 0) {
2608
                return -1;
2609
        }
2610
 
2611
        return 0;
2612
}
2613
 
2614
/*
2615
 *      Bring a controller online into OPERATIONAL state.
2616
 */
2617
 
2618
static int adpt_i2o_online_hba(adpt_hba* pHba)
2619
{
2620
        if (adpt_i2o_systab_send(pHba) < 0) {
2621
                adpt_i2o_delete_hba(pHba);
2622
                return -1;
2623
        }
2624
        /* In READY state */
2625
 
2626
        if (adpt_i2o_enable_hba(pHba) < 0) {
2627
                adpt_i2o_delete_hba(pHba);
2628
                return -1;
2629
        }
2630
 
2631
        /* In OPERATIONAL state  */
2632
        return 0;
2633
}
2634
 
2635
static s32 adpt_send_nop(adpt_hba*pHba,u32 m)
2636
{
2637
        u32 *msg;
2638
        ulong timeout = jiffies + 5*HZ;
2639
 
2640
        while(m == EMPTY_QUEUE){
2641
                rmb();
2642
                m = readl(pHba->post_port);
2643
                if(m != EMPTY_QUEUE){
2644
                        break;
2645
                }
2646
                if(time_after(jiffies,timeout)){
2647
                        printk(KERN_ERR "%s: Timeout waiting for message frame!\n",pHba->name);
2648
                        return 2;
2649
                }
2650
        }
2651
        msg = (u32*)(pHba->msg_addr_virt + m);
2652
        writel( THREE_WORD_MSG_SIZE | SGL_OFFSET_0,&msg[0]);
2653
        writel( I2O_CMD_UTIL_NOP << 24 | HOST_TID << 12 | 0,&msg[1]);
2654
        writel( 0,&msg[2]);
2655
        wmb();
2656
 
2657
        writel(m, pHba->post_port);
2658
        wmb();
2659
        return 0;
2660
}
2661
 
2662
static s32 adpt_i2o_init_outbound_q(adpt_hba* pHba)
2663
{
2664
        u8 *status;
2665
        u32 *msg = NULL;
2666
        int i;
2667
        ulong timeout = jiffies + TMOUT_INITOUTBOUND*HZ;
2668
        u32* ptr;
2669
        u32 outbound_frame;  // This had to be a 32 bit address
2670
        u32 m;
2671
 
2672
        do {
2673
                rmb();
2674
                m = readl(pHba->post_port);
2675
                if (m != EMPTY_QUEUE) {
2676
                        break;
2677
                }
2678
 
2679
                if(time_after(jiffies,timeout)){
2680
                        printk(KERN_WARNING"%s: Timeout waiting for message frame\n",pHba->name);
2681
                        return -ETIMEDOUT;
2682
                }
2683
        } while(m == EMPTY_QUEUE);
2684
 
2685
        msg=(u32 *)(pHba->msg_addr_virt+m);
2686
 
2687
        status = kmalloc(4,GFP_KERNEL|ADDR32);
2688
        if (status==NULL) {
2689
                adpt_send_nop(pHba, m);
2690
                printk(KERN_WARNING"%s: IOP reset failed - no free memory.\n",
2691
                        pHba->name);
2692
                return -ENOMEM;
2693
        }
2694
        memset(status, 0, 4);
2695
 
2696
        writel(EIGHT_WORD_MSG_SIZE| SGL_OFFSET_6, &msg[0]);
2697
        writel(I2O_CMD_OUTBOUND_INIT<<24 | HOST_TID<<12 | ADAPTER_TID, &msg[1]);
2698
        writel(0, &msg[2]);
2699
        writel(0x0106, &msg[3]);        /* Transaction context */
2700
        writel(4096, &msg[4]);          /* Host page frame size */
2701
        writel((REPLY_FRAME_SIZE)<<16|0x80, &msg[5]);   /* Outbound msg frame size and Initcode */
2702
        writel(0xD0000004, &msg[6]);            /* Simple SG LE, EOB */
2703
        writel(virt_to_bus(status), &msg[7]);
2704
 
2705
        writel(m, pHba->post_port);
2706
        wmb();
2707
 
2708
        // Wait for the reply status to come back
2709
        do {
2710
                if (*status) {
2711
                        if (*status != 0x01 /*I2O_EXEC_OUTBOUND_INIT_IN_PROGRESS*/) {
2712
                                break;
2713
                        }
2714
                }
2715
                rmb();
2716
                if(time_after(jiffies,timeout)){
2717
                        printk(KERN_WARNING"%s: Timeout Initializing\n",pHba->name);
2718
                        kfree((void*)status);
2719
                        return -ETIMEDOUT;
2720
                }
2721
        } while (1);
2722
 
2723
        // If the command was successful, fill the fifo with our reply
2724
        // message packets
2725
        if(*status != 0x04 /*I2O_EXEC_OUTBOUND_INIT_COMPLETE*/) {
2726
                kfree((void*)status);
2727
                return -2;
2728
        }
2729
        kfree((void*)status);
2730
 
2731
        if(pHba->reply_pool != NULL){
2732
                kfree(pHba->reply_pool);
2733
        }
2734
 
2735
        pHba->reply_pool = (u32*)kmalloc(pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4, GFP_KERNEL|ADDR32);
2736
        if(!pHba->reply_pool){
2737
                printk(KERN_ERR"%s: Could not allocate reply pool\n",pHba->name);
2738
                return -1;
2739
        }
2740
        memset(pHba->reply_pool, 0 , pHba->reply_fifo_size * REPLY_FRAME_SIZE * 4);
2741
 
2742
        ptr = pHba->reply_pool;
2743
        for(i = 0; i < pHba->reply_fifo_size; i++) {
2744
                outbound_frame = (u32)virt_to_bus(ptr);
2745
                writel(outbound_frame, pHba->reply_port);
2746
                wmb();
2747
                ptr +=  REPLY_FRAME_SIZE;
2748
        }
2749
        adpt_i2o_status_get(pHba);
2750
        return 0;
2751
}
2752
 
2753
 
2754
/*
2755
 * I2O System Table.  Contains information about
2756
 * all the IOPs in the system.  Used to inform IOPs
2757
 * about each other's existence.
2758
 *
2759
 * sys_tbl_ver is the CurrentChangeIndicator that is
2760
 * used by IOPs to track changes.
2761
 */
2762
 
2763
 
2764
 
2765
static s32 adpt_i2o_status_get(adpt_hba* pHba)
2766
{
2767
        ulong timeout;
2768
        u32 m;
2769
        u32 *msg;
2770
        u8 *status_block=NULL;
2771
        ulong status_block_bus;
2772
 
2773
        if(pHba->status_block == NULL) {
2774
                pHba->status_block = (i2o_status_block*)
2775
                        kmalloc(sizeof(i2o_status_block),GFP_KERNEL|ADDR32);
2776
                if(pHba->status_block == NULL) {
2777
                        printk(KERN_ERR
2778
                        "dpti%d: Get Status Block failed; Out of memory. \n",
2779
                        pHba->unit);
2780
                        return -ENOMEM;
2781
                }
2782
        }
2783
        memset(pHba->status_block, 0, sizeof(i2o_status_block));
2784
        status_block = (u8*)(pHba->status_block);
2785
        status_block_bus = virt_to_bus(pHba->status_block);
2786
        timeout = jiffies+TMOUT_GETSTATUS*HZ;
2787
        do {
2788
                rmb();
2789
                m = readl(pHba->post_port);
2790
                if (m != EMPTY_QUEUE) {
2791
                        break;
2792
                }
2793
                if(time_after(jiffies,timeout)){
2794
                        printk(KERN_ERR "%s: Timeout waiting for message !\n",
2795
                                        pHba->name);
2796
                        return -ETIMEDOUT;
2797
                }
2798
        } while(m==EMPTY_QUEUE);
2799
 
2800
 
2801
        msg=(u32*)(pHba->msg_addr_virt+m);
2802
 
2803
        writel(NINE_WORD_MSG_SIZE|SGL_OFFSET_0, &msg[0]);
2804
        writel(I2O_CMD_STATUS_GET<<24|HOST_TID<<12|ADAPTER_TID, &msg[1]);
2805
        writel(1, &msg[2]);
2806
        writel(0, &msg[3]);
2807
        writel(0, &msg[4]);
2808
        writel(0, &msg[5]);
2809
        writel(((u32)status_block_bus)&0xffffffff, &msg[6]);
2810
        writel(0, &msg[7]);
2811
        writel(sizeof(i2o_status_block), &msg[8]); // 88 bytes
2812
 
2813
        //post message
2814
        writel(m, pHba->post_port);
2815
        wmb();
2816
 
2817
        while(status_block[87]!=0xff){
2818
                if(time_after(jiffies,timeout)){
2819
                        printk(KERN_ERR"dpti%d: Get status timeout.\n",
2820
                                pHba->unit);
2821
                        return -ETIMEDOUT;
2822
                }
2823
                rmb();
2824
        }
2825
 
2826
        // Set up our number of outbound and inbound messages
2827
        pHba->post_fifo_size = pHba->status_block->max_inbound_frames;
2828
        if (pHba->post_fifo_size > MAX_TO_IOP_MESSAGES) {
2829
                pHba->post_fifo_size = MAX_TO_IOP_MESSAGES;
2830
        }
2831
 
2832
        pHba->reply_fifo_size = pHba->status_block->max_outbound_frames;
2833
        if (pHba->reply_fifo_size > MAX_FROM_IOP_MESSAGES) {
2834
                pHba->reply_fifo_size = MAX_FROM_IOP_MESSAGES;
2835
        }
2836
 
2837
        // Calculate the Scatter Gather list size
2838
        pHba->sg_tablesize = (pHba->status_block->inbound_frame_size * 4 -40)/ sizeof(struct sg_simple_element);
2839
        if (pHba->sg_tablesize > SG_LIST_ELEMENTS) {
2840
                pHba->sg_tablesize = SG_LIST_ELEMENTS;
2841
        }
2842
 
2843
 
2844
#ifdef DEBUG
2845
        printk("dpti%d: State = ",pHba->unit);
2846
        switch(pHba->status_block->iop_state) {
2847
                case 0x01:
2848
                        printk("INIT\n");
2849
                        break;
2850
                case 0x02:
2851
                        printk("RESET\n");
2852
                        break;
2853
                case 0x04:
2854
                        printk("HOLD\n");
2855
                        break;
2856
                case 0x05:
2857
                        printk("READY\n");
2858
                        break;
2859
                case 0x08:
2860
                        printk("OPERATIONAL\n");
2861
                        break;
2862
                case 0x10:
2863
                        printk("FAILED\n");
2864
                        break;
2865
                case 0x11:
2866
                        printk("FAULTED\n");
2867
                        break;
2868
                default:
2869
                        printk("%x (unknown!!)\n",pHba->status_block->iop_state);
2870
        }
2871
#endif
2872
        return 0;
2873
}
2874
 
2875
/*
2876
 * Get the IOP's Logical Configuration Table
2877
 */
2878
static int adpt_i2o_lct_get(adpt_hba* pHba)
2879
{
2880
        u32 msg[8];
2881
        int ret;
2882
        u32 buf[16];
2883
 
2884
        if ((pHba->lct_size == 0) || (pHba->lct == NULL)){
2885
                pHba->lct_size = pHba->status_block->expected_lct_size;
2886
        }
2887
        do {
2888
                if (pHba->lct == NULL) {
2889
                        pHba->lct = kmalloc(pHba->lct_size, GFP_KERNEL|ADDR32);
2890
                        if(pHba->lct == NULL) {
2891
                                printk(KERN_CRIT "%s: Lct Get failed. Out of memory.\n",
2892
                                        pHba->name);
2893
                                return -ENOMEM;
2894
                        }
2895
                }
2896
                memset(pHba->lct, 0, pHba->lct_size);
2897
 
2898
                msg[0] = EIGHT_WORD_MSG_SIZE|SGL_OFFSET_6;
2899
                msg[1] = I2O_CMD_LCT_NOTIFY<<24 | HOST_TID<<12 | ADAPTER_TID;
2900
                msg[2] = 0;
2901
                msg[3] = 0;
2902
                msg[4] = 0xFFFFFFFF;    /* All devices */
2903
                msg[5] = 0x00000000;    /* Report now */
2904
                msg[6] = 0xD0000000|pHba->lct_size;
2905
                msg[7] = virt_to_bus(pHba->lct);
2906
 
2907
                if ((ret=adpt_i2o_post_wait(pHba, msg, sizeof(msg), 360))) {
2908
                        printk(KERN_ERR "%s: LCT Get failed (status=%#10x.\n",
2909
                                pHba->name, ret);
2910
                        printk(KERN_ERR"Adaptec: Error Reading Hardware.\n");
2911
                        return ret;
2912
                }
2913
 
2914
                if ((pHba->lct->table_size << 2) > pHba->lct_size) {
2915
                        pHba->lct_size = pHba->lct->table_size << 2;
2916
                        kfree(pHba->lct);
2917
                        pHba->lct = NULL;
2918
                }
2919
        } while (pHba->lct == NULL);
2920
 
2921
        PDEBUG("%s: Hardware resource table read.\n", pHba->name);
2922
 
2923
 
2924
        // I2O_DPT_EXEC_IOP_BUFFERS_GROUP_NO;
2925
        if(adpt_i2o_query_scalar(pHba, 0 , 0x8000, -1, buf, sizeof(buf))>=0) {
2926
                pHba->FwDebugBufferSize = buf[1];
2927
                pHba->FwDebugBuffer_P    = pHba->base_addr_virt + buf[0];
2928
                pHba->FwDebugFlags_P     = pHba->FwDebugBuffer_P + FW_DEBUG_FLAGS_OFFSET;
2929
                pHba->FwDebugBLEDvalue_P = pHba->FwDebugBuffer_P + FW_DEBUG_BLED_OFFSET;
2930
                pHba->FwDebugBLEDflag_P  = pHba->FwDebugBLEDvalue_P + 1;
2931
                pHba->FwDebugStrLength_P = pHba->FwDebugBuffer_P + FW_DEBUG_STR_LENGTH_OFFSET;
2932
                pHba->FwDebugBuffer_P += buf[2];
2933
                pHba->FwDebugFlags = 0;
2934
        }
2935
 
2936
        return 0;
2937
}
2938
 
2939
static int adpt_i2o_build_sys_table(void)
2940
{
2941
        adpt_hba* pHba = NULL;
2942
        int count = 0;
2943
 
2944
        sys_tbl_len = sizeof(struct i2o_sys_tbl) +      // Header + IOPs
2945
                                (hba_count) * sizeof(struct i2o_sys_tbl_entry);
2946
 
2947
        if(sys_tbl)
2948
                kfree(sys_tbl);
2949
 
2950
        sys_tbl = kmalloc(sys_tbl_len, GFP_KERNEL|ADDR32);
2951
        if(!sys_tbl) {
2952
                printk(KERN_WARNING "SysTab Set failed. Out of memory.\n");
2953
                return -ENOMEM;
2954
        }
2955
        memset(sys_tbl, 0, sys_tbl_len);
2956
 
2957
        sys_tbl->num_entries = hba_count;
2958
        sys_tbl->version = I2OVERSION;
2959
        sys_tbl->change_ind = sys_tbl_ind++;
2960
 
2961
        for(pHba = hba_chain; pHba; pHba = pHba->next) {
2962
                // Get updated Status Block so we have the latest information
2963
                if (adpt_i2o_status_get(pHba)) {
2964
                        sys_tbl->num_entries--;
2965
                        continue; // try next one       
2966
                }
2967
 
2968
                sys_tbl->iops[count].org_id = pHba->status_block->org_id;
2969
                sys_tbl->iops[count].iop_id = pHba->unit + 2;
2970
                sys_tbl->iops[count].seg_num = 0;
2971
                sys_tbl->iops[count].i2o_version = pHba->status_block->i2o_version;
2972
                sys_tbl->iops[count].iop_state = pHba->status_block->iop_state;
2973
                sys_tbl->iops[count].msg_type = pHba->status_block->msg_type;
2974
                sys_tbl->iops[count].frame_size = pHba->status_block->inbound_frame_size;
2975
                sys_tbl->iops[count].last_changed = sys_tbl_ind - 1; // ??
2976
                sys_tbl->iops[count].iop_capabilities = pHba->status_block->iop_capabilities;
2977
                sys_tbl->iops[count].inbound_low = (u32)virt_to_bus((void*)pHba->post_port);
2978
                sys_tbl->iops[count].inbound_high = (u32)((u64)virt_to_bus((void*)pHba->post_port)>>32);
2979
 
2980
                count++;
2981
        }
2982
 
2983
#ifdef DEBUG
2984
{
2985
        u32 *table = (u32*)sys_tbl;
2986
        printk(KERN_DEBUG"sys_tbl_len=%d in 32bit words\n",(sys_tbl_len >>2));
2987
        for(count = 0; count < (sys_tbl_len >>2); count++) {
2988
                printk(KERN_INFO "sys_tbl[%d] = %0#10x\n",
2989
                        count, table[count]);
2990
        }
2991
}
2992
#endif
2993
 
2994
        return 0;
2995
}
2996
 
2997
 
2998
/*
2999
 *       Dump the information block associated with a given unit (TID)
3000
 */
3001
 
3002
static void adpt_i2o_report_hba_unit(adpt_hba* pHba, struct i2o_device *d)
3003
{
3004
        char buf[64];
3005
        int unit = d->lct_data.tid;
3006
 
3007
        printk(KERN_INFO "TID %3.3d ", unit);
3008
 
3009
        if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 3, buf, 16)>=0)
3010
        {
3011
                buf[16]=0;
3012
                printk(" Vendor: %-12.12s", buf);
3013
        }
3014
        if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 4, buf, 16)>=0)
3015
        {
3016
                buf[16]=0;
3017
                printk(" Device: %-12.12s", buf);
3018
        }
3019
        if(adpt_i2o_query_scalar(pHba, unit, 0xF100, 6, buf, 8)>=0)
3020
        {
3021
                buf[8]=0;
3022
                printk(" Rev: %-12.12s\n", buf);
3023
        }
3024
#ifdef DEBUG
3025
         printk(KERN_INFO "\tClass: %.21s\n", adpt_i2o_get_class_name(d->lct_data.class_id));
3026
         printk(KERN_INFO "\tSubclass: 0x%04X\n", d->lct_data.sub_class);
3027
         printk(KERN_INFO "\tFlags: ");
3028
 
3029
         if(d->lct_data.device_flags&(1<<0))
3030
                  printk("C");       // ConfigDialog requested
3031
         if(d->lct_data.device_flags&(1<<1))
3032
                  printk("U");       // Multi-user capable
3033
         if(!(d->lct_data.device_flags&(1<<4)))
3034
                  printk("P");       // Peer service enabled!
3035
         if(!(d->lct_data.device_flags&(1<<5)))
3036
                  printk("M");       // Mgmt service enabled!
3037
         printk("\n");
3038
#endif
3039
}
3040
 
3041
#ifdef DEBUG
3042
/*
3043
 *      Do i2o class name lookup
3044
 */
3045
static const char *adpt_i2o_get_class_name(int class)
3046
{
3047
        int idx = 16;
3048
        static char *i2o_class_name[] = {
3049
                "Executive",
3050
                "Device Driver Module",
3051
                "Block Device",
3052
                "Tape Device",
3053
                "LAN Interface",
3054
                "WAN Interface",
3055
                "Fibre Channel Port",
3056
                "Fibre Channel Device",
3057
                "SCSI Device",
3058
                "ATE Port",
3059
                "ATE Device",
3060
                "Floppy Controller",
3061
                "Floppy Device",
3062
                "Secondary Bus Port",
3063
                "Peer Transport Agent",
3064
                "Peer Transport",
3065
                "Unknown"
3066
        };
3067
 
3068
        switch(class&0xFFF) {
3069
        case I2O_CLASS_EXECUTIVE:
3070
                idx = 0; break;
3071
        case I2O_CLASS_DDM:
3072
                idx = 1; break;
3073
        case I2O_CLASS_RANDOM_BLOCK_STORAGE:
3074
                idx = 2; break;
3075
        case I2O_CLASS_SEQUENTIAL_STORAGE:
3076
                idx = 3; break;
3077
        case I2O_CLASS_LAN:
3078
                idx = 4; break;
3079
        case I2O_CLASS_WAN:
3080
                idx = 5; break;
3081
        case I2O_CLASS_FIBRE_CHANNEL_PORT:
3082
                idx = 6; break;
3083
        case I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL:
3084
                idx = 7; break;
3085
        case I2O_CLASS_SCSI_PERIPHERAL:
3086
                idx = 8; break;
3087
        case I2O_CLASS_ATE_PORT:
3088
                idx = 9; break;
3089
        case I2O_CLASS_ATE_PERIPHERAL:
3090
                idx = 10; break;
3091
        case I2O_CLASS_FLOPPY_CONTROLLER:
3092
                idx = 11; break;
3093
        case I2O_CLASS_FLOPPY_DEVICE:
3094
                idx = 12; break;
3095
        case I2O_CLASS_BUS_ADAPTER_PORT:
3096
                idx = 13; break;
3097
        case I2O_CLASS_PEER_TRANSPORT_AGENT:
3098
                idx = 14; break;
3099
        case I2O_CLASS_PEER_TRANSPORT:
3100
                idx = 15; break;
3101
        }
3102
        return i2o_class_name[idx];
3103
}
3104
#endif
3105
 
3106
 
3107
static s32 adpt_i2o_hrt_get(adpt_hba* pHba)
3108
{
3109
        u32 msg[6];
3110
        int ret, size = sizeof(i2o_hrt);
3111
 
3112
        do {
3113
                if (pHba->hrt == NULL) {
3114
                        pHba->hrt=kmalloc(size, GFP_KERNEL|ADDR32);
3115
                        if (pHba->hrt == NULL) {
3116
                                printk(KERN_CRIT "%s: Hrt Get failed; Out of memory.\n", pHba->name);
3117
                                return -ENOMEM;
3118
                        }
3119
                }
3120
 
3121
                msg[0]= SIX_WORD_MSG_SIZE| SGL_OFFSET_4;
3122
                msg[1]= I2O_CMD_HRT_GET<<24 | HOST_TID<<12 | ADAPTER_TID;
3123
                msg[2]= 0;
3124
                msg[3]= 0;
3125
                msg[4]= (0xD0000000 | size);    /* Simple transaction */
3126
                msg[5]= virt_to_bus(pHba->hrt);   /* Dump it here */
3127
 
3128
                if ((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg),20))) {
3129
                        printk(KERN_ERR "%s: Unable to get HRT (status=%#10x)\n", pHba->name, ret);
3130
                        return ret;
3131
                }
3132
 
3133
                if (pHba->hrt->num_entries * pHba->hrt->entry_len << 2 > size) {
3134
                        size = pHba->hrt->num_entries * pHba->hrt->entry_len << 2;
3135
                        kfree(pHba->hrt);
3136
                        pHba->hrt = NULL;
3137
                }
3138
        } while(pHba->hrt == NULL);
3139
        return 0;
3140
}
3141
 
3142
/*
3143
 *       Query one scalar group value or a whole scalar group.
3144
 */
3145
static int adpt_i2o_query_scalar(adpt_hba* pHba, int tid,
3146
                        int group, int field, void *buf, int buflen)
3147
{
3148
        u16 opblk[] = { 1, 0, I2O_PARAMS_FIELD_GET, group, 1, field };
3149
        u8  resblk[8+buflen]; /* 8 bytes for header */
3150
        int size;
3151
 
3152
        if (field == -1)                /* whole group */
3153
                        opblk[4] = -1;
3154
 
3155
        size = adpt_i2o_issue_params(I2O_CMD_UTIL_PARAMS_GET, pHba, tid,
3156
                opblk, sizeof(opblk), resblk, sizeof(resblk));
3157
 
3158
        memcpy(buf, resblk+8, buflen);  /* cut off header */
3159
 
3160
        if (size < 0)
3161
                return size;
3162
 
3163
        return buflen;
3164
}
3165
 
3166
 
3167
/*      Issue UTIL_PARAMS_GET or UTIL_PARAMS_SET
3168
 *
3169
 *      This function can be used for all UtilParamsGet/Set operations.
3170
 *      The OperationBlock is given in opblk-buffer,
3171
 *      and results are returned in resblk-buffer.
3172
 *      Note that the minimum sized resblk is 8 bytes and contains
3173
 *      ResultCount, ErrorInfoSize, BlockStatus and BlockSize.
3174
 */
3175
static int adpt_i2o_issue_params(int cmd, adpt_hba* pHba, int tid,
3176
                  void *opblk, int oplen, void *resblk, int reslen)
3177
{
3178
        u32 msg[9];
3179
        u32 *res = (u32 *)resblk;
3180
        int wait_status;
3181
 
3182
        msg[0] = NINE_WORD_MSG_SIZE | SGL_OFFSET_5;
3183
        msg[1] = cmd << 24 | HOST_TID << 12 | tid;
3184
        msg[2] = 0;
3185
        msg[3] = 0;
3186
        msg[4] = 0;
3187
        msg[5] = 0x54000000 | oplen;    /* OperationBlock */
3188
        msg[6] = virt_to_bus(opblk);
3189
        msg[7] = 0xD0000000 | reslen;   /* ResultBlock */
3190
        msg[8] = virt_to_bus(resblk);
3191
 
3192
        if ((wait_status = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 20))) {
3193
                return wait_status;     /* -DetailedStatus */
3194
        }
3195
 
3196
        if (res[1]&0x00FF0000) {        /* BlockStatus != SUCCESS */
3197
                printk(KERN_WARNING "%s: %s - Error:\n  ErrorInfoSize = 0x%02x, "
3198
                        "BlockStatus = 0x%02x, BlockSize = 0x%04x\n",
3199
                        pHba->name,
3200
                        (cmd == I2O_CMD_UTIL_PARAMS_SET) ? "PARAMS_SET"
3201
                                                         : "PARAMS_GET",
3202
                        res[1]>>24, (res[1]>>16)&0xFF, res[1]&0xFFFF);
3203
                return -((res[1] >> 16) & 0xFF); /* -BlockStatus */
3204
        }
3205
 
3206
         return 4 + ((res[1] & 0x0000FFFF) << 2); /* bytes used in resblk */
3207
}
3208
 
3209
 
3210
static s32 adpt_i2o_quiesce_hba(adpt_hba* pHba)
3211
{
3212
        u32 msg[4];
3213
        int ret;
3214
 
3215
        adpt_i2o_status_get(pHba);
3216
 
3217
        /* SysQuiesce discarded if IOP not in READY or OPERATIONAL state */
3218
 
3219
        if((pHba->status_block->iop_state != ADAPTER_STATE_READY) &&
3220
           (pHba->status_block->iop_state != ADAPTER_STATE_OPERATIONAL)){
3221
                return 0;
3222
        }
3223
 
3224
        msg[0] = FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
3225
        msg[1] = I2O_CMD_SYS_QUIESCE<<24|HOST_TID<<12|ADAPTER_TID;
3226
        msg[2] = 0;
3227
        msg[3] = 0;
3228
 
3229
        if((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 240))) {
3230
                printk(KERN_INFO"dpti%d: Unable to quiesce (status=%#x).\n",
3231
                                pHba->unit, -ret);
3232
        } else {
3233
                printk(KERN_INFO"dpti%d: Quiesced.\n",pHba->unit);
3234
        }
3235
 
3236
        adpt_i2o_status_get(pHba);
3237
        return ret;
3238
}
3239
 
3240
 
3241
/*
3242
 * Enable IOP. Allows the IOP to resume external operations.
3243
 */
3244
static int adpt_i2o_enable_hba(adpt_hba* pHba)
3245
{
3246
        u32 msg[4];
3247
        int ret;
3248
 
3249
        adpt_i2o_status_get(pHba);
3250
        if(!pHba->status_block){
3251
                return -ENOMEM;
3252
        }
3253
        /* Enable only allowed on READY state */
3254
        if(pHba->status_block->iop_state == ADAPTER_STATE_OPERATIONAL)
3255
                return 0;
3256
 
3257
        if(pHba->status_block->iop_state != ADAPTER_STATE_READY)
3258
                return -EINVAL;
3259
 
3260
        msg[0]=FOUR_WORD_MSG_SIZE|SGL_OFFSET_0;
3261
        msg[1]=I2O_CMD_SYS_ENABLE<<24|HOST_TID<<12|ADAPTER_TID;
3262
        msg[2]= 0;
3263
        msg[3]= 0;
3264
 
3265
        if ((ret = adpt_i2o_post_wait(pHba, msg, sizeof(msg), 240))) {
3266
                printk(KERN_WARNING"%s: Could not enable (status=%#10x).\n",
3267
                        pHba->name, ret);
3268
        } else {
3269
                PDEBUG("%s: Enabled.\n", pHba->name);
3270
        }
3271
 
3272
        adpt_i2o_status_get(pHba);
3273
        return ret;
3274
}
3275
 
3276
 
3277
static int adpt_i2o_systab_send(adpt_hba* pHba)
3278
{
3279
         u32 msg[12];
3280
         int ret;
3281
 
3282
        msg[0] = I2O_MESSAGE_SIZE(12) | SGL_OFFSET_6;
3283
        msg[1] = I2O_CMD_SYS_TAB_SET<<24 | HOST_TID<<12 | ADAPTER_TID;
3284
        msg[2] = 0;
3285
        msg[3] = 0;
3286
        msg[4] = (0<<16) | ((pHba->unit+2) << 12); /* Host 0 IOP ID (unit + 2) */
3287
        msg[5] = 0;                                 /* Segment 0 */
3288
 
3289
        /*
3290
         * Provide three SGL-elements:
3291
         * System table (SysTab), Private memory space declaration and
3292
         * Private i/o space declaration
3293
         */
3294
        msg[6] = 0x54000000 | sys_tbl_len;
3295
        msg[7] = virt_to_phys(sys_tbl);
3296
        msg[8] = 0x54000000 | 0;
3297
        msg[9] = 0;
3298
        msg[10] = 0xD4000000 | 0;
3299
        msg[11] = 0;
3300
 
3301
        if ((ret=adpt_i2o_post_wait(pHba, msg, sizeof(msg), 120))) {
3302
                printk(KERN_INFO "%s: Unable to set SysTab (status=%#10x).\n",
3303
                        pHba->name, ret);
3304
        }
3305
#ifdef DEBUG
3306
        else {
3307
                PINFO("%s: SysTab set.\n", pHba->name);
3308
        }
3309
#endif
3310
 
3311
        return ret;
3312
 }
3313
 
3314
 
3315
/*============================================================================
3316
 *
3317
 *============================================================================
3318
 */
3319
 
3320
 
3321
#ifdef UARTDELAY 
3322
 
3323
static static void adpt_delay(int millisec)
3324
{
3325
        int i;
3326
        for (i = 0; i < millisec; i++) {
3327
                udelay(1000);   /* delay for one millisecond */
3328
        }
3329
}
3330
 
3331
#endif
3332
 
3333
static Scsi_Host_Template driver_template = DPT_I2O;
3334
#include "scsi_module.c"
3335
EXPORT_NO_SYMBOLS;
3336
MODULE_LICENSE("GPL");

powered by: WebSVN 2.1.0

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