1 /* 2 * Linux MegaRAID driver for SAS based RAID controllers 3 * 4 * Copyright (c) 2009-2013 LSI Corporation 5 * Copyright (c) 2013-2016 Avago Technologies 6 * Copyright (c) 2016-2018 Broadcom Inc. 7 * 8 * This program is free software; you can redistribute it and/or 9 * modify it under the terms of the GNU General Public License 10 * as published by the Free Software Foundation; either version 2 11 * of the License, or (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program. If not, see <http://www.gnu.org/licenses/>. 20 * 21 * FILE: megaraid_sas_fusion.c 22 * 23 * Authors: Broadcom Inc. 24 * Sumant Patro 25 * Adam Radford 26 * Kashyap Desai <kashyap.desai@broadcom.com> 27 * Sumit Saxena <sumit.saxena@broadcom.com> 28 * 29 * Send feedback to: megaraidlinux.pdl@broadcom.com 30 */ 31 32 #include <linux/kernel.h> 33 #include <linux/types.h> 34 #include <linux/pci.h> 35 #include <linux/list.h> 36 #include <linux/moduleparam.h> 37 #include <linux/module.h> 38 #include <linux/spinlock.h> 39 #include <linux/interrupt.h> 40 #include <linux/delay.h> 41 #include <linux/uio.h> 42 #include <linux/uaccess.h> 43 #include <linux/fs.h> 44 #include <linux/compat.h> 45 #include <linux/blkdev.h> 46 #include <linux/mutex.h> 47 #include <linux/poll.h> 48 #include <linux/vmalloc.h> 49 #include <linux/workqueue.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> 55 #include <scsi/scsi_dbg.h> 56 #include <linux/dmi.h> 57 58 #include "megaraid_sas_fusion.h" 59 #include "megaraid_sas.h" 60 61 62 extern void megasas_free_cmds(struct megasas_instance *instance); 63 extern struct megasas_cmd *megasas_get_cmd(struct megasas_instance 64 *instance); 65 extern void 66 megasas_complete_cmd(struct megasas_instance *instance, 67 struct megasas_cmd *cmd, u8 alt_status); 68 int 69 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd, 70 int seconds); 71 72 void 73 megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd); 74 int megasas_alloc_cmds(struct megasas_instance *instance); 75 int 76 megasas_clear_intr_fusion(struct megasas_instance *instance); 77 int 78 megasas_issue_polled(struct megasas_instance *instance, 79 struct megasas_cmd *cmd); 80 void 81 megasas_check_and_restore_queue_depth(struct megasas_instance *instance); 82 83 int megasas_transition_to_ready(struct megasas_instance *instance, int ocr); 84 void megaraid_sas_kill_hba(struct megasas_instance *instance); 85 86 extern u32 megasas_dbg_lvl; 87 int megasas_sriov_start_heartbeat(struct megasas_instance *instance, 88 int initial); 89 void megasas_start_timer(struct megasas_instance *instance); 90 extern struct megasas_mgmt_info megasas_mgmt_info; 91 extern unsigned int resetwaittime; 92 extern unsigned int dual_qdepth_disable; 93 static void megasas_free_rdpq_fusion(struct megasas_instance *instance); 94 static void megasas_free_reply_fusion(struct megasas_instance *instance); 95 static inline 96 void megasas_configure_queue_sizes(struct megasas_instance *instance); 97 static void megasas_fusion_crash_dump(struct megasas_instance *instance); 98 extern u32 megasas_readl(struct megasas_instance *instance, 99 const volatile void __iomem *addr); 100 101 /** 102 * megasas_check_same_4gb_region - check if allocation 103 * crosses same 4GB boundary or not 104 * @instance - adapter's soft instance 105 * start_addr - start address of DMA allocation 106 * size - size of allocation in bytes 107 * return - true : allocation does not cross same 108 * 4GB boundary 109 * false: allocation crosses same 110 * 4GB boundary 111 */ 112 static inline bool megasas_check_same_4gb_region 113 (struct megasas_instance *instance, dma_addr_t start_addr, size_t size) 114 { 115 dma_addr_t end_addr; 116 117 end_addr = start_addr + size; 118 119 if (upper_32_bits(start_addr) != upper_32_bits(end_addr)) { 120 dev_err(&instance->pdev->dev, 121 "Failed to get same 4GB boundary: start_addr: 0x%llx end_addr: 0x%llx\n", 122 (unsigned long long)start_addr, 123 (unsigned long long)end_addr); 124 return false; 125 } 126 127 return true; 128 } 129 130 /** 131 * megasas_enable_intr_fusion - Enables interrupts 132 * @regs: MFI register set 133 */ 134 void 135 megasas_enable_intr_fusion(struct megasas_instance *instance) 136 { 137 struct megasas_register_set __iomem *regs; 138 regs = instance->reg_set; 139 140 instance->mask_interrupts = 0; 141 /* For Thunderbolt/Invader also clear intr on enable */ 142 writel(~0, ®s->outbound_intr_status); 143 readl(®s->outbound_intr_status); 144 145 writel(~MFI_FUSION_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask); 146 147 /* Dummy readl to force pci flush */ 148 readl(®s->outbound_intr_mask); 149 } 150 151 /** 152 * megasas_disable_intr_fusion - Disables interrupt 153 * @regs: MFI register set 154 */ 155 void 156 megasas_disable_intr_fusion(struct megasas_instance *instance) 157 { 158 u32 mask = 0xFFFFFFFF; 159 u32 status; 160 struct megasas_register_set __iomem *regs; 161 regs = instance->reg_set; 162 instance->mask_interrupts = 1; 163 164 writel(mask, ®s->outbound_intr_mask); 165 /* Dummy readl to force pci flush */ 166 status = readl(®s->outbound_intr_mask); 167 } 168 169 int 170 megasas_clear_intr_fusion(struct megasas_instance *instance) 171 { 172 u32 status; 173 struct megasas_register_set __iomem *regs; 174 regs = instance->reg_set; 175 /* 176 * Check if it is our interrupt 177 */ 178 status = megasas_readl(instance, 179 ®s->outbound_intr_status); 180 181 if (status & 1) { 182 writel(status, ®s->outbound_intr_status); 183 readl(®s->outbound_intr_status); 184 return 1; 185 } 186 if (!(status & MFI_FUSION_ENABLE_INTERRUPT_MASK)) 187 return 0; 188 189 return 1; 190 } 191 192 /** 193 * megasas_get_cmd_fusion - Get a command from the free pool 194 * @instance: Adapter soft state 195 * 196 * Returns a blk_tag indexed mpt frame 197 */ 198 inline struct megasas_cmd_fusion *megasas_get_cmd_fusion(struct megasas_instance 199 *instance, u32 blk_tag) 200 { 201 struct fusion_context *fusion; 202 203 fusion = instance->ctrl_context; 204 return fusion->cmd_list[blk_tag]; 205 } 206 207 /** 208 * megasas_return_cmd_fusion - Return a cmd to free command pool 209 * @instance: Adapter soft state 210 * @cmd: Command packet to be returned to free command pool 211 */ 212 inline void megasas_return_cmd_fusion(struct megasas_instance *instance, 213 struct megasas_cmd_fusion *cmd) 214 { 215 cmd->scmd = NULL; 216 memset(cmd->io_request, 0, MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE); 217 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID; 218 cmd->cmd_completed = false; 219 } 220 221 /** 222 * megasas_fire_cmd_fusion - Sends command to the FW 223 * @instance: Adapter soft state 224 * @req_desc: 64bit Request descriptor 225 * 226 * Perform PCI Write. 227 */ 228 229 static void 230 megasas_fire_cmd_fusion(struct megasas_instance *instance, 231 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc) 232 { 233 #if defined(writeq) && defined(CONFIG_64BIT) 234 u64 req_data = (((u64)le32_to_cpu(req_desc->u.high) << 32) | 235 le32_to_cpu(req_desc->u.low)); 236 237 writeq(req_data, &instance->reg_set->inbound_low_queue_port); 238 #else 239 unsigned long flags; 240 spin_lock_irqsave(&instance->hba_lock, flags); 241 writel(le32_to_cpu(req_desc->u.low), 242 &instance->reg_set->inbound_low_queue_port); 243 writel(le32_to_cpu(req_desc->u.high), 244 &instance->reg_set->inbound_high_queue_port); 245 mmiowb(); 246 spin_unlock_irqrestore(&instance->hba_lock, flags); 247 #endif 248 } 249 250 /** 251 * megasas_fusion_update_can_queue - Do all Adapter Queue depth related calculations here 252 * @instance: Adapter soft state 253 * fw_boot_context: Whether this function called during probe or after OCR 254 * 255 * This function is only for fusion controllers. 256 * Update host can queue, if firmware downgrade max supported firmware commands. 257 * Firmware upgrade case will be skiped because underlying firmware has 258 * more resource than exposed to the OS. 259 * 260 */ 261 static void 262 megasas_fusion_update_can_queue(struct megasas_instance *instance, int fw_boot_context) 263 { 264 u16 cur_max_fw_cmds = 0; 265 u16 ldio_threshold = 0; 266 struct megasas_register_set __iomem *reg_set; 267 268 reg_set = instance->reg_set; 269 270 /* ventura FW does not fill outbound_scratch_pad_2 with queue depth */ 271 if (instance->adapter_type < VENTURA_SERIES) 272 cur_max_fw_cmds = 273 megasas_readl(instance, 274 &instance->reg_set->outbound_scratch_pad_2) & 0x00FFFF; 275 276 if (dual_qdepth_disable || !cur_max_fw_cmds) 277 cur_max_fw_cmds = instance->instancet->read_fw_status_reg(instance) & 0x00FFFF; 278 else 279 ldio_threshold = 280 (instance->instancet->read_fw_status_reg(instance) & 0x00FFFF) - MEGASAS_FUSION_IOCTL_CMDS; 281 282 dev_info(&instance->pdev->dev, 283 "Current firmware supports maximum commands: %d\t LDIO threshold: %d\n", 284 cur_max_fw_cmds, ldio_threshold); 285 286 if (fw_boot_context == OCR_CONTEXT) { 287 cur_max_fw_cmds = cur_max_fw_cmds - 1; 288 if (cur_max_fw_cmds < instance->max_fw_cmds) { 289 instance->cur_can_queue = 290 cur_max_fw_cmds - (MEGASAS_FUSION_INTERNAL_CMDS + 291 MEGASAS_FUSION_IOCTL_CMDS); 292 instance->host->can_queue = instance->cur_can_queue; 293 instance->ldio_threshold = ldio_threshold; 294 } 295 } else { 296 instance->max_fw_cmds = cur_max_fw_cmds; 297 instance->ldio_threshold = ldio_threshold; 298 299 if (reset_devices) 300 instance->max_fw_cmds = min(instance->max_fw_cmds, 301 (u16)MEGASAS_KDUMP_QUEUE_DEPTH); 302 /* 303 * Reduce the max supported cmds by 1. This is to ensure that the 304 * reply_q_sz (1 more than the max cmd that driver may send) 305 * does not exceed max cmds that the FW can support 306 */ 307 instance->max_fw_cmds = instance->max_fw_cmds-1; 308 } 309 } 310 /** 311 * megasas_free_cmds_fusion - Free all the cmds in the free cmd pool 312 * @instance: Adapter soft state 313 */ 314 void 315 megasas_free_cmds_fusion(struct megasas_instance *instance) 316 { 317 int i; 318 struct fusion_context *fusion = instance->ctrl_context; 319 struct megasas_cmd_fusion *cmd; 320 321 if (fusion->sense) 322 dma_pool_free(fusion->sense_dma_pool, fusion->sense, 323 fusion->sense_phys_addr); 324 325 /* SG */ 326 if (fusion->cmd_list) { 327 for (i = 0; i < instance->max_mpt_cmds; i++) { 328 cmd = fusion->cmd_list[i]; 329 if (cmd) { 330 if (cmd->sg_frame) 331 dma_pool_free(fusion->sg_dma_pool, 332 cmd->sg_frame, 333 cmd->sg_frame_phys_addr); 334 } 335 kfree(cmd); 336 } 337 kfree(fusion->cmd_list); 338 } 339 340 if (fusion->sg_dma_pool) { 341 dma_pool_destroy(fusion->sg_dma_pool); 342 fusion->sg_dma_pool = NULL; 343 } 344 if (fusion->sense_dma_pool) { 345 dma_pool_destroy(fusion->sense_dma_pool); 346 fusion->sense_dma_pool = NULL; 347 } 348 349 350 /* Reply Frame, Desc*/ 351 if (instance->is_rdpq) 352 megasas_free_rdpq_fusion(instance); 353 else 354 megasas_free_reply_fusion(instance); 355 356 /* Request Frame, Desc*/ 357 if (fusion->req_frames_desc) 358 dma_free_coherent(&instance->pdev->dev, 359 fusion->request_alloc_sz, fusion->req_frames_desc, 360 fusion->req_frames_desc_phys); 361 if (fusion->io_request_frames) 362 dma_pool_free(fusion->io_request_frames_pool, 363 fusion->io_request_frames, 364 fusion->io_request_frames_phys); 365 if (fusion->io_request_frames_pool) { 366 dma_pool_destroy(fusion->io_request_frames_pool); 367 fusion->io_request_frames_pool = NULL; 368 } 369 } 370 371 /** 372 * megasas_create_sg_sense_fusion - Creates DMA pool for cmd frames 373 * @instance: Adapter soft state 374 * 375 */ 376 static int megasas_create_sg_sense_fusion(struct megasas_instance *instance) 377 { 378 int i; 379 u16 max_cmd; 380 struct fusion_context *fusion; 381 struct megasas_cmd_fusion *cmd; 382 int sense_sz; 383 u32 offset; 384 385 fusion = instance->ctrl_context; 386 max_cmd = instance->max_fw_cmds; 387 sense_sz = instance->max_mpt_cmds * SCSI_SENSE_BUFFERSIZE; 388 389 fusion->sg_dma_pool = 390 dma_pool_create("mr_sg", &instance->pdev->dev, 391 instance->max_chain_frame_sz, 392 MR_DEFAULT_NVME_PAGE_SIZE, 0); 393 /* SCSI_SENSE_BUFFERSIZE = 96 bytes */ 394 fusion->sense_dma_pool = 395 dma_pool_create("mr_sense", &instance->pdev->dev, 396 sense_sz, 64, 0); 397 398 if (!fusion->sense_dma_pool || !fusion->sg_dma_pool) { 399 dev_err(&instance->pdev->dev, 400 "Failed from %s %d\n", __func__, __LINE__); 401 return -ENOMEM; 402 } 403 404 fusion->sense = dma_pool_alloc(fusion->sense_dma_pool, 405 GFP_KERNEL, &fusion->sense_phys_addr); 406 if (!fusion->sense) { 407 dev_err(&instance->pdev->dev, 408 "failed from %s %d\n", __func__, __LINE__); 409 return -ENOMEM; 410 } 411 412 /* sense buffer, request frame and reply desc pool requires to be in 413 * same 4 gb region. Below function will check this. 414 * In case of failure, new pci pool will be created with updated 415 * alignment. 416 * Older allocation and pool will be destroyed. 417 * Alignment will be used such a way that next allocation if success, 418 * will always meet same 4gb region requirement. 419 * Actual requirement is not alignment, but we need start and end of 420 * DMA address must have same upper 32 bit address. 421 */ 422 423 if (!megasas_check_same_4gb_region(instance, fusion->sense_phys_addr, 424 sense_sz)) { 425 dma_pool_free(fusion->sense_dma_pool, fusion->sense, 426 fusion->sense_phys_addr); 427 fusion->sense = NULL; 428 dma_pool_destroy(fusion->sense_dma_pool); 429 430 fusion->sense_dma_pool = 431 dma_pool_create("mr_sense_align", &instance->pdev->dev, 432 sense_sz, roundup_pow_of_two(sense_sz), 433 0); 434 if (!fusion->sense_dma_pool) { 435 dev_err(&instance->pdev->dev, 436 "Failed from %s %d\n", __func__, __LINE__); 437 return -ENOMEM; 438 } 439 fusion->sense = dma_pool_alloc(fusion->sense_dma_pool, 440 GFP_KERNEL, 441 &fusion->sense_phys_addr); 442 if (!fusion->sense) { 443 dev_err(&instance->pdev->dev, 444 "failed from %s %d\n", __func__, __LINE__); 445 return -ENOMEM; 446 } 447 } 448 449 /* 450 * Allocate and attach a frame to each of the commands in cmd_list 451 */ 452 for (i = 0; i < max_cmd; i++) { 453 cmd = fusion->cmd_list[i]; 454 cmd->sg_frame = dma_pool_alloc(fusion->sg_dma_pool, 455 GFP_KERNEL, &cmd->sg_frame_phys_addr); 456 457 offset = SCSI_SENSE_BUFFERSIZE * i; 458 cmd->sense = (u8 *)fusion->sense + offset; 459 cmd->sense_phys_addr = fusion->sense_phys_addr + offset; 460 461 if (!cmd->sg_frame) { 462 dev_err(&instance->pdev->dev, 463 "Failed from %s %d\n", __func__, __LINE__); 464 return -ENOMEM; 465 } 466 } 467 468 /* create sense buffer for the raid 1/10 fp */ 469 for (i = max_cmd; i < instance->max_mpt_cmds; i++) { 470 cmd = fusion->cmd_list[i]; 471 offset = SCSI_SENSE_BUFFERSIZE * i; 472 cmd->sense = (u8 *)fusion->sense + offset; 473 cmd->sense_phys_addr = fusion->sense_phys_addr + offset; 474 475 } 476 477 return 0; 478 } 479 480 int 481 megasas_alloc_cmdlist_fusion(struct megasas_instance *instance) 482 { 483 u32 max_mpt_cmd, i, j; 484 struct fusion_context *fusion; 485 486 fusion = instance->ctrl_context; 487 488 max_mpt_cmd = instance->max_mpt_cmds; 489 490 /* 491 * fusion->cmd_list is an array of struct megasas_cmd_fusion pointers. 492 * Allocate the dynamic array first and then allocate individual 493 * commands. 494 */ 495 fusion->cmd_list = 496 kcalloc(max_mpt_cmd, sizeof(struct megasas_cmd_fusion *), 497 GFP_KERNEL); 498 if (!fusion->cmd_list) { 499 dev_err(&instance->pdev->dev, 500 "Failed from %s %d\n", __func__, __LINE__); 501 return -ENOMEM; 502 } 503 504 for (i = 0; i < max_mpt_cmd; i++) { 505 fusion->cmd_list[i] = kzalloc(sizeof(struct megasas_cmd_fusion), 506 GFP_KERNEL); 507 if (!fusion->cmd_list[i]) { 508 for (j = 0; j < i; j++) 509 kfree(fusion->cmd_list[j]); 510 kfree(fusion->cmd_list); 511 dev_err(&instance->pdev->dev, 512 "Failed from %s %d\n", __func__, __LINE__); 513 return -ENOMEM; 514 } 515 } 516 517 return 0; 518 } 519 int 520 megasas_alloc_request_fusion(struct megasas_instance *instance) 521 { 522 struct fusion_context *fusion; 523 524 fusion = instance->ctrl_context; 525 526 retry_alloc: 527 fusion->io_request_frames_pool = 528 dma_pool_create("mr_ioreq", &instance->pdev->dev, 529 fusion->io_frames_alloc_sz, 16, 0); 530 531 if (!fusion->io_request_frames_pool) { 532 dev_err(&instance->pdev->dev, 533 "Failed from %s %d\n", __func__, __LINE__); 534 return -ENOMEM; 535 } 536 537 fusion->io_request_frames = 538 dma_pool_alloc(fusion->io_request_frames_pool, 539 GFP_KERNEL, &fusion->io_request_frames_phys); 540 if (!fusion->io_request_frames) { 541 if (instance->max_fw_cmds >= (MEGASAS_REDUCE_QD_COUNT * 2)) { 542 instance->max_fw_cmds -= MEGASAS_REDUCE_QD_COUNT; 543 dma_pool_destroy(fusion->io_request_frames_pool); 544 megasas_configure_queue_sizes(instance); 545 goto retry_alloc; 546 } else { 547 dev_err(&instance->pdev->dev, 548 "Failed from %s %d\n", __func__, __LINE__); 549 return -ENOMEM; 550 } 551 } 552 553 if (!megasas_check_same_4gb_region(instance, 554 fusion->io_request_frames_phys, 555 fusion->io_frames_alloc_sz)) { 556 dma_pool_free(fusion->io_request_frames_pool, 557 fusion->io_request_frames, 558 fusion->io_request_frames_phys); 559 fusion->io_request_frames = NULL; 560 dma_pool_destroy(fusion->io_request_frames_pool); 561 562 fusion->io_request_frames_pool = 563 dma_pool_create("mr_ioreq_align", 564 &instance->pdev->dev, 565 fusion->io_frames_alloc_sz, 566 roundup_pow_of_two(fusion->io_frames_alloc_sz), 567 0); 568 569 if (!fusion->io_request_frames_pool) { 570 dev_err(&instance->pdev->dev, 571 "Failed from %s %d\n", __func__, __LINE__); 572 return -ENOMEM; 573 } 574 575 fusion->io_request_frames = 576 dma_pool_alloc(fusion->io_request_frames_pool, 577 GFP_KERNEL, 578 &fusion->io_request_frames_phys); 579 580 if (!fusion->io_request_frames) { 581 dev_err(&instance->pdev->dev, 582 "Failed from %s %d\n", __func__, __LINE__); 583 return -ENOMEM; 584 } 585 } 586 587 fusion->req_frames_desc = 588 dma_alloc_coherent(&instance->pdev->dev, 589 fusion->request_alloc_sz, 590 &fusion->req_frames_desc_phys, GFP_KERNEL); 591 if (!fusion->req_frames_desc) { 592 dev_err(&instance->pdev->dev, 593 "Failed from %s %d\n", __func__, __LINE__); 594 return -ENOMEM; 595 } 596 597 return 0; 598 } 599 600 int 601 megasas_alloc_reply_fusion(struct megasas_instance *instance) 602 { 603 int i, count; 604 struct fusion_context *fusion; 605 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc; 606 fusion = instance->ctrl_context; 607 608 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1; 609 fusion->reply_frames_desc_pool = 610 dma_pool_create("mr_reply", &instance->pdev->dev, 611 fusion->reply_alloc_sz * count, 16, 0); 612 613 if (!fusion->reply_frames_desc_pool) { 614 dev_err(&instance->pdev->dev, 615 "Failed from %s %d\n", __func__, __LINE__); 616 return -ENOMEM; 617 } 618 619 fusion->reply_frames_desc[0] = 620 dma_pool_alloc(fusion->reply_frames_desc_pool, 621 GFP_KERNEL, &fusion->reply_frames_desc_phys[0]); 622 if (!fusion->reply_frames_desc[0]) { 623 dev_err(&instance->pdev->dev, 624 "Failed from %s %d\n", __func__, __LINE__); 625 return -ENOMEM; 626 } 627 628 if (!megasas_check_same_4gb_region(instance, 629 fusion->reply_frames_desc_phys[0], 630 (fusion->reply_alloc_sz * count))) { 631 dma_pool_free(fusion->reply_frames_desc_pool, 632 fusion->reply_frames_desc[0], 633 fusion->reply_frames_desc_phys[0]); 634 fusion->reply_frames_desc[0] = NULL; 635 dma_pool_destroy(fusion->reply_frames_desc_pool); 636 637 fusion->reply_frames_desc_pool = 638 dma_pool_create("mr_reply_align", 639 &instance->pdev->dev, 640 fusion->reply_alloc_sz * count, 641 roundup_pow_of_two(fusion->reply_alloc_sz * count), 642 0); 643 644 if (!fusion->reply_frames_desc_pool) { 645 dev_err(&instance->pdev->dev, 646 "Failed from %s %d\n", __func__, __LINE__); 647 return -ENOMEM; 648 } 649 650 fusion->reply_frames_desc[0] = 651 dma_pool_alloc(fusion->reply_frames_desc_pool, 652 GFP_KERNEL, 653 &fusion->reply_frames_desc_phys[0]); 654 655 if (!fusion->reply_frames_desc[0]) { 656 dev_err(&instance->pdev->dev, 657 "Failed from %s %d\n", __func__, __LINE__); 658 return -ENOMEM; 659 } 660 } 661 662 reply_desc = fusion->reply_frames_desc[0]; 663 for (i = 0; i < fusion->reply_q_depth * count; i++, reply_desc++) 664 reply_desc->Words = cpu_to_le64(ULLONG_MAX); 665 666 /* This is not a rdpq mode, but driver still populate 667 * reply_frame_desc array to use same msix index in ISR path. 668 */ 669 for (i = 0; i < (count - 1); i++) 670 fusion->reply_frames_desc[i + 1] = 671 fusion->reply_frames_desc[i] + 672 (fusion->reply_alloc_sz)/sizeof(union MPI2_REPLY_DESCRIPTORS_UNION); 673 674 return 0; 675 } 676 677 int 678 megasas_alloc_rdpq_fusion(struct megasas_instance *instance) 679 { 680 int i, j, k, msix_count; 681 struct fusion_context *fusion; 682 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc; 683 union MPI2_REPLY_DESCRIPTORS_UNION *rdpq_chunk_virt[RDPQ_MAX_CHUNK_COUNT]; 684 dma_addr_t rdpq_chunk_phys[RDPQ_MAX_CHUNK_COUNT]; 685 u8 dma_alloc_count, abs_index; 686 u32 chunk_size, array_size, offset; 687 688 fusion = instance->ctrl_context; 689 chunk_size = fusion->reply_alloc_sz * RDPQ_MAX_INDEX_IN_ONE_CHUNK; 690 array_size = sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * 691 MAX_MSIX_QUEUES_FUSION; 692 693 fusion->rdpq_virt = dma_alloc_coherent(&instance->pdev->dev, 694 array_size, &fusion->rdpq_phys, 695 GFP_KERNEL); 696 if (!fusion->rdpq_virt) { 697 dev_err(&instance->pdev->dev, 698 "Failed from %s %d\n", __func__, __LINE__); 699 return -ENOMEM; 700 } 701 702 msix_count = instance->msix_vectors > 0 ? instance->msix_vectors : 1; 703 704 fusion->reply_frames_desc_pool = dma_pool_create("mr_rdpq", 705 &instance->pdev->dev, 706 chunk_size, 16, 0); 707 fusion->reply_frames_desc_pool_align = 708 dma_pool_create("mr_rdpq_align", 709 &instance->pdev->dev, 710 chunk_size, 711 roundup_pow_of_two(chunk_size), 712 0); 713 714 if (!fusion->reply_frames_desc_pool || 715 !fusion->reply_frames_desc_pool_align) { 716 dev_err(&instance->pdev->dev, 717 "Failed from %s %d\n", __func__, __LINE__); 718 return -ENOMEM; 719 } 720 721 /* 722 * For INVADER_SERIES each set of 8 reply queues(0-7, 8-15, ..) and 723 * VENTURA_SERIES each set of 16 reply queues(0-15, 16-31, ..) should be 724 * within 4GB boundary and also reply queues in a set must have same 725 * upper 32-bits in their memory address. so here driver is allocating the 726 * DMA'able memory for reply queues according. Driver uses limitation of 727 * VENTURA_SERIES to manage INVADER_SERIES as well. 728 */ 729 dma_alloc_count = DIV_ROUND_UP(msix_count, RDPQ_MAX_INDEX_IN_ONE_CHUNK); 730 731 for (i = 0; i < dma_alloc_count; i++) { 732 rdpq_chunk_virt[i] = 733 dma_pool_alloc(fusion->reply_frames_desc_pool, 734 GFP_KERNEL, &rdpq_chunk_phys[i]); 735 if (!rdpq_chunk_virt[i]) { 736 dev_err(&instance->pdev->dev, 737 "Failed from %s %d\n", __func__, __LINE__); 738 return -ENOMEM; 739 } 740 /* reply desc pool requires to be in same 4 gb region. 741 * Below function will check this. 742 * In case of failure, new pci pool will be created with updated 743 * alignment. 744 * For RDPQ buffers, driver always allocate two separate pci pool. 745 * Alignment will be used such a way that next allocation if 746 * success, will always meet same 4gb region requirement. 747 * rdpq_tracker keep track of each buffer's physical, 748 * virtual address and pci pool descriptor. It will help driver 749 * while freeing the resources. 750 * 751 */ 752 if (!megasas_check_same_4gb_region(instance, rdpq_chunk_phys[i], 753 chunk_size)) { 754 dma_pool_free(fusion->reply_frames_desc_pool, 755 rdpq_chunk_virt[i], 756 rdpq_chunk_phys[i]); 757 758 rdpq_chunk_virt[i] = 759 dma_pool_alloc(fusion->reply_frames_desc_pool_align, 760 GFP_KERNEL, &rdpq_chunk_phys[i]); 761 if (!rdpq_chunk_virt[i]) { 762 dev_err(&instance->pdev->dev, 763 "Failed from %s %d\n", 764 __func__, __LINE__); 765 return -ENOMEM; 766 } 767 fusion->rdpq_tracker[i].dma_pool_ptr = 768 fusion->reply_frames_desc_pool_align; 769 } else { 770 fusion->rdpq_tracker[i].dma_pool_ptr = 771 fusion->reply_frames_desc_pool; 772 } 773 774 fusion->rdpq_tracker[i].pool_entry_phys = rdpq_chunk_phys[i]; 775 fusion->rdpq_tracker[i].pool_entry_virt = rdpq_chunk_virt[i]; 776 } 777 778 for (k = 0; k < dma_alloc_count; k++) { 779 for (i = 0; i < RDPQ_MAX_INDEX_IN_ONE_CHUNK; i++) { 780 abs_index = (k * RDPQ_MAX_INDEX_IN_ONE_CHUNK) + i; 781 782 if (abs_index == msix_count) 783 break; 784 offset = fusion->reply_alloc_sz * i; 785 fusion->rdpq_virt[abs_index].RDPQBaseAddress = 786 cpu_to_le64(rdpq_chunk_phys[k] + offset); 787 fusion->reply_frames_desc_phys[abs_index] = 788 rdpq_chunk_phys[k] + offset; 789 fusion->reply_frames_desc[abs_index] = 790 (union MPI2_REPLY_DESCRIPTORS_UNION *)((u8 *)rdpq_chunk_virt[k] + offset); 791 792 reply_desc = fusion->reply_frames_desc[abs_index]; 793 for (j = 0; j < fusion->reply_q_depth; j++, reply_desc++) 794 reply_desc->Words = ULLONG_MAX; 795 } 796 } 797 798 return 0; 799 } 800 801 static void 802 megasas_free_rdpq_fusion(struct megasas_instance *instance) { 803 804 int i; 805 struct fusion_context *fusion; 806 807 fusion = instance->ctrl_context; 808 809 for (i = 0; i < RDPQ_MAX_CHUNK_COUNT; i++) { 810 if (fusion->rdpq_tracker[i].pool_entry_virt) 811 dma_pool_free(fusion->rdpq_tracker[i].dma_pool_ptr, 812 fusion->rdpq_tracker[i].pool_entry_virt, 813 fusion->rdpq_tracker[i].pool_entry_phys); 814 815 } 816 817 dma_pool_destroy(fusion->reply_frames_desc_pool); 818 dma_pool_destroy(fusion->reply_frames_desc_pool_align); 819 820 if (fusion->rdpq_virt) 821 dma_free_coherent(&instance->pdev->dev, 822 sizeof(struct MPI2_IOC_INIT_RDPQ_ARRAY_ENTRY) * MAX_MSIX_QUEUES_FUSION, 823 fusion->rdpq_virt, fusion->rdpq_phys); 824 } 825 826 static void 827 megasas_free_reply_fusion(struct megasas_instance *instance) { 828 829 struct fusion_context *fusion; 830 831 fusion = instance->ctrl_context; 832 833 if (fusion->reply_frames_desc[0]) 834 dma_pool_free(fusion->reply_frames_desc_pool, 835 fusion->reply_frames_desc[0], 836 fusion->reply_frames_desc_phys[0]); 837 838 dma_pool_destroy(fusion->reply_frames_desc_pool); 839 840 } 841 842 843 /** 844 * megasas_alloc_cmds_fusion - Allocates the command packets 845 * @instance: Adapter soft state 846 * 847 * 848 * Each frame has a 32-bit field called context. This context is used to get 849 * back the megasas_cmd_fusion from the frame when a frame gets completed 850 * In this driver, the 32 bit values are the indices into an array cmd_list. 851 * This array is used only to look up the megasas_cmd_fusion given the context. 852 * The free commands themselves are maintained in a linked list called cmd_pool. 853 * 854 * cmds are formed in the io_request and sg_frame members of the 855 * megasas_cmd_fusion. The context field is used to get a request descriptor 856 * and is used as SMID of the cmd. 857 * SMID value range is from 1 to max_fw_cmds. 858 */ 859 int 860 megasas_alloc_cmds_fusion(struct megasas_instance *instance) 861 { 862 int i; 863 struct fusion_context *fusion; 864 struct megasas_cmd_fusion *cmd; 865 u32 offset; 866 dma_addr_t io_req_base_phys; 867 u8 *io_req_base; 868 869 870 fusion = instance->ctrl_context; 871 872 if (megasas_alloc_request_fusion(instance)) 873 goto fail_exit; 874 875 if (instance->is_rdpq) { 876 if (megasas_alloc_rdpq_fusion(instance)) 877 goto fail_exit; 878 } else 879 if (megasas_alloc_reply_fusion(instance)) 880 goto fail_exit; 881 882 if (megasas_alloc_cmdlist_fusion(instance)) 883 goto fail_exit; 884 885 dev_info(&instance->pdev->dev, "Configured max firmware commands: %d\n", 886 instance->max_fw_cmds); 887 888 /* The first 256 bytes (SMID 0) is not used. Don't add to the cmd list */ 889 io_req_base = fusion->io_request_frames + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE; 890 io_req_base_phys = fusion->io_request_frames_phys + MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE; 891 892 /* 893 * Add all the commands to command pool (fusion->cmd_pool) 894 */ 895 896 /* SMID 0 is reserved. Set SMID/index from 1 */ 897 for (i = 0; i < instance->max_mpt_cmds; i++) { 898 cmd = fusion->cmd_list[i]; 899 offset = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE * i; 900 memset(cmd, 0, sizeof(struct megasas_cmd_fusion)); 901 cmd->index = i + 1; 902 cmd->scmd = NULL; 903 cmd->sync_cmd_idx = 904 (i >= instance->max_scsi_cmds && i < instance->max_fw_cmds) ? 905 (i - instance->max_scsi_cmds) : 906 (u32)ULONG_MAX; /* Set to Invalid */ 907 cmd->instance = instance; 908 cmd->io_request = 909 (struct MPI2_RAID_SCSI_IO_REQUEST *) 910 (io_req_base + offset); 911 memset(cmd->io_request, 0, 912 sizeof(struct MPI2_RAID_SCSI_IO_REQUEST)); 913 cmd->io_request_phys_addr = io_req_base_phys + offset; 914 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID; 915 } 916 917 if (megasas_create_sg_sense_fusion(instance)) 918 goto fail_exit; 919 920 return 0; 921 922 fail_exit: 923 megasas_free_cmds_fusion(instance); 924 return -ENOMEM; 925 } 926 927 /** 928 * wait_and_poll - Issues a polling command 929 * @instance: Adapter soft state 930 * @cmd: Command packet to be issued 931 * 932 * For polling, MFI requires the cmd_status to be set to 0xFF before posting. 933 */ 934 int 935 wait_and_poll(struct megasas_instance *instance, struct megasas_cmd *cmd, 936 int seconds) 937 { 938 int i; 939 struct megasas_header *frame_hdr = &cmd->frame->hdr; 940 struct fusion_context *fusion; 941 942 u32 msecs = seconds * 1000; 943 944 fusion = instance->ctrl_context; 945 /* 946 * Wait for cmd_status to change 947 */ 948 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i += 20) { 949 rmb(); 950 msleep(20); 951 } 952 953 if (frame_hdr->cmd_status == MFI_STAT_INVALID_STATUS) 954 return DCMD_TIMEOUT; 955 else if (frame_hdr->cmd_status == MFI_STAT_OK) 956 return DCMD_SUCCESS; 957 else 958 return DCMD_FAILED; 959 } 960 961 /** 962 * megasas_ioc_init_fusion - Initializes the FW 963 * @instance: Adapter soft state 964 * 965 * Issues the IOC Init cmd 966 */ 967 int 968 megasas_ioc_init_fusion(struct megasas_instance *instance) 969 { 970 struct megasas_init_frame *init_frame; 971 struct MPI2_IOC_INIT_REQUEST *IOCInitMessage = NULL; 972 dma_addr_t ioc_init_handle; 973 struct megasas_cmd *cmd; 974 u8 ret, cur_rdpq_mode; 975 struct fusion_context *fusion; 976 union MEGASAS_REQUEST_DESCRIPTOR_UNION req_desc; 977 int i; 978 struct megasas_header *frame_hdr; 979 const char *sys_info; 980 MFI_CAPABILITIES *drv_ops; 981 u32 scratch_pad_1; 982 ktime_t time; 983 bool cur_fw_64bit_dma_capable; 984 985 fusion = instance->ctrl_context; 986 987 ioc_init_handle = fusion->ioc_init_request_phys; 988 IOCInitMessage = fusion->ioc_init_request; 989 990 cmd = fusion->ioc_init_cmd; 991 992 scratch_pad_1 = megasas_readl 993 (instance, &instance->reg_set->outbound_scratch_pad_1); 994 995 cur_rdpq_mode = (scratch_pad_1 & MR_RDPQ_MODE_OFFSET) ? 1 : 0; 996 997 if (instance->adapter_type == INVADER_SERIES) { 998 cur_fw_64bit_dma_capable = 999 (scratch_pad_1 & MR_CAN_HANDLE_64_BIT_DMA_OFFSET) ? true : false; 1000 1001 if (instance->consistent_mask_64bit && !cur_fw_64bit_dma_capable) { 1002 dev_err(&instance->pdev->dev, "Driver was operating on 64bit " 1003 "DMA mask, but upcoming FW does not support 64bit DMA mask\n"); 1004 megaraid_sas_kill_hba(instance); 1005 ret = 1; 1006 goto fail_fw_init; 1007 } 1008 } 1009 1010 if (instance->is_rdpq && !cur_rdpq_mode) { 1011 dev_err(&instance->pdev->dev, "Firmware downgrade *NOT SUPPORTED*" 1012 " from RDPQ mode to non RDPQ mode\n"); 1013 ret = 1; 1014 goto fail_fw_init; 1015 } 1016 1017 instance->fw_sync_cache_support = (scratch_pad_1 & 1018 MR_CAN_HANDLE_SYNC_CACHE_OFFSET) ? 1 : 0; 1019 dev_info(&instance->pdev->dev, "FW supports sync cache\t: %s\n", 1020 instance->fw_sync_cache_support ? "Yes" : "No"); 1021 1022 memset(IOCInitMessage, 0, sizeof(struct MPI2_IOC_INIT_REQUEST)); 1023 1024 IOCInitMessage->Function = MPI2_FUNCTION_IOC_INIT; 1025 IOCInitMessage->WhoInit = MPI2_WHOINIT_HOST_DRIVER; 1026 IOCInitMessage->MsgVersion = cpu_to_le16(MPI2_VERSION); 1027 IOCInitMessage->HeaderVersion = cpu_to_le16(MPI2_HEADER_VERSION); 1028 IOCInitMessage->SystemRequestFrameSize = cpu_to_le16(MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE / 4); 1029 1030 IOCInitMessage->ReplyDescriptorPostQueueDepth = cpu_to_le16(fusion->reply_q_depth); 1031 IOCInitMessage->ReplyDescriptorPostQueueAddress = instance->is_rdpq ? 1032 cpu_to_le64(fusion->rdpq_phys) : 1033 cpu_to_le64(fusion->reply_frames_desc_phys[0]); 1034 IOCInitMessage->MsgFlags = instance->is_rdpq ? 1035 MPI2_IOCINIT_MSGFLAG_RDPQ_ARRAY_MODE : 0; 1036 IOCInitMessage->SystemRequestFrameBaseAddress = cpu_to_le64(fusion->io_request_frames_phys); 1037 IOCInitMessage->SenseBufferAddressHigh = cpu_to_le32(upper_32_bits(fusion->sense_phys_addr)); 1038 IOCInitMessage->HostMSIxVectors = instance->msix_vectors; 1039 IOCInitMessage->HostPageSize = MR_DEFAULT_NVME_PAGE_SHIFT; 1040 1041 time = ktime_get_real(); 1042 /* Convert to milliseconds as per FW requirement */ 1043 IOCInitMessage->TimeStamp = cpu_to_le64(ktime_to_ms(time)); 1044 1045 init_frame = (struct megasas_init_frame *)cmd->frame; 1046 memset(init_frame, 0, IOC_INIT_FRAME_SIZE); 1047 1048 frame_hdr = &cmd->frame->hdr; 1049 frame_hdr->cmd_status = 0xFF; 1050 frame_hdr->flags |= cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE); 1051 1052 init_frame->cmd = MFI_CMD_INIT; 1053 init_frame->cmd_status = 0xFF; 1054 1055 drv_ops = (MFI_CAPABILITIES *) &(init_frame->driver_operations); 1056 1057 /* driver support Extended MSIX */ 1058 if (instance->adapter_type >= INVADER_SERIES) 1059 drv_ops->mfi_capabilities.support_additional_msix = 1; 1060 /* driver supports HA / Remote LUN over Fast Path interface */ 1061 drv_ops->mfi_capabilities.support_fp_remote_lun = 1; 1062 1063 drv_ops->mfi_capabilities.support_max_255lds = 1; 1064 drv_ops->mfi_capabilities.support_ndrive_r1_lb = 1; 1065 drv_ops->mfi_capabilities.security_protocol_cmds_fw = 1; 1066 1067 if (instance->max_chain_frame_sz > MEGASAS_CHAIN_FRAME_SZ_MIN) 1068 drv_ops->mfi_capabilities.support_ext_io_size = 1; 1069 1070 drv_ops->mfi_capabilities.support_fp_rlbypass = 1; 1071 if (!dual_qdepth_disable) 1072 drv_ops->mfi_capabilities.support_ext_queue_depth = 1; 1073 1074 drv_ops->mfi_capabilities.support_qd_throttling = 1; 1075 drv_ops->mfi_capabilities.support_pd_map_target_id = 1; 1076 drv_ops->mfi_capabilities.support_nvme_passthru = 1; 1077 1078 if (instance->consistent_mask_64bit) 1079 drv_ops->mfi_capabilities.support_64bit_mode = 1; 1080 1081 /* Convert capability to LE32 */ 1082 cpu_to_le32s((u32 *)&init_frame->driver_operations.mfi_capabilities); 1083 1084 sys_info = dmi_get_system_info(DMI_PRODUCT_UUID); 1085 if (instance->system_info_buf && sys_info) { 1086 memcpy(instance->system_info_buf->systemId, sys_info, 1087 strlen(sys_info) > 64 ? 64 : strlen(sys_info)); 1088 instance->system_info_buf->systemIdLength = 1089 strlen(sys_info) > 64 ? 64 : strlen(sys_info); 1090 init_frame->system_info_lo = cpu_to_le32(lower_32_bits(instance->system_info_h)); 1091 init_frame->system_info_hi = cpu_to_le32(upper_32_bits(instance->system_info_h)); 1092 } 1093 1094 init_frame->queue_info_new_phys_addr_hi = 1095 cpu_to_le32(upper_32_bits(ioc_init_handle)); 1096 init_frame->queue_info_new_phys_addr_lo = 1097 cpu_to_le32(lower_32_bits(ioc_init_handle)); 1098 init_frame->data_xfer_len = cpu_to_le32(sizeof(struct MPI2_IOC_INIT_REQUEST)); 1099 1100 req_desc.u.low = cpu_to_le32(lower_32_bits(cmd->frame_phys_addr)); 1101 req_desc.u.high = cpu_to_le32(upper_32_bits(cmd->frame_phys_addr)); 1102 req_desc.MFAIo.RequestFlags = 1103 (MEGASAS_REQ_DESCRIPT_FLAGS_MFA << 1104 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 1105 1106 /* 1107 * disable the intr before firing the init frame 1108 */ 1109 instance->instancet->disable_intr(instance); 1110 1111 for (i = 0; i < (10 * 1000); i += 20) { 1112 if (megasas_readl(instance, &instance->reg_set->doorbell) & 1) 1113 msleep(20); 1114 else 1115 break; 1116 } 1117 1118 megasas_fire_cmd_fusion(instance, &req_desc); 1119 1120 wait_and_poll(instance, cmd, MFI_IO_TIMEOUT_SECS); 1121 1122 frame_hdr = &cmd->frame->hdr; 1123 if (frame_hdr->cmd_status != 0) { 1124 ret = 1; 1125 goto fail_fw_init; 1126 } 1127 1128 return 0; 1129 1130 fail_fw_init: 1131 dev_err(&instance->pdev->dev, 1132 "Init cmd return status FAILED for SCSI host %d\n", 1133 instance->host->host_no); 1134 1135 return ret; 1136 } 1137 1138 /** 1139 * megasas_sync_pd_seq_num - JBOD SEQ MAP 1140 * @instance: Adapter soft state 1141 * @pend: set to 1, if it is pended jbod map. 1142 * 1143 * Issue Jbod map to the firmware. If it is pended command, 1144 * issue command and return. If it is first instance of jbod map 1145 * issue and receive command. 1146 */ 1147 int 1148 megasas_sync_pd_seq_num(struct megasas_instance *instance, bool pend) { 1149 int ret = 0; 1150 u32 pd_seq_map_sz; 1151 struct megasas_cmd *cmd; 1152 struct megasas_dcmd_frame *dcmd; 1153 struct fusion_context *fusion = instance->ctrl_context; 1154 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync; 1155 dma_addr_t pd_seq_h; 1156 1157 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id & 1)]; 1158 pd_seq_h = fusion->pd_seq_phys[(instance->pd_seq_map_id & 1)]; 1159 pd_seq_map_sz = sizeof(struct MR_PD_CFG_SEQ_NUM_SYNC) + 1160 (sizeof(struct MR_PD_CFG_SEQ) * 1161 (MAX_PHYSICAL_DEVICES - 1)); 1162 1163 cmd = megasas_get_cmd(instance); 1164 if (!cmd) { 1165 dev_err(&instance->pdev->dev, 1166 "Could not get mfi cmd. Fail from %s %d\n", 1167 __func__, __LINE__); 1168 return -ENOMEM; 1169 } 1170 1171 dcmd = &cmd->frame->dcmd; 1172 1173 memset(pd_sync, 0, pd_seq_map_sz); 1174 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE); 1175 1176 if (pend) { 1177 dcmd->mbox.b[0] = MEGASAS_DCMD_MBOX_PEND_FLAG; 1178 dcmd->flags = MFI_FRAME_DIR_WRITE; 1179 instance->jbod_seq_cmd = cmd; 1180 } else { 1181 dcmd->flags = MFI_FRAME_DIR_READ; 1182 } 1183 1184 dcmd->cmd = MFI_CMD_DCMD; 1185 dcmd->cmd_status = 0xFF; 1186 dcmd->sge_count = 1; 1187 dcmd->timeout = 0; 1188 dcmd->pad_0 = 0; 1189 dcmd->data_xfer_len = cpu_to_le32(pd_seq_map_sz); 1190 dcmd->opcode = cpu_to_le32(MR_DCMD_SYSTEM_PD_MAP_GET_INFO); 1191 1192 megasas_set_dma_settings(instance, dcmd, pd_seq_h, pd_seq_map_sz); 1193 1194 if (pend) { 1195 instance->instancet->issue_dcmd(instance, cmd); 1196 return 0; 1197 } 1198 1199 /* Below code is only for non pended DCMD */ 1200 if (!instance->mask_interrupts) 1201 ret = megasas_issue_blocked_cmd(instance, cmd, 1202 MFI_IO_TIMEOUT_SECS); 1203 else 1204 ret = megasas_issue_polled(instance, cmd); 1205 1206 if (le32_to_cpu(pd_sync->count) > MAX_PHYSICAL_DEVICES) { 1207 dev_warn(&instance->pdev->dev, 1208 "driver supports max %d JBOD, but FW reports %d\n", 1209 MAX_PHYSICAL_DEVICES, le32_to_cpu(pd_sync->count)); 1210 ret = -EINVAL; 1211 } 1212 1213 if (ret == DCMD_TIMEOUT) 1214 megaraid_sas_kill_hba(instance); 1215 1216 if (ret == DCMD_SUCCESS) 1217 instance->pd_seq_map_id++; 1218 1219 megasas_return_cmd(instance, cmd); 1220 return ret; 1221 } 1222 1223 /* 1224 * megasas_get_ld_map_info - Returns FW's ld_map structure 1225 * @instance: Adapter soft state 1226 * @pend: Pend the command or not 1227 * Issues an internal command (DCMD) to get the FW's controller PD 1228 * list structure. This information is mainly used to find out SYSTEM 1229 * supported by the FW. 1230 * dcmd.mbox value setting for MR_DCMD_LD_MAP_GET_INFO 1231 * dcmd.mbox.b[0] - number of LDs being sync'd 1232 * dcmd.mbox.b[1] - 0 - complete command immediately. 1233 * - 1 - pend till config change 1234 * dcmd.mbox.b[2] - 0 - supports max 64 lds and uses legacy MR_FW_RAID_MAP 1235 * - 1 - supports max MAX_LOGICAL_DRIVES_EXT lds and 1236 * uses extended struct MR_FW_RAID_MAP_EXT 1237 */ 1238 static int 1239 megasas_get_ld_map_info(struct megasas_instance *instance) 1240 { 1241 int ret = 0; 1242 struct megasas_cmd *cmd; 1243 struct megasas_dcmd_frame *dcmd; 1244 void *ci; 1245 dma_addr_t ci_h = 0; 1246 u32 size_map_info; 1247 struct fusion_context *fusion; 1248 1249 cmd = megasas_get_cmd(instance); 1250 1251 if (!cmd) { 1252 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for map info\n"); 1253 return -ENOMEM; 1254 } 1255 1256 fusion = instance->ctrl_context; 1257 1258 if (!fusion) { 1259 megasas_return_cmd(instance, cmd); 1260 return -ENXIO; 1261 } 1262 1263 dcmd = &cmd->frame->dcmd; 1264 1265 size_map_info = fusion->current_map_sz; 1266 1267 ci = (void *) fusion->ld_map[(instance->map_id & 1)]; 1268 ci_h = fusion->ld_map_phys[(instance->map_id & 1)]; 1269 1270 if (!ci) { 1271 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to alloc mem for ld_map_info\n"); 1272 megasas_return_cmd(instance, cmd); 1273 return -ENOMEM; 1274 } 1275 1276 memset(ci, 0, fusion->max_map_sz); 1277 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE); 1278 dcmd->cmd = MFI_CMD_DCMD; 1279 dcmd->cmd_status = 0xFF; 1280 dcmd->sge_count = 1; 1281 dcmd->flags = MFI_FRAME_DIR_READ; 1282 dcmd->timeout = 0; 1283 dcmd->pad_0 = 0; 1284 dcmd->data_xfer_len = cpu_to_le32(size_map_info); 1285 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO); 1286 1287 megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info); 1288 1289 if (!instance->mask_interrupts) 1290 ret = megasas_issue_blocked_cmd(instance, cmd, 1291 MFI_IO_TIMEOUT_SECS); 1292 else 1293 ret = megasas_issue_polled(instance, cmd); 1294 1295 if (ret == DCMD_TIMEOUT) 1296 megaraid_sas_kill_hba(instance); 1297 1298 megasas_return_cmd(instance, cmd); 1299 1300 return ret; 1301 } 1302 1303 u8 1304 megasas_get_map_info(struct megasas_instance *instance) 1305 { 1306 struct fusion_context *fusion = instance->ctrl_context; 1307 1308 fusion->fast_path_io = 0; 1309 if (!megasas_get_ld_map_info(instance)) { 1310 if (MR_ValidateMapInfo(instance, instance->map_id)) { 1311 fusion->fast_path_io = 1; 1312 return 0; 1313 } 1314 } 1315 return 1; 1316 } 1317 1318 /* 1319 * megasas_sync_map_info - Returns FW's ld_map structure 1320 * @instance: Adapter soft state 1321 * 1322 * Issues an internal command (DCMD) to get the FW's controller PD 1323 * list structure. This information is mainly used to find out SYSTEM 1324 * supported by the FW. 1325 */ 1326 int 1327 megasas_sync_map_info(struct megasas_instance *instance) 1328 { 1329 int i; 1330 struct megasas_cmd *cmd; 1331 struct megasas_dcmd_frame *dcmd; 1332 u16 num_lds; 1333 u32 size_sync_info; 1334 struct fusion_context *fusion; 1335 struct MR_LD_TARGET_SYNC *ci = NULL; 1336 struct MR_DRV_RAID_MAP_ALL *map; 1337 struct MR_LD_RAID *raid; 1338 struct MR_LD_TARGET_SYNC *ld_sync; 1339 dma_addr_t ci_h = 0; 1340 u32 size_map_info; 1341 1342 cmd = megasas_get_cmd(instance); 1343 1344 if (!cmd) { 1345 dev_printk(KERN_DEBUG, &instance->pdev->dev, "Failed to get cmd for sync info\n"); 1346 return -ENOMEM; 1347 } 1348 1349 fusion = instance->ctrl_context; 1350 1351 if (!fusion) { 1352 megasas_return_cmd(instance, cmd); 1353 return 1; 1354 } 1355 1356 map = fusion->ld_drv_map[instance->map_id & 1]; 1357 1358 num_lds = le16_to_cpu(map->raidMap.ldCount); 1359 1360 dcmd = &cmd->frame->dcmd; 1361 1362 size_sync_info = sizeof(struct MR_LD_TARGET_SYNC) *num_lds; 1363 1364 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE); 1365 1366 ci = (struct MR_LD_TARGET_SYNC *) 1367 fusion->ld_map[(instance->map_id - 1) & 1]; 1368 memset(ci, 0, fusion->max_map_sz); 1369 1370 ci_h = fusion->ld_map_phys[(instance->map_id - 1) & 1]; 1371 1372 ld_sync = (struct MR_LD_TARGET_SYNC *)ci; 1373 1374 for (i = 0; i < num_lds; i++, ld_sync++) { 1375 raid = MR_LdRaidGet(i, map); 1376 ld_sync->targetId = MR_GetLDTgtId(i, map); 1377 ld_sync->seqNum = raid->seqNum; 1378 } 1379 1380 size_map_info = fusion->current_map_sz; 1381 1382 dcmd->cmd = MFI_CMD_DCMD; 1383 dcmd->cmd_status = 0xFF; 1384 dcmd->sge_count = 1; 1385 dcmd->flags = MFI_FRAME_DIR_WRITE; 1386 dcmd->timeout = 0; 1387 dcmd->pad_0 = 0; 1388 dcmd->data_xfer_len = cpu_to_le32(size_map_info); 1389 dcmd->mbox.b[0] = num_lds; 1390 dcmd->mbox.b[1] = MEGASAS_DCMD_MBOX_PEND_FLAG; 1391 dcmd->opcode = cpu_to_le32(MR_DCMD_LD_MAP_GET_INFO); 1392 1393 megasas_set_dma_settings(instance, dcmd, ci_h, size_map_info); 1394 1395 instance->map_update_cmd = cmd; 1396 1397 instance->instancet->issue_dcmd(instance, cmd); 1398 1399 return 0; 1400 } 1401 1402 /* 1403 * meagasas_display_intel_branding - Display branding string 1404 * @instance: per adapter object 1405 * 1406 * Return nothing. 1407 */ 1408 static void 1409 megasas_display_intel_branding(struct megasas_instance *instance) 1410 { 1411 if (instance->pdev->subsystem_vendor != PCI_VENDOR_ID_INTEL) 1412 return; 1413 1414 switch (instance->pdev->device) { 1415 case PCI_DEVICE_ID_LSI_INVADER: 1416 switch (instance->pdev->subsystem_device) { 1417 case MEGARAID_INTEL_RS3DC080_SSDID: 1418 dev_info(&instance->pdev->dev, "scsi host %d: %s\n", 1419 instance->host->host_no, 1420 MEGARAID_INTEL_RS3DC080_BRANDING); 1421 break; 1422 case MEGARAID_INTEL_RS3DC040_SSDID: 1423 dev_info(&instance->pdev->dev, "scsi host %d: %s\n", 1424 instance->host->host_no, 1425 MEGARAID_INTEL_RS3DC040_BRANDING); 1426 break; 1427 case MEGARAID_INTEL_RS3SC008_SSDID: 1428 dev_info(&instance->pdev->dev, "scsi host %d: %s\n", 1429 instance->host->host_no, 1430 MEGARAID_INTEL_RS3SC008_BRANDING); 1431 break; 1432 case MEGARAID_INTEL_RS3MC044_SSDID: 1433 dev_info(&instance->pdev->dev, "scsi host %d: %s\n", 1434 instance->host->host_no, 1435 MEGARAID_INTEL_RS3MC044_BRANDING); 1436 break; 1437 default: 1438 break; 1439 } 1440 break; 1441 case PCI_DEVICE_ID_LSI_FURY: 1442 switch (instance->pdev->subsystem_device) { 1443 case MEGARAID_INTEL_RS3WC080_SSDID: 1444 dev_info(&instance->pdev->dev, "scsi host %d: %s\n", 1445 instance->host->host_no, 1446 MEGARAID_INTEL_RS3WC080_BRANDING); 1447 break; 1448 case MEGARAID_INTEL_RS3WC040_SSDID: 1449 dev_info(&instance->pdev->dev, "scsi host %d: %s\n", 1450 instance->host->host_no, 1451 MEGARAID_INTEL_RS3WC040_BRANDING); 1452 break; 1453 default: 1454 break; 1455 } 1456 break; 1457 case PCI_DEVICE_ID_LSI_CUTLASS_52: 1458 case PCI_DEVICE_ID_LSI_CUTLASS_53: 1459 switch (instance->pdev->subsystem_device) { 1460 case MEGARAID_INTEL_RMS3BC160_SSDID: 1461 dev_info(&instance->pdev->dev, "scsi host %d: %s\n", 1462 instance->host->host_no, 1463 MEGARAID_INTEL_RMS3BC160_BRANDING); 1464 break; 1465 default: 1466 break; 1467 } 1468 break; 1469 default: 1470 break; 1471 } 1472 } 1473 1474 /** 1475 * megasas_allocate_raid_maps - Allocate memory for RAID maps 1476 * @instance: Adapter soft state 1477 * 1478 * return: if success: return 0 1479 * failed: return -ENOMEM 1480 */ 1481 static inline int megasas_allocate_raid_maps(struct megasas_instance *instance) 1482 { 1483 struct fusion_context *fusion; 1484 int i = 0; 1485 1486 fusion = instance->ctrl_context; 1487 1488 fusion->drv_map_pages = get_order(fusion->drv_map_sz); 1489 1490 for (i = 0; i < 2; i++) { 1491 fusion->ld_map[i] = NULL; 1492 1493 fusion->ld_drv_map[i] = (void *) 1494 __get_free_pages(__GFP_ZERO | GFP_KERNEL, 1495 fusion->drv_map_pages); 1496 1497 if (!fusion->ld_drv_map[i]) { 1498 fusion->ld_drv_map[i] = vzalloc(fusion->drv_map_sz); 1499 1500 if (!fusion->ld_drv_map[i]) { 1501 dev_err(&instance->pdev->dev, 1502 "Could not allocate memory for local map" 1503 " size requested: %d\n", 1504 fusion->drv_map_sz); 1505 goto ld_drv_map_alloc_fail; 1506 } 1507 } 1508 } 1509 1510 for (i = 0; i < 2; i++) { 1511 fusion->ld_map[i] = dma_alloc_coherent(&instance->pdev->dev, 1512 fusion->max_map_sz, 1513 &fusion->ld_map_phys[i], 1514 GFP_KERNEL); 1515 if (!fusion->ld_map[i]) { 1516 dev_err(&instance->pdev->dev, 1517 "Could not allocate memory for map info %s:%d\n", 1518 __func__, __LINE__); 1519 goto ld_map_alloc_fail; 1520 } 1521 } 1522 1523 return 0; 1524 1525 ld_map_alloc_fail: 1526 for (i = 0; i < 2; i++) { 1527 if (fusion->ld_map[i]) 1528 dma_free_coherent(&instance->pdev->dev, 1529 fusion->max_map_sz, 1530 fusion->ld_map[i], 1531 fusion->ld_map_phys[i]); 1532 } 1533 1534 ld_drv_map_alloc_fail: 1535 for (i = 0; i < 2; i++) { 1536 if (fusion->ld_drv_map[i]) { 1537 if (is_vmalloc_addr(fusion->ld_drv_map[i])) 1538 vfree(fusion->ld_drv_map[i]); 1539 else 1540 free_pages((ulong)fusion->ld_drv_map[i], 1541 fusion->drv_map_pages); 1542 } 1543 } 1544 1545 return -ENOMEM; 1546 } 1547 1548 /** 1549 * megasas_configure_queue_sizes - Calculate size of request desc queue, 1550 * reply desc queue, 1551 * IO request frame queue, set can_queue. 1552 * @instance: Adapter soft state 1553 * @return: void 1554 */ 1555 static inline 1556 void megasas_configure_queue_sizes(struct megasas_instance *instance) 1557 { 1558 struct fusion_context *fusion; 1559 u16 max_cmd; 1560 1561 fusion = instance->ctrl_context; 1562 max_cmd = instance->max_fw_cmds; 1563 1564 if (instance->adapter_type >= VENTURA_SERIES) 1565 instance->max_mpt_cmds = instance->max_fw_cmds * RAID_1_PEER_CMDS; 1566 else 1567 instance->max_mpt_cmds = instance->max_fw_cmds; 1568 1569 instance->max_scsi_cmds = instance->max_fw_cmds - instance->max_mfi_cmds; 1570 instance->cur_can_queue = instance->max_scsi_cmds; 1571 instance->host->can_queue = instance->cur_can_queue; 1572 1573 fusion->reply_q_depth = 2 * ((max_cmd + 1 + 15) / 16) * 16; 1574 1575 fusion->request_alloc_sz = sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * 1576 instance->max_mpt_cmds; 1577 fusion->reply_alloc_sz = sizeof(union MPI2_REPLY_DESCRIPTORS_UNION) * 1578 (fusion->reply_q_depth); 1579 fusion->io_frames_alloc_sz = MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE + 1580 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE 1581 * (instance->max_mpt_cmds + 1)); /* Extra 1 for SMID 0 */ 1582 } 1583 1584 static int megasas_alloc_ioc_init_frame(struct megasas_instance *instance) 1585 { 1586 struct fusion_context *fusion; 1587 struct megasas_cmd *cmd; 1588 1589 fusion = instance->ctrl_context; 1590 1591 cmd = kzalloc(sizeof(struct megasas_cmd), GFP_KERNEL); 1592 1593 if (!cmd) { 1594 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n", 1595 __func__, __LINE__); 1596 return -ENOMEM; 1597 } 1598 1599 cmd->frame = dma_alloc_coherent(&instance->pdev->dev, 1600 IOC_INIT_FRAME_SIZE, 1601 &cmd->frame_phys_addr, GFP_KERNEL); 1602 1603 if (!cmd->frame) { 1604 dev_err(&instance->pdev->dev, "Failed from func: %s line: %d\n", 1605 __func__, __LINE__); 1606 kfree(cmd); 1607 return -ENOMEM; 1608 } 1609 1610 fusion->ioc_init_cmd = cmd; 1611 return 0; 1612 } 1613 1614 /** 1615 * megasas_free_ioc_init_cmd - Free IOC INIT command frame 1616 * @instance: Adapter soft state 1617 */ 1618 static inline void megasas_free_ioc_init_cmd(struct megasas_instance *instance) 1619 { 1620 struct fusion_context *fusion; 1621 1622 fusion = instance->ctrl_context; 1623 1624 if (fusion->ioc_init_cmd && fusion->ioc_init_cmd->frame) 1625 dma_free_coherent(&instance->pdev->dev, 1626 IOC_INIT_FRAME_SIZE, 1627 fusion->ioc_init_cmd->frame, 1628 fusion->ioc_init_cmd->frame_phys_addr); 1629 1630 kfree(fusion->ioc_init_cmd); 1631 } 1632 1633 /** 1634 * megasas_init_adapter_fusion - Initializes the FW 1635 * @instance: Adapter soft state 1636 * 1637 * This is the main function for initializing firmware. 1638 */ 1639 u32 1640 megasas_init_adapter_fusion(struct megasas_instance *instance) 1641 { 1642 struct megasas_register_set __iomem *reg_set; 1643 struct fusion_context *fusion; 1644 u32 scratch_pad_1; 1645 int i = 0, count; 1646 1647 fusion = instance->ctrl_context; 1648 1649 reg_set = instance->reg_set; 1650 1651 megasas_fusion_update_can_queue(instance, PROBE_CONTEXT); 1652 1653 /* 1654 * Only Driver's internal DCMDs and IOCTL DCMDs needs to have MFI frames 1655 */ 1656 instance->max_mfi_cmds = 1657 MEGASAS_FUSION_INTERNAL_CMDS + MEGASAS_FUSION_IOCTL_CMDS; 1658 1659 megasas_configure_queue_sizes(instance); 1660 1661 scratch_pad_1 = megasas_readl(instance, 1662 &instance->reg_set->outbound_scratch_pad_1); 1663 /* If scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK is set, 1664 * Firmware support extended IO chain frame which is 4 times more than 1665 * legacy Firmware. 1666 * Legacy Firmware - Frame size is (8 * 128) = 1K 1667 * 1M IO Firmware - Frame size is (8 * 128 * 4) = 4K 1668 */ 1669 if (scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_UNITS_MASK) 1670 instance->max_chain_frame_sz = 1671 ((scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_MASK) >> 1672 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_1MB_IO; 1673 else 1674 instance->max_chain_frame_sz = 1675 ((scratch_pad_1 & MEGASAS_MAX_CHAIN_SIZE_MASK) >> 1676 MEGASAS_MAX_CHAIN_SHIFT) * MEGASAS_256K_IO; 1677 1678 if (instance->max_chain_frame_sz < MEGASAS_CHAIN_FRAME_SZ_MIN) { 1679 dev_warn(&instance->pdev->dev, "frame size %d invalid, fall back to legacy max frame size %d\n", 1680 instance->max_chain_frame_sz, 1681 MEGASAS_CHAIN_FRAME_SZ_MIN); 1682 instance->max_chain_frame_sz = MEGASAS_CHAIN_FRAME_SZ_MIN; 1683 } 1684 1685 fusion->max_sge_in_main_msg = 1686 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE 1687 - offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL))/16; 1688 1689 fusion->max_sge_in_chain = 1690 instance->max_chain_frame_sz 1691 / sizeof(union MPI2_SGE_IO_UNION); 1692 1693 instance->max_num_sge = 1694 rounddown_pow_of_two(fusion->max_sge_in_main_msg 1695 + fusion->max_sge_in_chain - 2); 1696 1697 /* Used for pass thru MFI frame (DCMD) */ 1698 fusion->chain_offset_mfi_pthru = 1699 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL)/16; 1700 1701 fusion->chain_offset_io_request = 1702 (MEGA_MPI2_RAID_DEFAULT_IO_FRAME_SIZE - 1703 sizeof(union MPI2_SGE_IO_UNION))/16; 1704 1705 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1; 1706 for (i = 0 ; i < count; i++) 1707 fusion->last_reply_idx[i] = 0; 1708 1709 /* 1710 * For fusion adapters, 3 commands for IOCTL and 8 commands 1711 * for driver's internal DCMDs. 1712 */ 1713 instance->max_scsi_cmds = instance->max_fw_cmds - 1714 (MEGASAS_FUSION_INTERNAL_CMDS + 1715 MEGASAS_FUSION_IOCTL_CMDS); 1716 sema_init(&instance->ioctl_sem, MEGASAS_FUSION_IOCTL_CMDS); 1717 1718 if (megasas_alloc_ioc_init_frame(instance)) 1719 return 1; 1720 1721 /* 1722 * Allocate memory for descriptors 1723 * Create a pool of commands 1724 */ 1725 if (megasas_alloc_cmds(instance)) 1726 goto fail_alloc_mfi_cmds; 1727 if (megasas_alloc_cmds_fusion(instance)) 1728 goto fail_alloc_cmds; 1729 1730 if (megasas_ioc_init_fusion(instance)) 1731 goto fail_ioc_init; 1732 1733 megasas_display_intel_branding(instance); 1734 if (megasas_get_ctrl_info(instance)) { 1735 dev_err(&instance->pdev->dev, 1736 "Could not get controller info. Fail from %s %d\n", 1737 __func__, __LINE__); 1738 goto fail_ioc_init; 1739 } 1740 1741 instance->flag_ieee = 1; 1742 instance->r1_ldio_hint_default = MR_R1_LDIO_PIGGYBACK_DEFAULT; 1743 fusion->fast_path_io = 0; 1744 1745 if (megasas_allocate_raid_maps(instance)) 1746 goto fail_ioc_init; 1747 1748 if (!megasas_get_map_info(instance)) 1749 megasas_sync_map_info(instance); 1750 1751 return 0; 1752 1753 fail_ioc_init: 1754 megasas_free_cmds_fusion(instance); 1755 fail_alloc_cmds: 1756 megasas_free_cmds(instance); 1757 fail_alloc_mfi_cmds: 1758 megasas_free_ioc_init_cmd(instance); 1759 return 1; 1760 } 1761 1762 /** 1763 * megasas_fault_detect_work - Worker function of 1764 * FW fault handling workqueue. 1765 */ 1766 static void 1767 megasas_fault_detect_work(struct work_struct *work) 1768 { 1769 struct megasas_instance *instance = 1770 container_of(work, struct megasas_instance, 1771 fw_fault_work.work); 1772 u32 fw_state, dma_state, status; 1773 1774 /* Check the fw state */ 1775 fw_state = instance->instancet->read_fw_status_reg(instance) & 1776 MFI_STATE_MASK; 1777 1778 if (fw_state == MFI_STATE_FAULT) { 1779 dma_state = instance->instancet->read_fw_status_reg(instance) & 1780 MFI_STATE_DMADONE; 1781 /* Start collecting crash, if DMA bit is done */ 1782 if (instance->crash_dump_drv_support && 1783 instance->crash_dump_app_support && dma_state) { 1784 megasas_fusion_crash_dump(instance); 1785 } else { 1786 if (instance->unload == 0) { 1787 status = megasas_reset_fusion(instance->host, 0); 1788 if (status != SUCCESS) { 1789 dev_err(&instance->pdev->dev, 1790 "Failed from %s %d, do not re-arm timer\n", 1791 __func__, __LINE__); 1792 return; 1793 } 1794 } 1795 } 1796 } 1797 1798 if (instance->fw_fault_work_q) 1799 queue_delayed_work(instance->fw_fault_work_q, 1800 &instance->fw_fault_work, 1801 msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL)); 1802 } 1803 1804 int 1805 megasas_fusion_start_watchdog(struct megasas_instance *instance) 1806 { 1807 /* Check if the Fault WQ is already started */ 1808 if (instance->fw_fault_work_q) 1809 return SUCCESS; 1810 1811 INIT_DELAYED_WORK(&instance->fw_fault_work, megasas_fault_detect_work); 1812 1813 snprintf(instance->fault_handler_work_q_name, 1814 sizeof(instance->fault_handler_work_q_name), 1815 "poll_megasas%d_status", instance->host->host_no); 1816 1817 instance->fw_fault_work_q = 1818 create_singlethread_workqueue(instance->fault_handler_work_q_name); 1819 if (!instance->fw_fault_work_q) { 1820 dev_err(&instance->pdev->dev, "Failed from %s %d\n", 1821 __func__, __LINE__); 1822 return FAILED; 1823 } 1824 1825 queue_delayed_work(instance->fw_fault_work_q, 1826 &instance->fw_fault_work, 1827 msecs_to_jiffies(MEGASAS_WATCHDOG_THREAD_INTERVAL)); 1828 1829 return SUCCESS; 1830 } 1831 1832 void 1833 megasas_fusion_stop_watchdog(struct megasas_instance *instance) 1834 { 1835 struct workqueue_struct *wq; 1836 1837 if (instance->fw_fault_work_q) { 1838 wq = instance->fw_fault_work_q; 1839 instance->fw_fault_work_q = NULL; 1840 if (!cancel_delayed_work_sync(&instance->fw_fault_work)) 1841 flush_workqueue(wq); 1842 destroy_workqueue(wq); 1843 } 1844 } 1845 1846 /** 1847 * map_cmd_status - Maps FW cmd status to OS cmd status 1848 * @cmd : Pointer to cmd 1849 * @status : status of cmd returned by FW 1850 * @ext_status : ext status of cmd returned by FW 1851 */ 1852 1853 void 1854 map_cmd_status(struct fusion_context *fusion, 1855 struct scsi_cmnd *scmd, u8 status, u8 ext_status, 1856 u32 data_length, u8 *sense) 1857 { 1858 u8 cmd_type; 1859 int resid; 1860 1861 cmd_type = megasas_cmd_type(scmd); 1862 switch (status) { 1863 1864 case MFI_STAT_OK: 1865 scmd->result = DID_OK << 16; 1866 break; 1867 1868 case MFI_STAT_SCSI_IO_FAILED: 1869 case MFI_STAT_LD_INIT_IN_PROGRESS: 1870 scmd->result = (DID_ERROR << 16) | ext_status; 1871 break; 1872 1873 case MFI_STAT_SCSI_DONE_WITH_ERROR: 1874 1875 scmd->result = (DID_OK << 16) | ext_status; 1876 if (ext_status == SAM_STAT_CHECK_CONDITION) { 1877 memset(scmd->sense_buffer, 0, 1878 SCSI_SENSE_BUFFERSIZE); 1879 memcpy(scmd->sense_buffer, sense, 1880 SCSI_SENSE_BUFFERSIZE); 1881 scmd->result |= DRIVER_SENSE << 24; 1882 } 1883 1884 /* 1885 * If the IO request is partially completed, then MR FW will 1886 * update "io_request->DataLength" field with actual number of 1887 * bytes transferred.Driver will set residual bytes count in 1888 * SCSI command structure. 1889 */ 1890 resid = (scsi_bufflen(scmd) - data_length); 1891 scsi_set_resid(scmd, resid); 1892 1893 if (resid && 1894 ((cmd_type == READ_WRITE_LDIO) || 1895 (cmd_type == READ_WRITE_SYSPDIO))) 1896 scmd_printk(KERN_INFO, scmd, "BRCM Debug mfi stat 0x%x, data len" 1897 " requested/completed 0x%x/0x%x\n", 1898 status, scsi_bufflen(scmd), data_length); 1899 break; 1900 1901 case MFI_STAT_LD_OFFLINE: 1902 case MFI_STAT_DEVICE_NOT_FOUND: 1903 scmd->result = DID_BAD_TARGET << 16; 1904 break; 1905 case MFI_STAT_CONFIG_SEQ_MISMATCH: 1906 scmd->result = DID_IMM_RETRY << 16; 1907 break; 1908 default: 1909 scmd->result = DID_ERROR << 16; 1910 break; 1911 } 1912 } 1913 1914 /** 1915 * megasas_is_prp_possible - 1916 * Checks if native NVMe PRPs can be built for the IO 1917 * 1918 * @instance: Adapter soft state 1919 * @scmd: SCSI command from the mid-layer 1920 * @sge_count: scatter gather element count. 1921 * 1922 * Returns: true: PRPs can be built 1923 * false: IEEE SGLs needs to be built 1924 */ 1925 static bool 1926 megasas_is_prp_possible(struct megasas_instance *instance, 1927 struct scsi_cmnd *scmd, int sge_count) 1928 { 1929 struct fusion_context *fusion; 1930 int i; 1931 u32 data_length = 0; 1932 struct scatterlist *sg_scmd; 1933 bool build_prp = false; 1934 u32 mr_nvme_pg_size; 1935 1936 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size, 1937 MR_DEFAULT_NVME_PAGE_SIZE); 1938 fusion = instance->ctrl_context; 1939 data_length = scsi_bufflen(scmd); 1940 sg_scmd = scsi_sglist(scmd); 1941 1942 /* 1943 * NVMe uses one PRP for each page (or part of a page) 1944 * look at the data length - if 4 pages or less then IEEE is OK 1945 * if > 5 pages then we need to build a native SGL 1946 * if > 4 and <= 5 pages, then check physical address of 1st SG entry 1947 * if this first size in the page is >= the residual beyond 4 pages 1948 * then use IEEE, otherwise use native SGL 1949 */ 1950 1951 if (data_length > (mr_nvme_pg_size * 5)) { 1952 build_prp = true; 1953 } else if ((data_length > (mr_nvme_pg_size * 4)) && 1954 (data_length <= (mr_nvme_pg_size * 5))) { 1955 /* check if 1st SG entry size is < residual beyond 4 pages */ 1956 if (sg_dma_len(sg_scmd) < (data_length - (mr_nvme_pg_size * 4))) 1957 build_prp = true; 1958 } 1959 1960 /* 1961 * Below code detects gaps/holes in IO data buffers. 1962 * What does holes/gaps mean? 1963 * Any SGE except first one in a SGL starts at non NVME page size 1964 * aligned address OR Any SGE except last one in a SGL ends at 1965 * non NVME page size boundary. 1966 * 1967 * Driver has already informed block layer by setting boundary rules for 1968 * bio merging done at NVME page size boundary calling kernel API 1969 * blk_queue_virt_boundary inside slave_config. 1970 * Still there is possibility of IO coming with holes to driver because of 1971 * IO merging done by IO scheduler. 1972 * 1973 * With SCSI BLK MQ enabled, there will be no IO with holes as there is no 1974 * IO scheduling so no IO merging. 1975 * 1976 * With SCSI BLK MQ disabled, IO scheduler may attempt to merge IOs and 1977 * then sending IOs with holes. 1978 * 1979 * Though driver can request block layer to disable IO merging by calling- 1980 * blk_queue_flag_set(QUEUE_FLAG_NOMERGES, sdev->request_queue) but 1981 * user may tune sysfs parameter- nomerges again to 0 or 1. 1982 * 1983 * If in future IO scheduling is enabled with SCSI BLK MQ, 1984 * this algorithm to detect holes will be required in driver 1985 * for SCSI BLK MQ enabled case as well. 1986 * 1987 * 1988 */ 1989 scsi_for_each_sg(scmd, sg_scmd, sge_count, i) { 1990 if ((i != 0) && (i != (sge_count - 1))) { 1991 if (mega_mod64(sg_dma_len(sg_scmd), mr_nvme_pg_size) || 1992 mega_mod64(sg_dma_address(sg_scmd), 1993 mr_nvme_pg_size)) { 1994 build_prp = false; 1995 atomic_inc(&instance->sge_holes_type1); 1996 break; 1997 } 1998 } 1999 2000 if ((sge_count > 1) && (i == 0)) { 2001 if ((mega_mod64((sg_dma_address(sg_scmd) + 2002 sg_dma_len(sg_scmd)), 2003 mr_nvme_pg_size))) { 2004 build_prp = false; 2005 atomic_inc(&instance->sge_holes_type2); 2006 break; 2007 } 2008 } 2009 2010 if ((sge_count > 1) && (i == (sge_count - 1))) { 2011 if (mega_mod64(sg_dma_address(sg_scmd), 2012 mr_nvme_pg_size)) { 2013 build_prp = false; 2014 atomic_inc(&instance->sge_holes_type3); 2015 break; 2016 } 2017 } 2018 } 2019 2020 return build_prp; 2021 } 2022 2023 /** 2024 * megasas_make_prp_nvme - 2025 * Prepare PRPs(Physical Region Page)- SGLs specific to NVMe drives only 2026 * 2027 * @instance: Adapter soft state 2028 * @scmd: SCSI command from the mid-layer 2029 * @sgl_ptr: SGL to be filled in 2030 * @cmd: Fusion command frame 2031 * @sge_count: scatter gather element count. 2032 * 2033 * Returns: true: PRPs are built 2034 * false: IEEE SGLs needs to be built 2035 */ 2036 static bool 2037 megasas_make_prp_nvme(struct megasas_instance *instance, struct scsi_cmnd *scmd, 2038 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr, 2039 struct megasas_cmd_fusion *cmd, int sge_count) 2040 { 2041 int sge_len, offset, num_prp_in_chain = 0; 2042 struct MPI25_IEEE_SGE_CHAIN64 *main_chain_element, *ptr_first_sgl; 2043 u64 *ptr_sgl; 2044 dma_addr_t ptr_sgl_phys; 2045 u64 sge_addr; 2046 u32 page_mask, page_mask_result; 2047 struct scatterlist *sg_scmd; 2048 u32 first_prp_len; 2049 bool build_prp = false; 2050 int data_len = scsi_bufflen(scmd); 2051 struct fusion_context *fusion; 2052 u32 mr_nvme_pg_size = max_t(u32, instance->nvme_page_size, 2053 MR_DEFAULT_NVME_PAGE_SIZE); 2054 2055 fusion = instance->ctrl_context; 2056 2057 build_prp = megasas_is_prp_possible(instance, scmd, sge_count); 2058 2059 if (!build_prp) 2060 return false; 2061 2062 /* 2063 * Nvme has a very convoluted prp format. One prp is required 2064 * for each page or partial page. Driver need to split up OS sg_list 2065 * entries if it is longer than one page or cross a page 2066 * boundary. Driver also have to insert a PRP list pointer entry as 2067 * the last entry in each physical page of the PRP list. 2068 * 2069 * NOTE: The first PRP "entry" is actually placed in the first 2070 * SGL entry in the main message as IEEE 64 format. The 2nd 2071 * entry in the main message is the chain element, and the rest 2072 * of the PRP entries are built in the contiguous pcie buffer. 2073 */ 2074 page_mask = mr_nvme_pg_size - 1; 2075 ptr_sgl = (u64 *)cmd->sg_frame; 2076 ptr_sgl_phys = cmd->sg_frame_phys_addr; 2077 memset(ptr_sgl, 0, instance->max_chain_frame_sz); 2078 2079 /* Build chain frame element which holds all prps except first*/ 2080 main_chain_element = (struct MPI25_IEEE_SGE_CHAIN64 *) 2081 ((u8 *)sgl_ptr + sizeof(struct MPI25_IEEE_SGE_CHAIN64)); 2082 2083 main_chain_element->Address = cpu_to_le64(ptr_sgl_phys); 2084 main_chain_element->NextChainOffset = 0; 2085 main_chain_element->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT | 2086 IEEE_SGE_FLAGS_SYSTEM_ADDR | 2087 MPI26_IEEE_SGE_FLAGS_NSF_NVME_PRP; 2088 2089 /* Build first prp, sge need not to be page aligned*/ 2090 ptr_first_sgl = sgl_ptr; 2091 sg_scmd = scsi_sglist(scmd); 2092 sge_addr = sg_dma_address(sg_scmd); 2093 sge_len = sg_dma_len(sg_scmd); 2094 2095 offset = (u32)(sge_addr & page_mask); 2096 first_prp_len = mr_nvme_pg_size - offset; 2097 2098 ptr_first_sgl->Address = cpu_to_le64(sge_addr); 2099 ptr_first_sgl->Length = cpu_to_le32(first_prp_len); 2100 2101 data_len -= first_prp_len; 2102 2103 if (sge_len > first_prp_len) { 2104 sge_addr += first_prp_len; 2105 sge_len -= first_prp_len; 2106 } else if (sge_len == first_prp_len) { 2107 sg_scmd = sg_next(sg_scmd); 2108 sge_addr = sg_dma_address(sg_scmd); 2109 sge_len = sg_dma_len(sg_scmd); 2110 } 2111 2112 for (;;) { 2113 offset = (u32)(sge_addr & page_mask); 2114 2115 /* Put PRP pointer due to page boundary*/ 2116 page_mask_result = (uintptr_t)(ptr_sgl + 1) & page_mask; 2117 if (unlikely(!page_mask_result)) { 2118 scmd_printk(KERN_NOTICE, 2119 scmd, "page boundary ptr_sgl: 0x%p\n", 2120 ptr_sgl); 2121 ptr_sgl_phys += 8; 2122 *ptr_sgl = cpu_to_le64(ptr_sgl_phys); 2123 ptr_sgl++; 2124 num_prp_in_chain++; 2125 } 2126 2127 *ptr_sgl = cpu_to_le64(sge_addr); 2128 ptr_sgl++; 2129 ptr_sgl_phys += 8; 2130 num_prp_in_chain++; 2131 2132 sge_addr += mr_nvme_pg_size; 2133 sge_len -= mr_nvme_pg_size; 2134 data_len -= mr_nvme_pg_size; 2135 2136 if (data_len <= 0) 2137 break; 2138 2139 if (sge_len > 0) 2140 continue; 2141 2142 sg_scmd = sg_next(sg_scmd); 2143 sge_addr = sg_dma_address(sg_scmd); 2144 sge_len = sg_dma_len(sg_scmd); 2145 } 2146 2147 main_chain_element->Length = 2148 cpu_to_le32(num_prp_in_chain * sizeof(u64)); 2149 2150 atomic_inc(&instance->prp_sgl); 2151 return build_prp; 2152 } 2153 2154 /** 2155 * megasas_make_sgl_fusion - Prepares 32-bit SGL 2156 * @instance: Adapter soft state 2157 * @scp: SCSI command from the mid-layer 2158 * @sgl_ptr: SGL to be filled in 2159 * @cmd: cmd we are working on 2160 * @sge_count sge count 2161 * 2162 */ 2163 static void 2164 megasas_make_sgl_fusion(struct megasas_instance *instance, 2165 struct scsi_cmnd *scp, 2166 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr, 2167 struct megasas_cmd_fusion *cmd, int sge_count) 2168 { 2169 int i, sg_processed; 2170 struct scatterlist *os_sgl; 2171 struct fusion_context *fusion; 2172 2173 fusion = instance->ctrl_context; 2174 2175 if (instance->adapter_type >= INVADER_SERIES) { 2176 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = sgl_ptr; 2177 sgl_ptr_end += fusion->max_sge_in_main_msg - 1; 2178 sgl_ptr_end->Flags = 0; 2179 } 2180 2181 scsi_for_each_sg(scp, os_sgl, sge_count, i) { 2182 sgl_ptr->Length = cpu_to_le32(sg_dma_len(os_sgl)); 2183 sgl_ptr->Address = cpu_to_le64(sg_dma_address(os_sgl)); 2184 sgl_ptr->Flags = 0; 2185 if (instance->adapter_type >= INVADER_SERIES) 2186 if (i == sge_count - 1) 2187 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST; 2188 sgl_ptr++; 2189 sg_processed = i + 1; 2190 2191 if ((sg_processed == (fusion->max_sge_in_main_msg - 1)) && 2192 (sge_count > fusion->max_sge_in_main_msg)) { 2193 2194 struct MPI25_IEEE_SGE_CHAIN64 *sg_chain; 2195 if (instance->adapter_type >= INVADER_SERIES) { 2196 if ((le16_to_cpu(cmd->io_request->IoFlags) & 2197 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) != 2198 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) 2199 cmd->io_request->ChainOffset = 2200 fusion-> 2201 chain_offset_io_request; 2202 else 2203 cmd->io_request->ChainOffset = 0; 2204 } else 2205 cmd->io_request->ChainOffset = 2206 fusion->chain_offset_io_request; 2207 2208 sg_chain = sgl_ptr; 2209 /* Prepare chain element */ 2210 sg_chain->NextChainOffset = 0; 2211 if (instance->adapter_type >= INVADER_SERIES) 2212 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT; 2213 else 2214 sg_chain->Flags = 2215 (IEEE_SGE_FLAGS_CHAIN_ELEMENT | 2216 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR); 2217 sg_chain->Length = cpu_to_le32((sizeof(union MPI2_SGE_IO_UNION) * (sge_count - sg_processed))); 2218 sg_chain->Address = cpu_to_le64(cmd->sg_frame_phys_addr); 2219 2220 sgl_ptr = 2221 (struct MPI25_IEEE_SGE_CHAIN64 *)cmd->sg_frame; 2222 memset(sgl_ptr, 0, instance->max_chain_frame_sz); 2223 } 2224 } 2225 atomic_inc(&instance->ieee_sgl); 2226 } 2227 2228 /** 2229 * megasas_make_sgl - Build Scatter Gather List(SGLs) 2230 * @scp: SCSI command pointer 2231 * @instance: Soft instance of controller 2232 * @cmd: Fusion command pointer 2233 * 2234 * This function will build sgls based on device type. 2235 * For nvme drives, there is different way of building sgls in nvme native 2236 * format- PRPs(Physical Region Page). 2237 * 2238 * Returns the number of sg lists actually used, zero if the sg lists 2239 * is NULL, or -ENOMEM if the mapping failed 2240 */ 2241 static 2242 int megasas_make_sgl(struct megasas_instance *instance, struct scsi_cmnd *scp, 2243 struct megasas_cmd_fusion *cmd) 2244 { 2245 int sge_count; 2246 bool build_prp = false; 2247 struct MPI25_IEEE_SGE_CHAIN64 *sgl_chain64; 2248 2249 sge_count = scsi_dma_map(scp); 2250 2251 if ((sge_count > instance->max_num_sge) || (sge_count <= 0)) 2252 return sge_count; 2253 2254 sgl_chain64 = (struct MPI25_IEEE_SGE_CHAIN64 *)&cmd->io_request->SGL; 2255 if ((le16_to_cpu(cmd->io_request->IoFlags) & 2256 MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH) && 2257 (cmd->pd_interface == NVME_PD)) 2258 build_prp = megasas_make_prp_nvme(instance, scp, sgl_chain64, 2259 cmd, sge_count); 2260 2261 if (!build_prp) 2262 megasas_make_sgl_fusion(instance, scp, sgl_chain64, 2263 cmd, sge_count); 2264 2265 return sge_count; 2266 } 2267 2268 /** 2269 * megasas_set_pd_lba - Sets PD LBA 2270 * @cdb: CDB 2271 * @cdb_len: cdb length 2272 * @start_blk: Start block of IO 2273 * 2274 * Used to set the PD LBA in CDB for FP IOs 2275 */ 2276 void 2277 megasas_set_pd_lba(struct MPI2_RAID_SCSI_IO_REQUEST *io_request, u8 cdb_len, 2278 struct IO_REQUEST_INFO *io_info, struct scsi_cmnd *scp, 2279 struct MR_DRV_RAID_MAP_ALL *local_map_ptr, u32 ref_tag) 2280 { 2281 struct MR_LD_RAID *raid; 2282 u16 ld; 2283 u64 start_blk = io_info->pdBlock; 2284 u8 *cdb = io_request->CDB.CDB32; 2285 u32 num_blocks = io_info->numBlocks; 2286 u8 opcode = 0, flagvals = 0, groupnum = 0, control = 0; 2287 2288 /* Check if T10 PI (DIF) is enabled for this LD */ 2289 ld = MR_TargetIdToLdGet(io_info->ldTgtId, local_map_ptr); 2290 raid = MR_LdRaidGet(ld, local_map_ptr); 2291 if (raid->capability.ldPiMode == MR_PROT_INFO_TYPE_CONTROLLER) { 2292 memset(cdb, 0, sizeof(io_request->CDB.CDB32)); 2293 cdb[0] = MEGASAS_SCSI_VARIABLE_LENGTH_CMD; 2294 cdb[7] = MEGASAS_SCSI_ADDL_CDB_LEN; 2295 2296 if (scp->sc_data_direction == DMA_FROM_DEVICE) 2297 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_READ32; 2298 else 2299 cdb[9] = MEGASAS_SCSI_SERVICE_ACTION_WRITE32; 2300 cdb[10] = MEGASAS_RD_WR_PROTECT_CHECK_ALL; 2301 2302 /* LBA */ 2303 cdb[12] = (u8)((start_blk >> 56) & 0xff); 2304 cdb[13] = (u8)((start_blk >> 48) & 0xff); 2305 cdb[14] = (u8)((start_blk >> 40) & 0xff); 2306 cdb[15] = (u8)((start_blk >> 32) & 0xff); 2307 cdb[16] = (u8)((start_blk >> 24) & 0xff); 2308 cdb[17] = (u8)((start_blk >> 16) & 0xff); 2309 cdb[18] = (u8)((start_blk >> 8) & 0xff); 2310 cdb[19] = (u8)(start_blk & 0xff); 2311 2312 /* Logical block reference tag */ 2313 io_request->CDB.EEDP32.PrimaryReferenceTag = 2314 cpu_to_be32(ref_tag); 2315 io_request->CDB.EEDP32.PrimaryApplicationTagMask = cpu_to_be16(0xffff); 2316 io_request->IoFlags = cpu_to_le16(32); /* Specify 32-byte cdb */ 2317 2318 /* Transfer length */ 2319 cdb[28] = (u8)((num_blocks >> 24) & 0xff); 2320 cdb[29] = (u8)((num_blocks >> 16) & 0xff); 2321 cdb[30] = (u8)((num_blocks >> 8) & 0xff); 2322 cdb[31] = (u8)(num_blocks & 0xff); 2323 2324 /* set SCSI IO EEDPFlags */ 2325 if (scp->sc_data_direction == DMA_FROM_DEVICE) { 2326 io_request->EEDPFlags = cpu_to_le16( 2327 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG | 2328 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG | 2329 MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP | 2330 MPI2_SCSIIO_EEDPFLAGS_CHECK_APPTAG | 2331 MPI25_SCSIIO_EEDPFLAGS_DO_NOT_DISABLE_MODE | 2332 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD); 2333 } else { 2334 io_request->EEDPFlags = cpu_to_le16( 2335 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG | 2336 MPI2_SCSIIO_EEDPFLAGS_INSERT_OP); 2337 } 2338 io_request->Control |= cpu_to_le32((0x4 << 26)); 2339 io_request->EEDPBlockSize = cpu_to_le32(scp->device->sector_size); 2340 } else { 2341 /* Some drives don't support 16/12 byte CDB's, convert to 10 */ 2342 if (((cdb_len == 12) || (cdb_len == 16)) && 2343 (start_blk <= 0xffffffff)) { 2344 if (cdb_len == 16) { 2345 opcode = cdb[0] == READ_16 ? READ_10 : WRITE_10; 2346 flagvals = cdb[1]; 2347 groupnum = cdb[14]; 2348 control = cdb[15]; 2349 } else { 2350 opcode = cdb[0] == READ_12 ? READ_10 : WRITE_10; 2351 flagvals = cdb[1]; 2352 groupnum = cdb[10]; 2353 control = cdb[11]; 2354 } 2355 2356 memset(cdb, 0, sizeof(io_request->CDB.CDB32)); 2357 2358 cdb[0] = opcode; 2359 cdb[1] = flagvals; 2360 cdb[6] = groupnum; 2361 cdb[9] = control; 2362 2363 /* Transfer length */ 2364 cdb[8] = (u8)(num_blocks & 0xff); 2365 cdb[7] = (u8)((num_blocks >> 8) & 0xff); 2366 2367 io_request->IoFlags = cpu_to_le16(10); /* Specify 10-byte cdb */ 2368 cdb_len = 10; 2369 } else if ((cdb_len < 16) && (start_blk > 0xffffffff)) { 2370 /* Convert to 16 byte CDB for large LBA's */ 2371 switch (cdb_len) { 2372 case 6: 2373 opcode = cdb[0] == READ_6 ? READ_16 : WRITE_16; 2374 control = cdb[5]; 2375 break; 2376 case 10: 2377 opcode = 2378 cdb[0] == READ_10 ? READ_16 : WRITE_16; 2379 flagvals = cdb[1]; 2380 groupnum = cdb[6]; 2381 control = cdb[9]; 2382 break; 2383 case 12: 2384 opcode = 2385 cdb[0] == READ_12 ? READ_16 : WRITE_16; 2386 flagvals = cdb[1]; 2387 groupnum = cdb[10]; 2388 control = cdb[11]; 2389 break; 2390 } 2391 2392 memset(cdb, 0, sizeof(io_request->CDB.CDB32)); 2393 2394 cdb[0] = opcode; 2395 cdb[1] = flagvals; 2396 cdb[14] = groupnum; 2397 cdb[15] = control; 2398 2399 /* Transfer length */ 2400 cdb[13] = (u8)(num_blocks & 0xff); 2401 cdb[12] = (u8)((num_blocks >> 8) & 0xff); 2402 cdb[11] = (u8)((num_blocks >> 16) & 0xff); 2403 cdb[10] = (u8)((num_blocks >> 24) & 0xff); 2404 2405 io_request->IoFlags = cpu_to_le16(16); /* Specify 16-byte cdb */ 2406 cdb_len = 16; 2407 } 2408 2409 /* Normal case, just load LBA here */ 2410 switch (cdb_len) { 2411 case 6: 2412 { 2413 u8 val = cdb[1] & 0xE0; 2414 cdb[3] = (u8)(start_blk & 0xff); 2415 cdb[2] = (u8)((start_blk >> 8) & 0xff); 2416 cdb[1] = val | ((u8)(start_blk >> 16) & 0x1f); 2417 break; 2418 } 2419 case 10: 2420 cdb[5] = (u8)(start_blk & 0xff); 2421 cdb[4] = (u8)((start_blk >> 8) & 0xff); 2422 cdb[3] = (u8)((start_blk >> 16) & 0xff); 2423 cdb[2] = (u8)((start_blk >> 24) & 0xff); 2424 break; 2425 case 12: 2426 cdb[5] = (u8)(start_blk & 0xff); 2427 cdb[4] = (u8)((start_blk >> 8) & 0xff); 2428 cdb[3] = (u8)((start_blk >> 16) & 0xff); 2429 cdb[2] = (u8)((start_blk >> 24) & 0xff); 2430 break; 2431 case 16: 2432 cdb[9] = (u8)(start_blk & 0xff); 2433 cdb[8] = (u8)((start_blk >> 8) & 0xff); 2434 cdb[7] = (u8)((start_blk >> 16) & 0xff); 2435 cdb[6] = (u8)((start_blk >> 24) & 0xff); 2436 cdb[5] = (u8)((start_blk >> 32) & 0xff); 2437 cdb[4] = (u8)((start_blk >> 40) & 0xff); 2438 cdb[3] = (u8)((start_blk >> 48) & 0xff); 2439 cdb[2] = (u8)((start_blk >> 56) & 0xff); 2440 break; 2441 } 2442 } 2443 } 2444 2445 /** 2446 * megasas_stream_detect - stream detection on read and and write IOs 2447 * @instance: Adapter soft state 2448 * @cmd: Command to be prepared 2449 * @io_info: IO Request info 2450 * 2451 */ 2452 2453 /** stream detection on read and and write IOs */ 2454 static void megasas_stream_detect(struct megasas_instance *instance, 2455 struct megasas_cmd_fusion *cmd, 2456 struct IO_REQUEST_INFO *io_info) 2457 { 2458 struct fusion_context *fusion = instance->ctrl_context; 2459 u32 device_id = io_info->ldTgtId; 2460 struct LD_STREAM_DETECT *current_ld_sd 2461 = fusion->stream_detect_by_ld[device_id]; 2462 u32 *track_stream = ¤t_ld_sd->mru_bit_map, stream_num; 2463 u32 shifted_values, unshifted_values; 2464 u32 index_value_mask, shifted_values_mask; 2465 int i; 2466 bool is_read_ahead = false; 2467 struct STREAM_DETECT *current_sd; 2468 /* find possible stream */ 2469 for (i = 0; i < MAX_STREAMS_TRACKED; ++i) { 2470 stream_num = (*track_stream >> 2471 (i * BITS_PER_INDEX_STREAM)) & 2472 STREAM_MASK; 2473 current_sd = ¤t_ld_sd->stream_track[stream_num]; 2474 /* if we found a stream, update the raid 2475 * context and also update the mruBitMap 2476 */ 2477 /* boundary condition */ 2478 if ((current_sd->next_seq_lba) && 2479 (io_info->ldStartBlock >= current_sd->next_seq_lba) && 2480 (io_info->ldStartBlock <= (current_sd->next_seq_lba + 32)) && 2481 (current_sd->is_read == io_info->isRead)) { 2482 2483 if ((io_info->ldStartBlock != current_sd->next_seq_lba) && 2484 ((!io_info->isRead) || (!is_read_ahead))) 2485 /* 2486 * Once the API availible we need to change this. 2487 * At this point we are not allowing any gap 2488 */ 2489 continue; 2490 2491 SET_STREAM_DETECTED(cmd->io_request->RaidContext.raid_context_g35); 2492 current_sd->next_seq_lba = 2493 io_info->ldStartBlock + io_info->numBlocks; 2494 /* 2495 * update the mruBitMap LRU 2496 */ 2497 shifted_values_mask = 2498 (1 << i * BITS_PER_INDEX_STREAM) - 1; 2499 shifted_values = ((*track_stream & shifted_values_mask) 2500 << BITS_PER_INDEX_STREAM); 2501 index_value_mask = 2502 STREAM_MASK << i * BITS_PER_INDEX_STREAM; 2503 unshifted_values = 2504 *track_stream & ~(shifted_values_mask | 2505 index_value_mask); 2506 *track_stream = 2507 unshifted_values | shifted_values | stream_num; 2508 return; 2509 } 2510 } 2511 /* 2512 * if we did not find any stream, create a new one 2513 * from the least recently used 2514 */ 2515 stream_num = (*track_stream >> 2516 ((MAX_STREAMS_TRACKED - 1) * BITS_PER_INDEX_STREAM)) & 2517 STREAM_MASK; 2518 current_sd = ¤t_ld_sd->stream_track[stream_num]; 2519 current_sd->is_read = io_info->isRead; 2520 current_sd->next_seq_lba = io_info->ldStartBlock + io_info->numBlocks; 2521 *track_stream = (((*track_stream & ZERO_LAST_STREAM) << 4) | stream_num); 2522 return; 2523 } 2524 2525 /** 2526 * megasas_set_raidflag_cpu_affinity - This function sets the cpu 2527 * affinity (cpu of the controller) and raid_flags in the raid context 2528 * based on IO type. 2529 * 2530 * @praid_context: IO RAID context 2531 * @raid: LD raid map 2532 * @fp_possible: Is fast path possible? 2533 * @is_read: Is read IO? 2534 * 2535 */ 2536 static void 2537 megasas_set_raidflag_cpu_affinity(union RAID_CONTEXT_UNION *praid_context, 2538 struct MR_LD_RAID *raid, bool fp_possible, 2539 u8 is_read, u32 scsi_buff_len) 2540 { 2541 u8 cpu_sel = MR_RAID_CTX_CPUSEL_0; 2542 struct RAID_CONTEXT_G35 *rctx_g35; 2543 2544 rctx_g35 = &praid_context->raid_context_g35; 2545 if (fp_possible) { 2546 if (is_read) { 2547 if ((raid->cpuAffinity.pdRead.cpu0) && 2548 (raid->cpuAffinity.pdRead.cpu1)) 2549 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS; 2550 else if (raid->cpuAffinity.pdRead.cpu1) 2551 cpu_sel = MR_RAID_CTX_CPUSEL_1; 2552 } else { 2553 if ((raid->cpuAffinity.pdWrite.cpu0) && 2554 (raid->cpuAffinity.pdWrite.cpu1)) 2555 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS; 2556 else if (raid->cpuAffinity.pdWrite.cpu1) 2557 cpu_sel = MR_RAID_CTX_CPUSEL_1; 2558 /* Fast path cache by pass capable R0/R1 VD */ 2559 if ((raid->level <= 1) && 2560 (raid->capability.fp_cache_bypass_capable)) { 2561 rctx_g35->routing_flags |= 2562 (1 << MR_RAID_CTX_ROUTINGFLAGS_SLD_SHIFT); 2563 rctx_g35->raid_flags = 2564 (MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS 2565 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT); 2566 } 2567 } 2568 } else { 2569 if (is_read) { 2570 if ((raid->cpuAffinity.ldRead.cpu0) && 2571 (raid->cpuAffinity.ldRead.cpu1)) 2572 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS; 2573 else if (raid->cpuAffinity.ldRead.cpu1) 2574 cpu_sel = MR_RAID_CTX_CPUSEL_1; 2575 } else { 2576 if ((raid->cpuAffinity.ldWrite.cpu0) && 2577 (raid->cpuAffinity.ldWrite.cpu1)) 2578 cpu_sel = MR_RAID_CTX_CPUSEL_FCFS; 2579 else if (raid->cpuAffinity.ldWrite.cpu1) 2580 cpu_sel = MR_RAID_CTX_CPUSEL_1; 2581 2582 if (is_stream_detected(rctx_g35) && 2583 ((raid->level == 5) || (raid->level == 6)) && 2584 (raid->writeMode == MR_RL_WRITE_THROUGH_MODE) && 2585 (cpu_sel == MR_RAID_CTX_CPUSEL_FCFS)) 2586 cpu_sel = MR_RAID_CTX_CPUSEL_0; 2587 } 2588 } 2589 2590 rctx_g35->routing_flags |= 2591 (cpu_sel << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT); 2592 2593 /* Always give priority to MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT 2594 * vs MR_RAID_FLAGS_IO_SUB_TYPE_CACHE_BYPASS. 2595 * IO Subtype is not bitmap. 2596 */ 2597 if ((raid->level == 1) && (!is_read)) { 2598 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE) 2599 praid_context->raid_context_g35.raid_flags = 2600 (MR_RAID_FLAGS_IO_SUB_TYPE_LDIO_BW_LIMIT 2601 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT); 2602 } 2603 } 2604 2605 /** 2606 * megasas_build_ldio_fusion - Prepares IOs to devices 2607 * @instance: Adapter soft state 2608 * @scp: SCSI command 2609 * @cmd: Command to be prepared 2610 * 2611 * Prepares the io_request and chain elements (sg_frame) for IO 2612 * The IO can be for PD (Fast Path) or LD 2613 */ 2614 void 2615 megasas_build_ldio_fusion(struct megasas_instance *instance, 2616 struct scsi_cmnd *scp, 2617 struct megasas_cmd_fusion *cmd) 2618 { 2619 bool fp_possible; 2620 u16 ld; 2621 u32 start_lba_lo, start_lba_hi, device_id, datalength = 0; 2622 u32 scsi_buff_len; 2623 struct MPI2_RAID_SCSI_IO_REQUEST *io_request; 2624 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc; 2625 struct IO_REQUEST_INFO io_info; 2626 struct fusion_context *fusion; 2627 struct MR_DRV_RAID_MAP_ALL *local_map_ptr; 2628 u8 *raidLUN; 2629 unsigned long spinlock_flags; 2630 struct MR_LD_RAID *raid = NULL; 2631 struct MR_PRIV_DEVICE *mrdev_priv; 2632 struct RAID_CONTEXT *rctx; 2633 struct RAID_CONTEXT_G35 *rctx_g35; 2634 2635 device_id = MEGASAS_DEV_INDEX(scp); 2636 2637 fusion = instance->ctrl_context; 2638 2639 io_request = cmd->io_request; 2640 rctx = &io_request->RaidContext.raid_context; 2641 rctx_g35 = &io_request->RaidContext.raid_context_g35; 2642 2643 rctx->virtual_disk_tgt_id = cpu_to_le16(device_id); 2644 rctx->status = 0; 2645 rctx->ex_status = 0; 2646 2647 req_desc = (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)cmd->request_desc; 2648 2649 start_lba_lo = 0; 2650 start_lba_hi = 0; 2651 fp_possible = false; 2652 2653 /* 2654 * 6-byte READ(0x08) or WRITE(0x0A) cdb 2655 */ 2656 if (scp->cmd_len == 6) { 2657 datalength = (u32) scp->cmnd[4]; 2658 start_lba_lo = ((u32) scp->cmnd[1] << 16) | 2659 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3]; 2660 2661 start_lba_lo &= 0x1FFFFF; 2662 } 2663 2664 /* 2665 * 10-byte READ(0x28) or WRITE(0x2A) cdb 2666 */ 2667 else if (scp->cmd_len == 10) { 2668 datalength = (u32) scp->cmnd[8] | 2669 ((u32) scp->cmnd[7] << 8); 2670 start_lba_lo = ((u32) scp->cmnd[2] << 24) | 2671 ((u32) scp->cmnd[3] << 16) | 2672 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5]; 2673 } 2674 2675 /* 2676 * 12-byte READ(0xA8) or WRITE(0xAA) cdb 2677 */ 2678 else if (scp->cmd_len == 12) { 2679 datalength = ((u32) scp->cmnd[6] << 24) | 2680 ((u32) scp->cmnd[7] << 16) | 2681 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9]; 2682 start_lba_lo = ((u32) scp->cmnd[2] << 24) | 2683 ((u32) scp->cmnd[3] << 16) | 2684 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5]; 2685 } 2686 2687 /* 2688 * 16-byte READ(0x88) or WRITE(0x8A) cdb 2689 */ 2690 else if (scp->cmd_len == 16) { 2691 datalength = ((u32) scp->cmnd[10] << 24) | 2692 ((u32) scp->cmnd[11] << 16) | 2693 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13]; 2694 start_lba_lo = ((u32) scp->cmnd[6] << 24) | 2695 ((u32) scp->cmnd[7] << 16) | 2696 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9]; 2697 2698 start_lba_hi = ((u32) scp->cmnd[2] << 24) | 2699 ((u32) scp->cmnd[3] << 16) | 2700 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5]; 2701 } 2702 2703 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO)); 2704 io_info.ldStartBlock = ((u64)start_lba_hi << 32) | start_lba_lo; 2705 io_info.numBlocks = datalength; 2706 io_info.ldTgtId = device_id; 2707 io_info.r1_alt_dev_handle = MR_DEVHANDLE_INVALID; 2708 scsi_buff_len = scsi_bufflen(scp); 2709 io_request->DataLength = cpu_to_le32(scsi_buff_len); 2710 2711 if (scp->sc_data_direction == DMA_FROM_DEVICE) 2712 io_info.isRead = 1; 2713 2714 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)]; 2715 ld = MR_TargetIdToLdGet(device_id, local_map_ptr); 2716 2717 if (ld < instance->fw_supported_vd_count) 2718 raid = MR_LdRaidGet(ld, local_map_ptr); 2719 2720 if (!raid || (!fusion->fast_path_io)) { 2721 rctx->reg_lock_flags = 0; 2722 fp_possible = false; 2723 } else { 2724 if (MR_BuildRaidContext(instance, &io_info, rctx, 2725 local_map_ptr, &raidLUN)) 2726 fp_possible = (io_info.fpOkForIo > 0) ? true : false; 2727 } 2728 2729 cmd->request_desc->SCSIIO.MSIxIndex = 2730 instance->reply_map[raw_smp_processor_id()]; 2731 2732 if (instance->adapter_type >= VENTURA_SERIES) { 2733 /* FP for Optimal raid level 1. 2734 * All large RAID-1 writes (> 32 KiB, both WT and WB modes) 2735 * are built by the driver as LD I/Os. 2736 * All small RAID-1 WT writes (<= 32 KiB) are built as FP I/Os 2737 * (there is never a reason to process these as buffered writes) 2738 * All small RAID-1 WB writes (<= 32 KiB) are built as FP I/Os 2739 * with the SLD bit asserted. 2740 */ 2741 if (io_info.r1_alt_dev_handle != MR_DEVHANDLE_INVALID) { 2742 mrdev_priv = scp->device->hostdata; 2743 2744 if (atomic_inc_return(&instance->fw_outstanding) > 2745 (instance->host->can_queue)) { 2746 fp_possible = false; 2747 atomic_dec(&instance->fw_outstanding); 2748 } else if ((scsi_buff_len > MR_LARGE_IO_MIN_SIZE) || 2749 (atomic_dec_if_positive(&mrdev_priv->r1_ldio_hint) > 0)) { 2750 fp_possible = false; 2751 atomic_dec(&instance->fw_outstanding); 2752 if (scsi_buff_len > MR_LARGE_IO_MIN_SIZE) 2753 atomic_set(&mrdev_priv->r1_ldio_hint, 2754 instance->r1_ldio_hint_default); 2755 } 2756 } 2757 2758 if (!fp_possible || 2759 (io_info.isRead && io_info.ra_capable)) { 2760 spin_lock_irqsave(&instance->stream_lock, 2761 spinlock_flags); 2762 megasas_stream_detect(instance, cmd, &io_info); 2763 spin_unlock_irqrestore(&instance->stream_lock, 2764 spinlock_flags); 2765 /* In ventura if stream detected for a read and it is 2766 * read ahead capable make this IO as LDIO 2767 */ 2768 if (is_stream_detected(rctx_g35)) 2769 fp_possible = false; 2770 } 2771 2772 /* If raid is NULL, set CPU affinity to default CPU0 */ 2773 if (raid) 2774 megasas_set_raidflag_cpu_affinity(&io_request->RaidContext, 2775 raid, fp_possible, io_info.isRead, 2776 scsi_buff_len); 2777 else 2778 rctx_g35->routing_flags |= 2779 (MR_RAID_CTX_CPUSEL_0 << MR_RAID_CTX_ROUTINGFLAGS_CPUSEL_SHIFT); 2780 } 2781 2782 if (fp_possible) { 2783 megasas_set_pd_lba(io_request, scp->cmd_len, &io_info, scp, 2784 local_map_ptr, start_lba_lo); 2785 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST; 2786 cmd->request_desc->SCSIIO.RequestFlags = 2787 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO 2788 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 2789 if (instance->adapter_type == INVADER_SERIES) { 2790 if (rctx->reg_lock_flags == REGION_TYPE_UNUSED) 2791 cmd->request_desc->SCSIIO.RequestFlags = 2792 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK << 2793 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 2794 rctx->type = MPI2_TYPE_CUDA; 2795 rctx->nseg = 0x1; 2796 io_request->IoFlags |= cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH); 2797 rctx->reg_lock_flags |= 2798 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA | 2799 MR_RL_FLAGS_SEQ_NUM_ENABLE); 2800 } else if (instance->adapter_type >= VENTURA_SERIES) { 2801 rctx_g35->nseg_type |= (1 << RAID_CONTEXT_NSEG_SHIFT); 2802 rctx_g35->nseg_type |= (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT); 2803 rctx_g35->routing_flags |= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT); 2804 io_request->IoFlags |= 2805 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH); 2806 } 2807 if (fusion->load_balance_info && 2808 (fusion->load_balance_info[device_id].loadBalanceFlag) && 2809 (io_info.isRead)) { 2810 io_info.devHandle = 2811 get_updated_dev_handle(instance, 2812 &fusion->load_balance_info[device_id], 2813 &io_info, local_map_ptr); 2814 scp->SCp.Status |= MEGASAS_LOAD_BALANCE_FLAG; 2815 cmd->pd_r1_lb = io_info.pd_after_lb; 2816 if (instance->adapter_type >= VENTURA_SERIES) 2817 rctx_g35->span_arm = io_info.span_arm; 2818 else 2819 rctx->span_arm = io_info.span_arm; 2820 2821 } else 2822 scp->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG; 2823 2824 if (instance->adapter_type >= VENTURA_SERIES) 2825 cmd->r1_alt_dev_handle = io_info.r1_alt_dev_handle; 2826 else 2827 cmd->r1_alt_dev_handle = MR_DEVHANDLE_INVALID; 2828 2829 if ((raidLUN[0] == 1) && 2830 (local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].validHandles > 1)) { 2831 instance->dev_handle = !(instance->dev_handle); 2832 io_info.devHandle = 2833 local_map_ptr->raidMap.devHndlInfo[io_info.pd_after_lb].devHandle[instance->dev_handle]; 2834 } 2835 2836 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle; 2837 io_request->DevHandle = io_info.devHandle; 2838 cmd->pd_interface = io_info.pd_interface; 2839 /* populate the LUN field */ 2840 memcpy(io_request->LUN, raidLUN, 8); 2841 } else { 2842 rctx->timeout_value = 2843 cpu_to_le16(local_map_ptr->raidMap.fpPdIoTimeoutSec); 2844 cmd->request_desc->SCSIIO.RequestFlags = 2845 (MEGASAS_REQ_DESCRIPT_FLAGS_LD_IO 2846 << MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 2847 if (instance->adapter_type == INVADER_SERIES) { 2848 if (io_info.do_fp_rlbypass || 2849 (rctx->reg_lock_flags == REGION_TYPE_UNUSED)) 2850 cmd->request_desc->SCSIIO.RequestFlags = 2851 (MEGASAS_REQ_DESCRIPT_FLAGS_NO_LOCK << 2852 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 2853 rctx->type = MPI2_TYPE_CUDA; 2854 rctx->reg_lock_flags |= 2855 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 | 2856 MR_RL_FLAGS_SEQ_NUM_ENABLE); 2857 rctx->nseg = 0x1; 2858 } else if (instance->adapter_type >= VENTURA_SERIES) { 2859 rctx_g35->routing_flags |= (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT); 2860 rctx_g35->nseg_type |= (1 << RAID_CONTEXT_NSEG_SHIFT); 2861 rctx_g35->nseg_type |= (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT); 2862 } 2863 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST; 2864 io_request->DevHandle = cpu_to_le16(device_id); 2865 2866 } /* Not FP */ 2867 } 2868 2869 /** 2870 * megasas_build_ld_nonrw_fusion - prepares non rw ios for virtual disk 2871 * @instance: Adapter soft state 2872 * @scp: SCSI command 2873 * @cmd: Command to be prepared 2874 * 2875 * Prepares the io_request frame for non-rw io cmds for vd. 2876 */ 2877 static void megasas_build_ld_nonrw_fusion(struct megasas_instance *instance, 2878 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd) 2879 { 2880 u32 device_id; 2881 struct MPI2_RAID_SCSI_IO_REQUEST *io_request; 2882 u16 ld; 2883 struct MR_DRV_RAID_MAP_ALL *local_map_ptr; 2884 struct fusion_context *fusion = instance->ctrl_context; 2885 u8 span, physArm; 2886 __le16 devHandle; 2887 u32 arRef, pd; 2888 struct MR_LD_RAID *raid; 2889 struct RAID_CONTEXT *pRAID_Context; 2890 u8 fp_possible = 1; 2891 2892 io_request = cmd->io_request; 2893 device_id = MEGASAS_DEV_INDEX(scmd); 2894 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)]; 2895 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd)); 2896 /* get RAID_Context pointer */ 2897 pRAID_Context = &io_request->RaidContext.raid_context; 2898 /* Check with FW team */ 2899 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id); 2900 pRAID_Context->reg_lock_row_lba = 0; 2901 pRAID_Context->reg_lock_length = 0; 2902 2903 if (fusion->fast_path_io && ( 2904 device_id < instance->fw_supported_vd_count)) { 2905 2906 ld = MR_TargetIdToLdGet(device_id, local_map_ptr); 2907 if (ld >= instance->fw_supported_vd_count - 1) 2908 fp_possible = 0; 2909 else { 2910 raid = MR_LdRaidGet(ld, local_map_ptr); 2911 if (!(raid->capability.fpNonRWCapable)) 2912 fp_possible = 0; 2913 } 2914 } else 2915 fp_possible = 0; 2916 2917 if (!fp_possible) { 2918 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST; 2919 io_request->DevHandle = cpu_to_le16(device_id); 2920 io_request->LUN[1] = scmd->device->lun; 2921 pRAID_Context->timeout_value = 2922 cpu_to_le16 (scmd->request->timeout / HZ); 2923 cmd->request_desc->SCSIIO.RequestFlags = 2924 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO << 2925 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 2926 } else { 2927 2928 /* set RAID context values */ 2929 pRAID_Context->config_seq_num = raid->seqNum; 2930 if (instance->adapter_type < VENTURA_SERIES) 2931 pRAID_Context->reg_lock_flags = REGION_TYPE_SHARED_READ; 2932 pRAID_Context->timeout_value = 2933 cpu_to_le16(raid->fpIoTimeoutForLd); 2934 2935 /* get the DevHandle for the PD (since this is 2936 fpNonRWCapable, this is a single disk RAID0) */ 2937 span = physArm = 0; 2938 arRef = MR_LdSpanArrayGet(ld, span, local_map_ptr); 2939 pd = MR_ArPdGet(arRef, physArm, local_map_ptr); 2940 devHandle = MR_PdDevHandleGet(pd, local_map_ptr); 2941 2942 /* build request descriptor */ 2943 cmd->request_desc->SCSIIO.RequestFlags = 2944 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO << 2945 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 2946 cmd->request_desc->SCSIIO.DevHandle = devHandle; 2947 2948 /* populate the LUN field */ 2949 memcpy(io_request->LUN, raid->LUN, 8); 2950 2951 /* build the raidScsiIO structure */ 2952 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST; 2953 io_request->DevHandle = devHandle; 2954 } 2955 } 2956 2957 /** 2958 * megasas_build_syspd_fusion - prepares rw/non-rw ios for syspd 2959 * @instance: Adapter soft state 2960 * @scp: SCSI command 2961 * @cmd: Command to be prepared 2962 * @fp_possible: parameter to detect fast path or firmware path io. 2963 * 2964 * Prepares the io_request frame for rw/non-rw io cmds for syspds 2965 */ 2966 static void 2967 megasas_build_syspd_fusion(struct megasas_instance *instance, 2968 struct scsi_cmnd *scmd, struct megasas_cmd_fusion *cmd, 2969 bool fp_possible) 2970 { 2971 u32 device_id; 2972 struct MPI2_RAID_SCSI_IO_REQUEST *io_request; 2973 u16 pd_index = 0; 2974 u16 os_timeout_value; 2975 u16 timeout_limit; 2976 struct MR_DRV_RAID_MAP_ALL *local_map_ptr; 2977 struct RAID_CONTEXT *pRAID_Context; 2978 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync; 2979 struct MR_PRIV_DEVICE *mr_device_priv_data; 2980 struct fusion_context *fusion = instance->ctrl_context; 2981 pd_sync = (void *)fusion->pd_seq_sync[(instance->pd_seq_map_id - 1) & 1]; 2982 2983 device_id = MEGASAS_DEV_INDEX(scmd); 2984 pd_index = MEGASAS_PD_INDEX(scmd); 2985 os_timeout_value = scmd->request->timeout / HZ; 2986 mr_device_priv_data = scmd->device->hostdata; 2987 cmd->pd_interface = mr_device_priv_data->interface_type; 2988 2989 io_request = cmd->io_request; 2990 /* get RAID_Context pointer */ 2991 pRAID_Context = &io_request->RaidContext.raid_context; 2992 pRAID_Context->reg_lock_flags = 0; 2993 pRAID_Context->reg_lock_row_lba = 0; 2994 pRAID_Context->reg_lock_length = 0; 2995 io_request->DataLength = cpu_to_le32(scsi_bufflen(scmd)); 2996 io_request->LUN[1] = scmd->device->lun; 2997 pRAID_Context->raid_flags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD 2998 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT; 2999 3000 /* If FW supports PD sequence number */ 3001 if (instance->use_seqnum_jbod_fp && 3002 instance->pd_list[pd_index].driveType == TYPE_DISK) { 3003 /* TgtId must be incremented by 255 as jbod seq number is index 3004 * below raid map 3005 */ 3006 /* More than 256 PD/JBOD support for Ventura */ 3007 if (instance->support_morethan256jbod) 3008 pRAID_Context->virtual_disk_tgt_id = 3009 pd_sync->seq[pd_index].pd_target_id; 3010 else 3011 pRAID_Context->virtual_disk_tgt_id = 3012 cpu_to_le16(device_id + (MAX_PHYSICAL_DEVICES - 1)); 3013 pRAID_Context->config_seq_num = pd_sync->seq[pd_index].seqNum; 3014 io_request->DevHandle = pd_sync->seq[pd_index].devHandle; 3015 if (instance->adapter_type >= VENTURA_SERIES) { 3016 io_request->RaidContext.raid_context_g35.routing_flags |= 3017 (1 << MR_RAID_CTX_ROUTINGFLAGS_SQN_SHIFT); 3018 io_request->RaidContext.raid_context_g35.nseg_type |= 3019 (1 << RAID_CONTEXT_NSEG_SHIFT); 3020 io_request->RaidContext.raid_context_g35.nseg_type |= 3021 (MPI2_TYPE_CUDA << RAID_CONTEXT_TYPE_SHIFT); 3022 } else { 3023 pRAID_Context->type = MPI2_TYPE_CUDA; 3024 pRAID_Context->nseg = 0x1; 3025 pRAID_Context->reg_lock_flags |= 3026 (MR_RL_FLAGS_SEQ_NUM_ENABLE|MR_RL_FLAGS_GRANT_DESTINATION_CUDA); 3027 } 3028 } else if (fusion->fast_path_io) { 3029 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id); 3030 pRAID_Context->config_seq_num = 0; 3031 local_map_ptr = fusion->ld_drv_map[(instance->map_id & 1)]; 3032 io_request->DevHandle = 3033 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl; 3034 } else { 3035 /* Want to send all IO via FW path */ 3036 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id); 3037 pRAID_Context->config_seq_num = 0; 3038 io_request->DevHandle = cpu_to_le16(0xFFFF); 3039 } 3040 3041 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle; 3042 3043 cmd->request_desc->SCSIIO.MSIxIndex = 3044 instance->reply_map[raw_smp_processor_id()]; 3045 3046 if (!fp_possible) { 3047 /* system pd firmware path */ 3048 io_request->Function = MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST; 3049 cmd->request_desc->SCSIIO.RequestFlags = 3050 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO << 3051 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 3052 pRAID_Context->timeout_value = cpu_to_le16(os_timeout_value); 3053 pRAID_Context->virtual_disk_tgt_id = cpu_to_le16(device_id); 3054 } else { 3055 if (os_timeout_value) 3056 os_timeout_value++; 3057 3058 /* system pd Fast Path */ 3059 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST; 3060 timeout_limit = (scmd->device->type == TYPE_DISK) ? 3061 255 : 0xFFFF; 3062 pRAID_Context->timeout_value = 3063 cpu_to_le16((os_timeout_value > timeout_limit) ? 3064 timeout_limit : os_timeout_value); 3065 if (instance->adapter_type >= INVADER_SERIES) 3066 io_request->IoFlags |= 3067 cpu_to_le16(MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH); 3068 3069 cmd->request_desc->SCSIIO.RequestFlags = 3070 (MPI2_REQ_DESCRIPT_FLAGS_FP_IO << 3071 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 3072 } 3073 } 3074 3075 /** 3076 * megasas_build_io_fusion - Prepares IOs to devices 3077 * @instance: Adapter soft state 3078 * @scp: SCSI command 3079 * @cmd: Command to be prepared 3080 * 3081 * Invokes helper functions to prepare request frames 3082 * and sets flags appropriate for IO/Non-IO cmd 3083 */ 3084 int 3085 megasas_build_io_fusion(struct megasas_instance *instance, 3086 struct scsi_cmnd *scp, 3087 struct megasas_cmd_fusion *cmd) 3088 { 3089 int sge_count; 3090 u8 cmd_type; 3091 struct MPI2_RAID_SCSI_IO_REQUEST *io_request = cmd->io_request; 3092 struct MR_PRIV_DEVICE *mr_device_priv_data; 3093 mr_device_priv_data = scp->device->hostdata; 3094 3095 /* Zero out some fields so they don't get reused */ 3096 memset(io_request->LUN, 0x0, 8); 3097 io_request->CDB.EEDP32.PrimaryReferenceTag = 0; 3098 io_request->CDB.EEDP32.PrimaryApplicationTagMask = 0; 3099 io_request->EEDPFlags = 0; 3100 io_request->Control = 0; 3101 io_request->EEDPBlockSize = 0; 3102 io_request->ChainOffset = 0; 3103 io_request->RaidContext.raid_context.raid_flags = 0; 3104 io_request->RaidContext.raid_context.type = 0; 3105 io_request->RaidContext.raid_context.nseg = 0; 3106 3107 memcpy(io_request->CDB.CDB32, scp->cmnd, scp->cmd_len); 3108 /* 3109 * Just the CDB length,rest of the Flags are zero 3110 * This will be modified for FP in build_ldio_fusion 3111 */ 3112 io_request->IoFlags = cpu_to_le16(scp->cmd_len); 3113 3114 switch (cmd_type = megasas_cmd_type(scp)) { 3115 case READ_WRITE_LDIO: 3116 megasas_build_ldio_fusion(instance, scp, cmd); 3117 break; 3118 case NON_READ_WRITE_LDIO: 3119 megasas_build_ld_nonrw_fusion(instance, scp, cmd); 3120 break; 3121 case READ_WRITE_SYSPDIO: 3122 megasas_build_syspd_fusion(instance, scp, cmd, true); 3123 break; 3124 case NON_READ_WRITE_SYSPDIO: 3125 if (instance->secure_jbod_support || 3126 mr_device_priv_data->is_tm_capable) 3127 megasas_build_syspd_fusion(instance, scp, cmd, false); 3128 else 3129 megasas_build_syspd_fusion(instance, scp, cmd, true); 3130 break; 3131 default: 3132 break; 3133 } 3134 3135 /* 3136 * Construct SGL 3137 */ 3138 3139 sge_count = megasas_make_sgl(instance, scp, cmd); 3140 3141 if (sge_count > instance->max_num_sge || (sge_count < 0)) { 3142 dev_err(&instance->pdev->dev, 3143 "%s %d sge_count (%d) is out of range. Range is: 0-%d\n", 3144 __func__, __LINE__, sge_count, instance->max_num_sge); 3145 return 1; 3146 } 3147 3148 if (instance->adapter_type >= VENTURA_SERIES) { 3149 set_num_sge(&io_request->RaidContext.raid_context_g35, sge_count); 3150 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.routing_flags); 3151 cpu_to_le16s(&io_request->RaidContext.raid_context_g35.nseg_type); 3152 } else { 3153 /* numSGE store lower 8 bit of sge_count. 3154 * numSGEExt store higher 8 bit of sge_count 3155 */ 3156 io_request->RaidContext.raid_context.num_sge = sge_count; 3157 io_request->RaidContext.raid_context.num_sge_ext = 3158 (u8)(sge_count >> 8); 3159 } 3160 3161 io_request->SGLFlags = cpu_to_le16(MPI2_SGE_FLAGS_64_BIT_ADDRESSING); 3162 3163 if (scp->sc_data_direction == DMA_TO_DEVICE) 3164 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_WRITE); 3165 else if (scp->sc_data_direction == DMA_FROM_DEVICE) 3166 io_request->Control |= cpu_to_le32(MPI2_SCSIIO_CONTROL_READ); 3167 3168 io_request->SGLOffset0 = 3169 offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, SGL) / 4; 3170 3171 io_request->SenseBufferLowAddress = 3172 cpu_to_le32(lower_32_bits(cmd->sense_phys_addr)); 3173 io_request->SenseBufferLength = SCSI_SENSE_BUFFERSIZE; 3174 3175 cmd->scmd = scp; 3176 scp->SCp.ptr = (char *)cmd; 3177 3178 return 0; 3179 } 3180 3181 static union MEGASAS_REQUEST_DESCRIPTOR_UNION * 3182 megasas_get_request_descriptor(struct megasas_instance *instance, u16 index) 3183 { 3184 u8 *p; 3185 struct fusion_context *fusion; 3186 3187 fusion = instance->ctrl_context; 3188 p = fusion->req_frames_desc + 3189 sizeof(union MEGASAS_REQUEST_DESCRIPTOR_UNION) * index; 3190 3191 return (union MEGASAS_REQUEST_DESCRIPTOR_UNION *)p; 3192 } 3193 3194 3195 /* megasas_prepate_secondRaid1_IO 3196 * It prepares the raid 1 second IO 3197 */ 3198 void megasas_prepare_secondRaid1_IO(struct megasas_instance *instance, 3199 struct megasas_cmd_fusion *cmd, 3200 struct megasas_cmd_fusion *r1_cmd) 3201 { 3202 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc, *req_desc2 = NULL; 3203 struct fusion_context *fusion; 3204 fusion = instance->ctrl_context; 3205 req_desc = cmd->request_desc; 3206 /* copy the io request frame as well as 8 SGEs data for r1 command*/ 3207 memcpy(r1_cmd->io_request, cmd->io_request, 3208 (sizeof(struct MPI2_RAID_SCSI_IO_REQUEST))); 3209 memcpy(&r1_cmd->io_request->SGL, &cmd->io_request->SGL, 3210 (fusion->max_sge_in_main_msg * sizeof(union MPI2_SGE_IO_UNION))); 3211 /*sense buffer is different for r1 command*/ 3212 r1_cmd->io_request->SenseBufferLowAddress = 3213 cpu_to_le32(lower_32_bits(r1_cmd->sense_phys_addr)); 3214 r1_cmd->scmd = cmd->scmd; 3215 req_desc2 = megasas_get_request_descriptor(instance, 3216 (r1_cmd->index - 1)); 3217 req_desc2->Words = 0; 3218 r1_cmd->request_desc = req_desc2; 3219 req_desc2->SCSIIO.SMID = cpu_to_le16(r1_cmd->index); 3220 req_desc2->SCSIIO.RequestFlags = req_desc->SCSIIO.RequestFlags; 3221 r1_cmd->request_desc->SCSIIO.DevHandle = cmd->r1_alt_dev_handle; 3222 r1_cmd->io_request->DevHandle = cmd->r1_alt_dev_handle; 3223 r1_cmd->r1_alt_dev_handle = cmd->io_request->DevHandle; 3224 cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid = 3225 cpu_to_le16(r1_cmd->index); 3226 r1_cmd->io_request->RaidContext.raid_context_g35.smid.peer_smid = 3227 cpu_to_le16(cmd->index); 3228 /*MSIxIndex of both commands request descriptors should be same*/ 3229 r1_cmd->request_desc->SCSIIO.MSIxIndex = 3230 cmd->request_desc->SCSIIO.MSIxIndex; 3231 /*span arm is different for r1 cmd*/ 3232 r1_cmd->io_request->RaidContext.raid_context_g35.span_arm = 3233 cmd->io_request->RaidContext.raid_context_g35.span_arm + 1; 3234 } 3235 3236 /** 3237 * megasas_build_and_issue_cmd_fusion -Main routine for building and 3238 * issuing non IOCTL cmd 3239 * @instance: Adapter soft state 3240 * @scmd: pointer to scsi cmd from OS 3241 */ 3242 static u32 3243 megasas_build_and_issue_cmd_fusion(struct megasas_instance *instance, 3244 struct scsi_cmnd *scmd) 3245 { 3246 struct megasas_cmd_fusion *cmd, *r1_cmd = NULL; 3247 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc; 3248 u32 index; 3249 struct fusion_context *fusion; 3250 3251 fusion = instance->ctrl_context; 3252 3253 if ((megasas_cmd_type(scmd) == READ_WRITE_LDIO) && 3254 instance->ldio_threshold && 3255 (atomic_inc_return(&instance->ldio_outstanding) > 3256 instance->ldio_threshold)) { 3257 atomic_dec(&instance->ldio_outstanding); 3258 return SCSI_MLQUEUE_DEVICE_BUSY; 3259 } 3260 3261 if (atomic_inc_return(&instance->fw_outstanding) > 3262 instance->host->can_queue) { 3263 atomic_dec(&instance->fw_outstanding); 3264 return SCSI_MLQUEUE_HOST_BUSY; 3265 } 3266 3267 cmd = megasas_get_cmd_fusion(instance, scmd->request->tag); 3268 3269 if (!cmd) { 3270 atomic_dec(&instance->fw_outstanding); 3271 return SCSI_MLQUEUE_HOST_BUSY; 3272 } 3273 3274 index = cmd->index; 3275 3276 req_desc = megasas_get_request_descriptor(instance, index-1); 3277 3278 req_desc->Words = 0; 3279 cmd->request_desc = req_desc; 3280 3281 if (megasas_build_io_fusion(instance, scmd, cmd)) { 3282 megasas_return_cmd_fusion(instance, cmd); 3283 dev_err(&instance->pdev->dev, "Error building command\n"); 3284 cmd->request_desc = NULL; 3285 atomic_dec(&instance->fw_outstanding); 3286 return SCSI_MLQUEUE_HOST_BUSY; 3287 } 3288 3289 req_desc = cmd->request_desc; 3290 req_desc->SCSIIO.SMID = cpu_to_le16(index); 3291 3292 if (cmd->io_request->ChainOffset != 0 && 3293 cmd->io_request->ChainOffset != 0xF) 3294 dev_err(&instance->pdev->dev, "The chain offset value is not " 3295 "correct : %x\n", cmd->io_request->ChainOffset); 3296 /* 3297 * if it is raid 1/10 fp write capable. 3298 * try to get second command from pool and construct it. 3299 * From FW, it has confirmed that lba values of two PDs 3300 * corresponds to single R1/10 LD are always same 3301 * 3302 */ 3303 /* driver side count always should be less than max_fw_cmds 3304 * to get new command 3305 */ 3306 if (cmd->r1_alt_dev_handle != MR_DEVHANDLE_INVALID) { 3307 r1_cmd = megasas_get_cmd_fusion(instance, 3308 (scmd->request->tag + instance->max_fw_cmds)); 3309 megasas_prepare_secondRaid1_IO(instance, cmd, r1_cmd); 3310 } 3311 3312 3313 /* 3314 * Issue the command to the FW 3315 */ 3316 3317 megasas_fire_cmd_fusion(instance, req_desc); 3318 3319 if (r1_cmd) 3320 megasas_fire_cmd_fusion(instance, r1_cmd->request_desc); 3321 3322 3323 return 0; 3324 } 3325 3326 /** 3327 * megasas_complete_r1_command - 3328 * completes R1 FP write commands which has valid peer smid 3329 * @instance: Adapter soft state 3330 * @cmd_fusion: MPT command frame 3331 * 3332 */ 3333 static inline void 3334 megasas_complete_r1_command(struct megasas_instance *instance, 3335 struct megasas_cmd_fusion *cmd) 3336 { 3337 u8 *sense, status, ex_status; 3338 u32 data_length; 3339 u16 peer_smid; 3340 struct fusion_context *fusion; 3341 struct megasas_cmd_fusion *r1_cmd = NULL; 3342 struct scsi_cmnd *scmd_local = NULL; 3343 struct RAID_CONTEXT_G35 *rctx_g35; 3344 3345 rctx_g35 = &cmd->io_request->RaidContext.raid_context_g35; 3346 fusion = instance->ctrl_context; 3347 peer_smid = le16_to_cpu(rctx_g35->smid.peer_smid); 3348 3349 r1_cmd = fusion->cmd_list[peer_smid - 1]; 3350 scmd_local = cmd->scmd; 3351 status = rctx_g35->status; 3352 ex_status = rctx_g35->ex_status; 3353 data_length = cmd->io_request->DataLength; 3354 sense = cmd->sense; 3355 3356 cmd->cmd_completed = true; 3357 3358 /* Check if peer command is completed or not*/ 3359 if (r1_cmd->cmd_completed) { 3360 rctx_g35 = &r1_cmd->io_request->RaidContext.raid_context_g35; 3361 if (rctx_g35->status != MFI_STAT_OK) { 3362 status = rctx_g35->status; 3363 ex_status = rctx_g35->ex_status; 3364 data_length = r1_cmd->io_request->DataLength; 3365 sense = r1_cmd->sense; 3366 } 3367 3368 megasas_return_cmd_fusion(instance, r1_cmd); 3369 map_cmd_status(fusion, scmd_local, status, ex_status, 3370 le32_to_cpu(data_length), sense); 3371 if (instance->ldio_threshold && 3372 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO) 3373 atomic_dec(&instance->ldio_outstanding); 3374 scmd_local->SCp.ptr = NULL; 3375 megasas_return_cmd_fusion(instance, cmd); 3376 scsi_dma_unmap(scmd_local); 3377 scmd_local->scsi_done(scmd_local); 3378 } 3379 } 3380 3381 /** 3382 * complete_cmd_fusion - Completes command 3383 * @instance: Adapter soft state 3384 * Completes all commands that is in reply descriptor queue 3385 */ 3386 int 3387 complete_cmd_fusion(struct megasas_instance *instance, u32 MSIxIndex) 3388 { 3389 union MPI2_REPLY_DESCRIPTORS_UNION *desc; 3390 struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *reply_desc; 3391 struct MPI2_RAID_SCSI_IO_REQUEST *scsi_io_req; 3392 struct fusion_context *fusion; 3393 struct megasas_cmd *cmd_mfi; 3394 struct megasas_cmd_fusion *cmd_fusion; 3395 u16 smid, num_completed; 3396 u8 reply_descript_type, *sense, status, extStatus; 3397 u32 device_id, data_length; 3398 union desc_value d_val; 3399 struct LD_LOAD_BALANCE_INFO *lbinfo; 3400 int threshold_reply_count = 0; 3401 struct scsi_cmnd *scmd_local = NULL; 3402 struct MR_TASK_MANAGE_REQUEST *mr_tm_req; 3403 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_tm_req; 3404 3405 fusion = instance->ctrl_context; 3406 3407 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) 3408 return IRQ_HANDLED; 3409 3410 desc = fusion->reply_frames_desc[MSIxIndex] + 3411 fusion->last_reply_idx[MSIxIndex]; 3412 3413 reply_desc = (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc; 3414 3415 d_val.word = desc->Words; 3416 3417 reply_descript_type = reply_desc->ReplyFlags & 3418 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK; 3419 3420 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) 3421 return IRQ_NONE; 3422 3423 num_completed = 0; 3424 3425 while (d_val.u.low != cpu_to_le32(UINT_MAX) && 3426 d_val.u.high != cpu_to_le32(UINT_MAX)) { 3427 3428 smid = le16_to_cpu(reply_desc->SMID); 3429 cmd_fusion = fusion->cmd_list[smid - 1]; 3430 scsi_io_req = (struct MPI2_RAID_SCSI_IO_REQUEST *) 3431 cmd_fusion->io_request; 3432 3433 scmd_local = cmd_fusion->scmd; 3434 status = scsi_io_req->RaidContext.raid_context.status; 3435 extStatus = scsi_io_req->RaidContext.raid_context.ex_status; 3436 sense = cmd_fusion->sense; 3437 data_length = scsi_io_req->DataLength; 3438 3439 switch (scsi_io_req->Function) { 3440 case MPI2_FUNCTION_SCSI_TASK_MGMT: 3441 mr_tm_req = (struct MR_TASK_MANAGE_REQUEST *) 3442 cmd_fusion->io_request; 3443 mpi_tm_req = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) 3444 &mr_tm_req->TmRequest; 3445 dev_dbg(&instance->pdev->dev, "TM completion:" 3446 "type: 0x%x TaskMID: 0x%x\n", 3447 mpi_tm_req->TaskType, mpi_tm_req->TaskMID); 3448 complete(&cmd_fusion->done); 3449 break; 3450 case MPI2_FUNCTION_SCSI_IO_REQUEST: /*Fast Path IO.*/ 3451 /* Update load balancing info */ 3452 if (fusion->load_balance_info && 3453 (cmd_fusion->scmd->SCp.Status & 3454 MEGASAS_LOAD_BALANCE_FLAG)) { 3455 device_id = MEGASAS_DEV_INDEX(scmd_local); 3456 lbinfo = &fusion->load_balance_info[device_id]; 3457 atomic_dec(&lbinfo->scsi_pending_cmds[cmd_fusion->pd_r1_lb]); 3458 cmd_fusion->scmd->SCp.Status &= ~MEGASAS_LOAD_BALANCE_FLAG; 3459 } 3460 /* Fall through - and complete IO */ 3461 case MEGASAS_MPI2_FUNCTION_LD_IO_REQUEST: /* LD-IO Path */ 3462 atomic_dec(&instance->fw_outstanding); 3463 if (cmd_fusion->r1_alt_dev_handle == MR_DEVHANDLE_INVALID) { 3464 map_cmd_status(fusion, scmd_local, status, 3465 extStatus, le32_to_cpu(data_length), 3466 sense); 3467 if (instance->ldio_threshold && 3468 (megasas_cmd_type(scmd_local) == READ_WRITE_LDIO)) 3469 atomic_dec(&instance->ldio_outstanding); 3470 scmd_local->SCp.ptr = NULL; 3471 megasas_return_cmd_fusion(instance, cmd_fusion); 3472 scsi_dma_unmap(scmd_local); 3473 scmd_local->scsi_done(scmd_local); 3474 } else /* Optimal VD - R1 FP command completion. */ 3475 megasas_complete_r1_command(instance, cmd_fusion); 3476 break; 3477 case MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST: /*MFI command */ 3478 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx]; 3479 /* Poll mode. Dummy free. 3480 * In case of Interrupt mode, caller has reverse check. 3481 */ 3482 if (cmd_mfi->flags & DRV_DCMD_POLLED_MODE) { 3483 cmd_mfi->flags &= ~DRV_DCMD_POLLED_MODE; 3484 megasas_return_cmd(instance, cmd_mfi); 3485 } else 3486 megasas_complete_cmd(instance, cmd_mfi, DID_OK); 3487 break; 3488 } 3489 3490 fusion->last_reply_idx[MSIxIndex]++; 3491 if (fusion->last_reply_idx[MSIxIndex] >= 3492 fusion->reply_q_depth) 3493 fusion->last_reply_idx[MSIxIndex] = 0; 3494 3495 desc->Words = cpu_to_le64(ULLONG_MAX); 3496 num_completed++; 3497 threshold_reply_count++; 3498 3499 /* Get the next reply descriptor */ 3500 if (!fusion->last_reply_idx[MSIxIndex]) 3501 desc = fusion->reply_frames_desc[MSIxIndex]; 3502 else 3503 desc++; 3504 3505 reply_desc = 3506 (struct MPI2_SCSI_IO_SUCCESS_REPLY_DESCRIPTOR *)desc; 3507 3508 d_val.word = desc->Words; 3509 3510 reply_descript_type = reply_desc->ReplyFlags & 3511 MPI2_RPY_DESCRIPT_FLAGS_TYPE_MASK; 3512 3513 if (reply_descript_type == MPI2_RPY_DESCRIPT_FLAGS_UNUSED) 3514 break; 3515 /* 3516 * Write to reply post host index register after completing threshold 3517 * number of reply counts and still there are more replies in reply queue 3518 * pending to be completed 3519 */ 3520 if (threshold_reply_count >= THRESHOLD_REPLY_COUNT) { 3521 if (instance->msix_combined) 3522 writel(((MSIxIndex & 0x7) << 24) | 3523 fusion->last_reply_idx[MSIxIndex], 3524 instance->reply_post_host_index_addr[MSIxIndex/8]); 3525 else 3526 writel((MSIxIndex << 24) | 3527 fusion->last_reply_idx[MSIxIndex], 3528 instance->reply_post_host_index_addr[0]); 3529 threshold_reply_count = 0; 3530 } 3531 } 3532 3533 if (!num_completed) 3534 return IRQ_NONE; 3535 3536 wmb(); 3537 if (instance->msix_combined) 3538 writel(((MSIxIndex & 0x7) << 24) | 3539 fusion->last_reply_idx[MSIxIndex], 3540 instance->reply_post_host_index_addr[MSIxIndex/8]); 3541 else 3542 writel((MSIxIndex << 24) | 3543 fusion->last_reply_idx[MSIxIndex], 3544 instance->reply_post_host_index_addr[0]); 3545 megasas_check_and_restore_queue_depth(instance); 3546 return IRQ_HANDLED; 3547 } 3548 3549 /** 3550 * megasas_sync_irqs - Synchronizes all IRQs owned by adapter 3551 * @instance: Adapter soft state 3552 */ 3553 void megasas_sync_irqs(unsigned long instance_addr) 3554 { 3555 u32 count, i; 3556 struct megasas_instance *instance = 3557 (struct megasas_instance *)instance_addr; 3558 3559 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1; 3560 3561 for (i = 0; i < count; i++) 3562 synchronize_irq(pci_irq_vector(instance->pdev, i)); 3563 } 3564 3565 /** 3566 * megasas_complete_cmd_dpc_fusion - Completes command 3567 * @instance: Adapter soft state 3568 * 3569 * Tasklet to complete cmds 3570 */ 3571 void 3572 megasas_complete_cmd_dpc_fusion(unsigned long instance_addr) 3573 { 3574 struct megasas_instance *instance = 3575 (struct megasas_instance *)instance_addr; 3576 u32 count, MSIxIndex; 3577 3578 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1; 3579 3580 /* If we have already declared adapter dead, donot complete cmds */ 3581 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) 3582 return; 3583 3584 for (MSIxIndex = 0 ; MSIxIndex < count; MSIxIndex++) 3585 complete_cmd_fusion(instance, MSIxIndex); 3586 } 3587 3588 /** 3589 * megasas_isr_fusion - isr entry point 3590 */ 3591 irqreturn_t megasas_isr_fusion(int irq, void *devp) 3592 { 3593 struct megasas_irq_context *irq_context = devp; 3594 struct megasas_instance *instance = irq_context->instance; 3595 u32 mfiStatus; 3596 3597 if (instance->mask_interrupts) 3598 return IRQ_NONE; 3599 3600 if (!instance->msix_vectors) { 3601 mfiStatus = instance->instancet->clear_intr(instance); 3602 if (!mfiStatus) 3603 return IRQ_NONE; 3604 } 3605 3606 /* If we are resetting, bail */ 3607 if (test_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags)) { 3608 instance->instancet->clear_intr(instance); 3609 return IRQ_HANDLED; 3610 } 3611 3612 return complete_cmd_fusion(instance, irq_context->MSIxIndex); 3613 } 3614 3615 /** 3616 * build_mpt_mfi_pass_thru - builds a cmd fo MFI Pass thru 3617 * @instance: Adapter soft state 3618 * mfi_cmd: megasas_cmd pointer 3619 * 3620 */ 3621 void 3622 build_mpt_mfi_pass_thru(struct megasas_instance *instance, 3623 struct megasas_cmd *mfi_cmd) 3624 { 3625 struct MPI25_IEEE_SGE_CHAIN64 *mpi25_ieee_chain; 3626 struct MPI2_RAID_SCSI_IO_REQUEST *io_req; 3627 struct megasas_cmd_fusion *cmd; 3628 struct fusion_context *fusion; 3629 struct megasas_header *frame_hdr = &mfi_cmd->frame->hdr; 3630 3631 fusion = instance->ctrl_context; 3632 3633 cmd = megasas_get_cmd_fusion(instance, 3634 instance->max_scsi_cmds + mfi_cmd->index); 3635 3636 /* Save the smid. To be used for returning the cmd */ 3637 mfi_cmd->context.smid = cmd->index; 3638 3639 /* 3640 * For cmds where the flag is set, store the flag and check 3641 * on completion. For cmds with this flag, don't call 3642 * megasas_complete_cmd 3643 */ 3644 3645 if (frame_hdr->flags & cpu_to_le16(MFI_FRAME_DONT_POST_IN_REPLY_QUEUE)) 3646 mfi_cmd->flags |= DRV_DCMD_POLLED_MODE; 3647 3648 io_req = cmd->io_request; 3649 3650 if (instance->adapter_type >= INVADER_SERIES) { 3651 struct MPI25_IEEE_SGE_CHAIN64 *sgl_ptr_end = 3652 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL; 3653 sgl_ptr_end += fusion->max_sge_in_main_msg - 1; 3654 sgl_ptr_end->Flags = 0; 3655 } 3656 3657 mpi25_ieee_chain = 3658 (struct MPI25_IEEE_SGE_CHAIN64 *)&io_req->SGL.IeeeChain; 3659 3660 io_req->Function = MEGASAS_MPI2_FUNCTION_PASSTHRU_IO_REQUEST; 3661 io_req->SGLOffset0 = offsetof(struct MPI2_RAID_SCSI_IO_REQUEST, 3662 SGL) / 4; 3663 io_req->ChainOffset = fusion->chain_offset_mfi_pthru; 3664 3665 mpi25_ieee_chain->Address = cpu_to_le64(mfi_cmd->frame_phys_addr); 3666 3667 mpi25_ieee_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT | 3668 MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR; 3669 3670 mpi25_ieee_chain->Length = cpu_to_le32(instance->mfi_frame_size); 3671 } 3672 3673 /** 3674 * build_mpt_cmd - Calls helper function to build a cmd MFI Pass thru cmd 3675 * @instance: Adapter soft state 3676 * @cmd: mfi cmd to build 3677 * 3678 */ 3679 union MEGASAS_REQUEST_DESCRIPTOR_UNION * 3680 build_mpt_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd) 3681 { 3682 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc = NULL; 3683 u16 index; 3684 3685 build_mpt_mfi_pass_thru(instance, cmd); 3686 index = cmd->context.smid; 3687 3688 req_desc = megasas_get_request_descriptor(instance, index - 1); 3689 3690 req_desc->Words = 0; 3691 req_desc->SCSIIO.RequestFlags = (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO << 3692 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 3693 3694 req_desc->SCSIIO.SMID = cpu_to_le16(index); 3695 3696 return req_desc; 3697 } 3698 3699 /** 3700 * megasas_issue_dcmd_fusion - Issues a MFI Pass thru cmd 3701 * @instance: Adapter soft state 3702 * @cmd: mfi cmd pointer 3703 * 3704 */ 3705 void 3706 megasas_issue_dcmd_fusion(struct megasas_instance *instance, 3707 struct megasas_cmd *cmd) 3708 { 3709 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc; 3710 3711 req_desc = build_mpt_cmd(instance, cmd); 3712 3713 megasas_fire_cmd_fusion(instance, req_desc); 3714 return; 3715 } 3716 3717 /** 3718 * megasas_release_fusion - Reverses the FW initialization 3719 * @instance: Adapter soft state 3720 */ 3721 void 3722 megasas_release_fusion(struct megasas_instance *instance) 3723 { 3724 megasas_free_ioc_init_cmd(instance); 3725 megasas_free_cmds(instance); 3726 megasas_free_cmds_fusion(instance); 3727 3728 iounmap(instance->reg_set); 3729 3730 pci_release_selected_regions(instance->pdev, 1<<instance->bar); 3731 } 3732 3733 /** 3734 * megasas_read_fw_status_reg_fusion - returns the current FW status value 3735 * @regs: MFI register set 3736 */ 3737 static u32 3738 megasas_read_fw_status_reg_fusion(struct megasas_instance *instance) 3739 { 3740 return megasas_readl(instance, &instance->reg_set->outbound_scratch_pad_0); 3741 } 3742 3743 /** 3744 * megasas_alloc_host_crash_buffer - Host buffers for Crash dump collection from Firmware 3745 * @instance: Controller's soft instance 3746 * return: Number of allocated host crash buffers 3747 */ 3748 static void 3749 megasas_alloc_host_crash_buffer(struct megasas_instance *instance) 3750 { 3751 unsigned int i; 3752 3753 for (i = 0; i < MAX_CRASH_DUMP_SIZE; i++) { 3754 instance->crash_buf[i] = vzalloc(CRASH_DMA_BUF_SIZE); 3755 if (!instance->crash_buf[i]) { 3756 dev_info(&instance->pdev->dev, "Firmware crash dump " 3757 "memory allocation failed at index %d\n", i); 3758 break; 3759 } 3760 } 3761 instance->drv_buf_alloc = i; 3762 } 3763 3764 /** 3765 * megasas_free_host_crash_buffer - Host buffers for Crash dump collection from Firmware 3766 * @instance: Controller's soft instance 3767 */ 3768 void 3769 megasas_free_host_crash_buffer(struct megasas_instance *instance) 3770 { 3771 unsigned int i; 3772 for (i = 0; i < instance->drv_buf_alloc; i++) { 3773 if (instance->crash_buf[i]) 3774 vfree(instance->crash_buf[i]); 3775 } 3776 instance->drv_buf_index = 0; 3777 instance->drv_buf_alloc = 0; 3778 instance->fw_crash_state = UNAVAILABLE; 3779 instance->fw_crash_buffer_size = 0; 3780 } 3781 3782 /** 3783 * megasas_adp_reset_fusion - For controller reset 3784 * @regs: MFI register set 3785 */ 3786 static int 3787 megasas_adp_reset_fusion(struct megasas_instance *instance, 3788 struct megasas_register_set __iomem *regs) 3789 { 3790 u32 host_diag, abs_state, retry; 3791 3792 /* Now try to reset the chip */ 3793 writel(MPI2_WRSEQ_FLUSH_KEY_VALUE, &instance->reg_set->fusion_seq_offset); 3794 writel(MPI2_WRSEQ_1ST_KEY_VALUE, &instance->reg_set->fusion_seq_offset); 3795 writel(MPI2_WRSEQ_2ND_KEY_VALUE, &instance->reg_set->fusion_seq_offset); 3796 writel(MPI2_WRSEQ_3RD_KEY_VALUE, &instance->reg_set->fusion_seq_offset); 3797 writel(MPI2_WRSEQ_4TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset); 3798 writel(MPI2_WRSEQ_5TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset); 3799 writel(MPI2_WRSEQ_6TH_KEY_VALUE, &instance->reg_set->fusion_seq_offset); 3800 3801 /* Check that the diag write enable (DRWE) bit is on */ 3802 host_diag = megasas_readl(instance, &instance->reg_set->fusion_host_diag); 3803 retry = 0; 3804 while (!(host_diag & HOST_DIAG_WRITE_ENABLE)) { 3805 msleep(100); 3806 host_diag = megasas_readl(instance, 3807 &instance->reg_set->fusion_host_diag); 3808 if (retry++ == 100) { 3809 dev_warn(&instance->pdev->dev, 3810 "Host diag unlock failed from %s %d\n", 3811 __func__, __LINE__); 3812 break; 3813 } 3814 } 3815 if (!(host_diag & HOST_DIAG_WRITE_ENABLE)) 3816 return -1; 3817 3818 /* Send chip reset command */ 3819 writel(host_diag | HOST_DIAG_RESET_ADAPTER, 3820 &instance->reg_set->fusion_host_diag); 3821 msleep(3000); 3822 3823 /* Make sure reset adapter bit is cleared */ 3824 host_diag = megasas_readl(instance, &instance->reg_set->fusion_host_diag); 3825 retry = 0; 3826 while (host_diag & HOST_DIAG_RESET_ADAPTER) { 3827 msleep(100); 3828 host_diag = megasas_readl(instance, 3829 &instance->reg_set->fusion_host_diag); 3830 if (retry++ == 1000) { 3831 dev_warn(&instance->pdev->dev, 3832 "Diag reset adapter never cleared %s %d\n", 3833 __func__, __LINE__); 3834 break; 3835 } 3836 } 3837 if (host_diag & HOST_DIAG_RESET_ADAPTER) 3838 return -1; 3839 3840 abs_state = instance->instancet->read_fw_status_reg(instance) 3841 & MFI_STATE_MASK; 3842 retry = 0; 3843 3844 while ((abs_state <= MFI_STATE_FW_INIT) && (retry++ < 1000)) { 3845 msleep(100); 3846 abs_state = instance->instancet-> 3847 read_fw_status_reg(instance) & MFI_STATE_MASK; 3848 } 3849 if (abs_state <= MFI_STATE_FW_INIT) { 3850 dev_warn(&instance->pdev->dev, 3851 "fw state < MFI_STATE_FW_INIT, state = 0x%x %s %d\n", 3852 abs_state, __func__, __LINE__); 3853 return -1; 3854 } 3855 3856 return 0; 3857 } 3858 3859 /** 3860 * megasas_check_reset_fusion - For controller reset check 3861 * @regs: MFI register set 3862 */ 3863 static int 3864 megasas_check_reset_fusion(struct megasas_instance *instance, 3865 struct megasas_register_set __iomem *regs) 3866 { 3867 return 0; 3868 } 3869 3870 /** 3871 * megasas_trigger_snap_dump - Trigger snap dump in FW 3872 * @instance: Soft instance of adapter 3873 */ 3874 static inline void megasas_trigger_snap_dump(struct megasas_instance *instance) 3875 { 3876 int j; 3877 u32 fw_state; 3878 3879 if (!instance->disableOnlineCtrlReset) { 3880 dev_info(&instance->pdev->dev, "Trigger snap dump\n"); 3881 writel(MFI_ADP_TRIGGER_SNAP_DUMP, 3882 &instance->reg_set->doorbell); 3883 readl(&instance->reg_set->doorbell); 3884 } 3885 3886 for (j = 0; j < instance->snapdump_wait_time; j++) { 3887 fw_state = instance->instancet->read_fw_status_reg(instance) & 3888 MFI_STATE_MASK; 3889 if (fw_state == MFI_STATE_FAULT) { 3890 dev_err(&instance->pdev->dev, 3891 "Found FW in FAULT state, after snap dump trigger\n"); 3892 return; 3893 } 3894 msleep(1000); 3895 } 3896 } 3897 3898 /* This function waits for outstanding commands on fusion to complete */ 3899 int megasas_wait_for_outstanding_fusion(struct megasas_instance *instance, 3900 int reason, int *convert) 3901 { 3902 int i, outstanding, retval = 0, hb_seconds_missed = 0; 3903 u32 fw_state; 3904 u32 waittime_for_io_completion; 3905 3906 waittime_for_io_completion = 3907 min_t(u32, resetwaittime, 3908 (resetwaittime - instance->snapdump_wait_time)); 3909 3910 if (reason == MFI_IO_TIMEOUT_OCR) { 3911 dev_info(&instance->pdev->dev, 3912 "MFI command is timed out\n"); 3913 megasas_complete_cmd_dpc_fusion((unsigned long)instance); 3914 if (instance->snapdump_wait_time) 3915 megasas_trigger_snap_dump(instance); 3916 retval = 1; 3917 goto out; 3918 } 3919 3920 for (i = 0; i < waittime_for_io_completion; i++) { 3921 /* Check if firmware is in fault state */ 3922 fw_state = instance->instancet->read_fw_status_reg(instance) & 3923 MFI_STATE_MASK; 3924 if (fw_state == MFI_STATE_FAULT) { 3925 dev_warn(&instance->pdev->dev, "Found FW in FAULT state," 3926 " will reset adapter scsi%d.\n", 3927 instance->host->host_no); 3928 megasas_complete_cmd_dpc_fusion((unsigned long)instance); 3929 if (instance->requestorId && reason) { 3930 dev_warn(&instance->pdev->dev, "SR-IOV Found FW in FAULT" 3931 " state while polling during" 3932 " I/O timeout handling for %d\n", 3933 instance->host->host_no); 3934 *convert = 1; 3935 } 3936 3937 retval = 1; 3938 goto out; 3939 } 3940 3941 3942 /* If SR-IOV VF mode & heartbeat timeout, don't wait */ 3943 if (instance->requestorId && !reason) { 3944 retval = 1; 3945 goto out; 3946 } 3947 3948 /* If SR-IOV VF mode & I/O timeout, check for HB timeout */ 3949 if (instance->requestorId && (reason == SCSIIO_TIMEOUT_OCR)) { 3950 if (instance->hb_host_mem->HB.fwCounter != 3951 instance->hb_host_mem->HB.driverCounter) { 3952 instance->hb_host_mem->HB.driverCounter = 3953 instance->hb_host_mem->HB.fwCounter; 3954 hb_seconds_missed = 0; 3955 } else { 3956 hb_seconds_missed++; 3957 if (hb_seconds_missed == 3958 (MEGASAS_SRIOV_HEARTBEAT_INTERVAL_VF/HZ)) { 3959 dev_warn(&instance->pdev->dev, "SR-IOV:" 3960 " Heartbeat never completed " 3961 " while polling during I/O " 3962 " timeout handling for " 3963 "scsi%d.\n", 3964 instance->host->host_no); 3965 *convert = 1; 3966 retval = 1; 3967 goto out; 3968 } 3969 } 3970 } 3971 3972 megasas_complete_cmd_dpc_fusion((unsigned long)instance); 3973 outstanding = atomic_read(&instance->fw_outstanding); 3974 if (!outstanding) 3975 goto out; 3976 3977 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) { 3978 dev_notice(&instance->pdev->dev, "[%2d]waiting for %d " 3979 "commands to complete for scsi%d\n", i, 3980 outstanding, instance->host->host_no); 3981 } 3982 msleep(1000); 3983 } 3984 3985 if (instance->snapdump_wait_time) { 3986 megasas_trigger_snap_dump(instance); 3987 retval = 1; 3988 goto out; 3989 } 3990 3991 if (atomic_read(&instance->fw_outstanding)) { 3992 dev_err(&instance->pdev->dev, "pending commands remain after waiting, " 3993 "will reset adapter scsi%d.\n", 3994 instance->host->host_no); 3995 *convert = 1; 3996 retval = 1; 3997 } 3998 3999 out: 4000 return retval; 4001 } 4002 4003 void megasas_reset_reply_desc(struct megasas_instance *instance) 4004 { 4005 int i, j, count; 4006 struct fusion_context *fusion; 4007 union MPI2_REPLY_DESCRIPTORS_UNION *reply_desc; 4008 4009 fusion = instance->ctrl_context; 4010 count = instance->msix_vectors > 0 ? instance->msix_vectors : 1; 4011 for (i = 0 ; i < count ; i++) { 4012 fusion->last_reply_idx[i] = 0; 4013 reply_desc = fusion->reply_frames_desc[i]; 4014 for (j = 0 ; j < fusion->reply_q_depth; j++, reply_desc++) 4015 reply_desc->Words = cpu_to_le64(ULLONG_MAX); 4016 } 4017 } 4018 4019 /* 4020 * megasas_refire_mgmt_cmd : Re-fire management commands 4021 * @instance: Controller's soft instance 4022 */ 4023 void megasas_refire_mgmt_cmd(struct megasas_instance *instance) 4024 { 4025 int j; 4026 struct megasas_cmd_fusion *cmd_fusion; 4027 struct fusion_context *fusion; 4028 struct megasas_cmd *cmd_mfi; 4029 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc; 4030 u16 smid; 4031 bool refire_cmd = 0; 4032 u8 result; 4033 u32 opcode = 0; 4034 4035 fusion = instance->ctrl_context; 4036 4037 /* Re-fire management commands. 4038 * Do not traverse complet MPT frame pool. Start from max_scsi_cmds. 4039 */ 4040 for (j = instance->max_scsi_cmds ; j < instance->max_fw_cmds; j++) { 4041 cmd_fusion = fusion->cmd_list[j]; 4042 cmd_mfi = instance->cmd_list[cmd_fusion->sync_cmd_idx]; 4043 smid = le16_to_cpu(cmd_mfi->context.smid); 4044 result = REFIRE_CMD; 4045 4046 if (!smid) 4047 continue; 4048 4049 req_desc = megasas_get_request_descriptor(instance, smid - 1); 4050 4051 switch (cmd_mfi->frame->hdr.cmd) { 4052 case MFI_CMD_DCMD: 4053 opcode = le32_to_cpu(cmd_mfi->frame->dcmd.opcode); 4054 /* Do not refire shutdown command */ 4055 if (opcode == MR_DCMD_CTRL_SHUTDOWN) { 4056 cmd_mfi->frame->dcmd.cmd_status = MFI_STAT_OK; 4057 result = COMPLETE_CMD; 4058 break; 4059 } 4060 4061 refire_cmd = ((opcode != MR_DCMD_LD_MAP_GET_INFO)) && 4062 (opcode != MR_DCMD_SYSTEM_PD_MAP_GET_INFO) && 4063 !(cmd_mfi->flags & DRV_DCMD_SKIP_REFIRE); 4064 4065 if (!refire_cmd) 4066 result = RETURN_CMD; 4067 4068 break; 4069 case MFI_CMD_NVME: 4070 if (!instance->support_nvme_passthru) { 4071 cmd_mfi->frame->hdr.cmd_status = MFI_STAT_INVALID_CMD; 4072 result = COMPLETE_CMD; 4073 } 4074 4075 break; 4076 default: 4077 break; 4078 } 4079 4080 switch (result) { 4081 case REFIRE_CMD: 4082 megasas_fire_cmd_fusion(instance, req_desc); 4083 break; 4084 case RETURN_CMD: 4085 megasas_return_cmd(instance, cmd_mfi); 4086 break; 4087 case COMPLETE_CMD: 4088 megasas_complete_cmd(instance, cmd_mfi, DID_OK); 4089 break; 4090 } 4091 } 4092 } 4093 4094 /* 4095 * megasas_track_scsiio : Track SCSI IOs outstanding to a SCSI device 4096 * @instance: per adapter struct 4097 * @channel: the channel assigned by the OS 4098 * @id: the id assigned by the OS 4099 * 4100 * Returns SUCCESS if no IOs pending to SCSI device, else return FAILED 4101 */ 4102 4103 static int megasas_track_scsiio(struct megasas_instance *instance, 4104 int id, int channel) 4105 { 4106 int i, found = 0; 4107 struct megasas_cmd_fusion *cmd_fusion; 4108 struct fusion_context *fusion; 4109 fusion = instance->ctrl_context; 4110 4111 for (i = 0 ; i < instance->max_scsi_cmds; i++) { 4112 cmd_fusion = fusion->cmd_list[i]; 4113 if (cmd_fusion->scmd && 4114 (cmd_fusion->scmd->device->id == id && 4115 cmd_fusion->scmd->device->channel == channel)) { 4116 dev_info(&instance->pdev->dev, 4117 "SCSI commands pending to target" 4118 "channel %d id %d \tSMID: 0x%x\n", 4119 channel, id, cmd_fusion->index); 4120 scsi_print_command(cmd_fusion->scmd); 4121 found = 1; 4122 break; 4123 } 4124 } 4125 4126 return found ? FAILED : SUCCESS; 4127 } 4128 4129 /** 4130 * megasas_tm_response_code - translation of device response code 4131 * @ioc: per adapter object 4132 * @mpi_reply: MPI reply returned by firmware 4133 * 4134 * Return nothing. 4135 */ 4136 static void 4137 megasas_tm_response_code(struct megasas_instance *instance, 4138 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply) 4139 { 4140 char *desc; 4141 4142 switch (mpi_reply->ResponseCode) { 4143 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE: 4144 desc = "task management request completed"; 4145 break; 4146 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME: 4147 desc = "invalid frame"; 4148 break; 4149 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED: 4150 desc = "task management request not supported"; 4151 break; 4152 case MPI2_SCSITASKMGMT_RSP_TM_FAILED: 4153 desc = "task management request failed"; 4154 break; 4155 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED: 4156 desc = "task management request succeeded"; 4157 break; 4158 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN: 4159 desc = "invalid lun"; 4160 break; 4161 case 0xA: 4162 desc = "overlapped tag attempted"; 4163 break; 4164 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC: 4165 desc = "task queued, however not sent to target"; 4166 break; 4167 default: 4168 desc = "unknown"; 4169 break; 4170 } 4171 dev_dbg(&instance->pdev->dev, "response_code(%01x): %s\n", 4172 mpi_reply->ResponseCode, desc); 4173 dev_dbg(&instance->pdev->dev, 4174 "TerminationCount/DevHandle/Function/TaskType/IOCStat/IOCLoginfo" 4175 " 0x%x/0x%x/0x%x/0x%x/0x%x/0x%x\n", 4176 mpi_reply->TerminationCount, mpi_reply->DevHandle, 4177 mpi_reply->Function, mpi_reply->TaskType, 4178 mpi_reply->IOCStatus, mpi_reply->IOCLogInfo); 4179 } 4180 4181 /** 4182 * megasas_issue_tm - main routine for sending tm requests 4183 * @instance: per adapter struct 4184 * @device_handle: device handle 4185 * @channel: the channel assigned by the OS 4186 * @id: the id assigned by the OS 4187 * @type: MPI2_SCSITASKMGMT_TASKTYPE__XXX (defined in megaraid_sas_fusion.c) 4188 * @smid_task: smid assigned to the task 4189 * @m_type: TM_MUTEX_ON or TM_MUTEX_OFF 4190 * Context: user 4191 * 4192 * MegaRaid use MPT interface for Task Magement request. 4193 * A generic API for sending task management requests to firmware. 4194 * 4195 * Return SUCCESS or FAILED. 4196 */ 4197 static int 4198 megasas_issue_tm(struct megasas_instance *instance, u16 device_handle, 4199 uint channel, uint id, u16 smid_task, u8 type, 4200 struct MR_PRIV_DEVICE *mr_device_priv_data) 4201 { 4202 struct MR_TASK_MANAGE_REQUEST *mr_request; 4203 struct MPI2_SCSI_TASK_MANAGE_REQUEST *mpi_request; 4204 unsigned long timeleft; 4205 struct megasas_cmd_fusion *cmd_fusion; 4206 struct megasas_cmd *cmd_mfi; 4207 union MEGASAS_REQUEST_DESCRIPTOR_UNION *req_desc; 4208 struct fusion_context *fusion = NULL; 4209 struct megasas_cmd_fusion *scsi_lookup; 4210 int rc; 4211 int timeout = MEGASAS_DEFAULT_TM_TIMEOUT; 4212 struct MPI2_SCSI_TASK_MANAGE_REPLY *mpi_reply; 4213 4214 fusion = instance->ctrl_context; 4215 4216 cmd_mfi = megasas_get_cmd(instance); 4217 4218 if (!cmd_mfi) { 4219 dev_err(&instance->pdev->dev, "Failed from %s %d\n", 4220 __func__, __LINE__); 4221 return -ENOMEM; 4222 } 4223 4224 cmd_fusion = megasas_get_cmd_fusion(instance, 4225 instance->max_scsi_cmds + cmd_mfi->index); 4226 4227 /* Save the smid. To be used for returning the cmd */ 4228 cmd_mfi->context.smid = cmd_fusion->index; 4229 4230 req_desc = megasas_get_request_descriptor(instance, 4231 (cmd_fusion->index - 1)); 4232 4233 cmd_fusion->request_desc = req_desc; 4234 req_desc->Words = 0; 4235 4236 mr_request = (struct MR_TASK_MANAGE_REQUEST *) cmd_fusion->io_request; 4237 memset(mr_request, 0, sizeof(struct MR_TASK_MANAGE_REQUEST)); 4238 mpi_request = (struct MPI2_SCSI_TASK_MANAGE_REQUEST *) &mr_request->TmRequest; 4239 mpi_request->Function = MPI2_FUNCTION_SCSI_TASK_MGMT; 4240 mpi_request->DevHandle = cpu_to_le16(device_handle); 4241 mpi_request->TaskType = type; 4242 mpi_request->TaskMID = cpu_to_le16(smid_task); 4243 mpi_request->LUN[1] = 0; 4244 4245 4246 req_desc = cmd_fusion->request_desc; 4247 req_desc->HighPriority.SMID = cpu_to_le16(cmd_fusion->index); 4248 req_desc->HighPriority.RequestFlags = 4249 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY << 4250 MEGASAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT); 4251 req_desc->HighPriority.MSIxIndex = 0; 4252 req_desc->HighPriority.LMID = 0; 4253 req_desc->HighPriority.Reserved1 = 0; 4254 4255 if (channel < MEGASAS_MAX_PD_CHANNELS) 4256 mr_request->tmReqFlags.isTMForPD = 1; 4257 else 4258 mr_request->tmReqFlags.isTMForLD = 1; 4259 4260 init_completion(&cmd_fusion->done); 4261 megasas_fire_cmd_fusion(instance, req_desc); 4262 4263 switch (type) { 4264 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK: 4265 timeout = mr_device_priv_data->task_abort_tmo; 4266 break; 4267 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET: 4268 timeout = mr_device_priv_data->target_reset_tmo; 4269 break; 4270 } 4271 4272 timeleft = wait_for_completion_timeout(&cmd_fusion->done, timeout * HZ); 4273 4274 if (!timeleft) { 4275 dev_err(&instance->pdev->dev, 4276 "task mgmt type 0x%x timed out\n", type); 4277 cmd_mfi->flags |= DRV_DCMD_SKIP_REFIRE; 4278 mutex_unlock(&instance->reset_mutex); 4279 rc = megasas_reset_fusion(instance->host, MFI_IO_TIMEOUT_OCR); 4280 mutex_lock(&instance->reset_mutex); 4281 return rc; 4282 } 4283 4284 mpi_reply = (struct MPI2_SCSI_TASK_MANAGE_REPLY *) &mr_request->TMReply; 4285 megasas_tm_response_code(instance, mpi_reply); 4286 4287 megasas_return_cmd(instance, cmd_mfi); 4288 rc = SUCCESS; 4289 switch (type) { 4290 case MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK: 4291 scsi_lookup = fusion->cmd_list[smid_task - 1]; 4292 4293 if (scsi_lookup->scmd == NULL) 4294 break; 4295 else { 4296 instance->instancet->disable_intr(instance); 4297 megasas_sync_irqs((unsigned long)instance); 4298 instance->instancet->enable_intr(instance); 4299 if (scsi_lookup->scmd == NULL) 4300 break; 4301 } 4302 rc = FAILED; 4303 break; 4304 4305 case MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET: 4306 if ((channel == 0xFFFFFFFF) && (id == 0xFFFFFFFF)) 4307 break; 4308 instance->instancet->disable_intr(instance); 4309 megasas_sync_irqs((unsigned long)instance); 4310 rc = megasas_track_scsiio(instance, id, channel); 4311 instance->instancet->enable_intr(instance); 4312 4313 break; 4314 case MPI2_SCSITASKMGMT_TASKTYPE_ABRT_TASK_SET: 4315 case MPI2_SCSITASKMGMT_TASKTYPE_QUERY_TASK: 4316 break; 4317 default: 4318 rc = FAILED; 4319 break; 4320 } 4321 4322 return rc; 4323 4324 } 4325 4326 /* 4327 * megasas_fusion_smid_lookup : Look for fusion command correpspodning to SCSI 4328 * @instance: per adapter struct 4329 * 4330 * Return Non Zero index, if SMID found in outstanding commands 4331 */ 4332 static u16 megasas_fusion_smid_lookup(struct scsi_cmnd *scmd) 4333 { 4334 int i, ret = 0; 4335 struct megasas_instance *instance; 4336 struct megasas_cmd_fusion *cmd_fusion; 4337 struct fusion_context *fusion; 4338 4339 instance = (struct megasas_instance *)scmd->device->host->hostdata; 4340 4341 fusion = instance->ctrl_context; 4342 4343 for (i = 0; i < instance->max_scsi_cmds; i++) { 4344 cmd_fusion = fusion->cmd_list[i]; 4345 if (cmd_fusion->scmd && (cmd_fusion->scmd == scmd)) { 4346 scmd_printk(KERN_NOTICE, scmd, "Abort request is for" 4347 " SMID: %d\n", cmd_fusion->index); 4348 ret = cmd_fusion->index; 4349 break; 4350 } 4351 } 4352 4353 return ret; 4354 } 4355 4356 /* 4357 * megasas_get_tm_devhandle - Get devhandle for TM request 4358 * @sdev- OS provided scsi device 4359 * 4360 * Returns- devhandle/targetID of SCSI device 4361 */ 4362 static u16 megasas_get_tm_devhandle(struct scsi_device *sdev) 4363 { 4364 u16 pd_index = 0; 4365 u32 device_id; 4366 struct megasas_instance *instance; 4367 struct fusion_context *fusion; 4368 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync; 4369 u16 devhandle = (u16)ULONG_MAX; 4370 4371 instance = (struct megasas_instance *)sdev->host->hostdata; 4372 fusion = instance->ctrl_context; 4373 4374 if (!MEGASAS_IS_LOGICAL(sdev)) { 4375 if (instance->use_seqnum_jbod_fp) { 4376 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) 4377 + sdev->id; 4378 pd_sync = (void *)fusion->pd_seq_sync 4379 [(instance->pd_seq_map_id - 1) & 1]; 4380 devhandle = pd_sync->seq[pd_index].devHandle; 4381 } else 4382 sdev_printk(KERN_ERR, sdev, "Firmware expose tmCapable" 4383 " without JBOD MAP support from %s %d\n", __func__, __LINE__); 4384 } else { 4385 device_id = ((sdev->channel % 2) * MEGASAS_MAX_DEV_PER_CHANNEL) 4386 + sdev->id; 4387 devhandle = device_id; 4388 } 4389 4390 return devhandle; 4391 } 4392 4393 /* 4394 * megasas_task_abort_fusion : SCSI task abort function for fusion adapters 4395 * @scmd : pointer to scsi command object 4396 * 4397 * Return SUCCESS, if command aborted else FAILED 4398 */ 4399 4400 int megasas_task_abort_fusion(struct scsi_cmnd *scmd) 4401 { 4402 struct megasas_instance *instance; 4403 u16 smid, devhandle; 4404 struct fusion_context *fusion; 4405 int ret; 4406 struct MR_PRIV_DEVICE *mr_device_priv_data; 4407 mr_device_priv_data = scmd->device->hostdata; 4408 4409 4410 instance = (struct megasas_instance *)scmd->device->host->hostdata; 4411 fusion = instance->ctrl_context; 4412 4413 scmd_printk(KERN_INFO, scmd, "task abort called for scmd(%p)\n", scmd); 4414 scsi_print_command(scmd); 4415 4416 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) { 4417 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL," 4418 "SCSI host:%d\n", instance->host->host_no); 4419 ret = FAILED; 4420 return ret; 4421 } 4422 4423 if (!mr_device_priv_data) { 4424 sdev_printk(KERN_INFO, scmd->device, "device been deleted! " 4425 "scmd(%p)\n", scmd); 4426 scmd->result = DID_NO_CONNECT << 16; 4427 ret = SUCCESS; 4428 goto out; 4429 } 4430 4431 4432 if (!mr_device_priv_data->is_tm_capable) { 4433 ret = FAILED; 4434 goto out; 4435 } 4436 4437 mutex_lock(&instance->reset_mutex); 4438 4439 smid = megasas_fusion_smid_lookup(scmd); 4440 4441 if (!smid) { 4442 ret = SUCCESS; 4443 scmd_printk(KERN_NOTICE, scmd, "Command for which abort is" 4444 " issued is not found in oustanding commands\n"); 4445 mutex_unlock(&instance->reset_mutex); 4446 goto out; 4447 } 4448 4449 devhandle = megasas_get_tm_devhandle(scmd->device); 4450 4451 if (devhandle == (u16)ULONG_MAX) { 4452 ret = SUCCESS; 4453 sdev_printk(KERN_INFO, scmd->device, 4454 "task abort issued for invalid devhandle\n"); 4455 mutex_unlock(&instance->reset_mutex); 4456 goto out; 4457 } 4458 sdev_printk(KERN_INFO, scmd->device, 4459 "attempting task abort! scmd(%p) tm_dev_handle 0x%x\n", 4460 scmd, devhandle); 4461 4462 mr_device_priv_data->tm_busy = 1; 4463 ret = megasas_issue_tm(instance, devhandle, 4464 scmd->device->channel, scmd->device->id, smid, 4465 MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK, 4466 mr_device_priv_data); 4467 mr_device_priv_data->tm_busy = 0; 4468 4469 mutex_unlock(&instance->reset_mutex); 4470 out: 4471 sdev_printk(KERN_INFO, scmd->device, "task abort: %s scmd(%p)\n", 4472 ((ret == SUCCESS) ? "SUCCESS" : "FAILED"), scmd); 4473 4474 return ret; 4475 } 4476 4477 /* 4478 * megasas_reset_target_fusion : target reset function for fusion adapters 4479 * scmd: SCSI command pointer 4480 * 4481 * Returns SUCCESS if all commands associated with target aborted else FAILED 4482 */ 4483 4484 int megasas_reset_target_fusion(struct scsi_cmnd *scmd) 4485 { 4486 4487 struct megasas_instance *instance; 4488 int ret = FAILED; 4489 u16 devhandle; 4490 struct fusion_context *fusion; 4491 struct MR_PRIV_DEVICE *mr_device_priv_data; 4492 mr_device_priv_data = scmd->device->hostdata; 4493 4494 instance = (struct megasas_instance *)scmd->device->host->hostdata; 4495 fusion = instance->ctrl_context; 4496 4497 sdev_printk(KERN_INFO, scmd->device, 4498 "target reset called for scmd(%p)\n", scmd); 4499 4500 if (atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) { 4501 dev_err(&instance->pdev->dev, "Controller is not OPERATIONAL," 4502 "SCSI host:%d\n", instance->host->host_no); 4503 ret = FAILED; 4504 return ret; 4505 } 4506 4507 if (!mr_device_priv_data) { 4508 sdev_printk(KERN_INFO, scmd->device, "device been deleted! " 4509 "scmd(%p)\n", scmd); 4510 scmd->result = DID_NO_CONNECT << 16; 4511 ret = SUCCESS; 4512 goto out; 4513 } 4514 4515 4516 if (!mr_device_priv_data->is_tm_capable) { 4517 ret = FAILED; 4518 goto out; 4519 } 4520 4521 mutex_lock(&instance->reset_mutex); 4522 devhandle = megasas_get_tm_devhandle(scmd->device); 4523 4524 if (devhandle == (u16)ULONG_MAX) { 4525 ret = SUCCESS; 4526 sdev_printk(KERN_INFO, scmd->device, 4527 "target reset issued for invalid devhandle\n"); 4528 mutex_unlock(&instance->reset_mutex); 4529 goto out; 4530 } 4531 4532 sdev_printk(KERN_INFO, scmd->device, 4533 "attempting target reset! scmd(%p) tm_dev_handle 0x%x\n", 4534 scmd, devhandle); 4535 mr_device_priv_data->tm_busy = 1; 4536 ret = megasas_issue_tm(instance, devhandle, 4537 scmd->device->channel, scmd->device->id, 0, 4538 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET, 4539 mr_device_priv_data); 4540 mr_device_priv_data->tm_busy = 0; 4541 mutex_unlock(&instance->reset_mutex); 4542 out: 4543 scmd_printk(KERN_NOTICE, scmd, "megasas: target reset %s!!\n", 4544 (ret == SUCCESS) ? "SUCCESS" : "FAILED"); 4545 4546 return ret; 4547 } 4548 4549 /*SRIOV get other instance in cluster if any*/ 4550 struct megasas_instance *megasas_get_peer_instance(struct megasas_instance *instance) 4551 { 4552 int i; 4553 4554 for (i = 0; i < MAX_MGMT_ADAPTERS; i++) { 4555 if (megasas_mgmt_info.instance[i] && 4556 (megasas_mgmt_info.instance[i] != instance) && 4557 megasas_mgmt_info.instance[i]->requestorId && 4558 megasas_mgmt_info.instance[i]->peerIsPresent && 4559 (memcmp((megasas_mgmt_info.instance[i]->clusterId), 4560 instance->clusterId, MEGASAS_CLUSTER_ID_SIZE) == 0)) 4561 return megasas_mgmt_info.instance[i]; 4562 } 4563 return NULL; 4564 } 4565 4566 /* Check for a second path that is currently UP */ 4567 int megasas_check_mpio_paths(struct megasas_instance *instance, 4568 struct scsi_cmnd *scmd) 4569 { 4570 struct megasas_instance *peer_instance = NULL; 4571 int retval = (DID_REQUEUE << 16); 4572 4573 if (instance->peerIsPresent) { 4574 peer_instance = megasas_get_peer_instance(instance); 4575 if ((peer_instance) && 4576 (atomic_read(&peer_instance->adprecovery) == 4577 MEGASAS_HBA_OPERATIONAL)) 4578 retval = (DID_NO_CONNECT << 16); 4579 } 4580 return retval; 4581 } 4582 4583 /* Core fusion reset function */ 4584 int megasas_reset_fusion(struct Scsi_Host *shost, int reason) 4585 { 4586 int retval = SUCCESS, i, j, convert = 0; 4587 struct megasas_instance *instance; 4588 struct megasas_cmd_fusion *cmd_fusion, *r1_cmd; 4589 struct fusion_context *fusion; 4590 u32 abs_state, status_reg, reset_adapter; 4591 u32 io_timeout_in_crash_mode = 0; 4592 struct scsi_cmnd *scmd_local = NULL; 4593 struct scsi_device *sdev; 4594 int ret_target_prop = DCMD_FAILED; 4595 bool is_target_prop = false; 4596 4597 instance = (struct megasas_instance *)shost->hostdata; 4598 fusion = instance->ctrl_context; 4599 4600 mutex_lock(&instance->reset_mutex); 4601 4602 if (atomic_read(&instance->adprecovery) == MEGASAS_HW_CRITICAL_ERROR) { 4603 dev_warn(&instance->pdev->dev, "Hardware critical error, " 4604 "returning FAILED for scsi%d.\n", 4605 instance->host->host_no); 4606 mutex_unlock(&instance->reset_mutex); 4607 return FAILED; 4608 } 4609 status_reg = instance->instancet->read_fw_status_reg(instance); 4610 abs_state = status_reg & MFI_STATE_MASK; 4611 4612 /* IO timeout detected, forcibly put FW in FAULT state */ 4613 if (abs_state != MFI_STATE_FAULT && instance->crash_dump_buf && 4614 instance->crash_dump_app_support && reason) { 4615 dev_info(&instance->pdev->dev, "IO/DCMD timeout is detected, " 4616 "forcibly FAULT Firmware\n"); 4617 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT); 4618 status_reg = megasas_readl(instance, &instance->reg_set->doorbell); 4619 writel(status_reg | MFI_STATE_FORCE_OCR, 4620 &instance->reg_set->doorbell); 4621 readl(&instance->reg_set->doorbell); 4622 mutex_unlock(&instance->reset_mutex); 4623 do { 4624 ssleep(3); 4625 io_timeout_in_crash_mode++; 4626 dev_dbg(&instance->pdev->dev, "waiting for [%d] " 4627 "seconds for crash dump collection and OCR " 4628 "to be done\n", (io_timeout_in_crash_mode * 3)); 4629 } while ((atomic_read(&instance->adprecovery) != MEGASAS_HBA_OPERATIONAL) && 4630 (io_timeout_in_crash_mode < 80)); 4631 4632 if (atomic_read(&instance->adprecovery) == MEGASAS_HBA_OPERATIONAL) { 4633 dev_info(&instance->pdev->dev, "OCR done for IO " 4634 "timeout case\n"); 4635 retval = SUCCESS; 4636 } else { 4637 dev_info(&instance->pdev->dev, "Controller is not " 4638 "operational after 240 seconds wait for IO " 4639 "timeout case in FW crash dump mode\n do " 4640 "OCR/kill adapter\n"); 4641 retval = megasas_reset_fusion(shost, 0); 4642 } 4643 return retval; 4644 } 4645 4646 if (instance->requestorId && !instance->skip_heartbeat_timer_del) 4647 del_timer_sync(&instance->sriov_heartbeat_timer); 4648 set_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags); 4649 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_POLLING); 4650 instance->instancet->disable_intr(instance); 4651 megasas_sync_irqs((unsigned long)instance); 4652 4653 /* First try waiting for commands to complete */ 4654 if (megasas_wait_for_outstanding_fusion(instance, reason, 4655 &convert)) { 4656 atomic_set(&instance->adprecovery, MEGASAS_ADPRESET_SM_INFAULT); 4657 dev_warn(&instance->pdev->dev, "resetting fusion " 4658 "adapter scsi%d.\n", instance->host->host_no); 4659 if (convert) 4660 reason = 0; 4661 4662 if (megasas_dbg_lvl & OCR_LOGS) 4663 dev_info(&instance->pdev->dev, "\nPending SCSI commands:\n"); 4664 4665 /* Now return commands back to the OS */ 4666 for (i = 0 ; i < instance->max_scsi_cmds; i++) { 4667 cmd_fusion = fusion->cmd_list[i]; 4668 /*check for extra commands issued by driver*/ 4669 if (instance->adapter_type >= VENTURA_SERIES) { 4670 r1_cmd = fusion->cmd_list[i + instance->max_fw_cmds]; 4671 megasas_return_cmd_fusion(instance, r1_cmd); 4672 } 4673 scmd_local = cmd_fusion->scmd; 4674 if (cmd_fusion->scmd) { 4675 if (megasas_dbg_lvl & OCR_LOGS) { 4676 sdev_printk(KERN_INFO, 4677 cmd_fusion->scmd->device, "SMID: 0x%x\n", 4678 cmd_fusion->index); 4679 scsi_print_command(cmd_fusion->scmd); 4680 } 4681 4682 scmd_local->result = 4683 megasas_check_mpio_paths(instance, 4684 scmd_local); 4685 if (instance->ldio_threshold && 4686 megasas_cmd_type(scmd_local) == READ_WRITE_LDIO) 4687 atomic_dec(&instance->ldio_outstanding); 4688 megasas_return_cmd_fusion(instance, cmd_fusion); 4689 scsi_dma_unmap(scmd_local); 4690 scmd_local->scsi_done(scmd_local); 4691 } 4692 } 4693 4694 atomic_set(&instance->fw_outstanding, 0); 4695 4696 status_reg = instance->instancet->read_fw_status_reg(instance); 4697 abs_state = status_reg & MFI_STATE_MASK; 4698 reset_adapter = status_reg & MFI_RESET_ADAPTER; 4699 if (instance->disableOnlineCtrlReset || 4700 (abs_state == MFI_STATE_FAULT && !reset_adapter)) { 4701 /* Reset not supported, kill adapter */ 4702 dev_warn(&instance->pdev->dev, "Reset not supported" 4703 ", killing adapter scsi%d.\n", 4704 instance->host->host_no); 4705 megaraid_sas_kill_hba(instance); 4706 instance->skip_heartbeat_timer_del = 1; 4707 retval = FAILED; 4708 goto out; 4709 } 4710 4711 /* Let SR-IOV VF & PF sync up if there was a HB failure */ 4712 if (instance->requestorId && !reason) { 4713 msleep(MEGASAS_OCR_SETTLE_TIME_VF); 4714 goto transition_to_ready; 4715 } 4716 4717 /* Now try to reset the chip */ 4718 for (i = 0; i < MEGASAS_FUSION_MAX_RESET_TRIES; i++) { 4719 4720 if (instance->instancet->adp_reset 4721 (instance, instance->reg_set)) 4722 continue; 4723 transition_to_ready: 4724 /* Wait for FW to become ready */ 4725 if (megasas_transition_to_ready(instance, 1)) { 4726 dev_warn(&instance->pdev->dev, 4727 "Failed to transition controller to ready for " 4728 "scsi%d.\n", instance->host->host_no); 4729 if (instance->requestorId && !reason) 4730 goto fail_kill_adapter; 4731 else 4732 continue; 4733 } 4734 megasas_reset_reply_desc(instance); 4735 megasas_fusion_update_can_queue(instance, OCR_CONTEXT); 4736 4737 if (megasas_ioc_init_fusion(instance)) { 4738 if (instance->requestorId && !reason) 4739 goto fail_kill_adapter; 4740 else 4741 continue; 4742 } 4743 4744 if (megasas_get_ctrl_info(instance)) { 4745 dev_info(&instance->pdev->dev, 4746 "Failed from %s %d\n", 4747 __func__, __LINE__); 4748 megaraid_sas_kill_hba(instance); 4749 retval = FAILED; 4750 goto out; 4751 } 4752 4753 megasas_refire_mgmt_cmd(instance); 4754 4755 /* Reset load balance info */ 4756 if (fusion->load_balance_info) 4757 memset(fusion->load_balance_info, 0, 4758 (sizeof(struct LD_LOAD_BALANCE_INFO) * 4759 MAX_LOGICAL_DRIVES_EXT)); 4760 4761 if (!megasas_get_map_info(instance)) 4762 megasas_sync_map_info(instance); 4763 4764 megasas_setup_jbod_map(instance); 4765 4766 /* reset stream detection array */ 4767 if (instance->adapter_type >= VENTURA_SERIES) { 4768 for (j = 0; j < MAX_LOGICAL_DRIVES_EXT; ++j) { 4769 memset(fusion->stream_detect_by_ld[j], 4770 0, sizeof(struct LD_STREAM_DETECT)); 4771 fusion->stream_detect_by_ld[j]->mru_bit_map 4772 = MR_STREAM_BITMAP; 4773 } 4774 } 4775 4776 clear_bit(MEGASAS_FUSION_IN_RESET, 4777 &instance->reset_flags); 4778 instance->instancet->enable_intr(instance); 4779 4780 shost_for_each_device(sdev, shost) { 4781 if ((instance->tgt_prop) && 4782 (instance->nvme_page_size)) 4783 ret_target_prop = megasas_get_target_prop(instance, sdev); 4784 4785 is_target_prop = (ret_target_prop == DCMD_SUCCESS) ? true : false; 4786 megasas_set_dynamic_target_properties(sdev, is_target_prop); 4787 } 4788 4789 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL); 4790 4791 dev_info(&instance->pdev->dev, "Interrupts are enabled and" 4792 " controller is OPERATIONAL for scsi:%d\n", 4793 instance->host->host_no); 4794 4795 /* Restart SR-IOV heartbeat */ 4796 if (instance->requestorId) { 4797 if (!megasas_sriov_start_heartbeat(instance, 0)) 4798 megasas_start_timer(instance); 4799 else 4800 instance->skip_heartbeat_timer_del = 1; 4801 } 4802 4803 if (instance->crash_dump_drv_support && 4804 instance->crash_dump_app_support) 4805 megasas_set_crash_dump_params(instance, 4806 MR_CRASH_BUF_TURN_ON); 4807 else 4808 megasas_set_crash_dump_params(instance, 4809 MR_CRASH_BUF_TURN_OFF); 4810 4811 if (instance->snapdump_wait_time) { 4812 megasas_get_snapdump_properties(instance); 4813 dev_info(&instance->pdev->dev, 4814 "Snap dump wait time\t: %d\n", 4815 instance->snapdump_wait_time); 4816 } 4817 4818 retval = SUCCESS; 4819 4820 /* Adapter reset completed successfully */ 4821 dev_warn(&instance->pdev->dev, 4822 "Reset successful for scsi%d.\n", 4823 instance->host->host_no); 4824 4825 goto out; 4826 } 4827 fail_kill_adapter: 4828 /* Reset failed, kill the adapter */ 4829 dev_warn(&instance->pdev->dev, "Reset failed, killing " 4830 "adapter scsi%d.\n", instance->host->host_no); 4831 megaraid_sas_kill_hba(instance); 4832 instance->skip_heartbeat_timer_del = 1; 4833 retval = FAILED; 4834 } else { 4835 /* For VF: Restart HB timer if we didn't OCR */ 4836 if (instance->requestorId) { 4837 megasas_start_timer(instance); 4838 } 4839 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags); 4840 instance->instancet->enable_intr(instance); 4841 atomic_set(&instance->adprecovery, MEGASAS_HBA_OPERATIONAL); 4842 } 4843 out: 4844 clear_bit(MEGASAS_FUSION_IN_RESET, &instance->reset_flags); 4845 mutex_unlock(&instance->reset_mutex); 4846 return retval; 4847 } 4848 4849 /* Fusion Crash dump collection */ 4850 void megasas_fusion_crash_dump(struct megasas_instance *instance) 4851 { 4852 u32 status_reg; 4853 u8 partial_copy = 0; 4854 int wait = 0; 4855 4856 4857 status_reg = instance->instancet->read_fw_status_reg(instance); 4858 4859 /* 4860 * Allocate host crash buffers to copy data from 1 MB DMA crash buffer 4861 * to host crash buffers 4862 */ 4863 if (instance->drv_buf_index == 0) { 4864 /* Buffer is already allocated for old Crash dump. 4865 * Do OCR and do not wait for crash dump collection 4866 */ 4867 if (instance->drv_buf_alloc) { 4868 dev_info(&instance->pdev->dev, "earlier crash dump is " 4869 "not yet copied by application, ignoring this " 4870 "crash dump and initiating OCR\n"); 4871 status_reg |= MFI_STATE_CRASH_DUMP_DONE; 4872 writel(status_reg, 4873 &instance->reg_set->outbound_scratch_pad_0); 4874 readl(&instance->reg_set->outbound_scratch_pad_0); 4875 return; 4876 } 4877 megasas_alloc_host_crash_buffer(instance); 4878 dev_info(&instance->pdev->dev, "Number of host crash buffers " 4879 "allocated: %d\n", instance->drv_buf_alloc); 4880 } 4881 4882 while (!(status_reg & MFI_STATE_CRASH_DUMP_DONE) && 4883 (wait < MEGASAS_WATCHDOG_WAIT_COUNT)) { 4884 if (!(status_reg & MFI_STATE_DMADONE)) { 4885 /* 4886 * Next crash dump buffer is not yet DMA'd by FW 4887 * Check after 10ms. Wait for 1 second for FW to 4888 * post the next buffer. If not bail out. 4889 */ 4890 wait++; 4891 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS); 4892 status_reg = instance->instancet->read_fw_status_reg( 4893 instance); 4894 continue; 4895 } 4896 4897 wait = 0; 4898 if (instance->drv_buf_index >= instance->drv_buf_alloc) { 4899 dev_info(&instance->pdev->dev, 4900 "Driver is done copying the buffer: %d\n", 4901 instance->drv_buf_alloc); 4902 status_reg |= MFI_STATE_CRASH_DUMP_DONE; 4903 partial_copy = 1; 4904 break; 4905 } else { 4906 memcpy(instance->crash_buf[instance->drv_buf_index], 4907 instance->crash_dump_buf, CRASH_DMA_BUF_SIZE); 4908 instance->drv_buf_index++; 4909 status_reg &= ~MFI_STATE_DMADONE; 4910 } 4911 4912 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0); 4913 readl(&instance->reg_set->outbound_scratch_pad_0); 4914 4915 msleep(MEGASAS_WAIT_FOR_NEXT_DMA_MSECS); 4916 status_reg = instance->instancet->read_fw_status_reg(instance); 4917 } 4918 4919 if (status_reg & MFI_STATE_CRASH_DUMP_DONE) { 4920 dev_info(&instance->pdev->dev, "Crash Dump is available,number " 4921 "of copied buffers: %d\n", instance->drv_buf_index); 4922 instance->fw_crash_buffer_size = instance->drv_buf_index; 4923 instance->fw_crash_state = AVAILABLE; 4924 instance->drv_buf_index = 0; 4925 writel(status_reg, &instance->reg_set->outbound_scratch_pad_0); 4926 readl(&instance->reg_set->outbound_scratch_pad_0); 4927 if (!partial_copy) 4928 megasas_reset_fusion(instance->host, 0); 4929 } 4930 } 4931 4932 4933 /* Fusion OCR work queue */ 4934 void megasas_fusion_ocr_wq(struct work_struct *work) 4935 { 4936 struct megasas_instance *instance = 4937 container_of(work, struct megasas_instance, work_init); 4938 4939 megasas_reset_fusion(instance->host, 0); 4940 } 4941 4942 /* Allocate fusion context */ 4943 int 4944 megasas_alloc_fusion_context(struct megasas_instance *instance) 4945 { 4946 struct fusion_context *fusion; 4947 4948 instance->ctrl_context = kzalloc(sizeof(struct fusion_context), 4949 GFP_KERNEL); 4950 if (!instance->ctrl_context) { 4951 dev_err(&instance->pdev->dev, "Failed from %s %d\n", 4952 __func__, __LINE__); 4953 return -ENOMEM; 4954 } 4955 4956 fusion = instance->ctrl_context; 4957 4958 fusion->log_to_span_pages = get_order(MAX_LOGICAL_DRIVES_EXT * 4959 sizeof(LD_SPAN_INFO)); 4960 fusion->log_to_span = 4961 (PLD_SPAN_INFO)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 4962 fusion->log_to_span_pages); 4963 if (!fusion->log_to_span) { 4964 fusion->log_to_span = 4965 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT, 4966 sizeof(LD_SPAN_INFO))); 4967 if (!fusion->log_to_span) { 4968 dev_err(&instance->pdev->dev, "Failed from %s %d\n", 4969 __func__, __LINE__); 4970 return -ENOMEM; 4971 } 4972 } 4973 4974 fusion->load_balance_info_pages = get_order(MAX_LOGICAL_DRIVES_EXT * 4975 sizeof(struct LD_LOAD_BALANCE_INFO)); 4976 fusion->load_balance_info = 4977 (struct LD_LOAD_BALANCE_INFO *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 4978 fusion->load_balance_info_pages); 4979 if (!fusion->load_balance_info) { 4980 fusion->load_balance_info = 4981 vzalloc(array_size(MAX_LOGICAL_DRIVES_EXT, 4982 sizeof(struct LD_LOAD_BALANCE_INFO))); 4983 if (!fusion->load_balance_info) 4984 dev_err(&instance->pdev->dev, "Failed to allocate load_balance_info, " 4985 "continuing without Load Balance support\n"); 4986 } 4987 4988 return 0; 4989 } 4990 4991 void 4992 megasas_free_fusion_context(struct megasas_instance *instance) 4993 { 4994 struct fusion_context *fusion = instance->ctrl_context; 4995 4996 if (fusion) { 4997 if (fusion->load_balance_info) { 4998 if (is_vmalloc_addr(fusion->load_balance_info)) 4999 vfree(fusion->load_balance_info); 5000 else 5001 free_pages((ulong)fusion->load_balance_info, 5002 fusion->load_balance_info_pages); 5003 } 5004 5005 if (fusion->log_to_span) { 5006 if (is_vmalloc_addr(fusion->log_to_span)) 5007 vfree(fusion->log_to_span); 5008 else 5009 free_pages((ulong)fusion->log_to_span, 5010 fusion->log_to_span_pages); 5011 } 5012 5013 kfree(fusion); 5014 } 5015 } 5016 5017 struct megasas_instance_template megasas_instance_template_fusion = { 5018 .enable_intr = megasas_enable_intr_fusion, 5019 .disable_intr = megasas_disable_intr_fusion, 5020 .clear_intr = megasas_clear_intr_fusion, 5021 .read_fw_status_reg = megasas_read_fw_status_reg_fusion, 5022 .adp_reset = megasas_adp_reset_fusion, 5023 .check_reset = megasas_check_reset_fusion, 5024 .service_isr = megasas_isr_fusion, 5025 .tasklet = megasas_complete_cmd_dpc_fusion, 5026 .init_adapter = megasas_init_adapter_fusion, 5027 .build_and_issue_cmd = megasas_build_and_issue_cmd_fusion, 5028 .issue_dcmd = megasas_issue_dcmd_fusion, 5029 }; 5030