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