blob: a26abc041435ee2b482209f8c6487814d2899c25 [file] [log] [blame]
adam radford9c915a82010-12-21 13:34:31 -08001/*
2 * Linux MegaRAID driver for SAS based RAID controllers
3 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +05304 * Copyright (c) 2009-2013 LSI Corporation
Shivasharan S365597c2018-10-16 23:37:43 -07005 * Copyright (c) 2013-2016 Avago Technologies
6 * Copyright (c) 2016-2018 Broadcom Inc.
adam radford9c915a82010-12-21 13:34:31 -08007 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053019 * along with this program. If not, see <http://www.gnu.org/licenses/>.
adam radford9c915a82010-12-21 13:34:31 -080020 *
21 * FILE: megaraid_sas_fusion.c
22 *
Shivasharan S365597c2018-10-16 23:37:43 -070023 * Authors: Broadcom Inc.
adam radford9c915a82010-12-21 13:34:31 -080024 * Sumant Patro
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053025 * Adam Radford
Shivasharan S365597c2018-10-16 23:37:43 -070026 * Kashyap Desai <kashyap.desai@broadcom.com>
27 * Sumit Saxena <sumit.saxena@broadcom.com>
adam radford9c915a82010-12-21 13:34:31 -080028 *
Shivasharan S365597c2018-10-16 23:37:43 -070029 * Send feedback to: megaraidlinux.pdl@broadcom.com
adam radford9c915a82010-12-21 13:34:31 -080030 */
31
32#include <linux/kernel.h>
33#include <linux/types.h>
34#include <linux/pci.h>
35#include <linux/list.h>
36#include <linux/moduleparam.h>
37#include <linux/module.h>
38#include <linux/spinlock.h>
39#include <linux/interrupt.h>
40#include <linux/delay.h>
adam radford9c915a82010-12-21 13:34:31 -080041#include <linux/uio.h>
42#include <linux/uaccess.h>
43#include <linux/fs.h>
44#include <linux/compat.h>
45#include <linux/blkdev.h>
46#include <linux/mutex.h>
47#include <linux/poll.h>
Shivasharan S5fc499b2017-02-10 00:59:17 -080048#include <linux/vmalloc.h>
Shivasharan S3f6194a2018-10-16 23:37:39 -070049#include <linux/workqueue.h>
adam radford9c915a82010-12-21 13:34:31 -080050
51#include <scsi/scsi.h>
52#include <scsi/scsi_cmnd.h>
53#include <scsi/scsi_device.h>
54#include <scsi/scsi_host.h>
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +053055#include <scsi/scsi_dbg.h>
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +053056#include <linux/dmi.h>
adam radford9c915a82010-12-21 13:34:31 -080057
58#include "megaraid_sas_fusion.h"
59#include "megaraid_sas.h"
60
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +053061
adam radford9c915a82010-12-21 13:34:31 -080062extern void megasas_free_cmds(struct megasas_instance *instance);
63extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
64 *instance);
65extern void
66megasas_complete_cmd(struct megasas_instance *instance,
67 struct megasas_cmd *cmd, u8 alt_status);
adam radford9c915a82010-12-21 13:34:31 -080068int
adam radford229fe472014-03-10 02:51:56 -070069wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
70 int seconds);
adam radford9c915a82010-12-21 13:34:31 -080071
72void
73megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
74int megasas_alloc_cmds(struct megasas_instance *instance);
75int
76megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
77int
78megasas_issue_polled(struct megasas_instance *instance,
79 struct megasas_cmd *cmd);
adam radford53ef2bb2011-02-24 20:56:05 -080080void
81megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
82
adam radford058a8fa2011-10-08 18:14:27 -070083int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
adam radford9c915a82010-12-21 13:34:31 -080084void megaraid_sas_kill_hba(struct megasas_instance *instance);
85
86extern u32 megasas_dbg_lvl;
adam radford229fe472014-03-10 02:51:56 -070087int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
88 int initial);
Kees Cookc251a7b2017-10-22 15:30:04 -070089void megasas_start_timer(struct megasas_instance *instance);
adam radford229fe472014-03-10 02:51:56 -070090extern struct megasas_mgmt_info megasas_mgmt_info;
Sumit Saxenae3d178c2016-01-28 21:04:34 +053091extern unsigned int resetwaittime;
Sumit Saxena308ec452016-01-28 21:04:30 +053092extern unsigned int dual_qdepth_disable;
Sumit Saxena179ac142016-01-28 21:04:28 +053093static void megasas_free_rdpq_fusion(struct megasas_instance *instance);
94static void megasas_free_reply_fusion(struct megasas_instance *instance);
Shivasharan Se97e6732017-10-19 02:49:03 -070095static inline
96void megasas_configure_queue_sizes(struct megasas_instance *instance);
Shivasharan S3f6194a2018-10-16 23:37:39 -070097static void megasas_fusion_crash_dump(struct megasas_instance *instance);
adam radford9c915a82010-12-21 13:34:31 -080098
Shivasharan S107a60d2017-10-19 02:49:05 -070099/**
100 * megasas_check_same_4gb_region - check if allocation
101 * crosses same 4GB boundary or not
102 * @instance - adapter's soft instance
103 * start_addr - start address of DMA allocation
104 * size - size of allocation in bytes
105 * return - true : allocation does not cross same
106 * 4GB boundary
107 * false: allocation crosses same
108 * 4GB boundary
109 */
110static inline bool megasas_check_same_4gb_region
111 (struct megasas_instance *instance, dma_addr_t start_addr, size_t size)
112{
113 dma_addr_t end_addr;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +0530114
Shivasharan S107a60d2017-10-19 02:49:05 -0700115 end_addr = start_addr + size;
116
117 if (upper_32_bits(start_addr) != upper_32_bits(end_addr)) {
118 dev_err(&instance->pdev->dev,
119 "Failed to get same 4GB boundary: start_addr: 0x%llx end_addr: 0x%llx\n",
120 (unsigned long long)start_addr,
121 (unsigned long long)end_addr);
122 return false;
123 }
124
125 return true;
126}
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +0530127
adam radford9c915a82010-12-21 13:34:31 -0800128/**
129 * megasas_enable_intr_fusion - Enables interrupts
130 * @regs: MFI register set
131 */
132void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530133megasas_enable_intr_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800134{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530135 struct megasas_register_set __iomem *regs;
136 regs = instance->reg_set;
Sumit.Saxena@avagotech.comc2ced172015-01-05 20:06:13 +0530137
138 instance->mask_interrupts = 0;
adam radford6497b242011-10-08 18:14:50 -0700139 /* For Thunderbolt/Invader also clear intr on enable */
140 writel(~0, &regs->outbound_intr_status);
141 readl(&regs->outbound_intr_status);
142
adam radford9c915a82010-12-21 13:34:31 -0800143 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
144
145 /* Dummy readl to force pci flush */
146 readl(&regs->outbound_intr_mask);
147}
148
149/**
150 * megasas_disable_intr_fusion - Disables interrupt
151 * @regs: MFI register set
152 */
153void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530154megasas_disable_intr_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800155{
156 u32 mask = 0xFFFFFFFF;
157 u32 status;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530158 struct megasas_register_set __iomem *regs;
159 regs = instance->reg_set;
160 instance->mask_interrupts = 1;
adam radford9c915a82010-12-21 13:34:31 -0800161
162 writel(mask, &regs->outbound_intr_mask);
163 /* Dummy readl to force pci flush */
164 status = readl(&regs->outbound_intr_mask);
165}
166
167int
168megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
169{
170 u32 status;
171 /*
172 * Check if it is our interrupt
173 */
174 status = readl(&regs->outbound_intr_status);
175
176 if (status & 1) {
177 writel(status, &regs->outbound_intr_status);
178 readl(&regs->outbound_intr_status);
179 return 1;
180 }
181 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
182 return 0;
183
adam radford9c915a82010-12-21 13:34:31 -0800184 return 1;
185}
186
187/**
188 * megasas_get_cmd_fusion - Get a command from the free pool
189 * @instance: Adapter soft state
190 *
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530191 * Returns a blk_tag indexed mpt frame
adam radford9c915a82010-12-21 13:34:31 -0800192 */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530193inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
194 *instance, u32 blk_tag)
adam radford9c915a82010-12-21 13:34:31 -0800195{
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530196 struct fusion_context *fusion;
adam radford9c915a82010-12-21 13:34:31 -0800197
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530198 fusion = instance->ctrl_context;
199 return fusion->cmd_list[blk_tag];
adam radford9c915a82010-12-21 13:34:31 -0800200}
201
202/**
203 * megasas_return_cmd_fusion - Return a cmd to free command pool
204 * @instance: Adapter soft state
205 * @cmd: Command packet to be returned to free command pool
206 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530207inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
208 struct megasas_cmd_fusion *cmd)
adam radford9c915a82010-12-21 13:34:31 -0800209{
adam radford9c915a82010-12-21 13:34:31 -0800210 cmd->scmd = NULL;
Shivasharan S8bf7c652017-02-10 00:59:03 -0800211 memset(cmd->io_request, 0, MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
212 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
213 cmd->cmd_completed = false;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530214}
215
216/**
Christoph Hellwig01d7f03b2015-04-23 16:34:09 +0530217 * megasas_fire_cmd_fusion - Sends command to the FW
Shivasharan S45b8a352017-02-10 00:59:04 -0800218 * @instance: Adapter soft state
Shivasharan S9ff97fa2018-02-14 00:10:52 -0800219 * @req_desc: 64bit Request descriptor
Shivasharan S45b8a352017-02-10 00:59:04 -0800220 *
Shivasharan S9ff97fa2018-02-14 00:10:52 -0800221 * Perform PCI Write.
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530222 */
Shivasharan S45b8a352017-02-10 00:59:04 -0800223
Christoph Hellwig01d7f03b2015-04-23 16:34:09 +0530224static void
225megasas_fire_cmd_fusion(struct megasas_instance *instance,
Shivasharan S45b8a352017-02-10 00:59:04 -0800226 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530227{
Shivasharan S45b8a352017-02-10 00:59:04 -0800228#if defined(writeq) && defined(CONFIG_64BIT)
Shivasharan S9ff97fa2018-02-14 00:10:52 -0800229 u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
230 le32_to_cpu(req_desc->u.low));
Shivasharan S45b8a352017-02-10 00:59:04 -0800231
Shivasharan S9ff97fa2018-02-14 00:10:52 -0800232 writeq(req_data, &instance->reg_set->inbound_low_queue_port);
Shivasharan S45b8a352017-02-10 00:59:04 -0800233#else
Shivasharan S9ff97fa2018-02-14 00:10:52 -0800234 unsigned long flags;
235 spin_lock_irqsave(&instance->hba_lock, flags);
236 writel(le32_to_cpu(req_desc->u.low),
237 &instance->reg_set->inbound_low_queue_port);
238 writel(le32_to_cpu(req_desc->u.high),
239 &instance->reg_set->inbound_high_queue_port);
240 mmiowb();
241 spin_unlock_irqrestore(&instance->hba_lock, flags);
Christoph Hellwig01d7f03b2015-04-23 16:34:09 +0530242#endif
adam radford9c915a82010-12-21 13:34:31 -0800243}
244
adam radford9c915a82010-12-21 13:34:31 -0800245/**
Sumit Saxena308ec452016-01-28 21:04:30 +0530246 * megasas_fusion_update_can_queue - Do all Adapter Queue depth related calculations here
247 * @instance: Adapter soft state
248 * fw_boot_context: Whether this function called during probe or after OCR
249 *
250 * This function is only for fusion controllers.
251 * Update host can queue, if firmware downgrade max supported firmware commands.
252 * Firmware upgrade case will be skiped because underlying firmware has
253 * more resource than exposed to the OS.
254 *
255 */
256static void
257megasas_fusion_update_can_queue(struct megasas_instance *instance, int fw_boot_context)
258{
259 u16 cur_max_fw_cmds = 0;
260 u16 ldio_threshold = 0;
261 struct megasas_register_set __iomem *reg_set;
262
263 reg_set = instance->reg_set;
264
Shivasharan S81b76452018-10-16 23:37:51 -0700265 /* ventura FW does not fill outbound_scratch_pad_2 with queue depth */
Shivasharan Sf369a312017-10-19 02:48:52 -0700266 if (instance->adapter_type < VENTURA_SERIES)
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -0500267 cur_max_fw_cmds =
Shivasharan S81b76452018-10-16 23:37:51 -0700268 readl(&instance->reg_set->outbound_scratch_pad_2) & 0x00FFFF;
Sumit Saxena308ec452016-01-28 21:04:30 +0530269
270 if (dual_qdepth_disable || !cur_max_fw_cmds)
271 cur_max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
272 else
273 ldio_threshold =
274 (instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF) - MEGASAS_FUSION_IOCTL_CMDS;
275
276 dev_info(&instance->pdev->dev,
Colin Ian Kingb5c5d0a2017-11-02 10:18:10 +0000277 "Current firmware supports maximum commands: %d\t LDIO threshold: %d\n",
Shivasharan Se97e6732017-10-19 02:49:03 -0700278 cur_max_fw_cmds, ldio_threshold);
Sumit Saxena308ec452016-01-28 21:04:30 +0530279
280 if (fw_boot_context == OCR_CONTEXT) {
281 cur_max_fw_cmds = cur_max_fw_cmds - 1;
Shivasharan S013aec62017-02-10 00:59:26 -0800282 if (cur_max_fw_cmds < instance->max_fw_cmds) {
Sumit Saxena308ec452016-01-28 21:04:30 +0530283 instance->cur_can_queue =
284 cur_max_fw_cmds - (MEGASAS_FUSION_INTERNAL_CMDS +
285 MEGASAS_FUSION_IOCTL_CMDS);
286 instance->host->can_queue = instance->cur_can_queue;
287 instance->ldio_threshold = ldio_threshold;
288 }
289 } else {
290 instance->max_fw_cmds = cur_max_fw_cmds;
291 instance->ldio_threshold = ldio_threshold;
292
Sumit Saxenac3e385a2016-04-15 00:23:30 -0700293 if (reset_devices)
294 instance->max_fw_cmds = min(instance->max_fw_cmds,
295 (u16)MEGASAS_KDUMP_QUEUE_DEPTH);
Sumit Saxena308ec452016-01-28 21:04:30 +0530296 /*
297 * Reduce the max supported cmds by 1. This is to ensure that the
298 * reply_q_sz (1 more than the max cmd that driver may send)
299 * does not exceed max cmds that the FW can support
300 */
301 instance->max_fw_cmds = instance->max_fw_cmds-1;
Sumit Saxena308ec452016-01-28 21:04:30 +0530302 }
303}
304/**
adam radford9c915a82010-12-21 13:34:31 -0800305 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
306 * @instance: Adapter soft state
307 */
308void
309megasas_free_cmds_fusion(struct megasas_instance *instance)
310{
311 int i;
312 struct fusion_context *fusion = instance->ctrl_context;
Sumit Saxena179ac142016-01-28 21:04:28 +0530313 struct megasas_cmd_fusion *cmd;
adam radford9c915a82010-12-21 13:34:31 -0800314
Shivasharan S107a60d2017-10-19 02:49:05 -0700315 if (fusion->sense)
316 dma_pool_free(fusion->sense_dma_pool, fusion->sense,
317 fusion->sense_phys_addr);
318
319 /* SG */
320 if (fusion->cmd_list) {
321 for (i = 0; i < instance->max_mpt_cmds; i++) {
322 cmd = fusion->cmd_list[i];
323 if (cmd) {
324 if (cmd->sg_frame)
325 dma_pool_free(fusion->sg_dma_pool,
326 cmd->sg_frame,
327 cmd->sg_frame_phys_addr);
328 }
329 kfree(cmd);
Sumit Saxena179ac142016-01-28 21:04:28 +0530330 }
Shivasharan S107a60d2017-10-19 02:49:05 -0700331 kfree(fusion->cmd_list);
Sumit Saxena179ac142016-01-28 21:04:28 +0530332 }
333
334 if (fusion->sg_dma_pool) {
Romain Perierfc69d862017-07-06 10:13:06 +0200335 dma_pool_destroy(fusion->sg_dma_pool);
Sumit Saxena179ac142016-01-28 21:04:28 +0530336 fusion->sg_dma_pool = NULL;
337 }
338 if (fusion->sense_dma_pool) {
Romain Perierfc69d862017-07-06 10:13:06 +0200339 dma_pool_destroy(fusion->sense_dma_pool);
Sumit Saxena179ac142016-01-28 21:04:28 +0530340 fusion->sense_dma_pool = NULL;
341 }
adam radford9c915a82010-12-21 13:34:31 -0800342
343
Sumit Saxena179ac142016-01-28 21:04:28 +0530344 /* Reply Frame, Desc*/
345 if (instance->is_rdpq)
346 megasas_free_rdpq_fusion(instance);
347 else
348 megasas_free_reply_fusion(instance);
adam radford9c915a82010-12-21 13:34:31 -0800349
Sumit Saxena179ac142016-01-28 21:04:28 +0530350 /* Request Frame, Desc*/
adam radford9c915a82010-12-21 13:34:31 -0800351 if (fusion->req_frames_desc)
Sumit Saxena179ac142016-01-28 21:04:28 +0530352 dma_free_coherent(&instance->pdev->dev,
353 fusion->request_alloc_sz, fusion->req_frames_desc,
354 fusion->req_frames_desc_phys);
355 if (fusion->io_request_frames)
Romain Perierfc69d862017-07-06 10:13:06 +0200356 dma_pool_free(fusion->io_request_frames_pool,
Sumit Saxena179ac142016-01-28 21:04:28 +0530357 fusion->io_request_frames,
358 fusion->io_request_frames_phys);
359 if (fusion->io_request_frames_pool) {
Romain Perierfc69d862017-07-06 10:13:06 +0200360 dma_pool_destroy(fusion->io_request_frames_pool);
Sumit Saxena179ac142016-01-28 21:04:28 +0530361 fusion->io_request_frames_pool = NULL;
adam radford9c915a82010-12-21 13:34:31 -0800362 }
adam radford9c915a82010-12-21 13:34:31 -0800363}
364
365/**
Sumit Saxena179ac142016-01-28 21:04:28 +0530366 * megasas_create_sg_sense_fusion - Creates DMA pool for cmd frames
adam radford9c915a82010-12-21 13:34:31 -0800367 * @instance: Adapter soft state
368 *
369 */
Sumit Saxena179ac142016-01-28 21:04:28 +0530370static int megasas_create_sg_sense_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800371{
372 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -0800373 u16 max_cmd;
adam radford9c915a82010-12-21 13:34:31 -0800374 struct fusion_context *fusion;
375 struct megasas_cmd_fusion *cmd;
Shivasharan S107a60d2017-10-19 02:49:05 -0700376 int sense_sz;
377 u32 offset;
adam radford9c915a82010-12-21 13:34:31 -0800378
379 fusion = instance->ctrl_context;
380 max_cmd = instance->max_fw_cmds;
Shivasharan S107a60d2017-10-19 02:49:05 -0700381 sense_sz = instance->max_mpt_cmds * SCSI_SENSE_BUFFERSIZE;
adam radford9c915a82010-12-21 13:34:31 -0800382
Sumit Saxena179ac142016-01-28 21:04:28 +0530383 fusion->sg_dma_pool =
Romain Perierfc69d862017-07-06 10:13:06 +0200384 dma_pool_create("mr_sg", &instance->pdev->dev,
Shivasharan S15dd0382017-02-10 00:59:10 -0800385 instance->max_chain_frame_sz,
386 MR_DEFAULT_NVME_PAGE_SIZE, 0);
Sumit Saxena179ac142016-01-28 21:04:28 +0530387 /* SCSI_SENSE_BUFFERSIZE = 96 bytes */
388 fusion->sense_dma_pool =
Romain Perierfc69d862017-07-06 10:13:06 +0200389 dma_pool_create("mr_sense", &instance->pdev->dev,
Shivasharan S107a60d2017-10-19 02:49:05 -0700390 sense_sz, 64, 0);
adam radford9c915a82010-12-21 13:34:31 -0800391
Sumit Saxena179ac142016-01-28 21:04:28 +0530392 if (!fusion->sense_dma_pool || !fusion->sg_dma_pool) {
393 dev_err(&instance->pdev->dev,
394 "Failed from %s %d\n", __func__, __LINE__);
adam radford9c915a82010-12-21 13:34:31 -0800395 return -ENOMEM;
396 }
397
Shivasharan S107a60d2017-10-19 02:49:05 -0700398 fusion->sense = dma_pool_alloc(fusion->sense_dma_pool,
399 GFP_KERNEL, &fusion->sense_phys_addr);
400 if (!fusion->sense) {
401 dev_err(&instance->pdev->dev,
402 "failed from %s %d\n", __func__, __LINE__);
403 return -ENOMEM;
404 }
405
406 /* sense buffer, request frame and reply desc pool requires to be in
407 * same 4 gb region. Below function will check this.
408 * In case of failure, new pci pool will be created with updated
409 * alignment.
410 * Older allocation and pool will be destroyed.
411 * Alignment will be used such a way that next allocation if success,
412 * will always meet same 4gb region requirement.
413 * Actual requirement is not alignment, but we need start and end of
414 * DMA address must have same upper 32 bit address.
415 */
416
417 if (!megasas_check_same_4gb_region(instance, fusion->sense_phys_addr,
418 sense_sz)) {
419 dma_pool_free(fusion->sense_dma_pool, fusion->sense,
420 fusion->sense_phys_addr);
421 fusion->sense = NULL;
422 dma_pool_destroy(fusion->sense_dma_pool);
423
424 fusion->sense_dma_pool =
425 dma_pool_create("mr_sense_align", &instance->pdev->dev,
426 sense_sz, roundup_pow_of_two(sense_sz),
427 0);
428 if (!fusion->sense_dma_pool) {
429 dev_err(&instance->pdev->dev,
430 "Failed from %s %d\n", __func__, __LINE__);
431 return -ENOMEM;
432 }
433 fusion->sense = dma_pool_alloc(fusion->sense_dma_pool,
434 GFP_KERNEL,
435 &fusion->sense_phys_addr);
436 if (!fusion->sense) {
437 dev_err(&instance->pdev->dev,
438 "failed from %s %d\n", __func__, __LINE__);
439 return -ENOMEM;
440 }
441 }
442
adam radford9c915a82010-12-21 13:34:31 -0800443 /*
444 * Allocate and attach a frame to each of the commands in cmd_list
445 */
446 for (i = 0; i < max_cmd; i++) {
adam radford9c915a82010-12-21 13:34:31 -0800447 cmd = fusion->cmd_list[i];
Romain Perierfc69d862017-07-06 10:13:06 +0200448 cmd->sg_frame = dma_pool_alloc(fusion->sg_dma_pool,
Sumit Saxena179ac142016-01-28 21:04:28 +0530449 GFP_KERNEL, &cmd->sg_frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -0800450
Shivasharan S107a60d2017-10-19 02:49:05 -0700451 offset = SCSI_SENSE_BUFFERSIZE * i;
452 cmd->sense = (u8 *)fusion->sense + offset;
453 cmd->sense_phys_addr = fusion->sense_phys_addr + offset;
454
455 if (!cmd->sg_frame) {
Sumit Saxena179ac142016-01-28 21:04:28 +0530456 dev_err(&instance->pdev->dev,
457 "Failed from %s %d\n", __func__, __LINE__);
adam radford9c915a82010-12-21 13:34:31 -0800458 return -ENOMEM;
459 }
460 }
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500461
462 /* create sense buffer for the raid 1/10 fp */
463 for (i = max_cmd; i < instance->max_mpt_cmds; i++) {
464 cmd = fusion->cmd_list[i];
Shivasharan S107a60d2017-10-19 02:49:05 -0700465 offset = SCSI_SENSE_BUFFERSIZE * i;
466 cmd->sense = (u8 *)fusion->sense + offset;
467 cmd->sense_phys_addr = fusion->sense_phys_addr + offset;
468
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500469 }
470
adam radford9c915a82010-12-21 13:34:31 -0800471 return 0;
472}
473
Sumit Saxena179ac142016-01-28 21:04:28 +0530474int
475megasas_alloc_cmdlist_fusion(struct megasas_instance *instance)
476{
Shu Wang70c54e22017-07-21 18:54:09 +0800477 u32 max_mpt_cmd, i, j;
Sumit Saxena179ac142016-01-28 21:04:28 +0530478 struct fusion_context *fusion;
479
480 fusion = instance->ctrl_context;
481
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500482 max_mpt_cmd = instance->max_mpt_cmds;
Sumit Saxena179ac142016-01-28 21:04:28 +0530483
484 /*
485 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
486 * Allocate the dynamic array first and then allocate individual
487 * commands.
488 */
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500489 fusion->cmd_list =
Kees Cook6396bb22018-06-12 14:03:40 -0700490 kcalloc(max_mpt_cmd, sizeof(struct megasas_cmd_fusion *),
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500491 GFP_KERNEL);
Sumit Saxena179ac142016-01-28 21:04:28 +0530492 if (!fusion->cmd_list) {
493 dev_err(&instance->pdev->dev,
494 "Failed from %s %d\n", __func__, __LINE__);
495 return -ENOMEM;
496 }
497
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500498 for (i = 0; i < max_mpt_cmd; i++) {
Sumit Saxena179ac142016-01-28 21:04:28 +0530499 fusion->cmd_list[i] = kzalloc(sizeof(struct megasas_cmd_fusion),
500 GFP_KERNEL);
501 if (!fusion->cmd_list[i]) {
Shu Wang70c54e22017-07-21 18:54:09 +0800502 for (j = 0; j < i; j++)
503 kfree(fusion->cmd_list[j]);
504 kfree(fusion->cmd_list);
Sumit Saxena179ac142016-01-28 21:04:28 +0530505 dev_err(&instance->pdev->dev,
506 "Failed from %s %d\n", __func__, __LINE__);
507 return -ENOMEM;
508 }
509 }
Shu Wang70c54e22017-07-21 18:54:09 +0800510
Sumit Saxena179ac142016-01-28 21:04:28 +0530511 return 0;
512}
513int
514megasas_alloc_request_fusion(struct megasas_instance *instance)
515{
516 struct fusion_context *fusion;
517
518 fusion = instance->ctrl_context;
519
Shivasharan Se97e6732017-10-19 02:49:03 -0700520retry_alloc:
Sumit Saxena179ac142016-01-28 21:04:28 +0530521 fusion->io_request_frames_pool =
Romain Perierfc69d862017-07-06 10:13:06 +0200522 dma_pool_create("mr_ioreq", &instance->pdev->dev,
Sumit Saxena179ac142016-01-28 21:04:28 +0530523 fusion->io_frames_alloc_sz, 16, 0);
524
525 if (!fusion->io_request_frames_pool) {
526 dev_err(&instance->pdev->dev,
527 "Failed from %s %d\n", __func__, __LINE__);
528 return -ENOMEM;
529 }
530
531 fusion->io_request_frames =
Romain Perierfc69d862017-07-06 10:13:06 +0200532 dma_pool_alloc(fusion->io_request_frames_pool,
Sumit Saxena179ac142016-01-28 21:04:28 +0530533 GFP_KERNEL, &fusion->io_request_frames_phys);
534 if (!fusion->io_request_frames) {
Shivasharan Se97e6732017-10-19 02:49:03 -0700535 if (instance->max_fw_cmds >= (MEGASAS_REDUCE_QD_COUNT * 2)) {
536 instance->max_fw_cmds -= MEGASAS_REDUCE_QD_COUNT;
537 dma_pool_destroy(fusion->io_request_frames_pool);
538 megasas_configure_queue_sizes(instance);
539 goto retry_alloc;
540 } else {
541 dev_err(&instance->pdev->dev,
542 "Failed from %s %d\n", __func__, __LINE__);
543 return -ENOMEM;
544 }
545 }
546
Shivasharan S107a60d2017-10-19 02:49:05 -0700547 if (!megasas_check_same_4gb_region(instance,
548 fusion->io_request_frames_phys,
549 fusion->io_frames_alloc_sz)) {
550 dma_pool_free(fusion->io_request_frames_pool,
551 fusion->io_request_frames,
552 fusion->io_request_frames_phys);
553 fusion->io_request_frames = NULL;
554 dma_pool_destroy(fusion->io_request_frames_pool);
555
556 fusion->io_request_frames_pool =
557 dma_pool_create("mr_ioreq_align",
558 &instance->pdev->dev,
559 fusion->io_frames_alloc_sz,
560 roundup_pow_of_two(fusion->io_frames_alloc_sz),
561 0);
562
563 if (!fusion->io_request_frames_pool) {
564 dev_err(&instance->pdev->dev,
565 "Failed from %s %d\n", __func__, __LINE__);
566 return -ENOMEM;
567 }
568
569 fusion->io_request_frames =
570 dma_pool_alloc(fusion->io_request_frames_pool,
571 GFP_KERNEL,
572 &fusion->io_request_frames_phys);
573
574 if (!fusion->io_request_frames) {
575 dev_err(&instance->pdev->dev,
576 "Failed from %s %d\n", __func__, __LINE__);
577 return -ENOMEM;
578 }
579 }
580
Shivasharan Se97e6732017-10-19 02:49:03 -0700581 fusion->req_frames_desc =
582 dma_alloc_coherent(&instance->pdev->dev,
583 fusion->request_alloc_sz,
584 &fusion->req_frames_desc_phys, GFP_KERNEL);
585 if (!fusion->req_frames_desc) {
Sumit Saxena179ac142016-01-28 21:04:28 +0530586 dev_err(&instance->pdev->dev,
587 "Failed from %s %d\n", __func__, __LINE__);
588 return -ENOMEM;
589 }
Shivasharan Se97e6732017-10-19 02:49:03 -0700590
Sumit Saxena179ac142016-01-28 21:04:28 +0530591 return 0;
592}
593
594int
595megasas_alloc_reply_fusion(struct megasas_instance *instance)
596{
597 int i, count;
598 struct fusion_context *fusion;
599 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
600 fusion = instance->ctrl_context;
601
602 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
603 fusion->reply_frames_desc_pool =
Romain Perierfc69d862017-07-06 10:13:06 +0200604 dma_pool_create("mr_reply", &instance->pdev->dev,
Sumit Saxena179ac142016-01-28 21:04:28 +0530605 fusion->reply_alloc_sz * count, 16, 0);
606
607 if (!fusion->reply_frames_desc_pool) {
608 dev_err(&instance->pdev->dev,
609 "Failed from %s %d\n", __func__, __LINE__);
610 return -ENOMEM;
611 }
612
613 fusion->reply_frames_desc[0] =
Romain Perierfc69d862017-07-06 10:13:06 +0200614 dma_pool_alloc(fusion->reply_frames_desc_pool,
Sumit Saxena179ac142016-01-28 21:04:28 +0530615 GFP_KERNEL, &fusion->reply_frames_desc_phys[0]);
616 if (!fusion->reply_frames_desc[0]) {
617 dev_err(&instance->pdev->dev,
618 "Failed from %s %d\n", __func__, __LINE__);
619 return -ENOMEM;
620 }
Shivasharan S107a60d2017-10-19 02:49:05 -0700621
622 if (!megasas_check_same_4gb_region(instance,
623 fusion->reply_frames_desc_phys[0],
624 (fusion->reply_alloc_sz * count))) {
625 dma_pool_free(fusion->reply_frames_desc_pool,
626 fusion->reply_frames_desc[0],
627 fusion->reply_frames_desc_phys[0]);
628 fusion->reply_frames_desc[0] = NULL;
629 dma_pool_destroy(fusion->reply_frames_desc_pool);
630
631 fusion->reply_frames_desc_pool =
632 dma_pool_create("mr_reply_align",
633 &instance->pdev->dev,
634 fusion->reply_alloc_sz * count,
635 roundup_pow_of_two(fusion->reply_alloc_sz * count),
636 0);
637
638 if (!fusion->reply_frames_desc_pool) {
639 dev_err(&instance->pdev->dev,
640 "Failed from %s %d\n", __func__, __LINE__);
641 return -ENOMEM;
642 }
643
644 fusion->reply_frames_desc[0] =
645 dma_pool_alloc(fusion->reply_frames_desc_pool,
646 GFP_KERNEL,
647 &fusion->reply_frames_desc_phys[0]);
648
649 if (!fusion->reply_frames_desc[0]) {
650 dev_err(&instance->pdev->dev,
651 "Failed from %s %d\n", __func__, __LINE__);
652 return -ENOMEM;
653 }
654 }
655
Sumit Saxena179ac142016-01-28 21:04:28 +0530656 reply_desc = fusion->reply_frames_desc[0];
657 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
658 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
659
660 /* This is not a rdpq mode, but driver still populate
661 * reply_frame_desc array to use same msix index in ISR path.
662 */
663 for (i = 0; i < (count - 1); i++)
664 fusion->reply_frames_desc[i + 1] =
665 fusion->reply_frames_desc[i] +
666 (fusion->reply_alloc_sz)/sizeof(union MPI2_REPLY_DESCRIPTORS_UNION);
667
668 return 0;
669}
670
671int
672megasas_alloc_rdpq_fusion(struct megasas_instance *instance)
673{
Shivasharan S107a60d2017-10-19 02:49:05 -0700674 int i, j, k, msix_count;
Sumit Saxena179ac142016-01-28 21:04:28 +0530675 struct fusion_context *fusion;
676 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
Shivasharan S107a60d2017-10-19 02:49:05 -0700677 union MPI2_REPLY_DESCRIPTORS_UNION *rdpq_chunk_virt[RDPQ_MAX_CHUNK_COUNT];
678 dma_addr_t rdpq_chunk_phys[RDPQ_MAX_CHUNK_COUNT];
679 u8 dma_alloc_count, abs_index;
680 u32 chunk_size, array_size, offset;
Sumit Saxena179ac142016-01-28 21:04:28 +0530681
682 fusion = instance->ctrl_context;
Shivasharan S107a60d2017-10-19 02:49:05 -0700683 chunk_size = fusion->reply_alloc_sz * RDPQ_MAX_INDEX_IN_ONE_CHUNK;
684 array_size = sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) *
685 MAX_MSIX_QUEUES_FUSION;
Sumit Saxena179ac142016-01-28 21:04:28 +0530686
Christoph Hellwig60ee6522018-10-10 19:31:25 +0200687 fusion->rdpq_virt = dma_zalloc_coherent(&instance->pdev->dev,
688 array_size, &fusion->rdpq_phys, GFP_KERNEL);
Sumit Saxena179ac142016-01-28 21:04:28 +0530689 if (!fusion->rdpq_virt) {
690 dev_err(&instance->pdev->dev,
691 "Failed from %s %d\n", __func__, __LINE__);
692 return -ENOMEM;
693 }
694
Shivasharan S107a60d2017-10-19 02:49:05 -0700695 msix_count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
696
Romain Perierfc69d862017-07-06 10:13:06 +0200697 fusion->reply_frames_desc_pool = dma_pool_create("mr_rdpq",
698 &instance->pdev->dev,
Shivasharan S107a60d2017-10-19 02:49:05 -0700699 chunk_size, 16, 0);
700 fusion->reply_frames_desc_pool_align =
701 dma_pool_create("mr_rdpq_align",
702 &instance->pdev->dev,
703 chunk_size,
704 roundup_pow_of_two(chunk_size),
705 0);
Sumit Saxena179ac142016-01-28 21:04:28 +0530706
Shivasharan S107a60d2017-10-19 02:49:05 -0700707 if (!fusion->reply_frames_desc_pool ||
708 !fusion->reply_frames_desc_pool_align) {
Sumit Saxena179ac142016-01-28 21:04:28 +0530709 dev_err(&instance->pdev->dev,
710 "Failed from %s %d\n", __func__, __LINE__);
711 return -ENOMEM;
712 }
713
Shivasharan S107a60d2017-10-19 02:49:05 -0700714/*
715 * For INVADER_SERIES each set of 8 reply queues(0-7, 8-15, ..) and
716 * VENTURA_SERIES each set of 16 reply queues(0-15, 16-31, ..) should be
717 * within 4GB boundary and also reply queues in a set must have same
718 * upper 32-bits in their memory address. so here driver is allocating the
719 * DMA'able memory for reply queues according. Driver uses limitation of
720 * VENTURA_SERIES to manage INVADER_SERIES as well.
721 */
722 dma_alloc_count = DIV_ROUND_UP(msix_count, RDPQ_MAX_INDEX_IN_ONE_CHUNK);
723
724 for (i = 0; i < dma_alloc_count; i++) {
725 rdpq_chunk_virt[i] =
726 dma_pool_alloc(fusion->reply_frames_desc_pool,
727 GFP_KERNEL, &rdpq_chunk_phys[i]);
728 if (!rdpq_chunk_virt[i]) {
Sumit Saxena179ac142016-01-28 21:04:28 +0530729 dev_err(&instance->pdev->dev,
730 "Failed from %s %d\n", __func__, __LINE__);
731 return -ENOMEM;
732 }
Shivasharan S107a60d2017-10-19 02:49:05 -0700733 /* reply desc pool requires to be in same 4 gb region.
734 * Below function will check this.
735 * In case of failure, new pci pool will be created with updated
736 * alignment.
737 * For RDPQ buffers, driver always allocate two separate pci pool.
738 * Alignment will be used such a way that next allocation if
739 * success, will always meet same 4gb region requirement.
740 * rdpq_tracker keep track of each buffer's physical,
741 * virtual address and pci pool descriptor. It will help driver
742 * while freeing the resources.
743 *
744 */
745 if (!megasas_check_same_4gb_region(instance, rdpq_chunk_phys[i],
746 chunk_size)) {
747 dma_pool_free(fusion->reply_frames_desc_pool,
748 rdpq_chunk_virt[i],
749 rdpq_chunk_phys[i]);
Sumit Saxena179ac142016-01-28 21:04:28 +0530750
Shivasharan S107a60d2017-10-19 02:49:05 -0700751 rdpq_chunk_virt[i] =
752 dma_pool_alloc(fusion->reply_frames_desc_pool_align,
753 GFP_KERNEL, &rdpq_chunk_phys[i]);
754 if (!rdpq_chunk_virt[i]) {
755 dev_err(&instance->pdev->dev,
756 "Failed from %s %d\n",
757 __func__, __LINE__);
758 return -ENOMEM;
759 }
760 fusion->rdpq_tracker[i].dma_pool_ptr =
761 fusion->reply_frames_desc_pool_align;
762 } else {
763 fusion->rdpq_tracker[i].dma_pool_ptr =
764 fusion->reply_frames_desc_pool;
765 }
Sumit Saxena179ac142016-01-28 21:04:28 +0530766
Shivasharan S107a60d2017-10-19 02:49:05 -0700767 fusion->rdpq_tracker[i].pool_entry_phys = rdpq_chunk_phys[i];
768 fusion->rdpq_tracker[i].pool_entry_virt = rdpq_chunk_virt[i];
Sumit Saxena179ac142016-01-28 21:04:28 +0530769 }
Shivasharan S107a60d2017-10-19 02:49:05 -0700770
771 for (k = 0; k < dma_alloc_count; k++) {
772 for (i = 0; i < RDPQ_MAX_INDEX_IN_ONE_CHUNK; i++) {
773 abs_index = (k * RDPQ_MAX_INDEX_IN_ONE_CHUNK) + i;
774
775 if (abs_index == msix_count)
776 break;
777 offset = fusion->reply_alloc_sz * i;
778 fusion->rdpq_virt[abs_index].RDPQBaseAddress =
779 cpu_to_le64(rdpq_chunk_phys[k] + offset);
780 fusion->reply_frames_desc_phys[abs_index] =
781 rdpq_chunk_phys[k] + offset;
782 fusion->reply_frames_desc[abs_index] =
783 (union MPI2_REPLY_DESCRIPTORS_UNION *)((u8 *)rdpq_chunk_virt[k] + offset);
784
785 reply_desc = fusion->reply_frames_desc[abs_index];
786 for (j = 0; j < fusion->reply_q_depth; j++, reply_desc++)
787 reply_desc->Words = ULLONG_MAX;
788 }
789 }
790
Sumit Saxena179ac142016-01-28 21:04:28 +0530791 return 0;
792}
793
794static void
795megasas_free_rdpq_fusion(struct megasas_instance *instance) {
796
797 int i;
798 struct fusion_context *fusion;
799
800 fusion = instance->ctrl_context;
801
Shivasharan S107a60d2017-10-19 02:49:05 -0700802 for (i = 0; i < RDPQ_MAX_CHUNK_COUNT; i++) {
803 if (fusion->rdpq_tracker[i].pool_entry_virt)
804 dma_pool_free(fusion->rdpq_tracker[i].dma_pool_ptr,
805 fusion->rdpq_tracker[i].pool_entry_virt,
806 fusion->rdpq_tracker[i].pool_entry_phys);
807
Sumit Saxena179ac142016-01-28 21:04:28 +0530808 }
809
810 if (fusion->reply_frames_desc_pool)
Romain Perierfc69d862017-07-06 10:13:06 +0200811 dma_pool_destroy(fusion->reply_frames_desc_pool);
Shivasharan S107a60d2017-10-19 02:49:05 -0700812 if (fusion->reply_frames_desc_pool_align)
813 dma_pool_destroy(fusion->reply_frames_desc_pool_align);
Sumit Saxena179ac142016-01-28 21:04:28 +0530814
815 if (fusion->rdpq_virt)
Christoph Hellwig60ee6522018-10-10 19:31:25 +0200816 dma_free_coherent(&instance->pdev->dev,
Sumit Saxena179ac142016-01-28 21:04:28 +0530817 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
818 fusion->rdpq_virt, fusion->rdpq_phys);
819}
820
821static void
822megasas_free_reply_fusion(struct megasas_instance *instance) {
823
824 struct fusion_context *fusion;
825
826 fusion = instance->ctrl_context;
827
828 if (fusion->reply_frames_desc[0])
Romain Perierfc69d862017-07-06 10:13:06 +0200829 dma_pool_free(fusion->reply_frames_desc_pool,
Sumit Saxena179ac142016-01-28 21:04:28 +0530830 fusion->reply_frames_desc[0],
831 fusion->reply_frames_desc_phys[0]);
832
833 if (fusion->reply_frames_desc_pool)
Romain Perierfc69d862017-07-06 10:13:06 +0200834 dma_pool_destroy(fusion->reply_frames_desc_pool);
Sumit Saxena179ac142016-01-28 21:04:28 +0530835
836}
837
838
adam radford9c915a82010-12-21 13:34:31 -0800839/**
840 * megasas_alloc_cmds_fusion - Allocates the command packets
841 * @instance: Adapter soft state
842 *
843 *
844 * Each frame has a 32-bit field called context. This context is used to get
845 * back the megasas_cmd_fusion from the frame when a frame gets completed
846 * In this driver, the 32 bit values are the indices into an array cmd_list.
847 * This array is used only to look up the megasas_cmd_fusion given the context.
848 * The free commands themselves are maintained in a linked list called cmd_pool.
849 *
850 * cmds are formed in the io_request and sg_frame members of the
851 * megasas_cmd_fusion. The context field is used to get a request descriptor
852 * and is used as SMID of the cmd.
853 * SMID value range is from 1 to max_fw_cmds.
854 */
855int
856megasas_alloc_cmds_fusion(struct megasas_instance *instance)
857{
Sumit Saxena179ac142016-01-28 21:04:28 +0530858 int i;
adam radford9c915a82010-12-21 13:34:31 -0800859 struct fusion_context *fusion;
860 struct megasas_cmd_fusion *cmd;
adam radford9c915a82010-12-21 13:34:31 -0800861 u32 offset;
862 dma_addr_t io_req_base_phys;
863 u8 *io_req_base;
864
Sumit Saxena179ac142016-01-28 21:04:28 +0530865
adam radford9c915a82010-12-21 13:34:31 -0800866 fusion = instance->ctrl_context;
867
Sumit Saxena179ac142016-01-28 21:04:28 +0530868 if (megasas_alloc_request_fusion(instance))
869 goto fail_exit;
adam radford9c915a82010-12-21 13:34:31 -0800870
Sumit Saxena179ac142016-01-28 21:04:28 +0530871 if (instance->is_rdpq) {
872 if (megasas_alloc_rdpq_fusion(instance))
873 goto fail_exit;
874 } else
875 if (megasas_alloc_reply_fusion(instance))
876 goto fail_exit;
adam radford9c915a82010-12-21 13:34:31 -0800877
Shivasharan Se97e6732017-10-19 02:49:03 -0700878 if (megasas_alloc_cmdlist_fusion(instance))
879 goto fail_exit;
880
881 dev_info(&instance->pdev->dev, "Configured max firmware commands: %d\n",
882 instance->max_fw_cmds);
adam radford9c915a82010-12-21 13:34:31 -0800883
Sumit Saxena179ac142016-01-28 21:04:28 +0530884 /* The first 256 bytes (SMID 0) is not used. Don't add to the cmd list */
885 io_req_base = fusion->io_request_frames + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
886 io_req_base_phys = fusion->io_request_frames_phys + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
adam radford9c915a82010-12-21 13:34:31 -0800887
888 /*
889 * Add all the commands to command pool (fusion->cmd_pool)
890 */
891
892 /* SMID 0 is reserved. Set SMID/index from 1 */
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500893 for (i = 0; i < instance->max_mpt_cmds; i++) {
adam radford9c915a82010-12-21 13:34:31 -0800894 cmd = fusion->cmd_list[i];
895 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
896 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
897 cmd->index = i + 1;
898 cmd->scmd = NULL;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500899 cmd->sync_cmd_idx =
900 (i >= instance->max_scsi_cmds && i < instance->max_fw_cmds) ?
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530901 (i - instance->max_scsi_cmds) :
902 (u32)ULONG_MAX; /* Set to Invalid */
adam radford9c915a82010-12-21 13:34:31 -0800903 cmd->instance = instance;
904 cmd->io_request =
905 (struct MPI2_RAID_SCSI_IO_REQUEST *)
906 (io_req_base + offset);
907 memset(cmd->io_request, 0,
908 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
909 cmd->io_request_phys_addr = io_req_base_phys + offset;
Shivasharan S8bf7c652017-02-10 00:59:03 -0800910 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
adam radford9c915a82010-12-21 13:34:31 -0800911 }
912
Sumit Saxena179ac142016-01-28 21:04:28 +0530913 if (megasas_create_sg_sense_fusion(instance))
914 goto fail_exit;
adam radford9c915a82010-12-21 13:34:31 -0800915
916 return 0;
917
Sumit Saxena179ac142016-01-28 21:04:28 +0530918fail_exit:
919 megasas_free_cmds_fusion(instance);
adam radford9c915a82010-12-21 13:34:31 -0800920 return -ENOMEM;
921}
922
923/**
924 * wait_and_poll - Issues a polling command
925 * @instance: Adapter soft state
926 * @cmd: Command packet to be issued
927 *
928 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
929 */
930int
adam radford229fe472014-03-10 02:51:56 -0700931wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
932 int seconds)
adam radford9c915a82010-12-21 13:34:31 -0800933{
934 int i;
935 struct megasas_header *frame_hdr = &cmd->frame->hdr;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530936 struct fusion_context *fusion;
adam radford9c915a82010-12-21 13:34:31 -0800937
adam radford229fe472014-03-10 02:51:56 -0700938 u32 msecs = seconds * 1000;
adam radford9c915a82010-12-21 13:34:31 -0800939
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530940 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -0800941 /*
942 * Wait for cmd_status to change
943 */
944 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
945 rmb();
946 msleep(20);
947 }
948
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530949 if (frame_hdr->cmd_status == MFI_STAT_INVALID_STATUS)
950 return DCMD_TIMEOUT;
951 else if (frame_hdr->cmd_status == MFI_STAT_OK)
952 return DCMD_SUCCESS;
953 else
954 return DCMD_FAILED;
adam radford9c915a82010-12-21 13:34:31 -0800955}
956
957/**
958 * megasas_ioc_init_fusion - Initializes the FW
959 * @instance: Adapter soft state
960 *
961 * Issues the IOC Init cmd
962 */
963int
964megasas_ioc_init_fusion(struct megasas_instance *instance)
965{
966 struct megasas_init_frame *init_frame;
Sumit Saxena179ac142016-01-28 21:04:28 +0530967 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage = NULL;
adam radford9c915a82010-12-21 13:34:31 -0800968 dma_addr_t ioc_init_handle;
adam radford9c915a82010-12-21 13:34:31 -0800969 struct megasas_cmd *cmd;
Sumit Saxena179ac142016-01-28 21:04:28 +0530970 u8 ret, cur_rdpq_mode;
adam radford9c915a82010-12-21 13:34:31 -0800971 struct fusion_context *fusion;
adam radfordc77a9bd2014-03-10 02:51:36 -0700972 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
adam radford9c915a82010-12-21 13:34:31 -0800973 int i;
974 struct megasas_header *frame_hdr;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +0530975 const char *sys_info;
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530976 MFI_CAPABILITIES *drv_ops;
Shivasharan S81b76452018-10-16 23:37:51 -0700977 u32 scratch_pad_1;
Arnd Bergmannb45093d2018-01-17 15:48:51 +0100978 ktime_t time;
Shivasharan S107a60d2017-10-19 02:49:05 -0700979 bool cur_fw_64bit_dma_capable;
adam radford9c915a82010-12-21 13:34:31 -0800980
981 fusion = instance->ctrl_context;
982
Shivasharan S9b3d0282017-10-19 02:48:56 -0700983 ioc_init_handle = fusion->ioc_init_request_phys;
984 IOCInitMessage = fusion->ioc_init_request;
adam radford9c915a82010-12-21 13:34:31 -0800985
Shivasharan Sb9637d12017-10-19 02:49:01 -0700986 cmd = fusion->ioc_init_cmd;
adam radford9c915a82010-12-21 13:34:31 -0800987
Shivasharan S81b76452018-10-16 23:37:51 -0700988 scratch_pad_1 = readl
989 (&instance->reg_set->outbound_scratch_pad_1);
Sumit Saxena179ac142016-01-28 21:04:28 +0530990
Shivasharan S81b76452018-10-16 23:37:51 -0700991 cur_rdpq_mode = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ? 1 : 0;
Sumit Saxena179ac142016-01-28 21:04:28 +0530992
Shivasharan S107a60d2017-10-19 02:49:05 -0700993 if (instance->adapter_type == INVADER_SERIES) {
994 cur_fw_64bit_dma_capable =
Shivasharan S81b76452018-10-16 23:37:51 -0700995 (scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET) ? true : false;
Shivasharan S107a60d2017-10-19 02:49:05 -0700996
997 if (instance->consistent_mask_64bit && !cur_fw_64bit_dma_capable) {
998 dev_err(&instance->pdev->dev, "Driver was operating on 64bit "
999 "DMA mask, but upcoming FW does not support 64bit DMA mask\n");
1000 megaraid_sas_kill_hba(instance);
1001 ret = 1;
1002 goto fail_fw_init;
1003 }
1004 }
1005
Sumit Saxena179ac142016-01-28 21:04:28 +05301006 if (instance->is_rdpq && !cur_rdpq_mode) {
1007 dev_err(&instance->pdev->dev, "Firmware downgrade *NOT SUPPORTED*"
1008 " from RDPQ mode to non RDPQ mode\n");
1009 ret = 1;
1010 goto fail_fw_init;
1011 }
1012
Shivasharan S81b76452018-10-16 23:37:51 -07001013 instance->fw_sync_cache_support = (scratch_pad_1 &
Kashyap Desaid0fc91d2016-10-21 06:33:33 -07001014 MR_CAN_HANDLE_SYNC_CACHE_OFFSET) ? 1 : 0;
1015 dev_info(&instance->pdev->dev, "FW supports sync cache\t: %s\n",
1016 instance->fw_sync_cache_support ? "Yes" : "No");
1017
adam radford9c915a82010-12-21 13:34:31 -08001018 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
1019
1020 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
1021 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301022 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
1023 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
1024 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
adam radford9c915a82010-12-21 13:34:31 -08001025
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301026 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
Sumit Saxena179ac142016-01-28 21:04:28 +05301027 IOCInitMessage->ReplyDescriptorPostQueueAddress = instance->is_rdpq ?
1028 cpu_to_le64(fusion->rdpq_phys) :
1029 cpu_to_le64(fusion->reply_frames_desc_phys[0]);
1030 IOCInitMessage->MsgFlags = instance->is_rdpq ?
1031 MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE : 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301032 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
Shivasharan S107a60d2017-10-19 02:49:05 -07001033 IOCInitMessage->SenseBufferAddressHigh = cpu_to_le32(upper_32_bits(fusion->sense_phys_addr));
adam radford5738f992012-03-19 19:49:53 -07001034 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
Shivasharan S15dd0382017-02-10 00:59:10 -08001035 IOCInitMessage->HostPageSize = MR_DEFAULT_NVME_PAGE_SHIFT;
Shivasharan Sb99fc2022017-10-19 02:49:00 -07001036
Arnd Bergmannb45093d2018-01-17 15:48:51 +01001037 time = ktime_get_real();
Shivasharan Sb99fc2022017-10-19 02:49:00 -07001038 /* Convert to milliseconds as per FW requirement */
Arnd Bergmannb45093d2018-01-17 15:48:51 +01001039 IOCInitMessage->TimeStamp = cpu_to_le64(ktime_to_ms(time));
Shivasharan Sb99fc2022017-10-19 02:49:00 -07001040
adam radford9c915a82010-12-21 13:34:31 -08001041 init_frame = (struct megasas_init_frame *)cmd->frame;
Shivasharan Scb51efe2018-01-05 05:27:37 -08001042 memset(init_frame, 0, IOC_INIT_FRAME_SIZE);
adam radford9c915a82010-12-21 13:34:31 -08001043
1044 frame_hdr = &cmd->frame->hdr;
adam radford9c915a82010-12-21 13:34:31 -08001045 frame_hdr->cmd_status = 0xFF;
Shivasharan Sb52fd072018-10-16 23:37:53 -07001046 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
adam radford9c915a82010-12-21 13:34:31 -08001047
1048 init_frame->cmd = MFI_CMD_INIT;
1049 init_frame->cmd_status = 0xFF;
1050
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +05301051 drv_ops = (MFI_CAPABILITIES *) &(init_frame->driver_operations);
1052
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05301053 /* driver support Extended MSIX */
Shivasharan Sc3651782017-10-19 02:48:48 -07001054 if (instance->adapter_type >= INVADER_SERIES)
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +05301055 drv_ops->mfi_capabilities.support_additional_msix = 1;
adam radford21c9e162013-09-06 15:27:14 -07001056 /* driver supports HA / Remote LUN over Fast Path interface */
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +05301057 drv_ops->mfi_capabilities.support_fp_remote_lun = 1;
1058
1059 drv_ops->mfi_capabilities.support_max_255lds = 1;
1060 drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1;
1061 drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1;
1062
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301063 if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
1064 drv_ops->mfi_capabilities.support_ext_io_size = 1;
1065
Sumit Saxena8f050242016-01-28 21:04:27 +05301066 drv_ops->mfi_capabilities.support_fp_rlbypass = 1;
Sumit Saxena308ec452016-01-28 21:04:30 +05301067 if (!dual_qdepth_disable)
1068 drv_ops->mfi_capabilities.support_ext_queue_depth = 1;
Sumit Saxena8f050242016-01-28 21:04:27 +05301069
Sumit Saxena52b62ac2016-01-28 21:04:31 +05301070 drv_ops->mfi_capabilities.support_qd_throttling = 1;
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05001071 drv_ops->mfi_capabilities.support_pd_map_target_id = 1;
Shivasharan Sf870bcb2018-01-05 05:33:04 -08001072 drv_ops->mfi_capabilities.support_nvme_passthru = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07001073
1074 if (instance->consistent_mask_64bit)
1075 drv_ops->mfi_capabilities.support_64bit_mode = 1;
1076
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301077 /* Convert capability to LE32 */
1078 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
adam radford21c9e162013-09-06 15:27:14 -07001079
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05301080 sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
1081 if (instance->system_info_buf && sys_info) {
1082 memcpy(instance->system_info_buf->systemId, sys_info,
1083 strlen(sys_info) > 64 ? 64 : strlen(sys_info));
1084 instance->system_info_buf->systemIdLength =
1085 strlen(sys_info) > 64 ? 64 : strlen(sys_info);
Shivasharan S107a60d2017-10-19 02:49:05 -07001086 init_frame->system_info_lo = cpu_to_le32(lower_32_bits(instance->system_info_h));
1087 init_frame->system_info_hi = cpu_to_le32(upper_32_bits(instance->system_info_h));
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05301088 }
1089
Hannes Reineckefdc5a972014-01-16 11:25:33 +01001090 init_frame->queue_info_new_phys_addr_hi =
1091 cpu_to_le32(upper_32_bits(ioc_init_handle));
1092 init_frame->queue_info_new_phys_addr_lo =
1093 cpu_to_le32(lower_32_bits(ioc_init_handle));
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301094 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
adam radford9c915a82010-12-21 13:34:31 -08001095
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05301096 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
1097 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
adam radfordc77a9bd2014-03-10 02:51:36 -07001098 req_desc.MFAIo.RequestFlags =
adam radford9c915a82010-12-21 13:34:31 -08001099 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +05301100 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford9c915a82010-12-21 13:34:31 -08001101
1102 /*
1103 * disable the intr before firing the init frame
1104 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05301105 instance->instancet->disable_intr(instance);
adam radford9c915a82010-12-21 13:34:31 -08001106
1107 for (i = 0; i < (10 * 1000); i += 20) {
1108 if (readl(&instance->reg_set->doorbell) & 1)
1109 msleep(20);
1110 else
1111 break;
1112 }
1113
Shivasharan S9ff97fa2018-02-14 00:10:52 -08001114 megasas_fire_cmd_fusion(instance, &req_desc);
adam radford9c915a82010-12-21 13:34:31 -08001115
adam radford229fe472014-03-10 02:51:56 -07001116 wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -08001117
1118 frame_hdr = &cmd->frame->hdr;
1119 if (frame_hdr->cmd_status != 0) {
1120 ret = 1;
1121 goto fail_fw_init;
1122 }
adam radford9c915a82010-12-21 13:34:31 -08001123
Vinson Leefb1633d2018-03-21 21:04:12 +00001124 return 0;
adam radford9c915a82010-12-21 13:34:31 -08001125
1126fail_fw_init:
Shivasharan S2d2c2332017-08-23 04:47:06 -07001127 dev_err(&instance->pdev->dev,
Vinson Leefb1633d2018-03-21 21:04:12 +00001128 "Init cmd return status FAILED for SCSI host %d\n",
1129 instance->host->host_no);
Shivasharan S2d2c2332017-08-23 04:47:06 -07001130
adam radford9c915a82010-12-21 13:34:31 -08001131 return ret;
1132}
1133
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301134/**
1135 * megasas_sync_pd_seq_num - JBOD SEQ MAP
1136 * @instance: Adapter soft state
1137 * @pend: set to 1, if it is pended jbod map.
1138 *
1139 * Issue Jbod map to the firmware. If it is pended command,
1140 * issue command and return. If it is first instance of jbod map
1141 * issue and receive command.
1142 */
1143int
1144megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) {
1145 int ret = 0;
1146 u32 pd_seq_map_sz;
1147 struct megasas_cmd *cmd;
1148 struct megasas_dcmd_frame *dcmd;
1149 struct fusion_context *fusion = instance->ctrl_context;
1150 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1151 dma_addr_t pd_seq_h;
1152
1153 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)];
1154 pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)];
1155 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
1156 (sizeof(struct MR_PD_CFG_SEQ) *
1157 (MAX_PHYSICAL_DEVICES - 1));
1158
1159 cmd = megasas_get_cmd(instance);
1160 if (!cmd) {
1161 dev_err(&instance->pdev->dev,
1162 "Could not get mfi cmd. Fail from %s %d\n",
1163 __func__, __LINE__);
1164 return -ENOMEM;
1165 }
1166
1167 dcmd = &cmd->frame->dcmd;
1168
1169 memset(pd_sync, 0, pd_seq_map_sz);
1170 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
Shivasharan S107a60d2017-10-19 02:49:05 -07001171
1172 if (pend) {
1173 dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG;
1174 dcmd->flags = MFI_FRAME_DIR_WRITE;
1175 instance->jbod_seq_cmd = cmd;
1176 } else {
1177 dcmd->flags = MFI_FRAME_DIR_READ;
1178 }
1179
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301180 dcmd->cmd = MFI_CMD_DCMD;
1181 dcmd->cmd_status = 0xFF;
1182 dcmd->sge_count = 1;
1183 dcmd->timeout = 0;
1184 dcmd->pad_0 = 0;
1185 dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz);
1186 dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
Shivasharan S107a60d2017-10-19 02:49:05 -07001187
1188 megasas_set_dma_settings(instance, dcmd, pd_seq_h, pd_seq_map_sz);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301189
1190 if (pend) {
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301191 instance->instancet->issue_dcmd(instance, cmd);
1192 return 0;
1193 }
1194
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301195 /* Below code is only for non pended DCMD */
Shivasharan S55fecae2017-10-19 02:48:51 -07001196 if (!instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301197 ret = megasas_issue_blocked_cmd(instance, cmd,
1198 MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301199 else
1200 ret = megasas_issue_polled(instance, cmd);
1201
1202 if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
1203 dev_warn(&instance->pdev->dev,
1204 "driver supports max %d JBOD, but FW reports %d\n",
1205 MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count));
1206 ret = -EINVAL;
1207 }
1208
Shivasharan S55fecae2017-10-19 02:48:51 -07001209 if (ret == DCMD_TIMEOUT)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301210 megaraid_sas_kill_hba(instance);
1211
1212 if (ret == DCMD_SUCCESS)
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301213 instance->pd_seq_map_id++;
1214
1215 megasas_return_cmd(instance, cmd);
1216 return ret;
1217}
1218
adam radford9c915a82010-12-21 13:34:31 -08001219/*
adam radford9c915a82010-12-21 13:34:31 -08001220 * megasas_get_ld_map_info - Returns FW's ld_map structure
1221 * @instance: Adapter soft state
1222 * @pend: Pend the command or not
1223 * Issues an internal command (DCMD) to get the FW's controller PD
1224 * list structure. This information is mainly used to find out SYSTEM
1225 * supported by the FW.
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301226 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
1227 * dcmd.mbox.b[0] - number of LDs being sync'd
1228 * dcmd.mbox.b[1] - 0 - complete command immediately.
1229 * - 1 - pend till config change
1230 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
1231 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
1232 * uses extended struct MR_FW_RAID_MAP_EXT
adam radford9c915a82010-12-21 13:34:31 -08001233 */
1234static int
1235megasas_get_ld_map_info(struct megasas_instance *instance)
1236{
1237 int ret = 0;
1238 struct megasas_cmd *cmd;
1239 struct megasas_dcmd_frame *dcmd;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301240 void *ci;
adam radford9c915a82010-12-21 13:34:31 -08001241 dma_addr_t ci_h = 0;
1242 u32 size_map_info;
1243 struct fusion_context *fusion;
1244
1245 cmd = megasas_get_cmd(instance);
1246
1247 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001248 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n");
adam radford9c915a82010-12-21 13:34:31 -08001249 return -ENOMEM;
1250 }
1251
1252 fusion = instance->ctrl_context;
1253
1254 if (!fusion) {
1255 megasas_return_cmd(instance, cmd);
Hannes Reinecke2f8bdfa2014-01-16 11:25:35 +01001256 return -ENXIO;
adam radford9c915a82010-12-21 13:34:31 -08001257 }
1258
1259 dcmd = &cmd->frame->dcmd;
1260
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301261 size_map_info = fusion->current_map_sz;
adam radford9c915a82010-12-21 13:34:31 -08001262
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301263 ci = (void *) fusion->ld_map[(instance->map_id & 1)];
adam radford9c915a82010-12-21 13:34:31 -08001264 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
1265
1266 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001267 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n");
adam radford9c915a82010-12-21 13:34:31 -08001268 megasas_return_cmd(instance, cmd);
1269 return -ENOMEM;
1270 }
1271
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301272 memset(ci, 0, fusion->max_map_sz);
adam radford9c915a82010-12-21 13:34:31 -08001273 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
adam radford9c915a82010-12-21 13:34:31 -08001274 dcmd->cmd = MFI_CMD_DCMD;
1275 dcmd->cmd_status = 0xFF;
1276 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07001277 dcmd->flags = MFI_FRAME_DIR_READ;
adam radford9c915a82010-12-21 13:34:31 -08001278 dcmd->timeout = 0;
1279 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301280 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1281 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
adam radford9c915a82010-12-21 13:34:31 -08001282
Shivasharan S107a60d2017-10-19 02:49:05 -07001283 megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info);
adam radford9c915a82010-12-21 13:34:31 -08001284
Shivasharan S55fecae2017-10-19 02:48:51 -07001285 if (!instance->mask_interrupts)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301286 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301287 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301288 else
1289 ret = megasas_issue_polled(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08001290
Shivasharan S55fecae2017-10-19 02:48:51 -07001291 if (ret == DCMD_TIMEOUT)
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301292 megaraid_sas_kill_hba(instance);
1293
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05301294 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08001295
1296 return ret;
1297}
1298
1299u8
1300megasas_get_map_info(struct megasas_instance *instance)
1301{
1302 struct fusion_context *fusion = instance->ctrl_context;
1303
1304 fusion->fast_path_io = 0;
1305 if (!megasas_get_ld_map_info(instance)) {
Shivasharan S5f19f7c2018-01-05 05:27:44 -08001306 if (MR_ValidateMapInfo(instance, instance->map_id)) {
adam radford9c915a82010-12-21 13:34:31 -08001307 fusion->fast_path_io = 1;
1308 return 0;
1309 }
1310 }
1311 return 1;
1312}
1313
1314/*
1315 * megasas_sync_map_info - Returns FW's ld_map structure
1316 * @instance: Adapter soft state
1317 *
1318 * Issues an internal command (DCMD) to get the FW's controller PD
1319 * list structure. This information is mainly used to find out SYSTEM
1320 * supported by the FW.
1321 */
1322int
1323megasas_sync_map_info(struct megasas_instance *instance)
1324{
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001325 int i;
adam radford9c915a82010-12-21 13:34:31 -08001326 struct megasas_cmd *cmd;
1327 struct megasas_dcmd_frame *dcmd;
Shivasharan Sd2d03582017-02-10 00:59:19 -08001328 u16 num_lds;
1329 u32 size_sync_info;
adam radford9c915a82010-12-21 13:34:31 -08001330 struct fusion_context *fusion;
1331 struct MR_LD_TARGET_SYNC *ci = NULL;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301332 struct MR_DRV_RAID_MAP_ALL *map;
adam radford9c915a82010-12-21 13:34:31 -08001333 struct MR_LD_RAID *raid;
1334 struct MR_LD_TARGET_SYNC *ld_sync;
1335 dma_addr_t ci_h = 0;
1336 u32 size_map_info;
1337
1338 cmd = megasas_get_cmd(instance);
1339
1340 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001341 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n");
adam radford9c915a82010-12-21 13:34:31 -08001342 return -ENOMEM;
1343 }
1344
1345 fusion = instance->ctrl_context;
1346
1347 if (!fusion) {
1348 megasas_return_cmd(instance, cmd);
1349 return 1;
1350 }
1351
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301352 map = fusion->ld_drv_map[instance->map_id & 1];
adam radford9c915a82010-12-21 13:34:31 -08001353
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301354 num_lds = le16_to_cpu(map->raidMap.ldCount);
adam radford9c915a82010-12-21 13:34:31 -08001355
1356 dcmd = &cmd->frame->dcmd;
1357
1358 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
1359
1360 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1361
1362 ci = (struct MR_LD_TARGET_SYNC *)
1363 fusion->ld_map[(instance->map_id - 1) & 1];
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301364 memset(ci, 0, fusion->max_map_sz);
adam radford9c915a82010-12-21 13:34:31 -08001365
1366 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
1367
1368 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
1369
1370 for (i = 0; i < num_lds; i++, ld_sync++) {
1371 raid = MR_LdRaidGet(i, map);
1372 ld_sync->targetId = MR_GetLDTgtId(i, map);
1373 ld_sync->seqNum = raid->seqNum;
1374 }
1375
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301376 size_map_info = fusion->current_map_sz;
adam radford9c915a82010-12-21 13:34:31 -08001377
1378 dcmd->cmd = MFI_CMD_DCMD;
1379 dcmd->cmd_status = 0xFF;
1380 dcmd->sge_count = 1;
Shivasharan S107a60d2017-10-19 02:49:05 -07001381 dcmd->flags = MFI_FRAME_DIR_WRITE;
adam radford9c915a82010-12-21 13:34:31 -08001382 dcmd->timeout = 0;
1383 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301384 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -08001385 dcmd->mbox.b[0] = num_lds;
1386 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301387 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
Shivasharan S107a60d2017-10-19 02:49:05 -07001388
1389 megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info);
adam radford9c915a82010-12-21 13:34:31 -08001390
1391 instance->map_update_cmd = cmd;
1392
1393 instance->instancet->issue_dcmd(instance, cmd);
1394
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001395 return 0;
adam radford9c915a82010-12-21 13:34:31 -08001396}
1397
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301398/*
1399 * meagasas_display_intel_branding - Display branding string
1400 * @instance: per adapter object
1401 *
1402 * Return nothing.
1403 */
1404static void
1405megasas_display_intel_branding(struct megasas_instance *instance)
1406{
1407 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
1408 return;
1409
1410 switch (instance->pdev->device) {
1411 case PCI_DEVICE_ID_LSI_INVADER:
1412 switch (instance->pdev->subsystem_device) {
1413 case MEGARAID_INTEL_RS3DC080_SSDID:
1414 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1415 instance->host->host_no,
1416 MEGARAID_INTEL_RS3DC080_BRANDING);
1417 break;
1418 case MEGARAID_INTEL_RS3DC040_SSDID:
1419 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1420 instance->host->host_no,
1421 MEGARAID_INTEL_RS3DC040_BRANDING);
1422 break;
1423 case MEGARAID_INTEL_RS3SC008_SSDID:
1424 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1425 instance->host->host_no,
1426 MEGARAID_INTEL_RS3SC008_BRANDING);
1427 break;
1428 case MEGARAID_INTEL_RS3MC044_SSDID:
1429 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1430 instance->host->host_no,
1431 MEGARAID_INTEL_RS3MC044_BRANDING);
1432 break;
1433 default:
1434 break;
1435 }
1436 break;
1437 case PCI_DEVICE_ID_LSI_FURY:
1438 switch (instance->pdev->subsystem_device) {
1439 case MEGARAID_INTEL_RS3WC080_SSDID:
1440 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1441 instance->host->host_no,
1442 MEGARAID_INTEL_RS3WC080_BRANDING);
1443 break;
1444 case MEGARAID_INTEL_RS3WC040_SSDID:
1445 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1446 instance->host->host_no,
1447 MEGARAID_INTEL_RS3WC040_BRANDING);
1448 break;
1449 default:
1450 break;
1451 }
1452 break;
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +05301453 case PCI_DEVICE_ID_LSI_CUTLASS_52:
1454 case PCI_DEVICE_ID_LSI_CUTLASS_53:
1455 switch (instance->pdev->subsystem_device) {
1456 case MEGARAID_INTEL_RMS3BC160_SSDID:
1457 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1458 instance->host->host_no,
1459 MEGARAID_INTEL_RMS3BC160_BRANDING);
1460 break;
1461 default:
1462 break;
1463 }
1464 break;
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301465 default:
1466 break;
1467 }
1468}
1469
adam radford9c915a82010-12-21 13:34:31 -08001470/**
Shivasharan Sdef3e8d2017-08-23 04:47:03 -07001471 * megasas_allocate_raid_maps - Allocate memory for RAID maps
1472 * @instance: Adapter soft state
1473 *
1474 * return: if success: return 0
1475 * failed: return -ENOMEM
1476 */
1477static inline int megasas_allocate_raid_maps(struct megasas_instance *instance)
1478{
1479 struct fusion_context *fusion;
1480 int i = 0;
1481
1482 fusion = instance->ctrl_context;
1483
1484 fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1485
1486 for (i = 0; i < 2; i++) {
1487 fusion->ld_map[i] = NULL;
1488
1489 fusion->ld_drv_map[i] = (void *)
1490 __get_free_pages(__GFP_ZERO | GFP_KERNEL,
1491 fusion->drv_map_pages);
1492
1493 if (!fusion->ld_drv_map[i]) {
1494 fusion->ld_drv_map[i] = vzalloc(fusion->drv_map_sz);
1495
1496 if (!fusion->ld_drv_map[i]) {
1497 dev_err(&instance->pdev->dev,
1498 "Could not allocate memory for local map"
1499 " size requested: %d\n",
1500 fusion->drv_map_sz);
1501 goto ld_drv_map_alloc_fail;
1502 }
1503 }
1504 }
1505
1506 for (i = 0; i < 2; i++) {
1507 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
1508 fusion->max_map_sz,
1509 &fusion->ld_map_phys[i],
1510 GFP_KERNEL);
1511 if (!fusion->ld_map[i]) {
1512 dev_err(&instance->pdev->dev,
1513 "Could not allocate memory for map info %s:%d\n",
1514 __func__, __LINE__);
1515 goto ld_map_alloc_fail;
1516 }
1517 }
1518
1519 return 0;
1520
1521ld_map_alloc_fail:
1522 for (i = 0; i < 2; i++) {
1523 if (fusion->ld_map[i])
1524 dma_free_coherent(&instance->pdev->dev,
1525 fusion->max_map_sz,
1526 fusion->ld_map[i],
1527 fusion->ld_map_phys[i]);
1528 }
1529
1530ld_drv_map_alloc_fail:
1531 for (i = 0; i < 2; i++) {
1532 if (fusion->ld_drv_map[i]) {
1533 if (is_vmalloc_addr(fusion->ld_drv_map[i]))
1534 vfree(fusion->ld_drv_map[i]);
1535 else
1536 free_pages((ulong)fusion->ld_drv_map[i],
1537 fusion->drv_map_pages);
1538 }
1539 }
1540
1541 return -ENOMEM;
1542}
1543
1544/**
Shivasharan Se97e6732017-10-19 02:49:03 -07001545 * megasas_configure_queue_sizes - Calculate size of request desc queue,
1546 * reply desc queue,
1547 * IO request frame queue, set can_queue.
1548 * @instance: Adapter soft state
1549 * @return: void
1550 */
1551static inline
1552void megasas_configure_queue_sizes(struct megasas_instance *instance)
1553{
1554 struct fusion_context *fusion;
1555 u16 max_cmd;
1556
1557 fusion = instance->ctrl_context;
1558 max_cmd = instance->max_fw_cmds;
1559
1560 if (instance->adapter_type == VENTURA_SERIES)
1561 instance->max_mpt_cmds = instance->max_fw_cmds * RAID_1_PEER_CMDS;
1562 else
1563 instance->max_mpt_cmds = instance->max_fw_cmds;
1564
Shivasharan S9fb98562018-10-16 23:37:52 -07001565 instance->max_scsi_cmds = instance->max_fw_cmds - instance->max_mfi_cmds;
Shivasharan Se97e6732017-10-19 02:49:03 -07001566 instance->cur_can_queue = instance->max_scsi_cmds;
1567 instance->host->can_queue = instance->cur_can_queue;
1568
1569 fusion->reply_q_depth = 2 * ((max_cmd + 1 + 15) / 16) * 16;
1570
1571 fusion->request_alloc_sz = sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *
1572 instance->max_mpt_cmds;
1573 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION) *
1574 (fusion->reply_q_depth);
1575 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
1576 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1577 * (instance->max_mpt_cmds + 1)); /* Extra 1 for SMID 0 */
1578}
1579
Shivasharan Sb9637d12017-10-19 02:49:01 -07001580static int megasas_alloc_ioc_init_frame(struct megasas_instance *instance)
1581{
1582 struct fusion_context *fusion;
1583 struct megasas_cmd *cmd;
1584
1585 fusion = instance->ctrl_context;
1586
Shivasharan Se05ee4e2018-01-05 05:27:36 -08001587 cmd = kzalloc(sizeof(struct megasas_cmd), GFP_KERNEL);
Shivasharan Sb9637d12017-10-19 02:49:01 -07001588
1589 if (!cmd) {
1590 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
1591 __func__, __LINE__);
1592 return -ENOMEM;
1593 }
1594
1595 cmd->frame = dma_alloc_coherent(&instance->pdev->dev,
1596 IOC_INIT_FRAME_SIZE,
1597 &cmd->frame_phys_addr, GFP_KERNEL);
1598
1599 if (!cmd->frame) {
1600 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n",
1601 __func__, __LINE__);
1602 kfree(cmd);
1603 return -ENOMEM;
1604 }
1605
1606 fusion->ioc_init_cmd = cmd;
1607 return 0;
1608}
1609
1610/**
1611 * megasas_free_ioc_init_cmd - Free IOC INIT command frame
1612 * @instance: Adapter soft state
1613 */
1614static inline void megasas_free_ioc_init_cmd(struct megasas_instance *instance)
1615{
1616 struct fusion_context *fusion;
1617
1618 fusion = instance->ctrl_context;
1619
1620 if (fusion->ioc_init_cmd && fusion->ioc_init_cmd->frame)
1621 dma_free_coherent(&instance->pdev->dev,
1622 IOC_INIT_FRAME_SIZE,
1623 fusion->ioc_init_cmd->frame,
1624 fusion->ioc_init_cmd->frame_phys_addr);
1625
1626 if (fusion->ioc_init_cmd)
1627 kfree(fusion->ioc_init_cmd);
1628}
1629
adam radford9c915a82010-12-21 13:34:31 -08001630/**
1631 * megasas_init_adapter_fusion - Initializes the FW
1632 * @instance: Adapter soft state
1633 *
1634 * This is the main function for initializing firmware.
1635 */
1636u32
1637megasas_init_adapter_fusion(struct megasas_instance *instance)
1638{
1639 struct megasas_register_set __iomem *reg_set;
1640 struct fusion_context *fusion;
Shivasharan S81b76452018-10-16 23:37:51 -07001641 u32 scratch_pad_1;
adam radfordc8e858f2011-10-08 18:15:13 -07001642 int i = 0, count;
adam radford9c915a82010-12-21 13:34:31 -08001643
1644 fusion = instance->ctrl_context;
1645
1646 reg_set = instance->reg_set;
1647
Sumit Saxena308ec452016-01-28 21:04:30 +05301648 megasas_fusion_update_can_queue(instance, PROBE_CONTEXT);
adam radford9c915a82010-12-21 13:34:31 -08001649
1650 /*
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05301651 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
1652 */
1653 instance->max_mfi_cmds =
1654 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
adam radford9c915a82010-12-21 13:34:31 -08001655
Shivasharan Se97e6732017-10-19 02:49:03 -07001656 megasas_configure_queue_sizes(instance);
adam radford9c915a82010-12-21 13:34:31 -08001657
Shivasharan S81b76452018-10-16 23:37:51 -07001658 scratch_pad_1 = readl(&instance->reg_set->outbound_scratch_pad_1);
1659 /* If scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301660 * Firmware support extended IO chain frame which is 4 times more than
1661 * legacy Firmware.
1662 * Legacy Firmware - Frame size is (8 * 128) = 1K
1663 * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
1664 */
Shivasharan S81b76452018-10-16 23:37:51 -07001665 if (scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301666 instance->max_chain_frame_sz =
Shivasharan S81b76452018-10-16 23:37:51 -07001667 ((scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301668 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
1669 else
1670 instance->max_chain_frame_sz =
Shivasharan S81b76452018-10-16 23:37:51 -07001671 ((scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301672 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;
1673
1674 if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
1675 dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
1676 instance->max_chain_frame_sz,
1677 MEGASAS_CHAIN_FRAME_SZ_MIN);
1678 instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
1679 }
1680
adam radford9c915a82010-12-21 13:34:31 -08001681 fusion->max_sge_in_main_msg =
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301682 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1683 - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
adam radford9c915a82010-12-21 13:34:31 -08001684
1685 fusion->max_sge_in_chain =
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301686 instance->max_chain_frame_sz
1687 / sizeof(union MPI2_SGE_IO_UNION);
adam radford9c915a82010-12-21 13:34:31 -08001688
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301689 instance->max_num_sge =
1690 rounddown_pow_of_two(fusion->max_sge_in_main_msg
1691 + fusion->max_sge_in_chain - 2);
adam radford9c915a82010-12-21 13:34:31 -08001692
1693 /* Used for pass thru MFI frame (DCMD) */
1694 fusion->chain_offset_mfi_pthru =
1695 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1696
1697 fusion->chain_offset_io_request =
1698 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1699 sizeof(union MPI2_SGE_IO_UNION))/16;
1700
adam radfordc8e858f2011-10-08 18:15:13 -07001701 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1702 for (i = 0 ; i < count; i++)
1703 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -08001704
1705 /*
Shivasharan Sec779592017-02-10 00:59:35 -08001706 * For fusion adapters, 3 commands for IOCTL and 8 commands
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05301707 * for driver's internal DCMDs.
1708 */
1709 instance->max_scsi_cmds = instance->max_fw_cmds -
1710 (MEGASAS_FUSION_INTERNAL_CMDS +
1711 MEGASAS_FUSION_IOCTL_CMDS);
1712 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1713
Shivasharan Sb9637d12017-10-19 02:49:01 -07001714 if (megasas_alloc_ioc_init_frame(instance))
1715 return 1;
1716
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05301717 /*
adam radford9c915a82010-12-21 13:34:31 -08001718 * Allocate memory for descriptors
1719 * Create a pool of commands
1720 */
1721 if (megasas_alloc_cmds(instance))
1722 goto fail_alloc_mfi_cmds;
1723 if (megasas_alloc_cmds_fusion(instance))
1724 goto fail_alloc_cmds;
1725
1726 if (megasas_ioc_init_fusion(instance))
1727 goto fail_ioc_init;
1728
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301729 megasas_display_intel_branding(instance);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05301730 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301731 dev_err(&instance->pdev->dev,
1732 "Could not get controller info. Fail from %s %d\n",
1733 __func__, __LINE__);
1734 goto fail_ioc_init;
1735 }
1736
adam radford9c915a82010-12-21 13:34:31 -08001737 instance->flag_ieee = 1;
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08001738 instance->r1_ldio_hint_default = MR_R1_LDIO_PIGGYBACK_DEFAULT;
adam radford9c915a82010-12-21 13:34:31 -08001739 fusion->fast_path_io = 0;
1740
Shivasharan Sdef3e8d2017-08-23 04:47:03 -07001741 if (megasas_allocate_raid_maps(instance))
1742 goto fail_ioc_init;
adam radford9c915a82010-12-21 13:34:31 -08001743
1744 if (!megasas_get_map_info(instance))
1745 megasas_sync_map_info(instance);
1746
1747 return 0;
1748
adam radford9c915a82010-12-21 13:34:31 -08001749fail_ioc_init:
adam radfordeb1b1232011-02-24 20:55:56 -08001750 megasas_free_cmds_fusion(instance);
1751fail_alloc_cmds:
1752 megasas_free_cmds(instance);
1753fail_alloc_mfi_cmds:
Shivasharan Sb9637d12017-10-19 02:49:01 -07001754 megasas_free_ioc_init_cmd(instance);
adam radford9c915a82010-12-21 13:34:31 -08001755 return 1;
1756}
1757
1758/**
Shivasharan S3f6194a2018-10-16 23:37:39 -07001759 * megasas_fault_detect_work - Worker function of
1760 * FW fault handling workqueue.
1761 */
1762static void
1763megasas_fault_detect_work(struct work_struct *work)
1764{
1765 struct megasas_instance *instance =
1766 container_of(work, struct megasas_instance,
1767 fw_fault_work.work);
1768 u32 fw_state, dma_state, status;
1769
1770 /* Check the fw state */
1771 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) &
1772 MFI_STATE_MASK;
1773
1774 if (fw_state == MFI_STATE_FAULT) {
1775 dma_state = instance->instancet->read_fw_status_reg(
1776 instance->reg_set) & MFI_STATE_DMADONE;
1777 /* Start collecting crash, if DMA bit is done */
1778 if (instance->crash_dump_drv_support &&
1779 instance->crash_dump_app_support && dma_state) {
1780 megasas_fusion_crash_dump(instance);
1781 } else {
1782 if (instance->unload == 0) {
1783 status = megasas_reset_fusion(instance->host, 0);
1784 if (status != SUCCESS) {
1785 dev_err(&instance->pdev->dev,
1786 "Failed from %s %d, do not re-arm timer\n",
1787 __func__, __LINE__);
1788 return;
1789 }
1790 }
1791 }
1792 }
1793
1794 if (instance->fw_fault_work_q)
1795 queue_delayed_work(instance->fw_fault_work_q,
1796 &instance->fw_fault_work,
1797 msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL));
1798}
1799
1800int
1801megasas_fusion_start_watchdog(struct megasas_instance *instance)
1802{
1803 /* Check if the Fault WQ is already started */
1804 if (instance->fw_fault_work_q)
1805 return SUCCESS;
1806
1807 INIT_DELAYED_WORK(&instance->fw_fault_work, megasas_fault_detect_work);
1808
1809 snprintf(instance->fault_handler_work_q_name,
1810 sizeof(instance->fault_handler_work_q_name),
1811 "poll_megasas%d_status", instance->host->host_no);
1812
1813 instance->fw_fault_work_q =
1814 create_singlethread_workqueue(instance->fault_handler_work_q_name);
1815 if (!instance->fw_fault_work_q) {
1816 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
1817 __func__, __LINE__);
1818 return FAILED;
1819 }
1820
1821 queue_delayed_work(instance->fw_fault_work_q,
1822 &instance->fw_fault_work,
1823 msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL));
1824
1825 return SUCCESS;
1826}
1827
1828void
1829megasas_fusion_stop_watchdog(struct megasas_instance *instance)
1830{
1831 struct workqueue_struct *wq;
1832
1833 if (instance->fw_fault_work_q) {
1834 wq = instance->fw_fault_work_q;
1835 instance->fw_fault_work_q = NULL;
1836 if (!cancel_delayed_work_sync(&instance->fw_fault_work))
1837 flush_workqueue(wq);
1838 destroy_workqueue(wq);
1839 }
1840}
1841
1842/**
adam radford9c915a82010-12-21 13:34:31 -08001843 * map_cmd_status - Maps FW cmd status to OS cmd status
1844 * @cmd : Pointer to cmd
1845 * @status : status of cmd returned by FW
1846 * @ext_status : ext status of cmd returned by FW
1847 */
1848
1849void
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001850map_cmd_status(struct fusion_context *fusion,
Shivasharan S41064f12017-02-10 00:59:37 -08001851 struct scsi_cmnd *scmd, u8 status, u8 ext_status,
1852 u32 data_length, u8 *sense)
adam radford9c915a82010-12-21 13:34:31 -08001853{
Shivasharan Sdef0eab2017-02-10 00:59:15 -08001854 u8 cmd_type;
Shivasharan S31d9a572017-02-10 00:59:14 -08001855 int resid;
adam radford9c915a82010-12-21 13:34:31 -08001856
Shivasharan Sdef0eab2017-02-10 00:59:15 -08001857 cmd_type = megasas_cmd_type(scmd);
adam radford9c915a82010-12-21 13:34:31 -08001858 switch (status) {
1859
1860 case MFI_STAT_OK:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001861 scmd->result = DID_OK << 16;
adam radford9c915a82010-12-21 13:34:31 -08001862 break;
1863
1864 case MFI_STAT_SCSI_IO_FAILED:
1865 case MFI_STAT_LD_INIT_IN_PROGRESS:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001866 scmd->result = (DID_ERROR << 16) | ext_status;
adam radford9c915a82010-12-21 13:34:31 -08001867 break;
1868
1869 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1870
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001871 scmd->result = (DID_OK << 16) | ext_status;
adam radford9c915a82010-12-21 13:34:31 -08001872 if (ext_status == SAM_STAT_CHECK_CONDITION) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001873 memset(scmd->sense_buffer, 0,
adam radford9c915a82010-12-21 13:34:31 -08001874 SCSI_SENSE_BUFFERSIZE);
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001875 memcpy(scmd->sense_buffer, sense,
adam radford9c915a82010-12-21 13:34:31 -08001876 SCSI_SENSE_BUFFERSIZE);
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001877 scmd->result |= DRIVER_SENSE << 24;
adam radford9c915a82010-12-21 13:34:31 -08001878 }
Shivasharan S31d9a572017-02-10 00:59:14 -08001879
1880 /*
1881 * If the IO request is partially completed, then MR FW will
1882 * update "io_request->DataLength" field with actual number of
1883 * bytes transferred.Driver will set residual bytes count in
1884 * SCSI command structure.
1885 */
1886 resid = (scsi_bufflen(scmd) - data_length);
1887 scsi_set_resid(scmd, resid);
Shivasharan Sdef0eab2017-02-10 00:59:15 -08001888
1889 if (resid &&
1890 ((cmd_type == READ_WRITE_LDIO) ||
1891 (cmd_type == READ_WRITE_SYSPDIO)))
1892 scmd_printk(KERN_INFO, scmd, "BRCM Debug mfi stat 0x%x, data len"
1893 " requested/completed 0x%x/0x%x\n",
1894 status, scsi_bufflen(scmd), data_length);
adam radford9c915a82010-12-21 13:34:31 -08001895 break;
1896
1897 case MFI_STAT_LD_OFFLINE:
1898 case MFI_STAT_DEVICE_NOT_FOUND:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001899 scmd->result = DID_BAD_TARGET << 16;
adam radford9c915a82010-12-21 13:34:31 -08001900 break;
adam radford36807e62011-10-08 18:15:06 -07001901 case MFI_STAT_CONFIG_SEQ_MISMATCH:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001902 scmd->result = DID_IMM_RETRY << 16;
adam radford36807e62011-10-08 18:15:06 -07001903 break;
adam radford9c915a82010-12-21 13:34:31 -08001904 default:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001905 scmd->result = DID_ERROR << 16;
adam radford9c915a82010-12-21 13:34:31 -08001906 break;
1907 }
1908}
1909
1910/**
Shivasharan S33203bc2017-02-10 00:59:12 -08001911 * megasas_is_prp_possible -
1912 * Checks if native NVMe PRPs can be built for the IO
1913 *
1914 * @instance: Adapter soft state
1915 * @scmd: SCSI command from the mid-layer
1916 * @sge_count: scatter gather element count.
1917 *
1918 * Returns: true: PRPs can be built
1919 * false: IEEE SGLs needs to be built
1920 */
1921static bool
1922megasas_is_prp_possible(struct megasas_instance *instance,
1923 struct scsi_cmnd *scmd, int sge_count)
1924{
1925 struct fusion_context *fusion;
1926 int i;
1927 u32 data_length = 0;
1928 struct scatterlist *sg_scmd;
1929 bool build_prp = false;
1930 u32 mr_nvme_pg_size;
1931
1932 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1933 MR_DEFAULT_NVME_PAGE_SIZE);
1934 fusion = instance->ctrl_context;
1935 data_length = scsi_bufflen(scmd);
1936 sg_scmd = scsi_sglist(scmd);
1937
1938 /*
1939 * NVMe uses one PRP for each page (or part of a page)
1940 * look at the data length - if 4 pages or less then IEEE is OK
1941 * if > 5 pages then we need to build a native SGL
1942 * if > 4 and <= 5 pages, then check physical address of 1st SG entry
1943 * if this first size in the page is >= the residual beyond 4 pages
1944 * then use IEEE, otherwise use native SGL
1945 */
1946
1947 if (data_length > (mr_nvme_pg_size * 5)) {
1948 build_prp = true;
1949 } else if ((data_length > (mr_nvme_pg_size * 4)) &&
1950 (data_length <= (mr_nvme_pg_size * 5))) {
1951 /* check if 1st SG entry size is < residual beyond 4 pages */
1952 if (sg_dma_len(sg_scmd) < (data_length - (mr_nvme_pg_size * 4)))
1953 build_prp = true;
1954 }
1955
1956/*
1957 * Below code detects gaps/holes in IO data buffers.
1958 * What does holes/gaps mean?
1959 * Any SGE except first one in a SGL starts at non NVME page size
1960 * aligned address OR Any SGE except last one in a SGL ends at
1961 * non NVME page size boundary.
1962 *
1963 * Driver has already informed block layer by setting boundary rules for
1964 * bio merging done at NVME page size boundary calling kernel API
1965 * blk_queue_virt_boundary inside slave_config.
1966 * Still there is possibility of IO coming with holes to driver because of
1967 * IO merging done by IO scheduler.
1968 *
1969 * With SCSI BLK MQ enabled, there will be no IO with holes as there is no
1970 * IO scheduling so no IO merging.
1971 *
1972 * With SCSI BLK MQ disabled, IO scheduler may attempt to merge IOs and
1973 * then sending IOs with holes.
1974 *
1975 * Though driver can request block layer to disable IO merging by calling-
Bart Van Assche8b904b52018-03-07 17:10:10 -08001976 * blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue) but
Shivasharan S33203bc2017-02-10 00:59:12 -08001977 * user may tune sysfs parameter- nomerges again to 0 or 1.
1978 *
1979 * If in future IO scheduling is enabled with SCSI BLK MQ,
1980 * this algorithm to detect holes will be required in driver
1981 * for SCSI BLK MQ enabled case as well.
1982 *
1983 *
1984 */
1985 scsi_for_each_sg(scmd, sg_scmd, sge_count, i) {
1986 if ((i != 0) && (i != (sge_count - 1))) {
1987 if (mega_mod64(sg_dma_len(sg_scmd), mr_nvme_pg_size) ||
1988 mega_mod64(sg_dma_address(sg_scmd),
1989 mr_nvme_pg_size)) {
1990 build_prp = false;
1991 atomic_inc(&instance->sge_holes_type1);
1992 break;
1993 }
1994 }
1995
1996 if ((sge_count > 1) && (i == 0)) {
1997 if ((mega_mod64((sg_dma_address(sg_scmd) +
1998 sg_dma_len(sg_scmd)),
1999 mr_nvme_pg_size))) {
2000 build_prp = false;
2001 atomic_inc(&instance->sge_holes_type2);
2002 break;
2003 }
2004 }
2005
2006 if ((sge_count > 1) && (i == (sge_count - 1))) {
2007 if (mega_mod64(sg_dma_address(sg_scmd),
2008 mr_nvme_pg_size)) {
2009 build_prp = false;
2010 atomic_inc(&instance->sge_holes_type3);
2011 break;
2012 }
2013 }
2014 }
2015
2016 return build_prp;
2017}
2018
2019/**
2020 * megasas_make_prp_nvme -
2021 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
2022 *
2023 * @instance: Adapter soft state
2024 * @scmd: SCSI command from the mid-layer
2025 * @sgl_ptr: SGL to be filled in
2026 * @cmd: Fusion command frame
2027 * @sge_count: scatter gather element count.
2028 *
2029 * Returns: true: PRPs are built
2030 * false: IEEE SGLs needs to be built
2031 */
2032static bool
2033megasas_make_prp_nvme(struct megasas_instance *instance, struct scsi_cmnd *scmd,
2034 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
2035 struct megasas_cmd_fusion *cmd, int sge_count)
2036{
2037 int sge_len, offset, num_prp_in_chain = 0;
2038 struct MPI25_IEEE_SGE_CHAIN64 *main_chain_element, *ptr_first_sgl;
Arnd Bergmannd1da5222017-02-14 22:21:51 +01002039 u64 *ptr_sgl;
2040 dma_addr_t ptr_sgl_phys;
Shivasharan S33203bc2017-02-10 00:59:12 -08002041 u64 sge_addr;
2042 u32 page_mask, page_mask_result;
2043 struct scatterlist *sg_scmd;
2044 u32 first_prp_len;
2045 bool build_prp = false;
2046 int data_len = scsi_bufflen(scmd);
2047 struct fusion_context *fusion;
2048 u32 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
2049 MR_DEFAULT_NVME_PAGE_SIZE);
2050
2051 fusion = instance->ctrl_context;
2052
2053 build_prp = megasas_is_prp_possible(instance, scmd, sge_count);
2054
2055 if (!build_prp)
2056 return false;
2057
2058 /*
2059 * Nvme has a very convoluted prp format. One prp is required
2060 * for each page or partial page. Driver need to split up OS sg_list
2061 * entries if it is longer than one page or cross a page
2062 * boundary. Driver also have to insert a PRP list pointer entry as
2063 * the last entry in each physical page of the PRP list.
2064 *
2065 * NOTE: The first PRP "entry" is actually placed in the first
2066 * SGL entry in the main message as IEEE 64 format. The 2nd
2067 * entry in the main message is the chain element, and the rest
2068 * of the PRP entries are built in the contiguous pcie buffer.
2069 */
2070 page_mask = mr_nvme_pg_size - 1;
2071 ptr_sgl = (u64 *)cmd->sg_frame;
Arnd Bergmannd1da5222017-02-14 22:21:51 +01002072 ptr_sgl_phys = cmd->sg_frame_phys_addr;
Shivasharan S33203bc2017-02-10 00:59:12 -08002073 memset(ptr_sgl, 0, instance->max_chain_frame_sz);
2074
2075 /* Build chain frame element which holds all prps except first*/
2076 main_chain_element = (struct MPI25_IEEE_SGE_CHAIN64 *)
2077 ((u8 *)sgl_ptr + sizeof(struct MPI25_IEEE_SGE_CHAIN64));
2078
Arnd Bergmannd1da5222017-02-14 22:21:51 +01002079 main_chain_element->Address = cpu_to_le64(ptr_sgl_phys);
Shivasharan S33203bc2017-02-10 00:59:12 -08002080 main_chain_element->NextChainOffset = 0;
2081 main_chain_element->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2082 IEEE_SGE_FLAGS_SYSTEM_ADDR |
2083 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
2084
2085 /* Build first prp, sge need not to be page aligned*/
2086 ptr_first_sgl = sgl_ptr;
2087 sg_scmd = scsi_sglist(scmd);
2088 sge_addr = sg_dma_address(sg_scmd);
2089 sge_len = sg_dma_len(sg_scmd);
2090
2091 offset = (u32)(sge_addr & page_mask);
2092 first_prp_len = mr_nvme_pg_size - offset;
2093
2094 ptr_first_sgl->Address = cpu_to_le64(sge_addr);
2095 ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
2096
2097 data_len -= first_prp_len;
2098
2099 if (sge_len > first_prp_len) {
2100 sge_addr += first_prp_len;
2101 sge_len -= first_prp_len;
2102 } else if (sge_len == first_prp_len) {
2103 sg_scmd = sg_next(sg_scmd);
2104 sge_addr = sg_dma_address(sg_scmd);
2105 sge_len = sg_dma_len(sg_scmd);
2106 }
2107
2108 for (;;) {
2109 offset = (u32)(sge_addr & page_mask);
2110
2111 /* Put PRP pointer due to page boundary*/
2112 page_mask_result = (uintptr_t)(ptr_sgl + 1) & page_mask;
2113 if (unlikely(!page_mask_result)) {
2114 scmd_printk(KERN_NOTICE,
2115 scmd, "page boundary ptr_sgl: 0x%p\n",
2116 ptr_sgl);
Arnd Bergmannd1da5222017-02-14 22:21:51 +01002117 ptr_sgl_phys += 8;
2118 *ptr_sgl = cpu_to_le64(ptr_sgl_phys);
Shivasharan S33203bc2017-02-10 00:59:12 -08002119 ptr_sgl++;
2120 num_prp_in_chain++;
2121 }
2122
2123 *ptr_sgl = cpu_to_le64(sge_addr);
2124 ptr_sgl++;
Arnd Bergmannd1da5222017-02-14 22:21:51 +01002125 ptr_sgl_phys += 8;
Shivasharan S33203bc2017-02-10 00:59:12 -08002126 num_prp_in_chain++;
2127
2128 sge_addr += mr_nvme_pg_size;
2129 sge_len -= mr_nvme_pg_size;
2130 data_len -= mr_nvme_pg_size;
2131
2132 if (data_len <= 0)
2133 break;
2134
2135 if (sge_len > 0)
2136 continue;
2137
2138 sg_scmd = sg_next(sg_scmd);
2139 sge_addr = sg_dma_address(sg_scmd);
2140 sge_len = sg_dma_len(sg_scmd);
2141 }
2142
2143 main_chain_element->Length =
2144 cpu_to_le32(num_prp_in_chain * sizeof(u64));
2145
2146 atomic_inc(&instance->prp_sgl);
2147 return build_prp;
2148}
2149
2150/**
adam radford9c915a82010-12-21 13:34:31 -08002151 * megasas_make_sgl_fusion - Prepares 32-bit SGL
2152 * @instance: Adapter soft state
2153 * @scp: SCSI command from the mid-layer
2154 * @sgl_ptr: SGL to be filled in
2155 * @cmd: cmd we are working on
Shivasharan S33203bc2017-02-10 00:59:12 -08002156 * @sge_count sge count
adam radford9c915a82010-12-21 13:34:31 -08002157 *
adam radford9c915a82010-12-21 13:34:31 -08002158 */
Shivasharan S33203bc2017-02-10 00:59:12 -08002159static void
adam radford9c915a82010-12-21 13:34:31 -08002160megasas_make_sgl_fusion(struct megasas_instance *instance,
2161 struct scsi_cmnd *scp,
2162 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
Shivasharan S33203bc2017-02-10 00:59:12 -08002163 struct megasas_cmd_fusion *cmd, int sge_count)
adam radford9c915a82010-12-21 13:34:31 -08002164{
Shivasharan S33203bc2017-02-10 00:59:12 -08002165 int i, sg_processed;
adam radford9c915a82010-12-21 13:34:31 -08002166 struct scatterlist *os_sgl;
2167 struct fusion_context *fusion;
2168
2169 fusion = instance->ctrl_context;
2170
Shivasharan Sc3651782017-10-19 02:48:48 -07002171 if (instance->adapter_type >= INVADER_SERIES) {
adam radford36807e62011-10-08 18:15:06 -07002172 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
2173 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2174 sgl_ptr_end->Flags = 0;
2175 }
adam radford9c915a82010-12-21 13:34:31 -08002176
adam radford9c915a82010-12-21 13:34:31 -08002177 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302178 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
2179 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
adam radford9c915a82010-12-21 13:34:31 -08002180 sgl_ptr->Flags = 0;
Shivasharan Sc3651782017-10-19 02:48:48 -07002181 if (instance->adapter_type >= INVADER_SERIES)
adam radford36807e62011-10-08 18:15:06 -07002182 if (i == sge_count - 1)
2183 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
adam radford9c915a82010-12-21 13:34:31 -08002184 sgl_ptr++;
adam radford9c915a82010-12-21 13:34:31 -08002185 sg_processed = i + 1;
2186
2187 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
2188 (sge_count > fusion->max_sge_in_main_msg)) {
2189
2190 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
Shivasharan Sc3651782017-10-19 02:48:48 -07002191 if (instance->adapter_type >= INVADER_SERIES) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302192 if ((le16_to_cpu(cmd->io_request->IoFlags) &
2193 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
2194 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
adam radford36807e62011-10-08 18:15:06 -07002195 cmd->io_request->ChainOffset =
2196 fusion->
2197 chain_offset_io_request;
2198 else
2199 cmd->io_request->ChainOffset = 0;
2200 } else
2201 cmd->io_request->ChainOffset =
2202 fusion->chain_offset_io_request;
2203
adam radford9c915a82010-12-21 13:34:31 -08002204 sg_chain = sgl_ptr;
2205 /* Prepare chain element */
2206 sg_chain->NextChainOffset = 0;
Shivasharan Sc3651782017-10-19 02:48:48 -07002207 if (instance->adapter_type >= INVADER_SERIES)
adam radford36807e62011-10-08 18:15:06 -07002208 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
2209 else
2210 sg_chain->Flags =
2211 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2212 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302213 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
2214 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08002215
2216 sgl_ptr =
2217 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05302218 memset(sgl_ptr, 0, instance->max_chain_frame_sz);
adam radford9c915a82010-12-21 13:34:31 -08002219 }
2220 }
Shivasharan S33203bc2017-02-10 00:59:12 -08002221 atomic_inc(&instance->ieee_sgl);
2222}
2223
2224/**
2225 * megasas_make_sgl - Build Scatter Gather List(SGLs)
2226 * @scp: SCSI command pointer
2227 * @instance: Soft instance of controller
2228 * @cmd: Fusion command pointer
2229 *
2230 * This function will build sgls based on device type.
2231 * For nvme drives, there is different way of building sgls in nvme native
2232 * format- PRPs(Physical Region Page).
2233 *
2234 * Returns the number of sg lists actually used, zero if the sg lists
2235 * is NULL, or -ENOMEM if the mapping failed
2236 */
2237static
2238int megasas_make_sgl(struct megasas_instance *instance, struct scsi_cmnd *scp,
2239 struct megasas_cmd_fusion *cmd)
2240{
2241 int sge_count;
2242 bool build_prp = false;
2243 struct MPI25_IEEE_SGE_CHAIN64 *sgl_chain64;
2244
2245 sge_count = scsi_dma_map(scp);
2246
2247 if ((sge_count > instance->max_num_sge) || (sge_count <= 0))
2248 return sge_count;
2249
2250 sgl_chain64 = (struct MPI25_IEEE_SGE_CHAIN64 *)&cmd->io_request->SGL;
2251 if ((le16_to_cpu(cmd->io_request->IoFlags) &
2252 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
2253 (cmd->pd_interface == NVME_PD))
2254 build_prp = megasas_make_prp_nvme(instance, scp, sgl_chain64,
2255 cmd, sge_count);
2256
2257 if (!build_prp)
2258 megasas_make_sgl_fusion(instance, scp, sgl_chain64,
2259 cmd, sge_count);
adam radford9c915a82010-12-21 13:34:31 -08002260
2261 return sge_count;
2262}
2263
2264/**
2265 * megasas_set_pd_lba - Sets PD LBA
2266 * @cdb: CDB
2267 * @cdb_len: cdb length
2268 * @start_blk: Start block of IO
2269 *
2270 * Used to set the PD LBA in CDB for FP IOs
2271 */
2272void
2273megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
2274 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302275 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
adam radford9c915a82010-12-21 13:34:31 -08002276{
2277 struct MR_LD_RAID *raid;
Shivasharan Sd2d03582017-02-10 00:59:19 -08002278 u16 ld;
adam radford9c915a82010-12-21 13:34:31 -08002279 u64 start_blk = io_info->pdBlock;
2280 u8 *cdb = io_request->CDB.CDB32;
2281 u32 num_blocks = io_info->numBlocks;
adam radford495c5602011-05-11 18:34:40 -07002282 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
adam radford9c915a82010-12-21 13:34:31 -08002283
2284 /* Check if T10 PI (DIF) is enabled for this LD */
2285 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
2286 raid = MR_LdRaidGet(ld, local_map_ptr);
2287 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
2288 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2289 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
2290 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
2291
Christoph Hellwig60ee6522018-10-10 19:31:25 +02002292 if (scp->sc_data_direction == DMA_FROM_DEVICE)
adam radford9c915a82010-12-21 13:34:31 -08002293 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
2294 else
2295 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
2296 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
2297
2298 /* LBA */
2299 cdb[12] = (u8)((start_blk >> 56) & 0xff);
2300 cdb[13] = (u8)((start_blk >> 48) & 0xff);
2301 cdb[14] = (u8)((start_blk >> 40) & 0xff);
2302 cdb[15] = (u8)((start_blk >> 32) & 0xff);
2303 cdb[16] = (u8)((start_blk >> 24) & 0xff);
2304 cdb[17] = (u8)((start_blk >> 16) & 0xff);
2305 cdb[18] = (u8)((start_blk >> 8) & 0xff);
2306 cdb[19] = (u8)(start_blk & 0xff);
2307
2308 /* Logical block reference tag */
2309 io_request->CDB.EEDP32.PrimaryReferenceTag =
2310 cpu_to_be32(ref_tag);
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05302311 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302312 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08002313
2314 /* Transfer length */
2315 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
2316 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
2317 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
2318 cdb[31] = (u8)(num_blocks & 0xff);
2319
2320 /* set SCSI IO EEDPFlags */
Christoph Hellwig60ee6522018-10-10 19:31:25 +02002321 if (scp->sc_data_direction == DMA_FROM_DEVICE) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302322 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08002323 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
2324 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
2325 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
2326 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
Sasikumar Chandrasekaran45d44602017-01-10 18:20:45 -05002327 MPI25_SCSIIO_EEDPFLAGS_DO_NOT_DISABLE_MODE |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302328 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
adam radford9c915a82010-12-21 13:34:31 -08002329 } else {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302330 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08002331 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302332 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
adam radford9c915a82010-12-21 13:34:31 -08002333 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302334 io_request->Control |= cpu_to_le32((0x4 << 26));
2335 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
adam radford9c915a82010-12-21 13:34:31 -08002336 } else {
2337 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
2338 if (((cdb_len == 12) || (cdb_len == 16)) &&
2339 (start_blk <= 0xffffffff)) {
2340 if (cdb_len == 16) {
2341 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
2342 flagvals = cdb[1];
2343 groupnum = cdb[14];
2344 control = cdb[15];
2345 } else {
2346 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
2347 flagvals = cdb[1];
2348 groupnum = cdb[10];
2349 control = cdb[11];
2350 }
2351
2352 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2353
2354 cdb[0] = opcode;
2355 cdb[1] = flagvals;
2356 cdb[6] = groupnum;
2357 cdb[9] = control;
2358
2359 /* Transfer length */
2360 cdb[8] = (u8)(num_blocks & 0xff);
2361 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
2362
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302363 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08002364 cdb_len = 10;
adam radford495c5602011-05-11 18:34:40 -07002365 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
2366 /* Convert to 16 byte CDB for large LBA's */
2367 switch (cdb_len) {
2368 case 6:
2369 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
2370 control = cdb[5];
2371 break;
2372 case 10:
2373 opcode =
2374 cdb[0] == READ_10 ? READ_16 : WRITE_16;
2375 flagvals = cdb[1];
2376 groupnum = cdb[6];
2377 control = cdb[9];
2378 break;
2379 case 12:
2380 opcode =
2381 cdb[0] == READ_12 ? READ_16 : WRITE_16;
2382 flagvals = cdb[1];
2383 groupnum = cdb[10];
2384 control = cdb[11];
2385 break;
2386 }
2387
2388 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
2389
2390 cdb[0] = opcode;
2391 cdb[1] = flagvals;
2392 cdb[14] = groupnum;
2393 cdb[15] = control;
2394
2395 /* Transfer length */
2396 cdb[13] = (u8)(num_blocks & 0xff);
2397 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
2398 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
2399 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
2400
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302401 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
adam radford495c5602011-05-11 18:34:40 -07002402 cdb_len = 16;
adam radford9c915a82010-12-21 13:34:31 -08002403 }
2404
2405 /* Normal case, just load LBA here */
2406 switch (cdb_len) {
2407 case 6:
2408 {
2409 u8 val = cdb[1] & 0xE0;
2410 cdb[3] = (u8)(start_blk & 0xff);
2411 cdb[2] = (u8)((start_blk >> 8) & 0xff);
2412 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
2413 break;
2414 }
2415 case 10:
2416 cdb[5] = (u8)(start_blk & 0xff);
2417 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2418 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2419 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2420 break;
2421 case 12:
2422 cdb[5] = (u8)(start_blk & 0xff);
2423 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2424 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2425 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2426 break;
2427 case 16:
2428 cdb[9] = (u8)(start_blk & 0xff);
2429 cdb[8] = (u8)((start_blk >> 8) & 0xff);
2430 cdb[7] = (u8)((start_blk >> 16) & 0xff);
2431 cdb[6] = (u8)((start_blk >> 24) & 0xff);
2432 cdb[5] = (u8)((start_blk >> 32) & 0xff);
2433 cdb[4] = (u8)((start_blk >> 40) & 0xff);
2434 cdb[3] = (u8)((start_blk >> 48) & 0xff);
2435 cdb[2] = (u8)((start_blk >> 56) & 0xff);
2436 break;
2437 }
2438 }
2439}
2440
2441/**
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002442 * megasas_stream_detect - stream detection on read and and write IOs
2443 * @instance: Adapter soft state
2444 * @cmd: Command to be prepared
2445 * @io_info: IO Request info
2446 *
2447 */
2448
2449/** stream detection on read and and write IOs */
2450static void megasas_stream_detect(struct megasas_instance *instance,
Shivasharan S41064f12017-02-10 00:59:37 -08002451 struct megasas_cmd_fusion *cmd,
2452 struct IO_REQUEST_INFO *io_info)
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002453{
2454 struct fusion_context *fusion = instance->ctrl_context;
2455 u32 device_id = io_info->ldTgtId;
2456 struct LD_STREAM_DETECT *current_ld_sd
2457 = fusion->stream_detect_by_ld[device_id];
2458 u32 *track_stream = &current_ld_sd->mru_bit_map, stream_num;
2459 u32 shifted_values, unshifted_values;
2460 u32 index_value_mask, shifted_values_mask;
2461 int i;
2462 bool is_read_ahead = false;
2463 struct STREAM_DETECT *current_sd;
2464 /* find possible stream */
2465 for (i = 0; i < MAX_STREAMS_TRACKED; ++i) {
Shivasharan S41064f12017-02-10 00:59:37 -08002466 stream_num = (*track_stream >>
2467 (i * BITS_PER_INDEX_STREAM)) &
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002468 STREAM_MASK;
2469 current_sd = &current_ld_sd->stream_track[stream_num];
Shivasharan S41064f12017-02-10 00:59:37 -08002470 /* if we found a stream, update the raid
2471 * context and also update the mruBitMap
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002472 */
Shivasharan S41064f12017-02-10 00:59:37 -08002473 /* boundary condition */
2474 if ((current_sd->next_seq_lba) &&
2475 (io_info->ldStartBlock >= current_sd->next_seq_lba) &&
2476 (io_info->ldStartBlock <= (current_sd->next_seq_lba + 32)) &&
2477 (current_sd->is_read == io_info->isRead)) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002478
Shivasharan S41064f12017-02-10 00:59:37 -08002479 if ((io_info->ldStartBlock != current_sd->next_seq_lba) &&
2480 ((!io_info->isRead) || (!is_read_ahead)))
2481 /*
2482 * Once the API availible we need to change this.
2483 * At this point we are not allowing any gap
2484 */
2485 continue;
2486
2487 SET_STREAM_DETECTED(cmd->io_request->RaidContext.raid_context_g35);
2488 current_sd->next_seq_lba =
2489 io_info->ldStartBlock + io_info->numBlocks;
2490 /*
2491 * update the mruBitMap LRU
2492 */
2493 shifted_values_mask =
2494 (1 << i * BITS_PER_INDEX_STREAM) - 1;
2495 shifted_values = ((*track_stream & shifted_values_mask)
2496 << BITS_PER_INDEX_STREAM);
2497 index_value_mask =
2498 STREAM_MASK << i * BITS_PER_INDEX_STREAM;
2499 unshifted_values =
2500 *track_stream & ~(shifted_values_mask |
2501 index_value_mask);
2502 *track_stream =
2503 unshifted_values | shifted_values | stream_num;
2504 return;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002505 }
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002506 }
2507 /*
2508 * if we did not find any stream, create a new one
2509 * from the least recently used
2510 */
Shivasharan S41064f12017-02-10 00:59:37 -08002511 stream_num = (*track_stream >>
2512 ((MAX_STREAMS_TRACKED - 1) * BITS_PER_INDEX_STREAM)) &
2513 STREAM_MASK;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002514 current_sd = &current_ld_sd->stream_track[stream_num];
2515 current_sd->is_read = io_info->isRead;
2516 current_sd->next_seq_lba = io_info->ldStartBlock + io_info->numBlocks;
Shivasharan S41064f12017-02-10 00:59:37 -08002517 *track_stream = (((*track_stream & ZERO_LAST_STREAM) << 4) | stream_num);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002518 return;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002519}
2520
2521/**
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002522 * megasas_set_raidflag_cpu_affinity - This function sets the cpu
2523 * affinity (cpu of the controller) and raid_flags in the raid context
2524 * based on IO type.
2525 *
2526 * @praid_context: IO RAID context
2527 * @raid: LD raid map
2528 * @fp_possible: Is fast path possible?
2529 * @is_read: Is read IO?
2530 *
2531 */
2532static void
2533megasas_set_raidflag_cpu_affinity(union RAID_CONTEXT_UNION *praid_context,
2534 struct MR_LD_RAID *raid, bool fp_possible,
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002535 u8 is_read, u32 scsi_buff_len)
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002536{
2537 u8 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2538 struct RAID_CONTEXT_G35 *rctx_g35;
2539
2540 rctx_g35 = &praid_context->raid_context_g35;
2541 if (fp_possible) {
2542 if (is_read) {
2543 if ((raid->cpuAffinity.pdRead.cpu0) &&
2544 (raid->cpuAffinity.pdRead.cpu1))
2545 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2546 else if (raid->cpuAffinity.pdRead.cpu1)
2547 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2548 } else {
2549 if ((raid->cpuAffinity.pdWrite.cpu0) &&
2550 (raid->cpuAffinity.pdWrite.cpu1))
2551 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2552 else if (raid->cpuAffinity.pdWrite.cpu1)
2553 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2554 /* Fast path cache by pass capable R0/R1 VD */
2555 if ((raid->level <= 1) &&
2556 (raid->capability.fp_cache_bypass_capable)) {
Shivasharan Sa174118b2017-02-10 00:59:21 -08002557 rctx_g35->routing_flags |=
2558 (1 << MR_RAID_CTX_ROUTINGFLAGS_SLD_SHIFT);
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002559 rctx_g35->raid_flags =
2560 (MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS
2561 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2562 }
2563 }
2564 } else {
2565 if (is_read) {
2566 if ((raid->cpuAffinity.ldRead.cpu0) &&
2567 (raid->cpuAffinity.ldRead.cpu1))
2568 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2569 else if (raid->cpuAffinity.ldRead.cpu1)
2570 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2571 } else {
2572 if ((raid->cpuAffinity.ldWrite.cpu0) &&
2573 (raid->cpuAffinity.ldWrite.cpu1))
2574 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2575 else if (raid->cpuAffinity.ldWrite.cpu1)
2576 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2577
Shivasharan Sa174118b2017-02-10 00:59:21 -08002578 if (is_stream_detected(rctx_g35) &&
Shivasharan S874d0252017-03-10 03:22:14 -08002579 ((raid->level == 5) || (raid->level == 6)) &&
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002580 (raid->writeMode == MR_RL_WRITE_THROUGH_MODE) &&
2581 (cpu_sel == MR_RAID_CTX_CPUSEL_FCFS))
2582 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2583 }
2584 }
2585
Shivasharan Sa174118b2017-02-10 00:59:21 -08002586 rctx_g35->routing_flags |=
2587 (cpu_sel << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002588
2589 /* Always give priority to MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2590 * vs MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS.
2591 * IO Subtype is not bitmap.
2592 */
2593 if ((raid->level == 1) && (!is_read)) {
2594 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2595 praid_context->raid_context_g35.raid_flags =
2596 (MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2597 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2598 }
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002599}
2600
2601/**
adam radford9c915a82010-12-21 13:34:31 -08002602 * megasas_build_ldio_fusion - Prepares IOs to devices
2603 * @instance: Adapter soft state
2604 * @scp: SCSI command
2605 * @cmd: Command to be prepared
2606 *
2607 * Prepares the io_request and chain elements (sg_frame) for IO
2608 * The IO can be for PD (Fast Path) or LD
2609 */
2610void
2611megasas_build_ldio_fusion(struct megasas_instance *instance,
2612 struct scsi_cmnd *scp,
2613 struct megasas_cmd_fusion *cmd)
2614{
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002615 bool fp_possible;
2616 u16 ld;
2617 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
2618 u32 scsi_buff_len;
adam radford9c915a82010-12-21 13:34:31 -08002619 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2620 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2621 struct IO_REQUEST_INFO io_info;
2622 struct fusion_context *fusion;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302623 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
adam radford21c9e162013-09-06 15:27:14 -07002624 u8 *raidLUN;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002625 unsigned long spinlock_flags;
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002626 struct MR_LD_RAID *raid = NULL;
2627 struct MR_PRIV_DEVICE *mrdev_priv;
Shivasharan S9e770182018-10-16 23:37:49 -07002628 struct RAID_CONTEXT *rctx;
2629 struct RAID_CONTEXT_G35 *rctx_g35;
adam radford9c915a82010-12-21 13:34:31 -08002630
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302631 device_id = MEGASAS_DEV_INDEX(scp);
adam radford9c915a82010-12-21 13:34:31 -08002632
2633 fusion = instance->ctrl_context;
2634
2635 io_request = cmd->io_request;
Shivasharan S9e770182018-10-16 23:37:49 -07002636 rctx = &io_request->RaidContext.raid_context;
2637 rctx_g35 = &io_request->RaidContext.raid_context_g35;
2638
2639 rctx->virtual_disk_tgt_id = cpu_to_le16(device_id);
2640 rctx->status = 0;
2641 rctx->ex_status = 0;
adam radford9c915a82010-12-21 13:34:31 -08002642
2643 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
2644
2645 start_lba_lo = 0;
2646 start_lba_hi = 0;
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002647 fp_possible = false;
adam radford9c915a82010-12-21 13:34:31 -08002648
2649 /*
2650 * 6-byte READ(0x08) or WRITE(0x0A) cdb
2651 */
2652 if (scp->cmd_len == 6) {
adam radfordf9eff812012-10-01 19:27:07 -07002653 datalength = (u32) scp->cmnd[4];
adam radford9c915a82010-12-21 13:34:31 -08002654 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
2655 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
2656
2657 start_lba_lo &= 0x1FFFFF;
2658 }
2659
2660 /*
2661 * 10-byte READ(0x28) or WRITE(0x2A) cdb
2662 */
2663 else if (scp->cmd_len == 10) {
adam radfordf9eff812012-10-01 19:27:07 -07002664 datalength = (u32) scp->cmnd[8] |
adam radford9c915a82010-12-21 13:34:31 -08002665 ((u32) scp->cmnd[7] << 8);
2666 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2667 ((u32) scp->cmnd[3] << 16) |
2668 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2669 }
2670
2671 /*
2672 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
2673 */
2674 else if (scp->cmd_len == 12) {
adam radfordf9eff812012-10-01 19:27:07 -07002675 datalength = ((u32) scp->cmnd[6] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08002676 ((u32) scp->cmnd[7] << 16) |
2677 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2678 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2679 ((u32) scp->cmnd[3] << 16) |
2680 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2681 }
2682
2683 /*
2684 * 16-byte READ(0x88) or WRITE(0x8A) cdb
2685 */
2686 else if (scp->cmd_len == 16) {
adam radfordf9eff812012-10-01 19:27:07 -07002687 datalength = ((u32) scp->cmnd[10] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08002688 ((u32) scp->cmnd[11] << 16) |
2689 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
2690 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
2691 ((u32) scp->cmnd[7] << 16) |
2692 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2693
2694 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
2695 ((u32) scp->cmnd[3] << 16) |
2696 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2697 }
2698
2699 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
2700 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
adam radfordf9eff812012-10-01 19:27:07 -07002701 io_info.numBlocks = datalength;
adam radford9c915a82010-12-21 13:34:31 -08002702 io_info.ldTgtId = device_id;
Shivasharan S8bf7c652017-02-10 00:59:03 -08002703 io_info.r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002704 scsi_buff_len = scsi_bufflen(scp);
2705 io_request->DataLength = cpu_to_le32(scsi_buff_len);
adam radford9c915a82010-12-21 13:34:31 -08002706
Christoph Hellwig60ee6522018-10-10 19:31:25 +02002707 if (scp->sc_data_direction == DMA_FROM_DEVICE)
adam radford9c915a82010-12-21 13:34:31 -08002708 io_info.isRead = 1;
2709
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302710 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002711 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
adam radford9c915a82010-12-21 13:34:31 -08002712
Shivasharan Sd2d03582017-02-10 00:59:19 -08002713 if (ld < instance->fw_supported_vd_count)
2714 raid = MR_LdRaidGet(ld, local_map_ptr);
2715
2716 if (!raid || (!fusion->fast_path_io)) {
Shivasharan S9e770182018-10-16 23:37:49 -07002717 rctx->reg_lock_flags = 0;
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002718 fp_possible = false;
adam radford9c915a82010-12-21 13:34:31 -08002719 } else {
Shivasharan S9e770182018-10-16 23:37:49 -07002720 if (MR_BuildRaidContext(instance, &io_info, rctx,
adam radford21c9e162013-09-06 15:27:14 -07002721 local_map_ptr, &raidLUN))
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002722 fp_possible = (io_info.fpOkForIo > 0) ? true : false;
adam radford9c915a82010-12-21 13:34:31 -08002723 }
2724
Ming Leiadbe5522018-03-13 17:42:40 +08002725 cmd->request_desc->SCSIIO.MSIxIndex =
2726 instance->reply_map[raw_smp_processor_id()];
adam radfordc8e858f2011-10-08 18:15:13 -07002727
Shivasharan Sf369a312017-10-19 02:48:52 -07002728 if (instance->adapter_type == VENTURA_SERIES) {
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002729 /* FP for Optimal raid level 1.
2730 * All large RAID-1 writes (> 32 KiB, both WT and WB modes)
2731 * are built by the driver as LD I/Os.
2732 * All small RAID-1 WT writes (<= 32 KiB) are built as FP I/Os
2733 * (there is never a reason to process these as buffered writes)
2734 * All small RAID-1 WB writes (<= 32 KiB) are built as FP I/Os
2735 * with the SLD bit asserted.
2736 */
Shivasharan S8bf7c652017-02-10 00:59:03 -08002737 if (io_info.r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002738 mrdev_priv = scp->device->hostdata;
2739
2740 if (atomic_inc_return(&instance->fw_outstanding) >
2741 (instance->host->can_queue)) {
2742 fp_possible = false;
2743 atomic_dec(&instance->fw_outstanding);
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002744 } else if ((scsi_buff_len > MR_LARGE_IO_MIN_SIZE) ||
Shivasharan S49524b32017-03-10 03:22:13 -08002745 (atomic_dec_if_positive(&mrdev_priv->r1_ldio_hint) > 0)) {
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002746 fp_possible = false;
2747 atomic_dec(&instance->fw_outstanding);
2748 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2749 atomic_set(&mrdev_priv->r1_ldio_hint,
2750 instance->r1_ldio_hint_default);
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002751 }
2752 }
2753
Shivasharan S4959e612018-01-05 05:27:45 -08002754 if (!fp_possible ||
2755 (io_info.isRead && io_info.ra_capable)) {
2756 spin_lock_irqsave(&instance->stream_lock,
2757 spinlock_flags);
2758 megasas_stream_detect(instance, cmd, &io_info);
2759 spin_unlock_irqrestore(&instance->stream_lock,
2760 spinlock_flags);
2761 /* In ventura if stream detected for a read and it is
2762 * read ahead capable make this IO as LDIO
2763 */
Shivasharan S9e770182018-10-16 23:37:49 -07002764 if (is_stream_detected(rctx_g35))
Shivasharan S4959e612018-01-05 05:27:45 -08002765 fp_possible = false;
2766 }
2767
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002768 /* If raid is NULL, set CPU affinity to default CPU0 */
2769 if (raid)
Shivasharan S9e770182018-10-16 23:37:49 -07002770 megasas_set_raidflag_cpu_affinity(&io_request->RaidContext,
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002771 raid, fp_possible, io_info.isRead,
2772 scsi_buff_len);
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002773 else
Shivasharan S9e770182018-10-16 23:37:49 -07002774 rctx_g35->routing_flags |=
Shivasharan Sa174118b2017-02-10 00:59:21 -08002775 (MR_RAID_CTX_CPUSEL_0 << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002776 }
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002777
adam radford9c915a82010-12-21 13:34:31 -08002778 if (fp_possible) {
2779 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
2780 local_map_ptr, start_lba_lo);
adam radford9c915a82010-12-21 13:34:31 -08002781 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2782 cmd->request_desc->SCSIIO.RequestFlags =
Sumit Saxena2c048352016-01-28 21:04:24 +05302783 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
adam radford9c915a82010-12-21 13:34:31 -08002784 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Shivasharan Sc3651782017-10-19 02:48:48 -07002785 if (instance->adapter_type == INVADER_SERIES) {
Shivasharan S9e770182018-10-16 23:37:49 -07002786 if (rctx->reg_lock_flags == REGION_TYPE_UNUSED)
adam radford36807e62011-10-08 18:15:06 -07002787 cmd->request_desc->SCSIIO.RequestFlags =
2788 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2789 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Shivasharan S9e770182018-10-16 23:37:49 -07002790 rctx->type = MPI2_TYPE_CUDA;
2791 rctx->nseg = 0x1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302792 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
Shivasharan S9e770182018-10-16 23:37:49 -07002793 rctx->reg_lock_flags |=
adam radford36807e62011-10-08 18:15:06 -07002794 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
2795 MR_RL_FLAGS_SEQ_NUM_ENABLE);
Shivasharan Sf369a312017-10-19 02:48:52 -07002796 } else if (instance->adapter_type == VENTURA_SERIES) {
Shivasharan S9e770182018-10-16 23:37:49 -07002797 rctx_g35->nseg_type |= (1 << RAID_CONTEXT_NSEG_SHIFT);
2798 rctx_g35->nseg_type |= (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2799 rctx_g35->routing_flags |= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002800 io_request->IoFlags |=
Shivasharan Sa174118b2017-02-10 00:59:21 -08002801 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
adam radford36807e62011-10-08 18:15:06 -07002802 }
Shivasharan S5fc499b2017-02-10 00:59:17 -08002803 if (fusion->load_balance_info &&
2804 (fusion->load_balance_info[device_id].loadBalanceFlag) &&
2805 (io_info.isRead)) {
adam radford9c915a82010-12-21 13:34:31 -08002806 io_info.devHandle =
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05302807 get_updated_dev_handle(instance,
adam radford9c915a82010-12-21 13:34:31 -08002808 &fusion->load_balance_info[device_id],
Shivasharan S33203bc2017-02-10 00:59:12 -08002809 &io_info, local_map_ptr);
adam radford9c915a82010-12-21 13:34:31 -08002810 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05302811 cmd->pd_r1_lb = io_info.pd_after_lb;
Shivasharan Sf369a312017-10-19 02:48:52 -07002812 if (instance->adapter_type == VENTURA_SERIES)
Shivasharan S9e770182018-10-16 23:37:49 -07002813 rctx_g35->span_arm = io_info.span_arm;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002814 else
Shivasharan S9e770182018-10-16 23:37:49 -07002815 rctx->span_arm = io_info.span_arm;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002816
adam radford9c915a82010-12-21 13:34:31 -08002817 } else
2818 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05302819
Shivasharan Sf369a312017-10-19 02:48:52 -07002820 if (instance->adapter_type == VENTURA_SERIES)
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002821 cmd->r1_alt_dev_handle = io_info.r1_alt_dev_handle;
Shivasharan S8bf7c652017-02-10 00:59:03 -08002822 else
2823 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002824
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05302825 if ((raidLUN[0] == 1) &&
sumit.saxena@avagotech.com75b96061e2015-08-31 17:24:01 +05302826 (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) {
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05302827 instance->dev_handle = !(instance->dev_handle);
2828 io_info.devHandle =
2829 local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
2830 }
2831
adam radford9c915a82010-12-21 13:34:31 -08002832 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
2833 io_request->DevHandle = io_info.devHandle;
Shivasharan S33203bc2017-02-10 00:59:12 -08002834 cmd->pd_interface = io_info.pd_interface;
adam radford21c9e162013-09-06 15:27:14 -07002835 /* populate the LUN field */
2836 memcpy(io_request->LUN, raidLUN, 8);
adam radford9c915a82010-12-21 13:34:31 -08002837 } else {
Shivasharan S9e770182018-10-16 23:37:49 -07002838 rctx->timeout_value =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302839 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
adam radford9c915a82010-12-21 13:34:31 -08002840 cmd->request_desc->SCSIIO.RequestFlags =
2841 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
2842 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Shivasharan Sc3651782017-10-19 02:48:48 -07002843 if (instance->adapter_type == INVADER_SERIES) {
Sumit Saxena8f050242016-01-28 21:04:27 +05302844 if (io_info.do_fp_rlbypass ||
Shivasharan S9e770182018-10-16 23:37:49 -07002845 (rctx->reg_lock_flags == REGION_TYPE_UNUSED))
adam radford36807e62011-10-08 18:15:06 -07002846 cmd->request_desc->SCSIIO.RequestFlags =
2847 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2848 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Shivasharan S9e770182018-10-16 23:37:49 -07002849 rctx->type = MPI2_TYPE_CUDA;
2850 rctx->reg_lock_flags |=
adam radford36807e62011-10-08 18:15:06 -07002851 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
Shivasharan S9e770182018-10-16 23:37:49 -07002852 MR_RL_FLAGS_SEQ_NUM_ENABLE);
2853 rctx->nseg = 0x1;
Shivasharan Sf369a312017-10-19 02:48:52 -07002854 } else if (instance->adapter_type == VENTURA_SERIES) {
Shivasharan S9e770182018-10-16 23:37:49 -07002855 rctx_g35->routing_flags |= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2856 rctx_g35->nseg_type |= (1 << RAID_CONTEXT_NSEG_SHIFT);
2857 rctx_g35->nseg_type |= (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
adam radford36807e62011-10-08 18:15:06 -07002858 }
2859 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302860 io_request->DevHandle = cpu_to_le16(device_id);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002861
adam radford9c915a82010-12-21 13:34:31 -08002862 } /* Not FP */
2863}
2864
2865/**
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302866 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
adam radford9c915a82010-12-21 13:34:31 -08002867 * @instance: Adapter soft state
2868 * @scp: SCSI command
2869 * @cmd: Command to be prepared
2870 *
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302871 * Prepares the io_request frame for non-rw io cmds for vd.
adam radford9c915a82010-12-21 13:34:31 -08002872 */
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302873static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
2874 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
adam radford9c915a82010-12-21 13:34:31 -08002875{
2876 u32 device_id;
2877 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
Shivasharan S25fb13d2017-02-10 00:59:29 -08002878 u16 ld;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302879 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
adam radford9c915a82010-12-21 13:34:31 -08002880 struct fusion_context *fusion = instance->ctrl_context;
adam radford21c9e162013-09-06 15:27:14 -07002881 u8 span, physArm;
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05302882 __le16 devHandle;
Shivasharan Sd2d03582017-02-10 00:59:19 -08002883 u32 arRef, pd;
adam radford21c9e162013-09-06 15:27:14 -07002884 struct MR_LD_RAID *raid;
2885 struct RAID_CONTEXT *pRAID_Context;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302886 u8 fp_possible = 1;
adam radford9c915a82010-12-21 13:34:31 -08002887
2888 io_request = cmd->io_request;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302889 device_id = MEGASAS_DEV_INDEX(scmd);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302890 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302891 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302892 /* get RAID_Context pointer */
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002893 pRAID_Context = &io_request->RaidContext.raid_context;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302894 /* Check with FW team */
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002895 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2896 pRAID_Context->reg_lock_row_lba = 0;
2897 pRAID_Context->reg_lock_length = 0;
adam radford21c9e162013-09-06 15:27:14 -07002898
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302899 if (fusion->fast_path_io && (
2900 device_id < instance->fw_supported_vd_count)) {
Sumit.Saxena@avagotech.comab2f0602015-01-05 20:06:08 +05302901
adam radford21c9e162013-09-06 15:27:14 -07002902 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
Sumit.Saxena@avagotech.comab2f0602015-01-05 20:06:08 +05302903 if (ld >= instance->fw_supported_vd_count)
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302904 fp_possible = 0;
Shivasharan Sd2d03582017-02-10 00:59:19 -08002905 else {
2906 raid = MR_LdRaidGet(ld, local_map_ptr);
2907 if (!(raid->capability.fpNonRWCapable))
2908 fp_possible = 0;
2909 }
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302910 } else
2911 fp_possible = 0;
adam radford21c9e162013-09-06 15:27:14 -07002912
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302913 if (!fp_possible) {
2914 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2915 io_request->DevHandle = cpu_to_le16(device_id);
2916 io_request->LUN[1] = scmd->device->lun;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002917 pRAID_Context->timeout_value =
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302918 cpu_to_le16 (scmd->request->timeout / HZ);
2919 cmd->request_desc->SCSIIO.RequestFlags =
2920 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2921 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2922 } else {
adam radford21c9e162013-09-06 15:27:14 -07002923
2924 /* set RAID context values */
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002925 pRAID_Context->config_seq_num = raid->seqNum;
Shivasharan Sf369a312017-10-19 02:48:52 -07002926 if (instance->adapter_type != VENTURA_SERIES)
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002927 pRAID_Context->reg_lock_flags = REGION_TYPE_SHARED_READ;
2928 pRAID_Context->timeout_value =
2929 cpu_to_le16(raid->fpIoTimeoutForLd);
adam radford21c9e162013-09-06 15:27:14 -07002930
2931 /* get the DevHandle for the PD (since this is
2932 fpNonRWCapable, this is a single disk RAID0) */
2933 span = physArm = 0;
2934 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
2935 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
2936 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
2937
2938 /* build request descriptor */
2939 cmd->request_desc->SCSIIO.RequestFlags =
Sumit Saxena2c048352016-01-28 21:04:24 +05302940 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302941 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford21c9e162013-09-06 15:27:14 -07002942 cmd->request_desc->SCSIIO.DevHandle = devHandle;
2943
2944 /* populate the LUN field */
2945 memcpy(io_request->LUN, raid->LUN, 8);
2946
2947 /* build the raidScsiIO structure */
2948 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2949 io_request->DevHandle = devHandle;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302950 }
2951}
adam radford21c9e162013-09-06 15:27:14 -07002952
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302953/**
2954 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
2955 * @instance: Adapter soft state
2956 * @scp: SCSI command
2957 * @cmd: Command to be prepared
2958 * @fp_possible: parameter to detect fast path or firmware path io.
2959 *
2960 * Prepares the io_request frame for rw/non-rw io cmds for syspds
2961 */
2962static void
2963megasas_build_syspd_fusion(struct megasas_instance *instance,
Shivasharan S1d6dbd12017-02-10 00:59:30 -08002964 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd,
2965 bool fp_possible)
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302966{
2967 u32 device_id;
2968 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2969 u16 pd_index = 0;
2970 u16 os_timeout_value;
2971 u16 timeout_limit;
2972 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2973 struct RAID_CONTEXT *pRAID_Context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302974 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
Shivasharan S33203bc2017-02-10 00:59:12 -08002975 struct MR_PRIV_DEVICE *mr_device_priv_data;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302976 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302977 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
adam radford21c9e162013-09-06 15:27:14 -07002978
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302979 device_id = MEGASAS_DEV_INDEX(scmd);
2980 pd_index = MEGASAS_PD_INDEX(scmd);
2981 os_timeout_value = scmd->request->timeout / HZ;
Shivasharan S33203bc2017-02-10 00:59:12 -08002982 mr_device_priv_data = scmd->device->hostdata;
2983 cmd->pd_interface = mr_device_priv_data->interface_type;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302984
2985 io_request = cmd->io_request;
2986 /* get RAID_Context pointer */
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002987 pRAID_Context = &io_request->RaidContext.raid_context;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002988 pRAID_Context->reg_lock_flags = 0;
2989 pRAID_Context->reg_lock_row_lba = 0;
2990 pRAID_Context->reg_lock_length = 0;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302991 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2992 io_request->LUN[1] = scmd->device->lun;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002993 pRAID_Context->raid_flags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302994 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
2995
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302996 /* If FW supports PD sequence number */
2997 if (instance->use_seqnum_jbod_fp &&
2998 instance->pd_list[pd_index].driveType == TYPE_DISK) {
2999 /* TgtId must be incremented by 255 as jbod seq number is index
3000 * below raid map
3001 */
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05003002 /* More than 256 PD/JBOD support for Ventura */
3003 if (instance->support_morethan256jbod)
3004 pRAID_Context->virtual_disk_tgt_id =
3005 pd_sync->seq[pd_index].pd_target_id;
3006 else
Shivasharan Sa174118b2017-02-10 00:59:21 -08003007 pRAID_Context->virtual_disk_tgt_id =
3008 cpu_to_le16(device_id + (MAX_PHYSICAL_DEVICES - 1));
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05003009 pRAID_Context->config_seq_num = pd_sync->seq[pd_index].seqNum;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303010 io_request->DevHandle = pd_sync->seq[pd_index].devHandle;
Shivasharan Sf369a312017-10-19 02:48:52 -07003011 if (instance->adapter_type == VENTURA_SERIES) {
Shivasharan Sa174118b2017-02-10 00:59:21 -08003012 io_request->RaidContext.raid_context_g35.routing_flags |=
3013 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
3014 io_request->RaidContext.raid_context_g35.nseg_type |=
3015 (1 << RAID_CONTEXT_NSEG_SHIFT);
3016 io_request->RaidContext.raid_context_g35.nseg_type |=
3017 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
3018 } else {
3019 pRAID_Context->type = MPI2_TYPE_CUDA;
3020 pRAID_Context->nseg = 0x1;
3021 pRAID_Context->reg_lock_flags |=
3022 (MR_RL_FLAGS_SEQ_NUM_ENABLE|MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
3023 }
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303024 } else if (fusion->fast_path_io) {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05003025 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3026 pRAID_Context->config_seq_num = 0;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303027 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
3028 io_request->DevHandle =
3029 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
3030 } else {
3031 /* Want to send all IO via FW path */
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05003032 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
3033 pRAID_Context->config_seq_num = 0;
Sumit Saxena9fb74c42015-11-05 21:17:38 +05303034 io_request->DevHandle = cpu_to_le16(0xFFFF);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303035 }
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05303036
3037 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05303038
Ming Leiadbe5522018-03-13 17:42:40 +08003039 cmd->request_desc->SCSIIO.MSIxIndex =
3040 instance->reply_map[raw_smp_processor_id()];
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05303041
3042 if (!fp_possible) {
3043 /* system pd firmware path */
adam radford9c915a82010-12-21 13:34:31 -08003044 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
adam radford9c915a82010-12-21 13:34:31 -08003045 cmd->request_desc->SCSIIO.RequestFlags =
3046 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05303047 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05003048 pRAID_Context->timeout_value = cpu_to_le16(os_timeout_value);
3049 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05303050 } else {
Shivasharan S3239b8c2018-04-06 02:02:11 -07003051 if (os_timeout_value)
3052 os_timeout_value++;
3053
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05303054 /* system pd Fast Path */
3055 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05303056 timeout_limit = (scmd->device->type == TYPE_DISK) ?
3057 255 : 0xFFFF;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05003058 pRAID_Context->timeout_value =
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05303059 cpu_to_le16((os_timeout_value > timeout_limit) ?
3060 timeout_limit : os_timeout_value);
Shivasharan Sc3651782017-10-19 02:48:48 -07003061 if (instance->adapter_type >= INVADER_SERIES)
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05303062 io_request->IoFlags |=
3063 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
Kashyap Desaid5573582016-10-21 06:33:35 -07003064
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05303065 cmd->request_desc->SCSIIO.RequestFlags =
Sumit Saxena2c048352016-01-28 21:04:24 +05303066 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05303067 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford9c915a82010-12-21 13:34:31 -08003068 }
adam radford9c915a82010-12-21 13:34:31 -08003069}
3070
3071/**
3072 * megasas_build_io_fusion - Prepares IOs to devices
3073 * @instance: Adapter soft state
3074 * @scp: SCSI command
3075 * @cmd: Command to be prepared
3076 *
3077 * Invokes helper functions to prepare request frames
3078 * and sets flags appropriate for IO/Non-IO cmd
3079 */
3080int
3081megasas_build_io_fusion(struct megasas_instance *instance,
3082 struct scsi_cmnd *scp,
3083 struct megasas_cmd_fusion *cmd)
3084{
Shivasharan S33203bc2017-02-10 00:59:12 -08003085 int sge_count;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05303086 u8 cmd_type;
adam radford9c915a82010-12-21 13:34:31 -08003087 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
Shivasharan S1d6dbd12017-02-10 00:59:30 -08003088 struct MR_PRIV_DEVICE *mr_device_priv_data;
3089 mr_device_priv_data = scp->device->hostdata;
adam radford9c915a82010-12-21 13:34:31 -08003090
adam radford9c915a82010-12-21 13:34:31 -08003091 /* Zero out some fields so they don't get reused */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02003092 memset(io_request->LUN, 0x0, 8);
adam radford9c915a82010-12-21 13:34:31 -08003093 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
3094 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
3095 io_request->EEDPFlags = 0;
3096 io_request->Control = 0;
3097 io_request->EEDPBlockSize = 0;
adam radford36807e62011-10-08 18:15:06 -07003098 io_request->ChainOffset = 0;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05003099 io_request->RaidContext.raid_context.raid_flags = 0;
3100 io_request->RaidContext.raid_context.type = 0;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05003101 io_request->RaidContext.raid_context.nseg = 0;
adam radford9c915a82010-12-21 13:34:31 -08003102
3103 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
3104 /*
3105 * Just the CDB length,rest of the Flags are zero
3106 * This will be modified for FP in build_ldio_fusion
3107 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303108 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
adam radford9c915a82010-12-21 13:34:31 -08003109
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05303110 switch (cmd_type = megasas_cmd_type(scp)) {
3111 case READ_WRITE_LDIO:
adam radford9c915a82010-12-21 13:34:31 -08003112 megasas_build_ldio_fusion(instance, scp, cmd);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05303113 break;
3114 case NON_READ_WRITE_LDIO:
3115 megasas_build_ld_nonrw_fusion(instance, scp, cmd);
3116 break;
3117 case READ_WRITE_SYSPDIO:
Shivasharan S1d6dbd12017-02-10 00:59:30 -08003118 megasas_build_syspd_fusion(instance, scp, cmd, true);
3119 break;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05303120 case NON_READ_WRITE_SYSPDIO:
Shivasharan S1d6dbd12017-02-10 00:59:30 -08003121 if (instance->secure_jbod_support ||
3122 mr_device_priv_data->is_tm_capable)
3123 megasas_build_syspd_fusion(instance, scp, cmd, false);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05303124 else
Shivasharan S1d6dbd12017-02-10 00:59:30 -08003125 megasas_build_syspd_fusion(instance, scp, cmd, true);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05303126 break;
3127 default:
3128 break;
3129 }
adam radford9c915a82010-12-21 13:34:31 -08003130
3131 /*
3132 * Construct SGL
3133 */
3134
Shivasharan S33203bc2017-02-10 00:59:12 -08003135 sge_count = megasas_make_sgl(instance, scp, cmd);
adam radford9c915a82010-12-21 13:34:31 -08003136
Shivasharan S33203bc2017-02-10 00:59:12 -08003137 if (sge_count > instance->max_num_sge || (sge_count < 0)) {
3138 dev_err(&instance->pdev->dev,
3139 "%s %d sge_count (%d) is out of range. Range is: 0-%d\n",
3140 __func__, __LINE__, sge_count, instance->max_num_sge);
adam radford9c915a82010-12-21 13:34:31 -08003141 return 1;
3142 }
3143
Shivasharan Sf369a312017-10-19 02:48:52 -07003144 if (instance->adapter_type == VENTURA_SERIES) {
Shivasharan Sa174118b2017-02-10 00:59:21 -08003145 set_num_sge(&io_request->RaidContext.raid_context_g35, sge_count);
3146 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.routing_flags);
3147 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.nseg_type);
3148 } else {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05003149 /* numSGE store lower 8 bit of sge_count.
3150 * numSGEExt store higher 8 bit of sge_count
3151 */
3152 io_request->RaidContext.raid_context.num_sge = sge_count;
3153 io_request->RaidContext.raid_context.num_sge_ext =
3154 (u8)(sge_count >> 8);
3155 }
adam radford9c915a82010-12-21 13:34:31 -08003156
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303157 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
adam radford9c915a82010-12-21 13:34:31 -08003158
Christoph Hellwig60ee6522018-10-10 19:31:25 +02003159 if (scp->sc_data_direction == DMA_TO_DEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303160 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
Christoph Hellwig60ee6522018-10-10 19:31:25 +02003161 else if (scp->sc_data_direction == DMA_FROM_DEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303162 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
adam radford9c915a82010-12-21 13:34:31 -08003163
3164 io_request->SGLOffset0 =
3165 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
3166
Shivasharan S107a60d2017-10-19 02:49:05 -07003167 io_request->SenseBufferLowAddress =
3168 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr));
adam radford9c915a82010-12-21 13:34:31 -08003169 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
3170
3171 cmd->scmd = scp;
3172 scp->SCp.ptr = (char *)cmd;
3173
3174 return 0;
3175}
3176
Shivasharan S52205ac2017-02-10 00:59:08 -08003177static union MEGASAS_REQUEST_DESCRIPTOR_UNION *
adam radford9c915a82010-12-21 13:34:31 -08003178megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
3179{
3180 u8 *p;
3181 struct fusion_context *fusion;
3182
adam radford9c915a82010-12-21 13:34:31 -08003183 fusion = instance->ctrl_context;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003184 p = fusion->req_frames_desc +
3185 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * index;
adam radford9c915a82010-12-21 13:34:31 -08003186
3187 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
3188}
3189
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003190
3191/* megasas_prepate_secondRaid1_IO
3192 * It prepares the raid 1 second IO
3193 */
3194void megasas_prepare_secondRaid1_IO(struct megasas_instance *instance,
3195 struct megasas_cmd_fusion *cmd,
3196 struct megasas_cmd_fusion *r1_cmd)
3197{
3198 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc, *req_desc2 = NULL;
3199 struct fusion_context *fusion;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003200 fusion = instance->ctrl_context;
3201 req_desc = cmd->request_desc;
Shivasharan S8bf7c652017-02-10 00:59:03 -08003202 /* copy the io request frame as well as 8 SGEs data for r1 command*/
3203 memcpy(r1_cmd->io_request, cmd->io_request,
3204 (sizeof(struct MPI2_RAID_SCSI_IO_REQUEST)));
3205 memcpy(&r1_cmd->io_request->SGL, &cmd->io_request->SGL,
3206 (fusion->max_sge_in_main_msg * sizeof(union MPI2_SGE_IO_UNION)));
3207 /*sense buffer is different for r1 command*/
3208 r1_cmd->io_request->SenseBufferLowAddress =
Shivasharan S107a60d2017-10-19 02:49:05 -07003209 cpu_to_le32(lower_32_bits(r1_cmd->sense_phys_addr));
Shivasharan S8bf7c652017-02-10 00:59:03 -08003210 r1_cmd->scmd = cmd->scmd;
3211 req_desc2 = megasas_get_request_descriptor(instance,
3212 (r1_cmd->index - 1));
3213 req_desc2->Words = 0;
3214 r1_cmd->request_desc = req_desc2;
3215 req_desc2->SCSIIO.SMID = cpu_to_le16(r1_cmd->index);
3216 req_desc2->SCSIIO.RequestFlags = req_desc->SCSIIO.RequestFlags;
3217 r1_cmd->request_desc->SCSIIO.DevHandle = cmd->r1_alt_dev_handle;
3218 r1_cmd->io_request->DevHandle = cmd->r1_alt_dev_handle;
3219 r1_cmd->r1_alt_dev_handle = cmd->io_request->DevHandle;
3220 cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
3221 cpu_to_le16(r1_cmd->index);
3222 r1_cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
3223 cpu_to_le16(cmd->index);
3224 /*MSIxIndex of both commands request descriptors should be same*/
3225 r1_cmd->request_desc->SCSIIO.MSIxIndex =
3226 cmd->request_desc->SCSIIO.MSIxIndex;
3227 /*span arm is different for r1 cmd*/
3228 r1_cmd->io_request->RaidContext.raid_context_g35.span_arm =
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003229 cmd->io_request->RaidContext.raid_context_g35.span_arm + 1;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003230}
3231
adam radford9c915a82010-12-21 13:34:31 -08003232/**
3233 * megasas_build_and_issue_cmd_fusion -Main routine for building and
3234 * issuing non IOCTL cmd
3235 * @instance: Adapter soft state
3236 * @scmd: pointer to scsi cmd from OS
3237 */
3238static u32
3239megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
3240 struct scsi_cmnd *scmd)
3241{
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003242 struct megasas_cmd_fusion *cmd, *r1_cmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -08003243 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3244 u32 index;
3245 struct fusion_context *fusion;
3246
3247 fusion = instance->ctrl_context;
3248
Sumit Saxena308ec452016-01-28 21:04:30 +05303249 if ((megasas_cmd_type(scmd) == READ_WRITE_LDIO) &&
3250 instance->ldio_threshold &&
3251 (atomic_inc_return(&instance->ldio_outstanding) >
3252 instance->ldio_threshold)) {
3253 atomic_dec(&instance->ldio_outstanding);
3254 return SCSI_MLQUEUE_DEVICE_BUSY;
3255 }
3256
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003257 if (atomic_inc_return(&instance->fw_outstanding) >
3258 instance->host->can_queue) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003259 atomic_dec(&instance->fw_outstanding);
3260 return SCSI_MLQUEUE_HOST_BUSY;
3261 }
3262
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303263 cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
adam radford9c915a82010-12-21 13:34:31 -08003264
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003265 if (!cmd) {
3266 atomic_dec(&instance->fw_outstanding);
3267 return SCSI_MLQUEUE_HOST_BUSY;
3268 }
3269
adam radford9c915a82010-12-21 13:34:31 -08003270 index = cmd->index;
3271
3272 req_desc = megasas_get_request_descriptor(instance, index-1);
adam radford9c915a82010-12-21 13:34:31 -08003273
3274 req_desc->Words = 0;
3275 cmd->request_desc = req_desc;
adam radford9c915a82010-12-21 13:34:31 -08003276
3277 if (megasas_build_io_fusion(instance, scmd, cmd)) {
3278 megasas_return_cmd_fusion(instance, cmd);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003279 dev_err(&instance->pdev->dev, "Error building command\n");
adam radford9c915a82010-12-21 13:34:31 -08003280 cmd->request_desc = NULL;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003281 atomic_dec(&instance->fw_outstanding);
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05303282 return SCSI_MLQUEUE_HOST_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08003283 }
3284
3285 req_desc = cmd->request_desc;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303286 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08003287
3288 if (cmd->io_request->ChainOffset != 0 &&
3289 cmd->io_request->ChainOffset != 0xF)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003290 dev_err(&instance->pdev->dev, "The chain offset value is not "
adam radford9c915a82010-12-21 13:34:31 -08003291 "correct : %x\n", cmd->io_request->ChainOffset);
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003292 /*
3293 * if it is raid 1/10 fp write capable.
3294 * try to get second command from pool and construct it.
3295 * From FW, it has confirmed that lba values of two PDs
3296 * corresponds to single R1/10 LD are always same
3297 *
3298 */
3299 /* driver side count always should be less than max_fw_cmds
3300 * to get new command
3301 */
Shivasharan S8bf7c652017-02-10 00:59:03 -08003302 if (cmd->r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003303 r1_cmd = megasas_get_cmd_fusion(instance,
3304 (scmd->request->tag + instance->max_fw_cmds));
3305 megasas_prepare_secondRaid1_IO(instance, cmd, r1_cmd);
3306 }
3307
Shivasharan S18bbcab2017-02-10 00:59:01 -08003308
adam radford9c915a82010-12-21 13:34:31 -08003309 /*
3310 * Issue the command to the FW
3311 */
adam radford9c915a82010-12-21 13:34:31 -08003312
Shivasharan S45b8a352017-02-10 00:59:04 -08003313 megasas_fire_cmd_fusion(instance, req_desc);
adam radford9c915a82010-12-21 13:34:31 -08003314
Shivasharan S18bbcab2017-02-10 00:59:01 -08003315 if (r1_cmd)
Shivasharan S45b8a352017-02-10 00:59:04 -08003316 megasas_fire_cmd_fusion(instance, r1_cmd->request_desc);
Shivasharan S18bbcab2017-02-10 00:59:01 -08003317
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003318
adam radford9c915a82010-12-21 13:34:31 -08003319 return 0;
3320}
3321
3322/**
Shivasharan S8bf7c652017-02-10 00:59:03 -08003323 * megasas_complete_r1_command -
3324 * completes R1 FP write commands which has valid peer smid
3325 * @instance: Adapter soft state
3326 * @cmd_fusion: MPT command frame
3327 *
3328 */
3329static inline void
3330megasas_complete_r1_command(struct megasas_instance *instance,
3331 struct megasas_cmd_fusion *cmd)
3332{
3333 u8 *sense, status, ex_status;
3334 u32 data_length;
3335 u16 peer_smid;
3336 struct fusion_context *fusion;
3337 struct megasas_cmd_fusion *r1_cmd = NULL;
3338 struct scsi_cmnd *scmd_local = NULL;
3339 struct RAID_CONTEXT_G35 *rctx_g35;
3340
3341 rctx_g35 = &cmd->io_request->RaidContext.raid_context_g35;
3342 fusion = instance->ctrl_context;
3343 peer_smid = le16_to_cpu(rctx_g35->smid.peer_smid);
3344
3345 r1_cmd = fusion->cmd_list[peer_smid - 1];
3346 scmd_local = cmd->scmd;
3347 status = rctx_g35->status;
3348 ex_status = rctx_g35->ex_status;
3349 data_length = cmd->io_request->DataLength;
3350 sense = cmd->sense;
3351
3352 cmd->cmd_completed = true;
3353
3354 /* Check if peer command is completed or not*/
3355 if (r1_cmd->cmd_completed) {
3356 rctx_g35 = &r1_cmd->io_request->RaidContext.raid_context_g35;
3357 if (rctx_g35->status != MFI_STAT_OK) {
3358 status = rctx_g35->status;
3359 ex_status = rctx_g35->ex_status;
3360 data_length = r1_cmd->io_request->DataLength;
3361 sense = r1_cmd->sense;
3362 }
3363
3364 megasas_return_cmd_fusion(instance, r1_cmd);
3365 map_cmd_status(fusion, scmd_local, status, ex_status,
3366 le32_to_cpu(data_length), sense);
3367 if (instance->ldio_threshold &&
3368 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
3369 atomic_dec(&instance->ldio_outstanding);
3370 scmd_local->SCp.ptr = NULL;
3371 megasas_return_cmd_fusion(instance, cmd);
3372 scsi_dma_unmap(scmd_local);
3373 scmd_local->scsi_done(scmd_local);
3374 }
3375}
3376
3377/**
adam radford9c915a82010-12-21 13:34:31 -08003378 * complete_cmd_fusion - Completes command
3379 * @instance: Adapter soft state
3380 * Completes all commands that is in reply descriptor queue
3381 */
3382int
adam radfordc8e858f2011-10-08 18:15:13 -07003383complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
adam radford9c915a82010-12-21 13:34:31 -08003384{
3385 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
3386 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
3387 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
3388 struct fusion_context *fusion;
3389 struct megasas_cmd *cmd_mfi;
Shivasharan S8bf7c652017-02-10 00:59:03 -08003390 struct megasas_cmd_fusion *cmd_fusion;
adam radford9c915a82010-12-21 13:34:31 -08003391 u16 smid, num_completed;
Shivasharan S8bf7c652017-02-10 00:59:03 -08003392 u8 reply_descript_type, *sense, status, extStatus;
3393 u32 device_id, data_length;
adam radford9c915a82010-12-21 13:34:31 -08003394 union desc_value d_val;
3395 struct LD_LOAD_BALANCE_INFO *lbinfo;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05303396 int threshold_reply_count = 0;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303397 struct scsi_cmnd *scmd_local = NULL;
Sumit Saxena18365b12016-01-28 21:04:25 +05303398 struct MR_TASK_MANAGE_REQUEST *mr_tm_req;
3399 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req;
adam radford9c915a82010-12-21 13:34:31 -08003400
3401 fusion = instance->ctrl_context;
3402
Sumit Saxena8a01a412016-01-28 21:04:32 +05303403 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
adam radford9c915a82010-12-21 13:34:31 -08003404 return IRQ_HANDLED;
3405
Sumit Saxena179ac142016-01-28 21:04:28 +05303406 desc = fusion->reply_frames_desc[MSIxIndex] +
3407 fusion->last_reply_idx[MSIxIndex];
adam radford9c915a82010-12-21 13:34:31 -08003408
3409 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3410
3411 d_val.word = desc->Words;
3412
3413 reply_descript_type = reply_desc->ReplyFlags &
3414 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3415
3416 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3417 return IRQ_NONE;
3418
adam radford9c915a82010-12-21 13:34:31 -08003419 num_completed = 0;
3420
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05303421 while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
3422 d_val.u.high != cpu_to_le32(UINT_MAX)) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003423
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303424 smid = le16_to_cpu(reply_desc->SMID);
adam radford9c915a82010-12-21 13:34:31 -08003425 cmd_fusion = fusion->cmd_list[smid - 1];
Shivasharan S8bf7c652017-02-10 00:59:03 -08003426 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
3427 cmd_fusion->io_request;
adam radford9c915a82010-12-21 13:34:31 -08003428
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303429 scmd_local = cmd_fusion->scmd;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05003430 status = scsi_io_req->RaidContext.raid_context.status;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05003431 extStatus = scsi_io_req->RaidContext.raid_context.ex_status;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003432 sense = cmd_fusion->sense;
3433 data_length = scsi_io_req->DataLength;
adam radford9c915a82010-12-21 13:34:31 -08003434
3435 switch (scsi_io_req->Function) {
Sumit Saxena18365b12016-01-28 21:04:25 +05303436 case MPI2_FUNCTION_SCSI_TASK_MGMT:
3437 mr_tm_req = (struct MR_TASK_MANAGE_REQUEST *)
3438 cmd_fusion->io_request;
3439 mpi_tm_req = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *)
3440 &mr_tm_req->TmRequest;
3441 dev_dbg(&instance->pdev->dev, "TM completion:"
3442 "type: 0x%x TaskMID: 0x%x\n",
3443 mpi_tm_req->TaskType, mpi_tm_req->TaskMID);
3444 complete(&cmd_fusion->done);
3445 break;
adam radford9c915a82010-12-21 13:34:31 -08003446 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
3447 /* Update load balancing info */
Shivasharan S8bf7c652017-02-10 00:59:03 -08003448 if (fusion->load_balance_info &&
3449 (cmd_fusion->scmd->SCp.Status &
3450 MEGASAS_LOAD_BALANCE_FLAG)) {
3451 device_id = MEGASAS_DEV_INDEX(scmd_local);
3452 lbinfo = &fusion->load_balance_info[device_id];
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05303453 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
Shivasharan S8bf7c652017-02-10 00:59:03 -08003454 cmd_fusion->scmd->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
adam radford9c915a82010-12-21 13:34:31 -08003455 }
Shivasharan S8bf7c652017-02-10 00:59:03 -08003456 //Fall thru and complete IO
adam radford9c915a82010-12-21 13:34:31 -08003457 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
Shivasharan S8bf7c652017-02-10 00:59:03 -08003458 atomic_dec(&instance->fw_outstanding);
3459 if (cmd_fusion->r1_alt_dev_handle == MR_DEVHANDLE_INVALID) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003460 map_cmd_status(fusion, scmd_local, status,
Shivasharan S8bf7c652017-02-10 00:59:03 -08003461 extStatus, le32_to_cpu(data_length),
3462 sense);
3463 if (instance->ldio_threshold &&
3464 (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO))
Sasikumar Chandrasekaranb71b49c2017-01-10 18:20:51 -05003465 atomic_dec(&instance->ldio_outstanding);
Shivasharan S8bf7c652017-02-10 00:59:03 -08003466 scmd_local->SCp.ptr = NULL;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003467 megasas_return_cmd_fusion(instance, cmd_fusion);
3468 scsi_dma_unmap(scmd_local);
3469 scmd_local->scsi_done(scmd_local);
Shivasharan S8bf7c652017-02-10 00:59:03 -08003470 } else /* Optimal VD - R1 FP command completion. */
3471 megasas_complete_r1_command(instance, cmd_fusion);
adam radford9c915a82010-12-21 13:34:31 -08003472 break;
3473 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
3474 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303475 /* Poll mode. Dummy free.
3476 * In case of Interrupt mode, caller has reverse check.
3477 */
3478 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
3479 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
3480 megasas_return_cmd(instance, cmd_mfi);
3481 } else
3482 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
adam radford9c915a82010-12-21 13:34:31 -08003483 break;
3484 }
3485
adam radfordc8e858f2011-10-08 18:15:13 -07003486 fusion->last_reply_idx[MSIxIndex]++;
3487 if (fusion->last_reply_idx[MSIxIndex] >=
3488 fusion->reply_q_depth)
3489 fusion->last_reply_idx[MSIxIndex] = 0;
adam radford9c915a82010-12-21 13:34:31 -08003490
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05303491 desc->Words = cpu_to_le64(ULLONG_MAX);
adam radford9c915a82010-12-21 13:34:31 -08003492 num_completed++;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05303493 threshold_reply_count++;
adam radford9c915a82010-12-21 13:34:31 -08003494
3495 /* Get the next reply descriptor */
adam radfordc8e858f2011-10-08 18:15:13 -07003496 if (!fusion->last_reply_idx[MSIxIndex])
Sumit Saxena179ac142016-01-28 21:04:28 +05303497 desc = fusion->reply_frames_desc[MSIxIndex];
adam radford9c915a82010-12-21 13:34:31 -08003498 else
3499 desc++;
3500
3501 reply_desc =
3502 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3503
3504 d_val.word = desc->Words;
3505
3506 reply_descript_type = reply_desc->ReplyFlags &
3507 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3508
3509 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3510 break;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05303511 /*
3512 * Write to reply post host index register after completing threshold
3513 * number of reply counts and still there are more replies in reply queue
3514 * pending to be completed
3515 */
3516 if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05003517 if (instance->msix_combined)
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05303518 writel(((MSIxIndex & 0x7) << 24) |
3519 fusion->last_reply_idx[MSIxIndex],
3520 instance->reply_post_host_index_addr[MSIxIndex/8]);
3521 else
3522 writel((MSIxIndex << 24) |
3523 fusion->last_reply_idx[MSIxIndex],
3524 instance->reply_post_host_index_addr[0]);
3525 threshold_reply_count = 0;
3526 }
adam radford9c915a82010-12-21 13:34:31 -08003527 }
3528
3529 if (!num_completed)
3530 return IRQ_NONE;
3531
3532 wmb();
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05003533 if (instance->msix_combined)
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303534 writel(((MSIxIndex & 0x7) << 24) |
3535 fusion->last_reply_idx[MSIxIndex],
3536 instance->reply_post_host_index_addr[MSIxIndex/8]);
3537 else
3538 writel((MSIxIndex << 24) |
3539 fusion->last_reply_idx[MSIxIndex],
3540 instance->reply_post_host_index_addr[0]);
adam radford53ef2bb2011-02-24 20:56:05 -08003541 megasas_check_and_restore_queue_depth(instance);
adam radford9c915a82010-12-21 13:34:31 -08003542 return IRQ_HANDLED;
3543}
3544
3545/**
Shivasharan S29206da2017-02-10 00:59:34 -08003546 * megasas_sync_irqs - Synchronizes all IRQs owned by adapter
3547 * @instance: Adapter soft state
3548 */
3549void megasas_sync_irqs(unsigned long instance_addr)
3550{
3551 u32 count, i;
3552 struct megasas_instance *instance =
3553 (struct megasas_instance *)instance_addr;
3554
3555 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3556
3557 for (i = 0; i < count; i++)
3558 synchronize_irq(pci_irq_vector(instance->pdev, i));
3559}
3560
3561/**
adam radford9c915a82010-12-21 13:34:31 -08003562 * megasas_complete_cmd_dpc_fusion - Completes command
3563 * @instance: Adapter soft state
3564 *
3565 * Tasklet to complete cmds
3566 */
3567void
3568megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
3569{
3570 struct megasas_instance *instance =
3571 (struct megasas_instance *)instance_addr;
adam radfordc8e858f2011-10-08 18:15:13 -07003572 u32 count, MSIxIndex;
3573
3574 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
adam radford9c915a82010-12-21 13:34:31 -08003575
3576 /* If we have already declared adapter dead, donot complete cmds */
Shivasharan S34bd9f22018-10-16 23:37:50 -07003577 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
adam radford9c915a82010-12-21 13:34:31 -08003578 return;
adam radford9c915a82010-12-21 13:34:31 -08003579
adam radfordc8e858f2011-10-08 18:15:13 -07003580 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
3581 complete_cmd_fusion(instance, MSIxIndex);
adam radford9c915a82010-12-21 13:34:31 -08003582}
3583
3584/**
3585 * megasas_isr_fusion - isr entry point
3586 */
3587irqreturn_t megasas_isr_fusion(int irq, void *devp)
3588{
adam radfordc8e858f2011-10-08 18:15:13 -07003589 struct megasas_irq_context *irq_context = devp;
3590 struct megasas_instance *instance = irq_context->instance;
Shivasharan S3f6194a2018-10-16 23:37:39 -07003591 u32 mfiStatus;
adam radford9c915a82010-12-21 13:34:31 -08003592
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303593 if (instance->mask_interrupts)
3594 return IRQ_NONE;
3595
adam radfordc8e858f2011-10-08 18:15:13 -07003596 if (!instance->msix_vectors) {
adam radford9c915a82010-12-21 13:34:31 -08003597 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
3598 if (!mfiStatus)
3599 return IRQ_NONE;
3600 }
3601
3602 /* If we are resetting, bail */
adam radford6497b242011-10-08 18:14:50 -07003603 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
3604 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08003605 return IRQ_HANDLED;
adam radford6497b242011-10-08 18:14:50 -07003606 }
adam radford9c915a82010-12-21 13:34:31 -08003607
Shivasharan S3f6194a2018-10-16 23:37:39 -07003608 return complete_cmd_fusion(instance, irq_context->MSIxIndex);
adam radford9c915a82010-12-21 13:34:31 -08003609}
3610
3611/**
3612 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
3613 * @instance: Adapter soft state
3614 * mfi_cmd: megasas_cmd pointer
3615 *
3616 */
Shivasharan S7a7ae4f2017-02-10 00:59:32 -08003617void
adam radford9c915a82010-12-21 13:34:31 -08003618build_mpt_mfi_pass_thru(struct megasas_instance *instance,
3619 struct megasas_cmd *mfi_cmd)
3620{
3621 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
3622 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
3623 struct megasas_cmd_fusion *cmd;
3624 struct fusion_context *fusion;
3625 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
3626
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303627 fusion = instance->ctrl_context;
3628
3629 cmd = megasas_get_cmd_fusion(instance,
3630 instance->max_scsi_cmds + mfi_cmd->index);
adam radford9c915a82010-12-21 13:34:31 -08003631
3632 /* Save the smid. To be used for returning the cmd */
3633 mfi_cmd->context.smid = cmd->index;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303634
adam radford9c915a82010-12-21 13:34:31 -08003635 /*
3636 * For cmds where the flag is set, store the flag and check
3637 * on completion. For cmds with this flag, don't call
3638 * megasas_complete_cmd
3639 */
3640
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05303641 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303642 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
adam radford9c915a82010-12-21 13:34:31 -08003643
adam radford9c915a82010-12-21 13:34:31 -08003644 io_req = cmd->io_request;
adam radford36807e62011-10-08 18:15:06 -07003645
Shivasharan Sc3651782017-10-19 02:48:48 -07003646 if (instance->adapter_type >= INVADER_SERIES) {
adam radford36807e62011-10-08 18:15:06 -07003647 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
3648 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
3649 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
3650 sgl_ptr_end->Flags = 0;
3651 }
3652
adam radford9c915a82010-12-21 13:34:31 -08003653 mpi25_ieee_chain =
3654 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
3655
3656 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
3657 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
3658 SGL) / 4;
3659 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
3660
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303661 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08003662
3663 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
3664 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
3665
Shivasharan Sed2983f2017-08-23 04:46:55 -07003666 mpi25_ieee_chain->Length = cpu_to_le32(instance->mfi_frame_size);
adam radford9c915a82010-12-21 13:34:31 -08003667}
3668
3669/**
3670 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
3671 * @instance: Adapter soft state
3672 * @cmd: mfi cmd to build
3673 *
3674 */
3675union MEGASAS_REQUEST_DESCRIPTOR_UNION *
3676build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
3677{
Shivasharan S52205ac2017-02-10 00:59:08 -08003678 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc = NULL;
adam radford9c915a82010-12-21 13:34:31 -08003679 u16 index;
3680
Shivasharan S7a7ae4f2017-02-10 00:59:32 -08003681 build_mpt_mfi_pass_thru(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08003682 index = cmd->context.smid;
3683
3684 req_desc = megasas_get_request_descriptor(instance, index - 1);
3685
adam radford9c915a82010-12-21 13:34:31 -08003686 req_desc->Words = 0;
3687 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3688 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3689
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303690 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08003691
3692 return req_desc;
3693}
3694
3695/**
3696 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
3697 * @instance: Adapter soft state
3698 * @cmd: mfi cmd pointer
3699 *
3700 */
Shivasharan Sf4fc2092017-02-10 00:59:09 -08003701void
adam radford9c915a82010-12-21 13:34:31 -08003702megasas_issue_dcmd_fusion(struct megasas_instance *instance,
3703 struct megasas_cmd *cmd)
3704{
3705 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
adam radford9c915a82010-12-21 13:34:31 -08003706
3707 req_desc = build_mpt_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303708
Shivasharan S45b8a352017-02-10 00:59:04 -08003709 megasas_fire_cmd_fusion(instance, req_desc);
Shivasharan Sf4fc2092017-02-10 00:59:09 -08003710 return;
adam radford9c915a82010-12-21 13:34:31 -08003711}
3712
3713/**
3714 * megasas_release_fusion - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01003715 * @instance: Adapter soft state
adam radford9c915a82010-12-21 13:34:31 -08003716 */
3717void
3718megasas_release_fusion(struct megasas_instance *instance)
3719{
Shivasharan Sb9637d12017-10-19 02:49:01 -07003720 megasas_free_ioc_init_cmd(instance);
adam radford9c915a82010-12-21 13:34:31 -08003721 megasas_free_cmds(instance);
3722 megasas_free_cmds_fusion(instance);
3723
3724 iounmap(instance->reg_set);
3725
Yinghai Lue7f85162016-08-05 23:37:34 -07003726 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
adam radford9c915a82010-12-21 13:34:31 -08003727}
3728
3729/**
3730 * megasas_read_fw_status_reg_fusion - returns the current FW status value
3731 * @regs: MFI register set
3732 */
3733static u32
3734megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
3735{
Shivasharan S81b76452018-10-16 23:37:51 -07003736 return readl(&(regs)->outbound_scratch_pad_0);
adam radford9c915a82010-12-21 13:34:31 -08003737}
3738
3739/**
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303740 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3741 * @instance: Controller's soft instance
3742 * return: Number of allocated host crash buffers
3743 */
3744static void
3745megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
3746{
3747 unsigned int i;
3748
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303749 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
Shivasharan Sdef3e8d2017-08-23 04:47:03 -07003750 instance->crash_buf[i] = vzalloc(CRASH_DMA_BUF_SIZE);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303751 if (!instance->crash_buf[i]) {
3752 dev_info(&instance->pdev->dev, "Firmware crash dump "
3753 "memory allocation failed at index %d\n", i);
3754 break;
3755 }
3756 }
3757 instance->drv_buf_alloc = i;
3758}
3759
3760/**
3761 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3762 * @instance: Controller's soft instance
3763 */
3764void
3765megasas_free_host_crash_buffer(struct megasas_instance *instance)
3766{
Shivasharan Sdef3e8d2017-08-23 04:47:03 -07003767 unsigned int i;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303768 for (i = 0; i < instance->drv_buf_alloc; i++) {
3769 if (instance->crash_buf[i])
Shivasharan Sdef3e8d2017-08-23 04:47:03 -07003770 vfree(instance->crash_buf[i]);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303771 }
3772 instance->drv_buf_index = 0;
3773 instance->drv_buf_alloc = 0;
3774 instance->fw_crash_state = UNAVAILABLE;
3775 instance->fw_crash_buffer_size = 0;
3776}
3777
3778/**
adam radford9c915a82010-12-21 13:34:31 -08003779 * megasas_adp_reset_fusion - For controller reset
3780 * @regs: MFI register set
3781 */
3782static int
3783megasas_adp_reset_fusion(struct megasas_instance *instance,
3784 struct megasas_register_set __iomem *regs)
3785{
sumit.saxena@avagotech.com79b82c22015-08-31 17:23:41 +05303786 u32 host_diag, abs_state, retry;
3787
3788 /* Now try to reset the chip */
3789 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3790 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3791 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3792 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3793 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3794 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3795 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3796
3797 /* Check that the diag write enable (DRWE) bit is on */
3798 host_diag = readl(&instance->reg_set->fusion_host_diag);
3799 retry = 0;
3800 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
3801 msleep(100);
3802 host_diag = readl(&instance->reg_set->fusion_host_diag);
3803 if (retry++ == 100) {
3804 dev_warn(&instance->pdev->dev,
3805 "Host diag unlock failed from %s %d\n",
3806 __func__, __LINE__);
3807 break;
3808 }
3809 }
3810 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
3811 return -1;
3812
3813 /* Send chip reset command */
3814 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
3815 &instance->reg_set->fusion_host_diag);
3816 msleep(3000);
3817
3818 /* Make sure reset adapter bit is cleared */
3819 host_diag = readl(&instance->reg_set->fusion_host_diag);
3820 retry = 0;
3821 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
3822 msleep(100);
3823 host_diag = readl(&instance->reg_set->fusion_host_diag);
3824 if (retry++ == 1000) {
3825 dev_warn(&instance->pdev->dev,
3826 "Diag reset adapter never cleared %s %d\n",
3827 __func__, __LINE__);
3828 break;
3829 }
3830 }
3831 if (host_diag & HOST_DIAG_RESET_ADAPTER)
3832 return -1;
3833
3834 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set)
3835 & MFI_STATE_MASK;
3836 retry = 0;
3837
3838 while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
3839 msleep(100);
3840 abs_state = instance->instancet->
3841 read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
3842 }
3843 if (abs_state <= MFI_STATE_FW_INIT) {
3844 dev_warn(&instance->pdev->dev,
3845 "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
3846 abs_state, __func__, __LINE__);
3847 return -1;
3848 }
3849
adam radford9c915a82010-12-21 13:34:31 -08003850 return 0;
3851}
3852
3853/**
3854 * megasas_check_reset_fusion - For controller reset check
3855 * @regs: MFI register set
3856 */
3857static int
3858megasas_check_reset_fusion(struct megasas_instance *instance,
3859 struct megasas_register_set __iomem *regs)
3860{
3861 return 0;
3862}
3863
Shivasharan Sf0c21df2018-10-16 23:37:40 -07003864/**
3865 * megasas_trigger_snap_dump - Trigger snap dump in FW
3866 * @instance: Soft instance of adapter
3867 */
3868static inline void megasas_trigger_snap_dump(struct megasas_instance *instance)
3869{
3870 int j;
3871 u32 fw_state;
3872
3873 if (!instance->disableOnlineCtrlReset) {
3874 dev_info(&instance->pdev->dev, "Trigger snap dump\n");
3875 writel(MFI_ADP_TRIGGER_SNAP_DUMP,
3876 &instance->reg_set->doorbell);
3877 readl(&instance->reg_set->doorbell);
3878 }
3879
3880 for (j = 0; j < instance->snapdump_wait_time; j++) {
3881 fw_state = instance->instancet->read_fw_status_reg(
3882 instance->reg_set) & MFI_STATE_MASK;
3883 if (fw_state == MFI_STATE_FAULT) {
3884 dev_err(&instance->pdev->dev,
3885 "Found FW in FAULT state, after snap dump trigger\n");
3886 return;
3887 }
3888 msleep(1000);
3889 }
3890}
3891
adam radford9c915a82010-12-21 13:34:31 -08003892/* This function waits for outstanding commands on fusion to complete */
adam radford229fe472014-03-10 02:51:56 -07003893int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303894 int reason, int *convert)
adam radford9c915a82010-12-21 13:34:31 -08003895{
adam radford229fe472014-03-10 02:51:56 -07003896 int i, outstanding, retval = 0, hb_seconds_missed = 0;
adam radfordc007b8b2012-07-17 18:20:24 -07003897 u32 fw_state;
Shivasharan Sf0c21df2018-10-16 23:37:40 -07003898 u32 waittime_for_io_completion;
adam radford9c915a82010-12-21 13:34:31 -08003899
Shivasharan Sf0c21df2018-10-16 23:37:40 -07003900 waittime_for_io_completion =
3901 min_t(u32, resetwaittime,
3902 (resetwaittime - instance->snapdump_wait_time));
3903
3904 if (reason == MFI_IO_TIMEOUT_OCR) {
3905 dev_info(&instance->pdev->dev,
3906 "MFI command is timed out\n");
3907 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
3908 if (instance->snapdump_wait_time)
3909 megasas_trigger_snap_dump(instance);
3910 retval = 1;
3911 goto out;
3912 }
3913
3914 for (i = 0; i < waittime_for_io_completion; i++) {
adam radford9c915a82010-12-21 13:34:31 -08003915 /* Check if firmware is in fault state */
3916 fw_state = instance->instancet->read_fw_status_reg(
3917 instance->reg_set) & MFI_STATE_MASK;
3918 if (fw_state == MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003919 dev_warn(&instance->pdev->dev, "Found FW in FAULT state,"
adam radford229fe472014-03-10 02:51:56 -07003920 " will reset adapter scsi%d.\n",
3921 instance->host->host_no);
Sumit Saxena64d0b8e2016-04-15 00:23:31 -07003922 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
Shivasharan Sdef0eab2017-02-10 00:59:15 -08003923 if (instance->requestorId && reason) {
3924 dev_warn(&instance->pdev->dev, "SR-IOV Found FW in FAULT"
3925 " state while polling during"
3926 " I/O timeout handling for %d\n",
3927 instance->host->host_no);
3928 *convert = 1;
3929 }
3930
adam radford9c915a82010-12-21 13:34:31 -08003931 retval = 1;
3932 goto out;
3933 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303934
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303935
adam radford229fe472014-03-10 02:51:56 -07003936 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303937 if (instance->requestorId && !reason) {
adam radford229fe472014-03-10 02:51:56 -07003938 retval = 1;
3939 goto out;
3940 }
3941
3942 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
Shivasharan Sdef0eab2017-02-10 00:59:15 -08003943 if (instance->requestorId && (reason == SCSIIO_TIMEOUT_OCR)) {
adam radford229fe472014-03-10 02:51:56 -07003944 if (instance->hb_host_mem->HB.fwCounter !=
3945 instance->hb_host_mem->HB.driverCounter) {
3946 instance->hb_host_mem->HB.driverCounter =
3947 instance->hb_host_mem->HB.fwCounter;
3948 hb_seconds_missed = 0;
3949 } else {
3950 hb_seconds_missed++;
3951 if (hb_seconds_missed ==
3952 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003953 dev_warn(&instance->pdev->dev, "SR-IOV:"
adam radford229fe472014-03-10 02:51:56 -07003954 " Heartbeat never completed "
3955 " while polling during I/O "
3956 " timeout handling for "
3957 "scsi%d.\n",
3958 instance->host->host_no);
3959 *convert = 1;
3960 retval = 1;
3961 goto out;
3962 }
3963 }
3964 }
adam radford9c915a82010-12-21 13:34:31 -08003965
Shivasharan S14298cf2017-08-23 04:46:58 -07003966 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
adam radford9c915a82010-12-21 13:34:31 -08003967 outstanding = atomic_read(&instance->fw_outstanding);
3968 if (!outstanding)
3969 goto out;
3970
3971 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003972 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
adam radford229fe472014-03-10 02:51:56 -07003973 "commands to complete for scsi%d\n", i,
3974 outstanding, instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08003975 }
3976 msleep(1000);
3977 }
3978
Shivasharan Sf0c21df2018-10-16 23:37:40 -07003979 if (instance->snapdump_wait_time) {
3980 megasas_trigger_snap_dump(instance);
3981 retval = 1;
3982 goto out;
3983 }
3984
adam radford9c915a82010-12-21 13:34:31 -08003985 if (atomic_read(&instance->fw_outstanding)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003986 dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
adam radford229fe472014-03-10 02:51:56 -07003987 "will reset adapter scsi%d.\n",
3988 instance->host->host_no);
Kashyap Desai18e1c7f2016-10-21 06:33:29 -07003989 *convert = 1;
adam radford9c915a82010-12-21 13:34:31 -08003990 retval = 1;
3991 }
Shivasharan Sf0c21df2018-10-16 23:37:40 -07003992
adam radford9c915a82010-12-21 13:34:31 -08003993out:
3994 return retval;
3995}
3996
3997void megasas_reset_reply_desc(struct megasas_instance *instance)
3998{
Sumit Saxena179ac142016-01-28 21:04:28 +05303999 int i, j, count;
adam radford9c915a82010-12-21 13:34:31 -08004000 struct fusion_context *fusion;
4001 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
4002
4003 fusion = instance->ctrl_context;
adam radfordc8e858f2011-10-08 18:15:13 -07004004 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
Sumit Saxena179ac142016-01-28 21:04:28 +05304005 for (i = 0 ; i < count ; i++) {
adam radfordc8e858f2011-10-08 18:15:13 -07004006 fusion->last_reply_idx[i] = 0;
Sumit Saxena179ac142016-01-28 21:04:28 +05304007 reply_desc = fusion->reply_frames_desc[i];
4008 for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++)
4009 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
4010 }
adam radford9c915a82010-12-21 13:34:31 -08004011}
4012
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05304013/*
4014 * megasas_refire_mgmt_cmd : Re-fire management commands
4015 * @instance: Controller's soft instance
4016*/
4017void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
4018{
4019 int j;
4020 struct megasas_cmd_fusion *cmd_fusion;
4021 struct fusion_context *fusion;
4022 struct megasas_cmd *cmd_mfi;
4023 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
4024 u16 smid;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05304025 bool refire_cmd = 0;
Shivasharan S54b28042018-01-05 05:27:47 -08004026 u8 result;
4027 u32 opcode = 0;
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05304028
4029 fusion = instance->ctrl_context;
4030
4031 /* Re-fire management commands.
4032 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
4033 */
4034 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
4035 cmd_fusion = fusion->cmd_list[j];
4036 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
4037 smid = le16_to_cpu(cmd_mfi->context.smid);
Shivasharan S54b28042018-01-05 05:27:47 -08004038 result = REFIRE_CMD;
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05304039
4040 if (!smid)
4041 continue;
Shivasharan S690e9c32017-08-23 04:46:59 -07004042
Shivasharan S54b28042018-01-05 05:27:47 -08004043 req_desc = megasas_get_request_descriptor(instance, smid - 1);
4044
4045 switch (cmd_mfi->frame->hdr.cmd) {
4046 case MFI_CMD_DCMD:
4047 opcode = le32_to_cpu(cmd_mfi->frame->dcmd.opcode);
4048 /* Do not refire shutdown command */
4049 if (opcode == MR_DCMD_CTRL_SHUTDOWN) {
4050 cmd_mfi->frame->dcmd.cmd_status = MFI_STAT_OK;
4051 result = COMPLETE_CMD;
4052 break;
4053 }
4054
4055 refire_cmd = ((opcode != MR_DCMD_LD_MAP_GET_INFO)) &&
4056 (opcode != MR_DCMD_SYSTEM_PD_MAP_GET_INFO) &&
4057 !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE);
4058
4059 if (!refire_cmd)
4060 result = RETURN_CMD;
4061
4062 break;
Shivasharan Sf870bcb2018-01-05 05:33:04 -08004063 case MFI_CMD_NVME:
4064 if (!instance->support_nvme_passthru) {
4065 cmd_mfi->frame->hdr.cmd_status = MFI_STAT_INVALID_CMD;
4066 result = COMPLETE_CMD;
4067 }
Shivasharan S54b28042018-01-05 05:27:47 -08004068
Shivasharan Sf870bcb2018-01-05 05:33:04 -08004069 break;
Shivasharan S54b28042018-01-05 05:27:47 -08004070 default:
4071 break;
Shivasharan S690e9c32017-08-23 04:46:59 -07004072 }
4073
Shivasharan S54b28042018-01-05 05:27:47 -08004074 switch (result) {
4075 case REFIRE_CMD:
Shivasharan S45b8a352017-02-10 00:59:04 -08004076 megasas_fire_cmd_fusion(instance, req_desc);
Shivasharan S54b28042018-01-05 05:27:47 -08004077 break;
4078 case RETURN_CMD:
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05304079 megasas_return_cmd(instance, cmd_mfi);
Shivasharan S54b28042018-01-05 05:27:47 -08004080 break;
4081 case COMPLETE_CMD:
4082 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
4083 break;
4084 }
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05304085 }
adam radford9c915a82010-12-21 13:34:31 -08004086}
4087
Sumit Saxena18365b12016-01-28 21:04:25 +05304088/*
4089 * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
4090 * @instance: per adapter struct
4091 * @channel: the channel assigned by the OS
4092 * @id: the id assigned by the OS
4093 *
4094 * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
4095 */
4096
4097static int megasas_track_scsiio(struct megasas_instance *instance,
4098 int id, int channel)
4099{
4100 int i, found = 0;
4101 struct megasas_cmd_fusion *cmd_fusion;
4102 struct fusion_context *fusion;
4103 fusion = instance->ctrl_context;
4104
4105 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
4106 cmd_fusion = fusion->cmd_list[i];
4107 if (cmd_fusion->scmd &&
4108 (cmd_fusion->scmd->device->id == id &&
4109 cmd_fusion->scmd->device->channel == channel)) {
4110 dev_info(&instance->pdev->dev,
4111 "SCSI commands pending to target"
4112 "channel %d id %d \tSMID: 0x%x\n",
4113 channel, id, cmd_fusion->index);
4114 scsi_print_command(cmd_fusion->scmd);
4115 found = 1;
4116 break;
4117 }
4118 }
4119
4120 return found ? FAILED : SUCCESS;
4121}
4122
4123/**
4124 * megasas_tm_response_code - translation of device response code
4125 * @ioc: per adapter object
4126 * @mpi_reply: MPI reply returned by firmware
4127 *
4128 * Return nothing.
4129 */
4130static void
4131megasas_tm_response_code(struct megasas_instance *instance,
4132 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply)
4133{
4134 char *desc;
4135
4136 switch (mpi_reply->ResponseCode) {
4137 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
4138 desc = "task management request completed";
4139 break;
4140 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
4141 desc = "invalid frame";
4142 break;
4143 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
4144 desc = "task management request not supported";
4145 break;
4146 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
4147 desc = "task management request failed";
4148 break;
4149 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
4150 desc = "task management request succeeded";
4151 break;
4152 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
4153 desc = "invalid lun";
4154 break;
4155 case 0xA:
4156 desc = "overlapped tag attempted";
4157 break;
4158 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
4159 desc = "task queued, however not sent to target";
4160 break;
4161 default:
4162 desc = "unknown";
4163 break;
4164 }
4165 dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n",
4166 mpi_reply->ResponseCode, desc);
4167 dev_dbg(&instance->pdev->dev,
4168 "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
4169 " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
4170 mpi_reply->TerminationCount, mpi_reply->DevHandle,
4171 mpi_reply->Function, mpi_reply->TaskType,
4172 mpi_reply->IOCStatus, mpi_reply->IOCLogInfo);
4173}
4174
4175/**
4176 * megasas_issue_tm - main routine for sending tm requests
4177 * @instance: per adapter struct
4178 * @device_handle: device handle
4179 * @channel: the channel assigned by the OS
4180 * @id: the id assigned by the OS
4181 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
4182 * @smid_task: smid assigned to the task
4183 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
4184 * Context: user
4185 *
4186 * MegaRaid use MPT interface for Task Magement request.
4187 * A generic API for sending task management requests to firmware.
4188 *
4189 * Return SUCCESS or FAILED.
4190 */
4191static int
4192megasas_issue_tm(struct megasas_instance *instance, u16 device_handle,
Shivasharan Se9495e22018-06-04 03:45:12 -07004193 uint channel, uint id, u16 smid_task, u8 type,
4194 struct MR_PRIV_DEVICE *mr_device_priv_data)
Sumit Saxena18365b12016-01-28 21:04:25 +05304195{
4196 struct MR_TASK_MANAGE_REQUEST *mr_request;
4197 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request;
4198 unsigned long timeleft;
4199 struct megasas_cmd_fusion *cmd_fusion;
4200 struct megasas_cmd *cmd_mfi;
4201 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
Shivasharan S28661c82017-08-23 04:47:02 -07004202 struct fusion_context *fusion = NULL;
Sumit Saxena18365b12016-01-28 21:04:25 +05304203 struct megasas_cmd_fusion *scsi_lookup;
4204 int rc;
Shivasharan Se9495e22018-06-04 03:45:12 -07004205 int timeout = MEGASAS_DEFAULT_TM_TIMEOUT;
Sumit Saxena18365b12016-01-28 21:04:25 +05304206 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply;
4207
4208 fusion = instance->ctrl_context;
4209
4210 cmd_mfi = megasas_get_cmd(instance);
4211
4212 if (!cmd_mfi) {
4213 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
4214 __func__, __LINE__);
4215 return -ENOMEM;
4216 }
4217
4218 cmd_fusion = megasas_get_cmd_fusion(instance,
4219 instance->max_scsi_cmds + cmd_mfi->index);
4220
4221 /* Save the smid. To be used for returning the cmd */
4222 cmd_mfi->context.smid = cmd_fusion->index;
4223
4224 req_desc = megasas_get_request_descriptor(instance,
4225 (cmd_fusion->index - 1));
Sumit Saxena18365b12016-01-28 21:04:25 +05304226
4227 cmd_fusion->request_desc = req_desc;
4228 req_desc->Words = 0;
4229
Sumit Saxena18365b12016-01-28 21:04:25 +05304230 mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request;
4231 memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST));
4232 mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest;
4233 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
4234 mpi_request->DevHandle = cpu_to_le16(device_handle);
4235 mpi_request->TaskType = type;
4236 mpi_request->TaskMID = cpu_to_le16(smid_task);
4237 mpi_request->LUN[1] = 0;
4238
4239
4240 req_desc = cmd_fusion->request_desc;
4241 req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index);
4242 req_desc->HighPriority.RequestFlags =
4243 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
4244 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
4245 req_desc->HighPriority.MSIxIndex = 0;
4246 req_desc->HighPriority.LMID = 0;
4247 req_desc->HighPriority.Reserved1 = 0;
4248
4249 if (channel < MEGASAS_MAX_PD_CHANNELS)
4250 mr_request->tmReqFlags.isTMForPD = 1;
4251 else
4252 mr_request->tmReqFlags.isTMForLD = 1;
4253
4254 init_completion(&cmd_fusion->done);
Shivasharan S45b8a352017-02-10 00:59:04 -08004255 megasas_fire_cmd_fusion(instance, req_desc);
Sumit Saxena18365b12016-01-28 21:04:25 +05304256
Shivasharan Se9495e22018-06-04 03:45:12 -07004257 switch (type) {
4258 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
4259 timeout = mr_device_priv_data->task_abort_tmo;
4260 break;
4261 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4262 timeout = mr_device_priv_data->target_reset_tmo;
4263 break;
4264 }
4265
4266 timeleft = wait_for_completion_timeout(&cmd_fusion->done, timeout * HZ);
Sumit Saxena18365b12016-01-28 21:04:25 +05304267
4268 if (!timeleft) {
4269 dev_err(&instance->pdev->dev,
4270 "task mgmt type 0x%x timed out\n", type);
4271 cmd_mfi->flags |= DRV_DCMD_SKIP_REFIRE;
4272 mutex_unlock(&instance->reset_mutex);
4273 rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR);
4274 mutex_lock(&instance->reset_mutex);
4275 return rc;
4276 }
4277
4278 mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply;
4279 megasas_tm_response_code(instance, mpi_reply);
4280
4281 megasas_return_cmd(instance, cmd_mfi);
4282 rc = SUCCESS;
4283 switch (type) {
4284 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
Shivasharan S28661c82017-08-23 04:47:02 -07004285 scsi_lookup = fusion->cmd_list[smid_task - 1];
4286
Sumit Saxena18365b12016-01-28 21:04:25 +05304287 if (scsi_lookup->scmd == NULL)
4288 break;
4289 else {
4290 instance->instancet->disable_intr(instance);
Shivasharan S29206da2017-02-10 00:59:34 -08004291 megasas_sync_irqs((unsigned long)instance);
Sumit Saxena18365b12016-01-28 21:04:25 +05304292 instance->instancet->enable_intr(instance);
4293 if (scsi_lookup->scmd == NULL)
4294 break;
4295 }
4296 rc = FAILED;
4297 break;
4298
4299 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
4300 if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF))
4301 break;
4302 instance->instancet->disable_intr(instance);
Shivasharan Sbdb5c552017-08-23 04:46:57 -07004303 megasas_sync_irqs((unsigned long)instance);
Sumit Saxena18365b12016-01-28 21:04:25 +05304304 rc = megasas_track_scsiio(instance, id, channel);
4305 instance->instancet->enable_intr(instance);
4306
4307 break;
4308 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
4309 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
4310 break;
4311 default:
4312 rc = FAILED;
4313 break;
4314 }
4315
4316 return rc;
4317
4318}
4319
4320/*
4321 * megasas_fusion_smid_lookup : Look for fusion command correpspodning to SCSI
4322 * @instance: per adapter struct
4323 *
4324 * Return Non Zero index, if SMID found in outstanding commands
4325 */
4326static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd)
4327{
4328 int i, ret = 0;
4329 struct megasas_instance *instance;
4330 struct megasas_cmd_fusion *cmd_fusion;
4331 struct fusion_context *fusion;
4332
4333 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4334
4335 fusion = instance->ctrl_context;
4336
4337 for (i = 0; i < instance->max_scsi_cmds; i++) {
4338 cmd_fusion = fusion->cmd_list[i];
4339 if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) {
4340 scmd_printk(KERN_NOTICE, scmd, "Abort request is for"
4341 " SMID: %d\n", cmd_fusion->index);
4342 ret = cmd_fusion->index;
4343 break;
4344 }
4345 }
4346
4347 return ret;
4348}
4349
4350/*
4351* megasas_get_tm_devhandle - Get devhandle for TM request
4352* @sdev- OS provided scsi device
4353*
4354* Returns- devhandle/targetID of SCSI device
4355*/
4356static u16 megasas_get_tm_devhandle(struct scsi_device *sdev)
4357{
4358 u16 pd_index = 0;
4359 u32 device_id;
4360 struct megasas_instance *instance;
4361 struct fusion_context *fusion;
4362 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
4363 u16 devhandle = (u16)ULONG_MAX;
4364
4365 instance = (struct megasas_instance *)sdev->host->hostdata;
4366 fusion = instance->ctrl_context;
4367
Shivasharan S3cabd162017-02-10 00:59:05 -08004368 if (!MEGASAS_IS_LOGICAL(sdev)) {
Sumit Saxena18365b12016-01-28 21:04:25 +05304369 if (instance->use_seqnum_jbod_fp) {
Shivasharan S3cabd162017-02-10 00:59:05 -08004370 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
4371 + sdev->id;
4372 pd_sync = (void *)fusion->pd_seq_sync
4373 [(instance->pd_seq_map_id - 1) & 1];
4374 devhandle = pd_sync->seq[pd_index].devHandle;
Sumit Saxena18365b12016-01-28 21:04:25 +05304375 } else
4376 sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable"
4377 " without JBOD MAP support from %s %d\n", __func__, __LINE__);
4378 } else {
4379 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
4380 + sdev->id;
4381 devhandle = device_id;
4382 }
4383
4384 return devhandle;
4385}
4386
4387/*
4388 * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
4389 * @scmd : pointer to scsi command object
4390 *
4391 * Return SUCCESS, if command aborted else FAILED
4392 */
4393
4394int megasas_task_abort_fusion(struct scsi_cmnd *scmd)
4395{
4396 struct megasas_instance *instance;
4397 u16 smid, devhandle;
4398 struct fusion_context *fusion;
4399 int ret;
4400 struct MR_PRIV_DEVICE *mr_device_priv_data;
4401 mr_device_priv_data = scmd->device->hostdata;
4402
4403
4404 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4405 fusion = instance->ctrl_context;
4406
Shivasharan Sdef0eab2017-02-10 00:59:15 -08004407 scmd_printk(KERN_INFO, scmd, "task abort called for scmd(%p)\n", scmd);
4408 scsi_print_command(scmd);
4409
Sumit Saxena8a01a412016-01-28 21:04:32 +05304410 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Sumit Saxena18365b12016-01-28 21:04:25 +05304411 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4412 "SCSI host:%d\n", instance->host->host_no);
4413 ret = FAILED;
4414 return ret;
4415 }
4416
4417 if (!mr_device_priv_data) {
4418 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
4419 "scmd(%p)\n", scmd);
4420 scmd->result = DID_NO_CONNECT << 16;
4421 ret = SUCCESS;
4422 goto out;
4423 }
4424
4425
4426 if (!mr_device_priv_data->is_tm_capable) {
4427 ret = FAILED;
4428 goto out;
4429 }
4430
4431 mutex_lock(&instance->reset_mutex);
4432
4433 smid = megasas_fusion_smid_lookup(scmd);
4434
4435 if (!smid) {
4436 ret = SUCCESS;
4437 scmd_printk(KERN_NOTICE, scmd, "Command for which abort is"
4438 " issued is not found in oustanding commands\n");
4439 mutex_unlock(&instance->reset_mutex);
4440 goto out;
4441 }
4442
4443 devhandle = megasas_get_tm_devhandle(scmd->device);
4444
4445 if (devhandle == (u16)ULONG_MAX) {
4446 ret = SUCCESS;
4447 sdev_printk(KERN_INFO, scmd->device,
4448 "task abort issued for invalid devhandle\n");
4449 mutex_unlock(&instance->reset_mutex);
4450 goto out;
4451 }
4452 sdev_printk(KERN_INFO, scmd->device,
4453 "attempting task abort! scmd(%p) tm_dev_handle 0x%x\n",
4454 scmd, devhandle);
4455
4456 mr_device_priv_data->tm_busy = 1;
4457 ret = megasas_issue_tm(instance, devhandle,
4458 scmd->device->channel, scmd->device->id, smid,
Shivasharan Se9495e22018-06-04 03:45:12 -07004459 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
4460 mr_device_priv_data);
Sumit Saxena18365b12016-01-28 21:04:25 +05304461 mr_device_priv_data->tm_busy = 0;
4462
4463 mutex_unlock(&instance->reset_mutex);
4464out:
4465 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
4466 ((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4467
4468 return ret;
4469}
4470
4471/*
4472 * megasas_reset_target_fusion : target reset function for fusion adapters
4473 * scmd: SCSI command pointer
4474 *
4475 * Returns SUCCESS if all commands associated with target aborted else FAILED
4476 */
4477
4478int megasas_reset_target_fusion(struct scsi_cmnd *scmd)
4479{
4480
4481 struct megasas_instance *instance;
4482 int ret = FAILED;
4483 u16 devhandle;
4484 struct fusion_context *fusion;
4485 struct MR_PRIV_DEVICE *mr_device_priv_data;
4486 mr_device_priv_data = scmd->device->hostdata;
4487
4488 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4489 fusion = instance->ctrl_context;
4490
Shivasharan Sdef0eab2017-02-10 00:59:15 -08004491 sdev_printk(KERN_INFO, scmd->device,
4492 "target reset called for scmd(%p)\n", scmd);
4493
Sumit Saxena8a01a412016-01-28 21:04:32 +05304494 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Sumit Saxena18365b12016-01-28 21:04:25 +05304495 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4496 "SCSI host:%d\n", instance->host->host_no);
4497 ret = FAILED;
4498 return ret;
4499 }
4500
4501 if (!mr_device_priv_data) {
4502 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
4503 "scmd(%p)\n", scmd);
4504 scmd->result = DID_NO_CONNECT << 16;
4505 ret = SUCCESS;
4506 goto out;
4507 }
4508
4509
4510 if (!mr_device_priv_data->is_tm_capable) {
4511 ret = FAILED;
4512 goto out;
4513 }
4514
4515 mutex_lock(&instance->reset_mutex);
4516 devhandle = megasas_get_tm_devhandle(scmd->device);
4517
4518 if (devhandle == (u16)ULONG_MAX) {
4519 ret = SUCCESS;
4520 sdev_printk(KERN_INFO, scmd->device,
4521 "target reset issued for invalid devhandle\n");
4522 mutex_unlock(&instance->reset_mutex);
4523 goto out;
4524 }
4525
4526 sdev_printk(KERN_INFO, scmd->device,
4527 "attempting target reset! scmd(%p) tm_dev_handle 0x%x\n",
4528 scmd, devhandle);
4529 mr_device_priv_data->tm_busy = 1;
4530 ret = megasas_issue_tm(instance, devhandle,
4531 scmd->device->channel, scmd->device->id, 0,
Shivasharan Se9495e22018-06-04 03:45:12 -07004532 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET,
4533 mr_device_priv_data);
Sumit Saxena18365b12016-01-28 21:04:25 +05304534 mr_device_priv_data->tm_busy = 0;
4535 mutex_unlock(&instance->reset_mutex);
4536out:
4537 scmd_printk(KERN_NOTICE, scmd, "megasas: target reset %s!!\n",
4538 (ret == SUCCESS) ? "SUCCESS" : "FAILED");
4539
4540 return ret;
4541}
4542
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05304543/*SRIOV get other instance in cluster if any*/
4544struct megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance)
4545{
4546 int i;
4547
4548 for (i = 0; i < MAX_MGMT_ADAPTERS; i++) {
4549 if (megasas_mgmt_info.instance[i] &&
4550 (megasas_mgmt_info.instance[i] != instance) &&
4551 megasas_mgmt_info.instance[i]->requestorId &&
4552 megasas_mgmt_info.instance[i]->peerIsPresent &&
4553 (memcmp((megasas_mgmt_info.instance[i]->clusterId),
4554 instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0))
4555 return megasas_mgmt_info.instance[i];
4556 }
4557 return NULL;
4558}
4559
adam radford229fe472014-03-10 02:51:56 -07004560/* Check for a second path that is currently UP */
4561int megasas_check_mpio_paths(struct megasas_instance *instance,
4562 struct scsi_cmnd *scmd)
adam radford9c915a82010-12-21 13:34:31 -08004563{
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05304564 struct megasas_instance *peer_instance = NULL;
Shivasharan Sf55cf472017-02-10 00:59:07 -08004565 int retval = (DID_REQUEUE << 16);
adam radford229fe472014-03-10 02:51:56 -07004566
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05304567 if (instance->peerIsPresent) {
4568 peer_instance = megasas_get_peer_instance(instance);
4569 if ((peer_instance) &&
4570 (atomic_read(&peer_instance->adprecovery) ==
4571 MEGASAS_HBA_OPERATIONAL))
4572 retval = (DID_NO_CONNECT << 16);
adam radford229fe472014-03-10 02:51:56 -07004573 }
adam radford229fe472014-03-10 02:51:56 -07004574 return retval;
4575}
4576
4577/* Core fusion reset function */
Sumit Saxena18365b12016-01-28 21:04:25 +05304578int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
adam radford229fe472014-03-10 02:51:56 -07004579{
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05004580 int retval = SUCCESS, i, j, convert = 0;
adam radford9c915a82010-12-21 13:34:31 -08004581 struct megasas_instance *instance;
Shivasharan S8bf7c652017-02-10 00:59:03 -08004582 struct megasas_cmd_fusion *cmd_fusion, *r1_cmd;
adam radford9c915a82010-12-21 13:34:31 -08004583 struct fusion_context *fusion;
sumit.saxena@avagotech.com79b82c22015-08-31 17:23:41 +05304584 u32 abs_state, status_reg, reset_adapter;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304585 u32 io_timeout_in_crash_mode = 0;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304586 struct scsi_cmnd *scmd_local = NULL;
Sumit Saxena18365b12016-01-28 21:04:25 +05304587 struct scsi_device *sdev;
Shivasharan Se9495e22018-06-04 03:45:12 -07004588 int ret_target_prop = DCMD_FAILED;
4589 bool is_target_prop = false;
adam radford9c915a82010-12-21 13:34:31 -08004590
4591 instance = (struct megasas_instance *)shost->hostdata;
4592 fusion = instance->ctrl_context;
4593
adam radford229fe472014-03-10 02:51:56 -07004594 mutex_lock(&instance->reset_mutex);
4595
Sumit Saxena8a01a412016-01-28 21:04:32 +05304596 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004597 dev_warn(&instance->pdev->dev, "Hardware critical error, "
adam radford229fe472014-03-10 02:51:56 -07004598 "returning FAILED for scsi%d.\n",
4599 instance->host->host_no);
Adam Radforda2fbcbc2014-07-09 15:17:54 -07004600 mutex_unlock(&instance->reset_mutex);
adam radfordd4a759a2011-10-08 18:14:39 -07004601 return FAILED;
adam radford9c915a82010-12-21 13:34:31 -08004602 }
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304603 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
4604 abs_state = status_reg & MFI_STATE_MASK;
4605
4606 /* IO timeout detected, forcibly put FW in FAULT state */
4607 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
Sumit Saxena18365b12016-01-28 21:04:25 +05304608 instance->crash_dump_app_support && reason) {
4609 dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, "
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304610 "forcibly FAULT Firmware\n");
Sumit Saxena8a01a412016-01-28 21:04:32 +05304611 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304612 status_reg = readl(&instance->reg_set->doorbell);
4613 writel(status_reg | MFI_STATE_FORCE_OCR,
4614 &instance->reg_set->doorbell);
4615 readl(&instance->reg_set->doorbell);
4616 mutex_unlock(&instance->reset_mutex);
4617 do {
4618 ssleep(3);
4619 io_timeout_in_crash_mode++;
4620 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
4621 "seconds for crash dump collection and OCR "
4622 "to be done\n", (io_timeout_in_crash_mode * 3));
Sumit Saxena8a01a412016-01-28 21:04:32 +05304623 } while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304624 (io_timeout_in_crash_mode < 80));
4625
Sumit Saxena8a01a412016-01-28 21:04:32 +05304626 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304627 dev_info(&instance->pdev->dev, "OCR done for IO "
4628 "timeout case\n");
4629 retval = SUCCESS;
4630 } else {
4631 dev_info(&instance->pdev->dev, "Controller is not "
4632 "operational after 240 seconds wait for IO "
4633 "timeout case in FW crash dump mode\n do "
4634 "OCR/kill adapter\n");
4635 retval = megasas_reset_fusion(shost, 0);
4636 }
4637 return retval;
4638 }
adam radford9c915a82010-12-21 13:34:31 -08004639
adam radford229fe472014-03-10 02:51:56 -07004640 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
4641 del_timer_sync(&instance->sriov_heartbeat_timer);
adam radford7e70e732011-05-11 18:34:08 -07004642 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05304643 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304644 instance->instancet->disable_intr(instance);
Shivasharan S29206da2017-02-10 00:59:34 -08004645 megasas_sync_irqs((unsigned long)instance);
adam radford7e70e732011-05-11 18:34:08 -07004646
adam radford9c915a82010-12-21 13:34:31 -08004647 /* First try waiting for commands to complete */
Sumit Saxena18365b12016-01-28 21:04:25 +05304648 if (megasas_wait_for_outstanding_fusion(instance, reason,
adam radford229fe472014-03-10 02:51:56 -07004649 &convert)) {
Sumit Saxena8a01a412016-01-28 21:04:32 +05304650 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004651 dev_warn(&instance->pdev->dev, "resetting fusion "
adam radford229fe472014-03-10 02:51:56 -07004652 "adapter scsi%d.\n", instance->host->host_no);
4653 if (convert)
Sumit Saxena18365b12016-01-28 21:04:25 +05304654 reason = 0;
adam radford229fe472014-03-10 02:51:56 -07004655
Shivasharan Sdef0eab2017-02-10 00:59:15 -08004656 if (megasas_dbg_lvl & OCR_LOGS)
4657 dev_info(&instance->pdev->dev, "\nPending SCSI commands:\n");
4658
adam radford9c915a82010-12-21 13:34:31 -08004659 /* Now return commands back to the OS */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304660 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
adam radford9c915a82010-12-21 13:34:31 -08004661 cmd_fusion = fusion->cmd_list[i];
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05004662 /*check for extra commands issued by driver*/
Shivasharan Sf369a312017-10-19 02:48:52 -07004663 if (instance->adapter_type == VENTURA_SERIES) {
Shivasharan S8bf7c652017-02-10 00:59:03 -08004664 r1_cmd = fusion->cmd_list[i + instance->max_fw_cmds];
4665 megasas_return_cmd_fusion(instance, r1_cmd);
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05004666 }
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304667 scmd_local = cmd_fusion->scmd;
adam radford9c915a82010-12-21 13:34:31 -08004668 if (cmd_fusion->scmd) {
Shivasharan Sdef0eab2017-02-10 00:59:15 -08004669 if (megasas_dbg_lvl & OCR_LOGS) {
4670 sdev_printk(KERN_INFO,
4671 cmd_fusion->scmd->device, "SMID: 0x%x\n",
4672 cmd_fusion->index);
4673 scsi_print_command(cmd_fusion->scmd);
4674 }
4675
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304676 scmd_local->result =
adam radford229fe472014-03-10 02:51:56 -07004677 megasas_check_mpio_paths(instance,
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304678 scmd_local);
Sasikumar Chandrasekaranb71b49c2017-01-10 18:20:51 -05004679 if (instance->ldio_threshold &&
4680 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
Sumit Saxena308ec452016-01-28 21:04:30 +05304681 atomic_dec(&instance->ldio_outstanding);
adam radford9c915a82010-12-21 13:34:31 -08004682 megasas_return_cmd_fusion(instance, cmd_fusion);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304683 scsi_dma_unmap(scmd_local);
4684 scmd_local->scsi_done(scmd_local);
adam radford9c915a82010-12-21 13:34:31 -08004685 }
4686 }
4687
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05004688 atomic_set(&instance->fw_outstanding, 0);
4689
adam radford7e70e732011-05-11 18:34:08 -07004690 status_reg = instance->instancet->read_fw_status_reg(
4691 instance->reg_set);
4692 abs_state = status_reg & MFI_STATE_MASK;
4693 reset_adapter = status_reg & MFI_RESET_ADAPTER;
4694 if (instance->disableOnlineCtrlReset ||
4695 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
adam radford9c915a82010-12-21 13:34:31 -08004696 /* Reset not supported, kill adapter */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004697 dev_warn(&instance->pdev->dev, "Reset not supported"
adam radford229fe472014-03-10 02:51:56 -07004698 ", killing adapter scsi%d.\n",
4699 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08004700 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07004701 instance->skip_heartbeat_timer_del = 1;
adam radford9c915a82010-12-21 13:34:31 -08004702 retval = FAILED;
4703 goto out;
4704 }
4705
adam radford229fe472014-03-10 02:51:56 -07004706 /* Let SR-IOV VF & PF sync up if there was a HB failure */
Sumit Saxena18365b12016-01-28 21:04:25 +05304707 if (instance->requestorId && !reason) {
adam radford229fe472014-03-10 02:51:56 -07004708 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
Sumit Saxena3885c262016-01-28 21:04:35 +05304709 goto transition_to_ready;
adam radford229fe472014-03-10 02:51:56 -07004710 }
4711
adam radford9c915a82010-12-21 13:34:31 -08004712 /* Now try to reset the chip */
4713 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
adam radford9c915a82010-12-21 13:34:31 -08004714
sumit.saxena@avagotech.com79b82c22015-08-31 17:23:41 +05304715 if (instance->instancet->adp_reset
4716 (instance, instance->reg_set))
adam radford9c915a82010-12-21 13:34:31 -08004717 continue;
Sumit Saxena3885c262016-01-28 21:04:35 +05304718transition_to_ready:
adam radford9c915a82010-12-21 13:34:31 -08004719 /* Wait for FW to become ready */
adam radford058a8fa2011-10-08 18:14:27 -07004720 if (megasas_transition_to_ready(instance, 1)) {
Sumit Saxena3885c262016-01-28 21:04:35 +05304721 dev_warn(&instance->pdev->dev,
4722 "Failed to transition controller to ready for "
4723 "scsi%d.\n", instance->host->host_no);
4724 if (instance->requestorId && !reason)
4725 goto fail_kill_adapter;
4726 else
4727 continue;
adam radford9c915a82010-12-21 13:34:31 -08004728 }
adam radford9c915a82010-12-21 13:34:31 -08004729 megasas_reset_reply_desc(instance);
Sumit Saxena308ec452016-01-28 21:04:30 +05304730 megasas_fusion_update_can_queue(instance, OCR_CONTEXT);
4731
adam radford9c915a82010-12-21 13:34:31 -08004732 if (megasas_ioc_init_fusion(instance)) {
Sumit Saxena3885c262016-01-28 21:04:35 +05304733 if (instance->requestorId && !reason)
4734 goto fail_kill_adapter;
4735 else
4736 continue;
adam radford9c915a82010-12-21 13:34:31 -08004737 }
4738
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304739 if (megasas_get_ctrl_info(instance)) {
4740 dev_info(&instance->pdev->dev,
4741 "Failed from %s %d\n",
4742 __func__, __LINE__);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304743 megaraid_sas_kill_hba(instance);
4744 retval = FAILED;
Shivasharan Sa6821ca2017-02-10 00:59:31 -08004745 goto out;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304746 }
Shivasharan S54b28042018-01-05 05:27:47 -08004747
4748 megasas_refire_mgmt_cmd(instance);
4749
adam radford9c915a82010-12-21 13:34:31 -08004750 /* Reset load balance info */
Shivasharan S5fc499b2017-02-10 00:59:17 -08004751 if (fusion->load_balance_info)
4752 memset(fusion->load_balance_info, 0,
4753 (sizeof(struct LD_LOAD_BALANCE_INFO) *
4754 MAX_LOGICAL_DRIVES_EXT));
adam radford9c915a82010-12-21 13:34:31 -08004755
4756 if (!megasas_get_map_info(instance))
4757 megasas_sync_map_info(instance);
4758
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304759 megasas_setup_jbod_map(instance);
4760
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05004761 /* reset stream detection array */
Shivasharan Sf369a312017-10-19 02:48:52 -07004762 if (instance->adapter_type == VENTURA_SERIES) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05004763 for (j = 0; j < MAX_LOGICAL_DRIVES_EXT; ++j) {
4764 memset(fusion->stream_detect_by_ld[j],
4765 0, sizeof(struct LD_STREAM_DETECT));
4766 fusion->stream_detect_by_ld[j]->mru_bit_map
4767 = MR_STREAM_BITMAP;
4768 }
4769 }
4770
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05304771 clear_bit(MEGASAS_FUSION_IN_RESET,
4772 &instance->reset_flags);
4773 instance->instancet->enable_intr(instance);
Shivasharan Se9495e22018-06-04 03:45:12 -07004774
4775 shost_for_each_device(sdev, shost) {
4776 if ((instance->tgt_prop) &&
4777 (instance->nvme_page_size))
4778 ret_target_prop = megasas_get_target_prop(instance, sdev);
4779
4780 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false;
4781 megasas_set_dynamic_target_properties(sdev, is_target_prop);
4782 }
4783
Sumit Saxena8a01a412016-01-28 21:04:32 +05304784 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05304785
Shivasharan S2d2c2332017-08-23 04:47:06 -07004786 dev_info(&instance->pdev->dev, "Interrupts are enabled and"
4787 " controller is OPERATIONAL for scsi:%d\n",
4788 instance->host->host_no);
4789
adam radford229fe472014-03-10 02:51:56 -07004790 /* Restart SR-IOV heartbeat */
4791 if (instance->requestorId) {
4792 if (!megasas_sriov_start_heartbeat(instance, 0))
Kees Cookc251a7b2017-10-22 15:30:04 -07004793 megasas_start_timer(instance);
adam radford229fe472014-03-10 02:51:56 -07004794 else
4795 instance->skip_heartbeat_timer_del = 1;
4796 }
4797
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05304798 if (instance->crash_dump_drv_support &&
4799 instance->crash_dump_app_support)
4800 megasas_set_crash_dump_params(instance,
4801 MR_CRASH_BUF_TURN_ON);
4802 else
4803 megasas_set_crash_dump_params(instance,
4804 MR_CRASH_BUF_TURN_OFF);
4805
Shivasharan Sf0c21df2018-10-16 23:37:40 -07004806 if (instance->snapdump_wait_time) {
4807 megasas_get_snapdump_properties(instance);
4808 dev_info(&instance->pdev->dev,
4809 "Snap dump wait time\t: %d\n",
4810 instance->snapdump_wait_time);
4811 }
4812
adam radford9c915a82010-12-21 13:34:31 -08004813 retval = SUCCESS;
Shivasharan S2d2c2332017-08-23 04:47:06 -07004814
4815 /* Adapter reset completed successfully */
4816 dev_warn(&instance->pdev->dev,
4817 "Reset successful for scsi%d.\n",
4818 instance->host->host_no);
4819
adam radford9c915a82010-12-21 13:34:31 -08004820 goto out;
4821 }
Sumit Saxena3885c262016-01-28 21:04:35 +05304822fail_kill_adapter:
adam radford9c915a82010-12-21 13:34:31 -08004823 /* Reset failed, kill the adapter */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004824 dev_warn(&instance->pdev->dev, "Reset failed, killing "
adam radford229fe472014-03-10 02:51:56 -07004825 "adapter scsi%d.\n", instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08004826 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07004827 instance->skip_heartbeat_timer_del = 1;
adam radford9c915a82010-12-21 13:34:31 -08004828 retval = FAILED;
4829 } else {
adam radford229fe472014-03-10 02:51:56 -07004830 /* For VF: Restart HB timer if we didn't OCR */
4831 if (instance->requestorId) {
Kees Cookc251a7b2017-10-22 15:30:04 -07004832 megasas_start_timer(instance);
adam radford229fe472014-03-10 02:51:56 -07004833 }
adam radford3f0e58bc2011-10-08 18:14:59 -07004834 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304835 instance->instancet->enable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05304836 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
adam radford9c915a82010-12-21 13:34:31 -08004837 }
4838out:
4839 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4840 mutex_unlock(&instance->reset_mutex);
4841 return retval;
4842}
4843
Shivasharan S3f6194a2018-10-16 23:37:39 -07004844/* Fusion Crash dump collection */
4845void megasas_fusion_crash_dump(struct megasas_instance *instance)
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304846{
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304847 u32 status_reg;
4848 u8 partial_copy = 0;
Shivasharan S3f6194a2018-10-16 23:37:39 -07004849 int wait = 0;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304850
4851
4852 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
4853
4854 /*
4855 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
4856 * to host crash buffers
4857 */
4858 if (instance->drv_buf_index == 0) {
4859 /* Buffer is already allocated for old Crash dump.
4860 * Do OCR and do not wait for crash dump collection
4861 */
4862 if (instance->drv_buf_alloc) {
4863 dev_info(&instance->pdev->dev, "earlier crash dump is "
4864 "not yet copied by application, ignoring this "
4865 "crash dump and initiating OCR\n");
4866 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
4867 writel(status_reg,
Shivasharan S81b76452018-10-16 23:37:51 -07004868 &instance->reg_set->outbound_scratch_pad_0);
4869 readl(&instance->reg_set->outbound_scratch_pad_0);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304870 return;
4871 }
4872 megasas_alloc_host_crash_buffer(instance);
4873 dev_info(&instance->pdev->dev, "Number of host crash buffers "
4874 "allocated: %d\n", instance->drv_buf_alloc);
4875 }
4876
Shivasharan S3f6194a2018-10-16 23:37:39 -07004877 while (!(status_reg & MFI_STATE_CRASH_DUMP_DONE) &&
4878 (wait < MEGASAS_WATCHDOG_WAIT_COUNT)) {
4879 if (!(status_reg & MFI_STATE_DMADONE)) {
4880 /*
4881 * Next crash dump buffer is not yet DMA'd by FW
4882 * Check after 10ms. Wait for 1 second for FW to
4883 * post the next buffer. If not bail out.
4884 */
4885 wait++;
4886 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS);
4887 status_reg = instance->instancet->read_fw_status_reg(
4888 instance->reg_set);
4889 continue;
4890 }
4891
4892 wait = 0;
4893 if (instance->drv_buf_index >= instance->drv_buf_alloc) {
4894 dev_info(&instance->pdev->dev,
4895 "Driver is done copying the buffer: %d\n",
4896 instance->drv_buf_alloc);
4897 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
4898 partial_copy = 1;
4899 break;
4900 } else {
4901 memcpy(instance->crash_buf[instance->drv_buf_index],
4902 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
4903 instance->drv_buf_index++;
4904 status_reg &= ~MFI_STATE_DMADONE;
4905 }
4906
Shivasharan S81b76452018-10-16 23:37:51 -07004907 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0);
4908 readl(&instance->reg_set->outbound_scratch_pad_0);
Shivasharan S3f6194a2018-10-16 23:37:39 -07004909
4910 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS);
4911 status_reg = instance->instancet->read_fw_status_reg(
4912 instance->reg_set);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304913 }
4914
4915 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
4916 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
4917 "of copied buffers: %d\n", instance->drv_buf_index);
4918 instance->fw_crash_buffer_size = instance->drv_buf_index;
4919 instance->fw_crash_state = AVAILABLE;
4920 instance->drv_buf_index = 0;
Shivasharan S81b76452018-10-16 23:37:51 -07004921 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0);
4922 readl(&instance->reg_set->outbound_scratch_pad_0);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304923 if (!partial_copy)
4924 megasas_reset_fusion(instance->host, 0);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304925 }
4926}
4927
4928
adam radford9c915a82010-12-21 13:34:31 -08004929/* Fusion OCR work queue */
4930void megasas_fusion_ocr_wq(struct work_struct *work)
4931{
4932 struct megasas_instance *instance =
4933 container_of(work, struct megasas_instance, work_init);
4934
adam radford229fe472014-03-10 02:51:56 -07004935 megasas_reset_fusion(instance->host, 0);
adam radford9c915a82010-12-21 13:34:31 -08004936}
4937
Shivasharan S5fc499b2017-02-10 00:59:17 -08004938/* Allocate fusion context */
4939int
4940megasas_alloc_fusion_context(struct megasas_instance *instance)
4941{
4942 struct fusion_context *fusion;
4943
Shivasharan S2dd689c2017-10-19 02:48:53 -07004944 instance->ctrl_context = kzalloc(sizeof(struct fusion_context),
4945 GFP_KERNEL);
Shivasharan S5fc499b2017-02-10 00:59:17 -08004946 if (!instance->ctrl_context) {
Shivasharan S2dd689c2017-10-19 02:48:53 -07004947 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
4948 __func__, __LINE__);
4949 return -ENOMEM;
Shivasharan S5fc499b2017-02-10 00:59:17 -08004950 }
4951
4952 fusion = instance->ctrl_context;
4953
Shivasharan S2dd689c2017-10-19 02:48:53 -07004954 fusion->log_to_span_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
4955 sizeof(LD_SPAN_INFO));
4956 fusion->log_to_span =
4957 (PLD_SPAN_INFO)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
4958 fusion->log_to_span_pages);
4959 if (!fusion->log_to_span) {
Kees Cookfad953c2018-06-12 14:27:37 -07004960 fusion->log_to_span =
4961 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
4962 sizeof(LD_SPAN_INFO)));
Shivasharan S2dd689c2017-10-19 02:48:53 -07004963 if (!fusion->log_to_span) {
4964 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
4965 __func__, __LINE__);
4966 return -ENOMEM;
4967 }
4968 }
4969
Shivasharan S5fc499b2017-02-10 00:59:17 -08004970 fusion->load_balance_info_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
4971 sizeof(struct LD_LOAD_BALANCE_INFO));
4972 fusion->load_balance_info =
4973 (struct LD_LOAD_BALANCE_INFO *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
4974 fusion->load_balance_info_pages);
4975 if (!fusion->load_balance_info) {
Kees Cookfad953c2018-06-12 14:27:37 -07004976 fusion->load_balance_info =
4977 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT,
4978 sizeof(struct LD_LOAD_BALANCE_INFO)));
Shivasharan S5fc499b2017-02-10 00:59:17 -08004979 if (!fusion->load_balance_info)
4980 dev_err(&instance->pdev->dev, "Failed to allocate load_balance_info, "
4981 "continuing without Load Balance support\n");
4982 }
4983
4984 return 0;
4985}
4986
4987void
4988megasas_free_fusion_context(struct megasas_instance *instance)
4989{
4990 struct fusion_context *fusion = instance->ctrl_context;
4991
4992 if (fusion) {
4993 if (fusion->load_balance_info) {
4994 if (is_vmalloc_addr(fusion->load_balance_info))
4995 vfree(fusion->load_balance_info);
4996 else
4997 free_pages((ulong)fusion->load_balance_info,
4998 fusion->load_balance_info_pages);
4999 }
5000
Shivasharan S2dd689c2017-10-19 02:48:53 -07005001 if (fusion->log_to_span) {
5002 if (is_vmalloc_addr(fusion->log_to_span))
5003 vfree(fusion->log_to_span);
5004 else
5005 free_pages((ulong)fusion->log_to_span,
5006 fusion->log_to_span_pages);
5007 }
5008
5009 kfree(fusion);
Shivasharan S5fc499b2017-02-10 00:59:17 -08005010 }
5011}
5012
adam radford9c915a82010-12-21 13:34:31 -08005013struct megasas_instance_template megasas_instance_template_fusion = {
adam radford9c915a82010-12-21 13:34:31 -08005014 .enable_intr = megasas_enable_intr_fusion,
5015 .disable_intr = megasas_disable_intr_fusion,
5016 .clear_intr = megasas_clear_intr_fusion,
5017 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
5018 .adp_reset = megasas_adp_reset_fusion,
5019 .check_reset = megasas_check_reset_fusion,
5020 .service_isr = megasas_isr_fusion,
5021 .tasklet = megasas_complete_cmd_dpc_fusion,
5022 .init_adapter = megasas_init_adapter_fusion,
5023 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
5024 .issue_dcmd = megasas_issue_dcmd_fusion,
5025};