blob: 78e252acea7a75f51677230a0b3d10b68e9c698a [file] [log] [blame]
adam radford9c915a82010-12-21 13:34:31 -08001/*
2 * Linux MegaRAID driver for SAS based RAID controllers
3 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +05304 * Copyright (c) 2009-2013 LSI Corporation
5 * Copyright (c) 2013-2014 Avago Technologies
adam radford9c915a82010-12-21 13:34:31 -08006 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053018 * along with this program. If not, see <http://www.gnu.org/licenses/>.
adam radford9c915a82010-12-21 13:34:31 -080019 *
20 * FILE: megaraid_sas_fusion.c
21 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053022 * Authors: Avago Technologies
adam radford9c915a82010-12-21 13:34:31 -080023 * Sumant Patro
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053024 * Adam Radford
25 * Kashyap Desai <kashyap.desai@avagotech.com>
26 * Sumit Saxena <sumit.saxena@avagotech.com>
adam radford9c915a82010-12-21 13:34:31 -080027 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053028 * Send feedback to: megaraidlinux.pdl@avagotech.com
adam radford9c915a82010-12-21 13:34:31 -080029 *
Sumit.Saxena@avagotech.come3990652014-11-17 15:24:03 +053030 * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31 * San Jose, California 95131
adam radford9c915a82010-12-21 13:34:31 -080032 */
33
34#include <linux/kernel.h>
35#include <linux/types.h>
36#include <linux/pci.h>
37#include <linux/list.h>
38#include <linux/moduleparam.h>
39#include <linux/module.h>
40#include <linux/spinlock.h>
41#include <linux/interrupt.h>
42#include <linux/delay.h>
adam radford9c915a82010-12-21 13:34:31 -080043#include <linux/uio.h>
44#include <linux/uaccess.h>
45#include <linux/fs.h>
46#include <linux/compat.h>
47#include <linux/blkdev.h>
48#include <linux/mutex.h>
49#include <linux/poll.h>
50
51#include <scsi/scsi.h>
52#include <scsi/scsi_cmnd.h>
53#include <scsi/scsi_device.h>
54#include <scsi/scsi_host.h>
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +053055#include <scsi/scsi_dbg.h>
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +053056#include <linux/dmi.h>
adam radford9c915a82010-12-21 13:34:31 -080057
58#include "megaraid_sas_fusion.h"
59#include "megaraid_sas.h"
60
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +053061
adam radford9c915a82010-12-21 13:34:31 -080062extern void megasas_free_cmds(struct megasas_instance *instance);
63extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
64 *instance);
65extern void
66megasas_complete_cmd(struct megasas_instance *instance,
67 struct megasas_cmd *cmd, u8 alt_status);
adam radford9c915a82010-12-21 13:34:31 -080068int
adam radford229fe472014-03-10 02:51:56 -070069wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
70 int seconds);
adam radford9c915a82010-12-21 13:34:31 -080071
72void
73megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
74int megasas_alloc_cmds(struct megasas_instance *instance);
75int
76megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
77int
78megasas_issue_polled(struct megasas_instance *instance,
79 struct megasas_cmd *cmd);
adam radford53ef2bb2011-02-24 20:56:05 -080080void
81megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
82
adam radford058a8fa2011-10-08 18:14:27 -070083int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
adam radford9c915a82010-12-21 13:34:31 -080084void megaraid_sas_kill_hba(struct megasas_instance *instance);
85
86extern u32 megasas_dbg_lvl;
adam radford229fe472014-03-10 02:51:56 -070087void megasas_sriov_heartbeat_handler(unsigned long instance_addr);
88int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
89 int initial);
90void megasas_start_timer(struct megasas_instance *instance,
91 struct timer_list *timer,
92 void *fn, unsigned long interval);
93extern struct megasas_mgmt_info megasas_mgmt_info;
adam radfordc007b8b2012-07-17 18:20:24 -070094extern int resetwaittime;
adam radford9c915a82010-12-21 13:34:31 -080095
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +053096
97
adam radford9c915a82010-12-21 13:34:31 -080098/**
99 * megasas_enable_intr_fusion - Enables interrupts
100 * @regs: MFI register set
101 */
102void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530103megasas_enable_intr_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800104{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530105 struct megasas_register_set __iomem *regs;
106 regs = instance->reg_set;
Sumit.Saxena@avagotech.comc2ced172015-01-05 20:06:13 +0530107
108 instance->mask_interrupts = 0;
adam radford6497b242011-10-08 18:14:50 -0700109 /* For Thunderbolt/Invader also clear intr on enable */
110 writel(~0, &regs->outbound_intr_status);
111 readl(&regs->outbound_intr_status);
112
adam radford9c915a82010-12-21 13:34:31 -0800113 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
114
115 /* Dummy readl to force pci flush */
116 readl(&regs->outbound_intr_mask);
117}
118
119/**
120 * megasas_disable_intr_fusion - Disables interrupt
121 * @regs: MFI register set
122 */
123void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530124megasas_disable_intr_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800125{
126 u32 mask = 0xFFFFFFFF;
127 u32 status;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530128 struct megasas_register_set __iomem *regs;
129 regs = instance->reg_set;
130 instance->mask_interrupts = 1;
adam radford9c915a82010-12-21 13:34:31 -0800131
132 writel(mask, &regs->outbound_intr_mask);
133 /* Dummy readl to force pci flush */
134 status = readl(&regs->outbound_intr_mask);
135}
136
137int
138megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
139{
140 u32 status;
141 /*
142 * Check if it is our interrupt
143 */
144 status = readl(&regs->outbound_intr_status);
145
146 if (status & 1) {
147 writel(status, &regs->outbound_intr_status);
148 readl(&regs->outbound_intr_status);
149 return 1;
150 }
151 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
152 return 0;
153
adam radford9c915a82010-12-21 13:34:31 -0800154 return 1;
155}
156
157/**
158 * megasas_get_cmd_fusion - Get a command from the free pool
159 * @instance: Adapter soft state
160 *
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530161 * Returns a blk_tag indexed mpt frame
adam radford9c915a82010-12-21 13:34:31 -0800162 */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530163inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
164 *instance, u32 blk_tag)
adam radford9c915a82010-12-21 13:34:31 -0800165{
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530166 struct fusion_context *fusion;
adam radford9c915a82010-12-21 13:34:31 -0800167
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530168 fusion = instance->ctrl_context;
169 return fusion->cmd_list[blk_tag];
adam radford9c915a82010-12-21 13:34:31 -0800170}
171
172/**
173 * megasas_return_cmd_fusion - Return a cmd to free command pool
174 * @instance: Adapter soft state
175 * @cmd: Command packet to be returned to free command pool
176 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530177inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
178 struct megasas_cmd_fusion *cmd)
adam radford9c915a82010-12-21 13:34:31 -0800179{
adam radford9c915a82010-12-21 13:34:31 -0800180 cmd->scmd = NULL;
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +0530181 memset(cmd->io_request, 0, sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530182}
183
184/**
Christoph Hellwig01d7f032015-04-23 16:34:09 +0530185 * megasas_fire_cmd_fusion - Sends command to the FW
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530186 */
Christoph Hellwig01d7f032015-04-23 16:34:09 +0530187static void
188megasas_fire_cmd_fusion(struct megasas_instance *instance,
189 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc)
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530190{
Christoph Hellwig01d7f032015-04-23 16:34:09 +0530191#if defined(writeq) && defined(CONFIG_64BIT)
192 u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) |
193 le32_to_cpu(req_desc->u.low));
194
195 writeq(req_data, &instance->reg_set->inbound_low_queue_port);
196#else
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530197 unsigned long flags;
198
Christoph Hellwig01d7f032015-04-23 16:34:09 +0530199 spin_lock_irqsave(&instance->hba_lock, flags);
200 writel(le32_to_cpu(req_desc->u.low),
201 &instance->reg_set->inbound_low_queue_port);
202 writel(le32_to_cpu(req_desc->u.high),
203 &instance->reg_set->inbound_high_queue_port);
204 spin_unlock_irqrestore(&instance->hba_lock, flags);
205#endif
adam radford9c915a82010-12-21 13:34:31 -0800206}
207
Christoph Hellwig01d7f032015-04-23 16:34:09 +0530208
adam radford9c915a82010-12-21 13:34:31 -0800209/**
210 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
211 * @instance: Adapter soft state
212 */
213static void megasas_teardown_frame_pool_fusion(
214 struct megasas_instance *instance)
215{
216 int i;
217 struct fusion_context *fusion = instance->ctrl_context;
218
219 u16 max_cmd = instance->max_fw_cmds;
220
221 struct megasas_cmd_fusion *cmd;
222
223 if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500224 dev_err(&instance->pdev->dev, "dma pool is null. SG Pool %p, "
adam radford9c915a82010-12-21 13:34:31 -0800225 "sense pool : %p\n", fusion->sg_dma_pool,
226 fusion->sense_dma_pool);
227 return;
228 }
229
230 /*
231 * Return all frames to pool
232 */
233 for (i = 0; i < max_cmd; i++) {
234
235 cmd = fusion->cmd_list[i];
236
237 if (cmd->sg_frame)
238 pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
239 cmd->sg_frame_phys_addr);
240
241 if (cmd->sense)
242 pci_pool_free(fusion->sense_dma_pool, cmd->sense,
243 cmd->sense_phys_addr);
244 }
245
246 /*
247 * Now destroy the pool itself
248 */
249 pci_pool_destroy(fusion->sg_dma_pool);
250 pci_pool_destroy(fusion->sense_dma_pool);
251
252 fusion->sg_dma_pool = NULL;
253 fusion->sense_dma_pool = NULL;
254}
255
256/**
257 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
258 * @instance: Adapter soft state
259 */
260void
261megasas_free_cmds_fusion(struct megasas_instance *instance)
262{
263 int i;
264 struct fusion_context *fusion = instance->ctrl_context;
265
266 u32 max_cmds, req_sz, reply_sz, io_frames_sz;
267
268
269 req_sz = fusion->request_alloc_sz;
270 reply_sz = fusion->reply_alloc_sz;
271 io_frames_sz = fusion->io_frames_alloc_sz;
272
273 max_cmds = instance->max_fw_cmds;
274
275 /* Free descriptors and request Frames memory */
276 if (fusion->req_frames_desc)
277 dma_free_coherent(&instance->pdev->dev, req_sz,
278 fusion->req_frames_desc,
279 fusion->req_frames_desc_phys);
280
281 if (fusion->reply_frames_desc) {
282 pci_pool_free(fusion->reply_frames_desc_pool,
283 fusion->reply_frames_desc,
284 fusion->reply_frames_desc_phys);
285 pci_pool_destroy(fusion->reply_frames_desc_pool);
286 }
287
288 if (fusion->io_request_frames) {
289 pci_pool_free(fusion->io_request_frames_pool,
290 fusion->io_request_frames,
291 fusion->io_request_frames_phys);
292 pci_pool_destroy(fusion->io_request_frames_pool);
293 }
294
295 /* Free the Fusion frame pool */
296 megasas_teardown_frame_pool_fusion(instance);
297
298 /* Free all the commands in the cmd_list */
299 for (i = 0; i < max_cmds; i++)
300 kfree(fusion->cmd_list[i]);
301
302 /* Free the cmd_list buffer itself */
303 kfree(fusion->cmd_list);
304 fusion->cmd_list = NULL;
305
adam radford9c915a82010-12-21 13:34:31 -0800306}
307
308/**
309 * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
310 * @instance: Adapter soft state
311 *
312 */
313static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
314{
315 int i;
316 u32 max_cmd;
317 struct fusion_context *fusion;
318 struct megasas_cmd_fusion *cmd;
adam radford9c915a82010-12-21 13:34:31 -0800319
320 fusion = instance->ctrl_context;
321 max_cmd = instance->max_fw_cmds;
322
adam radford9c915a82010-12-21 13:34:31 -0800323
324 /*
325 * Use DMA pool facility provided by PCI layer
326 */
327
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +0530328 fusion->sg_dma_pool = pci_pool_create("sg_pool_fusion", instance->pdev,
329 instance->max_chain_frame_sz,
330 4, 0);
adam radford9c915a82010-12-21 13:34:31 -0800331 if (!fusion->sg_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500332 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup request pool fusion\n");
adam radford9c915a82010-12-21 13:34:31 -0800333 return -ENOMEM;
334 }
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +0530335 fusion->sense_dma_pool = pci_pool_create("sense pool fusion",
adam radford9c915a82010-12-21 13:34:31 -0800336 instance->pdev,
337 SCSI_SENSE_BUFFERSIZE, 64, 0);
338
339 if (!fusion->sense_dma_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500340 dev_printk(KERN_DEBUG, &instance->pdev->dev, "failed to setup sense pool fusion\n");
adam radford9c915a82010-12-21 13:34:31 -0800341 pci_pool_destroy(fusion->sg_dma_pool);
342 fusion->sg_dma_pool = NULL;
343 return -ENOMEM;
344 }
345
346 /*
347 * Allocate and attach a frame to each of the commands in cmd_list
348 */
349 for (i = 0; i < max_cmd; i++) {
350
351 cmd = fusion->cmd_list[i];
352
353 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
354 GFP_KERNEL,
355 &cmd->sg_frame_phys_addr);
356
357 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
358 GFP_KERNEL, &cmd->sense_phys_addr);
359 /*
360 * megasas_teardown_frame_pool_fusion() takes care of freeing
361 * whatever has been allocated
362 */
363 if (!cmd->sg_frame || !cmd->sense) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500364 dev_printk(KERN_DEBUG, &instance->pdev->dev, "pci_pool_alloc failed\n");
adam radford9c915a82010-12-21 13:34:31 -0800365 megasas_teardown_frame_pool_fusion(instance);
366 return -ENOMEM;
367 }
368 }
369 return 0;
370}
371
372/**
373 * megasas_alloc_cmds_fusion - Allocates the command packets
374 * @instance: Adapter soft state
375 *
376 *
377 * Each frame has a 32-bit field called context. This context is used to get
378 * back the megasas_cmd_fusion from the frame when a frame gets completed
379 * In this driver, the 32 bit values are the indices into an array cmd_list.
380 * This array is used only to look up the megasas_cmd_fusion given the context.
381 * The free commands themselves are maintained in a linked list called cmd_pool.
382 *
383 * cmds are formed in the io_request and sg_frame members of the
384 * megasas_cmd_fusion. The context field is used to get a request descriptor
385 * and is used as SMID of the cmd.
386 * SMID value range is from 1 to max_fw_cmds.
387 */
388int
389megasas_alloc_cmds_fusion(struct megasas_instance *instance)
390{
adam radfordc8e858f2011-10-08 18:15:13 -0700391 int i, j, count;
adam radford9c915a82010-12-21 13:34:31 -0800392 u32 max_cmd, io_frames_sz;
393 struct fusion_context *fusion;
394 struct megasas_cmd_fusion *cmd;
395 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
396 u32 offset;
397 dma_addr_t io_req_base_phys;
398 u8 *io_req_base;
399
400 fusion = instance->ctrl_context;
401
402 max_cmd = instance->max_fw_cmds;
403
404 fusion->req_frames_desc =
405 dma_alloc_coherent(&instance->pdev->dev,
406 fusion->request_alloc_sz,
407 &fusion->req_frames_desc_phys, GFP_KERNEL);
408
409 if (!fusion->req_frames_desc) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500410 dev_err(&instance->pdev->dev, "Could not allocate memory for "
adam radford9c915a82010-12-21 13:34:31 -0800411 "request_frames\n");
412 goto fail_req_desc;
413 }
414
adam radfordc8e858f2011-10-08 18:15:13 -0700415 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
adam radford9c915a82010-12-21 13:34:31 -0800416 fusion->reply_frames_desc_pool =
417 pci_pool_create("reply_frames pool", instance->pdev,
adam radfordc8e858f2011-10-08 18:15:13 -0700418 fusion->reply_alloc_sz * count, 16, 0);
adam radford9c915a82010-12-21 13:34:31 -0800419
420 if (!fusion->reply_frames_desc_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500421 dev_err(&instance->pdev->dev, "Could not allocate memory for "
adam radford9c915a82010-12-21 13:34:31 -0800422 "reply_frame pool\n");
423 goto fail_reply_desc;
424 }
425
426 fusion->reply_frames_desc =
427 pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
428 &fusion->reply_frames_desc_phys);
429 if (!fusion->reply_frames_desc) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500430 dev_err(&instance->pdev->dev, "Could not allocate memory for "
adam radford9c915a82010-12-21 13:34:31 -0800431 "reply_frame pool\n");
432 pci_pool_destroy(fusion->reply_frames_desc_pool);
433 goto fail_reply_desc;
434 }
435
436 reply_desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -0700437 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +0530438 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
adam radford9c915a82010-12-21 13:34:31 -0800439
440 io_frames_sz = fusion->io_frames_alloc_sz;
441
442 fusion->io_request_frames_pool =
443 pci_pool_create("io_request_frames pool", instance->pdev,
444 fusion->io_frames_alloc_sz, 16, 0);
445
446 if (!fusion->io_request_frames_pool) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500447 dev_err(&instance->pdev->dev, "Could not allocate memory for "
adam radford9c915a82010-12-21 13:34:31 -0800448 "io_request_frame pool\n");
449 goto fail_io_frames;
450 }
451
452 fusion->io_request_frames =
453 pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
454 &fusion->io_request_frames_phys);
455 if (!fusion->io_request_frames) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500456 dev_err(&instance->pdev->dev, "Could not allocate memory for "
adam radford9c915a82010-12-21 13:34:31 -0800457 "io_request_frames frames\n");
458 pci_pool_destroy(fusion->io_request_frames_pool);
459 goto fail_io_frames;
460 }
461
462 /*
463 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
464 * Allocate the dynamic array first and then allocate individual
465 * commands.
466 */
Fengguang Wu7731e6b2012-08-24 23:27:35 +0800467 fusion->cmd_list = kzalloc(sizeof(struct megasas_cmd_fusion *)
468 * max_cmd, GFP_KERNEL);
adam radford9c915a82010-12-21 13:34:31 -0800469
470 if (!fusion->cmd_list) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500471 dev_printk(KERN_DEBUG, &instance->pdev->dev, "out of memory. Could not alloc "
adam radford9c915a82010-12-21 13:34:31 -0800472 "memory for cmd_list_fusion\n");
473 goto fail_cmd_list;
474 }
475
adam radford9c915a82010-12-21 13:34:31 -0800476 max_cmd = instance->max_fw_cmds;
477 for (i = 0; i < max_cmd; i++) {
478 fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
479 GFP_KERNEL);
480 if (!fusion->cmd_list[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500481 dev_err(&instance->pdev->dev, "Could not alloc cmd list fusion\n");
adam radford9c915a82010-12-21 13:34:31 -0800482
483 for (j = 0; j < i; j++)
484 kfree(fusion->cmd_list[j]);
485
486 kfree(fusion->cmd_list);
487 fusion->cmd_list = NULL;
488 goto fail_cmd_list;
489 }
490 }
491
492 /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
493 io_req_base = fusion->io_request_frames +
494 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
495 io_req_base_phys = fusion->io_request_frames_phys +
496 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
497
498 /*
499 * Add all the commands to command pool (fusion->cmd_pool)
500 */
501
502 /* SMID 0 is reserved. Set SMID/index from 1 */
503 for (i = 0; i < max_cmd; i++) {
504 cmd = fusion->cmd_list[i];
505 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
506 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
507 cmd->index = i + 1;
508 cmd->scmd = NULL;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530509 cmd->sync_cmd_idx = (i >= instance->max_scsi_cmds) ?
510 (i - instance->max_scsi_cmds) :
511 (u32)ULONG_MAX; /* Set to Invalid */
adam radford9c915a82010-12-21 13:34:31 -0800512 cmd->instance = instance;
513 cmd->io_request =
514 (struct MPI2_RAID_SCSI_IO_REQUEST *)
515 (io_req_base + offset);
516 memset(cmd->io_request, 0,
517 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
518 cmd->io_request_phys_addr = io_req_base_phys + offset;
adam radford9c915a82010-12-21 13:34:31 -0800519 }
520
521 /*
522 * Create a frame pool and assign one frame to each cmd
523 */
524 if (megasas_create_frame_pool_fusion(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500525 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Error creating frame DMA pool\n");
adam radford9c915a82010-12-21 13:34:31 -0800526 megasas_free_cmds_fusion(instance);
527 goto fail_req_desc;
528 }
529
530 return 0;
531
532fail_cmd_list:
533 pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
534 fusion->io_request_frames_phys);
535 pci_pool_destroy(fusion->io_request_frames_pool);
536fail_io_frames:
537 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
538 fusion->reply_frames_desc,
539 fusion->reply_frames_desc_phys);
540 pci_pool_free(fusion->reply_frames_desc_pool,
541 fusion->reply_frames_desc,
542 fusion->reply_frames_desc_phys);
543 pci_pool_destroy(fusion->reply_frames_desc_pool);
544
545fail_reply_desc:
546 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
547 fusion->req_frames_desc,
548 fusion->req_frames_desc_phys);
549fail_req_desc:
550 return -ENOMEM;
551}
552
553/**
554 * wait_and_poll - Issues a polling command
555 * @instance: Adapter soft state
556 * @cmd: Command packet to be issued
557 *
558 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
559 */
560int
adam radford229fe472014-03-10 02:51:56 -0700561wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
562 int seconds)
adam radford9c915a82010-12-21 13:34:31 -0800563{
564 int i;
565 struct megasas_header *frame_hdr = &cmd->frame->hdr;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530566 struct fusion_context *fusion;
adam radford9c915a82010-12-21 13:34:31 -0800567
adam radford229fe472014-03-10 02:51:56 -0700568 u32 msecs = seconds * 1000;
adam radford9c915a82010-12-21 13:34:31 -0800569
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530570 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -0800571 /*
572 * Wait for cmd_status to change
573 */
574 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
575 rmb();
576 msleep(20);
577 }
578
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530579 if (frame_hdr->cmd_status == 0xff)
adam radford9c915a82010-12-21 13:34:31 -0800580 return -ETIME;
581
Sumit.Saxena@avagotech.com2be2a982015-05-06 19:01:02 +0530582 return (frame_hdr->cmd_status == MFI_STAT_OK) ?
583 0 : 1;
adam radford9c915a82010-12-21 13:34:31 -0800584}
585
586/**
587 * megasas_ioc_init_fusion - Initializes the FW
588 * @instance: Adapter soft state
589 *
590 * Issues the IOC Init cmd
591 */
592int
593megasas_ioc_init_fusion(struct megasas_instance *instance)
594{
595 struct megasas_init_frame *init_frame;
596 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
597 dma_addr_t ioc_init_handle;
adam radford9c915a82010-12-21 13:34:31 -0800598 struct megasas_cmd *cmd;
599 u8 ret;
600 struct fusion_context *fusion;
adam radfordc77a9bd2014-03-10 02:51:36 -0700601 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
adam radford9c915a82010-12-21 13:34:31 -0800602 int i;
603 struct megasas_header *frame_hdr;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +0530604 const char *sys_info;
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530605 MFI_CAPABILITIES *drv_ops;
adam radford9c915a82010-12-21 13:34:31 -0800606
607 fusion = instance->ctrl_context;
608
609 cmd = megasas_get_cmd(instance);
610
611 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500612 dev_err(&instance->pdev->dev, "Could not allocate cmd for INIT Frame\n");
adam radford9c915a82010-12-21 13:34:31 -0800613 ret = 1;
614 goto fail_get_cmd;
615 }
616
617 IOCInitMessage =
618 dma_alloc_coherent(&instance->pdev->dev,
619 sizeof(struct MPI2_IOC_INIT_REQUEST),
620 &ioc_init_handle, GFP_KERNEL);
621
622 if (!IOCInitMessage) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500623 dev_err(&instance->pdev->dev, "Could not allocate memory for "
adam radford9c915a82010-12-21 13:34:31 -0800624 "IOCInitMessage\n");
625 ret = 1;
626 goto fail_fw_init;
627 }
628
629 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
630
631 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
632 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530633 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
634 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
635 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
adam radford9c915a82010-12-21 13:34:31 -0800636
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530637 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
638 IOCInitMessage->ReplyDescriptorPostQueueAddress = cpu_to_le64(fusion->reply_frames_desc_phys);
639 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
adam radford5738f992012-03-19 19:49:53 -0700640 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
adam radford9c915a82010-12-21 13:34:31 -0800641 init_frame = (struct megasas_init_frame *)cmd->frame;
642 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
643
644 frame_hdr = &cmd->frame->hdr;
adam radford9c915a82010-12-21 13:34:31 -0800645 frame_hdr->cmd_status = 0xFF;
Christoph Hellwig48433bd2015-04-23 16:33:39 +0530646 frame_hdr->flags = cpu_to_le16(
647 le16_to_cpu(frame_hdr->flags) |
648 MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
adam radford9c915a82010-12-21 13:34:31 -0800649
650 init_frame->cmd = MFI_CMD_INIT;
651 init_frame->cmd_status = 0xFF;
652
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530653 drv_ops = (MFI_CAPABILITIES *) &(init_frame->driver_operations);
654
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530655 /* driver support Extended MSIX */
656 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
657 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530658 drv_ops->mfi_capabilities.support_additional_msix = 1;
adam radford21c9e162013-09-06 15:27:14 -0700659 /* driver supports HA / Remote LUN over Fast Path interface */
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530660 drv_ops->mfi_capabilities.support_fp_remote_lun = 1;
661
662 drv_ops->mfi_capabilities.support_max_255lds = 1;
663 drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1;
664 drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1;
665
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +0530666 if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
667 drv_ops->mfi_capabilities.support_ext_io_size = 1;
668
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530669 /* Convert capability to LE32 */
670 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
adam radford21c9e162013-09-06 15:27:14 -0700671
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +0530672 sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
673 if (instance->system_info_buf && sys_info) {
674 memcpy(instance->system_info_buf->systemId, sys_info,
675 strlen(sys_info) > 64 ? 64 : strlen(sys_info));
676 instance->system_info_buf->systemIdLength =
677 strlen(sys_info) > 64 ? 64 : strlen(sys_info);
678 init_frame->system_info_lo = instance->system_info_h;
679 init_frame->system_info_hi = 0;
680 }
681
Hannes Reineckefdc5a972014-01-16 11:25:33 +0100682 init_frame->queue_info_new_phys_addr_hi =
683 cpu_to_le32(upper_32_bits(ioc_init_handle));
684 init_frame->queue_info_new_phys_addr_lo =
685 cpu_to_le32(lower_32_bits(ioc_init_handle));
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530686 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
adam radford9c915a82010-12-21 13:34:31 -0800687
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +0530688 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
689 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
adam radfordc77a9bd2014-03-10 02:51:36 -0700690 req_desc.MFAIo.RequestFlags =
adam radford9c915a82010-12-21 13:34:31 -0800691 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +0530692 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford9c915a82010-12-21 13:34:31 -0800693
694 /*
695 * disable the intr before firing the init frame
696 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530697 instance->instancet->disable_intr(instance);
adam radford9c915a82010-12-21 13:34:31 -0800698
699 for (i = 0; i < (10 * 1000); i += 20) {
700 if (readl(&instance->reg_set->doorbell) & 1)
701 msleep(20);
702 else
703 break;
704 }
705
Christoph Hellwig01d7f032015-04-23 16:34:09 +0530706 megasas_fire_cmd_fusion(instance, &req_desc);
adam radford9c915a82010-12-21 13:34:31 -0800707
adam radford229fe472014-03-10 02:51:56 -0700708 wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -0800709
710 frame_hdr = &cmd->frame->hdr;
711 if (frame_hdr->cmd_status != 0) {
712 ret = 1;
713 goto fail_fw_init;
714 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500715 dev_err(&instance->pdev->dev, "Init cmd success\n");
adam radford9c915a82010-12-21 13:34:31 -0800716
717 ret = 0;
718
719fail_fw_init:
720 megasas_return_cmd(instance, cmd);
721 if (IOCInitMessage)
722 dma_free_coherent(&instance->pdev->dev,
723 sizeof(struct MPI2_IOC_INIT_REQUEST),
724 IOCInitMessage, ioc_init_handle);
725fail_get_cmd:
726 return ret;
727}
728
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +0530729/**
730 * megasas_sync_pd_seq_num - JBOD SEQ MAP
731 * @instance: Adapter soft state
732 * @pend: set to 1, if it is pended jbod map.
733 *
734 * Issue Jbod map to the firmware. If it is pended command,
735 * issue command and return. If it is first instance of jbod map
736 * issue and receive command.
737 */
738int
739megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) {
740 int ret = 0;
741 u32 pd_seq_map_sz;
742 struct megasas_cmd *cmd;
743 struct megasas_dcmd_frame *dcmd;
744 struct fusion_context *fusion = instance->ctrl_context;
745 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
746 dma_addr_t pd_seq_h;
747
748 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)];
749 pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)];
750 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
751 (sizeof(struct MR_PD_CFG_SEQ) *
752 (MAX_PHYSICAL_DEVICES - 1));
753
754 cmd = megasas_get_cmd(instance);
755 if (!cmd) {
756 dev_err(&instance->pdev->dev,
757 "Could not get mfi cmd. Fail from %s %d\n",
758 __func__, __LINE__);
759 return -ENOMEM;
760 }
761
762 dcmd = &cmd->frame->dcmd;
763
764 memset(pd_sync, 0, pd_seq_map_sz);
765 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
766 dcmd->cmd = MFI_CMD_DCMD;
767 dcmd->cmd_status = 0xFF;
768 dcmd->sge_count = 1;
769 dcmd->timeout = 0;
770 dcmd->pad_0 = 0;
771 dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz);
772 dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
773 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(pd_seq_h);
774 dcmd->sgl.sge32[0].length = cpu_to_le32(pd_seq_map_sz);
775
776 if (pend) {
777 dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG;
778 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
779 instance->jbod_seq_cmd = cmd;
780 instance->instancet->issue_dcmd(instance, cmd);
781 return 0;
782 }
783
784 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
785
786 /* Below code is only for non pended DCMD */
787 if (instance->ctrl_context && !instance->mask_interrupts)
788 ret = megasas_issue_blocked_cmd(instance, cmd, 60);
789 else
790 ret = megasas_issue_polled(instance, cmd);
791
792 if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
793 dev_warn(&instance->pdev->dev,
794 "driver supports max %d JBOD, but FW reports %d\n",
795 MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count));
796 ret = -EINVAL;
797 }
798
799 if (!ret)
800 instance->pd_seq_map_id++;
801
802 megasas_return_cmd(instance, cmd);
803 return ret;
804}
805
adam radford9c915a82010-12-21 13:34:31 -0800806/*
adam radford9c915a82010-12-21 13:34:31 -0800807 * megasas_get_ld_map_info - Returns FW's ld_map structure
808 * @instance: Adapter soft state
809 * @pend: Pend the command or not
810 * Issues an internal command (DCMD) to get the FW's controller PD
811 * list structure. This information is mainly used to find out SYSTEM
812 * supported by the FW.
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530813 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
814 * dcmd.mbox.b[0] - number of LDs being sync'd
815 * dcmd.mbox.b[1] - 0 - complete command immediately.
816 * - 1 - pend till config change
817 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
818 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
819 * uses extended struct MR_FW_RAID_MAP_EXT
adam radford9c915a82010-12-21 13:34:31 -0800820 */
821static int
822megasas_get_ld_map_info(struct megasas_instance *instance)
823{
824 int ret = 0;
825 struct megasas_cmd *cmd;
826 struct megasas_dcmd_frame *dcmd;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530827 void *ci;
adam radford9c915a82010-12-21 13:34:31 -0800828 dma_addr_t ci_h = 0;
829 u32 size_map_info;
830 struct fusion_context *fusion;
831
832 cmd = megasas_get_cmd(instance);
833
834 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500835 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n");
adam radford9c915a82010-12-21 13:34:31 -0800836 return -ENOMEM;
837 }
838
839 fusion = instance->ctrl_context;
840
841 if (!fusion) {
842 megasas_return_cmd(instance, cmd);
Hannes Reinecke2f8bdfa2014-01-16 11:25:35 +0100843 return -ENXIO;
adam radford9c915a82010-12-21 13:34:31 -0800844 }
845
846 dcmd = &cmd->frame->dcmd;
847
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530848 size_map_info = fusion->current_map_sz;
adam radford9c915a82010-12-21 13:34:31 -0800849
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530850 ci = (void *) fusion->ld_map[(instance->map_id & 1)];
adam radford9c915a82010-12-21 13:34:31 -0800851 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
852
853 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500854 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n");
adam radford9c915a82010-12-21 13:34:31 -0800855 megasas_return_cmd(instance, cmd);
856 return -ENOMEM;
857 }
858
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530859 memset(ci, 0, fusion->max_map_sz);
adam radford9c915a82010-12-21 13:34:31 -0800860 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530861#if VD_EXT_DEBUG
862 dev_dbg(&instance->pdev->dev,
863 "%s sending MR_DCMD_LD_MAP_GET_INFO with size %d\n",
864 __func__, cpu_to_le32(size_map_info));
865#endif
adam radford9c915a82010-12-21 13:34:31 -0800866 dcmd->cmd = MFI_CMD_DCMD;
867 dcmd->cmd_status = 0xFF;
868 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530869 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford9c915a82010-12-21 13:34:31 -0800870 dcmd->timeout = 0;
871 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530872 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
873 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
874 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
875 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -0800876
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530877 if (instance->ctrl_context && !instance->mask_interrupts)
878 ret = megasas_issue_blocked_cmd(instance, cmd,
879 MEGASAS_BLOCKED_CMD_TIMEOUT);
880 else
881 ret = megasas_issue_polled(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -0800882
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530883 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -0800884
885 return ret;
886}
887
888u8
889megasas_get_map_info(struct megasas_instance *instance)
890{
891 struct fusion_context *fusion = instance->ctrl_context;
892
893 fusion->fast_path_io = 0;
894 if (!megasas_get_ld_map_info(instance)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +0530895 if (MR_ValidateMapInfo(instance)) {
adam radford9c915a82010-12-21 13:34:31 -0800896 fusion->fast_path_io = 1;
897 return 0;
898 }
899 }
900 return 1;
901}
902
903/*
904 * megasas_sync_map_info - Returns FW's ld_map structure
905 * @instance: Adapter soft state
906 *
907 * Issues an internal command (DCMD) to get the FW's controller PD
908 * list structure. This information is mainly used to find out SYSTEM
909 * supported by the FW.
910 */
911int
912megasas_sync_map_info(struct megasas_instance *instance)
913{
914 int ret = 0, i;
915 struct megasas_cmd *cmd;
916 struct megasas_dcmd_frame *dcmd;
917 u32 size_sync_info, num_lds;
918 struct fusion_context *fusion;
919 struct MR_LD_TARGET_SYNC *ci = NULL;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530920 struct MR_DRV_RAID_MAP_ALL *map;
adam radford9c915a82010-12-21 13:34:31 -0800921 struct MR_LD_RAID *raid;
922 struct MR_LD_TARGET_SYNC *ld_sync;
923 dma_addr_t ci_h = 0;
924 u32 size_map_info;
925
926 cmd = megasas_get_cmd(instance);
927
928 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500929 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n");
adam radford9c915a82010-12-21 13:34:31 -0800930 return -ENOMEM;
931 }
932
933 fusion = instance->ctrl_context;
934
935 if (!fusion) {
936 megasas_return_cmd(instance, cmd);
937 return 1;
938 }
939
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530940 map = fusion->ld_drv_map[instance->map_id & 1];
adam radford9c915a82010-12-21 13:34:31 -0800941
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +0530942 num_lds = le16_to_cpu(map->raidMap.ldCount);
adam radford9c915a82010-12-21 13:34:31 -0800943
944 dcmd = &cmd->frame->dcmd;
945
946 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
947
948 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
949
950 ci = (struct MR_LD_TARGET_SYNC *)
951 fusion->ld_map[(instance->map_id - 1) & 1];
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530952 memset(ci, 0, fusion->max_map_sz);
adam radford9c915a82010-12-21 13:34:31 -0800953
954 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
955
956 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
957
958 for (i = 0; i < num_lds; i++, ld_sync++) {
959 raid = MR_LdRaidGet(i, map);
960 ld_sync->targetId = MR_GetLDTgtId(i, map);
961 ld_sync->seqNum = raid->seqNum;
962 }
963
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530964 size_map_info = fusion->current_map_sz;
adam radford9c915a82010-12-21 13:34:31 -0800965
966 dcmd->cmd = MFI_CMD_DCMD;
967 dcmd->cmd_status = 0xFF;
968 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530969 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
adam radford9c915a82010-12-21 13:34:31 -0800970 dcmd->timeout = 0;
971 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530972 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -0800973 dcmd->mbox.b[0] = num_lds;
974 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530975 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
976 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
977 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -0800978
979 instance->map_update_cmd = cmd;
980
981 instance->instancet->issue_dcmd(instance, cmd);
982
983 return ret;
984}
985
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +0530986/*
987 * meagasas_display_intel_branding - Display branding string
988 * @instance: per adapter object
989 *
990 * Return nothing.
991 */
992static void
993megasas_display_intel_branding(struct megasas_instance *instance)
994{
995 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
996 return;
997
998 switch (instance->pdev->device) {
999 case PCI_DEVICE_ID_LSI_INVADER:
1000 switch (instance->pdev->subsystem_device) {
1001 case MEGARAID_INTEL_RS3DC080_SSDID:
1002 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1003 instance->host->host_no,
1004 MEGARAID_INTEL_RS3DC080_BRANDING);
1005 break;
1006 case MEGARAID_INTEL_RS3DC040_SSDID:
1007 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1008 instance->host->host_no,
1009 MEGARAID_INTEL_RS3DC040_BRANDING);
1010 break;
1011 case MEGARAID_INTEL_RS3SC008_SSDID:
1012 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1013 instance->host->host_no,
1014 MEGARAID_INTEL_RS3SC008_BRANDING);
1015 break;
1016 case MEGARAID_INTEL_RS3MC044_SSDID:
1017 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1018 instance->host->host_no,
1019 MEGARAID_INTEL_RS3MC044_BRANDING);
1020 break;
1021 default:
1022 break;
1023 }
1024 break;
1025 case PCI_DEVICE_ID_LSI_FURY:
1026 switch (instance->pdev->subsystem_device) {
1027 case MEGARAID_INTEL_RS3WC080_SSDID:
1028 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1029 instance->host->host_no,
1030 MEGARAID_INTEL_RS3WC080_BRANDING);
1031 break;
1032 case MEGARAID_INTEL_RS3WC040_SSDID:
1033 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1034 instance->host->host_no,
1035 MEGARAID_INTEL_RS3WC040_BRANDING);
1036 break;
1037 default:
1038 break;
1039 }
1040 break;
1041 default:
1042 break;
1043 }
1044}
1045
adam radford9c915a82010-12-21 13:34:31 -08001046/**
1047 * megasas_init_adapter_fusion - Initializes the FW
1048 * @instance: Adapter soft state
1049 *
1050 * This is the main function for initializing firmware.
1051 */
1052u32
1053megasas_init_adapter_fusion(struct megasas_instance *instance)
1054{
1055 struct megasas_register_set __iomem *reg_set;
1056 struct fusion_context *fusion;
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301057 u32 max_cmd, scratch_pad_2;
adam radfordc8e858f2011-10-08 18:15:13 -07001058 int i = 0, count;
adam radford9c915a82010-12-21 13:34:31 -08001059
1060 fusion = instance->ctrl_context;
1061
1062 reg_set = instance->reg_set;
1063
1064 /*
1065 * Get various operational parameters from status register
1066 */
1067 instance->max_fw_cmds =
1068 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
1069 instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
1070
1071 /*
1072 * Reduce the max supported cmds by 1. This is to ensure that the
1073 * reply_q_sz (1 more than the max cmd that driver may send)
1074 * does not exceed max cmds that the FW can support
1075 */
1076 instance->max_fw_cmds = instance->max_fw_cmds-1;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05301077
1078 /*
1079 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
1080 */
1081 instance->max_mfi_cmds =
1082 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
adam radford9c915a82010-12-21 13:34:31 -08001083
1084 max_cmd = instance->max_fw_cmds;
1085
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05301086 fusion->reply_q_depth = 2 * (((max_cmd + 1 + 15)/16)*16);
adam radford9c915a82010-12-21 13:34:31 -08001087
1088 fusion->request_alloc_sz =
1089 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
1090 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
1091 *(fusion->reply_q_depth);
1092 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
1093 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
1094 (max_cmd + 1)); /* Extra 1 for SMID 0 */
1095
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301096 scratch_pad_2 = readl(&instance->reg_set->outbound_scratch_pad_2);
1097 /* If scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
1098 * Firmware support extended IO chain frame which is 4 times more than
1099 * legacy Firmware.
1100 * Legacy Firmware - Frame size is (8 * 128) = 1K
1101 * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
1102 */
1103 if (scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
1104 instance->max_chain_frame_sz =
1105 ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1106 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
1107 else
1108 instance->max_chain_frame_sz =
1109 ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1110 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;
1111
1112 if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
1113 dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
1114 instance->max_chain_frame_sz,
1115 MEGASAS_CHAIN_FRAME_SZ_MIN);
1116 instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
1117 }
1118
adam radford9c915a82010-12-21 13:34:31 -08001119 fusion->max_sge_in_main_msg =
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301120 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1121 - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
adam radford9c915a82010-12-21 13:34:31 -08001122
1123 fusion->max_sge_in_chain =
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301124 instance->max_chain_frame_sz
1125 / sizeof(union MPI2_SGE_IO_UNION);
adam radford9c915a82010-12-21 13:34:31 -08001126
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301127 instance->max_num_sge =
1128 rounddown_pow_of_two(fusion->max_sge_in_main_msg
1129 + fusion->max_sge_in_chain - 2);
adam radford9c915a82010-12-21 13:34:31 -08001130
1131 /* Used for pass thru MFI frame (DCMD) */
1132 fusion->chain_offset_mfi_pthru =
1133 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1134
1135 fusion->chain_offset_io_request =
1136 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1137 sizeof(union MPI2_SGE_IO_UNION))/16;
1138
adam radfordc8e858f2011-10-08 18:15:13 -07001139 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1140 for (i = 0 ; i < count; i++)
1141 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -08001142
1143 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05301144 * For fusion adapters, 3 commands for IOCTL and 5 commands
1145 * for driver's internal DCMDs.
1146 */
1147 instance->max_scsi_cmds = instance->max_fw_cmds -
1148 (MEGASAS_FUSION_INTERNAL_CMDS +
1149 MEGASAS_FUSION_IOCTL_CMDS);
1150 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1151
1152 /*
adam radford9c915a82010-12-21 13:34:31 -08001153 * Allocate memory for descriptors
1154 * Create a pool of commands
1155 */
1156 if (megasas_alloc_cmds(instance))
1157 goto fail_alloc_mfi_cmds;
1158 if (megasas_alloc_cmds_fusion(instance))
1159 goto fail_alloc_cmds;
1160
1161 if (megasas_ioc_init_fusion(instance))
1162 goto fail_ioc_init;
1163
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301164 megasas_display_intel_branding(instance);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05301165 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301166 dev_err(&instance->pdev->dev,
1167 "Could not get controller info. Fail from %s %d\n",
1168 __func__, __LINE__);
1169 goto fail_ioc_init;
1170 }
1171
adam radford9c915a82010-12-21 13:34:31 -08001172 instance->flag_ieee = 1;
adam radford9c915a82010-12-21 13:34:31 -08001173 fusion->fast_path_io = 0;
1174
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301175 fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1176 for (i = 0; i < 2; i++) {
1177 fusion->ld_map[i] = NULL;
1178 fusion->ld_drv_map[i] = (void *)__get_free_pages(GFP_KERNEL,
1179 fusion->drv_map_pages);
1180 if (!fusion->ld_drv_map[i]) {
1181 dev_err(&instance->pdev->dev, "Could not allocate "
1182 "memory for local map info for %d pages\n",
1183 fusion->drv_map_pages);
1184 if (i == 1)
1185 free_pages((ulong)fusion->ld_drv_map[0],
1186 fusion->drv_map_pages);
1187 goto fail_ioc_init;
1188 }
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05301189 memset(fusion->ld_drv_map[i], 0,
1190 ((1 << PAGE_SHIFT) << fusion->drv_map_pages));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301191 }
1192
adam radford9c915a82010-12-21 13:34:31 -08001193 for (i = 0; i < 2; i++) {
1194 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301195 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08001196 &fusion->ld_map_phys[i],
1197 GFP_KERNEL);
1198 if (!fusion->ld_map[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001199 dev_err(&instance->pdev->dev, "Could not allocate memory "
adam radford9c915a82010-12-21 13:34:31 -08001200 "for map info\n");
1201 goto fail_map_info;
1202 }
1203 }
1204
1205 if (!megasas_get_map_info(instance))
1206 megasas_sync_map_info(instance);
1207
1208 return 0;
1209
adam radford9c915a82010-12-21 13:34:31 -08001210fail_map_info:
1211 if (i == 1)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301212 dma_free_coherent(&instance->pdev->dev, fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08001213 fusion->ld_map[0], fusion->ld_map_phys[0]);
1214fail_ioc_init:
adam radfordeb1b1232011-02-24 20:55:56 -08001215 megasas_free_cmds_fusion(instance);
1216fail_alloc_cmds:
1217 megasas_free_cmds(instance);
1218fail_alloc_mfi_cmds:
adam radford9c915a82010-12-21 13:34:31 -08001219 return 1;
1220}
1221
1222/**
adam radford9c915a82010-12-21 13:34:31 -08001223 * map_cmd_status - Maps FW cmd status to OS cmd status
1224 * @cmd : Pointer to cmd
1225 * @status : status of cmd returned by FW
1226 * @ext_status : ext status of cmd returned by FW
1227 */
1228
1229void
1230map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1231{
1232
1233 switch (status) {
1234
1235 case MFI_STAT_OK:
1236 cmd->scmd->result = DID_OK << 16;
1237 break;
1238
1239 case MFI_STAT_SCSI_IO_FAILED:
1240 case MFI_STAT_LD_INIT_IN_PROGRESS:
1241 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1242 break;
1243
1244 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1245
1246 cmd->scmd->result = (DID_OK << 16) | ext_status;
1247 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1248 memset(cmd->scmd->sense_buffer, 0,
1249 SCSI_SENSE_BUFFERSIZE);
1250 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1251 SCSI_SENSE_BUFFERSIZE);
1252 cmd->scmd->result |= DRIVER_SENSE << 24;
1253 }
1254 break;
1255
1256 case MFI_STAT_LD_OFFLINE:
1257 case MFI_STAT_DEVICE_NOT_FOUND:
1258 cmd->scmd->result = DID_BAD_TARGET << 16;
1259 break;
adam radford36807e62011-10-08 18:15:06 -07001260 case MFI_STAT_CONFIG_SEQ_MISMATCH:
1261 cmd->scmd->result = DID_IMM_RETRY << 16;
1262 break;
adam radford9c915a82010-12-21 13:34:31 -08001263 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001264 dev_printk(KERN_DEBUG, &cmd->instance->pdev->dev, "FW status %#x\n", status);
adam radford9c915a82010-12-21 13:34:31 -08001265 cmd->scmd->result = DID_ERROR << 16;
1266 break;
1267 }
1268}
1269
1270/**
1271 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1272 * @instance: Adapter soft state
1273 * @scp: SCSI command from the mid-layer
1274 * @sgl_ptr: SGL to be filled in
1275 * @cmd: cmd we are working on
1276 *
1277 * If successful, this function returns the number of SG elements.
1278 */
1279static int
1280megasas_make_sgl_fusion(struct megasas_instance *instance,
1281 struct scsi_cmnd *scp,
1282 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1283 struct megasas_cmd_fusion *cmd)
1284{
adam radford36807e62011-10-08 18:15:06 -07001285 int i, sg_processed, sge_count;
adam radford9c915a82010-12-21 13:34:31 -08001286 struct scatterlist *os_sgl;
1287 struct fusion_context *fusion;
1288
1289 fusion = instance->ctrl_context;
1290
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301291 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1292 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001293 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1294 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1295 sgl_ptr_end->Flags = 0;
1296 }
adam radford9c915a82010-12-21 13:34:31 -08001297
1298 sge_count = scsi_dma_map(scp);
1299
1300 BUG_ON(sge_count < 0);
1301
1302 if (sge_count > instance->max_num_sge || !sge_count)
1303 return sge_count;
1304
adam radford9c915a82010-12-21 13:34:31 -08001305 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301306 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
1307 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
adam radford9c915a82010-12-21 13:34:31 -08001308 sgl_ptr->Flags = 0;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301309 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1310 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001311 if (i == sge_count - 1)
1312 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1313 }
adam radford9c915a82010-12-21 13:34:31 -08001314 sgl_ptr++;
1315
1316 sg_processed = i + 1;
1317
1318 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1319 (sge_count > fusion->max_sge_in_main_msg)) {
1320
1321 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301322 if ((instance->pdev->device ==
1323 PCI_DEVICE_ID_LSI_INVADER) ||
1324 (instance->pdev->device ==
1325 PCI_DEVICE_ID_LSI_FURY)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301326 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1327 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1328 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
adam radford36807e62011-10-08 18:15:06 -07001329 cmd->io_request->ChainOffset =
1330 fusion->
1331 chain_offset_io_request;
1332 else
1333 cmd->io_request->ChainOffset = 0;
1334 } else
1335 cmd->io_request->ChainOffset =
1336 fusion->chain_offset_io_request;
1337
adam radford9c915a82010-12-21 13:34:31 -08001338 sg_chain = sgl_ptr;
1339 /* Prepare chain element */
1340 sg_chain->NextChainOffset = 0;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301341 if ((instance->pdev->device ==
1342 PCI_DEVICE_ID_LSI_INVADER) ||
1343 (instance->pdev->device ==
1344 PCI_DEVICE_ID_LSI_FURY))
adam radford36807e62011-10-08 18:15:06 -07001345 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1346 else
1347 sg_chain->Flags =
1348 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1349 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301350 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
1351 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08001352
1353 sgl_ptr =
1354 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301355 memset(sgl_ptr, 0, instance->max_chain_frame_sz);
adam radford9c915a82010-12-21 13:34:31 -08001356 }
1357 }
1358
1359 return sge_count;
1360}
1361
1362/**
1363 * megasas_set_pd_lba - Sets PD LBA
1364 * @cdb: CDB
1365 * @cdb_len: cdb length
1366 * @start_blk: Start block of IO
1367 *
1368 * Used to set the PD LBA in CDB for FP IOs
1369 */
1370void
1371megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1372 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301373 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
adam radford9c915a82010-12-21 13:34:31 -08001374{
1375 struct MR_LD_RAID *raid;
1376 u32 ld;
1377 u64 start_blk = io_info->pdBlock;
1378 u8 *cdb = io_request->CDB.CDB32;
1379 u32 num_blocks = io_info->numBlocks;
adam radford495c5602011-05-11 18:34:40 -07001380 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
adam radford9c915a82010-12-21 13:34:31 -08001381
1382 /* Check if T10 PI (DIF) is enabled for this LD */
1383 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1384 raid = MR_LdRaidGet(ld, local_map_ptr);
1385 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1386 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1387 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1388 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1389
1390 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1391 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1392 else
1393 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1394 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1395
1396 /* LBA */
1397 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1398 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1399 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1400 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1401 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1402 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1403 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1404 cdb[19] = (u8)(start_blk & 0xff);
1405
1406 /* Logical block reference tag */
1407 io_request->CDB.EEDP32.PrimaryReferenceTag =
1408 cpu_to_be32(ref_tag);
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301409 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301410 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001411
1412 /* Transfer length */
1413 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1414 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1415 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1416 cdb[31] = (u8)(num_blocks & 0xff);
1417
1418 /* set SCSI IO EEDPFlags */
1419 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301420 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001421 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1422 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1423 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1424 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301425 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
adam radford9c915a82010-12-21 13:34:31 -08001426 } else {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301427 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001428 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301429 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
adam radford9c915a82010-12-21 13:34:31 -08001430 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301431 io_request->Control |= cpu_to_le32((0x4 << 26));
1432 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
adam radford9c915a82010-12-21 13:34:31 -08001433 } else {
1434 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1435 if (((cdb_len == 12) || (cdb_len == 16)) &&
1436 (start_blk <= 0xffffffff)) {
1437 if (cdb_len == 16) {
1438 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1439 flagvals = cdb[1];
1440 groupnum = cdb[14];
1441 control = cdb[15];
1442 } else {
1443 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1444 flagvals = cdb[1];
1445 groupnum = cdb[10];
1446 control = cdb[11];
1447 }
1448
1449 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1450
1451 cdb[0] = opcode;
1452 cdb[1] = flagvals;
1453 cdb[6] = groupnum;
1454 cdb[9] = control;
1455
1456 /* Transfer length */
1457 cdb[8] = (u8)(num_blocks & 0xff);
1458 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1459
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301460 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001461 cdb_len = 10;
adam radford495c5602011-05-11 18:34:40 -07001462 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1463 /* Convert to 16 byte CDB for large LBA's */
1464 switch (cdb_len) {
1465 case 6:
1466 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1467 control = cdb[5];
1468 break;
1469 case 10:
1470 opcode =
1471 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1472 flagvals = cdb[1];
1473 groupnum = cdb[6];
1474 control = cdb[9];
1475 break;
1476 case 12:
1477 opcode =
1478 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1479 flagvals = cdb[1];
1480 groupnum = cdb[10];
1481 control = cdb[11];
1482 break;
1483 }
1484
1485 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1486
1487 cdb[0] = opcode;
1488 cdb[1] = flagvals;
1489 cdb[14] = groupnum;
1490 cdb[15] = control;
1491
1492 /* Transfer length */
1493 cdb[13] = (u8)(num_blocks & 0xff);
1494 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1495 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1496 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1497
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301498 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
adam radford495c5602011-05-11 18:34:40 -07001499 cdb_len = 16;
adam radford9c915a82010-12-21 13:34:31 -08001500 }
1501
1502 /* Normal case, just load LBA here */
1503 switch (cdb_len) {
1504 case 6:
1505 {
1506 u8 val = cdb[1] & 0xE0;
1507 cdb[3] = (u8)(start_blk & 0xff);
1508 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1509 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1510 break;
1511 }
1512 case 10:
1513 cdb[5] = (u8)(start_blk & 0xff);
1514 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1515 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1516 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1517 break;
1518 case 12:
1519 cdb[5] = (u8)(start_blk & 0xff);
1520 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1521 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1522 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1523 break;
1524 case 16:
1525 cdb[9] = (u8)(start_blk & 0xff);
1526 cdb[8] = (u8)((start_blk >> 8) & 0xff);
1527 cdb[7] = (u8)((start_blk >> 16) & 0xff);
1528 cdb[6] = (u8)((start_blk >> 24) & 0xff);
1529 cdb[5] = (u8)((start_blk >> 32) & 0xff);
1530 cdb[4] = (u8)((start_blk >> 40) & 0xff);
1531 cdb[3] = (u8)((start_blk >> 48) & 0xff);
1532 cdb[2] = (u8)((start_blk >> 56) & 0xff);
1533 break;
1534 }
1535 }
1536}
1537
1538/**
1539 * megasas_build_ldio_fusion - Prepares IOs to devices
1540 * @instance: Adapter soft state
1541 * @scp: SCSI command
1542 * @cmd: Command to be prepared
1543 *
1544 * Prepares the io_request and chain elements (sg_frame) for IO
1545 * The IO can be for PD (Fast Path) or LD
1546 */
1547void
1548megasas_build_ldio_fusion(struct megasas_instance *instance,
1549 struct scsi_cmnd *scp,
1550 struct megasas_cmd_fusion *cmd)
1551{
1552 u8 fp_possible;
adam radfordf9eff812012-10-01 19:27:07 -07001553 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
adam radford9c915a82010-12-21 13:34:31 -08001554 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1555 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1556 struct IO_REQUEST_INFO io_info;
1557 struct fusion_context *fusion;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301558 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
adam radford21c9e162013-09-06 15:27:14 -07001559 u8 *raidLUN;
adam radford9c915a82010-12-21 13:34:31 -08001560
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301561 device_id = MEGASAS_DEV_INDEX(scp);
adam radford9c915a82010-12-21 13:34:31 -08001562
1563 fusion = instance->ctrl_context;
1564
1565 io_request = cmd->io_request;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301566 io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
adam radford9c915a82010-12-21 13:34:31 -08001567 io_request->RaidContext.status = 0;
1568 io_request->RaidContext.exStatus = 0;
1569
1570 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1571
1572 start_lba_lo = 0;
1573 start_lba_hi = 0;
1574 fp_possible = 0;
1575
1576 /*
1577 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1578 */
1579 if (scp->cmd_len == 6) {
adam radfordf9eff812012-10-01 19:27:07 -07001580 datalength = (u32) scp->cmnd[4];
adam radford9c915a82010-12-21 13:34:31 -08001581 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1582 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1583
1584 start_lba_lo &= 0x1FFFFF;
1585 }
1586
1587 /*
1588 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1589 */
1590 else if (scp->cmd_len == 10) {
adam radfordf9eff812012-10-01 19:27:07 -07001591 datalength = (u32) scp->cmnd[8] |
adam radford9c915a82010-12-21 13:34:31 -08001592 ((u32) scp->cmnd[7] << 8);
1593 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1594 ((u32) scp->cmnd[3] << 16) |
1595 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1596 }
1597
1598 /*
1599 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1600 */
1601 else if (scp->cmd_len == 12) {
adam radfordf9eff812012-10-01 19:27:07 -07001602 datalength = ((u32) scp->cmnd[6] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08001603 ((u32) scp->cmnd[7] << 16) |
1604 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1605 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1606 ((u32) scp->cmnd[3] << 16) |
1607 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1608 }
1609
1610 /*
1611 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1612 */
1613 else if (scp->cmd_len == 16) {
adam radfordf9eff812012-10-01 19:27:07 -07001614 datalength = ((u32) scp->cmnd[10] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08001615 ((u32) scp->cmnd[11] << 16) |
1616 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1617 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1618 ((u32) scp->cmnd[7] << 16) |
1619 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1620
1621 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1622 ((u32) scp->cmnd[3] << 16) |
1623 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1624 }
1625
1626 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1627 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
adam radfordf9eff812012-10-01 19:27:07 -07001628 io_info.numBlocks = datalength;
adam radford9c915a82010-12-21 13:34:31 -08001629 io_info.ldTgtId = device_id;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301630 io_request->DataLength = cpu_to_le32(scsi_bufflen(scp));
adam radford9c915a82010-12-21 13:34:31 -08001631
1632 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1633 io_info.isRead = 1;
1634
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301635 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
adam radford9c915a82010-12-21 13:34:31 -08001636
1637 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301638 instance->fw_supported_vd_count) || (!fusion->fast_path_io)) {
adam radford9c915a82010-12-21 13:34:31 -08001639 io_request->RaidContext.regLockFlags = 0;
1640 fp_possible = 0;
1641 } else {
adam radford36807e62011-10-08 18:15:06 -07001642 if (MR_BuildRaidContext(instance, &io_info,
1643 &io_request->RaidContext,
adam radford21c9e162013-09-06 15:27:14 -07001644 local_map_ptr, &raidLUN))
adam radford9c915a82010-12-21 13:34:31 -08001645 fp_possible = io_info.fpOkForIo;
1646 }
1647
Christoph Hellwig16b85282015-04-15 09:44:37 -07001648 /* Use raw_smp_processor_id() for now until cmd->request->cpu is CPU
adam radfordc8e858f2011-10-08 18:15:13 -07001649 id by default, not CPU group id, otherwise all MSI-X queues won't
1650 be utilized */
1651 cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
Christoph Hellwig16b85282015-04-15 09:44:37 -07001652 raw_smp_processor_id() % instance->msix_vectors : 0;
adam radfordc8e858f2011-10-08 18:15:13 -07001653
adam radford9c915a82010-12-21 13:34:31 -08001654 if (fp_possible) {
1655 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1656 local_map_ptr, start_lba_lo);
adam radford9c915a82010-12-21 13:34:31 -08001657 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1658 cmd->request_desc->SCSIIO.RequestFlags =
1659 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1660 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301661 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1662 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001663 if (io_request->RaidContext.regLockFlags ==
1664 REGION_TYPE_UNUSED)
1665 cmd->request_desc->SCSIIO.RequestFlags =
1666 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1667 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1668 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1669 io_request->RaidContext.nseg = 0x1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301670 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
adam radford36807e62011-10-08 18:15:06 -07001671 io_request->RaidContext.regLockFlags |=
1672 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
1673 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1674 }
adam radford9c915a82010-12-21 13:34:31 -08001675 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1676 (io_info.isRead)) {
1677 io_info.devHandle =
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05301678 get_updated_dev_handle(instance,
adam radford9c915a82010-12-21 13:34:31 -08001679 &fusion->load_balance_info[device_id],
1680 &io_info);
1681 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05301682 cmd->pd_r1_lb = io_info.pd_after_lb;
adam radford9c915a82010-12-21 13:34:31 -08001683 } else
1684 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05301685
1686 if ((raidLUN[0] == 1) &&
1687 (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 2)) {
1688 instance->dev_handle = !(instance->dev_handle);
1689 io_info.devHandle =
1690 local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
1691 }
1692
adam radford9c915a82010-12-21 13:34:31 -08001693 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1694 io_request->DevHandle = io_info.devHandle;
adam radford21c9e162013-09-06 15:27:14 -07001695 /* populate the LUN field */
1696 memcpy(io_request->LUN, raidLUN, 8);
adam radford9c915a82010-12-21 13:34:31 -08001697 } else {
1698 io_request->RaidContext.timeoutValue =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301699 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
adam radford9c915a82010-12-21 13:34:31 -08001700 cmd->request_desc->SCSIIO.RequestFlags =
1701 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1702 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301703 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1704 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001705 if (io_request->RaidContext.regLockFlags ==
1706 REGION_TYPE_UNUSED)
1707 cmd->request_desc->SCSIIO.RequestFlags =
1708 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1709 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1710 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1711 io_request->RaidContext.regLockFlags |=
1712 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
1713 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1714 io_request->RaidContext.nseg = 0x1;
1715 }
1716 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301717 io_request->DevHandle = cpu_to_le16(device_id);
adam radford9c915a82010-12-21 13:34:31 -08001718 } /* Not FP */
1719}
1720
1721/**
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301722 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
adam radford9c915a82010-12-21 13:34:31 -08001723 * @instance: Adapter soft state
1724 * @scp: SCSI command
1725 * @cmd: Command to be prepared
1726 *
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301727 * Prepares the io_request frame for non-rw io cmds for vd.
adam radford9c915a82010-12-21 13:34:31 -08001728 */
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301729static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
1730 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
adam radford9c915a82010-12-21 13:34:31 -08001731{
1732 u32 device_id;
1733 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1734 u16 pd_index = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301735 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
adam radford9c915a82010-12-21 13:34:31 -08001736 struct fusion_context *fusion = instance->ctrl_context;
adam radford21c9e162013-09-06 15:27:14 -07001737 u8 span, physArm;
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05301738 __le16 devHandle;
adam radford21c9e162013-09-06 15:27:14 -07001739 u32 ld, arRef, pd;
1740 struct MR_LD_RAID *raid;
1741 struct RAID_CONTEXT *pRAID_Context;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301742 u8 fp_possible = 1;
adam radford9c915a82010-12-21 13:34:31 -08001743
1744 io_request = cmd->io_request;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301745 device_id = MEGASAS_DEV_INDEX(scmd);
1746 pd_index = MEGASAS_PD_INDEX(scmd);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301747 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301748 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301749 /* get RAID_Context pointer */
1750 pRAID_Context = &io_request->RaidContext;
1751 /* Check with FW team */
1752 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
1753 pRAID_Context->regLockRowLBA = 0;
1754 pRAID_Context->regLockLength = 0;
adam radford21c9e162013-09-06 15:27:14 -07001755
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301756 if (fusion->fast_path_io && (
1757 device_id < instance->fw_supported_vd_count)) {
Sumit.Saxena@avagotech.comab2f0602015-01-05 20:06:08 +05301758
adam radford21c9e162013-09-06 15:27:14 -07001759 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
Sumit.Saxena@avagotech.comab2f0602015-01-05 20:06:08 +05301760 if (ld >= instance->fw_supported_vd_count)
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301761 fp_possible = 0;
adam radford21c9e162013-09-06 15:27:14 -07001762
1763 raid = MR_LdRaidGet(ld, local_map_ptr);
adam radford21c9e162013-09-06 15:27:14 -07001764 if (!(raid->capability.fpNonRWCapable))
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301765 fp_possible = 0;
1766 } else
1767 fp_possible = 0;
adam radford21c9e162013-09-06 15:27:14 -07001768
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301769 if (!fp_possible) {
1770 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1771 io_request->DevHandle = cpu_to_le16(device_id);
1772 io_request->LUN[1] = scmd->device->lun;
1773 pRAID_Context->timeoutValue =
1774 cpu_to_le16 (scmd->request->timeout / HZ);
1775 cmd->request_desc->SCSIIO.RequestFlags =
1776 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1777 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1778 } else {
adam radford21c9e162013-09-06 15:27:14 -07001779
1780 /* set RAID context values */
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301781 pRAID_Context->configSeqNum = raid->seqNum;
1782 pRAID_Context->regLockFlags = REGION_TYPE_SHARED_READ;
1783 pRAID_Context->timeoutValue = cpu_to_le16(raid->fpIoTimeoutForLd);
adam radford21c9e162013-09-06 15:27:14 -07001784
1785 /* get the DevHandle for the PD (since this is
1786 fpNonRWCapable, this is a single disk RAID0) */
1787 span = physArm = 0;
1788 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
1789 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
1790 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
1791
1792 /* build request descriptor */
1793 cmd->request_desc->SCSIIO.RequestFlags =
1794 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301795 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford21c9e162013-09-06 15:27:14 -07001796 cmd->request_desc->SCSIIO.DevHandle = devHandle;
1797
1798 /* populate the LUN field */
1799 memcpy(io_request->LUN, raid->LUN, 8);
1800
1801 /* build the raidScsiIO structure */
1802 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1803 io_request->DevHandle = devHandle;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301804 }
1805}
adam radford21c9e162013-09-06 15:27:14 -07001806
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301807/**
1808 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
1809 * @instance: Adapter soft state
1810 * @scp: SCSI command
1811 * @cmd: Command to be prepared
1812 * @fp_possible: parameter to detect fast path or firmware path io.
1813 *
1814 * Prepares the io_request frame for rw/non-rw io cmds for syspds
1815 */
1816static void
1817megasas_build_syspd_fusion(struct megasas_instance *instance,
1818 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd, u8 fp_possible)
1819{
1820 u32 device_id;
1821 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1822 u16 pd_index = 0;
1823 u16 os_timeout_value;
1824 u16 timeout_limit;
1825 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1826 struct RAID_CONTEXT *pRAID_Context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301827 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301828 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301829 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
adam radford21c9e162013-09-06 15:27:14 -07001830
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301831 device_id = MEGASAS_DEV_INDEX(scmd);
1832 pd_index = MEGASAS_PD_INDEX(scmd);
1833 os_timeout_value = scmd->request->timeout / HZ;
1834
1835 io_request = cmd->io_request;
1836 /* get RAID_Context pointer */
1837 pRAID_Context = &io_request->RaidContext;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301838 pRAID_Context->regLockFlags = 0;
1839 pRAID_Context->regLockRowLBA = 0;
1840 pRAID_Context->regLockLength = 0;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301841 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
1842 io_request->LUN[1] = scmd->device->lun;
1843 pRAID_Context->RAIDFlags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
1844 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1845
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301846 /* If FW supports PD sequence number */
1847 if (instance->use_seqnum_jbod_fp &&
1848 instance->pd_list[pd_index].driveType == TYPE_DISK) {
1849 /* TgtId must be incremented by 255 as jbod seq number is index
1850 * below raid map
1851 */
1852 pRAID_Context->VirtualDiskTgtId =
1853 cpu_to_le16(device_id + (MAX_PHYSICAL_DEVICES - 1));
1854 pRAID_Context->configSeqNum = pd_sync->seq[pd_index].seqNum;
1855 io_request->DevHandle = pd_sync->seq[pd_index].devHandle;
1856 pRAID_Context->regLockFlags |=
1857 (MR_RL_FLAGS_SEQ_NUM_ENABLE|MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
1858 } else if (fusion->fast_path_io) {
1859 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
1860 pRAID_Context->configSeqNum = 0;
1861 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1862 io_request->DevHandle =
1863 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1864 } else {
1865 /* Want to send all IO via FW path */
1866 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
1867 pRAID_Context->configSeqNum = 0;
1868 io_request->DevHandle = le16_to_cpu(0xFFFF);
1869 }
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301870
1871 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
1872 cmd->request_desc->SCSIIO.MSIxIndex =
1873 instance->msix_vectors ?
1874 (raw_smp_processor_id() % instance->msix_vectors) : 0;
1875
1876
1877 if (!fp_possible) {
1878 /* system pd firmware path */
adam radford9c915a82010-12-21 13:34:31 -08001879 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
adam radford9c915a82010-12-21 13:34:31 -08001880 cmd->request_desc->SCSIIO.RequestFlags =
1881 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301882 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1883 pRAID_Context->timeoutValue = cpu_to_le16(os_timeout_value);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301884 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301885 } else {
1886 /* system pd Fast Path */
1887 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301888 timeout_limit = (scmd->device->type == TYPE_DISK) ?
1889 255 : 0xFFFF;
1890 pRAID_Context->timeoutValue =
1891 cpu_to_le16((os_timeout_value > timeout_limit) ?
1892 timeout_limit : os_timeout_value);
1893 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1894 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301895 pRAID_Context->Type = MPI2_TYPE_CUDA;
1896 pRAID_Context->nseg = 0x1;
1897 io_request->IoFlags |=
1898 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
1899 }
1900 cmd->request_desc->SCSIIO.RequestFlags =
1901 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1902 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford9c915a82010-12-21 13:34:31 -08001903 }
adam radford9c915a82010-12-21 13:34:31 -08001904}
1905
1906/**
1907 * megasas_build_io_fusion - Prepares IOs to devices
1908 * @instance: Adapter soft state
1909 * @scp: SCSI command
1910 * @cmd: Command to be prepared
1911 *
1912 * Invokes helper functions to prepare request frames
1913 * and sets flags appropriate for IO/Non-IO cmd
1914 */
1915int
1916megasas_build_io_fusion(struct megasas_instance *instance,
1917 struct scsi_cmnd *scp,
1918 struct megasas_cmd_fusion *cmd)
1919{
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301920 u16 sge_count;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301921 u8 cmd_type;
adam radford9c915a82010-12-21 13:34:31 -08001922 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1923
adam radford9c915a82010-12-21 13:34:31 -08001924 /* Zero out some fields so they don't get reused */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001925 memset(io_request->LUN, 0x0, 8);
adam radford9c915a82010-12-21 13:34:31 -08001926 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1927 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1928 io_request->EEDPFlags = 0;
1929 io_request->Control = 0;
1930 io_request->EEDPBlockSize = 0;
adam radford36807e62011-10-08 18:15:06 -07001931 io_request->ChainOffset = 0;
adam radford9c915a82010-12-21 13:34:31 -08001932 io_request->RaidContext.RAIDFlags = 0;
adam radford36807e62011-10-08 18:15:06 -07001933 io_request->RaidContext.Type = 0;
1934 io_request->RaidContext.nseg = 0;
adam radford9c915a82010-12-21 13:34:31 -08001935
1936 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1937 /*
1938 * Just the CDB length,rest of the Flags are zero
1939 * This will be modified for FP in build_ldio_fusion
1940 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301941 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
adam radford9c915a82010-12-21 13:34:31 -08001942
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301943 switch (cmd_type = megasas_cmd_type(scp)) {
1944 case READ_WRITE_LDIO:
adam radford9c915a82010-12-21 13:34:31 -08001945 megasas_build_ldio_fusion(instance, scp, cmd);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301946 break;
1947 case NON_READ_WRITE_LDIO:
1948 megasas_build_ld_nonrw_fusion(instance, scp, cmd);
1949 break;
1950 case READ_WRITE_SYSPDIO:
1951 case NON_READ_WRITE_SYSPDIO:
1952 if (instance->secure_jbod_support &&
1953 (cmd_type == NON_READ_WRITE_SYSPDIO))
1954 megasas_build_syspd_fusion(instance, scp, cmd, 0);
1955 else
1956 megasas_build_syspd_fusion(instance, scp, cmd, 1);
1957 break;
1958 default:
1959 break;
1960 }
adam radford9c915a82010-12-21 13:34:31 -08001961
1962 /*
1963 * Construct SGL
1964 */
1965
1966 sge_count =
1967 megasas_make_sgl_fusion(instance, scp,
1968 (struct MPI25_IEEE_SGE_CHAIN64 *)
1969 &io_request->SGL, cmd);
1970
1971 if (sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001972 dev_err(&instance->pdev->dev, "Error. sge_count (0x%x) exceeds "
adam radford9c915a82010-12-21 13:34:31 -08001973 "max (0x%x) allowed\n", sge_count,
1974 instance->max_num_sge);
1975 return 1;
1976 }
1977
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301978 /* numSGE store lower 8 bit of sge_count.
1979 * numSGEExt store higher 8 bit of sge_count
1980 */
adam radford9c915a82010-12-21 13:34:31 -08001981 io_request->RaidContext.numSGE = sge_count;
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301982 io_request->RaidContext.numSGEExt = (u8)(sge_count >> 8);
adam radford9c915a82010-12-21 13:34:31 -08001983
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301984 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
adam radford9c915a82010-12-21 13:34:31 -08001985
1986 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301987 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
adam radford9c915a82010-12-21 13:34:31 -08001988 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301989 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
adam radford9c915a82010-12-21 13:34:31 -08001990
1991 io_request->SGLOffset0 =
1992 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1993
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301994 io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08001995 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1996
1997 cmd->scmd = scp;
1998 scp->SCp.ptr = (char *)cmd;
1999
2000 return 0;
2001}
2002
2003union MEGASAS_REQUEST_DESCRIPTOR_UNION *
2004megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
2005{
2006 u8 *p;
2007 struct fusion_context *fusion;
2008
2009 if (index >= instance->max_fw_cmds) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002010 dev_err(&instance->pdev->dev, "Invalid SMID (0x%x)request for "
adam radford229fe472014-03-10 02:51:56 -07002011 "descriptor for scsi%d\n", index,
2012 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002013 return NULL;
2014 }
2015 fusion = instance->ctrl_context;
2016 p = fusion->req_frames_desc
2017 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
2018
2019 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
2020}
2021
2022/**
2023 * megasas_build_and_issue_cmd_fusion -Main routine for building and
2024 * issuing non IOCTL cmd
2025 * @instance: Adapter soft state
2026 * @scmd: pointer to scsi cmd from OS
2027 */
2028static u32
2029megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
2030 struct scsi_cmnd *scmd)
2031{
2032 struct megasas_cmd_fusion *cmd;
2033 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2034 u32 index;
2035 struct fusion_context *fusion;
2036
2037 fusion = instance->ctrl_context;
2038
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302039 cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
adam radford9c915a82010-12-21 13:34:31 -08002040
2041 index = cmd->index;
2042
2043 req_desc = megasas_get_request_descriptor(instance, index-1);
2044 if (!req_desc)
2045 return 1;
2046
2047 req_desc->Words = 0;
2048 cmd->request_desc = req_desc;
adam radford9c915a82010-12-21 13:34:31 -08002049
2050 if (megasas_build_io_fusion(instance, scmd, cmd)) {
2051 megasas_return_cmd_fusion(instance, cmd);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002052 dev_err(&instance->pdev->dev, "Error building command\n");
adam radford9c915a82010-12-21 13:34:31 -08002053 cmd->request_desc = NULL;
2054 return 1;
2055 }
2056
2057 req_desc = cmd->request_desc;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302058 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08002059
2060 if (cmd->io_request->ChainOffset != 0 &&
2061 cmd->io_request->ChainOffset != 0xF)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002062 dev_err(&instance->pdev->dev, "The chain offset value is not "
adam radford9c915a82010-12-21 13:34:31 -08002063 "correct : %x\n", cmd->io_request->ChainOffset);
2064
2065 /*
2066 * Issue the command to the FW
2067 */
2068 atomic_inc(&instance->fw_outstanding);
2069
Christoph Hellwig01d7f032015-04-23 16:34:09 +05302070 megasas_fire_cmd_fusion(instance, req_desc);
adam radford9c915a82010-12-21 13:34:31 -08002071
2072 return 0;
2073}
2074
2075/**
2076 * complete_cmd_fusion - Completes command
2077 * @instance: Adapter soft state
2078 * Completes all commands that is in reply descriptor queue
2079 */
2080int
adam radfordc8e858f2011-10-08 18:15:13 -07002081complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
adam radford9c915a82010-12-21 13:34:31 -08002082{
2083 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
2084 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
2085 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
2086 struct fusion_context *fusion;
2087 struct megasas_cmd *cmd_mfi;
2088 struct megasas_cmd_fusion *cmd_fusion;
2089 u16 smid, num_completed;
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05302090 u8 reply_descript_type;
adam radford9c915a82010-12-21 13:34:31 -08002091 u32 status, extStatus, device_id;
2092 union desc_value d_val;
2093 struct LD_LOAD_BALANCE_INFO *lbinfo;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05302094 int threshold_reply_count = 0;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302095 struct scsi_cmnd *scmd_local = NULL;
adam radford9c915a82010-12-21 13:34:31 -08002096
2097 fusion = instance->ctrl_context;
2098
2099 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
2100 return IRQ_HANDLED;
2101
2102 desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -07002103 desc += ((MSIxIndex * fusion->reply_alloc_sz)/
2104 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
2105 fusion->last_reply_idx[MSIxIndex];
adam radford9c915a82010-12-21 13:34:31 -08002106
2107 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
2108
2109 d_val.word = desc->Words;
2110
2111 reply_descript_type = reply_desc->ReplyFlags &
2112 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
2113
2114 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
2115 return IRQ_NONE;
2116
adam radford9c915a82010-12-21 13:34:31 -08002117 num_completed = 0;
2118
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05302119 while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
2120 d_val.u.high != cpu_to_le32(UINT_MAX)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302121 smid = le16_to_cpu(reply_desc->SMID);
adam radford9c915a82010-12-21 13:34:31 -08002122
2123 cmd_fusion = fusion->cmd_list[smid - 1];
2124
2125 scsi_io_req =
2126 (struct MPI2_RAID_SCSI_IO_REQUEST *)
2127 cmd_fusion->io_request;
2128
2129 if (cmd_fusion->scmd)
2130 cmd_fusion->scmd->SCp.ptr = NULL;
2131
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302132 scmd_local = cmd_fusion->scmd;
adam radford9c915a82010-12-21 13:34:31 -08002133 status = scsi_io_req->RaidContext.status;
2134 extStatus = scsi_io_req->RaidContext.exStatus;
2135
2136 switch (scsi_io_req->Function) {
2137 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
2138 /* Update load balancing info */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302139 device_id = MEGASAS_DEV_INDEX(scmd_local);
adam radford9c915a82010-12-21 13:34:31 -08002140 lbinfo = &fusion->load_balance_info[device_id];
2141 if (cmd_fusion->scmd->SCp.Status &
2142 MEGASAS_LOAD_BALANCE_FLAG) {
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05302143 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
adam radford9c915a82010-12-21 13:34:31 -08002144 cmd_fusion->scmd->SCp.Status &=
2145 ~MEGASAS_LOAD_BALANCE_FLAG;
2146 }
2147 if (reply_descript_type ==
2148 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
2149 if (megasas_dbg_lvl == 5)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002150 dev_err(&instance->pdev->dev, "\nFAST Path "
adam radford9c915a82010-12-21 13:34:31 -08002151 "IO Success\n");
2152 }
2153 /* Fall thru and complete IO */
2154 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
2155 /* Map the FW Cmd Status */
2156 map_cmd_status(cmd_fusion, status, extStatus);
adam radford9c915a82010-12-21 13:34:31 -08002157 scsi_io_req->RaidContext.status = 0;
2158 scsi_io_req->RaidContext.exStatus = 0;
2159 megasas_return_cmd_fusion(instance, cmd_fusion);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302160 scsi_dma_unmap(scmd_local);
2161 scmd_local->scsi_done(scmd_local);
adam radford9c915a82010-12-21 13:34:31 -08002162 atomic_dec(&instance->fw_outstanding);
2163
2164 break;
2165 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
2166 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302167
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302168 /* Poll mode. Dummy free.
2169 * In case of Interrupt mode, caller has reverse check.
2170 */
2171 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
2172 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
2173 megasas_return_cmd(instance, cmd_mfi);
2174 } else
2175 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
adam radford9c915a82010-12-21 13:34:31 -08002176 break;
2177 }
2178
adam radfordc8e858f2011-10-08 18:15:13 -07002179 fusion->last_reply_idx[MSIxIndex]++;
2180 if (fusion->last_reply_idx[MSIxIndex] >=
2181 fusion->reply_q_depth)
2182 fusion->last_reply_idx[MSIxIndex] = 0;
adam radford9c915a82010-12-21 13:34:31 -08002183
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05302184 desc->Words = cpu_to_le64(ULLONG_MAX);
adam radford9c915a82010-12-21 13:34:31 -08002185 num_completed++;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05302186 threshold_reply_count++;
adam radford9c915a82010-12-21 13:34:31 -08002187
2188 /* Get the next reply descriptor */
adam radfordc8e858f2011-10-08 18:15:13 -07002189 if (!fusion->last_reply_idx[MSIxIndex])
2190 desc = fusion->reply_frames_desc +
2191 ((MSIxIndex * fusion->reply_alloc_sz)/
2192 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
adam radford9c915a82010-12-21 13:34:31 -08002193 else
2194 desc++;
2195
2196 reply_desc =
2197 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
2198
2199 d_val.word = desc->Words;
2200
2201 reply_descript_type = reply_desc->ReplyFlags &
2202 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
2203
2204 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
2205 break;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05302206 /*
2207 * Write to reply post host index register after completing threshold
2208 * number of reply counts and still there are more replies in reply queue
2209 * pending to be completed
2210 */
2211 if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
2212 if ((instance->pdev->device ==
2213 PCI_DEVICE_ID_LSI_INVADER) ||
2214 (instance->pdev->device ==
2215 PCI_DEVICE_ID_LSI_FURY))
2216 writel(((MSIxIndex & 0x7) << 24) |
2217 fusion->last_reply_idx[MSIxIndex],
2218 instance->reply_post_host_index_addr[MSIxIndex/8]);
2219 else
2220 writel((MSIxIndex << 24) |
2221 fusion->last_reply_idx[MSIxIndex],
2222 instance->reply_post_host_index_addr[0]);
2223 threshold_reply_count = 0;
2224 }
adam radford9c915a82010-12-21 13:34:31 -08002225 }
2226
2227 if (!num_completed)
2228 return IRQ_NONE;
2229
2230 wmb();
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302231 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2232 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
2233 writel(((MSIxIndex & 0x7) << 24) |
2234 fusion->last_reply_idx[MSIxIndex],
2235 instance->reply_post_host_index_addr[MSIxIndex/8]);
2236 else
2237 writel((MSIxIndex << 24) |
2238 fusion->last_reply_idx[MSIxIndex],
2239 instance->reply_post_host_index_addr[0]);
adam radford53ef2bb2011-02-24 20:56:05 -08002240 megasas_check_and_restore_queue_depth(instance);
adam radford9c915a82010-12-21 13:34:31 -08002241 return IRQ_HANDLED;
2242}
2243
2244/**
2245 * megasas_complete_cmd_dpc_fusion - Completes command
2246 * @instance: Adapter soft state
2247 *
2248 * Tasklet to complete cmds
2249 */
2250void
2251megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
2252{
2253 struct megasas_instance *instance =
2254 (struct megasas_instance *)instance_addr;
2255 unsigned long flags;
adam radfordc8e858f2011-10-08 18:15:13 -07002256 u32 count, MSIxIndex;
2257
2258 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
adam radford9c915a82010-12-21 13:34:31 -08002259
2260 /* If we have already declared adapter dead, donot complete cmds */
2261 spin_lock_irqsave(&instance->hba_lock, flags);
2262 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2263 spin_unlock_irqrestore(&instance->hba_lock, flags);
2264 return;
2265 }
2266 spin_unlock_irqrestore(&instance->hba_lock, flags);
2267
adam radfordc8e858f2011-10-08 18:15:13 -07002268 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
2269 complete_cmd_fusion(instance, MSIxIndex);
adam radford9c915a82010-12-21 13:34:31 -08002270}
2271
2272/**
2273 * megasas_isr_fusion - isr entry point
2274 */
2275irqreturn_t megasas_isr_fusion(int irq, void *devp)
2276{
adam radfordc8e858f2011-10-08 18:15:13 -07002277 struct megasas_irq_context *irq_context = devp;
2278 struct megasas_instance *instance = irq_context->instance;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302279 u32 mfiStatus, fw_state, dma_state;
adam radford9c915a82010-12-21 13:34:31 -08002280
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302281 if (instance->mask_interrupts)
2282 return IRQ_NONE;
2283
adam radfordc8e858f2011-10-08 18:15:13 -07002284 if (!instance->msix_vectors) {
adam radford9c915a82010-12-21 13:34:31 -08002285 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
2286 if (!mfiStatus)
2287 return IRQ_NONE;
2288 }
2289
2290 /* If we are resetting, bail */
adam radford6497b242011-10-08 18:14:50 -07002291 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
2292 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08002293 return IRQ_HANDLED;
adam radford6497b242011-10-08 18:14:50 -07002294 }
adam radford9c915a82010-12-21 13:34:31 -08002295
adam radfordc8e858f2011-10-08 18:15:13 -07002296 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
adam radford6497b242011-10-08 18:14:50 -07002297 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08002298 /* If we didn't complete any commands, check for FW fault */
2299 fw_state = instance->instancet->read_fw_status_reg(
2300 instance->reg_set) & MFI_STATE_MASK;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302301 dma_state = instance->instancet->read_fw_status_reg
2302 (instance->reg_set) & MFI_STATE_DMADONE;
2303 if (instance->crash_dump_drv_support &&
2304 instance->crash_dump_app_support) {
2305 /* Start collecting crash, if DMA bit is done */
2306 if ((fw_state == MFI_STATE_FAULT) && dma_state)
2307 schedule_work(&instance->crash_init);
2308 else if (fw_state == MFI_STATE_FAULT)
2309 schedule_work(&instance->work_init);
2310 } else if (fw_state == MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002311 dev_warn(&instance->pdev->dev, "Iop2SysDoorbellInt"
adam radford229fe472014-03-10 02:51:56 -07002312 "for scsi%d\n", instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002313 schedule_work(&instance->work_init);
adam radford229fe472014-03-10 02:51:56 -07002314 }
adam radford9c915a82010-12-21 13:34:31 -08002315 }
2316
2317 return IRQ_HANDLED;
2318}
2319
2320/**
2321 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
2322 * @instance: Adapter soft state
2323 * mfi_cmd: megasas_cmd pointer
2324 *
2325 */
2326u8
2327build_mpt_mfi_pass_thru(struct megasas_instance *instance,
2328 struct megasas_cmd *mfi_cmd)
2329{
2330 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
2331 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
2332 struct megasas_cmd_fusion *cmd;
2333 struct fusion_context *fusion;
2334 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
2335
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302336 fusion = instance->ctrl_context;
2337
2338 cmd = megasas_get_cmd_fusion(instance,
2339 instance->max_scsi_cmds + mfi_cmd->index);
adam radford9c915a82010-12-21 13:34:31 -08002340
2341 /* Save the smid. To be used for returning the cmd */
2342 mfi_cmd->context.smid = cmd->index;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302343
adam radford9c915a82010-12-21 13:34:31 -08002344 /*
2345 * For cmds where the flag is set, store the flag and check
2346 * on completion. For cmds with this flag, don't call
2347 * megasas_complete_cmd
2348 */
2349
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05302350 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302351 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
adam radford9c915a82010-12-21 13:34:31 -08002352
adam radford9c915a82010-12-21 13:34:31 -08002353 io_req = cmd->io_request;
adam radford36807e62011-10-08 18:15:06 -07002354
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05302355 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2356 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07002357 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
2358 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
2359 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2360 sgl_ptr_end->Flags = 0;
2361 }
2362
adam radford9c915a82010-12-21 13:34:31 -08002363 mpi25_ieee_chain =
2364 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
2365
2366 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
2367 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
2368 SGL) / 4;
2369 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
2370
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302371 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08002372
2373 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2374 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
2375
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05302376 mpi25_ieee_chain->Length = cpu_to_le32(instance->max_chain_frame_sz);
adam radford9c915a82010-12-21 13:34:31 -08002377
2378 return 0;
2379}
2380
2381/**
2382 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
2383 * @instance: Adapter soft state
2384 * @cmd: mfi cmd to build
2385 *
2386 */
2387union MEGASAS_REQUEST_DESCRIPTOR_UNION *
2388build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
2389{
2390 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2391 u16 index;
2392
2393 if (build_mpt_mfi_pass_thru(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002394 dev_err(&instance->pdev->dev, "Couldn't build MFI pass thru cmd\n");
adam radford9c915a82010-12-21 13:34:31 -08002395 return NULL;
2396 }
2397
2398 index = cmd->context.smid;
2399
2400 req_desc = megasas_get_request_descriptor(instance, index - 1);
2401
2402 if (!req_desc)
2403 return NULL;
2404
2405 req_desc->Words = 0;
2406 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2407 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2408
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302409 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08002410
2411 return req_desc;
2412}
2413
2414/**
2415 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
2416 * @instance: Adapter soft state
2417 * @cmd: mfi cmd pointer
2418 *
2419 */
2420void
2421megasas_issue_dcmd_fusion(struct megasas_instance *instance,
2422 struct megasas_cmd *cmd)
2423{
2424 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
adam radford9c915a82010-12-21 13:34:31 -08002425
2426 req_desc = build_mpt_cmd(instance, cmd);
2427 if (!req_desc) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002428 dev_err(&instance->pdev->dev, "Couldn't issue MFI pass thru cmd\n");
adam radford9c915a82010-12-21 13:34:31 -08002429 return;
2430 }
Christoph Hellwig01d7f032015-04-23 16:34:09 +05302431 megasas_fire_cmd_fusion(instance, req_desc);
adam radford9c915a82010-12-21 13:34:31 -08002432}
2433
2434/**
2435 * megasas_release_fusion - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01002436 * @instance: Adapter soft state
adam radford9c915a82010-12-21 13:34:31 -08002437 */
2438void
2439megasas_release_fusion(struct megasas_instance *instance)
2440{
2441 megasas_free_cmds(instance);
2442 megasas_free_cmds_fusion(instance);
2443
2444 iounmap(instance->reg_set);
2445
2446 pci_release_selected_regions(instance->pdev, instance->bar);
2447}
2448
2449/**
2450 * megasas_read_fw_status_reg_fusion - returns the current FW status value
2451 * @regs: MFI register set
2452 */
2453static u32
2454megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
2455{
2456 return readl(&(regs)->outbound_scratch_pad);
2457}
2458
2459/**
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302460 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
2461 * @instance: Controller's soft instance
2462 * return: Number of allocated host crash buffers
2463 */
2464static void
2465megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
2466{
2467 unsigned int i;
2468
2469 instance->crash_buf_pages = get_order(CRASH_DMA_BUF_SIZE);
2470 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
2471 instance->crash_buf[i] = (void *)__get_free_pages(GFP_KERNEL,
2472 instance->crash_buf_pages);
2473 if (!instance->crash_buf[i]) {
2474 dev_info(&instance->pdev->dev, "Firmware crash dump "
2475 "memory allocation failed at index %d\n", i);
2476 break;
2477 }
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05302478 memset(instance->crash_buf[i], 0,
2479 ((1 << PAGE_SHIFT) << instance->crash_buf_pages));
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302480 }
2481 instance->drv_buf_alloc = i;
2482}
2483
2484/**
2485 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
2486 * @instance: Controller's soft instance
2487 */
2488void
2489megasas_free_host_crash_buffer(struct megasas_instance *instance)
2490{
2491 unsigned int i
2492;
2493 for (i = 0; i < instance->drv_buf_alloc; i++) {
2494 if (instance->crash_buf[i])
2495 free_pages((ulong)instance->crash_buf[i],
2496 instance->crash_buf_pages);
2497 }
2498 instance->drv_buf_index = 0;
2499 instance->drv_buf_alloc = 0;
2500 instance->fw_crash_state = UNAVAILABLE;
2501 instance->fw_crash_buffer_size = 0;
2502}
2503
2504/**
adam radford9c915a82010-12-21 13:34:31 -08002505 * megasas_adp_reset_fusion - For controller reset
2506 * @regs: MFI register set
2507 */
2508static int
2509megasas_adp_reset_fusion(struct megasas_instance *instance,
2510 struct megasas_register_set __iomem *regs)
2511{
2512 return 0;
2513}
2514
2515/**
2516 * megasas_check_reset_fusion - For controller reset check
2517 * @regs: MFI register set
2518 */
2519static int
2520megasas_check_reset_fusion(struct megasas_instance *instance,
2521 struct megasas_register_set __iomem *regs)
2522{
2523 return 0;
2524}
2525
2526/* This function waits for outstanding commands on fusion to complete */
adam radford229fe472014-03-10 02:51:56 -07002527int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
2528 int iotimeout, int *convert)
adam radford9c915a82010-12-21 13:34:31 -08002529{
adam radford229fe472014-03-10 02:51:56 -07002530 int i, outstanding, retval = 0, hb_seconds_missed = 0;
adam radfordc007b8b2012-07-17 18:20:24 -07002531 u32 fw_state;
adam radford9c915a82010-12-21 13:34:31 -08002532
adam radfordc007b8b2012-07-17 18:20:24 -07002533 for (i = 0; i < resetwaittime; i++) {
adam radford9c915a82010-12-21 13:34:31 -08002534 /* Check if firmware is in fault state */
2535 fw_state = instance->instancet->read_fw_status_reg(
2536 instance->reg_set) & MFI_STATE_MASK;
2537 if (fw_state == MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002538 dev_warn(&instance->pdev->dev, "Found FW in FAULT state,"
adam radford229fe472014-03-10 02:51:56 -07002539 " will reset adapter scsi%d.\n",
2540 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002541 retval = 1;
2542 goto out;
2543 }
adam radford229fe472014-03-10 02:51:56 -07002544 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
2545 if (instance->requestorId && !iotimeout) {
2546 retval = 1;
2547 goto out;
2548 }
2549
2550 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
2551 if (instance->requestorId && iotimeout) {
2552 if (instance->hb_host_mem->HB.fwCounter !=
2553 instance->hb_host_mem->HB.driverCounter) {
2554 instance->hb_host_mem->HB.driverCounter =
2555 instance->hb_host_mem->HB.fwCounter;
2556 hb_seconds_missed = 0;
2557 } else {
2558 hb_seconds_missed++;
2559 if (hb_seconds_missed ==
2560 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002561 dev_warn(&instance->pdev->dev, "SR-IOV:"
adam radford229fe472014-03-10 02:51:56 -07002562 " Heartbeat never completed "
2563 " while polling during I/O "
2564 " timeout handling for "
2565 "scsi%d.\n",
2566 instance->host->host_no);
2567 *convert = 1;
2568 retval = 1;
2569 goto out;
2570 }
2571 }
2572 }
adam radford9c915a82010-12-21 13:34:31 -08002573
2574 outstanding = atomic_read(&instance->fw_outstanding);
2575 if (!outstanding)
2576 goto out;
2577
2578 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002579 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
adam radford229fe472014-03-10 02:51:56 -07002580 "commands to complete for scsi%d\n", i,
2581 outstanding, instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002582 megasas_complete_cmd_dpc_fusion(
2583 (unsigned long)instance);
2584 }
2585 msleep(1000);
2586 }
2587
2588 if (atomic_read(&instance->fw_outstanding)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002589 dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
adam radford229fe472014-03-10 02:51:56 -07002590 "will reset adapter scsi%d.\n",
2591 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002592 retval = 1;
2593 }
2594out:
2595 return retval;
2596}
2597
2598void megasas_reset_reply_desc(struct megasas_instance *instance)
2599{
adam radfordc8e858f2011-10-08 18:15:13 -07002600 int i, count;
adam radford9c915a82010-12-21 13:34:31 -08002601 struct fusion_context *fusion;
2602 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2603
2604 fusion = instance->ctrl_context;
adam radfordc8e858f2011-10-08 18:15:13 -07002605 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2606 for (i = 0 ; i < count ; i++)
2607 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -08002608 reply_desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -07002609 for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05302610 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
adam radford9c915a82010-12-21 13:34:31 -08002611}
2612
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05302613/*
2614 * megasas_refire_mgmt_cmd : Re-fire management commands
2615 * @instance: Controller's soft instance
2616*/
2617void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
2618{
2619 int j;
2620 struct megasas_cmd_fusion *cmd_fusion;
2621 struct fusion_context *fusion;
2622 struct megasas_cmd *cmd_mfi;
2623 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2624 u16 smid;
2625
2626 fusion = instance->ctrl_context;
2627
2628 /* Re-fire management commands.
2629 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
2630 */
2631 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
2632 cmd_fusion = fusion->cmd_list[j];
2633 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2634 smid = le16_to_cpu(cmd_mfi->context.smid);
2635
2636 if (!smid)
2637 continue;
2638 req_desc = megasas_get_request_descriptor
2639 (instance, smid - 1);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302640 if (req_desc && ((cmd_mfi->frame->dcmd.opcode !=
2641 cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO)) &&
2642 (cmd_mfi->frame->dcmd.opcode !=
2643 cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO))))
Christoph Hellwig01d7f032015-04-23 16:34:09 +05302644 megasas_fire_cmd_fusion(instance, req_desc);
2645 else
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05302646 megasas_return_cmd(instance, cmd_mfi);
2647 }
adam radford9c915a82010-12-21 13:34:31 -08002648}
2649
adam radford229fe472014-03-10 02:51:56 -07002650/* Check for a second path that is currently UP */
2651int megasas_check_mpio_paths(struct megasas_instance *instance,
2652 struct scsi_cmnd *scmd)
adam radford9c915a82010-12-21 13:34:31 -08002653{
adam radford229fe472014-03-10 02:51:56 -07002654 int i, j, retval = (DID_RESET << 16);
2655
2656 if (instance->mpio && instance->requestorId) {
2657 for (i = 0 ; i < MAX_MGMT_ADAPTERS ; i++)
2658 for (j = 0 ; j < MAX_LOGICAL_DRIVES; j++)
2659 if (megasas_mgmt_info.instance[i] &&
2660 (megasas_mgmt_info.instance[i] != instance) &&
2661 megasas_mgmt_info.instance[i]->mpio &&
2662 megasas_mgmt_info.instance[i]->requestorId
2663 &&
2664 (megasas_mgmt_info.instance[i]->ld_ids[j]
2665 == scmd->device->id)) {
2666 retval = (DID_NO_CONNECT << 16);
2667 goto out;
2668 }
2669 }
2670out:
2671 return retval;
2672}
2673
2674/* Core fusion reset function */
2675int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout)
2676{
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05302677 int retval = SUCCESS, i, retry = 0, convert = 0;
adam radford9c915a82010-12-21 13:34:31 -08002678 struct megasas_instance *instance;
2679 struct megasas_cmd_fusion *cmd_fusion;
2680 struct fusion_context *fusion;
adam radford7e70e732011-05-11 18:34:08 -07002681 u32 host_diag, abs_state, status_reg, reset_adapter;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302682 u32 io_timeout_in_crash_mode = 0;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302683 struct scsi_cmnd *scmd_local = NULL;
adam radford9c915a82010-12-21 13:34:31 -08002684
2685 instance = (struct megasas_instance *)shost->hostdata;
2686 fusion = instance->ctrl_context;
2687
adam radford229fe472014-03-10 02:51:56 -07002688 mutex_lock(&instance->reset_mutex);
2689
adam radford9c915a82010-12-21 13:34:31 -08002690 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002691 dev_warn(&instance->pdev->dev, "Hardware critical error, "
adam radford229fe472014-03-10 02:51:56 -07002692 "returning FAILED for scsi%d.\n",
2693 instance->host->host_no);
Adam Radforda2fbcbc2014-07-09 15:17:54 -07002694 mutex_unlock(&instance->reset_mutex);
adam radfordd4a759a2011-10-08 18:14:39 -07002695 return FAILED;
adam radford9c915a82010-12-21 13:34:31 -08002696 }
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302697 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
2698 abs_state = status_reg & MFI_STATE_MASK;
2699
2700 /* IO timeout detected, forcibly put FW in FAULT state */
2701 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
2702 instance->crash_dump_app_support && iotimeout) {
2703 dev_info(&instance->pdev->dev, "IO timeout is detected, "
2704 "forcibly FAULT Firmware\n");
2705 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2706 status_reg = readl(&instance->reg_set->doorbell);
2707 writel(status_reg | MFI_STATE_FORCE_OCR,
2708 &instance->reg_set->doorbell);
2709 readl(&instance->reg_set->doorbell);
2710 mutex_unlock(&instance->reset_mutex);
2711 do {
2712 ssleep(3);
2713 io_timeout_in_crash_mode++;
2714 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
2715 "seconds for crash dump collection and OCR "
2716 "to be done\n", (io_timeout_in_crash_mode * 3));
2717 } while ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
2718 (io_timeout_in_crash_mode < 80));
2719
2720 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
2721 dev_info(&instance->pdev->dev, "OCR done for IO "
2722 "timeout case\n");
2723 retval = SUCCESS;
2724 } else {
2725 dev_info(&instance->pdev->dev, "Controller is not "
2726 "operational after 240 seconds wait for IO "
2727 "timeout case in FW crash dump mode\n do "
2728 "OCR/kill adapter\n");
2729 retval = megasas_reset_fusion(shost, 0);
2730 }
2731 return retval;
2732 }
adam radford9c915a82010-12-21 13:34:31 -08002733
adam radford229fe472014-03-10 02:51:56 -07002734 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
2735 del_timer_sync(&instance->sriov_heartbeat_timer);
adam radford7e70e732011-05-11 18:34:08 -07002736 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
adam radford229fe472014-03-10 02:51:56 -07002737 instance->adprecovery = MEGASAS_ADPRESET_SM_POLLING;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302738 instance->instancet->disable_intr(instance);
adam radford7e70e732011-05-11 18:34:08 -07002739 msleep(1000);
2740
adam radford9c915a82010-12-21 13:34:31 -08002741 /* First try waiting for commands to complete */
adam radford229fe472014-03-10 02:51:56 -07002742 if (megasas_wait_for_outstanding_fusion(instance, iotimeout,
2743 &convert)) {
2744 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002745 dev_warn(&instance->pdev->dev, "resetting fusion "
adam radford229fe472014-03-10 02:51:56 -07002746 "adapter scsi%d.\n", instance->host->host_no);
2747 if (convert)
2748 iotimeout = 0;
2749
adam radford9c915a82010-12-21 13:34:31 -08002750 /* Now return commands back to the OS */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302751 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
adam radford9c915a82010-12-21 13:34:31 -08002752 cmd_fusion = fusion->cmd_list[i];
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302753 scmd_local = cmd_fusion->scmd;
adam radford9c915a82010-12-21 13:34:31 -08002754 if (cmd_fusion->scmd) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302755 scmd_local->result =
adam radford229fe472014-03-10 02:51:56 -07002756 megasas_check_mpio_paths(instance,
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302757 scmd_local);
adam radford9c915a82010-12-21 13:34:31 -08002758 megasas_return_cmd_fusion(instance, cmd_fusion);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302759 scsi_dma_unmap(scmd_local);
2760 scmd_local->scsi_done(scmd_local);
adam radford9c915a82010-12-21 13:34:31 -08002761 atomic_dec(&instance->fw_outstanding);
2762 }
2763 }
2764
adam radford7e70e732011-05-11 18:34:08 -07002765 status_reg = instance->instancet->read_fw_status_reg(
2766 instance->reg_set);
2767 abs_state = status_reg & MFI_STATE_MASK;
2768 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2769 if (instance->disableOnlineCtrlReset ||
2770 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
adam radford9c915a82010-12-21 13:34:31 -08002771 /* Reset not supported, kill adapter */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002772 dev_warn(&instance->pdev->dev, "Reset not supported"
adam radford229fe472014-03-10 02:51:56 -07002773 ", killing adapter scsi%d.\n",
2774 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002775 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07002776 instance->skip_heartbeat_timer_del = 1;
adam radford9c915a82010-12-21 13:34:31 -08002777 retval = FAILED;
2778 goto out;
2779 }
2780
adam radford229fe472014-03-10 02:51:56 -07002781 /* Let SR-IOV VF & PF sync up if there was a HB failure */
2782 if (instance->requestorId && !iotimeout) {
2783 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
2784 /* Look for a late HB update after VF settle time */
2785 if (abs_state == MFI_STATE_OPERATIONAL &&
2786 (instance->hb_host_mem->HB.fwCounter !=
2787 instance->hb_host_mem->HB.driverCounter)) {
2788 instance->hb_host_mem->HB.driverCounter =
2789 instance->hb_host_mem->HB.fwCounter;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002790 dev_warn(&instance->pdev->dev, "SR-IOV:"
adam radford229fe472014-03-10 02:51:56 -07002791 "Late FW heartbeat update for "
2792 "scsi%d.\n",
2793 instance->host->host_no);
2794 } else {
2795 /* In VF mode, first poll for FW ready */
2796 for (i = 0;
2797 i < (MEGASAS_RESET_WAIT_TIME * 1000);
2798 i += 20) {
2799 status_reg =
2800 instance->instancet->
2801 read_fw_status_reg(
2802 instance->reg_set);
2803 abs_state = status_reg &
2804 MFI_STATE_MASK;
2805 if (abs_state == MFI_STATE_READY) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002806 dev_warn(&instance->pdev->dev,
2807 "SR-IOV: FW was found"
adam radford229fe472014-03-10 02:51:56 -07002808 "to be in ready state "
2809 "for scsi%d.\n",
2810 instance->host->host_no);
2811 break;
2812 }
2813 msleep(20);
2814 }
2815 if (abs_state != MFI_STATE_READY) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002816 dev_warn(&instance->pdev->dev, "SR-IOV: "
adam radford229fe472014-03-10 02:51:56 -07002817 "FW not in ready state after %d"
2818 " seconds for scsi%d, status_reg = "
2819 "0x%x.\n",
2820 MEGASAS_RESET_WAIT_TIME,
2821 instance->host->host_no,
2822 status_reg);
2823 megaraid_sas_kill_hba(instance);
2824 instance->skip_heartbeat_timer_del = 1;
2825 instance->adprecovery =
2826 MEGASAS_HW_CRITICAL_ERROR;
2827 retval = FAILED;
2828 goto out;
2829 }
2830 }
2831 }
2832
adam radford9c915a82010-12-21 13:34:31 -08002833 /* Now try to reset the chip */
2834 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2835 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2836 &instance->reg_set->fusion_seq_offset);
2837 writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2838 &instance->reg_set->fusion_seq_offset);
2839 writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2840 &instance->reg_set->fusion_seq_offset);
2841 writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2842 &instance->reg_set->fusion_seq_offset);
2843 writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2844 &instance->reg_set->fusion_seq_offset);
2845 writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2846 &instance->reg_set->fusion_seq_offset);
2847 writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2848 &instance->reg_set->fusion_seq_offset);
2849
2850 /* Check that the diag write enable (DRWE) bit is on */
2851 host_diag = readl(&instance->reg_set->fusion_host_diag);
adam radford7e70e732011-05-11 18:34:08 -07002852 retry = 0;
adam radford9c915a82010-12-21 13:34:31 -08002853 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2854 msleep(100);
2855 host_diag =
2856 readl(&instance->reg_set->fusion_host_diag);
2857 if (retry++ == 100) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002858 dev_warn(&instance->pdev->dev,
adam radford229fe472014-03-10 02:51:56 -07002859 "Host diag unlock failed! "
2860 "for scsi%d\n",
2861 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002862 break;
2863 }
2864 }
2865 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2866 continue;
2867
2868 /* Send chip reset command */
2869 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2870 &instance->reg_set->fusion_host_diag);
2871 msleep(3000);
2872
2873 /* Make sure reset adapter bit is cleared */
2874 host_diag = readl(&instance->reg_set->fusion_host_diag);
2875 retry = 0;
2876 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2877 msleep(100);
2878 host_diag =
2879 readl(&instance->reg_set->fusion_host_diag);
2880 if (retry++ == 1000) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002881 dev_warn(&instance->pdev->dev,
adam radford9c915a82010-12-21 13:34:31 -08002882 "Diag reset adapter never "
adam radford229fe472014-03-10 02:51:56 -07002883 "cleared for scsi%d!\n",
2884 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002885 break;
2886 }
2887 }
2888 if (host_diag & HOST_DIAG_RESET_ADAPTER)
2889 continue;
2890
2891 abs_state =
2892 instance->instancet->read_fw_status_reg(
adam radford7e70e732011-05-11 18:34:08 -07002893 instance->reg_set) & MFI_STATE_MASK;
adam radford9c915a82010-12-21 13:34:31 -08002894 retry = 0;
2895
2896 while ((abs_state <= MFI_STATE_FW_INIT) &&
2897 (retry++ < 1000)) {
2898 msleep(100);
2899 abs_state =
2900 instance->instancet->read_fw_status_reg(
adam radford7e70e732011-05-11 18:34:08 -07002901 instance->reg_set) & MFI_STATE_MASK;
adam radford9c915a82010-12-21 13:34:31 -08002902 }
2903 if (abs_state <= MFI_STATE_FW_INIT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002904 dev_warn(&instance->pdev->dev, "firmware "
adam radford9c915a82010-12-21 13:34:31 -08002905 "state < MFI_STATE_FW_INIT, state = "
adam radford229fe472014-03-10 02:51:56 -07002906 "0x%x for scsi%d\n", abs_state,
2907 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002908 continue;
2909 }
2910
2911 /* Wait for FW to become ready */
adam radford058a8fa2011-10-08 18:14:27 -07002912 if (megasas_transition_to_ready(instance, 1)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002913 dev_warn(&instance->pdev->dev, "Failed to "
adam radford229fe472014-03-10 02:51:56 -07002914 "transition controller to ready "
2915 "for scsi%d.\n",
2916 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002917 continue;
2918 }
2919
2920 megasas_reset_reply_desc(instance);
2921 if (megasas_ioc_init_fusion(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002922 dev_warn(&instance->pdev->dev,
adam radford229fe472014-03-10 02:51:56 -07002923 "megasas_ioc_init_fusion() failed!"
2924 " for scsi%d\n",
2925 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002926 continue;
2927 }
2928
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05302929 megasas_refire_mgmt_cmd(instance);
adam radford9c915a82010-12-21 13:34:31 -08002930
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05302931 if (megasas_get_ctrl_info(instance)) {
2932 dev_info(&instance->pdev->dev,
2933 "Failed from %s %d\n",
2934 __func__, __LINE__);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05302935 megaraid_sas_kill_hba(instance);
2936 retval = FAILED;
2937 }
adam radford9c915a82010-12-21 13:34:31 -08002938 /* Reset load balance info */
2939 memset(fusion->load_balance_info, 0,
2940 sizeof(struct LD_LOAD_BALANCE_INFO)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302941 *MAX_LOGICAL_DRIVES_EXT);
adam radford9c915a82010-12-21 13:34:31 -08002942
2943 if (!megasas_get_map_info(instance))
2944 megasas_sync_map_info(instance);
2945
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302946 megasas_setup_jbod_map(instance);
2947
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05302948 clear_bit(MEGASAS_FUSION_IN_RESET,
2949 &instance->reset_flags);
2950 instance->instancet->enable_intr(instance);
2951 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2952
adam radford229fe472014-03-10 02:51:56 -07002953 /* Restart SR-IOV heartbeat */
2954 if (instance->requestorId) {
2955 if (!megasas_sriov_start_heartbeat(instance, 0))
2956 megasas_start_timer(instance,
2957 &instance->sriov_heartbeat_timer,
2958 megasas_sriov_heartbeat_handler,
2959 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2960 else
2961 instance->skip_heartbeat_timer_del = 1;
2962 }
2963
adam radford9c915a82010-12-21 13:34:31 -08002964 /* Adapter reset completed successfully */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002965 dev_warn(&instance->pdev->dev, "Reset "
adam radford229fe472014-03-10 02:51:56 -07002966 "successful for scsi%d.\n",
2967 instance->host->host_no);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302968
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05302969 if (instance->crash_dump_drv_support &&
2970 instance->crash_dump_app_support)
2971 megasas_set_crash_dump_params(instance,
2972 MR_CRASH_BUF_TURN_ON);
2973 else
2974 megasas_set_crash_dump_params(instance,
2975 MR_CRASH_BUF_TURN_OFF);
2976
adam radford9c915a82010-12-21 13:34:31 -08002977 retval = SUCCESS;
2978 goto out;
2979 }
2980 /* Reset failed, kill the adapter */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002981 dev_warn(&instance->pdev->dev, "Reset failed, killing "
adam radford229fe472014-03-10 02:51:56 -07002982 "adapter scsi%d.\n", instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002983 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07002984 instance->skip_heartbeat_timer_del = 1;
adam radford9c915a82010-12-21 13:34:31 -08002985 retval = FAILED;
2986 } else {
adam radford229fe472014-03-10 02:51:56 -07002987 /* For VF: Restart HB timer if we didn't OCR */
2988 if (instance->requestorId) {
2989 megasas_start_timer(instance,
2990 &instance->sriov_heartbeat_timer,
2991 megasas_sriov_heartbeat_handler,
2992 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2993 }
adam radford3f0e58bc2011-10-08 18:14:59 -07002994 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302995 instance->instancet->enable_intr(instance);
adam radford9c915a82010-12-21 13:34:31 -08002996 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2997 }
2998out:
2999 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
3000 mutex_unlock(&instance->reset_mutex);
3001 return retval;
3002}
3003
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05303004/* Fusion Crash dump collection work queue */
3005void megasas_fusion_crash_dump_wq(struct work_struct *work)
3006{
3007 struct megasas_instance *instance =
3008 container_of(work, struct megasas_instance, crash_init);
3009 u32 status_reg;
3010 u8 partial_copy = 0;
3011
3012
3013 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
3014
3015 /*
3016 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
3017 * to host crash buffers
3018 */
3019 if (instance->drv_buf_index == 0) {
3020 /* Buffer is already allocated for old Crash dump.
3021 * Do OCR and do not wait for crash dump collection
3022 */
3023 if (instance->drv_buf_alloc) {
3024 dev_info(&instance->pdev->dev, "earlier crash dump is "
3025 "not yet copied by application, ignoring this "
3026 "crash dump and initiating OCR\n");
3027 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
3028 writel(status_reg,
3029 &instance->reg_set->outbound_scratch_pad);
3030 readl(&instance->reg_set->outbound_scratch_pad);
3031 return;
3032 }
3033 megasas_alloc_host_crash_buffer(instance);
3034 dev_info(&instance->pdev->dev, "Number of host crash buffers "
3035 "allocated: %d\n", instance->drv_buf_alloc);
3036 }
3037
3038 /*
3039 * Driver has allocated max buffers, which can be allocated
3040 * and FW has more crash dump data, then driver will
3041 * ignore the data.
3042 */
3043 if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
3044 dev_info(&instance->pdev->dev, "Driver is done copying "
3045 "the buffer: %d\n", instance->drv_buf_alloc);
3046 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
3047 partial_copy = 1;
3048 } else {
3049 memcpy(instance->crash_buf[instance->drv_buf_index],
3050 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
3051 instance->drv_buf_index++;
3052 status_reg &= ~MFI_STATE_DMADONE;
3053 }
3054
3055 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
3056 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
3057 "of copied buffers: %d\n", instance->drv_buf_index);
3058 instance->fw_crash_buffer_size = instance->drv_buf_index;
3059 instance->fw_crash_state = AVAILABLE;
3060 instance->drv_buf_index = 0;
3061 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
3062 readl(&instance->reg_set->outbound_scratch_pad);
3063 if (!partial_copy)
3064 megasas_reset_fusion(instance->host, 0);
3065 } else {
3066 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
3067 readl(&instance->reg_set->outbound_scratch_pad);
3068 }
3069}
3070
3071
adam radford9c915a82010-12-21 13:34:31 -08003072/* Fusion OCR work queue */
3073void megasas_fusion_ocr_wq(struct work_struct *work)
3074{
3075 struct megasas_instance *instance =
3076 container_of(work, struct megasas_instance, work_init);
3077
adam radford229fe472014-03-10 02:51:56 -07003078 megasas_reset_fusion(instance->host, 0);
adam radford9c915a82010-12-21 13:34:31 -08003079}
3080
3081struct megasas_instance_template megasas_instance_template_fusion = {
adam radford9c915a82010-12-21 13:34:31 -08003082 .enable_intr = megasas_enable_intr_fusion,
3083 .disable_intr = megasas_disable_intr_fusion,
3084 .clear_intr = megasas_clear_intr_fusion,
3085 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
3086 .adp_reset = megasas_adp_reset_fusion,
3087 .check_reset = megasas_check_reset_fusion,
3088 .service_isr = megasas_isr_fusion,
3089 .tasklet = megasas_complete_cmd_dpc_fusion,
3090 .init_adapter = megasas_init_adapter_fusion,
3091 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
3092 .issue_dcmd = megasas_issue_dcmd_fusion,
3093};