Merge tag 'scsi-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/jejb/scsi
[sfrench/cifs-2.6.git] / drivers / scsi / megaraid / megaraid_sas_base.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Linux MegaRAID driver for SAS based RAID controllers
4  *
5  *  Copyright (c) 2003-2013  LSI Corporation
6  *  Copyright (c) 2013-2016  Avago Technologies
7  *  Copyright (c) 2016-2018  Broadcom Inc.
8  *
9  *  Authors: Broadcom Inc.
10  *           Sreenivas Bagalkote
11  *           Sumant Patro
12  *           Bo Yang
13  *           Adam Radford
14  *           Kashyap Desai <kashyap.desai@broadcom.com>
15  *           Sumit Saxena <sumit.saxena@broadcom.com>
16  *
17  *  Send feedback to: megaraidlinux.pdl@broadcom.com
18  */
19
20 #include <linux/kernel.h>
21 #include <linux/types.h>
22 #include <linux/pci.h>
23 #include <linux/list.h>
24 #include <linux/moduleparam.h>
25 #include <linux/module.h>
26 #include <linux/spinlock.h>
27 #include <linux/interrupt.h>
28 #include <linux/delay.h>
29 #include <linux/uio.h>
30 #include <linux/slab.h>
31 #include <linux/uaccess.h>
32 #include <asm/unaligned.h>
33 #include <linux/fs.h>
34 #include <linux/compat.h>
35 #include <linux/blkdev.h>
36 #include <linux/mutex.h>
37 #include <linux/poll.h>
38 #include <linux/vmalloc.h>
39 #include <linux/irq_poll.h>
40
41 #include <scsi/scsi.h>
42 #include <scsi/scsi_cmnd.h>
43 #include <scsi/scsi_device.h>
44 #include <scsi/scsi_host.h>
45 #include <scsi/scsi_tcq.h>
46 #include <scsi/scsi_dbg.h>
47 #include "megaraid_sas_fusion.h"
48 #include "megaraid_sas.h"
49
50 /*
51  * Number of sectors per IO command
52  * Will be set in megasas_init_mfi if user does not provide
53  */
54 static unsigned int max_sectors;
55 module_param_named(max_sectors, max_sectors, int, 0444);
56 MODULE_PARM_DESC(max_sectors,
57         "Maximum number of sectors per IO command");
58
59 static int msix_disable;
60 module_param(msix_disable, int, 0444);
61 MODULE_PARM_DESC(msix_disable, "Disable MSI-X interrupt handling. Default: 0");
62
63 static unsigned int msix_vectors;
64 module_param(msix_vectors, int, 0444);
65 MODULE_PARM_DESC(msix_vectors, "MSI-X max vector count. Default: Set by FW");
66
67 static int allow_vf_ioctls;
68 module_param(allow_vf_ioctls, int, 0444);
69 MODULE_PARM_DESC(allow_vf_ioctls, "Allow ioctls in SR-IOV VF mode. Default: 0");
70
71 static unsigned int throttlequeuedepth = MEGASAS_THROTTLE_QUEUE_DEPTH;
72 module_param(throttlequeuedepth, int, 0444);
73 MODULE_PARM_DESC(throttlequeuedepth,
74         "Adapter queue depth when throttled due to I/O timeout. Default: 16");
75
76 unsigned int resetwaittime = MEGASAS_RESET_WAIT_TIME;
77 module_param(resetwaittime, int, 0444);
78 MODULE_PARM_DESC(resetwaittime, "Wait time in (1-180s) after I/O timeout before resetting adapter. Default: 180s");
79
80 int smp_affinity_enable = 1;
81 module_param(smp_affinity_enable, int, 0444);
82 MODULE_PARM_DESC(smp_affinity_enable, "SMP affinity feature enable/disable Default: enable(1)");
83
84 int rdpq_enable = 1;
85 module_param(rdpq_enable, int, 0444);
86 MODULE_PARM_DESC(rdpq_enable, "Allocate reply queue in chunks for large queue depth enable/disable Default: enable(1)");
87
88 unsigned int dual_qdepth_disable;
89 module_param(dual_qdepth_disable, int, 0444);
90 MODULE_PARM_DESC(dual_qdepth_disable, "Disable dual queue depth feature. Default: 0");
91
92 unsigned int scmd_timeout = MEGASAS_DEFAULT_CMD_TIMEOUT;
93 module_param(scmd_timeout, int, 0444);
94 MODULE_PARM_DESC(scmd_timeout, "scsi command timeout (10-90s), default 90s. See megasas_reset_timer.");
95
96 int perf_mode = -1;
97 module_param(perf_mode, int, 0444);
98 MODULE_PARM_DESC(perf_mode, "Performance mode (only for Aero adapters), options:\n\t\t"
99                 "0 - balanced: High iops and low latency queues are allocated &\n\t\t"
100                 "interrupt coalescing is enabled only on high iops queues\n\t\t"
101                 "1 - iops: High iops queues are not allocated &\n\t\t"
102                 "interrupt coalescing is enabled on all queues\n\t\t"
103                 "2 - latency: High iops queues are not allocated &\n\t\t"
104                 "interrupt coalescing is disabled on all queues\n\t\t"
105                 "default mode is 'balanced'"
106                 );
107
108 int event_log_level = MFI_EVT_CLASS_CRITICAL;
109 module_param(event_log_level, int, 0644);
110 MODULE_PARM_DESC(event_log_level, "Asynchronous event logging level- range is: -2(CLASS_DEBUG) to 4(CLASS_DEAD), Default: 2(CLASS_CRITICAL)");
111
112 MODULE_LICENSE("GPL");
113 MODULE_VERSION(MEGASAS_VERSION);
114 MODULE_AUTHOR("megaraidlinux.pdl@broadcom.com");
115 MODULE_DESCRIPTION("Broadcom MegaRAID SAS Driver");
116
117 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
118 static int megasas_get_pd_list(struct megasas_instance *instance);
119 static int megasas_ld_list_query(struct megasas_instance *instance,
120                                  u8 query_type);
121 static int megasas_issue_init_mfi(struct megasas_instance *instance);
122 static int megasas_register_aen(struct megasas_instance *instance,
123                                 u32 seq_num, u32 class_locale_word);
124 static void megasas_get_pd_info(struct megasas_instance *instance,
125                                 struct scsi_device *sdev);
126
127 /*
128  * PCI ID table for all supported controllers
129  */
130 static struct pci_device_id megasas_pci_table[] = {
131
132         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)},
133         /* xscale IOP */
134         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)},
135         /* ppc IOP */
136         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)},
137         /* ppc IOP */
138         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)},
139         /* gen2*/
140         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)},
141         /* gen2*/
142         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)},
143         /* skinny*/
144         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)},
145         /* skinny*/
146         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)},
147         /* xscale IOP, vega */
148         {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)},
149         /* xscale IOP */
150         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FUSION)},
151         /* Fusion */
152         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_PLASMA)},
153         /* Plasma */
154         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INVADER)},
155         /* Invader */
156         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_FURY)},
157         /* Fury */
158         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER)},
159         /* Intruder */
160         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_INTRUDER_24)},
161         /* Intruder 24 port*/
162         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_52)},
163         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CUTLASS_53)},
164         /* VENTURA */
165         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA)},
166         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER)},
167         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_HARPOON)},
168         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_TOMCAT)},
169         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VENTURA_4PORT)},
170         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_CRUSADER_4PORT)},
171         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E1)},
172         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E2)},
173         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E5)},
174         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E6)},
175         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E0)},
176         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E3)},
177         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E4)},
178         {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_AERO_10E7)},
179         {}
180 };
181
182 MODULE_DEVICE_TABLE(pci, megasas_pci_table);
183
184 static int megasas_mgmt_majorno;
185 struct megasas_mgmt_info megasas_mgmt_info;
186 static struct fasync_struct *megasas_async_queue;
187 static DEFINE_MUTEX(megasas_async_queue_mutex);
188
189 static int megasas_poll_wait_aen;
190 static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait);
191 static u32 support_poll_for_event;
192 u32 megasas_dbg_lvl;
193 static u32 support_device_change;
194 static bool support_nvme_encapsulation;
195 static bool support_pci_lane_margining;
196
197 /* define lock for aen poll */
198 spinlock_t poll_aen_lock;
199
200 extern struct dentry *megasas_debugfs_root;
201 extern void megasas_init_debugfs(void);
202 extern void megasas_exit_debugfs(void);
203 extern void megasas_setup_debugfs(struct megasas_instance *instance);
204 extern void megasas_destroy_debugfs(struct megasas_instance *instance);
205
206 void
207 megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd,
208                      u8 alt_status);
209 static u32
210 megasas_read_fw_status_reg_gen2(struct megasas_instance *instance);
211 static int
212 megasas_adp_reset_gen2(struct megasas_instance *instance,
213                        struct megasas_register_set __iomem *reg_set);
214 static irqreturn_t megasas_isr(int irq, void *devp);
215 static u32
216 megasas_init_adapter_mfi(struct megasas_instance *instance);
217 u32
218 megasas_build_and_issue_cmd(struct megasas_instance *instance,
219                             struct scsi_cmnd *scmd);
220 static void megasas_complete_cmd_dpc(unsigned long instance_addr);
221 int
222 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
223         int seconds);
224 void megasas_fusion_ocr_wq(struct work_struct *work);
225 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
226                                          int initial);
227 static int
228 megasas_set_dma_mask(struct megasas_instance *instance);
229 static int
230 megasas_alloc_ctrl_mem(struct megasas_instance *instance);
231 static inline void
232 megasas_free_ctrl_mem(struct megasas_instance *instance);
233 static inline int
234 megasas_alloc_ctrl_dma_buffers(struct megasas_instance *instance);
235 static inline void
236 megasas_free_ctrl_dma_buffers(struct megasas_instance *instance);
237 static inline void
238 megasas_init_ctrl_params(struct megasas_instance *instance);
239
240 u32 megasas_readl(struct megasas_instance *instance,
241                   const volatile void __iomem *addr)
242 {
243         u32 i = 0, ret_val;
244         /*
245          * Due to a HW errata in Aero controllers, reads to certain
246          * Fusion registers could intermittently return all zeroes.
247          * This behavior is transient in nature and subsequent reads will
248          * return valid value. As a workaround in driver, retry readl for
249          * upto three times until a non-zero value is read.
250          */
251         if (instance->adapter_type == AERO_SERIES) {
252                 do {
253                         ret_val = readl(addr);
254                         i++;
255                 } while (ret_val == 0 && i < 3);
256                 return ret_val;
257         } else {
258                 return readl(addr);
259         }
260 }
261
262 /**
263  * megasas_set_dma_settings -   Populate DMA address, length and flags for DCMDs
264  * @instance:                   Adapter soft state
265  * @dcmd:                       DCMD frame inside MFI command
266  * @dma_addr:                   DMA address of buffer to be passed to FW
267  * @dma_len:                    Length of DMA buffer to be passed to FW
268  * @return:                     void
269  */
270 void megasas_set_dma_settings(struct megasas_instance *instance,
271                               struct megasas_dcmd_frame *dcmd,
272                               dma_addr_t dma_addr, u32 dma_len)
273 {
274         if (instance->consistent_mask_64bit) {
275                 dcmd->sgl.sge64[0].phys_addr = cpu_to_le64(dma_addr);
276                 dcmd->sgl.sge64[0].length = cpu_to_le32(dma_len);
277                 dcmd->flags = cpu_to_le16(dcmd->flags | MFI_FRAME_SGL64);
278
279         } else {
280                 dcmd->sgl.sge32[0].phys_addr =
281                                 cpu_to_le32(lower_32_bits(dma_addr));
282                 dcmd->sgl.sge32[0].length = cpu_to_le32(dma_len);
283                 dcmd->flags = cpu_to_le16(dcmd->flags);
284         }
285 }
286
287 static void
288 megasas_issue_dcmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
289 {
290         instance->instancet->fire_cmd(instance,
291                 cmd->frame_phys_addr, 0, instance->reg_set);
292         return;
293 }
294
295 /**
296  * megasas_get_cmd -    Get a command from the free pool
297  * @instance:           Adapter soft state
298  *
299  * Returns a free command from the pool
300  */
301 struct megasas_cmd *megasas_get_cmd(struct megasas_instance
302                                                   *instance)
303 {
304         unsigned long flags;
305         struct megasas_cmd *cmd = NULL;
306
307         spin_lock_irqsave(&instance->mfi_pool_lock, flags);
308
309         if (!list_empty(&instance->cmd_pool)) {
310                 cmd = list_entry((&instance->cmd_pool)->next,
311                                  struct megasas_cmd, list);
312                 list_del_init(&cmd->list);
313         } else {
314                 dev_err(&instance->pdev->dev, "Command pool empty!\n");
315         }
316
317         spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
318         return cmd;
319 }
320
321 /**
322  * megasas_return_cmd - Return a cmd to free command pool
323  * @instance:           Adapter soft state
324  * @cmd:                Command packet to be returned to free command pool
325  */
326 void
327 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
328 {
329         unsigned long flags;
330         u32 blk_tags;
331         struct megasas_cmd_fusion *cmd_fusion;
332         struct fusion_context *fusion = instance->ctrl_context;
333
334         /* This flag is used only for fusion adapter.
335          * Wait for Interrupt for Polled mode DCMD
336          */
337         if (cmd->flags & DRV_DCMD_POLLED_MODE)
338                 return;
339
340         spin_lock_irqsave(&instance->mfi_pool_lock, flags);
341
342         if (fusion) {
343                 blk_tags = instance->max_scsi_cmds + cmd->index;
344                 cmd_fusion = fusion->cmd_list[blk_tags];
345                 megasas_return_cmd_fusion(instance, cmd_fusion);
346         }
347         cmd->scmd = NULL;
348         cmd->frame_count = 0;
349         cmd->flags = 0;
350         memset(cmd->frame, 0, instance->mfi_frame_size);
351         cmd->frame->io.context = cpu_to_le32(cmd->index);
352         if (!fusion && reset_devices)
353                 cmd->frame->hdr.cmd = MFI_CMD_INVALID;
354         list_add(&cmd->list, (&instance->cmd_pool)->next);
355
356         spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
357
358 }
359
360 static const char *
361 format_timestamp(uint32_t timestamp)
362 {
363         static char buffer[32];
364
365         if ((timestamp & 0xff000000) == 0xff000000)
366                 snprintf(buffer, sizeof(buffer), "boot + %us", timestamp &
367                 0x00ffffff);
368         else
369                 snprintf(buffer, sizeof(buffer), "%us", timestamp);
370         return buffer;
371 }
372
373 static const char *
374 format_class(int8_t class)
375 {
376         static char buffer[6];
377
378         switch (class) {
379         case MFI_EVT_CLASS_DEBUG:
380                 return "debug";
381         case MFI_EVT_CLASS_PROGRESS:
382                 return "progress";
383         case MFI_EVT_CLASS_INFO:
384                 return "info";
385         case MFI_EVT_CLASS_WARNING:
386                 return "WARN";
387         case MFI_EVT_CLASS_CRITICAL:
388                 return "CRIT";
389         case MFI_EVT_CLASS_FATAL:
390                 return "FATAL";
391         case MFI_EVT_CLASS_DEAD:
392                 return "DEAD";
393         default:
394                 snprintf(buffer, sizeof(buffer), "%d", class);
395                 return buffer;
396         }
397 }
398
399 /**
400   * megasas_decode_evt: Decode FW AEN event and print critical event
401   * for information.
402   * @instance:                  Adapter soft state
403   */
404 static void
405 megasas_decode_evt(struct megasas_instance *instance)
406 {
407         struct megasas_evt_detail *evt_detail = instance->evt_detail;
408         union megasas_evt_class_locale class_locale;
409         class_locale.word = le32_to_cpu(evt_detail->cl.word);
410
411         if ((event_log_level < MFI_EVT_CLASS_DEBUG) ||
412             (event_log_level > MFI_EVT_CLASS_DEAD)) {
413                 printk(KERN_WARNING "megaraid_sas: provided event log level is out of range, setting it to default 2(CLASS_CRITICAL), permissible range is: -2 to 4\n");
414                 event_log_level = MFI_EVT_CLASS_CRITICAL;
415         }
416
417         if (class_locale.members.class >= event_log_level)
418                 dev_info(&instance->pdev->dev, "%d (%s/0x%04x/%s) - %s\n",
419                         le32_to_cpu(evt_detail->seq_num),
420                         format_timestamp(le32_to_cpu(evt_detail->time_stamp)),
421                         (class_locale.members.locale),
422                         format_class(class_locale.members.class),
423                         evt_detail->description);
424 }
425
426 /**
427 *       The following functions are defined for xscale
428 *       (deviceid : 1064R, PERC5) controllers
429 */
430
431 /**
432  * megasas_enable_intr_xscale - Enables interrupts
433  * @regs:                       MFI register set
434  */
435 static inline void
436 megasas_enable_intr_xscale(struct megasas_instance *instance)
437 {
438         struct megasas_register_set __iomem *regs;
439
440         regs = instance->reg_set;
441         writel(0, &(regs)->outbound_intr_mask);
442
443         /* Dummy readl to force pci flush */
444         readl(&regs->outbound_intr_mask);
445 }
446
447 /**
448  * megasas_disable_intr_xscale -Disables interrupt
449  * @regs:                       MFI register set
450  */
451 static inline void
452 megasas_disable_intr_xscale(struct megasas_instance *instance)
453 {
454         struct megasas_register_set __iomem *regs;
455         u32 mask = 0x1f;
456
457         regs = instance->reg_set;
458         writel(mask, &regs->outbound_intr_mask);
459         /* Dummy readl to force pci flush */
460         readl(&regs->outbound_intr_mask);
461 }
462
463 /**
464  * megasas_read_fw_status_reg_xscale - returns the current FW status value
465  * @regs:                       MFI register set
466  */
467 static u32
468 megasas_read_fw_status_reg_xscale(struct megasas_instance *instance)
469 {
470         return readl(&instance->reg_set->outbound_msg_0);
471 }
472 /**
473  * megasas_clear_interrupt_xscale -     Check & clear interrupt
474  * @regs:                               MFI register set
475  */
476 static int
477 megasas_clear_intr_xscale(struct megasas_instance *instance)
478 {
479         u32 status;
480         u32 mfiStatus = 0;
481         struct megasas_register_set __iomem *regs;
482         regs = instance->reg_set;
483
484         /*
485          * Check if it is our interrupt
486          */
487         status = readl(&regs->outbound_intr_status);
488
489         if (status & MFI_OB_INTR_STATUS_MASK)
490                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
491         if (status & MFI_XSCALE_OMR0_CHANGE_INTERRUPT)
492                 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
493
494         /*
495          * Clear the interrupt by writing back the same value
496          */
497         if (mfiStatus)
498                 writel(status, &regs->outbound_intr_status);
499
500         /* Dummy readl to force pci flush */
501         readl(&regs->outbound_intr_status);
502
503         return mfiStatus;
504 }
505
506 /**
507  * megasas_fire_cmd_xscale -    Sends command to the FW
508  * @frame_phys_addr :           Physical address of cmd
509  * @frame_count :               Number of frames for the command
510  * @regs :                      MFI register set
511  */
512 static inline void
513 megasas_fire_cmd_xscale(struct megasas_instance *instance,
514                 dma_addr_t frame_phys_addr,
515                 u32 frame_count,
516                 struct megasas_register_set __iomem *regs)
517 {
518         unsigned long flags;
519
520         spin_lock_irqsave(&instance->hba_lock, flags);
521         writel((frame_phys_addr >> 3)|(frame_count),
522                &(regs)->inbound_queue_port);
523         spin_unlock_irqrestore(&instance->hba_lock, flags);
524 }
525
526 /**
527  * megasas_adp_reset_xscale -  For controller reset
528  * @regs:                              MFI register set
529  */
530 static int
531 megasas_adp_reset_xscale(struct megasas_instance *instance,
532         struct megasas_register_set __iomem *regs)
533 {
534         u32 i;
535         u32 pcidata;
536
537         writel(MFI_ADP_RESET, &regs->inbound_doorbell);
538
539         for (i = 0; i < 3; i++)
540                 msleep(1000); /* sleep for 3 secs */
541         pcidata  = 0;
542         pci_read_config_dword(instance->pdev, MFI_1068_PCSR_OFFSET, &pcidata);
543         dev_notice(&instance->pdev->dev, "pcidata = %x\n", pcidata);
544         if (pcidata & 0x2) {
545                 dev_notice(&instance->pdev->dev, "mfi 1068 offset read=%x\n", pcidata);
546                 pcidata &= ~0x2;
547                 pci_write_config_dword(instance->pdev,
548                                 MFI_1068_PCSR_OFFSET, pcidata);
549
550                 for (i = 0; i < 2; i++)
551                         msleep(1000); /* need to wait 2 secs again */
552
553                 pcidata  = 0;
554                 pci_read_config_dword(instance->pdev,
555                                 MFI_1068_FW_HANDSHAKE_OFFSET, &pcidata);
556                 dev_notice(&instance->pdev->dev, "1068 offset handshake read=%x\n", pcidata);
557                 if ((pcidata & 0xffff0000) == MFI_1068_FW_READY) {
558                         dev_notice(&instance->pdev->dev, "1068 offset pcidt=%x\n", pcidata);
559                         pcidata = 0;
560                         pci_write_config_dword(instance->pdev,
561                                 MFI_1068_FW_HANDSHAKE_OFFSET, pcidata);
562                 }
563         }
564         return 0;
565 }
566
567 /**
568  * megasas_check_reset_xscale - For controller reset check
569  * @regs:                               MFI register set
570  */
571 static int
572 megasas_check_reset_xscale(struct megasas_instance *instance,
573                 struct megasas_register_set __iomem *regs)
574 {
575         if ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
576             (le32_to_cpu(*instance->consumer) ==
577                 MEGASAS_ADPRESET_INPROG_SIGN))
578                 return 1;
579         return 0;
580 }
581
582 static struct megasas_instance_template megasas_instance_template_xscale = {
583
584         .fire_cmd = megasas_fire_cmd_xscale,
585         .enable_intr = megasas_enable_intr_xscale,
586         .disable_intr = megasas_disable_intr_xscale,
587         .clear_intr = megasas_clear_intr_xscale,
588         .read_fw_status_reg = megasas_read_fw_status_reg_xscale,
589         .adp_reset = megasas_adp_reset_xscale,
590         .check_reset = megasas_check_reset_xscale,
591         .service_isr = megasas_isr,
592         .tasklet = megasas_complete_cmd_dpc,
593         .init_adapter = megasas_init_adapter_mfi,
594         .build_and_issue_cmd = megasas_build_and_issue_cmd,
595         .issue_dcmd = megasas_issue_dcmd,
596 };
597
598 /**
599 *       This is the end of set of functions & definitions specific
600 *       to xscale (deviceid : 1064R, PERC5) controllers
601 */
602
603 /**
604 *       The following functions are defined for ppc (deviceid : 0x60)
605 *       controllers
606 */
607
608 /**
609  * megasas_enable_intr_ppc -    Enables interrupts
610  * @regs:                       MFI register set
611  */
612 static inline void
613 megasas_enable_intr_ppc(struct megasas_instance *instance)
614 {
615         struct megasas_register_set __iomem *regs;
616
617         regs = instance->reg_set;
618         writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
619
620         writel(~0x80000000, &(regs)->outbound_intr_mask);
621
622         /* Dummy readl to force pci flush */
623         readl(&regs->outbound_intr_mask);
624 }
625
626 /**
627  * megasas_disable_intr_ppc -   Disable interrupt
628  * @regs:                       MFI register set
629  */
630 static inline void
631 megasas_disable_intr_ppc(struct megasas_instance *instance)
632 {
633         struct megasas_register_set __iomem *regs;
634         u32 mask = 0xFFFFFFFF;
635
636         regs = instance->reg_set;
637         writel(mask, &regs->outbound_intr_mask);
638         /* Dummy readl to force pci flush */
639         readl(&regs->outbound_intr_mask);
640 }
641
642 /**
643  * megasas_read_fw_status_reg_ppc - returns the current FW status value
644  * @regs:                       MFI register set
645  */
646 static u32
647 megasas_read_fw_status_reg_ppc(struct megasas_instance *instance)
648 {
649         return readl(&instance->reg_set->outbound_scratch_pad_0);
650 }
651
652 /**
653  * megasas_clear_interrupt_ppc -        Check & clear interrupt
654  * @regs:                               MFI register set
655  */
656 static int
657 megasas_clear_intr_ppc(struct megasas_instance *instance)
658 {
659         u32 status, mfiStatus = 0;
660         struct megasas_register_set __iomem *regs;
661         regs = instance->reg_set;
662
663         /*
664          * Check if it is our interrupt
665          */
666         status = readl(&regs->outbound_intr_status);
667
668         if (status & MFI_REPLY_1078_MESSAGE_INTERRUPT)
669                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
670
671         if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT)
672                 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
673
674         /*
675          * Clear the interrupt by writing back the same value
676          */
677         writel(status, &regs->outbound_doorbell_clear);
678
679         /* Dummy readl to force pci flush */
680         readl(&regs->outbound_doorbell_clear);
681
682         return mfiStatus;
683 }
684
685 /**
686  * megasas_fire_cmd_ppc -       Sends command to the FW
687  * @frame_phys_addr :           Physical address of cmd
688  * @frame_count :               Number of frames for the command
689  * @regs :                      MFI register set
690  */
691 static inline void
692 megasas_fire_cmd_ppc(struct megasas_instance *instance,
693                 dma_addr_t frame_phys_addr,
694                 u32 frame_count,
695                 struct megasas_register_set __iomem *regs)
696 {
697         unsigned long flags;
698
699         spin_lock_irqsave(&instance->hba_lock, flags);
700         writel((frame_phys_addr | (frame_count<<1))|1,
701                         &(regs)->inbound_queue_port);
702         spin_unlock_irqrestore(&instance->hba_lock, flags);
703 }
704
705 /**
706  * megasas_check_reset_ppc -    For controller reset check
707  * @regs:                               MFI register set
708  */
709 static int
710 megasas_check_reset_ppc(struct megasas_instance *instance,
711                         struct megasas_register_set __iomem *regs)
712 {
713         if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
714                 return 1;
715
716         return 0;
717 }
718
719 static struct megasas_instance_template megasas_instance_template_ppc = {
720
721         .fire_cmd = megasas_fire_cmd_ppc,
722         .enable_intr = megasas_enable_intr_ppc,
723         .disable_intr = megasas_disable_intr_ppc,
724         .clear_intr = megasas_clear_intr_ppc,
725         .read_fw_status_reg = megasas_read_fw_status_reg_ppc,
726         .adp_reset = megasas_adp_reset_xscale,
727         .check_reset = megasas_check_reset_ppc,
728         .service_isr = megasas_isr,
729         .tasklet = megasas_complete_cmd_dpc,
730         .init_adapter = megasas_init_adapter_mfi,
731         .build_and_issue_cmd = megasas_build_and_issue_cmd,
732         .issue_dcmd = megasas_issue_dcmd,
733 };
734
735 /**
736  * megasas_enable_intr_skinny - Enables interrupts
737  * @regs:                       MFI register set
738  */
739 static inline void
740 megasas_enable_intr_skinny(struct megasas_instance *instance)
741 {
742         struct megasas_register_set __iomem *regs;
743
744         regs = instance->reg_set;
745         writel(0xFFFFFFFF, &(regs)->outbound_intr_mask);
746
747         writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
748
749         /* Dummy readl to force pci flush */
750         readl(&regs->outbound_intr_mask);
751 }
752
753 /**
754  * megasas_disable_intr_skinny -        Disables interrupt
755  * @regs:                       MFI register set
756  */
757 static inline void
758 megasas_disable_intr_skinny(struct megasas_instance *instance)
759 {
760         struct megasas_register_set __iomem *regs;
761         u32 mask = 0xFFFFFFFF;
762
763         regs = instance->reg_set;
764         writel(mask, &regs->outbound_intr_mask);
765         /* Dummy readl to force pci flush */
766         readl(&regs->outbound_intr_mask);
767 }
768
769 /**
770  * megasas_read_fw_status_reg_skinny - returns the current FW status value
771  * @regs:                       MFI register set
772  */
773 static u32
774 megasas_read_fw_status_reg_skinny(struct megasas_instance *instance)
775 {
776         return readl(&instance->reg_set->outbound_scratch_pad_0);
777 }
778
779 /**
780  * megasas_clear_interrupt_skinny -     Check & clear interrupt
781  * @regs:                               MFI register set
782  */
783 static int
784 megasas_clear_intr_skinny(struct megasas_instance *instance)
785 {
786         u32 status;
787         u32 mfiStatus = 0;
788         struct megasas_register_set __iomem *regs;
789         regs = instance->reg_set;
790
791         /*
792          * Check if it is our interrupt
793          */
794         status = readl(&regs->outbound_intr_status);
795
796         if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) {
797                 return 0;
798         }
799
800         /*
801          * Check if it is our interrupt
802          */
803         if ((megasas_read_fw_status_reg_skinny(instance) & MFI_STATE_MASK) ==
804             MFI_STATE_FAULT) {
805                 mfiStatus = MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
806         } else
807                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
808
809         /*
810          * Clear the interrupt by writing back the same value
811          */
812         writel(status, &regs->outbound_intr_status);
813
814         /*
815          * dummy read to flush PCI
816          */
817         readl(&regs->outbound_intr_status);
818
819         return mfiStatus;
820 }
821
822 /**
823  * megasas_fire_cmd_skinny -    Sends command to the FW
824  * @frame_phys_addr :           Physical address of cmd
825  * @frame_count :               Number of frames for the command
826  * @regs :                      MFI register set
827  */
828 static inline void
829 megasas_fire_cmd_skinny(struct megasas_instance *instance,
830                         dma_addr_t frame_phys_addr,
831                         u32 frame_count,
832                         struct megasas_register_set __iomem *regs)
833 {
834         unsigned long flags;
835
836         spin_lock_irqsave(&instance->hba_lock, flags);
837         writel(upper_32_bits(frame_phys_addr),
838                &(regs)->inbound_high_queue_port);
839         writel((lower_32_bits(frame_phys_addr) | (frame_count<<1))|1,
840                &(regs)->inbound_low_queue_port);
841         spin_unlock_irqrestore(&instance->hba_lock, flags);
842 }
843
844 /**
845  * megasas_check_reset_skinny - For controller reset check
846  * @regs:                               MFI register set
847  */
848 static int
849 megasas_check_reset_skinny(struct megasas_instance *instance,
850                                 struct megasas_register_set __iomem *regs)
851 {
852         if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
853                 return 1;
854
855         return 0;
856 }
857
858 static struct megasas_instance_template megasas_instance_template_skinny = {
859
860         .fire_cmd = megasas_fire_cmd_skinny,
861         .enable_intr = megasas_enable_intr_skinny,
862         .disable_intr = megasas_disable_intr_skinny,
863         .clear_intr = megasas_clear_intr_skinny,
864         .read_fw_status_reg = megasas_read_fw_status_reg_skinny,
865         .adp_reset = megasas_adp_reset_gen2,
866         .check_reset = megasas_check_reset_skinny,
867         .service_isr = megasas_isr,
868         .tasklet = megasas_complete_cmd_dpc,
869         .init_adapter = megasas_init_adapter_mfi,
870         .build_and_issue_cmd = megasas_build_and_issue_cmd,
871         .issue_dcmd = megasas_issue_dcmd,
872 };
873
874
875 /**
876 *       The following functions are defined for gen2 (deviceid : 0x78 0x79)
877 *       controllers
878 */
879
880 /**
881  * megasas_enable_intr_gen2 -  Enables interrupts
882  * @regs:                      MFI register set
883  */
884 static inline void
885 megasas_enable_intr_gen2(struct megasas_instance *instance)
886 {
887         struct megasas_register_set __iomem *regs;
888
889         regs = instance->reg_set;
890         writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear);
891
892         /* write ~0x00000005 (4 & 1) to the intr mask*/
893         writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
894
895         /* Dummy readl to force pci flush */
896         readl(&regs->outbound_intr_mask);
897 }
898
899 /**
900  * megasas_disable_intr_gen2 - Disables interrupt
901  * @regs:                      MFI register set
902  */
903 static inline void
904 megasas_disable_intr_gen2(struct megasas_instance *instance)
905 {
906         struct megasas_register_set __iomem *regs;
907         u32 mask = 0xFFFFFFFF;
908
909         regs = instance->reg_set;
910         writel(mask, &regs->outbound_intr_mask);
911         /* Dummy readl to force pci flush */
912         readl(&regs->outbound_intr_mask);
913 }
914
915 /**
916  * megasas_read_fw_status_reg_gen2 - returns the current FW status value
917  * @regs:                      MFI register set
918  */
919 static u32
920 megasas_read_fw_status_reg_gen2(struct megasas_instance *instance)
921 {
922         return readl(&instance->reg_set->outbound_scratch_pad_0);
923 }
924
925 /**
926  * megasas_clear_interrupt_gen2 -      Check & clear interrupt
927  * @regs:                              MFI register set
928  */
929 static int
930 megasas_clear_intr_gen2(struct megasas_instance *instance)
931 {
932         u32 status;
933         u32 mfiStatus = 0;
934         struct megasas_register_set __iomem *regs;
935         regs = instance->reg_set;
936
937         /*
938          * Check if it is our interrupt
939          */
940         status = readl(&regs->outbound_intr_status);
941
942         if (status & MFI_INTR_FLAG_REPLY_MESSAGE) {
943                 mfiStatus = MFI_INTR_FLAG_REPLY_MESSAGE;
944         }
945         if (status & MFI_G2_OUTBOUND_DOORBELL_CHANGE_INTERRUPT) {
946                 mfiStatus |= MFI_INTR_FLAG_FIRMWARE_STATE_CHANGE;
947         }
948
949         /*
950          * Clear the interrupt by writing back the same value
951          */
952         if (mfiStatus)
953                 writel(status, &regs->outbound_doorbell_clear);
954
955         /* Dummy readl to force pci flush */
956         readl(&regs->outbound_intr_status);
957
958         return mfiStatus;
959 }
960 /**
961  * megasas_fire_cmd_gen2 -     Sends command to the FW
962  * @frame_phys_addr :          Physical address of cmd
963  * @frame_count :              Number of frames for the command
964  * @regs :                     MFI register set
965  */
966 static inline void
967 megasas_fire_cmd_gen2(struct megasas_instance *instance,
968                         dma_addr_t frame_phys_addr,
969                         u32 frame_count,
970                         struct megasas_register_set __iomem *regs)
971 {
972         unsigned long flags;
973
974         spin_lock_irqsave(&instance->hba_lock, flags);
975         writel((frame_phys_addr | (frame_count<<1))|1,
976                         &(regs)->inbound_queue_port);
977         spin_unlock_irqrestore(&instance->hba_lock, flags);
978 }
979
980 /**
981  * megasas_adp_reset_gen2 -     For controller reset
982  * @regs:                               MFI register set
983  */
984 static int
985 megasas_adp_reset_gen2(struct megasas_instance *instance,
986                         struct megasas_register_set __iomem *reg_set)
987 {
988         u32 retry = 0 ;
989         u32 HostDiag;
990         u32 __iomem *seq_offset = &reg_set->seq_offset;
991         u32 __iomem *hostdiag_offset = &reg_set->host_diag;
992
993         if (instance->instancet == &megasas_instance_template_skinny) {
994                 seq_offset = &reg_set->fusion_seq_offset;
995                 hostdiag_offset = &reg_set->fusion_host_diag;
996         }
997
998         writel(0, seq_offset);
999         writel(4, seq_offset);
1000         writel(0xb, seq_offset);
1001         writel(2, seq_offset);
1002         writel(7, seq_offset);
1003         writel(0xd, seq_offset);
1004
1005         msleep(1000);
1006
1007         HostDiag = (u32)readl(hostdiag_offset);
1008
1009         while (!(HostDiag & DIAG_WRITE_ENABLE)) {
1010                 msleep(100);
1011                 HostDiag = (u32)readl(hostdiag_offset);
1012                 dev_notice(&instance->pdev->dev, "RESETGEN2: retry=%x, hostdiag=%x\n",
1013                                         retry, HostDiag);
1014
1015                 if (retry++ >= 100)
1016                         return 1;
1017
1018         }
1019
1020         dev_notice(&instance->pdev->dev, "ADP_RESET_GEN2: HostDiag=%x\n", HostDiag);
1021
1022         writel((HostDiag | DIAG_RESET_ADAPTER), hostdiag_offset);
1023
1024         ssleep(10);
1025
1026         HostDiag = (u32)readl(hostdiag_offset);
1027         while (HostDiag & DIAG_RESET_ADAPTER) {
1028                 msleep(100);
1029                 HostDiag = (u32)readl(hostdiag_offset);
1030                 dev_notice(&instance->pdev->dev, "RESET_GEN2: retry=%x, hostdiag=%x\n",
1031                                 retry, HostDiag);
1032
1033                 if (retry++ >= 1000)
1034                         return 1;
1035
1036         }
1037         return 0;
1038 }
1039
1040 /**
1041  * megasas_check_reset_gen2 -   For controller reset check
1042  * @regs:                               MFI register set
1043  */
1044 static int
1045 megasas_check_reset_gen2(struct megasas_instance *instance,
1046                 struct megasas_register_set __iomem *regs)
1047 {
1048         if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1049                 return 1;
1050
1051         return 0;
1052 }
1053
1054 static struct megasas_instance_template megasas_instance_template_gen2 = {
1055
1056         .fire_cmd = megasas_fire_cmd_gen2,
1057         .enable_intr = megasas_enable_intr_gen2,
1058         .disable_intr = megasas_disable_intr_gen2,
1059         .clear_intr = megasas_clear_intr_gen2,
1060         .read_fw_status_reg = megasas_read_fw_status_reg_gen2,
1061         .adp_reset = megasas_adp_reset_gen2,
1062         .check_reset = megasas_check_reset_gen2,
1063         .service_isr = megasas_isr,
1064         .tasklet = megasas_complete_cmd_dpc,
1065         .init_adapter = megasas_init_adapter_mfi,
1066         .build_and_issue_cmd = megasas_build_and_issue_cmd,
1067         .issue_dcmd = megasas_issue_dcmd,
1068 };
1069
1070 /**
1071 *       This is the end of set of functions & definitions
1072 *       specific to gen2 (deviceid : 0x78, 0x79) controllers
1073 */
1074
1075 /*
1076  * Template added for TB (Fusion)
1077  */
1078 extern struct megasas_instance_template megasas_instance_template_fusion;
1079
1080 /**
1081  * megasas_issue_polled -       Issues a polling command
1082  * @instance:                   Adapter soft state
1083  * @cmd:                        Command packet to be issued
1084  *
1085  * For polling, MFI requires the cmd_status to be set to MFI_STAT_INVALID_STATUS before posting.
1086  */
1087 int
1088 megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd)
1089 {
1090         struct megasas_header *frame_hdr = &cmd->frame->hdr;
1091
1092         frame_hdr->cmd_status = MFI_STAT_INVALID_STATUS;
1093         frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
1094
1095         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1096                 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1097                         __func__, __LINE__);
1098                 return DCMD_NOT_FIRED;
1099         }
1100
1101         instance->instancet->issue_dcmd(instance, cmd);
1102
1103         return wait_and_poll(instance, cmd, instance->requestorId ?
1104                         MEGASAS_ROUTINE_WAIT_TIME_VF : MFI_IO_TIMEOUT_SECS);
1105 }
1106
1107 /**
1108  * megasas_issue_blocked_cmd -  Synchronous wrapper around regular FW cmds
1109  * @instance:                   Adapter soft state
1110  * @cmd:                        Command to be issued
1111  * @timeout:                    Timeout in seconds
1112  *
1113  * This function waits on an event for the command to be returned from ISR.
1114  * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1115  * Used to issue ioctl commands.
1116  */
1117 int
1118 megasas_issue_blocked_cmd(struct megasas_instance *instance,
1119                           struct megasas_cmd *cmd, int timeout)
1120 {
1121         int ret = 0;
1122         cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1123
1124         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1125                 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1126                         __func__, __LINE__);
1127                 return DCMD_NOT_FIRED;
1128         }
1129
1130         instance->instancet->issue_dcmd(instance, cmd);
1131
1132         if (timeout) {
1133                 ret = wait_event_timeout(instance->int_cmd_wait_q,
1134                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1135                 if (!ret) {
1136                         dev_err(&instance->pdev->dev,
1137                                 "DCMD(opcode: 0x%x) is timed out, func:%s\n",
1138                                 cmd->frame->dcmd.opcode, __func__);
1139                         return DCMD_TIMEOUT;
1140                 }
1141         } else
1142                 wait_event(instance->int_cmd_wait_q,
1143                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1144
1145         return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1146                 DCMD_SUCCESS : DCMD_FAILED;
1147 }
1148
1149 /**
1150  * megasas_issue_blocked_abort_cmd -    Aborts previously issued cmd
1151  * @instance:                           Adapter soft state
1152  * @cmd_to_abort:                       Previously issued cmd to be aborted
1153  * @timeout:                            Timeout in seconds
1154  *
1155  * MFI firmware can abort previously issued AEN comamnd (automatic event
1156  * notification). The megasas_issue_blocked_abort_cmd() issues such abort
1157  * cmd and waits for return status.
1158  * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs
1159  */
1160 static int
1161 megasas_issue_blocked_abort_cmd(struct megasas_instance *instance,
1162                                 struct megasas_cmd *cmd_to_abort, int timeout)
1163 {
1164         struct megasas_cmd *cmd;
1165         struct megasas_abort_frame *abort_fr;
1166         int ret = 0;
1167         u32 opcode;
1168
1169         cmd = megasas_get_cmd(instance);
1170
1171         if (!cmd)
1172                 return -1;
1173
1174         abort_fr = &cmd->frame->abort;
1175
1176         /*
1177          * Prepare and issue the abort frame
1178          */
1179         abort_fr->cmd = MFI_CMD_ABORT;
1180         abort_fr->cmd_status = MFI_STAT_INVALID_STATUS;
1181         abort_fr->flags = cpu_to_le16(0);
1182         abort_fr->abort_context = cpu_to_le32(cmd_to_abort->index);
1183         abort_fr->abort_mfi_phys_addr_lo =
1184                 cpu_to_le32(lower_32_bits(cmd_to_abort->frame_phys_addr));
1185         abort_fr->abort_mfi_phys_addr_hi =
1186                 cpu_to_le32(upper_32_bits(cmd_to_abort->frame_phys_addr));
1187
1188         cmd->sync_cmd = 1;
1189         cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
1190
1191         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1192                 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1193                         __func__, __LINE__);
1194                 return DCMD_NOT_FIRED;
1195         }
1196
1197         instance->instancet->issue_dcmd(instance, cmd);
1198
1199         if (timeout) {
1200                 ret = wait_event_timeout(instance->abort_cmd_wait_q,
1201                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS, timeout * HZ);
1202                 if (!ret) {
1203                         opcode = cmd_to_abort->frame->dcmd.opcode;
1204                         dev_err(&instance->pdev->dev,
1205                                 "Abort(to be aborted DCMD opcode: 0x%x) is timed out func:%s\n",
1206                                 opcode,  __func__);
1207                         return DCMD_TIMEOUT;
1208                 }
1209         } else
1210                 wait_event(instance->abort_cmd_wait_q,
1211                                 cmd->cmd_status_drv != MFI_STAT_INVALID_STATUS);
1212
1213         cmd->sync_cmd = 0;
1214
1215         megasas_return_cmd(instance, cmd);
1216         return (cmd->cmd_status_drv == MFI_STAT_OK) ?
1217                 DCMD_SUCCESS : DCMD_FAILED;
1218 }
1219
1220 /**
1221  * megasas_make_sgl32 - Prepares 32-bit SGL
1222  * @instance:           Adapter soft state
1223  * @scp:                SCSI command from the mid-layer
1224  * @mfi_sgl:            SGL to be filled in
1225  *
1226  * If successful, this function returns the number of SG elements. Otherwise,
1227  * it returnes -1.
1228  */
1229 static int
1230 megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp,
1231                    union megasas_sgl *mfi_sgl)
1232 {
1233         int i;
1234         int sge_count;
1235         struct scatterlist *os_sgl;
1236
1237         sge_count = scsi_dma_map(scp);
1238         BUG_ON(sge_count < 0);
1239
1240         if (sge_count) {
1241                 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1242                         mfi_sgl->sge32[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1243                         mfi_sgl->sge32[i].phys_addr = cpu_to_le32(sg_dma_address(os_sgl));
1244                 }
1245         }
1246         return sge_count;
1247 }
1248
1249 /**
1250  * megasas_make_sgl64 - Prepares 64-bit SGL
1251  * @instance:           Adapter soft state
1252  * @scp:                SCSI command from the mid-layer
1253  * @mfi_sgl:            SGL to be filled in
1254  *
1255  * If successful, this function returns the number of SG elements. Otherwise,
1256  * it returnes -1.
1257  */
1258 static int
1259 megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp,
1260                    union megasas_sgl *mfi_sgl)
1261 {
1262         int i;
1263         int sge_count;
1264         struct scatterlist *os_sgl;
1265
1266         sge_count = scsi_dma_map(scp);
1267         BUG_ON(sge_count < 0);
1268
1269         if (sge_count) {
1270                 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1271                         mfi_sgl->sge64[i].length = cpu_to_le32(sg_dma_len(os_sgl));
1272                         mfi_sgl->sge64[i].phys_addr = cpu_to_le64(sg_dma_address(os_sgl));
1273                 }
1274         }
1275         return sge_count;
1276 }
1277
1278 /**
1279  * megasas_make_sgl_skinny - Prepares IEEE SGL
1280  * @instance:           Adapter soft state
1281  * @scp:                SCSI command from the mid-layer
1282  * @mfi_sgl:            SGL to be filled in
1283  *
1284  * If successful, this function returns the number of SG elements. Otherwise,
1285  * it returnes -1.
1286  */
1287 static int
1288 megasas_make_sgl_skinny(struct megasas_instance *instance,
1289                 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl)
1290 {
1291         int i;
1292         int sge_count;
1293         struct scatterlist *os_sgl;
1294
1295         sge_count = scsi_dma_map(scp);
1296
1297         if (sge_count) {
1298                 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1299                         mfi_sgl->sge_skinny[i].length =
1300                                 cpu_to_le32(sg_dma_len(os_sgl));
1301                         mfi_sgl->sge_skinny[i].phys_addr =
1302                                 cpu_to_le64(sg_dma_address(os_sgl));
1303                         mfi_sgl->sge_skinny[i].flag = cpu_to_le32(0);
1304                 }
1305         }
1306         return sge_count;
1307 }
1308
1309  /**
1310  * megasas_get_frame_count - Computes the number of frames
1311  * @frame_type          : type of frame- io or pthru frame
1312  * @sge_count           : number of sg elements
1313  *
1314  * Returns the number of frames required for numnber of sge's (sge_count)
1315  */
1316
1317 static u32 megasas_get_frame_count(struct megasas_instance *instance,
1318                         u8 sge_count, u8 frame_type)
1319 {
1320         int num_cnt;
1321         int sge_bytes;
1322         u32 sge_sz;
1323         u32 frame_count = 0;
1324
1325         sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) :
1326             sizeof(struct megasas_sge32);
1327
1328         if (instance->flag_ieee) {
1329                 sge_sz = sizeof(struct megasas_sge_skinny);
1330         }
1331
1332         /*
1333          * Main frame can contain 2 SGEs for 64-bit SGLs and
1334          * 3 SGEs for 32-bit SGLs for ldio &
1335          * 1 SGEs for 64-bit SGLs and
1336          * 2 SGEs for 32-bit SGLs for pthru frame
1337          */
1338         if (unlikely(frame_type == PTHRU_FRAME)) {
1339                 if (instance->flag_ieee == 1) {
1340                         num_cnt = sge_count - 1;
1341                 } else if (IS_DMA64)
1342                         num_cnt = sge_count - 1;
1343                 else
1344                         num_cnt = sge_count - 2;
1345         } else {
1346                 if (instance->flag_ieee == 1) {
1347                         num_cnt = sge_count - 1;
1348                 } else if (IS_DMA64)
1349                         num_cnt = sge_count - 2;
1350                 else
1351                         num_cnt = sge_count - 3;
1352         }
1353
1354         if (num_cnt > 0) {
1355                 sge_bytes = sge_sz * num_cnt;
1356
1357                 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) +
1358                     ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ;
1359         }
1360         /* Main frame */
1361         frame_count += 1;
1362
1363         if (frame_count > 7)
1364                 frame_count = 8;
1365         return frame_count;
1366 }
1367
1368 /**
1369  * megasas_build_dcdb - Prepares a direct cdb (DCDB) command
1370  * @instance:           Adapter soft state
1371  * @scp:                SCSI command
1372  * @cmd:                Command to be prepared in
1373  *
1374  * This function prepares CDB commands. These are typcially pass-through
1375  * commands to the devices.
1376  */
1377 static int
1378 megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp,
1379                    struct megasas_cmd *cmd)
1380 {
1381         u32 is_logical;
1382         u32 device_id;
1383         u16 flags = 0;
1384         struct megasas_pthru_frame *pthru;
1385
1386         is_logical = MEGASAS_IS_LOGICAL(scp->device);
1387         device_id = MEGASAS_DEV_INDEX(scp);
1388         pthru = (struct megasas_pthru_frame *)cmd->frame;
1389
1390         if (scp->sc_data_direction == DMA_TO_DEVICE)
1391                 flags = MFI_FRAME_DIR_WRITE;
1392         else if (scp->sc_data_direction == DMA_FROM_DEVICE)
1393                 flags = MFI_FRAME_DIR_READ;
1394         else if (scp->sc_data_direction == DMA_NONE)
1395                 flags = MFI_FRAME_DIR_NONE;
1396
1397         if (instance->flag_ieee == 1) {
1398                 flags |= MFI_FRAME_IEEE;
1399         }
1400
1401         /*
1402          * Prepare the DCDB frame
1403          */
1404         pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO;
1405         pthru->cmd_status = 0x0;
1406         pthru->scsi_status = 0x0;
1407         pthru->target_id = device_id;
1408         pthru->lun = scp->device->lun;
1409         pthru->cdb_len = scp->cmd_len;
1410         pthru->timeout = 0;
1411         pthru->pad_0 = 0;
1412         pthru->flags = cpu_to_le16(flags);
1413         pthru->data_xfer_len = cpu_to_le32(scsi_bufflen(scp));
1414
1415         memcpy(pthru->cdb, scp->cmnd, scp->cmd_len);
1416
1417         /*
1418          * If the command is for the tape device, set the
1419          * pthru timeout to the os layer timeout value.
1420          */
1421         if (scp->device->type == TYPE_TAPE) {
1422                 if ((scp->request->timeout / HZ) > 0xFFFF)
1423                         pthru->timeout = cpu_to_le16(0xFFFF);
1424                 else
1425                         pthru->timeout = cpu_to_le16(scp->request->timeout / HZ);
1426         }
1427
1428         /*
1429          * Construct SGL
1430          */
1431         if (instance->flag_ieee == 1) {
1432                 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1433                 pthru->sge_count = megasas_make_sgl_skinny(instance, scp,
1434                                                       &pthru->sgl);
1435         } else if (IS_DMA64) {
1436                 pthru->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1437                 pthru->sge_count = megasas_make_sgl64(instance, scp,
1438                                                       &pthru->sgl);
1439         } else
1440                 pthru->sge_count = megasas_make_sgl32(instance, scp,
1441                                                       &pthru->sgl);
1442
1443         if (pthru->sge_count > instance->max_num_sge) {
1444                 dev_err(&instance->pdev->dev, "DCDB too many SGE NUM=%x\n",
1445                         pthru->sge_count);
1446                 return 0;
1447         }
1448
1449         /*
1450          * Sense info specific
1451          */
1452         pthru->sense_len = SCSI_SENSE_BUFFERSIZE;
1453         pthru->sense_buf_phys_addr_hi =
1454                 cpu_to_le32(upper_32_bits(cmd->sense_phys_addr));
1455         pthru->sense_buf_phys_addr_lo =
1456                 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
1457
1458         /*
1459          * Compute the total number of frames this command consumes. FW uses
1460          * this number to pull sufficient number of frames from host memory.
1461          */
1462         cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count,
1463                                                         PTHRU_FRAME);
1464
1465         return cmd->frame_count;
1466 }
1467
1468 /**
1469  * megasas_build_ldio - Prepares IOs to logical devices
1470  * @instance:           Adapter soft state
1471  * @scp:                SCSI command
1472  * @cmd:                Command to be prepared
1473  *
1474  * Frames (and accompanying SGLs) for regular SCSI IOs use this function.
1475  */
1476 static int
1477 megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp,
1478                    struct megasas_cmd *cmd)
1479 {
1480         u32 device_id;
1481         u8 sc = scp->cmnd[0];
1482         u16 flags = 0;
1483         struct megasas_io_frame *ldio;
1484
1485         device_id = MEGASAS_DEV_INDEX(scp);
1486         ldio = (struct megasas_io_frame *)cmd->frame;
1487
1488         if (scp->sc_data_direction == DMA_TO_DEVICE)
1489                 flags = MFI_FRAME_DIR_WRITE;
1490         else if (scp->sc_data_direction == DMA_FROM_DEVICE)
1491                 flags = MFI_FRAME_DIR_READ;
1492
1493         if (instance->flag_ieee == 1) {
1494                 flags |= MFI_FRAME_IEEE;
1495         }
1496
1497         /*
1498          * Prepare the Logical IO frame: 2nd bit is zero for all read cmds
1499          */
1500         ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ;
1501         ldio->cmd_status = 0x0;
1502         ldio->scsi_status = 0x0;
1503         ldio->target_id = device_id;
1504         ldio->timeout = 0;
1505         ldio->reserved_0 = 0;
1506         ldio->pad_0 = 0;
1507         ldio->flags = cpu_to_le16(flags);
1508         ldio->start_lba_hi = 0;
1509         ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0;
1510
1511         /*
1512          * 6-byte READ(0x08) or WRITE(0x0A) cdb
1513          */
1514         if (scp->cmd_len == 6) {
1515                 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[4]);
1516                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[1] << 16) |
1517                                                  ((u32) scp->cmnd[2] << 8) |
1518                                                  (u32) scp->cmnd[3]);
1519
1520                 ldio->start_lba_lo &= cpu_to_le32(0x1FFFFF);
1521         }
1522
1523         /*
1524          * 10-byte READ(0x28) or WRITE(0x2A) cdb
1525          */
1526         else if (scp->cmd_len == 10) {
1527                 ldio->lba_count = cpu_to_le32((u32) scp->cmnd[8] |
1528                                               ((u32) scp->cmnd[7] << 8));
1529                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1530                                                  ((u32) scp->cmnd[3] << 16) |
1531                                                  ((u32) scp->cmnd[4] << 8) |
1532                                                  (u32) scp->cmnd[5]);
1533         }
1534
1535         /*
1536          * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1537          */
1538         else if (scp->cmd_len == 12) {
1539                 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1540                                               ((u32) scp->cmnd[7] << 16) |
1541                                               ((u32) scp->cmnd[8] << 8) |
1542                                               (u32) scp->cmnd[9]);
1543
1544                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1545                                                  ((u32) scp->cmnd[3] << 16) |
1546                                                  ((u32) scp->cmnd[4] << 8) |
1547                                                  (u32) scp->cmnd[5]);
1548         }
1549
1550         /*
1551          * 16-byte READ(0x88) or WRITE(0x8A) cdb
1552          */
1553         else if (scp->cmd_len == 16) {
1554                 ldio->lba_count = cpu_to_le32(((u32) scp->cmnd[10] << 24) |
1555                                               ((u32) scp->cmnd[11] << 16) |
1556                                               ((u32) scp->cmnd[12] << 8) |
1557                                               (u32) scp->cmnd[13]);
1558
1559                 ldio->start_lba_lo = cpu_to_le32(((u32) scp->cmnd[6] << 24) |
1560                                                  ((u32) scp->cmnd[7] << 16) |
1561                                                  ((u32) scp->cmnd[8] << 8) |
1562                                                  (u32) scp->cmnd[9]);
1563
1564                 ldio->start_lba_hi = cpu_to_le32(((u32) scp->cmnd[2] << 24) |
1565                                                  ((u32) scp->cmnd[3] << 16) |
1566                                                  ((u32) scp->cmnd[4] << 8) |
1567                                                  (u32) scp->cmnd[5]);
1568
1569         }
1570
1571         /*
1572          * Construct SGL
1573          */
1574         if (instance->flag_ieee) {
1575                 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1576                 ldio->sge_count = megasas_make_sgl_skinny(instance, scp,
1577                                               &ldio->sgl);
1578         } else if (IS_DMA64) {
1579                 ldio->flags |= cpu_to_le16(MFI_FRAME_SGL64);
1580                 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl);
1581         } else
1582                 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl);
1583
1584         if (ldio->sge_count > instance->max_num_sge) {
1585                 dev_err(&instance->pdev->dev, "build_ld_io: sge_count = %x\n",
1586                         ldio->sge_count);
1587                 return 0;
1588         }
1589
1590         /*
1591          * Sense info specific
1592          */
1593         ldio->sense_len = SCSI_SENSE_BUFFERSIZE;
1594         ldio->sense_buf_phys_addr_hi = 0;
1595         ldio->sense_buf_phys_addr_lo = cpu_to_le32(cmd->sense_phys_addr);
1596
1597         /*
1598          * Compute the total number of frames this command consumes. FW uses
1599          * this number to pull sufficient number of frames from host memory.
1600          */
1601         cmd->frame_count = megasas_get_frame_count(instance,
1602                         ldio->sge_count, IO_FRAME);
1603
1604         return cmd->frame_count;
1605 }
1606
1607 /**
1608  * megasas_cmd_type -           Checks if the cmd is for logical drive/sysPD
1609  *                              and whether it's RW or non RW
1610  * @scmd:                       SCSI command
1611  *
1612  */
1613 inline int megasas_cmd_type(struct scsi_cmnd *cmd)
1614 {
1615         int ret;
1616
1617         switch (cmd->cmnd[0]) {
1618         case READ_10:
1619         case WRITE_10:
1620         case READ_12:
1621         case WRITE_12:
1622         case READ_6:
1623         case WRITE_6:
1624         case READ_16:
1625         case WRITE_16:
1626                 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1627                         READ_WRITE_LDIO : READ_WRITE_SYSPDIO;
1628                 break;
1629         default:
1630                 ret = (MEGASAS_IS_LOGICAL(cmd->device)) ?
1631                         NON_READ_WRITE_LDIO : NON_READ_WRITE_SYSPDIO;
1632         }
1633         return ret;
1634 }
1635
1636  /**
1637  * megasas_dump_pending_frames -        Dumps the frame address of all pending cmds
1638  *                                      in FW
1639  * @instance:                           Adapter soft state
1640  */
1641 static inline void
1642 megasas_dump_pending_frames(struct megasas_instance *instance)
1643 {
1644         struct megasas_cmd *cmd;
1645         int i,n;
1646         union megasas_sgl *mfi_sgl;
1647         struct megasas_io_frame *ldio;
1648         struct megasas_pthru_frame *pthru;
1649         u32 sgcount;
1650         u16 max_cmd = instance->max_fw_cmds;
1651
1652         dev_err(&instance->pdev->dev, "[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no);
1653         dev_err(&instance->pdev->dev, "[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding));
1654         if (IS_DMA64)
1655                 dev_err(&instance->pdev->dev, "[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no);
1656         else
1657                 dev_err(&instance->pdev->dev, "[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no);
1658
1659         dev_err(&instance->pdev->dev, "[%d]: Pending OS cmds in FW : \n",instance->host->host_no);
1660         for (i = 0; i < max_cmd; i++) {
1661                 cmd = instance->cmd_list[i];
1662                 if (!cmd->scmd)
1663                         continue;
1664                 dev_err(&instance->pdev->dev, "[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr);
1665                 if (megasas_cmd_type(cmd->scmd) == READ_WRITE_LDIO) {
1666                         ldio = (struct megasas_io_frame *)cmd->frame;
1667                         mfi_sgl = &ldio->sgl;
1668                         sgcount = ldio->sge_count;
1669                         dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x,"
1670                         " lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1671                         instance->host->host_no, cmd->frame_count, ldio->cmd, ldio->target_id,
1672                         le32_to_cpu(ldio->start_lba_lo), le32_to_cpu(ldio->start_lba_hi),
1673                         le32_to_cpu(ldio->sense_buf_phys_addr_lo), sgcount);
1674                 } else {
1675                         pthru = (struct megasas_pthru_frame *) cmd->frame;
1676                         mfi_sgl = &pthru->sgl;
1677                         sgcount = pthru->sge_count;
1678                         dev_err(&instance->pdev->dev, "[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, "
1679                         "lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",
1680                         instance->host->host_no, cmd->frame_count, pthru->cmd, pthru->target_id,
1681                         pthru->lun, pthru->cdb_len, le32_to_cpu(pthru->data_xfer_len),
1682                         le32_to_cpu(pthru->sense_buf_phys_addr_lo), sgcount);
1683                 }
1684                 if (megasas_dbg_lvl & MEGASAS_DBG_LVL) {
1685                         for (n = 0; n < sgcount; n++) {
1686                                 if (IS_DMA64)
1687                                         dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%llx\n",
1688                                                 le32_to_cpu(mfi_sgl->sge64[n].length),
1689                                                 le64_to_cpu(mfi_sgl->sge64[n].phys_addr));
1690                                 else
1691                                         dev_err(&instance->pdev->dev, "sgl len : 0x%x, sgl addr : 0x%x\n",
1692                                                 le32_to_cpu(mfi_sgl->sge32[n].length),
1693                                                 le32_to_cpu(mfi_sgl->sge32[n].phys_addr));
1694                         }
1695                 }
1696         } /*for max_cmd*/
1697         dev_err(&instance->pdev->dev, "[%d]: Pending Internal cmds in FW : \n",instance->host->host_no);
1698         for (i = 0; i < max_cmd; i++) {
1699
1700                 cmd = instance->cmd_list[i];
1701
1702                 if (cmd->sync_cmd == 1)
1703                         dev_err(&instance->pdev->dev, "0x%08lx : ", (unsigned long)cmd->frame_phys_addr);
1704         }
1705         dev_err(&instance->pdev->dev, "[%d]: Dumping Done\n\n",instance->host->host_no);
1706 }
1707
1708 u32
1709 megasas_build_and_issue_cmd(struct megasas_instance *instance,
1710                             struct scsi_cmnd *scmd)
1711 {
1712         struct megasas_cmd *cmd;
1713         u32 frame_count;
1714
1715         cmd = megasas_get_cmd(instance);
1716         if (!cmd)
1717                 return SCSI_MLQUEUE_HOST_BUSY;
1718
1719         /*
1720          * Logical drive command
1721          */
1722         if (megasas_cmd_type(scmd) == READ_WRITE_LDIO)
1723                 frame_count = megasas_build_ldio(instance, scmd, cmd);
1724         else
1725                 frame_count = megasas_build_dcdb(instance, scmd, cmd);
1726
1727         if (!frame_count)
1728                 goto out_return_cmd;
1729
1730         cmd->scmd = scmd;
1731         scmd->SCp.ptr = (char *)cmd;
1732
1733         /*
1734          * Issue the command to the FW
1735          */
1736         atomic_inc(&instance->fw_outstanding);
1737
1738         instance->instancet->fire_cmd(instance, cmd->frame_phys_addr,
1739                                 cmd->frame_count-1, instance->reg_set);
1740
1741         return 0;
1742 out_return_cmd:
1743         megasas_return_cmd(instance, cmd);
1744         return SCSI_MLQUEUE_HOST_BUSY;
1745 }
1746
1747
1748 /**
1749  * megasas_queue_command -      Queue entry point
1750  * @scmd:                       SCSI command to be queued
1751  * @done:                       Callback entry point
1752  */
1753 static int
1754 megasas_queue_command(struct Scsi_Host *shost, struct scsi_cmnd *scmd)
1755 {
1756         struct megasas_instance *instance;
1757         struct MR_PRIV_DEVICE *mr_device_priv_data;
1758
1759         instance = (struct megasas_instance *)
1760             scmd->device->host->hostdata;
1761
1762         if (instance->unload == 1) {
1763                 scmd->result = DID_NO_CONNECT << 16;
1764                 scmd->scsi_done(scmd);
1765                 return 0;
1766         }
1767
1768         if (instance->issuepend_done == 0)
1769                 return SCSI_MLQUEUE_HOST_BUSY;
1770
1771
1772         /* Check for an mpio path and adjust behavior */
1773         if (atomic_read(&instance->adprecovery) == MEGASAS_ADPRESET_SM_INFAULT) {
1774                 if (megasas_check_mpio_paths(instance, scmd) ==
1775                     (DID_REQUEUE << 16)) {
1776                         return SCSI_MLQUEUE_HOST_BUSY;
1777                 } else {
1778                         scmd->result = DID_NO_CONNECT << 16;
1779                         scmd->scsi_done(scmd);
1780                         return 0;
1781                 }
1782         }
1783
1784         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
1785                 scmd->result = DID_NO_CONNECT << 16;
1786                 scmd->scsi_done(scmd);
1787                 return 0;
1788         }
1789
1790         mr_device_priv_data = scmd->device->hostdata;
1791         if (!mr_device_priv_data) {
1792                 scmd->result = DID_NO_CONNECT << 16;
1793                 scmd->scsi_done(scmd);
1794                 return 0;
1795         }
1796
1797         if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL)
1798                 return SCSI_MLQUEUE_HOST_BUSY;
1799
1800         if (mr_device_priv_data->tm_busy)
1801                 return SCSI_MLQUEUE_DEVICE_BUSY;
1802
1803
1804         scmd->result = 0;
1805
1806         if (MEGASAS_IS_LOGICAL(scmd->device) &&
1807             (scmd->device->id >= instance->fw_supported_vd_count ||
1808                 scmd->device->lun)) {
1809                 scmd->result = DID_BAD_TARGET << 16;
1810                 goto out_done;
1811         }
1812
1813         if ((scmd->cmnd[0] == SYNCHRONIZE_CACHE) &&
1814             MEGASAS_IS_LOGICAL(scmd->device) &&
1815             (!instance->fw_sync_cache_support)) {
1816                 scmd->result = DID_OK << 16;
1817                 goto out_done;
1818         }
1819
1820         return instance->instancet->build_and_issue_cmd(instance, scmd);
1821
1822  out_done:
1823         scmd->scsi_done(scmd);
1824         return 0;
1825 }
1826
1827 static struct megasas_instance *megasas_lookup_instance(u16 host_no)
1828 {
1829         int i;
1830
1831         for (i = 0; i < megasas_mgmt_info.max_index; i++) {
1832
1833                 if ((megasas_mgmt_info.instance[i]) &&
1834                     (megasas_mgmt_info.instance[i]->host->host_no == host_no))
1835                         return megasas_mgmt_info.instance[i];
1836         }
1837
1838         return NULL;
1839 }
1840
1841 /*
1842 * megasas_set_dynamic_target_properties -
1843 * Device property set by driver may not be static and it is required to be
1844 * updated after OCR
1845 *
1846 * set tm_capable.
1847 * set dma alignment (only for eedp protection enable vd).
1848 *
1849 * @sdev: OS provided scsi device
1850 *
1851 * Returns void
1852 */
1853 void megasas_set_dynamic_target_properties(struct scsi_device *sdev,
1854                                            bool is_target_prop)
1855 {
1856         u16 pd_index = 0, ld;
1857         u32 device_id;
1858         struct megasas_instance *instance;
1859         struct fusion_context *fusion;
1860         struct MR_PRIV_DEVICE *mr_device_priv_data;
1861         struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1862         struct MR_LD_RAID *raid;
1863         struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1864
1865         instance = megasas_lookup_instance(sdev->host->host_no);
1866         fusion = instance->ctrl_context;
1867         mr_device_priv_data = sdev->hostdata;
1868
1869         if (!fusion || !mr_device_priv_data)
1870                 return;
1871
1872         if (MEGASAS_IS_LOGICAL(sdev)) {
1873                 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
1874                                         + sdev->id;
1875                 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1876                 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1877                 if (ld >= instance->fw_supported_vd_count)
1878                         return;
1879                 raid = MR_LdRaidGet(ld, local_map_ptr);
1880
1881                 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER)
1882                 blk_queue_update_dma_alignment(sdev->request_queue, 0x7);
1883
1884                 mr_device_priv_data->is_tm_capable =
1885                         raid->capability.tmCapable;
1886         } else if (instance->use_seqnum_jbod_fp) {
1887                 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
1888                         sdev->id;
1889                 pd_sync = (void *)fusion->pd_seq_sync
1890                                 [(instance->pd_seq_map_id - 1) & 1];
1891                 mr_device_priv_data->is_tm_capable =
1892                         pd_sync->seq[pd_index].capability.tmCapable;
1893         }
1894
1895         if (is_target_prop && instance->tgt_prop->reset_tmo) {
1896                 /*
1897                  * If FW provides a target reset timeout value, driver will use
1898                  * it. If not set, fallback to default values.
1899                  */
1900                 mr_device_priv_data->target_reset_tmo =
1901                         min_t(u8, instance->max_reset_tmo,
1902                               instance->tgt_prop->reset_tmo);
1903                 mr_device_priv_data->task_abort_tmo = instance->task_abort_tmo;
1904         } else {
1905                 mr_device_priv_data->target_reset_tmo =
1906                                                 MEGASAS_DEFAULT_TM_TIMEOUT;
1907                 mr_device_priv_data->task_abort_tmo =
1908                                                 MEGASAS_DEFAULT_TM_TIMEOUT;
1909         }
1910 }
1911
1912 /*
1913  * megasas_set_nvme_device_properties -
1914  * set nomerges=2
1915  * set virtual page boundary = 4K (current mr_nvme_pg_size is 4K).
1916  * set maximum io transfer = MDTS of NVME device provided by MR firmware.
1917  *
1918  * MR firmware provides value in KB. Caller of this function converts
1919  * kb into bytes.
1920  *
1921  * e.a MDTS=5 means 2^5 * nvme page size. (In case of 4K page size,
1922  * MR firmware provides value 128 as (32 * 4K) = 128K.
1923  *
1924  * @sdev:                               scsi device
1925  * @max_io_size:                                maximum io transfer size
1926  *
1927  */
1928 static inline void
1929 megasas_set_nvme_device_properties(struct scsi_device *sdev, u32 max_io_size)
1930 {
1931         struct megasas_instance *instance;
1932         u32 mr_nvme_pg_size;
1933
1934         instance = (struct megasas_instance *)sdev->host->hostdata;
1935         mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1936                                 MR_DEFAULT_NVME_PAGE_SIZE);
1937
1938         blk_queue_max_hw_sectors(sdev->request_queue, (max_io_size / 512));
1939
1940         blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue);
1941         blk_queue_virt_boundary(sdev->request_queue, mr_nvme_pg_size - 1);
1942 }
1943
1944
1945 /*
1946  * megasas_set_static_target_properties -
1947  * Device property set by driver are static and it is not required to be
1948  * updated after OCR.
1949  *
1950  * set io timeout
1951  * set device queue depth
1952  * set nvme device properties. see - megasas_set_nvme_device_properties
1953  *
1954  * @sdev:                               scsi device
1955  * @is_target_prop                      true, if fw provided target properties.
1956  */
1957 static void megasas_set_static_target_properties(struct scsi_device *sdev,
1958                                                  bool is_target_prop)
1959 {
1960         u8 interface_type;
1961         u32 device_qd = MEGASAS_DEFAULT_CMD_PER_LUN;
1962         u32 max_io_size_kb = MR_DEFAULT_NVME_MDTS_KB;
1963         u32 tgt_device_qd;
1964         struct megasas_instance *instance;
1965         struct MR_PRIV_DEVICE *mr_device_priv_data;
1966
1967         instance = megasas_lookup_instance(sdev->host->host_no);
1968         mr_device_priv_data = sdev->hostdata;
1969         interface_type  = mr_device_priv_data->interface_type;
1970
1971         /*
1972          * The RAID firmware may require extended timeouts.
1973          */
1974         blk_queue_rq_timeout(sdev->request_queue, scmd_timeout * HZ);
1975
1976         switch (interface_type) {
1977         case SAS_PD:
1978                 device_qd = MEGASAS_SAS_QD;
1979                 break;
1980         case SATA_PD:
1981                 device_qd = MEGASAS_SATA_QD;
1982                 break;
1983         case NVME_PD:
1984                 device_qd = MEGASAS_NVME_QD;
1985                 break;
1986         }
1987
1988         if (is_target_prop) {
1989                 tgt_device_qd = le32_to_cpu(instance->tgt_prop->device_qdepth);
1990                 if (tgt_device_qd &&
1991                     (tgt_device_qd <= instance->host->can_queue))
1992                         device_qd = tgt_device_qd;
1993
1994                 /* max_io_size_kb will be set to non zero for
1995                  * nvme based vd and syspd.
1996                  */
1997                 max_io_size_kb = le32_to_cpu(instance->tgt_prop->max_io_size_kb);
1998         }
1999
2000         if (instance->nvme_page_size && max_io_size_kb)
2001                 megasas_set_nvme_device_properties(sdev, (max_io_size_kb << 10));
2002
2003         scsi_change_queue_depth(sdev, device_qd);
2004
2005 }
2006
2007
2008 static int megasas_slave_configure(struct scsi_device *sdev)
2009 {
2010         u16 pd_index = 0;
2011         struct megasas_instance *instance;
2012         int ret_target_prop = DCMD_FAILED;
2013         bool is_target_prop = false;
2014
2015         instance = megasas_lookup_instance(sdev->host->host_no);
2016         if (instance->pd_list_not_supported) {
2017                 if (!MEGASAS_IS_LOGICAL(sdev) && sdev->type == TYPE_DISK) {
2018                         pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2019                                 sdev->id;
2020                         if (instance->pd_list[pd_index].driveState !=
2021                                 MR_PD_STATE_SYSTEM)
2022                                 return -ENXIO;
2023                 }
2024         }
2025
2026         mutex_lock(&instance->reset_mutex);
2027         /* Send DCMD to Firmware and cache the information */
2028         if ((instance->pd_info) && !MEGASAS_IS_LOGICAL(sdev))
2029                 megasas_get_pd_info(instance, sdev);
2030
2031         /* Some ventura firmware may not have instance->nvme_page_size set.
2032          * Do not send MR_DCMD_DRV_GET_TARGET_PROP
2033          */
2034         if ((instance->tgt_prop) && (instance->nvme_page_size))
2035                 ret_target_prop = megasas_get_target_prop(instance, sdev);
2036
2037         is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
2038         megasas_set_static_target_properties(sdev, is_target_prop);
2039
2040         /* This sdev property may change post OCR */
2041         megasas_set_dynamic_target_properties(sdev, is_target_prop);
2042
2043         mutex_unlock(&instance->reset_mutex);
2044
2045         return 0;
2046 }
2047
2048 static int megasas_slave_alloc(struct scsi_device *sdev)
2049 {
2050         u16 pd_index = 0;
2051         struct megasas_instance *instance ;
2052         struct MR_PRIV_DEVICE *mr_device_priv_data;
2053
2054         instance = megasas_lookup_instance(sdev->host->host_no);
2055         if (!MEGASAS_IS_LOGICAL(sdev)) {
2056                 /*
2057                  * Open the OS scan to the SYSTEM PD
2058                  */
2059                 pd_index =
2060                         (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) +
2061                         sdev->id;
2062                 if ((instance->pd_list_not_supported ||
2063                         instance->pd_list[pd_index].driveState ==
2064                         MR_PD_STATE_SYSTEM)) {
2065                         goto scan_target;
2066                 }
2067                 return -ENXIO;
2068         }
2069
2070 scan_target:
2071         mr_device_priv_data = kzalloc(sizeof(*mr_device_priv_data),
2072                                         GFP_KERNEL);
2073         if (!mr_device_priv_data)
2074                 return -ENOMEM;
2075         sdev->hostdata = mr_device_priv_data;
2076
2077         atomic_set(&mr_device_priv_data->r1_ldio_hint,
2078                    instance->r1_ldio_hint_default);
2079         return 0;
2080 }
2081
2082 static void megasas_slave_destroy(struct scsi_device *sdev)
2083 {
2084         kfree(sdev->hostdata);
2085         sdev->hostdata = NULL;
2086 }
2087
2088 /*
2089 * megasas_complete_outstanding_ioctls - Complete outstanding ioctls after a
2090 *                                       kill adapter
2091 * @instance:                            Adapter soft state
2092 *
2093 */
2094 static void megasas_complete_outstanding_ioctls(struct megasas_instance *instance)
2095 {
2096         int i;
2097         struct megasas_cmd *cmd_mfi;
2098         struct megasas_cmd_fusion *cmd_fusion;
2099         struct fusion_context *fusion = instance->ctrl_context;
2100
2101         /* Find all outstanding ioctls */
2102         if (fusion) {
2103                 for (i = 0; i < instance->max_fw_cmds; i++) {
2104                         cmd_fusion = fusion->cmd_list[i];
2105                         if (cmd_fusion->sync_cmd_idx != (u32)ULONG_MAX) {
2106                                 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2107                                 if (cmd_mfi->sync_cmd &&
2108                                     (cmd_mfi->frame->hdr.cmd != MFI_CMD_ABORT)) {
2109                                         cmd_mfi->frame->hdr.cmd_status =
2110                                                         MFI_STAT_WRONG_STATE;
2111                                         megasas_complete_cmd(instance,
2112                                                              cmd_mfi, DID_OK);
2113                                 }
2114                         }
2115                 }
2116         } else {
2117                 for (i = 0; i < instance->max_fw_cmds; i++) {
2118                         cmd_mfi = instance->cmd_list[i];
2119                         if (cmd_mfi->sync_cmd && cmd_mfi->frame->hdr.cmd !=
2120                                 MFI_CMD_ABORT)
2121                                 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2122                 }
2123         }
2124 }
2125
2126
2127 void megaraid_sas_kill_hba(struct megasas_instance *instance)
2128 {
2129         /* Set critical error to block I/O & ioctls in case caller didn't */
2130         atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2131         /* Wait 1 second to ensure IO or ioctls in build have posted */
2132         msleep(1000);
2133         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) ||
2134                 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) ||
2135                 (instance->adapter_type != MFI_SERIES)) {
2136                 if (!instance->requestorId) {
2137                         writel(MFI_STOP_ADP, &instance->reg_set->doorbell);
2138                         /* Flush */
2139                         readl(&instance->reg_set->doorbell);
2140                 }
2141                 if (instance->requestorId && instance->peerIsPresent)
2142                         memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS);
2143         } else {
2144                 writel(MFI_STOP_ADP,
2145                         &instance->reg_set->inbound_doorbell);
2146         }
2147         /* Complete outstanding ioctls when adapter is killed */
2148         megasas_complete_outstanding_ioctls(instance);
2149 }
2150
2151  /**
2152   * megasas_check_and_restore_queue_depth - Check if queue depth needs to be
2153   *                                     restored to max value
2154   * @instance:                  Adapter soft state
2155   *
2156   */
2157 void
2158 megasas_check_and_restore_queue_depth(struct megasas_instance *instance)
2159 {
2160         unsigned long flags;
2161
2162         if (instance->flag & MEGASAS_FW_BUSY
2163             && time_after(jiffies, instance->last_time + 5 * HZ)
2164             && atomic_read(&instance->fw_outstanding) <
2165             instance->throttlequeuedepth + 1) {
2166
2167                 spin_lock_irqsave(instance->host->host_lock, flags);
2168                 instance->flag &= ~MEGASAS_FW_BUSY;
2169
2170                 instance->host->can_queue = instance->cur_can_queue;
2171                 spin_unlock_irqrestore(instance->host->host_lock, flags);
2172         }
2173 }
2174
2175 /**
2176  * megasas_complete_cmd_dpc      -      Returns FW's controller structure
2177  * @instance_addr:                      Address of adapter soft state
2178  *
2179  * Tasklet to complete cmds
2180  */
2181 static void megasas_complete_cmd_dpc(unsigned long instance_addr)
2182 {
2183         u32 producer;
2184         u32 consumer;
2185         u32 context;
2186         struct megasas_cmd *cmd;
2187         struct megasas_instance *instance =
2188                                 (struct megasas_instance *)instance_addr;
2189         unsigned long flags;
2190
2191         /* If we have already declared adapter dead, donot complete cmds */
2192         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
2193                 return;
2194
2195         spin_lock_irqsave(&instance->completion_lock, flags);
2196
2197         producer = le32_to_cpu(*instance->producer);
2198         consumer = le32_to_cpu(*instance->consumer);
2199
2200         while (consumer != producer) {
2201                 context = le32_to_cpu(instance->reply_queue[consumer]);
2202                 if (context >= instance->max_fw_cmds) {
2203                         dev_err(&instance->pdev->dev, "Unexpected context value %x\n",
2204                                 context);
2205                         BUG();
2206                 }
2207
2208                 cmd = instance->cmd_list[context];
2209
2210                 megasas_complete_cmd(instance, cmd, DID_OK);
2211
2212                 consumer++;
2213                 if (consumer == (instance->max_fw_cmds + 1)) {
2214                         consumer = 0;
2215                 }
2216         }
2217
2218         *instance->consumer = cpu_to_le32(producer);
2219
2220         spin_unlock_irqrestore(&instance->completion_lock, flags);
2221
2222         /*
2223          * Check if we can restore can_queue
2224          */
2225         megasas_check_and_restore_queue_depth(instance);
2226 }
2227
2228 static void megasas_sriov_heartbeat_handler(struct timer_list *t);
2229
2230 /**
2231  * megasas_start_timer - Initializes sriov heartbeat timer object
2232  * @instance:           Adapter soft state
2233  *
2234  */
2235 void megasas_start_timer(struct megasas_instance *instance)
2236 {
2237         struct timer_list *timer = &instance->sriov_heartbeat_timer;
2238
2239         timer_setup(timer, megasas_sriov_heartbeat_handler, 0);
2240         timer->expires = jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF;
2241         add_timer(timer);
2242 }
2243
2244 static void
2245 megasas_internal_reset_defer_cmds(struct megasas_instance *instance);
2246
2247 static void
2248 process_fw_state_change_wq(struct work_struct *work);
2249
2250 static void megasas_do_ocr(struct megasas_instance *instance)
2251 {
2252         if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1064R) ||
2253         (instance->pdev->device == PCI_DEVICE_ID_DELL_PERC5) ||
2254         (instance->pdev->device == PCI_DEVICE_ID_LSI_VERDE_ZCR)) {
2255                 *instance->consumer = cpu_to_le32(MEGASAS_ADPRESET_INPROG_SIGN);
2256         }
2257         instance->instancet->disable_intr(instance);
2258         atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
2259         instance->issuepend_done = 0;
2260
2261         atomic_set(&instance->fw_outstanding, 0);
2262         megasas_internal_reset_defer_cmds(instance);
2263         process_fw_state_change_wq(&instance->work_init);
2264 }
2265
2266 static int megasas_get_ld_vf_affiliation_111(struct megasas_instance *instance,
2267                                             int initial)
2268 {
2269         struct megasas_cmd *cmd;
2270         struct megasas_dcmd_frame *dcmd;
2271         struct MR_LD_VF_AFFILIATION_111 *new_affiliation_111 = NULL;
2272         dma_addr_t new_affiliation_111_h;
2273         int ld, retval = 0;
2274         u8 thisVf;
2275
2276         cmd = megasas_get_cmd(instance);
2277
2278         if (!cmd) {
2279                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation_111:"
2280                        "Failed to get cmd for scsi%d\n",
2281                         instance->host->host_no);
2282                 return -ENOMEM;
2283         }
2284
2285         dcmd = &cmd->frame->dcmd;
2286
2287         if (!instance->vf_affiliation_111) {
2288                 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2289                        "affiliation for scsi%d\n", instance->host->host_no);
2290                 megasas_return_cmd(instance, cmd);
2291                 return -ENOMEM;
2292         }
2293
2294         if (initial)
2295                         memset(instance->vf_affiliation_111, 0,
2296                                sizeof(struct MR_LD_VF_AFFILIATION_111));
2297         else {
2298                 new_affiliation_111 =
2299                         dma_alloc_coherent(&instance->pdev->dev,
2300                                            sizeof(struct MR_LD_VF_AFFILIATION_111),
2301                                            &new_affiliation_111_h, GFP_KERNEL);
2302                 if (!new_affiliation_111) {
2303                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2304                                "memory for new affiliation for scsi%d\n",
2305                                instance->host->host_no);
2306                         megasas_return_cmd(instance, cmd);
2307                         return -ENOMEM;
2308                 }
2309         }
2310
2311         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2312
2313         dcmd->cmd = MFI_CMD_DCMD;
2314         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2315         dcmd->sge_count = 1;
2316         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2317         dcmd->timeout = 0;
2318         dcmd->pad_0 = 0;
2319         dcmd->data_xfer_len =
2320                 cpu_to_le32(sizeof(struct MR_LD_VF_AFFILIATION_111));
2321         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS_111);
2322
2323         if (initial)
2324                 dcmd->sgl.sge32[0].phys_addr =
2325                         cpu_to_le32(instance->vf_affiliation_111_h);
2326         else
2327                 dcmd->sgl.sge32[0].phys_addr =
2328                         cpu_to_le32(new_affiliation_111_h);
2329
2330         dcmd->sgl.sge32[0].length = cpu_to_le32(
2331                 sizeof(struct MR_LD_VF_AFFILIATION_111));
2332
2333         dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2334                "scsi%d\n", instance->host->host_no);
2335
2336         if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2337                 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2338                        " failed with status 0x%x for scsi%d\n",
2339                        dcmd->cmd_status, instance->host->host_no);
2340                 retval = 1; /* Do a scan if we couldn't get affiliation */
2341                 goto out;
2342         }
2343
2344         if (!initial) {
2345                 thisVf = new_affiliation_111->thisVf;
2346                 for (ld = 0 ; ld < new_affiliation_111->vdCount; ld++)
2347                         if (instance->vf_affiliation_111->map[ld].policy[thisVf] !=
2348                             new_affiliation_111->map[ld].policy[thisVf]) {
2349                                 dev_warn(&instance->pdev->dev, "SR-IOV: "
2350                                        "Got new LD/VF affiliation for scsi%d\n",
2351                                        instance->host->host_no);
2352                                 memcpy(instance->vf_affiliation_111,
2353                                        new_affiliation_111,
2354                                        sizeof(struct MR_LD_VF_AFFILIATION_111));
2355                                 retval = 1;
2356                                 goto out;
2357                         }
2358         }
2359 out:
2360         if (new_affiliation_111) {
2361                 dma_free_coherent(&instance->pdev->dev,
2362                                     sizeof(struct MR_LD_VF_AFFILIATION_111),
2363                                     new_affiliation_111,
2364                                     new_affiliation_111_h);
2365         }
2366
2367         megasas_return_cmd(instance, cmd);
2368
2369         return retval;
2370 }
2371
2372 static int megasas_get_ld_vf_affiliation_12(struct megasas_instance *instance,
2373                                             int initial)
2374 {
2375         struct megasas_cmd *cmd;
2376         struct megasas_dcmd_frame *dcmd;
2377         struct MR_LD_VF_AFFILIATION *new_affiliation = NULL;
2378         struct MR_LD_VF_MAP *newmap = NULL, *savedmap = NULL;
2379         dma_addr_t new_affiliation_h;
2380         int i, j, retval = 0, found = 0, doscan = 0;
2381         u8 thisVf;
2382
2383         cmd = megasas_get_cmd(instance);
2384
2385         if (!cmd) {
2386                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_get_ld_vf_affiliation12: "
2387                        "Failed to get cmd for scsi%d\n",
2388                        instance->host->host_no);
2389                 return -ENOMEM;
2390         }
2391
2392         dcmd = &cmd->frame->dcmd;
2393
2394         if (!instance->vf_affiliation) {
2395                 dev_warn(&instance->pdev->dev, "SR-IOV: Couldn't get LD/VF "
2396                        "affiliation for scsi%d\n", instance->host->host_no);
2397                 megasas_return_cmd(instance, cmd);
2398                 return -ENOMEM;
2399         }
2400
2401         if (initial)
2402                 memset(instance->vf_affiliation, 0, (MAX_LOGICAL_DRIVES + 1) *
2403                        sizeof(struct MR_LD_VF_AFFILIATION));
2404         else {
2405                 new_affiliation =
2406                         dma_alloc_coherent(&instance->pdev->dev,
2407                                            (MAX_LOGICAL_DRIVES + 1) * sizeof(struct MR_LD_VF_AFFILIATION),
2408                                            &new_affiliation_h, GFP_KERNEL);
2409                 if (!new_affiliation) {
2410                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate "
2411                                "memory for new affiliation for scsi%d\n",
2412                                instance->host->host_no);
2413                         megasas_return_cmd(instance, cmd);
2414                         return -ENOMEM;
2415                 }
2416         }
2417
2418         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2419
2420         dcmd->cmd = MFI_CMD_DCMD;
2421         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2422         dcmd->sge_count = 1;
2423         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2424         dcmd->timeout = 0;
2425         dcmd->pad_0 = 0;
2426         dcmd->data_xfer_len = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2427                 sizeof(struct MR_LD_VF_AFFILIATION));
2428         dcmd->opcode = cpu_to_le32(MR_DCMD_LD_VF_MAP_GET_ALL_LDS);
2429
2430         if (initial)
2431                 dcmd->sgl.sge32[0].phys_addr =
2432                         cpu_to_le32(instance->vf_affiliation_h);
2433         else
2434                 dcmd->sgl.sge32[0].phys_addr =
2435                         cpu_to_le32(new_affiliation_h);
2436
2437         dcmd->sgl.sge32[0].length = cpu_to_le32((MAX_LOGICAL_DRIVES + 1) *
2438                 sizeof(struct MR_LD_VF_AFFILIATION));
2439
2440         dev_warn(&instance->pdev->dev, "SR-IOV: Getting LD/VF affiliation for "
2441                "scsi%d\n", instance->host->host_no);
2442
2443
2444         if (megasas_issue_blocked_cmd(instance, cmd, 0) != DCMD_SUCCESS) {
2445                 dev_warn(&instance->pdev->dev, "SR-IOV: LD/VF affiliation DCMD"
2446                        " failed with status 0x%x for scsi%d\n",
2447                        dcmd->cmd_status, instance->host->host_no);
2448                 retval = 1; /* Do a scan if we couldn't get affiliation */
2449                 goto out;
2450         }
2451
2452         if (!initial) {
2453                 if (!new_affiliation->ldCount) {
2454                         dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2455                                "affiliation for passive path for scsi%d\n",
2456                                instance->host->host_no);
2457                         retval = 1;
2458                         goto out;
2459                 }
2460                 newmap = new_affiliation->map;
2461                 savedmap = instance->vf_affiliation->map;
2462                 thisVf = new_affiliation->thisVf;
2463                 for (i = 0 ; i < new_affiliation->ldCount; i++) {
2464                         found = 0;
2465                         for (j = 0; j < instance->vf_affiliation->ldCount;
2466                              j++) {
2467                                 if (newmap->ref.targetId ==
2468                                     savedmap->ref.targetId) {
2469                                         found = 1;
2470                                         if (newmap->policy[thisVf] !=
2471                                             savedmap->policy[thisVf]) {
2472                                                 doscan = 1;
2473                                                 goto out;
2474                                         }
2475                                 }
2476                                 savedmap = (struct MR_LD_VF_MAP *)
2477                                         ((unsigned char *)savedmap +
2478                                          savedmap->size);
2479                         }
2480                         if (!found && newmap->policy[thisVf] !=
2481                             MR_LD_ACCESS_HIDDEN) {
2482                                 doscan = 1;
2483                                 goto out;
2484                         }
2485                         newmap = (struct MR_LD_VF_MAP *)
2486                                 ((unsigned char *)newmap + newmap->size);
2487                 }
2488
2489                 newmap = new_affiliation->map;
2490                 savedmap = instance->vf_affiliation->map;
2491
2492                 for (i = 0 ; i < instance->vf_affiliation->ldCount; i++) {
2493                         found = 0;
2494                         for (j = 0 ; j < new_affiliation->ldCount; j++) {
2495                                 if (savedmap->ref.targetId ==
2496                                     newmap->ref.targetId) {
2497                                         found = 1;
2498                                         if (savedmap->policy[thisVf] !=
2499                                             newmap->policy[thisVf]) {
2500                                                 doscan = 1;
2501                                                 goto out;
2502                                         }
2503                                 }
2504                                 newmap = (struct MR_LD_VF_MAP *)
2505                                         ((unsigned char *)newmap +
2506                                          newmap->size);
2507                         }
2508                         if (!found && savedmap->policy[thisVf] !=
2509                             MR_LD_ACCESS_HIDDEN) {
2510                                 doscan = 1;
2511                                 goto out;
2512                         }
2513                         savedmap = (struct MR_LD_VF_MAP *)
2514                                 ((unsigned char *)savedmap +
2515                                  savedmap->size);
2516                 }
2517         }
2518 out:
2519         if (doscan) {
2520                 dev_warn(&instance->pdev->dev, "SR-IOV: Got new LD/VF "
2521                        "affiliation for scsi%d\n", instance->host->host_no);
2522                 memcpy(instance->vf_affiliation, new_affiliation,
2523                        new_affiliation->size);
2524                 retval = 1;
2525         }
2526
2527         if (new_affiliation)
2528                 dma_free_coherent(&instance->pdev->dev,
2529                                     (MAX_LOGICAL_DRIVES + 1) *
2530                                     sizeof(struct MR_LD_VF_AFFILIATION),
2531                                     new_affiliation, new_affiliation_h);
2532         megasas_return_cmd(instance, cmd);
2533
2534         return retval;
2535 }
2536
2537 /* This function will get the current SR-IOV LD/VF affiliation */
2538 static int megasas_get_ld_vf_affiliation(struct megasas_instance *instance,
2539         int initial)
2540 {
2541         int retval;
2542
2543         if (instance->PlasmaFW111)
2544                 retval = megasas_get_ld_vf_affiliation_111(instance, initial);
2545         else
2546                 retval = megasas_get_ld_vf_affiliation_12(instance, initial);
2547         return retval;
2548 }
2549
2550 /* This function will tell FW to start the SR-IOV heartbeat */
2551 int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
2552                                          int initial)
2553 {
2554         struct megasas_cmd *cmd;
2555         struct megasas_dcmd_frame *dcmd;
2556         int retval = 0;
2557
2558         cmd = megasas_get_cmd(instance);
2559
2560         if (!cmd) {
2561                 dev_printk(KERN_DEBUG, &instance->pdev->dev, "megasas_sriov_start_heartbeat: "
2562                        "Failed to get cmd for scsi%d\n",
2563                        instance->host->host_no);
2564                 return -ENOMEM;
2565         }
2566
2567         dcmd = &cmd->frame->dcmd;
2568
2569         if (initial) {
2570                 instance->hb_host_mem =
2571                         dma_alloc_coherent(&instance->pdev->dev,
2572                                            sizeof(struct MR_CTRL_HB_HOST_MEM),
2573                                            &instance->hb_host_mem_h,
2574                                            GFP_KERNEL);
2575                 if (!instance->hb_host_mem) {
2576                         dev_printk(KERN_DEBUG, &instance->pdev->dev, "SR-IOV: Couldn't allocate"
2577                                " memory for heartbeat host memory for scsi%d\n",
2578                                instance->host->host_no);
2579                         retval = -ENOMEM;
2580                         goto out;
2581                 }
2582         }
2583
2584         memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
2585
2586         dcmd->mbox.s[0] = cpu_to_le16(sizeof(struct MR_CTRL_HB_HOST_MEM));
2587         dcmd->cmd = MFI_CMD_DCMD;
2588         dcmd->cmd_status = MFI_STAT_INVALID_STATUS;
2589         dcmd->sge_count = 1;
2590         dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_BOTH);
2591         dcmd->timeout = 0;
2592         dcmd->pad_0 = 0;
2593         dcmd->data_xfer_len = cpu_to_le32(sizeof(struct MR_CTRL_HB_HOST_MEM));
2594         dcmd->opcode = cpu_to_le32(MR_DCMD_CTRL_SHARED_HOST_MEM_ALLOC);
2595
2596         megasas_set_dma_settings(instance, dcmd, instance->hb_host_mem_h,
2597                                  sizeof(struct MR_CTRL_HB_HOST_MEM));
2598
2599         dev_warn(&instance->pdev->dev, "SR-IOV: Starting heartbeat for scsi%d\n",
2600                instance->host->host_no);
2601
2602         if ((instance->adapter_type != MFI_SERIES) &&
2603             !instance->mask_interrupts)
2604                 retval = megasas_issue_blocked_cmd(instance, cmd,
2605                         MEGASAS_ROUTINE_WAIT_TIME_VF);
2606         else
2607                 retval = megasas_issue_polled(instance, cmd);
2608
2609         if (retval) {
2610                 dev_warn(&instance->pdev->dev, "SR-IOV: MR_DCMD_CTRL_SHARED_HOST"
2611                         "_MEM_ALLOC DCMD %s for scsi%d\n",
2612                         (dcmd->cmd_status == MFI_STAT_INVALID_STATUS) ?
2613                         "timed out" : "failed", instance->host->host_no);
2614                 retval = 1;
2615         }
2616
2617 out:
2618         megasas_return_cmd(instance, cmd);
2619
2620         return retval;
2621 }
2622
2623 /* Handler for SR-IOV heartbeat */
2624 static void megasas_sriov_heartbeat_handler(struct timer_list *t)
2625 {
2626         struct megasas_instance *instance =
2627                 from_timer(instance, t, sriov_heartbeat_timer);
2628
2629         if (instance->hb_host_mem->HB.fwCounter !=
2630             instance->hb_host_mem->HB.driverCounter) {
2631                 instance->hb_host_mem->HB.driverCounter =
2632                         instance->hb_host_mem->HB.fwCounter;
2633                 mod_timer(&instance->sriov_heartbeat_timer,
2634                           jiffies + MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2635         } else {
2636                 dev_warn(&instance->pdev->dev, "SR-IOV: Heartbeat never "
2637                        "completed for scsi%d\n", instance->host->host_no);
2638                 schedule_work(&instance->work_init);
2639         }
2640 }
2641
2642 /**
2643  * megasas_wait_for_outstanding -       Wait for all outstanding cmds
2644  * @instance:                           Adapter soft state
2645  *
2646  * This function waits for up to MEGASAS_RESET_WAIT_TIME seconds for FW to
2647  * complete all its outstanding commands. Returns error if one or more IOs
2648  * are pending after this time period. It also marks the controller dead.
2649  */
2650 static int megasas_wait_for_outstanding(struct megasas_instance *instance)
2651 {
2652         int i, sl, outstanding;
2653         u32 reset_index;
2654         u32 wait_time = MEGASAS_RESET_WAIT_TIME;
2655         unsigned long flags;
2656         struct list_head clist_local;
2657         struct megasas_cmd *reset_cmd;
2658         u32 fw_state;
2659
2660         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2661                 dev_info(&instance->pdev->dev, "%s:%d HBA is killed.\n",
2662                 __func__, __LINE__);
2663                 return FAILED;
2664         }
2665
2666         if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2667
2668                 INIT_LIST_HEAD(&clist_local);
2669                 spin_lock_irqsave(&instance->hba_lock, flags);
2670                 list_splice_init(&instance->internal_reset_pending_q,
2671                                 &clist_local);
2672                 spin_unlock_irqrestore(&instance->hba_lock, flags);
2673
2674                 dev_notice(&instance->pdev->dev, "HBA reset wait ...\n");
2675                 for (i = 0; i < wait_time; i++) {
2676                         msleep(1000);
2677                         if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL)
2678                                 break;
2679                 }
2680
2681                 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
2682                         dev_notice(&instance->pdev->dev, "reset: Stopping HBA.\n");
2683                         atomic_set(&instance->adprecovery, MEGASAS_HW_CRITICAL_ERROR);
2684                         return FAILED;
2685                 }
2686
2687                 reset_index = 0;
2688                 while (!list_empty(&clist_local)) {
2689                         reset_cmd = list_entry((&clist_local)->next,
2690                                                 struct megasas_cmd, list);
2691                         list_del_init(&reset_cmd->list);
2692                         if (reset_cmd->scmd) {
2693                                 reset_cmd->scmd->result = DID_REQUEUE << 16;
2694                                 dev_notice(&instance->pdev->dev, "%d:%p reset [%02x]\n",
2695                                         reset_index, reset_cmd,
2696                                         reset_cmd->scmd->cmnd[0]);
2697
2698                                 reset_cmd->scmd->scsi_done(reset_cmd->scmd);
2699                                 megasas_return_cmd(instance, reset_cmd);
2700                         } else if (reset_cmd->sync_cmd) {
2701                                 dev_notice(&instance->pdev->dev, "%p synch cmds"
2702                                                 "reset queue\n",
2703                                                 reset_cmd);
2704
2705                                 reset_cmd->cmd_status_drv = MFI_STAT_INVALID_STATUS;
2706                                 instance->instancet->fire_cmd(instance,
2707                                                 reset_cmd->frame_phys_addr,
2708                                                 0, instance->reg_set);
2709                         } else {
2710                                 dev_notice(&instance->pdev->dev, "%p unexpected"
2711                                         "cmds lst\n",
2712                                         reset_cmd);
2713                         }
2714                         reset_index++;
2715                 }
2716
2717                 return SUCCESS;
2718         }
2719
2720         for (i = 0; i < resetwaittime; i++) {
2721                 outstanding = atomic_read(&instance->fw_outstanding);
2722
2723                 if (!outstanding)
2724                         break;
2725
2726                 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2727                         dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
2728                                "commands to complete\n",i,outstanding);
2729                         /*
2730                          * Call cmd completion routine. Cmd to be
2731                          * be completed directly without depending on isr.
2732                          */
2733                         megasas_complete_cmd_dpc((unsigned long)instance);
2734                 }
2735
2736                 msleep(1000);
2737         }
2738
2739         i = 0;
2740         outstanding = atomic_read(&instance->fw_outstanding);
2741         fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2742
2743         if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2744                 goto no_outstanding;
2745
2746         if (instance->disableOnlineCtrlReset)
2747                 goto kill_hba_and_failed;
2748         do {
2749                 if ((fw_state == MFI_STATE_FAULT) || atomic_read(&instance->fw_outstanding)) {
2750                         dev_info(&instance->pdev->dev,
2751                                 "%s:%d waiting_for_outstanding: before issue OCR. FW state = 0x%x, outstanding 0x%x\n",
2752                                 __func__, __LINE__, fw_state, atomic_read(&instance->fw_outstanding));
2753                         if (i == 3)
2754                                 goto kill_hba_and_failed;
2755                         megasas_do_ocr(instance);
2756
2757                         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2758                                 dev_info(&instance->pdev->dev, "%s:%d OCR failed and HBA is killed.\n",
2759                                 __func__, __LINE__);
2760                                 return FAILED;
2761                         }
2762                         dev_info(&instance->pdev->dev, "%s:%d waiting_for_outstanding: after issue OCR.\n",
2763                                 __func__, __LINE__);
2764
2765                         for (sl = 0; sl < 10; sl++)
2766                                 msleep(500);
2767
2768                         outstanding = atomic_read(&instance->fw_outstanding);
2769
2770                         fw_state = instance->instancet->read_fw_status_reg(instance) & MFI_STATE_MASK;
2771                         if ((!outstanding && (fw_state == MFI_STATE_OPERATIONAL)))
2772                                 goto no_outstanding;
2773                 }
2774                 i++;
2775         } while (i <= 3);
2776
2777 no_outstanding:
2778
2779         dev_info(&instance->pdev->dev, "%s:%d no more pending commands remain after reset handling.\n",
2780                 __func__, __LINE__);
2781         return SUCCESS;
2782
2783 kill_hba_and_failed:
2784
2785         /* Reset not supported, kill adapter */
2786         dev_info(&instance->pdev->dev, "%s:%d killing adapter scsi%d"
2787                 " disableOnlineCtrlReset %d fw_outstanding %d \n",
2788                 __func__, __LINE__, instance->host->host_no, instance->disableOnlineCtrlReset,
2789                 atomic_read(&instance->fw_outstanding));
2790         megasas_dump_pending_frames(instance);
2791         megaraid_sas_kill_hba(instance);
2792
2793         return FAILED;
2794 }
2795
2796 /**
2797  * megasas_generic_reset -      Generic reset routine
2798  * @scmd:                       Mid-layer SCSI command
2799  *
2800  * This routine implements a generic reset handler for device, bus and host
2801  * reset requests. Device, bus and host specific reset handlers can use this
2802  * function after they do their specific tasks.
2803  */
2804 static int megasas_generic_reset(struct scsi_cmnd *scmd)
2805 {
2806         int ret_val;
2807         struct megasas_instance *instance;
2808
2809         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2810
2811         scmd_printk(KERN_NOTICE, scmd, "megasas: RESET cmd=%x retries=%x\n",
2812                  scmd->cmnd[0], scmd->retries);
2813
2814         if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
2815                 dev_err(&instance->pdev->dev, "cannot recover from previous reset failures\n");
2816                 return FAILED;
2817         }
2818
2819         ret_val = megasas_wait_for_outstanding(instance);
2820         if (ret_val == SUCCESS)
2821                 dev_notice(&instance->pdev->dev, "reset successful\n");
2822         else
2823                 dev_err(&instance->pdev->dev, "failed to do reset\n");
2824
2825         return ret_val;
2826 }
2827
2828 /**
2829  * megasas_reset_timer - quiesce the adapter if required
2830  * @scmd:               scsi cmnd
2831  *
2832  * Sets the FW busy flag and reduces the host->can_queue if the
2833  * cmd has not been completed within the timeout period.
2834  */
2835 static enum
2836 blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd)
2837 {
2838         struct megasas_instance *instance;
2839         unsigned long flags;
2840
2841         if (time_after(jiffies, scmd->jiffies_at_alloc +
2842                                 (scmd_timeout * 2) * HZ)) {
2843                 return BLK_EH_DONE;
2844         }
2845
2846         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2847         if (!(instance->flag & MEGASAS_FW_BUSY)) {
2848                 /* FW is busy, throttle IO */
2849                 spin_lock_irqsave(instance->host->host_lock, flags);
2850
2851                 instance->host->can_queue = instance->throttlequeuedepth;
2852                 instance->last_time = jiffies;
2853                 instance->flag |= MEGASAS_FW_BUSY;
2854
2855                 spin_unlock_irqrestore(instance->host->host_lock, flags);
2856         }
2857         return BLK_EH_RESET_TIMER;
2858 }
2859
2860 /**
2861  * megasas_dump -       This function will print hexdump of provided buffer.
2862  * @buf:                Buffer to be dumped
2863  * @sz:         Size in bytes
2864  * @format:             Different formats of dumping e.g. format=n will
2865  *                      cause only 'n' 32 bit words to be dumped in a single
2866  *                      line.
2867  */
2868 inline void
2869 megasas_dump(void *buf, int sz, int format)
2870 {
2871         int i;
2872         __le32 *buf_loc = (__le32 *)buf;
2873
2874         for (i = 0; i < (sz / sizeof(__le32)); i++) {
2875                 if ((i % format) == 0) {
2876                         if (i != 0)
2877                                 printk(KERN_CONT "\n");
2878                         printk(KERN_CONT "%08x: ", (i * 4));
2879                 }
2880                 printk(KERN_CONT "%08x ", le32_to_cpu(buf_loc[i]));
2881         }
2882         printk(KERN_CONT "\n");
2883 }
2884
2885 /**
2886  * megasas_dump_reg_set -       This function will print hexdump of register set
2887  * @buf:                        Buffer to be dumped
2888  * @sz:                         Size in bytes
2889  * @format:                     Different formats of dumping e.g. format=n will
2890  *                              cause only 'n' 32 bit words to be dumped in a
2891  *                              single line.
2892  */
2893 inline void
2894 megasas_dump_reg_set(void __iomem *reg_set)
2895 {
2896         unsigned int i, sz = 256;
2897         u32 __iomem *reg = (u32 __iomem *)reg_set;
2898
2899         for (i = 0; i < (sz / sizeof(u32)); i++)
2900                 printk("%08x: %08x\n", (i * 4), readl(&reg[i]));
2901 }
2902
2903 /**
2904  * megasas_dump_fusion_io -     This function will print key details
2905  *                              of SCSI IO
2906  * @scmd:                       SCSI command pointer of SCSI IO
2907  */
2908 void
2909 megasas_dump_fusion_io(struct scsi_cmnd *scmd)
2910 {
2911         struct megasas_cmd_fusion *cmd;
2912         union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2913         struct megasas_instance *instance;
2914
2915         cmd = (struct megasas_cmd_fusion *)scmd->SCp.ptr;
2916         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2917
2918         scmd_printk(KERN_INFO, scmd,
2919                     "scmd: (0x%p)  retries: 0x%x  allowed: 0x%x\n",
2920                     scmd, scmd->retries, scmd->allowed);
2921         scsi_print_command(scmd);
2922
2923         if (cmd) {
2924                 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
2925                 scmd_printk(KERN_INFO, scmd, "Request descriptor details:\n");
2926                 scmd_printk(KERN_INFO, scmd,
2927                             "RequestFlags:0x%x  MSIxIndex:0x%x  SMID:0x%x  LMID:0x%x  DevHandle:0x%x\n",
2928                             req_desc->SCSIIO.RequestFlags,
2929                             req_desc->SCSIIO.MSIxIndex, req_desc->SCSIIO.SMID,
2930                             req_desc->SCSIIO.LMID, req_desc->SCSIIO.DevHandle);
2931
2932                 printk(KERN_INFO "IO request frame:\n");
2933                 megasas_dump(cmd->io_request,
2934                              MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE, 8);
2935                 printk(KERN_INFO "Chain frame:\n");
2936                 megasas_dump(cmd->sg_frame,
2937                              instance->max_chain_frame_sz, 8);
2938         }
2939
2940 }
2941
2942 /*
2943  * megasas_dump_sys_regs - This function will dump system registers through
2944  *                          sysfs.
2945  * @reg_set:                Pointer to System register set.
2946  * @buf:                    Buffer to which output is to be written.
2947  * @return:                 Number of bytes written to buffer.
2948  */
2949 static inline ssize_t
2950 megasas_dump_sys_regs(void __iomem *reg_set, char *buf)
2951 {
2952         unsigned int i, sz = 256;
2953         int bytes_wrote = 0;
2954         char *loc = (char *)buf;
2955         u32 __iomem *reg = (u32 __iomem *)reg_set;
2956
2957         for (i = 0; i < sz / sizeof(u32); i++) {
2958                 bytes_wrote += snprintf(loc + bytes_wrote, PAGE_SIZE,
2959                                         "%08x: %08x\n", (i * 4),
2960                                         readl(&reg[i]));
2961         }
2962         return bytes_wrote;
2963 }
2964
2965 /**
2966  * megasas_reset_bus_host -     Bus & host reset handler entry point
2967  */
2968 static int megasas_reset_bus_host(struct scsi_cmnd *scmd)
2969 {
2970         int ret;
2971         struct megasas_instance *instance;
2972
2973         instance = (struct megasas_instance *)scmd->device->host->hostdata;
2974
2975         scmd_printk(KERN_INFO, scmd,
2976                 "OCR is requested due to IO timeout!!\n");
2977
2978         scmd_printk(KERN_INFO, scmd,
2979                 "SCSI host state: %d  SCSI host busy: %d  FW outstanding: %d\n",
2980                 scmd->device->host->shost_state,
2981                 scsi_host_busy(scmd->device->host),
2982                 atomic_read(&instance->fw_outstanding));
2983         /*
2984          * First wait for all commands to complete
2985          */
2986         if (instance->adapter_type == MFI_SERIES) {
2987                 ret = megasas_generic_reset(scmd);
2988         } else {
2989                 megasas_dump_fusion_io(scmd);
2990                 ret = megasas_reset_fusion(scmd->device->host,
2991                                 SCSIIO_TIMEOUT_OCR);
2992         }
2993
2994         return ret;
2995 }
2996
2997 /**
2998  * megasas_task_abort - Issues task abort request to firmware
2999  *                      (supported only for fusion adapters)
3000  * @scmd:               SCSI command pointer
3001  */
3002 static int megasas_task_abort(struct scsi_cmnd *scmd)
3003 {
3004         int ret;
3005         struct megasas_instance *instance;
3006
3007         instance = (struct megasas_instance *)scmd->device->host->hostdata;
3008
3009         if (instance->adapter_type != MFI_SERIES)
3010                 ret = megasas_task_abort_fusion(scmd);
3011         else {
3012                 sdev_printk(KERN_NOTICE, scmd->device, "TASK ABORT not supported\n");
3013                 ret = FAILED;
3014         }
3015
3016         return ret;
3017 }
3018
3019 /**
3020  * megasas_reset_target:  Issues target reset request to firmware
3021  *                        (supported only for fusion adapters)
3022  * @scmd:                 SCSI command pointer
3023  */
3024 static int megasas_reset_target(struct scsi_cmnd *scmd)
3025 {
3026         int ret;
3027         struct megasas_instance *instance;
3028
3029         instance = (struct megasas_instance *)scmd->device->host->hostdata;
3030
3031         if (instance->adapter_type != MFI_SERIES)
3032                 ret = megasas_reset_target_fusion(scmd);
3033         else {
3034                 sdev_printk(KERN_NOTICE, scmd->device, "TARGET RESET not supported\n");
3035                 ret = FAILED;
3036         }
3037
3038         return ret;
3039 }