1/* 2 * 3 * Linux MegaRAID driver for SAS based RAID controllers 4 * 5 * Copyright (c) 2003-2005 LSI Corporation. 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * as published by the Free Software Foundation; either version 10 * 2 of the License, or (at your option) any later version. 11 * 12 * FILE : megaraid_sas.c 13 * Version : v00.00.04.17.1-rc1 14 * 15 * Authors: 16 * (email-id : megaraidlinux@lsi.com) 17 * Sreenivas Bagalkote 18 * Sumant Patro 19 * Bo Yang 20 * 21 * List of supported controllers 22 * 23 * OEM Product Name VID DID SSVID SSID 24 * --- ------------ --- --- ---- ---- 25 */ 26 27#include <linux/kernel.h> 28#include <linux/types.h> 29#include <linux/pci.h> 30#include <linux/list.h> 31#include <linux/moduleparam.h> 32#include <linux/module.h> 33#include <linux/spinlock.h> 34#include <linux/interrupt.h> 35#include <linux/delay.h> 36#include <linux/smp_lock.h> 37#include <linux/uio.h> 38#include <linux/slab.h> 39#include <asm/uaccess.h> 40#include <linux/fs.h> 41#include <linux/compat.h> 42#include <linux/blkdev.h> 43#include <linux/mutex.h> 44#include <linux/poll.h> 45 46#include <scsi/scsi.h> 47#include <scsi/scsi_cmnd.h> 48#include <scsi/scsi_device.h> 49#include <scsi/scsi_host.h> 50#include "megaraid_sas.h" 51 52/* 53 * poll_mode_io:1- schedule complete completion from q cmd 54 */ 55static unsigned int poll_mode_io; 56module_param_named(poll_mode_io, poll_mode_io, int, 0); 57MODULE_PARM_DESC(poll_mode_io, 58 "Complete cmds from IO path, (default=0)"); 59 60MODULE_LICENSE("GPL"); 61MODULE_VERSION(MEGASAS_VERSION); 62MODULE_AUTHOR("megaraidlinux@lsi.com"); 63MODULE_DESCRIPTION("LSI MegaRAID SAS Driver"); 64 65/* 66 * PCI ID table for all supported controllers 67 */ 68static struct pci_device_id megasas_pci_table[] = { 69 70 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1064R)}, 71 /* xscale IOP */ 72 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078R)}, 73 /* ppc IOP */ 74 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078DE)}, 75 /* ppc IOP */ 76 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS1078GEN2)}, 77 /* gen2*/ 78 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0079GEN2)}, 79 /* gen2*/ 80 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0073SKINNY)}, 81 /* skinny*/ 82 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_SAS0071SKINNY)}, 83 /* skinny*/ 84 {PCI_DEVICE(PCI_VENDOR_ID_LSI_LOGIC, PCI_DEVICE_ID_LSI_VERDE_ZCR)}, 85 /* xscale IOP, vega */ 86 {PCI_DEVICE(PCI_VENDOR_ID_DELL, PCI_DEVICE_ID_DELL_PERC5)}, 87 /* xscale IOP */ 88 {} 89}; 90 91MODULE_DEVICE_TABLE(pci, megasas_pci_table); 92 93static int megasas_mgmt_majorno; 94static struct megasas_mgmt_info megasas_mgmt_info; 95static struct fasync_struct *megasas_async_queue; 96static DEFINE_MUTEX(megasas_async_queue_mutex); 97 98static int megasas_poll_wait_aen; 99static DECLARE_WAIT_QUEUE_HEAD(megasas_poll_wait); 100static u32 support_poll_for_event; 101static u32 megasas_dbg_lvl; 102 103/* define lock for aen poll */ 104spinlock_t poll_aen_lock; 105 106static void 107megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd, 108 u8 alt_status); 109 110/** 111 * megasas_get_cmd - Get a command from the free pool 112 * @instance: Adapter soft state 113 * 114 * Returns a free command from the pool 115 */ 116static struct megasas_cmd *megasas_get_cmd(struct megasas_instance 117 *instance) 118{ 119 unsigned long flags; 120 struct megasas_cmd *cmd = NULL; 121 122 spin_lock_irqsave(&instance->cmd_pool_lock, flags); 123 124 if (!list_empty(&instance->cmd_pool)) { 125 cmd = list_entry((&instance->cmd_pool)->next, 126 struct megasas_cmd, list); 127 list_del_init(&cmd->list); 128 } else { 129 printk(KERN_ERR "megasas: Command pool empty!\n"); 130 } 131 132 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags); 133 return cmd; 134} 135 136/** 137 * megasas_return_cmd - Return a cmd to free command pool 138 * @instance: Adapter soft state 139 * @cmd: Command packet to be returned to free command pool 140 */ 141static inline void 142megasas_return_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd) 143{ 144 unsigned long flags; 145 146 spin_lock_irqsave(&instance->cmd_pool_lock, flags); 147 148 cmd->scmd = NULL; 149 list_add_tail(&cmd->list, &instance->cmd_pool); 150 151 spin_unlock_irqrestore(&instance->cmd_pool_lock, flags); 152} 153 154 155/** 156* The following functions are defined for xscale 157* (deviceid : 1064R, PERC5) controllers 158*/ 159 160/** 161 * megasas_enable_intr_xscale - Enables interrupts 162 * @regs: MFI register set 163 */ 164static inline void 165megasas_enable_intr_xscale(struct megasas_register_set __iomem * regs) 166{ 167 writel(1, &(regs)->outbound_intr_mask); 168 169 /* Dummy readl to force pci flush */ 170 readl(®s->outbound_intr_mask); 171} 172 173/** 174 * megasas_disable_intr_xscale -Disables interrupt 175 * @regs: MFI register set 176 */ 177static inline void 178megasas_disable_intr_xscale(struct megasas_register_set __iomem * regs) 179{ 180 u32 mask = 0x1f; 181 writel(mask, ®s->outbound_intr_mask); 182 /* Dummy readl to force pci flush */ 183 readl(®s->outbound_intr_mask); 184} 185 186/** 187 * megasas_read_fw_status_reg_xscale - returns the current FW status value 188 * @regs: MFI register set 189 */ 190static u32 191megasas_read_fw_status_reg_xscale(struct megasas_register_set __iomem * regs) 192{ 193 return readl(&(regs)->outbound_msg_0); 194} 195/** 196 * megasas_clear_interrupt_xscale - Check & clear interrupt 197 * @regs: MFI register set 198 */ 199static int 200megasas_clear_intr_xscale(struct megasas_register_set __iomem * regs) 201{ 202 u32 status; 203 /* 204 * Check if it is our interrupt 205 */ 206 status = readl(®s->outbound_intr_status); 207 208 if (!(status & MFI_OB_INTR_STATUS_MASK)) { 209 return 1; 210 } 211 212 /* 213 * Clear the interrupt by writing back the same value 214 */ 215 writel(status, ®s->outbound_intr_status); 216 217 /* Dummy readl to force pci flush */ 218 readl(®s->outbound_intr_status); 219 220 return 0; 221} 222 223/** 224 * megasas_fire_cmd_xscale - Sends command to the FW 225 * @frame_phys_addr : Physical address of cmd 226 * @frame_count : Number of frames for the command 227 * @regs : MFI register set 228 */ 229static inline void 230megasas_fire_cmd_xscale(struct megasas_instance *instance, 231 dma_addr_t frame_phys_addr, 232 u32 frame_count, 233 struct megasas_register_set __iomem *regs) 234{ 235 writel((frame_phys_addr >> 3)|(frame_count), 236 &(regs)->inbound_queue_port); 237} 238 239static struct megasas_instance_template megasas_instance_template_xscale = { 240 241 .fire_cmd = megasas_fire_cmd_xscale, 242 .enable_intr = megasas_enable_intr_xscale, 243 .disable_intr = megasas_disable_intr_xscale, 244 .clear_intr = megasas_clear_intr_xscale, 245 .read_fw_status_reg = megasas_read_fw_status_reg_xscale, 246}; 247 248/** 249* This is the end of set of functions & definitions specific 250* to xscale (deviceid : 1064R, PERC5) controllers 251*/ 252 253/** 254* The following functions are defined for ppc (deviceid : 0x60) 255* controllers 256*/ 257 258/** 259 * megasas_enable_intr_ppc - Enables interrupts 260 * @regs: MFI register set 261 */ 262static inline void 263megasas_enable_intr_ppc(struct megasas_register_set __iomem * regs) 264{ 265 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear); 266 267 writel(~0x80000004, &(regs)->outbound_intr_mask); 268 269 /* Dummy readl to force pci flush */ 270 readl(®s->outbound_intr_mask); 271} 272 273/** 274 * megasas_disable_intr_ppc - Disable interrupt 275 * @regs: MFI register set 276 */ 277static inline void 278megasas_disable_intr_ppc(struct megasas_register_set __iomem * regs) 279{ 280 u32 mask = 0xFFFFFFFF; 281 writel(mask, ®s->outbound_intr_mask); 282 /* Dummy readl to force pci flush */ 283 readl(®s->outbound_intr_mask); 284} 285 286/** 287 * megasas_read_fw_status_reg_ppc - returns the current FW status value 288 * @regs: MFI register set 289 */ 290static u32 291megasas_read_fw_status_reg_ppc(struct megasas_register_set __iomem * regs) 292{ 293 return readl(&(regs)->outbound_scratch_pad); 294} 295 296/** 297 * megasas_clear_interrupt_ppc - Check & clear interrupt 298 * @regs: MFI register set 299 */ 300static int 301megasas_clear_intr_ppc(struct megasas_register_set __iomem * regs) 302{ 303 u32 status; 304 /* 305 * Check if it is our interrupt 306 */ 307 status = readl(®s->outbound_intr_status); 308 309 if (!(status & MFI_REPLY_1078_MESSAGE_INTERRUPT)) { 310 return 1; 311 } 312 313 /* 314 * Clear the interrupt by writing back the same value 315 */ 316 writel(status, ®s->outbound_doorbell_clear); 317 318 /* Dummy readl to force pci flush */ 319 readl(®s->outbound_doorbell_clear); 320 321 return 0; 322} 323/** 324 * megasas_fire_cmd_ppc - Sends command to the FW 325 * @frame_phys_addr : Physical address of cmd 326 * @frame_count : Number of frames for the command 327 * @regs : MFI register set 328 */ 329static inline void 330megasas_fire_cmd_ppc(struct megasas_instance *instance, 331 dma_addr_t frame_phys_addr, 332 u32 frame_count, 333 struct megasas_register_set __iomem *regs) 334{ 335 writel((frame_phys_addr | (frame_count<<1))|1, 336 &(regs)->inbound_queue_port); 337} 338 339static struct megasas_instance_template megasas_instance_template_ppc = { 340 341 .fire_cmd = megasas_fire_cmd_ppc, 342 .enable_intr = megasas_enable_intr_ppc, 343 .disable_intr = megasas_disable_intr_ppc, 344 .clear_intr = megasas_clear_intr_ppc, 345 .read_fw_status_reg = megasas_read_fw_status_reg_ppc, 346}; 347 348/** 349 * megasas_enable_intr_skinny - Enables interrupts 350 * @regs: MFI register set 351 */ 352static inline void 353megasas_enable_intr_skinny(struct megasas_register_set __iomem *regs) 354{ 355 writel(0xFFFFFFFF, &(regs)->outbound_intr_mask); 356 357 writel(~MFI_SKINNY_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask); 358 359 /* Dummy readl to force pci flush */ 360 readl(®s->outbound_intr_mask); 361} 362 363/** 364 * megasas_disable_intr_skinny - Disables interrupt 365 * @regs: MFI register set 366 */ 367static inline void 368megasas_disable_intr_skinny(struct megasas_register_set __iomem *regs) 369{ 370 u32 mask = 0xFFFFFFFF; 371 writel(mask, ®s->outbound_intr_mask); 372 /* Dummy readl to force pci flush */ 373 readl(®s->outbound_intr_mask); 374} 375 376/** 377 * megasas_read_fw_status_reg_skinny - returns the current FW status value 378 * @regs: MFI register set 379 */ 380static u32 381megasas_read_fw_status_reg_skinny(struct megasas_register_set __iomem *regs) 382{ 383 return readl(&(regs)->outbound_scratch_pad); 384} 385 386/** 387 * megasas_clear_interrupt_skinny - Check & clear interrupt 388 * @regs: MFI register set 389 */ 390static int 391megasas_clear_intr_skinny(struct megasas_register_set __iomem *regs) 392{ 393 u32 status; 394 /* 395 * Check if it is our interrupt 396 */ 397 status = readl(®s->outbound_intr_status); 398 399 if (!(status & MFI_SKINNY_ENABLE_INTERRUPT_MASK)) { 400 return 1; 401 } 402 403 /* 404 * Clear the interrupt by writing back the same value 405 */ 406 writel(status, ®s->outbound_intr_status); 407 408 /* 409 * dummy read to flush PCI 410 */ 411 readl(®s->outbound_intr_status); 412 413 return 0; 414} 415 416/** 417 * megasas_fire_cmd_skinny - Sends command to the FW 418 * @frame_phys_addr : Physical address of cmd 419 * @frame_count : Number of frames for the command 420 * @regs : MFI register set 421 */ 422static inline void 423megasas_fire_cmd_skinny(struct megasas_instance *instance, 424 dma_addr_t frame_phys_addr, 425 u32 frame_count, 426 struct megasas_register_set __iomem *regs) 427{ 428 unsigned long flags; 429 spin_lock_irqsave(&instance->fire_lock, flags); 430 writel(0, &(regs)->inbound_high_queue_port); 431 writel((frame_phys_addr | (frame_count<<1))|1, 432 &(regs)->inbound_low_queue_port); 433 spin_unlock_irqrestore(&instance->fire_lock, flags); 434} 435 436static struct megasas_instance_template megasas_instance_template_skinny = { 437 438 .fire_cmd = megasas_fire_cmd_skinny, 439 .enable_intr = megasas_enable_intr_skinny, 440 .disable_intr = megasas_disable_intr_skinny, 441 .clear_intr = megasas_clear_intr_skinny, 442 .read_fw_status_reg = megasas_read_fw_status_reg_skinny, 443}; 444 445 446/** 447* The following functions are defined for gen2 (deviceid : 0x78 0x79) 448* controllers 449*/ 450 451/** 452 * megasas_enable_intr_gen2 - Enables interrupts 453 * @regs: MFI register set 454 */ 455static inline void 456megasas_enable_intr_gen2(struct megasas_register_set __iomem *regs) 457{ 458 writel(0xFFFFFFFF, &(regs)->outbound_doorbell_clear); 459 460 /* write ~0x00000005 (4 & 1) to the intr mask*/ 461 writel(~MFI_GEN2_ENABLE_INTERRUPT_MASK, &(regs)->outbound_intr_mask); 462 463 /* Dummy readl to force pci flush */ 464 readl(®s->outbound_intr_mask); 465} 466 467/** 468 * megasas_disable_intr_gen2 - Disables interrupt 469 * @regs: MFI register set 470 */ 471static inline void 472megasas_disable_intr_gen2(struct megasas_register_set __iomem *regs) 473{ 474 u32 mask = 0xFFFFFFFF; 475 writel(mask, ®s->outbound_intr_mask); 476 /* Dummy readl to force pci flush */ 477 readl(®s->outbound_intr_mask); 478} 479 480/** 481 * megasas_read_fw_status_reg_gen2 - returns the current FW status value 482 * @regs: MFI register set 483 */ 484static u32 485megasas_read_fw_status_reg_gen2(struct megasas_register_set __iomem *regs) 486{ 487 return readl(&(regs)->outbound_scratch_pad); 488} 489 490/** 491 * megasas_clear_interrupt_gen2 - Check & clear interrupt 492 * @regs: MFI register set 493 */ 494static int 495megasas_clear_intr_gen2(struct megasas_register_set __iomem *regs) 496{ 497 u32 status; 498 /* 499 * Check if it is our interrupt 500 */ 501 status = readl(®s->outbound_intr_status); 502 503 if (!(status & MFI_GEN2_ENABLE_INTERRUPT_MASK)) 504 return 1; 505 506 /* 507 * Clear the interrupt by writing back the same value 508 */ 509 writel(status, ®s->outbound_doorbell_clear); 510 511 /* Dummy readl to force pci flush */ 512 readl(®s->outbound_intr_status); 513 514 return 0; 515} 516/** 517 * megasas_fire_cmd_gen2 - Sends command to the FW 518 * @frame_phys_addr : Physical address of cmd 519 * @frame_count : Number of frames for the command 520 * @regs : MFI register set 521 */ 522static inline void 523megasas_fire_cmd_gen2(struct megasas_instance *instance, 524 dma_addr_t frame_phys_addr, 525 u32 frame_count, 526 struct megasas_register_set __iomem *regs) 527{ 528 writel((frame_phys_addr | (frame_count<<1))|1, 529 &(regs)->inbound_queue_port); 530} 531 532static struct megasas_instance_template megasas_instance_template_gen2 = { 533 534 .fire_cmd = megasas_fire_cmd_gen2, 535 .enable_intr = megasas_enable_intr_gen2, 536 .disable_intr = megasas_disable_intr_gen2, 537 .clear_intr = megasas_clear_intr_gen2, 538 .read_fw_status_reg = megasas_read_fw_status_reg_gen2, 539}; 540 541/** 542* This is the end of set of functions & definitions 543* specific to ppc (deviceid : 0x60) controllers 544*/ 545 546/** 547 * megasas_issue_polled - Issues a polling command 548 * @instance: Adapter soft state 549 * @cmd: Command packet to be issued 550 * 551 * For polling, MFI requires the cmd_status to be set to 0xFF before posting. 552 */ 553static int 554megasas_issue_polled(struct megasas_instance *instance, struct megasas_cmd *cmd) 555{ 556 int i; 557 u32 msecs = MFI_POLL_TIMEOUT_SECS * 1000; 558 559 struct megasas_header *frame_hdr = &cmd->frame->hdr; 560 561 frame_hdr->cmd_status = 0xFF; 562 frame_hdr->flags |= MFI_FRAME_DONT_POST_IN_REPLY_QUEUE; 563 564 /* 565 * Issue the frame using inbound queue port 566 */ 567 instance->instancet->fire_cmd(instance, 568 cmd->frame_phys_addr, 0, instance->reg_set); 569 570 /* 571 * Wait for cmd_status to change 572 */ 573 for (i = 0; (i < msecs) && (frame_hdr->cmd_status == 0xff); i++) { 574 rmb(); 575 msleep(1); 576 } 577 578 if (frame_hdr->cmd_status == 0xff) 579 return -ETIME; 580 581 return 0; 582} 583 584/** 585 * megasas_issue_blocked_cmd - Synchronous wrapper around regular FW cmds 586 * @instance: Adapter soft state 587 * @cmd: Command to be issued 588 * 589 * This function waits on an event for the command to be returned from ISR. 590 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs 591 * Used to issue ioctl commands. 592 */ 593static int 594megasas_issue_blocked_cmd(struct megasas_instance *instance, 595 struct megasas_cmd *cmd) 596{ 597 cmd->cmd_status = ENODATA; 598 599 instance->instancet->fire_cmd(instance, 600 cmd->frame_phys_addr, 0, instance->reg_set); 601 602 wait_event_timeout(instance->int_cmd_wait_q, (cmd->cmd_status != ENODATA), 603 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ); 604 605 return 0; 606} 607 608/** 609 * megasas_issue_blocked_abort_cmd - Aborts previously issued cmd 610 * @instance: Adapter soft state 611 * @cmd_to_abort: Previously issued cmd to be aborted 612 * 613 * MFI firmware can abort previously issued AEN comamnd (automatic event 614 * notification). The megasas_issue_blocked_abort_cmd() issues such abort 615 * cmd and waits for return status. 616 * Max wait time is MEGASAS_INTERNAL_CMD_WAIT_TIME secs 617 */ 618static int 619megasas_issue_blocked_abort_cmd(struct megasas_instance *instance, 620 struct megasas_cmd *cmd_to_abort) 621{ 622 struct megasas_cmd *cmd; 623 struct megasas_abort_frame *abort_fr; 624 625 cmd = megasas_get_cmd(instance); 626 627 if (!cmd) 628 return -1; 629 630 abort_fr = &cmd->frame->abort; 631 632 /* 633 * Prepare and issue the abort frame 634 */ 635 abort_fr->cmd = MFI_CMD_ABORT; 636 abort_fr->cmd_status = 0xFF; 637 abort_fr->flags = 0; 638 abort_fr->abort_context = cmd_to_abort->index; 639 abort_fr->abort_mfi_phys_addr_lo = cmd_to_abort->frame_phys_addr; 640 abort_fr->abort_mfi_phys_addr_hi = 0; 641 642 cmd->sync_cmd = 1; 643 cmd->cmd_status = 0xFF; 644 645 instance->instancet->fire_cmd(instance, 646 cmd->frame_phys_addr, 0, instance->reg_set); 647 648 /* 649 * Wait for this cmd to complete 650 */ 651 wait_event_timeout(instance->abort_cmd_wait_q, (cmd->cmd_status != 0xFF), 652 MEGASAS_INTERNAL_CMD_WAIT_TIME*HZ); 653 654 megasas_return_cmd(instance, cmd); 655 return 0; 656} 657 658/** 659 * megasas_make_sgl32 - Prepares 32-bit SGL 660 * @instance: Adapter soft state 661 * @scp: SCSI command from the mid-layer 662 * @mfi_sgl: SGL to be filled in 663 * 664 * If successful, this function returns the number of SG elements. Otherwise, 665 * it returnes -1. 666 */ 667static int 668megasas_make_sgl32(struct megasas_instance *instance, struct scsi_cmnd *scp, 669 union megasas_sgl *mfi_sgl) 670{ 671 int i; 672 int sge_count; 673 struct scatterlist *os_sgl; 674 675 sge_count = scsi_dma_map(scp); 676 BUG_ON(sge_count < 0); 677 678 if (sge_count) { 679 scsi_for_each_sg(scp, os_sgl, sge_count, i) { 680 mfi_sgl->sge32[i].length = sg_dma_len(os_sgl); 681 mfi_sgl->sge32[i].phys_addr = sg_dma_address(os_sgl); 682 } 683 } 684 return sge_count; 685} 686 687/** 688 * megasas_make_sgl64 - Prepares 64-bit SGL 689 * @instance: Adapter soft state 690 * @scp: SCSI command from the mid-layer 691 * @mfi_sgl: SGL to be filled in 692 * 693 * If successful, this function returns the number of SG elements. Otherwise, 694 * it returnes -1. 695 */ 696static int 697megasas_make_sgl64(struct megasas_instance *instance, struct scsi_cmnd *scp, 698 union megasas_sgl *mfi_sgl) 699{ 700 int i; 701 int sge_count; 702 struct scatterlist *os_sgl; 703 704 sge_count = scsi_dma_map(scp); 705 BUG_ON(sge_count < 0); 706 707 if (sge_count) { 708 scsi_for_each_sg(scp, os_sgl, sge_count, i) { 709 mfi_sgl->sge64[i].length = sg_dma_len(os_sgl); 710 mfi_sgl->sge64[i].phys_addr = sg_dma_address(os_sgl); 711 } 712 } 713 return sge_count; 714} 715 716/** 717 * megasas_make_sgl_skinny - Prepares IEEE SGL 718 * @instance: Adapter soft state 719 * @scp: SCSI command from the mid-layer 720 * @mfi_sgl: SGL to be filled in 721 * 722 * If successful, this function returns the number of SG elements. Otherwise, 723 * it returnes -1. 724 */ 725static int 726megasas_make_sgl_skinny(struct megasas_instance *instance, 727 struct scsi_cmnd *scp, union megasas_sgl *mfi_sgl) 728{ 729 int i; 730 int sge_count; 731 struct scatterlist *os_sgl; 732 733 sge_count = scsi_dma_map(scp); 734 735 if (sge_count) { 736 scsi_for_each_sg(scp, os_sgl, sge_count, i) { 737 mfi_sgl->sge_skinny[i].length = sg_dma_len(os_sgl); 738 mfi_sgl->sge_skinny[i].phys_addr = 739 sg_dma_address(os_sgl); 740 } 741 } 742 return sge_count; 743} 744 745 /** 746 * megasas_get_frame_count - Computes the number of frames 747 * @frame_type : type of frame- io or pthru frame 748 * @sge_count : number of sg elements 749 * 750 * Returns the number of frames required for numnber of sge's (sge_count) 751 */ 752 753static u32 megasas_get_frame_count(struct megasas_instance *instance, 754 u8 sge_count, u8 frame_type) 755{ 756 int num_cnt; 757 int sge_bytes; 758 u32 sge_sz; 759 u32 frame_count=0; 760 761 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) : 762 sizeof(struct megasas_sge32); 763 764 if (instance->flag_ieee) { 765 sge_sz = sizeof(struct megasas_sge_skinny); 766 } 767 768 /* 769 * Main frame can contain 2 SGEs for 64-bit SGLs and 770 * 3 SGEs for 32-bit SGLs for ldio & 771 * 1 SGEs for 64-bit SGLs and 772 * 2 SGEs for 32-bit SGLs for pthru frame 773 */ 774 if (unlikely(frame_type == PTHRU_FRAME)) { 775 if (instance->flag_ieee == 1) { 776 num_cnt = sge_count - 1; 777 } else if (IS_DMA64) 778 num_cnt = sge_count - 1; 779 else 780 num_cnt = sge_count - 2; 781 } else { 782 if (instance->flag_ieee == 1) { 783 num_cnt = sge_count - 1; 784 } else if (IS_DMA64) 785 num_cnt = sge_count - 2; 786 else 787 num_cnt = sge_count - 3; 788 } 789 790 if(num_cnt>0){ 791 sge_bytes = sge_sz * num_cnt; 792 793 frame_count = (sge_bytes / MEGAMFI_FRAME_SIZE) + 794 ((sge_bytes % MEGAMFI_FRAME_SIZE) ? 1 : 0) ; 795 } 796 /* Main frame */ 797 frame_count +=1; 798 799 if (frame_count > 7) 800 frame_count = 8; 801 return frame_count; 802} 803 804/** 805 * megasas_build_dcdb - Prepares a direct cdb (DCDB) command 806 * @instance: Adapter soft state 807 * @scp: SCSI command 808 * @cmd: Command to be prepared in 809 * 810 * This function prepares CDB commands. These are typcially pass-through 811 * commands to the devices. 812 */ 813static int 814megasas_build_dcdb(struct megasas_instance *instance, struct scsi_cmnd *scp, 815 struct megasas_cmd *cmd) 816{ 817 u32 is_logical; 818 u32 device_id; 819 u16 flags = 0; 820 struct megasas_pthru_frame *pthru; 821 822 is_logical = MEGASAS_IS_LOGICAL(scp); 823 device_id = MEGASAS_DEV_INDEX(instance, scp); 824 pthru = (struct megasas_pthru_frame *)cmd->frame; 825 826 if (scp->sc_data_direction == PCI_DMA_TODEVICE) 827 flags = MFI_FRAME_DIR_WRITE; 828 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) 829 flags = MFI_FRAME_DIR_READ; 830 else if (scp->sc_data_direction == PCI_DMA_NONE) 831 flags = MFI_FRAME_DIR_NONE; 832 833 if (instance->flag_ieee == 1) { 834 flags |= MFI_FRAME_IEEE; 835 } 836 837 /* 838 * Prepare the DCDB frame 839 */ 840 pthru->cmd = (is_logical) ? MFI_CMD_LD_SCSI_IO : MFI_CMD_PD_SCSI_IO; 841 pthru->cmd_status = 0x0; 842 pthru->scsi_status = 0x0; 843 pthru->target_id = device_id; 844 pthru->lun = scp->device->lun; 845 pthru->cdb_len = scp->cmd_len; 846 pthru->timeout = 0; 847 pthru->pad_0 = 0; 848 pthru->flags = flags; 849 pthru->data_xfer_len = scsi_bufflen(scp); 850 851 memcpy(pthru->cdb, scp->cmnd, scp->cmd_len); 852 853 /* 854 * If the command is for the tape device, set the 855 * pthru timeout to the os layer timeout value. 856 */ 857 if (scp->device->type == TYPE_TAPE) { 858 if ((scp->request->timeout / HZ) > 0xFFFF) 859 pthru->timeout = 0xFFFF; 860 else 861 pthru->timeout = scp->request->timeout / HZ; 862 } 863 864 /* 865 * Construct SGL 866 */ 867 if (instance->flag_ieee == 1) { 868 pthru->flags |= MFI_FRAME_SGL64; 869 pthru->sge_count = megasas_make_sgl_skinny(instance, scp, 870 &pthru->sgl); 871 } else if (IS_DMA64) { 872 pthru->flags |= MFI_FRAME_SGL64; 873 pthru->sge_count = megasas_make_sgl64(instance, scp, 874 &pthru->sgl); 875 } else 876 pthru->sge_count = megasas_make_sgl32(instance, scp, 877 &pthru->sgl); 878 879 if (pthru->sge_count > instance->max_num_sge) { 880 printk(KERN_ERR "megasas: DCDB two many SGE NUM=%x\n", 881 pthru->sge_count); 882 return 0; 883 } 884 885 /* 886 * Sense info specific 887 */ 888 pthru->sense_len = SCSI_SENSE_BUFFERSIZE; 889 pthru->sense_buf_phys_addr_hi = 0; 890 pthru->sense_buf_phys_addr_lo = cmd->sense_phys_addr; 891 892 /* 893 * Compute the total number of frames this command consumes. FW uses 894 * this number to pull sufficient number of frames from host memory. 895 */ 896 cmd->frame_count = megasas_get_frame_count(instance, pthru->sge_count, 897 PTHRU_FRAME); 898 899 return cmd->frame_count; 900} 901 902/** 903 * megasas_build_ldio - Prepares IOs to logical devices 904 * @instance: Adapter soft state 905 * @scp: SCSI command 906 * @cmd: Command to be prepared 907 * 908 * Frames (and accompanying SGLs) for regular SCSI IOs use this function. 909 */ 910static int 911megasas_build_ldio(struct megasas_instance *instance, struct scsi_cmnd *scp, 912 struct megasas_cmd *cmd) 913{ 914 u32 device_id; 915 u8 sc = scp->cmnd[0]; 916 u16 flags = 0; 917 struct megasas_io_frame *ldio; 918 919 device_id = MEGASAS_DEV_INDEX(instance, scp); 920 ldio = (struct megasas_io_frame *)cmd->frame; 921 922 if (scp->sc_data_direction == PCI_DMA_TODEVICE) 923 flags = MFI_FRAME_DIR_WRITE; 924 else if (scp->sc_data_direction == PCI_DMA_FROMDEVICE) 925 flags = MFI_FRAME_DIR_READ; 926 927 if (instance->flag_ieee == 1) { 928 flags |= MFI_FRAME_IEEE; 929 } 930 931 /* 932 * Prepare the Logical IO frame: 2nd bit is zero for all read cmds 933 */ 934 ldio->cmd = (sc & 0x02) ? MFI_CMD_LD_WRITE : MFI_CMD_LD_READ; 935 ldio->cmd_status = 0x0; 936 ldio->scsi_status = 0x0; 937 ldio->target_id = device_id; 938 ldio->timeout = 0; 939 ldio->reserved_0 = 0; 940 ldio->pad_0 = 0; 941 ldio->flags = flags; 942 ldio->start_lba_hi = 0; 943 ldio->access_byte = (scp->cmd_len != 6) ? scp->cmnd[1] : 0; 944 945 /* 946 * 6-byte READ(0x08) or WRITE(0x0A) cdb 947 */ 948 if (scp->cmd_len == 6) { 949 ldio->lba_count = (u32) scp->cmnd[4]; 950 ldio->start_lba_lo = ((u32) scp->cmnd[1] << 16) | 951 ((u32) scp->cmnd[2] << 8) | (u32) scp->cmnd[3]; 952 953 ldio->start_lba_lo &= 0x1FFFFF; 954 } 955 956 /* 957 * 10-byte READ(0x28) or WRITE(0x2A) cdb 958 */ 959 else if (scp->cmd_len == 10) { 960 ldio->lba_count = (u32) scp->cmnd[8] | 961 ((u32) scp->cmnd[7] << 8); 962 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) | 963 ((u32) scp->cmnd[3] << 16) | 964 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5]; 965 } 966 967 /* 968 * 12-byte READ(0xA8) or WRITE(0xAA) cdb 969 */ 970 else if (scp->cmd_len == 12) { 971 ldio->lba_count = ((u32) scp->cmnd[6] << 24) | 972 ((u32) scp->cmnd[7] << 16) | 973 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9]; 974 975 ldio->start_lba_lo = ((u32) scp->cmnd[2] << 24) | 976 ((u32) scp->cmnd[3] << 16) | 977 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5]; 978 } 979 980 /* 981 * 16-byte READ(0x88) or WRITE(0x8A) cdb 982 */ 983 else if (scp->cmd_len == 16) { 984 ldio->lba_count = ((u32) scp->cmnd[10] << 24) | 985 ((u32) scp->cmnd[11] << 16) | 986 ((u32) scp->cmnd[12] << 8) | (u32) scp->cmnd[13]; 987 988 ldio->start_lba_lo = ((u32) scp->cmnd[6] << 24) | 989 ((u32) scp->cmnd[7] << 16) | 990 ((u32) scp->cmnd[8] << 8) | (u32) scp->cmnd[9]; 991 992 ldio->start_lba_hi = ((u32) scp->cmnd[2] << 24) | 993 ((u32) scp->cmnd[3] << 16) | 994 ((u32) scp->cmnd[4] << 8) | (u32) scp->cmnd[5]; 995 996 } 997 998 /* 999 * Construct SGL 1000 */ 1001 if (instance->flag_ieee) { 1002 ldio->flags |= MFI_FRAME_SGL64; 1003 ldio->sge_count = megasas_make_sgl_skinny(instance, scp, 1004 &ldio->sgl); 1005 } else if (IS_DMA64) { 1006 ldio->flags |= MFI_FRAME_SGL64; 1007 ldio->sge_count = megasas_make_sgl64(instance, scp, &ldio->sgl); 1008 } else 1009 ldio->sge_count = megasas_make_sgl32(instance, scp, &ldio->sgl); 1010 1011 if (ldio->sge_count > instance->max_num_sge) { 1012 printk(KERN_ERR "megasas: build_ld_io: sge_count = %x\n", 1013 ldio->sge_count); 1014 return 0; 1015 } 1016 1017 /* 1018 * Sense info specific 1019 */ 1020 ldio->sense_len = SCSI_SENSE_BUFFERSIZE; 1021 ldio->sense_buf_phys_addr_hi = 0; 1022 ldio->sense_buf_phys_addr_lo = cmd->sense_phys_addr; 1023 1024 /* 1025 * Compute the total number of frames this command consumes. FW uses 1026 * this number to pull sufficient number of frames from host memory. 1027 */ 1028 cmd->frame_count = megasas_get_frame_count(instance, 1029 ldio->sge_count, IO_FRAME); 1030 1031 return cmd->frame_count; 1032} 1033 1034/** 1035 * megasas_is_ldio - Checks if the cmd is for logical drive 1036 * @scmd: SCSI command 1037 * 1038 * Called by megasas_queue_command to find out if the command to be queued 1039 * is a logical drive command 1040 */ 1041static inline int megasas_is_ldio(struct scsi_cmnd *cmd) 1042{ 1043 if (!MEGASAS_IS_LOGICAL(cmd)) 1044 return 0; 1045 switch (cmd->cmnd[0]) { 1046 case READ_10: 1047 case WRITE_10: 1048 case READ_12: 1049 case WRITE_12: 1050 case READ_6: 1051 case WRITE_6: 1052 case READ_16: 1053 case WRITE_16: 1054 return 1; 1055 default: 1056 return 0; 1057 } 1058} 1059 1060 /** 1061 * megasas_dump_pending_frames - Dumps the frame address of all pending cmds 1062 * in FW 1063 * @instance: Adapter soft state 1064 */ 1065static inline void 1066megasas_dump_pending_frames(struct megasas_instance *instance) 1067{ 1068 struct megasas_cmd *cmd; 1069 int i,n; 1070 union megasas_sgl *mfi_sgl; 1071 struct megasas_io_frame *ldio; 1072 struct megasas_pthru_frame *pthru; 1073 u32 sgcount; 1074 u32 max_cmd = instance->max_fw_cmds; 1075 1076 printk(KERN_ERR "\nmegasas[%d]: Dumping Frame Phys Address of all pending cmds in FW\n",instance->host->host_no); 1077 printk(KERN_ERR "megasas[%d]: Total OS Pending cmds : %d\n",instance->host->host_no,atomic_read(&instance->fw_outstanding)); 1078 if (IS_DMA64) 1079 printk(KERN_ERR "\nmegasas[%d]: 64 bit SGLs were sent to FW\n",instance->host->host_no); 1080 else 1081 printk(KERN_ERR "\nmegasas[%d]: 32 bit SGLs were sent to FW\n",instance->host->host_no); 1082 1083 printk(KERN_ERR "megasas[%d]: Pending OS cmds in FW : \n",instance->host->host_no); 1084 for (i = 0; i < max_cmd; i++) { 1085 cmd = instance->cmd_list[i]; 1086 if(!cmd->scmd) 1087 continue; 1088 printk(KERN_ERR "megasas[%d]: Frame addr :0x%08lx : ",instance->host->host_no,(unsigned long)cmd->frame_phys_addr); 1089 if (megasas_is_ldio(cmd->scmd)){ 1090 ldio = (struct megasas_io_frame *)cmd->frame; 1091 mfi_sgl = &ldio->sgl; 1092 sgcount = ldio->sge_count; 1093 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lba lo : 0x%x, lba_hi : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no, cmd->frame_count,ldio->cmd,ldio->target_id, ldio->start_lba_lo,ldio->start_lba_hi,ldio->sense_buf_phys_addr_lo,sgcount); 1094 } 1095 else { 1096 pthru = (struct megasas_pthru_frame *) cmd->frame; 1097 mfi_sgl = &pthru->sgl; 1098 sgcount = pthru->sge_count; 1099 printk(KERN_ERR "megasas[%d]: frame count : 0x%x, Cmd : 0x%x, Tgt id : 0x%x, lun : 0x%x, cdb_len : 0x%x, data xfer len : 0x%x, sense_buf addr : 0x%x,sge count : 0x%x\n",instance->host->host_no,cmd->frame_count,pthru->cmd,pthru->target_id,pthru->lun,pthru->cdb_len , pthru->data_xfer_len,pthru->sense_buf_phys_addr_lo,sgcount); 1100 } 1101 if(megasas_dbg_lvl & MEGASAS_DBG_LVL){ 1102 for (n = 0; n < sgcount; n++){ 1103 if (IS_DMA64) 1104 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%08lx ",mfi_sgl->sge64[n].length , (unsigned long)mfi_sgl->sge64[n].phys_addr) ; 1105 else 1106 printk(KERN_ERR "megasas: sgl len : 0x%x, sgl addr : 0x%x ",mfi_sgl->sge32[n].length , mfi_sgl->sge32[n].phys_addr) ; 1107 } 1108 } 1109 printk(KERN_ERR "\n"); 1110 } /*for max_cmd*/ 1111 printk(KERN_ERR "\nmegasas[%d]: Pending Internal cmds in FW : \n",instance->host->host_no); 1112 for (i = 0; i < max_cmd; i++) { 1113 1114 cmd = instance->cmd_list[i]; 1115 1116 if(cmd->sync_cmd == 1){ 1117 printk(KERN_ERR "0x%08lx : ", (unsigned long)cmd->frame_phys_addr); 1118 } 1119 } 1120 printk(KERN_ERR "megasas[%d]: Dumping Done.\n\n",instance->host->host_no); 1121} 1122 1123/** 1124 * megasas_queue_command - Queue entry point 1125 * @scmd: SCSI command to be queued 1126 * @done: Callback entry point 1127 */ 1128static int 1129megasas_queue_command(struct scsi_cmnd *scmd, void (*done) (struct scsi_cmnd *)) 1130{ 1131 u32 frame_count; 1132 struct megasas_cmd *cmd; 1133 struct megasas_instance *instance; 1134 1135 instance = (struct megasas_instance *) 1136 scmd->device->host->hostdata; 1137 1138 /* Don't process if we have already declared adapter dead */ 1139 if (instance->hw_crit_error) 1140 return SCSI_MLQUEUE_HOST_BUSY; 1141 1142 scmd->scsi_done = done; 1143 scmd->result = 0; 1144 1145 if (MEGASAS_IS_LOGICAL(scmd) && 1146 (scmd->device->id >= MEGASAS_MAX_LD || scmd->device->lun)) { 1147 scmd->result = DID_BAD_TARGET << 16; 1148 goto out_done; 1149 } 1150 1151 switch (scmd->cmnd[0]) { 1152 case SYNCHRONIZE_CACHE: 1153 /* 1154 * FW takes care of flush cache on its own 1155 * No need to send it down 1156 */ 1157 scmd->result = DID_OK << 16; 1158 goto out_done; 1159 default: 1160 break; 1161 } 1162 1163 cmd = megasas_get_cmd(instance); 1164 if (!cmd) 1165 return SCSI_MLQUEUE_HOST_BUSY; 1166 1167 /* 1168 * Logical drive command 1169 */ 1170 if (megasas_is_ldio(scmd)) 1171 frame_count = megasas_build_ldio(instance, scmd, cmd); 1172 else 1173 frame_count = megasas_build_dcdb(instance, scmd, cmd); 1174 1175 if (!frame_count) 1176 goto out_return_cmd; 1177 1178 cmd->scmd = scmd; 1179 scmd->SCp.ptr = (char *)cmd; 1180 1181 /* 1182 * Issue the command to the FW 1183 */ 1184 atomic_inc(&instance->fw_outstanding); 1185 1186 instance->instancet->fire_cmd(instance, cmd->frame_phys_addr, 1187 cmd->frame_count-1, instance->reg_set); 1188 /* 1189 * Check if we have pend cmds to be completed 1190 */ 1191 if (poll_mode_io && atomic_read(&instance->fw_outstanding)) 1192 tasklet_schedule(&instance->isr_tasklet); 1193 1194 1195 return 0; 1196 1197 out_return_cmd: 1198 megasas_return_cmd(instance, cmd); 1199 out_done: 1200 done(scmd); 1201 return 0; 1202} 1203 1204static struct megasas_instance *megasas_lookup_instance(u16 host_no) 1205{ 1206 int i; 1207 1208 for (i = 0; i < megasas_mgmt_info.max_index; i++) { 1209 1210 if ((megasas_mgmt_info.instance[i]) && 1211 (megasas_mgmt_info.instance[i]->host->host_no == host_no)) 1212 return megasas_mgmt_info.instance[i]; 1213 } 1214 1215 return NULL; 1216} 1217 1218static int megasas_slave_configure(struct scsi_device *sdev) 1219{ 1220 u16 pd_index = 0; 1221 struct megasas_instance *instance ; 1222 1223 instance = megasas_lookup_instance(sdev->host->host_no); 1224 1225 if (sdev->channel < MEGASAS_MAX_PD_CHANNELS && 1226 sdev->type == TYPE_DISK) { 1227 pd_index = (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + 1228 sdev->id; 1229 if (instance->pd_list[pd_index].driveState == 1230 MR_PD_STATE_SYSTEM) { 1231 blk_queue_rq_timeout(sdev->request_queue, 1232 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ); 1233 return 0; 1234 } 1235 return -ENXIO; 1236 } 1237 1238 /* 1239 * The RAID firmware may require extended timeouts. 1240 */ 1241 blk_queue_rq_timeout(sdev->request_queue, 1242 MEGASAS_DEFAULT_CMD_TIMEOUT * HZ); 1243 return 0; 1244} 1245 1246static int megasas_slave_alloc(struct scsi_device *sdev) 1247{ 1248 u16 pd_index = 0; 1249 struct megasas_instance *instance ; 1250 instance = megasas_lookup_instance(sdev->host->host_no); 1251 if ((sdev->channel < MEGASAS_MAX_PD_CHANNELS) && 1252 (sdev->type == TYPE_DISK)) { 1253 /* 1254 * Open the OS scan to the SYSTEM PD 1255 */ 1256 pd_index = 1257 (sdev->channel * MEGASAS_MAX_DEV_PER_CHANNEL) + 1258 sdev->id; 1259 if ((instance->pd_list[pd_index].driveState == 1260 MR_PD_STATE_SYSTEM) && 1261 (instance->pd_list[pd_index].driveType == 1262 TYPE_DISK)) { 1263 return 0; 1264 } 1265 return -ENXIO; 1266 } 1267 return 0; 1268} 1269 1270/** 1271 * megasas_complete_cmd_dpc - Returns FW's controller structure 1272 * @instance_addr: Address of adapter soft state 1273 * 1274 * Tasklet to complete cmds 1275 */ 1276static void megasas_complete_cmd_dpc(unsigned long instance_addr) 1277{ 1278 u32 producer; 1279 u32 consumer; 1280 u32 context; 1281 struct megasas_cmd *cmd; 1282 struct megasas_instance *instance = 1283 (struct megasas_instance *)instance_addr; 1284 unsigned long flags; 1285 1286 /* If we have already declared adapter dead, donot complete cmds */ 1287 if (instance->hw_crit_error) 1288 return; 1289 1290 spin_lock_irqsave(&instance->completion_lock, flags); 1291 1292 producer = *instance->producer; 1293 consumer = *instance->consumer; 1294 1295 while (consumer != producer) { 1296 context = instance->reply_queue[consumer]; 1297 1298 cmd = instance->cmd_list[context]; 1299 1300 megasas_complete_cmd(instance, cmd, DID_OK); 1301 1302 consumer++; 1303 if (consumer == (instance->max_fw_cmds + 1)) { 1304 consumer = 0; 1305 } 1306 } 1307 1308 *instance->consumer = producer; 1309 1310 spin_unlock_irqrestore(&instance->completion_lock, flags); 1311 1312 /* 1313 * Check if we can restore can_queue 1314 */ 1315 if (instance->flag & MEGASAS_FW_BUSY 1316 && time_after(jiffies, instance->last_time + 5 * HZ) 1317 && atomic_read(&instance->fw_outstanding) < 17) { 1318 1319 spin_lock_irqsave(instance->host->host_lock, flags); 1320 instance->flag &= ~MEGASAS_FW_BUSY; 1321 if ((instance->pdev->device == 1322 PCI_DEVICE_ID_LSI_SAS0073SKINNY) || 1323 (instance->pdev->device == 1324 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) { 1325 instance->host->can_queue = 1326 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS; 1327 } else 1328 instance->host->can_queue = 1329 instance->max_fw_cmds - MEGASAS_INT_CMDS; 1330 1331 spin_unlock_irqrestore(instance->host->host_lock, flags); 1332 } 1333} 1334 1335/** 1336 * megasas_wait_for_outstanding - Wait for all outstanding cmds 1337 * @instance: Adapter soft state 1338 * 1339 * This function waits for upto MEGASAS_RESET_WAIT_TIME seconds for FW to 1340 * complete all its outstanding commands. Returns error if one or more IOs 1341 * are pending after this time period. It also marks the controller dead. 1342 */ 1343static int megasas_wait_for_outstanding(struct megasas_instance *instance) 1344{ 1345 int i; 1346 u32 wait_time = MEGASAS_RESET_WAIT_TIME; 1347 1348 for (i = 0; i < wait_time; i++) { 1349 1350 int outstanding = atomic_read(&instance->fw_outstanding); 1351 1352 if (!outstanding) 1353 break; 1354 1355 if (!(i % MEGASAS_RESET_NOTICE_INTERVAL)) { 1356 printk(KERN_NOTICE "megasas: [%2d]waiting for %d " 1357 "commands to complete\n",i,outstanding); 1358 /* 1359 * Call cmd completion routine. Cmd to be 1360 * be completed directly without depending on isr. 1361 */ 1362 megasas_complete_cmd_dpc((unsigned long)instance); 1363 } 1364 1365 msleep(1000); 1366 } 1367 1368 if (atomic_read(&instance->fw_outstanding)) { 1369 /* 1370 * Send signal to FW to stop processing any pending cmds. 1371 * The controller will be taken offline by the OS now. 1372 */ 1373 if ((instance->pdev->device == 1374 PCI_DEVICE_ID_LSI_SAS0073SKINNY) || 1375 (instance->pdev->device == 1376 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) { 1377 writel(MFI_STOP_ADP, 1378 &instance->reg_set->reserved_0[0]); 1379 } else { 1380 writel(MFI_STOP_ADP, 1381 &instance->reg_set->inbound_doorbell); 1382 } 1383 megasas_dump_pending_frames(instance); 1384 instance->hw_crit_error = 1; 1385 return FAILED; 1386 } 1387 1388 return SUCCESS; 1389} 1390 1391/** 1392 * megasas_generic_reset - Generic reset routine 1393 * @scmd: Mid-layer SCSI command 1394 * 1395 * This routine implements a generic reset handler for device, bus and host 1396 * reset requests. Device, bus and host specific reset handlers can use this 1397 * function after they do their specific tasks. 1398 */ 1399static int megasas_generic_reset(struct scsi_cmnd *scmd) 1400{ 1401 int ret_val; 1402 struct megasas_instance *instance; 1403 1404 instance = (struct megasas_instance *)scmd->device->host->hostdata; 1405 1406 scmd_printk(KERN_NOTICE, scmd, "megasas: RESET -%ld cmd=%x retries=%x\n", 1407 scmd->serial_number, scmd->cmnd[0], scmd->retries); 1408 1409 if (instance->hw_crit_error) { 1410 printk(KERN_ERR "megasas: cannot recover from previous reset " 1411 "failures\n"); 1412 return FAILED; 1413 } 1414 1415 ret_val = megasas_wait_for_outstanding(instance); 1416 if (ret_val == SUCCESS) 1417 printk(KERN_NOTICE "megasas: reset successful \n"); 1418 else 1419 printk(KERN_ERR "megasas: failed to do reset\n"); 1420 1421 return ret_val; 1422} 1423 1424/** 1425 * megasas_reset_timer - quiesce the adapter if required 1426 * @scmd: scsi cmnd 1427 * 1428 * Sets the FW busy flag and reduces the host->can_queue if the 1429 * cmd has not been completed within the timeout period. 1430 */ 1431static enum 1432blk_eh_timer_return megasas_reset_timer(struct scsi_cmnd *scmd) 1433{ 1434 struct megasas_cmd *cmd = (struct megasas_cmd *)scmd->SCp.ptr; 1435 struct megasas_instance *instance; 1436 unsigned long flags; 1437 1438 if (time_after(jiffies, scmd->jiffies_at_alloc + 1439 (MEGASAS_DEFAULT_CMD_TIMEOUT * 2) * HZ)) { 1440 return BLK_EH_NOT_HANDLED; 1441 } 1442 1443 instance = cmd->instance; 1444 if (!(instance->flag & MEGASAS_FW_BUSY)) { 1445 /* FW is busy, throttle IO */ 1446 spin_lock_irqsave(instance->host->host_lock, flags); 1447 1448 instance->host->can_queue = 16; 1449 instance->last_time = jiffies; 1450 instance->flag |= MEGASAS_FW_BUSY; 1451 1452 spin_unlock_irqrestore(instance->host->host_lock, flags); 1453 } 1454 return BLK_EH_RESET_TIMER; 1455} 1456 1457/** 1458 * megasas_reset_device - Device reset handler entry point 1459 */ 1460static int megasas_reset_device(struct scsi_cmnd *scmd) 1461{ 1462 int ret; 1463 1464 /* 1465 * First wait for all commands to complete 1466 */ 1467 ret = megasas_generic_reset(scmd); 1468 1469 return ret; 1470} 1471 1472/** 1473 * megasas_reset_bus_host - Bus & host reset handler entry point 1474 */ 1475static int megasas_reset_bus_host(struct scsi_cmnd *scmd) 1476{ 1477 int ret; 1478 1479 /* 1480 * First wait for all commands to complete 1481 */ 1482 ret = megasas_generic_reset(scmd); 1483 1484 return ret; 1485} 1486 1487/** 1488 * megasas_bios_param - Returns disk geometry for a disk 1489 * @sdev: device handle 1490 * @bdev: block device 1491 * @capacity: drive capacity 1492 * @geom: geometry parameters 1493 */ 1494static int 1495megasas_bios_param(struct scsi_device *sdev, struct block_device *bdev, 1496 sector_t capacity, int geom[]) 1497{ 1498 int heads; 1499 int sectors; 1500 sector_t cylinders; 1501 unsigned long tmp; 1502 /* Default heads (64) & sectors (32) */ 1503 heads = 64; 1504 sectors = 32; 1505 1506 tmp = heads * sectors; 1507 cylinders = capacity; 1508 1509 sector_div(cylinders, tmp); 1510 1511 /* 1512 * Handle extended translation size for logical drives > 1Gb 1513 */ 1514 1515 if (capacity >= 0x200000) { 1516 heads = 255; 1517 sectors = 63; 1518 tmp = heads*sectors; 1519 cylinders = capacity; 1520 sector_div(cylinders, tmp); 1521 } 1522 1523 geom[0] = heads; 1524 geom[1] = sectors; 1525 geom[2] = cylinders; 1526 1527 return 0; 1528} 1529 1530static void megasas_aen_polling(struct work_struct *work); 1531 1532/** 1533 * megasas_service_aen - Processes an event notification 1534 * @instance: Adapter soft state 1535 * @cmd: AEN command completed by the ISR 1536 * 1537 * For AEN, driver sends a command down to FW that is held by the FW till an 1538 * event occurs. When an event of interest occurs, FW completes the command 1539 * that it was previously holding. 1540 * 1541 * This routines sends SIGIO signal to processes that have registered with the 1542 * driver for AEN. 1543 */ 1544static void 1545megasas_service_aen(struct megasas_instance *instance, struct megasas_cmd *cmd) 1546{ 1547 unsigned long flags; 1548 /* 1549 * Don't signal app if it is just an aborted previously registered aen 1550 */ 1551 if ((!cmd->abort_aen) && (instance->unload == 0)) { 1552 spin_lock_irqsave(&poll_aen_lock, flags); 1553 megasas_poll_wait_aen = 1; 1554 spin_unlock_irqrestore(&poll_aen_lock, flags); 1555 wake_up(&megasas_poll_wait); 1556 kill_fasync(&megasas_async_queue, SIGIO, POLL_IN); 1557 } 1558 else 1559 cmd->abort_aen = 0; 1560 1561 instance->aen_cmd = NULL; 1562 megasas_return_cmd(instance, cmd); 1563 1564 if (instance->unload == 0) { 1565 struct megasas_aen_event *ev; 1566 ev = kzalloc(sizeof(*ev), GFP_ATOMIC); 1567 if (!ev) { 1568 printk(KERN_ERR "megasas_service_aen: out of memory\n"); 1569 } else { 1570 ev->instance = instance; 1571 instance->ev = ev; 1572 INIT_WORK(&ev->hotplug_work, megasas_aen_polling); 1573 schedule_delayed_work( 1574 (struct delayed_work *)&ev->hotplug_work, 0); 1575 } 1576 } 1577} 1578 1579/* 1580 * Scsi host template for megaraid_sas driver 1581 */ 1582static struct scsi_host_template megasas_template = { 1583 1584 .module = THIS_MODULE, 1585 .name = "LSI SAS based MegaRAID driver", 1586 .proc_name = "megaraid_sas", 1587 .slave_configure = megasas_slave_configure, 1588 .slave_alloc = megasas_slave_alloc, 1589 .queuecommand = megasas_queue_command, 1590 .eh_device_reset_handler = megasas_reset_device, 1591 .eh_bus_reset_handler = megasas_reset_bus_host, 1592 .eh_host_reset_handler = megasas_reset_bus_host, 1593 .eh_timed_out = megasas_reset_timer, 1594 .bios_param = megasas_bios_param, 1595 .use_clustering = ENABLE_CLUSTERING, 1596}; 1597 1598/** 1599 * megasas_complete_int_cmd - Completes an internal command 1600 * @instance: Adapter soft state 1601 * @cmd: Command to be completed 1602 * 1603 * The megasas_issue_blocked_cmd() function waits for a command to complete 1604 * after it issues a command. This function wakes up that waiting routine by 1605 * calling wake_up() on the wait queue. 1606 */ 1607static void 1608megasas_complete_int_cmd(struct megasas_instance *instance, 1609 struct megasas_cmd *cmd) 1610{ 1611 cmd->cmd_status = cmd->frame->io.cmd_status; 1612 1613 if (cmd->cmd_status == ENODATA) { 1614 cmd->cmd_status = 0; 1615 } 1616 wake_up(&instance->int_cmd_wait_q); 1617} 1618 1619/** 1620 * megasas_complete_abort - Completes aborting a command 1621 * @instance: Adapter soft state 1622 * @cmd: Cmd that was issued to abort another cmd 1623 * 1624 * The megasas_issue_blocked_abort_cmd() function waits on abort_cmd_wait_q 1625 * after it issues an abort on a previously issued command. This function 1626 * wakes up all functions waiting on the same wait queue. 1627 */ 1628static void 1629megasas_complete_abort(struct megasas_instance *instance, 1630 struct megasas_cmd *cmd) 1631{ 1632 if (cmd->sync_cmd) { 1633 cmd->sync_cmd = 0; 1634 cmd->cmd_status = 0; 1635 wake_up(&instance->abort_cmd_wait_q); 1636 } 1637 1638 return; 1639} 1640 1641/** 1642 * megasas_complete_cmd - Completes a command 1643 * @instance: Adapter soft state 1644 * @cmd: Command to be completed 1645 * @alt_status: If non-zero, use this value as status to 1646 * SCSI mid-layer instead of the value returned 1647 * by the FW. This should be used if caller wants 1648 * an alternate status (as in the case of aborted 1649 * commands) 1650 */ 1651static void 1652megasas_complete_cmd(struct megasas_instance *instance, struct megasas_cmd *cmd, 1653 u8 alt_status) 1654{ 1655 int exception = 0; 1656 struct megasas_header *hdr = &cmd->frame->hdr; 1657 unsigned long flags; 1658 1659 if (cmd->scmd) 1660 cmd->scmd->SCp.ptr = NULL; 1661 1662 switch (hdr->cmd) { 1663 1664 case MFI_CMD_PD_SCSI_IO: 1665 case MFI_CMD_LD_SCSI_IO: 1666 1667 /* 1668 * MFI_CMD_PD_SCSI_IO and MFI_CMD_LD_SCSI_IO could have been 1669 * issued either through an IO path or an IOCTL path. If it 1670 * was via IOCTL, we will send it to internal completion. 1671 */ 1672 if (cmd->sync_cmd) { 1673 cmd->sync_cmd = 0; 1674 megasas_complete_int_cmd(instance, cmd); 1675 break; 1676 } 1677 1678 case MFI_CMD_LD_READ: 1679 case MFI_CMD_LD_WRITE: 1680 1681 if (alt_status) { 1682 cmd->scmd->result = alt_status << 16; 1683 exception = 1; 1684 } 1685 1686 if (exception) { 1687 1688 atomic_dec(&instance->fw_outstanding); 1689 1690 scsi_dma_unmap(cmd->scmd); 1691 cmd->scmd->scsi_done(cmd->scmd); 1692 megasas_return_cmd(instance, cmd); 1693 1694 break; 1695 } 1696 1697 switch (hdr->cmd_status) { 1698 1699 case MFI_STAT_OK: 1700 cmd->scmd->result = DID_OK << 16; 1701 break; 1702 1703 case MFI_STAT_SCSI_IO_FAILED: 1704 case MFI_STAT_LD_INIT_IN_PROGRESS: 1705 cmd->scmd->result = 1706 (DID_ERROR << 16) | hdr->scsi_status; 1707 break; 1708 1709 case MFI_STAT_SCSI_DONE_WITH_ERROR: 1710 1711 cmd->scmd->result = (DID_OK << 16) | hdr->scsi_status; 1712 1713 if (hdr->scsi_status == SAM_STAT_CHECK_CONDITION) { 1714 memset(cmd->scmd->sense_buffer, 0, 1715 SCSI_SENSE_BUFFERSIZE); 1716 memcpy(cmd->scmd->sense_buffer, cmd->sense, 1717 hdr->sense_len); 1718 1719 cmd->scmd->result |= DRIVER_SENSE << 24; 1720 } 1721 1722 break; 1723 1724 case MFI_STAT_LD_OFFLINE: 1725 case MFI_STAT_DEVICE_NOT_FOUND: 1726 cmd->scmd->result = DID_BAD_TARGET << 16; 1727 break; 1728 1729 default: 1730 printk(KERN_DEBUG "megasas: MFI FW status %#x\n", 1731 hdr->cmd_status); 1732 cmd->scmd->result = DID_ERROR << 16; 1733 break; 1734 } 1735 1736 atomic_dec(&instance->fw_outstanding); 1737 1738 scsi_dma_unmap(cmd->scmd); 1739 cmd->scmd->scsi_done(cmd->scmd); 1740 megasas_return_cmd(instance, cmd); 1741 1742 break; 1743 1744 case MFI_CMD_SMP: 1745 case MFI_CMD_STP: 1746 case MFI_CMD_DCMD: 1747 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET_INFO || 1748 cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_GET) { 1749 spin_lock_irqsave(&poll_aen_lock, flags); 1750 megasas_poll_wait_aen = 0; 1751 spin_unlock_irqrestore(&poll_aen_lock, flags); 1752 } 1753 1754 /* 1755 * See if got an event notification 1756 */ 1757 if (cmd->frame->dcmd.opcode == MR_DCMD_CTRL_EVENT_WAIT) 1758 megasas_service_aen(instance, cmd); 1759 else 1760 megasas_complete_int_cmd(instance, cmd); 1761 1762 break; 1763 1764 case MFI_CMD_ABORT: 1765 /* 1766 * Cmd issued to abort another cmd returned 1767 */ 1768 megasas_complete_abort(instance, cmd); 1769 break; 1770 1771 default: 1772 printk("megasas: Unknown command completed! [0x%X]\n", 1773 hdr->cmd); 1774 break; 1775 } 1776} 1777 1778/** 1779 * megasas_deplete_reply_queue - Processes all completed commands 1780 * @instance: Adapter soft state 1781 * @alt_status: Alternate status to be returned to 1782 * SCSI mid-layer instead of the status 1783 * returned by the FW 1784 */ 1785static int 1786megasas_deplete_reply_queue(struct megasas_instance *instance, u8 alt_status) 1787{ 1788 /* 1789 * Check if it is our interrupt 1790 * Clear the interrupt 1791 */ 1792 if(instance->instancet->clear_intr(instance->reg_set)) 1793 return IRQ_NONE; 1794 1795 if (instance->hw_crit_error) 1796 goto out_done; 1797 /* 1798 * Schedule the tasklet for cmd completion 1799 */ 1800 tasklet_schedule(&instance->isr_tasklet); 1801out_done: 1802 return IRQ_HANDLED; 1803} 1804 1805/** 1806 * megasas_isr - isr entry point 1807 */ 1808static irqreturn_t megasas_isr(int irq, void *devp) 1809{ 1810 return megasas_deplete_reply_queue((struct megasas_instance *)devp, 1811 DID_OK); 1812} 1813 1814/** 1815 * megasas_transition_to_ready - Move the FW to READY state 1816 * @instance: Adapter soft state 1817 * 1818 * During the initialization, FW passes can potentially be in any one of 1819 * several possible states. If the FW in operational, waiting-for-handshake 1820 * states, driver must take steps to bring it to ready state. Otherwise, it 1821 * has to wait for the ready state. 1822 */ 1823static int 1824megasas_transition_to_ready(struct megasas_instance* instance) 1825{ 1826 int i; 1827 u8 max_wait; 1828 u32 fw_state; 1829 u32 cur_state; 1830 u32 abs_state, curr_abs_state; 1831 1832 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & MFI_STATE_MASK; 1833 1834 if (fw_state != MFI_STATE_READY) 1835 printk(KERN_INFO "megasas: Waiting for FW to come to ready" 1836 " state\n"); 1837 1838 while (fw_state != MFI_STATE_READY) { 1839 1840 abs_state = 1841 instance->instancet->read_fw_status_reg(instance->reg_set); 1842 1843 switch (fw_state) { 1844 1845 case MFI_STATE_FAULT: 1846 1847 printk(KERN_DEBUG "megasas: FW in FAULT state!!\n"); 1848 return -ENODEV; 1849 1850 case MFI_STATE_WAIT_HANDSHAKE: 1851 /* 1852 * Set the CLR bit in inbound doorbell 1853 */ 1854 if ((instance->pdev->device == 1855 PCI_DEVICE_ID_LSI_SAS0073SKINNY) || 1856 (instance->pdev->device == 1857 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) { 1858 1859 writel( 1860 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG, 1861 &instance->reg_set->reserved_0[0]); 1862 } else { 1863 writel( 1864 MFI_INIT_CLEAR_HANDSHAKE|MFI_INIT_HOTPLUG, 1865 &instance->reg_set->inbound_doorbell); 1866 } 1867 1868 max_wait = MEGASAS_RESET_WAIT_TIME; 1869 cur_state = MFI_STATE_WAIT_HANDSHAKE; 1870 break; 1871 1872 case MFI_STATE_BOOT_MESSAGE_PENDING: 1873 if ((instance->pdev->device == 1874 PCI_DEVICE_ID_LSI_SAS0073SKINNY) || 1875 (instance->pdev->device == 1876 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) { 1877 writel(MFI_INIT_HOTPLUG, 1878 &instance->reg_set->reserved_0[0]); 1879 } else 1880 writel(MFI_INIT_HOTPLUG, 1881 &instance->reg_set->inbound_doorbell); 1882 1883 max_wait = MEGASAS_RESET_WAIT_TIME; 1884 cur_state = MFI_STATE_BOOT_MESSAGE_PENDING; 1885 break; 1886 1887 case MFI_STATE_OPERATIONAL: 1888 /* 1889 * Bring it to READY state; assuming max wait 10 secs 1890 */ 1891 instance->instancet->disable_intr(instance->reg_set); 1892 if ((instance->pdev->device == 1893 PCI_DEVICE_ID_LSI_SAS0073SKINNY) || 1894 (instance->pdev->device == 1895 PCI_DEVICE_ID_LSI_SAS0071SKINNY)) { 1896 writel(MFI_RESET_FLAGS, 1897 &instance->reg_set->reserved_0[0]); 1898 } else 1899 writel(MFI_RESET_FLAGS, 1900 &instance->reg_set->inbound_doorbell); 1901 1902 max_wait = MEGASAS_RESET_WAIT_TIME; 1903 cur_state = MFI_STATE_OPERATIONAL; 1904 break; 1905 1906 case MFI_STATE_UNDEFINED: 1907 /* 1908 * This state should not last for more than 2 seconds 1909 */ 1910 max_wait = MEGASAS_RESET_WAIT_TIME; 1911 cur_state = MFI_STATE_UNDEFINED; 1912 break; 1913 1914 case MFI_STATE_BB_INIT: 1915 max_wait = MEGASAS_RESET_WAIT_TIME; 1916 cur_state = MFI_STATE_BB_INIT; 1917 break; 1918 1919 case MFI_STATE_FW_INIT: 1920 max_wait = MEGASAS_RESET_WAIT_TIME; 1921 cur_state = MFI_STATE_FW_INIT; 1922 break; 1923 1924 case MFI_STATE_FW_INIT_2: 1925 max_wait = MEGASAS_RESET_WAIT_TIME; 1926 cur_state = MFI_STATE_FW_INIT_2; 1927 break; 1928 1929 case MFI_STATE_DEVICE_SCAN: 1930 max_wait = MEGASAS_RESET_WAIT_TIME; 1931 cur_state = MFI_STATE_DEVICE_SCAN; 1932 break; 1933 1934 case MFI_STATE_FLUSH_CACHE: 1935 max_wait = MEGASAS_RESET_WAIT_TIME; 1936 cur_state = MFI_STATE_FLUSH_CACHE; 1937 break; 1938 1939 default: 1940 printk(KERN_DEBUG "megasas: Unknown state 0x%x\n", 1941 fw_state); 1942 return -ENODEV; 1943 } 1944 1945 /* 1946 * The cur_state should not last for more than max_wait secs 1947 */ 1948 for (i = 0; i < (max_wait * 1000); i++) { 1949 fw_state = instance->instancet->read_fw_status_reg(instance->reg_set) & 1950 MFI_STATE_MASK ; 1951 curr_abs_state = 1952 instance->instancet->read_fw_status_reg(instance->reg_set); 1953 1954 if (abs_state == curr_abs_state) { 1955 msleep(1); 1956 } else 1957 break; 1958 } 1959 1960 /* 1961 * Return error if fw_state hasn't changed after max_wait 1962 */ 1963 if (curr_abs_state == abs_state) { 1964 printk(KERN_DEBUG "FW state [%d] hasn't changed " 1965 "in %d secs\n", fw_state, max_wait); 1966 return -ENODEV; 1967 } 1968 }; 1969 printk(KERN_INFO "megasas: FW now in Ready state\n"); 1970 1971 return 0; 1972} 1973 1974/** 1975 * megasas_teardown_frame_pool - Destroy the cmd frame DMA pool 1976 * @instance: Adapter soft state 1977 */ 1978static void megasas_teardown_frame_pool(struct megasas_instance *instance) 1979{ 1980 int i; 1981 u32 max_cmd = instance->max_fw_cmds; 1982 struct megasas_cmd *cmd; 1983 1984 if (!instance->frame_dma_pool) 1985 return; 1986 1987 /* 1988 * Return all frames to pool 1989 */ 1990 for (i = 0; i < max_cmd; i++) { 1991 1992 cmd = instance->cmd_list[i]; 1993 1994 if (cmd->frame) 1995 pci_pool_free(instance->frame_dma_pool, cmd->frame, 1996 cmd->frame_phys_addr); 1997 1998 if (cmd->sense) 1999 pci_pool_free(instance->sense_dma_pool, cmd->sense, 2000 cmd->sense_phys_addr); 2001 } 2002 2003 /* 2004 * Now destroy the pool itself 2005 */ 2006 pci_pool_destroy(instance->frame_dma_pool); 2007 pci_pool_destroy(instance->sense_dma_pool); 2008 2009 instance->frame_dma_pool = NULL; 2010 instance->sense_dma_pool = NULL; 2011} 2012 2013/** 2014 * megasas_create_frame_pool - Creates DMA pool for cmd frames 2015 * @instance: Adapter soft state 2016 * 2017 * Each command packet has an embedded DMA memory buffer that is used for 2018 * filling MFI frame and the SG list that immediately follows the frame. This 2019 * function creates those DMA memory buffers for each command packet by using 2020 * PCI pool facility. 2021 */ 2022static int megasas_create_frame_pool(struct megasas_instance *instance) 2023{ 2024 int i; 2025 u32 max_cmd; 2026 u32 sge_sz; 2027 u32 sgl_sz; 2028 u32 total_sz; 2029 u32 frame_count; 2030 struct megasas_cmd *cmd; 2031 2032 max_cmd = instance->max_fw_cmds; 2033 2034 /* 2035 * Size of our frame is 64 bytes for MFI frame, followed by max SG 2036 * elements and finally SCSI_SENSE_BUFFERSIZE bytes for sense buffer 2037 */ 2038 sge_sz = (IS_DMA64) ? sizeof(struct megasas_sge64) : 2039 sizeof(struct megasas_sge32); 2040 2041 if (instance->flag_ieee) { 2042 sge_sz = sizeof(struct megasas_sge_skinny); 2043 } 2044 2045 /* 2046 * Calculated the number of 64byte frames required for SGL 2047 */ 2048 sgl_sz = sge_sz * instance->max_num_sge; 2049 frame_count = (sgl_sz + MEGAMFI_FRAME_SIZE - 1) / MEGAMFI_FRAME_SIZE; 2050 2051 /* 2052 * We need one extra frame for the MFI command 2053 */ 2054 frame_count++; 2055 2056 total_sz = MEGAMFI_FRAME_SIZE * frame_count; 2057 /* 2058 * Use DMA pool facility provided by PCI layer 2059 */ 2060 instance->frame_dma_pool = pci_pool_create("megasas frame pool", 2061 instance->pdev, total_sz, 64, 2062 0); 2063 2064 if (!instance->frame_dma_pool) { 2065 printk(KERN_DEBUG "megasas: failed to setup frame pool\n"); 2066 return -ENOMEM; 2067 } 2068 2069 instance->sense_dma_pool = pci_pool_create("megasas sense pool", 2070 instance->pdev, 128, 4, 0); 2071 2072 if (!instance->sense_dma_pool) { 2073 printk(KERN_DEBUG "megasas: failed to setup sense pool\n"); 2074 2075 pci_pool_destroy(instance->frame_dma_pool); 2076 instance->frame_dma_pool = NULL; 2077 2078 return -ENOMEM; 2079 } 2080 2081 /* 2082 * Allocate and attach a frame to each of the commands in cmd_list. 2083 * By making cmd->index as the context instead of the &cmd, we can 2084 * always use 32bit context regardless of the architecture 2085 */ 2086 for (i = 0; i < max_cmd; i++) { 2087 2088 cmd = instance->cmd_list[i]; 2089 2090 cmd->frame = pci_pool_alloc(instance->frame_dma_pool, 2091 GFP_KERNEL, &cmd->frame_phys_addr); 2092 2093 cmd->sense = pci_pool_alloc(instance->sense_dma_pool, 2094 GFP_KERNEL, &cmd->sense_phys_addr); 2095 2096 /* 2097 * megasas_teardown_frame_pool() takes care of freeing 2098 * whatever has been allocated 2099 */ 2100 if (!cmd->frame || !cmd->sense) { 2101 printk(KERN_DEBUG "megasas: pci_pool_alloc failed \n"); 2102 megasas_teardown_frame_pool(instance); 2103 return -ENOMEM; 2104 } 2105 2106 cmd->frame->io.context = cmd->index; 2107 cmd->frame->io.pad_0 = 0; 2108 } 2109 2110 return 0; 2111} 2112 2113/** 2114 * megasas_free_cmds - Free all the cmds in the free cmd pool 2115 * @instance: Adapter soft state 2116 */ 2117static void megasas_free_cmds(struct megasas_instance *instance) 2118{ 2119 int i; 2120 /* First free the MFI frame pool */ 2121 megasas_teardown_frame_pool(instance); 2122 2123 /* Free all the commands in the cmd_list */ 2124 for (i = 0; i < instance->max_fw_cmds; i++) 2125 kfree(instance->cmd_list[i]); 2126 2127 /* Free the cmd_list buffer itself */ 2128 kfree(instance->cmd_list); 2129 instance->cmd_list = NULL; 2130 2131 INIT_LIST_HEAD(&instance->cmd_pool); 2132} 2133 2134/** 2135 * megasas_alloc_cmds - Allocates the command packets 2136 * @instance: Adapter soft state 2137 * 2138 * Each command that is issued to the FW, whether IO commands from the OS or 2139 * internal commands like IOCTLs, are wrapped in local data structure called 2140 * megasas_cmd. The frame embedded in this megasas_cmd is actually issued to 2141 * the FW. 2142 * 2143 * Each frame has a 32-bit field called context (tag). This context is used 2144 * to get back the megasas_cmd from the frame when a frame gets completed in 2145 * the ISR. Typically the address of the megasas_cmd itself would be used as 2146 * the context. But we wanted to keep the differences between 32 and 64 bit 2147 * systems to the mininum. We always use 32 bit integers for the context. In 2148 * this driver, the 32 bit values are the indices into an array cmd_list. 2149 * This array is used only to look up the megasas_cmd given the context. The 2150 * free commands themselves are maintained in a linked list called cmd_pool. 2151 */ 2152static int megasas_alloc_cmds(struct megasas_instance *instance) 2153{ 2154 int i; 2155 int j; 2156 u32 max_cmd; 2157 struct megasas_cmd *cmd; 2158 2159 max_cmd = instance->max_fw_cmds; 2160 2161 /* 2162 * instance->cmd_list is an array of struct megasas_cmd pointers. 2163 * Allocate the dynamic array first and then allocate individual 2164 * commands. 2165 */ 2166 instance->cmd_list = kcalloc(max_cmd, sizeof(struct megasas_cmd*), GFP_KERNEL); 2167 2168 if (!instance->cmd_list) { 2169 printk(KERN_DEBUG "megasas: out of memory\n"); 2170 return -ENOMEM; 2171 } 2172 2173 2174 for (i = 0; i < max_cmd; i++) { 2175 instance->cmd_list[i] = kmalloc(sizeof(struct megasas_cmd), 2176 GFP_KERNEL); 2177 2178 if (!instance->cmd_list[i]) { 2179 2180 for (j = 0; j < i; j++) 2181 kfree(instance->cmd_list[j]); 2182 2183 kfree(instance->cmd_list); 2184 instance->cmd_list = NULL; 2185 2186 return -ENOMEM; 2187 } 2188 } 2189 2190 /* 2191 * Add all the commands to command pool (instance->cmd_pool) 2192 */ 2193 for (i = 0; i < max_cmd; i++) { 2194 cmd = instance->cmd_list[i]; 2195 memset(cmd, 0, sizeof(struct megasas_cmd)); 2196 cmd->index = i; 2197 cmd->instance = instance; 2198 2199 list_add_tail(&cmd->list, &instance->cmd_pool); 2200 } 2201 2202 /* 2203 * Create a frame pool and assign one frame to each cmd 2204 */ 2205 if (megasas_create_frame_pool(instance)) { 2206 printk(KERN_DEBUG "megasas: Error creating frame DMA pool\n"); 2207 megasas_free_cmds(instance); 2208 } 2209 2210 return 0; 2211} 2212 2213/* 2214 * megasas_get_pd_list_info - Returns FW's pd_list structure 2215 * @instance: Adapter soft state 2216 * @pd_list: pd_list structure 2217 * 2218 * Issues an internal command (DCMD) to get the FW's controller PD 2219 * list structure. This information is mainly used to find out SYSTEM 2220 * supported by the FW. 2221 */ 2222static int 2223megasas_get_pd_list(struct megasas_instance *instance) 2224{ 2225 int ret = 0, pd_index = 0; 2226 struct megasas_cmd *cmd; 2227 struct megasas_dcmd_frame *dcmd; 2228 struct MR_PD_LIST *ci; 2229 struct MR_PD_ADDRESS *pd_addr; 2230 dma_addr_t ci_h = 0; 2231 2232 cmd = megasas_get_cmd(instance); 2233 2234 if (!cmd) { 2235 printk(KERN_DEBUG "megasas (get_pd_list): Failed to get cmd\n"); 2236 return -ENOMEM; 2237 } 2238 2239 dcmd = &cmd->frame->dcmd; 2240 2241 ci = pci_alloc_consistent(instance->pdev, 2242 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), &ci_h); 2243 2244 if (!ci) { 2245 printk(KERN_DEBUG "Failed to alloc mem for pd_list\n"); 2246 megasas_return_cmd(instance, cmd); 2247 return -ENOMEM; 2248 } 2249 2250 memset(ci, 0, sizeof(*ci)); 2251 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE); 2252 2253 dcmd->mbox.b[0] = MR_PD_QUERY_TYPE_EXPOSED_TO_HOST; 2254 dcmd->mbox.b[1] = 0; 2255 dcmd->cmd = MFI_CMD_DCMD; 2256 dcmd->cmd_status = 0xFF; 2257 dcmd->sge_count = 1; 2258 dcmd->flags = MFI_FRAME_DIR_READ; 2259 dcmd->timeout = 0; 2260 dcmd->pad_0 = 0; 2261 dcmd->data_xfer_len = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST); 2262 dcmd->opcode = MR_DCMD_PD_LIST_QUERY; 2263 dcmd->sgl.sge32[0].phys_addr = ci_h; 2264 dcmd->sgl.sge32[0].length = MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST); 2265 2266 if (!megasas_issue_polled(instance, cmd)) { 2267 ret = 0; 2268 } else { 2269 ret = -1; 2270 } 2271 2272 /* 2273 * the following function will get the instance PD LIST. 2274 */ 2275 2276 pd_addr = ci->addr; 2277 2278 if ( ret == 0 && 2279 (ci->count < 2280 (MEGASAS_MAX_PD_CHANNELS * MEGASAS_MAX_DEV_PER_CHANNEL))) { 2281 2282 memset(instance->pd_list, 0, 2283 MEGASAS_MAX_PD * sizeof(struct megasas_pd_list)); 2284 2285 for (pd_index = 0; pd_index < ci->count; pd_index++) { 2286 2287 instance->pd_list[pd_addr->deviceId].tid = 2288 pd_addr->deviceId; 2289 instance->pd_list[pd_addr->deviceId].driveType = 2290 pd_addr->scsiDevType; 2291 instance->pd_list[pd_addr->deviceId].driveState = 2292 MR_PD_STATE_SYSTEM; 2293 pd_addr++; 2294 } 2295 } 2296 2297 pci_free_consistent(instance->pdev, 2298 MEGASAS_MAX_PD * sizeof(struct MR_PD_LIST), 2299 ci, ci_h); 2300 megasas_return_cmd(instance, cmd); 2301 2302 return ret; 2303} 2304 2305/* 2306 * megasas_get_ld_list_info - Returns FW's ld_list structure 2307 * @instance: Adapter soft state 2308 * @ld_list: ld_list structure 2309 * 2310 * Issues an internal command (DCMD) to get the FW's controller PD 2311 * list structure. This information is mainly used to find out SYSTEM 2312 * supported by the FW. 2313 */ 2314static int 2315megasas_get_ld_list(struct megasas_instance *instance) 2316{ 2317 int ret = 0, ld_index = 0, ids = 0; 2318 struct megasas_cmd *cmd; 2319 struct megasas_dcmd_frame *dcmd; 2320 struct MR_LD_LIST *ci; 2321 dma_addr_t ci_h = 0; 2322 2323 cmd = megasas_get_cmd(instance); 2324 2325 if (!cmd) { 2326 printk(KERN_DEBUG "megasas_get_ld_list: Failed to get cmd\n"); 2327 return -ENOMEM; 2328 } 2329 2330 dcmd = &cmd->frame->dcmd; 2331 2332 ci = pci_alloc_consistent(instance->pdev, 2333 sizeof(struct MR_LD_LIST), 2334 &ci_h); 2335 2336 if (!ci) { 2337 printk(KERN_DEBUG "Failed to alloc mem in get_ld_list\n"); 2338 megasas_return_cmd(instance, cmd); 2339 return -ENOMEM; 2340 } 2341 2342 memset(ci, 0, sizeof(*ci)); 2343 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE); 2344 2345 dcmd->cmd = MFI_CMD_DCMD; 2346 dcmd->cmd_status = 0xFF; 2347 dcmd->sge_count = 1; 2348 dcmd->flags = MFI_FRAME_DIR_READ; 2349 dcmd->timeout = 0; 2350 dcmd->data_xfer_len = sizeof(struct MR_LD_LIST); 2351 dcmd->opcode = MR_DCMD_LD_GET_LIST; 2352 dcmd->sgl.sge32[0].phys_addr = ci_h; 2353 dcmd->sgl.sge32[0].length = sizeof(struct MR_LD_LIST); 2354 dcmd->pad_0 = 0; 2355 2356 if (!megasas_issue_polled(instance, cmd)) { 2357 ret = 0; 2358 } else { 2359 ret = -1; 2360 } 2361 2362 /* the following function will get the instance PD LIST */ 2363 2364 if ((ret == 0) && (ci->ldCount < MAX_LOGICAL_DRIVES)) { 2365 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS); 2366 2367 for (ld_index = 0; ld_index < ci->ldCount; ld_index++) { 2368 if (ci->ldList[ld_index].state != 0) { 2369 ids = ci->ldList[ld_index].ref.targetId; 2370 instance->ld_ids[ids] = 2371 ci->ldList[ld_index].ref.targetId; 2372 } 2373 } 2374 } 2375 2376 pci_free_consistent(instance->pdev, 2377 sizeof(struct MR_LD_LIST), 2378 ci, 2379 ci_h); 2380 2381 megasas_return_cmd(instance, cmd); 2382 return ret; 2383} 2384 2385/** 2386 * megasas_get_controller_info - Returns FW's controller structure 2387 * @instance: Adapter soft state 2388 * @ctrl_info: Controller information structure 2389 * 2390 * Issues an internal command (DCMD) to get the FW's controller structure. 2391 * This information is mainly used to find out the maximum IO transfer per 2392 * command supported by the FW. 2393 */ 2394static int 2395megasas_get_ctrl_info(struct megasas_instance *instance, 2396 struct megasas_ctrl_info *ctrl_info) 2397{ 2398 int ret = 0; 2399 struct megasas_cmd *cmd; 2400 struct megasas_dcmd_frame *dcmd; 2401 struct megasas_ctrl_info *ci; 2402 dma_addr_t ci_h = 0; 2403 2404 cmd = megasas_get_cmd(instance); 2405 2406 if (!cmd) { 2407 printk(KERN_DEBUG "megasas: Failed to get a free cmd\n"); 2408 return -ENOMEM; 2409 } 2410 2411 dcmd = &cmd->frame->dcmd; 2412 2413 ci = pci_alloc_consistent(instance->pdev, 2414 sizeof(struct megasas_ctrl_info), &ci_h); 2415 2416 if (!ci) { 2417 printk(KERN_DEBUG "Failed to alloc mem for ctrl info\n"); 2418 megasas_return_cmd(instance, cmd); 2419 return -ENOMEM; 2420 } 2421 2422 memset(ci, 0, sizeof(*ci)); 2423 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE); 2424 2425 dcmd->cmd = MFI_CMD_DCMD; 2426 dcmd->cmd_status = 0xFF; 2427 dcmd->sge_count = 1; 2428 dcmd->flags = MFI_FRAME_DIR_READ; 2429 dcmd->timeout = 0; 2430 dcmd->pad_0 = 0; 2431 dcmd->data_xfer_len = sizeof(struct megasas_ctrl_info); 2432 dcmd->opcode = MR_DCMD_CTRL_GET_INFO; 2433 dcmd->sgl.sge32[0].phys_addr = ci_h; 2434 dcmd->sgl.sge32[0].length = sizeof(struct megasas_ctrl_info); 2435 2436 if (!megasas_issue_polled(instance, cmd)) { 2437 ret = 0; 2438 memcpy(ctrl_info, ci, sizeof(struct megasas_ctrl_info)); 2439 } else { 2440 ret = -1; 2441 } 2442 2443 pci_free_consistent(instance->pdev, sizeof(struct megasas_ctrl_info), 2444 ci, ci_h); 2445 2446 megasas_return_cmd(instance, cmd); 2447 return ret; 2448} 2449 2450/** 2451 * megasas_issue_init_mfi - Initializes the FW 2452 * @instance: Adapter soft state 2453 * 2454 * Issues the INIT MFI cmd 2455 */ 2456static int 2457megasas_issue_init_mfi(struct megasas_instance *instance) 2458{ 2459 u32 context; 2460 2461 struct megasas_cmd *cmd; 2462 2463 struct megasas_init_frame *init_frame; 2464 struct megasas_init_queue_info *initq_info; 2465 dma_addr_t init_frame_h; 2466 dma_addr_t initq_info_h; 2467 2468 /* 2469 * Prepare a init frame. Note the init frame points to queue info 2470 * structure. Each frame has SGL allocated after first 64 bytes. For 2471 * this frame - since we don't need any SGL - we use SGL's space as 2472 * queue info structure 2473 * 2474 * We will not get a NULL command below. We just created the pool. 2475 */ 2476 cmd = megasas_get_cmd(instance); 2477 2478 init_frame = (struct megasas_init_frame *)cmd->frame; 2479 initq_info = (struct megasas_init_queue_info *) 2480 ((unsigned long)init_frame + 64); 2481 2482 init_frame_h = cmd->frame_phys_addr; 2483 initq_info_h = init_frame_h + 64; 2484 2485 context = init_frame->context; 2486 memset(init_frame, 0, MEGAMFI_FRAME_SIZE); 2487 memset(initq_info, 0, sizeof(struct megasas_init_queue_info)); 2488 init_frame->context = context; 2489 2490 initq_info->reply_queue_entries = instance->max_fw_cmds + 1; 2491 initq_info->reply_queue_start_phys_addr_lo = instance->reply_queue_h; 2492 2493 initq_info->producer_index_phys_addr_lo = instance->producer_h; 2494 initq_info->consumer_index_phys_addr_lo = instance->consumer_h; 2495 2496 init_frame->cmd = MFI_CMD_INIT; 2497 init_frame->cmd_status = 0xFF; 2498 init_frame->queue_info_new_phys_addr_lo = initq_info_h; 2499 2500 init_frame->data_xfer_len = sizeof(struct megasas_init_queue_info); 2501 2502 /* 2503 * disable the intr before firing the init frame to FW 2504 */ 2505 instance->instancet->disable_intr(instance->reg_set); 2506 2507 /* 2508 * Issue the init frame in polled mode 2509 */ 2510 2511 if (megasas_issue_polled(instance, cmd)) { 2512 printk(KERN_ERR "megasas: Failed to init firmware\n"); 2513 megasas_return_cmd(instance, cmd); 2514 goto fail_fw_init; 2515 } 2516 2517 megasas_return_cmd(instance, cmd); 2518 2519 return 0; 2520 2521fail_fw_init: 2522 return -EINVAL; 2523} 2524 2525/** 2526 * megasas_start_timer - Initializes a timer object 2527 * @instance: Adapter soft state 2528 * @timer: timer object to be initialized 2529 * @fn: timer function 2530 * @interval: time interval between timer function call 2531 */ 2532static inline void 2533megasas_start_timer(struct megasas_instance *instance, 2534 struct timer_list *timer, 2535 void *fn, unsigned long interval) 2536{ 2537 init_timer(timer); 2538 timer->expires = jiffies + interval; 2539 timer->data = (unsigned long)instance; 2540 timer->function = fn; 2541 add_timer(timer); 2542} 2543 2544/** 2545 * megasas_io_completion_timer - Timer fn 2546 * @instance_addr: Address of adapter soft state 2547 * 2548 * Schedules tasklet for cmd completion 2549 * if poll_mode_io is set 2550 */ 2551static void 2552megasas_io_completion_timer(unsigned long instance_addr) 2553{ 2554 struct megasas_instance *instance = 2555 (struct megasas_instance *)instance_addr; 2556 2557 if (atomic_read(&instance->fw_outstanding)) 2558 tasklet_schedule(&instance->isr_tasklet); 2559 2560 /* Restart timer */ 2561 if (poll_mode_io) 2562 mod_timer(&instance->io_completion_timer, 2563 jiffies + MEGASAS_COMPLETION_TIMER_INTERVAL); 2564} 2565 2566/** 2567 * megasas_init_mfi - Initializes the FW 2568 * @instance: Adapter soft state 2569 * 2570 * This is the main function for initializing MFI firmware. 2571 */ 2572static int megasas_init_mfi(struct megasas_instance *instance) 2573{ 2574 u32 context_sz; 2575 u32 reply_q_sz; 2576 u32 max_sectors_1; 2577 u32 max_sectors_2; 2578 u32 tmp_sectors; 2579 struct megasas_register_set __iomem *reg_set; 2580 struct megasas_ctrl_info *ctrl_info; 2581 /* 2582 * Map the message registers 2583 */ 2584 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS1078GEN2) || 2585 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY) || 2586 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) || 2587 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0079GEN2)) { 2588 instance->base_addr = pci_resource_start(instance->pdev, 1); 2589 } else { 2590 instance->base_addr = pci_resource_start(instance->pdev, 0); 2591 } 2592 2593 if (pci_request_selected_regions(instance->pdev, 2594 pci_select_bars(instance->pdev, IORESOURCE_MEM), 2595 "megasas: LSI")) { 2596 printk(KERN_DEBUG "megasas: IO memory region busy!\n"); 2597 return -EBUSY; 2598 } 2599 2600 instance->reg_set = ioremap_nocache(instance->base_addr, 8192); 2601 2602 if (!instance->reg_set) { 2603 printk(KERN_DEBUG "megasas: Failed to map IO mem\n"); 2604 goto fail_ioremap; 2605 } 2606 2607 reg_set = instance->reg_set; 2608 2609 switch(instance->pdev->device) 2610 { 2611 case PCI_DEVICE_ID_LSI_SAS1078R: 2612 case PCI_DEVICE_ID_LSI_SAS1078DE: 2613 instance->instancet = &megasas_instance_template_ppc; 2614 break; 2615 case PCI_DEVICE_ID_LSI_SAS1078GEN2: 2616 case PCI_DEVICE_ID_LSI_SAS0079GEN2: 2617 instance->instancet = &megasas_instance_template_gen2; 2618 break; 2619 case PCI_DEVICE_ID_LSI_SAS0073SKINNY: 2620 case PCI_DEVICE_ID_LSI_SAS0071SKINNY: 2621 instance->instancet = &megasas_instance_template_skinny; 2622 break; 2623 case PCI_DEVICE_ID_LSI_SAS1064R: 2624 case PCI_DEVICE_ID_DELL_PERC5: 2625 default: 2626 instance->instancet = &megasas_instance_template_xscale; 2627 break; 2628 } 2629 2630 /* 2631 * We expect the FW state to be READY 2632 */ 2633 if (megasas_transition_to_ready(instance)) 2634 goto fail_ready_state; 2635 2636 /* 2637 * Get various operational parameters from status register 2638 */ 2639 instance->max_fw_cmds = instance->instancet->read_fw_status_reg(reg_set) & 0x00FFFF; 2640 /* 2641 * Reduce the max supported cmds by 1. This is to ensure that the 2642 * reply_q_sz (1 more than the max cmd that driver may send) 2643 * does not exceed max cmds that the FW can support 2644 */ 2645 instance->max_fw_cmds = instance->max_fw_cmds-1; 2646 instance->max_num_sge = (instance->instancet->read_fw_status_reg(reg_set) & 0xFF0000) >> 2647 0x10; 2648 /* 2649 * Create a pool of commands 2650 */ 2651 if (megasas_alloc_cmds(instance)) 2652 goto fail_alloc_cmds; 2653 2654 /* 2655 * Allocate memory for reply queue. Length of reply queue should 2656 * be _one_ more than the maximum commands handled by the firmware. 2657 * 2658 * Note: When FW completes commands, it places corresponding contex 2659 * values in this circular reply queue. This circular queue is a fairly 2660 * typical producer-consumer queue. FW is the producer (of completed 2661 * commands) and the driver is the consumer. 2662 */ 2663 context_sz = sizeof(u32); 2664 reply_q_sz = context_sz * (instance->max_fw_cmds + 1); 2665 2666 instance->reply_queue = pci_alloc_consistent(instance->pdev, 2667 reply_q_sz, 2668 &instance->reply_queue_h); 2669 2670 if (!instance->reply_queue) { 2671 printk(KERN_DEBUG "megasas: Out of DMA mem for reply queue\n"); 2672 goto fail_reply_queue; 2673 } 2674 2675 if (megasas_issue_init_mfi(instance)) 2676 goto fail_fw_init; 2677 2678 memset(instance->pd_list, 0 , 2679 (MEGASAS_MAX_PD * sizeof(struct megasas_pd_list))); 2680 megasas_get_pd_list(instance); 2681 2682 memset(instance->ld_ids, 0xff, MEGASAS_MAX_LD_IDS); 2683 megasas_get_ld_list(instance); 2684 2685 ctrl_info = kmalloc(sizeof(struct megasas_ctrl_info), GFP_KERNEL); 2686 2687 /* 2688 * Compute the max allowed sectors per IO: The controller info has two 2689 * limits on max sectors. Driver should use the minimum of these two. 2690 * 2691 * 1 << stripe_sz_ops.min = max sectors per strip 2692 * 2693 * Note that older firmwares ( < FW ver 30) didn't report information 2694 * to calculate max_sectors_1. So the number ended up as zero always. 2695 */ 2696 tmp_sectors = 0; 2697 if (ctrl_info && !megasas_get_ctrl_info(instance, ctrl_info)) { 2698 2699 max_sectors_1 = (1 << ctrl_info->stripe_sz_ops.min) * 2700 ctrl_info->max_strips_per_io; 2701 max_sectors_2 = ctrl_info->max_request_size; 2702 2703 tmp_sectors = min_t(u32, max_sectors_1 , max_sectors_2); 2704 } 2705 2706 instance->max_sectors_per_req = instance->max_num_sge * 2707 PAGE_SIZE / 512; 2708 if (tmp_sectors && (instance->max_sectors_per_req > tmp_sectors)) 2709 instance->max_sectors_per_req = tmp_sectors; 2710 2711 kfree(ctrl_info); 2712 2713 /* 2714 * Setup tasklet for cmd completion 2715 */ 2716 2717 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc, 2718 (unsigned long)instance); 2719 2720 /* Initialize the cmd completion timer */ 2721 if (poll_mode_io) 2722 megasas_start_timer(instance, &instance->io_completion_timer, 2723 megasas_io_completion_timer, 2724 MEGASAS_COMPLETION_TIMER_INTERVAL); 2725 return 0; 2726 2727 fail_fw_init: 2728 2729 pci_free_consistent(instance->pdev, reply_q_sz, 2730 instance->reply_queue, instance->reply_queue_h); 2731 fail_reply_queue: 2732 megasas_free_cmds(instance); 2733 2734 fail_alloc_cmds: 2735 fail_ready_state: 2736 iounmap(instance->reg_set); 2737 2738 fail_ioremap: 2739 pci_release_selected_regions(instance->pdev, 2740 pci_select_bars(instance->pdev, IORESOURCE_MEM)); 2741 2742 return -EINVAL; 2743} 2744 2745/** 2746 * megasas_release_mfi - Reverses the FW initialization 2747 * @intance: Adapter soft state 2748 */ 2749static void megasas_release_mfi(struct megasas_instance *instance) 2750{ 2751 u32 reply_q_sz = sizeof(u32) * (instance->max_fw_cmds + 1); 2752 2753 pci_free_consistent(instance->pdev, reply_q_sz, 2754 instance->reply_queue, instance->reply_queue_h); 2755 2756 megasas_free_cmds(instance); 2757 2758 iounmap(instance->reg_set); 2759 2760 pci_release_selected_regions(instance->pdev, 2761 pci_select_bars(instance->pdev, IORESOURCE_MEM)); 2762} 2763 2764/** 2765 * megasas_get_seq_num - Gets latest event sequence numbers 2766 * @instance: Adapter soft state 2767 * @eli: FW event log sequence numbers information 2768 * 2769 * FW maintains a log of all events in a non-volatile area. Upper layers would 2770 * usually find out the latest sequence number of the events, the seq number at 2771 * the boot etc. They would "read" all the events below the latest seq number 2772 * by issuing a direct fw cmd (DCMD). For the future events (beyond latest seq 2773 * number), they would subsribe to AEN (asynchronous event notification) and 2774 * wait for the events to happen. 2775 */ 2776static int 2777megasas_get_seq_num(struct megasas_instance *instance, 2778 struct megasas_evt_log_info *eli) 2779{ 2780 struct megasas_cmd *cmd; 2781 struct megasas_dcmd_frame *dcmd; 2782 struct megasas_evt_log_info *el_info; 2783 dma_addr_t el_info_h = 0; 2784 2785 cmd = megasas_get_cmd(instance); 2786 2787 if (!cmd) { 2788 return -ENOMEM; 2789 } 2790 2791 dcmd = &cmd->frame->dcmd; 2792 el_info = pci_alloc_consistent(instance->pdev, 2793 sizeof(struct megasas_evt_log_info), 2794 &el_info_h); 2795 2796 if (!el_info) { 2797 megasas_return_cmd(instance, cmd); 2798 return -ENOMEM; 2799 } 2800 2801 memset(el_info, 0, sizeof(*el_info)); 2802 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE); 2803 2804 dcmd->cmd = MFI_CMD_DCMD; 2805 dcmd->cmd_status = 0x0; 2806 dcmd->sge_count = 1; 2807 dcmd->flags = MFI_FRAME_DIR_READ; 2808 dcmd->timeout = 0; 2809 dcmd->pad_0 = 0; 2810 dcmd->data_xfer_len = sizeof(struct megasas_evt_log_info); 2811 dcmd->opcode = MR_DCMD_CTRL_EVENT_GET_INFO; 2812 dcmd->sgl.sge32[0].phys_addr = el_info_h; 2813 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_log_info); 2814 2815 megasas_issue_blocked_cmd(instance, cmd); 2816 2817 /* 2818 * Copy the data back into callers buffer 2819 */ 2820 memcpy(eli, el_info, sizeof(struct megasas_evt_log_info)); 2821 2822 pci_free_consistent(instance->pdev, sizeof(struct megasas_evt_log_info), 2823 el_info, el_info_h); 2824 2825 megasas_return_cmd(instance, cmd); 2826 2827 return 0; 2828} 2829 2830/** 2831 * megasas_register_aen - Registers for asynchronous event notification 2832 * @instance: Adapter soft state 2833 * @seq_num: The starting sequence number 2834 * @class_locale: Class of the event 2835 * 2836 * This function subscribes for AEN for events beyond the @seq_num. It requests 2837 * to be notified if and only if the event is of type @class_locale 2838 */ 2839static int 2840megasas_register_aen(struct megasas_instance *instance, u32 seq_num, 2841 u32 class_locale_word) 2842{ 2843 int ret_val; 2844 struct megasas_cmd *cmd; 2845 struct megasas_dcmd_frame *dcmd; 2846 union megasas_evt_class_locale curr_aen; 2847 union megasas_evt_class_locale prev_aen; 2848 2849 /* 2850 * If there an AEN pending already (aen_cmd), check if the 2851 * class_locale of that pending AEN is inclusive of the new 2852 * AEN request we currently have. If it is, then we don't have 2853 * to do anything. In other words, whichever events the current 2854 * AEN request is subscribing to, have already been subscribed 2855 * to. 2856 * 2857 * If the old_cmd is _not_ inclusive, then we have to abort 2858 * that command, form a class_locale that is superset of both 2859 * old and current and re-issue to the FW 2860 */ 2861 2862 curr_aen.word = class_locale_word; 2863 2864 if (instance->aen_cmd) { 2865 2866 prev_aen.word = instance->aen_cmd->frame->dcmd.mbox.w[1]; 2867 2868 /* 2869 * A class whose enum value is smaller is inclusive of all 2870 * higher values. If a PROGRESS (= -1) was previously 2871 * registered, then a new registration requests for higher 2872 * classes need not be sent to FW. They are automatically 2873 * included. 2874 * 2875 * Locale numbers don't have such hierarchy. They are bitmap 2876 * values 2877 */ 2878 if ((prev_aen.members.class <= curr_aen.members.class) && 2879 !((prev_aen.members.locale & curr_aen.members.locale) ^ 2880 curr_aen.members.locale)) { 2881 /* 2882 * Previously issued event registration includes 2883 * current request. Nothing to do. 2884 */ 2885 return 0; 2886 } else { 2887 curr_aen.members.locale |= prev_aen.members.locale; 2888 2889 if (prev_aen.members.class < curr_aen.members.class) 2890 curr_aen.members.class = prev_aen.members.class; 2891 2892 instance->aen_cmd->abort_aen = 1; 2893 ret_val = megasas_issue_blocked_abort_cmd(instance, 2894 instance-> 2895 aen_cmd); 2896 2897 if (ret_val) { 2898 printk(KERN_DEBUG "megasas: Failed to abort " 2899 "previous AEN command\n"); 2900 return ret_val; 2901 } 2902 } 2903 } 2904 2905 cmd = megasas_get_cmd(instance); 2906 2907 if (!cmd) 2908 return -ENOMEM; 2909 2910 dcmd = &cmd->frame->dcmd; 2911 2912 memset(instance->evt_detail, 0, sizeof(struct megasas_evt_detail)); 2913 2914 /* 2915 * Prepare DCMD for aen registration 2916 */ 2917 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE); 2918 2919 dcmd->cmd = MFI_CMD_DCMD; 2920 dcmd->cmd_status = 0x0; 2921 dcmd->sge_count = 1; 2922 dcmd->flags = MFI_FRAME_DIR_READ; 2923 dcmd->timeout = 0; 2924 dcmd->pad_0 = 0; 2925 dcmd->data_xfer_len = sizeof(struct megasas_evt_detail); 2926 dcmd->opcode = MR_DCMD_CTRL_EVENT_WAIT; 2927 dcmd->mbox.w[0] = seq_num; 2928 dcmd->mbox.w[1] = curr_aen.word; 2929 dcmd->sgl.sge32[0].phys_addr = (u32) instance->evt_detail_h; 2930 dcmd->sgl.sge32[0].length = sizeof(struct megasas_evt_detail); 2931 2932 if (instance->aen_cmd != NULL) { 2933 megasas_return_cmd(instance, cmd); 2934 return 0; 2935 } 2936 2937 /* 2938 * Store reference to the cmd used to register for AEN. When an 2939 * application wants us to register for AEN, we have to abort this 2940 * cmd and re-register with a new EVENT LOCALE supplied by that app 2941 */ 2942 instance->aen_cmd = cmd; 2943 2944 /* 2945 * Issue the aen registration frame 2946 */ 2947 instance->instancet->fire_cmd(instance, 2948 cmd->frame_phys_addr, 0, instance->reg_set); 2949 2950 return 0; 2951} 2952 2953/** 2954 * megasas_start_aen - Subscribes to AEN during driver load time 2955 * @instance: Adapter soft state 2956 */ 2957static int megasas_start_aen(struct megasas_instance *instance) 2958{ 2959 struct megasas_evt_log_info eli; 2960 union megasas_evt_class_locale class_locale; 2961 2962 /* 2963 * Get the latest sequence number from FW 2964 */ 2965 memset(&eli, 0, sizeof(eli)); 2966 2967 if (megasas_get_seq_num(instance, &eli)) 2968 return -1; 2969 2970 /* 2971 * Register AEN with FW for latest sequence number plus 1 2972 */ 2973 class_locale.members.reserved = 0; 2974 class_locale.members.locale = MR_EVT_LOCALE_ALL; 2975 class_locale.members.class = MR_EVT_CLASS_DEBUG; 2976 2977 return megasas_register_aen(instance, eli.newest_seq_num + 1, 2978 class_locale.word); 2979} 2980 2981/** 2982 * megasas_io_attach - Attaches this driver to SCSI mid-layer 2983 * @instance: Adapter soft state 2984 */ 2985static int megasas_io_attach(struct megasas_instance *instance) 2986{ 2987 struct Scsi_Host *host = instance->host; 2988 2989 /* 2990 * Export parameters required by SCSI mid-layer 2991 */ 2992 host->irq = instance->pdev->irq; 2993 host->unique_id = instance->unique_id; 2994 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) || 2995 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) { 2996 host->can_queue = 2997 instance->max_fw_cmds - MEGASAS_SKINNY_INT_CMDS; 2998 } else 2999 host->can_queue = 3000 instance->max_fw_cmds - MEGASAS_INT_CMDS; 3001 host->this_id = instance->init_id; 3002 host->sg_tablesize = instance->max_num_sge; 3003 host->max_sectors = instance->max_sectors_per_req; 3004 host->cmd_per_lun = 128; 3005 host->max_channel = MEGASAS_MAX_CHANNELS - 1; 3006 host->max_id = MEGASAS_MAX_DEV_PER_CHANNEL; 3007 host->max_lun = MEGASAS_MAX_LUN; 3008 host->max_cmd_len = 16; 3009 3010 /* 3011 * Notify the mid-layer about the new controller 3012 */ 3013 if (scsi_add_host(host, &instance->pdev->dev)) { 3014 printk(KERN_DEBUG "megasas: scsi_add_host failed\n"); 3015 return -ENODEV; 3016 } 3017 3018 /* 3019 * Trigger SCSI to scan our drives 3020 */ 3021 scsi_scan_host(host); 3022 return 0; 3023} 3024 3025static int 3026megasas_set_dma_mask(struct pci_dev *pdev) 3027{ 3028 /* 3029 * All our contollers are capable of performing 64-bit DMA 3030 */ 3031 if (IS_DMA64) { 3032 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(64)) != 0) { 3033 3034 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) 3035 goto fail_set_dma_mask; 3036 } 3037 } else { 3038 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32)) != 0) 3039 goto fail_set_dma_mask; 3040 } 3041 return 0; 3042 3043fail_set_dma_mask: 3044 return 1; 3045} 3046 3047/** 3048 * megasas_probe_one - PCI hotplug entry point 3049 * @pdev: PCI device structure 3050 * @id: PCI ids of supported hotplugged adapter 3051 */ 3052static int __devinit 3053megasas_probe_one(struct pci_dev *pdev, const struct pci_device_id *id) 3054{ 3055 int rval; 3056 struct Scsi_Host *host; 3057 struct megasas_instance *instance; 3058 3059 /* 3060 * Announce PCI information 3061 */ 3062 printk(KERN_INFO "megasas: %#4.04x:%#4.04x:%#4.04x:%#4.04x: ", 3063 pdev->vendor, pdev->device, pdev->subsystem_vendor, 3064 pdev->subsystem_device); 3065 3066 printk("bus %d:slot %d:func %d\n", 3067 pdev->bus->number, PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn)); 3068 3069 /* 3070 * PCI prepping: enable device set bus mastering and dma mask 3071 */ 3072 rval = pci_enable_device_mem(pdev); 3073 3074 if (rval) { 3075 return rval; 3076 } 3077 3078 pci_set_master(pdev); 3079 3080 if (megasas_set_dma_mask(pdev)) 3081 goto fail_set_dma_mask; 3082 3083 host = scsi_host_alloc(&megasas_template, 3084 sizeof(struct megasas_instance)); 3085 3086 if (!host) { 3087 printk(KERN_DEBUG "megasas: scsi_host_alloc failed\n"); 3088 goto fail_alloc_instance; 3089 } 3090 3091 instance = (struct megasas_instance *)host->hostdata; 3092 memset(instance, 0, sizeof(*instance)); 3093 3094 instance->producer = pci_alloc_consistent(pdev, sizeof(u32), 3095 &instance->producer_h); 3096 instance->consumer = pci_alloc_consistent(pdev, sizeof(u32), 3097 &instance->consumer_h); 3098 3099 if (!instance->producer || !instance->consumer) { 3100 printk(KERN_DEBUG "megasas: Failed to allocate memory for " 3101 "producer, consumer\n"); 3102 goto fail_alloc_dma_buf; 3103 } 3104 3105 *instance->producer = 0; 3106 *instance->consumer = 0; 3107 megasas_poll_wait_aen = 0; 3108 instance->flag_ieee = 0; 3109 instance->ev = NULL; 3110 3111 instance->evt_detail = pci_alloc_consistent(pdev, 3112 sizeof(struct 3113 megasas_evt_detail), 3114 &instance->evt_detail_h); 3115 3116 if (!instance->evt_detail) { 3117 printk(KERN_DEBUG "megasas: Failed to allocate memory for " 3118 "event detail structure\n"); 3119 goto fail_alloc_dma_buf; 3120 } 3121 3122 /* 3123 * Initialize locks and queues 3124 */ 3125 INIT_LIST_HEAD(&instance->cmd_pool); 3126 3127 atomic_set(&instance->fw_outstanding,0); 3128 3129 init_waitqueue_head(&instance->int_cmd_wait_q); 3130 init_waitqueue_head(&instance->abort_cmd_wait_q); 3131 3132 spin_lock_init(&instance->cmd_pool_lock); 3133 spin_lock_init(&instance->fire_lock); 3134 spin_lock_init(&instance->completion_lock); 3135 spin_lock_init(&poll_aen_lock); 3136 3137 mutex_init(&instance->aen_mutex); 3138 3139 /* 3140 * Initialize PCI related and misc parameters 3141 */ 3142 instance->pdev = pdev; 3143 instance->host = host; 3144 instance->unique_id = pdev->bus->number << 8 | pdev->devfn; 3145 instance->init_id = MEGASAS_DEFAULT_INIT_ID; 3146 3147 if ((instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0073SKINNY) || 3148 (instance->pdev->device == PCI_DEVICE_ID_LSI_SAS0071SKINNY)) { 3149 instance->flag_ieee = 1; 3150 sema_init(&instance->ioctl_sem, MEGASAS_SKINNY_INT_CMDS); 3151 } else 3152 sema_init(&instance->ioctl_sem, MEGASAS_INT_CMDS); 3153 3154 megasas_dbg_lvl = 0; 3155 instance->flag = 0; 3156 instance->unload = 1; 3157 instance->last_time = 0; 3158 3159 /* 3160 * Initialize MFI Firmware 3161 */ 3162 if (megasas_init_mfi(instance)) 3163 goto fail_init_mfi; 3164 3165 /* 3166 * Register IRQ 3167 */ 3168 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, "megasas", instance)) { 3169 printk(KERN_DEBUG "megasas: Failed to register IRQ\n"); 3170 goto fail_irq; 3171 } 3172 3173 instance->instancet->enable_intr(instance->reg_set); 3174 3175 /* 3176 * Store instance in PCI softstate 3177 */ 3178 pci_set_drvdata(pdev, instance); 3179 3180 /* 3181 * Add this controller to megasas_mgmt_info structure so that it 3182 * can be exported to management applications 3183 */ 3184 megasas_mgmt_info.count++; 3185 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = instance; 3186 megasas_mgmt_info.max_index++; 3187 3188 /* 3189 * Initiate AEN (Asynchronous Event Notification) 3190 */ 3191 if (megasas_start_aen(instance)) { 3192 printk(KERN_DEBUG "megasas: start aen failed\n"); 3193 goto fail_start_aen; 3194 } 3195 3196 /* 3197 * Register with SCSI mid-layer 3198 */ 3199 if (megasas_io_attach(instance)) 3200 goto fail_io_attach; 3201 3202 instance->unload = 0; 3203 return 0; 3204 3205 fail_start_aen: 3206 fail_io_attach: 3207 megasas_mgmt_info.count--; 3208 megasas_mgmt_info.instance[megasas_mgmt_info.max_index] = NULL; 3209 megasas_mgmt_info.max_index--; 3210 3211 pci_set_drvdata(pdev, NULL); 3212 instance->instancet->disable_intr(instance->reg_set); 3213 free_irq(instance->pdev->irq, instance); 3214 3215 megasas_release_mfi(instance); 3216 3217 fail_irq: 3218 fail_init_mfi: 3219 fail_alloc_dma_buf: 3220 if (instance->evt_detail) 3221 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail), 3222 instance->evt_detail, 3223 instance->evt_detail_h); 3224 3225 if (instance->producer) 3226 pci_free_consistent(pdev, sizeof(u32), instance->producer, 3227 instance->producer_h); 3228 if (instance->consumer) 3229 pci_free_consistent(pdev, sizeof(u32), instance->consumer, 3230 instance->consumer_h); 3231 scsi_host_put(host); 3232 3233 fail_alloc_instance: 3234 fail_set_dma_mask: 3235 pci_disable_device(pdev); 3236 3237 return -ENODEV; 3238} 3239 3240/** 3241 * megasas_flush_cache - Requests FW to flush all its caches 3242 * @instance: Adapter soft state 3243 */ 3244static void megasas_flush_cache(struct megasas_instance *instance) 3245{ 3246 struct megasas_cmd *cmd; 3247 struct megasas_dcmd_frame *dcmd; 3248 3249 cmd = megasas_get_cmd(instance); 3250 3251 if (!cmd) 3252 return; 3253 3254 dcmd = &cmd->frame->dcmd; 3255 3256 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE); 3257 3258 dcmd->cmd = MFI_CMD_DCMD; 3259 dcmd->cmd_status = 0x0; 3260 dcmd->sge_count = 0; 3261 dcmd->flags = MFI_FRAME_DIR_NONE; 3262 dcmd->timeout = 0; 3263 dcmd->pad_0 = 0; 3264 dcmd->data_xfer_len = 0; 3265 dcmd->opcode = MR_DCMD_CTRL_CACHE_FLUSH; 3266 dcmd->mbox.b[0] = MR_FLUSH_CTRL_CACHE | MR_FLUSH_DISK_CACHE; 3267 3268 megasas_issue_blocked_cmd(instance, cmd); 3269 3270 megasas_return_cmd(instance, cmd); 3271 3272 return; 3273} 3274 3275/** 3276 * megasas_shutdown_controller - Instructs FW to shutdown the controller 3277 * @instance: Adapter soft state 3278 * @opcode: Shutdown/Hibernate 3279 */ 3280static void megasas_shutdown_controller(struct megasas_instance *instance, 3281 u32 opcode) 3282{ 3283 struct megasas_cmd *cmd; 3284 struct megasas_dcmd_frame *dcmd; 3285 3286 cmd = megasas_get_cmd(instance); 3287 3288 if (!cmd) 3289 return; 3290 3291 if (instance->aen_cmd) 3292 megasas_issue_blocked_abort_cmd(instance, instance->aen_cmd); 3293 3294 dcmd = &cmd->frame->dcmd; 3295 3296 memset(dcmd->mbox.b, 0, MFI_MBOX_SIZE); 3297 3298 dcmd->cmd = MFI_CMD_DCMD; 3299 dcmd->cmd_status = 0x0; 3300 dcmd->sge_count = 0; 3301 dcmd->flags = MFI_FRAME_DIR_NONE; 3302 dcmd->timeout = 0; 3303 dcmd->pad_0 = 0; 3304 dcmd->data_xfer_len = 0; 3305 dcmd->opcode = opcode; 3306 3307 megasas_issue_blocked_cmd(instance, cmd); 3308 3309 megasas_return_cmd(instance, cmd); 3310 3311 return; 3312} 3313 3314#ifdef CONFIG_PM 3315/** 3316 * megasas_suspend - driver suspend entry point 3317 * @pdev: PCI device structure 3318 * @state: PCI power state to suspend routine 3319 */ 3320static int 3321megasas_suspend(struct pci_dev *pdev, pm_message_t state) 3322{ 3323 struct Scsi_Host *host; 3324 struct megasas_instance *instance; 3325 3326 instance = pci_get_drvdata(pdev); 3327 host = instance->host; 3328 instance->unload = 1; 3329 3330 if (poll_mode_io) 3331 del_timer_sync(&instance->io_completion_timer); 3332 3333 megasas_flush_cache(instance); 3334 megasas_shutdown_controller(instance, MR_DCMD_HIBERNATE_SHUTDOWN); 3335 3336 /* cancel the delayed work if this work still in queue */ 3337 if (instance->ev != NULL) { 3338 struct megasas_aen_event *ev = instance->ev; 3339 cancel_delayed_work( 3340 (struct delayed_work *)&ev->hotplug_work); 3341 flush_scheduled_work(); 3342 instance->ev = NULL; 3343 } 3344 3345 tasklet_kill(&instance->isr_tasklet); 3346 3347 pci_set_drvdata(instance->pdev, instance); 3348 instance->instancet->disable_intr(instance->reg_set); 3349 free_irq(instance->pdev->irq, instance); 3350 3351 pci_save_state(pdev); 3352 pci_disable_device(pdev); 3353 3354 pci_set_power_state(pdev, pci_choose_state(pdev, state)); 3355 3356 return 0; 3357} 3358 3359/** 3360 * megasas_resume- driver resume entry point 3361 * @pdev: PCI device structure 3362 */ 3363static int 3364megasas_resume(struct pci_dev *pdev) 3365{ 3366 int rval; 3367 struct Scsi_Host *host; 3368 struct megasas_instance *instance; 3369 3370 instance = pci_get_drvdata(pdev); 3371 host = instance->host; 3372 pci_set_power_state(pdev, PCI_D0); 3373 pci_enable_wake(pdev, PCI_D0, 0); 3374 pci_restore_state(pdev); 3375 3376 /* 3377 * PCI prepping: enable device set bus mastering and dma mask 3378 */ 3379 rval = pci_enable_device_mem(pdev); 3380 3381 if (rval) { 3382 printk(KERN_ERR "megasas: Enable device failed\n"); 3383 return rval; 3384 } 3385 3386 pci_set_master(pdev); 3387 3388 if (megasas_set_dma_mask(pdev)) 3389 goto fail_set_dma_mask; 3390 3391 /* 3392 * Initialize MFI Firmware 3393 */ 3394 3395 *instance->producer = 0; 3396 *instance->consumer = 0; 3397 3398 atomic_set(&instance->fw_outstanding, 0); 3399 3400 /* 3401 * We expect the FW state to be READY 3402 */ 3403 if (megasas_transition_to_ready(instance)) 3404 goto fail_ready_state; 3405 3406 if (megasas_issue_init_mfi(instance)) 3407 goto fail_init_mfi; 3408 3409 tasklet_init(&instance->isr_tasklet, megasas_complete_cmd_dpc, 3410 (unsigned long)instance); 3411 3412 /* 3413 * Register IRQ 3414 */ 3415 if (request_irq(pdev->irq, megasas_isr, IRQF_SHARED, 3416 "megasas", instance)) { 3417 printk(KERN_ERR "megasas: Failed to register IRQ\n"); 3418 goto fail_irq; 3419 } 3420 3421 instance->instancet->enable_intr(instance->reg_set); 3422 3423 /* 3424 * Initiate AEN (Asynchronous Event Notification) 3425 */ 3426 if (megasas_start_aen(instance)) 3427 printk(KERN_ERR "megasas: Start AEN failed\n"); 3428 3429 /* Initialize the cmd completion timer */ 3430 if (poll_mode_io) 3431 megasas_start_timer(instance, &instance->io_completion_timer, 3432 megasas_io_completion_timer, 3433 MEGASAS_COMPLETION_TIMER_INTERVAL); 3434 instance->unload = 0; 3435 3436 return 0; 3437 3438fail_irq: 3439fail_init_mfi: 3440 if (instance->evt_detail) 3441 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail), 3442 instance->evt_detail, 3443 instance->evt_detail_h); 3444 3445 if (instance->producer) 3446 pci_free_consistent(pdev, sizeof(u32), instance->producer, 3447 instance->producer_h); 3448 if (instance->consumer) 3449 pci_free_consistent(pdev, sizeof(u32), instance->consumer, 3450 instance->consumer_h); 3451 scsi_host_put(host); 3452 3453fail_set_dma_mask: 3454fail_ready_state: 3455 3456 pci_disable_device(pdev); 3457 3458 return -ENODEV; 3459} 3460#else 3461#define megasas_suspend NULL 3462#define megasas_resume NULL 3463#endif 3464 3465/** 3466 * megasas_detach_one - PCI hot"un"plug entry point 3467 * @pdev: PCI device structure 3468 */ 3469static void __devexit megasas_detach_one(struct pci_dev *pdev) 3470{ 3471 int i; 3472 struct Scsi_Host *host; 3473 struct megasas_instance *instance; 3474 3475 instance = pci_get_drvdata(pdev); 3476 instance->unload = 1; 3477 host = instance->host; 3478 3479 if (poll_mode_io) 3480 del_timer_sync(&instance->io_completion_timer); 3481 3482 scsi_remove_host(instance->host); 3483 megasas_flush_cache(instance); 3484 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN); 3485 3486 /* cancel the delayed work if this work still in queue*/ 3487 if (instance->ev != NULL) { 3488 struct megasas_aen_event *ev = instance->ev; 3489 cancel_delayed_work( 3490 (struct delayed_work *)&ev->hotplug_work); 3491 flush_scheduled_work(); 3492 instance->ev = NULL; 3493 } 3494 3495 tasklet_kill(&instance->isr_tasklet); 3496 3497 /* 3498 * Take the instance off the instance array. Note that we will not 3499 * decrement the max_index. We let this array be sparse array 3500 */ 3501 for (i = 0; i < megasas_mgmt_info.max_index; i++) { 3502 if (megasas_mgmt_info.instance[i] == instance) { 3503 megasas_mgmt_info.count--; 3504 megasas_mgmt_info.instance[i] = NULL; 3505 3506 break; 3507 } 3508 } 3509 3510 pci_set_drvdata(instance->pdev, NULL); 3511 3512 instance->instancet->disable_intr(instance->reg_set); 3513 3514 free_irq(instance->pdev->irq, instance); 3515 3516 megasas_release_mfi(instance); 3517 3518 pci_free_consistent(pdev, sizeof(struct megasas_evt_detail), 3519 instance->evt_detail, instance->evt_detail_h); 3520 3521 pci_free_consistent(pdev, sizeof(u32), instance->producer, 3522 instance->producer_h); 3523 3524 pci_free_consistent(pdev, sizeof(u32), instance->consumer, 3525 instance->consumer_h); 3526 3527 scsi_host_put(host); 3528 3529 pci_set_drvdata(pdev, NULL); 3530 3531 pci_disable_device(pdev); 3532 3533 return; 3534} 3535 3536/** 3537 * megasas_shutdown - Shutdown entry point 3538 * @device: Generic device structure 3539 */ 3540static void megasas_shutdown(struct pci_dev *pdev) 3541{ 3542 struct megasas_instance *instance = pci_get_drvdata(pdev); 3543 instance->unload = 1; 3544 megasas_flush_cache(instance); 3545 megasas_shutdown_controller(instance, MR_DCMD_CTRL_SHUTDOWN); 3546} 3547 3548/** 3549 * megasas_mgmt_open - char node "open" entry point 3550 */ 3551static int megasas_mgmt_open(struct inode *inode, struct file *filep) 3552{ 3553 cycle_kernel_lock(); 3554 /* 3555 * Allow only those users with admin rights 3556 */ 3557 if (!capable(CAP_SYS_ADMIN)) 3558 return -EACCES; 3559 3560 return 0; 3561} 3562 3563/** 3564 * megasas_mgmt_fasync - Async notifier registration from applications 3565 * 3566 * This function adds the calling process to a driver global queue. When an 3567 * event occurs, SIGIO will be sent to all processes in this queue. 3568 */ 3569static int megasas_mgmt_fasync(int fd, struct file *filep, int mode) 3570{ 3571 int rc; 3572 3573 mutex_lock(&megasas_async_queue_mutex); 3574 3575 rc = fasync_helper(fd, filep, mode, &megasas_async_queue); 3576 3577 mutex_unlock(&megasas_async_queue_mutex); 3578 3579 if (rc >= 0) { 3580 /* For sanity check when we get ioctl */ 3581 filep->private_data = filep; 3582 return 0; 3583 } 3584 3585 printk(KERN_DEBUG "megasas: fasync_helper failed [%d]\n", rc); 3586 3587 return rc; 3588} 3589 3590/** 3591 * megasas_mgmt_poll - char node "poll" entry point 3592 * */ 3593static unsigned int megasas_mgmt_poll(struct file *file, poll_table *wait) 3594{ 3595 unsigned int mask; 3596 unsigned long flags; 3597 poll_wait(file, &megasas_poll_wait, wait); 3598 spin_lock_irqsave(&poll_aen_lock, flags); 3599 if (megasas_poll_wait_aen) 3600 mask = (POLLIN | POLLRDNORM); 3601 else 3602 mask = 0; 3603 spin_unlock_irqrestore(&poll_aen_lock, flags); 3604 return mask; 3605} 3606 3607/** 3608 * megasas_mgmt_fw_ioctl - Issues management ioctls to FW 3609 * @instance: Adapter soft state 3610 * @argp: User's ioctl packet 3611 */ 3612static int 3613megasas_mgmt_fw_ioctl(struct megasas_instance *instance, 3614 struct megasas_iocpacket __user * user_ioc, 3615 struct megasas_iocpacket *ioc) 3616{ 3617 struct megasas_sge32 *kern_sge32; 3618 struct megasas_cmd *cmd; 3619 void *kbuff_arr[MAX_IOCTL_SGE]; 3620 dma_addr_t buf_handle = 0; 3621 int error = 0, i; 3622 void *sense = NULL; 3623 dma_addr_t sense_handle; 3624 unsigned long *sense_ptr; 3625 3626 memset(kbuff_arr, 0, sizeof(kbuff_arr)); 3627 3628 if (ioc->sge_count > MAX_IOCTL_SGE) { 3629 printk(KERN_DEBUG "megasas: SGE count [%d] > max limit [%d]\n", 3630 ioc->sge_count, MAX_IOCTL_SGE); 3631 return -EINVAL; 3632 } 3633 3634 cmd = megasas_get_cmd(instance); 3635 if (!cmd) { 3636 printk(KERN_DEBUG "megasas: Failed to get a cmd packet\n"); 3637 return -ENOMEM; 3638 } 3639 3640 /* 3641 * User's IOCTL packet has 2 frames (maximum). Copy those two 3642 * frames into our cmd's frames. cmd->frame's context will get 3643 * overwritten when we copy from user's frames. So set that value 3644 * alone separately 3645 */ 3646 memcpy(cmd->frame, ioc->frame.raw, 2 * MEGAMFI_FRAME_SIZE); 3647 cmd->frame->hdr.context = cmd->index; 3648 cmd->frame->hdr.pad_0 = 0; 3649 3650 /* 3651 * The management interface between applications and the fw uses 3652 * MFI frames. E.g, RAID configuration changes, LD property changes 3653 * etc are accomplishes through different kinds of MFI frames. The 3654 * driver needs to care only about substituting user buffers with 3655 * kernel buffers in SGLs. The location of SGL is embedded in the 3656 * struct iocpacket itself. 3657 */ 3658 kern_sge32 = (struct megasas_sge32 *) 3659 ((unsigned long)cmd->frame + ioc->sgl_off); 3660 3661 /* 3662 * For each user buffer, create a mirror buffer and copy in 3663 */ 3664 for (i = 0; i < ioc->sge_count; i++) { 3665 kbuff_arr[i] = dma_alloc_coherent(&instance->pdev->dev, 3666 ioc->sgl[i].iov_len, 3667 &buf_handle, GFP_KERNEL); 3668 if (!kbuff_arr[i]) { 3669 printk(KERN_DEBUG "megasas: Failed to alloc " 3670 "kernel SGL buffer for IOCTL \n"); 3671 error = -ENOMEM; 3672 goto out; 3673 } 3674 3675 /* 3676 * We don't change the dma_coherent_mask, so 3677 * pci_alloc_consistent only returns 32bit addresses 3678 */ 3679 kern_sge32[i].phys_addr = (u32) buf_handle; 3680 kern_sge32[i].length = ioc->sgl[i].iov_len; 3681 3682 /* 3683 * We created a kernel buffer corresponding to the 3684 * user buffer. Now copy in from the user buffer 3685 */ 3686 if (copy_from_user(kbuff_arr[i], ioc->sgl[i].iov_base, 3687 (u32) (ioc->sgl[i].iov_len))) { 3688 error = -EFAULT; 3689 goto out; 3690 } 3691 } 3692 3693 if (ioc->sense_len) { 3694 sense = dma_alloc_coherent(&instance->pdev->dev, ioc->sense_len, 3695 &sense_handle, GFP_KERNEL); 3696 if (!sense) { 3697 error = -ENOMEM; 3698 goto out; 3699 } 3700 3701 sense_ptr = 3702 (unsigned long *) ((unsigned long)cmd->frame + ioc->sense_off); 3703 *sense_ptr = sense_handle; 3704 } 3705 3706 /* 3707 * Set the sync_cmd flag so that the ISR knows not to complete this 3708 * cmd to the SCSI mid-layer 3709 */ 3710 cmd->sync_cmd = 1; 3711 megasas_issue_blocked_cmd(instance, cmd); 3712 cmd->sync_cmd = 0; 3713 3714 /* 3715 * copy out the kernel buffers to user buffers 3716 */ 3717 for (i = 0; i < ioc->sge_count; i++) { 3718 if (copy_to_user(ioc->sgl[i].iov_base, kbuff_arr[i], 3719 ioc->sgl[i].iov_len)) { 3720 error = -EFAULT; 3721 goto out; 3722 } 3723 } 3724 3725 /* 3726 * copy out the sense 3727 */ 3728 if (ioc->sense_len) { 3729 /* 3730 * sense_ptr points to the location that has the user 3731 * sense buffer address 3732 */ 3733 sense_ptr = (unsigned long *) ((unsigned long)ioc->frame.raw + 3734 ioc->sense_off); 3735 3736 if (copy_to_user((void __user *)((unsigned long)(*sense_ptr)), 3737 sense, ioc->sense_len)) { 3738 printk(KERN_ERR "megasas: Failed to copy out to user " 3739 "sense data\n"); 3740 error = -EFAULT; 3741 goto out; 3742 } 3743 } 3744 3745 /* 3746 * copy the status codes returned by the fw 3747 */ 3748 if (copy_to_user(&user_ioc->frame.hdr.cmd_status, 3749 &cmd->frame->hdr.cmd_status, sizeof(u8))) { 3750 printk(KERN_DEBUG "megasas: Error copying out cmd_status\n"); 3751 error = -EFAULT; 3752 } 3753 3754 out: 3755 if (sense) { 3756 dma_free_coherent(&instance->pdev->dev, ioc->sense_len, 3757 sense, sense_handle); 3758 } 3759 3760 for (i = 0; i < ioc->sge_count && kbuff_arr[i]; i++) { 3761 dma_free_coherent(&instance->pdev->dev, 3762 kern_sge32[i].length, 3763 kbuff_arr[i], kern_sge32[i].phys_addr); 3764 } 3765 3766 megasas_return_cmd(instance, cmd); 3767 return error; 3768} 3769 3770static int megasas_mgmt_ioctl_fw(struct file *file, unsigned long arg) 3771{ 3772 struct megasas_iocpacket __user *user_ioc = 3773 (struct megasas_iocpacket __user *)arg; 3774 struct megasas_iocpacket *ioc; 3775 struct megasas_instance *instance; 3776 int error; 3777 3778 ioc = kmalloc(sizeof(*ioc), GFP_KERNEL); 3779 if (!ioc) 3780 return -ENOMEM; 3781 3782 if (copy_from_user(ioc, user_ioc, sizeof(*ioc))) { 3783 error = -EFAULT; 3784 goto out_kfree_ioc; 3785 } 3786 3787 instance = megasas_lookup_instance(ioc->host_no); 3788 if (!instance) { 3789 error = -ENODEV; 3790 goto out_kfree_ioc; 3791 } 3792 3793 if (instance->hw_crit_error == 1) { 3794 printk(KERN_DEBUG "Controller in Crit ERROR\n"); 3795 error = -ENODEV; 3796 goto out_kfree_ioc; 3797 } 3798 3799 if (instance->unload == 1) { 3800 error = -ENODEV; 3801 goto out_kfree_ioc; 3802 } 3803 3804 /* 3805 * We will allow only MEGASAS_INT_CMDS number of parallel ioctl cmds 3806 */ 3807 if (down_interruptible(&instance->ioctl_sem)) { 3808 error = -ERESTARTSYS; 3809 goto out_kfree_ioc; 3810 } 3811 error = megasas_mgmt_fw_ioctl(instance, user_ioc, ioc); 3812 up(&instance->ioctl_sem); 3813 3814 out_kfree_ioc: 3815 kfree(ioc); 3816 return error; 3817} 3818 3819static int megasas_mgmt_ioctl_aen(struct file *file, unsigned long arg) 3820{ 3821 struct megasas_instance *instance; 3822 struct megasas_aen aen; 3823 int error; 3824 3825 if (file->private_data != file) { 3826 printk(KERN_DEBUG "megasas: fasync_helper was not " 3827 "called first\n"); 3828 return -EINVAL; 3829 } 3830 3831 if (copy_from_user(&aen, (void __user *)arg, sizeof(aen))) 3832 return -EFAULT; 3833 3834 instance = megasas_lookup_instance(aen.host_no); 3835 3836 if (!instance) 3837 return -ENODEV; 3838 3839 if (instance->hw_crit_error == 1) { 3840 error = -ENODEV; 3841 } 3842 3843 if (instance->unload == 1) { 3844 return -ENODEV; 3845 } 3846 3847 mutex_lock(&instance->aen_mutex); 3848 error = megasas_register_aen(instance, aen.seq_num, 3849 aen.class_locale_word); 3850 mutex_unlock(&instance->aen_mutex); 3851 return error; 3852} 3853 3854/** 3855 * megasas_mgmt_ioctl - char node ioctl entry point 3856 */ 3857static long 3858megasas_mgmt_ioctl(struct file *file, unsigned int cmd, unsigned long arg) 3859{ 3860 switch (cmd) { 3861 case MEGASAS_IOC_FIRMWARE: 3862 return megasas_mgmt_ioctl_fw(file, arg); 3863 3864 case MEGASAS_IOC_GET_AEN: 3865 return megasas_mgmt_ioctl_aen(file, arg); 3866 } 3867 3868 return -ENOTTY; 3869} 3870 3871#ifdef CONFIG_COMPAT 3872static int megasas_mgmt_compat_ioctl_fw(struct file *file, unsigned long arg) 3873{ 3874 struct compat_megasas_iocpacket __user *cioc = 3875 (struct compat_megasas_iocpacket __user *)arg; 3876 struct megasas_iocpacket __user *ioc = 3877 compat_alloc_user_space(sizeof(struct megasas_iocpacket)); 3878 int i; 3879 int error = 0; 3880 compat_uptr_t ptr; 3881 3882 if (clear_user(ioc, sizeof(*ioc))) 3883 return -EFAULT; 3884 3885 if (copy_in_user(&ioc->host_no, &cioc->host_no, sizeof(u16)) || 3886 copy_in_user(&ioc->sgl_off, &cioc->sgl_off, sizeof(u32)) || 3887 copy_in_user(&ioc->sense_off, &cioc->sense_off, sizeof(u32)) || 3888 copy_in_user(&ioc->sense_len, &cioc->sense_len, sizeof(u32)) || 3889 copy_in_user(ioc->frame.raw, cioc->frame.raw, 128) || 3890 copy_in_user(&ioc->sge_count, &cioc->sge_count, sizeof(u32))) 3891 return -EFAULT; 3892 3893 /* 3894 * The sense_ptr is used in megasas_mgmt_fw_ioctl only when 3895 * sense_len is not null, so prepare the 64bit value under 3896 * the same condition. 3897 */ 3898 if (ioc->sense_len) { 3899 void __user **sense_ioc_ptr = 3900 (void __user **)(ioc->frame.raw + ioc->sense_off); 3901 compat_uptr_t *sense_cioc_ptr = 3902 (compat_uptr_t *)(cioc->frame.raw + cioc->sense_off); 3903 if (get_user(ptr, sense_cioc_ptr) || 3904 put_user(compat_ptr(ptr), sense_ioc_ptr)) 3905 return -EFAULT; 3906 } 3907 3908 for (i = 0; i < MAX_IOCTL_SGE; i++) { 3909 if (get_user(ptr, &cioc->sgl[i].iov_base) || 3910 put_user(compat_ptr(ptr), &ioc->sgl[i].iov_base) || 3911 copy_in_user(&ioc->sgl[i].iov_len, 3912 &cioc->sgl[i].iov_len, sizeof(compat_size_t))) 3913 return -EFAULT; 3914 } 3915 3916 error = megasas_mgmt_ioctl_fw(file, (unsigned long)ioc); 3917 3918 if (copy_in_user(&cioc->frame.hdr.cmd_status, 3919 &ioc->frame.hdr.cmd_status, sizeof(u8))) { 3920 printk(KERN_DEBUG "megasas: error copy_in_user cmd_status\n"); 3921 return -EFAULT; 3922 } 3923 return error; 3924} 3925 3926static long 3927megasas_mgmt_compat_ioctl(struct file *file, unsigned int cmd, 3928 unsigned long arg) 3929{ 3930 switch (cmd) { 3931 case MEGASAS_IOC_FIRMWARE32: 3932 return megasas_mgmt_compat_ioctl_fw(file, arg); 3933 case MEGASAS_IOC_GET_AEN: 3934 return megasas_mgmt_ioctl_aen(file, arg); 3935 } 3936 3937 return -ENOTTY; 3938} 3939#endif 3940 3941/* 3942 * File operations structure for management interface 3943 */ 3944static const struct file_operations megasas_mgmt_fops = { 3945 .owner = THIS_MODULE, 3946 .open = megasas_mgmt_open, 3947 .fasync = megasas_mgmt_fasync, 3948 .unlocked_ioctl = megasas_mgmt_ioctl, 3949 .poll = megasas_mgmt_poll, 3950#ifdef CONFIG_COMPAT 3951 .compat_ioctl = megasas_mgmt_compat_ioctl, 3952#endif 3953}; 3954 3955/* 3956 * PCI hotplug support registration structure 3957 */ 3958static struct pci_driver megasas_pci_driver = { 3959 3960 .name = "megaraid_sas", 3961 .id_table = megasas_pci_table, 3962 .probe = megasas_probe_one, 3963 .remove = __devexit_p(megasas_detach_one), 3964 .suspend = megasas_suspend, 3965 .resume = megasas_resume, 3966 .shutdown = megasas_shutdown, 3967}; 3968 3969/* 3970 * Sysfs driver attributes 3971 */ 3972static ssize_t megasas_sysfs_show_version(struct device_driver *dd, char *buf) 3973{ 3974 return snprintf(buf, strlen(MEGASAS_VERSION) + 2, "%s\n", 3975 MEGASAS_VERSION); 3976} 3977 3978static DRIVER_ATTR(version, S_IRUGO, megasas_sysfs_show_version, NULL); 3979 3980static ssize_t 3981megasas_sysfs_show_release_date(struct device_driver *dd, char *buf) 3982{ 3983 return snprintf(buf, strlen(MEGASAS_RELDATE) + 2, "%s\n", 3984 MEGASAS_RELDATE); 3985} 3986 3987static DRIVER_ATTR(release_date, S_IRUGO, megasas_sysfs_show_release_date, 3988 NULL); 3989 3990static ssize_t 3991megasas_sysfs_show_support_poll_for_event(struct device_driver *dd, char *buf) 3992{ 3993 return sprintf(buf, "%u\n", support_poll_for_event); 3994} 3995 3996static DRIVER_ATTR(support_poll_for_event, S_IRUGO, 3997 megasas_sysfs_show_support_poll_for_event, NULL); 3998 3999static ssize_t 4000megasas_sysfs_show_dbg_lvl(struct device_driver *dd, char *buf) 4001{ 4002 return sprintf(buf, "%u\n", megasas_dbg_lvl); 4003} 4004 4005static ssize_t 4006megasas_sysfs_set_dbg_lvl(struct device_driver *dd, const char *buf, size_t count) 4007{ 4008 int retval = count; 4009 if(sscanf(buf,"%u",&megasas_dbg_lvl)<1){ 4010 printk(KERN_ERR "megasas: could not set dbg_lvl\n"); 4011 retval = -EINVAL; 4012 } 4013 return retval; 4014} 4015 4016static DRIVER_ATTR(dbg_lvl, S_IRUGO|S_IWUSR, megasas_sysfs_show_dbg_lvl, 4017 megasas_sysfs_set_dbg_lvl); 4018 4019static ssize_t 4020megasas_sysfs_show_poll_mode_io(struct device_driver *dd, char *buf) 4021{ 4022 return sprintf(buf, "%u\n", poll_mode_io); 4023} 4024 4025static ssize_t 4026megasas_sysfs_set_poll_mode_io(struct device_driver *dd, 4027 const char *buf, size_t count) 4028{ 4029 int retval = count; 4030 int tmp = poll_mode_io; 4031 int i; 4032 struct megasas_instance *instance; 4033 4034 if (sscanf(buf, "%u", &poll_mode_io) < 1) { 4035 printk(KERN_ERR "megasas: could not set poll_mode_io\n"); 4036 retval = -EINVAL; 4037 } 4038 4039 /* 4040 * Check if poll_mode_io is already set or is same as previous value 4041 */ 4042 if ((tmp && poll_mode_io) || (tmp == poll_mode_io)) 4043 goto out; 4044 4045 if (poll_mode_io) { 4046 /* 4047 * Start timers for all adapters 4048 */ 4049 for (i = 0; i < megasas_mgmt_info.max_index; i++) { 4050 instance = megasas_mgmt_info.instance[i]; 4051 if (instance) { 4052 megasas_start_timer(instance, 4053 &instance->io_completion_timer, 4054 megasas_io_completion_timer, 4055 MEGASAS_COMPLETION_TIMER_INTERVAL); 4056 } 4057 } 4058 } else { 4059 /* 4060 * Delete timers for all adapters 4061 */ 4062 for (i = 0; i < megasas_mgmt_info.max_index; i++) { 4063 instance = megasas_mgmt_info.instance[i]; 4064 if (instance) 4065 del_timer_sync(&instance->io_completion_timer); 4066 } 4067 } 4068 4069out: 4070 return retval; 4071} 4072 4073static void 4074megasas_aen_polling(struct work_struct *work) 4075{ 4076 struct megasas_aen_event *ev = 4077 container_of(work, struct megasas_aen_event, hotplug_work); 4078 struct megasas_instance *instance = ev->instance; 4079 union megasas_evt_class_locale class_locale; 4080 struct Scsi_Host *host; 4081 struct scsi_device *sdev1; 4082 u16 pd_index = 0; 4083 u16 ld_index = 0; 4084 int i, j, doscan = 0; 4085 u32 seq_num; 4086 int error; 4087 4088 if (!instance) { 4089 printk(KERN_ERR "invalid instance!\n"); 4090 kfree(ev); 4091 return; 4092 } 4093 instance->ev = NULL; 4094 host = instance->host; 4095 if (instance->evt_detail) { 4096 4097 switch (instance->evt_detail->code) { 4098 case MR_EVT_PD_INSERTED: 4099 if (megasas_get_pd_list(instance) == 0) { 4100 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) { 4101 for (j = 0; 4102 j < MEGASAS_MAX_DEV_PER_CHANNEL; 4103 j++) { 4104 4105 pd_index = 4106 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j; 4107 4108 sdev1 = 4109 scsi_device_lookup(host, i, j, 0); 4110 4111 if (instance->pd_list[pd_index].driveState 4112 == MR_PD_STATE_SYSTEM) { 4113 if (!sdev1) { 4114 scsi_add_device(host, i, j, 0); 4115 } 4116 4117 if (sdev1) 4118 scsi_device_put(sdev1); 4119 } 4120 } 4121 } 4122 } 4123 doscan = 0; 4124 break; 4125 4126 case MR_EVT_PD_REMOVED: 4127 if (megasas_get_pd_list(instance) == 0) { 4128 megasas_get_pd_list(instance); 4129 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) { 4130 for (j = 0; 4131 j < MEGASAS_MAX_DEV_PER_CHANNEL; 4132 j++) { 4133 4134 pd_index = 4135 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j; 4136 4137 sdev1 = 4138 scsi_device_lookup(host, i, j, 0); 4139 4140 if (instance->pd_list[pd_index].driveState 4141 == MR_PD_STATE_SYSTEM) { 4142 if (sdev1) { 4143 scsi_device_put(sdev1); 4144 } 4145 } else { 4146 if (sdev1) { 4147 scsi_remove_device(sdev1); 4148 scsi_device_put(sdev1); 4149 } 4150 } 4151 } 4152 } 4153 } 4154 doscan = 0; 4155 break; 4156 4157 case MR_EVT_LD_OFFLINE: 4158 case MR_EVT_LD_DELETED: 4159 megasas_get_ld_list(instance); 4160 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) { 4161 for (j = 0; 4162 j < MEGASAS_MAX_DEV_PER_CHANNEL; 4163 j++) { 4164 4165 ld_index = 4166 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j; 4167 4168 sdev1 = scsi_device_lookup(host, 4169 i + MEGASAS_MAX_LD_CHANNELS, 4170 j, 4171 0); 4172 4173 if (instance->ld_ids[ld_index] != 0xff) { 4174 if (sdev1) { 4175 scsi_device_put(sdev1); 4176 } 4177 } else { 4178 if (sdev1) { 4179 scsi_remove_device(sdev1); 4180 scsi_device_put(sdev1); 4181 } 4182 } 4183 } 4184 } 4185 doscan = 0; 4186 break; 4187 case MR_EVT_LD_CREATED: 4188 megasas_get_ld_list(instance); 4189 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) { 4190 for (j = 0; 4191 j < MEGASAS_MAX_DEV_PER_CHANNEL; 4192 j++) { 4193 ld_index = 4194 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j; 4195 4196 sdev1 = scsi_device_lookup(host, 4197 i+MEGASAS_MAX_LD_CHANNELS, 4198 j, 0); 4199 4200 if (instance->ld_ids[ld_index] != 4201 0xff) { 4202 if (!sdev1) { 4203 scsi_add_device(host, 4204 i + 2, 4205 j, 0); 4206 } 4207 } 4208 if (sdev1) { 4209 scsi_device_put(sdev1); 4210 } 4211 } 4212 } 4213 doscan = 0; 4214 break; 4215 case MR_EVT_CTRL_HOST_BUS_SCAN_REQUESTED: 4216 case MR_EVT_FOREIGN_CFG_IMPORTED: 4217 doscan = 1; 4218 break; 4219 default: 4220 doscan = 0; 4221 break; 4222 } 4223 } else { 4224 printk(KERN_ERR "invalid evt_detail!\n"); 4225 kfree(ev); 4226 return; 4227 } 4228 4229 if (doscan) { 4230 printk(KERN_INFO "scanning ...\n"); 4231 megasas_get_pd_list(instance); 4232 for (i = 0; i < MEGASAS_MAX_PD_CHANNELS; i++) { 4233 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) { 4234 pd_index = i*MEGASAS_MAX_DEV_PER_CHANNEL + j; 4235 sdev1 = scsi_device_lookup(host, i, j, 0); 4236 if (instance->pd_list[pd_index].driveState == 4237 MR_PD_STATE_SYSTEM) { 4238 if (!sdev1) { 4239 scsi_add_device(host, i, j, 0); 4240 } 4241 if (sdev1) 4242 scsi_device_put(sdev1); 4243 } else { 4244 if (sdev1) { 4245 scsi_remove_device(sdev1); 4246 scsi_device_put(sdev1); 4247 } 4248 } 4249 } 4250 } 4251 4252 megasas_get_ld_list(instance); 4253 for (i = 0; i < MEGASAS_MAX_LD_CHANNELS; i++) { 4254 for (j = 0; j < MEGASAS_MAX_DEV_PER_CHANNEL; j++) { 4255 ld_index = 4256 (i * MEGASAS_MAX_DEV_PER_CHANNEL) + j; 4257 4258 sdev1 = scsi_device_lookup(host, 4259 i+MEGASAS_MAX_LD_CHANNELS, j, 0); 4260 if (instance->ld_ids[ld_index] != 0xff) { 4261 if (!sdev1) { 4262 scsi_add_device(host, 4263 i+2, 4264 j, 0); 4265 } else { 4266 scsi_device_put(sdev1); 4267 } 4268 } else { 4269 if (sdev1) { 4270 scsi_remove_device(sdev1); 4271 scsi_device_put(sdev1); 4272 } 4273 } 4274 } 4275 } 4276 } 4277 4278 if ( instance->aen_cmd != NULL ) { 4279 kfree(ev); 4280 return ; 4281 } 4282 4283 seq_num = instance->evt_detail->seq_num + 1; 4284 4285 /* Register AEN with FW for latest sequence number plus 1 */ 4286 class_locale.members.reserved = 0; 4287 class_locale.members.locale = MR_EVT_LOCALE_ALL; 4288 class_locale.members.class = MR_EVT_CLASS_DEBUG; 4289 mutex_lock(&instance->aen_mutex); 4290 error = megasas_register_aen(instance, seq_num, 4291 class_locale.word); 4292 mutex_unlock(&instance->aen_mutex); 4293 4294 if (error) 4295 printk(KERN_ERR "register aen failed error %x\n", error); 4296 4297 kfree(ev); 4298} 4299 4300 4301static DRIVER_ATTR(poll_mode_io, S_IRUGO|S_IWUSR, 4302 megasas_sysfs_show_poll_mode_io, 4303 megasas_sysfs_set_poll_mode_io); 4304 4305/** 4306 * megasas_init - Driver load entry point 4307 */ 4308static int __init megasas_init(void) 4309{ 4310 int rval; 4311 4312 /* 4313 * Announce driver version and other information 4314 */ 4315 printk(KERN_INFO "megasas: %s %s\n", MEGASAS_VERSION, 4316 MEGASAS_EXT_VERSION); 4317 4318 support_poll_for_event = 2; 4319 4320 memset(&megasas_mgmt_info, 0, sizeof(megasas_mgmt_info)); 4321 4322 /* 4323 * Register character device node 4324 */ 4325 rval = register_chrdev(0, "megaraid_sas_ioctl", &megasas_mgmt_fops); 4326 4327 if (rval < 0) { 4328 printk(KERN_DEBUG "megasas: failed to open device node\n"); 4329 return rval; 4330 } 4331 4332 megasas_mgmt_majorno = rval; 4333 4334 /* 4335 * Register ourselves as PCI hotplug module 4336 */ 4337 rval = pci_register_driver(&megasas_pci_driver); 4338 4339 if (rval) { 4340 printk(KERN_DEBUG "megasas: PCI hotplug regisration failed \n"); 4341 goto err_pcidrv; 4342 } 4343 4344 rval = driver_create_file(&megasas_pci_driver.driver, 4345 &driver_attr_version); 4346 if (rval) 4347 goto err_dcf_attr_ver; 4348 rval = driver_create_file(&megasas_pci_driver.driver, 4349 &driver_attr_release_date); 4350 if (rval) 4351 goto err_dcf_rel_date; 4352 4353 rval = driver_create_file(&megasas_pci_driver.driver, 4354 &driver_attr_support_poll_for_event); 4355 if (rval) 4356 goto err_dcf_support_poll_for_event; 4357 4358 rval = driver_create_file(&megasas_pci_driver.driver, 4359 &driver_attr_dbg_lvl); 4360 if (rval) 4361 goto err_dcf_dbg_lvl; 4362 rval = driver_create_file(&megasas_pci_driver.driver, 4363 &driver_attr_poll_mode_io); 4364 if (rval) 4365 goto err_dcf_poll_mode_io; 4366 4367 return rval; 4368 4369err_dcf_poll_mode_io: 4370 driver_remove_file(&megasas_pci_driver.driver, 4371 &driver_attr_dbg_lvl); 4372err_dcf_dbg_lvl: 4373 driver_remove_file(&megasas_pci_driver.driver, 4374 &driver_attr_support_poll_for_event); 4375 4376err_dcf_support_poll_for_event: 4377 driver_remove_file(&megasas_pci_driver.driver, 4378 &driver_attr_release_date); 4379 4380err_dcf_rel_date: 4381 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version); 4382err_dcf_attr_ver: 4383 pci_unregister_driver(&megasas_pci_driver); 4384err_pcidrv: 4385 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl"); 4386 return rval; 4387} 4388 4389/** 4390 * megasas_exit - Driver unload entry point 4391 */ 4392static void __exit megasas_exit(void) 4393{ 4394 driver_remove_file(&megasas_pci_driver.driver, 4395 &driver_attr_poll_mode_io); 4396 driver_remove_file(&megasas_pci_driver.driver, 4397 &driver_attr_dbg_lvl); 4398 driver_remove_file(&megasas_pci_driver.driver, 4399 &driver_attr_release_date); 4400 driver_remove_file(&megasas_pci_driver.driver, &driver_attr_version); 4401 4402 pci_unregister_driver(&megasas_pci_driver); 4403 unregister_chrdev(megasas_mgmt_majorno, "megaraid_sas_ioctl"); 4404} 4405 4406module_init(megasas_init); 4407module_exit(megasas_exit); 4408