blob: b5362c19600029ef3cdb6aa2c0fd166d8e2ad76d [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);
66int megasas_is_ldio(struct scsi_cmnd *cmd);
67int
adam radford229fe472014-03-10 02:51:56 -070068wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
69 int seconds);
adam radford9c915a82010-12-21 13:34:31 -080070
71void
72megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
73int megasas_alloc_cmds(struct megasas_instance *instance);
74int
75megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
76int
77megasas_issue_polled(struct megasas_instance *instance,
78 struct megasas_cmd *cmd);
adam radford53ef2bb2011-02-24 20:56:05 -080079void
80megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
81
adam radford058a8fa2011-10-08 18:14:27 -070082int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
adam radford9c915a82010-12-21 13:34:31 -080083void megaraid_sas_kill_hba(struct megasas_instance *instance);
84
85extern u32 megasas_dbg_lvl;
adam radford229fe472014-03-10 02:51:56 -070086void megasas_sriov_heartbeat_handler(unsigned long instance_addr);
87int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
88 int initial);
89void megasas_start_timer(struct megasas_instance *instance,
90 struct timer_list *timer,
91 void *fn, unsigned long interval);
92extern struct megasas_mgmt_info megasas_mgmt_info;
adam radfordc007b8b2012-07-17 18:20:24 -070093extern int resetwaittime;
adam radford9c915a82010-12-21 13:34:31 -080094
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +053095
96
adam radford9c915a82010-12-21 13:34:31 -080097/**
98 * megasas_enable_intr_fusion - Enables interrupts
99 * @regs: MFI register set
100 */
101void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530102megasas_enable_intr_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800103{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530104 struct megasas_register_set __iomem *regs;
105 regs = instance->reg_set;
adam radford6497b242011-10-08 18:14:50 -0700106 /* For Thunderbolt/Invader also clear intr on enable */
107 writel(~0, &regs->outbound_intr_status);
108 readl(&regs->outbound_intr_status);
109
adam radford9c915a82010-12-21 13:34:31 -0800110 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
111
112 /* Dummy readl to force pci flush */
113 readl(&regs->outbound_intr_mask);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530114 instance->mask_interrupts = 0;
adam radford9c915a82010-12-21 13:34:31 -0800115}
116
117/**
118 * megasas_disable_intr_fusion - Disables interrupt
119 * @regs: MFI register set
120 */
121void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530122megasas_disable_intr_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800123{
124 u32 mask = 0xFFFFFFFF;
125 u32 status;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530126 struct megasas_register_set __iomem *regs;
127 regs = instance->reg_set;
128 instance->mask_interrupts = 1;
adam radford9c915a82010-12-21 13:34:31 -0800129
130 writel(mask, &regs->outbound_intr_mask);
131 /* Dummy readl to force pci flush */
132 status = readl(&regs->outbound_intr_mask);
133}
134
135int
136megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
137{
138 u32 status;
139 /*
140 * Check if it is our interrupt
141 */
142 status = readl(&regs->outbound_intr_status);
143
144 if (status & 1) {
145 writel(status, &regs->outbound_intr_status);
146 readl(&regs->outbound_intr_status);
147 return 1;
148 }
149 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
150 return 0;
151
adam radford9c915a82010-12-21 13:34:31 -0800152 return 1;
153}
154
155/**
156 * megasas_get_cmd_fusion - Get a command from the free pool
157 * @instance: Adapter soft state
158 *
159 * Returns a free command from the pool
160 */
161struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
162 *instance)
163{
164 unsigned long flags;
165 struct fusion_context *fusion =
166 (struct fusion_context *)instance->ctrl_context;
167 struct megasas_cmd_fusion *cmd = NULL;
168
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530169 spin_lock_irqsave(&fusion->mpt_pool_lock, flags);
adam radford9c915a82010-12-21 13:34:31 -0800170
171 if (!list_empty(&fusion->cmd_pool)) {
172 cmd = list_entry((&fusion->cmd_pool)->next,
173 struct megasas_cmd_fusion, list);
174 list_del_init(&cmd->list);
175 } else {
176 printk(KERN_ERR "megasas: Command pool (fusion) empty!\n");
177 }
178
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530179 spin_unlock_irqrestore(&fusion->mpt_pool_lock, flags);
adam radford9c915a82010-12-21 13:34:31 -0800180 return cmd;
181}
182
183/**
184 * megasas_return_cmd_fusion - Return a cmd to free command pool
185 * @instance: Adapter soft state
186 * @cmd: Command packet to be returned to free command pool
187 */
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530188inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
189 struct megasas_cmd_fusion *cmd)
adam radford9c915a82010-12-21 13:34:31 -0800190{
191 unsigned long flags;
192 struct fusion_context *fusion =
193 (struct fusion_context *)instance->ctrl_context;
194
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530195 spin_lock_irqsave(&fusion->mpt_pool_lock, flags);
adam radford9c915a82010-12-21 13:34:31 -0800196
197 cmd->scmd = NULL;
198 cmd->sync_cmd_idx = (u32)ULONG_MAX;
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@lsi.com94cd65d2013-09-06 15:50:52 +0530692 /* Convert capability to LE32 */
693 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
adam radford21c9e162013-09-06 15:27:14 -0700694
Hannes Reineckefdc5a972014-01-16 11:25:33 +0100695 init_frame->queue_info_new_phys_addr_hi =
696 cpu_to_le32(upper_32_bits(ioc_init_handle));
697 init_frame->queue_info_new_phys_addr_lo =
698 cpu_to_le32(lower_32_bits(ioc_init_handle));
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530699 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
adam radford9c915a82010-12-21 13:34:31 -0800700
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +0530701 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
702 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
adam radfordc77a9bd2014-03-10 02:51:36 -0700703 req_desc.MFAIo.RequestFlags =
adam radford9c915a82010-12-21 13:34:31 -0800704 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
Sumit.Saxena@avagotech.com200aed52015-01-05 20:05:58 +0530705 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford9c915a82010-12-21 13:34:31 -0800706
707 /*
708 * disable the intr before firing the init frame
709 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530710 instance->instancet->disable_intr(instance);
adam radford9c915a82010-12-21 13:34:31 -0800711
712 for (i = 0; i < (10 * 1000); i += 20) {
713 if (readl(&instance->reg_set->doorbell) & 1)
714 msleep(20);
715 else
716 break;
717 }
718
adam radfordc77a9bd2014-03-10 02:51:36 -0700719 instance->instancet->fire_cmd(instance, req_desc.u.low,
720 req_desc.u.high, instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -0800721
adam radford229fe472014-03-10 02:51:56 -0700722 wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -0800723
724 frame_hdr = &cmd->frame->hdr;
725 if (frame_hdr->cmd_status != 0) {
726 ret = 1;
727 goto fail_fw_init;
728 }
729 printk(KERN_ERR "megasas:IOC Init cmd success\n");
730
731 ret = 0;
732
733fail_fw_init:
734 megasas_return_cmd(instance, cmd);
735 if (IOCInitMessage)
736 dma_free_coherent(&instance->pdev->dev,
737 sizeof(struct MPI2_IOC_INIT_REQUEST),
738 IOCInitMessage, ioc_init_handle);
739fail_get_cmd:
740 return ret;
741}
742
743/*
adam radford9c915a82010-12-21 13:34:31 -0800744 * megasas_get_ld_map_info - Returns FW's ld_map structure
745 * @instance: Adapter soft state
746 * @pend: Pend the command or not
747 * Issues an internal command (DCMD) to get the FW's controller PD
748 * list structure. This information is mainly used to find out SYSTEM
749 * supported by the FW.
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530750 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
751 * dcmd.mbox.b[0] - number of LDs being sync'd
752 * dcmd.mbox.b[1] - 0 - complete command immediately.
753 * - 1 - pend till config change
754 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
755 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
756 * uses extended struct MR_FW_RAID_MAP_EXT
adam radford9c915a82010-12-21 13:34:31 -0800757 */
758static int
759megasas_get_ld_map_info(struct megasas_instance *instance)
760{
761 int ret = 0;
762 struct megasas_cmd *cmd;
763 struct megasas_dcmd_frame *dcmd;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530764 void *ci;
adam radford9c915a82010-12-21 13:34:31 -0800765 dma_addr_t ci_h = 0;
766 u32 size_map_info;
767 struct fusion_context *fusion;
768
769 cmd = megasas_get_cmd(instance);
770
771 if (!cmd) {
772 printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
773 return -ENOMEM;
774 }
775
776 fusion = instance->ctrl_context;
777
778 if (!fusion) {
779 megasas_return_cmd(instance, cmd);
Hannes Reinecke2f8bdfa2014-01-16 11:25:35 +0100780 return -ENXIO;
adam radford9c915a82010-12-21 13:34:31 -0800781 }
782
783 dcmd = &cmd->frame->dcmd;
784
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530785 size_map_info = fusion->current_map_sz;
adam radford9c915a82010-12-21 13:34:31 -0800786
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530787 ci = (void *) fusion->ld_map[(instance->map_id & 1)];
adam radford9c915a82010-12-21 13:34:31 -0800788 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
789
790 if (!ci) {
791 printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
792 megasas_return_cmd(instance, cmd);
793 return -ENOMEM;
794 }
795
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530796 memset(ci, 0, fusion->max_map_sz);
adam radford9c915a82010-12-21 13:34:31 -0800797 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530798#if VD_EXT_DEBUG
799 dev_dbg(&instance->pdev->dev,
800 "%s sending MR_DCMD_LD_MAP_GET_INFO with size %d\n",
801 __func__, cpu_to_le32(size_map_info));
802#endif
adam radford9c915a82010-12-21 13:34:31 -0800803 dcmd->cmd = MFI_CMD_DCMD;
804 dcmd->cmd_status = 0xFF;
805 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530806 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford9c915a82010-12-21 13:34:31 -0800807 dcmd->timeout = 0;
808 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530809 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
810 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
811 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
812 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -0800813
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530814 if (instance->ctrl_context && !instance->mask_interrupts)
815 ret = megasas_issue_blocked_cmd(instance, cmd,
816 MEGASAS_BLOCKED_CMD_TIMEOUT);
817 else
818 ret = megasas_issue_polled(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -0800819
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +0530820 if (instance->ctrl_context && cmd->mpt_pthr_cmd_blocked)
821 megasas_return_mfi_mpt_pthr(instance, cmd,
822 cmd->mpt_pthr_cmd_blocked);
823 else
824 megasas_return_cmd(instance, cmd);
adam radford9c915a82010-12-21 13:34:31 -0800825
826 return ret;
827}
828
829u8
830megasas_get_map_info(struct megasas_instance *instance)
831{
832 struct fusion_context *fusion = instance->ctrl_context;
833
834 fusion->fast_path_io = 0;
835 if (!megasas_get_ld_map_info(instance)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +0530836 if (MR_ValidateMapInfo(instance)) {
adam radford9c915a82010-12-21 13:34:31 -0800837 fusion->fast_path_io = 1;
838 return 0;
839 }
840 }
841 return 1;
842}
843
844/*
845 * megasas_sync_map_info - Returns FW's ld_map structure
846 * @instance: Adapter soft state
847 *
848 * Issues an internal command (DCMD) to get the FW's controller PD
849 * list structure. This information is mainly used to find out SYSTEM
850 * supported by the FW.
851 */
852int
853megasas_sync_map_info(struct megasas_instance *instance)
854{
855 int ret = 0, i;
856 struct megasas_cmd *cmd;
857 struct megasas_dcmd_frame *dcmd;
858 u32 size_sync_info, num_lds;
859 struct fusion_context *fusion;
860 struct MR_LD_TARGET_SYNC *ci = NULL;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530861 struct MR_DRV_RAID_MAP_ALL *map;
adam radford9c915a82010-12-21 13:34:31 -0800862 struct MR_LD_RAID *raid;
863 struct MR_LD_TARGET_SYNC *ld_sync;
864 dma_addr_t ci_h = 0;
865 u32 size_map_info;
866
867 cmd = megasas_get_cmd(instance);
868
869 if (!cmd) {
870 printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
871 "info.\n");
872 return -ENOMEM;
873 }
874
875 fusion = instance->ctrl_context;
876
877 if (!fusion) {
878 megasas_return_cmd(instance, cmd);
879 return 1;
880 }
881
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530882 map = fusion->ld_drv_map[instance->map_id & 1];
adam radford9c915a82010-12-21 13:34:31 -0800883
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +0530884 num_lds = le16_to_cpu(map->raidMap.ldCount);
adam radford9c915a82010-12-21 13:34:31 -0800885
886 dcmd = &cmd->frame->dcmd;
887
888 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
889
890 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
891
892 ci = (struct MR_LD_TARGET_SYNC *)
893 fusion->ld_map[(instance->map_id - 1) & 1];
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530894 memset(ci, 0, fusion->max_map_sz);
adam radford9c915a82010-12-21 13:34:31 -0800895
896 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
897
898 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
899
900 for (i = 0; i < num_lds; i++, ld_sync++) {
901 raid = MR_LdRaidGet(i, map);
902 ld_sync->targetId = MR_GetLDTgtId(i, map);
903 ld_sync->seqNum = raid->seqNum;
904 }
905
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +0530906 size_map_info = fusion->current_map_sz;
adam radford9c915a82010-12-21 13:34:31 -0800907
908 dcmd->cmd = MFI_CMD_DCMD;
909 dcmd->cmd_status = 0xFF;
910 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530911 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
adam radford9c915a82010-12-21 13:34:31 -0800912 dcmd->timeout = 0;
913 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530914 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -0800915 dcmd->mbox.b[0] = num_lds;
916 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530917 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
918 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
919 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -0800920
921 instance->map_update_cmd = cmd;
922
923 instance->instancet->issue_dcmd(instance, cmd);
924
925 return ret;
926}
927
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +0530928/*
929 * meagasas_display_intel_branding - Display branding string
930 * @instance: per adapter object
931 *
932 * Return nothing.
933 */
934static void
935megasas_display_intel_branding(struct megasas_instance *instance)
936{
937 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
938 return;
939
940 switch (instance->pdev->device) {
941 case PCI_DEVICE_ID_LSI_INVADER:
942 switch (instance->pdev->subsystem_device) {
943 case MEGARAID_INTEL_RS3DC080_SSDID:
944 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
945 instance->host->host_no,
946 MEGARAID_INTEL_RS3DC080_BRANDING);
947 break;
948 case MEGARAID_INTEL_RS3DC040_SSDID:
949 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
950 instance->host->host_no,
951 MEGARAID_INTEL_RS3DC040_BRANDING);
952 break;
953 case MEGARAID_INTEL_RS3SC008_SSDID:
954 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
955 instance->host->host_no,
956 MEGARAID_INTEL_RS3SC008_BRANDING);
957 break;
958 case MEGARAID_INTEL_RS3MC044_SSDID:
959 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
960 instance->host->host_no,
961 MEGARAID_INTEL_RS3MC044_BRANDING);
962 break;
963 default:
964 break;
965 }
966 break;
967 case PCI_DEVICE_ID_LSI_FURY:
968 switch (instance->pdev->subsystem_device) {
969 case MEGARAID_INTEL_RS3WC080_SSDID:
970 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
971 instance->host->host_no,
972 MEGARAID_INTEL_RS3WC080_BRANDING);
973 break;
974 case MEGARAID_INTEL_RS3WC040_SSDID:
975 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
976 instance->host->host_no,
977 MEGARAID_INTEL_RS3WC040_BRANDING);
978 break;
979 default:
980 break;
981 }
982 break;
983 default:
984 break;
985 }
986}
987
adam radford9c915a82010-12-21 13:34:31 -0800988/**
989 * megasas_init_adapter_fusion - Initializes the FW
990 * @instance: Adapter soft state
991 *
992 * This is the main function for initializing firmware.
993 */
994u32
995megasas_init_adapter_fusion(struct megasas_instance *instance)
996{
997 struct megasas_register_set __iomem *reg_set;
998 struct fusion_context *fusion;
999 u32 max_cmd;
adam radfordc8e858f2011-10-08 18:15:13 -07001000 int i = 0, count;
adam radford9c915a82010-12-21 13:34:31 -08001001
1002 fusion = instance->ctrl_context;
1003
1004 reg_set = instance->reg_set;
1005
1006 /*
1007 * Get various operational parameters from status register
1008 */
1009 instance->max_fw_cmds =
1010 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
1011 instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
1012
1013 /*
1014 * Reduce the max supported cmds by 1. This is to ensure that the
1015 * reply_q_sz (1 more than the max cmd that driver may send)
1016 * does not exceed max cmds that the FW can support
1017 */
1018 instance->max_fw_cmds = instance->max_fw_cmds-1;
1019 /* Only internal cmds (DCMD) need to have MFI frames */
1020 instance->max_mfi_cmds = MEGASAS_INT_CMDS;
1021
1022 max_cmd = instance->max_fw_cmds;
1023
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05301024 fusion->reply_q_depth = 2 * (((max_cmd + 1 + 15)/16)*16);
adam radford9c915a82010-12-21 13:34:31 -08001025
1026 fusion->request_alloc_sz =
1027 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
1028 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
1029 *(fusion->reply_q_depth);
1030 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
1031 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
1032 (max_cmd + 1)); /* Extra 1 for SMID 0 */
1033
1034 fusion->max_sge_in_main_msg =
1035 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1036 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
1037
1038 fusion->max_sge_in_chain =
1039 MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
1040
Sumit.Saxena@avagotech.coma5fd2852014-09-12 18:57:43 +05301041 instance->max_num_sge = rounddown_pow_of_two(
1042 fusion->max_sge_in_main_msg + fusion->max_sge_in_chain - 2);
adam radford9c915a82010-12-21 13:34:31 -08001043
1044 /* Used for pass thru MFI frame (DCMD) */
1045 fusion->chain_offset_mfi_pthru =
1046 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
1047
1048 fusion->chain_offset_io_request =
1049 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1050 sizeof(union MPI2_SGE_IO_UNION))/16;
1051
adam radfordc8e858f2011-10-08 18:15:13 -07001052 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1053 for (i = 0 ; i < count; i++)
1054 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -08001055
1056 /*
1057 * Allocate memory for descriptors
1058 * Create a pool of commands
1059 */
1060 if (megasas_alloc_cmds(instance))
1061 goto fail_alloc_mfi_cmds;
1062 if (megasas_alloc_cmds_fusion(instance))
1063 goto fail_alloc_cmds;
1064
1065 if (megasas_ioc_init_fusion(instance))
1066 goto fail_ioc_init;
1067
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301068 megasas_display_intel_branding(instance);
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05301069 if (megasas_get_ctrl_info(instance)) {
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301070 dev_err(&instance->pdev->dev,
1071 "Could not get controller info. Fail from %s %d\n",
1072 __func__, __LINE__);
1073 goto fail_ioc_init;
1074 }
1075
adam radford9c915a82010-12-21 13:34:31 -08001076 instance->flag_ieee = 1;
adam radford9c915a82010-12-21 13:34:31 -08001077 fusion->fast_path_io = 0;
1078
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301079 fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1080 for (i = 0; i < 2; i++) {
1081 fusion->ld_map[i] = NULL;
1082 fusion->ld_drv_map[i] = (void *)__get_free_pages(GFP_KERNEL,
1083 fusion->drv_map_pages);
1084 if (!fusion->ld_drv_map[i]) {
1085 dev_err(&instance->pdev->dev, "Could not allocate "
1086 "memory for local map info for %d pages\n",
1087 fusion->drv_map_pages);
1088 if (i == 1)
1089 free_pages((ulong)fusion->ld_drv_map[0],
1090 fusion->drv_map_pages);
1091 goto fail_ioc_init;
1092 }
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05301093 memset(fusion->ld_drv_map[i], 0,
1094 ((1 << PAGE_SHIFT) << fusion->drv_map_pages));
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301095 }
1096
adam radford9c915a82010-12-21 13:34:31 -08001097 for (i = 0; i < 2; i++) {
1098 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301099 fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08001100 &fusion->ld_map_phys[i],
1101 GFP_KERNEL);
1102 if (!fusion->ld_map[i]) {
1103 printk(KERN_ERR "megasas: Could not allocate memory "
1104 "for map info\n");
1105 goto fail_map_info;
1106 }
1107 }
1108
1109 if (!megasas_get_map_info(instance))
1110 megasas_sync_map_info(instance);
1111
1112 return 0;
1113
adam radford9c915a82010-12-21 13:34:31 -08001114fail_map_info:
1115 if (i == 1)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301116 dma_free_coherent(&instance->pdev->dev, fusion->max_map_sz,
adam radford9c915a82010-12-21 13:34:31 -08001117 fusion->ld_map[0], fusion->ld_map_phys[0]);
1118fail_ioc_init:
adam radfordeb1b1232011-02-24 20:55:56 -08001119 megasas_free_cmds_fusion(instance);
1120fail_alloc_cmds:
1121 megasas_free_cmds(instance);
1122fail_alloc_mfi_cmds:
adam radford9c915a82010-12-21 13:34:31 -08001123 return 1;
1124}
1125
1126/**
1127 * megasas_fire_cmd_fusion - Sends command to the FW
1128 * @frame_phys_addr : Physical address of cmd
1129 * @frame_count : Number of frames for the command
1130 * @regs : MFI register set
1131 */
1132void
1133megasas_fire_cmd_fusion(struct megasas_instance *instance,
1134 dma_addr_t req_desc_lo,
1135 u32 req_desc_hi,
1136 struct megasas_register_set __iomem *regs)
1137{
Sumit.Saxena@avagotech.com07560402014-09-12 18:57:18 +05301138#if defined(writeq) && defined(CONFIG_64BIT)
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301139 u64 req_data = (((u64)le32_to_cpu(req_desc_hi) << 32) |
1140 le32_to_cpu(req_desc_lo));
Sumit.Saxena@avagotech.com07560402014-09-12 18:57:18 +05301141
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301142 writeq(req_data, &(regs)->inbound_low_queue_port);
Sumit.Saxena@avagotech.com07560402014-09-12 18:57:18 +05301143#else
adam radford9c915a82010-12-21 13:34:31 -08001144 unsigned long flags;
1145
1146 spin_lock_irqsave(&instance->hba_lock, flags);
1147
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301148 writel(le32_to_cpu(req_desc_lo), &(regs)->inbound_low_queue_port);
1149 writel(le32_to_cpu(req_desc_hi), &(regs)->inbound_high_queue_port);
adam radford9c915a82010-12-21 13:34:31 -08001150 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@avagotech.com07560402014-09-12 18:57:18 +05301151#endif
adam radford9c915a82010-12-21 13:34:31 -08001152}
1153
1154/**
1155 * map_cmd_status - Maps FW cmd status to OS cmd status
1156 * @cmd : Pointer to cmd
1157 * @status : status of cmd returned by FW
1158 * @ext_status : ext status of cmd returned by FW
1159 */
1160
1161void
1162map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1163{
1164
1165 switch (status) {
1166
1167 case MFI_STAT_OK:
1168 cmd->scmd->result = DID_OK << 16;
1169 break;
1170
1171 case MFI_STAT_SCSI_IO_FAILED:
1172 case MFI_STAT_LD_INIT_IN_PROGRESS:
1173 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1174 break;
1175
1176 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1177
1178 cmd->scmd->result = (DID_OK << 16) | ext_status;
1179 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1180 memset(cmd->scmd->sense_buffer, 0,
1181 SCSI_SENSE_BUFFERSIZE);
1182 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1183 SCSI_SENSE_BUFFERSIZE);
1184 cmd->scmd->result |= DRIVER_SENSE << 24;
1185 }
1186 break;
1187
1188 case MFI_STAT_LD_OFFLINE:
1189 case MFI_STAT_DEVICE_NOT_FOUND:
1190 cmd->scmd->result = DID_BAD_TARGET << 16;
1191 break;
adam radford36807e62011-10-08 18:15:06 -07001192 case MFI_STAT_CONFIG_SEQ_MISMATCH:
1193 cmd->scmd->result = DID_IMM_RETRY << 16;
1194 break;
adam radford9c915a82010-12-21 13:34:31 -08001195 default:
1196 printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1197 cmd->scmd->result = DID_ERROR << 16;
1198 break;
1199 }
1200}
1201
1202/**
1203 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1204 * @instance: Adapter soft state
1205 * @scp: SCSI command from the mid-layer
1206 * @sgl_ptr: SGL to be filled in
1207 * @cmd: cmd we are working on
1208 *
1209 * If successful, this function returns the number of SG elements.
1210 */
1211static int
1212megasas_make_sgl_fusion(struct megasas_instance *instance,
1213 struct scsi_cmnd *scp,
1214 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1215 struct megasas_cmd_fusion *cmd)
1216{
adam radford36807e62011-10-08 18:15:06 -07001217 int i, sg_processed, sge_count;
adam radford9c915a82010-12-21 13:34:31 -08001218 struct scatterlist *os_sgl;
1219 struct fusion_context *fusion;
1220
1221 fusion = instance->ctrl_context;
1222
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301223 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1224 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001225 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1226 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1227 sgl_ptr_end->Flags = 0;
1228 }
adam radford9c915a82010-12-21 13:34:31 -08001229
1230 sge_count = scsi_dma_map(scp);
1231
1232 BUG_ON(sge_count < 0);
1233
1234 if (sge_count > instance->max_num_sge || !sge_count)
1235 return sge_count;
1236
adam radford9c915a82010-12-21 13:34:31 -08001237 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301238 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
1239 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
adam radford9c915a82010-12-21 13:34:31 -08001240 sgl_ptr->Flags = 0;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301241 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1242 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001243 if (i == sge_count - 1)
1244 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1245 }
adam radford9c915a82010-12-21 13:34:31 -08001246 sgl_ptr++;
1247
1248 sg_processed = i + 1;
1249
1250 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1251 (sge_count > fusion->max_sge_in_main_msg)) {
1252
1253 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301254 if ((instance->pdev->device ==
1255 PCI_DEVICE_ID_LSI_INVADER) ||
1256 (instance->pdev->device ==
1257 PCI_DEVICE_ID_LSI_FURY)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301258 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1259 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1260 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
adam radford36807e62011-10-08 18:15:06 -07001261 cmd->io_request->ChainOffset =
1262 fusion->
1263 chain_offset_io_request;
1264 else
1265 cmd->io_request->ChainOffset = 0;
1266 } else
1267 cmd->io_request->ChainOffset =
1268 fusion->chain_offset_io_request;
1269
adam radford9c915a82010-12-21 13:34:31 -08001270 sg_chain = sgl_ptr;
1271 /* Prepare chain element */
1272 sg_chain->NextChainOffset = 0;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301273 if ((instance->pdev->device ==
1274 PCI_DEVICE_ID_LSI_INVADER) ||
1275 (instance->pdev->device ==
1276 PCI_DEVICE_ID_LSI_FURY))
adam radford36807e62011-10-08 18:15:06 -07001277 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1278 else
1279 sg_chain->Flags =
1280 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1281 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301282 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
1283 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08001284
1285 sgl_ptr =
1286 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1287 }
1288 }
1289
1290 return sge_count;
1291}
1292
1293/**
1294 * megasas_set_pd_lba - Sets PD LBA
1295 * @cdb: CDB
1296 * @cdb_len: cdb length
1297 * @start_blk: Start block of IO
1298 *
1299 * Used to set the PD LBA in CDB for FP IOs
1300 */
1301void
1302megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1303 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301304 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
adam radford9c915a82010-12-21 13:34:31 -08001305{
1306 struct MR_LD_RAID *raid;
1307 u32 ld;
1308 u64 start_blk = io_info->pdBlock;
1309 u8 *cdb = io_request->CDB.CDB32;
1310 u32 num_blocks = io_info->numBlocks;
adam radford495c5602011-05-11 18:34:40 -07001311 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
adam radford9c915a82010-12-21 13:34:31 -08001312
1313 /* Check if T10 PI (DIF) is enabled for this LD */
1314 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1315 raid = MR_LdRaidGet(ld, local_map_ptr);
1316 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1317 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1318 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1319 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1320
1321 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1322 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1323 else
1324 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1325 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1326
1327 /* LBA */
1328 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1329 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1330 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1331 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1332 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1333 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1334 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1335 cdb[19] = (u8)(start_blk & 0xff);
1336
1337 /* Logical block reference tag */
1338 io_request->CDB.EEDP32.PrimaryReferenceTag =
1339 cpu_to_be32(ref_tag);
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301340 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301341 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001342
1343 /* Transfer length */
1344 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1345 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1346 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1347 cdb[31] = (u8)(num_blocks & 0xff);
1348
1349 /* set SCSI IO EEDPFlags */
1350 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301351 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001352 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1353 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1354 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1355 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301356 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
adam radford9c915a82010-12-21 13:34:31 -08001357 } else {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301358 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001359 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301360 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
adam radford9c915a82010-12-21 13:34:31 -08001361 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301362 io_request->Control |= cpu_to_le32((0x4 << 26));
1363 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
adam radford9c915a82010-12-21 13:34:31 -08001364 } else {
1365 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1366 if (((cdb_len == 12) || (cdb_len == 16)) &&
1367 (start_blk <= 0xffffffff)) {
1368 if (cdb_len == 16) {
1369 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1370 flagvals = cdb[1];
1371 groupnum = cdb[14];
1372 control = cdb[15];
1373 } else {
1374 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1375 flagvals = cdb[1];
1376 groupnum = cdb[10];
1377 control = cdb[11];
1378 }
1379
1380 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1381
1382 cdb[0] = opcode;
1383 cdb[1] = flagvals;
1384 cdb[6] = groupnum;
1385 cdb[9] = control;
1386
1387 /* Transfer length */
1388 cdb[8] = (u8)(num_blocks & 0xff);
1389 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1390
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301391 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001392 cdb_len = 10;
adam radford495c5602011-05-11 18:34:40 -07001393 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1394 /* Convert to 16 byte CDB for large LBA's */
1395 switch (cdb_len) {
1396 case 6:
1397 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1398 control = cdb[5];
1399 break;
1400 case 10:
1401 opcode =
1402 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1403 flagvals = cdb[1];
1404 groupnum = cdb[6];
1405 control = cdb[9];
1406 break;
1407 case 12:
1408 opcode =
1409 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1410 flagvals = cdb[1];
1411 groupnum = cdb[10];
1412 control = cdb[11];
1413 break;
1414 }
1415
1416 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1417
1418 cdb[0] = opcode;
1419 cdb[1] = flagvals;
1420 cdb[14] = groupnum;
1421 cdb[15] = control;
1422
1423 /* Transfer length */
1424 cdb[13] = (u8)(num_blocks & 0xff);
1425 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1426 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1427 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1428
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301429 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
adam radford495c5602011-05-11 18:34:40 -07001430 cdb_len = 16;
adam radford9c915a82010-12-21 13:34:31 -08001431 }
1432
1433 /* Normal case, just load LBA here */
1434 switch (cdb_len) {
1435 case 6:
1436 {
1437 u8 val = cdb[1] & 0xE0;
1438 cdb[3] = (u8)(start_blk & 0xff);
1439 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1440 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1441 break;
1442 }
1443 case 10:
1444 cdb[5] = (u8)(start_blk & 0xff);
1445 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1446 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1447 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1448 break;
1449 case 12:
1450 cdb[5] = (u8)(start_blk & 0xff);
1451 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1452 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1453 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1454 break;
1455 case 16:
1456 cdb[9] = (u8)(start_blk & 0xff);
1457 cdb[8] = (u8)((start_blk >> 8) & 0xff);
1458 cdb[7] = (u8)((start_blk >> 16) & 0xff);
1459 cdb[6] = (u8)((start_blk >> 24) & 0xff);
1460 cdb[5] = (u8)((start_blk >> 32) & 0xff);
1461 cdb[4] = (u8)((start_blk >> 40) & 0xff);
1462 cdb[3] = (u8)((start_blk >> 48) & 0xff);
1463 cdb[2] = (u8)((start_blk >> 56) & 0xff);
1464 break;
1465 }
1466 }
1467}
1468
1469/**
1470 * megasas_build_ldio_fusion - Prepares IOs to devices
1471 * @instance: Adapter soft state
1472 * @scp: SCSI command
1473 * @cmd: Command to be prepared
1474 *
1475 * Prepares the io_request and chain elements (sg_frame) for IO
1476 * The IO can be for PD (Fast Path) or LD
1477 */
1478void
1479megasas_build_ldio_fusion(struct megasas_instance *instance,
1480 struct scsi_cmnd *scp,
1481 struct megasas_cmd_fusion *cmd)
1482{
1483 u8 fp_possible;
adam radfordf9eff812012-10-01 19:27:07 -07001484 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
adam radford9c915a82010-12-21 13:34:31 -08001485 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1486 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1487 struct IO_REQUEST_INFO io_info;
1488 struct fusion_context *fusion;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301489 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
adam radford21c9e162013-09-06 15:27:14 -07001490 u8 *raidLUN;
adam radford9c915a82010-12-21 13:34:31 -08001491
1492 device_id = MEGASAS_DEV_INDEX(instance, scp);
1493
1494 fusion = instance->ctrl_context;
1495
1496 io_request = cmd->io_request;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301497 io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
adam radford9c915a82010-12-21 13:34:31 -08001498 io_request->RaidContext.status = 0;
1499 io_request->RaidContext.exStatus = 0;
1500
1501 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1502
1503 start_lba_lo = 0;
1504 start_lba_hi = 0;
1505 fp_possible = 0;
1506
1507 /*
1508 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1509 */
1510 if (scp->cmd_len == 6) {
adam radfordf9eff812012-10-01 19:27:07 -07001511 datalength = (u32) scp->cmnd[4];
adam radford9c915a82010-12-21 13:34:31 -08001512 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1513 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1514
1515 start_lba_lo &= 0x1FFFFF;
1516 }
1517
1518 /*
1519 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1520 */
1521 else if (scp->cmd_len == 10) {
adam radfordf9eff812012-10-01 19:27:07 -07001522 datalength = (u32) scp->cmnd[8] |
adam radford9c915a82010-12-21 13:34:31 -08001523 ((u32) scp->cmnd[7] << 8);
1524 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1525 ((u32) scp->cmnd[3] << 16) |
1526 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1527 }
1528
1529 /*
1530 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1531 */
1532 else if (scp->cmd_len == 12) {
adam radfordf9eff812012-10-01 19:27:07 -07001533 datalength = ((u32) scp->cmnd[6] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08001534 ((u32) scp->cmnd[7] << 16) |
1535 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1536 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1537 ((u32) scp->cmnd[3] << 16) |
1538 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1539 }
1540
1541 /*
1542 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1543 */
1544 else if (scp->cmd_len == 16) {
adam radfordf9eff812012-10-01 19:27:07 -07001545 datalength = ((u32) scp->cmnd[10] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08001546 ((u32) scp->cmnd[11] << 16) |
1547 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1548 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1549 ((u32) scp->cmnd[7] << 16) |
1550 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1551
1552 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1553 ((u32) scp->cmnd[3] << 16) |
1554 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1555 }
1556
1557 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1558 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
adam radfordf9eff812012-10-01 19:27:07 -07001559 io_info.numBlocks = datalength;
adam radford9c915a82010-12-21 13:34:31 -08001560 io_info.ldTgtId = device_id;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301561 io_request->DataLength = cpu_to_le32(scsi_bufflen(scp));
adam radford9c915a82010-12-21 13:34:31 -08001562
1563 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1564 io_info.isRead = 1;
1565
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301566 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
adam radford9c915a82010-12-21 13:34:31 -08001567
1568 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301569 instance->fw_supported_vd_count) || (!fusion->fast_path_io)) {
adam radford9c915a82010-12-21 13:34:31 -08001570 io_request->RaidContext.regLockFlags = 0;
1571 fp_possible = 0;
1572 } else {
adam radford36807e62011-10-08 18:15:06 -07001573 if (MR_BuildRaidContext(instance, &io_info,
1574 &io_request->RaidContext,
adam radford21c9e162013-09-06 15:27:14 -07001575 local_map_ptr, &raidLUN))
adam radford9c915a82010-12-21 13:34:31 -08001576 fp_possible = io_info.fpOkForIo;
1577 }
1578
adam radfordc8e858f2011-10-08 18:15:13 -07001579 /* Use smp_processor_id() for now until cmd->request->cpu is CPU
1580 id by default, not CPU group id, otherwise all MSI-X queues won't
1581 be utilized */
1582 cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
1583 smp_processor_id() % instance->msix_vectors : 0;
1584
adam radford9c915a82010-12-21 13:34:31 -08001585 if (fp_possible) {
1586 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1587 local_map_ptr, start_lba_lo);
adam radford9c915a82010-12-21 13:34:31 -08001588 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1589 cmd->request_desc->SCSIIO.RequestFlags =
1590 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1591 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301592 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1593 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001594 if (io_request->RaidContext.regLockFlags ==
1595 REGION_TYPE_UNUSED)
1596 cmd->request_desc->SCSIIO.RequestFlags =
1597 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1598 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1599 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1600 io_request->RaidContext.nseg = 0x1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301601 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
adam radford36807e62011-10-08 18:15:06 -07001602 io_request->RaidContext.regLockFlags |=
1603 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
1604 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1605 }
adam radford9c915a82010-12-21 13:34:31 -08001606 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1607 (io_info.isRead)) {
1608 io_info.devHandle =
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05301609 get_updated_dev_handle(instance,
adam radford9c915a82010-12-21 13:34:31 -08001610 &fusion->load_balance_info[device_id],
1611 &io_info);
1612 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05301613 cmd->pd_r1_lb = io_info.pd_after_lb;
adam radford9c915a82010-12-21 13:34:31 -08001614 } else
1615 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1616 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1617 io_request->DevHandle = io_info.devHandle;
adam radford21c9e162013-09-06 15:27:14 -07001618 /* populate the LUN field */
1619 memcpy(io_request->LUN, raidLUN, 8);
adam radford9c915a82010-12-21 13:34:31 -08001620 } else {
1621 io_request->RaidContext.timeoutValue =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301622 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
adam radford9c915a82010-12-21 13:34:31 -08001623 cmd->request_desc->SCSIIO.RequestFlags =
1624 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1625 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301626 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1627 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001628 if (io_request->RaidContext.regLockFlags ==
1629 REGION_TYPE_UNUSED)
1630 cmd->request_desc->SCSIIO.RequestFlags =
1631 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1632 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1633 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1634 io_request->RaidContext.regLockFlags |=
1635 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
1636 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1637 io_request->RaidContext.nseg = 0x1;
1638 }
1639 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301640 io_request->DevHandle = cpu_to_le16(device_id);
adam radford9c915a82010-12-21 13:34:31 -08001641 } /* Not FP */
1642}
1643
1644/**
1645 * megasas_build_dcdb_fusion - Prepares IOs to devices
1646 * @instance: Adapter soft state
1647 * @scp: SCSI command
1648 * @cmd: Command to be prepared
1649 *
1650 * Prepares the io_request frame for non-io cmds
1651 */
1652static void
1653megasas_build_dcdb_fusion(struct megasas_instance *instance,
1654 struct scsi_cmnd *scmd,
1655 struct megasas_cmd_fusion *cmd)
1656{
1657 u32 device_id;
1658 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1659 u16 pd_index = 0;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301660 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
adam radford9c915a82010-12-21 13:34:31 -08001661 struct fusion_context *fusion = instance->ctrl_context;
adam radford21c9e162013-09-06 15:27:14 -07001662 u8 span, physArm;
1663 u16 devHandle;
1664 u32 ld, arRef, pd;
1665 struct MR_LD_RAID *raid;
1666 struct RAID_CONTEXT *pRAID_Context;
adam radford9c915a82010-12-21 13:34:31 -08001667
1668 io_request = cmd->io_request;
1669 device_id = MEGASAS_DEV_INDEX(instance, scmd);
1670 pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
1671 +scmd->device->id;
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301672 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
adam radford9c915a82010-12-21 13:34:31 -08001673
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301674 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
adam radford21c9e162013-09-06 15:27:14 -07001675
1676
adam radford9c915a82010-12-21 13:34:31 -08001677 /* Check if this is a system PD I/O */
adam radforde187df62012-10-01 19:27:12 -07001678 if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS &&
1679 instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
adam radford9c915a82010-12-21 13:34:31 -08001680 io_request->Function = 0;
adam radford9c5ebd02013-02-09 15:29:20 -08001681 if (fusion->fast_path_io)
1682 io_request->DevHandle =
adam radford9c915a82010-12-21 13:34:31 -08001683 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1684 io_request->RaidContext.timeoutValue =
1685 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1686 io_request->RaidContext.regLockFlags = 0;
1687 io_request->RaidContext.regLockRowLBA = 0;
1688 io_request->RaidContext.regLockLength = 0;
1689 io_request->RaidContext.RAIDFlags =
1690 MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
1691 MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
Sumit.Saxena@lsi.com5d0d9082013-05-22 12:33:29 +05301692 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1693 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
Sumit.Saxena@lsi.combe263742014-02-12 23:37:46 +05301694 io_request->IoFlags |= cpu_to_le16(
1695 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
adam radford9c915a82010-12-21 13:34:31 -08001696 cmd->request_desc->SCSIIO.RequestFlags =
1697 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1698 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford8ead5812012-10-01 19:27:20 -07001699 cmd->request_desc->SCSIIO.DevHandle =
1700 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05301701 cmd->request_desc->SCSIIO.MSIxIndex =
1702 instance->msix_vectors ? smp_processor_id() % instance->msix_vectors : 0;
Sumit.Saxena@lsi.com32d87452013-05-22 12:31:18 +05301703 /*
1704 * If the command is for the tape device, set the
1705 * FP timeout to the os layer timeout value.
1706 */
1707 if (scmd->device->type == TYPE_TAPE) {
1708 if ((scmd->request->timeout / HZ) > 0xFFFF)
1709 io_request->RaidContext.timeoutValue =
1710 0xFFFF;
1711 else
1712 io_request->RaidContext.timeoutValue =
1713 scmd->request->timeout / HZ;
1714 }
adam radford9c915a82010-12-21 13:34:31 -08001715 } else {
adam radford21c9e162013-09-06 15:27:14 -07001716 if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS)
1717 goto NonFastPath;
1718
1719 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05301720 if ((ld >= instance->fw_supported_vd_count) ||
1721 (!fusion->fast_path_io))
adam radford21c9e162013-09-06 15:27:14 -07001722 goto NonFastPath;
1723
1724 raid = MR_LdRaidGet(ld, local_map_ptr);
1725
1726 /* check if this LD is FP capable */
1727 if (!(raid->capability.fpNonRWCapable))
1728 /* not FP capable, send as non-FP */
1729 goto NonFastPath;
1730
1731 /* get RAID_Context pointer */
1732 pRAID_Context = &io_request->RaidContext;
1733
1734 /* set RAID context values */
1735 pRAID_Context->regLockFlags = REGION_TYPE_SHARED_READ;
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05301736 pRAID_Context->timeoutValue = cpu_to_le16(raid->fpIoTimeoutForLd);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301737 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
adam radford21c9e162013-09-06 15:27:14 -07001738 pRAID_Context->regLockRowLBA = 0;
1739 pRAID_Context->regLockLength = 0;
1740 pRAID_Context->configSeqNum = raid->seqNum;
1741
1742 /* get the DevHandle for the PD (since this is
1743 fpNonRWCapable, this is a single disk RAID0) */
1744 span = physArm = 0;
1745 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
1746 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
1747 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
1748
1749 /* build request descriptor */
1750 cmd->request_desc->SCSIIO.RequestFlags =
1751 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1752 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1753 cmd->request_desc->SCSIIO.DevHandle = devHandle;
1754
1755 /* populate the LUN field */
1756 memcpy(io_request->LUN, raid->LUN, 8);
1757
1758 /* build the raidScsiIO structure */
1759 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1760 io_request->DevHandle = devHandle;
1761
1762 return;
1763
1764NonFastPath:
adam radford9c915a82010-12-21 13:34:31 -08001765 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301766 io_request->DevHandle = cpu_to_le16(device_id);
adam radford9c915a82010-12-21 13:34:31 -08001767 cmd->request_desc->SCSIIO.RequestFlags =
1768 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1769 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1770 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301771 io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001772 int_to_scsilun(scmd->device->lun, (struct scsi_lun *)io_request->LUN);
adam radford9c915a82010-12-21 13:34:31 -08001773}
1774
1775/**
1776 * megasas_build_io_fusion - Prepares IOs to devices
1777 * @instance: Adapter soft state
1778 * @scp: SCSI command
1779 * @cmd: Command to be prepared
1780 *
1781 * Invokes helper functions to prepare request frames
1782 * and sets flags appropriate for IO/Non-IO cmd
1783 */
1784int
1785megasas_build_io_fusion(struct megasas_instance *instance,
1786 struct scsi_cmnd *scp,
1787 struct megasas_cmd_fusion *cmd)
1788{
1789 u32 device_id, sge_count;
1790 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1791
1792 device_id = MEGASAS_DEV_INDEX(instance, scp);
1793
1794 /* Zero out some fields so they don't get reused */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001795 memset(io_request->LUN, 0x0, 8);
adam radford9c915a82010-12-21 13:34:31 -08001796 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1797 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1798 io_request->EEDPFlags = 0;
1799 io_request->Control = 0;
1800 io_request->EEDPBlockSize = 0;
adam radford36807e62011-10-08 18:15:06 -07001801 io_request->ChainOffset = 0;
adam radford9c915a82010-12-21 13:34:31 -08001802 io_request->RaidContext.RAIDFlags = 0;
adam radford36807e62011-10-08 18:15:06 -07001803 io_request->RaidContext.Type = 0;
1804 io_request->RaidContext.nseg = 0;
adam radford9c915a82010-12-21 13:34:31 -08001805
1806 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1807 /*
1808 * Just the CDB length,rest of the Flags are zero
1809 * This will be modified for FP in build_ldio_fusion
1810 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301811 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
adam radford9c915a82010-12-21 13:34:31 -08001812
1813 if (megasas_is_ldio(scp))
1814 megasas_build_ldio_fusion(instance, scp, cmd);
1815 else
1816 megasas_build_dcdb_fusion(instance, scp, cmd);
1817
1818 /*
1819 * Construct SGL
1820 */
1821
1822 sge_count =
1823 megasas_make_sgl_fusion(instance, scp,
1824 (struct MPI25_IEEE_SGE_CHAIN64 *)
1825 &io_request->SGL, cmd);
1826
1827 if (sge_count > instance->max_num_sge) {
1828 printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1829 "max (0x%x) allowed\n", sge_count,
1830 instance->max_num_sge);
1831 return 1;
1832 }
1833
1834 io_request->RaidContext.numSGE = sge_count;
1835
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301836 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
adam radford9c915a82010-12-21 13:34:31 -08001837
1838 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301839 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
adam radford9c915a82010-12-21 13:34:31 -08001840 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301841 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
adam radford9c915a82010-12-21 13:34:31 -08001842
1843 io_request->SGLOffset0 =
1844 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1845
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301846 io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08001847 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1848
1849 cmd->scmd = scp;
1850 scp->SCp.ptr = (char *)cmd;
1851
1852 return 0;
1853}
1854
1855union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1856megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1857{
1858 u8 *p;
1859 struct fusion_context *fusion;
1860
1861 if (index >= instance->max_fw_cmds) {
1862 printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
adam radford229fe472014-03-10 02:51:56 -07001863 "descriptor for scsi%d\n", index,
1864 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08001865 return NULL;
1866 }
1867 fusion = instance->ctrl_context;
1868 p = fusion->req_frames_desc
1869 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1870
1871 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1872}
1873
1874/**
1875 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1876 * issuing non IOCTL cmd
1877 * @instance: Adapter soft state
1878 * @scmd: pointer to scsi cmd from OS
1879 */
1880static u32
1881megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1882 struct scsi_cmnd *scmd)
1883{
1884 struct megasas_cmd_fusion *cmd;
1885 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1886 u32 index;
1887 struct fusion_context *fusion;
1888
1889 fusion = instance->ctrl_context;
1890
1891 cmd = megasas_get_cmd_fusion(instance);
1892 if (!cmd)
1893 return SCSI_MLQUEUE_HOST_BUSY;
1894
1895 index = cmd->index;
1896
1897 req_desc = megasas_get_request_descriptor(instance, index-1);
1898 if (!req_desc)
1899 return 1;
1900
1901 req_desc->Words = 0;
1902 cmd->request_desc = req_desc;
adam radford9c915a82010-12-21 13:34:31 -08001903
1904 if (megasas_build_io_fusion(instance, scmd, cmd)) {
1905 megasas_return_cmd_fusion(instance, cmd);
1906 printk(KERN_ERR "megasas: Error building command.\n");
1907 cmd->request_desc = NULL;
1908 return 1;
1909 }
1910
1911 req_desc = cmd->request_desc;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301912 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08001913
1914 if (cmd->io_request->ChainOffset != 0 &&
1915 cmd->io_request->ChainOffset != 0xF)
1916 printk(KERN_ERR "megasas: The chain offset value is not "
1917 "correct : %x\n", cmd->io_request->ChainOffset);
1918
1919 /*
1920 * Issue the command to the FW
1921 */
1922 atomic_inc(&instance->fw_outstanding);
1923
1924 instance->instancet->fire_cmd(instance,
1925 req_desc->u.low, req_desc->u.high,
1926 instance->reg_set);
1927
1928 return 0;
1929}
1930
1931/**
1932 * complete_cmd_fusion - Completes command
1933 * @instance: Adapter soft state
1934 * Completes all commands that is in reply descriptor queue
1935 */
1936int
adam radfordc8e858f2011-10-08 18:15:13 -07001937complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
adam radford9c915a82010-12-21 13:34:31 -08001938{
1939 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1940 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1941 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1942 struct fusion_context *fusion;
1943 struct megasas_cmd *cmd_mfi;
1944 struct megasas_cmd_fusion *cmd_fusion;
1945 u16 smid, num_completed;
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05301946 u8 reply_descript_type;
adam radford9c915a82010-12-21 13:34:31 -08001947 u32 status, extStatus, device_id;
1948 union desc_value d_val;
1949 struct LD_LOAD_BALANCE_INFO *lbinfo;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05301950 int threshold_reply_count = 0;
adam radford9c915a82010-12-21 13:34:31 -08001951
1952 fusion = instance->ctrl_context;
1953
1954 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1955 return IRQ_HANDLED;
1956
1957 desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -07001958 desc += ((MSIxIndex * fusion->reply_alloc_sz)/
1959 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
1960 fusion->last_reply_idx[MSIxIndex];
adam radford9c915a82010-12-21 13:34:31 -08001961
1962 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1963
1964 d_val.word = desc->Words;
1965
1966 reply_descript_type = reply_desc->ReplyFlags &
1967 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1968
1969 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1970 return IRQ_NONE;
1971
adam radford9c915a82010-12-21 13:34:31 -08001972 num_completed = 0;
1973
1974 while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301975 smid = le16_to_cpu(reply_desc->SMID);
adam radford9c915a82010-12-21 13:34:31 -08001976
1977 cmd_fusion = fusion->cmd_list[smid - 1];
1978
1979 scsi_io_req =
1980 (struct MPI2_RAID_SCSI_IO_REQUEST *)
1981 cmd_fusion->io_request;
1982
1983 if (cmd_fusion->scmd)
1984 cmd_fusion->scmd->SCp.ptr = NULL;
1985
1986 status = scsi_io_req->RaidContext.status;
1987 extStatus = scsi_io_req->RaidContext.exStatus;
1988
1989 switch (scsi_io_req->Function) {
1990 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
1991 /* Update load balancing info */
1992 device_id = MEGASAS_DEV_INDEX(instance,
1993 cmd_fusion->scmd);
1994 lbinfo = &fusion->load_balance_info[device_id];
1995 if (cmd_fusion->scmd->SCp.Status &
1996 MEGASAS_LOAD_BALANCE_FLAG) {
Sumit.Saxena@avagotech.comd2552eb2014-09-12 18:57:53 +05301997 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
adam radford9c915a82010-12-21 13:34:31 -08001998 cmd_fusion->scmd->SCp.Status &=
1999 ~MEGASAS_LOAD_BALANCE_FLAG;
2000 }
2001 if (reply_descript_type ==
2002 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
2003 if (megasas_dbg_lvl == 5)
2004 printk(KERN_ERR "\nmegasas: FAST Path "
2005 "IO Success\n");
2006 }
2007 /* Fall thru and complete IO */
2008 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
2009 /* Map the FW Cmd Status */
2010 map_cmd_status(cmd_fusion, status, extStatus);
2011 scsi_dma_unmap(cmd_fusion->scmd);
2012 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2013 scsi_io_req->RaidContext.status = 0;
2014 scsi_io_req->RaidContext.exStatus = 0;
2015 megasas_return_cmd_fusion(instance, cmd_fusion);
2016 atomic_dec(&instance->fw_outstanding);
2017
2018 break;
2019 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
2020 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302021
2022 if (!cmd_mfi->mpt_pthr_cmd_blocked) {
2023 if (megasas_dbg_lvl == 5)
2024 dev_info(&instance->pdev->dev,
2025 "freeing mfi/mpt pass-through "
2026 "from %s %d\n",
2027 __func__, __LINE__);
2028 megasas_return_mfi_mpt_pthr(instance, cmd_mfi,
2029 cmd_fusion);
2030 }
2031
adam radford9c915a82010-12-21 13:34:31 -08002032 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
2033 cmd_fusion->flags = 0;
adam radford9c915a82010-12-21 13:34:31 -08002034 break;
2035 }
2036
adam radfordc8e858f2011-10-08 18:15:13 -07002037 fusion->last_reply_idx[MSIxIndex]++;
2038 if (fusion->last_reply_idx[MSIxIndex] >=
2039 fusion->reply_q_depth)
2040 fusion->last_reply_idx[MSIxIndex] = 0;
adam radford9c915a82010-12-21 13:34:31 -08002041
2042 desc->Words = ULLONG_MAX;
2043 num_completed++;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05302044 threshold_reply_count++;
adam radford9c915a82010-12-21 13:34:31 -08002045
2046 /* Get the next reply descriptor */
adam radfordc8e858f2011-10-08 18:15:13 -07002047 if (!fusion->last_reply_idx[MSIxIndex])
2048 desc = fusion->reply_frames_desc +
2049 ((MSIxIndex * fusion->reply_alloc_sz)/
2050 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
adam radford9c915a82010-12-21 13:34:31 -08002051 else
2052 desc++;
2053
2054 reply_desc =
2055 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
2056
2057 d_val.word = desc->Words;
2058
2059 reply_descript_type = reply_desc->ReplyFlags &
2060 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
2061
2062 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
2063 break;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05302064 /*
2065 * Write to reply post host index register after completing threshold
2066 * number of reply counts and still there are more replies in reply queue
2067 * pending to be completed
2068 */
2069 if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
2070 if ((instance->pdev->device ==
2071 PCI_DEVICE_ID_LSI_INVADER) ||
2072 (instance->pdev->device ==
2073 PCI_DEVICE_ID_LSI_FURY))
2074 writel(((MSIxIndex & 0x7) << 24) |
2075 fusion->last_reply_idx[MSIxIndex],
2076 instance->reply_post_host_index_addr[MSIxIndex/8]);
2077 else
2078 writel((MSIxIndex << 24) |
2079 fusion->last_reply_idx[MSIxIndex],
2080 instance->reply_post_host_index_addr[0]);
2081 threshold_reply_count = 0;
2082 }
adam radford9c915a82010-12-21 13:34:31 -08002083 }
2084
2085 if (!num_completed)
2086 return IRQ_NONE;
2087
2088 wmb();
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302089 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2090 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
2091 writel(((MSIxIndex & 0x7) << 24) |
2092 fusion->last_reply_idx[MSIxIndex],
2093 instance->reply_post_host_index_addr[MSIxIndex/8]);
2094 else
2095 writel((MSIxIndex << 24) |
2096 fusion->last_reply_idx[MSIxIndex],
2097 instance->reply_post_host_index_addr[0]);
adam radford53ef2bb2011-02-24 20:56:05 -08002098 megasas_check_and_restore_queue_depth(instance);
adam radford9c915a82010-12-21 13:34:31 -08002099 return IRQ_HANDLED;
2100}
2101
2102/**
2103 * megasas_complete_cmd_dpc_fusion - Completes command
2104 * @instance: Adapter soft state
2105 *
2106 * Tasklet to complete cmds
2107 */
2108void
2109megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
2110{
2111 struct megasas_instance *instance =
2112 (struct megasas_instance *)instance_addr;
2113 unsigned long flags;
adam radfordc8e858f2011-10-08 18:15:13 -07002114 u32 count, MSIxIndex;
2115
2116 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
adam radford9c915a82010-12-21 13:34:31 -08002117
2118 /* If we have already declared adapter dead, donot complete cmds */
2119 spin_lock_irqsave(&instance->hba_lock, flags);
2120 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2121 spin_unlock_irqrestore(&instance->hba_lock, flags);
2122 return;
2123 }
2124 spin_unlock_irqrestore(&instance->hba_lock, flags);
2125
adam radfordc8e858f2011-10-08 18:15:13 -07002126 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
2127 complete_cmd_fusion(instance, MSIxIndex);
adam radford9c915a82010-12-21 13:34:31 -08002128}
2129
2130/**
2131 * megasas_isr_fusion - isr entry point
2132 */
2133irqreturn_t megasas_isr_fusion(int irq, void *devp)
2134{
adam radfordc8e858f2011-10-08 18:15:13 -07002135 struct megasas_irq_context *irq_context = devp;
2136 struct megasas_instance *instance = irq_context->instance;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302137 u32 mfiStatus, fw_state, dma_state;
adam radford9c915a82010-12-21 13:34:31 -08002138
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302139 if (instance->mask_interrupts)
2140 return IRQ_NONE;
2141
adam radfordc8e858f2011-10-08 18:15:13 -07002142 if (!instance->msix_vectors) {
adam radford9c915a82010-12-21 13:34:31 -08002143 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
2144 if (!mfiStatus)
2145 return IRQ_NONE;
2146 }
2147
2148 /* If we are resetting, bail */
adam radford6497b242011-10-08 18:14:50 -07002149 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
2150 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08002151 return IRQ_HANDLED;
adam radford6497b242011-10-08 18:14:50 -07002152 }
adam radford9c915a82010-12-21 13:34:31 -08002153
adam radfordc8e858f2011-10-08 18:15:13 -07002154 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
adam radford6497b242011-10-08 18:14:50 -07002155 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08002156 /* If we didn't complete any commands, check for FW fault */
2157 fw_state = instance->instancet->read_fw_status_reg(
2158 instance->reg_set) & MFI_STATE_MASK;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302159 dma_state = instance->instancet->read_fw_status_reg
2160 (instance->reg_set) & MFI_STATE_DMADONE;
2161 if (instance->crash_dump_drv_support &&
2162 instance->crash_dump_app_support) {
2163 /* Start collecting crash, if DMA bit is done */
2164 if ((fw_state == MFI_STATE_FAULT) && dma_state)
2165 schedule_work(&instance->crash_init);
2166 else if (fw_state == MFI_STATE_FAULT)
2167 schedule_work(&instance->work_init);
2168 } else if (fw_state == MFI_STATE_FAULT) {
adam radford229fe472014-03-10 02:51:56 -07002169 printk(KERN_WARNING "megaraid_sas: Iop2SysDoorbellInt"
2170 "for scsi%d\n", instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002171 schedule_work(&instance->work_init);
adam radford229fe472014-03-10 02:51:56 -07002172 }
adam radford9c915a82010-12-21 13:34:31 -08002173 }
2174
2175 return IRQ_HANDLED;
2176}
2177
2178/**
2179 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
2180 * @instance: Adapter soft state
2181 * mfi_cmd: megasas_cmd pointer
2182 *
2183 */
2184u8
2185build_mpt_mfi_pass_thru(struct megasas_instance *instance,
2186 struct megasas_cmd *mfi_cmd)
2187{
2188 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
2189 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
2190 struct megasas_cmd_fusion *cmd;
2191 struct fusion_context *fusion;
2192 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302193 u32 opcode;
adam radford9c915a82010-12-21 13:34:31 -08002194
2195 cmd = megasas_get_cmd_fusion(instance);
2196 if (!cmd)
2197 return 1;
2198
2199 /* Save the smid. To be used for returning the cmd */
2200 mfi_cmd->context.smid = cmd->index;
adam radford9c915a82010-12-21 13:34:31 -08002201 cmd->sync_cmd_idx = mfi_cmd->index;
2202
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302203 /* Set this only for Blocked commands */
2204 opcode = le32_to_cpu(mfi_cmd->frame->dcmd.opcode);
2205 if ((opcode == MR_DCMD_LD_MAP_GET_INFO)
2206 && (mfi_cmd->frame->dcmd.mbox.b[1] == 1))
2207 mfi_cmd->is_wait_event = 1;
2208
2209 if (opcode == MR_DCMD_CTRL_EVENT_WAIT)
2210 mfi_cmd->is_wait_event = 1;
2211
2212 if (mfi_cmd->is_wait_event)
2213 mfi_cmd->mpt_pthr_cmd_blocked = cmd;
2214
adam radford9c915a82010-12-21 13:34:31 -08002215 /*
2216 * For cmds where the flag is set, store the flag and check
2217 * on completion. For cmds with this flag, don't call
2218 * megasas_complete_cmd
2219 */
2220
Sumit.Saxena@avagotech.com6e755dd2014-11-17 15:24:28 +05302221 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
adam radford9c915a82010-12-21 13:34:31 -08002222 cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
2223
2224 fusion = instance->ctrl_context;
2225 io_req = cmd->io_request;
adam radford36807e62011-10-08 18:15:06 -07002226
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05302227 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2228 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07002229 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
2230 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
2231 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2232 sgl_ptr_end->Flags = 0;
2233 }
2234
adam radford9c915a82010-12-21 13:34:31 -08002235 mpi25_ieee_chain =
2236 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
2237
2238 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
2239 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
2240 SGL) / 4;
2241 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
2242
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302243 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08002244
2245 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2246 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
2247
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302248 mpi25_ieee_chain->Length = cpu_to_le32(MEGASAS_MAX_SZ_CHAIN_FRAME);
adam radford9c915a82010-12-21 13:34:31 -08002249
2250 return 0;
2251}
2252
2253/**
2254 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
2255 * @instance: Adapter soft state
2256 * @cmd: mfi cmd to build
2257 *
2258 */
2259union MEGASAS_REQUEST_DESCRIPTOR_UNION *
2260build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
2261{
2262 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2263 u16 index;
2264
2265 if (build_mpt_mfi_pass_thru(instance, cmd)) {
2266 printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
2267 return NULL;
2268 }
2269
2270 index = cmd->context.smid;
2271
2272 req_desc = megasas_get_request_descriptor(instance, index - 1);
2273
2274 if (!req_desc)
2275 return NULL;
2276
2277 req_desc->Words = 0;
2278 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2279 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2280
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302281 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08002282
2283 return req_desc;
2284}
2285
2286/**
2287 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
2288 * @instance: Adapter soft state
2289 * @cmd: mfi cmd pointer
2290 *
2291 */
2292void
2293megasas_issue_dcmd_fusion(struct megasas_instance *instance,
2294 struct megasas_cmd *cmd)
2295{
2296 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
adam radford9c915a82010-12-21 13:34:31 -08002297
2298 req_desc = build_mpt_cmd(instance, cmd);
2299 if (!req_desc) {
2300 printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
2301 return;
2302 }
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302303 atomic_set(&cmd->mfi_mpt_pthr, MFI_MPT_ATTACHED);
adam radford9c915a82010-12-21 13:34:31 -08002304 instance->instancet->fire_cmd(instance, req_desc->u.low,
2305 req_desc->u.high, instance->reg_set);
2306}
2307
2308/**
2309 * megasas_release_fusion - Reverses the FW initialization
2310 * @intance: Adapter soft state
2311 */
2312void
2313megasas_release_fusion(struct megasas_instance *instance)
2314{
2315 megasas_free_cmds(instance);
2316 megasas_free_cmds_fusion(instance);
2317
2318 iounmap(instance->reg_set);
2319
2320 pci_release_selected_regions(instance->pdev, instance->bar);
2321}
2322
2323/**
2324 * megasas_read_fw_status_reg_fusion - returns the current FW status value
2325 * @regs: MFI register set
2326 */
2327static u32
2328megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
2329{
2330 return readl(&(regs)->outbound_scratch_pad);
2331}
2332
2333/**
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302334 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
2335 * @instance: Controller's soft instance
2336 * return: Number of allocated host crash buffers
2337 */
2338static void
2339megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
2340{
2341 unsigned int i;
2342
2343 instance->crash_buf_pages = get_order(CRASH_DMA_BUF_SIZE);
2344 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
2345 instance->crash_buf[i] = (void *)__get_free_pages(GFP_KERNEL,
2346 instance->crash_buf_pages);
2347 if (!instance->crash_buf[i]) {
2348 dev_info(&instance->pdev->dev, "Firmware crash dump "
2349 "memory allocation failed at index %d\n", i);
2350 break;
2351 }
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05302352 memset(instance->crash_buf[i], 0,
2353 ((1 << PAGE_SHIFT) << instance->crash_buf_pages));
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302354 }
2355 instance->drv_buf_alloc = i;
2356}
2357
2358/**
2359 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
2360 * @instance: Controller's soft instance
2361 */
2362void
2363megasas_free_host_crash_buffer(struct megasas_instance *instance)
2364{
2365 unsigned int i
2366;
2367 for (i = 0; i < instance->drv_buf_alloc; i++) {
2368 if (instance->crash_buf[i])
2369 free_pages((ulong)instance->crash_buf[i],
2370 instance->crash_buf_pages);
2371 }
2372 instance->drv_buf_index = 0;
2373 instance->drv_buf_alloc = 0;
2374 instance->fw_crash_state = UNAVAILABLE;
2375 instance->fw_crash_buffer_size = 0;
2376}
2377
2378/**
adam radford9c915a82010-12-21 13:34:31 -08002379 * megasas_adp_reset_fusion - For controller reset
2380 * @regs: MFI register set
2381 */
2382static int
2383megasas_adp_reset_fusion(struct megasas_instance *instance,
2384 struct megasas_register_set __iomem *regs)
2385{
2386 return 0;
2387}
2388
2389/**
2390 * megasas_check_reset_fusion - For controller reset check
2391 * @regs: MFI register set
2392 */
2393static int
2394megasas_check_reset_fusion(struct megasas_instance *instance,
2395 struct megasas_register_set __iomem *regs)
2396{
2397 return 0;
2398}
2399
2400/* This function waits for outstanding commands on fusion to complete */
adam radford229fe472014-03-10 02:51:56 -07002401int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
2402 int iotimeout, int *convert)
adam radford9c915a82010-12-21 13:34:31 -08002403{
adam radford229fe472014-03-10 02:51:56 -07002404 int i, outstanding, retval = 0, hb_seconds_missed = 0;
adam radfordc007b8b2012-07-17 18:20:24 -07002405 u32 fw_state;
adam radford9c915a82010-12-21 13:34:31 -08002406
adam radfordc007b8b2012-07-17 18:20:24 -07002407 for (i = 0; i < resetwaittime; i++) {
adam radford9c915a82010-12-21 13:34:31 -08002408 /* Check if firmware is in fault state */
2409 fw_state = instance->instancet->read_fw_status_reg(
2410 instance->reg_set) & MFI_STATE_MASK;
2411 if (fw_state == MFI_STATE_FAULT) {
2412 printk(KERN_WARNING "megasas: Found FW in FAULT state,"
adam radford229fe472014-03-10 02:51:56 -07002413 " will reset adapter scsi%d.\n",
2414 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002415 retval = 1;
2416 goto out;
2417 }
adam radford229fe472014-03-10 02:51:56 -07002418 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
2419 if (instance->requestorId && !iotimeout) {
2420 retval = 1;
2421 goto out;
2422 }
2423
2424 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
2425 if (instance->requestorId && iotimeout) {
2426 if (instance->hb_host_mem->HB.fwCounter !=
2427 instance->hb_host_mem->HB.driverCounter) {
2428 instance->hb_host_mem->HB.driverCounter =
2429 instance->hb_host_mem->HB.fwCounter;
2430 hb_seconds_missed = 0;
2431 } else {
2432 hb_seconds_missed++;
2433 if (hb_seconds_missed ==
2434 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
2435 printk(KERN_WARNING "megasas: SR-IOV:"
2436 " Heartbeat never completed "
2437 " while polling during I/O "
2438 " timeout handling for "
2439 "scsi%d.\n",
2440 instance->host->host_no);
2441 *convert = 1;
2442 retval = 1;
2443 goto out;
2444 }
2445 }
2446 }
adam radford9c915a82010-12-21 13:34:31 -08002447
2448 outstanding = atomic_read(&instance->fw_outstanding);
2449 if (!outstanding)
2450 goto out;
2451
2452 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2453 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
adam radford229fe472014-03-10 02:51:56 -07002454 "commands to complete for scsi%d\n", i,
2455 outstanding, instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002456 megasas_complete_cmd_dpc_fusion(
2457 (unsigned long)instance);
2458 }
2459 msleep(1000);
2460 }
2461
2462 if (atomic_read(&instance->fw_outstanding)) {
2463 printk("megaraid_sas: pending commands remain after waiting, "
adam radford229fe472014-03-10 02:51:56 -07002464 "will reset adapter scsi%d.\n",
2465 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002466 retval = 1;
2467 }
2468out:
2469 return retval;
2470}
2471
2472void megasas_reset_reply_desc(struct megasas_instance *instance)
2473{
adam radfordc8e858f2011-10-08 18:15:13 -07002474 int i, count;
adam radford9c915a82010-12-21 13:34:31 -08002475 struct fusion_context *fusion;
2476 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2477
2478 fusion = instance->ctrl_context;
adam radfordc8e858f2011-10-08 18:15:13 -07002479 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2480 for (i = 0 ; i < count ; i++)
2481 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -08002482 reply_desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -07002483 for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
adam radford9c915a82010-12-21 13:34:31 -08002484 reply_desc->Words = ULLONG_MAX;
2485}
2486
adam radford229fe472014-03-10 02:51:56 -07002487/* Check for a second path that is currently UP */
2488int megasas_check_mpio_paths(struct megasas_instance *instance,
2489 struct scsi_cmnd *scmd)
adam radford9c915a82010-12-21 13:34:31 -08002490{
adam radford229fe472014-03-10 02:51:56 -07002491 int i, j, retval = (DID_RESET << 16);
2492
2493 if (instance->mpio && instance->requestorId) {
2494 for (i = 0 ; i < MAX_MGMT_ADAPTERS ; i++)
2495 for (j = 0 ; j < MAX_LOGICAL_DRIVES; j++)
2496 if (megasas_mgmt_info.instance[i] &&
2497 (megasas_mgmt_info.instance[i] != instance) &&
2498 megasas_mgmt_info.instance[i]->mpio &&
2499 megasas_mgmt_info.instance[i]->requestorId
2500 &&
2501 (megasas_mgmt_info.instance[i]->ld_ids[j]
2502 == scmd->device->id)) {
2503 retval = (DID_NO_CONNECT << 16);
2504 goto out;
2505 }
2506 }
2507out:
2508 return retval;
2509}
2510
2511/* Core fusion reset function */
2512int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout)
2513{
2514 int retval = SUCCESS, i, j, retry = 0, convert = 0;
adam radford9c915a82010-12-21 13:34:31 -08002515 struct megasas_instance *instance;
2516 struct megasas_cmd_fusion *cmd_fusion;
2517 struct fusion_context *fusion;
2518 struct megasas_cmd *cmd_mfi;
2519 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
adam radford7e70e732011-05-11 18:34:08 -07002520 u32 host_diag, abs_state, status_reg, reset_adapter;
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302521 u32 io_timeout_in_crash_mode = 0;
adam radford9c915a82010-12-21 13:34:31 -08002522
2523 instance = (struct megasas_instance *)shost->hostdata;
2524 fusion = instance->ctrl_context;
2525
adam radford229fe472014-03-10 02:51:56 -07002526 mutex_lock(&instance->reset_mutex);
2527
adam radford9c915a82010-12-21 13:34:31 -08002528 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2529 printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
adam radford229fe472014-03-10 02:51:56 -07002530 "returning FAILED for scsi%d.\n",
2531 instance->host->host_no);
Adam Radforda2fbcbc2014-07-09 15:17:54 -07002532 mutex_unlock(&instance->reset_mutex);
adam radfordd4a759a2011-10-08 18:14:39 -07002533 return FAILED;
adam radford9c915a82010-12-21 13:34:31 -08002534 }
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302535 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
2536 abs_state = status_reg & MFI_STATE_MASK;
2537
2538 /* IO timeout detected, forcibly put FW in FAULT state */
2539 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
2540 instance->crash_dump_app_support && iotimeout) {
2541 dev_info(&instance->pdev->dev, "IO timeout is detected, "
2542 "forcibly FAULT Firmware\n");
2543 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2544 status_reg = readl(&instance->reg_set->doorbell);
2545 writel(status_reg | MFI_STATE_FORCE_OCR,
2546 &instance->reg_set->doorbell);
2547 readl(&instance->reg_set->doorbell);
2548 mutex_unlock(&instance->reset_mutex);
2549 do {
2550 ssleep(3);
2551 io_timeout_in_crash_mode++;
2552 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
2553 "seconds for crash dump collection and OCR "
2554 "to be done\n", (io_timeout_in_crash_mode * 3));
2555 } while ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
2556 (io_timeout_in_crash_mode < 80));
2557
2558 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
2559 dev_info(&instance->pdev->dev, "OCR done for IO "
2560 "timeout case\n");
2561 retval = SUCCESS;
2562 } else {
2563 dev_info(&instance->pdev->dev, "Controller is not "
2564 "operational after 240 seconds wait for IO "
2565 "timeout case in FW crash dump mode\n do "
2566 "OCR/kill adapter\n");
2567 retval = megasas_reset_fusion(shost, 0);
2568 }
2569 return retval;
2570 }
adam radford9c915a82010-12-21 13:34:31 -08002571
adam radford229fe472014-03-10 02:51:56 -07002572 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
2573 del_timer_sync(&instance->sriov_heartbeat_timer);
adam radford7e70e732011-05-11 18:34:08 -07002574 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
adam radford229fe472014-03-10 02:51:56 -07002575 instance->adprecovery = MEGASAS_ADPRESET_SM_POLLING;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302576 instance->instancet->disable_intr(instance);
adam radford7e70e732011-05-11 18:34:08 -07002577 msleep(1000);
2578
adam radford9c915a82010-12-21 13:34:31 -08002579 /* First try waiting for commands to complete */
adam radford229fe472014-03-10 02:51:56 -07002580 if (megasas_wait_for_outstanding_fusion(instance, iotimeout,
2581 &convert)) {
2582 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
adam radford9c915a82010-12-21 13:34:31 -08002583 printk(KERN_WARNING "megaraid_sas: resetting fusion "
adam radford229fe472014-03-10 02:51:56 -07002584 "adapter scsi%d.\n", instance->host->host_no);
2585 if (convert)
2586 iotimeout = 0;
2587
adam radford9c915a82010-12-21 13:34:31 -08002588 /* Now return commands back to the OS */
2589 for (i = 0 ; i < instance->max_fw_cmds; i++) {
2590 cmd_fusion = fusion->cmd_list[i];
2591 if (cmd_fusion->scmd) {
2592 scsi_dma_unmap(cmd_fusion->scmd);
adam radford229fe472014-03-10 02:51:56 -07002593 cmd_fusion->scmd->result =
2594 megasas_check_mpio_paths(instance,
2595 cmd_fusion->scmd);
adam radford9c915a82010-12-21 13:34:31 -08002596 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2597 megasas_return_cmd_fusion(instance, cmd_fusion);
2598 atomic_dec(&instance->fw_outstanding);
2599 }
2600 }
2601
adam radford7e70e732011-05-11 18:34:08 -07002602 status_reg = instance->instancet->read_fw_status_reg(
2603 instance->reg_set);
2604 abs_state = status_reg & MFI_STATE_MASK;
2605 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2606 if (instance->disableOnlineCtrlReset ||
2607 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
adam radford9c915a82010-12-21 13:34:31 -08002608 /* Reset not supported, kill adapter */
2609 printk(KERN_WARNING "megaraid_sas: Reset not supported"
adam radford229fe472014-03-10 02:51:56 -07002610 ", killing adapter scsi%d.\n",
2611 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002612 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07002613 instance->skip_heartbeat_timer_del = 1;
adam radford9c915a82010-12-21 13:34:31 -08002614 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2615 retval = FAILED;
2616 goto out;
2617 }
2618
adam radford229fe472014-03-10 02:51:56 -07002619 /* Let SR-IOV VF & PF sync up if there was a HB failure */
2620 if (instance->requestorId && !iotimeout) {
2621 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
2622 /* Look for a late HB update after VF settle time */
2623 if (abs_state == MFI_STATE_OPERATIONAL &&
2624 (instance->hb_host_mem->HB.fwCounter !=
2625 instance->hb_host_mem->HB.driverCounter)) {
2626 instance->hb_host_mem->HB.driverCounter =
2627 instance->hb_host_mem->HB.fwCounter;
2628 printk(KERN_WARNING "megasas: SR-IOV:"
2629 "Late FW heartbeat update for "
2630 "scsi%d.\n",
2631 instance->host->host_no);
2632 } else {
2633 /* In VF mode, first poll for FW ready */
2634 for (i = 0;
2635 i < (MEGASAS_RESET_WAIT_TIME * 1000);
2636 i += 20) {
2637 status_reg =
2638 instance->instancet->
2639 read_fw_status_reg(
2640 instance->reg_set);
2641 abs_state = status_reg &
2642 MFI_STATE_MASK;
2643 if (abs_state == MFI_STATE_READY) {
2644 printk(KERN_WARNING "megasas"
2645 ": SR-IOV: FW was found"
2646 "to be in ready state "
2647 "for scsi%d.\n",
2648 instance->host->host_no);
2649 break;
2650 }
2651 msleep(20);
2652 }
2653 if (abs_state != MFI_STATE_READY) {
2654 printk(KERN_WARNING "megasas: SR-IOV: "
2655 "FW not in ready state after %d"
2656 " seconds for scsi%d, status_reg = "
2657 "0x%x.\n",
2658 MEGASAS_RESET_WAIT_TIME,
2659 instance->host->host_no,
2660 status_reg);
2661 megaraid_sas_kill_hba(instance);
2662 instance->skip_heartbeat_timer_del = 1;
2663 instance->adprecovery =
2664 MEGASAS_HW_CRITICAL_ERROR;
2665 retval = FAILED;
2666 goto out;
2667 }
2668 }
2669 }
2670
adam radford9c915a82010-12-21 13:34:31 -08002671 /* Now try to reset the chip */
2672 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2673 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2674 &instance->reg_set->fusion_seq_offset);
2675 writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2676 &instance->reg_set->fusion_seq_offset);
2677 writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2678 &instance->reg_set->fusion_seq_offset);
2679 writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2680 &instance->reg_set->fusion_seq_offset);
2681 writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2682 &instance->reg_set->fusion_seq_offset);
2683 writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2684 &instance->reg_set->fusion_seq_offset);
2685 writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2686 &instance->reg_set->fusion_seq_offset);
2687
2688 /* Check that the diag write enable (DRWE) bit is on */
2689 host_diag = readl(&instance->reg_set->fusion_host_diag);
adam radford7e70e732011-05-11 18:34:08 -07002690 retry = 0;
adam radford9c915a82010-12-21 13:34:31 -08002691 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2692 msleep(100);
2693 host_diag =
2694 readl(&instance->reg_set->fusion_host_diag);
2695 if (retry++ == 100) {
2696 printk(KERN_WARNING "megaraid_sas: "
adam radford229fe472014-03-10 02:51:56 -07002697 "Host diag unlock failed! "
2698 "for scsi%d\n",
2699 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002700 break;
2701 }
2702 }
2703 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2704 continue;
2705
2706 /* Send chip reset command */
2707 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2708 &instance->reg_set->fusion_host_diag);
2709 msleep(3000);
2710
2711 /* Make sure reset adapter bit is cleared */
2712 host_diag = readl(&instance->reg_set->fusion_host_diag);
2713 retry = 0;
2714 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2715 msleep(100);
2716 host_diag =
2717 readl(&instance->reg_set->fusion_host_diag);
2718 if (retry++ == 1000) {
2719 printk(KERN_WARNING "megaraid_sas: "
2720 "Diag reset adapter never "
adam radford229fe472014-03-10 02:51:56 -07002721 "cleared for scsi%d!\n",
2722 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002723 break;
2724 }
2725 }
2726 if (host_diag & HOST_DIAG_RESET_ADAPTER)
2727 continue;
2728
2729 abs_state =
2730 instance->instancet->read_fw_status_reg(
adam radford7e70e732011-05-11 18:34:08 -07002731 instance->reg_set) & MFI_STATE_MASK;
adam radford9c915a82010-12-21 13:34:31 -08002732 retry = 0;
2733
2734 while ((abs_state <= MFI_STATE_FW_INIT) &&
2735 (retry++ < 1000)) {
2736 msleep(100);
2737 abs_state =
2738 instance->instancet->read_fw_status_reg(
adam radford7e70e732011-05-11 18:34:08 -07002739 instance->reg_set) & MFI_STATE_MASK;
adam radford9c915a82010-12-21 13:34:31 -08002740 }
2741 if (abs_state <= MFI_STATE_FW_INIT) {
2742 printk(KERN_WARNING "megaraid_sas: firmware "
2743 "state < MFI_STATE_FW_INIT, state = "
adam radford229fe472014-03-10 02:51:56 -07002744 "0x%x for scsi%d\n", abs_state,
2745 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002746 continue;
2747 }
2748
2749 /* Wait for FW to become ready */
adam radford058a8fa2011-10-08 18:14:27 -07002750 if (megasas_transition_to_ready(instance, 1)) {
adam radford9c915a82010-12-21 13:34:31 -08002751 printk(KERN_WARNING "megaraid_sas: Failed to "
adam radford229fe472014-03-10 02:51:56 -07002752 "transition controller to ready "
2753 "for scsi%d.\n",
2754 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002755 continue;
2756 }
2757
2758 megasas_reset_reply_desc(instance);
2759 if (megasas_ioc_init_fusion(instance)) {
2760 printk(KERN_WARNING "megaraid_sas: "
adam radford229fe472014-03-10 02:51:56 -07002761 "megasas_ioc_init_fusion() failed!"
2762 " for scsi%d\n",
2763 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002764 continue;
2765 }
2766
adam radford9c915a82010-12-21 13:34:31 -08002767 /* Re-fire management commands */
2768 for (j = 0 ; j < instance->max_fw_cmds; j++) {
2769 cmd_fusion = fusion->cmd_list[j];
2770 if (cmd_fusion->sync_cmd_idx !=
2771 (u32)ULONG_MAX) {
2772 cmd_mfi =
2773 instance->
2774 cmd_list[cmd_fusion->sync_cmd_idx];
2775 if (cmd_mfi->frame->dcmd.opcode ==
Sumit.Saxena@lsi.combe263742014-02-12 23:37:46 +05302776 cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO)) {
Sumit.Saxena@avagotech.com90dc9d92014-09-12 18:57:58 +05302777 megasas_return_mfi_mpt_pthr(instance, cmd_mfi, cmd_fusion);
adam radford9c915a82010-12-21 13:34:31 -08002778 } else {
2779 req_desc =
2780 megasas_get_request_descriptor(
2781 instance,
2782 cmd_mfi->context.smid
2783 -1);
adam radford3d0c24c2014-03-10 02:51:45 -07002784 if (!req_desc) {
adam radford9c915a82010-12-21 13:34:31 -08002785 printk(KERN_WARNING
2786 "req_desc NULL"
adam radford229fe472014-03-10 02:51:56 -07002787 " for scsi%d\n",
2788 instance->host->host_no);
adam radford3d0c24c2014-03-10 02:51:45 -07002789 /* Return leaked MPT
2790 frame */
2791 megasas_return_cmd_fusion(instance, cmd_fusion);
2792 } else {
adam radford9c915a82010-12-21 13:34:31 -08002793 instance->instancet->
2794 fire_cmd(instance,
2795 req_desc->
2796 u.low,
2797 req_desc->
2798 u.high,
2799 instance->
2800 reg_set);
2801 }
2802 }
2803 }
2804 }
2805
Sumit.Saxena@avagotech.comd009b572014-11-17 15:24:13 +05302806 if (megasas_get_ctrl_info(instance)) {
2807 dev_info(&instance->pdev->dev,
2808 "Failed from %s %d\n",
2809 __func__, __LINE__);
2810 instance->adprecovery =
2811 MEGASAS_HW_CRITICAL_ERROR;
2812 megaraid_sas_kill_hba(instance);
2813 retval = FAILED;
2814 }
adam radford9c915a82010-12-21 13:34:31 -08002815 /* Reset load balance info */
2816 memset(fusion->load_balance_info, 0,
2817 sizeof(struct LD_LOAD_BALANCE_INFO)
Sumit.Saxena@avagotech.com51087a82014-09-12 18:57:33 +05302818 *MAX_LOGICAL_DRIVES_EXT);
adam radford9c915a82010-12-21 13:34:31 -08002819
2820 if (!megasas_get_map_info(instance))
2821 megasas_sync_map_info(instance);
2822
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05302823 clear_bit(MEGASAS_FUSION_IN_RESET,
2824 &instance->reset_flags);
2825 instance->instancet->enable_intr(instance);
2826 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2827
adam radford229fe472014-03-10 02:51:56 -07002828 /* Restart SR-IOV heartbeat */
2829 if (instance->requestorId) {
2830 if (!megasas_sriov_start_heartbeat(instance, 0))
2831 megasas_start_timer(instance,
2832 &instance->sriov_heartbeat_timer,
2833 megasas_sriov_heartbeat_handler,
2834 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2835 else
2836 instance->skip_heartbeat_timer_del = 1;
2837 }
2838
adam radford9c915a82010-12-21 13:34:31 -08002839 /* Adapter reset completed successfully */
2840 printk(KERN_WARNING "megaraid_sas: Reset "
adam radford229fe472014-03-10 02:51:56 -07002841 "successful for scsi%d.\n",
2842 instance->host->host_no);
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302843
Sumit.Saxena@avagotech.comac340c52014-11-17 15:24:18 +05302844 if (instance->crash_dump_drv_support &&
2845 instance->crash_dump_app_support)
2846 megasas_set_crash_dump_params(instance,
2847 MR_CRASH_BUF_TURN_ON);
2848 else
2849 megasas_set_crash_dump_params(instance,
2850 MR_CRASH_BUF_TURN_OFF);
2851
adam radford9c915a82010-12-21 13:34:31 -08002852 retval = SUCCESS;
2853 goto out;
2854 }
2855 /* Reset failed, kill the adapter */
2856 printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
adam radford229fe472014-03-10 02:51:56 -07002857 "adapter scsi%d.\n", instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002858 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07002859 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05302860 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
adam radford9c915a82010-12-21 13:34:31 -08002861 retval = FAILED;
2862 } else {
adam radford229fe472014-03-10 02:51:56 -07002863 /* For VF: Restart HB timer if we didn't OCR */
2864 if (instance->requestorId) {
2865 megasas_start_timer(instance,
2866 &instance->sriov_heartbeat_timer,
2867 megasas_sriov_heartbeat_handler,
2868 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2869 }
adam radford3f0e58bc2011-10-08 18:14:59 -07002870 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302871 instance->instancet->enable_intr(instance);
adam radford9c915a82010-12-21 13:34:31 -08002872 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2873 }
2874out:
2875 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2876 mutex_unlock(&instance->reset_mutex);
2877 return retval;
2878}
2879
Sumit.Saxena@avagotech.comfc62b3f2014-09-12 18:57:28 +05302880/* Fusion Crash dump collection work queue */
2881void megasas_fusion_crash_dump_wq(struct work_struct *work)
2882{
2883 struct megasas_instance *instance =
2884 container_of(work, struct megasas_instance, crash_init);
2885 u32 status_reg;
2886 u8 partial_copy = 0;
2887
2888
2889 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
2890
2891 /*
2892 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
2893 * to host crash buffers
2894 */
2895 if (instance->drv_buf_index == 0) {
2896 /* Buffer is already allocated for old Crash dump.
2897 * Do OCR and do not wait for crash dump collection
2898 */
2899 if (instance->drv_buf_alloc) {
2900 dev_info(&instance->pdev->dev, "earlier crash dump is "
2901 "not yet copied by application, ignoring this "
2902 "crash dump and initiating OCR\n");
2903 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
2904 writel(status_reg,
2905 &instance->reg_set->outbound_scratch_pad);
2906 readl(&instance->reg_set->outbound_scratch_pad);
2907 return;
2908 }
2909 megasas_alloc_host_crash_buffer(instance);
2910 dev_info(&instance->pdev->dev, "Number of host crash buffers "
2911 "allocated: %d\n", instance->drv_buf_alloc);
2912 }
2913
2914 /*
2915 * Driver has allocated max buffers, which can be allocated
2916 * and FW has more crash dump data, then driver will
2917 * ignore the data.
2918 */
2919 if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
2920 dev_info(&instance->pdev->dev, "Driver is done copying "
2921 "the buffer: %d\n", instance->drv_buf_alloc);
2922 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
2923 partial_copy = 1;
2924 } else {
2925 memcpy(instance->crash_buf[instance->drv_buf_index],
2926 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
2927 instance->drv_buf_index++;
2928 status_reg &= ~MFI_STATE_DMADONE;
2929 }
2930
2931 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
2932 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
2933 "of copied buffers: %d\n", instance->drv_buf_index);
2934 instance->fw_crash_buffer_size = instance->drv_buf_index;
2935 instance->fw_crash_state = AVAILABLE;
2936 instance->drv_buf_index = 0;
2937 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
2938 readl(&instance->reg_set->outbound_scratch_pad);
2939 if (!partial_copy)
2940 megasas_reset_fusion(instance->host, 0);
2941 } else {
2942 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
2943 readl(&instance->reg_set->outbound_scratch_pad);
2944 }
2945}
2946
2947
adam radford9c915a82010-12-21 13:34:31 -08002948/* Fusion OCR work queue */
2949void megasas_fusion_ocr_wq(struct work_struct *work)
2950{
2951 struct megasas_instance *instance =
2952 container_of(work, struct megasas_instance, work_init);
2953
adam radford229fe472014-03-10 02:51:56 -07002954 megasas_reset_fusion(instance->host, 0);
adam radford9c915a82010-12-21 13:34:31 -08002955}
2956
2957struct megasas_instance_template megasas_instance_template_fusion = {
2958 .fire_cmd = megasas_fire_cmd_fusion,
2959 .enable_intr = megasas_enable_intr_fusion,
2960 .disable_intr = megasas_disable_intr_fusion,
2961 .clear_intr = megasas_clear_intr_fusion,
2962 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2963 .adp_reset = megasas_adp_reset_fusion,
2964 .check_reset = megasas_check_reset_fusion,
2965 .service_isr = megasas_isr_fusion,
2966 .tasklet = megasas_complete_cmd_dpc_fusion,
2967 .init_adapter = megasas_init_adapter_fusion,
2968 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2969 .issue_dcmd = megasas_issue_dcmd_fusion,
2970};