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