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

Subversion Repositories or1k

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

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

Line No. Rev Author Line
1 1275 phoenix
/*
2
 *
3
 *                      Linux MegaRAID device driver
4
 *
5
 * Copyright (c) 2002  LSI Logic Corporation.
6
 *
7
 *         This program is free software; you can redistribute it and/or
8
 *         modify it under the terms of the GNU General Public License
9
 *         as published by the Free Software Foundation; either version
10
 *         2 of the License, or (at your option) any later version.
11
 *
12
 * Copyright (c) 2002  Red Hat, Inc. All rights reserved.
13
 *        - fixes
14
 *        - speed-ups (list handling fixes, issued_list, optimizations.)
15
 *        - lots of cleanups.
16
 *
17
 * Version : v2.10.1 (Dec 03, 2003) - Atul Mukker <Atul.Mukker@lsil.com>
18
 *
19
 * Description: Linux device driver for LSI Logic MegaRAID controller
20
 *
21
 * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
22
 *                                      518, 520, 531, 532
23
 *
24
 * This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
25
 * and others. Please send updates to the public mailing list
26
 * linux-megaraid-devel@dell.com, and subscribe to and read archives of this
27
 * list at http://lists.us.dell.com/.
28
 *
29
 * For history of changes, see ChangeLog.megaraid.
30
 *
31
 */
32
 
33
#include <linux/mm.h>
34
#include <linux/fs.h>
35
#include <linux/blk.h>
36
#include <asm/uaccess.h>
37
#include <linux/delay.h>
38
#include <linux/reboot.h>
39
#include <linux/module.h>
40
#include <linux/list.h>
41
 
42
#include "sd.h"
43
#include "scsi.h"
44
#include "hosts.h"
45
 
46
#include "megaraid2.h"
47
 
48
MODULE_AUTHOR ("LSI Logic Corporation");
49
MODULE_DESCRIPTION ("LSI Logic MegaRAID driver");
50
MODULE_LICENSE ("GPL");
51
 
52
static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
53
MODULE_PARM(max_cmd_per_lun, "i");
54
MODULE_PARM_DESC(max_cmd_per_lun, "Maximum number of commands which can be issued to a single LUN (default=DEF_CMD_PER_LUN=63)");
55
 
56
static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
57
MODULE_PARM(max_sectors_per_io, "h");
58
MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
59
 
60
 
61
static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
62
MODULE_PARM(max_mbox_busy_wait, "h");
63
MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
64
 
65
#define RDINDOOR(adapter)               readl((adapter)->base + 0x20)
66
#define RDOUTDOOR(adapter)              readl((adapter)->base + 0x2C)
67
#define WRINDOOR(adapter,value)         writel(value, (adapter)->base + 0x20)
68
#define WROUTDOOR(adapter,value)        writel(value, (adapter)->base + 0x2C)
69
 
70
/*
71
 * Global variables
72
 */
73
 
74
static int hba_count;
75
static adapter_t *hba_soft_state[MAX_CONTROLLERS];
76
#ifdef CONFIG_PROC_FS
77
static struct proc_dir_entry *mega_proc_dir_entry;
78
#endif
79
 
80
static struct notifier_block mega_notifier = {
81
        .notifier_call = megaraid_reboot_notify
82
};
83
 
84
/* For controller re-ordering */
85
static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
86
 
87
/*
88
 * The File Operations structure for the serial/ioctl interface of the driver
89
 */
90
static struct file_operations megadev_fops = {
91
        .ioctl          = megadev_ioctl,
92
        .open           = megadev_open,
93
        .release        = megadev_close,
94
        .owner          = THIS_MODULE,
95
};
96
 
97
/*
98
 * Array to structures for storing the information about the controllers. This
99
 * information is sent to the user level applications, when they do an ioctl
100
 * for this information.
101
 */
102
static struct mcontroller mcontroller[MAX_CONTROLLERS];
103
 
104
/* The current driver version */
105
static u32 driver_ver = 0x02100000;
106
 
107
/* major number used by the device for character interface */
108
static int major;
109
 
110
#define IS_RAID_CH(hba, ch)     (((hba)->mega_ch_class >> (ch)) & 0x01)
111
 
112
 
113
/*
114
 * Debug variable to print some diagnostic messages
115
 */
116
static int trace_level;
117
 
118
/*
119
 * megaraid_validate_parms()
120
 *
121
 * Validate that any module parms passed in
122
 * have proper values.
123
 */
124
static void
125
megaraid_validate_parms(void)
126
{
127
        if( (max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN) )
128
                max_cmd_per_lun = MAX_CMD_PER_LUN;
129
        if( max_mbox_busy_wait > MBOX_BUSY_WAIT )
130
                max_mbox_busy_wait = MBOX_BUSY_WAIT;
131
}
132
 
133
 
134
/**
135
 * megaraid_detect()
136
 * @host_template - Our soft state maintained by mid-layer
137
 *
138
 * the detect entry point for the mid-layer.
139
 * We scan the PCI bus for our controllers and start them.
140
 *
141
 * Note: PCI_DEVICE_ID_PERC4_DI below represents the PERC4/Di class of
142
 * products. All of them share the same vendor id, device id, and subsystem
143
 * vendor id but different subsystem ids. As of now, driver does not use the
144
 * subsystem id.
145
 * PERC4E device ids are for the PCI-Express controllers
146
 */
147
static int
148
megaraid_detect(Scsi_Host_Template *host_template)
149
{
150
        int     i;
151
        u16     dev_sw_table[] = {      /* Table of all supported
152
                                           vendor/device ids */
153
 
154
                PCI_VENDOR_ID_LSI_LOGIC,        PCI_DEVICE_ID_LSI_SATA_PCIX,
155
                PCI_VENDOR_ID_LSI_LOGIC,        PCI_DEVICE_ID_PERC4E_DC_SC,
156
                PCI_VENDOR_ID_DELL,             PCI_DEVICE_ID_PERC4E_SI_DI,
157
                PCI_VENDOR_ID_DELL,             PCI_DEVICE_ID_DISCOVERY,
158
                PCI_VENDOR_ID_DELL,             PCI_DEVICE_ID_PERC4_DI,
159
                PCI_VENDOR_ID_LSI_LOGIC,        PCI_DEVICE_ID_PERC4_QC_VERDE,
160
                PCI_VENDOR_ID_AMI,              PCI_DEVICE_ID_AMI_MEGARAID,
161
                PCI_VENDOR_ID_AMI,              PCI_DEVICE_ID_AMI_MEGARAID2,
162
                PCI_VENDOR_ID_AMI,              PCI_DEVICE_ID_AMI_MEGARAID3,
163
                PCI_VENDOR_ID_INTEL,            PCI_DEVICE_ID_AMI_MEGARAID3,
164
                PCI_VENDOR_ID_LSI_LOGIC,        PCI_DEVICE_ID_AMI_MEGARAID3 };
165
 
166
 
167
        printk(KERN_NOTICE "megaraid: " MEGARAID_VERSION);
168
 
169
        megaraid_validate_parms();
170
 
171
        /*
172
         * Scan PCI bus for our all devices.
173
         */
174
        for( i = 0; i < sizeof(dev_sw_table)/sizeof(u16); i += 2 ) {
175
 
176
                mega_find_card(host_template, dev_sw_table[i],
177
                                dev_sw_table[i+1]);
178
        }
179
 
180
        if(hba_count) {
181
                /*
182
                 * re-order hosts so that one with bootable logical drive
183
                 * comes first
184
                 */
185
                mega_reorder_hosts();
186
 
187
#ifdef CONFIG_PROC_FS
188
                mega_proc_dir_entry = proc_mkdir("megaraid", &proc_root);
189
 
190
                if(!mega_proc_dir_entry) {
191
                        printk(KERN_WARNING
192
                                "megaraid: failed to create megaraid root\n");
193
                }
194
                else {
195
                        for(i = 0; i < hba_count; i++) {
196
                                mega_create_proc_entry(i, mega_proc_dir_entry);
197
                        }
198
                }
199
#endif
200
 
201
                /*
202
                 * Register the driver as a character device, for applications
203
                 * to access it for ioctls.
204
                 * First argument (major) to register_chrdev implies a dynamic
205
                 * major number allocation.
206
                 */
207
                major = register_chrdev(0, "megadev", &megadev_fops);
208
 
209
                /*
210
                 * Register the Shutdown Notification hook in kernel
211
                 */
212
                if(register_reboot_notifier(&mega_notifier)) {
213
                        printk(KERN_WARNING
214
                                "MegaRAID Shutdown routine not registered!!\n");
215
                }
216
 
217
        }
218
 
219
        return hba_count;
220
}
221
 
222
 
223
 
224
/**
225
 * mega_find_card() - find and start this controller
226
 * @host_template - Our soft state maintained by mid-layer
227
 * @pci_vendor - pci vendor id for this controller
228
 * @pci_device - pci device id for this controller
229
 *
230
 * Scans the PCI bus for this vendor and device id combination, setup the
231
 * resources, and register ourselves as a SCSI HBA driver, and setup all
232
 * parameters for our soft state.
233
 *
234
 * This routine also checks for some buggy firmware and ajust the flags
235
 * accordingly.
236
 */
237
static void
238
mega_find_card(Scsi_Host_Template *host_template, u16 pci_vendor,
239
        u16 pci_device)
240
{
241
        struct Scsi_Host        *host = NULL;
242
        adapter_t       *adapter = NULL;
243
        u32     magic64;
244
        unsigned long   mega_baseport;
245
        u16     subsysid, subsysvid;
246
        u8      pci_bus;
247
        u8      pci_dev_func;
248
        u8      irq;
249
        struct pci_dev  *pdev = NULL;
250
        u8      did_ioremap_f = 0;
251
        u8      did_req_region_f = 0;
252
        u8      did_scsi_reg_f = 0;
253
        u8      alloc_int_buf_f = 0;
254
        u8      alloc_scb_f = 0;
255
        u8      got_irq_f = 0;
256
        u8      did_setup_mbox_f = 0;
257
        unsigned long   tbase;
258
        unsigned long   flag = 0;
259
        int     i, j;
260
 
261
        while((pdev = pci_find_device(pci_vendor, pci_device, pdev))) {
262
 
263
                // reset flags for all controllers in this class
264
                did_ioremap_f = 0;
265
                did_req_region_f = 0;
266
                did_scsi_reg_f = 0;
267
                alloc_int_buf_f = 0;
268
                alloc_scb_f = 0;
269
                got_irq_f = 0;
270
                did_setup_mbox_f = 0;
271
 
272
                if(pci_enable_device (pdev)) continue;
273
 
274
                pci_bus = pdev->bus->number;
275
                pci_dev_func = pdev->devfn;
276
 
277
                /*
278
                 * For these vendor and device ids, signature offsets are not
279
                 * valid and 64 bit is implicit
280
                 */
281
                if( (pci_vendor == PCI_VENDOR_ID_DELL &&
282
                                pci_device == PCI_DEVICE_ID_PERC4_DI) ||
283
                        (pci_vendor == PCI_VENDOR_ID_LSI_LOGIC &&
284
                                pci_device == PCI_DEVICE_ID_PERC4_QC_VERDE) ||
285
                        (pci_vendor == PCI_VENDOR_ID_LSI_LOGIC &&
286
                                pci_device == PCI_DEVICE_ID_PERC4E_DC_SC) ||
287
                        (pci_vendor == PCI_VENDOR_ID_DELL &&
288
                                pci_device == PCI_DEVICE_ID_PERC4E_SI_DI) ||
289
                        (pci_vendor == PCI_VENDOR_ID_LSI_LOGIC &&
290
                                pci_device == PCI_DEVICE_ID_LSI_SATA_PCIX)) {
291
 
292
                        flag |= BOARD_64BIT;
293
                }
294
                else {
295
                        pci_read_config_dword(pdev, PCI_CONF_AMISIG64,
296
                                        &magic64);
297
 
298
                        if (magic64 == HBA_SIGNATURE_64BIT)
299
                                flag |= BOARD_64BIT;
300
                }
301
 
302
                subsysvid = pdev->subsystem_vendor;
303
                subsysid = pdev->subsystem_device;
304
 
305
                /*
306
                 * If we do not find the valid subsys vendor id, refuse to
307
                 * load the driver. This is part of PCI200X compliance
308
                 * We load the driver if subsysvid is 0.
309
                 */
310
                if( subsysvid && (subsysvid != AMI_SUBSYS_VID) &&
311
                                (subsysvid != DELL_SUBSYS_VID) &&
312
                                (subsysvid != HP_SUBSYS_VID) &&
313
                                (subsysvid != INTEL_SUBSYS_VID) &&
314
                                (subsysvid != LSI_SUBSYS_VID) ) continue;
315
 
316
 
317
                printk(KERN_NOTICE "megaraid: found 0x%4.04x:0x%4.04x:bus %d:",
318
                        pci_vendor, pci_device, pci_bus);
319
 
320
                printk("slot %d:func %d\n",
321
                        PCI_SLOT(pci_dev_func), PCI_FUNC(pci_dev_func));
322
 
323
                /* Read the base port and IRQ from PCI */
324
                mega_baseport = pci_resource_start(pdev, 0);
325
                irq = pdev->irq;
326
 
327
                tbase = mega_baseport;
328
 
329
                if( pci_resource_flags(pdev, 0) & IORESOURCE_MEM ) {
330
 
331
                        if( check_mem_region(mega_baseport, 128) ) {
332
                                printk(KERN_WARNING
333
                                        "megaraid: mem region busy!\n");
334
                                continue;
335
                        }
336
                        request_mem_region(mega_baseport, 128,
337
                                        "MegaRAID: LSI Logic Corporation.");
338
 
339
                        mega_baseport =
340
                                (unsigned long)ioremap(mega_baseport, 128);
341
 
342
                        if( !mega_baseport ) {
343
                                printk(KERN_WARNING
344
                                        "megaraid: could not map hba memory\n");
345
 
346
                                release_mem_region(tbase, 128);
347
 
348
                                continue;
349
                        }
350
 
351
                        flag |= BOARD_MEMMAP;
352
 
353
                        did_ioremap_f = 1;
354
                }
355
                else {
356
                        mega_baseport += 0x10;
357
 
358
                        if( !request_region(mega_baseport, 16, "megaraid") )
359
                                goto fail_attach;
360
 
361
                        flag |= BOARD_IOMAP;
362
 
363
                        did_req_region_f = 1;
364
                }
365
 
366
                /* Initialize SCSI Host structure */
367
                host = scsi_register(host_template, sizeof(adapter_t));
368
 
369
                if(!host) goto fail_attach;
370
 
371
                did_scsi_reg_f = 1;
372
 
373
                scsi_set_pci_device(host, pdev);
374
 
375
                adapter = (adapter_t *)host->hostdata;
376
                memset(adapter, 0, sizeof(adapter_t));
377
 
378
                printk(KERN_NOTICE
379
                        "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
380
                        host->host_no, mega_baseport, irq);
381
 
382
                adapter->base = mega_baseport;
383
 
384
                /* Copy resource info into structure */
385
                INIT_LIST_HEAD(&adapter->free_list);
386
                INIT_LIST_HEAD(&adapter->pending_list);
387
 
388
                adapter->flag = flag;
389
                spin_lock_init(&adapter->lock);
390
 
391
#ifdef SCSI_HAS_HOST_LOCK
392
#  if LINUX_VERSION_CODE <= KERNEL_VERSION(2,4,9)
393
                /* This is the Red Hat AS2.1 kernel */
394
                adapter->host_lock = &adapter->lock;
395
                host->lock = adapter->host_lock;
396
#  elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
397
                /* This is the later Red Hat 2.4 kernels */
398
                adapter->host_lock = &adapter->lock;
399
                host->host_lock = adapter->host_lock;
400
#  else
401
                /* This is the 2.6 and later kernel series */
402
                adapter->host_lock = &adapter->lock;
403
                scsi_set_host_lock(&adapter->lock);
404
#  endif
405
#else
406
                /* And this is the remainder of the 2.4 kernel series */
407
                adapter->host_lock = &io_request_lock;
408
#endif
409
 
410
                host->cmd_per_lun = max_cmd_per_lun;
411
                host->max_sectors = max_sectors_per_io;
412
 
413
                adapter->dev = pdev;
414
                adapter->host = host;
415
 
416
                adapter->host->irq = irq;
417
 
418
                if( flag & BOARD_MEMMAP ) {
419
                        adapter->host->base = tbase;
420
                }
421
                else {
422
                        adapter->host->io_port = tbase;
423
                        adapter->host->n_io_port = 16;
424
                }
425
 
426
                adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
427
 
428
                /*
429
                 * Allocate buffer to issue internal commands.
430
                 */
431
                adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
432
                        MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
433
 
434
                if( !adapter->mega_buffer ) {
435
                        printk(KERN_WARNING "megaraid: out of RAM.\n");
436
                        goto fail_attach;
437
                }
438
                alloc_int_buf_f = 1;
439
 
440
                adapter->scb_list = kmalloc(sizeof(scb_t)*MAX_COMMANDS,
441
                                GFP_KERNEL);
442
 
443
                if(!adapter->scb_list) {
444
                        printk(KERN_WARNING "megaraid: out of RAM.\n");
445
                        goto fail_attach;
446
                }
447
 
448
                alloc_scb_f = 1;
449
 
450
                /* Request our IRQ */
451
                if( adapter->flag & BOARD_MEMMAP ) {
452
                        if(request_irq(irq, megaraid_isr_memmapped, SA_SHIRQ,
453
                                                "megaraid", adapter)) {
454
                                printk(KERN_WARNING
455
                                        "megaraid: Couldn't register IRQ %d!\n",
456
                                        irq);
457
                                goto fail_attach;
458
                        }
459
                }
460
                else {
461
                        if(request_irq(irq, megaraid_isr_iomapped, SA_SHIRQ,
462
                                                "megaraid", adapter)) {
463
                                printk(KERN_WARNING
464
                                        "megaraid: Couldn't register IRQ %d!\n",
465
                                        irq);
466
                                goto fail_attach;
467
                        }
468
                }
469
                got_irq_f = 1;
470
 
471
                if( mega_setup_mailbox(adapter) != 0 )
472
                        goto fail_attach;
473
 
474
                did_setup_mbox_f = 1;
475
 
476
                if( mega_query_adapter(adapter) != 0 )
477
                        goto fail_attach;
478
 
479
                /*
480
                 * Have checks for some buggy f/w
481
                 */
482
                if((subsysid == 0x1111) && (subsysvid == 0x1111)) {
483
                        /*
484
                         * Which firmware
485
                         */
486
                        if (!strcmp(adapter->fw_version, "3.00") ||
487
                                        !strcmp(adapter->fw_version, "3.01")) {
488
 
489
                                printk( KERN_WARNING
490
                                        "megaraid: Your  card is a Dell PERC "
491
                                        "2/SC RAID controller with  "
492
                                        "firmware\nmegaraid: 3.00 or 3.01.  "
493
                                        "This driver is known to have "
494
                                        "corruption issues\nmegaraid: with "
495
                                        "those firmware versions on this "
496
                                        "specific card.  In order\nmegaraid: "
497
                                        "to protect your data, please upgrade "
498
                                        "your firmware to version\nmegaraid: "
499
                                        "3.10 or later, available from the "
500
                                        "Dell Technical Support web\n"
501
                                        "megaraid: site at\nhttp://support."
502
                                        "dell.com/us/en/filelib/download/"
503
                                        "index.asp?fileid=2940\n"
504
                                );
505
                        }
506
                }
507
 
508
                /*
509
                 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
510
                 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
511
                 * support, since this firmware cannot handle 64 bit
512
                 * addressing
513
                 */
514
 
515
                if((subsysvid == HP_SUBSYS_VID) &&
516
                                ((subsysid == 0x60E7)||(subsysid == 0x60E8))) {
517
 
518
                        /*
519
                         * which firmware
520
                         */
521
                        if( !strcmp(adapter->fw_version, "H01.07") ||
522
                                !strcmp(adapter->fw_version, "H01.08") ||
523
                                !strcmp(adapter->fw_version, "H01.09") ) {
524
 
525
                                printk(KERN_WARNING
526
                                        "megaraid: Firmware H.01.07, "
527
                                        "H.01.08, and H.01.09 on 1M/2M "
528
                                        "controllers\n"
529
                                        "megaraid: do not support 64 bit "
530
                                        "addressing.\nmegaraid: DISABLING "
531
                                        "64 bit support.\n");
532
                                adapter->flag &= ~BOARD_64BIT;
533
                        }
534
                }
535
 
536
 
537
                if(mega_is_bios_enabled(adapter)) {
538
                        mega_hbas[hba_count].is_bios_enabled = 1;
539
                }
540
                mega_hbas[hba_count].hostdata_addr = adapter;
541
 
542
                /*
543
                 * Find out which channel is raid and which is scsi. This is
544
                 * for ROMB support.
545
                 */
546
                mega_enum_raid_scsi(adapter);
547
 
548
                /*
549
                 * Find out if a logical drive is set as the boot drive. If
550
                 * there is one, will make that as the first logical drive.
551
                 * ROMB: Do we have to boot from a physical drive. Then all
552
                 * the physical drives would appear before the logical disks.
553
                 * Else, all the physical drives would be exported to the mid
554
                 * layer after logical drives.
555
                 */
556
                mega_get_boot_drv(adapter);
557
 
558
                if( ! adapter->boot_pdrv_enabled ) {
559
                        for( i = 0; i < NVIRT_CHAN; i++ )
560
                                adapter->logdrv_chan[i] = 1;
561
 
562
                        for( i = NVIRT_CHAN; i<MAX_CHANNELS+NVIRT_CHAN; i++ )
563
                                adapter->logdrv_chan[i] = 0;
564
 
565
                        adapter->mega_ch_class <<= NVIRT_CHAN;
566
                }
567
                else {
568
                        j = adapter->product_info.nchannels;
569
                        for( i = 0; i < j; i++ )
570
                                adapter->logdrv_chan[i] = 0;
571
 
572
                        for( i = j; i < NVIRT_CHAN + j; i++ )
573
                                adapter->logdrv_chan[i] = 1;
574
                }
575
 
576
 
577
                /*
578
                 * Do we support random deletion and addition of logical
579
                 * drives
580
                 */
581
                adapter->read_ldidmap = 0;       /* set it after first logdrv
582
                                                   delete cmd */
583
                adapter->support_random_del = mega_support_random_del(adapter);
584
 
585
                /* Initialize SCBs */
586
                if(mega_init_scb(adapter)) {
587
                        goto fail_attach;
588
                }
589
 
590
                /*
591
                 * Reset the pending commands counter
592
                 */
593
                atomic_set(&adapter->pend_cmds, 0);
594
 
595
                /*
596
                 * Reset the adapter quiescent flag
597
                 */
598
                atomic_set(&adapter->quiescent, 0);
599
 
600
                hba_soft_state[hba_count] = adapter;
601
 
602
                /*
603
                 * Fill in the structure which needs to be passed back to the
604
                 * application when it does an ioctl() for controller related
605
                 * information.
606
                 */
607
                i = hba_count;
608
 
609
                mcontroller[i].base = mega_baseport;
610
                mcontroller[i].irq = irq;
611
                mcontroller[i].numldrv = adapter->numldrv;
612
                mcontroller[i].pcibus = pci_bus;
613
                mcontroller[i].pcidev = pci_device;
614
                mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
615
                mcontroller[i].pciid = -1;
616
                mcontroller[i].pcivendor = pci_vendor;
617
                mcontroller[i].pcislot = PCI_SLOT (pci_dev_func);
618
                mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
619
 
620
 
621
                /* Set the Mode of addressing to 64 bit if we can */
622
                if((adapter->flag & BOARD_64BIT)&&(sizeof(dma_addr_t) == 8)) {
623
                        pci_set_dma_mask(pdev, 0xffffffffffffffffULL);
624
                        adapter->has_64bit_addr = 1;
625
                }
626
                else  {
627
                        pci_set_dma_mask(pdev, 0xffffffff);
628
                        adapter->has_64bit_addr = 0;
629
                }
630
 
631
                init_MUTEX(&adapter->int_mtx);
632
                init_waitqueue_head(&adapter->int_waitq);
633
 
634
                adapter->this_id = DEFAULT_INITIATOR_ID;
635
                adapter->host->this_id = DEFAULT_INITIATOR_ID;
636
 
637
#if MEGA_HAVE_CLUSTERING
638
                /*
639
                 * Is cluster support enabled on this controller
640
                 * Note: In a cluster the HBAs ( the initiators ) will have
641
                 * different target IDs and we cannot assume it to be 7. Call
642
                 * to mega_support_cluster() will get the target ids also if
643
                 * the cluster support is available
644
                 */
645
                adapter->has_cluster = mega_support_cluster(adapter);
646
 
647
                if( adapter->has_cluster ) {
648
                        printk(KERN_NOTICE
649
                                "megaraid: Cluster driver, initiator id:%d\n",
650
                                adapter->this_id);
651
                }
652
#endif
653
 
654
                hba_count++;
655
                continue;
656
 
657
fail_attach:
658
                if( did_setup_mbox_f ) {
659
                        pci_free_consistent(adapter->dev, sizeof(mbox64_t),
660
                                        (void *)adapter->una_mbox64,
661
                                        adapter->una_mbox64_dma);
662
                }
663
 
664
                if( got_irq_f ) {
665
                        irq_disable(adapter);
666
                        free_irq(adapter->host->irq, adapter);
667
                }
668
 
669
                if( alloc_scb_f ) {
670
                        kfree(adapter->scb_list);
671
                }
672
 
673
                if( alloc_int_buf_f ) {
674
                        pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
675
                                        (void *)adapter->mega_buffer,
676
                                        adapter->buf_dma_handle);
677
                }
678
 
679
                if( did_scsi_reg_f ) scsi_unregister(host);
680
 
681
                if( did_ioremap_f ) {
682
                        iounmap((void *)mega_baseport);
683
                        release_mem_region(tbase, 128);
684
                }
685
 
686
                if( did_req_region_f )
687
                        release_region(mega_baseport, 16);
688
        }
