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