megaraid_sas : Use Block layer tag support for internal command indexing
[deliverable/linux.git] / drivers / scsi / megaraid / megaraid_sas_fusion.c
CommitLineData
9c915a8c 1/*
2 * Linux MegaRAID driver for SAS based RAID controllers
3 *
e399065b
SS
4 * Copyright (c) 2009-2013 LSI Corporation
5 * Copyright (c) 2013-2014 Avago Technologies
9c915a8c 6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
e399065b 18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
9c915a8c 19 *
20 * FILE: megaraid_sas_fusion.c
21 *
e399065b 22 * Authors: Avago Technologies
9c915a8c 23 * Sumant Patro
e399065b
SS
24 * Adam Radford
25 * Kashyap Desai <kashyap.desai@avagotech.com>
26 * Sumit Saxena <sumit.saxena@avagotech.com>
9c915a8c 27 *
e399065b 28 * Send feedback to: megaraidlinux.pdl@avagotech.com
9c915a8c 29 *
e399065b
SS
30 * Mail to: Avago Technologies, 350 West Trimble Road, Building 90,
31 * San Jose, California 95131
9c915a8c 32 */
33
34#include <linux/kernel.h>
35#include <linux/types.h>
36#include <linux/pci.h>
37#include <linux/list.h>
38#include <linux/moduleparam.h>
39#include <linux/module.h>
40#include <linux/spinlock.h>
41#include <linux/interrupt.h>
42#include <linux/delay.h>
9c915a8c 43#include <linux/uio.h>
44#include <linux/uaccess.h>
45#include <linux/fs.h>
46#include <linux/compat.h>
47#include <linux/blkdev.h>
48#include <linux/mutex.h>
49#include <linux/poll.h>
50
51#include <scsi/scsi.h>
52#include <scsi/scsi_cmnd.h>
53#include <scsi/scsi_device.h>
54#include <scsi/scsi_host.h>
90dc9d98 55#include <scsi/scsi_dbg.h>
9c915a8c 56
57#include "megaraid_sas_fusion.h"
58#include "megaraid_sas.h"
59
4026e9aa 60
9c915a8c 61extern void megasas_free_cmds(struct megasas_instance *instance);
62extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance
63 *instance);
64extern void
65megasas_complete_cmd(struct megasas_instance *instance,
66 struct megasas_cmd *cmd, u8 alt_status);
9c915a8c 67int
229fe47c 68wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
69 int seconds);
9c915a8c 70
71void
72megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd);
73int megasas_alloc_cmds(struct megasas_instance *instance);
74int
75megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs);
76int
77megasas_issue_polled(struct megasas_instance *instance,
78 struct megasas_cmd *cmd);
53ef2bbd 79void
80megasas_check_and_restore_queue_depth(struct megasas_instance *instance);
81
058a8fac 82int megasas_transition_to_ready(struct megasas_instance *instance, int ocr);
9c915a8c 83void megaraid_sas_kill_hba(struct megasas_instance *instance);
84
85extern u32 megasas_dbg_lvl;
229fe47c 86void megasas_sriov_heartbeat_handler(unsigned long instance_addr);
87int megasas_sriov_start_heartbeat(struct megasas_instance *instance,
88 int initial);
89void megasas_start_timer(struct megasas_instance *instance,
90 struct timer_list *timer,
91 void *fn, unsigned long interval);
92extern struct megasas_mgmt_info megasas_mgmt_info;
c007b8b2 93extern int resetwaittime;
9c915a8c 94
fc62b3fc
SS
95
96
9c915a8c 97/**
98 * megasas_enable_intr_fusion - Enables interrupts
99 * @regs: MFI register set
100 */
101void
d46a3ad6 102megasas_enable_intr_fusion(struct megasas_instance *instance)
9c915a8c 103{
d46a3ad6
SS
104 struct megasas_register_set __iomem *regs;
105 regs = instance->reg_set;
c2ced171
SS
106
107 instance->mask_interrupts = 0;
6497b247 108 /* For Thunderbolt/Invader also clear intr on enable */
109 writel(~0, &regs->outbound_intr_status);
110 readl(&regs->outbound_intr_status);
111
9c915a8c 112 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask);
113
114 /* Dummy readl to force pci flush */
115 readl(&regs->outbound_intr_mask);
116}
117
118/**
119 * megasas_disable_intr_fusion - Disables interrupt
120 * @regs: MFI register set
121 */
122void
d46a3ad6 123megasas_disable_intr_fusion(struct megasas_instance *instance)
9c915a8c 124{
125 u32 mask = 0xFFFFFFFF;
126 u32 status;
d46a3ad6
SS
127 struct megasas_register_set __iomem *regs;
128 regs = instance->reg_set;
129 instance->mask_interrupts = 1;
9c915a8c 130
131 writel(mask, &regs->outbound_intr_mask);
132 /* Dummy readl to force pci flush */
133 status = readl(&regs->outbound_intr_mask);
134}
135
136int
137megasas_clear_intr_fusion(struct megasas_register_set __iomem *regs)
138{
139 u32 status;
140 /*
141 * Check if it is our interrupt
142 */
143 status = readl(&regs->outbound_intr_status);
144
145 if (status & 1) {
146 writel(status, &regs->outbound_intr_status);
147 readl(&regs->outbound_intr_status);
148 return 1;
149 }
150 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK))
151 return 0;
152
9c915a8c 153 return 1;
154}
155
156/**
157 * megasas_get_cmd_fusion - Get a command from the free pool
158 * @instance: Adapter soft state
159 *
4026e9aa 160 * Returns a blk_tag indexed mpt frame
9c915a8c 161 */
4026e9aa
SS
162inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance
163 *instance, u32 blk_tag)
9c915a8c 164{
4026e9aa 165 struct fusion_context *fusion;
9c915a8c 166
4026e9aa
SS
167 fusion = instance->ctrl_context;
168 return fusion->cmd_list[blk_tag];
9c915a8c 169}
170
171/**
172 * megasas_return_cmd_fusion - Return a cmd to free command pool
173 * @instance: Adapter soft state
174 * @cmd: Command packet to be returned to free command pool
175 */
90dc9d98
SS
176inline void megasas_return_cmd_fusion(struct megasas_instance *instance,
177 struct megasas_cmd_fusion *cmd)
9c915a8c 178{
9c915a8c 179 cmd->scmd = NULL;
7497cde8 180 memset(cmd->io_request, 0, sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
9c915a8c 181}
182
183/**
184 * megasas_teardown_frame_pool_fusion - Destroy the cmd frame DMA pool
185 * @instance: Adapter soft state
186 */
187static void megasas_teardown_frame_pool_fusion(
188 struct megasas_instance *instance)
189{
190 int i;
191 struct fusion_context *fusion = instance->ctrl_context;
192
193 u16 max_cmd = instance->max_fw_cmds;
194
195 struct megasas_cmd_fusion *cmd;
196
197 if (!fusion->sg_dma_pool || !fusion->sense_dma_pool) {
198 printk(KERN_ERR "megasas: dma pool is null. SG Pool %p, "
199 "sense pool : %p\n", fusion->sg_dma_pool,
200 fusion->sense_dma_pool);
201 return;
202 }
203
204 /*
205 * Return all frames to pool
206 */
207 for (i = 0; i < max_cmd; i++) {
208
209 cmd = fusion->cmd_list[i];
210
211 if (cmd->sg_frame)
212 pci_pool_free(fusion->sg_dma_pool, cmd->sg_frame,
213 cmd->sg_frame_phys_addr);
214
215 if (cmd->sense)
216 pci_pool_free(fusion->sense_dma_pool, cmd->sense,
217 cmd->sense_phys_addr);
218 }
219
220 /*
221 * Now destroy the pool itself
222 */
223 pci_pool_destroy(fusion->sg_dma_pool);
224 pci_pool_destroy(fusion->sense_dma_pool);
225
226 fusion->sg_dma_pool = NULL;
227 fusion->sense_dma_pool = NULL;
228}
229
230/**
231 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool
232 * @instance: Adapter soft state
233 */
234void
235megasas_free_cmds_fusion(struct megasas_instance *instance)
236{
237 int i;
238 struct fusion_context *fusion = instance->ctrl_context;
239
240 u32 max_cmds, req_sz, reply_sz, io_frames_sz;
241
242
243 req_sz = fusion->request_alloc_sz;
244 reply_sz = fusion->reply_alloc_sz;
245 io_frames_sz = fusion->io_frames_alloc_sz;
246
247 max_cmds = instance->max_fw_cmds;
248
249 /* Free descriptors and request Frames memory */
250 if (fusion->req_frames_desc)
251 dma_free_coherent(&instance->pdev->dev, req_sz,
252 fusion->req_frames_desc,
253 fusion->req_frames_desc_phys);
254
255 if (fusion->reply_frames_desc) {
256 pci_pool_free(fusion->reply_frames_desc_pool,
257 fusion->reply_frames_desc,
258 fusion->reply_frames_desc_phys);
259 pci_pool_destroy(fusion->reply_frames_desc_pool);
260 }
261
262 if (fusion->io_request_frames) {
263 pci_pool_free(fusion->io_request_frames_pool,
264 fusion->io_request_frames,
265 fusion->io_request_frames_phys);
266 pci_pool_destroy(fusion->io_request_frames_pool);
267 }
268
269 /* Free the Fusion frame pool */
270 megasas_teardown_frame_pool_fusion(instance);
271
272 /* Free all the commands in the cmd_list */
273 for (i = 0; i < max_cmds; i++)
274 kfree(fusion->cmd_list[i]);
275
276 /* Free the cmd_list buffer itself */
277 kfree(fusion->cmd_list);
278 fusion->cmd_list = NULL;
279
9c915a8c 280}
281
282/**
283 * megasas_create_frame_pool_fusion - Creates DMA pool for cmd frames
284 * @instance: Adapter soft state
285 *
286 */
287static int megasas_create_frame_pool_fusion(struct megasas_instance *instance)
288{
289 int i;
290 u32 max_cmd;
291 struct fusion_context *fusion;
292 struct megasas_cmd_fusion *cmd;
293 u32 total_sz_chain_frame;
294
295 fusion = instance->ctrl_context;
296 max_cmd = instance->max_fw_cmds;
297
298 total_sz_chain_frame = MEGASAS_MAX_SZ_CHAIN_FRAME;
299
300 /*
301 * Use DMA pool facility provided by PCI layer
302 */
303
304 fusion->sg_dma_pool = pci_pool_create("megasas sg pool fusion",
305 instance->pdev,
306 total_sz_chain_frame, 4,
307 0);
308 if (!fusion->sg_dma_pool) {
309 printk(KERN_DEBUG "megasas: failed to setup request pool "
310 "fusion\n");
311 return -ENOMEM;
312 }
313 fusion->sense_dma_pool = pci_pool_create("megasas sense pool fusion",
314 instance->pdev,
315 SCSI_SENSE_BUFFERSIZE, 64, 0);
316
317 if (!fusion->sense_dma_pool) {
318 printk(KERN_DEBUG "megasas: failed to setup sense pool "
319 "fusion\n");
320 pci_pool_destroy(fusion->sg_dma_pool);
321 fusion->sg_dma_pool = NULL;
322 return -ENOMEM;
323 }
324
325 /*
326 * Allocate and attach a frame to each of the commands in cmd_list
327 */
328 for (i = 0; i < max_cmd; i++) {
329
330 cmd = fusion->cmd_list[i];
331
332 cmd->sg_frame = pci_pool_alloc(fusion->sg_dma_pool,
333 GFP_KERNEL,
334 &cmd->sg_frame_phys_addr);
335
336 cmd->sense = pci_pool_alloc(fusion->sense_dma_pool,
337 GFP_KERNEL, &cmd->sense_phys_addr);
338 /*
339 * megasas_teardown_frame_pool_fusion() takes care of freeing
340 * whatever has been allocated
341 */
342 if (!cmd->sg_frame || !cmd->sense) {
343 printk(KERN_DEBUG "megasas: pci_pool_alloc failed\n");
344 megasas_teardown_frame_pool_fusion(instance);
345 return -ENOMEM;
346 }
347 }
348 return 0;
349}
350
351/**
352 * megasas_alloc_cmds_fusion - Allocates the command packets
353 * @instance: Adapter soft state
354 *
355 *
356 * Each frame has a 32-bit field called context. This context is used to get
357 * back the megasas_cmd_fusion from the frame when a frame gets completed
358 * In this driver, the 32 bit values are the indices into an array cmd_list.
359 * This array is used only to look up the megasas_cmd_fusion given the context.
360 * The free commands themselves are maintained in a linked list called cmd_pool.
361 *
362 * cmds are formed in the io_request and sg_frame members of the
363 * megasas_cmd_fusion. The context field is used to get a request descriptor
364 * and is used as SMID of the cmd.
365 * SMID value range is from 1 to max_fw_cmds.
366 */
367int
368megasas_alloc_cmds_fusion(struct megasas_instance *instance)
369{
c8e858fe 370 int i, j, count;
9c915a8c 371 u32 max_cmd, io_frames_sz;
372 struct fusion_context *fusion;
373 struct megasas_cmd_fusion *cmd;
374 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
375 u32 offset;
376 dma_addr_t io_req_base_phys;
377 u8 *io_req_base;
378
379 fusion = instance->ctrl_context;
380
381 max_cmd = instance->max_fw_cmds;
382
383 fusion->req_frames_desc =
384 dma_alloc_coherent(&instance->pdev->dev,
385 fusion->request_alloc_sz,
386 &fusion->req_frames_desc_phys, GFP_KERNEL);
387
388 if (!fusion->req_frames_desc) {
389 printk(KERN_ERR "megasas; Could not allocate memory for "
390 "request_frames\n");
391 goto fail_req_desc;
392 }
393
c8e858fe 394 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
9c915a8c 395 fusion->reply_frames_desc_pool =
396 pci_pool_create("reply_frames pool", instance->pdev,
c8e858fe 397 fusion->reply_alloc_sz * count, 16, 0);
9c915a8c 398
399 if (!fusion->reply_frames_desc_pool) {
400 printk(KERN_ERR "megasas; Could not allocate memory for "
401 "reply_frame pool\n");
402 goto fail_reply_desc;
403 }
404
405 fusion->reply_frames_desc =
406 pci_pool_alloc(fusion->reply_frames_desc_pool, GFP_KERNEL,
407 &fusion->reply_frames_desc_phys);
408 if (!fusion->reply_frames_desc) {
409 printk(KERN_ERR "megasas; Could not allocate memory for "
410 "reply_frame pool\n");
411 pci_pool_destroy(fusion->reply_frames_desc_pool);
412 goto fail_reply_desc;
413 }
414
415 reply_desc = fusion->reply_frames_desc;
c8e858fe 416 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++)
9c915a8c 417 reply_desc->Words = ULLONG_MAX;
418
419 io_frames_sz = fusion->io_frames_alloc_sz;
420
421 fusion->io_request_frames_pool =
422 pci_pool_create("io_request_frames pool", instance->pdev,
423 fusion->io_frames_alloc_sz, 16, 0);
424
425 if (!fusion->io_request_frames_pool) {
426 printk(KERN_ERR "megasas: Could not allocate memory for "
427 "io_request_frame pool\n");
428 goto fail_io_frames;
429 }
430
431 fusion->io_request_frames =
432 pci_pool_alloc(fusion->io_request_frames_pool, GFP_KERNEL,
433 &fusion->io_request_frames_phys);
434 if (!fusion->io_request_frames) {
435 printk(KERN_ERR "megasas: Could not allocate memory for "
436 "io_request_frames frames\n");
437 pci_pool_destroy(fusion->io_request_frames_pool);
438 goto fail_io_frames;
439 }
440
441 /*
442 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers.
443 * Allocate the dynamic array first and then allocate individual
444 * commands.
445 */
7731e6bb
FW
446 fusion->cmd_list = kzalloc(sizeof(struct megasas_cmd_fusion *)
447 * max_cmd, GFP_KERNEL);
9c915a8c 448
449 if (!fusion->cmd_list) {
450 printk(KERN_DEBUG "megasas: out of memory. Could not alloc "
451 "memory for cmd_list_fusion\n");
452 goto fail_cmd_list;
453 }
454
9c915a8c 455 max_cmd = instance->max_fw_cmds;
456 for (i = 0; i < max_cmd; i++) {
457 fusion->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd_fusion),
458 GFP_KERNEL);
459 if (!fusion->cmd_list[i]) {
460 printk(KERN_ERR "Could not alloc cmd list fusion\n");
461
462 for (j = 0; j < i; j++)
463 kfree(fusion->cmd_list[j]);
464
465 kfree(fusion->cmd_list);
466 fusion->cmd_list = NULL;
467 goto fail_cmd_list;
468 }
469 }
470
471 /* The first 256 bytes (SMID 0) is not used. Don't add to cmd list */
472 io_req_base = fusion->io_request_frames +
473 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
474 io_req_base_phys = fusion->io_request_frames_phys +
475 MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE;
476
477 /*
478 * Add all the commands to command pool (fusion->cmd_pool)
479 */
480
481 /* SMID 0 is reserved. Set SMID/index from 1 */
482 for (i = 0; i < max_cmd; i++) {
483 cmd = fusion->cmd_list[i];
484 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i;
485 memset(cmd, 0, sizeof(struct megasas_cmd_fusion));
486 cmd->index = i + 1;
487 cmd->scmd = NULL;
4026e9aa
SS
488 cmd->sync_cmd_idx = (i >= instance->max_scsi_cmds) ?
489 (i - instance->max_scsi_cmds) :
490 (u32)ULONG_MAX; /* Set to Invalid */
9c915a8c 491 cmd->instance = instance;
492 cmd->io_request =
493 (struct MPI2_RAID_SCSI_IO_REQUEST *)
494 (io_req_base + offset);
495 memset(cmd->io_request, 0,
496 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST));
497 cmd->io_request_phys_addr = io_req_base_phys + offset;
9c915a8c 498 }
499
500 /*
501 * Create a frame pool and assign one frame to each cmd
502 */
503 if (megasas_create_frame_pool_fusion(instance)) {
504 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n");
505 megasas_free_cmds_fusion(instance);
506 goto fail_req_desc;
507 }
508
509 return 0;
510
511fail_cmd_list:
512 pci_pool_free(fusion->io_request_frames_pool, fusion->io_request_frames,
513 fusion->io_request_frames_phys);
514 pci_pool_destroy(fusion->io_request_frames_pool);
515fail_io_frames:
516 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
517 fusion->reply_frames_desc,
518 fusion->reply_frames_desc_phys);
519 pci_pool_free(fusion->reply_frames_desc_pool,
520 fusion->reply_frames_desc,
521 fusion->reply_frames_desc_phys);
522 pci_pool_destroy(fusion->reply_frames_desc_pool);
523
524fail_reply_desc:
525 dma_free_coherent(&instance->pdev->dev, fusion->request_alloc_sz,
526 fusion->req_frames_desc,
527 fusion->req_frames_desc_phys);
528fail_req_desc:
529 return -ENOMEM;
530}
531
532/**
533 * wait_and_poll - Issues a polling command
534 * @instance: Adapter soft state
535 * @cmd: Command packet to be issued
536 *
537 * For polling, MFI requires the cmd_status to be set to 0xFF before posting.
538 */
539int
229fe47c 540wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd,
541 int seconds)
9c915a8c 542{
543 int i;
544 struct megasas_header *frame_hdr = &cmd->frame->hdr;
90dc9d98 545 struct fusion_context *fusion;
9c915a8c 546
229fe47c 547 u32 msecs = seconds * 1000;
9c915a8c 548
90dc9d98 549 fusion = instance->ctrl_context;
9c915a8c 550 /*
551 * Wait for cmd_status to change
552 */
553 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) {
554 rmb();
555 msleep(20);
556 }
557
4026e9aa 558 if (frame_hdr->cmd_status == 0xff)
9c915a8c 559 return -ETIME;
560
561 return 0;
562}
563
564/**
565 * megasas_ioc_init_fusion - Initializes the FW
566 * @instance: Adapter soft state
567 *
568 * Issues the IOC Init cmd
569 */
570int
571megasas_ioc_init_fusion(struct megasas_instance *instance)
572{
573 struct megasas_init_frame *init_frame;
574 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage;
575 dma_addr_t ioc_init_handle;
9c915a8c 576 struct megasas_cmd *cmd;
577 u8 ret;
578 struct fusion_context *fusion;
c77a9bd8 579 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc;
9c915a8c 580 int i;
581 struct megasas_header *frame_hdr;
582
583 fusion = instance->ctrl_context;
584
585 cmd = megasas_get_cmd(instance);
586
587 if (!cmd) {
588 printk(KERN_ERR "Could not allocate cmd for INIT Frame\n");
589 ret = 1;
590 goto fail_get_cmd;
591 }
592
593 IOCInitMessage =
594 dma_alloc_coherent(&instance->pdev->dev,
595 sizeof(struct MPI2_IOC_INIT_REQUEST),
596 &ioc_init_handle, GFP_KERNEL);
597
598 if (!IOCInitMessage) {
599 printk(KERN_ERR "Could not allocate memory for "
600 "IOCInitMessage\n");
601 ret = 1;
602 goto fail_fw_init;
603 }
604
605 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST));
606
607 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT;
608 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER;
94cd65dd
SS
609 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION);
610 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION);
611 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4);
612
613 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth);
614 IOCInitMessage->ReplyDescriptorPostQueueAddress = cpu_to_le64(fusion->reply_frames_desc_phys);
615 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys);
5738f996 616 IOCInitMessage->HostMSIxVectors = instance->msix_vectors;
9c915a8c 617 init_frame = (struct megasas_init_frame *)cmd->frame;
618 memset(init_frame, 0, MEGAMFI_FRAME_SIZE);
619
620 frame_hdr = &cmd->frame->hdr;
9c915a8c 621 frame_hdr->cmd_status = 0xFF;
94cd65dd 622 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE);
9c915a8c 623
624 init_frame->cmd = MFI_CMD_INIT;
625 init_frame->cmd_status = 0xFF;
626
d46a3ad6
SS
627 /* driver support Extended MSIX */
628 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
629 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
630 init_frame->driver_operations.
631 mfi_capabilities.support_additional_msix = 1;
21c9e160 632 /* driver supports HA / Remote LUN over Fast Path interface */
633 init_frame->driver_operations.mfi_capabilities.support_fp_remote_lun
634 = 1;
51087a86
SS
635 init_frame->driver_operations.mfi_capabilities.support_max_255lds
636 = 1;
d2552ebe
SS
637 init_frame->driver_operations.mfi_capabilities.support_ndrive_r1_lb
638 = 1;
7497cde8
SS
639 init_frame->driver_operations.mfi_capabilities.security_protocol_cmds_fw
640 = 1;
94cd65dd
SS
641 /* Convert capability to LE32 */
642 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities);
21c9e160 643
fdc5a97c
HR
644 init_frame->queue_info_new_phys_addr_hi =
645 cpu_to_le32(upper_32_bits(ioc_init_handle));
646 init_frame->queue_info_new_phys_addr_lo =
647 cpu_to_le32(lower_32_bits(ioc_init_handle));
94cd65dd 648 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST));
9c915a8c 649
200aed58
SS
650 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr));
651 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr));
c77a9bd8 652 req_desc.MFAIo.RequestFlags =
9c915a8c 653 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA <<
200aed58 654 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
9c915a8c 655
656 /*
657 * disable the intr before firing the init frame
658 */
d46a3ad6 659 instance->instancet->disable_intr(instance);
9c915a8c 660
661 for (i = 0; i < (10 * 1000); i += 20) {
662 if (readl(&instance->reg_set->doorbell) & 1)
663 msleep(20);
664 else
665 break;
666 }
667
c77a9bd8 668 instance->instancet->fire_cmd(instance, req_desc.u.low,
669 req_desc.u.high, instance->reg_set);
9c915a8c 670
229fe47c 671 wait_and_poll(instance, cmd, MFI_POLL_TIMEOUT_SECS);
9c915a8c 672
673 frame_hdr = &cmd->frame->hdr;
674 if (frame_hdr->cmd_status != 0) {
675 ret = 1;
676 goto fail_fw_init;
677 }
678 printk(KERN_ERR "megasas:IOC Init cmd success\n");
679
680 ret = 0;
681
682fail_fw_init:
683 megasas_return_cmd(instance, cmd);
684 if (IOCInitMessage)
685 dma_free_coherent(&instance->pdev->dev,
686 sizeof(struct MPI2_IOC_INIT_REQUEST),
687 IOCInitMessage, ioc_init_handle);
688fail_get_cmd:
689 return ret;
690}
691
9c915a8c 692/*
693 * megasas_get_ld_map_info - Returns FW's ld_map structure
694 * @instance: Adapter soft state
695 * @pend: Pend the command or not
696 * Issues an internal command (DCMD) to get the FW's controller PD
697 * list structure. This information is mainly used to find out SYSTEM
698 * supported by the FW.
51087a86
SS
699 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO
700 * dcmd.mbox.b[0] - number of LDs being sync'd
701 * dcmd.mbox.b[1] - 0 - complete command immediately.
702 * - 1 - pend till config change
703 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP
704 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and
705 * uses extended struct MR_FW_RAID_MAP_EXT
9c915a8c 706 */
707static int
708megasas_get_ld_map_info(struct megasas_instance *instance)
709{
710 int ret = 0;
711 struct megasas_cmd *cmd;
712 struct megasas_dcmd_frame *dcmd;
51087a86 713 void *ci;
9c915a8c 714 dma_addr_t ci_h = 0;
715 u32 size_map_info;
716 struct fusion_context *fusion;
717
718 cmd = megasas_get_cmd(instance);
719
720 if (!cmd) {
721 printk(KERN_DEBUG "megasas: Failed to get cmd for map info.\n");
722 return -ENOMEM;
723 }
724
725 fusion = instance->ctrl_context;
726
727 if (!fusion) {
728 megasas_return_cmd(instance, cmd);
2f8bdfa8 729 return -ENXIO;
9c915a8c 730 }
731
732 dcmd = &cmd->frame->dcmd;
733
51087a86 734 size_map_info = fusion->current_map_sz;
9c915a8c 735
51087a86 736 ci = (void *) fusion->ld_map[(instance->map_id & 1)];
9c915a8c 737 ci_h = fusion->ld_map_phys[(instance->map_id & 1)];
738
739 if (!ci) {
740 printk(KERN_DEBUG "Failed to alloc mem for ld_map_info\n");
741 megasas_return_cmd(instance, cmd);
742 return -ENOMEM;
743 }
744
51087a86 745 memset(ci, 0, fusion->max_map_sz);
9c915a8c 746 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
51087a86
SS
747#if VD_EXT_DEBUG
748 dev_dbg(&instance->pdev->dev,
749 "%s sending MR_DCMD_LD_MAP_GET_INFO with size %d\n",
750 __func__, cpu_to_le32(size_map_info));
751#endif
9c915a8c 752 dcmd->cmd = MFI_CMD_DCMD;
753 dcmd->cmd_status = 0xFF;
754 dcmd->sge_count = 1;
94cd65dd 755 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_READ);
9c915a8c 756 dcmd->timeout = 0;
757 dcmd->pad_0 = 0;
94cd65dd
SS
758 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
759 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
760 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
761 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
9c915a8c 762
90dc9d98
SS
763 if (instance->ctrl_context && !instance->mask_interrupts)
764 ret = megasas_issue_blocked_cmd(instance, cmd,
765 MEGASAS_BLOCKED_CMD_TIMEOUT);
766 else
767 ret = megasas_issue_polled(instance, cmd);
9c915a8c 768
4026e9aa 769 megasas_return_cmd(instance, cmd);
9c915a8c 770
771 return ret;
772}
773
774u8
775megasas_get_map_info(struct megasas_instance *instance)
776{
777 struct fusion_context *fusion = instance->ctrl_context;
778
779 fusion->fast_path_io = 0;
780 if (!megasas_get_ld_map_info(instance)) {
bc93d425 781 if (MR_ValidateMapInfo(instance)) {
9c915a8c 782 fusion->fast_path_io = 1;
783 return 0;
784 }
785 }
786 return 1;
787}
788
789/*
790 * megasas_sync_map_info - Returns FW's ld_map structure
791 * @instance: Adapter soft state
792 *
793 * Issues an internal command (DCMD) to get the FW's controller PD
794 * list structure. This information is mainly used to find out SYSTEM
795 * supported by the FW.
796 */
797int
798megasas_sync_map_info(struct megasas_instance *instance)
799{
800 int ret = 0, i;
801 struct megasas_cmd *cmd;
802 struct megasas_dcmd_frame *dcmd;
803 u32 size_sync_info, num_lds;
804 struct fusion_context *fusion;
805 struct MR_LD_TARGET_SYNC *ci = NULL;
51087a86 806 struct MR_DRV_RAID_MAP_ALL *map;
9c915a8c 807 struct MR_LD_RAID *raid;
808 struct MR_LD_TARGET_SYNC *ld_sync;
809 dma_addr_t ci_h = 0;
810 u32 size_map_info;
811
812 cmd = megasas_get_cmd(instance);
813
814 if (!cmd) {
815 printk(KERN_DEBUG "megasas: Failed to get cmd for sync"
816 "info.\n");
817 return -ENOMEM;
818 }
819
820 fusion = instance->ctrl_context;
821
822 if (!fusion) {
823 megasas_return_cmd(instance, cmd);
824 return 1;
825 }
826
51087a86 827 map = fusion->ld_drv_map[instance->map_id & 1];
9c915a8c 828
6e755ddc 829 num_lds = le16_to_cpu(map->raidMap.ldCount);
9c915a8c 830
831 dcmd = &cmd->frame->dcmd;
832
833 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds;
834
835 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE);
836
837 ci = (struct MR_LD_TARGET_SYNC *)
838 fusion->ld_map[(instance->map_id - 1) & 1];
51087a86 839 memset(ci, 0, fusion->max_map_sz);
9c915a8c 840
841 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1];
842
843 ld_sync = (struct MR_LD_TARGET_SYNC *)ci;
844
845 for (i = 0; i < num_lds; i++, ld_sync++) {
846 raid = MR_LdRaidGet(i, map);
847 ld_sync->targetId = MR_GetLDTgtId(i, map);
848 ld_sync->seqNum = raid->seqNum;
849 }
850
51087a86 851 size_map_info = fusion->current_map_sz;
9c915a8c 852
853 dcmd->cmd = MFI_CMD_DCMD;
854 dcmd->cmd_status = 0xFF;
855 dcmd->sge_count = 1;
94cd65dd 856 dcmd->flags = cpu_to_le16(MFI_FRAME_DIR_WRITE);
9c915a8c 857 dcmd->timeout = 0;
858 dcmd->pad_0 = 0;
94cd65dd 859 dcmd->data_xfer_len = cpu_to_le32(size_map_info);
9c915a8c 860 dcmd->mbox.b[0] = num_lds;
861 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG;
94cd65dd
SS
862 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO);
863 dcmd->sgl.sge32[0].phys_addr = cpu_to_le32(ci_h);
864 dcmd->sgl.sge32[0].length = cpu_to_le32(size_map_info);
9c915a8c 865
866 instance->map_update_cmd = cmd;
867
868 instance->instancet->issue_dcmd(instance, cmd);
869
870 return ret;
871}
872
39b72c3c
SS
873/*
874 * meagasas_display_intel_branding - Display branding string
875 * @instance: per adapter object
876 *
877 * Return nothing.
878 */
879static void
880megasas_display_intel_branding(struct megasas_instance *instance)
881{
882 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL)
883 return;
884
885 switch (instance->pdev->device) {
886 case PCI_DEVICE_ID_LSI_INVADER:
887 switch (instance->pdev->subsystem_device) {
888 case MEGARAID_INTEL_RS3DC080_SSDID:
889 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
890 instance->host->host_no,
891 MEGARAID_INTEL_RS3DC080_BRANDING);
892 break;
893 case MEGARAID_INTEL_RS3DC040_SSDID:
894 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
895 instance->host->host_no,
896 MEGARAID_INTEL_RS3DC040_BRANDING);
897 break;
898 case MEGARAID_INTEL_RS3SC008_SSDID:
899 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
900 instance->host->host_no,
901 MEGARAID_INTEL_RS3SC008_BRANDING);
902 break;
903 case MEGARAID_INTEL_RS3MC044_SSDID:
904 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
905 instance->host->host_no,
906 MEGARAID_INTEL_RS3MC044_BRANDING);
907 break;
908 default:
909 break;
910 }
911 break;
912 case PCI_DEVICE_ID_LSI_FURY:
913 switch (instance->pdev->subsystem_device) {
914 case MEGARAID_INTEL_RS3WC080_SSDID:
915 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
916 instance->host->host_no,
917 MEGARAID_INTEL_RS3WC080_BRANDING);
918 break;
919 case MEGARAID_INTEL_RS3WC040_SSDID:
920 dev_info(&instance->pdev->dev, "scsi host %d: %s\n",
921 instance->host->host_no,
922 MEGARAID_INTEL_RS3WC040_BRANDING);
923 break;
924 default:
925 break;
926 }
927 break;
928 default:
929 break;
930 }
931}
932
9c915a8c 933/**
934 * megasas_init_adapter_fusion - Initializes the FW
935 * @instance: Adapter soft state
936 *
937 * This is the main function for initializing firmware.
938 */
939u32
940megasas_init_adapter_fusion(struct megasas_instance *instance)
941{
942 struct megasas_register_set __iomem *reg_set;
943 struct fusion_context *fusion;
944 u32 max_cmd;
c8e858fe 945 int i = 0, count;
9c915a8c 946
947 fusion = instance->ctrl_context;
948
949 reg_set = instance->reg_set;
950
951 /*
952 * Get various operational parameters from status register
953 */
954 instance->max_fw_cmds =
955 instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF;
956 instance->max_fw_cmds = min(instance->max_fw_cmds, (u16)1008);
957
958 /*
959 * Reduce the max supported cmds by 1. This is to ensure that the
960 * reply_q_sz (1 more than the max cmd that driver may send)
961 * does not exceed max cmds that the FW can support
962 */
963 instance->max_fw_cmds = instance->max_fw_cmds-1;
ae09a6c1
SS
964
965 /*
966 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames
967 */
968 instance->max_mfi_cmds =
969 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS;
9c915a8c 970
971 max_cmd = instance->max_fw_cmds;
972
db4fc864 973 fusion->reply_q_depth = 2 * (((max_cmd + 1 + 15)/16)*16);
9c915a8c 974
975 fusion->request_alloc_sz =
976 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *max_cmd;
977 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)
978 *(fusion->reply_q_depth);
979 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE +
980 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE *
981 (max_cmd + 1)); /* Extra 1 for SMID 0 */
982
983 fusion->max_sge_in_main_msg =
984 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
985 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16;
986
987 fusion->max_sge_in_chain =
988 MEGASAS_MAX_SZ_CHAIN_FRAME / sizeof(union MPI2_SGE_IO_UNION);
989
a5fd2858
SS
990 instance->max_num_sge = rounddown_pow_of_two(
991 fusion->max_sge_in_main_msg + fusion->max_sge_in_chain - 2);
9c915a8c 992
993 /* Used for pass thru MFI frame (DCMD) */
994 fusion->chain_offset_mfi_pthru =
995 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16;
996
997 fusion->chain_offset_io_request =
998 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE -
999 sizeof(union MPI2_SGE_IO_UNION))/16;
1000
c8e858fe 1001 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
1002 for (i = 0 ; i < count; i++)
1003 fusion->last_reply_idx[i] = 0;
9c915a8c 1004
f26ac3a1
SS
1005 /*
1006 * For fusion adapters, 3 commands for IOCTL and 5 commands
1007 * for driver's internal DCMDs.
1008 */
1009 instance->max_scsi_cmds = instance->max_fw_cmds -
1010 (MEGASAS_FUSION_INTERNAL_CMDS +
1011 MEGASAS_FUSION_IOCTL_CMDS);
1012 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS);
1013
9c915a8c 1014 /*
1015 * Allocate memory for descriptors
1016 * Create a pool of commands
1017 */
1018 if (megasas_alloc_cmds(instance))
1019 goto fail_alloc_mfi_cmds;
1020 if (megasas_alloc_cmds_fusion(instance))
1021 goto fail_alloc_cmds;
1022
1023 if (megasas_ioc_init_fusion(instance))
1024 goto fail_ioc_init;
1025
39b72c3c 1026 megasas_display_intel_branding(instance);
d009b576 1027 if (megasas_get_ctrl_info(instance)) {
51087a86
SS
1028 dev_err(&instance->pdev->dev,
1029 "Could not get controller info. Fail from %s %d\n",
1030 __func__, __LINE__);
1031 goto fail_ioc_init;
1032 }
1033
9c915a8c 1034 instance->flag_ieee = 1;
51087a86 1035 fusion->fast_path_io = 0;
9c915a8c 1036
51087a86
SS
1037 fusion->drv_map_pages = get_order(fusion->drv_map_sz);
1038 for (i = 0; i < 2; i++) {
1039 fusion->ld_map[i] = NULL;
1040 fusion->ld_drv_map[i] = (void *)__get_free_pages(GFP_KERNEL,
1041 fusion->drv_map_pages);
1042 if (!fusion->ld_drv_map[i]) {
1043 dev_err(&instance->pdev->dev, "Could not allocate "
1044 "memory for local map info for %d pages\n",
1045 fusion->drv_map_pages);
1046 if (i == 1)
1047 free_pages((ulong)fusion->ld_drv_map[0],
1048 fusion->drv_map_pages);
1049 goto fail_ioc_init;
1050 }
d009b576
SS
1051 memset(fusion->ld_drv_map[i], 0,
1052 ((1 << PAGE_SHIFT) << fusion->drv_map_pages));
51087a86
SS
1053 }
1054
9c915a8c 1055 for (i = 0; i < 2; i++) {
1056 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev,
51087a86 1057 fusion->max_map_sz,
9c915a8c 1058 &fusion->ld_map_phys[i],
1059 GFP_KERNEL);
1060 if (!fusion->ld_map[i]) {
1061 printk(KERN_ERR "megasas: Could not allocate memory "
1062 "for map info\n");
1063 goto fail_map_info;
1064 }
1065 }
1066
1067 if (!megasas_get_map_info(instance))
1068 megasas_sync_map_info(instance);
1069
1070 return 0;
1071
9c915a8c 1072fail_map_info:
1073 if (i == 1)
51087a86 1074 dma_free_coherent(&instance->pdev->dev, fusion->max_map_sz,
9c915a8c 1075 fusion->ld_map[0], fusion->ld_map_phys[0]);
1076fail_ioc_init:
eb1b1237 1077 megasas_free_cmds_fusion(instance);
1078fail_alloc_cmds:
1079 megasas_free_cmds(instance);
1080fail_alloc_mfi_cmds:
9c915a8c 1081 return 1;
1082}
1083
1084/**
1085 * megasas_fire_cmd_fusion - Sends command to the FW
1086 * @frame_phys_addr : Physical address of cmd
1087 * @frame_count : Number of frames for the command
1088 * @regs : MFI register set
1089 */
1090void
1091megasas_fire_cmd_fusion(struct megasas_instance *instance,
1092 dma_addr_t req_desc_lo,
1093 u32 req_desc_hi,
1094 struct megasas_register_set __iomem *regs)
1095{
07560409 1096#if defined(writeq) && defined(CONFIG_64BIT)
6e755ddc
SS
1097 u64 req_data = (((u64)le32_to_cpu(req_desc_hi) << 32) |
1098 le32_to_cpu(req_desc_lo));
07560409 1099
6e755ddc 1100 writeq(req_data, &(regs)->inbound_low_queue_port);
07560409 1101#else
9c915a8c 1102 unsigned long flags;
1103
1104 spin_lock_irqsave(&instance->hba_lock, flags);
1105
94cd65dd
SS
1106 writel(le32_to_cpu(req_desc_lo), &(regs)->inbound_low_queue_port);
1107 writel(le32_to_cpu(req_desc_hi), &(regs)->inbound_high_queue_port);
9c915a8c 1108 spin_unlock_irqrestore(&instance->hba_lock, flags);
07560409 1109#endif
9c915a8c 1110}
1111
1112/**
1113 * map_cmd_status - Maps FW cmd status to OS cmd status
1114 * @cmd : Pointer to cmd
1115 * @status : status of cmd returned by FW
1116 * @ext_status : ext status of cmd returned by FW
1117 */
1118
1119void
1120map_cmd_status(struct megasas_cmd_fusion *cmd, u8 status, u8 ext_status)
1121{
1122
1123 switch (status) {
1124
1125 case MFI_STAT_OK:
1126 cmd->scmd->result = DID_OK << 16;
1127 break;
1128
1129 case MFI_STAT_SCSI_IO_FAILED:
1130 case MFI_STAT_LD_INIT_IN_PROGRESS:
1131 cmd->scmd->result = (DID_ERROR << 16) | ext_status;
1132 break;
1133
1134 case MFI_STAT_SCSI_DONE_WITH_ERROR:
1135
1136 cmd->scmd->result = (DID_OK << 16) | ext_status;
1137 if (ext_status == SAM_STAT_CHECK_CONDITION) {
1138 memset(cmd->scmd->sense_buffer, 0,
1139 SCSI_SENSE_BUFFERSIZE);
1140 memcpy(cmd->scmd->sense_buffer, cmd->sense,
1141 SCSI_SENSE_BUFFERSIZE);
1142 cmd->scmd->result |= DRIVER_SENSE << 24;
1143 }
1144 break;
1145
1146 case MFI_STAT_LD_OFFLINE:
1147 case MFI_STAT_DEVICE_NOT_FOUND:
1148 cmd->scmd->result = DID_BAD_TARGET << 16;
1149 break;
36807e67 1150 case MFI_STAT_CONFIG_SEQ_MISMATCH:
1151 cmd->scmd->result = DID_IMM_RETRY << 16;
1152 break;
9c915a8c 1153 default:
1154 printk(KERN_DEBUG "megasas: FW status %#x\n", status);
1155 cmd->scmd->result = DID_ERROR << 16;
1156 break;
1157 }
1158}
1159
1160/**
1161 * megasas_make_sgl_fusion - Prepares 32-bit SGL
1162 * @instance: Adapter soft state
1163 * @scp: SCSI command from the mid-layer
1164 * @sgl_ptr: SGL to be filled in
1165 * @cmd: cmd we are working on
1166 *
1167 * If successful, this function returns the number of SG elements.
1168 */
1169static int
1170megasas_make_sgl_fusion(struct megasas_instance *instance,
1171 struct scsi_cmnd *scp,
1172 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr,
1173 struct megasas_cmd_fusion *cmd)
1174{
36807e67 1175 int i, sg_processed, sge_count;
9c915a8c 1176 struct scatterlist *os_sgl;
1177 struct fusion_context *fusion;
1178
1179 fusion = instance->ctrl_context;
1180
21d3c710
SS
1181 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1182 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
36807e67 1183 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr;
1184 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
1185 sgl_ptr_end->Flags = 0;
1186 }
9c915a8c 1187
1188 sge_count = scsi_dma_map(scp);
1189
1190 BUG_ON(sge_count < 0);
1191
1192 if (sge_count > instance->max_num_sge || !sge_count)
1193 return sge_count;
1194
9c915a8c 1195 scsi_for_each_sg(scp, os_sgl, sge_count, i) {
94cd65dd
SS
1196 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl));
1197 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl));
9c915a8c 1198 sgl_ptr->Flags = 0;
21d3c710
SS
1199 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1200 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
36807e67 1201 if (i == sge_count - 1)
1202 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1203 }
9c915a8c 1204 sgl_ptr++;
1205
1206 sg_processed = i + 1;
1207
1208 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) &&
1209 (sge_count > fusion->max_sge_in_main_msg)) {
1210
1211 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain;
21d3c710
SS
1212 if ((instance->pdev->device ==
1213 PCI_DEVICE_ID_LSI_INVADER) ||
1214 (instance->pdev->device ==
1215 PCI_DEVICE_ID_LSI_FURY)) {
94cd65dd
SS
1216 if ((le16_to_cpu(cmd->io_request->IoFlags) &
1217 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) !=
1218 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
36807e67 1219 cmd->io_request->ChainOffset =
1220 fusion->
1221 chain_offset_io_request;
1222 else
1223 cmd->io_request->ChainOffset = 0;
1224 } else
1225 cmd->io_request->ChainOffset =
1226 fusion->chain_offset_io_request;
1227
9c915a8c 1228 sg_chain = sgl_ptr;
1229 /* Prepare chain element */
1230 sg_chain->NextChainOffset = 0;
21d3c710
SS
1231 if ((instance->pdev->device ==
1232 PCI_DEVICE_ID_LSI_INVADER) ||
1233 (instance->pdev->device ==
1234 PCI_DEVICE_ID_LSI_FURY))
36807e67 1235 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1236 else
1237 sg_chain->Flags =
1238 (IEEE_SGE_FLAGS_CHAIN_ELEMENT |
1239 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
94cd65dd
SS
1240 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed)));
1241 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr);
9c915a8c 1242
1243 sgl_ptr =
1244 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame;
7497cde8 1245 memset(sgl_ptr, 0, MEGASAS_MAX_SZ_CHAIN_FRAME);
9c915a8c 1246 }
1247 }
1248
1249 return sge_count;
1250}
1251
1252/**
1253 * megasas_set_pd_lba - Sets PD LBA
1254 * @cdb: CDB
1255 * @cdb_len: cdb length
1256 * @start_blk: Start block of IO
1257 *
1258 * Used to set the PD LBA in CDB for FP IOs
1259 */
1260void
1261megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len,
1262 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp,
51087a86 1263 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag)
9c915a8c 1264{
1265 struct MR_LD_RAID *raid;
1266 u32 ld;
1267 u64 start_blk = io_info->pdBlock;
1268 u8 *cdb = io_request->CDB.CDB32;
1269 u32 num_blocks = io_info->numBlocks;
495c5609 1270 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0;
9c915a8c 1271
1272 /* Check if T10 PI (DIF) is enabled for this LD */
1273 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr);
1274 raid = MR_LdRaidGet(ld, local_map_ptr);
1275 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) {
1276 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1277 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD;
1278 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN;
1279
1280 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1281 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32;
1282 else
1283 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32;
1284 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL;
1285
1286 /* LBA */
1287 cdb[12] = (u8)((start_blk >> 56) & 0xff);
1288 cdb[13] = (u8)((start_blk >> 48) & 0xff);
1289 cdb[14] = (u8)((start_blk >> 40) & 0xff);
1290 cdb[15] = (u8)((start_blk >> 32) & 0xff);
1291 cdb[16] = (u8)((start_blk >> 24) & 0xff);
1292 cdb[17] = (u8)((start_blk >> 16) & 0xff);
1293 cdb[18] = (u8)((start_blk >> 8) & 0xff);
1294 cdb[19] = (u8)(start_blk & 0xff);
1295
1296 /* Logical block reference tag */
1297 io_request->CDB.EEDP32.PrimaryReferenceTag =
1298 cpu_to_be32(ref_tag);
6e755ddc 1299 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff);
94cd65dd 1300 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */
9c915a8c 1301
1302 /* Transfer length */
1303 cdb[28] = (u8)((num_blocks >> 24) & 0xff);
1304 cdb[29] = (u8)((num_blocks >> 16) & 0xff);
1305 cdb[30] = (u8)((num_blocks >> 8) & 0xff);
1306 cdb[31] = (u8)(num_blocks & 0xff);
1307
1308 /* set SCSI IO EEDPFlags */
1309 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) {
94cd65dd 1310 io_request->EEDPFlags = cpu_to_le16(
9c915a8c 1311 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1312 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1313 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP |
1314 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG |
94cd65dd 1315 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
9c915a8c 1316 } else {
94cd65dd 1317 io_request->EEDPFlags = cpu_to_le16(
9c915a8c 1318 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
94cd65dd 1319 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP);
9c915a8c 1320 }
94cd65dd
SS
1321 io_request->Control |= cpu_to_le32((0x4 << 26));
1322 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size);
9c915a8c 1323 } else {
1324 /* Some drives don't support 16/12 byte CDB's, convert to 10 */
1325 if (((cdb_len == 12) || (cdb_len == 16)) &&
1326 (start_blk <= 0xffffffff)) {
1327 if (cdb_len == 16) {
1328 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10;
1329 flagvals = cdb[1];
1330 groupnum = cdb[14];
1331 control = cdb[15];
1332 } else {
1333 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10;
1334 flagvals = cdb[1];
1335 groupnum = cdb[10];
1336 control = cdb[11];
1337 }
1338
1339 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1340
1341 cdb[0] = opcode;
1342 cdb[1] = flagvals;
1343 cdb[6] = groupnum;
1344 cdb[9] = control;
1345
1346 /* Transfer length */
1347 cdb[8] = (u8)(num_blocks & 0xff);
1348 cdb[7] = (u8)((num_blocks >> 8) & 0xff);
1349
94cd65dd 1350 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */
9c915a8c 1351 cdb_len = 10;
495c5609 1352 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) {
1353 /* Convert to 16 byte CDB for large LBA's */
1354 switch (cdb_len) {
1355 case 6:
1356 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16;
1357 control = cdb[5];
1358 break;
1359 case 10:
1360 opcode =
1361 cdb[0] == READ_10 ? READ_16 : WRITE_16;
1362 flagvals = cdb[1];
1363 groupnum = cdb[6];
1364 control = cdb[9];
1365 break;
1366 case 12:
1367 opcode =
1368 cdb[0] == READ_12 ? READ_16 : WRITE_16;
1369 flagvals = cdb[1];
1370 groupnum = cdb[10];
1371 control = cdb[11];
1372 break;
1373 }
1374
1375 memset(cdb, 0, sizeof(io_request->CDB.CDB32));
1376
1377 cdb[0] = opcode;
1378 cdb[1] = flagvals;
1379 cdb[14] = groupnum;
1380 cdb[15] = control;
1381
1382 /* Transfer length */
1383 cdb[13] = (u8)(num_blocks & 0xff);
1384 cdb[12] = (u8)((num_blocks >> 8) & 0xff);
1385 cdb[11] = (u8)((num_blocks >> 16) & 0xff);
1386 cdb[10] = (u8)((num_blocks >> 24) & 0xff);
1387
94cd65dd 1388 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */
495c5609 1389 cdb_len = 16;
9c915a8c 1390 }
1391
1392 /* Normal case, just load LBA here */
1393 switch (cdb_len) {
1394 case 6:
1395 {
1396 u8 val = cdb[1] & 0xE0;
1397 cdb[3] = (u8)(start_blk & 0xff);
1398 cdb[2] = (u8)((start_blk >> 8) & 0xff);
1399 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f);
1400 break;
1401 }
1402 case 10:
1403 cdb[5] = (u8)(start_blk & 0xff);
1404 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1405 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1406 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1407 break;
1408 case 12:
1409 cdb[5] = (u8)(start_blk & 0xff);
1410 cdb[4] = (u8)((start_blk >> 8) & 0xff);
1411 cdb[3] = (u8)((start_blk >> 16) & 0xff);
1412 cdb[2] = (u8)((start_blk >> 24) & 0xff);
1413 break;
1414 case 16:
1415 cdb[9] = (u8)(start_blk & 0xff);
1416 cdb[8] = (u8)((start_blk >> 8) & 0xff);
1417 cdb[7] = (u8)((start_blk >> 16) & 0xff);
1418 cdb[6] = (u8)((start_blk >> 24) & 0xff);
1419 cdb[5] = (u8)((start_blk >> 32) & 0xff);
1420 cdb[4] = (u8)((start_blk >> 40) & 0xff);
1421 cdb[3] = (u8)((start_blk >> 48) & 0xff);
1422 cdb[2] = (u8)((start_blk >> 56) & 0xff);
1423 break;
1424 }
1425 }
1426}
1427
1428/**
1429 * megasas_build_ldio_fusion - Prepares IOs to devices
1430 * @instance: Adapter soft state
1431 * @scp: SCSI command
1432 * @cmd: Command to be prepared
1433 *
1434 * Prepares the io_request and chain elements (sg_frame) for IO
1435 * The IO can be for PD (Fast Path) or LD
1436 */
1437void
1438megasas_build_ldio_fusion(struct megasas_instance *instance,
1439 struct scsi_cmnd *scp,
1440 struct megasas_cmd_fusion *cmd)
1441{
1442 u8 fp_possible;
f9eff815 1443 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0;
9c915a8c 1444 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1445 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1446 struct IO_REQUEST_INFO io_info;
1447 struct fusion_context *fusion;
51087a86 1448 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
21c9e160 1449 u8 *raidLUN;
9c915a8c 1450
4a5c814d 1451 device_id = MEGASAS_DEV_INDEX(scp);
9c915a8c 1452
1453 fusion = instance->ctrl_context;
1454
1455 io_request = cmd->io_request;
94cd65dd 1456 io_request->RaidContext.VirtualDiskTgtId = cpu_to_le16(device_id);
9c915a8c 1457 io_request->RaidContext.status = 0;
1458 io_request->RaidContext.exStatus = 0;
1459
1460 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc;
1461
1462 start_lba_lo = 0;
1463 start_lba_hi = 0;
1464 fp_possible = 0;
1465
1466 /*
1467 * 6-byte READ(0x08) or WRITE(0x0A) cdb
1468 */
1469 if (scp->cmd_len == 6) {
f9eff815 1470 datalength = (u32) scp->cmnd[4];
9c915a8c 1471 start_lba_lo = ((u32) scp->cmnd[1] << 16) |
1472 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3];
1473
1474 start_lba_lo &= 0x1FFFFF;
1475 }
1476
1477 /*
1478 * 10-byte READ(0x28) or WRITE(0x2A) cdb
1479 */
1480 else if (scp->cmd_len == 10) {
f9eff815 1481 datalength = (u32) scp->cmnd[8] |
9c915a8c 1482 ((u32) scp->cmnd[7] << 8);
1483 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1484 ((u32) scp->cmnd[3] << 16) |
1485 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1486 }
1487
1488 /*
1489 * 12-byte READ(0xA8) or WRITE(0xAA) cdb
1490 */
1491 else if (scp->cmd_len == 12) {
f9eff815 1492 datalength = ((u32) scp->cmnd[6] << 24) |
9c915a8c 1493 ((u32) scp->cmnd[7] << 16) |
1494 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1495 start_lba_lo = ((u32) scp->cmnd[2] << 24) |
1496 ((u32) scp->cmnd[3] << 16) |
1497 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1498 }
1499
1500 /*
1501 * 16-byte READ(0x88) or WRITE(0x8A) cdb
1502 */
1503 else if (scp->cmd_len == 16) {
f9eff815 1504 datalength = ((u32) scp->cmnd[10] << 24) |
9c915a8c 1505 ((u32) scp->cmnd[11] << 16) |
1506 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13];
1507 start_lba_lo = ((u32) scp->cmnd[6] << 24) |
1508 ((u32) scp->cmnd[7] << 16) |
1509 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9];
1510
1511 start_lba_hi = ((u32) scp->cmnd[2] << 24) |
1512 ((u32) scp->cmnd[3] << 16) |
1513 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5];
1514 }
1515
1516 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
1517 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo;
f9eff815 1518 io_info.numBlocks = datalength;
9c915a8c 1519 io_info.ldTgtId = device_id;
94cd65dd 1520 io_request->DataLength = cpu_to_le32(scsi_bufflen(scp));
9c915a8c 1521
1522 if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
1523 io_info.isRead = 1;
1524
51087a86 1525 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
9c915a8c 1526
1527 if ((MR_TargetIdToLdGet(device_id, local_map_ptr) >=
51087a86 1528 instance->fw_supported_vd_count) || (!fusion->fast_path_io)) {
9c915a8c 1529 io_request->RaidContext.regLockFlags = 0;
1530 fp_possible = 0;
1531 } else {
36807e67 1532 if (MR_BuildRaidContext(instance, &io_info,
1533 &io_request->RaidContext,
21c9e160 1534 local_map_ptr, &raidLUN))
9c915a8c 1535 fp_possible = io_info.fpOkForIo;
1536 }
1537
16b8528d 1538 /* Use raw_smp_processor_id() for now until cmd->request->cpu is CPU
c8e858fe 1539 id by default, not CPU group id, otherwise all MSI-X queues won't
1540 be utilized */
1541 cmd->request_desc->SCSIIO.MSIxIndex = instance->msix_vectors ?
16b8528d 1542 raw_smp_processor_id() % instance->msix_vectors : 0;
c8e858fe 1543
9c915a8c 1544 if (fp_possible) {
1545 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp,
1546 local_map_ptr, start_lba_lo);
9c915a8c 1547 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1548 cmd->request_desc->SCSIIO.RequestFlags =
1549 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
1550 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
21d3c710
SS
1551 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1552 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
36807e67 1553 if (io_request->RaidContext.regLockFlags ==
1554 REGION_TYPE_UNUSED)
1555 cmd->request_desc->SCSIIO.RequestFlags =
1556 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1557 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1558 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1559 io_request->RaidContext.nseg = 0x1;
94cd65dd 1560 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
36807e67 1561 io_request->RaidContext.regLockFlags |=
1562 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
1563 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1564 }
9c915a8c 1565 if ((fusion->load_balance_info[device_id].loadBalanceFlag) &&
1566 (io_info.isRead)) {
1567 io_info.devHandle =
d2552ebe 1568 get_updated_dev_handle(instance,
9c915a8c 1569 &fusion->load_balance_info[device_id],
1570 &io_info);
1571 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG;
d2552ebe 1572 cmd->pd_r1_lb = io_info.pd_after_lb;
9c915a8c 1573 } else
1574 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG;
1575 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
1576 io_request->DevHandle = io_info.devHandle;
21c9e160 1577 /* populate the LUN field */
1578 memcpy(io_request->LUN, raidLUN, 8);
9c915a8c 1579 } else {
1580 io_request->RaidContext.timeoutValue =
94cd65dd 1581 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec);
9c915a8c 1582 cmd->request_desc->SCSIIO.RequestFlags =
1583 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO
1584 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
21d3c710
SS
1585 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1586 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
36807e67 1587 if (io_request->RaidContext.regLockFlags ==
1588 REGION_TYPE_UNUSED)
1589 cmd->request_desc->SCSIIO.RequestFlags =
1590 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1591 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1592 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1593 io_request->RaidContext.regLockFlags |=
1594 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
1595 MR_RL_FLAGS_SEQ_NUM_ENABLE);
1596 io_request->RaidContext.nseg = 0x1;
1597 }
1598 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
94cd65dd 1599 io_request->DevHandle = cpu_to_le16(device_id);
9c915a8c 1600 } /* Not FP */
1601}
1602
1603/**
4a5c814d 1604 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk
9c915a8c 1605 * @instance: Adapter soft state
1606 * @scp: SCSI command
1607 * @cmd: Command to be prepared
1608 *
4a5c814d 1609 * Prepares the io_request frame for non-rw io cmds for vd.
9c915a8c 1610 */
4a5c814d
SS
1611static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance,
1612 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd)
9c915a8c 1613{
1614 u32 device_id;
1615 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1616 u16 pd_index = 0;
51087a86 1617 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
9c915a8c 1618 struct fusion_context *fusion = instance->ctrl_context;
21c9e160 1619 u8 span, physArm;
1620 u16 devHandle;
1621 u32 ld, arRef, pd;
1622 struct MR_LD_RAID *raid;
1623 struct RAID_CONTEXT *pRAID_Context;
4a5c814d 1624 u8 fp_possible = 1;
9c915a8c 1625
1626 io_request = cmd->io_request;
4a5c814d
SS
1627 device_id = MEGASAS_DEV_INDEX(scmd);
1628 pd_index = MEGASAS_PD_INDEX(scmd);
51087a86 1629 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
94cd65dd 1630 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
4a5c814d
SS
1631 /* get RAID_Context pointer */
1632 pRAID_Context = &io_request->RaidContext;
1633 /* Check with FW team */
1634 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
1635 pRAID_Context->regLockRowLBA = 0;
1636 pRAID_Context->regLockLength = 0;
21c9e160 1637
4a5c814d
SS
1638 if (fusion->fast_path_io && (
1639 device_id < instance->fw_supported_vd_count)) {
ab2f0608 1640
21c9e160 1641 ld = MR_TargetIdToLdGet(device_id, local_map_ptr);
ab2f0608 1642 if (ld >= instance->fw_supported_vd_count)
4a5c814d 1643 fp_possible = 0;
21c9e160 1644
1645 raid = MR_LdRaidGet(ld, local_map_ptr);
21c9e160 1646 if (!(raid->capability.fpNonRWCapable))
4a5c814d
SS
1647 fp_possible = 0;
1648 } else
1649 fp_possible = 0;
21c9e160 1650
4a5c814d
SS
1651 if (!fp_possible) {
1652 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
1653 io_request->DevHandle = cpu_to_le16(device_id);
1654 io_request->LUN[1] = scmd->device->lun;
1655 pRAID_Context->timeoutValue =
1656 cpu_to_le16 (scmd->request->timeout / HZ);
1657 cmd->request_desc->SCSIIO.RequestFlags =
1658 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1659 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1660 } else {
21c9e160 1661
1662 /* set RAID context values */
4a5c814d
SS
1663 pRAID_Context->configSeqNum = raid->seqNum;
1664 pRAID_Context->regLockFlags = REGION_TYPE_SHARED_READ;
1665 pRAID_Context->timeoutValue = cpu_to_le16(raid->fpIoTimeoutForLd);
21c9e160 1666
1667 /* get the DevHandle for the PD (since this is
1668 fpNonRWCapable, this is a single disk RAID0) */
1669 span = physArm = 0;
1670 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr);
1671 pd = MR_ArPdGet(arRef, physArm, local_map_ptr);
1672 devHandle = MR_PdDevHandleGet(pd, local_map_ptr);
1673
1674 /* build request descriptor */
1675 cmd->request_desc->SCSIIO.RequestFlags =
1676 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
4a5c814d 1677 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
21c9e160 1678 cmd->request_desc->SCSIIO.DevHandle = devHandle;
1679
1680 /* populate the LUN field */
1681 memcpy(io_request->LUN, raid->LUN, 8);
1682
1683 /* build the raidScsiIO structure */
1684 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1685 io_request->DevHandle = devHandle;
4a5c814d
SS
1686 }
1687}
21c9e160 1688
4a5c814d
SS
1689/**
1690 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd
1691 * @instance: Adapter soft state
1692 * @scp: SCSI command
1693 * @cmd: Command to be prepared
1694 * @fp_possible: parameter to detect fast path or firmware path io.
1695 *
1696 * Prepares the io_request frame for rw/non-rw io cmds for syspds
1697 */
1698static void
1699megasas_build_syspd_fusion(struct megasas_instance *instance,
1700 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd, u8 fp_possible)
1701{
1702 u32 device_id;
1703 struct MPI2_RAID_SCSI_IO_REQUEST *io_request;
1704 u16 pd_index = 0;
1705 u16 os_timeout_value;
1706 u16 timeout_limit;
1707 struct MR_DRV_RAID_MAP_ALL *local_map_ptr;
1708 struct RAID_CONTEXT *pRAID_Context;
1709 struct fusion_context *fusion = instance->ctrl_context;
1710
1711 device_id = MEGASAS_DEV_INDEX(scmd);
1712 pd_index = MEGASAS_PD_INDEX(scmd);
1713 os_timeout_value = scmd->request->timeout / HZ;
21c9e160 1714
4a5c814d
SS
1715 io_request = cmd->io_request;
1716 /* get RAID_Context pointer */
1717 pRAID_Context = &io_request->RaidContext;
1718 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd));
1719 io_request->LUN[1] = scmd->device->lun;
1720 pRAID_Context->RAIDFlags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
1721 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1722
1723 pRAID_Context->VirtualDiskTgtId = cpu_to_le16(device_id);
1724 pRAID_Context->configSeqNum = 0;
1725 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)];
1726 io_request->DevHandle =
1727 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1728
1729 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
1730 cmd->request_desc->SCSIIO.MSIxIndex =
1731 instance->msix_vectors ?
1732 (raw_smp_processor_id() % instance->msix_vectors) : 0;
1733
1734
1735 if (!fp_possible) {
1736 /* system pd firmware path */
9c915a8c 1737 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST;
9c915a8c 1738 cmd->request_desc->SCSIIO.RequestFlags =
1739 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
4a5c814d
SS
1740 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1741 pRAID_Context->timeoutValue = cpu_to_le16(os_timeout_value);
1742 } else {
1743 /* system pd Fast Path */
1744 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1745 pRAID_Context->regLockFlags = 0;
1746 pRAID_Context->regLockRowLBA = 0;
1747 pRAID_Context->regLockLength = 0;
1748 timeout_limit = (scmd->device->type == TYPE_DISK) ?
1749 255 : 0xFFFF;
1750 pRAID_Context->timeoutValue =
1751 cpu_to_le16((os_timeout_value > timeout_limit) ?
1752 timeout_limit : os_timeout_value);
1753 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
1754 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
1755 cmd->request_desc->SCSIIO.RequestFlags |=
1756 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
1757 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1758 pRAID_Context->Type = MPI2_TYPE_CUDA;
1759 pRAID_Context->nseg = 0x1;
1760 io_request->IoFlags |=
1761 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH);
1762 }
1763 cmd->request_desc->SCSIIO.RequestFlags =
1764 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1765 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
9c915a8c 1766 }
9c915a8c 1767}
1768
1769/**
1770 * megasas_build_io_fusion - Prepares IOs to devices
1771 * @instance: Adapter soft state
1772 * @scp: SCSI command
1773 * @cmd: Command to be prepared
1774 *
1775 * Invokes helper functions to prepare request frames
1776 * and sets flags appropriate for IO/Non-IO cmd
1777 */
1778int
1779megasas_build_io_fusion(struct megasas_instance *instance,
1780 struct scsi_cmnd *scp,
1781 struct megasas_cmd_fusion *cmd)
1782{
4a5c814d
SS
1783 u32 sge_count;
1784 u8 cmd_type;
9c915a8c 1785 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request;
1786
9c915a8c 1787 /* Zero out some fields so they don't get reused */
9cb78c16 1788 memset(io_request->LUN, 0x0, 8);
9c915a8c 1789 io_request->CDB.EEDP32.PrimaryReferenceTag = 0;
1790 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0;
1791 io_request->EEDPFlags = 0;
1792 io_request->Control = 0;
1793 io_request->EEDPBlockSize = 0;
36807e67 1794 io_request->ChainOffset = 0;
9c915a8c 1795 io_request->RaidContext.RAIDFlags = 0;
36807e67 1796 io_request->RaidContext.Type = 0;
1797 io_request->RaidContext.nseg = 0;
9c915a8c 1798
1799 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len);
1800 /*
1801 * Just the CDB length,rest of the Flags are zero
1802 * This will be modified for FP in build_ldio_fusion
1803 */
94cd65dd 1804 io_request->IoFlags = cpu_to_le16(scp->cmd_len);
9c915a8c 1805
4a5c814d
SS
1806 switch (cmd_type = megasas_cmd_type(scp)) {
1807 case READ_WRITE_LDIO:
9c915a8c 1808 megasas_build_ldio_fusion(instance, scp, cmd);
4a5c814d
SS
1809 break;
1810 case NON_READ_WRITE_LDIO:
1811 megasas_build_ld_nonrw_fusion(instance, scp, cmd);
1812 break;
1813 case READ_WRITE_SYSPDIO:
1814 case NON_READ_WRITE_SYSPDIO:
1815 if (instance->secure_jbod_support &&
1816 (cmd_type == NON_READ_WRITE_SYSPDIO))
1817 megasas_build_syspd_fusion(instance, scp, cmd, 0);
1818 else
1819 megasas_build_syspd_fusion(instance, scp, cmd, 1);
1820 break;
1821 default:
1822 break;
1823 }
9c915a8c 1824
1825 /*
1826 * Construct SGL
1827 */
1828
1829 sge_count =
1830 megasas_make_sgl_fusion(instance, scp,
1831 (struct MPI25_IEEE_SGE_CHAIN64 *)
1832 &io_request->SGL, cmd);
1833
1834 if (sge_count > instance->max_num_sge) {
1835 printk(KERN_ERR "megasas: Error. sge_count (0x%x) exceeds "
1836 "max (0x%x) allowed\n", sge_count,
1837 instance->max_num_sge);
1838 return 1;
1839 }
1840
1841 io_request->RaidContext.numSGE = sge_count;
1842
94cd65dd 1843 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING);
9c915a8c 1844
1845 if (scp->sc_data_direction == PCI_DMA_TODEVICE)
94cd65dd 1846 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE);
9c915a8c 1847 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE)
94cd65dd 1848 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ);
9c915a8c 1849
1850 io_request->SGLOffset0 =
1851 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4;
1852
94cd65dd 1853 io_request->SenseBufferLowAddress = cpu_to_le32(cmd->sense_phys_addr);
9c915a8c 1854 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE;
1855
1856 cmd->scmd = scp;
1857 scp->SCp.ptr = (char *)cmd;
1858
1859 return 0;
1860}
1861
1862union MEGASAS_REQUEST_DESCRIPTOR_UNION *
1863megasas_get_request_descriptor(struct megasas_instance *instance, u16 index)
1864{
1865 u8 *p;
1866 struct fusion_context *fusion;
1867
1868 if (index >= instance->max_fw_cmds) {
1869 printk(KERN_ERR "megasas: Invalid SMID (0x%x)request for "
229fe47c 1870 "descriptor for scsi%d\n", index,
1871 instance->host->host_no);
9c915a8c 1872 return NULL;
1873 }
1874 fusion = instance->ctrl_context;
1875 p = fusion->req_frames_desc
1876 +sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) *index;
1877
1878 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p;
1879}
1880
1881/**
1882 * megasas_build_and_issue_cmd_fusion -Main routine for building and
1883 * issuing non IOCTL cmd
1884 * @instance: Adapter soft state
1885 * @scmd: pointer to scsi cmd from OS
1886 */
1887static u32
1888megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance,
1889 struct scsi_cmnd *scmd)
1890{
1891 struct megasas_cmd_fusion *cmd;
1892 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
1893 u32 index;
1894 struct fusion_context *fusion;
1895
1896 fusion = instance->ctrl_context;
1897
4026e9aa 1898 cmd = megasas_get_cmd_fusion(instance, scmd->request->tag);
9c915a8c 1899
1900 index = cmd->index;
1901
1902 req_desc = megasas_get_request_descriptor(instance, index-1);
1903 if (!req_desc)
1904 return 1;
1905
1906 req_desc->Words = 0;
1907 cmd->request_desc = req_desc;
9c915a8c 1908
1909 if (megasas_build_io_fusion(instance, scmd, cmd)) {
1910 megasas_return_cmd_fusion(instance, cmd);
1911 printk(KERN_ERR "megasas: Error building command.\n");
1912 cmd->request_desc = NULL;
1913 return 1;
1914 }
1915
1916 req_desc = cmd->request_desc;
94cd65dd 1917 req_desc->SCSIIO.SMID = cpu_to_le16(index);
9c915a8c 1918
1919 if (cmd->io_request->ChainOffset != 0 &&
1920 cmd->io_request->ChainOffset != 0xF)
1921 printk(KERN_ERR "megasas: The chain offset value is not "
1922 "correct : %x\n", cmd->io_request->ChainOffset);
1923
1924 /*
1925 * Issue the command to the FW
1926 */
1927 atomic_inc(&instance->fw_outstanding);
1928
1929 instance->instancet->fire_cmd(instance,
1930 req_desc->u.low, req_desc->u.high,
1931 instance->reg_set);
1932
1933 return 0;
1934}
1935
1936/**
1937 * complete_cmd_fusion - Completes command
1938 * @instance: Adapter soft state
1939 * Completes all commands that is in reply descriptor queue
1940 */
1941int
c8e858fe 1942complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex)
9c915a8c 1943{
1944 union MPI2_REPLY_DESCRIPTORS_UNION *desc;
1945 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc;
1946 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req;
1947 struct fusion_context *fusion;
1948 struct megasas_cmd *cmd_mfi;
1949 struct megasas_cmd_fusion *cmd_fusion;
1950 u16 smid, num_completed;
d2552ebe 1951 u8 reply_descript_type;
9c915a8c 1952 u32 status, extStatus, device_id;
1953 union desc_value d_val;
1954 struct LD_LOAD_BALANCE_INFO *lbinfo;
db4fc864 1955 int threshold_reply_count = 0;
4026e9aa 1956 struct scsi_cmnd *scmd_local = NULL;
9c915a8c 1957
1958 fusion = instance->ctrl_context;
1959
1960 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR)
1961 return IRQ_HANDLED;
1962
1963 desc = fusion->reply_frames_desc;
c8e858fe 1964 desc += ((MSIxIndex * fusion->reply_alloc_sz)/
1965 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION)) +
1966 fusion->last_reply_idx[MSIxIndex];
9c915a8c 1967
1968 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
1969
1970 d_val.word = desc->Words;
1971
1972 reply_descript_type = reply_desc->ReplyFlags &
1973 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
1974
1975 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
1976 return IRQ_NONE;
1977
9c915a8c 1978 num_completed = 0;
1979
1980 while ((d_val.u.low != UINT_MAX) && (d_val.u.high != UINT_MAX)) {
94cd65dd 1981 smid = le16_to_cpu(reply_desc->SMID);
9c915a8c 1982
1983 cmd_fusion = fusion->cmd_list[smid - 1];
1984
1985 scsi_io_req =
1986 (struct MPI2_RAID_SCSI_IO_REQUEST *)
1987 cmd_fusion->io_request;
1988
1989 if (cmd_fusion->scmd)
1990 cmd_fusion->scmd->SCp.ptr = NULL;
1991
4026e9aa 1992 scmd_local = cmd_fusion->scmd;
9c915a8c 1993 status = scsi_io_req->RaidContext.status;
1994 extStatus = scsi_io_req->RaidContext.exStatus;
1995
1996 switch (scsi_io_req->Function) {
1997 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/
1998 /* Update load balancing info */
4026e9aa 1999 device_id = MEGASAS_DEV_INDEX(scmd_local);
9c915a8c 2000 lbinfo = &fusion->load_balance_info[device_id];
2001 if (cmd_fusion->scmd->SCp.Status &
2002 MEGASAS_LOAD_BALANCE_FLAG) {
d2552ebe 2003 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]);
9c915a8c 2004 cmd_fusion->scmd->SCp.Status &=
2005 ~MEGASAS_LOAD_BALANCE_FLAG;
2006 }
2007 if (reply_descript_type ==
2008 MPI2_RPY_DESCRIPT_FLAGS_SCSI_IO_SUCCESS) {
2009 if (megasas_dbg_lvl == 5)
2010 printk(KERN_ERR "\nmegasas: FAST Path "
2011 "IO Success\n");
2012 }
2013 /* Fall thru and complete IO */
2014 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */
2015 /* Map the FW Cmd Status */
2016 map_cmd_status(cmd_fusion, status, extStatus);
9c915a8c 2017 scsi_io_req->RaidContext.status = 0;
2018 scsi_io_req->RaidContext.exStatus = 0;
2019 megasas_return_cmd_fusion(instance, cmd_fusion);
4026e9aa
SS
2020 scsi_dma_unmap(scmd_local);
2021 scmd_local->scsi_done(scmd_local);
9c915a8c 2022 atomic_dec(&instance->fw_outstanding);
2023
2024 break;
2025 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */
2026 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
90dc9d98 2027
4026e9aa
SS
2028 /* Poll mode. Dummy free.
2029 * In case of Interrupt mode, caller has reverse check.
2030 */
2031 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) {
2032 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE;
2033 megasas_return_cmd(instance, cmd_mfi);
2034 } else
2035 megasas_complete_cmd(instance, cmd_mfi, DID_OK);
9c915a8c 2036 break;
2037 }
2038
c8e858fe 2039 fusion->last_reply_idx[MSIxIndex]++;
2040 if (fusion->last_reply_idx[MSIxIndex] >=
2041 fusion->reply_q_depth)
2042 fusion->last_reply_idx[MSIxIndex] = 0;
9c915a8c 2043
2044 desc->Words = ULLONG_MAX;
2045 num_completed++;
db4fc864 2046 threshold_reply_count++;
9c915a8c 2047
2048 /* Get the next reply descriptor */
c8e858fe 2049 if (!fusion->last_reply_idx[MSIxIndex])
2050 desc = fusion->reply_frames_desc +
2051 ((MSIxIndex * fusion->reply_alloc_sz)/
2052 sizeof(union MPI2_REPLY_DESCRIPTORS_UNION));
9c915a8c 2053 else
2054 desc++;
2055
2056 reply_desc =
2057 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc;
2058
2059 d_val.word = desc->Words;
2060
2061 reply_descript_type = reply_desc->ReplyFlags &
2062 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK;
2063
2064 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED)
2065 break;
db4fc864
SS
2066 /*
2067 * Write to reply post host index register after completing threshold
2068 * number of reply counts and still there are more replies in reply queue
2069 * pending to be completed
2070 */
2071 if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) {
2072 if ((instance->pdev->device ==
2073 PCI_DEVICE_ID_LSI_INVADER) ||
2074 (instance->pdev->device ==
2075 PCI_DEVICE_ID_LSI_FURY))
2076 writel(((MSIxIndex & 0x7) << 24) |
2077 fusion->last_reply_idx[MSIxIndex],
2078 instance->reply_post_host_index_addr[MSIxIndex/8]);
2079 else
2080 writel((MSIxIndex << 24) |
2081 fusion->last_reply_idx[MSIxIndex],
2082 instance->reply_post_host_index_addr[0]);
2083 threshold_reply_count = 0;
2084 }
9c915a8c 2085 }
2086
2087 if (!num_completed)
2088 return IRQ_NONE;
2089
2090 wmb();
d46a3ad6
SS
2091 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2092 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY))
2093 writel(((MSIxIndex & 0x7) << 24) |
2094 fusion->last_reply_idx[MSIxIndex],
2095 instance->reply_post_host_index_addr[MSIxIndex/8]);
2096 else
2097 writel((MSIxIndex << 24) |
2098 fusion->last_reply_idx[MSIxIndex],
2099 instance->reply_post_host_index_addr[0]);
53ef2bbd 2100 megasas_check_and_restore_queue_depth(instance);
9c915a8c 2101 return IRQ_HANDLED;
2102}
2103
2104/**
2105 * megasas_complete_cmd_dpc_fusion - Completes command
2106 * @instance: Adapter soft state
2107 *
2108 * Tasklet to complete cmds
2109 */
2110void
2111megasas_complete_cmd_dpc_fusion(unsigned long instance_addr)
2112{
2113 struct megasas_instance *instance =
2114 (struct megasas_instance *)instance_addr;
2115 unsigned long flags;
c8e858fe 2116 u32 count, MSIxIndex;
2117
2118 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
9c915a8c 2119
2120 /* If we have already declared adapter dead, donot complete cmds */
2121 spin_lock_irqsave(&instance->hba_lock, flags);
2122 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2123 spin_unlock_irqrestore(&instance->hba_lock, flags);
2124 return;
2125 }
2126 spin_unlock_irqrestore(&instance->hba_lock, flags);
2127
c8e858fe 2128 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++)
2129 complete_cmd_fusion(instance, MSIxIndex);
9c915a8c 2130}
2131
2132/**
2133 * megasas_isr_fusion - isr entry point
2134 */
2135irqreturn_t megasas_isr_fusion(int irq, void *devp)
2136{
c8e858fe 2137 struct megasas_irq_context *irq_context = devp;
2138 struct megasas_instance *instance = irq_context->instance;
fc62b3fc 2139 u32 mfiStatus, fw_state, dma_state;
9c915a8c 2140
d46a3ad6
SS
2141 if (instance->mask_interrupts)
2142 return IRQ_NONE;
2143
c8e858fe 2144 if (!instance->msix_vectors) {
9c915a8c 2145 mfiStatus = instance->instancet->clear_intr(instance->reg_set);
2146 if (!mfiStatus)
2147 return IRQ_NONE;
2148 }
2149
2150 /* If we are resetting, bail */
6497b247 2151 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) {
2152 instance->instancet->clear_intr(instance->reg_set);
9c915a8c 2153 return IRQ_HANDLED;
6497b247 2154 }
9c915a8c 2155
c8e858fe 2156 if (!complete_cmd_fusion(instance, irq_context->MSIxIndex)) {
6497b247 2157 instance->instancet->clear_intr(instance->reg_set);
9c915a8c 2158 /* If we didn't complete any commands, check for FW fault */
2159 fw_state = instance->instancet->read_fw_status_reg(
2160 instance->reg_set) & MFI_STATE_MASK;
fc62b3fc
SS
2161 dma_state = instance->instancet->read_fw_status_reg
2162 (instance->reg_set) & MFI_STATE_DMADONE;
2163 if (instance->crash_dump_drv_support &&
2164 instance->crash_dump_app_support) {
2165 /* Start collecting crash, if DMA bit is done */
2166 if ((fw_state == MFI_STATE_FAULT) && dma_state)
2167 schedule_work(&instance->crash_init);
2168 else if (fw_state == MFI_STATE_FAULT)
2169 schedule_work(&instance->work_init);
2170 } else if (fw_state == MFI_STATE_FAULT) {
229fe47c 2171 printk(KERN_WARNING "megaraid_sas: Iop2SysDoorbellInt"
2172 "for scsi%d\n", instance->host->host_no);
9c915a8c 2173 schedule_work(&instance->work_init);
229fe47c 2174 }
9c915a8c 2175 }
2176
2177 return IRQ_HANDLED;
2178}
2179
2180/**
2181 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru
2182 * @instance: Adapter soft state
2183 * mfi_cmd: megasas_cmd pointer
2184 *
2185 */
2186u8
2187build_mpt_mfi_pass_thru(struct megasas_instance *instance,
2188 struct megasas_cmd *mfi_cmd)
2189{
2190 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain;
2191 struct MPI2_RAID_SCSI_IO_REQUEST *io_req;
2192 struct megasas_cmd_fusion *cmd;
2193 struct fusion_context *fusion;
2194 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr;
2195
4026e9aa
SS
2196 fusion = instance->ctrl_context;
2197
2198 cmd = megasas_get_cmd_fusion(instance,
2199 instance->max_scsi_cmds + mfi_cmd->index);
9c915a8c 2200
2201 /* Save the smid. To be used for returning the cmd */
2202 mfi_cmd->context.smid = cmd->index;
90dc9d98 2203
9c915a8c 2204 /*
2205 * For cmds where the flag is set, store the flag and check
2206 * on completion. For cmds with this flag, don't call
2207 * megasas_complete_cmd
2208 */
2209
6e755ddc 2210 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE))
4026e9aa 2211 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE;
9c915a8c 2212
9c915a8c 2213 io_req = cmd->io_request;
36807e67 2214
21d3c710
SS
2215 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_INVADER) ||
2216 (instance->pdev->device == PCI_DEVICE_ID_LSI_FURY)) {
36807e67 2217 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end =
2218 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL;
2219 sgl_ptr_end += fusion->max_sge_in_main_msg - 1;
2220 sgl_ptr_end->Flags = 0;
2221 }
2222
9c915a8c 2223 mpi25_ieee_chain =
2224 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain;
2225
2226 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST;
2227 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST,
2228 SGL) / 4;
2229 io_req->ChainOffset = fusion->chain_offset_mfi_pthru;
2230
94cd65dd 2231 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr);
9c915a8c 2232
2233 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT |
2234 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR;
2235
94cd65dd 2236 mpi25_ieee_chain->Length = cpu_to_le32(MEGASAS_MAX_SZ_CHAIN_FRAME);
9c915a8c 2237
2238 return 0;
2239}
2240
2241/**
2242 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd
2243 * @instance: Adapter soft state
2244 * @cmd: mfi cmd to build
2245 *
2246 */
2247union MEGASAS_REQUEST_DESCRIPTOR_UNION *
2248build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd)
2249{
2250 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2251 u16 index;
2252
2253 if (build_mpt_mfi_pass_thru(instance, cmd)) {
2254 printk(KERN_ERR "Couldn't build MFI pass thru cmd\n");
2255 return NULL;
2256 }
2257
2258 index = cmd->context.smid;
2259
2260 req_desc = megasas_get_request_descriptor(instance, index - 1);
2261
2262 if (!req_desc)
2263 return NULL;
2264
2265 req_desc->Words = 0;
2266 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
2267 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
2268
94cd65dd 2269 req_desc->SCSIIO.SMID = cpu_to_le16(index);
9c915a8c 2270
2271 return req_desc;
2272}
2273
2274/**
2275 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd
2276 * @instance: Adapter soft state
2277 * @cmd: mfi cmd pointer
2278 *
2279 */
2280void
2281megasas_issue_dcmd_fusion(struct megasas_instance *instance,
2282 struct megasas_cmd *cmd)
2283{
2284 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
9c915a8c 2285
2286 req_desc = build_mpt_cmd(instance, cmd);
2287 if (!req_desc) {
2288 printk(KERN_ERR "Couldn't issue MFI pass thru cmd\n");
2289 return;
2290 }
9c915a8c 2291 instance->instancet->fire_cmd(instance, req_desc->u.low,
2292 req_desc->u.high, instance->reg_set);
2293}
2294
2295/**
2296 * megasas_release_fusion - Reverses the FW initialization
2297 * @intance: Adapter soft state
2298 */
2299void
2300megasas_release_fusion(struct megasas_instance *instance)
2301{
2302 megasas_free_cmds(instance);
2303 megasas_free_cmds_fusion(instance);
2304
2305 iounmap(instance->reg_set);
2306
2307 pci_release_selected_regions(instance->pdev, instance->bar);
2308}
2309
2310/**
2311 * megasas_read_fw_status_reg_fusion - returns the current FW status value
2312 * @regs: MFI register set
2313 */
2314static u32
2315megasas_read_fw_status_reg_fusion(struct megasas_register_set __iomem *regs)
2316{
2317 return readl(&(regs)->outbound_scratch_pad);
2318}
2319
fc62b3fc
SS
2320/**
2321 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware
2322 * @instance: Controller's soft instance
2323 * return: Number of allocated host crash buffers
2324 */
2325static void
2326megasas_alloc_host_crash_buffer(struct megasas_instance *instance)
2327{
2328 unsigned int i;
2329
2330 instance->crash_buf_pages = get_order(CRASH_DMA_BUF_SIZE);
2331 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) {
2332 instance->crash_buf[i] = (void *)__get_free_pages(GFP_KERNEL,
2333 instance->crash_buf_pages);
2334 if (!instance->crash_buf[i]) {
2335 dev_info(&instance->pdev->dev, "Firmware crash dump "
2336 "memory allocation failed at index %d\n", i);
2337 break;
2338 }
d009b576
SS
2339 memset(instance->crash_buf[i], 0,
2340 ((1 << PAGE_SHIFT) << instance->crash_buf_pages));
fc62b3fc
SS
2341 }
2342 instance->drv_buf_alloc = i;
2343}
2344
2345/**
2346 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware
2347 * @instance: Controller's soft instance
2348 */
2349void
2350megasas_free_host_crash_buffer(struct megasas_instance *instance)
2351{
2352 unsigned int i
2353;
2354 for (i = 0; i < instance->drv_buf_alloc; i++) {
2355 if (instance->crash_buf[i])
2356 free_pages((ulong)instance->crash_buf[i],
2357 instance->crash_buf_pages);
2358 }
2359 instance->drv_buf_index = 0;
2360 instance->drv_buf_alloc = 0;
2361 instance->fw_crash_state = UNAVAILABLE;
2362 instance->fw_crash_buffer_size = 0;
2363}
2364
9c915a8c 2365/**
2366 * megasas_adp_reset_fusion - For controller reset
2367 * @regs: MFI register set
2368 */
2369static int
2370megasas_adp_reset_fusion(struct megasas_instance *instance,
2371 struct megasas_register_set __iomem *regs)
2372{
2373 return 0;
2374}
2375
2376/**
2377 * megasas_check_reset_fusion - For controller reset check
2378 * @regs: MFI register set
2379 */
2380static int
2381megasas_check_reset_fusion(struct megasas_instance *instance,
2382 struct megasas_register_set __iomem *regs)
2383{
2384 return 0;
2385}
2386
2387/* This function waits for outstanding commands on fusion to complete */
229fe47c 2388int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance,
2389 int iotimeout, int *convert)
9c915a8c 2390{
229fe47c 2391 int i, outstanding, retval = 0, hb_seconds_missed = 0;
c007b8b2 2392 u32 fw_state;
9c915a8c 2393
c007b8b2 2394 for (i = 0; i < resetwaittime; i++) {
9c915a8c 2395 /* Check if firmware is in fault state */
2396 fw_state = instance->instancet->read_fw_status_reg(
2397 instance->reg_set) & MFI_STATE_MASK;
2398 if (fw_state == MFI_STATE_FAULT) {
2399 printk(KERN_WARNING "megasas: Found FW in FAULT state,"
229fe47c 2400 " will reset adapter scsi%d.\n",
2401 instance->host->host_no);
2402 retval = 1;
2403 goto out;
2404 }
2405 /* If SR-IOV VF mode & heartbeat timeout, don't wait */
2406 if (instance->requestorId && !iotimeout) {
9c915a8c 2407 retval = 1;
2408 goto out;
2409 }
2410
229fe47c 2411 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */
2412 if (instance->requestorId && iotimeout) {
2413 if (instance->hb_host_mem->HB.fwCounter !=
2414 instance->hb_host_mem->HB.driverCounter) {
2415 instance->hb_host_mem->HB.driverCounter =
2416 instance->hb_host_mem->HB.fwCounter;
2417 hb_seconds_missed = 0;
2418 } else {
2419 hb_seconds_missed++;
2420 if (hb_seconds_missed ==
2421 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) {
2422 printk(KERN_WARNING "megasas: SR-IOV:"
2423 " Heartbeat never completed "
2424 " while polling during I/O "
2425 " timeout handling for "
2426 "scsi%d.\n",
2427 instance->host->host_no);
2428 *convert = 1;
2429 retval = 1;
2430 goto out;
2431 }
2432 }
2433 }
2434
9c915a8c 2435 outstanding = atomic_read(&instance->fw_outstanding);
2436 if (!outstanding)
2437 goto out;
2438
2439 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) {
2440 printk(KERN_NOTICE "megasas: [%2d]waiting for %d "
229fe47c 2441 "commands to complete for scsi%d\n", i,
2442 outstanding, instance->host->host_no);
9c915a8c 2443 megasas_complete_cmd_dpc_fusion(
2444 (unsigned long)instance);
2445 }
2446 msleep(1000);
2447 }
2448
2449 if (atomic_read(&instance->fw_outstanding)) {
2450 printk("megaraid_sas: pending commands remain after waiting, "
229fe47c 2451 "will reset adapter scsi%d.\n",
2452 instance->host->host_no);
9c915a8c 2453 retval = 1;
2454 }
2455out:
2456 return retval;
2457}
2458
2459void megasas_reset_reply_desc(struct megasas_instance *instance)
2460{
c8e858fe 2461 int i, count;
9c915a8c 2462 struct fusion_context *fusion;
2463 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc;
2464
2465 fusion = instance->ctrl_context;
c8e858fe 2466 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1;
2467 for (i = 0 ; i < count ; i++)
2468 fusion->last_reply_idx[i] = 0;
9c915a8c 2469 reply_desc = fusion->reply_frames_desc;
c8e858fe 2470 for (i = 0 ; i < fusion->reply_q_depth * count; i++, reply_desc++)
9c915a8c 2471 reply_desc->Words = ULLONG_MAX;
2472}
2473
9b828182
SS
2474/*
2475 * megasas_refire_mgmt_cmd : Re-fire management commands
2476 * @instance: Controller's soft instance
2477*/
2478void megasas_refire_mgmt_cmd(struct megasas_instance *instance)
2479{
2480 int j;
2481 struct megasas_cmd_fusion *cmd_fusion;
2482 struct fusion_context *fusion;
2483 struct megasas_cmd *cmd_mfi;
2484 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc;
2485 u16 smid;
2486
2487 fusion = instance->ctrl_context;
2488
2489 /* Re-fire management commands.
2490 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds.
2491 */
2492 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) {
2493 cmd_fusion = fusion->cmd_list[j];
2494 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx];
2495 smid = le16_to_cpu(cmd_mfi->context.smid);
2496
2497 if (!smid)
2498 continue;
2499 req_desc = megasas_get_request_descriptor
2500 (instance, smid - 1);
2501 if (req_desc && (cmd_mfi->frame->dcmd.opcode !=
2502 cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO))) {
2503 instance->instancet->fire_cmd(instance,
2504 req_desc->u.low, req_desc->u.high,
2505 instance->reg_set);
2506 } else
2507 megasas_return_cmd(instance, cmd_mfi);
2508 }
2509}
2510
229fe47c 2511/* Check for a second path that is currently UP */
2512int megasas_check_mpio_paths(struct megasas_instance *instance,
2513 struct scsi_cmnd *scmd)
2514{
2515 int i, j, retval = (DID_RESET << 16);
2516
2517 if (instance->mpio && instance->requestorId) {
2518 for (i = 0 ; i < MAX_MGMT_ADAPTERS ; i++)
2519 for (j = 0 ; j < MAX_LOGICAL_DRIVES; j++)
2520 if (megasas_mgmt_info.instance[i] &&
2521 (megasas_mgmt_info.instance[i] != instance) &&
2522 megasas_mgmt_info.instance[i]->mpio &&
2523 megasas_mgmt_info.instance[i]->requestorId
2524 &&
2525 (megasas_mgmt_info.instance[i]->ld_ids[j]
2526 == scmd->device->id)) {
2527 retval = (DID_NO_CONNECT << 16);
2528 goto out;
2529 }
2530 }
2531out:
2532 return retval;
2533}
2534
9c915a8c 2535/* Core fusion reset function */
229fe47c 2536int megasas_reset_fusion(struct Scsi_Host *shost, int iotimeout)
9c915a8c 2537{
9b828182 2538 int retval = SUCCESS, i, retry = 0, convert = 0;
9c915a8c 2539 struct megasas_instance *instance;
2540 struct megasas_cmd_fusion *cmd_fusion;
2541 struct fusion_context *fusion;
7e70e733 2542 u32 host_diag, abs_state, status_reg, reset_adapter;
fc62b3fc 2543 u32 io_timeout_in_crash_mode = 0;
4026e9aa 2544 struct scsi_cmnd *scmd_local = NULL;
9c915a8c 2545
2546 instance = (struct megasas_instance *)shost->hostdata;
2547 fusion = instance->ctrl_context;
2548
229fe47c 2549 mutex_lock(&instance->reset_mutex);
2550
9c915a8c 2551 if (instance->adprecovery == MEGASAS_HW_CRITICAL_ERROR) {
2552 printk(KERN_WARNING "megaraid_sas: Hardware critical error, "
229fe47c 2553 "returning FAILED for scsi%d.\n",
2554 instance->host->host_no);
a2fbcbc3 2555 mutex_unlock(&instance->reset_mutex);
d4a759a9 2556 return FAILED;
9c915a8c 2557 }
fc62b3fc
SS
2558 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
2559 abs_state = status_reg & MFI_STATE_MASK;
2560
2561 /* IO timeout detected, forcibly put FW in FAULT state */
2562 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf &&
2563 instance->crash_dump_app_support && iotimeout) {
2564 dev_info(&instance->pdev->dev, "IO timeout is detected, "
2565 "forcibly FAULT Firmware\n");
2566 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
2567 status_reg = readl(&instance->reg_set->doorbell);
2568 writel(status_reg | MFI_STATE_FORCE_OCR,
2569 &instance->reg_set->doorbell);
2570 readl(&instance->reg_set->doorbell);
2571 mutex_unlock(&instance->reset_mutex);
2572 do {
2573 ssleep(3);
2574 io_timeout_in_crash_mode++;
2575 dev_dbg(&instance->pdev->dev, "waiting for [%d] "
2576 "seconds for crash dump collection and OCR "
2577 "to be done\n", (io_timeout_in_crash_mode * 3));
2578 } while ((instance->adprecovery != MEGASAS_HBA_OPERATIONAL) &&
2579 (io_timeout_in_crash_mode < 80));
2580
2581 if (instance->adprecovery == MEGASAS_HBA_OPERATIONAL) {
2582 dev_info(&instance->pdev->dev, "OCR done for IO "
2583 "timeout case\n");
2584 retval = SUCCESS;
2585 } else {
2586 dev_info(&instance->pdev->dev, "Controller is not "
2587 "operational after 240 seconds wait for IO "
2588 "timeout case in FW crash dump mode\n do "
2589 "OCR/kill adapter\n");
2590 retval = megasas_reset_fusion(shost, 0);
2591 }
2592 return retval;
2593 }
9c915a8c 2594
229fe47c 2595 if (instance->requestorId && !instance->skip_heartbeat_timer_del)
2596 del_timer_sync(&instance->sriov_heartbeat_timer);
7e70e733 2597 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
229fe47c 2598 instance->adprecovery = MEGASAS_ADPRESET_SM_POLLING;
d46a3ad6 2599 instance->instancet->disable_intr(instance);
7e70e733 2600 msleep(1000);
2601
9c915a8c 2602 /* First try waiting for commands to complete */
229fe47c 2603 if (megasas_wait_for_outstanding_fusion(instance, iotimeout,
2604 &convert)) {
2605 instance->adprecovery = MEGASAS_ADPRESET_SM_INFAULT;
9c915a8c 2606 printk(KERN_WARNING "megaraid_sas: resetting fusion "
229fe47c 2607 "adapter scsi%d.\n", instance->host->host_no);
2608 if (convert)
2609 iotimeout = 0;
2610
9c915a8c 2611 /* Now return commands back to the OS */
4026e9aa 2612 for (i = 0 ; i < instance->max_scsi_cmds; i++) {
9c915a8c 2613 cmd_fusion = fusion->cmd_list[i];
4026e9aa 2614 scmd_local = cmd_fusion->scmd;
9c915a8c 2615 if (cmd_fusion->scmd) {
4026e9aa 2616 scmd_local->result =
229fe47c 2617 megasas_check_mpio_paths(instance,
4026e9aa 2618 scmd_local);
9c915a8c 2619 megasas_return_cmd_fusion(instance, cmd_fusion);
4026e9aa
SS
2620 scsi_dma_unmap(scmd_local);
2621 scmd_local->scsi_done(scmd_local);
9c915a8c 2622 atomic_dec(&instance->fw_outstanding);
2623 }
2624 }
2625
7e70e733 2626 status_reg = instance->instancet->read_fw_status_reg(
2627 instance->reg_set);
2628 abs_state = status_reg & MFI_STATE_MASK;
2629 reset_adapter = status_reg & MFI_RESET_ADAPTER;
2630 if (instance->disableOnlineCtrlReset ||
2631 (abs_state == MFI_STATE_FAULT && !reset_adapter)) {
9c915a8c 2632 /* Reset not supported, kill adapter */
2633 printk(KERN_WARNING "megaraid_sas: Reset not supported"
229fe47c 2634 ", killing adapter scsi%d.\n",
2635 instance->host->host_no);
9c915a8c 2636 megaraid_sas_kill_hba(instance);
229fe47c 2637 instance->skip_heartbeat_timer_del = 1;
9c915a8c 2638 retval = FAILED;
2639 goto out;
2640 }
2641
229fe47c 2642 /* Let SR-IOV VF & PF sync up if there was a HB failure */
2643 if (instance->requestorId && !iotimeout) {
2644 msleep(MEGASAS_OCR_SETTLE_TIME_VF);
2645 /* Look for a late HB update after VF settle time */
2646 if (abs_state == MFI_STATE_OPERATIONAL &&
2647 (instance->hb_host_mem->HB.fwCounter !=
2648 instance->hb_host_mem->HB.driverCounter)) {
2649 instance->hb_host_mem->HB.driverCounter =
2650 instance->hb_host_mem->HB.fwCounter;
2651 printk(KERN_WARNING "megasas: SR-IOV:"
2652 "Late FW heartbeat update for "
2653 "scsi%d.\n",
2654 instance->host->host_no);
2655 } else {
2656 /* In VF mode, first poll for FW ready */
2657 for (i = 0;
2658 i < (MEGASAS_RESET_WAIT_TIME * 1000);
2659 i += 20) {
2660 status_reg =
2661 instance->instancet->
2662 read_fw_status_reg(
2663 instance->reg_set);
2664 abs_state = status_reg &
2665 MFI_STATE_MASK;
2666 if (abs_state == MFI_STATE_READY) {
2667 printk(KERN_WARNING "megasas"
2668 ": SR-IOV: FW was found"
2669 "to be in ready state "
2670 "for scsi%d.\n",
2671 instance->host->host_no);
2672 break;
2673 }
2674 msleep(20);
2675 }
2676 if (abs_state != MFI_STATE_READY) {
2677 printk(KERN_WARNING "megasas: SR-IOV: "
2678 "FW not in ready state after %d"
2679 " seconds for scsi%d, status_reg = "
2680 "0x%x.\n",
2681 MEGASAS_RESET_WAIT_TIME,
2682 instance->host->host_no,
2683 status_reg);
2684 megaraid_sas_kill_hba(instance);
2685 instance->skip_heartbeat_timer_del = 1;
2686 instance->adprecovery =
2687 MEGASAS_HW_CRITICAL_ERROR;
2688 retval = FAILED;
2689 goto out;
2690 }
2691 }
2692 }
2693
9c915a8c 2694 /* Now try to reset the chip */
2695 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) {
2696 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE,
2697 &instance->reg_set->fusion_seq_offset);
2698 writel(MPI2_WRSEQ_1ST_KEY_VALUE,
2699 &instance->reg_set->fusion_seq_offset);
2700 writel(MPI2_WRSEQ_2ND_KEY_VALUE,
2701 &instance->reg_set->fusion_seq_offset);
2702 writel(MPI2_WRSEQ_3RD_KEY_VALUE,
2703 &instance->reg_set->fusion_seq_offset);
2704 writel(MPI2_WRSEQ_4TH_KEY_VALUE,
2705 &instance->reg_set->fusion_seq_offset);
2706 writel(MPI2_WRSEQ_5TH_KEY_VALUE,
2707 &instance->reg_set->fusion_seq_offset);
2708 writel(MPI2_WRSEQ_6TH_KEY_VALUE,
2709 &instance->reg_set->fusion_seq_offset);
2710
2711 /* Check that the diag write enable (DRWE) bit is on */
2712 host_diag = readl(&instance->reg_set->fusion_host_diag);
7e70e733 2713 retry = 0;
9c915a8c 2714 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) {
2715 msleep(100);
2716 host_diag =
2717 readl(&instance->reg_set->fusion_host_diag);
2718 if (retry++ == 100) {
2719 printk(KERN_WARNING "megaraid_sas: "
229fe47c 2720 "Host diag unlock failed! "
2721 "for scsi%d\n",
2722 instance->host->host_no);
9c915a8c 2723 break;
2724 }
2725 }
2726 if (!(host_diag & HOST_DIAG_WRITE_ENABLE))
2727 continue;
2728
2729 /* Send chip reset command */
2730 writel(host_diag | HOST_DIAG_RESET_ADAPTER,
2731 &instance->reg_set->fusion_host_diag);
2732 msleep(3000);
2733
2734 /* Make sure reset adapter bit is cleared */
2735 host_diag = readl(&instance->reg_set->fusion_host_diag);
2736 retry = 0;
2737 while (host_diag & HOST_DIAG_RESET_ADAPTER) {
2738 msleep(100);
2739 host_diag =
2740 readl(&instance->reg_set->fusion_host_diag);
2741 if (retry++ == 1000) {
2742 printk(KERN_WARNING "megaraid_sas: "
2743 "Diag reset adapter never "
229fe47c 2744 "cleared for scsi%d!\n",
2745 instance->host->host_no);
9c915a8c 2746 break;
2747 }
2748 }
2749 if (host_diag & HOST_DIAG_RESET_ADAPTER)
2750 continue;
2751
2752 abs_state =
2753 instance->instancet->read_fw_status_reg(
7e70e733 2754 instance->reg_set) & MFI_STATE_MASK;
9c915a8c 2755 retry = 0;
2756
2757 while ((abs_state <= MFI_STATE_FW_INIT) &&
2758 (retry++ < 1000)) {
2759 msleep(100);
2760 abs_state =
2761 instance->instancet->read_fw_status_reg(
7e70e733 2762 instance->reg_set) & MFI_STATE_MASK;
9c915a8c 2763 }
2764 if (abs_state <= MFI_STATE_FW_INIT) {
2765 printk(KERN_WARNING "megaraid_sas: firmware "
2766 "state < MFI_STATE_FW_INIT, state = "
229fe47c 2767 "0x%x for scsi%d\n", abs_state,
2768 instance->host->host_no);
9c915a8c 2769 continue;
2770 }
2771
2772 /* Wait for FW to become ready */
058a8fac 2773 if (megasas_transition_to_ready(instance, 1)) {
9c915a8c 2774 printk(KERN_WARNING "megaraid_sas: Failed to "
229fe47c 2775 "transition controller to ready "
2776 "for scsi%d.\n",
2777 instance->host->host_no);
9c915a8c 2778 continue;
2779 }
2780
2781 megasas_reset_reply_desc(instance);
2782 if (megasas_ioc_init_fusion(instance)) {
2783 printk(KERN_WARNING "megaraid_sas: "
229fe47c 2784 "megasas_ioc_init_fusion() failed!"
2785 " for scsi%d\n",
2786 instance->host->host_no);
9c915a8c 2787 continue;
2788 }
2789
9b828182 2790 megasas_refire_mgmt_cmd(instance);
9c915a8c 2791
d009b576
SS
2792 if (megasas_get_ctrl_info(instance)) {
2793 dev_info(&instance->pdev->dev,
2794 "Failed from %s %d\n",
2795 __func__, __LINE__);
d009b576
SS
2796 megaraid_sas_kill_hba(instance);
2797 retval = FAILED;
2798 }
9c915a8c 2799 /* Reset load balance info */
2800 memset(fusion->load_balance_info, 0,
2801 sizeof(struct LD_LOAD_BALANCE_INFO)
51087a86 2802 *MAX_LOGICAL_DRIVES_EXT);
9c915a8c 2803
2804 if (!megasas_get_map_info(instance))
2805 megasas_sync_map_info(instance);
2806
ac340c5f
SS
2807 clear_bit(MEGASAS_FUSION_IN_RESET,
2808 &instance->reset_flags);
2809 instance->instancet->enable_intr(instance);
2810 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2811
229fe47c 2812 /* Restart SR-IOV heartbeat */
2813 if (instance->requestorId) {
2814 if (!megasas_sriov_start_heartbeat(instance, 0))
2815 megasas_start_timer(instance,
2816 &instance->sriov_heartbeat_timer,
2817 megasas_sriov_heartbeat_handler,
2818 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2819 else
2820 instance->skip_heartbeat_timer_del = 1;
2821 }
2822
9c915a8c 2823 /* Adapter reset completed successfully */
2824 printk(KERN_WARNING "megaraid_sas: Reset "
229fe47c 2825 "successful for scsi%d.\n",
2826 instance->host->host_no);
fc62b3fc 2827
ac340c5f
SS
2828 if (instance->crash_dump_drv_support &&
2829 instance->crash_dump_app_support)
2830 megasas_set_crash_dump_params(instance,
2831 MR_CRASH_BUF_TURN_ON);
2832 else
2833 megasas_set_crash_dump_params(instance,
2834 MR_CRASH_BUF_TURN_OFF);
2835
9c915a8c 2836 retval = SUCCESS;
2837 goto out;
2838 }
2839 /* Reset failed, kill the adapter */
2840 printk(KERN_WARNING "megaraid_sas: Reset failed, killing "
229fe47c 2841 "adapter scsi%d.\n", instance->host->host_no);
9c915a8c 2842 megaraid_sas_kill_hba(instance);
229fe47c 2843 instance->skip_heartbeat_timer_del = 1;
9c915a8c 2844 retval = FAILED;
2845 } else {
229fe47c 2846 /* For VF: Restart HB timer if we didn't OCR */
2847 if (instance->requestorId) {
2848 megasas_start_timer(instance,
2849 &instance->sriov_heartbeat_timer,
2850 megasas_sriov_heartbeat_handler,
2851 MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF);
2852 }
3f0e58bc 2853 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
d46a3ad6 2854 instance->instancet->enable_intr(instance);
9c915a8c 2855 instance->adprecovery = MEGASAS_HBA_OPERATIONAL;
2856 }
2857out:
2858 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags);
2859 mutex_unlock(&instance->reset_mutex);
2860 return retval;
2861}
2862
fc62b3fc
SS
2863/* Fusion Crash dump collection work queue */
2864void megasas_fusion_crash_dump_wq(struct work_struct *work)
2865{
2866 struct megasas_instance *instance =
2867 container_of(work, struct megasas_instance, crash_init);
2868 u32 status_reg;
2869 u8 partial_copy = 0;
2870
2871
2872 status_reg = instance->instancet->read_fw_status_reg(instance->reg_set);
2873
2874 /*
2875 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer
2876 * to host crash buffers
2877 */
2878 if (instance->drv_buf_index == 0) {
2879 /* Buffer is already allocated for old Crash dump.
2880 * Do OCR and do not wait for crash dump collection
2881 */
2882 if (instance->drv_buf_alloc) {
2883 dev_info(&instance->pdev->dev, "earlier crash dump is "
2884 "not yet copied by application, ignoring this "
2885 "crash dump and initiating OCR\n");
2886 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
2887 writel(status_reg,
2888 &instance->reg_set->outbound_scratch_pad);
2889 readl(&instance->reg_set->outbound_scratch_pad);
2890 return;
2891 }
2892 megasas_alloc_host_crash_buffer(instance);
2893 dev_info(&instance->pdev->dev, "Number of host crash buffers "
2894 "allocated: %d\n", instance->drv_buf_alloc);
2895 }
2896
2897 /*
2898 * Driver has allocated max buffers, which can be allocated
2899 * and FW has more crash dump data, then driver will
2900 * ignore the data.
2901 */
2902 if (instance->drv_buf_index >= (instance->drv_buf_alloc)) {
2903 dev_info(&instance->pdev->dev, "Driver is done copying "
2904 "the buffer: %d\n", instance->drv_buf_alloc);
2905 status_reg |= MFI_STATE_CRASH_DUMP_DONE;
2906 partial_copy = 1;
2907 } else {
2908 memcpy(instance->crash_buf[instance->drv_buf_index],
2909 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE);
2910 instance->drv_buf_index++;
2911 status_reg &= ~MFI_STATE_DMADONE;
2912 }
2913
2914 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) {
2915 dev_info(&instance->pdev->dev, "Crash Dump is available,number "
2916 "of copied buffers: %d\n", instance->drv_buf_index);
2917 instance->fw_crash_buffer_size = instance->drv_buf_index;
2918 instance->fw_crash_state = AVAILABLE;
2919 instance->drv_buf_index = 0;
2920 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
2921 readl(&instance->reg_set->outbound_scratch_pad);
2922 if (!partial_copy)
2923 megasas_reset_fusion(instance->host, 0);
2924 } else {
2925 writel(status_reg, &instance->reg_set->outbound_scratch_pad);
2926 readl(&instance->reg_set->outbound_scratch_pad);
2927 }
2928}
2929
2930
9c915a8c 2931/* Fusion OCR work queue */
2932void megasas_fusion_ocr_wq(struct work_struct *work)
2933{
2934 struct megasas_instance *instance =
2935 container_of(work, struct megasas_instance, work_init);
2936
229fe47c 2937 megasas_reset_fusion(instance->host, 0);
9c915a8c 2938}
2939
2940struct megasas_instance_template megasas_instance_template_fusion = {
2941 .fire_cmd = megasas_fire_cmd_fusion,
2942 .enable_intr = megasas_enable_intr_fusion,
2943 .disable_intr = megasas_disable_intr_fusion,
2944 .clear_intr = megasas_clear_intr_fusion,
2945 .read_fw_status_reg = megasas_read_fw_status_reg_fusion,
2946 .adp_reset = megasas_adp_reset_fusion,
2947 .check_reset = megasas_check_reset_fusion,
2948 .service_isr = megasas_isr_fusion,
2949 .tasklet = megasas_complete_cmd_dpc_fusion,
2950 .init_adapter = megasas_init_adapter_fusion,
2951 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion,
2952 .issue_dcmd = megasas_issue_dcmd_fusion,
2953};
This page took 0.544609 seconds and 5 git commands to generate.