689
 
690
        return;
691
}
692
 
693
 
694
/**
695
 * mega_setup_mailbox()
696
 * @adapter - pointer to our soft state
697
 *
698
 * Allocates a 8 byte aligned memory for the handshake mailbox.
699
 */
700
static int
701
mega_setup_mailbox(adapter_t *adapter)
702
{
703
        unsigned long   align;
704
 
705
        adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
706
                        sizeof(mbox64_t), &adapter->una_mbox64_dma);
707
 
708
        if( !adapter->una_mbox64 ) return -1;
709
 
710
        adapter->mbox = &adapter->una_mbox64->mbox;
711
 
712
        adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
713
                        (~0UL ^ 0xFUL));
714
 
715
        adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
716
 
717
        align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
718
 
719
        adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
720
 
721
        /*
722
         * Register the mailbox if the controller is an io-mapped controller
723
         */
724
        if( adapter->flag & BOARD_IOMAP ) {
725
 
726
                outb_p(adapter->mbox_dma & 0xFF,
727
                                adapter->host->io_port + MBOX_PORT0);
728
 
729
                outb_p((adapter->mbox_dma >> 8) & 0xFF,
730
                                adapter->host->io_port + MBOX_PORT1);
731
 
732
                outb_p((adapter->mbox_dma >> 16) & 0xFF,
733
                                adapter->host->io_port + MBOX_PORT2);
734
 
735
                outb_p((adapter->mbox_dma >> 24) & 0xFF,
736
                                adapter->host->io_port + MBOX_PORT3);
737
 
738
                outb_p(ENABLE_MBOX_BYTE,
739
                                adapter->host->io_port + ENABLE_MBOX_REGION);
740
 
741
                irq_ack(adapter);
742
 
743
                irq_enable(adapter);
744
        }
745
 
746
        return 0;
747
}
748
 
749
 
750
/*
751
 * mega_query_adapter()
752
 * @adapter - pointer to our soft state
753
 *
754
 * Issue the adapter inquiry commands to the controller and find out
755
 * information and parameter about the devices attached
756
 */
757
static int
758
mega_query_adapter(adapter_t *adapter)
759
{
760
        dma_addr_t      prod_info_dma_handle;
761
        mega_inquiry3   *inquiry3;
762
        u8      raw_mbox[sizeof(mbox_t)];
763
        mbox_t  *mbox;
764
        int     retval;
765
 
766
        /* Initialize adapter inquiry mailbox */
767
 
768
        mbox = (mbox_t *)raw_mbox;
769
 
770
        memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
771
        memset(raw_mbox, 0, sizeof(raw_mbox));
772
 
773
        /*
774
         * Try to issue Inquiry3 command
775
         * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
776
         * update enquiry3 structure
777
         */
778
        mbox->xferaddr = (u32)adapter->buf_dma_handle;
779
 
780
        inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
781
 
782
        raw_mbox[0] = FC_NEW_CONFIG;             /* i.e. mbox->cmd=0xA1 */
783
        raw_mbox[2] = NC_SUBOP_ENQUIRY3;        /* i.e. 0x0F */
784
        raw_mbox[3] = ENQ3_GET_SOLICITED_FULL;  /* i.e. 0x02 */
785
 
786
        /* Issue a blocking command to the card */
787
        if ((retval = issue_scb_block(adapter, raw_mbox))) {
788
                /* the adapter does not support 40ld */
789
 
790
                mraid_ext_inquiry       *ext_inq;
791
                mraid_inquiry           *inq;
792
                dma_addr_t              dma_handle;
793
 
794
                ext_inq = pci_alloc_consistent(adapter->dev,
795
                                sizeof(mraid_ext_inquiry), &dma_handle);
796
 
797
                if( ext_inq == NULL ) return -1;
798
 
799
                inq = &ext_inq->raid_inq;
800
 
801
                mbox->xferaddr = (u32)dma_handle;
802
 
803
                /*issue old 0x04 command to adapter */
804
                mbox->cmd = MEGA_MBOXCMD_ADPEXTINQ;
805
 
806
                issue_scb_block(adapter, raw_mbox);
807
 
808
                /*
809
                 * update Enquiry3 and ProductInfo structures with
810
                 * mraid_inquiry structure
811
                 */
812
                mega_8_to_40ld(inq, inquiry3,
813
                                (mega_product_info *)&adapter->product_info);
814
 
815
                pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
816
                                ext_inq, dma_handle);
817
 
818
        } else {                /*adapter supports 40ld */
819
                adapter->flag |= BOARD_40LD;
820
 
821
                /*
822
                 * get product_info, which is static information and will be
823
                 * unchanged
824
                 */
825
                prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
826
                                &adapter->product_info,
827
                                sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
828
 
829
                mbox->xferaddr = prod_info_dma_handle;
830
 
831
                raw_mbox[0] = FC_NEW_CONFIG;     /* i.e. mbox->cmd=0xA1 */
832
                raw_mbox[2] = NC_SUBOP_PRODUCT_INFO;    /* i.e. 0x0E */
833
 
834
                if ((retval = issue_scb_block(adapter, raw_mbox)))
835
                        printk(KERN_WARNING
836
                        "megaraid: Product_info cmd failed with error: %d\n",
837
                                retval);
838
 
839
                pci_dma_sync_single(adapter->dev, prod_info_dma_handle,
840
                                sizeof(mega_product_info),
841
                                PCI_DMA_FROMDEVICE);
842
 
843
                pci_unmap_single(adapter->dev, prod_info_dma_handle,
844
                                sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
845
        }
846
 
847
 
848
        /*
849
         * kernel scans the channels from 0 to <= max_channel
850
         */
851
        adapter->host->max_channel =
852
                adapter->product_info.nchannels + NVIRT_CHAN -1;
853
 
854
        adapter->host->max_id = 16;     /* max targets per channel */
855
 
856
        adapter->host->max_lun = 7;     /* Upto 7 luns for non disk devices */
857
 
858
        adapter->host->cmd_per_lun = max_cmd_per_lun;
859
 
860
        adapter->numldrv = inquiry3->num_ldrv;
861
 
862
        adapter->max_cmds = adapter->product_info.max_commands;
863
 
864
        if(adapter->max_cmds > MAX_COMMANDS)
865
                adapter->max_cmds = MAX_COMMANDS;
866
 
867
        adapter->host->can_queue = adapter->max_cmds - 1;
868
 
869
        /*
870
         * Get the maximum number of scatter-gather elements supported by this
871
         * firmware
872
         */
873
        mega_get_max_sgl(adapter);
874
 
875
        adapter->host->sg_tablesize = adapter->sglen;
876
 
877
 
878
        /* use HP firmware and bios version encoding */
879
        if (adapter->product_info.subsysvid == HP_SUBSYS_VID) {
880
                sprintf (adapter->fw_version, "%c%d%d.%d%d",
881
                         adapter->product_info.fw_version[2],
882
                         adapter->product_info.fw_version[1] >> 8,
883
                         adapter->product_info.fw_version[1] & 0x0f,
884
                         adapter->product_info.fw_version[0] >> 8,
885
                         adapter->product_info.fw_version[0] & 0x0f);
886
                sprintf (adapter->bios_version, "%c%d%d.%d%d",
887
                         adapter->product_info.bios_version[2],
888
                         adapter->product_info.bios_version[1] >> 8,
889
                         adapter->product_info.bios_version[1] & 0x0f,
890
                         adapter->product_info.bios_version[0] >> 8,
891
                         adapter->product_info.bios_version[0] & 0x0f);
892
        } else {
893
                memcpy(adapter->fw_version,
894
                                (char *)adapter->product_info.fw_version, 4);
895
                adapter->fw_version[4] = 0;
896
 
897
                memcpy(adapter->bios_version,
898
                                (char *)adapter->product_info.bios_version, 4);
899
 
900
                adapter->bios_version[4] = 0;
901
        }
902
 
903
        printk(KERN_NOTICE "megaraid: [%s:%s] detected %d logical drives.\n",
904
                adapter->fw_version, adapter->bios_version, adapter->numldrv);
905
 
906
        /*
907
         * Do we support extended (>10 bytes) cdbs
908
         */
909
        adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
910
        if (adapter->support_ext_cdb)
911
                printk(KERN_NOTICE "megaraid: supports extended CDBs.\n");
912
 
913
 
914
        return 0;
915
}
916
 
917
 
918
/*
919
 * megaraid_queue()
920
 * @scmd - Issue this scsi command
921
 * @done - the callback hook into the scsi mid-layer
922
 *
923
 * The command queuing entry point for the mid-layer.
924
 */
925
static int
926
megaraid_queue(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
927
{
928
        adapter_t       *adapter;
929
        scb_t   *scb;
930
        int     busy=0;
931
 
932
        adapter = (adapter_t *)scmd->host->hostdata;
933
 
934
        scmd->scsi_done = done;
935
 
936
 
937
        /*
938
         * Allocate and build a SCB request
939
         * busy flag will be set if mega_build_cmd() command could not
940
         * allocate scb. We will return non-zero status in that case.
941
         * NOTE: scb can be null even though certain commands completed
942
         * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
943
         * return 0 in that case.
944
         */
945
 
946
        scb = mega_build_cmd(adapter, scmd, &busy);
947
 
948
        if(scb) {
949
                scb->state |= SCB_PENDQ;
950
                list_add_tail(&scb->list, &adapter->pending_list);
951
 
952
                /*
953
                 * Check if the HBA is in quiescent state, e.g., during a
954
                 * delete logical drive opertion. If it is, don't run
955
                 * the pending_list.
956
                 */
957
                if(atomic_read(&adapter->quiescent) == 0) {
958
                        mega_runpendq(adapter);
959
                }
960
                return 0;
961
        }
962
 
963
        return busy;
964
}
965
 
966
 
967
/**
968
 * mega_build_cmd()
969
 * @adapter - pointer to our soft state
970
 * @cmd - Prepare using this scsi command
971
 * @busy - busy flag if no resources
972
 *
973
 * Prepares a command and scatter gather list for the controller. This routine
974
 * also finds out if the commands is intended for a logical drive or a
975
 * physical device and prepares the controller command accordingly.
976
 *
977
 * We also re-order the logical drives and physical devices based on their
978
 * boot settings.
979
 */
980
static scb_t *
981
mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
982
{
983
        mega_ext_passthru       *epthru;
984
        mega_passthru   *pthru;
985
        scb_t   *scb;
986
        mbox_t  *mbox;
987
        long    seg;
988
        char    islogical;
989
        int     max_ldrv_num;
990
        int     channel = 0;
991
        int     target = 0;
992
        int     ldrv_num = 0;   /* logical drive number */
993
 
994
 
995
        /*
996
         * filter the internal and ioctl commands
997
         */
998
        if((cmd->cmnd[0] == MEGA_INTERNAL_CMD)) {
999
                return cmd->buffer;
1000
        }
1001
 
1002
 
1003
        /*
1004
         * We know what channels our logical drives are on - mega_find_card()
1005
         */
1006
        islogical = adapter->logdrv_chan[cmd->channel];
1007
 
1008
        /*
1009
         * The theory: If physical drive is chosen for boot, all the physical
1010
         * devices are exported before the logical drives, otherwise physical
1011
         * devices are pushed after logical drives, in which case - Kernel sees
1012
         * the physical devices on virtual channel which is obviously converted
1013
         * to actual channel on the HBA.
1014
         */
1015
        if( adapter->boot_pdrv_enabled ) {
1016
                if( islogical ) {
1017
                        /* logical channel */
1018
                        channel = cmd->channel -
1019
                                adapter->product_info.nchannels;
1020
                }
1021
                else {
1022
                        channel = cmd->channel; /* this is physical channel */
1023
                        target = cmd->target;
1024
 
1025
                        /*
1026
                         * boot from a physical disk, that disk needs to be
1027
                         * exposed first IF both the channels are SCSI, then
1028
                         * booting from the second channel is not allowed.
1029
                         */
1030
                        if( target == 0 ) {
1031
                                target = adapter->boot_pdrv_tgt;
1032
                        }
1033
                        else if( target == adapter->boot_pdrv_tgt ) {
1034
                                target = 0;
1035
                        }
1036
                }
1037
        }
1038
        else {
1039
                if( islogical ) {
1040
                        channel = cmd->channel; /* this is the logical channel
1041
                                                 */
1042
                }
1043
                else {
1044
                        channel = cmd->channel - NVIRT_CHAN;    /* physical
1045
                                                                   channel */
1046
                        target = cmd->target;
1047
                }
1048
        }
1049
 
1050
 
1051
        if(islogical) {
1052
 
1053
                /* have just LUN 0 for each target on virtual channels */
1054
                if (cmd->lun) {
1055
                        cmd->result = (DID_BAD_TARGET << 16);
1056
                        cmd->scsi_done(cmd);
1057
                        return NULL;
1058
                }
1059
 
1060
                ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
1061
 
1062
 
1063
                max_ldrv_num = (adapter->flag & BOARD_40LD) ?
1064
                        MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
1065
 
1066
                /*
1067
                 * max_ldrv_num increases by 0x80 if some logical drive was
1068
                 * deleted.
1069
                 */
1070
                if(adapter->read_ldidmap)
1071
                        max_ldrv_num += 0x80;
1072
 
1073
                if(ldrv_num > max_ldrv_num ) {
1074
                        cmd->result = (DID_BAD_TARGET << 16);
1075
                        cmd->scsi_done(cmd);
1076
                        return NULL;
1077
                }
1078
 
1079
        }
1080
        else {
1081
                if( cmd->lun > 7) {
1082
                        /*
1083
                         * Do not support lun >7 for physically accessed
1084
                         * devices
1085
                         */
1086
                        cmd->result = (DID_BAD_TARGET << 16);
1087
                        cmd->scsi_done(cmd);
1088
                        return NULL;
1089
                }
1090
        }
1091
 
1092
        /*
1093
         *
1094
         * Logical drive commands
1095
         *
1096
         */
1097
        if(islogical) {
1098
                switch (cmd->cmnd[0]) {
1099
                case TEST_UNIT_READY:
1100
                        memset(cmd->request_buffer, 0, cmd->request_bufflen);
1101
 
1102
#if MEGA_HAVE_CLUSTERING
1103
                        /*
1104
                         * Do we support clustering and is the support enabled
1105
                         * If no, return success always
1106
                         */
1107
                        if( !adapter->has_cluster ) {
1108
                                cmd->result = (DID_OK << 16);
1109
                                cmd->scsi_done(cmd);
1110
                                return NULL;
1111
                        }
1112
 
1113
                        if(!(scb = mega_allocate_scb(adapter, cmd))) {
1114
 
1115
                                cmd->result = (DID_ERROR << 16);
1116
                                cmd->scsi_done(cmd);
1117
                                *busy = 1;
1118
 
1119
                                return NULL;
1120
                        }
1121
 
1122
                        scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
1123
                        scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
1124
                        scb->raw_mbox[3] = ldrv_num;
1125
 
1126
                        scb->dma_direction = PCI_DMA_NONE;
1127
 
1128
                        return scb;
1129
#else
1130
                        cmd->result = (DID_OK << 16);
1131
                        cmd->scsi_done(cmd);
1132
                        return NULL;
1133
#endif
1134
 
1135
                case MODE_SENSE:
1136
                        memset(cmd->request_buffer, 0, cmd->cmnd[4]);
1137
                        cmd->result = (DID_OK << 16);
1138
                        cmd->scsi_done(cmd);
1139
                        return NULL;
1140
 
1141
                case READ_CAPACITY:
1142
                case INQUIRY:
1143
 
1144
                        if(!(adapter->flag & (1L << cmd->channel))) {
1145
 
1146
                                printk(KERN_NOTICE
1147
                                        "scsi%d: scanning scsi channel %d ",
1148
                                                adapter->host->host_no,
1149
                                                cmd->channel);
1150
                                printk("for logical drives.\n");
1151
 
1152
                                adapter->flag |= (1L << cmd->channel);
1153
                        }
1154
 
1155
                        /* Allocate a SCB and initialize passthru */
1156
                        if(!(scb = mega_allocate_scb(adapter, cmd))) {
1157
 
1158
                                cmd->result = (DID_ERROR << 16);
1159
                                cmd->scsi_done(cmd);
1160
                                *busy = 1;
1161
 
1162
                                return NULL;
1163
                        }
1164
                        pthru = scb->pthru;
1165
 
1166
                        mbox = (mbox_t *)scb->raw_mbox;
1167
                        memset(mbox, 0, sizeof(scb->raw_mbox));
1168
                        memset(pthru, 0, sizeof(mega_passthru));
1169
 
1170
                        pthru->timeout = 0;
1171
                        pthru->ars = 1;
1172
                        pthru->reqsenselen = 14;
1173
                        pthru->islogical = 1;
1174
                        pthru->logdrv = ldrv_num;
1175
                        pthru->cdblen = cmd->cmd_len;
1176
                        memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
1177
 
1178
                        if( adapter->has_64bit_addr ) {
1179
                                mbox->cmd = MEGA_MBOXCMD_PASSTHRU64;
1180
                        }
1181
                        else {
1182
                                mbox->cmd = MEGA_MBOXCMD_PASSTHRU;
1183
                        }
1184
 
1185
                        scb->dma_direction = PCI_DMA_FROMDEVICE;
1186
 
1187
                        pthru->numsgelements = mega_build_sglist(adapter, scb,
1188
                                &pthru->dataxferaddr, &pthru->dataxferlen);
1189
 
1190
                        mbox->xferaddr = scb->pthru_dma_addr;
1191
 
1192
                        return scb;
1193
 
1194
                case READ_6:
1195
                case WRITE_6:
1196
                case READ_10:
1197
                case WRITE_10:
1198
                case READ_12:
1199
                case WRITE_12:
1200
 
1201
                        /* Allocate a SCB and initialize mailbox */
1202
                        if(!(scb = mega_allocate_scb(adapter, cmd))) {
1203
 
1204
                                cmd->result = (DID_ERROR << 16);
1205
                                cmd->scsi_done(cmd);
1206
                                *busy = 1;
1207
 
1208
                                return NULL;
1209
                        }
1210
                        mbox = (mbox_t *)scb->raw_mbox;
1211
 
1212
                        memset(mbox, 0, sizeof(scb->raw_mbox));
1213
                        mbox->logdrv = ldrv_num;
1214
 
1215
                        /*
1216
                         * A little hack: 2nd bit is zero for all scsi read
1217
                         * commands and is set for all scsi write commands
1218
                         */
1219
                        if( adapter->has_64bit_addr ) {
1220
                                mbox->cmd = (*cmd->cmnd & 0x02) ?
1221
                                        MEGA_MBOXCMD_LWRITE64:
1222
                                        MEGA_MBOXCMD_LREAD64 ;
1223
                        }
1224
                        else {
1225
                                mbox->cmd = (*cmd->cmnd & 0x02) ?
1226
                                        MEGA_MBOXCMD_LWRITE:
1227
                                        MEGA_MBOXCMD_LREAD ;
1228
                        }
1229
 
1230
                        /*
1231
                         * 6-byte READ(0x08) or WRITE(0x0A) cdb
1232
                         */
1233
                        if( cmd->cmd_len == 6 ) {
1234
                                mbox->numsectors = (u32) cmd->cmnd[4];
1235
                                mbox->lba =
1236
                                        ((u32)cmd->cmnd[1] << 16) |
1237
                                        ((u32)cmd->cmnd[2] << 8) |
1238
                                        (u32)cmd->cmnd[3];
1239
 
1240
                                mbox->lba &= 0x1FFFFF;
1241
 
1242
#if MEGA_HAVE_STATS
1243
                                /*
1244
                                 * Take modulo 0x80, since the logical drive
1245
                                 * number increases by 0x80 when a logical
1246
                                 * drive was deleted
1247
                                 */
1248
                                if (*cmd->cmnd == READ_6) {
1249
                                        adapter->nreads[ldrv_num%0x80]++;
1250
                                        adapter->nreadblocks[ldrv_num%0x80] +=
1251
                                                mbox->numsectors;
1252
                                } else {
1253
                                        adapter->nwrites[ldrv_num%0x80]++;
1254
                                        adapter->nwriteblocks[ldrv_num%0x80] +=
1255
                                                mbox->numsectors;
1256
                                }
1257
#endif
1258
                        }
1259
 
1260
                        /*
1261
                         * 10-byte READ(0x28) or WRITE(0x2A) cdb
1262
                         */
1263
                        if( cmd->cmd_len == 10 ) {
1264
                                mbox->numsectors =
1265
                                        (u32)cmd->cmnd[8] |
1266
                                        ((u32)cmd->cmnd[7] << 8);
1267
                                mbox->lba =
1268
                                        ((u32)cmd->cmnd[2] << 24) |
1269
                                        ((u32)cmd->cmnd[3] << 16) |
1270
                                        ((u32)cmd->cmnd[4] << 8) |
1271
                                        (u32)cmd->cmnd[5];
1272
 
1273
#if MEGA_HAVE_STATS
1274
                                if (*cmd->cmnd == READ_10) {
1275
                                        adapter->nreads[ldrv_num%0x80]++;
1276
                                        adapter->nreadblocks[ldrv_num%0x80] +=
1277
                                                mbox->numsectors;
1278
                                } else {
1279
                                        adapter->nwrites[ldrv_num%0x80]++;
1280
                                        adapter->nwriteblocks[ldrv_num%0x80] +=
1281
                                                mbox->numsectors;
1282
                                }
1283
#endif
1284
                        }
1285
 
1286
                        /*
1287
                         * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1288
                         */
1289
                        if( cmd->cmd_len == 12 ) {
1290
                                mbox->lba =
1291
                                        ((u32)cmd->cmnd[2] << 24) |
1292
                                        ((u32)cmd->cmnd[3] << 16) |
1293
                                        ((u32)cmd->cmnd[4] << 8) |
1294
                                        (u32)cmd->cmnd[5];
1295
 
1296
                                mbox->numsectors =
1297
                                        ((u32)cmd->cmnd[6] << 24) |
1298
                                        ((u32)cmd->cmnd[7] << 16) |
1299
                                        ((u32)cmd->cmnd[8] << 8) |
1300
                                        (u32)cmd->cmnd[9];
1301
 
1302
#if MEGA_HAVE_STATS
1303
                                if (*cmd->cmnd == READ_12) {
1304
                                        adapter->nreads[ldrv_num%0x80]++;
1305
                                        adapter->nreadblocks[ldrv_num%0x80] +=
1306
                                                mbox->numsectors;
1307
                                } else {
1308
                                        adapter->nwrites[ldrv_num%0x80]++;
1309
                                        adapter->nwriteblocks[ldrv_num%0x80] +=
1310
                                                mbox->numsectors;
1311
                                }
1312
#endif
1313
                        }
1314
 
1315
                        /*
1316
                         * If it is a read command
1317
                         */
1318
                        if( (*cmd->cmnd & 0x0F) == 0x08 ) {
1319
                                scb->dma_direction = PCI_DMA_FROMDEVICE;
1320
                        }
1321
                        else {
1322
                                scb->dma_direction = PCI_DMA_TODEVICE;
1323
                        }
1324
 
1325
                        /* Calculate Scatter-Gather info */
1326
                        mbox->numsgelements = mega_build_sglist(adapter, scb,
1327
                                        (u32 *)&mbox->xferaddr, (u32 *)&seg);
1328
 
1329
                        return scb;
1330
 
1331
#if MEGA_HAVE_CLUSTERING
1332
                case RESERVE:   /* Fall through */
1333
                case RELEASE:
1334
 
1335
                        /*
1336
                         * Do we support clustering and is the support enabled
1337
                         */
1338
                        if( ! adapter->has_cluster ) {
1339
 
1340
                                cmd->result = (DID_BAD_TARGET << 16);
1341
                                cmd->scsi_done(cmd);
1342
                                return NULL;
1343
                        }
1344
 
1345
                        /* Allocate a SCB and initialize mailbox */
1346
                        if(!(scb = mega_allocate_scb(adapter, cmd))) {
1347
 
1348
                                cmd->result = (DID_ERROR << 16);
1349
                                cmd->scsi_done(cmd);
1350
                                *busy = 1;
1351
 
1352
                                return NULL;
1353
                        }
1354
 
1355
                        scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
1356
                        scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
1357
                                MEGA_RESERVE_LD : MEGA_RELEASE_LD;
1358
 
1359
                        scb->raw_mbox[3] = ldrv_num;
1360
 
1361
                        scb->dma_direction = PCI_DMA_NONE;
1362
 
1363
                        return scb;
1364
#endif
1365
 
1366
                default:
1367
                        cmd->result = (DID_BAD_TARGET << 16);
1368
                        cmd->scsi_done(cmd);
1369
                        return NULL;
1370
                }
1371
        }
1372
 
1373
        /*
1374
         * Passthru drive commands
1375
         */
1376
        else {
1377
                /* Allocate a SCB and initialize passthru */
1378
                if(!(scb = mega_allocate_scb(adapter, cmd))) {
1379
 
1380
                        cmd->result = (DID_ERROR << 16);
1381
                        cmd->scsi_done(cmd);
1382
                        *busy = 1;
1383
 
1384
                        return NULL;
1385
                }
1386
 
1387
                mbox = (mbox_t *)scb->raw_mbox;
1388
                memset(mbox, 0, sizeof(scb->raw_mbox));
1389
 
1390
                if( adapter->support_ext_cdb ) {
1391
 
1392
                        epthru = mega_prepare_extpassthru(adapter, scb, cmd,
1393
                                        channel, target);
1394
 
1395
                        mbox->cmd = MEGA_MBOXCMD_EXTPTHRU;
1396
 
1397
                        mbox->xferaddr = scb->epthru_dma_addr;
1398
 
1399
                }
1400
                else {
1401
 
1402
                        pthru = mega_prepare_passthru(adapter, scb, cmd,
1403
                                        channel, target);
1404
 
1405
                        /* Initialize mailbox */
1406
                        if( adapter->has_64bit_addr ) {
1407
                                mbox->cmd = MEGA_MBOXCMD_PASSTHRU64;
1408
                        }
1409
                        else {
1410
                                mbox->cmd = MEGA_MBOXCMD_PASSTHRU;
1411
                        }
1412
 
1413
                        mbox->xferaddr = scb->pthru_dma_addr;
1414
 
1415
                }
1416
                return scb;
1417
        }
1418
        return NULL;
1419
}
1420
 
1421
 
1422
/**
1423
 * mega_prepare_passthru()
1424
 * @adapter - pointer to our soft state
1425
 * @scb - our scsi control block
1426
 * @cmd - scsi command from the mid-layer
1427
 * @channel - actual channel on the controller
1428
 * @target - actual id on the controller.
1429
 *
1430
 * prepare a command for the scsi physical devices.
1431
 */
1432
static mega_passthru *
1433
mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
1434
                int channel, int target)
