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