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