1435
{
1436
        mega_passthru *pthru;
1437
 
1438
        pthru = scb->pthru;
1439
        memset(pthru, 0, sizeof (mega_passthru));
1440
 
1441
        /* 0=6sec/1=60sec/2=10min/3=3hrs */
1442
        pthru->timeout = 2;
1443
 
1444
        pthru->ars = 1;
1445
        pthru->reqsenselen = 14;
1446
        pthru->islogical = 0;
1447
 
1448
        pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
1449
 
1450
        pthru->target = (adapter->flag & BOARD_40LD) ?
1451
                (channel << 4) | target : target;
1452
 
1453
        pthru->cdblen = cmd->cmd_len;
1454
        pthru->logdrv = cmd->lun;
1455
 
1456
        memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
1457
 
1458
        /* Not sure about the direction */
1459
        scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
1460
 
1461
        /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
1462
        switch (cmd->cmnd[0]) {
1463
        case INQUIRY:
1464
        case READ_CAPACITY:
1465
                if(!(adapter->flag & (1L << cmd->channel))) {
1466
 
1467
                        printk(KERN_NOTICE
1468
                                "scsi%d: scanning scsi channel %d [P%d] ",
1469
                                        adapter->host->host_no,
1470
                                        cmd->channel, channel);
1471
                        printk("for physical devices.\n");
1472
 
1473
                        adapter->flag |= (1L << cmd->channel);
1474
                }
1475
                /* Fall through */
1476
        default:
1477
                pthru->numsgelements = mega_build_sglist(adapter, scb,
1478
                                &pthru->dataxferaddr, &pthru->dataxferlen);
1479
                break;
1480
        }
1481
        return pthru;
1482
}
1483
 
1484
 
1485
/**
1486
 * mega_prepare_extpassthru()
1487
 * @adapter - pointer to our soft state
1488
 * @scb - our scsi control block
1489
 * @cmd - scsi command from the mid-layer
1490
 * @channel - actual channel on the controller
1491
 * @target - actual id on the controller.
1492
 *
1493
 * prepare a command for the scsi physical devices. This rountine prepares
1494
 * commands for devices which can take extended CDBs (>10 bytes)
1495
 */
1496
static mega_ext_passthru *
1497
mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
1498
                int channel, int target)
1499
{
1500
        mega_ext_passthru       *epthru;
1501
 
1502
        epthru = scb->epthru;
1503
        memset(epthru, 0, sizeof(mega_ext_passthru));
1504
 
1505
        /* 0=6sec/1=60sec/2=10min/3=3hrs */
1506
        epthru->timeout = 2;
1507
 
1508
        epthru->ars = 1;
1509
        epthru->reqsenselen = 14;
1510
        epthru->islogical = 0;
1511
 
1512
        epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
1513
        epthru->target = (adapter->flag & BOARD_40LD) ?
1514
                (channel << 4) | target : target;
1515
 
1516
        epthru->cdblen = cmd->cmd_len;
1517
        epthru->logdrv = cmd->lun;
1518
 
1519
        memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
1520
 
1521
        /* Not sure about the direction */
1522
        scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
1523
 
1524
        switch(cmd->cmnd[0]) {
1525
        case INQUIRY:
1526
        case READ_CAPACITY:
1527
                if(!(adapter->flag & (1L << cmd->channel))) {
1528
 
1529
                        printk(KERN_NOTICE
1530
                                "scsi%d: scanning scsi channel %d [P%d] ",
1531
                                        adapter->host->host_no,
1532
                                        cmd->channel, channel);
1533
                        printk("for physical devices.\n");
1534
 
1535
                        adapter->flag |= (1L << cmd->channel);
1536
                }
1537
                /* Fall through */
1538
        default:
1539
                epthru->numsgelements = mega_build_sglist(adapter, scb,
1540
                                &epthru->dataxferaddr, &epthru->dataxferlen);
1541
                break;
1542
        }
1543
 
1544
        return epthru;
1545
}
1546
 
1547
 
1548
/**
1549
 * mega_allocate_scb()
1550
 * @adapter - pointer to our soft state
1551
 * @cmd - scsi command from the mid-layer
1552
 *
1553
 * Allocate a SCB structure. This is the central structure for controller
1554
 * commands.
1555
 */
1556
static inline scb_t *
1557
mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
1558
{
1559
        struct list_head *head = &adapter->free_list;
1560
        scb_t   *scb;
1561
 
1562
        /* Unlink command from Free List */
1563
        if( !list_empty(head) ) {
1564
 
1565
                scb = list_entry(head->next, scb_t, list);
1566
 
1567
                list_del_init(head->next);
1568
 
1569
                scb->state = SCB_ACTIVE;
1570
                scb->cmd = cmd;
1571
                scb->dma_type = MEGA_DMA_TYPE_NONE;
1572
 
1573
                return scb;
1574
        }
1575
 
1576
        return NULL;
1577
}
1578
 
1579
 
1580
/**
1581
 * mega_runpendq()
1582
 * @adapter - pointer to our soft state
1583
 *
1584
 * Runs through the list of pending requests.
1585
 */
1586
static inline void
1587
mega_runpendq(adapter_t *adapter)
1588
{
1589
        if(!list_empty(&adapter->pending_list))
1590
                __mega_runpendq(adapter);
1591
}
1592
 
1593
static void
1594
__mega_runpendq(adapter_t *adapter)
1595
{
1596
        scb_t *scb;
1597
        struct list_head *pos, *next;
1598
 
1599
        /* Issue any pending commands to the card */
1600
        list_for_each_safe(pos, next, &adapter->pending_list) {
1601
 
1602
                scb = list_entry(pos, scb_t, list);
1603
 
1604
                if( !(scb->state & SCB_ISSUED) ) {
1605
 
1606
                        if( issue_scb(adapter, scb) != 0 )
1607
                                return;
1608
                }
1609
        }
1610
 
1611
        return;
1612
}
1613
 
1614
 
1615
/**
1616
 * issue_scb()
1617
 * @adapter - pointer to our soft state
1618
 * @scb - scsi control block
1619
 *
1620
 * Post a command to the card if the mailbox is available, otherwise return
1621
 * busy. We also take the scb from the pending list if the mailbox is
1622
 * available.
1623
 */
1624
static inline int
1625
issue_scb(adapter_t *adapter, scb_t *scb)
1626
{
1627
        volatile mbox64_t       *mbox64 = adapter->mbox64;
1628
        volatile mbox_t         *mbox = adapter->mbox;
1629
        unsigned int    i = 0;
1630
 
1631
        if(unlikely(mbox->busy)) {
1632
                do {
1633
                        udelay(1);
1634
                        i++;
1635
                } while( mbox->busy && (i < max_mbox_busy_wait) );
1636
 
1637
                if(mbox->busy) return -1;
1638
        }
1639
 
1640
        /* Copy mailbox data into host structure */
1641
        memcpy((char *)mbox, (char *)scb->raw_mbox, 16);
1642
 
1643
        mbox->cmdid = scb->idx; /* Set cmdid */
1644
        mbox->busy = 1;         /* Set busy */
1645
 
1646
 
1647
        /*
1648
         * Increment the pending queue counter
1649
         */
1650
        atomic_inc(&adapter->pend_cmds);
1651
 
1652
        switch (mbox->cmd) {
1653
        case MEGA_MBOXCMD_EXTPTHRU:
1654
                if( !adapter->has_64bit_addr ) break;
1655
                // else fall through
1656
        case MEGA_MBOXCMD_LREAD64:
1657
        case MEGA_MBOXCMD_LWRITE64:
1658
        case MEGA_MBOXCMD_PASSTHRU64:
1659
                mbox64->xfer_segment_lo = mbox->xferaddr;
1660
                mbox64->xfer_segment_hi = 0;
1661
                mbox->xferaddr = 0xFFFFFFFF;
1662
                break;
1663
        default:
1664
                mbox64->xfer_segment_lo = 0;
1665
                mbox64->xfer_segment_hi = 0;
1666
        }
1667
 
1668
        /*
1669
         * post the command
1670
         */
1671
        scb->state |= SCB_ISSUED;
1672
 
1673
        if( likely(adapter->flag & BOARD_MEMMAP) ) {
1674
                mbox->poll = 0;
1675
                mbox->ack = 0;
1676
                WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1677
        }
1678
        else {
1679
                irq_enable(adapter);
1680
                issue_command(adapter);
1681
        }
1682
 
1683
        return 0;
1684
}
1685
 
1686
 
1687
/**
1688
 * issue_scb_block()
1689
 * @adapter - pointer to our soft state
1690
 * @raw_mbox - the mailbox
1691
 *
1692
 * Issue a scb in synchronous and non-interrupt mode
1693
 */
1694
static int
1695
issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
1696
{
1697
        volatile mbox64_t *mbox64 = adapter->mbox64;
1698
        volatile mbox_t *mbox = adapter->mbox;
1699
        u8      byte;
1700
        u8      status;
1701
        int     i;
1702
 
1703
        /* Wait until mailbox is free */
1704
        if(mega_busywait_mbox (adapter))
1705
                goto bug_blocked_mailbox;
1706
 
1707
        /* Copy mailbox data into host structure */
1708
        memcpy((char *)mbox, raw_mbox, 16);
1709
        mbox->cmdid = 0xFE;
1710
        mbox->busy = 1;
1711
 
1712
        switch (raw_mbox[0]) {
1713
        case MEGA_MBOXCMD_EXTPTHRU:
1714
                if( !adapter->has_64bit_addr ) break;
1715
                // else fall through
1716
        case MEGA_MBOXCMD_LREAD64:
1717
        case MEGA_MBOXCMD_LWRITE64:
1718
        case MEGA_MBOXCMD_PASSTHRU64:
1719
                mbox64->xfer_segment_lo = mbox->xferaddr;
1720
                mbox64->xfer_segment_hi = 0;
1721
                mbox->xferaddr = 0xFFFFFFFF;
1722
                break;
1723
        default:
1724
                mbox64->xfer_segment_lo = 0;
1725
                mbox64->xfer_segment_hi = 0;
1726
        }
1727
 
1728
        if( likely(adapter->flag & BOARD_MEMMAP) ) {
1729
                mbox->poll = 0;
1730
                mbox->ack = 0;
1731
                mbox->numstatus = 0xFF;
1732
                mbox->status = 0xFF;
1733
                WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1734
 
1735
                while((volatile u8)mbox->numstatus == 0xFF)
1736
                        cpu_relax();
1737
 
1738
                mbox->numstatus = 0xFF;
1739
 
1740
                while((volatile u8)mbox->status == 0xFF)
1741
                        cpu_relax();
1742
 
1743
                status = mbox->status;
1744
                mbox->status = 0xFF;
1745
 
1746
                while( (volatile u8)mbox->poll != 0x77 )
1747
                        cpu_relax();
1748
 
1749
                mbox->poll = 0;
1750
                mbox->ack = 0x77;
1751
 
1752
                WRINDOOR(adapter, adapter->mbox_dma | 0x2);
1753
 
1754
                while(RDINDOOR(adapter) & 0x2)
1755
                        cpu_relax();
1756
        }
1757
        else {
1758
                irq_disable(adapter);
1759
                issue_command(adapter);
1760
 
1761
                while (!((byte = irq_state(adapter)) & INTR_VALID))
1762
                        cpu_relax();
1763
 
1764
                status = mbox->status;
1765
                mbox->numstatus = 0xFF;
1766
                mbox->status = 0xFF;
1767
 
1768
                set_irq_state(adapter, byte);
1769
                irq_enable(adapter);
1770
                irq_ack(adapter);
1771
        }
1772
 
1773
        // invalidate the completed command id array. After command
1774
        // completion, firmware would write the valid id.
1775
        for (i = 0; i < MAX_FIRMWARE_STATUS; i++) {
1776
                mbox->completed[i] = 0xFF;
1777
        }
1778
 
1779
        return status;
1780
 
1781
bug_blocked_mailbox:
1782
        printk(KERN_WARNING "megaraid: Blocked mailbox......!!\n");
1783
        udelay (1000);
1784
        return -1;
1785
}
1786
 
1787
 
1788
/**
1789
 * megaraid_isr_iomapped()
1790
 * @irq - irq
1791
 * @devp - pointer to our soft state
1792
 * @regs - unused
1793
 *
1794
 * Interrupt service routine for io-mapped controllers.
1795
 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1796
 * and service the completed commands.
1797
 */
1798
static void
1799
megaraid_isr_iomapped(int irq, void *devp, struct pt_regs *regs)
1800
{
1801
        adapter_t       *adapter = devp;
1802
        unsigned long   flags;
1803
 
1804
 
1805
        spin_lock_irqsave(adapter->host_lock, flags);
1806
 
1807
        megaraid_iombox_ack_sequence(adapter);
1808
 
1809
        /* Loop through any pending requests */
1810
        if( atomic_read(&adapter->quiescent ) == 0) {
1811
                mega_runpendq(adapter);
1812
        }
1813
 
1814
        spin_unlock_irqrestore(adapter->host_lock, flags);
1815
 
1816
        return;
1817
}
1818
 
1819
 
1820
/**
1821
 * megaraid_iombox_ack_sequence - interrupt ack sequence for IO mapped HBAs
1822
 * @adapter     - controller's soft state
1823
 *
1824
 * Interrupt ackrowledgement sequence for IO mapped HBAs
1825
 */
1826
static inline void
1827
megaraid_iombox_ack_sequence(adapter_t *adapter)
1828
{
1829
        u8      status;
1830
        u8      nstatus;
1831
        u8      completed[MAX_FIRMWARE_STATUS];
1832
        u8      byte;
1833
        int     i;
1834
 
1835
 
1836
        /*
1837
         * loop till F/W has more commands for us to complete.
1838
         */
1839
        do {
1840
                /* Check if a valid interrupt is pending */
1841
                byte = irq_state(adapter);
1842
                if( (byte & VALID_INTR_BYTE) == 0 ) {
1843
                        return;
1844
                }
1845
                set_irq_state(adapter, byte);
1846
 
1847
                while ((nstatus = adapter->mbox->numstatus) == 0xFF) {
1848
                        cpu_relax();
1849
                }
1850
                adapter->mbox->numstatus = 0xFF;
1851
 
1852
                for (i = 0; i < nstatus; i++) {
1853
                        while ((completed[i] = adapter->mbox->completed[i])
1854
                                        == 0xFF) {
1855
                                cpu_relax();
1856
                        }
1857
 
1858
                        adapter->mbox->completed[i] = 0xFF;
1859
                }
1860
 
1861
                // we must read the valid status now
1862
                if ((status = adapter->mbox->status) == 0xFF) {
1863
                        printk(KERN_WARNING
1864
                        "megaraid critical: status 0xFF from firmware.\n");
1865
                }
1866
                adapter->mbox->status = 0xFF;
1867
 
1868
                /*
1869
                 * decrement the pending queue counter
1870
                 */
1871
                atomic_sub(nstatus, &adapter->pend_cmds);
1872
 
1873
                /* Acknowledge interrupt */
1874
                irq_ack(adapter);
1875
 
1876
                mega_cmd_done(adapter, completed, nstatus, status);
1877
 
1878
        } while(1);
1879
}
1880
 
1881
 
1882
/**
1883
 * megaraid_isr_memmapped()
1884
 * @irq - irq
1885
 * @devp - pointer to our soft state
1886
 * @regs - unused
1887
 *
1888
 * Interrupt service routine for memory-mapped controllers.
1889
 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1890
 * and service the completed commands.
1891
 */
1892
static void
1893
megaraid_isr_memmapped(int irq, void *devp, struct pt_regs *regs)
1894
{
1895
        adapter_t       *adapter = devp;
1896
        unsigned long   flags;
1897
 
1898
 
1899
        spin_lock_irqsave(adapter->host_lock, flags);
1900
 
1901
        megaraid_memmbox_ack_sequence(adapter);
1902
 
1903
        /* Loop through any pending requests */
1904
        if(atomic_read(&adapter->quiescent) == 0) {
1905
                mega_runpendq(adapter);
1906
        }
1907
 
1908
        spin_unlock_irqrestore(adapter->host_lock, flags);
1909
 
1910
        return;
1911
}
1912
 
1913
 
1914
/**
1915
 * megaraid_memmbox_ack_sequence - interrupt ack sequence for memory mapped HBAs
1916
 * @adapter     - controller's soft state
1917
 *
1918
 * Interrupt ackrowledgement sequence for memory mapped HBAs
1919
 */
1920
static inline void
1921
megaraid_memmbox_ack_sequence(adapter_t *adapter)
1922
{
1923
        u8      status;
1924
        u32     dword = 0;
1925
        u8      nstatus;
1926
        u8      completed[MAX_FIRMWARE_STATUS];
1927
        int     i;
1928
 
1929
 
1930
        /*
1931
         * loop till F/W has more commands for us to complete.
1932
         */
1933
        do {
1934
                /* Check if a valid interrupt is pending */
1935
                dword = RDOUTDOOR(adapter);
1936
                if( dword != 0x10001234 ) {
1937
                        /*
1938
                         * No more pending commands
1939
                         */
1940
                        return;
1941
                }
1942
                WROUTDOOR(adapter, 0x10001234);
1943
 
1944
                while ((nstatus = adapter->mbox->numstatus) == 0xFF) {
1945
                        cpu_relax();
1946
                }
1947
                adapter->mbox->numstatus = 0xFF;
1948
 
1949
                for (i = 0; i < nstatus; i++ ) {
1950
                        while ((completed[i] = adapter->mbox->completed[i])
1951
                                        == 0xFF) {
1952
                                cpu_relax();
1953
                        }
1954
 
1955
                        adapter->mbox->completed[i] = 0xFF;
1956
                }
1957
 
1958
                // we must read the valid status now
1959
                if ((status = adapter->mbox->status) == 0xFF) {
1960
                        printk(KERN_WARNING
1961
                        "megaraid critical: status 0xFF from firmware.\n");
1962
                }
1963
                adapter->mbox->status = 0xFF;
1964
 
1965
                /*
1966
                 * decrement the pending queue counter
1967
                 */
1968
                atomic_sub(nstatus, &adapter->pend_cmds);
1969
 
1970
                /* Acknowledge interrupt */
1971
                WRINDOOR(adapter, 0x2);
1972
 
1973
                while( RDINDOOR(adapter) & 0x02 ) cpu_relax();
1974
 
1975
                mega_cmd_done(adapter, completed, nstatus, status);
1976
 
1977
        } while(1);
1978
}
1979
 
1980
 
1981
/**
1982
 * mega_cmd_done()
1983
 * @adapter - pointer to our soft state
1984
 * @completed - array of ids of completed commands
1985
 * @nstatus - number of completed commands
1986
 * @status - status of the last command completed
1987
 *
1988
 * Complete the comamnds and call the scsi mid-layer callback hooks.
1989
 */
