blob: 2fb53af8de2a186339f296f2745d57899fd8a97b [file] [log] [blame]
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001/*
2 *
3 * Linux MegaRAID driver for SAS based RAID controllers
4 *
5 * Copyright (c) 2003-2005 LSI Logic Corporation.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
12 * FILE : megaraid_sas.c
Sumant Patrocb59aa62006-01-25 11:53:25 -080013 * Version : v00.00.02.01
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040014 *
15 * Authors:
16 * Sreenivas Bagalkote <Sreenivas.Bagalkote@lsil.com>
17 * Sumant Patro <Sumant.Patro@lsil.com>
18 *
19 * List of supported controllers
20 *
21 * OEM Product Name VID DID SSVID SSID
22 * --- ------------ --- --- ---- ----
23 */
24
25#include <linux/kernel.h>
26#include <linux/types.h>
27#include <linux/pci.h>
28#include <linux/list.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040029#include <linux/moduleparam.h>
30#include <linux/module.h>
31#include <linux/spinlock.h>
32#include <linux/interrupt.h>
33#include <linux/delay.h>
34#include <linux/uio.h>
35#include <asm/uaccess.h>
Al Viro43399232005-10-04 17:36:04 +010036#include <linux/fs.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040037#include <linux/compat.h>
Arjan van de Ven0b950672006-01-11 13:16:10 +010038#include <linux/mutex.h>
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040039
40#include <scsi/scsi.h>
41#include <scsi/scsi_cmnd.h>
42#include <scsi/scsi_device.h>
43#include <scsi/scsi_host.h>
44#include "megaraid_sas.h"
45
46MODULE_LICENSE("GPL");
47MODULE_VERSION(MEGASAS_VERSION);
48MODULE_AUTHOR("sreenivas.bagalkote@lsil.com");
49MODULE_DESCRIPTION("LSI Logic MegaRAID SAS Driver");
50
51/*
52 * PCI ID table for all supported controllers
53 */
54static struct pci_device_id megasas_pci_table[] = {
55
56 {
57 PCI_VENDOR_ID_LSI_LOGIC,
58 PCI_DEVICE_ID_LSI_SAS1064R,
59 PCI_ANY_ID,
60 PCI_ANY_ID,
61 },
62 {
63 PCI_VENDOR_ID_DELL,
64 PCI_DEVICE_ID_DELL_PERC5,
65 PCI_ANY_ID,
66 PCI_ANY_ID,
67 },
68 {0} /* Terminating entry */
69};
70
71MODULE_DEVICE_TABLE(pci, megasas_pci_table);
72
73static int megasas_mgmt_majorno;
74static struct megasas_mgmt_info megasas_mgmt_info;
75static struct fasync_struct *megasas_async_queue;
Arjan van de Ven0b950672006-01-11 13:16:10 +010076static DEFINE_MUTEX(megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040077
78/**
79 * megasas_get_cmd - Get a command from the free pool
80 * @instance: Adapter soft state
81 *
82 * Returns a free command from the pool
83 */
Arjan van de Ven858119e2006-01-14 13:20:43 -080084static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -040085 *instance)
86{
87 unsigned long flags;
88 struct megasas_cmd *cmd = NULL;
89
90 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
91
92 if (!list_empty(&instance->cmd_pool)) {
93 cmd = list_entry((&instance->cmd_pool)->next,
94 struct megasas_cmd, list);
95 list_del_init(&cmd->list);
96 } else {
97 printk(KERN_ERR "megasas: Command pool empty!\n");
98 }
99
100 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
101 return cmd;
102}
103
104/**
105 * megasas_return_cmd - Return a cmd to free command pool
106 * @instance: Adapter soft state
107 * @cmd: Command packet to be returned to free command pool
108 */
109static inline void
110megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
111{
112 unsigned long flags;
113
114 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
115
116 cmd->scmd = NULL;
117 list_add_tail(&cmd->list, &instance->cmd_pool);
118
119 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
120}
121
122/**
123 * megasas_enable_intr - Enables interrupts
124 * @regs: MFI register set
125 */
126static inline void
127megasas_enable_intr(struct megasas_register_set __iomem * regs)
128{
129 writel(1, &(regs)->outbound_intr_mask);
130
131 /* Dummy readl to force pci flush */
132 readl(&regs->outbound_intr_mask);
133}
134
135/**
136 * megasas_disable_intr - Disables interrupts
137 * @regs: MFI register set
138 */
139static inline void
140megasas_disable_intr(struct megasas_register_set __iomem * regs)
141{
142 u32 mask = readl(&regs->outbound_intr_mask) & (~0x00000001);
143 writel(mask, &regs->outbound_intr_mask);
144
145 /* Dummy readl to force pci flush */
146 readl(&regs->outbound_intr_mask);
147}
148
149/**
150 * megasas_issue_polled - Issues a polling command
151 * @instance: Adapter soft state
152 * @cmd: Command packet to be issued
153 *
154 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
155 */
156static int
157megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
158{
159 int i;
160 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
161
162 struct megasas_header *frame_hdr = &cmd->frame->hdr;
163
164 frame_hdr->cmd_status = 0xFF;
165 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
166
167 /*
168 * Issue the frame using inbound queue port
169 */
170 writel(cmd->frame_phys_addr >> 3,
171 &instance->reg_set->inbound_queue_port);
172
173 /*
174 * Wait for cmd_status to change
175 */
176 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) {
177 rmb();
178 msleep(1);
179 }
180
181 if (frame_hdr->cmd_status == 0xff)
182 return -ETIME;
183
184 return 0;
185}
186
187/**
188 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds
189 * @instance: Adapter soft state
190 * @cmd: Command to be issued
191 *
192 * This function waits on an event for the command to be returned from ISR.
193 * Used to issue ioctl commands.
194 */
195static int
196megasas_issue_blocked_cmd(struct megasas_instance *instance,
197 struct megasas_cmd *cmd)
198{
199 cmd->cmd_status = ENODATA;
200
201 writel(cmd->frame_phys_addr >> 3,
202 &instance->reg_set->inbound_queue_port);
203
204 wait_event(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA));
205
206 return 0;
207}
208
209/**
210 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd
211 * @instance: Adapter soft state
212 * @cmd_to_abort: Previously issued cmd to be aborted
213 *
214 * MFI firmware can abort previously issued AEN comamnd (automatic event
215 * notification). The megasas_issue_blocked_abort_cmd() issues such abort
216 * cmd and blocks till it is completed.
217 */
218static int
219megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
220 struct megasas_cmd *cmd_to_abort)
221{
222 struct megasas_cmd *cmd;
223 struct megasas_abort_frame *abort_fr;
224
225 cmd = megasas_get_cmd(instance);
226
227 if (!cmd)
228 return -1;
229
230 abort_fr = &cmd->frame->abort;
231
232 /*
233 * Prepare and issue the abort frame
234 */
235 abort_fr->cmd = MFI_CMD_ABORT;
236 abort_fr->cmd_status = 0xFF;
237 abort_fr->flags = 0;
238 abort_fr->abort_context = cmd_to_abort->index;
239 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
240 abort_fr->abort_mfi_phys_addr_hi = 0;
241
242 cmd->sync_cmd = 1;
243 cmd->cmd_status = 0xFF;
244
245 writel(cmd->frame_phys_addr >> 3,
246 &instance->reg_set->inbound_queue_port);
247
248 /*
249 * Wait for this cmd to complete
250 */
251 wait_event(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF));
252
253 megasas_return_cmd(instance, cmd);
254 return 0;
255}
256
257/**
258 * megasas_make_sgl32 - Prepares 32-bit SGL
259 * @instance: Adapter soft state
260 * @scp: SCSI command from the mid-layer
261 * @mfi_sgl: SGL to be filled in
262 *
263 * If successful, this function returns the number of SG elements. Otherwise,
264 * it returnes -1.
265 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800266static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400267megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
268 union megasas_sgl *mfi_sgl)
269{
270 int i;
271 int sge_count;
272 struct scatterlist *os_sgl;
273
274 /*
275 * Return 0 if there is no data transfer
276 */
277 if (!scp->request_buffer || !scp->request_bufflen)
278 return 0;
279
280 if (!scp->use_sg) {
281 mfi_sgl->sge32[0].phys_addr = pci_map_single(instance->pdev,
282 scp->
283 request_buffer,
284 scp->
285 request_bufflen,
286 scp->
287 sc_data_direction);
288 mfi_sgl->sge32[0].length = scp->request_bufflen;
289
290 return 1;
291 }
292
293 os_sgl = (struct scatterlist *)scp->request_buffer;
294 sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
295 scp->sc_data_direction);
296
297 for (i = 0; i < sge_count; i++, os_sgl++) {
298 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
299 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
300 }
301
302 return sge_count;
303}
304
305/**
306 * megasas_make_sgl64 - Prepares 64-bit SGL
307 * @instance: Adapter soft state
308 * @scp: SCSI command from the mid-layer
309 * @mfi_sgl: SGL to be filled in
310 *
311 * If successful, this function returns the number of SG elements. Otherwise,
312 * it returnes -1.
313 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800314static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400315megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
316 union megasas_sgl *mfi_sgl)
317{
318 int i;
319 int sge_count;
320 struct scatterlist *os_sgl;
321
322 /*
323 * Return 0 if there is no data transfer
324 */
325 if (!scp->request_buffer || !scp->request_bufflen)
326 return 0;
327
328 if (!scp->use_sg) {
329 mfi_sgl->sge64[0].phys_addr = pci_map_single(instance->pdev,
330 scp->
331 request_buffer,
332 scp->
333 request_bufflen,
334 scp->
335 sc_data_direction);
336
337 mfi_sgl->sge64[0].length = scp->request_bufflen;
338
339 return 1;
340 }
341
342 os_sgl = (struct scatterlist *)scp->request_buffer;
343 sge_count = pci_map_sg(instance->pdev, os_sgl, scp->use_sg,
344 scp->sc_data_direction);
345
346 for (i = 0; i < sge_count; i++, os_sgl++) {
347 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
348 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
349 }
350
351 return sge_count;
352}
353
354/**
355 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
356 * @instance: Adapter soft state
357 * @scp: SCSI command
358 * @cmd: Command to be prepared in
359 *
360 * This function prepares CDB commands. These are typcially pass-through
361 * commands to the devices.
362 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800363static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400364megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
365 struct megasas_cmd *cmd)
366{
367 u32 sge_sz;
368 int sge_bytes;
369 u32 is_logical;
370 u32 device_id;
371 u16 flags = 0;
372 struct megasas_pthru_frame *pthru;
373
374 is_logical = MEGASAS_IS_LOGICAL(scp);
375 device_id = MEGASAS_DEV_INDEX(instance, scp);
376 pthru = (struct megasas_pthru_frame *)cmd->frame;
377
378 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
379 flags = MFI_FRAME_DIR_WRITE;
380 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
381 flags = MFI_FRAME_DIR_READ;
382 else if (scp->sc_data_direction == PCI_DMA_NONE)
383 flags = MFI_FRAME_DIR_NONE;
384
385 /*
386 * Prepare the DCDB frame
387 */
388 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
389 pthru->cmd_status = 0x0;
390 pthru->scsi_status = 0x0;
391 pthru->target_id = device_id;
392 pthru->lun = scp->device->lun;
393 pthru->cdb_len = scp->cmd_len;
394 pthru->timeout = 0;
395 pthru->flags = flags;
396 pthru->data_xfer_len = scp->request_bufflen;
397
398 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
399
400 /*
401 * Construct SGL
402 */
403 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
404 sizeof(struct megasas_sge32);
405
406 if (IS_DMA64) {
407 pthru->flags |= MFI_FRAME_SGL64;
408 pthru->sge_count = megasas_make_sgl64(instance, scp,
409 &pthru->sgl);
410 } else
411 pthru->sge_count = megasas_make_sgl32(instance, scp,
412 &pthru->sgl);
413
414 /*
415 * Sense info specific
416 */
417 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
418 pthru->sense_buf_phys_addr_hi = 0;
419 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
420
421 sge_bytes = sge_sz * pthru->sge_count;
422
423 /*
424 * Compute the total number of frames this command consumes. FW uses
425 * this number to pull sufficient number of frames from host memory.
426 */
427 cmd->frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
428 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) + 1;
429
430 if (cmd->frame_count > 7)
431 cmd->frame_count = 8;
432
433 return cmd->frame_count;
434}
435
436/**
437 * megasas_build_ldio - Prepares IOs to logical devices
438 * @instance: Adapter soft state
439 * @scp: SCSI command
440 * @cmd: Command to to be prepared
441 *
442 * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
443 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800444static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400445megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
446 struct megasas_cmd *cmd)
447{
448 u32 sge_sz;
449 int sge_bytes;
450 u32 device_id;
451 u8 sc = scp->cmnd[0];
452 u16 flags = 0;
453 struct megasas_io_frame *ldio;
454
455 device_id = MEGASAS_DEV_INDEX(instance, scp);
456 ldio = (struct megasas_io_frame *)cmd->frame;
457
458 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
459 flags = MFI_FRAME_DIR_WRITE;
460 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
461 flags = MFI_FRAME_DIR_READ;
462
463 /*
464 * Preare the Logical IO frame: 2nd bit is zero for all read cmds
465 */
466 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
467 ldio->cmd_status = 0x0;
468 ldio->scsi_status = 0x0;
469 ldio->target_id = device_id;
470 ldio->timeout = 0;
471 ldio->reserved_0 = 0;
472 ldio->pad_0 = 0;
473 ldio->flags = flags;
474 ldio->start_lba_hi = 0;
475 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
476
477 /*
478 * 6-byte READ(0x08) or WRITE(0x0A) cdb
479 */
480 if (scp->cmd_len == 6) {
481 ldio->lba_count = (u32) scp->cmnd[4];
482 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
483 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
484
485 ldio->start_lba_lo &= 0x1FFFFF;
486 }
487
488 /*
489 * 10-byte READ(0x28) or WRITE(0x2A) cdb
490 */
491 else if (scp->cmd_len == 10) {
492 ldio->lba_count = (u32) scp->cmnd[8] |
493 ((u32) scp->cmnd[7] << 8);
494 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
495 ((u32) scp->cmnd[3] << 16) |
496 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
497 }
498
499 /*
500 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
501 */
502 else if (scp->cmd_len == 12) {
503 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
504 ((u32) scp->cmnd[7] << 16) |
505 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
506
507 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
508 ((u32) scp->cmnd[3] << 16) |
509 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
510 }
511
512 /*
513 * 16-byte READ(0x88) or WRITE(0x8A) cdb
514 */
515 else if (scp->cmd_len == 16) {
516 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
517 ((u32) scp->cmnd[11] << 16) |
518 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
519
520 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
521 ((u32) scp->cmnd[7] << 16) |
522 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
523
524 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
525 ((u32) scp->cmnd[3] << 16) |
526 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
527
528 }
529
530 /*
531 * Construct SGL
532 */
533 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
534 sizeof(struct megasas_sge32);
535
536 if (IS_DMA64) {
537 ldio->flags |= MFI_FRAME_SGL64;
538 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
539 } else
540 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
541
542 /*
543 * Sense info specific
544 */
545 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
546 ldio->sense_buf_phys_addr_hi = 0;
547 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
548
549 sge_bytes = sge_sz * ldio->sge_count;
550
551 cmd->frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
552 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) + 1;
553
554 if (cmd->frame_count > 7)
555 cmd->frame_count = 8;
556
557 return cmd->frame_count;
558}
559
560/**
Sumant Patrocb59aa62006-01-25 11:53:25 -0800561 * megasas_is_ldio - Checks if the cmd is for logical drive
562 * @scmd: SCSI command
563 *
564 * Called by megasas_queue_command to find out if the command to be queued
565 * is a logical drive command
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400566 */
Sumant Patrocb59aa62006-01-25 11:53:25 -0800567static inline int megasas_is_ldio(struct scsi_cmnd *cmd)
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400568{
Sumant Patrocb59aa62006-01-25 11:53:25 -0800569 if (!MEGASAS_IS_LOGICAL(cmd))
570 return 0;
571 switch (cmd->cmnd[0]) {
572 case READ_10:
573 case WRITE_10:
574 case READ_12:
575 case WRITE_12:
576 case READ_6:
577 case WRITE_6:
578 case READ_16:
579 case WRITE_16:
580 return 1;
581 default:
582 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400583 }
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400584}
585
586/**
587 * megasas_queue_command - Queue entry point
588 * @scmd: SCSI command to be queued
589 * @done: Callback entry point
590 */
591static int
592megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
593{
594 u32 frame_count;
595 unsigned long flags;
596 struct megasas_cmd *cmd;
597 struct megasas_instance *instance;
598
599 instance = (struct megasas_instance *)
600 scmd->device->host->hostdata;
601 scmd->scsi_done = done;
602 scmd->result = 0;
603
Sumant Patrocb59aa62006-01-25 11:53:25 -0800604 if (MEGASAS_IS_LOGICAL(scmd) &&
605 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
606 scmd->result = DID_BAD_TARGET << 16;
607 goto out_done;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400608 }
609
Sumant Patrocb59aa62006-01-25 11:53:25 -0800610 cmd = megasas_get_cmd(instance);
611 if (!cmd)
612 return SCSI_MLQUEUE_HOST_BUSY;
613
614 /*
615 * Logical drive command
616 */
617 if (megasas_is_ldio(scmd))
618 frame_count = megasas_build_ldio(instance, scmd, cmd);
619 else
620 frame_count = megasas_build_dcdb(instance, scmd, cmd);
621
622 if (!frame_count)
623 goto out_return_cmd;
624
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400625 cmd->scmd = scmd;
626 scmd->SCp.ptr = (char *)cmd;
627 scmd->SCp.sent_command = jiffies;
628
629 /*
630 * Issue the command to the FW
631 */
632 spin_lock_irqsave(&instance->instance_lock, flags);
633 instance->fw_outstanding++;
634 spin_unlock_irqrestore(&instance->instance_lock, flags);
635
636 writel(((cmd->frame_phys_addr >> 3) | (cmd->frame_count - 1)),
637 &instance->reg_set->inbound_queue_port);
638
639 return 0;
Sumant Patrocb59aa62006-01-25 11:53:25 -0800640
641 out_return_cmd:
642 megasas_return_cmd(instance, cmd);
643 out_done:
644 done(scmd);
645 return 0;
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400646}
647
648/**
649 * megasas_wait_for_outstanding - Wait for all outstanding cmds
650 * @instance: Adapter soft state
651 *
652 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
653 * complete all its outstanding commands. Returns error if one or more IOs
654 * are pending after this time period. It also marks the controller dead.
655 */
656static int megasas_wait_for_outstanding(struct megasas_instance *instance)
657{
658 int i;
659 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
660
661 for (i = 0; i < wait_time; i++) {
662
663 if (!instance->fw_outstanding)
664 break;
665
666 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
667 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
668 "commands to complete\n", i,
669 instance->fw_outstanding);
670 }
671
672 msleep(1000);
673 }
674
675 if (instance->fw_outstanding) {
676 instance->hw_crit_error = 1;
677 return FAILED;
678 }
679
680 return SUCCESS;
681}
682
683/**
684 * megasas_generic_reset - Generic reset routine
685 * @scmd: Mid-layer SCSI command
686 *
687 * This routine implements a generic reset handler for device, bus and host
688 * reset requests. Device, bus and host specific reset handlers can use this
689 * function after they do their specific tasks.
690 */
691static int megasas_generic_reset(struct scsi_cmnd *scmd)
692{
693 int ret_val;
694 struct megasas_instance *instance;
695
696 instance = (struct megasas_instance *)scmd->device->host->hostdata;
697
Jeff Garzik017560f2005-10-24 18:04:36 -0400698 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x\n",
699 scmd->serial_number, scmd->cmnd[0]);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400700
701 if (instance->hw_crit_error) {
702 printk(KERN_ERR "megasas: cannot recover from previous reset "
703 "failures\n");
704 return FAILED;
705 }
706
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400707 ret_val = megasas_wait_for_outstanding(instance);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400708 if (ret_val == SUCCESS)
709 printk(KERN_NOTICE "megasas: reset successful \n");
710 else
711 printk(KERN_ERR "megasas: failed to do reset\n");
712
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400713 return ret_val;
714}
715
716static enum scsi_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
717{
718 unsigned long seconds;
719
720 if (scmd->SCp.ptr) {
721 seconds = (jiffies - scmd->SCp.sent_command) / HZ;
722
723 if (seconds < 90) {
724 return EH_RESET_TIMER;
725 } else {
726 return EH_NOT_HANDLED;
727 }
728 }
729
730 return EH_HANDLED;
731}
732
733/**
734 * megasas_reset_device - Device reset handler entry point
735 */
736static int megasas_reset_device(struct scsi_cmnd *scmd)
737{
738 int ret;
739
740 /*
741 * First wait for all commands to complete
742 */
743 ret = megasas_generic_reset(scmd);
744
745 return ret;
746}
747
748/**
749 * megasas_reset_bus_host - Bus & host reset handler entry point
750 */
751static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
752{
753 int ret;
754
755 /*
756 * Frist wait for all commands to complete
757 */
758 ret = megasas_generic_reset(scmd);
759
760 return ret;
761}
762
763/**
764 * megasas_service_aen - Processes an event notification
765 * @instance: Adapter soft state
766 * @cmd: AEN command completed by the ISR
767 *
768 * For AEN, driver sends a command down to FW that is held by the FW till an
769 * event occurs. When an event of interest occurs, FW completes the command
770 * that it was previously holding.
771 *
772 * This routines sends SIGIO signal to processes that have registered with the
773 * driver for AEN.
774 */
775static void
776megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
777{
778 /*
779 * Don't signal app if it is just an aborted previously registered aen
780 */
781 if (!cmd->abort_aen)
782 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
783 else
784 cmd->abort_aen = 0;
785
786 instance->aen_cmd = NULL;
787 megasas_return_cmd(instance, cmd);
788}
789
790/*
791 * Scsi host template for megaraid_sas driver
792 */
793static struct scsi_host_template megasas_template = {
794
795 .module = THIS_MODULE,
796 .name = "LSI Logic SAS based MegaRAID driver",
797 .proc_name = "megaraid_sas",
798 .queuecommand = megasas_queue_command,
799 .eh_device_reset_handler = megasas_reset_device,
800 .eh_bus_reset_handler = megasas_reset_bus_host,
801 .eh_host_reset_handler = megasas_reset_bus_host,
802 .eh_timed_out = megasas_reset_timer,
803 .use_clustering = ENABLE_CLUSTERING,
804};
805
806/**
807 * megasas_complete_int_cmd - Completes an internal command
808 * @instance: Adapter soft state
809 * @cmd: Command to be completed
810 *
811 * The megasas_issue_blocked_cmd() function waits for a command to complete
812 * after it issues a command. This function wakes up that waiting routine by
813 * calling wake_up() on the wait queue.
814 */
815static void
816megasas_complete_int_cmd(struct megasas_instance *instance,
817 struct megasas_cmd *cmd)
818{
819 cmd->cmd_status = cmd->frame->io.cmd_status;
820
821 if (cmd->cmd_status == ENODATA) {
822 cmd->cmd_status = 0;
823 }
824 wake_up(&instance->int_cmd_wait_q);
825}
826
827/**
828 * megasas_complete_abort - Completes aborting a command
829 * @instance: Adapter soft state
830 * @cmd: Cmd that was issued to abort another cmd
831 *
832 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
833 * after it issues an abort on a previously issued command. This function
834 * wakes up all functions waiting on the same wait queue.
835 */
836static void
837megasas_complete_abort(struct megasas_instance *instance,
838 struct megasas_cmd *cmd)
839{
840 if (cmd->sync_cmd) {
841 cmd->sync_cmd = 0;
842 cmd->cmd_status = 0;
843 wake_up(&instance->abort_cmd_wait_q);
844 }
845
846 return;
847}
848
849/**
850 * megasas_unmap_sgbuf - Unmap SG buffers
851 * @instance: Adapter soft state
852 * @cmd: Completed command
853 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800854static void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400855megasas_unmap_sgbuf(struct megasas_instance *instance, struct megasas_cmd *cmd)
856{
857 dma_addr_t buf_h;
858 u8 opcode;
859
860 if (cmd->scmd->use_sg) {
861 pci_unmap_sg(instance->pdev, cmd->scmd->request_buffer,
862 cmd->scmd->use_sg, cmd->scmd->sc_data_direction);
863 return;
864 }
865
866 if (!cmd->scmd->request_bufflen)
867 return;
868
869 opcode = cmd->frame->hdr.cmd;
870
871 if ((opcode == MFI_CMD_LD_READ) || (opcode == MFI_CMD_LD_WRITE)) {
872 if (IS_DMA64)
873 buf_h = cmd->frame->io.sgl.sge64[0].phys_addr;
874 else
875 buf_h = cmd->frame->io.sgl.sge32[0].phys_addr;
876 } else {
877 if (IS_DMA64)
878 buf_h = cmd->frame->pthru.sgl.sge64[0].phys_addr;
879 else
880 buf_h = cmd->frame->pthru.sgl.sge32[0].phys_addr;
881 }
882
883 pci_unmap_single(instance->pdev, buf_h, cmd->scmd->request_bufflen,
884 cmd->scmd->sc_data_direction);
885 return;
886}
887
888/**
889 * megasas_complete_cmd - Completes a command
890 * @instance: Adapter soft state
891 * @cmd: Command to be completed
892 * @alt_status: If non-zero, use this value as status to
893 * SCSI mid-layer instead of the value returned
894 * by the FW. This should be used if caller wants
895 * an alternate status (as in the case of aborted
896 * commands)
897 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800898static void
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -0400899megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
900 u8 alt_status)
901{
902 int exception = 0;
903 struct megasas_header *hdr = &cmd->frame->hdr;
904 unsigned long flags;
905
906 if (cmd->scmd) {
907 cmd->scmd->SCp.ptr = (char *)0;
908 }
909
910 switch (hdr->cmd) {
911
912 case MFI_CMD_PD_SCSI_IO:
913 case MFI_CMD_LD_SCSI_IO:
914
915 /*
916 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
917 * issued either through an IO path or an IOCTL path. If it
918 * was via IOCTL, we will send it to internal completion.
919 */
920 if (cmd->sync_cmd) {
921 cmd->sync_cmd = 0;
922 megasas_complete_int_cmd(instance, cmd);
923 break;
924 }
925
926 /*
927 * Don't export physical disk devices to mid-layer.
928 */
929 if (!MEGASAS_IS_LOGICAL(cmd->scmd) &&
930 (hdr->cmd_status == MFI_STAT_OK) &&
931 (cmd->scmd->cmnd[0] == INQUIRY)) {
932
933 if (((*(u8 *) cmd->scmd->request_buffer) & 0x1F) ==
934 TYPE_DISK) {
935 cmd->scmd->result = DID_BAD_TARGET << 16;
936 exception = 1;
937 }
938 }
939
940 case MFI_CMD_LD_READ:
941 case MFI_CMD_LD_WRITE:
942
943 if (alt_status) {
944 cmd->scmd->result = alt_status << 16;
945 exception = 1;
946 }
947
948 if (exception) {
949
950 spin_lock_irqsave(&instance->instance_lock, flags);
951 instance->fw_outstanding--;
952 spin_unlock_irqrestore(&instance->instance_lock, flags);
953
954 megasas_unmap_sgbuf(instance, cmd);
955 cmd->scmd->scsi_done(cmd->scmd);
956 megasas_return_cmd(instance, cmd);
957
958 break;
959 }
960
961 switch (hdr->cmd_status) {
962
963 case MFI_STAT_OK:
964 cmd->scmd->result = DID_OK << 16;
965 break;
966
967 case MFI_STAT_SCSI_IO_FAILED:
968 case MFI_STAT_LD_INIT_IN_PROGRESS:
969 cmd->scmd->result =
970 (DID_ERROR << 16) | hdr->scsi_status;
971 break;
972
973 case MFI_STAT_SCSI_DONE_WITH_ERROR:
974
975 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
976
977 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
978 memset(cmd->scmd->sense_buffer, 0,
979 SCSI_SENSE_BUFFERSIZE);
980 memcpy(cmd->scmd->sense_buffer, cmd->sense,
981 hdr->sense_len);
982
983 cmd->scmd->result |= DRIVER_SENSE << 24;
984 }
985
986 break;
987
988 case MFI_STAT_LD_OFFLINE:
989 case MFI_STAT_DEVICE_NOT_FOUND:
990 cmd->scmd->result = DID_BAD_TARGET << 16;
991 break;
992
993 default:
994 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
995 hdr->cmd_status);
996 cmd->scmd->result = DID_ERROR << 16;
997 break;
998 }
999
1000 spin_lock_irqsave(&instance->instance_lock, flags);
1001 instance->fw_outstanding--;
1002 spin_unlock_irqrestore(&instance->instance_lock, flags);
1003
1004 megasas_unmap_sgbuf(instance, cmd);
1005 cmd->scmd->scsi_done(cmd->scmd);
1006 megasas_return_cmd(instance, cmd);
1007
1008 break;
1009
1010 case MFI_CMD_SMP:
1011 case MFI_CMD_STP:
1012 case MFI_CMD_DCMD:
1013
1014 /*
1015 * See if got an event notification
1016 */
1017 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1018 megasas_service_aen(instance, cmd);
1019 else
1020 megasas_complete_int_cmd(instance, cmd);
1021
1022 break;
1023
1024 case MFI_CMD_ABORT:
1025 /*
1026 * Cmd issued to abort another cmd returned
1027 */
1028 megasas_complete_abort(instance, cmd);
1029 break;
1030
1031 default:
1032 printk("megasas: Unknown command completed! [0x%X]\n",
1033 hdr->cmd);
1034 break;
1035 }
1036}
1037
1038/**
1039 * megasas_deplete_reply_queue - Processes all completed commands
1040 * @instance: Adapter soft state
1041 * @alt_status: Alternate status to be returned to
1042 * SCSI mid-layer instead of the status
1043 * returned by the FW
1044 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001045static int
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04001046megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1047{
1048 u32 status;
1049 u32 producer;
1050 u32 consumer;
1051 u32 context;
1052 struct megasas_cmd *cmd;
1053
1054 /*
1055 * Check if it is our interrupt
1056 */
1057 status = readl(&instance->reg_set->outbound_intr_status);
1058
1059 if (!(status & MFI_OB_INTR_STATUS_MASK)) {
1060 return IRQ_NONE;
1061 }
1062
1063 /*
1064 * Clear the interrupt by writing back the same value
1065 */
1066 writel(status, &instance->reg_set->outbound_intr_status);
1067
1068 producer = *instance->producer;
1069 consumer = *instance->consumer;
1070
1071 while (consumer != producer) {
1072 context = instance->reply_queue[consumer];
1073
1074 cmd = instance->cmd_list[context];
1075
1076 megasas_complete_cmd(instance, cmd, alt_status);
1077
1078 consumer++;
1079 if (consumer == (instance->max_fw_cmds + 1)) {
1080 consumer = 0;
1081 }
1082 }
1083
1084 *instance->consumer = producer;
1085
1086 return IRQ_HANDLED;
1087}
1088
1089/**
1090 * megasas_isr - isr entry point
1091 */
1092static irqreturn_t megasas_isr(int irq, void *devp, struct pt_regs *regs)
1093{
1094 return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1095 DID_OK);
1096}
1097
1098/**
1099 * megasas_transition_to_ready - Move the FW to READY state
1100 * @reg_set: MFI register set
1101 *
1102 * During the initialization, FW passes can potentially be in any one of
1103 * several possible states. If the FW in operational, waiting-for-handshake
1104 * states, driver must take steps to bring it to ready state. Otherwise, it
1105 * has to wait for the ready state.
1106 */
1107static int
1108megasas_transition_to_ready(struct megasas_register_set __iomem * reg_set)
1109{
1110 int i;
1111 u8 max_wait;
1112 u32 fw_state;
1113 u32 cur_state;
1114
1115 fw_state = readl(&reg_set->outbound_msg_0) & MFI_STATE_MASK;
1116
1117 while (fw_state != MFI_STATE_READY) {
1118
1119 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1120 " state\n");
1121 switch (fw_state) {
1122
1123 case MFI_STATE_FAULT:
1124
1125 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1126 return -ENODEV;
1127
1128 case MFI_STATE_WAIT_HANDSHAKE:
1129 /*
1130 * Set the CLR bit in inbound doorbell
1131 */
1132 writel(MFI_INIT_CLEAR_HANDSHAKE,
1133 &reg_set->inbound_doorbell);
1134
1135 max_wait = 2;
1136 cur_state = MFI_STATE_WAIT_HANDSHAKE;
1137 break;
1138
1139 case MFI_STATE_OPERATIONAL:
1140 /*
1141 * Bring it to READY state; assuming max wait 2 secs
1142 */
1143 megasas_disable_intr(reg_set);
1144 writel(MFI_INIT_READY, &reg_set->inbound_doorbell);
1145
1146 max_wait = 10;
1147 cur_state = MFI_STATE_OPERATIONAL;
1148 break;
1149
1150 case MFI_STATE_UNDEFINED:
1151 /*
1152 * This state should not last for more than 2 seconds
1153 */
1154 max_wait = 2;
1155 cur_state = MFI_STATE_UNDEFINED;
1156 break;
1157
1158 case MFI_STATE_BB_INIT:
1159 max_wait = 2;
1160 cur_state = MFI_STATE_BB_INIT;
1161 break;
1162
1163 case MFI_STATE_FW_INIT:
1164 max_wait = 20;
1165 cur_state = MFI_STATE_FW_INIT;
1166 break;
1167
1168 case MFI_STATE_FW_INIT_2:
1169 max_wait = 20;
1170 cur_state = MFI_STATE_FW_INIT_2;
1171 break;
1172
1173 case MFI_STATE_DEVICE_SCAN:
1174 max_wait = 20;
1175 cur_state = MFI_STATE_DEVICE_SCAN;
1176 break;
1177
1178 case MFI_STATE_FLUSH_CACHE:
1179 max_wait = 20;
1180 cur_state = MFI_STATE_FLUSH_CACHE;
1181 break;
1182
1183 default:
1184 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1185 fw_state);
1186 return -ENODEV;
1187 }
1188
1189 /*
1190 * The cur_state should not last for more than max_wait secs
1191 */
1192 for (i = 0; i < (max_wait * 1000); i++) {
1193 fw_state = MFI_STATE_MASK &
1194 readl(&reg_set->outbound_msg_0);
1195
1196 if (fw_state == cur_state) {
1197 msleep(1);
1198 } else
1199 break;
1200 }
1201
1202 /*
1203 * Return error if fw_state hasn't changed after max_wait
1204 */
1205 if (fw_state == cur_state) {
1206 printk(KERN_DEBUG "FW state [%d] hasn't changed "
1207 "in %d secs\n", fw_state, max_wait);
1208 return -ENODEV;
1209 }
1210 };
1211
1212 return 0;
1213}
1214
1215/**
1216 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool
1217 * @instance: Adapter soft state
1218 */
1219static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1220{
1221 int i;
1222 u32 max_cmd = instance->max_fw_cmds;
1223 struct megasas_cmd *cmd;
1224
1225 if (!instance->frame_dma_pool)
1226 return;
1227
1228 /*
1229 * Return all frames to pool
1230 */
1231 for (i = 0; i < max_cmd; i++) {
1232
1233 cmd = instance->cmd_list[i];
1234
1235 if (cmd->frame)
1236 pci_pool_free(instance->frame_dma_pool, cmd->frame,
1237 cmd->frame_phys_addr);
1238
1239 if (cmd->sense)
1240 pci_pool_free(instance->sense_dma_pool, cmd->frame,
1241 cmd->sense_phys_addr);
1242 }
1243
1244 /*
1245 * Now destroy the pool itself
1246 */
1247 pci_pool_destroy(instance->frame_dma_pool);
1248 pci_pool_destroy(instance->sense_dma_pool);
1249
1250 instance->frame_dma_pool = NULL;
1251 instance->sense_dma_pool = NULL;
1252}
1253
1254/**
1255 * megasas_create_frame_pool - Creates DMA pool for cmd frames
1256 * @instance: Adapter soft state
1257 *
1258 * Each command packet has an embedded DMA memory buffer that is used for
1259 * filling MFI frame and the SG list that immediately follows the frame. This
1260 * function creates those DMA memory buffers for each command packet by using
1261 * PCI pool facility.
1262 */
1263static int megasas_create_frame_pool(struct megasas_instance *instance)
1264{
1265 int i;
1266 u32 max_cmd;
1267 u32 sge_sz;
1268 u32 sgl_sz;
1269 u32 total_sz;
1270 u32 frame_count;
1271 struct megasas_cmd *cmd;
1272
1273 max_cmd = instance->max_fw_cmds;
1274
1275 /*
1276 * Size of our frame is 64 bytes for MFI frame, followed by max SG
1277 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1278 */
1279 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1280 sizeof(struct megasas_sge32);
1281
1282 /*
1283 * Calculated the number of 64byte frames required for SGL
1284 */
1285 sgl_sz = sge_sz * instance->max_num_sge;
1286 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1287
1288 /*
1289 * We need one extra frame for the MFI command
1290 */
1291 frame_count++;
1292
1293 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1294 /*
1295 * Use DMA pool facility provided by PCI layer
1296 */
1297 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1298 instance->pdev, total_sz, 64,
1299 0);
1300
1301 if (!instance->frame_dma_pool) {
1302 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1303 return -ENOMEM;
1304 }
1305
1306 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1307 instance->pdev, 128, 4, 0);
1308
1309 if (!instance->sense_dma_pool) {
1310 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1311
1312 pci_pool_destroy(instance->frame_dma_pool);
1313 instance->frame_dma_pool = NULL;
1314
1315 return -ENOMEM;
1316 }
1317
1318 /*
1319 * Allocate and attach a frame to each of the commands in cmd_list.
1320 * By making cmd->index as the context instead of the &cmd, we can
1321 * always use 32bit context regardless of the architecture
1322 */
1323 for (i = 0; i < max_cmd; i++) {
1324
1325 cmd = instance->cmd_list[i];
1326
1327 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1328 GFP_KERNEL, &cmd->frame_phys_addr);
1329
1330 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1331 GFP_KERNEL, &cmd->sense_phys_addr);
1332
1333 /*
1334 * megasas_teardown_frame_pool() takes care of freeing
1335 * whatever has been allocated
1336 */
1337 if (!cmd->frame || !cmd->sense) {
1338 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1339 megasas_teardown_frame_pool(instance);
1340 return -ENOMEM;
1341 }
1342
1343 cmd->frame->io.context = cmd->index;
1344 }
1345
1346 return 0;
1347}
1348
1349/**
1350 * megasas_free_cmds - Free all the cmds in the free cmd pool
1351 * @instance: Adapter soft state
1352 */
1353static void megasas_free_cmds(struct megasas_instance *instance)
1354{
1355 int i;
1356 /* First free the MFI frame pool */
1357 megasas_teardown_frame_pool(instance);
1358
1359 /* Free all the commands in the cmd_list */
1360 for (i = 0; i < instance->max_fw_cmds; i++)
1361 kfree(instance->cmd_list[i]);
1362
1363 /* Free the cmd_list buffer itself */
1364 kfree(instance->cmd_list);
1365 instance->cmd_list = NULL;
1366
1367 INIT_LIST_HEAD(&instance->cmd_pool);
1368}
1369
1370/**
1371 * megasas_alloc_cmds - Allocates the command packets
1372 * @instance: Adapter soft state
1373 *
1374 * Each command that is issued to the FW, whether IO commands from the OS or
1375 * internal commands like IOCTLs, are wrapped in local data structure called
1376 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1377 * the FW.
1378 *
1379 * Each frame has a 32-bit field called context (tag). This context is used
1380 * to get back the megasas_cmd from the frame when a frame gets completed in
1381 * the ISR. Typically the address of the megasas_cmd itself would be used as
1382 * the context. But we wanted to keep the differences between 32 and 64 bit
1383 * systems to the mininum. We always use 32 bit integers for the context. In
1384 * this driver, the 32 bit values are the indices into an array cmd_list.
1385 * This array is used only to look up the megasas_cmd given the context. The
1386 * free commands themselves are maintained in a linked list called cmd_pool.
1387 */
1388static int megasas_alloc_cmds(struct megasas_instance *instance)
1389{
1390 int i;
1391 int j;
1392 u32 max_cmd;
1393 struct megasas_cmd *cmd;
1394
1395 max_cmd = instance->max_fw_cmds;
1396
1397 /*
1398 * instance->cmd_list is an array of struct megasas_cmd pointers.
1399 * Allocate the dynamic array first and then allocate individual
1400 * commands.
1401 */
1402 instance->cmd_list = kmalloc(sizeof(struct megasas_cmd *) * max_cmd,
1403 GFP_KERNEL);
1404
1405 if (!instance->cmd_list) {
1406 printk(KERN_DEBUG "megasas: out of memory\n");
1407 return -ENOMEM;
1408 }
1409
1410 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) * max_cmd);
1411
1412 for (i = 0; i < max_cmd; i++) {
1413 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1414 GFP_KERNEL);
1415
1416 if (!instance->cmd_list[i]) {
1417
1418 for (j = 0; j < i; j++)
1419 kfree(instance->cmd_list[j]);
1420
1421 kfree(instance->cmd_list);
1422 instance->cmd_list = NULL;
1423
1424 return -ENOMEM;
1425 }
1426 }
1427
1428 /*
1429 * Add all the commands to command pool (instance->cmd_pool)
1430 */
1431 for (i = 0; i < max_cmd; i++) {
1432 cmd = instance->cmd_list[i];
1433 memset(cmd, 0, sizeof(struct megasas_cmd));
1434 cmd->index = i;
1435 cmd->instance = instance;
1436
1437 list_add_tail(&cmd->list, &instance->cmd_pool);
1438 }
1439
1440 /*
1441 * Create a frame pool and assign one frame to each cmd
1442 */
1443 if (megasas_create_frame_pool(instance)) {
1444 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1445 megasas_free_cmds(instance);
1446 }
1447
1448 return 0;
1449}
1450
1451/**
1452 * megasas_get_controller_info - Returns FW's controller structure
1453 * @instance: Adapter soft state
1454 * @ctrl_info: Controller information structure
1455 *
1456 * Issues an internal command (DCMD) to get the FW's controller structure.
1457 * This information is mainly used to find out the maximum IO transfer per
1458 * command supported by the FW.
1459 */
1460static int
1461megasas_get_ctrl_info(struct megasas_instance *instance,
1462 struct megasas_ctrl_info *ctrl_info)
1463{
1464 int ret = 0;
1465 struct megasas_cmd *cmd;
1466 struct megasas_dcmd_frame *dcmd;
1467 struct megasas_ctrl_info *ci;
1468 dma_addr_t ci_h = 0;
1469
1470 cmd = megasas_get_cmd(instance);
1471
1472 if (!cmd) {
1473 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1474 return -ENOMEM;
1475 }
1476
1477 dcmd = &cmd->frame->dcmd;
1478
1479 ci = pci_alloc_consistent(instance->pdev,
1480 sizeof(struct megasas_ctrl_info), &ci_h);
1481
1482 if (!ci) {
1483 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1484 megasas_return_cmd(instance, cmd);
1485 return -ENOMEM;
1486 }
1487
1488 memset(ci, 0, sizeof(*ci));
1489 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1490
1491 dcmd->cmd = MFI_CMD_DCMD;
1492 dcmd->cmd_status = 0xFF;
1493 dcmd->sge_count = 1;
1494 dcmd->flags = MFI_FRAME_DIR_READ;
1495 dcmd->timeout = 0;
1496 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1497 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1498 dcmd->sgl.sge32[0].phys_addr = ci_h;
1499 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1500
1501 if (!megasas_issue_polled(instance, cmd)) {
1502 ret = 0;
1503 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1504 } else {
1505 ret = -1;
1506 }
1507
1508 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1509 ci, ci_h);
1510
1511 megasas_return_cmd(instance, cmd);
1512 return ret;
1513}
1514
1515/**
1516 * megasas_init_mfi - Initializes the FW
1517 * @instance: Adapter soft state
1518 *
1519 * This is the main function for initializing MFI firmware.
1520 */
1521static int megasas_init_mfi(struct megasas_instance *instance)
1522{
1523 u32 context_sz;
1524 u32 reply_q_sz;
1525 u32 max_sectors_1;
1526 u32 max_sectors_2;
1527 struct megasas_register_set __iomem *reg_set;
1528
1529 struct megasas_cmd *cmd;
1530 struct megasas_ctrl_info *ctrl_info;
1531
1532 struct megasas_init_frame *init_frame;
1533 struct megasas_init_queue_info *initq_info;
1534 dma_addr_t init_frame_h;
1535 dma_addr_t initq_info_h;
1536
1537 /*
1538 * Map the message registers
1539 */
1540 instance->base_addr = pci_resource_start(instance->pdev, 0);
1541
1542 if (pci_request_regions(instance->pdev, "megasas: LSI Logic")) {
1543 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1544 return -EBUSY;
1545 }
1546
1547 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1548
1549 if (!instance->reg_set) {
1550 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1551 goto fail_ioremap;
1552 }
1553
1554 reg_set = instance->reg_set;
1555
1556 /*
1557 * We expect the FW state to be READY
1558 */
1559 if (megasas_transition_to_ready(instance->reg_set))
1560 goto fail_ready_state;
1561
1562 /*
1563 * Get various operational parameters from status register
1564 */
1565 instance->max_fw_cmds = readl(&reg_set->outbound_msg_0) & 0x00FFFF;
1566 instance->max_num_sge = (readl(&reg_set->outbound_msg_0) & 0xFF0000) >>
1567 0x10;
1568 /*
1569 * Create a pool of commands
1570 */
1571 if (megasas_alloc_cmds(instance))
1572 goto fail_alloc_cmds;
1573
1574 /*
1575 * Allocate memory for reply queue. Length of reply queue should
1576 * be _one_ more than the maximum commands handled by the firmware.
1577 *
1578 * Note: When FW completes commands, it places corresponding contex
1579 * values in this circular reply queue. This circular queue is a fairly
1580 * typical producer-consumer queue. FW is the producer (of completed
1581 * commands) and the driver is the consumer.
1582 */
1583 context_sz = sizeof(u32);
1584 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
1585
1586 instance->reply_queue = pci_alloc_consistent(instance->pdev,
1587 reply_q_sz,
1588 &instance->reply_queue_h);
1589
1590 if (!instance->reply_queue) {
1591 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
1592 goto fail_reply_queue;
1593 }
1594
1595 /*
1596 * Prepare a init frame. Note the init frame points to queue info
1597 * structure. Each frame has SGL allocated after first 64 bytes. For
1598 * this frame - since we don't need any SGL - we use SGL's space as
1599 * queue info structure
1600 *
1601 * We will not get a NULL command below. We just created the pool.
1602 */
1603 cmd = megasas_get_cmd(instance);
1604
1605 init_frame = (struct megasas_init_frame *)cmd->frame;
1606 initq_info = (struct megasas_init_queue_info *)
1607 ((unsigned long)init_frame + 64);
1608
1609 init_frame_h = cmd->frame_phys_addr;
1610 initq_info_h = init_frame_h + 64;
1611
1612 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1613 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1614
1615 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1616 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1617
1618 initq_info->producer_index_phys_addr_lo = instance->producer_h;
1619 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1620
1621 init_frame->cmd = MFI_CMD_INIT;
1622 init_frame->cmd_status = 0xFF;
1623 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1624
1625 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1626
1627 /*
1628 * Issue the init frame in polled mode
1629 */
1630 if (megasas_issue_polled(instance, cmd)) {
1631 printk(KERN_DEBUG "megasas: Failed to init firmware\n");
1632 goto fail_fw_init;
1633 }
1634
1635 megasas_return_cmd(instance, cmd);
1636
1637 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
1638
1639 /*
1640 * Compute the max allowed sectors per IO: The controller info has two
1641 * limits on max sectors. Driver should use the minimum of these two.
1642 *
1643 * 1 << stripe_sz_ops.min = max sectors per strip
1644 *
1645 * Note that older firmwares ( < FW ver 30) didn't report information
1646 * to calculate max_sectors_1. So the number ended up as zero always.
1647 */
1648 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
1649
1650 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
1651 ctrl_info->max_strips_per_io;
1652 max_sectors_2 = ctrl_info->max_request_size;
1653
1654 instance->max_sectors_per_req = (max_sectors_1 < max_sectors_2)
1655 ? max_sectors_1 : max_sectors_2;
1656 } else
1657 instance->max_sectors_per_req = instance->max_num_sge *
1658 PAGE_SIZE / 512;
1659
1660 kfree(ctrl_info);
1661
1662 return 0;
1663
1664 fail_fw_init:
1665 megasas_return_cmd(instance, cmd);
1666
1667 pci_free_consistent(instance->pdev, reply_q_sz,
1668 instance->reply_queue, instance->reply_queue_h);
1669 fail_reply_queue:
1670 megasas_free_cmds(instance);
1671
1672 fail_alloc_cmds:
1673 fail_ready_state:
1674 iounmap(instance->reg_set);
1675
1676 fail_ioremap:
1677 pci_release_regions(instance->pdev);
1678
1679 return -EINVAL;
1680}
1681
1682/**
1683 * megasas_release_mfi - Reverses the FW initialization
1684 * @intance: Adapter soft state
1685 */
1686static void megasas_release_mfi(struct megasas_instance *instance)
1687{
1688 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
1689
1690 pci_free_consistent(instance->pdev, reply_q_sz,
1691 instance->reply_queue, instance->reply_queue_h);
1692
1693 megasas_free_cmds(instance);
1694
1695 iounmap(instance->reg_set);
1696
1697 pci_release_regions(instance->pdev);
1698}
1699
1700/**
1701 * megasas_get_seq_num - Gets latest event sequence numbers
1702 * @instance: Adapter soft state
1703 * @eli: FW event log sequence numbers information
1704 *
1705 * FW maintains a log of all events in a non-volatile area. Upper layers would
1706 * usually find out the latest sequence number of the events, the seq number at
1707 * the boot etc. They would "read" all the events below the latest seq number
1708 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
1709 * number), they would subsribe to AEN (asynchronous event notification) and
1710 * wait for the events to happen.
1711 */
1712static int
1713megasas_get_seq_num(struct megasas_instance *instance,
1714 struct megasas_evt_log_info *eli)
1715{
1716 struct megasas_cmd *cmd;
1717 struct megasas_dcmd_frame *dcmd;
1718 struct megasas_evt_log_info *el_info;
1719 dma_addr_t el_info_h = 0;
1720
1721 cmd = megasas_get_cmd(instance);
1722
1723 if (!cmd) {
1724 return -ENOMEM;
1725 }
1726
1727 dcmd = &cmd->frame->dcmd;
1728 el_info = pci_alloc_consistent(instance->pdev,
1729 sizeof(struct megasas_evt_log_info),
1730 &el_info_h);
1731
1732 if (!el_info) {
1733 megasas_return_cmd(instance, cmd);
1734 return -ENOMEM;
1735 }
1736
1737 memset(el_info, 0, sizeof(*el_info));
1738 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1739
1740 dcmd->cmd = MFI_CMD_DCMD;
1741 dcmd->cmd_status = 0x0;
1742 dcmd->sge_count = 1;
1743 dcmd->flags = MFI_FRAME_DIR_READ;
1744 dcmd->timeout = 0;
1745 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
1746 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
1747 dcmd->sgl.sge32[0].phys_addr = el_info_h;
1748 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
1749
1750 megasas_issue_blocked_cmd(instance, cmd);
1751
1752 /*
1753 * Copy the data back into callers buffer
1754 */
1755 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
1756
1757 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
1758 el_info, el_info_h);
1759
1760 megasas_return_cmd(instance, cmd);
1761
1762 return 0;
1763}
1764
1765/**
1766 * megasas_register_aen - Registers for asynchronous event notification
1767 * @instance: Adapter soft state
1768 * @seq_num: The starting sequence number
1769 * @class_locale: Class of the event
1770 *
1771 * This function subscribes for AEN for events beyond the @seq_num. It requests
1772 * to be notified if and only if the event is of type @class_locale
1773 */
1774static int
1775megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
1776 u32 class_locale_word)
1777{
1778 int ret_val;
1779 struct megasas_cmd *cmd;
1780 struct megasas_dcmd_frame *dcmd;
1781 union megasas_evt_class_locale curr_aen;
1782 union megasas_evt_class_locale prev_aen;
1783
1784 /*
1785 * If there an AEN pending already (aen_cmd), check if the
1786 * class_locale of that pending AEN is inclusive of the new
1787 * AEN request we currently have. If it is, then we don't have
1788 * to do anything. In other words, whichever events the current
1789 * AEN request is subscribing to, have already been subscribed
1790 * to.
1791 *
1792 * If the old_cmd is _not_ inclusive, then we have to abort
1793 * that command, form a class_locale that is superset of both
1794 * old and current and re-issue to the FW
1795 */
1796
1797 curr_aen.word = class_locale_word;
1798
1799 if (instance->aen_cmd) {
1800
1801 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
1802
1803 /*
1804 * A class whose enum value is smaller is inclusive of all
1805 * higher values. If a PROGRESS (= -1) was previously
1806 * registered, then a new registration requests for higher
1807 * classes need not be sent to FW. They are automatically
1808 * included.
1809 *
1810 * Locale numbers don't have such hierarchy. They are bitmap
1811 * values
1812 */
1813 if ((prev_aen.members.class <= curr_aen.members.class) &&
1814 !((prev_aen.members.locale & curr_aen.members.locale) ^
1815 curr_aen.members.locale)) {
1816 /*
1817 * Previously issued event registration includes
1818 * current request. Nothing to do.
1819 */
1820 return 0;
1821 } else {
1822 curr_aen.members.locale |= prev_aen.members.locale;
1823
1824 if (prev_aen.members.class < curr_aen.members.class)
1825 curr_aen.members.class = prev_aen.members.class;
1826
1827 instance->aen_cmd->abort_aen = 1;
1828 ret_val = megasas_issue_blocked_abort_cmd(instance,
1829 instance->
1830 aen_cmd);
1831
1832 if (ret_val) {
1833 printk(KERN_DEBUG "megasas: Failed to abort "
1834 "previous AEN command\n");
1835 return ret_val;
1836 }
1837 }
1838 }
1839
1840 cmd = megasas_get_cmd(instance);
1841
1842 if (!cmd)
1843 return -ENOMEM;
1844
1845 dcmd = &cmd->frame->dcmd;
1846
1847 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
1848
1849 /*
1850 * Prepare DCMD for aen registration
1851 */
1852 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1853
1854 dcmd->cmd = MFI_CMD_DCMD;
1855 dcmd->cmd_status = 0x0;
1856 dcmd->sge_count = 1;
1857 dcmd->flags = MFI_FRAME_DIR_READ;
1858 dcmd->timeout = 0;
1859 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
1860 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
1861 dcmd->mbox.w[0] = seq_num;
1862 dcmd->mbox.w[1] = curr_aen.word;
1863 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
1864 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
1865
1866 /*
1867 * Store reference to the cmd used to register for AEN. When an
1868 * application wants us to register for AEN, we have to abort this
1869 * cmd and re-register with a new EVENT LOCALE supplied by that app
1870 */
1871 instance->aen_cmd = cmd;
1872
1873 /*
1874 * Issue the aen registration frame
1875 */
1876 writel(cmd->frame_phys_addr >> 3,
1877 &instance->reg_set->inbound_queue_port);
1878
1879 return 0;
1880}
1881
1882/**
1883 * megasas_start_aen - Subscribes to AEN during driver load time
1884 * @instance: Adapter soft state
1885 */
1886static int megasas_start_aen(struct megasas_instance *instance)
1887{
1888 struct megasas_evt_log_info eli;
1889 union megasas_evt_class_locale class_locale;
1890
1891 /*
1892 * Get the latest sequence number from FW
1893 */
1894 memset(&eli, 0, sizeof(eli));
1895
1896 if (megasas_get_seq_num(instance, &eli))
1897 return -1;
1898
1899 /*
1900 * Register AEN with FW for latest sequence number plus 1
1901 */
1902 class_locale.members.reserved = 0;
1903 class_locale.members.locale = MR_EVT_LOCALE_ALL;
1904 class_locale.members.class = MR_EVT_CLASS_DEBUG;
1905
1906 return megasas_register_aen(instance, eli.newest_seq_num + 1,
1907 class_locale.word);
1908}
1909
1910/**
1911 * megasas_io_attach - Attaches this driver to SCSI mid-layer
1912 * @instance: Adapter soft state
1913 */
1914static int megasas_io_attach(struct megasas_instance *instance)
1915{
1916 struct Scsi_Host *host = instance->host;
1917
1918 /*
1919 * Export parameters required by SCSI mid-layer
1920 */
1921 host->irq = instance->pdev->irq;
1922 host->unique_id = instance->unique_id;
1923 host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
1924 host->this_id = instance->init_id;
1925 host->sg_tablesize = instance->max_num_sge;
1926 host->max_sectors = instance->max_sectors_per_req;
1927 host->cmd_per_lun = 128;
1928 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
1929 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
1930 host->max_lun = MEGASAS_MAX_LUN;
1931
1932 /*
1933 * Notify the mid-layer about the new controller
1934 */
1935 if (scsi_add_host(host, &instance->pdev->dev)) {
1936 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
1937 return -ENODEV;
1938 }
1939
1940 /*
1941 * Trigger SCSI to scan our drives
1942 */
1943 scsi_scan_host(host);
1944 return 0;
1945}
1946
1947/**
1948 * megasas_probe_one - PCI hotplug entry point
1949 * @pdev: PCI device structure
1950 * @id: PCI ids of supported hotplugged adapter
1951 */
1952static int __devinit
1953megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
1954{
1955 int rval;
1956 struct Scsi_Host *host;
1957 struct megasas_instance *instance;
1958
1959 /*
1960 * Announce PCI information
1961 */
1962 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
1963 pdev->vendor, pdev->device, pdev->subsystem_vendor,
1964 pdev->subsystem_device);
1965
1966 printk("bus %d:slot %d:func %d\n",
1967 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
1968
1969 /*
1970 * PCI prepping: enable device set bus mastering and dma mask
1971 */
1972 rval = pci_enable_device(pdev);
1973
1974 if (rval) {
1975 return rval;
1976 }
1977
1978 pci_set_master(pdev);
1979
1980 /*
1981 * All our contollers are capable of performing 64-bit DMA
1982 */
1983 if (IS_DMA64) {
1984 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
1985
1986 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
1987 goto fail_set_dma_mask;
1988 }
1989 } else {
1990 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
1991 goto fail_set_dma_mask;
1992 }
1993
1994 host = scsi_host_alloc(&megasas_template,
1995 sizeof(struct megasas_instance));
1996
1997 if (!host) {
1998 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
1999 goto fail_alloc_instance;
2000 }
2001
2002 instance = (struct megasas_instance *)host->hostdata;
2003 memset(instance, 0, sizeof(*instance));
2004
2005 instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2006 &instance->producer_h);
2007 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2008 &instance->consumer_h);
2009
2010 if (!instance->producer || !instance->consumer) {
2011 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2012 "producer, consumer\n");
2013 goto fail_alloc_dma_buf;
2014 }
2015
2016 *instance->producer = 0;
2017 *instance->consumer = 0;
2018
2019 instance->evt_detail = pci_alloc_consistent(pdev,
2020 sizeof(struct
2021 megasas_evt_detail),
2022 &instance->evt_detail_h);
2023
2024 if (!instance->evt_detail) {
2025 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2026 "event detail structure\n");
2027 goto fail_alloc_dma_buf;
2028 }
2029
2030 /*
2031 * Initialize locks and queues
2032 */
2033 INIT_LIST_HEAD(&instance->cmd_pool);
2034
2035 init_waitqueue_head(&instance->int_cmd_wait_q);
2036 init_waitqueue_head(&instance->abort_cmd_wait_q);
2037
2038 spin_lock_init(&instance->cmd_pool_lock);
2039 spin_lock_init(&instance->instance_lock);
2040
2041 sema_init(&instance->aen_mutex, 1);
2042 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2043
2044 /*
2045 * Initialize PCI related and misc parameters
2046 */
2047 instance->pdev = pdev;
2048 instance->host = host;
2049 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2050 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2051
2052 /*
2053 * Initialize MFI Firmware
2054 */
2055 if (megasas_init_mfi(instance))
2056 goto fail_init_mfi;
2057
2058 /*
2059 * Register IRQ
2060 */
2061 if (request_irq(pdev->irq, megasas_isr, SA_SHIRQ, "megasas", instance)) {
2062 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2063 goto fail_irq;
2064 }
2065
2066 megasas_enable_intr(instance->reg_set);
2067
2068 /*
2069 * Store instance in PCI softstate
2070 */
2071 pci_set_drvdata(pdev, instance);
2072
2073 /*
2074 * Add this controller to megasas_mgmt_info structure so that it
2075 * can be exported to management applications
2076 */
2077 megasas_mgmt_info.count++;
2078 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2079 megasas_mgmt_info.max_index++;
2080
2081 /*
2082 * Initiate AEN (Asynchronous Event Notification)
2083 */
2084 if (megasas_start_aen(instance)) {
2085 printk(KERN_DEBUG "megasas: start aen failed\n");
2086 goto fail_start_aen;
2087 }
2088
2089 /*
2090 * Register with SCSI mid-layer
2091 */
2092 if (megasas_io_attach(instance))
2093 goto fail_io_attach;
2094
2095 return 0;
2096
2097 fail_start_aen:
2098 fail_io_attach:
2099 megasas_mgmt_info.count--;
2100 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2101 megasas_mgmt_info.max_index--;
2102
2103 pci_set_drvdata(pdev, NULL);
2104 megasas_disable_intr(instance->reg_set);
2105 free_irq(instance->pdev->irq, instance);
2106
2107 megasas_release_mfi(instance);
2108
2109 fail_irq:
2110 fail_init_mfi:
2111 fail_alloc_dma_buf:
2112 if (instance->evt_detail)
2113 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2114 instance->evt_detail,
2115 instance->evt_detail_h);
2116
2117 if (instance->producer)
2118 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2119 instance->producer_h);
2120 if (instance->consumer)
2121 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2122 instance->consumer_h);
2123 scsi_host_put(host);
2124
2125 fail_alloc_instance:
2126 fail_set_dma_mask:
2127 pci_disable_device(pdev);
2128
2129 return -ENODEV;
2130}
2131
2132/**
2133 * megasas_flush_cache - Requests FW to flush all its caches
2134 * @instance: Adapter soft state
2135 */
2136static void megasas_flush_cache(struct megasas_instance *instance)
2137{
2138 struct megasas_cmd *cmd;
2139 struct megasas_dcmd_frame *dcmd;
2140
2141 cmd = megasas_get_cmd(instance);
2142
2143 if (!cmd)
2144 return;
2145
2146 dcmd = &cmd->frame->dcmd;
2147
2148 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2149
2150 dcmd->cmd = MFI_CMD_DCMD;
2151 dcmd->cmd_status = 0x0;
2152 dcmd->sge_count = 0;
2153 dcmd->flags = MFI_FRAME_DIR_NONE;
2154 dcmd->timeout = 0;
2155 dcmd->data_xfer_len = 0;
2156 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2157 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2158
2159 megasas_issue_blocked_cmd(instance, cmd);
2160
2161 megasas_return_cmd(instance, cmd);
2162
2163 return;
2164}
2165
2166/**
2167 * megasas_shutdown_controller - Instructs FW to shutdown the controller
2168 * @instance: Adapter soft state
2169 */
2170static void megasas_shutdown_controller(struct megasas_instance *instance)
2171{
2172 struct megasas_cmd *cmd;
2173 struct megasas_dcmd_frame *dcmd;
2174
2175 cmd = megasas_get_cmd(instance);
2176
2177 if (!cmd)
2178 return;
2179
2180 if (instance->aen_cmd)
2181 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2182
2183 dcmd = &cmd->frame->dcmd;
2184
2185 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2186
2187 dcmd->cmd = MFI_CMD_DCMD;
2188 dcmd->cmd_status = 0x0;
2189 dcmd->sge_count = 0;
2190 dcmd->flags = MFI_FRAME_DIR_NONE;
2191 dcmd->timeout = 0;
2192 dcmd->data_xfer_len = 0;
2193 dcmd->opcode = MR_DCMD_CTRL_SHUTDOWN;
2194
2195 megasas_issue_blocked_cmd(instance, cmd);
2196
2197 megasas_return_cmd(instance, cmd);
2198
2199 return;
2200}
2201
2202/**
2203 * megasas_detach_one - PCI hot"un"plug entry point
2204 * @pdev: PCI device structure
2205 */
2206static void megasas_detach_one(struct pci_dev *pdev)
2207{
2208 int i;
2209 struct Scsi_Host *host;
2210 struct megasas_instance *instance;
2211
2212 instance = pci_get_drvdata(pdev);
2213 host = instance->host;
2214
2215 scsi_remove_host(instance->host);
2216 megasas_flush_cache(instance);
2217 megasas_shutdown_controller(instance);
2218
2219 /*
2220 * Take the instance off the instance array. Note that we will not
2221 * decrement the max_index. We let this array be sparse array
2222 */
2223 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2224 if (megasas_mgmt_info.instance[i] == instance) {
2225 megasas_mgmt_info.count--;
2226 megasas_mgmt_info.instance[i] = NULL;
2227
2228 break;
2229 }
2230 }
2231
2232 pci_set_drvdata(instance->pdev, NULL);
2233
2234 megasas_disable_intr(instance->reg_set);
2235
2236 free_irq(instance->pdev->irq, instance);
2237
2238 megasas_release_mfi(instance);
2239
2240 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2241 instance->evt_detail, instance->evt_detail_h);
2242
2243 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2244 instance->producer_h);
2245
2246 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2247 instance->consumer_h);
2248
2249 scsi_host_put(host);
2250
2251 pci_set_drvdata(pdev, NULL);
2252
2253 pci_disable_device(pdev);
2254
2255 return;
2256}
2257
2258/**
2259 * megasas_shutdown - Shutdown entry point
2260 * @device: Generic device structure
2261 */
2262static void megasas_shutdown(struct pci_dev *pdev)
2263{
2264 struct megasas_instance *instance = pci_get_drvdata(pdev);
2265 megasas_flush_cache(instance);
2266}
2267
2268/**
2269 * megasas_mgmt_open - char node "open" entry point
2270 */
2271static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2272{
2273 /*
2274 * Allow only those users with admin rights
2275 */
2276 if (!capable(CAP_SYS_ADMIN))
2277 return -EACCES;
2278
2279 return 0;
2280}
2281
2282/**
2283 * megasas_mgmt_release - char node "release" entry point
2284 */
2285static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2286{
2287 filep->private_data = NULL;
2288 fasync_helper(-1, filep, 0, &megasas_async_queue);
2289
2290 return 0;
2291}
2292
2293/**
2294 * megasas_mgmt_fasync - Async notifier registration from applications
2295 *
2296 * This function adds the calling process to a driver global queue. When an
2297 * event occurs, SIGIO will be sent to all processes in this queue.
2298 */
2299static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2300{
2301 int rc;
2302
Arjan van de Ven0b950672006-01-11 13:16:10 +01002303 mutex_lock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002304
2305 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2306
Arjan van de Ven0b950672006-01-11 13:16:10 +01002307 mutex_unlock(&megasas_async_queue_mutex);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002308
2309 if (rc >= 0) {
2310 /* For sanity check when we get ioctl */
2311 filep->private_data = filep;
2312 return 0;
2313 }
2314
2315 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2316
2317 return rc;
2318}
2319
2320/**
2321 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW
2322 * @instance: Adapter soft state
2323 * @argp: User's ioctl packet
2324 */
2325static int
2326megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2327 struct megasas_iocpacket __user * user_ioc,
2328 struct megasas_iocpacket *ioc)
2329{
2330 struct megasas_sge32 *kern_sge32;
2331 struct megasas_cmd *cmd;
2332 void *kbuff_arr[MAX_IOCTL_SGE];
2333 dma_addr_t buf_handle = 0;
2334 int error = 0, i;
2335 void *sense = NULL;
2336 dma_addr_t sense_handle;
2337 u32 *sense_ptr;
2338
2339 memset(kbuff_arr, 0, sizeof(kbuff_arr));
2340
2341 if (ioc->sge_count > MAX_IOCTL_SGE) {
2342 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
2343 ioc->sge_count, MAX_IOCTL_SGE);
2344 return -EINVAL;
2345 }
2346
2347 cmd = megasas_get_cmd(instance);
2348 if (!cmd) {
2349 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2350 return -ENOMEM;
2351 }
2352
2353 /*
2354 * User's IOCTL packet has 2 frames (maximum). Copy those two
2355 * frames into our cmd's frames. cmd->frame's context will get
2356 * overwritten when we copy from user's frames. So set that value
2357 * alone separately
2358 */
2359 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2360 cmd->frame->hdr.context = cmd->index;
2361
2362 /*
2363 * The management interface between applications and the fw uses
2364 * MFI frames. E.g, RAID configuration changes, LD property changes
2365 * etc are accomplishes through different kinds of MFI frames. The
2366 * driver needs to care only about substituting user buffers with
2367 * kernel buffers in SGLs. The location of SGL is embedded in the
2368 * struct iocpacket itself.
2369 */
2370 kern_sge32 = (struct megasas_sge32 *)
2371 ((unsigned long)cmd->frame + ioc->sgl_off);
2372
2373 /*
2374 * For each user buffer, create a mirror buffer and copy in
2375 */
2376 for (i = 0; i < ioc->sge_count; i++) {
2377 kbuff_arr[i] = pci_alloc_consistent(instance->pdev,
2378 ioc->sgl[i].iov_len,
2379 &buf_handle);
2380 if (!kbuff_arr[i]) {
2381 printk(KERN_DEBUG "megasas: Failed to alloc "
2382 "kernel SGL buffer for IOCTL \n");
2383 error = -ENOMEM;
2384 goto out;
2385 }
2386
2387 /*
2388 * We don't change the dma_coherent_mask, so
2389 * pci_alloc_consistent only returns 32bit addresses
2390 */
2391 kern_sge32[i].phys_addr = (u32) buf_handle;
2392 kern_sge32[i].length = ioc->sgl[i].iov_len;
2393
2394 /*
2395 * We created a kernel buffer corresponding to the
2396 * user buffer. Now copy in from the user buffer
2397 */
2398 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2399 (u32) (ioc->sgl[i].iov_len))) {
2400 error = -EFAULT;
2401 goto out;
2402 }
2403 }
2404
2405 if (ioc->sense_len) {
2406 sense = pci_alloc_consistent(instance->pdev, ioc->sense_len,
2407 &sense_handle);
2408 if (!sense) {
2409 error = -ENOMEM;
2410 goto out;
2411 }
2412
2413 sense_ptr =
2414 (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
2415 *sense_ptr = sense_handle;
2416 }
2417
2418 /*
2419 * Set the sync_cmd flag so that the ISR knows not to complete this
2420 * cmd to the SCSI mid-layer
2421 */
2422 cmd->sync_cmd = 1;
2423 megasas_issue_blocked_cmd(instance, cmd);
2424 cmd->sync_cmd = 0;
2425
2426 /*
2427 * copy out the kernel buffers to user buffers
2428 */
2429 for (i = 0; i < ioc->sge_count; i++) {
2430 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
2431 ioc->sgl[i].iov_len)) {
2432 error = -EFAULT;
2433 goto out;
2434 }
2435 }
2436
2437 /*
2438 * copy out the sense
2439 */
2440 if (ioc->sense_len) {
2441 /*
2442 * sense_ptr points to the location that has the user
2443 * sense buffer address
2444 */
2445 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
2446 ioc->sense_off);
2447
2448 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
2449 sense, ioc->sense_len)) {
2450 error = -EFAULT;
2451 goto out;
2452 }
2453 }
2454
2455 /*
2456 * copy the status codes returned by the fw
2457 */
2458 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
2459 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
2460 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
2461 error = -EFAULT;
2462 }
2463
2464 out:
2465 if (sense) {
2466 pci_free_consistent(instance->pdev, ioc->sense_len,
2467 sense, sense_handle);
2468 }
2469
2470 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
2471 pci_free_consistent(instance->pdev,
2472 kern_sge32[i].length,
2473 kbuff_arr[i], kern_sge32[i].phys_addr);
2474 }
2475
2476 megasas_return_cmd(instance, cmd);
2477 return error;
2478}
2479
2480static struct megasas_instance *megasas_lookup_instance(u16 host_no)
2481{
2482 int i;
2483
2484 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2485
2486 if ((megasas_mgmt_info.instance[i]) &&
2487 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
2488 return megasas_mgmt_info.instance[i];
2489 }
2490
2491 return NULL;
2492}
2493
2494static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
2495{
2496 struct megasas_iocpacket __user *user_ioc =
2497 (struct megasas_iocpacket __user *)arg;
2498 struct megasas_iocpacket *ioc;
2499 struct megasas_instance *instance;
2500 int error;
2501
2502 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
2503 if (!ioc)
2504 return -ENOMEM;
2505
2506 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
2507 error = -EFAULT;
2508 goto out_kfree_ioc;
2509 }
2510
2511 instance = megasas_lookup_instance(ioc->host_no);
2512 if (!instance) {
2513 error = -ENODEV;
2514 goto out_kfree_ioc;
2515 }
2516
2517 /*
2518 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
2519 */
2520 if (down_interruptible(&instance->ioctl_sem)) {
2521 error = -ERESTARTSYS;
2522 goto out_kfree_ioc;
2523 }
2524 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
2525 up(&instance->ioctl_sem);
2526
2527 out_kfree_ioc:
2528 kfree(ioc);
2529 return error;
2530}
2531
2532static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
2533{
2534 struct megasas_instance *instance;
2535 struct megasas_aen aen;
2536 int error;
2537
2538 if (file->private_data != file) {
2539 printk(KERN_DEBUG "megasas: fasync_helper was not "
2540 "called first\n");
2541 return -EINVAL;
2542 }
2543
2544 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
2545 return -EFAULT;
2546
2547 instance = megasas_lookup_instance(aen.host_no);
2548
2549 if (!instance)
2550 return -ENODEV;
2551
2552 down(&instance->aen_mutex);
2553 error = megasas_register_aen(instance, aen.seq_num,
2554 aen.class_locale_word);
2555 up(&instance->aen_mutex);
2556 return error;
2557}
2558
2559/**
2560 * megasas_mgmt_ioctl - char node ioctl entry point
2561 */
2562static long
2563megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2564{
2565 switch (cmd) {
2566 case MEGASAS_IOC_FIRMWARE:
2567 return megasas_mgmt_ioctl_fw(file, arg);
2568
2569 case MEGASAS_IOC_GET_AEN:
2570 return megasas_mgmt_ioctl_aen(file, arg);
2571 }
2572
2573 return -ENOTTY;
2574}
2575
2576#ifdef CONFIG_COMPAT
2577static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
2578{
2579 struct compat_megasas_iocpacket __user *cioc =
2580 (struct compat_megasas_iocpacket __user *)arg;
2581 struct megasas_iocpacket __user *ioc =
2582 compat_alloc_user_space(sizeof(struct megasas_iocpacket));
2583 int i;
2584 int error = 0;
2585
2586 clear_user(ioc, sizeof(*ioc));
2587
2588 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
2589 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
2590 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
2591 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
2592 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
2593 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
2594 return -EFAULT;
2595
2596 for (i = 0; i < MAX_IOCTL_SGE; i++) {
2597 compat_uptr_t ptr;
2598
2599 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
2600 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
2601 copy_in_user(&ioc->sgl[i].iov_len,
2602 &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
2603 return -EFAULT;
2604 }
2605
2606 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
2607
2608 if (copy_in_user(&cioc->frame.hdr.cmd_status,
2609 &ioc->frame.hdr.cmd_status, sizeof(u8))) {
2610 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
2611 return -EFAULT;
2612 }
2613 return error;
2614}
2615
2616static long
2617megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
2618 unsigned long arg)
2619{
2620 switch (cmd) {
Sumant Patrocb59aa62006-01-25 11:53:25 -08002621 case MEGASAS_IOC_FIRMWARE32:
2622 return megasas_mgmt_compat_ioctl_fw(file, arg);
Bagalkote, Sreenivasc4a3e0a2005-09-20 17:46:58 -04002623 case MEGASAS_IOC_GET_AEN:
2624 return megasas_mgmt_ioctl_aen(file, arg);
2625 }
2626
2627 return -ENOTTY;
2628}
2629#endif
2630
2631/*
2632 * File operations structure for management interface
2633 */
2634static struct file_operations megasas_mgmt_fops = {
2635 .owner = THIS_MODULE,
2636 .open = megasas_mgmt_open,
2637 .release = megasas_mgmt_release,
2638 .fasync = megasas_mgmt_fasync,
2639 .unlocked_ioctl = megasas_mgmt_ioctl,
2640#ifdef CONFIG_COMPAT
2641 .compat_ioctl = megasas_mgmt_compat_ioctl,
2642#endif
2643};
2644
2645/*
2646 * PCI hotplug support registration structure
2647 */
2648static struct pci_driver megasas_pci_driver = {
2649
2650 .name = "megaraid_sas",
2651 .id_table = megasas_pci_table,
2652 .probe = megasas_probe_one,
2653 .remove = __devexit_p(megasas_detach_one),
2654 .shutdown = megasas_shutdown,
2655};
2656
2657/*
2658 * Sysfs driver attributes
2659 */
2660static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
2661{
2662 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
2663 MEGASAS_VERSION);
2664}
2665
2666static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
2667
2668static ssize_t
2669megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
2670{
2671 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
2672 MEGASAS_RELDATE);
2673}
2674
2675static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
2676 NULL);
2677
2678/**
2679 * megasas_init - Driver load entry point
2680 */
2681static int __init megasas_init(void)
2682{
2683 int rval;
2684
2685 /*
2686 * Announce driver version and other information
2687 */
2688 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
2689 MEGASAS_EXT_VERSION);
2690
2691 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
2692
2693 /*
2694 * Register character device node
2695 */
2696 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
2697
2698 if (rval < 0) {
2699 printk(KERN_DEBUG "megasas: failed to open device node\n");
2700 return rval;
2701 }
2702
2703 megasas_mgmt_majorno = rval;
2704
2705 /*
2706 * Register ourselves as PCI hotplug module
2707 */
2708 rval = pci_module_init(&megasas_pci_driver);
2709
2710 if (rval) {
2711 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
2712 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
2713 }
2714
2715 driver_create_file(&megasas_pci_driver.driver, &driver_attr_version);
2716 driver_create_file(&megasas_pci_driver.driver,
2717 &driver_attr_release_date);
2718
2719 return rval;
2720}
2721
2722/**
2723 * megasas_exit - Driver unload entry point
2724 */
2725static void __exit megasas_exit(void)
2726{
2727 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
2728 driver_remove_file(&megasas_pci_driver.driver,
2729 &driver_attr_release_date);
2730
2731 pci_unregister_driver(&megasas_pci_driver);
2732 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
2733}
2734
2735module_init(megasas_init);
2736module_exit(megasas_exit);