[ACPI] merge 3549 4320 4485 4588 4980 5483 5651 acpica asus fops pnpacpi branches...
[linux-2.6] / drivers / scsi / megaraid / megaraid_sas.c
1 /*
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
13  * Version      : v00.00.02.00-rc4
14  *
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>
29 #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>
36 #include <linux/fs.h>
37 #include <linux/compat.h>
38 #include <linux/mutex.h>
39
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
46 MODULE_LICENSE("GPL");
47 MODULE_VERSION(MEGASAS_VERSION);
48 MODULE_AUTHOR("sreenivas.bagalkote@lsil.com");
49 MODULE_DESCRIPTION("LSI Logic MegaRAID SAS Driver");
50
51 /*
52  * PCI ID table for all supported controllers
53  */
54 static 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
71 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
72
73 static int megasas_mgmt_majorno;
74 static struct megasas_mgmt_info megasas_mgmt_info;
75 static struct fasync_struct *megasas_async_queue;
76 static DEFINE_MUTEX(megasas_async_queue_mutex);
77
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  */
84 static struct megasas_cmd *megasas_get_cmd(struct megasas_instance
85                                                   *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  */
109 static inline void
110 megasas_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  */
126 static inline void
127 megasas_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  */
139 static inline void
140 megasas_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  */
156 static int
157 megasas_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  */
195 static int
196 megasas_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  */
218 static int
219 megasas_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  */
266 static int
267 megasas_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  */
314 static int
315 megasas_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  */
363 static int
364 megasas_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  */
444 static int
445 megasas_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 /**
561  * megasas_build_cmd -  Prepares a command packet
562  * @instance:           Adapter soft state
563  * @scp:                SCSI command
564  * @frame_count:        [OUT] Number of frames used to prepare this command
565  */
566 static struct megasas_cmd *megasas_build_cmd(struct megasas_instance
567                                                     *instance,
568                                                     struct scsi_cmnd *scp,
569                                                     int *frame_count)
570 {
571         u32 logical_cmd;
572         struct megasas_cmd *cmd;
573
574         /*
575          * Find out if this is logical or physical drive command.
576          */
577         logical_cmd = MEGASAS_IS_LOGICAL(scp);
578
579         /*
580          * Logical drive command
581          */
582         if (logical_cmd) {
583
584                 if (scp->device->id >= MEGASAS_MAX_LD) {
585                         scp->result = DID_BAD_TARGET << 16;
586                         return NULL;
587                 }
588
589                 switch (scp->cmnd[0]) {
590
591                 case READ_10:
592                 case WRITE_10:
593                 case READ_12:
594                 case WRITE_12:
595                 case READ_6:
596                 case WRITE_6:
597                 case READ_16:
598                 case WRITE_16:
599                         /*
600                          * Fail for LUN > 0
601                          */
602                         if (scp->device->lun) {
603                                 scp->result = DID_BAD_TARGET << 16;
604                                 return NULL;
605                         }
606
607                         cmd = megasas_get_cmd(instance);
608
609                         if (!cmd) {
610                                 scp->result = DID_IMM_RETRY << 16;
611                                 return NULL;
612                         }
613
614                         *frame_count = megasas_build_ldio(instance, scp, cmd);
615
616                         if (!(*frame_count)) {
617                                 megasas_return_cmd(instance, cmd);
618                                 return NULL;
619                         }
620
621                         return cmd;
622
623                 default:
624                         /*
625                          * Fail for LUN > 0
626                          */
627                         if (scp->device->lun) {
628                                 scp->result = DID_BAD_TARGET << 16;
629                                 return NULL;
630                         }
631
632                         cmd = megasas_get_cmd(instance);
633
634                         if (!cmd) {
635                                 scp->result = DID_IMM_RETRY << 16;
636                                 return NULL;
637                         }
638
639                         *frame_count = megasas_build_dcdb(instance, scp, cmd);
640
641                         if (!(*frame_count)) {
642                                 megasas_return_cmd(instance, cmd);
643                                 return NULL;
644                         }
645
646                         return cmd;
647                 }
648         } else {
649                 cmd = megasas_get_cmd(instance);
650
651                 if (!cmd) {
652                         scp->result = DID_IMM_RETRY << 16;
653                         return NULL;
654                 }
655
656                 *frame_count = megasas_build_dcdb(instance, scp, cmd);
657
658                 if (!(*frame_count)) {
659                         megasas_return_cmd(instance, cmd);
660                         return NULL;
661                 }
662
663                 return cmd;
664         }
665
666         return NULL;
667 }
668
669 /**
670  * megasas_queue_command -      Queue entry point
671  * @scmd:                       SCSI command to be queued
672  * @done:                       Callback entry point
673  */
674 static int
675 megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
676 {
677         u32 frame_count;
678         unsigned long flags;
679         struct megasas_cmd *cmd;
680         struct megasas_instance *instance;
681
682         instance = (struct megasas_instance *)
683             scmd->device->host->hostdata;
684         scmd->scsi_done = done;
685         scmd->result = 0;
686
687         cmd = megasas_build_cmd(instance, scmd, &frame_count);
688
689         if (!cmd) {
690                 done(scmd);
691                 return 0;
692         }
693
694         cmd->scmd = scmd;
695         scmd->SCp.ptr = (char *)cmd;
696         scmd->SCp.sent_command = jiffies;
697
698         /*
699          * Issue the command to the FW
700          */
701         spin_lock_irqsave(&instance->instance_lock, flags);
702         instance->fw_outstanding++;
703         spin_unlock_irqrestore(&instance->instance_lock, flags);
704
705         writel(((cmd->frame_phys_addr >> 3) | (cmd->frame_count - 1)),
706                &instance->reg_set->inbound_queue_port);
707
708         return 0;
709 }
710
711 /**
712  * megasas_wait_for_outstanding -       Wait for all outstanding cmds
713  * @instance:                           Adapter soft state
714  *
715  * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to
716  * complete all its outstanding commands. Returns error if one or more IOs
717  * are pending after this time period. It also marks the controller dead.
718  */
719 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
720 {
721         int i;
722         u32 wait_time = MEGASAS_RESET_WAIT_TIME;
723
724         for (i = 0; i < wait_time; i++) {
725
726                 if (!instance->fw_outstanding)
727                         break;
728
729                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
730                         printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
731                                "commands to complete\n", i,
732                                instance->fw_outstanding);
733                 }
734
735                 msleep(1000);
736         }
737
738         if (instance->fw_outstanding) {
739                 instance->hw_crit_error = 1;
740                 return FAILED;
741         }
742
743         return SUCCESS;
744 }
745
746 /**
747  * megasas_generic_reset -      Generic reset routine
748  * @scmd:                       Mid-layer SCSI command
749  *
750  * This routine implements a generic reset handler for device, bus and host
751  * reset requests. Device, bus and host specific reset handlers can use this
752  * function after they do their specific tasks.
753  */
754 static int megasas_generic_reset(struct scsi_cmnd *scmd)
755 {
756         int ret_val;
757         struct megasas_instance *instance;
758
759         instance = (struct megasas_instance *)scmd->device->host->hostdata;
760
761         scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x\n",
762                scmd->serial_number, scmd->cmnd[0]);
763
764         if (instance->hw_crit_error) {
765                 printk(KERN_ERR "megasas: cannot recover from previous reset "
766                        "failures\n");
767                 return FAILED;
768         }
769
770         ret_val = megasas_wait_for_outstanding(instance);
771         if (ret_val == SUCCESS)
772                 printk(KERN_NOTICE "megasas: reset successful \n");
773         else
774                 printk(KERN_ERR "megasas: failed to do reset\n");
775
776         return ret_val;
777 }
778
779 static enum scsi_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
780 {
781         unsigned long seconds;
782
783         if (scmd->SCp.ptr) {
784                 seconds = (jiffies - scmd->SCp.sent_command) / HZ;
785
786                 if (seconds < 90) {
787                         return EH_RESET_TIMER;
788                 } else {
789                         return EH_NOT_HANDLED;
790                 }
791         }
792
793         return EH_HANDLED;
794 }
795
796 /**
797  * megasas_reset_device -       Device reset handler entry point
798  */
799 static int megasas_reset_device(struct scsi_cmnd *scmd)
800 {
801         int ret;
802
803         /*
804          * First wait for all commands to complete
805          */
806         ret = megasas_generic_reset(scmd);
807
808         return ret;
809 }
810
811 /**
812  * megasas_reset_bus_host -     Bus & host reset handler entry point
813  */
814 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
815 {
816         int ret;
817
818         /*
819          * Frist wait for all commands to complete
820          */
821         ret = megasas_generic_reset(scmd);
822
823         return ret;
824 }
825
826 /**
827  * megasas_service_aen -        Processes an event notification
828  * @instance:                   Adapter soft state
829  * @cmd:                        AEN command completed by the ISR
830  *
831  * For AEN, driver sends a command down to FW that is held by the FW till an
832  * event occurs. When an event of interest occurs, FW completes the command
833  * that it was previously holding.
834  *
835  * This routines sends SIGIO signal to processes that have registered with the
836  * driver for AEN.
837  */
838 static void
839 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
840 {
841         /*
842          * Don't signal app if it is just an aborted previously registered aen
843          */
844         if (!cmd->abort_aen)
845                 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
846         else
847                 cmd->abort_aen = 0;
848
849         instance->aen_cmd = NULL;
850         megasas_return_cmd(instance, cmd);
851 }
852
853 /*
854  * Scsi host template for megaraid_sas driver
855  */
856 static struct scsi_host_template megasas_template = {
857
858         .module = THIS_MODULE,
859         .name = "LSI Logic SAS based MegaRAID driver",
860         .proc_name = "megaraid_sas",
861         .queuecommand = megasas_queue_command,
862         .eh_device_reset_handler = megasas_reset_device,
863         .eh_bus_reset_handler = megasas_reset_bus_host,
864         .eh_host_reset_handler = megasas_reset_bus_host,
865         .eh_timed_out = megasas_reset_timer,
866         .use_clustering = ENABLE_CLUSTERING,
867 };
868
869 /**
870  * megasas_complete_int_cmd -   Completes an internal command
871  * @instance:                   Adapter soft state
872  * @cmd:                        Command to be completed
873  *
874  * The megasas_issue_blocked_cmd() function waits for a command to complete
875  * after it issues a command. This function wakes up that waiting routine by
876  * calling wake_up() on the wait queue.
877  */
878 static void
879 megasas_complete_int_cmd(struct megasas_instance *instance,
880                          struct megasas_cmd *cmd)
881 {
882         cmd->cmd_status = cmd->frame->io.cmd_status;
883
884         if (cmd->cmd_status == ENODATA) {
885                 cmd->cmd_status = 0;
886         }
887         wake_up(&instance->int_cmd_wait_q);
888 }
889
890 /**
891  * megasas_complete_abort -     Completes aborting a command
892  * @instance:                   Adapter soft state
893  * @cmd:                        Cmd that was issued to abort another cmd
894  *
895  * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q 
896  * after it issues an abort on a previously issued command. This function 
897  * wakes up all functions waiting on the same wait queue.
898  */
899 static void
900 megasas_complete_abort(struct megasas_instance *instance,
901                        struct megasas_cmd *cmd)
902 {
903         if (cmd->sync_cmd) {
904                 cmd->sync_cmd = 0;
905                 cmd->cmd_status = 0;
906                 wake_up(&instance->abort_cmd_wait_q);
907         }
908
909         return;
910 }
911
912 /**
913  * megasas_unmap_sgbuf -        Unmap SG buffers
914  * @instance:                   Adapter soft state
915  * @cmd:                        Completed command
916  */
917 static void
918 megasas_unmap_sgbuf(struct megasas_instance *instance, struct megasas_cmd *cmd)
919 {
920         dma_addr_t buf_h;
921         u8 opcode;
922
923         if (cmd->scmd->use_sg) {
924                 pci_unmap_sg(instance->pdev, cmd->scmd->request_buffer,
925                              cmd->scmd->use_sg, cmd->scmd->sc_data_direction);
926                 return;
927         }
928
929         if (!cmd->scmd->request_bufflen)
930                 return;
931
932         opcode = cmd->frame->hdr.cmd;
933
934         if ((opcode == MFI_CMD_LD_READ) || (opcode == MFI_CMD_LD_WRITE)) {
935                 if (IS_DMA64)
936                         buf_h = cmd->frame->io.sgl.sge64[0].phys_addr;
937                 else
938                         buf_h = cmd->frame->io.sgl.sge32[0].phys_addr;
939         } else {
940                 if (IS_DMA64)
941                         buf_h = cmd->frame->pthru.sgl.sge64[0].phys_addr;
942                 else
943                         buf_h = cmd->frame->pthru.sgl.sge32[0].phys_addr;
944         }
945
946         pci_unmap_single(instance->pdev, buf_h, cmd->scmd->request_bufflen,
947                          cmd->scmd->sc_data_direction);
948         return;
949 }
950
951 /**
952  * megasas_complete_cmd -       Completes a command
953  * @instance:                   Adapter soft state
954  * @cmd:                        Command to be completed
955  * @alt_status:                 If non-zero, use this value as status to 
956  *                              SCSI mid-layer instead of the value returned
957  *                              by the FW. This should be used if caller wants
958  *                              an alternate status (as in the case of aborted
959  *                              commands)
960  */
961 static void
962 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
963                      u8 alt_status)
964 {
965         int exception = 0;
966         struct megasas_header *hdr = &cmd->frame->hdr;
967         unsigned long flags;
968
969         if (cmd->scmd) {
970                 cmd->scmd->SCp.ptr = (char *)0;
971         }
972
973         switch (hdr->cmd) {
974
975         case MFI_CMD_PD_SCSI_IO:
976         case MFI_CMD_LD_SCSI_IO:
977
978                 /*
979                  * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
980                  * issued either through an IO path or an IOCTL path. If it
981                  * was via IOCTL, we will send it to internal completion.
982                  */
983                 if (cmd->sync_cmd) {
984                         cmd->sync_cmd = 0;
985                         megasas_complete_int_cmd(instance, cmd);
986                         break;
987                 }
988
989                 /*
990                  * Don't export physical disk devices to mid-layer.
991                  */
992                 if (!MEGASAS_IS_LOGICAL(cmd->scmd) &&
993                     (hdr->cmd_status == MFI_STAT_OK) &&
994                     (cmd->scmd->cmnd[0] == INQUIRY)) {
995
996                         if (((*(u8 *) cmd->scmd->request_buffer) & 0x1F) ==
997                             TYPE_DISK) {
998                                 cmd->scmd->result = DID_BAD_TARGET << 16;
999                                 exception = 1;
1000                         }
1001                 }
1002
1003         case MFI_CMD_LD_READ:
1004         case MFI_CMD_LD_WRITE:
1005
1006                 if (alt_status) {
1007                         cmd->scmd->result = alt_status << 16;
1008                         exception = 1;
1009                 }
1010
1011                 if (exception) {
1012
1013                         spin_lock_irqsave(&instance->instance_lock, flags);
1014                         instance->fw_outstanding--;
1015                         spin_unlock_irqrestore(&instance->instance_lock, flags);
1016
1017                         megasas_unmap_sgbuf(instance, cmd);
1018                         cmd->scmd->scsi_done(cmd->scmd);
1019                         megasas_return_cmd(instance, cmd);
1020
1021                         break;
1022                 }
1023
1024                 switch (hdr->cmd_status) {
1025
1026                 case MFI_STAT_OK:
1027                         cmd->scmd->result = DID_OK << 16;
1028                         break;
1029
1030                 case MFI_STAT_SCSI_IO_FAILED:
1031                 case MFI_STAT_LD_INIT_IN_PROGRESS:
1032                         cmd->scmd->result =
1033                             (DID_ERROR << 16) | hdr->scsi_status;
1034                         break;
1035
1036                 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1037
1038                         cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
1039
1040                         if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
1041                                 memset(cmd->scmd->sense_buffer, 0,
1042                                        SCSI_SENSE_BUFFERSIZE);
1043                                 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1044                                        hdr->sense_len);
1045
1046                                 cmd->scmd->result |= DRIVER_SENSE << 24;
1047                         }
1048
1049                         break;
1050
1051                 case MFI_STAT_LD_OFFLINE:
1052                 case MFI_STAT_DEVICE_NOT_FOUND:
1053                         cmd->scmd->result = DID_BAD_TARGET << 16;
1054                         break;
1055
1056                 default:
1057                         printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
1058                                hdr->cmd_status);
1059                         cmd->scmd->result = DID_ERROR << 16;
1060                         break;
1061                 }
1062
1063                 spin_lock_irqsave(&instance->instance_lock, flags);
1064                 instance->fw_outstanding--;
1065                 spin_unlock_irqrestore(&instance->instance_lock, flags);
1066
1067                 megasas_unmap_sgbuf(instance, cmd);
1068                 cmd->scmd->scsi_done(cmd->scmd);
1069                 megasas_return_cmd(instance, cmd);
1070
1071                 break;
1072
1073         case MFI_CMD_SMP:
1074         case MFI_CMD_STP:
1075         case MFI_CMD_DCMD:
1076
1077                 /*
1078                  * See if got an event notification
1079                  */
1080                 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
1081                         megasas_service_aen(instance, cmd);
1082                 else
1083                         megasas_complete_int_cmd(instance, cmd);
1084
1085                 break;
1086
1087         case MFI_CMD_ABORT:
1088                 /*
1089                  * Cmd issued to abort another cmd returned
1090                  */
1091                 megasas_complete_abort(instance, cmd);
1092                 break;
1093
1094         default:
1095                 printk("megasas: Unknown command completed! [0x%X]\n",
1096                        hdr->cmd);
1097                 break;
1098         }
1099 }
1100
1101 /**
1102  * megasas_deplete_reply_queue -        Processes all completed commands
1103  * @instance:                           Adapter soft state
1104  * @alt_status:                         Alternate status to be returned to
1105  *                                      SCSI mid-layer instead of the status
1106  *                                      returned by the FW
1107  */
1108 static int
1109 megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status)
1110 {
1111         u32 status;
1112         u32 producer;
1113         u32 consumer;
1114         u32 context;
1115         struct megasas_cmd *cmd;
1116
1117         /*
1118          * Check if it is our interrupt
1119          */
1120         status = readl(&instance->reg_set->outbound_intr_status);
1121
1122         if (!(status & MFI_OB_INTR_STATUS_MASK)) {
1123                 return IRQ_NONE;
1124         }
1125
1126         /*
1127          * Clear the interrupt by writing back the same value
1128          */
1129         writel(status, &instance->reg_set->outbound_intr_status);
1130
1131         producer = *instance->producer;
1132         consumer = *instance->consumer;
1133
1134         while (consumer != producer) {
1135                 context = instance->reply_queue[consumer];
1136
1137                 cmd = instance->cmd_list[context];
1138
1139                 megasas_complete_cmd(instance, cmd, alt_status);
1140
1141                 consumer++;
1142                 if (consumer == (instance->max_fw_cmds + 1)) {
1143                         consumer = 0;
1144                 }
1145         }
1146
1147         *instance->consumer = producer;
1148
1149         return IRQ_HANDLED;
1150 }
1151
1152 /**
1153  * megasas_isr - isr entry point
1154  */
1155 static irqreturn_t megasas_isr(int irq, void *devp, struct pt_regs *regs)
1156 {
1157         return megasas_deplete_reply_queue((struct megasas_instance *)devp,
1158                                            DID_OK);
1159 }
1160
1161 /**
1162  * megasas_transition_to_ready -        Move the FW to READY state
1163  * @reg_set:                            MFI register set
1164  *
1165  * During the initialization, FW passes can potentially be in any one of
1166  * several possible states. If the FW in operational, waiting-for-handshake
1167  * states, driver must take steps to bring it to ready state. Otherwise, it
1168  * has to wait for the ready state.
1169  */
1170 static int
1171 megasas_transition_to_ready(struct megasas_register_set __iomem * reg_set)
1172 {
1173         int i;
1174         u8 max_wait;
1175         u32 fw_state;
1176         u32 cur_state;
1177
1178         fw_state = readl(&reg_set->outbound_msg_0) & MFI_STATE_MASK;
1179
1180         while (fw_state != MFI_STATE_READY) {
1181
1182                 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
1183                        " state\n");
1184                 switch (fw_state) {
1185
1186                 case MFI_STATE_FAULT:
1187
1188                         printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
1189                         return -ENODEV;
1190
1191                 case MFI_STATE_WAIT_HANDSHAKE:
1192                         /*
1193                          * Set the CLR bit in inbound doorbell
1194                          */
1195                         writel(MFI_INIT_CLEAR_HANDSHAKE,
1196                                &reg_set->inbound_doorbell);
1197
1198                         max_wait = 2;
1199                         cur_state = MFI_STATE_WAIT_HANDSHAKE;
1200                         break;
1201
1202                 case MFI_STATE_OPERATIONAL:
1203                         /*
1204                          * Bring it to READY state; assuming max wait 2 secs
1205                          */
1206                         megasas_disable_intr(reg_set);
1207                         writel(MFI_INIT_READY, &reg_set->inbound_doorbell);
1208
1209                         max_wait = 10;
1210                         cur_state = MFI_STATE_OPERATIONAL;
1211                         break;
1212
1213                 case MFI_STATE_UNDEFINED:
1214                         /*
1215                          * This state should not last for more than 2 seconds
1216                          */
1217                         max_wait = 2;
1218                         cur_state = MFI_STATE_UNDEFINED;
1219                         break;
1220
1221                 case MFI_STATE_BB_INIT:
1222                         max_wait = 2;
1223                         cur_state = MFI_STATE_BB_INIT;
1224                         break;
1225
1226                 case MFI_STATE_FW_INIT:
1227                         max_wait = 20;
1228                         cur_state = MFI_STATE_FW_INIT;
1229                         break;
1230
1231                 case MFI_STATE_FW_INIT_2:
1232                         max_wait = 20;
1233                         cur_state = MFI_STATE_FW_INIT_2;
1234                         break;
1235
1236                 case MFI_STATE_DEVICE_SCAN:
1237                         max_wait = 20;
1238                         cur_state = MFI_STATE_DEVICE_SCAN;
1239                         break;
1240
1241                 case MFI_STATE_FLUSH_CACHE:
1242                         max_wait = 20;
1243                         cur_state = MFI_STATE_FLUSH_CACHE;
1244                         break;
1245
1246                 default:
1247                         printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
1248                                fw_state);
1249                         return -ENODEV;
1250                 }
1251
1252                 /*
1253                  * The cur_state should not last for more than max_wait secs
1254                  */
1255                 for (i = 0; i < (max_wait * 1000); i++) {
1256                         fw_state = MFI_STATE_MASK &
1257                             readl(&reg_set->outbound_msg_0);
1258
1259                         if (fw_state == cur_state) {
1260                                 msleep(1);
1261                         } else
1262                                 break;
1263                 }
1264
1265                 /*
1266                  * Return error if fw_state hasn't changed after max_wait
1267                  */
1268                 if (fw_state == cur_state) {
1269                         printk(KERN_DEBUG "FW state [%d] hasn't changed "
1270                                "in %d secs\n", fw_state, max_wait);
1271                         return -ENODEV;
1272                 }
1273         };
1274
1275         return 0;
1276 }
1277
1278 /**
1279  * megasas_teardown_frame_pool -        Destroy the cmd frame DMA pool
1280  * @instance:                           Adapter soft state
1281  */
1282 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
1283 {
1284         int i;
1285         u32 max_cmd = instance->max_fw_cmds;
1286         struct megasas_cmd *cmd;
1287
1288         if (!instance->frame_dma_pool)
1289                 return;
1290
1291         /*
1292          * Return all frames to pool
1293          */
1294         for (i = 0; i < max_cmd; i++) {
1295
1296                 cmd = instance->cmd_list[i];
1297
1298                 if (cmd->frame)
1299                         pci_pool_free(instance->frame_dma_pool, cmd->frame,
1300                                       cmd->frame_phys_addr);
1301
1302                 if (cmd->sense)
1303                         pci_pool_free(instance->sense_dma_pool, cmd->frame,
1304                                       cmd->sense_phys_addr);
1305         }
1306
1307         /*
1308          * Now destroy the pool itself
1309          */
1310         pci_pool_destroy(instance->frame_dma_pool);
1311         pci_pool_destroy(instance->sense_dma_pool);
1312
1313         instance->frame_dma_pool = NULL;
1314         instance->sense_dma_pool = NULL;
1315 }
1316
1317 /**
1318  * megasas_create_frame_pool -  Creates DMA pool for cmd frames
1319  * @instance:                   Adapter soft state
1320  *
1321  * Each command packet has an embedded DMA memory buffer that is used for
1322  * filling MFI frame and the SG list that immediately follows the frame. This
1323  * function creates those DMA memory buffers for each command packet by using
1324  * PCI pool facility.
1325  */
1326 static int megasas_create_frame_pool(struct megasas_instance *instance)
1327 {
1328         int i;
1329         u32 max_cmd;
1330         u32 sge_sz;
1331         u32 sgl_sz;
1332         u32 total_sz;
1333         u32 frame_count;
1334         struct megasas_cmd *cmd;
1335
1336         max_cmd = instance->max_fw_cmds;
1337
1338         /*
1339          * Size of our frame is 64 bytes for MFI frame, followed by max SG
1340          * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
1341          */
1342         sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1343             sizeof(struct megasas_sge32);
1344
1345         /*
1346          * Calculated the number of 64byte frames required for SGL
1347          */
1348         sgl_sz = sge_sz * instance->max_num_sge;
1349         frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
1350
1351         /*
1352          * We need one extra frame for the MFI command
1353          */
1354         frame_count++;
1355
1356         total_sz = MEGAMFI_FRAME_SIZE * frame_count;
1357         /*
1358          * Use DMA pool facility provided by PCI layer
1359          */
1360         instance->frame_dma_pool = pci_pool_create("megasas frame pool",
1361                                                    instance->pdev, total_sz, 64,
1362                                                    0);
1363
1364         if (!instance->frame_dma_pool) {
1365                 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
1366                 return -ENOMEM;
1367         }
1368
1369         instance->sense_dma_pool = pci_pool_create("megasas sense pool",
1370                                                    instance->pdev, 128, 4, 0);
1371
1372         if (!instance->sense_dma_pool) {
1373                 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
1374
1375                 pci_pool_destroy(instance->frame_dma_pool);
1376                 instance->frame_dma_pool = NULL;
1377
1378                 return -ENOMEM;
1379         }
1380
1381         /*
1382          * Allocate and attach a frame to each of the commands in cmd_list.
1383          * By making cmd->index as the context instead of the &cmd, we can
1384          * always use 32bit context regardless of the architecture
1385          */
1386         for (i = 0; i < max_cmd; i++) {
1387
1388                 cmd = instance->cmd_list[i];
1389
1390                 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
1391                                             GFP_KERNEL, &cmd->frame_phys_addr);
1392
1393                 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
1394                                             GFP_KERNEL, &cmd->sense_phys_addr);
1395
1396                 /*
1397                  * megasas_teardown_frame_pool() takes care of freeing
1398                  * whatever has been allocated
1399                  */
1400                 if (!cmd->frame || !cmd->sense) {
1401                         printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
1402                         megasas_teardown_frame_pool(instance);
1403                         return -ENOMEM;
1404                 }
1405
1406                 cmd->frame->io.context = cmd->index;
1407         }
1408
1409         return 0;
1410 }
1411
1412 /**
1413  * megasas_free_cmds -  Free all the cmds in the free cmd pool
1414  * @instance:           Adapter soft state
1415  */
1416 static void megasas_free_cmds(struct megasas_instance *instance)
1417 {
1418         int i;
1419         /* First free the MFI frame pool */
1420         megasas_teardown_frame_pool(instance);
1421
1422         /* Free all the commands in the cmd_list */
1423         for (i = 0; i < instance->max_fw_cmds; i++)
1424                 kfree(instance->cmd_list[i]);
1425
1426         /* Free the cmd_list buffer itself */
1427         kfree(instance->cmd_list);
1428         instance->cmd_list = NULL;
1429
1430         INIT_LIST_HEAD(&instance->cmd_pool);
1431 }
1432
1433 /**
1434  * megasas_alloc_cmds - Allocates the command packets
1435  * @instance:           Adapter soft state
1436  *
1437  * Each command that is issued to the FW, whether IO commands from the OS or
1438  * internal commands like IOCTLs, are wrapped in local data structure called
1439  * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
1440  * the FW.
1441  *
1442  * Each frame has a 32-bit field called context (tag). This context is used
1443  * to get back the megasas_cmd from the frame when a frame gets completed in
1444  * the ISR. Typically the address of the megasas_cmd itself would be used as
1445  * the context. But we wanted to keep the differences between 32 and 64 bit
1446  * systems to the mininum. We always use 32 bit integers for the context. In
1447  * this driver, the 32 bit values are the indices into an array cmd_list.
1448  * This array is used only to look up the megasas_cmd given the context. The
1449  * free commands themselves are maintained in a linked list called cmd_pool.
1450  */
1451 static int megasas_alloc_cmds(struct megasas_instance *instance)
1452 {
1453         int i;
1454         int j;
1455         u32 max_cmd;
1456         struct megasas_cmd *cmd;
1457
1458         max_cmd = instance->max_fw_cmds;
1459
1460         /*
1461          * instance->cmd_list is an array of struct megasas_cmd pointers.
1462          * Allocate the dynamic array first and then allocate individual
1463          * commands.
1464          */
1465         instance->cmd_list = kmalloc(sizeof(struct megasas_cmd *) * max_cmd,
1466                                      GFP_KERNEL);
1467
1468         if (!instance->cmd_list) {
1469                 printk(KERN_DEBUG "megasas: out of memory\n");
1470                 return -ENOMEM;
1471         }
1472
1473         memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) * max_cmd);
1474
1475         for (i = 0; i < max_cmd; i++) {
1476                 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
1477                                                 GFP_KERNEL);
1478
1479                 if (!instance->cmd_list[i]) {
1480
1481                         for (j = 0; j < i; j++)
1482                                 kfree(instance->cmd_list[j]);
1483
1484                         kfree(instance->cmd_list);
1485                         instance->cmd_list = NULL;
1486
1487                         return -ENOMEM;
1488                 }
1489         }
1490
1491         /*
1492          * Add all the commands to command pool (instance->cmd_pool)
1493          */
1494         for (i = 0; i < max_cmd; i++) {
1495                 cmd = instance->cmd_list[i];
1496                 memset(cmd, 0, sizeof(struct megasas_cmd));
1497                 cmd->index = i;
1498                 cmd->instance = instance;
1499
1500                 list_add_tail(&cmd->list, &instance->cmd_pool);
1501         }
1502
1503         /*
1504          * Create a frame pool and assign one frame to each cmd
1505          */
1506         if (megasas_create_frame_pool(instance)) {
1507                 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
1508                 megasas_free_cmds(instance);
1509         }
1510
1511         return 0;
1512 }
1513
1514 /**
1515  * megasas_get_controller_info -        Returns FW's controller structure
1516  * @instance:                           Adapter soft state
1517  * @ctrl_info:                          Controller information structure
1518  *
1519  * Issues an internal command (DCMD) to get the FW's controller structure.
1520  * This information is mainly used to find out the maximum IO transfer per
1521  * command supported by the FW.
1522  */
1523 static int
1524 megasas_get_ctrl_info(struct megasas_instance *instance,
1525                       struct megasas_ctrl_info *ctrl_info)
1526 {
1527         int ret = 0;
1528         struct megasas_cmd *cmd;
1529         struct megasas_dcmd_frame *dcmd;
1530         struct megasas_ctrl_info *ci;
1531         dma_addr_t ci_h = 0;
1532
1533         cmd = megasas_get_cmd(instance);
1534
1535         if (!cmd) {
1536                 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
1537                 return -ENOMEM;
1538         }
1539
1540         dcmd = &cmd->frame->dcmd;
1541
1542         ci = pci_alloc_consistent(instance->pdev,
1543                                   sizeof(struct megasas_ctrl_info), &ci_h);
1544
1545         if (!ci) {
1546                 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
1547                 megasas_return_cmd(instance, cmd);
1548                 return -ENOMEM;
1549         }
1550
1551         memset(ci, 0, sizeof(*ci));
1552         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1553
1554         dcmd->cmd = MFI_CMD_DCMD;
1555         dcmd->cmd_status = 0xFF;
1556         dcmd->sge_count = 1;
1557         dcmd->flags = MFI_FRAME_DIR_READ;
1558         dcmd->timeout = 0;
1559         dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
1560         dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
1561         dcmd->sgl.sge32[0].phys_addr = ci_h;
1562         dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
1563
1564         if (!megasas_issue_polled(instance, cmd)) {
1565                 ret = 0;
1566                 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
1567         } else {
1568                 ret = -1;
1569         }
1570
1571         pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
1572                             ci, ci_h);
1573
1574         megasas_return_cmd(instance, cmd);
1575         return ret;
1576 }
1577
1578 /**
1579  * megasas_init_mfi -   Initializes the FW
1580  * @instance:           Adapter soft state
1581  *
1582  * This is the main function for initializing MFI firmware.
1583  */
1584 static int megasas_init_mfi(struct megasas_instance *instance)
1585 {
1586         u32 context_sz;
1587         u32 reply_q_sz;
1588         u32 max_sectors_1;
1589         u32 max_sectors_2;
1590         struct megasas_register_set __iomem *reg_set;
1591
1592         struct megasas_cmd *cmd;
1593         struct megasas_ctrl_info *ctrl_info;
1594
1595         struct megasas_init_frame *init_frame;
1596         struct megasas_init_queue_info *initq_info;
1597         dma_addr_t init_frame_h;
1598         dma_addr_t initq_info_h;
1599
1600         /*
1601          * Map the message registers
1602          */
1603         instance->base_addr = pci_resource_start(instance->pdev, 0);
1604
1605         if (pci_request_regions(instance->pdev, "megasas: LSI Logic")) {
1606                 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
1607                 return -EBUSY;
1608         }
1609
1610         instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
1611
1612         if (!instance->reg_set) {
1613                 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
1614                 goto fail_ioremap;
1615         }
1616
1617         reg_set = instance->reg_set;
1618
1619         /*
1620          * We expect the FW state to be READY
1621          */
1622         if (megasas_transition_to_ready(instance->reg_set))
1623                 goto fail_ready_state;
1624
1625         /*
1626          * Get various operational parameters from status register
1627          */
1628         instance->max_fw_cmds = readl(&reg_set->outbound_msg_0) & 0x00FFFF;
1629         instance->max_num_sge = (readl(&reg_set->outbound_msg_0) & 0xFF0000) >>
1630             0x10;
1631         /*
1632          * Create a pool of commands
1633          */
1634         if (megasas_alloc_cmds(instance))
1635                 goto fail_alloc_cmds;
1636
1637         /*
1638          * Allocate memory for reply queue. Length of reply queue should
1639          * be _one_ more than the maximum commands handled by the firmware.
1640          *
1641          * Note: When FW completes commands, it places corresponding contex
1642          * values in this circular reply queue. This circular queue is a fairly
1643          * typical producer-consumer queue. FW is the producer (of completed
1644          * commands) and the driver is the consumer.
1645          */
1646         context_sz = sizeof(u32);
1647         reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
1648
1649         instance->reply_queue = pci_alloc_consistent(instance->pdev,
1650                                                      reply_q_sz,
1651                                                      &instance->reply_queue_h);
1652
1653         if (!instance->reply_queue) {
1654                 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
1655                 goto fail_reply_queue;
1656         }
1657
1658         /*
1659          * Prepare a init frame. Note the init frame points to queue info
1660          * structure. Each frame has SGL allocated after first 64 bytes. For
1661          * this frame - since we don't need any SGL - we use SGL's space as
1662          * queue info structure
1663          *
1664          * We will not get a NULL command below. We just created the pool.
1665          */
1666         cmd = megasas_get_cmd(instance);
1667
1668         init_frame = (struct megasas_init_frame *)cmd->frame;
1669         initq_info = (struct megasas_init_queue_info *)
1670             ((unsigned long)init_frame + 64);
1671
1672         init_frame_h = cmd->frame_phys_addr;
1673         initq_info_h = init_frame_h + 64;
1674
1675         memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
1676         memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
1677
1678         initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
1679         initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
1680
1681         initq_info->producer_index_phys_addr_lo = instance->producer_h;
1682         initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
1683
1684         init_frame->cmd = MFI_CMD_INIT;
1685         init_frame->cmd_status = 0xFF;
1686         init_frame->queue_info_new_phys_addr_lo = initq_info_h;
1687
1688         init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
1689
1690         /*
1691          * Issue the init frame in polled mode
1692          */
1693         if (megasas_issue_polled(instance, cmd)) {
1694                 printk(KERN_DEBUG "megasas: Failed to init firmware\n");
1695                 goto fail_fw_init;
1696         }
1697
1698         megasas_return_cmd(instance, cmd);
1699
1700         ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
1701
1702         /*
1703          * Compute the max allowed sectors per IO: The controller info has two
1704          * limits on max sectors. Driver should use the minimum of these two.
1705          *
1706          * 1 << stripe_sz_ops.min = max sectors per strip
1707          *
1708          * Note that older firmwares ( < FW ver 30) didn't report information
1709          * to calculate max_sectors_1. So the number ended up as zero always.
1710          */
1711         if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
1712
1713                 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
1714                     ctrl_info->max_strips_per_io;
1715                 max_sectors_2 = ctrl_info->max_request_size;
1716
1717                 instance->max_sectors_per_req = (max_sectors_1 < max_sectors_2)
1718                     ? max_sectors_1 : max_sectors_2;
1719         } else
1720                 instance->max_sectors_per_req = instance->max_num_sge *
1721                     PAGE_SIZE / 512;
1722
1723         kfree(ctrl_info);
1724
1725         return 0;
1726
1727       fail_fw_init:
1728         megasas_return_cmd(instance, cmd);
1729
1730         pci_free_consistent(instance->pdev, reply_q_sz,
1731                             instance->reply_queue, instance->reply_queue_h);
1732       fail_reply_queue:
1733         megasas_free_cmds(instance);
1734
1735       fail_alloc_cmds:
1736       fail_ready_state:
1737         iounmap(instance->reg_set);
1738
1739       fail_ioremap:
1740         pci_release_regions(instance->pdev);
1741
1742         return -EINVAL;
1743 }
1744
1745 /**
1746  * megasas_release_mfi -        Reverses the FW initialization
1747  * @intance:                    Adapter soft state
1748  */
1749 static void megasas_release_mfi(struct megasas_instance *instance)
1750 {
1751         u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1);
1752
1753         pci_free_consistent(instance->pdev, reply_q_sz,
1754                             instance->reply_queue, instance->reply_queue_h);
1755
1756         megasas_free_cmds(instance);
1757
1758         iounmap(instance->reg_set);
1759
1760         pci_release_regions(instance->pdev);
1761 }
1762
1763 /**
1764  * megasas_get_seq_num -        Gets latest event sequence numbers
1765  * @instance:                   Adapter soft state
1766  * @eli:                        FW event log sequence numbers information
1767  *
1768  * FW maintains a log of all events in a non-volatile area. Upper layers would
1769  * usually find out the latest sequence number of the events, the seq number at
1770  * the boot etc. They would "read" all the events below the latest seq number
1771  * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
1772  * number), they would subsribe to AEN (asynchronous event notification) and
1773  * wait for the events to happen.
1774  */
1775 static int
1776 megasas_get_seq_num(struct megasas_instance *instance,
1777                     struct megasas_evt_log_info *eli)
1778 {
1779         struct megasas_cmd *cmd;
1780         struct megasas_dcmd_frame *dcmd;
1781         struct megasas_evt_log_info *el_info;
1782         dma_addr_t el_info_h = 0;
1783
1784         cmd = megasas_get_cmd(instance);
1785
1786         if (!cmd) {
1787                 return -ENOMEM;
1788         }
1789
1790         dcmd = &cmd->frame->dcmd;
1791         el_info = pci_alloc_consistent(instance->pdev,
1792                                        sizeof(struct megasas_evt_log_info),
1793                                        &el_info_h);
1794
1795         if (!el_info) {
1796                 megasas_return_cmd(instance, cmd);
1797                 return -ENOMEM;
1798         }
1799
1800         memset(el_info, 0, sizeof(*el_info));
1801         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1802
1803         dcmd->cmd = MFI_CMD_DCMD;
1804         dcmd->cmd_status = 0x0;
1805         dcmd->sge_count = 1;
1806         dcmd->flags = MFI_FRAME_DIR_READ;
1807         dcmd->timeout = 0;
1808         dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
1809         dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
1810         dcmd->sgl.sge32[0].phys_addr = el_info_h;
1811         dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
1812
1813         megasas_issue_blocked_cmd(instance, cmd);
1814
1815         /*
1816          * Copy the data back into callers buffer
1817          */
1818         memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
1819
1820         pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
1821                             el_info, el_info_h);
1822
1823         megasas_return_cmd(instance, cmd);
1824
1825         return 0;
1826 }
1827
1828 /**
1829  * megasas_register_aen -       Registers for asynchronous event notification
1830  * @instance:                   Adapter soft state
1831  * @seq_num:                    The starting sequence number
1832  * @class_locale:               Class of the event
1833  *
1834  * This function subscribes for AEN for events beyond the @seq_num. It requests
1835  * to be notified if and only if the event is of type @class_locale
1836  */
1837 static int
1838 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
1839                      u32 class_locale_word)
1840 {
1841         int ret_val;
1842         struct megasas_cmd *cmd;
1843         struct megasas_dcmd_frame *dcmd;
1844         union megasas_evt_class_locale curr_aen;
1845         union megasas_evt_class_locale prev_aen;
1846
1847         /*
1848          * If there an AEN pending already (aen_cmd), check if the
1849          * class_locale of that pending AEN is inclusive of the new
1850          * AEN request we currently have. If it is, then we don't have
1851          * to do anything. In other words, whichever events the current
1852          * AEN request is subscribing to, have already been subscribed
1853          * to.
1854          *
1855          * If the old_cmd is _not_ inclusive, then we have to abort
1856          * that command, form a class_locale that is superset of both
1857          * old and current and re-issue to the FW
1858          */
1859
1860         curr_aen.word = class_locale_word;
1861
1862         if (instance->aen_cmd) {
1863
1864                 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
1865
1866                 /*
1867                  * A class whose enum value is smaller is inclusive of all
1868                  * higher values. If a PROGRESS (= -1) was previously
1869                  * registered, then a new registration requests for higher
1870                  * classes need not be sent to FW. They are automatically
1871                  * included.
1872                  *
1873                  * Locale numbers don't have such hierarchy. They are bitmap
1874                  * values
1875                  */
1876                 if ((prev_aen.members.class <= curr_aen.members.class) &&
1877                     !((prev_aen.members.locale & curr_aen.members.locale) ^
1878                       curr_aen.members.locale)) {
1879                         /*
1880                          * Previously issued event registration includes
1881                          * current request. Nothing to do.
1882                          */
1883                         return 0;
1884                 } else {
1885                         curr_aen.members.locale |= prev_aen.members.locale;
1886
1887                         if (prev_aen.members.class < curr_aen.members.class)
1888                                 curr_aen.members.class = prev_aen.members.class;
1889
1890                         instance->aen_cmd->abort_aen = 1;
1891                         ret_val = megasas_issue_blocked_abort_cmd(instance,
1892                                                                   instance->
1893                                                                   aen_cmd);
1894
1895                         if (ret_val) {
1896                                 printk(KERN_DEBUG "megasas: Failed to abort "
1897                                        "previous AEN command\n");
1898                                 return ret_val;
1899                         }
1900                 }
1901         }
1902
1903         cmd = megasas_get_cmd(instance);
1904
1905         if (!cmd)
1906                 return -ENOMEM;
1907
1908         dcmd = &cmd->frame->dcmd;
1909
1910         memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
1911
1912         /*
1913          * Prepare DCMD for aen registration
1914          */
1915         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1916
1917         dcmd->cmd = MFI_CMD_DCMD;
1918         dcmd->cmd_status = 0x0;
1919         dcmd->sge_count = 1;
1920         dcmd->flags = MFI_FRAME_DIR_READ;
1921         dcmd->timeout = 0;
1922         dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
1923         dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
1924         dcmd->mbox.w[0] = seq_num;
1925         dcmd->mbox.w[1] = curr_aen.word;
1926         dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
1927         dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
1928
1929         /*
1930          * Store reference to the cmd used to register for AEN. When an
1931          * application wants us to register for AEN, we have to abort this
1932          * cmd and re-register with a new EVENT LOCALE supplied by that app
1933          */
1934         instance->aen_cmd = cmd;
1935
1936         /*
1937          * Issue the aen registration frame
1938          */
1939         writel(cmd->frame_phys_addr >> 3,
1940                &instance->reg_set->inbound_queue_port);
1941
1942         return 0;
1943 }
1944
1945 /**
1946  * megasas_start_aen -  Subscribes to AEN during driver load time
1947  * @instance:           Adapter soft state
1948  */
1949 static int megasas_start_aen(struct megasas_instance *instance)
1950 {
1951         struct megasas_evt_log_info eli;
1952         union megasas_evt_class_locale class_locale;
1953
1954         /*
1955          * Get the latest sequence number from FW
1956          */
1957         memset(&eli, 0, sizeof(eli));
1958
1959         if (megasas_get_seq_num(instance, &eli))
1960                 return -1;
1961
1962         /*
1963          * Register AEN with FW for latest sequence number plus 1
1964          */
1965         class_locale.members.reserved = 0;
1966         class_locale.members.locale = MR_EVT_LOCALE_ALL;
1967         class_locale.members.class = MR_EVT_CLASS_DEBUG;
1968
1969         return megasas_register_aen(instance, eli.newest_seq_num + 1,
1970                                     class_locale.word);
1971 }
1972
1973 /**
1974  * megasas_io_attach -  Attaches this driver to SCSI mid-layer
1975  * @instance:           Adapter soft state
1976  */
1977 static int megasas_io_attach(struct megasas_instance *instance)
1978 {
1979         struct Scsi_Host *host = instance->host;
1980
1981         /*
1982          * Export parameters required by SCSI mid-layer
1983          */
1984         host->irq = instance->pdev->irq;
1985         host->unique_id = instance->unique_id;
1986         host->can_queue = instance->max_fw_cmds - MEGASAS_INT_CMDS;
1987         host->this_id = instance->init_id;
1988         host->sg_tablesize = instance->max_num_sge;
1989         host->max_sectors = instance->max_sectors_per_req;
1990         host->cmd_per_lun = 128;
1991         host->max_channel = MEGASAS_MAX_CHANNELS - 1;
1992         host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
1993         host->max_lun = MEGASAS_MAX_LUN;
1994
1995         /*
1996          * Notify the mid-layer about the new controller
1997          */
1998         if (scsi_add_host(host, &instance->pdev->dev)) {
1999                 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
2000                 return -ENODEV;
2001         }
2002
2003         /*
2004          * Trigger SCSI to scan our drives
2005          */
2006         scsi_scan_host(host);
2007         return 0;
2008 }
2009
2010 /**
2011  * megasas_probe_one -  PCI hotplug entry point
2012  * @pdev:               PCI device structure
2013  * @id:                 PCI ids of supported hotplugged adapter 
2014  */
2015 static int __devinit
2016 megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id)
2017 {
2018         int rval;
2019         struct Scsi_Host *host;
2020         struct megasas_instance *instance;
2021
2022         /*
2023          * Announce PCI information
2024          */
2025         printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
2026                pdev->vendor, pdev->device, pdev->subsystem_vendor,
2027                pdev->subsystem_device);
2028
2029         printk("bus %d:slot %d:func %d\n",
2030                pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
2031
2032         /*
2033          * PCI prepping: enable device set bus mastering and dma mask
2034          */
2035         rval = pci_enable_device(pdev);
2036
2037         if (rval) {
2038                 return rval;
2039         }
2040
2041         pci_set_master(pdev);
2042
2043         /*
2044          * All our contollers are capable of performing 64-bit DMA
2045          */
2046         if (IS_DMA64) {
2047                 if (pci_set_dma_mask(pdev, DMA_64BIT_MASK) != 0) {
2048
2049                         if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2050                                 goto fail_set_dma_mask;
2051                 }
2052         } else {
2053                 if (pci_set_dma_mask(pdev, DMA_32BIT_MASK) != 0)
2054                         goto fail_set_dma_mask;
2055         }
2056
2057         host = scsi_host_alloc(&megasas_template,
2058                                sizeof(struct megasas_instance));
2059
2060         if (!host) {
2061                 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
2062                 goto fail_alloc_instance;
2063         }
2064
2065         instance = (struct megasas_instance *)host->hostdata;
2066         memset(instance, 0, sizeof(*instance));
2067
2068         instance->producer = pci_alloc_consistent(pdev, sizeof(u32),
2069                                                   &instance->producer_h);
2070         instance->consumer = pci_alloc_consistent(pdev, sizeof(u32),
2071                                                   &instance->consumer_h);
2072
2073         if (!instance->producer || !instance->consumer) {
2074                 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2075                        "producer, consumer\n");
2076                 goto fail_alloc_dma_buf;
2077         }
2078
2079         *instance->producer = 0;
2080         *instance->consumer = 0;
2081
2082         instance->evt_detail = pci_alloc_consistent(pdev,
2083                                                     sizeof(struct
2084                                                            megasas_evt_detail),
2085                                                     &instance->evt_detail_h);
2086
2087         if (!instance->evt_detail) {
2088                 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
2089                        "event detail structure\n");
2090                 goto fail_alloc_dma_buf;
2091         }
2092
2093         /*
2094          * Initialize locks and queues
2095          */
2096         INIT_LIST_HEAD(&instance->cmd_pool);
2097
2098         init_waitqueue_head(&instance->int_cmd_wait_q);
2099         init_waitqueue_head(&instance->abort_cmd_wait_q);
2100
2101         spin_lock_init(&instance->cmd_pool_lock);
2102         spin_lock_init(&instance->instance_lock);
2103
2104         sema_init(&instance->aen_mutex, 1);
2105         sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
2106
2107         /*
2108          * Initialize PCI related and misc parameters
2109          */
2110         instance->pdev = pdev;
2111         instance->host = host;
2112         instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
2113         instance->init_id = MEGASAS_DEFAULT_INIT_ID;
2114
2115         /*
2116          * Initialize MFI Firmware
2117          */
2118         if (megasas_init_mfi(instance))
2119                 goto fail_init_mfi;
2120
2121         /*
2122          * Register IRQ
2123          */
2124         if (request_irq(pdev->irq, megasas_isr, SA_SHIRQ, "megasas", instance)) {
2125                 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
2126                 goto fail_irq;
2127         }
2128
2129         megasas_enable_intr(instance->reg_set);
2130
2131         /*
2132          * Store instance in PCI softstate
2133          */
2134         pci_set_drvdata(pdev, instance);
2135
2136         /*
2137          * Add this controller to megasas_mgmt_info structure so that it
2138          * can be exported to management applications
2139          */
2140         megasas_mgmt_info.count++;
2141         megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
2142         megasas_mgmt_info.max_index++;
2143
2144         /*
2145          * Initiate AEN (Asynchronous Event Notification)
2146          */
2147         if (megasas_start_aen(instance)) {
2148                 printk(KERN_DEBUG "megasas: start aen failed\n");
2149                 goto fail_start_aen;
2150         }
2151
2152         /*
2153          * Register with SCSI mid-layer
2154          */
2155         if (megasas_io_attach(instance))
2156                 goto fail_io_attach;
2157
2158         return 0;
2159
2160       fail_start_aen:
2161       fail_io_attach:
2162         megasas_mgmt_info.count--;
2163         megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
2164         megasas_mgmt_info.max_index--;
2165
2166         pci_set_drvdata(pdev, NULL);
2167         megasas_disable_intr(instance->reg_set);
2168         free_irq(instance->pdev->irq, instance);
2169
2170         megasas_release_mfi(instance);
2171
2172       fail_irq:
2173       fail_init_mfi:
2174       fail_alloc_dma_buf:
2175         if (instance->evt_detail)
2176                 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2177                                     instance->evt_detail,
2178                                     instance->evt_detail_h);
2179
2180         if (instance->producer)
2181                 pci_free_consistent(pdev, sizeof(u32), instance->producer,
2182                                     instance->producer_h);
2183         if (instance->consumer)
2184                 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2185                                     instance->consumer_h);
2186         scsi_host_put(host);
2187
2188       fail_alloc_instance:
2189       fail_set_dma_mask:
2190         pci_disable_device(pdev);
2191
2192         return -ENODEV;
2193 }
2194
2195 /**
2196  * megasas_flush_cache -        Requests FW to flush all its caches
2197  * @instance:                   Adapter soft state
2198  */
2199 static void megasas_flush_cache(struct megasas_instance *instance)
2200 {
2201         struct megasas_cmd *cmd;
2202         struct megasas_dcmd_frame *dcmd;
2203
2204         cmd = megasas_get_cmd(instance);
2205
2206         if (!cmd)
2207                 return;
2208
2209         dcmd = &cmd->frame->dcmd;
2210
2211         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2212
2213         dcmd->cmd = MFI_CMD_DCMD;
2214         dcmd->cmd_status = 0x0;
2215         dcmd->sge_count = 0;
2216         dcmd->flags = MFI_FRAME_DIR_NONE;
2217         dcmd->timeout = 0;
2218         dcmd->data_xfer_len = 0;
2219         dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
2220         dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
2221
2222         megasas_issue_blocked_cmd(instance, cmd);
2223
2224         megasas_return_cmd(instance, cmd);
2225
2226         return;
2227 }
2228
2229 /**
2230  * megasas_shutdown_controller -        Instructs FW to shutdown the controller
2231  * @instance:                           Adapter soft state
2232  */
2233 static void megasas_shutdown_controller(struct megasas_instance *instance)
2234 {
2235         struct megasas_cmd *cmd;
2236         struct megasas_dcmd_frame *dcmd;
2237
2238         cmd = megasas_get_cmd(instance);
2239
2240         if (!cmd)
2241                 return;
2242
2243         if (instance->aen_cmd)
2244                 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
2245
2246         dcmd = &cmd->frame->dcmd;
2247
2248         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2249
2250         dcmd->cmd = MFI_CMD_DCMD;
2251         dcmd->cmd_status = 0x0;
2252         dcmd->sge_count = 0;
2253         dcmd->flags = MFI_FRAME_DIR_NONE;
2254         dcmd->timeout = 0;
2255         dcmd->data_xfer_len = 0;
2256         dcmd->opcode = MR_DCMD_CTRL_SHUTDOWN;
2257
2258         megasas_issue_blocked_cmd(instance, cmd);
2259
2260         megasas_return_cmd(instance, cmd);
2261
2262         return;
2263 }
2264
2265 /**
2266  * megasas_detach_one - PCI hot"un"plug entry point
2267  * @pdev:               PCI device structure
2268  */
2269 static void megasas_detach_one(struct pci_dev *pdev)
2270 {
2271         int i;
2272         struct Scsi_Host *host;
2273         struct megasas_instance *instance;
2274
2275         instance = pci_get_drvdata(pdev);
2276         host = instance->host;
2277
2278         scsi_remove_host(instance->host);
2279         megasas_flush_cache(instance);
2280         megasas_shutdown_controller(instance);
2281
2282         /*
2283          * Take the instance off the instance array. Note that we will not
2284          * decrement the max_index. We let this array be sparse array
2285          */
2286         for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2287                 if (megasas_mgmt_info.instance[i] == instance) {
2288                         megasas_mgmt_info.count--;
2289                         megasas_mgmt_info.instance[i] = NULL;
2290
2291                         break;
2292                 }
2293         }
2294
2295         pci_set_drvdata(instance->pdev, NULL);
2296
2297         megasas_disable_intr(instance->reg_set);
2298
2299         free_irq(instance->pdev->irq, instance);
2300
2301         megasas_release_mfi(instance);
2302
2303         pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
2304                             instance->evt_detail, instance->evt_detail_h);
2305
2306         pci_free_consistent(pdev, sizeof(u32), instance->producer,
2307                             instance->producer_h);
2308
2309         pci_free_consistent(pdev, sizeof(u32), instance->consumer,
2310                             instance->consumer_h);
2311
2312         scsi_host_put(host);
2313
2314         pci_set_drvdata(pdev, NULL);
2315
2316         pci_disable_device(pdev);
2317
2318         return;
2319 }
2320
2321 /**
2322  * megasas_shutdown -   Shutdown entry point
2323  * @device:             Generic device structure
2324  */
2325 static void megasas_shutdown(struct pci_dev *pdev)
2326 {
2327         struct megasas_instance *instance = pci_get_drvdata(pdev);
2328         megasas_flush_cache(instance);
2329 }
2330
2331 /**
2332  * megasas_mgmt_open -  char node "open" entry point
2333  */
2334 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
2335 {
2336         /*
2337          * Allow only those users with admin rights
2338          */
2339         if (!capable(CAP_SYS_ADMIN))
2340                 return -EACCES;
2341
2342         return 0;
2343 }
2344
2345 /**
2346  * megasas_mgmt_release - char node "release" entry point
2347  */
2348 static int megasas_mgmt_release(struct inode *inode, struct file *filep)
2349 {
2350         filep->private_data = NULL;
2351         fasync_helper(-1, filep, 0, &megasas_async_queue);
2352
2353         return 0;
2354 }
2355
2356 /**
2357  * megasas_mgmt_fasync -        Async notifier registration from applications
2358  *
2359  * This function adds the calling process to a driver global queue. When an
2360  * event occurs, SIGIO will be sent to all processes in this queue.
2361  */
2362 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
2363 {
2364         int rc;
2365
2366         mutex_lock(&megasas_async_queue_mutex);
2367
2368         rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
2369
2370         mutex_unlock(&megasas_async_queue_mutex);
2371
2372         if (rc >= 0) {
2373                 /* For sanity check when we get ioctl */
2374                 filep->private_data = filep;
2375                 return 0;
2376         }
2377
2378         printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
2379
2380         return rc;
2381 }
2382
2383 /**
2384  * megasas_mgmt_fw_ioctl -      Issues management ioctls to FW
2385  * @instance:                   Adapter soft state
2386  * @argp:                       User's ioctl packet
2387  */
2388 static int
2389 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
2390                       struct megasas_iocpacket __user * user_ioc,
2391                       struct megasas_iocpacket *ioc)
2392 {
2393         struct megasas_sge32 *kern_sge32;
2394         struct megasas_cmd *cmd;
2395         void *kbuff_arr[MAX_IOCTL_SGE];
2396         dma_addr_t buf_handle = 0;
2397         int error = 0, i;
2398         void *sense = NULL;
2399         dma_addr_t sense_handle;
2400         u32 *sense_ptr;
2401
2402         memset(kbuff_arr, 0, sizeof(kbuff_arr));
2403
2404         if (ioc->sge_count > MAX_IOCTL_SGE) {
2405                 printk(KERN_DEBUG "megasas: SGE count [%d] >  max limit [%d]\n",
2406                        ioc->sge_count, MAX_IOCTL_SGE);
2407                 return -EINVAL;
2408         }
2409
2410         cmd = megasas_get_cmd(instance);
2411         if (!cmd) {
2412                 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
2413                 return -ENOMEM;
2414         }
2415
2416         /*
2417          * User's IOCTL packet has 2 frames (maximum). Copy those two
2418          * frames into our cmd's frames. cmd->frame's context will get
2419          * overwritten when we copy from user's frames. So set that value
2420          * alone separately
2421          */
2422         memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
2423         cmd->frame->hdr.context = cmd->index;
2424
2425         /*
2426          * The management interface between applications and the fw uses
2427          * MFI frames. E.g, RAID configuration changes, LD property changes
2428          * etc are accomplishes through different kinds of MFI frames. The
2429          * driver needs to care only about substituting user buffers with
2430          * kernel buffers in SGLs. The location of SGL is embedded in the
2431          * struct iocpacket itself.
2432          */
2433         kern_sge32 = (struct megasas_sge32 *)
2434             ((unsigned long)cmd->frame + ioc->sgl_off);
2435
2436         /*
2437          * For each user buffer, create a mirror buffer and copy in
2438          */
2439         for (i = 0; i < ioc->sge_count; i++) {
2440                 kbuff_arr[i] = pci_alloc_consistent(instance->pdev,
2441                                                     ioc->sgl[i].iov_len,
2442                                                     &buf_handle);
2443                 if (!kbuff_arr[i]) {
2444                         printk(KERN_DEBUG "megasas: Failed to alloc "
2445                                "kernel SGL buffer for IOCTL \n");
2446                         error = -ENOMEM;
2447                         goto out;
2448                 }
2449
2450                 /*
2451                  * We don't change the dma_coherent_mask, so
2452                  * pci_alloc_consistent only returns 32bit addresses
2453                  */
2454                 kern_sge32[i].phys_addr = (u32) buf_handle;
2455                 kern_sge32[i].length = ioc->sgl[i].iov_len;
2456
2457                 /*
2458                  * We created a kernel buffer corresponding to the
2459                  * user buffer. Now copy in from the user buffer
2460                  */
2461                 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
2462                                    (u32) (ioc->sgl[i].iov_len))) {
2463                         error = -EFAULT;
2464                         goto out;
2465                 }
2466         }
2467
2468         if (ioc->sense_len) {
2469                 sense = pci_alloc_consistent(instance->pdev, ioc->sense_len,
2470                                              &sense_handle);
2471                 if (!sense) {
2472                         error = -ENOMEM;
2473                         goto out;
2474                 }
2475
2476                 sense_ptr =
2477                     (u32 *) ((unsigned long)cmd->frame + ioc->sense_off);
2478                 *sense_ptr = sense_handle;
2479         }
2480
2481         /*
2482          * Set the sync_cmd flag so that the ISR knows not to complete this
2483          * cmd to the SCSI mid-layer
2484          */
2485         cmd->sync_cmd = 1;
2486         megasas_issue_blocked_cmd(instance, cmd);
2487         cmd->sync_cmd = 0;
2488
2489         /*
2490          * copy out the kernel buffers to user buffers
2491          */
2492         for (i = 0; i < ioc->sge_count; i++) {
2493                 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
2494                                  ioc->sgl[i].iov_len)) {
2495                         error = -EFAULT;
2496                         goto out;
2497                 }
2498         }
2499
2500         /*
2501          * copy out the sense
2502          */
2503         if (ioc->sense_len) {
2504                 /*
2505                  * sense_ptr points to the location that has the user
2506                  * sense buffer address
2507                  */
2508                 sense_ptr = (u32 *) ((unsigned long)ioc->frame.raw +
2509                                      ioc->sense_off);
2510
2511                 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
2512                                  sense, ioc->sense_len)) {
2513                         error = -EFAULT;
2514                         goto out;
2515                 }
2516         }
2517
2518         /*
2519          * copy the status codes returned by the fw
2520          */
2521         if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
2522                          &cmd->frame->hdr.cmd_status, sizeof(u8))) {
2523                 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
2524                 error = -EFAULT;
2525         }
2526
2527       out:
2528         if (sense) {
2529                 pci_free_consistent(instance->pdev, ioc->sense_len,
2530                                     sense, sense_handle);
2531         }
2532
2533         for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
2534                 pci_free_consistent(instance->pdev,
2535                                     kern_sge32[i].length,
2536                                     kbuff_arr[i], kern_sge32[i].phys_addr);
2537         }
2538
2539         megasas_return_cmd(instance, cmd);
2540         return error;
2541 }
2542
2543 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
2544 {
2545         int i;
2546
2547         for (i = 0; i < megasas_mgmt_info.max_index; i++) {
2548
2549                 if ((megasas_mgmt_info.instance[i]) &&
2550                     (megasas_mgmt_info.instance[i]->host->host_no == host_no))
2551                         return megasas_mgmt_info.instance[i];
2552         }
2553
2554         return NULL;
2555 }
2556
2557 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
2558 {
2559         struct megasas_iocpacket __user *user_ioc =
2560             (struct megasas_iocpacket __user *)arg;
2561         struct megasas_iocpacket *ioc;
2562         struct megasas_instance *instance;
2563         int error;
2564
2565         ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
2566         if (!ioc)
2567                 return -ENOMEM;
2568
2569         if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
2570                 error = -EFAULT;
2571                 goto out_kfree_ioc;
2572         }
2573
2574         instance = megasas_lookup_instance(ioc->host_no);
2575         if (!instance) {
2576                 error = -ENODEV;
2577                 goto out_kfree_ioc;
2578         }
2579
2580         /*
2581          * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
2582          */
2583         if (down_interruptible(&instance->ioctl_sem)) {
2584                 error = -ERESTARTSYS;
2585                 goto out_kfree_ioc;
2586         }
2587         error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
2588         up(&instance->ioctl_sem);
2589
2590       out_kfree_ioc:
2591         kfree(ioc);
2592         return error;
2593 }
2594
2595 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
2596 {
2597         struct megasas_instance *instance;
2598         struct megasas_aen aen;
2599         int error;
2600
2601         if (file->private_data != file) {
2602                 printk(KERN_DEBUG "megasas: fasync_helper was not "
2603                        "called first\n");
2604                 return -EINVAL;
2605         }
2606
2607         if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
2608                 return -EFAULT;
2609
2610         instance = megasas_lookup_instance(aen.host_no);
2611
2612         if (!instance)
2613                 return -ENODEV;
2614
2615         down(&instance->aen_mutex);
2616         error = megasas_register_aen(instance, aen.seq_num,
2617                                      aen.class_locale_word);
2618         up(&instance->aen_mutex);
2619         return error;
2620 }
2621
2622 /**
2623  * megasas_mgmt_ioctl - char node ioctl entry point
2624  */
2625 static long
2626 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2627 {
2628         switch (cmd) {
2629         case MEGASAS_IOC_FIRMWARE:
2630                 return megasas_mgmt_ioctl_fw(file, arg);
2631
2632         case MEGASAS_IOC_GET_AEN:
2633                 return megasas_mgmt_ioctl_aen(file, arg);
2634         }
2635
2636         return -ENOTTY;
2637 }
2638
2639 #ifdef CONFIG_COMPAT
2640 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
2641 {
2642         struct compat_megasas_iocpacket __user *cioc =
2643             (struct compat_megasas_iocpacket __user *)arg;
2644         struct megasas_iocpacket __user *ioc =
2645             compat_alloc_user_space(sizeof(struct megasas_iocpacket));
2646         int i;
2647         int error = 0;
2648
2649         clear_user(ioc, sizeof(*ioc));
2650
2651         if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
2652             copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
2653             copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
2654             copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
2655             copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
2656             copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
2657                 return -EFAULT;
2658
2659         for (i = 0; i < MAX_IOCTL_SGE; i++) {
2660                 compat_uptr_t ptr;
2661
2662                 if (get_user(ptr, &cioc->sgl[i].iov_base) ||
2663                     put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
2664                     copy_in_user(&ioc->sgl[i].iov_len,
2665                                  &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
2666                         return -EFAULT;
2667         }
2668
2669         error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
2670
2671         if (copy_in_user(&cioc->frame.hdr.cmd_status,
2672                          &ioc->frame.hdr.cmd_status, sizeof(u8))) {
2673                 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
2674                 return -EFAULT;
2675         }
2676         return error;
2677 }
2678
2679 static long
2680 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
2681                           unsigned long arg)
2682 {
2683         switch (cmd) {
2684         case MEGASAS_IOC_FIRMWARE:{
2685                         return megasas_mgmt_compat_ioctl_fw(file, arg);
2686                 }
2687         case MEGASAS_IOC_GET_AEN:
2688                 return megasas_mgmt_ioctl_aen(file, arg);
2689         }
2690
2691         return -ENOTTY;
2692 }
2693 #endif
2694
2695 /*
2696  * File operations structure for management interface
2697  */
2698 static struct file_operations megasas_mgmt_fops = {
2699         .owner = THIS_MODULE,
2700         .open = megasas_mgmt_open,
2701         .release = megasas_mgmt_release,
2702         .fasync = megasas_mgmt_fasync,
2703         .unlocked_ioctl = megasas_mgmt_ioctl,
2704 #ifdef CONFIG_COMPAT
2705         .compat_ioctl = megasas_mgmt_compat_ioctl,
2706 #endif
2707 };
2708
2709 /*
2710  * PCI hotplug support registration structure
2711  */
2712 static struct pci_driver megasas_pci_driver = {
2713
2714         .name = "megaraid_sas",
2715         .id_table = megasas_pci_table,
2716         .probe = megasas_probe_one,
2717         .remove = __devexit_p(megasas_detach_one),
2718         .shutdown = megasas_shutdown,
2719 };
2720
2721 /*
2722  * Sysfs driver attributes
2723  */
2724 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
2725 {
2726         return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
2727                         MEGASAS_VERSION);
2728 }
2729
2730 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
2731
2732 static ssize_t
2733 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
2734 {
2735         return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
2736                         MEGASAS_RELDATE);
2737 }
2738
2739 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
2740                    NULL);
2741
2742 /**
2743  * megasas_init - Driver load entry point
2744  */
2745 static int __init megasas_init(void)
2746 {
2747         int rval;
2748
2749         /*
2750          * Announce driver version and other information
2751          */
2752         printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
2753                MEGASAS_EXT_VERSION);
2754
2755         memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info));
2756
2757         /*
2758          * Register character device node
2759          */
2760         rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
2761
2762         if (rval < 0) {
2763                 printk(KERN_DEBUG "megasas: failed to open device node\n");
2764                 return rval;
2765         }
2766
2767         megasas_mgmt_majorno = rval;
2768
2769         /*
2770          * Register ourselves as PCI hotplug module
2771          */
2772         rval = pci_module_init(&megasas_pci_driver);
2773
2774         if (rval) {
2775                 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
2776                 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
2777         }
2778
2779         driver_create_file(&megasas_pci_driver.driver, &driver_attr_version);
2780         driver_create_file(&megasas_pci_driver.driver,
2781                            &driver_attr_release_date);
2782
2783         return rval;
2784 }
2785
2786 /**
2787  * megasas_exit - Driver unload entry point
2788  */
2789 static void __exit megasas_exit(void)
2790 {
2791         driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
2792         driver_remove_file(&megasas_pci_driver.driver,
2793                            &driver_attr_release_date);
2794
2795         pci_unregister_driver(&megasas_pci_driver);
2796         unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
2797 }
2798
2799 module_init(megasas_init);
2800 module_exit(megasas_exit);