1990
static inline void
1991
mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
1992
{
1993
        mega_ext_passthru       *epthru = NULL;
1994
        struct scatterlist      *sgl;
1995
        Scsi_Cmnd       *cmd = NULL;
1996
        mega_passthru   *pthru = NULL;
1997
        mbox_t  *mbox = NULL;
1998
        int     islogical;
1999
        u8      c;
2000
        scb_t   *scb;
2001
        int     cmdid;
2002
        int     i;
2003
 
2004
        /*
2005
         * for all the commands completed, call the mid-layer callback routine
2006
         * and free the scb.
2007
         */
2008
        for( i = 0; i < nstatus; i++ ) {
2009
 
2010
                cmdid = completed[i];
2011
 
2012
                if( cmdid == CMDID_INT_CMDS ) { /* internal command */
2013
                        scb = &adapter->int_scb;
2014
                        cmd = scb->cmd;
2015
                        mbox = (mbox_t *)scb->raw_mbox;
2016
 
2017
                        /*
2018
                         * Internal command interface do not fire the extended
2019
                         * passthru or 64-bit passthru
2020
                         */
2021
                        pthru = scb->pthru;
2022
 
2023
                }
2024
                else {
2025
                        scb = &adapter->scb_list[cmdid];
2026
                        cmd = scb->cmd;
2027
                        pthru = scb->pthru;
2028
                        epthru = scb->epthru;
2029
                        mbox = (mbox_t *)scb->raw_mbox;
2030
 
2031
                        /*
2032
                         * Make sure f/w has completed a valid command
2033
                         */
2034
                        if( !(scb->state & SCB_ISSUED) || scb->cmd == NULL ) {
2035
                                printk(KERN_CRIT
2036
                                        "megaraid: invalid command ");
2037
                                printk("Id %d, scb->state:%x, scsi cmd:%p\n",
2038
                                        cmdid, scb->state, scb->cmd);
2039
 
2040
                                continue;
2041
                        }
2042
 
2043
                        /*
2044
                         * Was an abort issued for this command
2045
                         */
2046
                        if( scb->state & SCB_ABORT ) {
2047
 
2048
                                printk(KERN_NOTICE
2049
                                "megaraid: aborted cmd %lx[%x] complete.\n",
2050
                                        scb->cmd->serial_number, scb->idx);
2051
 
2052
                                cmd->result = (DID_ABORT << 16);
2053
 
2054
                                mega_free_scb(adapter, scb);
2055
 
2056
                                cmd->scsi_done(cmd);
2057
 
2058
                                continue;
2059
                        }
2060
 
2061
                        /*
2062
                         * Was a reset issued for this command
2063
                         */
2064
                        if( scb->state & SCB_RESET ) {
2065
 
2066
                                printk(KERN_WARNING
2067
                                "megaraid: reset cmd %lx[%x] complete.\n",
2068
                                        scb->cmd->serial_number, scb->idx);
2069
 
2070
                                scb->cmd->result = (DID_RESET << 16);
2071
 
2072
                                mega_free_scb (adapter, scb);
2073
 
2074
                                cmd->scsi_done(cmd);
2075
 
2076
                                continue;
2077
                        }
2078
 
2079
#if MEGA_HAVE_STATS
2080
                        {
2081
 
2082
                        int     logdrv = mbox->logdrv;
2083
 
2084
                        islogical = adapter->logdrv_chan[cmd->channel];
2085
 
2086
                        /*
2087
                         * Maintain an error counter for the logical drive.
2088
                         * Some application like SNMP agent need such
2089
                         * statistics
2090
                         */
2091
                        if( status && islogical && (cmd->cmnd[0] == READ_6 ||
2092
                                                cmd->cmnd[0] == READ_10 ||
2093
                                                cmd->cmnd[0] == READ_12)) {
2094
                                /*
2095
                                 * Logical drive number increases by 0x80 when
2096
                                 * a logical drive is deleted
2097
                                 */
2098
                                adapter->rd_errors[logdrv%0x80]++;
2099
                        }
2100
 
2101
                        if( status && islogical && (cmd->cmnd[0] == WRITE_6 ||
2102
                                                cmd->cmnd[0] == WRITE_10 ||
2103
                                                cmd->cmnd[0] == WRITE_12)) {
2104
                                /*
2105
                                 * Logical drive number increases by 0x80 when
2106
                                 * a logical drive is deleted
2107
                                 */
2108
                                adapter->wr_errors[logdrv%0x80]++;
2109
                        }
2110
 
2111
                        }
2112
#endif
2113
                }
2114
 
2115
                /*
2116
                 * Do not return the presence of hard disk on the channel so,
2117
                 * inquiry sent, and returned data==hard disk or removable
2118
                 * hard disk and not logical, request should return failure! -
2119
                 * PJ
2120
                 */
2121
                islogical = adapter->logdrv_chan[cmd->channel];
2122
                if (cmd->cmnd[0] == INQUIRY && !islogical) {
2123
 
2124
                        if( cmd->use_sg ) {
2125
                                sgl = (struct scatterlist *)
2126
                                        cmd->request_buffer;
2127
                                c = *(u8 *)sgl[0].address;
2128
                        }
2129
                        else {
2130
                                c = *(u8 *)cmd->request_buffer;
2131
                        }
2132
 
2133
                        if(IS_RAID_CH(adapter, cmd->channel) &&
2134
                                        ((c & 0x1F ) == TYPE_DISK)) {
2135
                                status = 0xF0;
2136
                        }
2137
                }
2138
 
2139
                /* clear result; otherwise, success returns corrupt value */
2140
                cmd->result = 0;
2141
 
2142
                /* Convert MegaRAID status to Linux error code */
2143
                switch (status) {
2144
                case 0x00:      /* SUCCESS , i.e. SCSI_STATUS_GOOD */
2145
                        cmd->result |= (DID_OK << 16);
2146
                        break;
2147
 
2148
                case 0x02:      /* ERROR_ABORTED, i.e.
2149
                                   SCSI_STATUS_CHECK_CONDITION */
2150
 
2151
                        /* set sense_buffer and result fields */
2152
                        if( mbox->cmd == MEGA_MBOXCMD_PASSTHRU ||
2153
                                mbox->cmd == MEGA_MBOXCMD_PASSTHRU64 ) {
2154
 
2155
                                memcpy(cmd->sense_buffer, pthru->reqsensearea,
2156
                                                14);
2157
 
2158
                                cmd->result = (DRIVER_SENSE << 24) |
2159
                                        (DID_OK << 16) |
2160
                                        (CHECK_CONDITION << 1);
2161
                        }
2162
                        else {
2163
                                if (mbox->cmd == MEGA_MBOXCMD_EXTPTHRU) {
2164
 
2165
                                        memcpy(cmd->sense_buffer,
2166
                                                epthru->reqsensearea, 14);
2167
 
2168
                                        cmd->result = (DRIVER_SENSE << 24) |
2169
                                                (DID_OK << 16) |
2170
                                                (CHECK_CONDITION << 1);
2171
                                } else {
2172
                                        cmd->sense_buffer[0] = 0x70;
2173
                                        cmd->sense_buffer[2] = ABORTED_COMMAND;
2174
                                        cmd->result |= (CHECK_CONDITION << 1);
2175
                                }
2176
                        }
2177
                        break;
2178
 
2179
                case 0x08:      /* ERR_DEST_DRIVE_FAILED, i.e.
2180
                                   SCSI_STATUS_BUSY */
2181
                        cmd->result |= (DID_BUS_BUSY << 16) | status;
2182
                        break;
2183
 
2184
                default:
2185
#if MEGA_HAVE_CLUSTERING
2186
                        /*
2187
                         * If TEST_UNIT_READY fails, we know
2188
                         * MEGA_RESERVATION_STATUS failed
2189
                         */
2190
                        if( cmd->cmnd[0] == TEST_UNIT_READY ) {
2191
                                cmd->result |= (DID_ERROR << 16) |
2192
                                        (RESERVATION_CONFLICT << 1);
2193
                        }
2194
                        else
2195
                        /*
2196
                         * Error code returned is 1 if Reserve or Release
2197
                         * failed or the input parameter is invalid
2198
                         */
2199
                        if( status == 1 &&
2200
                                (cmd->cmnd[0] == RESERVE ||
2201
                                         cmd->cmnd[0] == RELEASE) ) {
2202
 
2203
                                cmd->result |= (DID_ERROR << 16) |
2204
                                        (RESERVATION_CONFLICT << 1);
2205
                        }
2206
                        else
2207
#endif
2208
                                cmd->result |= (DID_BAD_TARGET << 16)|status;
2209
                }
2210
 
2211
                /*
2212
                 * Only free SCBs for the commands coming down from the
2213
                 * mid-layer, not for which were issued internally
2214
                 *
2215
                 * For internal command, restore the status returned by the
2216
                 * firmware so that user can interpret it.
2217
                 */
2218
                if( cmdid == CMDID_INT_CMDS ) { /* internal command */
2219
                        cmd->result = status;
2220
 
2221
                        /*
2222
                         * Remove the internal command from the pending list
2223
                         */
2224
                        list_del_init(&scb->list);
2225
                        scb->state = SCB_FREE;
2226
                }
2227
                else {
2228
                        mega_free_scb(adapter, scb);
2229
                }
2230
 
2231
                /*
2232
                 * Call the mid-layer callback for this command
2233
                 */
2234
                cmd->scsi_done(cmd);
2235
        }
2236
}
2237
 
2238
 
2239
/*
2240
 * Free a SCB structure
2241
 * Note: We assume the scsi commands associated with this scb is not free yet.
2242
 */
2243
static void
2244
mega_free_scb(adapter_t *adapter, scb_t *scb)
2245
{
2246
        switch( scb->dma_type ) {
2247
 
2248
        case MEGA_DMA_TYPE_NONE:
2249
                break;
2250
 
2251
        case MEGA_BULK_DATA:
2252
                pci_unmap_page(adapter->dev, scb->dma_h_bulkdata,
2253
                        scb->cmd->request_bufflen, scb->dma_direction);
2254
 
2255
                if( scb->dma_direction == PCI_DMA_FROMDEVICE ) {
2256
                        pci_dma_sync_single(adapter->dev, scb->dma_h_bulkdata,
2257
                                        scb->cmd->request_bufflen,
2258
                                        PCI_DMA_FROMDEVICE);
2259
                }
2260
 
2261
                break;
2262
 
2263
        case MEGA_SGLIST:
2264
                pci_unmap_sg(adapter->dev, scb->cmd->request_buffer,
2265
                        scb->cmd->use_sg, scb->dma_direction);
2266
 
2267
                if( scb->dma_direction == PCI_DMA_FROMDEVICE ) {
2268
                        pci_dma_sync_sg(adapter->dev, scb->cmd->request_buffer,
2269
                                        scb->cmd->use_sg, PCI_DMA_FROMDEVICE);
2270
                }
2271
 
2272
                break;
2273
 
2274
        default:
2275
                break;
2276
        }
2277
 
2278
        /*
2279
         * Remove from the pending list
2280
         */
2281
        list_del_init(&scb->list);
2282
 
2283
        /* Link the scb back into free list */
2284
        scb->state = SCB_FREE;
2285
        scb->cmd = NULL;
2286
 
2287
        list_add(&scb->list, &adapter->free_list);
2288
}
2289
 
2290
 
2291
/*
2292
 * Wait until the controller's mailbox is available
2293
 */
2294
static inline int
2295
mega_busywait_mbox (adapter_t *adapter)
2296
{
2297
        if (adapter->mbox->busy)
2298
                return __mega_busywait_mbox(adapter);
2299
        return 0;
2300
}
2301
 
2302
static int
2303
__mega_busywait_mbox (adapter_t *adapter)
2304
{
2305
        volatile mbox_t *mbox = adapter->mbox;
2306
        long counter;
2307
 
2308
        for (counter = 0; counter < 10000; counter++) {
2309
                if (!mbox->busy)
2310
                        return 0;
2311
                udelay(100); yield();
2312
        }
2313
        return -1;              /* give up after 1 second */
2314
}
2315
 
2316
/*
2317
 * Copies data to SGLIST
2318
 * Note: For 64 bit cards, we need a minimum of one SG element for read/write
2319
 */
2320
static int
2321
mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
2322
{
2323
        struct scatterlist      *sgl;
2324
        struct page     *page;
2325
        unsigned long   offset;
2326
        Scsi_Cmnd       *cmd;
2327
        int     sgcnt;
2328
        int     idx;
2329
 
2330
        cmd = scb->cmd;
2331
 
2332
        /* Scatter-gather not used */
2333
        if( !cmd->use_sg ) {
2334
 
2335
                page = virt_to_page(cmd->request_buffer);
2336
 
2337
                offset = ((unsigned long)cmd->request_buffer & ~PAGE_MASK);
2338
 
2339
                scb->dma_h_bulkdata = pci_map_page(adapter->dev, page, offset,
2340
                                                  cmd->request_bufflen,
2341
                                                  scb->dma_direction);
2342
                scb->dma_type = MEGA_BULK_DATA;
2343
 
2344
                /*
2345
                 * We need to handle special 64-bit commands that need a
2346
                 * minimum of 1 SG
2347
                 */
2348
                if( adapter->has_64bit_addr ) {
2349
                        scb->sgl64[0].address = scb->dma_h_bulkdata;
2350
                        scb->sgl64[0].length = cmd->request_bufflen;
2351
                        *buf = (u32)scb->sgl_dma_addr;
2352
                        *len = (u32)cmd->request_bufflen;
2353
                        return 1;
2354
                }
2355
                else {
2356
                        *buf = (u32)scb->dma_h_bulkdata;
2357
                        *len = (u32)cmd->request_bufflen;
2358
                }
2359
 
2360
                if( scb->dma_direction == PCI_DMA_TODEVICE ) {
2361
                        pci_dma_sync_single(adapter->dev,
2362
                                        scb->dma_h_bulkdata,
2363
                                        cmd->request_bufflen,
2364
                                        PCI_DMA_TODEVICE);
2365
                }
2366
 
2367
                return 0;
2368
        }
2369
 
2370
        sgl = (struct scatterlist *)cmd->request_buffer;
2371
 
2372
        /*
2373
         * Copy Scatter-Gather list info into controller structure.
2374
         *
2375
         * The number of sg elements returned must not exceed our limit
2376
         */
2377
        sgcnt = pci_map_sg(adapter->dev, sgl, cmd->use_sg, scb->dma_direction);
2378
 
2379
        scb->dma_type = MEGA_SGLIST;
2380
 
2381
        if( sgcnt > adapter->sglen ) BUG();
2382
 
2383
        for( idx = 0; idx < sgcnt; idx++, sgl++ ) {
2384
 
2385
                if( adapter->has_64bit_addr ) {
2386
                        scb->sgl64[idx].address = sg_dma_address(sgl);
2387
                        scb->sgl64[idx].length = sg_dma_len(sgl);
2388
                }
2389
                else {
2390
                        scb->sgl[idx].address = sg_dma_address(sgl);
2391
                        scb->sgl[idx].length = sg_dma_len(sgl);
2392
                }
2393
        }
2394
 
2395
        /* Reset pointer and length fields */
2396
        *buf = scb->sgl_dma_addr;
2397
 
2398
        /*
2399
         * For passthru command, dataxferlen must be set, even for commands
2400
         * with a sg list
2401
         */
2402
        *len = (u32)cmd->request_bufflen;
2403
 
2404
        if( scb->dma_direction == PCI_DMA_TODEVICE ) {
2405
                pci_dma_sync_sg(adapter->dev, cmd->request_buffer,
2406
                                cmd->use_sg, PCI_DMA_TODEVICE);
2407
        }
2408
 
2409
        /* Return count of SG requests */
2410
        return sgcnt;
2411
}
2412
 
2413
 
2414
/*
2415
 * mega_8_to_40ld()
2416
 *
2417
 * takes all info in AdapterInquiry structure and puts it into ProductInfo and
2418
 * Enquiry3 structures for later use
2419
 */
2420
static void
2421
mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
2422
                mega_product_info *product_info)
2423
{
2424
        int i;
2425
 
2426
        product_info->max_commands = inquiry->adapter_info.max_commands;
2427
        enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
2428
        product_info->nchannels = inquiry->adapter_info.nchannels;
2429
 
2430
        for (i = 0; i < 4; i++) {
2431
                product_info->fw_version[i] =
2432
                        inquiry->adapter_info.fw_version[i];
2433
 
2434
                product_info->bios_version[i] =
2435
                        inquiry->adapter_info.bios_version[i];
2436
        }
2437
        enquiry3->cache_flush_interval =
2438
                inquiry->adapter_info.cache_flush_interval;
2439
 
2440
        product_info->dram_size = inquiry->adapter_info.dram_size;
2441
 
2442
        enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
2443
 
2444
        for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
2445
                enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
2446
                enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
2447
                enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
2448
        }
2449
 
2450
        for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
2451
                enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
2452
}
2453
 
2454
 
2455
/*
2456
 * Release the controller's resources
2457
 */
2458
static int
2459
megaraid_release(struct Scsi_Host *host)
2460
{
2461
        adapter_t       *adapter;
2462
        mbox_t  *mbox;
2463
        u_char  raw_mbox[sizeof(mbox_t)];
2464
#ifdef CONFIG_PROC_FS
2465
        char    buf[12] = { 0 };
2466
#endif
2467
 
2468
        adapter = (adapter_t *)host->hostdata;
2469
        mbox = (mbox_t *)raw_mbox;
2470
 
2471
        printk(KERN_NOTICE "megaraid: being unloaded...");
2472
 
2473
        /* Flush adapter cache */
2474
        memset(raw_mbox, 0, sizeof(raw_mbox));
2475
        raw_mbox[0] = FLUSH_ADAPTER;
2476
 
2477
        irq_disable(adapter);
2478
        free_irq(adapter->host->irq, adapter);
2479
 
2480
        /* Issue a blocking (interrupts disabled) command to the card */
2481
        issue_scb_block(adapter, raw_mbox);
2482
 
2483
        /* Flush disks cache */
2484
        memset(raw_mbox, 0, sizeof(raw_mbox));
2485
        raw_mbox[0] = FLUSH_SYSTEM;
2486
 
2487
        /* Issue a blocking (interrupts disabled) command to the card */
2488
        issue_scb_block(adapter, raw_mbox);
2489
 
2490
 
2491
        /* Free our resources */
2492
        if( adapter->flag & BOARD_MEMMAP ) {
2493
                iounmap((void *)adapter->base);
2494
                release_mem_region(adapter->host->base, 128);
2495
        }
2496
        else {
2497
                release_region(adapter->base, 16);
2498
        }
2499
 
2500
        mega_free_sgl(adapter);
2501
 
2502
#ifdef CONFIG_PROC_FS
2503
        if( adapter->controller_proc_dir_entry ) {
2504
                remove_proc_entry("stat", adapter->controller_proc_dir_entry);
2505
                remove_proc_entry("config",
2506
                                adapter->controller_proc_dir_entry);
2507
                remove_proc_entry("mailbox",
2508
                                adapter->controller_proc_dir_entry);
2509
#if MEGA_HAVE_ENH_PROC
2510
                remove_proc_entry("rebuild-rate",
2511
                                adapter->controller_proc_dir_entry);
2512
                remove_proc_entry("battery-status",
2513
                                adapter->controller_proc_dir_entry);
2514
 
2515
                remove_proc_entry("diskdrives-ch0",
2516
                                adapter->controller_proc_dir_entry);
2517
                remove_proc_entry("diskdrives-ch1",
2518
                                adapter->controller_proc_dir_entry);
2519
                remove_proc_entry("diskdrives-ch2",
2520
                                adapter->controller_proc_dir_entry);
2521
                remove_proc_entry("diskdrives-ch3",
2522
                                adapter->controller_proc_dir_entry);
2523
 
2524
                remove_proc_entry("raiddrives-0-9",
2525
                                adapter->controller_proc_dir_entry);
2526
                remove_proc_entry("raiddrives-10-19",
2527
                                adapter->controller_proc_dir_entry);
2528
                remove_proc_entry("raiddrives-20-29",
2529
                                adapter->controller_proc_dir_entry);
2530
                remove_proc_entry("raiddrives-30-39",
2531
                                adapter->controller_proc_dir_entry);
2532
#endif
2533
 
2534
                sprintf(buf, "hba%d", adapter->host->host_no);
2535
                remove_proc_entry(buf, mega_proc_dir_entry);
2536
        }
2537
#endif
2538
 
2539
        pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
2540
                        adapter->mega_buffer, adapter->buf_dma_handle);
2541
        kfree(adapter->scb_list);
2542
        pci_free_consistent(adapter->dev, sizeof(mbox64_t),
2543
                        (void *)adapter->una_mbox64, adapter->una_mbox64_dma);
2544
 
2545
        hba_count--;
2546
 
2547
        if( hba_count == 0 ) {
2548
 
2549
                /*
2550
                 * Unregister the character device interface to the driver.
2551
                 */
2552
                unregister_chrdev(major, "megadev");
2553
 
2554
                unregister_reboot_notifier(&mega_notifier);
2555
 
2556
#ifdef CONFIG_PROC_FS
2557
                if( adapter->controller_proc_dir_entry ) {
2558
                        remove_proc_entry ("megaraid", &proc_root);
2559
                }
2560
#endif
2561
 
2562
        }
2563
 
2564
        /*
2565
         * Release the controller memory. A word of warning this frees
2566
         * hostdata and that includes adapter-> so be careful what you
2567
         * dereference beyond this point
2568
         */
2569
        scsi_unregister(host);
2570
 
2571
 
2572
        printk("ok.\n");
2573
 
2574
        return 0;
2575
}
2576
 
2577
static inline void
2578
mega_free_sgl(adapter_t *adapter)
2579
{
2580
        scb_t   *scb;
2581
        int     i;
2582
 
2583
        for(i = 0; i < adapter->max_cmds; i++) {
2584
 
2585
                scb = &adapter->scb_list[i];
2586
 
2587
                if( scb->sgl64 ) {
2588
                        pci_free_consistent(adapter->dev,
2589
                                sizeof(mega_sgl64) * adapter->sglen,
2590
                                scb->sgl64,
2591
                                scb->sgl_dma_addr);
2592
 
2593
                        scb->sgl64 = NULL;
2594
                }
2595
 
2596
                if( scb->pthru ) {
2597
                        pci_free_consistent(adapter->dev, sizeof(mega_passthru),
2598
                                scb->pthru, scb->pthru_dma_addr);
2599
 
2600
                        scb->pthru = NULL;
2601
                }
2602
 
2603
                if( scb->epthru ) {
2604
                        pci_free_consistent(adapter->dev,
2605
                                sizeof(mega_ext_passthru),
2606
                                scb->epthru, scb->epthru_dma_addr);
2607
 
2608
                        scb->epthru = NULL;
2609
                }
2610
 
2611
        }
2612
}
2613
 
2614
 
2615
/*
2616
 * Get information about the card/driver
2617
 */
2618
const char *
2619
megaraid_info(struct Scsi_Host *host)
2620
{
2621
        static char buffer[512];
2622
        adapter_t *adapter;
2623
 
2624
        adapter = (adapter_t *)host->hostdata;
2625
 
2626
        sprintf (buffer,
2627
                 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
2628
                 adapter->fw_version, adapter->product_info.max_commands,
2629
                 adapter->host->max_id, adapter->host->max_channel,
2630
                 adapter->host->max_lun);
2631
        return buffer;
2632
}
2633
 
2634
/* shouldn't be used, but included for completeness */
2635
static int
2636
megaraid_command (Scsi_Cmnd *cmd)
2637
{
2638
        printk(KERN_WARNING
2639
        "megaraid critcal error: synchronous interface is not implemented.\n");
2640
 
2641
        cmd->result = (DID_ERROR << 16);
2642
        cmd->scsi_done(cmd);
2643
 
2644
        return 1;
2645
}
2646
 
2647
 
2648
/**
2649
 * megaraid_abort - abort the scsi command
2650
 * @scp - command to be aborted
2651
 *
2652
 * Abort a previous SCSI request. Only commands on the pending list can be
2653
 * aborted. All the commands issued to the F/W must complete.
2654
 */
2655
static int
2656
megaraid_abort(Scsi_Cmnd *scp)
2657
{
2658
        adapter_t               *adapter;
2659
        struct list_head        *pos, *next;
2660
        scb_t                   *scb;
2661
        long                    iter;
2662
        int                     rval = SUCCESS;
2663
 
2664
        adapter = (adapter_t *)scp->host->hostdata;
2665
 
2666
        ASSERT( spin_is_locked(adapter->host_lock) );
2667
 
2668
        printk("megaraid: aborting-%ld cmd=%x <c=%d t=%d l=%d>\n",
2669
                scp->serial_number, scp->cmnd[0], scp->channel, scp->target,
2670
                scp->lun);
2671
 
2672
 
2673
        list_for_each_safe( pos, next, &adapter->pending_list ) {
2674
 
2675
                scb = list_entry(pos, scb_t, list);
2676
 
2677
                if( scb->cmd == scp ) { /* Found command */
2678
 
2679
                        scb->state |= SCB_ABORT;
2680
 
2681
                        /*
2682
                         * Check if this command was never issued. If this is
2683
                         * the case, take it off from the pending list and
2684
                         * complete.
2685
                         */
2686
                        if( !(scb->state & SCB_ISSUED) ) {
2687
 
2688
                                printk(KERN_WARNING
2689
                                "megaraid: %ld:%d, driver owner.\n",
2690
                                        scp->serial_number, scb->idx);
2691
 
2692
                                scp->result = (DID_ABORT << 16);
2693
 
2694
                                mega_free_scb(adapter, scb);
2695
 
2696
                                scp->scsi_done(scp);
2697
 
2698
                                break;
2699
                        }
2700
                }
2701
        }
2702
 
2703
        /*
2704
         * By this time, either all commands are completed or aborted by
2705
         * mid-layer. Do not return until all the commands are actually
2706
         * completed by the firmware
2707
         */
2708
        iter = 0;
2709
        while( atomic_read(&adapter->pend_cmds) > 0 ) {
2710
                /*
2711
                 * Perform the ack sequence, since interrupts are not
2712
                 * available right now!
2713
                 */
2714
                if( adapter->flag & BOARD_MEMMAP ) {
2715
                        megaraid_memmbox_ack_sequence(adapter);
2716
                }
2717
                else {
2718
                        megaraid_iombox_ack_sequence(adapter);
2719
                }
2720
 
2721
                /*
2722
                 * print a message once every second only
2723
                 */
2724
                if( !(iter % 1000) ) {
2725
                        printk(
2726
                        "megaraid: Waiting for %d commands to flush: iter:%ld\n",
2727
                                atomic_read(&adapter->pend_cmds), iter);
2728
                }
2729
 
2730
                if( iter++ < MBOX_ABORT_SLEEP*1000 ) {
2731
                        mdelay(1);
2732
                }
2733
                else {
2734
                        printk(KERN_WARNING
2735
                                "megaraid: critical hardware error!\n");
2736
 
2737
                        rval = FAILED;
2738
 
2739
                        break;
2740
                }
2741
        }
2742
 
2743
        if( rval == SUCCESS ) {
2744
                printk(KERN_INFO
2745
                        "megaraid: abort sequence successfully completed.\n");
2746
        }
2747
 
2748
        return rval;
2749
}
2750
 
2751
 
