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