blob: 46286719c0aaa16c48c63e85cbe845b6d89323d0 [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>
50
51#include <scsi/scsi.h>
52#include <scsi/scsi_cmnd.h>
53#include <scsi/scsi_device.h>
54#include <scsi/scsi_host.h>
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +053055#include <scsi/scsi_dbg.h>
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +053056#include <linux/dmi.h>
adam radford9c915a82010-12-21 13:34:31 -080057
58#include "megaraid_sas_fusion.h"
59#include "megaraid_sas.h"
60
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +053061
adam radford9c915a82010-12-21 13:34:31 -080062extern void megasas_free_cmds(struct megasas_instance *instance);
63extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
64 *instance);
65extern void
66megasas_complete_cmd(struct megasas_instance *instance,
67 struct megasas_cmd *cmd, u8 alt_status);
adam radford9c915a82010-12-21 13:34:31 -080068int
adam radford229fe472014-03-10 02:51:56 -070069wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
70 int seconds);
adam radford9c915a82010-12-21 13:34:31 -080071
72void
73megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
74int megasas_alloc_cmds(struct megasas_instance *instance);
75int
76megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
77int
78megasas_issue_polled(struct megasas_instance *instance,
79 struct megasas_cmd *cmd);
adam radford53ef2bb2011-02-24 20:56:05 -080080void
81megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
82
adam radford058a8fa2011-10-08 18:14:27 -070083int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
adam radford9c915a82010-12-21 13:34:31 -080084void megaraid_sas_kill_hba(struct megasas_instance *instance);
85
86extern u32 megasas_dbg_lvl;
adam radford229fe472014-03-10 02:51:56 -070087void megasas_sriov_heartbeat_handler(unsigned long instance_addr);
88int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
89 int initial);
90void megasas_start_timer(struct megasas_instance *instance,
91 struct timer_list *timer,
92 void *fn, unsigned long interval);
93extern struct megasas_mgmt_info megasas_mgmt_info;
Sumit Saxenae3d178c2016-01-28 21:04:34 +053094extern unsigned int resetwaittime;
Sumit Saxena308ec452016-01-28 21:04:30 +053095extern unsigned int dual_qdepth_disable;
Sumit Saxena179ac142016-01-28 21:04:28 +053096static void megasas_free_rdpq_fusion(struct megasas_instance *instance);
97static void megasas_free_reply_fusion(struct megasas_instance *instance);
adam radford9c915a82010-12-21 13:34:31 -080098
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +053099
100
adam radford9c915a82010-12-21 13:34:31 -0800101/**
102 * megasas_enable_intr_fusion - Enables interrupts
103 * @regs: MFI register set
104 */
105void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530106megasas_enable_intr_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800107{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530108 struct megasas_register_set __iomem *regs;
109 regs = instance->reg_set;
Sumit.Saxena@avagotech.comc2ced172015-01-05 20:06:13 +0530110
111 instance->mask_interrupts = 0;
adam radford6497b242011-10-08 18:14:50 -0700112 /* For Thunderbolt/Invader also clear intr on enable */
113 writel(~0, &regs->outbound_intr_status);
114 readl(&regs->outbound_intr_status);
115
adam radford9c915a82010-12-21 13:34:31 -0800116 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
117
118 /* Dummy readl to force pci flush */
119 readl(&regs->outbound_intr_mask);
120}
121
122/**
123 * megasas_disable_intr_fusion - Disables interrupt
124 * @regs: MFI register set
125 */
126void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530127megasas_disable_intr_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800128{
129 u32 mask = 0xFFFFFFFF;
130 u32 status;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530131 struct megasas_register_set __iomem *regs;
132 regs = instance->reg_set;
133 instance->mask_interrupts = 1;
adam radford9c915a82010-12-21 13:34:31 -0800134
135 writel(mask, &regs->outbound_intr_mask);
136 /* Dummy readl to force pci flush */
137 status = readl(&regs->outbound_intr_mask);
138}
139
140int
141megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
142{
143 u32 status;
144 /*
145 * Check if it is our interrupt
146 */
147 status = readl(&regs->outbound_intr_status);
148
149 if (status & 1) {
150 writel(status, &regs->outbound_intr_status);
151 readl(&regs->outbound_intr_status);
152 return 1;
153 }
154 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
155 return 0;
156
adam radford9c915a82010-12-21 13:34:31 -0800157 return 1;
158}
159
160/**
161 * megasas_get_cmd_fusion - Get a command from the free pool
162 * @instance: Adapter soft state
163 *
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530164 * Returns a blk_tag indexed mpt frame
adam radford9c915a82010-12-21 13:34:31 -0800165 */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530166inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
167 *instance, u32 blk_tag)
adam radford9c915a82010-12-21 13:34:31 -0800168{
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530169 struct fusion_context *fusion;
adam radford9c915a82010-12-21 13:34:31 -0800170
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530171 fusion = instance->ctrl_context;
172 return fusion->cmd_list[blk_tag];
adam radford9c915a82010-12-21 13:34:31 -0800173}
174
175/**
176 * megasas_return_cmd_fusion - Return a cmd to free command pool
177 * @instance: Adapter soft state
178 * @cmd: Command packet to be returned to free command pool
179 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530180inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
181 struct megasas_cmd_fusion *cmd)
adam radford9c915a82010-12-21 13:34:31 -0800182{
adam radford9c915a82010-12-21 13:34:31 -0800183 cmd->scmd = NULL;
Shivasharan S8bf7c652017-02-10 00:59:03 -0800184 memset(cmd->io_request, 0, MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
185 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
186 cmd->cmd_completed = false;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530187}
188
189/**
Christoph Hellwig01d7f032015-04-23 16:34:09 +0530190 * megasas_fire_cmd_fusion - Sends command to the FW
Shivasharan S45b8a352017-02-10 00:59:04 -0800191 * @instance: Adapter soft state
192 * @req_desc: 32bit or 64bit Request descriptor
193 *
194 * Perform PCI Write. Ventura supports 32 bit Descriptor.
195 * Prior to Ventura (12G) MR controller supports 64 bit Descriptor.
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530196 */
Shivasharan S45b8a352017-02-10 00:59:04 -0800197
Christoph Hellwig01d7f032015-04-23 16:34:09 +0530198static void
199megasas_fire_cmd_fusion(struct megasas_instance *instance,
Shivasharan S45b8a352017-02-10 00:59:04 -0800200 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530201{
Shivasharan S45b8a352017-02-10 00:59:04 -0800202 if (instance->is_ventura)
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500203 writel(le32_to_cpu(req_desc->u.low),
Shivasharan S45b8a352017-02-10 00:59:04 -0800204 &instance->reg_set->inbound_single_queue_port);
205 else {
206#if defined(writeq) && defined(CONFIG_64BIT)
207 u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
208 le32_to_cpu(req_desc->u.low));
209
210 writeq(req_data, &instance->reg_set->inbound_low_queue_port);
211#else
212 unsigned long flags;
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500213 spin_lock_irqsave(&instance->hba_lock, flags);
214 writel(le32_to_cpu(req_desc->u.low),
Shivasharan S45b8a352017-02-10 00:59:04 -0800215 &instance->reg_set->inbound_low_queue_port);
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500216 writel(le32_to_cpu(req_desc->u.high),
Shivasharan S45b8a352017-02-10 00:59:04 -0800217 &instance->reg_set->inbound_high_queue_port);
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500218 mmiowb();
219 spin_unlock_irqrestore(&instance->hba_lock, flags);
Christoph Hellwig01d7f032015-04-23 16:34:09 +0530220#endif
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500221 }
adam radford9c915a82010-12-21 13:34:31 -0800222}
223
adam radford9c915a82010-12-21 13:34:31 -0800224/**
Sumit Saxena308ec452016-01-28 21:04:30 +0530225 * megasas_fusion_update_can_queue - Do all Adapter Queue depth related calculations here
226 * @instance: Adapter soft state
227 * fw_boot_context: Whether this function called during probe or after OCR
228 *
229 * This function is only for fusion controllers.
230 * Update host can queue, if firmware downgrade max supported firmware commands.
231 * Firmware upgrade case will be skiped because underlying firmware has
232 * more resource than exposed to the OS.
233 *
234 */
235static void
236megasas_fusion_update_can_queue(struct megasas_instance *instance, int fw_boot_context)
237{
238 u16 cur_max_fw_cmds = 0;
239 u16 ldio_threshold = 0;
240 struct megasas_register_set __iomem *reg_set;
241
242 reg_set = instance->reg_set;
243
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -0500244 /* ventura FW does not fill outbound_scratch_pad_3 with queue depth */
245 if (!instance->is_ventura)
246 cur_max_fw_cmds =
247 readl(&instance->reg_set->outbound_scratch_pad_3) & 0x00FFFF;
Sumit Saxena308ec452016-01-28 21:04:30 +0530248
249 if (dual_qdepth_disable || !cur_max_fw_cmds)
250 cur_max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
251 else
252 ldio_threshold =
253 (instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF) - MEGASAS_FUSION_IOCTL_CMDS;
254
255 dev_info(&instance->pdev->dev,
256 "Current firmware maximum commands: %d\t LDIO threshold: %d\n",
257 cur_max_fw_cmds, ldio_threshold);
258
259 if (fw_boot_context == OCR_CONTEXT) {
260 cur_max_fw_cmds = cur_max_fw_cmds - 1;
261 if (cur_max_fw_cmds <= instance->max_fw_cmds) {
262 instance->cur_can_queue =
263 cur_max_fw_cmds - (MEGASAS_FUSION_INTERNAL_CMDS +
264 MEGASAS_FUSION_IOCTL_CMDS);
265 instance->host->can_queue = instance->cur_can_queue;
266 instance->ldio_threshold = ldio_threshold;
267 }
268 } else {
269 instance->max_fw_cmds = cur_max_fw_cmds;
270 instance->ldio_threshold = ldio_threshold;
271
272 if (!instance->is_rdpq)
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500273 instance->max_fw_cmds =
274 min_t(u16, instance->max_fw_cmds, 1024);
Sumit Saxena308ec452016-01-28 21:04:30 +0530275
Sumit Saxenac3e385a2016-04-15 00:23:30 -0700276 if (reset_devices)
277 instance->max_fw_cmds = min(instance->max_fw_cmds,
278 (u16)MEGASAS_KDUMP_QUEUE_DEPTH);
Sumit Saxena308ec452016-01-28 21:04:30 +0530279 /*
280 * Reduce the max supported cmds by 1. This is to ensure that the
281 * reply_q_sz (1 more than the max cmd that driver may send)
282 * does not exceed max cmds that the FW can support
283 */
284 instance->max_fw_cmds = instance->max_fw_cmds-1;
285
286 instance->max_scsi_cmds = instance->max_fw_cmds -
287 (MEGASAS_FUSION_INTERNAL_CMDS +
288 MEGASAS_FUSION_IOCTL_CMDS);
289 instance->cur_can_queue = instance->max_scsi_cmds;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500290 instance->host->can_queue = instance->cur_can_queue;
Sumit Saxena308ec452016-01-28 21:04:30 +0530291 }
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500292
293 if (instance->is_ventura)
294 instance->max_mpt_cmds =
295 instance->max_fw_cmds * RAID_1_10_RMW_CMDS;
296 else
297 instance->max_mpt_cmds = instance->max_fw_cmds;
Sumit Saxena308ec452016-01-28 21:04:30 +0530298}
299/**
adam radford9c915a82010-12-21 13:34:31 -0800300 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
301 * @instance: Adapter soft state
302 */
303void
304megasas_free_cmds_fusion(struct megasas_instance *instance)
305{
306 int i;
307 struct fusion_context *fusion = instance->ctrl_context;
Sumit Saxena179ac142016-01-28 21:04:28 +0530308 struct megasas_cmd_fusion *cmd;
adam radford9c915a82010-12-21 13:34:31 -0800309
Sumit Saxena179ac142016-01-28 21:04:28 +0530310 /* SG, Sense */
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500311 for (i = 0; i < instance->max_mpt_cmds; i++) {
Sumit Saxena179ac142016-01-28 21:04:28 +0530312 cmd = fusion->cmd_list[i];
313 if (cmd) {
314 if (cmd->sg_frame)
315 pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
316 cmd->sg_frame_phys_addr);
317 if (cmd->sense)
318 pci_pool_free(fusion->sense_dma_pool, cmd->sense,
319 cmd->sense_phys_addr);
320 }
321 }
322
323 if (fusion->sg_dma_pool) {
324 pci_pool_destroy(fusion->sg_dma_pool);
325 fusion->sg_dma_pool = NULL;
326 }
327 if (fusion->sense_dma_pool) {
328 pci_pool_destroy(fusion->sense_dma_pool);
329 fusion->sense_dma_pool = NULL;
330 }
adam radford9c915a82010-12-21 13:34:31 -0800331
332
Sumit Saxena179ac142016-01-28 21:04:28 +0530333 /* Reply Frame, Desc*/
334 if (instance->is_rdpq)
335 megasas_free_rdpq_fusion(instance);
336 else
337 megasas_free_reply_fusion(instance);
adam radford9c915a82010-12-21 13:34:31 -0800338
Sumit Saxena179ac142016-01-28 21:04:28 +0530339 /* Request Frame, Desc*/
adam radford9c915a82010-12-21 13:34:31 -0800340 if (fusion->req_frames_desc)
Sumit Saxena179ac142016-01-28 21:04:28 +0530341 dma_free_coherent(&instance->pdev->dev,
342 fusion->request_alloc_sz, fusion->req_frames_desc,
343 fusion->req_frames_desc_phys);
344 if (fusion->io_request_frames)
adam radford9c915a82010-12-21 13:34:31 -0800345 pci_pool_free(fusion->io_request_frames_pool,
Sumit Saxena179ac142016-01-28 21:04:28 +0530346 fusion->io_request_frames,
347 fusion->io_request_frames_phys);
348 if (fusion->io_request_frames_pool) {
adam radford9c915a82010-12-21 13:34:31 -0800349 pci_pool_destroy(fusion->io_request_frames_pool);
Sumit Saxena179ac142016-01-28 21:04:28 +0530350 fusion->io_request_frames_pool = NULL;
adam radford9c915a82010-12-21 13:34:31 -0800351 }
352
adam radford9c915a82010-12-21 13:34:31 -0800353
Sumit Saxena179ac142016-01-28 21:04:28 +0530354 /* cmd_list */
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500355 for (i = 0; i < instance->max_mpt_cmds; i++)
adam radford9c915a82010-12-21 13:34:31 -0800356 kfree(fusion->cmd_list[i]);
357
adam radford9c915a82010-12-21 13:34:31 -0800358 kfree(fusion->cmd_list);
adam radford9c915a82010-12-21 13:34:31 -0800359}
360
361/**
Sumit Saxena179ac142016-01-28 21:04:28 +0530362 * megasas_create_sg_sense_fusion - Creates DMA pool for cmd frames
adam radford9c915a82010-12-21 13:34:31 -0800363 * @instance: Adapter soft state
364 *
365 */
Sumit Saxena179ac142016-01-28 21:04:28 +0530366static int megasas_create_sg_sense_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800367{
368 int i;
369 u32 max_cmd;
370 struct fusion_context *fusion;
371 struct megasas_cmd_fusion *cmd;
adam radford9c915a82010-12-21 13:34:31 -0800372
373 fusion = instance->ctrl_context;
374 max_cmd = instance->max_fw_cmds;
375
adam radford9c915a82010-12-21 13:34:31 -0800376
Sumit Saxena179ac142016-01-28 21:04:28 +0530377 fusion->sg_dma_pool =
378 pci_pool_create("mr_sg", instance->pdev,
Shivasharan S15dd0382017-02-10 00:59:10 -0800379 instance->max_chain_frame_sz,
380 MR_DEFAULT_NVME_PAGE_SIZE, 0);
Sumit Saxena179ac142016-01-28 21:04:28 +0530381 /* SCSI_SENSE_BUFFERSIZE = 96 bytes */
382 fusion->sense_dma_pool =
383 pci_pool_create("mr_sense", instance->pdev,
384 SCSI_SENSE_BUFFERSIZE, 64, 0);
adam radford9c915a82010-12-21 13:34:31 -0800385
Sumit Saxena179ac142016-01-28 21:04:28 +0530386 if (!fusion->sense_dma_pool || !fusion->sg_dma_pool) {
387 dev_err(&instance->pdev->dev,
388 "Failed from %s %d\n", __func__, __LINE__);
adam radford9c915a82010-12-21 13:34:31 -0800389 return -ENOMEM;
390 }
391
392 /*
393 * Allocate and attach a frame to each of the commands in cmd_list
394 */
395 for (i = 0; i < max_cmd; i++) {
adam radford9c915a82010-12-21 13:34:31 -0800396 cmd = fusion->cmd_list[i];
adam radford9c915a82010-12-21 13:34:31 -0800397 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
Sumit Saxena179ac142016-01-28 21:04:28 +0530398 GFP_KERNEL, &cmd->sg_frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -0800399
400 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
Sumit Saxena179ac142016-01-28 21:04:28 +0530401 GFP_KERNEL, &cmd->sense_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -0800402 if (!cmd->sg_frame || !cmd->sense) {
Sumit Saxena179ac142016-01-28 21:04:28 +0530403 dev_err(&instance->pdev->dev,
404 "Failed from %s %d\n", __func__, __LINE__);
adam radford9c915a82010-12-21 13:34:31 -0800405 return -ENOMEM;
406 }
407 }
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500408
409 /* create sense buffer for the raid 1/10 fp */
410 for (i = max_cmd; i < instance->max_mpt_cmds; i++) {
411 cmd = fusion->cmd_list[i];
412 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
413 GFP_KERNEL, &cmd->sense_phys_addr);
414 if (!cmd->sense) {
415 dev_err(&instance->pdev->dev,
416 "Failed from %s %d\n", __func__, __LINE__);
417 return -ENOMEM;
418 }
419 }
420
adam radford9c915a82010-12-21 13:34:31 -0800421 return 0;
422}
423
Sumit Saxena179ac142016-01-28 21:04:28 +0530424int
425megasas_alloc_cmdlist_fusion(struct megasas_instance *instance)
426{
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500427 u32 max_mpt_cmd, i;
Sumit Saxena179ac142016-01-28 21:04:28 +0530428 struct fusion_context *fusion;
429
430 fusion = instance->ctrl_context;
431
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500432 max_mpt_cmd = instance->max_mpt_cmds;
Sumit Saxena179ac142016-01-28 21:04:28 +0530433
434 /*
435 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
436 * Allocate the dynamic array first and then allocate individual
437 * commands.
438 */
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500439 fusion->cmd_list =
440 kzalloc(sizeof(struct megasas_cmd_fusion *) * max_mpt_cmd,
441 GFP_KERNEL);
Sumit Saxena179ac142016-01-28 21:04:28 +0530442 if (!fusion->cmd_list) {
443 dev_err(&instance->pdev->dev,
444 "Failed from %s %d\n", __func__, __LINE__);
445 return -ENOMEM;
446 }
447
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500448
449
450 for (i = 0; i < max_mpt_cmd; i++) {
Sumit Saxena179ac142016-01-28 21:04:28 +0530451 fusion->cmd_list[i] = kzalloc(sizeof(struct megasas_cmd_fusion),
452 GFP_KERNEL);
453 if (!fusion->cmd_list[i]) {
454 dev_err(&instance->pdev->dev,
455 "Failed from %s %d\n", __func__, __LINE__);
456 return -ENOMEM;
457 }
458 }
459 return 0;
460}
461int
462megasas_alloc_request_fusion(struct megasas_instance *instance)
463{
464 struct fusion_context *fusion;
465
466 fusion = instance->ctrl_context;
467
468 fusion->req_frames_desc =
469 dma_alloc_coherent(&instance->pdev->dev,
470 fusion->request_alloc_sz,
471 &fusion->req_frames_desc_phys, GFP_KERNEL);
472 if (!fusion->req_frames_desc) {
473 dev_err(&instance->pdev->dev,
474 "Failed from %s %d\n", __func__, __LINE__);
475 return -ENOMEM;
476 }
477
478 fusion->io_request_frames_pool =
479 pci_pool_create("mr_ioreq", instance->pdev,
480 fusion->io_frames_alloc_sz, 16, 0);
481
482 if (!fusion->io_request_frames_pool) {
483 dev_err(&instance->pdev->dev,
484 "Failed from %s %d\n", __func__, __LINE__);
485 return -ENOMEM;
486 }
487
488 fusion->io_request_frames =
489 pci_pool_alloc(fusion->io_request_frames_pool,
490 GFP_KERNEL, &fusion->io_request_frames_phys);
491 if (!fusion->io_request_frames) {
492 dev_err(&instance->pdev->dev,
493 "Failed from %s %d\n", __func__, __LINE__);
494 return -ENOMEM;
495 }
496 return 0;
497}
498
499int
500megasas_alloc_reply_fusion(struct megasas_instance *instance)
501{
502 int i, count;
503 struct fusion_context *fusion;
504 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
505 fusion = instance->ctrl_context;
506
507 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
508 fusion->reply_frames_desc_pool =
509 pci_pool_create("mr_reply", instance->pdev,
510 fusion->reply_alloc_sz * count, 16, 0);
511
512 if (!fusion->reply_frames_desc_pool) {
513 dev_err(&instance->pdev->dev,
514 "Failed from %s %d\n", __func__, __LINE__);
515 return -ENOMEM;
516 }
517
518 fusion->reply_frames_desc[0] =
519 pci_pool_alloc(fusion->reply_frames_desc_pool,
520 GFP_KERNEL, &fusion->reply_frames_desc_phys[0]);
521 if (!fusion->reply_frames_desc[0]) {
522 dev_err(&instance->pdev->dev,
523 "Failed from %s %d\n", __func__, __LINE__);
524 return -ENOMEM;
525 }
526 reply_desc = fusion->reply_frames_desc[0];
527 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
528 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
529
530 /* This is not a rdpq mode, but driver still populate
531 * reply_frame_desc array to use same msix index in ISR path.
532 */
533 for (i = 0; i < (count - 1); i++)
534 fusion->reply_frames_desc[i + 1] =
535 fusion->reply_frames_desc[i] +
536 (fusion->reply_alloc_sz)/sizeof(union MPI2_REPLY_DESCRIPTORS_UNION);
537
538 return 0;
539}
540
541int
542megasas_alloc_rdpq_fusion(struct megasas_instance *instance)
543{
544 int i, j, count;
545 struct fusion_context *fusion;
546 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
547
548 fusion = instance->ctrl_context;
549
550 fusion->rdpq_virt = pci_alloc_consistent(instance->pdev,
551 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
552 &fusion->rdpq_phys);
553 if (!fusion->rdpq_virt) {
554 dev_err(&instance->pdev->dev,
555 "Failed from %s %d\n", __func__, __LINE__);
556 return -ENOMEM;
557 }
558
559 memset(fusion->rdpq_virt, 0,
560 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION);
561 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
562 fusion->reply_frames_desc_pool = pci_pool_create("mr_rdpq",
563 instance->pdev, fusion->reply_alloc_sz, 16, 0);
564
565 if (!fusion->reply_frames_desc_pool) {
566 dev_err(&instance->pdev->dev,
567 "Failed from %s %d\n", __func__, __LINE__);
568 return -ENOMEM;
569 }
570
571 for (i = 0; i < count; i++) {
572 fusion->reply_frames_desc[i] =
573 pci_pool_alloc(fusion->reply_frames_desc_pool,
574 GFP_KERNEL, &fusion->reply_frames_desc_phys[i]);
575 if (!fusion->reply_frames_desc[i]) {
576 dev_err(&instance->pdev->dev,
577 "Failed from %s %d\n", __func__, __LINE__);
578 return -ENOMEM;
579 }
580
581 fusion->rdpq_virt[i].RDPQBaseAddress =
582 fusion->reply_frames_desc_phys[i];
583
584 reply_desc = fusion->reply_frames_desc[i];
585 for (j = 0; j < fusion->reply_q_depth; j++, reply_desc++)
586 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
587 }
588 return 0;
589}
590
591static void
592megasas_free_rdpq_fusion(struct megasas_instance *instance) {
593
594 int i;
595 struct fusion_context *fusion;
596
597 fusion = instance->ctrl_context;
598
599 for (i = 0; i < MAX_MSIX_QUEUES_FUSION; i++) {
600 if (fusion->reply_frames_desc[i])
601 pci_pool_free(fusion->reply_frames_desc_pool,
602 fusion->reply_frames_desc[i],
603 fusion->reply_frames_desc_phys[i]);
604 }
605
606 if (fusion->reply_frames_desc_pool)
607 pci_pool_destroy(fusion->reply_frames_desc_pool);
608
609 if (fusion->rdpq_virt)
610 pci_free_consistent(instance->pdev,
611 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
612 fusion->rdpq_virt, fusion->rdpq_phys);
613}
614
615static void
616megasas_free_reply_fusion(struct megasas_instance *instance) {
617
618 struct fusion_context *fusion;
619
620 fusion = instance->ctrl_context;
621
622 if (fusion->reply_frames_desc[0])
623 pci_pool_free(fusion->reply_frames_desc_pool,
624 fusion->reply_frames_desc[0],
625 fusion->reply_frames_desc_phys[0]);
626
627 if (fusion->reply_frames_desc_pool)
628 pci_pool_destroy(fusion->reply_frames_desc_pool);
629
630}
631
632
adam radford9c915a82010-12-21 13:34:31 -0800633/**
634 * megasas_alloc_cmds_fusion - Allocates the command packets
635 * @instance: Adapter soft state
636 *
637 *
638 * Each frame has a 32-bit field called context. This context is used to get
639 * back the megasas_cmd_fusion from the frame when a frame gets completed
640 * In this driver, the 32 bit values are the indices into an array cmd_list.
641 * This array is used only to look up the megasas_cmd_fusion given the context.
642 * The free commands themselves are maintained in a linked list called cmd_pool.
643 *
644 * cmds are formed in the io_request and sg_frame members of the
645 * megasas_cmd_fusion. The context field is used to get a request descriptor
646 * and is used as SMID of the cmd.
647 * SMID value range is from 1 to max_fw_cmds.
648 */
649int
650megasas_alloc_cmds_fusion(struct megasas_instance *instance)
651{
Sumit Saxena179ac142016-01-28 21:04:28 +0530652 int i;
adam radford9c915a82010-12-21 13:34:31 -0800653 struct fusion_context *fusion;
654 struct megasas_cmd_fusion *cmd;
adam radford9c915a82010-12-21 13:34:31 -0800655 u32 offset;
656 dma_addr_t io_req_base_phys;
657 u8 *io_req_base;
658
Sumit Saxena179ac142016-01-28 21:04:28 +0530659
adam radford9c915a82010-12-21 13:34:31 -0800660 fusion = instance->ctrl_context;
661
Sumit Saxena179ac142016-01-28 21:04:28 +0530662 if (megasas_alloc_cmdlist_fusion(instance))
663 goto fail_exit;
adam radford9c915a82010-12-21 13:34:31 -0800664
Sumit Saxena179ac142016-01-28 21:04:28 +0530665 if (megasas_alloc_request_fusion(instance))
666 goto fail_exit;
adam radford9c915a82010-12-21 13:34:31 -0800667
Sumit Saxena179ac142016-01-28 21:04:28 +0530668 if (instance->is_rdpq) {
669 if (megasas_alloc_rdpq_fusion(instance))
670 goto fail_exit;
671 } else
672 if (megasas_alloc_reply_fusion(instance))
673 goto fail_exit;
adam radford9c915a82010-12-21 13:34:31 -0800674
adam radford9c915a82010-12-21 13:34:31 -0800675
Sumit Saxena179ac142016-01-28 21:04:28 +0530676 /* The first 256 bytes (SMID 0) is not used. Don't add to the cmd list */
677 io_req_base = fusion->io_request_frames + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
678 io_req_base_phys = fusion->io_request_frames_phys + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
adam radford9c915a82010-12-21 13:34:31 -0800679
680 /*
681 * Add all the commands to command pool (fusion->cmd_pool)
682 */
683
684 /* SMID 0 is reserved. Set SMID/index from 1 */
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500685 for (i = 0; i < instance->max_mpt_cmds; i++) {
adam radford9c915a82010-12-21 13:34:31 -0800686 cmd = fusion->cmd_list[i];
687 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
688 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
689 cmd->index = i + 1;
690 cmd->scmd = NULL;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500691 cmd->sync_cmd_idx =
692 (i >= instance->max_scsi_cmds && i < instance->max_fw_cmds) ?
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530693 (i - instance->max_scsi_cmds) :
694 (u32)ULONG_MAX; /* Set to Invalid */
adam radford9c915a82010-12-21 13:34:31 -0800695 cmd->instance = instance;
696 cmd->io_request =
697 (struct MPI2_RAID_SCSI_IO_REQUEST *)
698 (io_req_base + offset);
699 memset(cmd->io_request, 0,
700 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
701 cmd->io_request_phys_addr = io_req_base_phys + offset;
Shivasharan S8bf7c652017-02-10 00:59:03 -0800702 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
adam radford9c915a82010-12-21 13:34:31 -0800703 }
704
Sumit Saxena179ac142016-01-28 21:04:28 +0530705 if (megasas_create_sg_sense_fusion(instance))
706 goto fail_exit;
adam radford9c915a82010-12-21 13:34:31 -0800707
708 return 0;
709
Sumit Saxena179ac142016-01-28 21:04:28 +0530710fail_exit:
711 megasas_free_cmds_fusion(instance);
adam radford9c915a82010-12-21 13:34:31 -0800712 return -ENOMEM;
713}
714
715/**
716 * wait_and_poll - Issues a polling command
717 * @instance: Adapter soft state
718 * @cmd: Command packet to be issued
719 *
720 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
721 */
722int
adam radford229fe472014-03-10 02:51:56 -0700723wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
724 int seconds)
adam radford9c915a82010-12-21 13:34:31 -0800725{
726 int i;
727 struct megasas_header *frame_hdr = &cmd->frame->hdr;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530728 struct fusion_context *fusion;
adam radford9c915a82010-12-21 13:34:31 -0800729
adam radford229fe472014-03-10 02:51:56 -0700730 u32 msecs = seconds * 1000;
adam radford9c915a82010-12-21 13:34:31 -0800731
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530732 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -0800733 /*
734 * Wait for cmd_status to change
735 */
736 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
737 rmb();
738 msleep(20);
739 }
740
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530741 if (frame_hdr->cmd_status == MFI_STAT_INVALID_STATUS)
742 return DCMD_TIMEOUT;
743 else if (frame_hdr->cmd_status == MFI_STAT_OK)
744 return DCMD_SUCCESS;
745 else
746 return DCMD_FAILED;
adam radford9c915a82010-12-21 13:34:31 -0800747}
748
749/**
750 * megasas_ioc_init_fusion - Initializes the FW
751 * @instance: Adapter soft state
752 *
753 * Issues the IOC Init cmd
754 */
755int
756megasas_ioc_init_fusion(struct megasas_instance *instance)
757{
758 struct megasas_init_frame *init_frame;
Sumit Saxena179ac142016-01-28 21:04:28 +0530759 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage = NULL;
adam radford9c915a82010-12-21 13:34:31 -0800760 dma_addr_t ioc_init_handle;
adam radford9c915a82010-12-21 13:34:31 -0800761 struct megasas_cmd *cmd;
Sumit Saxena179ac142016-01-28 21:04:28 +0530762 u8 ret, cur_rdpq_mode;
adam radford9c915a82010-12-21 13:34:31 -0800763 struct fusion_context *fusion;
adam radfordc77a9bd2014-03-10 02:51:36 -0700764 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
adam radford9c915a82010-12-21 13:34:31 -0800765 int i;
766 struct megasas_header *frame_hdr;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +0530767 const char *sys_info;
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530768 MFI_CAPABILITIES *drv_ops;
Sumit Saxena179ac142016-01-28 21:04:28 +0530769 u32 scratch_pad_2;
Shivasharan S45b8a352017-02-10 00:59:04 -0800770 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -0800771
772 fusion = instance->ctrl_context;
773
774 cmd = megasas_get_cmd(instance);
775
776 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500777 dev_err(&instance->pdev->dev, "Could not allocate cmd for INIT Frame\n");
adam radford9c915a82010-12-21 13:34:31 -0800778 ret = 1;
779 goto fail_get_cmd;
780 }
781
Sumit Saxena179ac142016-01-28 21:04:28 +0530782 scratch_pad_2 = readl
783 (&instance->reg_set->outbound_scratch_pad_2);
784
785 cur_rdpq_mode = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ? 1 : 0;
786
787 if (instance->is_rdpq && !cur_rdpq_mode) {
788 dev_err(&instance->pdev->dev, "Firmware downgrade *NOT SUPPORTED*"
789 " from RDPQ mode to non RDPQ mode\n");
790 ret = 1;
791 goto fail_fw_init;
792 }
793
Kashyap Desaid0fc91d2016-10-21 06:33:33 -0700794 instance->fw_sync_cache_support = (scratch_pad_2 &
795 MR_CAN_HANDLE_SYNC_CACHE_OFFSET) ? 1 : 0;
796 dev_info(&instance->pdev->dev, "FW supports sync cache\t: %s\n",
797 instance->fw_sync_cache_support ? "Yes" : "No");
798
adam radford9c915a82010-12-21 13:34:31 -0800799 IOCInitMessage =
800 dma_alloc_coherent(&instance->pdev->dev,
801 sizeof(struct MPI2_IOC_INIT_REQUEST),
802 &ioc_init_handle, GFP_KERNEL);
803
804 if (!IOCInitMessage) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500805 dev_err(&instance->pdev->dev, "Could not allocate memory for "
adam radford9c915a82010-12-21 13:34:31 -0800806 "IOCInitMessage\n");
807 ret = 1;
808 goto fail_fw_init;
809 }
810
811 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
812
813 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
814 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530815 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
816 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
817 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
adam radford9c915a82010-12-21 13:34:31 -0800818
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530819 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
Sumit Saxena179ac142016-01-28 21:04:28 +0530820 IOCInitMessage->ReplyDescriptorPostQueueAddress = instance->is_rdpq ?
821 cpu_to_le64(fusion->rdpq_phys) :
822 cpu_to_le64(fusion->reply_frames_desc_phys[0]);
823 IOCInitMessage->MsgFlags = instance->is_rdpq ?
824 MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE : 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530825 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
adam radford5738f992012-03-19 19:49:53 -0700826 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
Shivasharan S15dd0382017-02-10 00:59:10 -0800827 IOCInitMessage->HostPageSize = MR_DEFAULT_NVME_PAGE_SHIFT;
adam radford9c915a82010-12-21 13:34:31 -0800828 init_frame = (struct megasas_init_frame *)cmd->frame;
829 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
830
831 frame_hdr = &cmd->frame->hdr;
adam radford9c915a82010-12-21 13:34:31 -0800832 frame_hdr->cmd_status = 0xFF;
Christoph Hellwig48433bd2015-04-23 16:33:39 +0530833 frame_hdr->flags = cpu_to_le16(
834 le16_to_cpu(frame_hdr->flags) |
835 MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
adam radford9c915a82010-12-21 13:34:31 -0800836
837 init_frame->cmd = MFI_CMD_INIT;
838 init_frame->cmd_status = 0xFF;
839
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530840 drv_ops = (MFI_CAPABILITIES *) &(init_frame->driver_operations);
841
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530842 /* driver support Extended MSIX */
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -0500843 if (fusion->adapter_type >= INVADER_SERIES)
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530844 drv_ops->mfi_capabilities.support_additional_msix = 1;
adam radford21c9e162013-09-06 15:27:14 -0700845 /* driver supports HA / Remote LUN over Fast Path interface */
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530846 drv_ops->mfi_capabilities.support_fp_remote_lun = 1;
847
848 drv_ops->mfi_capabilities.support_max_255lds = 1;
849 drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1;
850 drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1;
851
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +0530852 if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
853 drv_ops->mfi_capabilities.support_ext_io_size = 1;
854
Sumit Saxena8f050242016-01-28 21:04:27 +0530855 drv_ops->mfi_capabilities.support_fp_rlbypass = 1;
Sumit Saxena308ec452016-01-28 21:04:30 +0530856 if (!dual_qdepth_disable)
857 drv_ops->mfi_capabilities.support_ext_queue_depth = 1;
Sumit Saxena8f050242016-01-28 21:04:27 +0530858
Sumit Saxena52b62ac2016-01-28 21:04:31 +0530859 drv_ops->mfi_capabilities.support_qd_throttling = 1;
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -0500860 drv_ops->mfi_capabilities.support_pd_map_target_id = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530861 /* Convert capability to LE32 */
862 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
adam radford21c9e162013-09-06 15:27:14 -0700863
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +0530864 sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
865 if (instance->system_info_buf && sys_info) {
866 memcpy(instance->system_info_buf->systemId, sys_info,
867 strlen(sys_info) > 64 ? 64 : strlen(sys_info));
868 instance->system_info_buf->systemIdLength =
869 strlen(sys_info) > 64 ? 64 : strlen(sys_info);
870 init_frame->system_info_lo = instance->system_info_h;
871 init_frame->system_info_hi = 0;
872 }
873
Hannes Reineckefdc5a972014-01-16 11:25:33 +0100874 init_frame->queue_info_new_phys_addr_hi =
875 cpu_to_le32(upper_32_bits(ioc_init_handle));
876 init_frame->queue_info_new_phys_addr_lo =
877 cpu_to_le32(lower_32_bits(ioc_init_handle));
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530878 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
adam radford9c915a82010-12-21 13:34:31 -0800879
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +0530880 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
881 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
adam radfordc77a9bd2014-03-10 02:51:36 -0700882 req_desc.MFAIo.RequestFlags =
adam radford9c915a82010-12-21 13:34:31 -0800883 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +0530884 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford9c915a82010-12-21 13:34:31 -0800885
886 /*
887 * disable the intr before firing the init frame
888 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530889 instance->instancet->disable_intr(instance);
adam radford9c915a82010-12-21 13:34:31 -0800890
891 for (i = 0; i < (10 * 1000); i += 20) {
892 if (readl(&instance->reg_set->doorbell) & 1)
893 msleep(20);
894 else
895 break;
896 }
897
Shivasharan S45b8a352017-02-10 00:59:04 -0800898 /* For Ventura also IOC INIT required 64 bit Descriptor write. */
899 spin_lock_irqsave(&instance->hba_lock, flags);
900 writel(le32_to_cpu(req_desc.u.low),
901 &instance->reg_set->inbound_low_queue_port);
902 writel(le32_to_cpu(req_desc.u.high),
903 &instance->reg_set->inbound_high_queue_port);
904 mmiowb();
905 spin_unlock_irqrestore(&instance->hba_lock, flags);
adam radford9c915a82010-12-21 13:34:31 -0800906
adam radford229fe472014-03-10 02:51:56 -0700907 wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -0800908
909 frame_hdr = &cmd->frame->hdr;
910 if (frame_hdr->cmd_status != 0) {
911 ret = 1;
912 goto fail_fw_init;
913 }
Andy Lutomirski13f30772016-05-03 10:24:31 -0700914 dev_info(&instance->pdev->dev, "Init cmd success\n");
adam radford9c915a82010-12-21 13:34:31 -0800915
916 ret = 0;
917
918fail_fw_init:
919 megasas_return_cmd(instance, cmd);
920 if (IOCInitMessage)
921 dma_free_coherent(&instance->pdev->dev,
922 sizeof(struct MPI2_IOC_INIT_REQUEST),
923 IOCInitMessage, ioc_init_handle);
924fail_get_cmd:
925 return ret;
926}
927
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +0530928/**
929 * megasas_sync_pd_seq_num - JBOD SEQ MAP
930 * @instance: Adapter soft state
931 * @pend: set to 1, if it is pended jbod map.
932 *
933 * Issue Jbod map to the firmware. If it is pended command,
934 * issue command and return. If it is first instance of jbod map
935 * issue and receive command.
936 */
937int
938megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) {
939 int ret = 0;
940 u32 pd_seq_map_sz;
941 struct megasas_cmd *cmd;
942 struct megasas_dcmd_frame *dcmd;
943 struct fusion_context *fusion = instance->ctrl_context;
944 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
945 dma_addr_t pd_seq_h;
946
947 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)];
948 pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)];
949 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
950 (sizeof(struct MR_PD_CFG_SEQ) *
951 (MAX_PHYSICAL_DEVICES - 1));
952
953 cmd = megasas_get_cmd(instance);
954 if (!cmd) {
955 dev_err(&instance->pdev->dev,
956 "Could not get mfi cmd. Fail from %s %d\n",
957 __func__, __LINE__);
958 return -ENOMEM;
959 }
960
961 dcmd = &cmd->frame->dcmd;
962
963 memset(pd_sync, 0, pd_seq_map_sz);
964 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
965 dcmd->cmd = MFI_CMD_DCMD;
966 dcmd->cmd_status = 0xFF;
967 dcmd->sge_count = 1;
968 dcmd->timeout = 0;
969 dcmd->pad_0 = 0;
970 dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz);
971 dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
972 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(pd_seq_h);
973 dcmd->sgl.sge32[0].length = cpu_to_le32(pd_seq_map_sz);
974
975 if (pend) {
976 dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG;
977 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
978 instance->jbod_seq_cmd = cmd;
979 instance->instancet->issue_dcmd(instance, cmd);
980 return 0;
981 }
982
983 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
984
985 /* Below code is only for non pended DCMD */
986 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530987 ret = megasas_issue_blocked_cmd(instance, cmd,
988 MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +0530989 else
990 ret = megasas_issue_polled(instance, cmd);
991
992 if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
993 dev_warn(&instance->pdev->dev,
994 "driver supports max %d JBOD, but FW reports %d\n",
995 MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count));
996 ret = -EINVAL;
997 }
998
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530999 if (ret == DCMD_TIMEOUT && instance->ctrl_context)
1000 megaraid_sas_kill_hba(instance);
1001
1002 if (ret == DCMD_SUCCESS)
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301003 instance->pd_seq_map_id++;
1004
1005 megasas_return_cmd(instance, cmd);
1006 return ret;
1007}
1008
adam radford9c915a82010-12-21 13:34:31 -08001009/*
adam radford9c915a82010-12-21 13:34:31 -08001010 * megasas_get_ld_map_info - Returns FW's ld_map structure
1011 * @instance: Adapter soft state
1012 * @pend: Pend the command or not
1013 * Issues an internal command (DCMD) to get the FW's controller PD
1014 * list structure. This information is mainly used to find out SYSTEM
1015 * supported by the FW.
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301016 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
1017 * dcmd.mbox.b[0] - number of LDs being sync'd
1018 * dcmd.mbox.b[1] - 0 - complete command immediately.
1019 * - 1 - pend till config change
1020 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
1021 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
1022 * uses extended struct MR_FW_RAID_MAP_EXT
adam radford9c915a82010-12-21 13:34:31 -08001023 */
1024static int
1025megasas_get_ld_map_info(struct megasas_instance *instance)
1026{
1027 int ret = 0;
1028 struct megasas_cmd *cmd;
1029 struct megasas_dcmd_frame *dcmd;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301030 void *ci;
adam radford9c915a82010-12-21 13:34:31 -08001031 dma_addr_t ci_h = 0;
1032 u32 size_map_info;
1033 struct fusion_context *fusion;
1034
1035 cmd = megasas_get_cmd(instance);
1036
1037 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001038 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n");
adam radford9c915a82010-12-21 13:34:31 -08001039 return -ENOMEM;
1040 }
1041
1042 fusion = instance->ctrl_context;
1043
1044 if (!fusion) {
1045 megasas_return_cmd(instance, cmd);
Hannes Reinecke2f8bdfa2014-01-16 11:25:35 +01001046 return -ENXIO;
adam radford9c915a82010-12-21 13:34:31 -08001047 }
1048
1049 dcmd = &cmd->frame->dcmd;
1050
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301051 size_map_info = fusion->current_map_sz;
adam radford9c915a82010-12-21 13:34:31 -08001052
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301053 ci = (void *) fusion->ld_map[(instance->map_id & 1)];
adam radford9c915a82010-12-21 13:34:31 -08001054 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
1055
1056 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001057 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n");
adam radford9c915a82010-12-21 13:34:31 -08001058 megasas_return_cmd(instance, cmd);
1059 return -ENOMEM;
1060 }
1061
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301062 memset(ci, 0, fusion->max_map_sz);
adam radford9c915a82010-12-21 13:34:31 -08001063 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301064#if VD_EXT_DEBUG
1065 dev_dbg(&instance->pdev->dev,
1066 "%s sending MR_DCMD_LD_MAP_GET_INFO with size %d\n",
1067 __func__, cpu_to_le32(size_map_info));
1068#endif
adam radford9c915a82010-12-21 13:34:31 -08001069 dcmd->cmd = MFI_CMD_DCMD;
1070 dcmd->cmd_status = 0xFF;
1071 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301072 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford9c915a82010-12-21 13:34:31 -08001073 dcmd->timeout = 0;
1074 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301075 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1076 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1077 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
1078 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -08001079
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301080 if (instance->ctrl_context && !instance->mask_interrupts)
1081 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301082 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301083 else
1084 ret = megasas_issue_polled(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08001085
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301086 if (ret == DCMD_TIMEOUT && instance->ctrl_context)
1087 megaraid_sas_kill_hba(instance);
1088
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05301089 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08001090
1091 return ret;
1092}
1093
1094u8
1095megasas_get_map_info(struct megasas_instance *instance)
1096{
1097 struct fusion_context *fusion = instance->ctrl_context;
1098
1099 fusion->fast_path_io = 0;
1100 if (!megasas_get_ld_map_info(instance)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05301101 if (MR_ValidateMapInfo(instance)) {
adam radford9c915a82010-12-21 13:34:31 -08001102 fusion->fast_path_io = 1;
1103 return 0;
1104 }
1105 }
1106 return 1;
1107}
1108
1109/*
1110 * megasas_sync_map_info - Returns FW's ld_map structure
1111 * @instance: Adapter soft state
1112 *
1113 * Issues an internal command (DCMD) to get the FW's controller PD
1114 * list structure. This information is mainly used to find out SYSTEM
1115 * supported by the FW.
1116 */
1117int
1118megasas_sync_map_info(struct megasas_instance *instance)
1119{
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001120 int i;
adam radford9c915a82010-12-21 13:34:31 -08001121 struct megasas_cmd *cmd;
1122 struct megasas_dcmd_frame *dcmd;
1123 u32 size_sync_info, num_lds;
1124 struct fusion_context *fusion;
1125 struct MR_LD_TARGET_SYNC *ci = NULL;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301126 struct MR_DRV_RAID_MAP_ALL *map;
adam radford9c915a82010-12-21 13:34:31 -08001127 struct MR_LD_RAID *raid;
1128 struct MR_LD_TARGET_SYNC *ld_sync;
1129 dma_addr_t ci_h = 0;
1130 u32 size_map_info;
1131
1132 cmd = megasas_get_cmd(instance);
1133
1134 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001135 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n");
adam radford9c915a82010-12-21 13:34:31 -08001136 return -ENOMEM;
1137 }
1138
1139 fusion = instance->ctrl_context;
1140
1141 if (!fusion) {
1142 megasas_return_cmd(instance, cmd);
1143 return 1;
1144 }
1145
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301146 map = fusion->ld_drv_map[instance->map_id & 1];
adam radford9c915a82010-12-21 13:34:31 -08001147
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301148 num_lds = le16_to_cpu(map->raidMap.ldCount);
adam radford9c915a82010-12-21 13:34:31 -08001149
1150 dcmd = &cmd->frame->dcmd;
1151
1152 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
1153
1154 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1155
1156 ci = (struct MR_LD_TARGET_SYNC *)
1157 fusion->ld_map[(instance->map_id - 1) & 1];
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301158 memset(ci, 0, fusion->max_map_sz);
adam radford9c915a82010-12-21 13:34:31 -08001159
1160 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
1161
1162 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
1163
1164 for (i = 0; i < num_lds; i++, ld_sync++) {
1165 raid = MR_LdRaidGet(i, map);
1166 ld_sync->targetId = MR_GetLDTgtId(i, map);
1167 ld_sync->seqNum = raid->seqNum;
1168 }
1169
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301170 size_map_info = fusion->current_map_sz;
adam radford9c915a82010-12-21 13:34:31 -08001171
1172 dcmd->cmd = MFI_CMD_DCMD;
1173 dcmd->cmd_status = 0xFF;
1174 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301175 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
adam radford9c915a82010-12-21 13:34:31 -08001176 dcmd->timeout = 0;
1177 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301178 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -08001179 dcmd->mbox.b[0] = num_lds;
1180 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301181 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1182 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
1183 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -08001184
1185 instance->map_update_cmd = cmd;
1186
1187 instance->instancet->issue_dcmd(instance, cmd);
1188
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001189 return 0;
adam radford9c915a82010-12-21 13:34:31 -08001190}
1191
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301192/*
1193 * meagasas_display_intel_branding - Display branding string
1194 * @instance: per adapter object
1195 *
1196 * Return nothing.
1197 */
1198static void
1199megasas_display_intel_branding(struct megasas_instance *instance)
1200{
1201 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
1202 return;
1203
1204 switch (instance->pdev->device) {
1205 case PCI_DEVICE_ID_LSI_INVADER:
1206 switch (instance->pdev->subsystem_device) {
1207 case MEGARAID_INTEL_RS3DC080_SSDID:
1208 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1209 instance->host->host_no,
1210 MEGARAID_INTEL_RS3DC080_BRANDING);
1211 break;
1212 case MEGARAID_INTEL_RS3DC040_SSDID:
1213 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1214 instance->host->host_no,
1215 MEGARAID_INTEL_RS3DC040_BRANDING);
1216 break;
1217 case MEGARAID_INTEL_RS3SC008_SSDID:
1218 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1219 instance->host->host_no,
1220 MEGARAID_INTEL_RS3SC008_BRANDING);
1221 break;
1222 case MEGARAID_INTEL_RS3MC044_SSDID:
1223 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1224 instance->host->host_no,
1225 MEGARAID_INTEL_RS3MC044_BRANDING);
1226 break;
1227 default:
1228 break;
1229 }
1230 break;
1231 case PCI_DEVICE_ID_LSI_FURY:
1232 switch (instance->pdev->subsystem_device) {
1233 case MEGARAID_INTEL_RS3WC080_SSDID:
1234 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1235 instance->host->host_no,
1236 MEGARAID_INTEL_RS3WC080_BRANDING);
1237 break;
1238 case MEGARAID_INTEL_RS3WC040_SSDID:
1239 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1240 instance->host->host_no,
1241 MEGARAID_INTEL_RS3WC040_BRANDING);
1242 break;
1243 default:
1244 break;
1245 }
1246 break;
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +05301247 case PCI_DEVICE_ID_LSI_CUTLASS_52:
1248 case PCI_DEVICE_ID_LSI_CUTLASS_53:
1249 switch (instance->pdev->subsystem_device) {
1250 case MEGARAID_INTEL_RMS3BC160_SSDID:
1251 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1252 instance->host->host_no,
1253 MEGARAID_INTEL_RMS3BC160_BRANDING);
1254 break;
1255 default:
1256 break;
1257 }
1258 break;
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301259 default:
1260 break;
1261 }
1262}
1263
adam radford9c915a82010-12-21 13:34:31 -08001264/**
1265 * megasas_init_adapter_fusion - Initializes the FW
1266 * @instance: Adapter soft state
1267 *
1268 * This is the main function for initializing firmware.
1269 */
1270u32
1271megasas_init_adapter_fusion(struct megasas_instance *instance)
1272{
1273 struct megasas_register_set __iomem *reg_set;
1274 struct fusion_context *fusion;
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301275 u32 max_cmd, scratch_pad_2;
adam radfordc8e858f2011-10-08 18:15:13 -07001276 int i = 0, count;
adam radford9c915a82010-12-21 13:34:31 -08001277
1278 fusion = instance->ctrl_context;
1279
1280 reg_set = instance->reg_set;
1281
Sumit Saxena308ec452016-01-28 21:04:30 +05301282 megasas_fusion_update_can_queue(instance, PROBE_CONTEXT);
adam radford9c915a82010-12-21 13:34:31 -08001283
1284 /*
1285 * Reduce the max supported cmds by 1. This is to ensure that the
1286 * reply_q_sz (1 more than the max cmd that driver may send)
1287 * does not exceed max cmds that the FW can support
1288 */
1289 instance->max_fw_cmds = instance->max_fw_cmds-1;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05301290
1291 /*
1292 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
1293 */
1294 instance->max_mfi_cmds =
1295 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
adam radford9c915a82010-12-21 13:34:31 -08001296
1297 max_cmd = instance->max_fw_cmds;
1298
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05301299 fusion->reply_q_depth = 2 * (((max_cmd + 1 + 15)/16)*16);
adam radford9c915a82010-12-21 13:34:31 -08001300
1301 fusion->request_alloc_sz =
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001302 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * instance->max_mpt_cmds;
adam radford9c915a82010-12-21 13:34:31 -08001303 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
1304 *(fusion->reply_q_depth);
1305 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001306 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1307 * (instance->max_mpt_cmds + 1)); /* Extra 1 for SMID 0 */
adam radford9c915a82010-12-21 13:34:31 -08001308
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301309 scratch_pad_2 = readl(&instance->reg_set->outbound_scratch_pad_2);
1310 /* If scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
1311 * Firmware support extended IO chain frame which is 4 times more than
1312 * legacy Firmware.
1313 * Legacy Firmware - Frame size is (8 * 128) = 1K
1314 * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
1315 */
1316 if (scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
1317 instance->max_chain_frame_sz =
1318 ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1319 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
1320 else
1321 instance->max_chain_frame_sz =
1322 ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1323 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;
1324
1325 if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
1326 dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
1327 instance->max_chain_frame_sz,
1328 MEGASAS_CHAIN_FRAME_SZ_MIN);
1329 instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
1330 }
1331
adam radford9c915a82010-12-21 13:34:31 -08001332 fusion->max_sge_in_main_msg =
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301333 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1334 - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
adam radford9c915a82010-12-21 13:34:31 -08001335
1336 fusion->max_sge_in_chain =
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301337 instance->max_chain_frame_sz
1338 / sizeof(union MPI2_SGE_IO_UNION);
adam radford9c915a82010-12-21 13:34:31 -08001339
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301340 instance->max_num_sge =
1341 rounddown_pow_of_two(fusion->max_sge_in_main_msg
1342 + fusion->max_sge_in_chain - 2);
adam radford9c915a82010-12-21 13:34:31 -08001343
1344 /* Used for pass thru MFI frame (DCMD) */
1345 fusion->chain_offset_mfi_pthru =
1346 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1347
1348 fusion->chain_offset_io_request =
1349 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1350 sizeof(union MPI2_SGE_IO_UNION))/16;
1351
adam radfordc8e858f2011-10-08 18:15:13 -07001352 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1353 for (i = 0 ; i < count; i++)
1354 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -08001355
1356 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05301357 * For fusion adapters, 3 commands for IOCTL and 5 commands
1358 * for driver's internal DCMDs.
1359 */
1360 instance->max_scsi_cmds = instance->max_fw_cmds -
1361 (MEGASAS_FUSION_INTERNAL_CMDS +
1362 MEGASAS_FUSION_IOCTL_CMDS);
1363 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1364
1365 /*
adam radford9c915a82010-12-21 13:34:31 -08001366 * Allocate memory for descriptors
1367 * Create a pool of commands
1368 */
1369 if (megasas_alloc_cmds(instance))
1370 goto fail_alloc_mfi_cmds;
1371 if (megasas_alloc_cmds_fusion(instance))
1372 goto fail_alloc_cmds;
1373
1374 if (megasas_ioc_init_fusion(instance))
1375 goto fail_ioc_init;
1376
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301377 megasas_display_intel_branding(instance);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05301378 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301379 dev_err(&instance->pdev->dev,
1380 "Could not get controller info. Fail from %s %d\n",
1381 __func__, __LINE__);
1382 goto fail_ioc_init;
1383 }
1384
adam radford9c915a82010-12-21 13:34:31 -08001385 instance->flag_ieee = 1;
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08001386 instance->r1_ldio_hint_default = MR_R1_LDIO_PIGGYBACK_DEFAULT;
adam radford9c915a82010-12-21 13:34:31 -08001387 fusion->fast_path_io = 0;
1388
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301389 fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1390 for (i = 0; i < 2; i++) {
1391 fusion->ld_map[i] = NULL;
1392 fusion->ld_drv_map[i] = (void *)__get_free_pages(GFP_KERNEL,
1393 fusion->drv_map_pages);
1394 if (!fusion->ld_drv_map[i]) {
1395 dev_err(&instance->pdev->dev, "Could not allocate "
1396 "memory for local map info for %d pages\n",
1397 fusion->drv_map_pages);
1398 if (i == 1)
1399 free_pages((ulong)fusion->ld_drv_map[0],
1400 fusion->drv_map_pages);
1401 goto fail_ioc_init;
1402 }
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05301403 memset(fusion->ld_drv_map[i], 0,
1404 ((1 << PAGE_SHIFT) << fusion->drv_map_pages));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301405 }
1406
adam radford9c915a82010-12-21 13:34:31 -08001407 for (i = 0; i < 2; i++) {
1408 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301409 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08001410 &fusion->ld_map_phys[i],
1411 GFP_KERNEL);
1412 if (!fusion->ld_map[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001413 dev_err(&instance->pdev->dev, "Could not allocate memory "
adam radford9c915a82010-12-21 13:34:31 -08001414 "for map info\n");
1415 goto fail_map_info;
1416 }
1417 }
1418
1419 if (!megasas_get_map_info(instance))
1420 megasas_sync_map_info(instance);
1421
1422 return 0;
1423
adam radford9c915a82010-12-21 13:34:31 -08001424fail_map_info:
1425 if (i == 1)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301426 dma_free_coherent(&instance->pdev->dev, fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08001427 fusion->ld_map[0], fusion->ld_map_phys[0]);
1428fail_ioc_init:
adam radfordeb1b1232011-02-24 20:55:56 -08001429 megasas_free_cmds_fusion(instance);
1430fail_alloc_cmds:
1431 megasas_free_cmds(instance);
1432fail_alloc_mfi_cmds:
adam radford9c915a82010-12-21 13:34:31 -08001433 return 1;
1434}
1435
1436/**
adam radford9c915a82010-12-21 13:34:31 -08001437 * map_cmd_status - Maps FW cmd status to OS cmd status
1438 * @cmd : Pointer to cmd
1439 * @status : status of cmd returned by FW
1440 * @ext_status : ext status of cmd returned by FW
1441 */
1442
1443void
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001444map_cmd_status(struct fusion_context *fusion,
1445 struct scsi_cmnd *scmd, u8 status, u8 ext_status,
1446 u32 data_length, u8 *sense)
adam radford9c915a82010-12-21 13:34:31 -08001447{
Shivasharan S31d9a572017-02-10 00:59:14 -08001448 int resid;
adam radford9c915a82010-12-21 13:34:31 -08001449
1450 switch (status) {
1451
1452 case MFI_STAT_OK:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001453 scmd->result = DID_OK << 16;
adam radford9c915a82010-12-21 13:34:31 -08001454 break;
1455
1456 case MFI_STAT_SCSI_IO_FAILED:
1457 case MFI_STAT_LD_INIT_IN_PROGRESS:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001458 scmd->result = (DID_ERROR << 16) | ext_status;
adam radford9c915a82010-12-21 13:34:31 -08001459 break;
1460
1461 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1462
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001463 scmd->result = (DID_OK << 16) | ext_status;
adam radford9c915a82010-12-21 13:34:31 -08001464 if (ext_status == SAM_STAT_CHECK_CONDITION) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001465 memset(scmd->sense_buffer, 0,
adam radford9c915a82010-12-21 13:34:31 -08001466 SCSI_SENSE_BUFFERSIZE);
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001467 memcpy(scmd->sense_buffer, sense,
adam radford9c915a82010-12-21 13:34:31 -08001468 SCSI_SENSE_BUFFERSIZE);
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001469 scmd->result |= DRIVER_SENSE << 24;
adam radford9c915a82010-12-21 13:34:31 -08001470 }
Shivasharan S31d9a572017-02-10 00:59:14 -08001471
1472 /*
1473 * If the IO request is partially completed, then MR FW will
1474 * update "io_request->DataLength" field with actual number of
1475 * bytes transferred.Driver will set residual bytes count in
1476 * SCSI command structure.
1477 */
1478 resid = (scsi_bufflen(scmd) - data_length);
1479 scsi_set_resid(scmd, resid);
adam radford9c915a82010-12-21 13:34:31 -08001480 break;
1481
1482 case MFI_STAT_LD_OFFLINE:
1483 case MFI_STAT_DEVICE_NOT_FOUND:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001484 scmd->result = DID_BAD_TARGET << 16;
adam radford9c915a82010-12-21 13:34:31 -08001485 break;
adam radford36807e62011-10-08 18:15:06 -07001486 case MFI_STAT_CONFIG_SEQ_MISMATCH:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001487 scmd->result = DID_IMM_RETRY << 16;
adam radford36807e62011-10-08 18:15:06 -07001488 break;
adam radford9c915a82010-12-21 13:34:31 -08001489 default:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001490 scmd->result = DID_ERROR << 16;
adam radford9c915a82010-12-21 13:34:31 -08001491 break;
1492 }
1493}
1494
1495/**
Shivasharan S33203bc2017-02-10 00:59:12 -08001496 * megasas_is_prp_possible -
1497 * Checks if native NVMe PRPs can be built for the IO
1498 *
1499 * @instance: Adapter soft state
1500 * @scmd: SCSI command from the mid-layer
1501 * @sge_count: scatter gather element count.
1502 *
1503 * Returns: true: PRPs can be built
1504 * false: IEEE SGLs needs to be built
1505 */
1506static bool
1507megasas_is_prp_possible(struct megasas_instance *instance,
1508 struct scsi_cmnd *scmd, int sge_count)
1509{
1510 struct fusion_context *fusion;
1511 int i;
1512 u32 data_length = 0;
1513 struct scatterlist *sg_scmd;
1514 bool build_prp = false;
1515 u32 mr_nvme_pg_size;
1516
1517 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1518 MR_DEFAULT_NVME_PAGE_SIZE);
1519 fusion = instance->ctrl_context;
1520 data_length = scsi_bufflen(scmd);
1521 sg_scmd = scsi_sglist(scmd);
1522
1523 /*
1524 * NVMe uses one PRP for each page (or part of a page)
1525 * look at the data length - if 4 pages or less then IEEE is OK
1526 * if > 5 pages then we need to build a native SGL
1527 * if > 4 and <= 5 pages, then check physical address of 1st SG entry
1528 * if this first size in the page is >= the residual beyond 4 pages
1529 * then use IEEE, otherwise use native SGL
1530 */
1531
1532 if (data_length > (mr_nvme_pg_size * 5)) {
1533 build_prp = true;
1534 } else if ((data_length > (mr_nvme_pg_size * 4)) &&
1535 (data_length <= (mr_nvme_pg_size * 5))) {
1536 /* check if 1st SG entry size is < residual beyond 4 pages */
1537 if (sg_dma_len(sg_scmd) < (data_length - (mr_nvme_pg_size * 4)))
1538 build_prp = true;
1539 }
1540
1541/*
1542 * Below code detects gaps/holes in IO data buffers.
1543 * What does holes/gaps mean?
1544 * Any SGE except first one in a SGL starts at non NVME page size
1545 * aligned address OR Any SGE except last one in a SGL ends at
1546 * non NVME page size boundary.
1547 *
1548 * Driver has already informed block layer by setting boundary rules for
1549 * bio merging done at NVME page size boundary calling kernel API
1550 * blk_queue_virt_boundary inside slave_config.
1551 * Still there is possibility of IO coming with holes to driver because of
1552 * IO merging done by IO scheduler.
1553 *
1554 * With SCSI BLK MQ enabled, there will be no IO with holes as there is no
1555 * IO scheduling so no IO merging.
1556 *
1557 * With SCSI BLK MQ disabled, IO scheduler may attempt to merge IOs and
1558 * then sending IOs with holes.
1559 *
1560 * Though driver can request block layer to disable IO merging by calling-
1561 * queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, sdev->request_queue) but
1562 * user may tune sysfs parameter- nomerges again to 0 or 1.
1563 *
1564 * If in future IO scheduling is enabled with SCSI BLK MQ,
1565 * this algorithm to detect holes will be required in driver
1566 * for SCSI BLK MQ enabled case as well.
1567 *
1568 *
1569 */
1570 scsi_for_each_sg(scmd, sg_scmd, sge_count, i) {
1571 if ((i != 0) && (i != (sge_count - 1))) {
1572 if (mega_mod64(sg_dma_len(sg_scmd), mr_nvme_pg_size) ||
1573 mega_mod64(sg_dma_address(sg_scmd),
1574 mr_nvme_pg_size)) {
1575 build_prp = false;
1576 atomic_inc(&instance->sge_holes_type1);
1577 break;
1578 }
1579 }
1580
1581 if ((sge_count > 1) && (i == 0)) {
1582 if ((mega_mod64((sg_dma_address(sg_scmd) +
1583 sg_dma_len(sg_scmd)),
1584 mr_nvme_pg_size))) {
1585 build_prp = false;
1586 atomic_inc(&instance->sge_holes_type2);
1587 break;
1588 }
1589 }
1590
1591 if ((sge_count > 1) && (i == (sge_count - 1))) {
1592 if (mega_mod64(sg_dma_address(sg_scmd),
1593 mr_nvme_pg_size)) {
1594 build_prp = false;
1595 atomic_inc(&instance->sge_holes_type3);
1596 break;
1597 }
1598 }
1599 }
1600
1601 return build_prp;
1602}
1603
1604/**
1605 * megasas_make_prp_nvme -
1606 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
1607 *
1608 * @instance: Adapter soft state
1609 * @scmd: SCSI command from the mid-layer
1610 * @sgl_ptr: SGL to be filled in
1611 * @cmd: Fusion command frame
1612 * @sge_count: scatter gather element count.
1613 *
1614 * Returns: true: PRPs are built
1615 * false: IEEE SGLs needs to be built
1616 */
1617static bool
1618megasas_make_prp_nvme(struct megasas_instance *instance, struct scsi_cmnd *scmd,
1619 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1620 struct megasas_cmd_fusion *cmd, int sge_count)
1621{
1622 int sge_len, offset, num_prp_in_chain = 0;
1623 struct MPI25_IEEE_SGE_CHAIN64 *main_chain_element, *ptr_first_sgl;
1624 u64 *ptr_sgl, *ptr_sgl_phys;
1625 u64 sge_addr;
1626 u32 page_mask, page_mask_result;
1627 struct scatterlist *sg_scmd;
1628 u32 first_prp_len;
1629 bool build_prp = false;
1630 int data_len = scsi_bufflen(scmd);
1631 struct fusion_context *fusion;
1632 u32 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1633 MR_DEFAULT_NVME_PAGE_SIZE);
1634
1635 fusion = instance->ctrl_context;
1636
1637 build_prp = megasas_is_prp_possible(instance, scmd, sge_count);
1638
1639 if (!build_prp)
1640 return false;
1641
1642 /*
1643 * Nvme has a very convoluted prp format. One prp is required
1644 * for each page or partial page. Driver need to split up OS sg_list
1645 * entries if it is longer than one page or cross a page
1646 * boundary. Driver also have to insert a PRP list pointer entry as
1647 * the last entry in each physical page of the PRP list.
1648 *
1649 * NOTE: The first PRP "entry" is actually placed in the first
1650 * SGL entry in the main message as IEEE 64 format. The 2nd
1651 * entry in the main message is the chain element, and the rest
1652 * of the PRP entries are built in the contiguous pcie buffer.
1653 */
1654 page_mask = mr_nvme_pg_size - 1;
1655 ptr_sgl = (u64 *)cmd->sg_frame;
1656 ptr_sgl_phys = (u64 *)cmd->sg_frame_phys_addr;
1657 memset(ptr_sgl, 0, instance->max_chain_frame_sz);
1658
1659 /* Build chain frame element which holds all prps except first*/
1660 main_chain_element = (struct MPI25_IEEE_SGE_CHAIN64 *)
1661 ((u8 *)sgl_ptr + sizeof(struct MPI25_IEEE_SGE_CHAIN64));
1662
1663 main_chain_element->Address = cpu_to_le64((uintptr_t)ptr_sgl_phys);
1664 main_chain_element->NextChainOffset = 0;
1665 main_chain_element->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1666 IEEE_SGE_FLAGS_SYSTEM_ADDR |
1667 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
1668
1669 /* Build first prp, sge need not to be page aligned*/
1670 ptr_first_sgl = sgl_ptr;
1671 sg_scmd = scsi_sglist(scmd);
1672 sge_addr = sg_dma_address(sg_scmd);
1673 sge_len = sg_dma_len(sg_scmd);
1674
1675 offset = (u32)(sge_addr & page_mask);
1676 first_prp_len = mr_nvme_pg_size - offset;
1677
1678 ptr_first_sgl->Address = cpu_to_le64(sge_addr);
1679 ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
1680
1681 data_len -= first_prp_len;
1682
1683 if (sge_len > first_prp_len) {
1684 sge_addr += first_prp_len;
1685 sge_len -= first_prp_len;
1686 } else if (sge_len == first_prp_len) {
1687 sg_scmd = sg_next(sg_scmd);
1688 sge_addr = sg_dma_address(sg_scmd);
1689 sge_len = sg_dma_len(sg_scmd);
1690 }
1691
1692 for (;;) {
1693 offset = (u32)(sge_addr & page_mask);
1694
1695 /* Put PRP pointer due to page boundary*/
1696 page_mask_result = (uintptr_t)(ptr_sgl + 1) & page_mask;
1697 if (unlikely(!page_mask_result)) {
1698 scmd_printk(KERN_NOTICE,
1699 scmd, "page boundary ptr_sgl: 0x%p\n",
1700 ptr_sgl);
1701 ptr_sgl_phys++;
1702 *ptr_sgl =
1703 cpu_to_le64((uintptr_t)ptr_sgl_phys);
1704 ptr_sgl++;
1705 num_prp_in_chain++;
1706 }
1707
1708 *ptr_sgl = cpu_to_le64(sge_addr);
1709 ptr_sgl++;
1710 ptr_sgl_phys++;
1711 num_prp_in_chain++;
1712
1713 sge_addr += mr_nvme_pg_size;
1714 sge_len -= mr_nvme_pg_size;
1715 data_len -= mr_nvme_pg_size;
1716
1717 if (data_len <= 0)
1718 break;
1719
1720 if (sge_len > 0)
1721 continue;
1722
1723 sg_scmd = sg_next(sg_scmd);
1724 sge_addr = sg_dma_address(sg_scmd);
1725 sge_len = sg_dma_len(sg_scmd);
1726 }
1727
1728 main_chain_element->Length =
1729 cpu_to_le32(num_prp_in_chain * sizeof(u64));
1730
1731 atomic_inc(&instance->prp_sgl);
1732 return build_prp;
1733}
1734
1735/**
adam radford9c915a82010-12-21 13:34:31 -08001736 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1737 * @instance: Adapter soft state
1738 * @scp: SCSI command from the mid-layer
1739 * @sgl_ptr: SGL to be filled in
1740 * @cmd: cmd we are working on
Shivasharan S33203bc2017-02-10 00:59:12 -08001741 * @sge_count sge count
adam radford9c915a82010-12-21 13:34:31 -08001742 *
adam radford9c915a82010-12-21 13:34:31 -08001743 */
Shivasharan S33203bc2017-02-10 00:59:12 -08001744static void
adam radford9c915a82010-12-21 13:34:31 -08001745megasas_make_sgl_fusion(struct megasas_instance *instance,
1746 struct scsi_cmnd *scp,
1747 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
Shivasharan S33203bc2017-02-10 00:59:12 -08001748 struct megasas_cmd_fusion *cmd, int sge_count)
adam radford9c915a82010-12-21 13:34:31 -08001749{
Shivasharan S33203bc2017-02-10 00:59:12 -08001750 int i, sg_processed;
adam radford9c915a82010-12-21 13:34:31 -08001751 struct scatterlist *os_sgl;
1752 struct fusion_context *fusion;
1753
1754 fusion = instance->ctrl_context;
1755
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05001756 if (fusion->adapter_type >= INVADER_SERIES) {
adam radford36807e62011-10-08 18:15:06 -07001757 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1758 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1759 sgl_ptr_end->Flags = 0;
1760 }
adam radford9c915a82010-12-21 13:34:31 -08001761
adam radford9c915a82010-12-21 13:34:31 -08001762 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301763 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
1764 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
adam radford9c915a82010-12-21 13:34:31 -08001765 sgl_ptr->Flags = 0;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05001766 if (fusion->adapter_type >= INVADER_SERIES)
adam radford36807e62011-10-08 18:15:06 -07001767 if (i == sge_count - 1)
1768 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
adam radford9c915a82010-12-21 13:34:31 -08001769 sgl_ptr++;
adam radford9c915a82010-12-21 13:34:31 -08001770 sg_processed = i + 1;
1771
1772 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1773 (sge_count > fusion->max_sge_in_main_msg)) {
1774
1775 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05001776 if (fusion->adapter_type >= INVADER_SERIES) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301777 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1778 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1779 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
adam radford36807e62011-10-08 18:15:06 -07001780 cmd->io_request->ChainOffset =
1781 fusion->
1782 chain_offset_io_request;
1783 else
1784 cmd->io_request->ChainOffset = 0;
1785 } else
1786 cmd->io_request->ChainOffset =
1787 fusion->chain_offset_io_request;
1788
adam radford9c915a82010-12-21 13:34:31 -08001789 sg_chain = sgl_ptr;
1790 /* Prepare chain element */
1791 sg_chain->NextChainOffset = 0;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05001792 if (fusion->adapter_type >= INVADER_SERIES)
adam radford36807e62011-10-08 18:15:06 -07001793 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1794 else
1795 sg_chain->Flags =
1796 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1797 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301798 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
1799 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08001800
1801 sgl_ptr =
1802 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301803 memset(sgl_ptr, 0, instance->max_chain_frame_sz);
adam radford9c915a82010-12-21 13:34:31 -08001804 }
1805 }
Shivasharan S33203bc2017-02-10 00:59:12 -08001806 atomic_inc(&instance->ieee_sgl);
1807}
1808
1809/**
1810 * megasas_make_sgl - Build Scatter Gather List(SGLs)
1811 * @scp: SCSI command pointer
1812 * @instance: Soft instance of controller
1813 * @cmd: Fusion command pointer
1814 *
1815 * This function will build sgls based on device type.
1816 * For nvme drives, there is different way of building sgls in nvme native
1817 * format- PRPs(Physical Region Page).
1818 *
1819 * Returns the number of sg lists actually used, zero if the sg lists
1820 * is NULL, or -ENOMEM if the mapping failed
1821 */
1822static
1823int megasas_make_sgl(struct megasas_instance *instance, struct scsi_cmnd *scp,
1824 struct megasas_cmd_fusion *cmd)
1825{
1826 int sge_count;
1827 bool build_prp = false;
1828 struct MPI25_IEEE_SGE_CHAIN64 *sgl_chain64;
1829
1830 sge_count = scsi_dma_map(scp);
1831
1832 if ((sge_count > instance->max_num_sge) || (sge_count <= 0))
1833 return sge_count;
1834
1835 sgl_chain64 = (struct MPI25_IEEE_SGE_CHAIN64 *)&cmd->io_request->SGL;
1836 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1837 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
1838 (cmd->pd_interface == NVME_PD))
1839 build_prp = megasas_make_prp_nvme(instance, scp, sgl_chain64,
1840 cmd, sge_count);
1841
1842 if (!build_prp)
1843 megasas_make_sgl_fusion(instance, scp, sgl_chain64,
1844 cmd, sge_count);
adam radford9c915a82010-12-21 13:34:31 -08001845
1846 return sge_count;
1847}
1848
1849/**
1850 * megasas_set_pd_lba - Sets PD LBA
1851 * @cdb: CDB
1852 * @cdb_len: cdb length
1853 * @start_blk: Start block of IO
1854 *
1855 * Used to set the PD LBA in CDB for FP IOs
1856 */
1857void
1858megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1859 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301860 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
adam radford9c915a82010-12-21 13:34:31 -08001861{
1862 struct MR_LD_RAID *raid;
1863 u32 ld;
1864 u64 start_blk = io_info->pdBlock;
1865 u8 *cdb = io_request->CDB.CDB32;
1866 u32 num_blocks = io_info->numBlocks;
adam radford495c5602011-05-11 18:34:40 -07001867 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
adam radford9c915a82010-12-21 13:34:31 -08001868
1869 /* Check if T10 PI (DIF) is enabled for this LD */
1870 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1871 raid = MR_LdRaidGet(ld, local_map_ptr);
1872 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1873 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1874 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1875 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1876
1877 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1878 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1879 else
1880 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1881 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1882
1883 /* LBA */
1884 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1885 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1886 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1887 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1888 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1889 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1890 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1891 cdb[19] = (u8)(start_blk & 0xff);
1892
1893 /* Logical block reference tag */
1894 io_request->CDB.EEDP32.PrimaryReferenceTag =
1895 cpu_to_be32(ref_tag);
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301896 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301897 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001898
1899 /* Transfer length */
1900 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1901 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1902 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1903 cdb[31] = (u8)(num_blocks & 0xff);
1904
1905 /* set SCSI IO EEDPFlags */
1906 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301907 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001908 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1909 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1910 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1911 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
Sasikumar Chandrasekaran45d44602017-01-10 18:20:45 -05001912 MPI25_SCSIIO_EEDPFLAGS_DO_NOT_DISABLE_MODE |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301913 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
adam radford9c915a82010-12-21 13:34:31 -08001914 } else {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301915 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001916 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301917 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
adam radford9c915a82010-12-21 13:34:31 -08001918 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301919 io_request->Control |= cpu_to_le32((0x4 << 26));
1920 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
adam radford9c915a82010-12-21 13:34:31 -08001921 } else {
1922 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1923 if (((cdb_len == 12) || (cdb_len == 16)) &&
1924 (start_blk <= 0xffffffff)) {
1925 if (cdb_len == 16) {
1926 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1927 flagvals = cdb[1];
1928 groupnum = cdb[14];
1929 control = cdb[15];
1930 } else {
1931 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1932 flagvals = cdb[1];
1933 groupnum = cdb[10];
1934 control = cdb[11];
1935 }
1936
1937 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1938
1939 cdb[0] = opcode;
1940 cdb[1] = flagvals;
1941 cdb[6] = groupnum;
1942 cdb[9] = control;
1943
1944 /* Transfer length */
1945 cdb[8] = (u8)(num_blocks & 0xff);
1946 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1947
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301948 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001949 cdb_len = 10;
adam radford495c5602011-05-11 18:34:40 -07001950 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1951 /* Convert to 16 byte CDB for large LBA's */
1952 switch (cdb_len) {
1953 case 6:
1954 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1955 control = cdb[5];
1956 break;
1957 case 10:
1958 opcode =
1959 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1960 flagvals = cdb[1];
1961 groupnum = cdb[6];
1962 control = cdb[9];
1963 break;
1964 case 12:
1965 opcode =
1966 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1967 flagvals = cdb[1];
1968 groupnum = cdb[10];
1969 control = cdb[11];
1970 break;
1971 }
1972
1973 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1974
1975 cdb[0] = opcode;
1976 cdb[1] = flagvals;
1977 cdb[14] = groupnum;
1978 cdb[15] = control;
1979
1980 /* Transfer length */
1981 cdb[13] = (u8)(num_blocks & 0xff);
1982 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1983 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1984 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1985
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301986 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
adam radford495c5602011-05-11 18:34:40 -07001987 cdb_len = 16;
adam radford9c915a82010-12-21 13:34:31 -08001988 }
1989
1990 /* Normal case, just load LBA here */
1991 switch (cdb_len) {
1992 case 6:
1993 {
1994 u8 val = cdb[1] & 0xE0;
1995 cdb[3] = (u8)(start_blk & 0xff);
1996 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1997 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1998 break;
1999 }
2000 case 10:
2001 cdb[5] = (u8)(start_blk & 0xff);
2002 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2003 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2004 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2005 break;
2006 case 12:
2007 cdb[5] = (u8)(start_blk & 0xff);
2008 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2009 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2010 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2011 break;
2012 case 16:
2013 cdb[9] = (u8)(start_blk & 0xff);
2014 cdb[8] = (u8)((start_blk >> 8) & 0xff);
2015 cdb[7] = (u8)((start_blk >> 16) & 0xff);
2016 cdb[6] = (u8)((start_blk >> 24) & 0xff);
2017 cdb[5] = (u8)((start_blk >> 32) & 0xff);
2018 cdb[4] = (u8)((start_blk >> 40) & 0xff);
2019 cdb[3] = (u8)((start_blk >> 48) & 0xff);
2020 cdb[2] = (u8)((start_blk >> 56) & 0xff);
2021 break;
2022 }
2023 }
2024}
2025
2026/**
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002027 * megasas_stream_detect - stream detection on read and and write IOs
2028 * @instance: Adapter soft state
2029 * @cmd: Command to be prepared
2030 * @io_info: IO Request info
2031 *
2032 */
2033
2034/** stream detection on read and and write IOs */
2035static void megasas_stream_detect(struct megasas_instance *instance,
2036 struct megasas_cmd_fusion *cmd,
2037 struct IO_REQUEST_INFO *io_info)
2038{
2039 struct fusion_context *fusion = instance->ctrl_context;
2040 u32 device_id = io_info->ldTgtId;
2041 struct LD_STREAM_DETECT *current_ld_sd
2042 = fusion->stream_detect_by_ld[device_id];
2043 u32 *track_stream = &current_ld_sd->mru_bit_map, stream_num;
2044 u32 shifted_values, unshifted_values;
2045 u32 index_value_mask, shifted_values_mask;
2046 int i;
2047 bool is_read_ahead = false;
2048 struct STREAM_DETECT *current_sd;
2049 /* find possible stream */
2050 for (i = 0; i < MAX_STREAMS_TRACKED; ++i) {
2051 stream_num =
2052 (*track_stream >> (i * BITS_PER_INDEX_STREAM)) &
2053 STREAM_MASK;
2054 current_sd = &current_ld_sd->stream_track[stream_num];
2055 /* if we found a stream, update the raid
2056 * context and also update the mruBitMap
2057 */
2058 /* boundary condition */
2059 if ((current_sd->next_seq_lba) &&
2060 (io_info->ldStartBlock >= current_sd->next_seq_lba) &&
2061 (io_info->ldStartBlock <= (current_sd->next_seq_lba+32)) &&
2062 (current_sd->is_read == io_info->isRead)) {
2063
2064 if ((io_info->ldStartBlock != current_sd->next_seq_lba)
2065 && ((!io_info->isRead) || (!is_read_ahead)))
2066 /*
2067 * Once the API availible we need to change this.
2068 * At this point we are not allowing any gap
2069 */
2070 continue;
2071
2072 cmd->io_request->RaidContext.raid_context_g35.stream_detected = true;
2073 current_sd->next_seq_lba =
2074 io_info->ldStartBlock + io_info->numBlocks;
2075 /*
2076 * update the mruBitMap LRU
2077 */
2078 shifted_values_mask =
2079 (1 << i * BITS_PER_INDEX_STREAM) - 1;
2080 shifted_values = ((*track_stream & shifted_values_mask)
2081 << BITS_PER_INDEX_STREAM);
2082 index_value_mask =
2083 STREAM_MASK << i * BITS_PER_INDEX_STREAM;
2084 unshifted_values =
2085 *track_stream & ~(shifted_values_mask |
2086 index_value_mask);
2087 *track_stream =
2088 unshifted_values | shifted_values | stream_num;
2089 return;
2090
2091 }
2092
2093 }
2094 /*
2095 * if we did not find any stream, create a new one
2096 * from the least recently used
2097 */
2098 stream_num =
2099 (*track_stream >> ((MAX_STREAMS_TRACKED - 1) * BITS_PER_INDEX_STREAM)) &
2100 STREAM_MASK;
2101 current_sd = &current_ld_sd->stream_track[stream_num];
2102 current_sd->is_read = io_info->isRead;
2103 current_sd->next_seq_lba = io_info->ldStartBlock + io_info->numBlocks;
2104 *track_stream =
2105 (((*track_stream & ZERO_LAST_STREAM) << 4) | stream_num);
2106 return;
2107
2108}
2109
2110/**
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002111 * megasas_set_raidflag_cpu_affinity - This function sets the cpu
2112 * affinity (cpu of the controller) and raid_flags in the raid context
2113 * based on IO type.
2114 *
2115 * @praid_context: IO RAID context
2116 * @raid: LD raid map
2117 * @fp_possible: Is fast path possible?
2118 * @is_read: Is read IO?
2119 *
2120 */
2121static void
2122megasas_set_raidflag_cpu_affinity(union RAID_CONTEXT_UNION *praid_context,
2123 struct MR_LD_RAID *raid, bool fp_possible,
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002124 u8 is_read, u32 scsi_buff_len)
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002125{
2126 u8 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2127 struct RAID_CONTEXT_G35 *rctx_g35;
2128
2129 rctx_g35 = &praid_context->raid_context_g35;
2130 if (fp_possible) {
2131 if (is_read) {
2132 if ((raid->cpuAffinity.pdRead.cpu0) &&
2133 (raid->cpuAffinity.pdRead.cpu1))
2134 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2135 else if (raid->cpuAffinity.pdRead.cpu1)
2136 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2137 } else {
2138 if ((raid->cpuAffinity.pdWrite.cpu0) &&
2139 (raid->cpuAffinity.pdWrite.cpu1))
2140 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2141 else if (raid->cpuAffinity.pdWrite.cpu1)
2142 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2143 /* Fast path cache by pass capable R0/R1 VD */
2144 if ((raid->level <= 1) &&
2145 (raid->capability.fp_cache_bypass_capable)) {
2146 rctx_g35->routing_flags.bits.sld = 1;
2147 rctx_g35->raid_flags =
2148 (MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS
2149 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2150 }
2151 }
2152 } else {
2153 if (is_read) {
2154 if ((raid->cpuAffinity.ldRead.cpu0) &&
2155 (raid->cpuAffinity.ldRead.cpu1))
2156 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2157 else if (raid->cpuAffinity.ldRead.cpu1)
2158 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2159 } else {
2160 if ((raid->cpuAffinity.ldWrite.cpu0) &&
2161 (raid->cpuAffinity.ldWrite.cpu1))
2162 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2163 else if (raid->cpuAffinity.ldWrite.cpu1)
2164 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2165
2166 if (rctx_g35->stream_detected &&
2167 (raid->level == 5) &&
2168 (raid->writeMode == MR_RL_WRITE_THROUGH_MODE) &&
2169 (cpu_sel == MR_RAID_CTX_CPUSEL_FCFS))
2170 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2171 }
2172 }
2173
2174 rctx_g35->routing_flags.bits.cpu_sel = cpu_sel;
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002175
2176 /* Always give priority to MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2177 * vs MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS.
2178 * IO Subtype is not bitmap.
2179 */
2180 if ((raid->level == 1) && (!is_read)) {
2181 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2182 praid_context->raid_context_g35.raid_flags =
2183 (MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT
2184 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2185 }
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002186}
2187
2188/**
adam radford9c915a82010-12-21 13:34:31 -08002189 * megasas_build_ldio_fusion - Prepares IOs to devices
2190 * @instance: Adapter soft state
2191 * @scp: SCSI command
2192 * @cmd: Command to be prepared
2193 *
2194 * Prepares the io_request and chain elements (sg_frame) for IO
2195 * The IO can be for PD (Fast Path) or LD
2196 */
2197void
2198megasas_build_ldio_fusion(struct megasas_instance *instance,
2199 struct scsi_cmnd *scp,
2200 struct megasas_cmd_fusion *cmd)
2201{
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002202 bool fp_possible;
2203 u16 ld;
2204 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
2205 u32 scsi_buff_len;
adam radford9c915a82010-12-21 13:34:31 -08002206 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2207 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2208 struct IO_REQUEST_INFO io_info;
2209 struct fusion_context *fusion;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302210 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
adam radford21c9e162013-09-06 15:27:14 -07002211 u8 *raidLUN;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002212 unsigned long spinlock_flags;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002213 union RAID_CONTEXT_UNION *praid_context;
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002214 struct MR_LD_RAID *raid = NULL;
2215 struct MR_PRIV_DEVICE *mrdev_priv;
adam radford9c915a82010-12-21 13:34:31 -08002216
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302217 device_id = MEGASAS_DEV_INDEX(scp);
adam radford9c915a82010-12-21 13:34:31 -08002218
2219 fusion = instance->ctrl_context;
2220
2221 io_request = cmd->io_request;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002222 io_request->RaidContext.raid_context.virtual_disk_tgt_id =
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002223 cpu_to_le16(device_id);
2224 io_request->RaidContext.raid_context.status = 0;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002225 io_request->RaidContext.raid_context.ex_status = 0;
adam radford9c915a82010-12-21 13:34:31 -08002226
2227 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
2228
2229 start_lba_lo = 0;
2230 start_lba_hi = 0;
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002231 fp_possible = false;
adam radford9c915a82010-12-21 13:34:31 -08002232
2233 /*
2234 * 6-byte READ(0x08) or WRITE(0x0A) cdb
2235 */
2236 if (scp->cmd_len == 6) {
adam radfordf9eff812012-10-01 19:27:07 -07002237 datalength = (u32) scp->cmnd[4];
adam radford9c915a82010-12-21 13:34:31 -08002238 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
2239 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
2240
2241 start_lba_lo &= 0x1FFFFF;
2242 }
2243
2244 /*
2245 * 10-byte READ(0x28) or WRITE(0x2A) cdb
2246 */
2247 else if (scp->cmd_len == 10) {
adam radfordf9eff812012-10-01 19:27:07 -07002248 datalength = (u32) scp->cmnd[8] |
adam radford9c915a82010-12-21 13:34:31 -08002249 ((u32) scp->cmnd[7] << 8);
2250 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2251 ((u32) scp->cmnd[3] << 16) |
2252 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2253 }
2254
2255 /*
2256 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
2257 */
2258 else if (scp->cmd_len == 12) {
adam radfordf9eff812012-10-01 19:27:07 -07002259 datalength = ((u32) scp->cmnd[6] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08002260 ((u32) scp->cmnd[7] << 16) |
2261 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2262 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
2263 ((u32) scp->cmnd[3] << 16) |
2264 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2265 }
2266
2267 /*
2268 * 16-byte READ(0x88) or WRITE(0x8A) cdb
2269 */
2270 else if (scp->cmd_len == 16) {
adam radfordf9eff812012-10-01 19:27:07 -07002271 datalength = ((u32) scp->cmnd[10] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08002272 ((u32) scp->cmnd[11] << 16) |
2273 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
2274 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
2275 ((u32) scp->cmnd[7] << 16) |
2276 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
2277
2278 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
2279 ((u32) scp->cmnd[3] << 16) |
2280 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
2281 }
2282
2283 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
2284 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
adam radfordf9eff812012-10-01 19:27:07 -07002285 io_info.numBlocks = datalength;
adam radford9c915a82010-12-21 13:34:31 -08002286 io_info.ldTgtId = device_id;
Shivasharan S8bf7c652017-02-10 00:59:03 -08002287 io_info.r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002288 scsi_buff_len = scsi_bufflen(scp);
2289 io_request->DataLength = cpu_to_le32(scsi_buff_len);
adam radford9c915a82010-12-21 13:34:31 -08002290
2291 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
2292 io_info.isRead = 1;
2293
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302294 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002295 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
2296 raid = MR_LdRaidGet(ld, local_map_ptr);
adam radford9c915a82010-12-21 13:34:31 -08002297
2298 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302299 instance->fw_supported_vd_count) || (!fusion->fast_path_io)) {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002300 io_request->RaidContext.raid_context.reg_lock_flags = 0;
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002301 fp_possible = false;
adam radford9c915a82010-12-21 13:34:31 -08002302 } else {
adam radford36807e62011-10-08 18:15:06 -07002303 if (MR_BuildRaidContext(instance, &io_info,
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002304 &io_request->RaidContext.raid_context,
adam radford21c9e162013-09-06 15:27:14 -07002305 local_map_ptr, &raidLUN))
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002306 fp_possible = (io_info.fpOkForIo > 0) ? true : false;
adam radford9c915a82010-12-21 13:34:31 -08002307 }
2308
Christoph Hellwig16b85282015-04-15 09:44:37 -07002309 /* Use raw_smp_processor_id() for now until cmd->request->cpu is CPU
adam radfordc8e858f2011-10-08 18:15:13 -07002310 id by default, not CPU group id, otherwise all MSI-X queues won't
2311 be utilized */
2312 cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
Christoph Hellwig16b85282015-04-15 09:44:37 -07002313 raw_smp_processor_id() % instance->msix_vectors : 0;
adam radfordc8e858f2011-10-08 18:15:13 -07002314
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002315 praid_context = &io_request->RaidContext;
2316
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002317 if (instance->is_ventura) {
2318 spin_lock_irqsave(&instance->stream_lock, spinlock_flags);
2319 megasas_stream_detect(instance, cmd, &io_info);
2320 spin_unlock_irqrestore(&instance->stream_lock, spinlock_flags);
2321 /* In ventura if stream detected for a read and it is read ahead
2322 * capable make this IO as LDIO
2323 */
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002324 if (praid_context->raid_context_g35.stream_detected &&
2325 io_info.isRead && io_info.ra_capable)
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002326 fp_possible = false;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002327
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002328 /* FP for Optimal raid level 1.
2329 * All large RAID-1 writes (> 32 KiB, both WT and WB modes)
2330 * are built by the driver as LD I/Os.
2331 * All small RAID-1 WT writes (<= 32 KiB) are built as FP I/Os
2332 * (there is never a reason to process these as buffered writes)
2333 * All small RAID-1 WB writes (<= 32 KiB) are built as FP I/Os
2334 * with the SLD bit asserted.
2335 */
Shivasharan S8bf7c652017-02-10 00:59:03 -08002336 if (io_info.r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002337 mrdev_priv = scp->device->hostdata;
2338
2339 if (atomic_inc_return(&instance->fw_outstanding) >
2340 (instance->host->can_queue)) {
2341 fp_possible = false;
2342 atomic_dec(&instance->fw_outstanding);
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002343 } else if ((scsi_buff_len > MR_LARGE_IO_MIN_SIZE) ||
2344 atomic_dec_if_positive(&mrdev_priv->r1_ldio_hint)) {
2345 fp_possible = false;
2346 atomic_dec(&instance->fw_outstanding);
2347 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2348 atomic_set(&mrdev_priv->r1_ldio_hint,
2349 instance->r1_ldio_hint_default);
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002350 }
2351 }
2352
2353 /* If raid is NULL, set CPU affinity to default CPU0 */
2354 if (raid)
2355 megasas_set_raidflag_cpu_affinity(praid_context,
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002356 raid, fp_possible, io_info.isRead,
2357 scsi_buff_len);
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002358 else
2359 praid_context->raid_context_g35.routing_flags.bits.cpu_sel =
2360 MR_RAID_CTX_CPUSEL_0;
2361 }
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002362
adam radford9c915a82010-12-21 13:34:31 -08002363 if (fp_possible) {
2364 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
2365 local_map_ptr, start_lba_lo);
adam radford9c915a82010-12-21 13:34:31 -08002366 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2367 cmd->request_desc->SCSIIO.RequestFlags =
Sumit Saxena2c048352016-01-28 21:04:24 +05302368 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
adam radford9c915a82010-12-21 13:34:31 -08002369 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05302370 if (fusion->adapter_type == INVADER_SERIES) {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002371 if (io_request->RaidContext.raid_context.reg_lock_flags ==
adam radford36807e62011-10-08 18:15:06 -07002372 REGION_TYPE_UNUSED)
2373 cmd->request_desc->SCSIIO.RequestFlags =
2374 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2375 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002376 io_request->RaidContext.raid_context.type
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002377 = MPI2_TYPE_CUDA;
2378 io_request->RaidContext.raid_context.nseg = 0x1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302379 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002380 io_request->RaidContext.raid_context.reg_lock_flags |=
adam radford36807e62011-10-08 18:15:06 -07002381 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
2382 MR_RL_FLAGS_SEQ_NUM_ENABLE);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002383 } else if (instance->is_ventura) {
2384 io_request->RaidContext.raid_context_g35.type
2385 = MPI2_TYPE_CUDA;
2386 io_request->RaidContext.raid_context_g35.nseg = 0x1;
2387 io_request->RaidContext.raid_context_g35.routing_flags.bits.sqn = 1;
2388 io_request->IoFlags |=
2389 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
adam radford36807e62011-10-08 18:15:06 -07002390 }
adam radford9c915a82010-12-21 13:34:31 -08002391 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
2392 (io_info.isRead)) {
2393 io_info.devHandle =
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05302394 get_updated_dev_handle(instance,
adam radford9c915a82010-12-21 13:34:31 -08002395 &fusion->load_balance_info[device_id],
Shivasharan S33203bc2017-02-10 00:59:12 -08002396 &io_info, local_map_ptr);
adam radford9c915a82010-12-21 13:34:31 -08002397 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05302398 cmd->pd_r1_lb = io_info.pd_after_lb;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002399 if (instance->is_ventura)
2400 io_request->RaidContext.raid_context_g35.span_arm
2401 = io_info.span_arm;
2402 else
2403 io_request->RaidContext.raid_context.span_arm
2404 = io_info.span_arm;
2405
adam radford9c915a82010-12-21 13:34:31 -08002406 } else
2407 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05302408
Shivasharan S8bf7c652017-02-10 00:59:03 -08002409 if (instance->is_ventura)
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002410 cmd->r1_alt_dev_handle = io_info.r1_alt_dev_handle;
Shivasharan S8bf7c652017-02-10 00:59:03 -08002411 else
2412 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002413
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05302414 if ((raidLUN[0] == 1) &&
sumit.saxena@avagotech.com75b96061e2015-08-31 17:24:01 +05302415 (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) {
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05302416 instance->dev_handle = !(instance->dev_handle);
2417 io_info.devHandle =
2418 local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
2419 }
2420
adam radford9c915a82010-12-21 13:34:31 -08002421 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
2422 io_request->DevHandle = io_info.devHandle;
Shivasharan S33203bc2017-02-10 00:59:12 -08002423 cmd->pd_interface = io_info.pd_interface;
adam radford21c9e162013-09-06 15:27:14 -07002424 /* populate the LUN field */
2425 memcpy(io_request->LUN, raidLUN, 8);
adam radford9c915a82010-12-21 13:34:31 -08002426 } else {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002427 io_request->RaidContext.raid_context.timeout_value =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302428 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
adam radford9c915a82010-12-21 13:34:31 -08002429 cmd->request_desc->SCSIIO.RequestFlags =
2430 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
2431 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05302432 if (fusion->adapter_type == INVADER_SERIES) {
Sumit Saxena8f050242016-01-28 21:04:27 +05302433 if (io_info.do_fp_rlbypass ||
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002434 (io_request->RaidContext.raid_context.reg_lock_flags
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002435 == REGION_TYPE_UNUSED))
adam radford36807e62011-10-08 18:15:06 -07002436 cmd->request_desc->SCSIIO.RequestFlags =
2437 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2438 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002439 io_request->RaidContext.raid_context.type
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002440 = MPI2_TYPE_CUDA;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002441 io_request->RaidContext.raid_context.reg_lock_flags |=
adam radford36807e62011-10-08 18:15:06 -07002442 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
2443 MR_RL_FLAGS_SEQ_NUM_ENABLE);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002444 io_request->RaidContext.raid_context.nseg = 0x1;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002445 } else if (instance->is_ventura) {
2446 io_request->RaidContext.raid_context_g35.type
2447 = MPI2_TYPE_CUDA;
2448 io_request->RaidContext.raid_context_g35.routing_flags.bits.sqn = 1;
2449 io_request->RaidContext.raid_context_g35.nseg = 0x1;
adam radford36807e62011-10-08 18:15:06 -07002450 }
2451 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302452 io_request->DevHandle = cpu_to_le16(device_id);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002453
adam radford9c915a82010-12-21 13:34:31 -08002454 } /* Not FP */
2455}
2456
2457/**
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302458 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
adam radford9c915a82010-12-21 13:34:31 -08002459 * @instance: Adapter soft state
2460 * @scp: SCSI command
2461 * @cmd: Command to be prepared
2462 *
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302463 * Prepares the io_request frame for non-rw io cmds for vd.
adam radford9c915a82010-12-21 13:34:31 -08002464 */
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302465static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
2466 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
adam radford9c915a82010-12-21 13:34:31 -08002467{
2468 u32 device_id;
2469 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2470 u16 pd_index = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302471 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
adam radford9c915a82010-12-21 13:34:31 -08002472 struct fusion_context *fusion = instance->ctrl_context;
adam radford21c9e162013-09-06 15:27:14 -07002473 u8 span, physArm;
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05302474 __le16 devHandle;
adam radford21c9e162013-09-06 15:27:14 -07002475 u32 ld, arRef, pd;
2476 struct MR_LD_RAID *raid;
2477 struct RAID_CONTEXT *pRAID_Context;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302478 u8 fp_possible = 1;
adam radford9c915a82010-12-21 13:34:31 -08002479
2480 io_request = cmd->io_request;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302481 device_id = MEGASAS_DEV_INDEX(scmd);
2482 pd_index = MEGASAS_PD_INDEX(scmd);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302483 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302484 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302485 /* get RAID_Context pointer */
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002486 pRAID_Context = &io_request->RaidContext.raid_context;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302487 /* Check with FW team */
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002488 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2489 pRAID_Context->reg_lock_row_lba = 0;
2490 pRAID_Context->reg_lock_length = 0;
adam radford21c9e162013-09-06 15:27:14 -07002491
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302492 if (fusion->fast_path_io && (
2493 device_id < instance->fw_supported_vd_count)) {
Sumit.Saxena@avagotech.comab2f0602015-01-05 20:06:08 +05302494
adam radford21c9e162013-09-06 15:27:14 -07002495 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
Sumit.Saxena@avagotech.comab2f0602015-01-05 20:06:08 +05302496 if (ld >= instance->fw_supported_vd_count)
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302497 fp_possible = 0;
adam radford21c9e162013-09-06 15:27:14 -07002498
2499 raid = MR_LdRaidGet(ld, local_map_ptr);
adam radford21c9e162013-09-06 15:27:14 -07002500 if (!(raid->capability.fpNonRWCapable))
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302501 fp_possible = 0;
2502 } else
2503 fp_possible = 0;
adam radford21c9e162013-09-06 15:27:14 -07002504
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302505 if (!fp_possible) {
2506 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2507 io_request->DevHandle = cpu_to_le16(device_id);
2508 io_request->LUN[1] = scmd->device->lun;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002509 pRAID_Context->timeout_value =
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302510 cpu_to_le16 (scmd->request->timeout / HZ);
2511 cmd->request_desc->SCSIIO.RequestFlags =
2512 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2513 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2514 } else {
adam radford21c9e162013-09-06 15:27:14 -07002515
2516 /* set RAID context values */
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002517 pRAID_Context->config_seq_num = raid->seqNum;
2518 if (!instance->is_ventura)
2519 pRAID_Context->reg_lock_flags = REGION_TYPE_SHARED_READ;
2520 pRAID_Context->timeout_value =
2521 cpu_to_le16(raid->fpIoTimeoutForLd);
adam radford21c9e162013-09-06 15:27:14 -07002522
2523 /* get the DevHandle for the PD (since this is
2524 fpNonRWCapable, this is a single disk RAID0) */
2525 span = physArm = 0;
2526 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
2527 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
2528 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
2529
2530 /* build request descriptor */
2531 cmd->request_desc->SCSIIO.RequestFlags =
Sumit Saxena2c048352016-01-28 21:04:24 +05302532 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302533 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford21c9e162013-09-06 15:27:14 -07002534 cmd->request_desc->SCSIIO.DevHandle = devHandle;
2535
2536 /* populate the LUN field */
2537 memcpy(io_request->LUN, raid->LUN, 8);
2538
2539 /* build the raidScsiIO structure */
2540 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2541 io_request->DevHandle = devHandle;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302542 }
2543}
adam radford21c9e162013-09-06 15:27:14 -07002544
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302545/**
2546 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
2547 * @instance: Adapter soft state
2548 * @scp: SCSI command
2549 * @cmd: Command to be prepared
2550 * @fp_possible: parameter to detect fast path or firmware path io.
2551 *
2552 * Prepares the io_request frame for rw/non-rw io cmds for syspds
2553 */
2554static void
2555megasas_build_syspd_fusion(struct megasas_instance *instance,
2556 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd, u8 fp_possible)
2557{
2558 u32 device_id;
2559 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2560 u16 pd_index = 0;
2561 u16 os_timeout_value;
2562 u16 timeout_limit;
2563 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2564 struct RAID_CONTEXT *pRAID_Context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302565 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
Shivasharan S33203bc2017-02-10 00:59:12 -08002566 struct MR_PRIV_DEVICE *mr_device_priv_data;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302567 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302568 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
adam radford21c9e162013-09-06 15:27:14 -07002569
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302570 device_id = MEGASAS_DEV_INDEX(scmd);
2571 pd_index = MEGASAS_PD_INDEX(scmd);
2572 os_timeout_value = scmd->request->timeout / HZ;
Shivasharan S33203bc2017-02-10 00:59:12 -08002573 mr_device_priv_data = scmd->device->hostdata;
2574 cmd->pd_interface = mr_device_priv_data->interface_type;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302575
2576 io_request = cmd->io_request;
2577 /* get RAID_Context pointer */
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002578 pRAID_Context = &io_request->RaidContext.raid_context;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002579 pRAID_Context->reg_lock_flags = 0;
2580 pRAID_Context->reg_lock_row_lba = 0;
2581 pRAID_Context->reg_lock_length = 0;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302582 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2583 io_request->LUN[1] = scmd->device->lun;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002584 pRAID_Context->raid_flags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302585 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
2586
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302587 /* If FW supports PD sequence number */
2588 if (instance->use_seqnum_jbod_fp &&
2589 instance->pd_list[pd_index].driveType == TYPE_DISK) {
2590 /* TgtId must be incremented by 255 as jbod seq number is index
2591 * below raid map
2592 */
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05002593 /* More than 256 PD/JBOD support for Ventura */
2594 if (instance->support_morethan256jbod)
2595 pRAID_Context->virtual_disk_tgt_id =
2596 pd_sync->seq[pd_index].pd_target_id;
2597 else
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002598 pRAID_Context->virtual_disk_tgt_id =
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302599 cpu_to_le16(device_id + (MAX_PHYSICAL_DEVICES - 1));
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002600 pRAID_Context->config_seq_num = pd_sync->seq[pd_index].seqNum;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302601 io_request->DevHandle = pd_sync->seq[pd_index].devHandle;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002602 if (instance->is_ventura)
2603 io_request->RaidContext.raid_context_g35.routing_flags.bits.sqn = 1;
2604 else
2605 pRAID_Context->reg_lock_flags |=
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302606 (MR_RL_FLAGS_SEQ_NUM_ENABLE|MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002607 pRAID_Context->type = MPI2_TYPE_CUDA;
Kashyap Desaid5573582016-10-21 06:33:35 -07002608 pRAID_Context->nseg = 0x1;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302609 } else if (fusion->fast_path_io) {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002610 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2611 pRAID_Context->config_seq_num = 0;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302612 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2613 io_request->DevHandle =
2614 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
2615 } else {
2616 /* Want to send all IO via FW path */
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002617 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2618 pRAID_Context->config_seq_num = 0;
Sumit Saxena9fb74c42015-11-05 21:17:38 +05302619 io_request->DevHandle = cpu_to_le16(0xFFFF);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302620 }
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302621
2622 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
2623 cmd->request_desc->SCSIIO.MSIxIndex =
2624 instance->msix_vectors ?
2625 (raw_smp_processor_id() % instance->msix_vectors) : 0;
2626
2627
2628 if (!fp_possible) {
2629 /* system pd firmware path */
adam radford9c915a82010-12-21 13:34:31 -08002630 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
adam radford9c915a82010-12-21 13:34:31 -08002631 cmd->request_desc->SCSIIO.RequestFlags =
2632 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302633 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002634 pRAID_Context->timeout_value = cpu_to_le16(os_timeout_value);
2635 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302636 } else {
2637 /* system pd Fast Path */
2638 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302639 timeout_limit = (scmd->device->type == TYPE_DISK) ?
2640 255 : 0xFFFF;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002641 pRAID_Context->timeout_value =
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302642 cpu_to_le16((os_timeout_value > timeout_limit) ?
2643 timeout_limit : os_timeout_value);
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05002644 if (fusion->adapter_type >= INVADER_SERIES)
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302645 io_request->IoFlags |=
2646 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
Kashyap Desaid5573582016-10-21 06:33:35 -07002647
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302648 cmd->request_desc->SCSIIO.RequestFlags =
Sumit Saxena2c048352016-01-28 21:04:24 +05302649 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302650 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford9c915a82010-12-21 13:34:31 -08002651 }
adam radford9c915a82010-12-21 13:34:31 -08002652}
2653
2654/**
2655 * megasas_build_io_fusion - Prepares IOs to devices
2656 * @instance: Adapter soft state
2657 * @scp: SCSI command
2658 * @cmd: Command to be prepared
2659 *
2660 * Invokes helper functions to prepare request frames
2661 * and sets flags appropriate for IO/Non-IO cmd
2662 */
2663int
2664megasas_build_io_fusion(struct megasas_instance *instance,
2665 struct scsi_cmnd *scp,
2666 struct megasas_cmd_fusion *cmd)
2667{
Shivasharan S33203bc2017-02-10 00:59:12 -08002668 int sge_count;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302669 u8 cmd_type;
adam radford9c915a82010-12-21 13:34:31 -08002670 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
2671
adam radford9c915a82010-12-21 13:34:31 -08002672 /* Zero out some fields so they don't get reused */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002673 memset(io_request->LUN, 0x0, 8);
adam radford9c915a82010-12-21 13:34:31 -08002674 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
2675 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
2676 io_request->EEDPFlags = 0;
2677 io_request->Control = 0;
2678 io_request->EEDPBlockSize = 0;
adam radford36807e62011-10-08 18:15:06 -07002679 io_request->ChainOffset = 0;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002680 io_request->RaidContext.raid_context.raid_flags = 0;
2681 io_request->RaidContext.raid_context.type = 0;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002682 io_request->RaidContext.raid_context.nseg = 0;
adam radford9c915a82010-12-21 13:34:31 -08002683
2684 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
2685 /*
2686 * Just the CDB length,rest of the Flags are zero
2687 * This will be modified for FP in build_ldio_fusion
2688 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302689 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
adam radford9c915a82010-12-21 13:34:31 -08002690
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302691 switch (cmd_type = megasas_cmd_type(scp)) {
2692 case READ_WRITE_LDIO:
adam radford9c915a82010-12-21 13:34:31 -08002693 megasas_build_ldio_fusion(instance, scp, cmd);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302694 break;
2695 case NON_READ_WRITE_LDIO:
2696 megasas_build_ld_nonrw_fusion(instance, scp, cmd);
2697 break;
2698 case READ_WRITE_SYSPDIO:
2699 case NON_READ_WRITE_SYSPDIO:
2700 if (instance->secure_jbod_support &&
2701 (cmd_type == NON_READ_WRITE_SYSPDIO))
2702 megasas_build_syspd_fusion(instance, scp, cmd, 0);
2703 else
2704 megasas_build_syspd_fusion(instance, scp, cmd, 1);
2705 break;
2706 default:
2707 break;
2708 }
adam radford9c915a82010-12-21 13:34:31 -08002709
2710 /*
2711 * Construct SGL
2712 */
2713
Shivasharan S33203bc2017-02-10 00:59:12 -08002714 sge_count = megasas_make_sgl(instance, scp, cmd);
adam radford9c915a82010-12-21 13:34:31 -08002715
Shivasharan S33203bc2017-02-10 00:59:12 -08002716 if (sge_count > instance->max_num_sge || (sge_count < 0)) {
2717 dev_err(&instance->pdev->dev,
2718 "%s %d sge_count (%d) is out of range. Range is: 0-%d\n",
2719 __func__, __LINE__, sge_count, instance->max_num_sge);
adam radford9c915a82010-12-21 13:34:31 -08002720 return 1;
2721 }
2722
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002723 if (instance->is_ventura)
2724 io_request->RaidContext.raid_context_g35.num_sge = sge_count;
2725 else {
2726 /* numSGE store lower 8 bit of sge_count.
2727 * numSGEExt store higher 8 bit of sge_count
2728 */
2729 io_request->RaidContext.raid_context.num_sge = sge_count;
2730 io_request->RaidContext.raid_context.num_sge_ext =
2731 (u8)(sge_count >> 8);
2732 }
adam radford9c915a82010-12-21 13:34:31 -08002733
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302734 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
adam radford9c915a82010-12-21 13:34:31 -08002735
2736 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302737 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
adam radford9c915a82010-12-21 13:34:31 -08002738 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302739 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
adam radford9c915a82010-12-21 13:34:31 -08002740
2741 io_request->SGLOffset0 =
2742 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
2743
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302744 io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08002745 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
2746
2747 cmd->scmd = scp;
2748 scp->SCp.ptr = (char *)cmd;
2749
2750 return 0;
2751}
2752
Shivasharan S52205ac2017-02-10 00:59:08 -08002753static union MEGASAS_REQUEST_DESCRIPTOR_UNION *
adam radford9c915a82010-12-21 13:34:31 -08002754megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
2755{
2756 u8 *p;
2757 struct fusion_context *fusion;
2758
adam radford9c915a82010-12-21 13:34:31 -08002759 fusion = instance->ctrl_context;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002760 p = fusion->req_frames_desc +
2761 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * index;
adam radford9c915a82010-12-21 13:34:31 -08002762
2763 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
2764}
2765
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002766
2767/* megasas_prepate_secondRaid1_IO
2768 * It prepares the raid 1 second IO
2769 */
2770void megasas_prepare_secondRaid1_IO(struct megasas_instance *instance,
2771 struct megasas_cmd_fusion *cmd,
2772 struct megasas_cmd_fusion *r1_cmd)
2773{
2774 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc, *req_desc2 = NULL;
2775 struct fusion_context *fusion;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002776 fusion = instance->ctrl_context;
2777 req_desc = cmd->request_desc;
Shivasharan S8bf7c652017-02-10 00:59:03 -08002778 /* copy the io request frame as well as 8 SGEs data for r1 command*/
2779 memcpy(r1_cmd->io_request, cmd->io_request,
2780 (sizeof(struct MPI2_RAID_SCSI_IO_REQUEST)));
2781 memcpy(&r1_cmd->io_request->SGL, &cmd->io_request->SGL,
2782 (fusion->max_sge_in_main_msg * sizeof(union MPI2_SGE_IO_UNION)));
2783 /*sense buffer is different for r1 command*/
2784 r1_cmd->io_request->SenseBufferLowAddress =
2785 cpu_to_le32(r1_cmd->sense_phys_addr);
2786 r1_cmd->scmd = cmd->scmd;
2787 req_desc2 = megasas_get_request_descriptor(instance,
2788 (r1_cmd->index - 1));
2789 req_desc2->Words = 0;
2790 r1_cmd->request_desc = req_desc2;
2791 req_desc2->SCSIIO.SMID = cpu_to_le16(r1_cmd->index);
2792 req_desc2->SCSIIO.RequestFlags = req_desc->SCSIIO.RequestFlags;
2793 r1_cmd->request_desc->SCSIIO.DevHandle = cmd->r1_alt_dev_handle;
2794 r1_cmd->io_request->DevHandle = cmd->r1_alt_dev_handle;
2795 r1_cmd->r1_alt_dev_handle = cmd->io_request->DevHandle;
2796 cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
2797 cpu_to_le16(r1_cmd->index);
2798 r1_cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
2799 cpu_to_le16(cmd->index);
2800 /*MSIxIndex of both commands request descriptors should be same*/
2801 r1_cmd->request_desc->SCSIIO.MSIxIndex =
2802 cmd->request_desc->SCSIIO.MSIxIndex;
2803 /*span arm is different for r1 cmd*/
2804 r1_cmd->io_request->RaidContext.raid_context_g35.span_arm =
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002805 cmd->io_request->RaidContext.raid_context_g35.span_arm + 1;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002806}
2807
adam radford9c915a82010-12-21 13:34:31 -08002808/**
2809 * megasas_build_and_issue_cmd_fusion -Main routine for building and
2810 * issuing non IOCTL cmd
2811 * @instance: Adapter soft state
2812 * @scmd: pointer to scsi cmd from OS
2813 */
2814static u32
2815megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
2816 struct scsi_cmnd *scmd)
2817{
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002818 struct megasas_cmd_fusion *cmd, *r1_cmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -08002819 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2820 u32 index;
2821 struct fusion_context *fusion;
2822
2823 fusion = instance->ctrl_context;
2824
Sumit Saxena308ec452016-01-28 21:04:30 +05302825 if ((megasas_cmd_type(scmd) == READ_WRITE_LDIO) &&
2826 instance->ldio_threshold &&
2827 (atomic_inc_return(&instance->ldio_outstanding) >
2828 instance->ldio_threshold)) {
2829 atomic_dec(&instance->ldio_outstanding);
2830 return SCSI_MLQUEUE_DEVICE_BUSY;
2831 }
2832
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002833 if (atomic_inc_return(&instance->fw_outstanding) >
2834 instance->host->can_queue) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002835 atomic_dec(&instance->fw_outstanding);
2836 return SCSI_MLQUEUE_HOST_BUSY;
2837 }
2838
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302839 cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
adam radford9c915a82010-12-21 13:34:31 -08002840
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002841 if (!cmd) {
2842 atomic_dec(&instance->fw_outstanding);
2843 return SCSI_MLQUEUE_HOST_BUSY;
2844 }
2845
adam radford9c915a82010-12-21 13:34:31 -08002846 index = cmd->index;
2847
2848 req_desc = megasas_get_request_descriptor(instance, index-1);
adam radford9c915a82010-12-21 13:34:31 -08002849
2850 req_desc->Words = 0;
2851 cmd->request_desc = req_desc;
adam radford9c915a82010-12-21 13:34:31 -08002852
2853 if (megasas_build_io_fusion(instance, scmd, cmd)) {
2854 megasas_return_cmd_fusion(instance, cmd);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002855 dev_err(&instance->pdev->dev, "Error building command\n");
adam radford9c915a82010-12-21 13:34:31 -08002856 cmd->request_desc = NULL;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002857 atomic_dec(&instance->fw_outstanding);
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05302858 return SCSI_MLQUEUE_HOST_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08002859 }
2860
2861 req_desc = cmd->request_desc;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302862 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08002863
2864 if (cmd->io_request->ChainOffset != 0 &&
2865 cmd->io_request->ChainOffset != 0xF)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002866 dev_err(&instance->pdev->dev, "The chain offset value is not "
adam radford9c915a82010-12-21 13:34:31 -08002867 "correct : %x\n", cmd->io_request->ChainOffset);
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002868 /*
2869 * if it is raid 1/10 fp write capable.
2870 * try to get second command from pool and construct it.
2871 * From FW, it has confirmed that lba values of two PDs
2872 * corresponds to single R1/10 LD are always same
2873 *
2874 */
2875 /* driver side count always should be less than max_fw_cmds
2876 * to get new command
2877 */
Shivasharan S8bf7c652017-02-10 00:59:03 -08002878 if (cmd->r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002879 r1_cmd = megasas_get_cmd_fusion(instance,
2880 (scmd->request->tag + instance->max_fw_cmds));
2881 megasas_prepare_secondRaid1_IO(instance, cmd, r1_cmd);
2882 }
2883
Shivasharan S18bbcab2017-02-10 00:59:01 -08002884
adam radford9c915a82010-12-21 13:34:31 -08002885 /*
2886 * Issue the command to the FW
2887 */
adam radford9c915a82010-12-21 13:34:31 -08002888
Shivasharan S45b8a352017-02-10 00:59:04 -08002889 megasas_fire_cmd_fusion(instance, req_desc);
adam radford9c915a82010-12-21 13:34:31 -08002890
Shivasharan S18bbcab2017-02-10 00:59:01 -08002891 if (r1_cmd)
Shivasharan S45b8a352017-02-10 00:59:04 -08002892 megasas_fire_cmd_fusion(instance, r1_cmd->request_desc);
Shivasharan S18bbcab2017-02-10 00:59:01 -08002893
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002894
adam radford9c915a82010-12-21 13:34:31 -08002895 return 0;
2896}
2897
2898/**
Shivasharan S8bf7c652017-02-10 00:59:03 -08002899 * megasas_complete_r1_command -
2900 * completes R1 FP write commands which has valid peer smid
2901 * @instance: Adapter soft state
2902 * @cmd_fusion: MPT command frame
2903 *
2904 */
2905static inline void
2906megasas_complete_r1_command(struct megasas_instance *instance,
2907 struct megasas_cmd_fusion *cmd)
2908{
2909 u8 *sense, status, ex_status;
2910 u32 data_length;
2911 u16 peer_smid;
2912 struct fusion_context *fusion;
2913 struct megasas_cmd_fusion *r1_cmd = NULL;
2914 struct scsi_cmnd *scmd_local = NULL;
2915 struct RAID_CONTEXT_G35 *rctx_g35;
2916
2917 rctx_g35 = &cmd->io_request->RaidContext.raid_context_g35;
2918 fusion = instance->ctrl_context;
2919 peer_smid = le16_to_cpu(rctx_g35->smid.peer_smid);
2920
2921 r1_cmd = fusion->cmd_list[peer_smid - 1];
2922 scmd_local = cmd->scmd;
2923 status = rctx_g35->status;
2924 ex_status = rctx_g35->ex_status;
2925 data_length = cmd->io_request->DataLength;
2926 sense = cmd->sense;
2927
2928 cmd->cmd_completed = true;
2929
2930 /* Check if peer command is completed or not*/
2931 if (r1_cmd->cmd_completed) {
2932 rctx_g35 = &r1_cmd->io_request->RaidContext.raid_context_g35;
2933 if (rctx_g35->status != MFI_STAT_OK) {
2934 status = rctx_g35->status;
2935 ex_status = rctx_g35->ex_status;
2936 data_length = r1_cmd->io_request->DataLength;
2937 sense = r1_cmd->sense;
2938 }
2939
2940 megasas_return_cmd_fusion(instance, r1_cmd);
2941 map_cmd_status(fusion, scmd_local, status, ex_status,
2942 le32_to_cpu(data_length), sense);
2943 if (instance->ldio_threshold &&
2944 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
2945 atomic_dec(&instance->ldio_outstanding);
2946 scmd_local->SCp.ptr = NULL;
2947 megasas_return_cmd_fusion(instance, cmd);
2948 scsi_dma_unmap(scmd_local);
2949 scmd_local->scsi_done(scmd_local);
2950 }
2951}
2952
2953/**
adam radford9c915a82010-12-21 13:34:31 -08002954 * complete_cmd_fusion - Completes command
2955 * @instance: Adapter soft state
2956 * Completes all commands that is in reply descriptor queue
2957 */
2958int
adam radfordc8e858f2011-10-08 18:15:13 -07002959complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
adam radford9c915a82010-12-21 13:34:31 -08002960{
2961 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
2962 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
2963 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
2964 struct fusion_context *fusion;
2965 struct megasas_cmd *cmd_mfi;
Shivasharan S8bf7c652017-02-10 00:59:03 -08002966 struct megasas_cmd_fusion *cmd_fusion;
adam radford9c915a82010-12-21 13:34:31 -08002967 u16 smid, num_completed;
Shivasharan S8bf7c652017-02-10 00:59:03 -08002968 u8 reply_descript_type, *sense, status, extStatus;
2969 u32 device_id, data_length;
adam radford9c915a82010-12-21 13:34:31 -08002970 union desc_value d_val;
2971 struct LD_LOAD_BALANCE_INFO *lbinfo;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05302972 int threshold_reply_count = 0;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302973 struct scsi_cmnd *scmd_local = NULL;
Sumit Saxena18365b12016-01-28 21:04:25 +05302974 struct MR_TASK_MANAGE_REQUEST *mr_tm_req;
2975 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req;
adam radford9c915a82010-12-21 13:34:31 -08002976
2977 fusion = instance->ctrl_context;
2978
Sumit Saxena8a01a412016-01-28 21:04:32 +05302979 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
adam radford9c915a82010-12-21 13:34:31 -08002980 return IRQ_HANDLED;
2981
Sumit Saxena179ac142016-01-28 21:04:28 +05302982 desc = fusion->reply_frames_desc[MSIxIndex] +
2983 fusion->last_reply_idx[MSIxIndex];
adam radford9c915a82010-12-21 13:34:31 -08002984
2985 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
2986
2987 d_val.word = desc->Words;
2988
2989 reply_descript_type = reply_desc->ReplyFlags &
2990 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
2991
2992 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
2993 return IRQ_NONE;
2994
adam radford9c915a82010-12-21 13:34:31 -08002995 num_completed = 0;
2996
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05302997 while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
2998 d_val.u.high != cpu_to_le32(UINT_MAX)) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002999
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303000 smid = le16_to_cpu(reply_desc->SMID);
adam radford9c915a82010-12-21 13:34:31 -08003001 cmd_fusion = fusion->cmd_list[smid - 1];
Shivasharan S8bf7c652017-02-10 00:59:03 -08003002 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
3003 cmd_fusion->io_request;
adam radford9c915a82010-12-21 13:34:31 -08003004
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303005 scmd_local = cmd_fusion->scmd;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05003006 status = scsi_io_req->RaidContext.raid_context.status;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05003007 extStatus = scsi_io_req->RaidContext.raid_context.ex_status;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003008 sense = cmd_fusion->sense;
3009 data_length = scsi_io_req->DataLength;
adam radford9c915a82010-12-21 13:34:31 -08003010
3011 switch (scsi_io_req->Function) {
Sumit Saxena18365b12016-01-28 21:04:25 +05303012 case MPI2_FUNCTION_SCSI_TASK_MGMT:
3013 mr_tm_req = (struct MR_TASK_MANAGE_REQUEST *)
3014 cmd_fusion->io_request;
3015 mpi_tm_req = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *)
3016 &mr_tm_req->TmRequest;
3017 dev_dbg(&instance->pdev->dev, "TM completion:"
3018 "type: 0x%x TaskMID: 0x%x\n",
3019 mpi_tm_req->TaskType, mpi_tm_req->TaskMID);
3020 complete(&cmd_fusion->done);
3021 break;
adam radford9c915a82010-12-21 13:34:31 -08003022 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
3023 /* Update load balancing info */
Shivasharan S8bf7c652017-02-10 00:59:03 -08003024 if (fusion->load_balance_info &&
3025 (cmd_fusion->scmd->SCp.Status &
3026 MEGASAS_LOAD_BALANCE_FLAG)) {
3027 device_id = MEGASAS_DEV_INDEX(scmd_local);
3028 lbinfo = &fusion->load_balance_info[device_id];
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05303029 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
Shivasharan S8bf7c652017-02-10 00:59:03 -08003030 cmd_fusion->scmd->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
adam radford9c915a82010-12-21 13:34:31 -08003031 }
Shivasharan S8bf7c652017-02-10 00:59:03 -08003032 //Fall thru and complete IO
adam radford9c915a82010-12-21 13:34:31 -08003033 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
Shivasharan S8bf7c652017-02-10 00:59:03 -08003034 atomic_dec(&instance->fw_outstanding);
3035 if (cmd_fusion->r1_alt_dev_handle == MR_DEVHANDLE_INVALID) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003036 map_cmd_status(fusion, scmd_local, status,
Shivasharan S8bf7c652017-02-10 00:59:03 -08003037 extStatus, le32_to_cpu(data_length),
3038 sense);
3039 if (instance->ldio_threshold &&
3040 (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO))
Sasikumar Chandrasekaranb71b49c2017-01-10 18:20:51 -05003041 atomic_dec(&instance->ldio_outstanding);
Shivasharan S8bf7c652017-02-10 00:59:03 -08003042 scmd_local->SCp.ptr = NULL;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003043 megasas_return_cmd_fusion(instance, cmd_fusion);
3044 scsi_dma_unmap(scmd_local);
3045 scmd_local->scsi_done(scmd_local);
Shivasharan S8bf7c652017-02-10 00:59:03 -08003046 } else /* Optimal VD - R1 FP command completion. */
3047 megasas_complete_r1_command(instance, cmd_fusion);
adam radford9c915a82010-12-21 13:34:31 -08003048 break;
3049 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
3050 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303051 /* Poll mode. Dummy free.
3052 * In case of Interrupt mode, caller has reverse check.
3053 */
3054 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
3055 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
3056 megasas_return_cmd(instance, cmd_mfi);
3057 } else
3058 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
adam radford9c915a82010-12-21 13:34:31 -08003059 break;
3060 }
3061
adam radfordc8e858f2011-10-08 18:15:13 -07003062 fusion->last_reply_idx[MSIxIndex]++;
3063 if (fusion->last_reply_idx[MSIxIndex] >=
3064 fusion->reply_q_depth)
3065 fusion->last_reply_idx[MSIxIndex] = 0;
adam radford9c915a82010-12-21 13:34:31 -08003066
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05303067 desc->Words = cpu_to_le64(ULLONG_MAX);
adam radford9c915a82010-12-21 13:34:31 -08003068 num_completed++;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05303069 threshold_reply_count++;
adam radford9c915a82010-12-21 13:34:31 -08003070
3071 /* Get the next reply descriptor */
adam radfordc8e858f2011-10-08 18:15:13 -07003072 if (!fusion->last_reply_idx[MSIxIndex])
Sumit Saxena179ac142016-01-28 21:04:28 +05303073 desc = fusion->reply_frames_desc[MSIxIndex];
adam radford9c915a82010-12-21 13:34:31 -08003074 else
3075 desc++;
3076
3077 reply_desc =
3078 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3079
3080 d_val.word = desc->Words;
3081
3082 reply_descript_type = reply_desc->ReplyFlags &
3083 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3084
3085 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3086 break;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05303087 /*
3088 * Write to reply post host index register after completing threshold
3089 * number of reply counts and still there are more replies in reply queue
3090 * pending to be completed
3091 */
3092 if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05003093 if (instance->msix_combined)
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05303094 writel(((MSIxIndex & 0x7) << 24) |
3095 fusion->last_reply_idx[MSIxIndex],
3096 instance->reply_post_host_index_addr[MSIxIndex/8]);
3097 else
3098 writel((MSIxIndex << 24) |
3099 fusion->last_reply_idx[MSIxIndex],
3100 instance->reply_post_host_index_addr[0]);
3101 threshold_reply_count = 0;
3102 }
adam radford9c915a82010-12-21 13:34:31 -08003103 }
3104
3105 if (!num_completed)
3106 return IRQ_NONE;
3107
3108 wmb();
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05003109 if (instance->msix_combined)
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303110 writel(((MSIxIndex & 0x7) << 24) |
3111 fusion->last_reply_idx[MSIxIndex],
3112 instance->reply_post_host_index_addr[MSIxIndex/8]);
3113 else
3114 writel((MSIxIndex << 24) |
3115 fusion->last_reply_idx[MSIxIndex],
3116 instance->reply_post_host_index_addr[0]);
adam radford53ef2bb2011-02-24 20:56:05 -08003117 megasas_check_and_restore_queue_depth(instance);
adam radford9c915a82010-12-21 13:34:31 -08003118 return IRQ_HANDLED;
3119}
3120
3121/**
3122 * megasas_complete_cmd_dpc_fusion - Completes command
3123 * @instance: Adapter soft state
3124 *
3125 * Tasklet to complete cmds
3126 */
3127void
3128megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
3129{
3130 struct megasas_instance *instance =
3131 (struct megasas_instance *)instance_addr;
3132 unsigned long flags;
adam radfordc8e858f2011-10-08 18:15:13 -07003133 u32 count, MSIxIndex;
3134
3135 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
adam radford9c915a82010-12-21 13:34:31 -08003136
3137 /* If we have already declared adapter dead, donot complete cmds */
3138 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303139 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
adam radford9c915a82010-12-21 13:34:31 -08003140 spin_unlock_irqrestore(&instance->hba_lock, flags);
3141 return;
3142 }
3143 spin_unlock_irqrestore(&instance->hba_lock, flags);
3144
adam radfordc8e858f2011-10-08 18:15:13 -07003145 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
3146 complete_cmd_fusion(instance, MSIxIndex);
adam radford9c915a82010-12-21 13:34:31 -08003147}
3148
3149/**
3150 * megasas_isr_fusion - isr entry point
3151 */
3152irqreturn_t megasas_isr_fusion(int irq, void *devp)
3153{
adam radfordc8e858f2011-10-08 18:15:13 -07003154 struct megasas_irq_context *irq_context = devp;
3155 struct megasas_instance *instance = irq_context->instance;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303156 u32 mfiStatus, fw_state, dma_state;
adam radford9c915a82010-12-21 13:34:31 -08003157
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303158 if (instance->mask_interrupts)
3159 return IRQ_NONE;
3160
adam radfordc8e858f2011-10-08 18:15:13 -07003161 if (!instance->msix_vectors) {
adam radford9c915a82010-12-21 13:34:31 -08003162 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
3163 if (!mfiStatus)
3164 return IRQ_NONE;
3165 }
3166
3167 /* If we are resetting, bail */
adam radford6497b242011-10-08 18:14:50 -07003168 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
3169 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08003170 return IRQ_HANDLED;
adam radford6497b242011-10-08 18:14:50 -07003171 }
adam radford9c915a82010-12-21 13:34:31 -08003172
adam radfordc8e858f2011-10-08 18:15:13 -07003173 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
adam radford6497b242011-10-08 18:14:50 -07003174 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08003175 /* If we didn't complete any commands, check for FW fault */
3176 fw_state = instance->instancet->read_fw_status_reg(
3177 instance->reg_set) & MFI_STATE_MASK;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303178 dma_state = instance->instancet->read_fw_status_reg
3179 (instance->reg_set) & MFI_STATE_DMADONE;
3180 if (instance->crash_dump_drv_support &&
3181 instance->crash_dump_app_support) {
3182 /* Start collecting crash, if DMA bit is done */
3183 if ((fw_state == MFI_STATE_FAULT) && dma_state)
3184 schedule_work(&instance->crash_init);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07003185 else if (fw_state == MFI_STATE_FAULT) {
3186 if (instance->unload == 0)
3187 schedule_work(&instance->work_init);
3188 }
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303189 } else if (fw_state == MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003190 dev_warn(&instance->pdev->dev, "Iop2SysDoorbellInt"
adam radford229fe472014-03-10 02:51:56 -07003191 "for scsi%d\n", instance->host->host_no);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07003192 if (instance->unload == 0)
3193 schedule_work(&instance->work_init);
adam radford229fe472014-03-10 02:51:56 -07003194 }
adam radford9c915a82010-12-21 13:34:31 -08003195 }
3196
3197 return IRQ_HANDLED;
3198}
3199
3200/**
3201 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
3202 * @instance: Adapter soft state
3203 * mfi_cmd: megasas_cmd pointer
3204 *
3205 */
3206u8
3207build_mpt_mfi_pass_thru(struct megasas_instance *instance,
3208 struct megasas_cmd *mfi_cmd)
3209{
3210 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
3211 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
3212 struct megasas_cmd_fusion *cmd;
3213 struct fusion_context *fusion;
3214 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
3215
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303216 fusion = instance->ctrl_context;
3217
3218 cmd = megasas_get_cmd_fusion(instance,
3219 instance->max_scsi_cmds + mfi_cmd->index);
adam radford9c915a82010-12-21 13:34:31 -08003220
3221 /* Save the smid. To be used for returning the cmd */
3222 mfi_cmd->context.smid = cmd->index;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303223
adam radford9c915a82010-12-21 13:34:31 -08003224 /*
3225 * For cmds where the flag is set, store the flag and check
3226 * on completion. For cmds with this flag, don't call
3227 * megasas_complete_cmd
3228 */
3229
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05303230 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303231 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
adam radford9c915a82010-12-21 13:34:31 -08003232
adam radford9c915a82010-12-21 13:34:31 -08003233 io_req = cmd->io_request;
adam radford36807e62011-10-08 18:15:06 -07003234
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05003235 if (fusion->adapter_type >= INVADER_SERIES) {
adam radford36807e62011-10-08 18:15:06 -07003236 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
3237 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
3238 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
3239 sgl_ptr_end->Flags = 0;
3240 }
3241
adam radford9c915a82010-12-21 13:34:31 -08003242 mpi25_ieee_chain =
3243 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
3244
3245 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
3246 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
3247 SGL) / 4;
3248 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
3249
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303250 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08003251
3252 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
3253 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
3254
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05303255 mpi25_ieee_chain->Length = cpu_to_le32(instance->max_chain_frame_sz);
adam radford9c915a82010-12-21 13:34:31 -08003256
3257 return 0;
3258}
3259
3260/**
3261 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
3262 * @instance: Adapter soft state
3263 * @cmd: mfi cmd to build
3264 *
3265 */
3266union MEGASAS_REQUEST_DESCRIPTOR_UNION *
3267build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
3268{
Shivasharan S52205ac2017-02-10 00:59:08 -08003269 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc = NULL;
adam radford9c915a82010-12-21 13:34:31 -08003270 u16 index;
3271
3272 if (build_mpt_mfi_pass_thru(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003273 dev_err(&instance->pdev->dev, "Couldn't build MFI pass thru cmd\n");
adam radford9c915a82010-12-21 13:34:31 -08003274 return NULL;
3275 }
3276
3277 index = cmd->context.smid;
3278
3279 req_desc = megasas_get_request_descriptor(instance, index - 1);
3280
adam radford9c915a82010-12-21 13:34:31 -08003281 req_desc->Words = 0;
3282 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3283 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3284
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303285 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08003286
3287 return req_desc;
3288}
3289
3290/**
3291 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
3292 * @instance: Adapter soft state
3293 * @cmd: mfi cmd pointer
3294 *
3295 */
Shivasharan Sf4fc2092017-02-10 00:59:09 -08003296void
adam radford9c915a82010-12-21 13:34:31 -08003297megasas_issue_dcmd_fusion(struct megasas_instance *instance,
3298 struct megasas_cmd *cmd)
3299{
3300 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
adam radford9c915a82010-12-21 13:34:31 -08003301
3302 req_desc = build_mpt_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303303
Shivasharan S45b8a352017-02-10 00:59:04 -08003304 megasas_fire_cmd_fusion(instance, req_desc);
Shivasharan Sf4fc2092017-02-10 00:59:09 -08003305 return;
adam radford9c915a82010-12-21 13:34:31 -08003306}
3307
3308/**
3309 * megasas_release_fusion - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01003310 * @instance: Adapter soft state
adam radford9c915a82010-12-21 13:34:31 -08003311 */
3312void
3313megasas_release_fusion(struct megasas_instance *instance)
3314{
3315 megasas_free_cmds(instance);
3316 megasas_free_cmds_fusion(instance);
3317
3318 iounmap(instance->reg_set);
3319
Yinghai Lue7f85162016-08-05 23:37:34 -07003320 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
adam radford9c915a82010-12-21 13:34:31 -08003321}
3322
3323/**
3324 * megasas_read_fw_status_reg_fusion - returns the current FW status value
3325 * @regs: MFI register set
3326 */
3327static u32
3328megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
3329{
3330 return readl(&(regs)->outbound_scratch_pad);
3331}
3332
3333/**
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303334 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3335 * @instance: Controller's soft instance
3336 * return: Number of allocated host crash buffers
3337 */
3338static void
3339megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
3340{
3341 unsigned int i;
3342
3343 instance->crash_buf_pages = get_order(CRASH_DMA_BUF_SIZE);
3344 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
3345 instance->crash_buf[i] = (void *)__get_free_pages(GFP_KERNEL,
3346 instance->crash_buf_pages);
3347 if (!instance->crash_buf[i]) {
3348 dev_info(&instance->pdev->dev, "Firmware crash dump "
3349 "memory allocation failed at index %d\n", i);
3350 break;
3351 }
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05303352 memset(instance->crash_buf[i], 0,
3353 ((1 << PAGE_SHIFT) << instance->crash_buf_pages));
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303354 }
3355 instance->drv_buf_alloc = i;
3356}
3357
3358/**
3359 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3360 * @instance: Controller's soft instance
3361 */
3362void
3363megasas_free_host_crash_buffer(struct megasas_instance *instance)
3364{
3365 unsigned int i
3366;
3367 for (i = 0; i < instance->drv_buf_alloc; i++) {
3368 if (instance->crash_buf[i])
3369 free_pages((ulong)instance->crash_buf[i],
3370 instance->crash_buf_pages);
3371 }
3372 instance->drv_buf_index = 0;
3373 instance->drv_buf_alloc = 0;
3374 instance->fw_crash_state = UNAVAILABLE;
3375 instance->fw_crash_buffer_size = 0;
3376}
3377
3378/**
adam radford9c915a82010-12-21 13:34:31 -08003379 * megasas_adp_reset_fusion - For controller reset
3380 * @regs: MFI register set
3381 */
3382static int
3383megasas_adp_reset_fusion(struct megasas_instance *instance,
3384 struct megasas_register_set __iomem *regs)
3385{
sumit.saxena@avagotech.com79b82c22015-08-31 17:23:41 +05303386 u32 host_diag, abs_state, retry;
3387
3388 /* Now try to reset the chip */
3389 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3390 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3391 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3392 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3393 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3394 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3395 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3396
3397 /* Check that the diag write enable (DRWE) bit is on */
3398 host_diag = readl(&instance->reg_set->fusion_host_diag);
3399 retry = 0;
3400 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
3401 msleep(100);
3402 host_diag = readl(&instance->reg_set->fusion_host_diag);
3403 if (retry++ == 100) {
3404 dev_warn(&instance->pdev->dev,
3405 "Host diag unlock failed from %s %d\n",
3406 __func__, __LINE__);
3407 break;
3408 }
3409 }
3410 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
3411 return -1;
3412
3413 /* Send chip reset command */
3414 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
3415 &instance->reg_set->fusion_host_diag);
3416 msleep(3000);
3417
3418 /* Make sure reset adapter bit is cleared */
3419 host_diag = readl(&instance->reg_set->fusion_host_diag);
3420 retry = 0;
3421 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
3422 msleep(100);
3423 host_diag = readl(&instance->reg_set->fusion_host_diag);
3424 if (retry++ == 1000) {
3425 dev_warn(&instance->pdev->dev,
3426 "Diag reset adapter never cleared %s %d\n",
3427 __func__, __LINE__);
3428 break;
3429 }
3430 }
3431 if (host_diag & HOST_DIAG_RESET_ADAPTER)
3432 return -1;
3433
3434 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set)
3435 & MFI_STATE_MASK;
3436 retry = 0;
3437
3438 while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
3439 msleep(100);
3440 abs_state = instance->instancet->
3441 read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
3442 }
3443 if (abs_state <= MFI_STATE_FW_INIT) {
3444 dev_warn(&instance->pdev->dev,
3445 "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
3446 abs_state, __func__, __LINE__);
3447 return -1;
3448 }
3449
adam radford9c915a82010-12-21 13:34:31 -08003450 return 0;
3451}
3452
3453/**
3454 * megasas_check_reset_fusion - For controller reset check
3455 * @regs: MFI register set
3456 */
3457static int
3458megasas_check_reset_fusion(struct megasas_instance *instance,
3459 struct megasas_register_set __iomem *regs)
3460{
3461 return 0;
3462}
3463
3464/* This function waits for outstanding commands on fusion to complete */
adam radford229fe472014-03-10 02:51:56 -07003465int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303466 int reason, int *convert)
adam radford9c915a82010-12-21 13:34:31 -08003467{
adam radford229fe472014-03-10 02:51:56 -07003468 int i, outstanding, retval = 0, hb_seconds_missed = 0;
adam radfordc007b8b2012-07-17 18:20:24 -07003469 u32 fw_state;
adam radford9c915a82010-12-21 13:34:31 -08003470
adam radfordc007b8b2012-07-17 18:20:24 -07003471 for (i = 0; i < resetwaittime; i++) {
adam radford9c915a82010-12-21 13:34:31 -08003472 /* Check if firmware is in fault state */
3473 fw_state = instance->instancet->read_fw_status_reg(
3474 instance->reg_set) & MFI_STATE_MASK;
3475 if (fw_state == MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003476 dev_warn(&instance->pdev->dev, "Found FW in FAULT state,"
adam radford229fe472014-03-10 02:51:56 -07003477 " will reset adapter scsi%d.\n",
3478 instance->host->host_no);
Sumit Saxena64d0b8e2016-04-15 00:23:31 -07003479 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
adam radford9c915a82010-12-21 13:34:31 -08003480 retval = 1;
3481 goto out;
3482 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303483
3484 if (reason == MFI_IO_TIMEOUT_OCR) {
3485 dev_info(&instance->pdev->dev,
3486 "MFI IO is timed out, initiating OCR\n");
Sumit Saxena64d0b8e2016-04-15 00:23:31 -07003487 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303488 retval = 1;
3489 goto out;
3490 }
3491
adam radford229fe472014-03-10 02:51:56 -07003492 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303493 if (instance->requestorId && !reason) {
adam radford229fe472014-03-10 02:51:56 -07003494 retval = 1;
3495 goto out;
3496 }
3497
3498 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303499 if (instance->requestorId && reason) {
adam radford229fe472014-03-10 02:51:56 -07003500 if (instance->hb_host_mem->HB.fwCounter !=
3501 instance->hb_host_mem->HB.driverCounter) {
3502 instance->hb_host_mem->HB.driverCounter =
3503 instance->hb_host_mem->HB.fwCounter;
3504 hb_seconds_missed = 0;
3505 } else {
3506 hb_seconds_missed++;
3507 if (hb_seconds_missed ==
3508 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003509 dev_warn(&instance->pdev->dev, "SR-IOV:"
adam radford229fe472014-03-10 02:51:56 -07003510 " Heartbeat never completed "
3511 " while polling during I/O "
3512 " timeout handling for "
3513 "scsi%d.\n",
3514 instance->host->host_no);
3515 *convert = 1;
3516 retval = 1;
3517 goto out;
3518 }
3519 }
3520 }
adam radford9c915a82010-12-21 13:34:31 -08003521
3522 outstanding = atomic_read(&instance->fw_outstanding);
3523 if (!outstanding)
3524 goto out;
3525
3526 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003527 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
adam radford229fe472014-03-10 02:51:56 -07003528 "commands to complete for scsi%d\n", i,
3529 outstanding, instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08003530 megasas_complete_cmd_dpc_fusion(
3531 (unsigned long)instance);
3532 }
3533 msleep(1000);
3534 }
3535
3536 if (atomic_read(&instance->fw_outstanding)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003537 dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
adam radford229fe472014-03-10 02:51:56 -07003538 "will reset adapter scsi%d.\n",
3539 instance->host->host_no);
Kashyap Desai18e1c7f2016-10-21 06:33:29 -07003540 *convert = 1;
adam radford9c915a82010-12-21 13:34:31 -08003541 retval = 1;
3542 }
3543out:
3544 return retval;
3545}
3546
3547void megasas_reset_reply_desc(struct megasas_instance *instance)
3548{
Sumit Saxena179ac142016-01-28 21:04:28 +05303549 int i, j, count;
adam radford9c915a82010-12-21 13:34:31 -08003550 struct fusion_context *fusion;
3551 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
3552
3553 fusion = instance->ctrl_context;
adam radfordc8e858f2011-10-08 18:15:13 -07003554 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
Sumit Saxena179ac142016-01-28 21:04:28 +05303555 for (i = 0 ; i < count ; i++) {
adam radfordc8e858f2011-10-08 18:15:13 -07003556 fusion->last_reply_idx[i] = 0;
Sumit Saxena179ac142016-01-28 21:04:28 +05303557 reply_desc = fusion->reply_frames_desc[i];
3558 for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++)
3559 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
3560 }
adam radford9c915a82010-12-21 13:34:31 -08003561}
3562
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05303563/*
3564 * megasas_refire_mgmt_cmd : Re-fire management commands
3565 * @instance: Controller's soft instance
3566*/
3567void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
3568{
3569 int j;
3570 struct megasas_cmd_fusion *cmd_fusion;
3571 struct fusion_context *fusion;
3572 struct megasas_cmd *cmd_mfi;
3573 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3574 u16 smid;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303575 bool refire_cmd = 0;
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05303576
3577 fusion = instance->ctrl_context;
3578
3579 /* Re-fire management commands.
3580 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
3581 */
3582 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
3583 cmd_fusion = fusion->cmd_list[j];
3584 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
3585 smid = le16_to_cpu(cmd_mfi->context.smid);
3586
3587 if (!smid)
3588 continue;
3589 req_desc = megasas_get_request_descriptor
3590 (instance, smid - 1);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303591 refire_cmd = req_desc && ((cmd_mfi->frame->dcmd.opcode !=
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303592 cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO)) &&
3593 (cmd_mfi->frame->dcmd.opcode !=
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303594 cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO)))
3595 && !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE);
3596 if (refire_cmd)
Shivasharan S45b8a352017-02-10 00:59:04 -08003597 megasas_fire_cmd_fusion(instance, req_desc);
Christoph Hellwig01d7f032015-04-23 16:34:09 +05303598 else
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05303599 megasas_return_cmd(instance, cmd_mfi);
3600 }
adam radford9c915a82010-12-21 13:34:31 -08003601}
3602
Sumit Saxena18365b12016-01-28 21:04:25 +05303603/*
3604 * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
3605 * @instance: per adapter struct
3606 * @channel: the channel assigned by the OS
3607 * @id: the id assigned by the OS
3608 *
3609 * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
3610 */
3611
3612static int megasas_track_scsiio(struct megasas_instance *instance,
3613 int id, int channel)
3614{
3615 int i, found = 0;
3616 struct megasas_cmd_fusion *cmd_fusion;
3617 struct fusion_context *fusion;
3618 fusion = instance->ctrl_context;
3619
3620 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
3621 cmd_fusion = fusion->cmd_list[i];
3622 if (cmd_fusion->scmd &&
3623 (cmd_fusion->scmd->device->id == id &&
3624 cmd_fusion->scmd->device->channel == channel)) {
3625 dev_info(&instance->pdev->dev,
3626 "SCSI commands pending to target"
3627 "channel %d id %d \tSMID: 0x%x\n",
3628 channel, id, cmd_fusion->index);
3629 scsi_print_command(cmd_fusion->scmd);
3630 found = 1;
3631 break;
3632 }
3633 }
3634
3635 return found ? FAILED : SUCCESS;
3636}
3637
3638/**
3639 * megasas_tm_response_code - translation of device response code
3640 * @ioc: per adapter object
3641 * @mpi_reply: MPI reply returned by firmware
3642 *
3643 * Return nothing.
3644 */
3645static void
3646megasas_tm_response_code(struct megasas_instance *instance,
3647 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply)
3648{
3649 char *desc;
3650
3651 switch (mpi_reply->ResponseCode) {
3652 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
3653 desc = "task management request completed";
3654 break;
3655 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
3656 desc = "invalid frame";
3657 break;
3658 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
3659 desc = "task management request not supported";
3660 break;
3661 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
3662 desc = "task management request failed";
3663 break;
3664 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
3665 desc = "task management request succeeded";
3666 break;
3667 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
3668 desc = "invalid lun";
3669 break;
3670 case 0xA:
3671 desc = "overlapped tag attempted";
3672 break;
3673 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
3674 desc = "task queued, however not sent to target";
3675 break;
3676 default:
3677 desc = "unknown";
3678 break;
3679 }
3680 dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n",
3681 mpi_reply->ResponseCode, desc);
3682 dev_dbg(&instance->pdev->dev,
3683 "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
3684 " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
3685 mpi_reply->TerminationCount, mpi_reply->DevHandle,
3686 mpi_reply->Function, mpi_reply->TaskType,
3687 mpi_reply->IOCStatus, mpi_reply->IOCLogInfo);
3688}
3689
3690/**
3691 * megasas_issue_tm - main routine for sending tm requests
3692 * @instance: per adapter struct
3693 * @device_handle: device handle
3694 * @channel: the channel assigned by the OS
3695 * @id: the id assigned by the OS
3696 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
3697 * @smid_task: smid assigned to the task
3698 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
3699 * Context: user
3700 *
3701 * MegaRaid use MPT interface for Task Magement request.
3702 * A generic API for sending task management requests to firmware.
3703 *
3704 * Return SUCCESS or FAILED.
3705 */
3706static int
3707megasas_issue_tm(struct megasas_instance *instance, u16 device_handle,
3708 uint channel, uint id, u16 smid_task, u8 type)
3709{
3710 struct MR_TASK_MANAGE_REQUEST *mr_request;
3711 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request;
3712 unsigned long timeleft;
3713 struct megasas_cmd_fusion *cmd_fusion;
3714 struct megasas_cmd *cmd_mfi;
3715 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3716 struct fusion_context *fusion;
3717 struct megasas_cmd_fusion *scsi_lookup;
3718 int rc;
3719 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply;
3720
3721 fusion = instance->ctrl_context;
3722
3723 cmd_mfi = megasas_get_cmd(instance);
3724
3725 if (!cmd_mfi) {
3726 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
3727 __func__, __LINE__);
3728 return -ENOMEM;
3729 }
3730
3731 cmd_fusion = megasas_get_cmd_fusion(instance,
3732 instance->max_scsi_cmds + cmd_mfi->index);
3733
3734 /* Save the smid. To be used for returning the cmd */
3735 cmd_mfi->context.smid = cmd_fusion->index;
3736
3737 req_desc = megasas_get_request_descriptor(instance,
3738 (cmd_fusion->index - 1));
Sumit Saxena18365b12016-01-28 21:04:25 +05303739
3740 cmd_fusion->request_desc = req_desc;
3741 req_desc->Words = 0;
3742
3743 scsi_lookup = fusion->cmd_list[smid_task - 1];
3744
3745 mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request;
3746 memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST));
3747 mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest;
3748 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3749 mpi_request->DevHandle = cpu_to_le16(device_handle);
3750 mpi_request->TaskType = type;
3751 mpi_request->TaskMID = cpu_to_le16(smid_task);
3752 mpi_request->LUN[1] = 0;
3753
3754
3755 req_desc = cmd_fusion->request_desc;
3756 req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index);
3757 req_desc->HighPriority.RequestFlags =
3758 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
3759 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3760 req_desc->HighPriority.MSIxIndex = 0;
3761 req_desc->HighPriority.LMID = 0;
3762 req_desc->HighPriority.Reserved1 = 0;
3763
3764 if (channel < MEGASAS_MAX_PD_CHANNELS)
3765 mr_request->tmReqFlags.isTMForPD = 1;
3766 else
3767 mr_request->tmReqFlags.isTMForLD = 1;
3768
3769 init_completion(&cmd_fusion->done);
Shivasharan S45b8a352017-02-10 00:59:04 -08003770 megasas_fire_cmd_fusion(instance, req_desc);
Sumit Saxena18365b12016-01-28 21:04:25 +05303771
3772 timeleft = wait_for_completion_timeout(&cmd_fusion->done, 50 * HZ);
3773
3774 if (!timeleft) {
3775 dev_err(&instance->pdev->dev,
3776 "task mgmt type 0x%x timed out\n", type);
3777 cmd_mfi->flags |= DRV_DCMD_SKIP_REFIRE;
3778 mutex_unlock(&instance->reset_mutex);
3779 rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR);
3780 mutex_lock(&instance->reset_mutex);
3781 return rc;
3782 }
3783
3784 mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply;
3785 megasas_tm_response_code(instance, mpi_reply);
3786
3787 megasas_return_cmd(instance, cmd_mfi);
3788 rc = SUCCESS;
3789 switch (type) {
3790 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
3791 if (scsi_lookup->scmd == NULL)
3792 break;
3793 else {
3794 instance->instancet->disable_intr(instance);
3795 msleep(1000);
3796 megasas_complete_cmd_dpc_fusion
3797 ((unsigned long)instance);
3798 instance->instancet->enable_intr(instance);
3799 if (scsi_lookup->scmd == NULL)
3800 break;
3801 }
3802 rc = FAILED;
3803 break;
3804
3805 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
3806 if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF))
3807 break;
3808 instance->instancet->disable_intr(instance);
3809 msleep(1000);
3810 megasas_complete_cmd_dpc_fusion
3811 ((unsigned long)instance);
3812 rc = megasas_track_scsiio(instance, id, channel);
3813 instance->instancet->enable_intr(instance);
3814
3815 break;
3816 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
3817 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
3818 break;
3819 default:
3820 rc = FAILED;
3821 break;
3822 }
3823
3824 return rc;
3825
3826}
3827
3828/*
3829 * megasas_fusion_smid_lookup : Look for fusion command correpspodning to SCSI
3830 * @instance: per adapter struct
3831 *
3832 * Return Non Zero index, if SMID found in outstanding commands
3833 */
3834static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd)
3835{
3836 int i, ret = 0;
3837 struct megasas_instance *instance;
3838 struct megasas_cmd_fusion *cmd_fusion;
3839 struct fusion_context *fusion;
3840
3841 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3842
3843 fusion = instance->ctrl_context;
3844
3845 for (i = 0; i < instance->max_scsi_cmds; i++) {
3846 cmd_fusion = fusion->cmd_list[i];
3847 if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) {
3848 scmd_printk(KERN_NOTICE, scmd, "Abort request is for"
3849 " SMID: %d\n", cmd_fusion->index);
3850 ret = cmd_fusion->index;
3851 break;
3852 }
3853 }
3854
3855 return ret;
3856}
3857
3858/*
3859* megasas_get_tm_devhandle - Get devhandle for TM request
3860* @sdev- OS provided scsi device
3861*
3862* Returns- devhandle/targetID of SCSI device
3863*/
3864static u16 megasas_get_tm_devhandle(struct scsi_device *sdev)
3865{
3866 u16 pd_index = 0;
3867 u32 device_id;
3868 struct megasas_instance *instance;
3869 struct fusion_context *fusion;
3870 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
3871 u16 devhandle = (u16)ULONG_MAX;
3872
3873 instance = (struct megasas_instance *)sdev->host->hostdata;
3874 fusion = instance->ctrl_context;
3875
Shivasharan S3cabd162017-02-10 00:59:05 -08003876 if (!MEGASAS_IS_LOGICAL(sdev)) {
Sumit Saxena18365b12016-01-28 21:04:25 +05303877 if (instance->use_seqnum_jbod_fp) {
Shivasharan S3cabd162017-02-10 00:59:05 -08003878 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
3879 + sdev->id;
3880 pd_sync = (void *)fusion->pd_seq_sync
3881 [(instance->pd_seq_map_id - 1) & 1];
3882 devhandle = pd_sync->seq[pd_index].devHandle;
Sumit Saxena18365b12016-01-28 21:04:25 +05303883 } else
3884 sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable"
3885 " without JBOD MAP support from %s %d\n", __func__, __LINE__);
3886 } else {
3887 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
3888 + sdev->id;
3889 devhandle = device_id;
3890 }
3891
3892 return devhandle;
3893}
3894
3895/*
3896 * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
3897 * @scmd : pointer to scsi command object
3898 *
3899 * Return SUCCESS, if command aborted else FAILED
3900 */
3901
3902int megasas_task_abort_fusion(struct scsi_cmnd *scmd)
3903{
3904 struct megasas_instance *instance;
3905 u16 smid, devhandle;
3906 struct fusion_context *fusion;
3907 int ret;
3908 struct MR_PRIV_DEVICE *mr_device_priv_data;
3909 mr_device_priv_data = scmd->device->hostdata;
3910
3911
3912 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3913 fusion = instance->ctrl_context;
3914
Sumit Saxena8a01a412016-01-28 21:04:32 +05303915 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Sumit Saxena18365b12016-01-28 21:04:25 +05303916 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
3917 "SCSI host:%d\n", instance->host->host_no);
3918 ret = FAILED;
3919 return ret;
3920 }
3921
3922 if (!mr_device_priv_data) {
3923 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
3924 "scmd(%p)\n", scmd);
3925 scmd->result = DID_NO_CONNECT << 16;
3926 ret = SUCCESS;
3927 goto out;
3928 }
3929
3930
3931 if (!mr_device_priv_data->is_tm_capable) {
3932 ret = FAILED;
3933 goto out;
3934 }
3935
3936 mutex_lock(&instance->reset_mutex);
3937
3938 smid = megasas_fusion_smid_lookup(scmd);
3939
3940 if (!smid) {
3941 ret = SUCCESS;
3942 scmd_printk(KERN_NOTICE, scmd, "Command for which abort is"
3943 " issued is not found in oustanding commands\n");
3944 mutex_unlock(&instance->reset_mutex);
3945 goto out;
3946 }
3947
3948 devhandle = megasas_get_tm_devhandle(scmd->device);
3949
3950 if (devhandle == (u16)ULONG_MAX) {
3951 ret = SUCCESS;
3952 sdev_printk(KERN_INFO, scmd->device,
3953 "task abort issued for invalid devhandle\n");
3954 mutex_unlock(&instance->reset_mutex);
3955 goto out;
3956 }
3957 sdev_printk(KERN_INFO, scmd->device,
3958 "attempting task abort! scmd(%p) tm_dev_handle 0x%x\n",
3959 scmd, devhandle);
3960
3961 mr_device_priv_data->tm_busy = 1;
3962 ret = megasas_issue_tm(instance, devhandle,
3963 scmd->device->channel, scmd->device->id, smid,
3964 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK);
3965 mr_device_priv_data->tm_busy = 0;
3966
3967 mutex_unlock(&instance->reset_mutex);
3968out:
3969 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
3970 ((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
3971
3972 return ret;
3973}
3974
3975/*
3976 * megasas_reset_target_fusion : target reset function for fusion adapters
3977 * scmd: SCSI command pointer
3978 *
3979 * Returns SUCCESS if all commands associated with target aborted else FAILED
3980 */
3981
3982int megasas_reset_target_fusion(struct scsi_cmnd *scmd)
3983{
3984
3985 struct megasas_instance *instance;
3986 int ret = FAILED;
3987 u16 devhandle;
3988 struct fusion_context *fusion;
3989 struct MR_PRIV_DEVICE *mr_device_priv_data;
3990 mr_device_priv_data = scmd->device->hostdata;
3991
3992 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3993 fusion = instance->ctrl_context;
3994
Sumit Saxena8a01a412016-01-28 21:04:32 +05303995 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Sumit Saxena18365b12016-01-28 21:04:25 +05303996 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
3997 "SCSI host:%d\n", instance->host->host_no);
3998 ret = FAILED;
3999 return ret;
4000 }
4001
4002 if (!mr_device_priv_data) {
4003 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
4004 "scmd(%p)\n", scmd);
4005 scmd->result = DID_NO_CONNECT << 16;
4006 ret = SUCCESS;
4007 goto out;
4008 }
4009
4010
4011 if (!mr_device_priv_data->is_tm_capable) {
4012 ret = FAILED;
4013 goto out;
4014 }
4015
4016 mutex_lock(&instance->reset_mutex);
4017 devhandle = megasas_get_tm_devhandle(scmd->device);
4018
4019 if (devhandle == (u16)ULONG_MAX) {
4020 ret = SUCCESS;
4021 sdev_printk(KERN_INFO, scmd->device,
4022 "target reset issued for invalid devhandle\n");
4023 mutex_unlock(&instance->reset_mutex);
4024 goto out;
4025 }
4026
4027 sdev_printk(KERN_INFO, scmd->device,
4028 "attempting target reset! scmd(%p) tm_dev_handle 0x%x\n",
4029 scmd, devhandle);
4030 mr_device_priv_data->tm_busy = 1;
4031 ret = megasas_issue_tm(instance, devhandle,
4032 scmd->device->channel, scmd->device->id, 0,
4033 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
4034 mr_device_priv_data->tm_busy = 0;
4035 mutex_unlock(&instance->reset_mutex);
4036out:
4037 scmd_printk(KERN_NOTICE, scmd, "megasas: target reset %s!!\n",
4038 (ret == SUCCESS) ? "SUCCESS" : "FAILED");
4039
4040 return ret;
4041}
4042
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05304043/*SRIOV get other instance in cluster if any*/
4044struct megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance)
4045{
4046 int i;
4047
4048 for (i = 0; i < MAX_MGMT_ADAPTERS; i++) {
4049 if (megasas_mgmt_info.instance[i] &&
4050 (megasas_mgmt_info.instance[i] != instance) &&
4051 megasas_mgmt_info.instance[i]->requestorId &&
4052 megasas_mgmt_info.instance[i]->peerIsPresent &&
4053 (memcmp((megasas_mgmt_info.instance[i]->clusterId),
4054 instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0))
4055 return megasas_mgmt_info.instance[i];
4056 }
4057 return NULL;
4058}
4059
adam radford229fe472014-03-10 02:51:56 -07004060/* Check for a second path that is currently UP */
4061int megasas_check_mpio_paths(struct megasas_instance *instance,
4062 struct scsi_cmnd *scmd)
adam radford9c915a82010-12-21 13:34:31 -08004063{
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05304064 struct megasas_instance *peer_instance = NULL;
Shivasharan Sf55cf472017-02-10 00:59:07 -08004065 int retval = (DID_REQUEUE << 16);
adam radford229fe472014-03-10 02:51:56 -07004066
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05304067 if (instance->peerIsPresent) {
4068 peer_instance = megasas_get_peer_instance(instance);
4069 if ((peer_instance) &&
4070 (atomic_read(&peer_instance->adprecovery) ==
4071 MEGASAS_HBA_OPERATIONAL))
4072 retval = (DID_NO_CONNECT << 16);
adam radford229fe472014-03-10 02:51:56 -07004073 }
adam radford229fe472014-03-10 02:51:56 -07004074 return retval;
4075}
4076
4077/* Core fusion reset function */
Sumit Saxena18365b12016-01-28 21:04:25 +05304078int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
adam radford229fe472014-03-10 02:51:56 -07004079{
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05004080 int retval = SUCCESS, i, j, convert = 0;
adam radford9c915a82010-12-21 13:34:31 -08004081 struct megasas_instance *instance;
Shivasharan S8bf7c652017-02-10 00:59:03 -08004082 struct megasas_cmd_fusion *cmd_fusion, *r1_cmd;
adam radford9c915a82010-12-21 13:34:31 -08004083 struct fusion_context *fusion;
sumit.saxena@avagotech.com79b82c22015-08-31 17:23:41 +05304084 u32 abs_state, status_reg, reset_adapter;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304085 u32 io_timeout_in_crash_mode = 0;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304086 struct scsi_cmnd *scmd_local = NULL;
Sumit Saxena18365b12016-01-28 21:04:25 +05304087 struct scsi_device *sdev;
adam radford9c915a82010-12-21 13:34:31 -08004088
4089 instance = (struct megasas_instance *)shost->hostdata;
4090 fusion = instance->ctrl_context;
4091
adam radford229fe472014-03-10 02:51:56 -07004092 mutex_lock(&instance->reset_mutex);
4093
Sumit Saxena8a01a412016-01-28 21:04:32 +05304094 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004095 dev_warn(&instance->pdev->dev, "Hardware critical error, "
adam radford229fe472014-03-10 02:51:56 -07004096 "returning FAILED for scsi%d.\n",
4097 instance->host->host_no);
Adam Radforda2fbcbc2014-07-09 15:17:54 -07004098 mutex_unlock(&instance->reset_mutex);
adam radfordd4a759a2011-10-08 18:14:39 -07004099 return FAILED;
adam radford9c915a82010-12-21 13:34:31 -08004100 }
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304101 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
4102 abs_state = status_reg & MFI_STATE_MASK;
4103
4104 /* IO timeout detected, forcibly put FW in FAULT state */
4105 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
Sumit Saxena18365b12016-01-28 21:04:25 +05304106 instance->crash_dump_app_support && reason) {
4107 dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, "
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304108 "forcibly FAULT Firmware\n");
Sumit Saxena8a01a412016-01-28 21:04:32 +05304109 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304110 status_reg = readl(&instance->reg_set->doorbell);
4111 writel(status_reg | MFI_STATE_FORCE_OCR,
4112 &instance->reg_set->doorbell);
4113 readl(&instance->reg_set->doorbell);
4114 mutex_unlock(&instance->reset_mutex);
4115 do {
4116 ssleep(3);
4117 io_timeout_in_crash_mode++;
4118 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
4119 "seconds for crash dump collection and OCR "
4120 "to be done\n", (io_timeout_in_crash_mode * 3));
Sumit Saxena8a01a412016-01-28 21:04:32 +05304121 } while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304122 (io_timeout_in_crash_mode < 80));
4123
Sumit Saxena8a01a412016-01-28 21:04:32 +05304124 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304125 dev_info(&instance->pdev->dev, "OCR done for IO "
4126 "timeout case\n");
4127 retval = SUCCESS;
4128 } else {
4129 dev_info(&instance->pdev->dev, "Controller is not "
4130 "operational after 240 seconds wait for IO "
4131 "timeout case in FW crash dump mode\n do "
4132 "OCR/kill adapter\n");
4133 retval = megasas_reset_fusion(shost, 0);
4134 }
4135 return retval;
4136 }
adam radford9c915a82010-12-21 13:34:31 -08004137
adam radford229fe472014-03-10 02:51:56 -07004138 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
4139 del_timer_sync(&instance->sriov_heartbeat_timer);
adam radford7e70e732011-05-11 18:34:08 -07004140 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05304141 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304142 instance->instancet->disable_intr(instance);
adam radford7e70e732011-05-11 18:34:08 -07004143 msleep(1000);
4144
adam radford9c915a82010-12-21 13:34:31 -08004145 /* First try waiting for commands to complete */
Sumit Saxena18365b12016-01-28 21:04:25 +05304146 if (megasas_wait_for_outstanding_fusion(instance, reason,
adam radford229fe472014-03-10 02:51:56 -07004147 &convert)) {
Sumit Saxena8a01a412016-01-28 21:04:32 +05304148 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004149 dev_warn(&instance->pdev->dev, "resetting fusion "
adam radford229fe472014-03-10 02:51:56 -07004150 "adapter scsi%d.\n", instance->host->host_no);
4151 if (convert)
Sumit Saxena18365b12016-01-28 21:04:25 +05304152 reason = 0;
adam radford229fe472014-03-10 02:51:56 -07004153
adam radford9c915a82010-12-21 13:34:31 -08004154 /* Now return commands back to the OS */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304155 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
adam radford9c915a82010-12-21 13:34:31 -08004156 cmd_fusion = fusion->cmd_list[i];
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05004157 /*check for extra commands issued by driver*/
4158 if (instance->is_ventura) {
Shivasharan S8bf7c652017-02-10 00:59:03 -08004159 r1_cmd = fusion->cmd_list[i + instance->max_fw_cmds];
4160 megasas_return_cmd_fusion(instance, r1_cmd);
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05004161 }
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304162 scmd_local = cmd_fusion->scmd;
adam radford9c915a82010-12-21 13:34:31 -08004163 if (cmd_fusion->scmd) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304164 scmd_local->result =
adam radford229fe472014-03-10 02:51:56 -07004165 megasas_check_mpio_paths(instance,
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304166 scmd_local);
Sasikumar Chandrasekaranb71b49c2017-01-10 18:20:51 -05004167 if (instance->ldio_threshold &&
4168 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
Sumit Saxena308ec452016-01-28 21:04:30 +05304169 atomic_dec(&instance->ldio_outstanding);
adam radford9c915a82010-12-21 13:34:31 -08004170 megasas_return_cmd_fusion(instance, cmd_fusion);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304171 scsi_dma_unmap(scmd_local);
4172 scmd_local->scsi_done(scmd_local);
adam radford9c915a82010-12-21 13:34:31 -08004173 }
4174 }
4175
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05004176 atomic_set(&instance->fw_outstanding, 0);
4177
adam radford7e70e732011-05-11 18:34:08 -07004178 status_reg = instance->instancet->read_fw_status_reg(
4179 instance->reg_set);
4180 abs_state = status_reg & MFI_STATE_MASK;
4181 reset_adapter = status_reg & MFI_RESET_ADAPTER;
4182 if (instance->disableOnlineCtrlReset ||
4183 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
adam radford9c915a82010-12-21 13:34:31 -08004184 /* Reset not supported, kill adapter */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004185 dev_warn(&instance->pdev->dev, "Reset not supported"
adam radford229fe472014-03-10 02:51:56 -07004186 ", killing adapter scsi%d.\n",
4187 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08004188 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07004189 instance->skip_heartbeat_timer_del = 1;
adam radford9c915a82010-12-21 13:34:31 -08004190 retval = FAILED;
4191 goto out;
4192 }
4193
adam radford229fe472014-03-10 02:51:56 -07004194 /* Let SR-IOV VF & PF sync up if there was a HB failure */
Sumit Saxena18365b12016-01-28 21:04:25 +05304195 if (instance->requestorId && !reason) {
adam radford229fe472014-03-10 02:51:56 -07004196 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
Sumit Saxena3885c262016-01-28 21:04:35 +05304197 goto transition_to_ready;
adam radford229fe472014-03-10 02:51:56 -07004198 }
4199
adam radford9c915a82010-12-21 13:34:31 -08004200 /* Now try to reset the chip */
4201 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
adam radford9c915a82010-12-21 13:34:31 -08004202
sumit.saxena@avagotech.com79b82c22015-08-31 17:23:41 +05304203 if (instance->instancet->adp_reset
4204 (instance, instance->reg_set))
adam radford9c915a82010-12-21 13:34:31 -08004205 continue;
Sumit Saxena3885c262016-01-28 21:04:35 +05304206transition_to_ready:
adam radford9c915a82010-12-21 13:34:31 -08004207 /* Wait for FW to become ready */
adam radford058a8fa2011-10-08 18:14:27 -07004208 if (megasas_transition_to_ready(instance, 1)) {
Sumit Saxena3885c262016-01-28 21:04:35 +05304209 dev_warn(&instance->pdev->dev,
4210 "Failed to transition controller to ready for "
4211 "scsi%d.\n", instance->host->host_no);
4212 if (instance->requestorId && !reason)
4213 goto fail_kill_adapter;
4214 else
4215 continue;
adam radford9c915a82010-12-21 13:34:31 -08004216 }
adam radford9c915a82010-12-21 13:34:31 -08004217 megasas_reset_reply_desc(instance);
Sumit Saxena308ec452016-01-28 21:04:30 +05304218 megasas_fusion_update_can_queue(instance, OCR_CONTEXT);
4219
adam radford9c915a82010-12-21 13:34:31 -08004220 if (megasas_ioc_init_fusion(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004221 dev_warn(&instance->pdev->dev,
Sumit Saxena3885c262016-01-28 21:04:35 +05304222 "megasas_ioc_init_fusion() failed! for "
4223 "scsi%d\n", instance->host->host_no);
4224 if (instance->requestorId && !reason)
4225 goto fail_kill_adapter;
4226 else
4227 continue;
adam radford9c915a82010-12-21 13:34:31 -08004228 }
4229
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05304230 megasas_refire_mgmt_cmd(instance);
adam radford9c915a82010-12-21 13:34:31 -08004231
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304232 if (megasas_get_ctrl_info(instance)) {
4233 dev_info(&instance->pdev->dev,
4234 "Failed from %s %d\n",
4235 __func__, __LINE__);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304236 megaraid_sas_kill_hba(instance);
4237 retval = FAILED;
4238 }
adam radford9c915a82010-12-21 13:34:31 -08004239 /* Reset load balance info */
4240 memset(fusion->load_balance_info, 0,
4241 sizeof(struct LD_LOAD_BALANCE_INFO)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05304242 *MAX_LOGICAL_DRIVES_EXT);
adam radford9c915a82010-12-21 13:34:31 -08004243
4244 if (!megasas_get_map_info(instance))
4245 megasas_sync_map_info(instance);
4246
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304247 megasas_setup_jbod_map(instance);
4248
Sumit Saxena18365b12016-01-28 21:04:25 +05304249 shost_for_each_device(sdev, shost)
Shivasharan S15dd0382017-02-10 00:59:10 -08004250 megasas_set_dynamic_target_properties(sdev);
Sumit Saxena18365b12016-01-28 21:04:25 +05304251
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05004252 /* reset stream detection array */
4253 if (instance->is_ventura) {
4254 for (j = 0; j < MAX_LOGICAL_DRIVES_EXT; ++j) {
4255 memset(fusion->stream_detect_by_ld[j],
4256 0, sizeof(struct LD_STREAM_DETECT));
4257 fusion->stream_detect_by_ld[j]->mru_bit_map
4258 = MR_STREAM_BITMAP;
4259 }
4260 }
4261
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05304262 clear_bit(MEGASAS_FUSION_IN_RESET,
4263 &instance->reset_flags);
4264 instance->instancet->enable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05304265 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05304266
adam radford229fe472014-03-10 02:51:56 -07004267 /* Restart SR-IOV heartbeat */
4268 if (instance->requestorId) {
4269 if (!megasas_sriov_start_heartbeat(instance, 0))
4270 megasas_start_timer(instance,
4271 &instance->sriov_heartbeat_timer,
4272 megasas_sriov_heartbeat_handler,
4273 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4274 else
4275 instance->skip_heartbeat_timer_del = 1;
4276 }
4277
adam radford9c915a82010-12-21 13:34:31 -08004278 /* Adapter reset completed successfully */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004279 dev_warn(&instance->pdev->dev, "Reset "
adam radford229fe472014-03-10 02:51:56 -07004280 "successful for scsi%d.\n",
4281 instance->host->host_no);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304282
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05304283 if (instance->crash_dump_drv_support &&
4284 instance->crash_dump_app_support)
4285 megasas_set_crash_dump_params(instance,
4286 MR_CRASH_BUF_TURN_ON);
4287 else
4288 megasas_set_crash_dump_params(instance,
4289 MR_CRASH_BUF_TURN_OFF);
4290
adam radford9c915a82010-12-21 13:34:31 -08004291 retval = SUCCESS;
4292 goto out;
4293 }
Sumit Saxena3885c262016-01-28 21:04:35 +05304294fail_kill_adapter:
adam radford9c915a82010-12-21 13:34:31 -08004295 /* Reset failed, kill the adapter */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004296 dev_warn(&instance->pdev->dev, "Reset failed, killing "
adam radford229fe472014-03-10 02:51:56 -07004297 "adapter scsi%d.\n", instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08004298 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07004299 instance->skip_heartbeat_timer_del = 1;
adam radford9c915a82010-12-21 13:34:31 -08004300 retval = FAILED;
4301 } else {
adam radford229fe472014-03-10 02:51:56 -07004302 /* For VF: Restart HB timer if we didn't OCR */
4303 if (instance->requestorId) {
4304 megasas_start_timer(instance,
4305 &instance->sriov_heartbeat_timer,
4306 megasas_sriov_heartbeat_handler,
4307 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4308 }
adam radford3f0e58bc2011-10-08 18:14:59 -07004309 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304310 instance->instancet->enable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05304311 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
adam radford9c915a82010-12-21 13:34:31 -08004312 }
4313out:
4314 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4315 mutex_unlock(&instance->reset_mutex);
4316 return retval;
4317}
4318
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304319/* Fusion Crash dump collection work queue */
4320void megasas_fusion_crash_dump_wq(struct work_struct *work)
4321{
4322 struct megasas_instance *instance =
4323 container_of(work, struct megasas_instance, crash_init);
4324 u32 status_reg;
4325 u8 partial_copy = 0;
4326
4327
4328 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
4329
4330 /*
4331 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
4332 * to host crash buffers
4333 */
4334 if (instance->drv_buf_index == 0) {
4335 /* Buffer is already allocated for old Crash dump.
4336 * Do OCR and do not wait for crash dump collection
4337 */
4338 if (instance->drv_buf_alloc) {
4339 dev_info(&instance->pdev->dev, "earlier crash dump is "
4340 "not yet copied by application, ignoring this "
4341 "crash dump and initiating OCR\n");
4342 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
4343 writel(status_reg,
4344 &instance->reg_set->outbound_scratch_pad);
4345 readl(&instance->reg_set->outbound_scratch_pad);
4346 return;
4347 }
4348 megasas_alloc_host_crash_buffer(instance);
4349 dev_info(&instance->pdev->dev, "Number of host crash buffers "
4350 "allocated: %d\n", instance->drv_buf_alloc);
4351 }
4352
4353 /*
4354 * Driver has allocated max buffers, which can be allocated
4355 * and FW has more crash dump data, then driver will
4356 * ignore the data.
4357 */
4358 if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
4359 dev_info(&instance->pdev->dev, "Driver is done copying "
4360 "the buffer: %d\n", instance->drv_buf_alloc);
4361 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
4362 partial_copy = 1;
4363 } else {
4364 memcpy(instance->crash_buf[instance->drv_buf_index],
4365 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
4366 instance->drv_buf_index++;
4367 status_reg &= ~MFI_STATE_DMADONE;
4368 }
4369
4370 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
4371 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
4372 "of copied buffers: %d\n", instance->drv_buf_index);
4373 instance->fw_crash_buffer_size = instance->drv_buf_index;
4374 instance->fw_crash_state = AVAILABLE;
4375 instance->drv_buf_index = 0;
4376 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
4377 readl(&instance->reg_set->outbound_scratch_pad);
4378 if (!partial_copy)
4379 megasas_reset_fusion(instance->host, 0);
4380 } else {
4381 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
4382 readl(&instance->reg_set->outbound_scratch_pad);
4383 }
4384}
4385
4386
adam radford9c915a82010-12-21 13:34:31 -08004387/* Fusion OCR work queue */
4388void megasas_fusion_ocr_wq(struct work_struct *work)
4389{
4390 struct megasas_instance *instance =
4391 container_of(work, struct megasas_instance, work_init);
4392
adam radford229fe472014-03-10 02:51:56 -07004393 megasas_reset_fusion(instance->host, 0);
adam radford9c915a82010-12-21 13:34:31 -08004394}
4395
4396struct megasas_instance_template megasas_instance_template_fusion = {
adam radford9c915a82010-12-21 13:34:31 -08004397 .enable_intr = megasas_enable_intr_fusion,
4398 .disable_intr = megasas_disable_intr_fusion,
4399 .clear_intr = megasas_clear_intr_fusion,
4400 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
4401 .adp_reset = megasas_adp_reset_fusion,
4402 .check_reset = megasas_check_reset_fusion,
4403 .service_isr = megasas_isr_fusion,
4404 .tasklet = megasas_complete_cmd_dpc_fusion,
4405 .init_adapter = megasas_init_adapter_fusion,
4406 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
4407 .issue_dcmd = megasas_issue_dcmd_fusion,
4408};