2752
static int
2753
megaraid_reset(Scsi_Cmnd *cmd)
2754
{
2755
        adapter_t       *adapter;
2756
        megacmd_t       mc;
2757
        long            iter;
2758
        int             rval = SUCCESS;
2759
 
2760
        adapter = (adapter_t *)cmd->host->hostdata;
2761
 
2762
        ASSERT( spin_is_locked(adapter->host_lock) );
2763
 
2764
        printk("megaraid: reset-%ld cmd=%x <c=%d t=%d l=%d>\n",
2765
                cmd->serial_number, cmd->cmnd[0], cmd->channel, cmd->target,
2766
                cmd->lun);
2767
 
2768
 
2769
#if MEGA_HAVE_CLUSTERING
2770
        mc.cmd = MEGA_CLUSTER_CMD;
2771
        mc.opcode = MEGA_RESET_RESERVATIONS;
2772
 
2773
        spin_unlock_irq(adapter->host_lock);
2774
        if( mega_internal_command(adapter, LOCK_INT, &mc, NULL) != 0 ) {
2775
                printk(KERN_WARNING
2776
                                "megaraid: reservation reset failed.\n");
2777
        }
2778
        else {
2779
                printk(KERN_INFO "megaraid: reservation reset.\n");
2780
        }
2781
        spin_lock_irq(adapter->host_lock);
2782
#endif
2783
 
2784
        /*
2785
         * Do not return until all the commands are actually completed by the
2786
         * firmware
2787
         */
2788
        iter = 0;
2789
        while( atomic_read(&adapter->pend_cmds) > 0 ) {
2790
                /*
2791
                 * Perform the ack sequence, since interrupts are not
2792
                 * available right now!
2793
                 */
2794
                if( adapter->flag & BOARD_MEMMAP ) {
2795
                        megaraid_memmbox_ack_sequence(adapter);
2796
                }
2797
                else {
2798
                        megaraid_iombox_ack_sequence(adapter);
2799
                }
2800
 
2801
                /*
2802
                 * print a message once every second only
2803
                 */
2804
                if( !(iter % 1000) ) {
2805
                        printk(
2806
                        "megaraid: Waiting for %d commands to flush: iter:%ld\n",
2807
                                atomic_read(&adapter->pend_cmds), iter);
2808
                }
2809
 
2810
                if( iter++ < MBOX_RESET_SLEEP*1000 ) {
2811
                        mdelay(1);
2812
                }
2813
                else {
2814
                        printk(KERN_WARNING
2815
                                "megaraid: critical hardware error!\n");
2816
 
2817
                        rval = FAILED;
2818
 
2819
                        break;
2820
                }
2821
        }
2822
 
2823
        if( rval == SUCCESS ) {
2824
                printk(KERN_INFO
2825
                        "megaraid: reset sequence successfully completed.\n");
2826
        }
2827
 
2828
        return rval;
2829
}
2830
 
2831
 
2832
#ifdef CONFIG_PROC_FS
2833
/* Following code handles /proc fs  */
2834
 
2835
#define CREATE_READ_PROC(string, func)  create_proc_read_entry(string,  \
2836
                                        S_IRUSR | S_IFREG,              \
2837
                                        controller_proc_dir_entry,      \
2838
                                        func, adapter)
2839
 
2840
/**
2841
 * mega_create_proc_entry()
2842
 * @index - index in soft state array
2843
 * @parent - parent node for this /proc entry
2844
 *
2845
 * Creates /proc entries for our controllers.
2846
 */
2847
static void
2848
mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2849
{
2850
        struct proc_dir_entry   *controller_proc_dir_entry = NULL;
2851
        u8              string[64] = { 0 };
2852
        adapter_t       *adapter = hba_soft_state[index];
2853
 
2854
        sprintf(string, "hba%d", adapter->host->host_no);
2855
 
2856
        controller_proc_dir_entry =
2857
                adapter->controller_proc_dir_entry = proc_mkdir(string, parent);
2858
 
2859
        if(!controller_proc_dir_entry) {
2860
                printk(KERN_WARNING "\nmegaraid: proc_mkdir failed\n");
2861
                return;
2862
        }
2863
        adapter->proc_read = CREATE_READ_PROC("config", proc_read_config);
2864
        adapter->proc_stat = CREATE_READ_PROC("stat", proc_read_stat);
2865
        adapter->proc_mbox = CREATE_READ_PROC("mailbox", proc_read_mbox);
2866
#if MEGA_HAVE_ENH_PROC
2867
        adapter->proc_rr = CREATE_READ_PROC("rebuild-rate", proc_rebuild_rate);
2868
        adapter->proc_battery = CREATE_READ_PROC("battery-status",
2869
                        proc_battery);
2870
 
2871
        /*
2872
         * Display each physical drive on its channel
2873
         */
2874
        adapter->proc_pdrvstat[0] = CREATE_READ_PROC("diskdrives-ch0",
2875
                                        proc_pdrv_ch0);
2876
        adapter->proc_pdrvstat[1] = CREATE_READ_PROC("diskdrives-ch1",
2877
                                        proc_pdrv_ch1);
2878
        adapter->proc_pdrvstat[2] = CREATE_READ_PROC("diskdrives-ch2",
2879
                                        proc_pdrv_ch2);
2880
        adapter->proc_pdrvstat[3] = CREATE_READ_PROC("diskdrives-ch3",
2881
                                        proc_pdrv_ch3);
2882
 
2883
        /*
2884
         * Display a set of up to 10 logical drive through each of following
2885
         * /proc entries
2886
         */
2887
        adapter->proc_rdrvstat[0] = CREATE_READ_PROC("raiddrives-0-9",
2888
                                        proc_rdrv_10);
2889
        adapter->proc_rdrvstat[1] = CREATE_READ_PROC("raiddrives-10-19",
2890
                                        proc_rdrv_20);
2891
        adapter->proc_rdrvstat[2] = CREATE_READ_PROC("raiddrives-20-29",
2892
                                        proc_rdrv_30);
2893
        adapter->proc_rdrvstat[3] = CREATE_READ_PROC("raiddrives-30-39",
2894
                                        proc_rdrv_40);
2895
#endif
2896
}
2897
 
2898
 
2899
/**
2900
 * proc_read_config()
2901
 * @page - buffer to write the data in
2902
 * @start - where the actual data has been written in page
2903
 * @offset - same meaning as the read system call
2904
 * @count - same meaning as the read system call
2905
 * @eof - set if no more data needs to be returned
2906
 * @data - pointer to our soft state
2907
 *
2908
 * Display configuration information about the controller.
2909
 */
2910
static int
2911
proc_read_config(char *page, char **start, off_t offset, int count, int *eof,
2912
                void *data)
2913
{
2914
 
2915
        adapter_t *adapter = (adapter_t *)data;
2916
        int len = 0;
2917
 
2918
        len += sprintf(page+len, "%s", MEGARAID_VERSION);
2919
 
2920
        if(adapter->product_info.product_name[0])
2921
                len += sprintf(page+len, "%s\n",
2922
                                adapter->product_info.product_name);
2923
 
2924
        len += sprintf(page+len, "Controller Type: ");
2925
 
2926
        if( adapter->flag & BOARD_MEMMAP ) {
2927
                len += sprintf(page+len,
2928
                        "438/466/467/471/493/518/520/531/532\n");
2929
        }
2930
        else {
2931
                len += sprintf(page+len,
2932
                        "418/428/434\n");
2933
        }
2934
 
2935
        if(adapter->flag & BOARD_40LD) {
2936
                len += sprintf(page+len,
2937
                                "Controller Supports 40 Logical Drives\n");
2938
        }
2939
 
2940
        if(adapter->flag & BOARD_64BIT) {
2941
                len += sprintf(page+len,
2942
                "Controller capable of 64-bit memory addressing\n");
2943
        }
2944
        if( adapter->has_64bit_addr ) {
2945
                len += sprintf(page+len,
2946
                        "Controller using 64-bit memory addressing\n");
2947
        }
2948
        else {
2949
                len += sprintf(page+len,
2950
                        "Controller is not using 64-bit memory addressing\n");
2951
        }
2952
 
2953
        len += sprintf(page+len, "Base = %08lx, Irq = %d, ", adapter->base,
2954
                        adapter->host->irq);
2955
 
2956
        len += sprintf(page+len, "Initial Logical Drives = %d, Channels = %d\n",
2957
                        adapter->numldrv, adapter->product_info.nchannels);
2958
 
2959
        len += sprintf(page+len, "Version =%s:%s, DRAM = %dMb\n",
2960
                        adapter->fw_version, adapter->bios_version,
2961
                        adapter->product_info.dram_size);
2962
 
2963
        len += sprintf(page+len,
2964
                "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
2965
                adapter->product_info.max_commands, adapter->max_cmds);
2966
 
2967
        len += sprintf(page+len, "support_ext_cdb    = %d\n",
2968
                        adapter->support_ext_cdb);
2969
        len += sprintf(page+len, "support_random_del = %d\n",
2970
                        adapter->support_random_del);
2971
        len += sprintf(page+len, "boot_ldrv_enabled  = %d\n",
2972
                        adapter->boot_ldrv_enabled);
2973
        len += sprintf(page+len, "boot_ldrv          = %d\n",
2974
                        adapter->boot_ldrv);
2975
        len += sprintf(page+len, "boot_pdrv_enabled  = %d\n",
2976
                        adapter->boot_pdrv_enabled);
2977
        len += sprintf(page+len, "boot_pdrv_ch       = %d\n",
2978
                        adapter->boot_pdrv_ch);
2979
        len += sprintf(page+len, "boot_pdrv_tgt      = %d\n",
2980
                        adapter->boot_pdrv_tgt);
2981
        len += sprintf(page+len, "quiescent          = %d\n",
2982
                        atomic_read(&adapter->quiescent));
2983
        len += sprintf(page+len, "has_cluster        = %d\n",
2984
                        adapter->has_cluster);
2985
 
2986
        len += sprintf(page+len, "\nModule Parameters:\n");
2987
        len += sprintf(page+len, "max_cmd_per_lun    = %d\n",
2988
                        max_cmd_per_lun);
2989
        len += sprintf(page+len, "max_sectors_per_io = %d\n",
2990
                        max_sectors_per_io);
2991
 
2992
        *eof = 1;
2993
 
2994
        return len;
2995
}
2996
 
2997
 
2998
 
2999
/**
3000
 * proc_read_stat()
3001
 * @page - buffer to write the data in
3002
 * @start - where the actual data has been written in page
3003
 * @offset - same meaning as the read system call
3004
 * @count - same meaning as the read system call
3005
 * @eof - set if no more data needs to be returned
3006
 * @data - pointer to our soft state
3007
 *
3008
 * Diaplay statistical information about the I/O activity.
3009
 */
3010
static int
3011
proc_read_stat(char *page, char **start, off_t offset, int count, int *eof,
3012
                void *data)
3013
{
3014
        adapter_t       *adapter;
3015
        int     len;
3016
        int     i;
3017
 
3018
        i = 0;   /* avoid compilation warnings */
3019
        len = 0;
3020
        adapter = (adapter_t *)data;
3021
 
3022
        len = sprintf(page, "Statistical Information for this controller\n");
3023
        len += sprintf(page+len, "pend_cmds = %d\n",
3024
                        atomic_read(&adapter->pend_cmds));
3025
#if MEGA_HAVE_STATS
3026
        for(i = 0; i < adapter->numldrv; i++) {
3027
                len += sprintf(page+len, "Logical Drive %d:\n", i);
3028
 
3029
                len += sprintf(page+len,
3030
                        "\tReads Issued = %lu, Writes Issued = %lu\n",
3031
                        adapter->nreads[i], adapter->nwrites[i]);
3032
 
3033
                len += sprintf(page+len,
3034
                        "\tSectors Read = %lu, Sectors Written = %lu\n",
3035
                        adapter->nreadblocks[i], adapter->nwriteblocks[i]);
3036
 
3037
                len += sprintf(page+len,
3038
                        "\tRead errors = %lu, Write errors = %lu\n\n",
3039
                        adapter->rd_errors[i], adapter->wr_errors[i]);
3040
        }
3041
#else
3042
        len += sprintf(page+len,
3043
                        "IO and error counters not compiled in driver.\n");
3044
#endif
3045
 
3046
        *eof = 1;
3047
 
3048
        return len;
3049
}
3050
 
3051
 
3052
/**
3053
 * proc_read_mbox()
3054
 * @page - buffer to write the data in
3055
 * @start - where the actual data has been written in page
3056
 * @offset - same meaning as the read system call
3057
 * @count - same meaning as the read system call
3058
 * @eof - set if no more data needs to be returned
3059
 * @data - pointer to our soft state
3060
 *
3061
 * Display mailbox information for the last command issued. This information
3062
 * is good for debugging.
3063
 */
3064
static int
3065
proc_read_mbox(char *page, char **start, off_t offset, int count, int *eof,
3066
                void *data)
3067
{
3068
 
3069
        adapter_t       *adapter = (adapter_t *)data;
3070
        volatile mbox_t *mbox = adapter->mbox;
3071
        int     len = 0;
3072
 
3073
        len = sprintf(page, "Contents of Mail Box Structure\n");
3074
        len += sprintf(page+len, "  Fw Command   = 0x%02x\n", mbox->cmd);
3075
        len += sprintf(page+len, "  Cmd Sequence = 0x%02x\n", mbox->cmdid);
3076
        len += sprintf(page+len, "  No of Sectors= %04d\n", mbox->numsectors);
3077
        len += sprintf(page+len, "  LBA          = 0x%02x\n", mbox->lba);
3078
        len += sprintf(page+len, "  DTA          = 0x%08x\n", mbox->xferaddr);
3079
        len += sprintf(page+len, "  Logical Drive= 0x%02x\n", mbox->logdrv);
3080
        len += sprintf(page+len, "  No of SG Elmt= 0x%02x\n",
3081
                        mbox->numsgelements);
3082
        len += sprintf(page+len, "  Busy         = %01x\n", mbox->busy);
3083
        len += sprintf(page+len, "  Status       = 0x%02x\n", mbox->status);
3084
 
3085
        *eof = 1;
3086
 
3087
        return len;
3088
}
3089
 
3090
 
3091
/**
3092
 * proc_rebuild_rate()
3093
 * @page - buffer to write the data in
3094
 * @start - where the actual data has been written in page
3095
 * @offset - same meaning as the read system call
3096
 * @count - same meaning as the read system call
3097
 * @eof - set if no more data needs to be returned
3098
 * @data - pointer to our soft state
3099
 *
3100
 * Display current rebuild rate
3101
 */
3102
static int
3103
proc_rebuild_rate(char *page, char **start, off_t offset, int count, int *eof,
3104
                void *data)
3105
{
3106
        adapter_t       *adapter = (adapter_t *)data;
3107
        dma_addr_t      dma_handle;
3108
        caddr_t         inquiry;
3109
        struct pci_dev  *pdev;
3110
        int     len = 0;
3111
 
3112
        pdev = adapter->dev;
3113
 
3114
        if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
3115
                *eof = 1;
3116
                return len;
3117
        }
3118
 
3119
        if( mega_adapinq(adapter, dma_handle) != 0 ) {
3120
 
3121
                len = sprintf(page, "Adapter inquiry failed.\n");
3122
 
3123
                printk(KERN_WARNING "megaraid: inquiry failed.\n");
3124
 
3125
                mega_free_inquiry(inquiry, dma_handle, pdev);
3126
 
3127
                *eof = 1;
3128
 
3129
                return len;
3130
        }
3131
 
3132
        if( adapter->flag & BOARD_40LD ) {
3133
                len = sprintf(page, "Rebuild Rate: [%d%%]\n",
3134
                        ((mega_inquiry3 *)inquiry)->rebuild_rate);
3135
        }
3136
        else {
3137
                len = sprintf(page, "Rebuild Rate: [%d%%]\n",
3138
                        ((mraid_ext_inquiry *)
3139
                        inquiry)->raid_inq.adapter_info.rebuild_rate);
3140
        }
3141
 
3142
 
3143
        mega_free_inquiry(inquiry, dma_handle, pdev);
3144
 
3145
        *eof = 1;
3146
 
3147
        return len;
3148
}
3149
 
3150
 
3151
/**
3152
 * proc_battery()
3153
 * @page - buffer to write the data in
3154
 * @start - where the actual data has been written in page
3155
 * @offset - same meaning as the read system call
3156
 * @count - same meaning as the read system call
3157
 * @eof - set if no more data needs to be returned
3158
 * @data - pointer to our soft state
3159
 *
3160
 * Display information about the battery module on the controller.
3161
 */
3162
static int
3163
proc_battery(char *page, char **start, off_t offset, int count, int *eof,
3164
                void *data)
3165
{
3166
        adapter_t       *adapter = (adapter_t *)data;
3167
        dma_addr_t      dma_handle;
3168
        caddr_t         inquiry;
3169
        struct pci_dev  *pdev;
3170
        u8      battery_status = 0;
3171
        char    str[256];
3172
        int     len = 0;
3173
 
3174
        pdev = adapter->dev;
3175
 
3176
        if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
3177
                *eof = 1;
3178
                return len;
3179
        }
3180
 
3181
        if( mega_adapinq(adapter, dma_handle) != 0 ) {
3182
 
3183
                len = sprintf(page, "Adapter inquiry failed.\n");
3184
 
3185
                printk(KERN_WARNING "megaraid: inquiry failed.\n");
3186
 
3187
                mega_free_inquiry(inquiry, dma_handle, pdev);
3188
 
3189
                *eof = 1;
3190
 
3191
                return len;
3192
        }
3193
 
3194
        if( adapter->flag & BOARD_40LD ) {
3195
                battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
3196
        }
3197
        else {
3198
                battery_status = ((mraid_ext_inquiry *)inquiry)->
3199
                        raid_inq.adapter_info.battery_status;
3200
        }
3201
 
3202
        /*
3203
         * Decode the battery status
3204
         */
3205
        sprintf(str, "Battery Status:[%d]", battery_status);
3206
 
3207
        if(battery_status == MEGA_BATT_CHARGE_DONE)
3208
                strcat(str, " Charge Done");
3209
 
3210
        if(battery_status & MEGA_BATT_MODULE_MISSING)
3211
                strcat(str, " Module Missing");
3212
 
3213
        if(battery_status & MEGA_BATT_LOW_VOLTAGE)
3214
                strcat(str, " Low Voltage");
3215
 
3216
        if(battery_status & MEGA_BATT_TEMP_HIGH)
3217
                strcat(str, " Temperature High");
3218
 
3219
        if(battery_status & MEGA_BATT_PACK_MISSING)
3220
                strcat(str, " Pack Missing");
3221
 
3222
        if(battery_status & MEGA_BATT_CHARGE_INPROG)
3223
                strcat(str, " Charge In-progress");
3224
 
3225
        if(battery_status & MEGA_BATT_CHARGE_FAIL)
3226
                strcat(str, " Charge Fail");
3227
 
3228
        if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
3229
                strcat(str, " Cycles Exceeded");
3230
 
3231
        len = sprintf(page, "%s\n", str);
3232
 
3233
 
3234
        mega_free_inquiry(inquiry, dma_handle, pdev);
3235
 
3236
        *eof = 1;
3237
 
3238
        return len;
3239
}
3240
 
3241
 
3242
/**
3243
 * proc_pdrv_ch0()
3244
 * @page - buffer to write the data in
3245
 * @start - where the actual data has been written in page
3246
 * @offset - same meaning as the read system call
3247
 * @count - same meaning as the read system call
3248
 * @eof - set if no more data needs to be returned
3249
 * @data - pointer to our soft state
3250
 *
3251
 * Display information about the physical drives on physical channel 0.
3252
 */
3253
static int
3254
proc_pdrv_ch0(char *page, char **start, off_t offset, int count, int *eof,
3255
                void *data)
3256
{
3257
        adapter_t *adapter = (adapter_t *)data;
3258
 
3259
        *eof = 1;
3260
 
3261
        return (proc_pdrv(adapter, page, 0));
3262
}
3263
 
3264
 
3265
/**
3266
 * proc_pdrv_ch1()
3267
 * @page - buffer to write the data in
3268
 * @start - where the actual data has been written in page
3269
 * @offset - same meaning as the read system call
3270
 * @count - same meaning as the read system call
3271
 * @eof - set if no more data needs to be returned
3272
 * @data - pointer to our soft state
3273
 *
3274
 * Display information about the physical drives on physical channel 1.
3275
 */
3276
static int
3277
proc_pdrv_ch1(char *page, char **start, off_t offset, int count, int *eof,
3278
                void *data)
3279
{
3280
        adapter_t *adapter = (adapter_t *)data;
3281
 
3282
        *eof = 1;
3283
 
3284
        return (proc_pdrv(adapter, page, 1));
3285
}
3286
 
3287
 
3288
/**
3289
 * proc_pdrv_ch2()
3290
 * @page - buffer to write the data in
3291
 * @start - where the actual data has been written in page
3292
 * @offset - same meaning as the read system call
3293
 * @count - same meaning as the read system call
3294
 * @eof - set if no more data needs to be returned
3295
 * @data - pointer to our soft state
3296
 *
3297
 * Display information about the physical drives on physical channel 2.
3298
 */
3299
static int
3300
proc_pdrv_ch2(char *page, char **start, off_t offset, int count, int *eof,
3301
                void *data)
3302
{
3303
        adapter_t *adapter = (adapter_t *)data;
3304
 
3305
        *eof = 1;
3306
 
3307
        return (proc_pdrv(adapter, page, 2));
3308
}
3309
 
3310
 
3311
/**
3312
 * proc_pdrv_ch3()
3313
 * @page - buffer to write the data in
3314
 * @start - where the actual data has been written in page
3315
 * @offset - same meaning as the read system call
3316
 * @count - same meaning as the read system call
3317
 * @eof - set if no more data needs to be returned
3318
 * @data - pointer to our soft state
3319
 *
3320
 * Display information about the physical drives on physical channel 3.
3321
 */
3322
static int
3323
proc_pdrv_ch3(char *page, char **start, off_t offset, int count, int *eof,
3324
                void *data)
3325
{
3326
        adapter_t *adapter = (adapter_t *)data;
3327
 
3328
        *eof = 1;
3329
 
3330
        return (proc_pdrv(adapter, page, 3));
3331
}
3332
 
3333
 
3334
/**
3335
 * proc_pdrv()
3336
 * @page - buffer to write the data in
3337
 * @adapter - pointer to our soft state
3338
 *
3339
 * Display information about the physical drives.
3340
 */
3341
static int
3342
proc_pdrv(adapter_t *adapter, char *page, int channel)
3343
{
3344
        dma_addr_t      dma_handle;
3345
        char            *scsi_inq;
3346
        dma_addr_t      scsi_inq_dma_handle;
3347
        caddr_t         inquiry;
3348
        struct pci_dev  *pdev;
3349
        u8      *pdrv_state;
3350
        u8      state;
3351
        int     tgt;
3352
        int     max_channels;
3353
        int     len = 0;
3354
        char    str[80];
3355
        int     i;
3356
 
3357
        pdev = adapter->dev;
3358
 
3359
        if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
3360
                return len;
3361
        }
3362
 
3363
        if( mega_adapinq(adapter, dma_handle) != 0 ) {
3364
 
3365
                len = sprintf(page, "Adapter inquiry failed.\n");
3366
 
3367
                printk(KERN_WARNING "megaraid: inquiry failed.\n");
3368
 
3369
                mega_free_inquiry(inquiry, dma_handle, pdev);
3370
 
3371
                return len;
3372
        }
3373
 
3374
 
3375
        scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
3376
 
3377
        if( scsi_inq == NULL ) {
3378
                len = sprintf(page, "memory not available for scsi inq.\n");
3379
 
3380
                mega_free_inquiry(inquiry, dma_handle, pdev);
3381
 
3382
                return len;
3383
        }
3384
 
3385
        if( adapter->flag & BOARD_40LD ) {
3386
                pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
3387
        }
3388
        else {
3389
                pdrv_state = ((mraid_ext_inquiry *)inquiry)->
3390
                        raid_inq.pdrv_info.pdrv_state;
3391
        }
3392
 
3393
        max_channels = adapter->product_info.nchannels;
3394
 
3395
        if( channel >= max_channels ) return 0;
3396
 
3397
        for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
3398
 
3399
                i = channel*16 + tgt;
3400
 
3401
                state = *(pdrv_state + i);
3402
 
3403
                switch( state & 0x0F ) {
3404
 
3405
                case PDRV_ONLINE:
3406
                        sprintf(str, "Channel:%2d Id:%2d State: Online",
3407
                                channel, tgt);
3408
                        break;
3409
 
3410
                case PDRV_FAILED:
3411
                        sprintf(str, "Channel:%2d Id:%2d State: Failed",
3412
                                channel, tgt);
3413
                        break;
3414
 
3415
                case PDRV_RBLD:
3416
                        sprintf(str, "Channel:%2d Id:%2d State: Rebuild",
3417
                                channel, tgt);
3418
                        break;
3419
 
3420
                case PDRV_HOTSPARE:
3421
                        sprintf(str, "Channel:%2d Id:%2d State: Hot spare",
3422
                                channel, tgt);
3423
                        break;
3424
 
3425
                default:
3426
                        sprintf(str, "Channel:%2d Id:%2d State: Un-configured",
3427
                                channel, tgt);
3428
                        break;
3429
 
3430
                }
3431
 
3432
                /*
3433
                 * This interface displays inquiries for disk drives
3434
                 * only. Inquries for logical drives and non-disk
3435
                 * devices are available through /proc/scsi/scsi
3436
                 */
