blob: 155b8b1a8f4bb248774107b4bad1c11cf1ae545e [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
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +0530974 fusion->reply_q_depth = 2 * (((max_cmd + 1 + 15)/16)*16);
adam radford9c915a82010-12-21 13:34:31 -0800975
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{
Sumit.Saxena@avagotech.com07560402014-09-12 18:57:18 +05301068#if defined(writeq) && defined(CONFIG_64BIT)
1069 u64 req_data = (((u64)req_desc_hi << 32) | (u32)req_desc_lo);
1070
1071 writeq(le64_to_cpu(req_data), &(regs)->inbound_low_queue_port);
1072#else
adam radford9c915a82010-12-21 13:34:31 -08001073 unsigned long flags;
1074
1075 spin_lock_irqsave(&instance->hba_lock, flags);
1076
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301077 writel(le32_to_cpu(req_desc_lo), &(regs)->inbound_low_queue_port);
1078 writel(le32_to_cpu(req_desc_hi), &(regs)->inbound_high_queue_port);
adam radford9c915a82010-12-21 13:34:31 -08001079 spin_unlock_irqrestore(&instance->hba_lock, flags);
Sumit.Saxena@avagotech.com07560402014-09-12 18:57:18 +05301080#endif
adam radford9c915a82010-12-21 13:34:31 -08001081}
1082
1083/**
1084 * map_cmd_status - Maps FW cmd status to OS cmd status
1085 * @cmd : Pointer to cmd
1086 * @status : status of cmd returned by FW
1087 * @ext_status : ext status of cmd returned by FW
1088 */
1089
1090void
1091map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1092{
1093
1094 switch (status) {
1095
1096 case MFI_STAT_OK:
1097 cmd->scmd->result = DID_OK << 16;
1098 break;
1099
1100 case MFI_STAT_SCSI_IO_FAILED:
1101 case MFI_STAT_LD_INIT_IN_PROGRESS:
1102 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1103 break;
1104
1105 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1106
1107 cmd->scmd->result = (DID_OK << 16) | ext_status;
1108 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1109 memset(cmd->scmd->sense_buffer, 0,
1110 SCSI_SENSE_BUFFERSIZE);
1111 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1112 SCSI_SENSE_BUFFERSIZE);
1113 cmd->scmd->result |= DRIVER_SENSE << 24;
1114 }
1115 break;
1116
1117 case MFI_STAT_LD_OFFLINE:
1118 case MFI_STAT_DEVICE_NOT_FOUND:
1119 cmd->scmd->result = DID_BAD_TARGET << 16;
1120 break;
adam radford36807e62011-10-08 18:15:06 -07001121 case MFI_STAT_CONFIG_SEQ_MISMATCH:
1122 cmd->scmd->result = DID_IMM_RETRY << 16;
1123 break;
adam radford9c915a82010-12-21 13:34:31 -08001124 default:
1125 printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1126 cmd->scmd->result = DID_ERROR << 16;
1127 break;
1128 }
1129}
1130
1131/**
1132 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1133 * @instance: Adapter soft state
1134 * @scp: SCSI command from the mid-layer
1135 * @sgl_ptr: SGL to be filled in
1136 * @cmd: cmd we are working on
1137 *
1138 * If successful, this function returns the number of SG elements.
1139 */
1140static int
1141megasas_make_sgl_fusion(struct megasas_instance *instance,
1142 struct scsi_cmnd *scp,
1143 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1144 struct megasas_cmd_fusion *cmd)
1145{
adam radford36807e62011-10-08 18:15:06 -07001146 int i, sg_processed, sge_count;
adam radford9c915a82010-12-21 13:34:31 -08001147 struct scatterlist *os_sgl;
1148 struct fusion_context *fusion;
1149
1150 fusion = instance->ctrl_context;
1151
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301152 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1153 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001154 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1155 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1156 sgl_ptr_end->Flags = 0;
1157 }
adam radford9c915a82010-12-21 13:34:31 -08001158
1159 sge_count = scsi_dma_map(scp);
1160
1161 BUG_ON(sge_count < 0);
1162
1163 if (sge_count > instance->max_num_sge || !sge_count)
1164 return sge_count;
1165
adam radford9c915a82010-12-21 13:34:31 -08001166 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301167 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
1168 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
adam radford9c915a82010-12-21 13:34:31 -08001169 sgl_ptr->Flags = 0;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301170 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1171 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001172 if (i == sge_count - 1)
1173 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1174 }
adam radford9c915a82010-12-21 13:34:31 -08001175 sgl_ptr++;
1176
1177 sg_processed = i + 1;
1178
1179 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1180 (sge_count > fusion->max_sge_in_main_msg)) {
1181
1182 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301183 if ((instance->pdev->device ==
1184 PCI_DEVICE_ID_LSI_INVADER) ||
1185 (instance->pdev->device ==
1186 PCI_DEVICE_ID_LSI_FURY)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301187 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1188 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1189 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
adam radford36807e62011-10-08 18:15:06 -07001190 cmd->io_request->ChainOffset =
1191 fusion->
1192 chain_offset_io_request;
1193 else
1194 cmd->io_request->ChainOffset = 0;
1195 } else
1196 cmd->io_request->ChainOffset =
1197 fusion->chain_offset_io_request;
1198
adam radford9c915a82010-12-21 13:34:31 -08001199 sg_chain = sgl_ptr;
1200 /* Prepare chain element */
1201 sg_chain->NextChainOffset = 0;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301202 if ((instance->pdev->device ==
1203 PCI_DEVICE_ID_LSI_INVADER) ||
1204 (instance->pdev->device ==
1205 PCI_DEVICE_ID_LSI_FURY))
adam radford36807e62011-10-08 18:15:06 -07001206 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1207 else
1208 sg_chain->Flags =
1209 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1210 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301211 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
1212 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08001213
1214 sgl_ptr =
1215 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1216 }
1217 }
1218
1219 return sge_count;
1220}
1221
1222/**
1223 * megasas_set_pd_lba - Sets PD LBA
1224 * @cdb: CDB
1225 * @cdb_len: cdb length
1226 * @start_blk: Start block of IO
1227 *
1228 * Used to set the PD LBA in CDB for FP IOs
1229 */
1230void
1231megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1232 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
1233 struct MR_FW_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
1234{
1235 struct MR_LD_RAID *raid;
1236 u32 ld;
1237 u64 start_blk = io_info->pdBlock;
1238 u8 *cdb = io_request->CDB.CDB32;
1239 u32 num_blocks = io_info->numBlocks;
adam radford495c5602011-05-11 18:34:40 -07001240 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
adam radford9c915a82010-12-21 13:34:31 -08001241
1242 /* Check if T10 PI (DIF) is enabled for this LD */
1243 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1244 raid = MR_LdRaidGet(ld, local_map_ptr);
1245 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1246 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1247 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1248 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1249
1250 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1251 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1252 else
1253 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1254 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1255
1256 /* LBA */
1257 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1258 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1259 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1260 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1261 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1262 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1263 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1264 cdb[19] = (u8)(start_blk & 0xff);
1265
1266 /* Logical block reference tag */
1267 io_request->CDB.EEDP32.PrimaryReferenceTag =
1268 cpu_to_be32(ref_tag);
1269 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0xffff;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301270 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001271
1272 /* Transfer length */
1273 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1274 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1275 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1276 cdb[31] = (u8)(num_blocks & 0xff);
1277
1278 /* set SCSI IO EEDPFlags */
1279 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301280 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001281 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1282 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1283 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1284 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301285 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
adam radford9c915a82010-12-21 13:34:31 -08001286 } else {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301287 io_request->EEDPFlags = cpu_to_le16(
adam radford9c915a82010-12-21 13:34:31 -08001288 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301289 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
adam radford9c915a82010-12-21 13:34:31 -08001290 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301291 io_request->Control |= cpu_to_le32((0x4 << 26));
1292 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
adam radford9c915a82010-12-21 13:34:31 -08001293 } else {
1294 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1295 if (((cdb_len == 12) || (cdb_len == 16)) &&
1296 (start_blk <= 0xffffffff)) {
1297 if (cdb_len == 16) {
1298 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1299 flagvals = cdb[1];
1300 groupnum = cdb[14];
1301 control = cdb[15];
1302 } else {
1303 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1304 flagvals = cdb[1];
1305 groupnum = cdb[10];
1306 control = cdb[11];
1307 }
1308
1309 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1310
1311 cdb[0] = opcode;
1312 cdb[1] = flagvals;
1313 cdb[6] = groupnum;
1314 cdb[9] = control;
1315
1316 /* Transfer length */
1317 cdb[8] = (u8)(num_blocks & 0xff);
1318 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1319
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301320 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001321 cdb_len = 10;
adam radford495c5602011-05-11 18:34:40 -07001322 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1323 /* Convert to 16 byte CDB for large LBA's */
1324 switch (cdb_len) {
1325 case 6:
1326 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1327 control = cdb[5];
1328 break;
1329 case 10:
1330 opcode =
1331 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1332 flagvals = cdb[1];
1333 groupnum = cdb[6];
1334 control = cdb[9];
1335 break;
1336 case 12:
1337 opcode =
1338 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1339 flagvals = cdb[1];
1340 groupnum = cdb[10];
1341 control = cdb[11];
1342 break;
1343 }
1344
1345 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1346
1347 cdb[0] = opcode;
1348 cdb[1] = flagvals;
1349 cdb[14] = groupnum;
1350 cdb[15] = control;
1351
1352 /* Transfer length */
1353 cdb[13] = (u8)(num_blocks & 0xff);
1354 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1355 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1356 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1357
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301358 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
adam radford495c5602011-05-11 18:34:40 -07001359 cdb_len = 16;
adam radford9c915a82010-12-21 13:34:31 -08001360 }
1361
1362 /* Normal case, just load LBA here */
1363 switch (cdb_len) {
1364 case 6:
1365 {
1366 u8 val = cdb[1] & 0xE0;
1367 cdb[3] = (u8)(start_blk & 0xff);
1368 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1369 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1370 break;
1371 }
1372 case 10:
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 12:
1379 cdb[5] = (u8)(start_blk & 0xff);
1380 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1381 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1382 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1383 break;
1384 case 16:
1385 cdb[9] = (u8)(start_blk & 0xff);
1386 cdb[8] = (u8)((start_blk >> 8) & 0xff);
1387 cdb[7] = (u8)((start_blk >> 16) & 0xff);
1388 cdb[6] = (u8)((start_blk >> 24) & 0xff);
1389 cdb[5] = (u8)((start_blk >> 32) & 0xff);
1390 cdb[4] = (u8)((start_blk >> 40) & 0xff);
1391 cdb[3] = (u8)((start_blk >> 48) & 0xff);
1392 cdb[2] = (u8)((start_blk >> 56) & 0xff);
1393 break;
1394 }
1395 }
1396}
1397
1398/**
1399 * megasas_build_ldio_fusion - Prepares IOs to devices
1400 * @instance: Adapter soft state
1401 * @scp: SCSI command
1402 * @cmd: Command to be prepared
1403 *
1404 * Prepares the io_request and chain elements (sg_frame) for IO
1405 * The IO can be for PD (Fast Path) or LD
1406 */
1407void
1408megasas_build_ldio_fusion(struct megasas_instance *instance,
1409 struct scsi_cmnd *scp,
1410 struct megasas_cmd_fusion *cmd)
1411{
1412 u8 fp_possible;
adam radfordf9eff812012-10-01 19:27:07 -07001413 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
adam radford9c915a82010-12-21 13:34:31 -08001414 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1415 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1416 struct IO_REQUEST_INFO io_info;
1417 struct fusion_context *fusion;
1418 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
adam radford21c9e162013-09-06 15:27:14 -07001419 u8 *raidLUN;
adam radford9c915a82010-12-21 13:34:31 -08001420
1421 device_id = MEGASAS_DEV_INDEX(instance, scp);
1422
1423 fusion = instance->ctrl_context;
1424
1425 io_request = cmd->io_request;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301426 io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
adam radford9c915a82010-12-21 13:34:31 -08001427 io_request->RaidContext.status = 0;
1428 io_request->RaidContext.exStatus = 0;
1429
1430 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1431
1432 start_lba_lo = 0;
1433 start_lba_hi = 0;
1434 fp_possible = 0;
1435
1436 /*
1437 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1438 */
1439 if (scp->cmd_len == 6) {
adam radfordf9eff812012-10-01 19:27:07 -07001440 datalength = (u32) scp->cmnd[4];
adam radford9c915a82010-12-21 13:34:31 -08001441 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1442 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1443
1444 start_lba_lo &= 0x1FFFFF;
1445 }
1446
1447 /*
1448 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1449 */
1450 else if (scp->cmd_len == 10) {
adam radfordf9eff812012-10-01 19:27:07 -07001451 datalength = (u32) scp->cmnd[8] |
adam radford9c915a82010-12-21 13:34:31 -08001452 ((u32) scp->cmnd[7] << 8);
1453 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1454 ((u32) scp->cmnd[3] << 16) |
1455 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1456 }
1457
1458 /*
1459 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1460 */
1461 else if (scp->cmd_len == 12) {
adam radfordf9eff812012-10-01 19:27:07 -07001462 datalength = ((u32) scp->cmnd[6] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08001463 ((u32) scp->cmnd[7] << 16) |
1464 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1465 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1466 ((u32) scp->cmnd[3] << 16) |
1467 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1468 }
1469
1470 /*
1471 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1472 */
1473 else if (scp->cmd_len == 16) {
adam radfordf9eff812012-10-01 19:27:07 -07001474 datalength = ((u32) scp->cmnd[10] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08001475 ((u32) scp->cmnd[11] << 16) |
1476 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1477 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1478 ((u32) scp->cmnd[7] << 16) |
1479 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1480
1481 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1482 ((u32) scp->cmnd[3] << 16) |
1483 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1484 }
1485
1486 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1487 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
adam radfordf9eff812012-10-01 19:27:07 -07001488 io_info.numBlocks = datalength;
adam radford9c915a82010-12-21 13:34:31 -08001489 io_info.ldTgtId = device_id;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301490 io_request->DataLength = cpu_to_le32(scsi_bufflen(scp));
adam radford9c915a82010-12-21 13:34:31 -08001491
1492 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1493 io_info.isRead = 1;
1494
1495 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1496
1497 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
1498 MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io)) {
1499 io_request->RaidContext.regLockFlags = 0;
1500 fp_possible = 0;
1501 } else {
adam radford36807e62011-10-08 18:15:06 -07001502 if (MR_BuildRaidContext(instance, &io_info,
1503 &io_request->RaidContext,
adam radford21c9e162013-09-06 15:27:14 -07001504 local_map_ptr, &raidLUN))
adam radford9c915a82010-12-21 13:34:31 -08001505 fp_possible = io_info.fpOkForIo;
1506 }
1507
adam radfordc8e858f2011-10-08 18:15:13 -07001508 /* Use smp_processor_id() for now until cmd->request->cpu is CPU
1509 id by default, not CPU group id, otherwise all MSI-X queues won't
1510 be utilized */
1511 cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
1512 smp_processor_id() % instance->msix_vectors : 0;
1513
adam radford9c915a82010-12-21 13:34:31 -08001514 if (fp_possible) {
1515 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1516 local_map_ptr, start_lba_lo);
adam radford9c915a82010-12-21 13:34:31 -08001517 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1518 cmd->request_desc->SCSIIO.RequestFlags =
1519 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1520 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301521 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1522 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001523 if (io_request->RaidContext.regLockFlags ==
1524 REGION_TYPE_UNUSED)
1525 cmd->request_desc->SCSIIO.RequestFlags =
1526 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1527 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1528 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1529 io_request->RaidContext.nseg = 0x1;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301530 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
adam radford36807e62011-10-08 18:15:06 -07001531 io_request->RaidContext.regLockFlags |=
1532 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
1533 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1534 }
adam radford9c915a82010-12-21 13:34:31 -08001535 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1536 (io_info.isRead)) {
1537 io_info.devHandle =
1538 get_updated_dev_handle(
1539 &fusion->load_balance_info[device_id],
1540 &io_info);
1541 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
1542 } else
1543 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1544 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1545 io_request->DevHandle = io_info.devHandle;
adam radford21c9e162013-09-06 15:27:14 -07001546 /* populate the LUN field */
1547 memcpy(io_request->LUN, raidLUN, 8);
adam radford9c915a82010-12-21 13:34:31 -08001548 } else {
1549 io_request->RaidContext.timeoutValue =
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301550 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
adam radford9c915a82010-12-21 13:34:31 -08001551 cmd->request_desc->SCSIIO.RequestFlags =
1552 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1553 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301554 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1555 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001556 if (io_request->RaidContext.regLockFlags ==
1557 REGION_TYPE_UNUSED)
1558 cmd->request_desc->SCSIIO.RequestFlags =
1559 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1560 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1561 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1562 io_request->RaidContext.regLockFlags |=
1563 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
1564 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1565 io_request->RaidContext.nseg = 0x1;
1566 }
1567 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301568 io_request->DevHandle = cpu_to_le16(device_id);
adam radford9c915a82010-12-21 13:34:31 -08001569 } /* Not FP */
1570}
1571
1572/**
1573 * megasas_build_dcdb_fusion - Prepares IOs to devices
1574 * @instance: Adapter soft state
1575 * @scp: SCSI command
1576 * @cmd: Command to be prepared
1577 *
1578 * Prepares the io_request frame for non-io cmds
1579 */
1580static void
1581megasas_build_dcdb_fusion(struct megasas_instance *instance,
1582 struct scsi_cmnd *scmd,
1583 struct megasas_cmd_fusion *cmd)
1584{
1585 u32 device_id;
1586 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1587 u16 pd_index = 0;
1588 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1589 struct fusion_context *fusion = instance->ctrl_context;
adam radford21c9e162013-09-06 15:27:14 -07001590 u8 span, physArm;
1591 u16 devHandle;
1592 u32 ld, arRef, pd;
1593 struct MR_LD_RAID *raid;
1594 struct RAID_CONTEXT *pRAID_Context;
adam radford9c915a82010-12-21 13:34:31 -08001595
1596 io_request = cmd->io_request;
1597 device_id = MEGASAS_DEV_INDEX(instance, scmd);
1598 pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
1599 +scmd->device->id;
1600 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1601
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301602 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
adam radford21c9e162013-09-06 15:27:14 -07001603
1604
adam radford9c915a82010-12-21 13:34:31 -08001605 /* Check if this is a system PD I/O */
adam radforde187df62012-10-01 19:27:12 -07001606 if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS &&
1607 instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
adam radford9c915a82010-12-21 13:34:31 -08001608 io_request->Function = 0;
adam radford9c5ebd02013-02-09 15:29:20 -08001609 if (fusion->fast_path_io)
1610 io_request->DevHandle =
adam radford9c915a82010-12-21 13:34:31 -08001611 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1612 io_request->RaidContext.timeoutValue =
1613 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1614 io_request->RaidContext.regLockFlags = 0;
1615 io_request->RaidContext.regLockRowLBA = 0;
1616 io_request->RaidContext.regLockLength = 0;
1617 io_request->RaidContext.RAIDFlags =
1618 MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
1619 MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
Sumit.Saxena@lsi.com5d0d9082013-05-22 12:33:29 +05301620 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1621 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
Sumit.Saxena@lsi.combe263742014-02-12 23:37:46 +05301622 io_request->IoFlags |= cpu_to_le16(
1623 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
adam radford9c915a82010-12-21 13:34:31 -08001624 cmd->request_desc->SCSIIO.RequestFlags =
1625 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1626 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford8ead5812012-10-01 19:27:20 -07001627 cmd->request_desc->SCSIIO.DevHandle =
1628 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
Sumit.Saxena@lsi.com8058a162014-03-02 05:28:11 +05301629 cmd->request_desc->SCSIIO.MSIxIndex =
1630 instance->msix_vectors ? smp_processor_id() % instance->msix_vectors : 0;
Sumit.Saxena@lsi.com32d87452013-05-22 12:31:18 +05301631 /*
1632 * If the command is for the tape device, set the
1633 * FP timeout to the os layer timeout value.
1634 */
1635 if (scmd->device->type == TYPE_TAPE) {
1636 if ((scmd->request->timeout / HZ) > 0xFFFF)
1637 io_request->RaidContext.timeoutValue =
1638 0xFFFF;
1639 else
1640 io_request->RaidContext.timeoutValue =
1641 scmd->request->timeout / HZ;
1642 }
adam radford9c915a82010-12-21 13:34:31 -08001643 } else {
adam radford21c9e162013-09-06 15:27:14 -07001644 if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS)
1645 goto NonFastPath;
1646
1647 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
1648 if ((ld >= MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io))
1649 goto NonFastPath;
1650
1651 raid = MR_LdRaidGet(ld, local_map_ptr);
1652
1653 /* check if this LD is FP capable */
1654 if (!(raid->capability.fpNonRWCapable))
1655 /* not FP capable, send as non-FP */
1656 goto NonFastPath;
1657
1658 /* get RAID_Context pointer */
1659 pRAID_Context = &io_request->RaidContext;
1660
1661 /* set RAID context values */
1662 pRAID_Context->regLockFlags = REGION_TYPE_SHARED_READ;
1663 pRAID_Context->timeoutValue = raid->fpIoTimeoutForLd;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301664 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
adam radford21c9e162013-09-06 15:27:14 -07001665 pRAID_Context->regLockRowLBA = 0;
1666 pRAID_Context->regLockLength = 0;
1667 pRAID_Context->configSeqNum = raid->seqNum;
1668
1669 /* get the DevHandle for the PD (since this is
1670 fpNonRWCapable, this is a single disk RAID0) */
1671 span = physArm = 0;
1672 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
1673 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
1674 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
1675
1676 /* build request descriptor */
1677 cmd->request_desc->SCSIIO.RequestFlags =
1678 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1679 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1680 cmd->request_desc->SCSIIO.DevHandle = devHandle;
1681
1682 /* populate the LUN field */
1683 memcpy(io_request->LUN, raid->LUN, 8);
1684
1685 /* build the raidScsiIO structure */
1686 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1687 io_request->DevHandle = devHandle;
1688
1689 return;
1690
1691NonFastPath:
adam radford9c915a82010-12-21 13:34:31 -08001692 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301693 io_request->DevHandle = cpu_to_le16(device_id);
adam radford9c915a82010-12-21 13:34:31 -08001694 cmd->request_desc->SCSIIO.RequestFlags =
1695 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1696 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1697 }
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301698 io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001699 int_to_scsilun(scmd->device->lun, (struct scsi_lun *)io_request->LUN);
adam radford9c915a82010-12-21 13:34:31 -08001700}
1701
1702/**
1703 * megasas_build_io_fusion - Prepares IOs to devices
1704 * @instance: Adapter soft state
1705 * @scp: SCSI command
1706 * @cmd: Command to be prepared
1707 *
1708 * Invokes helper functions to prepare request frames
1709 * and sets flags appropriate for IO/Non-IO cmd
1710 */
1711int
1712megasas_build_io_fusion(struct megasas_instance *instance,
1713 struct scsi_cmnd *scp,
1714 struct megasas_cmd_fusion *cmd)
1715{
1716 u32 device_id, sge_count;
1717 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1718
1719 device_id = MEGASAS_DEV_INDEX(instance, scp);
1720
1721 /* Zero out some fields so they don't get reused */
Hannes Reinecke9cb78c12014-06-25 15:27:36 +02001722 memset(io_request->LUN, 0x0, 8);
adam radford9c915a82010-12-21 13:34:31 -08001723 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1724 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1725 io_request->EEDPFlags = 0;
1726 io_request->Control = 0;
1727 io_request->EEDPBlockSize = 0;
adam radford36807e62011-10-08 18:15:06 -07001728 io_request->ChainOffset = 0;
adam radford9c915a82010-12-21 13:34:31 -08001729 io_request->RaidContext.RAIDFlags = 0;
adam radford36807e62011-10-08 18:15:06 -07001730 io_request->RaidContext.Type = 0;
1731 io_request->RaidContext.nseg = 0;
adam radford9c915a82010-12-21 13:34:31 -08001732
1733 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1734 /*
1735 * Just the CDB length,rest of the Flags are zero
1736 * This will be modified for FP in build_ldio_fusion
1737 */
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301738 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
adam radford9c915a82010-12-21 13:34:31 -08001739
1740 if (megasas_is_ldio(scp))
1741 megasas_build_ldio_fusion(instance, scp, cmd);
1742 else
1743 megasas_build_dcdb_fusion(instance, scp, cmd);
1744
1745 /*
1746 * Construct SGL
1747 */
1748
1749 sge_count =
1750 megasas_make_sgl_fusion(instance, scp,
1751 (struct MPI25_IEEE_SGE_CHAIN64 *)
1752 &io_request->SGL, cmd);
1753
1754 if (sge_count > instance->max_num_sge) {
1755 printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1756 "max (0x%x) allowed\n", sge_count,
1757 instance->max_num_sge);
1758 return 1;
1759 }
1760
1761 io_request->RaidContext.numSGE = sge_count;
1762
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301763 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
adam radford9c915a82010-12-21 13:34:31 -08001764
1765 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301766 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
adam radford9c915a82010-12-21 13:34:31 -08001767 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301768 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
adam radford9c915a82010-12-21 13:34:31 -08001769
1770 io_request->SGLOffset0 =
1771 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1772
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301773 io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08001774 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1775
1776 cmd->scmd = scp;
1777 scp->SCp.ptr = (char *)cmd;
1778
1779 return 0;
1780}
1781
1782union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1783megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1784{
1785 u8 *p;
1786 struct fusion_context *fusion;
1787
1788 if (index >= instance->max_fw_cmds) {
1789 printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
adam radford229fe472014-03-10 02:51:56 -07001790 "descriptor for scsi%d\n", index,
1791 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08001792 return NULL;
1793 }
1794 fusion = instance->ctrl_context;
1795 p = fusion->req_frames_desc
1796 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1797
1798 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1799}
1800
1801/**
1802 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1803 * issuing non IOCTL cmd
1804 * @instance: Adapter soft state
1805 * @scmd: pointer to scsi cmd from OS
1806 */
1807static u32
1808megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1809 struct scsi_cmnd *scmd)
1810{
1811 struct megasas_cmd_fusion *cmd;
1812 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1813 u32 index;
1814 struct fusion_context *fusion;
1815
1816 fusion = instance->ctrl_context;
1817
1818 cmd = megasas_get_cmd_fusion(instance);
1819 if (!cmd)
1820 return SCSI_MLQUEUE_HOST_BUSY;
1821
1822 index = cmd->index;
1823
1824 req_desc = megasas_get_request_descriptor(instance, index-1);
1825 if (!req_desc)
1826 return 1;
1827
1828 req_desc->Words = 0;
1829 cmd->request_desc = req_desc;
adam radford9c915a82010-12-21 13:34:31 -08001830
1831 if (megasas_build_io_fusion(instance, scmd, cmd)) {
1832 megasas_return_cmd_fusion(instance, cmd);
1833 printk(KERN_ERR "megasas: Error building command.\n");
1834 cmd->request_desc = NULL;
1835 return 1;
1836 }
1837
1838 req_desc = cmd->request_desc;
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301839 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08001840
1841 if (cmd->io_request->ChainOffset != 0 &&
1842 cmd->io_request->ChainOffset != 0xF)
1843 printk(KERN_ERR "megasas: The chain offset value is not "
1844 "correct : %x\n", cmd->io_request->ChainOffset);
1845
1846 /*
1847 * Issue the command to the FW
1848 */
1849 atomic_inc(&instance->fw_outstanding);
1850
1851 instance->instancet->fire_cmd(instance,
1852 req_desc->u.low, req_desc->u.high,
1853 instance->reg_set);
1854
1855 return 0;
1856}
1857
1858/**
1859 * complete_cmd_fusion - Completes command
1860 * @instance: Adapter soft state
1861 * Completes all commands that is in reply descriptor queue
1862 */
1863int
adam radfordc8e858f2011-10-08 18:15:13 -07001864complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
adam radford9c915a82010-12-21 13:34:31 -08001865{
1866 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1867 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1868 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1869 struct fusion_context *fusion;
1870 struct megasas_cmd *cmd_mfi;
1871 struct megasas_cmd_fusion *cmd_fusion;
1872 u16 smid, num_completed;
1873 u8 reply_descript_type, arm;
1874 u32 status, extStatus, device_id;
1875 union desc_value d_val;
1876 struct LD_LOAD_BALANCE_INFO *lbinfo;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05301877 int threshold_reply_count = 0;
adam radford9c915a82010-12-21 13:34:31 -08001878
1879 fusion = instance->ctrl_context;
1880
1881 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1882 return IRQ_HANDLED;
1883
1884 desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -07001885 desc += ((MSIxIndex * fusion->reply_alloc_sz)/
1886 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
1887 fusion->last_reply_idx[MSIxIndex];
adam radford9c915a82010-12-21 13:34:31 -08001888
1889 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1890
1891 d_val.word = desc->Words;
1892
1893 reply_descript_type = reply_desc->ReplyFlags &
1894 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1895
1896 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1897 return IRQ_NONE;
1898
adam radford9c915a82010-12-21 13:34:31 -08001899 num_completed = 0;
1900
1901 while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05301902 smid = le16_to_cpu(reply_desc->SMID);
adam radford9c915a82010-12-21 13:34:31 -08001903
1904 cmd_fusion = fusion->cmd_list[smid - 1];
1905
1906 scsi_io_req =
1907 (struct MPI2_RAID_SCSI_IO_REQUEST *)
1908 cmd_fusion->io_request;
1909
1910 if (cmd_fusion->scmd)
1911 cmd_fusion->scmd->SCp.ptr = NULL;
1912
1913 status = scsi_io_req->RaidContext.status;
1914 extStatus = scsi_io_req->RaidContext.exStatus;
1915
1916 switch (scsi_io_req->Function) {
1917 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
1918 /* Update load balancing info */
1919 device_id = MEGASAS_DEV_INDEX(instance,
1920 cmd_fusion->scmd);
1921 lbinfo = &fusion->load_balance_info[device_id];
1922 if (cmd_fusion->scmd->SCp.Status &
1923 MEGASAS_LOAD_BALANCE_FLAG) {
1924 arm = lbinfo->raid1DevHandle[0] ==
1925 cmd_fusion->io_request->DevHandle ? 0 :
1926 1;
1927 atomic_dec(&lbinfo->scsi_pending_cmds[arm]);
1928 cmd_fusion->scmd->SCp.Status &=
1929 ~MEGASAS_LOAD_BALANCE_FLAG;
1930 }
1931 if (reply_descript_type ==
1932 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
1933 if (megasas_dbg_lvl == 5)
1934 printk(KERN_ERR "\nmegasas: FAST Path "
1935 "IO Success\n");
1936 }
1937 /* Fall thru and complete IO */
1938 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
1939 /* Map the FW Cmd Status */
1940 map_cmd_status(cmd_fusion, status, extStatus);
1941 scsi_dma_unmap(cmd_fusion->scmd);
1942 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
1943 scsi_io_req->RaidContext.status = 0;
1944 scsi_io_req->RaidContext.exStatus = 0;
1945 megasas_return_cmd_fusion(instance, cmd_fusion);
1946 atomic_dec(&instance->fw_outstanding);
1947
1948 break;
1949 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
1950 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1951 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1952 cmd_fusion->flags = 0;
1953 megasas_return_cmd_fusion(instance, cmd_fusion);
1954
1955 break;
1956 }
1957
adam radfordc8e858f2011-10-08 18:15:13 -07001958 fusion->last_reply_idx[MSIxIndex]++;
1959 if (fusion->last_reply_idx[MSIxIndex] >=
1960 fusion->reply_q_depth)
1961 fusion->last_reply_idx[MSIxIndex] = 0;
adam radford9c915a82010-12-21 13:34:31 -08001962
1963 desc->Words = ULLONG_MAX;
1964 num_completed++;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05301965 threshold_reply_count++;
adam radford9c915a82010-12-21 13:34:31 -08001966
1967 /* Get the next reply descriptor */
adam radfordc8e858f2011-10-08 18:15:13 -07001968 if (!fusion->last_reply_idx[MSIxIndex])
1969 desc = fusion->reply_frames_desc +
1970 ((MSIxIndex * fusion->reply_alloc_sz)/
1971 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
adam radford9c915a82010-12-21 13:34:31 -08001972 else
1973 desc++;
1974
1975 reply_desc =
1976 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1977
1978 d_val.word = desc->Words;
1979
1980 reply_descript_type = reply_desc->ReplyFlags &
1981 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1982
1983 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1984 break;
Sumit.Saxena@avagotech.comdb4fc862014-09-12 18:57:23 +05301985 /*
1986 * Write to reply post host index register after completing threshold
1987 * number of reply counts and still there are more replies in reply queue
1988 * pending to be completed
1989 */
1990 if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
1991 if ((instance->pdev->device ==
1992 PCI_DEVICE_ID_LSI_INVADER) ||
1993 (instance->pdev->device ==
1994 PCI_DEVICE_ID_LSI_FURY))
1995 writel(((MSIxIndex & 0x7) << 24) |
1996 fusion->last_reply_idx[MSIxIndex],
1997 instance->reply_post_host_index_addr[MSIxIndex/8]);
1998 else
1999 writel((MSIxIndex << 24) |
2000 fusion->last_reply_idx[MSIxIndex],
2001 instance->reply_post_host_index_addr[0]);
2002 threshold_reply_count = 0;
2003 }
adam radford9c915a82010-12-21 13:34:31 -08002004 }
2005
2006 if (!num_completed)
2007 return IRQ_NONE;
2008
2009 wmb();
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302010 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2011 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
2012 writel(((MSIxIndex & 0x7) << 24) |
2013 fusion->last_reply_idx[MSIxIndex],
2014 instance->reply_post_host_index_addr[MSIxIndex/8]);
2015 else
2016 writel((MSIxIndex << 24) |
2017 fusion->last_reply_idx[MSIxIndex],
2018 instance->reply_post_host_index_addr[0]);
adam radford53ef2bb2011-02-24 20:56:05 -08002019 megasas_check_and_restore_queue_depth(instance);
adam radford9c915a82010-12-21 13:34:31 -08002020 return IRQ_HANDLED;
2021}
2022
2023/**
2024 * megasas_complete_cmd_dpc_fusion - Completes command
2025 * @instance: Adapter soft state
2026 *
2027 * Tasklet to complete cmds
2028 */
2029void
2030megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
2031{
2032 struct megasas_instance *instance =
2033 (struct megasas_instance *)instance_addr;
2034 unsigned long flags;
adam radfordc8e858f2011-10-08 18:15:13 -07002035 u32 count, MSIxIndex;
2036
2037 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
adam radford9c915a82010-12-21 13:34:31 -08002038
2039 /* If we have already declared adapter dead, donot complete cmds */
2040 spin_lock_irqsave(&instance->hba_lock, flags);
2041 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2042 spin_unlock_irqrestore(&instance->hba_lock, flags);
2043 return;
2044 }
2045 spin_unlock_irqrestore(&instance->hba_lock, flags);
2046
adam radfordc8e858f2011-10-08 18:15:13 -07002047 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
2048 complete_cmd_fusion(instance, MSIxIndex);
adam radford9c915a82010-12-21 13:34:31 -08002049}
2050
2051/**
2052 * megasas_isr_fusion - isr entry point
2053 */
2054irqreturn_t megasas_isr_fusion(int irq, void *devp)
2055{
adam radfordc8e858f2011-10-08 18:15:13 -07002056 struct megasas_irq_context *irq_context = devp;
2057 struct megasas_instance *instance = irq_context->instance;
adam radford9c915a82010-12-21 13:34:31 -08002058 u32 mfiStatus, fw_state;
2059
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302060 if (instance->mask_interrupts)
2061 return IRQ_NONE;
2062
adam radfordc8e858f2011-10-08 18:15:13 -07002063 if (!instance->msix_vectors) {
adam radford9c915a82010-12-21 13:34:31 -08002064 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
2065 if (!mfiStatus)
2066 return IRQ_NONE;
2067 }
2068
2069 /* If we are resetting, bail */
adam radford6497b242011-10-08 18:14:50 -07002070 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
2071 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08002072 return IRQ_HANDLED;
adam radford6497b242011-10-08 18:14:50 -07002073 }
adam radford9c915a82010-12-21 13:34:31 -08002074
adam radfordc8e858f2011-10-08 18:15:13 -07002075 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
adam radford6497b242011-10-08 18:14:50 -07002076 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08002077 /* If we didn't complete any commands, check for FW fault */
2078 fw_state = instance->instancet->read_fw_status_reg(
2079 instance->reg_set) & MFI_STATE_MASK;
adam radford229fe472014-03-10 02:51:56 -07002080 if (fw_state == MFI_STATE_FAULT) {
2081 printk(KERN_WARNING "megaraid_sas: Iop2SysDoorbellInt"
2082 "for scsi%d\n", instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002083 schedule_work(&instance->work_init);
adam radford229fe472014-03-10 02:51:56 -07002084 }
adam radford9c915a82010-12-21 13:34:31 -08002085 }
2086
2087 return IRQ_HANDLED;
2088}
2089
2090/**
2091 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
2092 * @instance: Adapter soft state
2093 * mfi_cmd: megasas_cmd pointer
2094 *
2095 */
2096u8
2097build_mpt_mfi_pass_thru(struct megasas_instance *instance,
2098 struct megasas_cmd *mfi_cmd)
2099{
2100 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
2101 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
2102 struct megasas_cmd_fusion *cmd;
2103 struct fusion_context *fusion;
2104 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
2105
2106 cmd = megasas_get_cmd_fusion(instance);
2107 if (!cmd)
2108 return 1;
2109
2110 /* Save the smid. To be used for returning the cmd */
2111 mfi_cmd->context.smid = cmd->index;
2112
2113 cmd->sync_cmd_idx = mfi_cmd->index;
2114
2115 /*
2116 * For cmds where the flag is set, store the flag and check
2117 * on completion. For cmds with this flag, don't call
2118 * megasas_complete_cmd
2119 */
2120
2121 if (frame_hdr->flags & MFI_FRAME_DONT_POST_IN_REPLY_QUEUE)
2122 cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
2123
2124 fusion = instance->ctrl_context;
2125 io_req = cmd->io_request;
adam radford36807e62011-10-08 18:15:06 -07002126
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05302127 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2128 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07002129 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
2130 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
2131 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2132 sgl_ptr_end->Flags = 0;
2133 }
2134
adam radford9c915a82010-12-21 13:34:31 -08002135 mpi25_ieee_chain =
2136 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
2137
2138 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
2139 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
2140 SGL) / 4;
2141 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
2142
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302143 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
adam radford9c915a82010-12-21 13:34:31 -08002144
2145 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2146 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
2147
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302148 mpi25_ieee_chain->Length = cpu_to_le32(MEGASAS_MAX_SZ_CHAIN_FRAME);
adam radford9c915a82010-12-21 13:34:31 -08002149
2150 return 0;
2151}
2152
2153/**
2154 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
2155 * @instance: Adapter soft state
2156 * @cmd: mfi cmd to build
2157 *
2158 */
2159union MEGASAS_REQUEST_DESCRIPTOR_UNION *
2160build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
2161{
2162 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2163 u16 index;
2164
2165 if (build_mpt_mfi_pass_thru(instance, cmd)) {
2166 printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
2167 return NULL;
2168 }
2169
2170 index = cmd->context.smid;
2171
2172 req_desc = megasas_get_request_descriptor(instance, index - 1);
2173
2174 if (!req_desc)
2175 return NULL;
2176
2177 req_desc->Words = 0;
2178 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2179 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2180
Sumit.Saxena@lsi.com94cd65d2013-09-06 15:50:52 +05302181 req_desc->SCSIIO.SMID = cpu_to_le16(index);
adam radford9c915a82010-12-21 13:34:31 -08002182
2183 return req_desc;
2184}
2185
2186/**
2187 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
2188 * @instance: Adapter soft state
2189 * @cmd: mfi cmd pointer
2190 *
2191 */
2192void
2193megasas_issue_dcmd_fusion(struct megasas_instance *instance,
2194 struct megasas_cmd *cmd)
2195{
2196 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
adam radford9c915a82010-12-21 13:34:31 -08002197
2198 req_desc = build_mpt_cmd(instance, cmd);
2199 if (!req_desc) {
2200 printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
2201 return;
2202 }
adam radford9c915a82010-12-21 13:34:31 -08002203 instance->instancet->fire_cmd(instance, req_desc->u.low,
2204 req_desc->u.high, instance->reg_set);
2205}
2206
2207/**
2208 * megasas_release_fusion - Reverses the FW initialization
2209 * @intance: Adapter soft state
2210 */
2211void
2212megasas_release_fusion(struct megasas_instance *instance)
2213{
2214 megasas_free_cmds(instance);
2215 megasas_free_cmds_fusion(instance);
2216
2217 iounmap(instance->reg_set);
2218
2219 pci_release_selected_regions(instance->pdev, instance->bar);
2220}
2221
2222/**
2223 * megasas_read_fw_status_reg_fusion - returns the current FW status value
2224 * @regs: MFI register set
2225 */
2226static u32
2227megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
2228{
2229 return readl(&(regs)->outbound_scratch_pad);
2230}
2231
2232/**
2233 * megasas_adp_reset_fusion - For controller reset
2234 * @regs: MFI register set
2235 */
2236static int
2237megasas_adp_reset_fusion(struct megasas_instance *instance,
2238 struct megasas_register_set __iomem *regs)
2239{
2240 return 0;
2241}
2242
2243/**
2244 * megasas_check_reset_fusion - For controller reset check
2245 * @regs: MFI register set
2246 */
2247static int
2248megasas_check_reset_fusion(struct megasas_instance *instance,
2249 struct megasas_register_set __iomem *regs)
2250{
2251 return 0;
2252}
2253
2254/* This function waits for outstanding commands on fusion to complete */
adam radford229fe472014-03-10 02:51:56 -07002255int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
2256 int iotimeout, int *convert)
adam radford9c915a82010-12-21 13:34:31 -08002257{
adam radford229fe472014-03-10 02:51:56 -07002258 int i, outstanding, retval = 0, hb_seconds_missed = 0;
adam radfordc007b8b2012-07-17 18:20:24 -07002259 u32 fw_state;
adam radford9c915a82010-12-21 13:34:31 -08002260
adam radfordc007b8b2012-07-17 18:20:24 -07002261 for (i = 0; i < resetwaittime; i++) {
adam radford9c915a82010-12-21 13:34:31 -08002262 /* Check if firmware is in fault state */
2263 fw_state = instance->instancet->read_fw_status_reg(
2264 instance->reg_set) & MFI_STATE_MASK;
2265 if (fw_state == MFI_STATE_FAULT) {
2266 printk(KERN_WARNING "megasas: Found FW in FAULT state,"
adam radford229fe472014-03-10 02:51:56 -07002267 " will reset adapter scsi%d.\n",
2268 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002269 retval = 1;
2270 goto out;
2271 }
adam radford229fe472014-03-10 02:51:56 -07002272 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
2273 if (instance->requestorId && !iotimeout) {
2274 retval = 1;
2275 goto out;
2276 }
2277
2278 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
2279 if (instance->requestorId && iotimeout) {
2280 if (instance->hb_host_mem->HB.fwCounter !=
2281 instance->hb_host_mem->HB.driverCounter) {
2282 instance->hb_host_mem->HB.driverCounter =
2283 instance->hb_host_mem->HB.fwCounter;
2284 hb_seconds_missed = 0;
2285 } else {
2286 hb_seconds_missed++;
2287 if (hb_seconds_missed ==
2288 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
2289 printk(KERN_WARNING "megasas: SR-IOV:"
2290 " Heartbeat never completed "
2291 " while polling during I/O "
2292 " timeout handling for "
2293 "scsi%d.\n",
2294 instance->host->host_no);
2295 *convert = 1;
2296 retval = 1;
2297 goto out;
2298 }
2299 }
2300 }
adam radford9c915a82010-12-21 13:34:31 -08002301
2302 outstanding = atomic_read(&instance->fw_outstanding);
2303 if (!outstanding)
2304 goto out;
2305
2306 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2307 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
adam radford229fe472014-03-10 02:51:56 -07002308 "commands to complete for scsi%d\n", i,
2309 outstanding, instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002310 megasas_complete_cmd_dpc_fusion(
2311 (unsigned long)instance);
2312 }
2313 msleep(1000);
2314 }
2315
2316 if (atomic_read(&instance->fw_outstanding)) {
2317 printk("megaraid_sas: pending commands remain after waiting, "
adam radford229fe472014-03-10 02:51:56 -07002318 "will reset adapter scsi%d.\n",
2319 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002320 retval = 1;
2321 }
2322out:
2323 return retval;
2324}
2325
2326void megasas_reset_reply_desc(struct megasas_instance *instance)
2327{
adam radfordc8e858f2011-10-08 18:15:13 -07002328 int i, count;
adam radford9c915a82010-12-21 13:34:31 -08002329 struct fusion_context *fusion;
2330 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2331
2332 fusion = instance->ctrl_context;
adam radfordc8e858f2011-10-08 18:15:13 -07002333 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2334 for (i = 0 ; i < count ; i++)
2335 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -08002336 reply_desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -07002337 for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
adam radford9c915a82010-12-21 13:34:31 -08002338 reply_desc->Words = ULLONG_MAX;
2339}
2340
adam radford229fe472014-03-10 02:51:56 -07002341/* Check for a second path that is currently UP */
2342int megasas_check_mpio_paths(struct megasas_instance *instance,
2343 struct scsi_cmnd *scmd)
adam radford9c915a82010-12-21 13:34:31 -08002344{
adam radford229fe472014-03-10 02:51:56 -07002345 int i, j, retval = (DID_RESET << 16);
2346
2347 if (instance->mpio && instance->requestorId) {
2348 for (i = 0 ; i < MAX_MGMT_ADAPTERS ; i++)
2349 for (j = 0 ; j < MAX_LOGICAL_DRIVES; j++)
2350 if (megasas_mgmt_info.instance[i] &&
2351 (megasas_mgmt_info.instance[i] != instance) &&
2352 megasas_mgmt_info.instance[i]->mpio &&
2353 megasas_mgmt_info.instance[i]->requestorId
2354 &&
2355 (megasas_mgmt_info.instance[i]->ld_ids[j]
2356 == scmd->device->id)) {
2357 retval = (DID_NO_CONNECT << 16);
2358 goto out;
2359 }
2360 }
2361out:
2362 return retval;
2363}
2364
2365/* Core fusion reset function */
2366int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout)
2367{
2368 int retval = SUCCESS, i, j, retry = 0, convert = 0;
adam radford9c915a82010-12-21 13:34:31 -08002369 struct megasas_instance *instance;
2370 struct megasas_cmd_fusion *cmd_fusion;
2371 struct fusion_context *fusion;
2372 struct megasas_cmd *cmd_mfi;
2373 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
adam radford7e70e732011-05-11 18:34:08 -07002374 u32 host_diag, abs_state, status_reg, reset_adapter;
adam radford9c915a82010-12-21 13:34:31 -08002375
2376 instance = (struct megasas_instance *)shost->hostdata;
2377 fusion = instance->ctrl_context;
2378
adam radford229fe472014-03-10 02:51:56 -07002379 mutex_lock(&instance->reset_mutex);
2380
adam radford9c915a82010-12-21 13:34:31 -08002381 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2382 printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
adam radford229fe472014-03-10 02:51:56 -07002383 "returning FAILED for scsi%d.\n",
2384 instance->host->host_no);
Adam Radforda2fbcbc2014-07-09 15:17:54 -07002385 mutex_unlock(&instance->reset_mutex);
adam radfordd4a759a2011-10-08 18:14:39 -07002386 return FAILED;
adam radford9c915a82010-12-21 13:34:31 -08002387 }
2388
adam radford229fe472014-03-10 02:51:56 -07002389 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
2390 del_timer_sync(&instance->sriov_heartbeat_timer);
adam radford7e70e732011-05-11 18:34:08 -07002391 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
adam radford229fe472014-03-10 02:51:56 -07002392 instance->adprecovery = MEGASAS_ADPRESET_SM_POLLING;
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302393 instance->instancet->disable_intr(instance);
adam radford7e70e732011-05-11 18:34:08 -07002394 msleep(1000);
2395
adam radford9c915a82010-12-21 13:34:31 -08002396 /* First try waiting for commands to complete */
adam radford229fe472014-03-10 02:51:56 -07002397 if (megasas_wait_for_outstanding_fusion(instance, iotimeout,
2398 &convert)) {
2399 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
adam radford9c915a82010-12-21 13:34:31 -08002400 printk(KERN_WARNING "megaraid_sas: resetting fusion "
adam radford229fe472014-03-10 02:51:56 -07002401 "adapter scsi%d.\n", instance->host->host_no);
2402 if (convert)
2403 iotimeout = 0;
2404
adam radford9c915a82010-12-21 13:34:31 -08002405 /* Now return commands back to the OS */
2406 for (i = 0 ; i < instance->max_fw_cmds; i++) {
2407 cmd_fusion = fusion->cmd_list[i];
2408 if (cmd_fusion->scmd) {
2409 scsi_dma_unmap(cmd_fusion->scmd);
adam radford229fe472014-03-10 02:51:56 -07002410 cmd_fusion->scmd->result =
2411 megasas_check_mpio_paths(instance,
2412 cmd_fusion->scmd);
adam radford9c915a82010-12-21 13:34:31 -08002413 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2414 megasas_return_cmd_fusion(instance, cmd_fusion);
2415 atomic_dec(&instance->fw_outstanding);
2416 }
2417 }
2418
adam radford7e70e732011-05-11 18:34:08 -07002419 status_reg = instance->instancet->read_fw_status_reg(
2420 instance->reg_set);
2421 abs_state = status_reg & MFI_STATE_MASK;
2422 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2423 if (instance->disableOnlineCtrlReset ||
2424 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
adam radford9c915a82010-12-21 13:34:31 -08002425 /* Reset not supported, kill adapter */
2426 printk(KERN_WARNING "megaraid_sas: Reset not supported"
adam radford229fe472014-03-10 02:51:56 -07002427 ", killing adapter scsi%d.\n",
2428 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002429 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07002430 instance->skip_heartbeat_timer_del = 1;
adam radford9c915a82010-12-21 13:34:31 -08002431 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2432 retval = FAILED;
2433 goto out;
2434 }
2435
adam radford229fe472014-03-10 02:51:56 -07002436 /* Let SR-IOV VF & PF sync up if there was a HB failure */
2437 if (instance->requestorId && !iotimeout) {
2438 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
2439 /* Look for a late HB update after VF settle time */
2440 if (abs_state == MFI_STATE_OPERATIONAL &&
2441 (instance->hb_host_mem->HB.fwCounter !=
2442 instance->hb_host_mem->HB.driverCounter)) {
2443 instance->hb_host_mem->HB.driverCounter =
2444 instance->hb_host_mem->HB.fwCounter;
2445 printk(KERN_WARNING "megasas: SR-IOV:"
2446 "Late FW heartbeat update for "
2447 "scsi%d.\n",
2448 instance->host->host_no);
2449 } else {
2450 /* In VF mode, first poll for FW ready */
2451 for (i = 0;
2452 i < (MEGASAS_RESET_WAIT_TIME * 1000);
2453 i += 20) {
2454 status_reg =
2455 instance->instancet->
2456 read_fw_status_reg(
2457 instance->reg_set);
2458 abs_state = status_reg &
2459 MFI_STATE_MASK;
2460 if (abs_state == MFI_STATE_READY) {
2461 printk(KERN_WARNING "megasas"
2462 ": SR-IOV: FW was found"
2463 "to be in ready state "
2464 "for scsi%d.\n",
2465 instance->host->host_no);
2466 break;
2467 }
2468 msleep(20);
2469 }
2470 if (abs_state != MFI_STATE_READY) {
2471 printk(KERN_WARNING "megasas: SR-IOV: "
2472 "FW not in ready state after %d"
2473 " seconds for scsi%d, status_reg = "
2474 "0x%x.\n",
2475 MEGASAS_RESET_WAIT_TIME,
2476 instance->host->host_no,
2477 status_reg);
2478 megaraid_sas_kill_hba(instance);
2479 instance->skip_heartbeat_timer_del = 1;
2480 instance->adprecovery =
2481 MEGASAS_HW_CRITICAL_ERROR;
2482 retval = FAILED;
2483 goto out;
2484 }
2485 }
2486 }
2487
adam radford9c915a82010-12-21 13:34:31 -08002488 /* Now try to reset the chip */
2489 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2490 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2491 &instance->reg_set->fusion_seq_offset);
2492 writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2493 &instance->reg_set->fusion_seq_offset);
2494 writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2495 &instance->reg_set->fusion_seq_offset);
2496 writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2497 &instance->reg_set->fusion_seq_offset);
2498 writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2499 &instance->reg_set->fusion_seq_offset);
2500 writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2501 &instance->reg_set->fusion_seq_offset);
2502 writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2503 &instance->reg_set->fusion_seq_offset);
2504
2505 /* Check that the diag write enable (DRWE) bit is on */
2506 host_diag = readl(&instance->reg_set->fusion_host_diag);
adam radford7e70e732011-05-11 18:34:08 -07002507 retry = 0;
adam radford9c915a82010-12-21 13:34:31 -08002508 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2509 msleep(100);
2510 host_diag =
2511 readl(&instance->reg_set->fusion_host_diag);
2512 if (retry++ == 100) {
2513 printk(KERN_WARNING "megaraid_sas: "
adam radford229fe472014-03-10 02:51:56 -07002514 "Host diag unlock failed! "
2515 "for scsi%d\n",
2516 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002517 break;
2518 }
2519 }
2520 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2521 continue;
2522
2523 /* Send chip reset command */
2524 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2525 &instance->reg_set->fusion_host_diag);
2526 msleep(3000);
2527
2528 /* Make sure reset adapter bit is cleared */
2529 host_diag = readl(&instance->reg_set->fusion_host_diag);
2530 retry = 0;
2531 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2532 msleep(100);
2533 host_diag =
2534 readl(&instance->reg_set->fusion_host_diag);
2535 if (retry++ == 1000) {
2536 printk(KERN_WARNING "megaraid_sas: "
2537 "Diag reset adapter never "
adam radford229fe472014-03-10 02:51:56 -07002538 "cleared for scsi%d!\n",
2539 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002540 break;
2541 }
2542 }
2543 if (host_diag & HOST_DIAG_RESET_ADAPTER)
2544 continue;
2545
2546 abs_state =
2547 instance->instancet->read_fw_status_reg(
adam radford7e70e732011-05-11 18:34:08 -07002548 instance->reg_set) & MFI_STATE_MASK;
adam radford9c915a82010-12-21 13:34:31 -08002549 retry = 0;
2550
2551 while ((abs_state <= MFI_STATE_FW_INIT) &&
2552 (retry++ < 1000)) {
2553 msleep(100);
2554 abs_state =
2555 instance->instancet->read_fw_status_reg(
adam radford7e70e732011-05-11 18:34:08 -07002556 instance->reg_set) & MFI_STATE_MASK;
adam radford9c915a82010-12-21 13:34:31 -08002557 }
2558 if (abs_state <= MFI_STATE_FW_INIT) {
2559 printk(KERN_WARNING "megaraid_sas: firmware "
2560 "state < MFI_STATE_FW_INIT, state = "
adam radford229fe472014-03-10 02:51:56 -07002561 "0x%x for scsi%d\n", abs_state,
2562 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002563 continue;
2564 }
2565
2566 /* Wait for FW to become ready */
adam radford058a8fa2011-10-08 18:14:27 -07002567 if (megasas_transition_to_ready(instance, 1)) {
adam radford9c915a82010-12-21 13:34:31 -08002568 printk(KERN_WARNING "megaraid_sas: Failed to "
adam radford229fe472014-03-10 02:51:56 -07002569 "transition controller to ready "
2570 "for scsi%d.\n",
2571 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002572 continue;
2573 }
2574
2575 megasas_reset_reply_desc(instance);
2576 if (megasas_ioc_init_fusion(instance)) {
2577 printk(KERN_WARNING "megaraid_sas: "
adam radford229fe472014-03-10 02:51:56 -07002578 "megasas_ioc_init_fusion() failed!"
2579 " for scsi%d\n",
2580 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002581 continue;
2582 }
2583
adam radford9c915a82010-12-21 13:34:31 -08002584 /* Re-fire management commands */
2585 for (j = 0 ; j < instance->max_fw_cmds; j++) {
2586 cmd_fusion = fusion->cmd_list[j];
2587 if (cmd_fusion->sync_cmd_idx !=
2588 (u32)ULONG_MAX) {
2589 cmd_mfi =
2590 instance->
2591 cmd_list[cmd_fusion->sync_cmd_idx];
2592 if (cmd_mfi->frame->dcmd.opcode ==
Sumit.Saxena@lsi.combe263742014-02-12 23:37:46 +05302593 cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO)) {
adam radford9c915a82010-12-21 13:34:31 -08002594 megasas_return_cmd(instance,
2595 cmd_mfi);
2596 megasas_return_cmd_fusion(
2597 instance, cmd_fusion);
2598 } else {
2599 req_desc =
2600 megasas_get_request_descriptor(
2601 instance,
2602 cmd_mfi->context.smid
2603 -1);
adam radford3d0c24c2014-03-10 02:51:45 -07002604 if (!req_desc) {
adam radford9c915a82010-12-21 13:34:31 -08002605 printk(KERN_WARNING
2606 "req_desc NULL"
adam radford229fe472014-03-10 02:51:56 -07002607 " for scsi%d\n",
2608 instance->host->host_no);
adam radford3d0c24c2014-03-10 02:51:45 -07002609 /* Return leaked MPT
2610 frame */
2611 megasas_return_cmd_fusion(instance, cmd_fusion);
2612 } else {
adam radford9c915a82010-12-21 13:34:31 -08002613 instance->instancet->
2614 fire_cmd(instance,
2615 req_desc->
2616 u.low,
2617 req_desc->
2618 u.high,
2619 instance->
2620 reg_set);
2621 }
2622 }
2623 }
2624 }
2625
adam radford229fe472014-03-10 02:51:56 -07002626 clear_bit(MEGASAS_FUSION_IN_RESET,
2627 &instance->reset_flags);
2628 instance->instancet->enable_intr(instance);
2629 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2630
adam radford9c915a82010-12-21 13:34:31 -08002631 /* Reset load balance info */
2632 memset(fusion->load_balance_info, 0,
2633 sizeof(struct LD_LOAD_BALANCE_INFO)
2634 *MAX_LOGICAL_DRIVES);
2635
2636 if (!megasas_get_map_info(instance))
2637 megasas_sync_map_info(instance);
2638
adam radford229fe472014-03-10 02:51:56 -07002639 /* Restart SR-IOV heartbeat */
2640 if (instance->requestorId) {
2641 if (!megasas_sriov_start_heartbeat(instance, 0))
2642 megasas_start_timer(instance,
2643 &instance->sriov_heartbeat_timer,
2644 megasas_sriov_heartbeat_handler,
2645 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2646 else
2647 instance->skip_heartbeat_timer_del = 1;
2648 }
2649
adam radford9c915a82010-12-21 13:34:31 -08002650 /* Adapter reset completed successfully */
2651 printk(KERN_WARNING "megaraid_sas: Reset "
adam radford229fe472014-03-10 02:51:56 -07002652 "successful for scsi%d.\n",
2653 instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002654 retval = SUCCESS;
2655 goto out;
2656 }
2657 /* Reset failed, kill the adapter */
2658 printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
adam radford229fe472014-03-10 02:51:56 -07002659 "adapter scsi%d.\n", instance->host->host_no);
adam radford9c915a82010-12-21 13:34:31 -08002660 megaraid_sas_kill_hba(instance);
adam radford229fe472014-03-10 02:51:56 -07002661 instance->skip_heartbeat_timer_del = 1;
Sumit.Saxena@lsi.comcfbe7552014-02-12 23:36:15 +05302662 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
adam radford9c915a82010-12-21 13:34:31 -08002663 retval = FAILED;
2664 } else {
adam radford229fe472014-03-10 02:51:56 -07002665 /* For VF: Restart HB timer if we didn't OCR */
2666 if (instance->requestorId) {
2667 megasas_start_timer(instance,
2668 &instance->sriov_heartbeat_timer,
2669 megasas_sriov_heartbeat_handler,
2670 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2671 }
adam radford3f0e58bc2011-10-08 18:14:59 -07002672 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
Sumit.Saxena@lsi.comd46a3ad2013-05-22 12:34:14 +05302673 instance->instancet->enable_intr(instance);
adam radford9c915a82010-12-21 13:34:31 -08002674 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2675 }
2676out:
2677 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2678 mutex_unlock(&instance->reset_mutex);
2679 return retval;
2680}
2681
2682/* Fusion OCR work queue */
2683void megasas_fusion_ocr_wq(struct work_struct *work)
2684{
2685 struct megasas_instance *instance =
2686 container_of(work, struct megasas_instance, work_init);
2687
adam radford229fe472014-03-10 02:51:56 -07002688 megasas_reset_fusion(instance->host, 0);
adam radford9c915a82010-12-21 13:34:31 -08002689}
2690
2691struct megasas_instance_template megasas_instance_template_fusion = {
2692 .fire_cmd = megasas_fire_cmd_fusion,
2693 .enable_intr = megasas_enable_intr_fusion,
2694 .disable_intr = megasas_disable_intr_fusion,
2695 .clear_intr = megasas_clear_intr_fusion,
2696 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2697 .adp_reset = megasas_adp_reset_fusion,
2698 .check_reset = megasas_check_reset_fusion,
2699 .service_isr = megasas_isr_fusion,
2700 .tasklet = megasas_complete_cmd_dpc_fusion,
2701 .init_adapter = megasas_init_adapter_fusion,
2702 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2703 .issue_dcmd = megasas_issue_dcmd_fusion,
2704};