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