blob: 3ed03dfab76c4997eadb43aed51be2dbad3837a3 [file] [log] [blame]
adam radford9c915a82010-12-21 13:34:31 -08001/*
2 * Linux MegaRAID driver for SAS based RAID controllers
3 *
adam radfordae590572012-10-01 19:27:34 -07004 * Copyright (c) 2009-2012 LSI Corporation.
adam radford9c915a82010-12-21 13:34:31 -08005 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * FILE: megaraid_sas_fusion.c
21 *
22 * Authors: LSI Corporation
23 * Sumant Patro
24 * Adam Radford <linuxraid@lsi.com>
25 *
26 * Send feedback to: <megaraidlinux@lsi.com>
27 *
28 * Mail to: LSI Corporation, 1621 Barber Lane, Milpitas, CA 95035
29 * ATTN: Linuxraid
30 */
31
32#include <linux/kernel.h>
33#include <linux/types.h>
34#include <linux/pci.h>
35#include <linux/list.h>
36#include <linux/moduleparam.h>
37#include <linux/module.h>
38#include <linux/spinlock.h>
39#include <linux/interrupt.h>
40#include <linux/delay.h>
adam radford9c915a82010-12-21 13:34:31 -080041#include <linux/uio.h>
42#include <linux/uaccess.h>
43#include <linux/fs.h>
44#include <linux/compat.h>
45#include <linux/blkdev.h>
46#include <linux/mutex.h>
47#include <linux/poll.h>
48
49#include <scsi/scsi.h>
50#include <scsi/scsi_cmnd.h>
51#include <scsi/scsi_device.h>
52#include <scsi/scsi_host.h>
53
54#include "megaraid_sas_fusion.h"
55#include "megaraid_sas.h"
56
57extern void megasas_free_cmds(struct megasas_instance *instance);
58extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
59 *instance);
60extern void
61megasas_complete_cmd(struct megasas_instance *instance,
62 struct megasas_cmd *cmd, u8 alt_status);
63int megasas_is_ldio(struct scsi_cmnd *cmd);
64int
adam radford229fe472014-03-10 02:51:56 -070065wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
66 int seconds);
adam radford9c915a82010-12-21 13:34:31 -080067
68void
69megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
70int megasas_alloc_cmds(struct megasas_instance *instance);
71int
72megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
73int
74megasas_issue_polled(struct megasas_instance *instance,
75 struct megasas_cmd *cmd);
adam radford53ef2bb2011-02-24 20:56:05 -080076void
77megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
78
adam radford9c915a82010-12-21 13:34:31 -080079u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo,
80 struct IO_REQUEST_INFO *in_info);
adam radford058a8fa2011-10-08 18:14:27 -070081int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
adam radford9c915a82010-12-21 13:34:31 -080082void megaraid_sas_kill_hba(struct megasas_instance *instance);
83
84extern u32 megasas_dbg_lvl;
adam radford229fe472014-03-10 02:51:56 -070085void megasas_sriov_heartbeat_handler(unsigned long instance_addr);
86int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
87 int initial);
88void megasas_start_timer(struct megasas_instance *instance,
89 struct timer_list *timer,
90 void *fn, unsigned long interval);
91extern struct megasas_mgmt_info megasas_mgmt_info;
adam radfordc007b8b2012-07-17 18:20:24 -070092extern int resetwaittime;
adam radford9c915a82010-12-21 13:34:31 -080093
94/**
95 * megasas_enable_intr_fusion - Enables interrupts
96 * @regs: MFI register set
97 */
98void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +053099megasas_enable_intr_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800100{
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530101 struct megasas_register_set __iomem *regs;
102 regs = instance->reg_set;
adam radford6497b242011-10-08 18:14:50 -0700103 /* For Thunderbolt/Invader also clear intr on enable */
104 writel(~0, &regs->outbound_intr_status);
105 readl(&regs->outbound_intr_status);
106
adam radford9c915a82010-12-21 13:34:31 -0800107 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
108
109 /* Dummy readl to force pci flush */
110 readl(&regs->outbound_intr_mask);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530111 instance->mask_interrupts = 0;
adam radford9c915a82010-12-21 13:34:31 -0800112}
113
114/**
115 * megasas_disable_intr_fusion - Disables interrupt
116 * @regs: MFI register set
117 */
118void
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530119megasas_disable_intr_fusion(struct megasas_instance *instance)
adam radford9c915a82010-12-21 13:34:31 -0800120{
121 u32 mask = 0xFFFFFFFF;
122 u32 status;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530123 struct megasas_register_set __iomem *regs;
124 regs = instance->reg_set;
125 instance->mask_interrupts = 1;
adam radford9c915a82010-12-21 13:34:31 -0800126
127 writel(mask, &regs->outbound_intr_mask);
128 /* Dummy readl to force pci flush */
129 status = readl(&regs->outbound_intr_mask);
130}
131
132int
133megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
134{
135 u32 status;
136 /*
137 * Check if it is our interrupt
138 */
139 status = readl(&regs->outbound_intr_status);
140
141 if (status & 1) {
142 writel(status, &regs->outbound_intr_status);
143 readl(&regs->outbound_intr_status);
144 return 1;
145 }
146 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
147 return 0;
148
adam radford9c915a82010-12-21 13:34:31 -0800149 return 1;
150}
151
152/**
153 * megasas_get_cmd_fusion - Get a command from the free pool
154 * @instance: Adapter soft state
155 *
156 * Returns a free command from the pool
157 */
158struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
159 *instance)
160{
161 unsigned long flags;
162 struct fusion_context *fusion =
163 (struct fusion_context *)instance->ctrl_context;
164 struct megasas_cmd_fusion *cmd = NULL;
165
166 spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
167
168 if (!list_empty(&fusion->cmd_pool)) {
169 cmd = list_entry((&fusion->cmd_pool)->next,
170 struct megasas_cmd_fusion, list);
171 list_del_init(&cmd->list);
172 } else {
173 printk(KERN_ERR "megasas: Command pool (fusion) empty!\n");
174 }
175
176 spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
177 return cmd;
178}
179
180/**
181 * megasas_return_cmd_fusion - Return a cmd to free command pool
182 * @instance: Adapter soft state
183 * @cmd: Command packet to be returned to free command pool
184 */
185static inline void
186megasas_return_cmd_fusion(struct megasas_instance *instance,
187 struct megasas_cmd_fusion *cmd)
188{
189 unsigned long flags;
190 struct fusion_context *fusion =
191 (struct fusion_context *)instance->ctrl_context;
192
193 spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
194
195 cmd->scmd = NULL;
196 cmd->sync_cmd_idx = (u32)ULONG_MAX;
197 list_add_tail(&cmd->list, &fusion->cmd_pool);
198
199 spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
200}
201
202/**
203 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
204 * @instance: Adapter soft state
205 */
206static void megasas_teardown_frame_pool_fusion(
207 struct megasas_instance *instance)
208{
209 int i;
210 struct fusion_context *fusion = instance->ctrl_context;
211
212 u16 max_cmd = instance->max_fw_cmds;
213
214 struct megasas_cmd_fusion *cmd;
215
216 if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
217 printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
218 "sense pool : %p\n", fusion->sg_dma_pool,
219 fusion->sense_dma_pool);
220 return;
221 }
222
223 /*
224 * Return all frames to pool
225 */
226 for (i = 0; i < max_cmd; i++) {
227
228 cmd = fusion->cmd_list[i];
229
230 if (cmd->sg_frame)
231 pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
232 cmd->sg_frame_phys_addr);
233
234 if (cmd->sense)
235 pci_pool_free(fusion->sense_dma_pool, cmd->sense,
236 cmd->sense_phys_addr);
237 }
238
239 /*
240 * Now destroy the pool itself
241 */
242 pci_pool_destroy(fusion->sg_dma_pool);
243 pci_pool_destroy(fusion->sense_dma_pool);
244
245 fusion->sg_dma_pool = NULL;
246 fusion->sense_dma_pool = NULL;
247}
248
249/**
250 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
251 * @instance: Adapter soft state
252 */
253void
254megasas_free_cmds_fusion(struct megasas_instance *instance)
255{
256 int i;
257 struct fusion_context *fusion = instance->ctrl_context;
258
259 u32 max_cmds, req_sz, reply_sz, io_frames_sz;
260
261
262 req_sz = fusion->request_alloc_sz;
263 reply_sz = fusion->reply_alloc_sz;
264 io_frames_sz = fusion->io_frames_alloc_sz;
265
266 max_cmds = instance->max_fw_cmds;
267
268 /* Free descriptors and request Frames memory */
269 if (fusion->req_frames_desc)
270 dma_free_coherent(&instance->pdev->dev, req_sz,
271 fusion->req_frames_desc,
272 fusion->req_frames_desc_phys);
273
274 if (fusion->reply_frames_desc) {
275 pci_pool_free(fusion->reply_frames_desc_pool,
276 fusion->reply_frames_desc,
277 fusion->reply_frames_desc_phys);
278 pci_pool_destroy(fusion->reply_frames_desc_pool);
279 }
280
281 if (fusion->io_request_frames) {
282 pci_pool_free(fusion->io_request_frames_pool,
283 fusion->io_request_frames,
284 fusion->io_request_frames_phys);
285 pci_pool_destroy(fusion->io_request_frames_pool);
286 }
287
288 /* Free the Fusion frame pool */
289 megasas_teardown_frame_pool_fusion(instance);
290
291 /* Free all the commands in the cmd_list */
292 for (i = 0; i < max_cmds; i++)
293 kfree(fusion->cmd_list[i]);
294
295 /* Free the cmd_list buffer itself */
296 kfree(fusion->cmd_list);
297 fusion->cmd_list = NULL;
298
299 INIT_LIST_HEAD(&fusion->cmd_pool);
300}
301
302/**
303 * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
304 * @instance: Adapter soft state
305 *
306 */
307static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
308{
309 int i;
310 u32 max_cmd;
311 struct fusion_context *fusion;
312 struct megasas_cmd_fusion *cmd;
313 u32 total_sz_chain_frame;
314
315 fusion = instance->ctrl_context;
316 max_cmd = instance->max_fw_cmds;
317
318 total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
319
320 /*
321 * Use DMA pool facility provided by PCI layer
322 */
323
324 fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
325 instance->pdev,
326 total_sz_chain_frame, 4,
327 0);
328 if (!fusion->sg_dma_pool) {
329 printk(KERN_DEBUG "megasas: failed to setup request pool "
330 "fusion\n");
331 return -ENOMEM;
332 }
333 fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
334 instance->pdev,
335 SCSI_SENSE_BUFFERSIZE, 64, 0);
336
337 if (!fusion->sense_dma_pool) {
338 printk(KERN_DEBUG "megasas: failed to setup sense pool "
339 "fusion\n");
340 pci_pool_destroy(fusion->sg_dma_pool);
341 fusion->sg_dma_pool = NULL;
342 return -ENOMEM;
343 }
344
345 /*
346 * Allocate and attach a frame to each of the commands in cmd_list
347 */
348 for (i = 0; i < max_cmd; i++) {
349
350 cmd = fusion->cmd_list[i];
351
352 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
353 GFP_KERNEL,
354 &cmd->sg_frame_phys_addr);
355
356 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
357 GFP_KERNEL, &cmd->sense_phys_addr);
358 /*
359 * megasas_teardown_frame_pool_fusion() takes care of freeing
360 * whatever has been allocated
361 */
362 if (!cmd->sg_frame || !cmd->sense) {
363 printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
364 megasas_teardown_frame_pool_fusion(instance);
365 return -ENOMEM;
366 }
367 }
368 return 0;
369}
370
371/**
372 * megasas_alloc_cmds_fusion - Allocates the command packets
373 * @instance: Adapter soft state
374 *
375 *
376 * Each frame has a 32-bit field called context. This context is used to get
377 * back the megasas_cmd_fusion from the frame when a frame gets completed
378 * In this driver, the 32 bit values are the indices into an array cmd_list.
379 * This array is used only to look up the megasas_cmd_fusion given the context.
380 * The free commands themselves are maintained in a linked list called cmd_pool.
381 *
382 * cmds are formed in the io_request and sg_frame members of the
383 * megasas_cmd_fusion. The context field is used to get a request descriptor
384 * and is used as SMID of the cmd.
385 * SMID value range is from 1 to max_fw_cmds.
386 */
387int
388megasas_alloc_cmds_fusion(struct megasas_instance *instance)
389{
adam radfordc8e858f2011-10-08 18:15:13 -0700390 int i, j, count;
adam radford9c915a82010-12-21 13:34:31 -0800391 u32 max_cmd, io_frames_sz;
392 struct fusion_context *fusion;
393 struct megasas_cmd_fusion *cmd;
394 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
395 u32 offset;
396 dma_addr_t io_req_base_phys;
397 u8 *io_req_base;
398
399 fusion = instance->ctrl_context;
400
401 max_cmd = instance->max_fw_cmds;
402
403 fusion->req_frames_desc =
404 dma_alloc_coherent(&instance->pdev->dev,
405 fusion->request_alloc_sz,
406 &fusion->req_frames_desc_phys, GFP_KERNEL);
407
408 if (!fusion->req_frames_desc) {
409 printk(KERN_ERR "megasas; Could not allocate memory for "
410 "request_frames\n");
411 goto fail_req_desc;
412 }
413
adam radfordc8e858f2011-10-08 18:15:13 -0700414 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
adam radford9c915a82010-12-21 13:34:31 -0800415 fusion->reply_frames_desc_pool =
416 pci_pool_create("reply_frames pool", instance->pdev,
adam radfordc8e858f2011-10-08 18:15:13 -0700417 fusion->reply_alloc_sz * count, 16, 0);
adam radford9c915a82010-12-21 13:34:31 -0800418
419 if (!fusion->reply_frames_desc_pool) {
420 printk(KERN_ERR "megasas; Could not allocate memory for "
421 "reply_frame pool\n");
422 goto fail_reply_desc;
423 }
424
425 fusion->reply_frames_desc =
426 pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
427 &fusion->reply_frames_desc_phys);
428 if (!fusion->reply_frames_desc) {
429 printk(KERN_ERR "megasas; Could not allocate memory for "
430 "reply_frame pool\n");
431 pci_pool_destroy(fusion->reply_frames_desc_pool);
432 goto fail_reply_desc;
433 }
434
435 reply_desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -0700436 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
adam radford9c915a82010-12-21 13:34:31 -0800437 reply_desc->Words = ULLONG_MAX;
438
439 io_frames_sz = fusion->io_frames_alloc_sz;
440
441 fusion->io_request_frames_pool =
442 pci_pool_create("io_request_frames pool", instance->pdev,
443 fusion->io_frames_alloc_sz, 16, 0);
444
445 if (!fusion->io_request_frames_pool) {
446 printk(KERN_ERR "megasas: Could not allocate memory for "
447 "io_request_frame pool\n");
448 goto fail_io_frames;
449 }
450
451 fusion->io_request_frames =
452 pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
453 &fusion->io_request_frames_phys);
454 if (!fusion->io_request_frames) {
455 printk(KERN_ERR "megasas: Could not allocate memory for "
456 "io_request_frames frames\n");
457 pci_pool_destroy(fusion->io_request_frames_pool);
458 goto fail_io_frames;
459 }
460
461 /*
462 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
463 * Allocate the dynamic array first and then allocate individual
464 * commands.
465 */
Fengguang Wu7731e6b2012-08-24 23:27:35 +0800466 fusion->cmd_list = kzalloc(sizeof(struct megasas_cmd_fusion *)
467 * max_cmd, GFP_KERNEL);
adam radford9c915a82010-12-21 13:34:31 -0800468
469 if (!fusion->cmd_list) {
470 printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
471 "memory for cmd_list_fusion\n");
472 goto fail_cmd_list;
473 }
474
adam radford9c915a82010-12-21 13:34:31 -0800475 max_cmd = instance->max_fw_cmds;
476 for (i = 0; i < max_cmd; i++) {
477 fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
478 GFP_KERNEL);
479 if (!fusion->cmd_list[i]) {
480 printk(KERN_ERR "Could not alloc cmd list fusion\n");
481
482 for (j = 0; j < i; j++)
483 kfree(fusion->cmd_list[j]);
484
485 kfree(fusion->cmd_list);
486 fusion->cmd_list = NULL;
487 goto fail_cmd_list;
488 }
489 }
490
491 /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
492 io_req_base = fusion->io_request_frames +
493 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
494 io_req_base_phys = fusion->io_request_frames_phys +
495 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
496
497 /*
498 * Add all the commands to command pool (fusion->cmd_pool)
499 */
500
501 /* SMID 0 is reserved. Set SMID/index from 1 */
502 for (i = 0; i < max_cmd; i++) {
503 cmd = fusion->cmd_list[i];
504 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
505 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
506 cmd->index = i + 1;
507 cmd->scmd = NULL;
508 cmd->sync_cmd_idx = (u32)ULONG_MAX; /* Set to Invalid */
509 cmd->instance = instance;
510 cmd->io_request =
511 (struct MPI2_RAID_SCSI_IO_REQUEST *)
512 (io_req_base + offset);
513 memset(cmd->io_request, 0,
514 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
515 cmd->io_request_phys_addr = io_req_base_phys + offset;
516
517 list_add_tail(&cmd->list, &fusion->cmd_pool);
518 }
519
520 /*
521 * Create a frame pool and assign one frame to each cmd
522 */
523 if (megasas_create_frame_pool_fusion(instance)) {
524 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
525 megasas_free_cmds_fusion(instance);
526 goto fail_req_desc;
527 }
528
529 return 0;
530
531fail_cmd_list:
532 pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
533 fusion->io_request_frames_phys);
534 pci_pool_destroy(fusion->io_request_frames_pool);
535fail_io_frames:
536 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
537 fusion->reply_frames_desc,
538 fusion->reply_frames_desc_phys);
539 pci_pool_free(fusion->reply_frames_desc_pool,
540 fusion->reply_frames_desc,
541 fusion->reply_frames_desc_phys);
542 pci_pool_destroy(fusion->reply_frames_desc_pool);
543
544fail_reply_desc:
545 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
546 fusion->req_frames_desc,
547 fusion->req_frames_desc_phys);
548fail_req_desc:
549 return -ENOMEM;
550}
551
552/**
553 * wait_and_poll - Issues a polling command
554 * @instance: Adapter soft state
555 * @cmd: Command packet to be issued
556 *
557 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
558 */
559int
adam radford229fe472014-03-10 02:51:56 -0700560wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
561 int seconds)
adam radford9c915a82010-12-21 13:34:31 -0800562{
563 int i;
564 struct megasas_header *frame_hdr = &cmd->frame->hdr;
565
adam radford229fe472014-03-10 02:51:56 -0700566 u32 msecs = seconds * 1000;
adam radford9c915a82010-12-21 13:34:31 -0800567
568 /*
569 * Wait for cmd_status to change
570 */
571 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
572 rmb();
573 msleep(20);
574 }
575
576 if (frame_hdr->cmd_status == 0xff)
577 return -ETIME;
578
579 return 0;
580}
581
582/**
583 * megasas_ioc_init_fusion - Initializes the FW
584 * @instance: Adapter soft state
585 *
586 * Issues the IOC Init cmd
587 */
588int
589megasas_ioc_init_fusion(struct megasas_instance *instance)
590{
591 struct megasas_init_frame *init_frame;
592 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
593 dma_addr_t ioc_init_handle;
adam radford9c915a82010-12-21 13:34:31 -0800594 struct megasas_cmd *cmd;
595 u8 ret;
596 struct fusion_context *fusion;
adam radfordc77a9bd2014-03-10 02:51:36 -0700597 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
adam radford9c915a82010-12-21 13:34:31 -0800598 int i;
599 struct megasas_header *frame_hdr;
600
601 fusion = instance->ctrl_context;
602
603 cmd = megasas_get_cmd(instance);
604
605 if (!cmd) {
606 printk(KERN_ERR "Could not allocate cmd for INIT Frame\n");
607 ret = 1;
608 goto fail_get_cmd;
609 }
610
611 IOCInitMessage =
612 dma_alloc_coherent(&instance->pdev->dev,
613 sizeof(struct MPI2_IOC_INIT_REQUEST),
614 &ioc_init_handle, GFP_KERNEL);
615
616 if (!IOCInitMessage) {
617 printk(KERN_ERR "Could not allocate memory for "
618 "IOCInitMessage\n");
619 ret = 1;
620 goto fail_fw_init;
621 }
622
623 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
624
625 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
626 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530627 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
628 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
629 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
adam radford9c915a82010-12-21 13:34:31 -0800630
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530631 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
632 IOCInitMessage->ReplyDescriptorPostQueueAddress = cpu_to_le64(fusion->reply_frames_desc_phys);
633 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
adam radford5738f992012-03-19 19:49:53 -0700634 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
adam radford9c915a82010-12-21 13:34:31 -0800635 init_frame = (struct megasas_init_frame *)cmd->frame;
636 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
637
638 frame_hdr = &cmd->frame->hdr;
adam radford9c915a82010-12-21 13:34:31 -0800639 frame_hdr->cmd_status = 0xFF;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530640 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
adam radford9c915a82010-12-21 13:34:31 -0800641
642 init_frame->cmd = MFI_CMD_INIT;
643 init_frame->cmd_status = 0xFF;
644
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530645 /* driver support Extended MSIX */
646 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
647 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
648 init_frame->driver_operations.
649 mfi_capabilities.support_additional_msix = 1;
adam radford21c9e162013-09-06 15:27:14 -0700650 /* driver supports HA / Remote LUN over Fast Path interface */
651 init_frame->driver_operations.mfi_capabilities.support_fp_remote_lun
652 = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530653 /* Convert capability to LE32 */
654 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
adam radford21c9e162013-09-06 15:27:14 -0700655
Hannes Reineckefdc5a972014-01-16 11:25:33 +0100656 init_frame->queue_info_new_phys_addr_hi =
657 cpu_to_le32(upper_32_bits(ioc_init_handle));
658 init_frame->queue_info_new_phys_addr_lo =
659 cpu_to_le32(lower_32_bits(ioc_init_handle));
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530660 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
adam radford9c915a82010-12-21 13:34:31 -0800661
adam radfordc77a9bd2014-03-10 02:51:36 -0700662 req_desc.Words = 0;
663 req_desc.MFAIo.RequestFlags =
adam radford9c915a82010-12-21 13:34:31 -0800664 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
665 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radfordc77a9bd2014-03-10 02:51:36 -0700666 cpu_to_le32s((u32 *)&req_desc.MFAIo);
667 req_desc.Words |= cpu_to_le64(cmd->frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -0800668
669 /*
670 * disable the intr before firing the init frame
671 */
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +0530672 instance->instancet->disable_intr(instance);
adam radford9c915a82010-12-21 13:34:31 -0800673
674 for (i = 0; i < (10 * 1000); i += 20) {
675 if (readl(&instance->reg_set->doorbell) & 1)
676 msleep(20);
677 else
678 break;
679 }
680
adam radfordc77a9bd2014-03-10 02:51:36 -0700681 instance->instancet->fire_cmd(instance, req_desc.u.low,
682 req_desc.u.high, instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -0800683
adam radford229fe472014-03-10 02:51:56 -0700684 wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
adam radford9c915a82010-12-21 13:34:31 -0800685
686 frame_hdr = &cmd->frame->hdr;
687 if (frame_hdr->cmd_status != 0) {
688 ret = 1;
689 goto fail_fw_init;
690 }
691 printk(KERN_ERR "megasas:IOC Init cmd success\n");
692
693 ret = 0;
694
695fail_fw_init:
696 megasas_return_cmd(instance, cmd);
697 if (IOCInitMessage)
698 dma_free_coherent(&instance->pdev->dev,
699 sizeof(struct MPI2_IOC_INIT_REQUEST),
700 IOCInitMessage, ioc_init_handle);
701fail_get_cmd:
702 return ret;
703}
704
705/*
adam radford9c915a82010-12-21 13:34:31 -0800706 * megasas_get_ld_map_info - Returns FW's ld_map structure
707 * @instance: Adapter soft state
708 * @pend: Pend the command or not
709 * Issues an internal command (DCMD) to get the FW's controller PD
710 * list structure. This information is mainly used to find out SYSTEM
711 * supported by the FW.
712 */
713static int
714megasas_get_ld_map_info(struct megasas_instance *instance)
715{
716 int ret = 0;
717 struct megasas_cmd *cmd;
718 struct megasas_dcmd_frame *dcmd;
719 struct MR_FW_RAID_MAP_ALL *ci;
720 dma_addr_t ci_h = 0;
721 u32 size_map_info;
722 struct fusion_context *fusion;
723
724 cmd = megasas_get_cmd(instance);
725
726 if (!cmd) {
727 printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
728 return -ENOMEM;
729 }
730
731 fusion = instance->ctrl_context;
732
733 if (!fusion) {
734 megasas_return_cmd(instance, cmd);
Hannes Reinecke2f8bdfa2014-01-16 11:25:35 +0100735 return -ENXIO;
adam radford9c915a82010-12-21 13:34:31 -0800736 }
737
738 dcmd = &cmd->frame->dcmd;
739
740 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
741 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
742
743 ci = fusion->ld_map[(instance->map_id & 1)];
744 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
745
746 if (!ci) {
747 printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
748 megasas_return_cmd(instance, cmd);
749 return -ENOMEM;
750 }
751
752 memset(ci, 0, sizeof(*ci));
753 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
754
755 dcmd->cmd = MFI_CMD_DCMD;
756 dcmd->cmd_status = 0xFF;
757 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530758 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
adam radford9c915a82010-12-21 13:34:31 -0800759 dcmd->timeout = 0;
760 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530761 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
762 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
763 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
764 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -0800765
766 if (!megasas_issue_polled(instance, cmd))
767 ret = 0;
768 else {
769 printk(KERN_ERR "megasas: Get LD Map Info Failed\n");
770 ret = -1;
771 }
772
773 megasas_return_cmd(instance, cmd);
774
775 return ret;
776}
777
778u8
779megasas_get_map_info(struct megasas_instance *instance)
780{
781 struct fusion_context *fusion = instance->ctrl_context;
782
783 fusion->fast_path_io = 0;
784 if (!megasas_get_ld_map_info(instance)) {
Sumit.Saxena@lsi.combc93d422013-05-22 12:35:04 +0530785 if (MR_ValidateMapInfo(instance)) {
adam radford9c915a82010-12-21 13:34:31 -0800786 fusion->fast_path_io = 1;
787 return 0;
788 }
789 }
790 return 1;
791}
792
793/*
794 * megasas_sync_map_info - Returns FW's ld_map structure
795 * @instance: Adapter soft state
796 *
797 * Issues an internal command (DCMD) to get the FW's controller PD
798 * list structure. This information is mainly used to find out SYSTEM
799 * supported by the FW.
800 */
801int
802megasas_sync_map_info(struct megasas_instance *instance)
803{
804 int ret = 0, i;
805 struct megasas_cmd *cmd;
806 struct megasas_dcmd_frame *dcmd;
807 u32 size_sync_info, num_lds;
808 struct fusion_context *fusion;
809 struct MR_LD_TARGET_SYNC *ci = NULL;
810 struct MR_FW_RAID_MAP_ALL *map;
811 struct MR_LD_RAID *raid;
812 struct MR_LD_TARGET_SYNC *ld_sync;
813 dma_addr_t ci_h = 0;
814 u32 size_map_info;
815
816 cmd = megasas_get_cmd(instance);
817
818 if (!cmd) {
819 printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
820 "info.\n");
821 return -ENOMEM;
822 }
823
824 fusion = instance->ctrl_context;
825
826 if (!fusion) {
827 megasas_return_cmd(instance, cmd);
828 return 1;
829 }
830
831 map = fusion->ld_map[instance->map_id & 1];
832
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530833 num_lds = le32_to_cpu(map->raidMap.ldCount);
adam radford9c915a82010-12-21 13:34:31 -0800834
835 dcmd = &cmd->frame->dcmd;
836
837 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
838
839 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
840
841 ci = (struct MR_LD_TARGET_SYNC *)
842 fusion->ld_map[(instance->map_id - 1) & 1];
843 memset(ci, 0, sizeof(struct MR_FW_RAID_MAP_ALL));
844
845 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
846
847 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
848
849 for (i = 0; i < num_lds; i++, ld_sync++) {
850 raid = MR_LdRaidGet(i, map);
851 ld_sync->targetId = MR_GetLDTgtId(i, map);
852 ld_sync->seqNum = raid->seqNum;
853 }
854
855 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
856 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
857
858 dcmd->cmd = MFI_CMD_DCMD;
859 dcmd->cmd_status = 0xFF;
860 dcmd->sge_count = 1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530861 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
adam radford9c915a82010-12-21 13:34:31 -0800862 dcmd->timeout = 0;
863 dcmd->pad_0 = 0;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530864 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -0800865 dcmd->mbox.b[0] = num_lds;
866 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +0530867 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
868 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
869 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
adam radford9c915a82010-12-21 13:34:31 -0800870
871 instance->map_update_cmd = cmd;
872
873 instance->instancet->issue_dcmd(instance, cmd);
874
875 return ret;
876}
877
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +0530878/*
879 * meagasas_display_intel_branding - Display branding string
880 * @instance: per adapter object
881 *
882 * Return nothing.
883 */
884static void
885megasas_display_intel_branding(struct megasas_instance *instance)
886{
887 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
888 return;
889
890 switch (instance->pdev->device) {
891 case PCI_DEVICE_ID_LSI_INVADER:
892 switch (instance->pdev->subsystem_device) {
893 case MEGARAID_INTEL_RS3DC080_SSDID:
894 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
895 instance->host->host_no,
896 MEGARAID_INTEL_RS3DC080_BRANDING);
897 break;
898 case MEGARAID_INTEL_RS3DC040_SSDID:
899 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
900 instance->host->host_no,
901 MEGARAID_INTEL_RS3DC040_BRANDING);
902 break;
903 case MEGARAID_INTEL_RS3SC008_SSDID:
904 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
905 instance->host->host_no,
906 MEGARAID_INTEL_RS3SC008_BRANDING);
907 break;
908 case MEGARAID_INTEL_RS3MC044_SSDID:
909 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
910 instance->host->host_no,
911 MEGARAID_INTEL_RS3MC044_BRANDING);
912 break;
913 default:
914 break;
915 }
916 break;
917 case PCI_DEVICE_ID_LSI_FURY:
918 switch (instance->pdev->subsystem_device) {
919 case MEGARAID_INTEL_RS3WC080_SSDID:
920 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
921 instance->host->host_no,
922 MEGARAID_INTEL_RS3WC080_BRANDING);
923 break;
924 case MEGARAID_INTEL_RS3WC040_SSDID:
925 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
926 instance->host->host_no,
927 MEGARAID_INTEL_RS3WC040_BRANDING);
928 break;
929 default:
930 break;
931 }
932 break;
933 default:
934 break;
935 }
936}
937
adam radford9c915a82010-12-21 13:34:31 -0800938/**
939 * megasas_init_adapter_fusion - Initializes the FW
940 * @instance: Adapter soft state
941 *
942 * This is the main function for initializing firmware.
943 */
944u32
945megasas_init_adapter_fusion(struct megasas_instance *instance)
946{
947 struct megasas_register_set __iomem *reg_set;
948 struct fusion_context *fusion;
949 u32 max_cmd;
adam radfordc8e858f2011-10-08 18:15:13 -0700950 int i = 0, count;
adam radford9c915a82010-12-21 13:34:31 -0800951
952 fusion = instance->ctrl_context;
953
954 reg_set = instance->reg_set;
955
956 /*
957 * Get various operational parameters from status register
958 */
959 instance->max_fw_cmds =
960 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
961 instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
962
963 /*
964 * Reduce the max supported cmds by 1. This is to ensure that the
965 * reply_q_sz (1 more than the max cmd that driver may send)
966 * does not exceed max cmds that the FW can support
967 */
968 instance->max_fw_cmds = instance->max_fw_cmds-1;
969 /* Only internal cmds (DCMD) need to have MFI frames */
970 instance->max_mfi_cmds = MEGASAS_INT_CMDS;
971
972 max_cmd = instance->max_fw_cmds;
973
974 fusion->reply_q_depth = ((max_cmd + 1 + 15)/16)*16;
975
976 fusion->request_alloc_sz =
977 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
978 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
979 *(fusion->reply_q_depth);
980 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
981 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
982 (max_cmd + 1)); /* Extra 1 for SMID 0 */
983
984 fusion->max_sge_in_main_msg =
985 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
986 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
987
988 fusion->max_sge_in_chain =
989 MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
990
991 instance->max_num_sge = fusion->max_sge_in_main_msg +
992 fusion->max_sge_in_chain - 2;
993
994 /* Used for pass thru MFI frame (DCMD) */
995 fusion->chain_offset_mfi_pthru =
996 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
997
998 fusion->chain_offset_io_request =
999 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
1000 sizeof(union MPI2_SGE_IO_UNION))/16;
1001
adam radfordc8e858f2011-10-08 18:15:13 -07001002 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1003 for (i = 0 ; i < count; i++)
1004 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -08001005
1006 /*
1007 * Allocate memory for descriptors
1008 * Create a pool of commands
1009 */
1010 if (megasas_alloc_cmds(instance))
1011 goto fail_alloc_mfi_cmds;
1012 if (megasas_alloc_cmds_fusion(instance))
1013 goto fail_alloc_cmds;
1014
1015 if (megasas_ioc_init_fusion(instance))
1016 goto fail_ioc_init;
1017
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301018 megasas_display_intel_branding(instance);
1019
adam radford9c915a82010-12-21 13:34:31 -08001020 instance->flag_ieee = 1;
1021
1022 fusion->map_sz = sizeof(struct MR_FW_RAID_MAP) +
1023 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
1024
1025 fusion->fast_path_io = 0;
1026
1027 for (i = 0; i < 2; i++) {
1028 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
1029 fusion->map_sz,
1030 &fusion->ld_map_phys[i],
1031 GFP_KERNEL);
1032 if (!fusion->ld_map[i]) {
1033 printk(KERN_ERR "megasas: Could not allocate memory "
1034 "for map info\n");
1035 goto fail_map_info;
1036 }
1037 }
1038
1039 if (!megasas_get_map_info(instance))
1040 megasas_sync_map_info(instance);
1041
1042 return 0;
1043
adam radford9c915a82010-12-21 13:34:31 -08001044fail_map_info:
1045 if (i == 1)
1046 dma_free_coherent(&instance->pdev->dev, fusion->map_sz,
1047 fusion->ld_map[0], fusion->ld_map_phys[0]);
1048fail_ioc_init:
adam radfordeb1b1232011-02-24 20:55:56 -08001049 megasas_free_cmds_fusion(instance);
1050fail_alloc_cmds:
1051 megasas_free_cmds(instance);
1052fail_alloc_mfi_cmds:
adam radford9c915a82010-12-21 13:34:31 -08001053 return 1;
1054}
1055
1056/**
1057 * megasas_fire_cmd_fusion - Sends command to the FW
1058 * @frame_phys_addr : Physical address of cmd
1059 * @frame_count : Number of frames for the command
1060 * @regs : MFI register set
1061 */
1062void
1063megasas_fire_cmd_fusion(struct megasas_instance *instance,
1064 dma_addr_t req_desc_lo,
1065 u32 req_desc_hi,
1066 struct megasas_register_set __iomem *regs)
1067{
1068 unsigned long flags;
1069
1070 spin_lock_irqsave(&instance->hba_lock, flags);
1071
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301072 writel(le32_to_cpu(req_desc_lo), &(regs)->inbound_low_queue_port);
1073 writel(le32_to_cpu(req_desc_hi), &(regs)->inbound_high_queue_port);
adam radford9c915a82010-12-21 13:34:31 -08001074 spin_unlock_irqrestore(&instance->hba_lock, flags);
1075}
1076
1077/**
1078 * map_cmd_status - Maps FW cmd status to OS cmd status
1079 * @cmd : Pointer to cmd
1080 * @status : status of cmd returned by FW
1081 * @ext_status : ext status of cmd returned by FW
1082 */
1083
1084void
1085map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1086{
1087
1088 switch (status) {
1089
1090 case MFI_STAT_OK:
1091 cmd->scmd->result = DID_OK << 16;
1092 break;
1093
1094 case MFI_STAT_SCSI_IO_FAILED:
1095 case MFI_STAT_LD_INIT_IN_PROGRESS:
1096 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1097 break;
1098
1099 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1100
1101 cmd->scmd->result = (DID_OK << 16) | ext_status;
1102 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1103 memset(cmd->scmd->sense_buffer, 0,
1104 SCSI_SENSE_BUFFERSIZE);
1105 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1106 SCSI_SENSE_BUFFERSIZE);
1107 cmd->scmd->result |= DRIVER_SENSE << 24;
1108 }
1109 break;
1110
1111 case MFI_STAT_LD_OFFLINE:
1112 case MFI_STAT_DEVICE_NOT_FOUND:
1113 cmd->scmd->result = DID_BAD_TARGET << 16;
1114 break;
adam radford36807e62011-10-08 18:15:06 -07001115 case MFI_STAT_CONFIG_SEQ_MISMATCH:
1116 cmd->scmd->result = DID_IMM_RETRY << 16;
1117 break;
adam radford9c915a82010-12-21 13:34:31 -08001118 default:
1119 printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1120 cmd->scmd->result = DID_ERROR << 16;
1121 break;
1122 }
1123}
1124
1125/**
1126 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1127 * @instance: Adapter soft state
1128 * @scp: SCSI command from the mid-layer
1129 * @sgl_ptr: SGL to be filled in
1130 * @cmd: cmd we are working on
1131 *
1132 * If successful, this function returns the number of SG elements.
1133 */
1134static int
1135megasas_make_sgl_fusion(struct megasas_instance *instance,
1136 struct scsi_cmnd *scp,
1137 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1138 struct megasas_cmd_fusion *cmd)
1139{
adam radford36807e62011-10-08 18:15:06 -07001140 int i, sg_processed, sge_count;
adam radford9c915a82010-12-21 13:34:31 -08001141 struct scatterlist *os_sgl;
1142 struct fusion_context *fusion;
1143
1144 fusion = instance->ctrl_context;
1145
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301146 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1147 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001148 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1149 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1150 sgl_ptr_end->Flags = 0;
1151 }
adam radford9c915a82010-12-21 13:34:31 -08001152
1153 sge_count = scsi_dma_map(scp);
1154
1155 BUG_ON(sge_count < 0);
1156
1157 if (sge_count > instance->max_num_sge || !sge_count)
1158 return sge_count;
1159
adam radford9c915a82010-12-21 13:34:31 -08001160 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301161 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
1162 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
adam radford9c915a82010-12-21 13:34:31 -08001163 sgl_ptr->Flags = 0;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301164 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1165 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001166 if (i == sge_count - 1)
1167 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1168 }
adam radford9c915a82010-12-21 13:34:31 -08001169 sgl_ptr++;
1170
1171 sg_processed = i + 1;
1172
1173 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1174 (sge_count > fusion->max_sge_in_main_msg)) {
1175
1176 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301177 if ((instance->pdev->device ==
1178 PCI_DEVICE_ID_LSI_INVADER) ||
1179 (instance->pdev->device ==
1180 PCI_DEVICE_ID_LSI_FURY)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301181 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1182 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1183 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
adam radford36807e62011-10-08 18:15:06 -07001184 cmd->io_request->ChainOffset =
1185 fusion->
1186 chain_offset_io_request;
1187 else
1188 cmd->io_request->ChainOffset = 0;
1189 } else
1190 cmd->io_request->ChainOffset =
1191 fusion->chain_offset_io_request;
1192
adam radford9c915a82010-12-21 13:34:31 -08001193 sg_chain = sgl_ptr;
1194 /* Prepare chain element */
1195 sg_chain->NextChainOffset = 0;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301196 if ((instance->pdev->device ==
1197 PCI_DEVICE_ID_LSI_INVADER) ||
1198 (instance->pdev->device ==
1199 PCI_DEVICE_ID_LSI_FURY))
adam radford36807e62011-10-08 18:15:06 -07001200 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1201 else
1202 sg_chain->Flags =
1203 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1204 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301205 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
1206 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08001207
1208 sgl_ptr =
1209 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1210 }
1211 }
1212
1213 return sge_count;
1214}
1215
1216/**
1217 * megasas_set_pd_lba - Sets PD LBA
1218 * @cdb: CDB
1219 * @cdb_len: cdb length
1220 * @start_blk: Start block of IO
1221 *
1222 * Used to set the PD LBA in CDB for FP IOs
1223 */
1224void
1225megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1226 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
1227 struct MR_FW_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
1228{
1229 struct MR_LD_RAID *raid;
1230 u32 ld;
1231 u64 start_blk = io_info->pdBlock;
1232 u8 *cdb = io_request->CDB.CDB32;
1233 u32 num_blocks = io_info->numBlocks;
adam radford495c5602011-05-11 18:34:40 -07001234 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
adam radford9c915a82010-12-21 13:34:31 -08001235
1236 /* Check if T10 PI (DIF) is enabled for this LD */
1237 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1238 raid = MR_LdRaidGet(ld, local_map_ptr);
1239 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1240 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1241 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1242 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1243
1244 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1245 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1246 else
1247 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1248 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1249
1250 /* LBA */
1251 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1252 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1253 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1254 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1255 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1256 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1257 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1258 cdb[19] = (u8)(start_blk & 0xff);
1259
1260 /* Logical block reference tag */
1261 io_request->CDB.EEDP32.PrimaryReferenceTag =
1262 cpu_to_be32(ref_tag);
1263 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0xffff;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301264 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001265
1266 /* Transfer length */
1267 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1268 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1269 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1270 cdb[31] = (u8)(num_blocks & 0xff);
1271
1272 /* set SCSI IO EEDPFlags */
1273 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301274 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001275 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1276 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1277 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1278 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301279 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
adam radford9c915a82010-12-21 13:34:31 -08001280 } else {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301281 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001282 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301283 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
adam radford9c915a82010-12-21 13:34:31 -08001284 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301285 io_request->Control |= cpu_to_le32((0x4 << 26));
1286 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
adam radford9c915a82010-12-21 13:34:31 -08001287 } else {
1288 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1289 if (((cdb_len == 12) || (cdb_len == 16)) &&
1290 (start_blk <= 0xffffffff)) {
1291 if (cdb_len == 16) {
1292 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1293 flagvals = cdb[1];
1294 groupnum = cdb[14];
1295 control = cdb[15];
1296 } else {
1297 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1298 flagvals = cdb[1];
1299 groupnum = cdb[10];
1300 control = cdb[11];
1301 }
1302
1303 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1304
1305 cdb[0] = opcode;
1306 cdb[1] = flagvals;
1307 cdb[6] = groupnum;
1308 cdb[9] = control;
1309
1310 /* Transfer length */
1311 cdb[8] = (u8)(num_blocks & 0xff);
1312 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1313
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301314 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001315 cdb_len = 10;
adam radford495c5602011-05-11 18:34:40 -07001316 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1317 /* Convert to 16 byte CDB for large LBA's */
1318 switch (cdb_len) {
1319 case 6:
1320 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1321 control = cdb[5];
1322 break;
1323 case 10:
1324 opcode =
1325 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1326 flagvals = cdb[1];
1327 groupnum = cdb[6];
1328 control = cdb[9];
1329 break;
1330 case 12:
1331 opcode =
1332 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1333 flagvals = cdb[1];
1334 groupnum = cdb[10];
1335 control = cdb[11];
1336 break;
1337 }
1338
1339 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1340
1341 cdb[0] = opcode;
1342 cdb[1] = flagvals;
1343 cdb[14] = groupnum;
1344 cdb[15] = control;
1345
1346 /* Transfer length */
1347 cdb[13] = (u8)(num_blocks & 0xff);
1348 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1349 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1350 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1351
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301352 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
adam radford495c5602011-05-11 18:34:40 -07001353 cdb_len = 16;
adam radford9c915a82010-12-21 13:34:31 -08001354 }
1355
1356 /* Normal case, just load LBA here */
1357 switch (cdb_len) {
1358 case 6:
1359 {
1360 u8 val = cdb[1] & 0xE0;
1361 cdb[3] = (u8)(start_blk & 0xff);
1362 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1363 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1364 break;
1365 }
1366 case 10:
1367 cdb[5] = (u8)(start_blk & 0xff);
1368 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1369 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1370 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1371 break;
1372 case 12:
1373 cdb[5] = (u8)(start_blk & 0xff);
1374 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1375 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1376 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1377 break;
1378 case 16:
1379 cdb[9] = (u8)(start_blk & 0xff);
1380 cdb[8] = (u8)((start_blk >> 8) & 0xff);
1381 cdb[7] = (u8)((start_blk >> 16) & 0xff);
1382 cdb[6] = (u8)((start_blk >> 24) & 0xff);
1383 cdb[5] = (u8)((start_blk >> 32) & 0xff);
1384 cdb[4] = (u8)((start_blk >> 40) & 0xff);
1385 cdb[3] = (u8)((start_blk >> 48) & 0xff);
1386 cdb[2] = (u8)((start_blk >> 56) & 0xff);
1387 break;
1388 }
1389 }
1390}
1391
1392/**
1393 * megasas_build_ldio_fusion - Prepares IOs to devices
1394 * @instance: Adapter soft state
1395 * @scp: SCSI command
1396 * @cmd: Command to be prepared
1397 *
1398 * Prepares the io_request and chain elements (sg_frame) for IO
1399 * The IO can be for PD (Fast Path) or LD
1400 */
1401void
1402megasas_build_ldio_fusion(struct megasas_instance *instance,
1403 struct scsi_cmnd *scp,
1404 struct megasas_cmd_fusion *cmd)
1405{
1406 u8 fp_possible;
adam radfordf9eff812012-10-01 19:27:07 -07001407 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
adam radford9c915a82010-12-21 13:34:31 -08001408 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1409 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1410 struct IO_REQUEST_INFO io_info;
1411 struct fusion_context *fusion;
1412 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
adam radford21c9e162013-09-06 15:27:14 -07001413 u8 *raidLUN;
adam radford9c915a82010-12-21 13:34:31 -08001414
1415 device_id = MEGASAS_DEV_INDEX(instance, scp);
1416
1417 fusion = instance->ctrl_context;
1418
1419 io_request = cmd->io_request;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301420 io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
adam radford9c915a82010-12-21 13:34:31 -08001421 io_request->RaidContext.status = 0;
1422 io_request->RaidContext.exStatus = 0;
1423
1424 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1425
1426 start_lba_lo = 0;
1427 start_lba_hi = 0;
1428 fp_possible = 0;
1429
1430 /*
1431 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1432 */
1433 if (scp->cmd_len == 6) {
adam radfordf9eff812012-10-01 19:27:07 -07001434 datalength = (u32) scp->cmnd[4];
adam radford9c915a82010-12-21 13:34:31 -08001435 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1436 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1437
1438 start_lba_lo &= 0x1FFFFF;
1439 }
1440
1441 /*
1442 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1443 */
1444 else if (scp->cmd_len == 10) {
adam radfordf9eff812012-10-01 19:27:07 -07001445 datalength = (u32) scp->cmnd[8] |
adam radford9c915a82010-12-21 13:34:31 -08001446 ((u32) scp->cmnd[7] << 8);
1447 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1448 ((u32) scp->cmnd[3] << 16) |
1449 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1450 }
1451
1452 /*
1453 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1454 */
1455 else if (scp->cmd_len == 12) {
adam radfordf9eff812012-10-01 19:27:07 -07001456 datalength = ((u32) scp->cmnd[6] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08001457 ((u32) scp->cmnd[7] << 16) |
1458 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1459 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1460 ((u32) scp->cmnd[3] << 16) |
1461 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1462 }
1463
1464 /*
1465 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1466 */
1467 else if (scp->cmd_len == 16) {
adam radfordf9eff812012-10-01 19:27:07 -07001468 datalength = ((u32) scp->cmnd[10] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08001469 ((u32) scp->cmnd[11] << 16) |
1470 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1471 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1472 ((u32) scp->cmnd[7] << 16) |
1473 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1474
1475 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1476 ((u32) scp->cmnd[3] << 16) |
1477 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1478 }
1479
1480 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1481 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
adam radfordf9eff812012-10-01 19:27:07 -07001482 io_info.numBlocks = datalength;
adam radford9c915a82010-12-21 13:34:31 -08001483 io_info.ldTgtId = device_id;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301484 io_request->DataLength = cpu_to_le32(scsi_bufflen(scp));
adam radford9c915a82010-12-21 13:34:31 -08001485
1486 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1487 io_info.isRead = 1;
1488
1489 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1490
1491 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
1492 MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io)) {
1493 io_request->RaidContext.regLockFlags = 0;
1494 fp_possible = 0;
1495 } else {
adam radford36807e62011-10-08 18:15:06 -07001496 if (MR_BuildRaidContext(instance, &io_info,
1497 &io_request->RaidContext,
adam radford21c9e162013-09-06 15:27:14 -07001498 local_map_ptr, &raidLUN))
adam radford9c915a82010-12-21 13:34:31 -08001499 fp_possible = io_info.fpOkForIo;
1500 }
1501
adam radfordc8e858f2011-10-08 18:15:13 -07001502 /* Use smp_processor_id() for now until cmd->request->cpu is CPU
1503 id by default, not CPU group id, otherwise all MSI-X queues won't
1504 be utilized */
1505 cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
1506 smp_processor_id() % instance->msix_vectors : 0;
1507
adam radford9c915a82010-12-21 13:34:31 -08001508 if (fp_possible) {
1509 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1510 local_map_ptr, start_lba_lo);
adam radford9c915a82010-12-21 13:34:31 -08001511 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1512 cmd->request_desc->SCSIIO.RequestFlags =
1513 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1514 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301515 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1516 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001517 if (io_request->RaidContext.regLockFlags ==
1518 REGION_TYPE_UNUSED)
1519 cmd->request_desc->SCSIIO.RequestFlags =
1520 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1521 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1522 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1523 io_request->RaidContext.nseg = 0x1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301524 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
adam radford36807e62011-10-08 18:15:06 -07001525 io_request->RaidContext.regLockFlags |=
1526 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
1527 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1528 }
adam radford9c915a82010-12-21 13:34:31 -08001529 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1530 (io_info.isRead)) {
1531 io_info.devHandle =
1532 get_updated_dev_handle(
1533 &fusion->load_balance_info[device_id],
1534 &io_info);
1535 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
1536 } else
1537 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1538 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1539 io_request->DevHandle = io_info.devHandle;
adam radford21c9e162013-09-06 15:27:14 -07001540 /* populate the LUN field */
1541 memcpy(io_request->LUN, raidLUN, 8);
adam radford9c915a82010-12-21 13:34:31 -08001542 } else {
1543 io_request->RaidContext.timeoutValue =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301544 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
adam radford9c915a82010-12-21 13:34:31 -08001545 cmd->request_desc->SCSIIO.RequestFlags =
1546 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1547 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301548 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1549 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001550 if (io_request->RaidContext.regLockFlags ==
1551 REGION_TYPE_UNUSED)
1552 cmd->request_desc->SCSIIO.RequestFlags =
1553 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1554 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1555 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1556 io_request->RaidContext.regLockFlags |=
1557 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
1558 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1559 io_request->RaidContext.nseg = 0x1;
1560 }
1561 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301562 io_request->DevHandle = cpu_to_le16(device_id);
adam radford9c915a82010-12-21 13:34:31 -08001563 } /* Not FP */
1564}
1565
1566/**
1567 * megasas_build_dcdb_fusion - Prepares IOs to devices
1568 * @instance: Adapter soft state
1569 * @scp: SCSI command
1570 * @cmd: Command to be prepared
1571 *
1572 * Prepares the io_request frame for non-io cmds
1573 */
1574static void
1575megasas_build_dcdb_fusion(struct megasas_instance *instance,
1576 struct scsi_cmnd *scmd,
1577 struct megasas_cmd_fusion *cmd)
1578{
1579 u32 device_id;
1580 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1581 u16 pd_index = 0;
1582 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1583 struct fusion_context *fusion = instance->ctrl_context;
adam radford21c9e162013-09-06 15:27:14 -07001584 u8 span, physArm;
1585 u16 devHandle;
1586 u32 ld, arRef, pd;
1587 struct MR_LD_RAID *raid;
1588 struct RAID_CONTEXT *pRAID_Context;
adam radford9c915a82010-12-21 13:34:31 -08001589
1590 io_request = cmd->io_request;
1591 device_id = MEGASAS_DEV_INDEX(instance, scmd);
1592 pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
1593 +scmd->device->id;
1594 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1595
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301596 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
adam radford21c9e162013-09-06 15:27:14 -07001597
1598
adam radford9c915a82010-12-21 13:34:31 -08001599 /* Check if this is a system PD I/O */
adam radforde187df62012-10-01 19:27:12 -07001600 if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS &&
1601 instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
adam radford9c915a82010-12-21 13:34:31 -08001602 io_request->Function = 0;
adam radford9c5ebd02013-02-09 15:29:20 -08001603 if (fusion->fast_path_io)
1604 io_request->DevHandle =
adam radford9c915a82010-12-21 13:34:31 -08001605 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1606 io_request->RaidContext.timeoutValue =
1607 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1608 io_request->RaidContext.regLockFlags = 0;
1609 io_request->RaidContext.regLockRowLBA = 0;
1610 io_request->RaidContext.regLockLength = 0;
1611 io_request->RaidContext.RAIDFlags =
1612 MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
1613 MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
Sumit.Saxena@lsi.com5d0d9082013-05-22 12:33:29 +05301614 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1615 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
Sumit.Saxena@lsi.combe263742014-02-12 23:37:46 +05301616 io_request->IoFlags |= cpu_to_le16(
1617 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
adam radford9c915a82010-12-21 13:34:31 -08001618 cmd->request_desc->SCSIIO.RequestFlags =
1619 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1620 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford8ead5812012-10-01 19:27:20 -07001621 cmd->request_desc->SCSIIO.DevHandle =
1622 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05301623 cmd->request_desc->SCSIIO.MSIxIndex =
1624 instance->msix_vectors ? smp_processor_id() % instance->msix_vectors : 0;
Sumit.Saxena@lsi.com32d87452013-05-22 12:31:18 +05301625 /*
1626 * If the command is for the tape device, set the
1627 * FP timeout to the os layer timeout value.
1628 */
1629 if (scmd->device->type == TYPE_TAPE) {
1630 if ((scmd->request->timeout / HZ) > 0xFFFF)
1631 io_request->RaidContext.timeoutValue =
1632 0xFFFF;
1633 else
1634 io_request->RaidContext.timeoutValue =
1635 scmd->request->timeout / HZ;
1636 }
adam radford9c915a82010-12-21 13:34:31 -08001637 } else {
adam radford21c9e162013-09-06 15:27:14 -07001638 if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS)
1639 goto NonFastPath;
1640
1641 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1642 if ((ld >= MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io))
1643 goto NonFastPath;
1644
1645 raid = MR_LdRaidGet(ld, local_map_ptr);
1646
1647 /* check if this LD is FP capable */
1648 if (!(raid->capability.fpNonRWCapable))
1649 /* not FP capable, send as non-FP */
1650 goto NonFastPath;
1651
1652 /* get RAID_Context pointer */
1653 pRAID_Context = &io_request->RaidContext;
1654
1655 /* set RAID context values */
1656 pRAID_Context->regLockFlags = REGION_TYPE_SHARED_READ;
1657 pRAID_Context->timeoutValue = raid->fpIoTimeoutForLd;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301658 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
adam radford21c9e162013-09-06 15:27:14 -07001659 pRAID_Context->regLockRowLBA = 0;
1660 pRAID_Context->regLockLength = 0;
1661 pRAID_Context->configSeqNum = raid->seqNum;
1662
1663 /* get the DevHandle for the PD (since this is
1664 fpNonRWCapable, this is a single disk RAID0) */
1665 span = physArm = 0;
1666 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
1667 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
1668 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
1669
1670 /* build request descriptor */
1671 cmd->request_desc->SCSIIO.RequestFlags =
1672 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1673 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1674 cmd->request_desc->SCSIIO.DevHandle = devHandle;
1675
1676 /* populate the LUN field */
1677 memcpy(io_request->LUN, raid->LUN, 8);
1678
1679 /* build the raidScsiIO structure */
1680 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1681 io_request->DevHandle = devHandle;
1682
1683 return;
1684
1685NonFastPath:
adam radford9c915a82010-12-21 13:34:31 -08001686 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301687 io_request->DevHandle = cpu_to_le16(device_id);
adam radford9c915a82010-12-21 13:34:31 -08001688 cmd->request_desc->SCSIIO.RequestFlags =
1689 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1690 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1691 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301692 io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001693 int_to_scsilun(scmd->device->lun, (struct scsi_lun *)io_request->LUN);
adam radford9c915a82010-12-21 13:34:31 -08001694}
1695
1696/**
1697 * megasas_build_io_fusion - Prepares IOs to devices
1698 * @instance: Adapter soft state
1699 * @scp: SCSI command
1700 * @cmd: Command to be prepared
1701 *
1702 * Invokes helper functions to prepare request frames
1703 * and sets flags appropriate for IO/Non-IO cmd
1704 */
1705int
1706megasas_build_io_fusion(struct megasas_instance *instance,
1707 struct scsi_cmnd *scp,
1708 struct megasas_cmd_fusion *cmd)
1709{
1710 u32 device_id, sge_count;
1711 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1712
1713 device_id = MEGASAS_DEV_INDEX(instance, scp);
1714
1715 /* Zero out some fields so they don't get reused */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001716 memset(io_request->LUN, 0x0, 8);
adam radford9c915a82010-12-21 13:34:31 -08001717 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1718 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1719 io_request->EEDPFlags = 0;
1720 io_request->Control = 0;
1721 io_request->EEDPBlockSize = 0;
adam radford36807e62011-10-08 18:15:06 -07001722 io_request->ChainOffset = 0;
adam radford9c915a82010-12-21 13:34:31 -08001723 io_request->RaidContext.RAIDFlags = 0;
adam radford36807e62011-10-08 18:15:06 -07001724 io_request->RaidContext.Type = 0;
1725 io_request->RaidContext.nseg = 0;
adam radford9c915a82010-12-21 13:34:31 -08001726
1727 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1728 /*
1729 * Just the CDB length,rest of the Flags are zero
1730 * This will be modified for FP in build_ldio_fusion
1731 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301732 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
adam radford9c915a82010-12-21 13:34:31 -08001733
1734 if (megasas_is_ldio(scp))
1735 megasas_build_ldio_fusion(instance, scp, cmd);
1736 else
1737 megasas_build_dcdb_fusion(instance, scp, cmd);
1738
1739 /*
1740 * Construct SGL
1741 */
1742
1743 sge_count =
1744 megasas_make_sgl_fusion(instance, scp,
1745 (struct MPI25_IEEE_SGE_CHAIN64 *)
1746 &io_request->SGL, cmd);
1747
1748 if (sge_count > instance->max_num_sge) {
1749 printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1750 "max (0x%x) allowed\n", sge_count,
1751 instance->max_num_sge);
1752 return 1;
1753 }
1754
1755 io_request->RaidContext.numSGE = sge_count;
1756
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301757 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
adam radford9c915a82010-12-21 13:34:31 -08001758
1759 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301760 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
adam radford9c915a82010-12-21 13:34:31 -08001761 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301762 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
adam radford9c915a82010-12-21 13:34:31 -08001763
1764 io_request->SGLOffset0 =
1765 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1766
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301767 io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08001768 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1769
1770 cmd->scmd = scp;
1771 scp->SCp.ptr = (char *)cmd;
1772
1773 return 0;
1774}
1775
1776union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1777megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1778{
1779 u8 *p;
1780 struct fusion_context *fusion;
1781
1782 if (index >= instance->max_fw_cmds) {
1783 printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
adam radford229fe472014-03-10 02:51:56 -07001784 "descriptor for scsi%d\n", index,
1785 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08001786 return NULL;
1787 }
1788 fusion = instance->ctrl_context;
1789 p = fusion->req_frames_desc
1790 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1791
1792 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1793}
1794
1795/**
1796 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1797 * issuing non IOCTL cmd
1798 * @instance: Adapter soft state
1799 * @scmd: pointer to scsi cmd from OS
1800 */
1801static u32
1802megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1803 struct scsi_cmnd *scmd)
1804{
1805 struct megasas_cmd_fusion *cmd;
1806 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1807 u32 index;
1808 struct fusion_context *fusion;
1809
1810 fusion = instance->ctrl_context;
1811
1812 cmd = megasas_get_cmd_fusion(instance);
1813 if (!cmd)
1814 return SCSI_MLQUEUE_HOST_BUSY;
1815
1816 index = cmd->index;
1817
1818 req_desc = megasas_get_request_descriptor(instance, index-1);
1819 if (!req_desc)
1820 return 1;
1821
1822 req_desc->Words = 0;
1823 cmd->request_desc = req_desc;
adam radford9c915a82010-12-21 13:34:31 -08001824
1825 if (megasas_build_io_fusion(instance, scmd, cmd)) {
1826 megasas_return_cmd_fusion(instance, cmd);
1827 printk(KERN_ERR "megasas: Error building command.\n");
1828 cmd->request_desc = NULL;
1829 return 1;
1830 }
1831
1832 req_desc = cmd->request_desc;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301833 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08001834
1835 if (cmd->io_request->ChainOffset != 0 &&
1836 cmd->io_request->ChainOffset != 0xF)
1837 printk(KERN_ERR "megasas: The chain offset value is not "
1838 "correct : %x\n", cmd->io_request->ChainOffset);
1839
1840 /*
1841 * Issue the command to the FW
1842 */
1843 atomic_inc(&instance->fw_outstanding);
1844
1845 instance->instancet->fire_cmd(instance,
1846 req_desc->u.low, req_desc->u.high,
1847 instance->reg_set);
1848
1849 return 0;
1850}
1851
1852/**
1853 * complete_cmd_fusion - Completes command
1854 * @instance: Adapter soft state
1855 * Completes all commands that is in reply descriptor queue
1856 */
1857int
adam radfordc8e858f2011-10-08 18:15:13 -07001858complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
adam radford9c915a82010-12-21 13:34:31 -08001859{
1860 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1861 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1862 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1863 struct fusion_context *fusion;
1864 struct megasas_cmd *cmd_mfi;
1865 struct megasas_cmd_fusion *cmd_fusion;
1866 u16 smid, num_completed;
1867 u8 reply_descript_type, arm;
1868 u32 status, extStatus, device_id;
1869 union desc_value d_val;
1870 struct LD_LOAD_BALANCE_INFO *lbinfo;
1871
1872 fusion = instance->ctrl_context;
1873
1874 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1875 return IRQ_HANDLED;
1876
1877 desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -07001878 desc += ((MSIxIndex * fusion->reply_alloc_sz)/
1879 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
1880 fusion->last_reply_idx[MSIxIndex];
adam radford9c915a82010-12-21 13:34:31 -08001881
1882 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1883
1884 d_val.word = desc->Words;
1885
1886 reply_descript_type = reply_desc->ReplyFlags &
1887 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1888
1889 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1890 return IRQ_NONE;
1891
adam radford9c915a82010-12-21 13:34:31 -08001892 num_completed = 0;
1893
1894 while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301895 smid = le16_to_cpu(reply_desc->SMID);
adam radford9c915a82010-12-21 13:34:31 -08001896
1897 cmd_fusion = fusion->cmd_list[smid - 1];
1898
1899 scsi_io_req =
1900 (struct MPI2_RAID_SCSI_IO_REQUEST *)
1901 cmd_fusion->io_request;
1902
1903 if (cmd_fusion->scmd)
1904 cmd_fusion->scmd->SCp.ptr = NULL;
1905
1906 status = scsi_io_req->RaidContext.status;
1907 extStatus = scsi_io_req->RaidContext.exStatus;
1908
1909 switch (scsi_io_req->Function) {
1910 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
1911 /* Update load balancing info */
1912 device_id = MEGASAS_DEV_INDEX(instance,
1913 cmd_fusion->scmd);
1914 lbinfo = &fusion->load_balance_info[device_id];
1915 if (cmd_fusion->scmd->SCp.Status &
1916 MEGASAS_LOAD_BALANCE_FLAG) {
1917 arm = lbinfo->raid1DevHandle[0] ==
1918 cmd_fusion->io_request->DevHandle ? 0 :
1919 1;
1920 atomic_dec(&lbinfo->scsi_pending_cmds[arm]);
1921 cmd_fusion->scmd->SCp.Status &=
1922 ~MEGASAS_LOAD_BALANCE_FLAG;
1923 }
1924 if (reply_descript_type ==
1925 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
1926 if (megasas_dbg_lvl == 5)
1927 printk(KERN_ERR "\nmegasas: FAST Path "
1928 "IO Success\n");
1929 }
1930 /* Fall thru and complete IO */
1931 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
1932 /* Map the FW Cmd Status */
1933 map_cmd_status(cmd_fusion, status, extStatus);
1934 scsi_dma_unmap(cmd_fusion->scmd);
1935 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
1936 scsi_io_req->RaidContext.status = 0;
1937 scsi_io_req->RaidContext.exStatus = 0;
1938 megasas_return_cmd_fusion(instance, cmd_fusion);
1939 atomic_dec(&instance->fw_outstanding);
1940
1941 break;
1942 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
1943 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1944 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1945 cmd_fusion->flags = 0;
1946 megasas_return_cmd_fusion(instance, cmd_fusion);
1947
1948 break;
1949 }
1950
adam radfordc8e858f2011-10-08 18:15:13 -07001951 fusion->last_reply_idx[MSIxIndex]++;
1952 if (fusion->last_reply_idx[MSIxIndex] >=
1953 fusion->reply_q_depth)
1954 fusion->last_reply_idx[MSIxIndex] = 0;
adam radford9c915a82010-12-21 13:34:31 -08001955
1956 desc->Words = ULLONG_MAX;
1957 num_completed++;
1958
1959 /* Get the next reply descriptor */
adam radfordc8e858f2011-10-08 18:15:13 -07001960 if (!fusion->last_reply_idx[MSIxIndex])
1961 desc = fusion->reply_frames_desc +
1962 ((MSIxIndex * fusion->reply_alloc_sz)/
1963 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
adam radford9c915a82010-12-21 13:34:31 -08001964 else
1965 desc++;
1966
1967 reply_desc =
1968 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1969
1970 d_val.word = desc->Words;
1971
1972 reply_descript_type = reply_desc->ReplyFlags &
1973 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1974
1975 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1976 break;
1977 }
1978
1979 if (!num_completed)
1980 return IRQ_NONE;
1981
1982 wmb();
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05301983 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1984 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
1985 writel(((MSIxIndex & 0x7) << 24) |
1986 fusion->last_reply_idx[MSIxIndex],
1987 instance->reply_post_host_index_addr[MSIxIndex/8]);
1988 else
1989 writel((MSIxIndex << 24) |
1990 fusion->last_reply_idx[MSIxIndex],
1991 instance->reply_post_host_index_addr[0]);
adam radford53ef2bb2011-02-24 20:56:05 -08001992 megasas_check_and_restore_queue_depth(instance);
adam radford9c915a82010-12-21 13:34:31 -08001993 return IRQ_HANDLED;
1994}
1995
1996/**
1997 * megasas_complete_cmd_dpc_fusion - Completes command
1998 * @instance: Adapter soft state
1999 *
2000 * Tasklet to complete cmds
2001 */
2002void
2003megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
2004{
2005 struct megasas_instance *instance =
2006 (struct megasas_instance *)instance_addr;
2007 unsigned long flags;
adam radfordc8e858f2011-10-08 18:15:13 -07002008 u32 count, MSIxIndex;
2009
2010 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
adam radford9c915a82010-12-21 13:34:31 -08002011
2012 /* If we have already declared adapter dead, donot complete cmds */
2013 spin_lock_irqsave(&instance->hba_lock, flags);
2014 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2015 spin_unlock_irqrestore(&instance->hba_lock, flags);
2016 return;
2017 }
2018 spin_unlock_irqrestore(&instance->hba_lock, flags);
2019
adam radfordc8e858f2011-10-08 18:15:13 -07002020 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
2021 complete_cmd_fusion(instance, MSIxIndex);
adam radford9c915a82010-12-21 13:34:31 -08002022}
2023
2024/**
2025 * megasas_isr_fusion - isr entry point
2026 */
2027irqreturn_t megasas_isr_fusion(int irq, void *devp)
2028{
adam radfordc8e858f2011-10-08 18:15:13 -07002029 struct megasas_irq_context *irq_context = devp;
2030 struct megasas_instance *instance = irq_context->instance;
adam radford9c915a82010-12-21 13:34:31 -08002031 u32 mfiStatus, fw_state;
2032
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302033 if (instance->mask_interrupts)
2034 return IRQ_NONE;
2035
adam radfordc8e858f2011-10-08 18:15:13 -07002036 if (!instance->msix_vectors) {
adam radford9c915a82010-12-21 13:34:31 -08002037 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
2038 if (!mfiStatus)
2039 return IRQ_NONE;
2040 }
2041
2042 /* If we are resetting, bail */
adam radford6497b242011-10-08 18:14:50 -07002043 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
2044 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08002045 return IRQ_HANDLED;
adam radford6497b242011-10-08 18:14:50 -07002046 }
adam radford9c915a82010-12-21 13:34:31 -08002047
adam radfordc8e858f2011-10-08 18:15:13 -07002048 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
adam radford6497b242011-10-08 18:14:50 -07002049 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08002050 /* If we didn't complete any commands, check for FW fault */
2051 fw_state = instance->instancet->read_fw_status_reg(
2052 instance->reg_set) & MFI_STATE_MASK;
adam radford229fe472014-03-10 02:51:56 -07002053 if (fw_state == MFI_STATE_FAULT) {
2054 printk(KERN_WARNING "megaraid_sas: Iop2SysDoorbellInt"
2055 "for scsi%d\n", instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002056 schedule_work(&instance->work_init);
adam radford229fe472014-03-10 02:51:56 -07002057 }
adam radford9c915a82010-12-21 13:34:31 -08002058 }
2059
2060 return IRQ_HANDLED;
2061}
2062
2063/**
2064 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
2065 * @instance: Adapter soft state
2066 * mfi_cmd: megasas_cmd pointer
2067 *
2068 */
2069u8
2070build_mpt_mfi_pass_thru(struct megasas_instance *instance,
2071 struct megasas_cmd *mfi_cmd)
2072{
2073 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
2074 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
2075 struct megasas_cmd_fusion *cmd;
2076 struct fusion_context *fusion;
2077 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
2078
2079 cmd = megasas_get_cmd_fusion(instance);
2080 if (!cmd)
2081 return 1;
2082
2083 /* Save the smid. To be used for returning the cmd */
2084 mfi_cmd->context.smid = cmd->index;
2085
2086 cmd->sync_cmd_idx = mfi_cmd->index;
2087
2088 /*
2089 * For cmds where the flag is set, store the flag and check
2090 * on completion. For cmds with this flag, don't call
2091 * megasas_complete_cmd
2092 */
2093
2094 if (frame_hdr->flags & MFI_FRAME_DONT_POST_IN_REPLY_QUEUE)
2095 cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
2096
2097 fusion = instance->ctrl_context;
2098 io_req = cmd->io_request;
adam radford36807e62011-10-08 18:15:06 -07002099
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05302100 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2101 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07002102 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
2103 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
2104 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2105 sgl_ptr_end->Flags = 0;
2106 }
2107
adam radford9c915a82010-12-21 13:34:31 -08002108 mpi25_ieee_chain =
2109 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
2110
2111 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
2112 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
2113 SGL) / 4;
2114 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
2115
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302116 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08002117
2118 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2119 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
2120
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302121 mpi25_ieee_chain->Length = cpu_to_le32(MEGASAS_MAX_SZ_CHAIN_FRAME);
adam radford9c915a82010-12-21 13:34:31 -08002122
2123 return 0;
2124}
2125
2126/**
2127 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
2128 * @instance: Adapter soft state
2129 * @cmd: mfi cmd to build
2130 *
2131 */
2132union MEGASAS_REQUEST_DESCRIPTOR_UNION *
2133build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
2134{
2135 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2136 u16 index;
2137
2138 if (build_mpt_mfi_pass_thru(instance, cmd)) {
2139 printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
2140 return NULL;
2141 }
2142
2143 index = cmd->context.smid;
2144
2145 req_desc = megasas_get_request_descriptor(instance, index - 1);
2146
2147 if (!req_desc)
2148 return NULL;
2149
2150 req_desc->Words = 0;
2151 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2152 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2153
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302154 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08002155
2156 return req_desc;
2157}
2158
2159/**
2160 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
2161 * @instance: Adapter soft state
2162 * @cmd: mfi cmd pointer
2163 *
2164 */
2165void
2166megasas_issue_dcmd_fusion(struct megasas_instance *instance,
2167 struct megasas_cmd *cmd)
2168{
2169 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
adam radford9c915a82010-12-21 13:34:31 -08002170
2171 req_desc = build_mpt_cmd(instance, cmd);
2172 if (!req_desc) {
2173 printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
2174 return;
2175 }
adam radford9c915a82010-12-21 13:34:31 -08002176 instance->instancet->fire_cmd(instance, req_desc->u.low,
2177 req_desc->u.high, instance->reg_set);
2178}
2179
2180/**
2181 * megasas_release_fusion - Reverses the FW initialization
2182 * @intance: Adapter soft state
2183 */
2184void
2185megasas_release_fusion(struct megasas_instance *instance)
2186{
2187 megasas_free_cmds(instance);
2188 megasas_free_cmds_fusion(instance);
2189
2190 iounmap(instance->reg_set);
2191
2192 pci_release_selected_regions(instance->pdev, instance->bar);
2193}
2194
2195/**
2196 * megasas_read_fw_status_reg_fusion - returns the current FW status value
2197 * @regs: MFI register set
2198 */
2199static u32
2200megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
2201{
2202 return readl(&(regs)->outbound_scratch_pad);
2203}
2204
2205/**
2206 * megasas_adp_reset_fusion - For controller reset
2207 * @regs: MFI register set
2208 */
2209static int
2210megasas_adp_reset_fusion(struct megasas_instance *instance,
2211 struct megasas_register_set __iomem *regs)
2212{
2213 return 0;
2214}
2215
2216/**
2217 * megasas_check_reset_fusion - For controller reset check
2218 * @regs: MFI register set
2219 */
2220static int
2221megasas_check_reset_fusion(struct megasas_instance *instance,
2222 struct megasas_register_set __iomem *regs)
2223{
2224 return 0;
2225}
2226
2227/* This function waits for outstanding commands on fusion to complete */
adam radford229fe472014-03-10 02:51:56 -07002228int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
2229 int iotimeout, int *convert)
adam radford9c915a82010-12-21 13:34:31 -08002230{
adam radford229fe472014-03-10 02:51:56 -07002231 int i, outstanding, retval = 0, hb_seconds_missed = 0;
adam radfordc007b8b2012-07-17 18:20:24 -07002232 u32 fw_state;
adam radford9c915a82010-12-21 13:34:31 -08002233
adam radfordc007b8b2012-07-17 18:20:24 -07002234 for (i = 0; i < resetwaittime; i++) {
adam radford9c915a82010-12-21 13:34:31 -08002235 /* Check if firmware is in fault state */
2236 fw_state = instance->instancet->read_fw_status_reg(
2237 instance->reg_set) & MFI_STATE_MASK;
2238 if (fw_state == MFI_STATE_FAULT) {
2239 printk(KERN_WARNING "megasas: Found FW in FAULT state,"
adam radford229fe472014-03-10 02:51:56 -07002240 " will reset adapter scsi%d.\n",
2241 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002242 retval = 1;
2243 goto out;
2244 }
adam radford229fe472014-03-10 02:51:56 -07002245 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
2246 if (instance->requestorId && !iotimeout) {
2247 retval = 1;
2248 goto out;
2249 }
2250
2251 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
2252 if (instance->requestorId && iotimeout) {
2253 if (instance->hb_host_mem->HB.fwCounter !=
2254 instance->hb_host_mem->HB.driverCounter) {
2255 instance->hb_host_mem->HB.driverCounter =
2256 instance->hb_host_mem->HB.fwCounter;
2257 hb_seconds_missed = 0;
2258 } else {
2259 hb_seconds_missed++;
2260 if (hb_seconds_missed ==
2261 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
2262 printk(KERN_WARNING "megasas: SR-IOV:"
2263 " Heartbeat never completed "
2264 " while polling during I/O "
2265 " timeout handling for "
2266 "scsi%d.\n",
2267 instance->host->host_no);
2268 *convert = 1;
2269 retval = 1;
2270 goto out;
2271 }
2272 }
2273 }
adam radford9c915a82010-12-21 13:34:31 -08002274
2275 outstanding = atomic_read(&instance->fw_outstanding);
2276 if (!outstanding)
2277 goto out;
2278
2279 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2280 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
adam radford229fe472014-03-10 02:51:56 -07002281 "commands to complete for scsi%d\n", i,
2282 outstanding, instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002283 megasas_complete_cmd_dpc_fusion(
2284 (unsigned long)instance);
2285 }
2286 msleep(1000);
2287 }
2288
2289 if (atomic_read(&instance->fw_outstanding)) {
2290 printk("megaraid_sas: pending commands remain after waiting, "
adam radford229fe472014-03-10 02:51:56 -07002291 "will reset adapter scsi%d.\n",
2292 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002293 retval = 1;
2294 }
2295out:
2296 return retval;
2297}
2298
2299void megasas_reset_reply_desc(struct megasas_instance *instance)
2300{
adam radfordc8e858f2011-10-08 18:15:13 -07002301 int i, count;
adam radford9c915a82010-12-21 13:34:31 -08002302 struct fusion_context *fusion;
2303 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2304
2305 fusion = instance->ctrl_context;
adam radfordc8e858f2011-10-08 18:15:13 -07002306 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2307 for (i = 0 ; i < count ; i++)
2308 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -08002309 reply_desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -07002310 for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
adam radford9c915a82010-12-21 13:34:31 -08002311 reply_desc->Words = ULLONG_MAX;
2312}
2313
adam radford229fe472014-03-10 02:51:56 -07002314/* Check for a second path that is currently UP */
2315int megasas_check_mpio_paths(struct megasas_instance *instance,
2316 struct scsi_cmnd *scmd)
adam radford9c915a82010-12-21 13:34:31 -08002317{
adam radford229fe472014-03-10 02:51:56 -07002318 int i, j, retval = (DID_RESET << 16);
2319
2320 if (instance->mpio && instance->requestorId) {
2321 for (i = 0 ; i < MAX_MGMT_ADAPTERS ; i++)
2322 for (j = 0 ; j < MAX_LOGICAL_DRIVES; j++)
2323 if (megasas_mgmt_info.instance[i] &&
2324 (megasas_mgmt_info.instance[i] != instance) &&
2325 megasas_mgmt_info.instance[i]->mpio &&
2326 megasas_mgmt_info.instance[i]->requestorId
2327 &&
2328 (megasas_mgmt_info.instance[i]->ld_ids[j]
2329 == scmd->device->id)) {
2330 retval = (DID_NO_CONNECT << 16);
2331 goto out;
2332 }
2333 }
2334out:
2335 return retval;
2336}
2337
2338/* Core fusion reset function */
2339int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout)
2340{
2341 int retval = SUCCESS, i, j, retry = 0, convert = 0;
adam radford9c915a82010-12-21 13:34:31 -08002342 struct megasas_instance *instance;
2343 struct megasas_cmd_fusion *cmd_fusion;
2344 struct fusion_context *fusion;
2345 struct megasas_cmd *cmd_mfi;
2346 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
adam radford7e70e732011-05-11 18:34:08 -07002347 u32 host_diag, abs_state, status_reg, reset_adapter;
adam radford9c915a82010-12-21 13:34:31 -08002348
2349 instance = (struct megasas_instance *)shost->hostdata;
2350 fusion = instance->ctrl_context;
2351
adam radford229fe472014-03-10 02:51:56 -07002352 mutex_lock(&instance->reset_mutex);
2353
adam radford9c915a82010-12-21 13:34:31 -08002354 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2355 printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
adam radford229fe472014-03-10 02:51:56 -07002356 "returning FAILED for scsi%d.\n",
2357 instance->host->host_no);
adam radfordd4a759a2011-10-08 18:14:39 -07002358 return FAILED;
adam radford9c915a82010-12-21 13:34:31 -08002359 }
2360
adam radford229fe472014-03-10 02:51:56 -07002361 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
2362 del_timer_sync(&instance->sriov_heartbeat_timer);
adam radford7e70e732011-05-11 18:34:08 -07002363 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
adam radford229fe472014-03-10 02:51:56 -07002364 instance->adprecovery = MEGASAS_ADPRESET_SM_POLLING;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302365 instance->instancet->disable_intr(instance);
adam radford7e70e732011-05-11 18:34:08 -07002366 msleep(1000);
2367
adam radford9c915a82010-12-21 13:34:31 -08002368 /* First try waiting for commands to complete */
adam radford229fe472014-03-10 02:51:56 -07002369 if (megasas_wait_for_outstanding_fusion(instance, iotimeout,
2370 &convert)) {
2371 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
adam radford9c915a82010-12-21 13:34:31 -08002372 printk(KERN_WARNING "megaraid_sas: resetting fusion "
adam radford229fe472014-03-10 02:51:56 -07002373 "adapter scsi%d.\n", instance->host->host_no);
2374 if (convert)
2375 iotimeout = 0;
2376
adam radford9c915a82010-12-21 13:34:31 -08002377 /* Now return commands back to the OS */
2378 for (i = 0 ; i < instance->max_fw_cmds; i++) {
2379 cmd_fusion = fusion->cmd_list[i];
2380 if (cmd_fusion->scmd) {
2381 scsi_dma_unmap(cmd_fusion->scmd);
adam radford229fe472014-03-10 02:51:56 -07002382 cmd_fusion->scmd->result =
2383 megasas_check_mpio_paths(instance,
2384 cmd_fusion->scmd);
adam radford9c915a82010-12-21 13:34:31 -08002385 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2386 megasas_return_cmd_fusion(instance, cmd_fusion);
2387 atomic_dec(&instance->fw_outstanding);
2388 }
2389 }
2390
adam radford7e70e732011-05-11 18:34:08 -07002391 status_reg = instance->instancet->read_fw_status_reg(
2392 instance->reg_set);
2393 abs_state = status_reg & MFI_STATE_MASK;
2394 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2395 if (instance->disableOnlineCtrlReset ||
2396 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
adam radford9c915a82010-12-21 13:34:31 -08002397 /* Reset not supported, kill adapter */
2398 printk(KERN_WARNING "megaraid_sas: Reset not supported"
adam radford229fe472014-03-10 02:51:56 -07002399 ", killing adapter scsi%d.\n",
2400 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002401 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07002402 instance->skip_heartbeat_timer_del = 1;
adam radford9c915a82010-12-21 13:34:31 -08002403 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2404 retval = FAILED;
2405 goto out;
2406 }
2407
adam radford229fe472014-03-10 02:51:56 -07002408 /* Let SR-IOV VF & PF sync up if there was a HB failure */
2409 if (instance->requestorId && !iotimeout) {
2410 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
2411 /* Look for a late HB update after VF settle time */
2412 if (abs_state == MFI_STATE_OPERATIONAL &&
2413 (instance->hb_host_mem->HB.fwCounter !=
2414 instance->hb_host_mem->HB.driverCounter)) {
2415 instance->hb_host_mem->HB.driverCounter =
2416 instance->hb_host_mem->HB.fwCounter;
2417 printk(KERN_WARNING "megasas: SR-IOV:"
2418 "Late FW heartbeat update for "
2419 "scsi%d.\n",
2420 instance->host->host_no);
2421 } else {
2422 /* In VF mode, first poll for FW ready */
2423 for (i = 0;
2424 i < (MEGASAS_RESET_WAIT_TIME * 1000);
2425 i += 20) {
2426 status_reg =
2427 instance->instancet->
2428 read_fw_status_reg(
2429 instance->reg_set);
2430 abs_state = status_reg &
2431 MFI_STATE_MASK;
2432 if (abs_state == MFI_STATE_READY) {
2433 printk(KERN_WARNING "megasas"
2434 ": SR-IOV: FW was found"
2435 "to be in ready state "
2436 "for scsi%d.\n",
2437 instance->host->host_no);
2438 break;
2439 }
2440 msleep(20);
2441 }
2442 if (abs_state != MFI_STATE_READY) {
2443 printk(KERN_WARNING "megasas: SR-IOV: "
2444 "FW not in ready state after %d"
2445 " seconds for scsi%d, status_reg = "
2446 "0x%x.\n",
2447 MEGASAS_RESET_WAIT_TIME,
2448 instance->host->host_no,
2449 status_reg);
2450 megaraid_sas_kill_hba(instance);
2451 instance->skip_heartbeat_timer_del = 1;
2452 instance->adprecovery =
2453 MEGASAS_HW_CRITICAL_ERROR;
2454 retval = FAILED;
2455 goto out;
2456 }
2457 }
2458 }
2459
adam radford9c915a82010-12-21 13:34:31 -08002460 /* Now try to reset the chip */
2461 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2462 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2463 &instance->reg_set->fusion_seq_offset);
2464 writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2465 &instance->reg_set->fusion_seq_offset);
2466 writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2467 &instance->reg_set->fusion_seq_offset);
2468 writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2469 &instance->reg_set->fusion_seq_offset);
2470 writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2471 &instance->reg_set->fusion_seq_offset);
2472 writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2473 &instance->reg_set->fusion_seq_offset);
2474 writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2475 &instance->reg_set->fusion_seq_offset);
2476
2477 /* Check that the diag write enable (DRWE) bit is on */
2478 host_diag = readl(&instance->reg_set->fusion_host_diag);
adam radford7e70e732011-05-11 18:34:08 -07002479 retry = 0;
adam radford9c915a82010-12-21 13:34:31 -08002480 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2481 msleep(100);
2482 host_diag =
2483 readl(&instance->reg_set->fusion_host_diag);
2484 if (retry++ == 100) {
2485 printk(KERN_WARNING "megaraid_sas: "
adam radford229fe472014-03-10 02:51:56 -07002486 "Host diag unlock failed! "
2487 "for scsi%d\n",
2488 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002489 break;
2490 }
2491 }
2492 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2493 continue;
2494
2495 /* Send chip reset command */
2496 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2497 &instance->reg_set->fusion_host_diag);
2498 msleep(3000);
2499
2500 /* Make sure reset adapter bit is cleared */
2501 host_diag = readl(&instance->reg_set->fusion_host_diag);
2502 retry = 0;
2503 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2504 msleep(100);
2505 host_diag =
2506 readl(&instance->reg_set->fusion_host_diag);
2507 if (retry++ == 1000) {
2508 printk(KERN_WARNING "megaraid_sas: "
2509 "Diag reset adapter never "
adam radford229fe472014-03-10 02:51:56 -07002510 "cleared for scsi%d!\n",
2511 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002512 break;
2513 }
2514 }
2515 if (host_diag & HOST_DIAG_RESET_ADAPTER)
2516 continue;
2517
2518 abs_state =
2519 instance->instancet->read_fw_status_reg(
adam radford7e70e732011-05-11 18:34:08 -07002520 instance->reg_set) & MFI_STATE_MASK;
adam radford9c915a82010-12-21 13:34:31 -08002521 retry = 0;
2522
2523 while ((abs_state <= MFI_STATE_FW_INIT) &&
2524 (retry++ < 1000)) {
2525 msleep(100);
2526 abs_state =
2527 instance->instancet->read_fw_status_reg(
adam radford7e70e732011-05-11 18:34:08 -07002528 instance->reg_set) & MFI_STATE_MASK;
adam radford9c915a82010-12-21 13:34:31 -08002529 }
2530 if (abs_state <= MFI_STATE_FW_INIT) {
2531 printk(KERN_WARNING "megaraid_sas: firmware "
2532 "state < MFI_STATE_FW_INIT, state = "
adam radford229fe472014-03-10 02:51:56 -07002533 "0x%x for scsi%d\n", abs_state,
2534 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002535 continue;
2536 }
2537
2538 /* Wait for FW to become ready */
adam radford058a8fa2011-10-08 18:14:27 -07002539 if (megasas_transition_to_ready(instance, 1)) {
adam radford9c915a82010-12-21 13:34:31 -08002540 printk(KERN_WARNING "megaraid_sas: Failed to "
adam radford229fe472014-03-10 02:51:56 -07002541 "transition controller to ready "
2542 "for scsi%d.\n",
2543 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002544 continue;
2545 }
2546
2547 megasas_reset_reply_desc(instance);
2548 if (megasas_ioc_init_fusion(instance)) {
2549 printk(KERN_WARNING "megaraid_sas: "
adam radford229fe472014-03-10 02:51:56 -07002550 "megasas_ioc_init_fusion() failed!"
2551 " for scsi%d\n",
2552 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002553 continue;
2554 }
2555
adam radford9c915a82010-12-21 13:34:31 -08002556 /* Re-fire management commands */
2557 for (j = 0 ; j < instance->max_fw_cmds; j++) {
2558 cmd_fusion = fusion->cmd_list[j];
2559 if (cmd_fusion->sync_cmd_idx !=
2560 (u32)ULONG_MAX) {
2561 cmd_mfi =
2562 instance->
2563 cmd_list[cmd_fusion->sync_cmd_idx];
2564 if (cmd_mfi->frame->dcmd.opcode ==
Sumit.Saxena@lsi.combe263742014-02-12 23:37:46 +05302565 cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO)) {
adam radford9c915a82010-12-21 13:34:31 -08002566 megasas_return_cmd(instance,
2567 cmd_mfi);
2568 megasas_return_cmd_fusion(
2569 instance, cmd_fusion);
2570 } else {
2571 req_desc =
2572 megasas_get_request_descriptor(
2573 instance,
2574 cmd_mfi->context.smid
2575 -1);
adam radford3d0c24c2014-03-10 02:51:45 -07002576 if (!req_desc) {
adam radford9c915a82010-12-21 13:34:31 -08002577 printk(KERN_WARNING
2578 "req_desc NULL"
adam radford229fe472014-03-10 02:51:56 -07002579 " for scsi%d\n",
2580 instance->host->host_no);
adam radford3d0c24c2014-03-10 02:51:45 -07002581 /* Return leaked MPT
2582 frame */
2583 megasas_return_cmd_fusion(instance, cmd_fusion);
2584 } else {
adam radford9c915a82010-12-21 13:34:31 -08002585 instance->instancet->
2586 fire_cmd(instance,
2587 req_desc->
2588 u.low,
2589 req_desc->
2590 u.high,
2591 instance->
2592 reg_set);
2593 }
2594 }
2595 }
2596 }
2597
adam radford229fe472014-03-10 02:51:56 -07002598 clear_bit(MEGASAS_FUSION_IN_RESET,
2599 &instance->reset_flags);
2600 instance->instancet->enable_intr(instance);
2601 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2602
adam radford9c915a82010-12-21 13:34:31 -08002603 /* Reset load balance info */
2604 memset(fusion->load_balance_info, 0,
2605 sizeof(struct LD_LOAD_BALANCE_INFO)
2606 *MAX_LOGICAL_DRIVES);
2607
2608 if (!megasas_get_map_info(instance))
2609 megasas_sync_map_info(instance);
2610
adam radford229fe472014-03-10 02:51:56 -07002611 /* Restart SR-IOV heartbeat */
2612 if (instance->requestorId) {
2613 if (!megasas_sriov_start_heartbeat(instance, 0))
2614 megasas_start_timer(instance,
2615 &instance->sriov_heartbeat_timer,
2616 megasas_sriov_heartbeat_handler,
2617 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2618 else
2619 instance->skip_heartbeat_timer_del = 1;
2620 }
2621
adam radford9c915a82010-12-21 13:34:31 -08002622 /* Adapter reset completed successfully */
2623 printk(KERN_WARNING "megaraid_sas: Reset "
adam radford229fe472014-03-10 02:51:56 -07002624 "successful for scsi%d.\n",
2625 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002626 retval = SUCCESS;
2627 goto out;
2628 }
2629 /* Reset failed, kill the adapter */
2630 printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
adam radford229fe472014-03-10 02:51:56 -07002631 "adapter scsi%d.\n", instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002632 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07002633 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05302634 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
adam radford9c915a82010-12-21 13:34:31 -08002635 retval = FAILED;
2636 } else {
adam radford229fe472014-03-10 02:51:56 -07002637 /* For VF: Restart HB timer if we didn't OCR */
2638 if (instance->requestorId) {
2639 megasas_start_timer(instance,
2640 &instance->sriov_heartbeat_timer,
2641 megasas_sriov_heartbeat_handler,
2642 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2643 }
adam radford3f0e58bc2011-10-08 18:14:59 -07002644 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302645 instance->instancet->enable_intr(instance);
adam radford9c915a82010-12-21 13:34:31 -08002646 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2647 }
2648out:
2649 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2650 mutex_unlock(&instance->reset_mutex);
2651 return retval;
2652}
2653
2654/* Fusion OCR work queue */
2655void megasas_fusion_ocr_wq(struct work_struct *work)
2656{
2657 struct megasas_instance *instance =
2658 container_of(work, struct megasas_instance, work_init);
2659
adam radford229fe472014-03-10 02:51:56 -07002660 megasas_reset_fusion(instance->host, 0);
adam radford9c915a82010-12-21 13:34:31 -08002661}
2662
2663struct megasas_instance_template megasas_instance_template_fusion = {
2664 .fire_cmd = megasas_fire_cmd_fusion,
2665 .enable_intr = megasas_enable_intr_fusion,
2666 .disable_intr = megasas_disable_intr_fusion,
2667 .clear_intr = megasas_clear_intr_fusion,
2668 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2669 .adp_reset = megasas_adp_reset_fusion,
2670 .check_reset = megasas_check_reset_fusion,
2671 .service_isr = megasas_isr_fusion,
2672 .tasklet = megasas_complete_cmd_dpc_fusion,
2673 .init_adapter = megasas_init_adapter_fusion,
2674 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2675 .issue_dcmd = megasas_issue_dcmd_fusion,
2676};