blob: 91f5e2c68dbc64950f6e714e0f6383345f1483da [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 *
3 * Linux MegaRAID device driver
4 *
Ju, Seokmann3492b322005-11-17 13:13:31 -05005 * Copyright (c) 2002 LSI Logic Corporation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
13 * - fixes
14 * - speed-ups (list handling fixes, issued_list, optimizations.)
15 * - lots of cleanups.
16 *
17 * Copyright (c) 2003 Christoph Hellwig <hch@lst.de>
18 * - new-style, hotplug-aware pci probing and scsi registration
19 *
Ju, Seokmann3492b322005-11-17 13:13:31 -050020 * Version : v2.00.4 Mon Nov 14 14:02:43 EST 2005 - Seokmann Ju
21 * <Seokmann.Ju@lsil.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 *
23 * Description: Linux device driver for LSI Logic MegaRAID controller
24 *
25 * Supported controllers: MegaRAID 418, 428, 438, 466, 762, 467, 471, 490, 493
26 * 518, 520, 531, 532
27 *
28 * This driver is supported by LSI Logic, with assistance from Red Hat, Dell,
29 * and others. Please send updates to the mailing list
30 * linux-scsi@vger.kernel.org .
31 *
32 */
33
34#include <linux/mm.h>
35#include <linux/fs.h>
36#include <linux/blkdev.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080037#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <asm/io.h>
Christoph Hellwig8d115f82005-06-19 13:42:05 +020039#include <linux/completion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/delay.h>
41#include <linux/proc_fs.h>
David Howellsc7f079c2013-04-10 13:33:21 +010042#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/reboot.h>
44#include <linux/module.h>
45#include <linux/list.h>
46#include <linux/interrupt.h>
47#include <linux/pci.h>
48#include <linux/init.h>
Matthias Gehre910638a2006-03-28 01:56:48 -080049#include <linux/dma-mapping.h>
Arnd Bergmannc45d15d2010-06-02 14:28:52 +020050#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090051#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <scsi/scsicam.h>
53
54#include "scsi.h"
55#include <scsi/scsi_host.h>
56
57#include "megaraid.h"
58
Ju, Seokmann3492b322005-11-17 13:13:31 -050059#define MEGARAID_MODULE_VERSION "2.00.4"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Ju, Seokmann3492b322005-11-17 13:13:31 -050061MODULE_AUTHOR ("sju@lsil.com");
62MODULE_DESCRIPTION ("LSI Logic MegaRAID legacy driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -070063MODULE_LICENSE ("GPL");
64MODULE_VERSION(MEGARAID_MODULE_VERSION);
65
Arnd Bergmannc45d15d2010-06-02 14:28:52 +020066static DEFINE_MUTEX(megadev_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static unsigned int max_cmd_per_lun = DEF_CMD_PER_LUN;
68module_param(max_cmd_per_lun, uint, 0);
69MODULE_PARM_DESC(max_cmd_per_lun, "Maximum number of commands which can be issued to a single LUN (default=DEF_CMD_PER_LUN=63)");
70
71static unsigned short int max_sectors_per_io = MAX_SECTORS_PER_IO;
72module_param(max_sectors_per_io, ushort, 0);
73MODULE_PARM_DESC(max_sectors_per_io, "Maximum number of sectors per I/O request (default=MAX_SECTORS_PER_IO=128)");
74
75
76static unsigned short int max_mbox_busy_wait = MBOX_BUSY_WAIT;
77module_param(max_mbox_busy_wait, ushort, 0);
78MODULE_PARM_DESC(max_mbox_busy_wait, "Maximum wait for mailbox in microseconds if busy (default=MBOX_BUSY_WAIT=10)");
79
Jeff Garzik00769ec2006-12-03 20:49:23 -050080#define RDINDOOR(adapter) readl((adapter)->mmio_base + 0x20)
81#define RDOUTDOOR(adapter) readl((adapter)->mmio_base + 0x2C)
82#define WRINDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x20)
83#define WROUTDOOR(adapter,value) writel(value, (adapter)->mmio_base + 0x2C)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85/*
86 * Global variables
87 */
88
89static int hba_count;
90static adapter_t *hba_soft_state[MAX_CONTROLLERS];
91static struct proc_dir_entry *mega_proc_dir_entry;
92
93/* For controller re-ordering */
94static struct mega_hbas mega_hbas[MAX_CONTROLLERS];
95
Arnd Bergmannf4927c42010-04-27 00:24:01 +020096static long
97megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg);
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*
100 * The File Operations structure for the serial/ioctl interface of the driver
101 */
Arjan van de Ven00977a52007-02-12 00:55:34 -0800102static const struct file_operations megadev_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 .owner = THIS_MODULE,
Arnd Bergmannf4927c42010-04-27 00:24:01 +0200104 .unlocked_ioctl = megadev_unlocked_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 .open = megadev_open,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200106 .llseek = noop_llseek,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
109/*
110 * Array to structures for storing the information about the controllers. This
111 * information is sent to the user level applications, when they do an ioctl
112 * for this information.
113 */
114static struct mcontroller mcontroller[MAX_CONTROLLERS];
115
116/* The current driver version */
117static u32 driver_ver = 0x02000000;
118
119/* major number used by the device for character interface */
120static int major;
121
122#define IS_RAID_CH(hba, ch) (((hba)->mega_ch_class >> (ch)) & 0x01)
123
124
125/*
126 * Debug variable to print some diagnostic messages
127 */
128static int trace_level;
129
130/**
131 * mega_setup_mailbox()
132 * @adapter - pointer to our soft state
133 *
134 * Allocates a 8 byte aligned memory for the handshake mailbox.
135 */
136static int
137mega_setup_mailbox(adapter_t *adapter)
138{
139 unsigned long align;
140
141 adapter->una_mbox64 = pci_alloc_consistent(adapter->dev,
142 sizeof(mbox64_t), &adapter->una_mbox64_dma);
143
144 if( !adapter->una_mbox64 ) return -1;
145
146 adapter->mbox = &adapter->una_mbox64->mbox;
147
148 adapter->mbox = (mbox_t *)((((unsigned long) adapter->mbox) + 15) &
149 (~0UL ^ 0xFUL));
150
151 adapter->mbox64 = (mbox64_t *)(((unsigned long)adapter->mbox) - 8);
152
153 align = ((void *)adapter->mbox) - ((void *)&adapter->una_mbox64->mbox);
154
155 adapter->mbox_dma = adapter->una_mbox64_dma + 8 + align;
156
157 /*
158 * Register the mailbox if the controller is an io-mapped controller
159 */
160 if( adapter->flag & BOARD_IOMAP ) {
161
Alan Cox7d1abbe2008-02-08 15:34:24 +0000162 outb(adapter->mbox_dma & 0xFF,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 adapter->host->io_port + MBOX_PORT0);
164
Alan Cox7d1abbe2008-02-08 15:34:24 +0000165 outb((adapter->mbox_dma >> 8) & 0xFF,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 adapter->host->io_port + MBOX_PORT1);
167
Alan Cox7d1abbe2008-02-08 15:34:24 +0000168 outb((adapter->mbox_dma >> 16) & 0xFF,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 adapter->host->io_port + MBOX_PORT2);
170
Alan Cox7d1abbe2008-02-08 15:34:24 +0000171 outb((adapter->mbox_dma >> 24) & 0xFF,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 adapter->host->io_port + MBOX_PORT3);
173
Alan Cox7d1abbe2008-02-08 15:34:24 +0000174 outb(ENABLE_MBOX_BYTE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 adapter->host->io_port + ENABLE_MBOX_REGION);
176
177 irq_ack(adapter);
178
179 irq_enable(adapter);
180 }
181
182 return 0;
183}
184
185
186/*
187 * mega_query_adapter()
188 * @adapter - pointer to our soft state
189 *
190 * Issue the adapter inquiry commands to the controller and find out
191 * information and parameter about the devices attached
192 */
193static int
194mega_query_adapter(adapter_t *adapter)
195{
196 dma_addr_t prod_info_dma_handle;
197 mega_inquiry3 *inquiry3;
198 u8 raw_mbox[sizeof(struct mbox_out)];
199 mbox_t *mbox;
200 int retval;
201
202 /* Initialize adapter inquiry mailbox */
203
204 mbox = (mbox_t *)raw_mbox;
205
206 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
207 memset(&mbox->m_out, 0, sizeof(raw_mbox));
208
209 /*
210 * Try to issue Inquiry3 command
211 * if not succeeded, then issue MEGA_MBOXCMD_ADAPTERINQ command and
212 * update enquiry3 structure
213 */
214 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
215
216 inquiry3 = (mega_inquiry3 *)adapter->mega_buffer;
217
218 raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
219 raw_mbox[2] = NC_SUBOP_ENQUIRY3; /* i.e. 0x0F */
220 raw_mbox[3] = ENQ3_GET_SOLICITED_FULL; /* i.e. 0x02 */
221
222 /* Issue a blocking command to the card */
223 if ((retval = issue_scb_block(adapter, raw_mbox))) {
224 /* the adapter does not support 40ld */
225
226 mraid_ext_inquiry *ext_inq;
227 mraid_inquiry *inq;
228 dma_addr_t dma_handle;
229
230 ext_inq = pci_alloc_consistent(adapter->dev,
231 sizeof(mraid_ext_inquiry), &dma_handle);
232
233 if( ext_inq == NULL ) return -1;
234
235 inq = &ext_inq->raid_inq;
236
237 mbox->m_out.xferaddr = (u32)dma_handle;
238
239 /*issue old 0x04 command to adapter */
240 mbox->m_out.cmd = MEGA_MBOXCMD_ADPEXTINQ;
241
242 issue_scb_block(adapter, raw_mbox);
243
244 /*
245 * update Enquiry3 and ProductInfo structures with
246 * mraid_inquiry structure
247 */
248 mega_8_to_40ld(inq, inquiry3,
249 (mega_product_info *)&adapter->product_info);
250
251 pci_free_consistent(adapter->dev, sizeof(mraid_ext_inquiry),
252 ext_inq, dma_handle);
253
254 } else { /*adapter supports 40ld */
255 adapter->flag |= BOARD_40LD;
256
257 /*
258 * get product_info, which is static information and will be
259 * unchanged
260 */
261 prod_info_dma_handle = pci_map_single(adapter->dev, (void *)
262 &adapter->product_info,
263 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
264
265 mbox->m_out.xferaddr = prod_info_dma_handle;
266
267 raw_mbox[0] = FC_NEW_CONFIG; /* i.e. mbox->cmd=0xA1 */
268 raw_mbox[2] = NC_SUBOP_PRODUCT_INFO; /* i.e. 0x0E */
269
270 if ((retval = issue_scb_block(adapter, raw_mbox)))
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -0500271 dev_warn(&adapter->dev->dev,
272 "Product_info cmd failed with error: %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 retval);
274
275 pci_unmap_single(adapter->dev, prod_info_dma_handle,
276 sizeof(mega_product_info), PCI_DMA_FROMDEVICE);
277 }
278
279
280 /*
281 * kernel scans the channels from 0 to <= max_channel
282 */
283 adapter->host->max_channel =
284 adapter->product_info.nchannels + NVIRT_CHAN -1;
285
286 adapter->host->max_id = 16; /* max targets per channel */
287
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300288 adapter->host->max_lun = 7; /* Up to 7 luns for non disk devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290 adapter->host->cmd_per_lun = max_cmd_per_lun;
291
292 adapter->numldrv = inquiry3->num_ldrv;
293
294 adapter->max_cmds = adapter->product_info.max_commands;
295
296 if(adapter->max_cmds > MAX_COMMANDS)
297 adapter->max_cmds = MAX_COMMANDS;
298
299 adapter->host->can_queue = adapter->max_cmds - 1;
300
301 /*
302 * Get the maximum number of scatter-gather elements supported by this
303 * firmware
304 */
305 mega_get_max_sgl(adapter);
306
307 adapter->host->sg_tablesize = adapter->sglen;
308
adam radford124dd902012-01-10 18:07:56 -0800309 /* use HP firmware and bios version encoding
310 Note: fw_version[0|1] and bios_version[0|1] were originally shifted
311 right 8 bits making them zero. This 0 value was hardcoded to fix
312 sparse warnings. */
Jon Mason54ebfd52012-07-10 15:31:28 -0700313 if (adapter->product_info.subsysvid == PCI_VENDOR_ID_HP) {
Arnd Bergmann875826a2017-07-14 14:06:54 +0200314 snprintf(adapter->fw_version, sizeof(adapter->fw_version),
315 "%c%d%d.%d%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 adapter->product_info.fw_version[2],
adam radford124dd902012-01-10 18:07:56 -0800317 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 adapter->product_info.fw_version[1] & 0x0f,
adam radford124dd902012-01-10 18:07:56 -0800319 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 adapter->product_info.fw_version[0] & 0x0f);
Arnd Bergmann875826a2017-07-14 14:06:54 +0200321 snprintf(adapter->bios_version, sizeof(adapter->fw_version),
322 "%c%d%d.%d%d",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 adapter->product_info.bios_version[2],
adam radford124dd902012-01-10 18:07:56 -0800324 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 adapter->product_info.bios_version[1] & 0x0f,
adam radford124dd902012-01-10 18:07:56 -0800326 0,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 adapter->product_info.bios_version[0] & 0x0f);
328 } else {
329 memcpy(adapter->fw_version,
330 (char *)adapter->product_info.fw_version, 4);
331 adapter->fw_version[4] = 0;
332
333 memcpy(adapter->bios_version,
334 (char *)adapter->product_info.bios_version, 4);
335
336 adapter->bios_version[4] = 0;
337 }
338
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -0500339 dev_notice(&adapter->dev->dev, "[%s:%s] detected %d logical drives\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 adapter->fw_version, adapter->bios_version, adapter->numldrv);
341
342 /*
343 * Do we support extended (>10 bytes) cdbs
344 */
345 adapter->support_ext_cdb = mega_support_ext_cdb(adapter);
346 if (adapter->support_ext_cdb)
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -0500347 dev_notice(&adapter->dev->dev, "supports extended CDBs\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
349
350 return 0;
351}
352
353/**
354 * mega_runpendq()
355 * @adapter - pointer to our soft state
356 *
357 * Runs through the list of pending requests.
358 */
359static inline void
360mega_runpendq(adapter_t *adapter)
361{
362 if(!list_empty(&adapter->pending_list))
363 __mega_runpendq(adapter);
364}
365
366/*
367 * megaraid_queue()
368 * @scmd - Issue this scsi command
369 * @done - the callback hook into the scsi mid-layer
370 *
371 * The command queuing entry point for the mid-layer.
372 */
373static int
Jeff Garzikf2812332010-11-16 02:10:29 -0500374megaraid_queue_lck(Scsi_Cmnd *scmd, void (*done)(Scsi_Cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
376 adapter_t *adapter;
377 scb_t *scb;
378 int busy=0;
Christoph Hellwigcb0258a2005-10-31 20:12:07 +0100379 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
381 adapter = (adapter_t *)scmd->device->host->hostdata;
382
383 scmd->scsi_done = done;
384
385
386 /*
387 * Allocate and build a SCB request
388 * busy flag will be set if mega_build_cmd() command could not
389 * allocate scb. We will return non-zero status in that case.
390 * NOTE: scb can be null even though certain commands completed
391 * successfully, e.g., MODE_SENSE and TEST_UNIT_READY, we would
392 * return 0 in that case.
393 */
394
Christoph Hellwigcb0258a2005-10-31 20:12:07 +0100395 spin_lock_irqsave(&adapter->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 scb = mega_build_cmd(adapter, scmd, &busy);
Christoph Hellwig238f9b02005-11-29 21:36:16 +0100397 if (!scb)
398 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Christoph Hellwig238f9b02005-11-29 21:36:16 +0100400 scb->state |= SCB_PENDQ;
401 list_add_tail(&scb->list, &adapter->pending_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Christoph Hellwig238f9b02005-11-29 21:36:16 +0100403 /*
404 * Check if the HBA is in quiescent state, e.g., during a
405 * delete logical drive opertion. If it is, don't run
406 * the pending_list.
407 */
408 if (atomic_read(&adapter->quiescent) == 0)
409 mega_runpendq(adapter);
410
411 busy = 0;
412 out:
Christoph Hellwigcb0258a2005-10-31 20:12:07 +0100413 spin_unlock_irqrestore(&adapter->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 return busy;
415}
416
Jeff Garzikf2812332010-11-16 02:10:29 -0500417static DEF_SCSI_QCMD(megaraid_queue)
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/**
420 * mega_allocate_scb()
421 * @adapter - pointer to our soft state
422 * @cmd - scsi command from the mid-layer
423 *
424 * Allocate a SCB structure. This is the central structure for controller
425 * commands.
426 */
427static inline scb_t *
428mega_allocate_scb(adapter_t *adapter, Scsi_Cmnd *cmd)
429{
430 struct list_head *head = &adapter->free_list;
431 scb_t *scb;
432
433 /* Unlink command from Free List */
434 if( !list_empty(head) ) {
435
436 scb = list_entry(head->next, scb_t, list);
437
438 list_del_init(head->next);
439
440 scb->state = SCB_ACTIVE;
441 scb->cmd = cmd;
442 scb->dma_type = MEGA_DMA_TYPE_NONE;
443
444 return scb;
445 }
446
447 return NULL;
448}
449
450/**
451 * mega_get_ldrv_num()
452 * @adapter - pointer to our soft state
453 * @cmd - scsi mid layer command
454 * @channel - channel on the controller
455 *
456 * Calculate the logical drive number based on the information in scsi command
457 * and the channel number.
458 */
459static inline int
460mega_get_ldrv_num(adapter_t *adapter, Scsi_Cmnd *cmd, int channel)
461{
462 int tgt;
463 int ldrv_num;
464
465 tgt = cmd->device->id;
466
467 if ( tgt > adapter->this_id )
468 tgt--; /* we do not get inquires for initiator id */
469
470 ldrv_num = (channel * 15) + tgt;
471
472
473 /*
474 * If we have a logical drive with boot enabled, project it first
475 */
476 if( adapter->boot_ldrv_enabled ) {
477 if( ldrv_num == 0 ) {
478 ldrv_num = adapter->boot_ldrv;
479 }
480 else {
481 if( ldrv_num <= adapter->boot_ldrv ) {
482 ldrv_num--;
483 }
484 }
485 }
486
487 /*
488 * If "delete logical drive" feature is enabled on this controller.
489 * Do only if at least one delete logical drive operation was done.
490 *
491 * Also, after logical drive deletion, instead of logical drive number,
492 * the value returned should be 0x80+logical drive id.
493 *
494 * These is valid only for IO commands.
495 */
496
497 if (adapter->support_random_del && adapter->read_ldidmap )
498 switch (cmd->cmnd[0]) {
499 case READ_6: /* fall through */
500 case WRITE_6: /* fall through */
501 case READ_10: /* fall through */
502 case WRITE_10:
503 ldrv_num += 0x80;
504 }
505
506 return ldrv_num;
507}
508
509/**
510 * mega_build_cmd()
511 * @adapter - pointer to our soft state
512 * @cmd - Prepare using this scsi command
513 * @busy - busy flag if no resources
514 *
515 * Prepares a command and scatter gather list for the controller. This routine
516 * also finds out if the commands is intended for a logical drive or a
517 * physical device and prepares the controller command accordingly.
518 *
519 * We also re-order the logical drives and physical devices based on their
520 * boot settings.
521 */
522static scb_t *
523mega_build_cmd(adapter_t *adapter, Scsi_Cmnd *cmd, int *busy)
524{
525 mega_ext_passthru *epthru;
526 mega_passthru *pthru;
527 scb_t *scb;
528 mbox_t *mbox;
Dan Carpenter9d5d93e2012-06-27 12:08:25 +0300529 u32 seg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 char islogical;
531 int max_ldrv_num;
532 int channel = 0;
533 int target = 0;
534 int ldrv_num = 0; /* logical drive number */
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /*
537 * We know what channels our logical drives are on - mega_find_card()
538 */
539 islogical = adapter->logdrv_chan[cmd->device->channel];
540
541 /*
542 * The theory: If physical drive is chosen for boot, all the physical
543 * devices are exported before the logical drives, otherwise physical
544 * devices are pushed after logical drives, in which case - Kernel sees
545 * the physical devices on virtual channel which is obviously converted
546 * to actual channel on the HBA.
547 */
548 if( adapter->boot_pdrv_enabled ) {
549 if( islogical ) {
550 /* logical channel */
551 channel = cmd->device->channel -
552 adapter->product_info.nchannels;
553 }
554 else {
555 /* this is physical channel */
556 channel = cmd->device->channel;
557 target = cmd->device->id;
558
559 /*
560 * boot from a physical disk, that disk needs to be
561 * exposed first IF both the channels are SCSI, then
562 * booting from the second channel is not allowed.
563 */
564 if( target == 0 ) {
565 target = adapter->boot_pdrv_tgt;
566 }
567 else if( target == adapter->boot_pdrv_tgt ) {
568 target = 0;
569 }
570 }
571 }
572 else {
573 if( islogical ) {
574 /* this is the logical channel */
575 channel = cmd->device->channel;
576 }
577 else {
578 /* physical channel */
579 channel = cmd->device->channel - NVIRT_CHAN;
580 target = cmd->device->id;
581 }
582 }
583
584
585 if(islogical) {
586
587 /* have just LUN 0 for each target on virtual channels */
588 if (cmd->device->lun) {
589 cmd->result = (DID_BAD_TARGET << 16);
590 cmd->scsi_done(cmd);
591 return NULL;
592 }
593
594 ldrv_num = mega_get_ldrv_num(adapter, cmd, channel);
595
596
597 max_ldrv_num = (adapter->flag & BOARD_40LD) ?
598 MAX_LOGICAL_DRIVES_40LD : MAX_LOGICAL_DRIVES_8LD;
599
600 /*
601 * max_ldrv_num increases by 0x80 if some logical drive was
602 * deleted.
603 */
604 if(adapter->read_ldidmap)
605 max_ldrv_num += 0x80;
606
607 if(ldrv_num > max_ldrv_num ) {
608 cmd->result = (DID_BAD_TARGET << 16);
609 cmd->scsi_done(cmd);
610 return NULL;
611 }
612
613 }
614 else {
615 if( cmd->device->lun > 7) {
616 /*
617 * Do not support lun >7 for physically accessed
618 * devices
619 */
620 cmd->result = (DID_BAD_TARGET << 16);
621 cmd->scsi_done(cmd);
622 return NULL;
623 }
624 }
625
626 /*
627 *
628 * Logical drive commands
629 *
630 */
631 if(islogical) {
632 switch (cmd->cmnd[0]) {
633 case TEST_UNIT_READY:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634#if MEGA_HAVE_CLUSTERING
635 /*
636 * Do we support clustering and is the support enabled
637 * If no, return success always
638 */
639 if( !adapter->has_cluster ) {
640 cmd->result = (DID_OK << 16);
641 cmd->scsi_done(cmd);
642 return NULL;
643 }
644
645 if(!(scb = mega_allocate_scb(adapter, cmd))) {
646 *busy = 1;
647 return NULL;
648 }
649
650 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
651 scb->raw_mbox[2] = MEGA_RESERVATION_STATUS;
652 scb->raw_mbox[3] = ldrv_num;
653
654 scb->dma_direction = PCI_DMA_NONE;
655
656 return scb;
657#else
658 cmd->result = (DID_OK << 16);
659 cmd->scsi_done(cmd);
660 return NULL;
661#endif
662
James Bottomley51c928c2005-10-01 09:38:05 -0500663 case MODE_SENSE: {
664 char *buf;
FUJITA Tomonori3f6270e2007-05-14 20:17:27 +0900665 struct scatterlist *sg;
James Bottomley51c928c2005-10-01 09:38:05 -0500666
FUJITA Tomonori3f6270e2007-05-14 20:17:27 +0900667 sg = scsi_sglist(cmd);
Cong Wang77dfce02011-11-25 23:14:23 +0800668 buf = kmap_atomic(sg_page(sg)) + sg->offset;
James Bottomley51c928c2005-10-01 09:38:05 -0500669
Mark Lordf0353302005-12-07 17:46:57 -0500670 memset(buf, 0, cmd->cmnd[4]);
Cong Wang77dfce02011-11-25 23:14:23 +0800671 kunmap_atomic(buf - sg->offset);
James Bottomley51c928c2005-10-01 09:38:05 -0500672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 cmd->result = (DID_OK << 16);
674 cmd->scsi_done(cmd);
675 return NULL;
James Bottomley51c928c2005-10-01 09:38:05 -0500676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 case READ_CAPACITY:
679 case INQUIRY:
680
681 if(!(adapter->flag & (1L << cmd->device->channel))) {
682
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -0500683 dev_notice(&adapter->dev->dev,
684 "scsi%d: scanning scsi channel %d "
685 "for logical drives\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 adapter->host->host_no,
687 cmd->device->channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689 adapter->flag |= (1L << cmd->device->channel);
690 }
691
692 /* Allocate a SCB and initialize passthru */
693 if(!(scb = mega_allocate_scb(adapter, cmd))) {
694 *busy = 1;
695 return NULL;
696 }
697 pthru = scb->pthru;
698
699 mbox = (mbox_t *)scb->raw_mbox;
700 memset(mbox, 0, sizeof(scb->raw_mbox));
701 memset(pthru, 0, sizeof(mega_passthru));
702
703 pthru->timeout = 0;
704 pthru->ars = 1;
705 pthru->reqsenselen = 14;
706 pthru->islogical = 1;
707 pthru->logdrv = ldrv_num;
708 pthru->cdblen = cmd->cmd_len;
709 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
710
711 if( adapter->has_64bit_addr ) {
712 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
713 }
714 else {
715 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
716 }
717
718 scb->dma_direction = PCI_DMA_FROMDEVICE;
719
720 pthru->numsgelements = mega_build_sglist(adapter, scb,
721 &pthru->dataxferaddr, &pthru->dataxferlen);
722
723 mbox->m_out.xferaddr = scb->pthru_dma_addr;
724
725 return scb;
726
727 case READ_6:
728 case WRITE_6:
729 case READ_10:
730 case WRITE_10:
731 case READ_12:
732 case WRITE_12:
733
734 /* Allocate a SCB and initialize mailbox */
735 if(!(scb = mega_allocate_scb(adapter, cmd))) {
736 *busy = 1;
737 return NULL;
738 }
739 mbox = (mbox_t *)scb->raw_mbox;
740
741 memset(mbox, 0, sizeof(scb->raw_mbox));
742 mbox->m_out.logdrv = ldrv_num;
743
744 /*
745 * A little hack: 2nd bit is zero for all scsi read
746 * commands and is set for all scsi write commands
747 */
748 if( adapter->has_64bit_addr ) {
749 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
750 MEGA_MBOXCMD_LWRITE64:
751 MEGA_MBOXCMD_LREAD64 ;
752 }
753 else {
754 mbox->m_out.cmd = (*cmd->cmnd & 0x02) ?
755 MEGA_MBOXCMD_LWRITE:
756 MEGA_MBOXCMD_LREAD ;
757 }
758
759 /*
760 * 6-byte READ(0x08) or WRITE(0x0A) cdb
761 */
762 if( cmd->cmd_len == 6 ) {
763 mbox->m_out.numsectors = (u32) cmd->cmnd[4];
764 mbox->m_out.lba =
765 ((u32)cmd->cmnd[1] << 16) |
766 ((u32)cmd->cmnd[2] << 8) |
767 (u32)cmd->cmnd[3];
768
769 mbox->m_out.lba &= 0x1FFFFF;
770
771#if MEGA_HAVE_STATS
772 /*
773 * Take modulo 0x80, since the logical drive
774 * number increases by 0x80 when a logical
775 * drive was deleted
776 */
777 if (*cmd->cmnd == READ_6) {
778 adapter->nreads[ldrv_num%0x80]++;
779 adapter->nreadblocks[ldrv_num%0x80] +=
780 mbox->m_out.numsectors;
781 } else {
782 adapter->nwrites[ldrv_num%0x80]++;
783 adapter->nwriteblocks[ldrv_num%0x80] +=
784 mbox->m_out.numsectors;
785 }
786#endif
787 }
788
789 /*
790 * 10-byte READ(0x28) or WRITE(0x2A) cdb
791 */
792 if( cmd->cmd_len == 10 ) {
793 mbox->m_out.numsectors =
794 (u32)cmd->cmnd[8] |
795 ((u32)cmd->cmnd[7] << 8);
796 mbox->m_out.lba =
797 ((u32)cmd->cmnd[2] << 24) |
798 ((u32)cmd->cmnd[3] << 16) |
799 ((u32)cmd->cmnd[4] << 8) |
800 (u32)cmd->cmnd[5];
801
802#if MEGA_HAVE_STATS
803 if (*cmd->cmnd == READ_10) {
804 adapter->nreads[ldrv_num%0x80]++;
805 adapter->nreadblocks[ldrv_num%0x80] +=
806 mbox->m_out.numsectors;
807 } else {
808 adapter->nwrites[ldrv_num%0x80]++;
809 adapter->nwriteblocks[ldrv_num%0x80] +=
810 mbox->m_out.numsectors;
811 }
812#endif
813 }
814
815 /*
816 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
817 */
818 if( cmd->cmd_len == 12 ) {
819 mbox->m_out.lba =
820 ((u32)cmd->cmnd[2] << 24) |
821 ((u32)cmd->cmnd[3] << 16) |
822 ((u32)cmd->cmnd[4] << 8) |
823 (u32)cmd->cmnd[5];
824
825 mbox->m_out.numsectors =
826 ((u32)cmd->cmnd[6] << 24) |
827 ((u32)cmd->cmnd[7] << 16) |
828 ((u32)cmd->cmnd[8] << 8) |
829 (u32)cmd->cmnd[9];
830
831#if MEGA_HAVE_STATS
832 if (*cmd->cmnd == READ_12) {
833 adapter->nreads[ldrv_num%0x80]++;
834 adapter->nreadblocks[ldrv_num%0x80] +=
835 mbox->m_out.numsectors;
836 } else {
837 adapter->nwrites[ldrv_num%0x80]++;
838 adapter->nwriteblocks[ldrv_num%0x80] +=
839 mbox->m_out.numsectors;
840 }
841#endif
842 }
843
844 /*
845 * If it is a read command
846 */
847 if( (*cmd->cmnd & 0x0F) == 0x08 ) {
848 scb->dma_direction = PCI_DMA_FROMDEVICE;
849 }
850 else {
851 scb->dma_direction = PCI_DMA_TODEVICE;
852 }
853
854 /* Calculate Scatter-Gather info */
855 mbox->m_out.numsgelements = mega_build_sglist(adapter, scb,
Dan Carpenter9d5d93e2012-06-27 12:08:25 +0300856 (u32 *)&mbox->m_out.xferaddr, &seg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857
858 return scb;
859
860#if MEGA_HAVE_CLUSTERING
861 case RESERVE: /* Fall through */
862 case RELEASE:
863
864 /*
865 * Do we support clustering and is the support enabled
866 */
867 if( ! adapter->has_cluster ) {
868
869 cmd->result = (DID_BAD_TARGET << 16);
870 cmd->scsi_done(cmd);
871 return NULL;
872 }
873
874 /* Allocate a SCB and initialize mailbox */
875 if(!(scb = mega_allocate_scb(adapter, cmd))) {
876 *busy = 1;
877 return NULL;
878 }
879
880 scb->raw_mbox[0] = MEGA_CLUSTER_CMD;
881 scb->raw_mbox[2] = ( *cmd->cmnd == RESERVE ) ?
882 MEGA_RESERVE_LD : MEGA_RELEASE_LD;
883
884 scb->raw_mbox[3] = ldrv_num;
885
886 scb->dma_direction = PCI_DMA_NONE;
887
888 return scb;
889#endif
890
891 default:
892 cmd->result = (DID_BAD_TARGET << 16);
893 cmd->scsi_done(cmd);
894 return NULL;
895 }
896 }
897
898 /*
899 * Passthru drive commands
900 */
901 else {
902 /* Allocate a SCB and initialize passthru */
903 if(!(scb = mega_allocate_scb(adapter, cmd))) {
904 *busy = 1;
905 return NULL;
906 }
907
908 mbox = (mbox_t *)scb->raw_mbox;
909 memset(mbox, 0, sizeof(scb->raw_mbox));
910
911 if( adapter->support_ext_cdb ) {
912
913 epthru = mega_prepare_extpassthru(adapter, scb, cmd,
914 channel, target);
915
916 mbox->m_out.cmd = MEGA_MBOXCMD_EXTPTHRU;
917
918 mbox->m_out.xferaddr = scb->epthru_dma_addr;
919
920 }
921 else {
922
923 pthru = mega_prepare_passthru(adapter, scb, cmd,
924 channel, target);
925
926 /* Initialize mailbox */
927 if( adapter->has_64bit_addr ) {
928 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU64;
929 }
930 else {
931 mbox->m_out.cmd = MEGA_MBOXCMD_PASSTHRU;
932 }
933
934 mbox->m_out.xferaddr = scb->pthru_dma_addr;
935
936 }
937 return scb;
938 }
939 return NULL;
940}
941
942
943/**
944 * mega_prepare_passthru()
945 * @adapter - pointer to our soft state
946 * @scb - our scsi control block
947 * @cmd - scsi command from the mid-layer
948 * @channel - actual channel on the controller
949 * @target - actual id on the controller.
950 *
951 * prepare a command for the scsi physical devices.
952 */
953static mega_passthru *
954mega_prepare_passthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
955 int channel, int target)
956{
957 mega_passthru *pthru;
958
959 pthru = scb->pthru;
960 memset(pthru, 0, sizeof (mega_passthru));
961
962 /* 0=6sec/1=60sec/2=10min/3=3hrs */
963 pthru->timeout = 2;
964
965 pthru->ars = 1;
966 pthru->reqsenselen = 14;
967 pthru->islogical = 0;
968
969 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
970
971 pthru->target = (adapter->flag & BOARD_40LD) ?
972 (channel << 4) | target : target;
973
974 pthru->cdblen = cmd->cmd_len;
975 pthru->logdrv = cmd->device->lun;
976
977 memcpy(pthru->cdb, cmd->cmnd, cmd->cmd_len);
978
979 /* Not sure about the direction */
980 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
981
982 /* Special Code for Handling READ_CAPA/ INQ using bounce buffers */
983 switch (cmd->cmnd[0]) {
984 case INQUIRY:
985 case READ_CAPACITY:
986 if(!(adapter->flag & (1L << cmd->device->channel))) {
987
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -0500988 dev_notice(&adapter->dev->dev,
989 "scsi%d: scanning scsi channel %d [P%d] "
990 "for physical devices\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 adapter->host->host_no,
992 cmd->device->channel, channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 adapter->flag |= (1L << cmd->device->channel);
995 }
996 /* Fall through */
997 default:
998 pthru->numsgelements = mega_build_sglist(adapter, scb,
999 &pthru->dataxferaddr, &pthru->dataxferlen);
1000 break;
1001 }
1002 return pthru;
1003}
1004
1005
1006/**
1007 * mega_prepare_extpassthru()
1008 * @adapter - pointer to our soft state
1009 * @scb - our scsi control block
1010 * @cmd - scsi command from the mid-layer
1011 * @channel - actual channel on the controller
1012 * @target - actual id on the controller.
1013 *
1014 * prepare a command for the scsi physical devices. This rountine prepares
1015 * commands for devices which can take extended CDBs (>10 bytes)
1016 */
1017static mega_ext_passthru *
1018mega_prepare_extpassthru(adapter_t *adapter, scb_t *scb, Scsi_Cmnd *cmd,
1019 int channel, int target)
1020{
1021 mega_ext_passthru *epthru;
1022
1023 epthru = scb->epthru;
1024 memset(epthru, 0, sizeof(mega_ext_passthru));
1025
1026 /* 0=6sec/1=60sec/2=10min/3=3hrs */
1027 epthru->timeout = 2;
1028
1029 epthru->ars = 1;
1030 epthru->reqsenselen = 14;
1031 epthru->islogical = 0;
1032
1033 epthru->channel = (adapter->flag & BOARD_40LD) ? 0 : channel;
1034 epthru->target = (adapter->flag & BOARD_40LD) ?
1035 (channel << 4) | target : target;
1036
1037 epthru->cdblen = cmd->cmd_len;
1038 epthru->logdrv = cmd->device->lun;
1039
1040 memcpy(epthru->cdb, cmd->cmnd, cmd->cmd_len);
1041
1042 /* Not sure about the direction */
1043 scb->dma_direction = PCI_DMA_BIDIRECTIONAL;
1044
1045 switch(cmd->cmnd[0]) {
1046 case INQUIRY:
1047 case READ_CAPACITY:
1048 if(!(adapter->flag & (1L << cmd->device->channel))) {
1049
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05001050 dev_notice(&adapter->dev->dev,
1051 "scsi%d: scanning scsi channel %d [P%d] "
1052 "for physical devices\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 adapter->host->host_no,
1054 cmd->device->channel, channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 adapter->flag |= (1L << cmd->device->channel);
1057 }
1058 /* Fall through */
1059 default:
1060 epthru->numsgelements = mega_build_sglist(adapter, scb,
1061 &epthru->dataxferaddr, &epthru->dataxferlen);
1062 break;
1063 }
1064
1065 return epthru;
1066}
1067
1068static void
1069__mega_runpendq(adapter_t *adapter)
1070{
1071 scb_t *scb;
1072 struct list_head *pos, *next;
1073
1074 /* Issue any pending commands to the card */
1075 list_for_each_safe(pos, next, &adapter->pending_list) {
1076
1077 scb = list_entry(pos, scb_t, list);
1078
1079 if( !(scb->state & SCB_ISSUED) ) {
1080
1081 if( issue_scb(adapter, scb) != 0 )
1082 return;
1083 }
1084 }
1085
1086 return;
1087}
1088
1089
1090/**
1091 * issue_scb()
1092 * @adapter - pointer to our soft state
1093 * @scb - scsi control block
1094 *
1095 * Post a command to the card if the mailbox is available, otherwise return
1096 * busy. We also take the scb from the pending list if the mailbox is
1097 * available.
1098 */
1099static int
1100issue_scb(adapter_t *adapter, scb_t *scb)
1101{
1102 volatile mbox64_t *mbox64 = adapter->mbox64;
1103 volatile mbox_t *mbox = adapter->mbox;
1104 unsigned int i = 0;
1105
1106 if(unlikely(mbox->m_in.busy)) {
1107 do {
1108 udelay(1);
1109 i++;
1110 } while( mbox->m_in.busy && (i < max_mbox_busy_wait) );
1111
1112 if(mbox->m_in.busy) return -1;
1113 }
1114
1115 /* Copy mailbox data into host structure */
1116 memcpy((char *)&mbox->m_out, (char *)scb->raw_mbox,
1117 sizeof(struct mbox_out));
1118
1119 mbox->m_out.cmdid = scb->idx; /* Set cmdid */
1120 mbox->m_in.busy = 1; /* Set busy */
1121
1122
1123 /*
1124 * Increment the pending queue counter
1125 */
1126 atomic_inc(&adapter->pend_cmds);
1127
1128 switch (mbox->m_out.cmd) {
1129 case MEGA_MBOXCMD_LREAD64:
1130 case MEGA_MBOXCMD_LWRITE64:
1131 case MEGA_MBOXCMD_PASSTHRU64:
1132 case MEGA_MBOXCMD_EXTPTHRU:
1133 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1134 mbox64->xfer_segment_hi = 0;
1135 mbox->m_out.xferaddr = 0xFFFFFFFF;
1136 break;
1137 default:
1138 mbox64->xfer_segment_lo = 0;
1139 mbox64->xfer_segment_hi = 0;
1140 }
1141
1142 /*
1143 * post the command
1144 */
1145 scb->state |= SCB_ISSUED;
1146
1147 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1148 mbox->m_in.poll = 0;
1149 mbox->m_in.ack = 0;
1150 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1151 }
1152 else {
1153 irq_enable(adapter);
1154 issue_command(adapter);
1155 }
1156
1157 return 0;
1158}
1159
1160/*
1161 * Wait until the controller's mailbox is available
1162 */
1163static inline int
1164mega_busywait_mbox (adapter_t *adapter)
1165{
1166 if (adapter->mbox->m_in.busy)
1167 return __mega_busywait_mbox(adapter);
1168 return 0;
1169}
1170
1171/**
1172 * issue_scb_block()
1173 * @adapter - pointer to our soft state
1174 * @raw_mbox - the mailbox
1175 *
1176 * Issue a scb in synchronous and non-interrupt mode
1177 */
1178static int
1179issue_scb_block(adapter_t *adapter, u_char *raw_mbox)
1180{
1181 volatile mbox64_t *mbox64 = adapter->mbox64;
1182 volatile mbox_t *mbox = adapter->mbox;
1183 u8 byte;
1184
1185 /* Wait until mailbox is free */
1186 if(mega_busywait_mbox (adapter))
1187 goto bug_blocked_mailbox;
1188
1189 /* Copy mailbox data into host structure */
1190 memcpy((char *) mbox, raw_mbox, sizeof(struct mbox_out));
1191 mbox->m_out.cmdid = 0xFE;
1192 mbox->m_in.busy = 1;
1193
1194 switch (raw_mbox[0]) {
1195 case MEGA_MBOXCMD_LREAD64:
1196 case MEGA_MBOXCMD_LWRITE64:
1197 case MEGA_MBOXCMD_PASSTHRU64:
1198 case MEGA_MBOXCMD_EXTPTHRU:
1199 mbox64->xfer_segment_lo = mbox->m_out.xferaddr;
1200 mbox64->xfer_segment_hi = 0;
1201 mbox->m_out.xferaddr = 0xFFFFFFFF;
1202 break;
1203 default:
1204 mbox64->xfer_segment_lo = 0;
1205 mbox64->xfer_segment_hi = 0;
1206 }
1207
1208 if( likely(adapter->flag & BOARD_MEMMAP) ) {
1209 mbox->m_in.poll = 0;
1210 mbox->m_in.ack = 0;
1211 mbox->m_in.numstatus = 0xFF;
1212 mbox->m_in.status = 0xFF;
1213 WRINDOOR(adapter, adapter->mbox_dma | 0x1);
1214
1215 while((volatile u8)mbox->m_in.numstatus == 0xFF)
1216 cpu_relax();
1217
1218 mbox->m_in.numstatus = 0xFF;
1219
1220 while( (volatile u8)mbox->m_in.poll != 0x77 )
1221 cpu_relax();
1222
1223 mbox->m_in.poll = 0;
1224 mbox->m_in.ack = 0x77;
1225
1226 WRINDOOR(adapter, adapter->mbox_dma | 0x2);
1227
1228 while(RDINDOOR(adapter) & 0x2)
1229 cpu_relax();
1230 }
1231 else {
1232 irq_disable(adapter);
1233 issue_command(adapter);
1234
1235 while (!((byte = irq_state(adapter)) & INTR_VALID))
1236 cpu_relax();
1237
1238 set_irq_state(adapter, byte);
1239 irq_enable(adapter);
1240 irq_ack(adapter);
1241 }
1242
1243 return mbox->m_in.status;
1244
1245bug_blocked_mailbox:
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05001246 dev_warn(&adapter->dev->dev, "Blocked mailbox......!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 udelay (1000);
1248 return -1;
1249}
1250
1251
1252/**
1253 * megaraid_isr_iomapped()
1254 * @irq - irq
1255 * @devp - pointer to our soft state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 *
1257 * Interrupt service routine for io-mapped controllers.
1258 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1259 * and service the completed commands.
1260 */
1261static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001262megaraid_isr_iomapped(int irq, void *devp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263{
1264 adapter_t *adapter = devp;
1265 unsigned long flags;
1266 u8 status;
1267 u8 nstatus;
1268 u8 completed[MAX_FIRMWARE_STATUS];
1269 u8 byte;
1270 int handled = 0;
1271
1272
1273 /*
1274 * loop till F/W has more commands for us to complete.
1275 */
1276 spin_lock_irqsave(&adapter->lock, flags);
1277
1278 do {
1279 /* Check if a valid interrupt is pending */
1280 byte = irq_state(adapter);
1281 if( (byte & VALID_INTR_BYTE) == 0 ) {
1282 /*
1283 * No more pending commands
1284 */
1285 goto out_unlock;
1286 }
1287 set_irq_state(adapter, byte);
1288
1289 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1290 == 0xFF)
1291 cpu_relax();
1292 adapter->mbox->m_in.numstatus = 0xFF;
1293
1294 status = adapter->mbox->m_in.status;
1295
1296 /*
1297 * decrement the pending queue counter
1298 */
1299 atomic_sub(nstatus, &adapter->pend_cmds);
1300
1301 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1302 nstatus);
1303
1304 /* Acknowledge interrupt */
1305 irq_ack(adapter);
1306
1307 mega_cmd_done(adapter, completed, nstatus, status);
1308
1309 mega_rundoneq(adapter);
1310
1311 handled = 1;
1312
1313 /* Loop through any pending requests */
1314 if(atomic_read(&adapter->quiescent) == 0) {
1315 mega_runpendq(adapter);
1316 }
1317
1318 } while(1);
1319
1320 out_unlock:
1321
1322 spin_unlock_irqrestore(&adapter->lock, flags);
1323
1324 return IRQ_RETVAL(handled);
1325}
1326
1327
1328/**
1329 * megaraid_isr_memmapped()
1330 * @irq - irq
1331 * @devp - pointer to our soft state
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 *
1333 * Interrupt service routine for memory-mapped controllers.
1334 * Find out if our device is interrupting. If yes, acknowledge the interrupt
1335 * and service the completed commands.
1336 */
1337static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +01001338megaraid_isr_memmapped(int irq, void *devp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 adapter_t *adapter = devp;
1341 unsigned long flags;
1342 u8 status;
1343 u32 dword = 0;
1344 u8 nstatus;
1345 u8 completed[MAX_FIRMWARE_STATUS];
1346 int handled = 0;
1347
1348
1349 /*
1350 * loop till F/W has more commands for us to complete.
1351 */
1352 spin_lock_irqsave(&adapter->lock, flags);
1353
1354 do {
1355 /* Check if a valid interrupt is pending */
1356 dword = RDOUTDOOR(adapter);
1357 if(dword != 0x10001234) {
1358 /*
1359 * No more pending commands
1360 */
1361 goto out_unlock;
1362 }
1363 WROUTDOOR(adapter, 0x10001234);
1364
1365 while((nstatus = (volatile u8)adapter->mbox->m_in.numstatus)
1366 == 0xFF) {
1367 cpu_relax();
1368 }
1369 adapter->mbox->m_in.numstatus = 0xFF;
1370
1371 status = adapter->mbox->m_in.status;
1372
1373 /*
1374 * decrement the pending queue counter
1375 */
1376 atomic_sub(nstatus, &adapter->pend_cmds);
1377
1378 memcpy(completed, (void *)adapter->mbox->m_in.completed,
1379 nstatus);
1380
1381 /* Acknowledge interrupt */
1382 WRINDOOR(adapter, 0x2);
1383
1384 handled = 1;
1385
Jeff Garzik00769ec2006-12-03 20:49:23 -05001386 while( RDINDOOR(adapter) & 0x02 )
1387 cpu_relax();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
1389 mega_cmd_done(adapter, completed, nstatus, status);
1390
1391 mega_rundoneq(adapter);
1392
1393 /* Loop through any pending requests */
1394 if(atomic_read(&adapter->quiescent) == 0) {
1395 mega_runpendq(adapter);
1396 }
1397
1398 } while(1);
1399
1400 out_unlock:
1401
1402 spin_unlock_irqrestore(&adapter->lock, flags);
1403
1404 return IRQ_RETVAL(handled);
1405}
1406/**
1407 * mega_cmd_done()
1408 * @adapter - pointer to our soft state
1409 * @completed - array of ids of completed commands
1410 * @nstatus - number of completed commands
1411 * @status - status of the last command completed
1412 *
Justin P. Mattock8e572ba2011-02-02 11:31:21 +01001413 * Complete the commands and call the scsi mid-layer callback hooks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 */
1415static void
1416mega_cmd_done(adapter_t *adapter, u8 completed[], int nstatus, int status)
1417{
1418 mega_ext_passthru *epthru = NULL;
1419 struct scatterlist *sgl;
1420 Scsi_Cmnd *cmd = NULL;
1421 mega_passthru *pthru = NULL;
1422 mbox_t *mbox = NULL;
1423 u8 c;
1424 scb_t *scb;
1425 int islogical;
1426 int cmdid;
1427 int i;
1428
1429 /*
1430 * for all the commands completed, call the mid-layer callback routine
1431 * and free the scb.
1432 */
1433 for( i = 0; i < nstatus; i++ ) {
1434
1435 cmdid = completed[i];
1436
Christoph Hellwig0f2bb842014-02-20 14:20:59 -08001437 /*
1438 * Only free SCBs for the commands coming down from the
1439 * mid-layer, not for which were issued internally
1440 *
1441 * For internal command, restore the status returned by the
1442 * firmware so that user can interpret it.
1443 */
1444 if (cmdid == CMDID_INT_CMDS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 scb = &adapter->int_scb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Christoph Hellwig0f2bb842014-02-20 14:20:59 -08001447 list_del_init(&scb->list);
1448 scb->state = SCB_FREE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449
Christoph Hellwig0f2bb842014-02-20 14:20:59 -08001450 adapter->int_status = status;
1451 complete(&adapter->int_waitq);
1452 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 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 ) {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05001459 dev_crit(&adapter->dev->dev, "invalid command "
1460 "Id %d, scb->state:%x, scsi cmd:%p\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 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
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05001471 dev_warn(&adapter->dev->dev,
1472 "aborted cmd [%x] complete\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001473 scb->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
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
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05001490 dev_warn(&adapter->dev->dev,
1491 "reset cmd [%x] complete\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001492 scb->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
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
FUJITA Tomonori3f6270e2007-05-14 20:17:27 +09001553 sgl = scsi_sglist(cmd);
Jens Axboe45711f12007-10-22 21:19:53 +02001554 if( sg_page(sgl) ) {
1555 c = *(unsigned char *) sg_virt(&sgl[0]);
FUJITA Tomonori3f6270e2007-05-14 20:17:27 +09001556 } else {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05001557 dev_warn(&adapter->dev->dev, "invalid sg\n");
FUJITA Tomonori3f6270e2007-05-14 20:17:27 +09001558 c = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 }
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 = (DRIVER_SENSE << 24) |
1587 (DID_OK << 16) |
1588 (CHECK_CONDITION << 1);
1589 }
1590 else {
1591 if (mbox->m_out.cmd == MEGA_MBOXCMD_EXTPTHRU) {
1592
1593 memcpy(cmd->sense_buffer,
1594 epthru->reqsensearea, 14);
1595
1596 cmd->result = (DRIVER_SENSE << 24) |
1597 (DID_OK << 16) |
1598 (CHECK_CONDITION << 1);
1599 } else {
1600 cmd->sense_buffer[0] = 0x70;
1601 cmd->sense_buffer[2] = ABORTED_COMMAND;
1602 cmd->result |= (CHECK_CONDITION << 1);
1603 }
1604 }
1605 break;
1606
1607 case 0x08: /* ERR_DEST_DRIVE_FAILED, i.e.
1608 SCSI_STATUS_BUSY */
1609 cmd->result |= (DID_BUS_BUSY << 16) | status;
1610 break;
1611
1612 default:
1613#if MEGA_HAVE_CLUSTERING
1614 /*
1615 * If TEST_UNIT_READY fails, we know
1616 * MEGA_RESERVATION_STATUS failed
1617 */
1618 if( cmd->cmnd[0] == TEST_UNIT_READY ) {
1619 cmd->result |= (DID_ERROR << 16) |
1620 (RESERVATION_CONFLICT << 1);
1621 }
1622 else
1623 /*
1624 * Error code returned is 1 if Reserve or Release
1625 * failed or the input parameter is invalid
1626 */
1627 if( status == 1 &&
1628 (cmd->cmnd[0] == RESERVE ||
1629 cmd->cmnd[0] == RELEASE) ) {
1630
1631 cmd->result |= (DID_ERROR << 16) |
1632 (RESERVATION_CONFLICT << 1);
1633 }
1634 else
1635#endif
1636 cmd->result |= (DID_BAD_TARGET << 16)|status;
1637 }
1638
Christoph Hellwig0f2bb842014-02-20 14:20:59 -08001639 mega_free_scb(adapter, scb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
1641 /* Add Scsi_Command to end of completed queue */
1642 list_add_tail(SCSI_LIST(cmd), &adapter->completed_list);
1643 }
1644}
1645
1646
1647/*
1648 * mega_runpendq()
1649 *
1650 * Run through the list of completed requests and finish it
1651 */
1652static void
1653mega_rundoneq (adapter_t *adapter)
1654{
1655 Scsi_Cmnd *cmd;
1656 struct list_head *pos;
1657
1658 list_for_each(pos, &adapter->completed_list) {
1659
Christoph Hellwig0a041372005-10-31 18:31:56 +01001660 struct scsi_pointer* spos = (struct scsi_pointer *)pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 cmd = list_entry(spos, Scsi_Cmnd, SCp);
1663 cmd->scsi_done(cmd);
1664 }
1665
1666 INIT_LIST_HEAD(&adapter->completed_list);
1667}
1668
1669
1670/*
1671 * Free a SCB structure
1672 * Note: We assume the scsi commands associated with this scb is not free yet.
1673 */
1674static void
1675mega_free_scb(adapter_t *adapter, scb_t *scb)
1676{
1677 switch( scb->dma_type ) {
1678
1679 case MEGA_DMA_TYPE_NONE:
1680 break;
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 case MEGA_SGLIST:
FUJITA Tomonori3f6270e2007-05-14 20:17:27 +09001683 scsi_dma_unmap(scb->cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 default:
1686 break;
1687 }
1688
1689 /*
1690 * Remove from the pending list
1691 */
1692 list_del_init(&scb->list);
1693
1694 /* Link the scb back into free list */
1695 scb->state = SCB_FREE;
1696 scb->cmd = NULL;
1697
1698 list_add(&scb->list, &adapter->free_list);
1699}
1700
1701
1702static int
1703__mega_busywait_mbox (adapter_t *adapter)
1704{
1705 volatile mbox_t *mbox = adapter->mbox;
1706 long counter;
1707
1708 for (counter = 0; counter < 10000; counter++) {
1709 if (!mbox->m_in.busy)
1710 return 0;
Amol Lade1fa0ce2007-04-26 00:35:13 -07001711 udelay(100);
1712 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
1714 return -1; /* give up after 1 second */
1715}
1716
1717/*
1718 * Copies data to SGLIST
1719 * Note: For 64 bit cards, we need a minimum of one SG element for read/write
1720 */
1721static int
1722mega_build_sglist(adapter_t *adapter, scb_t *scb, u32 *buf, u32 *len)
1723{
FUJITA Tomonori3f6270e2007-05-14 20:17:27 +09001724 struct scatterlist *sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 Scsi_Cmnd *cmd;
1726 int sgcnt;
1727 int idx;
1728
1729 cmd = scb->cmd;
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /*
1732 * Copy Scatter-Gather list info into controller structure.
1733 *
1734 * The number of sg elements returned must not exceed our limit
1735 */
FUJITA Tomonori3f6270e2007-05-14 20:17:27 +09001736 sgcnt = scsi_dma_map(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737
1738 scb->dma_type = MEGA_SGLIST;
1739
FUJITA Tomonori3f6270e2007-05-14 20:17:27 +09001740 BUG_ON(sgcnt > adapter->sglen || sgcnt < 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741
James Bottomley51c928c2005-10-01 09:38:05 -05001742 *len = 0;
1743
FUJITA Tomonorid5e89382007-10-03 09:00:58 +09001744 if (scsi_sg_count(cmd) == 1 && !adapter->has_64bit_addr) {
1745 sg = scsi_sglist(cmd);
1746 scb->dma_h_bulkdata = sg_dma_address(sg);
1747 *buf = (u32)scb->dma_h_bulkdata;
1748 *len = sg_dma_len(sg);
1749 return 0;
1750 }
1751
FUJITA Tomonori3f6270e2007-05-14 20:17:27 +09001752 scsi_for_each_sg(cmd, sg, sgcnt, idx) {
1753 if (adapter->has_64bit_addr) {
1754 scb->sgl64[idx].address = sg_dma_address(sg);
1755 *len += scb->sgl64[idx].length = sg_dma_len(sg);
1756 } else {
1757 scb->sgl[idx].address = sg_dma_address(sg);
1758 *len += scb->sgl[idx].length = sg_dma_len(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
1760 }
1761
1762 /* Reset pointer and length fields */
1763 *buf = scb->sgl_dma_addr;
1764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 /* Return count of SG requests */
1766 return sgcnt;
1767}
1768
1769
1770/*
1771 * mega_8_to_40ld()
1772 *
1773 * takes all info in AdapterInquiry structure and puts it into ProductInfo and
1774 * Enquiry3 structures for later use
1775 */
1776static void
1777mega_8_to_40ld(mraid_inquiry *inquiry, mega_inquiry3 *enquiry3,
1778 mega_product_info *product_info)
1779{
1780 int i;
1781
1782 product_info->max_commands = inquiry->adapter_info.max_commands;
1783 enquiry3->rebuild_rate = inquiry->adapter_info.rebuild_rate;
1784 product_info->nchannels = inquiry->adapter_info.nchannels;
1785
1786 for (i = 0; i < 4; i++) {
1787 product_info->fw_version[i] =
1788 inquiry->adapter_info.fw_version[i];
1789
1790 product_info->bios_version[i] =
1791 inquiry->adapter_info.bios_version[i];
1792 }
1793 enquiry3->cache_flush_interval =
1794 inquiry->adapter_info.cache_flush_interval;
1795
1796 product_info->dram_size = inquiry->adapter_info.dram_size;
1797
1798 enquiry3->num_ldrv = inquiry->logdrv_info.num_ldrv;
1799
1800 for (i = 0; i < MAX_LOGICAL_DRIVES_8LD; i++) {
1801 enquiry3->ldrv_size[i] = inquiry->logdrv_info.ldrv_size[i];
1802 enquiry3->ldrv_prop[i] = inquiry->logdrv_info.ldrv_prop[i];
1803 enquiry3->ldrv_state[i] = inquiry->logdrv_info.ldrv_state[i];
1804 }
1805
1806 for (i = 0; i < (MAX_PHYSICAL_DRIVES); i++)
1807 enquiry3->pdrv_state[i] = inquiry->pdrv_info.pdrv_state[i];
1808}
1809
1810static inline void
1811mega_free_sgl(adapter_t *adapter)
1812{
1813 scb_t *scb;
1814 int i;
1815
1816 for(i = 0; i < adapter->max_cmds; i++) {
1817
1818 scb = &adapter->scb_list[i];
1819
1820 if( scb->sgl64 ) {
1821 pci_free_consistent(adapter->dev,
1822 sizeof(mega_sgl64) * adapter->sglen,
1823 scb->sgl64,
1824 scb->sgl_dma_addr);
1825
1826 scb->sgl64 = NULL;
1827 }
1828
1829 if( scb->pthru ) {
1830 pci_free_consistent(adapter->dev, sizeof(mega_passthru),
1831 scb->pthru, scb->pthru_dma_addr);
1832
1833 scb->pthru = NULL;
1834 }
1835
1836 if( scb->epthru ) {
1837 pci_free_consistent(adapter->dev,
1838 sizeof(mega_ext_passthru),
1839 scb->epthru, scb->epthru_dma_addr);
1840
1841 scb->epthru = NULL;
1842 }
1843
1844 }
1845}
1846
1847
1848/*
1849 * Get information about the card/driver
1850 */
1851const char *
1852megaraid_info(struct Scsi_Host *host)
1853{
1854 static char buffer[512];
1855 adapter_t *adapter;
1856
1857 adapter = (adapter_t *)host->hostdata;
1858
1859 sprintf (buffer,
1860 "LSI Logic MegaRAID %s %d commands %d targs %d chans %d luns",
1861 adapter->fw_version, adapter->product_info.max_commands,
1862 adapter->host->max_id, adapter->host->max_channel,
Hannes Reinecke1abf6352014-06-25 15:27:38 +02001863 (u32)adapter->host->max_lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 return buffer;
1865}
1866
1867/*
1868 * Abort a previous SCSI request. Only commands on the pending list can be
1869 * aborted. All the commands issued to the F/W must complete.
1870 */
1871static int
1872megaraid_abort(Scsi_Cmnd *cmd)
1873{
1874 adapter_t *adapter;
1875 int rval;
1876
1877 adapter = (adapter_t *)cmd->device->host->hostdata;
1878
1879 rval = megaraid_abort_and_reset(adapter, cmd, SCB_ABORT);
1880
1881 /*
1882 * This is required here to complete any completed requests
1883 * to be communicated over to the mid layer.
1884 */
1885 mega_rundoneq(adapter);
1886
1887 return rval;
1888}
1889
1890
1891static int
James Bottomleyfa4c4962005-06-26 08:45:39 -05001892megaraid_reset(struct scsi_cmnd *cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893{
1894 adapter_t *adapter;
1895 megacmd_t mc;
1896 int rval;
1897
1898 adapter = (adapter_t *)cmd->device->host->hostdata;
1899
1900#if MEGA_HAVE_CLUSTERING
1901 mc.cmd = MEGA_CLUSTER_CMD;
1902 mc.opcode = MEGA_RESET_RESERVATIONS;
1903
Christoph Hellwigcb0258a2005-10-31 20:12:07 +01001904 if( mega_internal_command(adapter, &mc, NULL) != 0 ) {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05001905 dev_warn(&adapter->dev->dev, "reservation reset failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
1907 else {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05001908 dev_info(&adapter->dev->dev, "reservation reset\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910#endif
1911
James Bottomleyfa4c4962005-06-26 08:45:39 -05001912 spin_lock_irq(&adapter->lock);
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 rval = megaraid_abort_and_reset(adapter, cmd, SCB_RESET);
1915
1916 /*
1917 * This is required here to complete any completed requests
1918 * to be communicated over to the mid layer.
1919 */
1920 mega_rundoneq(adapter);
James Bottomleyfa4c4962005-06-26 08:45:39 -05001921 spin_unlock_irq(&adapter->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 return rval;
1924}
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926/**
1927 * megaraid_abort_and_reset()
1928 * @adapter - megaraid soft state
1929 * @cmd - scsi command to be aborted or reset
1930 * @aor - abort or reset flag
1931 *
1932 * Try to locate the scsi command in the pending queue. If found and is not
1933 * issued to the controller, abort/reset it. Otherwise return failure
1934 */
1935static int
1936megaraid_abort_and_reset(adapter_t *adapter, Scsi_Cmnd *cmd, int aor)
1937{
1938 struct list_head *pos, *next;
1939 scb_t *scb;
1940
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05001941 dev_warn(&adapter->dev->dev, "%s cmd=%x <c=%d t=%d l=%d>\n",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001942 (aor == SCB_ABORT)? "ABORTING":"RESET",
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001943 cmd->cmnd[0], cmd->device->channel,
1944 cmd->device->id, (u32)cmd->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
1946 if(list_empty(&adapter->pending_list))
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01001947 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 list_for_each_safe(pos, next, &adapter->pending_list) {
1950
1951 scb = list_entry(pos, scb_t, list);
1952
1953 if (scb->cmd == cmd) { /* Found command */
1954
1955 scb->state |= aor;
1956
1957 /*
Nick Andrewd41ad932009-01-03 18:55:39 +11001958 * Check if this command has firmware ownership. If
1959 * yes, we cannot reset this command. Whenever f/w
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 * completes this command, we will return appropriate
1961 * status from ISR.
1962 */
1963 if( scb->state & SCB_ISSUED ) {
1964
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05001965 dev_warn(&adapter->dev->dev,
1966 "%s[%x], fw owner\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 (aor==SCB_ABORT) ? "ABORTING":"RESET",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001968 scb->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01001970 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 }
1972 else {
1973
1974 /*
1975 * Not yet issued! Remove from the pending
1976 * list
1977 */
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05001978 dev_warn(&adapter->dev->dev,
1979 "%s-[%x], driver owner\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 (aor==SCB_ABORT) ? "ABORTING":"RESET",
Christoph Hellwig5cd049a2011-04-04 09:42:14 -04001981 scb->idx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982
1983 mega_free_scb(adapter, scb);
1984
1985 if( aor == SCB_ABORT ) {
1986 cmd->result = (DID_ABORT << 16);
1987 }
1988 else {
1989 cmd->result = (DID_RESET << 16);
1990 }
1991
1992 list_add_tail(SCSI_LIST(cmd),
1993 &adapter->completed_list);
1994
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01001995 return SUCCESS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997 }
1998 }
1999
Hannes Reineckeb6c92b72014-10-30 09:44:36 +01002000 return FAILED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001}
2002
2003static inline int
2004make_local_pdev(adapter_t *adapter, struct pci_dev **pdev)
2005{
Gu Zheng8b1fce02013-05-25 21:48:31 +08002006 *pdev = pci_alloc_dev(NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 if( *pdev == NULL ) return -1;
2009
2010 memcpy(*pdev, adapter->dev, sizeof(struct pci_dev));
2011
Yang Hongyang284901a2009-04-06 19:01:15 -07002012 if( pci_set_dma_mask(*pdev, DMA_BIT_MASK(32)) != 0 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 kfree(*pdev);
2014 return -1;
2015 }
2016
2017 return 0;
2018}
2019
2020static inline void
2021free_local_pdev(struct pci_dev *pdev)
2022{
2023 kfree(pdev);
2024}
2025
2026/**
2027 * mega_allocate_inquiry()
2028 * @dma_handle - handle returned for dma address
2029 * @pdev - handle to pci device
2030 *
2031 * allocates memory for inquiry structure
2032 */
2033static inline void *
2034mega_allocate_inquiry(dma_addr_t *dma_handle, struct pci_dev *pdev)
2035{
2036 return pci_alloc_consistent(pdev, sizeof(mega_inquiry3), dma_handle);
2037}
2038
2039
2040static inline void
2041mega_free_inquiry(void *inquiry, dma_addr_t dma_handle, struct pci_dev *pdev)
2042{
2043 pci_free_consistent(pdev, sizeof(mega_inquiry3), inquiry, dma_handle);
2044}
2045
2046
2047#ifdef CONFIG_PROC_FS
2048/* Following code handles /proc fs */
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050/**
David Howellsc7f079c2013-04-10 13:33:21 +01002051 * proc_show_config()
2052 * @m - Synthetic file construction data
2053 * @v - File iterator
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 *
2055 * Display configuration information about the controller.
2056 */
2057static int
David Howellsc7f079c2013-04-10 13:33:21 +01002058proc_show_config(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059{
2060
David Howellsc7f079c2013-04-10 13:33:21 +01002061 adapter_t *adapter = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
David Howellsc7f079c2013-04-10 13:33:21 +01002063 seq_puts(m, MEGARAID_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if(adapter->product_info.product_name[0])
David Howellsc7f079c2013-04-10 13:33:21 +01002065 seq_printf(m, "%s\n", adapter->product_info.product_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
David Howellsc7f079c2013-04-10 13:33:21 +01002067 seq_puts(m, "Controller Type: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
David Howellsc7f079c2013-04-10 13:33:21 +01002069 if( adapter->flag & BOARD_MEMMAP )
2070 seq_puts(m, "438/466/467/471/493/518/520/531/532\n");
2071 else
2072 seq_puts(m, "418/428/434\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073
David Howellsc7f079c2013-04-10 13:33:21 +01002074 if(adapter->flag & BOARD_40LD)
2075 seq_puts(m, "Controller Supports 40 Logical Drives\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
David Howellsc7f079c2013-04-10 13:33:21 +01002077 if(adapter->flag & BOARD_64BIT)
2078 seq_puts(m, "Controller capable of 64-bit memory addressing\n");
2079 if( adapter->has_64bit_addr )
2080 seq_puts(m, "Controller using 64-bit memory addressing\n");
2081 else
2082 seq_puts(m, "Controller is not using 64-bit memory addressing\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
David Howellsc7f079c2013-04-10 13:33:21 +01002084 seq_printf(m, "Base = %08lx, Irq = %d, ",
2085 adapter->base, adapter->host->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
David Howellsc7f079c2013-04-10 13:33:21 +01002087 seq_printf(m, "Logical Drives = %d, Channels = %d\n",
2088 adapter->numldrv, adapter->product_info.nchannels);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
David Howellsc7f079c2013-04-10 13:33:21 +01002090 seq_printf(m, "Version =%s:%s, DRAM = %dMb\n",
2091 adapter->fw_version, adapter->bios_version,
2092 adapter->product_info.dram_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
David Howellsc7f079c2013-04-10 13:33:21 +01002094 seq_printf(m, "Controller Queue Depth = %d, Driver Queue Depth = %d\n",
2095 adapter->product_info.max_commands, adapter->max_cmds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
David Howellsc7f079c2013-04-10 13:33:21 +01002097 seq_printf(m, "support_ext_cdb = %d\n", adapter->support_ext_cdb);
2098 seq_printf(m, "support_random_del = %d\n", adapter->support_random_del);
2099 seq_printf(m, "boot_ldrv_enabled = %d\n", adapter->boot_ldrv_enabled);
2100 seq_printf(m, "boot_ldrv = %d\n", adapter->boot_ldrv);
2101 seq_printf(m, "boot_pdrv_enabled = %d\n", adapter->boot_pdrv_enabled);
2102 seq_printf(m, "boot_pdrv_ch = %d\n", adapter->boot_pdrv_ch);
2103 seq_printf(m, "boot_pdrv_tgt = %d\n", adapter->boot_pdrv_tgt);
2104 seq_printf(m, "quiescent = %d\n",
2105 atomic_read(&adapter->quiescent));
2106 seq_printf(m, "has_cluster = %d\n", adapter->has_cluster);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
David Howellsc7f079c2013-04-10 13:33:21 +01002108 seq_puts(m, "\nModule Parameters:\n");
2109 seq_printf(m, "max_cmd_per_lun = %d\n", max_cmd_per_lun);
2110 seq_printf(m, "max_sectors_per_io = %d\n", max_sectors_per_io);
2111 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114/**
David Howellsc7f079c2013-04-10 13:33:21 +01002115 * proc_show_stat()
2116 * @m - Synthetic file construction data
2117 * @v - File iterator
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 *
David Howellsc7f079c2013-04-10 13:33:21 +01002119 * Display statistical information about the I/O activity.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 */
2121static int
David Howellsc7f079c2013-04-10 13:33:21 +01002122proc_show_stat(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
David Howellsc7f079c2013-04-10 13:33:21 +01002124 adapter_t *adapter = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125#if MEGA_HAVE_STATS
David Howellsc7f079c2013-04-10 13:33:21 +01002126 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127#endif
2128
David Howellsc7f079c2013-04-10 13:33:21 +01002129 seq_puts(m, "Statistical Information for this controller\n");
2130 seq_printf(m, "pend_cmds = %d\n", atomic_read(&adapter->pend_cmds));
2131#if MEGA_HAVE_STATS
2132 for(i = 0; i < adapter->numldrv; i++) {
2133 seq_printf(m, "Logical Drive %d:\n", i);
2134 seq_printf(m, "\tReads Issued = %lu, Writes Issued = %lu\n",
2135 adapter->nreads[i], adapter->nwrites[i]);
2136 seq_printf(m, "\tSectors Read = %lu, Sectors Written = %lu\n",
2137 adapter->nreadblocks[i], adapter->nwriteblocks[i]);
2138 seq_printf(m, "\tRead errors = %lu, Write errors = %lu\n\n",
2139 adapter->rd_errors[i], adapter->wr_errors[i]);
2140 }
2141#else
2142 seq_puts(m, "IO and error counters not compiled in driver.\n");
2143#endif
2144 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145}
2146
2147
2148/**
David Howellsc7f079c2013-04-10 13:33:21 +01002149 * proc_show_mbox()
2150 * @m - Synthetic file construction data
2151 * @v - File iterator
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 *
2153 * Display mailbox information for the last command issued. This information
2154 * is good for debugging.
2155 */
2156static int
David Howellsc7f079c2013-04-10 13:33:21 +01002157proc_show_mbox(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158{
David Howellsc7f079c2013-04-10 13:33:21 +01002159 adapter_t *adapter = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 volatile mbox_t *mbox = adapter->mbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
David Howellsc7f079c2013-04-10 13:33:21 +01002162 seq_puts(m, "Contents of Mail Box Structure\n");
2163 seq_printf(m, " Fw Command = 0x%02x\n", mbox->m_out.cmd);
2164 seq_printf(m, " Cmd Sequence = 0x%02x\n", mbox->m_out.cmdid);
2165 seq_printf(m, " No of Sectors= %04d\n", mbox->m_out.numsectors);
2166 seq_printf(m, " LBA = 0x%02x\n", mbox->m_out.lba);
2167 seq_printf(m, " DTA = 0x%08x\n", mbox->m_out.xferaddr);
2168 seq_printf(m, " Logical Drive= 0x%02x\n", mbox->m_out.logdrv);
2169 seq_printf(m, " No of SG Elmt= 0x%02x\n", mbox->m_out.numsgelements);
2170 seq_printf(m, " Busy = %01x\n", mbox->m_in.busy);
2171 seq_printf(m, " Status = 0x%02x\n", mbox->m_in.status);
2172 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173}
2174
2175
2176/**
David Howellsc7f079c2013-04-10 13:33:21 +01002177 * proc_show_rebuild_rate()
2178 * @m - Synthetic file construction data
2179 * @v - File iterator
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 *
2181 * Display current rebuild rate
2182 */
2183static int
David Howellsc7f079c2013-04-10 13:33:21 +01002184proc_show_rebuild_rate(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
David Howellsc7f079c2013-04-10 13:33:21 +01002186 adapter_t *adapter = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 dma_addr_t dma_handle;
2188 caddr_t inquiry;
2189 struct pci_dev *pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
David Howellsc7f079c2013-04-10 13:33:21 +01002191 if( make_local_pdev(adapter, &pdev) != 0 )
2192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193
David Howellsc7f079c2013-04-10 13:33:21 +01002194 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2195 goto free_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
2197 if( mega_adapinq(adapter, dma_handle) != 0 ) {
David Howellsc7f079c2013-04-10 13:33:21 +01002198 seq_puts(m, "Adapter inquiry failed.\n");
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05002199 dev_warn(&adapter->dev->dev, "inquiry failed\n");
David Howellsc7f079c2013-04-10 13:33:21 +01002200 goto free_inquiry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 }
2202
David Howellsc7f079c2013-04-10 13:33:21 +01002203 if( adapter->flag & BOARD_40LD )
2204 seq_printf(m, "Rebuild Rate: [%d%%]\n",
2205 ((mega_inquiry3 *)inquiry)->rebuild_rate);
2206 else
2207 seq_printf(m, "Rebuild Rate: [%d%%]\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 ((mraid_ext_inquiry *)
David Howellsc7f079c2013-04-10 13:33:21 +01002209 inquiry)->raid_inq.adapter_info.rebuild_rate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
David Howellsc7f079c2013-04-10 13:33:21 +01002211free_inquiry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 mega_free_inquiry(inquiry, dma_handle, pdev);
David Howellsc7f079c2013-04-10 13:33:21 +01002213free_pdev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 free_local_pdev(pdev);
David Howellsc7f079c2013-04-10 13:33:21 +01002215 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216}
2217
2218
2219/**
David Howellsc7f079c2013-04-10 13:33:21 +01002220 * proc_show_battery()
2221 * @m - Synthetic file construction data
2222 * @v - File iterator
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 *
2224 * Display information about the battery module on the controller.
2225 */
2226static int
David Howellsc7f079c2013-04-10 13:33:21 +01002227proc_show_battery(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228{
David Howellsc7f079c2013-04-10 13:33:21 +01002229 adapter_t *adapter = m->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 dma_addr_t dma_handle;
2231 caddr_t inquiry;
2232 struct pci_dev *pdev;
David Howellsc7f079c2013-04-10 13:33:21 +01002233 u8 battery_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234
David Howellsc7f079c2013-04-10 13:33:21 +01002235 if( make_local_pdev(adapter, &pdev) != 0 )
2236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
David Howellsc7f079c2013-04-10 13:33:21 +01002238 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2239 goto free_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
2241 if( mega_adapinq(adapter, dma_handle) != 0 ) {
Rasmus Villemoes91c40f22014-12-03 00:10:52 +01002242 seq_puts(m, "Adapter inquiry failed.\n");
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05002243 dev_warn(&adapter->dev->dev, "inquiry failed\n");
David Howellsc7f079c2013-04-10 13:33:21 +01002244 goto free_inquiry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
2246
2247 if( adapter->flag & BOARD_40LD ) {
2248 battery_status = ((mega_inquiry3 *)inquiry)->battery_status;
2249 }
2250 else {
2251 battery_status = ((mraid_ext_inquiry *)inquiry)->
2252 raid_inq.adapter_info.battery_status;
2253 }
2254
2255 /*
2256 * Decode the battery status
2257 */
David Howellsc7f079c2013-04-10 13:33:21 +01002258 seq_printf(m, "Battery Status:[%d]", battery_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259
2260 if(battery_status == MEGA_BATT_CHARGE_DONE)
David Howellsc7f079c2013-04-10 13:33:21 +01002261 seq_puts(m, " Charge Done");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
2263 if(battery_status & MEGA_BATT_MODULE_MISSING)
David Howellsc7f079c2013-04-10 13:33:21 +01002264 seq_puts(m, " Module Missing");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 if(battery_status & MEGA_BATT_LOW_VOLTAGE)
David Howellsc7f079c2013-04-10 13:33:21 +01002267 seq_puts(m, " Low Voltage");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 if(battery_status & MEGA_BATT_TEMP_HIGH)
David Howellsc7f079c2013-04-10 13:33:21 +01002270 seq_puts(m, " Temperature High");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
2272 if(battery_status & MEGA_BATT_PACK_MISSING)
David Howellsc7f079c2013-04-10 13:33:21 +01002273 seq_puts(m, " Pack Missing");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
2275 if(battery_status & MEGA_BATT_CHARGE_INPROG)
David Howellsc7f079c2013-04-10 13:33:21 +01002276 seq_puts(m, " Charge In-progress");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
2278 if(battery_status & MEGA_BATT_CHARGE_FAIL)
David Howellsc7f079c2013-04-10 13:33:21 +01002279 seq_puts(m, " Charge Fail");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
2281 if(battery_status & MEGA_BATT_CYCLES_EXCEEDED)
David Howellsc7f079c2013-04-10 13:33:21 +01002282 seq_puts(m, " Cycles Exceeded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
David Howellsc7f079c2013-04-10 13:33:21 +01002284 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
David Howellsc7f079c2013-04-10 13:33:21 +01002286free_inquiry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 mega_free_inquiry(inquiry, dma_handle, pdev);
David Howellsc7f079c2013-04-10 13:33:21 +01002288free_pdev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 free_local_pdev(pdev);
David Howellsc7f079c2013-04-10 13:33:21 +01002290 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291}
2292
2293
David Howellsc7f079c2013-04-10 13:33:21 +01002294/*
2295 * Display scsi inquiry
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 */
David Howellsc7f079c2013-04-10 13:33:21 +01002297static void
2298mega_print_inquiry(struct seq_file *m, char *scsi_inq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299{
David Howellsc7f079c2013-04-10 13:33:21 +01002300 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
David Howellsc7f079c2013-04-10 13:33:21 +01002302 seq_puts(m, " Vendor: ");
2303 seq_write(m, scsi_inq + 8, 8);
2304 seq_puts(m, " Model: ");
2305 seq_write(m, scsi_inq + 16, 16);
2306 seq_puts(m, " Rev: ");
2307 seq_write(m, scsi_inq + 32, 4);
2308 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
David Howellsc7f079c2013-04-10 13:33:21 +01002310 i = scsi_inq[0] & 0x1f;
2311 seq_printf(m, " Type: %s ", scsi_device_type(i));
2312
2313 seq_printf(m, " ANSI SCSI revision: %02x",
2314 scsi_inq[2] & 0x07);
2315
2316 if( (scsi_inq[2] & 0x07) == 1 && (scsi_inq[3] & 0x0f) == 1 )
2317 seq_puts(m, " CCS\n");
2318 else
2319 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320}
2321
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322/**
David Howellsc7f079c2013-04-10 13:33:21 +01002323 * proc_show_pdrv()
2324 * @m - Synthetic file construction data
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 * @page - buffer to write the data in
2326 * @adapter - pointer to our soft state
2327 *
2328 * Display information about the physical drives.
2329 */
2330static int
David Howellsc7f079c2013-04-10 13:33:21 +01002331proc_show_pdrv(struct seq_file *m, adapter_t *adapter, int channel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332{
2333 dma_addr_t dma_handle;
2334 char *scsi_inq;
2335 dma_addr_t scsi_inq_dma_handle;
2336 caddr_t inquiry;
2337 struct pci_dev *pdev;
2338 u8 *pdrv_state;
2339 u8 state;
2340 int tgt;
2341 int max_channels;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 int i;
2343
David Howellsc7f079c2013-04-10 13:33:21 +01002344 if( make_local_pdev(adapter, &pdev) != 0 )
2345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
David Howellsc7f079c2013-04-10 13:33:21 +01002347 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 goto free_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 if( mega_adapinq(adapter, dma_handle) != 0 ) {
David Howellsc7f079c2013-04-10 13:33:21 +01002351 seq_puts(m, "Adapter inquiry failed.\n");
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05002352 dev_warn(&adapter->dev->dev, "inquiry failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 goto free_inquiry;
2354 }
2355
2356
2357 scsi_inq = pci_alloc_consistent(pdev, 256, &scsi_inq_dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 if( scsi_inq == NULL ) {
David Howellsc7f079c2013-04-10 13:33:21 +01002359 seq_puts(m, "memory not available for scsi inq.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 goto free_inquiry;
2361 }
2362
2363 if( adapter->flag & BOARD_40LD ) {
2364 pdrv_state = ((mega_inquiry3 *)inquiry)->pdrv_state;
2365 }
2366 else {
2367 pdrv_state = ((mraid_ext_inquiry *)inquiry)->
2368 raid_inq.pdrv_info.pdrv_state;
2369 }
2370
2371 max_channels = adapter->product_info.nchannels;
2372
2373 if( channel >= max_channels ) {
2374 goto free_pci;
2375 }
2376
2377 for( tgt = 0; tgt <= MAX_TARGET; tgt++ ) {
2378
2379 i = channel*16 + tgt;
2380
2381 state = *(pdrv_state + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 switch( state & 0x0F ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 case PDRV_ONLINE:
David Howellsc7f079c2013-04-10 13:33:21 +01002384 seq_printf(m, "Channel:%2d Id:%2d State: Online",
2385 channel, tgt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 break;
2387
2388 case PDRV_FAILED:
David Howellsc7f079c2013-04-10 13:33:21 +01002389 seq_printf(m, "Channel:%2d Id:%2d State: Failed",
2390 channel, tgt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 break;
2392
2393 case PDRV_RBLD:
David Howellsc7f079c2013-04-10 13:33:21 +01002394 seq_printf(m, "Channel:%2d Id:%2d State: Rebuild",
2395 channel, tgt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 break;
2397
2398 case PDRV_HOTSPARE:
David Howellsc7f079c2013-04-10 13:33:21 +01002399 seq_printf(m, "Channel:%2d Id:%2d State: Hot spare",
2400 channel, tgt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 break;
2402
2403 default:
David Howellsc7f079c2013-04-10 13:33:21 +01002404 seq_printf(m, "Channel:%2d Id:%2d State: Un-configured",
2405 channel, tgt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 }
2408
2409 /*
2410 * This interface displays inquiries for disk drives
2411 * only. Inquries for logical drives and non-disk
2412 * devices are available through /proc/scsi/scsi
2413 */
2414 memset(scsi_inq, 0, 256);
2415 if( mega_internal_dev_inquiry(adapter, channel, tgt,
2416 scsi_inq_dma_handle) ||
2417 (scsi_inq[0] & 0x1F) != TYPE_DISK ) {
2418 continue;
2419 }
2420
2421 /*
2422 * Check for overflow. We print less than 240
2423 * characters for inquiry
2424 */
David Howellsc7f079c2013-04-10 13:33:21 +01002425 seq_puts(m, ".\n");
2426 mega_print_inquiry(m, scsi_inq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 }
2428
2429free_pci:
2430 pci_free_consistent(pdev, 256, scsi_inq, scsi_inq_dma_handle);
2431free_inquiry:
2432 mega_free_inquiry(inquiry, dma_handle, pdev);
2433free_pdev:
2434 free_local_pdev(pdev);
David Howellsc7f079c2013-04-10 13:33:21 +01002435 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438/**
David Howellsc7f079c2013-04-10 13:33:21 +01002439 * proc_show_pdrv_ch0()
2440 * @m - Synthetic file construction data
2441 * @v - File iterator
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 *
David Howellsc7f079c2013-04-10 13:33:21 +01002443 * Display information about the physical drives on physical channel 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 */
2445static int
David Howellsc7f079c2013-04-10 13:33:21 +01002446proc_show_pdrv_ch0(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
David Howellsc7f079c2013-04-10 13:33:21 +01002448 return proc_show_pdrv(m, m->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449}
2450
2451
2452/**
David Howellsc7f079c2013-04-10 13:33:21 +01002453 * proc_show_pdrv_ch1()
2454 * @m - Synthetic file construction data
2455 * @v - File iterator
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 *
David Howellsc7f079c2013-04-10 13:33:21 +01002457 * Display information about the physical drives on physical channel 1.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 */
2459static int
David Howellsc7f079c2013-04-10 13:33:21 +01002460proc_show_pdrv_ch1(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
David Howellsc7f079c2013-04-10 13:33:21 +01002462 return proc_show_pdrv(m, m->private, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463}
2464
2465
2466/**
David Howellsc7f079c2013-04-10 13:33:21 +01002467 * proc_show_pdrv_ch2()
2468 * @m - Synthetic file construction data
2469 * @v - File iterator
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 *
David Howellsc7f079c2013-04-10 13:33:21 +01002471 * Display information about the physical drives on physical channel 2.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 */
2473static int
David Howellsc7f079c2013-04-10 13:33:21 +01002474proc_show_pdrv_ch2(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475{
David Howellsc7f079c2013-04-10 13:33:21 +01002476 return proc_show_pdrv(m, m->private, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477}
2478
2479
2480/**
David Howellsc7f079c2013-04-10 13:33:21 +01002481 * proc_show_pdrv_ch3()
2482 * @m - Synthetic file construction data
2483 * @v - File iterator
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 *
David Howellsc7f079c2013-04-10 13:33:21 +01002485 * Display information about the physical drives on physical channel 3.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 */
2487static int
David Howellsc7f079c2013-04-10 13:33:21 +01002488proc_show_pdrv_ch3(struct seq_file *m, void *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
David Howellsc7f079c2013-04-10 13:33:21 +01002490 return proc_show_pdrv(m, m->private, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491}
2492
2493
2494/**
David Howellsc7f079c2013-04-10 13:33:21 +01002495 * proc_show_rdrv()
2496 * @m - Synthetic file construction data
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 * @adapter - pointer to our soft state
2498 * @start - starting logical drive to display
2499 * @end - ending logical drive to display
2500 *
2501 * We do not print the inquiry information since its already available through
2502 * /proc/scsi/scsi interface
2503 */
2504static int
David Howellsc7f079c2013-04-10 13:33:21 +01002505proc_show_rdrv(struct seq_file *m, adapter_t *adapter, int start, int end )
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
2507 dma_addr_t dma_handle;
2508 logdrv_param *lparam;
2509 megacmd_t mc;
2510 char *disk_array;
2511 dma_addr_t disk_array_dma_handle;
2512 caddr_t inquiry;
2513 struct pci_dev *pdev;
2514 u8 *rdrv_state;
2515 int num_ldrv;
2516 u32 array_sz;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 int i;
2518
David Howellsc7f079c2013-04-10 13:33:21 +01002519 if( make_local_pdev(adapter, &pdev) != 0 )
2520 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
David Howellsc7f079c2013-04-10 13:33:21 +01002522 if( (inquiry = mega_allocate_inquiry(&dma_handle, pdev)) == NULL )
2523 goto free_pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
2525 if( mega_adapinq(adapter, dma_handle) != 0 ) {
David Howellsc7f079c2013-04-10 13:33:21 +01002526 seq_puts(m, "Adapter inquiry failed.\n");
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05002527 dev_warn(&adapter->dev->dev, "inquiry failed\n");
David Howellsc7f079c2013-04-10 13:33:21 +01002528 goto free_inquiry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 }
2530
2531 memset(&mc, 0, sizeof(megacmd_t));
2532
2533 if( adapter->flag & BOARD_40LD ) {
2534 array_sz = sizeof(disk_array_40ld);
2535
2536 rdrv_state = ((mega_inquiry3 *)inquiry)->ldrv_state;
2537
2538 num_ldrv = ((mega_inquiry3 *)inquiry)->num_ldrv;
2539 }
2540 else {
2541 array_sz = sizeof(disk_array_8ld);
2542
2543 rdrv_state = ((mraid_ext_inquiry *)inquiry)->
2544 raid_inq.logdrv_info.ldrv_state;
2545
2546 num_ldrv = ((mraid_ext_inquiry *)inquiry)->
2547 raid_inq.logdrv_info.num_ldrv;
2548 }
2549
2550 disk_array = pci_alloc_consistent(pdev, array_sz,
2551 &disk_array_dma_handle);
2552
2553 if( disk_array == NULL ) {
David Howellsc7f079c2013-04-10 13:33:21 +01002554 seq_puts(m, "memory not available.\n");
2555 goto free_inquiry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 }
2557
2558 mc.xferaddr = (u32)disk_array_dma_handle;
2559
2560 if( adapter->flag & BOARD_40LD ) {
2561 mc.cmd = FC_NEW_CONFIG;
2562 mc.opcode = OP_DCMD_READ_CONFIG;
2563
Christoph Hellwigcb0258a2005-10-31 20:12:07 +01002564 if( mega_internal_command(adapter, &mc, NULL) ) {
David Howellsc7f079c2013-04-10 13:33:21 +01002565 seq_puts(m, "40LD read config failed.\n");
2566 goto free_pci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 }
2568
2569 }
2570 else {
2571 mc.cmd = NEW_READ_CONFIG_8LD;
2572
Christoph Hellwigcb0258a2005-10-31 20:12:07 +01002573 if( mega_internal_command(adapter, &mc, NULL) ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 mc.cmd = READ_CONFIG_8LD;
David Howellsc7f079c2013-04-10 13:33:21 +01002575 if( mega_internal_command(adapter, &mc, NULL) ) {
2576 seq_puts(m, "8LD read config failed.\n");
2577 goto free_pci;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 }
2579 }
2580 }
2581
2582 for( i = start; i < ( (end+1 < num_ldrv) ? end+1 : num_ldrv ); i++ ) {
2583
2584 if( adapter->flag & BOARD_40LD ) {
2585 lparam =
2586 &((disk_array_40ld *)disk_array)->ldrv[i].lparam;
2587 }
2588 else {
2589 lparam =
2590 &((disk_array_8ld *)disk_array)->ldrv[i].lparam;
2591 }
2592
2593 /*
2594 * Check for overflow. We print less than 240 characters for
2595 * information about each logical drive.
2596 */
David Howellsc7f079c2013-04-10 13:33:21 +01002597 seq_printf(m, "Logical drive:%2d:, ", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598
2599 switch( rdrv_state[i] & 0x0F ) {
2600 case RDRV_OFFLINE:
David Howellsc7f079c2013-04-10 13:33:21 +01002601 seq_puts(m, "state: offline");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 case RDRV_DEGRADED:
David Howellsc7f079c2013-04-10 13:33:21 +01002604 seq_puts(m, "state: degraded");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 case RDRV_OPTIMAL:
David Howellsc7f079c2013-04-10 13:33:21 +01002607 seq_puts(m, "state: optimal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 case RDRV_DELETED:
David Howellsc7f079c2013-04-10 13:33:21 +01002610 seq_puts(m, "state: deleted");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 default:
David Howellsc7f079c2013-04-10 13:33:21 +01002613 seq_puts(m, "state: unknown");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 break;
2615 }
2616
2617 /*
2618 * Check if check consistency or initialization is going on
2619 * for this logical drive.
2620 */
David Howellsc7f079c2013-04-10 13:33:21 +01002621 if( (rdrv_state[i] & 0xF0) == 0x20 )
2622 seq_puts(m, ", check-consistency in progress");
2623 else if( (rdrv_state[i] & 0xF0) == 0x10 )
2624 seq_puts(m, ", initialization in progress");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
David Howellsc7f079c2013-04-10 13:33:21 +01002626 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627
David Howellsc7f079c2013-04-10 13:33:21 +01002628 seq_printf(m, "Span depth:%3d, ", lparam->span_depth);
2629 seq_printf(m, "RAID level:%3d, ", lparam->level);
2630 seq_printf(m, "Stripe size:%3d, ",
2631 lparam->stripe_sz ? lparam->stripe_sz/2: 128);
2632 seq_printf(m, "Row size:%3d\n", lparam->row_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633
David Howellsc7f079c2013-04-10 13:33:21 +01002634 seq_puts(m, "Read Policy: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 switch(lparam->read_ahead) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 case NO_READ_AHEAD:
David Howellsc7f079c2013-04-10 13:33:21 +01002637 seq_puts(m, "No read ahead, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 case READ_AHEAD:
David Howellsc7f079c2013-04-10 13:33:21 +01002640 seq_puts(m, "Read ahead, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 case ADAP_READ_AHEAD:
David Howellsc7f079c2013-04-10 13:33:21 +01002643 seq_puts(m, "Adaptive, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 break;
2645
2646 }
2647
David Howellsc7f079c2013-04-10 13:33:21 +01002648 seq_puts(m, "Write Policy: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 switch(lparam->write_mode) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 case WRMODE_WRITE_THRU:
David Howellsc7f079c2013-04-10 13:33:21 +01002651 seq_puts(m, "Write thru, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 case WRMODE_WRITE_BACK:
David Howellsc7f079c2013-04-10 13:33:21 +01002654 seq_puts(m, "Write back, ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 break;
2656 }
2657
David Howellsc7f079c2013-04-10 13:33:21 +01002658 seq_puts(m, "Cache Policy: ");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 switch(lparam->direct_io) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 case CACHED_IO:
David Howellsc7f079c2013-04-10 13:33:21 +01002661 seq_puts(m, "Cached IO\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 case DIRECT_IO:
David Howellsc7f079c2013-04-10 13:33:21 +01002664 seq_puts(m, "Direct IO\n\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 break;
2666 }
2667 }
2668
David Howellsc7f079c2013-04-10 13:33:21 +01002669free_pci:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 pci_free_consistent(pdev, array_sz, disk_array,
2671 disk_array_dma_handle);
David Howellsc7f079c2013-04-10 13:33:21 +01002672free_inquiry:
2673 mega_free_inquiry(inquiry, dma_handle, pdev);
2674free_pdev:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 free_local_pdev(pdev);
David Howellsc7f079c2013-04-10 13:33:21 +01002676 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677}
David Howellsc7f079c2013-04-10 13:33:21 +01002678
2679/**
2680 * proc_show_rdrv_10()
2681 * @m - Synthetic file construction data
2682 * @v - File iterator
2683 *
2684 * Display real time information about the logical drives 0 through 9.
2685 */
2686static int
2687proc_show_rdrv_10(struct seq_file *m, void *v)
2688{
2689 return proc_show_rdrv(m, m->private, 0, 9);
2690}
2691
2692
2693/**
2694 * proc_show_rdrv_20()
2695 * @m - Synthetic file construction data
2696 * @v - File iterator
2697 *
2698 * Display real time information about the logical drives 0 through 9.
2699 */
2700static int
2701proc_show_rdrv_20(struct seq_file *m, void *v)
2702{
2703 return proc_show_rdrv(m, m->private, 10, 19);
2704}
2705
2706
2707/**
2708 * proc_show_rdrv_30()
2709 * @m - Synthetic file construction data
2710 * @v - File iterator
2711 *
2712 * Display real time information about the logical drives 0 through 9.
2713 */
2714static int
2715proc_show_rdrv_30(struct seq_file *m, void *v)
2716{
2717 return proc_show_rdrv(m, m->private, 20, 29);
2718}
2719
2720
2721/**
2722 * proc_show_rdrv_40()
2723 * @m - Synthetic file construction data
2724 * @v - File iterator
2725 *
2726 * Display real time information about the logical drives 0 through 9.
2727 */
2728static int
2729proc_show_rdrv_40(struct seq_file *m, void *v)
2730{
2731 return proc_show_rdrv(m, m->private, 30, 39);
2732}
2733
David Howellsc7f079c2013-04-10 13:33:21 +01002734/**
2735 * mega_create_proc_entry()
2736 * @index - index in soft state array
2737 * @parent - parent node for this /proc entry
2738 *
2739 * Creates /proc entries for our controllers.
2740 */
2741static void
2742mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2743{
Christoph Hellwigf7680be2018-04-13 20:57:56 +02002744 adapter_t *adapter = hba_soft_state[index];
2745 struct proc_dir_entry *dir;
2746 u8 string[16];
David Howellsc7f079c2013-04-10 13:33:21 +01002747
2748 sprintf(string, "hba%d", adapter->host->host_no);
Christoph Hellwigf7680be2018-04-13 20:57:56 +02002749 dir = proc_mkdir_data(string, 0, parent, adapter);
2750 if (!dir) {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05002751 dev_warn(&adapter->dev->dev, "proc_mkdir failed\n");
David Howellsc7f079c2013-04-10 13:33:21 +01002752 return;
2753 }
David Howellsc7f079c2013-04-10 13:33:21 +01002754
Christoph Hellwigf7680be2018-04-13 20:57:56 +02002755 proc_create_single_data("config", S_IRUSR, dir,
2756 proc_show_config, adapter);
2757 proc_create_single_data("stat", S_IRUSR, dir,
2758 proc_show_stat, adapter);
2759 proc_create_single_data("mailbox", S_IRUSR, dir,
2760 proc_show_mbox, adapter);
2761#if MEGA_HAVE_ENH_PROC
2762 proc_create_single_data("rebuild-rate", S_IRUSR, dir,
2763 proc_show_rebuild_rate, adapter);
2764 proc_create_single_data("battery-status", S_IRUSR, dir,
2765 proc_show_battery, adapter);
2766 proc_create_single_data("diskdrives-ch0", S_IRUSR, dir,
2767 proc_show_pdrv_ch0, adapter);
2768 proc_create_single_data("diskdrives-ch1", S_IRUSR, dir,
2769 proc_show_pdrv_ch1, adapter);
2770 proc_create_single_data("diskdrives-ch2", S_IRUSR, dir,
2771 proc_show_pdrv_ch2, adapter);
2772 proc_create_single_data("diskdrives-ch3", S_IRUSR, dir,
2773 proc_show_pdrv_ch3, adapter);
2774 proc_create_single_data("raiddrives-0-9", S_IRUSR, dir,
2775 proc_show_rdrv_10, adapter);
2776 proc_create_single_data("raiddrives-10-19", S_IRUSR, dir,
2777 proc_show_rdrv_20, adapter);
2778 proc_create_single_data("raiddrives-20-29", S_IRUSR, dir,
2779 proc_show_rdrv_30, adapter);
2780 proc_create_single_data("raiddrives-30-39", S_IRUSR, dir,
2781 proc_show_rdrv_40, adapter);
2782#endif
David Howellsc7f079c2013-04-10 13:33:21 +01002783}
2784
walter harms84a3c972007-04-26 00:35:09 -07002785#else
2786static inline void mega_create_proc_entry(int index, struct proc_dir_entry *parent)
2787{
2788}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789#endif
2790
2791
2792/**
2793 * megaraid_biosparam()
2794 *
2795 * Return the disk geometry for a particular disk
2796 */
2797static int
2798megaraid_biosparam(struct scsi_device *sdev, struct block_device *bdev,
2799 sector_t capacity, int geom[])
2800{
2801 adapter_t *adapter;
2802 unsigned char *bh;
2803 int heads;
2804 int sectors;
2805 int cylinders;
2806 int rval;
2807
2808 /* Get pointer to host config structure */
2809 adapter = (adapter_t *)sdev->host->hostdata;
2810
2811 if (IS_RAID_CH(adapter, sdev->channel)) {
2812 /* Default heads (64) & sectors (32) */
2813 heads = 64;
2814 sectors = 32;
2815 cylinders = (ulong)capacity / (heads * sectors);
2816
2817 /*
2818 * Handle extended translation size for logical drives
2819 * > 1Gb
2820 */
2821 if ((ulong)capacity >= 0x200000) {
2822 heads = 255;
2823 sectors = 63;
2824 cylinders = (ulong)capacity / (heads * sectors);
2825 }
2826
2827 /* return result */
2828 geom[0] = heads;
2829 geom[1] = sectors;
2830 geom[2] = cylinders;
2831 }
2832 else {
2833 bh = scsi_bios_ptable(bdev);
2834
2835 if( bh ) {
2836 rval = scsi_partsize(bh, capacity,
2837 &geom[2], &geom[0], &geom[1]);
2838 kfree(bh);
2839 if( rval != -1 )
2840 return rval;
2841 }
2842
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05002843 dev_info(&adapter->dev->dev,
2844 "invalid partition on this disk on channel %d\n",
2845 sdev->channel);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
2847 /* Default heads (64) & sectors (32) */
2848 heads = 64;
2849 sectors = 32;
2850 cylinders = (ulong)capacity / (heads * sectors);
2851
2852 /* Handle extended translation size for logical drives > 1Gb */
2853 if ((ulong)capacity >= 0x200000) {
2854 heads = 255;
2855 sectors = 63;
2856 cylinders = (ulong)capacity / (heads * sectors);
2857 }
2858
2859 /* return result */
2860 geom[0] = heads;
2861 geom[1] = sectors;
2862 geom[2] = cylinders;
2863 }
2864
2865 return 0;
2866}
2867
2868/**
2869 * mega_init_scb()
2870 * @adapter - pointer to our soft state
2871 *
2872 * Allocate memory for the various pointers in the scb structures:
2873 * scatter-gather list pointer, passthru and extended passthru structure
2874 * pointers.
2875 */
2876static int
2877mega_init_scb(adapter_t *adapter)
2878{
2879 scb_t *scb;
2880 int i;
2881
2882 for( i = 0; i < adapter->max_cmds; i++ ) {
2883
2884 scb = &adapter->scb_list[i];
2885
2886 scb->sgl64 = NULL;
2887 scb->sgl = NULL;
2888 scb->pthru = NULL;
2889 scb->epthru = NULL;
2890 }
2891
2892 for( i = 0; i < adapter->max_cmds; i++ ) {
2893
2894 scb = &adapter->scb_list[i];
2895
2896 scb->idx = i;
2897
2898 scb->sgl64 = pci_alloc_consistent(adapter->dev,
2899 sizeof(mega_sgl64) * adapter->sglen,
2900 &scb->sgl_dma_addr);
2901
2902 scb->sgl = (mega_sglist *)scb->sgl64;
2903
2904 if( !scb->sgl ) {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05002905 dev_warn(&adapter->dev->dev, "RAID: Can't allocate sglist\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 mega_free_sgl(adapter);
2907 return -1;
2908 }
2909
2910 scb->pthru = pci_alloc_consistent(adapter->dev,
2911 sizeof(mega_passthru),
2912 &scb->pthru_dma_addr);
2913
2914 if( !scb->pthru ) {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05002915 dev_warn(&adapter->dev->dev, "RAID: Can't allocate passthru\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 mega_free_sgl(adapter);
2917 return -1;
2918 }
2919
2920 scb->epthru = pci_alloc_consistent(adapter->dev,
2921 sizeof(mega_ext_passthru),
2922 &scb->epthru_dma_addr);
2923
2924 if( !scb->epthru ) {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05002925 dev_warn(&adapter->dev->dev,
2926 "Can't allocate extended passthru\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 mega_free_sgl(adapter);
2928 return -1;
2929 }
2930
2931
2932 scb->dma_type = MEGA_DMA_TYPE_NONE;
2933
2934 /*
2935 * Link to free list
2936 * lock not required since we are loading the driver, so no
2937 * commands possible right now.
2938 */
2939 scb->state = SCB_FREE;
2940 scb->cmd = NULL;
2941 list_add(&scb->list, &adapter->free_list);
2942 }
2943
2944 return 0;
2945}
2946
2947
2948/**
2949 * megadev_open()
2950 * @inode - unused
2951 * @filep - unused
2952 *
2953 * Routines for the character/ioctl interface to the driver. Find out if this
Jonathan Corbetd21c95c2008-05-16 13:40:30 -06002954 * is a valid open.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 */
2956static int
2957megadev_open (struct inode *inode, struct file *filep)
2958{
2959 /*
2960 * Only allow superuser to access private ioctl interface
2961 */
2962 if( !capable(CAP_SYS_ADMIN) ) return -EACCES;
2963
2964 return 0;
2965}
2966
2967
2968/**
2969 * megadev_ioctl()
2970 * @inode - Our device inode
2971 * @filep - unused
2972 * @cmd - ioctl command
2973 * @arg - user buffer
2974 *
2975 * ioctl entry point for our private ioctl interface. We move the data in from
2976 * the user space, prepare the command (if necessary, convert the old MIMD
2977 * ioctl to new ioctl command), and issue a synchronous command to the
2978 * controller.
2979 */
2980static int
Arnd Bergmannf4927c42010-04-27 00:24:01 +02002981megadev_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
2983 adapter_t *adapter;
2984 nitioctl_t uioc;
2985 int adapno;
2986 int rval;
2987 mega_passthru __user *upthru; /* user address for passthru */
2988 mega_passthru *pthru; /* copy user passthru here */
2989 dma_addr_t pthru_dma_hndl;
2990 void *data = NULL; /* data to be transferred */
2991 dma_addr_t data_dma_hndl; /* dma handle for data xfer area */
2992 megacmd_t mc;
2993 megastat_t __user *ustats;
2994 int num_ldrv;
2995 u32 uxferaddr = 0;
2996 struct pci_dev *pdev;
2997
2998 ustats = NULL; /* avoid compilation warnings */
2999 num_ldrv = 0;
3000
3001 /*
3002 * Make sure only USCSICMD are issued through this interface.
3003 * MIMD application would still fire different command.
3004 */
3005 if( (_IOC_TYPE(cmd) != MEGAIOC_MAGIC) && (cmd != USCSICMD) ) {
3006 return -EINVAL;
3007 }
3008
3009 /*
3010 * Check and convert a possible MIMD command to NIT command.
3011 * mega_m_to_n() copies the data from the user space, so we do not
3012 * have to do it here.
3013 * NOTE: We will need some user address to copyout the data, therefore
3014 * the inteface layer will also provide us with the required user
3015 * addresses.
3016 */
3017 memset(&uioc, 0, sizeof(nitioctl_t));
3018 if( (rval = mega_m_to_n( (void __user *)arg, &uioc)) != 0 )
3019 return rval;
3020
3021
3022 switch( uioc.opcode ) {
3023
3024 case GET_DRIVER_VER:
3025 if( put_user(driver_ver, (u32 __user *)uioc.uioc_uaddr) )
3026 return (-EFAULT);
3027
3028 break;
3029
3030 case GET_N_ADAP:
3031 if( put_user(hba_count, (u32 __user *)uioc.uioc_uaddr) )
3032 return (-EFAULT);
3033
3034 /*
3035 * Shucks. MIMD interface returns a positive value for number
3036 * of adapters. TODO: Change it to return 0 when there is no
3037 * applicatio using mimd interface.
3038 */
3039 return hba_count;
3040
3041 case GET_ADAP_INFO:
3042
3043 /*
3044 * Which adapter
3045 */
3046 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3047 return (-ENODEV);
3048
3049 if( copy_to_user(uioc.uioc_uaddr, mcontroller+adapno,
3050 sizeof(struct mcontroller)) )
3051 return (-EFAULT);
3052 break;
3053
3054#if MEGA_HAVE_STATS
3055
3056 case GET_STATS:
3057 /*
3058 * Which adapter
3059 */
3060 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3061 return (-ENODEV);
3062
3063 adapter = hba_soft_state[adapno];
3064
3065 ustats = uioc.uioc_uaddr;
3066
3067 if( copy_from_user(&num_ldrv, &ustats->num_ldrv, sizeof(int)) )
3068 return (-EFAULT);
3069
3070 /*
3071 * Check for the validity of the logical drive number
3072 */
3073 if( num_ldrv >= MAX_LOGICAL_DRIVES_40LD ) return -EINVAL;
3074
3075 if( copy_to_user(ustats->nreads, adapter->nreads,
3076 num_ldrv*sizeof(u32)) )
3077 return -EFAULT;
3078
3079 if( copy_to_user(ustats->nreadblocks, adapter->nreadblocks,
3080 num_ldrv*sizeof(u32)) )
3081 return -EFAULT;
3082
3083 if( copy_to_user(ustats->nwrites, adapter->nwrites,
3084 num_ldrv*sizeof(u32)) )
3085 return -EFAULT;
3086
3087 if( copy_to_user(ustats->nwriteblocks, adapter->nwriteblocks,
3088 num_ldrv*sizeof(u32)) )
3089 return -EFAULT;
3090
3091 if( copy_to_user(ustats->rd_errors, adapter->rd_errors,
3092 num_ldrv*sizeof(u32)) )
3093 return -EFAULT;
3094
3095 if( copy_to_user(ustats->wr_errors, adapter->wr_errors,
3096 num_ldrv*sizeof(u32)) )
3097 return -EFAULT;
3098
3099 return 0;
3100
3101#endif
3102 case MBOX_CMD:
3103
3104 /*
3105 * Which adapter
3106 */
3107 if( (adapno = GETADAP(uioc.adapno)) >= hba_count )
3108 return (-ENODEV);
3109
3110 adapter = hba_soft_state[adapno];
3111
3112 /*
3113 * Deletion of logical drive is a special case. The adapter
3114 * should be quiescent before this command is issued.
3115 */
3116 if( uioc.uioc_rmbox[0] == FC_DEL_LOGDRV &&
3117 uioc.uioc_rmbox[2] == OP_DEL_LOGDRV ) {
3118
3119 /*
3120 * Do we support this feature
3121 */
3122 if( !adapter->support_random_del ) {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05003123 dev_warn(&adapter->dev->dev, "logdrv "
3124 "delete on non-supporting F/W\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
3126 return (-EINVAL);
3127 }
3128
3129 rval = mega_del_logdrv( adapter, uioc.uioc_rmbox[3] );
3130
3131 if( rval == 0 ) {
3132 memset(&mc, 0, sizeof(megacmd_t));
3133
3134 mc.status = rval;
3135
3136 rval = mega_n_to_m((void __user *)arg, &mc);
3137 }
3138
3139 return rval;
3140 }
3141 /*
3142 * This interface only support the regular passthru commands.
3143 * Reject extended passthru and 64-bit passthru
3144 */
3145 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU64 ||
3146 uioc.uioc_rmbox[0] == MEGA_MBOXCMD_EXTPTHRU ) {
3147
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05003148 dev_warn(&adapter->dev->dev, "rejected passthru\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
3150 return (-EINVAL);
3151 }
3152
3153 /*
3154 * For all internal commands, the buffer must be allocated in
3155 * <4GB address range
3156 */
3157 if( make_local_pdev(adapter, &pdev) != 0 )
3158 return -EIO;
3159
3160 /* Is it a passthru command or a DCMD */
3161 if( uioc.uioc_rmbox[0] == MEGA_MBOXCMD_PASSTHRU ) {
3162 /* Passthru commands */
3163
3164 pthru = pci_alloc_consistent(pdev,
3165 sizeof(mega_passthru),
3166 &pthru_dma_hndl);
3167
3168 if( pthru == NULL ) {
3169 free_local_pdev(pdev);
3170 return (-ENOMEM);
3171 }
3172
3173 /*
3174 * The user passthru structure
3175 */
Martin Blighde5952e2007-05-23 16:11:46 -07003176 upthru = (mega_passthru __user *)(unsigned long)MBOX(uioc)->xferaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
3178 /*
3179 * Copy in the user passthru here.
3180 */
3181 if( copy_from_user(pthru, upthru,
3182 sizeof(mega_passthru)) ) {
3183
3184 pci_free_consistent(pdev,
3185 sizeof(mega_passthru), pthru,
3186 pthru_dma_hndl);
3187
3188 free_local_pdev(pdev);
3189
3190 return (-EFAULT);
3191 }
3192
3193 /*
3194 * Is there a data transfer
3195 */
3196 if( pthru->dataxferlen ) {
3197 data = pci_alloc_consistent(pdev,
3198 pthru->dataxferlen,
3199 &data_dma_hndl);
3200
3201 if( data == NULL ) {
3202 pci_free_consistent(pdev,
3203 sizeof(mega_passthru),
3204 pthru,
3205 pthru_dma_hndl);
3206
3207 free_local_pdev(pdev);
3208
3209 return (-ENOMEM);
3210 }
3211
3212 /*
3213 * Save the user address and point the kernel
3214 * address at just allocated memory
3215 */
3216 uxferaddr = pthru->dataxferaddr;
3217 pthru->dataxferaddr = data_dma_hndl;
3218 }
3219
3220
3221 /*
3222 * Is data coming down-stream
3223 */
3224 if( pthru->dataxferlen && (uioc.flags & UIOC_WR) ) {
3225 /*
3226 * Get the user data
3227 */
Martin Blighde5952e2007-05-23 16:11:46 -07003228 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 pthru->dataxferlen) ) {
3230 rval = (-EFAULT);
3231 goto freemem_and_return;
3232 }
3233 }
3234
3235 memset(&mc, 0, sizeof(megacmd_t));
3236
3237 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
3238 mc.xferaddr = (u32)pthru_dma_hndl;
3239
3240 /*
3241 * Issue the command
3242 */
Christoph Hellwigcb0258a2005-10-31 20:12:07 +01003243 mega_internal_command(adapter, &mc, pthru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
3245 rval = mega_n_to_m((void __user *)arg, &mc);
3246
3247 if( rval ) goto freemem_and_return;
3248
3249
3250 /*
3251 * Is data going up-stream
3252 */
3253 if( pthru->dataxferlen && (uioc.flags & UIOC_RD) ) {
Martin Blighde5952e2007-05-23 16:11:46 -07003254 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 pthru->dataxferlen) ) {
3256 rval = (-EFAULT);
3257 }
3258 }
3259
3260 /*
3261 * Send the request sense data also, irrespective of
3262 * whether the user has asked for it or not.
3263 */
Jesper Juhl2d2f8d52006-09-15 14:43:11 +02003264 if (copy_to_user(upthru->reqsensearea,
3265 pthru->reqsensearea, 14))
3266 rval = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267
3268freemem_and_return:
3269 if( pthru->dataxferlen ) {
3270 pci_free_consistent(pdev,
3271 pthru->dataxferlen, data,
3272 data_dma_hndl);
3273 }
3274
3275 pci_free_consistent(pdev, sizeof(mega_passthru),
3276 pthru, pthru_dma_hndl);
3277
3278 free_local_pdev(pdev);
3279
3280 return rval;
3281 }
3282 else {
3283 /* DCMD commands */
3284
3285 /*
3286 * Is there a data transfer
3287 */
3288 if( uioc.xferlen ) {
3289 data = pci_alloc_consistent(pdev,
3290 uioc.xferlen, &data_dma_hndl);
3291
3292 if( data == NULL ) {
3293 free_local_pdev(pdev);
3294 return (-ENOMEM);
3295 }
3296
3297 uxferaddr = MBOX(uioc)->xferaddr;
3298 }
3299
3300 /*
3301 * Is data coming down-stream
3302 */
3303 if( uioc.xferlen && (uioc.flags & UIOC_WR) ) {
3304 /*
3305 * Get the user data
3306 */
Martin Blighde5952e2007-05-23 16:11:46 -07003307 if( copy_from_user(data, (char __user *)(unsigned long) uxferaddr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 uioc.xferlen) ) {
3309
3310 pci_free_consistent(pdev,
3311 uioc.xferlen,
3312 data, data_dma_hndl);
3313
3314 free_local_pdev(pdev);
3315
3316 return (-EFAULT);
3317 }
3318 }
3319
3320 memcpy(&mc, MBOX(uioc), sizeof(megacmd_t));
3321
3322 mc.xferaddr = (u32)data_dma_hndl;
3323
3324 /*
3325 * Issue the command
3326 */
Christoph Hellwigcb0258a2005-10-31 20:12:07 +01003327 mega_internal_command(adapter, &mc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328
3329 rval = mega_n_to_m((void __user *)arg, &mc);
3330
3331 if( rval ) {
3332 if( uioc.xferlen ) {
3333 pci_free_consistent(pdev,
3334 uioc.xferlen, data,
3335 data_dma_hndl);
3336 }
3337
3338 free_local_pdev(pdev);
3339
3340 return rval;
3341 }
3342
3343 /*
3344 * Is data going up-stream
3345 */
3346 if( uioc.xferlen && (uioc.flags & UIOC_RD) ) {
Martin Blighde5952e2007-05-23 16:11:46 -07003347 if( copy_to_user((char __user *)(unsigned long) uxferaddr, data,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 uioc.xferlen) ) {
3349
3350 rval = (-EFAULT);
3351 }
3352 }
3353
3354 if( uioc.xferlen ) {
3355 pci_free_consistent(pdev,
3356 uioc.xferlen, data,
3357 data_dma_hndl);
3358 }
3359
3360 free_local_pdev(pdev);
3361
3362 return rval;
3363 }
3364
3365 default:
3366 return (-EINVAL);
3367 }
3368
3369 return 0;
3370}
3371
Arnd Bergmannf4927c42010-04-27 00:24:01 +02003372static long
3373megadev_unlocked_ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
3374{
3375 int ret;
3376
Arnd Bergmannc45d15d2010-06-02 14:28:52 +02003377 mutex_lock(&megadev_mutex);
Arnd Bergmannf4927c42010-04-27 00:24:01 +02003378 ret = megadev_ioctl(filep, cmd, arg);
Arnd Bergmannc45d15d2010-06-02 14:28:52 +02003379 mutex_unlock(&megadev_mutex);
Arnd Bergmannf4927c42010-04-27 00:24:01 +02003380
3381 return ret;
3382}
3383
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384/**
3385 * mega_m_to_n()
3386 * @arg - user address
3387 * @uioc - new ioctl structure
3388 *
3389 * A thin layer to convert older mimd interface ioctl structure to NIT ioctl
3390 * structure
3391 *
3392 * Converts the older mimd ioctl structure to newer NIT structure
3393 */
3394static int
3395mega_m_to_n(void __user *arg, nitioctl_t *uioc)
3396{
3397 struct uioctl_t uioc_mimd;
3398 char signature[8] = {0};
3399 u8 opcode;
3400 u8 subopcode;
3401
3402
3403 /*
3404 * check is the application conforms to NIT. We do not have to do much
3405 * in that case.
3406 * We exploit the fact that the signature is stored in the very
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003407 * beginning of the structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 */
3409
3410 if( copy_from_user(signature, arg, 7) )
3411 return (-EFAULT);
3412
3413 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3414
3415 /*
3416 * NOTE NOTE: The nit ioctl is still under flux because of
3417 * change of mailbox definition, in HPE. No applications yet
3418 * use this interface and let's not have applications use this
3419 * interface till the new specifitions are in place.
3420 */
3421 return -EINVAL;
3422#if 0
3423 if( copy_from_user(uioc, arg, sizeof(nitioctl_t)) )
3424 return (-EFAULT);
3425 return 0;
3426#endif
3427 }
3428
3429 /*
3430 * Else assume we have mimd uioctl_t as arg. Convert to nitioctl_t
3431 *
3432 * Get the user ioctl structure
3433 */
3434 if( copy_from_user(&uioc_mimd, arg, sizeof(struct uioctl_t)) )
3435 return (-EFAULT);
3436
3437
3438 /*
3439 * Get the opcode and subopcode for the commands
3440 */
3441 opcode = uioc_mimd.ui.fcs.opcode;
3442 subopcode = uioc_mimd.ui.fcs.subopcode;
3443
3444 switch (opcode) {
3445 case 0x82:
3446
3447 switch (subopcode) {
3448
3449 case MEGAIOC_QDRVRVER: /* Query driver version */
3450 uioc->opcode = GET_DRIVER_VER;
3451 uioc->uioc_uaddr = uioc_mimd.data;
3452 break;
3453
3454 case MEGAIOC_QNADAP: /* Get # of adapters */
3455 uioc->opcode = GET_N_ADAP;
3456 uioc->uioc_uaddr = uioc_mimd.data;
3457 break;
3458
3459 case MEGAIOC_QADAPINFO: /* Get adapter information */
3460 uioc->opcode = GET_ADAP_INFO;
3461 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3462 uioc->uioc_uaddr = uioc_mimd.data;
3463 break;
3464
3465 default:
3466 return(-EINVAL);
3467 }
3468
3469 break;
3470
3471
3472 case 0x81:
3473
3474 uioc->opcode = MBOX_CMD;
3475 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3476
3477 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3478
3479 uioc->xferlen = uioc_mimd.ui.fcs.length;
3480
3481 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3482 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3483
3484 break;
3485
3486 case 0x80:
3487
3488 uioc->opcode = MBOX_CMD;
3489 uioc->adapno = uioc_mimd.ui.fcs.adapno;
3490
3491 memcpy(uioc->uioc_rmbox, uioc_mimd.mbox, 18);
3492
3493 /*
3494 * Choose the xferlen bigger of input and output data
3495 */
3496 uioc->xferlen = uioc_mimd.outlen > uioc_mimd.inlen ?
3497 uioc_mimd.outlen : uioc_mimd.inlen;
3498
3499 if( uioc_mimd.outlen ) uioc->flags = UIOC_RD;
3500 if( uioc_mimd.inlen ) uioc->flags |= UIOC_WR;
3501
3502 break;
3503
3504 default:
3505 return (-EINVAL);
3506
3507 }
3508
3509 return 0;
3510}
3511
3512/*
3513 * mega_n_to_m()
3514 * @arg - user address
3515 * @mc - mailbox command
3516 *
3517 * Updates the status information to the application, depending on application
3518 * conforms to older mimd ioctl interface or newer NIT ioctl interface
3519 */
3520static int
3521mega_n_to_m(void __user *arg, megacmd_t *mc)
3522{
3523 nitioctl_t __user *uiocp;
3524 megacmd_t __user *umc;
3525 mega_passthru __user *upthru;
3526 struct uioctl_t __user *uioc_mimd;
3527 char signature[8] = {0};
3528
3529 /*
3530 * check is the application conforms to NIT.
3531 */
3532 if( copy_from_user(signature, arg, 7) )
3533 return -EFAULT;
3534
3535 if( memcmp(signature, "MEGANIT", 7) == 0 ) {
3536
3537 uiocp = arg;
3538
3539 if( put_user(mc->status, (u8 __user *)&MBOX_P(uiocp)->status) )
3540 return (-EFAULT);
3541
3542 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3543
3544 umc = MBOX_P(uiocp);
3545
3546 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3547 return -EFAULT;
3548
3549 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus))
3550 return (-EFAULT);
3551 }
3552 }
3553 else {
3554 uioc_mimd = arg;
3555
3556 if( put_user(mc->status, (u8 __user *)&uioc_mimd->mbox[17]) )
3557 return (-EFAULT);
3558
3559 if( mc->cmd == MEGA_MBOXCMD_PASSTHRU ) {
3560
3561 umc = (megacmd_t __user *)uioc_mimd->mbox;
3562
3563 if (get_user(upthru, (mega_passthru __user * __user *)&umc->xferaddr))
3564 return (-EFAULT);
3565
3566 if( put_user(mc->status, (u8 __user *)&upthru->scsistatus) )
3567 return (-EFAULT);
3568 }
3569 }
3570
3571 return 0;
3572}
3573
3574
3575/*
3576 * MEGARAID 'FW' commands.
3577 */
3578
3579/**
3580 * mega_is_bios_enabled()
3581 * @adapter - pointer to our soft state
3582 *
3583 * issue command to find out if the BIOS is enabled for this controller
3584 */
3585static int
3586mega_is_bios_enabled(adapter_t *adapter)
3587{
3588 unsigned char raw_mbox[sizeof(struct mbox_out)];
3589 mbox_t *mbox;
3590 int ret;
3591
3592 mbox = (mbox_t *)raw_mbox;
3593
3594 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3595
3596 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3597
3598 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3599
3600 raw_mbox[0] = IS_BIOS_ENABLED;
3601 raw_mbox[2] = GET_BIOS;
3602
3603
3604 ret = issue_scb_block(adapter, raw_mbox);
3605
3606 return *(char *)adapter->mega_buffer;
3607}
3608
3609
3610/**
3611 * mega_enum_raid_scsi()
3612 * @adapter - pointer to our soft state
3613 *
3614 * Find out what channels are RAID/SCSI. This information is used to
3615 * differentiate the virtual channels and physical channels and to support
3616 * ROMB feature and non-disk devices.
3617 */
3618static void
3619mega_enum_raid_scsi(adapter_t *adapter)
3620{
3621 unsigned char raw_mbox[sizeof(struct mbox_out)];
3622 mbox_t *mbox;
3623 int i;
3624
3625 mbox = (mbox_t *)raw_mbox;
3626
3627 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3628
3629 /*
3630 * issue command to find out what channels are raid/scsi
3631 */
3632 raw_mbox[0] = CHNL_CLASS;
3633 raw_mbox[2] = GET_CHNL_CLASS;
3634
3635 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3636
3637 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3638
3639 /*
3640 * Non-ROMB firmware fail this command, so all channels
3641 * must be shown RAID
3642 */
3643 adapter->mega_ch_class = 0xFF;
3644
3645 if(!issue_scb_block(adapter, raw_mbox)) {
3646 adapter->mega_ch_class = *((char *)adapter->mega_buffer);
3647
3648 }
3649
3650 for( i = 0; i < adapter->product_info.nchannels; i++ ) {
3651 if( (adapter->mega_ch_class >> i) & 0x01 ) {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05003652 dev_info(&adapter->dev->dev, "channel[%d] is raid\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 i);
3654 }
3655 else {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05003656 dev_info(&adapter->dev->dev, "channel[%d] is scsi\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 i);
3658 }
3659 }
3660
3661 return;
3662}
3663
3664
3665/**
3666 * mega_get_boot_drv()
3667 * @adapter - pointer to our soft state
3668 *
3669 * Find out which device is the boot device. Note, any logical drive or any
3670 * phyical device (e.g., a CDROM) can be designated as a boot device.
3671 */
3672static void
3673mega_get_boot_drv(adapter_t *adapter)
3674{
3675 struct private_bios_data *prv_bios_data;
3676 unsigned char raw_mbox[sizeof(struct mbox_out)];
3677 mbox_t *mbox;
3678 u16 cksum = 0;
3679 u8 *cksum_p;
3680 u8 boot_pdrv;
3681 int i;
3682
3683 mbox = (mbox_t *)raw_mbox;
3684
3685 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3686
3687 raw_mbox[0] = BIOS_PVT_DATA;
3688 raw_mbox[2] = GET_BIOS_PVT_DATA;
3689
3690 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3691
3692 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3693
3694 adapter->boot_ldrv_enabled = 0;
3695 adapter->boot_ldrv = 0;
3696
3697 adapter->boot_pdrv_enabled = 0;
3698 adapter->boot_pdrv_ch = 0;
3699 adapter->boot_pdrv_tgt = 0;
3700
3701 if(issue_scb_block(adapter, raw_mbox) == 0) {
3702 prv_bios_data =
3703 (struct private_bios_data *)adapter->mega_buffer;
3704
3705 cksum = 0;
3706 cksum_p = (char *)prv_bios_data;
3707 for (i = 0; i < 14; i++ ) {
3708 cksum += (u16)(*cksum_p++);
3709 }
3710
3711 if (prv_bios_data->cksum == (u16)(0-cksum) ) {
3712
3713 /*
3714 * If MSB is set, a physical drive is set as boot
3715 * device
3716 */
3717 if( prv_bios_data->boot_drv & 0x80 ) {
3718 adapter->boot_pdrv_enabled = 1;
3719 boot_pdrv = prv_bios_data->boot_drv & 0x7F;
3720 adapter->boot_pdrv_ch = boot_pdrv / 16;
3721 adapter->boot_pdrv_tgt = boot_pdrv % 16;
3722 }
3723 else {
3724 adapter->boot_ldrv_enabled = 1;
3725 adapter->boot_ldrv = prv_bios_data->boot_drv;
3726 }
3727 }
3728 }
3729
3730}
3731
3732/**
3733 * mega_support_random_del()
3734 * @adapter - pointer to our soft state
3735 *
3736 * Find out if this controller supports random deletion and addition of
3737 * logical drives
3738 */
3739static int
3740mega_support_random_del(adapter_t *adapter)
3741{
3742 unsigned char raw_mbox[sizeof(struct mbox_out)];
3743 mbox_t *mbox;
3744 int rval;
3745
3746 mbox = (mbox_t *)raw_mbox;
3747
3748 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3749
3750 /*
3751 * issue command
3752 */
3753 raw_mbox[0] = FC_DEL_LOGDRV;
3754 raw_mbox[2] = OP_SUP_DEL_LOGDRV;
3755
3756 rval = issue_scb_block(adapter, raw_mbox);
3757
3758 return !rval;
3759}
3760
3761
3762/**
3763 * mega_support_ext_cdb()
3764 * @adapter - pointer to our soft state
3765 *
3766 * Find out if this firmware support cdblen > 10
3767 */
3768static int
3769mega_support_ext_cdb(adapter_t *adapter)
3770{
3771 unsigned char raw_mbox[sizeof(struct mbox_out)];
3772 mbox_t *mbox;
3773 int rval;
3774
3775 mbox = (mbox_t *)raw_mbox;
3776
3777 memset(&mbox->m_out, 0, sizeof(raw_mbox));
3778 /*
3779 * issue command to find out if controller supports extended CDBs.
3780 */
3781 raw_mbox[0] = 0xA4;
3782 raw_mbox[2] = 0x16;
3783
3784 rval = issue_scb_block(adapter, raw_mbox);
3785
3786 return !rval;
3787}
3788
3789
3790/**
3791 * mega_del_logdrv()
3792 * @adapter - pointer to our soft state
3793 * @logdrv - logical drive to be deleted
3794 *
3795 * Delete the specified logical drive. It is the responsibility of the user
3796 * app to let the OS know about this operation.
3797 */
3798static int
3799mega_del_logdrv(adapter_t *adapter, int logdrv)
3800{
3801 unsigned long flags;
3802 scb_t *scb;
3803 int rval;
3804
3805 /*
3806 * Stop sending commands to the controller, queue them internally.
3807 * When deletion is complete, ISR will flush the queue.
3808 */
3809 atomic_set(&adapter->quiescent, 1);
3810
3811 /*
3812 * Wait till all the issued commands are complete and there are no
3813 * commands in the pending queue
3814 */
3815 while (atomic_read(&adapter->pend_cmds) > 0 ||
3816 !list_empty(&adapter->pending_list))
3817 msleep(1000); /* sleep for 1s */
3818
3819 rval = mega_do_del_logdrv(adapter, logdrv);
3820
3821 spin_lock_irqsave(&adapter->lock, flags);
3822
3823 /*
3824 * If delete operation was successful, add 0x80 to the logical drive
3825 * ids for commands in the pending queue.
3826 */
3827 if (adapter->read_ldidmap) {
3828 struct list_head *pos;
3829 list_for_each(pos, &adapter->pending_list) {
3830 scb = list_entry(pos, scb_t, list);
3831 if (scb->pthru->logdrv < 0x80 )
3832 scb->pthru->logdrv += 0x80;
3833 }
3834 }
3835
3836 atomic_set(&adapter->quiescent, 0);
3837
3838 mega_runpendq(adapter);
3839
3840 spin_unlock_irqrestore(&adapter->lock, flags);
3841
3842 return rval;
3843}
3844
3845
3846static int
3847mega_do_del_logdrv(adapter_t *adapter, int logdrv)
3848{
3849 megacmd_t mc;
3850 int rval;
3851
3852 memset( &mc, 0, sizeof(megacmd_t));
3853
3854 mc.cmd = FC_DEL_LOGDRV;
3855 mc.opcode = OP_DEL_LOGDRV;
3856 mc.subopcode = logdrv;
3857
Christoph Hellwigcb0258a2005-10-31 20:12:07 +01003858 rval = mega_internal_command(adapter, &mc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
3860 /* log this event */
3861 if(rval) {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05003862 dev_warn(&adapter->dev->dev, "Delete LD-%d failed", logdrv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 return rval;
3864 }
3865
3866 /*
3867 * After deleting first logical drive, the logical drives must be
3868 * addressed by adding 0x80 to the logical drive id.
3869 */
3870 adapter->read_ldidmap = 1;
3871
3872 return rval;
3873}
3874
3875
3876/**
3877 * mega_get_max_sgl()
3878 * @adapter - pointer to our soft state
3879 *
3880 * Find out the maximum number of scatter-gather elements supported by this
3881 * version of the firmware
3882 */
3883static void
3884mega_get_max_sgl(adapter_t *adapter)
3885{
3886 unsigned char raw_mbox[sizeof(struct mbox_out)];
3887 mbox_t *mbox;
3888
3889 mbox = (mbox_t *)raw_mbox;
3890
3891 memset(mbox, 0, sizeof(raw_mbox));
3892
3893 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3894
3895 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3896
3897 raw_mbox[0] = MAIN_MISC_OPCODE;
3898 raw_mbox[2] = GET_MAX_SG_SUPPORT;
3899
3900
3901 if( issue_scb_block(adapter, raw_mbox) ) {
3902 /*
3903 * f/w does not support this command. Choose the default value
3904 */
3905 adapter->sglen = MIN_SGLIST;
3906 }
3907 else {
3908 adapter->sglen = *((char *)adapter->mega_buffer);
3909
3910 /*
3911 * Make sure this is not more than the resources we are
3912 * planning to allocate
3913 */
3914 if ( adapter->sglen > MAX_SGLIST )
3915 adapter->sglen = MAX_SGLIST;
3916 }
3917
3918 return;
3919}
3920
3921
3922/**
3923 * mega_support_cluster()
3924 * @adapter - pointer to our soft state
3925 *
3926 * Find out if this firmware support cluster calls.
3927 */
3928static int
3929mega_support_cluster(adapter_t *adapter)
3930{
3931 unsigned char raw_mbox[sizeof(struct mbox_out)];
3932 mbox_t *mbox;
3933
3934 mbox = (mbox_t *)raw_mbox;
3935
3936 memset(mbox, 0, sizeof(raw_mbox));
3937
3938 memset((void *)adapter->mega_buffer, 0, MEGA_BUFFER_SIZE);
3939
3940 mbox->m_out.xferaddr = (u32)adapter->buf_dma_handle;
3941
3942 /*
3943 * Try to get the initiator id. This command will succeed iff the
3944 * clustering is available on this HBA.
3945 */
3946 raw_mbox[0] = MEGA_GET_TARGET_ID;
3947
3948 if( issue_scb_block(adapter, raw_mbox) == 0 ) {
3949
3950 /*
3951 * Cluster support available. Get the initiator target id.
3952 * Tell our id to mid-layer too.
3953 */
3954 adapter->this_id = *(u32 *)adapter->mega_buffer;
3955 adapter->host->this_id = adapter->this_id;
3956
3957 return 1;
3958 }
3959
3960 return 0;
3961}
3962
walter harms84a3c972007-04-26 00:35:09 -07003963#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964/**
3965 * mega_adapinq()
3966 * @adapter - pointer to our soft state
3967 * @dma_handle - DMA address of the buffer
3968 *
Justin P. Mattock8e572ba2011-02-02 11:31:21 +01003969 * Issue internal commands while interrupts are available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 * We only issue direct mailbox commands from within the driver. ioctl()
3971 * interface using these routines can issue passthru commands.
3972 */
3973static int
3974mega_adapinq(adapter_t *adapter, dma_addr_t dma_handle)
3975{
3976 megacmd_t mc;
3977
3978 memset(&mc, 0, sizeof(megacmd_t));
3979
3980 if( adapter->flag & BOARD_40LD ) {
3981 mc.cmd = FC_NEW_CONFIG;
3982 mc.opcode = NC_SUBOP_ENQUIRY3;
3983 mc.subopcode = ENQ3_GET_SOLICITED_FULL;
3984 }
3985 else {
3986 mc.cmd = MEGA_MBOXCMD_ADPEXTINQ;
3987 }
3988
3989 mc.xferaddr = (u32)dma_handle;
3990
Christoph Hellwigcb0258a2005-10-31 20:12:07 +01003991 if ( mega_internal_command(adapter, &mc, NULL) != 0 ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 return -1;
3993 }
3994
3995 return 0;
3996}
3997
3998
3999/** mega_internal_dev_inquiry()
4000 * @adapter - pointer to our soft state
4001 * @ch - channel for this device
4002 * @tgt - ID of this device
4003 * @buf_dma_handle - DMA address of the buffer
4004 *
4005 * Issue the scsi inquiry for the specified device.
4006 */
4007static int
4008mega_internal_dev_inquiry(adapter_t *adapter, u8 ch, u8 tgt,
4009 dma_addr_t buf_dma_handle)
4010{
4011 mega_passthru *pthru;
4012 dma_addr_t pthru_dma_handle;
4013 megacmd_t mc;
4014 int rval;
4015 struct pci_dev *pdev;
4016
4017
4018 /*
4019 * For all internal commands, the buffer must be allocated in <4GB
4020 * address range
4021 */
4022 if( make_local_pdev(adapter, &pdev) != 0 ) return -1;
4023
4024 pthru = pci_alloc_consistent(pdev, sizeof(mega_passthru),
4025 &pthru_dma_handle);
4026
4027 if( pthru == NULL ) {
4028 free_local_pdev(pdev);
4029 return -1;
4030 }
4031
4032 pthru->timeout = 2;
4033 pthru->ars = 1;
4034 pthru->reqsenselen = 14;
4035 pthru->islogical = 0;
4036
4037 pthru->channel = (adapter->flag & BOARD_40LD) ? 0 : ch;
4038
4039 pthru->target = (adapter->flag & BOARD_40LD) ? (ch << 4)|tgt : tgt;
4040
4041 pthru->cdblen = 6;
4042
4043 pthru->cdb[0] = INQUIRY;
4044 pthru->cdb[1] = 0;
4045 pthru->cdb[2] = 0;
4046 pthru->cdb[3] = 0;
4047 pthru->cdb[4] = 255;
4048 pthru->cdb[5] = 0;
4049
4050
4051 pthru->dataxferaddr = (u32)buf_dma_handle;
4052 pthru->dataxferlen = 256;
4053
4054 memset(&mc, 0, sizeof(megacmd_t));
4055
4056 mc.cmd = MEGA_MBOXCMD_PASSTHRU;
4057 mc.xferaddr = (u32)pthru_dma_handle;
4058
Christoph Hellwigcb0258a2005-10-31 20:12:07 +01004059 rval = mega_internal_command(adapter, &mc, pthru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
4061 pci_free_consistent(pdev, sizeof(mega_passthru), pthru,
4062 pthru_dma_handle);
4063
4064 free_local_pdev(pdev);
4065
4066 return rval;
4067}
walter harms84a3c972007-04-26 00:35:09 -07004068#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069
4070/**
4071 * mega_internal_command()
4072 * @adapter - pointer to our soft state
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 * @mc - the mailbox command
4074 * @pthru - Passthru structure for DCDB commands
4075 *
4076 * Issue the internal commands in interrupt mode.
4077 * The last argument is the address of the passthru structure if the command
4078 * to be fired is a passthru command
4079 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 * Note: parameter 'pthru' is null for non-passthru commands.
4081 */
4082static int
Christoph Hellwigcb0258a2005-10-31 20:12:07 +01004083mega_internal_command(adapter_t *adapter, megacmd_t *mc, mega_passthru *pthru)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084{
Christoph Hellwig0f2bb842014-02-20 14:20:59 -08004085 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 scb_t *scb;
4087 int rval;
4088
4089 /*
4090 * The internal commands share one command id and hence are
4091 * serialized. This is so because we want to reserve maximum number of
4092 * available command ids for the I/O commands.
4093 */
Arjan van de Ven0b950672006-01-11 13:16:10 +01004094 mutex_lock(&adapter->int_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
4096 scb = &adapter->int_scb;
4097 memset(scb, 0, sizeof(scb_t));
4098
Christoph Hellwig0f2bb842014-02-20 14:20:59 -08004099 scb->idx = CMDID_INT_CMDS;
4100 scb->state |= SCB_ACTIVE | SCB_PENDQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
4102 memcpy(scb->raw_mbox, mc, sizeof(megacmd_t));
4103
4104 /*
4105 * Is it a passthru command
4106 */
Christoph Hellwig0f2bb842014-02-20 14:20:59 -08004107 if (mc->cmd == MEGA_MBOXCMD_PASSTHRU)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 scb->pthru = pthru;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
Christoph Hellwig0f2bb842014-02-20 14:20:59 -08004110 spin_lock_irqsave(&adapter->lock, flags);
4111 list_add_tail(&scb->list, &adapter->pending_list);
4112 /*
4113 * Check if the HBA is in quiescent state, e.g., during a
4114 * delete logical drive opertion. If it is, don't run
4115 * the pending_list.
4116 */
4117 if (atomic_read(&adapter->quiescent) == 0)
4118 mega_runpendq(adapter);
4119 spin_unlock_irqrestore(&adapter->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
Christoph Hellwig8d115f82005-06-19 13:42:05 +02004121 wait_for_completion(&adapter->int_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
Christoph Hellwig0f2bb842014-02-20 14:20:59 -08004123 mc->status = rval = adapter->int_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124
4125 /*
4126 * Print a debug message for all failed commands. Applications can use
4127 * this information.
4128 */
Christoph Hellwig0f2bb842014-02-20 14:20:59 -08004129 if (rval && trace_level) {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05004130 dev_info(&adapter->dev->dev, "cmd [%x, %x, %x] status:[%x]\n",
Christoph Hellwig0f2bb842014-02-20 14:20:59 -08004131 mc->cmd, mc->opcode, mc->subopcode, rval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 }
4133
Arjan van de Ven0b950672006-01-11 13:16:10 +01004134 mutex_unlock(&adapter->int_mtx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 return rval;
4136}
4137
Linus Torvalds1da177e2005-04-16 15:20:36 -07004138static struct scsi_host_template megaraid_template = {
4139 .module = THIS_MODULE,
4140 .name = "MegaRAID",
Ju, Seokmann3492b322005-11-17 13:13:31 -05004141 .proc_name = "megaraid_legacy",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 .info = megaraid_info,
4143 .queuecommand = megaraid_queue,
4144 .bios_param = megaraid_biosparam,
4145 .max_sectors = MAX_SECTORS_PER_IO,
4146 .can_queue = MAX_COMMANDS,
4147 .this_id = DEFAULT_INITIATOR_ID,
4148 .sg_tablesize = MAX_SGLIST,
4149 .cmd_per_lun = DEF_CMD_PER_LUN,
4150 .use_clustering = ENABLE_CLUSTERING,
4151 .eh_abort_handler = megaraid_abort,
4152 .eh_device_reset_handler = megaraid_reset,
4153 .eh_bus_reset_handler = megaraid_reset,
4154 .eh_host_reset_handler = megaraid_reset,
Martin K. Petersen54b2b502013-10-23 06:25:40 -04004155 .no_write_same = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156};
4157
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004158static int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159megaraid_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
4160{
4161 struct Scsi_Host *host;
4162 adapter_t *adapter;
4163 unsigned long mega_baseport, tbase, flag = 0;
4164 u16 subsysid, subsysvid;
4165 u8 pci_bus, pci_dev_func;
4166 int irq, i, j;
4167 int error = -ENODEV;
4168
4169 if (pci_enable_device(pdev))
4170 goto out;
4171 pci_set_master(pdev);
4172
4173 pci_bus = pdev->bus->number;
4174 pci_dev_func = pdev->devfn;
4175
4176 /*
4177 * The megaraid3 stuff reports the ID of the Intel part which is not
4178 * remotely specific to the megaraid
4179 */
4180 if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
4181 u16 magic;
4182 /*
4183 * Don't fall over the Compaq management cards using the same
4184 * PCI identifier
4185 */
4186 if (pdev->subsystem_vendor == PCI_VENDOR_ID_COMPAQ &&
4187 pdev->subsystem_device == 0xC000)
4188 return -ENODEV;
4189 /* Now check the magic signature byte */
4190 pci_read_config_word(pdev, PCI_CONF_AMISIG, &magic);
4191 if (magic != HBA_SIGNATURE_471 && magic != HBA_SIGNATURE)
4192 return -ENODEV;
4193 /* Ok it is probably a megaraid */
4194 }
4195
4196 /*
4197 * For these vendor and device ids, signature offsets are not
4198 * valid and 64 bit is implicit
4199 */
4200 if (id->driver_data & BOARD_64BIT)
4201 flag |= BOARD_64BIT;
4202 else {
4203 u32 magic64;
4204
4205 pci_read_config_dword(pdev, PCI_CONF_AMISIG64, &magic64);
4206 if (magic64 == HBA_SIGNATURE_64BIT)
4207 flag |= BOARD_64BIT;
4208 }
4209
4210 subsysvid = pdev->subsystem_vendor;
4211 subsysid = pdev->subsystem_device;
4212
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05004213 dev_notice(&pdev->dev, "found 0x%4.04x:0x%4.04x\n",
4214 id->vendor, id->device);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
4216 /* Read the base port and IRQ from PCI */
4217 mega_baseport = pci_resource_start(pdev, 0);
4218 irq = pdev->irq;
4219
4220 tbase = mega_baseport;
4221 if (pci_resource_flags(pdev, 0) & IORESOURCE_MEM) {
4222 flag |= BOARD_MEMMAP;
4223
4224 if (!request_mem_region(mega_baseport, 128, "megaraid")) {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05004225 dev_warn(&pdev->dev, "mem region busy!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 goto out_disable_device;
4227 }
4228
4229 mega_baseport = (unsigned long)ioremap(mega_baseport, 128);
4230 if (!mega_baseport) {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05004231 dev_warn(&pdev->dev, "could not map hba memory\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 goto out_release_region;
4233 }
4234 } else {
4235 flag |= BOARD_IOMAP;
4236 mega_baseport += 0x10;
4237
4238 if (!request_region(mega_baseport, 16, "megaraid"))
4239 goto out_disable_device;
4240 }
4241
4242 /* Initialize SCSI Host structure */
4243 host = scsi_host_alloc(&megaraid_template, sizeof(adapter_t));
4244 if (!host)
4245 goto out_iounmap;
4246
4247 adapter = (adapter_t *)host->hostdata;
4248 memset(adapter, 0, sizeof(adapter_t));
4249
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05004250 dev_notice(&pdev->dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 "scsi%d:Found MegaRAID controller at 0x%lx, IRQ:%d\n",
4252 host->host_no, mega_baseport, irq);
4253
4254 adapter->base = mega_baseport;
Jeff Garzik00769ec2006-12-03 20:49:23 -05004255 if (flag & BOARD_MEMMAP)
4256 adapter->mmio_base = (void __iomem *) mega_baseport;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004257
4258 INIT_LIST_HEAD(&adapter->free_list);
4259 INIT_LIST_HEAD(&adapter->pending_list);
4260 INIT_LIST_HEAD(&adapter->completed_list);
4261
4262 adapter->flag = flag;
4263 spin_lock_init(&adapter->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
4265 host->cmd_per_lun = max_cmd_per_lun;
4266 host->max_sectors = max_sectors_per_io;
4267
4268 adapter->dev = pdev;
4269 adapter->host = host;
4270
4271 adapter->host->irq = irq;
4272
4273 if (flag & BOARD_MEMMAP)
4274 adapter->host->base = tbase;
4275 else {
4276 adapter->host->io_port = tbase;
4277 adapter->host->n_io_port = 16;
4278 }
4279
4280 adapter->host->unique_id = (pci_bus << 8) | pci_dev_func;
4281
4282 /*
4283 * Allocate buffer to issue internal commands.
4284 */
4285 adapter->mega_buffer = pci_alloc_consistent(adapter->dev,
4286 MEGA_BUFFER_SIZE, &adapter->buf_dma_handle);
4287 if (!adapter->mega_buffer) {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05004288 dev_warn(&pdev->dev, "out of RAM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 goto out_host_put;
4290 }
4291
4292 adapter->scb_list = kmalloc(sizeof(scb_t) * MAX_COMMANDS, GFP_KERNEL);
4293 if (!adapter->scb_list) {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05004294 dev_warn(&pdev->dev, "out of RAM\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 goto out_free_cmd_buffer;
4296 }
4297
4298 if (request_irq(irq, (adapter->flag & BOARD_MEMMAP) ?
4299 megaraid_isr_memmapped : megaraid_isr_iomapped,
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07004300 IRQF_SHARED, "megaraid", adapter)) {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05004301 dev_warn(&pdev->dev, "Couldn't register IRQ %d!\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 goto out_free_scb_list;
4303 }
4304
4305 if (mega_setup_mailbox(adapter))
4306 goto out_free_irq;
4307
4308 if (mega_query_adapter(adapter))
4309 goto out_free_mbox;
4310
4311 /*
4312 * Have checks for some buggy f/w
4313 */
4314 if ((subsysid == 0x1111) && (subsysvid == 0x1111)) {
4315 /*
4316 * Which firmware
4317 */
4318 if (!strcmp(adapter->fw_version, "3.00") ||
4319 !strcmp(adapter->fw_version, "3.01")) {
4320
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05004321 dev_warn(&pdev->dev,
4322 "Your card is a Dell PERC "
4323 "2/SC RAID controller with "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004324 "firmware\nmegaraid: 3.00 or 3.01. "
4325 "This driver is known to have "
4326 "corruption issues\nmegaraid: with "
4327 "those firmware versions on this "
4328 "specific card. In order\nmegaraid: "
4329 "to protect your data, please upgrade "
4330 "your firmware to version\nmegaraid: "
4331 "3.10 or later, available from the "
4332 "Dell Technical Support web\n"
4333 "megaraid: site at\nhttp://support."
4334 "dell.com/us/en/filelib/download/"
4335 "index.asp?fileid=2940\n"
4336 );
4337 }
4338 }
4339
4340 /*
4341 * If we have a HP 1M(0x60E7)/2M(0x60E8) controller with
4342 * firmware H.01.07, H.01.08, and H.01.09 disable 64 bit
4343 * support, since this firmware cannot handle 64 bit
4344 * addressing
4345 */
Jon Mason54ebfd52012-07-10 15:31:28 -07004346 if ((subsysvid == PCI_VENDOR_ID_HP) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 ((subsysid == 0x60E7) || (subsysid == 0x60E8))) {
4348 /*
4349 * which firmware
4350 */
4351 if (!strcmp(adapter->fw_version, "H01.07") ||
4352 !strcmp(adapter->fw_version, "H01.08") ||
4353 !strcmp(adapter->fw_version, "H01.09") ) {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05004354 dev_warn(&pdev->dev,
4355 "Firmware H.01.07, "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 "H.01.08, and H.01.09 on 1M/2M "
4357 "controllers\n"
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05004358 "do not support 64 bit "
4359 "addressing.\nDISABLING "
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 "64 bit support.\n");
4361 adapter->flag &= ~BOARD_64BIT;
4362 }
4363 }
4364
4365 if (mega_is_bios_enabled(adapter))
4366 mega_hbas[hba_count].is_bios_enabled = 1;
4367 mega_hbas[hba_count].hostdata_addr = adapter;
4368
4369 /*
4370 * Find out which channel is raid and which is scsi. This is
4371 * for ROMB support.
4372 */
4373 mega_enum_raid_scsi(adapter);
4374
4375 /*
4376 * Find out if a logical drive is set as the boot drive. If
4377 * there is one, will make that as the first logical drive.
4378 * ROMB: Do we have to boot from a physical drive. Then all
4379 * the physical drives would appear before the logical disks.
4380 * Else, all the physical drives would be exported to the mid
4381 * layer after logical drives.
4382 */
4383 mega_get_boot_drv(adapter);
4384
4385 if (adapter->boot_pdrv_enabled) {
4386 j = adapter->product_info.nchannels;
4387 for( i = 0; i < j; i++ )
4388 adapter->logdrv_chan[i] = 0;
4389 for( i = j; i < NVIRT_CHAN + j; i++ )
4390 adapter->logdrv_chan[i] = 1;
4391 } else {
4392 for (i = 0; i < NVIRT_CHAN; i++)
4393 adapter->logdrv_chan[i] = 1;
4394 for (i = NVIRT_CHAN; i < MAX_CHANNELS+NVIRT_CHAN; i++)
4395 adapter->logdrv_chan[i] = 0;
4396 adapter->mega_ch_class <<= NVIRT_CHAN;
4397 }
4398
4399 /*
4400 * Do we support random deletion and addition of logical
4401 * drives
4402 */
4403 adapter->read_ldidmap = 0; /* set it after first logdrv
4404 delete cmd */
4405 adapter->support_random_del = mega_support_random_del(adapter);
4406
4407 /* Initialize SCBs */
4408 if (mega_init_scb(adapter))
4409 goto out_free_mbox;
4410
4411 /*
4412 * Reset the pending commands counter
4413 */
4414 atomic_set(&adapter->pend_cmds, 0);
4415
4416 /*
4417 * Reset the adapter quiescent flag
4418 */
4419 atomic_set(&adapter->quiescent, 0);
4420
4421 hba_soft_state[hba_count] = adapter;
4422
4423 /*
4424 * Fill in the structure which needs to be passed back to the
4425 * application when it does an ioctl() for controller related
4426 * information.
4427 */
4428 i = hba_count;
4429
4430 mcontroller[i].base = mega_baseport;
4431 mcontroller[i].irq = irq;
4432 mcontroller[i].numldrv = adapter->numldrv;
4433 mcontroller[i].pcibus = pci_bus;
4434 mcontroller[i].pcidev = id->device;
4435 mcontroller[i].pcifun = PCI_FUNC (pci_dev_func);
4436 mcontroller[i].pciid = -1;
4437 mcontroller[i].pcivendor = id->vendor;
4438 mcontroller[i].pcislot = PCI_SLOT(pci_dev_func);
4439 mcontroller[i].uid = (pci_bus << 8) | pci_dev_func;
4440
4441
4442 /* Set the Mode of addressing to 64 bit if we can */
4443 if ((adapter->flag & BOARD_64BIT) && (sizeof(dma_addr_t) == 8)) {
Yang Hongyang6a355282009-04-06 19:01:13 -07004444 pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445 adapter->has_64bit_addr = 1;
4446 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07004447 pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 adapter->has_64bit_addr = 0;
4449 }
4450
Arjan van de Ven0b950672006-01-11 13:16:10 +01004451 mutex_init(&adapter->int_mtx);
Christoph Hellwig8d115f82005-06-19 13:42:05 +02004452 init_completion(&adapter->int_waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453
4454 adapter->this_id = DEFAULT_INITIATOR_ID;
4455 adapter->host->this_id = DEFAULT_INITIATOR_ID;
4456
4457#if MEGA_HAVE_CLUSTERING
4458 /*
4459 * Is cluster support enabled on this controller
4460 * Note: In a cluster the HBAs ( the initiators ) will have
4461 * different target IDs and we cannot assume it to be 7. Call
4462 * to mega_support_cluster() will get the target ids also if
4463 * the cluster support is available
4464 */
4465 adapter->has_cluster = mega_support_cluster(adapter);
4466 if (adapter->has_cluster) {
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05004467 dev_notice(&pdev->dev,
4468 "Cluster driver, initiator id:%d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 adapter->this_id);
4470 }
4471#endif
4472
4473 pci_set_drvdata(pdev, host);
4474
4475 mega_create_proc_entry(hba_count, mega_proc_dir_entry);
4476
4477 error = scsi_add_host(host, &pdev->dev);
4478 if (error)
4479 goto out_free_mbox;
4480
4481 scsi_scan_host(host);
4482 hba_count++;
4483 return 0;
4484
4485 out_free_mbox:
4486 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4487 adapter->una_mbox64, adapter->una_mbox64_dma);
4488 out_free_irq:
4489 free_irq(adapter->host->irq, adapter);
4490 out_free_scb_list:
4491 kfree(adapter->scb_list);
4492 out_free_cmd_buffer:
4493 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4494 adapter->mega_buffer, adapter->buf_dma_handle);
4495 out_host_put:
4496 scsi_host_put(host);
4497 out_iounmap:
4498 if (flag & BOARD_MEMMAP)
4499 iounmap((void *)mega_baseport);
4500 out_release_region:
4501 if (flag & BOARD_MEMMAP)
4502 release_mem_region(tbase, 128);
4503 else
4504 release_region(mega_baseport, 16);
4505 out_disable_device:
4506 pci_disable_device(pdev);
4507 out:
4508 return error;
4509}
4510
4511static void
4512__megaraid_shutdown(adapter_t *adapter)
4513{
4514 u_char raw_mbox[sizeof(struct mbox_out)];
4515 mbox_t *mbox = (mbox_t *)raw_mbox;
4516 int i;
4517
4518 /* Flush adapter cache */
4519 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4520 raw_mbox[0] = FLUSH_ADAPTER;
4521
4522 free_irq(adapter->host->irq, adapter);
4523
4524 /* Issue a blocking (interrupts disabled) command to the card */
4525 issue_scb_block(adapter, raw_mbox);
4526
4527 /* Flush disks cache */
4528 memset(&mbox->m_out, 0, sizeof(raw_mbox));
4529 raw_mbox[0] = FLUSH_SYSTEM;
4530
4531 /* Issue a blocking (interrupts disabled) command to the card */
4532 issue_scb_block(adapter, raw_mbox);
4533
4534 if (atomic_read(&adapter->pend_cmds) > 0)
Bjorn Helgaas3b8a1ba2015-07-07 15:52:25 -05004535 dev_warn(&adapter->dev->dev, "pending commands!!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536
4537 /*
4538 * Have a delibrate delay to make sure all the caches are
4539 * actually flushed.
4540 */
4541 for (i = 0; i <= 10; i++)
4542 mdelay(1000);
4543}
4544
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004545static void
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546megaraid_remove_one(struct pci_dev *pdev)
4547{
4548 struct Scsi_Host *host = pci_get_drvdata(pdev);
4549 adapter_t *adapter = (adapter_t *)host->hostdata;
Christoph Hellwigf7680be2018-04-13 20:57:56 +02004550 char buf[12] = { 0 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551
4552 scsi_remove_host(host);
4553
4554 __megaraid_shutdown(adapter);
4555
4556 /* Free our resources */
4557 if (adapter->flag & BOARD_MEMMAP) {
4558 iounmap((void *)adapter->base);
4559 release_mem_region(adapter->host->base, 128);
4560 } else
4561 release_region(adapter->base, 16);
4562
4563 mega_free_sgl(adapter);
4564
Christoph Hellwigf7680be2018-04-13 20:57:56 +02004565 sprintf(buf, "hba%d", adapter->host->host_no);
4566 remove_proc_subtree(buf, mega_proc_dir_entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567
4568 pci_free_consistent(adapter->dev, MEGA_BUFFER_SIZE,
4569 adapter->mega_buffer, adapter->buf_dma_handle);
4570 kfree(adapter->scb_list);
4571 pci_free_consistent(adapter->dev, sizeof(mbox64_t),
4572 adapter->una_mbox64, adapter->una_mbox64_dma);
4573
4574 scsi_host_put(host);
4575 pci_disable_device(pdev);
4576
4577 hba_count--;
4578}
4579
4580static void
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07004581megaraid_shutdown(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582{
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07004583 struct Scsi_Host *host = pci_get_drvdata(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 adapter_t *adapter = (adapter_t *)host->hostdata;
4585
4586 __megaraid_shutdown(adapter);
4587}
4588
4589static struct pci_device_id megaraid_pci_tbl[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID,
4591 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
4592 {PCI_VENDOR_ID_AMI, PCI_DEVICE_ID_AMI_MEGARAID2,
4593 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_AMI_MEGARAID3,
4595 PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596 {0,}
4597};
4598MODULE_DEVICE_TABLE(pci, megaraid_pci_tbl);
4599
4600static struct pci_driver megaraid_pci_driver = {
Ju, Seokmann3542adc2006-02-09 12:32:59 -07004601 .name = "megaraid_legacy",
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 .id_table = megaraid_pci_tbl,
4603 .probe = megaraid_probe_one,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08004604 .remove = megaraid_remove_one,
Greg Kroah-Hartmand18c3db2005-06-23 17:35:56 -07004605 .shutdown = megaraid_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606};
4607
4608static int __init megaraid_init(void)
4609{
4610 int error;
4611
4612 if ((max_cmd_per_lun <= 0) || (max_cmd_per_lun > MAX_CMD_PER_LUN))
4613 max_cmd_per_lun = MAX_CMD_PER_LUN;
4614 if (max_mbox_busy_wait > MBOX_BUSY_WAIT)
4615 max_mbox_busy_wait = MBOX_BUSY_WAIT;
4616
4617#ifdef CONFIG_PROC_FS
Alexey Dobriyanc74c1202008-04-29 01:01:44 -07004618 mega_proc_dir_entry = proc_mkdir("megaraid", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 if (!mega_proc_dir_entry) {
4620 printk(KERN_WARNING
4621 "megaraid: failed to create megaraid root\n");
4622 }
4623#endif
Richard Knutsson4520b002007-02-14 01:40:39 +01004624 error = pci_register_driver(&megaraid_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 if (error) {
4626#ifdef CONFIG_PROC_FS
Alexey Dobriyanc74c1202008-04-29 01:01:44 -07004627 remove_proc_entry("megaraid", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628#endif
4629 return error;
4630 }
4631
4632 /*
4633 * Register the driver as a character device, for applications
4634 * to access it for ioctls.
4635 * First argument (major) to register_chrdev implies a dynamic
4636 * major number allocation.
4637 */
Ju, Seokmann3492b322005-11-17 13:13:31 -05004638 major = register_chrdev(0, "megadev_legacy", &megadev_fops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 if (!major) {
4640 printk(KERN_WARNING
4641 "megaraid: failed to register char device\n");
4642 }
4643
4644 return 0;
4645}
4646
4647static void __exit megaraid_exit(void)
4648{
4649 /*
4650 * Unregister the character device interface to the driver.
4651 */
Ju, Seokmann3492b322005-11-17 13:13:31 -05004652 unregister_chrdev(major, "megadev_legacy");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653
4654 pci_unregister_driver(&megaraid_pci_driver);
4655
4656#ifdef CONFIG_PROC_FS
Alexey Dobriyanc74c1202008-04-29 01:01:44 -07004657 remove_proc_entry("megaraid", NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658#endif
4659}
4660
4661module_init(megaraid_init);
4662module_exit(megaraid_exit);
4663
4664/* vi: set ts=8 sw=8 tw=78: */