3437
                memset(scsi_inq, 0, 256);
3438
                if( mega_internal_dev_inquiry(adapter, channel, tgt,
3439
                                scsi_inq_dma_handle) ||
3440
                                (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
3441
                        continue;
3442
                }
3443
 
3444
                /*
3445
                 * Check for overflow. We print less than 240
3446
                 * characters for inquiry
3447
                 */
3448
                if( (len + 240) >= PAGE_SIZE ) break;
3449
 
3450
                len += sprintf(page+len, "%s.\n", str);
3451
 
3452
                len += mega_print_inquiry(page+len, scsi_inq);
3453
        }
3454
 
3455
        pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
3456
 
3457
        mega_free_inquiry(inquiry, dma_handle, pdev);
3458
 
3459
        return len;
3460
}
3461
 
3462
 
3463
/*
3464
 * Display scsi inquiry
3465
 */
3466
static int
3467
mega_print_inquiry(char *page, char *scsi_inq)
3468
{
3469
        int     len = 0;
3470
        int     i;
3471
 
3472
        len = sprintf(page, "  Vendor: ");
3473
        for( i = 8; i < 16; i++ ) {
3474
                len += sprintf(page+len, "%c", scsi_inq[i]);
3475
        }
3476
 
3477
        len += sprintf(page+len, "  Model: ");
3478
 
3479
        for( i = 16; i < 32; i++ ) {
3480
                len += sprintf(page+len, "%c", scsi_inq[i]);
3481
        }
3482
 
3483
        len += sprintf(page+len, "  Rev: ");
3484
 
3485
        for( i = 32; i < 36; i++ ) {
3486
                len += sprintf(page+len, "%c", scsi_inq[i]);
3487
        }
3488
 
3489
        len += sprintf(page+len, "\n");
3490
 
3491
        i = scsi_inq[0] & 0x1f;
3492
 
3493
        len += sprintf(page+len, "  Type:   %s ",
3494
                i < MAX_SCSI_DEVICE_CODE ? scsi_device_types[i] :
3495
                   "Unknown          ");
3496
 
3497
        len += sprintf(page+len,
3498
        "                 ANSI SCSI revision: %02x", scsi_inq[2] & 0x07);
3499
 
3500
        if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
3501
                len += sprintf(page+len, " CCS\n");
3502
        else
3503
                len += sprintf(page+len, "\n");
3504
 
3505
        return len;
3506
}
3507
 
3508
 
3509
/**
3510
 * proc_rdrv_10()
3511
 * @page - buffer to write the data in
3512
 * @start - where the actual data has been written in page
3513
 * @offset - same meaning as the read system call
3514
 * @count - same meaning as the read system call
3515
 * @eof - set if no more data needs to be returned
3516
 * @data - pointer to our soft state
3517
 *
3518
 * Display real time information about the logical drives 0 through 9.
3519
 */
3520
static int
3521
proc_rdrv_10(char *page, char **start, off_t offset, int count, int *eof,
3522
                void *data)
3523
{
3524
        adapter_t *adapter = (adapter_t *)data;
3525
 
3526
        *eof = 1;
3527
 
3528
        return (proc_rdrv(adapter, page, 0, 9));
3529
}
3530
 
3531
 
3532
/**
3533
 * proc_rdrv_20()
3534
 * @page - buffer to write the data in
3535
 * @start - where the actual data has been written in page
3536
 * @offset - same meaning as the read system call
3537
 * @count - same meaning as the read system call
3538
 * @eof - set if no more data needs to be returned
3539
 * @data - pointer to our soft state
3540
 *
3541
 * Display real time information about the logical drives 10 through 19.
3542
 */
3543
static int
3544
proc_rdrv_20(char *page, char **start, off_t offset, int count, int *eof,
3545
                void *data)
3546
{
3547
        adapter_t *adapter = (adapter_t *)data;
3548
 
3549
        *eof = 1;
3550
 
3551
        return (proc_rdrv(adapter, page, 10, 19));
3552
}
3553
 
3554
 
3555
/**
3556
 * proc_rdrv_30()
3557
 * @page - buffer to write the data in
3558
 * @start - where the actual data has been written in page
3559
 * @offset - same meaning as the read system call
3560
 * @count - same meaning as the read system call
3561
 * @eof - set if no more data needs to be returned
3562
 * @data - pointer to our soft state
3563
 *
3564
 * Display real time information about the logical drives 20 through 29.
3565
 */
3566
static int
3567
proc_rdrv_30(char *page, char **start, off_t offset, int count, int *eof,
3568
                void *data)
3569
{
3570
        adapter_t *adapter = (adapter_t *)data;
3571
 
3572
        *eof = 1;
3573
 
3574
        return (proc_rdrv(adapter, page, 20, 29));
3575
}
3576
 
3577
 
3578
/**
3579
 * proc_rdrv_40()
3580
 * @page - buffer to write the data in
3581
 * @start - where the actual data has been written in page
3582
 * @offset - same meaning as the read system call
3583
 * @count - same meaning as the read system call
3584
 * @eof - set if no more data needs to be returned
3585
 * @data - pointer to our soft state
3586
 *
3587
 * Display real time information about the logical drives 30 through 39.
3588
 */
3589
static int
3590
proc_rdrv_40(char *page, char **start, off_t offset, int count, int *eof,
3591
                void *data)
3592
{
3593
        adapter_t *adapter = (adapter_t *)data;
3594
 
3595
        *eof = 1;
3596
 
3597
        return (proc_rdrv(adapter, page, 30, 39));
3598
}
3599
 
3600
 
3601
/**
3602
 * proc_rdrv()
3603
 * @page - buffer to write the data in
3604
 * @adapter - pointer to our soft state
3605
 * @start - starting logical drive to display
3606
 * @end - ending logical drive to display
3607
 *
3608
 * We do not print the inquiry information since its already available through
3609
 * /proc/scsi/scsi interface
3610
 */
3611
static int
3612
proc_rdrv(adapter_t *adapter, char *page, int start, int end)
3613
{
3614
        dma_addr_t      dma_handle;
3615
        logdrv_param    *lparam;
3616
        megacmd_t       mc;
3617
        char            *disk_array;
3618
        dma_addr_t      disk_array_dma_handle;
3619
        caddr_t         inquiry;
3620
        struct pci_dev  *pdev;
3621
        u8      *rdrv_state;
3622
        int     num_ldrv;
3623
        u32     array_sz;
3624
        int     len = 0;
3625
        int     i;
3626
        u8      span8_flag = 1;
3627
 
3628
        pdev = adapter->dev;
3629
 
3630
        if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL ) {
3631
                return len;
3632
        }
3633
 
3634
        if( mega_adapinq(adapter, dma_handle) != 0 ) {
3635
 
3636
                len = sprintf(page, "Adapter inquiry failed.\n");
3637
 
3638
                printk(KERN_WARNING "megaraid: inquiry failed.\n");
3639
 
3640
                mega_free_inquiry(inquiry, dma_handle, pdev);
3641
 
3642
                return len;
3643
        }
3644
 
3645
        memset(&mc, 0, sizeof(megacmd_t));
3646
 
3647
        if( adapter->flag & BOARD_40LD ) {
3648
 
3649
                array_sz = sizeof(disk_array_40ld);
3650
 
3651
                rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
3652
 
3653
                num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
3654
        }
3655
        else {
3656
                /*
3657
                 * 'array_sz' is either the size of diskarray_span4_t or the
3658
                 * size of disk_array_span8_t. We use span8_t's size because
3659
                 * it is bigger of the two.
3660
                 */
3661
                array_sz = sizeof( diskarray_span8_t );
3662
 
3663
                rdrv_state = ((mraid_ext_inquiry *)inquiry)->
3664
                        raid_inq.logdrv_info.ldrv_state;
3665
 
3666
                num_ldrv = ((mraid_ext_inquiry *)inquiry)->
3667
                        raid_inq.logdrv_info.num_ldrv;
3668
        }
3669
 
3670
        disk_array = pci_alloc_consistent(pdev, array_sz,
3671
                        &disk_array_dma_handle);
3672
 
3673
        if( disk_array == NULL ) {
3674
                len = sprintf(page, "memory not available.\n");
3675
 
3676
                mega_free_inquiry(inquiry, dma_handle, pdev);
3677
 
3678
                return len;
3679
        }
3680
 
3681
        mc.xferaddr = (u32)disk_array_dma_handle;
3682
 
3683
        if( adapter->flag & BOARD_40LD ) {
3684
                mc.cmd = FC_NEW_CONFIG;
3685
                mc.opcode = OP_DCMD_READ_CONFIG;
3686
 
3687
                if( mega_internal_command(adapter, LOCK_INT, &mc, NULL) ) {
3688
 
3689
                        len = sprintf(page, "40LD read config failed.\n");
3690
 
3691
                        mega_free_inquiry(inquiry, dma_handle, pdev);
3692
 
3693
                        pci_free_consistent(pdev, array_sz, disk_array,
3694
                                        disk_array_dma_handle);
3695
 
3696
                        return len;
3697
                }
3698
 
3699
        }
3700
        else {
3701
                /*
3702
                 * Try 8-Span "read config" command
3703
                 */
3704
                mc.cmd = NEW_READ_CONFIG_8LD;
3705
 
3706
                if( mega_internal_command(adapter, LOCK_INT, &mc, NULL) ) {
3707
 
3708
                        /*
3709
                         * 8-Span command failed; try 4-Span command
3710
                         */
3711
                        span8_flag = 0;
3712
                        mc.cmd = READ_CONFIG_8LD;
3713
 
3714
                        if( mega_internal_command(adapter, LOCK_INT, &mc,
3715
                                                NULL) ){
3716
 
3717
                                len = sprintf(page,
3718
                                        "8LD read config failed.\n");
3719
 
3720
                                mega_free_inquiry(inquiry, dma_handle, pdev);
3721
 
3722
                                pci_free_consistent(pdev, array_sz,
3723
                                                disk_array,
3724
                                                disk_array_dma_handle);
3725
 
3726
                                return len;
3727
                        }
3728
                }
3729
        }
3730
 
3731
        for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
3732
 
3733
                if( adapter->flag & BOARD_40LD ) {
3734
                        lparam =
3735
                        &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
3736
                }
3737
                else {
3738
                        if( span8_flag ) {
3739
                                lparam = (logdrv_param*) &((diskarray_span8_t*)
3740
                                                (disk_array))->log_drv[i];
3741
                        }
3742
                        else {
3743
                                lparam = (logdrv_param*) &((diskarray_span4_t*)
3744
                                                (disk_array))->log_drv[i];
3745
                        }
3746
                }
3747
 
3748
                /*
3749
                 * Check for overflow. We print less than 240 characters for
3750
                 * information about each logical drive.
3751
                 */
3752
                if( (len + 240) >= PAGE_SIZE ) break;
3753
 
3754
                len += sprintf(page+len, "Logical drive:%2d:, ", i);
3755
 
3756
                switch( rdrv_state[i] & 0x0F ) {
3757
                case RDRV_OFFLINE:
3758
                        len += sprintf(page+len, "state: offline");
3759
                        break;
3760
 
3761
                case RDRV_DEGRADED:
3762
                        len += sprintf(page+len, "state: degraded");
3763
                        break;
3764
 
3765
                case RDRV_OPTIMAL:
3766
                        len += sprintf(page+len, "state: optimal");
3767
                        break;
3768
 
3769
                case RDRV_DELETED:
3770
                        len += sprintf(page+len, "state: deleted");
3771
                        break;
3772
 
3773
                default:
3774
                        len += sprintf(page+len, "state: unknown");
3775
                        break;
3776
                }
3777
 
3778
                /*
3779
                 * Check if check consistency or initialization is going on
3780
                 * for this logical drive.
3781
                 */
3782
                if( (rdrv_state[i] & 0xF0) == 0x20 ) {
3783
                        len += sprintf(page+len,
3784
                                        ", check-consistency in progress");
3785
                }
3786
                else if( (rdrv_state[i] & 0xF0) == 0x10 ) {
3787
                        len += sprintf(page+len,
3788
                                        ", initialization in progress");
3789
                }
3790
 
3791
                len += sprintf(page+len, "\n");
3792
 
3793
                len += sprintf(page+len, "Span depth:%3d, ",
3794
                                lparam->span_depth);
3795
 
3796
                len += sprintf(page+len, "RAID level:%3d, ",
3797
                                lparam->level);
3798
 
3799
                len += sprintf(page+len, "Stripe size:%3d, ",
3800
                                lparam->stripe_sz ? lparam->stripe_sz/2: 128);
3801
 
3802
                len += sprintf(page+len, "Row size:%3d\n",
3803
                                lparam->row_size);
3804
 
3805
 
3806
                len += sprintf(page+len, "Read Policy: ");
3807
 
3808
                switch(lparam->read_ahead) {
3809
 
3810
                case NO_READ_AHEAD:
3811
                        len += sprintf(page+len, "No read ahead, ");
3812
                        break;
3813
 
3814
                case READ_AHEAD:
3815
                        len += sprintf(page+len, "Read ahead, ");
3816
                        break;
3817
 
3818
                case ADAP_READ_AHEAD:
3819
                        len += sprintf(page+len, "Adaptive, ");
3820
                        break;
3821
 
3822
                }
3823
 
3824
                len += sprintf(page+len, "Write Policy: ");
3825
 
3826
                switch(lparam->write_mode) {
3827
 
3828
                case WRMODE_WRITE_THRU:
3829
                        len += sprintf(page+len, "Write thru, ");
3830
                        break;
3831
 
3832
                case WRMODE_WRITE_BACK:
3833
                        len += sprintf(page+len, "Write back, ");
3834
                        break;
3835
                }
3836
 
3837
                len += sprintf(page+len, "Cache Policy: ");
3838
 
3839
                switch(lparam->direct_io) {
3840
 
3841
                case CACHED_IO:
3842
                        len += sprintf(page+len, "Cached IO\n\n");
3843
                        break;
3844
 
3845
                case DIRECT_IO:
3846
                        len += sprintf(page+len, "Direct IO\n\n");
3847
                        break;
3848
                }
3849
        }
3850
 
3851
        mega_free_inquiry(inquiry, dma_handle, pdev);
3852
 
3853
        pci_free_consistent(pdev, array_sz, disk_array,
3854
                        disk_array_dma_handle);
3855
 
3856
        return len;
3857
}
3858
 
3859
#endif
3860
 
3861
 
3862
/**
3863
 * megaraid_biosparam()
3864
 * @disk
3865
 * @dev
3866
 * @geom
3867
 *
3868
 * Return the disk geometry for a particular disk
3869
 * Input:
3870
 *      Disk *disk - Disk geometry
3871
 *      kdev_t dev - Device node
3872
 *      int *geom  - Returns geometry fields
3873
 *              geom[0] = heads
3874
 *              geom[1] = sectors
3875
 *              geom[2] = cylinders
3876
 */
3877
static int
3878
megaraid_biosparam(Disk *disk, kdev_t dev, int *geom)
3879
{
3880
        int heads, sectors, cylinders;
3881
        adapter_t *adapter;
3882
 
3883
        /* Get pointer to host config structure */
3884
        adapter = (adapter_t *)disk->device->host->hostdata;
3885
 
3886
        if (IS_RAID_CH(adapter, disk->device->channel)) {
3887
                        /* Default heads (64) & sectors (32) */
3888
                        heads = 64;
3889
                        sectors = 32;
3890
                        cylinders = disk->capacity / (heads * sectors);
3891
 
3892
                        /*
3893
                         * Handle extended translation size for logical drives
3894
                         * > 1Gb
3895
                         */
3896
                        if (disk->capacity >= 0x200000) {
3897
                                heads = 255;
3898
                                sectors = 63;
3899
                                cylinders = disk->capacity / (heads * sectors);
3900
                        }
3901
 
3902
                        /* return result */
3903
                        geom[0] = heads;
3904
                        geom[1] = sectors;
3905
                        geom[2] = cylinders;
3906
        }
3907
        else {
3908
                if( !mega_partsize(disk, dev, geom) )
3909
                        return 0;
3910
 
3911
                printk(KERN_WARNING
3912
                "megaraid: invalid partition on this disk on channel %d\n",
3913
                                disk->device->channel);
3914
 
3915
                /* Default heads (64) & sectors (32) */
3916
                heads = 64;
3917
                sectors = 32;
3918
                cylinders = disk->capacity / (heads * sectors);
3919
 
3920
                /* Handle extended translation size for logical drives > 1Gb */
3921
                if (disk->capacity >= 0x200000) {
3922
                        heads = 255;
3923
                        sectors = 63;
3924
                        cylinders = disk->capacity / (heads * sectors);
3925
                }
3926
 
3927
                /* return result */
3928
                geom[0] = heads;
3929
                geom[1] = sectors;
3930
                geom[2] = cylinders;
3931
        }
3932
 
3933
        return 0;
3934
}
3935
 
3936
/*
3937
 * mega_partsize()
3938
 * @disk
3939
 * @geom
3940
 *
3941
 * Purpose : to determine the BIOS mapping used to create the partition
3942
 *      table, storing the results (cyls, hds, and secs) in geom
3943
 *
3944
 * Note:        Code is picked from scsicam.h
3945
 *
3946
 * Returns : -1 on failure, 0 on success.
3947
 */
3948
static int
3949
mega_partsize(Disk *disk, kdev_t dev, int *geom)
3950
{
3951
        struct buffer_head *bh;
3952
        struct partition *p, *largest = NULL;
3953
        int i, largest_cyl;
3954
        int heads, cyls, sectors;
3955
        int capacity = disk->capacity;
3956
 
3957
        int ma = MAJOR(dev);
3958
        int mi = (MINOR(dev) & ~0xf);
3959
 
3960
        int block = 1024;
3961
 
3962
        if (blksize_size[ma])
3963
                block = blksize_size[ma][mi];
3964
 
3965
        if (!(bh = bread(MKDEV(ma,mi), 0, block)))
3966
                return -1;
3967
 
3968
        if (*(unsigned short *)(bh->b_data + 510) == 0xAA55 ) {
3969
 
3970
                for (largest_cyl = -1,
3971
                        p = (struct partition *)(0x1BE + bh->b_data), i = 0;
3972
                        i < 4; ++i, ++p) {
3973
 
3974
                        if (!p->sys_ind) continue;
3975
 
3976
                        cyls = p->end_cyl + ((p->end_sector & 0xc0) << 2);
3977
 
3978
                        if (cyls >= largest_cyl) {
3979
                                largest_cyl = cyls;
3980
                                largest = p;
3981
                        }
3982
                }
3983
        }
3984
 
3985
        if (largest) {
3986
                heads = largest->end_head + 1;
3987
                sectors = largest->end_sector & 0x3f;
3988
 
3989
                if (!heads || !sectors) {
3990
                        brelse(bh);
3991
                        return -1;
3992
                }
3993
 
3994
                cyls = capacity/(heads * sectors);
3995
 
3996
                geom[0] = heads;
3997
                geom[1] = sectors;
3998
                geom[2] = cyls;
3999
 
4000
                brelse(bh);
4001
                return 0;
4002
        }
4003
 
4004
        brelse(bh);
4005
        return -1;
4006
}
4007
 
4008
 
4009
/**
4010
 * megaraid_reboot_notify()
4011
 * @this - unused
4012
 * @code - shutdown code
4013
 * @unused - unused
4014
 *
4015
 * This routine will be called when the use has done a forced shutdown on the
4016
 * system. Flush the Adapter and disks cache.
4017
 */
4018
static int
4019
megaraid_reboot_notify (struct notifier_block *this, unsigned long code,
4020
                void *unused)
4021
{
4022
        adapter_t *adapter;
4023
        struct Scsi_Host *host;
4024
        u8 raw_mbox[sizeof(mbox_t)];
4025
        mbox_t *mbox;
4026
        int i;
4027
 
4028
        /*
4029
         * Flush the controller's cache irrespective of the codes coming down.
4030
         * SYS_DOWN, SYS_HALT, SYS_RESTART, SYS_POWER_OFF
4031
         */
4032
        for( i = 0; i < hba_count; i++ ) {
4033
                printk(KERN_INFO "megaraid: flushing adapter %d..", i);
4034
                host = hba_soft_state[i]->host;
4035
 
4036
                adapter = (adapter_t *)host->hostdata;
4037
                mbox = (mbox_t *)raw_mbox;
4038
 
4039
                /* Flush adapter cache */
4040
                memset(raw_mbox, 0, sizeof(raw_mbox));
4041
                raw_mbox[0] = FLUSH_ADAPTER;
4042
 
4043
                irq_disable(adapter);
4044
                free_irq(adapter->host->irq, adapter);
4045
 
4046
                /*
4047
                 * Issue a blocking (interrupts disabled) command to
4048
                 * the card
4049
                 */
4050
                issue_scb_block(adapter, raw_mbox);
4051
 
4052
                /* Flush disks cache */
4053
                memset(raw_mbox, 0, sizeof(raw_mbox));
4054
                raw_mbox[0] = FLUSH_SYSTEM;
4055
 
4056
                issue_scb_block(adapter, raw_mbox);
4057
 
4058
                printk("Done.\n");
4059
 
4060
                if( atomic_read(&adapter->pend_cmds) > 0 ) {
4061
                        printk(KERN_WARNING "megaraid: pending commands!!\n");
4062
                }
4063
        }
4064
 
4065
        /*
4066
         * Have a delibrate delay to make sure all the caches are
4067
         * actually flushed.
4068
         */
4069
        printk(KERN_INFO "megaraid: cache flush delay:   ");
4070
        for( i = 9; i >= 0; i-- ) {
4071
                printk("\b\b\b[%d]", i);
4072
                mdelay(1000);
4073
        }
4074
        printk("\b\b\b[done]\n");
4075
        mdelay(1000);
4076
 
4077
        return NOTIFY_DONE;
4078
}
4079
 
4080
/**
4081
 * mega_init_scb()
4082
 * @adapter - pointer to our soft state
4083
 *
4084
 * Allocate memory for the various pointers in the scb structures:
4085
 * scatter-gather list pointer, passthru and extended passthru structure
4086
 * pointers.
4087
 */
4088
static int
4089
mega_init_scb(adapter_t *adapter)
4090
{
4091
        scb_t   *scb;
4092
        int     i;
4093
 
4094
        for( i = 0; i < adapter->max_cmds; i++ ) {
4095
 
4096
                scb = &adapter->scb_list[i];
4097
 
4098
                scb->sgl64 = NULL;
4099
                scb->sgl = NULL;
4100
                scb->pthru = NULL;
4101
                scb->epthru = NULL;
4102
        }
4103
 
4104
        for( i = 0; i < adapter->max_cmds; i++ ) {
4105
 
4106
                scb = &adapter->scb_list[i];
4107
 
4108
                scb->idx = i;
4109
 
4110
                scb->sgl64 = pci_alloc_consistent(adapter->dev,
4111
                                sizeof(mega_sgl64) * adapter->sglen,
4112
                                &scb->sgl_dma_addr);
4113
 
4114
                scb->sgl = (mega_sglist *)scb->sgl64;
4115
 
4116
                if( !scb->sgl ) {
4117
                        printk(KERN_WARNING "RAID: Can't allocate sglist.\n");
4118
                        mega_free_sgl(adapter);
4119
                        return -1;
4120
                }
4121
 
4122
                scb->pthru = pci_alloc_consistent(adapter->dev,
4123
                                sizeof(mega_passthru),
4124
                                &scb->pthru_dma_addr);
4125
 
4126
                if( !scb->pthru ) {
4127
                        printk(KERN_WARNING "RAID: Can't allocate passthru.\n");
4128
                        mega_free_sgl(adapter);
4129
                        return -1;
4130
                }
4131
 
4132
                scb->epthru = pci_alloc_consistent(adapter->dev,
4133
                                sizeof(mega_ext_passthru),
4134
                                &scb->epthru_dma_addr);
4135
 
4136
                if( !scb->epthru ) {
4137
                        printk(KERN_WARNING
4138
                                "Can't allocate extended passthru.\n");
4139
                        mega_free_sgl(adapter);
4140
                        return -1;
4141
                }
4142
 
4143
 
4144
                scb->dma_type = MEGA_DMA_TYPE_NONE;
4145
 
4146
                /*
4147
                 * Link to free list
4148
                 * lock not required since we are loading the driver, so no
4149
                 * commands possible right now.
4150
                 */
4151
                scb->state = SCB_FREE;
4152
                scb->cmd = NULL;
4153
                list_add(&scb->list, &adapter->free_list);
4154
        }
4155
 
4156
        return 0;
4157
}
4158
 
4159
 
4160
/**
4161
 * megadev_open()
4162
 * @inode - unused
4163
 * @filep - unused
4164
 *
4165
 * Routines for the character/ioctl interface to the driver. Find out if this
4166
 * is a valid open. If yes, increment the module use count so that it cannot
4167
 * be unloaded.
4168
 */
