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