Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
megaraid_sas_base.c
Go to the documentation of this file.
1 /*
2  * Linux MegaRAID driver for SAS based RAID controllers
3  *
4  * Copyright (c) 2003-2012 LSI Corporation.
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * FILE: megaraid_sas_base.c
21  * Version : v06.504.01.00-rc1
22  *
23  * Authors: LSI Corporation
24  * Sreenivas Bagalkote
25  * Sumant Patro
26  * Bo Yang
27  * Adam Radford <[email protected]>
28  *
29  * Send feedback to: <[email protected]>
30  *
31  * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
32  * ATTN: Linuxraid
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  * Number of sectors per IO command
63  * Will be set in megasas_init_mfi if user does not provide
64  */
65 static unsigned int max_sectors;
66 module_param_named(max_sectors, max_sectors, int, 0);
67 MODULE_PARM_DESC(max_sectors,
68  "Maximum number of sectors per IO command");
69 
70 static int msix_disable;
71 module_param(msix_disable, int, S_IRUGO);
72 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
73 
74 static unsigned int msix_vectors;
76 MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
77 
81  "Adapter queue depth when throttled due to I/O timeout. Default: 16");
82 
85 MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
86  "before resetting adapter. Default: 180");
87 
88 MODULE_LICENSE("GPL");
91 MODULE_DESCRIPTION("LSI MegaRAID SAS Driver");
92 
93 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
94 static int megasas_get_pd_list(struct megasas_instance *instance);
95 static int megasas_issue_init_mfi(struct megasas_instance *instance);
96 static int megasas_register_aen(struct megasas_instance *instance,
98 /*
99  * PCI ID table for all supported controllers
100  */
101 static struct pci_device_id megasas_pci_table[] = {
102 
104  /* xscale IOP */
106  /* ppc IOP */
108  /* ppc IOP */
110  /* gen2*/
112  /* gen2*/
114  /* skinny*/
116  /* skinny*/
118  /* xscale IOP, vega */
120  /* xscale IOP */
122  /* Fusion */
124  /* Invader */
125  {}
126 };
127 
128 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
129 
130 static int megasas_mgmt_majorno;
132 static struct fasync_struct *megasas_async_queue;
133 static DEFINE_MUTEX(megasas_async_queue_mutex);
134 
135 static int megasas_poll_wait_aen;
136 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
137 static u32 support_poll_for_event;
139 static u32 support_device_change;
140 
141 /* define lock for aen poll */
143 
144 void
145 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
146  u8 alt_status);
147 static u32
148 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
149 static int
150 megasas_adp_reset_gen2(struct megasas_instance *instance,
152 static irqreturn_t megasas_isr(int irq, void *devp);
153 static u32
154 megasas_init_adapter_mfi(struct megasas_instance *instance);
155 u32
157  struct scsi_cmnd *scmd);
158 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
159 void
160 megasas_release_fusion(struct megasas_instance *instance);
161 int
162 megasas_ioc_init_fusion(struct megasas_instance *instance);
163 void
165 u8
166 megasas_get_map_info(struct megasas_instance *instance);
167 int
168 megasas_sync_map_info(struct megasas_instance *instance);
169 int
170 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
171 void megasas_reset_reply_desc(struct megasas_instance *instance);
173  struct LD_LOAD_BALANCE_INFO *lbInfo);
176 
177 void
179 {
180  instance->instancet->fire_cmd(instance,
181  cmd->frame_phys_addr, 0, instance->reg_set);
182 }
183 
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 
215 inline void
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 
243 static inline void
244 megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs)
245 {
246  writel(0, &(regs)->outbound_intr_mask);
247 
248  /* Dummy readl to force pci flush */
249  readl(&regs->outbound_intr_mask);
250 }
251 
256 static inline void
257 megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs)
258 {
259  u32 mask = 0x1f;
260  writel(mask, &regs->outbound_intr_mask);
261  /* Dummy readl to force pci flush */
262  readl(&regs->outbound_intr_mask);
263 }
264 
269 static u32
270 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
271 {
272  return readl(&(regs)->outbound_msg_0);
273 }
278 static int
279 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
280 {
281  u32 status;
282  u32 mfiStatus = 0;
283  /*
284  * Check if it is our interrupt
285  */
286  status = readl(&regs->outbound_intr_status);
287 
288  if (status & MFI_OB_INTR_STATUS_MASK)
289  mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
292 
293  /*
294  * Clear the interrupt by writing back the same value
295  */
296  if (mfiStatus)
297  writel(status, &regs->outbound_intr_status);
298 
299  /* Dummy readl to force pci flush */
300  readl(&regs->outbound_intr_status);
301 
302  return mfiStatus;
303 }
304 
311 static inline void
312 megasas_fire_cmd_xscale(struct megasas_instance *instance,
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 
328 static int
329 megasas_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, &regs->inbound_doorbell);
335 
336  for (i = 0; i < 3; i++)
337  msleep(1000); /* sleep for 3 secs */
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); /* need to wait 2 secs again */
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,
359  }
360  }
361  return 0;
362 }
363 
368 static int
369 megasas_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 
382 static 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 
412 static inline void
413 megasas_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  /* Dummy readl to force pci flush */
420  readl(&regs->outbound_intr_mask);
421 }
422 
427 static inline void
428 megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs)
429 {
430  u32 mask = 0xFFFFFFFF;
431  writel(mask, &regs->outbound_intr_mask);
432  /* Dummy readl to force pci flush */
433  readl(&regs->outbound_intr_mask);
434 }
435 
440 static u32
441 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
442 {
443  return readl(&(regs)->outbound_scratch_pad);
444 }
445 
450 static int
451 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
452 {
453  u32 status, mfiStatus = 0;
454 
455  /*
456  * Check if it is our interrupt
457  */
458  status = readl(&regs->outbound_intr_status);
459 
461  mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
462 
465 
466  /*
467  * Clear the interrupt by writing back the same value
468  */
469  writel(status, &regs->outbound_doorbell_clear);
470 
471  /* Dummy readl to force pci flush */
472  readl(&regs->outbound_doorbell_clear);
473 
474  return mfiStatus;
475 }
476 
483 static inline void
484 megasas_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 
500 static int
501 megasas_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 
510 static 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 
530 static inline void
531 megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs)
532 {
533  writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
534 
536 
537  /* Dummy readl to force pci flush */
538  readl(&regs->outbound_intr_mask);
539 }
540 
545 static inline void
546 megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs)
547 {
548  u32 mask = 0xFFFFFFFF;
549  writel(mask, &regs->outbound_intr_mask);
550  /* Dummy readl to force pci flush */
551  readl(&regs->outbound_intr_mask);
552 }
553 
558 static u32
559 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
560 {
561  return readl(&(regs)->outbound_scratch_pad);
562 }
563 
568 static int
569 megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
570 {
571  u32 status;
572  u32 mfiStatus = 0;
573 
574  /*
575  * Check if it is our interrupt
576  */
577  status = readl(&regs->outbound_intr_status);
578 
579  if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
580  return 0;
581  }
582 
583  /*
584  * Check if it is our interrupt
585  */
586  if ((megasas_read_fw_status_reg_gen2(regs) & MFI_STATE_MASK) ==
587  MFI_STATE_FAULT) {
589  } else
590  mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
591 
592  /*
593  * Clear the interrupt by writing back the same value
594  */
595  writel(status, &regs->outbound_intr_status);
596 
597  /*
598  * dummy read to flush PCI
599  */
600  readl(&regs->outbound_intr_status);
601 
602  return mfiStatus;
603 }
604 
611 static inline void
612 megasas_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 
629 static int
630 megasas_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 
639 static 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 
665 static inline void
666 megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs)
667 {
668  writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
669 
670  /* write ~0x00000005 (4 & 1) to the intr mask*/
672 
673  /* Dummy readl to force pci flush */
674  readl(&regs->outbound_intr_mask);
675 }
676 
681 static inline void
682 megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs)
683 {
684  u32 mask = 0xFFFFFFFF;
685  writel(mask, &regs->outbound_intr_mask);
686  /* Dummy readl to force pci flush */
687  readl(&regs->outbound_intr_mask);
688 }
689 
694 static u32
695 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
696 {
697  return readl(&(regs)->outbound_scratch_pad);
698 }
699 
704 static int
705 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
706 {
707  u32 status;
708  u32 mfiStatus = 0;
709  /*
710  * Check if it is our interrupt
711  */
712  status = readl(&regs->outbound_intr_status);
713 
714  if (status & MFI_GEN2_ENABLE_INTERRUPT_MASK) {
715  mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
716  }
719  }
720 
721  /*
722  * Clear the interrupt by writing back the same value
723  */
724  if (mfiStatus)
725  writel(status, &regs->outbound_doorbell_clear);
726 
727  /* Dummy readl to force pci flush */
728  readl(&regs->outbound_intr_status);
729 
730  return mfiStatus;
731 }
738 static inline void
739 megasas_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 
755 static int
756 megasas_adp_reset_gen2(struct megasas_instance *instance,
758 {
759  u32 retry = 0 ;
760  u32 HostDiag;
761  u32 *seq_offset = &reg_set->seq_offset;
762  u32 *hostdiag_offset = &reg_set->host_diag;
763 
764  if (instance->instancet == &megasas_instance_template_skinny) {
765  seq_offset = &reg_set->fusion_seq_offset;
766  hostdiag_offset = &reg_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 
815 static int
816 megasas_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 
826 static 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 
847 /*
848  * Template added for TB (Fusion)
849  */
851 
859 int
861 {
862 
863  struct megasas_header *frame_hdr = &cmd->frame->hdr;
864 
865  frame_hdr->cmd_status = 0xFF;
867 
868  /*
869  * Issue the frame using inbound queue port
870  */
871  instance->instancet->issue_dcmd(instance, cmd);
872 
873  /*
874  * Wait for cmd_status to change
875  */
876  return wait_and_poll(instance, cmd);
877 }
878 
888 static int
889 megasas_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 
911 static int
912 megasas_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  * Prepare and issue the abort frame
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  * Wait for this cmd to complete
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 
959 static int
960 megasas_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 
988 static int
989 megasas_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 
1017 static int
1018 megasas_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 
1046 static 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  * Main frame can contain 2 SGEs for 64-bit SGLs and
1063  * 3 SGEs for 32-bit SGLs for ldio &
1064  * 1 SGEs for 64-bit SGLs and
1065  * 2 SGEs for 32-bit SGLs for pthru frame
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  /* Main frame */
1090  frame_count +=1;
1091 
1092  if (frame_count > 7)
1093  frame_count = 8;
1094  return frame_count;
1095 }
1096 
1106 static int
1107 megasas_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 
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  * Prepare the DCDB frame
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  * If the command is for the tape device, set the
1148  * pthru timeout to the os layer timeout value.
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  * Construct SGL
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  * Sense info specific
1180  */
1182  pthru->sense_buf_phys_addr_hi = 0;
1184 
1185  /*
1186  * Compute the total number of frames this command consumes. FW uses
1187  * this number to pull sufficient number of frames from host memory.
1188  */
1189  cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1190  PTHRU_FRAME);
1191 
1192  return cmd->frame_count;
1193 }
1194 
1203 static int
1204 megasas_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 
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  * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
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  * 6-byte READ(0x08) or WRITE(0x0A) cdb
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  * 10-byte READ(0x28) or WRITE(0x2A) cdb
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  * 12-byte READ(0xA8) or WRITE(0xAA) cdb
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  * 16-byte READ(0x88) or WRITE(0x8A) cdb
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  * Construct SGL
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  * Sense info specific
1312  */
1314  ldio->sense_buf_phys_addr_hi = 0;
1316 
1317  /*
1318  * Compute the total number of frames this command consumes. FW uses
1319  * this number to pull sufficient number of frames from host memory.
1320  */
1321  cmd->frame_count = megasas_get_frame_count(instance,
1322  ldio->sge_count, IO_FRAME);
1323 
1324  return cmd->frame_count;
1325 }
1326 
1334 inline 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 
1358 static inline void
1359 megasas_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  }
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  } /*for max_cmd*/
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 
1416 u32
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  * Logical drive command
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  * Issue the command to the FW
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;
1450 out_return_cmd:
1451  megasas_return_cmd(instance, cmd);
1452  return 1;
1453 }
1454 
1455 
1461 static int
1462 megasas_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  * FW takes care of flush cache on its own
1494  * No need to send it down
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 
1514 static DEF_SCSI_QCMD(megasas_queue_command)
1515 
1516 static 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 
1530 static 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  * Don't export physical disk devices to the disk driver.
1539  *
1540  * FIXME: Currently we don't export them to the midlayer at all.
1541  * That will be fixed once LSI engineers have audited the
1542  * firmware for possible issues.
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 ==
1552  return 0;
1553  }
1554  return -ENXIO;
1555  }
1556 
1557  /*
1558  * The RAID firmware may require extended timeouts.
1559  */
1562  return 0;
1563 }
1564 
1565 static 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  * Open the OS scan to the SYSTEM PD
1574  */
1575  pd_index =
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 
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 
1607 void
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 ==
1620  (instance->pdev->device ==
1622  instance->host->can_queue =
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 
1638 static 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  /* If we have already declared adapter dead, donot complete cmds */
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  * Check if we can restore can_queue
1681  */
1683 }
1684 
1685 static void
1686 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1687 
1688 static void
1689 process_fw_state_change_wq(struct work_struct *work);
1690 
1691 void 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)) {
1697  }
1698  instance->instancet->disable_intr(instance->reg_set);
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 
1715 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
1716 {
1717  int i;
1718  u32 reset_index;
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);
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  * Call cmd completion routine. Cmd to be
1802  * be completed directly without depending on isr.
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  /* wait for 1 secs to let FW finish the pending cmds */
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  /* wait for 5 secs to let FW finish the pending cmds */
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  * Send signal to FW to stop processing any pending cmds.
1852  * The controller will be taken offline by the OS now.
1853  */
1854  if ((instance->pdev->device ==
1856  (instance->pdev->device ==
1859  &instance->reg_set->doorbell);
1860  } else {
1862  &instance->reg_set->inbound_doorbell);
1863  }
1864  megasas_dump_pending_frames(instance);
1865  spin_lock_irqsave(&instance->hba_lock, flags);
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 
1884 static 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 
1916 static enum
1917 blk_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  /* FW is busy, throttle IO */
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 
1944 static int megasas_reset_device(struct scsi_cmnd *scmd)
1945 {
1946  int ret;
1947 
1948  /*
1949  * First wait for all commands to complete
1950  */
1951  ret = megasas_generic_reset(scmd);
1952 
1953  return ret;
1954 }
1955 
1959 static 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  * First wait for all commands to complete
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 
1984 static int
1985 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
1986  sector_t capacity, int geom[])
1987 {
1988  int heads;
1989  int sectors;
1991  unsigned long tmp;
1992  /* Default heads (64) & sectors (32) */
1993  heads = 64;
1994  sectors = 32;
1995 
1996  tmp = heads * sectors;
1997  cylinders = capacity;
1998 
1999  sector_div(cylinders, tmp);
2000 
2001  /*
2002  * Handle extended translation size for logical drives > 1Gb
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 
2020 static void megasas_aen_polling(struct work_struct *work);
2021 
2034 static void
2035 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2036 {
2037  unsigned long flags;
2038  /*
2039  * Don't signal app if it is just an aborted previously registered aen
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;
2064  megasas_aen_polling);
2066  }
2067  }
2068 }
2069 
2070 static 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  * Scsi host template for megaraid_sas driver
2086  */
2087 static 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 
2113 static void
2114 megasas_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 
2134 static void
2135 megasas_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 
2157 void
2158 megasas_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  /* flag for the retry reset */
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  /* Some older 1068 controller FW may keep a pended
2175  MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2176  when booting the kdump kernel. Ignore this command to
2177  prevent a kernel panic on shutdown of the kdump kernel. */
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  * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2188  * issued either through an IO path or an IOCTL path. If it
2189  * was via IOCTL, we will send it to internal completion.
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 
2224  cmd->scmd->result =
2225  (DID_ERROR << 16) | hdr->scsi_status;
2226  break;
2227 
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,
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:
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  /* Check for LD map update */
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 !=
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  * See if got an event notification
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  * Cmd issued to abort another cmd returned
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 
2333 static inline void
2334 megasas_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 =
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  * Initiate AEN (Asynchronous Event Notification)
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 
2427 static void
2428 megasas_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 
2462 static void
2463 process_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  /*waitting for about 20 second before start the second init*/
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);
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);
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 
2537 static int
2538 megasas_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  /* Hardware may not set outbound_intr_status in MSI-X mode */
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 ==
2574  (instance->pdev->device ==
2576  (instance->pdev->device ==
2578 
2579  *instance->consumer =
2581  }
2582 
2583 
2584  instance->instancet->disable_intr(instance->reg_set);
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 }
2609 static 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 
2635 int
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 
2667  /*
2668  * Set the CLR bit in inbound doorbell
2669  */
2670  if ((instance->pdev->device ==
2672  (instance->pdev->device ==
2674  (instance->pdev->device ==
2676  (instance->pdev->device ==
2678  writel(
2680  &instance->reg_set->doorbell);
2681  } else {
2682  writel(
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 
2692  if ((instance->pdev->device ==
2694  (instance->pdev->device ==
2696  (instance->pdev->device ==
2698  (instance->pdev->device ==
2701  &instance->reg_set->doorbell);
2702  } else
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  * Bring it to READY state; assuming max wait 10 secs
2713  */
2714  instance->instancet->disable_intr(instance->reg_set);
2715  if ((instance->pdev->device ==
2717  (instance->pdev->device ==
2719  (instance->pdev->device
2721  (instance->pdev->device
2724  &instance->reg_set->doorbell);
2725  if ((instance->pdev->device ==
2727  (instance->pdev->device ==
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
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  * This state should not last for more than 2 seconds
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  * The cur_state should not last for more than max_wait secs
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  * Return error if fw_state hasn't changed after max_wait
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 
2819 static 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  * Return all frames to pool
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  * Now destroy the pool itself
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 
2863 static 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  * Size of our frame is 64 bytes for MFI frame, followed by max SG
2877  * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
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  * Calculated the number of 64byte frames required for SGL
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  * We need one extra frame for the MFI command
2895  */
2896  frame_count++;
2897 
2898  total_sz = MEGAMFI_FRAME_SIZE * frame_count;
2899  /*
2900  * Use DMA pool facility provided by PCI layer
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  * Allocate and attach a frame to each of the commands in cmd_list.
2925  * By making cmd->index as the context instead of the &cmd, we can
2926  * always use 32bit context regardless of the architecture
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  * megasas_teardown_frame_pool() takes care of freeing
2940  * whatever has been allocated
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 
2964 void megasas_free_cmds(struct megasas_instance *instance)
2965 {
2966  int i;
2967  /* First free the MFI frame pool */
2968  megasas_teardown_frame_pool(instance);
2969 
2970  /* Free all the commands in the cmd_list */
2971  for (i = 0; i < instance->max_mfi_cmds; i++)
2972 
2973  kfree(instance->cmd_list[i]);
2974 
2975  /* Free the cmd_list buffer itself */
2976  kfree(instance->cmd_list);
2977  instance->cmd_list = NULL;
2978 
2979  INIT_LIST_HEAD(&instance->cmd_pool);
2980 }
2981 
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  * instance->cmd_list is an array of struct megasas_cmd pointers.
3011  * Allocate the dynamic array first and then allocate individual
3012  * commands.
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  * Add all the commands to command pool (instance->cmd_pool)
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  * Create a frame pool and assign one frame to each cmd
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  * megasas_get_pd_list_info - Returns FW's pd_list structure
3065  * @instance: Adapter soft state
3066  * @pd_list: pd_list structure
3067  *
3068  * Issues an internal command (DCMD) to get the FW's controller PD
3069  * list structure. This information is mainly used to find out SYSTEM
3070  * supported by the FW.
3071  */
3072 static int
3073 megasas_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 
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  * the following function will get the instance PD LIST.
3124  */
3125 
3126  pd_addr = ci->addr;
3127 
3128  if ( ret == 0 &&
3129  (ci->count <
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 =
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  * megasas_get_ld_list_info - Returns FW's ld_list structure
3157  * @instance: Adapter soft state
3158  * @ld_list: ld_list structure
3159  *
3160  * Issues an internal command (DCMD) to get the FW's controller PD
3161  * list structure. This information is mainly used to find out SYSTEM
3162  * supported by the FW.
3163  */
3164 static int
3165 megasas_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  /* the following function will get the instance PD LIST */
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 
3244 static int
3245 megasas_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 
3306 static int
3307 megasas_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  * Prepare a init frame. Note the init frame points to queue info
3320  * structure. Each frame has SGL allocated after first 64 bytes. For
3321  * this frame - since we don't need any SGL - we use SGL's space as
3322  * queue info structure
3323  *
3324  * We will not get a NULL command below. We just created the pool.
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  * disable the intr before firing the init frame to FW
3354  */
3355  instance->instancet->disable_intr(instance->reg_set);
3356 
3357  /*
3358  * Issue the init frame in polled mode
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 
3371 fail_fw_init:
3372  return -EINVAL;
3373 }
3374 
3375 static u32
3376 megasas_init_adapter_mfi(struct megasas_instance *instance)
3377 {
3379  u32 context_sz;
3380  u32 reply_q_sz;
3381 
3382  reg_set = instance->reg_set;
3383 
3384  /*
3385  * Get various operational parameters from status register
3386  */
3387  instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
3388  /*
3389  * Reduce the max supported cmds by 1. This is to ensure that the
3390  * reply_q_sz (1 more than the max cmd that driver may send)
3391  * does not exceed max cmds that the FW can support
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  * Create a pool of commands
3399  */
3400  if (megasas_alloc_cmds(instance))
3401  goto fail_alloc_cmds;
3402 
3403  /*
3404  * Allocate memory for reply queue. Length of reply queue should
3405  * be _one_ more than the maximum commands handled by the firmware.
3406  *
3407  * Note: When FW completes commands, it places corresponding contex
3408  * values in this circular reply queue. This circular queue is a fairly
3409  * typical producer-consumer queue. FW is the producer (of completed
3410  * commands) and the driver is the consumer.
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 
3440 fail_fw_init:
3441 
3442  pci_free_consistent(instance->pdev, reply_q_sz,
3443  instance->reply_queue, instance->reply_queue_h);
3444 fail_reply_queue:
3445  megasas_free_cmds(instance);
3446 
3447 fail_alloc_cmds:
3448  return 1;
3449 }
3450 
3458 static 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;
3464  struct megasas_ctrl_info *ctrl_info;
3465  unsigned long bar_list;
3466  int i;
3467 
3468  /* Find first memory bar */
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) {
3491  break;
3494  instance->instancet = &megasas_instance_template_ppc;
3495  break;
3498  instance->instancet = &megasas_instance_template_gen2;
3499  break;
3502  instance->instancet = &megasas_instance_template_skinny;
3503  break;
3506  default:
3507  instance->instancet = &megasas_instance_template_xscale;
3508  break;
3509  }
3510 
3511  /*
3512  * We expect the FW state to be READY
3513  */
3514  if (megasas_transition_to_ready(instance, 0))
3515  goto fail_ready_state;
3516 
3517  /* Check if MSI-X is supported while in ready state */
3518  msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
3519  0x4000000) >> 0x1a;
3520  if (msix_enable && !msix_disable) {
3521  /* Check max MSI-X vectors */
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->
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  /* Don't bother allocating more MSI-X vectors than cpus */
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  /* Get operational params, sge flags, send init cmd to controller */
3553  if (instance->instancet->init_adapter(instance))
3554  goto fail_init_adapter;
3555 
3556  printk(KERN_ERR "megasas: INIT adapter done\n");
3557 
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  * Compute the max allowed sectors per IO: The controller info has two
3573  * limits on max sectors. Driver should use the minimum of these two.
3574  *
3575  * 1 << stripe_sz_ops.min = max sectors per strip
3576  *
3577  * Note that older firmwares ( < FW ver 30) didn't report information
3578  * to calculate max_sectors_1. So the number ended up as zero always.
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  /* Check for valid throttlequeuedepth module parameter */
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 -
3604  instance->throttlequeuedepth =
3606  else
3607  instance->throttlequeuedepth = throttlequeuedepth;
3608  } else {
3609  if (throttlequeuedepth > (instance->max_fw_cmds -
3611  instance->throttlequeuedepth =
3613  else
3614  instance->throttlequeuedepth = throttlequeuedepth;
3615  }
3616 
3617  /*
3618  * Setup tasklet for cmd completion
3619  */
3620 
3621  tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
3622  (unsigned long)instance);
3623 
3624  return 0;
3625 
3626 fail_init_adapter:
3627 fail_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 
3640 static 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 
3667 static int
3668 megasas_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);
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  * Copy the data back into callers buffer
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 
3730 static int
3731 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
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  * If there an AEN pending already (aen_cmd), check if the
3742  * class_locale of that pending AEN is inclusive of the new
3743  * AEN request we currently have. If it is, then we don't have
3744  * to do anything. In other words, whichever events the current
3745  * AEN request is subscribing to, have already been subscribed
3746  * to.
3747  *
3748  * If the old_cmd is _not_ inclusive, then we have to abort
3749  * that command, form a class_locale that is superset of both
3750  * old and current and re-issue to the FW
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  * A class whose enum value is smaller is inclusive of all
3761  * higher values. If a PROGRESS (= -1) was previously
3762  * registered, then a new registration requests for higher
3763  * classes need not be sent to FW. They are automatically
3764  * included.
3765  *
3766  * Locale numbers don't have such hierarchy. They are bitmap
3767  * values
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  * Previously issued event registration includes
3774  * current request. Nothing to do.
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  * Prepare DCMD for aen registration
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);
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  * Store reference to the cmd used to register for AEN. When an
3831  * application wants us to register for AEN, we have to abort this
3832  * cmd and re-register with a new EVENT LOCALE supplied by that app
3833  */
3834  instance->aen_cmd = cmd;
3835 
3836  /*
3837  * Issue the aen registration frame
3838  */
3839  instance->instancet->issue_dcmd(instance, cmd);
3840 
3841  return 0;
3842 }
3843 
3848 static 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  * Get the latest sequence number from FW
3855  */
3856  memset(&eli, 0, sizeof(eli));
3857 
3858  if (megasas_get_seq_num(instance, &eli))
3859  return -1;
3860 
3861  /*
3862  * Register AEN with FW for latest sequence number plus 1
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 
3876 static int megasas_io_attach(struct megasas_instance *instance)
3877 {
3878  struct Scsi_Host *host = instance->host;
3879 
3880  /*
3881  * Export parameters required by SCSI mid-layer
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 =
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)
3897 
3898  /*
3899  * Check if the module parameter value for max_sectors can be used
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 ==
3907  (instance->pdev->device ==
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;
3921  host->max_channel = MEGASAS_MAX_CHANNELS - 1;
3923  host->max_lun = MEGASAS_MAX_LUN;
3924  host->max_cmd_len = 16;
3925 
3926  /* Fusion only supports host reset */
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  * Notify the mid-layer about the new controller
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  * Trigger SCSI to scan our drives
3943  */
3944  scsi_scan_host(host);
3945  return 0;
3946 }
3947 
3948 static int
3949 megasas_set_dma_mask(struct pci_dev *pdev)
3950 {
3951  /*
3952  * All our contollers are capable of performing 64-bit DMA
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 
3966 fail_set_dma_mask:
3967  return 1;
3968 }
3969 
3975 static int __devinit
3976 megasas_probe_one(struct pci_dev *pdev, 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  /* Reset MSI-X in the kdump kernel */
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  * Announce PCI information
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  * PCI prepping: enable device set bus mastering and dma mask
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) {
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: /* For all other supported controllers */
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;
4079  megasas_poll_wait_aen = 0;
4080 
4081  instance->evt_detail = pci_alloc_consistent(pdev,
4082  sizeof(struct
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  * Initialize locks and queues
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);
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  * Initialize PCI related and misc parameters
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))
4133  else
4134  INIT_WORK(&instance->work_init, process_fw_state_change_wq);
4135 
4136  /*
4137  * Initialize MFI Firmware
4138  */
4139  if (megasas_init_fw(instance))
4140  goto fail_init_mfi;
4141 
4142  /*
4143  * Register IRQ
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  * Store instance in PCI softstate
4177  */
4178  pci_set_drvdata(pdev, instance);
4179 
4180  /*
4181  * Add this controller to megasas_mgmt_info structure so that it
4182  * can be exported to management applications
4183  */
4187 
4188  /*
4189  * Register with SCSI mid-layer
4190  */
4191  if (megasas_io_attach(instance))
4192  goto fail_io_attach;
4193 
4194  instance->unload = 0;
4195 
4196  /*
4197  * Initiate AEN (Asynchronous Event Notification)
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:
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]);
4220 fail_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 
4254 static 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;
4280 
4281  megasas_issue_blocked_cmd(instance, cmd);
4282 
4283  megasas_return_cmd(instance, cmd);
4284 
4285  return;
4286 }
4287 
4293 static 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 
4338 static int
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  /* cancel the delayed work if this work still in queue */
4353  if (instance->ev != NULL) {
4354  struct megasas_aen_event *ev = instance->ev;
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 
4385 static int
4386 megasas_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  * PCI prepping: enable device set bus mastering and dma mask
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  * Initialize MFI Firmware
4415  */
4416 
4417  atomic_set(&instance->fw_outstanding, 0);
4418 
4419  /*
4420  * We expect the FW state to be READY
4421  */
4422  if (megasas_transition_to_ready(instance, 0))
4423  goto fail_ready_state;
4424 
4425  /* Now re-enable MSI-X */
4426  if (instance->msix_vectors)
4427  pci_enable_msix(instance->pdev, instance->msixentry,
4428  instance->msix_vectors);
4429 
4430  switch (instance->pdev->device) {
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  * Register IRQ
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  * Initiate AEN (Asynchronous Event Notification)
4491  */
4492  if (megasas_start_aen(instance))
4493  printk(KERN_ERR "megasas: Start AEN failed\n");
4494 
4495  return 0;
4496 
4497 fail_irq:
4498 fail_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 
4512 fail_set_dma_mask:
4513 fail_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 
4528 static void __devexit 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  /* cancel the delayed work if this work still in queue*/
4545  if (instance->ev != NULL) {
4546  struct megasas_aen_event *ev = instance->ev;
4548  instance->ev = NULL;
4549  }
4550 
4551  tasklet_kill(&instance->isr_tasklet);
4552 
4553  /*
4554  * Take the instance off the instance array. Note that we will not
4555  * decrement the max_index. We let this array be sparse array
4556  */
4557  for (i = 0; i < megasas_mgmt_info.max_index; i++) {
4558  if (megasas_mgmt_info.instance[i] == instance) {
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) {
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 
4620 static 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 
4642 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
4643 {
4644  /*
4645  * Allow only those users with admin rights
4646  */
4647  if (!capable(CAP_SYS_ADMIN))
4648  return -EACCES;
4649 
4650  return 0;
4651 }
4652 
4659 static 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  /* For sanity check when we get ioctl */
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 
4683 static 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 
4702 static int
4703 megasas_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  * User's IOCTL packet has 2 frames (maximum). Copy those two
4732  * frames into our cmd's frames. cmd->frame's context will get
4733  * overwritten when we copy from user's frames. So set that value
4734  * alone separately
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 |
4741 
4742  /*
4743  * The management interface between applications and the fw uses
4744  * MFI frames. E.g, RAID configuration changes, LD property changes
4745  * etc are accomplishes through different kinds of MFI frames. The
4746  * driver needs to care only about substituting user buffers with
4747  * kernel buffers in SGLs. The location of SGL is embedded in the
4748  * struct iocpacket itself.
4749  */
4750  kern_sge32 = (struct megasas_sge32 *)
4751  ((unsigned long)cmd->frame + ioc->sgl_off);
4752 
4753  /*
4754  * For each user buffer, create a mirror buffer and copy in
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  * We don't change the dma_coherent_mask, so
4772  * pci_alloc_consistent only returns 32bit addresses
4773  */
4774  kern_sge32[i].phys_addr = (u32) buf_handle;
4775  kern_sge32[i].length = ioc->sgl[i].iov_len;
4776 
4777  /*
4778  * We created a kernel buffer corresponding to the
4779  * user buffer. Now copy in from the user buffer
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  * Set the sync_cmd flag so that the ISR knows not to complete this
4803  * cmd to the SCSI mid-layer
4804  */
4805  cmd->sync_cmd = 1;
4806  megasas_issue_blocked_cmd(instance, cmd);
4807  cmd->sync_cmd = 0;
4808 
4809  /*
4810  * copy out the kernel buffers to user buffers
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  * copy out the sense
4822  */
4823  if (ioc->sense_len) {
4824  /*
4825  * sense_ptr points to the location that has the user
4826  * sense buffer address
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  * copy the status codes returned by the fw
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 
4865 static 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;
4889  }
4890 
4891  if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4892  printk(KERN_ERR "Controller in crit error\n");
4893  error = -ENODEV;
4894  goto out_kfree_ioc;
4895  }
4896 
4897  if (instance->unload == 1) {
4898  error = -ENODEV;
4899  goto out_kfree_ioc;
4900  }
4901 
4902  /*
4903  * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds
4904  */
4905  if (down_interruptible(&instance->ioctl_sem)) {
4906  error = -ERESTARTSYS;
4907  goto out_kfree_ioc;
4908  }
4909 
4910  for (i = 0; i < wait_time; i++) {
4911 
4912  spin_lock_irqsave(&instance->hba_lock, flags);
4913  if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4914  spin_unlock_irqrestore(&instance->hba_lock, flags);
4915  break;
4916  }
4917  spin_unlock_irqrestore(&instance->hba_lock, flags);
4918 
4919  if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4920  printk(KERN_NOTICE "megasas: waiting"
4921  "for controller reset to finish\n");
4922  }
4923 
4924  msleep(1000);
4925  }
4926 
4927  spin_lock_irqsave(&instance->hba_lock, flags);
4928  if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4929  spin_unlock_irqrestore(&instance->hba_lock, flags);
4930 
4931  printk(KERN_ERR "megaraid_sas: timed out while"
4932  "waiting for HBA to recover\n");
4933  error = -ENODEV;
4934  goto out_kfree_ioc;
4935  }
4936  spin_unlock_irqrestore(&instance->hba_lock, flags);
4937 
4938  error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
4939  up(&instance->ioctl_sem);
4940 
4941  out_kfree_ioc:
4942  kfree(ioc);
4943  return error;
4944 }
4945 
4946 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
4947 {
4948  struct megasas_instance *instance;
4949  struct megasas_aen aen;
4950  int error;
4951  int i;
4952  unsigned long flags;
4953  u32 wait_time = MEGASAS_RESET_WAIT_TIME;
4954 
4955  if (file->private_data != file) {
4956  printk(KERN_DEBUG "megasas: fasync_helper was not "
4957  "called first\n");
4958  return -EINVAL;
4959  }
4960 
4961  if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
4962  return -EFAULT;
4963 
4964  instance = megasas_lookup_instance(aen.host_no);
4965 
4966  if (!instance)
4967  return -ENODEV;
4968 
4969  if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
4970  return -ENODEV;
4971  }
4972 
4973  if (instance->unload == 1) {
4974  return -ENODEV;
4975  }
4976 
4977  for (i = 0; i < wait_time; i++) {
4978 
4979  spin_lock_irqsave(&instance->hba_lock, flags);
4980  if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
4981  spin_unlock_irqrestore(&instance->hba_lock,
4982  flags);
4983  break;
4984  }
4985 
4986  spin_unlock_irqrestore(&instance->hba_lock, flags);
4987 
4988  if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
4989  printk(KERN_NOTICE "megasas: waiting for"
4990  "controller reset to finish\n");
4991  }
4992 
4993  msleep(1000);
4994  }
4995 
4996  spin_lock_irqsave(&instance->hba_lock, flags);
4997  if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
4998  spin_unlock_irqrestore(&instance->hba_lock, flags);
4999  printk(KERN_ERR "megaraid_sas: timed out while waiting"
5000  "for HBA to recover.\n");
5001  return -ENODEV;
5002  }
5003  spin_unlock_irqrestore(&instance->hba_lock, flags);
5004 
5005  mutex_lock(&instance->aen_mutex);
5006  error = megasas_register_aen(instance, aen.seq_num,
5007  aen.class_locale_word);
5008  mutex_unlock(&instance->aen_mutex);
5009  return error;
5010 }
5011 
5015 static long
5016 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
5017 {
5018  switch (cmd) {
5019  case MEGASAS_IOC_FIRMWARE:
5020  return megasas_mgmt_ioctl_fw(file, arg);
5021 
5022  case MEGASAS_IOC_GET_AEN:
5023  return megasas_mgmt_ioctl_aen(file, arg);
5024  }
5025 
5026  return -ENOTTY;
5027 }
5028 
5029 #ifdef CONFIG_COMPAT
5030 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
5031 {
5032  struct compat_megasas_iocpacket __user *cioc =
5033  (struct compat_megasas_iocpacket __user *)arg;
5034  struct megasas_iocpacket __user *ioc =
5036  int i;
5037  int error = 0;
5039 
5040  if (clear_user(ioc, sizeof(*ioc)))
5041  return -EFAULT;
5042 
5043  if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
5044  copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
5045  copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
5046  copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
5047  copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
5048  copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
5049  return -EFAULT;
5050 
5051  /*
5052  * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
5053  * sense_len is not null, so prepare the 64bit value under
5054  * the same condition.
5055  */
5056  if (ioc->sense_len) {
5057  void __user **sense_ioc_ptr =
5058  (void __user **)(ioc->frame.raw + ioc->sense_off);
5059  compat_uptr_t *sense_cioc_ptr =
5060  (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off);
5061  if (get_user(ptr, sense_cioc_ptr) ||
5062  put_user(compat_ptr(ptr), sense_ioc_ptr))
5063  return -EFAULT;
5064  }
5065 
5066  for (i = 0; i < MAX_IOCTL_SGE; i++) {
5067  if (get_user(ptr, &cioc->sgl[i].iov_base) ||
5068  put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) ||
5069  copy_in_user(&ioc->sgl[i].iov_len,
5070  &cioc->sgl[i].iov_len, sizeof(compat_size_t)))
5071  return -EFAULT;
5072  }
5073 
5074  error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc);
5075 
5076  if (copy_in_user(&cioc->frame.hdr.cmd_status,
5077  &ioc->frame.hdr.cmd_status, sizeof(u8))) {
5078  printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n");
5079  return -EFAULT;
5080  }
5081  return error;
5082 }
5083 
5084 static long
5085 megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd,
5086  unsigned long arg)
5087 {
5088  switch (cmd) {
5089  case MEGASAS_IOC_FIRMWARE32:
5090  return megasas_mgmt_compat_ioctl_fw(file, arg);
5091  case MEGASAS_IOC_GET_AEN:
5092  return megasas_mgmt_ioctl_aen(file, arg);
5093  }
5094 
5095  return -ENOTTY;
5096 }
5097 #endif
5098 
5099 /*
5100  * File operations structure for management interface
5101  */
5102 static const struct file_operations megasas_mgmt_fops = {
5103  .owner = THIS_MODULE,
5104  .open = megasas_mgmt_open,
5105  .fasync = megasas_mgmt_fasync,
5106  .unlocked_ioctl = megasas_mgmt_ioctl,
5107  .poll = megasas_mgmt_poll,
5108 #ifdef CONFIG_COMPAT
5109  .compat_ioctl = megasas_mgmt_compat_ioctl,
5110 #endif
5111  .llseek = noop_llseek,
5112 };
5113 
5114 /*
5115  * PCI hotplug support registration structure
5116  */
5117 static struct pci_driver megasas_pci_driver = {
5118 
5119  .name = "megaraid_sas",
5120  .id_table = megasas_pci_table,
5121  .probe = megasas_probe_one,
5122  .remove = __devexit_p(megasas_detach_one),
5123  .suspend = megasas_suspend,
5124  .resume = megasas_resume,
5125  .shutdown = megasas_shutdown,
5126 };
5127 
5128 /*
5129  * Sysfs driver attributes
5130  */
5131 static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf)
5132 {
5133  return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n",
5134  MEGASAS_VERSION);
5135 }
5136 
5137 static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL);
5138 
5139 static ssize_t
5140 megasas_sysfs_show_release_date(struct device_driver *dd, char *buf)
5141 {
5142  return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n",
5143  MEGASAS_RELDATE);
5144 }
5145 
5146 static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date,
5147  NULL);
5148 
5149 static ssize_t
5150 megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf)
5151 {
5152  return sprintf(buf, "%u\n", support_poll_for_event);
5153 }
5154 
5155 static DRIVER_ATTR(support_poll_for_event, S_IRUGO,
5156  megasas_sysfs_show_support_poll_for_event, NULL);
5157 
5158  static ssize_t
5159 megasas_sysfs_show_support_device_change(struct device_driver *dd, char *buf)
5160 {
5161  return sprintf(buf, "%u\n", support_device_change);
5162 }
5163 
5164 static DRIVER_ATTR(support_device_change, S_IRUGO,
5165  megasas_sysfs_show_support_device_change, NULL);
5166 
5167 static ssize_t
5168 megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf)
5169 {
5170  return sprintf(buf, "%u\n", megasas_dbg_lvl);
5171 }
5172 
5173 static ssize_t
5174 megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count)
5175 {
5176  int retval = count;
5177  if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){
5178  printk(KERN_ERR "megasas: could not set dbg_lvl\n");
5179  retval = -EINVAL;
5180  }
5181  return retval;
5182 }
5183 
5184 static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl,
5185  megasas_sysfs_set_dbg_lvl);
5186 
5187 static void
5188 megasas_aen_polling(struct work_struct *work)
5189 {
5190  struct megasas_aen_event *ev =
5191  container_of(work, struct megasas_aen_event, hotplug_work.work);
5192  struct megasas_instance *instance = ev->instance;
5193  union megasas_evt_class_locale class_locale;
5194  struct Scsi_Host *host;
5195  struct scsi_device *sdev1;
5196  u16 pd_index = 0;
5197  u16 ld_index = 0;
5198  int i, j, doscan = 0;
5199  u32 seq_num;
5200  int error;
5201 
5202  if (!instance) {
5203  printk(KERN_ERR "invalid instance!\n");
5204  kfree(ev);
5205  return;
5206  }
5207  instance->ev = NULL;
5208  host = instance->host;
5209  if (instance->evt_detail) {
5210 
5211  switch (instance->evt_detail->code) {
5212  case MR_EVT_PD_INSERTED:
5213  if (megasas_get_pd_list(instance) == 0) {
5214  for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5215  for (j = 0;
5217  j++) {
5218 
5219  pd_index =
5220  (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5221 
5222  sdev1 =
5223  scsi_device_lookup(host, i, j, 0);
5224 
5225  if (instance->pd_list[pd_index].driveState
5226  == MR_PD_STATE_SYSTEM) {
5227  if (!sdev1) {
5228  scsi_add_device(host, i, j, 0);
5229  }
5230 
5231  if (sdev1)
5232  scsi_device_put(sdev1);
5233  }
5234  }
5235  }
5236  }
5237  doscan = 0;
5238  break;
5239 
5240  case MR_EVT_PD_REMOVED:
5241  if (megasas_get_pd_list(instance) == 0) {
5242  for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5243  for (j = 0;
5245  j++) {
5246 
5247  pd_index =
5248  (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5249 
5250  sdev1 =
5251  scsi_device_lookup(host, i, j, 0);
5252 
5253  if (instance->pd_list[pd_index].driveState
5254  == MR_PD_STATE_SYSTEM) {
5255  if (sdev1) {
5256  scsi_device_put(sdev1);
5257  }
5258  } else {
5259  if (sdev1) {
5260  scsi_remove_device(sdev1);
5261  scsi_device_put(sdev1);
5262  }
5263  }
5264  }
5265  }
5266  }
5267  doscan = 0;
5268  break;
5269 
5270  case MR_EVT_LD_OFFLINE:
5271  case MR_EVT_CFG_CLEARED:
5272  case MR_EVT_LD_DELETED:
5273  megasas_get_ld_list(instance);
5274  for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5275  for (j = 0;
5277  j++) {
5278 
5279  ld_index =
5280  (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5281 
5282  sdev1 = scsi_device_lookup(host,
5283  i + MEGASAS_MAX_LD_CHANNELS,
5284  j,
5285  0);
5286 
5287  if (instance->ld_ids[ld_index] != 0xff) {
5288  if (sdev1) {
5289  scsi_device_put(sdev1);
5290  }
5291  } else {
5292  if (sdev1) {
5293  scsi_remove_device(sdev1);
5294  scsi_device_put(sdev1);
5295  }
5296  }
5297  }
5298  }
5299  doscan = 0;
5300  break;
5301  case MR_EVT_LD_CREATED:
5302  megasas_get_ld_list(instance);
5303  for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5304  for (j = 0;
5306  j++) {
5307  ld_index =
5308  (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5309 
5310  sdev1 = scsi_device_lookup(host,
5311  i+MEGASAS_MAX_LD_CHANNELS,
5312  j, 0);
5313 
5314  if (instance->ld_ids[ld_index] !=
5315  0xff) {
5316  if (!sdev1) {
5317  scsi_add_device(host,
5318  i + 2,
5319  j, 0);
5320  }
5321  }
5322  if (sdev1) {
5323  scsi_device_put(sdev1);
5324  }
5325  }
5326  }
5327  doscan = 0;
5328  break;
5332  doscan = 1;
5333  break;
5334  default:
5335  doscan = 0;
5336  break;
5337  }
5338  } else {
5339  printk(KERN_ERR "invalid evt_detail!\n");
5340  kfree(ev);
5341  return;
5342  }
5343 
5344  if (doscan) {
5345  printk(KERN_INFO "scanning ...\n");
5346  megasas_get_pd_list(instance);
5347  for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) {
5348  for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5349  pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j;
5350  sdev1 = scsi_device_lookup(host, i, j, 0);
5351  if (instance->pd_list[pd_index].driveState ==
5353  if (!sdev1) {
5354  scsi_add_device(host, i, j, 0);
5355  }
5356  if (sdev1)
5357  scsi_device_put(sdev1);
5358  } else {
5359  if (sdev1) {
5360  scsi_remove_device(sdev1);
5361  scsi_device_put(sdev1);
5362  }
5363  }
5364  }
5365  }
5366 
5367  megasas_get_ld_list(instance);
5368  for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) {
5369  for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) {
5370  ld_index =
5371  (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j;
5372 
5373  sdev1 = scsi_device_lookup(host,
5374  i+MEGASAS_MAX_LD_CHANNELS, j, 0);
5375  if (instance->ld_ids[ld_index] != 0xff) {
5376  if (!sdev1) {
5377  scsi_add_device(host,
5378  i+2,
5379  j, 0);
5380  } else {
5381  scsi_device_put(sdev1);
5382  }
5383  } else {
5384  if (sdev1) {
5385  scsi_remove_device(sdev1);
5386  scsi_device_put(sdev1);
5387  }
5388  }
5389  }
5390  }
5391  }
5392 
5393  if ( instance->aen_cmd != NULL ) {
5394  kfree(ev);
5395  return ;
5396  }
5397 
5398  seq_num = instance->evt_detail->seq_num + 1;
5399 
5400  /* Register AEN with FW for latest sequence number plus 1 */
5401  class_locale.members.reserved = 0;
5402  class_locale.members.locale = MR_EVT_LOCALE_ALL;
5403  class_locale.members.class = MR_EVT_CLASS_DEBUG;
5404  mutex_lock(&instance->aen_mutex);
5405  error = megasas_register_aen(instance, seq_num,
5406  class_locale.word);
5407  mutex_unlock(&instance->aen_mutex);
5408 
5409  if (error)
5410  printk(KERN_ERR "register aen failed error %x\n", error);
5411 
5412  kfree(ev);
5413 }
5414 
5418 static int __init megasas_init(void)
5419 {
5420  int rval;
5421 
5422  /*
5423  * Announce driver version and other information
5424  */
5425  printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION,
5427 
5428  spin_lock_init(&poll_aen_lock);
5429 
5430  support_poll_for_event = 2;
5431  support_device_change = 1;
5432 
5434 
5435  /*
5436  * Register character device node
5437  */
5438  rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops);
5439 
5440  if (rval < 0) {
5441  printk(KERN_DEBUG "megasas: failed to open device node\n");
5442  return rval;
5443  }
5444 
5445  megasas_mgmt_majorno = rval;
5446 
5447  /*
5448  * Register ourselves as PCI hotplug module
5449  */
5450  rval = pci_register_driver(&megasas_pci_driver);
5451 
5452  if (rval) {
5453  printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n");
5454  goto err_pcidrv;
5455  }
5456 
5457  rval = driver_create_file(&megasas_pci_driver.driver,
5458  &driver_attr_version);
5459  if (rval)
5460  goto err_dcf_attr_ver;
5461  rval = driver_create_file(&megasas_pci_driver.driver,
5462  &driver_attr_release_date);
5463  if (rval)
5464  goto err_dcf_rel_date;
5465 
5466  rval = driver_create_file(&megasas_pci_driver.driver,
5467  &driver_attr_support_poll_for_event);
5468  if (rval)
5469  goto err_dcf_support_poll_for_event;
5470 
5471  rval = driver_create_file(&megasas_pci_driver.driver,
5472  &driver_attr_dbg_lvl);
5473  if (rval)
5474  goto err_dcf_dbg_lvl;
5475  rval = driver_create_file(&megasas_pci_driver.driver,
5476  &driver_attr_support_device_change);
5477  if (rval)
5478  goto err_dcf_support_device_change;
5479 
5480  return rval;
5481 
5482 err_dcf_support_device_change:
5483  driver_remove_file(&megasas_pci_driver.driver,
5484  &driver_attr_dbg_lvl);
5485 err_dcf_dbg_lvl:
5486  driver_remove_file(&megasas_pci_driver.driver,
5487  &driver_attr_support_poll_for_event);
5488 
5489 err_dcf_support_poll_for_event:
5490  driver_remove_file(&megasas_pci_driver.driver,
5491  &driver_attr_release_date);
5492 
5493 err_dcf_rel_date:
5494  driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5495 err_dcf_attr_ver:
5496  pci_unregister_driver(&megasas_pci_driver);
5497 err_pcidrv:
5498  unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5499  return rval;
5500 }
5501 
5505 static void __exit megasas_exit(void)
5506 {
5507  driver_remove_file(&megasas_pci_driver.driver,
5508  &driver_attr_dbg_lvl);
5509  driver_remove_file(&megasas_pci_driver.driver,
5510  &driver_attr_support_poll_for_event);
5511  driver_remove_file(&megasas_pci_driver.driver,
5512  &driver_attr_support_device_change);
5513  driver_remove_file(&megasas_pci_driver.driver,
5514  &driver_attr_release_date);
5515  driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version);
5516 
5517  pci_unregister_driver(&megasas_pci_driver);
5518  unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl");
5519 }
5520 
5521 module_init(megasas_init);
5522 module_exit(megasas_exit);