4169
static int
4170
megadev_open (struct inode *inode, struct file *filep)
4171
{
4172
        /*
4173
         * Only allow superuser to access private ioctl interface
4174
         */
4175
        if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
4176
 
4177
        MOD_INC_USE_COUNT;
4178
        return 0;
4179
}
4180
 
4181
 
4182
/**
4183
 * megadev_ioctl()
4184
 * @inode - Our device inode
4185
 * @filep - unused
4186
 * @cmd - ioctl command
4187
 * @arg - user buffer
4188
 *
4189
 * ioctl entry point for our private ioctl interface. We move the data in from
4190
 * the user space, prepare the command (if necessary, convert the old MIMD
4191
 * ioctl to new ioctl command), and issue a synchronous command to the
4192
 * controller.
4193
 */
4194
static int
4195
megadev_ioctl(struct inode *inode, struct file *filep, unsigned int cmd,
4196
                unsigned long arg)
4197
{
4198
        adapter_t       *adapter;
4199
        nitioctl_t      uioc;
4200
        int             adapno;
4201
        int             rval;
4202
        mega_passthru   *upthru;        /* user address for passthru */
4203
        mega_passthru   *pthru;         /* copy user passthru here */
4204
        dma_addr_t      pthru_dma_hndl;
4205
        void            *data = NULL;   /* data to be transferred */
4206
        dma_addr_t      data_dma_hndl;  /* dma handle for data xfer area */
4207
        megacmd_t       mc;
4208
        megastat_t      *ustats;
4209
        int             num_ldrv;
4210
        u32             uxferaddr = 0;
4211
        struct pci_dev  *pdev;
4212
 
4213
        ustats = NULL; /* avoid compilation warnings */
4214
        num_ldrv = 0;
4215
 
4216
        /*
4217
         * Make sure only USCSICMD are issued through this interface.
4218
         * MIMD application would still fire different command.
4219
         */
4220
        if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
4221
                return -EINVAL;
4222
        }
4223
 
4224
        /*
4225
         * Check and convert a possible MIMD command to NIT command.
4226
         * mega_m_to_n() copies the data from the user space, so we do not
4227
         * have to do it here.
4228
         * NOTE: We will need some user address to copyout the data, therefore
4229
         * the inteface layer will also provide us with the required user
4230
         * addresses.
4231
         */
4232
        memset(&uioc, 0, sizeof(nitioctl_t));
4233
        if( (rval = mega_m_to_n( (void *)arg, &uioc)) != 0 )
4234
                return rval;
4235
 
4236
 
4237
        switch( uioc.opcode ) {
4238
 
4239
        case GET_DRIVER_VER:
4240
                if( put_user(driver_ver, (u32 *)uioc.uioc_uaddr) )
4241
                        return (-EFAULT);
4242
 
4243
                break;
4244
 
4245
        case GET_N_ADAP:
4246
                if( put_user(hba_count, (u32 *)uioc.uioc_uaddr) )
4247
                        return (-EFAULT);
4248
 
4249
                /*
4250
                 * Shucks. MIMD interface returns a positive value for number
4251
                 * of adapters. TODO: Change it to return 0 when there is no
4252
                 * applicatio using mimd interface.
4253
                 */
4254
                return hba_count;
4255
 
4256
        case GET_ADAP_INFO:
4257
 
4258
                /*
4259
                 * Which adapter
4260
                 */
4261
                if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
4262
                        return (-ENODEV);
4263
 
4264
                if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
4265
                                sizeof(struct mcontroller)) )
4266
                        return (-EFAULT);
4267
                break;
4268
 
4269
#if MEGA_HAVE_STATS
4270
 
4271
        case GET_STATS:
4272
                /*
4273
                 * Which adapter
4274
                 */
4275
                if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
4276
                        return (-ENODEV);
4277
 
4278
                adapter = hba_soft_state[adapno];
4279
 
4280
                ustats = (megastat_t *)uioc.uioc_uaddr;
4281
 
4282
                if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
4283
                        return (-EFAULT);
4284
 
4285
                /*
4286
                 * Check for the validity of the logical drive number
4287
                 */
4288
                if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
4289
 
4290
                if( copy_to_user(ustats->nreads, adapter->nreads,
4291
                                        num_ldrv*sizeof(u32)) )
4292
                        return -EFAULT;
4293
 
4294
                if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
4295
                                        num_ldrv*sizeof(u32)) )
4296
                        return -EFAULT;
4297
 
4298
                if( copy_to_user(ustats->nwrites, adapter->nwrites,
4299
                                        num_ldrv*sizeof(u32)) )
4300
                        return -EFAULT;
4301
 
4302
                if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
4303
                                        num_ldrv*sizeof(u32)) )
4304
                        return -EFAULT;
4305
 
4306
                if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
4307
                                        num_ldrv*sizeof(u32)) )
4308
                        return -EFAULT;
4309
 
4310
                if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
4311
                                        num_ldrv*sizeof(u32)) )
4312
                        return -EFAULT;
4313
 
4314
                return 0;
4315
 
4316
#endif
4317
        case MBOX_CMD:
4318
 
4319
                /*
4320
                 * Which adapter
4321
                 */
4322
                if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
4323
                        return (-ENODEV);
4324
 
4325
                adapter = hba_soft_state[adapno];
4326
 
4327
                /*
4328
                 * Deletion of logical drive is a special case. The adapter
4329
                 * should be quiescent before this command is issued.
4330
                 */
4331
                if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
4332
                                uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
4333
 
4334
                        /*
4335
                         * Do we support this feature
4336
                         */
4337
                        if( !adapter->support_random_del ) {
4338
                                printk(KERN_WARNING "megaraid: logdrv ");
4339
                                printk("delete on non-supporting F/W.\n");
4340
 
4341
                                return (-EINVAL);
4342
                        }
4343
 
4344
                        rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
4345
 
4346
                        if( rval == 0 ) {
4347
                                memset(&mc, 0, sizeof(megacmd_t));
4348
 
4349
                                mc.status = rval;
4350
 
4351
                                rval = mega_n_to_m((void *)arg, &mc);
4352
                        }
4353
 
4354
                        return rval;
4355
                }
4356
                /*
4357
                 * This interface only support the regular passthru commands.
4358
                 * Reject extended passthru and 64-bit passthru
4359
                 */
4360
                if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
4361
                        uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
4362
 
4363
                        printk(KERN_WARNING "megaraid: rejected passthru.\n");
4364
 
4365
                        return (-EINVAL);
4366
                }
4367
 
4368
                /*
4369
                 * For all internal commands, the buffer must be allocated in
4370
                 * <4GB address range
4371
                 */
4372
                pdev = adapter->dev;
4373
 
4374
                /* Is it a passthru command or a DCMD */
4375
                if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
4376
                        /* Passthru commands */
4377
 
4378
                        pthru = pci_alloc_consistent(pdev,
4379
                                        sizeof(mega_passthru),
4380
                                        &pthru_dma_hndl);
4381
 
4382
                        if( pthru == NULL ) {
4383
                                return (-ENOMEM);
4384
                        }
4385
 
4386
                        /*
4387
                         * The user passthru structure
4388
                         */
4389
                        upthru = (mega_passthru *)MBOX(uioc)->xferaddr;
4390
 
4391
                        /*
4392
                         * Copy in the user passthru here.
4393
                         */
4394
                        if( copy_from_user(pthru, (char *)upthru,
4395
                                                sizeof(mega_passthru)) ) {
4396
 
4397
                                pci_free_consistent(pdev,
4398
                                                sizeof(mega_passthru), pthru,
4399
                                                pthru_dma_hndl);
4400
 
4401
                                return (-EFAULT);
4402
                        }
4403
 
4404
                        /*
4405
                         * Is there a data transfer
4406
                         */
4407
                        if( pthru->dataxferlen ) {
4408
                                data = pci_alloc_consistent(pdev,
4409
                                                pthru->dataxferlen,
4410
                                                &data_dma_hndl);
4411
 
4412
                                if( data == NULL ) {
4413
                                        pci_free_consistent(pdev,
4414
                                                        sizeof(mega_passthru),
4415
                                                        pthru,
4416
                                                        pthru_dma_hndl);
4417
 
4418
                                        return (-ENOMEM);
4419
                                }
4420
 
4421
                                /*
4422
                                 * Save the user address and point the kernel
4423
                                 * address at just allocated memory
4424
                                 */
4425
                                uxferaddr = pthru->dataxferaddr;
4426
                                pthru->dataxferaddr = data_dma_hndl;
4427
                        }
4428
 
4429
 
4430
                        /*
4431
                         * Is data coming down-stream
4432
                         */
4433
                        if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
4434
                                /*
4435
                                 * Get the user data
4436
                                 */
4437
                                if( copy_from_user(data, (char *)uxferaddr,
4438
                                                        pthru->dataxferlen) ) {
4439
                                        rval = (-EFAULT);
4440
                                        goto freemem_and_return;
4441
                                }
4442
                        }
4443
 
4444
                        memset(&mc, 0, sizeof(megacmd_t));
4445
 
4446
                        mc.cmd = MEGA_MBOXCMD_PASSTHRU;
4447
                        mc.xferaddr = (u32)pthru_dma_hndl;
4448
 
4449
                        /*
4450
                         * Issue the command
4451
                         */
4452
                        mega_internal_command(adapter, LOCK_INT, &mc, pthru);
4453
 
4454
                        rval = mega_n_to_m((void *)arg, &mc);
4455
 
4456
                        if( rval ) goto freemem_and_return;
4457
 
4458
 
4459
                        /*
4460
                         * Is data going up-stream
4461
                         */
4462
                        if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
4463
                                if( copy_to_user((char *)uxferaddr, data,
4464
                                                        pthru->dataxferlen) ) {
4465
                                        rval = (-EFAULT);
4466
                                }
4467
                        }
4468
 
4469
                        /*
4470
                         * Send the request sense data also, irrespective of
4471
                         * whether the user has asked for it or not.
4472
                         */
4473
                        copy_to_user(upthru->reqsensearea,
4474
                                        pthru->reqsensearea, 14);
4475
 
4476
freemem_and_return:
4477
                        if( pthru->dataxferlen ) {
4478
                                pci_free_consistent(pdev,
4479
                                                pthru->dataxferlen, data,
4480
                                                data_dma_hndl);
4481
                        }
4482
 
4483
                        pci_free_consistent(pdev, sizeof(mega_passthru),
4484
                                        pthru, pthru_dma_hndl);
4485
 
4486
                        return rval;
4487
                }
4488
                else {
4489
                        /* DCMD commands */
4490
 
4491
                        /*
4492
                         * Is there a data transfer
4493
                         */
4494
                        if( uioc.xferlen ) {
4495
                                data = pci_alloc_consistent(pdev,
4496
                                                uioc.xferlen, &data_dma_hndl);
4497
 
4498
                                if( data == NULL ) {
4499
                                        return (-ENOMEM);
4500
                                }
4501
 
4502
                                uxferaddr = MBOX(uioc)->xferaddr;
4503
                        }
4504
 
4505
                        /*
4506
                         * Is data coming down-stream
4507
                         */
4508
                        if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
4509
                                /*
4510
                                 * Get the user data
4511
                                 */
4512
                                if( copy_from_user(data, (char *)uxferaddr,
4513
                                                        uioc.xferlen) ) {
4514
 
4515
                                        pci_free_consistent(pdev,
4516
                                                        uioc.xferlen,
4517
                                                        data, data_dma_hndl);
4518
 
4519
                                        return (-EFAULT);
4520
                                }
4521
                        }
4522
 
4523
                        memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
4524
 
4525
                        mc.xferaddr = (u32)data_dma_hndl;
4526
 
4527
                        /*
4528
                         * Issue the command
4529
                         */
4530
                        mega_internal_command(adapter, LOCK_INT, &mc, NULL);
4531
 
4532
                        rval = mega_n_to_m((void *)arg, &mc);
4533
 
4534
                        if( rval ) {
4535
                                if( uioc.xferlen ) {
4536
                                        pci_free_consistent(pdev,
4537
                                                        uioc.xferlen, data,
4538
                                                        data_dma_hndl);
4539
                                }
4540
 
4541
                                return rval;
4542
                        }
4543
 
4544
                        /*
4545
                         * Is data going up-stream
4546
                         */
4547
                        if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
4548
                                if( copy_to_user((char *)uxferaddr, data,
4549
                                                        uioc.xferlen) ) {
4550
 
4551
                                        rval = (-EFAULT);
4552
                                }
4553
                        }
4554
 
4555
                        if( uioc.xferlen ) {
4556
                                pci_free_consistent(pdev,
4557
                                                uioc.xferlen, data,
4558
                                                data_dma_hndl);
4559
                        }
4560
 
4561
                        return rval;
4562
                }
4563
 
4564
        default:
4565
                return (-EINVAL);
4566
        }
4567
 
4568
        return 0;
4569
}
4570
 
4571
/**
4572
 * mega_m_to_n()
4573
 * @arg - user address
4574
 * @uioc - new ioctl structure
4575
 *
4576
 * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
4577
 * structure
4578
 *
4579
 * Converts the older mimd ioctl structure to newer NIT structure
4580
 */
4581
static int
4582
mega_m_to_n(void *arg, nitioctl_t *uioc)
4583
{
4584
        struct uioctl_t uioc_mimd;
4585
        char    signature[8] = {0};
4586
        u8      opcode;
4587
        u8      subopcode;
4588
 
4589
 
4590
        /*
4591
         * check is the application conforms to NIT. We do not have to do much
4592
         * in that case.
4593
         * We exploit the fact that the signature is stored in the very
4594
         * begining of the structure.
4595
         */
4596
 
4597
        if( copy_from_user(signature, (char *)arg, 7) )
4598
                return (-EFAULT);
4599
 
4600
        if( memcmp(signature, "MEGANIT", 7) == 0 ) {
4601
 
4602
                /*
4603
                 * NOTE NOTE: The nit ioctl is still under flux because of
4604
                 * change of mailbox definition, in HPE. No applications yet
4605
                 * use this interface and let's not have applications use this
4606
                 * interface till the new specifitions are in place.
4607
                 */
4608
                return -EINVAL;
4609
#if 0
4610
                if( copy_from_user(uioc, (char *)arg, sizeof(nitioctl_t)) )
4611
                        return (-EFAULT);
4612
                return 0;
4613
#endif
4614
        }
4615
 
4616
        /*
4617
         * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
4618
         *
4619
         * Get the user ioctl structure
4620
         */
4621
        if( copy_from_user(&uioc_mimd, (char *)arg, sizeof(struct uioctl_t)) )
4622
                return (-EFAULT);
4623
 
4624
 
4625
        /*
4626
         * Get the opcode and subopcode for the commands
4627
         */
4628
        opcode = uioc_mimd.ui.fcs.opcode;
4629
        subopcode = uioc_mimd.ui.fcs.subopcode;
4630
 
4631
        switch (opcode) {
4632
        case 0x82:
4633
 
4634
                switch (subopcode) {
4635
 
4636
                case MEGAIOC_QDRVRVER:  /* Query driver version */
4637
                        uioc->opcode = GET_DRIVER_VER;
4638
                        uioc->uioc_uaddr = uioc_mimd.data;
4639
                        break;
4640
 
4641
                case MEGAIOC_QNADAP:    /* Get # of adapters */
4642
                        uioc->opcode = GET_N_ADAP;
4643
                        uioc->uioc_uaddr = uioc_mimd.data;
4644
                        break;
4645
 
4646
                case MEGAIOC_QADAPINFO: /* Get adapter information */
4647
                        uioc->opcode = GET_ADAP_INFO;
4648
                        uioc->adapno = uioc_mimd.ui.fcs.adapno;
4649
                        uioc->uioc_uaddr = uioc_mimd.data;
4650
                        break;
4651
 
4652
                default:
4653
                        return(-EINVAL);
4654
                }
4655
 
4656
                break;
4657
 
4658
 
4659
        case 0x81:
4660
 
4661
                uioc->opcode = MBOX_CMD;
4662
                uioc->adapno = uioc_mimd.ui.fcs.adapno;
4663
 
4664
                memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
4665
 
4666
                uioc->xferlen = uioc_mimd.ui.fcs.length;
4667
 
4668
                if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
4669
                if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
4670
 
4671
                break;
4672
 
4673
        case 0x80:
4674
 
4675
                uioc->opcode = MBOX_CMD;
4676
                uioc->adapno = uioc_mimd.ui.fcs.adapno;
4677
 
4678
                memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
4679
 
4680
                /*
4681
                 * Choose the xferlen bigger of input and output data
4682
                 */
4683
                uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
4684
                        uioc_mimd.outlen : uioc_mimd.inlen;
4685
 
4686
                if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
4687
                if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
4688
 
4689
                break;
4690
 
4691
        default:
4692
                return (-EINVAL);
4693
 
4694
        }
4695
 
4696
        return 0;
4697
}
4698
 
4699
/*
4700
 * mega_n_to_m()
4701
 * @arg - user address
4702
 * @mc - mailbox command
4703
 *
4704
 * Updates the status information to the application, depending on application
4705
 * conforms to older mimd ioctl interface or newer NIT ioctl interface
4706
 */
4707
static int
4708
mega_n_to_m(void *arg, megacmd_t *mc)
4709
{
4710
        nitioctl_t      *uiocp;
4711
        megacmd_t       *umc;
4712
        megacmd_t       kmc;
4713
        mega_passthru   *upthru;
4714
        struct uioctl_t *uioc_mimd;
4715
        char    signature[8] = {0};
4716
 
4717
        /*
4718
         * check is the application conforms to NIT.
4719
         */
4720
        if( copy_from_user(signature, (char *)arg, 7) )
4721
                return -EFAULT;
4722
 
4723
        if( memcmp(signature, "MEGANIT", 7) == 0 ) {
4724
 
4725
                uiocp = (nitioctl_t *)arg;
4726
 
4727
                if( put_user(mc->status, (u8 *)&MBOX_P(uiocp)->status) )
4728
                        return (-EFAULT);
4729
 
4730
                if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
4731
 
4732
                        umc = MBOX_P(uiocp);
4733
 
4734
                        upthru = (mega_passthru *)umc->xferaddr;
4735
 
4736
                        if( put_user(mc->status, (u8 *)&upthru->scsistatus) )
4737
                                return (-EFAULT);
4738
                }
4739
        }
4740
        else {
4741
                uioc_mimd = (struct uioctl_t *)arg;
4742
 
4743
                if( put_user(mc->status, (u8 *)&uioc_mimd->mbox[17]) )
4744
                        return (-EFAULT);
4745
 
4746
                if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
4747
 
4748
                        umc = (megacmd_t *)uioc_mimd->mbox;
4749
                        if (copy_from_user(&kmc, umc, sizeof(megacmd_t)))
4750
                                return -EFAULT;
4751
 
4752
                        upthru = (mega_passthru *)kmc.xferaddr;
4753
 
4754
                        if( put_user(mc->status, (u8 *)&upthru->scsistatus) )
4755
                                return (-EFAULT);
4756
                }
4757
        }
4758
 
4759
        return 0;
4760
}
4761
 
4762
 
4763
static int
4764
megadev_close (struct inode *inode, struct file *filep)
4765
{
4766
        MOD_DEC_USE_COUNT;
4767
        return 0;
4768
}
4769
 
4770
 
4771
/*
4772
 * MEGARAID 'FW' commands.
4773
 */
4774
 
4775
/**
4776
 * mega_is_bios_enabled()
4777
 * @adapter - pointer to our soft state
4778
 *
4779
 * issue command to find out if the BIOS is enabled for this controller
4780
 */
4781
static int
4782
mega_is_bios_enabled(adapter_t *adapter)
4783
{
4784
        unsigned char   raw_mbox[sizeof(mbox_t)];
4785
        mbox_t  *mbox;
4786
        int     ret;
4787
 
4788
        mbox = (mbox_t *)raw_mbox;
4789
 
4790
        memset(raw_mbox, 0, sizeof(raw_mbox));
4791
 
4792
        memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4793
 
4794
        mbox->xferaddr = (u32)adapter->buf_dma_handle;
4795
 
4796
        raw_mbox[0] = IS_BIOS_ENABLED;
4797
        raw_mbox[2] = GET_BIOS;
4798
 
4799
 
4800
        ret = issue_scb_block(adapter, raw_mbox);
4801
 
4802
        return *(char *)adapter->mega_buffer;
4803
}
4804
 
4805
 
4806
/**
4807
 * mega_enum_raid_scsi()
4808
 * @adapter - pointer to our soft state
4809
 *
4810
 * Find out what channels are RAID/SCSI. This information is used to
4811
 * differentiate the virtual channels and physical channels and to support
4812
 * ROMB feature and non-disk devices.
4813
 */
4814
static void
4815
mega_enum_raid_scsi(adapter_t *adapter)
4816
{
4817
        unsigned char raw_mbox[sizeof(mbox_t)];
4818
        mbox_t *mbox;
4819
        int i;
4820
 
4821
        mbox = (mbox_t *)raw_mbox;
4822
 
4823
        memset(raw_mbox, 0, sizeof(raw_mbox));
4824
 
4825
        /*
4826
         * issue command to find out what channels are raid/scsi
4827
         */
4828
        raw_mbox[0] = CHNL_CLASS;
4829
        raw_mbox[2] = GET_CHNL_CLASS;
4830
 
4831
        memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4832
 
4833
        mbox->xferaddr = (u32)adapter->buf_dma_handle;
4834
 
4835
        /*
4836
         * Non-ROMB firware fail this command, so all channels
4837
         * must be shown RAID
4838
         */
4839
        adapter->mega_ch_class = 0xFF;
4840
 
4841
        if(!issue_scb_block(adapter, raw_mbox)) {
4842
                adapter->mega_ch_class = *((char *)adapter->mega_buffer);
4843
 
4844
        }
4845
 
4846
        for( i = 0; i < adapter->product_info.nchannels; i++ ) {
4847
                if( (adapter->mega_ch_class >> i) & 0x01 ) {
4848
                        printk(KERN_INFO "megaraid: channel[%d] is raid.\n",
4849
                                        i);
4850
                }
4851
                else {
4852
                        printk(KERN_INFO "megaraid: channel[%d] is scsi.\n",
4853
                                        i);
4854
                }
4855
        }
4856
 
4857
        return;
4858
}
4859
 
4860
 
4861
/**
4862
 * mega_get_boot_drv()
4863
 * @adapter - pointer to our soft state
4864
 *
4865
 * Find out which device is the boot device. Note, any logical drive or any
4866
 * phyical device (e.g., a CDROM) can be designated as a boot device.
4867
 */
4868
static void
4869
mega_get_boot_drv(adapter_t *adapter)
4870
{
4871
        struct private_bios_data        *prv_bios_data;
4872
        unsigned char   raw_mbox[sizeof(mbox_t)];
4873
        mbox_t  *mbox;
4874
        u16     cksum = 0;
4875
        u8      *cksum_p;
4876
        u8      boot_pdrv;
4877
        int     i;
4878
 
4879
        mbox = (mbox_t *)raw_mbox;
4880
 
4881
        memset(raw_mbox, 0, sizeof(raw_mbox));
4882
 
4883
        raw_mbox[0] = BIOS_PVT_DATA;
4884
        raw_mbox[2] = GET_BIOS_PVT_DATA;
4885
 
4886
        memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
4887
 
4888
        mbox->xferaddr = (u32)adapter->buf_dma_handle;
4889
 
4890
        adapter->boot_ldrv_enabled = 0;
4891
        adapter->boot_ldrv = 0;
4892
 
4893
        adapter->boot_pdrv_enabled = 0;
4894
        adapter->boot_pdrv_ch = 0;
4895
        adapter->boot_pdrv_tgt = 0;
4896
 
4897
        if(issue_scb_block(adapter, raw_mbox) == 0) {
4898
                prv_bios_data =
4899
                        (struct private_bios_data *)adapter->mega_buffer;
4900
 
4901
                cksum = 0;
4902
                cksum_p = (char *)prv_bios_data;
4903
                for (i = 0; i < 14; i++ ) {
4904
                        cksum += (u16)(*cksum_p++);
4905
                }
4906
 
4907
                if (prv_bios_data->cksum == (u16)(0-cksum) ) {
4908
 
4909
                        /*
4910
                         * If MSB is set, a physical drive is set as boot
4911
                         * device
4912
                         */
4913
                        if( prv_bios_data->boot_drv & 0x80 ) {
4914
                                adapter->boot_pdrv_enabled = 1;
4915
                                boot_pdrv = prv_bios_data->boot_drv & 0x7F;
4916
                                adapter->boot_pdrv_ch = boot_pdrv / 16;
4917
                                adapter->boot_pdrv_tgt = boot_pdrv % 16;
4918
                        }
4919
                        else {
4920
                                adapter->boot_ldrv_enabled = 1;
4921
                                adapter->boot_ldrv = prv_bios_data->boot_drv;
4922
                        }
4923
                }
4924
        }
4925
 
4926
}
4927
 
