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