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