blob: 985510628f564286f5df386085476e9020e2ad47 [file] [log] [blame]
adam radford9c915a82010-12-21 13:34:31 -08001/*
2 * Linux MegaRAID driver for SAS based RAID controllers
3 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +05304 * Copyright (c) 2009-2013 LSI Corporation
5 * Copyright (c) 2013-2014 Avago Technologies
adam radford9c915a82010-12-21 13:34:31 -08006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053018 * along with this program. If not, see <http://www.gnu.org/licenses/>.
adam radford9c915a82010-12-21 13:34:31 -080019 *
20 * FILE: megaraid_sas_fusion.c
21 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053022 * Authors: Avago Technologies
adam radford9c915a82010-12-21 13:34:31 -080023 * Sumant Patro
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053024 * Adam Radford
25 * Kashyap Desai <kashyap.desai@avagotech.com>
26 * Sumit Saxena <sumit.saxena@avagotech.com>
adam radford9c915a82010-12-21 13:34:31 -080027 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053028 * Send feedback to: megaraidlinux.pdl@avagotech.com
adam radford9c915a82010-12-21 13:34:31 -080029 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053030 * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31 * San Jose, California 95131
adam radford9c915a82010-12-21 13:34:31 -080032 */
33
34#include <linux/kernel.h>
35#include <linux/types.h>
36#include <linux/pci.h>
37#include <linux/list.h>
38#include <linux/moduleparam.h>
39#include <linux/module.h>
40#include <linux/spinlock.h>
41#include <linux/interrupt.h>
42#include <linux/delay.h>
adam radford9c915a82010-12-21 13:34:31 -080043#include <linux/uio.h>
44#include <linux/uaccess.h>
45#include <linux/fs.h>
46#include <linux/compat.h>
47#include <linux/blkdev.h>
48#include <linux/mutex.h>
49#include <linux/poll.h>
Shivasharan S5fc499b2017-02-10 00:59:17 -080050#include <linux/vmalloc.h>
adam radford9c915a82010-12-21 13:34:31 -080051
52#include <scsi/scsi.h>
53#include <scsi/scsi_cmnd.h>
54#include <scsi/scsi_device.h>
55#include <scsi/scsi_host.h>
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +053056#include <scsi/scsi_dbg.h>
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +053057#include <linux/dmi.h>
adam radford9c915a82010-12-21 13:34:31 -080058
59#include "megaraid_sas_fusion.h"
60#include "megaraid_sas.h"
61
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +053062
adam radford9c915a82010-12-21 13:34:31 -080063extern void megasas_free_cmds(struct megasas_instance *instance);
64extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
65 *instance);
66extern void
67megasas_complete_cmd(struct megasas_instance *instance,
68 struct megasas_cmd *cmd, u8 alt_status);
adam radford9c915a82010-12-21 13:34:31 -080069int
adam radford229fe472014-03-10 02:51:56 -070070wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
71 int seconds);
adam radford9c915a82010-12-21 13:34:31 -080072
73void
74megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
75int megasas_alloc_cmds(struct megasas_instance *instance);
76int
77megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
78int
79megasas_issue_polled(struct megasas_instance *instance,
80 struct megasas_cmd *cmd);
adam radford53ef2bb2011-02-24 20:56:05 -080081void
82megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
83
adam radford058a8fa2011-10-08 18:14:27 -070084int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
adam radford9c915a82010-12-21 13:34:31 -080085void megaraid_sas_kill_hba(struct megasas_instance *instance);
86
87extern u32 megasas_dbg_lvl;
adam radford229fe472014-03-10 02:51:56 -070088void megasas_sriov_heartbeat_handler(unsigned long instance_addr);
89int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
90 int initial);
91void megasas_start_timer(struct megasas_instance *instance,
92 struct timer_list *timer,
93 void *fn, unsigned long interval);
94extern struct megasas_mgmt_info megasas_mgmt_info;
Sumit Saxenae3d178c2016-01-28 21:04:34 +053095extern unsigned int resetwaittime;
Sumit Saxena308ec452016-01-28 21:04:30 +053096extern unsigned int dual_qdepth_disable;
Sumit Saxena179ac142016-01-28 21:04:28 +053097static void megasas_free_rdpq_fusion(struct megasas_instance *instance);
98static void megasas_free_reply_fusion(struct megasas_instance *instance);
adam radford9c915a82010-12-21 13:34:31 -080099
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +0530100
101
adam radford9c915a82010-12-21 13:34:31 -0800102/**
103 * megasas_enable_intr_fusion - Enables interrupts
104 * @regs: MFI register set
105 */
106void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530107megasas_enable_intr_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800108{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530109 struct megasas_register_set __iomem *regs;
110 regs = instance->reg_set;
Sumit.Saxena@avagotech.comc2ced172015-01-05 20:06:13 +0530111
112 instance->mask_interrupts = 0;
adam radford6497b242011-10-08 18:14:50 -0700113 /* For Thunderbolt/Invader also clear intr on enable */
114 writel(~0, &regs->outbound_intr_status);
115 readl(&regs->outbound_intr_status);
116
adam radford9c915a82010-12-21 13:34:31 -0800117 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
118
119 /* Dummy readl to force pci flush */
120 readl(&regs->outbound_intr_mask);
121}
122
123/**
124 * megasas_disable_intr_fusion - Disables interrupt
125 * @regs: MFI register set
126 */
127void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530128megasas_disable_intr_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800129{
130 u32 mask = 0xFFFFFFFF;
131 u32 status;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530132 struct megasas_register_set __iomem *regs;
133 regs = instance->reg_set;
134 instance->mask_interrupts = 1;
adam radford9c915a82010-12-21 13:34:31 -0800135
136 writel(mask, &regs->outbound_intr_mask);
137 /* Dummy readl to force pci flush */
138 status = readl(&regs->outbound_intr_mask);
139}
140
141int
142megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
143{
144 u32 status;
145 /*
146 * Check if it is our interrupt
147 */
148 status = readl(&regs->outbound_intr_status);
149
150 if (status & 1) {
151 writel(status, &regs->outbound_intr_status);
152 readl(&regs->outbound_intr_status);
153 return 1;
154 }
155 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
156 return 0;
157
adam radford9c915a82010-12-21 13:34:31 -0800158 return 1;
159}
160
161/**
162 * megasas_get_cmd_fusion - Get a command from the free pool
163 * @instance: Adapter soft state
164 *
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530165 * Returns a blk_tag indexed mpt frame
adam radford9c915a82010-12-21 13:34:31 -0800166 */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530167inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
168 *instance, u32 blk_tag)
adam radford9c915a82010-12-21 13:34:31 -0800169{
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530170 struct fusion_context *fusion;
adam radford9c915a82010-12-21 13:34:31 -0800171
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530172 fusion = instance->ctrl_context;
173 return fusion->cmd_list[blk_tag];
adam radford9c915a82010-12-21 13:34:31 -0800174}
175
176/**
177 * megasas_return_cmd_fusion - Return a cmd to free command pool
178 * @instance: Adapter soft state
179 * @cmd: Command packet to be returned to free command pool
180 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530181inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
182 struct megasas_cmd_fusion *cmd)
adam radford9c915a82010-12-21 13:34:31 -0800183{
adam radford9c915a82010-12-21 13:34:31 -0800184 cmd->scmd = NULL;
Shivasharan S8bf7c652017-02-10 00:59:03 -0800185 memset(cmd->io_request, 0, MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
186 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
187 cmd->cmd_completed = false;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530188}
189
190/**
Christoph Hellwig01d7f032015-04-23 16:34:09 +0530191 * megasas_fire_cmd_fusion - Sends command to the FW
Shivasharan S45b8a352017-02-10 00:59:04 -0800192 * @instance: Adapter soft state
193 * @req_desc: 32bit or 64bit Request descriptor
194 *
195 * Perform PCI Write. Ventura supports 32 bit Descriptor.
196 * Prior to Ventura (12G) MR controller supports 64 bit Descriptor.
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530197 */
Shivasharan S45b8a352017-02-10 00:59:04 -0800198
Christoph Hellwig01d7f032015-04-23 16:34:09 +0530199static void
200megasas_fire_cmd_fusion(struct megasas_instance *instance,
Shivasharan S45b8a352017-02-10 00:59:04 -0800201 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530202{
Shivasharan S45b8a352017-02-10 00:59:04 -0800203 if (instance->is_ventura)
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500204 writel(le32_to_cpu(req_desc->u.low),
Shivasharan S45b8a352017-02-10 00:59:04 -0800205 &instance->reg_set->inbound_single_queue_port);
206 else {
207#if defined(writeq) && defined(CONFIG_64BIT)
208 u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
209 le32_to_cpu(req_desc->u.low));
210
211 writeq(req_data, &instance->reg_set->inbound_low_queue_port);
212#else
213 unsigned long flags;
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500214 spin_lock_irqsave(&instance->hba_lock, flags);
215 writel(le32_to_cpu(req_desc->u.low),
Shivasharan S45b8a352017-02-10 00:59:04 -0800216 &instance->reg_set->inbound_low_queue_port);
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500217 writel(le32_to_cpu(req_desc->u.high),
Shivasharan S45b8a352017-02-10 00:59:04 -0800218 &instance->reg_set->inbound_high_queue_port);
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500219 mmiowb();
220 spin_unlock_irqrestore(&instance->hba_lock, flags);
Christoph Hellwig01d7f032015-04-23 16:34:09 +0530221#endif
Sasikumar Chandrasekaran45f4f2e2017-01-10 18:20:43 -0500222 }
adam radford9c915a82010-12-21 13:34:31 -0800223}
224
adam radford9c915a82010-12-21 13:34:31 -0800225/**
Sumit Saxena308ec452016-01-28 21:04:30 +0530226 * megasas_fusion_update_can_queue - Do all Adapter Queue depth related calculations here
227 * @instance: Adapter soft state
228 * fw_boot_context: Whether this function called during probe or after OCR
229 *
230 * This function is only for fusion controllers.
231 * Update host can queue, if firmware downgrade max supported firmware commands.
232 * Firmware upgrade case will be skiped because underlying firmware has
233 * more resource than exposed to the OS.
234 *
235 */
236static void
237megasas_fusion_update_can_queue(struct megasas_instance *instance, int fw_boot_context)
238{
239 u16 cur_max_fw_cmds = 0;
240 u16 ldio_threshold = 0;
241 struct megasas_register_set __iomem *reg_set;
242
243 reg_set = instance->reg_set;
244
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -0500245 /* ventura FW does not fill outbound_scratch_pad_3 with queue depth */
246 if (!instance->is_ventura)
247 cur_max_fw_cmds =
248 readl(&instance->reg_set->outbound_scratch_pad_3) & 0x00FFFF;
Sumit Saxena308ec452016-01-28 21:04:30 +0530249
250 if (dual_qdepth_disable || !cur_max_fw_cmds)
251 cur_max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
252 else
253 ldio_threshold =
254 (instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF) - MEGASAS_FUSION_IOCTL_CMDS;
255
256 dev_info(&instance->pdev->dev,
257 "Current firmware maximum commands: %d\t LDIO threshold: %d\n",
258 cur_max_fw_cmds, ldio_threshold);
259
260 if (fw_boot_context == OCR_CONTEXT) {
261 cur_max_fw_cmds = cur_max_fw_cmds - 1;
Shivasharan S013aec62017-02-10 00:59:26 -0800262 if (cur_max_fw_cmds < instance->max_fw_cmds) {
Sumit Saxena308ec452016-01-28 21:04:30 +0530263 instance->cur_can_queue =
264 cur_max_fw_cmds - (MEGASAS_FUSION_INTERNAL_CMDS +
265 MEGASAS_FUSION_IOCTL_CMDS);
266 instance->host->can_queue = instance->cur_can_queue;
267 instance->ldio_threshold = ldio_threshold;
268 }
269 } else {
270 instance->max_fw_cmds = cur_max_fw_cmds;
271 instance->ldio_threshold = ldio_threshold;
272
273 if (!instance->is_rdpq)
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500274 instance->max_fw_cmds =
275 min_t(u16, instance->max_fw_cmds, 1024);
Sumit Saxena308ec452016-01-28 21:04:30 +0530276
Sumit Saxenac3e385a2016-04-15 00:23:30 -0700277 if (reset_devices)
278 instance->max_fw_cmds = min(instance->max_fw_cmds,
279 (u16)MEGASAS_KDUMP_QUEUE_DEPTH);
Sumit Saxena308ec452016-01-28 21:04:30 +0530280 /*
281 * Reduce the max supported cmds by 1. This is to ensure that the
282 * reply_q_sz (1 more than the max cmd that driver may send)
283 * does not exceed max cmds that the FW can support
284 */
285 instance->max_fw_cmds = instance->max_fw_cmds-1;
286
287 instance->max_scsi_cmds = instance->max_fw_cmds -
288 (MEGASAS_FUSION_INTERNAL_CMDS +
289 MEGASAS_FUSION_IOCTL_CMDS);
290 instance->cur_can_queue = instance->max_scsi_cmds;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500291 instance->host->can_queue = instance->cur_can_queue;
Sumit Saxena308ec452016-01-28 21:04:30 +0530292 }
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500293
294 if (instance->is_ventura)
295 instance->max_mpt_cmds =
Shivasharan Sa73b0a42017-02-10 00:59:38 -0800296 instance->max_fw_cmds * RAID_1_PEER_CMDS;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500297 else
298 instance->max_mpt_cmds = instance->max_fw_cmds;
Sumit Saxena308ec452016-01-28 21:04:30 +0530299}
300/**
adam radford9c915a82010-12-21 13:34:31 -0800301 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
302 * @instance: Adapter soft state
303 */
304void
305megasas_free_cmds_fusion(struct megasas_instance *instance)
306{
307 int i;
308 struct fusion_context *fusion = instance->ctrl_context;
Sumit Saxena179ac142016-01-28 21:04:28 +0530309 struct megasas_cmd_fusion *cmd;
adam radford9c915a82010-12-21 13:34:31 -0800310
Sumit Saxena179ac142016-01-28 21:04:28 +0530311 /* SG, Sense */
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500312 for (i = 0; i < instance->max_mpt_cmds; i++) {
Sumit Saxena179ac142016-01-28 21:04:28 +0530313 cmd = fusion->cmd_list[i];
314 if (cmd) {
315 if (cmd->sg_frame)
316 pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
317 cmd->sg_frame_phys_addr);
318 if (cmd->sense)
319 pci_pool_free(fusion->sense_dma_pool, cmd->sense,
320 cmd->sense_phys_addr);
321 }
322 }
323
324 if (fusion->sg_dma_pool) {
325 pci_pool_destroy(fusion->sg_dma_pool);
326 fusion->sg_dma_pool = NULL;
327 }
328 if (fusion->sense_dma_pool) {
329 pci_pool_destroy(fusion->sense_dma_pool);
330 fusion->sense_dma_pool = NULL;
331 }
adam radford9c915a82010-12-21 13:34:31 -0800332
333
Sumit Saxena179ac142016-01-28 21:04:28 +0530334 /* Reply Frame, Desc*/
335 if (instance->is_rdpq)
336 megasas_free_rdpq_fusion(instance);
337 else
338 megasas_free_reply_fusion(instance);
adam radford9c915a82010-12-21 13:34:31 -0800339
Sumit Saxena179ac142016-01-28 21:04:28 +0530340 /* Request Frame, Desc*/
adam radford9c915a82010-12-21 13:34:31 -0800341 if (fusion->req_frames_desc)
Sumit Saxena179ac142016-01-28 21:04:28 +0530342 dma_free_coherent(&instance->pdev->dev,
343 fusion->request_alloc_sz, fusion->req_frames_desc,
344 fusion->req_frames_desc_phys);
345 if (fusion->io_request_frames)
adam radford9c915a82010-12-21 13:34:31 -0800346 pci_pool_free(fusion->io_request_frames_pool,
Sumit Saxena179ac142016-01-28 21:04:28 +0530347 fusion->io_request_frames,
348 fusion->io_request_frames_phys);
349 if (fusion->io_request_frames_pool) {
adam radford9c915a82010-12-21 13:34:31 -0800350 pci_pool_destroy(fusion->io_request_frames_pool);
Sumit Saxena179ac142016-01-28 21:04:28 +0530351 fusion->io_request_frames_pool = NULL;
adam radford9c915a82010-12-21 13:34:31 -0800352 }
353
adam radford9c915a82010-12-21 13:34:31 -0800354
Sumit Saxena179ac142016-01-28 21:04:28 +0530355 /* cmd_list */
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500356 for (i = 0; i < instance->max_mpt_cmds; i++)
adam radford9c915a82010-12-21 13:34:31 -0800357 kfree(fusion->cmd_list[i]);
358
adam radford9c915a82010-12-21 13:34:31 -0800359 kfree(fusion->cmd_list);
adam radford9c915a82010-12-21 13:34:31 -0800360}
361
362/**
Sumit Saxena179ac142016-01-28 21:04:28 +0530363 * megasas_create_sg_sense_fusion - Creates DMA pool for cmd frames
adam radford9c915a82010-12-21 13:34:31 -0800364 * @instance: Adapter soft state
365 *
366 */
Sumit Saxena179ac142016-01-28 21:04:28 +0530367static int megasas_create_sg_sense_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800368{
369 int i;
Shivasharan S50b7f5a2017-02-10 00:59:25 -0800370 u16 max_cmd;
adam radford9c915a82010-12-21 13:34:31 -0800371 struct fusion_context *fusion;
372 struct megasas_cmd_fusion *cmd;
adam radford9c915a82010-12-21 13:34:31 -0800373
374 fusion = instance->ctrl_context;
375 max_cmd = instance->max_fw_cmds;
376
adam radford9c915a82010-12-21 13:34:31 -0800377
Sumit Saxena179ac142016-01-28 21:04:28 +0530378 fusion->sg_dma_pool =
379 pci_pool_create("mr_sg", instance->pdev,
Shivasharan S15dd0382017-02-10 00:59:10 -0800380 instance->max_chain_frame_sz,
381 MR_DEFAULT_NVME_PAGE_SIZE, 0);
Sumit Saxena179ac142016-01-28 21:04:28 +0530382 /* SCSI_SENSE_BUFFERSIZE = 96 bytes */
383 fusion->sense_dma_pool =
384 pci_pool_create("mr_sense", instance->pdev,
385 SCSI_SENSE_BUFFERSIZE, 64, 0);
adam radford9c915a82010-12-21 13:34:31 -0800386
Sumit Saxena179ac142016-01-28 21:04:28 +0530387 if (!fusion->sense_dma_pool || !fusion->sg_dma_pool) {
388 dev_err(&instance->pdev->dev,
389 "Failed from %s %d\n", __func__, __LINE__);
adam radford9c915a82010-12-21 13:34:31 -0800390 return -ENOMEM;
391 }
392
393 /*
394 * Allocate and attach a frame to each of the commands in cmd_list
395 */
396 for (i = 0; i < max_cmd; i++) {
adam radford9c915a82010-12-21 13:34:31 -0800397 cmd = fusion->cmd_list[i];
adam radford9c915a82010-12-21 13:34:31 -0800398 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
Sumit Saxena179ac142016-01-28 21:04:28 +0530399 GFP_KERNEL, &cmd->sg_frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -0800400
401 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
Sumit Saxena179ac142016-01-28 21:04:28 +0530402 GFP_KERNEL, &cmd->sense_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -0800403 if (!cmd->sg_frame || !cmd->sense) {
Sumit Saxena179ac142016-01-28 21:04:28 +0530404 dev_err(&instance->pdev->dev,
405 "Failed from %s %d\n", __func__, __LINE__);
adam radford9c915a82010-12-21 13:34:31 -0800406 return -ENOMEM;
407 }
408 }
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500409
410 /* create sense buffer for the raid 1/10 fp */
411 for (i = max_cmd; i < instance->max_mpt_cmds; i++) {
412 cmd = fusion->cmd_list[i];
413 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
414 GFP_KERNEL, &cmd->sense_phys_addr);
415 if (!cmd->sense) {
416 dev_err(&instance->pdev->dev,
417 "Failed from %s %d\n", __func__, __LINE__);
418 return -ENOMEM;
419 }
420 }
421
adam radford9c915a82010-12-21 13:34:31 -0800422 return 0;
423}
424
Sumit Saxena179ac142016-01-28 21:04:28 +0530425int
426megasas_alloc_cmdlist_fusion(struct megasas_instance *instance)
427{
Shu Wang70c54e22017-07-21 18:54:09 +0800428 u32 max_mpt_cmd, i, j;
Sumit Saxena179ac142016-01-28 21:04:28 +0530429 struct fusion_context *fusion;
430
431 fusion = instance->ctrl_context;
432
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500433 max_mpt_cmd = instance->max_mpt_cmds;
Sumit Saxena179ac142016-01-28 21:04:28 +0530434
435 /*
436 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
437 * Allocate the dynamic array first and then allocate individual
438 * commands.
439 */
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500440 fusion->cmd_list =
441 kzalloc(sizeof(struct megasas_cmd_fusion *) * max_mpt_cmd,
442 GFP_KERNEL);
Sumit Saxena179ac142016-01-28 21:04:28 +0530443 if (!fusion->cmd_list) {
444 dev_err(&instance->pdev->dev,
445 "Failed from %s %d\n", __func__, __LINE__);
446 return -ENOMEM;
447 }
448
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500449 for (i = 0; i < max_mpt_cmd; i++) {
Sumit Saxena179ac142016-01-28 21:04:28 +0530450 fusion->cmd_list[i] = kzalloc(sizeof(struct megasas_cmd_fusion),
451 GFP_KERNEL);
452 if (!fusion->cmd_list[i]) {
Shu Wang70c54e22017-07-21 18:54:09 +0800453 for (j = 0; j < i; j++)
454 kfree(fusion->cmd_list[j]);
455 kfree(fusion->cmd_list);
Sumit Saxena179ac142016-01-28 21:04:28 +0530456 dev_err(&instance->pdev->dev,
457 "Failed from %s %d\n", __func__, __LINE__);
458 return -ENOMEM;
459 }
460 }
Shu Wang70c54e22017-07-21 18:54:09 +0800461
Sumit Saxena179ac142016-01-28 21:04:28 +0530462 return 0;
463}
464int
465megasas_alloc_request_fusion(struct megasas_instance *instance)
466{
467 struct fusion_context *fusion;
468
469 fusion = instance->ctrl_context;
470
471 fusion->req_frames_desc =
472 dma_alloc_coherent(&instance->pdev->dev,
473 fusion->request_alloc_sz,
474 &fusion->req_frames_desc_phys, GFP_KERNEL);
475 if (!fusion->req_frames_desc) {
476 dev_err(&instance->pdev->dev,
477 "Failed from %s %d\n", __func__, __LINE__);
478 return -ENOMEM;
479 }
480
481 fusion->io_request_frames_pool =
482 pci_pool_create("mr_ioreq", instance->pdev,
483 fusion->io_frames_alloc_sz, 16, 0);
484
485 if (!fusion->io_request_frames_pool) {
486 dev_err(&instance->pdev->dev,
487 "Failed from %s %d\n", __func__, __LINE__);
488 return -ENOMEM;
489 }
490
491 fusion->io_request_frames =
492 pci_pool_alloc(fusion->io_request_frames_pool,
493 GFP_KERNEL, &fusion->io_request_frames_phys);
494 if (!fusion->io_request_frames) {
495 dev_err(&instance->pdev->dev,
496 "Failed from %s %d\n", __func__, __LINE__);
497 return -ENOMEM;
498 }
499 return 0;
500}
501
502int
503megasas_alloc_reply_fusion(struct megasas_instance *instance)
504{
505 int i, count;
506 struct fusion_context *fusion;
507 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
508 fusion = instance->ctrl_context;
509
510 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
511 fusion->reply_frames_desc_pool =
512 pci_pool_create("mr_reply", instance->pdev,
513 fusion->reply_alloc_sz * count, 16, 0);
514
515 if (!fusion->reply_frames_desc_pool) {
516 dev_err(&instance->pdev->dev,
517 "Failed from %s %d\n", __func__, __LINE__);
518 return -ENOMEM;
519 }
520
521 fusion->reply_frames_desc[0] =
522 pci_pool_alloc(fusion->reply_frames_desc_pool,
523 GFP_KERNEL, &fusion->reply_frames_desc_phys[0]);
524 if (!fusion->reply_frames_desc[0]) {
525 dev_err(&instance->pdev->dev,
526 "Failed from %s %d\n", __func__, __LINE__);
527 return -ENOMEM;
528 }
529 reply_desc = fusion->reply_frames_desc[0];
530 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
531 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
532
533 /* This is not a rdpq mode, but driver still populate
534 * reply_frame_desc array to use same msix index in ISR path.
535 */
536 for (i = 0; i < (count - 1); i++)
537 fusion->reply_frames_desc[i + 1] =
538 fusion->reply_frames_desc[i] +
539 (fusion->reply_alloc_sz)/sizeof(union MPI2_REPLY_DESCRIPTORS_UNION);
540
541 return 0;
542}
543
544int
545megasas_alloc_rdpq_fusion(struct megasas_instance *instance)
546{
547 int i, j, count;
548 struct fusion_context *fusion;
549 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
550
551 fusion = instance->ctrl_context;
552
553 fusion->rdpq_virt = pci_alloc_consistent(instance->pdev,
554 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
555 &fusion->rdpq_phys);
556 if (!fusion->rdpq_virt) {
557 dev_err(&instance->pdev->dev,
558 "Failed from %s %d\n", __func__, __LINE__);
559 return -ENOMEM;
560 }
561
562 memset(fusion->rdpq_virt, 0,
563 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION);
564 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
565 fusion->reply_frames_desc_pool = pci_pool_create("mr_rdpq",
566 instance->pdev, fusion->reply_alloc_sz, 16, 0);
567
568 if (!fusion->reply_frames_desc_pool) {
569 dev_err(&instance->pdev->dev,
570 "Failed from %s %d\n", __func__, __LINE__);
571 return -ENOMEM;
572 }
573
574 for (i = 0; i < count; i++) {
575 fusion->reply_frames_desc[i] =
576 pci_pool_alloc(fusion->reply_frames_desc_pool,
577 GFP_KERNEL, &fusion->reply_frames_desc_phys[i]);
578 if (!fusion->reply_frames_desc[i]) {
579 dev_err(&instance->pdev->dev,
580 "Failed from %s %d\n", __func__, __LINE__);
581 return -ENOMEM;
582 }
583
584 fusion->rdpq_virt[i].RDPQBaseAddress =
Shivasharan Sff96f922017-02-10 00:59:20 -0800585 cpu_to_le64(fusion->reply_frames_desc_phys[i]);
Sumit Saxena179ac142016-01-28 21:04:28 +0530586
587 reply_desc = fusion->reply_frames_desc[i];
588 for (j = 0; j < fusion->reply_q_depth; j++, reply_desc++)
589 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
590 }
591 return 0;
592}
593
594static void
595megasas_free_rdpq_fusion(struct megasas_instance *instance) {
596
597 int i;
598 struct fusion_context *fusion;
599
600 fusion = instance->ctrl_context;
601
602 for (i = 0; i < MAX_MSIX_QUEUES_FUSION; i++) {
603 if (fusion->reply_frames_desc[i])
604 pci_pool_free(fusion->reply_frames_desc_pool,
605 fusion->reply_frames_desc[i],
606 fusion->reply_frames_desc_phys[i]);
607 }
608
609 if (fusion->reply_frames_desc_pool)
610 pci_pool_destroy(fusion->reply_frames_desc_pool);
611
612 if (fusion->rdpq_virt)
613 pci_free_consistent(instance->pdev,
614 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION,
615 fusion->rdpq_virt, fusion->rdpq_phys);
616}
617
618static void
619megasas_free_reply_fusion(struct megasas_instance *instance) {
620
621 struct fusion_context *fusion;
622
623 fusion = instance->ctrl_context;
624
625 if (fusion->reply_frames_desc[0])
626 pci_pool_free(fusion->reply_frames_desc_pool,
627 fusion->reply_frames_desc[0],
628 fusion->reply_frames_desc_phys[0]);
629
630 if (fusion->reply_frames_desc_pool)
631 pci_pool_destroy(fusion->reply_frames_desc_pool);
632
633}
634
635
adam radford9c915a82010-12-21 13:34:31 -0800636/**
637 * megasas_alloc_cmds_fusion - Allocates the command packets
638 * @instance: Adapter soft state
639 *
640 *
641 * Each frame has a 32-bit field called context. This context is used to get
642 * back the megasas_cmd_fusion from the frame when a frame gets completed
643 * In this driver, the 32 bit values are the indices into an array cmd_list.
644 * This array is used only to look up the megasas_cmd_fusion given the context.
645 * The free commands themselves are maintained in a linked list called cmd_pool.
646 *
647 * cmds are formed in the io_request and sg_frame members of the
648 * megasas_cmd_fusion. The context field is used to get a request descriptor
649 * and is used as SMID of the cmd.
650 * SMID value range is from 1 to max_fw_cmds.
651 */
652int
653megasas_alloc_cmds_fusion(struct megasas_instance *instance)
654{
Sumit Saxena179ac142016-01-28 21:04:28 +0530655 int i;
adam radford9c915a82010-12-21 13:34:31 -0800656 struct fusion_context *fusion;
657 struct megasas_cmd_fusion *cmd;
adam radford9c915a82010-12-21 13:34:31 -0800658 u32 offset;
659 dma_addr_t io_req_base_phys;
660 u8 *io_req_base;
661
Sumit Saxena179ac142016-01-28 21:04:28 +0530662
adam radford9c915a82010-12-21 13:34:31 -0800663 fusion = instance->ctrl_context;
664
Sumit Saxena179ac142016-01-28 21:04:28 +0530665 if (megasas_alloc_cmdlist_fusion(instance))
666 goto fail_exit;
adam radford9c915a82010-12-21 13:34:31 -0800667
Sumit Saxena179ac142016-01-28 21:04:28 +0530668 if (megasas_alloc_request_fusion(instance))
669 goto fail_exit;
adam radford9c915a82010-12-21 13:34:31 -0800670
Sumit Saxena179ac142016-01-28 21:04:28 +0530671 if (instance->is_rdpq) {
672 if (megasas_alloc_rdpq_fusion(instance))
673 goto fail_exit;
674 } else
675 if (megasas_alloc_reply_fusion(instance))
676 goto fail_exit;
adam radford9c915a82010-12-21 13:34:31 -0800677
adam radford9c915a82010-12-21 13:34:31 -0800678
Sumit Saxena179ac142016-01-28 21:04:28 +0530679 /* The first 256 bytes (SMID 0) is not used. Don't add to the cmd list */
680 io_req_base = fusion->io_request_frames + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
681 io_req_base_phys = fusion->io_request_frames_phys + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
adam radford9c915a82010-12-21 13:34:31 -0800682
683 /*
684 * Add all the commands to command pool (fusion->cmd_pool)
685 */
686
687 /* SMID 0 is reserved. Set SMID/index from 1 */
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500688 for (i = 0; i < instance->max_mpt_cmds; i++) {
adam radford9c915a82010-12-21 13:34:31 -0800689 cmd = fusion->cmd_list[i];
690 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
691 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
692 cmd->index = i + 1;
693 cmd->scmd = NULL;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -0500694 cmd->sync_cmd_idx =
695 (i >= instance->max_scsi_cmds && i < instance->max_fw_cmds) ?
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530696 (i - instance->max_scsi_cmds) :
697 (u32)ULONG_MAX; /* Set to Invalid */
adam radford9c915a82010-12-21 13:34:31 -0800698 cmd->instance = instance;
699 cmd->io_request =
700 (struct MPI2_RAID_SCSI_IO_REQUEST *)
701 (io_req_base + offset);
702 memset(cmd->io_request, 0,
703 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
704 cmd->io_request_phys_addr = io_req_base_phys + offset;
Shivasharan S8bf7c652017-02-10 00:59:03 -0800705 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
adam radford9c915a82010-12-21 13:34:31 -0800706 }
707
Sumit Saxena179ac142016-01-28 21:04:28 +0530708 if (megasas_create_sg_sense_fusion(instance))
709 goto fail_exit;
adam radford9c915a82010-12-21 13:34:31 -0800710
711 return 0;
712
Sumit Saxena179ac142016-01-28 21:04:28 +0530713fail_exit:
714 megasas_free_cmds_fusion(instance);
adam radford9c915a82010-12-21 13:34:31 -0800715 return -ENOMEM;
716}
717
718/**
719 * wait_and_poll - Issues a polling command
720 * @instance: Adapter soft state
721 * @cmd: Command packet to be issued
722 *
723 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
724 */
725int
adam radford229fe472014-03-10 02:51:56 -0700726wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
727 int seconds)
adam radford9c915a82010-12-21 13:34:31 -0800728{
729 int i;
730 struct megasas_header *frame_hdr = &cmd->frame->hdr;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530731 struct fusion_context *fusion;
adam radford9c915a82010-12-21 13:34:31 -0800732
adam radford229fe472014-03-10 02:51:56 -0700733 u32 msecs = seconds * 1000;
adam radford9c915a82010-12-21 13:34:31 -0800734
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530735 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -0800736 /*
737 * Wait for cmd_status to change
738 */
739 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
740 rmb();
741 msleep(20);
742 }
743
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530744 if (frame_hdr->cmd_status == MFI_STAT_INVALID_STATUS)
745 return DCMD_TIMEOUT;
746 else if (frame_hdr->cmd_status == MFI_STAT_OK)
747 return DCMD_SUCCESS;
748 else
749 return DCMD_FAILED;
adam radford9c915a82010-12-21 13:34:31 -0800750}
751
752/**
753 * megasas_ioc_init_fusion - Initializes the FW
754 * @instance: Adapter soft state
755 *
756 * Issues the IOC Init cmd
757 */
758int
759megasas_ioc_init_fusion(struct megasas_instance *instance)
760{
761 struct megasas_init_frame *init_frame;
Sumit Saxena179ac142016-01-28 21:04:28 +0530762 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage = NULL;
adam radford9c915a82010-12-21 13:34:31 -0800763 dma_addr_t ioc_init_handle;
adam radford9c915a82010-12-21 13:34:31 -0800764 struct megasas_cmd *cmd;
Sumit Saxena179ac142016-01-28 21:04:28 +0530765 u8 ret, cur_rdpq_mode;
adam radford9c915a82010-12-21 13:34:31 -0800766 struct fusion_context *fusion;
adam radfordc77a9bd2014-03-10 02:51:36 -0700767 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
adam radford9c915a82010-12-21 13:34:31 -0800768 int i;
769 struct megasas_header *frame_hdr;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +0530770 const char *sys_info;
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530771 MFI_CAPABILITIES *drv_ops;
Sumit Saxena179ac142016-01-28 21:04:28 +0530772 u32 scratch_pad_2;
Shivasharan S45b8a352017-02-10 00:59:04 -0800773 unsigned long flags;
adam radford9c915a82010-12-21 13:34:31 -0800774
775 fusion = instance->ctrl_context;
776
777 cmd = megasas_get_cmd(instance);
778
779 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500780 dev_err(&instance->pdev->dev, "Could not allocate cmd for INIT Frame\n");
adam radford9c915a82010-12-21 13:34:31 -0800781 ret = 1;
782 goto fail_get_cmd;
783 }
784
Sumit Saxena179ac142016-01-28 21:04:28 +0530785 scratch_pad_2 = readl
786 (&instance->reg_set->outbound_scratch_pad_2);
787
788 cur_rdpq_mode = (scratch_pad_2 & MR_RDPQ_MODE_OFFSET) ? 1 : 0;
789
790 if (instance->is_rdpq && !cur_rdpq_mode) {
791 dev_err(&instance->pdev->dev, "Firmware downgrade *NOT SUPPORTED*"
792 " from RDPQ mode to non RDPQ mode\n");
793 ret = 1;
794 goto fail_fw_init;
795 }
796
Kashyap Desaid0fc91d2016-10-21 06:33:33 -0700797 instance->fw_sync_cache_support = (scratch_pad_2 &
798 MR_CAN_HANDLE_SYNC_CACHE_OFFSET) ? 1 : 0;
799 dev_info(&instance->pdev->dev, "FW supports sync cache\t: %s\n",
800 instance->fw_sync_cache_support ? "Yes" : "No");
801
adam radford9c915a82010-12-21 13:34:31 -0800802 IOCInitMessage =
803 dma_alloc_coherent(&instance->pdev->dev,
804 sizeof(struct MPI2_IOC_INIT_REQUEST),
805 &ioc_init_handle, GFP_KERNEL);
806
807 if (!IOCInitMessage) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500808 dev_err(&instance->pdev->dev, "Could not allocate memory for "
adam radford9c915a82010-12-21 13:34:31 -0800809 "IOCInitMessage\n");
810 ret = 1;
811 goto fail_fw_init;
812 }
813
814 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
815
816 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
817 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530818 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
819 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
820 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
adam radford9c915a82010-12-21 13:34:31 -0800821
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530822 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
Sumit Saxena179ac142016-01-28 21:04:28 +0530823 IOCInitMessage->ReplyDescriptorPostQueueAddress = instance->is_rdpq ?
824 cpu_to_le64(fusion->rdpq_phys) :
825 cpu_to_le64(fusion->reply_frames_desc_phys[0]);
826 IOCInitMessage->MsgFlags = instance->is_rdpq ?
827 MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE : 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530828 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
adam radford5738f992012-03-19 19:49:53 -0700829 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
Shivasharan S15dd0382017-02-10 00:59:10 -0800830 IOCInitMessage->HostPageSize = MR_DEFAULT_NVME_PAGE_SHIFT;
adam radford9c915a82010-12-21 13:34:31 -0800831 init_frame = (struct megasas_init_frame *)cmd->frame;
832 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
833
834 frame_hdr = &cmd->frame->hdr;
adam radford9c915a82010-12-21 13:34:31 -0800835 frame_hdr->cmd_status = 0xFF;
Christoph Hellwig48433bd2015-04-23 16:33:39 +0530836 frame_hdr->flags = cpu_to_le16(
837 le16_to_cpu(frame_hdr->flags) |
838 MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
adam radford9c915a82010-12-21 13:34:31 -0800839
840 init_frame->cmd = MFI_CMD_INIT;
841 init_frame->cmd_status = 0xFF;
842
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530843 drv_ops = (MFI_CAPABILITIES *) &(init_frame->driver_operations);
844
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530845 /* driver support Extended MSIX */
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -0500846 if (fusion->adapter_type >= INVADER_SERIES)
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530847 drv_ops->mfi_capabilities.support_additional_msix = 1;
adam radford21c9e162013-09-06 15:27:14 -0700848 /* driver supports HA / Remote LUN over Fast Path interface */
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530849 drv_ops->mfi_capabilities.support_fp_remote_lun = 1;
850
851 drv_ops->mfi_capabilities.support_max_255lds = 1;
852 drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1;
853 drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1;
854
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +0530855 if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
856 drv_ops->mfi_capabilities.support_ext_io_size = 1;
857
Sumit Saxena8f050242016-01-28 21:04:27 +0530858 drv_ops->mfi_capabilities.support_fp_rlbypass = 1;
Sumit Saxena308ec452016-01-28 21:04:30 +0530859 if (!dual_qdepth_disable)
860 drv_ops->mfi_capabilities.support_ext_queue_depth = 1;
Sumit Saxena8f050242016-01-28 21:04:27 +0530861
Sumit Saxena52b62ac2016-01-28 21:04:31 +0530862 drv_ops->mfi_capabilities.support_qd_throttling = 1;
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -0500863 drv_ops->mfi_capabilities.support_pd_map_target_id = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530864 /* Convert capability to LE32 */
865 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
adam radford21c9e162013-09-06 15:27:14 -0700866
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +0530867 sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
868 if (instance->system_info_buf && sys_info) {
869 memcpy(instance->system_info_buf->systemId, sys_info,
870 strlen(sys_info) > 64 ? 64 : strlen(sys_info));
871 instance->system_info_buf->systemIdLength =
872 strlen(sys_info) > 64 ? 64 : strlen(sys_info);
873 init_frame->system_info_lo = instance->system_info_h;
874 init_frame->system_info_hi = 0;
875 }
876
Hannes Reineckefdc5a972014-01-16 11:25:33 +0100877 init_frame->queue_info_new_phys_addr_hi =
878 cpu_to_le32(upper_32_bits(ioc_init_handle));
879 init_frame->queue_info_new_phys_addr_lo =
880 cpu_to_le32(lower_32_bits(ioc_init_handle));
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530881 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
adam radford9c915a82010-12-21 13:34:31 -0800882
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +0530883 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
884 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
adam radfordc77a9bd2014-03-10 02:51:36 -0700885 req_desc.MFAIo.RequestFlags =
adam radford9c915a82010-12-21 13:34:31 -0800886 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +0530887 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford9c915a82010-12-21 13:34:31 -0800888
889 /*
890 * disable the intr before firing the init frame
891 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530892 instance->instancet->disable_intr(instance);
adam radford9c915a82010-12-21 13:34:31 -0800893
894 for (i = 0; i < (10 * 1000); i += 20) {
895 if (readl(&instance->reg_set->doorbell) & 1)
896 msleep(20);
897 else
898 break;
899 }
900
Shivasharan S45b8a352017-02-10 00:59:04 -0800901 /* For Ventura also IOC INIT required 64 bit Descriptor write. */
902 spin_lock_irqsave(&instance->hba_lock, flags);
903 writel(le32_to_cpu(req_desc.u.low),
904 &instance->reg_set->inbound_low_queue_port);
905 writel(le32_to_cpu(req_desc.u.high),
906 &instance->reg_set->inbound_high_queue_port);
907 mmiowb();
908 spin_unlock_irqrestore(&instance->hba_lock, flags);
adam radford9c915a82010-12-21 13:34:31 -0800909
adam radford229fe472014-03-10 02:51:56 -0700910 wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -0800911
912 frame_hdr = &cmd->frame->hdr;
913 if (frame_hdr->cmd_status != 0) {
914 ret = 1;
915 goto fail_fw_init;
916 }
Andy Lutomirski13f30772016-05-03 10:24:31 -0700917 dev_info(&instance->pdev->dev, "Init cmd success\n");
adam radford9c915a82010-12-21 13:34:31 -0800918
919 ret = 0;
920
921fail_fw_init:
922 megasas_return_cmd(instance, cmd);
923 if (IOCInitMessage)
924 dma_free_coherent(&instance->pdev->dev,
925 sizeof(struct MPI2_IOC_INIT_REQUEST),
926 IOCInitMessage, ioc_init_handle);
927fail_get_cmd:
928 return ret;
929}
930
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +0530931/**
932 * megasas_sync_pd_seq_num - JBOD SEQ MAP
933 * @instance: Adapter soft state
934 * @pend: set to 1, if it is pended jbod map.
935 *
936 * Issue Jbod map to the firmware. If it is pended command,
937 * issue command and return. If it is first instance of jbod map
938 * issue and receive command.
939 */
940int
941megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) {
942 int ret = 0;
943 u32 pd_seq_map_sz;
944 struct megasas_cmd *cmd;
945 struct megasas_dcmd_frame *dcmd;
946 struct fusion_context *fusion = instance->ctrl_context;
947 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
948 dma_addr_t pd_seq_h;
949
950 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)];
951 pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)];
952 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
953 (sizeof(struct MR_PD_CFG_SEQ) *
954 (MAX_PHYSICAL_DEVICES - 1));
955
956 cmd = megasas_get_cmd(instance);
957 if (!cmd) {
958 dev_err(&instance->pdev->dev,
959 "Could not get mfi cmd. Fail from %s %d\n",
960 __func__, __LINE__);
961 return -ENOMEM;
962 }
963
964 dcmd = &cmd->frame->dcmd;
965
966 memset(pd_sync, 0, pd_seq_map_sz);
967 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
968 dcmd->cmd = MFI_CMD_DCMD;
969 dcmd->cmd_status = 0xFF;
970 dcmd->sge_count = 1;
971 dcmd->timeout = 0;
972 dcmd->pad_0 = 0;
973 dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz);
974 dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
975 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(pd_seq_h);
976 dcmd->sgl.sge32[0].length = cpu_to_le32(pd_seq_map_sz);
977
978 if (pend) {
979 dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG;
980 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
981 instance->jbod_seq_cmd = cmd;
982 instance->instancet->issue_dcmd(instance, cmd);
983 return 0;
984 }
985
986 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
987
988 /* Below code is only for non pended DCMD */
989 if (instance->ctrl_context && !instance->mask_interrupts)
Sumit Saxena6d40afb2016-01-28 21:04:23 +0530990 ret = megasas_issue_blocked_cmd(instance, cmd,
991 MFI_IO_TIMEOUT_SECS);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +0530992 else
993 ret = megasas_issue_polled(instance, cmd);
994
995 if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
996 dev_warn(&instance->pdev->dev,
997 "driver supports max %d JBOD, but FW reports %d\n",
998 MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count));
999 ret = -EINVAL;
1000 }
1001
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301002 if (ret == DCMD_TIMEOUT && instance->ctrl_context)
1003 megaraid_sas_kill_hba(instance);
1004
1005 if (ret == DCMD_SUCCESS)
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301006 instance->pd_seq_map_id++;
1007
1008 megasas_return_cmd(instance, cmd);
1009 return ret;
1010}
1011
adam radford9c915a82010-12-21 13:34:31 -08001012/*
adam radford9c915a82010-12-21 13:34:31 -08001013 * megasas_get_ld_map_info - Returns FW's ld_map structure
1014 * @instance: Adapter soft state
1015 * @pend: Pend the command or not
1016 * Issues an internal command (DCMD) to get the FW's controller PD
1017 * list structure. This information is mainly used to find out SYSTEM
1018 * supported by the FW.
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301019 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
1020 * dcmd.mbox.b[0] - number of LDs being sync'd
1021 * dcmd.mbox.b[1] - 0 - complete command immediately.
1022 * - 1 - pend till config change
1023 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
1024 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
1025 * uses extended struct MR_FW_RAID_MAP_EXT
adam radford9c915a82010-12-21 13:34:31 -08001026 */
1027static int
1028megasas_get_ld_map_info(struct megasas_instance *instance)
1029{
1030 int ret = 0;
1031 struct megasas_cmd *cmd;
1032 struct megasas_dcmd_frame *dcmd;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301033 void *ci;
adam radford9c915a82010-12-21 13:34:31 -08001034 dma_addr_t ci_h = 0;
1035 u32 size_map_info;
1036 struct fusion_context *fusion;
1037
1038 cmd = megasas_get_cmd(instance);
1039
1040 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001041 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n");
adam radford9c915a82010-12-21 13:34:31 -08001042 return -ENOMEM;
1043 }
1044
1045 fusion = instance->ctrl_context;
1046
1047 if (!fusion) {
1048 megasas_return_cmd(instance, cmd);
Hannes Reinecke2f8bdfa2014-01-16 11:25:35 +01001049 return -ENXIO;
adam radford9c915a82010-12-21 13:34:31 -08001050 }
1051
1052 dcmd = &cmd->frame->dcmd;
1053
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301054 size_map_info = fusion->current_map_sz;
adam radford9c915a82010-12-21 13:34:31 -08001055
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301056 ci = (void *) fusion->ld_map[(instance->map_id & 1)];
adam radford9c915a82010-12-21 13:34:31 -08001057 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
1058
1059 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001060 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n");
adam radford9c915a82010-12-21 13:34:31 -08001061 megasas_return_cmd(instance, cmd);
1062 return -ENOMEM;
1063 }
1064
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301065 memset(ci, 0, fusion->max_map_sz);
adam radford9c915a82010-12-21 13:34:31 -08001066 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
adam radford9c915a82010-12-21 13:34:31 -08001067 dcmd->cmd = MFI_CMD_DCMD;
1068 dcmd->cmd_status = 0xFF;
1069 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301070 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford9c915a82010-12-21 13:34:31 -08001071 dcmd->timeout = 0;
1072 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301073 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
1074 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1075 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
1076 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -08001077
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301078 if (instance->ctrl_context && !instance->mask_interrupts)
1079 ret = megasas_issue_blocked_cmd(instance, cmd,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301080 MFI_IO_TIMEOUT_SECS);
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05301081 else
1082 ret = megasas_issue_polled(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08001083
Sumit Saxena6d40afb2016-01-28 21:04:23 +05301084 if (ret == DCMD_TIMEOUT && instance->ctrl_context)
1085 megaraid_sas_kill_hba(instance);
1086
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05301087 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08001088
1089 return ret;
1090}
1091
1092u8
1093megasas_get_map_info(struct megasas_instance *instance)
1094{
1095 struct fusion_context *fusion = instance->ctrl_context;
1096
1097 fusion->fast_path_io = 0;
1098 if (!megasas_get_ld_map_info(instance)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +05301099 if (MR_ValidateMapInfo(instance)) {
adam radford9c915a82010-12-21 13:34:31 -08001100 fusion->fast_path_io = 1;
1101 return 0;
1102 }
1103 }
1104 return 1;
1105}
1106
1107/*
1108 * megasas_sync_map_info - Returns FW's ld_map structure
1109 * @instance: Adapter soft state
1110 *
1111 * Issues an internal command (DCMD) to get the FW's controller PD
1112 * list structure. This information is mainly used to find out SYSTEM
1113 * supported by the FW.
1114 */
1115int
1116megasas_sync_map_info(struct megasas_instance *instance)
1117{
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001118 int i;
adam radford9c915a82010-12-21 13:34:31 -08001119 struct megasas_cmd *cmd;
1120 struct megasas_dcmd_frame *dcmd;
Shivasharan Sd2d03582017-02-10 00:59:19 -08001121 u16 num_lds;
1122 u32 size_sync_info;
adam radford9c915a82010-12-21 13:34:31 -08001123 struct fusion_context *fusion;
1124 struct MR_LD_TARGET_SYNC *ci = NULL;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301125 struct MR_DRV_RAID_MAP_ALL *map;
adam radford9c915a82010-12-21 13:34:31 -08001126 struct MR_LD_RAID *raid;
1127 struct MR_LD_TARGET_SYNC *ld_sync;
1128 dma_addr_t ci_h = 0;
1129 u32 size_map_info;
1130
1131 cmd = megasas_get_cmd(instance);
1132
1133 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001134 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n");
adam radford9c915a82010-12-21 13:34:31 -08001135 return -ENOMEM;
1136 }
1137
1138 fusion = instance->ctrl_context;
1139
1140 if (!fusion) {
1141 megasas_return_cmd(instance, cmd);
1142 return 1;
1143 }
1144
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301145 map = fusion->ld_drv_map[instance->map_id & 1];
adam radford9c915a82010-12-21 13:34:31 -08001146
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301147 num_lds = le16_to_cpu(map->raidMap.ldCount);
adam radford9c915a82010-12-21 13:34:31 -08001148
1149 dcmd = &cmd->frame->dcmd;
1150
1151 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
1152
1153 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
1154
1155 ci = (struct MR_LD_TARGET_SYNC *)
1156 fusion->ld_map[(instance->map_id - 1) & 1];
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301157 memset(ci, 0, fusion->max_map_sz);
adam radford9c915a82010-12-21 13:34:31 -08001158
1159 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
1160
1161 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
1162
1163 for (i = 0; i < num_lds; i++, ld_sync++) {
1164 raid = MR_LdRaidGet(i, map);
1165 ld_sync->targetId = MR_GetLDTgtId(i, map);
1166 ld_sync->seqNum = raid->seqNum;
1167 }
1168
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301169 size_map_info = fusion->current_map_sz;
adam radford9c915a82010-12-21 13:34:31 -08001170
1171 dcmd->cmd = MFI_CMD_DCMD;
1172 dcmd->cmd_status = 0xFF;
1173 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301174 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
adam radford9c915a82010-12-21 13:34:31 -08001175 dcmd->timeout = 0;
1176 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301177 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -08001178 dcmd->mbox.b[0] = num_lds;
1179 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301180 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
1181 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
1182 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -08001183
1184 instance->map_update_cmd = cmd;
1185
1186 instance->instancet->issue_dcmd(instance, cmd);
1187
Shivasharan Sf4fc2092017-02-10 00:59:09 -08001188 return 0;
adam radford9c915a82010-12-21 13:34:31 -08001189}
1190
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301191/*
1192 * meagasas_display_intel_branding - Display branding string
1193 * @instance: per adapter object
1194 *
1195 * Return nothing.
1196 */
1197static void
1198megasas_display_intel_branding(struct megasas_instance *instance)
1199{
1200 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
1201 return;
1202
1203 switch (instance->pdev->device) {
1204 case PCI_DEVICE_ID_LSI_INVADER:
1205 switch (instance->pdev->subsystem_device) {
1206 case MEGARAID_INTEL_RS3DC080_SSDID:
1207 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1208 instance->host->host_no,
1209 MEGARAID_INTEL_RS3DC080_BRANDING);
1210 break;
1211 case MEGARAID_INTEL_RS3DC040_SSDID:
1212 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1213 instance->host->host_no,
1214 MEGARAID_INTEL_RS3DC040_BRANDING);
1215 break;
1216 case MEGARAID_INTEL_RS3SC008_SSDID:
1217 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1218 instance->host->host_no,
1219 MEGARAID_INTEL_RS3SC008_BRANDING);
1220 break;
1221 case MEGARAID_INTEL_RS3MC044_SSDID:
1222 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1223 instance->host->host_no,
1224 MEGARAID_INTEL_RS3MC044_BRANDING);
1225 break;
1226 default:
1227 break;
1228 }
1229 break;
1230 case PCI_DEVICE_ID_LSI_FURY:
1231 switch (instance->pdev->subsystem_device) {
1232 case MEGARAID_INTEL_RS3WC080_SSDID:
1233 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1234 instance->host->host_no,
1235 MEGARAID_INTEL_RS3WC080_BRANDING);
1236 break;
1237 case MEGARAID_INTEL_RS3WC040_SSDID:
1238 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1239 instance->host->host_no,
1240 MEGARAID_INTEL_RS3WC040_BRANDING);
1241 break;
1242 default:
1243 break;
1244 }
1245 break;
sumit.saxena@avagotech.com7364d342015-10-15 13:39:54 +05301246 case PCI_DEVICE_ID_LSI_CUTLASS_52:
1247 case PCI_DEVICE_ID_LSI_CUTLASS_53:
1248 switch (instance->pdev->subsystem_device) {
1249 case MEGARAID_INTEL_RMS3BC160_SSDID:
1250 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1251 instance->host->host_no,
1252 MEGARAID_INTEL_RMS3BC160_BRANDING);
1253 break;
1254 default:
1255 break;
1256 }
1257 break;
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301258 default:
1259 break;
1260 }
1261}
1262
adam radford9c915a82010-12-21 13:34:31 -08001263/**
1264 * megasas_init_adapter_fusion - Initializes the FW
1265 * @instance: Adapter soft state
1266 *
1267 * This is the main function for initializing firmware.
1268 */
1269u32
1270megasas_init_adapter_fusion(struct megasas_instance *instance)
1271{
1272 struct megasas_register_set __iomem *reg_set;
1273 struct fusion_context *fusion;
Shivasharan S50b7f5a2017-02-10 00:59:25 -08001274 u16 max_cmd;
1275 u32 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 /*
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05301285 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
1286 */
1287 instance->max_mfi_cmds =
1288 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
adam radford9c915a82010-12-21 13:34:31 -08001289
1290 max_cmd = instance->max_fw_cmds;
1291
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05301292 fusion->reply_q_depth = 2 * (((max_cmd + 1 + 15)/16)*16);
adam radford9c915a82010-12-21 13:34:31 -08001293
1294 fusion->request_alloc_sz =
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001295 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * instance->max_mpt_cmds;
adam radford9c915a82010-12-21 13:34:31 -08001296 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
1297 *(fusion->reply_q_depth);
1298 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001299 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1300 * (instance->max_mpt_cmds + 1)); /* Extra 1 for SMID 0 */
adam radford9c915a82010-12-21 13:34:31 -08001301
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301302 scratch_pad_2 = readl(&instance->reg_set->outbound_scratch_pad_2);
1303 /* If scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
1304 * Firmware support extended IO chain frame which is 4 times more than
1305 * legacy Firmware.
1306 * Legacy Firmware - Frame size is (8 * 128) = 1K
1307 * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
1308 */
1309 if (scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
1310 instance->max_chain_frame_sz =
1311 ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1312 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
1313 else
1314 instance->max_chain_frame_sz =
1315 ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1316 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;
1317
1318 if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
1319 dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
1320 instance->max_chain_frame_sz,
1321 MEGASAS_CHAIN_FRAME_SZ_MIN);
1322 instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
1323 }
1324
adam radford9c915a82010-12-21 13:34:31 -08001325 fusion->max_sge_in_main_msg =
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301326 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1327 - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
adam radford9c915a82010-12-21 13:34:31 -08001328
1329 fusion->max_sge_in_chain =
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301330 instance->max_chain_frame_sz
1331 / sizeof(union MPI2_SGE_IO_UNION);
adam radford9c915a82010-12-21 13:34:31 -08001332
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301333 instance->max_num_sge =
1334 rounddown_pow_of_two(fusion->max_sge_in_main_msg
1335 + fusion->max_sge_in_chain - 2);
adam radford9c915a82010-12-21 13:34:31 -08001336
1337 /* Used for pass thru MFI frame (DCMD) */
1338 fusion->chain_offset_mfi_pthru =
1339 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1340
1341 fusion->chain_offset_io_request =
1342 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1343 sizeof(union MPI2_SGE_IO_UNION))/16;
1344
adam radfordc8e858f2011-10-08 18:15:13 -07001345 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1346 for (i = 0 ; i < count; i++)
1347 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -08001348
1349 /*
Shivasharan Sec779592017-02-10 00:59:35 -08001350 * For fusion adapters, 3 commands for IOCTL and 8 commands
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05301351 * for driver's internal DCMDs.
1352 */
1353 instance->max_scsi_cmds = instance->max_fw_cmds -
1354 (MEGASAS_FUSION_INTERNAL_CMDS +
1355 MEGASAS_FUSION_IOCTL_CMDS);
1356 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1357
1358 /*
adam radford9c915a82010-12-21 13:34:31 -08001359 * Allocate memory for descriptors
1360 * Create a pool of commands
1361 */
1362 if (megasas_alloc_cmds(instance))
1363 goto fail_alloc_mfi_cmds;
1364 if (megasas_alloc_cmds_fusion(instance))
1365 goto fail_alloc_cmds;
1366
1367 if (megasas_ioc_init_fusion(instance))
1368 goto fail_ioc_init;
1369
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301370 megasas_display_intel_branding(instance);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05301371 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301372 dev_err(&instance->pdev->dev,
1373 "Could not get controller info. Fail from %s %d\n",
1374 __func__, __LINE__);
1375 goto fail_ioc_init;
1376 }
1377
adam radford9c915a82010-12-21 13:34:31 -08001378 instance->flag_ieee = 1;
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08001379 instance->r1_ldio_hint_default = MR_R1_LDIO_PIGGYBACK_DEFAULT;
adam radford9c915a82010-12-21 13:34:31 -08001380 fusion->fast_path_io = 0;
1381
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301382 fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1383 for (i = 0; i < 2; i++) {
1384 fusion->ld_map[i] = NULL;
1385 fusion->ld_drv_map[i] = (void *)__get_free_pages(GFP_KERNEL,
1386 fusion->drv_map_pages);
1387 if (!fusion->ld_drv_map[i]) {
1388 dev_err(&instance->pdev->dev, "Could not allocate "
1389 "memory for local map info for %d pages\n",
1390 fusion->drv_map_pages);
1391 if (i == 1)
1392 free_pages((ulong)fusion->ld_drv_map[0],
1393 fusion->drv_map_pages);
1394 goto fail_ioc_init;
1395 }
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05301396 memset(fusion->ld_drv_map[i], 0,
1397 ((1 << PAGE_SHIFT) << fusion->drv_map_pages));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301398 }
1399
adam radford9c915a82010-12-21 13:34:31 -08001400 for (i = 0; i < 2; i++) {
1401 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301402 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08001403 &fusion->ld_map_phys[i],
1404 GFP_KERNEL);
1405 if (!fusion->ld_map[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001406 dev_err(&instance->pdev->dev, "Could not allocate memory "
adam radford9c915a82010-12-21 13:34:31 -08001407 "for map info\n");
1408 goto fail_map_info;
1409 }
1410 }
1411
1412 if (!megasas_get_map_info(instance))
1413 megasas_sync_map_info(instance);
1414
1415 return 0;
1416
adam radford9c915a82010-12-21 13:34:31 -08001417fail_map_info:
1418 if (i == 1)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301419 dma_free_coherent(&instance->pdev->dev, fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08001420 fusion->ld_map[0], fusion->ld_map_phys[0]);
1421fail_ioc_init:
adam radfordeb1b1232011-02-24 20:55:56 -08001422 megasas_free_cmds_fusion(instance);
1423fail_alloc_cmds:
1424 megasas_free_cmds(instance);
1425fail_alloc_mfi_cmds:
adam radford9c915a82010-12-21 13:34:31 -08001426 return 1;
1427}
1428
1429/**
adam radford9c915a82010-12-21 13:34:31 -08001430 * map_cmd_status - Maps FW cmd status to OS cmd status
1431 * @cmd : Pointer to cmd
1432 * @status : status of cmd returned by FW
1433 * @ext_status : ext status of cmd returned by FW
1434 */
1435
1436void
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001437map_cmd_status(struct fusion_context *fusion,
Shivasharan S41064f12017-02-10 00:59:37 -08001438 struct scsi_cmnd *scmd, u8 status, u8 ext_status,
1439 u32 data_length, u8 *sense)
adam radford9c915a82010-12-21 13:34:31 -08001440{
Shivasharan Sdef0eab2017-02-10 00:59:15 -08001441 u8 cmd_type;
Shivasharan S31d9a572017-02-10 00:59:14 -08001442 int resid;
adam radford9c915a82010-12-21 13:34:31 -08001443
Shivasharan Sdef0eab2017-02-10 00:59:15 -08001444 cmd_type = megasas_cmd_type(scmd);
adam radford9c915a82010-12-21 13:34:31 -08001445 switch (status) {
1446
1447 case MFI_STAT_OK:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001448 scmd->result = DID_OK << 16;
adam radford9c915a82010-12-21 13:34:31 -08001449 break;
1450
1451 case MFI_STAT_SCSI_IO_FAILED:
1452 case MFI_STAT_LD_INIT_IN_PROGRESS:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001453 scmd->result = (DID_ERROR << 16) | ext_status;
adam radford9c915a82010-12-21 13:34:31 -08001454 break;
1455
1456 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1457
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001458 scmd->result = (DID_OK << 16) | ext_status;
adam radford9c915a82010-12-21 13:34:31 -08001459 if (ext_status == SAM_STAT_CHECK_CONDITION) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001460 memset(scmd->sense_buffer, 0,
adam radford9c915a82010-12-21 13:34:31 -08001461 SCSI_SENSE_BUFFERSIZE);
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001462 memcpy(scmd->sense_buffer, sense,
adam radford9c915a82010-12-21 13:34:31 -08001463 SCSI_SENSE_BUFFERSIZE);
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001464 scmd->result |= DRIVER_SENSE << 24;
adam radford9c915a82010-12-21 13:34:31 -08001465 }
Shivasharan S31d9a572017-02-10 00:59:14 -08001466
1467 /*
1468 * If the IO request is partially completed, then MR FW will
1469 * update "io_request->DataLength" field with actual number of
1470 * bytes transferred.Driver will set residual bytes count in
1471 * SCSI command structure.
1472 */
1473 resid = (scsi_bufflen(scmd) - data_length);
1474 scsi_set_resid(scmd, resid);
Shivasharan Sdef0eab2017-02-10 00:59:15 -08001475
1476 if (resid &&
1477 ((cmd_type == READ_WRITE_LDIO) ||
1478 (cmd_type == READ_WRITE_SYSPDIO)))
1479 scmd_printk(KERN_INFO, scmd, "BRCM Debug mfi stat 0x%x, data len"
1480 " requested/completed 0x%x/0x%x\n",
1481 status, scsi_bufflen(scmd), data_length);
adam radford9c915a82010-12-21 13:34:31 -08001482 break;
1483
1484 case MFI_STAT_LD_OFFLINE:
1485 case MFI_STAT_DEVICE_NOT_FOUND:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001486 scmd->result = DID_BAD_TARGET << 16;
adam radford9c915a82010-12-21 13:34:31 -08001487 break;
adam radford36807e62011-10-08 18:15:06 -07001488 case MFI_STAT_CONFIG_SEQ_MISMATCH:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001489 scmd->result = DID_IMM_RETRY << 16;
adam radford36807e62011-10-08 18:15:06 -07001490 break;
adam radford9c915a82010-12-21 13:34:31 -08001491 default:
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05001492 scmd->result = DID_ERROR << 16;
adam radford9c915a82010-12-21 13:34:31 -08001493 break;
1494 }
1495}
1496
1497/**
Shivasharan S33203bc2017-02-10 00:59:12 -08001498 * megasas_is_prp_possible -
1499 * Checks if native NVMe PRPs can be built for the IO
1500 *
1501 * @instance: Adapter soft state
1502 * @scmd: SCSI command from the mid-layer
1503 * @sge_count: scatter gather element count.
1504 *
1505 * Returns: true: PRPs can be built
1506 * false: IEEE SGLs needs to be built
1507 */
1508static bool
1509megasas_is_prp_possible(struct megasas_instance *instance,
1510 struct scsi_cmnd *scmd, int sge_count)
1511{
1512 struct fusion_context *fusion;
1513 int i;
1514 u32 data_length = 0;
1515 struct scatterlist *sg_scmd;
1516 bool build_prp = false;
1517 u32 mr_nvme_pg_size;
1518
1519 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1520 MR_DEFAULT_NVME_PAGE_SIZE);
1521 fusion = instance->ctrl_context;
1522 data_length = scsi_bufflen(scmd);
1523 sg_scmd = scsi_sglist(scmd);
1524
1525 /*
1526 * NVMe uses one PRP for each page (or part of a page)
1527 * look at the data length - if 4 pages or less then IEEE is OK
1528 * if > 5 pages then we need to build a native SGL
1529 * if > 4 and <= 5 pages, then check physical address of 1st SG entry
1530 * if this first size in the page is >= the residual beyond 4 pages
1531 * then use IEEE, otherwise use native SGL
1532 */
1533
1534 if (data_length > (mr_nvme_pg_size * 5)) {
1535 build_prp = true;
1536 } else if ((data_length > (mr_nvme_pg_size * 4)) &&
1537 (data_length <= (mr_nvme_pg_size * 5))) {
1538 /* check if 1st SG entry size is < residual beyond 4 pages */
1539 if (sg_dma_len(sg_scmd) < (data_length - (mr_nvme_pg_size * 4)))
1540 build_prp = true;
1541 }
1542
1543/*
1544 * Below code detects gaps/holes in IO data buffers.
1545 * What does holes/gaps mean?
1546 * Any SGE except first one in a SGL starts at non NVME page size
1547 * aligned address OR Any SGE except last one in a SGL ends at
1548 * non NVME page size boundary.
1549 *
1550 * Driver has already informed block layer by setting boundary rules for
1551 * bio merging done at NVME page size boundary calling kernel API
1552 * blk_queue_virt_boundary inside slave_config.
1553 * Still there is possibility of IO coming with holes to driver because of
1554 * IO merging done by IO scheduler.
1555 *
1556 * With SCSI BLK MQ enabled, there will be no IO with holes as there is no
1557 * IO scheduling so no IO merging.
1558 *
1559 * With SCSI BLK MQ disabled, IO scheduler may attempt to merge IOs and
1560 * then sending IOs with holes.
1561 *
1562 * Though driver can request block layer to disable IO merging by calling-
1563 * queue_flag_set_unlocked(QUEUE_FLAG_NOMERGES, sdev->request_queue) but
1564 * user may tune sysfs parameter- nomerges again to 0 or 1.
1565 *
1566 * If in future IO scheduling is enabled with SCSI BLK MQ,
1567 * this algorithm to detect holes will be required in driver
1568 * for SCSI BLK MQ enabled case as well.
1569 *
1570 *
1571 */
1572 scsi_for_each_sg(scmd, sg_scmd, sge_count, i) {
1573 if ((i != 0) && (i != (sge_count - 1))) {
1574 if (mega_mod64(sg_dma_len(sg_scmd), mr_nvme_pg_size) ||
1575 mega_mod64(sg_dma_address(sg_scmd),
1576 mr_nvme_pg_size)) {
1577 build_prp = false;
1578 atomic_inc(&instance->sge_holes_type1);
1579 break;
1580 }
1581 }
1582
1583 if ((sge_count > 1) && (i == 0)) {
1584 if ((mega_mod64((sg_dma_address(sg_scmd) +
1585 sg_dma_len(sg_scmd)),
1586 mr_nvme_pg_size))) {
1587 build_prp = false;
1588 atomic_inc(&instance->sge_holes_type2);
1589 break;
1590 }
1591 }
1592
1593 if ((sge_count > 1) && (i == (sge_count - 1))) {
1594 if (mega_mod64(sg_dma_address(sg_scmd),
1595 mr_nvme_pg_size)) {
1596 build_prp = false;
1597 atomic_inc(&instance->sge_holes_type3);
1598 break;
1599 }
1600 }
1601 }
1602
1603 return build_prp;
1604}
1605
1606/**
1607 * megasas_make_prp_nvme -
1608 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only
1609 *
1610 * @instance: Adapter soft state
1611 * @scmd: SCSI command from the mid-layer
1612 * @sgl_ptr: SGL to be filled in
1613 * @cmd: Fusion command frame
1614 * @sge_count: scatter gather element count.
1615 *
1616 * Returns: true: PRPs are built
1617 * false: IEEE SGLs needs to be built
1618 */
1619static bool
1620megasas_make_prp_nvme(struct megasas_instance *instance, struct scsi_cmnd *scmd,
1621 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1622 struct megasas_cmd_fusion *cmd, int sge_count)
1623{
1624 int sge_len, offset, num_prp_in_chain = 0;
1625 struct MPI25_IEEE_SGE_CHAIN64 *main_chain_element, *ptr_first_sgl;
Arnd Bergmannd1da5222017-02-14 22:21:51 +01001626 u64 *ptr_sgl;
1627 dma_addr_t ptr_sgl_phys;
Shivasharan S33203bc2017-02-10 00:59:12 -08001628 u64 sge_addr;
1629 u32 page_mask, page_mask_result;
1630 struct scatterlist *sg_scmd;
1631 u32 first_prp_len;
1632 bool build_prp = false;
1633 int data_len = scsi_bufflen(scmd);
1634 struct fusion_context *fusion;
1635 u32 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size,
1636 MR_DEFAULT_NVME_PAGE_SIZE);
1637
1638 fusion = instance->ctrl_context;
1639
1640 build_prp = megasas_is_prp_possible(instance, scmd, sge_count);
1641
1642 if (!build_prp)
1643 return false;
1644
1645 /*
1646 * Nvme has a very convoluted prp format. One prp is required
1647 * for each page or partial page. Driver need to split up OS sg_list
1648 * entries if it is longer than one page or cross a page
1649 * boundary. Driver also have to insert a PRP list pointer entry as
1650 * the last entry in each physical page of the PRP list.
1651 *
1652 * NOTE: The first PRP "entry" is actually placed in the first
1653 * SGL entry in the main message as IEEE 64 format. The 2nd
1654 * entry in the main message is the chain element, and the rest
1655 * of the PRP entries are built in the contiguous pcie buffer.
1656 */
1657 page_mask = mr_nvme_pg_size - 1;
1658 ptr_sgl = (u64 *)cmd->sg_frame;
Arnd Bergmannd1da5222017-02-14 22:21:51 +01001659 ptr_sgl_phys = cmd->sg_frame_phys_addr;
Shivasharan S33203bc2017-02-10 00:59:12 -08001660 memset(ptr_sgl, 0, instance->max_chain_frame_sz);
1661
1662 /* Build chain frame element which holds all prps except first*/
1663 main_chain_element = (struct MPI25_IEEE_SGE_CHAIN64 *)
1664 ((u8 *)sgl_ptr + sizeof(struct MPI25_IEEE_SGE_CHAIN64));
1665
Arnd Bergmannd1da5222017-02-14 22:21:51 +01001666 main_chain_element->Address = cpu_to_le64(ptr_sgl_phys);
Shivasharan S33203bc2017-02-10 00:59:12 -08001667 main_chain_element->NextChainOffset = 0;
1668 main_chain_element->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1669 IEEE_SGE_FLAGS_SYSTEM_ADDR |
1670 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP;
1671
1672 /* Build first prp, sge need not to be page aligned*/
1673 ptr_first_sgl = sgl_ptr;
1674 sg_scmd = scsi_sglist(scmd);
1675 sge_addr = sg_dma_address(sg_scmd);
1676 sge_len = sg_dma_len(sg_scmd);
1677
1678 offset = (u32)(sge_addr & page_mask);
1679 first_prp_len = mr_nvme_pg_size - offset;
1680
1681 ptr_first_sgl->Address = cpu_to_le64(sge_addr);
1682 ptr_first_sgl->Length = cpu_to_le32(first_prp_len);
1683
1684 data_len -= first_prp_len;
1685
1686 if (sge_len > first_prp_len) {
1687 sge_addr += first_prp_len;
1688 sge_len -= first_prp_len;
1689 } else if (sge_len == first_prp_len) {
1690 sg_scmd = sg_next(sg_scmd);
1691 sge_addr = sg_dma_address(sg_scmd);
1692 sge_len = sg_dma_len(sg_scmd);
1693 }
1694
1695 for (;;) {
1696 offset = (u32)(sge_addr & page_mask);
1697
1698 /* Put PRP pointer due to page boundary*/
1699 page_mask_result = (uintptr_t)(ptr_sgl + 1) & page_mask;
1700 if (unlikely(!page_mask_result)) {
1701 scmd_printk(KERN_NOTICE,
1702 scmd, "page boundary ptr_sgl: 0x%p\n",
1703 ptr_sgl);
Arnd Bergmannd1da5222017-02-14 22:21:51 +01001704 ptr_sgl_phys += 8;
1705 *ptr_sgl = cpu_to_le64(ptr_sgl_phys);
Shivasharan S33203bc2017-02-10 00:59:12 -08001706 ptr_sgl++;
1707 num_prp_in_chain++;
1708 }
1709
1710 *ptr_sgl = cpu_to_le64(sge_addr);
1711 ptr_sgl++;
Arnd Bergmannd1da5222017-02-14 22:21:51 +01001712 ptr_sgl_phys += 8;
Shivasharan S33203bc2017-02-10 00:59:12 -08001713 num_prp_in_chain++;
1714
1715 sge_addr += mr_nvme_pg_size;
1716 sge_len -= mr_nvme_pg_size;
1717 data_len -= mr_nvme_pg_size;
1718
1719 if (data_len <= 0)
1720 break;
1721
1722 if (sge_len > 0)
1723 continue;
1724
1725 sg_scmd = sg_next(sg_scmd);
1726 sge_addr = sg_dma_address(sg_scmd);
1727 sge_len = sg_dma_len(sg_scmd);
1728 }
1729
1730 main_chain_element->Length =
1731 cpu_to_le32(num_prp_in_chain * sizeof(u64));
1732
1733 atomic_inc(&instance->prp_sgl);
1734 return build_prp;
1735}
1736
1737/**
adam radford9c915a82010-12-21 13:34:31 -08001738 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1739 * @instance: Adapter soft state
1740 * @scp: SCSI command from the mid-layer
1741 * @sgl_ptr: SGL to be filled in
1742 * @cmd: cmd we are working on
Shivasharan S33203bc2017-02-10 00:59:12 -08001743 * @sge_count sge count
adam radford9c915a82010-12-21 13:34:31 -08001744 *
adam radford9c915a82010-12-21 13:34:31 -08001745 */
Shivasharan S33203bc2017-02-10 00:59:12 -08001746static void
adam radford9c915a82010-12-21 13:34:31 -08001747megasas_make_sgl_fusion(struct megasas_instance *instance,
1748 struct scsi_cmnd *scp,
1749 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
Shivasharan S33203bc2017-02-10 00:59:12 -08001750 struct megasas_cmd_fusion *cmd, int sge_count)
adam radford9c915a82010-12-21 13:34:31 -08001751{
Shivasharan S33203bc2017-02-10 00:59:12 -08001752 int i, sg_processed;
adam radford9c915a82010-12-21 13:34:31 -08001753 struct scatterlist *os_sgl;
1754 struct fusion_context *fusion;
1755
1756 fusion = instance->ctrl_context;
1757
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05001758 if (fusion->adapter_type >= INVADER_SERIES) {
adam radford36807e62011-10-08 18:15:06 -07001759 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1760 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1761 sgl_ptr_end->Flags = 0;
1762 }
adam radford9c915a82010-12-21 13:34:31 -08001763
adam radford9c915a82010-12-21 13:34:31 -08001764 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301765 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
1766 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
adam radford9c915a82010-12-21 13:34:31 -08001767 sgl_ptr->Flags = 0;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05001768 if (fusion->adapter_type >= INVADER_SERIES)
adam radford36807e62011-10-08 18:15:06 -07001769 if (i == sge_count - 1)
1770 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
adam radford9c915a82010-12-21 13:34:31 -08001771 sgl_ptr++;
adam radford9c915a82010-12-21 13:34:31 -08001772 sg_processed = i + 1;
1773
1774 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1775 (sge_count > fusion->max_sge_in_main_msg)) {
1776
1777 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05001778 if (fusion->adapter_type >= INVADER_SERIES) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301779 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1780 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1781 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
adam radford36807e62011-10-08 18:15:06 -07001782 cmd->io_request->ChainOffset =
1783 fusion->
1784 chain_offset_io_request;
1785 else
1786 cmd->io_request->ChainOffset = 0;
1787 } else
1788 cmd->io_request->ChainOffset =
1789 fusion->chain_offset_io_request;
1790
adam radford9c915a82010-12-21 13:34:31 -08001791 sg_chain = sgl_ptr;
1792 /* Prepare chain element */
1793 sg_chain->NextChainOffset = 0;
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05001794 if (fusion->adapter_type >= INVADER_SERIES)
adam radford36807e62011-10-08 18:15:06 -07001795 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1796 else
1797 sg_chain->Flags =
1798 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1799 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301800 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
1801 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08001802
1803 sgl_ptr =
1804 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301805 memset(sgl_ptr, 0, instance->max_chain_frame_sz);
adam radford9c915a82010-12-21 13:34:31 -08001806 }
1807 }
Shivasharan S33203bc2017-02-10 00:59:12 -08001808 atomic_inc(&instance->ieee_sgl);
1809}
1810
1811/**
1812 * megasas_make_sgl - Build Scatter Gather List(SGLs)
1813 * @scp: SCSI command pointer
1814 * @instance: Soft instance of controller
1815 * @cmd: Fusion command pointer
1816 *
1817 * This function will build sgls based on device type.
1818 * For nvme drives, there is different way of building sgls in nvme native
1819 * format- PRPs(Physical Region Page).
1820 *
1821 * Returns the number of sg lists actually used, zero if the sg lists
1822 * is NULL, or -ENOMEM if the mapping failed
1823 */
1824static
1825int megasas_make_sgl(struct megasas_instance *instance, struct scsi_cmnd *scp,
1826 struct megasas_cmd_fusion *cmd)
1827{
1828 int sge_count;
1829 bool build_prp = false;
1830 struct MPI25_IEEE_SGE_CHAIN64 *sgl_chain64;
1831
1832 sge_count = scsi_dma_map(scp);
1833
1834 if ((sge_count > instance->max_num_sge) || (sge_count <= 0))
1835 return sge_count;
1836
1837 sgl_chain64 = (struct MPI25_IEEE_SGE_CHAIN64 *)&cmd->io_request->SGL;
1838 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1839 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) &&
1840 (cmd->pd_interface == NVME_PD))
1841 build_prp = megasas_make_prp_nvme(instance, scp, sgl_chain64,
1842 cmd, sge_count);
1843
1844 if (!build_prp)
1845 megasas_make_sgl_fusion(instance, scp, sgl_chain64,
1846 cmd, sge_count);
adam radford9c915a82010-12-21 13:34:31 -08001847
1848 return sge_count;
1849}
1850
1851/**
1852 * megasas_set_pd_lba - Sets PD LBA
1853 * @cdb: CDB
1854 * @cdb_len: cdb length
1855 * @start_blk: Start block of IO
1856 *
1857 * Used to set the PD LBA in CDB for FP IOs
1858 */
1859void
1860megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1861 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301862 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
adam radford9c915a82010-12-21 13:34:31 -08001863{
1864 struct MR_LD_RAID *raid;
Shivasharan Sd2d03582017-02-10 00:59:19 -08001865 u16 ld;
adam radford9c915a82010-12-21 13:34:31 -08001866 u64 start_blk = io_info->pdBlock;
1867 u8 *cdb = io_request->CDB.CDB32;
1868 u32 num_blocks = io_info->numBlocks;
adam radford495c5602011-05-11 18:34:40 -07001869 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
adam radford9c915a82010-12-21 13:34:31 -08001870
1871 /* Check if T10 PI (DIF) is enabled for this LD */
1872 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1873 raid = MR_LdRaidGet(ld, local_map_ptr);
1874 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1875 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1876 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1877 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1878
1879 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1880 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1881 else
1882 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1883 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1884
1885 /* LBA */
1886 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1887 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1888 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1889 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1890 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1891 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1892 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1893 cdb[19] = (u8)(start_blk & 0xff);
1894
1895 /* Logical block reference tag */
1896 io_request->CDB.EEDP32.PrimaryReferenceTag =
1897 cpu_to_be32(ref_tag);
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301898 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301899 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001900
1901 /* Transfer length */
1902 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1903 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1904 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1905 cdb[31] = (u8)(num_blocks & 0xff);
1906
1907 /* set SCSI IO EEDPFlags */
1908 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301909 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001910 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1911 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1912 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1913 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
Sasikumar Chandrasekaran45d44602017-01-10 18:20:45 -05001914 MPI25_SCSIIO_EEDPFLAGS_DO_NOT_DISABLE_MODE |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301915 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
adam radford9c915a82010-12-21 13:34:31 -08001916 } else {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301917 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001918 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301919 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
adam radford9c915a82010-12-21 13:34:31 -08001920 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301921 io_request->Control |= cpu_to_le32((0x4 << 26));
1922 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
adam radford9c915a82010-12-21 13:34:31 -08001923 } else {
1924 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1925 if (((cdb_len == 12) || (cdb_len == 16)) &&
1926 (start_blk <= 0xffffffff)) {
1927 if (cdb_len == 16) {
1928 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1929 flagvals = cdb[1];
1930 groupnum = cdb[14];
1931 control = cdb[15];
1932 } else {
1933 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1934 flagvals = cdb[1];
1935 groupnum = cdb[10];
1936 control = cdb[11];
1937 }
1938
1939 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1940
1941 cdb[0] = opcode;
1942 cdb[1] = flagvals;
1943 cdb[6] = groupnum;
1944 cdb[9] = control;
1945
1946 /* Transfer length */
1947 cdb[8] = (u8)(num_blocks & 0xff);
1948 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1949
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301950 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001951 cdb_len = 10;
adam radford495c5602011-05-11 18:34:40 -07001952 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1953 /* Convert to 16 byte CDB for large LBA's */
1954 switch (cdb_len) {
1955 case 6:
1956 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1957 control = cdb[5];
1958 break;
1959 case 10:
1960 opcode =
1961 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1962 flagvals = cdb[1];
1963 groupnum = cdb[6];
1964 control = cdb[9];
1965 break;
1966 case 12:
1967 opcode =
1968 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1969 flagvals = cdb[1];
1970 groupnum = cdb[10];
1971 control = cdb[11];
1972 break;
1973 }
1974
1975 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1976
1977 cdb[0] = opcode;
1978 cdb[1] = flagvals;
1979 cdb[14] = groupnum;
1980 cdb[15] = control;
1981
1982 /* Transfer length */
1983 cdb[13] = (u8)(num_blocks & 0xff);
1984 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1985 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1986 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1987
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301988 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
adam radford495c5602011-05-11 18:34:40 -07001989 cdb_len = 16;
adam radford9c915a82010-12-21 13:34:31 -08001990 }
1991
1992 /* Normal case, just load LBA here */
1993 switch (cdb_len) {
1994 case 6:
1995 {
1996 u8 val = cdb[1] & 0xE0;
1997 cdb[3] = (u8)(start_blk & 0xff);
1998 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1999 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
2000 break;
2001 }
2002 case 10:
2003 cdb[5] = (u8)(start_blk & 0xff);
2004 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2005 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2006 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2007 break;
2008 case 12:
2009 cdb[5] = (u8)(start_blk & 0xff);
2010 cdb[4] = (u8)((start_blk >> 8) & 0xff);
2011 cdb[3] = (u8)((start_blk >> 16) & 0xff);
2012 cdb[2] = (u8)((start_blk >> 24) & 0xff);
2013 break;
2014 case 16:
2015 cdb[9] = (u8)(start_blk & 0xff);
2016 cdb[8] = (u8)((start_blk >> 8) & 0xff);
2017 cdb[7] = (u8)((start_blk >> 16) & 0xff);
2018 cdb[6] = (u8)((start_blk >> 24) & 0xff);
2019 cdb[5] = (u8)((start_blk >> 32) & 0xff);
2020 cdb[4] = (u8)((start_blk >> 40) & 0xff);
2021 cdb[3] = (u8)((start_blk >> 48) & 0xff);
2022 cdb[2] = (u8)((start_blk >> 56) & 0xff);
2023 break;
2024 }
2025 }
2026}
2027
2028/**
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002029 * megasas_stream_detect - stream detection on read and and write IOs
2030 * @instance: Adapter soft state
2031 * @cmd: Command to be prepared
2032 * @io_info: IO Request info
2033 *
2034 */
2035
2036/** stream detection on read and and write IOs */
2037static void megasas_stream_detect(struct megasas_instance *instance,
Shivasharan S41064f12017-02-10 00:59:37 -08002038 struct megasas_cmd_fusion *cmd,
2039 struct IO_REQUEST_INFO *io_info)
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002040{
2041 struct fusion_context *fusion = instance->ctrl_context;
2042 u32 device_id = io_info->ldTgtId;
2043 struct LD_STREAM_DETECT *current_ld_sd
2044 = fusion->stream_detect_by_ld[device_id];
2045 u32 *track_stream = &current_ld_sd->mru_bit_map, stream_num;
2046 u32 shifted_values, unshifted_values;
2047 u32 index_value_mask, shifted_values_mask;
2048 int i;
2049 bool is_read_ahead = false;
2050 struct STREAM_DETECT *current_sd;
2051 /* find possible stream */
2052 for (i = 0; i < MAX_STREAMS_TRACKED; ++i) {
Shivasharan S41064f12017-02-10 00:59:37 -08002053 stream_num = (*track_stream >>
2054 (i * BITS_PER_INDEX_STREAM)) &
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002055 STREAM_MASK;
2056 current_sd = &current_ld_sd->stream_track[stream_num];
Shivasharan S41064f12017-02-10 00:59:37 -08002057 /* if we found a stream, update the raid
2058 * context and also update the mruBitMap
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002059 */
Shivasharan S41064f12017-02-10 00:59:37 -08002060 /* boundary condition */
2061 if ((current_sd->next_seq_lba) &&
2062 (io_info->ldStartBlock >= current_sd->next_seq_lba) &&
2063 (io_info->ldStartBlock <= (current_sd->next_seq_lba + 32)) &&
2064 (current_sd->is_read == io_info->isRead)) {
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002065
Shivasharan S41064f12017-02-10 00:59:37 -08002066 if ((io_info->ldStartBlock != current_sd->next_seq_lba) &&
2067 ((!io_info->isRead) || (!is_read_ahead)))
2068 /*
2069 * Once the API availible we need to change this.
2070 * At this point we are not allowing any gap
2071 */
2072 continue;
2073
2074 SET_STREAM_DETECTED(cmd->io_request->RaidContext.raid_context_g35);
2075 current_sd->next_seq_lba =
2076 io_info->ldStartBlock + io_info->numBlocks;
2077 /*
2078 * update the mruBitMap LRU
2079 */
2080 shifted_values_mask =
2081 (1 << i * BITS_PER_INDEX_STREAM) - 1;
2082 shifted_values = ((*track_stream & shifted_values_mask)
2083 << BITS_PER_INDEX_STREAM);
2084 index_value_mask =
2085 STREAM_MASK << i * BITS_PER_INDEX_STREAM;
2086 unshifted_values =
2087 *track_stream & ~(shifted_values_mask |
2088 index_value_mask);
2089 *track_stream =
2090 unshifted_values | shifted_values | stream_num;
2091 return;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002092 }
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002093 }
2094 /*
2095 * if we did not find any stream, create a new one
2096 * from the least recently used
2097 */
Shivasharan S41064f12017-02-10 00:59:37 -08002098 stream_num = (*track_stream >>
2099 ((MAX_STREAMS_TRACKED - 1) * BITS_PER_INDEX_STREAM)) &
2100 STREAM_MASK;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002101 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;
Shivasharan S41064f12017-02-10 00:59:37 -08002104 *track_stream = (((*track_stream & ZERO_LAST_STREAM) << 4) | stream_num);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002105 return;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002106}
2107
2108/**
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002109 * megasas_set_raidflag_cpu_affinity - This function sets the cpu
2110 * affinity (cpu of the controller) and raid_flags in the raid context
2111 * based on IO type.
2112 *
2113 * @praid_context: IO RAID context
2114 * @raid: LD raid map
2115 * @fp_possible: Is fast path possible?
2116 * @is_read: Is read IO?
2117 *
2118 */
2119static void
2120megasas_set_raidflag_cpu_affinity(union RAID_CONTEXT_UNION *praid_context,
2121 struct MR_LD_RAID *raid, bool fp_possible,
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002122 u8 is_read, u32 scsi_buff_len)
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002123{
2124 u8 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2125 struct RAID_CONTEXT_G35 *rctx_g35;
2126
2127 rctx_g35 = &praid_context->raid_context_g35;
2128 if (fp_possible) {
2129 if (is_read) {
2130 if ((raid->cpuAffinity.pdRead.cpu0) &&
2131 (raid->cpuAffinity.pdRead.cpu1))
2132 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2133 else if (raid->cpuAffinity.pdRead.cpu1)
2134 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2135 } else {
2136 if ((raid->cpuAffinity.pdWrite.cpu0) &&
2137 (raid->cpuAffinity.pdWrite.cpu1))
2138 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2139 else if (raid->cpuAffinity.pdWrite.cpu1)
2140 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2141 /* Fast path cache by pass capable R0/R1 VD */
2142 if ((raid->level <= 1) &&
2143 (raid->capability.fp_cache_bypass_capable)) {
Shivasharan Sa174118b2017-02-10 00:59:21 -08002144 rctx_g35->routing_flags |=
2145 (1 << MR_RAID_CTX_ROUTINGFLAGS_SLD_SHIFT);
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002146 rctx_g35->raid_flags =
2147 (MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS
2148 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT);
2149 }
2150 }
2151 } else {
2152 if (is_read) {
2153 if ((raid->cpuAffinity.ldRead.cpu0) &&
2154 (raid->cpuAffinity.ldRead.cpu1))
2155 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2156 else if (raid->cpuAffinity.ldRead.cpu1)
2157 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2158 } else {
2159 if ((raid->cpuAffinity.ldWrite.cpu0) &&
2160 (raid->cpuAffinity.ldWrite.cpu1))
2161 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS;
2162 else if (raid->cpuAffinity.ldWrite.cpu1)
2163 cpu_sel = MR_RAID_CTX_CPUSEL_1;
2164
Shivasharan Sa174118b2017-02-10 00:59:21 -08002165 if (is_stream_detected(rctx_g35) &&
Shivasharan S874d0252017-03-10 03:22:14 -08002166 ((raid->level == 5) || (raid->level == 6)) &&
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002167 (raid->writeMode == MR_RL_WRITE_THROUGH_MODE) &&
2168 (cpu_sel == MR_RAID_CTX_CPUSEL_FCFS))
2169 cpu_sel = MR_RAID_CTX_CPUSEL_0;
2170 }
2171 }
2172
Shivasharan Sa174118b2017-02-10 00:59:21 -08002173 rctx_g35->routing_flags |=
2174 (cpu_sel << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
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);
adam radford9c915a82010-12-21 13:34:31 -08002296
Shivasharan Sd2d03582017-02-10 00:59:19 -08002297 if (ld < instance->fw_supported_vd_count)
2298 raid = MR_LdRaidGet(ld, local_map_ptr);
2299
2300 if (!raid || (!fusion->fast_path_io)) {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002301 io_request->RaidContext.raid_context.reg_lock_flags = 0;
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002302 fp_possible = false;
adam radford9c915a82010-12-21 13:34:31 -08002303 } else {
adam radford36807e62011-10-08 18:15:06 -07002304 if (MR_BuildRaidContext(instance, &io_info,
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002305 &io_request->RaidContext.raid_context,
adam radford21c9e162013-09-06 15:27:14 -07002306 local_map_ptr, &raidLUN))
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002307 fp_possible = (io_info.fpOkForIo > 0) ? true : false;
adam radford9c915a82010-12-21 13:34:31 -08002308 }
2309
Christoph Hellwig16b85282015-04-15 09:44:37 -07002310 /* Use raw_smp_processor_id() for now until cmd->request->cpu is CPU
adam radfordc8e858f2011-10-08 18:15:13 -07002311 id by default, not CPU group id, otherwise all MSI-X queues won't
2312 be utilized */
2313 cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
Christoph Hellwig16b85282015-04-15 09:44:37 -07002314 raw_smp_processor_id() % instance->msix_vectors : 0;
adam radfordc8e858f2011-10-08 18:15:13 -07002315
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002316 praid_context = &io_request->RaidContext;
2317
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002318 if (instance->is_ventura) {
2319 spin_lock_irqsave(&instance->stream_lock, spinlock_flags);
2320 megasas_stream_detect(instance, cmd, &io_info);
2321 spin_unlock_irqrestore(&instance->stream_lock, spinlock_flags);
2322 /* In ventura if stream detected for a read and it is read ahead
2323 * capable make this IO as LDIO
2324 */
Shivasharan Sa174118b2017-02-10 00:59:21 -08002325 if (is_stream_detected(&io_request->RaidContext.raid_context_g35) &&
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002326 io_info.isRead && io_info.ra_capable)
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002327 fp_possible = false;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002328
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002329 /* FP for Optimal raid level 1.
2330 * All large RAID-1 writes (> 32 KiB, both WT and WB modes)
2331 * are built by the driver as LD I/Os.
2332 * All small RAID-1 WT writes (<= 32 KiB) are built as FP I/Os
2333 * (there is never a reason to process these as buffered writes)
2334 * All small RAID-1 WB writes (<= 32 KiB) are built as FP I/Os
2335 * with the SLD bit asserted.
2336 */
Shivasharan S8bf7c652017-02-10 00:59:03 -08002337 if (io_info.r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002338 mrdev_priv = scp->device->hostdata;
2339
2340 if (atomic_inc_return(&instance->fw_outstanding) >
2341 (instance->host->can_queue)) {
2342 fp_possible = false;
2343 atomic_dec(&instance->fw_outstanding);
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002344 } else if ((scsi_buff_len > MR_LARGE_IO_MIN_SIZE) ||
Shivasharan S49524b32017-03-10 03:22:13 -08002345 (atomic_dec_if_positive(&mrdev_priv->r1_ldio_hint) > 0)) {
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002346 fp_possible = false;
2347 atomic_dec(&instance->fw_outstanding);
2348 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE)
2349 atomic_set(&mrdev_priv->r1_ldio_hint,
2350 instance->r1_ldio_hint_default);
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002351 }
2352 }
2353
2354 /* If raid is NULL, set CPU affinity to default CPU0 */
2355 if (raid)
2356 megasas_set_raidflag_cpu_affinity(praid_context,
Shivasharan Sa48ba0e2017-02-10 00:59:13 -08002357 raid, fp_possible, io_info.isRead,
2358 scsi_buff_len);
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002359 else
Shivasharan Sa174118b2017-02-10 00:59:21 -08002360 praid_context->raid_context_g35.routing_flags |=
2361 (MR_RAID_CTX_CPUSEL_0 << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT);
Shivasharan Sf6c0d552017-02-10 00:59:02 -08002362 }
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002363
adam radford9c915a82010-12-21 13:34:31 -08002364 if (fp_possible) {
2365 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
2366 local_map_ptr, start_lba_lo);
adam radford9c915a82010-12-21 13:34:31 -08002367 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2368 cmd->request_desc->SCSIIO.RequestFlags =
Sumit Saxena2c048352016-01-28 21:04:24 +05302369 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO
adam radford9c915a82010-12-21 13:34:31 -08002370 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05302371 if (fusion->adapter_type == INVADER_SERIES) {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002372 if (io_request->RaidContext.raid_context.reg_lock_flags ==
adam radford36807e62011-10-08 18:15:06 -07002373 REGION_TYPE_UNUSED)
2374 cmd->request_desc->SCSIIO.RequestFlags =
2375 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2376 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002377 io_request->RaidContext.raid_context.type
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002378 = MPI2_TYPE_CUDA;
2379 io_request->RaidContext.raid_context.nseg = 0x1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302380 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002381 io_request->RaidContext.raid_context.reg_lock_flags |=
adam radford36807e62011-10-08 18:15:06 -07002382 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
2383 MR_RL_FLAGS_SEQ_NUM_ENABLE);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002384 } else if (instance->is_ventura) {
Shivasharan Sa174118b2017-02-10 00:59:21 -08002385 io_request->RaidContext.raid_context_g35.nseg_type |=
2386 (1 << RAID_CONTEXT_NSEG_SHIFT);
2387 io_request->RaidContext.raid_context_g35.nseg_type |=
2388 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2389 io_request->RaidContext.raid_context_g35.routing_flags |=
2390 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002391 io_request->IoFlags |=
Shivasharan Sa174118b2017-02-10 00:59:21 -08002392 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
adam radford36807e62011-10-08 18:15:06 -07002393 }
Shivasharan S5fc499b2017-02-10 00:59:17 -08002394 if (fusion->load_balance_info &&
2395 (fusion->load_balance_info[device_id].loadBalanceFlag) &&
2396 (io_info.isRead)) {
adam radford9c915a82010-12-21 13:34:31 -08002397 io_info.devHandle =
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05302398 get_updated_dev_handle(instance,
adam radford9c915a82010-12-21 13:34:31 -08002399 &fusion->load_balance_info[device_id],
Shivasharan S33203bc2017-02-10 00:59:12 -08002400 &io_info, local_map_ptr);
adam radford9c915a82010-12-21 13:34:31 -08002401 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05302402 cmd->pd_r1_lb = io_info.pd_after_lb;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002403 if (instance->is_ventura)
2404 io_request->RaidContext.raid_context_g35.span_arm
2405 = io_info.span_arm;
2406 else
2407 io_request->RaidContext.raid_context.span_arm
2408 = io_info.span_arm;
2409
adam radford9c915a82010-12-21 13:34:31 -08002410 } else
2411 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05302412
Shivasharan S8bf7c652017-02-10 00:59:03 -08002413 if (instance->is_ventura)
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002414 cmd->r1_alt_dev_handle = io_info.r1_alt_dev_handle;
Shivasharan S8bf7c652017-02-10 00:59:03 -08002415 else
2416 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002417
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05302418 if ((raidLUN[0] == 1) &&
sumit.saxena@avagotech.com75b96061e2015-08-31 17:24:01 +05302419 (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) {
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05302420 instance->dev_handle = !(instance->dev_handle);
2421 io_info.devHandle =
2422 local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
2423 }
2424
adam radford9c915a82010-12-21 13:34:31 -08002425 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
2426 io_request->DevHandle = io_info.devHandle;
Shivasharan S33203bc2017-02-10 00:59:12 -08002427 cmd->pd_interface = io_info.pd_interface;
adam radford21c9e162013-09-06 15:27:14 -07002428 /* populate the LUN field */
2429 memcpy(io_request->LUN, raidLUN, 8);
adam radford9c915a82010-12-21 13:34:31 -08002430 } else {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002431 io_request->RaidContext.raid_context.timeout_value =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302432 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
adam radford9c915a82010-12-21 13:34:31 -08002433 cmd->request_desc->SCSIIO.RequestFlags =
2434 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
2435 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05302436 if (fusion->adapter_type == INVADER_SERIES) {
Sumit Saxena8f050242016-01-28 21:04:27 +05302437 if (io_info.do_fp_rlbypass ||
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002438 (io_request->RaidContext.raid_context.reg_lock_flags
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002439 == REGION_TYPE_UNUSED))
adam radford36807e62011-10-08 18:15:06 -07002440 cmd->request_desc->SCSIIO.RequestFlags =
2441 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
2442 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002443 io_request->RaidContext.raid_context.type
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002444 = MPI2_TYPE_CUDA;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002445 io_request->RaidContext.raid_context.reg_lock_flags |=
adam radford36807e62011-10-08 18:15:06 -07002446 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
2447 MR_RL_FLAGS_SEQ_NUM_ENABLE);
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002448 io_request->RaidContext.raid_context.nseg = 0x1;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002449 } else if (instance->is_ventura) {
Shivasharan Sa174118b2017-02-10 00:59:21 -08002450 io_request->RaidContext.raid_context_g35.routing_flags |=
2451 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2452 io_request->RaidContext.raid_context_g35.nseg_type |=
2453 (1 << RAID_CONTEXT_NSEG_SHIFT);
2454 io_request->RaidContext.raid_context_g35.nseg_type |=
2455 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
adam radford36807e62011-10-08 18:15:06 -07002456 }
2457 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302458 io_request->DevHandle = cpu_to_le16(device_id);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002459
adam radford9c915a82010-12-21 13:34:31 -08002460 } /* Not FP */
2461}
2462
2463/**
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302464 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
adam radford9c915a82010-12-21 13:34:31 -08002465 * @instance: Adapter soft state
2466 * @scp: SCSI command
2467 * @cmd: Command to be prepared
2468 *
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302469 * Prepares the io_request frame for non-rw io cmds for vd.
adam radford9c915a82010-12-21 13:34:31 -08002470 */
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302471static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
2472 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
adam radford9c915a82010-12-21 13:34:31 -08002473{
2474 u32 device_id;
2475 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
Shivasharan S25fb13d2017-02-10 00:59:29 -08002476 u16 ld;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302477 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
adam radford9c915a82010-12-21 13:34:31 -08002478 struct fusion_context *fusion = instance->ctrl_context;
adam radford21c9e162013-09-06 15:27:14 -07002479 u8 span, physArm;
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05302480 __le16 devHandle;
Shivasharan Sd2d03582017-02-10 00:59:19 -08002481 u32 arRef, pd;
adam radford21c9e162013-09-06 15:27:14 -07002482 struct MR_LD_RAID *raid;
2483 struct RAID_CONTEXT *pRAID_Context;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302484 u8 fp_possible = 1;
adam radford9c915a82010-12-21 13:34:31 -08002485
2486 io_request = cmd->io_request;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302487 device_id = MEGASAS_DEV_INDEX(scmd);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302488 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302489 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302490 /* get RAID_Context pointer */
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002491 pRAID_Context = &io_request->RaidContext.raid_context;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302492 /* Check with FW team */
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002493 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2494 pRAID_Context->reg_lock_row_lba = 0;
2495 pRAID_Context->reg_lock_length = 0;
adam radford21c9e162013-09-06 15:27:14 -07002496
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302497 if (fusion->fast_path_io && (
2498 device_id < instance->fw_supported_vd_count)) {
Sumit.Saxena@avagotech.comab2f0602015-01-05 20:06:08 +05302499
adam radford21c9e162013-09-06 15:27:14 -07002500 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
Sumit.Saxena@avagotech.comab2f0602015-01-05 20:06:08 +05302501 if (ld >= instance->fw_supported_vd_count)
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302502 fp_possible = 0;
Shivasharan Sd2d03582017-02-10 00:59:19 -08002503 else {
2504 raid = MR_LdRaidGet(ld, local_map_ptr);
2505 if (!(raid->capability.fpNonRWCapable))
2506 fp_possible = 0;
2507 }
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302508 } else
2509 fp_possible = 0;
adam radford21c9e162013-09-06 15:27:14 -07002510
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302511 if (!fp_possible) {
2512 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
2513 io_request->DevHandle = cpu_to_le16(device_id);
2514 io_request->LUN[1] = scmd->device->lun;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002515 pRAID_Context->timeout_value =
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302516 cpu_to_le16 (scmd->request->timeout / HZ);
2517 cmd->request_desc->SCSIIO.RequestFlags =
2518 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2519 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2520 } else {
adam radford21c9e162013-09-06 15:27:14 -07002521
2522 /* set RAID context values */
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002523 pRAID_Context->config_seq_num = raid->seqNum;
2524 if (!instance->is_ventura)
2525 pRAID_Context->reg_lock_flags = REGION_TYPE_SHARED_READ;
2526 pRAID_Context->timeout_value =
2527 cpu_to_le16(raid->fpIoTimeoutForLd);
adam radford21c9e162013-09-06 15:27:14 -07002528
2529 /* get the DevHandle for the PD (since this is
2530 fpNonRWCapable, this is a single disk RAID0) */
2531 span = physArm = 0;
2532 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
2533 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
2534 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
2535
2536 /* build request descriptor */
2537 cmd->request_desc->SCSIIO.RequestFlags =
Sumit Saxena2c048352016-01-28 21:04:24 +05302538 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302539 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford21c9e162013-09-06 15:27:14 -07002540 cmd->request_desc->SCSIIO.DevHandle = devHandle;
2541
2542 /* populate the LUN field */
2543 memcpy(io_request->LUN, raid->LUN, 8);
2544
2545 /* build the raidScsiIO structure */
2546 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
2547 io_request->DevHandle = devHandle;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302548 }
2549}
adam radford21c9e162013-09-06 15:27:14 -07002550
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302551/**
2552 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
2553 * @instance: Adapter soft state
2554 * @scp: SCSI command
2555 * @cmd: Command to be prepared
2556 * @fp_possible: parameter to detect fast path or firmware path io.
2557 *
2558 * Prepares the io_request frame for rw/non-rw io cmds for syspds
2559 */
2560static void
2561megasas_build_syspd_fusion(struct megasas_instance *instance,
Shivasharan S1d6dbd12017-02-10 00:59:30 -08002562 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd,
2563 bool fp_possible)
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302564{
2565 u32 device_id;
2566 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
2567 u16 pd_index = 0;
2568 u16 os_timeout_value;
2569 u16 timeout_limit;
2570 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
2571 struct RAID_CONTEXT *pRAID_Context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302572 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
Shivasharan S33203bc2017-02-10 00:59:12 -08002573 struct MR_PRIV_DEVICE *mr_device_priv_data;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302574 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302575 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
adam radford21c9e162013-09-06 15:27:14 -07002576
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302577 device_id = MEGASAS_DEV_INDEX(scmd);
2578 pd_index = MEGASAS_PD_INDEX(scmd);
2579 os_timeout_value = scmd->request->timeout / HZ;
Shivasharan S33203bc2017-02-10 00:59:12 -08002580 mr_device_priv_data = scmd->device->hostdata;
2581 cmd->pd_interface = mr_device_priv_data->interface_type;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302582
2583 io_request = cmd->io_request;
2584 /* get RAID_Context pointer */
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002585 pRAID_Context = &io_request->RaidContext.raid_context;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002586 pRAID_Context->reg_lock_flags = 0;
2587 pRAID_Context->reg_lock_row_lba = 0;
2588 pRAID_Context->reg_lock_length = 0;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302589 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
2590 io_request->LUN[1] = scmd->device->lun;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002591 pRAID_Context->raid_flags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302592 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
2593
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302594 /* If FW supports PD sequence number */
2595 if (instance->use_seqnum_jbod_fp &&
2596 instance->pd_list[pd_index].driveType == TYPE_DISK) {
2597 /* TgtId must be incremented by 255 as jbod seq number is index
2598 * below raid map
2599 */
Sasikumar Chandrasekaranede7c3c2017-01-10 18:20:52 -05002600 /* More than 256 PD/JBOD support for Ventura */
2601 if (instance->support_morethan256jbod)
2602 pRAID_Context->virtual_disk_tgt_id =
2603 pd_sync->seq[pd_index].pd_target_id;
2604 else
Shivasharan Sa174118b2017-02-10 00:59:21 -08002605 pRAID_Context->virtual_disk_tgt_id =
2606 cpu_to_le16(device_id + (MAX_PHYSICAL_DEVICES - 1));
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002607 pRAID_Context->config_seq_num = pd_sync->seq[pd_index].seqNum;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302608 io_request->DevHandle = pd_sync->seq[pd_index].devHandle;
Shivasharan Sa174118b2017-02-10 00:59:21 -08002609 if (instance->is_ventura) {
2610 io_request->RaidContext.raid_context_g35.routing_flags |=
2611 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT);
2612 io_request->RaidContext.raid_context_g35.nseg_type |=
2613 (1 << RAID_CONTEXT_NSEG_SHIFT);
2614 io_request->RaidContext.raid_context_g35.nseg_type |=
2615 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT);
2616 } else {
2617 pRAID_Context->type = MPI2_TYPE_CUDA;
2618 pRAID_Context->nseg = 0x1;
2619 pRAID_Context->reg_lock_flags |=
2620 (MR_RL_FLAGS_SEQ_NUM_ENABLE|MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
2621 }
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302622 } else if (fusion->fast_path_io) {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002623 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2624 pRAID_Context->config_seq_num = 0;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302625 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
2626 io_request->DevHandle =
2627 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
2628 } else {
2629 /* Want to send all IO via FW path */
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002630 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
2631 pRAID_Context->config_seq_num = 0;
Sumit Saxena9fb74c42015-11-05 21:17:38 +05302632 io_request->DevHandle = cpu_to_le16(0xFFFF);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302633 }
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302634
2635 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
2636 cmd->request_desc->SCSIIO.MSIxIndex =
2637 instance->msix_vectors ?
2638 (raw_smp_processor_id() % instance->msix_vectors) : 0;
2639
2640
2641 if (!fp_possible) {
2642 /* system pd firmware path */
adam radford9c915a82010-12-21 13:34:31 -08002643 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
adam radford9c915a82010-12-21 13:34:31 -08002644 cmd->request_desc->SCSIIO.RequestFlags =
2645 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302646 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002647 pRAID_Context->timeout_value = cpu_to_le16(os_timeout_value);
2648 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302649 } else {
2650 /* system pd Fast Path */
2651 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302652 timeout_limit = (scmd->device->type == TYPE_DISK) ?
2653 255 : 0xFFFF;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002654 pRAID_Context->timeout_value =
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302655 cpu_to_le16((os_timeout_value > timeout_limit) ?
2656 timeout_limit : os_timeout_value);
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05002657 if (fusion->adapter_type >= INVADER_SERIES)
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302658 io_request->IoFlags |=
2659 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
Kashyap Desaid5573582016-10-21 06:33:35 -07002660
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302661 cmd->request_desc->SCSIIO.RequestFlags =
Sumit Saxena2c048352016-01-28 21:04:24 +05302662 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO <<
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302663 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford9c915a82010-12-21 13:34:31 -08002664 }
adam radford9c915a82010-12-21 13:34:31 -08002665}
2666
2667/**
2668 * megasas_build_io_fusion - Prepares IOs to devices
2669 * @instance: Adapter soft state
2670 * @scp: SCSI command
2671 * @cmd: Command to be prepared
2672 *
2673 * Invokes helper functions to prepare request frames
2674 * and sets flags appropriate for IO/Non-IO cmd
2675 */
2676int
2677megasas_build_io_fusion(struct megasas_instance *instance,
2678 struct scsi_cmnd *scp,
2679 struct megasas_cmd_fusion *cmd)
2680{
Shivasharan S33203bc2017-02-10 00:59:12 -08002681 int sge_count;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302682 u8 cmd_type;
adam radford9c915a82010-12-21 13:34:31 -08002683 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
Shivasharan S1d6dbd12017-02-10 00:59:30 -08002684 struct MR_PRIV_DEVICE *mr_device_priv_data;
2685 mr_device_priv_data = scp->device->hostdata;
adam radford9c915a82010-12-21 13:34:31 -08002686
adam radford9c915a82010-12-21 13:34:31 -08002687 /* Zero out some fields so they don't get reused */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02002688 memset(io_request->LUN, 0x0, 8);
adam radford9c915a82010-12-21 13:34:31 -08002689 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
2690 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
2691 io_request->EEDPFlags = 0;
2692 io_request->Control = 0;
2693 io_request->EEDPBlockSize = 0;
adam radford36807e62011-10-08 18:15:06 -07002694 io_request->ChainOffset = 0;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002695 io_request->RaidContext.raid_context.raid_flags = 0;
2696 io_request->RaidContext.raid_context.type = 0;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05002697 io_request->RaidContext.raid_context.nseg = 0;
adam radford9c915a82010-12-21 13:34:31 -08002698
2699 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
2700 /*
2701 * Just the CDB length,rest of the Flags are zero
2702 * This will be modified for FP in build_ldio_fusion
2703 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302704 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
adam radford9c915a82010-12-21 13:34:31 -08002705
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302706 switch (cmd_type = megasas_cmd_type(scp)) {
2707 case READ_WRITE_LDIO:
adam radford9c915a82010-12-21 13:34:31 -08002708 megasas_build_ldio_fusion(instance, scp, cmd);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302709 break;
2710 case NON_READ_WRITE_LDIO:
2711 megasas_build_ld_nonrw_fusion(instance, scp, cmd);
2712 break;
2713 case READ_WRITE_SYSPDIO:
Shivasharan S1d6dbd12017-02-10 00:59:30 -08002714 megasas_build_syspd_fusion(instance, scp, cmd, true);
2715 break;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302716 case NON_READ_WRITE_SYSPDIO:
Shivasharan S1d6dbd12017-02-10 00:59:30 -08002717 if (instance->secure_jbod_support ||
2718 mr_device_priv_data->is_tm_capable)
2719 megasas_build_syspd_fusion(instance, scp, cmd, false);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302720 else
Shivasharan S1d6dbd12017-02-10 00:59:30 -08002721 megasas_build_syspd_fusion(instance, scp, cmd, true);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05302722 break;
2723 default:
2724 break;
2725 }
adam radford9c915a82010-12-21 13:34:31 -08002726
2727 /*
2728 * Construct SGL
2729 */
2730
Shivasharan S33203bc2017-02-10 00:59:12 -08002731 sge_count = megasas_make_sgl(instance, scp, cmd);
adam radford9c915a82010-12-21 13:34:31 -08002732
Shivasharan S33203bc2017-02-10 00:59:12 -08002733 if (sge_count > instance->max_num_sge || (sge_count < 0)) {
2734 dev_err(&instance->pdev->dev,
2735 "%s %d sge_count (%d) is out of range. Range is: 0-%d\n",
2736 __func__, __LINE__, sge_count, instance->max_num_sge);
adam radford9c915a82010-12-21 13:34:31 -08002737 return 1;
2738 }
2739
Shivasharan Sa174118b2017-02-10 00:59:21 -08002740 if (instance->is_ventura) {
2741 set_num_sge(&io_request->RaidContext.raid_context_g35, sge_count);
2742 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.routing_flags);
2743 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.nseg_type);
2744 } else {
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05002745 /* numSGE store lower 8 bit of sge_count.
2746 * numSGEExt store higher 8 bit of sge_count
2747 */
2748 io_request->RaidContext.raid_context.num_sge = sge_count;
2749 io_request->RaidContext.raid_context.num_sge_ext =
2750 (u8)(sge_count >> 8);
2751 }
adam radford9c915a82010-12-21 13:34:31 -08002752
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302753 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
adam radford9c915a82010-12-21 13:34:31 -08002754
2755 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302756 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
adam radford9c915a82010-12-21 13:34:31 -08002757 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302758 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
adam radford9c915a82010-12-21 13:34:31 -08002759
2760 io_request->SGLOffset0 =
2761 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
2762
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302763 io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08002764 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
2765
2766 cmd->scmd = scp;
2767 scp->SCp.ptr = (char *)cmd;
2768
2769 return 0;
2770}
2771
Shivasharan S52205ac2017-02-10 00:59:08 -08002772static union MEGASAS_REQUEST_DESCRIPTOR_UNION *
adam radford9c915a82010-12-21 13:34:31 -08002773megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
2774{
2775 u8 *p;
2776 struct fusion_context *fusion;
2777
adam radford9c915a82010-12-21 13:34:31 -08002778 fusion = instance->ctrl_context;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002779 p = fusion->req_frames_desc +
2780 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * index;
adam radford9c915a82010-12-21 13:34:31 -08002781
2782 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
2783}
2784
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002785
2786/* megasas_prepate_secondRaid1_IO
2787 * It prepares the raid 1 second IO
2788 */
2789void megasas_prepare_secondRaid1_IO(struct megasas_instance *instance,
2790 struct megasas_cmd_fusion *cmd,
2791 struct megasas_cmd_fusion *r1_cmd)
2792{
2793 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc, *req_desc2 = NULL;
2794 struct fusion_context *fusion;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002795 fusion = instance->ctrl_context;
2796 req_desc = cmd->request_desc;
Shivasharan S8bf7c652017-02-10 00:59:03 -08002797 /* copy the io request frame as well as 8 SGEs data for r1 command*/
2798 memcpy(r1_cmd->io_request, cmd->io_request,
2799 (sizeof(struct MPI2_RAID_SCSI_IO_REQUEST)));
2800 memcpy(&r1_cmd->io_request->SGL, &cmd->io_request->SGL,
2801 (fusion->max_sge_in_main_msg * sizeof(union MPI2_SGE_IO_UNION)));
2802 /*sense buffer is different for r1 command*/
2803 r1_cmd->io_request->SenseBufferLowAddress =
2804 cpu_to_le32(r1_cmd->sense_phys_addr);
2805 r1_cmd->scmd = cmd->scmd;
2806 req_desc2 = megasas_get_request_descriptor(instance,
2807 (r1_cmd->index - 1));
2808 req_desc2->Words = 0;
2809 r1_cmd->request_desc = req_desc2;
2810 req_desc2->SCSIIO.SMID = cpu_to_le16(r1_cmd->index);
2811 req_desc2->SCSIIO.RequestFlags = req_desc->SCSIIO.RequestFlags;
2812 r1_cmd->request_desc->SCSIIO.DevHandle = cmd->r1_alt_dev_handle;
2813 r1_cmd->io_request->DevHandle = cmd->r1_alt_dev_handle;
2814 r1_cmd->r1_alt_dev_handle = cmd->io_request->DevHandle;
2815 cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
2816 cpu_to_le16(r1_cmd->index);
2817 r1_cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid =
2818 cpu_to_le16(cmd->index);
2819 /*MSIxIndex of both commands request descriptors should be same*/
2820 r1_cmd->request_desc->SCSIIO.MSIxIndex =
2821 cmd->request_desc->SCSIIO.MSIxIndex;
2822 /*span arm is different for r1 cmd*/
2823 r1_cmd->io_request->RaidContext.raid_context_g35.span_arm =
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002824 cmd->io_request->RaidContext.raid_context_g35.span_arm + 1;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002825}
2826
adam radford9c915a82010-12-21 13:34:31 -08002827/**
2828 * megasas_build_and_issue_cmd_fusion -Main routine for building and
2829 * issuing non IOCTL cmd
2830 * @instance: Adapter soft state
2831 * @scmd: pointer to scsi cmd from OS
2832 */
2833static u32
2834megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
2835 struct scsi_cmnd *scmd)
2836{
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002837 struct megasas_cmd_fusion *cmd, *r1_cmd = NULL;
adam radford9c915a82010-12-21 13:34:31 -08002838 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2839 u32 index;
2840 struct fusion_context *fusion;
2841
2842 fusion = instance->ctrl_context;
2843
Sumit Saxena308ec452016-01-28 21:04:30 +05302844 if ((megasas_cmd_type(scmd) == READ_WRITE_LDIO) &&
2845 instance->ldio_threshold &&
2846 (atomic_inc_return(&instance->ldio_outstanding) >
2847 instance->ldio_threshold)) {
2848 atomic_dec(&instance->ldio_outstanding);
2849 return SCSI_MLQUEUE_DEVICE_BUSY;
2850 }
2851
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002852 if (atomic_inc_return(&instance->fw_outstanding) >
2853 instance->host->can_queue) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002854 atomic_dec(&instance->fw_outstanding);
2855 return SCSI_MLQUEUE_HOST_BUSY;
2856 }
2857
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302858 cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
adam radford9c915a82010-12-21 13:34:31 -08002859
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002860 if (!cmd) {
2861 atomic_dec(&instance->fw_outstanding);
2862 return SCSI_MLQUEUE_HOST_BUSY;
2863 }
2864
adam radford9c915a82010-12-21 13:34:31 -08002865 index = cmd->index;
2866
2867 req_desc = megasas_get_request_descriptor(instance, index-1);
adam radford9c915a82010-12-21 13:34:31 -08002868
2869 req_desc->Words = 0;
2870 cmd->request_desc = req_desc;
adam radford9c915a82010-12-21 13:34:31 -08002871
2872 if (megasas_build_io_fusion(instance, scmd, cmd)) {
2873 megasas_return_cmd_fusion(instance, cmd);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002874 dev_err(&instance->pdev->dev, "Error building command\n");
adam radford9c915a82010-12-21 13:34:31 -08002875 cmd->request_desc = NULL;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002876 atomic_dec(&instance->fw_outstanding);
Sumit Saxenaf9a9dee2016-01-28 21:04:29 +05302877 return SCSI_MLQUEUE_HOST_BUSY;
adam radford9c915a82010-12-21 13:34:31 -08002878 }
2879
2880 req_desc = cmd->request_desc;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302881 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08002882
2883 if (cmd->io_request->ChainOffset != 0 &&
2884 cmd->io_request->ChainOffset != 0xF)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002885 dev_err(&instance->pdev->dev, "The chain offset value is not "
adam radford9c915a82010-12-21 13:34:31 -08002886 "correct : %x\n", cmd->io_request->ChainOffset);
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002887 /*
2888 * if it is raid 1/10 fp write capable.
2889 * try to get second command from pool and construct it.
2890 * From FW, it has confirmed that lba values of two PDs
2891 * corresponds to single R1/10 LD are always same
2892 *
2893 */
2894 /* driver side count always should be less than max_fw_cmds
2895 * to get new command
2896 */
Shivasharan S8bf7c652017-02-10 00:59:03 -08002897 if (cmd->r1_alt_dev_handle != MR_DEVHANDLE_INVALID) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002898 r1_cmd = megasas_get_cmd_fusion(instance,
2899 (scmd->request->tag + instance->max_fw_cmds));
2900 megasas_prepare_secondRaid1_IO(instance, cmd, r1_cmd);
2901 }
2902
Shivasharan S18bbcab2017-02-10 00:59:01 -08002903
adam radford9c915a82010-12-21 13:34:31 -08002904 /*
2905 * Issue the command to the FW
2906 */
adam radford9c915a82010-12-21 13:34:31 -08002907
Shivasharan S45b8a352017-02-10 00:59:04 -08002908 megasas_fire_cmd_fusion(instance, req_desc);
adam radford9c915a82010-12-21 13:34:31 -08002909
Shivasharan S18bbcab2017-02-10 00:59:01 -08002910 if (r1_cmd)
Shivasharan S45b8a352017-02-10 00:59:04 -08002911 megasas_fire_cmd_fusion(instance, r1_cmd->request_desc);
Shivasharan S18bbcab2017-02-10 00:59:01 -08002912
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05002913
adam radford9c915a82010-12-21 13:34:31 -08002914 return 0;
2915}
2916
2917/**
Shivasharan S8bf7c652017-02-10 00:59:03 -08002918 * megasas_complete_r1_command -
2919 * completes R1 FP write commands which has valid peer smid
2920 * @instance: Adapter soft state
2921 * @cmd_fusion: MPT command frame
2922 *
2923 */
2924static inline void
2925megasas_complete_r1_command(struct megasas_instance *instance,
2926 struct megasas_cmd_fusion *cmd)
2927{
2928 u8 *sense, status, ex_status;
2929 u32 data_length;
2930 u16 peer_smid;
2931 struct fusion_context *fusion;
2932 struct megasas_cmd_fusion *r1_cmd = NULL;
2933 struct scsi_cmnd *scmd_local = NULL;
2934 struct RAID_CONTEXT_G35 *rctx_g35;
2935
2936 rctx_g35 = &cmd->io_request->RaidContext.raid_context_g35;
2937 fusion = instance->ctrl_context;
2938 peer_smid = le16_to_cpu(rctx_g35->smid.peer_smid);
2939
2940 r1_cmd = fusion->cmd_list[peer_smid - 1];
2941 scmd_local = cmd->scmd;
2942 status = rctx_g35->status;
2943 ex_status = rctx_g35->ex_status;
2944 data_length = cmd->io_request->DataLength;
2945 sense = cmd->sense;
2946
2947 cmd->cmd_completed = true;
2948
2949 /* Check if peer command is completed or not*/
2950 if (r1_cmd->cmd_completed) {
2951 rctx_g35 = &r1_cmd->io_request->RaidContext.raid_context_g35;
2952 if (rctx_g35->status != MFI_STAT_OK) {
2953 status = rctx_g35->status;
2954 ex_status = rctx_g35->ex_status;
2955 data_length = r1_cmd->io_request->DataLength;
2956 sense = r1_cmd->sense;
2957 }
2958
2959 megasas_return_cmd_fusion(instance, r1_cmd);
2960 map_cmd_status(fusion, scmd_local, status, ex_status,
2961 le32_to_cpu(data_length), sense);
2962 if (instance->ldio_threshold &&
2963 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
2964 atomic_dec(&instance->ldio_outstanding);
2965 scmd_local->SCp.ptr = NULL;
2966 megasas_return_cmd_fusion(instance, cmd);
2967 scsi_dma_unmap(scmd_local);
2968 scmd_local->scsi_done(scmd_local);
2969 }
2970}
2971
2972/**
adam radford9c915a82010-12-21 13:34:31 -08002973 * complete_cmd_fusion - Completes command
2974 * @instance: Adapter soft state
2975 * Completes all commands that is in reply descriptor queue
2976 */
2977int
adam radfordc8e858f2011-10-08 18:15:13 -07002978complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
adam radford9c915a82010-12-21 13:34:31 -08002979{
2980 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
2981 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
2982 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
2983 struct fusion_context *fusion;
2984 struct megasas_cmd *cmd_mfi;
Shivasharan S8bf7c652017-02-10 00:59:03 -08002985 struct megasas_cmd_fusion *cmd_fusion;
adam radford9c915a82010-12-21 13:34:31 -08002986 u16 smid, num_completed;
Shivasharan S8bf7c652017-02-10 00:59:03 -08002987 u8 reply_descript_type, *sense, status, extStatus;
2988 u32 device_id, data_length;
adam radford9c915a82010-12-21 13:34:31 -08002989 union desc_value d_val;
2990 struct LD_LOAD_BALANCE_INFO *lbinfo;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05302991 int threshold_reply_count = 0;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302992 struct scsi_cmnd *scmd_local = NULL;
Sumit Saxena18365b12016-01-28 21:04:25 +05302993 struct MR_TASK_MANAGE_REQUEST *mr_tm_req;
2994 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req;
adam radford9c915a82010-12-21 13:34:31 -08002995
2996 fusion = instance->ctrl_context;
2997
Sumit Saxena8a01a412016-01-28 21:04:32 +05302998 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR)
adam radford9c915a82010-12-21 13:34:31 -08002999 return IRQ_HANDLED;
3000
Sumit Saxena179ac142016-01-28 21:04:28 +05303001 desc = fusion->reply_frames_desc[MSIxIndex] +
3002 fusion->last_reply_idx[MSIxIndex];
adam radford9c915a82010-12-21 13:34:31 -08003003
3004 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3005
3006 d_val.word = desc->Words;
3007
3008 reply_descript_type = reply_desc->ReplyFlags &
3009 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3010
3011 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3012 return IRQ_NONE;
3013
adam radford9c915a82010-12-21 13:34:31 -08003014 num_completed = 0;
3015
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05303016 while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
3017 d_val.u.high != cpu_to_le32(UINT_MAX)) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003018
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303019 smid = le16_to_cpu(reply_desc->SMID);
adam radford9c915a82010-12-21 13:34:31 -08003020 cmd_fusion = fusion->cmd_list[smid - 1];
Shivasharan S8bf7c652017-02-10 00:59:03 -08003021 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *)
3022 cmd_fusion->io_request;
adam radford9c915a82010-12-21 13:34:31 -08003023
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303024 scmd_local = cmd_fusion->scmd;
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05003025 status = scsi_io_req->RaidContext.raid_context.status;
Sasikumar Chandrasekarand8893442017-01-10 18:20:48 -05003026 extStatus = scsi_io_req->RaidContext.raid_context.ex_status;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003027 sense = cmd_fusion->sense;
3028 data_length = scsi_io_req->DataLength;
adam radford9c915a82010-12-21 13:34:31 -08003029
3030 switch (scsi_io_req->Function) {
Sumit Saxena18365b12016-01-28 21:04:25 +05303031 case MPI2_FUNCTION_SCSI_TASK_MGMT:
3032 mr_tm_req = (struct MR_TASK_MANAGE_REQUEST *)
3033 cmd_fusion->io_request;
3034 mpi_tm_req = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *)
3035 &mr_tm_req->TmRequest;
3036 dev_dbg(&instance->pdev->dev, "TM completion:"
3037 "type: 0x%x TaskMID: 0x%x\n",
3038 mpi_tm_req->TaskType, mpi_tm_req->TaskMID);
3039 complete(&cmd_fusion->done);
3040 break;
adam radford9c915a82010-12-21 13:34:31 -08003041 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
3042 /* Update load balancing info */
Shivasharan S8bf7c652017-02-10 00:59:03 -08003043 if (fusion->load_balance_info &&
3044 (cmd_fusion->scmd->SCp.Status &
3045 MEGASAS_LOAD_BALANCE_FLAG)) {
3046 device_id = MEGASAS_DEV_INDEX(scmd_local);
3047 lbinfo = &fusion->load_balance_info[device_id];
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05303048 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
Shivasharan S8bf7c652017-02-10 00:59:03 -08003049 cmd_fusion->scmd->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
adam radford9c915a82010-12-21 13:34:31 -08003050 }
Shivasharan S8bf7c652017-02-10 00:59:03 -08003051 //Fall thru and complete IO
adam radford9c915a82010-12-21 13:34:31 -08003052 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
Shivasharan S8bf7c652017-02-10 00:59:03 -08003053 atomic_dec(&instance->fw_outstanding);
3054 if (cmd_fusion->r1_alt_dev_handle == MR_DEVHANDLE_INVALID) {
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003055 map_cmd_status(fusion, scmd_local, status,
Shivasharan S8bf7c652017-02-10 00:59:03 -08003056 extStatus, le32_to_cpu(data_length),
3057 sense);
3058 if (instance->ldio_threshold &&
3059 (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO))
Sasikumar Chandrasekaranb71b49c2017-01-10 18:20:51 -05003060 atomic_dec(&instance->ldio_outstanding);
Shivasharan S8bf7c652017-02-10 00:59:03 -08003061 scmd_local->SCp.ptr = NULL;
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05003062 megasas_return_cmd_fusion(instance, cmd_fusion);
3063 scsi_dma_unmap(scmd_local);
3064 scmd_local->scsi_done(scmd_local);
Shivasharan S8bf7c652017-02-10 00:59:03 -08003065 } else /* Optimal VD - R1 FP command completion. */
3066 megasas_complete_r1_command(instance, cmd_fusion);
adam radford9c915a82010-12-21 13:34:31 -08003067 break;
3068 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
3069 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303070 /* Poll mode. Dummy free.
3071 * In case of Interrupt mode, caller has reverse check.
3072 */
3073 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
3074 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
3075 megasas_return_cmd(instance, cmd_mfi);
3076 } else
3077 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
adam radford9c915a82010-12-21 13:34:31 -08003078 break;
3079 }
3080
adam radfordc8e858f2011-10-08 18:15:13 -07003081 fusion->last_reply_idx[MSIxIndex]++;
3082 if (fusion->last_reply_idx[MSIxIndex] >=
3083 fusion->reply_q_depth)
3084 fusion->last_reply_idx[MSIxIndex] = 0;
adam radford9c915a82010-12-21 13:34:31 -08003085
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05303086 desc->Words = cpu_to_le64(ULLONG_MAX);
adam radford9c915a82010-12-21 13:34:31 -08003087 num_completed++;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05303088 threshold_reply_count++;
adam radford9c915a82010-12-21 13:34:31 -08003089
3090 /* Get the next reply descriptor */
adam radfordc8e858f2011-10-08 18:15:13 -07003091 if (!fusion->last_reply_idx[MSIxIndex])
Sumit Saxena179ac142016-01-28 21:04:28 +05303092 desc = fusion->reply_frames_desc[MSIxIndex];
adam radford9c915a82010-12-21 13:34:31 -08003093 else
3094 desc++;
3095
3096 reply_desc =
3097 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
3098
3099 d_val.word = desc->Words;
3100
3101 reply_descript_type = reply_desc->ReplyFlags &
3102 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
3103
3104 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
3105 break;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05303106 /*
3107 * Write to reply post host index register after completing threshold
3108 * number of reply counts and still there are more replies in reply queue
3109 * pending to be completed
3110 */
3111 if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05003112 if (instance->msix_combined)
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05303113 writel(((MSIxIndex & 0x7) << 24) |
3114 fusion->last_reply_idx[MSIxIndex],
3115 instance->reply_post_host_index_addr[MSIxIndex/8]);
3116 else
3117 writel((MSIxIndex << 24) |
3118 fusion->last_reply_idx[MSIxIndex],
3119 instance->reply_post_host_index_addr[0]);
3120 threshold_reply_count = 0;
3121 }
adam radford9c915a82010-12-21 13:34:31 -08003122 }
3123
3124 if (!num_completed)
3125 return IRQ_NONE;
3126
3127 wmb();
Sasikumar Chandrasekaran2493c672017-01-10 18:20:44 -05003128 if (instance->msix_combined)
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303129 writel(((MSIxIndex & 0x7) << 24) |
3130 fusion->last_reply_idx[MSIxIndex],
3131 instance->reply_post_host_index_addr[MSIxIndex/8]);
3132 else
3133 writel((MSIxIndex << 24) |
3134 fusion->last_reply_idx[MSIxIndex],
3135 instance->reply_post_host_index_addr[0]);
adam radford53ef2bb2011-02-24 20:56:05 -08003136 megasas_check_and_restore_queue_depth(instance);
adam radford9c915a82010-12-21 13:34:31 -08003137 return IRQ_HANDLED;
3138}
3139
3140/**
Shivasharan S29206da2017-02-10 00:59:34 -08003141 * megasas_sync_irqs - Synchronizes all IRQs owned by adapter
3142 * @instance: Adapter soft state
3143 */
3144void megasas_sync_irqs(unsigned long instance_addr)
3145{
3146 u32 count, i;
3147 struct megasas_instance *instance =
3148 (struct megasas_instance *)instance_addr;
3149
3150 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
3151
3152 for (i = 0; i < count; i++)
3153 synchronize_irq(pci_irq_vector(instance->pdev, i));
3154}
3155
3156/**
adam radford9c915a82010-12-21 13:34:31 -08003157 * megasas_complete_cmd_dpc_fusion - Completes command
3158 * @instance: Adapter soft state
3159 *
3160 * Tasklet to complete cmds
3161 */
3162void
3163megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
3164{
3165 struct megasas_instance *instance =
3166 (struct megasas_instance *)instance_addr;
3167 unsigned long flags;
adam radfordc8e858f2011-10-08 18:15:13 -07003168 u32 count, MSIxIndex;
3169
3170 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
adam radford9c915a82010-12-21 13:34:31 -08003171
3172 /* If we have already declared adapter dead, donot complete cmds */
3173 spin_lock_irqsave(&instance->hba_lock, flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05303174 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
adam radford9c915a82010-12-21 13:34:31 -08003175 spin_unlock_irqrestore(&instance->hba_lock, flags);
3176 return;
3177 }
3178 spin_unlock_irqrestore(&instance->hba_lock, flags);
3179
adam radfordc8e858f2011-10-08 18:15:13 -07003180 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
3181 complete_cmd_fusion(instance, MSIxIndex);
adam radford9c915a82010-12-21 13:34:31 -08003182}
3183
3184/**
3185 * megasas_isr_fusion - isr entry point
3186 */
3187irqreturn_t megasas_isr_fusion(int irq, void *devp)
3188{
adam radfordc8e858f2011-10-08 18:15:13 -07003189 struct megasas_irq_context *irq_context = devp;
3190 struct megasas_instance *instance = irq_context->instance;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303191 u32 mfiStatus, fw_state, dma_state;
adam radford9c915a82010-12-21 13:34:31 -08003192
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05303193 if (instance->mask_interrupts)
3194 return IRQ_NONE;
3195
adam radfordc8e858f2011-10-08 18:15:13 -07003196 if (!instance->msix_vectors) {
adam radford9c915a82010-12-21 13:34:31 -08003197 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
3198 if (!mfiStatus)
3199 return IRQ_NONE;
3200 }
3201
3202 /* If we are resetting, bail */
adam radford6497b242011-10-08 18:14:50 -07003203 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
3204 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08003205 return IRQ_HANDLED;
adam radford6497b242011-10-08 18:14:50 -07003206 }
adam radford9c915a82010-12-21 13:34:31 -08003207
adam radfordc8e858f2011-10-08 18:15:13 -07003208 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
adam radford6497b242011-10-08 18:14:50 -07003209 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08003210 /* If we didn't complete any commands, check for FW fault */
3211 fw_state = instance->instancet->read_fw_status_reg(
3212 instance->reg_set) & MFI_STATE_MASK;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303213 dma_state = instance->instancet->read_fw_status_reg
3214 (instance->reg_set) & MFI_STATE_DMADONE;
3215 if (instance->crash_dump_drv_support &&
3216 instance->crash_dump_app_support) {
3217 /* Start collecting crash, if DMA bit is done */
3218 if ((fw_state == MFI_STATE_FAULT) && dma_state)
3219 schedule_work(&instance->crash_init);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07003220 else if (fw_state == MFI_STATE_FAULT) {
3221 if (instance->unload == 0)
3222 schedule_work(&instance->work_init);
3223 }
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303224 } else if (fw_state == MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003225 dev_warn(&instance->pdev->dev, "Iop2SysDoorbellInt"
adam radford229fe472014-03-10 02:51:56 -07003226 "for scsi%d\n", instance->host->host_no);
Kashyap Desaia1dfd622016-10-21 06:33:31 -07003227 if (instance->unload == 0)
3228 schedule_work(&instance->work_init);
adam radford229fe472014-03-10 02:51:56 -07003229 }
adam radford9c915a82010-12-21 13:34:31 -08003230 }
3231
3232 return IRQ_HANDLED;
3233}
3234
3235/**
3236 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
3237 * @instance: Adapter soft state
3238 * mfi_cmd: megasas_cmd pointer
3239 *
3240 */
Shivasharan S7a7ae4f2017-02-10 00:59:32 -08003241void
adam radford9c915a82010-12-21 13:34:31 -08003242build_mpt_mfi_pass_thru(struct megasas_instance *instance,
3243 struct megasas_cmd *mfi_cmd)
3244{
3245 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
3246 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
3247 struct megasas_cmd_fusion *cmd;
3248 struct fusion_context *fusion;
3249 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
3250
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303251 fusion = instance->ctrl_context;
3252
3253 cmd = megasas_get_cmd_fusion(instance,
3254 instance->max_scsi_cmds + mfi_cmd->index);
adam radford9c915a82010-12-21 13:34:31 -08003255
3256 /* Save the smid. To be used for returning the cmd */
3257 mfi_cmd->context.smid = cmd->index;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05303258
adam radford9c915a82010-12-21 13:34:31 -08003259 /*
3260 * For cmds where the flag is set, store the flag and check
3261 * on completion. For cmds with this flag, don't call
3262 * megasas_complete_cmd
3263 */
3264
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05303265 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05303266 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
adam radford9c915a82010-12-21 13:34:31 -08003267
adam radford9c915a82010-12-21 13:34:31 -08003268 io_req = cmd->io_request;
adam radford36807e62011-10-08 18:15:06 -07003269
Sasikumar Chandrasekaran9581ebe2017-01-10 18:20:49 -05003270 if (fusion->adapter_type >= INVADER_SERIES) {
adam radford36807e62011-10-08 18:15:06 -07003271 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
3272 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
3273 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
3274 sgl_ptr_end->Flags = 0;
3275 }
3276
adam radford9c915a82010-12-21 13:34:31 -08003277 mpi25_ieee_chain =
3278 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
3279
3280 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
3281 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
3282 SGL) / 4;
3283 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
3284
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303285 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08003286
3287 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
3288 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
3289
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05303290 mpi25_ieee_chain->Length = cpu_to_le32(instance->max_chain_frame_sz);
adam radford9c915a82010-12-21 13:34:31 -08003291}
3292
3293/**
3294 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
3295 * @instance: Adapter soft state
3296 * @cmd: mfi cmd to build
3297 *
3298 */
3299union MEGASAS_REQUEST_DESCRIPTOR_UNION *
3300build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
3301{
Shivasharan S52205ac2017-02-10 00:59:08 -08003302 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc = NULL;
adam radford9c915a82010-12-21 13:34:31 -08003303 u16 index;
3304
Shivasharan S7a7ae4f2017-02-10 00:59:32 -08003305 build_mpt_mfi_pass_thru(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -08003306 index = cmd->context.smid;
3307
3308 req_desc = megasas_get_request_descriptor(instance, index - 1);
3309
adam radford9c915a82010-12-21 13:34:31 -08003310 req_desc->Words = 0;
3311 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
3312 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3313
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05303314 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08003315
3316 return req_desc;
3317}
3318
3319/**
3320 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
3321 * @instance: Adapter soft state
3322 * @cmd: mfi cmd pointer
3323 *
3324 */
Shivasharan Sf4fc2092017-02-10 00:59:09 -08003325void
adam radford9c915a82010-12-21 13:34:31 -08003326megasas_issue_dcmd_fusion(struct megasas_instance *instance,
3327 struct megasas_cmd *cmd)
3328{
3329 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
adam radford9c915a82010-12-21 13:34:31 -08003330
3331 req_desc = build_mpt_cmd(instance, cmd);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303332
Shivasharan S45b8a352017-02-10 00:59:04 -08003333 megasas_fire_cmd_fusion(instance, req_desc);
Shivasharan Sf4fc2092017-02-10 00:59:09 -08003334 return;
adam radford9c915a82010-12-21 13:34:31 -08003335}
3336
3337/**
3338 * megasas_release_fusion - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01003339 * @instance: Adapter soft state
adam radford9c915a82010-12-21 13:34:31 -08003340 */
3341void
3342megasas_release_fusion(struct megasas_instance *instance)
3343{
3344 megasas_free_cmds(instance);
3345 megasas_free_cmds_fusion(instance);
3346
3347 iounmap(instance->reg_set);
3348
Yinghai Lue7f85162016-08-05 23:37:34 -07003349 pci_release_selected_regions(instance->pdev, 1<<instance->bar);
adam radford9c915a82010-12-21 13:34:31 -08003350}
3351
3352/**
3353 * megasas_read_fw_status_reg_fusion - returns the current FW status value
3354 * @regs: MFI register set
3355 */
3356static u32
3357megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
3358{
3359 return readl(&(regs)->outbound_scratch_pad);
3360}
3361
3362/**
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303363 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3364 * @instance: Controller's soft instance
3365 * return: Number of allocated host crash buffers
3366 */
3367static void
3368megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
3369{
3370 unsigned int i;
3371
3372 instance->crash_buf_pages = get_order(CRASH_DMA_BUF_SIZE);
3373 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
3374 instance->crash_buf[i] = (void *)__get_free_pages(GFP_KERNEL,
3375 instance->crash_buf_pages);
3376 if (!instance->crash_buf[i]) {
3377 dev_info(&instance->pdev->dev, "Firmware crash dump "
3378 "memory allocation failed at index %d\n", i);
3379 break;
3380 }
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05303381 memset(instance->crash_buf[i], 0,
3382 ((1 << PAGE_SHIFT) << instance->crash_buf_pages));
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303383 }
3384 instance->drv_buf_alloc = i;
3385}
3386
3387/**
3388 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
3389 * @instance: Controller's soft instance
3390 */
3391void
3392megasas_free_host_crash_buffer(struct megasas_instance *instance)
3393{
3394 unsigned int i
3395;
3396 for (i = 0; i < instance->drv_buf_alloc; i++) {
3397 if (instance->crash_buf[i])
3398 free_pages((ulong)instance->crash_buf[i],
3399 instance->crash_buf_pages);
3400 }
3401 instance->drv_buf_index = 0;
3402 instance->drv_buf_alloc = 0;
3403 instance->fw_crash_state = UNAVAILABLE;
3404 instance->fw_crash_buffer_size = 0;
3405}
3406
3407/**
adam radford9c915a82010-12-21 13:34:31 -08003408 * megasas_adp_reset_fusion - For controller reset
3409 * @regs: MFI register set
3410 */
3411static int
3412megasas_adp_reset_fusion(struct megasas_instance *instance,
3413 struct megasas_register_set __iomem *regs)
3414{
sumit.saxena@avagotech.com79b82c22015-08-31 17:23:41 +05303415 u32 host_diag, abs_state, retry;
3416
3417 /* Now try to reset the chip */
3418 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3419 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3420 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3421 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3422 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3423 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3424 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
3425
3426 /* Check that the diag write enable (DRWE) bit is on */
3427 host_diag = readl(&instance->reg_set->fusion_host_diag);
3428 retry = 0;
3429 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
3430 msleep(100);
3431 host_diag = readl(&instance->reg_set->fusion_host_diag);
3432 if (retry++ == 100) {
3433 dev_warn(&instance->pdev->dev,
3434 "Host diag unlock failed from %s %d\n",
3435 __func__, __LINE__);
3436 break;
3437 }
3438 }
3439 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
3440 return -1;
3441
3442 /* Send chip reset command */
3443 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
3444 &instance->reg_set->fusion_host_diag);
3445 msleep(3000);
3446
3447 /* Make sure reset adapter bit is cleared */
3448 host_diag = readl(&instance->reg_set->fusion_host_diag);
3449 retry = 0;
3450 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
3451 msleep(100);
3452 host_diag = readl(&instance->reg_set->fusion_host_diag);
3453 if (retry++ == 1000) {
3454 dev_warn(&instance->pdev->dev,
3455 "Diag reset adapter never cleared %s %d\n",
3456 __func__, __LINE__);
3457 break;
3458 }
3459 }
3460 if (host_diag & HOST_DIAG_RESET_ADAPTER)
3461 return -1;
3462
3463 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set)
3464 & MFI_STATE_MASK;
3465 retry = 0;
3466
3467 while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
3468 msleep(100);
3469 abs_state = instance->instancet->
3470 read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
3471 }
3472 if (abs_state <= MFI_STATE_FW_INIT) {
3473 dev_warn(&instance->pdev->dev,
3474 "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
3475 abs_state, __func__, __LINE__);
3476 return -1;
3477 }
3478
adam radford9c915a82010-12-21 13:34:31 -08003479 return 0;
3480}
3481
3482/**
3483 * megasas_check_reset_fusion - For controller reset check
3484 * @regs: MFI register set
3485 */
3486static int
3487megasas_check_reset_fusion(struct megasas_instance *instance,
3488 struct megasas_register_set __iomem *regs)
3489{
3490 return 0;
3491}
3492
3493/* This function waits for outstanding commands on fusion to complete */
adam radford229fe472014-03-10 02:51:56 -07003494int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303495 int reason, int *convert)
adam radford9c915a82010-12-21 13:34:31 -08003496{
adam radford229fe472014-03-10 02:51:56 -07003497 int i, outstanding, retval = 0, hb_seconds_missed = 0;
adam radfordc007b8b2012-07-17 18:20:24 -07003498 u32 fw_state;
adam radford9c915a82010-12-21 13:34:31 -08003499
adam radfordc007b8b2012-07-17 18:20:24 -07003500 for (i = 0; i < resetwaittime; i++) {
adam radford9c915a82010-12-21 13:34:31 -08003501 /* Check if firmware is in fault state */
3502 fw_state = instance->instancet->read_fw_status_reg(
3503 instance->reg_set) & MFI_STATE_MASK;
3504 if (fw_state == MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003505 dev_warn(&instance->pdev->dev, "Found FW in FAULT state,"
adam radford229fe472014-03-10 02:51:56 -07003506 " will reset adapter scsi%d.\n",
3507 instance->host->host_no);
Sumit Saxena64d0b8e2016-04-15 00:23:31 -07003508 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
Shivasharan Sdef0eab2017-02-10 00:59:15 -08003509 if (instance->requestorId && reason) {
3510 dev_warn(&instance->pdev->dev, "SR-IOV Found FW in FAULT"
3511 " state while polling during"
3512 " I/O timeout handling for %d\n",
3513 instance->host->host_no);
3514 *convert = 1;
3515 }
3516
adam radford9c915a82010-12-21 13:34:31 -08003517 retval = 1;
3518 goto out;
3519 }
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303520
3521 if (reason == MFI_IO_TIMEOUT_OCR) {
3522 dev_info(&instance->pdev->dev,
3523 "MFI IO is timed out, initiating OCR\n");
Sumit Saxena64d0b8e2016-04-15 00:23:31 -07003524 megasas_complete_cmd_dpc_fusion((unsigned long)instance);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303525 retval = 1;
3526 goto out;
3527 }
3528
adam radford229fe472014-03-10 02:51:56 -07003529 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303530 if (instance->requestorId && !reason) {
adam radford229fe472014-03-10 02:51:56 -07003531 retval = 1;
3532 goto out;
3533 }
3534
3535 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
Shivasharan Sdef0eab2017-02-10 00:59:15 -08003536 if (instance->requestorId && (reason == SCSIIO_TIMEOUT_OCR)) {
adam radford229fe472014-03-10 02:51:56 -07003537 if (instance->hb_host_mem->HB.fwCounter !=
3538 instance->hb_host_mem->HB.driverCounter) {
3539 instance->hb_host_mem->HB.driverCounter =
3540 instance->hb_host_mem->HB.fwCounter;
3541 hb_seconds_missed = 0;
3542 } else {
3543 hb_seconds_missed++;
3544 if (hb_seconds_missed ==
3545 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003546 dev_warn(&instance->pdev->dev, "SR-IOV:"
adam radford229fe472014-03-10 02:51:56 -07003547 " Heartbeat never completed "
3548 " while polling during I/O "
3549 " timeout handling for "
3550 "scsi%d.\n",
3551 instance->host->host_no);
3552 *convert = 1;
3553 retval = 1;
3554 goto out;
3555 }
3556 }
3557 }
adam radford9c915a82010-12-21 13:34:31 -08003558
3559 outstanding = atomic_read(&instance->fw_outstanding);
3560 if (!outstanding)
3561 goto out;
3562
3563 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003564 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
adam radford229fe472014-03-10 02:51:56 -07003565 "commands to complete for scsi%d\n", i,
3566 outstanding, instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08003567 megasas_complete_cmd_dpc_fusion(
3568 (unsigned long)instance);
3569 }
3570 msleep(1000);
3571 }
3572
3573 if (atomic_read(&instance->fw_outstanding)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05003574 dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
adam radford229fe472014-03-10 02:51:56 -07003575 "will reset adapter scsi%d.\n",
3576 instance->host->host_no);
Kashyap Desai18e1c7f2016-10-21 06:33:29 -07003577 *convert = 1;
adam radford9c915a82010-12-21 13:34:31 -08003578 retval = 1;
3579 }
3580out:
3581 return retval;
3582}
3583
3584void megasas_reset_reply_desc(struct megasas_instance *instance)
3585{
Sumit Saxena179ac142016-01-28 21:04:28 +05303586 int i, j, count;
adam radford9c915a82010-12-21 13:34:31 -08003587 struct fusion_context *fusion;
3588 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
3589
3590 fusion = instance->ctrl_context;
adam radfordc8e858f2011-10-08 18:15:13 -07003591 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
Sumit Saxena179ac142016-01-28 21:04:28 +05303592 for (i = 0 ; i < count ; i++) {
adam radfordc8e858f2011-10-08 18:15:13 -07003593 fusion->last_reply_idx[i] = 0;
Sumit Saxena179ac142016-01-28 21:04:28 +05303594 reply_desc = fusion->reply_frames_desc[i];
3595 for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++)
3596 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
3597 }
adam radford9c915a82010-12-21 13:34:31 -08003598}
3599
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05303600/*
3601 * megasas_refire_mgmt_cmd : Re-fire management commands
3602 * @instance: Controller's soft instance
3603*/
3604void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
3605{
3606 int j;
3607 struct megasas_cmd_fusion *cmd_fusion;
3608 struct fusion_context *fusion;
3609 struct megasas_cmd *cmd_mfi;
3610 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3611 u16 smid;
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303612 bool refire_cmd = 0;
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05303613
3614 fusion = instance->ctrl_context;
3615
3616 /* Re-fire management commands.
3617 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
3618 */
3619 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
3620 cmd_fusion = fusion->cmd_list[j];
3621 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
3622 smid = le16_to_cpu(cmd_mfi->context.smid);
3623
3624 if (!smid)
3625 continue;
3626 req_desc = megasas_get_request_descriptor
3627 (instance, smid - 1);
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303628 refire_cmd = req_desc && ((cmd_mfi->frame->dcmd.opcode !=
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05303629 cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO)) &&
3630 (cmd_mfi->frame->dcmd.opcode !=
Sumit Saxena6d40afb2016-01-28 21:04:23 +05303631 cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO)))
3632 && !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE);
3633 if (refire_cmd)
Shivasharan S45b8a352017-02-10 00:59:04 -08003634 megasas_fire_cmd_fusion(instance, req_desc);
Christoph Hellwig01d7f032015-04-23 16:34:09 +05303635 else
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05303636 megasas_return_cmd(instance, cmd_mfi);
3637 }
adam radford9c915a82010-12-21 13:34:31 -08003638}
3639
Sumit Saxena18365b12016-01-28 21:04:25 +05303640/*
3641 * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device
3642 * @instance: per adapter struct
3643 * @channel: the channel assigned by the OS
3644 * @id: the id assigned by the OS
3645 *
3646 * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED
3647 */
3648
3649static int megasas_track_scsiio(struct megasas_instance *instance,
3650 int id, int channel)
3651{
3652 int i, found = 0;
3653 struct megasas_cmd_fusion *cmd_fusion;
3654 struct fusion_context *fusion;
3655 fusion = instance->ctrl_context;
3656
3657 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
3658 cmd_fusion = fusion->cmd_list[i];
3659 if (cmd_fusion->scmd &&
3660 (cmd_fusion->scmd->device->id == id &&
3661 cmd_fusion->scmd->device->channel == channel)) {
3662 dev_info(&instance->pdev->dev,
3663 "SCSI commands pending to target"
3664 "channel %d id %d \tSMID: 0x%x\n",
3665 channel, id, cmd_fusion->index);
3666 scsi_print_command(cmd_fusion->scmd);
3667 found = 1;
3668 break;
3669 }
3670 }
3671
3672 return found ? FAILED : SUCCESS;
3673}
3674
3675/**
3676 * megasas_tm_response_code - translation of device response code
3677 * @ioc: per adapter object
3678 * @mpi_reply: MPI reply returned by firmware
3679 *
3680 * Return nothing.
3681 */
3682static void
3683megasas_tm_response_code(struct megasas_instance *instance,
3684 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply)
3685{
3686 char *desc;
3687
3688 switch (mpi_reply->ResponseCode) {
3689 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
3690 desc = "task management request completed";
3691 break;
3692 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
3693 desc = "invalid frame";
3694 break;
3695 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
3696 desc = "task management request not supported";
3697 break;
3698 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
3699 desc = "task management request failed";
3700 break;
3701 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
3702 desc = "task management request succeeded";
3703 break;
3704 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
3705 desc = "invalid lun";
3706 break;
3707 case 0xA:
3708 desc = "overlapped tag attempted";
3709 break;
3710 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
3711 desc = "task queued, however not sent to target";
3712 break;
3713 default:
3714 desc = "unknown";
3715 break;
3716 }
3717 dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n",
3718 mpi_reply->ResponseCode, desc);
3719 dev_dbg(&instance->pdev->dev,
3720 "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo"
3721 " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n",
3722 mpi_reply->TerminationCount, mpi_reply->DevHandle,
3723 mpi_reply->Function, mpi_reply->TaskType,
3724 mpi_reply->IOCStatus, mpi_reply->IOCLogInfo);
3725}
3726
3727/**
3728 * megasas_issue_tm - main routine for sending tm requests
3729 * @instance: per adapter struct
3730 * @device_handle: device handle
3731 * @channel: the channel assigned by the OS
3732 * @id: the id assigned by the OS
3733 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c)
3734 * @smid_task: smid assigned to the task
3735 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF
3736 * Context: user
3737 *
3738 * MegaRaid use MPT interface for Task Magement request.
3739 * A generic API for sending task management requests to firmware.
3740 *
3741 * Return SUCCESS or FAILED.
3742 */
3743static int
3744megasas_issue_tm(struct megasas_instance *instance, u16 device_handle,
3745 uint channel, uint id, u16 smid_task, u8 type)
3746{
3747 struct MR_TASK_MANAGE_REQUEST *mr_request;
3748 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request;
3749 unsigned long timeleft;
3750 struct megasas_cmd_fusion *cmd_fusion;
3751 struct megasas_cmd *cmd_mfi;
3752 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
3753 struct fusion_context *fusion;
3754 struct megasas_cmd_fusion *scsi_lookup;
3755 int rc;
3756 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply;
3757
3758 fusion = instance->ctrl_context;
3759
3760 cmd_mfi = megasas_get_cmd(instance);
3761
3762 if (!cmd_mfi) {
3763 dev_err(&instance->pdev->dev, "Failed from %s %d\n",
3764 __func__, __LINE__);
3765 return -ENOMEM;
3766 }
3767
3768 cmd_fusion = megasas_get_cmd_fusion(instance,
3769 instance->max_scsi_cmds + cmd_mfi->index);
3770
3771 /* Save the smid. To be used for returning the cmd */
3772 cmd_mfi->context.smid = cmd_fusion->index;
3773
3774 req_desc = megasas_get_request_descriptor(instance,
3775 (cmd_fusion->index - 1));
Sumit Saxena18365b12016-01-28 21:04:25 +05303776
3777 cmd_fusion->request_desc = req_desc;
3778 req_desc->Words = 0;
3779
3780 scsi_lookup = fusion->cmd_list[smid_task - 1];
3781
3782 mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request;
3783 memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST));
3784 mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest;
3785 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3786 mpi_request->DevHandle = cpu_to_le16(device_handle);
3787 mpi_request->TaskType = type;
3788 mpi_request->TaskMID = cpu_to_le16(smid_task);
3789 mpi_request->LUN[1] = 0;
3790
3791
3792 req_desc = cmd_fusion->request_desc;
3793 req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index);
3794 req_desc->HighPriority.RequestFlags =
3795 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
3796 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
3797 req_desc->HighPriority.MSIxIndex = 0;
3798 req_desc->HighPriority.LMID = 0;
3799 req_desc->HighPriority.Reserved1 = 0;
3800
3801 if (channel < MEGASAS_MAX_PD_CHANNELS)
3802 mr_request->tmReqFlags.isTMForPD = 1;
3803 else
3804 mr_request->tmReqFlags.isTMForLD = 1;
3805
3806 init_completion(&cmd_fusion->done);
Shivasharan S45b8a352017-02-10 00:59:04 -08003807 megasas_fire_cmd_fusion(instance, req_desc);
Sumit Saxena18365b12016-01-28 21:04:25 +05303808
3809 timeleft = wait_for_completion_timeout(&cmd_fusion->done, 50 * HZ);
3810
3811 if (!timeleft) {
3812 dev_err(&instance->pdev->dev,
3813 "task mgmt type 0x%x timed out\n", type);
3814 cmd_mfi->flags |= DRV_DCMD_SKIP_REFIRE;
3815 mutex_unlock(&instance->reset_mutex);
3816 rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR);
3817 mutex_lock(&instance->reset_mutex);
3818 return rc;
3819 }
3820
3821 mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply;
3822 megasas_tm_response_code(instance, mpi_reply);
3823
3824 megasas_return_cmd(instance, cmd_mfi);
3825 rc = SUCCESS;
3826 switch (type) {
3827 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK:
3828 if (scsi_lookup->scmd == NULL)
3829 break;
3830 else {
3831 instance->instancet->disable_intr(instance);
Shivasharan S29206da2017-02-10 00:59:34 -08003832 megasas_sync_irqs((unsigned long)instance);
Sumit Saxena18365b12016-01-28 21:04:25 +05303833 megasas_complete_cmd_dpc_fusion
3834 ((unsigned long)instance);
3835 instance->instancet->enable_intr(instance);
3836 if (scsi_lookup->scmd == NULL)
3837 break;
3838 }
3839 rc = FAILED;
3840 break;
3841
3842 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET:
3843 if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF))
3844 break;
3845 instance->instancet->disable_intr(instance);
3846 msleep(1000);
3847 megasas_complete_cmd_dpc_fusion
3848 ((unsigned long)instance);
3849 rc = megasas_track_scsiio(instance, id, channel);
3850 instance->instancet->enable_intr(instance);
3851
3852 break;
3853 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET:
3854 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK:
3855 break;
3856 default:
3857 rc = FAILED;
3858 break;
3859 }
3860
3861 return rc;
3862
3863}
3864
3865/*
3866 * megasas_fusion_smid_lookup : Look for fusion command correpspodning to SCSI
3867 * @instance: per adapter struct
3868 *
3869 * Return Non Zero index, if SMID found in outstanding commands
3870 */
3871static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd)
3872{
3873 int i, ret = 0;
3874 struct megasas_instance *instance;
3875 struct megasas_cmd_fusion *cmd_fusion;
3876 struct fusion_context *fusion;
3877
3878 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3879
3880 fusion = instance->ctrl_context;
3881
3882 for (i = 0; i < instance->max_scsi_cmds; i++) {
3883 cmd_fusion = fusion->cmd_list[i];
3884 if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) {
3885 scmd_printk(KERN_NOTICE, scmd, "Abort request is for"
3886 " SMID: %d\n", cmd_fusion->index);
3887 ret = cmd_fusion->index;
3888 break;
3889 }
3890 }
3891
3892 return ret;
3893}
3894
3895/*
3896* megasas_get_tm_devhandle - Get devhandle for TM request
3897* @sdev- OS provided scsi device
3898*
3899* Returns- devhandle/targetID of SCSI device
3900*/
3901static u16 megasas_get_tm_devhandle(struct scsi_device *sdev)
3902{
3903 u16 pd_index = 0;
3904 u32 device_id;
3905 struct megasas_instance *instance;
3906 struct fusion_context *fusion;
3907 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
3908 u16 devhandle = (u16)ULONG_MAX;
3909
3910 instance = (struct megasas_instance *)sdev->host->hostdata;
3911 fusion = instance->ctrl_context;
3912
Shivasharan S3cabd162017-02-10 00:59:05 -08003913 if (!MEGASAS_IS_LOGICAL(sdev)) {
Sumit Saxena18365b12016-01-28 21:04:25 +05303914 if (instance->use_seqnum_jbod_fp) {
Shivasharan S3cabd162017-02-10 00:59:05 -08003915 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
3916 + sdev->id;
3917 pd_sync = (void *)fusion->pd_seq_sync
3918 [(instance->pd_seq_map_id - 1) & 1];
3919 devhandle = pd_sync->seq[pd_index].devHandle;
Sumit Saxena18365b12016-01-28 21:04:25 +05303920 } else
3921 sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable"
3922 " without JBOD MAP support from %s %d\n", __func__, __LINE__);
3923 } else {
3924 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL)
3925 + sdev->id;
3926 devhandle = device_id;
3927 }
3928
3929 return devhandle;
3930}
3931
3932/*
3933 * megasas_task_abort_fusion : SCSI task abort function for fusion adapters
3934 * @scmd : pointer to scsi command object
3935 *
3936 * Return SUCCESS, if command aborted else FAILED
3937 */
3938
3939int megasas_task_abort_fusion(struct scsi_cmnd *scmd)
3940{
3941 struct megasas_instance *instance;
3942 u16 smid, devhandle;
3943 struct fusion_context *fusion;
3944 int ret;
3945 struct MR_PRIV_DEVICE *mr_device_priv_data;
3946 mr_device_priv_data = scmd->device->hostdata;
3947
3948
3949 instance = (struct megasas_instance *)scmd->device->host->hostdata;
3950 fusion = instance->ctrl_context;
3951
Shivasharan Sdef0eab2017-02-10 00:59:15 -08003952 scmd_printk(KERN_INFO, scmd, "task abort called for scmd(%p)\n", scmd);
3953 scsi_print_command(scmd);
3954
Sumit Saxena8a01a412016-01-28 21:04:32 +05303955 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Sumit Saxena18365b12016-01-28 21:04:25 +05303956 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
3957 "SCSI host:%d\n", instance->host->host_no);
3958 ret = FAILED;
3959 return ret;
3960 }
3961
3962 if (!mr_device_priv_data) {
3963 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
3964 "scmd(%p)\n", scmd);
3965 scmd->result = DID_NO_CONNECT << 16;
3966 ret = SUCCESS;
3967 goto out;
3968 }
3969
3970
3971 if (!mr_device_priv_data->is_tm_capable) {
3972 ret = FAILED;
3973 goto out;
3974 }
3975
3976 mutex_lock(&instance->reset_mutex);
3977
3978 smid = megasas_fusion_smid_lookup(scmd);
3979
3980 if (!smid) {
3981 ret = SUCCESS;
3982 scmd_printk(KERN_NOTICE, scmd, "Command for which abort is"
3983 " issued is not found in oustanding commands\n");
3984 mutex_unlock(&instance->reset_mutex);
3985 goto out;
3986 }
3987
3988 devhandle = megasas_get_tm_devhandle(scmd->device);
3989
3990 if (devhandle == (u16)ULONG_MAX) {
3991 ret = SUCCESS;
3992 sdev_printk(KERN_INFO, scmd->device,
3993 "task abort issued for invalid devhandle\n");
3994 mutex_unlock(&instance->reset_mutex);
3995 goto out;
3996 }
3997 sdev_printk(KERN_INFO, scmd->device,
3998 "attempting task abort! scmd(%p) tm_dev_handle 0x%x\n",
3999 scmd, devhandle);
4000
4001 mr_device_priv_data->tm_busy = 1;
4002 ret = megasas_issue_tm(instance, devhandle,
4003 scmd->device->channel, scmd->device->id, smid,
4004 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK);
4005 mr_device_priv_data->tm_busy = 0;
4006
4007 mutex_unlock(&instance->reset_mutex);
4008out:
4009 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n",
4010 ((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd);
4011
4012 return ret;
4013}
4014
4015/*
4016 * megasas_reset_target_fusion : target reset function for fusion adapters
4017 * scmd: SCSI command pointer
4018 *
4019 * Returns SUCCESS if all commands associated with target aborted else FAILED
4020 */
4021
4022int megasas_reset_target_fusion(struct scsi_cmnd *scmd)
4023{
4024
4025 struct megasas_instance *instance;
4026 int ret = FAILED;
4027 u16 devhandle;
4028 struct fusion_context *fusion;
4029 struct MR_PRIV_DEVICE *mr_device_priv_data;
4030 mr_device_priv_data = scmd->device->hostdata;
4031
4032 instance = (struct megasas_instance *)scmd->device->host->hostdata;
4033 fusion = instance->ctrl_context;
4034
Shivasharan Sdef0eab2017-02-10 00:59:15 -08004035 sdev_printk(KERN_INFO, scmd->device,
4036 "target reset called for scmd(%p)\n", scmd);
4037
Sumit Saxena8a01a412016-01-28 21:04:32 +05304038 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) {
Sumit Saxena18365b12016-01-28 21:04:25 +05304039 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL,"
4040 "SCSI host:%d\n", instance->host->host_no);
4041 ret = FAILED;
4042 return ret;
4043 }
4044
4045 if (!mr_device_priv_data) {
4046 sdev_printk(KERN_INFO, scmd->device, "device been deleted! "
4047 "scmd(%p)\n", scmd);
4048 scmd->result = DID_NO_CONNECT << 16;
4049 ret = SUCCESS;
4050 goto out;
4051 }
4052
4053
4054 if (!mr_device_priv_data->is_tm_capable) {
4055 ret = FAILED;
4056 goto out;
4057 }
4058
4059 mutex_lock(&instance->reset_mutex);
4060 devhandle = megasas_get_tm_devhandle(scmd->device);
4061
4062 if (devhandle == (u16)ULONG_MAX) {
4063 ret = SUCCESS;
4064 sdev_printk(KERN_INFO, scmd->device,
4065 "target reset issued for invalid devhandle\n");
4066 mutex_unlock(&instance->reset_mutex);
4067 goto out;
4068 }
4069
4070 sdev_printk(KERN_INFO, scmd->device,
4071 "attempting target reset! scmd(%p) tm_dev_handle 0x%x\n",
4072 scmd, devhandle);
4073 mr_device_priv_data->tm_busy = 1;
4074 ret = megasas_issue_tm(instance, devhandle,
4075 scmd->device->channel, scmd->device->id, 0,
4076 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
4077 mr_device_priv_data->tm_busy = 0;
4078 mutex_unlock(&instance->reset_mutex);
4079out:
4080 scmd_printk(KERN_NOTICE, scmd, "megasas: target reset %s!!\n",
4081 (ret == SUCCESS) ? "SUCCESS" : "FAILED");
4082
4083 return ret;
4084}
4085
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05304086/*SRIOV get other instance in cluster if any*/
4087struct megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance)
4088{
4089 int i;
4090
4091 for (i = 0; i < MAX_MGMT_ADAPTERS; i++) {
4092 if (megasas_mgmt_info.instance[i] &&
4093 (megasas_mgmt_info.instance[i] != instance) &&
4094 megasas_mgmt_info.instance[i]->requestorId &&
4095 megasas_mgmt_info.instance[i]->peerIsPresent &&
4096 (memcmp((megasas_mgmt_info.instance[i]->clusterId),
4097 instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0))
4098 return megasas_mgmt_info.instance[i];
4099 }
4100 return NULL;
4101}
4102
adam radford229fe472014-03-10 02:51:56 -07004103/* Check for a second path that is currently UP */
4104int megasas_check_mpio_paths(struct megasas_instance *instance,
4105 struct scsi_cmnd *scmd)
adam radford9c915a82010-12-21 13:34:31 -08004106{
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05304107 struct megasas_instance *peer_instance = NULL;
Shivasharan Sf55cf472017-02-10 00:59:07 -08004108 int retval = (DID_REQUEUE << 16);
adam radford229fe472014-03-10 02:51:56 -07004109
Sumit Saxena8f67c8c2016-01-28 21:14:25 +05304110 if (instance->peerIsPresent) {
4111 peer_instance = megasas_get_peer_instance(instance);
4112 if ((peer_instance) &&
4113 (atomic_read(&peer_instance->adprecovery) ==
4114 MEGASAS_HBA_OPERATIONAL))
4115 retval = (DID_NO_CONNECT << 16);
adam radford229fe472014-03-10 02:51:56 -07004116 }
adam radford229fe472014-03-10 02:51:56 -07004117 return retval;
4118}
4119
4120/* Core fusion reset function */
Sumit Saxena18365b12016-01-28 21:04:25 +05304121int megasas_reset_fusion(struct Scsi_Host *shost, int reason)
adam radford229fe472014-03-10 02:51:56 -07004122{
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05004123 int retval = SUCCESS, i, j, convert = 0;
adam radford9c915a82010-12-21 13:34:31 -08004124 struct megasas_instance *instance;
Shivasharan S8bf7c652017-02-10 00:59:03 -08004125 struct megasas_cmd_fusion *cmd_fusion, *r1_cmd;
adam radford9c915a82010-12-21 13:34:31 -08004126 struct fusion_context *fusion;
sumit.saxena@avagotech.com79b82c22015-08-31 17:23:41 +05304127 u32 abs_state, status_reg, reset_adapter;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304128 u32 io_timeout_in_crash_mode = 0;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304129 struct scsi_cmnd *scmd_local = NULL;
Sumit Saxena18365b12016-01-28 21:04:25 +05304130 struct scsi_device *sdev;
adam radford9c915a82010-12-21 13:34:31 -08004131
4132 instance = (struct megasas_instance *)shost->hostdata;
4133 fusion = instance->ctrl_context;
4134
adam radford229fe472014-03-10 02:51:56 -07004135 mutex_lock(&instance->reset_mutex);
4136
Sumit Saxena8a01a412016-01-28 21:04:32 +05304137 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004138 dev_warn(&instance->pdev->dev, "Hardware critical error, "
adam radford229fe472014-03-10 02:51:56 -07004139 "returning FAILED for scsi%d.\n",
4140 instance->host->host_no);
Adam Radforda2fbcbc2014-07-09 15:17:54 -07004141 mutex_unlock(&instance->reset_mutex);
adam radfordd4a759a2011-10-08 18:14:39 -07004142 return FAILED;
adam radford9c915a82010-12-21 13:34:31 -08004143 }
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304144 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
4145 abs_state = status_reg & MFI_STATE_MASK;
4146
4147 /* IO timeout detected, forcibly put FW in FAULT state */
4148 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
Sumit Saxena18365b12016-01-28 21:04:25 +05304149 instance->crash_dump_app_support && reason) {
4150 dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, "
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304151 "forcibly FAULT Firmware\n");
Sumit Saxena8a01a412016-01-28 21:04:32 +05304152 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304153 status_reg = readl(&instance->reg_set->doorbell);
4154 writel(status_reg | MFI_STATE_FORCE_OCR,
4155 &instance->reg_set->doorbell);
4156 readl(&instance->reg_set->doorbell);
4157 mutex_unlock(&instance->reset_mutex);
4158 do {
4159 ssleep(3);
4160 io_timeout_in_crash_mode++;
4161 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
4162 "seconds for crash dump collection and OCR "
4163 "to be done\n", (io_timeout_in_crash_mode * 3));
Sumit Saxena8a01a412016-01-28 21:04:32 +05304164 } while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) &&
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304165 (io_timeout_in_crash_mode < 80));
4166
Sumit Saxena8a01a412016-01-28 21:04:32 +05304167 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) {
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304168 dev_info(&instance->pdev->dev, "OCR done for IO "
4169 "timeout case\n");
4170 retval = SUCCESS;
4171 } else {
4172 dev_info(&instance->pdev->dev, "Controller is not "
4173 "operational after 240 seconds wait for IO "
4174 "timeout case in FW crash dump mode\n do "
4175 "OCR/kill adapter\n");
4176 retval = megasas_reset_fusion(shost, 0);
4177 }
4178 return retval;
4179 }
adam radford9c915a82010-12-21 13:34:31 -08004180
adam radford229fe472014-03-10 02:51:56 -07004181 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
4182 del_timer_sync(&instance->sriov_heartbeat_timer);
adam radford7e70e732011-05-11 18:34:08 -07004183 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
Sumit Saxena8a01a412016-01-28 21:04:32 +05304184 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304185 instance->instancet->disable_intr(instance);
Shivasharan S29206da2017-02-10 00:59:34 -08004186 megasas_sync_irqs((unsigned long)instance);
adam radford7e70e732011-05-11 18:34:08 -07004187
adam radford9c915a82010-12-21 13:34:31 -08004188 /* First try waiting for commands to complete */
Sumit Saxena18365b12016-01-28 21:04:25 +05304189 if (megasas_wait_for_outstanding_fusion(instance, reason,
adam radford229fe472014-03-10 02:51:56 -07004190 &convert)) {
Sumit Saxena8a01a412016-01-28 21:04:32 +05304191 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004192 dev_warn(&instance->pdev->dev, "resetting fusion "
adam radford229fe472014-03-10 02:51:56 -07004193 "adapter scsi%d.\n", instance->host->host_no);
4194 if (convert)
Sumit Saxena18365b12016-01-28 21:04:25 +05304195 reason = 0;
adam radford229fe472014-03-10 02:51:56 -07004196
Shivasharan Sdef0eab2017-02-10 00:59:15 -08004197 if (megasas_dbg_lvl & OCR_LOGS)
4198 dev_info(&instance->pdev->dev, "\nPending SCSI commands:\n");
4199
adam radford9c915a82010-12-21 13:34:31 -08004200 /* Now return commands back to the OS */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304201 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
adam radford9c915a82010-12-21 13:34:31 -08004202 cmd_fusion = fusion->cmd_list[i];
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05004203 /*check for extra commands issued by driver*/
4204 if (instance->is_ventura) {
Shivasharan S8bf7c652017-02-10 00:59:03 -08004205 r1_cmd = fusion->cmd_list[i + instance->max_fw_cmds];
4206 megasas_return_cmd_fusion(instance, r1_cmd);
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05004207 }
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304208 scmd_local = cmd_fusion->scmd;
adam radford9c915a82010-12-21 13:34:31 -08004209 if (cmd_fusion->scmd) {
Shivasharan Sdef0eab2017-02-10 00:59:15 -08004210 if (megasas_dbg_lvl & OCR_LOGS) {
4211 sdev_printk(KERN_INFO,
4212 cmd_fusion->scmd->device, "SMID: 0x%x\n",
4213 cmd_fusion->index);
4214 scsi_print_command(cmd_fusion->scmd);
4215 }
4216
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304217 scmd_local->result =
adam radford229fe472014-03-10 02:51:56 -07004218 megasas_check_mpio_paths(instance,
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304219 scmd_local);
Sasikumar Chandrasekaranb71b49c2017-01-10 18:20:51 -05004220 if (instance->ldio_threshold &&
4221 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)
Sumit Saxena308ec452016-01-28 21:04:30 +05304222 atomic_dec(&instance->ldio_outstanding);
adam radford9c915a82010-12-21 13:34:31 -08004223 megasas_return_cmd_fusion(instance, cmd_fusion);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05304224 scsi_dma_unmap(scmd_local);
4225 scmd_local->scsi_done(scmd_local);
adam radford9c915a82010-12-21 13:34:31 -08004226 }
4227 }
4228
Sasikumar Chandrasekaran69c337c2017-01-10 18:20:47 -05004229 atomic_set(&instance->fw_outstanding, 0);
4230
adam radford7e70e732011-05-11 18:34:08 -07004231 status_reg = instance->instancet->read_fw_status_reg(
4232 instance->reg_set);
4233 abs_state = status_reg & MFI_STATE_MASK;
4234 reset_adapter = status_reg & MFI_RESET_ADAPTER;
4235 if (instance->disableOnlineCtrlReset ||
4236 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
adam radford9c915a82010-12-21 13:34:31 -08004237 /* Reset not supported, kill adapter */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004238 dev_warn(&instance->pdev->dev, "Reset not supported"
adam radford229fe472014-03-10 02:51:56 -07004239 ", killing adapter scsi%d.\n",
4240 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08004241 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07004242 instance->skip_heartbeat_timer_del = 1;
adam radford9c915a82010-12-21 13:34:31 -08004243 retval = FAILED;
4244 goto out;
4245 }
4246
adam radford229fe472014-03-10 02:51:56 -07004247 /* Let SR-IOV VF & PF sync up if there was a HB failure */
Sumit Saxena18365b12016-01-28 21:04:25 +05304248 if (instance->requestorId && !reason) {
adam radford229fe472014-03-10 02:51:56 -07004249 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
Sumit Saxena3885c262016-01-28 21:04:35 +05304250 goto transition_to_ready;
adam radford229fe472014-03-10 02:51:56 -07004251 }
4252
adam radford9c915a82010-12-21 13:34:31 -08004253 /* Now try to reset the chip */
4254 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
adam radford9c915a82010-12-21 13:34:31 -08004255
sumit.saxena@avagotech.com79b82c22015-08-31 17:23:41 +05304256 if (instance->instancet->adp_reset
4257 (instance, instance->reg_set))
adam radford9c915a82010-12-21 13:34:31 -08004258 continue;
Sumit Saxena3885c262016-01-28 21:04:35 +05304259transition_to_ready:
adam radford9c915a82010-12-21 13:34:31 -08004260 /* Wait for FW to become ready */
adam radford058a8fa2011-10-08 18:14:27 -07004261 if (megasas_transition_to_ready(instance, 1)) {
Sumit Saxena3885c262016-01-28 21:04:35 +05304262 dev_warn(&instance->pdev->dev,
4263 "Failed to transition controller to ready for "
4264 "scsi%d.\n", instance->host->host_no);
4265 if (instance->requestorId && !reason)
4266 goto fail_kill_adapter;
4267 else
4268 continue;
adam radford9c915a82010-12-21 13:34:31 -08004269 }
adam radford9c915a82010-12-21 13:34:31 -08004270 megasas_reset_reply_desc(instance);
Sumit Saxena308ec452016-01-28 21:04:30 +05304271 megasas_fusion_update_can_queue(instance, OCR_CONTEXT);
4272
adam radford9c915a82010-12-21 13:34:31 -08004273 if (megasas_ioc_init_fusion(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004274 dev_warn(&instance->pdev->dev,
Sumit Saxena3885c262016-01-28 21:04:35 +05304275 "megasas_ioc_init_fusion() failed! for "
4276 "scsi%d\n", instance->host->host_no);
4277 if (instance->requestorId && !reason)
4278 goto fail_kill_adapter;
4279 else
4280 continue;
adam radford9c915a82010-12-21 13:34:31 -08004281 }
4282
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05304283 megasas_refire_mgmt_cmd(instance);
adam radford9c915a82010-12-21 13:34:31 -08004284
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304285 if (megasas_get_ctrl_info(instance)) {
4286 dev_info(&instance->pdev->dev,
4287 "Failed from %s %d\n",
4288 __func__, __LINE__);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304289 megaraid_sas_kill_hba(instance);
4290 retval = FAILED;
Shivasharan Sa6821ca2017-02-10 00:59:31 -08004291 goto out;
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05304292 }
adam radford9c915a82010-12-21 13:34:31 -08004293 /* Reset load balance info */
Shivasharan S5fc499b2017-02-10 00:59:17 -08004294 if (fusion->load_balance_info)
4295 memset(fusion->load_balance_info, 0,
4296 (sizeof(struct LD_LOAD_BALANCE_INFO) *
4297 MAX_LOGICAL_DRIVES_EXT));
adam radford9c915a82010-12-21 13:34:31 -08004298
4299 if (!megasas_get_map_info(instance))
4300 megasas_sync_map_info(instance);
4301
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05304302 megasas_setup_jbod_map(instance);
4303
Sumit Saxena18365b12016-01-28 21:04:25 +05304304 shost_for_each_device(sdev, shost)
Shivasharan S15dd0382017-02-10 00:59:10 -08004305 megasas_set_dynamic_target_properties(sdev);
Sumit Saxena18365b12016-01-28 21:04:25 +05304306
Sasikumar Chandrasekaranfdd84e22017-01-10 18:20:46 -05004307 /* reset stream detection array */
4308 if (instance->is_ventura) {
4309 for (j = 0; j < MAX_LOGICAL_DRIVES_EXT; ++j) {
4310 memset(fusion->stream_detect_by_ld[j],
4311 0, sizeof(struct LD_STREAM_DETECT));
4312 fusion->stream_detect_by_ld[j]->mru_bit_map
4313 = MR_STREAM_BITMAP;
4314 }
4315 }
4316
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05304317 clear_bit(MEGASAS_FUSION_IN_RESET,
4318 &instance->reset_flags);
4319 instance->instancet->enable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05304320 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05304321
adam radford229fe472014-03-10 02:51:56 -07004322 /* Restart SR-IOV heartbeat */
4323 if (instance->requestorId) {
4324 if (!megasas_sriov_start_heartbeat(instance, 0))
4325 megasas_start_timer(instance,
4326 &instance->sriov_heartbeat_timer,
4327 megasas_sriov_heartbeat_handler,
4328 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4329 else
4330 instance->skip_heartbeat_timer_del = 1;
4331 }
4332
adam radford9c915a82010-12-21 13:34:31 -08004333 /* Adapter reset completed successfully */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004334 dev_warn(&instance->pdev->dev, "Reset "
adam radford229fe472014-03-10 02:51:56 -07004335 "successful for scsi%d.\n",
4336 instance->host->host_no);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304337
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05304338 if (instance->crash_dump_drv_support &&
4339 instance->crash_dump_app_support)
4340 megasas_set_crash_dump_params(instance,
4341 MR_CRASH_BUF_TURN_ON);
4342 else
4343 megasas_set_crash_dump_params(instance,
4344 MR_CRASH_BUF_TURN_OFF);
4345
adam radford9c915a82010-12-21 13:34:31 -08004346 retval = SUCCESS;
4347 goto out;
4348 }
Sumit Saxena3885c262016-01-28 21:04:35 +05304349fail_kill_adapter:
adam radford9c915a82010-12-21 13:34:31 -08004350 /* Reset failed, kill the adapter */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05004351 dev_warn(&instance->pdev->dev, "Reset failed, killing "
adam radford229fe472014-03-10 02:51:56 -07004352 "adapter scsi%d.\n", instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08004353 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07004354 instance->skip_heartbeat_timer_del = 1;
adam radford9c915a82010-12-21 13:34:31 -08004355 retval = FAILED;
4356 } else {
adam radford229fe472014-03-10 02:51:56 -07004357 /* For VF: Restart HB timer if we didn't OCR */
4358 if (instance->requestorId) {
4359 megasas_start_timer(instance,
4360 &instance->sriov_heartbeat_timer,
4361 megasas_sriov_heartbeat_handler,
4362 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
4363 }
adam radford3f0e58bc2011-10-08 18:14:59 -07004364 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05304365 instance->instancet->enable_intr(instance);
Sumit Saxena8a01a412016-01-28 21:04:32 +05304366 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL);
adam radford9c915a82010-12-21 13:34:31 -08004367 }
4368out:
4369 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
4370 mutex_unlock(&instance->reset_mutex);
4371 return retval;
4372}
4373
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05304374/* Fusion Crash dump collection work queue */
4375void megasas_fusion_crash_dump_wq(struct work_struct *work)
4376{
4377 struct megasas_instance *instance =
4378 container_of(work, struct megasas_instance, crash_init);
4379 u32 status_reg;
4380 u8 partial_copy = 0;
4381
4382
4383 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
4384
4385 /*
4386 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
4387 * to host crash buffers
4388 */
4389 if (instance->drv_buf_index == 0) {
4390 /* Buffer is already allocated for old Crash dump.
4391 * Do OCR and do not wait for crash dump collection
4392 */
4393 if (instance->drv_buf_alloc) {
4394 dev_info(&instance->pdev->dev, "earlier crash dump is "
4395 "not yet copied by application, ignoring this "
4396 "crash dump and initiating OCR\n");
4397 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
4398 writel(status_reg,
4399 &instance->reg_set->outbound_scratch_pad);
4400 readl(&instance->reg_set->outbound_scratch_pad);
4401 return;
4402 }
4403 megasas_alloc_host_crash_buffer(instance);
4404 dev_info(&instance->pdev->dev, "Number of host crash buffers "
4405 "allocated: %d\n", instance->drv_buf_alloc);
4406 }
4407
4408 /*
4409 * Driver has allocated max buffers, which can be allocated
4410 * and FW has more crash dump data, then driver will
4411 * ignore the data.
4412 */
4413 if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
4414 dev_info(&instance->pdev->dev, "Driver is done copying "
4415 "the buffer: %d\n", instance->drv_buf_alloc);
4416 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
4417 partial_copy = 1;
4418 } else {
4419 memcpy(instance->crash_buf[instance->drv_buf_index],
4420 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
4421 instance->drv_buf_index++;
4422 status_reg &= ~MFI_STATE_DMADONE;
4423 }
4424
4425 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
4426 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
4427 "of copied buffers: %d\n", instance->drv_buf_index);
4428 instance->fw_crash_buffer_size = instance->drv_buf_index;
4429 instance->fw_crash_state = AVAILABLE;
4430 instance->drv_buf_index = 0;
4431 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
4432 readl(&instance->reg_set->outbound_scratch_pad);
4433 if (!partial_copy)
4434 megasas_reset_fusion(instance->host, 0);
4435 } else {
4436 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
4437 readl(&instance->reg_set->outbound_scratch_pad);
4438 }
4439}
4440
4441
adam radford9c915a82010-12-21 13:34:31 -08004442/* Fusion OCR work queue */
4443void megasas_fusion_ocr_wq(struct work_struct *work)
4444{
4445 struct megasas_instance *instance =
4446 container_of(work, struct megasas_instance, work_init);
4447
adam radford229fe472014-03-10 02:51:56 -07004448 megasas_reset_fusion(instance->host, 0);
adam radford9c915a82010-12-21 13:34:31 -08004449}
4450
Shivasharan S5fc499b2017-02-10 00:59:17 -08004451/* Allocate fusion context */
4452int
4453megasas_alloc_fusion_context(struct megasas_instance *instance)
4454{
4455 struct fusion_context *fusion;
4456
4457 instance->ctrl_context_pages = get_order(sizeof(struct fusion_context));
4458 instance->ctrl_context = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
4459 instance->ctrl_context_pages);
4460 if (!instance->ctrl_context) {
4461 /* fall back to using vmalloc for fusion_context */
4462 instance->ctrl_context = vzalloc(sizeof(struct fusion_context));
4463 if (!instance->ctrl_context) {
4464 dev_err(&instance->pdev->dev, "Failed from %s %d\n", __func__, __LINE__);
4465 return -ENOMEM;
4466 }
4467 }
4468
4469 fusion = instance->ctrl_context;
4470
4471 fusion->load_balance_info_pages = get_order(MAX_LOGICAL_DRIVES_EXT *
4472 sizeof(struct LD_LOAD_BALANCE_INFO));
4473 fusion->load_balance_info =
4474 (struct LD_LOAD_BALANCE_INFO *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
4475 fusion->load_balance_info_pages);
4476 if (!fusion->load_balance_info) {
4477 fusion->load_balance_info = vzalloc(MAX_LOGICAL_DRIVES_EXT *
4478 sizeof(struct LD_LOAD_BALANCE_INFO));
4479 if (!fusion->load_balance_info)
4480 dev_err(&instance->pdev->dev, "Failed to allocate load_balance_info, "
4481 "continuing without Load Balance support\n");
4482 }
4483
4484 return 0;
4485}
4486
4487void
4488megasas_free_fusion_context(struct megasas_instance *instance)
4489{
4490 struct fusion_context *fusion = instance->ctrl_context;
4491
4492 if (fusion) {
4493 if (fusion->load_balance_info) {
4494 if (is_vmalloc_addr(fusion->load_balance_info))
4495 vfree(fusion->load_balance_info);
4496 else
4497 free_pages((ulong)fusion->load_balance_info,
4498 fusion->load_balance_info_pages);
4499 }
4500
4501 if (is_vmalloc_addr(fusion))
4502 vfree(fusion);
4503 else
4504 free_pages((ulong)fusion,
4505 instance->ctrl_context_pages);
4506 }
4507}
4508
adam radford9c915a82010-12-21 13:34:31 -08004509struct megasas_instance_template megasas_instance_template_fusion = {
adam radford9c915a82010-12-21 13:34:31 -08004510 .enable_intr = megasas_enable_intr_fusion,
4511 .disable_intr = megasas_disable_intr_fusion,
4512 .clear_intr = megasas_clear_intr_fusion,
4513 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
4514 .adp_reset = megasas_adp_reset_fusion,
4515 .check_reset = megasas_check_reset_fusion,
4516 .service_isr = megasas_isr_fusion,
4517 .tasklet = megasas_complete_cmd_dpc_fusion,
4518 .init_adapter = megasas_init_adapter_fusion,
4519 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
4520 .issue_dcmd = megasas_issue_dcmd_fusion,
4521};