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