blob: 73aa68e60a12f4b48530f9457c12bdd69943d8ee [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
65wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd);
66
67void
68megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
69int megasas_alloc_cmds(struct megasas_instance *instance);
70int
71megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
72int
73megasas_issue_polled(struct megasas_instance *instance,
74 struct megasas_cmd *cmd);
75
76u8
adam radford36807e62011-10-08 18:15:06 -070077MR_BuildRaidContext(struct megasas_instance *instance,
78 struct IO_REQUEST_INFO *io_info,
adam radford9c915a82010-12-21 13:34:31 -080079 struct RAID_CONTEXT *pRAID_Context,
80 struct MR_FW_RAID_MAP_ALL *map);
81u16 MR_TargetIdToLdGet(u32 ldTgtId, struct MR_FW_RAID_MAP_ALL *map);
82struct MR_LD_RAID *MR_LdRaidGet(u32 ld, struct MR_FW_RAID_MAP_ALL *map);
83
84u16 MR_GetLDTgtId(u32 ld, struct MR_FW_RAID_MAP_ALL *map);
adam radford53ef2bb2011-02-24 20:56:05 -080085
86void
87megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
88
adam radford9c915a82010-12-21 13:34:31 -080089u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
90 struct LD_LOAD_BALANCE_INFO *lbInfo);
91u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo,
92 struct IO_REQUEST_INFO *in_info);
adam radford058a8fa2011-10-08 18:14:27 -070093int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
adam radford9c915a82010-12-21 13:34:31 -080094void megaraid_sas_kill_hba(struct megasas_instance *instance);
95
96extern u32 megasas_dbg_lvl;
adam radfordc007b8b2012-07-17 18:20:24 -070097extern int resetwaittime;
adam radford9c915a82010-12-21 13:34:31 -080098
99/**
100 * megasas_enable_intr_fusion - Enables interrupts
101 * @regs: MFI register set
102 */
103void
104megasas_enable_intr_fusion(struct megasas_register_set __iomem *regs)
105{
adam radford6497b242011-10-08 18:14:50 -0700106 /* For Thunderbolt/Invader also clear intr on enable */
107 writel(~0, &regs->outbound_intr_status);
108 readl(&regs->outbound_intr_status);
109
adam radford9c915a82010-12-21 13:34:31 -0800110 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
111
112 /* Dummy readl to force pci flush */
113 readl(&regs->outbound_intr_mask);
114}
115
116/**
117 * megasas_disable_intr_fusion - Disables interrupt
118 * @regs: MFI register set
119 */
120void
121megasas_disable_intr_fusion(struct megasas_register_set __iomem *regs)
122{
123 u32 mask = 0xFFFFFFFF;
124 u32 status;
125
126 writel(mask, &regs->outbound_intr_mask);
127 /* Dummy readl to force pci flush */
128 status = readl(&regs->outbound_intr_mask);
129}
130
131int
132megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
133{
134 u32 status;
135 /*
136 * Check if it is our interrupt
137 */
138 status = readl(&regs->outbound_intr_status);
139
140 if (status & 1) {
141 writel(status, &regs->outbound_intr_status);
142 readl(&regs->outbound_intr_status);
143 return 1;
144 }
145 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
146 return 0;
147
adam radford9c915a82010-12-21 13:34:31 -0800148 return 1;
149}
150
151/**
152 * megasas_get_cmd_fusion - Get a command from the free pool
153 * @instance: Adapter soft state
154 *
155 * Returns a free command from the pool
156 */
157struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
158 *instance)
159{
160 unsigned long flags;
161 struct fusion_context *fusion =
162 (struct fusion_context *)instance->ctrl_context;
163 struct megasas_cmd_fusion *cmd = NULL;
164
165 spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
166
167 if (!list_empty(&fusion->cmd_pool)) {
168 cmd = list_entry((&fusion->cmd_pool)->next,
169 struct megasas_cmd_fusion, list);
170 list_del_init(&cmd->list);
171 } else {
172 printk(KERN_ERR "megasas: Command pool (fusion) empty!\n");
173 }
174
175 spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
176 return cmd;
177}
178
179/**
180 * megasas_return_cmd_fusion - Return a cmd to free command pool
181 * @instance: Adapter soft state
182 * @cmd: Command packet to be returned to free command pool
183 */
184static inline void
185megasas_return_cmd_fusion(struct megasas_instance *instance,
186 struct megasas_cmd_fusion *cmd)
187{
188 unsigned long flags;
189 struct fusion_context *fusion =
190 (struct fusion_context *)instance->ctrl_context;
191
192 spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
193
194 cmd->scmd = NULL;
195 cmd->sync_cmd_idx = (u32)ULONG_MAX;
196 list_add_tail(&cmd->list, &fusion->cmd_pool);
197
198 spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
199}
200
201/**
202 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
203 * @instance: Adapter soft state
204 */
205static void megasas_teardown_frame_pool_fusion(
206 struct megasas_instance *instance)
207{
208 int i;
209 struct fusion_context *fusion = instance->ctrl_context;
210
211 u16 max_cmd = instance->max_fw_cmds;
212
213 struct megasas_cmd_fusion *cmd;
214
215 if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
216 printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
217 "sense pool : %p\n", fusion->sg_dma_pool,
218 fusion->sense_dma_pool);
219 return;
220 }
221
222 /*
223 * Return all frames to pool
224 */
225 for (i = 0; i < max_cmd; i++) {
226
227 cmd = fusion->cmd_list[i];
228
229 if (cmd->sg_frame)
230 pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
231 cmd->sg_frame_phys_addr);
232
233 if (cmd->sense)
234 pci_pool_free(fusion->sense_dma_pool, cmd->sense,
235 cmd->sense_phys_addr);
236 }
237
238 /*
239 * Now destroy the pool itself
240 */
241 pci_pool_destroy(fusion->sg_dma_pool);
242 pci_pool_destroy(fusion->sense_dma_pool);
243
244 fusion->sg_dma_pool = NULL;
245 fusion->sense_dma_pool = NULL;
246}
247
248/**
249 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
250 * @instance: Adapter soft state
251 */
252void
253megasas_free_cmds_fusion(struct megasas_instance *instance)
254{
255 int i;
256 struct fusion_context *fusion = instance->ctrl_context;
257
258 u32 max_cmds, req_sz, reply_sz, io_frames_sz;
259
260
261 req_sz = fusion->request_alloc_sz;
262 reply_sz = fusion->reply_alloc_sz;
263 io_frames_sz = fusion->io_frames_alloc_sz;
264
265 max_cmds = instance->max_fw_cmds;
266
267 /* Free descriptors and request Frames memory */
268 if (fusion->req_frames_desc)
269 dma_free_coherent(&instance->pdev->dev, req_sz,
270 fusion->req_frames_desc,
271 fusion->req_frames_desc_phys);
272
273 if (fusion->reply_frames_desc) {
274 pci_pool_free(fusion->reply_frames_desc_pool,
275 fusion->reply_frames_desc,
276 fusion->reply_frames_desc_phys);
277 pci_pool_destroy(fusion->reply_frames_desc_pool);
278 }
279
280 if (fusion->io_request_frames) {
281 pci_pool_free(fusion->io_request_frames_pool,
282 fusion->io_request_frames,
283 fusion->io_request_frames_phys);
284 pci_pool_destroy(fusion->io_request_frames_pool);
285 }
286
287 /* Free the Fusion frame pool */
288 megasas_teardown_frame_pool_fusion(instance);
289
290 /* Free all the commands in the cmd_list */
291 for (i = 0; i < max_cmds; i++)
292 kfree(fusion->cmd_list[i]);
293
294 /* Free the cmd_list buffer itself */
295 kfree(fusion->cmd_list);
296 fusion->cmd_list = NULL;
297
298 INIT_LIST_HEAD(&fusion->cmd_pool);
299}
300
301/**
302 * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
303 * @instance: Adapter soft state
304 *
305 */
306static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
307{
308 int i;
309 u32 max_cmd;
310 struct fusion_context *fusion;
311 struct megasas_cmd_fusion *cmd;
312 u32 total_sz_chain_frame;
313
314 fusion = instance->ctrl_context;
315 max_cmd = instance->max_fw_cmds;
316
317 total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
318
319 /*
320 * Use DMA pool facility provided by PCI layer
321 */
322
323 fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
324 instance->pdev,
325 total_sz_chain_frame, 4,
326 0);
327 if (!fusion->sg_dma_pool) {
328 printk(KERN_DEBUG "megasas: failed to setup request pool "
329 "fusion\n");
330 return -ENOMEM;
331 }
332 fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
333 instance->pdev,
334 SCSI_SENSE_BUFFERSIZE, 64, 0);
335
336 if (!fusion->sense_dma_pool) {
337 printk(KERN_DEBUG "megasas: failed to setup sense pool "
338 "fusion\n");
339 pci_pool_destroy(fusion->sg_dma_pool);
340 fusion->sg_dma_pool = NULL;
341 return -ENOMEM;
342 }
343
344 /*
345 * Allocate and attach a frame to each of the commands in cmd_list
346 */
347 for (i = 0; i < max_cmd; i++) {
348
349 cmd = fusion->cmd_list[i];
350
351 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
352 GFP_KERNEL,
353 &cmd->sg_frame_phys_addr);
354
355 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
356 GFP_KERNEL, &cmd->sense_phys_addr);
357 /*
358 * megasas_teardown_frame_pool_fusion() takes care of freeing
359 * whatever has been allocated
360 */
361 if (!cmd->sg_frame || !cmd->sense) {
362 printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
363 megasas_teardown_frame_pool_fusion(instance);
364 return -ENOMEM;
365 }
366 }
367 return 0;
368}
369
370/**
371 * megasas_alloc_cmds_fusion - Allocates the command packets
372 * @instance: Adapter soft state
373 *
374 *
375 * Each frame has a 32-bit field called context. This context is used to get
376 * back the megasas_cmd_fusion from the frame when a frame gets completed
377 * In this driver, the 32 bit values are the indices into an array cmd_list.
378 * This array is used only to look up the megasas_cmd_fusion given the context.
379 * The free commands themselves are maintained in a linked list called cmd_pool.
380 *
381 * cmds are formed in the io_request and sg_frame members of the
382 * megasas_cmd_fusion. The context field is used to get a request descriptor
383 * and is used as SMID of the cmd.
384 * SMID value range is from 1 to max_fw_cmds.
385 */
386int
387megasas_alloc_cmds_fusion(struct megasas_instance *instance)
388{
adam radfordc8e858f2011-10-08 18:15:13 -0700389 int i, j, count;
adam radford9c915a82010-12-21 13:34:31 -0800390 u32 max_cmd, io_frames_sz;
391 struct fusion_context *fusion;
392 struct megasas_cmd_fusion *cmd;
393 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
394 u32 offset;
395 dma_addr_t io_req_base_phys;
396 u8 *io_req_base;
397
398 fusion = instance->ctrl_context;
399
400 max_cmd = instance->max_fw_cmds;
401
402 fusion->req_frames_desc =
403 dma_alloc_coherent(&instance->pdev->dev,
404 fusion->request_alloc_sz,
405 &fusion->req_frames_desc_phys, GFP_KERNEL);
406
407 if (!fusion->req_frames_desc) {
408 printk(KERN_ERR "megasas; Could not allocate memory for "
409 "request_frames\n");
410 goto fail_req_desc;
411 }
412
adam radfordc8e858f2011-10-08 18:15:13 -0700413 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
adam radford9c915a82010-12-21 13:34:31 -0800414 fusion->reply_frames_desc_pool =
415 pci_pool_create("reply_frames pool", instance->pdev,
adam radfordc8e858f2011-10-08 18:15:13 -0700416 fusion->reply_alloc_sz * count, 16, 0);
adam radford9c915a82010-12-21 13:34:31 -0800417
418 if (!fusion->reply_frames_desc_pool) {
419 printk(KERN_ERR "megasas; Could not allocate memory for "
420 "reply_frame pool\n");
421 goto fail_reply_desc;
422 }
423
424 fusion->reply_frames_desc =
425 pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
426 &fusion->reply_frames_desc_phys);
427 if (!fusion->reply_frames_desc) {
428 printk(KERN_ERR "megasas; Could not allocate memory for "
429 "reply_frame pool\n");
430 pci_pool_destroy(fusion->reply_frames_desc_pool);
431 goto fail_reply_desc;
432 }
433
434 reply_desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -0700435 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
adam radford9c915a82010-12-21 13:34:31 -0800436 reply_desc->Words = ULLONG_MAX;
437
438 io_frames_sz = fusion->io_frames_alloc_sz;
439
440 fusion->io_request_frames_pool =
441 pci_pool_create("io_request_frames pool", instance->pdev,
442 fusion->io_frames_alloc_sz, 16, 0);
443
444 if (!fusion->io_request_frames_pool) {
445 printk(KERN_ERR "megasas: Could not allocate memory for "
446 "io_request_frame pool\n");
447 goto fail_io_frames;
448 }
449
450 fusion->io_request_frames =
451 pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
452 &fusion->io_request_frames_phys);
453 if (!fusion->io_request_frames) {
454 printk(KERN_ERR "megasas: Could not allocate memory for "
455 "io_request_frames frames\n");
456 pci_pool_destroy(fusion->io_request_frames_pool);
457 goto fail_io_frames;
458 }
459
460 /*
461 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
462 * Allocate the dynamic array first and then allocate individual
463 * commands.
464 */
Fengguang Wu7731e6b2012-08-24 23:27:35 +0800465 fusion->cmd_list = kzalloc(sizeof(struct megasas_cmd_fusion *)
466 * max_cmd, GFP_KERNEL);
adam radford9c915a82010-12-21 13:34:31 -0800467
468 if (!fusion->cmd_list) {
469 printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
470 "memory for cmd_list_fusion\n");
471 goto fail_cmd_list;
472 }
473
adam radford9c915a82010-12-21 13:34:31 -0800474 max_cmd = instance->max_fw_cmds;
475 for (i = 0; i < max_cmd; i++) {
476 fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
477 GFP_KERNEL);
478 if (!fusion->cmd_list[i]) {
479 printk(KERN_ERR "Could not alloc cmd list fusion\n");
480
481 for (j = 0; j < i; j++)
482 kfree(fusion->cmd_list[j]);
483
484 kfree(fusion->cmd_list);
485 fusion->cmd_list = NULL;
486 goto fail_cmd_list;
487 }
488 }
489
490 /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
491 io_req_base = fusion->io_request_frames +
492 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
493 io_req_base_phys = fusion->io_request_frames_phys +
494 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
495
496 /*
497 * Add all the commands to command pool (fusion->cmd_pool)
498 */
499
500 /* SMID 0 is reserved. Set SMID/index from 1 */
501 for (i = 0; i < max_cmd; i++) {
502 cmd = fusion->cmd_list[i];
503 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
504 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
505 cmd->index = i + 1;
506 cmd->scmd = NULL;
507 cmd->sync_cmd_idx = (u32)ULONG_MAX; /* Set to Invalid */
508 cmd->instance = instance;
509 cmd->io_request =
510 (struct MPI2_RAID_SCSI_IO_REQUEST *)
511 (io_req_base + offset);
512 memset(cmd->io_request, 0,
513 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
514 cmd->io_request_phys_addr = io_req_base_phys + offset;
515
516 list_add_tail(&cmd->list, &fusion->cmd_pool);
517 }
518
519 /*
520 * Create a frame pool and assign one frame to each cmd
521 */
522 if (megasas_create_frame_pool_fusion(instance)) {
523 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
524 megasas_free_cmds_fusion(instance);
525 goto fail_req_desc;
526 }
527
528 return 0;
529
530fail_cmd_list:
531 pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
532 fusion->io_request_frames_phys);
533 pci_pool_destroy(fusion->io_request_frames_pool);
534fail_io_frames:
535 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
536 fusion->reply_frames_desc,
537 fusion->reply_frames_desc_phys);
538 pci_pool_free(fusion->reply_frames_desc_pool,
539 fusion->reply_frames_desc,
540 fusion->reply_frames_desc_phys);
541 pci_pool_destroy(fusion->reply_frames_desc_pool);
542
543fail_reply_desc:
544 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
545 fusion->req_frames_desc,
546 fusion->req_frames_desc_phys);
547fail_req_desc:
548 return -ENOMEM;
549}
550
551/**
552 * wait_and_poll - Issues a polling command
553 * @instance: Adapter soft state
554 * @cmd: Command packet to be issued
555 *
556 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
557 */
558int
559wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd)
560{
561 int i;
562 struct megasas_header *frame_hdr = &cmd->frame->hdr;
563
564 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
565
566 /*
567 * Wait for cmd_status to change
568 */
569 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
570 rmb();
571 msleep(20);
572 }
573
574 if (frame_hdr->cmd_status == 0xff)
575 return -ETIME;
576
577 return 0;
578}
579
580/**
581 * megasas_ioc_init_fusion - Initializes the FW
582 * @instance: Adapter soft state
583 *
584 * Issues the IOC Init cmd
585 */
586int
587megasas_ioc_init_fusion(struct megasas_instance *instance)
588{
589 struct megasas_init_frame *init_frame;
590 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
591 dma_addr_t ioc_init_handle;
adam radford9c915a82010-12-21 13:34:31 -0800592 struct megasas_cmd *cmd;
593 u8 ret;
594 struct fusion_context *fusion;
595 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
596 int i;
597 struct megasas_header *frame_hdr;
598
599 fusion = instance->ctrl_context;
600
601 cmd = megasas_get_cmd(instance);
602
603 if (!cmd) {
604 printk(KERN_ERR "Could not allocate cmd for INIT Frame\n");
605 ret = 1;
606 goto fail_get_cmd;
607 }
608
609 IOCInitMessage =
610 dma_alloc_coherent(&instance->pdev->dev,
611 sizeof(struct MPI2_IOC_INIT_REQUEST),
612 &ioc_init_handle, GFP_KERNEL);
613
614 if (!IOCInitMessage) {
615 printk(KERN_ERR "Could not allocate memory for "
616 "IOCInitMessage\n");
617 ret = 1;
618 goto fail_fw_init;
619 }
620
621 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
622
623 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
624 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
625 IOCInitMessage->MsgVersion = MPI2_VERSION;
626 IOCInitMessage->HeaderVersion = MPI2_HEADER_VERSION;
627 IOCInitMessage->SystemRequestFrameSize =
628 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4;
629
630 IOCInitMessage->ReplyDescriptorPostQueueDepth = fusion->reply_q_depth;
631 IOCInitMessage->ReplyDescriptorPostQueueAddress =
632 fusion->reply_frames_desc_phys;
633 IOCInitMessage->SystemRequestFrameBaseAddress =
634 fusion->io_request_frames_phys;
adam radford5738f992012-03-19 19:49:53 -0700635 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
adam radford9c915a82010-12-21 13:34:31 -0800636 init_frame = (struct megasas_init_frame *)cmd->frame;
637 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
638
639 frame_hdr = &cmd->frame->hdr;
adam radford9c915a82010-12-21 13:34:31 -0800640 frame_hdr->cmd_status = 0xFF;
641 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
642
643 init_frame->cmd = MFI_CMD_INIT;
644 init_frame->cmd_status = 0xFF;
645
646 init_frame->queue_info_new_phys_addr_lo = ioc_init_handle;
647 init_frame->data_xfer_len = sizeof(struct MPI2_IOC_INIT_REQUEST);
648
649 req_desc =
650 (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)fusion->req_frames_desc;
651
652 req_desc->Words = cmd->frame_phys_addr;
653 req_desc->MFAIo.RequestFlags =
654 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
655 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
656
657 /*
658 * disable the intr before firing the init frame
659 */
660 instance->instancet->disable_intr(instance->reg_set);
661
662 for (i = 0; i < (10 * 1000); i += 20) {
663 if (readl(&instance->reg_set->doorbell) & 1)
664 msleep(20);
665 else
666 break;
667 }
668
669 instance->instancet->fire_cmd(instance, req_desc->u.low,
670 req_desc->u.high, instance->reg_set);
671
672 wait_and_poll(instance, cmd);
673
674 frame_hdr = &cmd->frame->hdr;
675 if (frame_hdr->cmd_status != 0) {
676 ret = 1;
677 goto fail_fw_init;
678 }
679 printk(KERN_ERR "megasas:IOC Init cmd success\n");
680
681 ret = 0;
682
683fail_fw_init:
684 megasas_return_cmd(instance, cmd);
685 if (IOCInitMessage)
686 dma_free_coherent(&instance->pdev->dev,
687 sizeof(struct MPI2_IOC_INIT_REQUEST),
688 IOCInitMessage, ioc_init_handle);
689fail_get_cmd:
690 return ret;
691}
692
693/*
adam radford9c915a82010-12-21 13:34:31 -0800694 * megasas_get_ld_map_info - Returns FW's ld_map structure
695 * @instance: Adapter soft state
696 * @pend: Pend the command or not
697 * Issues an internal command (DCMD) to get the FW's controller PD
698 * list structure. This information is mainly used to find out SYSTEM
699 * supported by the FW.
700 */
701static int
702megasas_get_ld_map_info(struct megasas_instance *instance)
703{
704 int ret = 0;
705 struct megasas_cmd *cmd;
706 struct megasas_dcmd_frame *dcmd;
707 struct MR_FW_RAID_MAP_ALL *ci;
708 dma_addr_t ci_h = 0;
709 u32 size_map_info;
710 struct fusion_context *fusion;
711
712 cmd = megasas_get_cmd(instance);
713
714 if (!cmd) {
715 printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
716 return -ENOMEM;
717 }
718
719 fusion = instance->ctrl_context;
720
721 if (!fusion) {
722 megasas_return_cmd(instance, cmd);
723 return 1;
724 }
725
726 dcmd = &cmd->frame->dcmd;
727
728 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
729 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
730
731 ci = fusion->ld_map[(instance->map_id & 1)];
732 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
733
734 if (!ci) {
735 printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
736 megasas_return_cmd(instance, cmd);
737 return -ENOMEM;
738 }
739
740 memset(ci, 0, sizeof(*ci));
741 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
742
743 dcmd->cmd = MFI_CMD_DCMD;
744 dcmd->cmd_status = 0xFF;
745 dcmd->sge_count = 1;
746 dcmd->flags = MFI_FRAME_DIR_READ;
747 dcmd->timeout = 0;
748 dcmd->pad_0 = 0;
749 dcmd->data_xfer_len = size_map_info;
750 dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
751 dcmd->sgl.sge32[0].phys_addr = ci_h;
752 dcmd->sgl.sge32[0].length = size_map_info;
753
754 if (!megasas_issue_polled(instance, cmd))
755 ret = 0;
756 else {
757 printk(KERN_ERR "megasas: Get LD Map Info Failed\n");
758 ret = -1;
759 }
760
761 megasas_return_cmd(instance, cmd);
762
763 return ret;
764}
765
766u8
767megasas_get_map_info(struct megasas_instance *instance)
768{
769 struct fusion_context *fusion = instance->ctrl_context;
770
771 fusion->fast_path_io = 0;
772 if (!megasas_get_ld_map_info(instance)) {
773 if (MR_ValidateMapInfo(fusion->ld_map[(instance->map_id & 1)],
774 fusion->load_balance_info)) {
775 fusion->fast_path_io = 1;
776 return 0;
777 }
778 }
779 return 1;
780}
781
782/*
783 * megasas_sync_map_info - Returns FW's ld_map structure
784 * @instance: Adapter soft state
785 *
786 * Issues an internal command (DCMD) to get the FW's controller PD
787 * list structure. This information is mainly used to find out SYSTEM
788 * supported by the FW.
789 */
790int
791megasas_sync_map_info(struct megasas_instance *instance)
792{
793 int ret = 0, i;
794 struct megasas_cmd *cmd;
795 struct megasas_dcmd_frame *dcmd;
796 u32 size_sync_info, num_lds;
797 struct fusion_context *fusion;
798 struct MR_LD_TARGET_SYNC *ci = NULL;
799 struct MR_FW_RAID_MAP_ALL *map;
800 struct MR_LD_RAID *raid;
801 struct MR_LD_TARGET_SYNC *ld_sync;
802 dma_addr_t ci_h = 0;
803 u32 size_map_info;
804
805 cmd = megasas_get_cmd(instance);
806
807 if (!cmd) {
808 printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
809 "info.\n");
810 return -ENOMEM;
811 }
812
813 fusion = instance->ctrl_context;
814
815 if (!fusion) {
816 megasas_return_cmd(instance, cmd);
817 return 1;
818 }
819
820 map = fusion->ld_map[instance->map_id & 1];
821
822 num_lds = map->raidMap.ldCount;
823
824 dcmd = &cmd->frame->dcmd;
825
826 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
827
828 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
829
830 ci = (struct MR_LD_TARGET_SYNC *)
831 fusion->ld_map[(instance->map_id - 1) & 1];
832 memset(ci, 0, sizeof(struct MR_FW_RAID_MAP_ALL));
833
834 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
835
836 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
837
838 for (i = 0; i < num_lds; i++, ld_sync++) {
839 raid = MR_LdRaidGet(i, map);
840 ld_sync->targetId = MR_GetLDTgtId(i, map);
841 ld_sync->seqNum = raid->seqNum;
842 }
843
844 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
845 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
846
847 dcmd->cmd = MFI_CMD_DCMD;
848 dcmd->cmd_status = 0xFF;
849 dcmd->sge_count = 1;
850 dcmd->flags = MFI_FRAME_DIR_WRITE;
851 dcmd->timeout = 0;
852 dcmd->pad_0 = 0;
853 dcmd->data_xfer_len = size_map_info;
854 dcmd->mbox.b[0] = num_lds;
855 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
856 dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
857 dcmd->sgl.sge32[0].phys_addr = ci_h;
858 dcmd->sgl.sge32[0].length = size_map_info;
859
860 instance->map_update_cmd = cmd;
861
862 instance->instancet->issue_dcmd(instance, cmd);
863
864 return ret;
865}
866
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +0530867/*
868 * meagasas_display_intel_branding - Display branding string
869 * @instance: per adapter object
870 *
871 * Return nothing.
872 */
873static void
874megasas_display_intel_branding(struct megasas_instance *instance)
875{
876 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
877 return;
878
879 switch (instance->pdev->device) {
880 case PCI_DEVICE_ID_LSI_INVADER:
881 switch (instance->pdev->subsystem_device) {
882 case MEGARAID_INTEL_RS3DC080_SSDID:
883 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
884 instance->host->host_no,
885 MEGARAID_INTEL_RS3DC080_BRANDING);
886 break;
887 case MEGARAID_INTEL_RS3DC040_SSDID:
888 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
889 instance->host->host_no,
890 MEGARAID_INTEL_RS3DC040_BRANDING);
891 break;
892 case MEGARAID_INTEL_RS3SC008_SSDID:
893 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
894 instance->host->host_no,
895 MEGARAID_INTEL_RS3SC008_BRANDING);
896 break;
897 case MEGARAID_INTEL_RS3MC044_SSDID:
898 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
899 instance->host->host_no,
900 MEGARAID_INTEL_RS3MC044_BRANDING);
901 break;
902 default:
903 break;
904 }
905 break;
906 case PCI_DEVICE_ID_LSI_FURY:
907 switch (instance->pdev->subsystem_device) {
908 case MEGARAID_INTEL_RS3WC080_SSDID:
909 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
910 instance->host->host_no,
911 MEGARAID_INTEL_RS3WC080_BRANDING);
912 break;
913 case MEGARAID_INTEL_RS3WC040_SSDID:
914 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
915 instance->host->host_no,
916 MEGARAID_INTEL_RS3WC040_BRANDING);
917 break;
918 default:
919 break;
920 }
921 break;
922 default:
923 break;
924 }
925}
926
adam radford9c915a82010-12-21 13:34:31 -0800927/**
928 * megasas_init_adapter_fusion - Initializes the FW
929 * @instance: Adapter soft state
930 *
931 * This is the main function for initializing firmware.
932 */
933u32
934megasas_init_adapter_fusion(struct megasas_instance *instance)
935{
936 struct megasas_register_set __iomem *reg_set;
937 struct fusion_context *fusion;
938 u32 max_cmd;
adam radfordc8e858f2011-10-08 18:15:13 -0700939 int i = 0, count;
adam radford9c915a82010-12-21 13:34:31 -0800940
941 fusion = instance->ctrl_context;
942
943 reg_set = instance->reg_set;
944
945 /*
946 * Get various operational parameters from status register
947 */
948 instance->max_fw_cmds =
949 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
950 instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
951
952 /*
953 * Reduce the max supported cmds by 1. This is to ensure that the
954 * reply_q_sz (1 more than the max cmd that driver may send)
955 * does not exceed max cmds that the FW can support
956 */
957 instance->max_fw_cmds = instance->max_fw_cmds-1;
958 /* Only internal cmds (DCMD) need to have MFI frames */
959 instance->max_mfi_cmds = MEGASAS_INT_CMDS;
960
961 max_cmd = instance->max_fw_cmds;
962
963 fusion->reply_q_depth = ((max_cmd + 1 + 15)/16)*16;
964
965 fusion->request_alloc_sz =
966 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
967 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
968 *(fusion->reply_q_depth);
969 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
970 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
971 (max_cmd + 1)); /* Extra 1 for SMID 0 */
972
973 fusion->max_sge_in_main_msg =
974 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
975 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
976
977 fusion->max_sge_in_chain =
978 MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
979
980 instance->max_num_sge = fusion->max_sge_in_main_msg +
981 fusion->max_sge_in_chain - 2;
982
983 /* Used for pass thru MFI frame (DCMD) */
984 fusion->chain_offset_mfi_pthru =
985 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
986
987 fusion->chain_offset_io_request =
988 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
989 sizeof(union MPI2_SGE_IO_UNION))/16;
990
adam radfordc8e858f2011-10-08 18:15:13 -0700991 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
992 for (i = 0 ; i < count; i++)
993 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -0800994
995 /*
996 * Allocate memory for descriptors
997 * Create a pool of commands
998 */
999 if (megasas_alloc_cmds(instance))
1000 goto fail_alloc_mfi_cmds;
1001 if (megasas_alloc_cmds_fusion(instance))
1002 goto fail_alloc_cmds;
1003
1004 if (megasas_ioc_init_fusion(instance))
1005 goto fail_ioc_init;
1006
Sumit.Saxena@lsi.com39b72c32013-05-22 12:32:43 +05301007 megasas_display_intel_branding(instance);
1008
adam radford9c915a82010-12-21 13:34:31 -08001009 instance->flag_ieee = 1;
1010
1011 fusion->map_sz = sizeof(struct MR_FW_RAID_MAP) +
1012 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
1013
1014 fusion->fast_path_io = 0;
1015
1016 for (i = 0; i < 2; i++) {
1017 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
1018 fusion->map_sz,
1019 &fusion->ld_map_phys[i],
1020 GFP_KERNEL);
1021 if (!fusion->ld_map[i]) {
1022 printk(KERN_ERR "megasas: Could not allocate memory "
1023 "for map info\n");
1024 goto fail_map_info;
1025 }
1026 }
1027
1028 if (!megasas_get_map_info(instance))
1029 megasas_sync_map_info(instance);
1030
1031 return 0;
1032
adam radford9c915a82010-12-21 13:34:31 -08001033fail_map_info:
1034 if (i == 1)
1035 dma_free_coherent(&instance->pdev->dev, fusion->map_sz,
1036 fusion->ld_map[0], fusion->ld_map_phys[0]);
1037fail_ioc_init:
adam radfordeb1b1232011-02-24 20:55:56 -08001038 megasas_free_cmds_fusion(instance);
1039fail_alloc_cmds:
1040 megasas_free_cmds(instance);
1041fail_alloc_mfi_cmds:
adam radford9c915a82010-12-21 13:34:31 -08001042 return 1;
1043}
1044
1045/**
1046 * megasas_fire_cmd_fusion - Sends command to the FW
1047 * @frame_phys_addr : Physical address of cmd
1048 * @frame_count : Number of frames for the command
1049 * @regs : MFI register set
1050 */
1051void
1052megasas_fire_cmd_fusion(struct megasas_instance *instance,
1053 dma_addr_t req_desc_lo,
1054 u32 req_desc_hi,
1055 struct megasas_register_set __iomem *regs)
1056{
1057 unsigned long flags;
1058
1059 spin_lock_irqsave(&instance->hba_lock, flags);
1060
1061 writel(req_desc_lo,
1062 &(regs)->inbound_low_queue_port);
1063 writel(req_desc_hi, &(regs)->inbound_high_queue_port);
1064 spin_unlock_irqrestore(&instance->hba_lock, flags);
1065}
1066
1067/**
1068 * map_cmd_status - Maps FW cmd status to OS cmd status
1069 * @cmd : Pointer to cmd
1070 * @status : status of cmd returned by FW
1071 * @ext_status : ext status of cmd returned by FW
1072 */
1073
1074void
1075map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1076{
1077
1078 switch (status) {
1079
1080 case MFI_STAT_OK:
1081 cmd->scmd->result = DID_OK << 16;
1082 break;
1083
1084 case MFI_STAT_SCSI_IO_FAILED:
1085 case MFI_STAT_LD_INIT_IN_PROGRESS:
1086 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1087 break;
1088
1089 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1090
1091 cmd->scmd->result = (DID_OK << 16) | ext_status;
1092 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1093 memset(cmd->scmd->sense_buffer, 0,
1094 SCSI_SENSE_BUFFERSIZE);
1095 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1096 SCSI_SENSE_BUFFERSIZE);
1097 cmd->scmd->result |= DRIVER_SENSE << 24;
1098 }
1099 break;
1100
1101 case MFI_STAT_LD_OFFLINE:
1102 case MFI_STAT_DEVICE_NOT_FOUND:
1103 cmd->scmd->result = DID_BAD_TARGET << 16;
1104 break;
adam radford36807e62011-10-08 18:15:06 -07001105 case MFI_STAT_CONFIG_SEQ_MISMATCH:
1106 cmd->scmd->result = DID_IMM_RETRY << 16;
1107 break;
adam radford9c915a82010-12-21 13:34:31 -08001108 default:
1109 printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1110 cmd->scmd->result = DID_ERROR << 16;
1111 break;
1112 }
1113}
1114
1115/**
1116 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1117 * @instance: Adapter soft state
1118 * @scp: SCSI command from the mid-layer
1119 * @sgl_ptr: SGL to be filled in
1120 * @cmd: cmd we are working on
1121 *
1122 * If successful, this function returns the number of SG elements.
1123 */
1124static int
1125megasas_make_sgl_fusion(struct megasas_instance *instance,
1126 struct scsi_cmnd *scp,
1127 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1128 struct megasas_cmd_fusion *cmd)
1129{
adam radford36807e62011-10-08 18:15:06 -07001130 int i, sg_processed, sge_count;
adam radford9c915a82010-12-21 13:34:31 -08001131 struct scatterlist *os_sgl;
1132 struct fusion_context *fusion;
1133
1134 fusion = instance->ctrl_context;
1135
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301136 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1137 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001138 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1139 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1140 sgl_ptr_end->Flags = 0;
1141 }
adam radford9c915a82010-12-21 13:34:31 -08001142
1143 sge_count = scsi_dma_map(scp);
1144
1145 BUG_ON(sge_count < 0);
1146
1147 if (sge_count > instance->max_num_sge || !sge_count)
1148 return sge_count;
1149
adam radford9c915a82010-12-21 13:34:31 -08001150 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1151 sgl_ptr->Length = sg_dma_len(os_sgl);
1152 sgl_ptr->Address = sg_dma_address(os_sgl);
1153 sgl_ptr->Flags = 0;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301154 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1155 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001156 if (i == sge_count - 1)
1157 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1158 }
adam radford9c915a82010-12-21 13:34:31 -08001159 sgl_ptr++;
1160
1161 sg_processed = i + 1;
1162
1163 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1164 (sge_count > fusion->max_sge_in_main_msg)) {
1165
1166 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301167 if ((instance->pdev->device ==
1168 PCI_DEVICE_ID_LSI_INVADER) ||
1169 (instance->pdev->device ==
1170 PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001171 if ((cmd->io_request->IoFlags &
1172 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1173 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1174 cmd->io_request->ChainOffset =
1175 fusion->
1176 chain_offset_io_request;
1177 else
1178 cmd->io_request->ChainOffset = 0;
1179 } else
1180 cmd->io_request->ChainOffset =
1181 fusion->chain_offset_io_request;
1182
adam radford9c915a82010-12-21 13:34:31 -08001183 sg_chain = sgl_ptr;
1184 /* Prepare chain element */
1185 sg_chain->NextChainOffset = 0;
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301186 if ((instance->pdev->device ==
1187 PCI_DEVICE_ID_LSI_INVADER) ||
1188 (instance->pdev->device ==
1189 PCI_DEVICE_ID_LSI_FURY))
adam radford36807e62011-10-08 18:15:06 -07001190 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1191 else
1192 sg_chain->Flags =
1193 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1194 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
adam radford9c915a82010-12-21 13:34:31 -08001195 sg_chain->Length = (sizeof(union MPI2_SGE_IO_UNION)
1196 *(sge_count - sg_processed));
1197 sg_chain->Address = cmd->sg_frame_phys_addr;
1198
1199 sgl_ptr =
1200 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1201 }
1202 }
1203
1204 return sge_count;
1205}
1206
1207/**
1208 * megasas_set_pd_lba - Sets PD LBA
1209 * @cdb: CDB
1210 * @cdb_len: cdb length
1211 * @start_blk: Start block of IO
1212 *
1213 * Used to set the PD LBA in CDB for FP IOs
1214 */
1215void
1216megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1217 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
1218 struct MR_FW_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
1219{
1220 struct MR_LD_RAID *raid;
1221 u32 ld;
1222 u64 start_blk = io_info->pdBlock;
1223 u8 *cdb = io_request->CDB.CDB32;
1224 u32 num_blocks = io_info->numBlocks;
adam radford495c5602011-05-11 18:34:40 -07001225 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
adam radford9c915a82010-12-21 13:34:31 -08001226
1227 /* Check if T10 PI (DIF) is enabled for this LD */
1228 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1229 raid = MR_LdRaidGet(ld, local_map_ptr);
1230 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1231 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1232 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1233 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1234
1235 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1236 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1237 else
1238 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1239 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1240
1241 /* LBA */
1242 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1243 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1244 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1245 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1246 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1247 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1248 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1249 cdb[19] = (u8)(start_blk & 0xff);
1250
1251 /* Logical block reference tag */
1252 io_request->CDB.EEDP32.PrimaryReferenceTag =
1253 cpu_to_be32(ref_tag);
1254 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0xffff;
adam radford9c915a82010-12-21 13:34:31 -08001255 io_request->IoFlags = 32; /* Specify 32-byte cdb */
1256
1257 /* Transfer length */
1258 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1259 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1260 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1261 cdb[31] = (u8)(num_blocks & 0xff);
1262
1263 /* set SCSI IO EEDPFlags */
1264 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
1265 io_request->EEDPFlags =
1266 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1267 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1268 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1269 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
1270 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
1271 } else {
1272 io_request->EEDPFlags =
1273 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1274 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
1275 }
1276 io_request->Control |= (0x4 << 26);
adam radford046c9ec2013-02-09 15:29:15 -08001277 io_request->EEDPBlockSize = scp->device->sector_size;
adam radford9c915a82010-12-21 13:34:31 -08001278 } else {
1279 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1280 if (((cdb_len == 12) || (cdb_len == 16)) &&
1281 (start_blk <= 0xffffffff)) {
1282 if (cdb_len == 16) {
1283 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1284 flagvals = cdb[1];
1285 groupnum = cdb[14];
1286 control = cdb[15];
1287 } else {
1288 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1289 flagvals = cdb[1];
1290 groupnum = cdb[10];
1291 control = cdb[11];
1292 }
1293
1294 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1295
1296 cdb[0] = opcode;
1297 cdb[1] = flagvals;
1298 cdb[6] = groupnum;
1299 cdb[9] = control;
1300
1301 /* Transfer length */
1302 cdb[8] = (u8)(num_blocks & 0xff);
1303 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1304
adam radford495c5602011-05-11 18:34:40 -07001305 io_request->IoFlags = 10; /* Specify 10-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001306 cdb_len = 10;
adam radford495c5602011-05-11 18:34:40 -07001307 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1308 /* Convert to 16 byte CDB for large LBA's */
1309 switch (cdb_len) {
1310 case 6:
1311 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1312 control = cdb[5];
1313 break;
1314 case 10:
1315 opcode =
1316 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1317 flagvals = cdb[1];
1318 groupnum = cdb[6];
1319 control = cdb[9];
1320 break;
1321 case 12:
1322 opcode =
1323 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1324 flagvals = cdb[1];
1325 groupnum = cdb[10];
1326 control = cdb[11];
1327 break;
1328 }
1329
1330 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1331
1332 cdb[0] = opcode;
1333 cdb[1] = flagvals;
1334 cdb[14] = groupnum;
1335 cdb[15] = control;
1336
1337 /* Transfer length */
1338 cdb[13] = (u8)(num_blocks & 0xff);
1339 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1340 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1341 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1342
1343 io_request->IoFlags = 16; /* Specify 16-byte cdb */
1344 cdb_len = 16;
adam radford9c915a82010-12-21 13:34:31 -08001345 }
1346
1347 /* Normal case, just load LBA here */
1348 switch (cdb_len) {
1349 case 6:
1350 {
1351 u8 val = cdb[1] & 0xE0;
1352 cdb[3] = (u8)(start_blk & 0xff);
1353 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1354 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1355 break;
1356 }
1357 case 10:
1358 cdb[5] = (u8)(start_blk & 0xff);
1359 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1360 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1361 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1362 break;
1363 case 12:
1364 cdb[5] = (u8)(start_blk & 0xff);
1365 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1366 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1367 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1368 break;
1369 case 16:
1370 cdb[9] = (u8)(start_blk & 0xff);
1371 cdb[8] = (u8)((start_blk >> 8) & 0xff);
1372 cdb[7] = (u8)((start_blk >> 16) & 0xff);
1373 cdb[6] = (u8)((start_blk >> 24) & 0xff);
1374 cdb[5] = (u8)((start_blk >> 32) & 0xff);
1375 cdb[4] = (u8)((start_blk >> 40) & 0xff);
1376 cdb[3] = (u8)((start_blk >> 48) & 0xff);
1377 cdb[2] = (u8)((start_blk >> 56) & 0xff);
1378 break;
1379 }
1380 }
1381}
1382
1383/**
1384 * megasas_build_ldio_fusion - Prepares IOs to devices
1385 * @instance: Adapter soft state
1386 * @scp: SCSI command
1387 * @cmd: Command to be prepared
1388 *
1389 * Prepares the io_request and chain elements (sg_frame) for IO
1390 * The IO can be for PD (Fast Path) or LD
1391 */
1392void
1393megasas_build_ldio_fusion(struct megasas_instance *instance,
1394 struct scsi_cmnd *scp,
1395 struct megasas_cmd_fusion *cmd)
1396{
1397 u8 fp_possible;
adam radfordf9eff812012-10-01 19:27:07 -07001398 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
adam radford9c915a82010-12-21 13:34:31 -08001399 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1400 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1401 struct IO_REQUEST_INFO io_info;
1402 struct fusion_context *fusion;
1403 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1404
1405 device_id = MEGASAS_DEV_INDEX(instance, scp);
1406
1407 fusion = instance->ctrl_context;
1408
1409 io_request = cmd->io_request;
1410 io_request->RaidContext.VirtualDiskTgtId = device_id;
1411 io_request->RaidContext.status = 0;
1412 io_request->RaidContext.exStatus = 0;
1413
1414 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1415
1416 start_lba_lo = 0;
1417 start_lba_hi = 0;
1418 fp_possible = 0;
1419
1420 /*
1421 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1422 */
1423 if (scp->cmd_len == 6) {
adam radfordf9eff812012-10-01 19:27:07 -07001424 datalength = (u32) scp->cmnd[4];
adam radford9c915a82010-12-21 13:34:31 -08001425 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1426 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1427
1428 start_lba_lo &= 0x1FFFFF;
1429 }
1430
1431 /*
1432 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1433 */
1434 else if (scp->cmd_len == 10) {
adam radfordf9eff812012-10-01 19:27:07 -07001435 datalength = (u32) scp->cmnd[8] |
adam radford9c915a82010-12-21 13:34:31 -08001436 ((u32) scp->cmnd[7] << 8);
1437 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1438 ((u32) scp->cmnd[3] << 16) |
1439 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1440 }
1441
1442 /*
1443 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1444 */
1445 else if (scp->cmd_len == 12) {
adam radfordf9eff812012-10-01 19:27:07 -07001446 datalength = ((u32) scp->cmnd[6] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08001447 ((u32) scp->cmnd[7] << 16) |
1448 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1449 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1450 ((u32) scp->cmnd[3] << 16) |
1451 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1452 }
1453
1454 /*
1455 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1456 */
1457 else if (scp->cmd_len == 16) {
adam radfordf9eff812012-10-01 19:27:07 -07001458 datalength = ((u32) scp->cmnd[10] << 24) |
adam radford9c915a82010-12-21 13:34:31 -08001459 ((u32) scp->cmnd[11] << 16) |
1460 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1461 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1462 ((u32) scp->cmnd[7] << 16) |
1463 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1464
1465 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1466 ((u32) scp->cmnd[3] << 16) |
1467 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1468 }
1469
1470 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1471 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
adam radfordf9eff812012-10-01 19:27:07 -07001472 io_info.numBlocks = datalength;
adam radford9c915a82010-12-21 13:34:31 -08001473 io_info.ldTgtId = device_id;
adam radfordf9eff812012-10-01 19:27:07 -07001474 io_request->DataLength = scsi_bufflen(scp);
adam radford9c915a82010-12-21 13:34:31 -08001475
1476 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1477 io_info.isRead = 1;
1478
1479 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1480
1481 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
1482 MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io)) {
1483 io_request->RaidContext.regLockFlags = 0;
1484 fp_possible = 0;
1485 } else {
adam radford36807e62011-10-08 18:15:06 -07001486 if (MR_BuildRaidContext(instance, &io_info,
1487 &io_request->RaidContext,
adam radford9c915a82010-12-21 13:34:31 -08001488 local_map_ptr))
1489 fp_possible = io_info.fpOkForIo;
1490 }
1491
adam radfordc8e858f2011-10-08 18:15:13 -07001492 /* Use smp_processor_id() for now until cmd->request->cpu is CPU
1493 id by default, not CPU group id, otherwise all MSI-X queues won't
1494 be utilized */
1495 cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
1496 smp_processor_id() % instance->msix_vectors : 0;
1497
adam radford9c915a82010-12-21 13:34:31 -08001498 if (fp_possible) {
1499 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1500 local_map_ptr, start_lba_lo);
adam radford9c915a82010-12-21 13:34:31 -08001501 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1502 cmd->request_desc->SCSIIO.RequestFlags =
1503 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1504 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301505 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1506 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001507 if (io_request->RaidContext.regLockFlags ==
1508 REGION_TYPE_UNUSED)
1509 cmd->request_desc->SCSIIO.RequestFlags =
1510 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1511 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1512 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1513 io_request->RaidContext.nseg = 0x1;
1514 io_request->IoFlags |=
1515 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
1516 io_request->RaidContext.regLockFlags |=
1517 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
1518 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1519 }
adam radford9c915a82010-12-21 13:34:31 -08001520 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1521 (io_info.isRead)) {
1522 io_info.devHandle =
1523 get_updated_dev_handle(
1524 &fusion->load_balance_info[device_id],
1525 &io_info);
1526 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
1527 } else
1528 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1529 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1530 io_request->DevHandle = io_info.devHandle;
1531 } else {
1532 io_request->RaidContext.timeoutValue =
1533 local_map_ptr->raidMap.fpPdIoTimeoutSec;
adam radford9c915a82010-12-21 13:34:31 -08001534 cmd->request_desc->SCSIIO.RequestFlags =
1535 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1536 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05301537 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1538 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07001539 if (io_request->RaidContext.regLockFlags ==
1540 REGION_TYPE_UNUSED)
1541 cmd->request_desc->SCSIIO.RequestFlags =
1542 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1543 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1544 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1545 io_request->RaidContext.regLockFlags |=
1546 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
1547 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1548 io_request->RaidContext.nseg = 0x1;
1549 }
1550 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1551 io_request->DevHandle = device_id;
adam radford9c915a82010-12-21 13:34:31 -08001552 } /* Not FP */
1553}
1554
1555/**
1556 * megasas_build_dcdb_fusion - Prepares IOs to devices
1557 * @instance: Adapter soft state
1558 * @scp: SCSI command
1559 * @cmd: Command to be prepared
1560 *
1561 * Prepares the io_request frame for non-io cmds
1562 */
1563static void
1564megasas_build_dcdb_fusion(struct megasas_instance *instance,
1565 struct scsi_cmnd *scmd,
1566 struct megasas_cmd_fusion *cmd)
1567{
1568 u32 device_id;
1569 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1570 u16 pd_index = 0;
1571 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1572 struct fusion_context *fusion = instance->ctrl_context;
1573
1574 io_request = cmd->io_request;
1575 device_id = MEGASAS_DEV_INDEX(instance, scmd);
1576 pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
1577 +scmd->device->id;
1578 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1579
1580 /* Check if this is a system PD I/O */
adam radforde187df62012-10-01 19:27:12 -07001581 if (scmd->device->channel < MEGASAS_MAX_PD_CHANNELS &&
1582 instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
adam radford9c915a82010-12-21 13:34:31 -08001583 io_request->Function = 0;
adam radford9c5ebd02013-02-09 15:29:20 -08001584 if (fusion->fast_path_io)
1585 io_request->DevHandle =
adam radford9c915a82010-12-21 13:34:31 -08001586 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1587 io_request->RaidContext.timeoutValue =
1588 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1589 io_request->RaidContext.regLockFlags = 0;
1590 io_request->RaidContext.regLockRowLBA = 0;
1591 io_request->RaidContext.regLockLength = 0;
1592 io_request->RaidContext.RAIDFlags =
1593 MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
1594 MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1595 cmd->request_desc->SCSIIO.RequestFlags =
1596 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1597 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
adam radford8ead5812012-10-01 19:27:20 -07001598 cmd->request_desc->SCSIIO.DevHandle =
1599 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
Sumit.Saxena@lsi.com32d87452013-05-22 12:31:18 +05301600 /*
1601 * If the command is for the tape device, set the
1602 * FP timeout to the os layer timeout value.
1603 */
1604 if (scmd->device->type == TYPE_TAPE) {
1605 if ((scmd->request->timeout / HZ) > 0xFFFF)
1606 io_request->RaidContext.timeoutValue =
1607 0xFFFF;
1608 else
1609 io_request->RaidContext.timeoutValue =
1610 scmd->request->timeout / HZ;
1611 }
adam radford9c915a82010-12-21 13:34:31 -08001612 } else {
1613 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1614 io_request->DevHandle = device_id;
1615 cmd->request_desc->SCSIIO.RequestFlags =
1616 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1617 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1618 }
1619 io_request->RaidContext.VirtualDiskTgtId = device_id;
adam radfordeaa3c242011-02-24 20:56:49 -08001620 io_request->LUN[1] = scmd->device->lun;
adam radford9c915a82010-12-21 13:34:31 -08001621 io_request->DataLength = scsi_bufflen(scmd);
1622}
1623
1624/**
1625 * megasas_build_io_fusion - Prepares IOs to devices
1626 * @instance: Adapter soft state
1627 * @scp: SCSI command
1628 * @cmd: Command to be prepared
1629 *
1630 * Invokes helper functions to prepare request frames
1631 * and sets flags appropriate for IO/Non-IO cmd
1632 */
1633int
1634megasas_build_io_fusion(struct megasas_instance *instance,
1635 struct scsi_cmnd *scp,
1636 struct megasas_cmd_fusion *cmd)
1637{
1638 u32 device_id, sge_count;
1639 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1640
1641 device_id = MEGASAS_DEV_INDEX(instance, scp);
1642
1643 /* Zero out some fields so they don't get reused */
adam radfordeaa3c242011-02-24 20:56:49 -08001644 io_request->LUN[1] = 0;
adam radford9c915a82010-12-21 13:34:31 -08001645 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1646 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1647 io_request->EEDPFlags = 0;
1648 io_request->Control = 0;
1649 io_request->EEDPBlockSize = 0;
adam radford36807e62011-10-08 18:15:06 -07001650 io_request->ChainOffset = 0;
adam radford9c915a82010-12-21 13:34:31 -08001651 io_request->RaidContext.RAIDFlags = 0;
adam radford36807e62011-10-08 18:15:06 -07001652 io_request->RaidContext.Type = 0;
1653 io_request->RaidContext.nseg = 0;
adam radford9c915a82010-12-21 13:34:31 -08001654
1655 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1656 /*
1657 * Just the CDB length,rest of the Flags are zero
1658 * This will be modified for FP in build_ldio_fusion
1659 */
1660 io_request->IoFlags = scp->cmd_len;
1661
1662 if (megasas_is_ldio(scp))
1663 megasas_build_ldio_fusion(instance, scp, cmd);
1664 else
1665 megasas_build_dcdb_fusion(instance, scp, cmd);
1666
1667 /*
1668 * Construct SGL
1669 */
1670
1671 sge_count =
1672 megasas_make_sgl_fusion(instance, scp,
1673 (struct MPI25_IEEE_SGE_CHAIN64 *)
1674 &io_request->SGL, cmd);
1675
1676 if (sge_count > instance->max_num_sge) {
1677 printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1678 "max (0x%x) allowed\n", sge_count,
1679 instance->max_num_sge);
1680 return 1;
1681 }
1682
1683 io_request->RaidContext.numSGE = sge_count;
1684
1685 io_request->SGLFlags = MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
1686
1687 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1688 io_request->Control |= MPI2_SCSIIO_CONTROL_WRITE;
1689 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1690 io_request->Control |= MPI2_SCSIIO_CONTROL_READ;
1691
1692 io_request->SGLOffset0 =
1693 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1694
1695 io_request->SenseBufferLowAddress = cmd->sense_phys_addr;
1696 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1697
1698 cmd->scmd = scp;
1699 scp->SCp.ptr = (char *)cmd;
1700
1701 return 0;
1702}
1703
1704union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1705megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1706{
1707 u8 *p;
1708 struct fusion_context *fusion;
1709
1710 if (index >= instance->max_fw_cmds) {
1711 printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
1712 "descriptor\n", index);
1713 return NULL;
1714 }
1715 fusion = instance->ctrl_context;
1716 p = fusion->req_frames_desc
1717 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1718
1719 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1720}
1721
1722/**
1723 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1724 * issuing non IOCTL cmd
1725 * @instance: Adapter soft state
1726 * @scmd: pointer to scsi cmd from OS
1727 */
1728static u32
1729megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1730 struct scsi_cmnd *scmd)
1731{
1732 struct megasas_cmd_fusion *cmd;
1733 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1734 u32 index;
1735 struct fusion_context *fusion;
1736
1737 fusion = instance->ctrl_context;
1738
1739 cmd = megasas_get_cmd_fusion(instance);
1740 if (!cmd)
1741 return SCSI_MLQUEUE_HOST_BUSY;
1742
1743 index = cmd->index;
1744
1745 req_desc = megasas_get_request_descriptor(instance, index-1);
1746 if (!req_desc)
1747 return 1;
1748
1749 req_desc->Words = 0;
1750 cmd->request_desc = req_desc;
adam radford9c915a82010-12-21 13:34:31 -08001751
1752 if (megasas_build_io_fusion(instance, scmd, cmd)) {
1753 megasas_return_cmd_fusion(instance, cmd);
1754 printk(KERN_ERR "megasas: Error building command.\n");
1755 cmd->request_desc = NULL;
1756 return 1;
1757 }
1758
1759 req_desc = cmd->request_desc;
1760 req_desc->SCSIIO.SMID = index;
1761
1762 if (cmd->io_request->ChainOffset != 0 &&
1763 cmd->io_request->ChainOffset != 0xF)
1764 printk(KERN_ERR "megasas: The chain offset value is not "
1765 "correct : %x\n", cmd->io_request->ChainOffset);
1766
1767 /*
1768 * Issue the command to the FW
1769 */
1770 atomic_inc(&instance->fw_outstanding);
1771
1772 instance->instancet->fire_cmd(instance,
1773 req_desc->u.low, req_desc->u.high,
1774 instance->reg_set);
1775
1776 return 0;
1777}
1778
1779/**
1780 * complete_cmd_fusion - Completes command
1781 * @instance: Adapter soft state
1782 * Completes all commands that is in reply descriptor queue
1783 */
1784int
adam radfordc8e858f2011-10-08 18:15:13 -07001785complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
adam radford9c915a82010-12-21 13:34:31 -08001786{
1787 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1788 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1789 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1790 struct fusion_context *fusion;
1791 struct megasas_cmd *cmd_mfi;
1792 struct megasas_cmd_fusion *cmd_fusion;
1793 u16 smid, num_completed;
1794 u8 reply_descript_type, arm;
1795 u32 status, extStatus, device_id;
1796 union desc_value d_val;
1797 struct LD_LOAD_BALANCE_INFO *lbinfo;
1798
1799 fusion = instance->ctrl_context;
1800
1801 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1802 return IRQ_HANDLED;
1803
1804 desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -07001805 desc += ((MSIxIndex * fusion->reply_alloc_sz)/
1806 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
1807 fusion->last_reply_idx[MSIxIndex];
adam radford9c915a82010-12-21 13:34:31 -08001808
1809 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1810
1811 d_val.word = desc->Words;
1812
1813 reply_descript_type = reply_desc->ReplyFlags &
1814 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1815
1816 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1817 return IRQ_NONE;
1818
adam radford9c915a82010-12-21 13:34:31 -08001819 num_completed = 0;
1820
1821 while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
1822 smid = reply_desc->SMID;
1823
1824 cmd_fusion = fusion->cmd_list[smid - 1];
1825
1826 scsi_io_req =
1827 (struct MPI2_RAID_SCSI_IO_REQUEST *)
1828 cmd_fusion->io_request;
1829
1830 if (cmd_fusion->scmd)
1831 cmd_fusion->scmd->SCp.ptr = NULL;
1832
1833 status = scsi_io_req->RaidContext.status;
1834 extStatus = scsi_io_req->RaidContext.exStatus;
1835
1836 switch (scsi_io_req->Function) {
1837 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
1838 /* Update load balancing info */
1839 device_id = MEGASAS_DEV_INDEX(instance,
1840 cmd_fusion->scmd);
1841 lbinfo = &fusion->load_balance_info[device_id];
1842 if (cmd_fusion->scmd->SCp.Status &
1843 MEGASAS_LOAD_BALANCE_FLAG) {
1844 arm = lbinfo->raid1DevHandle[0] ==
1845 cmd_fusion->io_request->DevHandle ? 0 :
1846 1;
1847 atomic_dec(&lbinfo->scsi_pending_cmds[arm]);
1848 cmd_fusion->scmd->SCp.Status &=
1849 ~MEGASAS_LOAD_BALANCE_FLAG;
1850 }
1851 if (reply_descript_type ==
1852 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
1853 if (megasas_dbg_lvl == 5)
1854 printk(KERN_ERR "\nmegasas: FAST Path "
1855 "IO Success\n");
1856 }
1857 /* Fall thru and complete IO */
1858 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
1859 /* Map the FW Cmd Status */
1860 map_cmd_status(cmd_fusion, status, extStatus);
1861 scsi_dma_unmap(cmd_fusion->scmd);
1862 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
1863 scsi_io_req->RaidContext.status = 0;
1864 scsi_io_req->RaidContext.exStatus = 0;
1865 megasas_return_cmd_fusion(instance, cmd_fusion);
1866 atomic_dec(&instance->fw_outstanding);
1867
1868 break;
1869 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
1870 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1871 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1872 cmd_fusion->flags = 0;
1873 megasas_return_cmd_fusion(instance, cmd_fusion);
1874
1875 break;
1876 }
1877
adam radfordc8e858f2011-10-08 18:15:13 -07001878 fusion->last_reply_idx[MSIxIndex]++;
1879 if (fusion->last_reply_idx[MSIxIndex] >=
1880 fusion->reply_q_depth)
1881 fusion->last_reply_idx[MSIxIndex] = 0;
adam radford9c915a82010-12-21 13:34:31 -08001882
1883 desc->Words = ULLONG_MAX;
1884 num_completed++;
1885
1886 /* Get the next reply descriptor */
adam radfordc8e858f2011-10-08 18:15:13 -07001887 if (!fusion->last_reply_idx[MSIxIndex])
1888 desc = fusion->reply_frames_desc +
1889 ((MSIxIndex * fusion->reply_alloc_sz)/
1890 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
adam radford9c915a82010-12-21 13:34:31 -08001891 else
1892 desc++;
1893
1894 reply_desc =
1895 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1896
1897 d_val.word = desc->Words;
1898
1899 reply_descript_type = reply_desc->ReplyFlags &
1900 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1901
1902 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1903 break;
1904 }
1905
1906 if (!num_completed)
1907 return IRQ_NONE;
1908
1909 wmb();
adam radfordc8e858f2011-10-08 18:15:13 -07001910 writel((MSIxIndex << 24) | fusion->last_reply_idx[MSIxIndex],
adam radford9c915a82010-12-21 13:34:31 -08001911 &instance->reg_set->reply_post_host_index);
adam radford53ef2bb2011-02-24 20:56:05 -08001912 megasas_check_and_restore_queue_depth(instance);
adam radford9c915a82010-12-21 13:34:31 -08001913 return IRQ_HANDLED;
1914}
1915
1916/**
1917 * megasas_complete_cmd_dpc_fusion - Completes command
1918 * @instance: Adapter soft state
1919 *
1920 * Tasklet to complete cmds
1921 */
1922void
1923megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
1924{
1925 struct megasas_instance *instance =
1926 (struct megasas_instance *)instance_addr;
1927 unsigned long flags;
adam radfordc8e858f2011-10-08 18:15:13 -07001928 u32 count, MSIxIndex;
1929
1930 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
adam radford9c915a82010-12-21 13:34:31 -08001931
1932 /* If we have already declared adapter dead, donot complete cmds */
1933 spin_lock_irqsave(&instance->hba_lock, flags);
1934 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1935 spin_unlock_irqrestore(&instance->hba_lock, flags);
1936 return;
1937 }
1938 spin_unlock_irqrestore(&instance->hba_lock, flags);
1939
adam radfordc8e858f2011-10-08 18:15:13 -07001940 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
1941 complete_cmd_fusion(instance, MSIxIndex);
adam radford9c915a82010-12-21 13:34:31 -08001942}
1943
1944/**
1945 * megasas_isr_fusion - isr entry point
1946 */
1947irqreturn_t megasas_isr_fusion(int irq, void *devp)
1948{
adam radfordc8e858f2011-10-08 18:15:13 -07001949 struct megasas_irq_context *irq_context = devp;
1950 struct megasas_instance *instance = irq_context->instance;
adam radford9c915a82010-12-21 13:34:31 -08001951 u32 mfiStatus, fw_state;
1952
adam radfordc8e858f2011-10-08 18:15:13 -07001953 if (!instance->msix_vectors) {
adam radford9c915a82010-12-21 13:34:31 -08001954 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
1955 if (!mfiStatus)
1956 return IRQ_NONE;
1957 }
1958
1959 /* If we are resetting, bail */
adam radford6497b242011-10-08 18:14:50 -07001960 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
1961 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08001962 return IRQ_HANDLED;
adam radford6497b242011-10-08 18:14:50 -07001963 }
adam radford9c915a82010-12-21 13:34:31 -08001964
adam radfordc8e858f2011-10-08 18:15:13 -07001965 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
adam radford6497b242011-10-08 18:14:50 -07001966 instance->instancet->clear_intr(instance->reg_set);
adam radford9c915a82010-12-21 13:34:31 -08001967 /* If we didn't complete any commands, check for FW fault */
1968 fw_state = instance->instancet->read_fw_status_reg(
1969 instance->reg_set) & MFI_STATE_MASK;
1970 if (fw_state == MFI_STATE_FAULT)
1971 schedule_work(&instance->work_init);
1972 }
1973
1974 return IRQ_HANDLED;
1975}
1976
1977/**
1978 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
1979 * @instance: Adapter soft state
1980 * mfi_cmd: megasas_cmd pointer
1981 *
1982 */
1983u8
1984build_mpt_mfi_pass_thru(struct megasas_instance *instance,
1985 struct megasas_cmd *mfi_cmd)
1986{
1987 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
1988 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
1989 struct megasas_cmd_fusion *cmd;
1990 struct fusion_context *fusion;
1991 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
1992
1993 cmd = megasas_get_cmd_fusion(instance);
1994 if (!cmd)
1995 return 1;
1996
1997 /* Save the smid. To be used for returning the cmd */
1998 mfi_cmd->context.smid = cmd->index;
1999
2000 cmd->sync_cmd_idx = mfi_cmd->index;
2001
2002 /*
2003 * For cmds where the flag is set, store the flag and check
2004 * on completion. For cmds with this flag, don't call
2005 * megasas_complete_cmd
2006 */
2007
2008 if (frame_hdr->flags & MFI_FRAME_DONT_POST_IN_REPLY_QUEUE)
2009 cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
2010
2011 fusion = instance->ctrl_context;
2012 io_req = cmd->io_request;
adam radford36807e62011-10-08 18:15:06 -07002013
Sumit.Saxena@lsi.com21d3c712013-05-22 12:31:43 +05302014 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2015 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
adam radford36807e62011-10-08 18:15:06 -07002016 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
2017 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
2018 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2019 sgl_ptr_end->Flags = 0;
2020 }
2021
adam radford9c915a82010-12-21 13:34:31 -08002022 mpi25_ieee_chain =
2023 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
2024
2025 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
2026 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
2027 SGL) / 4;
2028 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
2029
2030 mpi25_ieee_chain->Address = mfi_cmd->frame_phys_addr;
2031
2032 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2033 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
2034
2035 mpi25_ieee_chain->Length = MEGASAS_MAX_SZ_CHAIN_FRAME;
2036
2037 return 0;
2038}
2039
2040/**
2041 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
2042 * @instance: Adapter soft state
2043 * @cmd: mfi cmd to build
2044 *
2045 */
2046union MEGASAS_REQUEST_DESCRIPTOR_UNION *
2047build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
2048{
2049 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2050 u16 index;
2051
2052 if (build_mpt_mfi_pass_thru(instance, cmd)) {
2053 printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
2054 return NULL;
2055 }
2056
2057 index = cmd->context.smid;
2058
2059 req_desc = megasas_get_request_descriptor(instance, index - 1);
2060
2061 if (!req_desc)
2062 return NULL;
2063
2064 req_desc->Words = 0;
2065 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2066 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2067
2068 req_desc->SCSIIO.SMID = index;
2069
2070 return req_desc;
2071}
2072
2073/**
2074 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
2075 * @instance: Adapter soft state
2076 * @cmd: mfi cmd pointer
2077 *
2078 */
2079void
2080megasas_issue_dcmd_fusion(struct megasas_instance *instance,
2081 struct megasas_cmd *cmd)
2082{
2083 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
adam radford9c915a82010-12-21 13:34:31 -08002084
2085 req_desc = build_mpt_cmd(instance, cmd);
2086 if (!req_desc) {
2087 printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
2088 return;
2089 }
adam radford9c915a82010-12-21 13:34:31 -08002090 instance->instancet->fire_cmd(instance, req_desc->u.low,
2091 req_desc->u.high, instance->reg_set);
2092}
2093
2094/**
2095 * megasas_release_fusion - Reverses the FW initialization
2096 * @intance: Adapter soft state
2097 */
2098void
2099megasas_release_fusion(struct megasas_instance *instance)
2100{
2101 megasas_free_cmds(instance);
2102 megasas_free_cmds_fusion(instance);
2103
2104 iounmap(instance->reg_set);
2105
2106 pci_release_selected_regions(instance->pdev, instance->bar);
2107}
2108
2109/**
2110 * megasas_read_fw_status_reg_fusion - returns the current FW status value
2111 * @regs: MFI register set
2112 */
2113static u32
2114megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
2115{
2116 return readl(&(regs)->outbound_scratch_pad);
2117}
2118
2119/**
2120 * megasas_adp_reset_fusion - For controller reset
2121 * @regs: MFI register set
2122 */
2123static int
2124megasas_adp_reset_fusion(struct megasas_instance *instance,
2125 struct megasas_register_set __iomem *regs)
2126{
2127 return 0;
2128}
2129
2130/**
2131 * megasas_check_reset_fusion - For controller reset check
2132 * @regs: MFI register set
2133 */
2134static int
2135megasas_check_reset_fusion(struct megasas_instance *instance,
2136 struct megasas_register_set __iomem *regs)
2137{
2138 return 0;
2139}
2140
2141/* This function waits for outstanding commands on fusion to complete */
2142int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance)
2143{
2144 int i, outstanding, retval = 0;
adam radfordc007b8b2012-07-17 18:20:24 -07002145 u32 fw_state;
adam radford9c915a82010-12-21 13:34:31 -08002146
adam radfordc007b8b2012-07-17 18:20:24 -07002147 for (i = 0; i < resetwaittime; i++) {
adam radford9c915a82010-12-21 13:34:31 -08002148 /* Check if firmware is in fault state */
2149 fw_state = instance->instancet->read_fw_status_reg(
2150 instance->reg_set) & MFI_STATE_MASK;
2151 if (fw_state == MFI_STATE_FAULT) {
2152 printk(KERN_WARNING "megasas: Found FW in FAULT state,"
2153 " will reset adapter.\n");
2154 retval = 1;
2155 goto out;
2156 }
2157
2158 outstanding = atomic_read(&instance->fw_outstanding);
2159 if (!outstanding)
2160 goto out;
2161
2162 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2163 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
2164 "commands to complete\n", i, outstanding);
2165 megasas_complete_cmd_dpc_fusion(
2166 (unsigned long)instance);
2167 }
2168 msleep(1000);
2169 }
2170
2171 if (atomic_read(&instance->fw_outstanding)) {
2172 printk("megaraid_sas: pending commands remain after waiting, "
2173 "will reset adapter.\n");
2174 retval = 1;
2175 }
2176out:
2177 return retval;
2178}
2179
2180void megasas_reset_reply_desc(struct megasas_instance *instance)
2181{
adam radfordc8e858f2011-10-08 18:15:13 -07002182 int i, count;
adam radford9c915a82010-12-21 13:34:31 -08002183 struct fusion_context *fusion;
2184 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2185
2186 fusion = instance->ctrl_context;
adam radfordc8e858f2011-10-08 18:15:13 -07002187 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2188 for (i = 0 ; i < count ; i++)
2189 fusion->last_reply_idx[i] = 0;
adam radford9c915a82010-12-21 13:34:31 -08002190 reply_desc = fusion->reply_frames_desc;
adam radfordc8e858f2011-10-08 18:15:13 -07002191 for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
adam radford9c915a82010-12-21 13:34:31 -08002192 reply_desc->Words = ULLONG_MAX;
2193}
2194
2195/* Core fusion reset function */
2196int megasas_reset_fusion(struct Scsi_Host *shost)
2197{
2198 int retval = SUCCESS, i, j, retry = 0;
2199 struct megasas_instance *instance;
2200 struct megasas_cmd_fusion *cmd_fusion;
2201 struct fusion_context *fusion;
2202 struct megasas_cmd *cmd_mfi;
2203 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
adam radford7e70e732011-05-11 18:34:08 -07002204 u32 host_diag, abs_state, status_reg, reset_adapter;
adam radford9c915a82010-12-21 13:34:31 -08002205
2206 instance = (struct megasas_instance *)shost->hostdata;
2207 fusion = instance->ctrl_context;
2208
adam radford9c915a82010-12-21 13:34:31 -08002209 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2210 printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
2211 "returning FAILED.\n");
adam radfordd4a759a2011-10-08 18:14:39 -07002212 return FAILED;
adam radford9c915a82010-12-21 13:34:31 -08002213 }
2214
adam radford7e70e732011-05-11 18:34:08 -07002215 mutex_lock(&instance->reset_mutex);
2216 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2217 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2218 instance->instancet->disable_intr(instance->reg_set);
2219 msleep(1000);
2220
adam radford9c915a82010-12-21 13:34:31 -08002221 /* First try waiting for commands to complete */
2222 if (megasas_wait_for_outstanding_fusion(instance)) {
2223 printk(KERN_WARNING "megaraid_sas: resetting fusion "
2224 "adapter.\n");
2225 /* Now return commands back to the OS */
2226 for (i = 0 ; i < instance->max_fw_cmds; i++) {
2227 cmd_fusion = fusion->cmd_list[i];
2228 if (cmd_fusion->scmd) {
2229 scsi_dma_unmap(cmd_fusion->scmd);
2230 cmd_fusion->scmd->result = (DID_RESET << 16);
2231 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2232 megasas_return_cmd_fusion(instance, cmd_fusion);
2233 atomic_dec(&instance->fw_outstanding);
2234 }
2235 }
2236
adam radford7e70e732011-05-11 18:34:08 -07002237 status_reg = instance->instancet->read_fw_status_reg(
2238 instance->reg_set);
2239 abs_state = status_reg & MFI_STATE_MASK;
2240 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2241 if (instance->disableOnlineCtrlReset ||
2242 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
adam radford9c915a82010-12-21 13:34:31 -08002243 /* Reset not supported, kill adapter */
2244 printk(KERN_WARNING "megaraid_sas: Reset not supported"
2245 ", killing adapter.\n");
2246 megaraid_sas_kill_hba(instance);
2247 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2248 retval = FAILED;
2249 goto out;
2250 }
2251
2252 /* Now try to reset the chip */
2253 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2254 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2255 &instance->reg_set->fusion_seq_offset);
2256 writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2257 &instance->reg_set->fusion_seq_offset);
2258 writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2259 &instance->reg_set->fusion_seq_offset);
2260 writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2261 &instance->reg_set->fusion_seq_offset);
2262 writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2263 &instance->reg_set->fusion_seq_offset);
2264 writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2265 &instance->reg_set->fusion_seq_offset);
2266 writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2267 &instance->reg_set->fusion_seq_offset);
2268
2269 /* Check that the diag write enable (DRWE) bit is on */
2270 host_diag = readl(&instance->reg_set->fusion_host_diag);
adam radford7e70e732011-05-11 18:34:08 -07002271 retry = 0;
adam radford9c915a82010-12-21 13:34:31 -08002272 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2273 msleep(100);
2274 host_diag =
2275 readl(&instance->reg_set->fusion_host_diag);
2276 if (retry++ == 100) {
2277 printk(KERN_WARNING "megaraid_sas: "
2278 "Host diag unlock failed!\n");
2279 break;
2280 }
2281 }
2282 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2283 continue;
2284
2285 /* Send chip reset command */
2286 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2287 &instance->reg_set->fusion_host_diag);
2288 msleep(3000);
2289
2290 /* Make sure reset adapter bit is cleared */
2291 host_diag = readl(&instance->reg_set->fusion_host_diag);
2292 retry = 0;
2293 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2294 msleep(100);
2295 host_diag =
2296 readl(&instance->reg_set->fusion_host_diag);
2297 if (retry++ == 1000) {
2298 printk(KERN_WARNING "megaraid_sas: "
2299 "Diag reset adapter never "
2300 "cleared!\n");
2301 break;
2302 }
2303 }
2304 if (host_diag & HOST_DIAG_RESET_ADAPTER)
2305 continue;
2306
2307 abs_state =
2308 instance->instancet->read_fw_status_reg(
adam radford7e70e732011-05-11 18:34:08 -07002309 instance->reg_set) & MFI_STATE_MASK;
adam radford9c915a82010-12-21 13:34:31 -08002310 retry = 0;
2311
2312 while ((abs_state <= MFI_STATE_FW_INIT) &&
2313 (retry++ < 1000)) {
2314 msleep(100);
2315 abs_state =
2316 instance->instancet->read_fw_status_reg(
adam radford7e70e732011-05-11 18:34:08 -07002317 instance->reg_set) & MFI_STATE_MASK;
adam radford9c915a82010-12-21 13:34:31 -08002318 }
2319 if (abs_state <= MFI_STATE_FW_INIT) {
2320 printk(KERN_WARNING "megaraid_sas: firmware "
2321 "state < MFI_STATE_FW_INIT, state = "
2322 "0x%x\n", abs_state);
2323 continue;
2324 }
2325
2326 /* Wait for FW to become ready */
adam radford058a8fa2011-10-08 18:14:27 -07002327 if (megasas_transition_to_ready(instance, 1)) {
adam radford9c915a82010-12-21 13:34:31 -08002328 printk(KERN_WARNING "megaraid_sas: Failed to "
2329 "transition controller to ready.\n");
2330 continue;
2331 }
2332
2333 megasas_reset_reply_desc(instance);
2334 if (megasas_ioc_init_fusion(instance)) {
2335 printk(KERN_WARNING "megaraid_sas: "
2336 "megasas_ioc_init_fusion() failed!\n");
2337 continue;
2338 }
2339
adam radford3f0e58bc2011-10-08 18:14:59 -07002340 clear_bit(MEGASAS_FUSION_IN_RESET,
2341 &instance->reset_flags);
adam radford9c915a82010-12-21 13:34:31 -08002342 instance->instancet->enable_intr(instance->reg_set);
2343 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2344
2345 /* Re-fire management commands */
2346 for (j = 0 ; j < instance->max_fw_cmds; j++) {
2347 cmd_fusion = fusion->cmd_list[j];
2348 if (cmd_fusion->sync_cmd_idx !=
2349 (u32)ULONG_MAX) {
2350 cmd_mfi =
2351 instance->
2352 cmd_list[cmd_fusion->sync_cmd_idx];
2353 if (cmd_mfi->frame->dcmd.opcode ==
2354 MR_DCMD_LD_MAP_GET_INFO) {
2355 megasas_return_cmd(instance,
2356 cmd_mfi);
2357 megasas_return_cmd_fusion(
2358 instance, cmd_fusion);
2359 } else {
2360 req_desc =
2361 megasas_get_request_descriptor(
2362 instance,
2363 cmd_mfi->context.smid
2364 -1);
2365 if (!req_desc)
2366 printk(KERN_WARNING
2367 "req_desc NULL"
2368 "\n");
2369 else {
2370 instance->instancet->
2371 fire_cmd(instance,
2372 req_desc->
2373 u.low,
2374 req_desc->
2375 u.high,
2376 instance->
2377 reg_set);
2378 }
2379 }
2380 }
2381 }
2382
2383 /* Reset load balance info */
2384 memset(fusion->load_balance_info, 0,
2385 sizeof(struct LD_LOAD_BALANCE_INFO)
2386 *MAX_LOGICAL_DRIVES);
2387
2388 if (!megasas_get_map_info(instance))
2389 megasas_sync_map_info(instance);
2390
2391 /* Adapter reset completed successfully */
2392 printk(KERN_WARNING "megaraid_sas: Reset "
2393 "successful.\n");
2394 retval = SUCCESS;
2395 goto out;
2396 }
2397 /* Reset failed, kill the adapter */
2398 printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
2399 "adapter.\n");
2400 megaraid_sas_kill_hba(instance);
2401 retval = FAILED;
2402 } else {
adam radford3f0e58bc2011-10-08 18:14:59 -07002403 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
adam radford9c915a82010-12-21 13:34:31 -08002404 instance->instancet->enable_intr(instance->reg_set);
2405 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2406 }
2407out:
2408 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2409 mutex_unlock(&instance->reset_mutex);
2410 return retval;
2411}
2412
2413/* Fusion OCR work queue */
2414void megasas_fusion_ocr_wq(struct work_struct *work)
2415{
2416 struct megasas_instance *instance =
2417 container_of(work, struct megasas_instance, work_init);
2418
2419 megasas_reset_fusion(instance->host);
2420}
2421
2422struct megasas_instance_template megasas_instance_template_fusion = {
2423 .fire_cmd = megasas_fire_cmd_fusion,
2424 .enable_intr = megasas_enable_intr_fusion,
2425 .disable_intr = megasas_disable_intr_fusion,
2426 .clear_intr = megasas_clear_intr_fusion,
2427 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2428 .adp_reset = megasas_adp_reset_fusion,
2429 .check_reset = megasas_check_reset_fusion,
2430 .service_isr = megasas_isr_fusion,
2431 .tasklet = megasas_complete_cmd_dpc_fusion,
2432 .init_adapter = megasas_init_adapter_fusion,
2433 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2434 .issue_dcmd = megasas_issue_dcmd_fusion,
2435};