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