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