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
28
29
30
31
32
33
34
35#include <linux/kernel.h>
36#include <linux/types.h>
37#include <linux/pci.h>
38#include <linux/list.h>
39#include <linux/moduleparam.h>
40#include <linux/module.h>
41#include <linux/spinlock.h>
42#include <linux/interrupt.h>
43#include <linux/delay.h>
44#include <linux/uio.h>
45#include <linux/slab.h>
46#include <asm/uaccess.h>
47#include <linux/fs.h>
48#include <linux/compat.h>
49#include <linux/blkdev.h>
50#include <linux/mutex.h>
51#include <linux/poll.h>
52
53#include <scsi/scsi.h>
54#include <scsi/scsi_cmnd.h>
55#include <scsi/scsi_device.h>
56#include <scsi/scsi_host.h>
57#include <scsi/scsi_tcq.h>
58#include "megaraid_sas_fusion.h"
59#include "megaraid_sas.h"
60
61
62
63
64
65static unsigned int max_sectors;
66module_param_named(max_sectors, max_sectors, int, 0);
67MODULE_PARM_DESC(max_sectors,
68 "Maximum number of sectors per IO command");
69
70static int msix_disable;
71module_param(msix_disable, int, S_IRUGO);
72MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
73
74static unsigned int msix_vectors;
75module_param(msix_vectors, int, S_IRUGO);
76MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
77
78static int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
79module_param(throttlequeuedepth, int, S_IRUGO);
80MODULE_PARM_DESC(throttlequeuedepth,
81 "Adapter queue depth when throttled due to I/O timeout. Default: 16");
82
83int resetwaittime = MEGASAS_RESET_WAIT_TIME;
84module_param(resetwaittime, int, S_IRUGO);
85MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
86 "before resetting adapter. Default: 180");
87
88MODULE_LICENSE("GPL");
89MODULE_VERSION(MEGASAS_VERSION);
90MODULE_AUTHOR("megaraidlinux@lsi.com");
91MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
92
93int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
94static int megasas_get_pd_list(struct megasas_instance *instance);
95static int megasas_issue_init_mfi(struct megasas_instance *instance);
96static int megasas_register_aen(struct megasas_instance *instance,
97 u32 seq_num, u32 class_locale_word);
98
99
100
101static struct pci_device_id megasas_pci_table[] = {
102
103 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
104
105 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
106
107 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
108
109 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
110
111 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
112
113 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
114
115 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
116
117 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
118
119 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
120
121 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
122
123 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
124
125 {}
126};
127
128MODULE_DEVICE_TABLE(pci, megasas_pci_table);
129
130static int megasas_mgmt_majorno;
131static struct megasas_mgmt_info megasas_mgmt_info;
132static struct fasync_struct *megasas_async_queue;
133static DEFINE_MUTEX(megasas_async_queue_mutex);
134
135static int megasas_poll_wait_aen;
136static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
137static u32 support_poll_for_event;
138u32 megasas_dbg_lvl;
139static u32 support_device_change;
140
141
142spinlock_t poll_aen_lock;
143
144void
145megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
146 u8 alt_status);
147static u32
148megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
149static int
150megasas_adp_reset_gen2(struct megasas_instance *instance,
151 struct megasas_register_set __iomem *reg_set);
152static irqreturn_t megasas_isr(int irq, void *devp);
153static u32
154megasas_init_adapter_mfi(struct megasas_instance *instance);
155u32
156megasas_build_and_issue_cmd(struct megasas_instance *instance,
157 struct scsi_cmnd *scmd);
158static void megasas_complete_cmd_dpc(unsigned long instance_addr);
159void
160megasas_release_fusion(struct megasas_instance *instance);
161int
162megasas_ioc_init_fusion(struct megasas_instance *instance);
163void
164megasas_free_cmds_fusion(struct megasas_instance *instance);
165u8
166megasas_get_map_info(struct megasas_instance *instance);
167int
168megasas_sync_map_info(struct megasas_instance *instance);
169int
170wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
171void megasas_reset_reply_desc(struct megasas_instance *instance);
172u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
173 struct LD_LOAD_BALANCE_INFO *lbInfo);
174int megasas_reset_fusion(struct Scsi_Host *shost);
175void megasas_fusion_ocr_wq(struct work_struct *work);
176
177void
178megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
179{
180 instance->instancet->fire_cmd(instance,
181 cmd->frame_phys_addr, 0, instance->reg_set);
182}
183
184
185
186
187
188
189
190struct megasas_cmd *megasas_get_cmd(struct megasas_instance
191 *instance)
192{
193 unsigned long flags;
194 struct megasas_cmd *cmd = NULL;
195
196 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
197
198 if (!list_empty(&instance->cmd_pool)) {
199 cmd = list_entry((&instance->cmd_pool)->next,
200 struct megasas_cmd, list);
201 list_del_init(&cmd->list);
202 } else {
203 printk(KERN_ERR "megasas: Command pool empty!\n");
204 }
205
206 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
207 return cmd;
208}
209
210
211
212
213
214
215inline void
216megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
217{
218 unsigned long flags;
219
220 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
221
222 cmd->scmd = NULL;
223 cmd->frame_count = 0;
224 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
225 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
226 (reset_devices))
227 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
228 list_add_tail(&cmd->list, &instance->cmd_pool);
229
230 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
231}
232
233
234
235
236
237
238
239
240
241
242
243static inline void
244megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
245{
246 writel(0, &(regs)->outbound_intr_mask);
247
248
249 readl(®s->outbound_intr_mask);
250}
251
252
253
254
255
256static inline void
257megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
258{
259 u32 mask = 0x1f;
260 writel(mask, ®s->outbound_intr_mask);
261
262 readl(®s->outbound_intr_mask);
263}
264
265
266
267
268
269static u32
270megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
271{
272 return readl(&(regs)->outbound_msg_0);
273}
274
275
276
277
278static int
279megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
280{
281 u32 status;
282 u32 mfiStatus = 0;
283
284
285
286 status = readl(®s->outbound_intr_status);
287
288 if (status & MFI_OB_INTR_STATUS_MASK)
289 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
290 if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
291 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
292
293
294
295
296 if (mfiStatus)
297 writel(status, ®s->outbound_intr_status);
298
299
300 readl(®s->outbound_intr_status);
301
302 return mfiStatus;
303}
304
305
306
307
308
309
310
311static inline void
312megasas_fire_cmd_xscale(struct megasas_instance *instance,
313 dma_addr_t frame_phys_addr,
314 u32 frame_count,
315 struct megasas_register_set __iomem *regs)
316{
317 unsigned long flags;
318 spin_lock_irqsave(&instance->hba_lock, flags);
319 writel((frame_phys_addr >> 3)|(frame_count),
320 &(regs)->inbound_queue_port);
321 spin_unlock_irqrestore(&instance->hba_lock, flags);
322}
323
324
325
326
327
328static int
329megasas_adp_reset_xscale(struct megasas_instance *instance,
330 struct megasas_register_set __iomem *regs)
331{
332 u32 i;
333 u32 pcidata;
334 writel(MFI_ADP_RESET, ®s->inbound_doorbell);
335
336 for (i = 0; i < 3; i++)
337 msleep(1000);
338 pcidata = 0;
339 pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
340 printk(KERN_NOTICE "pcidata = %x\n", pcidata);
341 if (pcidata & 0x2) {
342 printk(KERN_NOTICE "mfi 1068 offset read=%x\n", pcidata);
343 pcidata &= ~0x2;
344 pci_write_config_dword(instance->pdev,
345 MFI_1068_PCSR_OFFSET, pcidata);
346
347 for (i = 0; i < 2; i++)
348 msleep(1000);
349
350 pcidata = 0;
351 pci_read_config_dword(instance->pdev,
352 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
353 printk(KERN_NOTICE "1068 offset handshake read=%x\n", pcidata);
354 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
355 printk(KERN_NOTICE "1068 offset pcidt=%x\n", pcidata);
356 pcidata = 0;
357 pci_write_config_dword(instance->pdev,
358 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
359 }
360 }
361 return 0;
362}
363
364
365
366
367
368static int
369megasas_check_reset_xscale(struct megasas_instance *instance,
370 struct megasas_register_set __iomem *regs)
371{
372 u32 consumer;
373 consumer = *instance->consumer;
374
375 if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
376 (*instance->consumer == MEGASAS_ADPRESET_INPROG_SIGN)) {
377 return 1;
378 }
379 return 0;
380}
381
382static struct megasas_instance_template megasas_instance_template_xscale = {
383
384 .fire_cmd = megasas_fire_cmd_xscale,
385 .enable_intr = megasas_enable_intr_xscale,
386 .disable_intr = megasas_disable_intr_xscale,
387 .clear_intr = megasas_clear_intr_xscale,
388 .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
389 .adp_reset = megasas_adp_reset_xscale,
390 .check_reset = megasas_check_reset_xscale,
391 .service_isr = megasas_isr,
392 .tasklet = megasas_complete_cmd_dpc,
393 .init_adapter = megasas_init_adapter_mfi,
394 .build_and_issue_cmd = megasas_build_and_issue_cmd,
395 .issue_dcmd = megasas_issue_dcmd,
396};
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412static inline void
413megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs)
414{
415 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
416
417 writel(~0x80000000, &(regs)->outbound_intr_mask);
418
419
420 readl(®s->outbound_intr_mask);
421}
422
423
424
425
426
427static inline void
428megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
429{
430 u32 mask = 0xFFFFFFFF;
431 writel(mask, ®s->outbound_intr_mask);
432
433 readl(®s->outbound_intr_mask);
434}
435
436
437
438
439
440static u32
441megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
442{
443 return readl(&(regs)->outbound_scratch_pad);
444}
445
446
447
448
449
450static int
451megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
452{
453 u32 status, mfiStatus = 0;
454
455
456
457
458 status = readl(®s->outbound_intr_status);
459
460 if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
461 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
462
463 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
464 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
465
466
467
468
469 writel(status, ®s->outbound_doorbell_clear);
470
471
472 readl(®s->outbound_doorbell_clear);
473
474 return mfiStatus;
475}
476
477
478
479
480
481
482
483static inline void
484megasas_fire_cmd_ppc(struct megasas_instance *instance,
485 dma_addr_t frame_phys_addr,
486 u32 frame_count,
487 struct megasas_register_set __iomem *regs)
488{
489 unsigned long flags;
490 spin_lock_irqsave(&instance->hba_lock, flags);
491 writel((frame_phys_addr | (frame_count<<1))|1,
492 &(regs)->inbound_queue_port);
493 spin_unlock_irqrestore(&instance->hba_lock, flags);
494}
495
496
497
498
499
500static int
501megasas_check_reset_ppc(struct megasas_instance *instance,
502 struct megasas_register_set __iomem *regs)
503{
504 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
505 return 1;
506
507 return 0;
508}
509
510static struct megasas_instance_template megasas_instance_template_ppc = {
511
512 .fire_cmd = megasas_fire_cmd_ppc,
513 .enable_intr = megasas_enable_intr_ppc,
514 .disable_intr = megasas_disable_intr_ppc,
515 .clear_intr = megasas_clear_intr_ppc,
516 .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
517 .adp_reset = megasas_adp_reset_xscale,
518 .check_reset = megasas_check_reset_ppc,
519 .service_isr = megasas_isr,
520 .tasklet = megasas_complete_cmd_dpc,
521 .init_adapter = megasas_init_adapter_mfi,
522 .build_and_issue_cmd = megasas_build_and_issue_cmd,
523 .issue_dcmd = megasas_issue_dcmd,
524};
525
526
527
528
529
530static inline void
531megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
532{
533 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
534
535 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
536
537
538 readl(®s->outbound_intr_mask);
539}
540
541
542
543
544
545static inline void
546megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
547{
548 u32 mask = 0xFFFFFFFF;
549 writel(mask, ®s->outbound_intr_mask);
550
551 readl(®s->outbound_intr_mask);
552}
553
554
555
556
557
558static u32
559megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
560{
561 return readl(&(regs)->outbound_scratch_pad);
562}
563
564
565
566
567
568static int
569megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
570{
571 u32 status;
572 u32 mfiStatus = 0;
573
574
575
576
577 status = readl(®s->outbound_intr_status);
578
579 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
580 return 0;
581 }
582
583
584
585
586 if ((megasas_read_fw_status_reg_gen2(regs) & MFI_STATE_MASK) ==
587 MFI_STATE_FAULT) {
588 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
589 } else
590 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
591
592
593
594
595 writel(status, ®s->outbound_intr_status);
596
597
598
599
600 readl(®s->outbound_intr_status);
601
602 return mfiStatus;
603}
604
605
606
607
608
609
610
611static inline void
612megasas_fire_cmd_skinny(struct megasas_instance *instance,
613 dma_addr_t frame_phys_addr,
614 u32 frame_count,
615 struct megasas_register_set __iomem *regs)
616{
617 unsigned long flags;
618 spin_lock_irqsave(&instance->hba_lock, flags);
619 writel(0, &(regs)->inbound_high_queue_port);
620 writel((frame_phys_addr | (frame_count<<1))|1,
621 &(regs)->inbound_low_queue_port);
622 spin_unlock_irqrestore(&instance->hba_lock, flags);
623}
624
625
626
627
628
629static int
630megasas_check_reset_skinny(struct megasas_instance *instance,
631 struct megasas_register_set __iomem *regs)
632{
633 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
634 return 1;
635
636 return 0;
637}
638
639static struct megasas_instance_template megasas_instance_template_skinny = {
640
641 .fire_cmd = megasas_fire_cmd_skinny,
642 .enable_intr = megasas_enable_intr_skinny,
643 .disable_intr = megasas_disable_intr_skinny,
644 .clear_intr = megasas_clear_intr_skinny,
645 .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
646 .adp_reset = megasas_adp_reset_gen2,
647 .check_reset = megasas_check_reset_skinny,
648 .service_isr = megasas_isr,
649 .tasklet = megasas_complete_cmd_dpc,
650 .init_adapter = megasas_init_adapter_mfi,
651 .build_and_issue_cmd = megasas_build_and_issue_cmd,
652 .issue_dcmd = megasas_issue_dcmd,
653};
654
655
656
657
658
659
660
661
662
663
664
665static inline void
666megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
667{
668 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
669
670
671 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
672
673
674 readl(®s->outbound_intr_mask);
675}
676
677
678
679
680
681static inline void
682megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
683{
684 u32 mask = 0xFFFFFFFF;
685 writel(mask, ®s->outbound_intr_mask);
686
687 readl(®s->outbound_intr_mask);
688}
689
690
691
692
693
694static u32
695megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
696{
697 return readl(&(regs)->outbound_scratch_pad);
698}
699
700
701
702
703
704static int
705megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
706{
707 u32 status;
708 u32 mfiStatus = 0;
709
710
711
712 status = readl(®s->outbound_intr_status);
713
714 if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
715 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
716 }
717 if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
718 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
719 }
720
721
722
723
724 if (mfiStatus)
725 writel(status, ®s->outbound_doorbell_clear);
726
727
728 readl(®s->outbound_intr_status);
729
730 return mfiStatus;
731}
732
733
734
735
736
737
738static inline void
739megasas_fire_cmd_gen2(struct megasas_instance *instance,
740 dma_addr_t frame_phys_addr,
741 u32 frame_count,
742 struct megasas_register_set __iomem *regs)
743{
744 unsigned long flags;
745 spin_lock_irqsave(&instance->hba_lock, flags);
746 writel((frame_phys_addr | (frame_count<<1))|1,
747 &(regs)->inbound_queue_port);
748 spin_unlock_irqrestore(&instance->hba_lock, flags);
749}
750
751
752
753
754
755static int
756megasas_adp_reset_gen2(struct megasas_instance *instance,
757 struct megasas_register_set __iomem *reg_set)
758{
759 u32 retry = 0 ;
760 u32 HostDiag;
761 u32 *seq_offset = ®_set->seq_offset;
762 u32 *hostdiag_offset = ®_set->host_diag;
763
764 if (instance->instancet == &megasas_instance_template_skinny) {
765 seq_offset = ®_set->fusion_seq_offset;
766 hostdiag_offset = ®_set->fusion_host_diag;
767 }
768
769 writel(0, seq_offset);
770 writel(4, seq_offset);
771 writel(0xb, seq_offset);
772 writel(2, seq_offset);
773 writel(7, seq_offset);
774 writel(0xd, seq_offset);
775
776 msleep(1000);
777
778 HostDiag = (u32)readl(hostdiag_offset);
779
780 while ( !( HostDiag & DIAG_WRITE_ENABLE) ) {
781 msleep(100);
782 HostDiag = (u32)readl(hostdiag_offset);
783 printk(KERN_NOTICE "RESETGEN2: retry=%x, hostdiag=%x\n",
784 retry, HostDiag);
785
786 if (retry++ >= 100)
787 return 1;
788
789 }
790
791 printk(KERN_NOTICE "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
792
793 writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
794
795 ssleep(10);
796
797 HostDiag = (u32)readl(hostdiag_offset);
798 while ( ( HostDiag & DIAG_RESET_ADAPTER) ) {
799 msleep(100);
800 HostDiag = (u32)readl(hostdiag_offset);
801 printk(KERN_NOTICE "RESET_GEN2: retry=%x, hostdiag=%x\n",
802 retry, HostDiag);
803
804 if (retry++ >= 1000)
805 return 1;
806
807 }
808 return 0;
809}
810
811
812
813
814
815static int
816megasas_check_reset_gen2(struct megasas_instance *instance,
817 struct megasas_register_set __iomem *regs)
818{
819 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
820 return 1;
821 }
822
823 return 0;
824}
825
826static struct megasas_instance_template megasas_instance_template_gen2 = {
827
828 .fire_cmd = megasas_fire_cmd_gen2,
829 .enable_intr = megasas_enable_intr_gen2,
830 .disable_intr = megasas_disable_intr_gen2,
831 .clear_intr = megasas_clear_intr_gen2,
832 .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
833 .adp_reset = megasas_adp_reset_gen2,
834 .check_reset = megasas_check_reset_gen2,
835 .service_isr = megasas_isr,
836 .tasklet = megasas_complete_cmd_dpc,
837 .init_adapter = megasas_init_adapter_mfi,
838 .build_and_issue_cmd = megasas_build_and_issue_cmd,
839 .issue_dcmd = megasas_issue_dcmd,
840};
841
842
843
844
845
846
847
848
849
850extern struct megasas_instance_template megasas_instance_template_fusion;
851
852
853
854
855
856
857
858
859int
860megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
861{
862
863 struct megasas_header *frame_hdr = &cmd->frame->hdr;
864
865 frame_hdr->cmd_status = 0xFF;
866 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
867
868
869
870
871 instance->instancet->issue_dcmd(instance, cmd);
872
873
874
875
876 return wait_and_poll(instance, cmd);
877}
878
879
880
881
882
883
884
885
886
887
888static int
889megasas_issue_blocked_cmd(struct megasas_instance *instance,
890 struct megasas_cmd *cmd)
891{
892 cmd->cmd_status = ENODATA;
893
894 instance->instancet->issue_dcmd(instance, cmd);
895
896 wait_event(instance->int_cmd_wait_q, cmd->cmd_status != ENODATA);
897
898 return 0;
899}
900
901
902
903
904
905
906
907
908
909
910
911static int
912megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
913 struct megasas_cmd *cmd_to_abort)
914{
915 struct megasas_cmd *cmd;
916 struct megasas_abort_frame *abort_fr;
917
918 cmd = megasas_get_cmd(instance);
919
920 if (!cmd)
921 return -1;
922
923 abort_fr = &cmd->frame->abort;
924
925
926
927
928 abort_fr->cmd = MFI_CMD_ABORT;
929 abort_fr->cmd_status = 0xFF;
930 abort_fr->flags = 0;
931 abort_fr->abort_context = cmd_to_abort->index;
932 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr;
933 abort_fr->abort_mfi_phys_addr_hi = 0;
934
935 cmd->sync_cmd = 1;
936 cmd->cmd_status = 0xFF;
937
938 instance->instancet->issue_dcmd(instance, cmd);
939
940
941
942
943 wait_event(instance->abort_cmd_wait_q, cmd->cmd_status != 0xFF);
944 cmd->sync_cmd = 0;
945
946 megasas_return_cmd(instance, cmd);
947 return 0;
948}
949
950
951
952
953
954
955
956
957
958
959static int
960megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
961 union megasas_sgl *mfi_sgl)
962{
963 int i;
964 int sge_count;
965 struct scatterlist *os_sgl;
966
967 sge_count = scsi_dma_map(scp);
968 BUG_ON(sge_count < 0);
969
970 if (sge_count) {
971 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
972 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl);
973 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl);
974 }
975 }
976 return sge_count;
977}
978
979
980
981
982
983
984
985
986
987
988static int
989megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
990 union megasas_sgl *mfi_sgl)
991{
992 int i;
993 int sge_count;
994 struct scatterlist *os_sgl;
995
996 sge_count = scsi_dma_map(scp);
997 BUG_ON(sge_count < 0);
998
999 if (sge_count) {
1000 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1001 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl);
1002 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl);
1003 }
1004 }
1005 return sge_count;
1006}
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017static int
1018megasas_make_sgl_skinny(struct megasas_instance *instance,
1019 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1020{
1021 int i;
1022 int sge_count;
1023 struct scatterlist *os_sgl;
1024
1025 sge_count = scsi_dma_map(scp);
1026
1027 if (sge_count) {
1028 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1029 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl);
1030 mfi_sgl->sge_skinny[i].phys_addr =
1031 sg_dma_address(os_sgl);
1032 mfi_sgl->sge_skinny[i].flag = 0;
1033 }
1034 }
1035 return sge_count;
1036}
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046static u32 megasas_get_frame_count(struct megasas_instance *instance,
1047 u8 sge_count, u8 frame_type)
1048{
1049 int num_cnt;
1050 int sge_bytes;
1051 u32 sge_sz;
1052 u32 frame_count=0;
1053
1054 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1055 sizeof(struct megasas_sge32);
1056
1057 if (instance->flag_ieee) {
1058 sge_sz = sizeof(struct megasas_sge_skinny);
1059 }
1060
1061
1062
1063
1064
1065
1066
1067 if (unlikely(frame_type == PTHRU_FRAME)) {
1068 if (instance->flag_ieee == 1) {
1069 num_cnt = sge_count - 1;
1070 } else if (IS_DMA64)
1071 num_cnt = sge_count - 1;
1072 else
1073 num_cnt = sge_count - 2;
1074 } else {
1075 if (instance->flag_ieee == 1) {
1076 num_cnt = sge_count - 1;
1077 } else if (IS_DMA64)
1078 num_cnt = sge_count - 2;
1079 else
1080 num_cnt = sge_count - 3;
1081 }
1082
1083 if(num_cnt>0){
1084 sge_bytes = sge_sz * num_cnt;
1085
1086 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1087 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1088 }
1089
1090 frame_count +=1;
1091
1092 if (frame_count > 7)
1093 frame_count = 8;
1094 return frame_count;
1095}
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106static int
1107megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1108 struct megasas_cmd *cmd)
1109{
1110 u32 is_logical;
1111 u32 device_id;
1112 u16 flags = 0;
1113 struct megasas_pthru_frame *pthru;
1114
1115 is_logical = MEGASAS_IS_LOGICAL(scp);
1116 device_id = MEGASAS_DEV_INDEX(instance, scp);
1117 pthru = (struct megasas_pthru_frame *)cmd->frame;
1118
1119 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1120 flags = MFI_FRAME_DIR_WRITE;
1121 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1122 flags = MFI_FRAME_DIR_READ;
1123 else if (scp->sc_data_direction == PCI_DMA_NONE)
1124 flags = MFI_FRAME_DIR_NONE;
1125
1126 if (instance->flag_ieee == 1) {
1127 flags |= MFI_FRAME_IEEE;
1128 }
1129
1130
1131
1132
1133 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1134 pthru->cmd_status = 0x0;
1135 pthru->scsi_status = 0x0;
1136 pthru->target_id = device_id;
1137 pthru->lun = scp->device->lun;
1138 pthru->cdb_len = scp->cmd_len;
1139 pthru->timeout = 0;
1140 pthru->pad_0 = 0;
1141 pthru->flags = flags;
1142 pthru->data_xfer_len = scsi_bufflen(scp);
1143
1144 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1145
1146
1147
1148
1149
1150 if (scp->device->type == TYPE_TAPE) {
1151 if ((scp->request->timeout / HZ) > 0xFFFF)
1152 pthru->timeout = 0xFFFF;
1153 else
1154 pthru->timeout = scp->request->timeout / HZ;
1155 }
1156
1157
1158
1159
1160 if (instance->flag_ieee == 1) {
1161 pthru->flags |= MFI_FRAME_SGL64;
1162 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1163 &pthru->sgl);
1164 } else if (IS_DMA64) {
1165 pthru->flags |= MFI_FRAME_SGL64;
1166 pthru->sge_count = megasas_make_sgl64(instance, scp,
1167 &pthru->sgl);
1168 } else
1169 pthru->sge_count = megasas_make_sgl32(instance, scp,
1170 &pthru->sgl);
1171
1172 if (pthru->sge_count > instance->max_num_sge) {
1173 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n",
1174 pthru->sge_count);
1175 return 0;
1176 }
1177
1178
1179
1180
1181 pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1182 pthru->sense_buf_phys_addr_hi = 0;
1183 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1184
1185
1186
1187
1188
1189 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1190 PTHRU_FRAME);
1191
1192 return cmd->frame_count;
1193}
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203static int
1204megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1205 struct megasas_cmd *cmd)
1206{
1207 u32 device_id;
1208 u8 sc = scp->cmnd[0];
1209 u16 flags = 0;
1210 struct megasas_io_frame *ldio;
1211
1212 device_id = MEGASAS_DEV_INDEX(instance, scp);
1213 ldio = (struct megasas_io_frame *)cmd->frame;
1214
1215 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1216 flags = MFI_FRAME_DIR_WRITE;
1217 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1218 flags = MFI_FRAME_DIR_READ;
1219
1220 if (instance->flag_ieee == 1) {
1221 flags |= MFI_FRAME_IEEE;
1222 }
1223
1224
1225
1226
1227 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1228 ldio->cmd_status = 0x0;
1229 ldio->scsi_status = 0x0;
1230 ldio->target_id = device_id;
1231 ldio->timeout = 0;
1232 ldio->reserved_0 = 0;
1233 ldio->pad_0 = 0;
1234 ldio->flags = flags;
1235 ldio->start_lba_hi = 0;
1236 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1237
1238
1239
1240
1241 if (scp->cmd_len == 6) {
1242 ldio->lba_count = (u32) scp->cmnd[4];
1243 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1244 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1245
1246 ldio->start_lba_lo &= 0x1FFFFF;
1247 }
1248
1249
1250
1251
1252 else if (scp->cmd_len == 10) {
1253 ldio->lba_count = (u32) scp->cmnd[8] |
1254 ((u32) scp->cmnd[7] << 8);
1255 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1256 ((u32) scp->cmnd[3] << 16) |
1257 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1258 }
1259
1260
1261
1262
1263 else if (scp->cmd_len == 12) {
1264 ldio->lba_count = ((u32) scp->cmnd[6] << 24) |
1265 ((u32) scp->cmnd[7] << 16) |
1266 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1267
1268 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1269 ((u32) scp->cmnd[3] << 16) |
1270 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1271 }
1272
1273
1274
1275
1276 else if (scp->cmd_len == 16) {
1277 ldio->lba_count = ((u32) scp->cmnd[10] << 24) |
1278 ((u32) scp->cmnd[11] << 16) |
1279 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1280
1281 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1282 ((u32) scp->cmnd[7] << 16) |
1283 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1284
1285 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1286 ((u32) scp->cmnd[3] << 16) |
1287 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1288
1289 }
1290
1291
1292
1293
1294 if (instance->flag_ieee) {
1295 ldio->flags |= MFI_FRAME_SGL64;
1296 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1297 &ldio->sgl);
1298 } else if (IS_DMA64) {
1299 ldio->flags |= MFI_FRAME_SGL64;
1300 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1301 } else
1302 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1303
1304 if (ldio->sge_count > instance->max_num_sge) {
1305 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n",
1306 ldio->sge_count);
1307 return 0;
1308 }
1309
1310
1311
1312
1313 ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1314 ldio->sense_buf_phys_addr_hi = 0;
1315 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr;
1316
1317
1318
1319
1320
1321 cmd->frame_count = megasas_get_frame_count(instance,
1322 ldio->sge_count, IO_FRAME);
1323
1324 return cmd->frame_count;
1325}
1326
1327
1328
1329
1330
1331
1332
1333
1334inline int megasas_is_ldio(struct scsi_cmnd *cmd)
1335{
1336 if (!MEGASAS_IS_LOGICAL(cmd))
1337 return 0;
1338 switch (cmd->cmnd[0]) {
1339 case READ_10:
1340 case WRITE_10:
1341 case READ_12:
1342 case WRITE_12:
1343 case READ_6:
1344 case WRITE_6:
1345 case READ_16:
1346 case WRITE_16:
1347 return 1;
1348 default:
1349 return 0;
1350 }
1351}
1352
1353
1354
1355
1356
1357
1358static inline void
1359megasas_dump_pending_frames(struct megasas_instance *instance)
1360{
1361 struct megasas_cmd *cmd;
1362 int i,n;
1363 union megasas_sgl *mfi_sgl;
1364 struct megasas_io_frame *ldio;
1365 struct megasas_pthru_frame *pthru;
1366 u32 sgcount;
1367 u32 max_cmd = instance->max_fw_cmds;
1368
1369 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1370 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1371 if (IS_DMA64)
1372 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1373 else
1374 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1375
1376 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1377 for (i = 0; i < max_cmd; i++) {
1378 cmd = instance->cmd_list[i];
1379 if(!cmd->scmd)
1380 continue;
1381 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1382 if (megasas_is_ldio(cmd->scmd)){
1383 ldio = (struct megasas_io_frame *)cmd->frame;
1384 mfi_sgl = &ldio->sgl;
1385 sgcount = ldio->sge_count;
1386 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);
1387 }
1388 else {
1389 pthru = (struct megasas_pthru_frame *) cmd->frame;
1390 mfi_sgl = &pthru->sgl;
1391 sgcount = pthru->sge_count;
1392 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);
1393 }
1394 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){
1395 for (n = 0; n < sgcount; n++){
1396 if (IS_DMA64)
1397 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) ;
1398 else
1399 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ;
1400 }
1401 }
1402 printk(KERN_ERR "\n");
1403 }
1404 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1405 for (i = 0; i < max_cmd; i++) {
1406
1407 cmd = instance->cmd_list[i];
1408
1409 if(cmd->sync_cmd == 1){
1410 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1411 }
1412 }
1413 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no);
1414}
1415
1416u32
1417megasas_build_and_issue_cmd(struct megasas_instance *instance,
1418 struct scsi_cmnd *scmd)
1419{
1420 struct megasas_cmd *cmd;
1421 u32 frame_count;
1422
1423 cmd = megasas_get_cmd(instance);
1424 if (!cmd)
1425 return SCSI_MLQUEUE_HOST_BUSY;
1426
1427
1428
1429
1430 if (megasas_is_ldio(scmd))
1431 frame_count = megasas_build_ldio(instance, scmd, cmd);
1432 else
1433 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1434
1435 if (!frame_count)
1436 goto out_return_cmd;
1437
1438 cmd->scmd = scmd;
1439 scmd->SCp.ptr = (char *)cmd;
1440
1441
1442
1443
1444 atomic_inc(&instance->fw_outstanding);
1445
1446 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1447 cmd->frame_count-1, instance->reg_set);
1448
1449 return 0;
1450out_return_cmd:
1451 megasas_return_cmd(instance, cmd);
1452 return 1;
1453}
1454
1455
1456
1457
1458
1459
1460
1461static int
1462megasas_queue_command_lck(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *))
1463{
1464 struct megasas_instance *instance;
1465 unsigned long flags;
1466
1467 instance = (struct megasas_instance *)
1468 scmd->device->host->hostdata;
1469
1470 if (instance->issuepend_done == 0)
1471 return SCSI_MLQUEUE_HOST_BUSY;
1472
1473 spin_lock_irqsave(&instance->hba_lock, flags);
1474 if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1475 spin_unlock_irqrestore(&instance->hba_lock, flags);
1476 return SCSI_MLQUEUE_HOST_BUSY;
1477 }
1478
1479 spin_unlock_irqrestore(&instance->hba_lock, flags);
1480
1481 scmd->scsi_done = done;
1482 scmd->result = 0;
1483
1484 if (MEGASAS_IS_LOGICAL(scmd) &&
1485 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) {
1486 scmd->result = DID_BAD_TARGET << 16;
1487 goto out_done;
1488 }
1489
1490 switch (scmd->cmnd[0]) {
1491 case SYNCHRONIZE_CACHE:
1492
1493
1494
1495
1496 scmd->result = DID_OK << 16;
1497 goto out_done;
1498 default:
1499 break;
1500 }
1501
1502 if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1503 printk(KERN_ERR "megasas: Err returned from build_and_issue_cmd\n");
1504 return SCSI_MLQUEUE_HOST_BUSY;
1505 }
1506
1507 return 0;
1508
1509 out_done:
1510 done(scmd);
1511 return 0;
1512}
1513
1514static DEF_SCSI_QCMD(megasas_queue_command)
1515
1516static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1517{
1518 int i;
1519
1520 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1521
1522 if ((megasas_mgmt_info.instance[i]) &&
1523 (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1524 return megasas_mgmt_info.instance[i];
1525 }
1526
1527 return NULL;
1528}
1529
1530static int megasas_slave_configure(struct scsi_device *sdev)
1531{
1532 u16 pd_index = 0;
1533 struct megasas_instance *instance ;
1534
1535 instance = megasas_lookup_instance(sdev->host->host_no);
1536
1537
1538
1539
1540
1541
1542
1543
1544 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1545 sdev->type == TYPE_DISK) {
1546 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1547 sdev->id;
1548 if (instance->pd_list[pd_index].driveState ==
1549 MR_PD_STATE_SYSTEM) {
1550 blk_queue_rq_timeout(sdev->request_queue,
1551 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1552 return 0;
1553 }
1554 return -ENXIO;
1555 }
1556
1557
1558
1559
1560 blk_queue_rq_timeout(sdev->request_queue,
1561 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1562 return 0;
1563}
1564
1565static int megasas_slave_alloc(struct scsi_device *sdev)
1566{
1567 u16 pd_index = 0;
1568 struct megasas_instance *instance ;
1569 instance = megasas_lookup_instance(sdev->host->host_no);
1570 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) &&
1571 (sdev->type == TYPE_DISK)) {
1572
1573
1574
1575 pd_index =
1576 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1577 sdev->id;
1578 if ((instance->pd_list[pd_index].driveState ==
1579 MR_PD_STATE_SYSTEM) &&
1580 (instance->pd_list[pd_index].driveType ==
1581 TYPE_DISK)) {
1582 return 0;
1583 }
1584 return -ENXIO;
1585 }
1586 return 0;
1587}
1588
1589void megaraid_sas_kill_hba(struct megasas_instance *instance)
1590{
1591 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1592 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1593 (instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1594 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
1595 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
1596 } else {
1597 writel(MFI_STOP_ADP, &instance->reg_set->inbound_doorbell);
1598 }
1599}
1600
1601
1602
1603
1604
1605
1606
1607void
1608megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1609{
1610 unsigned long flags;
1611 if (instance->flag & MEGASAS_FW_BUSY
1612 && time_after(jiffies, instance->last_time + 5 * HZ)
1613 && atomic_read(&instance->fw_outstanding) <
1614 instance->throttlequeuedepth + 1) {
1615
1616 spin_lock_irqsave(instance->host->host_lock, flags);
1617 instance->flag &= ~MEGASAS_FW_BUSY;
1618 if ((instance->pdev->device ==
1619 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1620 (instance->pdev->device ==
1621 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1622 instance->host->can_queue =
1623 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
1624 } else
1625 instance->host->can_queue =
1626 instance->max_fw_cmds - MEGASAS_INT_CMDS;
1627
1628 spin_unlock_irqrestore(instance->host->host_lock, flags);
1629 }
1630}
1631
1632
1633
1634
1635
1636
1637
1638static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1639{
1640 u32 producer;
1641 u32 consumer;
1642 u32 context;
1643 struct megasas_cmd *cmd;
1644 struct megasas_instance *instance =
1645 (struct megasas_instance *)instance_addr;
1646 unsigned long flags;
1647
1648
1649 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR )
1650 return;
1651
1652 spin_lock_irqsave(&instance->completion_lock, flags);
1653
1654 producer = *instance->producer;
1655 consumer = *instance->consumer;
1656
1657 while (consumer != producer) {
1658 context = instance->reply_queue[consumer];
1659 if (context >= instance->max_fw_cmds) {
1660 printk(KERN_ERR "Unexpected context value %x\n",
1661 context);
1662 BUG();
1663 }
1664
1665 cmd = instance->cmd_list[context];
1666
1667 megasas_complete_cmd(instance, cmd, DID_OK);
1668
1669 consumer++;
1670 if (consumer == (instance->max_fw_cmds + 1)) {
1671 consumer = 0;
1672 }
1673 }
1674
1675 *instance->consumer = producer;
1676
1677 spin_unlock_irqrestore(&instance->completion_lock, flags);
1678
1679
1680
1681
1682 megasas_check_and_restore_queue_depth(instance);
1683}
1684
1685static void
1686megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1687
1688static void
1689process_fw_state_change_wq(struct work_struct *work);
1690
1691void megasas_do_ocr(struct megasas_instance *instance)
1692{
1693 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1694 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1695 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1696 *instance->consumer = MEGASAS_ADPRESET_INPROG_SIGN;
1697 }
1698 instance->instancet->disable_intr(instance->reg_set);
1699 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
1700 instance->issuepend_done = 0;
1701
1702 atomic_set(&instance->fw_outstanding, 0);
1703 megasas_internal_reset_defer_cmds(instance);
1704 process_fw_state_change_wq(&instance->work_init);
1705}
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1716{
1717 int i;
1718 u32 reset_index;
1719 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
1720 u8 adprecovery;
1721 unsigned long flags;
1722 struct list_head clist_local;
1723 struct megasas_cmd *reset_cmd;
1724 u32 fw_state;
1725 u8 kill_adapter_flag;
1726
1727 spin_lock_irqsave(&instance->hba_lock, flags);
1728 adprecovery = instance->adprecovery;
1729 spin_unlock_irqrestore(&instance->hba_lock, flags);
1730
1731 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1732
1733 INIT_LIST_HEAD(&clist_local);
1734 spin_lock_irqsave(&instance->hba_lock, flags);
1735 list_splice_init(&instance->internal_reset_pending_q,
1736 &clist_local);
1737 spin_unlock_irqrestore(&instance->hba_lock, flags);
1738
1739 printk(KERN_NOTICE "megasas: HBA reset wait ...\n");
1740 for (i = 0; i < wait_time; i++) {
1741 msleep(1000);
1742 spin_lock_irqsave(&instance->hba_lock, flags);
1743 adprecovery = instance->adprecovery;
1744 spin_unlock_irqrestore(&instance->hba_lock, flags);
1745 if (adprecovery == MEGASAS_HBA_OPERATIONAL)
1746 break;
1747 }
1748
1749 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
1750 printk(KERN_NOTICE "megasas: reset: Stopping HBA.\n");
1751 spin_lock_irqsave(&instance->hba_lock, flags);
1752 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1753 spin_unlock_irqrestore(&instance->hba_lock, flags);
1754 return FAILED;
1755 }
1756
1757 reset_index = 0;
1758 while (!list_empty(&clist_local)) {
1759 reset_cmd = list_entry((&clist_local)->next,
1760 struct megasas_cmd, list);
1761 list_del_init(&reset_cmd->list);
1762 if (reset_cmd->scmd) {
1763 reset_cmd->scmd->result = DID_RESET << 16;
1764 printk(KERN_NOTICE "%d:%p reset [%02x]\n",
1765 reset_index, reset_cmd,
1766 reset_cmd->scmd->cmnd[0]);
1767
1768 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
1769 megasas_return_cmd(instance, reset_cmd);
1770 } else if (reset_cmd->sync_cmd) {
1771 printk(KERN_NOTICE "megasas:%p synch cmds"
1772 "reset queue\n",
1773 reset_cmd);
1774
1775 reset_cmd->cmd_status = ENODATA;
1776 instance->instancet->fire_cmd(instance,
1777 reset_cmd->frame_phys_addr,
1778 0, instance->reg_set);
1779 } else {
1780 printk(KERN_NOTICE "megasas: %p unexpected"
1781 "cmds lst\n",
1782 reset_cmd);
1783 }
1784 reset_index++;
1785 }
1786
1787 return SUCCESS;
1788 }
1789
1790 for (i = 0; i < resetwaittime; i++) {
1791
1792 int outstanding = atomic_read(&instance->fw_outstanding);
1793
1794 if (!outstanding)
1795 break;
1796
1797 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
1798 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
1799 "commands to complete\n",i,outstanding);
1800
1801
1802
1803
1804 megasas_complete_cmd_dpc((unsigned long)instance);
1805 }
1806
1807 msleep(1000);
1808 }
1809
1810 i = 0;
1811 kill_adapter_flag = 0;
1812 do {
1813 fw_state = instance->instancet->read_fw_status_reg(
1814 instance->reg_set) & MFI_STATE_MASK;
1815 if ((fw_state == MFI_STATE_FAULT) &&
1816 (instance->disableOnlineCtrlReset == 0)) {
1817 if (i == 3) {
1818 kill_adapter_flag = 2;
1819 break;
1820 }
1821 megasas_do_ocr(instance);
1822 kill_adapter_flag = 1;
1823
1824
1825 msleep(1000);
1826 }
1827 i++;
1828 } while (i <= 3);
1829
1830 if (atomic_read(&instance->fw_outstanding) &&
1831 !kill_adapter_flag) {
1832 if (instance->disableOnlineCtrlReset == 0) {
1833
1834 megasas_do_ocr(instance);
1835
1836
1837 for (i = 0; i < wait_time; i++) {
1838 int outstanding =
1839 atomic_read(&instance->fw_outstanding);
1840 if (!outstanding)
1841 return SUCCESS;
1842 msleep(1000);
1843 }
1844 }
1845 }
1846
1847 if (atomic_read(&instance->fw_outstanding) ||
1848 (kill_adapter_flag == 2)) {
1849 printk(KERN_NOTICE "megaraid_sas: pending cmds after reset\n");
1850
1851
1852
1853
1854 if ((instance->pdev->device ==
1855 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1856 (instance->pdev->device ==
1857 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
1858 writel(MFI_STOP_ADP,
1859 &instance->reg_set->doorbell);
1860 } else {
1861 writel(MFI_STOP_ADP,
1862 &instance->reg_set->inbound_doorbell);
1863 }
1864 megasas_dump_pending_frames(instance);
1865 spin_lock_irqsave(&instance->hba_lock, flags);
1866 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1867 spin_unlock_irqrestore(&instance->hba_lock, flags);
1868 return FAILED;
1869 }
1870
1871 printk(KERN_NOTICE "megaraid_sas: no pending cmds after reset\n");
1872
1873 return SUCCESS;
1874}
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884static int megasas_generic_reset(struct scsi_cmnd *scmd)
1885{
1886 int ret_val;
1887 struct megasas_instance *instance;
1888
1889 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1890
1891 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
1892 scmd->cmnd[0], scmd->retries);
1893
1894 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1895 printk(KERN_ERR "megasas: cannot recover from previous reset "
1896 "failures\n");
1897 return FAILED;
1898 }
1899
1900 ret_val = megasas_wait_for_outstanding(instance);
1901 if (ret_val == SUCCESS)
1902 printk(KERN_NOTICE "megasas: reset successful \n");
1903 else
1904 printk(KERN_ERR "megasas: failed to do reset\n");
1905
1906 return ret_val;
1907}
1908
1909
1910
1911
1912
1913
1914
1915
1916static enum
1917blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
1918{
1919 struct megasas_instance *instance;
1920 unsigned long flags;
1921
1922 if (time_after(jiffies, scmd->jiffies_at_alloc +
1923 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
1924 return BLK_EH_NOT_HANDLED;
1925 }
1926
1927 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1928 if (!(instance->flag & MEGASAS_FW_BUSY)) {
1929
1930 spin_lock_irqsave(instance->host->host_lock, flags);
1931
1932 instance->host->can_queue = instance->throttlequeuedepth;
1933 instance->last_time = jiffies;
1934 instance->flag |= MEGASAS_FW_BUSY;
1935
1936 spin_unlock_irqrestore(instance->host->host_lock, flags);
1937 }
1938 return BLK_EH_RESET_TIMER;
1939}
1940
1941
1942
1943
1944static int megasas_reset_device(struct scsi_cmnd *scmd)
1945{
1946 int ret;
1947
1948
1949
1950
1951 ret = megasas_generic_reset(scmd);
1952
1953 return ret;
1954}
1955
1956
1957
1958
1959static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
1960{
1961 int ret;
1962 struct megasas_instance *instance;
1963 instance = (struct megasas_instance *)scmd->device->host->hostdata;
1964
1965
1966
1967
1968 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
1969 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
1970 ret = megasas_reset_fusion(scmd->device->host);
1971 else
1972 ret = megasas_generic_reset(scmd);
1973
1974 return ret;
1975}
1976
1977
1978
1979
1980
1981
1982
1983
1984static int
1985megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1986 sector_t capacity, int geom[])
1987{
1988 int heads;
1989 int sectors;
1990 sector_t cylinders;
1991 unsigned long tmp;
1992
1993 heads = 64;
1994 sectors = 32;
1995
1996 tmp = heads * sectors;
1997 cylinders = capacity;
1998
1999 sector_div(cylinders, tmp);
2000
2001
2002
2003
2004
2005 if (capacity >= 0x200000) {
2006 heads = 255;
2007 sectors = 63;
2008 tmp = heads*sectors;
2009 cylinders = capacity;
2010 sector_div(cylinders, tmp);
2011 }
2012
2013 geom[0] = heads;
2014 geom[1] = sectors;
2015 geom[2] = cylinders;
2016
2017 return 0;
2018}
2019
2020static void megasas_aen_polling(struct work_struct *work);
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034static void
2035megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2036{
2037 unsigned long flags;
2038
2039
2040
2041 if ((!cmd->abort_aen) && (instance->unload == 0)) {
2042 spin_lock_irqsave(&poll_aen_lock, flags);
2043 megasas_poll_wait_aen = 1;
2044 spin_unlock_irqrestore(&poll_aen_lock, flags);
2045 wake_up(&megasas_poll_wait);
2046 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
2047 }
2048 else
2049 cmd->abort_aen = 0;
2050
2051 instance->aen_cmd = NULL;
2052 megasas_return_cmd(instance, cmd);
2053
2054 if ((instance->unload == 0) &&
2055 ((instance->issuepend_done == 1))) {
2056 struct megasas_aen_event *ev;
2057 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2058 if (!ev) {
2059 printk(KERN_ERR "megasas_service_aen: out of memory\n");
2060 } else {
2061 ev->instance = instance;
2062 instance->ev = ev;
2063 INIT_DELAYED_WORK(&ev->hotplug_work,
2064 megasas_aen_polling);
2065 schedule_delayed_work(&ev->hotplug_work, 0);
2066 }
2067 }
2068}
2069
2070static int megasas_change_queue_depth(struct scsi_device *sdev,
2071 int queue_depth, int reason)
2072{
2073 if (reason != SCSI_QDEPTH_DEFAULT)
2074 return -EOPNOTSUPP;
2075
2076 if (queue_depth > sdev->host->can_queue)
2077 queue_depth = sdev->host->can_queue;
2078 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev),
2079 queue_depth);
2080
2081 return queue_depth;
2082}
2083
2084
2085
2086
2087static struct scsi_host_template megasas_template = {
2088
2089 .module = THIS_MODULE,
2090 .name = "LSI SAS based MegaRAID driver",
2091 .proc_name = "megaraid_sas",
2092 .slave_configure = megasas_slave_configure,
2093 .slave_alloc = megasas_slave_alloc,
2094 .queuecommand = megasas_queue_command,
2095 .eh_device_reset_handler = megasas_reset_device,
2096 .eh_bus_reset_handler = megasas_reset_bus_host,
2097 .eh_host_reset_handler = megasas_reset_bus_host,
2098 .eh_timed_out = megasas_reset_timer,
2099 .bios_param = megasas_bios_param,
2100 .use_clustering = ENABLE_CLUSTERING,
2101 .change_queue_depth = megasas_change_queue_depth,
2102};
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113static void
2114megasas_complete_int_cmd(struct megasas_instance *instance,
2115 struct megasas_cmd *cmd)
2116{
2117 cmd->cmd_status = cmd->frame->io.cmd_status;
2118
2119 if (cmd->cmd_status == ENODATA) {
2120 cmd->cmd_status = 0;
2121 }
2122 wake_up(&instance->int_cmd_wait_q);
2123}
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134static void
2135megasas_complete_abort(struct megasas_instance *instance,
2136 struct megasas_cmd *cmd)
2137{
2138 if (cmd->sync_cmd) {
2139 cmd->sync_cmd = 0;
2140 cmd->cmd_status = 0;
2141 wake_up(&instance->abort_cmd_wait_q);
2142 }
2143
2144 return;
2145}
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157void
2158megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2159 u8 alt_status)
2160{
2161 int exception = 0;
2162 struct megasas_header *hdr = &cmd->frame->hdr;
2163 unsigned long flags;
2164 struct fusion_context *fusion = instance->ctrl_context;
2165
2166
2167 cmd->retry_for_fw_reset = 0;
2168
2169 if (cmd->scmd)
2170 cmd->scmd->SCp.ptr = NULL;
2171
2172 switch (hdr->cmd) {
2173 case MFI_CMD_INVALID:
2174
2175
2176
2177
2178 printk(KERN_WARNING "megaraid_sas: MFI_CMD_INVALID command "
2179 "completed.\n");
2180 printk(KERN_WARNING "megaraid_sas: If you have a controller "
2181 "other than PERC5, please upgrade your firmware.\n");
2182 break;
2183 case MFI_CMD_PD_SCSI_IO:
2184 case MFI_CMD_LD_SCSI_IO:
2185
2186
2187
2188
2189
2190
2191 if (cmd->sync_cmd) {
2192 cmd->sync_cmd = 0;
2193 megasas_complete_int_cmd(instance, cmd);
2194 break;
2195 }
2196
2197 case MFI_CMD_LD_READ:
2198 case MFI_CMD_LD_WRITE:
2199
2200 if (alt_status) {
2201 cmd->scmd->result = alt_status << 16;
2202 exception = 1;
2203 }
2204
2205 if (exception) {
2206
2207 atomic_dec(&instance->fw_outstanding);
2208
2209 scsi_dma_unmap(cmd->scmd);
2210 cmd->scmd->scsi_done(cmd->scmd);
2211 megasas_return_cmd(instance, cmd);
2212
2213 break;
2214 }
2215
2216 switch (hdr->cmd_status) {
2217
2218 case MFI_STAT_OK:
2219 cmd->scmd->result = DID_OK << 16;
2220 break;
2221
2222 case MFI_STAT_SCSI_IO_FAILED:
2223 case MFI_STAT_LD_INIT_IN_PROGRESS:
2224 cmd->scmd->result =
2225 (DID_ERROR << 16) | hdr->scsi_status;
2226 break;
2227
2228 case MFI_STAT_SCSI_DONE_WITH_ERROR:
2229
2230 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
2231
2232 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
2233 memset(cmd->scmd->sense_buffer, 0,
2234 SCSI_SENSE_BUFFERSIZE);
2235 memcpy(cmd->scmd->sense_buffer, cmd->sense,
2236 hdr->sense_len);
2237
2238 cmd->scmd->result |= DRIVER_SENSE << 24;
2239 }
2240
2241 break;
2242
2243 case MFI_STAT_LD_OFFLINE:
2244 case MFI_STAT_DEVICE_NOT_FOUND:
2245 cmd->scmd->result = DID_BAD_TARGET << 16;
2246 break;
2247
2248 default:
2249 printk(KERN_DEBUG "megasas: MFI FW status %#x\n",
2250 hdr->cmd_status);
2251 cmd->scmd->result = DID_ERROR << 16;
2252 break;
2253 }
2254
2255 atomic_dec(&instance->fw_outstanding);
2256
2257 scsi_dma_unmap(cmd->scmd);
2258 cmd->scmd->scsi_done(cmd->scmd);
2259 megasas_return_cmd(instance, cmd);
2260
2261 break;
2262
2263 case MFI_CMD_SMP:
2264 case MFI_CMD_STP:
2265 case MFI_CMD_DCMD:
2266
2267 if ((cmd->frame->dcmd.opcode == MR_DCMD_LD_MAP_GET_INFO) &&
2268 (cmd->frame->dcmd.mbox.b[1] == 1)) {
2269 spin_lock_irqsave(instance->host->host_lock, flags);
2270 if (cmd->frame->hdr.cmd_status != 0) {
2271 if (cmd->frame->hdr.cmd_status !=
2272 MFI_STAT_NOT_FOUND)
2273 printk(KERN_WARNING "megasas: map sync"
2274 "failed, status = 0x%x.\n",
2275 cmd->frame->hdr.cmd_status);
2276 else {
2277 megasas_return_cmd(instance, cmd);
2278 spin_unlock_irqrestore(
2279 instance->host->host_lock,
2280 flags);
2281 break;
2282 }
2283 } else
2284 instance->map_id++;
2285 megasas_return_cmd(instance, cmd);
2286 if (MR_ValidateMapInfo(
2287 fusion->ld_map[(instance->map_id & 1)],
2288 fusion->load_balance_info))
2289 fusion->fast_path_io = 1;
2290 else
2291 fusion->fast_path_io = 0;
2292 megasas_sync_map_info(instance);
2293 spin_unlock_irqrestore(instance->host->host_lock,
2294 flags);
2295 break;
2296 }
2297 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
2298 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) {
2299 spin_lock_irqsave(&poll_aen_lock, flags);
2300 megasas_poll_wait_aen = 0;
2301 spin_unlock_irqrestore(&poll_aen_lock, flags);
2302 }
2303
2304
2305
2306
2307 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT)
2308 megasas_service_aen(instance, cmd);
2309 else
2310 megasas_complete_int_cmd(instance, cmd);
2311
2312 break;
2313
2314 case MFI_CMD_ABORT:
2315
2316
2317
2318 megasas_complete_abort(instance, cmd);
2319 break;
2320
2321 default:
2322 printk("megasas: Unknown command completed! [0x%X]\n",
2323 hdr->cmd);
2324 break;
2325 }
2326}
2327
2328
2329
2330
2331
2332
2333static inline void
2334megasas_issue_pending_cmds_again(struct megasas_instance *instance)
2335{
2336 struct megasas_cmd *cmd;
2337 struct list_head clist_local;
2338 union megasas_evt_class_locale class_locale;
2339 unsigned long flags;
2340 u32 seq_num;
2341
2342 INIT_LIST_HEAD(&clist_local);
2343 spin_lock_irqsave(&instance->hba_lock, flags);
2344 list_splice_init(&instance->internal_reset_pending_q, &clist_local);
2345 spin_unlock_irqrestore(&instance->hba_lock, flags);
2346
2347 while (!list_empty(&clist_local)) {
2348 cmd = list_entry((&clist_local)->next,
2349 struct megasas_cmd, list);
2350 list_del_init(&cmd->list);
2351
2352 if (cmd->sync_cmd || cmd->scmd) {
2353 printk(KERN_NOTICE "megaraid_sas: command %p, %p:%d"
2354 "detected to be pending while HBA reset.\n",
2355 cmd, cmd->scmd, cmd->sync_cmd);
2356
2357 cmd->retry_for_fw_reset++;
2358
2359 if (cmd->retry_for_fw_reset == 3) {
2360 printk(KERN_NOTICE "megaraid_sas: cmd %p, %p:%d"
2361 "was tried multiple times during reset."
2362 "Shutting down the HBA\n",
2363 cmd, cmd->scmd, cmd->sync_cmd);
2364 megaraid_sas_kill_hba(instance);
2365
2366 instance->adprecovery =
2367 MEGASAS_HW_CRITICAL_ERROR;
2368 return;
2369 }
2370 }
2371
2372 if (cmd->sync_cmd == 1) {
2373 if (cmd->scmd) {
2374 printk(KERN_NOTICE "megaraid_sas: unexpected"
2375 "cmd attached to internal command!\n");
2376 }
2377 printk(KERN_NOTICE "megasas: %p synchronous cmd"
2378 "on the internal reset queue,"
2379 "issue it again.\n", cmd);
2380 cmd->cmd_status = ENODATA;
2381 instance->instancet->fire_cmd(instance,
2382 cmd->frame_phys_addr ,
2383 0, instance->reg_set);
2384 } else if (cmd->scmd) {
2385 printk(KERN_NOTICE "megasas: %p scsi cmd [%02x]"
2386 "detected on the internal queue, issue again.\n",
2387 cmd, cmd->scmd->cmnd[0]);
2388
2389 atomic_inc(&instance->fw_outstanding);
2390 instance->instancet->fire_cmd(instance,
2391 cmd->frame_phys_addr,
2392 cmd->frame_count-1, instance->reg_set);
2393 } else {
2394 printk(KERN_NOTICE "megasas: %p unexpected cmd on the"
2395 "internal reset defer list while re-issue!!\n",
2396 cmd);
2397 }
2398 }
2399
2400 if (instance->aen_cmd) {
2401 printk(KERN_NOTICE "megaraid_sas: aen_cmd in def process\n");
2402 megasas_return_cmd(instance, instance->aen_cmd);
2403
2404 instance->aen_cmd = NULL;
2405 }
2406
2407
2408
2409
2410 seq_num = instance->last_seq_num;
2411 class_locale.members.reserved = 0;
2412 class_locale.members.locale = MR_EVT_LOCALE_ALL;
2413 class_locale.members.class = MR_EVT_CLASS_DEBUG;
2414
2415 megasas_register_aen(instance, seq_num, class_locale.word);
2416}
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427static void
2428megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
2429{
2430 struct megasas_cmd *cmd;
2431 int i;
2432 u32 max_cmd = instance->max_fw_cmds;
2433 u32 defer_index;
2434 unsigned long flags;
2435
2436 defer_index = 0;
2437 spin_lock_irqsave(&instance->cmd_pool_lock, flags);
2438 for (i = 0; i < max_cmd; i++) {
2439 cmd = instance->cmd_list[i];
2440 if (cmd->sync_cmd == 1 || cmd->scmd) {
2441 printk(KERN_NOTICE "megasas: moving cmd[%d]:%p:%d:%p"
2442 "on the defer queue as internal\n",
2443 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
2444
2445 if (!list_empty(&cmd->list)) {
2446 printk(KERN_NOTICE "megaraid_sas: ERROR while"
2447 " moving this cmd:%p, %d %p, it was"
2448 "discovered on some list?\n",
2449 cmd, cmd->sync_cmd, cmd->scmd);
2450
2451 list_del_init(&cmd->list);
2452 }
2453 defer_index++;
2454 list_add_tail(&cmd->list,
2455 &instance->internal_reset_pending_q);
2456 }
2457 }
2458 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags);
2459}
2460
2461
2462static void
2463process_fw_state_change_wq(struct work_struct *work)
2464{
2465 struct megasas_instance *instance =
2466 container_of(work, struct megasas_instance, work_init);
2467 u32 wait;
2468 unsigned long flags;
2469
2470 if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
2471 printk(KERN_NOTICE "megaraid_sas: error, recovery st %x \n",
2472 instance->adprecovery);
2473 return ;
2474 }
2475
2476 if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
2477 printk(KERN_NOTICE "megaraid_sas: FW detected to be in fault"
2478 "state, restarting it...\n");
2479
2480 instance->instancet->disable_intr(instance->reg_set);
2481 atomic_set(&instance->fw_outstanding, 0);
2482
2483 atomic_set(&instance->fw_reset_no_pci_access, 1);
2484 instance->instancet->adp_reset(instance, instance->reg_set);
2485 atomic_set(&instance->fw_reset_no_pci_access, 0 );
2486
2487 printk(KERN_NOTICE "megaraid_sas: FW restarted successfully,"
2488 "initiating next stage...\n");
2489
2490 printk(KERN_NOTICE "megaraid_sas: HBA recovery state machine,"
2491 "state 2 starting...\n");
2492
2493
2494 for (wait = 0; wait < 30; wait++) {
2495 msleep(1000);
2496 }
2497
2498 if (megasas_transition_to_ready(instance, 1)) {
2499 printk(KERN_NOTICE "megaraid_sas:adapter not ready\n");
2500
2501 megaraid_sas_kill_hba(instance);
2502 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2503 return ;
2504 }
2505
2506 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2507 (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2508 (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
2509 ) {
2510 *instance->consumer = *instance->producer;
2511 } else {
2512 *instance->consumer = 0;
2513 *instance->producer = 0;
2514 }
2515
2516 megasas_issue_init_mfi(instance);
2517
2518 spin_lock_irqsave(&instance->hba_lock, flags);
2519 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2520 spin_unlock_irqrestore(&instance->hba_lock, flags);
2521 instance->instancet->enable_intr(instance->reg_set);
2522
2523 megasas_issue_pending_cmds_again(instance);
2524 instance->issuepend_done = 1;
2525 }
2526 return ;
2527}
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537static int
2538megasas_deplete_reply_queue(struct megasas_instance *instance,
2539 u8 alt_status)
2540{
2541 u32 mfiStatus;
2542 u32 fw_state;
2543
2544 if ((mfiStatus = instance->instancet->check_reset(instance,
2545 instance->reg_set)) == 1) {
2546 return IRQ_HANDLED;
2547 }
2548
2549 if ((mfiStatus = instance->instancet->clear_intr(
2550 instance->reg_set)
2551 ) == 0) {
2552
2553 if (!instance->msix_vectors)
2554 return IRQ_NONE;
2555 }
2556
2557 instance->mfiStatus = mfiStatus;
2558
2559 if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
2560 fw_state = instance->instancet->read_fw_status_reg(
2561 instance->reg_set) & MFI_STATE_MASK;
2562
2563 if (fw_state != MFI_STATE_FAULT) {
2564 printk(KERN_NOTICE "megaraid_sas: fw state:%x\n",
2565 fw_state);
2566 }
2567
2568 if ((fw_state == MFI_STATE_FAULT) &&
2569 (instance->disableOnlineCtrlReset == 0)) {
2570 printk(KERN_NOTICE "megaraid_sas: wait adp restart\n");
2571
2572 if ((instance->pdev->device ==
2573 PCI_DEVICE_ID_LSI_SAS1064R) ||
2574 (instance->pdev->device ==
2575 PCI_DEVICE_ID_DELL_PERC5) ||
2576 (instance->pdev->device ==
2577 PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2578
2579 *instance->consumer =
2580 MEGASAS_ADPRESET_INPROG_SIGN;
2581 }
2582
2583
2584 instance->instancet->disable_intr(instance->reg_set);
2585 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2586 instance->issuepend_done = 0;
2587
2588 atomic_set(&instance->fw_outstanding, 0);
2589 megasas_internal_reset_defer_cmds(instance);
2590
2591 printk(KERN_NOTICE "megasas: fwState=%x, stage:%d\n",
2592 fw_state, instance->adprecovery);
2593
2594 schedule_work(&instance->work_init);
2595 return IRQ_HANDLED;
2596
2597 } else {
2598 printk(KERN_NOTICE "megasas: fwstate:%x, dis_OCR=%x\n",
2599 fw_state, instance->disableOnlineCtrlReset);
2600 }
2601 }
2602
2603 tasklet_schedule(&instance->isr_tasklet);
2604 return IRQ_HANDLED;
2605}
2606
2607
2608
2609static irqreturn_t megasas_isr(int irq, void *devp)
2610{
2611 struct megasas_irq_context *irq_context = devp;
2612 struct megasas_instance *instance = irq_context->instance;
2613 unsigned long flags;
2614 irqreturn_t rc;
2615
2616 if (atomic_read(&instance->fw_reset_no_pci_access))
2617 return IRQ_HANDLED;
2618
2619 spin_lock_irqsave(&instance->hba_lock, flags);
2620 rc = megasas_deplete_reply_queue(instance, DID_OK);
2621 spin_unlock_irqrestore(&instance->hba_lock, flags);
2622
2623 return rc;
2624}
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635int
2636megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
2637{
2638 int i;
2639 u8 max_wait;
2640 u32 fw_state;
2641 u32 cur_state;
2642 u32 abs_state, curr_abs_state;
2643
2644 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2645
2646 if (fw_state != MFI_STATE_READY)
2647 printk(KERN_INFO "megasas: Waiting for FW to come to ready"
2648 " state\n");
2649
2650 while (fw_state != MFI_STATE_READY) {
2651
2652 abs_state =
2653 instance->instancet->read_fw_status_reg(instance->reg_set);
2654
2655 switch (fw_state) {
2656
2657 case MFI_STATE_FAULT:
2658 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n");
2659 if (ocr) {
2660 max_wait = MEGASAS_RESET_WAIT_TIME;
2661 cur_state = MFI_STATE_FAULT;
2662 break;
2663 } else
2664 return -ENODEV;
2665
2666 case MFI_STATE_WAIT_HANDSHAKE:
2667
2668
2669
2670 if ((instance->pdev->device ==
2671 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2672 (instance->pdev->device ==
2673 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2674 (instance->pdev->device ==
2675 PCI_DEVICE_ID_LSI_FUSION) ||
2676 (instance->pdev->device ==
2677 PCI_DEVICE_ID_LSI_INVADER)) {
2678 writel(
2679 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2680 &instance->reg_set->doorbell);
2681 } else {
2682 writel(
2683 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
2684 &instance->reg_set->inbound_doorbell);
2685 }
2686
2687 max_wait = MEGASAS_RESET_WAIT_TIME;
2688 cur_state = MFI_STATE_WAIT_HANDSHAKE;
2689 break;
2690
2691 case MFI_STATE_BOOT_MESSAGE_PENDING:
2692 if ((instance->pdev->device ==
2693 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2694 (instance->pdev->device ==
2695 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2696 (instance->pdev->device ==
2697 PCI_DEVICE_ID_LSI_FUSION) ||
2698 (instance->pdev->device ==
2699 PCI_DEVICE_ID_LSI_INVADER)) {
2700 writel(MFI_INIT_HOTPLUG,
2701 &instance->reg_set->doorbell);
2702 } else
2703 writel(MFI_INIT_HOTPLUG,
2704 &instance->reg_set->inbound_doorbell);
2705
2706 max_wait = MEGASAS_RESET_WAIT_TIME;
2707 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
2708 break;
2709
2710 case MFI_STATE_OPERATIONAL:
2711
2712
2713
2714 instance->instancet->disable_intr(instance->reg_set);
2715 if ((instance->pdev->device ==
2716 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2717 (instance->pdev->device ==
2718 PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2719 (instance->pdev->device
2720 == PCI_DEVICE_ID_LSI_FUSION) ||
2721 (instance->pdev->device
2722 == PCI_DEVICE_ID_LSI_INVADER)) {
2723 writel(MFI_RESET_FLAGS,
2724 &instance->reg_set->doorbell);
2725 if ((instance->pdev->device ==
2726 PCI_DEVICE_ID_LSI_FUSION) ||
2727 (instance->pdev->device ==
2728 PCI_DEVICE_ID_LSI_INVADER)) {
2729 for (i = 0; i < (10 * 1000); i += 20) {
2730 if (readl(
2731 &instance->
2732 reg_set->
2733 doorbell) & 1)
2734 msleep(20);
2735 else
2736 break;
2737 }
2738 }
2739 } else
2740 writel(MFI_RESET_FLAGS,
2741 &instance->reg_set->inbound_doorbell);
2742
2743 max_wait = MEGASAS_RESET_WAIT_TIME;
2744 cur_state = MFI_STATE_OPERATIONAL;
2745 break;
2746
2747 case MFI_STATE_UNDEFINED:
2748
2749
2750
2751 max_wait = MEGASAS_RESET_WAIT_TIME;
2752 cur_state = MFI_STATE_UNDEFINED;
2753 break;
2754
2755 case MFI_STATE_BB_INIT:
2756 max_wait = MEGASAS_RESET_WAIT_TIME;
2757 cur_state = MFI_STATE_BB_INIT;
2758 break;
2759
2760 case MFI_STATE_FW_INIT:
2761 max_wait = MEGASAS_RESET_WAIT_TIME;
2762 cur_state = MFI_STATE_FW_INIT;
2763 break;
2764
2765 case MFI_STATE_FW_INIT_2:
2766 max_wait = MEGASAS_RESET_WAIT_TIME;
2767 cur_state = MFI_STATE_FW_INIT_2;
2768 break;
2769
2770 case MFI_STATE_DEVICE_SCAN:
2771 max_wait = MEGASAS_RESET_WAIT_TIME;
2772 cur_state = MFI_STATE_DEVICE_SCAN;
2773 break;
2774
2775 case MFI_STATE_FLUSH_CACHE:
2776 max_wait = MEGASAS_RESET_WAIT_TIME;
2777 cur_state = MFI_STATE_FLUSH_CACHE;
2778 break;
2779
2780 default:
2781 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n",
2782 fw_state);
2783 return -ENODEV;
2784 }
2785
2786
2787
2788
2789 for (i = 0; i < (max_wait * 1000); i++) {
2790 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
2791 MFI_STATE_MASK ;
2792 curr_abs_state =
2793 instance->instancet->read_fw_status_reg(instance->reg_set);
2794
2795 if (abs_state == curr_abs_state) {
2796 msleep(1);
2797 } else
2798 break;
2799 }
2800
2801
2802
2803
2804 if (curr_abs_state == abs_state) {
2805 printk(KERN_DEBUG "FW state [%d] hasn't changed "
2806 "in %d secs\n", fw_state, max_wait);
2807 return -ENODEV;
2808 }
2809 }
2810 printk(KERN_INFO "megasas: FW now in Ready state\n");
2811
2812 return 0;
2813}
2814
2815
2816
2817
2818
2819static void megasas_teardown_frame_pool(struct megasas_instance *instance)
2820{
2821 int i;
2822 u32 max_cmd = instance->max_mfi_cmds;
2823 struct megasas_cmd *cmd;
2824
2825 if (!instance->frame_dma_pool)
2826 return;
2827
2828
2829
2830
2831 for (i = 0; i < max_cmd; i++) {
2832
2833 cmd = instance->cmd_list[i];
2834
2835 if (cmd->frame)
2836 pci_pool_free(instance->frame_dma_pool, cmd->frame,
2837 cmd->frame_phys_addr);
2838
2839 if (cmd->sense)
2840 pci_pool_free(instance->sense_dma_pool, cmd->sense,
2841 cmd->sense_phys_addr);
2842 }
2843
2844
2845
2846
2847 pci_pool_destroy(instance->frame_dma_pool);
2848 pci_pool_destroy(instance->sense_dma_pool);
2849
2850 instance->frame_dma_pool = NULL;
2851 instance->sense_dma_pool = NULL;
2852}
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863static int megasas_create_frame_pool(struct megasas_instance *instance)
2864{
2865 int i;
2866 u32 max_cmd;
2867 u32 sge_sz;
2868 u32 sgl_sz;
2869 u32 total_sz;
2870 u32 frame_count;
2871 struct megasas_cmd *cmd;
2872
2873 max_cmd = instance->max_mfi_cmds;
2874
2875
2876
2877
2878
2879 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
2880 sizeof(struct megasas_sge32);
2881
2882 if (instance->flag_ieee) {
2883 sge_sz = sizeof(struct megasas_sge_skinny);
2884 }
2885
2886
2887
2888
2889 sgl_sz = sge_sz * instance->max_num_sge;
2890 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE;
2891 frame_count = 15;
2892
2893
2894
2895
2896 frame_count++;
2897
2898 total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2899
2900
2901
2902 instance->frame_dma_pool = pci_pool_create("megasas frame pool",
2903 instance->pdev, total_sz, 64,
2904 0);
2905
2906 if (!instance->frame_dma_pool) {
2907 printk(KERN_DEBUG "megasas: failed to setup frame pool\n");
2908 return -ENOMEM;
2909 }
2910
2911 instance->sense_dma_pool = pci_pool_create("megasas sense pool",
2912 instance->pdev, 128, 4, 0);
2913
2914 if (!instance->sense_dma_pool) {
2915 printk(KERN_DEBUG "megasas: failed to setup sense pool\n");
2916
2917 pci_pool_destroy(instance->frame_dma_pool);
2918 instance->frame_dma_pool = NULL;
2919
2920 return -ENOMEM;
2921 }
2922
2923
2924
2925
2926
2927
2928 for (i = 0; i < max_cmd; i++) {
2929
2930 cmd = instance->cmd_list[i];
2931
2932 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
2933 GFP_KERNEL, &cmd->frame_phys_addr);
2934
2935 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
2936 GFP_KERNEL, &cmd->sense_phys_addr);
2937
2938
2939
2940
2941
2942 if (!cmd->frame || !cmd->sense) {
2943 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n");
2944 megasas_teardown_frame_pool(instance);
2945 return -ENOMEM;
2946 }
2947
2948 memset(cmd->frame, 0, total_sz);
2949 cmd->frame->io.context = cmd->index;
2950 cmd->frame->io.pad_0 = 0;
2951 if ((instance->pdev->device != PCI_DEVICE_ID_LSI_FUSION) &&
2952 (instance->pdev->device != PCI_DEVICE_ID_LSI_INVADER) &&
2953 (reset_devices))
2954 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
2955 }
2956
2957 return 0;
2958}
2959
2960
2961
2962
2963
2964void megasas_free_cmds(struct megasas_instance *instance)
2965{
2966 int i;
2967
2968 megasas_teardown_frame_pool(instance);
2969
2970
2971 for (i = 0; i < instance->max_mfi_cmds; i++)
2972
2973 kfree(instance->cmd_list[i]);
2974
2975
2976 kfree(instance->cmd_list);
2977 instance->cmd_list = NULL;
2978
2979 INIT_LIST_HEAD(&instance->cmd_pool);
2980}
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000int megasas_alloc_cmds(struct megasas_instance *instance)
3001{
3002 int i;
3003 int j;
3004 u32 max_cmd;
3005 struct megasas_cmd *cmd;
3006
3007 max_cmd = instance->max_mfi_cmds;
3008
3009
3010
3011
3012
3013
3014 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
3015
3016 if (!instance->cmd_list) {
3017 printk(KERN_DEBUG "megasas: out of memory\n");
3018 return -ENOMEM;
3019 }
3020
3021 memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
3022
3023 for (i = 0; i < max_cmd; i++) {
3024 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3025 GFP_KERNEL);
3026
3027 if (!instance->cmd_list[i]) {
3028
3029 for (j = 0; j < i; j++)
3030 kfree(instance->cmd_list[j]);
3031
3032 kfree(instance->cmd_list);
3033 instance->cmd_list = NULL;
3034
3035 return -ENOMEM;
3036 }
3037 }
3038
3039
3040
3041
3042 for (i = 0; i < max_cmd; i++) {
3043 cmd = instance->cmd_list[i];
3044 memset(cmd, 0, sizeof(struct megasas_cmd));
3045 cmd->index = i;
3046 cmd->scmd = NULL;
3047 cmd->instance = instance;
3048
3049 list_add_tail(&cmd->list, &instance->cmd_pool);
3050 }
3051
3052
3053
3054
3055 if (megasas_create_frame_pool(instance)) {
3056 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
3057 megasas_free_cmds(instance);
3058 }
3059
3060 return 0;
3061}
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072static int
3073megasas_get_pd_list(struct megasas_instance *instance)
3074{
3075 int ret = 0, pd_index = 0;
3076 struct megasas_cmd *cmd;
3077 struct megasas_dcmd_frame *dcmd;
3078 struct MR_PD_LIST *ci;
3079 struct MR_PD_ADDRESS *pd_addr;
3080 dma_addr_t ci_h = 0;
3081
3082 cmd = megasas_get_cmd(instance);
3083
3084 if (!cmd) {
3085 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n");
3086 return -ENOMEM;
3087 }
3088
3089 dcmd = &cmd->frame->dcmd;
3090
3091 ci = pci_alloc_consistent(instance->pdev,
3092 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3093
3094 if (!ci) {
3095 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n");
3096 megasas_return_cmd(instance, cmd);
3097 return -ENOMEM;
3098 }
3099
3100 memset(ci, 0, sizeof(*ci));
3101 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3102
3103 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3104 dcmd->mbox.b[1] = 0;
3105 dcmd->cmd = MFI_CMD_DCMD;
3106 dcmd->cmd_status = 0xFF;
3107 dcmd->sge_count = 1;
3108 dcmd->flags = MFI_FRAME_DIR_READ;
3109 dcmd->timeout = 0;
3110 dcmd->pad_0 = 0;
3111 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3112 dcmd->opcode = MR_DCMD_PD_LIST_QUERY;
3113 dcmd->sgl.sge32[0].phys_addr = ci_h;
3114 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST);
3115
3116 if (!megasas_issue_polled(instance, cmd)) {
3117 ret = 0;
3118 } else {
3119 ret = -1;
3120 }
3121
3122
3123
3124
3125
3126 pd_addr = ci->addr;
3127
3128 if ( ret == 0 &&
3129 (ci->count <
3130 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3131
3132 memset(instance->pd_list, 0,
3133 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3134
3135 for (pd_index = 0; pd_index < ci->count; pd_index++) {
3136
3137 instance->pd_list[pd_addr->deviceId].tid =
3138 pd_addr->deviceId;
3139 instance->pd_list[pd_addr->deviceId].driveType =
3140 pd_addr->scsiDevType;
3141 instance->pd_list[pd_addr->deviceId].driveState =
3142 MR_PD_STATE_SYSTEM;
3143 pd_addr++;
3144 }
3145 }
3146
3147 pci_free_consistent(instance->pdev,
3148 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3149 ci, ci_h);
3150 megasas_return_cmd(instance, cmd);
3151
3152 return ret;
3153}
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164static int
3165megasas_get_ld_list(struct megasas_instance *instance)
3166{
3167 int ret = 0, ld_index = 0, ids = 0;
3168 struct megasas_cmd *cmd;
3169 struct megasas_dcmd_frame *dcmd;
3170 struct MR_LD_LIST *ci;
3171 dma_addr_t ci_h = 0;
3172
3173 cmd = megasas_get_cmd(instance);
3174
3175 if (!cmd) {
3176 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n");
3177 return -ENOMEM;
3178 }
3179
3180 dcmd = &cmd->frame->dcmd;
3181
3182 ci = pci_alloc_consistent(instance->pdev,
3183 sizeof(struct MR_LD_LIST),
3184 &ci_h);
3185
3186 if (!ci) {
3187 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n");
3188 megasas_return_cmd(instance, cmd);
3189 return -ENOMEM;
3190 }
3191
3192 memset(ci, 0, sizeof(*ci));
3193 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3194
3195 dcmd->cmd = MFI_CMD_DCMD;
3196 dcmd->cmd_status = 0xFF;
3197 dcmd->sge_count = 1;
3198 dcmd->flags = MFI_FRAME_DIR_READ;
3199 dcmd->timeout = 0;
3200 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST);
3201 dcmd->opcode = MR_DCMD_LD_GET_LIST;
3202 dcmd->sgl.sge32[0].phys_addr = ci_h;
3203 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST);
3204 dcmd->pad_0 = 0;
3205
3206 if (!megasas_issue_polled(instance, cmd)) {
3207 ret = 0;
3208 } else {
3209 ret = -1;
3210 }
3211
3212
3213
3214 if ((ret == 0) && (ci->ldCount <= MAX_LOGICAL_DRIVES)) {
3215 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3216
3217 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) {
3218 if (ci->ldList[ld_index].state != 0) {
3219 ids = ci->ldList[ld_index].ref.targetId;
3220 instance->ld_ids[ids] =
3221 ci->ldList[ld_index].ref.targetId;
3222 }
3223 }
3224 }
3225
3226 pci_free_consistent(instance->pdev,
3227 sizeof(struct MR_LD_LIST),
3228 ci,
3229 ci_h);
3230
3231 megasas_return_cmd(instance, cmd);
3232 return ret;
3233}
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244static int
3245megasas_get_ctrl_info(struct megasas_instance *instance,
3246 struct megasas_ctrl_info *ctrl_info)
3247{
3248 int ret = 0;
3249 struct megasas_cmd *cmd;
3250 struct megasas_dcmd_frame *dcmd;
3251 struct megasas_ctrl_info *ci;
3252 dma_addr_t ci_h = 0;
3253
3254 cmd = megasas_get_cmd(instance);
3255
3256 if (!cmd) {
3257 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n");
3258 return -ENOMEM;
3259 }
3260
3261 dcmd = &cmd->frame->dcmd;
3262
3263 ci = pci_alloc_consistent(instance->pdev,
3264 sizeof(struct megasas_ctrl_info), &ci_h);
3265
3266 if (!ci) {
3267 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n");
3268 megasas_return_cmd(instance, cmd);
3269 return -ENOMEM;
3270 }
3271
3272 memset(ci, 0, sizeof(*ci));
3273 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3274
3275 dcmd->cmd = MFI_CMD_DCMD;
3276 dcmd->cmd_status = 0xFF;
3277 dcmd->sge_count = 1;
3278 dcmd->flags = MFI_FRAME_DIR_READ;
3279 dcmd->timeout = 0;
3280 dcmd->pad_0 = 0;
3281 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info);
3282 dcmd->opcode = MR_DCMD_CTRL_GET_INFO;
3283 dcmd->sgl.sge32[0].phys_addr = ci_h;
3284 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info);
3285
3286 if (!megasas_issue_polled(instance, cmd)) {
3287 ret = 0;
3288 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
3289 } else {
3290 ret = -1;
3291 }
3292
3293 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
3294 ci, ci_h);
3295
3296 megasas_return_cmd(instance, cmd);
3297 return ret;
3298}
3299
3300
3301
3302
3303
3304
3305
3306static int
3307megasas_issue_init_mfi(struct megasas_instance *instance)
3308{
3309 u32 context;
3310
3311 struct megasas_cmd *cmd;
3312
3313 struct megasas_init_frame *init_frame;
3314 struct megasas_init_queue_info *initq_info;
3315 dma_addr_t init_frame_h;
3316 dma_addr_t initq_info_h;
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326 cmd = megasas_get_cmd(instance);
3327
3328 init_frame = (struct megasas_init_frame *)cmd->frame;
3329 initq_info = (struct megasas_init_queue_info *)
3330 ((unsigned long)init_frame + 64);
3331
3332 init_frame_h = cmd->frame_phys_addr;
3333 initq_info_h = init_frame_h + 64;
3334
3335 context = init_frame->context;
3336 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
3337 memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
3338 init_frame->context = context;
3339
3340 initq_info->reply_queue_entries = instance->max_fw_cmds + 1;
3341 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h;
3342
3343 initq_info->producer_index_phys_addr_lo = instance->producer_h;
3344 initq_info->consumer_index_phys_addr_lo = instance->consumer_h;
3345
3346 init_frame->cmd = MFI_CMD_INIT;
3347 init_frame->cmd_status = 0xFF;
3348 init_frame->queue_info_new_phys_addr_lo = initq_info_h;
3349
3350 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info);
3351
3352
3353
3354
3355 instance->instancet->disable_intr(instance->reg_set);
3356
3357
3358
3359
3360
3361 if (megasas_issue_polled(instance, cmd)) {
3362 printk(KERN_ERR "megasas: Failed to init firmware\n");
3363 megasas_return_cmd(instance, cmd);
3364 goto fail_fw_init;
3365 }
3366
3367 megasas_return_cmd(instance, cmd);
3368
3369 return 0;
3370
3371fail_fw_init:
3372 return -EINVAL;
3373}
3374
3375static u32
3376megasas_init_adapter_mfi(struct megasas_instance *instance)
3377{
3378 struct megasas_register_set __iomem *reg_set;
3379 u32 context_sz;
3380 u32 reply_q_sz;
3381
3382 reg_set = instance->reg_set;
3383
3384
3385
3386
3387 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
3388
3389
3390
3391
3392
3393 instance->max_fw_cmds = instance->max_fw_cmds-1;
3394 instance->max_mfi_cmds = instance->max_fw_cmds;
3395 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
3396 0x10;
3397
3398
3399
3400 if (megasas_alloc_cmds(instance))
3401 goto fail_alloc_cmds;
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412 context_sz = sizeof(u32);
3413 reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
3414
3415 instance->reply_queue = pci_alloc_consistent(instance->pdev,
3416 reply_q_sz,
3417 &instance->reply_queue_h);
3418
3419 if (!instance->reply_queue) {
3420 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n");
3421 goto fail_reply_queue;
3422 }
3423
3424 if (megasas_issue_init_mfi(instance))
3425 goto fail_fw_init;
3426
3427 instance->fw_support_ieee = 0;
3428 instance->fw_support_ieee =
3429 (instance->instancet->read_fw_status_reg(reg_set) &
3430 0x04000000);
3431
3432 printk(KERN_NOTICE "megasas_init_mfi: fw_support_ieee=%d",
3433 instance->fw_support_ieee);
3434
3435 if (instance->fw_support_ieee)
3436 instance->flag_ieee = 1;
3437
3438 return 0;
3439
3440fail_fw_init:
3441
3442 pci_free_consistent(instance->pdev, reply_q_sz,
3443 instance->reply_queue, instance->reply_queue_h);
3444fail_reply_queue:
3445 megasas_free_cmds(instance);
3446
3447fail_alloc_cmds:
3448 return 1;
3449}
3450
3451
3452
3453
3454
3455
3456
3457
3458static int megasas_init_fw(struct megasas_instance *instance)
3459{
3460 u32 max_sectors_1;
3461 u32 max_sectors_2;
3462 u32 tmp_sectors, msix_enable;
3463 struct megasas_register_set __iomem *reg_set;
3464 struct megasas_ctrl_info *ctrl_info;
3465 unsigned long bar_list;
3466 int i;
3467
3468
3469 bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
3470 instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
3471 instance->base_addr = pci_resource_start(instance->pdev, instance->bar);
3472 if (pci_request_selected_regions(instance->pdev, instance->bar,
3473 "megasas: LSI")) {
3474 printk(KERN_DEBUG "megasas: IO memory region busy!\n");
3475 return -EBUSY;
3476 }
3477
3478 instance->reg_set = ioremap_nocache(instance->base_addr, 8192);
3479
3480 if (!instance->reg_set) {
3481 printk(KERN_DEBUG "megasas: Failed to map IO mem\n");
3482 goto fail_ioremap;
3483 }
3484
3485 reg_set = instance->reg_set;
3486
3487 switch (instance->pdev->device) {
3488 case PCI_DEVICE_ID_LSI_FUSION:
3489 case PCI_DEVICE_ID_LSI_INVADER:
3490 instance->instancet = &megasas_instance_template_fusion;
3491 break;
3492 case PCI_DEVICE_ID_LSI_SAS1078R:
3493 case PCI_DEVICE_ID_LSI_SAS1078DE:
3494 instance->instancet = &megasas_instance_template_ppc;
3495 break;
3496 case PCI_DEVICE_ID_LSI_SAS1078GEN2:
3497 case PCI_DEVICE_ID_LSI_SAS0079GEN2:
3498 instance->instancet = &megasas_instance_template_gen2;
3499 break;
3500 case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
3501 case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
3502 instance->instancet = &megasas_instance_template_skinny;
3503 break;
3504 case PCI_DEVICE_ID_LSI_SAS1064R:
3505 case PCI_DEVICE_ID_DELL_PERC5:
3506 default:
3507 instance->instancet = &megasas_instance_template_xscale;
3508 break;
3509 }
3510
3511
3512
3513
3514 if (megasas_transition_to_ready(instance, 0))
3515 goto fail_ready_state;
3516
3517
3518 msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
3519 0x4000000) >> 0x1a;
3520 if (msix_enable && !msix_disable) {
3521
3522 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3523 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
3524 instance->msix_vectors = (readl(&instance->reg_set->
3525 outbound_scratch_pad_2
3526 ) & 0x1F) + 1;
3527 if (msix_vectors)
3528 instance->msix_vectors =
3529 min(msix_vectors,
3530 instance->msix_vectors);
3531 } else
3532 instance->msix_vectors = 1;
3533
3534 instance->msix_vectors = min(instance->msix_vectors,
3535 (unsigned int)num_online_cpus());
3536 for (i = 0; i < instance->msix_vectors; i++)
3537 instance->msixentry[i].entry = i;
3538 i = pci_enable_msix(instance->pdev, instance->msixentry,
3539 instance->msix_vectors);
3540 if (i >= 0) {
3541 if (i) {
3542 if (!pci_enable_msix(instance->pdev,
3543 instance->msixentry, i))
3544 instance->msix_vectors = i;
3545 else
3546 instance->msix_vectors = 0;
3547 }
3548 } else
3549 instance->msix_vectors = 0;
3550 }
3551
3552
3553 if (instance->instancet->init_adapter(instance))
3554 goto fail_init_adapter;
3555
3556 printk(KERN_ERR "megasas: INIT adapter done\n");
3557
3558
3559
3560
3561
3562 memset(instance->pd_list, 0 ,
3563 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
3564 megasas_get_pd_list(instance);
3565
3566 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
3567 megasas_get_ld_list(instance);
3568
3569 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL);
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580 tmp_sectors = 0;
3581 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) {
3582
3583 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
3584 ctrl_info->max_strips_per_io;
3585 max_sectors_2 = ctrl_info->max_request_size;
3586
3587 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2);
3588 instance->disableOnlineCtrlReset =
3589 ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
3590 }
3591
3592 instance->max_sectors_per_req = instance->max_num_sge *
3593 PAGE_SIZE / 512;
3594 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
3595 instance->max_sectors_per_req = tmp_sectors;
3596
3597 kfree(ctrl_info);
3598
3599
3600 if (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY ||
3601 instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) {
3602 if (throttlequeuedepth > (instance->max_fw_cmds -
3603 MEGASAS_SKINNY_INT_CMDS))
3604 instance->throttlequeuedepth =
3605 MEGASAS_THROTTLE_QUEUE_DEPTH;
3606 else
3607 instance->throttlequeuedepth = throttlequeuedepth;
3608 } else {
3609 if (throttlequeuedepth > (instance->max_fw_cmds -
3610 MEGASAS_INT_CMDS))
3611 instance->throttlequeuedepth =
3612 MEGASAS_THROTTLE_QUEUE_DEPTH;
3613 else
3614 instance->throttlequeuedepth = throttlequeuedepth;
3615 }
3616
3617
3618
3619
3620
3621 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
3622 (unsigned long)instance);
3623
3624 return 0;
3625
3626fail_init_adapter:
3627fail_ready_state:
3628 iounmap(instance->reg_set);
3629
3630 fail_ioremap:
3631 pci_release_selected_regions(instance->pdev, instance->bar);
3632
3633 return -EINVAL;
3634}
3635
3636
3637
3638
3639
3640static void megasas_release_mfi(struct megasas_instance *instance)
3641{
3642 u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
3643
3644 if (instance->reply_queue)
3645 pci_free_consistent(instance->pdev, reply_q_sz,
3646 instance->reply_queue, instance->reply_queue_h);
3647
3648 megasas_free_cmds(instance);
3649
3650 iounmap(instance->reg_set);
3651
3652 pci_release_selected_regions(instance->pdev, instance->bar);
3653}
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667static int
3668megasas_get_seq_num(struct megasas_instance *instance,
3669 struct megasas_evt_log_info *eli)
3670{
3671 struct megasas_cmd *cmd;
3672 struct megasas_dcmd_frame *dcmd;
3673 struct megasas_evt_log_info *el_info;
3674 dma_addr_t el_info_h = 0;
3675
3676 cmd = megasas_get_cmd(instance);
3677
3678 if (!cmd) {
3679 return -ENOMEM;
3680 }
3681
3682 dcmd = &cmd->frame->dcmd;
3683 el_info = pci_alloc_consistent(instance->pdev,
3684 sizeof(struct megasas_evt_log_info),
3685 &el_info_h);
3686
3687 if (!el_info) {
3688 megasas_return_cmd(instance, cmd);
3689 return -ENOMEM;
3690 }
3691
3692 memset(el_info, 0, sizeof(*el_info));
3693 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3694
3695 dcmd->cmd = MFI_CMD_DCMD;
3696 dcmd->cmd_status = 0x0;
3697 dcmd->sge_count = 1;
3698 dcmd->flags = MFI_FRAME_DIR_READ;
3699 dcmd->timeout = 0;
3700 dcmd->pad_0 = 0;
3701 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info);
3702 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO;
3703 dcmd->sgl.sge32[0].phys_addr = el_info_h;
3704 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info);
3705
3706 megasas_issue_blocked_cmd(instance, cmd);
3707
3708
3709
3710
3711 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info));
3712
3713 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
3714 el_info, el_info_h);
3715
3716 megasas_return_cmd(instance, cmd);
3717
3718 return 0;
3719}
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730static int
3731megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
3732 u32 class_locale_word)
3733{
3734 int ret_val;
3735 struct megasas_cmd *cmd;
3736 struct megasas_dcmd_frame *dcmd;
3737 union megasas_evt_class_locale curr_aen;
3738 union megasas_evt_class_locale prev_aen;
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753 curr_aen.word = class_locale_word;
3754
3755 if (instance->aen_cmd) {
3756
3757 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1];
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769 if ((prev_aen.members.class <= curr_aen.members.class) &&
3770 !((prev_aen.members.locale & curr_aen.members.locale) ^
3771 curr_aen.members.locale)) {
3772
3773
3774
3775
3776 return 0;
3777 } else {
3778 curr_aen.members.locale |= prev_aen.members.locale;
3779
3780 if (prev_aen.members.class < curr_aen.members.class)
3781 curr_aen.members.class = prev_aen.members.class;
3782
3783 instance->aen_cmd->abort_aen = 1;
3784 ret_val = megasas_issue_blocked_abort_cmd(instance,
3785 instance->
3786 aen_cmd);
3787
3788 if (ret_val) {
3789 printk(KERN_DEBUG "megasas: Failed to abort "
3790 "previous AEN command\n");
3791 return ret_val;
3792 }
3793 }
3794 }
3795
3796 cmd = megasas_get_cmd(instance);
3797
3798 if (!cmd)
3799 return -ENOMEM;
3800
3801 dcmd = &cmd->frame->dcmd;
3802
3803 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
3804
3805
3806
3807
3808 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3809
3810 dcmd->cmd = MFI_CMD_DCMD;
3811 dcmd->cmd_status = 0x0;
3812 dcmd->sge_count = 1;
3813 dcmd->flags = MFI_FRAME_DIR_READ;
3814 dcmd->timeout = 0;
3815 dcmd->pad_0 = 0;
3816 instance->last_seq_num = seq_num;
3817 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail);
3818 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT;
3819 dcmd->mbox.w[0] = seq_num;
3820 dcmd->mbox.w[1] = curr_aen.word;
3821 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h;
3822 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail);
3823
3824 if (instance->aen_cmd != NULL) {
3825 megasas_return_cmd(instance, cmd);
3826 return 0;
3827 }
3828
3829
3830
3831
3832
3833
3834 instance->aen_cmd = cmd;
3835
3836
3837
3838
3839 instance->instancet->issue_dcmd(instance, cmd);
3840
3841 return 0;
3842}
3843
3844
3845
3846
3847
3848static int megasas_start_aen(struct megasas_instance *instance)
3849{
3850 struct megasas_evt_log_info eli;
3851 union megasas_evt_class_locale class_locale;
3852
3853
3854
3855
3856 memset(&eli, 0, sizeof(eli));
3857
3858 if (megasas_get_seq_num(instance, &eli))
3859 return -1;
3860
3861
3862
3863
3864 class_locale.members.reserved = 0;
3865 class_locale.members.locale = MR_EVT_LOCALE_ALL;
3866 class_locale.members.class = MR_EVT_CLASS_DEBUG;
3867
3868 return megasas_register_aen(instance, eli.newest_seq_num + 1,
3869 class_locale.word);
3870}
3871
3872
3873
3874
3875
3876static int megasas_io_attach(struct megasas_instance *instance)
3877{
3878 struct Scsi_Host *host = instance->host;
3879
3880
3881
3882
3883 host->irq = instance->pdev->irq;
3884 host->unique_id = instance->unique_id;
3885 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3886 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
3887 host->can_queue =
3888 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS;
3889 } else
3890 host->can_queue =
3891 instance->max_fw_cmds - MEGASAS_INT_CMDS;
3892 host->this_id = instance->init_id;
3893 host->sg_tablesize = instance->max_num_sge;
3894
3895 if (instance->fw_support_ieee)
3896 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
3897
3898
3899
3900
3901 if (max_sectors && max_sectors < instance->max_sectors_per_req)
3902 instance->max_sectors_per_req = max_sectors;
3903 else {
3904 if (max_sectors) {
3905 if (((instance->pdev->device ==
3906 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
3907 (instance->pdev->device ==
3908 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
3909 (max_sectors <= MEGASAS_MAX_SECTORS)) {
3910 instance->max_sectors_per_req = max_sectors;
3911 } else {
3912 printk(KERN_INFO "megasas: max_sectors should be > 0"
3913 "and <= %d (or < 1MB for GEN2 controller)\n",
3914 instance->max_sectors_per_req);
3915 }
3916 }
3917 }
3918
3919 host->max_sectors = instance->max_sectors_per_req;
3920 host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
3921 host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3922 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
3923 host->max_lun = MEGASAS_MAX_LUN;
3924 host->max_cmd_len = 16;
3925
3926
3927 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
3928 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER)) {
3929 host->hostt->eh_device_reset_handler = NULL;
3930 host->hostt->eh_bus_reset_handler = NULL;
3931 }
3932
3933
3934
3935
3936 if (scsi_add_host(host, &instance->pdev->dev)) {
3937 printk(KERN_DEBUG "megasas: scsi_add_host failed\n");
3938 return -ENODEV;
3939 }
3940
3941
3942
3943
3944 scsi_scan_host(host);
3945 return 0;
3946}
3947
3948static int
3949megasas_set_dma_mask(struct pci_dev *pdev)
3950{
3951
3952
3953
3954 if (IS_DMA64) {
3955 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
3956
3957 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3958 goto fail_set_dma_mask;
3959 }
3960 } else {
3961 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
3962 goto fail_set_dma_mask;
3963 }
3964 return 0;
3965
3966fail_set_dma_mask:
3967 return 1;
3968}
3969
3970
3971
3972
3973
3974
3975static int megasas_probe_one(struct pci_dev *pdev,
3976 const struct pci_device_id *id)
3977{
3978 int rval, pos, i, j;
3979 struct Scsi_Host *host;
3980 struct megasas_instance *instance;
3981 u16 control = 0;
3982
3983
3984 if (reset_devices) {
3985 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
3986 if (pos) {
3987 pci_read_config_word(pdev, msi_control_reg(pos),
3988 &control);
3989 if (control & PCI_MSIX_FLAGS_ENABLE) {
3990 dev_info(&pdev->dev, "resetting MSI-X\n");
3991 pci_write_config_word(pdev,
3992 msi_control_reg(pos),
3993 control &
3994 ~PCI_MSIX_FLAGS_ENABLE);
3995 }
3996 }
3997 }
3998
3999
4000
4001
4002 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ",
4003 pdev->vendor, pdev->device, pdev->subsystem_vendor,
4004 pdev->subsystem_device);
4005
4006 printk("bus %d:slot %d:func %d\n",
4007 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
4008
4009
4010
4011
4012 rval = pci_enable_device_mem(pdev);
4013
4014 if (rval) {
4015 return rval;
4016 }
4017
4018 pci_set_master(pdev);
4019
4020 if (megasas_set_dma_mask(pdev))
4021 goto fail_set_dma_mask;
4022
4023 host = scsi_host_alloc(&megasas_template,
4024 sizeof(struct megasas_instance));
4025
4026 if (!host) {
4027 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n");
4028 goto fail_alloc_instance;
4029 }
4030
4031 instance = (struct megasas_instance *)host->hostdata;
4032 memset(instance, 0, sizeof(*instance));
4033 atomic_set( &instance->fw_reset_no_pci_access, 0 );
4034 instance->pdev = pdev;
4035
4036 switch (instance->pdev->device) {
4037 case PCI_DEVICE_ID_LSI_FUSION:
4038 case PCI_DEVICE_ID_LSI_INVADER:
4039 {
4040 struct fusion_context *fusion;
4041
4042 instance->ctrl_context =
4043 kzalloc(sizeof(struct fusion_context), GFP_KERNEL);
4044 if (!instance->ctrl_context) {
4045 printk(KERN_DEBUG "megasas: Failed to allocate "
4046 "memory for Fusion context info\n");
4047 goto fail_alloc_dma_buf;
4048 }
4049 fusion = instance->ctrl_context;
4050 INIT_LIST_HEAD(&fusion->cmd_pool);
4051 spin_lock_init(&fusion->cmd_pool_lock);
4052 }
4053 break;
4054 default:
4055
4056 instance->producer =
4057 pci_alloc_consistent(pdev, sizeof(u32),
4058 &instance->producer_h);
4059 instance->consumer =
4060 pci_alloc_consistent(pdev, sizeof(u32),
4061 &instance->consumer_h);
4062
4063 if (!instance->producer || !instance->consumer) {
4064 printk(KERN_DEBUG "megasas: Failed to allocate"
4065 "memory for producer, consumer\n");
4066 goto fail_alloc_dma_buf;
4067 }
4068
4069 *instance->producer = 0;
4070 *instance->consumer = 0;
4071 break;
4072 }
4073
4074 megasas_poll_wait_aen = 0;
4075 instance->flag_ieee = 0;
4076 instance->ev = NULL;
4077 instance->issuepend_done = 1;
4078 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
4079 megasas_poll_wait_aen = 0;
4080
4081 instance->evt_detail = pci_alloc_consistent(pdev,
4082 sizeof(struct
4083 megasas_evt_detail),
4084 &instance->evt_detail_h);
4085
4086 if (!instance->evt_detail) {
4087 printk(KERN_DEBUG "megasas: Failed to allocate memory for "
4088 "event detail structure\n");
4089 goto fail_alloc_dma_buf;
4090 }
4091
4092
4093
4094
4095 INIT_LIST_HEAD(&instance->cmd_pool);
4096 INIT_LIST_HEAD(&instance->internal_reset_pending_q);
4097
4098 atomic_set(&instance->fw_outstanding,0);
4099
4100 init_waitqueue_head(&instance->int_cmd_wait_q);
4101 init_waitqueue_head(&instance->abort_cmd_wait_q);
4102
4103 spin_lock_init(&instance->cmd_pool_lock);
4104 spin_lock_init(&instance->hba_lock);
4105 spin_lock_init(&instance->completion_lock);
4106
4107 mutex_init(&instance->aen_mutex);
4108 mutex_init(&instance->reset_mutex);
4109
4110
4111
4112
4113 instance->host = host;
4114 instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
4115 instance->init_id = MEGASAS_DEFAULT_INIT_ID;
4116
4117 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4118 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4119 instance->flag_ieee = 1;
4120 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4121 } else
4122 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS);
4123
4124 megasas_dbg_lvl = 0;
4125 instance->flag = 0;
4126 instance->unload = 1;
4127 instance->last_time = 0;
4128 instance->disableOnlineCtrlReset = 1;
4129
4130 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4131 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
4132 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
4133 else
4134 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
4135
4136
4137
4138
4139 if (megasas_init_fw(instance))
4140 goto fail_init_mfi;
4141
4142
4143
4144
4145 if (instance->msix_vectors) {
4146 for (i = 0 ; i < instance->msix_vectors; i++) {
4147 instance->irq_context[i].instance = instance;
4148 instance->irq_context[i].MSIxIndex = i;
4149 if (request_irq(instance->msixentry[i].vector,
4150 instance->instancet->service_isr, 0,
4151 "megasas",
4152 &instance->irq_context[i])) {
4153 printk(KERN_DEBUG "megasas: Failed to "
4154 "register IRQ for vector %d.\n", i);
4155 for (j = 0 ; j < i ; j++)
4156 free_irq(
4157 instance->msixentry[j].vector,
4158 &instance->irq_context[j]);
4159 goto fail_irq;
4160 }
4161 }
4162 } else {
4163 instance->irq_context[0].instance = instance;
4164 instance->irq_context[0].MSIxIndex = 0;
4165 if (request_irq(pdev->irq, instance->instancet->service_isr,
4166 IRQF_SHARED, "megasas",
4167 &instance->irq_context[0])) {
4168 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4169 goto fail_irq;
4170 }
4171 }
4172
4173 instance->instancet->enable_intr(instance->reg_set);
4174
4175
4176
4177
4178 pci_set_drvdata(pdev, instance);
4179
4180
4181
4182
4183
4184 megasas_mgmt_info.count++;
4185 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
4186 megasas_mgmt_info.max_index++;
4187
4188
4189
4190
4191 if (megasas_io_attach(instance))
4192 goto fail_io_attach;
4193
4194 instance->unload = 0;
4195
4196
4197
4198
4199 if (megasas_start_aen(instance)) {
4200 printk(KERN_DEBUG "megasas: start aen failed\n");
4201 goto fail_start_aen;
4202 }
4203
4204 return 0;
4205
4206 fail_start_aen:
4207 fail_io_attach:
4208 megasas_mgmt_info.count--;
4209 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
4210 megasas_mgmt_info.max_index--;
4211
4212 pci_set_drvdata(pdev, NULL);
4213 instance->instancet->disable_intr(instance->reg_set);
4214 if (instance->msix_vectors)
4215 for (i = 0 ; i < instance->msix_vectors; i++)
4216 free_irq(instance->msixentry[i].vector,
4217 &instance->irq_context[i]);
4218 else
4219 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4220fail_irq:
4221 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
4222 (instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER))
4223 megasas_release_fusion(instance);
4224 else
4225 megasas_release_mfi(instance);
4226 fail_init_mfi:
4227 if (instance->msix_vectors)
4228 pci_disable_msix(instance->pdev);
4229 fail_alloc_dma_buf:
4230 if (instance->evt_detail)
4231 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4232 instance->evt_detail,
4233 instance->evt_detail_h);
4234
4235 if (instance->producer)
4236 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4237 instance->producer_h);
4238 if (instance->consumer)
4239 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4240 instance->consumer_h);
4241 scsi_host_put(host);
4242
4243 fail_alloc_instance:
4244 fail_set_dma_mask:
4245 pci_disable_device(pdev);
4246
4247 return -ENODEV;
4248}
4249
4250
4251
4252
4253
4254static void megasas_flush_cache(struct megasas_instance *instance)
4255{
4256 struct megasas_cmd *cmd;
4257 struct megasas_dcmd_frame *dcmd;
4258
4259 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4260 return;
4261
4262 cmd = megasas_get_cmd(instance);
4263
4264 if (!cmd)
4265 return;
4266
4267 dcmd = &cmd->frame->dcmd;
4268
4269 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4270
4271 dcmd->cmd = MFI_CMD_DCMD;
4272 dcmd->cmd_status = 0x0;
4273 dcmd->sge_count = 0;
4274 dcmd->flags = MFI_FRAME_DIR_NONE;
4275 dcmd->timeout = 0;
4276 dcmd->pad_0 = 0;
4277 dcmd->data_xfer_len = 0;
4278 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH;
4279 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
4280
4281 megasas_issue_blocked_cmd(instance, cmd);
4282
4283 megasas_return_cmd(instance, cmd);
4284
4285 return;
4286}
4287
4288
4289
4290
4291
4292
4293static void megasas_shutdown_controller(struct megasas_instance *instance,
4294 u32 opcode)
4295{
4296 struct megasas_cmd *cmd;
4297 struct megasas_dcmd_frame *dcmd;
4298
4299 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
4300 return;
4301
4302 cmd = megasas_get_cmd(instance);
4303
4304 if (!cmd)
4305 return;
4306
4307 if (instance->aen_cmd)
4308 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd);
4309 if (instance->map_update_cmd)
4310 megasas_issue_blocked_abort_cmd(instance,
4311 instance->map_update_cmd);
4312 dcmd = &cmd->frame->dcmd;
4313
4314 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4315
4316 dcmd->cmd = MFI_CMD_DCMD;
4317 dcmd->cmd_status = 0x0;
4318 dcmd->sge_count = 0;
4319 dcmd->flags = MFI_FRAME_DIR_NONE;
4320 dcmd->timeout = 0;
4321 dcmd->pad_0 = 0;
4322 dcmd->data_xfer_len = 0;
4323 dcmd->opcode = opcode;
4324
4325 megasas_issue_blocked_cmd(instance, cmd);
4326
4327 megasas_return_cmd(instance, cmd);
4328
4329 return;
4330}
4331
4332#ifdef CONFIG_PM
4333
4334
4335
4336
4337
4338static int
4339megasas_suspend(struct pci_dev *pdev, pm_message_t state)
4340{
4341 struct Scsi_Host *host;
4342 struct megasas_instance *instance;
4343 int i;
4344
4345 instance = pci_get_drvdata(pdev);
4346 host = instance->host;
4347 instance->unload = 1;
4348
4349 megasas_flush_cache(instance);
4350 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
4351
4352
4353 if (instance->ev != NULL) {
4354 struct megasas_aen_event *ev = instance->ev;
4355 cancel_delayed_work_sync(&ev->hotplug_work);
4356 instance->ev = NULL;
4357 }
4358
4359 tasklet_kill(&instance->isr_tasklet);
4360
4361 pci_set_drvdata(instance->pdev, instance);
4362 instance->instancet->disable_intr(instance->reg_set);
4363
4364 if (instance->msix_vectors)
4365 for (i = 0 ; i < instance->msix_vectors; i++)
4366 free_irq(instance->msixentry[i].vector,
4367 &instance->irq_context[i]);
4368 else
4369 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4370 if (instance->msix_vectors)
4371 pci_disable_msix(instance->pdev);
4372
4373 pci_save_state(pdev);
4374 pci_disable_device(pdev);
4375
4376 pci_set_power_state(pdev, pci_choose_state(pdev, state));
4377
4378 return 0;
4379}
4380
4381
4382
4383
4384
4385static int
4386megasas_resume(struct pci_dev *pdev)
4387{
4388 int rval, i, j;
4389 struct Scsi_Host *host;
4390 struct megasas_instance *instance;
4391
4392 instance = pci_get_drvdata(pdev);
4393 host = instance->host;
4394 pci_set_power_state(pdev, PCI_D0);
4395 pci_enable_wake(pdev, PCI_D0, 0);
4396 pci_restore_state(pdev);
4397
4398
4399
4400
4401 rval = pci_enable_device_mem(pdev);
4402
4403 if (rval) {
4404 printk(KERN_ERR "megasas: Enable device failed\n");
4405 return rval;
4406 }
4407
4408 pci_set_master(pdev);
4409
4410 if (megasas_set_dma_mask(pdev))
4411 goto fail_set_dma_mask;
4412
4413
4414
4415
4416
4417 atomic_set(&instance->fw_outstanding, 0);
4418
4419
4420
4421
4422 if (megasas_transition_to_ready(instance, 0))
4423 goto fail_ready_state;
4424
4425
4426 if (instance->msix_vectors)
4427 pci_enable_msix(instance->pdev, instance->msixentry,
4428 instance->msix_vectors);
4429
4430 switch (instance->pdev->device) {
4431 case PCI_DEVICE_ID_LSI_FUSION:
4432 case PCI_DEVICE_ID_LSI_INVADER:
4433 {
4434 megasas_reset_reply_desc(instance);
4435 if (megasas_ioc_init_fusion(instance)) {
4436 megasas_free_cmds(instance);
4437 megasas_free_cmds_fusion(instance);
4438 goto fail_init_mfi;
4439 }
4440 if (!megasas_get_map_info(instance))
4441 megasas_sync_map_info(instance);
4442 }
4443 break;
4444 default:
4445 *instance->producer = 0;
4446 *instance->consumer = 0;
4447 if (megasas_issue_init_mfi(instance))
4448 goto fail_init_mfi;
4449 break;
4450 }
4451
4452 tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4453 (unsigned long)instance);
4454
4455
4456
4457
4458 if (instance->msix_vectors) {
4459 for (i = 0 ; i < instance->msix_vectors; i++) {
4460 instance->irq_context[i].instance = instance;
4461 instance->irq_context[i].MSIxIndex = i;
4462 if (request_irq(instance->msixentry[i].vector,
4463 instance->instancet->service_isr, 0,
4464 "megasas",
4465 &instance->irq_context[i])) {
4466 printk(KERN_DEBUG "megasas: Failed to "
4467 "register IRQ for vector %d.\n", i);
4468 for (j = 0 ; j < i ; j++)
4469 free_irq(
4470 instance->msixentry[j].vector,
4471 &instance->irq_context[j]);
4472 goto fail_irq;
4473 }
4474 }
4475 } else {
4476 instance->irq_context[0].instance = instance;
4477 instance->irq_context[0].MSIxIndex = 0;
4478 if (request_irq(pdev->irq, instance->instancet->service_isr,
4479 IRQF_SHARED, "megasas",
4480 &instance->irq_context[0])) {
4481 printk(KERN_DEBUG "megasas: Failed to register IRQ\n");
4482 goto fail_irq;
4483 }
4484 }
4485
4486 instance->instancet->enable_intr(instance->reg_set);
4487 instance->unload = 0;
4488
4489
4490
4491
4492 if (megasas_start_aen(instance))
4493 printk(KERN_ERR "megasas: Start AEN failed\n");
4494
4495 return 0;
4496
4497fail_irq:
4498fail_init_mfi:
4499 if (instance->evt_detail)
4500 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
4501 instance->evt_detail,
4502 instance->evt_detail_h);
4503
4504 if (instance->producer)
4505 pci_free_consistent(pdev, sizeof(u32), instance->producer,
4506 instance->producer_h);
4507 if (instance->consumer)
4508 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
4509 instance->consumer_h);
4510 scsi_host_put(host);
4511
4512fail_set_dma_mask:
4513fail_ready_state:
4514
4515 pci_disable_device(pdev);
4516
4517 return -ENODEV;
4518}
4519#else
4520#define megasas_suspend NULL
4521#define megasas_resume NULL
4522#endif
4523
4524
4525
4526
4527
4528static void megasas_detach_one(struct pci_dev *pdev)
4529{
4530 int i;
4531 struct Scsi_Host *host;
4532 struct megasas_instance *instance;
4533 struct fusion_context *fusion;
4534
4535 instance = pci_get_drvdata(pdev);
4536 instance->unload = 1;
4537 host = instance->host;
4538 fusion = instance->ctrl_context;
4539
4540 scsi_remove_host(instance->host);
4541 megasas_flush_cache(instance);
4542 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4543
4544
4545 if (instance->ev != NULL) {
4546 struct megasas_aen_event *ev = instance->ev;
4547 cancel_delayed_work_sync(&ev->hotplug_work);
4548 instance->ev = NULL;
4549 }
4550
4551 tasklet_kill(&instance->isr_tasklet);
4552
4553
4554
4555
4556
4557 for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4558 if (megasas_mgmt_info.instance[i] == instance) {
4559 megasas_mgmt_info.count--;
4560 megasas_mgmt_info.instance[i] = NULL;
4561
4562 break;
4563 }
4564 }
4565
4566 pci_set_drvdata(instance->pdev, NULL);
4567
4568 instance->instancet->disable_intr(instance->reg_set);
4569
4570 if (instance->msix_vectors)
4571 for (i = 0 ; i < instance->msix_vectors; i++)
4572 free_irq(instance->msixentry[i].vector,
4573 &instance->irq_context[i]);
4574 else
4575 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4576 if (instance->msix_vectors)
4577 pci_disable_msix(instance->pdev);
4578
4579 switch (instance->pdev->device) {
4580 case PCI_DEVICE_ID_LSI_FUSION:
4581 case PCI_DEVICE_ID_LSI_INVADER:
4582 megasas_release_fusion(instance);
4583 for (i = 0; i < 2 ; i++)
4584 if (fusion->ld_map[i])
4585 dma_free_coherent(&instance->pdev->dev,
4586 fusion->map_sz,
4587 fusion->ld_map[i],
4588 fusion->
4589 ld_map_phys[i]);
4590 kfree(instance->ctrl_context);
4591 break;
4592 default:
4593 megasas_release_mfi(instance);
4594 pci_free_consistent(pdev,
4595 sizeof(struct megasas_evt_detail),
4596 instance->evt_detail,
4597 instance->evt_detail_h);
4598 pci_free_consistent(pdev, sizeof(u32),
4599 instance->producer,
4600 instance->producer_h);
4601 pci_free_consistent(pdev, sizeof(u32),
4602 instance->consumer,
4603 instance->consumer_h);
4604 break;
4605 }
4606
4607 scsi_host_put(host);
4608
4609 pci_set_drvdata(pdev, NULL);
4610
4611 pci_disable_device(pdev);
4612
4613 return;
4614}
4615
4616
4617
4618
4619
4620static void megasas_shutdown(struct pci_dev *pdev)
4621{
4622 int i;
4623 struct megasas_instance *instance = pci_get_drvdata(pdev);
4624
4625 instance->unload = 1;
4626 megasas_flush_cache(instance);
4627 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
4628 instance->instancet->disable_intr(instance->reg_set);
4629 if (instance->msix_vectors)
4630 for (i = 0 ; i < instance->msix_vectors; i++)
4631 free_irq(instance->msixentry[i].vector,
4632 &instance->irq_context[i]);
4633 else
4634 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4635 if (instance->msix_vectors)
4636 pci_disable_msix(instance->pdev);
4637}
4638
4639
4640
4641
4642static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4643{
4644
4645
4646
4647 if (!capable(CAP_SYS_ADMIN))
4648 return -EACCES;
4649
4650 return 0;
4651}
4652
4653
4654
4655
4656
4657
4658
4659static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
4660{
4661 int rc;
4662
4663 mutex_lock(&megasas_async_queue_mutex);
4664
4665 rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
4666
4667 mutex_unlock(&megasas_async_queue_mutex);
4668
4669 if (rc >= 0) {
4670
4671 filep->private_data = filep;
4672 return 0;
4673 }
4674
4675 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
4676
4677 return rc;
4678}
4679
4680
4681
4682
4683static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
4684{
4685 unsigned int mask;
4686 unsigned long flags;
4687 poll_wait(file, &megasas_poll_wait, wait);
4688 spin_lock_irqsave(&poll_aen_lock, flags);
4689 if (megasas_poll_wait_aen)
4690 mask = (POLLIN | POLLRDNORM);
4691 else
4692 mask = 0;
4693 spin_unlock_irqrestore(&poll_aen_lock, flags);
4694 return mask;
4695}
4696
4697
4698
4699
4700
4701
4702static int
4703megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
4704 struct megasas_iocpacket __user * user_ioc,
4705 struct megasas_iocpacket *ioc)
4706{
4707 struct megasas_sge32 *kern_sge32;
4708 struct megasas_cmd *cmd;
4709 void *kbuff_arr[MAX_IOCTL_SGE];
4710 dma_addr_t buf_handle = 0;
4711 int error = 0, i;
4712 void *sense = NULL;
4713 dma_addr_t sense_handle;
4714 unsigned long *sense_ptr;
4715
4716 memset(kbuff_arr, 0, sizeof(kbuff_arr));
4717
4718 if (ioc->sge_count > MAX_IOCTL_SGE) {
4719 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n",
4720 ioc->sge_count, MAX_IOCTL_SGE);
4721 return -EINVAL;
4722 }
4723
4724 cmd = megasas_get_cmd(instance);
4725 if (!cmd) {
4726 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n");
4727 return -ENOMEM;
4728 }
4729
4730
4731
4732
4733
4734
4735
4736 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
4737 cmd->frame->hdr.context = cmd->index;
4738 cmd->frame->hdr.pad_0 = 0;
4739 cmd->frame->hdr.flags &= ~(MFI_FRAME_IEEE | MFI_FRAME_SGL64 |
4740 MFI_FRAME_SENSE64);
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750 kern_sge32 = (struct megasas_sge32 *)
4751 ((unsigned long)cmd->frame + ioc->sgl_off);
4752
4753
4754
4755
4756 for (i = 0; i < ioc->sge_count; i++) {
4757 if (!ioc->sgl[i].iov_len)
4758 continue;
4759
4760 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
4761 ioc->sgl[i].iov_len,
4762 &buf_handle, GFP_KERNEL);
4763 if (!kbuff_arr[i]) {
4764 printk(KERN_DEBUG "megasas: Failed to alloc "
4765 "kernel SGL buffer for IOCTL \n");
4766 error = -ENOMEM;
4767 goto out;
4768 }
4769
4770
4771
4772
4773
4774 kern_sge32[i].phys_addr = (u32) buf_handle;
4775 kern_sge32[i].length = ioc->sgl[i].iov_len;
4776
4777
4778
4779
4780
4781 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
4782 (u32) (ioc->sgl[i].iov_len))) {
4783 error = -EFAULT;
4784 goto out;
4785 }
4786 }
4787
4788 if (ioc->sense_len) {
4789 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
4790 &sense_handle, GFP_KERNEL);
4791 if (!sense) {
4792 error = -ENOMEM;
4793 goto out;
4794 }
4795
4796 sense_ptr =
4797 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
4798 *sense_ptr = sense_handle;
4799 }
4800
4801
4802
4803
4804
4805 cmd->sync_cmd = 1;
4806 megasas_issue_blocked_cmd(instance, cmd);
4807 cmd->sync_cmd = 0;
4808
4809
4810
4811
4812 for (i = 0; i < ioc->sge_count; i++) {
4813 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
4814 ioc->sgl[i].iov_len)) {
4815 error = -EFAULT;
4816 goto out;
4817 }
4818 }
4819
4820
4821
4822
4823 if (ioc->sense_len) {
4824
4825
4826
4827
4828 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
4829 ioc->sense_off);
4830
4831 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
4832 sense, ioc->sense_len)) {
4833 printk(KERN_ERR "megasas: Failed to copy out to user "
4834 "sense data\n");
4835 error = -EFAULT;
4836 goto out;
4837 }
4838 }
4839
4840
4841
4842
4843 if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
4844 &cmd->frame->hdr.cmd_status, sizeof(u8))) {
4845 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n");
4846 error = -EFAULT;
4847 }
4848
4849 out:
4850 if (sense) {
4851 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
4852 sense, sense_handle);
4853 }
4854
4855 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) {
4856 dma_free_coherent(&instance->pdev->dev,
4857 kern_sge32[i].length,
4858 kbuff_arr[i], kern_sge32[i].phys_addr);
4859 }
4860
4861 megasas_return_cmd(instance, cmd);
4862 return error;
4863}
4864
4865static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
4866{
4867 struct megasas_iocpacket __user *user_ioc =
4868 (struct megasas_iocpacket __user *)arg;
4869 struct megasas_iocpacket *ioc;
4870 struct megasas_instance *instance;
4871 int error;
4872 int i;
4873 unsigned long flags;
4874 u32 wait_time = MEGASAS_RESET_WAIT_TIME;
4875
4876 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
4877 if (!ioc)
4878 return -ENOMEM;
4879
4880 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
4881 error = -EFAULT;
4882 goto out_kfree_ioc;
4883 }
4884
4885 instance = megasas_lookup_instance(ioc->host_no);
4886 if (!instance) {
4887 error = -ENODEV;
4888 goto out_kfree_ioc;