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