blob: f13e7abd345a9b3956243fc5e047207f66d0dbe8 [file] [log] [blame]
adam radford9c915a82010-12-21 13:34:31 -08001/*
2 * Linux MegaRAID driver for SAS based RAID controllers
3 *
4 * Copyright (c) 2009-2011 LSI Corporation.
5 *
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
77MR_BuildRaidContext(struct IO_REQUEST_INFO *io_info,
78 struct RAID_CONTEXT *pRAID_Context,
79 struct MR_FW_RAID_MAP_ALL *map);
80u16 MR_TargetIdToLdGet(u32 ldTgtId, struct MR_FW_RAID_MAP_ALL *map);
81struct MR_LD_RAID *MR_LdRaidGet(u32 ld, struct MR_FW_RAID_MAP_ALL *map);
82
83u16 MR_GetLDTgtId(u32 ld, struct MR_FW_RAID_MAP_ALL *map);
adam radford53ef2bb2011-02-24 20:56:05 -080084
85void
86megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
87
adam radford9c915a82010-12-21 13:34:31 -080088u8 MR_ValidateMapInfo(struct MR_FW_RAID_MAP_ALL *map,
89 struct LD_LOAD_BALANCE_INFO *lbInfo);
90u16 get_updated_dev_handle(struct LD_LOAD_BALANCE_INFO *lbInfo,
91 struct IO_REQUEST_INFO *in_info);
92int megasas_transition_to_ready(struct megasas_instance *instance);
93void megaraid_sas_kill_hba(struct megasas_instance *instance);
94
95extern u32 megasas_dbg_lvl;
96
97/**
98 * megasas_enable_intr_fusion - Enables interrupts
99 * @regs: MFI register set
100 */
101void
102megasas_enable_intr_fusion(struct megasas_register_set __iomem *regs)
103{
104 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
105
106 /* Dummy readl to force pci flush */
107 readl(&regs->outbound_intr_mask);
108}
109
110/**
111 * megasas_disable_intr_fusion - Disables interrupt
112 * @regs: MFI register set
113 */
114void
115megasas_disable_intr_fusion(struct megasas_register_set __iomem *regs)
116{
117 u32 mask = 0xFFFFFFFF;
118 u32 status;
119
120 writel(mask, &regs->outbound_intr_mask);
121 /* Dummy readl to force pci flush */
122 status = readl(&regs->outbound_intr_mask);
123}
124
125int
126megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
127{
128 u32 status;
129 /*
130 * Check if it is our interrupt
131 */
132 status = readl(&regs->outbound_intr_status);
133
134 if (status & 1) {
135 writel(status, &regs->outbound_intr_status);
136 readl(&regs->outbound_intr_status);
137 return 1;
138 }
139 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
140 return 0;
141
142 /*
143 * dummy read to flush PCI
144 */
145 readl(&regs->outbound_intr_status);
146
147 return 1;
148}
149
150/**
151 * megasas_get_cmd_fusion - Get a command from the free pool
152 * @instance: Adapter soft state
153 *
154 * Returns a free command from the pool
155 */
156struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
157 *instance)
158{
159 unsigned long flags;
160 struct fusion_context *fusion =
161 (struct fusion_context *)instance->ctrl_context;
162 struct megasas_cmd_fusion *cmd = NULL;
163
164 spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
165
166 if (!list_empty(&fusion->cmd_pool)) {
167 cmd = list_entry((&fusion->cmd_pool)->next,
168 struct megasas_cmd_fusion, list);
169 list_del_init(&cmd->list);
170 } else {
171 printk(KERN_ERR "megasas: Command pool (fusion) empty!\n");
172 }
173
174 spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
175 return cmd;
176}
177
178/**
179 * megasas_return_cmd_fusion - Return a cmd to free command pool
180 * @instance: Adapter soft state
181 * @cmd: Command packet to be returned to free command pool
182 */
183static inline void
184megasas_return_cmd_fusion(struct megasas_instance *instance,
185 struct megasas_cmd_fusion *cmd)
186{
187 unsigned long flags;
188 struct fusion_context *fusion =
189 (struct fusion_context *)instance->ctrl_context;
190
191 spin_lock_irqsave(&fusion->cmd_pool_lock, flags);
192
193 cmd->scmd = NULL;
194 cmd->sync_cmd_idx = (u32)ULONG_MAX;
195 list_add_tail(&cmd->list, &fusion->cmd_pool);
196
197 spin_unlock_irqrestore(&fusion->cmd_pool_lock, flags);
198}
199
200/**
201 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
202 * @instance: Adapter soft state
203 */
204static void megasas_teardown_frame_pool_fusion(
205 struct megasas_instance *instance)
206{
207 int i;
208 struct fusion_context *fusion = instance->ctrl_context;
209
210 u16 max_cmd = instance->max_fw_cmds;
211
212 struct megasas_cmd_fusion *cmd;
213
214 if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
215 printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
216 "sense pool : %p\n", fusion->sg_dma_pool,
217 fusion->sense_dma_pool);
218 return;
219 }
220
221 /*
222 * Return all frames to pool
223 */
224 for (i = 0; i < max_cmd; i++) {
225
226 cmd = fusion->cmd_list[i];
227
228 if (cmd->sg_frame)
229 pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
230 cmd->sg_frame_phys_addr);
231
232 if (cmd->sense)
233 pci_pool_free(fusion->sense_dma_pool, cmd->sense,
234 cmd->sense_phys_addr);
235 }
236
237 /*
238 * Now destroy the pool itself
239 */
240 pci_pool_destroy(fusion->sg_dma_pool);
241 pci_pool_destroy(fusion->sense_dma_pool);
242
243 fusion->sg_dma_pool = NULL;
244 fusion->sense_dma_pool = NULL;
245}
246
247/**
248 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
249 * @instance: Adapter soft state
250 */
251void
252megasas_free_cmds_fusion(struct megasas_instance *instance)
253{
254 int i;
255 struct fusion_context *fusion = instance->ctrl_context;
256
257 u32 max_cmds, req_sz, reply_sz, io_frames_sz;
258
259
260 req_sz = fusion->request_alloc_sz;
261 reply_sz = fusion->reply_alloc_sz;
262 io_frames_sz = fusion->io_frames_alloc_sz;
263
264 max_cmds = instance->max_fw_cmds;
265
266 /* Free descriptors and request Frames memory */
267 if (fusion->req_frames_desc)
268 dma_free_coherent(&instance->pdev->dev, req_sz,
269 fusion->req_frames_desc,
270 fusion->req_frames_desc_phys);
271
272 if (fusion->reply_frames_desc) {
273 pci_pool_free(fusion->reply_frames_desc_pool,
274 fusion->reply_frames_desc,
275 fusion->reply_frames_desc_phys);
276 pci_pool_destroy(fusion->reply_frames_desc_pool);
277 }
278
279 if (fusion->io_request_frames) {
280 pci_pool_free(fusion->io_request_frames_pool,
281 fusion->io_request_frames,
282 fusion->io_request_frames_phys);
283 pci_pool_destroy(fusion->io_request_frames_pool);
284 }
285
286 /* Free the Fusion frame pool */
287 megasas_teardown_frame_pool_fusion(instance);
288
289 /* Free all the commands in the cmd_list */
290 for (i = 0; i < max_cmds; i++)
291 kfree(fusion->cmd_list[i]);
292
293 /* Free the cmd_list buffer itself */
294 kfree(fusion->cmd_list);
295 fusion->cmd_list = NULL;
296
297 INIT_LIST_HEAD(&fusion->cmd_pool);
298}
299
300/**
301 * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
302 * @instance: Adapter soft state
303 *
304 */
305static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
306{
307 int i;
308 u32 max_cmd;
309 struct fusion_context *fusion;
310 struct megasas_cmd_fusion *cmd;
311 u32 total_sz_chain_frame;
312
313 fusion = instance->ctrl_context;
314 max_cmd = instance->max_fw_cmds;
315
316 total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
317
318 /*
319 * Use DMA pool facility provided by PCI layer
320 */
321
322 fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
323 instance->pdev,
324 total_sz_chain_frame, 4,
325 0);
326 if (!fusion->sg_dma_pool) {
327 printk(KERN_DEBUG "megasas: failed to setup request pool "
328 "fusion\n");
329 return -ENOMEM;
330 }
331 fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
332 instance->pdev,
333 SCSI_SENSE_BUFFERSIZE, 64, 0);
334
335 if (!fusion->sense_dma_pool) {
336 printk(KERN_DEBUG "megasas: failed to setup sense pool "
337 "fusion\n");
338 pci_pool_destroy(fusion->sg_dma_pool);
339 fusion->sg_dma_pool = NULL;
340 return -ENOMEM;
341 }
342
343 /*
344 * Allocate and attach a frame to each of the commands in cmd_list
345 */
346 for (i = 0; i < max_cmd; i++) {
347
348 cmd = fusion->cmd_list[i];
349
350 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
351 GFP_KERNEL,
352 &cmd->sg_frame_phys_addr);
353
354 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
355 GFP_KERNEL, &cmd->sense_phys_addr);
356 /*
357 * megasas_teardown_frame_pool_fusion() takes care of freeing
358 * whatever has been allocated
359 */
360 if (!cmd->sg_frame || !cmd->sense) {
361 printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
362 megasas_teardown_frame_pool_fusion(instance);
363 return -ENOMEM;
364 }
365 }
366 return 0;
367}
368
369/**
370 * megasas_alloc_cmds_fusion - Allocates the command packets
371 * @instance: Adapter soft state
372 *
373 *
374 * Each frame has a 32-bit field called context. This context is used to get
375 * back the megasas_cmd_fusion from the frame when a frame gets completed
376 * In this driver, the 32 bit values are the indices into an array cmd_list.
377 * This array is used only to look up the megasas_cmd_fusion given the context.
378 * The free commands themselves are maintained in a linked list called cmd_pool.
379 *
380 * cmds are formed in the io_request and sg_frame members of the
381 * megasas_cmd_fusion. The context field is used to get a request descriptor
382 * and is used as SMID of the cmd.
383 * SMID value range is from 1 to max_fw_cmds.
384 */
385int
386megasas_alloc_cmds_fusion(struct megasas_instance *instance)
387{
388 int i, j;
389 u32 max_cmd, io_frames_sz;
390 struct fusion_context *fusion;
391 struct megasas_cmd_fusion *cmd;
392 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
393 u32 offset;
394 dma_addr_t io_req_base_phys;
395 u8 *io_req_base;
396
397 fusion = instance->ctrl_context;
398
399 max_cmd = instance->max_fw_cmds;
400
401 fusion->req_frames_desc =
402 dma_alloc_coherent(&instance->pdev->dev,
403 fusion->request_alloc_sz,
404 &fusion->req_frames_desc_phys, GFP_KERNEL);
405
406 if (!fusion->req_frames_desc) {
407 printk(KERN_ERR "megasas; Could not allocate memory for "
408 "request_frames\n");
409 goto fail_req_desc;
410 }
411
412 fusion->reply_frames_desc_pool =
413 pci_pool_create("reply_frames pool", instance->pdev,
414 fusion->reply_alloc_sz, 16, 0);
415
416 if (!fusion->reply_frames_desc_pool) {
417 printk(KERN_ERR "megasas; Could not allocate memory for "
418 "reply_frame pool\n");
419 goto fail_reply_desc;
420 }
421
422 fusion->reply_frames_desc =
423 pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
424 &fusion->reply_frames_desc_phys);
425 if (!fusion->reply_frames_desc) {
426 printk(KERN_ERR "megasas; Could not allocate memory for "
427 "reply_frame pool\n");
428 pci_pool_destroy(fusion->reply_frames_desc_pool);
429 goto fail_reply_desc;
430 }
431
432 reply_desc = fusion->reply_frames_desc;
433 for (i = 0; i < fusion->reply_q_depth; i++, reply_desc++)
434 reply_desc->Words = ULLONG_MAX;
435
436 io_frames_sz = fusion->io_frames_alloc_sz;
437
438 fusion->io_request_frames_pool =
439 pci_pool_create("io_request_frames pool", instance->pdev,
440 fusion->io_frames_alloc_sz, 16, 0);
441
442 if (!fusion->io_request_frames_pool) {
443 printk(KERN_ERR "megasas: Could not allocate memory for "
444 "io_request_frame pool\n");
445 goto fail_io_frames;
446 }
447
448 fusion->io_request_frames =
449 pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
450 &fusion->io_request_frames_phys);
451 if (!fusion->io_request_frames) {
452 printk(KERN_ERR "megasas: Could not allocate memory for "
453 "io_request_frames frames\n");
454 pci_pool_destroy(fusion->io_request_frames_pool);
455 goto fail_io_frames;
456 }
457
458 /*
459 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
460 * Allocate the dynamic array first and then allocate individual
461 * commands.
462 */
463 fusion->cmd_list = kmalloc(sizeof(struct megasas_cmd_fusion *)
464 *max_cmd, GFP_KERNEL);
465
466 if (!fusion->cmd_list) {
467 printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
468 "memory for cmd_list_fusion\n");
469 goto fail_cmd_list;
470 }
471
472 memset(fusion->cmd_list, 0, sizeof(struct megasas_cmd_fusion *)
473 *max_cmd);
474
475 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
560wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd)
561{
562 int i;
563 struct megasas_header *frame_hdr = &cmd->frame->hdr;
564
565 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000;
566
567 /*
568 * Wait for cmd_status to change
569 */
570 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
571 rmb();
572 msleep(20);
573 }
574
575 if (frame_hdr->cmd_status == 0xff)
576 return -ETIME;
577
578 return 0;
579}
580
581/**
582 * megasas_ioc_init_fusion - Initializes the FW
583 * @instance: Adapter soft state
584 *
585 * Issues the IOC Init cmd
586 */
587int
588megasas_ioc_init_fusion(struct megasas_instance *instance)
589{
590 struct megasas_init_frame *init_frame;
591 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
592 dma_addr_t ioc_init_handle;
593 u32 context;
594 struct megasas_cmd *cmd;
595 u8 ret;
596 struct fusion_context *fusion;
597 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
598 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;
627 IOCInitMessage->MsgVersion = MPI2_VERSION;
628 IOCInitMessage->HeaderVersion = MPI2_HEADER_VERSION;
629 IOCInitMessage->SystemRequestFrameSize =
630 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4;
631
632 IOCInitMessage->ReplyDescriptorPostQueueDepth = fusion->reply_q_depth;
633 IOCInitMessage->ReplyDescriptorPostQueueAddress =
634 fusion->reply_frames_desc_phys;
635 IOCInitMessage->SystemRequestFrameBaseAddress =
636 fusion->io_request_frames_phys;
637
638 init_frame = (struct megasas_init_frame *)cmd->frame;
639 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
640
641 frame_hdr = &cmd->frame->hdr;
642 context = init_frame->context;
643 init_frame->context = context;
644
645 frame_hdr->cmd_status = 0xFF;
646 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
647
648 init_frame->cmd = MFI_CMD_INIT;
649 init_frame->cmd_status = 0xFF;
650
651 init_frame->queue_info_new_phys_addr_lo = ioc_init_handle;
652 init_frame->data_xfer_len = sizeof(struct MPI2_IOC_INIT_REQUEST);
653
654 req_desc =
655 (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)fusion->req_frames_desc;
656
657 req_desc->Words = cmd->frame_phys_addr;
658 req_desc->MFAIo.RequestFlags =
659 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
660 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
661
662 /*
663 * disable the intr before firing the init frame
664 */
665 instance->instancet->disable_intr(instance->reg_set);
666
667 for (i = 0; i < (10 * 1000); i += 20) {
668 if (readl(&instance->reg_set->doorbell) & 1)
669 msleep(20);
670 else
671 break;
672 }
673
674 instance->instancet->fire_cmd(instance, req_desc->u.low,
675 req_desc->u.high, instance->reg_set);
676
677 wait_and_poll(instance, cmd);
678
679 frame_hdr = &cmd->frame->hdr;
680 if (frame_hdr->cmd_status != 0) {
681 ret = 1;
682 goto fail_fw_init;
683 }
684 printk(KERN_ERR "megasas:IOC Init cmd success\n");
685
686 ret = 0;
687
688fail_fw_init:
689 megasas_return_cmd(instance, cmd);
690 if (IOCInitMessage)
691 dma_free_coherent(&instance->pdev->dev,
692 sizeof(struct MPI2_IOC_INIT_REQUEST),
693 IOCInitMessage, ioc_init_handle);
694fail_get_cmd:
695 return ret;
696}
697
698/*
adam radford9c915a82010-12-21 13:34:31 -0800699 * megasas_get_ld_map_info - Returns FW's ld_map structure
700 * @instance: Adapter soft state
701 * @pend: Pend the command or not
702 * Issues an internal command (DCMD) to get the FW's controller PD
703 * list structure. This information is mainly used to find out SYSTEM
704 * supported by the FW.
705 */
706static int
707megasas_get_ld_map_info(struct megasas_instance *instance)
708{
709 int ret = 0;
710 struct megasas_cmd *cmd;
711 struct megasas_dcmd_frame *dcmd;
712 struct MR_FW_RAID_MAP_ALL *ci;
713 dma_addr_t ci_h = 0;
714 u32 size_map_info;
715 struct fusion_context *fusion;
716
717 cmd = megasas_get_cmd(instance);
718
719 if (!cmd) {
720 printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
721 return -ENOMEM;
722 }
723
724 fusion = instance->ctrl_context;
725
726 if (!fusion) {
727 megasas_return_cmd(instance, cmd);
728 return 1;
729 }
730
731 dcmd = &cmd->frame->dcmd;
732
733 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
734 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
735
736 ci = fusion->ld_map[(instance->map_id & 1)];
737 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
738
739 if (!ci) {
740 printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
741 megasas_return_cmd(instance, cmd);
742 return -ENOMEM;
743 }
744
745 memset(ci, 0, sizeof(*ci));
746 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
747
748 dcmd->cmd = MFI_CMD_DCMD;
749 dcmd->cmd_status = 0xFF;
750 dcmd->sge_count = 1;
751 dcmd->flags = MFI_FRAME_DIR_READ;
752 dcmd->timeout = 0;
753 dcmd->pad_0 = 0;
754 dcmd->data_xfer_len = size_map_info;
755 dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
756 dcmd->sgl.sge32[0].phys_addr = ci_h;
757 dcmd->sgl.sge32[0].length = size_map_info;
758
759 if (!megasas_issue_polled(instance, cmd))
760 ret = 0;
761 else {
762 printk(KERN_ERR "megasas: Get LD Map Info Failed\n");
763 ret = -1;
764 }
765
766 megasas_return_cmd(instance, cmd);
767
768 return ret;
769}
770
771u8
772megasas_get_map_info(struct megasas_instance *instance)
773{
774 struct fusion_context *fusion = instance->ctrl_context;
775
776 fusion->fast_path_io = 0;
777 if (!megasas_get_ld_map_info(instance)) {
778 if (MR_ValidateMapInfo(fusion->ld_map[(instance->map_id & 1)],
779 fusion->load_balance_info)) {
780 fusion->fast_path_io = 1;
781 return 0;
782 }
783 }
784 return 1;
785}
786
787/*
788 * megasas_sync_map_info - Returns FW's ld_map structure
789 * @instance: Adapter soft state
790 *
791 * Issues an internal command (DCMD) to get the FW's controller PD
792 * list structure. This information is mainly used to find out SYSTEM
793 * supported by the FW.
794 */
795int
796megasas_sync_map_info(struct megasas_instance *instance)
797{
798 int ret = 0, i;
799 struct megasas_cmd *cmd;
800 struct megasas_dcmd_frame *dcmd;
801 u32 size_sync_info, num_lds;
802 struct fusion_context *fusion;
803 struct MR_LD_TARGET_SYNC *ci = NULL;
804 struct MR_FW_RAID_MAP_ALL *map;
805 struct MR_LD_RAID *raid;
806 struct MR_LD_TARGET_SYNC *ld_sync;
807 dma_addr_t ci_h = 0;
808 u32 size_map_info;
809
810 cmd = megasas_get_cmd(instance);
811
812 if (!cmd) {
813 printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
814 "info.\n");
815 return -ENOMEM;
816 }
817
818 fusion = instance->ctrl_context;
819
820 if (!fusion) {
821 megasas_return_cmd(instance, cmd);
822 return 1;
823 }
824
825 map = fusion->ld_map[instance->map_id & 1];
826
827 num_lds = map->raidMap.ldCount;
828
829 dcmd = &cmd->frame->dcmd;
830
831 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
832
833 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
834
835 ci = (struct MR_LD_TARGET_SYNC *)
836 fusion->ld_map[(instance->map_id - 1) & 1];
837 memset(ci, 0, sizeof(struct MR_FW_RAID_MAP_ALL));
838
839 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
840
841 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
842
843 for (i = 0; i < num_lds; i++, ld_sync++) {
844 raid = MR_LdRaidGet(i, map);
845 ld_sync->targetId = MR_GetLDTgtId(i, map);
846 ld_sync->seqNum = raid->seqNum;
847 }
848
849 size_map_info = sizeof(struct MR_FW_RAID_MAP) +
850 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
851
852 dcmd->cmd = MFI_CMD_DCMD;
853 dcmd->cmd_status = 0xFF;
854 dcmd->sge_count = 1;
855 dcmd->flags = MFI_FRAME_DIR_WRITE;
856 dcmd->timeout = 0;
857 dcmd->pad_0 = 0;
858 dcmd->data_xfer_len = size_map_info;
859 dcmd->mbox.b[0] = num_lds;
860 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
861 dcmd->opcode = MR_DCMD_LD_MAP_GET_INFO;
862 dcmd->sgl.sge32[0].phys_addr = ci_h;
863 dcmd->sgl.sge32[0].length = size_map_info;
864
865 instance->map_update_cmd = cmd;
866
867 instance->instancet->issue_dcmd(instance, cmd);
868
869 return ret;
870}
871
872/**
873 * megasas_init_adapter_fusion - Initializes the FW
874 * @instance: Adapter soft state
875 *
876 * This is the main function for initializing firmware.
877 */
878u32
879megasas_init_adapter_fusion(struct megasas_instance *instance)
880{
881 struct megasas_register_set __iomem *reg_set;
882 struct fusion_context *fusion;
883 u32 max_cmd;
884 int i = 0;
885
886 fusion = instance->ctrl_context;
887
888 reg_set = instance->reg_set;
889
890 /*
891 * Get various operational parameters from status register
892 */
893 instance->max_fw_cmds =
894 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
895 instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
896
897 /*
898 * Reduce the max supported cmds by 1. This is to ensure that the
899 * reply_q_sz (1 more than the max cmd that driver may send)
900 * does not exceed max cmds that the FW can support
901 */
902 instance->max_fw_cmds = instance->max_fw_cmds-1;
903 /* Only internal cmds (DCMD) need to have MFI frames */
904 instance->max_mfi_cmds = MEGASAS_INT_CMDS;
905
906 max_cmd = instance->max_fw_cmds;
907
908 fusion->reply_q_depth = ((max_cmd + 1 + 15)/16)*16;
909
910 fusion->request_alloc_sz =
911 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
912 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
913 *(fusion->reply_q_depth);
914 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
915 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
916 (max_cmd + 1)); /* Extra 1 for SMID 0 */
917
918 fusion->max_sge_in_main_msg =
919 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
920 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
921
922 fusion->max_sge_in_chain =
923 MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
924
925 instance->max_num_sge = fusion->max_sge_in_main_msg +
926 fusion->max_sge_in_chain - 2;
927
928 /* Used for pass thru MFI frame (DCMD) */
929 fusion->chain_offset_mfi_pthru =
930 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
931
932 fusion->chain_offset_io_request =
933 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
934 sizeof(union MPI2_SGE_IO_UNION))/16;
935
936 fusion->last_reply_idx = 0;
937
938 /*
939 * Allocate memory for descriptors
940 * Create a pool of commands
941 */
942 if (megasas_alloc_cmds(instance))
943 goto fail_alloc_mfi_cmds;
944 if (megasas_alloc_cmds_fusion(instance))
945 goto fail_alloc_cmds;
946
947 if (megasas_ioc_init_fusion(instance))
948 goto fail_ioc_init;
949
950 instance->flag_ieee = 1;
951
952 fusion->map_sz = sizeof(struct MR_FW_RAID_MAP) +
953 (sizeof(struct MR_LD_SPAN_MAP) *(MAX_LOGICAL_DRIVES - 1));
954
955 fusion->fast_path_io = 0;
956
957 for (i = 0; i < 2; i++) {
958 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
959 fusion->map_sz,
960 &fusion->ld_map_phys[i],
961 GFP_KERNEL);
962 if (!fusion->ld_map[i]) {
963 printk(KERN_ERR "megasas: Could not allocate memory "
964 "for map info\n");
965 goto fail_map_info;
966 }
967 }
968
969 if (!megasas_get_map_info(instance))
970 megasas_sync_map_info(instance);
971
972 return 0;
973
adam radford9c915a82010-12-21 13:34:31 -0800974fail_map_info:
975 if (i == 1)
976 dma_free_coherent(&instance->pdev->dev, fusion->map_sz,
977 fusion->ld_map[0], fusion->ld_map_phys[0]);
978fail_ioc_init:
adam radfordeb1b1232011-02-24 20:55:56 -0800979 megasas_free_cmds_fusion(instance);
980fail_alloc_cmds:
981 megasas_free_cmds(instance);
982fail_alloc_mfi_cmds:
adam radford9c915a82010-12-21 13:34:31 -0800983 return 1;
984}
985
986/**
987 * megasas_fire_cmd_fusion - Sends command to the FW
988 * @frame_phys_addr : Physical address of cmd
989 * @frame_count : Number of frames for the command
990 * @regs : MFI register set
991 */
992void
993megasas_fire_cmd_fusion(struct megasas_instance *instance,
994 dma_addr_t req_desc_lo,
995 u32 req_desc_hi,
996 struct megasas_register_set __iomem *regs)
997{
998 unsigned long flags;
999
1000 spin_lock_irqsave(&instance->hba_lock, flags);
1001
1002 writel(req_desc_lo,
1003 &(regs)->inbound_low_queue_port);
1004 writel(req_desc_hi, &(regs)->inbound_high_queue_port);
1005 spin_unlock_irqrestore(&instance->hba_lock, flags);
1006}
1007
1008/**
1009 * map_cmd_status - Maps FW cmd status to OS cmd status
1010 * @cmd : Pointer to cmd
1011 * @status : status of cmd returned by FW
1012 * @ext_status : ext status of cmd returned by FW
1013 */
1014
1015void
1016map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1017{
1018
1019 switch (status) {
1020
1021 case MFI_STAT_OK:
1022 cmd->scmd->result = DID_OK << 16;
1023 break;
1024
1025 case MFI_STAT_SCSI_IO_FAILED:
1026 case MFI_STAT_LD_INIT_IN_PROGRESS:
1027 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1028 break;
1029
1030 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1031
1032 cmd->scmd->result = (DID_OK << 16) | ext_status;
1033 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1034 memset(cmd->scmd->sense_buffer, 0,
1035 SCSI_SENSE_BUFFERSIZE);
1036 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1037 SCSI_SENSE_BUFFERSIZE);
1038 cmd->scmd->result |= DRIVER_SENSE << 24;
1039 }
1040 break;
1041
1042 case MFI_STAT_LD_OFFLINE:
1043 case MFI_STAT_DEVICE_NOT_FOUND:
1044 cmd->scmd->result = DID_BAD_TARGET << 16;
1045 break;
1046
1047 default:
1048 printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1049 cmd->scmd->result = DID_ERROR << 16;
1050 break;
1051 }
1052}
1053
1054/**
1055 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1056 * @instance: Adapter soft state
1057 * @scp: SCSI command from the mid-layer
1058 * @sgl_ptr: SGL to be filled in
1059 * @cmd: cmd we are working on
1060 *
1061 * If successful, this function returns the number of SG elements.
1062 */
1063static int
1064megasas_make_sgl_fusion(struct megasas_instance *instance,
1065 struct scsi_cmnd *scp,
1066 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1067 struct megasas_cmd_fusion *cmd)
1068{
1069 int i, sg_processed;
1070 int sge_count, sge_idx;
1071 struct scatterlist *os_sgl;
1072 struct fusion_context *fusion;
1073
1074 fusion = instance->ctrl_context;
1075
1076 cmd->io_request->ChainOffset = 0;
1077
1078 sge_count = scsi_dma_map(scp);
1079
1080 BUG_ON(sge_count < 0);
1081
1082 if (sge_count > instance->max_num_sge || !sge_count)
1083 return sge_count;
1084
1085 if (sge_count > fusion->max_sge_in_main_msg) {
1086 /* One element to store the chain info */
1087 sge_idx = fusion->max_sge_in_main_msg - 1;
1088 } else
1089 sge_idx = sge_count;
1090
1091 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
1092 sgl_ptr->Length = sg_dma_len(os_sgl);
1093 sgl_ptr->Address = sg_dma_address(os_sgl);
1094 sgl_ptr->Flags = 0;
1095 sgl_ptr++;
1096
1097 sg_processed = i + 1;
1098
1099 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1100 (sge_count > fusion->max_sge_in_main_msg)) {
1101
1102 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
1103 cmd->io_request->ChainOffset =
1104 fusion->chain_offset_io_request;
1105 sg_chain = sgl_ptr;
1106 /* Prepare chain element */
1107 sg_chain->NextChainOffset = 0;
1108 sg_chain->Flags = (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1109 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1110 sg_chain->Length = (sizeof(union MPI2_SGE_IO_UNION)
1111 *(sge_count - sg_processed));
1112 sg_chain->Address = cmd->sg_frame_phys_addr;
1113
1114 sgl_ptr =
1115 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
1116 }
1117 }
1118
1119 return sge_count;
1120}
1121
1122/**
1123 * megasas_set_pd_lba - Sets PD LBA
1124 * @cdb: CDB
1125 * @cdb_len: cdb length
1126 * @start_blk: Start block of IO
1127 *
1128 * Used to set the PD LBA in CDB for FP IOs
1129 */
1130void
1131megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1132 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
1133 struct MR_FW_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
1134{
1135 struct MR_LD_RAID *raid;
1136 u32 ld;
1137 u64 start_blk = io_info->pdBlock;
1138 u8 *cdb = io_request->CDB.CDB32;
1139 u32 num_blocks = io_info->numBlocks;
adam radford495c5602011-05-11 18:34:40 -07001140 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
adam radford9c915a82010-12-21 13:34:31 -08001141
1142 /* Check if T10 PI (DIF) is enabled for this LD */
1143 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1144 raid = MR_LdRaidGet(ld, local_map_ptr);
1145 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1146 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1147 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1148 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1149
1150 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1151 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1152 else
1153 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1154 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1155
1156 /* LBA */
1157 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1158 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1159 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1160 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1161 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1162 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1163 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1164 cdb[19] = (u8)(start_blk & 0xff);
1165
1166 /* Logical block reference tag */
1167 io_request->CDB.EEDP32.PrimaryReferenceTag =
1168 cpu_to_be32(ref_tag);
1169 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0xffff;
1170
1171 io_request->DataLength = num_blocks * 512;
1172 io_request->IoFlags = 32; /* Specify 32-byte cdb */
1173
1174 /* Transfer length */
1175 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1176 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1177 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1178 cdb[31] = (u8)(num_blocks & 0xff);
1179
1180 /* set SCSI IO EEDPFlags */
1181 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
1182 io_request->EEDPFlags =
1183 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1184 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1185 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1186 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
1187 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD;
1188 } else {
1189 io_request->EEDPFlags =
1190 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1191 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP;
1192 }
1193 io_request->Control |= (0x4 << 26);
1194 io_request->EEDPBlockSize = MEGASAS_EEDPBLOCKSIZE;
1195 } else {
1196 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1197 if (((cdb_len == 12) || (cdb_len == 16)) &&
1198 (start_blk <= 0xffffffff)) {
1199 if (cdb_len == 16) {
1200 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1201 flagvals = cdb[1];
1202 groupnum = cdb[14];
1203 control = cdb[15];
1204 } else {
1205 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1206 flagvals = cdb[1];
1207 groupnum = cdb[10];
1208 control = cdb[11];
1209 }
1210
1211 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1212
1213 cdb[0] = opcode;
1214 cdb[1] = flagvals;
1215 cdb[6] = groupnum;
1216 cdb[9] = control;
1217
1218 /* Transfer length */
1219 cdb[8] = (u8)(num_blocks & 0xff);
1220 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1221
adam radford495c5602011-05-11 18:34:40 -07001222 io_request->IoFlags = 10; /* Specify 10-byte cdb */
adam radford9c915a82010-12-21 13:34:31 -08001223 cdb_len = 10;
adam radford495c5602011-05-11 18:34:40 -07001224 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1225 /* Convert to 16 byte CDB for large LBA's */
1226 switch (cdb_len) {
1227 case 6:
1228 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1229 control = cdb[5];
1230 break;
1231 case 10:
1232 opcode =
1233 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1234 flagvals = cdb[1];
1235 groupnum = cdb[6];
1236 control = cdb[9];
1237 break;
1238 case 12:
1239 opcode =
1240 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1241 flagvals = cdb[1];
1242 groupnum = cdb[10];
1243 control = cdb[11];
1244 break;
1245 }
1246
1247 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1248
1249 cdb[0] = opcode;
1250 cdb[1] = flagvals;
1251 cdb[14] = groupnum;
1252 cdb[15] = control;
1253
1254 /* Transfer length */
1255 cdb[13] = (u8)(num_blocks & 0xff);
1256 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1257 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1258 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1259
1260 io_request->IoFlags = 16; /* Specify 16-byte cdb */
1261 cdb_len = 16;
adam radford9c915a82010-12-21 13:34:31 -08001262 }
1263
1264 /* Normal case, just load LBA here */
1265 switch (cdb_len) {
1266 case 6:
1267 {
1268 u8 val = cdb[1] & 0xE0;
1269 cdb[3] = (u8)(start_blk & 0xff);
1270 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1271 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1272 break;
1273 }
1274 case 10:
1275 cdb[5] = (u8)(start_blk & 0xff);
1276 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1277 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1278 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1279 break;
1280 case 12:
1281 cdb[5] = (u8)(start_blk & 0xff);
1282 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1283 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1284 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1285 break;
1286 case 16:
1287 cdb[9] = (u8)(start_blk & 0xff);
1288 cdb[8] = (u8)((start_blk >> 8) & 0xff);
1289 cdb[7] = (u8)((start_blk >> 16) & 0xff);
1290 cdb[6] = (u8)((start_blk >> 24) & 0xff);
1291 cdb[5] = (u8)((start_blk >> 32) & 0xff);
1292 cdb[4] = (u8)((start_blk >> 40) & 0xff);
1293 cdb[3] = (u8)((start_blk >> 48) & 0xff);
1294 cdb[2] = (u8)((start_blk >> 56) & 0xff);
1295 break;
1296 }
1297 }
1298}
1299
1300/**
1301 * megasas_build_ldio_fusion - Prepares IOs to devices
1302 * @instance: Adapter soft state
1303 * @scp: SCSI command
1304 * @cmd: Command to be prepared
1305 *
1306 * Prepares the io_request and chain elements (sg_frame) for IO
1307 * The IO can be for PD (Fast Path) or LD
1308 */
1309void
1310megasas_build_ldio_fusion(struct megasas_instance *instance,
1311 struct scsi_cmnd *scp,
1312 struct megasas_cmd_fusion *cmd)
1313{
1314 u8 fp_possible;
1315 u32 start_lba_lo, start_lba_hi, device_id;
1316 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1317 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1318 struct IO_REQUEST_INFO io_info;
1319 struct fusion_context *fusion;
1320 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1321
1322 device_id = MEGASAS_DEV_INDEX(instance, scp);
1323
1324 fusion = instance->ctrl_context;
1325
1326 io_request = cmd->io_request;
1327 io_request->RaidContext.VirtualDiskTgtId = device_id;
1328 io_request->RaidContext.status = 0;
1329 io_request->RaidContext.exStatus = 0;
1330
1331 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1332
1333 start_lba_lo = 0;
1334 start_lba_hi = 0;
1335 fp_possible = 0;
1336
1337 /*
1338 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1339 */
1340 if (scp->cmd_len == 6) {
1341 io_request->DataLength = (u32) scp->cmnd[4];
1342 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1343 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1344
1345 start_lba_lo &= 0x1FFFFF;
1346 }
1347
1348 /*
1349 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1350 */
1351 else if (scp->cmd_len == 10) {
1352 io_request->DataLength = (u32) scp->cmnd[8] |
1353 ((u32) scp->cmnd[7] << 8);
1354 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1355 ((u32) scp->cmnd[3] << 16) |
1356 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1357 }
1358
1359 /*
1360 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1361 */
1362 else if (scp->cmd_len == 12) {
1363 io_request->DataLength = ((u32) scp->cmnd[6] << 24) |
1364 ((u32) scp->cmnd[7] << 16) |
1365 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1366 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1367 ((u32) scp->cmnd[3] << 16) |
1368 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1369 }
1370
1371 /*
1372 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1373 */
1374 else if (scp->cmd_len == 16) {
1375 io_request->DataLength = ((u32) scp->cmnd[10] << 24) |
1376 ((u32) scp->cmnd[11] << 16) |
1377 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1378 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1379 ((u32) scp->cmnd[7] << 16) |
1380 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1381
1382 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1383 ((u32) scp->cmnd[3] << 16) |
1384 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1385 }
1386
1387 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1388 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
1389 io_info.numBlocks = io_request->DataLength;
1390 io_info.ldTgtId = device_id;
1391
1392 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1393 io_info.isRead = 1;
1394
1395 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1396
1397 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
1398 MAX_LOGICAL_DRIVES) || (!fusion->fast_path_io)) {
1399 io_request->RaidContext.regLockFlags = 0;
1400 fp_possible = 0;
1401 } else {
1402 if (MR_BuildRaidContext(&io_info, &io_request->RaidContext,
1403 local_map_ptr))
1404 fp_possible = io_info.fpOkForIo;
1405 }
1406
1407 if (fp_possible) {
1408 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1409 local_map_ptr, start_lba_lo);
1410 io_request->DataLength = scsi_bufflen(scp);
1411 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1412 cmd->request_desc->SCSIIO.RequestFlags =
1413 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1414 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1415 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1416 (io_info.isRead)) {
1417 io_info.devHandle =
1418 get_updated_dev_handle(
1419 &fusion->load_balance_info[device_id],
1420 &io_info);
1421 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
1422 } else
1423 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1424 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1425 io_request->DevHandle = io_info.devHandle;
1426 } else {
1427 io_request->RaidContext.timeoutValue =
1428 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1429 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1430 io_request->DevHandle = device_id;
1431 cmd->request_desc->SCSIIO.RequestFlags =
1432 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1433 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1434 } /* Not FP */
1435}
1436
1437/**
1438 * megasas_build_dcdb_fusion - Prepares IOs to devices
1439 * @instance: Adapter soft state
1440 * @scp: SCSI command
1441 * @cmd: Command to be prepared
1442 *
1443 * Prepares the io_request frame for non-io cmds
1444 */
1445static void
1446megasas_build_dcdb_fusion(struct megasas_instance *instance,
1447 struct scsi_cmnd *scmd,
1448 struct megasas_cmd_fusion *cmd)
1449{
1450 u32 device_id;
1451 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1452 u16 pd_index = 0;
1453 struct MR_FW_RAID_MAP_ALL *local_map_ptr;
1454 struct fusion_context *fusion = instance->ctrl_context;
1455
1456 io_request = cmd->io_request;
1457 device_id = MEGASAS_DEV_INDEX(instance, scmd);
1458 pd_index = (scmd->device->channel * MEGASAS_MAX_DEV_PER_CHANNEL)
1459 +scmd->device->id;
1460 local_map_ptr = fusion->ld_map[(instance->map_id & 1)];
1461
1462 /* Check if this is a system PD I/O */
adam radfordf5124402011-02-24 20:56:43 -08001463 if (instance->pd_list[pd_index].driveState == MR_PD_STATE_SYSTEM) {
adam radford9c915a82010-12-21 13:34:31 -08001464 io_request->Function = 0;
1465 io_request->DevHandle =
1466 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1467 io_request->RaidContext.timeoutValue =
1468 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1469 io_request->RaidContext.regLockFlags = 0;
1470 io_request->RaidContext.regLockRowLBA = 0;
1471 io_request->RaidContext.regLockLength = 0;
1472 io_request->RaidContext.RAIDFlags =
1473 MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
1474 MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1475 cmd->request_desc->SCSIIO.RequestFlags =
1476 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1477 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1478 } else {
1479 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1480 io_request->DevHandle = device_id;
1481 cmd->request_desc->SCSIIO.RequestFlags =
1482 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1483 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1484 }
1485 io_request->RaidContext.VirtualDiskTgtId = device_id;
adam radfordeaa3c242011-02-24 20:56:49 -08001486 io_request->LUN[1] = scmd->device->lun;
adam radford9c915a82010-12-21 13:34:31 -08001487 io_request->DataLength = scsi_bufflen(scmd);
1488}
1489
1490/**
1491 * megasas_build_io_fusion - Prepares IOs to devices
1492 * @instance: Adapter soft state
1493 * @scp: SCSI command
1494 * @cmd: Command to be prepared
1495 *
1496 * Invokes helper functions to prepare request frames
1497 * and sets flags appropriate for IO/Non-IO cmd
1498 */
1499int
1500megasas_build_io_fusion(struct megasas_instance *instance,
1501 struct scsi_cmnd *scp,
1502 struct megasas_cmd_fusion *cmd)
1503{
1504 u32 device_id, sge_count;
1505 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1506
1507 device_id = MEGASAS_DEV_INDEX(instance, scp);
1508
1509 /* Zero out some fields so they don't get reused */
adam radfordeaa3c242011-02-24 20:56:49 -08001510 io_request->LUN[1] = 0;
adam radford9c915a82010-12-21 13:34:31 -08001511 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1512 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1513 io_request->EEDPFlags = 0;
1514 io_request->Control = 0;
1515 io_request->EEDPBlockSize = 0;
1516 io_request->IoFlags = 0;
1517 io_request->RaidContext.RAIDFlags = 0;
1518
1519 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1520 /*
1521 * Just the CDB length,rest of the Flags are zero
1522 * This will be modified for FP in build_ldio_fusion
1523 */
1524 io_request->IoFlags = scp->cmd_len;
1525
1526 if (megasas_is_ldio(scp))
1527 megasas_build_ldio_fusion(instance, scp, cmd);
1528 else
1529 megasas_build_dcdb_fusion(instance, scp, cmd);
1530
1531 /*
1532 * Construct SGL
1533 */
1534
1535 sge_count =
1536 megasas_make_sgl_fusion(instance, scp,
1537 (struct MPI25_IEEE_SGE_CHAIN64 *)
1538 &io_request->SGL, cmd);
1539
1540 if (sge_count > instance->max_num_sge) {
1541 printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1542 "max (0x%x) allowed\n", sge_count,
1543 instance->max_num_sge);
1544 return 1;
1545 }
1546
1547 io_request->RaidContext.numSGE = sge_count;
1548
1549 io_request->SGLFlags = MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
1550
1551 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
1552 io_request->Control |= MPI2_SCSIIO_CONTROL_WRITE;
1553 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1554 io_request->Control |= MPI2_SCSIIO_CONTROL_READ;
1555
1556 io_request->SGLOffset0 =
1557 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1558
1559 io_request->SenseBufferLowAddress = cmd->sense_phys_addr;
1560 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1561
1562 cmd->scmd = scp;
1563 scp->SCp.ptr = (char *)cmd;
1564
1565 return 0;
1566}
1567
1568union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1569megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1570{
1571 u8 *p;
1572 struct fusion_context *fusion;
1573
1574 if (index >= instance->max_fw_cmds) {
1575 printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
1576 "descriptor\n", index);
1577 return NULL;
1578 }
1579 fusion = instance->ctrl_context;
1580 p = fusion->req_frames_desc
1581 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1582
1583 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1584}
1585
1586/**
1587 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1588 * issuing non IOCTL cmd
1589 * @instance: Adapter soft state
1590 * @scmd: pointer to scsi cmd from OS
1591 */
1592static u32
1593megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1594 struct scsi_cmnd *scmd)
1595{
1596 struct megasas_cmd_fusion *cmd;
1597 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1598 u32 index;
1599 struct fusion_context *fusion;
1600
1601 fusion = instance->ctrl_context;
1602
1603 cmd = megasas_get_cmd_fusion(instance);
1604 if (!cmd)
1605 return SCSI_MLQUEUE_HOST_BUSY;
1606
1607 index = cmd->index;
1608
1609 req_desc = megasas_get_request_descriptor(instance, index-1);
1610 if (!req_desc)
1611 return 1;
1612
1613 req_desc->Words = 0;
1614 cmd->request_desc = req_desc;
1615 cmd->request_desc->Words = 0;
1616
1617 if (megasas_build_io_fusion(instance, scmd, cmd)) {
1618 megasas_return_cmd_fusion(instance, cmd);
1619 printk(KERN_ERR "megasas: Error building command.\n");
1620 cmd->request_desc = NULL;
1621 return 1;
1622 }
1623
1624 req_desc = cmd->request_desc;
1625 req_desc->SCSIIO.SMID = index;
1626
1627 if (cmd->io_request->ChainOffset != 0 &&
1628 cmd->io_request->ChainOffset != 0xF)
1629 printk(KERN_ERR "megasas: The chain offset value is not "
1630 "correct : %x\n", cmd->io_request->ChainOffset);
1631
1632 /*
1633 * Issue the command to the FW
1634 */
1635 atomic_inc(&instance->fw_outstanding);
1636
1637 instance->instancet->fire_cmd(instance,
1638 req_desc->u.low, req_desc->u.high,
1639 instance->reg_set);
1640
1641 return 0;
1642}
1643
1644/**
1645 * complete_cmd_fusion - Completes command
1646 * @instance: Adapter soft state
1647 * Completes all commands that is in reply descriptor queue
1648 */
1649int
1650complete_cmd_fusion(struct megasas_instance *instance)
1651{
1652 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1653 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1654 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1655 struct fusion_context *fusion;
1656 struct megasas_cmd *cmd_mfi;
1657 struct megasas_cmd_fusion *cmd_fusion;
1658 u16 smid, num_completed;
1659 u8 reply_descript_type, arm;
1660 u32 status, extStatus, device_id;
1661 union desc_value d_val;
1662 struct LD_LOAD_BALANCE_INFO *lbinfo;
1663
1664 fusion = instance->ctrl_context;
1665
1666 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1667 return IRQ_HANDLED;
1668
1669 desc = fusion->reply_frames_desc;
1670 desc += fusion->last_reply_idx;
1671
1672 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1673
1674 d_val.word = desc->Words;
1675
1676 reply_descript_type = reply_desc->ReplyFlags &
1677 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1678
1679 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1680 return IRQ_NONE;
1681
1682 d_val.word = desc->Words;
1683
1684 num_completed = 0;
1685
1686 while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
1687 smid = reply_desc->SMID;
1688
1689 cmd_fusion = fusion->cmd_list[smid - 1];
1690
1691 scsi_io_req =
1692 (struct MPI2_RAID_SCSI_IO_REQUEST *)
1693 cmd_fusion->io_request;
1694
1695 if (cmd_fusion->scmd)
1696 cmd_fusion->scmd->SCp.ptr = NULL;
1697
1698 status = scsi_io_req->RaidContext.status;
1699 extStatus = scsi_io_req->RaidContext.exStatus;
1700
1701 switch (scsi_io_req->Function) {
1702 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
1703 /* Update load balancing info */
1704 device_id = MEGASAS_DEV_INDEX(instance,
1705 cmd_fusion->scmd);
1706 lbinfo = &fusion->load_balance_info[device_id];
1707 if (cmd_fusion->scmd->SCp.Status &
1708 MEGASAS_LOAD_BALANCE_FLAG) {
1709 arm = lbinfo->raid1DevHandle[0] ==
1710 cmd_fusion->io_request->DevHandle ? 0 :
1711 1;
1712 atomic_dec(&lbinfo->scsi_pending_cmds[arm]);
1713 cmd_fusion->scmd->SCp.Status &=
1714 ~MEGASAS_LOAD_BALANCE_FLAG;
1715 }
1716 if (reply_descript_type ==
1717 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
1718 if (megasas_dbg_lvl == 5)
1719 printk(KERN_ERR "\nmegasas: FAST Path "
1720 "IO Success\n");
1721 }
1722 /* Fall thru and complete IO */
1723 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
1724 /* Map the FW Cmd Status */
1725 map_cmd_status(cmd_fusion, status, extStatus);
1726 scsi_dma_unmap(cmd_fusion->scmd);
1727 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
1728 scsi_io_req->RaidContext.status = 0;
1729 scsi_io_req->RaidContext.exStatus = 0;
1730 megasas_return_cmd_fusion(instance, cmd_fusion);
1731 atomic_dec(&instance->fw_outstanding);
1732
1733 break;
1734 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
1735 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
1736 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
1737 cmd_fusion->flags = 0;
1738 megasas_return_cmd_fusion(instance, cmd_fusion);
1739
1740 break;
1741 }
1742
1743 fusion->last_reply_idx++;
1744 if (fusion->last_reply_idx >= fusion->reply_q_depth)
1745 fusion->last_reply_idx = 0;
1746
1747 desc->Words = ULLONG_MAX;
1748 num_completed++;
1749
1750 /* Get the next reply descriptor */
1751 if (!fusion->last_reply_idx)
1752 desc = fusion->reply_frames_desc;
1753 else
1754 desc++;
1755
1756 reply_desc =
1757 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1758
1759 d_val.word = desc->Words;
1760
1761 reply_descript_type = reply_desc->ReplyFlags &
1762 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1763
1764 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1765 break;
1766 }
1767
1768 if (!num_completed)
1769 return IRQ_NONE;
1770
1771 wmb();
1772 writel(fusion->last_reply_idx,
1773 &instance->reg_set->reply_post_host_index);
adam radford53ef2bb2011-02-24 20:56:05 -08001774 megasas_check_and_restore_queue_depth(instance);
adam radford9c915a82010-12-21 13:34:31 -08001775 return IRQ_HANDLED;
1776}
1777
1778/**
1779 * megasas_complete_cmd_dpc_fusion - Completes command
1780 * @instance: Adapter soft state
1781 *
1782 * Tasklet to complete cmds
1783 */
1784void
1785megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
1786{
1787 struct megasas_instance *instance =
1788 (struct megasas_instance *)instance_addr;
1789 unsigned long flags;
1790
1791 /* If we have already declared adapter dead, donot complete cmds */
1792 spin_lock_irqsave(&instance->hba_lock, flags);
1793 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
1794 spin_unlock_irqrestore(&instance->hba_lock, flags);
1795 return;
1796 }
1797 spin_unlock_irqrestore(&instance->hba_lock, flags);
1798
1799 spin_lock_irqsave(&instance->completion_lock, flags);
1800 complete_cmd_fusion(instance);
1801 spin_unlock_irqrestore(&instance->completion_lock, flags);
1802}
1803
1804/**
1805 * megasas_isr_fusion - isr entry point
1806 */
1807irqreturn_t megasas_isr_fusion(int irq, void *devp)
1808{
1809 struct megasas_instance *instance = (struct megasas_instance *)devp;
1810 u32 mfiStatus, fw_state;
1811
1812 if (!instance->msi_flag) {
1813 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
1814 if (!mfiStatus)
1815 return IRQ_NONE;
1816 }
1817
1818 /* If we are resetting, bail */
1819 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags))
1820 return IRQ_HANDLED;
1821
1822 if (!complete_cmd_fusion(instance)) {
1823 /* If we didn't complete any commands, check for FW fault */
1824 fw_state = instance->instancet->read_fw_status_reg(
1825 instance->reg_set) & MFI_STATE_MASK;
1826 if (fw_state == MFI_STATE_FAULT)
1827 schedule_work(&instance->work_init);
1828 }
1829
1830 return IRQ_HANDLED;
1831}
1832
1833/**
1834 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
1835 * @instance: Adapter soft state
1836 * mfi_cmd: megasas_cmd pointer
1837 *
1838 */
1839u8
1840build_mpt_mfi_pass_thru(struct megasas_instance *instance,
1841 struct megasas_cmd *mfi_cmd)
1842{
1843 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
1844 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
1845 struct megasas_cmd_fusion *cmd;
1846 struct fusion_context *fusion;
1847 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
1848
1849 cmd = megasas_get_cmd_fusion(instance);
1850 if (!cmd)
1851 return 1;
1852
1853 /* Save the smid. To be used for returning the cmd */
1854 mfi_cmd->context.smid = cmd->index;
1855
1856 cmd->sync_cmd_idx = mfi_cmd->index;
1857
1858 /*
1859 * For cmds where the flag is set, store the flag and check
1860 * on completion. For cmds with this flag, don't call
1861 * megasas_complete_cmd
1862 */
1863
1864 if (frame_hdr->flags & MFI_FRAME_DONT_POST_IN_REPLY_QUEUE)
1865 cmd->flags = MFI_FRAME_DONT_POST_IN_REPLY_QUEUE;
1866
1867 fusion = instance->ctrl_context;
1868 io_req = cmd->io_request;
1869 mpi25_ieee_chain =
1870 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
1871
1872 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
1873 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
1874 SGL) / 4;
1875 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
1876
1877 mpi25_ieee_chain->Address = mfi_cmd->frame_phys_addr;
1878
1879 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1880 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
1881
1882 mpi25_ieee_chain->Length = MEGASAS_MAX_SZ_CHAIN_FRAME;
1883
1884 return 0;
1885}
1886
1887/**
1888 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
1889 * @instance: Adapter soft state
1890 * @cmd: mfi cmd to build
1891 *
1892 */
1893union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1894build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
1895{
1896 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1897 u16 index;
1898
1899 if (build_mpt_mfi_pass_thru(instance, cmd)) {
1900 printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
1901 return NULL;
1902 }
1903
1904 index = cmd->context.smid;
1905
1906 req_desc = megasas_get_request_descriptor(instance, index - 1);
1907
1908 if (!req_desc)
1909 return NULL;
1910
1911 req_desc->Words = 0;
1912 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1913 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1914
1915 req_desc->SCSIIO.SMID = index;
1916
1917 return req_desc;
1918}
1919
1920/**
1921 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
1922 * @instance: Adapter soft state
1923 * @cmd: mfi cmd pointer
1924 *
1925 */
1926void
1927megasas_issue_dcmd_fusion(struct megasas_instance *instance,
1928 struct megasas_cmd *cmd)
1929{
1930 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1931 union desc_value d_val;
1932
1933 req_desc = build_mpt_cmd(instance, cmd);
1934 if (!req_desc) {
1935 printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
1936 return;
1937 }
1938 d_val.word = req_desc->Words;
1939
1940 instance->instancet->fire_cmd(instance, req_desc->u.low,
1941 req_desc->u.high, instance->reg_set);
1942}
1943
1944/**
1945 * megasas_release_fusion - Reverses the FW initialization
1946 * @intance: Adapter soft state
1947 */
1948void
1949megasas_release_fusion(struct megasas_instance *instance)
1950{
1951 megasas_free_cmds(instance);
1952 megasas_free_cmds_fusion(instance);
1953
1954 iounmap(instance->reg_set);
1955
1956 pci_release_selected_regions(instance->pdev, instance->bar);
1957}
1958
1959/**
1960 * megasas_read_fw_status_reg_fusion - returns the current FW status value
1961 * @regs: MFI register set
1962 */
1963static u32
1964megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
1965{
1966 return readl(&(regs)->outbound_scratch_pad);
1967}
1968
1969/**
1970 * megasas_adp_reset_fusion - For controller reset
1971 * @regs: MFI register set
1972 */
1973static int
1974megasas_adp_reset_fusion(struct megasas_instance *instance,
1975 struct megasas_register_set __iomem *regs)
1976{
1977 return 0;
1978}
1979
1980/**
1981 * megasas_check_reset_fusion - For controller reset check
1982 * @regs: MFI register set
1983 */
1984static int
1985megasas_check_reset_fusion(struct megasas_instance *instance,
1986 struct megasas_register_set __iomem *regs)
1987{
1988 return 0;
1989}
1990
1991/* This function waits for outstanding commands on fusion to complete */
1992int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance)
1993{
1994 int i, outstanding, retval = 0;
1995 u32 fw_state, wait_time = MEGASAS_RESET_WAIT_TIME;
1996
1997 for (i = 0; i < wait_time; i++) {
1998 /* Check if firmware is in fault state */
1999 fw_state = instance->instancet->read_fw_status_reg(
2000 instance->reg_set) & MFI_STATE_MASK;
2001 if (fw_state == MFI_STATE_FAULT) {
2002 printk(KERN_WARNING "megasas: Found FW in FAULT state,"
2003 " will reset adapter.\n");
2004 retval = 1;
2005 goto out;
2006 }
2007
2008 outstanding = atomic_read(&instance->fw_outstanding);
2009 if (!outstanding)
2010 goto out;
2011
2012 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2013 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
2014 "commands to complete\n", i, outstanding);
2015 megasas_complete_cmd_dpc_fusion(
2016 (unsigned long)instance);
2017 }
2018 msleep(1000);
2019 }
2020
2021 if (atomic_read(&instance->fw_outstanding)) {
2022 printk("megaraid_sas: pending commands remain after waiting, "
2023 "will reset adapter.\n");
2024 retval = 1;
2025 }
2026out:
2027 return retval;
2028}
2029
2030void megasas_reset_reply_desc(struct megasas_instance *instance)
2031{
2032 int i;
2033 struct fusion_context *fusion;
2034 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2035
2036 fusion = instance->ctrl_context;
2037 fusion->last_reply_idx = 0;
2038 reply_desc = fusion->reply_frames_desc;
2039 for (i = 0 ; i < fusion->reply_q_depth; i++, reply_desc++)
2040 reply_desc->Words = ULLONG_MAX;
2041}
2042
2043/* Core fusion reset function */
2044int megasas_reset_fusion(struct Scsi_Host *shost)
2045{
2046 int retval = SUCCESS, i, j, retry = 0;
2047 struct megasas_instance *instance;
2048 struct megasas_cmd_fusion *cmd_fusion;
2049 struct fusion_context *fusion;
2050 struct megasas_cmd *cmd_mfi;
2051 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
adam radford7e70e732011-05-11 18:34:08 -07002052 u32 host_diag, abs_state, status_reg, reset_adapter;
adam radford9c915a82010-12-21 13:34:31 -08002053
2054 instance = (struct megasas_instance *)shost->hostdata;
2055 fusion = instance->ctrl_context;
2056
adam radford9c915a82010-12-21 13:34:31 -08002057 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2058 printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
2059 "returning FAILED.\n");
2060 retval = FAILED;
2061 goto out;
2062 }
2063
adam radford7e70e732011-05-11 18:34:08 -07002064 mutex_lock(&instance->reset_mutex);
2065 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2066 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2067 instance->instancet->disable_intr(instance->reg_set);
2068 msleep(1000);
2069
adam radford9c915a82010-12-21 13:34:31 -08002070 /* First try waiting for commands to complete */
2071 if (megasas_wait_for_outstanding_fusion(instance)) {
2072 printk(KERN_WARNING "megaraid_sas: resetting fusion "
2073 "adapter.\n");
2074 /* Now return commands back to the OS */
2075 for (i = 0 ; i < instance->max_fw_cmds; i++) {
2076 cmd_fusion = fusion->cmd_list[i];
2077 if (cmd_fusion->scmd) {
2078 scsi_dma_unmap(cmd_fusion->scmd);
2079 cmd_fusion->scmd->result = (DID_RESET << 16);
2080 cmd_fusion->scmd->scsi_done(cmd_fusion->scmd);
2081 megasas_return_cmd_fusion(instance, cmd_fusion);
2082 atomic_dec(&instance->fw_outstanding);
2083 }
2084 }
2085
adam radford7e70e732011-05-11 18:34:08 -07002086 status_reg = instance->instancet->read_fw_status_reg(
2087 instance->reg_set);
2088 abs_state = status_reg & MFI_STATE_MASK;
2089 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2090 if (instance->disableOnlineCtrlReset ||
2091 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
adam radford9c915a82010-12-21 13:34:31 -08002092 /* Reset not supported, kill adapter */
2093 printk(KERN_WARNING "megaraid_sas: Reset not supported"
2094 ", killing adapter.\n");
2095 megaraid_sas_kill_hba(instance);
2096 instance->adprecovery = MEGASAS_HW_CRITICAL_ERROR;
2097 retval = FAILED;
2098 goto out;
2099 }
2100
2101 /* Now try to reset the chip */
2102 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2103 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2104 &instance->reg_set->fusion_seq_offset);
2105 writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2106 &instance->reg_set->fusion_seq_offset);
2107 writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2108 &instance->reg_set->fusion_seq_offset);
2109 writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2110 &instance->reg_set->fusion_seq_offset);
2111 writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2112 &instance->reg_set->fusion_seq_offset);
2113 writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2114 &instance->reg_set->fusion_seq_offset);
2115 writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2116 &instance->reg_set->fusion_seq_offset);
2117
2118 /* Check that the diag write enable (DRWE) bit is on */
2119 host_diag = readl(&instance->reg_set->fusion_host_diag);
adam radford7e70e732011-05-11 18:34:08 -07002120 retry = 0;
adam radford9c915a82010-12-21 13:34:31 -08002121 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2122 msleep(100);
2123 host_diag =
2124 readl(&instance->reg_set->fusion_host_diag);
2125 if (retry++ == 100) {
2126 printk(KERN_WARNING "megaraid_sas: "
2127 "Host diag unlock failed!\n");
2128 break;
2129 }
2130 }
2131 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2132 continue;
2133
2134 /* Send chip reset command */
2135 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2136 &instance->reg_set->fusion_host_diag);
2137 msleep(3000);
2138
2139 /* Make sure reset adapter bit is cleared */
2140 host_diag = readl(&instance->reg_set->fusion_host_diag);
2141 retry = 0;
2142 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2143 msleep(100);
2144 host_diag =
2145 readl(&instance->reg_set->fusion_host_diag);
2146 if (retry++ == 1000) {
2147 printk(KERN_WARNING "megaraid_sas: "
2148 "Diag reset adapter never "
2149 "cleared!\n");
2150 break;
2151 }
2152 }
2153 if (host_diag & HOST_DIAG_RESET_ADAPTER)
2154 continue;
2155
2156 abs_state =
2157 instance->instancet->read_fw_status_reg(
adam radford7e70e732011-05-11 18:34:08 -07002158 instance->reg_set) & MFI_STATE_MASK;
adam radford9c915a82010-12-21 13:34:31 -08002159 retry = 0;
2160
2161 while ((abs_state <= MFI_STATE_FW_INIT) &&
2162 (retry++ < 1000)) {
2163 msleep(100);
2164 abs_state =
2165 instance->instancet->read_fw_status_reg(
adam radford7e70e732011-05-11 18:34:08 -07002166 instance->reg_set) & MFI_STATE_MASK;
adam radford9c915a82010-12-21 13:34:31 -08002167 }
2168 if (abs_state <= MFI_STATE_FW_INIT) {
2169 printk(KERN_WARNING "megaraid_sas: firmware "
2170 "state < MFI_STATE_FW_INIT, state = "
2171 "0x%x\n", abs_state);
2172 continue;
2173 }
2174
2175 /* Wait for FW to become ready */
2176 if (megasas_transition_to_ready(instance)) {
2177 printk(KERN_WARNING "megaraid_sas: Failed to "
2178 "transition controller to ready.\n");
2179 continue;
2180 }
2181
2182 megasas_reset_reply_desc(instance);
2183 if (megasas_ioc_init_fusion(instance)) {
2184 printk(KERN_WARNING "megaraid_sas: "
2185 "megasas_ioc_init_fusion() failed!\n");
2186 continue;
2187 }
2188
2189 instance->instancet->enable_intr(instance->reg_set);
2190 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2191
2192 /* Re-fire management commands */
2193 for (j = 0 ; j < instance->max_fw_cmds; j++) {
2194 cmd_fusion = fusion->cmd_list[j];
2195 if (cmd_fusion->sync_cmd_idx !=
2196 (u32)ULONG_MAX) {
2197 cmd_mfi =
2198 instance->
2199 cmd_list[cmd_fusion->sync_cmd_idx];
2200 if (cmd_mfi->frame->dcmd.opcode ==
2201 MR_DCMD_LD_MAP_GET_INFO) {
2202 megasas_return_cmd(instance,
2203 cmd_mfi);
2204 megasas_return_cmd_fusion(
2205 instance, cmd_fusion);
2206 } else {
2207 req_desc =
2208 megasas_get_request_descriptor(
2209 instance,
2210 cmd_mfi->context.smid
2211 -1);
2212 if (!req_desc)
2213 printk(KERN_WARNING
2214 "req_desc NULL"
2215 "\n");
2216 else {
2217 instance->instancet->
2218 fire_cmd(instance,
2219 req_desc->
2220 u.low,
2221 req_desc->
2222 u.high,
2223 instance->
2224 reg_set);
2225 }
2226 }
2227 }
2228 }
2229
2230 /* Reset load balance info */
2231 memset(fusion->load_balance_info, 0,
2232 sizeof(struct LD_LOAD_BALANCE_INFO)
2233 *MAX_LOGICAL_DRIVES);
2234
2235 if (!megasas_get_map_info(instance))
2236 megasas_sync_map_info(instance);
2237
2238 /* Adapter reset completed successfully */
2239 printk(KERN_WARNING "megaraid_sas: Reset "
2240 "successful.\n");
2241 retval = SUCCESS;
2242 goto out;
2243 }
2244 /* Reset failed, kill the adapter */
2245 printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
2246 "adapter.\n");
2247 megaraid_sas_kill_hba(instance);
2248 retval = FAILED;
2249 } else {
2250 instance->instancet->enable_intr(instance->reg_set);
2251 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2252 }
2253out:
2254 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2255 mutex_unlock(&instance->reset_mutex);
2256 return retval;
2257}
2258
2259/* Fusion OCR work queue */
2260void megasas_fusion_ocr_wq(struct work_struct *work)
2261{
2262 struct megasas_instance *instance =
2263 container_of(work, struct megasas_instance, work_init);
2264
2265 megasas_reset_fusion(instance->host);
2266}
2267
2268struct megasas_instance_template megasas_instance_template_fusion = {
2269 .fire_cmd = megasas_fire_cmd_fusion,
2270 .enable_intr = megasas_enable_intr_fusion,
2271 .disable_intr = megasas_disable_intr_fusion,
2272 .clear_intr = megasas_clear_intr_fusion,
2273 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2274 .adp_reset = megasas_adp_reset_fusion,
2275 .check_reset = megasas_check_reset_fusion,
2276 .service_isr = megasas_isr_fusion,
2277 .tasklet = megasas_complete_cmd_dpc_fusion,
2278 .init_adapter = megasas_init_adapter_fusion,
2279 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2280 .issue_dcmd = megasas_issue_dcmd_fusion,
2281};