Version:  2.0.40 2.2.26 2.4.37 3.8 3.9 3.10 3.11 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4

Linux/drivers/scsi/megaraid/megaraid_sas_base.c

  1 /*
  2  *  Linux MegaRAID driver for SAS based RAID controllers
  3  *
  4  *  Copyright (c) 2003-2013  LSI Corporation
  5  *  Copyright (c) 2013-2014  Avago Technologies
  6  *
  7  *  This program is free software; you can redistribute it and/or
  8  *  modify it under the terms of the GNU General Public License
  9  *  as published by the Free Software Foundation; either version 2
 10  *  of the License, or (at your option) any later version.
 11  *
 12  *  This program is distributed in the hope that it will be useful,
 13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15  *  GNU General Public License for more details.
 16  *
 17  *  You should have received a copy of the GNU General Public License
 18  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 19  *
 20  *  Authors: Avago Technologies
 21  *           Sreenivas Bagalkote
 22  *           Sumant Patro
 23  *           Bo Yang
 24  *           Adam Radford
 25  *           Kashyap Desai <kashyap.desai@avagotech.com>
 26  *           Sumit Saxena <sumit.saxena@avagotech.com>
 27  *
 28  *  Send feedback to: megaraidlinux.pdl@avagotech.com
 29  *
 30  *  Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
 31  *  San Jose, California 95131
 32  */
 33 
 34 #include <linux/kernel.h>
 35 #include <linux/types.h>
 36 #include <linux/pci.h>
 37 #include <linux/list.h>
 38 #include <linux/moduleparam.h>
 39 #include <linux/module.h>
 40 #include <linux/spinlock.h>
 41 #include <linux/interrupt.h>
 42 #include <linux/delay.h>
 43 #include <linux/uio.h>
 44 #include <linux/slab.h>
 45 #include <asm/uaccess.h>
 46 #include <linux/fs.h>
 47 #include <linux/compat.h>
 48 #include <linux/blkdev.h>
 49 #include <linux/mutex.h>
 50 #include <linux/poll.h>
 51 
 52 #include <scsi/scsi.h>
 53 #include <scsi/scsi_cmnd.h>
 54 #include <scsi/scsi_device.h>
 55 #include <scsi/scsi_host.h>
 56 #include <scsi/scsi_tcq.h>
 57 #include "megaraid_sas_fusion.h"
 58 #include "megaraid_sas.h"
 59 
 60 /*
 61  * Number of sectors per IO command
 62  * Will be set in megasas_init_mfi if user does not provide
 63  */
 64 static unsigned int max_sectors;
 65 module_param_named(max_sectors, max_sectors, int, 0);
 66 MODULE_PARM_DESC(max_sectors,
 67         "Maximum number of sectors per IO command");
 68 
 69 static int msix_disable;
 70 module_param(msix_disable, int, S_IRUGO);
 71 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
 72 
 73 static unsigned int msix_vectors;
 74 module_param(msix_vectors, int, S_IRUGO);
 75 MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
 76 
 77 static int allow_vf_ioctls;
 78 module_param(allow_vf_ioctls, int, S_IRUGO);
 79 MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
 80 
 81 static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
 82 module_param(throttlequeuedepth, int, S_IRUGO);
 83 MODULE_PARM_DESC(throttlequeuedepth,
 84         "Adapter queue depth when throttled due to I/O timeout. Default: 16");
 85 
 86 int resetwaittime = MEGASAS_RESET_WAIT_TIME;
 87 module_param(resetwaittime, int, S_IRUGO);
 88 MODULE_PARM_DESC(resetwaittime, "Wait time in seconds after I/O timeout "
 89                  "before resetting adapter. Default: 180");
 90 
 91 int smp_affinity_enable = 1;
 92 module_param(smp_affinity_enable, int, S_IRUGO);
 93 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disbale Default: enable(1)");
 94 
 95 MODULE_LICENSE("GPL");
 96 MODULE_VERSION(MEGASAS_VERSION);
 97 MODULE_AUTHOR("megaraidlinux.pdl@avagotech.com");
 98 MODULE_DESCRIPTION("Avago MegaRAID SAS Driver");
 99 
