blob: bed7fb1ba2a61ce9ba3a282111f4958e6d50f17e [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 */
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +0530656 if (fusion->adapter_type == INVADER_SERIES)
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530657 drv_ops->mfi_capabilities.support_additional_msix = 1;
adam radford21c9e162013-09-06 15:27:14 -0700658 /* driver supports HA / Remote LUN over Fast Path interface */
sumit.saxena@avagotech.com4dbbe3c2015-08-31 17:23:21 +0530659 drv_ops->mfi_capabilities.support_fp_remote_lun = 1;
660
661 drv_ops->mfi_capabilities.support_max_255lds = 1;
662 drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1;
663 drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1;
664
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +0530665 if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN)
666 drv_ops->mfi_capabilities.support_ext_io_size = 1;
667
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530668 /* Convert capability to LE32 */
669 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
adam radford21c9e162013-09-06 15:27:14 -0700670
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +0530671 sys_info = dmi_get_system_info(DMI_PRODUCT_UUID);
672 if (instance->system_info_buf && sys_info) {
673 memcpy(instance->system_info_buf->systemId, sys_info,
674 strlen(sys_info) > 64 ? 64 : strlen(sys_info));
675 instance->system_info_buf->systemIdLength =
676 strlen(sys_info) > 64 ? 64 : strlen(sys_info);
677 init_frame->system_info_lo = instance->system_info_h;
678 init_frame->system_info_hi = 0;
679 }
680
Hannes Reineckefdc5a972014-01-16 11:25:33 +0100681 init_frame->queue_info_new_phys_addr_hi =
682 cpu_to_le32(upper_32_bits(ioc_init_handle));
683 init_frame->queue_info_new_phys_addr_lo =
684 cpu_to_le32(lower_32_bits(ioc_init_handle));
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530685 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
adam radford9c915a82010-12-21 13:34:31 -0800686
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +0530687 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
688 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
adam radfordc77a9bd2014-03-10 02:51:36 -0700689 req_desc.MFAIo.RequestFlags =
adam radford9c915a82010-12-21 13:34:31 -0800690 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +0530691 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford9c915a82010-12-21 13:34:31 -0800692
693 /*
694 * disable the intr before firing the init frame
695 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530696 instance->instancet->disable_intr(instance);
adam radford9c915a82010-12-21 13:34:31 -0800697
698 for (i = 0; i < (10 * 1000); i += 20) {
699 if (readl(&instance->reg_set->doorbell) & 1)
700 msleep(20);
701 else
702 break;
703 }
704
Christoph Hellwig01d7f032015-04-23 16:34:09 +0530705 megasas_fire_cmd_fusion(instance, &req_desc);
adam radford9c915a82010-12-21 13:34:31 -0800706
adam radford229fe472014-03-10 02:51:56 -0700707 wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -0800708
709 frame_hdr = &cmd->frame->hdr;
710 if (frame_hdr->cmd_status != 0) {
711 ret = 1;
712 goto fail_fw_init;
713 }
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500714 dev_err(&instance->pdev->dev, "Init cmd success\n");
adam radford9c915a82010-12-21 13:34:31 -0800715
716 ret = 0;
717
718fail_fw_init:
719 megasas_return_cmd(instance, cmd);
720 if (IOCInitMessage)
721 dma_free_coherent(&instance->pdev->dev,
722 sizeof(struct MPI2_IOC_INIT_REQUEST),
723 IOCInitMessage, ioc_init_handle);
724fail_get_cmd:
725 return ret;
726}
727
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +0530728/**
729 * megasas_sync_pd_seq_num - JBOD SEQ MAP
730 * @instance: Adapter soft state
731 * @pend: set to 1, if it is pended jbod map.
732 *
733 * Issue Jbod map to the firmware. If it is pended command,
734 * issue command and return. If it is first instance of jbod map
735 * issue and receive command.
736 */
737int
738megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) {
739 int ret = 0;
740 u32 pd_seq_map_sz;
741 struct megasas_cmd *cmd;
742 struct megasas_dcmd_frame *dcmd;
743 struct fusion_context *fusion = instance->ctrl_context;
744 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
745 dma_addr_t pd_seq_h;
746
747 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)];
748 pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)];
749 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) +
750 (sizeof(struct MR_PD_CFG_SEQ) *
751 (MAX_PHYSICAL_DEVICES - 1));
752
753 cmd = megasas_get_cmd(instance);
754 if (!cmd) {
755 dev_err(&instance->pdev->dev,
756 "Could not get mfi cmd. Fail from %s %d\n",
757 __func__, __LINE__);
758 return -ENOMEM;
759 }
760
761 dcmd = &cmd->frame->dcmd;
762
763 memset(pd_sync, 0, pd_seq_map_sz);
764 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
765 dcmd->cmd = MFI_CMD_DCMD;
766 dcmd->cmd_status = 0xFF;
767 dcmd->sge_count = 1;
768 dcmd->timeout = 0;
769 dcmd->pad_0 = 0;
770 dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz);
771 dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO);
772 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(pd_seq_h);
773 dcmd->sgl.sge32[0].length = cpu_to_le32(pd_seq_map_sz);
774
775 if (pend) {
776 dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG;
777 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
778 instance->jbod_seq_cmd = cmd;
779 instance->instancet->issue_dcmd(instance, cmd);
780 return 0;
781 }
782
783 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
784
785 /* Below code is only for non pended DCMD */
786 if (instance->ctrl_context && !instance->mask_interrupts)
787 ret = megasas_issue_blocked_cmd(instance, cmd, 60);
788 else
789 ret = megasas_issue_polled(instance, cmd);
790
791 if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) {
792 dev_warn(&instance->pdev->dev,
793 "driver supports max %d JBOD, but FW reports %d\n",
794 MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count));
795 ret = -EINVAL;
796 }
797
798 if (!ret)
799 instance->pd_seq_map_id++;
800
801 megasas_return_cmd(instance, cmd);
802 return ret;
803}
804
adam radford9c915a82010-12-21 13:34:31 -0800805/*
adam radford9c915a82010-12-21 13:34:31 -0800806 * megasas_get_ld_map_info - Returns FW's ld_map structure
807 * @instance: Adapter soft state
808 * @pend: Pend the command or not
809 * Issues an internal command (DCMD) to get the FW's controller PD
810 * list structure. This information is mainly used to find out SYSTEM
811 * supported by the FW.
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530812 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
813 * dcmd.mbox.b[0] - number of LDs being sync'd
814 * dcmd.mbox.b[1] - 0 - complete command immediately.
815 * - 1 - pend till config change
816 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
817 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
818 * uses extended struct MR_FW_RAID_MAP_EXT
adam radford9c915a82010-12-21 13:34:31 -0800819 */
820static int
821megasas_get_ld_map_info(struct megasas_instance *instance)
822{
823 int ret = 0;
824 struct megasas_cmd *cmd;
825 struct megasas_dcmd_frame *dcmd;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530826 void *ci;
adam radford9c915a82010-12-21 13:34:31 -0800827 dma_addr_t ci_h = 0;
828 u32 size_map_info;
829 struct fusion_context *fusion;
830
831 cmd = megasas_get_cmd(instance);
832
833 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500834 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n");
adam radford9c915a82010-12-21 13:34:31 -0800835 return -ENOMEM;
836 }
837
838 fusion = instance->ctrl_context;
839
840 if (!fusion) {
841 megasas_return_cmd(instance, cmd);
Hannes Reinecke2f8bdfa2014-01-16 11:25:35 +0100842 return -ENXIO;
adam radford9c915a82010-12-21 13:34:31 -0800843 }
844
845 dcmd = &cmd->frame->dcmd;
846
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530847 size_map_info = fusion->current_map_sz;
adam radford9c915a82010-12-21 13:34:31 -0800848
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530849 ci = (void *) fusion->ld_map[(instance->map_id & 1)];
adam radford9c915a82010-12-21 13:34:31 -0800850 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
851
852 if (!ci) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500853 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n");
adam radford9c915a82010-12-21 13:34:31 -0800854 megasas_return_cmd(instance, cmd);
855 return -ENOMEM;
856 }
857
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530858 memset(ci, 0, fusion->max_map_sz);
adam radford9c915a82010-12-21 13:34:31 -0800859 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530860#if VD_EXT_DEBUG
861 dev_dbg(&instance->pdev->dev,
862 "%s sending MR_DCMD_LD_MAP_GET_INFO with size %d\n",
863 __func__, cpu_to_le32(size_map_info));
864#endif
adam radford9c915a82010-12-21 13:34:31 -0800865 dcmd->cmd = MFI_CMD_DCMD;
866 dcmd->cmd_status = 0xFF;
867 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530868 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford9c915a82010-12-21 13:34:31 -0800869 dcmd->timeout = 0;
870 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530871 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
872 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
873 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
874 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -0800875
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530876 if (instance->ctrl_context && !instance->mask_interrupts)
877 ret = megasas_issue_blocked_cmd(instance, cmd,
878 MEGASAS_BLOCKED_CMD_TIMEOUT);
879 else
880 ret = megasas_issue_polled(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -0800881
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +0530882 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -0800883
884 return ret;
885}
886
887u8
888megasas_get_map_info(struct megasas_instance *instance)
889{
890 struct fusion_context *fusion = instance->ctrl_context;
891
892 fusion->fast_path_io = 0;
893 if (!megasas_get_ld_map_info(instance)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +0530894 if (MR_ValidateMapInfo(instance)) {
adam radford9c915a82010-12-21 13:34:31 -0800895 fusion->fast_path_io = 1;
896 return 0;
897 }
898 }
899 return 1;
900}
901
902/*
903 * megasas_sync_map_info - Returns FW's ld_map structure
904 * @instance: Adapter soft state
905 *
906 * Issues an internal command (DCMD) to get the FW's controller PD
907 * list structure. This information is mainly used to find out SYSTEM
908 * supported by the FW.
909 */
910int
911megasas_sync_map_info(struct megasas_instance *instance)
912{
913 int ret = 0, i;
914 struct megasas_cmd *cmd;
915 struct megasas_dcmd_frame *dcmd;
916 u32 size_sync_info, num_lds;
917 struct fusion_context *fusion;
918 struct MR_LD_TARGET_SYNC *ci = NULL;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530919 struct MR_DRV_RAID_MAP_ALL *map;
adam radford9c915a82010-12-21 13:34:31 -0800920 struct MR_LD_RAID *raid;
921 struct MR_LD_TARGET_SYNC *ld_sync;
922 dma_addr_t ci_h = 0;
923 u32 size_map_info;
924
925 cmd = megasas_get_cmd(instance);
926
927 if (!cmd) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -0500928 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n");
adam radford9c915a82010-12-21 13:34:31 -0800929 return -ENOMEM;
930 }
931
932 fusion = instance->ctrl_context;
933
934 if (!fusion) {
935 megasas_return_cmd(instance, cmd);
936 return 1;
937 }
938
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530939 map = fusion->ld_drv_map[instance->map_id & 1];
adam radford9c915a82010-12-21 13:34:31 -0800940
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +0530941 num_lds = le16_to_cpu(map->raidMap.ldCount);
adam radford9c915a82010-12-21 13:34:31 -0800942
943 dcmd = &cmd->frame->dcmd;
944
945 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
946
947 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
948
949 ci = (struct MR_LD_TARGET_SYNC *)
950 fusion->ld_map[(instance->map_id - 1) & 1];
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530951 memset(ci, 0, fusion->max_map_sz);
adam radford9c915a82010-12-21 13:34:31 -0800952
953 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
954
955 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
956
957 for (i = 0; i < num_lds; i++, ld_sync++) {
958 raid = MR_LdRaidGet(i, map);
959 ld_sync->targetId = MR_GetLDTgtId(i, map);
960 ld_sync->seqNum = raid->seqNum;
961 }
962
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530963 size_map_info = fusion->current_map_sz;
adam radford9c915a82010-12-21 13:34:31 -0800964
965 dcmd->cmd = MFI_CMD_DCMD;
966 dcmd->cmd_status = 0xFF;
967 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530968 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
adam radford9c915a82010-12-21 13:34:31 -0800969 dcmd->timeout = 0;
970 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530971 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -0800972 dcmd->mbox.b[0] = num_lds;
973 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530974 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
975 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
976 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -0800977
978 instance->map_update_cmd = cmd;
979
980 instance->instancet->issue_dcmd(instance, cmd);
981
982 return ret;
983}
984
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +0530985/*
986 * meagasas_display_intel_branding - Display branding string
987 * @instance: per adapter object
988 *
989 * Return nothing.
990 */
991static void
992megasas_display_intel_branding(struct megasas_instance *instance)
993{
994 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
995 return;
996
997 switch (instance->pdev->device) {
998 case PCI_DEVICE_ID_LSI_INVADER:
999 switch (instance->pdev->subsystem_device) {
1000 case MEGARAID_INTEL_RS3DC080_SSDID:
1001 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1002 instance->host->host_no,
1003 MEGARAID_INTEL_RS3DC080_BRANDING);
1004 break;
1005 case MEGARAID_INTEL_RS3DC040_SSDID:
1006 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1007 instance->host->host_no,
1008 MEGARAID_INTEL_RS3DC040_BRANDING);
1009 break;
1010 case MEGARAID_INTEL_RS3SC008_SSDID:
1011 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1012 instance->host->host_no,
1013 MEGARAID_INTEL_RS3SC008_BRANDING);
1014 break;
1015 case MEGARAID_INTEL_RS3MC044_SSDID:
1016 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1017 instance->host->host_no,
1018 MEGARAID_INTEL_RS3MC044_BRANDING);
1019 break;
1020 default:
1021 break;
1022 }
1023 break;
1024 case PCI_DEVICE_ID_LSI_FURY:
1025 switch (instance->pdev->subsystem_device) {
1026 case MEGARAID_INTEL_RS3WC080_SSDID:
1027 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1028 instance->host->host_no,
1029 MEGARAID_INTEL_RS3WC080_BRANDING);
1030 break;
1031 case MEGARAID_INTEL_RS3WC040_SSDID:
1032 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
1033 instance->host->host_no,
1034 MEGARAID_INTEL_RS3WC040_BRANDING);
1035 break;
1036 default:
1037 break;
1038 }
1039 break;
1040 default:
1041 break;
1042 }
1043}
1044
adam radford9c915a82010-12-21 13:34:31 -08001045/**
1046 * megasas_init_adapter_fusion - Initializes the FW
1047 * @instance: Adapter soft state
1048 *
1049 * This is the main function for initializing firmware.
1050 */
1051u32
1052megasas_init_adapter_fusion(struct megasas_instance *instance)
1053{
1054 struct megasas_register_set __iomem *reg_set;
1055 struct fusion_context *fusion;
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301056 u32 max_cmd, scratch_pad_2;
adam radfordc8e858f2011-10-08 18:15:13 -07001057 int i = 0, count;
adam radford9c915a82010-12-21 13:34:31 -08001058
1059 fusion = instance->ctrl_context;
1060
1061 reg_set = instance->reg_set;
1062
1063 /*
1064 * Get various operational parameters from status register
1065 */
1066 instance->max_fw_cmds =
1067 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
1068 instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
1069
1070 /*
1071 * Reduce the max supported cmds by 1. This is to ensure that the
1072 * reply_q_sz (1 more than the max cmd that driver may send)
1073 * does not exceed max cmds that the FW can support
1074 */
1075 instance->max_fw_cmds = instance->max_fw_cmds-1;
Sumit.Saxena@avagotech.comae09a6c2015-01-05 20:06:23 +05301076
1077 /*
1078 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
1079 */
1080 instance->max_mfi_cmds =
1081 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
adam radford9c915a82010-12-21 13:34:31 -08001082
1083 max_cmd = instance->max_fw_cmds;
1084
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05301085 fusion->reply_q_depth = 2 * (((max_cmd + 1 + 15)/16)*16);
adam radford9c915a82010-12-21 13:34:31 -08001086
1087 fusion->request_alloc_sz =
1088 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
1089 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
1090 *(fusion->reply_q_depth);
1091 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
1092 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
1093 (max_cmd + 1)); /* Extra 1 for SMID 0 */
1094
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301095 scratch_pad_2 = readl(&instance->reg_set->outbound_scratch_pad_2);
1096 /* If scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set,
1097 * Firmware support extended IO chain frame which is 4 times more than
1098 * legacy Firmware.
1099 * Legacy Firmware - Frame size is (8 * 128) = 1K
1100 * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K
1101 */
1102 if (scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK)
1103 instance->max_chain_frame_sz =
1104 ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1105 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO;
1106 else
1107 instance->max_chain_frame_sz =
1108 ((scratch_pad_2 & MEGASAS_MAX_CHAIN_SIZE_MASK) >>
1109 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO;
1110
1111 if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) {
1112 dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n",
1113 instance->max_chain_frame_sz,
1114 MEGASAS_CHAIN_FRAME_SZ_MIN);
1115 instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN;
1116 }
1117
adam radford9c915a82010-12-21 13:34:31 -08001118 fusion->max_sge_in_main_msg =
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301119 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE
1120 - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
adam radford9c915a82010-12-21 13:34:31 -08001121
1122 fusion->max_sge_in_chain =
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301123 instance->max_chain_frame_sz
1124 / sizeof(union MPI2_SGE_IO_UNION);
adam radford9c915a82010-12-21 13:34:31 -08001125
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301126 instance->max_num_sge =
1127 rounddown_pow_of_two(fusion->max_sge_in_main_msg
1128 + fusion->max_sge_in_chain - 2);
adam radford9c915a82010-12-21 13:34:31 -08001129
1130 /* Used for pass thru MFI frame (DCMD) */
1131 fusion->chain_offset_mfi_pthru =
1132 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1133
1134 fusion->chain_offset_io_request =
1135 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1136 sizeof(union MPI2_SGE_IO_UNION))/16;
1137
adam radfordc8e858f2011-10-08 18:15:13 -07001138 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1139 for (i = 0 ; i < count; i++)
1140 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -08001141
1142 /*
Sumit.Saxena@avagotech.comf26ac3a2015-04-23 16:30:54 +05301143 * For fusion adapters, 3 commands for IOCTL and 5 commands
1144 * for driver's internal DCMDs.
1145 */
1146 instance->max_scsi_cmds = instance->max_fw_cmds -
1147 (MEGASAS_FUSION_INTERNAL_CMDS +
1148 MEGASAS_FUSION_IOCTL_CMDS);
1149 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1150
1151 /*
adam radford9c915a82010-12-21 13:34:31 -08001152 * Allocate memory for descriptors
1153 * Create a pool of commands
1154 */
1155 if (megasas_alloc_cmds(instance))
1156 goto fail_alloc_mfi_cmds;
1157 if (megasas_alloc_cmds_fusion(instance))
1158 goto fail_alloc_cmds;
1159
1160 if (megasas_ioc_init_fusion(instance))
1161 goto fail_ioc_init;
1162
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301163 megasas_display_intel_branding(instance);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05301164 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301165 dev_err(&instance->pdev->dev,
1166 "Could not get controller info. Fail from %s %d\n",
1167 __func__, __LINE__);
1168 goto fail_ioc_init;
1169 }
1170
adam radford9c915a82010-12-21 13:34:31 -08001171 instance->flag_ieee = 1;
adam radford9c915a82010-12-21 13:34:31 -08001172 fusion->fast_path_io = 0;
1173
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301174 fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1175 for (i = 0; i < 2; i++) {
1176 fusion->ld_map[i] = NULL;
1177 fusion->ld_drv_map[i] = (void *)__get_free_pages(GFP_KERNEL,
1178 fusion->drv_map_pages);
1179 if (!fusion->ld_drv_map[i]) {
1180 dev_err(&instance->pdev->dev, "Could not allocate "
1181 "memory for local map info for %d pages\n",
1182 fusion->drv_map_pages);
1183 if (i == 1)
1184 free_pages((ulong)fusion->ld_drv_map[0],
1185 fusion->drv_map_pages);
1186 goto fail_ioc_init;
1187 }
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05301188 memset(fusion->ld_drv_map[i], 0,
1189 ((1 << PAGE_SHIFT) << fusion->drv_map_pages));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301190 }
1191
adam radford9c915a82010-12-21 13:34:31 -08001192 for (i = 0; i < 2; i++) {
1193 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301194 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08001195 &fusion->ld_map_phys[i],
1196 GFP_KERNEL);
1197 if (!fusion->ld_map[i]) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001198 dev_err(&instance->pdev->dev, "Could not allocate memory "
adam radford9c915a82010-12-21 13:34:31 -08001199 "for map info\n");
1200 goto fail_map_info;
1201 }
1202 }
1203
1204 if (!megasas_get_map_info(instance))
1205 megasas_sync_map_info(instance);
1206
1207 return 0;
1208
adam radford9c915a82010-12-21 13:34:31 -08001209fail_map_info:
1210 if (i == 1)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301211 dma_free_coherent(&instance->pdev->dev, fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08001212 fusion->ld_map[0], fusion->ld_map_phys[0]);
1213fail_ioc_init:
adam radfordeb1b1232011-02-24 20:55:56 -08001214 megasas_free_cmds_fusion(instance);
1215fail_alloc_cmds:
1216 megasas_free_cmds(instance);
1217fail_alloc_mfi_cmds:
adam radford9c915a82010-12-21 13:34:31 -08001218 return 1;
1219}
1220
1221/**
adam radford9c915a82010-12-21 13:34:31 -08001222 * map_cmd_status - Maps FW cmd status to OS cmd status
1223 * @cmd : Pointer to cmd
1224 * @status : status of cmd returned by FW
1225 * @ext_status : ext status of cmd returned by FW
1226 */
1227
1228void
1229map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1230{
1231
1232 switch (status) {
1233
1234 case MFI_STAT_OK:
1235 cmd->scmd->result = DID_OK << 16;
1236 break;
1237
1238 case MFI_STAT_SCSI_IO_FAILED:
1239 case MFI_STAT_LD_INIT_IN_PROGRESS:
1240 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1241 break;
1242
1243 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1244
1245 cmd->scmd->result = (DID_OK << 16) | ext_status;
1246 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1247 memset(cmd->scmd->sense_buffer, 0,
1248 SCSI_SENSE_BUFFERSIZE);
1249 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1250 SCSI_SENSE_BUFFERSIZE);
1251 cmd->scmd->result |= DRIVER_SENSE << 24;
1252 }
1253 break;
1254
1255 case MFI_STAT_LD_OFFLINE:
1256 case MFI_STAT_DEVICE_NOT_FOUND:
1257 cmd->scmd->result = DID_BAD_TARGET << 16;
1258 break;
adam radford36807e62011-10-08 18:15:06 -07001259 case MFI_STAT_CONFIG_SEQ_MISMATCH:
1260 cmd->scmd->result = DID_IMM_RETRY << 16;
1261 break;
adam radford9c915a82010-12-21 13:34:31 -08001262 default:
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001263 dev_printk(KERN_DEBUG, &cmd->instance->pdev->dev, "FW status %#x\n", status);
adam radford9c915a82010-12-21 13:34:31 -08001264 cmd->scmd->result = DID_ERROR << 16;
1265 break;
1266 }
1267}
1268
1269/**
1270 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1271 * @instance: Adapter soft state
1272 * @scp: SCSI command from the mid-layer
1273 * @sgl_ptr: SGL to be filled in
1274 * @cmd: cmd we are working on
1275 *
1276 * If successful, this function returns the number of SG elements.
1277 */
1278static int
1279megasas_make_sgl_fusion(struct megasas_instance *instance,
1280 struct scsi_cmnd *scp,
1281 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1282 struct megasas_cmd_fusion *cmd)
1283{
adam radford36807e62011-10-08 18:15:06 -07001284 int i, sg_processed, sge_count;
adam radford9c915a82010-12-21 13:34:31 -08001285 struct scatterlist *os_sgl;
1286 struct fusion_context *fusion;
1287
1288 fusion = instance->ctrl_context;
1289
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05301290 if (fusion->adapter_type == INVADER_SERIES) {
adam radford36807e62011-10-08 18:15:06 -07001291 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1292 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1293 sgl_ptr_end->Flags = 0;
1294 }
adam radford9c915a82010-12-21 13:34:31 -08001295
1296 sge_count = scsi_dma_map(scp);
1297
1298 BUG_ON(sge_count < 0);
1299
1300 if (sge_count > instance->max_num_sge || !sge_count)
1301 return sge_count;
1302
adam radford9c915a82010-12-21 13:34:31 -08001303 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301304 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
1305 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
adam radford9c915a82010-12-21 13:34:31 -08001306 sgl_ptr->Flags = 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05301307 if (fusion->adapter_type == INVADER_SERIES)
adam radford36807e62011-10-08 18:15:06 -07001308 if (i == sge_count - 1)
1309 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
adam radford9c915a82010-12-21 13:34:31 -08001310 sgl_ptr++;
1311
1312 sg_processed = i + 1;
1313
1314 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1315 (sge_count > fusion->max_sge_in_main_msg)) {
1316
1317 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05301318 if (fusion->adapter_type == INVADER_SERIES) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301319 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1320 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1321 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
adam radford36807e62011-10-08 18:15:06 -07001322 cmd->io_request->ChainOffset =
1323 fusion->
1324 chain_offset_io_request;
1325 else
1326 cmd->io_request->ChainOffset = 0;
1327 } else
1328 cmd->io_request->ChainOffset =
1329 fusion->chain_offset_io_request;
1330
adam radford9c915a82010-12-21 13:34:31 -08001331 sg_chain = sgl_ptr;
1332 /* Prepare chain element */
1333 sg_chain->NextChainOffset = 0;
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05301334 if (fusion->adapter_type == INVADER_SERIES)
adam radford36807e62011-10-08 18:15:06 -07001335 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1336 else
1337 sg_chain->Flags =
1338 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1339 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301340 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
1341 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08001342
1343 sgl_ptr =
1344 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301345 memset(sgl_ptr, 0, instance->max_chain_frame_sz);
adam radford9c915a82010-12-21 13:34:31 -08001346 }
1347 }
1348
1349 return sge_count;
1350}
1351
1352/**
1353 * megasas_set_pd_lba - Sets PD LBA
1354 * @cdb: CDB
1355 * @cdb_len: cdb length
1356 * @start_blk: Start block of IO
1357 *
1358 * Used to set the PD LBA in CDB for FP IOs
1359 */
1360void
1361megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1362 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301363 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
adam radford9c915a82010-12-21 13:34:31 -08001364{
1365 struct MR_LD_RAID *raid;
1366 u32 ld;
1367 u64 start_blk = io_info->pdBlock;
1368 u8 *cdb = io_request->CDB.CDB32;
1369 u32 num_blocks = io_info->numBlocks;
adam radford495c5602011-05-11 18:34:40 -07001370 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
adam radford9c915a82010-12-21 13:34:31 -08001371
1372 /* Check if T10 PI (DIF) is enabled for this LD */
1373 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1374 raid = MR_LdRaidGet(ld, local_map_ptr);
1375 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1376 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1377 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1378 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1379
1380 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1381 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1382 else
1383 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1384 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1385
1386 /* LBA */
1387 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1388 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1389 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1390 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1391 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1392 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1393 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1394 cdb[19] = (u8)(start_blk & 0xff);
1395
1396 /* Logical block reference tag */
1397 io_request->CDB.EEDP32.PrimaryReferenceTag =
1398 cpu_to_be32(ref_tag);
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301399 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301400 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001401
1402 /* Transfer length */
1403 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1404 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1405 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1406 cdb[31] = (u8)(num_blocks & 0xff);
1407
1408 /* set SCSI IO EEDPFlags */
1409 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301410 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001411 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1412 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1413 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1414 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301415 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
adam radford9c915a82010-12-21 13:34:31 -08001416 } else {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301417 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001418 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301419 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
adam radford9c915a82010-12-21 13:34:31 -08001420 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301421 io_request->Control |= cpu_to_le32((0x4 << 26));
1422 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
adam radford9c915a82010-12-21 13:34:31 -08001423 } else {
1424 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1425 if (((cdb_len == 12) || (cdb_len == 16)) &&
1426 (start_blk <= 0xffffffff)) {
1427 if (cdb_len == 16) {
1428 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1429 flagvals = cdb[1];
1430 groupnum = cdb[14];
1431 control = cdb[15];
1432 } else {
1433 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1434 flagvals = cdb[1];
1435 groupnum = cdb[10];
1436 control = cdb[11];
1437 }
1438
1439 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1440
1441 cdb[0] = opcode;
1442 cdb[1] = flagvals;
1443 cdb[6] = groupnum;
1444 cdb[9] = control;
1445
1446 /* Transfer length */
1447 cdb[8] = (u8)(num_blocks & 0xff);
1448 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1449
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301450 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001451 cdb_len = 10;
adam radford495c5602011-05-11 18:34:40 -07001452 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1453 /* Convert to 16 byte CDB for large LBA's */
1454 switch (cdb_len) {
1455 case 6:
1456 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1457 control = cdb[5];
1458 break;
1459 case 10:
1460 opcode =
1461 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1462 flagvals = cdb[1];
1463 groupnum = cdb[6];
1464 control = cdb[9];
1465 break;
1466 case 12:
1467 opcode =
1468 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1469 flagvals = cdb[1];
1470 groupnum = cdb[10];
1471 control = cdb[11];
1472 break;
1473 }
1474
1475 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1476
1477 cdb[0] = opcode;
1478 cdb[1] = flagvals;
1479 cdb[14] = groupnum;
1480 cdb[15] = control;
1481
1482 /* Transfer length */
1483 cdb[13] = (u8)(num_blocks & 0xff);
1484 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1485 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1486 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1487
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301488 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
adam radford495c5602011-05-11 18:34:40 -07001489 cdb_len = 16;
adam radford9c915a82010-12-21 13:34:31 -08001490 }
1491
1492 /* Normal case, just load LBA here */
1493 switch (cdb_len) {
1494 case 6:
1495 {
1496 u8 val = cdb[1] & 0xE0;
1497 cdb[3] = (u8)(start_blk & 0xff);
1498 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1499 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1500 break;
1501 }
1502 case 10:
1503 cdb[5] = (u8)(start_blk & 0xff);
1504 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1505 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1506 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1507 break;
1508 case 12:
1509 cdb[5] = (u8)(start_blk & 0xff);
1510 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1511 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1512 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1513 break;
1514 case 16:
1515 cdb[9] = (u8)(start_blk & 0xff);
1516 cdb[8] = (u8)((start_blk >> 8) & 0xff);
1517 cdb[7] = (u8)((start_blk >> 16) & 0xff);
1518 cdb[6] = (u8)((start_blk >> 24) & 0xff);
1519 cdb[5] = (u8)((start_blk >> 32) & 0xff);
1520 cdb[4] = (u8)((start_blk >> 40) & 0xff);
1521 cdb[3] = (u8)((start_blk >> 48) & 0xff);
1522 cdb[2] = (u8)((start_blk >> 56) & 0xff);
1523 break;
1524 }
1525 }
1526}
1527
1528/**
1529 * megasas_build_ldio_fusion - Prepares IOs to devices
1530 * @instance: Adapter soft state
1531 * @scp: SCSI command
1532 * @cmd: Command to be prepared
1533 *
1534 * Prepares the io_request and chain elements (sg_frame) for IO
1535 * The IO can be for PD (Fast Path) or LD
1536 */
1537void
1538megasas_build_ldio_fusion(struct megasas_instance *instance,
1539 struct scsi_cmnd *scp,
1540 struct megasas_cmd_fusion *cmd)
1541{
1542 u8 fp_possible;
adam radfordf9eff812012-10-01 19:27:07 -07001543 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
adam radford9c915a82010-12-21 13:34:31 -08001544 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1545 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1546 struct IO_REQUEST_INFO io_info;
1547 struct fusion_context *fusion;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301548 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
adam radford21c9e162013-09-06 15:27:14 -07001549 u8 *raidLUN;
adam radford9c915a82010-12-21 13:34:31 -08001550
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301551 device_id = MEGASAS_DEV_INDEX(scp);
adam radford9c915a82010-12-21 13:34:31 -08001552
1553 fusion = instance->ctrl_context;
1554
1555 io_request = cmd->io_request;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301556 io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
adam radford9c915a82010-12-21 13:34:31 -08001557 io_request->RaidContext.status = 0;
1558 io_request->RaidContext.exStatus = 0;
1559
1560 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1561
1562 start_lba_lo = 0;
1563 start_lba_hi = 0;
1564 fp_possible = 0;
1565
1566 /*
1567 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1568 */
1569 if (scp->cmd_len == 6) {
adam radfordf9eff812012-10-01 19:27:07 -07001570 datalength = (u32) scp->cmnd[4];
adam radford9c915a82010-12-21 13:34:31 -08001571 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1572 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1573
1574 start_lba_lo &= 0x1FFFFF;
1575 }
1576
1577 /*
1578 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1579 */
1580 else if (scp->cmd_len == 10) {
adam radfordf9eff812012-10-01 19:27:07 -07001581 datalength = (u32) scp->cmnd[8] |
adam radford9c915a82010-12-21 13:34:31 -08001582 ((u32) scp->cmnd[7] << 8);
1583 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1584 ((u32) scp->cmnd[3] << 16) |
1585 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1586 }
1587
1588 /*
1589 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1590 */
1591 else if (scp->cmd_len == 12) {
adam radfordf9eff812012-10-01 19:27:07 -07001592 datalength = ((u32) scp->cmnd[6] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08001593 ((u32) scp->cmnd[7] << 16) |
1594 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1595 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1596 ((u32) scp->cmnd[3] << 16) |
1597 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1598 }
1599
1600 /*
1601 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1602 */
1603 else if (scp->cmd_len == 16) {
adam radfordf9eff812012-10-01 19:27:07 -07001604 datalength = ((u32) scp->cmnd[10] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08001605 ((u32) scp->cmnd[11] << 16) |
1606 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1607 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1608 ((u32) scp->cmnd[7] << 16) |
1609 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1610
1611 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1612 ((u32) scp->cmnd[3] << 16) |
1613 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1614 }
1615
1616 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1617 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
adam radfordf9eff812012-10-01 19:27:07 -07001618 io_info.numBlocks = datalength;
adam radford9c915a82010-12-21 13:34:31 -08001619 io_info.ldTgtId = device_id;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301620 io_request->DataLength = cpu_to_le32(scsi_bufflen(scp));
adam radford9c915a82010-12-21 13:34:31 -08001621
1622 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1623 io_info.isRead = 1;
1624
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301625 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
adam radford9c915a82010-12-21 13:34:31 -08001626
1627 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301628 instance->fw_supported_vd_count) || (!fusion->fast_path_io)) {
adam radford9c915a82010-12-21 13:34:31 -08001629 io_request->RaidContext.regLockFlags = 0;
1630 fp_possible = 0;
1631 } else {
adam radford36807e62011-10-08 18:15:06 -07001632 if (MR_BuildRaidContext(instance, &io_info,
1633 &io_request->RaidContext,
adam radford21c9e162013-09-06 15:27:14 -07001634 local_map_ptr, &raidLUN))
adam radford9c915a82010-12-21 13:34:31 -08001635 fp_possible = io_info.fpOkForIo;
1636 }
1637
Christoph Hellwig16b85282015-04-15 09:44:37 -07001638 /* Use raw_smp_processor_id() for now until cmd->request->cpu is CPU
adam radfordc8e858f2011-10-08 18:15:13 -07001639 id by default, not CPU group id, otherwise all MSI-X queues won't
1640 be utilized */
1641 cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
Christoph Hellwig16b85282015-04-15 09:44:37 -07001642 raw_smp_processor_id() % instance->msix_vectors : 0;
adam radfordc8e858f2011-10-08 18:15:13 -07001643
adam radford9c915a82010-12-21 13:34:31 -08001644 if (fp_possible) {
1645 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1646 local_map_ptr, start_lba_lo);
adam radford9c915a82010-12-21 13:34:31 -08001647 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1648 cmd->request_desc->SCSIIO.RequestFlags =
1649 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1650 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05301651 if (fusion->adapter_type == INVADER_SERIES) {
adam radford36807e62011-10-08 18:15:06 -07001652 if (io_request->RaidContext.regLockFlags ==
1653 REGION_TYPE_UNUSED)
1654 cmd->request_desc->SCSIIO.RequestFlags =
1655 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1656 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1657 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1658 io_request->RaidContext.nseg = 0x1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301659 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
adam radford36807e62011-10-08 18:15:06 -07001660 io_request->RaidContext.regLockFlags |=
1661 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
1662 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1663 }
adam radford9c915a82010-12-21 13:34:31 -08001664 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1665 (io_info.isRead)) {
1666 io_info.devHandle =
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05301667 get_updated_dev_handle(instance,
adam radford9c915a82010-12-21 13:34:31 -08001668 &fusion->load_balance_info[device_id],
1669 &io_info);
1670 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05301671 cmd->pd_r1_lb = io_info.pd_after_lb;
adam radford9c915a82010-12-21 13:34:31 -08001672 } else
1673 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05301674
1675 if ((raidLUN[0] == 1) &&
sumit.saxena@avagotech.com75b96061e2015-08-31 17:24:01 +05301676 (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) {
Sumit.Saxena@avagotech.com5765c5b2015-04-23 16:32:09 +05301677 instance->dev_handle = !(instance->dev_handle);
1678 io_info.devHandle =
1679 local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle];
1680 }
1681
adam radford9c915a82010-12-21 13:34:31 -08001682 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1683 io_request->DevHandle = io_info.devHandle;
adam radford21c9e162013-09-06 15:27:14 -07001684 /* populate the LUN field */
1685 memcpy(io_request->LUN, raidLUN, 8);
adam radford9c915a82010-12-21 13:34:31 -08001686 } else {
1687 io_request->RaidContext.timeoutValue =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301688 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
adam radford9c915a82010-12-21 13:34:31 -08001689 cmd->request_desc->SCSIIO.RequestFlags =
1690 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1691 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05301692 if (fusion->adapter_type == INVADER_SERIES) {
adam radford36807e62011-10-08 18:15:06 -07001693 if (io_request->RaidContext.regLockFlags ==
1694 REGION_TYPE_UNUSED)
1695 cmd->request_desc->SCSIIO.RequestFlags =
1696 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1697 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1698 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1699 io_request->RaidContext.regLockFlags |=
1700 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
1701 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1702 io_request->RaidContext.nseg = 0x1;
1703 }
1704 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301705 io_request->DevHandle = cpu_to_le16(device_id);
adam radford9c915a82010-12-21 13:34:31 -08001706 } /* Not FP */
1707}
1708
1709/**
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301710 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
adam radford9c915a82010-12-21 13:34:31 -08001711 * @instance: Adapter soft state
1712 * @scp: SCSI command
1713 * @cmd: Command to be prepared
1714 *
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301715 * Prepares the io_request frame for non-rw io cmds for vd.
adam radford9c915a82010-12-21 13:34:31 -08001716 */
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301717static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
1718 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
adam radford9c915a82010-12-21 13:34:31 -08001719{
1720 u32 device_id;
1721 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1722 u16 pd_index = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301723 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
adam radford9c915a82010-12-21 13:34:31 -08001724 struct fusion_context *fusion = instance->ctrl_context;
adam radford21c9e162013-09-06 15:27:14 -07001725 u8 span, physArm;
Christoph Hellwig9ab9ed382015-04-23 16:32:54 +05301726 __le16 devHandle;
adam radford21c9e162013-09-06 15:27:14 -07001727 u32 ld, arRef, pd;
1728 struct MR_LD_RAID *raid;
1729 struct RAID_CONTEXT *pRAID_Context;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301730 u8 fp_possible = 1;
adam radford9c915a82010-12-21 13:34:31 -08001731
1732 io_request = cmd->io_request;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301733 device_id = MEGASAS_DEV_INDEX(scmd);
1734 pd_index = MEGASAS_PD_INDEX(scmd);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301735 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301736 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301737 /* get RAID_Context pointer */
1738 pRAID_Context = &io_request->RaidContext;
1739 /* Check with FW team */
1740 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
1741 pRAID_Context->regLockRowLBA = 0;
1742 pRAID_Context->regLockLength = 0;
adam radford21c9e162013-09-06 15:27:14 -07001743
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301744 if (fusion->fast_path_io && (
1745 device_id < instance->fw_supported_vd_count)) {
Sumit.Saxena@avagotech.comab2f0602015-01-05 20:06:08 +05301746
adam radford21c9e162013-09-06 15:27:14 -07001747 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
Sumit.Saxena@avagotech.comab2f0602015-01-05 20:06:08 +05301748 if (ld >= instance->fw_supported_vd_count)
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301749 fp_possible = 0;
adam radford21c9e162013-09-06 15:27:14 -07001750
1751 raid = MR_LdRaidGet(ld, local_map_ptr);
adam radford21c9e162013-09-06 15:27:14 -07001752 if (!(raid->capability.fpNonRWCapable))
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301753 fp_possible = 0;
1754 } else
1755 fp_possible = 0;
adam radford21c9e162013-09-06 15:27:14 -07001756
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301757 if (!fp_possible) {
1758 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1759 io_request->DevHandle = cpu_to_le16(device_id);
1760 io_request->LUN[1] = scmd->device->lun;
1761 pRAID_Context->timeoutValue =
1762 cpu_to_le16 (scmd->request->timeout / HZ);
1763 cmd->request_desc->SCSIIO.RequestFlags =
1764 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1765 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1766 } else {
adam radford21c9e162013-09-06 15:27:14 -07001767
1768 /* set RAID context values */
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301769 pRAID_Context->configSeqNum = raid->seqNum;
1770 pRAID_Context->regLockFlags = REGION_TYPE_SHARED_READ;
1771 pRAID_Context->timeoutValue = cpu_to_le16(raid->fpIoTimeoutForLd);
adam radford21c9e162013-09-06 15:27:14 -07001772
1773 /* get the DevHandle for the PD (since this is
1774 fpNonRWCapable, this is a single disk RAID0) */
1775 span = physArm = 0;
1776 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
1777 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
1778 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
1779
1780 /* build request descriptor */
1781 cmd->request_desc->SCSIIO.RequestFlags =
1782 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301783 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford21c9e162013-09-06 15:27:14 -07001784 cmd->request_desc->SCSIIO.DevHandle = devHandle;
1785
1786 /* populate the LUN field */
1787 memcpy(io_request->LUN, raid->LUN, 8);
1788
1789 /* build the raidScsiIO structure */
1790 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1791 io_request->DevHandle = devHandle;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301792 }
1793}
adam radford21c9e162013-09-06 15:27:14 -07001794
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301795/**
1796 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
1797 * @instance: Adapter soft state
1798 * @scp: SCSI command
1799 * @cmd: Command to be prepared
1800 * @fp_possible: parameter to detect fast path or firmware path io.
1801 *
1802 * Prepares the io_request frame for rw/non-rw io cmds for syspds
1803 */
1804static void
1805megasas_build_syspd_fusion(struct megasas_instance *instance,
1806 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd, u8 fp_possible)
1807{
1808 u32 device_id;
1809 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1810 u16 pd_index = 0;
1811 u16 os_timeout_value;
1812 u16 timeout_limit;
1813 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1814 struct RAID_CONTEXT *pRAID_Context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301815 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301816 struct fusion_context *fusion = instance->ctrl_context;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301817 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1];
adam radford21c9e162013-09-06 15:27:14 -07001818
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301819 device_id = MEGASAS_DEV_INDEX(scmd);
1820 pd_index = MEGASAS_PD_INDEX(scmd);
1821 os_timeout_value = scmd->request->timeout / HZ;
1822
1823 io_request = cmd->io_request;
1824 /* get RAID_Context pointer */
1825 pRAID_Context = &io_request->RaidContext;
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301826 pRAID_Context->regLockFlags = 0;
1827 pRAID_Context->regLockRowLBA = 0;
1828 pRAID_Context->regLockLength = 0;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301829 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
1830 io_request->LUN[1] = scmd->device->lun;
1831 pRAID_Context->RAIDFlags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
1832 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1833
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301834 /* If FW supports PD sequence number */
1835 if (instance->use_seqnum_jbod_fp &&
1836 instance->pd_list[pd_index].driveType == TYPE_DISK) {
1837 /* TgtId must be incremented by 255 as jbod seq number is index
1838 * below raid map
1839 */
1840 pRAID_Context->VirtualDiskTgtId =
1841 cpu_to_le16(device_id + (MAX_PHYSICAL_DEVICES - 1));
1842 pRAID_Context->configSeqNum = pd_sync->seq[pd_index].seqNum;
1843 io_request->DevHandle = pd_sync->seq[pd_index].devHandle;
1844 pRAID_Context->regLockFlags |=
1845 (MR_RL_FLAGS_SEQ_NUM_ENABLE|MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
1846 } else if (fusion->fast_path_io) {
1847 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
1848 pRAID_Context->configSeqNum = 0;
1849 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1850 io_request->DevHandle =
1851 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1852 } else {
1853 /* Want to send all IO via FW path */
1854 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
1855 pRAID_Context->configSeqNum = 0;
1856 io_request->DevHandle = le16_to_cpu(0xFFFF);
1857 }
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301858
1859 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
1860 cmd->request_desc->SCSIIO.MSIxIndex =
1861 instance->msix_vectors ?
1862 (raw_smp_processor_id() % instance->msix_vectors) : 0;
1863
1864
1865 if (!fp_possible) {
1866 /* system pd firmware path */
adam radford9c915a82010-12-21 13:34:31 -08001867 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
adam radford9c915a82010-12-21 13:34:31 -08001868 cmd->request_desc->SCSIIO.RequestFlags =
1869 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301870 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1871 pRAID_Context->timeoutValue = cpu_to_le16(os_timeout_value);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05301872 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301873 } else {
1874 /* system pd Fast Path */
1875 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301876 timeout_limit = (scmd->device->type == TYPE_DISK) ?
1877 255 : 0xFFFF;
1878 pRAID_Context->timeoutValue =
1879 cpu_to_le16((os_timeout_value > timeout_limit) ?
1880 timeout_limit : os_timeout_value);
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05301881 if (fusion->adapter_type == INVADER_SERIES) {
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301882 pRAID_Context->Type = MPI2_TYPE_CUDA;
1883 pRAID_Context->nseg = 0x1;
1884 io_request->IoFlags |=
1885 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
1886 }
1887 cmd->request_desc->SCSIIO.RequestFlags =
1888 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1889 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford9c915a82010-12-21 13:34:31 -08001890 }
adam radford9c915a82010-12-21 13:34:31 -08001891}
1892
1893/**
1894 * megasas_build_io_fusion - Prepares IOs to devices
1895 * @instance: Adapter soft state
1896 * @scp: SCSI command
1897 * @cmd: Command to be prepared
1898 *
1899 * Invokes helper functions to prepare request frames
1900 * and sets flags appropriate for IO/Non-IO cmd
1901 */
1902int
1903megasas_build_io_fusion(struct megasas_instance *instance,
1904 struct scsi_cmnd *scp,
1905 struct megasas_cmd_fusion *cmd)
1906{
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301907 u16 sge_count;
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301908 u8 cmd_type;
adam radford9c915a82010-12-21 13:34:31 -08001909 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1910
adam radford9c915a82010-12-21 13:34:31 -08001911 /* Zero out some fields so they don't get reused */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001912 memset(io_request->LUN, 0x0, 8);
adam radford9c915a82010-12-21 13:34:31 -08001913 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1914 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1915 io_request->EEDPFlags = 0;
1916 io_request->Control = 0;
1917 io_request->EEDPBlockSize = 0;
adam radford36807e62011-10-08 18:15:06 -07001918 io_request->ChainOffset = 0;
adam radford9c915a82010-12-21 13:34:31 -08001919 io_request->RaidContext.RAIDFlags = 0;
adam radford36807e62011-10-08 18:15:06 -07001920 io_request->RaidContext.Type = 0;
1921 io_request->RaidContext.nseg = 0;
adam radford9c915a82010-12-21 13:34:31 -08001922
1923 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1924 /*
1925 * Just the CDB length,rest of the Flags are zero
1926 * This will be modified for FP in build_ldio_fusion
1927 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301928 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
adam radford9c915a82010-12-21 13:34:31 -08001929
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301930 switch (cmd_type = megasas_cmd_type(scp)) {
1931 case READ_WRITE_LDIO:
adam radford9c915a82010-12-21 13:34:31 -08001932 megasas_build_ldio_fusion(instance, scp, cmd);
Sumit.Saxena@avagotech.com4a5c8142015-04-23 16:30:39 +05301933 break;
1934 case NON_READ_WRITE_LDIO:
1935 megasas_build_ld_nonrw_fusion(instance, scp, cmd);
1936 break;
1937 case READ_WRITE_SYSPDIO:
1938 case NON_READ_WRITE_SYSPDIO:
1939 if (instance->secure_jbod_support &&
1940 (cmd_type == NON_READ_WRITE_SYSPDIO))
1941 megasas_build_syspd_fusion(instance, scp, cmd, 0);
1942 else
1943 megasas_build_syspd_fusion(instance, scp, cmd, 1);
1944 break;
1945 default:
1946 break;
1947 }
adam radford9c915a82010-12-21 13:34:31 -08001948
1949 /*
1950 * Construct SGL
1951 */
1952
1953 sge_count =
1954 megasas_make_sgl_fusion(instance, scp,
1955 (struct MPI25_IEEE_SGE_CHAIN64 *)
1956 &io_request->SGL, cmd);
1957
1958 if (sge_count > instance->max_num_sge) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001959 dev_err(&instance->pdev->dev, "Error. sge_count (0x%x) exceeds "
adam radford9c915a82010-12-21 13:34:31 -08001960 "max (0x%x) allowed\n", sge_count,
1961 instance->max_num_sge);
1962 return 1;
1963 }
1964
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301965 /* numSGE store lower 8 bit of sge_count.
1966 * numSGEExt store higher 8 bit of sge_count
1967 */
adam radford9c915a82010-12-21 13:34:31 -08001968 io_request->RaidContext.numSGE = sge_count;
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05301969 io_request->RaidContext.numSGEExt = (u8)(sge_count >> 8);
adam radford9c915a82010-12-21 13:34:31 -08001970
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301971 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
adam radford9c915a82010-12-21 13:34:31 -08001972
1973 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301974 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
adam radford9c915a82010-12-21 13:34:31 -08001975 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301976 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
adam radford9c915a82010-12-21 13:34:31 -08001977
1978 io_request->SGLOffset0 =
1979 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1980
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301981 io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08001982 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1983
1984 cmd->scmd = scp;
1985 scp->SCp.ptr = (char *)cmd;
1986
1987 return 0;
1988}
1989
1990union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1991megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1992{
1993 u8 *p;
1994 struct fusion_context *fusion;
1995
1996 if (index >= instance->max_fw_cmds) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05001997 dev_err(&instance->pdev->dev, "Invalid SMID (0x%x)request for "
adam radford229fe472014-03-10 02:51:56 -07001998 "descriptor for scsi%d\n", index,
1999 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002000 return NULL;
2001 }
2002 fusion = instance->ctrl_context;
2003 p = fusion->req_frames_desc
2004 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
2005
2006 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
2007}
2008
2009/**
2010 * megasas_build_and_issue_cmd_fusion -Main routine for building and
2011 * issuing non IOCTL cmd
2012 * @instance: Adapter soft state
2013 * @scmd: pointer to scsi cmd from OS
2014 */
2015static u32
2016megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
2017 struct scsi_cmnd *scmd)
2018{
2019 struct megasas_cmd_fusion *cmd;
2020 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2021 u32 index;
2022 struct fusion_context *fusion;
2023
2024 fusion = instance->ctrl_context;
2025
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302026 cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
adam radford9c915a82010-12-21 13:34:31 -08002027
2028 index = cmd->index;
2029
2030 req_desc = megasas_get_request_descriptor(instance, index-1);
2031 if (!req_desc)
2032 return 1;
2033
2034 req_desc->Words = 0;
2035 cmd->request_desc = req_desc;
adam radford9c915a82010-12-21 13:34:31 -08002036
2037 if (megasas_build_io_fusion(instance, scmd, cmd)) {
2038 megasas_return_cmd_fusion(instance, cmd);
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002039 dev_err(&instance->pdev->dev, "Error building command\n");
adam radford9c915a82010-12-21 13:34:31 -08002040 cmd->request_desc = NULL;
2041 return 1;
2042 }
2043
2044 req_desc = cmd->request_desc;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302045 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08002046
2047 if (cmd->io_request->ChainOffset != 0 &&
2048 cmd->io_request->ChainOffset != 0xF)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002049 dev_err(&instance->pdev->dev, "The chain offset value is not "
adam radford9c915a82010-12-21 13:34:31 -08002050 "correct : %x\n", cmd->io_request->ChainOffset);
2051
2052 /*
2053 * Issue the command to the FW
2054 */
2055 atomic_inc(&instance->fw_outstanding);
2056
Christoph Hellwig01d7f032015-04-23 16:34:09 +05302057 megasas_fire_cmd_fusion(instance, req_desc);
adam radford9c915a82010-12-21 13:34:31 -08002058
2059 return 0;
2060}
2061
2062/**
2063 * complete_cmd_fusion - Completes command
2064 * @instance: Adapter soft state
2065 * Completes all commands that is in reply descriptor queue
2066 */
2067int
adam radfordc8e858f2011-10-08 18:15:13 -07002068complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
adam radford9c915a82010-12-21 13:34:31 -08002069{
2070 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
2071 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
2072 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
2073 struct fusion_context *fusion;
2074 struct megasas_cmd *cmd_mfi;
2075 struct megasas_cmd_fusion *cmd_fusion;
2076 u16 smid, num_completed;
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05302077 u8 reply_descript_type;
adam radford9c915a82010-12-21 13:34:31 -08002078 u32 status, extStatus, device_id;
2079 union desc_value d_val;
2080 struct LD_LOAD_BALANCE_INFO *lbinfo;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05302081 int threshold_reply_count = 0;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302082 struct scsi_cmnd *scmd_local = NULL;
adam radford9c915a82010-12-21 13:34:31 -08002083
2084 fusion = instance->ctrl_context;
2085
2086 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
2087 return IRQ_HANDLED;
2088
2089 desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -07002090 desc += ((MSIxIndex * fusion->reply_alloc_sz)/
2091 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
2092 fusion->last_reply_idx[MSIxIndex];
adam radford9c915a82010-12-21 13:34:31 -08002093
2094 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
2095
2096 d_val.word = desc->Words;
2097
2098 reply_descript_type = reply_desc->ReplyFlags &
2099 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
2100
2101 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
2102 return IRQ_NONE;
2103
adam radford9c915a82010-12-21 13:34:31 -08002104 num_completed = 0;
2105
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05302106 while (d_val.u.low != cpu_to_le32(UINT_MAX) &&
2107 d_val.u.high != cpu_to_le32(UINT_MAX)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302108 smid = le16_to_cpu(reply_desc->SMID);
adam radford9c915a82010-12-21 13:34:31 -08002109
2110 cmd_fusion = fusion->cmd_list[smid - 1];
2111
2112 scsi_io_req =
2113 (struct MPI2_RAID_SCSI_IO_REQUEST *)
2114 cmd_fusion->io_request;
2115
2116 if (cmd_fusion->scmd)
2117 cmd_fusion->scmd->SCp.ptr = NULL;
2118
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302119 scmd_local = cmd_fusion->scmd;
adam radford9c915a82010-12-21 13:34:31 -08002120 status = scsi_io_req->RaidContext.status;
2121 extStatus = scsi_io_req->RaidContext.exStatus;
2122
2123 switch (scsi_io_req->Function) {
2124 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
2125 /* Update load balancing info */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302126 device_id = MEGASAS_DEV_INDEX(scmd_local);
adam radford9c915a82010-12-21 13:34:31 -08002127 lbinfo = &fusion->load_balance_info[device_id];
2128 if (cmd_fusion->scmd->SCp.Status &
2129 MEGASAS_LOAD_BALANCE_FLAG) {
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05302130 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
adam radford9c915a82010-12-21 13:34:31 -08002131 cmd_fusion->scmd->SCp.Status &=
2132 ~MEGASAS_LOAD_BALANCE_FLAG;
2133 }
2134 if (reply_descript_type ==
2135 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
2136 if (megasas_dbg_lvl == 5)
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002137 dev_err(&instance->pdev->dev, "\nFAST Path "
adam radford9c915a82010-12-21 13:34:31 -08002138 "IO Success\n");
2139 }
2140 /* Fall thru and complete IO */
2141 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
2142 /* Map the FW Cmd Status */
2143 map_cmd_status(cmd_fusion, status, extStatus);
adam radford9c915a82010-12-21 13:34:31 -08002144 scsi_io_req->RaidContext.status = 0;
2145 scsi_io_req->RaidContext.exStatus = 0;
2146 megasas_return_cmd_fusion(instance, cmd_fusion);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302147 scsi_dma_unmap(scmd_local);
2148 scmd_local->scsi_done(scmd_local);
adam radford9c915a82010-12-21 13:34:31 -08002149 atomic_dec(&instance->fw_outstanding);
2150
2151 break;
2152 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
2153 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302154
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302155 /* Poll mode. Dummy free.
2156 * In case of Interrupt mode, caller has reverse check.
2157 */
2158 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
2159 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
2160 megasas_return_cmd(instance, cmd_mfi);
2161 } else
2162 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
adam radford9c915a82010-12-21 13:34:31 -08002163 break;
2164 }
2165
adam radfordc8e858f2011-10-08 18:15:13 -07002166 fusion->last_reply_idx[MSIxIndex]++;
2167 if (fusion->last_reply_idx[MSIxIndex] >=
2168 fusion->reply_q_depth)
2169 fusion->last_reply_idx[MSIxIndex] = 0;
adam radford9c915a82010-12-21 13:34:31 -08002170
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05302171 desc->Words = cpu_to_le64(ULLONG_MAX);
adam radford9c915a82010-12-21 13:34:31 -08002172 num_completed++;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05302173 threshold_reply_count++;
adam radford9c915a82010-12-21 13:34:31 -08002174
2175 /* Get the next reply descriptor */
adam radfordc8e858f2011-10-08 18:15:13 -07002176 if (!fusion->last_reply_idx[MSIxIndex])
2177 desc = fusion->reply_frames_desc +
2178 ((MSIxIndex * fusion->reply_alloc_sz)/
2179 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
adam radford9c915a82010-12-21 13:34:31 -08002180 else
2181 desc++;
2182
2183 reply_desc =
2184 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
2185
2186 d_val.word = desc->Words;
2187
2188 reply_descript_type = reply_desc->ReplyFlags &
2189 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
2190
2191 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
2192 break;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05302193 /*
2194 * Write to reply post host index register after completing threshold
2195 * number of reply counts and still there are more replies in reply queue
2196 * pending to be completed
2197 */
2198 if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05302199 if (fusion->adapter_type == INVADER_SERIES)
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05302200 writel(((MSIxIndex & 0x7) << 24) |
2201 fusion->last_reply_idx[MSIxIndex],
2202 instance->reply_post_host_index_addr[MSIxIndex/8]);
2203 else
2204 writel((MSIxIndex << 24) |
2205 fusion->last_reply_idx[MSIxIndex],
2206 instance->reply_post_host_index_addr[0]);
2207 threshold_reply_count = 0;
2208 }
adam radford9c915a82010-12-21 13:34:31 -08002209 }
2210
2211 if (!num_completed)
2212 return IRQ_NONE;
2213
2214 wmb();
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05302215 if (fusion->adapter_type == INVADER_SERIES)
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302216 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]);
adam radford53ef2bb2011-02-24 20:56:05 -08002223 megasas_check_and_restore_queue_depth(instance);
adam radford9c915a82010-12-21 13:34:31 -08002224 return IRQ_HANDLED;
2225}
2226
2227/**
2228 * megasas_complete_cmd_dpc_fusion - Completes command
2229 * @instance: Adapter soft state
2230 *
2231 * Tasklet to complete cmds
2232 */
2233void
2234megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
2235{
2236 struct megasas_instance *instance =
2237 (struct megasas_instance *)instance_addr;
2238 unsigned long flags;
adam radfordc8e858f2011-10-08 18:15:13 -07002239 u32 count, MSIxIndex;
2240
2241 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
adam radford9c915a82010-12-21 13:34:31 -08002242
2243 /* If we have already declared adapter dead, donot complete cmds */
2244 spin_lock_irqsave(&instance->hba_lock, flags);
2245 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2246 spin_unlock_irqrestore(&instance->hba_lock, flags);
2247 return;
2248 }
2249 spin_unlock_irqrestore(&instance->hba_lock, flags);
2250
adam radfordc8e858f2011-10-08 18:15:13 -07002251 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
2252 complete_cmd_fusion(instance, MSIxIndex);
adam radford9c915a82010-12-21 13:34:31 -08002253}
2254
2255/**
2256 * megasas_isr_fusion - isr entry point
2257 */
2258irqreturn_t megasas_isr_fusion(int irq, void *devp)
2259{
adam radfordc8e858f2011-10-08 18:15:13 -07002260 struct megasas_irq_context *irq_context = devp;
2261 struct megasas_instance *instance = irq_context->instance;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302262 u32 mfiStatus, fw_state, dma_state;
adam radford9c915a82010-12-21 13:34:31 -08002263
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302264 if (instance->mask_interrupts)
2265 return IRQ_NONE;
2266
adam radfordc8e858f2011-10-08 18:15:13 -07002267 if (!instance->msix_vectors) {
adam radford9c915a82010-12-21 13:34:31 -08002268 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
2269 if (!mfiStatus)
2270 return IRQ_NONE;
2271 }
2272
2273 /* If we are resetting, bail */
adam radford6497b242011-10-08 18:14:50 -07002274 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
2275 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08002276 return IRQ_HANDLED;
adam radford6497b242011-10-08 18:14:50 -07002277 }
adam radford9c915a82010-12-21 13:34:31 -08002278
adam radfordc8e858f2011-10-08 18:15:13 -07002279 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
adam radford6497b242011-10-08 18:14:50 -07002280 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08002281 /* If we didn't complete any commands, check for FW fault */
2282 fw_state = instance->instancet->read_fw_status_reg(
2283 instance->reg_set) & MFI_STATE_MASK;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302284 dma_state = instance->instancet->read_fw_status_reg
2285 (instance->reg_set) & MFI_STATE_DMADONE;
2286 if (instance->crash_dump_drv_support &&
2287 instance->crash_dump_app_support) {
2288 /* Start collecting crash, if DMA bit is done */
2289 if ((fw_state == MFI_STATE_FAULT) && dma_state)
2290 schedule_work(&instance->crash_init);
2291 else if (fw_state == MFI_STATE_FAULT)
2292 schedule_work(&instance->work_init);
2293 } else if (fw_state == MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002294 dev_warn(&instance->pdev->dev, "Iop2SysDoorbellInt"
adam radford229fe472014-03-10 02:51:56 -07002295 "for scsi%d\n", instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002296 schedule_work(&instance->work_init);
adam radford229fe472014-03-10 02:51:56 -07002297 }
adam radford9c915a82010-12-21 13:34:31 -08002298 }
2299
2300 return IRQ_HANDLED;
2301}
2302
2303/**
2304 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
2305 * @instance: Adapter soft state
2306 * mfi_cmd: megasas_cmd pointer
2307 *
2308 */
2309u8
2310build_mpt_mfi_pass_thru(struct megasas_instance *instance,
2311 struct megasas_cmd *mfi_cmd)
2312{
2313 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
2314 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
2315 struct megasas_cmd_fusion *cmd;
2316 struct fusion_context *fusion;
2317 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
2318
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302319 fusion = instance->ctrl_context;
2320
2321 cmd = megasas_get_cmd_fusion(instance,
2322 instance->max_scsi_cmds + mfi_cmd->index);
adam radford9c915a82010-12-21 13:34:31 -08002323
2324 /* Save the smid. To be used for returning the cmd */
2325 mfi_cmd->context.smid = cmd->index;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302326
adam radford9c915a82010-12-21 13:34:31 -08002327 /*
2328 * For cmds where the flag is set, store the flag and check
2329 * on completion. For cmds with this flag, don't call
2330 * megasas_complete_cmd
2331 */
2332
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05302333 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302334 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
adam radford9c915a82010-12-21 13:34:31 -08002335
adam radford9c915a82010-12-21 13:34:31 -08002336 io_req = cmd->io_request;
adam radford36807e62011-10-08 18:15:06 -07002337
sumit.saxena@avagotech.com5a8cb852015-10-15 13:39:34 +05302338 if (fusion->adapter_type == INVADER_SERIES) {
adam radford36807e62011-10-08 18:15:06 -07002339 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
2340 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
2341 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2342 sgl_ptr_end->Flags = 0;
2343 }
2344
adam radford9c915a82010-12-21 13:34:31 -08002345 mpi25_ieee_chain =
2346 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
2347
2348 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
2349 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
2350 SGL) / 4;
2351 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
2352
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302353 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08002354
2355 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2356 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
2357
sumit.saxena@avagotech.combd5f9482015-08-31 17:23:31 +05302358 mpi25_ieee_chain->Length = cpu_to_le32(instance->max_chain_frame_sz);
adam radford9c915a82010-12-21 13:34:31 -08002359
2360 return 0;
2361}
2362
2363/**
2364 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
2365 * @instance: Adapter soft state
2366 * @cmd: mfi cmd to build
2367 *
2368 */
2369union MEGASAS_REQUEST_DESCRIPTOR_UNION *
2370build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
2371{
2372 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2373 u16 index;
2374
2375 if (build_mpt_mfi_pass_thru(instance, cmd)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002376 dev_err(&instance->pdev->dev, "Couldn't build MFI pass thru cmd\n");
adam radford9c915a82010-12-21 13:34:31 -08002377 return NULL;
2378 }
2379
2380 index = cmd->context.smid;
2381
2382 req_desc = megasas_get_request_descriptor(instance, index - 1);
2383
2384 if (!req_desc)
2385 return NULL;
2386
2387 req_desc->Words = 0;
2388 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2389 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2390
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302391 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08002392
2393 return req_desc;
2394}
2395
2396/**
2397 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
2398 * @instance: Adapter soft state
2399 * @cmd: mfi cmd pointer
2400 *
2401 */
2402void
2403megasas_issue_dcmd_fusion(struct megasas_instance *instance,
2404 struct megasas_cmd *cmd)
2405{
2406 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
adam radford9c915a82010-12-21 13:34:31 -08002407
2408 req_desc = build_mpt_cmd(instance, cmd);
2409 if (!req_desc) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002410 dev_err(&instance->pdev->dev, "Couldn't issue MFI pass thru cmd\n");
adam radford9c915a82010-12-21 13:34:31 -08002411 return;
2412 }
Christoph Hellwig01d7f032015-04-23 16:34:09 +05302413 megasas_fire_cmd_fusion(instance, req_desc);
adam radford9c915a82010-12-21 13:34:31 -08002414}
2415
2416/**
2417 * megasas_release_fusion - Reverses the FW initialization
Geert Uytterhoeven4b63b282015-03-03 11:58:07 +01002418 * @instance: Adapter soft state
adam radford9c915a82010-12-21 13:34:31 -08002419 */
2420void
2421megasas_release_fusion(struct megasas_instance *instance)
2422{
2423 megasas_free_cmds(instance);
2424 megasas_free_cmds_fusion(instance);
2425
2426 iounmap(instance->reg_set);
2427
2428 pci_release_selected_regions(instance->pdev, instance->bar);
2429}
2430
2431/**
2432 * megasas_read_fw_status_reg_fusion - returns the current FW status value
2433 * @regs: MFI register set
2434 */
2435static u32
2436megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
2437{
2438 return readl(&(regs)->outbound_scratch_pad);
2439}
2440
2441/**
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302442 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
2443 * @instance: Controller's soft instance
2444 * return: Number of allocated host crash buffers
2445 */
2446static void
2447megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
2448{
2449 unsigned int i;
2450
2451 instance->crash_buf_pages = get_order(CRASH_DMA_BUF_SIZE);
2452 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
2453 instance->crash_buf[i] = (void *)__get_free_pages(GFP_KERNEL,
2454 instance->crash_buf_pages);
2455 if (!instance->crash_buf[i]) {
2456 dev_info(&instance->pdev->dev, "Firmware crash dump "
2457 "memory allocation failed at index %d\n", i);
2458 break;
2459 }
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05302460 memset(instance->crash_buf[i], 0,
2461 ((1 << PAGE_SHIFT) << instance->crash_buf_pages));
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302462 }
2463 instance->drv_buf_alloc = i;
2464}
2465
2466/**
2467 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
2468 * @instance: Controller's soft instance
2469 */
2470void
2471megasas_free_host_crash_buffer(struct megasas_instance *instance)
2472{
2473 unsigned int i
2474;
2475 for (i = 0; i < instance->drv_buf_alloc; i++) {
2476 if (instance->crash_buf[i])
2477 free_pages((ulong)instance->crash_buf[i],
2478 instance->crash_buf_pages);
2479 }
2480 instance->drv_buf_index = 0;
2481 instance->drv_buf_alloc = 0;
2482 instance->fw_crash_state = UNAVAILABLE;
2483 instance->fw_crash_buffer_size = 0;
2484}
2485
2486/**
adam radford9c915a82010-12-21 13:34:31 -08002487 * megasas_adp_reset_fusion - For controller reset
2488 * @regs: MFI register set
2489 */
2490static int
2491megasas_adp_reset_fusion(struct megasas_instance *instance,
2492 struct megasas_register_set __iomem *regs)
2493{
sumit.saxena@avagotech.com79b82c22015-08-31 17:23:41 +05302494 u32 host_diag, abs_state, retry;
2495
2496 /* Now try to reset the chip */
2497 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
2498 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
2499 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
2500 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
2501 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
2502 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
2503 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset);
2504
2505 /* Check that the diag write enable (DRWE) bit is on */
2506 host_diag = readl(&instance->reg_set->fusion_host_diag);
2507 retry = 0;
2508 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2509 msleep(100);
2510 host_diag = readl(&instance->reg_set->fusion_host_diag);
2511 if (retry++ == 100) {
2512 dev_warn(&instance->pdev->dev,
2513 "Host diag unlock failed from %s %d\n",
2514 __func__, __LINE__);
2515 break;
2516 }
2517 }
2518 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2519 return -1;
2520
2521 /* Send chip reset command */
2522 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2523 &instance->reg_set->fusion_host_diag);
2524 msleep(3000);
2525
2526 /* Make sure reset adapter bit is cleared */
2527 host_diag = readl(&instance->reg_set->fusion_host_diag);
2528 retry = 0;
2529 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2530 msleep(100);
2531 host_diag = readl(&instance->reg_set->fusion_host_diag);
2532 if (retry++ == 1000) {
2533 dev_warn(&instance->pdev->dev,
2534 "Diag reset adapter never cleared %s %d\n",
2535 __func__, __LINE__);
2536 break;
2537 }
2538 }
2539 if (host_diag & HOST_DIAG_RESET_ADAPTER)
2540 return -1;
2541
2542 abs_state = instance->instancet->read_fw_status_reg(instance->reg_set)
2543 & MFI_STATE_MASK;
2544 retry = 0;
2545
2546 while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) {
2547 msleep(100);
2548 abs_state = instance->instancet->
2549 read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK;
2550 }
2551 if (abs_state <= MFI_STATE_FW_INIT) {
2552 dev_warn(&instance->pdev->dev,
2553 "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n",
2554 abs_state, __func__, __LINE__);
2555 return -1;
2556 }
2557
adam radford9c915a82010-12-21 13:34:31 -08002558 return 0;
2559}
2560
2561/**
2562 * megasas_check_reset_fusion - For controller reset check
2563 * @regs: MFI register set
2564 */
2565static int
2566megasas_check_reset_fusion(struct megasas_instance *instance,
2567 struct megasas_register_set __iomem *regs)
2568{
2569 return 0;
2570}
2571
2572/* This function waits for outstanding commands on fusion to complete */
adam radford229fe472014-03-10 02:51:56 -07002573int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
2574 int iotimeout, int *convert)
adam radford9c915a82010-12-21 13:34:31 -08002575{
adam radford229fe472014-03-10 02:51:56 -07002576 int i, outstanding, retval = 0, hb_seconds_missed = 0;
adam radfordc007b8b2012-07-17 18:20:24 -07002577 u32 fw_state;
adam radford9c915a82010-12-21 13:34:31 -08002578
adam radfordc007b8b2012-07-17 18:20:24 -07002579 for (i = 0; i < resetwaittime; i++) {
adam radford9c915a82010-12-21 13:34:31 -08002580 /* Check if firmware is in fault state */
2581 fw_state = instance->instancet->read_fw_status_reg(
2582 instance->reg_set) & MFI_STATE_MASK;
2583 if (fw_state == MFI_STATE_FAULT) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002584 dev_warn(&instance->pdev->dev, "Found FW in FAULT state,"
adam radford229fe472014-03-10 02:51:56 -07002585 " will reset adapter scsi%d.\n",
2586 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002587 retval = 1;
2588 goto out;
2589 }
adam radford229fe472014-03-10 02:51:56 -07002590 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
2591 if (instance->requestorId && !iotimeout) {
2592 retval = 1;
2593 goto out;
2594 }
2595
2596 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
2597 if (instance->requestorId && iotimeout) {
2598 if (instance->hb_host_mem->HB.fwCounter !=
2599 instance->hb_host_mem->HB.driverCounter) {
2600 instance->hb_host_mem->HB.driverCounter =
2601 instance->hb_host_mem->HB.fwCounter;
2602 hb_seconds_missed = 0;
2603 } else {
2604 hb_seconds_missed++;
2605 if (hb_seconds_missed ==
2606 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002607 dev_warn(&instance->pdev->dev, "SR-IOV:"
adam radford229fe472014-03-10 02:51:56 -07002608 " Heartbeat never completed "
2609 " while polling during I/O "
2610 " timeout handling for "
2611 "scsi%d.\n",
2612 instance->host->host_no);
2613 *convert = 1;
2614 retval = 1;
2615 goto out;
2616 }
2617 }
2618 }
adam radford9c915a82010-12-21 13:34:31 -08002619
2620 outstanding = atomic_read(&instance->fw_outstanding);
2621 if (!outstanding)
2622 goto out;
2623
2624 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002625 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d "
adam radford229fe472014-03-10 02:51:56 -07002626 "commands to complete for scsi%d\n", i,
2627 outstanding, instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002628 megasas_complete_cmd_dpc_fusion(
2629 (unsigned long)instance);
2630 }
2631 msleep(1000);
2632 }
2633
2634 if (atomic_read(&instance->fw_outstanding)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002635 dev_err(&instance->pdev->dev, "pending commands remain after waiting, "
adam radford229fe472014-03-10 02:51:56 -07002636 "will reset adapter scsi%d.\n",
2637 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002638 retval = 1;
2639 }
2640out:
2641 return retval;
2642}
2643
2644void megasas_reset_reply_desc(struct megasas_instance *instance)
2645{
adam radfordc8e858f2011-10-08 18:15:13 -07002646 int i, count;
adam radford9c915a82010-12-21 13:34:31 -08002647 struct fusion_context *fusion;
2648 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2649
2650 fusion = instance->ctrl_context;
adam radfordc8e858f2011-10-08 18:15:13 -07002651 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2652 for (i = 0 ; i < count ; i++)
2653 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -08002654 reply_desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -07002655 for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
Christoph Hellwigc6f5bf82015-04-23 16:33:09 +05302656 reply_desc->Words = cpu_to_le64(ULLONG_MAX);
adam radford9c915a82010-12-21 13:34:31 -08002657}
2658
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05302659/*
2660 * megasas_refire_mgmt_cmd : Re-fire management commands
2661 * @instance: Controller's soft instance
2662*/
2663void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
2664{
2665 int j;
2666 struct megasas_cmd_fusion *cmd_fusion;
2667 struct fusion_context *fusion;
2668 struct megasas_cmd *cmd_mfi;
2669 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2670 u16 smid;
2671
2672 fusion = instance->ctrl_context;
2673
2674 /* Re-fire management commands.
2675 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
2676 */
2677 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
2678 cmd_fusion = fusion->cmd_list[j];
2679 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2680 smid = le16_to_cpu(cmd_mfi->context.smid);
2681
2682 if (!smid)
2683 continue;
2684 req_desc = megasas_get_request_descriptor
2685 (instance, smid - 1);
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302686 if (req_desc && ((cmd_mfi->frame->dcmd.opcode !=
2687 cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO)) &&
2688 (cmd_mfi->frame->dcmd.opcode !=
2689 cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO))))
Christoph Hellwig01d7f032015-04-23 16:34:09 +05302690 megasas_fire_cmd_fusion(instance, req_desc);
2691 else
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05302692 megasas_return_cmd(instance, cmd_mfi);
2693 }
adam radford9c915a82010-12-21 13:34:31 -08002694}
2695
adam radford229fe472014-03-10 02:51:56 -07002696/* Check for a second path that is currently UP */
2697int megasas_check_mpio_paths(struct megasas_instance *instance,
2698 struct scsi_cmnd *scmd)
adam radford9c915a82010-12-21 13:34:31 -08002699{
adam radford229fe472014-03-10 02:51:56 -07002700 int i, j, retval = (DID_RESET << 16);
2701
2702 if (instance->mpio && instance->requestorId) {
2703 for (i = 0 ; i < MAX_MGMT_ADAPTERS ; i++)
2704 for (j = 0 ; j < MAX_LOGICAL_DRIVES; j++)
2705 if (megasas_mgmt_info.instance[i] &&
2706 (megasas_mgmt_info.instance[i] != instance) &&
2707 megasas_mgmt_info.instance[i]->mpio &&
2708 megasas_mgmt_info.instance[i]->requestorId
2709 &&
2710 (megasas_mgmt_info.instance[i]->ld_ids[j]
2711 == scmd->device->id)) {
2712 retval = (DID_NO_CONNECT << 16);
2713 goto out;
2714 }
2715 }
2716out:
2717 return retval;
2718}
2719
2720/* Core fusion reset function */
2721int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout)
2722{
sumit.saxena@avagotech.com79b82c22015-08-31 17:23:41 +05302723 int retval = SUCCESS, i, convert = 0;
adam radford9c915a82010-12-21 13:34:31 -08002724 struct megasas_instance *instance;
2725 struct megasas_cmd_fusion *cmd_fusion;
2726 struct fusion_context *fusion;
sumit.saxena@avagotech.com79b82c22015-08-31 17:23:41 +05302727 u32 abs_state, status_reg, reset_adapter;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302728 u32 io_timeout_in_crash_mode = 0;
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302729 struct scsi_cmnd *scmd_local = NULL;
adam radford9c915a82010-12-21 13:34:31 -08002730
2731 instance = (struct megasas_instance *)shost->hostdata;
2732 fusion = instance->ctrl_context;
2733
adam radford229fe472014-03-10 02:51:56 -07002734 mutex_lock(&instance->reset_mutex);
2735
adam radford9c915a82010-12-21 13:34:31 -08002736 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002737 dev_warn(&instance->pdev->dev, "Hardware critical error, "
adam radford229fe472014-03-10 02:51:56 -07002738 "returning FAILED for scsi%d.\n",
2739 instance->host->host_no);
Adam Radforda2fbcbc2014-07-09 15:17:54 -07002740 mutex_unlock(&instance->reset_mutex);
adam radfordd4a759a2011-10-08 18:14:39 -07002741 return FAILED;
adam radford9c915a82010-12-21 13:34:31 -08002742 }
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302743 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
2744 abs_state = status_reg & MFI_STATE_MASK;
2745
2746 /* IO timeout detected, forcibly put FW in FAULT state */
2747 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
2748 instance->crash_dump_app_support && iotimeout) {
2749 dev_info(&instance->pdev->dev, "IO timeout is detected, "
2750 "forcibly FAULT Firmware\n");
2751 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2752 status_reg = readl(&instance->reg_set->doorbell);
2753 writel(status_reg | MFI_STATE_FORCE_OCR,
2754 &instance->reg_set->doorbell);
2755 readl(&instance->reg_set->doorbell);
2756 mutex_unlock(&instance->reset_mutex);
2757 do {
2758 ssleep(3);
2759 io_timeout_in_crash_mode++;
2760 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
2761 "seconds for crash dump collection and OCR "
2762 "to be done\n", (io_timeout_in_crash_mode * 3));
2763 } while ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
2764 (io_timeout_in_crash_mode < 80));
2765
2766 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
2767 dev_info(&instance->pdev->dev, "OCR done for IO "
2768 "timeout case\n");
2769 retval = SUCCESS;
2770 } else {
2771 dev_info(&instance->pdev->dev, "Controller is not "
2772 "operational after 240 seconds wait for IO "
2773 "timeout case in FW crash dump mode\n do "
2774 "OCR/kill adapter\n");
2775 retval = megasas_reset_fusion(shost, 0);
2776 }
2777 return retval;
2778 }
adam radford9c915a82010-12-21 13:34:31 -08002779
adam radford229fe472014-03-10 02:51:56 -07002780 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
2781 del_timer_sync(&instance->sriov_heartbeat_timer);
adam radford7e70e732011-05-11 18:34:08 -07002782 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
adam radford229fe472014-03-10 02:51:56 -07002783 instance->adprecovery = MEGASAS_ADPRESET_SM_POLLING;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302784 instance->instancet->disable_intr(instance);
adam radford7e70e732011-05-11 18:34:08 -07002785 msleep(1000);
2786
adam radford9c915a82010-12-21 13:34:31 -08002787 /* First try waiting for commands to complete */
adam radford229fe472014-03-10 02:51:56 -07002788 if (megasas_wait_for_outstanding_fusion(instance, iotimeout,
2789 &convert)) {
2790 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002791 dev_warn(&instance->pdev->dev, "resetting fusion "
adam radford229fe472014-03-10 02:51:56 -07002792 "adapter scsi%d.\n", instance->host->host_no);
2793 if (convert)
2794 iotimeout = 0;
2795
adam radford9c915a82010-12-21 13:34:31 -08002796 /* Now return commands back to the OS */
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302797 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
adam radford9c915a82010-12-21 13:34:31 -08002798 cmd_fusion = fusion->cmd_list[i];
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302799 scmd_local = cmd_fusion->scmd;
adam radford9c915a82010-12-21 13:34:31 -08002800 if (cmd_fusion->scmd) {
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302801 scmd_local->result =
adam radford229fe472014-03-10 02:51:56 -07002802 megasas_check_mpio_paths(instance,
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302803 scmd_local);
adam radford9c915a82010-12-21 13:34:31 -08002804 megasas_return_cmd_fusion(instance, cmd_fusion);
Sumit.Saxena@avagotech.com4026e9a2015-04-23 16:31:24 +05302805 scsi_dma_unmap(scmd_local);
2806 scmd_local->scsi_done(scmd_local);
adam radford9c915a82010-12-21 13:34:31 -08002807 atomic_dec(&instance->fw_outstanding);
2808 }
2809 }
2810
adam radford7e70e732011-05-11 18:34:08 -07002811 status_reg = instance->instancet->read_fw_status_reg(
2812 instance->reg_set);
2813 abs_state = status_reg & MFI_STATE_MASK;
2814 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2815 if (instance->disableOnlineCtrlReset ||
2816 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
adam radford9c915a82010-12-21 13:34:31 -08002817 /* Reset not supported, kill adapter */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002818 dev_warn(&instance->pdev->dev, "Reset not supported"
adam radford229fe472014-03-10 02:51:56 -07002819 ", killing adapter scsi%d.\n",
2820 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002821 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07002822 instance->skip_heartbeat_timer_del = 1;
adam radford9c915a82010-12-21 13:34:31 -08002823 retval = FAILED;
2824 goto out;
2825 }
2826
adam radford229fe472014-03-10 02:51:56 -07002827 /* Let SR-IOV VF & PF sync up if there was a HB failure */
2828 if (instance->requestorId && !iotimeout) {
2829 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
2830 /* Look for a late HB update after VF settle time */
2831 if (abs_state == MFI_STATE_OPERATIONAL &&
2832 (instance->hb_host_mem->HB.fwCounter !=
2833 instance->hb_host_mem->HB.driverCounter)) {
2834 instance->hb_host_mem->HB.driverCounter =
2835 instance->hb_host_mem->HB.fwCounter;
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002836 dev_warn(&instance->pdev->dev, "SR-IOV:"
adam radford229fe472014-03-10 02:51:56 -07002837 "Late FW heartbeat update for "
2838 "scsi%d.\n",
2839 instance->host->host_no);
2840 } else {
2841 /* In VF mode, first poll for FW ready */
2842 for (i = 0;
2843 i < (MEGASAS_RESET_WAIT_TIME * 1000);
2844 i += 20) {
2845 status_reg =
2846 instance->instancet->
2847 read_fw_status_reg(
2848 instance->reg_set);
2849 abs_state = status_reg &
2850 MFI_STATE_MASK;
2851 if (abs_state == MFI_STATE_READY) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002852 dev_warn(&instance->pdev->dev,
2853 "SR-IOV: FW was found"
adam radford229fe472014-03-10 02:51:56 -07002854 "to be in ready state "
2855 "for scsi%d.\n",
2856 instance->host->host_no);
2857 break;
2858 }
2859 msleep(20);
2860 }
2861 if (abs_state != MFI_STATE_READY) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002862 dev_warn(&instance->pdev->dev, "SR-IOV: "
adam radford229fe472014-03-10 02:51:56 -07002863 "FW not in ready state after %d"
2864 " seconds for scsi%d, status_reg = "
2865 "0x%x.\n",
2866 MEGASAS_RESET_WAIT_TIME,
2867 instance->host->host_no,
2868 status_reg);
2869 megaraid_sas_kill_hba(instance);
2870 instance->skip_heartbeat_timer_del = 1;
2871 instance->adprecovery =
2872 MEGASAS_HW_CRITICAL_ERROR;
2873 retval = FAILED;
2874 goto out;
2875 }
2876 }
2877 }
2878
adam radford9c915a82010-12-21 13:34:31 -08002879 /* Now try to reset the chip */
2880 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
adam radford9c915a82010-12-21 13:34:31 -08002881
sumit.saxena@avagotech.com79b82c22015-08-31 17:23:41 +05302882 if (instance->instancet->adp_reset
2883 (instance, instance->reg_set))
adam radford9c915a82010-12-21 13:34:31 -08002884 continue;
2885
adam radford9c915a82010-12-21 13:34:31 -08002886 /* Wait for FW to become ready */
adam radford058a8fa2011-10-08 18:14:27 -07002887 if (megasas_transition_to_ready(instance, 1)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002888 dev_warn(&instance->pdev->dev, "Failed to "
adam radford229fe472014-03-10 02:51:56 -07002889 "transition controller to ready "
2890 "for scsi%d.\n",
2891 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002892 continue;
2893 }
2894
2895 megasas_reset_reply_desc(instance);
2896 if (megasas_ioc_init_fusion(instance)) {
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002897 dev_warn(&instance->pdev->dev,
adam radford229fe472014-03-10 02:51:56 -07002898 "megasas_ioc_init_fusion() failed!"
2899 " for scsi%d\n",
2900 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002901 continue;
2902 }
2903
Sumit.Saxena@avagotech.com9b828182015-04-23 16:30:24 +05302904 megasas_refire_mgmt_cmd(instance);
adam radford9c915a82010-12-21 13:34:31 -08002905
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05302906 if (megasas_get_ctrl_info(instance)) {
2907 dev_info(&instance->pdev->dev,
2908 "Failed from %s %d\n",
2909 __func__, __LINE__);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05302910 megaraid_sas_kill_hba(instance);
2911 retval = FAILED;
2912 }
adam radford9c915a82010-12-21 13:34:31 -08002913 /* Reset load balance info */
2914 memset(fusion->load_balance_info, 0,
2915 sizeof(struct LD_LOAD_BALANCE_INFO)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302916 *MAX_LOGICAL_DRIVES_EXT);
adam radford9c915a82010-12-21 13:34:31 -08002917
2918 if (!megasas_get_map_info(instance))
2919 megasas_sync_map_info(instance);
2920
sumit.saxena@avagotech.com3761cb42015-08-31 17:23:11 +05302921 megasas_setup_jbod_map(instance);
2922
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05302923 clear_bit(MEGASAS_FUSION_IN_RESET,
2924 &instance->reset_flags);
2925 instance->instancet->enable_intr(instance);
2926 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2927
adam radford229fe472014-03-10 02:51:56 -07002928 /* Restart SR-IOV heartbeat */
2929 if (instance->requestorId) {
2930 if (!megasas_sriov_start_heartbeat(instance, 0))
2931 megasas_start_timer(instance,
2932 &instance->sriov_heartbeat_timer,
2933 megasas_sriov_heartbeat_handler,
2934 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2935 else
2936 instance->skip_heartbeat_timer_del = 1;
2937 }
2938
adam radford9c915a82010-12-21 13:34:31 -08002939 /* Adapter reset completed successfully */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002940 dev_warn(&instance->pdev->dev, "Reset "
adam radford229fe472014-03-10 02:51:56 -07002941 "successful for scsi%d.\n",
2942 instance->host->host_no);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302943
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05302944 if (instance->crash_dump_drv_support &&
2945 instance->crash_dump_app_support)
2946 megasas_set_crash_dump_params(instance,
2947 MR_CRASH_BUF_TURN_ON);
2948 else
2949 megasas_set_crash_dump_params(instance,
2950 MR_CRASH_BUF_TURN_OFF);
2951
adam radford9c915a82010-12-21 13:34:31 -08002952 retval = SUCCESS;
2953 goto out;
2954 }
2955 /* Reset failed, kill the adapter */
Bjorn Helgaas1be18252015-07-07 15:52:34 -05002956 dev_warn(&instance->pdev->dev, "Reset failed, killing "
adam radford229fe472014-03-10 02:51:56 -07002957 "adapter scsi%d.\n", instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002958 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07002959 instance->skip_heartbeat_timer_del = 1;
adam radford9c915a82010-12-21 13:34:31 -08002960 retval = FAILED;
2961 } else {
adam radford229fe472014-03-10 02:51:56 -07002962 /* For VF: Restart HB timer if we didn't OCR */
2963 if (instance->requestorId) {
2964 megasas_start_timer(instance,
2965 &instance->sriov_heartbeat_timer,
2966 megasas_sriov_heartbeat_handler,
2967 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2968 }
adam radford3f0e58bc2011-10-08 18:14:59 -07002969 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302970 instance->instancet->enable_intr(instance);
adam radford9c915a82010-12-21 13:34:31 -08002971 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2972 }
2973out:
2974 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2975 mutex_unlock(&instance->reset_mutex);
2976 return retval;
2977}
2978
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302979/* Fusion Crash dump collection work queue */
2980void megasas_fusion_crash_dump_wq(struct work_struct *work)
2981{
2982 struct megasas_instance *instance =
2983 container_of(work, struct megasas_instance, crash_init);
2984 u32 status_reg;
2985 u8 partial_copy = 0;
2986
2987
2988 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
2989
2990 /*
2991 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
2992 * to host crash buffers
2993 */
2994 if (instance->drv_buf_index == 0) {
2995 /* Buffer is already allocated for old Crash dump.
2996 * Do OCR and do not wait for crash dump collection
2997 */
2998 if (instance->drv_buf_alloc) {
2999 dev_info(&instance->pdev->dev, "earlier crash dump is "
3000 "not yet copied by application, ignoring this "
3001 "crash dump and initiating OCR\n");
3002 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
3003 writel(status_reg,
3004 &instance->reg_set->outbound_scratch_pad);
3005 readl(&instance->reg_set->outbound_scratch_pad);
3006 return;
3007 }
3008 megasas_alloc_host_crash_buffer(instance);
3009 dev_info(&instance->pdev->dev, "Number of host crash buffers "
3010 "allocated: %d\n", instance->drv_buf_alloc);
3011 }
3012
3013 /*
3014 * Driver has allocated max buffers, which can be allocated
3015 * and FW has more crash dump data, then driver will
3016 * ignore the data.
3017 */
3018 if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
3019 dev_info(&instance->pdev->dev, "Driver is done copying "
3020 "the buffer: %d\n", instance->drv_buf_alloc);
3021 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
3022 partial_copy = 1;
3023 } else {
3024 memcpy(instance->crash_buf[instance->drv_buf_index],
3025 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
3026 instance->drv_buf_index++;
3027 status_reg &= ~MFI_STATE_DMADONE;
3028 }
3029
3030 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
3031 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
3032 "of copied buffers: %d\n", instance->drv_buf_index);
3033 instance->fw_crash_buffer_size = instance->drv_buf_index;
3034 instance->fw_crash_state = AVAILABLE;
3035 instance->drv_buf_index = 0;
3036 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
3037 readl(&instance->reg_set->outbound_scratch_pad);
3038 if (!partial_copy)
3039 megasas_reset_fusion(instance->host, 0);
3040 } else {
3041 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
3042 readl(&instance->reg_set->outbound_scratch_pad);
3043 }
3044}
3045
3046
adam radford9c915a82010-12-21 13:34:31 -08003047/* Fusion OCR work queue */
3048void megasas_fusion_ocr_wq(struct work_struct *work)
3049{
3050 struct megasas_instance *instance =
3051 container_of(work, struct megasas_instance, work_init);
3052
adam radford229fe472014-03-10 02:51:56 -07003053 megasas_reset_fusion(instance->host, 0);
adam radford9c915a82010-12-21 13:34:31 -08003054}
3055
3056struct megasas_instance_template megasas_instance_template_fusion = {
adam radford9c915a82010-12-21 13:34:31 -08003057 .enable_intr = megasas_enable_intr_fusion,
3058 .disable_intr = megasas_disable_intr_fusion,
3059 .clear_intr = megasas_clear_intr_fusion,
3060 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
3061 .adp_reset = megasas_adp_reset_fusion,
3062 .check_reset = megasas_check_reset_fusion,
3063 .service_isr = megasas_isr_fusion,
3064 .tasklet = megasas_complete_cmd_dpc_fusion,
3065 .init_adapter = megasas_init_adapter_fusion,
3066 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
3067 .issue_dcmd = megasas_issue_dcmd_fusion,
3068};