4928
/**
4929
 * mega_support_random_del()
4930
 * @adapter - pointer to our soft state
4931
 *
4932
 * Find out if this controller supports random deletion and addition of
4933
 * logical drives
4934
 */
4935
static int
4936
mega_support_random_del(adapter_t *adapter)
4937
{
4938
        unsigned char raw_mbox[sizeof(mbox_t)];
4939
        mbox_t *mbox;
4940
        int rval;
4941
 
4942
        mbox = (mbox_t *)raw_mbox;
4943
 
4944
        memset(raw_mbox, 0, sizeof(raw_mbox));
4945
 
4946
        /*
4947
         * issue command
4948
         */
4949
        raw_mbox[0] = FC_DEL_LOGDRV;
4950
        raw_mbox[2] = OP_SUP_DEL_LOGDRV;
4951
 
4952
        rval = issue_scb_block(adapter, raw_mbox);
4953
 
4954
        return !rval;
4955
}
4956
 
4957
 
4958
/**
4959
 * mega_support_ext_cdb()
4960
 * @adapter - pointer to our soft state
4961
 *
4962
 * Find out if this firmware support cdblen > 10
4963
 */
4964
static int
4965
mega_support_ext_cdb(adapter_t *adapter)
4966
{
4967
        unsigned char raw_mbox[sizeof(mbox_t)];
4968
        mbox_t *mbox;
4969
        int rval;
4970
 
4971
        mbox = (mbox_t *)raw_mbox;
4972
 
4973
        memset(raw_mbox, 0, sizeof(raw_mbox));
4974
        /*
4975
         * issue command to find out if controller supports extended CDBs.
4976
         */
4977
        raw_mbox[0] = 0xA4;
4978
        raw_mbox[2] = 0x16;
4979
 
4980
        rval = issue_scb_block(adapter, raw_mbox);
4981
 
4982
        return !rval;
4983
}
4984
 
4985
 
4986
/**
4987
 * mega_del_logdrv()
4988
 * @adapter - pointer to our soft state
4989
 * @logdrv - logical drive to be deleted
4990
 *
4991
 * Delete the specified logical drive. It is the responsibility of the user
4992
 * app to let the OS know about this operation.
4993
 */
4994
static int
4995
mega_del_logdrv(adapter_t *adapter, int logdrv)
4996
{
4997
        DECLARE_WAIT_QUEUE_HEAD(wq);
4998
        unsigned long flags;
4999
        scb_t *scb;
5000
        int rval;
5001
 
5002
        ASSERT( !spin_is_locked(adapter->host_lock) );
5003
 
5004
        /*
5005
         * Stop sending commands to the controller, queue them internally.
5006
         * When deletion is complete, ISR will flush the queue.
5007
         */
5008
        atomic_set(&adapter->quiescent, 1);
5009
 
5010
        /*
5011
         * Wait till all the issued commands are complete and there are no
5012
         * commands in the pending queue
5013
         */
5014
        while( atomic_read(&adapter->pend_cmds) > 0 ) {
5015
 
5016
                sleep_on_timeout( &wq, 1*HZ );  /* sleep for 1s */
5017
        }
5018
 
5019
        rval = mega_do_del_logdrv(adapter, logdrv);
5020
 
5021
 
5022
        spin_lock_irqsave(adapter->host_lock, flags);
5023
 
5024
        /*
5025
         * If delete operation was successful, add 0x80 to the logical drive
5026
         * ids for commands in the pending queue.
5027
         */
5028
        if (adapter->read_ldidmap) {
5029
                struct list_head *pos;
5030
                list_for_each(pos, &adapter->pending_list) {
5031
                        scb = list_entry(pos, scb_t, list);
5032
                        if (((mbox_t *)scb->raw_mbox)->logdrv < 0x80 )
5033
                                ((mbox_t *)scb->raw_mbox)->logdrv += 0x80 ;
5034
                }
5035
        }
5036
 
5037
        atomic_set(&adapter->quiescent, 0);
5038
 
5039
        mega_runpendq(adapter);
5040
 
5041
        spin_unlock_irqrestore(adapter->host_lock, flags);
5042
 
5043
        return rval;
5044
}
5045
 
5046
 
5047
static int
5048
mega_do_del_logdrv(adapter_t *adapter, int logdrv)
5049
{
5050
        int     rval;
5051
        u8      raw_mbox[sizeof(mbox_t)];
5052
 
5053
        memset(raw_mbox, 0, sizeof(raw_mbox));
5054
 
5055
        raw_mbox[0] = FC_DEL_LOGDRV;
5056
        raw_mbox[2] = OP_DEL_LOGDRV;
5057
        raw_mbox[3] = logdrv;
5058
 
5059
        /* Issue a blocking command to the card */
5060
        rval = issue_scb_block(adapter, raw_mbox);
5061
 
5062
        /* log this event */
5063
        if(rval) {
5064
                printk(KERN_WARNING "megaraid: Delete LD-%d failed.", logdrv);
5065
                return rval;
5066
        }
5067
 
5068
        /*
5069
         * After deleting first logical drive, the logical drives must be
5070
         * addressed by adding 0x80 to the logical drive id.
5071
         */
5072
        adapter->read_ldidmap = 1;
5073
 
5074
        return rval;
5075
}
5076
 
5077
 
5078
/**
5079
 * mega_get_max_sgl()
5080
 * @adapter - pointer to our soft state
5081
 *
5082
 * Find out the maximum number of scatter-gather elements supported by this
5083
 * version of the firmware
5084
 */
5085
static void
5086
mega_get_max_sgl(adapter_t *adapter)
5087
{
5088
        unsigned char   raw_mbox[sizeof(mbox_t)];
5089
        mbox_t  *mbox;
5090
 
5091
        mbox = (mbox_t *)raw_mbox;
5092
 
5093
        memset(raw_mbox, 0, sizeof(raw_mbox));
5094
 
5095
        memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
5096
 
5097
        mbox->xferaddr = (u32)adapter->buf_dma_handle;
5098
 
5099
        raw_mbox[0] = MAIN_MISC_OPCODE;
5100
        raw_mbox[2] = GET_MAX_SG_SUPPORT;
5101
 
5102
 
5103
        if( issue_scb_block(adapter, raw_mbox) ) {
5104
                /*
5105
                 * f/w does not support this command. Choose the default value
5106
                 */
5107
                adapter->sglen = MIN_SGLIST;
5108
        }
5109
        else {
5110
                adapter->sglen = *((char *)adapter->mega_buffer);
5111
 
5112
                /*
5113
                 * Make sure this is not more than the resources we are
5114
                 * planning to allocate
5115
                 */
5116
                if ( adapter->sglen > MAX_SGLIST )
5117
                        adapter->sglen = MAX_SGLIST;
5118
        }
5119
 
5120
        return;
5121
}
5122
 
5123
 
5124
/**
5125
 * mega_support_cluster()
5126
 * @adapter - pointer to our soft state
5127
 *
5128
 * Find out if this firmware support cluster calls.
5129
 */
5130
static int
5131
mega_support_cluster(adapter_t *adapter)
5132
{
5133
        unsigned char   raw_mbox[sizeof(mbox_t)];
5134
        mbox_t  *mbox;
5135
 
5136
        mbox = (mbox_t *)raw_mbox;
5137
 
5138
        memset(raw_mbox, 0, sizeof(raw_mbox));
5139
 
5140
        memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
5141
 
5142
        mbox->xferaddr = (u32)adapter->buf_dma_handle;
5143
 
5144
        /*
5145
         * Try to get the initiator id. This command will succeed iff the
5146
         * clustering is available on this HBA.
5147
         */
5148
        raw_mbox[0] = MEGA_GET_TARGET_ID;
5149
 
5150
        if( issue_scb_block(adapter, raw_mbox) == 0 ) {
5151
 
5152
                /*
5153
                 * Cluster support available. Get the initiator target id.
5154
                 * Tell our id to mid-layer too.
5155
                 */
5156
                adapter->this_id = *(u32 *)adapter->mega_buffer;
5157
                adapter->host->this_id = adapter->this_id;
5158
 
5159
                return 1;
5160
        }
5161
 
5162
        return 0;
5163
}
5164
 
5165
 
5166
 
5167
/**
5168
 * mega_get_ldrv_num()
5169
 * @adapter - pointer to our soft state
5170
 * @cmd - scsi mid layer command
5171
 * @channel - channel on the controller
5172
 *
5173
 * Calculate the logical drive number based on the information in scsi command
5174
 * and the channel number.
5175
 */
5176
static inline int
5177
mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
5178
{
5179
        int             tgt;
5180
        int             ldrv_num;
5181
 
5182
        tgt = cmd->target;
5183
 
5184
        if ( tgt > adapter->this_id )
5185
                tgt--;  /* we do not get inquires for initiator id */
5186
 
5187
        ldrv_num = (channel * 15) + tgt;
5188
 
5189
 
5190
        /*
5191
         * If we have a logical drive with boot enabled, project it first
5192
         */
5193
        if( adapter->boot_ldrv_enabled ) {
5194
                if( ldrv_num == 0 ) {
5195
                        ldrv_num = adapter->boot_ldrv;
5196
                }
5197
                else {
5198
                        if( ldrv_num <= adapter->boot_ldrv ) {
5199
                                ldrv_num--;
5200
                        }
5201
                }
5202
        }
5203
 
5204
        /*
5205
         * If "delete logical drive" feature is enabled on this controller.
5206
         * Do only if at least one delete logical drive operation was done.
5207
         *
5208
         * Also, after logical drive deletion, instead of logical drive number,
5209
         * the value returned should be 0x80+logical drive id.
5210
         *
5211
         * These is valid only for IO commands.
5212
         */
5213
 
5214
        if (adapter->support_random_del && adapter->read_ldidmap )
5215
                switch (cmd->cmnd[0]) {
5216
                case READ_6:    /* fall through */
5217
                case WRITE_6:   /* fall through */
5218
                case READ_10:   /* fall through */
5219
                case WRITE_10:
5220
                        ldrv_num += 0x80;
5221
                }
5222
 
5223
        return ldrv_num;
5224
}
5225
 
5226
 
5227
/**
5228
 * mega_reorder_hosts()
5229
 *
5230
 * Hack: reorder the scsi hosts in mid-layer so that the controller with the
5231
 * boot device on it appears first in the list.
5232
 */
5233
static void
5234
mega_reorder_hosts(void)
5235
{
5236
        struct Scsi_Host *shpnt;
5237
        struct Scsi_Host *shone;
5238
        struct Scsi_Host *shtwo;
5239
        adapter_t *boot_host;
5240
        int i;
5241
 
5242
        /*
5243
         * Find the (first) host which has it's BIOS enabled
5244
         */
5245
        boot_host = NULL;
5246
        for (i = 0; i < MAX_CONTROLLERS; i++) {
5247
                if (mega_hbas[i].is_bios_enabled) {
5248
                        boot_host = mega_hbas[i].hostdata_addr;
5249
                        break;
5250
                }
5251
        }
5252
 
5253
        if (!boot_host) {
5254
                printk(KERN_NOTICE "megaraid: no BIOS enabled.\n");
5255
                return;
5256
        }
5257
 
5258
        /*
5259
         * Traverse through the list of SCSI hosts for our HBA locations
5260
         */
5261
        shone = shtwo = NULL;
5262
        for (shpnt = scsi_hostlist; shpnt; shpnt = shpnt->next) {
5263
                /* Is it one of ours? */
5264
                for (i = 0; i < MAX_CONTROLLERS; i++) {
5265
                        if ((adapter_t *) shpnt->hostdata ==
5266
                                mega_hbas[i].hostdata_addr) {
5267
                                /* Does this one has BIOS enabled */
5268
                                if (mega_hbas[i].hostdata_addr == boot_host) {
5269
 
5270
                                        /* Are we first */
5271
                                        if (!shtwo)     /* Yes! */
5272
                                                return;
5273
                                        else    /* :-( */
5274
                                                shone = shpnt;
5275
                                } else {
5276
                                        if (!shtwo) {
5277
                                                /* were we here before? xchng
5278
                                                 * first */
5279
                                                shtwo = shpnt;
5280
                                        }
5281
                                }
5282
                                break;
5283
                        }
5284
                }
5285
                /*
5286
                 * Have we got the boot host and one which does not have the
5287
                 * bios enabled.
5288
                 */
5289
                if (shone && shtwo)
5290
                        break;
5291
        }
5292
        if (shone && shtwo) {
5293
                mega_swap_hosts (shone, shtwo);
5294
        }
5295
 
5296
        return;
5297
}
5298
 
5299
 
5300
static void
5301
mega_swap_hosts (struct Scsi_Host *shone, struct Scsi_Host *shtwo)
5302
{
5303
        struct Scsi_Host *prevtoshtwo;
5304
        struct Scsi_Host *prevtoshone;
5305
        struct Scsi_Host *save = NULL;
5306
 
5307
        /* Are these two nodes adjacent */
5308
        if (shtwo->next == shone) {
5309
 
5310
                if (shtwo == scsi_hostlist && !shone->next) {
5311
 
5312
                        /* just two nodes */
5313
                        scsi_hostlist = shone;
5314
                        shone->next = shtwo;
5315
                        shtwo->next = NULL;
5316
                } else if (shtwo == scsi_hostlist) {
5317
                        /* first two nodes of the list */
5318
 
5319
                        scsi_hostlist = shone;
5320
                        shtwo->next = shone->next;
5321
                        scsi_hostlist->next = shtwo;
5322
                } else if (!shone->next) {
5323
                        /* last two nodes of the list */
5324
 
5325
                        prevtoshtwo = scsi_hostlist;
5326
 
5327
                        while (prevtoshtwo->next != shtwo)
5328
                                prevtoshtwo = prevtoshtwo->next;
5329
 
5330
                        prevtoshtwo->next = shone;
5331
                        shone->next = shtwo;
5332
                        shtwo->next = NULL;
5333
                } else {
5334
                        prevtoshtwo = scsi_hostlist;
5335
 
5336
                        while (prevtoshtwo->next != shtwo)
5337
                                prevtoshtwo = prevtoshtwo->next;
5338
 
5339
                        prevtoshtwo->next = shone;
5340
                        shtwo->next = shone->next;
5341
                        shone->next = shtwo;
5342
                }
5343
 
5344
        } else if (shtwo == scsi_hostlist && !shone->next) {
5345
                /* shtwo at head, shone at tail, not adjacent */
5346
 
5347
                prevtoshone = scsi_hostlist;
5348
 
5349
                while (prevtoshone->next != shone)
5350
                        prevtoshone = prevtoshone->next;
5351
 
5352
                scsi_hostlist = shone;
5353
                shone->next = shtwo->next;
5354
                prevtoshone->next = shtwo;
5355
                shtwo->next = NULL;
5356
        } else if (shtwo == scsi_hostlist && shone->next) {
5357
                /* shtwo at head, shone is not at tail */
5358
 
5359
                prevtoshone = scsi_hostlist;
5360
                while (prevtoshone->next != shone)
5361
                        prevtoshone = prevtoshone->next;
5362
 
5363
                scsi_hostlist = shone;
5364
                prevtoshone->next = shtwo;
5365
                save = shtwo->next;
5366
                shtwo->next = shone->next;
5367
                shone->next = save;
5368
        } else if (!shone->next) {
5369
                /* shtwo not at head, shone at tail */
5370
 
5371
                prevtoshtwo = scsi_hostlist;
5372
                prevtoshone = scsi_hostlist;
5373
 
5374
                while (prevtoshtwo->next != shtwo)
5375
                        prevtoshtwo = prevtoshtwo->next;
5376
                while (prevtoshone->next != shone)
5377
                        prevtoshone = prevtoshone->next;
5378
 
5379
                prevtoshtwo->next = shone;
5380
                shone->next = shtwo->next;
5381
                prevtoshone->next = shtwo;
5382
                shtwo->next = NULL;
5383
 
5384
        } else {
5385
                prevtoshtwo = scsi_hostlist;
5386
                prevtoshone = scsi_hostlist;
5387
                save = NULL;
5388
 
5389
                while (prevtoshtwo->next != shtwo)
5390
                        prevtoshtwo = prevtoshtwo->next;
5391
                while (prevtoshone->next != shone)
5392
                        prevtoshone = prevtoshone->next;
5393
 
5394
                prevtoshtwo->next = shone;
5395
                save = shone->next;
5396
                shone->next = shtwo->next;
5397
                prevtoshone->next = shtwo;
5398
                shtwo->next = save;
5399
        }
5400
        return;
5401
}
5402
 
5403
 
5404
 
5405
#ifdef CONFIG_PROC_FS
5406
/**
5407
 * mega_adapinq()
5408
 * @adapter - pointer to our soft state
5409
 * @dma_handle - DMA address of the buffer
5410
 *
5411
 * Issue internal comamnds while interrupts are available.
5412
 * We only issue direct mailbox commands from within the driver. ioctl()
5413
 * interface using these routines can issue passthru commands.
5414
 */
5415
static int
5416
mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
5417
{
5418
        megacmd_t       mc;
5419
 
5420
        memset(&mc, 0, sizeof(megacmd_t));
5421
 
5422
        if( adapter->flag & BOARD_40LD ) {
5423
                mc.cmd = FC_NEW_CONFIG;
5424
                mc.opcode = NC_SUBOP_ENQUIRY3;
5425
                mc.subopcode = ENQ3_GET_SOLICITED_FULL;
5426
        }
5427
        else {
5428
                mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
5429
        }
5430
 
5431
        mc.xferaddr = (u32)dma_handle;
5432
 
5433
        if ( mega_internal_command(adapter, LOCK_INT, &mc, NULL) != 0 ) {
5434
                return -1;
5435
        }
5436
 
5437
        return 0;
5438
}
5439
 
5440
 
5441
/**
5442
 * mega_allocate_inquiry()
5443
 * @dma_handle - handle returned for dma address
5444
 * @pdev - handle to pci device
5445
 *
5446
 * allocates memory for inquiry structure
5447
 */
5448
static inline caddr_t
5449
mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
5450
{
5451
        return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
5452
}
5453
 
5454
 
5455
static inline void
5456
mega_free_inquiry(caddr_t inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
5457
{
5458
        pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
5459
}
5460
 
5461
 
5462
/** mega_internal_dev_inquiry()
5463
 * @adapter - pointer to our soft state
5464
 * @ch - channel for this device
5465
 * @tgt - ID of this device
5466
 * @buf_dma_handle - DMA address of the buffer
5467
 *
5468
 * Issue the scsi inquiry for the specified device.
5469
 */
5470
static int
5471
mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
5472
                dma_addr_t buf_dma_handle)
5473
{
5474
        mega_passthru   *pthru;
5475
        dma_addr_t      pthru_dma_handle;
5476
        megacmd_t       mc;
5477
        int             rval;
5478
        struct pci_dev  *pdev;
5479
 
5480
 
5481
        /*
5482
         * For all internal commands, the buffer must be allocated in <4GB
5483
         * address range
5484
         */
5485
        pdev = adapter->dev;
5486
 
5487
        pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
5488
                        &pthru_dma_handle);
5489
 
5490
        if( pthru == NULL ) {
5491
                return -1;
5492
        }
5493
 
5494
        pthru->timeout = 2;
5495
        pthru->ars = 1;
5496
        pthru->reqsenselen = 14;
5497
        pthru->islogical = 0;
5498
 
5499
        pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
5500
 
5501
        pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
5502
 
5503
        pthru->cdblen = 6;
5504
 
5505
        pthru->cdb[0] = INQUIRY;
5506
        pthru->cdb[1] = 0;
5507
        pthru->cdb[2] = 0;
5508
        pthru->cdb[3] = 0;
5509
        pthru->cdb[4] = 255;
5510
        pthru->cdb[5] = 0;
5511
 
5512
 
5513
        pthru->dataxferaddr = (u32)buf_dma_handle;
5514
        pthru->dataxferlen = 256;
5515
 
5516
        memset(&mc, 0, sizeof(megacmd_t));
5517
 
5518
        mc.cmd = MEGA_MBOXCMD_PASSTHRU;
5519
        mc.xferaddr = (u32)pthru_dma_handle;
5520
 
5521
        rval = mega_internal_command(adapter, LOCK_INT, &mc, pthru);
5522
 
5523
        pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
5524
                        pthru_dma_handle);
5525
 
5526
        return rval;
5527
}
5528
#endif  // #ifdef CONFIG_PROC_FS
5529
 
5530
 
5531
/**
5532
 * mega_internal_command()
5533
 * @adapter - pointer to our soft state
5534
 * @ls - the scope of the exclusion lock.
5535
 * @mc - the mailbox command
5536
 * @pthru - Passthru structure for DCDB commands
5537
 *
5538
 * Issue the internal commands in interrupt mode.
5539
 * The last argument is the address of the passthru structure if the command
5540
 * to be fired is a passthru command
5541
 *
5542
 * lockscope specifies whether the caller has already acquired the lock. Of
5543
 * course, the caller must know which lock we are talking about.
5544
 *
5545
 * Note: parameter 'pthru' is null for non-passthru commands.
5546
 */
5547
static int
5548
mega_internal_command(adapter_t *adapter, lockscope_t ls, megacmd_t *mc,
5549
                mega_passthru *pthru )
5550
{
5551
        Scsi_Cmnd       *scmd;
5552
        unsigned long   flags = 0;
5553
        scb_t   *scb;
5554
        int     rval;
5555
 
5556
        /*
5557
         * The internal commands share one command id and hence are
5558
         * serialized. This is so because we want to reserve maximum number of
5559
         * available command ids for the I/O commands.
5560
         */
5561
        down(&adapter->int_mtx);
5562
 
5563
        scb = &adapter->int_scb;
5564
        memset(scb, 0, sizeof(scb_t));
5565
 
5566
        scmd = &adapter->int_scmd;
5567
        memset(scmd, 0, sizeof(Scsi_Cmnd));
5568
 
5569
        scmd->host = adapter->host;
5570
        scmd->buffer = (void *)scb;
5571
        scmd->cmnd[0] = MEGA_INTERNAL_CMD;
5572
 
5573
        scb->state |= SCB_ACTIVE;
5574
        scb->cmd = scmd;
5575
 
5576
        memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
5577
 
5578
        /*
5579
         * Is it a passthru command
5580
         */
5581
        if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
5582
 
5583
                scb->pthru = pthru;
5584
        }
5585
 
5586
        scb->idx = CMDID_INT_CMDS;
5587
 
5588
        scmd->state = 0;
5589
 
5590
        /*
5591
         * Get the lock only if the caller has not acquired it already
5592
         */
5593
        if( ls == LOCK_INT ) spin_lock_irqsave(adapter->host_lock, flags);
5594
 
5595
        megaraid_queue(scmd, mega_internal_done);
5596
 
5597
        if( ls == LOCK_INT ) spin_unlock_irqrestore(adapter->host_lock, flags);
5598
 
5599
        /*
5600
         * Wait till this command finishes. Do not use
5601
         * wait_event_interruptible(). It causes panic if CTRL-C is hit when
5602
         * dumping e.g., physical disk information through /proc interface.
5603
         * Catching the return value should solve the issue but for now keep
5604
         * the call non-interruptible.
5605
         */
5606
#if 0
5607
        wait_event_interruptible(adapter->int_waitq, scmd->state);
5608
#endif
5609
        wait_event(adapter->int_waitq, scmd->state);
5610
 
5611
        rval = scmd->result;
5612
        mc->status = scmd->result;
5613
 
5614
        /*
5615
         * Print a debug message for all failed commands. Applications can use
5616
         * this information.
5617
         */
5618
        if( scmd->result && trace_level ) {
5619
                printk("megaraid: cmd [%x, %x, %x] status:[%x]\n",
5620
                        mc->cmd, mc->opcode, mc->subopcode, scmd->result);
5621
        }
5622
 
5623
        up(&adapter->int_mtx);
5624
 
5625
        return rval;
5626
}
5627
 
5628
 
5629
/**
5630
 * mega_internal_done()
5631
 * @scmd - internal scsi command
5632
 *
5633
 * Callback routine for internal commands.
5634
 */
5635
static void
5636
mega_internal_done(Scsi_Cmnd *scmd)
5637
{
5638
        adapter_t       *adapter;
5639
 
5640
        adapter = (adapter_t *)scmd->host->hostdata;
5641
 
5642
        scmd->state = 1; /* thread waiting for its command to complete */
5643
 
5644
        /*
5645
         * See comment in mega_internal_command() routine for
5646
         * wait_event_interruptible()
5647
         */
5648
#if 0
5649
        wake_up_interruptible(&adapter->int_waitq);
5650
#endif
5651
        wake_up(&adapter->int_waitq);
5652
 
5653
}
5654
 
5655
static Scsi_Host_Template driver_template = MEGARAID;
5656
 
5657
#include "scsi_module.c"
5658
 
5659
/* vi: set ts=8 sw=8 tw=78: */

powered by: WebSVN 2.1.0

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