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