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