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