blob: baea4c2137d7779b59c871904879cef638d3e263 [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
5 * Copyright (c) 2013-2014 Avago Technologies
adam radford9c915a82010-12-21 13:34:31 -08006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053018 * along with this program. If not, see <http://www.gnu.org/licenses/>.
adam radford9c915a82010-12-21 13:34:31 -080019 *
20 * FILE: megaraid_sas_fusion.c
21 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053022 * Authors: Avago Technologies
adam radford9c915a82010-12-21 13:34:31 -080023 * Sumant Patro
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053024 * Adam Radford
25 * Kashyap Desai <kashyap.desai@avagotech.com>
26 * Sumit Saxena <sumit.saxena@avagotech.com>
adam radford9c915a82010-12-21 13:34:31 -080027 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053028 * Send feedback to: megaraidlinux.pdl@avagotech.com
adam radford9c915a82010-12-21 13:34:31 -080029 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053030 * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31 * San Jose, California 95131
adam radford9c915a82010-12-21 13:34:31 -080032 */
33
34#include <linux/kernel.h>
35#include <linux/types.h>
36#include <linux/pci.h>
37#include <linux/list.h>
38#include <linux/moduleparam.h>
39#include <linux/module.h>
40#include <linux/spinlock.h>
41#include <linux/interrupt.h>
42#include <linux/delay.h>
adam radford9c915a82010-12-21 13:34:31 -080043#include <linux/uio.h>
44#include <linux/uaccess.h>
45#include <linux/fs.h>
46#include <linux/compat.h>
47#include <linux/blkdev.h>
48#include <linux/mutex.h>
49#include <linux/poll.h>
Shivasharan S5fc499b2017-02-10 00:59:17 -080050#include <linux/vmalloc.h>
adam radford9c915a82010-12-21 13:34:31 -080051
52#include <scsi/scsi.h>
53#include <scsi/scsi_cmnd.h>
54#include <scsi/scsi_device.h>
55#include <scsi/scsi_host.h>
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +053056#include <scsi/scsi_dbg.h>
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +053057#include <linux/dmi.h>
adam radford9c915a82010-12-21 13:34:31 -080058
59#include "megaraid_sas_fusion.h"
60#include "megaraid_sas.h"
61
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +053062
adam radford9c915a82010-12-21 13:34:31 -080063extern void megasas_free_cmds(struct megasas_instance *instance);
64extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
65 *instance);
66extern void
67megasas_complete_cmd(struct megasas_instance *instance,
68 struct megasas_cmd *cmd, u8 alt_status);
adam radford9c915a82010-12-21 13:34:31 -080069int
adam radford229fe472014-03-10 02:51:56 -070070wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
71 int seconds);
adam radford9c915a82010-12-21 13:34:31 -080072
73void
74megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
75int megasas_alloc_cmds(struct megasas_instance *instance);
76int
77megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
78int
79megasas_issue_polled(struct megasas_instance *instance,
80 struct megasas_cmd *cmd);
adam radford53ef2bb2011-02-24 20:56:05 -080081void
82megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
83
adam radford058a8fa2011-10-08 18:14:27 -070084int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
adam radford9c915a82010-12-21 13:34:31 -080085void megaraid_sas_kill_hba(struct megasas_instance *instance);
86
87extern u32 megasas_dbg_lvl;
adam radford229fe472014-03-10 02:51:56 -070088void megasas_sriov_heartbeat_handler(unsigned long instance_addr);
89int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
90 int initial);
91void megasas_start_timer(struct megasas_instance *instance,
92 struct timer_list *timer,
93 void *fn, unsigned long interval);
94extern struct megasas_mgmt_info megasas_mgmt_info;
Sumit Saxenae3d178c2016-01-28 21:04:34 +053095extern unsigned int resetwaittime;
Sumit Saxena308ec452016-01-28 21:04:30 +053096extern unsigned int dual_qdepth_disable;
Sumit Saxena179ac142016-01-28 21:04:28 +053097static void megasas_free_rdpq_fusion(struct megasas_instance *instance);
98static void megasas_free_reply_fusion(struct megasas_instance *instance);
adam radford9c915a82010-12-21 13:34:31 -080099
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +0530100
101
adam radford9c915a82010-12-21 13:34:31 -0800102/**
103 * megasas_enable_intr_fusion - Enables interrupts
104 * @regs: MFI register set
105 */
106void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530107megasas_enable_intr_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800108{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530109 struct megasas_register_set __iomem *regs;
110 regs = instance->reg_set;
Sumit.Saxena@avagotech.comc2ced172015-01-05 20:06:13 +0530111
112 instance->mask_interrupts = 0;
adam radford6497b242011-10-08 18:14:50 -0700113 /* For Thunderbolt/Invader also clear intr on enable */
114 writel(~0, &regs->outbound_intr_status);
115 readl(&regs->outbound_intr_status);
116
adam radford9c915a82010-12-21 13:34:31 -0800117 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
118
119 /* Dummy readl to force pci flush */
120 readl(&regs->outbound_intr_mask);
121}
122
123/**
124 * megasas_disable_intr_fusion - Disables interrupt
125 * @regs: MFI register set
126 */
127void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530128megasas_disable_intr_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800129{
130 u32 mask = 0xFFFFFFFF;
131 u32 status;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530132 struct megasas_register_set __iomem *regs;
133 regs = instance->reg_set;
134 instance->mask_interrupts = 1;
adam radford9c915a82010-12-21 13:34:31 -0800135
136 writel(mask, &regs->outbound_intr_mask);
137 /* Dummy readl to force pci flush */
138 status = readl(&regs->outbound_intr_mask);
139}
140
141int
142megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
143{
144 u32 status;
145 /*
146 * Check if it is our interrupt
147 */
148 status = readl(&regs->outbound_intr_status);
149
150 if (status & 1) {
151 writel(status, &regs->outbound_intr_status);
152 readl(&regs->outbound_intr_status);
153 return 1;
154 }
155 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
156 return 0;
157
adam radford9c915a82010-12-21 13:34:31 -0800158 return 1;
159}
160
161/**
162 * megasas_get_cmd_fusion - Get a command from the free pool
163 * @instance: Adapter soft state
164 *
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530165 * Returns a blk_tag indexed mpt frame
adam radford9c915a82010-12-21 13:34:31 -0800166 */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530167inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
168 *instance, u32 blk_tag)
adam radford9c915a82010-12-21 13:34:31 -0800169{
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530170 struct fusion_context *fusion;
adam radford9c915a82010-12-21 13:34:31 -0800171
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530172 fusion = instance->ctrl_context;
173 return fusion->cmd_list[blk_tag];
adam radford9c915a82010-12-21 13:34:31 -0800174}
175
176/**
177 * megasas_return_cmd_fusion - Return a cmd to free command pool
178 * @instance: Adapter soft state
179 * @cmd: Command packet to be returned to free command pool
180 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530181inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
182 struct megasas_cmd_fusion *cmd)
adam radford9c915a82010-12-21 13:34:31 -0800183{
adam radford9c915a82010-12-21 13:34:31 -0800184 cmd->scmd = NULL;
Shivasharan S8bf7c652017-02-10 00:59:03 -0800185 memset(cmd->io_request, 0, MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
186 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
187 cmd->cmd_completed = false;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530188}
189
190/**
Christoph Hellwig01d7f032015-04-23 16:34:09 +0530191 * megasas_fire_cmd_fusion - Sends command to the FW
Shivasharan S45b8a352017-02-10 00:59:04 -0800192 * @instance: Adapter soft state
193 * @req_desc: 32bit or 64bit Request descriptor
194 *
195 * Perform PCI Write. Ventura supports 32 bit Descriptor.
196 * Prior to Ventura (12G) MR controller supports 64 bit Descriptor.
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530197 */
Shivasharan S45b8a352017-02-10 00:59:04 -0800198
Christoph Hellwig01d7f032015-04-23 16:34:09 +0530199static void
200megasas_fire_cmd_fusion(struct megasas_instance *instance,
Shivasharan S45b8a352017-02-10 00:59:04 -0800201 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530202{
Shivasharan S45b8a352017-02-10 00:59:04 -0800203 if (instance->is_ventura)
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500204 writel(le32_to_cpu(req_desc->u.low),
Shivasharan S45b8a352017-02-10 00:59:04 -0800205 &instance->reg_set->inbound_single_queue_port);
206 else {
207#if defined(writeq) && defined(CONFIG_64BIT)
208 u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
209 le32_to_cpu(req_desc->u.low));
210
211 writeq(req_data, &instance->reg_set->inbound_low_queue_port);
212#else
213 unsigned long flags;
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500214 spin_lock_irqsave(&instance->hba_lock, flags);
215 writel(le32_to_cpu(req_desc->u.low),
Shivasharan S45b8a352017-02-10 00:59:04 -0800216 &instance->reg_set->inbound_low_queue_port);
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500217 writel(le32_to_cpu(req_desc->u.high),
Shivasharan S45b8a352017-02-10 00:59:04 -0800218 &instance->reg_set->inbound_high_queue_port);
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500219 mmiowb();
220 spin_unlock_irqrestore(&instance->hba_lock, flags);
Christoph Hellwig01d7f032015-04-23 16:34:09 +0530221#endif
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500222 }
adam radford9c915a82010-12-21 13:34:31 -0800223}
224
adam radford9c915a82010-12-21 13:34:31 -0800225/**
Sumit Saxena308ec452016-01-28 21:04:30 +0530226 * megasas_fusion_update_can_queue - Do all Adapter Queue depth related calculations here
227 * @instance: Adapter soft state
228 * fw_boot_context: Whether this function called during probe or after OCR
229 *
230 * This function is only for fusion controllers.
231 * Update host can queue, if firmware downgrade max supported firmware commands.
232 * Firmware upgrade case will be skiped because underlying firmware has
233 * more resource than exposed to the OS.
234 *
235 */
236static void
237megasas_fusion_update_can_queue(struct megasas_instance *instance, int fw_boot_context)
238{
239 u16 cur_max_fw_cmds = 0;
240 u16 ldio_threshold = 0;
241 struct megasas_register_set __iomem *reg_set;
242
243 reg_set = instance->reg_set;
244
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -0500245 /* ventura FW does not fill outbound_scratch_pad_3 with queue depth */
246 if (!instance->is_ventura)
247 cur_max_fw_cmds =
248 readl(&instance->reg_set->outbound_scratch_pad_3) & 0x00FFFF;
Sumit Saxena308ec452016-01-28 21:04:30 +0530249
250 if (dual_qdepth_disable || !cur_max_fw_cmds)
251 cur_max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
252 else
253 ldio_threshold =
254 (instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF) - MEGASAS_FUSION_IOCTL_CMDS;
255
256 dev_info(&instance->pdev->dev,
257 "Current firmware maximum commands: %d\t LDIO threshold: %d\n",
258 cur_max_fw_cmds, ldio_threshold);
259
260 if (fw_boot_context == OCR_CONTEXT) {
261 cur_max_fw_cmds = cur_max_fw_cmds - 1;
262 if (cur_max_fw_cmds <= instance->max_fw_cmds) {
263 instance->cur_can_queue =
264 cur_max_fw_cmds - (MEGASAS_FUSION_INTERNAL_CMDS +
265 MEGASAS_FUSION_IOCTL_CMDS);
266 instance->host->can_queue = instance->cur_can_queue;
267 instance->ldio_threshold = ldio_threshold;
268 }
269 } else {
270 instance->max_fw_cmds = cur_max_fw_cmds;
271 instance->ldio_threshold = ldio_threshold;
272
273 if (!instance->is_rdpq)
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500274 instance->max_fw_cmds =
275 min_t(u16, instance->max_fw_cmds, 1024);
Sumit Saxena308ec452016-01-28 21:04:30 +0530276
Sumit Saxenac3e385a2016-04-15 00:23:30 -0700277 if (reset_devices)
278 instance->max_fw_cmds = min(instance->max_fw_cmds,
279 (u16)MEGASAS_KDUMP_QUEUE_DEPTH);
Sumit Saxena308ec452016-01-28 21:04:30 +0530280 /*
281 * Reduce the max supported cmds by 1. This is to ensure that the
282 * reply_q_sz (1 more than the max cmd that driver may send)
283 * does not exceed max cmds that the FW can support
284 */
285 instance->max_fw_cmds = instance->max_fw_cmds-1;
286
287 instance->max_scsi_cmds = instance->max_fw_cmds -
288 (MEGASAS_FUSION_INTERNAL_CMDS +
289 MEGASAS_FUSION_IOCTL_CMDS);
290 instance->cur_can_queue = instance->max_scsi_cmds;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500291 instance->host->can_queue = instance->cur_can_queue;
Sumit Saxena308ec452016-01-28 21:04:30 +0530292 }
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500293
294 if (instance->is_ventura)
295 instance->max_mpt_cmds =
296 instance->max_fw_cmds * RAID_1_10_RMW_CMDS;
297 else
298 instance->max_mpt_cmds = instance->max_fw_cmds;
Sumit Saxena308ec452016-01-28 21:04:30 +0530299}
300/**
adam radford9c915a82010-12-21 13:34:31 -0800301 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
302 * @instance: Adapter soft state
303 */
304void
305megasas_free_cmds_fusion(struct megasas_instance *instance)
306{
307 int i;
308 struct fusion_context *fusion = instance->ctrl_context;
Sumit Saxena179ac142016-01-28 21:04:28 +0530309 struct megasas_cmd_fusion *cmd;
adam radford9c915a82010-12-21 13:34:31 -0800310
Sumit Saxena179ac142016-01-28 21:04:28 +0530311 /* SG, Sense */
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500312 for (i = 0; i < instance->max_mpt_cmds; i++) {
Sumit Saxena179ac142016-01-28 21:04:28 +0530313 cmd = fusion->cmd_list[i];
314 if (cmd) {
315 if (cmd->sg_frame)
316 pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
317 cmd->sg_frame_phys_addr);
318 if (cmd->sense)
319 pci_pool_free(fusion->sense_dma_pool, cmd->sense,
320 cmd->sense_phys_addr);
321 }
322 }
323
324 if (fusion->sg_dma_pool) {
325 pci_pool_destroy(fusion->sg_dma_pool);
326 fusion->sg_dma_pool = NULL;
327 }
328 if (fusion->sense_dma_pool) {
329 pci_pool_destroy(fusion->sense_dma_pool);
330 fusion->sense_dma_pool = NULL;
331 }
adam radford9c915a82010-12-21 13:34:31 -0800332
333
Sumit Saxena179ac142016-01-28 21:04:28 +0530334 /* Reply Frame, Desc*/
335 if (instance->is_rdpq)
336 megasas_free_rdpq_fusion(instance);
337 else
338 megasas_free_reply_fusion(instance);
adam radford9c915a82010-12-21 13:34:31 -0800339
Sumit Saxena179ac142016-01-28 21:04:28 +0530340 /* Request Frame, Desc*/
adam radford9c915a82010-12-21 13:34:31 -0800341 if (fusion->req_frames_desc)
Sumit Saxena179ac142016-01-28 21:04:28 +0530342 dma_free_coherent(&instance->pdev->dev,
343 fusion->request_alloc_sz, fusion->req_frames_desc,
344 fusion->req_frames_desc_phys);
345 if (fusion->io_request_frames)
adam radford9c915a82010-12-21 13:34:31 -0800346 pci_pool_free(fusion->io_request_frames_pool,
Sumit Saxena179ac142016-01-28 21:04:28 +0530347 fusion->io_request_frames,
348 fusion->io_request_frames_phys);
349 if (fusion->io_request_frames_pool) {
adam radford9c915a82010-12-21 13:34:31 -0800350 pci_pool_destroy(fusion->io_request_frames_pool);
Sumit Saxena179ac142016-01-28 21:04:28 +0530351 fusion->io_request_frames_pool = NULL;
adam radford9c915a82010-12-21 13:34:31 -0800352 }
353
adam radford9c915a82010-12-21 13:34:31 -0800354
Sumit Saxena179ac142016-01-28 21:04:28 +0530355 /* cmd_list */
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500356 for (i = 0; i < instance->max_mpt_cmds; i++)
adam radford9c915a82010-12-21 13:34:31 -0800357 kfree(fusion->cmd_list[i]);
358
adam radford9c915a82010-12-21 13:34:31 -0800359 kfree(fusion->cmd_list);
adam radford9c915a82010-12-21 13:34:31 -0800360}
361
362/**
Sumit Saxena179ac142016-01-28 21:04:28 +0530363 * megasas_create_sg_sense_fusion - Creates DMA pool for cmd frames
adam radford9c915a82010-12-21 13:34:31 -0800364 * @instance: Adapter soft state
365 *
366 */
Sumit Saxena179ac142016-01-28 21:04:28 +0530367static int megasas_create_sg_sense_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800368{
369 int i;
370 u32 max_cmd;
371 struct fusion_context *fusion;
372 struct megasas_cmd_fusion *cmd;
adam radford9c915a82010-12-21 13:34:31 -0800373
374 fusion = instance->ctrl_context;
375 max_cmd = instance->max_fw_cmds;
376
adam radford9c915a82010-12-21 13:34:31 -0800377
Sumit Saxena179ac142016-01-28 21:04:28 +0530378 fusion->sg_dma_pool =
379 pci_pool_create("mr_sg", instance->pdev,
Shivasharan S15dd0382017-02-10 00:59:10 -0800380 instance->max_chain_frame_sz,
381 MR_DEFAULT_NVME_PAGE_SIZE, 0);
Sumit Saxena179ac142016-01-28 21:04:28 +0530382 /* SCSI_SENSE_BUFFERSIZE = 96 bytes */
383 fusion->sense_dma_pool =
384 pci_pool_create("mr_sense", instance->pdev,
385 SCSI_SENSE_BUFFERSIZE, 64, 0);
adam radford9c915a82010-12-21 13:34:31 -0800386
Sumit Saxena179ac142016-01-28 21:04:28 +0530387 if (!fusion->sense_dma_pool || !fusion->sg_dma_pool) {
388 dev_err(&instance->pdev->dev,
389 "Failed from %s %d\n", __func__, __LINE__);
adam radford9c915a82010-12-21 13:34:31 -0800390 return -ENOMEM;
391 }
392
393 /*
394 * Allocate and attach a frame to each of the commands in cmd_list
395 */
396 for (i = 0; i < max_cmd; i++) {
adam radford9c915a82010-12-21 13:34:31 -0800397 cmd = fusion->cmd_list[i];
adam radford9c915a82010-12-21 13:34:31 -0800398 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
Sumit Saxena179ac142016-01-28 21:04:28 +0530399 GFP_KERNEL, &cmd->sg_frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -0800400
401 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
Sumit Saxena179ac142016-01-28 21:04:28 +0530402 GFP_KERNEL, &cmd->sense_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -0800403 if (!cmd->sg_frame || !cmd->sense) {
Sumit Saxena179ac142016-01-28 21:04:28 +0530404 dev_err(&instance->pdev->dev,
405 "Failed from %s %d\n", __func__, __LINE__);
adam radford9c915a82010-12-21 13:34:31 -0800406 return -ENOMEM;
407 }
408 }
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500409
410 /* create sense buffer for the raid 1/10 fp */
411 for (i = max_cmd; i < instance->max_mpt_cmds; i++) {
412 cmd = fusion->cmd_list[i];
413 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
414 GFP_KERNEL, &cmd->sense_phys_addr);
415 if (!cmd->sense) {
416 dev_err(&instance->pdev->dev,
417 "Failed from %s %d\n", __func__, __LINE__);
418 return -ENOMEM;
419 }
420 }
421
adam radford9c915a82010-12-21 13:34:31 -0800422 return 0;
423}
424
Sumit Saxena179ac142016-01-28 21:04:28 +0530425int
426megasas_alloc_cmdlist_fusion(struct megasas_instance *instance)
427{
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500428 u32 max_mpt_cmd, i;
Sumit Saxena179ac142016-01-28 21:04:28 +0530429 struct fusion_context *fusion;
430
431 fusion = instance->ctrl_context;
432
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500433 max_mpt_cmd = instance->max_mpt_cmds;
Sumit Saxena179ac142016-01-28 21:04:28 +0530434
435 /*
436 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
437 * Allocate the dynamic array first and then allocate individual
438 * commands.
439 */
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500440 fusion->cmd_list =
441 kzalloc(sizeof(struct megasas_cmd_fusion *) * max_mpt_cmd,
442 GFP_KERNEL);
Sumit Saxena179ac142016-01-28 21:04:28 +0530443 if (!fusion->cmd_list) {
444 dev_err(&instance->pdev->dev,
445 "Failed from %s %d\n", __func__, __LINE__);
446 return -ENOMEM;
447 }
448
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500449
450
451 for (i = 0; i < max_mpt_cmd; i++) {
Sumit Saxena179ac142016-01-28 21:04:28 +0530452 fusion->cmd_list[i] = kzalloc(sizeof(struct megasas_cmd_fusion),
453 GFP_KERNEL);
454 if (!fusion->cmd_list[i]) {
455 dev_err(&instance->pdev->dev,
456 "Failed from %s %d\n", __func__, __LINE__);
457 return -ENOMEM;
458 }
459 }
460 return 0;
461}
462int
463megasas_alloc_request_fusion(struct megasas_instance *instance)
464{
465 struct fusion_context *fusion;
466
467 fusion = instance->ctrl_context;
468
469 fusion->req_frames_desc =
470 dma_alloc_coherent(&instance->pdev->dev,
471 fusion->request_alloc_sz,
472 &fusion->req_frames_desc_phys, GFP_KERNEL);
473 if (!fusion->req_frames_desc) {
474 dev_err(&instance->pdev->dev,
475 "Failed from %s %d\n", __func__, __LINE__);
476 return -ENOMEM;
477 }
478
479 fusion->io_request_frames_pool =
480 pci_pool_create("mr_ioreq", instance->pdev,
481 fusion->io_frames_alloc_sz, 16, 0);
482
483 if (!fusion->io_request_frames_pool) {
484 dev_err(&instance->pdev->dev,
485 "Failed from %s %d\n", __func__, __LINE__);
486 return -ENOMEM;
487 }
488
489 fusion->io_request_frames =
490 pci_pool_alloc(fusion->io_request_frames_pool,
491 GFP_KERNEL, &fusion->io_request_frames_phys);
492 if (!fusion->io_request_frames) {
493 dev_err(&instance->pdev->dev,
494 "Failed from %s %d\n", __func__, __LINE__);
495 return -ENOMEM;
496 }
497 return 0;
498}
499
500int
501megasas_alloc_reply_fusion(struct megasas_instance *instance)
502{
503 int i, count;
504 struct fusion_context *fusion;
505 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
506 fusion = instance->ctrl_context;
507
508 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
509 fusion->reply_frames_desc_pool =
510 pci_pool_create("mr_reply", instance->pdev,
511 fusion->reply_alloc_sz * count, 16, 0);
512
513 if (!fusion->reply_frames_desc_pool) {
514 dev_err(&instance->pdev->dev,
515 "Failed from %s %d\n", __func__, __LINE__);
516 return -ENOMEM;
517 }
518
519 fusion->reply_frames_desc[0] =
520 pci_pool_alloc(fusion->reply_frames_desc_pool,
521 GFP_KERNEL, &fusion->reply_frames_desc_phys[0]);
522 if (!fusion->reply_frames_desc[0]) {
523 dev_err(&instance->pdev->dev,
524 "Failed from %s %d\n", __func__, __LINE__);
525 return -ENOMEM;
526 }
527 reply_desc = fusion->reply_frames_desc[0];
528 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
529 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
530
531 /* This is not a rdpq mode, but driver still populate
532 * reply_frame_desc array to use same msix index in ISR path.
533 */
534 for (i = 0; i < (count - 1); i++)
535 fusion->reply_frames_desc[i + 1] =
536 fusion->reply_frames_desc[i] +
537 (fusion->reply_alloc_sz)/sizeof(union MPI2_REPLY_DESCRIPTORS_UNION);
538
539 return 0;
540}
541
542int
543megasas_alloc_rdpq_fusion(struct megasas_instance *instance)
544{
545 int i, j, count;
546 struct fusion_context *fusion;
547 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
548
549 fusion = instance->ctrl_context;
550
551 fusion->rdpq_virt = pci_alloc_consistent(instance->pdev,
552 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
553 &fusion->rdpq_phys);
554 if (!fusion->rdpq_virt) {
555 dev_err(&instance->pdev->dev,
556 "Failed from %s %d\n", __func__, __LINE__);
557 return -ENOMEM;
558 }
559
560 memset(fusion->rdpq_virt, 0,
561 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION);
562 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
563 fusion->reply_frames_desc_pool = pci_pool_create("mr_rdpq",
564 instance->pdev, fusion->reply_alloc_sz, 16, 0);
565
566 if (!fusion->reply_frames_desc_pool) {
567 dev_err(&instance->pdev->dev,
568 "Failed from %s %d\n", __func__, __LINE__);
569 return -ENOMEM;
570 }
571
572 for (i = 0; i < count; i++) {
573 fusion->reply_frames_desc[i] =
574 pci_pool_alloc(fusion->reply_frames_desc_pool,
575 GFP_KERNEL, &fusion->reply_frames_desc_phys[i]);
576 if (!fusion->reply_frames_desc[i]) {
577 dev_err(&instance->pdev->dev,
578 "Failed from %s %d\n", __func__, __LINE__);
579 return -ENOMEM;
580 }
581
582 fusion->rdpq_virt[i].RDPQBaseAddress =
Shivasharan Sff96f922017-02-10 00:59:20 -0800583 cpu_to_le64(fusion->reply_frames_desc_phys[i]);
Sumit Saxena179ac142016-01-28 21:04:28 +0530584
585 reply_desc = fusion->reply_frames_desc[i];
586 for (j = 0; j < fusion->reply_q_depth; j++, reply_desc++)
587 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
588 }
589 return 0;
590}
591
592static void
593megasas_free_rdpq_fusion(struct megasas_instance *instance) {
594
595 int i;
596 struct fusion_context *fusion;
597
598 fusion = instance->ctrl_context;
599
600 for (i = 0; i < MAX_MSIX_QUEUES_FUSION; i++) {
601 if (fusion->reply_frames_desc[i])
602 pci_pool_free(fusion->reply_frames_desc_pool,
603 fusion->reply_frames_desc[i],
604 fusion->reply_frames_desc_phys[i]);
605 }
606
607 if (fusion->reply_frames_desc_pool)
608 pci_pool_destroy(fusion->reply_frames_desc_pool);
609
610 if (fusion->rdpq_virt)
611 pci_free_consistent(instance->pdev,
612 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
613 fusion->rdpq_virt, fusion->rdpq_phys);
614}
615
616static void
617megasas_free_reply_fusion(struct megasas_instance *instance) {
618
619 struct fusion_context *fusion;
620
621 fusion = instance->ctrl_context;
622
623 if (fusion->reply_frames_desc[0])
624 pci_pool_free(fusion->reply_frames_desc_pool,
625 fusion->reply_frames_desc[0],
626 fusion->reply_frames_desc_phys[0]);
627
628 if (fusion->reply_frames_desc_pool)
629 pci_pool_destroy(fusion->reply_frames_desc_pool);
630
631}
632
633
adam radford9c915a82010-12-21 13:34:31 -0800634/**
635 * megasas_alloc_cmds_fusion - Allocates the command packets
636 * @instance: Adapter soft state
637 *
638 *
639 * Each frame has a 32-bit field called context. This context is used to get
640 * back the megasas_cmd_fusion from the frame when a frame gets completed
641 * In this driver, the 32 bit values are the indices into an array cmd_list.
642 * This array is used only to look up the megasas_cmd_fusion given the context.
643 * The free commands themselves are maintained in a linked list called cmd_pool.
644 *
645 * cmds are formed in the io_request and sg_frame members of the
646 * megasas_cmd_fusion. The context field is used to get a request descriptor
647 * and is used as SMID of the cmd.
648 * SMID value range is from 1 to max_fw_cmds.
649 */
650int
651megasas_alloc_cmds_fusion(struct megasas_instance *instance)
652{
Sumit Saxena179ac142016-01-28 21:04:28 +0530653 int i;
adam radford9c915a82010-12-21 13:34:31 -0800654 struct fusion_context *fusion;
655 struct megasas_cmd_fusion *cmd;
adam radford9c915a82010-12-21 13:34:31 -0800656 u32 offset;
657 dma_addr_t io_req_base_phys;
658 u8 *io_req_base;
659
Sumit Saxena179ac142016-01-28 21:04:28 +0530660
adam radford9c915a82010-12-21 13:34:31 -0800661 fusion = instance->ctrl_context;
662
Sumit Saxena179ac142016-01-28 21:04:28 +0530663 if (megasas_alloc_cmdlist_fusion(instance))
664 goto fail_exit;
adam radford9c915a82010-12-21 13:34:31 -0800665
Sumit Saxena179ac142016-01-28 21:04:28 +0530666 if (megasas_alloc_request_fusion(instance))
667 goto fail_exit;
adam radford9c915a82010-12-21 13:34:31 -0800668
Sumit Saxena179ac142016-01-28 21:04:28 +0530669 if (instance->is_rdpq) {
670 if (megasas_alloc_rdpq_fusion(instance))
671 goto fail_exit;
672 } else
673 if (megasas_alloc_reply_fusion(instance))
674 goto fail_exit;
adam radford9c915a82010-12-21 13:34:31 -0800675
adam radford9c915a82010-12-21 13:34:31 -0800676
Sumit Saxena179ac142016-01-28 21:04:28 +0530677 /* The first 256 bytes (SMID 0) is not used. Don't add to the cmd list */
678 io_req_base = fusion->io_request_frames + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
679 io_req_base_phys = fusion->io_request_frames_phys + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
adam radford9c915a82010-12-21 13:34:31 -0800680
681 /*
682 * Add all the commands to command pool (fusion->cmd_pool)
683 */
684
685 /* SMID 0 is reserved. Set SMID/index from 1 */
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500686 for (i = 0; i < instance->max_mpt_cmds; i++) {
adam radford9c915a82010-12-21 13:34:31 -0800687 cmd = fusion->cmd_list[i];
688 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
689 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
690 cmd->index = i + 1;
691 cmd->scmd = NULL;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500692 cmd->sync_cmd_idx =
693 (i >= instance->max_scsi_cmds && i < instance->max_fw_cmds) ?
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530694 (i - instance->max_scsi_cmds) :
695 (u32)ULONG_MAX; /* Set to Invalid */
adam radford9c915a82010-12-21 13:34:31 -0800696 cmd->instance = instance;
697 cmd->io_request =
698 (struct MPI2_RAID_SCSI_IO_REQUEST *)
699 (io_req_base + offset);
700 memset(cmd->io_request, 0,
701 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
702 cmd->io_request_phys_addr = io_req_base_phys + offset;
Shivasharan S8bf7c652017-02-10 00:59:03 -0800703 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
adam radford9c915a82010-12-21 13:34:31 -0800704 }
705
Sumit Saxena179ac142016-01-28 21:04:28 +0530706 if (megasas_create_sg_sense_fusion(instance))
707 goto fail_exit;
adam radford9c915a82010-12-21 13:34:31 -0800708
709 return 0;
710
Sumit Saxena179ac142016-01-28 21:04:28 +0530711fail_exit:
712 megasas_free_cmds_fusion(instance);
adam radford9c915a82010-12-21 13:34:31 -0800713 return -ENOMEM;
714}
715
716/**
717 * wait_and_poll - Issues a polling command
718 * @instance: Adapter soft state
719 * @cmd: Command packet to be issued
720 *
721 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
722 */
723int
adam radford229fe472014-03-10 02:51:56 -0700724wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
725 int seconds)
adam radford9c915a82010-12-21 13:34:31 -0800726{
727 int i;
728 struct megasas_header *frame_hdr = &cmd->frame->hdr;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530729 struct fusion_context *fusion;
adam radford9c915a82010-12-21 13:34:31 -0800730
adam radford229fe472014-03-10 02:51:56 -0700731 u32 msecs = seconds * 1000;
adam radford9c915a82010-12-21 13:34:31 -0800732
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530733 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -0800734 /*
735 * Wait for cmd_status to change
736 */
737 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
738 rmb();
739 msleep(20);
740 }
741
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530742 if (frame_hdr->cmd_status == MFI_STAT_INVALID_STATUS)
743 return DCMD_TIMEOUT;
744 else if (frame_hdr->cmd_status == MFI_STAT_OK)
745 return DCMD_SUCCESS;
746 else
747 return DCMD_FAILED;
adam radford9c915a82010-12-21 13:34:31 -0800748}
749
750/**
751 * megasas_ioc_init_fusion - Initializes the FW
752 * @instance: Adapter soft state
753 *
754 * Issues the IOC Init cmd
755 */
756int
757megasas_ioc_init_fusion(struct megasas_instance *instance)
758{
759 struct megasas_init_frame *init_frame;
Sumit Saxena179ac142016-01-28 21:04:28 +0530760 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage = NULL;
adam radford9c915a82010-12-21 13:34:31 -0800761 dma_addr_t ioc_init_handle;
adam radford9c915a82010-12-21 13:34:31 -0800762 struct megasas_cmd *cmd;
Sumit Saxena179ac142016-01-28 21:04:28 +0530763 u8 ret, cur_rdpq_mode;
adam radford9c915a82010-12-21 13:34:31 -0800764 struct fusion_context *fusion;
adam radfordc77a9bd2014-03-10 02:51:36 -0700765 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
adam radford9c915a82010-12-21 13:34:31 -0800766 int i;
767 struct megasas_header *frame_hdr;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +0530768 const char *sys_info;
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530769 MFI_CAPABILITIES *drv_ops;
Sumit Saxena179ac142016-01-28 21:04:28 +0530770 u32 scratch_pad_2;
Shivasharan S45b8a352017-02-10 00:59:04 -0800771 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -0800772
773 fusion = instance->ctrl_context;
774
775 cmd = megasas_get_cmd(instance);
776
777 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500778 dev_err(&instance->pdev->dev, "Could not allocate cmd for INIT Frame\n");
adam radford9c915a82010-12-21 13:34:31 -0800779 ret = 1;
780 goto fail_get_cmd;
781 }
782
Sumit Saxena179ac142016-01-28 21:04:28 +0530783 scratch_pad_2 = readl
784 (&instance->reg_set->outbound_scratch_pad_2);
785
786 cur_rdpq_mode = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ? 1 : 0;
787
788 if (instance->is_rdpq && !cur_rdpq_mode) {
789 dev_err(&instance->pdev->dev, "Firmware downgrade *NOT SUPPORTED*"
790 " from RDPQ mode to non RDPQ mode\n");
791 ret = 1;
792 goto fail_fw_init;
793 }
794
Kashyap Desaid0fc91d2016-10-21 06:33:33 -0700795 instance->fw_sync_cache_support = (scratch_pad_2 &
796 MR_CAN_HANDLE_SYNC_CACHE_OFFSET) ? 1 : 0;
797 dev_info(&instance->pdev->dev, "FW supports sync cache\t: %s\n",
798 instance->fw_sync_cache_support ? "Yes" : "No");
799
adam radford9c915a82010-12-21 13:34:31 -0800800 IOCInitMessage =
801 dma_alloc_coherent(&instance->pdev->dev,
802 sizeof(struct MPI2_IOC_INIT_REQUEST),
803 &ioc_init_handle, GFP_KERNEL);
804
805 if (!IOCInitMessage) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500806 dev_err(&instance->pdev->dev, "Could not allocate memory for "
adam radford9c915a82010-12-21 13:34:31 -0800807 "IOCInitMessage\n");
808 ret = 1;
809 goto fail_fw_init;
810 }
811
812 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
813
814 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
815 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530816 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
817 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
818 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
adam radford9c915a82010-12-21 13:34:31 -0800819
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530820 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
Sumit Saxena179ac142016-01-28 21:04:28 +0530821 IOCInitMessage->ReplyDescriptorPostQueueAddress = instance->is_rdpq ?
822 cpu_to_le64(fusion->rdpq_phys) :
823 cpu_to_le64(fusion->reply_frames_desc_phys[0]);
824 IOCInitMessage->MsgFlags = instance->is_rdpq ?
825 MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE : 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530826 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
adam radford5738f992012-03-19 19:49:53 -0700827 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
Shivasharan S15dd0382017-02-10 00:59:10 -0800828 IOCInitMessage->HostPageSize = MR_DEFAULT_NVME_PAGE_SHIFT;
adam radford9c915a82010-12-21 13:34:31 -0800829 init_frame = (struct megasas_init_frame *)cmd->frame;
830 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
831
832 frame_hdr = &cmd->frame->hdr;
adam radford9c915a82010-12-21 13:34:31 -0800833 frame_hdr->cmd_status = 0xFF;
Christoph Hellwig48433bd2015-04-23 16:33:39 +0530834 frame_hdr->flags = cpu_to_le16(
835 le16_to_cpu(frame_hdr->flags) |
836 MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
adam radford9c915a82010-12-21 13:34:31 -0800837
838 init_frame->cmd = MFI_CMD_INIT;
839 init_frame->cmd_status = 0xFF;
840
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530841 drv_ops = (MFI_CAPABILITIES *) &(init_frame->driver_operations);
842
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530843 /* driver support Extended MSIX */
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -0500844 if (fusion->adapter_type >= INVADER_SERIES)
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530845 drv_ops->mfi_capabilities.support_additional_msix = 1;
adam radford21c9e162013-09-06 15:27:14 -0700846 /* driver supports HA / Remote LUN over Fast Path interface */
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530847 drv_ops->mfi_capabilities.support_fp_remote_lun = 1;
848
849 drv_ops->mfi_capabilities.support_max_255lds = 1;
850 drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1;
851 drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1;
852
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +0530853 if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
854 drv_ops->mfi_capabilities.support_ext_io_size = 1;
855
Sumit Saxena8f050242016-01-28 21:04:27 +0530856 drv_ops->mfi_capabilities.support_fp_rlbypass = 1;
Sumit Saxena308ec452016-01-28 21:04:30 +0530857 if (!dual_qdepth_disable)
858 drv_ops->mfi_capabilities.support_ext_queue_depth = 1;
Sumit Saxena8f050242016-01-28 21:04:27 +0530859
Sumit Saxena52b62ac2016-01-28 21:04:31 +0530860 drv_ops->mfi_capabilities.support_qd_throttling = 1;
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -0500861 drv_ops->mfi_capabilities.support_pd_map_target_id = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530862 /* Convert capability to LE32 */
863 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
adam radford21c9e162013-09-06 15:27:14 -0700864
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +0530865 sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
866 if (instance->system_info_buf && sys_info) {
867 memcpy(instance->system_info_buf->systemId, sys_info,
868 strlen(sys_info) > 64 ? 64 : strlen(sys_info));
869 instance->system_info_buf->systemIdLength =
870 strlen(sys_info) > 64 ? 64 : strlen(sys_info);
871 init_frame->system_info_lo = instance->system_info_h;
872 init_frame->system_info_hi = 0;
873 }
874
Hannes Reineckefdc5a972014-01-16 11:25:33 +0100875 init_frame->queue_info_new_phys_addr_hi =
876 cpu_to_le32(upper_32_bits(ioc_init_handle));
877 init_frame->queue_info_new_phys_addr_lo =
878 cpu_to_le32(lower_32_bits(ioc_init_handle));
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530879 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
adam radford9c915a82010-12-21 13:34:31 -0800880
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +0530881 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
882 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
adam radfordc77a9bd2014-03-10 02:51:36 -0700883 req_desc.MFAIo.RequestFlags =
adam radford9c915a82010-12-21 13:34:31 -0800884 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +0530885 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford9c915a82010-12-21 13:34:31 -0800886
887 /*
888 * disable the intr before firing the init frame
889 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530890 instance->instancet->disable_intr(instance);
adam radford9c915a82010-12-21 13:34:31 -0800891
892 for (i = 0; i < (10 * 1000); i += 20) {
893 if (readl(&instance->reg_set->doorbell) & 1)
894 msleep(20);
895 else
896 break;
897 }
898
Shivasharan S45b8a352017-02-10 00:59:04 -0800899 /* For Ventura also IOC INIT required 64 bit Descriptor write. */
900 spin_lock_irqsave(&instance->hba_lock, flags);
901 writel(le32_to_cpu(req_desc.u.low),
902 &instance->reg_set->inbound_low_queue_port);
903 writel(le32_to_cpu(req_desc.u.high),
904 &instance->reg_set->inbound_high_queue_port);
905 mmiowb();
906 spin_unlock_irqrestore(&instance->hba_lock, flags);
adam radford9c915a82010-12-21 13:34:31 -0800907
adam radford229fe472014-03-10 02:51:56 -0700908 wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -0800909
910 frame_hdr = &cmd->frame->hdr;
911 if (frame_hdr->cmd_status != 0) {
912 ret = 1;
913 goto fail_fw_init;
914 }
Andy Lutomirski13f30772016-05-03 10:24:31 -0700915 dev_info(&instance->pdev->dev, "Init cmd success\n");
adam radford9c915a82010-12-21 13:34:31 -0800916
917 ret = 0;
918
919fail_fw_init:
920 megasas_return_cmd(instance, cmd);
921 if (IOCInitMessage)
922 dma_free_coherent(&instance->pdev->dev,
923 sizeof(struct MPI2_IOC_INIT_REQUEST),
924 IOCInitMessage, ioc_init_handle);
925fail_get_cmd:
926 return ret;
927}
928
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +0530929/**
930 * megasas_sync_pd_seq_num - JBOD SEQ MAP
931 * @instance: Adapter soft state
932 * @pend: set to 1, if it is pended jbod map.
933 *
934 * Issue Jbod map to the firmware. If it is pended command,
935 * issue command and return. If it is first instance of jbod map
936 * issue and receive command.
937 */
938int
939megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) {
940 int ret = 0;
941 u32 pd_seq_map_sz;
942 struct megasas_cmd *cmd;
943 struct megasas_dcmd_frame *dcmd;
944 struct fusion_context *fusion = instance->ctrl_context;
945 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
946 dma_addr_t pd_seq_h;
947
948 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)];
949 pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)];
950 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
951 (sizeof(struct MR_PD_CFG_SEQ) *
952 (MAX_PHYSICAL_DEVICES - 1));
953
954 cmd = megasas_get_cmd(instance);
955 if (!cmd) {
956 dev_err(&instance->pdev->dev,
957 "Could not get mfi cmd. Fail from %s %d\n",
958 __func__, __LINE__);
959 return -ENOMEM;
960 }
961
962 dcmd = &cmd->frame->dcmd;
963
964 memset(pd_sync, 0, pd_seq_map_sz);
965 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
966 dcmd->cmd = MFI_CMD_DCMD;
967 dcmd->cmd_status = 0xFF;
968 dcmd->sge_count = 1;
969 dcmd->timeout = 0;
970 dcmd->pad_0 = 0;
971 dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz);
972 dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
973 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(pd_seq_h);
974 dcmd->sgl.sge32[0].length = cpu_to_le32(pd_seq_map_sz);
975
976 if (pend) {
977 dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG;
978 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
979 instance->jbod_seq_cmd = cmd;
980 instance->instancet->issue_dcmd(instance, cmd);
981 return 0;
982 }
983
984 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
985
986 /* Below code is only for non pended DCMD */
987 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530988 ret = megasas_issue_blocked_cmd(instance, cmd,
989 MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +0530990 else
991 ret = megasas_issue_polled(instance, cmd);
992
993 if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
994 dev_warn(&instance->pdev->dev,
995 "driver supports max %d JBOD, but FW reports %d\n",
996 MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count));
997 ret = -EINVAL;
998 }
999
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301000 if (ret == DCMD_TIMEOUT && instance->ctrl_context)
1001 megaraid_sas_kill_hba(instance);
1002
1003 if (ret == DCMD_SUCCESS)
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301004 instance->pd_seq_map_id++;
1005
1006 megasas_return_cmd(instance, cmd);
1007 return ret;
1008}
1009
adam radford9c915a82010-12-21 13:34:31 -08001010/*
adam radford9c915a82010-12-21 13:34:31 -08001011 * megasas_get_ld_map_info - Returns FW's ld_map structure
1012 * @instance: Adapter soft state
1013 * @pend: Pend the command or not
1014 * Issues an internal command (DCMD) to get the FW's controller PD
1015 * list structure. This information is mainly used to find out SYSTEM
1016 * supported by the FW.
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301017 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
1018 * dcmd.mbox.b[0] - number of LDs being sync'd
1019 * dcmd.mbox.b[1] - 0 - complete command immediately.
1020 * - 1 - pend till config change
1021 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
1022 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
1023 * uses extended struct MR_FW_RAID_MAP_EXT
adam radford9c915a82010-12-21 13:34:31 -08001024 */
1025static int
1026megasas_get_ld_map_info(struct megasas_instance *instance)
1027{
1028 int ret = 0;
1029 struct megasas_cmd *cmd;
1030 struct megasas_dcmd_frame *dcmd;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301031 void *ci;
adam radford9c915a82010-12-21 13:34:31 -08001032 dma_addr_t ci_h = 0;
1033 u32 size_map_info;
1034 struct fusion_context *fusion;
1035
1036 cmd = megasas_get_cmd(instance);
1037
1038 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001039 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n");
adam radford9c915a82010-12-21 13:34:31 -08001040 return -ENOMEM;
1041 }
1042
1043 fusion = instance->ctrl_context;
1044
1045 if (!fusion) {
1046 megasas_return_cmd(instance, cmd);
Hannes Reinecke2f8bdfa2014-01-16 11:25:35 +01001047 return -ENXIO;
adam radford9c915a82010-12-21 13:34:31 -08001048 }
1049
1050 dcmd = &cmd->frame->dcmd;
1051
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301052 size_map_info = fusion->current_map_sz;
adam radford9c915a82010-12-21 13:34:31 -08001053
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301054 ci = (void *) fusion->ld_map[(instance->map_id & 1)];
adam radford9c915a82010-12-21 13:34:31 -08001055 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
1056
1057 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001058 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n");
adam radford9c915a82010-12-21 13:34:31 -08001059 megasas_return_cmd(instance, cmd);
1060 return -ENOMEM;
1061 }
1062
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301063 memset(ci, 0, fusion->max_map_sz);
adam radford9c915a82010-12-21 13:34:31 -08001064 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301065#if VD_EXT_DEBUG
1066 dev_dbg(&instance->pdev->dev,
1067 "%s sending MR_DCMD_LD_MAP_GET_INFO with size %d\n",
1068 __func__, cpu_to_le32(size_map_info));
1069#endif
adam radford9c915a82010-12-21 13:34:31 -08001070 dcmd->cmd = MFI_CMD_DCMD;
1071 dcmd->cmd_status = 0xFF;
1072 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301073 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford9c915a82010-12-21 13:34:31 -08001074 dcmd->timeout = 0;
1075 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301076 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1077 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1078 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
1079 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -08001080
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301081 if (instance->ctrl_context && !instance->mask_interrupts)
1082 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301083 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301084 else
1085 ret = megasas_issue_polled(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08001086
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301087 if (ret == DCMD_TIMEOUT && instance->ctrl_context)
1088 megaraid_sas_kill_hba(instance);
1089
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05301090 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08001091
1092 return ret;
1093}
1094
1095u8
1096megasas_get_map_info(struct megasas_instance *instance)
1097{
1098 struct fusion_context *fusion = instance->ctrl_context;
1099
1100 fusion->fast_path_io = 0;
1101 if (!megasas_get_ld_map_info(instance)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05301102 if (MR_ValidateMapInfo(instance)) {
adam radford9c915a82010-12-21 13:34:31 -08001103 fusion->fast_path_io = 1;
1104 return 0;
1105 }
1106 }
1107 return 1;
1108}
1109
1110/*
1111 * megasas_sync_map_info - Returns FW's ld_map structure
1112 * @instance: Adapter soft state
1113 *
1114 * Issues an internal command (DCMD) to get the FW's controller PD
1115 * list structure. This information is mainly used to find out SYSTEM
1116 * supported by the FW.
1117 */
1118int
1119megasas_sync_map_info(struct megasas_instance *instance)
1120{
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001121 int i;
adam radford9c915a82010-12-21 13:34:31 -08001122 struct megasas_cmd *cmd;
1123 struct megasas_dcmd_frame *dcmd;
Shivasharan Sd2d03582017-02-10 00:59:19 -08001124 u16 num_lds;
1125 u32 size_sync_info;
adam radford9c915a82010-12-21 13:34:31 -08001126 struct fusion_context *fusion;
1127 struct MR_LD_TARGET_SYNC *ci = NULL;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301128 struct MR_DRV_RAID_MAP_ALL *map;
adam radford9c915a82010-12-21 13:34:31 -08001129 struct MR_LD_RAID *raid;
1130 struct MR_LD_TARGET_SYNC *ld_sync;
1131 dma_addr_t ci_h = 0;
1132 u32 size_map_info;
1133
1134 cmd = megasas_get_cmd(instance);
1135
1136 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001137 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n");
adam radford9c915a82010-12-21 13:34:31 -08001138 return -ENOMEM;
1139 }
1140
1141 fusion = instance->ctrl_context;
1142
1143 if (!fusion) {
1144 megasas_return_cmd(instance, cmd);
1145 return 1;
1146 }
1147
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301148 map = fusion->ld_drv_map[instance->map_id & 1];
adam radford9c915a82010-12-21 13:34:31 -08001149
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301150 num_lds = le16_to_cpu(map->raidMap.ldCount);
adam radford9c915a82010-12-21 13:34:31 -08001151
1152 dcmd = &cmd->frame->dcmd;
1153
1154 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
1155
1156 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1157
1158 ci = (struct MR_LD_TARGET_SYNC *)
1159 fusion->ld_map[(instance->map_id - 1) & 1];
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301160 memset(ci, 0, fusion->max_map_sz);
adam radford9c915a82010-12-21 13:34:31 -08001161
1162 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
1163
1164 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
1165
1166 for (i = 0; i < num_lds; i++, ld_sync++) {
1167 raid = MR_LdRaidGet(i, map);
1168 ld_sync->targetId = MR_GetLDTgtId(i, map);
1169 ld_sync->seqNum = raid->seqNum;
1170 }
1171
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301172 size_map_info = fusion->current_map_sz;
adam radford9c915a82010-12-21 13:34:31 -08001173
1174 dcmd->cmd = MFI_CMD_DCMD;
1175 dcmd->cmd_status = 0xFF;
1176 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301177 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
adam radford9c915a82010-12-21 13:34:31 -08001178 dcmd->timeout = 0;
1179 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301180 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -08001181 dcmd->mbox.b[0] = num_lds;
1182 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301183 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1184 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
1185 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -08001186
1187 instance->map_update_cmd = cmd;
1188
1189 instance->instancet->issue_dcmd(instance, cmd);
1190
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001191 return 0;
adam radford9c915a82010-12-21 13:34:31 -08001192}
1193
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301194/*
1195 * meagasas_display_intel_branding - Display branding string
1196 * @instance: per adapter object
1197 *
1198 * Return nothing.
1199 */
1200static void
1201megasas_display_intel_branding(struct megasas_instance *instance)
1202{
1203 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
1204 return;
1205
1206 switch (instance->pdev->device) {
1207 case PCI_DEVICE_ID_LSI_INVADER:
1208 switch (instance->pdev->subsystem_device) {
1209 case MEGARAID_INTEL_RS3DC080_SSDID:
1210 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1211 instance->host->host_no,
1212 MEGARAID_INTEL_RS3DC080_BRANDING);
1213 break;
1214 case MEGARAID_INTEL_RS3DC040_SSDID:
1215 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1216 instance->host->host_no,
1217 MEGARAID_INTEL_RS3DC040_BRANDING);
1218 break;
1219 case MEGARAID_INTEL_RS3SC008_SSDID:
1220 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1221 instance->host->host_no,
1222 MEGARAID_INTEL_RS3SC008_BRANDING);
1223 break;
1224 case MEGARAID_INTEL_RS3MC044_SSDID:
1225 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1226 instance->host->host_no,
1227 MEGARAID_INTEL_RS3MC044_BRANDING);
1228 break;
1229 default:
1230 break;
1231 }
1232 break;
1233 case PCI_DEVICE_ID_LSI_FURY:
1234 switch (instance->pdev->subsystem_device) {
1235 case MEGARAID_INTEL_RS3WC080_SSDID:
1236 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1237 instance->host->host_no,
1238 MEGARAID_INTEL_RS3WC080_BRANDING);
1239 break;
1240 case MEGARAID_INTEL_RS3WC040_SSDID:
1241 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1242 instance->host->host_no,
1243 MEGARAID_INTEL_RS3WC040_BRANDING);
1244 break;
1245 default:
1246 break;
1247 }
1248 break;
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +05301249 case PCI_DEVICE_ID_LSI_CUTLASS_52:
1250 case PCI_DEVICE_ID_LSI_CUTLASS_53:
1251 switch (instance->pdev->subsystem_device) {
1252 case MEGARAID_INTEL_RMS3BC160_SSDID:
1253 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1254 instance->host->host_no,
1255 MEGARAID_INTEL_RMS3BC160_BRANDING);
1256 break;
1257 default:
1258 break;
1259 }
1260 break;
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301261 default:
1262 break;
1263 }
1264}
1265
adam radford9c915a82010-12-21 13:34:31 -08001266/**
1267 * megasas_init_adapter_fusion - Initializes the FW
1268 * @instance: Adapter soft state
1269 *
1270 * This is the main function for initializing firmware.
1271 */
1272u32
1273megasas_init_adapter_fusion(struct megasas_instance *instance)
1274{
1275 struct megasas_register_set __iomem *reg_set;
1276 struct fusion_context *fusion;
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301277 u32 max_cmd, scratch_pad_2;
adam radfordc8e858f2011-10-08 18:15:13 -07001278 int i = 0, count;
adam radford9c915a82010-12-21 13:34:31 -08001279
1280 fusion = instance->ctrl_context;
1281
1282 reg_set = instance->reg_set;
1283
Sumit Saxena308ec452016-01-28 21:04:30 +05301284 megasas_fusion_update_can_queue(instance, PROBE_CONTEXT);
adam radford9c915a82010-12-21 13:34:31 -08001285
1286 /*
1287 * Reduce the max supported cmds by 1. This is to ensure that the
1288 * reply_q_sz (1 more than the max cmd that driver may send)
1289 * does not exceed max cmds that the FW can support
1290 */
1291 instance->max_fw_cmds = instance->max_fw_cmds-1;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05301292
1293 /*
1294 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
1295 */
1296 instance->max_mfi_cmds =
1297 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
adam radford9c915a82010-12-21 13:34:31 -08001298
1299 max_cmd = instance->max_fw_cmds;
1300
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05301301 fusion->reply_q_depth = 2 * (((max_cmd + 1 + 15)/16)*16);
adam radford9c915a82010-12-21 13:34:31 -08001302
1303 fusion->request_alloc_sz =
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001304 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * instance->max_mpt_cmds;
adam radford9c915a82010-12-21 13:34:31 -08001305 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
1306 *(fusion->reply_q_depth);
1307 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001308 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1309 * (instance->max_mpt_cmds + 1)); /* Extra 1 for SMID 0 */
adam radford9c915a82010-12-21 13:34:31 -08001310
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301311 scratch_pad_2 = readl(&instance->reg_set->outbound_scratch_pad_2);
1312 /* If scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
1313 * Firmware support extended IO chain frame which is 4 times more than
1314 * legacy Firmware.
1315 * Legacy Firmware - Frame size is (8 * 128) = 1K
1316 * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
1317 */
1318 if (scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
1319 instance->max_chain_frame_sz =
1320 ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1321 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
1322 else
1323 instance->max_chain_frame_sz =
1324 ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1325 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;
1326
1327 if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
1328 dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
1329 instance->max_chain_frame_sz,
1330 MEGASAS_CHAIN_FRAME_SZ_MIN);
1331 instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
1332 }
1333
adam radford9c915a82010-12-21 13:34:31 -08001334 fusion->max_sge_in_main_msg =
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301335 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1336 - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
adam radford9c915a82010-12-21 13:34:31 -08001337
1338 fusion->max_sge_in_chain =
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301339 instance->max_chain_frame_sz
1340 / sizeof(union MPI2_SGE_IO_UNION);
adam radford9c915a82010-12-21 13:34:31 -08001341
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301342 instance->max_num_sge =
1343 rounddown_pow_of_two(fusion->max_sge_in_main_msg
1344 + fusion->max_sge_in_chain - 2);
adam radford9c915a82010-12-21 13:34:31 -08001345
1346 /* Used for pass thru MFI frame (DCMD) */
1347 fusion->chain_offset_mfi_pthru =
1348 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1349
1350 fusion->chain_offset_io_request =
1351 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1352 sizeof(union MPI2_SGE_IO_UNION))/16;
1353
adam radfordc8e858f2011-10-08 18:15:13 -07001354 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1355 for (i = 0 ; i < count; i++)
1356 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -08001357
1358 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05301359 * For fusion adapters, 3 commands for IOCTL and 5 commands
1360 * for driver's internal DCMDs.
1361 */
1362 instance->max_scsi_cmds = instance->max_fw_cmds -
1363 (MEGASAS_FUSION_INTERNAL_CMDS +
1364 MEGASAS_FUSION_IOCTL_CMDS);
1365 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1366
1367 /*
adam radford9c915a82010-12-21 13:34:31 -08001368 * Allocate memory for descriptors
1369 * Create a pool of commands
1370 */
1371 if (megasas_alloc_cmds(instance))
1372 goto fail_alloc_mfi_cmds;
1373 if (megasas_alloc_cmds_fusion(instance))
1374 goto fail_alloc_cmds;
1375
1376 if (megasas_ioc_init_fusion(instance))
1377 goto fail_ioc_init;
1378
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301379 megasas_display_intel_branding(instance);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05301380 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301381 dev_err(&instance->pdev->dev,
1382 "Could not get controller info. Fail from %s %d\n",
1383 __func__, __LINE__);
1384 goto fail_ioc_init;
1385 }
1386
adam radford9c915a82010-12-21 13:34:31 -08001387 instance->flag_ieee = 1;
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08001388 instance->r1_ldio_hint_default = MR_R1_LDIO_PIGGYBACK_DEFAULT;
adam radford9c915a82010-12-21 13:34:31 -08001389 fusion->fast_path_io = 0;
1390
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301391 fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1392 for (i = 0; i < 2; i++) {
1393 fusion->ld_map[i] = NULL;
1394 fusion->ld_drv_map[i] = (void *)__get_free_pages(GFP_KERNEL,
1395 fusion->drv_map_pages);
1396 if (!fusion->ld_drv_map[i]) {
1397 dev_err(&instance->pdev->dev, "Could not allocate "
1398 "memory for local map info for %d pages\n",
1399 fusion->drv_map_pages);
1400 if (i == 1)
1401 free_pages((ulong)fusion->ld_drv_map[0],
1402 fusion->drv_map_pages);
1403 goto fail_ioc_init;
1404 }
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05301405 memset(fusion->ld_drv_map[i], 0,
1406 ((1 << PAGE_SHIFT) << fusion->drv_map_pages));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301407 }
1408
adam radford9c915a82010-12-21 13:34:31 -08001409 for (i = 0; i < 2; i++) {
1410 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301411 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08001412 &fusion->ld_map_phys[i],
1413 GFP_KERNEL);
1414 if (!fusion->ld_map[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001415 dev_err(&instance->pdev->dev, "Could not allocate memory "
adam radford9c915a82010-12-21 13:34:31 -08001416 "for map info\n");
1417 goto fail_map_info;
1418 }
1419 }
1420
1421 if (!megasas_get_map_info(instance))
1422 megasas_sync_map_info(instance);
1423
1424 return 0;
1425
adam radford9c915a82010-12-21 13:34:31 -08001426fail_map_info:
1427 if (i == 1)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301428 dma_free_coherent(&instance->pdev->dev, fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08001429 fusion->ld_map[0], fusion->ld_map_phys[0]);
1430fail_ioc_init:
adam radfordeb1b1232011-02-24 20:55:56 -08001431 megasas_free_cmds_fusion(instance);
1432fail_alloc_cmds:
1433 megasas_free_cmds(instance);
1434fail_alloc_mfi_cmds:
adam radford9c915a82010-12-21 13:34:31 -08001435 return 1;
1436}
1437
1438/**
adam radford9c915a82010-12-21 13:34:31 -08001439 * map_cmd_status - Maps FW cmd status to OS cmd status
1440 * @cmd : Pointer to cmd
1441 * @status : status of cmd returned by FW
1442 * @ext_status : ext status of cmd returned by FW
1443 */
1444
1445void
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001446map_cmd_status(struct fusion_context *fusion,
1447 struct scsi_cmnd *scmd, u8 status, u8 ext_status,
1448 u32 data_length, u8 *sense)
adam radford9c915a82010-12-21 13:34:31 -08001449{
Shivasharan Sdef0eab2017-02-10 00:59:15 -08001450 u8 cmd_type;
Shivasharan S31d9a572017-02-10 00:59:14 -08001451 int resid;
adam radford9c915a82010-12-21 13:34:31 -08001452
Shivasharan Sdef0eab2017-02-10 00:59:15 -08001453 cmd_type = megasas_cmd_type(scmd);
adam radford9c915a82010-12-21 13:34:31 -08001454 switch (status) {
1455
1456 case MFI_STAT_OK:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001457 scmd->result = DID_OK << 16;
adam radford9c915a82010-12-21 13:34:31 -08001458 break;
1459
1460 case MFI_STAT_SCSI_IO_FAILED:
1461 case MFI_STAT_LD_INIT_IN_PROGRESS:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001462 scmd->result = (DID_ERROR << 16) | ext_status;
adam radford9c915a82010-12-21 13:34:31 -08001463 break;
1464
1465 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1466
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001467 scmd->result = (DID_OK << 16) | ext_status;
adam radford9c915a82010-12-21 13:34:31 -08001468 if (ext_status == SAM_STAT_CHECK_CONDITION) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001469 memset(scmd->sense_buffer, 0,
adam radford9c915a82010-12-21 13:34:31 -08001470 SCSI_SENSE_BUFFERSIZE);
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001471 memcpy(scmd->sense_buffer, sense,
adam radford9c915a82010-12-21 13:34:31 -08001472 SCSI_SENSE_BUFFERSIZE);
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001473 scmd->result |= DRIVER_SENSE << 24;
adam radford9c915a82010-12-21 13:34:31 -08001474 }
Shivasharan S31d9a572017-02-10 00:59:14 -08001475
1476 /*
1477 * If the IO request is partially completed, then MR FW will
1478 * update "io_request->DataLength" field with actual number of
1479 * bytes transferred.Driver will set residual bytes count in
1480 * SCSI command structure.
1481 */
1482 resid = (scsi_bufflen(scmd) - data_length);
1483 scsi_set_resid(scmd, resid);
Shivasharan Sdef0eab2017-02-10 00:59:15 -08001484
1485 if (resid &&
1486 ((cmd_type == READ_WRITE_LDIO) ||
1487 (cmd_type == READ_WRITE_SYSPDIO)))
1488 scmd_printk(KERN_INFO, scmd, "BRCM Debug mfi stat 0x%x, data len"
1489 " requested/completed 0x%x/0x%x\n",
1490 status, scsi_bufflen(scmd), data_length);
adam radford9c915a82010-12-21 13:34:31 -08001491 break;
1492
1493 case MFI_STAT_LD_OFFLINE:
1494 case MFI_STAT_DEVICE_NOT_FOUND:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001495 scmd->result = DID_BAD_TARGET << 16;
adam radford9c915a82010-12-21 13:34:31 -08001496 break;
adam radford36807e62011-10-08 18:15:06 -07001497 case MFI_STAT_CONFIG_SEQ_MISMATCH:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001498 scmd->result = DID_IMM_RETRY << 16;
adam radford36807e62011-10-08 18:15:06 -07001499 break;
adam radford9c915a82010-12-21 13:34:31 -08001500 default:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001501 scmd->result = DID_ERROR << 16;
adam radford9c915a82010-12-21 13:34:31 -08001502 break;
1503 }
1504}
1505
1506/**
Shivasharan S33203bc2017-02-10 00:59:12 -08001507 * megasas_is_prp_possible -
1508 * Checks if native NVMe PRPs can be built for the IO
1509 *
1510 * @instance: Adapter soft state
1511 * @scmd: SCSI command from the mid-layer
1512 * @sge_count: scatter gather element count.
1513 *
1514 * Returns: true: PRPs can be built
1515 * false: IEEE SGLs needs to be built
1516 */
1517static bool
1518megasas_is_prp_possible(struct megasas_instance *instance,
1519 struct scsi_cmnd *scmd, int sge_count)
1520{
1521 struct fusion_context *fusion;
1522 int i;
1523 u32 data_length = 0;
1524 struct scatterlist *sg_scmd;
1525 bool build_prp = false;
1526 u32 mr_nvme_pg_size;
1527
1528 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1529 MR_DEFAULT_NVME_PAGE_SIZE);
1530 fusion = instance->ctrl_context;
1531 data_length = scsi_bufflen(scmd);
1532 sg_scmd = scsi_sglist(scmd);
1533
1534 /*
1535 * NVMe uses one PRP for each page (or part of a page)
1536 * look at the data length - if 4 pages or less then IEEE is OK
1537 * if > 5 pages then we need to build a native SGL
1538 * if > 4 and <= 5 pages, then check physical address of 1st SG entry
1539 * if this first size in the page is >= the residual beyond 4 pages
1540 * then use IEEE, otherwise use native SGL
1541 */
1542
1543 if (data_length > (mr_nvme_pg_size * 5)) {
1544 build_prp = true;
1545 } else if ((data_length > (mr_nvme_pg_size * 4)) &&
1546 (data_length <= (mr_nvme_pg_size * 5))) {
1547 /* check if 1st SG entry size is < residual beyond 4 pages */
1548 if (sg_dma_len(sg_scmd) < (data_length - (mr_nvme_pg_size * 4)))
1549 build_prp = true;
1550 }
1551
1552/*
1553 * Below code detects gaps/holes in IO data buffers.
1554 * What does holes/gaps mean?
1555 * Any SGE except first one in a SGL starts at non NVME page size
1556 * aligned address OR Any SGE except last one in a SGL ends at
1557 * non NVME page size boundary.
1558 *
1559 * Driver has already informed block layer by setting boundary rules for
1560 * bio merging done at NVME page size boundary calling kernel API
1561 * blk_queue_virt_boundary inside slave_config.
1562 * Still there is possibility of IO coming with holes to driver because of
1563 * IO merging done by IO scheduler.
1564 *
1565 * With SCSI BLK MQ enabled, there will be no IO with holes as there is no
1566 * IO scheduling so no IO merging.
1567 *
1568 * With SCSI BLK MQ disabled, IO scheduler may attempt to merge IOs and
1569 * then sending IOs with holes.
1570 *
1571 * Though driver can request block layer to disable IO merging by calling-
1572 * queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, sdev->request_queue) but
1573 * user may tune sysfs parameter- nomerges again to 0 or 1.
1574 *
1575 * If in future IO scheduling is enabled with SCSI BLK MQ,
1576 * this algorithm to detect holes will be required in driver
1577 * for SCSI BLK MQ enabled case as well.
1578 *
1579 *
1580 */
1581 scsi_for_each_sg(scmd, sg_scmd, sge_count, i) {
1582 if ((i != 0) && (i != (sge_count - 1))) {
1583 if (mega_mod64(sg_dma_len(sg_scmd), mr_nvme_pg_size) ||
1584 mega_mod64(sg_dma_address(sg_scmd),
1585 mr_nvme_pg_size)) {
1586 build_prp = false;
1587 atomic_inc(&instance->sge_holes_type1);
1588 break;
1589 }
1590 }
1591
1592 if ((sge_count > 1) && (i == 0)) {
1593 if ((mega_mod64((sg_dma_address(sg_scmd) +
1594 sg_dma_len(sg_scmd)),
1595 mr_nvme_pg_size))) {
1596 build_prp = false;
1597 atomic_inc(&instance->sge_holes_type2);
1598 break;
1599 }
1600 }
1601
1602 if ((sge_count > 1) && (i == (sge_count - 1))) {
1603 if (mega_mod64(sg_dma_address(sg_scmd),
1604 mr_nvme_pg_size)) {
1605 build_prp = false;
1606 atomic_inc(&instance->sge_holes_type3);
1607 break;
1608 }
1609 }
1610 }
1611
1612 return build_prp;
1613}
1614
1615/**
1616 * megasas_make_prp_nvme -
1617 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
1618 *
1619 * @instance: Adapter soft state
1620 * @scmd: SCSI command from the mid-layer
1621 * @sgl_ptr: SGL to be filled in
1622 * @cmd: Fusion command frame
1623 * @sge_count: scatter gather element count.
1624 *
1625 * Returns: true: PRPs are built
1626 * false: IEEE SGLs needs to be built
1627 */
1628static bool
1629megasas_make_prp_nvme(struct megasas_instance *instance, struct scsi_cmnd *scmd,
1630 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1631 struct megasas_cmd_fusion *cmd, int sge_count)
1632{
1633 int sge_len, offset, num_prp_in_chain = 0;
1634 struct MPI25_IEEE_SGE_CHAIN64 *main_chain_element, *ptr_first_sgl;
1635 u64 *ptr_sgl, *ptr_sgl_phys;
1636 u64 sge_addr;
1637 u32 page_mask, page_mask_result;
1638 struct scatterlist *sg_scmd;
1639 u32 first_prp_len;
1640 bool build_prp = false;
1641 int data_len = scsi_bufflen(scmd);
1642 struct fusion_context *fusion;
1643 u32 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1644 MR_DEFAULT_NVME_PAGE_SIZE);
1645
1646 fusion = instance->ctrl_context;
1647
1648 build_prp = megasas_is_prp_possible(instance, scmd, sge_count);
1649
1650 if (!build_prp)
1651 return false;
1652
1653 /*
1654 * Nvme has a very convoluted prp format. One prp is required
1655 * for each page or partial page. Driver need to split up OS sg_list
1656 * entries if it is longer than one page or cross a page
1657 * boundary. Driver also have to insert a PRP list pointer entry as
1658 * the last entry in each physical page of the PRP list.
1659 *
1660 * NOTE: The first PRP "entry" is actually placed in the first
1661 * SGL entry in the main message as IEEE 64 format. The 2nd
1662 * entry in the main message is the chain element, and the rest
1663 * of the PRP entries are built in the contiguous pcie buffer.
1664 */
1665 page_mask = mr_nvme_pg_size - 1;
1666 ptr_sgl = (u64 *)cmd->sg_frame;
1667 ptr_sgl_phys = (u64 *)cmd->sg_frame_phys_addr;
1668 memset(ptr_sgl, 0, instance->max_chain_frame_sz);
1669
1670 /* Build chain frame element which holds all prps except first*/
1671 main_chain_element = (struct MPI25_IEEE_SGE_CHAIN64 *)
1672 ((u8 *)sgl_ptr + sizeof(struct MPI25_IEEE_SGE_CHAIN64));
1673
1674 main_chain_element->Address = cpu_to_le64((uintptr_t)ptr_sgl_phys);
1675 main_chain_element->NextChainOffset = 0;
1676 main_chain_element->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1677 IEEE_SGE_FLAGS_SYSTEM_ADDR |
1678 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
1679
1680 /* Build first prp, sge need not to be page aligned*/
1681 ptr_first_sgl = sgl_ptr;
1682 sg_scmd = scsi_sglist(scmd);
1683 sge_addr = sg_dma_address(sg_scmd);
1684 sge_len = sg_dma_len(sg_scmd);
1685
1686 offset = (u32)(sge_addr & page_mask);
1687 first_prp_len = mr_nvme_pg_size - offset;
1688
1689 ptr_first_sgl->Address = cpu_to_le64(sge_addr);
1690 ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
1691
1692 data_len -= first_prp_len;
1693
1694 if (sge_len > first_prp_len) {
1695 sge_addr += first_prp_len;
1696 sge_len -= first_prp_len;
1697 } else if (sge_len == first_prp_len) {
1698 sg_scmd = sg_next(sg_scmd);
1699 sge_addr = sg_dma_address(sg_scmd);
1700 sge_len = sg_dma_len(sg_scmd);
1701 }
1702
1703 for (;;) {
1704 offset = (u32)(sge_addr & page_mask);
1705
1706 /* Put PRP pointer due to page boundary*/
1707 page_mask_result = (uintptr_t)(ptr_sgl + 1) & page_mask;
1708 if (unlikely(!page_mask_result)) {
1709 scmd_printk(KERN_NOTICE,
1710 scmd, "page boundary ptr_sgl: 0x%p\n",
1711 ptr_sgl);
1712 ptr_sgl_phys++;
1713 *ptr_sgl =
1714 cpu_to_le64((uintptr_t)ptr_sgl_phys);
1715 ptr_sgl++;
1716 num_prp_in_chain++;
1717 }
1718
1719 *ptr_sgl = cpu_to_le64(sge_addr);
1720 ptr_sgl++;
1721 ptr_sgl_phys++;
1722 num_prp_in_chain++;
1723
1724 sge_addr += mr_nvme_pg_size;
1725 sge_len -= mr_nvme_pg_size;
1726 data_len -= mr_nvme_pg_size;
1727
1728 if (data_len <= 0)
1729 break;
1730
1731 if (sge_len > 0)
1732 continue;
1733
1734 sg_scmd = sg_next(sg_scmd);
1735 sge_addr = sg_dma_address(sg_scmd);
1736 sge_len = sg_dma_len(sg_scmd);
1737 }
1738
1739 main_chain_element->Length =
1740 cpu_to_le32(num_prp_in_chain * sizeof(u64));
1741
1742 atomic_inc(&instance->prp_sgl);
1743 return build_prp;
1744}
1745
1746/**
adam radford9c915a82010-12-21 13:34:31 -08001747 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1748 * @instance: Adapter soft state
1749 * @scp: SCSI command from the mid-layer
1750 * @sgl_ptr: SGL to be filled in
1751 * @cmd: cmd we are working on
Shivasharan S33203bc2017-02-10 00:59:12 -08001752 * @sge_count sge count
adam radford9c915a82010-12-21 13:34:31 -08001753 *
adam radford9c915a82010-12-21 13:34:31 -08001754 */
Shivasharan S33203bc2017-02-10 00:59:12 -08001755static void
adam radford9c915a82010-12-21 13:34:31 -08001756megasas_make_sgl_fusion(struct megasas_instance *instance,
1757 struct scsi_cmnd *scp,
1758 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
Shivasharan S33203bc2017-02-10 00:59:12 -08001759 struct megasas_cmd_fusion *cmd, int sge_count)
adam radford9c915a82010-12-21 13:34:31 -08001760{
Shivasharan S33203bc2017-02-10 00:59:12 -08001761 int i, sg_processed;
adam radford9c915a82010-12-21 13:34:31 -08001762 struct scatterlist *os_sgl;
1763 struct fusion_context *fusion;
1764
1765 fusion = instance->ctrl_context;
1766
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05001767 if (fusion->adapter_type >= INVADER_SERIES) {
adam radford36807e62011-10-08 18:15:06 -07001768 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1769 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1770 sgl_ptr_end->Flags = 0;
1771 }
adam radford9c915a82010-12-21 13:34:31 -08001772
adam radford9c915a82010-12-21 13:34:31 -08001773 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301774 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
1775 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
adam radford9c915a82010-12-21 13:34:31 -08001776 sgl_ptr->Flags = 0;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05001777 if (fusion->adapter_type >= INVADER_SERIES)
adam radford36807e62011-10-08 18:15:06 -07001778 if (i == sge_count - 1)
1779 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
adam radford9c915a82010-12-21 13:34:31 -08001780 sgl_ptr++;
adam radford9c915a82010-12-21 13:34:31 -08001781 sg_processed = i + 1;
1782
1783 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1784 (sge_count > fusion->max_sge_in_main_msg)) {
1785
1786 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05001787 if (fusion->adapter_type >= INVADER_SERIES) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301788 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1789 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1790 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
adam radford36807e62011-10-08 18:15:06 -07001791 cmd->io_request->ChainOffset =
1792 fusion->
1793 chain_offset_io_request;
1794 else
1795 cmd->io_request->ChainOffset = 0;
1796 } else
1797 cmd->io_request->ChainOffset =
1798 fusion->chain_offset_io_request;
1799
adam radford9c915a82010-12-21 13:34:31 -08001800 sg_chain = sgl_ptr;
1801 /* Prepare chain element */
1802 sg_chain->NextChainOffset = 0;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05001803 if (fusion->adapter_type >= INVADER_SERIES)
adam radford36807e62011-10-08 18:15:06 -07001804 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1805 else
1806 sg_chain->Flags =
1807 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1808 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301809 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
1810 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08001811
1812 sgl_ptr =
1813 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301814 memset(sgl_ptr, 0, instance->max_chain_frame_sz);
adam radford9c915a82010-12-21 13:34:31 -08001815 }
1816 }
Shivasharan S33203bc2017-02-10 00:59:12 -08001817 atomic_inc(&instance->ieee_sgl);
1818}
1819
1820/**
1821 * megasas_make_sgl - Build Scatter Gather List(SGLs)
1822 * @scp: SCSI command pointer
1823 * @instance: Soft instance of controller
1824 * @cmd: Fusion command pointer
1825 *
1826 * This function will build sgls based on device type.
1827 * For nvme drives, there is different way of building sgls in nvme native
1828 * format- PRPs(Physical Region Page).
1829 *
1830 * Returns the number of sg lists actually used, zero if the sg lists
1831 * is NULL, or -ENOMEM if the mapping failed
1832 */
1833static
1834int megasas_make_sgl(struct megasas_instance *instance, struct scsi_cmnd *scp,
1835 struct megasas_cmd_fusion *cmd)
1836{
1837 int sge_count;
1838 bool build_prp = false;
1839 struct MPI25_IEEE_SGE_CHAIN64 *sgl_chain64;
1840
1841 sge_count = scsi_dma_map(scp);
1842
1843 if ((sge_count > instance->max_num_sge) || (sge_count <= 0))
1844 return sge_count;
1845
1846 sgl_chain64 = (struct MPI25_IEEE_SGE_CHAIN64 *)&cmd->io_request->SGL;
1847 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1848 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
1849 (cmd->pd_interface == NVME_PD))
1850 build_prp = megasas_make_prp_nvme(instance, scp, sgl_chain64,
1851 cmd, sge_count);
1852
1853 if (!build_prp)
1854 megasas_make_sgl_fusion(instance, scp, sgl_chain64,
1855 cmd, sge_count);
adam radford9c915a82010-12-21 13:34:31 -08001856
1857 return sge_count;
1858}
1859
1860/**
1861 * megasas_set_pd_lba - Sets PD LBA
1862 * @cdb: CDB
1863 * @cdb_len: cdb length
1864 * @start_blk: Start block of IO
1865 *
1866 * Used to set the PD LBA in CDB for FP IOs
1867 */
1868void
1869megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1870 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301871 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
adam radford9c915a82010-12-21 13:34:31 -08001872{
1873 struct MR_LD_RAID *raid;
Shivasharan Sd2d03582017-02-10 00:59:19 -08001874 u16 ld;
adam radford9c915a82010-12-21 13:34:31 -08001875 u64 start_blk = io_info->pdBlock;
1876 u8 *cdb = io_request->CDB.CDB32;
1877 u32 num_blocks = io_info->numBlocks;
adam radford495c5602011-05-11 18:34:40 -07001878 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
adam radford9c915a82010-12-21 13:34:31 -08001879
1880 /* Check if T10 PI (DIF) is enabled for this LD */
1881 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1882 raid = MR_LdRaidGet(ld, local_map_ptr);
1883 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1884 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1885 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1886 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1887
1888 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1889 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1890 else
1891 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1892 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1893
1894 /* LBA */
1895 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1896 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1897 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1898 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1899 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1900 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1901 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1902 cdb[19] = (u8)(start_blk & 0xff);
1903
1904 /* Logical block reference tag */
1905 io_request->CDB.EEDP32.PrimaryReferenceTag =
1906 cpu_to_be32(ref_tag);
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301907 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301908 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001909
1910 /* Transfer length */
1911 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1912 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1913 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1914 cdb[31] = (u8)(num_blocks & 0xff);
1915
1916 /* set SCSI IO EEDPFlags */
1917 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301918 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001919 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1920 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1921 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1922 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
Sasikumar Chandrasekaran45d44602017-01-10 18:20:45 -05001923 MPI25_SCSIIO_EEDPFLAGS_DO_NOT_DISABLE_MODE |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301924 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
adam radford9c915a82010-12-21 13:34:31 -08001925 } else {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301926 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001927 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301928 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
adam radford9c915a82010-12-21 13:34:31 -08001929 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301930 io_request->Control |= cpu_to_le32((0x4 << 26));
1931 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
adam radford9c915a82010-12-21 13:34:31 -08001932 } else {
1933 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1934 if (((cdb_len == 12) || (cdb_len == 16)) &&
1935 (start_blk <= 0xffffffff)) {
1936 if (cdb_len == 16) {
1937 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1938 flagvals = cdb[1];
1939 groupnum = cdb[14];
1940 control = cdb[15];
1941 } else {
1942 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1943 flagvals = cdb[1];
1944 groupnum = cdb[10];
1945 control = cdb[11];
1946 }
1947
1948 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1949
1950 cdb[0] = opcode;
1951 cdb[1] = flagvals;
1952 cdb[6] = groupnum;
1953 cdb[9] = control;
1954
1955 /* Transfer length */
1956 cdb[8] = (u8)(num_blocks & 0xff);
1957 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1958
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301959 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001960 cdb_len = 10;
adam radford495c5602011-05-11 18:34:40 -07001961 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1962 /* Convert to 16 byte CDB for large LBA's */
1963 switch (cdb_len) {
1964 case 6:
1965 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1966 control = cdb[5];
1967 break;
1968 case 10:
1969 opcode =
1970 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1971 flagvals = cdb[1];
1972 groupnum = cdb[6];
1973 control = cdb[9];
1974 break;
1975 case 12:
1976 opcode =
1977 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1978 flagvals = cdb[1];
1979 groupnum = cdb[10];
1980 control = cdb[11];
1981 break;
1982 }
1983
1984 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1985
1986 cdb[0] = opcode;
1987 cdb[1] = flagvals;
1988 cdb[14] = groupnum;
1989 cdb[15] = control;
1990
1991 /* Transfer length */
1992 cdb[13] = (u8)(num_blocks & 0xff);
1993 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1994 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1995 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1996
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301997 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
adam radford495c5602011-05-11 18:34:40 -07001998 cdb_len = 16;
adam radford9c915a82010-12-21 13:34:31 -08001999 }
2000
2001 /* Normal case, just load LBA here */
2002 switch (cdb_len) {
2003 case 6:
2004 {
2005 u8 val = cdb[1] & 0xE0;
2006 cdb[3] = (u8)(start_blk & 0xff);
2007 cdb[2] = (u8)((start_blk >> 8) & 0xff);
2008 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
2009 break;
2010 }
2011 case 10:
2012 cdb[5] = (u8)(start_blk & 0xff);
2013 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2014 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2015 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2016 break;
2017 case 12:
2018 cdb[5] = (u8)(start_blk & 0xff);
2019 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2020 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2021 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2022 break;
2023 case 16:
2024 cdb[9] = (u8)(start_blk & 0xff);
2025 cdb[8] = (u8)((start_blk >> 8) & 0xff);
2026 cdb[7] = (u8)((start_blk >> 16) & 0xff);
2027 cdb[6] = (u8)((start_blk >> 24) & 0xff);
2028 cdb[5] = (u8)((start_blk >> 32) & 0xff);
2029 cdb[4] = (u8)((start_blk >> 40) & 0xff);
2030 cdb[3] = (u8)((start_blk >> 48) & 0xff);
2031 cdb[2] = (u8)((start_blk >> 56) & 0xff);
2032 break;
2033 }
2034 }
2035}
2036
2037/**
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002038 * megasas_stream_detect - stream detection on read and and write IOs
2039 * @instance: Adapter soft state
2040 * @cmd: Command to be prepared
2041 * @io_info: IO Request info
2042 *
2043 */
2044
2045/** stream detection on read and and write IOs */
2046static void megasas_stream_detect(struct megasas_instance *instance,
2047 struct megasas_cmd_fusion *cmd,
2048 struct IO_REQUEST_INFO *io_info)
2049{
2050 struct fusion_context *fusion = instance->ctrl_context;
2051 u32 device_id = io_info->ldTgtId;
2052 struct LD_STREAM_DETECT *current_ld_sd
2053 = fusion->stream_detect_by_ld[device_id];
2054 u32 *track_stream = &current_ld_sd->mru_bit_map, stream_num;
2055 u32 shifted_values, unshifted_values;
2056 u32 index_value_mask, shifted_values_mask;
2057 int i;
2058 bool is_read_ahead = false;
2059 struct STREAM_DETECT *current_sd;
2060 /* find possible stream */
2061 for (i = 0; i < MAX_STREAMS_TRACKED; ++i) {
2062 stream_num =
2063 (*track_stream >> (i * BITS_PER_INDEX_STREAM)) &
2064 STREAM_MASK;
2065 current_sd = &current_ld_sd->stream_track[stream_num];
2066 /* if we found a stream, update the raid
2067 * context and also update the mruBitMap
2068 */
2069 /* boundary condition */
2070 if ((current_sd->next_seq_lba) &&
2071 (io_info->ldStartBlock >= current_sd->next_seq_lba) &&
2072 (io_info->ldStartBlock <= (current_sd->next_seq_lba+32)) &&
2073 (current_sd->is_read == io_info->isRead)) {
2074
2075 if ((io_info->ldStartBlock != current_sd->next_seq_lba)
2076 && ((!io_info->isRead) || (!is_read_ahead)))
2077 /*
2078 * Once the API availible we need to change this.
2079 * At this point we are not allowing any gap
2080 */
2081 continue;
2082
Shivasharan Sa174118b2017-02-10 00:59:21 -08002083 SET_STREAM_DETECTED(cmd->io_request->RaidContext.raid_context_g35);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002084 current_sd->next_seq_lba =
2085 io_info->ldStartBlock + io_info->numBlocks;
2086 /*
2087 * update the mruBitMap LRU
2088 */
2089 shifted_values_mask =
2090 (1 << i * BITS_PER_INDEX_STREAM) - 1;
2091 shifted_values = ((*track_stream & shifted_values_mask)
2092 << BITS_PER_INDEX_STREAM);
2093 index_value_mask =
2094 STREAM_MASK << i * BITS_PER_INDEX_STREAM;
2095 unshifted_values =
2096 *track_stream & ~(shifted_values_mask |
2097 index_value_mask);
2098 *track_stream =
2099 unshifted_values | shifted_values | stream_num;
2100 return;
2101
2102 }
2103
2104 }
2105 /*
2106 * if we did not find any stream, create a new one
2107 * from the least recently used
2108 */
2109 stream_num =
2110 (*track_stream >> ((MAX_STREAMS_TRACKED - 1) * BITS_PER_INDEX_STREAM)) &
2111 STREAM_MASK;
2112 current_sd = &current_ld_sd->stream_track[stream_num];
2113 current_sd->is_read = io_info->isRead;
2114 current_sd->next_seq_lba = io_info->ldStartBlock + io_info->numBlocks;
2115 *track_stream =
2116 (((*track_stream & ZERO_LAST_STREAM) << 4) | stream_num);
2117 return;
2118
2119}
2120
2121/**
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002122 * megasas_set_raidflag_cpu_affinity - This function sets the cpu
2123 * affinity (cpu of the controller) and raid_flags in the raid context
2124 * based on IO type.
2125 *
2126 * @praid_context: IO RAID context
2127 * @raid: LD raid map
2128 * @fp_possible: Is fast path possible?
2129 * @is_read: Is read IO?
2130 *
2131 */
2132static void
2133megasas_set_raidflag_cpu_affinity(union RAID_CONTEXT_UNION *praid_context,
2134 struct MR_LD_RAID *raid, bool fp_possible,
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002135 u8 is_read, u32 scsi_buff_len)
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002136{
2137 u8 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2138 struct RAID_CONTEXT_G35 *rctx_g35;
2139
2140 rctx_g35 = &praid_context->raid_context_g35;
2141 if (fp_possible) {
2142 if (is_read) {
2143 if ((raid->cpuAffinity.pdRead.cpu0) &&
2144 (raid->cpuAffinity.pdRead.cpu1))
2145 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2146 else if (raid->cpuAffinity.pdRead.cpu1)
2147 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2148 } else {
2149 if ((raid->cpuAffinity.pdWrite.cpu0) &&
2150 (raid->cpuAffinity.pdWrite.cpu1))
2151 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2152 else if (raid->cpuAffinity.pdWrite.cpu1)
2153 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2154 /* Fast path cache by pass capable R0/R1 VD */
2155 if ((raid->level <= 1) &&
2156 (raid->capability.fp_cache_bypass_capable)) {
Shivasharan Sa174118b2017-02-10 00:59:21 -08002157 rctx_g35->routing_flags |=
2158 (1 << MR_RAID_CTX_ROUTINGFLAGS_SLD_SHIFT);
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002159 rctx_g35->raid_flags =
2160 (MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS
2161 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2162 }
2163 }
2164 } else {
2165 if (is_read) {
2166 if ((raid->cpuAffinity.ldRead.cpu0) &&
2167 (raid->cpuAffinity.ldRead.cpu1))
2168 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2169 else if (raid->cpuAffinity.ldRead.cpu1)
2170 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2171 } else {
2172 if ((raid->cpuAffinity.ldWrite.cpu0) &&
2173 (raid->cpuAffinity.ldWrite.cpu1))
2174 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2175 else if (raid->cpuAffinity.ldWrite.cpu1)
2176 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2177
Shivasharan Sa174118b2017-02-10 00:59:21 -08002178 if (is_stream_detected(rctx_g35) &&
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002179 (raid->level == 5) &&
2180 (raid->writeMode == MR_RL_WRITE_THROUGH_MODE) &&
2181 (cpu_sel == MR_RAID_CTX_CPUSEL_FCFS))
2182 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2183 }
2184 }
2185
Shivasharan Sa174118b2017-02-10 00:59:21 -08002186 rctx_g35->routing_flags |=
2187 (cpu_sel << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002188
2189 /* Always give priority to MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2190 * vs MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS.
2191 * IO Subtype is not bitmap.
2192 */
2193 if ((raid->level == 1) && (!is_read)) {
2194 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2195 praid_context->raid_context_g35.raid_flags =
2196 (MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2197 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2198 }
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002199}
2200
2201/**
adam radford9c915a82010-12-21 13:34:31 -08002202 * megasas_build_ldio_fusion - Prepares IOs to devices
2203 * @instance: Adapter soft state
2204 * @scp: SCSI command
2205 * @cmd: Command to be prepared
2206 *
2207 * Prepares the io_request and chain elements (sg_frame) for IO
2208 * The IO can be for PD (Fast Path) or LD
2209 */
2210void
2211megasas_build_ldio_fusion(struct megasas_instance *instance,
2212 struct scsi_cmnd *scp,
2213 struct megasas_cmd_fusion *cmd)
2214{
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002215 bool fp_possible;
2216 u16 ld;
2217 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
2218 u32 scsi_buff_len;
adam radford9c915a82010-12-21 13:34:31 -08002219 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2220 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2221 struct IO_REQUEST_INFO io_info;
2222 struct fusion_context *fusion;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302223 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
adam radford21c9e162013-09-06 15:27:14 -07002224 u8 *raidLUN;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002225 unsigned long spinlock_flags;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002226 union RAID_CONTEXT_UNION *praid_context;
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002227 struct MR_LD_RAID *raid = NULL;
2228 struct MR_PRIV_DEVICE *mrdev_priv;
adam radford9c915a82010-12-21 13:34:31 -08002229
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302230 device_id = MEGASAS_DEV_INDEX(scp);
adam radford9c915a82010-12-21 13:34:31 -08002231
2232 fusion = instance->ctrl_context;
2233
2234 io_request = cmd->io_request;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002235 io_request->RaidContext.raid_context.virtual_disk_tgt_id =
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002236 cpu_to_le16(device_id);
2237 io_request->RaidContext.raid_context.status = 0;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002238 io_request->RaidContext.raid_context.ex_status = 0;
adam radford9c915a82010-12-21 13:34:31 -08002239
2240 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
2241
2242 start_lba_lo = 0;
2243 start_lba_hi = 0;
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002244 fp_possible = false;
adam radford9c915a82010-12-21 13:34:31 -08002245
2246 /*
2247 * 6-byte READ(0x08) or WRITE(0x0A) cdb
2248 */
2249 if (scp->cmd_len == 6) {
adam radfordf9eff812012-10-01 19:27:07 -07002250 datalength = (u32) scp->cmnd[4];
adam radford9c915a82010-12-21 13:34:31 -08002251 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
2252 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
2253
2254 start_lba_lo &= 0x1FFFFF;
2255 }
2256
2257 /*
2258 * 10-byte READ(0x28) or WRITE(0x2A) cdb
2259 */
2260 else if (scp->cmd_len == 10) {
adam radfordf9eff812012-10-01 19:27:07 -07002261 datalength = (u32) scp->cmnd[8] |
adam radford9c915a82010-12-21 13:34:31 -08002262 ((u32) scp->cmnd[7] << 8);
2263 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2264 ((u32) scp->cmnd[3] << 16) |
2265 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2266 }
2267
2268 /*
2269 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
2270 */
2271 else if (scp->cmd_len == 12) {
adam radfordf9eff812012-10-01 19:27:07 -07002272 datalength = ((u32) scp->cmnd[6] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08002273 ((u32) scp->cmnd[7] << 16) |
2274 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2275 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2276 ((u32) scp->cmnd[3] << 16) |
2277 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2278 }
2279
2280 /*
2281 * 16-byte READ(0x88) or WRITE(0x8A) cdb
2282 */
2283 else if (scp->cmd_len == 16) {
adam radfordf9eff812012-10-01 19:27:07 -07002284 datalength = ((u32) scp->cmnd[10] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08002285 ((u32) scp->cmnd[11] << 16) |
2286 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
2287 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
2288 ((u32) scp->cmnd[7] << 16) |
2289 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2290
2291 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
2292 ((u32) scp->cmnd[3] << 16) |
2293 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2294 }
2295
2296 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
2297 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
adam radfordf9eff812012-10-01 19:27:07 -07002298 io_info.numBlocks = datalength;
adam radford9c915a82010-12-21 13:34:31 -08002299 io_info.ldTgtId = device_id;
Shivasharan S8bf7c652017-02-10 00:59:03 -08002300 io_info.r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002301 scsi_buff_len = scsi_bufflen(scp);
2302 io_request->DataLength = cpu_to_le32(scsi_buff_len);
adam radford9c915a82010-12-21 13:34:31 -08002303
2304 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
2305 io_info.isRead = 1;
2306
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302307 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002308 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
adam radford9c915a82010-12-21 13:34:31 -08002309
Shivasharan Sd2d03582017-02-10 00:59:19 -08002310 if (ld < instance->fw_supported_vd_count)
2311 raid = MR_LdRaidGet(ld, local_map_ptr);
2312
2313 if (!raid || (!fusion->fast_path_io)) {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002314 io_request->RaidContext.raid_context.reg_lock_flags = 0;
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002315 fp_possible = false;
adam radford9c915a82010-12-21 13:34:31 -08002316 } else {
adam radford36807e62011-10-08 18:15:06 -07002317 if (MR_BuildRaidContext(instance, &io_info,
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002318 &io_request->RaidContext.raid_context,
adam radford21c9e162013-09-06 15:27:14 -07002319 local_map_ptr, &raidLUN))
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002320 fp_possible = (io_info.fpOkForIo > 0) ? true : false;
adam radford9c915a82010-12-21 13:34:31 -08002321 }
2322
Christoph Hellwig16b85282015-04-15 09:44:37 -07002323 /* Use raw_smp_processor_id() for now until cmd->request->cpu is CPU
adam radfordc8e858f2011-10-08 18:15:13 -07002324 id by default, not CPU group id, otherwise all MSI-X queues won't
2325 be utilized */
2326 cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
Christoph Hellwig16b85282015-04-15 09:44:37 -07002327 raw_smp_processor_id() % instance->msix_vectors : 0;
adam radfordc8e858f2011-10-08 18:15:13 -07002328
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002329 praid_context = &io_request->RaidContext;
2330
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002331 if (instance->is_ventura) {
2332 spin_lock_irqsave(&instance->stream_lock, spinlock_flags);
2333 megasas_stream_detect(instance, cmd, &io_info);
2334 spin_unlock_irqrestore(&instance->stream_lock, spinlock_flags);
2335 /* In ventura if stream detected for a read and it is read ahead
2336 * capable make this IO as LDIO
2337 */
Shivasharan Sa174118b2017-02-10 00:59:21 -08002338 if (is_stream_detected(&io_request->RaidContext.raid_context_g35) &&
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002339 io_info.isRead && io_info.ra_capable)
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002340 fp_possible = false;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002341
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002342 /* FP for Optimal raid level 1.
2343 * All large RAID-1 writes (> 32 KiB, both WT and WB modes)
2344 * are built by the driver as LD I/Os.
2345 * All small RAID-1 WT writes (<= 32 KiB) are built as FP I/Os
2346 * (there is never a reason to process these as buffered writes)
2347 * All small RAID-1 WB writes (<= 32 KiB) are built as FP I/Os
2348 * with the SLD bit asserted.
2349 */
Shivasharan S8bf7c652017-02-10 00:59:03 -08002350 if (io_info.r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002351 mrdev_priv = scp->device->hostdata;
2352
2353 if (atomic_inc_return(&instance->fw_outstanding) >
2354 (instance->host->can_queue)) {
2355 fp_possible = false;
2356 atomic_dec(&instance->fw_outstanding);
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002357 } else if ((scsi_buff_len > MR_LARGE_IO_MIN_SIZE) ||
2358 atomic_dec_if_positive(&mrdev_priv->r1_ldio_hint)) {
2359 fp_possible = false;
2360 atomic_dec(&instance->fw_outstanding);
2361 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2362 atomic_set(&mrdev_priv->r1_ldio_hint,
2363 instance->r1_ldio_hint_default);
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002364 }
2365 }
2366
2367 /* If raid is NULL, set CPU affinity to default CPU0 */
2368 if (raid)
2369 megasas_set_raidflag_cpu_affinity(praid_context,
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002370 raid, fp_possible, io_info.isRead,
2371 scsi_buff_len);
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002372 else
Shivasharan Sa174118b2017-02-10 00:59:21 -08002373 praid_context->raid_context_g35.routing_flags |=
2374 (MR_RAID_CTX_CPUSEL_0 << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002375 }
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002376
adam radford9c915a82010-12-21 13:34:31 -08002377 if (fp_possible) {
2378 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
2379 local_map_ptr, start_lba_lo);
adam radford9c915a82010-12-21 13:34:31 -08002380 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2381 cmd->request_desc->SCSIIO.RequestFlags =
Sumit Saxena2c048352016-01-28 21:04:24 +05302382 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
adam radford9c915a82010-12-21 13:34:31 -08002383 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05302384 if (fusion->adapter_type == INVADER_SERIES) {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002385 if (io_request->RaidContext.raid_context.reg_lock_flags ==
adam radford36807e62011-10-08 18:15:06 -07002386 REGION_TYPE_UNUSED)
2387 cmd->request_desc->SCSIIO.RequestFlags =
2388 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2389 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002390 io_request->RaidContext.raid_context.type
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002391 = MPI2_TYPE_CUDA;
2392 io_request->RaidContext.raid_context.nseg = 0x1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302393 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002394 io_request->RaidContext.raid_context.reg_lock_flags |=
adam radford36807e62011-10-08 18:15:06 -07002395 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
2396 MR_RL_FLAGS_SEQ_NUM_ENABLE);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002397 } else if (instance->is_ventura) {
Shivasharan Sa174118b2017-02-10 00:59:21 -08002398 io_request->RaidContext.raid_context_g35.nseg_type |=
2399 (1 << RAID_CONTEXT_NSEG_SHIFT);
2400 io_request->RaidContext.raid_context_g35.nseg_type |=
2401 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2402 io_request->RaidContext.raid_context_g35.routing_flags |=
2403 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002404 io_request->IoFlags |=
Shivasharan Sa174118b2017-02-10 00:59:21 -08002405 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
adam radford36807e62011-10-08 18:15:06 -07002406 }
Shivasharan S5fc499b2017-02-10 00:59:17 -08002407 if (fusion->load_balance_info &&
2408 (fusion->load_balance_info[device_id].loadBalanceFlag) &&
2409 (io_info.isRead)) {
adam radford9c915a82010-12-21 13:34:31 -08002410 io_info.devHandle =
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05302411 get_updated_dev_handle(instance,
adam radford9c915a82010-12-21 13:34:31 -08002412 &fusion->load_balance_info[device_id],
Shivasharan S33203bc2017-02-10 00:59:12 -08002413 &io_info, local_map_ptr);
adam radford9c915a82010-12-21 13:34:31 -08002414 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05302415 cmd->pd_r1_lb = io_info.pd_after_lb;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002416 if (instance->is_ventura)
2417 io_request->RaidContext.raid_context_g35.span_arm
2418 = io_info.span_arm;
2419 else
2420 io_request->RaidContext.raid_context.span_arm
2421 = io_info.span_arm;
2422
adam radford9c915a82010-12-21 13:34:31 -08002423 } else
2424 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05302425
Shivasharan S8bf7c652017-02-10 00:59:03 -08002426 if (instance->is_ventura)
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002427 cmd->r1_alt_dev_handle = io_info.r1_alt_dev_handle;
Shivasharan S8bf7c652017-02-10 00:59:03 -08002428 else
2429 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002430
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05302431 if ((raidLUN[0] == 1) &&
sumit.saxena@avagotech.com75b96061e2015-08-31 17:24:01 +05302432 (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) {
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05302433 instance->dev_handle = !(instance->dev_handle);
2434 io_info.devHandle =
2435 local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
2436 }
2437
adam radford9c915a82010-12-21 13:34:31 -08002438 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
2439 io_request->DevHandle = io_info.devHandle;
Shivasharan S33203bc2017-02-10 00:59:12 -08002440 cmd->pd_interface = io_info.pd_interface;
adam radford21c9e162013-09-06 15:27:14 -07002441 /* populate the LUN field */
2442 memcpy(io_request->LUN, raidLUN, 8);
adam radford9c915a82010-12-21 13:34:31 -08002443 } else {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002444 io_request->RaidContext.raid_context.timeout_value =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302445 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
adam radford9c915a82010-12-21 13:34:31 -08002446 cmd->request_desc->SCSIIO.RequestFlags =
2447 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
2448 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05302449 if (fusion->adapter_type == INVADER_SERIES) {
Sumit Saxena8f050242016-01-28 21:04:27 +05302450 if (io_info.do_fp_rlbypass ||
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002451 (io_request->RaidContext.raid_context.reg_lock_flags
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002452 == REGION_TYPE_UNUSED))
adam radford36807e62011-10-08 18:15:06 -07002453 cmd->request_desc->SCSIIO.RequestFlags =
2454 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2455 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002456 io_request->RaidContext.raid_context.type
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002457 = MPI2_TYPE_CUDA;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002458 io_request->RaidContext.raid_context.reg_lock_flags |=
adam radford36807e62011-10-08 18:15:06 -07002459 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
2460 MR_RL_FLAGS_SEQ_NUM_ENABLE);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002461 io_request->RaidContext.raid_context.nseg = 0x1;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002462 } else if (instance->is_ventura) {
Shivasharan Sa174118b2017-02-10 00:59:21 -08002463 io_request->RaidContext.raid_context_g35.routing_flags |=
2464 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2465 io_request->RaidContext.raid_context_g35.nseg_type |=
2466 (1 << RAID_CONTEXT_NSEG_SHIFT);
2467 io_request->RaidContext.raid_context_g35.nseg_type |=
2468 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
adam radford36807e62011-10-08 18:15:06 -07002469 }
2470 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302471 io_request->DevHandle = cpu_to_le16(device_id);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002472
adam radford9c915a82010-12-21 13:34:31 -08002473 } /* Not FP */
2474}
2475
2476/**
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302477 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
adam radford9c915a82010-12-21 13:34:31 -08002478 * @instance: Adapter soft state
2479 * @scp: SCSI command
2480 * @cmd: Command to be prepared
2481 *
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302482 * Prepares the io_request frame for non-rw io cmds for vd.
adam radford9c915a82010-12-21 13:34:31 -08002483 */
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302484static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
2485 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
adam radford9c915a82010-12-21 13:34:31 -08002486{
2487 u32 device_id;
2488 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
Shivasharan Sd2d03582017-02-10 00:59:19 -08002489 u16 pd_index = 0, ld;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302490 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
adam radford9c915a82010-12-21 13:34:31 -08002491 struct fusion_context *fusion = instance->ctrl_context;
adam radford21c9e162013-09-06 15:27:14 -07002492 u8 span, physArm;
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05302493 __le16 devHandle;
Shivasharan Sd2d03582017-02-10 00:59:19 -08002494 u32 arRef, pd;
adam radford21c9e162013-09-06 15:27:14 -07002495 struct MR_LD_RAID *raid;
2496 struct RAID_CONTEXT *pRAID_Context;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302497 u8 fp_possible = 1;
adam radford9c915a82010-12-21 13:34:31 -08002498
2499 io_request = cmd->io_request;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302500 device_id = MEGASAS_DEV_INDEX(scmd);
2501 pd_index = MEGASAS_PD_INDEX(scmd);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302502 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302503 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302504 /* get RAID_Context pointer */
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002505 pRAID_Context = &io_request->RaidContext.raid_context;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302506 /* Check with FW team */
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002507 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2508 pRAID_Context->reg_lock_row_lba = 0;
2509 pRAID_Context->reg_lock_length = 0;
adam radford21c9e162013-09-06 15:27:14 -07002510
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302511 if (fusion->fast_path_io && (
2512 device_id < instance->fw_supported_vd_count)) {
Sumit.Saxena@avagotech.comab2f0602015-01-05 20:06:08 +05302513
adam radford21c9e162013-09-06 15:27:14 -07002514 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
Sumit.Saxena@avagotech.comab2f0602015-01-05 20:06:08 +05302515 if (ld >= instance->fw_supported_vd_count)
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302516 fp_possible = 0;
Shivasharan Sd2d03582017-02-10 00:59:19 -08002517 else {
2518 raid = MR_LdRaidGet(ld, local_map_ptr);
2519 if (!(raid->capability.fpNonRWCapable))
2520 fp_possible = 0;
2521 }
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302522 } else
2523 fp_possible = 0;
adam radford21c9e162013-09-06 15:27:14 -07002524
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302525 if (!fp_possible) {
2526 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2527 io_request->DevHandle = cpu_to_le16(device_id);
2528 io_request->LUN[1] = scmd->device->lun;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002529 pRAID_Context->timeout_value =
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302530 cpu_to_le16 (scmd->request->timeout / HZ);
2531 cmd->request_desc->SCSIIO.RequestFlags =
2532 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2533 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2534 } else {
adam radford21c9e162013-09-06 15:27:14 -07002535
2536 /* set RAID context values */
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002537 pRAID_Context->config_seq_num = raid->seqNum;
2538 if (!instance->is_ventura)
2539 pRAID_Context->reg_lock_flags = REGION_TYPE_SHARED_READ;
2540 pRAID_Context->timeout_value =
2541 cpu_to_le16(raid->fpIoTimeoutForLd);
adam radford21c9e162013-09-06 15:27:14 -07002542
2543 /* get the DevHandle for the PD (since this is
2544 fpNonRWCapable, this is a single disk RAID0) */
2545 span = physArm = 0;
2546 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
2547 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
2548 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
2549
2550 /* build request descriptor */
2551 cmd->request_desc->SCSIIO.RequestFlags =
Sumit Saxena2c048352016-01-28 21:04:24 +05302552 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302553 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford21c9e162013-09-06 15:27:14 -07002554 cmd->request_desc->SCSIIO.DevHandle = devHandle;
2555
2556 /* populate the LUN field */
2557 memcpy(io_request->LUN, raid->LUN, 8);
2558
2559 /* build the raidScsiIO structure */
2560 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2561 io_request->DevHandle = devHandle;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302562 }
2563}
adam radford21c9e162013-09-06 15:27:14 -07002564
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302565/**
2566 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
2567 * @instance: Adapter soft state
2568 * @scp: SCSI command
2569 * @cmd: Command to be prepared
2570 * @fp_possible: parameter to detect fast path or firmware path io.
2571 *
2572 * Prepares the io_request frame for rw/non-rw io cmds for syspds
2573 */
2574static void
2575megasas_build_syspd_fusion(struct megasas_instance *instance,
2576 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd, u8 fp_possible)
2577{
2578 u32 device_id;
2579 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2580 u16 pd_index = 0;
2581 u16 os_timeout_value;
2582 u16 timeout_limit;
2583 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2584 struct RAID_CONTEXT *pRAID_Context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302585 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
Shivasharan S33203bc2017-02-10 00:59:12 -08002586 struct MR_PRIV_DEVICE *mr_device_priv_data;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302587 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302588 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
adam radford21c9e162013-09-06 15:27:14 -07002589
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302590 device_id = MEGASAS_DEV_INDEX(scmd);
2591 pd_index = MEGASAS_PD_INDEX(scmd);
2592 os_timeout_value = scmd->request->timeout / HZ;
Shivasharan S33203bc2017-02-10 00:59:12 -08002593 mr_device_priv_data = scmd->device->hostdata;
2594 cmd->pd_interface = mr_device_priv_data->interface_type;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302595
2596 io_request = cmd->io_request;
2597 /* get RAID_Context pointer */
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002598 pRAID_Context = &io_request->RaidContext.raid_context;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002599 pRAID_Context->reg_lock_flags = 0;
2600 pRAID_Context->reg_lock_row_lba = 0;
2601 pRAID_Context->reg_lock_length = 0;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302602 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2603 io_request->LUN[1] = scmd->device->lun;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002604 pRAID_Context->raid_flags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302605 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
2606
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302607 /* If FW supports PD sequence number */
2608 if (instance->use_seqnum_jbod_fp &&
2609 instance->pd_list[pd_index].driveType == TYPE_DISK) {
2610 /* TgtId must be incremented by 255 as jbod seq number is index
2611 * below raid map
2612 */
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05002613 /* More than 256 PD/JBOD support for Ventura */
2614 if (instance->support_morethan256jbod)
2615 pRAID_Context->virtual_disk_tgt_id =
2616 pd_sync->seq[pd_index].pd_target_id;
2617 else
Shivasharan Sa174118b2017-02-10 00:59:21 -08002618 pRAID_Context->virtual_disk_tgt_id =
2619 cpu_to_le16(device_id + (MAX_PHYSICAL_DEVICES - 1));
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002620 pRAID_Context->config_seq_num = pd_sync->seq[pd_index].seqNum;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302621 io_request->DevHandle = pd_sync->seq[pd_index].devHandle;
Shivasharan Sa174118b2017-02-10 00:59:21 -08002622 if (instance->is_ventura) {
2623 io_request->RaidContext.raid_context_g35.routing_flags |=
2624 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2625 io_request->RaidContext.raid_context_g35.nseg_type |=
2626 (1 << RAID_CONTEXT_NSEG_SHIFT);
2627 io_request->RaidContext.raid_context_g35.nseg_type |=
2628 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2629 } else {
2630 pRAID_Context->type = MPI2_TYPE_CUDA;
2631 pRAID_Context->nseg = 0x1;
2632 pRAID_Context->reg_lock_flags |=
2633 (MR_RL_FLAGS_SEQ_NUM_ENABLE|MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
2634 }
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302635 } else if (fusion->fast_path_io) {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002636 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2637 pRAID_Context->config_seq_num = 0;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302638 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2639 io_request->DevHandle =
2640 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
2641 } else {
2642 /* Want to send all IO via FW path */
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002643 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2644 pRAID_Context->config_seq_num = 0;
Sumit Saxena9fb74c42015-11-05 21:17:38 +05302645 io_request->DevHandle = cpu_to_le16(0xFFFF);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302646 }
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302647
2648 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
2649 cmd->request_desc->SCSIIO.MSIxIndex =
2650 instance->msix_vectors ?
2651 (raw_smp_processor_id() % instance->msix_vectors) : 0;
2652
2653
2654 if (!fp_possible) {
2655 /* system pd firmware path */
adam radford9c915a82010-12-21 13:34:31 -08002656 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
adam radford9c915a82010-12-21 13:34:31 -08002657 cmd->request_desc->SCSIIO.RequestFlags =
2658 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302659 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002660 pRAID_Context->timeout_value = cpu_to_le16(os_timeout_value);
2661 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302662 } else {
2663 /* system pd Fast Path */
2664 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302665 timeout_limit = (scmd->device->type == TYPE_DISK) ?
2666 255 : 0xFFFF;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002667 pRAID_Context->timeout_value =
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302668 cpu_to_le16((os_timeout_value > timeout_limit) ?
2669 timeout_limit : os_timeout_value);
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05002670 if (fusion->adapter_type >= INVADER_SERIES)
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302671 io_request->IoFlags |=
2672 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
Kashyap Desaid5573582016-10-21 06:33:35 -07002673
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302674 cmd->request_desc->SCSIIO.RequestFlags =
Sumit Saxena2c048352016-01-28 21:04:24 +05302675 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302676 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford9c915a82010-12-21 13:34:31 -08002677 }
adam radford9c915a82010-12-21 13:34:31 -08002678}
2679
2680/**
2681 * megasas_build_io_fusion - Prepares IOs to devices
2682 * @instance: Adapter soft state
2683 * @scp: SCSI command
2684 * @cmd: Command to be prepared
2685 *
2686 * Invokes helper functions to prepare request frames
2687 * and sets flags appropriate for IO/Non-IO cmd
2688 */
2689int
2690megasas_build_io_fusion(struct megasas_instance *instance,
2691 struct scsi_cmnd *scp,
2692 struct megasas_cmd_fusion *cmd)
2693{
Shivasharan S33203bc2017-02-10 00:59:12 -08002694 int sge_count;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302695 u8 cmd_type;
adam radford9c915a82010-12-21 13:34:31 -08002696 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
2697
adam radford9c915a82010-12-21 13:34:31 -08002698 /* Zero out some fields so they don't get reused */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002699 memset(io_request->LUN, 0x0, 8);
adam radford9c915a82010-12-21 13:34:31 -08002700 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
2701 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
2702 io_request->EEDPFlags = 0;
2703 io_request->Control = 0;
2704 io_request->EEDPBlockSize = 0;
adam radford36807e62011-10-08 18:15:06 -07002705 io_request->ChainOffset = 0;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002706 io_request->RaidContext.raid_context.raid_flags = 0;
2707 io_request->RaidContext.raid_context.type = 0;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002708 io_request->RaidContext.raid_context.nseg = 0;
adam radford9c915a82010-12-21 13:34:31 -08002709
2710 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
2711 /*
2712 * Just the CDB length,rest of the Flags are zero
2713 * This will be modified for FP in build_ldio_fusion
2714 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302715 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
adam radford9c915a82010-12-21 13:34:31 -08002716
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302717 switch (cmd_type = megasas_cmd_type(scp)) {
2718 case READ_WRITE_LDIO:
adam radford9c915a82010-12-21 13:34:31 -08002719 megasas_build_ldio_fusion(instance, scp, cmd);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302720 break;
2721 case NON_READ_WRITE_LDIO:
2722 megasas_build_ld_nonrw_fusion(instance, scp, cmd);
2723 break;
2724 case READ_WRITE_SYSPDIO:
2725 case NON_READ_WRITE_SYSPDIO:
2726 if (instance->secure_jbod_support &&
2727 (cmd_type == NON_READ_WRITE_SYSPDIO))
2728 megasas_build_syspd_fusion(instance, scp, cmd, 0);
2729 else
2730 megasas_build_syspd_fusion(instance, scp, cmd, 1);
2731 break;
2732 default:
2733 break;
2734 }
adam radford9c915a82010-12-21 13:34:31 -08002735
2736 /*
2737 * Construct SGL
2738 */
2739
Shivasharan S33203bc2017-02-10 00:59:12 -08002740 sge_count = megasas_make_sgl(instance, scp, cmd);
adam radford9c915a82010-12-21 13:34:31 -08002741
Shivasharan S33203bc2017-02-10 00:59:12 -08002742 if (sge_count > instance->max_num_sge || (sge_count < 0)) {
2743 dev_err(&instance->pdev->dev,
2744 "%s %d sge_count (%d) is out of range. Range is: 0-%d\n",
2745 __func__, __LINE__, sge_count, instance->max_num_sge);
adam radford9c915a82010-12-21 13:34:31 -08002746 return 1;
2747 }
2748
Shivasharan Sa174118b2017-02-10 00:59:21 -08002749 if (instance->is_ventura) {
2750 set_num_sge(&io_request->RaidContext.raid_context_g35, sge_count);
2751 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.routing_flags);
2752 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.nseg_type);
2753 } else {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002754 /* numSGE store lower 8 bit of sge_count.
2755 * numSGEExt store higher 8 bit of sge_count
2756 */
2757 io_request->RaidContext.raid_context.num_sge = sge_count;
2758 io_request->RaidContext.raid_context.num_sge_ext =
2759 (u8)(sge_count >> 8);
2760 }
adam radford9c915a82010-12-21 13:34:31 -08002761
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302762 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
adam radford9c915a82010-12-21 13:34:31 -08002763
2764 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302765 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
adam radford9c915a82010-12-21 13:34:31 -08002766 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302767 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
adam radford9c915a82010-12-21 13:34:31 -08002768
2769 io_request->SGLOffset0 =
2770 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
2771
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302772 io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08002773 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
2774
2775 cmd->scmd = scp;
2776 scp->SCp.ptr = (char *)cmd;
2777
2778 return 0;
2779}
2780
Shivasharan S52205ac2017-02-10 00:59:08 -08002781static union MEGASAS_REQUEST_DESCRIPTOR_UNION *
adam radford9c915a82010-12-21 13:34:31 -08002782megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
2783{
2784 u8 *p;
2785 struct fusion_context *fusion;
2786
adam radford9c915a82010-12-21 13:34:31 -08002787 fusion = instance->ctrl_context;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002788 p = fusion->req_frames_desc +
2789 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * index;
adam radford9c915a82010-12-21 13:34:31 -08002790
2791 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
2792}
2793
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002794
2795/* megasas_prepate_secondRaid1_IO
2796 * It prepares the raid 1 second IO
2797 */
2798void megasas_prepare_secondRaid1_IO(struct megasas_instance *instance,
2799 struct megasas_cmd_fusion *cmd,
2800 struct megasas_cmd_fusion *r1_cmd)
2801{
2802 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc, *req_desc2 = NULL;
2803 struct fusion_context *fusion;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002804 fusion = instance->ctrl_context;
2805 req_desc = cmd->request_desc;
Shivasharan S8bf7c652017-02-10 00:59:03 -08002806 /* copy the io request frame as well as 8 SGEs data for r1 command*/
2807 memcpy(r1_cmd->io_request, cmd->io_request,
2808 (sizeof(struct MPI2_RAID_SCSI_IO_REQUEST)));
2809 memcpy(&r1_cmd->io_request->SGL, &cmd->io_request->SGL,
2810 (fusion->max_sge_in_main_msg * sizeof(union MPI2_SGE_IO_UNION)));
2811 /*sense buffer is different for r1 command*/
2812 r1_cmd->io_request->SenseBufferLowAddress =
2813 cpu_to_le32(r1_cmd->sense_phys_addr);
2814 r1_cmd->scmd = cmd->scmd;
2815 req_desc2 = megasas_get_request_descriptor(instance,
2816 (r1_cmd->index - 1));
2817 req_desc2->Words = 0;
2818 r1_cmd->request_desc = req_desc2;
2819 req_desc2->SCSIIO.SMID = cpu_to_le16(r1_cmd->index);
2820 req_desc2->SCSIIO.RequestFlags = req_desc->SCSIIO.RequestFlags;
2821 r1_cmd->request_desc->SCSIIO.DevHandle = cmd->r1_alt_dev_handle;
2822 r1_cmd->io_request->DevHandle = cmd->r1_alt_dev_handle;
2823 r1_cmd->r1_alt_dev_handle = cmd->io_request->DevHandle;
2824 cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
2825 cpu_to_le16(r1_cmd->index);
2826 r1_cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
2827 cpu_to_le16(cmd->index);
2828 /*MSIxIndex of both commands request descriptors should be same*/
2829 r1_cmd->request_desc->SCSIIO.MSIxIndex =
2830 cmd->request_desc->SCSIIO.MSIxIndex;
2831 /*span arm is different for r1 cmd*/
2832 r1_cmd->io_request->RaidContext.raid_context_g35.span_arm =
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002833 cmd->io_request->RaidContext.raid_context_g35.span_arm + 1;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002834}
2835
adam radford9c915a82010-12-21 13:34:31 -08002836/**
2837 * megasas_build_and_issue_cmd_fusion -Main routine for building and
2838 * issuing non IOCTL cmd
2839 * @instance: Adapter soft state
2840 * @scmd: pointer to scsi cmd from OS
2841 */
2842static u32
2843megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
2844 struct scsi_cmnd *scmd)
2845{
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002846 struct megasas_cmd_fusion *cmd, *r1_cmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -08002847 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2848 u32 index;
2849 struct fusion_context *fusion;
2850
2851 fusion = instance->ctrl_context;
2852
Sumit Saxena308ec452016-01-28 21:04:30 +05302853 if ((megasas_cmd_type(scmd) == READ_WRITE_LDIO) &&
2854 instance->ldio_threshold &&
2855 (atomic_inc_return(&instance->ldio_outstanding) >
2856 instance->ldio_threshold)) {
2857 atomic_dec(&instance->ldio_outstanding);
2858 return SCSI_MLQUEUE_DEVICE_BUSY;
2859 }
2860
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002861 if (atomic_inc_return(&instance->fw_outstanding) >
2862 instance->host->can_queue) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002863 atomic_dec(&instance->fw_outstanding);
2864 return SCSI_MLQUEUE_HOST_BUSY;
2865 }
2866
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302867 cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
adam radford9c915a82010-12-21 13:34:31 -08002868
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002869 if (!cmd) {
2870 atomic_dec(&instance->fw_outstanding);
2871 return SCSI_MLQUEUE_HOST_BUSY;
2872 }
2873
adam radford9c915a82010-12-21 13:34:31 -08002874 index = cmd->index;
2875
2876 req_desc = megasas_get_request_descriptor(instance, index-1);
adam radford9c915a82010-12-21 13:34:31 -08002877
2878 req_desc->Words = 0;
2879 cmd->request_desc = req_desc;
adam radford9c915a82010-12-21 13:34:31 -08002880
2881 if (megasas_build_io_fusion(instance, scmd, cmd)) {
2882 megasas_return_cmd_fusion(instance, cmd);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002883 dev_err(&instance->pdev->dev, "Error building command\n");
adam radford9c915a82010-12-21 13:34:31 -08002884 cmd->request_desc = NULL;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002885 atomic_dec(&instance->fw_outstanding);
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05302886 return SCSI_MLQUEUE_HOST_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08002887 }
2888
2889 req_desc = cmd->request_desc;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302890 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08002891
2892 if (cmd->io_request->ChainOffset != 0 &&
2893 cmd->io_request->ChainOffset != 0xF)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002894 dev_err(&instance->pdev->dev, "The chain offset value is not "
adam radford9c915a82010-12-21 13:34:31 -08002895 "correct : %x\n", cmd->io_request->ChainOffset);
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002896 /*
2897 * if it is raid 1/10 fp write capable.
2898 * try to get second command from pool and construct it.
2899 * From FW, it has confirmed that lba values of two PDs
2900 * corresponds to single R1/10 LD are always same
2901 *
2902 */
2903 /* driver side count always should be less than max_fw_cmds
2904 * to get new command
2905 */
Shivasharan S8bf7c652017-02-10 00:59:03 -08002906 if (cmd->r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002907 r1_cmd = megasas_get_cmd_fusion(instance,
2908 (scmd->request->tag + instance->max_fw_cmds));
2909 megasas_prepare_secondRaid1_IO(instance, cmd, r1_cmd);
2910 }
2911
Shivasharan S18bbcab2017-02-10 00:59:01 -08002912
adam radford9c915a82010-12-21 13:34:31 -08002913 /*
2914 * Issue the command to the FW
2915 */
adam radford9c915a82010-12-21 13:34:31 -08002916
Shivasharan S45b8a352017-02-10 00:59:04 -08002917 megasas_fire_cmd_fusion(instance, req_desc);
adam radford9c915a82010-12-21 13:34:31 -08002918
Shivasharan S18bbcab2017-02-10 00:59:01 -08002919 if (r1_cmd)
Shivasharan S45b8a352017-02-10 00:59:04 -08002920 megasas_fire_cmd_fusion(instance, r1_cmd->request_desc);
Shivasharan S18bbcab2017-02-10 00:59:01 -08002921
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002922
adam radford9c915a82010-12-21 13:34:31 -08002923 return 0;
2924}
2925
2926/**
Shivasharan S8bf7c652017-02-10 00:59:03 -08002927 * megasas_complete_r1_command -
2928 * completes R1 FP write commands which has valid peer smid
2929 * @instance: Adapter soft state
2930 * @cmd_fusion: MPT command frame
2931 *
2932 */
2933static inline void
2934megasas_complete_r1_command(struct megasas_instance *instance,
2935 struct megasas_cmd_fusion *cmd)
2936{
2937 u8 *sense, status, ex_status;
2938 u32 data_length;
2939 u16 peer_smid;
2940 struct fusion_context *fusion;
2941 struct megasas_cmd_fusion *r1_cmd = NULL;
2942 struct scsi_cmnd *scmd_local = NULL;
2943 struct RAID_CONTEXT_G35 *rctx_g35;
2944
2945 rctx_g35 = &cmd->io_request->RaidContext.raid_context_g35;
2946 fusion = instance->ctrl_context;
2947 peer_smid = le16_to_cpu(rctx_g35->smid.peer_smid);
2948
2949 r1_cmd = fusion->cmd_list[peer_smid - 1];
2950 scmd_local = cmd->scmd;
2951 status = rctx_g35->status;
2952 ex_status = rctx_g35->ex_status;
2953 data_length = cmd->io_request->DataLength;
2954 sense = cmd->sense;
2955
2956 cmd->cmd_completed = true;
2957
2958 /* Check if peer command is completed or not*/
2959 if (r1_cmd->cmd_completed) {
2960 rctx_g35 = &r1_cmd->io_request->RaidContext.raid_context_g35;
2961 if (rctx_g35->status != MFI_STAT_OK) {
2962 status = rctx_g35->status;
2963 ex_status = rctx_g35->ex_status;
2964 data_length = r1_cmd->io_request->DataLength;
2965 sense = r1_cmd->sense;
2966 }
2967
2968 megasas_return_cmd_fusion(instance, r1_cmd);
2969 map_cmd_status(fusion, scmd_local, status, ex_status,
2970 le32_to_cpu(data_length), sense);
2971 if (instance->ldio_threshold &&
2972 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
2973 atomic_dec(&instance->ldio_outstanding);
2974 scmd_local->SCp.ptr = NULL;
2975 megasas_return_cmd_fusion(instance, cmd);
2976 scsi_dma_unmap(scmd_local);
2977 scmd_local->scsi_done(scmd_local);
2978 }
2979}
2980
2981/**
adam radford9c915a82010-12-21 13:34:31 -08002982 * complete_cmd_fusion - Completes command
2983 * @instance: Adapter soft state
2984 * Completes all commands that is in reply descriptor queue
2985 */
2986int
adam radfordc8e858f2011-10-08 18:15:13 -07002987complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
adam radford9c915a82010-12-21 13:34:31 -08002988{
2989 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
2990 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
2991 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
2992 struct fusion_context *fusion;
2993 struct megasas_cmd *cmd_mfi;
Shivasharan S8bf7c652017-02-10 00:59:03 -08002994 struct megasas_cmd_fusion *cmd_fusion;
adam radford9c915a82010-12-21 13:34:31 -08002995 u16 smid, num_completed;
Shivasharan S8bf7c652017-02-10 00:59:03 -08002996 u8 reply_descript_type, *sense, status, extStatus;
2997 u32 device_id, data_length;
adam radford9c915a82010-12-21 13:34:31 -08002998 union desc_value d_val;
2999 struct LD_LOAD_BALANCE_INFO *lbinfo;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05303000 int threshold_reply_count = 0;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303001 struct scsi_cmnd *scmd_local = NULL;
Sumit Saxena18365b12016-01-28 21:04:25 +05303002 struct MR_TASK_MANAGE_REQUEST *mr_tm_req;
3003 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req;
adam radford9c915a82010-12-21 13:34:31 -08003004
3005 fusion = instance->ctrl_context;
3006
Sumit Saxena8a01a412016-01-28 21:04:32 +05303007 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
adam radford9c915a82010-12-21 13:34:31 -08003008 return IRQ_HANDLED;
3009
Sumit Saxena179ac142016-01-28 21:04:28 +05303010 desc = fusion->reply_frames_desc[MSIxIndex] +
3011 fusion->last_reply_idx[MSIxIndex];
adam radford9c915a82010-12-21 13:34:31 -08003012
3013 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3014
3015 d_val.word = desc->Words;
3016
3017 reply_descript_type = reply_desc->ReplyFlags &
3018 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3019
3020 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3021 return IRQ_NONE;
3022
adam radford9c915a82010-12-21 13:34:31 -08003023 num_completed = 0;
3024
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05303025 while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
3026 d_val.u.high != cpu_to_le32(UINT_MAX)) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003027
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303028 smid = le16_to_cpu(reply_desc->SMID);
adam radford9c915a82010-12-21 13:34:31 -08003029 cmd_fusion = fusion->cmd_list[smid - 1];
Shivasharan S8bf7c652017-02-10 00:59:03 -08003030 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
3031 cmd_fusion->io_request;
adam radford9c915a82010-12-21 13:34:31 -08003032
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303033 scmd_local = cmd_fusion->scmd;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05003034 status = scsi_io_req->RaidContext.raid_context.status;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05003035 extStatus = scsi_io_req->RaidContext.raid_context.ex_status;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003036 sense = cmd_fusion->sense;
3037 data_length = scsi_io_req->DataLength;
adam radford9c915a82010-12-21 13:34:31 -08003038
3039 switch (scsi_io_req->Function) {
Sumit Saxena18365b12016-01-28 21:04:25 +05303040 case MPI2_FUNCTION_SCSI_TASK_MGMT:
3041 mr_tm_req = (struct MR_TASK_MANAGE_REQUEST *)
3042 cmd_fusion->io_request;
3043 mpi_tm_req = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *)
3044 &mr_tm_req->TmRequest;
3045 dev_dbg(&instance->pdev->dev, "TM completion:"
3046 "type: 0x%x TaskMID: 0x%x\n",
3047 mpi_tm_req->TaskType, mpi_tm_req->TaskMID);
3048 complete(&cmd_fusion->done);
3049 break;
adam radford9c915a82010-12-21 13:34:31 -08003050 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
3051 /* Update load balancing info */
Shivasharan S8bf7c652017-02-10 00:59:03 -08003052 if (fusion->load_balance_info &&
3053 (cmd_fusion->scmd->SCp.Status &
3054 MEGASAS_LOAD_BALANCE_FLAG)) {
3055 device_id = MEGASAS_DEV_INDEX(scmd_local);
3056 lbinfo = &fusion->load_balance_info[device_id];
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05303057 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
Shivasharan S8bf7c652017-02-10 00:59:03 -08003058 cmd_fusion->scmd->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
adam radford9c915a82010-12-21 13:34:31 -08003059 }
Shivasharan S8bf7c652017-02-10 00:59:03 -08003060 //Fall thru and complete IO
adam radford9c915a82010-12-21 13:34:31 -08003061 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
Shivasharan S8bf7c652017-02-10 00:59:03 -08003062 atomic_dec(&instance->fw_outstanding);
3063 if (cmd_fusion->r1_alt_dev_handle == MR_DEVHANDLE_INVALID) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003064 map_cmd_status(fusion, scmd_local, status,
Shivasharan S8bf7c652017-02-10 00:59:03 -08003065 extStatus, le32_to_cpu(data_length),
3066 sense);
3067 if (instance->ldio_threshold &&
3068 (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO))
Sasikumar Chandrasekaranb71b49c2017-01-10 18:20:51 -05003069 atomic_dec(&instance->ldio_outstanding);
Shivasharan S8bf7c652017-02-10 00:59:03 -08003070 scmd_local->SCp.ptr = NULL;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003071 megasas_return_cmd_fusion(instance, cmd_fusion);
3072 scsi_dma_unmap(scmd_local);
3073 scmd_local->scsi_done(scmd_local);
Shivasharan S8bf7c652017-02-10 00:59:03 -08003074 } else /* Optimal VD - R1 FP command completion. */
3075 megasas_complete_r1_command(instance, cmd_fusion);
adam radford9c915a82010-12-21 13:34:31 -08003076 break;
3077 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
3078 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303079 /* Poll mode. Dummy free.
3080 * In case of Interrupt mode, caller has reverse check.
3081 */
3082 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
3083 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
3084 megasas_return_cmd(instance, cmd_mfi);
3085 } else
3086 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
adam radford9c915a82010-12-21 13:34:31 -08003087 break;
3088 }
3089
adam radfordc8e858f2011-10-08 18:15:13 -07003090 fusion->last_reply_idx[MSIxIndex]++;
3091 if (fusion->last_reply_idx[MSIxIndex] >=
3092 fusion->reply_q_depth)
3093 fusion->last_reply_idx[MSIxIndex] = 0;
adam radford9c915a82010-12-21 13:34:31 -08003094
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05303095 desc->Words = cpu_to_le64(ULLONG_MAX);
adam radford9c915a82010-12-21 13:34:31 -08003096 num_completed++;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05303097 threshold_reply_count++;
adam radford9c915a82010-12-21 13:34:31 -08003098
3099 /* Get the next reply descriptor */
adam radfordc8e858f2011-10-08 18:15:13 -07003100 if (!fusion->last_reply_idx[MSIxIndex])
Sumit Saxena179ac142016-01-28 21:04:28 +05303101 desc = fusion->reply_frames_desc[MSIxIndex];
adam radford9c915a82010-12-21 13:34:31 -08003102 else
3103 desc++;
3104
3105 reply_desc =
3106 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3107
3108 d_val.word = desc->Words;
3109
3110 reply_descript_type = reply_desc->ReplyFlags &
3111 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3112
3113 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3114 break;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05303115 /*
3116 * Write to reply post host index register after completing threshold
3117 * number of reply counts and still there are more replies in reply queue
3118 * pending to be completed
3119 */
3120 if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05003121 if (instance->msix_combined)
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05303122 writel(((MSIxIndex & 0x7) << 24) |
3123 fusion->last_reply_idx[MSIxIndex],
3124 instance->reply_post_host_index_addr[MSIxIndex/8]);
3125 else
3126 writel((MSIxIndex << 24) |
3127 fusion->last_reply_idx[MSIxIndex],
3128 instance->reply_post_host_index_addr[0]);
3129 threshold_reply_count = 0;
3130 }
adam radford9c915a82010-12-21 13:34:31 -08003131 }
3132
3133 if (!num_completed)
3134 return IRQ_NONE;
3135
3136 wmb();
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05003137 if (instance->msix_combined)
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303138 writel(((MSIxIndex & 0x7) << 24) |
3139 fusion->last_reply_idx[MSIxIndex],
3140 instance->reply_post_host_index_addr[MSIxIndex/8]);
3141 else
3142 writel((MSIxIndex << 24) |
3143 fusion->last_reply_idx[MSIxIndex],
3144 instance->reply_post_host_index_addr[0]);
adam radford53ef2bb2011-02-24 20:56:05 -08003145 megasas_check_and_restore_queue_depth(instance);
adam radford9c915a82010-12-21 13:34:31 -08003146 return IRQ_HANDLED;
3147}
3148
3149/**
3150 * megasas_complete_cmd_dpc_fusion - Completes command
3151 * @instance: Adapter soft state
3152 *
3153 * Tasklet to complete cmds
3154 */
3155void
3156megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
3157{
3158 struct megasas_instance *instance =
3159 (struct megasas_instance *)instance_addr;
3160 unsigned long flags;
adam radfordc8e858f2011-10-08 18:15:13 -07003161 u32 count, MSIxIndex;
3162
3163 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
adam radford9c915a82010-12-21 13:34:31 -08003164
3165 /* If we have already declared adapter dead, donot complete cmds */
3166 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303167 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
adam radford9c915a82010-12-21 13:34:31 -08003168 spin_unlock_irqrestore(&instance->hba_lock, flags);
3169 return;
3170 }
3171 spin_unlock_irqrestore(&instance->hba_lock, flags);
3172
adam radfordc8e858f2011-10-08 18:15:13 -07003173 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
3174 complete_cmd_fusion(instance, MSIxIndex);
adam radford9c915a82010-12-21 13:34:31 -08003175}
3176
3177/**
3178 * megasas_isr_fusion - isr entry point
3179 */
3180irqreturn_t megasas_isr_fusion(int irq, void *devp)
3181{
adam radfordc8e858f2011-10-08 18:15:13 -07003182 struct megasas_irq_context *irq_context = devp;
3183 struct megasas_instance *instance = irq_context->instance;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303184 u32 mfiStatus, fw_state, dma_state;
adam radford9c915a82010-12-21 13:34:31 -08003185
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303186 if (instance->mask_interrupts)
3187 return IRQ_NONE;
3188
adam radfordc8e858f2011-10-08 18:15:13 -07003189 if (!instance->msix_vectors) {
adam radford9c915a82010-12-21 13:34:31 -08003190 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
3191 if (!mfiStatus)
3192 return IRQ_NONE;
3193 }
3194
3195 /* If we are resetting, bail */
adam radford6497b242011-10-08 18:14:50 -07003196 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
3197 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08003198 return IRQ_HANDLED;
adam radford6497b242011-10-08 18:14:50 -07003199 }
adam radford9c915a82010-12-21 13:34:31 -08003200
adam radfordc8e858f2011-10-08 18:15:13 -07003201 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
adam radford6497b242011-10-08 18:14:50 -07003202 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08003203 /* If we didn't complete any commands, check for FW fault */
3204 fw_state = instance->instancet->read_fw_status_reg(
3205 instance->reg_set) & MFI_STATE_MASK;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303206 dma_state = instance->instancet->read_fw_status_reg
3207 (instance->reg_set) & MFI_STATE_DMADONE;
3208 if (instance->crash_dump_drv_support &&
3209 instance->crash_dump_app_support) {
3210 /* Start collecting crash, if DMA bit is done */
3211 if ((fw_state == MFI_STATE_FAULT) && dma_state)
3212 schedule_work(&instance->crash_init);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07003213 else if (fw_state == MFI_STATE_FAULT) {
3214 if (instance->unload == 0)
3215 schedule_work(&instance->work_init);
3216 }
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303217 } else if (fw_state == MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003218 dev_warn(&instance->pdev->dev, "Iop2SysDoorbellInt"
adam radford229fe472014-03-10 02:51:56 -07003219 "for scsi%d\n", instance->host->host_no);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07003220 if (instance->unload == 0)
3221 schedule_work(&instance->work_init);
adam radford229fe472014-03-10 02:51:56 -07003222 }
adam radford9c915a82010-12-21 13:34:31 -08003223 }
3224
3225 return IRQ_HANDLED;
3226}
3227
3228/**
3229 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
3230 * @instance: Adapter soft state
3231 * mfi_cmd: megasas_cmd pointer
3232 *
3233 */
3234u8
3235build_mpt_mfi_pass_thru(struct megasas_instance *instance,
3236 struct megasas_cmd *mfi_cmd)
3237{
3238 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
3239 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
3240 struct megasas_cmd_fusion *cmd;
3241 struct fusion_context *fusion;
3242 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
3243
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303244 fusion = instance->ctrl_context;
3245
3246 cmd = megasas_get_cmd_fusion(instance,
3247 instance->max_scsi_cmds + mfi_cmd->index);
adam radford9c915a82010-12-21 13:34:31 -08003248
3249 /* Save the smid. To be used for returning the cmd */
3250 mfi_cmd->context.smid = cmd->index;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303251
adam radford9c915a82010-12-21 13:34:31 -08003252 /*
3253 * For cmds where the flag is set, store the flag and check
3254 * on completion. For cmds with this flag, don't call
3255 * megasas_complete_cmd
3256 */
3257
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05303258 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303259 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
adam radford9c915a82010-12-21 13:34:31 -08003260
adam radford9c915a82010-12-21 13:34:31 -08003261 io_req = cmd->io_request;
adam radford36807e62011-10-08 18:15:06 -07003262
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05003263 if (fusion->adapter_type >= INVADER_SERIES) {
adam radford36807e62011-10-08 18:15:06 -07003264 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
3265 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
3266 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
3267 sgl_ptr_end->Flags = 0;
3268 }
3269
adam radford9c915a82010-12-21 13:34:31 -08003270 mpi25_ieee_chain =
3271 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
3272
3273 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
3274 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
3275 SGL) / 4;
3276 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
3277
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303278 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08003279
3280 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
3281 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
3282
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05303283 mpi25_ieee_chain->Length = cpu_to_le32(instance->max_chain_frame_sz);
adam radford9c915a82010-12-21 13:34:31 -08003284
3285 return 0;
3286}
3287
3288/**
3289 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
3290 * @instance: Adapter soft state
3291 * @cmd: mfi cmd to build
3292 *
3293 */
3294union MEGASAS_REQUEST_DESCRIPTOR_UNION *
3295build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
3296{
Shivasharan S52205ac2017-02-10 00:59:08 -08003297 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc = NULL;
adam radford9c915a82010-12-21 13:34:31 -08003298 u16 index;
3299
3300 if (build_mpt_mfi_pass_thru(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003301 dev_err(&instance->pdev->dev, "Couldn't build MFI pass thru cmd\n");
adam radford9c915a82010-12-21 13:34:31 -08003302 return NULL;
3303 }
3304
3305 index = cmd->context.smid;
3306
3307 req_desc = megasas_get_request_descriptor(instance, index - 1);
3308
adam radford9c915a82010-12-21 13:34:31 -08003309 req_desc->Words = 0;
3310 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3311 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3312
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303313 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08003314
3315 return req_desc;
3316}
3317
3318/**
3319 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
3320 * @instance: Adapter soft state
3321 * @cmd: mfi cmd pointer
3322 *
3323 */
Shivasharan Sf4fc2092017-02-10 00:59:09 -08003324void
adam radford9c915a82010-12-21 13:34:31 -08003325megasas_issue_dcmd_fusion(struct megasas_instance *instance,
3326 struct megasas_cmd *cmd)
3327{
3328 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
adam radford9c915a82010-12-21 13:34:31 -08003329
3330 req_desc = build_mpt_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303331
Shivasharan S45b8a352017-02-10 00:59:04 -08003332 megasas_fire_cmd_fusion(instance, req_desc);
Shivasharan Sf4fc2092017-02-10 00:59:09 -08003333 return;
adam radford9c915a82010-12-21 13:34:31 -08003334}
3335
3336/**
3337 * megasas_release_fusion - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01003338 * @instance: Adapter soft state
adam radford9c915a82010-12-21 13:34:31 -08003339 */
3340void
3341megasas_release_fusion(struct megasas_instance *instance)
3342{
3343 megasas_free_cmds(instance);
3344 megasas_free_cmds_fusion(instance);
3345
3346 iounmap(instance->reg_set);
3347
Yinghai Lue7f85162016-08-05 23:37:34 -07003348 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
adam radford9c915a82010-12-21 13:34:31 -08003349}
3350
3351/**
3352 * megasas_read_fw_status_reg_fusion - returns the current FW status value
3353 * @regs: MFI register set
3354 */
3355static u32
3356megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
3357{
3358 return readl(&(regs)->outbound_scratch_pad);
3359}
3360
3361/**
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303362 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3363 * @instance: Controller's soft instance
3364 * return: Number of allocated host crash buffers
3365 */
3366static void
3367megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
3368{
3369 unsigned int i;
3370
3371 instance->crash_buf_pages = get_order(CRASH_DMA_BUF_SIZE);
3372 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
3373 instance->crash_buf[i] = (void *)__get_free_pages(GFP_KERNEL,
3374 instance->crash_buf_pages);
3375 if (!instance->crash_buf[i]) {
3376 dev_info(&instance->pdev->dev, "Firmware crash dump "
3377 "memory allocation failed at index %d\n", i);
3378 break;
3379 }
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05303380 memset(instance->crash_buf[i], 0,
3381 ((1 << PAGE_SHIFT) << instance->crash_buf_pages));
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303382 }
3383 instance->drv_buf_alloc = i;
3384}
3385
3386/**
3387 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3388 * @instance: Controller's soft instance
3389 */
3390void
3391megasas_free_host_crash_buffer(struct megasas_instance *instance)
3392{
3393 unsigned int i
3394;
3395 for (i = 0; i < instance->drv_buf_alloc; i++) {
3396 if (instance->crash_buf[i])
3397 free_pages((ulong)instance->crash_buf[i],
3398 instance->crash_buf_pages);
3399 }
3400 instance->drv_buf_index = 0;
3401 instance->drv_buf_alloc = 0;
3402 instance->fw_crash_state = UNAVAILABLE;
3403 instance->fw_crash_buffer_size = 0;
3404}
3405
3406/**
adam radford9c915a82010-12-21 13:34:31 -08003407 * megasas_adp_reset_fusion - For controller reset
3408 * @regs: MFI register set
3409 */
3410static int
3411megasas_adp_reset_fusion(struct megasas_instance *instance,
3412 struct megasas_register_set __iomem *regs)
3413{
sumit.saxena@avagotech.com79b82c22015-08-31 17:23:41 +05303414 u32 host_diag, abs_state, retry;
3415
3416 /* Now try to reset the chip */
3417 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3418 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3419 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3420 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3421 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3422 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3423 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3424
3425 /* Check that the diag write enable (DRWE) bit is on */
3426 host_diag = readl(&instance->reg_set->fusion_host_diag);
3427 retry = 0;
3428 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
3429 msleep(100);
3430 host_diag = readl(&instance->reg_set->fusion_host_diag);
3431 if (retry++ == 100) {
3432 dev_warn(&instance->pdev->dev,
3433 "Host diag unlock failed from %s %d\n",
3434 __func__, __LINE__);
3435 break;
3436 }
3437 }
3438 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
3439 return -1;
3440
3441 /* Send chip reset command */
3442 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
3443 &instance->reg_set->fusion_host_diag);
3444 msleep(3000);
3445
3446 /* Make sure reset adapter bit is cleared */
3447 host_diag = readl(&instance->reg_set->fusion_host_diag);
3448 retry = 0;
3449 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
3450 msleep(100);
3451 host_diag = readl(&instance->reg_set->fusion_host_diag);
3452 if (retry++ == 1000) {
3453 dev_warn(&instance->pdev->dev,
3454 "Diag reset adapter never cleared %s %d\n",
3455 __func__, __LINE__);
3456 break;
3457 }
3458 }
3459 if (host_diag & HOST_DIAG_RESET_ADAPTER)
3460 return -1;
3461
3462 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set)
3463 & MFI_STATE_MASK;
3464 retry = 0;
3465
3466 while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
3467 msleep(100);
3468 abs_state = instance->instancet->
3469 read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
3470 }
3471 if (abs_state <= MFI_STATE_FW_INIT) {
3472 dev_warn(&instance->pdev->dev,
3473 "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
3474 abs_state, __func__, __LINE__);
3475 return -1;
3476 }
3477
adam radford9c915a82010-12-21 13:34:31 -08003478 return 0;
3479}
3480
3481/**
3482 * megasas_check_reset_fusion - For controller reset check
3483 * @regs: MFI register set
3484 */
3485static int
3486megasas_check_reset_fusion(struct megasas_instance *instance,
3487 struct megasas_register_set __iomem *regs)
3488{
3489 return 0;
3490}
3491
3492/* This function waits for outstanding commands on fusion to complete */
adam radford229fe472014-03-10 02:51:56 -07003493int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303494 int reason, int *convert)
adam radford9c915a82010-12-21 13:34:31 -08003495{
adam radford229fe472014-03-10 02:51:56 -07003496 int i, outstanding, retval = 0, hb_seconds_missed = 0;
adam radfordc007b8b2012-07-17 18:20:24 -07003497 u32 fw_state;
adam radford9c915a82010-12-21 13:34:31 -08003498
adam radfordc007b8b2012-07-17 18:20:24 -07003499 for (i = 0; i < resetwaittime; i++) {
adam radford9c915a82010-12-21 13:34:31 -08003500 /* Check if firmware is in fault state */
3501 fw_state = instance->instancet->read_fw_status_reg(
3502 instance->reg_set) & MFI_STATE_MASK;
3503 if (fw_state == MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003504 dev_warn(&instance->pdev->dev, "Found FW in FAULT state,"
adam radford229fe472014-03-10 02:51:56 -07003505 " will reset adapter scsi%d.\n",
3506 instance->host->host_no);
Sumit Saxena64d0b8e2016-04-15 00:23:31 -07003507 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
Shivasharan Sdef0eab2017-02-10 00:59:15 -08003508 if (instance->requestorId && reason) {
3509 dev_warn(&instance->pdev->dev, "SR-IOV Found FW in FAULT"
3510 " state while polling during"
3511 " I/O timeout handling for %d\n",
3512 instance->host->host_no);
3513 *convert = 1;
3514 }
3515
adam radford9c915a82010-12-21 13:34:31 -08003516 retval = 1;
3517 goto out;
3518 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303519
3520 if (reason == MFI_IO_TIMEOUT_OCR) {
3521 dev_info(&instance->pdev->dev,
3522 "MFI IO is timed out, initiating OCR\n");
Sumit Saxena64d0b8e2016-04-15 00:23:31 -07003523 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303524 retval = 1;
3525 goto out;
3526 }
3527
adam radford229fe472014-03-10 02:51:56 -07003528 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303529 if (instance->requestorId && !reason) {
adam radford229fe472014-03-10 02:51:56 -07003530 retval = 1;
3531 goto out;
3532 }
3533
3534 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
Shivasharan Sdef0eab2017-02-10 00:59:15 -08003535 if (instance->requestorId && (reason == SCSIIO_TIMEOUT_OCR)) {
adam radford229fe472014-03-10 02:51:56 -07003536 if (instance->hb_host_mem->HB.fwCounter !=
3537 instance->hb_host_mem->HB.driverCounter) {
3538 instance->hb_host_mem->HB.driverCounter =
3539 instance->hb_host_mem->HB.fwCounter;
3540 hb_seconds_missed = 0;
3541 } else {
3542 hb_seconds_missed++;
3543 if (hb_seconds_missed ==
3544 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003545 dev_warn(&instance->pdev->dev, "SR-IOV:"
adam radford229fe472014-03-10 02:51:56 -07003546 " Heartbeat never completed "
3547 " while polling during I/O "
3548 " timeout handling for "
3549 "scsi%d.\n",
3550 instance->host->host_no);
3551 *convert = 1;
3552 retval = 1;
3553 goto out;
3554 }
3555 }
3556 }
adam radford9c915a82010-12-21 13:34:31 -08003557
3558 outstanding = atomic_read(&instance->fw_outstanding);
3559 if (!outstanding)
3560 goto out;
3561
3562 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003563 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
adam radford229fe472014-03-10 02:51:56 -07003564 "commands to complete for scsi%d\n", i,
3565 outstanding, instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08003566 megasas_complete_cmd_dpc_fusion(
3567 (unsigned long)instance);
3568 }
3569 msleep(1000);
3570 }
3571
3572 if (atomic_read(&instance->fw_outstanding)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003573 dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
adam radford229fe472014-03-10 02:51:56 -07003574 "will reset adapter scsi%d.\n",
3575 instance->host->host_no);
Kashyap Desai18e1c7f2016-10-21 06:33:29 -07003576 *convert = 1;
adam radford9c915a82010-12-21 13:34:31 -08003577 retval = 1;
3578 }
3579out:
3580 return retval;
3581}
3582
3583void megasas_reset_reply_desc(struct megasas_instance *instance)
3584{
Sumit Saxena179ac142016-01-28 21:04:28 +05303585 int i, j, count;
adam radford9c915a82010-12-21 13:34:31 -08003586 struct fusion_context *fusion;
3587 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
3588
3589 fusion = instance->ctrl_context;
adam radfordc8e858f2011-10-08 18:15:13 -07003590 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
Sumit Saxena179ac142016-01-28 21:04:28 +05303591 for (i = 0 ; i < count ; i++) {
adam radfordc8e858f2011-10-08 18:15:13 -07003592 fusion->last_reply_idx[i] = 0;
Sumit Saxena179ac142016-01-28 21:04:28 +05303593 reply_desc = fusion->reply_frames_desc[i];
3594 for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++)
3595 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
3596 }
adam radford9c915a82010-12-21 13:34:31 -08003597}
3598
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05303599/*
3600 * megasas_refire_mgmt_cmd : Re-fire management commands
3601 * @instance: Controller's soft instance
3602*/
3603void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
3604{
3605 int j;
3606 struct megasas_cmd_fusion *cmd_fusion;
3607 struct fusion_context *fusion;
3608 struct megasas_cmd *cmd_mfi;
3609 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3610 u16 smid;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303611 bool refire_cmd = 0;
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05303612
3613 fusion = instance->ctrl_context;
3614
3615 /* Re-fire management commands.
3616 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
3617 */
3618 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
3619 cmd_fusion = fusion->cmd_list[j];
3620 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
3621 smid = le16_to_cpu(cmd_mfi->context.smid);
3622
3623 if (!smid)
3624 continue;
3625 req_desc = megasas_get_request_descriptor
3626 (instance, smid - 1);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303627 refire_cmd = req_desc && ((cmd_mfi->frame->dcmd.opcode !=
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303628 cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO)) &&
3629 (cmd_mfi->frame->dcmd.opcode !=
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303630 cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO)))
3631 && !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE);
3632 if (refire_cmd)
Shivasharan S45b8a352017-02-10 00:59:04 -08003633 megasas_fire_cmd_fusion(instance, req_desc);
Christoph Hellwig01d7f032015-04-23 16:34:09 +05303634 else
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05303635 megasas_return_cmd(instance, cmd_mfi);
3636 }
adam radford9c915a82010-12-21 13:34:31 -08003637}
3638
Sumit Saxena18365b12016-01-28 21:04:25 +05303639/*
3640 * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
3641 * @instance: per adapter struct
3642 * @channel: the channel assigned by the OS
3643 * @id: the id assigned by the OS
3644 *
3645 * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
3646 */
3647
3648static int megasas_track_scsiio(struct megasas_instance *instance,
3649 int id, int channel)
3650{
3651 int i, found = 0;
3652 struct megasas_cmd_fusion *cmd_fusion;
3653 struct fusion_context *fusion;
3654 fusion = instance->ctrl_context;
3655
3656 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
3657 cmd_fusion = fusion->cmd_list[i];
3658 if (cmd_fusion->scmd &&
3659 (cmd_fusion->scmd->device->id == id &&
3660 cmd_fusion->scmd->device->channel == channel)) {
3661 dev_info(&instance->pdev->dev,
3662 "SCSI commands pending to target"
3663 "channel %d id %d \tSMID: 0x%x\n",
3664 channel, id, cmd_fusion->index);
3665 scsi_print_command(cmd_fusion->scmd);
3666 found = 1;
3667 break;
3668 }
3669 }
3670
3671 return found ? FAILED : SUCCESS;
3672}
3673
3674/**
3675 * megasas_tm_response_code - translation of device response code
3676 * @ioc: per adapter object
3677 * @mpi_reply: MPI reply returned by firmware
3678 *
3679 * Return nothing.
3680 */
3681static void
3682megasas_tm_response_code(struct megasas_instance *instance,
3683 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply)
3684{
3685 char *desc;
3686
3687 switch (mpi_reply->ResponseCode) {
3688 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
3689 desc = "task management request completed";
3690 break;
3691 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
3692 desc = "invalid frame";
3693 break;
3694 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
3695 desc = "task management request not supported";
3696 break;
3697 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
3698 desc = "task management request failed";
3699 break;
3700 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
3701 desc = "task management request succeeded";
3702 break;
3703 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
3704 desc = "invalid lun";
3705 break;
3706 case 0xA:
3707 desc = "overlapped tag attempted";
3708 break;
3709 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
3710 desc = "task queued, however not sent to target";
3711 break;
3712 default:
3713 desc = "unknown";
3714 break;
3715 }
3716 dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n",
3717 mpi_reply->ResponseCode, desc);
3718 dev_dbg(&instance->pdev->dev,
3719 "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
3720 " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
3721 mpi_reply->TerminationCount, mpi_reply->DevHandle,
3722 mpi_reply->Function, mpi_reply->TaskType,
3723 mpi_reply->IOCStatus, mpi_reply->IOCLogInfo);
3724}
3725
3726/**
3727 * megasas_issue_tm - main routine for sending tm requests
3728 * @instance: per adapter struct
3729 * @device_handle: device handle
3730 * @channel: the channel assigned by the OS
3731 * @id: the id assigned by the OS
3732 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
3733 * @smid_task: smid assigned to the task
3734 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
3735 * Context: user
3736 *
3737 * MegaRaid use MPT interface for Task Magement request.
3738 * A generic API for sending task management requests to firmware.
3739 *
3740 * Return SUCCESS or FAILED.
3741 */
3742static int
3743megasas_issue_tm(struct megasas_instance *instance, u16 device_handle,
3744 uint channel, uint id, u16 smid_task, u8 type)
3745{
3746 struct MR_TASK_MANAGE_REQUEST *mr_request;
3747 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request;
3748 unsigned long timeleft;
3749 struct megasas_cmd_fusion *cmd_fusion;
3750 struct megasas_cmd *cmd_mfi;
3751 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3752 struct fusion_context *fusion;
3753 struct megasas_cmd_fusion *scsi_lookup;
3754 int rc;
3755 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply;
3756
3757 fusion = instance->ctrl_context;
3758
3759 cmd_mfi = megasas_get_cmd(instance);
3760
3761 if (!cmd_mfi) {
3762 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
3763 __func__, __LINE__);
3764 return -ENOMEM;
3765 }
3766
3767 cmd_fusion = megasas_get_cmd_fusion(instance,
3768 instance->max_scsi_cmds + cmd_mfi->index);
3769
3770 /* Save the smid. To be used for returning the cmd */
3771 cmd_mfi->context.smid = cmd_fusion->index;
3772
3773 req_desc = megasas_get_request_descriptor(instance,
3774 (cmd_fusion->index - 1));
Sumit Saxena18365b12016-01-28 21:04:25 +05303775
3776 cmd_fusion->request_desc = req_desc;
3777 req_desc->Words = 0;
3778
3779 scsi_lookup = fusion->cmd_list[smid_task - 1];
3780
3781 mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request;
3782 memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST));
3783 mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest;
3784 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3785 mpi_request->DevHandle = cpu_to_le16(device_handle);
3786 mpi_request->TaskType = type;
3787 mpi_request->TaskMID = cpu_to_le16(smid_task);
3788 mpi_request->LUN[1] = 0;
3789
3790
3791 req_desc = cmd_fusion->request_desc;
3792 req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index);
3793 req_desc->HighPriority.RequestFlags =
3794 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
3795 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3796 req_desc->HighPriority.MSIxIndex = 0;
3797 req_desc->HighPriority.LMID = 0;
3798 req_desc->HighPriority.Reserved1 = 0;
3799
3800 if (channel < MEGASAS_MAX_PD_CHANNELS)
3801 mr_request->tmReqFlags.isTMForPD = 1;
3802 else
3803 mr_request->tmReqFlags.isTMForLD = 1;
3804
3805 init_completion(&cmd_fusion->done);
Shivasharan S45b8a352017-02-10 00:59:04 -08003806 megasas_fire_cmd_fusion(instance, req_desc);
Sumit Saxena18365b12016-01-28 21:04:25 +05303807
3808 timeleft = wait_for_completion_timeout(&cmd_fusion->done, 50 * HZ);
3809
3810 if (!timeleft) {
3811 dev_err(&instance->pdev->dev,
3812 "task mgmt type 0x%x timed out\n", type);
3813 cmd_mfi->flags |= DRV_DCMD_SKIP_REFIRE;
3814 mutex_unlock(&instance->reset_mutex);
3815 rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR);
3816 mutex_lock(&instance->reset_mutex);
3817 return rc;
3818 }
3819
3820 mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply;
3821 megasas_tm_response_code(instance, mpi_reply);
3822
3823 megasas_return_cmd(instance, cmd_mfi);
3824 rc = SUCCESS;
3825 switch (type) {
3826 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
3827 if (scsi_lookup->scmd == NULL)
3828 break;
3829 else {
3830 instance->instancet->disable_intr(instance);
3831 msleep(1000);
3832 megasas_complete_cmd_dpc_fusion
3833 ((unsigned long)instance);
3834 instance->instancet->enable_intr(instance);
3835 if (scsi_lookup->scmd == NULL)
3836 break;
3837 }
3838 rc = FAILED;
3839 break;
3840
3841 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
3842 if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF))
3843 break;
3844 instance->instancet->disable_intr(instance);
3845 msleep(1000);
3846 megasas_complete_cmd_dpc_fusion
3847 ((unsigned long)instance);
3848 rc = megasas_track_scsiio(instance, id, channel);
3849 instance->instancet->enable_intr(instance);
3850
3851 break;
3852 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
3853 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
3854 break;
3855 default:
3856 rc = FAILED;
3857 break;
3858 }
3859
3860 return rc;
3861
3862}
3863
3864/*
3865 * megasas_fusion_smid_lookup : Look for fusion command correpspodning to SCSI
3866 * @instance: per adapter struct
3867 *
3868 * Return Non Zero index, if SMID found in outstanding commands
3869 */
3870static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd)
3871{
3872 int i, ret = 0;
3873 struct megasas_instance *instance;
3874 struct megasas_cmd_fusion *cmd_fusion;
3875 struct fusion_context *fusion;
3876
3877 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3878
3879 fusion = instance->ctrl_context;
3880
3881 for (i = 0; i < instance->max_scsi_cmds; i++) {
3882 cmd_fusion = fusion->cmd_list[i];
3883 if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) {
3884 scmd_printk(KERN_NOTICE, scmd, "Abort request is for"
3885 " SMID: %d\n", cmd_fusion->index);
3886 ret = cmd_fusion->index;
3887 break;
3888 }
3889 }
3890
3891 return ret;
3892}
3893
3894/*
3895* megasas_get_tm_devhandle - Get devhandle for TM request
3896* @sdev- OS provided scsi device
3897*
3898* Returns- devhandle/targetID of SCSI device
3899*/
3900static u16 megasas_get_tm_devhandle(struct scsi_device *sdev)
3901{
3902 u16 pd_index = 0;
3903 u32 device_id;
3904 struct megasas_instance *instance;
3905 struct fusion_context *fusion;
3906 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
3907 u16 devhandle = (u16)ULONG_MAX;
3908
3909 instance = (struct megasas_instance *)sdev->host->hostdata;
3910 fusion = instance->ctrl_context;
3911
Shivasharan S3cabd162017-02-10 00:59:05 -08003912 if (!MEGASAS_IS_LOGICAL(sdev)) {
Sumit Saxena18365b12016-01-28 21:04:25 +05303913 if (instance->use_seqnum_jbod_fp) {
Shivasharan S3cabd162017-02-10 00:59:05 -08003914 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
3915 + sdev->id;
3916 pd_sync = (void *)fusion->pd_seq_sync
3917 [(instance->pd_seq_map_id - 1) & 1];
3918 devhandle = pd_sync->seq[pd_index].devHandle;
Sumit Saxena18365b12016-01-28 21:04:25 +05303919 } else
3920 sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable"
3921 " without JBOD MAP support from %s %d\n", __func__, __LINE__);
3922 } else {
3923 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
3924 + sdev->id;
3925 devhandle = device_id;
3926 }
3927
3928 return devhandle;
3929}
3930
3931/*
3932 * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
3933 * @scmd : pointer to scsi command object
3934 *
3935 * Return SUCCESS, if command aborted else FAILED
3936 */
3937
3938int megasas_task_abort_fusion(struct scsi_cmnd *scmd)
3939{
3940 struct megasas_instance *instance;
3941 u16 smid, devhandle;
3942 struct fusion_context *fusion;
3943 int ret;
3944 struct MR_PRIV_DEVICE *mr_device_priv_data;
3945 mr_device_priv_data = scmd->device->hostdata;
3946
3947
3948 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3949 fusion = instance->ctrl_context;
3950
Shivasharan Sdef0eab2017-02-10 00:59:15 -08003951 scmd_printk(KERN_INFO, scmd, "task abort called for scmd(%p)\n", scmd);
3952 scsi_print_command(scmd);
3953
Sumit Saxena8a01a412016-01-28 21:04:32 +05303954 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Sumit Saxena18365b12016-01-28 21:04:25 +05303955 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
3956 "SCSI host:%d\n", instance->host->host_no);
3957 ret = FAILED;
3958 return ret;
3959 }
3960
3961 if (!mr_device_priv_data) {
3962 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
3963 "scmd(%p)\n", scmd);
3964 scmd->result = DID_NO_CONNECT << 16;
3965 ret = SUCCESS;
3966 goto out;
3967 }
3968
3969
3970 if (!mr_device_priv_data->is_tm_capable) {
3971 ret = FAILED;
3972 goto out;
3973 }
3974
3975 mutex_lock(&instance->reset_mutex);
3976
3977 smid = megasas_fusion_smid_lookup(scmd);
3978
3979 if (!smid) {
3980 ret = SUCCESS;
3981 scmd_printk(KERN_NOTICE, scmd, "Command for which abort is"
3982 " issued is not found in oustanding commands\n");
3983 mutex_unlock(&instance->reset_mutex);
3984 goto out;
3985 }
3986
3987 devhandle = megasas_get_tm_devhandle(scmd->device);
3988
3989 if (devhandle == (u16)ULONG_MAX) {
3990 ret = SUCCESS;
3991 sdev_printk(KERN_INFO, scmd->device,
3992 "task abort issued for invalid devhandle\n");
3993 mutex_unlock(&instance->reset_mutex);
3994 goto out;
3995 }
3996 sdev_printk(KERN_INFO, scmd->device,
3997 "attempting task abort! scmd(%p) tm_dev_handle 0x%x\n",
3998 scmd, devhandle);
3999
4000 mr_device_priv_data->tm_busy = 1;
4001 ret = megasas_issue_tm(instance, devhandle,
4002 scmd->device->channel, scmd->device->id, smid,
4003 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK);
4004 mr_device_priv_data->tm_busy = 0;
4005
4006 mutex_unlock(&instance->reset_mutex);
4007out:
4008 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
4009 ((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4010
4011 return ret;
4012}
4013
4014/*
4015 * megasas_reset_target_fusion : target reset function for fusion adapters
4016 * scmd: SCSI command pointer
4017 *
4018 * Returns SUCCESS if all commands associated with target aborted else FAILED
4019 */
4020
4021int megasas_reset_target_fusion(struct scsi_cmnd *scmd)
4022{
4023
4024 struct megasas_instance *instance;
4025 int ret = FAILED;
4026 u16 devhandle;
4027 struct fusion_context *fusion;
4028 struct MR_PRIV_DEVICE *mr_device_priv_data;
4029 mr_device_priv_data = scmd->device->hostdata;
4030
4031 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4032 fusion = instance->ctrl_context;
4033
Shivasharan Sdef0eab2017-02-10 00:59:15 -08004034 sdev_printk(KERN_INFO, scmd->device,
4035 "target reset called for scmd(%p)\n", scmd);
4036
Sumit Saxena8a01a412016-01-28 21:04:32 +05304037 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Sumit Saxena18365b12016-01-28 21:04:25 +05304038 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4039 "SCSI host:%d\n", instance->host->host_no);
4040 ret = FAILED;
4041 return ret;
4042 }
4043
4044 if (!mr_device_priv_data) {
4045 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
4046 "scmd(%p)\n", scmd);
4047 scmd->result = DID_NO_CONNECT << 16;
4048 ret = SUCCESS;
4049 goto out;
4050 }
4051
4052
4053 if (!mr_device_priv_data->is_tm_capable) {
4054 ret = FAILED;
4055 goto out;
4056 }
4057
4058 mutex_lock(&instance->reset_mutex);
4059 devhandle = megasas_get_tm_devhandle(scmd->device);
4060
4061 if (devhandle == (u16)ULONG_MAX) {
4062 ret = SUCCESS;
4063 sdev_printk(KERN_INFO, scmd->device,
4064 "target reset issued for invalid devhandle\n");
4065 mutex_unlock(&instance->reset_mutex);
4066 goto out;
4067 }
4068
4069 sdev_printk(KERN_INFO, scmd->device,
4070 "attempting target reset! scmd(%p) tm_dev_handle 0x%x\n",
4071 scmd, devhandle);
4072 mr_device_priv_data->tm_busy = 1;
4073 ret = megasas_issue_tm(instance, devhandle,
4074 scmd->device->channel, scmd->device->id, 0,
4075 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
4076 mr_device_priv_data->tm_busy = 0;
4077 mutex_unlock(&instance->reset_mutex);
4078out:
4079 scmd_printk(KERN_NOTICE, scmd, "megasas: target reset %s!!\n",
4080 (ret == SUCCESS) ? "SUCCESS" : "FAILED");
4081
4082 return ret;
4083}
4084
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05304085/*SRIOV get other instance in cluster if any*/
4086struct megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance)
4087{
4088 int i;
4089
4090 for (i = 0; i < MAX_MGMT_ADAPTERS; i++) {
4091 if (megasas_mgmt_info.instance[i] &&
4092 (megasas_mgmt_info.instance[i] != instance) &&
4093 megasas_mgmt_info.instance[i]->requestorId &&
4094 megasas_mgmt_info.instance[i]->peerIsPresent &&
4095 (memcmp((megasas_mgmt_info.instance[i]->clusterId),
4096 instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0))
4097 return megasas_mgmt_info.instance[i];
4098 }
4099 return NULL;
4100}
4101
adam radford229fe472014-03-10 02:51:56 -07004102/* Check for a second path that is currently UP */
4103int megasas_check_mpio_paths(struct megasas_instance *instance,
4104 struct scsi_cmnd *scmd)
adam radford9c915a82010-12-21 13:34:31 -08004105{
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05304106 struct megasas_instance *peer_instance = NULL;
Shivasharan Sf55cf472017-02-10 00:59:07 -08004107 int retval = (DID_REQUEUE << 16);
adam radford229fe472014-03-10 02:51:56 -07004108
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05304109 if (instance->peerIsPresent) {
4110 peer_instance = megasas_get_peer_instance(instance);
4111 if ((peer_instance) &&
4112 (atomic_read(&peer_instance->adprecovery) ==
4113 MEGASAS_HBA_OPERATIONAL))
4114 retval = (DID_NO_CONNECT << 16);
adam radford229fe472014-03-10 02:51:56 -07004115 }
adam radford229fe472014-03-10 02:51:56 -07004116 return retval;
4117}
4118
4119/* Core fusion reset function */
Sumit Saxena18365b12016-01-28 21:04:25 +05304120int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
adam radford229fe472014-03-10 02:51:56 -07004121{
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05004122 int retval = SUCCESS, i, j, convert = 0;
adam radford9c915a82010-12-21 13:34:31 -08004123 struct megasas_instance *instance;
Shivasharan S8bf7c652017-02-10 00:59:03 -08004124 struct megasas_cmd_fusion *cmd_fusion, *r1_cmd;
adam radford9c915a82010-12-21 13:34:31 -08004125 struct fusion_context *fusion;
sumit.saxena@avagotech.com79b82c22015-08-31 17:23:41 +05304126 u32 abs_state, status_reg, reset_adapter;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304127 u32 io_timeout_in_crash_mode = 0;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304128 struct scsi_cmnd *scmd_local = NULL;
Sumit Saxena18365b12016-01-28 21:04:25 +05304129 struct scsi_device *sdev;
adam radford9c915a82010-12-21 13:34:31 -08004130
4131 instance = (struct megasas_instance *)shost->hostdata;
4132 fusion = instance->ctrl_context;
4133
adam radford229fe472014-03-10 02:51:56 -07004134 mutex_lock(&instance->reset_mutex);
4135
Sumit Saxena8a01a412016-01-28 21:04:32 +05304136 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004137 dev_warn(&instance->pdev->dev, "Hardware critical error, "
adam radford229fe472014-03-10 02:51:56 -07004138 "returning FAILED for scsi%d.\n",
4139 instance->host->host_no);
Adam Radforda2fbcbc2014-07-09 15:17:54 -07004140 mutex_unlock(&instance->reset_mutex);
adam radfordd4a759a2011-10-08 18:14:39 -07004141 return FAILED;
adam radford9c915a82010-12-21 13:34:31 -08004142 }
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304143 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
4144 abs_state = status_reg & MFI_STATE_MASK;
4145
4146 /* IO timeout detected, forcibly put FW in FAULT state */
4147 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
Sumit Saxena18365b12016-01-28 21:04:25 +05304148 instance->crash_dump_app_support && reason) {
4149 dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, "
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304150 "forcibly FAULT Firmware\n");
Sumit Saxena8a01a412016-01-28 21:04:32 +05304151 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304152 status_reg = readl(&instance->reg_set->doorbell);
4153 writel(status_reg | MFI_STATE_FORCE_OCR,
4154 &instance->reg_set->doorbell);
4155 readl(&instance->reg_set->doorbell);
4156 mutex_unlock(&instance->reset_mutex);
4157 do {
4158 ssleep(3);
4159 io_timeout_in_crash_mode++;
4160 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
4161 "seconds for crash dump collection and OCR "
4162 "to be done\n", (io_timeout_in_crash_mode * 3));
Sumit Saxena8a01a412016-01-28 21:04:32 +05304163 } while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304164 (io_timeout_in_crash_mode < 80));
4165
Sumit Saxena8a01a412016-01-28 21:04:32 +05304166 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304167 dev_info(&instance->pdev->dev, "OCR done for IO "
4168 "timeout case\n");
4169 retval = SUCCESS;
4170 } else {
4171 dev_info(&instance->pdev->dev, "Controller is not "
4172 "operational after 240 seconds wait for IO "
4173 "timeout case in FW crash dump mode\n do "
4174 "OCR/kill adapter\n");
4175 retval = megasas_reset_fusion(shost, 0);
4176 }
4177 return retval;
4178 }
adam radford9c915a82010-12-21 13:34:31 -08004179
adam radford229fe472014-03-10 02:51:56 -07004180 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
4181 del_timer_sync(&instance->sriov_heartbeat_timer);
adam radford7e70e732011-05-11 18:34:08 -07004182 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05304183 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304184 instance->instancet->disable_intr(instance);
adam radford7e70e732011-05-11 18:34:08 -07004185 msleep(1000);
4186
adam radford9c915a82010-12-21 13:34:31 -08004187 /* First try waiting for commands to complete */
Sumit Saxena18365b12016-01-28 21:04:25 +05304188 if (megasas_wait_for_outstanding_fusion(instance, reason,
adam radford229fe472014-03-10 02:51:56 -07004189 &convert)) {
Sumit Saxena8a01a412016-01-28 21:04:32 +05304190 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004191 dev_warn(&instance->pdev->dev, "resetting fusion "
adam radford229fe472014-03-10 02:51:56 -07004192 "adapter scsi%d.\n", instance->host->host_no);
4193 if (convert)
Sumit Saxena18365b12016-01-28 21:04:25 +05304194 reason = 0;
adam radford229fe472014-03-10 02:51:56 -07004195
Shivasharan Sdef0eab2017-02-10 00:59:15 -08004196 if (megasas_dbg_lvl & OCR_LOGS)
4197 dev_info(&instance->pdev->dev, "\nPending SCSI commands:\n");
4198
adam radford9c915a82010-12-21 13:34:31 -08004199 /* Now return commands back to the OS */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304200 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
adam radford9c915a82010-12-21 13:34:31 -08004201 cmd_fusion = fusion->cmd_list[i];
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05004202 /*check for extra commands issued by driver*/
4203 if (instance->is_ventura) {
Shivasharan S8bf7c652017-02-10 00:59:03 -08004204 r1_cmd = fusion->cmd_list[i + instance->max_fw_cmds];
4205 megasas_return_cmd_fusion(instance, r1_cmd);
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05004206 }
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304207 scmd_local = cmd_fusion->scmd;
adam radford9c915a82010-12-21 13:34:31 -08004208 if (cmd_fusion->scmd) {
Shivasharan Sdef0eab2017-02-10 00:59:15 -08004209 if (megasas_dbg_lvl & OCR_LOGS) {
4210 sdev_printk(KERN_INFO,
4211 cmd_fusion->scmd->device, "SMID: 0x%x\n",
4212 cmd_fusion->index);
4213 scsi_print_command(cmd_fusion->scmd);
4214 }
4215
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304216 scmd_local->result =
adam radford229fe472014-03-10 02:51:56 -07004217 megasas_check_mpio_paths(instance,
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304218 scmd_local);
Sasikumar Chandrasekaranb71b49c2017-01-10 18:20:51 -05004219 if (instance->ldio_threshold &&
4220 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
Sumit Saxena308ec452016-01-28 21:04:30 +05304221 atomic_dec(&instance->ldio_outstanding);
adam radford9c915a82010-12-21 13:34:31 -08004222 megasas_return_cmd_fusion(instance, cmd_fusion);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304223 scsi_dma_unmap(scmd_local);
4224 scmd_local->scsi_done(scmd_local);
adam radford9c915a82010-12-21 13:34:31 -08004225 }
4226 }
4227
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05004228 atomic_set(&instance->fw_outstanding, 0);
4229
adam radford7e70e732011-05-11 18:34:08 -07004230 status_reg = instance->instancet->read_fw_status_reg(
4231 instance->reg_set);
4232 abs_state = status_reg & MFI_STATE_MASK;
4233 reset_adapter = status_reg & MFI_RESET_ADAPTER;
4234 if (instance->disableOnlineCtrlReset ||
4235 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
adam radford9c915a82010-12-21 13:34:31 -08004236 /* Reset not supported, kill adapter */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004237 dev_warn(&instance->pdev->dev, "Reset not supported"
adam radford229fe472014-03-10 02:51:56 -07004238 ", killing adapter scsi%d.\n",
4239 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08004240 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07004241 instance->skip_heartbeat_timer_del = 1;
adam radford9c915a82010-12-21 13:34:31 -08004242 retval = FAILED;
4243 goto out;
4244 }
4245
adam radford229fe472014-03-10 02:51:56 -07004246 /* Let SR-IOV VF & PF sync up if there was a HB failure */
Sumit Saxena18365b12016-01-28 21:04:25 +05304247 if (instance->requestorId && !reason) {
adam radford229fe472014-03-10 02:51:56 -07004248 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
Sumit Saxena3885c262016-01-28 21:04:35 +05304249 goto transition_to_ready;
adam radford229fe472014-03-10 02:51:56 -07004250 }
4251
adam radford9c915a82010-12-21 13:34:31 -08004252 /* Now try to reset the chip */
4253 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
adam radford9c915a82010-12-21 13:34:31 -08004254
sumit.saxena@avagotech.com79b82c22015-08-31 17:23:41 +05304255 if (instance->instancet->adp_reset
4256 (instance, instance->reg_set))
adam radford9c915a82010-12-21 13:34:31 -08004257 continue;
Sumit Saxena3885c262016-01-28 21:04:35 +05304258transition_to_ready:
adam radford9c915a82010-12-21 13:34:31 -08004259 /* Wait for FW to become ready */
adam radford058a8fa2011-10-08 18:14:27 -07004260 if (megasas_transition_to_ready(instance, 1)) {
Sumit Saxena3885c262016-01-28 21:04:35 +05304261 dev_warn(&instance->pdev->dev,
4262 "Failed to transition controller to ready for "
4263 "scsi%d.\n", instance->host->host_no);
4264 if (instance->requestorId && !reason)
4265 goto fail_kill_adapter;
4266 else
4267 continue;
adam radford9c915a82010-12-21 13:34:31 -08004268 }
adam radford9c915a82010-12-21 13:34:31 -08004269 megasas_reset_reply_desc(instance);
Sumit Saxena308ec452016-01-28 21:04:30 +05304270 megasas_fusion_update_can_queue(instance, OCR_CONTEXT);
4271
adam radford9c915a82010-12-21 13:34:31 -08004272 if (megasas_ioc_init_fusion(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004273 dev_warn(&instance->pdev->dev,
Sumit Saxena3885c262016-01-28 21:04:35 +05304274 "megasas_ioc_init_fusion() failed! for "
4275 "scsi%d\n", instance->host->host_no);
4276 if (instance->requestorId && !reason)
4277 goto fail_kill_adapter;
4278 else
4279 continue;
adam radford9c915a82010-12-21 13:34:31 -08004280 }
4281
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05304282 megasas_refire_mgmt_cmd(instance);
adam radford9c915a82010-12-21 13:34:31 -08004283
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304284 if (megasas_get_ctrl_info(instance)) {
4285 dev_info(&instance->pdev->dev,
4286 "Failed from %s %d\n",
4287 __func__, __LINE__);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304288 megaraid_sas_kill_hba(instance);
4289 retval = FAILED;
4290 }
adam radford9c915a82010-12-21 13:34:31 -08004291 /* Reset load balance info */
Shivasharan S5fc499b2017-02-10 00:59:17 -08004292 if (fusion->load_balance_info)
4293 memset(fusion->load_balance_info, 0,
4294 (sizeof(struct LD_LOAD_BALANCE_INFO) *
4295 MAX_LOGICAL_DRIVES_EXT));
adam radford9c915a82010-12-21 13:34:31 -08004296
4297 if (!megasas_get_map_info(instance))
4298 megasas_sync_map_info(instance);
4299
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304300 megasas_setup_jbod_map(instance);
4301
Sumit Saxena18365b12016-01-28 21:04:25 +05304302 shost_for_each_device(sdev, shost)
Shivasharan S15dd0382017-02-10 00:59:10 -08004303 megasas_set_dynamic_target_properties(sdev);
Sumit Saxena18365b12016-01-28 21:04:25 +05304304
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05004305 /* reset stream detection array */
4306 if (instance->is_ventura) {
4307 for (j = 0; j < MAX_LOGICAL_DRIVES_EXT; ++j) {
4308 memset(fusion->stream_detect_by_ld[j],
4309 0, sizeof(struct LD_STREAM_DETECT));
4310 fusion->stream_detect_by_ld[j]->mru_bit_map
4311 = MR_STREAM_BITMAP;
4312 }
4313 }
4314
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05304315 clear_bit(MEGASAS_FUSION_IN_RESET,
4316 &instance->reset_flags);
4317 instance->instancet->enable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05304318 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05304319
adam radford229fe472014-03-10 02:51:56 -07004320 /* Restart SR-IOV heartbeat */
4321 if (instance->requestorId) {
4322 if (!megasas_sriov_start_heartbeat(instance, 0))
4323 megasas_start_timer(instance,
4324 &instance->sriov_heartbeat_timer,
4325 megasas_sriov_heartbeat_handler,
4326 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4327 else
4328 instance->skip_heartbeat_timer_del = 1;
4329 }
4330
adam radford9c915a82010-12-21 13:34:31 -08004331 /* Adapter reset completed successfully */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004332 dev_warn(&instance->pdev->dev, "Reset "
adam radford229fe472014-03-10 02:51:56 -07004333 "successful for scsi%d.\n",
4334 instance->host->host_no);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304335
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05304336 if (instance->crash_dump_drv_support &&
4337 instance->crash_dump_app_support)
4338 megasas_set_crash_dump_params(instance,
4339 MR_CRASH_BUF_TURN_ON);
4340 else
4341 megasas_set_crash_dump_params(instance,
4342 MR_CRASH_BUF_TURN_OFF);
4343
adam radford9c915a82010-12-21 13:34:31 -08004344 retval = SUCCESS;
4345 goto out;
4346 }
Sumit Saxena3885c262016-01-28 21:04:35 +05304347fail_kill_adapter:
adam radford9c915a82010-12-21 13:34:31 -08004348 /* Reset failed, kill the adapter */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004349 dev_warn(&instance->pdev->dev, "Reset failed, killing "
adam radford229fe472014-03-10 02:51:56 -07004350 "adapter scsi%d.\n", instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08004351 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07004352 instance->skip_heartbeat_timer_del = 1;
adam radford9c915a82010-12-21 13:34:31 -08004353 retval = FAILED;
4354 } else {
adam radford229fe472014-03-10 02:51:56 -07004355 /* For VF: Restart HB timer if we didn't OCR */
4356 if (instance->requestorId) {
4357 megasas_start_timer(instance,
4358 &instance->sriov_heartbeat_timer,
4359 megasas_sriov_heartbeat_handler,
4360 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4361 }
adam radford3f0e58bc2011-10-08 18:14:59 -07004362 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304363 instance->instancet->enable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05304364 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
adam radford9c915a82010-12-21 13:34:31 -08004365 }
4366out:
4367 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4368 mutex_unlock(&instance->reset_mutex);
4369 return retval;
4370}
4371
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304372/* Fusion Crash dump collection work queue */
4373void megasas_fusion_crash_dump_wq(struct work_struct *work)
4374{
4375 struct megasas_instance *instance =
4376 container_of(work, struct megasas_instance, crash_init);
4377 u32 status_reg;
4378 u8 partial_copy = 0;
4379
4380
4381 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
4382
4383 /*
4384 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
4385 * to host crash buffers
4386 */
4387 if (instance->drv_buf_index == 0) {
4388 /* Buffer is already allocated for old Crash dump.
4389 * Do OCR and do not wait for crash dump collection
4390 */
4391 if (instance->drv_buf_alloc) {
4392 dev_info(&instance->pdev->dev, "earlier crash dump is "
4393 "not yet copied by application, ignoring this "
4394 "crash dump and initiating OCR\n");
4395 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
4396 writel(status_reg,
4397 &instance->reg_set->outbound_scratch_pad);
4398 readl(&instance->reg_set->outbound_scratch_pad);
4399 return;
4400 }
4401 megasas_alloc_host_crash_buffer(instance);
4402 dev_info(&instance->pdev->dev, "Number of host crash buffers "
4403 "allocated: %d\n", instance->drv_buf_alloc);
4404 }
4405
4406 /*
4407 * Driver has allocated max buffers, which can be allocated
4408 * and FW has more crash dump data, then driver will
4409 * ignore the data.
4410 */
4411 if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
4412 dev_info(&instance->pdev->dev, "Driver is done copying "
4413 "the buffer: %d\n", instance->drv_buf_alloc);
4414 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
4415 partial_copy = 1;
4416 } else {
4417 memcpy(instance->crash_buf[instance->drv_buf_index],
4418 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
4419 instance->drv_buf_index++;
4420 status_reg &= ~MFI_STATE_DMADONE;
4421 }
4422
4423 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
4424 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
4425 "of copied buffers: %d\n", instance->drv_buf_index);
4426 instance->fw_crash_buffer_size = instance->drv_buf_index;
4427 instance->fw_crash_state = AVAILABLE;
4428 instance->drv_buf_index = 0;
4429 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
4430 readl(&instance->reg_set->outbound_scratch_pad);
4431 if (!partial_copy)
4432 megasas_reset_fusion(instance->host, 0);
4433 } else {
4434 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
4435 readl(&instance->reg_set->outbound_scratch_pad);
4436 }
4437}
4438
4439
adam radford9c915a82010-12-21 13:34:31 -08004440/* Fusion OCR work queue */
4441void megasas_fusion_ocr_wq(struct work_struct *work)
4442{
4443 struct megasas_instance *instance =
4444 container_of(work, struct megasas_instance, work_init);
4445
adam radford229fe472014-03-10 02:51:56 -07004446 megasas_reset_fusion(instance->host, 0);
adam radford9c915a82010-12-21 13:34:31 -08004447}
4448
Shivasharan S5fc499b2017-02-10 00:59:17 -08004449/* Allocate fusion context */
4450int
4451megasas_alloc_fusion_context(struct megasas_instance *instance)
4452{
4453 struct fusion_context *fusion;
4454
4455 instance->ctrl_context_pages = get_order(sizeof(struct fusion_context));
4456 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
4457 instance->ctrl_context_pages);
4458 if (!instance->ctrl_context) {
4459 /* fall back to using vmalloc for fusion_context */
4460 instance->ctrl_context = vzalloc(sizeof(struct fusion_context));
4461 if (!instance->ctrl_context) {
4462 dev_err(&instance->pdev->dev, "Failed from %s %d\n", __func__, __LINE__);
4463 return -ENOMEM;
4464 }
4465 }
4466
4467 fusion = instance->ctrl_context;
4468
4469 fusion->load_balance_info_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
4470 sizeof(struct LD_LOAD_BALANCE_INFO));
4471 fusion->load_balance_info =
4472 (struct LD_LOAD_BALANCE_INFO *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
4473 fusion->load_balance_info_pages);
4474 if (!fusion->load_balance_info) {
4475 fusion->load_balance_info = vzalloc(MAX_LOGICAL_DRIVES_EXT *
4476 sizeof(struct LD_LOAD_BALANCE_INFO));
4477 if (!fusion->load_balance_info)
4478 dev_err(&instance->pdev->dev, "Failed to allocate load_balance_info, "
4479 "continuing without Load Balance support\n");
4480 }
4481
4482 return 0;
4483}
4484
4485void
4486megasas_free_fusion_context(struct megasas_instance *instance)
4487{
4488 struct fusion_context *fusion = instance->ctrl_context;
4489
4490 if (fusion) {
4491 if (fusion->load_balance_info) {
4492 if (is_vmalloc_addr(fusion->load_balance_info))
4493 vfree(fusion->load_balance_info);
4494 else
4495 free_pages((ulong)fusion->load_balance_info,
4496 fusion->load_balance_info_pages);
4497 }
4498
4499 if (is_vmalloc_addr(fusion))
4500 vfree(fusion);
4501 else
4502 free_pages((ulong)fusion,
4503 instance->ctrl_context_pages);
4504 }
4505}
4506
adam radford9c915a82010-12-21 13:34:31 -08004507struct megasas_instance_template megasas_instance_template_fusion = {
adam radford9c915a82010-12-21 13:34:31 -08004508 .enable_intr = megasas_enable_intr_fusion,
4509 .disable_intr = megasas_disable_intr_fusion,
4510 .clear_intr = megasas_clear_intr_fusion,
4511 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
4512 .adp_reset = megasas_adp_reset_fusion,
4513 .check_reset = megasas_check_reset_fusion,
4514 .service_isr = megasas_isr_fusion,
4515 .tasklet = megasas_complete_cmd_dpc_fusion,
4516 .init_adapter = megasas_init_adapter_fusion,
4517 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
4518 .issue_dcmd = megasas_issue_dcmd_fusion,
4519};