100 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
101 static int megasas_get_pd_list(struct megasas_instance *instance);
102 static int megasas_ld_list_query(struct megasas_instance *instance,
103                                  u8 query_type);
104 static int megasas_issue_init_mfi(struct megasas_instance *instance);
105 static int megasas_register_aen(struct megasas_instance *instance,
106                                 u32 seq_num, u32 class_locale_word);
107 /*
108  * PCI ID table for all supported controllers
109  */
110 static struct pci_device_id megasas_pci_table[] = {
111 
112         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
113         /* xscale IOP */
114         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
115         /* ppc IOP */
116         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
117         /* ppc IOP */
118         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
119         /* gen2*/
120         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
121         /* gen2*/
122         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
123         /* skinny*/
124         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
125         /* skinny*/
126         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
127         /* xscale IOP, vega */
128         {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
129         /* xscale IOP */
130         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
131         /* Fusion */
132         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
133         /* Plasma */
134         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
135         /* Invader */
136         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
137         /* Fury */
138         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
139         /* Intruder */
140         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
141         /* Intruder 24 port*/
142         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
143         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
144         {}
145 };
146 
147 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
148 
149 static int megasas_mgmt_majorno;
150 struct megasas_mgmt_info megasas_mgmt_info;
151 static struct fasync_struct *megasas_async_queue;
152 static DEFINE_MUTEX(megasas_async_queue_mutex);
153 
154 static int megasas_poll_wait_aen;
155 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
156 static u32 support_poll_for_event;
157 u32 megasas_dbg_lvl;
158 static u32 support_device_change;
159 
160 /* define lock for aen poll */
161 spinlock_t poll_aen_lock;
162 
163 void
164 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
165                      u8 alt_status);
166 static u32
167 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs);
168 static int
169 megasas_adp_reset_gen2(struct megasas_instance *instance,
170                        struct megasas_register_set __iomem *reg_set);
171 static irqreturn_t megasas_isr(int irq, void *devp);
172 static u32
173 megasas_init_adapter_mfi(struct megasas_instance *instance);
174 u32
175 megasas_build_and_issue_cmd(struct megasas_instance *instance,
176                             struct scsi_cmnd *scmd);
177 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
178 void
179 megasas_release_fusion(struct megasas_instance *instance);
180 int
181 megasas_ioc_init_fusion(struct megasas_instance *instance);
182 void
183 megasas_free_cmds_fusion(struct megasas_instance *instance);
184 u8
185 megasas_get_map_info(struct megasas_instance *instance);
186 int
187 megasas_sync_map_info(struct megasas_instance *instance);
188 int
189 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
190         int seconds);
191 void megasas_reset_reply_desc(struct megasas_instance *instance);
192 int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout);
193 void megasas_fusion_ocr_wq(struct work_struct *work);
194 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
195                                          int initial);
196 int megasas_check_mpio_paths(struct megasas_instance *instance,
197                              struct scsi_cmnd *scmd);
198 
199 void
200 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
201 {
202         instance->instancet->fire_cmd(instance,
203                 cmd->frame_phys_addr, 0, instance->reg_set);
204 }
205 
206 /**
207  * megasas_get_cmd -    Get a command from the free pool
208  * @instance:           Adapter soft state
209  *
210  * Returns a free command from the pool
211  */
212 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
213                                                   *instance)
214 {
215         unsigned long flags;
216         struct megasas_cmd *cmd = NULL;
217 
218         spin_lock_irqsave(&instance->mfi_pool_lock, flags);
219 
220         if (!list_empty(&instance->cmd_pool)) {
221                 cmd = list_entry((&instance->cmd_pool)->next,
222                                  struct megasas_cmd, list);
223                 list_del_init(&cmd->list);
224         } else {
225                 dev_err(&instance->pdev->dev, "Command pool empty!\n");
226         }
227 
228         spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
229         return cmd;
230 }
231 
232 /**
233  * megasas_return_cmd - Return a cmd to free command pool
234  * @instance:           Adapter soft state
235  * @cmd:                Command packet to be returned to free command pool
236  */
237 inline void
238 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
239 {
240         unsigned long flags;
241         u32 blk_tags;
242         struct megasas_cmd_fusion *cmd_fusion;
243         struct fusion_context *fusion = instance->ctrl_context;
244 
245         /* This flag is used only for fusion adapter.
246          * Wait for Interrupt for Polled mode DCMD
247          */
248         if (cmd->flags & DRV_DCMD_POLLED_MODE)
249                 return;
250 
251         spin_lock_irqsave(&instance->mfi_pool_lock, flags);
252 
253         if (fusion) {
254                 blk_tags = instance->max_scsi_cmds + cmd->index;
255                 cmd_fusion = fusion->cmd_list[blk_tags];
256                 megasas_return_cmd_fusion(instance, cmd_fusion);
257         }
258         cmd->scmd = NULL;
259         cmd->frame_count = 0;
260         cmd->flags = 0;
261         if (!fusion && reset_devices)
262                 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
263         list_add(&cmd->list, (&instance->cmd_pool)->next);
264 
265         spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
266 
267 }
268 
269 static const char *
270 format_timestamp(uint32_t timestamp)
271 {
272         static char buffer[32];
273 
274         if ((timestamp & 0xff000000) == 0xff000000)
275                 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
276                 0x00ffffff);
277         else
278                 snprintf(buffer, sizeof(buffer), "%us", timestamp);
279         return buffer;
280 }
281 
282 static const char *
283 format_class(int8_t class)
284 {
285         static char buffer[6];
286 
287         switch (class) {
288         case MFI_EVT_CLASS_DEBUG:
289                 return "debug";
290         case MFI_EVT_CLASS_PROGRESS:
291                 return "progress";
292         case MFI_EVT_CLASS_INFO:
293                 return "info";
294         case MFI_EVT_CLASS_WARNING:
295                 return "WARN";
296         case MFI_EVT_CLASS_CRITICAL:
297                 return "CRIT";
298         case MFI_EVT_CLASS_FATAL:
299                 return "FATAL";
300         case MFI_EVT_CLASS_DEAD:
301                 return "DEAD";
302         default:
303                 snprintf(buffer, sizeof(buffer), "%d", class);
304                 return buffer;
305         }
306 }
307 
308 /**
309   * megasas_decode_evt: Decode FW AEN event and print critical event
310   * for information.
311   * @instance:                  Adapter soft state
312   */
313 static void
314 megasas_decode_evt(struct megasas_instance *instance)
315 {
316         struct megasas_evt_detail *evt_detail = instance->evt_detail;
317         union megasas_evt_class_locale class_locale;
318         class_locale.word = le32_to_cpu(evt_detail->cl.word);
319 
320         if (class_locale.members.class >= MFI_EVT_CLASS_CRITICAL)
321                 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
322                         le32_to_cpu(evt_detail->seq_num),
323                         format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
324                         (class_locale.members.locale),
325                         format_class(class_locale.members.class),
326                         evt_detail->description);
327 }
328 
329 /**
330 *       The following functions are defined for xscale
331 *       (deviceid : 1064R, PERC5) controllers
332 */
333 
334 /**
335  * megasas_enable_intr_xscale - Enables interrupts
336  * @regs:                       MFI register set
337  */
338 static inline void
339 megasas_enable_intr_xscale(struct megasas_instance *instance)
340 {
341         struct megasas_register_set __iomem *regs;
342 
343         regs = instance->reg_set;
344         writel(0, &(regs)->outbound_intr_mask);
345 
346         /* Dummy readl to force pci flush */
347         readl(&regs->outbound_intr_mask);
348 }
349 
350 /**
351  * megasas_disable_intr_xscale -Disables interrupt
352  * @regs:                       MFI register set
353  */
354 static inline void
355 megasas_disable_intr_xscale(struct megasas_instance *instance)
356 {
357         struct megasas_register_set __iomem *regs;
358         u32 mask = 0x1f;
359 
360         regs = instance->reg_set;
361         writel(mask, &regs->outbound_intr_mask);
362         /* Dummy readl to force pci flush */
363         readl(&regs->outbound_intr_mask);
364 }
365 
366 /**
367  * megasas_read_fw_status_reg_xscale - returns the current FW status value
368  * @regs:                       MFI register set
369  */
370 static u32
371 megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs)
372 {
373         return readl(&(regs)->outbound_msg_0);
374 }
375 /**
376  * megasas_clear_interrupt_xscale -     Check & clear interrupt
377  * @regs:                               MFI register set
378  */
379 static int
380 megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs)
381 {
382         u32 status;
383         u32 mfiStatus = 0;
384 
385         /*
386          * Check if it is our interrupt
387          */
388         status = readl(&regs->outbound_intr_status);
389 
390         if (status & MFI_OB_INTR_STATUS_MASK)
391                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
392         if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
393                 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
394 
395         /*
396          * Clear the interrupt by writing back the same value
397          */
398         if (mfiStatus)
399                 writel(status, &regs->outbound_intr_status);
400 
401         /* Dummy readl to force pci flush */
402         readl(&regs->outbound_intr_status);
403 
404         return mfiStatus;
405 }
406 
407 /**
408  * megasas_fire_cmd_xscale -    Sends command to the FW
409  * @frame_phys_addr :           Physical address of cmd
410  * @frame_count :               Number of frames for the command
411  * @regs :                      MFI register set
412  */
413 static inline void
414 megasas_fire_cmd_xscale(struct megasas_instance *instance,
415                 dma_addr_t frame_phys_addr,
416                 u32 frame_count,
417                 struct megasas_register_set __iomem *regs)
418 {
419         unsigned long flags;
420 
421         spin_lock_irqsave(&instance->hba_lock, flags);
422         writel((frame_phys_addr >> 3)|(frame_count),
423                &(regs)->inbound_queue_port);
424         spin_unlock_irqrestore(&instance->hba_lock, flags);
425 }
426 
427 /**
428  * megasas_adp_reset_xscale -  For controller reset
429  * @regs:                              MFI register set
430  */
431 static int
432 megasas_adp_reset_xscale(struct megasas_instance *instance,
433         struct megasas_register_set __iomem *regs)
434 {
435         u32 i;
436         u32 pcidata;
437 
438         writel(MFI_ADP_RESET, &regs->inbound_doorbell);
439 
440         for (i = 0; i < 3; i++)
441                 msleep(1000); /* sleep for 3 secs */
442         pcidata  = 0;
443         pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
444         dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
445         if (pcidata & 0x2) {
446                 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
447                 pcidata &= ~0x2;
448                 pci_write_config_dword(instance->pdev,
449                                 MFI_1068_PCSR_OFFSET, pcidata);
450 
451                 for (i = 0; i < 2; i++)
452                         msleep(1000); /* need to wait 2 secs again */
453 
454                 pcidata  = 0;
455                 pci_read_config_dword(instance->pdev,
456                                 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
457                 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
458                 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
459                         dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
460                         pcidata = 0;
461                         pci_write_config_dword(instance->pdev,
462                                 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
463                 }
464         }
465         return 0;
466 }
467 
468 /**
469  * megasas_check_reset_xscale - For controller reset check
470  * @regs:                               MFI register set
471  */
472 static int
473 megasas_check_reset_xscale(struct megasas_instance *instance,
474                 struct megasas_register_set __iomem *regs)
475 {
476         if ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
477             (le32_to_cpu(*instance->consumer) ==
478                 MEGASAS_ADPRESET_INPROG_SIGN))
479                 return 1;
480         return 0;
481 }
482 
483 static struct megasas_instance_template megasas_instance_template_xscale = {
484 
485         .fire_cmd = megasas_fire_cmd_xscale,
486         .enable_intr = megasas_enable_intr_xscale,
487         .disable_intr = megasas_disable_intr_xscale,
488         .clear_intr = megasas_clear_intr_xscale,
489         .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
490         .adp_reset = megasas_adp_reset_xscale,
491         .check_reset = megasas_check_reset_xscale,
492         .service_isr = megasas_isr,
493         .tasklet = megasas_complete_cmd_dpc,
494         .init_adapter = megasas_init_adapter_mfi,
495         .build_and_issue_cmd = megasas_build_and_issue_cmd,
496         .issue_dcmd = megasas_issue_dcmd,
497 };
498 
499 /**
500 *       This is the end of set of functions & definitions specific
501 *       to xscale (deviceid : 1064R, PERC5) controllers
502 */
503 
504 /**
505 *       The following functions are defined for ppc (deviceid : 0x60)
506 *       controllers
507 */
508 
509 /**
510  * megasas_enable_intr_ppc -    Enables interrupts
511  * @regs:                       MFI register set
512  */
513 static inline void
514 megasas_enable_intr_ppc(struct megasas_instance *instance)
515 {
516         struct megasas_register_set __iomem *regs;
517 
518         regs = instance->reg_set;
519         writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
520 
521         writel(~0x80000000, &(regs)->outbound_intr_mask);
522 
523         /* Dummy readl to force pci flush */
524         readl(&regs->outbound_intr_mask);
525 }
526 
527 /**
528  * megasas_disable_intr_ppc -   Disable interrupt
529  * @regs:                       MFI register set
530  */
531 static inline void
532 megasas_disable_intr_ppc(struct megasas_instance *instance)
533 {
534         struct megasas_register_set __iomem *regs;
535         u32 mask = 0xFFFFFFFF;
536 
537         regs = instance->reg_set;
538         writel(mask, &regs->outbound_intr_mask);
539         /* Dummy readl to force pci flush */
540         readl(&regs->outbound_intr_mask);
541 }
542 
543 /**
544  * megasas_read_fw_status_reg_ppc - returns the current FW status value
545  * @regs:                       MFI register set
546  */
547 static u32
548 megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs)
549 {
550         return readl(&(regs)->outbound_scratch_pad);
551 }
552 
553 /**
554  * megasas_clear_interrupt_ppc -        Check & clear interrupt
555  * @regs:                               MFI register set
556  */
557 static int
558 megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs)
559 {
560         u32 status, mfiStatus = 0;
561 
562         /*
563          * Check if it is our interrupt
564          */
565         status = readl(&regs->outbound_intr_status);
566 
567         if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
568                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
569 
570         if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
571                 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
572 
573         /*
574          * Clear the interrupt by writing back the same value
575          */
576         writel(status, &regs->outbound_doorbell_clear);
577 
578         /* Dummy readl to force pci flush */
579         readl(&regs->outbound_doorbell_clear);
580 
581         return mfiStatus;
582 }
583 
584 /**
585  * megasas_fire_cmd_ppc -       Sends command to the FW
586  * @frame_phys_addr :           Physical address of cmd
587  * @frame_count :               Number of frames for the command
588  * @regs :                      MFI register set
589  */
590 static inline void
591 megasas_fire_cmd_ppc(struct megasas_instance *instance,
592                 dma_addr_t frame_phys_addr,
593                 u32 frame_count,
594                 struct megasas_register_set __iomem *regs)
595 {
596         unsigned long flags;
597 
598         spin_lock_irqsave(&instance->hba_lock, flags);
599         writel((frame_phys_addr | (frame_count<<1))|1,
600                         &(regs)->inbound_queue_port);
601         spin_unlock_irqrestore(&instance->hba_lock, flags);
602 }
603 
604 /**
605  * megasas_check_reset_ppc -    For controller reset check
606  * @regs:                               MFI register set
607  */
608 static int
609 megasas_check_reset_ppc(struct megasas_instance *instance,
610                         struct megasas_register_set __iomem *regs)
611 {
612         if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
613                 return 1;
614 
615         return 0;
616 }
617 
618 static struct megasas_instance_template megasas_instance_template_ppc = {
619 
620         .fire_cmd = megasas_fire_cmd_ppc,
621         .enable_intr = megasas_enable_intr_ppc,
622         .disable_intr = megasas_disable_intr_ppc,
623         .clear_intr = megasas_clear_intr_ppc,
624         .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
625         .adp_reset = megasas_adp_reset_xscale,
626         .check_reset = megasas_check_reset_ppc,
627         .service_isr = megasas_isr,
628         .tasklet = megasas_complete_cmd_dpc,
629         .init_adapter = megasas_init_adapter_mfi,
630         .build_and_issue_cmd = megasas_build_and_issue_cmd,
631         .issue_dcmd = megasas_issue_dcmd,
632 };
633 
634 /**
635  * megasas_enable_intr_skinny - Enables interrupts
636  * @regs:                       MFI register set
637  */
638 static inline void
639 megasas_enable_intr_skinny(struct megasas_instance *instance)
640 {
641         struct megasas_register_set __iomem *regs;
642 
643         regs = instance->reg_set;
644         writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
645 
646         writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
647 
648         /* Dummy readl to force pci flush */
649         readl(&regs->outbound_intr_mask);
650 }
651 
652 /**
653  * megasas_disable_intr_skinny -        Disables interrupt
654  * @regs:                       MFI register set
655  */
656 static inline void
657 megasas_disable_intr_skinny(struct megasas_instance *instance)
658 {
659         struct megasas_register_set __iomem *regs;
660         u32 mask = 0xFFFFFFFF;
661 
662         regs = instance->reg_set;
663         writel(mask, &regs->outbound_intr_mask);
664         /* Dummy readl to force pci flush */
665         readl(&regs->outbound_intr_mask);
666 }
667 
668 /**
669  * megasas_read_fw_status_reg_skinny - returns the current FW status value
670  * @regs:                       MFI register set
671  */
672 static u32
673 megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs)
674 {
675         return readl(&(regs)->outbound_scratch_pad);
676 }
677 
678 /**
679  * megasas_clear_interrupt_skinny -     Check & clear interrupt
680  * @regs:                               MFI register set
681  */
682 static int
683 megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs)
684 {
685         u32 status;
686         u32 mfiStatus = 0;
687 
688         /*
689          * Check if it is our interrupt
690          */
691         status = readl(&regs->outbound_intr_status);
692 
693         if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
694                 return 0;
695         }
696 
697         /*
698          * Check if it is our interrupt
699          */
700         if ((megasas_read_fw_status_reg_skinny(regs) & MFI_STATE_MASK) ==
701             MFI_STATE_FAULT) {
702                 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
703         } else
704                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
705 
706         /*
707          * Clear the interrupt by writing back the same value
708          */
709         writel(status, &regs->outbound_intr_status);
710 
711         /*
712          * dummy read to flush PCI
713          */
714         readl(&regs->outbound_intr_status);
715 
716         return mfiStatus;
717 }
718 
719 /**
720  * megasas_fire_cmd_skinny -    Sends command to the FW
721  * @frame_phys_addr :           Physical address of cmd
722  * @frame_count :               Number of frames for the command
723  * @regs :                      MFI register set
724  */
725 static inline void
726 megasas_fire_cmd_skinny(struct megasas_instance *instance,
727                         dma_addr_t frame_phys_addr,
728                         u32 frame_count,
729                         struct megasas_register_set __iomem *regs)
730 {
731         unsigned long flags;
732 
733         spin_lock_irqsave(&instance->hba_lock, flags);
734         writel(upper_32_bits(frame_phys_addr),
735                &(regs)->inbound_high_queue_port);
736         writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
737                &(regs)->inbound_low_queue_port);
738         spin_unlock_irqrestore(&instance->hba_lock, flags);
739 }
740 
741 /**
742  * megasas_check_reset_skinny - For controller reset check
743  * @regs:                               MFI register set
744  */
745 static int
746 megasas_check_reset_skinny(struct megasas_instance *instance,
747                                 struct megasas_register_set __iomem *regs)
748 {
749         if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL)
750                 return 1;
751 
752         return 0;
753 }
754 
755 static struct megasas_instance_template megasas_instance_template_skinny = {
756 
757         .fire_cmd = megasas_fire_cmd_skinny,
758         .enable_intr = megasas_enable_intr_skinny,
759         .disable_intr = megasas_disable_intr_skinny,
760         .clear_intr = megasas_clear_intr_skinny,
761         .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
762         .adp_reset = megasas_adp_reset_gen2,
763         .check_reset = megasas_check_reset_skinny,
764         .service_isr = megasas_isr,
765         .tasklet = megasas_complete_cmd_dpc,
766         .init_adapter = megasas_init_adapter_mfi,
767         .build_and_issue_cmd = megasas_build_and_issue_cmd,
768         .issue_dcmd = megasas_issue_dcmd,
769 };
770 
771 
772 /**
773 *       The following functions are defined for gen2 (deviceid : 0x78 0x79)
774 *       controllers
775 */
776 
777 /**
778  * megasas_enable_intr_gen2 -  Enables interrupts
779  * @regs:                      MFI register set
780  */
781 static inline void
782 megasas_enable_intr_gen2(struct megasas_instance *instance)
783 {
784         struct megasas_register_set __iomem *regs;
785 
786         regs = instance->reg_set;
787         writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
788 
789         /* write ~0x00000005 (4 & 1) to the intr mask*/
790         writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
791 
792         /* Dummy readl to force pci flush */
793         readl(&regs->outbound_intr_mask);
794 }
795 
796 /**
797  * megasas_disable_intr_gen2 - Disables interrupt
798  * @regs:                      MFI register set
799  */
800 static inline void
801 megasas_disable_intr_gen2(struct megasas_instance *instance)
802 {
803         struct megasas_register_set __iomem *regs;
804         u32 mask = 0xFFFFFFFF;
805 
806         regs = instance->reg_set;
807         writel(mask, &regs->outbound_intr_mask);
808         /* Dummy readl to force pci flush */
809         readl(&regs->outbound_intr_mask);
810 }
811 
812 /**
813  * megasas_read_fw_status_reg_gen2 - returns the current FW status value
814  * @regs:                      MFI register set
815  */
816 static u32
817 megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs)
818 {
819         return readl(&(regs)->outbound_scratch_pad);
820 }
821 
822 /**
823  * megasas_clear_interrupt_gen2 -      Check & clear interrupt
824  * @regs:                              MFI register set
825  */
826 static int
827 megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs)
828 {
829         u32 status;
830         u32 mfiStatus = 0;
831 
832         /*
833          * Check if it is our interrupt
834          */
835         status = readl(&regs->outbound_intr_status);
836 
837         if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
838                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
839         }
840         if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
841                 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
842         }
843 
844         /*
845          * Clear the interrupt by writing back the same value
846          */
847         if (mfiStatus)
848                 writel(status, &regs->outbound_doorbell_clear);
849 
850         /* Dummy readl to force pci flush */
851         readl(&regs->outbound_intr_status);
852 
853         return mfiStatus;
854 }
855 /**
856  * megasas_fire_cmd_gen2 -     Sends command to the FW
857  * @frame_phys_addr :          Physical address of cmd
858  * @frame_count :              Number of frames for the command
859  * @regs :                     MFI register set
860  */
861 static inline void
862 megasas_fire_cmd_gen2(struct megasas_instance *instance,
863                         dma_addr_t frame_phys_addr,
864                         u32 frame_count,
865                         struct megasas_register_set __iomem *regs)
866 {
867         unsigned long flags;
868 
869         spin_lock_irqsave(&instance->hba_lock, flags);
870         writel((frame_phys_addr | (frame_count<<1))|1,
871                         &(regs)->inbound_queue_port);
872         spin_unlock_irqrestore(&instance->hba_lock, flags);
873 }
874 
875 /**
876  * megasas_adp_reset_gen2 -     For controller reset
877  * @regs:                               MFI register set
878  */
879 static int
880 megasas_adp_reset_gen2(struct megasas_instance *instance,
881                         struct megasas_register_set __iomem *reg_set)
882 {
883         u32 retry = 0 ;
884         u32 HostDiag;
885         u32 __iomem *seq_offset = &reg_set->seq_offset;
886         u32 __iomem *hostdiag_offset = &reg_set->host_diag;
887 
888         if (instance->instancet == &megasas_instance_template_skinny) {
889                 seq_offset = &reg_set->fusion_seq_offset;
890                 hostdiag_offset = &reg_set->fusion_host_diag;
891         }
892 
893         writel(0, seq_offset);
894         writel(4, seq_offset);
895         writel(0xb, seq_offset);
896         writel(2, seq_offset);
897         writel(7, seq_offset);
898         writel(0xd, seq_offset);
899 
900         msleep(1000);
901 
902         HostDiag = (u32)readl(hostdiag_offset);
903 
904         while (!(HostDiag & DIAG_WRITE_ENABLE)) {
905                 msleep(100);
906                 HostDiag = (u32)readl(hostdiag_offset);
907                 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
908                                         retry, HostDiag);
909 
910                 if (retry++ >= 100)
911                         return 1;
912 
913         }
914 
915         dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
916 
917         writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
918 
919         ssleep(10);
920 
921         HostDiag = (u32)readl(hostdiag_offset);
922         while (HostDiag & DIAG_RESET_ADAPTER) {
923                 msleep(100);
924                 HostDiag = (u32)readl(hostdiag_offset);
925                 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
926                                 retry, HostDiag);
927 
928                 if (retry++ >= 1000)
929                         return 1;
930 
931         }
932         return 0;
933 }
934 
935 /**
936  * megasas_check_reset_gen2 -   For controller reset check
937  * @regs:                               MFI register set
938  */
939 static int
940 megasas_check_reset_gen2(struct megasas_instance *instance,
941                 struct megasas_register_set __iomem *regs)
942 {
943         if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
944                 return 1;
945         }
946 
947         return 0;
948 }
949 
950 static struct megasas_instance_template megasas_instance_template_gen2 = {
951 
952         .fire_cmd = megasas_fire_cmd_gen2,
953         .enable_intr = megasas_enable_intr_gen2,
954         .disable_intr = megasas_disable_intr_gen2,
955         .clear_intr = megasas_clear_intr_gen2,
956         .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
957         .adp_reset = megasas_adp_reset_gen2,
958         .check_reset = megasas_check_reset_gen2,
959         .service_isr = megasas_isr,
960         .tasklet = megasas_complete_cmd_dpc,
961         .init_adapter = megasas_init_adapter_mfi,
962         .build_and_issue_cmd = megasas_build_and_issue_cmd,
963         .issue_dcmd = megasas_issue_dcmd,
964 };
965 
966 /**
967 *       This is the end of set of functions & definitions
968 *       specific to gen2 (deviceid : 0x78, 0x79) controllers
969 */
970 
971 /*
972  * Template added for TB (Fusion)
973  */
974 extern struct megasas_instance_template megasas_instance_template_fusion;
975 
976 /**
977  * megasas_issue_polled -       Issues a polling command
978  * @instance:                   Adapter soft state
979  * @cmd:                        Command packet to be issued
980  *
981  * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
982  */
983 int
984 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
985 {
986         int seconds;
987         struct megasas_header *frame_hdr = &cmd->frame->hdr;
988 
989         frame_hdr->cmd_status = MFI_CMD_STATUS_POLL_MODE;
990         frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
991 
992         /*
993          * Issue the frame using inbound queue port
994          */
995         instance->instancet->issue_dcmd(instance, cmd);
996 
997         /*
998          * Wait for cmd_status to change
999          */
1000         if (instance->requestorId)
1001                 seconds = MEGASAS_ROUTINE_WAIT_TIME_VF;
1002         else
1003                 seconds = MFI_POLL_TIMEOUT_SECS;
1004         return wait_and_poll(instance, cmd, seconds);
1005 }
1006 
1007 /**
1008  * megasas_issue_blocked_cmd -  Synchronous wrapper around regular FW cmds
1009  * @instance:                   Adapter soft state
1010  * @cmd:                        Command to be issued
1011  * @timeout:                    Timeout in seconds
1012  *
1013  * This function waits on an event for the command to be returned from ISR.
1014  * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1015  * Used to issue ioctl commands.
1016  */
1017 int
1018 megasas_issue_blocked_cmd(struct megasas_instance *instance,
1019                           struct megasas_cmd *cmd, int timeout)
1020 {
1021         int ret = 0;
1022 
1023         cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1024 
1025         instance->instancet->issue_dcmd(instance, cmd);
1026         if (timeout) {
1027                 ret = wait_event_timeout(instance->int_cmd_wait_q,
1028                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1029                 if (!ret)
1030                         return 1;
1031         } else
1032                 wait_event(instance->int_cmd_wait_q,
1033                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1034 
1035         return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1036                 0 : 1;
1037 }
1038 
1039 /**
1040  * megasas_issue_blocked_abort_cmd -    Aborts previously issued cmd
1041  * @instance:                           Adapter soft state
1042  * @cmd_to_abort:                       Previously issued cmd to be aborted
1043  * @timeout:                            Timeout in seconds
1044  *
1045  * MFI firmware can abort previously issued AEN comamnd (automatic event
1046  * notification). The megasas_issue_blocked_abort_cmd() issues such abort
1047  * cmd and waits for return status.
1048  * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1049  */
1050 static int
1051 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
1052                                 struct megasas_cmd *cmd_to_abort, int timeout)
1053 {
1054         struct megasas_cmd *cmd;
1055         struct megasas_abort_frame *abort_fr;
1056         int ret = 0;
1057 
1058         cmd = megasas_get_cmd(instance);
1059 
1060         if (!cmd)
1061                 return -1;
1062 
1063         abort_fr = &cmd->frame->abort;
1064 
1065         /*
1066          * Prepare and issue the abort frame
1067          */
1068         abort_fr->cmd = MFI_CMD_ABORT;
1069         abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
1070         abort_fr->flags = cpu_to_le16(0);
1071         abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1072         abort_fr->abort_mfi_phys_addr_lo =
1073                 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1074         abort_fr->abort_mfi_phys_addr_hi =
1075                 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
1076 
1077         cmd->sync_cmd = 1;
1078         cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1079 
1080         instance->instancet->issue_dcmd(instance, cmd);
1081 
1082         if (timeout) {
1083                 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1084                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1085                 if (!ret) {
1086                         dev_err(&instance->pdev->dev, "Command timedout"
1087                                 "from %s\n", __func__);
1088                         return 1;
1089                 }
1090         } else
1091                 wait_event(instance->abort_cmd_wait_q,
1092                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1093 
1094         cmd->sync_cmd = 0;
1095 
1096         megasas_return_cmd(instance, cmd);
1097         return 0;
1098 }
1099 
1100 /**
1101  * megasas_make_sgl32 - Prepares 32-bit SGL
1102  * @instance:           Adapter soft state
1103  * @scp:                SCSI command from the mid-layer
1104  * @mfi_sgl:            SGL to be filled in
1105  *
1106  * If successful, this function returns the number of SG elements. Otherwise,
1107  * it returnes -1.
1108  */
1109 static int
1110 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1111                    union megasas_sgl *mfi_sgl)
1112 {
1113         int i;
1114         int sge_count;
1115         struct scatterlist *os_sgl;
1116 
1117         sge_count = scsi_dma_map(scp);
1118         BUG_ON(sge_count < 0);
1119 
1120         if (sge_count) {
1121                 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1122                         mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1123                         mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1124                 }
1125         }
1126         return sge_count;
1127 }
1128 
1129 /**
1130  * megasas_make_sgl64 - Prepares 64-bit SGL
1131  * @instance:           Adapter soft state
1132  * @scp:                SCSI command from the mid-layer
1133  * @mfi_sgl:            SGL to be filled in
1134  *
1135  * If successful, this function returns the number of SG elements. Otherwise,
1136  * it returnes -1.
1137  */
1138 static int
1139 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1140                    union megasas_sgl *mfi_sgl)
1141 {
1142         int i;
1143         int sge_count;
1144         struct scatterlist *os_sgl;
1145 
1146         sge_count = scsi_dma_map(scp);
1147         BUG_ON(sge_count < 0);
1148 
1149         if (sge_count) {
1150                 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1151                         mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1152                         mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1153                 }
1154         }
1155         return sge_count;
1156 }
1157 
1158 /**
1159  * megasas_make_sgl_skinny - Prepares IEEE SGL
1160  * @instance:           Adapter soft state
1161  * @scp:                SCSI command from the mid-layer
1162  * @mfi_sgl:            SGL to be filled in
1163  *
1164  * If successful, this function returns the number of SG elements. Otherwise,
1165  * it returnes -1.
1166  */
1167 static int
1168 megasas_make_sgl_skinny(struct megasas_instance *instance,
1169                 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1170 {
1171         int i;
1172         int sge_count;
1173         struct scatterlist *os_sgl;
1174 
1175         sge_count = scsi_dma_map(scp);
1176 
1177         if (sge_count) {
1178                 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1179                         mfi_sgl->sge_skinny[i].length =
1180                                 cpu_to_le32(sg_dma_len(os_sgl));
1181                         mfi_sgl->sge_skinny[i].phys_addr =
1182                                 cpu_to_le64(sg_dma_address(os_sgl));
1183                         mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1184                 }
1185         }
1186         return sge_count;
1187 }
1188 
1189  /**
1190  * megasas_get_frame_count - Computes the number of frames
1191  * @frame_type          : type of frame- io or pthru frame
1192  * @sge_count           : number of sg elements
1193  *
1194  * Returns the number of frames required for numnber of sge's (sge_count)
1195  */
1196 
1197 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1198                         u8 sge_count, u8 frame_type)
1199 {
1200         int num_cnt;
1201         int sge_bytes;
1202         u32 sge_sz;
1203         u32 frame_count = 0;
1204 
1205         sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1206             sizeof(struct megasas_sge32);
1207 
1208         if (instance->flag_ieee) {
1209                 sge_sz = sizeof(struct megasas_sge_skinny);
1210         }
1211 
1212         /*
1213          * Main frame can contain 2 SGEs for 64-bit SGLs and
1214          * 3 SGEs for 32-bit SGLs for ldio &
1215          * 1 SGEs for 64-bit SGLs and
1216          * 2 SGEs for 32-bit SGLs for pthru frame
1217          */
1218         if (unlikely(frame_type == PTHRU_FRAME)) {
1219                 if (instance->flag_ieee == 1) {
1220                         num_cnt = sge_count - 1;
1221                 } else if (IS_DMA64)
1222                         num_cnt = sge_count - 1;
1223                 else
1224                         num_cnt = sge_count - 2;
1225         } else {
1226                 if (instance->flag_ieee == 1) {
1227                         num_cnt = sge_count - 1;
1228                 } else if (IS_DMA64)
1229                         num_cnt = sge_count - 2;
1230                 else
1231                         num_cnt = sge_count - 3;
1232         }
1233 
1234         if (num_cnt > 0) {
1235                 sge_bytes = sge_sz * num_cnt;
1236 
1237                 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1238                     ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1239         }
1240         /* Main frame */
1241         frame_count += 1;
1242 
1243         if (frame_count > 7)
1244                 frame_count = 8;
1245         return frame_count;
1246 }
1247 
1248 /**
1249  * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1250  * @instance:           Adapter soft state
1251  * @scp:                SCSI command
1252  * @cmd:                Command to be prepared in
1253  *
1254  * This function prepares CDB commands. These are typcially pass-through
1255  * commands to the devices.
1256  */
1257 static int
1258 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1259                    struct megasas_cmd *cmd)
1260 {
1261         u32 is_logical;
1262         u32 device_id;
1263         u16 flags = 0;
1264         struct megasas_pthru_frame *pthru;
1265 
1266         is_logical = MEGASAS_IS_LOGICAL(scp);
1267         device_id = MEGASAS_DEV_INDEX(scp);
1268         pthru = (struct megasas_pthru_frame *)cmd->frame;
1269 
1270         if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1271                 flags = MFI_FRAME_DIR_WRITE;
1272         else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1273                 flags = MFI_FRAME_DIR_READ;
1274         else if (scp->sc_data_direction == PCI_DMA_NONE)
1275                 flags = MFI_FRAME_DIR_NONE;
1276 
1277         if (instance->flag_ieee == 1) {
1278                 flags |= MFI_FRAME_IEEE;
1279         }
1280 
1281         /*
1282          * Prepare the DCDB frame
1283          */
1284         pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1285         pthru->cmd_status = 0x0;
1286         pthru->scsi_status = 0x0;
1287         pthru->target_id = device_id;
1288         pthru->lun = scp->device->lun;
1289         pthru->cdb_len = scp->cmd_len;
1290         pthru->timeout = 0;
1291         pthru->pad_0 = 0;
1292         pthru->flags = cpu_to_le16(flags);
1293         pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1294 
1295         memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1296 
1297         /*
1298          * If the command is for the tape device, set the
1299          * pthru timeout to the os layer timeout value.
1300          */
1301         if (scp->device->type == TYPE_TAPE) {
1302                 if ((scp->request->timeout / HZ) > 0xFFFF)
1303                         pthru->timeout = cpu_to_le16(0xFFFF);
1304                 else
1305                         pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
1306         }
1307 
1308         /*
1309          * Construct SGL
1310          */
1311         if (instance->flag_ieee == 1) {
1312                 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1313                 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1314                                                       &pthru->sgl);
1315         } else if (IS_DMA64) {
1316                 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1317                 pthru->sge_count = megasas_make_sgl64(instance, scp,
1318                                                       &pthru->sgl);
1319         } else
1320                 pthru->sge_count = megasas_make_sgl32(instance, scp,
1321                                                       &pthru->sgl);
1322 
1323         if (pthru->sge_count > instance->max_num_sge) {
1324                 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
1325                         pthru->sge_count);
1326                 return 0;
1327         }
1328 
1329         /*
1330          * Sense info specific
1331          */
1332         pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1333         pthru->sense_buf_phys_addr_hi =
1334                 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1335         pthru->sense_buf_phys_addr_lo =
1336                 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1337 
1338         /*
1339          * Compute the total number of frames this command consumes. FW uses
1340          * this number to pull sufficient number of frames from host memory.
1341          */
1342         cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1343                                                         PTHRU_FRAME);
1344 
1345         return cmd->frame_count;
1346 }
1347 
1348 /**
1349  * megasas_build_ldio - Prepares IOs to logical devices
1350  * @instance:           Adapter soft state
1351  * @scp:                SCSI command
1352  * @cmd:                Command to be prepared
1353  *
1354  * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1355  */
1356 static int
1357 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1358                    struct megasas_cmd *cmd)
1359 {
1360         u32 device_id;
1361         u8 sc = scp->cmnd[0];
1362         u16 flags = 0;
1363         struct megasas_io_frame *ldio;
1364 
1365         device_id = MEGASAS_DEV_INDEX(scp);
1366         ldio = (struct megasas_io_frame *)cmd->frame;
1367 
1368         if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1369                 flags = MFI_FRAME_DIR_WRITE;
1370         else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1371                 flags = MFI_FRAME_DIR_READ;
1372 
1373         if (instance->flag_ieee == 1) {
1374                 flags |= MFI_FRAME_IEEE;
1375         }
1376 
1377         /*
1378          * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1379          */
1380         ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1381         ldio->cmd_status = 0x0;
1382         ldio->scsi_status = 0x0;
1383         ldio->target_id = device_id;
1384         ldio->timeout = 0;
1385         ldio->reserved_0 = 0;
1386         ldio->pad_0 = 0;
1387         ldio->flags = cpu_to_le16(flags);
1388         ldio->start_lba_hi = 0;
1389         ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1390 
1391         /*
1392          * 6-byte READ(0x08) or WRITE(0x0A) cdb
1393          */
1394         if (scp->cmd_len == 6) {
1395                 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1396                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1397                                                  ((u32) scp->cmnd[2] << 8) |
1398                                                  (u32) scp->cmnd[3]);
1399 
1400                 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1401         }
1402 
1403         /*
1404          * 10-byte READ(0x28) or WRITE(0x2A) cdb
1405          */
1406         else if (scp->cmd_len == 10) {
1407                 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1408                                               ((u32) scp->cmnd[7] << 8));
1409                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1410                                                  ((u32) scp->cmnd[3] << 16) |
1411                                                  ((u32) scp->cmnd[4] << 8) |
1412                                                  (u32) scp->cmnd[5]);
1413         }
1414 
1415         /*
1416          * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1417          */
1418         else if (scp->cmd_len == 12) {
1419                 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1420                                               ((u32) scp->cmnd[7] << 16) |
1421                                               ((u32) scp->cmnd[8] << 8) |
1422                                               (u32) scp->cmnd[9]);
1423 
1424                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1425                                                  ((u32) scp->cmnd[3] << 16) |
1426                                                  ((u32) scp->cmnd[4] << 8) |
1427                                                  (u32) scp->cmnd[5]);
1428         }
1429 
1430         /*
1431          * 16-byte READ(0x88) or WRITE(0x8A) cdb
1432          */
1433         else if (scp->cmd_len == 16) {
1434                 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1435                                               ((u32) scp->cmnd[11] << 16) |
1436                                               ((u32) scp->cmnd[12] << 8) |
1437                                               (u32) scp->cmnd[13]);
1438 
1439                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1440                                                  ((u32) scp->cmnd[7] << 16) |
1441                                                  ((u32) scp->cmnd[8] << 8) |
1442                                                  (u32) scp->cmnd[9]);
1443 
1444                 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1445                                                  ((u32) scp->cmnd[3] << 16) |
1446                                                  ((u32) scp->cmnd[4] << 8) |
1447                                                  (u32) scp->cmnd[5]);
1448 
1449         }
1450 
1451         /*
1452          * Construct SGL
1453          */
1454         if (instance->flag_ieee) {
1455                 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1456                 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1457                                               &ldio->sgl);
1458         } else if (IS_DMA64) {
1459                 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1460                 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1461         } else
1462                 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1463 
1464         if (ldio->sge_count > instance->max_num_sge) {
1465                 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
1466                         ldio->sge_count);
1467                 return 0;
1468         }
1469 
1470         /*
1471          * Sense info specific
1472          */
1473         ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1474         ldio->sense_buf_phys_addr_hi = 0;
1475         ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1476 
1477         /*
1478          * Compute the total number of frames this command consumes. FW uses
1479          * this number to pull sufficient number of frames from host memory.
1480          */
1481         cmd->frame_count = megasas_get_frame_count(instance,
1482                         ldio->sge_count, IO_FRAME);
1483 
1484         return cmd->frame_count;
1485 }
1486 
1487 /**
1488  * megasas_cmd_type -           Checks if the cmd is for logical drive/sysPD
1489  *                              and whether it's RW or non RW
1490  * @scmd:                       SCSI command
1491  *
1492  */
1493 inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1494 {
1495         int ret;
1496 
1497         switch (cmd->cmnd[0]) {
1498         case READ_10:
1499         case WRITE_10:
1500         case READ_12:
1501         case WRITE_12:
1502         case READ_6:
1503         case WRITE_6:
1504         case READ_16:
1505         case WRITE_16:
1506                 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1507                         READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1508                 break;
1509         default:
1510                 ret = (MEGASAS_IS_LOGICAL(cmd)) ?
1511                         NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1512         }
1513         return ret;
1514 }
1515 
1516  /**
1517  * megasas_dump_pending_frames -        Dumps the frame address of all pending cmds
1518  *                                      in FW
1519  * @instance:                           Adapter soft state
1520  */
1521 static inline void
1522 megasas_dump_pending_frames(struct megasas_instance *instance)
1523 {
1524         struct megasas_cmd *cmd;
1525         int i,n;
1526         union megasas_sgl *mfi_sgl;
1527         struct megasas_io_frame *ldio;
1528         struct megasas_pthru_frame *pthru;
1529         u32 sgcount;
1530         u32 max_cmd = instance->max_fw_cmds;
1531 
1532         dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1533         dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1534         if (IS_DMA64)
1535                 dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1536         else
1537                 dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1538 
1539         dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1540         for (i = 0; i < max_cmd; i++) {
1541                 cmd = instance->cmd_list[i];
1542                 if (!cmd->scmd)
1543                         continue;
1544                 dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1545                 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1546                         ldio = (struct megasas_io_frame *)cmd->frame;
1547                         mfi_sgl = &ldio->sgl;
1548                         sgcount = ldio->sge_count;
1549                         dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1550                         " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1551                         instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1552                         le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1553                         le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1554                 } else {
1555                         pthru = (struct megasas_pthru_frame *) cmd->frame;
1556                         mfi_sgl = &pthru->sgl;
1557                         sgcount = pthru->sge_count;
1558                         dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1559                         "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1560                         instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1561                         pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1562                         le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1563                 }
1564                 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1565                         for (n = 0; n < sgcount; n++) {
1566                                 if (IS_DMA64)
1567                                         dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1568                                                 le32_to_cpu(mfi_sgl->sge64[n].length),
1569                                                 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1570                                 else
1571                                         dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1572                                                 le32_to_cpu(mfi_sgl->sge32[n].length),
1573                                                 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1574                         }
1575                 }
1576         } /*for max_cmd*/
1577         dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1578         for (i = 0; i < max_cmd; i++) {
1579 
1580                 cmd = instance->cmd_list[i];
1581 
1582                 if (cmd->sync_cmd == 1)
1583                         dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1584         }
1585         dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
1586 }
1587 
1588 u32
1589 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1590                             struct scsi_cmnd *scmd)
1591 {
1592         struct megasas_cmd *cmd;
1593         u32 frame_count;
1594 
1595         cmd = megasas_get_cmd(instance);
1596         if (!cmd)
1597                 return SCSI_MLQUEUE_HOST_BUSY;
1598 
1599         /*
1600          * Logical drive command
1601          */
1602         if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1603                 frame_count = megasas_build_ldio(instance, scmd, cmd);
1604         else
1605                 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1606 
1607         if (!frame_count)
1608                 goto out_return_cmd;
1609 
1610         cmd->scmd = scmd;
1611         scmd->SCp.ptr = (char *)cmd;
1612 
1613         /*
1614          * Issue the command to the FW
1615          */
1616         atomic_inc(&instance->fw_outstanding);
1617 
1618         instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1619                                 cmd->frame_count-1, instance->reg_set);
1620 
1621         return 0;
1622 out_return_cmd:
1623         megasas_return_cmd(instance, cmd);
1624         return 1;
1625 }
1626 
1627 
1628 /**
1629  * megasas_queue_command -      Queue entry point
1630  * @scmd:                       SCSI command to be queued
1631  * @done:                       Callback entry point
1632  */
1633 static int
1634 megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1635 {
1636         struct megasas_instance *instance;
1637         unsigned long flags;
1638 
1639         instance = (struct megasas_instance *)
1640             scmd->device->host->hostdata;
1641 
1642         if (instance->unload == 1) {
1643                 scmd->result = DID_NO_CONNECT << 16;
1644                 scmd->scsi_done(scmd);
1645                 return 0;
1646         }
1647 
1648         if (instance->issuepend_done == 0)
1649                 return SCSI_MLQUEUE_HOST_BUSY;
1650 
1651         spin_lock_irqsave(&instance->hba_lock, flags);
1652 
1653         /* Check for an mpio path and adjust behavior */
1654         if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
1655                 if (megasas_check_mpio_paths(instance, scmd) ==
1656                     (DID_RESET << 16)) {
1657                         spin_unlock_irqrestore(&instance->hba_lock, flags);
1658                         return SCSI_MLQUEUE_HOST_BUSY;
1659                 } else {
1660                         spin_unlock_irqrestore(&instance->hba_lock, flags);
1661                         scmd->result = DID_NO_CONNECT << 16;
1662                         scmd->scsi_done(scmd);
1663                         return 0;
1664                 }
1665         }
1666 
1667         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1668                 spin_unlock_irqrestore(&instance->hba_lock, flags);
1669                 scmd->result = DID_NO_CONNECT << 16;
1670                 scmd->scsi_done(scmd);
1671                 return 0;
1672         }
1673 
1674         if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
1675                 spin_unlock_irqrestore(&instance->hba_lock, flags);
1676                 return SCSI_MLQUEUE_HOST_BUSY;
1677         }
1678 
1679         spin_unlock_irqrestore(&instance->hba_lock, flags);
1680 
1681         scmd->result = 0;
1682 
1683         if (MEGASAS_IS_LOGICAL(scmd) &&
1684             (scmd->device->id >= instance->fw_supported_vd_count ||
1685                 scmd->device->lun)) {
1686                 scmd->result = DID_BAD_TARGET << 16;
1687                 goto out_done;
1688         }
1689 
1690         switch (scmd->cmnd[0]) {
1691         case SYNCHRONIZE_CACHE:
1692                 /*
1693                  * FW takes care of flush cache on its own
1694                  * No need to send it down
1695                  */
1696                 scmd->result = DID_OK << 16;
1697                 goto out_done;
1698         default:
1699                 break;
1700         }
1701 
1702         if (instance->instancet->build_and_issue_cmd(instance, scmd)) {
1703                 dev_err(&instance->pdev->dev, "Err returned from build_and_issue_cmd\n");
1704                 return SCSI_MLQUEUE_HOST_BUSY;
1705         }
1706 
1707         return 0;
1708 
1709  out_done:
1710         scmd->scsi_done(scmd);
1711         return 0;
1712 }
1713 
1714 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1715 {
1716         int i;
1717 
1718         for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1719 
1720                 if ((megasas_mgmt_info.instance[i]) &&
1721                     (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1722                         return megasas_mgmt_info.instance[i];
1723         }
1724 
1725         return NULL;
1726 }
1727 
1728 /*
1729 * megasas_set_dma_alignment - Set DMA alignment for PI enabled VD
1730 *
1731 * @sdev: OS provided scsi device
1732 *
1733 * Returns void
1734 */
1735 static void megasas_set_dma_alignment(struct scsi_device *sdev)
1736 {
1737         u32 device_id, ld;
1738         struct megasas_instance *instance;
1739         struct fusion_context *fusion;
1740         struct MR_LD_RAID *raid;
1741         struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1742 
1743         instance = megasas_lookup_instance(sdev->host->host_no);
1744         fusion = instance->ctrl_context;
1745 
1746         if (!fusion)
1747                 return;
1748 
1749         if (sdev->channel >= MEGASAS_MAX_PD_CHANNELS) {
1750                 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1751                                         + sdev->id;
1752                 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1753                 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1754                 raid = MR_LdRaidGet(ld, local_map_ptr);
1755 
1756                 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
1757                         blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1758         }
1759 }
1760 
1761 static int megasas_slave_configure(struct scsi_device *sdev)
1762 {
1763         u16 pd_index = 0;
1764         struct megasas_instance *instance;
1765 
1766         instance = megasas_lookup_instance(sdev->host->host_no);
1767         if (instance->allow_fw_scan) {
1768                 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS &&
1769                         sdev->type == TYPE_DISK) {
1770                         pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1771                                 sdev->id;
1772                         if (instance->pd_list[pd_index].driveState !=
1773                                 MR_PD_STATE_SYSTEM)
1774                                 return -ENXIO;
1775                 }
1776         }
1777         megasas_set_dma_alignment(sdev);
1778         /*
1779          * The RAID firmware may require extended timeouts.
1780          */
1781         blk_queue_rq_timeout(sdev->request_queue,
1782                 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ);
1783 
1784         return 0;
1785 }
1786 
1787 static int megasas_slave_alloc(struct scsi_device *sdev)
1788 {
1789         u16 pd_index = 0;
1790         struct megasas_instance *instance ;
1791 
1792         instance = megasas_lookup_instance(sdev->host->host_no);
1793         if (sdev->channel < MEGASAS_MAX_PD_CHANNELS) {
1794                 /*
1795                  * Open the OS scan to the SYSTEM PD
1796                  */
1797                 pd_index =
1798                         (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1799                         sdev->id;
1800                 if ((instance->allow_fw_scan || instance->pd_list[pd_index].driveState ==
1801                         MR_PD_STATE_SYSTEM)) {
1802                         return 0;
1803                 }
1804                 return -ENXIO;
1805         }
1806         return 0;
1807 }
1808 
1809 /*
1810 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
1811 *                                       kill adapter
1812 * @instance:                            Adapter soft state
1813 *
1814 */
1815 static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
1816 {
1817         int i;
1818         struct megasas_cmd *cmd_mfi;
1819         struct megasas_cmd_fusion *cmd_fusion;
1820         struct fusion_context *fusion = instance->ctrl_context;
1821 
1822         /* Find all outstanding ioctls */
1823         if (fusion) {
1824                 for (i = 0; i < instance->max_fw_cmds; i++) {
1825                         cmd_fusion = fusion->cmd_list[i];
1826                         if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
1827                                 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1828                                 if (cmd_mfi->sync_cmd &&
1829                                         cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)
1830                                         megasas_complete_cmd(instance,
1831                                                              cmd_mfi, DID_OK);
1832                         }
1833                 }
1834         } else {
1835                 for (i = 0; i < instance->max_fw_cmds; i++) {
1836                         cmd_mfi = instance->cmd_list[i];
1837                         if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
1838                                 MFI_CMD_ABORT)
1839                                 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1840                 }
1841         }
1842 }
1843 
1844 
1845 void megaraid_sas_kill_hba(struct megasas_instance *instance)
1846 {
1847         /* Set critical error to block I/O & ioctls in case caller didn't */
1848         instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
1849         /* Wait 1 second to ensure IO or ioctls in build have posted */
1850         msleep(1000);
1851         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
1852                 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
1853                 (instance->ctrl_context)) {
1854                 writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
1855                 /* Flush */
1856                 readl(&instance->reg_set->doorbell);
1857                 if (instance->mpio && instance->requestorId)
1858                         memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
1859         } else {
1860                 writel(MFI_STOP_ADP,
1861                         &instance->reg_set->inbound_doorbell);
1862         }
1863         /* Complete outstanding ioctls when adapter is killed */
1864         megasas_complete_outstanding_ioctls(instance);
1865 }
1866 
1867  /**
1868   * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
1869   *                                     restored to max value
1870   * @instance:                  Adapter soft state
1871   *
1872   */
1873 void
1874 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
1875 {
1876         unsigned long flags;
1877 
1878         if (instance->flag & MEGASAS_FW_BUSY
1879             && time_after(jiffies, instance->last_time + 5 * HZ)
1880             && atomic_read(&instance->fw_outstanding) <
1881             instance->throttlequeuedepth + 1) {
1882 
1883                 spin_lock_irqsave(instance->host->host_lock, flags);
1884                 instance->flag &= ~MEGASAS_FW_BUSY;
1885 
1886                 instance->host->can_queue = instance->max_scsi_cmds;
1887                 spin_unlock_irqrestore(instance->host->host_lock, flags);
1888         }
1889 }
1890 
1891 /**
1892  * megasas_complete_cmd_dpc      -      Returns FW's controller structure
1893  * @instance_addr:                      Address of adapter soft state
1894  *
1895  * Tasklet to complete cmds
1896  */
1897 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
1898 {
1899         u32 producer;
1900         u32 consumer;
1901         u32 context;
1902         struct megasas_cmd *cmd;
1903         struct megasas_instance *instance =
1904                                 (struct megasas_instance *)instance_addr;
1905         unsigned long flags;
1906 
1907         /* If we have already declared adapter dead, donot complete cmds */
1908         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1909                 return;
1910 
1911         spin_lock_irqsave(&instance->completion_lock, flags);
1912 
1913         producer = le32_to_cpu(*instance->producer);
1914         consumer = le32_to_cpu(*instance->consumer);
1915 
1916         while (consumer != producer) {
1917                 context = le32_to_cpu(instance->reply_queue[consumer]);
1918                 if (context >= instance->max_fw_cmds) {
1919                         dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
1920                                 context);
1921                         BUG();
1922                 }
1923 
1924                 cmd = instance->cmd_list[context];
1925 
1926                 megasas_complete_cmd(instance, cmd, DID_OK);
1927 
1928                 consumer++;
1929                 if (consumer == (instance->max_fw_cmds + 1)) {
1930                         consumer = 0;
1931                 }
1932         }
1933 
1934         *instance->consumer = cpu_to_le32(producer);
1935 
1936         spin_unlock_irqrestore(&instance->completion_lock, flags);
1937 
1938         /*
1939          * Check if we can restore can_queue
1940          */
1941         megasas_check_and_restore_queue_depth(instance);
1942 }
1943 
1944 /**
1945  * megasas_start_timer - Initializes a timer object
1946  * @instance:           Adapter soft state
1947  * @timer:              timer object to be initialized
1948  * @fn:                 timer function
1949  * @interval:           time interval between timer function call
1950  *
1951  */
1952 void megasas_start_timer(struct megasas_instance *instance,
1953                         struct timer_list *timer,
1954                         void *fn, unsigned long interval)
1955 {
1956         init_timer(timer);
1957         timer->expires = jiffies + interval;
1958         timer->data = (unsigned long)instance;
1959         timer->function = fn;
1960         add_timer(timer);
1961 }
1962 
1963 static void
1964 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
1965 
1966 static void
1967 process_fw_state_change_wq(struct work_struct *work);
1968 
1969 void megasas_do_ocr(struct megasas_instance *instance)
1970 {
1971         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
1972         (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
1973         (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
1974                 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
1975         }
1976         instance->instancet->disable_intr(instance);
1977         instance->adprecovery   = MEGASAS_ADPRESET_SM_INFAULT;
1978         instance->issuepend_done = 0;
1979 
1980         atomic_set(&instance->fw_outstanding, 0);
1981         megasas_internal_reset_defer_cmds(instance);
1982         process_fw_state_change_wq(&instance->work_init);
1983 }
1984 
1985 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
1986                                             int initial)
1987 {
1988         struct megasas_cmd *cmd;
1989         struct megasas_dcmd_frame *dcmd;
1990         struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
1991         dma_addr_t new_affiliation_111_h;
1992         int ld, retval = 0;
1993         u8 thisVf;
1994 
1995         cmd = megasas_get_cmd(instance);
1996 
1997         if (!cmd) {
1998                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
1999                        "Failed to get cmd for scsi%d\n",
2000                         instance->host->host_no);
2001                 return -ENOMEM;
2002         }
2003 
2004         dcmd = &cmd->frame->dcmd;
2005 
2006         if (!instance->vf_affiliation_111) {
2007                 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2008                        "affiliation for scsi%d\n", instance->host->host_no);
2009                 megasas_return_cmd(instance, cmd);
2010                 return -ENOMEM;
2011         }
2012 
2013         if (initial)
2014                         memset(instance->vf_affiliation_111, 0,
2015                                sizeof(struct MR_LD_VF_AFFILIATION_111));
2016         else {
2017                 new_affiliation_111 =
2018                         pci_alloc_consistent(instance->pdev,
2019                                              sizeof(struct MR_LD_VF_AFFILIATION_111),
2020                                              &new_affiliation_111_h);
2021                 if (!new_affiliation_111) {
2022                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2023                                "memory for new affiliation for scsi%d\n",
2024                                instance->host->host_no);
2025                         megasas_return_cmd(instance, cmd);
2026                         return -ENOMEM;
2027                 }
2028                 memset(new_affiliation_111, 0,
2029                        sizeof(struct MR_LD_VF_AFFILIATION_111));
2030         }
2031 
2032         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2033 
2034         dcmd->cmd = MFI_CMD_DCMD;
2035         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2036         dcmd->sge_count = 1;
2037         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2038         dcmd->timeout = 0;
2039         dcmd->pad_0 = 0;
2040         dcmd->data_xfer_len =
2041                 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2042         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
2043 
2044         if (initial)
2045                 dcmd->sgl.sge32[0].phys_addr =
2046                         cpu_to_le32(instance->vf_affiliation_111_h);
2047         else
2048                 dcmd->sgl.sge32[0].phys_addr =
2049                         cpu_to_le32(new_affiliation_111_h);
2050 
2051         dcmd->sgl.sge32[0].length = cpu_to_le32(
2052                 sizeof(struct MR_LD_VF_AFFILIATION_111));
2053 
2054         dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2055                "scsi%d\n", instance->host->host_no);
2056 
2057         megasas_issue_blocked_cmd(instance, cmd, 0);
2058 
2059         if (dcmd->cmd_status) {
2060                 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2061                        " failed with status 0x%x for scsi%d\n",
2062                        dcmd->cmd_status, instance->host->host_no);
2063                 retval = 1; /* Do a scan if we couldn't get affiliation */
2064                 goto out;
2065         }
2066 
2067         if (!initial) {
2068                 thisVf = new_affiliation_111->thisVf;
2069                 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2070                         if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2071                             new_affiliation_111->map[ld].policy[thisVf]) {
2072                                 dev_warn(&instance->pdev->dev, "SR-IOV: "
2073                                        "Got new LD/VF affiliation for scsi%d\n",
2074                                        instance->host->host_no);
2075                                 memcpy(instance->vf_affiliation_111,
2076                                        new_affiliation_111,
2077                                        sizeof(struct MR_LD_VF_AFFILIATION_111));
2078                                 retval = 1;
2079                                 goto out;
2080                         }
2081         }
2082 out:
2083         if (new_affiliation_111) {
2084                 pci_free_consistent(instance->pdev,
2085                                     sizeof(struct MR_LD_VF_AFFILIATION_111),
2086                                     new_affiliation_111,
2087                                     new_affiliation_111_h);
2088         }
2089 
2090         megasas_return_cmd(instance, cmd);
2091 
2092         return retval;
2093 }
2094 
2095 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2096                                             int initial)
2097 {
2098         struct megasas_cmd *cmd;
2099         struct megasas_dcmd_frame *dcmd;
2100         struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2101         struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2102         dma_addr_t new_affiliation_h;
2103         int i, j, retval = 0, found = 0, doscan = 0;
2104         u8 thisVf;
2105 
2106         cmd = megasas_get_cmd(instance);
2107 
2108         if (!cmd) {
2109                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2110                        "Failed to get cmd for scsi%d\n",
2111                        instance->host->host_no);
2112                 return -ENOMEM;
2113         }
2114 
2115         dcmd = &cmd->frame->dcmd;
2116 
2117         if (!instance->vf_affiliation) {
2118                 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2119                        "affiliation for scsi%d\n", instance->host->host_no);
2120                 megasas_return_cmd(instance, cmd);
2121                 return -ENOMEM;
2122         }
2123 
2124         if (initial)
2125                 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2126                        sizeof(struct MR_LD_VF_AFFILIATION));
2127         else {
2128                 new_affiliation =
2129                         pci_alloc_consistent(instance->pdev,
2130                                              (MAX_LOGICAL_DRIVES + 1) *
2131                                              sizeof(struct MR_LD_VF_AFFILIATION),
2132                                              &new_affiliation_h);
2133                 if (!new_affiliation) {
2134                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2135                                "memory for new affiliation for scsi%d\n",
2136                                instance->host->host_no);
2137                         megasas_return_cmd(instance, cmd);
2138                         return -ENOMEM;
2139                 }
2140                 memset(new_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2141                        sizeof(struct MR_LD_VF_AFFILIATION));
2142         }
2143 
2144         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2145 
2146         dcmd->cmd = MFI_CMD_DCMD;
2147         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2148         dcmd->sge_count = 1;
2149         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2150         dcmd->timeout = 0;
2151         dcmd->pad_0 = 0;
2152         dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2153                 sizeof(struct MR_LD_VF_AFFILIATION));
2154         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2155 
2156         if (initial)
2157                 dcmd->sgl.sge32[0].phys_addr =
2158                         cpu_to_le32(instance->vf_affiliation_h);
2159         else
2160                 dcmd->sgl.sge32[0].phys_addr =
2161                         cpu_to_le32(new_affiliation_h);
2162 
2163         dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2164                 sizeof(struct MR_LD_VF_AFFILIATION));
2165 
2166         dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2167                "scsi%d\n", instance->host->host_no);
2168 
2169         megasas_issue_blocked_cmd(instance, cmd, 0);
2170 
2171         if (dcmd->cmd_status) {
2172                 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2173                        " failed with status 0x%x for scsi%d\n",
2174                        dcmd->cmd_status, instance->host->host_no);
2175                 retval = 1; /* Do a scan if we couldn't get affiliation */
2176                 goto out;
2177         }
2178 
2179         if (!initial) {
2180                 if (!new_affiliation->ldCount) {
2181                         dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2182                                "affiliation for passive path for scsi%d\n",
2183                                instance->host->host_no);
2184                         retval = 1;
2185                         goto out;
2186                 }
2187                 newmap = new_affiliation->map;
2188                 savedmap = instance->vf_affiliation->map;
2189                 thisVf = new_affiliation->thisVf;
2190                 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2191                         found = 0;
2192                         for (j = 0; j < instance->vf_affiliation->ldCount;
2193                              j++) {
2194                                 if (newmap->ref.targetId ==
2195                                     savedmap->ref.targetId) {
2196                                         found = 1;
2197                                         if (newmap->policy[thisVf] !=
2198                                             savedmap->policy[thisVf]) {
2199                                                 doscan = 1;
2200                                                 goto out;
2201                                         }
2202                                 }
2203                                 savedmap = (struct MR_LD_VF_MAP *)
2204                                         ((unsigned char *)savedmap +
2205                                          savedmap->size);
2206                         }
2207                         if (!found && newmap->policy[thisVf] !=
2208                             MR_LD_ACCESS_HIDDEN) {
2209                                 doscan = 1;
2210                                 goto out;
2211                         }
2212                         newmap = (struct MR_LD_VF_MAP *)
2213                                 ((unsigned char *)newmap + newmap->size);
2214                 }
2215 
2216                 newmap = new_affiliation->map;
2217                 savedmap = instance->vf_affiliation->map;
2218 
2219                 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2220                         found = 0;
2221                         for (j = 0 ; j < new_affiliation->ldCount; j++) {
2222                                 if (savedmap->ref.targetId ==
2223                                     newmap->ref.targetId) {
2224                                         found = 1;
2225                                         if (savedmap->policy[thisVf] !=
2226                                             newmap->policy[thisVf]) {
2227                                                 doscan = 1;
2228                                                 goto out;
2229                                         }
2230                                 }
2231                                 newmap = (struct MR_LD_VF_MAP *)
2232                                         ((unsigned char *)newmap +
2233                                          newmap->size);
2234                         }
2235                         if (!found && savedmap->policy[thisVf] !=
2236                             MR_LD_ACCESS_HIDDEN) {
2237                                 doscan = 1;
2238                                 goto out;
2239                         }
2240                         savedmap = (struct MR_LD_VF_MAP *)
2241                                 ((unsigned char *)savedmap +
2242                                  savedmap->size);
2243                 }
2244         }
2245 out:
2246         if (doscan) {
2247                 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2248                        "affiliation for scsi%d\n", instance->host->host_no);
2249                 memcpy(instance->vf_affiliation, new_affiliation,
2250                        new_affiliation->size);
2251                 retval = 1;
2252         }
2253 
2254         if (new_affiliation)
2255                 pci_free_consistent(instance->pdev,
2256                                     (MAX_LOGICAL_DRIVES + 1) *
2257                                     sizeof(struct MR_LD_VF_AFFILIATION),
2258                                     new_affiliation, new_affiliation_h);
2259         megasas_return_cmd(instance, cmd);
2260 
2261         return retval;
2262 }
2263 
2264 /* This function will get the current SR-IOV LD/VF affiliation */
2265 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2266         int initial)
2267 {
2268         int retval;
2269 
2270         if (instance->PlasmaFW111)
2271                 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2272         else
2273                 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2274         return retval;
2275 }
2276 
2277 /* This function will tell FW to start the SR-IOV heartbeat */
2278 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2279                                          int initial)
2280 {
2281         struct megasas_cmd *cmd;
2282         struct megasas_dcmd_frame *dcmd;
2283         int retval = 0;
2284 
2285         cmd = megasas_get_cmd(instance);
2286 
2287         if (!cmd) {
2288                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2289                        "Failed to get cmd for scsi%d\n",
2290                        instance->host->host_no);
2291                 return -ENOMEM;
2292         }
2293 
2294         dcmd = &cmd->frame->dcmd;
2295 
2296         if (initial) {
2297                 instance->hb_host_mem =
2298                         pci_zalloc_consistent(instance->pdev,
2299                                               sizeof(struct MR_CTRL_HB_HOST_MEM),
2300                                               &instance->hb_host_mem_h);
2301                 if (!instance->hb_host_mem) {
2302                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2303                                " memory for heartbeat host memory for scsi%d\n",
2304                                instance->host->host_no);
2305                         retval = -ENOMEM;
2306                         goto out;
2307                 }
2308         }
2309 
2310         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2311 
2312         dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2313         dcmd->cmd = MFI_CMD_DCMD;
2314         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2315         dcmd->sge_count = 1;
2316         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2317         dcmd->timeout = 0;
2318         dcmd->pad_0 = 0;
2319         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2320         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2321         dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->hb_host_mem_h);
2322         dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2323 
2324         dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2325                instance->host->host_no);
2326 
2327         if (instance->ctrl_context && !instance->mask_interrupts)
2328                 retval = megasas_issue_blocked_cmd(instance, cmd,
2329                         MEGASAS_ROUTINE_WAIT_TIME_VF);
2330         else
2331                 retval = megasas_issue_polled(instance, cmd);
2332 
2333         if (retval) {
2334                 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2335                         "_MEM_ALLOC DCMD %s for scsi%d\n",
2336                         (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2337                         "timed out" : "failed", instance->host->host_no);
2338                 retval = 1;
2339         }
2340 
2341 out:
2342         megasas_return_cmd(instance, cmd);
2343 
2344         return retval;
2345 }
2346 
2347 /* Handler for SR-IOV heartbeat */
2348 void megasas_sriov_heartbeat_handler(unsigned long instance_addr)
2349 {
2350         struct megasas_instance *instance =
2351                 (struct megasas_instance *)instance_addr;
2352 
2353         if (instance->hb_host_mem->HB.fwCounter !=
2354             instance->hb_host_mem->HB.driverCounter) {
2355                 instance->hb_host_mem->HB.driverCounter =
2356                         instance->hb_host_mem->HB.fwCounter;
2357                 mod_timer(&instance->sriov_heartbeat_timer,
2358                           jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2359         } else {
2360                 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2361                        "completed for scsi%d\n", instance->host->host_no);
2362                 schedule_work(&instance->work_init);
2363         }
2364 }
2365 
2366 /**
2367  * megasas_wait_for_outstanding -       Wait for all outstanding cmds
2368  * @instance:                           Adapter soft state
2369  *
2370  * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2371  * complete all its outstanding commands. Returns error if one or more IOs
2372  * are pending after this time period. It also marks the controller dead.
2373  */
2374 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2375 {
2376         int i;
2377         u32 reset_index;
2378         u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2379         u8 adprecovery;
2380         unsigned long flags;
2381         struct list_head clist_local;
2382         struct megasas_cmd *reset_cmd;
2383         u32 fw_state;
2384         u8 kill_adapter_flag;
2385 
2386         spin_lock_irqsave(&instance->hba_lock, flags);
2387         adprecovery = instance->adprecovery;
2388         spin_unlock_irqrestore(&instance->hba_lock, flags);
2389 
2390         if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2391 
2392                 INIT_LIST_HEAD(&clist_local);
2393                 spin_lock_irqsave(&instance->hba_lock, flags);
2394                 list_splice_init(&instance->internal_reset_pending_q,
2395                                 &clist_local);
2396                 spin_unlock_irqrestore(&instance->hba_lock, flags);
2397 
2398                 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2399                 for (i = 0; i < wait_time; i++) {
2400                         msleep(1000);
2401                         spin_lock_irqsave(&instance->hba_lock, flags);
2402                         adprecovery = instance->adprecovery;
2403                         spin_unlock_irqrestore(&instance->hba_lock, flags);
2404                         if (adprecovery == MEGASAS_HBA_OPERATIONAL)
2405                                 break;
2406                 }
2407 
2408                 if (adprecovery != MEGASAS_HBA_OPERATIONAL) {
2409                         dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2410                         spin_lock_irqsave(&instance->hba_lock, flags);
2411                         instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2412                         spin_unlock_irqrestore(&instance->hba_lock, flags);
2413                         return FAILED;
2414                 }
2415 
2416                 reset_index = 0;
2417                 while (!list_empty(&clist_local)) {
2418                         reset_cmd = list_entry((&clist_local)->next,
2419                                                 struct megasas_cmd, list);
2420                         list_del_init(&reset_cmd->list);
2421                         if (reset_cmd->scmd) {
2422                                 reset_cmd->scmd->result = DID_RESET << 16;
2423                                 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2424                                         reset_index, reset_cmd,
2425                                         reset_cmd->scmd->cmnd[0]);
2426 
2427                                 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2428                                 megasas_return_cmd(instance, reset_cmd);
2429                         } else if (reset_cmd->sync_cmd) {
2430                                 dev_notice(&instance->pdev->dev, "%p synch cmds"
2431                                                 "reset queue\n",
2432                                                 reset_cmd);
2433 
2434                                 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
2435                                 instance->instancet->fire_cmd(instance,
2436                                                 reset_cmd->frame_phys_addr,
2437                                                 0, instance->reg_set);
2438                         } else {
2439                                 dev_notice(&instance->pdev->dev, "%p unexpected"
2440                                         "cmds lst\n",
2441                                         reset_cmd);
2442                         }
2443                         reset_index++;
2444                 }
2445 
2446                 return SUCCESS;
2447         }
2448 
2449         for (i = 0; i < resetwaittime; i++) {
2450                 int outstanding = atomic_read(&instance->fw_outstanding);
2451 
2452                 if (!outstanding)
2453                         break;
2454 
2455                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2456                         dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2457                                "commands to complete\n",i,outstanding);
2458                         /*
2459                          * Call cmd completion routine. Cmd to be
2460                          * be completed directly without depending on isr.
2461                          */
2462                         megasas_complete_cmd_dpc((unsigned long)instance);
2463                 }
2464 
2465                 msleep(1000);
2466         }
2467 
2468         i = 0;
2469         kill_adapter_flag = 0;
2470         do {
2471                 fw_state = instance->instancet->read_fw_status_reg(
2472                                         instance->reg_set) & MFI_STATE_MASK;
2473                 if ((fw_state == MFI_STATE_FAULT) &&
2474                         (instance->disableOnlineCtrlReset == 0)) {
2475                         if (i == 3) {
2476                                 kill_adapter_flag = 2;
2477                                 break;
2478                         }
2479                         megasas_do_ocr(instance);
2480                         kill_adapter_flag = 1;
2481 
2482                         /* wait for 1 secs to let FW finish the pending cmds */
2483                         msleep(1000);
2484                 }
2485                 i++;
2486         } while (i <= 3);
2487 
2488         if (atomic_read(&instance->fw_outstanding) && !kill_adapter_flag) {
2489                 if (instance->disableOnlineCtrlReset == 0) {
2490                         megasas_do_ocr(instance);
2491 
2492                         /* wait for 5 secs to let FW finish the pending cmds */
2493                         for (i = 0; i < wait_time; i++) {
2494                                 int outstanding =
2495                                         atomic_read(&instance->fw_outstanding);
2496                                 if (!outstanding)
2497                                         return SUCCESS;
2498                                 msleep(1000);
2499                         }
2500                 }
2501         }
2502 
2503         if (atomic_read(&instance->fw_outstanding) ||
2504                                         (kill_adapter_flag == 2)) {
2505                 dev_notice(&instance->pdev->dev, "pending cmds after reset\n");
2506                 /*
2507                  * Send signal to FW to stop processing any pending cmds.
2508                  * The controller will be taken offline by the OS now.
2509                  */
2510                 if ((instance->pdev->device ==
2511                         PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2512                         (instance->pdev->device ==
2513                         PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
2514                         writel(MFI_STOP_ADP,
2515                                 &instance->reg_set->doorbell);
2516                 } else {
2517                         writel(MFI_STOP_ADP,
2518                                 &instance->reg_set->inbound_doorbell);
2519                 }
2520                 megasas_dump_pending_frames(instance);
2521                 spin_lock_irqsave(&instance->hba_lock, flags);
2522                 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2523                 spin_unlock_irqrestore(&instance->hba_lock, flags);
2524                 return FAILED;
2525         }
2526 
2527         dev_notice(&instance->pdev->dev, "no pending cmds after reset\n");
2528 
2529         return SUCCESS;
2530 }
2531 
2532 /**
2533  * megasas_generic_reset -      Generic reset routine
2534  * @scmd:                       Mid-layer SCSI command
2535  *
2536  * This routine implements a generic reset handler for device, bus and host
2537  * reset requests. Device, bus and host specific reset handlers can use this
2538  * function after they do their specific tasks.
2539  */
2540 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2541 {
2542         int ret_val;
2543         struct megasas_instance *instance;
2544 
2545         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2546 
2547         scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2548                  scmd->cmnd[0], scmd->retries);
2549 
2550         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2551                 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2552                 return FAILED;
2553         }
2554 
2555         ret_val = megasas_wait_for_outstanding(instance);
2556         if (ret_val == SUCCESS)
2557                 dev_notice(&instance->pdev->dev, "reset successful\n");
2558         else
2559                 dev_err(&instance->pdev->dev, "failed to do reset\n");
2560 
2561         return ret_val;
2562 }
2563 
2564 /**
2565  * megasas_reset_timer - quiesce the adapter if required
2566  * @scmd:               scsi cmnd
2567  *
2568  * Sets the FW busy flag and reduces the host->can_queue if the
2569  * cmd has not been completed within the timeout period.
2570  */
2571 static enum
2572 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2573 {
2574         struct megasas_instance *instance;
2575         unsigned long flags;
2576 
2577         if (time_after(jiffies, scmd->jiffies_at_alloc +
2578                                 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) {
2579                 return BLK_EH_NOT_HANDLED;
2580         }
2581 
2582         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2583         if (!(instance->flag & MEGASAS_FW_BUSY)) {
2584                 /* FW is busy, throttle IO */
2585                 spin_lock_irqsave(instance->host->host_lock, flags);
2586 
2587                 instance->host->can_queue = instance->throttlequeuedepth;
2588                 instance->last_time = jiffies;
2589                 instance->flag |= MEGASAS_FW_BUSY;
2590 
2591                 spin_unlock_irqrestore(instance->host->host_lock, flags);
2592         }
2593         return BLK_EH_RESET_TIMER;
2594 }
2595 
2596 /**
2597  * megasas_reset_device -       Device reset handler entry point
2598  */
2599 static int megasas_reset_device(struct scsi_cmnd *scmd)
2600 {
2601         /*
2602          * First wait for all commands to complete
2603          */
2604         return megasas_generic_reset(scmd);
2605 }
2606 
2607 /**
2608  * megasas_reset_bus_host -     Bus & host reset handler entry point
2609  */
2610 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2611 {
2612         int ret;
2613         struct megasas_instance *instance;
2614 
2615         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2616 
2617         /*
2618          * First wait for all commands to complete
2619          */
2620         if (instance->ctrl_context)
2621                 ret = megasas_reset_fusion(scmd->device->host, 1);
2622         else
2623                 ret = megasas_generic_reset(scmd);
2624 
2625         return ret;
2626 }
2627 
2628 /**
2629  * megasas_bios_param - Returns disk geometry for a disk
2630  * @sdev:               device handle
2631  * @bdev:               block device
2632  * @capacity:           drive capacity
2633  * @geom:               geometry parameters
2634  */
2635 static int
2636 megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev,
2637                  sector_t capacity, int geom[])
2638 {
2639         int heads;
2640         int sectors;
2641         sector_t cylinders;
2642         unsigned long tmp;
2643 
2644         /* Default heads (64) & sectors (32) */
2645         heads = 64;
2646         sectors = 32;
2647 
2648         tmp = heads * sectors;
2649         cylinders = capacity;
2650 
2651         sector_div(cylinders, tmp);
2652 
2653         /*
2654          * Handle extended translation size for logical drives > 1Gb
2655          */
2656 
2657         if (capacity >= 0x200000) {
2658                 heads = 255;
2659                 sectors = 63;
2660                 tmp = heads*sectors;
2661                 cylinders = capacity;
2662                 sector_div(cylinders, tmp);
2663         }
2664 
2665         geom[0] = heads;
2666         geom[1] = sectors;
2667         geom[2] = cylinders;
2668 
2669         return 0;
2670 }
2671 
2672 static void megasas_aen_polling(struct work_struct *work);
2673 
2674 /**
2675  * megasas_service_aen -        Processes an event notification
2676  * @instance:                   Adapter soft state
2677  * @cmd:                        AEN command completed by the ISR
2678  *
2679  * For AEN, driver sends a command down to FW that is held by the FW till an
2680  * event occurs. When an event of interest occurs, FW completes the command
2681  * that it was previously holding.
2682  *
2683  * This routines sends SIGIO signal to processes that have registered with the
2684  * driver for AEN.
2685  */
2686 static void
2687 megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd)
2688 {
2689         unsigned long flags;
2690 
2691         /*
2692          * Don't signal app if it is just an aborted previously registered aen
2693          */
2694         if ((!cmd->abort_aen) && (instance->unload == 0)) {
2695                 spin_lock_irqsave(&poll_aen_lock, flags);
2696                 megasas_poll_wait_aen = 1;
2697                 spin_unlock_irqrestore(&poll_aen_lock, flags);
2698                 wake_up(&megasas_poll_wait);
2699                 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN);
2700         }
2701         else
2702                 cmd->abort_aen = 0;
2703 
2704         instance->aen_cmd = NULL;
2705 
2706         megasas_return_cmd(instance, cmd);
2707 
2708         if ((instance->unload == 0) &&
2709                 ((instance->issuepend_done == 1))) {
2710                 struct megasas_aen_event *ev;
2711 
2712                 ev = kzalloc(sizeof(*ev), GFP_ATOMIC);
2713                 if (!ev) {
2714                         dev_err(&instance->pdev->dev, "megasas_service_aen: out of memory\n");
2715                 } else {
2716                         ev->instance = instance;
2717                         instance->ev = ev;
2718                         INIT_DELAYED_WORK(&ev->hotplug_work,
2719                                           megasas_aen_polling);
2720                         schedule_delayed_work(&ev->hotplug_work, 0);
2721                 }
2722         }
2723 }
2724 
2725 static ssize_t
2726 megasas_fw_crash_buffer_store(struct device *cdev,
2727         struct device_attribute *attr, const char *buf, size_t count)
2728 {
2729         struct Scsi_Host *shost = class_to_shost(cdev);
2730         struct megasas_instance *instance =
2731                 (struct megasas_instance *) shost->hostdata;
2732         int val = 0;
2733         unsigned long flags;
2734 
2735         if (kstrtoint(buf, 0, &val) != 0)
2736                 return -EINVAL;
2737 
2738         spin_lock_irqsave(&instance->crashdump_lock, flags);
2739         instance->fw_crash_buffer_offset = val;
2740         spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2741         return strlen(buf);
2742 }
2743 
2744 static ssize_t
2745 megasas_fw_crash_buffer_show(struct device *cdev,
2746         struct device_attribute *attr, char *buf)
2747 {
2748         struct Scsi_Host *shost = class_to_shost(cdev);
2749         struct megasas_instance *instance =
2750                 (struct megasas_instance *) shost->hostdata;
2751         u32 size;
2752         unsigned long buff_addr;
2753         unsigned long dmachunk = CRASH_DMA_BUF_SIZE;
2754         unsigned long src_addr;
2755         unsigned long flags;
2756         u32 buff_offset;
2757 
2758         spin_lock_irqsave(&instance->crashdump_lock, flags);
2759         buff_offset = instance->fw_crash_buffer_offset;
2760         if (!instance->crash_dump_buf &&
2761                 !((instance->fw_crash_state == AVAILABLE) ||
2762                 (instance->fw_crash_state == COPYING))) {
2763                 dev_err(&instance->pdev->dev,
2764                         "Firmware crash dump is not available\n");
2765                 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2766                 return -EINVAL;
2767         }
2768 
2769         buff_addr = (unsigned long) buf;
2770 
2771         if (buff_offset > (instance->fw_crash_buffer_size * dmachunk)) {
2772                 dev_err(&instance->pdev->dev,
2773                         "Firmware crash dump offset is out of range\n");
2774                 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2775                 return 0;
2776         }
2777 
2778         size = (instance->fw_crash_buffer_size * dmachunk) - buff_offset;
2779         size = (size >= PAGE_SIZE) ? (PAGE_SIZE - 1) : size;
2780 
2781         src_addr = (unsigned long)instance->crash_buf[buff_offset / dmachunk] +
2782                 (buff_offset % dmachunk);
2783         memcpy(buf, (void *)src_addr, size);
2784         spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2785 
2786         return size;
2787 }
2788 
2789 static ssize_t
2790 megasas_fw_crash_buffer_size_show(struct device *cdev,
2791         struct device_attribute *attr, char *buf)
2792 {
2793         struct Scsi_Host *shost = class_to_shost(cdev);
2794         struct megasas_instance *instance =
2795                 (struct megasas_instance *) shost->hostdata;
2796 
2797         return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)
2798                 ((instance->fw_crash_buffer_size) * 1024 * 1024)/PAGE_SIZE);
2799 }
2800 
2801 static ssize_t
2802 megasas_fw_crash_state_store(struct device *cdev,
2803         struct device_attribute *attr, const char *buf, size_t count)
2804 {
2805         struct Scsi_Host *shost = class_to_shost(cdev);
2806         struct megasas_instance *instance =
2807                 (struct megasas_instance *) shost->hostdata;
2808         int val = 0;
2809         unsigned long flags;
2810 
2811         if (kstrtoint(buf, 0, &val) != 0)
2812                 return -EINVAL;
2813 
2814         if ((val <= AVAILABLE || val > COPY_ERROR)) {
2815                 dev_err(&instance->pdev->dev, "application updates invalid "
2816                         "firmware crash state\n");
2817                 return -EINVAL;
2818         }
2819 
2820         instance->fw_crash_state = val;
2821 
2822         if ((val == COPIED) || (val == COPY_ERROR)) {
2823                 spin_lock_irqsave(&instance->crashdump_lock, flags);
2824                 megasas_free_host_crash_buffer(instance);
2825                 spin_unlock_irqrestore(&instance->crashdump_lock, flags);
2826                 if (val == COPY_ERROR)
2827                         dev_info(&instance->pdev->dev, "application failed to "
2828                                 "copy Firmware crash dump\n");
2829                 else
2830                         dev_info(&instance->pdev->dev, "Firmware crash dump "
2831                                 "copied successfully\n");
2832         }
2833         return strlen(buf);
2834 }
2835 
2836 static ssize_t
2837 megasas_fw_crash_state_show(struct device *cdev,
2838         struct device_attribute *attr, char *buf)
2839 {
2840         struct Scsi_Host *shost = class_to_shost(cdev);
2841         struct megasas_instance *instance =
2842                 (struct megasas_instance *) shost->hostdata;
2843 
2844         return snprintf(buf, PAGE_SIZE, "%d\n", instance->fw_crash_state);
2845 }
2846 
2847 static ssize_t
2848 megasas_page_size_show(struct device *cdev,
2849         struct device_attribute *attr, char *buf)
2850 {
2851         return snprintf(buf, PAGE_SIZE, "%ld\n", (unsigned long)PAGE_SIZE - 1);
2852 }
2853 
2854 static DEVICE_ATTR(fw_crash_buffer, S_IRUGO | S_IWUSR,
2855         megasas_fw_crash_buffer_show, megasas_fw_crash_buffer_store);
2856 static DEVICE_ATTR(fw_crash_buffer_size, S_IRUGO,
2857         megasas_fw_crash_buffer_size_show, NULL);
2858 static DEVICE_ATTR(fw_crash_state, S_IRUGO | S_IWUSR,
2859         megasas_fw_crash_state_show, megasas_fw_crash_state_store);
2860 static DEVICE_ATTR(page_size, S_IRUGO,
2861         megasas_page_size_show, NULL);
2862 
2863 struct device_attribute *megaraid_host_attrs[] = {
2864         &dev_attr_fw_crash_buffer_size,
2865         &dev_attr_fw_crash_buffer,
2866         &dev_attr_fw_crash_state,
2867         &dev_attr_page_size,
2868         NULL,
2869 };
2870 
2871 /*
2872  * Scsi host template for megaraid_sas driver
2873  */
2874 static struct scsi_host_template megasas_template = {
2875 
2876         .module = THIS_MODULE,
2877         .name = "Avago SAS based MegaRAID driver",
2878         .proc_name = "megaraid_sas",
2879         .slave_configure = megasas_slave_configure,
2880         .slave_alloc = megasas_slave_alloc,
2881         .queuecommand = megasas_queue_command,
2882         .eh_device_reset_handler = megasas_reset_device,
2883         .eh_bus_reset_handler = megasas_reset_bus_host,
2884         .eh_host_reset_handler = megasas_reset_bus_host,
2885         .eh_timed_out = megasas_reset_timer,
2886         .shost_attrs = megaraid_host_attrs,
2887         .bios_param = megasas_bios_param,
2888         .use_clustering = ENABLE_CLUSTERING,
2889         .change_queue_depth = scsi_change_queue_depth,
2890         .no_write_same = 1,
2891 };
2892 
2893 /**
2894  * megasas_complete_int_cmd -   Completes an internal command
2895  * @instance:                   Adapter soft state
2896  * @cmd:                        Command to be completed
2897  *
2898  * The megasas_issue_blocked_cmd() function waits for a command to complete
2899  * after it issues a command. This function wakes up that waiting routine by
2900  * calling wake_up() on the wait queue.
2901  */
2902 static void
2903 megasas_complete_int_cmd(struct megasas_instance *instance,
2904                          struct megasas_cmd *cmd)
2905 {
2906         cmd->cmd_status_drv = cmd->frame->io.cmd_status;
2907         wake_up(&instance->int_cmd_wait_q);
2908 }
2909 
2910 /**
2911  * megasas_complete_abort -     Completes aborting a command
2912  * @instance:                   Adapter soft state
2913  * @cmd:                        Cmd that was issued to abort another cmd
2914  *
2915  * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q
2916  * after it issues an abort on a previously issued command. This function
2917  * wakes up all functions waiting on the same wait queue.
2918  */
2919 static void
2920 megasas_complete_abort(struct megasas_instance *instance,
2921                        struct megasas_cmd *cmd)
2922 {
2923         if (cmd->sync_cmd) {
2924                 cmd->sync_cmd = 0;
2925                 cmd->cmd_status_drv = 0;
2926                 wake_up(&instance->abort_cmd_wait_q);
2927         }
2928 }
2929 
2930 /**
2931  * megasas_complete_cmd -       Completes a command
2932  * @instance:                   Adapter soft state
2933  * @cmd:                        Command to be completed
2934  * @alt_status:                 If non-zero, use this value as status to
2935  *                              SCSI mid-layer instead of the value returned
2936  *                              by the FW. This should be used if caller wants
2937  *                              an alternate status (as in the case of aborted
2938  *                              commands)
2939  */
2940 void
2941 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
2942                      u8 alt_status)
2943 {
2944         int exception = 0;
2945         struct megasas_header *hdr = &cmd->frame->hdr;
2946         unsigned long flags;
2947         struct fusion_context *fusion = instance->ctrl_context;
2948         u32 opcode, status;
2949 
2950         /* flag for the retry reset */
2951         cmd->retry_for_fw_reset = 0;
2952 
2953         if (cmd->scmd)
2954                 cmd->scmd->SCp.ptr = NULL;
2955 
2956         switch (hdr->cmd) {
2957         case MFI_CMD_INVALID:
2958                 /* Some older 1068 controller FW may keep a pended
2959                    MR_DCMD_CTRL_EVENT_GET_INFO left over from the main kernel
2960                    when booting the kdump kernel.  Ignore this command to
2961                    prevent a kernel panic on shutdown of the kdump kernel. */
2962                 dev_warn(&instance->pdev->dev, "MFI_CMD_INVALID command "
2963                        "completed\n");
2964                 dev_warn(&instance->pdev->dev, "If you have a controller "
2965                        "other than PERC5, please upgrade your firmware\n");
2966                 break;
2967         case MFI_CMD_PD_SCSI_IO:
2968         case MFI_CMD_LD_SCSI_IO:
2969 
2970                 /*
2971                  * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been
2972                  * issued either through an IO path or an IOCTL path. If it
2973                  * was via IOCTL, we will send it to internal completion.
2974                  */
2975                 if (cmd->sync_cmd) {
2976                         cmd->sync_cmd = 0;
2977                         megasas_complete_int_cmd(instance, cmd);
2978                         break;
2979                 }
2980 
2981         case MFI_CMD_LD_READ:
2982         case MFI_CMD_LD_WRITE:
2983 
2984                 if (alt_status) {
2985                         cmd->scmd->result = alt_status << 16;
2986                         exception = 1;
2987                 }
2988 
2989                 if (exception) {
2990 
2991                         atomic_dec(&instance->fw_outstanding);
2992 
2993                         scsi_dma_unmap(cmd->scmd);
2994                         cmd->scmd->scsi_done(cmd->scmd);
2995                         megasas_return_cmd(instance, cmd);
2996 
2997                         break;
2998                 }
2999 
3000                 switch (hdr->cmd_status) {
3001 
3002                 case MFI_STAT_OK:
3003                         cmd->scmd->result = DID_OK << 16;
3004                         break;
3005 
3006                 case MFI_STAT_SCSI_IO_FAILED:
3007                 case MFI_STAT_LD_INIT_IN_PROGRESS:
3008                         cmd->scmd->result =
3009                             (DID_ERROR << 16) | hdr->scsi_status;
3010                         break;
3011 
3012                 case MFI_STAT_SCSI_DONE_WITH_ERROR:
3013 
3014                         cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status;
3015 
3016                         if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) {
3017                                 memset(cmd->scmd->sense_buffer, 0,
3018                                        SCSI_SENSE_BUFFERSIZE);
3019                                 memcpy(cmd->scmd->sense_buffer, cmd->sense,
3020                                        hdr->sense_len);
3021 
3022                                 cmd->scmd->result |= DRIVER_SENSE << 24;
3023                         }
3024 
3025                         break;
3026 
3027                 case MFI_STAT_LD_OFFLINE:
3028                 case MFI_STAT_DEVICE_NOT_FOUND:
3029                         cmd->scmd->result = DID_BAD_TARGET << 16;
3030                         break;
3031 
3032                 default:
3033                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "MFI FW status %#x\n",
3034                                hdr->cmd_status);
3035                         cmd->scmd->result = DID_ERROR << 16;
3036                         break;
3037                 }
3038 
3039                 atomic_dec(&instance->fw_outstanding);
3040 
3041                 scsi_dma_unmap(cmd->scmd);
3042                 cmd->scmd->scsi_done(cmd->scmd);
3043                 megasas_return_cmd(instance, cmd);
3044 
3045                 break;
3046 
3047         case MFI_CMD_SMP:
3048         case MFI_CMD_STP:
3049         case MFI_CMD_DCMD:
3050                 opcode = le32_to_cpu(cmd->frame->dcmd.opcode);
3051                 /* Check for LD map update */
3052                 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
3053                         && (cmd->frame->dcmd.mbox.b[1] == 1)) {
3054                         fusion->fast_path_io = 0;
3055                         spin_lock_irqsave(instance->host->host_lock, flags);
3056                         instance->map_update_cmd = NULL;
3057                         if (cmd->frame->hdr.cmd_status != 0) {
3058                                 if (cmd->frame->hdr.cmd_status !=
3059                                     MFI_STAT_NOT_FOUND)
3060                                         dev_warn(&instance->pdev->dev, "map syncfailed, status = 0x%x\n",
3061                                                cmd->frame->hdr.cmd_status);
3062                                 else {
3063                                         megasas_return_cmd(instance, cmd);
3064                                         spin_unlock_irqrestore(
3065                                                 instance->host->host_lock,
3066                                                 flags);
3067                                         break;
3068                                 }
3069                         } else
3070                                 instance->map_id++;
3071                         megasas_return_cmd(instance, cmd);
3072 
3073                         /*
3074                          * Set fast path IO to ZERO.
3075                          * Validate Map will set proper value.
3076                          * Meanwhile all IOs will go as LD IO.
3077                          */
3078                         if (MR_ValidateMapInfo(instance))
3079                                 fusion->fast_path_io = 1;
3080                         else
3081                                 fusion->fast_path_io = 0;
3082                         megasas_sync_map_info(instance);
3083                         spin_unlock_irqrestore(instance->host->host_lock,
3084                                                flags);
3085                         break;
3086                 }
3087                 if (opcode == MR_DCMD_CTRL_EVENT_GET_INFO ||
3088                     opcode == MR_DCMD_CTRL_EVENT_GET) {
3089                         spin_lock_irqsave(&poll_aen_lock, flags);
3090                         megasas_poll_wait_aen = 0;
3091                         spin_unlock_irqrestore(&poll_aen_lock, flags);
3092                 }
3093 
3094                 /* FW has an updated PD sequence */
3095                 if ((opcode == MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
3096                         (cmd->frame->dcmd.mbox.b[0] == 1)) {
3097 
3098                         spin_lock_irqsave(instance->host->host_lock, flags);
3099                         status = cmd->frame->hdr.cmd_status;
3100                         instance->jbod_seq_cmd = NULL;
3101                         megasas_return_cmd(instance, cmd);
3102 
3103                         if (status == MFI_STAT_OK) {
3104                                 instance->pd_seq_map_id++;
3105                                 /* Re-register a pd sync seq num cmd */
3106                                 if (megasas_sync_pd_seq_num(instance, true))
3107                                         instance->use_seqnum_jbod_fp = false;
3108                         } else
3109                                 instance->use_seqnum_jbod_fp = false;
3110 
3111                         spin_unlock_irqrestore(instance->host->host_lock, flags);
3112                         break;
3113                 }
3114 
3115                 /*
3116                  * See if got an event notification
3117                  */
3118                 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
3119                         megasas_service_aen(instance, cmd);
3120                 else
3121                         megasas_complete_int_cmd(instance, cmd);
3122 
3123                 break;
3124 
3125         case MFI_CMD_ABORT:
3126                 /*
3127                  * Cmd issued to abort another cmd returned
3128                  */
3129                 megasas_complete_abort(instance, cmd);
3130                 break;
3131 
3132         default:
3133                 dev_info(&instance->pdev->dev, "Unknown command completed! [0x%X]\n",
3134                        hdr->cmd);
3135                 break;
3136         }
3137 }
3138 
3139 /**
3140  * megasas_issue_pending_cmds_again -   issue all pending cmds
3141  *                                      in FW again because of the fw reset
3142  * @instance:                           Adapter soft state
3143  */
3144 static inline void
3145 megasas_issue_pending_cmds_again(struct megasas_instance *instance)
3146 {
3147         struct megasas_cmd *cmd;
3148         struct list_head clist_local;
3149         union megasas_evt_class_locale class_locale;
3150         unsigned long flags;
3151         u32 seq_num;
3152 
3153         INIT_LIST_HEAD(&clist_local);
3154         spin_lock_irqsave(&instance->hba_lock, flags);
3155         list_splice_init(&instance->internal_reset_pending_q, &clist_local);
3156         spin_unlock_irqrestore(&instance->hba_lock, flags);
3157 
3158         while (!list_empty(&clist_local)) {
3159                 cmd = list_entry((&clist_local)->next,
3160                                         struct megasas_cmd, list);
3161                 list_del_init(&cmd->list);
3162 
3163                 if (cmd->sync_cmd || cmd->scmd) {
3164                         dev_notice(&instance->pdev->dev, "command %p, %p:%d"
3165                                 "detected to be pending while HBA reset\n",
3166                                         cmd, cmd->scmd, cmd->sync_cmd);
3167 
3168                         cmd->retry_for_fw_reset++;
3169 
3170                         if (cmd->retry_for_fw_reset == 3) {
3171                                 dev_notice(&instance->pdev->dev, "cmd %p, %p:%d"
3172                                         "was tried multiple times during reset."
3173                                         "Shutting down the HBA\n",
3174                                         cmd, cmd->scmd, cmd->sync_cmd);
3175                                 instance->instancet->disable_intr(instance);
3176                                 atomic_set(&instance->fw_reset_no_pci_access, 1);
3177                                 megaraid_sas_kill_hba(instance);
3178                                 return;
3179                         }
3180                 }
3181 
3182                 if (cmd->sync_cmd == 1) {
3183                         if (cmd->scmd) {
3184                                 dev_notice(&instance->pdev->dev, "unexpected"
3185                                         "cmd attached to internal command!\n");
3186                         }
3187                         dev_notice(&instance->pdev->dev, "%p synchronous cmd"
3188                                                 "on the internal reset queue,"
3189                                                 "issue it again.\n", cmd);
3190                         cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
3191                         instance->instancet->fire_cmd(instance,
3192                                                         cmd->frame_phys_addr,
3193                                                         0, instance->reg_set);
3194                 } else if (cmd->scmd) {
3195                         dev_notice(&instance->pdev->dev, "%p scsi cmd [%02x]"
3196                         "detected on the internal queue, issue again.\n",
3197                         cmd, cmd->scmd->cmnd[0]);
3198 
3199                         atomic_inc(&instance->fw_outstanding);
3200                         instance->instancet->fire_cmd(instance,
3201                                         cmd->frame_phys_addr,
3202                                         cmd->frame_count-1, instance->reg_set);
3203                 } else {
3204                         dev_notice(&instance->pdev->dev, "%p unexpected cmd on the"
3205                                 "internal reset defer list while re-issue!!\n",
3206                                 cmd);
3207                 }
3208         }
3209 
3210         if (instance->aen_cmd) {
3211                 dev_notice(&instance->pdev->dev, "aen_cmd in def process\n");
3212                 megasas_return_cmd(instance, instance->aen_cmd);
3213 
3214                 instance->aen_cmd = NULL;
3215         }
3216 
3217         /*
3218          * Initiate AEN (Asynchronous Event Notification)
3219          */
3220         seq_num = instance->last_seq_num;
3221         class_locale.members.reserved = 0;
3222         class_locale.members.locale = MR_EVT_LOCALE_ALL;
3223         class_locale.members.class = MR_EVT_CLASS_DEBUG;
3224 
3225         megasas_register_aen(instance, seq_num, class_locale.word);
3226 }
3227 
3228 /**
3229  * Move the internal reset pending commands to a deferred queue.
3230  *
3231  * We move the commands pending at internal reset time to a
3232  * pending queue. This queue would be flushed after successful
3233  * completion of the internal reset sequence. if the internal reset
3234  * did not complete in time, the kernel reset handler would flush
3235  * these commands.
3236  **/
3237 static void
3238 megasas_internal_reset_defer_cmds(struct megasas_instance *instance)
3239 {
3240         struct megasas_cmd *cmd;
3241         int i;
3242         u32 max_cmd = instance->max_fw_cmds;
3243         u32 defer_index;
3244         unsigned long flags;
3245 
3246         defer_index = 0;
3247         spin_lock_irqsave(&instance->mfi_pool_lock, flags);
3248         for (i = 0; i < max_cmd; i++) {
3249                 cmd = instance->cmd_list[i];
3250                 if (cmd->sync_cmd == 1 || cmd->scmd) {
3251                         dev_notice(&instance->pdev->dev, "moving cmd[%d]:%p:%d:%p"
3252                                         "on the defer queue as internal\n",
3253                                 defer_index, cmd, cmd->sync_cmd, cmd->scmd);
3254 
3255                         if (!list_empty(&cmd->list)) {
3256                                 dev_notice(&instance->pdev->dev, "ERROR while"
3257                                         " moving this cmd:%p, %d %p, it was"
3258                                         "discovered on some list?\n",
3259                                         cmd, cmd->sync_cmd, cmd->scmd);
3260 
3261                                 list_del_init(&cmd->list);
3262                         }
3263                         defer_index++;
3264                         list_add_tail(&cmd->list,
3265                                 &instance->internal_reset_pending_q);
3266                 }
3267         }
3268         spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
3269 }
3270 
3271 
3272 static void
3273 process_fw_state_change_wq(struct work_struct *work)
3274 {
3275         struct megasas_instance *instance =
3276                 container_of(work, struct megasas_instance, work_init);
3277         u32 wait;
3278         unsigned long flags;
3279 
3280         if (instance->adprecovery != MEGASAS_ADPRESET_SM_INFAULT) {
3281                 dev_notice(&instance->pdev->dev, "error, recovery st %x\n",
3282                                 instance->adprecovery);
3283                 return ;
3284         }
3285 
3286         if (instance->adprecovery == MEGASAS_ADPRESET_SM_INFAULT) {
3287                 dev_notice(&instance->pdev->dev, "FW detected to be in fault"
3288                                         "state, restarting it...\n");
3289 
3290                 instance->instancet->disable_intr(instance);
3291                 atomic_set(&instance->fw_outstanding, 0);
3292 
3293                 atomic_set(&instance->fw_reset_no_pci_access, 1);
3294                 instance->instancet->adp_reset(instance, instance->reg_set);
3295                 atomic_set(&instance->fw_reset_no_pci_access, 0);
3296 
3297                 dev_notice(&instance->pdev->dev, "FW restarted successfully,"
3298                                         "initiating next stage...\n");
3299 
3300                 dev_notice(&instance->pdev->dev, "HBA recovery state machine,"
3301                                         "state 2 starting...\n");
3302 
3303                 /* waiting for about 20 second before start the second init */
3304                 for (wait = 0; wait < 30; wait++) {
3305                         msleep(1000);
3306                 }
3307 
3308                 if (megasas_transition_to_ready(instance, 1)) {
3309                         dev_notice(&instance->pdev->dev, "adapter not ready\n");
3310 
3311                         atomic_set(&instance->fw_reset_no_pci_access, 1);
3312                         megaraid_sas_kill_hba(instance);
3313                         return ;
3314                 }
3315 
3316                 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
3317                         (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
3318                         (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)
3319                         ) {
3320                         *instance->consumer = *instance->producer;
3321                 } else {
3322                         *instance->consumer = 0;
3323                         *instance->producer = 0;
3324                 }
3325 
3326                 megasas_issue_init_mfi(instance);
3327 
3328                 spin_lock_irqsave(&instance->hba_lock, flags);
3329                 instance->adprecovery   = MEGASAS_HBA_OPERATIONAL;
3330                 spin_unlock_irqrestore(&instance->hba_lock, flags);
3331                 instance->instancet->enable_intr(instance);
3332 
3333                 megasas_issue_pending_cmds_again(instance);
3334                 instance->issuepend_done = 1;
3335         }
3336 }
3337 
3338 /**
3339  * megasas_deplete_reply_queue -        Processes all completed commands
3340  * @instance:                           Adapter soft state
3341  * @alt_status:                         Alternate status to be returned to
3342  *                                      SCSI mid-layer instead of the status
3343  *                                      returned by the FW
3344  * Note: this must be called with hba lock held
3345  */
3346 static int
3347 megasas_deplete_reply_queue(struct megasas_instance *instance,
3348                                         u8 alt_status)
3349 {
3350         u32 mfiStatus;
3351         u32 fw_state;
3352 
3353         if ((mfiStatus = instance->instancet->check_reset(instance,
3354                                         instance->reg_set)) == 1) {
3355                 return IRQ_HANDLED;
3356         }
3357 
3358         if ((mfiStatus = instance->instancet->clear_intr(
3359                                                 instance->reg_set)
3360                                                 ) == 0) {
3361                 /* Hardware may not set outbound_intr_status in MSI-X mode */
3362                 if (!instance->msix_vectors)
3363                         return IRQ_NONE;
3364         }
3365 
3366         instance->mfiStatus = mfiStatus;
3367 
3368         if ((mfiStatus & MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE)) {
3369                 fw_state = instance->instancet->read_fw_status_reg(
3370                                 instance->reg_set) & MFI_STATE_MASK;
3371 
3372                 if (fw_state != MFI_STATE_FAULT) {
3373                         dev_notice(&instance->pdev->dev, "fw state:%x\n",
3374                                                 fw_state);
3375                 }
3376 
3377                 if ((fw_state == MFI_STATE_FAULT) &&
3378                                 (instance->disableOnlineCtrlReset == 0)) {
3379                         dev_notice(&instance->pdev->dev, "wait adp restart\n");
3380 
3381                         if ((instance->pdev->device ==
3382                                         PCI_DEVICE_ID_LSI_SAS1064R) ||
3383                                 (instance->pdev->device ==
3384                                         PCI_DEVICE_ID_DELL_PERC5) ||
3385                                 (instance->pdev->device ==
3386                                         PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
3387 
3388                                 *instance->consumer =
3389                                         cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
3390                         }
3391 
3392 
3393                         instance->instancet->disable_intr(instance);
3394                         instance->adprecovery   = MEGASAS_ADPRESET_SM_INFAULT;
3395                         instance->issuepend_done = 0;
3396 
3397                         atomic_set(&instance->fw_outstanding, 0);
3398                         megasas_internal_reset_defer_cmds(instance);
3399 
3400                         dev_notice(&instance->pdev->dev, "fwState=%x, stage:%d\n",
3401                                         fw_state, instance->adprecovery);
3402 
3403                         schedule_work(&instance->work_init);
3404                         return IRQ_HANDLED;
3405 
3406                 } else {
3407                         dev_notice(&instance->pdev->dev, "fwstate:%x, dis_OCR=%x\n",
3408                                 fw_state, instance->disableOnlineCtrlReset);
3409                 }
3410         }
3411 
3412         tasklet_schedule(&instance->isr_tasklet);
3413         return IRQ_HANDLED;
3414 }
3415 /**
3416  * megasas_isr - isr entry point
3417  */
3418 static irqreturn_t megasas_isr(int irq, void *devp)
3419 {
3420         struct megasas_irq_context *irq_context = devp;
3421         struct megasas_instance *instance = irq_context->instance;
3422         unsigned long flags;
3423         irqreturn_t rc;
3424 
3425         if (atomic_read(&instance->fw_reset_no_pci_access))
3426                 return IRQ_HANDLED;
3427 
3428         spin_lock_irqsave(&instance->hba_lock, flags);
3429         rc = megasas_deplete_reply_queue(instance, DID_OK);
3430         spin_unlock_irqrestore(&instance->hba_lock, flags);
3431 
3432         return rc;
3433 }
3434 
3435 /**
3436  * megasas_transition_to_ready -        Move the FW to READY state
3437  * @instance:                           Adapter soft state
3438  *
3439  * During the initialization, FW passes can potentially be in any one of
3440  * several possible states. If the FW in operational, waiting-for-handshake
3441  * states, driver must take steps to bring it to ready state. Otherwise, it
3442  * has to wait for the ready state.
3443  */
3444 int
3445 megasas_transition_to_ready(struct megasas_instance *instance, int ocr)
3446 {
3447         int i;
3448         u8 max_wait;
3449         u32 fw_state;
3450         u32 cur_state;
3451         u32 abs_state, curr_abs_state;
3452 
3453         abs_state = instance->instancet->read_fw_status_reg(instance->reg_set);
3454         fw_state = abs_state & MFI_STATE_MASK;
3455 
3456         if (fw_state != MFI_STATE_READY)
3457                 dev_info(&instance->pdev->dev, "Waiting for FW to come to ready"
3458                        " state\n");
3459 
3460         while (fw_state != MFI_STATE_READY) {
3461 
3462                 switch (fw_state) {
3463 
3464                 case MFI_STATE_FAULT:
3465                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW in FAULT state!!\n");
3466                         if (ocr) {
3467                                 max_wait = MEGASAS_RESET_WAIT_TIME;
3468                                 cur_state = MFI_STATE_FAULT;
3469                                 break;
3470                         } else
3471                                 return -ENODEV;
3472 
3473                 case MFI_STATE_WAIT_HANDSHAKE:
3474                         /*
3475                          * Set the CLR bit in inbound doorbell
3476                          */
3477                         if ((instance->pdev->device ==
3478                                 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3479                                 (instance->pdev->device ==
3480                                  PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3481                                 (instance->ctrl_context))
3482                                 writel(
3483                                   MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3484                                   &instance->reg_set->doorbell);
3485                         else
3486                                 writel(
3487                                     MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG,
3488                                         &instance->reg_set->inbound_doorbell);
3489 
3490                         max_wait = MEGASAS_RESET_WAIT_TIME;
3491                         cur_state = MFI_STATE_WAIT_HANDSHAKE;
3492                         break;
3493 
3494                 case MFI_STATE_BOOT_MESSAGE_PENDING:
3495                         if ((instance->pdev->device ==
3496                              PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3497                                 (instance->pdev->device ==
3498                                  PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
3499                                 (instance->ctrl_context))
3500                                 writel(MFI_INIT_HOTPLUG,
3501                                        &instance->reg_set->doorbell);
3502                         else
3503                                 writel(MFI_INIT_HOTPLUG,
3504                                         &instance->reg_set->inbound_doorbell);
3505 
3506                         max_wait = MEGASAS_RESET_WAIT_TIME;
3507                         cur_state = MFI_STATE_BOOT_MESSAGE_PENDING;
3508                         break;
3509 
3510                 case MFI_STATE_OPERATIONAL:
3511                         /*
3512                          * Bring it to READY state; assuming max wait 10 secs
3513                          */
3514                         instance->instancet->disable_intr(instance);
3515                         if ((instance->pdev->device ==
3516                                 PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
3517                                 (instance->pdev->device ==
3518                                 PCI_DEVICE_ID_LSI_SAS0071SKINNY)  ||
3519                                 (instance->ctrl_context)) {
3520                                 writel(MFI_RESET_FLAGS,
3521                                         &instance->reg_set->doorbell);
3522 
3523                                 if (instance->ctrl_context) {
3524                                         for (i = 0; i < (10 * 1000); i += 20) {
3525                                                 if (readl(
3526                                                             &instance->
3527                                                             reg_set->
3528                                                             doorbell) & 1)
3529                                                         msleep(20);
3530                                                 else
3531                                                         break;
3532                                         }
3533                                 }
3534                         } else
3535                                 writel(MFI_RESET_FLAGS,
3536                                         &instance->reg_set->inbound_doorbell);
3537 
3538                         max_wait = MEGASAS_RESET_WAIT_TIME;
3539                         cur_state = MFI_STATE_OPERATIONAL;
3540                         break;
3541 
3542                 case MFI_STATE_UNDEFINED:
3543                         /*
3544                          * This state should not last for more than 2 seconds
3545                          */
3546                         max_wait = MEGASAS_RESET_WAIT_TIME;
3547                         cur_state = MFI_STATE_UNDEFINED;
3548                         break;
3549 
3550                 case MFI_STATE_BB_INIT:
3551                         max_wait = MEGASAS_RESET_WAIT_TIME;
3552                         cur_state = MFI_STATE_BB_INIT;
3553                         break;
3554 
3555                 case MFI_STATE_FW_INIT:
3556                         max_wait = MEGASAS_RESET_WAIT_TIME;
3557                         cur_state = MFI_STATE_FW_INIT;
3558                         break;
3559 
3560                 case MFI_STATE_FW_INIT_2:
3561                         max_wait = MEGASAS_RESET_WAIT_TIME;
3562                         cur_state = MFI_STATE_FW_INIT_2;
3563                         break;
3564 
3565                 case MFI_STATE_DEVICE_SCAN:
3566                         max_wait = MEGASAS_RESET_WAIT_TIME;
3567                         cur_state = MFI_STATE_DEVICE_SCAN;
3568                         break;
3569 
3570                 case MFI_STATE_FLUSH_CACHE:
3571                         max_wait = MEGASAS_RESET_WAIT_TIME;
3572                         cur_state = MFI_STATE_FLUSH_CACHE;
3573                         break;
3574 
3575                 default:
3576                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "Unknown state 0x%x\n",
3577                                fw_state);
3578                         return -ENODEV;
3579                 }
3580 
3581                 /*
3582                  * The cur_state should not last for more than max_wait secs
3583                  */
3584                 for (i = 0; i < (max_wait * 1000); i++) {
3585                         curr_abs_state = instance->instancet->
3586                                 read_fw_status_reg(instance->reg_set);
3587 
3588                         if (abs_state == curr_abs_state) {
3589                                 msleep(1);
3590                         } else
3591                                 break;
3592                 }
3593 
3594                 /*
3595                  * Return error if fw_state hasn't changed after max_wait
3596                  */
3597                 if (curr_abs_state == abs_state) {
3598                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "FW state [%d] hasn't changed "
3599                                "in %d secs\n", fw_state, max_wait);
3600                         return -ENODEV;
3601                 }
3602 
3603                 abs_state = curr_abs_state;
3604                 fw_state = curr_abs_state & MFI_STATE_MASK;
3605         }
3606         dev_info(&instance->pdev->dev, "FW now in Ready state\n");
3607 
3608         return 0;
3609 }
3610 
3611 /**
3612  * megasas_teardown_frame_pool -        Destroy the cmd frame DMA pool
3613  * @instance:                           Adapter soft state
3614  */
3615 static void megasas_teardown_frame_pool(struct megasas_instance *instance)
3616 {
3617         int i;
3618         u32 max_cmd = instance->max_mfi_cmds;
3619         struct megasas_cmd *cmd;
3620 
3621         if (!instance->frame_dma_pool)
3622                 return;
3623 
3624         /*
3625          * Return all frames to pool
3626          */
3627         for (i = 0; i < max_cmd; i++) {
3628 
3629                 cmd = instance->cmd_list[i];
3630 
3631                 if (cmd->frame)
3632                         pci_pool_free(instance->frame_dma_pool, cmd->frame,
3633                                       cmd->frame_phys_addr);
3634 
3635                 if (cmd->sense)
3636                         pci_pool_free(instance->sense_dma_pool, cmd->sense,
3637                                       cmd->sense_phys_addr);
3638         }
3639 
3640         /*
3641          * Now destroy the pool itself
3642          */
3643         pci_pool_destroy(instance->frame_dma_pool);
3644         pci_pool_destroy(instance->sense_dma_pool);
3645 
3646         instance->frame_dma_pool = NULL;
3647         instance->sense_dma_pool = NULL;
3648 }
3649 
3650 /**
3651  * megasas_create_frame_pool -  Creates DMA pool for cmd frames
3652  * @instance:                   Adapter soft state
3653  *
3654  * Each command packet has an embedded DMA memory buffer that is used for
3655  * filling MFI frame and the SG list that immediately follows the frame. This
3656  * function creates those DMA memory buffers for each command packet by using
3657  * PCI pool facility.
3658  */
3659 static int megasas_create_frame_pool(struct megasas_instance *instance)
3660 {
3661         int i;
3662         u32 max_cmd;
3663         u32 sge_sz;
3664         u32 total_sz;
3665         u32 frame_count;
3666         struct megasas_cmd *cmd;
3667 
3668         max_cmd = instance->max_mfi_cmds;
3669 
3670         /*
3671          * Size of our frame is 64 bytes for MFI frame, followed by max SG
3672          * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer
3673          */
3674         sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
3675             sizeof(struct megasas_sge32);
3676 
3677         if (instance->flag_ieee)
3678                 sge_sz = sizeof(struct megasas_sge_skinny);
3679 
3680         /*
3681          * For MFI controllers.
3682          * max_num_sge = 60
3683          * max_sge_sz  = 16 byte (sizeof megasas_sge_skinny)
3684          * Total 960 byte (15 MFI frame of 64 byte)
3685          *
3686          * Fusion adapter require only 3 extra frame.
3687          * max_num_sge = 16 (defined as MAX_IOCTL_SGE)
3688          * max_sge_sz  = 12 byte (sizeof  megasas_sge64)
3689          * Total 192 byte (3 MFI frame of 64 byte)
3690          */
3691         frame_count = instance->ctrl_context ? (3 + 1) : (15 + 1);
3692         total_sz = MEGAMFI_FRAME_SIZE * frame_count;
3693         /*
3694          * Use DMA pool facility provided by PCI layer
3695          */
3696         instance->frame_dma_pool = pci_pool_create("megasas frame pool",
3697                                         instance->pdev, total_sz, 256, 0);
3698 
3699         if (!instance->frame_dma_pool) {
3700                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup frame pool\n");
3701                 return -ENOMEM;
3702         }
3703 
3704         instance->sense_dma_pool = pci_pool_create("megasas sense pool",
3705                                                    instance->pdev, 128, 4, 0);
3706 
3707         if (!instance->sense_dma_pool) {
3708                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool\n");
3709 
3710                 pci_pool_destroy(instance->frame_dma_pool);
3711                 instance->frame_dma_pool = NULL;
3712 
3713                 return -ENOMEM;
3714         }
3715 
3716         /*
3717          * Allocate and attach a frame to each of the commands in cmd_list.
3718          * By making cmd->index as the context instead of the &cmd, we can
3719          * always use 32bit context regardless of the architecture
3720          */
3721         for (i = 0; i < max_cmd; i++) {
3722 
3723                 cmd = instance->cmd_list[i];
3724 
3725                 cmd->frame = pci_pool_alloc(instance->frame_dma_pool,
3726                                             GFP_KERNEL, &cmd->frame_phys_addr);
3727 
3728                 cmd->sense = pci_pool_alloc(instance->sense_dma_pool,
3729                                             GFP_KERNEL, &cmd->sense_phys_addr);
3730 
3731                 /*
3732                  * megasas_teardown_frame_pool() takes care of freeing
3733                  * whatever has been allocated
3734                  */
3735                 if (!cmd->frame || !cmd->sense) {
3736                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "pci_pool_alloc failed\n");
3737                         megasas_teardown_frame_pool(instance);
3738                         return -ENOMEM;
3739                 }
3740 
3741                 memset(cmd->frame, 0, total_sz);
3742                 cmd->frame->io.context = cpu_to_le32(cmd->index);
3743                 cmd->frame->io.pad_0 = 0;
3744                 if (!instance->ctrl_context && reset_devices)
3745                         cmd->frame->hdr.cmd = MFI_CMD_INVALID;
3746         }
3747 
3748         return 0;
3749 }
3750 
3751 /**
3752  * megasas_free_cmds -  Free all the cmds in the free cmd pool
3753  * @instance:           Adapter soft state
3754  */
3755 void megasas_free_cmds(struct megasas_instance *instance)
3756 {
3757         int i;
3758 
3759         /* First free the MFI frame pool */
3760         megasas_teardown_frame_pool(instance);
3761 
3762         /* Free all the commands in the cmd_list */
3763         for (i = 0; i < instance->max_mfi_cmds; i++)
3764 
3765                 kfree(instance->cmd_list[i]);
3766 
3767         /* Free the cmd_list buffer itself */
3768         kfree(instance->cmd_list);
3769         instance->cmd_list = NULL;
3770 
3771         INIT_LIST_HEAD(&instance->cmd_pool);
3772 }
3773 
3774 /**
3775  * megasas_alloc_cmds - Allocates the command packets
3776  * @instance:           Adapter soft state
3777  *
3778  * Each command that is issued to the FW, whether IO commands from the OS or
3779  * internal commands like IOCTLs, are wrapped in local data structure called
3780  * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to
3781  * the FW.
3782  *
3783  * Each frame has a 32-bit field called context (tag). This context is used
3784  * to get back the megasas_cmd from the frame when a frame gets completed in
3785  * the ISR. Typically the address of the megasas_cmd itself would be used as
3786  * the context. But we wanted to keep the differences between 32 and 64 bit
3787  * systems to the mininum. We always use 32 bit integers for the context. In
3788  * this driver, the 32 bit values are the indices into an array cmd_list.
3789  * This array is used only to look up the megasas_cmd given the context. The
3790  * free commands themselves are maintained in a linked list called cmd_pool.
3791  */
3792 int megasas_alloc_cmds(struct megasas_instance *instance)
3793 {
3794         int i;
3795         int j;
3796         u32 max_cmd;
3797         struct megasas_cmd *cmd;
3798         struct fusion_context *fusion;
3799 
3800         fusion = instance->ctrl_context;
3801         max_cmd = instance->max_mfi_cmds;
3802 
3803         /*
3804          * instance->cmd_list is an array of struct megasas_cmd pointers.
3805          * Allocate the dynamic array first and then allocate individual
3806          * commands.
3807          */
3808         instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL);
3809 
3810         if (!instance->cmd_list) {
3811                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory\n");
3812                 return -ENOMEM;
3813         }
3814 
3815         memset(instance->cmd_list, 0, sizeof(struct megasas_cmd *) *max_cmd);
3816 
3817         for (i = 0; i < max_cmd; i++) {
3818                 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd),
3819                                                 GFP_KERNEL);
3820 
3821                 if (!instance->cmd_list[i]) {
3822 
3823                         for (j = 0; j < i; j++)
3824                                 kfree(instance->cmd_list[j]);
3825 
3826                         kfree(instance->cmd_list);
3827                         instance->cmd_list = NULL;
3828 
3829                         return -ENOMEM;
3830                 }
3831         }
3832 
3833         for (i = 0; i < max_cmd; i++) {
3834                 cmd = instance->cmd_list[i];
3835                 memset(cmd, 0, sizeof(struct megasas_cmd));
3836                 cmd->index = i;
3837                 cmd->scmd = NULL;
3838                 cmd->instance = instance;
3839 
3840                 list_add_tail(&cmd->list, &instance->cmd_pool);
3841         }
3842 
3843         /*
3844          * Create a frame pool and assign one frame to each cmd
3845          */
3846         if (megasas_create_frame_pool(instance)) {
3847                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
3848                 megasas_free_cmds(instance);
3849         }
3850 
3851         return 0;
3852 }
3853 
3854 /*
3855  * megasas_get_pd_list_info -   Returns FW's pd_list structure
3856  * @instance:                           Adapter soft state
3857  * @pd_list:                            pd_list structure
3858  *
3859  * Issues an internal command (DCMD) to get the FW's controller PD
3860  * list structure.  This information is mainly used to find out SYSTEM
3861  * supported by the FW.
3862  */
3863 static int
3864 megasas_get_pd_list(struct megasas_instance *instance)
3865 {
3866         int ret = 0, pd_index = 0;
3867         struct megasas_cmd *cmd;
3868         struct megasas_dcmd_frame *dcmd;
3869         struct MR_PD_LIST *ci;
3870         struct MR_PD_ADDRESS *pd_addr;
3871         dma_addr_t ci_h = 0;
3872 
3873         cmd = megasas_get_cmd(instance);
3874 
3875         if (!cmd) {
3876                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "(get_pd_list): Failed to get cmd\n");
3877                 return -ENOMEM;
3878         }
3879 
3880         dcmd = &cmd->frame->dcmd;
3881 
3882         ci = pci_alloc_consistent(instance->pdev,
3883                   MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h);
3884 
3885         if (!ci) {
3886                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for pd_list\n");
3887                 megasas_return_cmd(instance, cmd);
3888                 return -ENOMEM;
3889         }
3890 
3891         memset(ci, 0, sizeof(*ci));
3892         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3893 
3894         dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST;
3895         dcmd->mbox.b[1] = 0;
3896         dcmd->cmd = MFI_CMD_DCMD;
3897         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
3898         dcmd->sge_count = 1;
3899         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
3900         dcmd->timeout = 0;
3901         dcmd->pad_0 = 0;
3902         dcmd->data_xfer_len = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3903         dcmd->opcode = cpu_to_le32(MR_DCMD_PD_LIST_QUERY);
3904         dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
3905         dcmd->sgl.sge32[0].length = cpu_to_le32(MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST));
3906 
3907         if (instance->ctrl_context && !instance->mask_interrupts)
3908                 ret = megasas_issue_blocked_cmd(instance, cmd,
3909                         MEGASAS_BLOCKED_CMD_TIMEOUT);
3910         else
3911                 ret = megasas_issue_polled(instance, cmd);
3912 
3913         /*
3914          * the following function will get the instance PD LIST.
3915          */
3916 
3917         pd_addr = ci->addr;
3918 
3919         if (ret == 0 &&
3920              (le32_to_cpu(ci->count) <
3921                   (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) {
3922 
3923                 memset(instance->local_pd_list, 0,
3924                         MEGASAS_MAX_PD * sizeof(struct megasas_pd_list));
3925 
3926                 for (pd_index = 0; pd_index < le32_to_cpu(ci->count); pd_index++) {
3927 
3928                         instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].tid     =
3929                                 le16_to_cpu(pd_addr->deviceId);
3930                         instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveType       =
3931                                                         pd_addr->scsiDevType;
3932                         instance->local_pd_list[le16_to_cpu(pd_addr->deviceId)].driveState      =
3933                                                         MR_PD_STATE_SYSTEM;
3934                         pd_addr++;
3935                 }
3936                 memcpy(instance->pd_list, instance->local_pd_list,
3937                         sizeof(instance->pd_list));
3938         }
3939 
3940         pci_free_consistent(instance->pdev,
3941                                 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST),
3942                                 ci, ci_h);
3943 
3944         megasas_return_cmd(instance, cmd);
3945 
3946         return ret;
3947 }
3948 
3949 /*
3950  * megasas_get_ld_list_info -   Returns FW's ld_list structure
3951  * @instance:                           Adapter soft state
3952  * @ld_list:                            ld_list structure
3953  *
3954  * Issues an internal command (DCMD) to get the FW's controller PD
3955  * list structure.  This information is mainly used to find out SYSTEM
3956  * supported by the FW.
3957  */
3958 static int
3959 megasas_get_ld_list(struct megasas_instance *instance)
3960 {
3961         int ret = 0, ld_index = 0, ids = 0;
3962         struct megasas_cmd *cmd;
3963         struct megasas_dcmd_frame *dcmd;
3964         struct MR_LD_LIST *ci;
3965         dma_addr_t ci_h = 0;
3966         u32 ld_count;
3967 
3968         cmd = megasas_get_cmd(instance);
3969 
3970         if (!cmd) {
3971                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_list: Failed to get cmd\n");
3972                 return -ENOMEM;
3973         }
3974 
3975         dcmd = &cmd->frame->dcmd;
3976 
3977         ci = pci_alloc_consistent(instance->pdev,
3978                                 sizeof(struct MR_LD_LIST),
3979                                 &ci_h);
3980 
3981         if (!ci) {
3982                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem in get_ld_list\n");
3983                 megasas_return_cmd(instance, cmd);
3984                 return -ENOMEM;
3985         }
3986 
3987         memset(ci, 0, sizeof(*ci));
3988         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
3989 
3990         if (instance->supportmax256vd)
3991                 dcmd->mbox.b[0] = 1;
3992         dcmd->cmd = MFI_CMD_DCMD;
3993         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
3994         dcmd->sge_count = 1;
3995         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
3996         dcmd->timeout = 0;
3997         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_LIST));
3998         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_GET_LIST);
3999         dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4000         dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_LIST));
4001         dcmd->pad_0  = 0;
4002 
4003         if (instance->ctrl_context && !instance->mask_interrupts)
4004                 ret = megasas_issue_blocked_cmd(instance, cmd,
4005                         MEGASAS_BLOCKED_CMD_TIMEOUT);
4006         else
4007                 ret = megasas_issue_polled(instance, cmd);
4008 
4009 
4010         ld_count = le32_to_cpu(ci->ldCount);
4011 
4012         /* the following function will get the instance PD LIST */
4013 
4014         if ((ret == 0) && (ld_count <= instance->fw_supported_vd_count)) {
4015                 memset(instance->ld_ids, 0xff, MAX_LOGICAL_DRIVES_EXT);
4016 
4017                 for (ld_index = 0; ld_index < ld_count; ld_index++) {
4018                         if (ci->ldList[ld_index].state != 0) {
4019                                 ids = ci->ldList[ld_index].ref.targetId;
4020                                 instance->ld_ids[ids] =
4021                                         ci->ldList[ld_index].ref.targetId;
4022                         }
4023                 }
4024         }
4025 
4026         pci_free_consistent(instance->pdev,
4027                                 sizeof(struct MR_LD_LIST),
4028                                 ci,
4029                                 ci_h);
4030 
4031         megasas_return_cmd(instance, cmd);
4032         return ret;
4033 }
4034 
4035 /**
4036  * megasas_ld_list_query -      Returns FW's ld_list structure
4037  * @instance:                           Adapter soft state
4038  * @ld_list:                            ld_list structure
4039  *
4040  * Issues an internal command (DCMD) to get the FW's controller PD
4041  * list structure.  This information is mainly used to find out SYSTEM
4042  * supported by the FW.
4043  */
4044 static int
4045 megasas_ld_list_query(struct megasas_instance *instance, u8 query_type)
4046 {
4047         int ret = 0, ld_index = 0, ids = 0;
4048         struct megasas_cmd *cmd;
4049         struct megasas_dcmd_frame *dcmd;
4050         struct MR_LD_TARGETID_LIST *ci;
4051         dma_addr_t ci_h = 0;
4052         u32 tgtid_count;
4053 
4054         cmd = megasas_get_cmd(instance);
4055 
4056         if (!cmd) {
4057                 dev_warn(&instance->pdev->dev,
4058                          "megasas_ld_list_query: Failed to get cmd\n");
4059                 return -ENOMEM;
4060         }
4061 
4062         dcmd = &cmd->frame->dcmd;
4063 
4064         ci = pci_alloc_consistent(instance->pdev,
4065                                   sizeof(struct MR_LD_TARGETID_LIST), &ci_h);
4066 
4067         if (!ci) {
4068                 dev_warn(&instance->pdev->dev,
4069                          "Failed to alloc mem for ld_list_query\n");
4070                 megasas_return_cmd(instance, cmd);
4071                 return -ENOMEM;
4072         }
4073 
4074         memset(ci, 0, sizeof(*ci));
4075         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4076 
4077         dcmd->mbox.b[0] = query_type;
4078         if (instance->supportmax256vd)
4079                 dcmd->mbox.b[2] = 1;
4080 
4081         dcmd->cmd = MFI_CMD_DCMD;
4082         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4083         dcmd->sge_count = 1;
4084         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4085         dcmd->timeout = 0;
4086         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4087         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_LIST_QUERY);
4088         dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4089         dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct MR_LD_TARGETID_LIST));
4090         dcmd->pad_0  = 0;
4091 
4092         if (instance->ctrl_context && !instance->mask_interrupts)
4093                 ret = megasas_issue_blocked_cmd(instance, cmd,
4094                         MEGASAS_BLOCKED_CMD_TIMEOUT);
4095         else
4096                 ret = megasas_issue_polled(instance, cmd);
4097 
4098         tgtid_count = le32_to_cpu(ci->count);
4099 
4100         if ((ret == 0) && (tgtid_count <= (instance->fw_supported_vd_count))) {
4101                 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4102                 for (ld_index = 0; ld_index < tgtid_count; ld_index++) {
4103                         ids = ci->targetId[ld_index];
4104                         instance->ld_ids[ids] = ci->targetId[ld_index];
4105                 }
4106 
4107         }
4108 
4109         pci_free_consistent(instance->pdev, sizeof(struct MR_LD_TARGETID_LIST),
4110                             ci, ci_h);
4111 
4112         megasas_return_cmd(instance, cmd);
4113 
4114         return ret;
4115 }
4116 
4117 /*
4118  * megasas_update_ext_vd_details : Update details w.r.t Extended VD
4119  * instance                      : Controller's instance
4120 */
4121 static void megasas_update_ext_vd_details(struct megasas_instance *instance)
4122 {
4123         struct fusion_context *fusion;
4124         u32 old_map_sz;
4125         u32 new_map_sz;
4126 
4127         fusion = instance->ctrl_context;
4128         /* For MFI based controllers return dummy success */
4129         if (!fusion)
4130                 return;
4131 
4132         instance->supportmax256vd =
4133                 instance->ctrl_info->adapterOperations3.supportMaxExtLDs;
4134         /* Below is additional check to address future FW enhancement */
4135         if (instance->ctrl_info->max_lds > 64)
4136                 instance->supportmax256vd = 1;
4137 
4138         instance->drv_supported_vd_count = MEGASAS_MAX_LD_CHANNELS
4139                                         * MEGASAS_MAX_DEV_PER_CHANNEL;
4140         instance->drv_supported_pd_count = MEGASAS_MAX_PD_CHANNELS
4141                                         * MEGASAS_MAX_DEV_PER_CHANNEL;
4142         if (instance->supportmax256vd) {
4143                 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES_EXT;
4144                 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4145         } else {
4146                 instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4147                 instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4148         }
4149 
4150         dev_info(&instance->pdev->dev,
4151                 "firmware type\t: %s\n",
4152                 instance->supportmax256vd ? "Extended VD(240 VD)firmware" :
4153                 "Legacy(64 VD) firmware");
4154 
4155         old_map_sz = sizeof(struct MR_FW_RAID_MAP) +
4156                                 (sizeof(struct MR_LD_SPAN_MAP) *
4157                                 (instance->fw_supported_vd_count - 1));
4158         new_map_sz = sizeof(struct MR_FW_RAID_MAP_EXT);
4159         fusion->drv_map_sz = sizeof(struct MR_DRV_RAID_MAP) +
4160                                 (sizeof(struct MR_LD_SPAN_MAP) *
4161                                 (instance->drv_supported_vd_count - 1));
4162 
4163         fusion->max_map_sz = max(old_map_sz, new_map_sz);
4164 
4165 
4166         if (instance->supportmax256vd)
4167                 fusion->current_map_sz = new_map_sz;
4168         else
4169                 fusion->current_map_sz = old_map_sz;
4170 }
4171 
4172 /**
4173  * megasas_get_controller_info -        Returns FW's controller structure
4174  * @instance:                           Adapter soft state
4175  *
4176  * Issues an internal command (DCMD) to get the FW's controller structure.
4177  * This information is mainly used to find out the maximum IO transfer per
4178  * command supported by the FW.
4179  */
4180 int
4181 megasas_get_ctrl_info(struct megasas_instance *instance)
4182 {
4183         int ret = 0;
4184         struct megasas_cmd *cmd;
4185         struct megasas_dcmd_frame *dcmd;
4186         struct megasas_ctrl_info *ci;
4187         struct megasas_ctrl_info *ctrl_info;
4188         dma_addr_t ci_h = 0;
4189 
4190         ctrl_info = instance->ctrl_info;
4191 
4192         cmd = megasas_get_cmd(instance);
4193 
4194         if (!cmd) {
4195                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a free cmd\n");
4196                 return -ENOMEM;
4197         }
4198 
4199         dcmd = &cmd->frame->dcmd;
4200 
4201         ci = pci_alloc_consistent(instance->pdev,
4202                                   sizeof(struct megasas_ctrl_info), &ci_h);
4203 
4204         if (!ci) {
4205                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ctrl info\n");
4206                 megasas_return_cmd(instance, cmd);
4207                 return -ENOMEM;
4208         }
4209 
4210         memset(ci, 0, sizeof(*ci));
4211         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4212 
4213         dcmd->cmd = MFI_CMD_DCMD;
4214         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4215         dcmd->sge_count = 1;
4216         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
4217         dcmd->timeout = 0;
4218         dcmd->pad_0 = 0;
4219         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4220         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_GET_INFO);
4221         dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
4222         dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_ctrl_info));
4223         dcmd->mbox.b[0] = 1;
4224 
4225         if (instance->ctrl_context && !instance->mask_interrupts)
4226                 ret = megasas_issue_blocked_cmd(instance, cmd,
4227                         MEGASAS_BLOCKED_CMD_TIMEOUT);
4228         else
4229                 ret = megasas_issue_polled(instance, cmd);
4230 
4231         if (!ret) {
4232                 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info));
4233                 le32_to_cpus((u32 *)&ctrl_info->properties.OnOffProperties);
4234                 le32_to_cpus((u32 *)&ctrl_info->adapterOperations2);
4235                 le32_to_cpus((u32 *)&ctrl_info->adapterOperations3);
4236                 megasas_update_ext_vd_details(instance);
4237                 instance->use_seqnum_jbod_fp =
4238                         ctrl_info->adapterOperations3.useSeqNumJbodFP;
4239                 instance->is_imr = (ctrl_info->memory_size ? 0 : 1);
4240                 dev_info(&instance->pdev->dev,
4241                                 "controller type\t: %s(%dMB)\n",
4242                                 instance->is_imr ? "iMR" : "MR",
4243                                 le16_to_cpu(ctrl_info->memory_size));
4244                 instance->disableOnlineCtrlReset =
4245                         ctrl_info->properties.OnOffProperties.disableOnlineCtrlReset;
4246                 dev_info(&instance->pdev->dev, "Online Controller Reset(OCR)\t: %s\n",
4247                         instance->disableOnlineCtrlReset ? "Disabled" : "Enabled");
4248                 instance->secure_jbod_support =
4249                         ctrl_info->adapterOperations3.supportSecurityonJBOD;
4250                 dev_info(&instance->pdev->dev, "Secure JBOD support\t: %s\n",
4251                         instance->secure_jbod_support ? "Yes" : "No");
4252         }
4253 
4254         pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info),
4255                             ci, ci_h);
4256 
4257         megasas_return_cmd(instance, cmd);
4258         return ret;
4259 }
4260 
4261 /*
4262  * megasas_set_crash_dump_params -      Sends address of crash dump DMA buffer
4263  *                                      to firmware
4264  *
4265  * @instance:                           Adapter soft state
4266  * @crash_buf_state             -       tell FW to turn ON/OFF crash dump feature
4267                                         MR_CRASH_BUF_TURN_OFF = 0
4268                                         MR_CRASH_BUF_TURN_ON = 1
4269  * @return 0 on success non-zero on failure.
4270  * Issues an internal command (DCMD) to set parameters for crash dump feature.
4271  * Driver will send address of crash dump DMA buffer and set mbox to tell FW
4272  * that driver supports crash dump feature. This DCMD will be sent only if
4273  * crash dump feature is supported by the FW.
4274  *
4275  */
4276 int megasas_set_crash_dump_params(struct megasas_instance *instance,
4277         u8 crash_buf_state)
4278 {
4279         int ret = 0;
4280         struct megasas_cmd *cmd;
4281         struct megasas_dcmd_frame *dcmd;
4282 
4283         cmd = megasas_get_cmd(instance);
4284 
4285         if (!cmd) {
4286                 dev_err(&instance->pdev->dev, "Failed to get a free cmd\n");
4287                 return -ENOMEM;
4288         }
4289 
4290 
4291         dcmd = &cmd->frame->dcmd;
4292 
4293         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
4294         dcmd->mbox.b[0] = crash_buf_state;
4295         dcmd->cmd = MFI_CMD_DCMD;
4296         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
4297         dcmd->sge_count = 1;
4298         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
4299         dcmd->timeout = 0;
4300         dcmd->pad_0 = 0;
4301         dcmd->data_xfer_len = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4302         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SET_CRASH_DUMP_PARAMS);
4303         dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->crash_dump_h);
4304         dcmd->sgl.sge32[0].length = cpu_to_le32(CRASH_DMA_BUF_SIZE);
4305 
4306         if (instance->ctrl_context && !instance->mask_interrupts)
4307                 ret = megasas_issue_blocked_cmd(instance, cmd,
4308                         MEGASAS_BLOCKED_CMD_TIMEOUT);
4309         else
4310                 ret = megasas_issue_polled(instance, cmd);
4311 
4312         megasas_return_cmd(instance, cmd);
4313         return ret;
4314 }
4315 
4316 /**
4317  * megasas_issue_init_mfi -     Initializes the FW
4318  * @instance:           Adapter soft state
4319  *
4320  * Issues the INIT MFI cmd
4321  */
4322 static int
4323 megasas_issue_init_mfi(struct megasas_instance *instance)
4324 {
4325         __le32 context;
4326         struct megasas_cmd *cmd;
4327         struct megasas_init_frame *init_frame;
4328         struct megasas_init_queue_info *initq_info;
4329         dma_addr_t init_frame_h;
4330         dma_addr_t initq_info_h;
4331 
4332         /*
4333          * Prepare a init frame. Note the init frame points to queue info
4334          * structure. Each frame has SGL allocated after first 64 bytes. For
4335          * this frame - since we don't need any SGL - we use SGL's space as
4336          * queue info structure
4337          *
4338          * We will not get a NULL command below. We just created the pool.
4339          */
4340         cmd = megasas_get_cmd(instance);
4341 
4342         init_frame = (struct megasas_init_frame *)cmd->frame;
4343         initq_info = (struct megasas_init_queue_info *)
4344                 ((unsigned long)init_frame + 64);
4345 
4346         init_frame_h = cmd->frame_phys_addr;
4347         initq_info_h = init_frame_h + 64;
4348 
4349         context = init_frame->context;
4350         memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
4351         memset(initq_info, 0, sizeof(struct megasas_init_queue_info));
4352         init_frame->context = context;
4353 
4354         initq_info->reply_queue_entries = cpu_to_le32(instance->max_fw_cmds + 1);
4355         initq_info->reply_queue_start_phys_addr_lo = cpu_to_le32(instance->reply_queue_h);
4356 
4357         initq_info->producer_index_phys_addr_lo = cpu_to_le32(instance->producer_h);
4358         initq_info->consumer_index_phys_addr_lo = cpu_to_le32(instance->consumer_h);
4359 
4360         init_frame->cmd = MFI_CMD_INIT;
4361         init_frame->cmd_status = MFI_STAT_INVALID_STATUS;
4362         init_frame->queue_info_new_phys_addr_lo =
4363                 cpu_to_le32(lower_32_bits(initq_info_h));
4364         init_frame->queue_info_new_phys_addr_hi =
4365                 cpu_to_le32(upper_32_bits(initq_info_h));
4366 
4367         init_frame->data_xfer_len = cpu_to_le32(sizeof(struct megasas_init_queue_info));
4368 
4369         /*
4370          * disable the intr before firing the init frame to FW
4371          */
4372         instance->instancet->disable_intr(instance);
4373 
4374         /*
4375          * Issue the init frame in polled mode
4376          */
4377 
4378         if (megasas_issue_polled(instance, cmd)) {
4379                 dev_err(&instance->pdev->dev, "Failed to init firmware\n");
4380                 megasas_return_cmd(instance, cmd);
4381                 goto fail_fw_init;
4382         }
4383 
4384         megasas_return_cmd(instance, cmd);
4385 
4386         return 0;
4387 
4388 fail_fw_init:
4389         return -EINVAL;
4390 }
4391 
4392 static u32
4393 megasas_init_adapter_mfi(struct megasas_instance *instance)
4394 {
4395         struct megasas_register_set __iomem *reg_set;
4396         u32 context_sz;
4397         u32 reply_q_sz;
4398 
4399         reg_set = instance->reg_set;
4400 
4401         /*
4402          * Get various operational parameters from status register
4403          */
4404         instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
4405         /*
4406          * Reduce the max supported cmds by 1. This is to ensure that the
4407          * reply_q_sz (1 more than the max cmd that driver may send)
4408          * does not exceed max cmds that the FW can support
4409          */
4410         instance->max_fw_cmds = instance->max_fw_cmds-1;
4411         instance->max_mfi_cmds = instance->max_fw_cmds;
4412         instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >>
4413                                         0x10;
4414         /*
4415          * For MFI skinny adapters, MEGASAS_SKINNY_INT_CMDS commands
4416          * are reserved for IOCTL + driver's internal DCMDs.
4417          */
4418         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
4419                 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) {
4420                 instance->max_scsi_cmds = (instance->max_fw_cmds -
4421                         MEGASAS_SKINNY_INT_CMDS);
4422                 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS);
4423         } else {
4424                 instance->max_scsi_cmds = (instance->max_fw_cmds -
4425                         MEGASAS_INT_CMDS);
4426                 sema_init(&instance->ioctl_sem, (MEGASAS_MFI_IOCTL_CMDS));
4427         }
4428 
4429         /*
4430          * Create a pool of commands
4431          */
4432         if (megasas_alloc_cmds(instance))
4433                 goto fail_alloc_cmds;
4434 
4435         /*
4436          * Allocate memory for reply queue. Length of reply queue should
4437          * be _one_ more than the maximum commands handled by the firmware.
4438          *
4439          * Note: When FW completes commands, it places corresponding contex
4440          * values in this circular reply queue. This circular queue is a fairly
4441          * typical producer-consumer queue. FW is the producer (of completed
4442          * commands) and the driver is the consumer.
4443          */
4444         context_sz = sizeof(u32);
4445         reply_q_sz = context_sz * (instance->max_fw_cmds + 1);
4446 
4447         instance->reply_queue = pci_alloc_consistent(instance->pdev,
4448                                                      reply_q_sz,
4449                                                      &instance->reply_queue_h);
4450 
4451         if (!instance->reply_queue) {
4452                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Out of DMA mem for reply queue\n");
4453                 goto fail_reply_queue;
4454         }
4455 
4456         if (megasas_issue_init_mfi(instance))
4457                 goto fail_fw_init;
4458 
4459         if (megasas_get_ctrl_info(instance)) {
4460                 dev_err(&instance->pdev->dev, "(%d): Could get controller info "
4461                         "Fail from %s %d\n", instance->unique_id,
4462                         __func__, __LINE__);
4463                 goto fail_fw_init;
4464         }
4465 
4466         instance->fw_support_ieee = 0;
4467         instance->fw_support_ieee =
4468                 (instance->instancet->read_fw_status_reg(reg_set) &
4469                 0x04000000);
4470 
4471         dev_notice(&instance->pdev->dev, "megasas_init_mfi: fw_support_ieee=%d",
4472                         instance->fw_support_ieee);
4473 
4474         if (instance->fw_support_ieee)
4475                 instance->flag_ieee = 1;
4476 
4477         return 0;
4478 
4479 fail_fw_init:
4480 
4481         pci_free_consistent(instance->pdev, reply_q_sz,
4482                             instance->reply_queue, instance->reply_queue_h);
4483 fail_reply_queue:
4484         megasas_free_cmds(instance);
4485 
4486 fail_alloc_cmds:
4487         return 1;
4488 }
4489 
4490 /*
4491  * megasas_setup_irqs_msix -            register legacy interrupts.
4492  * @instance:                           Adapter soft state
4493  *
4494  * Do not enable interrupt, only setup ISRs.
4495  *
4496  * Return 0 on success.
4497  */
4498 static int
4499 megasas_setup_irqs_ioapic(struct megasas_instance *instance)
4500 {
4501         struct pci_dev *pdev;
4502 
4503         pdev = instance->pdev;
4504         instance->irq_context[0].instance = instance;
4505         instance->irq_context[0].MSIxIndex = 0;
4506         if (request_irq(pdev->irq, instance->instancet->service_isr,
4507                 IRQF_SHARED, "megasas", &instance->irq_context[0])) {
4508                 dev_err(&instance->pdev->dev,
4509                                 "Failed to register IRQ from %s %d\n",
4510                                 __func__, __LINE__);
4511                 return -1;
4512         }
4513         return 0;
4514 }
4515 
4516 /**
4517  * megasas_setup_irqs_msix -            register MSI-x interrupts.
4518  * @instance:                           Adapter soft state
4519  * @is_probe:                           Driver probe check
4520  *
4521  * Do not enable interrupt, only setup ISRs.
4522  *
4523  * Return 0 on success.
4524  */
4525 static int
4526 megasas_setup_irqs_msix(struct megasas_instance *instance, u8 is_probe)
4527 {
4528         int i, j, cpu;
4529         struct pci_dev *pdev;
4530 
4531         pdev = instance->pdev;
4532 
4533         /* Try MSI-x */
4534         cpu = cpumask_first(cpu_online_mask);
4535         for (i = 0; i < instance->msix_vectors; i++) {
4536                 instance->irq_context[i].instance = instance;
4537                 instance->irq_context[i].MSIxIndex = i;
4538                 if (request_irq(instance->msixentry[i].vector,
4539                         instance->instancet->service_isr, 0, "megasas",
4540                         &instance->irq_context[i])) {
4541                         dev_err(&instance->pdev->dev,
4542                                 "Failed to register IRQ for vector %d.\n", i);
4543                         for (j = 0; j < i; j++) {
4544                                 if (smp_affinity_enable)
4545                                         irq_set_affinity_hint(
4546                                                 instance->msixentry[j].vector, NULL);
4547                                 free_irq(instance->msixentry[j].vector,
4548                                         &instance->irq_context[j]);
4549                         }
4550                         /* Retry irq register for IO_APIC*/
4551                         instance->msix_vectors = 0;
4552                         if (is_probe)
4553                                 return megasas_setup_irqs_ioapic(instance);
4554                         else
4555                                 return -1;
4556                 }
4557                 if (smp_affinity_enable) {
4558                         if (irq_set_affinity_hint(instance->msixentry[i].vector,
4559                                 get_cpu_mask(cpu)))
4560                                 dev_err(&instance->pdev->dev,
4561                                         "Failed to set affinity hint"
4562                                         " for cpu %d\n", cpu);
4563                         cpu = cpumask_next(cpu, cpu_online_mask);
4564                 }
4565         }
4566         return 0;
4567 }
4568 
4569 /*
4570  * megasas_destroy_irqs-                unregister interrupts.
4571  * @instance:                           Adapter soft state
4572  * return:                              void
4573  */
4574 static void
4575 megasas_destroy_irqs(struct megasas_instance *instance) {
4576 
4577         int i;
4578 
4579         if (instance->msix_vectors)
4580                 for (i = 0; i < instance->msix_vectors; i++) {
4581                         if (smp_affinity_enable)
4582                                 irq_set_affinity_hint(
4583                                         instance->msixentry[i].vector, NULL);
4584                         free_irq(instance->msixentry[i].vector,
4585                                  &instance->irq_context[i]);
4586                 }
4587         else
4588                 free_irq(instance->pdev->irq, &instance->irq_context[0]);
4589 }
4590 
4591 /**
4592  * megasas_setup_jbod_map -     setup jbod map for FP seq_number.
4593  * @instance:                           Adapter soft state
4594  * @is_probe:                           Driver probe check
4595  *
4596  * Return 0 on success.
4597  */
4598 void
4599 megasas_setup_jbod_map(struct megasas_instance *instance)
4600 {
4601         int i;
4602         struct fusion_context *fusion = instance->ctrl_context;
4603         u32 pd_seq_map_sz;
4604 
4605         pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
4606                 (sizeof(struct MR_PD_CFG_SEQ) * (MAX_PHYSICAL_DEVICES - 1));
4607 
4608         if (reset_devices || !fusion ||
4609                 !instance->ctrl_info->adapterOperations3.useSeqNumJbodFP) {
4610                 dev_info(&instance->pdev->dev,
4611                         "Jbod map is not supported %s %d\n",
4612                         __func__, __LINE__);
4613                 instance->use_seqnum_jbod_fp = false;
4614                 return;
4615         }
4616 
4617         if (fusion->pd_seq_sync[0])
4618                 goto skip_alloc;
4619 
4620         for (i = 0; i < JBOD_MAPS_COUNT; i++) {
4621                 fusion->pd_seq_sync[i] = dma_alloc_coherent
4622                         (&instance->pdev->dev, pd_seq_map_sz,
4623                         &fusion->pd_seq_phys[i], GFP_KERNEL);
4624                 if (!fusion->pd_seq_sync[i]) {
4625                         dev_err(&instance->pdev->dev,
4626                                 "Failed to allocate memory from %s %d\n",
4627                                 __func__, __LINE__);
4628                         if (i == 1) {
4629                                 dma_free_coherent(&instance->pdev->dev,
4630                                         pd_seq_map_sz, fusion->pd_seq_sync[0],
4631                                         fusion->pd_seq_phys[0]);
4632                                 fusion->pd_seq_sync[0] = NULL;
4633                         }
4634                         instance->use_seqnum_jbod_fp = false;
4635                         return;
4636                 }
4637         }
4638 
4639 skip_alloc:
4640         if (!megasas_sync_pd_seq_num(instance, false) &&
4641                 !megasas_sync_pd_seq_num(instance, true))
4642                 instance->use_seqnum_jbod_fp = true;
4643         else
4644                 instance->use_seqnum_jbod_fp = false;
4645 }
4646 
4647 /**
4648  * megasas_init_fw -    Initializes the FW
4649  * @instance:           Adapter soft state
4650  *
4651  * This is the main function for initializing firmware
4652  */
4653 
4654 static int megasas_init_fw(struct megasas_instance *instance)
4655 {
4656         u32 max_sectors_1;
4657         u32 max_sectors_2;
4658         u32 tmp_sectors, msix_enable, scratch_pad_2;
4659         resource_size_t base_addr;
4660         struct megasas_register_set __iomem *reg_set;
4661         struct megasas_ctrl_info *ctrl_info = NULL;
4662         unsigned long bar_list;
4663         int i, loop, fw_msix_count = 0;
4664         struct IOV_111 *iovPtr;
4665         struct fusion_context *fusion;
4666 
4667         fusion = instance->ctrl_context;
4668 
4669         /* Find first memory bar */
4670         bar_list = pci_select_bars(instance->pdev, IORESOURCE_MEM);
4671         instance->bar = find_first_bit(&bar_list, sizeof(unsigned long));
4672         if (pci_request_selected_regions(instance->pdev, instance->bar,
4673                                          "megasas: LSI")) {
4674                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "IO memory region busy!\n");
4675                 return -EBUSY;
4676         }
4677 
4678         base_addr = pci_resource_start(instance->pdev, instance->bar);
4679         instance->reg_set = ioremap_nocache(base_addr, 8192);
4680 
4681         if (!instance->reg_set) {
4682                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to map IO mem\n");
4683                 goto fail_ioremap;
4684         }
4685 
4686         reg_set = instance->reg_set;
4687 
4688         switch (instance->pdev->device) {
4689         case PCI_DEVICE_ID_LSI_FUSION:
4690         case PCI_DEVICE_ID_LSI_PLASMA:
4691         case PCI_DEVICE_ID_LSI_INVADER:
4692         case PCI_DEVICE_ID_LSI_FURY:
4693         case PCI_DEVICE_ID_LSI_INTRUDER:
4694         case PCI_DEVICE_ID_LSI_INTRUDER_24:
4695         case PCI_DEVICE_ID_LSI_CUTLASS_52:
4696         case PCI_DEVICE_ID_LSI_CUTLASS_53:
4697                 instance->instancet = &megasas_instance_template_fusion;
4698                 break;
4699         case PCI_DEVICE_ID_LSI_SAS1078R:
4700         case PCI_DEVICE_ID_LSI_SAS1078DE:
4701                 instance->instancet = &megasas_instance_template_ppc;
4702                 break;
4703         case PCI_DEVICE_ID_LSI_SAS1078GEN2:
4704         case PCI_DEVICE_ID_LSI_SAS0079GEN2:
4705                 instance->instancet = &megasas_instance_template_gen2;
4706                 break;
4707         case PCI_DEVICE_ID_LSI_SAS0073SKINNY:
4708         case PCI_DEVICE_ID_LSI_SAS0071SKINNY:
4709                 instance->instancet = &megasas_instance_template_skinny;
4710                 break;
4711         case PCI_DEVICE_ID_LSI_SAS1064R:
4712         case PCI_DEVICE_ID_DELL_PERC5:
4713         default:
4714                 instance->instancet = &megasas_instance_template_xscale;
4715                 instance->allow_fw_scan = 1;
4716                 break;
4717         }
4718 
4719         if (megasas_transition_to_ready(instance, 0)) {
4720                 atomic_set(&instance->fw_reset_no_pci_access, 1);
4721                 instance->instancet->adp_reset
4722                         (instance, instance->reg_set);
4723                 atomic_set(&instance->fw_reset_no_pci_access, 0);
4724                 dev_info(&instance->pdev->dev,
4725                         "FW restarted successfully from %s!\n",
4726                         __func__);
4727 
4728                 /*waitting for about 30 second before retry*/
4729                 ssleep(30);
4730 
4731                 if (megasas_transition_to_ready(instance, 0))
4732                         goto fail_ready_state;
4733         }
4734 
4735         /*
4736          * MSI-X host index 0 is common for all adapter.
4737          * It is used for all MPT based Adapters.
4738          */
4739         instance->reply_post_host_index_addr[0] =
4740                 (u32 __iomem *)((u8 __iomem *)instance->reg_set +
4741                 MPI2_REPLY_POST_HOST_INDEX_OFFSET);
4742 
4743         /* Check if MSI-X is supported while in ready state */
4744         msix_enable = (instance->instancet->read_fw_status_reg(reg_set) &
4745                        0x4000000) >> 0x1a;
4746         if (msix_enable && !msix_disable) {
4747                 scratch_pad_2 = readl
4748                         (&instance->reg_set->outbound_scratch_pad_2);
4749                 /* Check max MSI-X vectors */
4750                 if (fusion) {
4751                         if (fusion->adapter_type == THUNDERBOLT_SERIES) { /* Thunderbolt Series*/
4752                                 instance->msix_vectors = (scratch_pad_2
4753                                         & MR_MAX_REPLY_QUEUES_OFFSET) + 1;
4754                                 fw_msix_count = instance->msix_vectors;
4755                         } else { /* Invader series supports more than 8 MSI-x vectors*/
4756                                 instance->msix_vectors = ((scratch_pad_2
4757                                         & MR_MAX_REPLY_QUEUES_EXT_OFFSET)
4758                                         >> MR_MAX_REPLY_QUEUES_EXT_OFFSET_SHIFT) + 1;
4759                                 fw_msix_count = instance->msix_vectors;
4760                                 /* Save 1-15 reply post index address to local memory
4761                                  * Index 0 is already saved from reg offset
4762                                  * MPI2_REPLY_POST_HOST_INDEX_OFFSET
4763                                  */
4764                                 for (loop = 1; loop < MR_MAX_MSIX_REG_ARRAY; loop++) {
4765                                         instance->reply_post_host_index_addr[loop] =
4766                                                 (u32 __iomem *)
4767                                                 ((u8 __iomem *)instance->reg_set +
4768                                                 MPI2_SUP_REPLY_POST_HOST_INDEX_OFFSET
4769                                                 + (loop * 0x10));
4770                                 }
4771                         }
4772                         if (msix_vectors)
4773                                 instance->msix_vectors = min(msix_vectors,
4774                                         instance->msix_vectors);
4775                 } else /* MFI adapters */
4776                         instance->msix_vectors = 1;
4777                 /* Don't bother allocating more MSI-X vectors than cpus */
4778                 instance->msix_vectors = min(instance->msix_vectors,
4779                                              (unsigned int)num_online_cpus());
4780                 for (i = 0; i < instance->msix_vectors; i++)
4781                         instance->msixentry[i].entry = i;
4782                 i = pci_enable_msix_range(instance->pdev, instance->msixentry,
4783                                           1, instance->msix_vectors);
4784                 if (i > 0)
4785                         instance->msix_vectors = i;
4786                 else
4787                         instance->msix_vectors = 0;
4788         }
4789 
4790         dev_info(&instance->pdev->dev,
4791                 "firmware supports msix\t: (%d)", fw_msix_count);
4792         dev_info(&instance->pdev->dev,
4793                 "current msix/online cpus\t: (%d/%d)\n",
4794                 instance->msix_vectors, (unsigned int)num_online_cpus());
4795 
4796         tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
4797                 (unsigned long)instance);
4798 
4799         if (instance->msix_vectors ?
4800                 megasas_setup_irqs_msix(instance, 1) :
4801                 megasas_setup_irqs_ioapic(instance))
4802                 goto fail_setup_irqs;
4803 
4804         instance->ctrl_info = kzalloc(sizeof(struct megasas_ctrl_info),
4805                                 GFP_KERNEL);
4806         if (instance->ctrl_info == NULL)
4807                 goto fail_init_adapter;
4808 
4809         /*
4810          * Below are default value for legacy Firmware.
4811          * non-fusion based controllers
4812          */
4813         instance->fw_supported_vd_count = MAX_LOGICAL_DRIVES;
4814         instance->fw_supported_pd_count = MAX_PHYSICAL_DEVICES;
4815         /* Get operational params, sge flags, send init cmd to controller */
4816         if (instance->instancet->init_adapter(instance))
4817                 goto fail_init_adapter;
4818 
4819 
4820         instance->instancet->enable_intr(instance);
4821 
4822         dev_err(&instance->pdev->dev, "INIT adapter done\n");
4823 
4824         megasas_setup_jbod_map(instance);
4825 
4826         /** for passthrough
4827          * the following function will get the PD LIST.
4828          */
4829         memset(instance->pd_list, 0,
4830                 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)));
4831         if (megasas_get_pd_list(instance) < 0) {
4832                 dev_err(&instance->pdev->dev, "failed to get PD list\n");
4833                 goto fail_get_pd_list;
4834         }
4835 
4836         memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
4837         if (megasas_ld_list_query(instance,
4838                                   MR_LD_QUERY_TYPE_EXPOSED_TO_HOST))
4839                 megasas_get_ld_list(instance);
4840 
4841         /*
4842          * Compute the max allowed sectors per IO: The controller info has two
4843          * limits on max sectors. Driver should use the minimum of these two.
4844          *
4845          * 1 << stripe_sz_ops.min = max sectors per strip
4846          *
4847          * Note that older firmwares ( < FW ver 30) didn't report information
4848          * to calculate max_sectors_1. So the number ended up as zero always.
4849          */
4850         tmp_sectors = 0;
4851         ctrl_info = instance->ctrl_info;
4852 
4853         max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) *
4854                 le16_to_cpu(ctrl_info->max_strips_per_io);
4855         max_sectors_2 = le32_to_cpu(ctrl_info->max_request_size);
4856 
4857         tmp_sectors = min_t(u32, max_sectors_1, max_sectors_2);
4858 
4859         instance->mpio = ctrl_info->adapterOperations2.mpio;
4860         instance->UnevenSpanSupport =
4861                 ctrl_info->adapterOperations2.supportUnevenSpans;
4862         if (instance->UnevenSpanSupport) {
4863                 struct fusion_context *fusion = instance->ctrl_context;
4864                 if (MR_ValidateMapInfo(instance))
4865                         fusion->fast_path_io = 1;
4866                 else
4867                         fusion->fast_path_io = 0;
4868 
4869         }
4870         if (ctrl_info->host_interface.SRIOV) {
4871                 instance->requestorId = ctrl_info->iov.requestorId;
4872                 if (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA) {
4873                         if (!ctrl_info->adapterOperations2.activePassive)
4874                             instance->PlasmaFW111 = 1;
4875 
4876                         dev_info(&instance->pdev->dev, "SR-IOV: firmware type: %s\n",
4877                             instance->PlasmaFW111 ? "1.11" : "new");
4878 
4879                         if (instance->PlasmaFW111) {
4880                             iovPtr = (struct IOV_111 *)
4881                                 ((unsigned char *)ctrl_info + IOV_111_OFFSET);
4882                             instance->requestorId = iovPtr->requestorId;
4883                         }
4884                 }
4885                 dev_info(&instance->pdev->dev, "SRIOV: VF requestorId %d\n",
4886                         instance->requestorId);
4887         }
4888 
4889         instance->crash_dump_fw_support =
4890                 ctrl_info->adapterOperations3.supportCrashDump;
4891         instance->crash_dump_drv_support =
4892                 (instance->crash_dump_fw_support &&
4893                 instance->crash_dump_buf);
4894         if (instance->crash_dump_drv_support)
4895                 megasas_set_crash_dump_params(instance,
4896                         MR_CRASH_BUF_TURN_OFF);
4897 
4898         else {
4899                 if (instance->crash_dump_buf)
4900                         pci_free_consistent(instance->pdev,
4901                                 CRASH_DMA_BUF_SIZE,
4902                                 instance->crash_dump_buf,
4903                                 instance->crash_dump_h);
4904                 instance->crash_dump_buf = NULL;
4905         }
4906 
4907 
4908         dev_info(&instance->pdev->dev,
4909                 "pci id\t\t: (0x%04x)/(0x%04x)/(0x%04x)/(0x%04x)\n",
4910                 le16_to_cpu(ctrl_info->pci.vendor_id),
4911                 le16_to_cpu(ctrl_info->pci.device_id),
4912                 le16_to_cpu(ctrl_info->pci.sub_vendor_id),
4913                 le16_to_cpu(ctrl_info->pci.sub_device_id));
4914         dev_info(&instance->pdev->dev, "unevenspan support      : %s\n",
4915                 instance->UnevenSpanSupport ? "yes" : "no");
4916         dev_info(&instance->pdev->dev, "firmware crash dump     : %s\n",
4917                 instance->crash_dump_drv_support ? "yes" : "no");
4918         dev_info(&instance->pdev->dev, "jbod sync map           : %s\n",
4919                 instance->use_seqnum_jbod_fp ? "yes" : "no");
4920 
4921 
4922         instance->max_sectors_per_req = instance->max_num_sge *
4923                                                 SGE_BUFFER_SIZE / 512;
4924         if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors))
4925                 instance->max_sectors_per_req = tmp_sectors;
4926 
4927         /* Check for valid throttlequeuedepth module parameter */
4928         if (throttlequeuedepth &&
4929                         throttlequeuedepth <= instance->max_scsi_cmds)
4930                 instance->throttlequeuedepth = throttlequeuedepth;
4931         else
4932                 instance->throttlequeuedepth =
4933                                 MEGASAS_THROTTLE_QUEUE_DEPTH;
4934 
4935 
4936         /* Launch SR-IOV heartbeat timer */
4937         if (instance->requestorId) {
4938                 if (!megasas_sriov_start_heartbeat(instance, 1))
4939                         megasas_start_timer(instance,
4940                                             &instance->sriov_heartbeat_timer,
4941                                             megasas_sriov_heartbeat_handler,
4942                                             MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4943                 else
4944                         instance->skip_heartbeat_timer_del = 1;
4945         }
4946 
4947         return 0;
4948 
4949 fail_get_pd_list:
4950         instance->instancet->disable_intr(instance);
4951 fail_init_adapter:
4952         megasas_destroy_irqs(instance);
4953 fail_setup_irqs:
4954         if (instance->msix_vectors)
4955                 pci_disable_msix(instance->pdev);
4956         instance->msix_vectors = 0;
4957 fail_ready_state:
4958         kfree(instance->ctrl_info);
4959         instance->ctrl_info = NULL;
4960         iounmap(instance->reg_set);
4961 
4962       fail_ioremap:
4963         pci_release_selected_regions(instance->pdev, instance->bar);
4964 
4965         return -EINVAL;
4966 }
4967 
4968 /**
4969  * megasas_release_mfi -        Reverses the FW initialization
4970  * @instance:                   Adapter soft state
4971  */
4972 static void megasas_release_mfi(struct megasas_instance *instance)
4973 {
4974         u32 reply_q_sz = sizeof(u32) *(instance->max_mfi_cmds + 1);
4975 
4976         if (instance->reply_queue)
4977                 pci_free_consistent(instance->pdev, reply_q_sz,
4978                             instance->reply_queue, instance->reply_queue_h);
4979 
4980         megasas_free_cmds(instance);
4981 
4982         iounmap(instance->reg_set);
4983 
4984         pci_release_selected_regions(instance->pdev, instance->bar);
4985 }
4986 
4987 /**
4988  * megasas_get_seq_num -        Gets latest event sequence numbers
4989  * @instance:                   Adapter soft state
4990  * @eli:                        FW event log sequence numbers information
4991  *
4992  * FW maintains a log of all events in a non-volatile area. Upper layers would
4993  * usually find out the latest sequence number of the events, the seq number at
4994  * the boot etc. They would "read" all the events below the latest seq number
4995  * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq
4996  * number), they would subsribe to AEN (asynchronous event notification) and
4997  * wait for the events to happen.
4998  */
4999 static int
5000 megasas_get_seq_num(struct megasas_instance *instance,
5001                     struct megasas_evt_log_info *eli)
5002 {
5003         struct megasas_cmd *cmd;
5004         struct megasas_dcmd_frame *dcmd;
5005         struct megasas_evt_log_info *el_info;
5006         dma_addr_t el_info_h = 0;
5007 
5008         cmd = megasas_get_cmd(instance);
5009 
5010         if (!cmd) {
5011                 return -ENOMEM;
5012         }
5013 
5014         dcmd = &cmd->frame->dcmd;
5015         el_info = pci_alloc_consistent(instance->pdev,
5016                                        sizeof(struct megasas_evt_log_info),
5017                                        &el_info_h);
5018 
5019         if (!el_info) {
5020                 megasas_return_cmd(instance, cmd);
5021                 return -ENOMEM;
5022         }
5023 
5024         memset(el_info, 0, sizeof(*el_info));
5025         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5026 
5027         dcmd->cmd = MFI_CMD_DCMD;
5028         dcmd->cmd_status = 0x0;
5029         dcmd->sge_count = 1;
5030         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5031         dcmd->timeout = 0;
5032         dcmd->pad_0 = 0;
5033         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5034         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_GET_INFO);
5035         dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(el_info_h);
5036         dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_log_info));
5037 
5038         if (megasas_issue_blocked_cmd(instance, cmd, 30))
5039                 dev_err(&instance->pdev->dev, "Command timedout"
5040                         "from %s\n", __func__);
5041         else {
5042                 /*
5043                  * Copy the data back into callers buffer
5044                  */
5045                 eli->newest_seq_num = el_info->newest_seq_num;
5046                 eli->oldest_seq_num = el_info->oldest_seq_num;
5047                 eli->clear_seq_num = el_info->clear_seq_num;
5048                 eli->shutdown_seq_num = el_info->shutdown_seq_num;
5049                 eli->boot_seq_num = el_info->boot_seq_num;
5050         }
5051 
5052         pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info),
5053                             el_info, el_info_h);
5054 
5055         megasas_return_cmd(instance, cmd);
5056 
5057         return 0;
5058 }
5059 
5060 /**
5061  * megasas_register_aen -       Registers for asynchronous event notification
5062  * @instance:                   Adapter soft state
5063  * @seq_num:                    The starting sequence number
5064  * @class_locale:               Class of the event
5065  *
5066  * This function subscribes for AEN for events beyond the @seq_num. It requests
5067  * to be notified if and only if the event is of type @class_locale
5068  */
5069 static int
5070 megasas_register_aen(struct megasas_instance *instance, u32 seq_num,
5071                      u32 class_locale_word)
5072 {
5073         int ret_val;
5074         struct megasas_cmd *cmd;
5075         struct megasas_dcmd_frame *dcmd;
5076         union megasas_evt_class_locale curr_aen;
5077         union megasas_evt_class_locale prev_aen;
5078 
5079         /*
5080          * If there an AEN pending already (aen_cmd), check if the
5081          * class_locale of that pending AEN is inclusive of the new
5082          * AEN request we currently have. If it is, then we don't have
5083          * to do anything. In other words, whichever events the current
5084          * AEN request is subscribing to, have already been subscribed
5085          * to.
5086          *
5087          * If the old_cmd is _not_ inclusive, then we have to abort
5088          * that command, form a class_locale that is superset of both
5089          * old and current and re-issue to the FW
5090          */
5091 
5092         curr_aen.word = class_locale_word;
5093 
5094         if (instance->aen_cmd) {
5095 
5096                 prev_aen.word =
5097                         le32_to_cpu(instance->aen_cmd->frame->dcmd.mbox.w[1]);
5098 
5099                 /*
5100                  * A class whose enum value is smaller is inclusive of all
5101                  * higher values. If a PROGRESS (= -1) was previously
5102                  * registered, then a new registration requests for higher
5103                  * classes need not be sent to FW. They are automatically
5104                  * included.
5105                  *
5106                  * Locale numbers don't have such hierarchy. They are bitmap
5107                  * values
5108                  */
5109                 if ((prev_aen.members.class <= curr_aen.members.class) &&
5110                     !((prev_aen.members.locale & curr_aen.members.locale) ^
5111                       curr_aen.members.locale)) {
5112                         /*
5113                          * Previously issued event registration includes
5114                          * current request. Nothing to do.
5115                          */
5116                         return 0;
5117                 } else {
5118                         curr_aen.members.locale |= prev_aen.members.locale;
5119 
5120                         if (prev_aen.members.class < curr_aen.members.class)
5121                                 curr_aen.members.class = prev_aen.members.class;
5122 
5123                         instance->aen_cmd->abort_aen = 1;
5124                         ret_val = megasas_issue_blocked_abort_cmd(instance,
5125                                                                   instance->
5126                                                                   aen_cmd, 30);
5127 
5128                         if (ret_val) {
5129                                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to abort "
5130                                        "previous AEN command\n");
5131                                 return ret_val;
5132                         }
5133                 }
5134         }
5135 
5136         cmd = megasas_get_cmd(instance);
5137 
5138         if (!cmd)
5139                 return -ENOMEM;
5140 
5141         dcmd = &cmd->frame->dcmd;
5142 
5143         memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail));
5144 
5145         /*
5146          * Prepare DCMD for aen registration
5147          */
5148         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5149 
5150         dcmd->cmd = MFI_CMD_DCMD;
5151         dcmd->cmd_status = 0x0;
5152         dcmd->sge_count = 1;
5153         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
5154         dcmd->timeout = 0;
5155         dcmd->pad_0 = 0;
5156         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct megasas_evt_detail));
5157         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_EVENT_WAIT);
5158         dcmd->mbox.w[0] = cpu_to_le32(seq_num);
5159         instance->last_seq_num = seq_num;
5160         dcmd->mbox.w[1] = cpu_to_le32(curr_aen.word);
5161         dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(instance->evt_detail_h);
5162         dcmd->sgl.sge32[0].length = cpu_to_le32(sizeof(struct megasas_evt_detail));
5163 
5164         if (instance->aen_cmd != NULL) {
5165                 megasas_return_cmd(instance, cmd);
5166                 return 0;
5167         }
5168 
5169         /*
5170          * Store reference to the cmd used to register for AEN. When an
5171          * application wants us to register for AEN, we have to abort this
5172          * cmd and re-register with a new EVENT LOCALE supplied by that app
5173          */
5174         instance->aen_cmd = cmd;
5175 
5176         /*
5177          * Issue the aen registration frame
5178          */
5179         instance->instancet->issue_dcmd(instance, cmd);
5180 
5181         return 0;
5182 }
5183 
5184 /**
5185  * megasas_start_aen -  Subscribes to AEN during driver load time
5186  * @instance:           Adapter soft state
5187  */
5188 static int megasas_start_aen(struct megasas_instance *instance)
5189 {
5190         struct megasas_evt_log_info eli;
5191         union megasas_evt_class_locale class_locale;
5192 
5193         /*
5194          * Get the latest sequence number from FW
5195          */
5196         memset(&eli, 0, sizeof(eli));
5197 
5198         if (megasas_get_seq_num(instance, &eli))
5199                 return -1;
5200 
5201         /*
5202          * Register AEN with FW for latest sequence number plus 1
5203          */
5204         class_locale.members.reserved = 0;
5205         class_locale.members.locale = MR_EVT_LOCALE_ALL;
5206         class_locale.members.class = MR_EVT_CLASS_DEBUG;
5207 
5208         return megasas_register_aen(instance,
5209                         le32_to_cpu(eli.newest_seq_num) + 1,
5210                         class_locale.word);
5211 }
5212 
5213 /**
5214  * megasas_io_attach -  Attaches this driver to SCSI mid-layer
5215  * @instance:           Adapter soft state
5216  */
5217 static int megasas_io_attach(struct megasas_instance *instance)
5218 {
5219         struct Scsi_Host *host = instance->host;
5220 
5221         /*
5222          * Export parameters required by SCSI mid-layer
5223          */
5224         host->irq = instance->pdev->irq;
5225         host->unique_id = instance->unique_id;
5226         host->can_queue = instance->max_scsi_cmds;
5227         host->this_id = instance->init_id;
5228         host->sg_tablesize = instance->max_num_sge;
5229 
5230         if (instance->fw_support_ieee)
5231                 instance->max_sectors_per_req = MEGASAS_MAX_SECTORS_IEEE;
5232 
5233         /*
5234          * Check if the module parameter value for max_sectors can be used
5235          */
5236         if (max_sectors && max_sectors < instance->max_sectors_per_req)
5237                 instance->max_sectors_per_req = max_sectors;
5238         else {
5239                 if (max_sectors) {
5240                         if (((instance->pdev->device ==
5241                                 PCI_DEVICE_ID_LSI_SAS1078GEN2) ||
5242                                 (instance->pdev->device ==
5243                                 PCI_DEVICE_ID_LSI_SAS0079GEN2)) &&
5244                                 (max_sectors <= MEGASAS_MAX_SECTORS)) {
5245                                 instance->max_sectors_per_req = max_sectors;
5246                         } else {
5247                         dev_info(&instance->pdev->dev, "max_sectors should be > 0"
5248                                 "and <= %d (or < 1MB for GEN2 controller)\n",
5249                                 instance->max_sectors_per_req);
5250                         }
5251                 }
5252         }
5253 
5254         host->max_sectors = instance->max_sectors_per_req;
5255         host->cmd_per_lun = MEGASAS_DEFAULT_CMD_PER_LUN;
5256         host->max_channel = MEGASAS_MAX_CHANNELS - 1;
5257         host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL;
5258         host->max_lun = MEGASAS_MAX_LUN;
5259         host->max_cmd_len = 16;
5260 
5261         /* Fusion only supports host reset */
5262         if (instance->ctrl_context) {
5263                 host->hostt->eh_device_reset_handler = NULL;
5264                 host->hostt->eh_bus_reset_handler = NULL;
5265         }
5266 
5267         /*
5268          * Notify the mid-layer about the new controller
5269          */
5270         if (scsi_add_host(host, &instance->pdev->dev)) {
5271                 dev_err(&instance->pdev->dev,
5272                         "Failed to add host from %s %d\n",
5273                         __func__, __LINE__);
5274                 return -ENODEV;
5275         }
5276 
5277         return 0;
5278 }
5279 
5280 static int
5281 megasas_set_dma_mask(struct pci_dev *pdev)
5282 {
5283         /*
5284          * All our controllers are capable of performing 64-bit DMA
5285          */
5286         if (IS_DMA64) {
5287                 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) {
5288 
5289                         if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5290                                 goto fail_set_dma_mask;
5291                 }
5292         } else {
5293                 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0)
5294                         goto fail_set_dma_mask;
5295         }
5296         /*
5297          * Ensure that all data structures are allocated in 32-bit
5298          * memory.
5299          */
5300         if (pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) {
5301                 /* Try 32bit DMA mask and 32 bit Consistent dma mask */
5302                 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))
5303                         && !pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32)))
5304                         dev_info(&pdev->dev, "set 32bit DMA mask"
5305                                 "and 32 bit consistent mask\n");
5306                 else
5307                         goto fail_set_dma_mask;
5308         }
5309 
5310         return 0;
5311 
5312 fail_set_dma_mask:
5313         return 1;
5314 }
5315 
5316 /**
5317  * megasas_probe_one -  PCI hotplug entry point
5318  * @pdev:               PCI device structure
5319  * @id:                 PCI ids of supported hotplugged adapter
5320  */
5321 static int megasas_probe_one(struct pci_dev *pdev,
5322                              const struct pci_device_id *id)
5323 {
5324         int rval, pos;
5325         struct Scsi_Host *host;
5326         struct megasas_instance *instance;
5327         u16 control = 0;
5328         struct fusion_context *fusion = NULL;
5329 
5330         /* Reset MSI-X in the kdump kernel */
5331         if (reset_devices) {
5332                 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
5333                 if (pos) {
5334                         pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS,
5335                                              &control);
5336                         if (control & PCI_MSIX_FLAGS_ENABLE) {
5337                                 dev_info(&pdev->dev, "resetting MSI-X\n");
5338                                 pci_write_config_word(pdev,
5339                                                       pos + PCI_MSIX_FLAGS,
5340                                                       control &
5341                                                       ~PCI_MSIX_FLAGS_ENABLE);
5342                         }
5343                 }
5344         }
5345 
5346         /*
5347          * PCI prepping: enable device set bus mastering and dma mask
5348          */
5349         rval = pci_enable_device_mem(pdev);
5350 
5351         if (rval) {
5352                 return rval;
5353         }
5354 
5355         pci_set_master(pdev);
5356 
5357         if (megasas_set_dma_mask(pdev))
5358                 goto fail_set_dma_mask;
5359 
5360         host = scsi_host_alloc(&megasas_template,
5361                                sizeof(struct megasas_instance));
5362 
5363         if (!host) {
5364                 dev_printk(KERN_DEBUG, &pdev->dev, "scsi_host_alloc failed\n");
5365                 goto fail_alloc_instance;
5366         }
5367 
5368         instance = (struct megasas_instance *)host->hostdata;
5369         memset(instance, 0, sizeof(*instance));
5370         atomic_set(&instance->fw_reset_no_pci_access, 0);
5371         instance->pdev = pdev;
5372 
5373         switch (instance->pdev->device) {
5374         case PCI_DEVICE_ID_LSI_FUSION:
5375         case PCI_DEVICE_ID_LSI_PLASMA:
5376         case PCI_DEVICE_ID_LSI_INVADER:
5377         case PCI_DEVICE_ID_LSI_FURY:
5378         case PCI_DEVICE_ID_LSI_INTRUDER:
5379         case PCI_DEVICE_ID_LSI_INTRUDER_24:
5380         case PCI_DEVICE_ID_LSI_CUTLASS_52:
5381         case PCI_DEVICE_ID_LSI_CUTLASS_53:
5382         {
5383                 instance->ctrl_context_pages =
5384                         get_order(sizeof(struct fusion_context));
5385                 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL,
5386                                 instance->ctrl_context_pages);
5387                 if (!instance->ctrl_context) {
5388                         dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate "
5389                                "memory for Fusion context info\n");
5390                         goto fail_alloc_dma_buf;
5391                 }
5392                 fusion = instance->ctrl_context;
5393                 memset(fusion, 0,
5394                         ((1 << PAGE_SHIFT) << instance->ctrl_context_pages));
5395                 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_FUSION) ||
5396                         (instance->pdev->device == PCI_DEVICE_ID_LSI_PLASMA))
5397                         fusion->adapter_type = THUNDERBOLT_SERIES;
5398                 else
5399                         fusion->adapter_type = INVADER_SERIES;
5400         }
5401         break;
5402         default: /* For all other supported controllers */
5403 
5404                 instance->producer =
5405                         pci_alloc_consistent(pdev, sizeof(u32),
5406                                              &instance->producer_h);
5407                 instance->consumer =
5408                         pci_alloc_consistent(pdev, sizeof(u32),
5409                                              &instance->consumer_h);
5410 
5411                 if (!instance->producer || !instance->consumer) {
5412                         dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate"
5413                                "memory for producer, consumer\n");
5414                         goto fail_alloc_dma_buf;
5415                 }
5416 
5417                 *instance->producer = 0;
5418                 *instance->consumer = 0;
5419                 break;
5420         }
5421 
5422         instance->system_info_buf = pci_zalloc_consistent(pdev,
5423                                         sizeof(struct MR_DRV_SYSTEM_INFO),
5424                                         &instance->system_info_h);
5425 
5426         if (!instance->system_info_buf)
5427                 dev_info(&instance->pdev->dev, "Can't allocate system info buffer\n");
5428 
5429         /* Crash dump feature related initialisation*/
5430         instance->drv_buf_index = 0;
5431         instance->drv_buf_alloc = 0;
5432         instance->crash_dump_fw_support = 0;
5433         instance->crash_dump_app_support = 0;
5434         instance->fw_crash_state = UNAVAILABLE;
5435         spin_lock_init(&instance->crashdump_lock);
5436         instance->crash_dump_buf = NULL;
5437 
5438         if (!reset_devices)
5439                 instance->crash_dump_buf = pci_alloc_consistent(pdev,
5440                                                 CRASH_DMA_BUF_SIZE,
5441                                                 &instance->crash_dump_h);
5442         if (!instance->crash_dump_buf)
5443                 dev_err(&pdev->dev, "Can't allocate Firmware "
5444                         "crash dump DMA buffer\n");
5445 
5446         megasas_poll_wait_aen = 0;
5447         instance->flag_ieee = 0;
5448         instance->ev = NULL;
5449         instance->issuepend_done = 1;
5450         instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
5451         instance->is_imr = 0;
5452 
5453         instance->evt_detail = pci_alloc_consistent(pdev,
5454                                                     sizeof(struct
5455                                                            megasas_evt_detail),
5456                                                     &instance->evt_detail_h);
5457 
5458         if (!instance->evt_detail) {
5459                 dev_printk(KERN_DEBUG, &pdev->dev, "Failed to allocate memory for "
5460                        "event detail structure\n");
5461                 goto fail_alloc_dma_buf;
5462         }
5463 
5464         /*
5465          * Initialize locks and queues
5466          */
5467         INIT_LIST_HEAD(&instance->cmd_pool);
5468         INIT_LIST_HEAD(&instance->internal_reset_pending_q);
5469 
5470         atomic_set(&instance->fw_outstanding,0);
5471 
5472         init_waitqueue_head(&instance->int_cmd_wait_q);
5473         init_waitqueue_head(&instance->abort_cmd_wait_q);
5474 
5475         spin_lock_init(&instance->mfi_pool_lock);
5476         spin_lock_init(&instance->hba_lock);
5477         spin_lock_init(&instance->completion_lock);
5478 
5479         mutex_init(&instance->aen_mutex);
5480         mutex_init(&instance->reset_mutex);
5481 
5482         /*
5483          * Initialize PCI related and misc parameters
5484          */
5485         instance->host = host;
5486         instance->unique_id = pdev->bus->number << 8 | pdev->devfn;
5487         instance->init_id = MEGASAS_DEFAULT_INIT_ID;
5488         instance->ctrl_info = NULL;
5489 
5490 
5491         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
5492                 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY))
5493                 instance->flag_ieee = 1;
5494 
5495         megasas_dbg_lvl = 0;
5496         instance->flag = 0;
5497         instance->unload = 1;
5498         instance->last_time = 0;
5499         instance->disableOnlineCtrlReset = 1;
5500         instance->UnevenSpanSupport = 0;
5501 
5502         if (instance->ctrl_context) {
5503                 INIT_WORK(&instance->work_init, megasas_fusion_ocr_wq);
5504                 INIT_WORK(&instance->crash_init, megasas_fusion_crash_dump_wq);
5505         } else
5506                 INIT_WORK(&instance->work_init, process_fw_state_change_wq);
5507 
5508         /*
5509          * Initialize MFI Firmware
5510          */
5511         if (megasas_init_fw(instance))
5512                 goto fail_init_mfi;
5513 
5514         if (instance->requestorId) {
5515                 if (instance->PlasmaFW111) {
5516                         instance->vf_affiliation_111 =
5517                                 pci_alloc_consistent(pdev, sizeof(struct MR_LD_VF_AFFILIATION_111),
5518                                                      &instance->vf_affiliation_111_h);
5519                         if (!instance->vf_affiliation_111)
5520                                 dev_warn(&pdev->dev, "Can't allocate "
5521                                        "memory for VF affiliation buffer\n");
5522                 } else {
5523                         instance->vf_affiliation =
5524                                 pci_alloc_consistent(pdev,
5525                                                      (MAX_LOGICAL_DRIVES + 1) *
5526                                                      sizeof(struct MR_LD_VF_AFFILIATION),
5527                                                      &instance->vf_affiliation_h);
5528                         if (!instance->vf_affiliation)
5529                                 dev_warn(&pdev->dev, "Can't allocate "
5530                                        "memory for VF affiliation buffer\n");
5531                 }
5532         }
5533 
5534         /*
5535          * Store instance in PCI softstate
5536          */
5537         pci_set_drvdata(pdev, instance);
5538 
5539         /*
5540          * Add this controller to megasas_mgmt_info structure so that it
5541          * can be exported to management applications
5542          */
5543         megasas_mgmt_info.count++;
5544         megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance;
5545         megasas_mgmt_info.max_index++;
5546 
5547         /*
5548          * Register with SCSI mid-layer
5549          */
5550         if (megasas_io_attach(instance))
5551                 goto fail_io_attach;
5552 
5553         instance->unload = 0;
5554         /*
5555          * Trigger SCSI to scan our drives
5556          */
5557         scsi_scan_host(host);
5558 
5559         /*
5560          * Initiate AEN (Asynchronous Event Notification)
5561          */
5562         if (megasas_start_aen(instance)) {
5563                 dev_printk(KERN_DEBUG, &pdev->dev, "start aen failed\n");
5564                 goto fail_start_aen;
5565         }
5566 
5567         /* Get current SR-IOV LD/VF affiliation */
5568         if (instance->requestorId)
5569                 megasas_get_ld_vf_affiliation(instance, 1);
5570 
5571         return 0;
5572 
5573 fail_start_aen:
5574 fail_io_attach:
5575         megasas_mgmt_info.count--;
5576         megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL;
5577         megasas_mgmt_info.max_index--;
5578 
5579         instance->instancet->disable_intr(instance);
5580         megasas_destroy_irqs(instance);
5581 
5582         if (instance->ctrl_context)
5583                 megasas_release_fusion(instance);
5584         else
5585                 megasas_release_mfi(instance);
5586         if (instance->msix_vectors)
5587                 pci_disable_msix(instance->pdev);
5588 fail_init_mfi:
5589 fail_alloc_dma_buf:
5590         if (instance->evt_detail)
5591                 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5592                                     instance->evt_detail,
5593                                     instance->evt_detail_h);
5594 
5595         if (instance->producer)
5596                 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5597                                     instance->producer_h);
5598         if (instance->consumer)
5599                 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5600                                     instance->consumer_h);
5601         scsi_host_put(host);
5602 
5603 fail_alloc_instance:
5604 fail_set_dma_mask:
5605         pci_disable_device(pdev);
5606 
5607         return -ENODEV;
5608 }
5609 
5610 /**
5611  * megasas_flush_cache -        Requests FW to flush all its caches
5612  * @instance:                   Adapter soft state
5613  */
5614 static void megasas_flush_cache(struct megasas_instance *instance)
5615 {
5616         struct megasas_cmd *cmd;
5617         struct megasas_dcmd_frame *dcmd;
5618 
5619         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5620                 return;
5621 
5622         cmd = megasas_get_cmd(instance);
5623 
5624         if (!cmd)
5625                 return;
5626 
5627         dcmd = &cmd->frame->dcmd;
5628 
5629         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5630 
5631         dcmd->cmd = MFI_CMD_DCMD;
5632         dcmd->cmd_status = 0x0;
5633         dcmd->sge_count = 0;
5634         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
5635         dcmd->timeout = 0;
5636         dcmd->pad_0 = 0;
5637         dcmd->data_xfer_len = 0;
5638         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_CACHE_FLUSH);
5639         dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE;
5640 
5641         if (megasas_issue_blocked_cmd(instance, cmd, 30))
5642                 dev_err(&instance->pdev->dev, "Command timedout"
5643                         " from %s\n", __func__);
5644 
5645         megasas_return_cmd(instance, cmd);
5646 }
5647 
5648 /**
5649  * megasas_shutdown_controller -        Instructs FW to shutdown the controller
5650  * @instance:                           Adapter soft state
5651  * @opcode:                             Shutdown/Hibernate
5652  */
5653 static void megasas_shutdown_controller(struct megasas_instance *instance,
5654                                         u32 opcode)
5655 {
5656         struct megasas_cmd *cmd;
5657         struct megasas_dcmd_frame *dcmd;
5658 
5659         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
5660                 return;
5661 
5662         cmd = megasas_get_cmd(instance);
5663 
5664         if (!cmd)
5665                 return;
5666 
5667         if (instance->aen_cmd)
5668                 megasas_issue_blocked_abort_cmd(instance,
5669                         instance->aen_cmd, MEGASAS_BLOCKED_CMD_TIMEOUT);
5670         if (instance->map_update_cmd)
5671                 megasas_issue_blocked_abort_cmd(instance,
5672                         instance->map_update_cmd, MEGASAS_BLOCKED_CMD_TIMEOUT);
5673         if (instance->jbod_seq_cmd)
5674                 megasas_issue_blocked_abort_cmd(instance,
5675                         instance->jbod_seq_cmd, MEGASAS_BLOCKED_CMD_TIMEOUT);
5676 
5677         dcmd = &cmd->frame->dcmd;
5678 
5679         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
5680 
5681         dcmd->cmd = MFI_CMD_DCMD;
5682         dcmd->cmd_status = 0x0;
5683         dcmd->sge_count = 0;
5684         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_NONE);
5685         dcmd->timeout = 0;
5686         dcmd->pad_0 = 0;
5687         dcmd->data_xfer_len = 0;
5688         dcmd->opcode = cpu_to_le32(opcode);
5689 
5690         if (megasas_issue_blocked_cmd(instance, cmd, 30))
5691                 dev_err(&instance->pdev->dev, "Command timedout"
5692                         "from %s\n", __func__);
5693 
5694         megasas_return_cmd(instance, cmd);
5695 }
5696 
5697 #ifdef CONFIG_PM
5698 /**
5699  * megasas_suspend -    driver suspend entry point
5700  * @pdev:               PCI device structure
5701  * @state:              PCI power state to suspend routine
5702  */
5703 static int
5704 megasas_suspend(struct pci_dev *pdev, pm_message_t state)
5705 {
5706         struct Scsi_Host *host;
5707         struct megasas_instance *instance;
5708 
5709         instance = pci_get_drvdata(pdev);
5710         host = instance->host;
5711         instance->unload = 1;
5712 
5713         /* Shutdown SR-IOV heartbeat timer */
5714         if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5715                 del_timer_sync(&instance->sriov_heartbeat_timer);
5716 
5717         megasas_flush_cache(instance);
5718         megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN);
5719 
5720         /* cancel the delayed work if this work still in queue */
5721         if (instance->ev != NULL) {
5722                 struct megasas_aen_event *ev = instance->ev;
5723                 cancel_delayed_work_sync(&ev->hotplug_work);
5724                 instance->ev = NULL;
5725         }
5726 
5727         tasklet_kill(&instance->isr_tasklet);
5728 
5729         pci_set_drvdata(instance->pdev, instance);
5730         instance->instancet->disable_intr(instance);
5731 
5732         megasas_destroy_irqs(instance);
5733 
5734         if (instance->msix_vectors)
5735                 pci_disable_msix(instance->pdev);
5736 
5737         pci_save_state(pdev);
5738         pci_disable_device(pdev);
5739 
5740         pci_set_power_state(pdev, pci_choose_state(pdev, state));
5741 
5742         return 0;
5743 }
5744 
5745 /**
5746  * megasas_resume-      driver resume entry point
5747  * @pdev:               PCI device structure
5748  */
5749 static int
5750 megasas_resume(struct pci_dev *pdev)
5751 {
5752         int rval;
5753         struct Scsi_Host *host;
5754         struct megasas_instance *instance;
5755 
5756         instance = pci_get_drvdata(pdev);
5757         host = instance->host;
5758         pci_set_power_state(pdev, PCI_D0);
5759         pci_enable_wake(pdev, PCI_D0, 0);
5760         pci_restore_state(pdev);
5761 
5762         /*
5763          * PCI prepping: enable device set bus mastering and dma mask
5764          */
5765         rval = pci_enable_device_mem(pdev);
5766 
5767         if (rval) {
5768                 dev_err(&pdev->dev, "Enable device failed\n");
5769                 return rval;
5770         }
5771 
5772         pci_set_master(pdev);
5773 
5774         if (megasas_set_dma_mask(pdev))
5775                 goto fail_set_dma_mask;
5776 
5777         /*
5778          * Initialize MFI Firmware
5779          */
5780 
5781         atomic_set(&instance->fw_outstanding, 0);
5782 
5783         /*
5784          * We expect the FW state to be READY
5785          */
5786         if (megasas_transition_to_ready(instance, 0))
5787                 goto fail_ready_state;
5788 
5789         /* Now re-enable MSI-X */
5790         if (instance->msix_vectors &&
5791             pci_enable_msix_exact(instance->pdev, instance->msixentry,
5792                                   instance->msix_vectors))
5793                 goto fail_reenable_msix;
5794 
5795         if (instance->ctrl_context) {
5796                 megasas_reset_reply_desc(instance);
5797                 if (megasas_ioc_init_fusion(instance)) {
5798                         megasas_free_cmds(instance);
5799                         megasas_free_cmds_fusion(instance);
5800                         goto fail_init_mfi;
5801                 }
5802                 if (!megasas_get_map_info(instance))
5803                         megasas_sync_map_info(instance);
5804         } else {
5805                 *instance->producer = 0;
5806                 *instance->consumer = 0;
5807                 if (megasas_issue_init_mfi(instance))
5808                         goto fail_init_mfi;
5809         }
5810 
5811         tasklet_init(&instance->isr_tasklet, instance->instancet->tasklet,
5812                      (unsigned long)instance);
5813 
5814         if (instance->msix_vectors ?
5815                         megasas_setup_irqs_msix(instance, 0) :
5816                         megasas_setup_irqs_ioapic(instance))
5817                 goto fail_init_mfi;
5818 
5819         /* Re-launch SR-IOV heartbeat timer */
5820         if (instance->requestorId) {
5821                 if (!megasas_sriov_start_heartbeat(instance, 0))
5822                         megasas_start_timer(instance,
5823                                             &instance->sriov_heartbeat_timer,
5824                                             megasas_sriov_heartbeat_handler,
5825                                             MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
5826                 else {
5827                         instance->skip_heartbeat_timer_del = 1;
5828                         goto fail_init_mfi;
5829                 }
5830         }
5831 
5832         instance->instancet->enable_intr(instance);
5833         megasas_setup_jbod_map(instance);
5834         instance->unload = 0;
5835 
5836         /*
5837          * Initiate AEN (Asynchronous Event Notification)
5838          */
5839         if (megasas_start_aen(instance))
5840                 dev_err(&instance->pdev->dev, "Start AEN failed\n");
5841 
5842         return 0;
5843 
5844 fail_init_mfi:
5845         if (instance->evt_detail)
5846                 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5847                                 instance->evt_detail,
5848                                 instance->evt_detail_h);
5849 
5850         if (instance->producer)
5851                 pci_free_consistent(pdev, sizeof(u32), instance->producer,
5852                                 instance->producer_h);
5853         if (instance->consumer)
5854                 pci_free_consistent(pdev, sizeof(u32), instance->consumer,
5855                                 instance->consumer_h);
5856         scsi_host_put(host);
5857 
5858 fail_set_dma_mask:
5859 fail_ready_state:
5860 fail_reenable_msix:
5861 
5862         pci_disable_device(pdev);
5863 
5864         return -ENODEV;
5865 }
5866 #else
5867 #define megasas_suspend NULL
5868 #define megasas_resume  NULL
5869 #endif
5870 
5871 /**
5872  * megasas_detach_one - PCI hot"un"plug entry point
5873  * @pdev:               PCI device structure
5874  */
5875 static void megasas_detach_one(struct pci_dev *pdev)
5876 {
5877         int i;
5878         struct Scsi_Host *host;
5879         struct megasas_instance *instance;
5880         struct fusion_context *fusion;
5881         u32 pd_seq_map_sz;
5882 
5883         instance = pci_get_drvdata(pdev);
5884         instance->unload = 1;
5885         host = instance->host;
5886         fusion = instance->ctrl_context;
5887 
5888         /* Shutdown SR-IOV heartbeat timer */
5889         if (instance->requestorId && !instance->skip_heartbeat_timer_del)
5890                 del_timer_sync(&instance->sriov_heartbeat_timer);
5891 
5892         if (instance->fw_crash_state != UNAVAILABLE)
5893                 megasas_free_host_crash_buffer(instance);
5894         scsi_remove_host(instance->host);
5895         megasas_flush_cache(instance);
5896         megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
5897 
5898         /* cancel the delayed work if this work still in queue*/
5899         if (instance->ev != NULL) {
5900                 struct megasas_aen_event *ev = instance->ev;
5901                 cancel_delayed_work_sync(&ev->hotplug_work);
5902                 instance->ev = NULL;
5903         }
5904 
5905         /* cancel all wait events */
5906         wake_up_all(&instance->int_cmd_wait_q);
5907 
5908         tasklet_kill(&instance->isr_tasklet);
5909 
5910         /*
5911          * Take the instance off the instance array. Note that we will not
5912          * decrement the max_index. We let this array be sparse array
5913          */
5914         for (i = 0; i < megasas_mgmt_info.max_index; i++) {
5915                 if (megasas_mgmt_info.instance[i] == instance) {
5916                         megasas_mgmt_info.count--;
5917                         megasas_mgmt_info.instance[i] = NULL;
5918 
5919                         break;
5920                 }
5921         }
5922 
5923         instance->instancet->disable_intr(instance);
5924 
5925         megasas_destroy_irqs(instance);
5926 
5927         if (instance->msix_vectors)
5928                 pci_disable_msix(instance->pdev);
5929 
5930         if (instance->ctrl_context) {
5931                 megasas_release_fusion(instance);
5932                         pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
5933                                 (sizeof(struct MR_PD_CFG_SEQ) *
5934                                         (MAX_PHYSICAL_DEVICES - 1));
5935                 for (i = 0; i < 2 ; i++) {
5936                         if (fusion->ld_map[i])
5937                                 dma_free_coherent(&instance->pdev->dev,
5938                                                   fusion->max_map_sz,
5939                                                   fusion->ld_map[i],
5940                                                   fusion->ld_map_phys[i]);
5941                         if (fusion->ld_drv_map[i])
5942                                 free_pages((ulong)fusion->ld_drv_map[i],
5943                                         fusion->drv_map_pages);
5944                                 if (fusion->pd_seq_sync)
5945                                         dma_free_coherent(&instance->pdev->dev,
5946                                                 pd_seq_map_sz,
5947                                                 fusion->pd_seq_sync[i],
5948                                                 fusion->pd_seq_phys[i]);
5949                 }
5950                 free_pages((ulong)instance->ctrl_context,
5951                         instance->ctrl_context_pages);
5952         } else {
5953                 megasas_release_mfi(instance);
5954                 pci_free_consistent(pdev, sizeof(u32),
5955                                     instance->producer,
5956                                     instance->producer_h);
5957                 pci_free_consistent(pdev, sizeof(u32),
5958                                     instance->consumer,
5959                                     instance->consumer_h);
5960         }
5961 
5962         kfree(instance->ctrl_info);
5963 
5964         if (instance->evt_detail)
5965                 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail),
5966                                 instance->evt_detail, instance->evt_detail_h);
5967 
5968         if (instance->vf_affiliation)
5969                 pci_free_consistent(pdev, (MAX_LOGICAL_DRIVES + 1) *
5970                                     sizeof(struct MR_LD_VF_AFFILIATION),
5971                                     instance->vf_affiliation,
5972                                     instance->vf_affiliation_h);
5973 
5974         if (instance->vf_affiliation_111)
5975                 pci_free_consistent(pdev,
5976                                     sizeof(struct MR_LD_VF_AFFILIATION_111),
5977                                     instance->vf_affiliation_111,
5978                                     instance->vf_affiliation_111_h);
5979 
5980         if (instance->hb_host_mem)
5981                 pci_free_consistent(pdev, sizeof(struct MR_CTRL_HB_HOST_MEM),
5982                                     instance->hb_host_mem,
5983                                     instance->hb_host_mem_h);
5984 
5985         if (instance->crash_dump_buf)
5986                 pci_free_consistent(pdev, CRASH_DMA_BUF_SIZE,
5987                             instance->crash_dump_buf, instance->crash_dump_h);
5988 
5989         if (instance->system_info_buf)
5990                 pci_free_consistent(pdev, sizeof(struct MR_DRV_SYSTEM_INFO),
5991                                     instance->system_info_buf, instance->system_info_h);
5992 
5993         scsi_host_put(host);
5994 
5995         pci_disable_device(pdev);
5996 }
5997 
5998 /**
5999  * megasas_shutdown -   Shutdown entry point
6000  * @device:             Generic device structure
6001  */
6002 static void megasas_shutdown(struct pci_dev *pdev)
6003 {
6004         struct megasas_instance *instance = pci_get_drvdata(pdev);
6005 
6006         instance->unload = 1;
6007         megasas_flush_cache(instance);
6008         megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN);
6009         instance->instancet->disable_intr(instance);
6010         megasas_destroy_irqs(instance);
6011 
6012         if (instance->msix_vectors)
6013                 pci_disable_msix(instance->pdev);
6014 }
6015 
6016 /**
6017  * megasas_mgmt_open -  char node "open" entry point
6018  */
6019 static int megasas_mgmt_open(struct inode *inode, struct file *filep)
6020 {
6021         /*
6022          * Allow only those users with admin rights
6023          */
6024         if (!capable(CAP_SYS_ADMIN))
6025                 return -EACCES;
6026 
6027         return 0;
6028 }
6029 
6030 /**
6031  * megasas_mgmt_fasync -        Async notifier registration from applications
6032  *
6033  * This function adds the calling process to a driver global queue. When an
6034  * event occurs, SIGIO will be sent to all processes in this queue.
6035  */
6036 static int megasas_mgmt_fasync(int fd, struct file *filep, int mode)
6037 {
6038         int rc;
6039 
6040         mutex_lock(&megasas_async_queue_mutex);
6041 
6042         rc = fasync_helper(fd, filep, mode, &megasas_async_queue);
6043 
6044         mutex_unlock(&megasas_async_queue_mutex);
6045 
6046         if (rc >= 0) {
6047                 /* For sanity check when we get ioctl */
6048                 filep->private_data = filep;
6049                 return 0;
6050         }
6051 
6052         printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc);
6053 
6054         return rc;
6055 }
6056 
6057 /**
6058  * megasas_mgmt_poll -  char node "poll" entry point
6059  * */
6060 static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait)
6061 {
6062         unsigned int mask;
6063         unsigned long flags;
6064 
6065         poll_wait(file, &megasas_poll_wait, wait);
6066         spin_lock_irqsave(&poll_aen_lock, flags);
6067         if (megasas_poll_wait_aen)
6068                 mask = (POLLIN | POLLRDNORM);
6069         else
6070                 mask = 0;
6071         megasas_poll_wait_aen = 0;
6072         spin_unlock_irqrestore(&poll_aen_lock, flags);
6073         return mask;
6074 }
6075 
6076 /*
6077  * megasas_set_crash_dump_params_ioctl:
6078  *              Send CRASH_DUMP_MODE DCMD to all controllers
6079  * @cmd:        MFI command frame
6080  */
6081 
6082 static int megasas_set_crash_dump_params_ioctl(struct megasas_cmd *cmd)
6083 {
6084         struct megasas_instance *local_instance;
6085         int i, error = 0;
6086         int crash_support;
6087 
6088         crash_support = cmd->frame->dcmd.mbox.w[0];
6089 
6090         for (i = 0; i < megasas_mgmt_info.max_index; i++) {
6091                 local_instance = megasas_mgmt_info.instance[i];
6092                 if (local_instance && local_instance->crash_dump_drv_support) {
6093                         if ((local_instance->adprecovery ==
6094                                 MEGASAS_HBA_OPERATIONAL) &&
6095                                 !megasas_set_crash_dump_params(local_instance,
6096                                         crash_support)) {
6097                                 local_instance->crash_dump_app_support =
6098                                         crash_support;
6099                                 dev_info(&local_instance->pdev->dev,
6100                                         "Application firmware crash "
6101                                         "dump mode set success\n");
6102                                 error = 0;
6103                         } else {
6104                                 dev_info(&local_instance->pdev->dev,
6105                                         "Application firmware crash "
6106                                         "dump mode set failed\n");
6107                                 error = -1;
6108                         }
6109                 }
6110         }
6111         return error;
6112 }
6113 
6114 /**
6115  * megasas_mgmt_fw_ioctl -      Issues management ioctls to FW
6116  * @instance:                   Adapter soft state
6117  * @argp:                       User's ioctl packet
6118  */
6119 static int
6120 megasas_mgmt_fw_ioctl(struct megasas_instance *instance,
6121                       struct megasas_iocpacket __user * user_ioc,
6122                       struct megasas_iocpacket *ioc)
6123 {
6124         struct megasas_sge32 *kern_sge32;
6125         struct megasas_cmd *cmd;
6126         void *kbuff_arr[MAX_IOCTL_SGE];
6127         dma_addr_t buf_handle = 0;
6128         int error = 0, i;
6129         void *sense = NULL;
6130         dma_addr_t sense_handle;
6131         unsigned long *sense_ptr;
6132 
6133         memset(kbuff_arr, 0, sizeof(kbuff_arr));
6134 
6135         if (ioc->sge_count > MAX_IOCTL_SGE) {
6136                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "SGE count [%d] >  max limit [%d]\n",
6137                        ioc->sge_count, MAX_IOCTL_SGE);
6138                 return -EINVAL;
6139         }
6140 
6141         cmd = megasas_get_cmd(instance);
6142         if (!cmd) {
6143                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get a cmd packet\n");
6144                 return -ENOMEM;
6145         }
6146 
6147         /*
6148          * User's IOCTL packet has 2 frames (maximum). Copy those two
6149          * frames into our cmd's frames. cmd->frame's context will get
6150          * overwritten when we copy from user's frames. So set that value
6151          * alone separately
6152          */
6153         memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE);
6154         cmd->frame->hdr.context = cpu_to_le32(cmd->index);
6155         cmd->frame->hdr.pad_0 = 0;
6156         cmd->frame->hdr.flags &= cpu_to_le16(~(MFI_FRAME_IEEE |
6157                                                MFI_FRAME_SGL64 |
6158                                                MFI_FRAME_SENSE64));
6159 
6160         if (cmd->frame->dcmd.opcode == MR_DRIVER_SET_APP_CRASHDUMP_MODE) {
6161                 error = megasas_set_crash_dump_params_ioctl(cmd);
6162                 megasas_return_cmd(instance, cmd);
6163                 return error;
6164         }
6165 
6166         /*
6167          * The management interface between applications and the fw uses
6168          * MFI frames. E.g, RAID configuration changes, LD property changes
6169          * etc are accomplishes through different kinds of MFI frames. The
6170          * driver needs to care only about substituting user buffers with
6171          * kernel buffers in SGLs. The location of SGL is embedded in the
6172          * struct iocpacket itself.
6173          */
6174         kern_sge32 = (struct megasas_sge32 *)
6175             ((unsigned long)cmd->frame + ioc->sgl_off);
6176 
6177         /*
6178          * For each user buffer, create a mirror buffer and copy in
6179          */
6180         for (i = 0; i < ioc->sge_count; i++) {
6181                 if (!ioc->sgl[i].iov_len)
6182                         continue;
6183 
6184                 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev,
6185                                                     ioc->sgl[i].iov_len,
6186                                                     &buf_handle, GFP_KERNEL);
6187                 if (!kbuff_arr[i]) {
6188                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc "
6189                                "kernel SGL buffer for IOCTL\n");
6190                         error = -ENOMEM;
6191                         goto out;
6192                 }
6193 
6194                 /*
6195                  * We don't change the dma_coherent_mask, so
6196                  * pci_alloc_consistent only returns 32bit addresses
6197                  */
6198                 kern_sge32[i].phys_addr = cpu_to_le32(buf_handle);
6199                 kern_sge32[i].length = cpu_to_le32(ioc->sgl[i].iov_len);
6200 
6201                 /*
6202                  * We created a kernel buffer corresponding to the
6203                  * user buffer. Now copy in from the user buffer
6204                  */
6205                 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base,
6206                                    (u32) (ioc->sgl[i].iov_len))) {
6207                         error = -EFAULT;
6208                         goto out;
6209                 }
6210         }
6211 
6212         if (ioc->sense_len) {
6213                 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len,
6214                                              &sense_handle, GFP_KERNEL);
6215                 if (!sense) {
6216                         error = -ENOMEM;
6217                         goto out;
6218                 }
6219 
6220                 sense_ptr =
6221                 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off);
6222                 *sense_ptr = cpu_to_le32(sense_handle);
6223         }
6224 
6225         /*
6226          * Set the sync_cmd flag so that the ISR knows not to complete this
6227          * cmd to the SCSI mid-layer
6228          */
6229         cmd->sync_cmd = 1;
6230         megasas_issue_blocked_cmd(instance, cmd, 0);
6231         cmd->sync_cmd = 0;
6232 
6233         if (instance->unload == 1) {
6234                 dev_info(&instance->pdev->dev, "Driver unload is in progress "
6235                         "don't submit data to application\n");
6236                 goto out;
6237         }
6238         /*
6239          * copy out the kernel buffers to user buffers
6240          */
6241         for (i = 0; i < ioc->sge_count; i++) {
6242                 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i],
6243                                  ioc->sgl[i].iov_len)) {
6244                         error = -EFAULT;
6245                         goto out;
6246                 }
6247         }
6248 
6249         /*
6250          * copy out the sense
6251          */
6252         if (ioc->sense_len) {
6253                 /*
6254                  * sense_ptr points to the location that has the user
6255                  * sense buffer address
6256                  */
6257                 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw +
6258                                 ioc->sense_off);
6259 
6260                 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)),
6261                                  sense, ioc->sense_len)) {
6262                         dev_err(&instance->pdev->dev, "Failed to copy out to user "
6263                                         "sense data\n");
6264                         error = -EFAULT;
6265                         goto out;
6266                 }
6267         }
6268 
6269         /*
6270          * copy the status codes returned by the fw
6271          */
6272         if (copy_to_user(&user_ioc->frame.hdr.cmd_status,
6273                          &cmd->frame->hdr.cmd_status, sizeof(u8))) {
6274                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error copying out cmd_status\n");
6275                 error = -EFAULT;
6276         }
6277 
6278 out:
6279         if (sense) {
6280                 dma_free_coherent(&instance->pdev->dev, ioc->sense_len,
6281                                     sense, sense_handle);
6282         }
6283 
6284         for (i = 0; i < ioc->sge_count; i++) {
6285                 if (kbuff_arr[i])
6286                         dma_free_coherent(&instance->pdev->dev,
6287                                           le32_to_cpu(kern_sge32[i].length),
6288                                           kbuff_arr[i],
6289                                           le32_to_cpu(kern_sge32[i].phys_addr));
6290                         kbuff_arr[i] = NULL;
6291         }
6292 
6293         megasas_return_cmd(instance, cmd);
6294         return error;
6295 }
6296 
6297 static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg)
6298 {
6299         struct megasas_iocpacket __user *user_ioc =
6300             (struct megasas_iocpacket __user *)arg;
6301         struct megasas_iocpacket *ioc;
6302         struct megasas_instance *instance;
6303         int error;
6304         int i;
6305         unsigned long flags;
6306         u32 wait_time = MEGASAS_RESET_WAIT_TIME;
6307 
6308         ioc = kmalloc(sizeof(*ioc), GFP_KERNEL);
6309         if (!ioc)
6310                 return -ENOMEM;
6311 
6312         if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) {
6313                 error = -EFAULT;
6314                 goto out_kfree_ioc;
6315         }
6316 
6317         instance = megasas_lookup_instance(ioc->host_no);
6318         if (!instance) {
6319                 error = -ENODEV;
6320                 goto out_kfree_ioc;
6321         }
6322 
6323         /* Adjust ioctl wait time for VF mode */
6324         if (instance->requestorId)
6325                 wait_time = MEGASAS_ROUTINE_WAIT_TIME_VF;
6326 
6327         /* Block ioctls in VF mode */
6328         if (instance->requestorId && !allow_vf_ioctls) {
6329                 error = -ENODEV;
6330                 goto out_kfree_ioc;
6331         }
6332 
6333         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6334                 dev_err(&instance->pdev->dev, "Controller in crit error\n");
6335                 error = -ENODEV;
6336                 goto out_kfree_ioc;
6337         }
6338 
6339         if (instance->unload == 1) {
6340                 error = -ENODEV;
6341                 goto out_kfree_ioc;
6342         }
6343 
6344         if (down_interruptible(&instance->ioctl_sem)) {
6345                 error = -ERESTARTSYS;
6346                 goto out_kfree_ioc;
6347         }
6348 
6349         for (i = 0; i < wait_time; i++) {
6350 
6351                 spin_lock_irqsave(&instance->hba_lock, flags);
6352                 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6353                         spin_unlock_irqrestore(&instance->hba_lock, flags);
6354                         break;
6355                 }
6356                 spin_unlock_irqrestore(&instance->hba_lock, flags);
6357 
6358                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6359                         dev_notice(&instance->pdev->dev, "waiting"
6360                                 "for controller reset to finish\n");
6361                 }
6362 
6363                 msleep(1000);
6364         }
6365 
6366         spin_lock_irqsave(&instance->hba_lock, flags);
6367         if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6368                 spin_unlock_irqrestore(&instance->hba_lock, flags);
6369 
6370                 dev_err(&instance->pdev->dev, "timed out while"
6371                         "waiting for HBA to recover\n");
6372                 error = -ENODEV;
6373                 goto out_up;
6374         }
6375         spin_unlock_irqrestore(&instance->hba_lock, flags);
6376 
6377         error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc);
6378 out_up:
6379         up(&instance->ioctl_sem);
6380 
6381 out_kfree_ioc:
6382         kfree(ioc);
6383         return error;
6384 }
6385 
6386 static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg)
6387 {
6388         struct megasas_instance *instance;
6389         struct megasas_aen aen;
6390         int error;
6391         int i;
6392         unsigned long flags;
6393         u32 wait_time = MEGASAS_RESET_WAIT_TIME;
6394 
6395         if (file->private_data != file) {
6396                 printk(KERN_DEBUG "megasas: fasync_helper was not "
6397                        "called first\n");
6398                 return -EINVAL;
6399         }
6400 
6401         if (copy_from_user(&aen, (void __user *)arg, sizeof(aen)))
6402                 return -EFAULT;
6403 
6404         instance = megasas_lookup_instance(aen.host_no);
6405 
6406         if (!instance)
6407                 return -ENODEV;
6408 
6409         if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
6410                 return -ENODEV;
6411         }
6412 
6413         if (instance->unload == 1) {
6414                 return -ENODEV;
6415         }
6416 
6417         for (i = 0; i < wait_time; i++) {
6418 
6419                 spin_lock_irqsave(&instance->hba_lock, flags);
6420                 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
6421                         spin_unlock_irqrestore(&instance->hba_lock,
6422                                                 flags);
6423                         break;
6424                 }
6425 
6426                 spin_unlock_irqrestore(&instance->hba_lock, flags);
6427 
6428                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
6429                         dev_notice(&instance->pdev->dev, "waiting for"
6430                                 "controller reset to finish\n");
6431                 }
6432 
6433                 msleep(1000);
6434         }
6435 
6436         spin_lock_irqsave(&instance->hba_lock, flags);
6437         if (instance->adprecovery != MEGASAS_HBA_OPERATIONAL) {
6438                 spin_unlock_irqrestore(&instance->hba_lock, flags);
6439                 dev_err(&instance->pdev->dev, "timed out while waiting"
6440                                 "for HBA to recover\n");
6441                 return -ENODEV;
6442         }
6443         spin_unlock_irqrestore(&instance->hba_lock, flags);
6444 
6445         mutex_lock(&instance->aen_mutex);
6446         error = megasas_register_aen(instance, aen.seq_num,
6447                                      aen.class_locale_word);
6448         mutex_unlock(&instance->aen_mutex);
6449         return error;
6450 }
6451 
6452 /**
6453  * megasas_mgmt_ioctl - char node ioctl entry point
6454  */
6455 static long
6456 megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
6457 {
6458         switch (cmd) {
6459         case MEGASAS_IOC_FIRMWARE:
6460                 return megasas_mgmt_ioctl_fw(file, arg);
6461 
6462         case MEGASAS_IOC_GET_AEN:
6463                 return megasas_mgmt_ioctl_aen(file, arg);
6464         }
6465 
6466         return -ENOTTY;
6467 }
6468 
6469 #ifdef CONFIG_COMPAT
6470 static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg)
6471 {
6472         struct compat_megasas_iocpacket __user *cioc =
6473             (struct compat_megasas_iocpacket __user *)arg;
6474         struct megasas_iocpacket __user *ioc =
6475             compat_alloc_user_space(sizeof(struct megasas_iocpacket));
6476         int i;
6477         int error = 0;
6478         compat_uptr_t ptr;
6479         unsigned long local_raw_ptr;
6480         u32 local_sense_off;
6481         u32 local_sense_len;
6482 
6483         if (clear_user(ioc, sizeof(*ioc)))
6484                 return -EFAULT;
6485 
6486         if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) ||
6487             copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) ||
6488             copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) ||
6489             copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) ||
6490             copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) ||
6491             copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32)))
6492                 return -EFAULT;
6493 
6494         /*
6495          * The sense_ptr is used in megasas_mgmt_fw_ioctl only when
6496          * sense_len is not null, so prepare the 64bit value under
6497          *