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