blob: af5ab9e5e8e5c239e39168218aae3c85c423f9f9 [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>
adam radford9c915a82010-12-21 13:34:31 -080056
57#include "megaraid_sas_fusion.h"
58#include "megaraid_sas.h"
59
60extern void megasas_free_cmds(struct megasas_instance *instance);
61extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
62 *instance);
63extern void
64megasas_complete_cmd(struct megasas_instance *instance,
65 struct megasas_cmd *cmd, u8 alt_status);
adam radford9c915a82010-12-21 13:34:31 -080066int
adam radford229fe472014-03-10 02:51:56 -070067wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
68 int seconds);
adam radford9c915a82010-12-21 13:34:31 -080069
70void
71megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
72int megasas_alloc_cmds(struct megasas_instance *instance);
73int
74megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
75int
76megasas_issue_polled(struct megasas_instance *instance,
77 struct megasas_cmd *cmd);
adam radford53ef2bb2011-02-24 20:56:05 -080078void
79megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
80
adam radford058a8fa2011-10-08 18:14:27 -070081int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
adam radford9c915a82010-12-21 13:34:31 -080082void megaraid_sas_kill_hba(struct megasas_instance *instance);
83
84extern u32 megasas_dbg_lvl;
adam radford229fe472014-03-10 02:51:56 -070085void megasas_sriov_heartbeat_handler(unsigned long instance_addr);
86int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
87 int initial);
88void megasas_start_timer(struct megasas_instance *instance,
89 struct timer_list *timer,
90 void *fn, unsigned long interval);
91extern struct megasas_mgmt_info megasas_mgmt_info;
adam radfordc007b8b2012-07-17 18:20:24 -070092extern int resetwaittime;
adam radford9c915a82010-12-21 13:34:31 -080093
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +053094
95
adam radford9c915a82010-12-21 13:34:31 -080096/**
97 * megasas_enable_intr_fusion - Enables interrupts
98 * @regs: MFI register set
99 */
100void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530101megasas_enable_intr_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800102{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530103 struct megasas_register_set __iomem *regs;
104 regs = instance->reg_set;
adam radford6497b242011-10-08 18:14:50 -0700105 /* For Thunderbolt/Invader also clear intr on enable */
106 writel(~0, &regs->outbound_intr_status);
107 readl(&regs->outbound_intr_status);
108
adam radford9c915a82010-12-21 13:34:31 -0800109 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
110
111 /* Dummy readl to force pci flush */
112 readl(&regs->outbound_intr_mask);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530113 instance->mask_interrupts = 0;
adam radford9c915a82010-12-21 13:34:31 -0800114}
115
116/**
117 * megasas_disable_intr_fusion - Disables interrupt
118 * @regs: MFI register set
119 */
120void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530121megasas_disable_intr_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800122{
123 u32 mask = 0xFFFFFFFF;
124 u32 status;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530125 struct megasas_register_set __iomem *regs;
126 regs = instance->reg_set;
127 instance->mask_interrupts = 1;
adam radford9c915a82010-12-21 13:34:31 -0800128
129 writel(mask, &regs->outbound_intr_mask);
130 /* Dummy readl to force pci flush */
131 status = readl(&regs->outbound_intr_mask);
132}
133
134int
135megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
136{
137 u32 status;
138 /*
139 * Check if it is our interrupt
140 */
141 status = readl(&regs->outbound_intr_status);
142
143 if (status & 1) {
144 writel(status, &regs->outbound_intr_status);
145 readl(&regs->outbound_intr_status);
146 return 1;
147 }
148 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
149 return 0;
150
adam radford9c915a82010-12-21 13:34:31 -0800151 return 1;
152}
153
154/**
155 * megasas_get_cmd_fusion - Get a command from the free pool
156 * @instance: Adapter soft state
157 *
158 * Returns a free command from the pool
159 */
160struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
161 *instance)
162{
163 unsigned long flags;
164 struct fusion_context *fusion =
165 (struct fusion_context *)instance->ctrl_context;
166 struct megasas_cmd_fusion *cmd = NULL;
167
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530168 spin_lock_irqsave(&fusion->mpt_pool_lock, flags);
adam radford9c915a82010-12-21 13:34:31 -0800169
170 if (!list_empty(&fusion->cmd_pool)) {
171 cmd = list_entry((&fusion->cmd_pool)->next,
172 struct megasas_cmd_fusion, list);
173 list_del_init(&cmd->list);
174 } else {
175 printk(KERN_ERR "megasas: Command pool (fusion) empty!\n");
176 }
177
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530178 spin_unlock_irqrestore(&fusion->mpt_pool_lock, flags);
adam radford9c915a82010-12-21 13:34:31 -0800179 return cmd;
180}
181
182/**
183 * megasas_return_cmd_fusion - Return a cmd to free command pool
184 * @instance: Adapter soft state
185 * @cmd: Command packet to be returned to free command pool
186 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530187inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
188 struct megasas_cmd_fusion *cmd)
adam radford9c915a82010-12-21 13:34:31 -0800189{
190 unsigned long flags;
191 struct fusion_context *fusion =
192 (struct fusion_context *)instance->ctrl_context;
193
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530194 spin_lock_irqsave(&fusion->mpt_pool_lock, flags);
adam radford9c915a82010-12-21 13:34:31 -0800195
196 cmd->scmd = NULL;
197 cmd->sync_cmd_idx = (u32)ULONG_MAX;
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +0530198 memset(cmd->io_request, 0, sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530199 list_add(&cmd->list, (&fusion->cmd_pool)->next);
adam radford9c915a82010-12-21 13:34:31 -0800200
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530201 spin_unlock_irqrestore(&fusion->mpt_pool_lock, flags);
202}
203
204/**
205 * megasas_return_mfi_mpt_pthr - Return a mfi and mpt to free command pool
206 * @instance: Adapter soft state
207 * @cmd_mfi: MFI Command packet to be returned to free command pool
208 * @cmd_mpt: MPT Command packet to be returned to free command pool
209 */
210inline void megasas_return_mfi_mpt_pthr(struct megasas_instance *instance,
211 struct megasas_cmd *cmd_mfi,
212 struct megasas_cmd_fusion *cmd_fusion)
213{
214 unsigned long flags;
215
216 /*
217 * TO DO: optimize this code and use only one lock instead of two
218 * locks being used currently- mpt_pool_lock is acquired
219 * inside mfi_pool_lock
220 */
221 spin_lock_irqsave(&instance->mfi_pool_lock, flags);
222 megasas_return_cmd_fusion(instance, cmd_fusion);
223 if (atomic_read(&cmd_mfi->mfi_mpt_pthr) != MFI_MPT_ATTACHED)
224 dev_err(&instance->pdev->dev, "Possible bug from %s %d\n",
225 __func__, __LINE__);
226 atomic_set(&cmd_mfi->mfi_mpt_pthr, MFI_MPT_DETACHED);
227 __megasas_return_cmd(instance, cmd_mfi);
228 spin_unlock_irqrestore(&instance->mfi_pool_lock, flags);
adam radford9c915a82010-12-21 13:34:31 -0800229}
230
231/**
232 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
233 * @instance: Adapter soft state
234 */
235static void megasas_teardown_frame_pool_fusion(
236 struct megasas_instance *instance)
237{
238 int i;
239 struct fusion_context *fusion = instance->ctrl_context;
240
241 u16 max_cmd = instance->max_fw_cmds;
242
243 struct megasas_cmd_fusion *cmd;
244
245 if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
246 printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
247 "sense pool : %p\n", fusion->sg_dma_pool,
248 fusion->sense_dma_pool);
249 return;
250 }
251
252 /*
253 * Return all frames to pool
254 */
255 for (i = 0; i < max_cmd; i++) {
256
257 cmd = fusion->cmd_list[i];
258
259 if (cmd->sg_frame)
260 pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
261 cmd->sg_frame_phys_addr);
262
263 if (cmd->sense)
264 pci_pool_free(fusion->sense_dma_pool, cmd->sense,
265 cmd->sense_phys_addr);
266 }
267
268 /*
269 * Now destroy the pool itself
270 */
271 pci_pool_destroy(fusion->sg_dma_pool);
272 pci_pool_destroy(fusion->sense_dma_pool);
273
274 fusion->sg_dma_pool = NULL;
275 fusion->sense_dma_pool = NULL;
276}
277
278/**
279 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
280 * @instance: Adapter soft state
281 */
282void
283megasas_free_cmds_fusion(struct megasas_instance *instance)
284{
285 int i;
286 struct fusion_context *fusion = instance->ctrl_context;
287
288 u32 max_cmds, req_sz, reply_sz, io_frames_sz;
289
290
291 req_sz = fusion->request_alloc_sz;
292 reply_sz = fusion->reply_alloc_sz;
293 io_frames_sz = fusion->io_frames_alloc_sz;
294
295 max_cmds = instance->max_fw_cmds;
296
297 /* Free descriptors and request Frames memory */
298 if (fusion->req_frames_desc)
299 dma_free_coherent(&instance->pdev->dev, req_sz,
300 fusion->req_frames_desc,
301 fusion->req_frames_desc_phys);
302
303 if (fusion->reply_frames_desc) {
304 pci_pool_free(fusion->reply_frames_desc_pool,
305 fusion->reply_frames_desc,
306 fusion->reply_frames_desc_phys);
307 pci_pool_destroy(fusion->reply_frames_desc_pool);
308 }
309
310 if (fusion->io_request_frames) {
311 pci_pool_free(fusion->io_request_frames_pool,
312 fusion->io_request_frames,
313 fusion->io_request_frames_phys);
314 pci_pool_destroy(fusion->io_request_frames_pool);
315 }
316
317 /* Free the Fusion frame pool */
318 megasas_teardown_frame_pool_fusion(instance);
319
320 /* Free all the commands in the cmd_list */
321 for (i = 0; i < max_cmds; i++)
322 kfree(fusion->cmd_list[i]);
323
324 /* Free the cmd_list buffer itself */
325 kfree(fusion->cmd_list);
326 fusion->cmd_list = NULL;
327
328 INIT_LIST_HEAD(&fusion->cmd_pool);
329}
330
331/**
332 * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
333 * @instance: Adapter soft state
334 *
335 */
336static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
337{
338 int i;
339 u32 max_cmd;
340 struct fusion_context *fusion;
341 struct megasas_cmd_fusion *cmd;
342 u32 total_sz_chain_frame;
343
344 fusion = instance->ctrl_context;
345 max_cmd = instance->max_fw_cmds;
346
347 total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
348
349 /*
350 * Use DMA pool facility provided by PCI layer
351 */
352
353 fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
354 instance->pdev,
355 total_sz_chain_frame, 4,
356 0);
357 if (!fusion->sg_dma_pool) {
358 printk(KERN_DEBUG "megasas: failed to setup request pool "
359 "fusion\n");
360 return -ENOMEM;
361 }
362 fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
363 instance->pdev,
364 SCSI_SENSE_BUFFERSIZE, 64, 0);
365
366 if (!fusion->sense_dma_pool) {
367 printk(KERN_DEBUG "megasas: failed to setup sense pool "
368 "fusion\n");
369 pci_pool_destroy(fusion->sg_dma_pool);
370 fusion->sg_dma_pool = NULL;
371 return -ENOMEM;
372 }
373
374 /*
375 * Allocate and attach a frame to each of the commands in cmd_list
376 */
377 for (i = 0; i < max_cmd; i++) {
378
379 cmd = fusion->cmd_list[i];
380
381 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
382 GFP_KERNEL,
383 &cmd->sg_frame_phys_addr);
384
385 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
386 GFP_KERNEL, &cmd->sense_phys_addr);
387 /*
388 * megasas_teardown_frame_pool_fusion() takes care of freeing
389 * whatever has been allocated
390 */
391 if (!cmd->sg_frame || !cmd->sense) {
392 printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
393 megasas_teardown_frame_pool_fusion(instance);
394 return -ENOMEM;
395 }
396 }
397 return 0;
398}
399
400/**
401 * megasas_alloc_cmds_fusion - Allocates the command packets
402 * @instance: Adapter soft state
403 *
404 *
405 * Each frame has a 32-bit field called context. This context is used to get
406 * back the megasas_cmd_fusion from the frame when a frame gets completed
407 * In this driver, the 32 bit values are the indices into an array cmd_list.
408 * This array is used only to look up the megasas_cmd_fusion given the context.
409 * The free commands themselves are maintained in a linked list called cmd_pool.
410 *
411 * cmds are formed in the io_request and sg_frame members of the
412 * megasas_cmd_fusion. The context field is used to get a request descriptor
413 * and is used as SMID of the cmd.
414 * SMID value range is from 1 to max_fw_cmds.
415 */
416int
417megasas_alloc_cmds_fusion(struct megasas_instance *instance)
418{
adam radfordc8e858f2011-10-08 18:15:13 -0700419 int i, j, count;
adam radford9c915a82010-12-21 13:34:31 -0800420 u32 max_cmd, io_frames_sz;
421 struct fusion_context *fusion;
422 struct megasas_cmd_fusion *cmd;
423 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
424 u32 offset;
425 dma_addr_t io_req_base_phys;
426 u8 *io_req_base;
427
428 fusion = instance->ctrl_context;
429
430 max_cmd = instance->max_fw_cmds;
431
432 fusion->req_frames_desc =
433 dma_alloc_coherent(&instance->pdev->dev,
434 fusion->request_alloc_sz,
435 &fusion->req_frames_desc_phys, GFP_KERNEL);
436
437 if (!fusion->req_frames_desc) {
438 printk(KERN_ERR "megasas; Could not allocate memory for "
439 "request_frames\n");
440 goto fail_req_desc;
441 }
442
adam radfordc8e858f2011-10-08 18:15:13 -0700443 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
adam radford9c915a82010-12-21 13:34:31 -0800444 fusion->reply_frames_desc_pool =
445 pci_pool_create("reply_frames pool", instance->pdev,
adam radfordc8e858f2011-10-08 18:15:13 -0700446 fusion->reply_alloc_sz * count, 16, 0);
adam radford9c915a82010-12-21 13:34:31 -0800447
448 if (!fusion->reply_frames_desc_pool) {
449 printk(KERN_ERR "megasas; Could not allocate memory for "
450 "reply_frame pool\n");
451 goto fail_reply_desc;
452 }
453
454 fusion->reply_frames_desc =
455 pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
456 &fusion->reply_frames_desc_phys);
457 if (!fusion->reply_frames_desc) {
458 printk(KERN_ERR "megasas; Could not allocate memory for "
459 "reply_frame pool\n");
460 pci_pool_destroy(fusion->reply_frames_desc_pool);
461 goto fail_reply_desc;
462 }
463
464 reply_desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -0700465 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
adam radford9c915a82010-12-21 13:34:31 -0800466 reply_desc->Words = ULLONG_MAX;
467
468 io_frames_sz = fusion->io_frames_alloc_sz;
469
470 fusion->io_request_frames_pool =
471 pci_pool_create("io_request_frames pool", instance->pdev,
472 fusion->io_frames_alloc_sz, 16, 0);
473
474 if (!fusion->io_request_frames_pool) {
475 printk(KERN_ERR "megasas: Could not allocate memory for "
476 "io_request_frame pool\n");
477 goto fail_io_frames;
478 }
479
480 fusion->io_request_frames =
481 pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
482 &fusion->io_request_frames_phys);
483 if (!fusion->io_request_frames) {
484 printk(KERN_ERR "megasas: Could not allocate memory for "
485 "io_request_frames frames\n");
486 pci_pool_destroy(fusion->io_request_frames_pool);
487 goto fail_io_frames;
488 }
489
490 /*
491 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
492 * Allocate the dynamic array first and then allocate individual
493 * commands.
494 */
Fengguang Wu7731e6b2012-08-24 23:27:35 +0800495 fusion->cmd_list = kzalloc(sizeof(struct megasas_cmd_fusion *)
496 * max_cmd, GFP_KERNEL);
adam radford9c915a82010-12-21 13:34:31 -0800497
498 if (!fusion->cmd_list) {
499 printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
500 "memory for cmd_list_fusion\n");
501 goto fail_cmd_list;
502 }
503
adam radford9c915a82010-12-21 13:34:31 -0800504 max_cmd = instance->max_fw_cmds;
505 for (i = 0; i < max_cmd; i++) {
506 fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
507 GFP_KERNEL);
508 if (!fusion->cmd_list[i]) {
509 printk(KERN_ERR "Could not alloc cmd list fusion\n");
510
511 for (j = 0; j < i; j++)
512 kfree(fusion->cmd_list[j]);
513
514 kfree(fusion->cmd_list);
515 fusion->cmd_list = NULL;
516 goto fail_cmd_list;
517 }
518 }
519
520 /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
521 io_req_base = fusion->io_request_frames +
522 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
523 io_req_base_phys = fusion->io_request_frames_phys +
524 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
525
526 /*
527 * Add all the commands to command pool (fusion->cmd_pool)
528 */
529
530 /* SMID 0 is reserved. Set SMID/index from 1 */
531 for (i = 0; i < max_cmd; i++) {
532 cmd = fusion->cmd_list[i];
533 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
534 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
535 cmd->index = i + 1;
536 cmd->scmd = NULL;
537 cmd->sync_cmd_idx = (u32)ULONG_MAX; /* Set to Invalid */
538 cmd->instance = instance;
539 cmd->io_request =
540 (struct MPI2_RAID_SCSI_IO_REQUEST *)
541 (io_req_base + offset);
542 memset(cmd->io_request, 0,
543 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
544 cmd->io_request_phys_addr = io_req_base_phys + offset;
545
546 list_add_tail(&cmd->list, &fusion->cmd_pool);
547 }
548
549 /*
550 * Create a frame pool and assign one frame to each cmd
551 */
552 if (megasas_create_frame_pool_fusion(instance)) {
553 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
554 megasas_free_cmds_fusion(instance);
555 goto fail_req_desc;
556 }
557
558 return 0;
559
560fail_cmd_list:
561 pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
562 fusion->io_request_frames_phys);
563 pci_pool_destroy(fusion->io_request_frames_pool);
564fail_io_frames:
565 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
566 fusion->reply_frames_desc,
567 fusion->reply_frames_desc_phys);
568 pci_pool_free(fusion->reply_frames_desc_pool,
569 fusion->reply_frames_desc,
570 fusion->reply_frames_desc_phys);
571 pci_pool_destroy(fusion->reply_frames_desc_pool);
572
573fail_reply_desc:
574 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
575 fusion->req_frames_desc,
576 fusion->req_frames_desc_phys);
577fail_req_desc:
578 return -ENOMEM;
579}
580
581/**
582 * wait_and_poll - Issues a polling command
583 * @instance: Adapter soft state
584 * @cmd: Command packet to be issued
585 *
586 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
587 */
588int
adam radford229fe472014-03-10 02:51:56 -0700589wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
590 int seconds)
adam radford9c915a82010-12-21 13:34:31 -0800591{
592 int i;
593 struct megasas_header *frame_hdr = &cmd->frame->hdr;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530594 struct fusion_context *fusion;
adam radford9c915a82010-12-21 13:34:31 -0800595
adam radford229fe472014-03-10 02:51:56 -0700596 u32 msecs = seconds * 1000;
adam radford9c915a82010-12-21 13:34:31 -0800597
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530598 fusion = instance->ctrl_context;
adam radford9c915a82010-12-21 13:34:31 -0800599 /*
600 * Wait for cmd_status to change
601 */
602 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
603 rmb();
604 msleep(20);
605 }
606
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530607 if (frame_hdr->cmd_status == 0xff) {
608 if (fusion)
609 megasas_return_mfi_mpt_pthr(instance, cmd,
610 cmd->mpt_pthr_cmd_blocked);
adam radford9c915a82010-12-21 13:34:31 -0800611 return -ETIME;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530612 }
adam radford9c915a82010-12-21 13:34:31 -0800613
614 return 0;
615}
616
617/**
618 * megasas_ioc_init_fusion - Initializes the FW
619 * @instance: Adapter soft state
620 *
621 * Issues the IOC Init cmd
622 */
623int
624megasas_ioc_init_fusion(struct megasas_instance *instance)
625{
626 struct megasas_init_frame *init_frame;
627 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
628 dma_addr_t ioc_init_handle;
adam radford9c915a82010-12-21 13:34:31 -0800629 struct megasas_cmd *cmd;
630 u8 ret;
631 struct fusion_context *fusion;
adam radfordc77a9bd2014-03-10 02:51:36 -0700632 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
adam radford9c915a82010-12-21 13:34:31 -0800633 int i;
634 struct megasas_header *frame_hdr;
635
636 fusion = instance->ctrl_context;
637
638 cmd = megasas_get_cmd(instance);
639
640 if (!cmd) {
641 printk(KERN_ERR "Could not allocate cmd for INIT Frame\n");
642 ret = 1;
643 goto fail_get_cmd;
644 }
645
646 IOCInitMessage =
647 dma_alloc_coherent(&instance->pdev->dev,
648 sizeof(struct MPI2_IOC_INIT_REQUEST),
649 &ioc_init_handle, GFP_KERNEL);
650
651 if (!IOCInitMessage) {
652 printk(KERN_ERR "Could not allocate memory for "
653 "IOCInitMessage\n");
654 ret = 1;
655 goto fail_fw_init;
656 }
657
658 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
659
660 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
661 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530662 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
663 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
664 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
adam radford9c915a82010-12-21 13:34:31 -0800665
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530666 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
667 IOCInitMessage->ReplyDescriptorPostQueueAddress = cpu_to_le64(fusion->reply_frames_desc_phys);
668 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
adam radford5738f992012-03-19 19:49:53 -0700669 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
adam radford9c915a82010-12-21 13:34:31 -0800670 init_frame = (struct megasas_init_frame *)cmd->frame;
671 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
672
673 frame_hdr = &cmd->frame->hdr;
adam radford9c915a82010-12-21 13:34:31 -0800674 frame_hdr->cmd_status = 0xFF;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530675 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
adam radford9c915a82010-12-21 13:34:31 -0800676
677 init_frame->cmd = MFI_CMD_INIT;
678 init_frame->cmd_status = 0xFF;
679
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530680 /* driver support Extended MSIX */
681 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
682 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
683 init_frame->driver_operations.
684 mfi_capabilities.support_additional_msix = 1;
adam radford21c9e162013-09-06 15:27:14 -0700685 /* driver supports HA / Remote LUN over Fast Path interface */
686 init_frame->driver_operations.mfi_capabilities.support_fp_remote_lun
687 = 1;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530688 init_frame->driver_operations.mfi_capabilities.support_max_255lds
689 = 1;
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +0530690 init_frame->driver_operations.mfi_capabilities.support_ndrive_r1_lb
691 = 1;
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +0530692 init_frame->driver_operations.mfi_capabilities.security_protocol_cmds_fw
693 = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530694 /* Convert capability to LE32 */
695 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
adam radford21c9e162013-09-06 15:27:14 -0700696
Hannes Reineckefdc5a972014-01-16 11:25:33 +0100697 init_frame->queue_info_new_phys_addr_hi =
698 cpu_to_le32(upper_32_bits(ioc_init_handle));
699 init_frame->queue_info_new_phys_addr_lo =
700 cpu_to_le32(lower_32_bits(ioc_init_handle));
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530701 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
adam radford9c915a82010-12-21 13:34:31 -0800702
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +0530703 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
704 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
adam radfordc77a9bd2014-03-10 02:51:36 -0700705 req_desc.MFAIo.RequestFlags =
adam radford9c915a82010-12-21 13:34:31 -0800706 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +0530707 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford9c915a82010-12-21 13:34:31 -0800708
709 /*
710 * disable the intr before firing the init frame
711 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530712 instance->instancet->disable_intr(instance);
adam radford9c915a82010-12-21 13:34:31 -0800713
714 for (i = 0; i < (10 * 1000); i += 20) {
715 if (readl(&instance->reg_set->doorbell) & 1)
716 msleep(20);
717 else
718 break;
719 }
720
adam radfordc77a9bd2014-03-10 02:51:36 -0700721 instance->instancet->fire_cmd(instance, req_desc.u.low,
722 req_desc.u.high, instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -0800723
adam radford229fe472014-03-10 02:51:56 -0700724 wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -0800725
726 frame_hdr = &cmd->frame->hdr;
727 if (frame_hdr->cmd_status != 0) {
728 ret = 1;
729 goto fail_fw_init;
730 }
731 printk(KERN_ERR "megasas:IOC Init cmd success\n");
732
733 ret = 0;
734
735fail_fw_init:
736 megasas_return_cmd(instance, cmd);
737 if (IOCInitMessage)
738 dma_free_coherent(&instance->pdev->dev,
739 sizeof(struct MPI2_IOC_INIT_REQUEST),
740 IOCInitMessage, ioc_init_handle);
741fail_get_cmd:
742 return ret;
743}
744
745/*
adam radford9c915a82010-12-21 13:34:31 -0800746 * megasas_get_ld_map_info - Returns FW's ld_map structure
747 * @instance: Adapter soft state
748 * @pend: Pend the command or not
749 * Issues an internal command (DCMD) to get the FW's controller PD
750 * list structure. This information is mainly used to find out SYSTEM
751 * supported by the FW.
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530752 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
753 * dcmd.mbox.b[0] - number of LDs being sync'd
754 * dcmd.mbox.b[1] - 0 - complete command immediately.
755 * - 1 - pend till config change
756 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
757 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
758 * uses extended struct MR_FW_RAID_MAP_EXT
adam radford9c915a82010-12-21 13:34:31 -0800759 */
760static int
761megasas_get_ld_map_info(struct megasas_instance *instance)
762{
763 int ret = 0;
764 struct megasas_cmd *cmd;
765 struct megasas_dcmd_frame *dcmd;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530766 void *ci;
adam radford9c915a82010-12-21 13:34:31 -0800767 dma_addr_t ci_h = 0;
768 u32 size_map_info;
769 struct fusion_context *fusion;
770
771 cmd = megasas_get_cmd(instance);
772
773 if (!cmd) {
774 printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
775 return -ENOMEM;
776 }
777
778 fusion = instance->ctrl_context;
779
780 if (!fusion) {
781 megasas_return_cmd(instance, cmd);
Hannes Reinecke2f8bdfa2014-01-16 11:25:35 +0100782 return -ENXIO;
adam radford9c915a82010-12-21 13:34:31 -0800783 }
784
785 dcmd = &cmd->frame->dcmd;
786
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530787 size_map_info = fusion->current_map_sz;
adam radford9c915a82010-12-21 13:34:31 -0800788
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530789 ci = (void *) fusion->ld_map[(instance->map_id & 1)];
adam radford9c915a82010-12-21 13:34:31 -0800790 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
791
792 if (!ci) {
793 printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
794 megasas_return_cmd(instance, cmd);
795 return -ENOMEM;
796 }
797
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530798 memset(ci, 0, fusion->max_map_sz);
adam radford9c915a82010-12-21 13:34:31 -0800799 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530800#if VD_EXT_DEBUG
801 dev_dbg(&instance->pdev->dev,
802 "%s sending MR_DCMD_LD_MAP_GET_INFO with size %d\n",
803 __func__, cpu_to_le32(size_map_info));
804#endif
adam radford9c915a82010-12-21 13:34:31 -0800805 dcmd->cmd = MFI_CMD_DCMD;
806 dcmd->cmd_status = 0xFF;
807 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530808 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford9c915a82010-12-21 13:34:31 -0800809 dcmd->timeout = 0;
810 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530811 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
812 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
813 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
814 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -0800815
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530816 if (instance->ctrl_context && !instance->mask_interrupts)
817 ret = megasas_issue_blocked_cmd(instance, cmd,
818 MEGASAS_BLOCKED_CMD_TIMEOUT);
819 else
820 ret = megasas_issue_polled(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -0800821
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530822 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
823 megasas_return_mfi_mpt_pthr(instance, cmd,
824 cmd->mpt_pthr_cmd_blocked);
825 else
826 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -0800827
828 return ret;
829}
830
831u8
832megasas_get_map_info(struct megasas_instance *instance)
833{
834 struct fusion_context *fusion = instance->ctrl_context;
835
836 fusion->fast_path_io = 0;
837 if (!megasas_get_ld_map_info(instance)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +0530838 if (MR_ValidateMapInfo(instance)) {
adam radford9c915a82010-12-21 13:34:31 -0800839 fusion->fast_path_io = 1;
840 return 0;
841 }
842 }
843 return 1;
844}
845
846/*
847 * megasas_sync_map_info - Returns FW's ld_map structure
848 * @instance: Adapter soft state
849 *
850 * Issues an internal command (DCMD) to get the FW's controller PD
851 * list structure. This information is mainly used to find out SYSTEM
852 * supported by the FW.
853 */
854int
855megasas_sync_map_info(struct megasas_instance *instance)
856{
857 int ret = 0, i;
858 struct megasas_cmd *cmd;
859 struct megasas_dcmd_frame *dcmd;
860 u32 size_sync_info, num_lds;
861 struct fusion_context *fusion;
862 struct MR_LD_TARGET_SYNC *ci = NULL;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530863 struct MR_DRV_RAID_MAP_ALL *map;
adam radford9c915a82010-12-21 13:34:31 -0800864 struct MR_LD_RAID *raid;
865 struct MR_LD_TARGET_SYNC *ld_sync;
866 dma_addr_t ci_h = 0;
867 u32 size_map_info;
868
869 cmd = megasas_get_cmd(instance);
870
871 if (!cmd) {
872 printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
873 "info.\n");
874 return -ENOMEM;
875 }
876
877 fusion = instance->ctrl_context;
878
879 if (!fusion) {
880 megasas_return_cmd(instance, cmd);
881 return 1;
882 }
883
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530884 map = fusion->ld_drv_map[instance->map_id & 1];
adam radford9c915a82010-12-21 13:34:31 -0800885
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +0530886 num_lds = le16_to_cpu(map->raidMap.ldCount);
adam radford9c915a82010-12-21 13:34:31 -0800887
888 dcmd = &cmd->frame->dcmd;
889
890 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
891
892 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
893
894 ci = (struct MR_LD_TARGET_SYNC *)
895 fusion->ld_map[(instance->map_id - 1) & 1];
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530896 memset(ci, 0, fusion->max_map_sz);
adam radford9c915a82010-12-21 13:34:31 -0800897
898 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
899
900 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
901
902 for (i = 0; i < num_lds; i++, ld_sync++) {
903 raid = MR_LdRaidGet(i, map);
904 ld_sync->targetId = MR_GetLDTgtId(i, map);
905 ld_sync->seqNum = raid->seqNum;
906 }
907
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530908 size_map_info = fusion->current_map_sz;
adam radford9c915a82010-12-21 13:34:31 -0800909
910 dcmd->cmd = MFI_CMD_DCMD;
911 dcmd->cmd_status = 0xFF;
912 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530913 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
adam radford9c915a82010-12-21 13:34:31 -0800914 dcmd->timeout = 0;
915 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530916 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -0800917 dcmd->mbox.b[0] = num_lds;
918 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530919 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
920 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
921 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -0800922
923 instance->map_update_cmd = cmd;
924
925 instance->instancet->issue_dcmd(instance, cmd);
926
927 return ret;
928}
929
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +0530930/*
931 * meagasas_display_intel_branding - Display branding string
932 * @instance: per adapter object
933 *
934 * Return nothing.
935 */
936static void
937megasas_display_intel_branding(struct megasas_instance *instance)
938{
939 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
940 return;
941
942 switch (instance->pdev->device) {
943 case PCI_DEVICE_ID_LSI_INVADER:
944 switch (instance->pdev->subsystem_device) {
945 case MEGARAID_INTEL_RS3DC080_SSDID:
946 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
947 instance->host->host_no,
948 MEGARAID_INTEL_RS3DC080_BRANDING);
949 break;
950 case MEGARAID_INTEL_RS3DC040_SSDID:
951 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
952 instance->host->host_no,
953 MEGARAID_INTEL_RS3DC040_BRANDING);
954 break;
955 case MEGARAID_INTEL_RS3SC008_SSDID:
956 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
957 instance->host->host_no,
958 MEGARAID_INTEL_RS3SC008_BRANDING);
959 break;
960 case MEGARAID_INTEL_RS3MC044_SSDID:
961 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
962 instance->host->host_no,
963 MEGARAID_INTEL_RS3MC044_BRANDING);
964 break;
965 default:
966 break;
967 }
968 break;
969 case PCI_DEVICE_ID_LSI_FURY:
970 switch (instance->pdev->subsystem_device) {
971 case MEGARAID_INTEL_RS3WC080_SSDID:
972 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
973 instance->host->host_no,
974 MEGARAID_INTEL_RS3WC080_BRANDING);
975 break;
976 case MEGARAID_INTEL_RS3WC040_SSDID:
977 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
978 instance->host->host_no,
979 MEGARAID_INTEL_RS3WC040_BRANDING);
980 break;
981 default:
982 break;
983 }
984 break;
985 default:
986 break;
987 }
988}
989
adam radford9c915a82010-12-21 13:34:31 -0800990/**
991 * megasas_init_adapter_fusion - Initializes the FW
992 * @instance: Adapter soft state
993 *
994 * This is the main function for initializing firmware.
995 */
996u32
997megasas_init_adapter_fusion(struct megasas_instance *instance)
998{
999 struct megasas_register_set __iomem *reg_set;
1000 struct fusion_context *fusion;
1001 u32 max_cmd;
adam radfordc8e858f2011-10-08 18:15:13 -07001002 int i = 0, count;
adam radford9c915a82010-12-21 13:34:31 -08001003
1004 fusion = instance->ctrl_context;
1005
1006 reg_set = instance->reg_set;
1007
1008 /*
1009 * Get various operational parameters from status register
1010 */
1011 instance->max_fw_cmds =
1012 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
1013 instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
1014
1015 /*
1016 * Reduce the max supported cmds by 1. This is to ensure that the
1017 * reply_q_sz (1 more than the max cmd that driver may send)
1018 * does not exceed max cmds that the FW can support
1019 */
1020 instance->max_fw_cmds = instance->max_fw_cmds-1;
1021 /* Only internal cmds (DCMD) need to have MFI frames */
1022 instance->max_mfi_cmds = MEGASAS_INT_CMDS;
1023
1024 max_cmd = instance->max_fw_cmds;
1025
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05301026 fusion->reply_q_depth = 2 * (((max_cmd + 1 + 15)/16)*16);
adam radford9c915a82010-12-21 13:34:31 -08001027
1028 fusion->request_alloc_sz =
1029 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
1030 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
1031 *(fusion->reply_q_depth);
1032 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
1033 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
1034 (max_cmd + 1)); /* Extra 1 for SMID 0 */
1035
1036 fusion->max_sge_in_main_msg =
1037 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1038 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
1039
1040 fusion->max_sge_in_chain =
1041 MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
1042
Sumit.Saxena@avagotech.coma5fd2852014-09-12 18:57:43 +05301043 instance->max_num_sge = rounddown_pow_of_two(
1044 fusion->max_sge_in_main_msg + fusion->max_sge_in_chain - 2);
adam radford9c915a82010-12-21 13:34:31 -08001045
1046 /* Used for pass thru MFI frame (DCMD) */
1047 fusion->chain_offset_mfi_pthru =
1048 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1049
1050 fusion->chain_offset_io_request =
1051 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1052 sizeof(union MPI2_SGE_IO_UNION))/16;
1053
adam radfordc8e858f2011-10-08 18:15:13 -07001054 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1055 for (i = 0 ; i < count; i++)
1056 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -08001057
1058 /*
1059 * Allocate memory for descriptors
1060 * Create a pool of commands
1061 */
1062 if (megasas_alloc_cmds(instance))
1063 goto fail_alloc_mfi_cmds;
1064 if (megasas_alloc_cmds_fusion(instance))
1065 goto fail_alloc_cmds;
1066
1067 if (megasas_ioc_init_fusion(instance))
1068 goto fail_ioc_init;
1069
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301070 megasas_display_intel_branding(instance);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05301071 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301072 dev_err(&instance->pdev->dev,
1073 "Could not get controller info. Fail from %s %d\n",
1074 __func__, __LINE__);
1075 goto fail_ioc_init;
1076 }
1077
adam radford9c915a82010-12-21 13:34:31 -08001078 instance->flag_ieee = 1;
adam radford9c915a82010-12-21 13:34:31 -08001079 fusion->fast_path_io = 0;
1080
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301081 fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1082 for (i = 0; i < 2; i++) {
1083 fusion->ld_map[i] = NULL;
1084 fusion->ld_drv_map[i] = (void *)__get_free_pages(GFP_KERNEL,
1085 fusion->drv_map_pages);
1086 if (!fusion->ld_drv_map[i]) {
1087 dev_err(&instance->pdev->dev, "Could not allocate "
1088 "memory for local map info for %d pages\n",
1089 fusion->drv_map_pages);
1090 if (i == 1)
1091 free_pages((ulong)fusion->ld_drv_map[0],
1092 fusion->drv_map_pages);
1093 goto fail_ioc_init;
1094 }
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05301095 memset(fusion->ld_drv_map[i], 0,
1096 ((1 << PAGE_SHIFT) << fusion->drv_map_pages));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301097 }
1098
adam radford9c915a82010-12-21 13:34:31 -08001099 for (i = 0; i < 2; i++) {
1100 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301101 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08001102 &fusion->ld_map_phys[i],
1103 GFP_KERNEL);
1104 if (!fusion->ld_map[i]) {
1105 printk(KERN_ERR "megasas: Could not allocate memory "
1106 "for map info\n");
1107 goto fail_map_info;
1108 }
1109 }
1110
1111 if (!megasas_get_map_info(instance))
1112 megasas_sync_map_info(instance);
1113
1114 return 0;
1115
adam radford9c915a82010-12-21 13:34:31 -08001116fail_map_info:
1117 if (i == 1)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301118 dma_free_coherent(&instance->pdev->dev, fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08001119 fusion->ld_map[0], fusion->ld_map_phys[0]);
1120fail_ioc_init:
adam radfordeb1b1232011-02-24 20:55:56 -08001121 megasas_free_cmds_fusion(instance);
1122fail_alloc_cmds:
1123 megasas_free_cmds(instance);
1124fail_alloc_mfi_cmds:
adam radford9c915a82010-12-21 13:34:31 -08001125 return 1;
1126}
1127
1128/**
1129 * megasas_fire_cmd_fusion - Sends command to the FW
1130 * @frame_phys_addr : Physical address of cmd
1131 * @frame_count : Number of frames for the command
1132 * @regs : MFI register set
1133 */
1134void
1135megasas_fire_cmd_fusion(struct megasas_instance *instance,
1136 dma_addr_t req_desc_lo,
1137 u32 req_desc_hi,
1138 struct megasas_register_set __iomem *regs)
1139{
Sumit.Saxena@avagotech.com07560402014-09-12 18:57:18 +05301140#if defined(writeq) && defined(CONFIG_64BIT)
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301141 u64 req_data = (((u64)le32_to_cpu(req_desc_hi) << 32) |
1142 le32_to_cpu(req_desc_lo));
Sumit.Saxena@avagotech.com07560402014-09-12 18:57:18 +05301143
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301144 writeq(req_data, &(regs)->inbound_low_queue_port);
Sumit.Saxena@avagotech.com07560402014-09-12 18:57:18 +05301145#else
adam radford9c915a82010-12-21 13:34:31 -08001146 unsigned long flags;
1147
1148 spin_lock_irqsave(&instance->hba_lock, flags);
1149
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301150 writel(le32_to_cpu(req_desc_lo), &(regs)->inbound_low_queue_port);
1151 writel(le32_to_cpu(req_desc_hi), &(regs)->inbound_high_queue_port);
adam radford9c915a82010-12-21 13:34:31 -08001152 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@avagotech.com07560402014-09-12 18:57:18 +05301153#endif
adam radford9c915a82010-12-21 13:34:31 -08001154}
1155
1156/**
1157 * map_cmd_status - Maps FW cmd status to OS cmd status
1158 * @cmd : Pointer to cmd
1159 * @status : status of cmd returned by FW
1160 * @ext_status : ext status of cmd returned by FW
1161 */
1162
1163void
1164map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1165{
1166
1167 switch (status) {
1168
1169 case MFI_STAT_OK:
1170 cmd->scmd->result = DID_OK << 16;
1171 break;
1172
1173 case MFI_STAT_SCSI_IO_FAILED:
1174 case MFI_STAT_LD_INIT_IN_PROGRESS:
1175 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1176 break;
1177
1178 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1179
1180 cmd->scmd->result = (DID_OK << 16) | ext_status;
1181 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1182 memset(cmd->scmd->sense_buffer, 0,
1183 SCSI_SENSE_BUFFERSIZE);
1184 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1185 SCSI_SENSE_BUFFERSIZE);
1186 cmd->scmd->result |= DRIVER_SENSE << 24;
1187 }
1188 break;
1189
1190 case MFI_STAT_LD_OFFLINE:
1191 case MFI_STAT_DEVICE_NOT_FOUND:
1192 cmd->scmd->result = DID_BAD_TARGET << 16;
1193 break;
adam radford36807e62011-10-08 18:15:06 -07001194 case MFI_STAT_CONFIG_SEQ_MISMATCH:
1195 cmd->scmd->result = DID_IMM_RETRY << 16;
1196 break;
adam radford9c915a82010-12-21 13:34:31 -08001197 default:
1198 printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1199 cmd->scmd->result = DID_ERROR << 16;
1200 break;
1201 }
1202}
1203
1204/**
1205 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1206 * @instance: Adapter soft state
1207 * @scp: SCSI command from the mid-layer
1208 * @sgl_ptr: SGL to be filled in
1209 * @cmd: cmd we are working on
1210 *
1211 * If successful, this function returns the number of SG elements.
1212 */
1213static int
1214megasas_make_sgl_fusion(struct megasas_instance *instance,
1215 struct scsi_cmnd *scp,
1216 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1217 struct megasas_cmd_fusion *cmd)
1218{
adam radford36807e62011-10-08 18:15:06 -07001219 int i, sg_processed, sge_count;
adam radford9c915a82010-12-21 13:34:31 -08001220 struct scatterlist *os_sgl;
1221 struct fusion_context *fusion;
1222
1223 fusion = instance->ctrl_context;
1224
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301225 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1226 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001227 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1228 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1229 sgl_ptr_end->Flags = 0;
1230 }
adam radford9c915a82010-12-21 13:34:31 -08001231
1232 sge_count = scsi_dma_map(scp);
1233
1234 BUG_ON(sge_count < 0);
1235
1236 if (sge_count > instance->max_num_sge || !sge_count)
1237 return sge_count;
1238
adam radford9c915a82010-12-21 13:34:31 -08001239 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301240 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
1241 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
adam radford9c915a82010-12-21 13:34:31 -08001242 sgl_ptr->Flags = 0;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301243 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1244 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001245 if (i == sge_count - 1)
1246 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1247 }
adam radford9c915a82010-12-21 13:34:31 -08001248 sgl_ptr++;
1249
1250 sg_processed = i + 1;
1251
1252 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1253 (sge_count > fusion->max_sge_in_main_msg)) {
1254
1255 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301256 if ((instance->pdev->device ==
1257 PCI_DEVICE_ID_LSI_INVADER) ||
1258 (instance->pdev->device ==
1259 PCI_DEVICE_ID_LSI_FURY)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301260 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1261 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1262 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
adam radford36807e62011-10-08 18:15:06 -07001263 cmd->io_request->ChainOffset =
1264 fusion->
1265 chain_offset_io_request;
1266 else
1267 cmd->io_request->ChainOffset = 0;
1268 } else
1269 cmd->io_request->ChainOffset =
1270 fusion->chain_offset_io_request;
1271
adam radford9c915a82010-12-21 13:34:31 -08001272 sg_chain = sgl_ptr;
1273 /* Prepare chain element */
1274 sg_chain->NextChainOffset = 0;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301275 if ((instance->pdev->device ==
1276 PCI_DEVICE_ID_LSI_INVADER) ||
1277 (instance->pdev->device ==
1278 PCI_DEVICE_ID_LSI_FURY))
adam radford36807e62011-10-08 18:15:06 -07001279 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1280 else
1281 sg_chain->Flags =
1282 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1283 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301284 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
1285 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08001286
1287 sgl_ptr =
1288 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301289 memset(sgl_ptr, 0, MEGASAS_MAX_SZ_CHAIN_FRAME);
adam radford9c915a82010-12-21 13:34:31 -08001290 }
1291 }
1292
1293 return sge_count;
1294}
1295
1296/**
1297 * megasas_set_pd_lba - Sets PD LBA
1298 * @cdb: CDB
1299 * @cdb_len: cdb length
1300 * @start_blk: Start block of IO
1301 *
1302 * Used to set the PD LBA in CDB for FP IOs
1303 */
1304void
1305megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1306 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301307 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
adam radford9c915a82010-12-21 13:34:31 -08001308{
1309 struct MR_LD_RAID *raid;
1310 u32 ld;
1311 u64 start_blk = io_info->pdBlock;
1312 u8 *cdb = io_request->CDB.CDB32;
1313 u32 num_blocks = io_info->numBlocks;
adam radford495c5602011-05-11 18:34:40 -07001314 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
adam radford9c915a82010-12-21 13:34:31 -08001315
1316 /* Check if T10 PI (DIF) is enabled for this LD */
1317 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1318 raid = MR_LdRaidGet(ld, local_map_ptr);
1319 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1320 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1321 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1322 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1323
1324 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1325 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1326 else
1327 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1328 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1329
1330 /* LBA */
1331 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1332 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1333 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1334 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1335 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1336 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1337 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1338 cdb[19] = (u8)(start_blk & 0xff);
1339
1340 /* Logical block reference tag */
1341 io_request->CDB.EEDP32.PrimaryReferenceTag =
1342 cpu_to_be32(ref_tag);
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301343 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301344 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001345
1346 /* Transfer length */
1347 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1348 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1349 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1350 cdb[31] = (u8)(num_blocks & 0xff);
1351
1352 /* set SCSI IO EEDPFlags */
1353 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301354 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001355 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1356 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1357 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1358 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301359 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
adam radford9c915a82010-12-21 13:34:31 -08001360 } else {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301361 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001362 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301363 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
adam radford9c915a82010-12-21 13:34:31 -08001364 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301365 io_request->Control |= cpu_to_le32((0x4 << 26));
1366 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
adam radford9c915a82010-12-21 13:34:31 -08001367 } else {
1368 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1369 if (((cdb_len == 12) || (cdb_len == 16)) &&
1370 (start_blk <= 0xffffffff)) {
1371 if (cdb_len == 16) {
1372 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1373 flagvals = cdb[1];
1374 groupnum = cdb[14];
1375 control = cdb[15];
1376 } else {
1377 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1378 flagvals = cdb[1];
1379 groupnum = cdb[10];
1380 control = cdb[11];
1381 }
1382
1383 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1384
1385 cdb[0] = opcode;
1386 cdb[1] = flagvals;
1387 cdb[6] = groupnum;
1388 cdb[9] = control;
1389
1390 /* Transfer length */
1391 cdb[8] = (u8)(num_blocks & 0xff);
1392 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1393
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301394 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001395 cdb_len = 10;
adam radford495c5602011-05-11 18:34:40 -07001396 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1397 /* Convert to 16 byte CDB for large LBA's */
1398 switch (cdb_len) {
1399 case 6:
1400 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1401 control = cdb[5];
1402 break;
1403 case 10:
1404 opcode =
1405 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1406 flagvals = cdb[1];
1407 groupnum = cdb[6];
1408 control = cdb[9];
1409 break;
1410 case 12:
1411 opcode =
1412 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1413 flagvals = cdb[1];
1414 groupnum = cdb[10];
1415 control = cdb[11];
1416 break;
1417 }
1418
1419 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1420
1421 cdb[0] = opcode;
1422 cdb[1] = flagvals;
1423 cdb[14] = groupnum;
1424 cdb[15] = control;
1425
1426 /* Transfer length */
1427 cdb[13] = (u8)(num_blocks & 0xff);
1428 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1429 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1430 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1431
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301432 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
adam radford495c5602011-05-11 18:34:40 -07001433 cdb_len = 16;
adam radford9c915a82010-12-21 13:34:31 -08001434 }
1435
1436 /* Normal case, just load LBA here */
1437 switch (cdb_len) {
1438 case 6:
1439 {
1440 u8 val = cdb[1] & 0xE0;
1441 cdb[3] = (u8)(start_blk & 0xff);
1442 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1443 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1444 break;
1445 }
1446 case 10:
1447 cdb[5] = (u8)(start_blk & 0xff);
1448 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1449 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1450 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1451 break;
1452 case 12:
1453 cdb[5] = (u8)(start_blk & 0xff);
1454 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1455 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1456 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1457 break;
1458 case 16:
1459 cdb[9] = (u8)(start_blk & 0xff);
1460 cdb[8] = (u8)((start_blk >> 8) & 0xff);
1461 cdb[7] = (u8)((start_blk >> 16) & 0xff);
1462 cdb[6] = (u8)((start_blk >> 24) & 0xff);
1463 cdb[5] = (u8)((start_blk >> 32) & 0xff);
1464 cdb[4] = (u8)((start_blk >> 40) & 0xff);
1465 cdb[3] = (u8)((start_blk >> 48) & 0xff);
1466 cdb[2] = (u8)((start_blk >> 56) & 0xff);
1467 break;
1468 }
1469 }
1470}
1471
1472/**
1473 * megasas_build_ldio_fusion - Prepares IOs to devices
1474 * @instance: Adapter soft state
1475 * @scp: SCSI command
1476 * @cmd: Command to be prepared
1477 *
1478 * Prepares the io_request and chain elements (sg_frame) for IO
1479 * The IO can be for PD (Fast Path) or LD
1480 */
1481void
1482megasas_build_ldio_fusion(struct megasas_instance *instance,
1483 struct scsi_cmnd *scp,
1484 struct megasas_cmd_fusion *cmd)
1485{
1486 u8 fp_possible;
adam radfordf9eff812012-10-01 19:27:07 -07001487 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
adam radford9c915a82010-12-21 13:34:31 -08001488 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1489 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1490 struct IO_REQUEST_INFO io_info;
1491 struct fusion_context *fusion;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301492 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
adam radford21c9e162013-09-06 15:27:14 -07001493 u8 *raidLUN;
adam radford9c915a82010-12-21 13:34:31 -08001494
1495 device_id = MEGASAS_DEV_INDEX(instance, scp);
1496
1497 fusion = instance->ctrl_context;
1498
1499 io_request = cmd->io_request;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301500 io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
adam radford9c915a82010-12-21 13:34:31 -08001501 io_request->RaidContext.status = 0;
1502 io_request->RaidContext.exStatus = 0;
1503
1504 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1505
1506 start_lba_lo = 0;
1507 start_lba_hi = 0;
1508 fp_possible = 0;
1509
1510 /*
1511 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1512 */
1513 if (scp->cmd_len == 6) {
adam radfordf9eff812012-10-01 19:27:07 -07001514 datalength = (u32) scp->cmnd[4];
adam radford9c915a82010-12-21 13:34:31 -08001515 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1516 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1517
1518 start_lba_lo &= 0x1FFFFF;
1519 }
1520
1521 /*
1522 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1523 */
1524 else if (scp->cmd_len == 10) {
adam radfordf9eff812012-10-01 19:27:07 -07001525 datalength = (u32) scp->cmnd[8] |
adam radford9c915a82010-12-21 13:34:31 -08001526 ((u32) scp->cmnd[7] << 8);
1527 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1528 ((u32) scp->cmnd[3] << 16) |
1529 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1530 }
1531
1532 /*
1533 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1534 */
1535 else if (scp->cmd_len == 12) {
adam radfordf9eff812012-10-01 19:27:07 -07001536 datalength = ((u32) scp->cmnd[6] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08001537 ((u32) scp->cmnd[7] << 16) |
1538 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1539 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1540 ((u32) scp->cmnd[3] << 16) |
1541 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1542 }
1543
1544 /*
1545 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1546 */
1547 else if (scp->cmd_len == 16) {
adam radfordf9eff812012-10-01 19:27:07 -07001548 datalength = ((u32) scp->cmnd[10] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08001549 ((u32) scp->cmnd[11] << 16) |
1550 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1551 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1552 ((u32) scp->cmnd[7] << 16) |
1553 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1554
1555 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1556 ((u32) scp->cmnd[3] << 16) |
1557 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1558 }
1559
1560 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1561 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
adam radfordf9eff812012-10-01 19:27:07 -07001562 io_info.numBlocks = datalength;
adam radford9c915a82010-12-21 13:34:31 -08001563 io_info.ldTgtId = device_id;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301564 io_request->DataLength = cpu_to_le32(scsi_bufflen(scp));
adam radford9c915a82010-12-21 13:34:31 -08001565
1566 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1567 io_info.isRead = 1;
1568
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301569 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
adam radford9c915a82010-12-21 13:34:31 -08001570
1571 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301572 instance->fw_supported_vd_count) || (!fusion->fast_path_io)) {
adam radford9c915a82010-12-21 13:34:31 -08001573 io_request->RaidContext.regLockFlags = 0;
1574 fp_possible = 0;
1575 } else {
adam radford36807e62011-10-08 18:15:06 -07001576 if (MR_BuildRaidContext(instance, &io_info,
1577 &io_request->RaidContext,
adam radford21c9e162013-09-06 15:27:14 -07001578 local_map_ptr, &raidLUN))
adam radford9c915a82010-12-21 13:34:31 -08001579 fp_possible = io_info.fpOkForIo;
1580 }
1581
adam radfordc8e858f2011-10-08 18:15:13 -07001582 /* Use smp_processor_id() for now until cmd->request->cpu is CPU
1583 id by default, not CPU group id, otherwise all MSI-X queues won't
1584 be utilized */
1585 cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
1586 smp_processor_id() % instance->msix_vectors : 0;
1587
adam radford9c915a82010-12-21 13:34:31 -08001588 if (fp_possible) {
1589 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1590 local_map_ptr, start_lba_lo);
adam radford9c915a82010-12-21 13:34:31 -08001591 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1592 cmd->request_desc->SCSIIO.RequestFlags =
1593 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1594 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301595 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1596 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001597 if (io_request->RaidContext.regLockFlags ==
1598 REGION_TYPE_UNUSED)
1599 cmd->request_desc->SCSIIO.RequestFlags =
1600 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1601 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1602 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1603 io_request->RaidContext.nseg = 0x1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301604 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
adam radford36807e62011-10-08 18:15:06 -07001605 io_request->RaidContext.regLockFlags |=
1606 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
1607 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1608 }
adam radford9c915a82010-12-21 13:34:31 -08001609 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1610 (io_info.isRead)) {
1611 io_info.devHandle =
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05301612 get_updated_dev_handle(instance,
adam radford9c915a82010-12-21 13:34:31 -08001613 &fusion->load_balance_info[device_id],
1614 &io_info);
1615 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05301616 cmd->pd_r1_lb = io_info.pd_after_lb;
adam radford9c915a82010-12-21 13:34:31 -08001617 } else
1618 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1619 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1620 io_request->DevHandle = io_info.devHandle;
adam radford21c9e162013-09-06 15:27:14 -07001621 /* populate the LUN field */
1622 memcpy(io_request->LUN, raidLUN, 8);
adam radford9c915a82010-12-21 13:34:31 -08001623 } else {
1624 io_request->RaidContext.timeoutValue =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301625 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
adam radford9c915a82010-12-21 13:34:31 -08001626 cmd->request_desc->SCSIIO.RequestFlags =
1627 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1628 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301629 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1630 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001631 if (io_request->RaidContext.regLockFlags ==
1632 REGION_TYPE_UNUSED)
1633 cmd->request_desc->SCSIIO.RequestFlags =
1634 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1635 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1636 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1637 io_request->RaidContext.regLockFlags |=
1638 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
1639 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1640 io_request->RaidContext.nseg = 0x1;
1641 }
1642 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301643 io_request->DevHandle = cpu_to_le16(device_id);
adam radford9c915a82010-12-21 13:34:31 -08001644 } /* Not FP */
1645}
1646
1647/**
1648 * megasas_build_dcdb_fusion - Prepares IOs to devices
1649 * @instance: Adapter soft state
1650 * @scp: SCSI command
1651 * @cmd: Command to be prepared
1652 *
1653 * Prepares the io_request frame for non-io cmds
1654 */
1655static void
1656megasas_build_dcdb_fusion(struct megasas_instance *instance,
1657 struct scsi_cmnd *scmd,
1658 struct megasas_cmd_fusion *cmd)
1659{
1660 u32 device_id;
1661 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1662 u16 pd_index = 0;
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301663 u16 os_timeout_value;
1664 u16 timeout_limit;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301665 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
adam radford9c915a82010-12-21 13:34:31 -08001666 struct fusion_context *fusion = instance->ctrl_context;
adam radford21c9e162013-09-06 15:27:14 -07001667 u8 span, physArm;
1668 u16 devHandle;
1669 u32 ld, arRef, pd;
1670 struct MR_LD_RAID *raid;
1671 struct RAID_CONTEXT *pRAID_Context;
adam radford9c915a82010-12-21 13:34:31 -08001672
1673 io_request = cmd->io_request;
1674 device_id = MEGASAS_DEV_INDEX(instance, scmd);
1675 pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
1676 +scmd->device->id;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301677 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
adam radford9c915a82010-12-21 13:34:31 -08001678
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301679 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
adam radford21c9e162013-09-06 15:27:14 -07001680
adam radforde187df62012-10-01 19:27:12 -07001681 if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS &&
1682 instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
adam radford9c5ebd02013-02-09 15:29:20 -08001683 if (fusion->fast_path_io)
1684 io_request->DevHandle =
adam radford9c915a82010-12-21 13:34:31 -08001685 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
adam radford9c915a82010-12-21 13:34:31 -08001686 io_request->RaidContext.RAIDFlags =
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301687 MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
1688 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1689 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05301690 cmd->request_desc->SCSIIO.MSIxIndex =
1691 instance->msix_vectors ? smp_processor_id() % instance->msix_vectors : 0;
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301692 os_timeout_value = scmd->request->timeout / HZ;
1693
1694 if (instance->secure_jbod_support &&
1695 (megasas_cmd_type(scmd) == NON_READ_WRITE_SYSPDIO)) {
1696 /* system pd firmware path */
1697 io_request->Function =
1698 MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1699 cmd->request_desc->SCSIIO.RequestFlags =
1700 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1701 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1702 io_request->RaidContext.timeoutValue =
1703 cpu_to_le16(os_timeout_value);
1704 } else {
1705 /* system pd Fast Path */
1706 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1707 io_request->RaidContext.regLockFlags = 0;
1708 io_request->RaidContext.regLockRowLBA = 0;
1709 io_request->RaidContext.regLockLength = 0;
1710 timeout_limit = (scmd->device->type == TYPE_DISK) ?
1711 255 : 0xFFFF;
1712 io_request->RaidContext.timeoutValue =
1713 cpu_to_le16((os_timeout_value > timeout_limit) ?
1714 timeout_limit : os_timeout_value);
1715 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1716 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
1717 io_request->IoFlags |=
1718 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
1719
1720 cmd->request_desc->SCSIIO.RequestFlags =
1721 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1722 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sumit.Saxena@lsi.com32d87452013-05-22 12:31:18 +05301723 }
adam radford9c915a82010-12-21 13:34:31 -08001724 } else {
adam radford21c9e162013-09-06 15:27:14 -07001725 if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS)
1726 goto NonFastPath;
1727
1728 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301729 if ((ld >= instance->fw_supported_vd_count) ||
1730 (!fusion->fast_path_io))
adam radford21c9e162013-09-06 15:27:14 -07001731 goto NonFastPath;
1732
1733 raid = MR_LdRaidGet(ld, local_map_ptr);
1734
1735 /* check if this LD is FP capable */
1736 if (!(raid->capability.fpNonRWCapable))
1737 /* not FP capable, send as non-FP */
1738 goto NonFastPath;
1739
1740 /* get RAID_Context pointer */
1741 pRAID_Context = &io_request->RaidContext;
1742
1743 /* set RAID context values */
1744 pRAID_Context->regLockFlags = REGION_TYPE_SHARED_READ;
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301745 pRAID_Context->timeoutValue = cpu_to_le16(raid->fpIoTimeoutForLd);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301746 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
adam radford21c9e162013-09-06 15:27:14 -07001747 pRAID_Context->regLockRowLBA = 0;
1748 pRAID_Context->regLockLength = 0;
1749 pRAID_Context->configSeqNum = raid->seqNum;
1750
1751 /* get the DevHandle for the PD (since this is
1752 fpNonRWCapable, this is a single disk RAID0) */
1753 span = physArm = 0;
1754 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
1755 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
1756 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
1757
1758 /* build request descriptor */
1759 cmd->request_desc->SCSIIO.RequestFlags =
1760 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1761 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1762 cmd->request_desc->SCSIIO.DevHandle = devHandle;
1763
1764 /* populate the LUN field */
1765 memcpy(io_request->LUN, raid->LUN, 8);
1766
1767 /* build the raidScsiIO structure */
1768 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1769 io_request->DevHandle = devHandle;
1770
1771 return;
1772
1773NonFastPath:
adam radford9c915a82010-12-21 13:34:31 -08001774 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301775 io_request->DevHandle = cpu_to_le16(device_id);
adam radford9c915a82010-12-21 13:34:31 -08001776 cmd->request_desc->SCSIIO.RequestFlags =
1777 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1778 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1779 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301780 io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001781 int_to_scsilun(scmd->device->lun, (struct scsi_lun *)io_request->LUN);
adam radford9c915a82010-12-21 13:34:31 -08001782}
1783
1784/**
1785 * megasas_build_io_fusion - Prepares IOs to devices
1786 * @instance: Adapter soft state
1787 * @scp: SCSI command
1788 * @cmd: Command to be prepared
1789 *
1790 * Invokes helper functions to prepare request frames
1791 * and sets flags appropriate for IO/Non-IO cmd
1792 */
1793int
1794megasas_build_io_fusion(struct megasas_instance *instance,
1795 struct scsi_cmnd *scp,
1796 struct megasas_cmd_fusion *cmd)
1797{
1798 u32 device_id, sge_count;
1799 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1800
1801 device_id = MEGASAS_DEV_INDEX(instance, scp);
1802
1803 /* Zero out some fields so they don't get reused */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001804 memset(io_request->LUN, 0x0, 8);
adam radford9c915a82010-12-21 13:34:31 -08001805 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1806 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1807 io_request->EEDPFlags = 0;
1808 io_request->Control = 0;
1809 io_request->EEDPBlockSize = 0;
adam radford36807e62011-10-08 18:15:06 -07001810 io_request->ChainOffset = 0;
adam radford9c915a82010-12-21 13:34:31 -08001811 io_request->RaidContext.RAIDFlags = 0;
adam radford36807e62011-10-08 18:15:06 -07001812 io_request->RaidContext.Type = 0;
1813 io_request->RaidContext.nseg = 0;
adam radford9c915a82010-12-21 13:34:31 -08001814
1815 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1816 /*
1817 * Just the CDB length,rest of the Flags are zero
1818 * This will be modified for FP in build_ldio_fusion
1819 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301820 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
adam radford9c915a82010-12-21 13:34:31 -08001821
Sumit.Saxena@avagotech.com7497cde2015-01-05 20:06:03 +05301822 if (megasas_cmd_type(scp) == READ_WRITE_LDIO)
adam radford9c915a82010-12-21 13:34:31 -08001823 megasas_build_ldio_fusion(instance, scp, cmd);
1824 else
1825 megasas_build_dcdb_fusion(instance, scp, cmd);
1826
1827 /*
1828 * Construct SGL
1829 */
1830
1831 sge_count =
1832 megasas_make_sgl_fusion(instance, scp,
1833 (struct MPI25_IEEE_SGE_CHAIN64 *)
1834 &io_request->SGL, cmd);
1835
1836 if (sge_count > instance->max_num_sge) {
1837 printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1838 "max (0x%x) allowed\n", sge_count,
1839 instance->max_num_sge);
1840 return 1;
1841 }
1842
1843 io_request->RaidContext.numSGE = sge_count;
1844
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301845 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
adam radford9c915a82010-12-21 13:34:31 -08001846
1847 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301848 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
adam radford9c915a82010-12-21 13:34:31 -08001849 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301850 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
adam radford9c915a82010-12-21 13:34:31 -08001851
1852 io_request->SGLOffset0 =
1853 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1854
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301855 io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08001856 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1857
1858 cmd->scmd = scp;
1859 scp->SCp.ptr = (char *)cmd;
1860
1861 return 0;
1862}
1863
1864union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1865megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1866{
1867 u8 *p;
1868 struct fusion_context *fusion;
1869
1870 if (index >= instance->max_fw_cmds) {
1871 printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
adam radford229fe472014-03-10 02:51:56 -07001872 "descriptor for scsi%d\n", index,
1873 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08001874 return NULL;
1875 }
1876 fusion = instance->ctrl_context;
1877 p = fusion->req_frames_desc
1878 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1879
1880 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1881}
1882
1883/**
1884 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1885 * issuing non IOCTL cmd
1886 * @instance: Adapter soft state
1887 * @scmd: pointer to scsi cmd from OS
1888 */
1889static u32
1890megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1891 struct scsi_cmnd *scmd)
1892{
1893 struct megasas_cmd_fusion *cmd;
1894 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1895 u32 index;
1896 struct fusion_context *fusion;
1897
1898 fusion = instance->ctrl_context;
1899
1900 cmd = megasas_get_cmd_fusion(instance);
1901 if (!cmd)
1902 return SCSI_MLQUEUE_HOST_BUSY;
1903
1904 index = cmd->index;
1905
1906 req_desc = megasas_get_request_descriptor(instance, index-1);
1907 if (!req_desc)
1908 return 1;
1909
1910 req_desc->Words = 0;
1911 cmd->request_desc = req_desc;
adam radford9c915a82010-12-21 13:34:31 -08001912
1913 if (megasas_build_io_fusion(instance, scmd, cmd)) {
1914 megasas_return_cmd_fusion(instance, cmd);
1915 printk(KERN_ERR "megasas: Error building command.\n");
1916 cmd->request_desc = NULL;
1917 return 1;
1918 }
1919
1920 req_desc = cmd->request_desc;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301921 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08001922
1923 if (cmd->io_request->ChainOffset != 0 &&
1924 cmd->io_request->ChainOffset != 0xF)
1925 printk(KERN_ERR "megasas: The chain offset value is not "
1926 "correct : %x\n", cmd->io_request->ChainOffset);
1927
1928 /*
1929 * Issue the command to the FW
1930 */
1931 atomic_inc(&instance->fw_outstanding);
1932
1933 instance->instancet->fire_cmd(instance,
1934 req_desc->u.low, req_desc->u.high,
1935 instance->reg_set);
1936
1937 return 0;
1938}
1939
1940/**
1941 * complete_cmd_fusion - Completes command
1942 * @instance: Adapter soft state
1943 * Completes all commands that is in reply descriptor queue
1944 */
1945int
adam radfordc8e858f2011-10-08 18:15:13 -07001946complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
adam radford9c915a82010-12-21 13:34:31 -08001947{
1948 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1949 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1950 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1951 struct fusion_context *fusion;
1952 struct megasas_cmd *cmd_mfi;
1953 struct megasas_cmd_fusion *cmd_fusion;
1954 u16 smid, num_completed;
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05301955 u8 reply_descript_type;
adam radford9c915a82010-12-21 13:34:31 -08001956 u32 status, extStatus, device_id;
1957 union desc_value d_val;
1958 struct LD_LOAD_BALANCE_INFO *lbinfo;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05301959 int threshold_reply_count = 0;
adam radford9c915a82010-12-21 13:34:31 -08001960
1961 fusion = instance->ctrl_context;
1962
1963 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1964 return IRQ_HANDLED;
1965
1966 desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -07001967 desc += ((MSIxIndex * fusion->reply_alloc_sz)/
1968 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
1969 fusion->last_reply_idx[MSIxIndex];
adam radford9c915a82010-12-21 13:34:31 -08001970
1971 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1972
1973 d_val.word = desc->Words;
1974
1975 reply_descript_type = reply_desc->ReplyFlags &
1976 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1977
1978 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1979 return IRQ_NONE;
1980
adam radford9c915a82010-12-21 13:34:31 -08001981 num_completed = 0;
1982
1983 while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301984 smid = le16_to_cpu(reply_desc->SMID);
adam radford9c915a82010-12-21 13:34:31 -08001985
1986 cmd_fusion = fusion->cmd_list[smid - 1];
1987
1988 scsi_io_req =
1989 (struct MPI2_RAID_SCSI_IO_REQUEST *)
1990 cmd_fusion->io_request;
1991
1992 if (cmd_fusion->scmd)
1993 cmd_fusion->scmd->SCp.ptr = NULL;
1994
1995 status = scsi_io_req->RaidContext.status;
1996 extStatus = scsi_io_req->RaidContext.exStatus;
1997
1998 switch (scsi_io_req->Function) {
1999 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
2000 /* Update load balancing info */
2001 device_id = MEGASAS_DEV_INDEX(instance,
2002 cmd_fusion->scmd);
2003 lbinfo = &fusion->load_balance_info[device_id];
2004 if (cmd_fusion->scmd->SCp.Status &
2005 MEGASAS_LOAD_BALANCE_FLAG) {
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05302006 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
adam radford9c915a82010-12-21 13:34:31 -08002007 cmd_fusion->scmd->SCp.Status &=
2008 ~MEGASAS_LOAD_BALANCE_FLAG;
2009 }
2010 if (reply_descript_type ==
2011 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
2012 if (megasas_dbg_lvl == 5)
2013 printk(KERN_ERR "\nmegasas: FAST Path "
2014 "IO Success\n");
2015 }
2016 /* Fall thru and complete IO */
2017 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
2018 /* Map the FW Cmd Status */
2019 map_cmd_status(cmd_fusion, status, extStatus);
2020 scsi_dma_unmap(cmd_fusion->scmd);
2021 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2022 scsi_io_req->RaidContext.status = 0;
2023 scsi_io_req->RaidContext.exStatus = 0;
2024 megasas_return_cmd_fusion(instance, cmd_fusion);
2025 atomic_dec(&instance->fw_outstanding);
2026
2027 break;
2028 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
2029 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302030
2031 if (!cmd_mfi->mpt_pthr_cmd_blocked) {
2032 if (megasas_dbg_lvl == 5)
2033 dev_info(&instance->pdev->dev,
2034 "freeing mfi/mpt pass-through "
2035 "from %s %d\n",
2036 __func__, __LINE__);
2037 megasas_return_mfi_mpt_pthr(instance, cmd_mfi,
2038 cmd_fusion);
2039 }
2040
adam radford9c915a82010-12-21 13:34:31 -08002041 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2042 cmd_fusion->flags = 0;
adam radford9c915a82010-12-21 13:34:31 -08002043 break;
2044 }
2045
adam radfordc8e858f2011-10-08 18:15:13 -07002046 fusion->last_reply_idx[MSIxIndex]++;
2047 if (fusion->last_reply_idx[MSIxIndex] >=
2048 fusion->reply_q_depth)
2049 fusion->last_reply_idx[MSIxIndex] = 0;
adam radford9c915a82010-12-21 13:34:31 -08002050
2051 desc->Words = ULLONG_MAX;
2052 num_completed++;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05302053 threshold_reply_count++;
adam radford9c915a82010-12-21 13:34:31 -08002054
2055 /* Get the next reply descriptor */
adam radfordc8e858f2011-10-08 18:15:13 -07002056 if (!fusion->last_reply_idx[MSIxIndex])
2057 desc = fusion->reply_frames_desc +
2058 ((MSIxIndex * fusion->reply_alloc_sz)/
2059 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
adam radford9c915a82010-12-21 13:34:31 -08002060 else
2061 desc++;
2062
2063 reply_desc =
2064 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
2065
2066 d_val.word = desc->Words;
2067
2068 reply_descript_type = reply_desc->ReplyFlags &
2069 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
2070
2071 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
2072 break;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05302073 /*
2074 * Write to reply post host index register after completing threshold
2075 * number of reply counts and still there are more replies in reply queue
2076 * pending to be completed
2077 */
2078 if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
2079 if ((instance->pdev->device ==
2080 PCI_DEVICE_ID_LSI_INVADER) ||
2081 (instance->pdev->device ==
2082 PCI_DEVICE_ID_LSI_FURY))
2083 writel(((MSIxIndex & 0x7) << 24) |
2084 fusion->last_reply_idx[MSIxIndex],
2085 instance->reply_post_host_index_addr[MSIxIndex/8]);
2086 else
2087 writel((MSIxIndex << 24) |
2088 fusion->last_reply_idx[MSIxIndex],
2089 instance->reply_post_host_index_addr[0]);
2090 threshold_reply_count = 0;
2091 }
adam radford9c915a82010-12-21 13:34:31 -08002092 }
2093
2094 if (!num_completed)
2095 return IRQ_NONE;
2096
2097 wmb();
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302098 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2099 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
2100 writel(((MSIxIndex & 0x7) << 24) |
2101 fusion->last_reply_idx[MSIxIndex],
2102 instance->reply_post_host_index_addr[MSIxIndex/8]);
2103 else
2104 writel((MSIxIndex << 24) |
2105 fusion->last_reply_idx[MSIxIndex],
2106 instance->reply_post_host_index_addr[0]);
adam radford53ef2bb2011-02-24 20:56:05 -08002107 megasas_check_and_restore_queue_depth(instance);
adam radford9c915a82010-12-21 13:34:31 -08002108 return IRQ_HANDLED;
2109}
2110
2111/**
2112 * megasas_complete_cmd_dpc_fusion - Completes command
2113 * @instance: Adapter soft state
2114 *
2115 * Tasklet to complete cmds
2116 */
2117void
2118megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
2119{
2120 struct megasas_instance *instance =
2121 (struct megasas_instance *)instance_addr;
2122 unsigned long flags;
adam radfordc8e858f2011-10-08 18:15:13 -07002123 u32 count, MSIxIndex;
2124
2125 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
adam radford9c915a82010-12-21 13:34:31 -08002126
2127 /* If we have already declared adapter dead, donot complete cmds */
2128 spin_lock_irqsave(&instance->hba_lock, flags);
2129 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2130 spin_unlock_irqrestore(&instance->hba_lock, flags);
2131 return;
2132 }
2133 spin_unlock_irqrestore(&instance->hba_lock, flags);
2134
adam radfordc8e858f2011-10-08 18:15:13 -07002135 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
2136 complete_cmd_fusion(instance, MSIxIndex);
adam radford9c915a82010-12-21 13:34:31 -08002137}
2138
2139/**
2140 * megasas_isr_fusion - isr entry point
2141 */
2142irqreturn_t megasas_isr_fusion(int irq, void *devp)
2143{
adam radfordc8e858f2011-10-08 18:15:13 -07002144 struct megasas_irq_context *irq_context = devp;
2145 struct megasas_instance *instance = irq_context->instance;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302146 u32 mfiStatus, fw_state, dma_state;
adam radford9c915a82010-12-21 13:34:31 -08002147
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302148 if (instance->mask_interrupts)
2149 return IRQ_NONE;
2150
adam radfordc8e858f2011-10-08 18:15:13 -07002151 if (!instance->msix_vectors) {
adam radford9c915a82010-12-21 13:34:31 -08002152 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
2153 if (!mfiStatus)
2154 return IRQ_NONE;
2155 }
2156
2157 /* If we are resetting, bail */
adam radford6497b242011-10-08 18:14:50 -07002158 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
2159 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08002160 return IRQ_HANDLED;
adam radford6497b242011-10-08 18:14:50 -07002161 }
adam radford9c915a82010-12-21 13:34:31 -08002162
adam radfordc8e858f2011-10-08 18:15:13 -07002163 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
adam radford6497b242011-10-08 18:14:50 -07002164 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08002165 /* If we didn't complete any commands, check for FW fault */
2166 fw_state = instance->instancet->read_fw_status_reg(
2167 instance->reg_set) & MFI_STATE_MASK;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302168 dma_state = instance->instancet->read_fw_status_reg
2169 (instance->reg_set) & MFI_STATE_DMADONE;
2170 if (instance->crash_dump_drv_support &&
2171 instance->crash_dump_app_support) {
2172 /* Start collecting crash, if DMA bit is done */
2173 if ((fw_state == MFI_STATE_FAULT) && dma_state)
2174 schedule_work(&instance->crash_init);
2175 else if (fw_state == MFI_STATE_FAULT)
2176 schedule_work(&instance->work_init);
2177 } else if (fw_state == MFI_STATE_FAULT) {
adam radford229fe472014-03-10 02:51:56 -07002178 printk(KERN_WARNING "megaraid_sas: Iop2SysDoorbellInt"
2179 "for scsi%d\n", instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002180 schedule_work(&instance->work_init);
adam radford229fe472014-03-10 02:51:56 -07002181 }
adam radford9c915a82010-12-21 13:34:31 -08002182 }
2183
2184 return IRQ_HANDLED;
2185}
2186
2187/**
2188 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
2189 * @instance: Adapter soft state
2190 * mfi_cmd: megasas_cmd pointer
2191 *
2192 */
2193u8
2194build_mpt_mfi_pass_thru(struct megasas_instance *instance,
2195 struct megasas_cmd *mfi_cmd)
2196{
2197 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
2198 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
2199 struct megasas_cmd_fusion *cmd;
2200 struct fusion_context *fusion;
2201 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302202 u32 opcode;
adam radford9c915a82010-12-21 13:34:31 -08002203
2204 cmd = megasas_get_cmd_fusion(instance);
2205 if (!cmd)
2206 return 1;
2207
2208 /* Save the smid. To be used for returning the cmd */
2209 mfi_cmd->context.smid = cmd->index;
adam radford9c915a82010-12-21 13:34:31 -08002210 cmd->sync_cmd_idx = mfi_cmd->index;
2211
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302212 /* Set this only for Blocked commands */
2213 opcode = le32_to_cpu(mfi_cmd->frame->dcmd.opcode);
2214 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
2215 && (mfi_cmd->frame->dcmd.mbox.b[1] == 1))
2216 mfi_cmd->is_wait_event = 1;
2217
2218 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
2219 mfi_cmd->is_wait_event = 1;
2220
2221 if (mfi_cmd->is_wait_event)
2222 mfi_cmd->mpt_pthr_cmd_blocked = cmd;
2223
adam radford9c915a82010-12-21 13:34:31 -08002224 /*
2225 * For cmds where the flag is set, store the flag and check
2226 * on completion. For cmds with this flag, don't call
2227 * megasas_complete_cmd
2228 */
2229
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05302230 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
adam radford9c915a82010-12-21 13:34:31 -08002231 cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
2232
2233 fusion = instance->ctrl_context;
2234 io_req = cmd->io_request;
adam radford36807e62011-10-08 18:15:06 -07002235
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05302236 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2237 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07002238 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
2239 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
2240 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2241 sgl_ptr_end->Flags = 0;
2242 }
2243
adam radford9c915a82010-12-21 13:34:31 -08002244 mpi25_ieee_chain =
2245 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
2246
2247 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
2248 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
2249 SGL) / 4;
2250 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
2251
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302252 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08002253
2254 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2255 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
2256
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302257 mpi25_ieee_chain->Length = cpu_to_le32(MEGASAS_MAX_SZ_CHAIN_FRAME);
adam radford9c915a82010-12-21 13:34:31 -08002258
2259 return 0;
2260}
2261
2262/**
2263 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
2264 * @instance: Adapter soft state
2265 * @cmd: mfi cmd to build
2266 *
2267 */
2268union MEGASAS_REQUEST_DESCRIPTOR_UNION *
2269build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
2270{
2271 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2272 u16 index;
2273
2274 if (build_mpt_mfi_pass_thru(instance, cmd)) {
2275 printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
2276 return NULL;
2277 }
2278
2279 index = cmd->context.smid;
2280
2281 req_desc = megasas_get_request_descriptor(instance, index - 1);
2282
2283 if (!req_desc)
2284 return NULL;
2285
2286 req_desc->Words = 0;
2287 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2288 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2289
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302290 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08002291
2292 return req_desc;
2293}
2294
2295/**
2296 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
2297 * @instance: Adapter soft state
2298 * @cmd: mfi cmd pointer
2299 *
2300 */
2301void
2302megasas_issue_dcmd_fusion(struct megasas_instance *instance,
2303 struct megasas_cmd *cmd)
2304{
2305 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
adam radford9c915a82010-12-21 13:34:31 -08002306
2307 req_desc = build_mpt_cmd(instance, cmd);
2308 if (!req_desc) {
2309 printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
2310 return;
2311 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302312 atomic_set(&cmd->mfi_mpt_pthr, MFI_MPT_ATTACHED);
adam radford9c915a82010-12-21 13:34:31 -08002313 instance->instancet->fire_cmd(instance, req_desc->u.low,
2314 req_desc->u.high, instance->reg_set);
2315}
2316
2317/**
2318 * megasas_release_fusion - Reverses the FW initialization
2319 * @intance: Adapter soft state
2320 */
2321void
2322megasas_release_fusion(struct megasas_instance *instance)
2323{
2324 megasas_free_cmds(instance);
2325 megasas_free_cmds_fusion(instance);
2326
2327 iounmap(instance->reg_set);
2328
2329 pci_release_selected_regions(instance->pdev, instance->bar);
2330}
2331
2332/**
2333 * megasas_read_fw_status_reg_fusion - returns the current FW status value
2334 * @regs: MFI register set
2335 */
2336static u32
2337megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
2338{
2339 return readl(&(regs)->outbound_scratch_pad);
2340}
2341
2342/**
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302343 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
2344 * @instance: Controller's soft instance
2345 * return: Number of allocated host crash buffers
2346 */
2347static void
2348megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
2349{
2350 unsigned int i;
2351
2352 instance->crash_buf_pages = get_order(CRASH_DMA_BUF_SIZE);
2353 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
2354 instance->crash_buf[i] = (void *)__get_free_pages(GFP_KERNEL,
2355 instance->crash_buf_pages);
2356 if (!instance->crash_buf[i]) {
2357 dev_info(&instance->pdev->dev, "Firmware crash dump "
2358 "memory allocation failed at index %d\n", i);
2359 break;
2360 }
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05302361 memset(instance->crash_buf[i], 0,
2362 ((1 << PAGE_SHIFT) << instance->crash_buf_pages));
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302363 }
2364 instance->drv_buf_alloc = i;
2365}
2366
2367/**
2368 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
2369 * @instance: Controller's soft instance
2370 */
2371void
2372megasas_free_host_crash_buffer(struct megasas_instance *instance)
2373{
2374 unsigned int i
2375;
2376 for (i = 0; i < instance->drv_buf_alloc; i++) {
2377 if (instance->crash_buf[i])
2378 free_pages((ulong)instance->crash_buf[i],
2379 instance->crash_buf_pages);
2380 }
2381 instance->drv_buf_index = 0;
2382 instance->drv_buf_alloc = 0;
2383 instance->fw_crash_state = UNAVAILABLE;
2384 instance->fw_crash_buffer_size = 0;
2385}
2386
2387/**
adam radford9c915a82010-12-21 13:34:31 -08002388 * megasas_adp_reset_fusion - For controller reset
2389 * @regs: MFI register set
2390 */
2391static int
2392megasas_adp_reset_fusion(struct megasas_instance *instance,
2393 struct megasas_register_set __iomem *regs)
2394{
2395 return 0;
2396}
2397
2398/**
2399 * megasas_check_reset_fusion - For controller reset check
2400 * @regs: MFI register set
2401 */
2402static int
2403megasas_check_reset_fusion(struct megasas_instance *instance,
2404 struct megasas_register_set __iomem *regs)
2405{
2406 return 0;
2407}
2408
2409/* This function waits for outstanding commands on fusion to complete */
adam radford229fe472014-03-10 02:51:56 -07002410int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
2411 int iotimeout, int *convert)
adam radford9c915a82010-12-21 13:34:31 -08002412{
adam radford229fe472014-03-10 02:51:56 -07002413 int i, outstanding, retval = 0, hb_seconds_missed = 0;
adam radfordc007b8b2012-07-17 18:20:24 -07002414 u32 fw_state;
adam radford9c915a82010-12-21 13:34:31 -08002415
adam radfordc007b8b2012-07-17 18:20:24 -07002416 for (i = 0; i < resetwaittime; i++) {
adam radford9c915a82010-12-21 13:34:31 -08002417 /* Check if firmware is in fault state */
2418 fw_state = instance->instancet->read_fw_status_reg(
2419 instance->reg_set) & MFI_STATE_MASK;
2420 if (fw_state == MFI_STATE_FAULT) {
2421 printk(KERN_WARNING "megasas: Found FW in FAULT state,"
adam radford229fe472014-03-10 02:51:56 -07002422 " will reset adapter scsi%d.\n",
2423 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002424 retval = 1;
2425 goto out;
2426 }
adam radford229fe472014-03-10 02:51:56 -07002427 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
2428 if (instance->requestorId && !iotimeout) {
2429 retval = 1;
2430 goto out;
2431 }
2432
2433 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
2434 if (instance->requestorId && iotimeout) {
2435 if (instance->hb_host_mem->HB.fwCounter !=
2436 instance->hb_host_mem->HB.driverCounter) {
2437 instance->hb_host_mem->HB.driverCounter =
2438 instance->hb_host_mem->HB.fwCounter;
2439 hb_seconds_missed = 0;
2440 } else {
2441 hb_seconds_missed++;
2442 if (hb_seconds_missed ==
2443 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
2444 printk(KERN_WARNING "megasas: SR-IOV:"
2445 " Heartbeat never completed "
2446 " while polling during I/O "
2447 " timeout handling for "
2448 "scsi%d.\n",
2449 instance->host->host_no);
2450 *convert = 1;
2451 retval = 1;
2452 goto out;
2453 }
2454 }
2455 }
adam radford9c915a82010-12-21 13:34:31 -08002456
2457 outstanding = atomic_read(&instance->fw_outstanding);
2458 if (!outstanding)
2459 goto out;
2460
2461 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2462 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
adam radford229fe472014-03-10 02:51:56 -07002463 "commands to complete for scsi%d\n", i,
2464 outstanding, instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002465 megasas_complete_cmd_dpc_fusion(
2466 (unsigned long)instance);
2467 }
2468 msleep(1000);
2469 }
2470
2471 if (atomic_read(&instance->fw_outstanding)) {
2472 printk("megaraid_sas: pending commands remain after waiting, "
adam radford229fe472014-03-10 02:51:56 -07002473 "will reset adapter scsi%d.\n",
2474 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002475 retval = 1;
2476 }
2477out:
2478 return retval;
2479}
2480
2481void megasas_reset_reply_desc(struct megasas_instance *instance)
2482{
adam radfordc8e858f2011-10-08 18:15:13 -07002483 int i, count;
adam radford9c915a82010-12-21 13:34:31 -08002484 struct fusion_context *fusion;
2485 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2486
2487 fusion = instance->ctrl_context;
adam radfordc8e858f2011-10-08 18:15:13 -07002488 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2489 for (i = 0 ; i < count ; i++)
2490 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -08002491 reply_desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -07002492 for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
adam radford9c915a82010-12-21 13:34:31 -08002493 reply_desc->Words = ULLONG_MAX;
2494}
2495
adam radford229fe472014-03-10 02:51:56 -07002496/* Check for a second path that is currently UP */
2497int megasas_check_mpio_paths(struct megasas_instance *instance,
2498 struct scsi_cmnd *scmd)
adam radford9c915a82010-12-21 13:34:31 -08002499{
adam radford229fe472014-03-10 02:51:56 -07002500 int i, j, retval = (DID_RESET << 16);
2501
2502 if (instance->mpio && instance->requestorId) {
2503 for (i = 0 ; i < MAX_MGMT_ADAPTERS ; i++)
2504 for (j = 0 ; j < MAX_LOGICAL_DRIVES; j++)
2505 if (megasas_mgmt_info.instance[i] &&
2506 (megasas_mgmt_info.instance[i] != instance) &&
2507 megasas_mgmt_info.instance[i]->mpio &&
2508 megasas_mgmt_info.instance[i]->requestorId
2509 &&
2510 (megasas_mgmt_info.instance[i]->ld_ids[j]
2511 == scmd->device->id)) {
2512 retval = (DID_NO_CONNECT << 16);
2513 goto out;
2514 }
2515 }
2516out:
2517 return retval;
2518}
2519
2520/* Core fusion reset function */
2521int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout)
2522{
2523 int retval = SUCCESS, i, j, retry = 0, convert = 0;
adam radford9c915a82010-12-21 13:34:31 -08002524 struct megasas_instance *instance;
2525 struct megasas_cmd_fusion *cmd_fusion;
2526 struct fusion_context *fusion;
2527 struct megasas_cmd *cmd_mfi;
2528 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
adam radford7e70e732011-05-11 18:34:08 -07002529 u32 host_diag, abs_state, status_reg, reset_adapter;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302530 u32 io_timeout_in_crash_mode = 0;
adam radford9c915a82010-12-21 13:34:31 -08002531
2532 instance = (struct megasas_instance *)shost->hostdata;
2533 fusion = instance->ctrl_context;
2534
adam radford229fe472014-03-10 02:51:56 -07002535 mutex_lock(&instance->reset_mutex);
2536
adam radford9c915a82010-12-21 13:34:31 -08002537 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2538 printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
adam radford229fe472014-03-10 02:51:56 -07002539 "returning FAILED for scsi%d.\n",
2540 instance->host->host_no);
Adam Radforda2fbcbc2014-07-09 15:17:54 -07002541 mutex_unlock(&instance->reset_mutex);
adam radfordd4a759a2011-10-08 18:14:39 -07002542 return FAILED;
adam radford9c915a82010-12-21 13:34:31 -08002543 }
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302544 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
2545 abs_state = status_reg & MFI_STATE_MASK;
2546
2547 /* IO timeout detected, forcibly put FW in FAULT state */
2548 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
2549 instance->crash_dump_app_support && iotimeout) {
2550 dev_info(&instance->pdev->dev, "IO timeout is detected, "
2551 "forcibly FAULT Firmware\n");
2552 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2553 status_reg = readl(&instance->reg_set->doorbell);
2554 writel(status_reg | MFI_STATE_FORCE_OCR,
2555 &instance->reg_set->doorbell);
2556 readl(&instance->reg_set->doorbell);
2557 mutex_unlock(&instance->reset_mutex);
2558 do {
2559 ssleep(3);
2560 io_timeout_in_crash_mode++;
2561 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
2562 "seconds for crash dump collection and OCR "
2563 "to be done\n", (io_timeout_in_crash_mode * 3));
2564 } while ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
2565 (io_timeout_in_crash_mode < 80));
2566
2567 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
2568 dev_info(&instance->pdev->dev, "OCR done for IO "
2569 "timeout case\n");
2570 retval = SUCCESS;
2571 } else {
2572 dev_info(&instance->pdev->dev, "Controller is not "
2573 "operational after 240 seconds wait for IO "
2574 "timeout case in FW crash dump mode\n do "
2575 "OCR/kill adapter\n");
2576 retval = megasas_reset_fusion(shost, 0);
2577 }
2578 return retval;
2579 }
adam radford9c915a82010-12-21 13:34:31 -08002580
adam radford229fe472014-03-10 02:51:56 -07002581 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
2582 del_timer_sync(&instance->sriov_heartbeat_timer);
adam radford7e70e732011-05-11 18:34:08 -07002583 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
adam radford229fe472014-03-10 02:51:56 -07002584 instance->adprecovery = MEGASAS_ADPRESET_SM_POLLING;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302585 instance->instancet->disable_intr(instance);
adam radford7e70e732011-05-11 18:34:08 -07002586 msleep(1000);
2587
adam radford9c915a82010-12-21 13:34:31 -08002588 /* First try waiting for commands to complete */
adam radford229fe472014-03-10 02:51:56 -07002589 if (megasas_wait_for_outstanding_fusion(instance, iotimeout,
2590 &convert)) {
2591 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
adam radford9c915a82010-12-21 13:34:31 -08002592 printk(KERN_WARNING "megaraid_sas: resetting fusion "
adam radford229fe472014-03-10 02:51:56 -07002593 "adapter scsi%d.\n", instance->host->host_no);
2594 if (convert)
2595 iotimeout = 0;
2596
adam radford9c915a82010-12-21 13:34:31 -08002597 /* Now return commands back to the OS */
2598 for (i = 0 ; i < instance->max_fw_cmds; i++) {
2599 cmd_fusion = fusion->cmd_list[i];
2600 if (cmd_fusion->scmd) {
2601 scsi_dma_unmap(cmd_fusion->scmd);
adam radford229fe472014-03-10 02:51:56 -07002602 cmd_fusion->scmd->result =
2603 megasas_check_mpio_paths(instance,
2604 cmd_fusion->scmd);
adam radford9c915a82010-12-21 13:34:31 -08002605 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2606 megasas_return_cmd_fusion(instance, cmd_fusion);
2607 atomic_dec(&instance->fw_outstanding);
2608 }
2609 }
2610
adam radford7e70e732011-05-11 18:34:08 -07002611 status_reg = instance->instancet->read_fw_status_reg(
2612 instance->reg_set);
2613 abs_state = status_reg & MFI_STATE_MASK;
2614 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2615 if (instance->disableOnlineCtrlReset ||
2616 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
adam radford9c915a82010-12-21 13:34:31 -08002617 /* Reset not supported, kill adapter */
2618 printk(KERN_WARNING "megaraid_sas: Reset not supported"
adam radford229fe472014-03-10 02:51:56 -07002619 ", killing adapter scsi%d.\n",
2620 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002621 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07002622 instance->skip_heartbeat_timer_del = 1;
adam radford9c915a82010-12-21 13:34:31 -08002623 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2624 retval = FAILED;
2625 goto out;
2626 }
2627
adam radford229fe472014-03-10 02:51:56 -07002628 /* Let SR-IOV VF & PF sync up if there was a HB failure */
2629 if (instance->requestorId && !iotimeout) {
2630 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
2631 /* Look for a late HB update after VF settle time */
2632 if (abs_state == MFI_STATE_OPERATIONAL &&
2633 (instance->hb_host_mem->HB.fwCounter !=
2634 instance->hb_host_mem->HB.driverCounter)) {
2635 instance->hb_host_mem->HB.driverCounter =
2636 instance->hb_host_mem->HB.fwCounter;
2637 printk(KERN_WARNING "megasas: SR-IOV:"
2638 "Late FW heartbeat update for "
2639 "scsi%d.\n",
2640 instance->host->host_no);
2641 } else {
2642 /* In VF mode, first poll for FW ready */
2643 for (i = 0;
2644 i < (MEGASAS_RESET_WAIT_TIME * 1000);
2645 i += 20) {
2646 status_reg =
2647 instance->instancet->
2648 read_fw_status_reg(
2649 instance->reg_set);
2650 abs_state = status_reg &
2651 MFI_STATE_MASK;
2652 if (abs_state == MFI_STATE_READY) {
2653 printk(KERN_WARNING "megasas"
2654 ": SR-IOV: FW was found"
2655 "to be in ready state "
2656 "for scsi%d.\n",
2657 instance->host->host_no);
2658 break;
2659 }
2660 msleep(20);
2661 }
2662 if (abs_state != MFI_STATE_READY) {
2663 printk(KERN_WARNING "megasas: SR-IOV: "
2664 "FW not in ready state after %d"
2665 " seconds for scsi%d, status_reg = "
2666 "0x%x.\n",
2667 MEGASAS_RESET_WAIT_TIME,
2668 instance->host->host_no,
2669 status_reg);
2670 megaraid_sas_kill_hba(instance);
2671 instance->skip_heartbeat_timer_del = 1;
2672 instance->adprecovery =
2673 MEGASAS_HW_CRITICAL_ERROR;
2674 retval = FAILED;
2675 goto out;
2676 }
2677 }
2678 }
2679
adam radford9c915a82010-12-21 13:34:31 -08002680 /* Now try to reset the chip */
2681 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2682 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2683 &instance->reg_set->fusion_seq_offset);
2684 writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2685 &instance->reg_set->fusion_seq_offset);
2686 writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2687 &instance->reg_set->fusion_seq_offset);
2688 writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2689 &instance->reg_set->fusion_seq_offset);
2690 writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2691 &instance->reg_set->fusion_seq_offset);
2692 writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2693 &instance->reg_set->fusion_seq_offset);
2694 writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2695 &instance->reg_set->fusion_seq_offset);
2696
2697 /* Check that the diag write enable (DRWE) bit is on */
2698 host_diag = readl(&instance->reg_set->fusion_host_diag);
adam radford7e70e732011-05-11 18:34:08 -07002699 retry = 0;
adam radford9c915a82010-12-21 13:34:31 -08002700 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2701 msleep(100);
2702 host_diag =
2703 readl(&instance->reg_set->fusion_host_diag);
2704 if (retry++ == 100) {
2705 printk(KERN_WARNING "megaraid_sas: "
adam radford229fe472014-03-10 02:51:56 -07002706 "Host diag unlock failed! "
2707 "for scsi%d\n",
2708 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002709 break;
2710 }
2711 }
2712 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2713 continue;
2714
2715 /* Send chip reset command */
2716 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2717 &instance->reg_set->fusion_host_diag);
2718 msleep(3000);
2719
2720 /* Make sure reset adapter bit is cleared */
2721 host_diag = readl(&instance->reg_set->fusion_host_diag);
2722 retry = 0;
2723 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2724 msleep(100);
2725 host_diag =
2726 readl(&instance->reg_set->fusion_host_diag);
2727 if (retry++ == 1000) {
2728 printk(KERN_WARNING "megaraid_sas: "
2729 "Diag reset adapter never "
adam radford229fe472014-03-10 02:51:56 -07002730 "cleared for scsi%d!\n",
2731 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002732 break;
2733 }
2734 }
2735 if (host_diag & HOST_DIAG_RESET_ADAPTER)
2736 continue;
2737
2738 abs_state =
2739 instance->instancet->read_fw_status_reg(
adam radford7e70e732011-05-11 18:34:08 -07002740 instance->reg_set) & MFI_STATE_MASK;
adam radford9c915a82010-12-21 13:34:31 -08002741 retry = 0;
2742
2743 while ((abs_state <= MFI_STATE_FW_INIT) &&
2744 (retry++ < 1000)) {
2745 msleep(100);
2746 abs_state =
2747 instance->instancet->read_fw_status_reg(
adam radford7e70e732011-05-11 18:34:08 -07002748 instance->reg_set) & MFI_STATE_MASK;
adam radford9c915a82010-12-21 13:34:31 -08002749 }
2750 if (abs_state <= MFI_STATE_FW_INIT) {
2751 printk(KERN_WARNING "megaraid_sas: firmware "
2752 "state < MFI_STATE_FW_INIT, state = "
adam radford229fe472014-03-10 02:51:56 -07002753 "0x%x for scsi%d\n", abs_state,
2754 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002755 continue;
2756 }
2757
2758 /* Wait for FW to become ready */
adam radford058a8fa2011-10-08 18:14:27 -07002759 if (megasas_transition_to_ready(instance, 1)) {
adam radford9c915a82010-12-21 13:34:31 -08002760 printk(KERN_WARNING "megaraid_sas: Failed to "
adam radford229fe472014-03-10 02:51:56 -07002761 "transition controller to ready "
2762 "for scsi%d.\n",
2763 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002764 continue;
2765 }
2766
2767 megasas_reset_reply_desc(instance);
2768 if (megasas_ioc_init_fusion(instance)) {
2769 printk(KERN_WARNING "megaraid_sas: "
adam radford229fe472014-03-10 02:51:56 -07002770 "megasas_ioc_init_fusion() failed!"
2771 " for scsi%d\n",
2772 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002773 continue;
2774 }
2775
adam radford9c915a82010-12-21 13:34:31 -08002776 /* Re-fire management commands */
2777 for (j = 0 ; j < instance->max_fw_cmds; j++) {
2778 cmd_fusion = fusion->cmd_list[j];
2779 if (cmd_fusion->sync_cmd_idx !=
2780 (u32)ULONG_MAX) {
2781 cmd_mfi =
2782 instance->
2783 cmd_list[cmd_fusion->sync_cmd_idx];
2784 if (cmd_mfi->frame->dcmd.opcode ==
Sumit.Saxena@lsi.combe263742014-02-12 23:37:46 +05302785 cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO)) {
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302786 megasas_return_mfi_mpt_pthr(instance, cmd_mfi, cmd_fusion);
adam radford9c915a82010-12-21 13:34:31 -08002787 } else {
2788 req_desc =
2789 megasas_get_request_descriptor(
2790 instance,
2791 cmd_mfi->context.smid
2792 -1);
adam radford3d0c24c2014-03-10 02:51:45 -07002793 if (!req_desc) {
adam radford9c915a82010-12-21 13:34:31 -08002794 printk(KERN_WARNING
2795 "req_desc NULL"
adam radford229fe472014-03-10 02:51:56 -07002796 " for scsi%d\n",
2797 instance->host->host_no);
adam radford3d0c24c2014-03-10 02:51:45 -07002798 /* Return leaked MPT
2799 frame */
2800 megasas_return_cmd_fusion(instance, cmd_fusion);
2801 } else {
adam radford9c915a82010-12-21 13:34:31 -08002802 instance->instancet->
2803 fire_cmd(instance,
2804 req_desc->
2805 u.low,
2806 req_desc->
2807 u.high,
2808 instance->
2809 reg_set);
2810 }
2811 }
2812 }
2813 }
2814
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05302815 if (megasas_get_ctrl_info(instance)) {
2816 dev_info(&instance->pdev->dev,
2817 "Failed from %s %d\n",
2818 __func__, __LINE__);
2819 instance->adprecovery =
2820 MEGASAS_HW_CRITICAL_ERROR;
2821 megaraid_sas_kill_hba(instance);
2822 retval = FAILED;
2823 }
adam radford9c915a82010-12-21 13:34:31 -08002824 /* Reset load balance info */
2825 memset(fusion->load_balance_info, 0,
2826 sizeof(struct LD_LOAD_BALANCE_INFO)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302827 *MAX_LOGICAL_DRIVES_EXT);
adam radford9c915a82010-12-21 13:34:31 -08002828
2829 if (!megasas_get_map_info(instance))
2830 megasas_sync_map_info(instance);
2831
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05302832 clear_bit(MEGASAS_FUSION_IN_RESET,
2833 &instance->reset_flags);
2834 instance->instancet->enable_intr(instance);
2835 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2836
adam radford229fe472014-03-10 02:51:56 -07002837 /* Restart SR-IOV heartbeat */
2838 if (instance->requestorId) {
2839 if (!megasas_sriov_start_heartbeat(instance, 0))
2840 megasas_start_timer(instance,
2841 &instance->sriov_heartbeat_timer,
2842 megasas_sriov_heartbeat_handler,
2843 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2844 else
2845 instance->skip_heartbeat_timer_del = 1;
2846 }
2847
adam radford9c915a82010-12-21 13:34:31 -08002848 /* Adapter reset completed successfully */
2849 printk(KERN_WARNING "megaraid_sas: Reset "
adam radford229fe472014-03-10 02:51:56 -07002850 "successful for scsi%d.\n",
2851 instance->host->host_no);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302852
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05302853 if (instance->crash_dump_drv_support &&
2854 instance->crash_dump_app_support)
2855 megasas_set_crash_dump_params(instance,
2856 MR_CRASH_BUF_TURN_ON);
2857 else
2858 megasas_set_crash_dump_params(instance,
2859 MR_CRASH_BUF_TURN_OFF);
2860
adam radford9c915a82010-12-21 13:34:31 -08002861 retval = SUCCESS;
2862 goto out;
2863 }
2864 /* Reset failed, kill the adapter */
2865 printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
adam radford229fe472014-03-10 02:51:56 -07002866 "adapter scsi%d.\n", instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002867 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07002868 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05302869 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
adam radford9c915a82010-12-21 13:34:31 -08002870 retval = FAILED;
2871 } else {
adam radford229fe472014-03-10 02:51:56 -07002872 /* For VF: Restart HB timer if we didn't OCR */
2873 if (instance->requestorId) {
2874 megasas_start_timer(instance,
2875 &instance->sriov_heartbeat_timer,
2876 megasas_sriov_heartbeat_handler,
2877 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2878 }
adam radford3f0e58bc2011-10-08 18:14:59 -07002879 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302880 instance->instancet->enable_intr(instance);
adam radford9c915a82010-12-21 13:34:31 -08002881 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2882 }
2883out:
2884 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2885 mutex_unlock(&instance->reset_mutex);
2886 return retval;
2887}
2888
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302889/* Fusion Crash dump collection work queue */
2890void megasas_fusion_crash_dump_wq(struct work_struct *work)
2891{
2892 struct megasas_instance *instance =
2893 container_of(work, struct megasas_instance, crash_init);
2894 u32 status_reg;
2895 u8 partial_copy = 0;
2896
2897
2898 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
2899
2900 /*
2901 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
2902 * to host crash buffers
2903 */
2904 if (instance->drv_buf_index == 0) {
2905 /* Buffer is already allocated for old Crash dump.
2906 * Do OCR and do not wait for crash dump collection
2907 */
2908 if (instance->drv_buf_alloc) {
2909 dev_info(&instance->pdev->dev, "earlier crash dump is "
2910 "not yet copied by application, ignoring this "
2911 "crash dump and initiating OCR\n");
2912 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
2913 writel(status_reg,
2914 &instance->reg_set->outbound_scratch_pad);
2915 readl(&instance->reg_set->outbound_scratch_pad);
2916 return;
2917 }
2918 megasas_alloc_host_crash_buffer(instance);
2919 dev_info(&instance->pdev->dev, "Number of host crash buffers "
2920 "allocated: %d\n", instance->drv_buf_alloc);
2921 }
2922
2923 /*
2924 * Driver has allocated max buffers, which can be allocated
2925 * and FW has more crash dump data, then driver will
2926 * ignore the data.
2927 */
2928 if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
2929 dev_info(&instance->pdev->dev, "Driver is done copying "
2930 "the buffer: %d\n", instance->drv_buf_alloc);
2931 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
2932 partial_copy = 1;
2933 } else {
2934 memcpy(instance->crash_buf[instance->drv_buf_index],
2935 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
2936 instance->drv_buf_index++;
2937 status_reg &= ~MFI_STATE_DMADONE;
2938 }
2939
2940 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
2941 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
2942 "of copied buffers: %d\n", instance->drv_buf_index);
2943 instance->fw_crash_buffer_size = instance->drv_buf_index;
2944 instance->fw_crash_state = AVAILABLE;
2945 instance->drv_buf_index = 0;
2946 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
2947 readl(&instance->reg_set->outbound_scratch_pad);
2948 if (!partial_copy)
2949 megasas_reset_fusion(instance->host, 0);
2950 } else {
2951 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
2952 readl(&instance->reg_set->outbound_scratch_pad);
2953 }
2954}
2955
2956
adam radford9c915a82010-12-21 13:34:31 -08002957/* Fusion OCR work queue */
2958void megasas_fusion_ocr_wq(struct work_struct *work)
2959{
2960 struct megasas_instance *instance =
2961 container_of(work, struct megasas_instance, work_init);
2962
adam radford229fe472014-03-10 02:51:56 -07002963 megasas_reset_fusion(instance->host, 0);
adam radford9c915a82010-12-21 13:34:31 -08002964}
2965
2966struct megasas_instance_template megasas_instance_template_fusion = {
2967 .fire_cmd = megasas_fire_cmd_fusion,
2968 .enable_intr = megasas_enable_intr_fusion,
2969 .disable_intr = megasas_disable_intr_fusion,
2970 .clear_intr = megasas_clear_intr_fusion,
2971 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2972 .adp_reset = megasas_adp_reset_fusion,
2973 .check_reset = megasas_check_reset_fusion,
2974 .service_isr = megasas_isr_fusion,
2975 .tasklet = megasas_complete_cmd_dpc_fusion,
2976 .init_adapter = megasas_init_adapter_fusion,
2977 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2978 .issue_dcmd = megasas_issue_dcmd_fusion,
2979};