1 /* 2 * Driver for the Micron P320 SSD 3 * Copyright (C) 2011 Micron Technology, Inc. 4 * 5 * Portions of this code were derived from works subjected to the 6 * following copyright: 7 * Copyright (C) 2009 Integrated Device Technology, Inc. 8 * 9 * This program is free software; you can redistribute it and/or modify 10 * it under the terms of the GNU General Public License as published by 11 * the Free Software Foundation; either version 2 of the License, or 12 * (at your option) any later version. 13 * 14 * This program is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU General Public License for more details. 18 * 19 */ 20 21 #include <linux/pci.h> 22 #include <linux/interrupt.h> 23 #include <linux/ata.h> 24 #include <linux/delay.h> 25 #include <linux/hdreg.h> 26 #include <linux/uaccess.h> 27 #include <linux/random.h> 28 #include <linux/smp.h> 29 #include <linux/compat.h> 30 #include <linux/fs.h> 31 #include <linux/module.h> 32 #include <linux/genhd.h> 33 #include <linux/blkdev.h> 34 #include <linux/blk-mq.h> 35 #include <linux/bio.h> 36 #include <linux/dma-mapping.h> 37 #include <linux/idr.h> 38 #include <linux/kthread.h> 39 #include <../drivers/ata/ahci.h> 40 #include <linux/export.h> 41 #include <linux/debugfs.h> 42 #include <linux/prefetch.h> 43 #include "mtip32xx.h" 44 45 #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32) 46 47 /* DMA region containing RX Fis, Identify, RLE10, and SMART buffers */ 48 #define AHCI_RX_FIS_SZ 0x100 49 #define AHCI_RX_FIS_OFFSET 0x0 50 #define AHCI_IDFY_SZ ATA_SECT_SIZE 51 #define AHCI_IDFY_OFFSET 0x400 52 #define AHCI_SECTBUF_SZ ATA_SECT_SIZE 53 #define AHCI_SECTBUF_OFFSET 0x800 54 #define AHCI_SMARTBUF_SZ ATA_SECT_SIZE 55 #define AHCI_SMARTBUF_OFFSET 0xC00 56 /* 0x100 + 0x200 + 0x200 + 0x200 is smaller than 4k but we pad it out */ 57 #define BLOCK_DMA_ALLOC_SZ 4096 58 59 /* DMA region containing command table (should be 8192 bytes) */ 60 #define AHCI_CMD_SLOT_SZ sizeof(struct mtip_cmd_hdr) 61 #define AHCI_CMD_TBL_SZ (MTIP_MAX_COMMAND_SLOTS * AHCI_CMD_SLOT_SZ) 62 #define AHCI_CMD_TBL_OFFSET 0x0 63 64 /* DMA region per command (contains header and SGL) */ 65 #define AHCI_CMD_TBL_HDR_SZ 0x80 66 #define AHCI_CMD_TBL_HDR_OFFSET 0x0 67 #define AHCI_CMD_TBL_SGL_SZ (MTIP_MAX_SG * sizeof(struct mtip_cmd_sg)) 68 #define AHCI_CMD_TBL_SGL_OFFSET AHCI_CMD_TBL_HDR_SZ 69 #define CMD_DMA_ALLOC_SZ (AHCI_CMD_TBL_SGL_SZ + AHCI_CMD_TBL_HDR_SZ) 70 71 72 #define HOST_CAP_NZDMA (1 << 19) 73 #define HOST_HSORG 0xFC 74 #define HSORG_DISABLE_SLOTGRP_INTR (1<<24) 75 #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16) 76 #define HSORG_HWREV 0xFF00 77 #define HSORG_STYLE 0x8 78 #define HSORG_SLOTGROUPS 0x7 79 80 #define PORT_COMMAND_ISSUE 0x38 81 #define PORT_SDBV 0x7C 82 83 #define PORT_OFFSET 0x100 84 #define PORT_MEM_SIZE 0x80 85 86 #define PORT_IRQ_ERR \ 87 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \ 88 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \ 89 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \ 90 PORT_IRQ_OVERFLOW) 91 #define PORT_IRQ_LEGACY \ 92 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS) 93 #define PORT_IRQ_HANDLED \ 94 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \ 95 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \ 96 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY) 97 #define DEF_PORT_IRQ \ 98 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS) 99 100 /* product numbers */ 101 #define MTIP_PRODUCT_UNKNOWN 0x00 102 #define MTIP_PRODUCT_ASICFPGA 0x11 103 104 /* Device instance number, incremented each time a device is probed. */ 105 static int instance; 106 107 static struct list_head online_list; 108 static struct list_head removing_list; 109 static spinlock_t dev_lock; 110 111 /* 112 * Global variable used to hold the major block device number 113 * allocated in mtip_init(). 114 */ 115 static int mtip_major; 116 static struct dentry *dfs_parent; 117 static struct dentry *dfs_device_status; 118 119 static u32 cpu_use[NR_CPUS]; 120 121 static DEFINE_SPINLOCK(rssd_index_lock); 122 static DEFINE_IDA(rssd_index_ida); 123 124 static int mtip_block_initialize(struct driver_data *dd); 125 126 #ifdef CONFIG_COMPAT 127 struct mtip_compat_ide_task_request_s { 128 __u8 io_ports[8]; 129 __u8 hob_ports[8]; 130 ide_reg_valid_t out_flags; 131 ide_reg_valid_t in_flags; 132 int data_phase; 133 int req_cmd; 134 compat_ulong_t out_size; 135 compat_ulong_t in_size; 136 }; 137 #endif 138 139 /* 140 * This function check_for_surprise_removal is called 141 * while card is removed from the system and it will 142 * read the vendor id from the configration space 143 * 144 * @pdev Pointer to the pci_dev structure. 145 * 146 * return value 147 * true if device removed, else false 148 */ 149 static bool mtip_check_surprise_removal(struct pci_dev *pdev) 150 { 151 u16 vendor_id = 0; 152 struct driver_data *dd = pci_get_drvdata(pdev); 153 154 if (dd->sr) 155 return true; 156 157 /* Read the vendorID from the configuration space */ 158 pci_read_config_word(pdev, 0x00, &vendor_id); 159 if (vendor_id == 0xFFFF) { 160 dd->sr = true; 161 if (dd->queue) 162 set_bit(QUEUE_FLAG_DEAD, &dd->queue->queue_flags); 163 else 164 dev_warn(&dd->pdev->dev, 165 "%s: dd->queue is NULL\n", __func__); 166 return true; /* device removed */ 167 } 168 169 return false; /* device present */ 170 } 171 172 /* we have to use runtime tag to setup command header */ 173 static void mtip_init_cmd_header(struct request *rq) 174 { 175 struct driver_data *dd = rq->q->queuedata; 176 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq); 177 u32 host_cap_64 = readl(dd->mmio + HOST_CAP) & HOST_CAP_64; 178 179 /* Point the command headers at the command tables. */ 180 cmd->command_header = dd->port->command_list + 181 (sizeof(struct mtip_cmd_hdr) * rq->tag); 182 cmd->command_header_dma = dd->port->command_list_dma + 183 (sizeof(struct mtip_cmd_hdr) * rq->tag); 184 185 if (host_cap_64) 186 cmd->command_header->ctbau = __force_bit2int cpu_to_le32((cmd->command_dma >> 16) >> 16); 187 188 cmd->command_header->ctba = __force_bit2int cpu_to_le32(cmd->command_dma & 0xFFFFFFFF); 189 } 190 191 static struct mtip_cmd *mtip_get_int_command(struct driver_data *dd) 192 { 193 struct request *rq; 194 195 if (mtip_check_surprise_removal(dd->pdev)) 196 return NULL; 197 198 rq = blk_mq_alloc_request(dd->queue, 0, BLK_MQ_REQ_RESERVED); 199 if (IS_ERR(rq)) 200 return NULL; 201 202 /* Internal cmd isn't submitted via .queue_rq */ 203 mtip_init_cmd_header(rq); 204 205 return blk_mq_rq_to_pdu(rq); 206 } 207 208 static void mtip_put_int_command(struct driver_data *dd, struct mtip_cmd *cmd) 209 { 210 blk_put_request(blk_mq_rq_from_pdu(cmd)); 211 } 212 213 /* 214 * Once we add support for one hctx per mtip group, this will change a bit 215 */ 216 static struct request *mtip_rq_from_tag(struct driver_data *dd, 217 unsigned int tag) 218 { 219 struct blk_mq_hw_ctx *hctx = dd->queue->queue_hw_ctx[0]; 220 221 return blk_mq_tag_to_rq(hctx->tags, tag); 222 } 223 224 static struct mtip_cmd *mtip_cmd_from_tag(struct driver_data *dd, 225 unsigned int tag) 226 { 227 struct request *rq = mtip_rq_from_tag(dd, tag); 228 229 return blk_mq_rq_to_pdu(rq); 230 } 231 232 /* 233 * IO completion function. 234 * 235 * This completion function is called by the driver ISR when a 236 * command that was issued by the kernel completes. It first calls the 237 * asynchronous completion function which normally calls back into the block 238 * layer passing the asynchronous callback data, then unmaps the 239 * scatter list associated with the completed command, and finally 240 * clears the allocated bit associated with the completed command. 241 * 242 * @port Pointer to the port data structure. 243 * @tag Tag of the command. 244 * @data Pointer to driver_data. 245 * @status Completion status. 246 * 247 * return value 248 * None 249 */ 250 static void mtip_async_complete(struct mtip_port *port, 251 int tag, struct mtip_cmd *cmd, int status) 252 { 253 struct driver_data *dd = port->dd; 254 struct request *rq; 255 256 if (unlikely(!dd) || unlikely(!port)) 257 return; 258 259 if (unlikely(status == PORT_IRQ_TF_ERR)) { 260 dev_warn(&port->dd->pdev->dev, 261 "Command tag %d failed due to TFE\n", tag); 262 } 263 264 rq = mtip_rq_from_tag(dd, tag); 265 266 cmd->status = status; 267 blk_mq_complete_request(rq); 268 } 269 270 /* 271 * Reset the HBA (without sleeping) 272 * 273 * @dd Pointer to the driver data structure. 274 * 275 * return value 276 * 0 The reset was successful. 277 * -1 The HBA Reset bit did not clear. 278 */ 279 static int mtip_hba_reset(struct driver_data *dd) 280 { 281 unsigned long timeout; 282 283 /* Set the reset bit */ 284 writel(HOST_RESET, dd->mmio + HOST_CTL); 285 286 /* Flush */ 287 readl(dd->mmio + HOST_CTL); 288 289 /* 290 * Spin for up to 10 seconds waiting for reset acknowledgement. Spec 291 * is 1 sec but in LUN failure conditions, up to 10 secs are required 292 */ 293 timeout = jiffies + msecs_to_jiffies(10000); 294 do { 295 mdelay(10); 296 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) 297 return -1; 298 299 } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET) 300 && time_before(jiffies, timeout)); 301 302 if (readl(dd->mmio + HOST_CTL) & HOST_RESET) 303 return -1; 304 305 return 0; 306 } 307 308 /* 309 * Issue a command to the hardware. 310 * 311 * Set the appropriate bit in the s_active and Command Issue hardware 312 * registers, causing hardware command processing to begin. 313 * 314 * @port Pointer to the port structure. 315 * @tag The tag of the command to be issued. 316 * 317 * return value 318 * None 319 */ 320 static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag) 321 { 322 int group = tag >> 5; 323 324 /* guard SACT and CI registers */ 325 spin_lock(&port->cmd_issue_lock[group]); 326 writel((1 << MTIP_TAG_BIT(tag)), 327 port->s_active[MTIP_TAG_INDEX(tag)]); 328 writel((1 << MTIP_TAG_BIT(tag)), 329 port->cmd_issue[MTIP_TAG_INDEX(tag)]); 330 spin_unlock(&port->cmd_issue_lock[group]); 331 } 332 333 /* 334 * Enable/disable the reception of FIS 335 * 336 * @port Pointer to the port data structure 337 * @enable 1 to enable, 0 to disable 338 * 339 * return value 340 * Previous state: 1 enabled, 0 disabled 341 */ 342 static int mtip_enable_fis(struct mtip_port *port, int enable) 343 { 344 u32 tmp; 345 346 /* enable FIS reception */ 347 tmp = readl(port->mmio + PORT_CMD); 348 if (enable) 349 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD); 350 else 351 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD); 352 353 /* Flush */ 354 readl(port->mmio + PORT_CMD); 355 356 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX)); 357 } 358 359 /* 360 * Enable/disable the DMA engine 361 * 362 * @port Pointer to the port data structure 363 * @enable 1 to enable, 0 to disable 364 * 365 * return value 366 * Previous state: 1 enabled, 0 disabled. 367 */ 368 static int mtip_enable_engine(struct mtip_port *port, int enable) 369 { 370 u32 tmp; 371 372 /* enable FIS reception */ 373 tmp = readl(port->mmio + PORT_CMD); 374 if (enable) 375 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD); 376 else 377 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD); 378 379 readl(port->mmio + PORT_CMD); 380 return (((tmp & PORT_CMD_START) == PORT_CMD_START)); 381 } 382 383 /* 384 * Enables the port DMA engine and FIS reception. 385 * 386 * return value 387 * None 388 */ 389 static inline void mtip_start_port(struct mtip_port *port) 390 { 391 /* Enable FIS reception */ 392 mtip_enable_fis(port, 1); 393 394 /* Enable the DMA engine */ 395 mtip_enable_engine(port, 1); 396 } 397 398 /* 399 * Deinitialize a port by disabling port interrupts, the DMA engine, 400 * and FIS reception. 401 * 402 * @port Pointer to the port structure 403 * 404 * return value 405 * None 406 */ 407 static inline void mtip_deinit_port(struct mtip_port *port) 408 { 409 /* Disable interrupts on this port */ 410 writel(0, port->mmio + PORT_IRQ_MASK); 411 412 /* Disable the DMA engine */ 413 mtip_enable_engine(port, 0); 414 415 /* Disable FIS reception */ 416 mtip_enable_fis(port, 0); 417 } 418 419 /* 420 * Initialize a port. 421 * 422 * This function deinitializes the port by calling mtip_deinit_port() and 423 * then initializes it by setting the command header and RX FIS addresses, 424 * clearing the SError register and any pending port interrupts before 425 * re-enabling the default set of port interrupts. 426 * 427 * @port Pointer to the port structure. 428 * 429 * return value 430 * None 431 */ 432 static void mtip_init_port(struct mtip_port *port) 433 { 434 int i; 435 mtip_deinit_port(port); 436 437 /* Program the command list base and FIS base addresses */ 438 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) { 439 writel((port->command_list_dma >> 16) >> 16, 440 port->mmio + PORT_LST_ADDR_HI); 441 writel((port->rxfis_dma >> 16) >> 16, 442 port->mmio + PORT_FIS_ADDR_HI); 443 } 444 445 writel(port->command_list_dma & 0xFFFFFFFF, 446 port->mmio + PORT_LST_ADDR); 447 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR); 448 449 /* Clear SError */ 450 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR); 451 452 /* reset the completed registers.*/ 453 for (i = 0; i < port->dd->slot_groups; i++) 454 writel(0xFFFFFFFF, port->completed[i]); 455 456 /* Clear any pending interrupts for this port */ 457 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT); 458 459 /* Clear any pending interrupts on the HBA. */ 460 writel(readl(port->dd->mmio + HOST_IRQ_STAT), 461 port->dd->mmio + HOST_IRQ_STAT); 462 463 /* Enable port interrupts */ 464 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK); 465 } 466 467 /* 468 * Restart a port 469 * 470 * @port Pointer to the port data structure. 471 * 472 * return value 473 * None 474 */ 475 static void mtip_restart_port(struct mtip_port *port) 476 { 477 unsigned long timeout; 478 479 /* Disable the DMA engine */ 480 mtip_enable_engine(port, 0); 481 482 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */ 483 timeout = jiffies + msecs_to_jiffies(500); 484 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) 485 && time_before(jiffies, timeout)) 486 ; 487 488 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag)) 489 return; 490 491 /* 492 * Chip quirk: escalate to hba reset if 493 * PxCMD.CR not clear after 500 ms 494 */ 495 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) { 496 dev_warn(&port->dd->pdev->dev, 497 "PxCMD.CR not clear, escalating reset\n"); 498 499 if (mtip_hba_reset(port->dd)) 500 dev_err(&port->dd->pdev->dev, 501 "HBA reset escalation failed.\n"); 502 503 /* 30 ms delay before com reset to quiesce chip */ 504 mdelay(30); 505 } 506 507 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n"); 508 509 /* Set PxSCTL.DET */ 510 writel(readl(port->mmio + PORT_SCR_CTL) | 511 1, port->mmio + PORT_SCR_CTL); 512 readl(port->mmio + PORT_SCR_CTL); 513 514 /* Wait 1 ms to quiesce chip function */ 515 timeout = jiffies + msecs_to_jiffies(1); 516 while (time_before(jiffies, timeout)) 517 ; 518 519 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag)) 520 return; 521 522 /* Clear PxSCTL.DET */ 523 writel(readl(port->mmio + PORT_SCR_CTL) & ~1, 524 port->mmio + PORT_SCR_CTL); 525 readl(port->mmio + PORT_SCR_CTL); 526 527 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */ 528 timeout = jiffies + msecs_to_jiffies(500); 529 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0) 530 && time_before(jiffies, timeout)) 531 ; 532 533 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag)) 534 return; 535 536 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0) 537 dev_warn(&port->dd->pdev->dev, 538 "COM reset failed\n"); 539 540 mtip_init_port(port); 541 mtip_start_port(port); 542 543 } 544 545 static int mtip_device_reset(struct driver_data *dd) 546 { 547 int rv = 0; 548 549 if (mtip_check_surprise_removal(dd->pdev)) 550 return 0; 551 552 if (mtip_hba_reset(dd) < 0) 553 rv = -EFAULT; 554 555 mdelay(1); 556 mtip_init_port(dd->port); 557 mtip_start_port(dd->port); 558 559 /* Enable interrupts on the HBA. */ 560 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN, 561 dd->mmio + HOST_CTL); 562 return rv; 563 } 564 565 /* 566 * Helper function for tag logging 567 */ 568 static void print_tags(struct driver_data *dd, 569 char *msg, 570 unsigned long *tagbits, 571 int cnt) 572 { 573 unsigned char tagmap[128]; 574 int group, tagmap_len = 0; 575 576 memset(tagmap, 0, sizeof(tagmap)); 577 for (group = SLOTBITS_IN_LONGS; group > 0; group--) 578 tagmap_len += sprintf(tagmap + tagmap_len, "%016lX ", 579 tagbits[group-1]); 580 dev_warn(&dd->pdev->dev, 581 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap); 582 } 583 584 /* 585 * Internal command completion callback function. 586 * 587 * This function is normally called by the driver ISR when an internal 588 * command completed. This function signals the command completion by 589 * calling complete(). 590 * 591 * @port Pointer to the port data structure. 592 * @tag Tag of the command that has completed. 593 * @data Pointer to a completion structure. 594 * @status Completion status. 595 * 596 * return value 597 * None 598 */ 599 static void mtip_completion(struct mtip_port *port, 600 int tag, struct mtip_cmd *command, int status) 601 { 602 struct completion *waiting = command->comp_data; 603 if (unlikely(status == PORT_IRQ_TF_ERR)) 604 dev_warn(&port->dd->pdev->dev, 605 "Internal command %d completed with TFE\n", tag); 606 607 command->comp_func = NULL; 608 command->comp_data = NULL; 609 complete(waiting); 610 } 611 612 static void mtip_null_completion(struct mtip_port *port, 613 int tag, struct mtip_cmd *command, int status) 614 { 615 } 616 617 static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer, 618 dma_addr_t buffer_dma, unsigned int sectors); 619 static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id, 620 struct smart_attr *attrib); 621 /* 622 * Handle an error. 623 * 624 * @dd Pointer to the DRIVER_DATA structure. 625 * 626 * return value 627 * None 628 */ 629 static void mtip_handle_tfe(struct driver_data *dd) 630 { 631 int group, tag, bit, reissue, rv; 632 struct mtip_port *port; 633 struct mtip_cmd *cmd; 634 u32 completed; 635 struct host_to_dev_fis *fis; 636 unsigned long tagaccum[SLOTBITS_IN_LONGS]; 637 unsigned int cmd_cnt = 0; 638 unsigned char *buf; 639 char *fail_reason = NULL; 640 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0; 641 642 dev_warn(&dd->pdev->dev, "Taskfile error\n"); 643 644 port = dd->port; 645 646 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) { 647 cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL); 648 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n"); 649 650 if (cmd->comp_data && cmd->comp_func) { 651 cmd->comp_func(port, MTIP_TAG_INTERNAL, 652 cmd, PORT_IRQ_TF_ERR); 653 } 654 return; 655 } 656 657 /* clear the tag accumulator */ 658 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long)); 659 660 /* Loop through all the groups */ 661 for (group = 0; group < dd->slot_groups; group++) { 662 completed = readl(port->completed[group]); 663 664 dev_warn(&dd->pdev->dev, "g=%u, comp=%x\n", group, completed); 665 666 /* clear completed status register in the hardware.*/ 667 writel(completed, port->completed[group]); 668 669 /* Process successfully completed commands */ 670 for (bit = 0; bit < 32 && completed; bit++) { 671 if (!(completed & (1<<bit))) 672 continue; 673 tag = (group << 5) + bit; 674 675 /* Skip the internal command slot */ 676 if (tag == MTIP_TAG_INTERNAL) 677 continue; 678 679 cmd = mtip_cmd_from_tag(dd, tag); 680 if (likely(cmd->comp_func)) { 681 set_bit(tag, tagaccum); 682 cmd_cnt++; 683 cmd->comp_func(port, tag, cmd, 0); 684 } else { 685 dev_err(&port->dd->pdev->dev, 686 "Missing completion func for tag %d", 687 tag); 688 if (mtip_check_surprise_removal(dd->pdev)) { 689 /* don't proceed further */ 690 return; 691 } 692 } 693 } 694 } 695 696 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt); 697 698 /* Restart the port */ 699 mdelay(20); 700 mtip_restart_port(port); 701 702 /* Trying to determine the cause of the error */ 703 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ, 704 dd->port->log_buf, 705 dd->port->log_buf_dma, 1); 706 if (rv) { 707 dev_warn(&dd->pdev->dev, 708 "Error in READ LOG EXT (10h) command\n"); 709 /* non-critical error, don't fail the load */ 710 } else { 711 buf = (unsigned char *)dd->port->log_buf; 712 if (buf[259] & 0x1) { 713 dev_info(&dd->pdev->dev, 714 "Write protect bit is set.\n"); 715 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag); 716 fail_all_ncq_write = 1; 717 fail_reason = "write protect"; 718 } 719 if (buf[288] == 0xF7) { 720 dev_info(&dd->pdev->dev, 721 "Exceeded Tmax, drive in thermal shutdown.\n"); 722 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag); 723 fail_all_ncq_cmds = 1; 724 fail_reason = "thermal shutdown"; 725 } 726 if (buf[288] == 0xBF) { 727 set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag); 728 dev_info(&dd->pdev->dev, 729 "Drive indicates rebuild has failed. Secure erase required.\n"); 730 fail_all_ncq_cmds = 1; 731 fail_reason = "rebuild failed"; 732 } 733 } 734 735 /* clear the tag accumulator */ 736 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long)); 737 738 /* Loop through all the groups */ 739 for (group = 0; group < dd->slot_groups; group++) { 740 for (bit = 0; bit < 32; bit++) { 741 reissue = 1; 742 tag = (group << 5) + bit; 743 cmd = mtip_cmd_from_tag(dd, tag); 744 745 fis = (struct host_to_dev_fis *)cmd->command; 746 747 /* Should re-issue? */ 748 if (tag == MTIP_TAG_INTERNAL || 749 fis->command == ATA_CMD_SET_FEATURES) 750 reissue = 0; 751 else { 752 if (fail_all_ncq_cmds || 753 (fail_all_ncq_write && 754 fis->command == ATA_CMD_FPDMA_WRITE)) { 755 dev_warn(&dd->pdev->dev, 756 " Fail: %s w/tag %d [%s].\n", 757 fis->command == ATA_CMD_FPDMA_WRITE ? 758 "write" : "read", 759 tag, 760 fail_reason != NULL ? 761 fail_reason : "unknown"); 762 if (cmd->comp_func) { 763 cmd->comp_func(port, tag, 764 cmd, -ENODATA); 765 } 766 continue; 767 } 768 } 769 770 /* 771 * First check if this command has 772 * exceeded its retries. 773 */ 774 if (reissue && (cmd->retries-- > 0)) { 775 776 set_bit(tag, tagaccum); 777 778 /* Re-issue the command. */ 779 mtip_issue_ncq_command(port, tag); 780 781 continue; 782 } 783 784 /* Retire a command that will not be reissued */ 785 dev_warn(&port->dd->pdev->dev, 786 "retiring tag %d\n", tag); 787 788 if (cmd->comp_func) 789 cmd->comp_func(port, tag, cmd, PORT_IRQ_TF_ERR); 790 else 791 dev_warn(&port->dd->pdev->dev, 792 "Bad completion for tag %d\n", 793 tag); 794 } 795 } 796 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt); 797 } 798 799 /* 800 * Handle a set device bits interrupt 801 */ 802 static inline void mtip_workq_sdbfx(struct mtip_port *port, int group, 803 u32 completed) 804 { 805 struct driver_data *dd = port->dd; 806 int tag, bit; 807 struct mtip_cmd *command; 808 809 if (!completed) { 810 WARN_ON_ONCE(!completed); 811 return; 812 } 813 /* clear completed status register in the hardware.*/ 814 writel(completed, port->completed[group]); 815 816 /* Process completed commands. */ 817 for (bit = 0; (bit < 32) && completed; bit++) { 818 if (completed & 0x01) { 819 tag = (group << 5) | bit; 820 821 /* skip internal command slot. */ 822 if (unlikely(tag == MTIP_TAG_INTERNAL)) 823 continue; 824 825 command = mtip_cmd_from_tag(dd, tag); 826 if (likely(command->comp_func)) 827 command->comp_func(port, tag, command, 0); 828 else { 829 dev_dbg(&dd->pdev->dev, 830 "Null completion for tag %d", 831 tag); 832 833 if (mtip_check_surprise_removal( 834 dd->pdev)) { 835 return; 836 } 837 } 838 } 839 completed >>= 1; 840 } 841 842 /* If last, re-enable interrupts */ 843 if (atomic_dec_return(&dd->irq_workers_active) == 0) 844 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT); 845 } 846 847 /* 848 * Process legacy pio and d2h interrupts 849 */ 850 static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat) 851 { 852 struct mtip_port *port = dd->port; 853 struct mtip_cmd *cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL); 854 855 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) && 856 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL]) 857 & (1 << MTIP_TAG_INTERNAL))) { 858 if (cmd->comp_func) { 859 cmd->comp_func(port, MTIP_TAG_INTERNAL, cmd, 0); 860 return; 861 } 862 } 863 864 return; 865 } 866 867 /* 868 * Demux and handle errors 869 */ 870 static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat) 871 { 872 873 if (unlikely(port_stat & PORT_IRQ_CONNECT)) { 874 dev_warn(&dd->pdev->dev, 875 "Clearing PxSERR.DIAG.x\n"); 876 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR); 877 } 878 879 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) { 880 dev_warn(&dd->pdev->dev, 881 "Clearing PxSERR.DIAG.n\n"); 882 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR); 883 } 884 885 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) { 886 dev_warn(&dd->pdev->dev, 887 "Port stat errors %x unhandled\n", 888 (port_stat & ~PORT_IRQ_HANDLED)); 889 if (mtip_check_surprise_removal(dd->pdev)) 890 return; 891 } 892 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR))) { 893 set_bit(MTIP_PF_EH_ACTIVE_BIT, &dd->port->flags); 894 wake_up_interruptible(&dd->port->svc_wait); 895 } 896 } 897 898 static inline irqreturn_t mtip_handle_irq(struct driver_data *data) 899 { 900 struct driver_data *dd = (struct driver_data *) data; 901 struct mtip_port *port = dd->port; 902 u32 hba_stat, port_stat; 903 int rv = IRQ_NONE; 904 int do_irq_enable = 1, i, workers; 905 struct mtip_work *twork; 906 907 hba_stat = readl(dd->mmio + HOST_IRQ_STAT); 908 if (hba_stat) { 909 rv = IRQ_HANDLED; 910 911 /* Acknowledge the interrupt status on the port.*/ 912 port_stat = readl(port->mmio + PORT_IRQ_STAT); 913 if (unlikely(port_stat == 0xFFFFFFFF)) { 914 mtip_check_surprise_removal(dd->pdev); 915 return IRQ_HANDLED; 916 } 917 writel(port_stat, port->mmio + PORT_IRQ_STAT); 918 919 /* Demux port status */ 920 if (likely(port_stat & PORT_IRQ_SDB_FIS)) { 921 do_irq_enable = 0; 922 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0); 923 924 /* Start at 1: group zero is always local? */ 925 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS; 926 i++) { 927 twork = &dd->work[i]; 928 twork->completed = readl(port->completed[i]); 929 if (twork->completed) 930 workers++; 931 } 932 933 atomic_set(&dd->irq_workers_active, workers); 934 if (workers) { 935 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) { 936 twork = &dd->work[i]; 937 if (twork->completed) 938 queue_work_on( 939 twork->cpu_binding, 940 dd->isr_workq, 941 &twork->work); 942 } 943 944 if (likely(dd->work[0].completed)) 945 mtip_workq_sdbfx(port, 0, 946 dd->work[0].completed); 947 948 } else { 949 /* 950 * Chip quirk: SDB interrupt but nothing 951 * to complete 952 */ 953 do_irq_enable = 1; 954 } 955 } 956 957 if (unlikely(port_stat & PORT_IRQ_ERR)) { 958 if (unlikely(mtip_check_surprise_removal(dd->pdev))) { 959 /* don't proceed further */ 960 return IRQ_HANDLED; 961 } 962 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, 963 &dd->dd_flag)) 964 return rv; 965 966 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR); 967 } 968 969 if (unlikely(port_stat & PORT_IRQ_LEGACY)) 970 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY); 971 } 972 973 /* acknowledge interrupt */ 974 if (unlikely(do_irq_enable)) 975 writel(hba_stat, dd->mmio + HOST_IRQ_STAT); 976 977 return rv; 978 } 979 980 /* 981 * HBA interrupt subroutine. 982 * 983 * @irq IRQ number. 984 * @instance Pointer to the driver data structure. 985 * 986 * return value 987 * IRQ_HANDLED A HBA interrupt was pending and handled. 988 * IRQ_NONE This interrupt was not for the HBA. 989 */ 990 static irqreturn_t mtip_irq_handler(int irq, void *instance) 991 { 992 struct driver_data *dd = instance; 993 994 return mtip_handle_irq(dd); 995 } 996 997 static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag) 998 { 999 writel(1 << MTIP_TAG_BIT(tag), 1000 port->cmd_issue[MTIP_TAG_INDEX(tag)]); 1001 } 1002 1003 static bool mtip_pause_ncq(struct mtip_port *port, 1004 struct host_to_dev_fis *fis) 1005 { 1006 struct host_to_dev_fis *reply; 1007 unsigned long task_file_data; 1008 1009 reply = port->rxfis + RX_FIS_D2H_REG; 1010 task_file_data = readl(port->mmio+PORT_TFDATA); 1011 1012 if ((task_file_data & 1)) 1013 return false; 1014 1015 if (fis->command == ATA_CMD_SEC_ERASE_PREP) { 1016 port->ic_pause_timer = jiffies; 1017 return true; 1018 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) && 1019 (fis->features == 0x03)) { 1020 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags); 1021 port->ic_pause_timer = jiffies; 1022 return true; 1023 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) || 1024 ((fis->command == 0xFC) && 1025 (fis->features == 0x27 || fis->features == 0x72 || 1026 fis->features == 0x62 || fis->features == 0x26))) { 1027 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag); 1028 clear_bit(MTIP_DDF_REBUILD_FAILED_BIT, &port->dd->dd_flag); 1029 /* Com reset after secure erase or lowlevel format */ 1030 mtip_restart_port(port); 1031 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags); 1032 return false; 1033 } 1034 1035 return false; 1036 } 1037 1038 /* 1039 * Wait for port to quiesce 1040 * 1041 * @port Pointer to port data structure 1042 * @timeout Max duration to wait (ms) 1043 * @atomic gfp_t flag to indicate blockable context or not 1044 * 1045 * return value 1046 * 0 Success 1047 * -EBUSY Commands still active 1048 */ 1049 static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout, 1050 gfp_t atomic) 1051 { 1052 unsigned long to; 1053 unsigned int n; 1054 unsigned int active = 1; 1055 1056 blk_mq_stop_hw_queues(port->dd->queue); 1057 1058 to = jiffies + msecs_to_jiffies(timeout); 1059 do { 1060 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) && 1061 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags) && 1062 atomic == GFP_KERNEL) { 1063 msleep(20); 1064 continue; /* svc thd is actively issuing commands */ 1065 } 1066 1067 if (atomic == GFP_KERNEL) 1068 msleep(100); 1069 else { 1070 cpu_relax(); 1071 udelay(100); 1072 } 1073 1074 if (mtip_check_surprise_removal(port->dd->pdev)) 1075 goto err_fault; 1076 1077 /* 1078 * Ignore s_active bit 0 of array element 0. 1079 * This bit will always be set 1080 */ 1081 active = readl(port->s_active[0]) & 0xFFFFFFFE; 1082 for (n = 1; n < port->dd->slot_groups; n++) 1083 active |= readl(port->s_active[n]); 1084 1085 if (!active) 1086 break; 1087 } while (time_before(jiffies, to)); 1088 1089 blk_mq_start_stopped_hw_queues(port->dd->queue, true); 1090 return active ? -EBUSY : 0; 1091 err_fault: 1092 blk_mq_start_stopped_hw_queues(port->dd->queue, true); 1093 return -EFAULT; 1094 } 1095 1096 /* 1097 * Execute an internal command and wait for the completion. 1098 * 1099 * @port Pointer to the port data structure. 1100 * @fis Pointer to the FIS that describes the command. 1101 * @fis_len Length in WORDS of the FIS. 1102 * @buffer DMA accessible for command data. 1103 * @buf_len Length, in bytes, of the data buffer. 1104 * @opts Command header options, excluding the FIS length 1105 * and the number of PRD entries. 1106 * @timeout Time in ms to wait for the command to complete. 1107 * 1108 * return value 1109 * 0 Command completed successfully. 1110 * -EFAULT The buffer address is not correctly aligned. 1111 * -EBUSY Internal command or other IO in progress. 1112 * -EAGAIN Time out waiting for command to complete. 1113 */ 1114 static int mtip_exec_internal_command(struct mtip_port *port, 1115 struct host_to_dev_fis *fis, 1116 int fis_len, 1117 dma_addr_t buffer, 1118 int buf_len, 1119 u32 opts, 1120 gfp_t atomic, 1121 unsigned long timeout) 1122 { 1123 struct mtip_cmd_sg *command_sg; 1124 DECLARE_COMPLETION_ONSTACK(wait); 1125 struct mtip_cmd *int_cmd; 1126 struct driver_data *dd = port->dd; 1127 int rv = 0; 1128 unsigned long start; 1129 1130 /* Make sure the buffer is 8 byte aligned. This is asic specific. */ 1131 if (buffer & 0x00000007) { 1132 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n"); 1133 return -EFAULT; 1134 } 1135 1136 int_cmd = mtip_get_int_command(dd); 1137 if (!int_cmd) { 1138 dbg_printk(MTIP_DRV_NAME "Unable to allocate tag for PIO cmd\n"); 1139 return -EFAULT; 1140 } 1141 1142 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags); 1143 1144 if (fis->command == ATA_CMD_SEC_ERASE_PREP) 1145 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags); 1146 1147 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags); 1148 1149 if (atomic == GFP_KERNEL) { 1150 if (fis->command != ATA_CMD_STANDBYNOW1) { 1151 /* wait for io to complete if non atomic */ 1152 if (mtip_quiesce_io(port, 1153 MTIP_QUIESCE_IO_TIMEOUT_MS, atomic) < 0) { 1154 dev_warn(&dd->pdev->dev, 1155 "Failed to quiesce IO\n"); 1156 mtip_put_int_command(dd, int_cmd); 1157 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags); 1158 wake_up_interruptible(&port->svc_wait); 1159 return -EBUSY; 1160 } 1161 } 1162 1163 /* Set the completion function and data for the command. */ 1164 int_cmd->comp_data = &wait; 1165 int_cmd->comp_func = mtip_completion; 1166 1167 } else { 1168 /* Clear completion - we're going to poll */ 1169 int_cmd->comp_data = NULL; 1170 int_cmd->comp_func = mtip_null_completion; 1171 } 1172 1173 /* Copy the command to the command table */ 1174 memcpy(int_cmd->command, fis, fis_len*4); 1175 1176 /* Populate the SG list */ 1177 int_cmd->command_header->opts = 1178 __force_bit2int cpu_to_le32(opts | fis_len); 1179 if (buf_len) { 1180 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ; 1181 1182 command_sg->info = 1183 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF); 1184 command_sg->dba = 1185 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF); 1186 command_sg->dba_upper = 1187 __force_bit2int cpu_to_le32((buffer >> 16) >> 16); 1188 1189 int_cmd->command_header->opts |= 1190 __force_bit2int cpu_to_le32((1 << 16)); 1191 } 1192 1193 /* Populate the command header */ 1194 int_cmd->command_header->byte_count = 0; 1195 1196 start = jiffies; 1197 1198 /* Issue the command to the hardware */ 1199 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL); 1200 1201 if (atomic == GFP_KERNEL) { 1202 /* Wait for the command to complete or timeout. */ 1203 if ((rv = wait_for_completion_interruptible_timeout( 1204 &wait, 1205 msecs_to_jiffies(timeout))) <= 0) { 1206 1207 if (rv == -ERESTARTSYS) { /* interrupted */ 1208 dev_err(&dd->pdev->dev, 1209 "Internal command [%02X] was interrupted after %u ms\n", 1210 fis->command, 1211 jiffies_to_msecs(jiffies - start)); 1212 rv = -EINTR; 1213 goto exec_ic_exit; 1214 } else if (rv == 0) /* timeout */ 1215 dev_err(&dd->pdev->dev, 1216 "Internal command did not complete [%02X] within timeout of %lu ms\n", 1217 fis->command, timeout); 1218 else 1219 dev_err(&dd->pdev->dev, 1220 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n", 1221 fis->command, rv, timeout); 1222 1223 if (mtip_check_surprise_removal(dd->pdev) || 1224 test_bit(MTIP_DDF_REMOVE_PENDING_BIT, 1225 &dd->dd_flag)) { 1226 dev_err(&dd->pdev->dev, 1227 "Internal command [%02X] wait returned due to SR\n", 1228 fis->command); 1229 rv = -ENXIO; 1230 goto exec_ic_exit; 1231 } 1232 mtip_device_reset(dd); /* recover from timeout issue */ 1233 rv = -EAGAIN; 1234 goto exec_ic_exit; 1235 } 1236 } else { 1237 u32 hba_stat, port_stat; 1238 1239 /* Spin for <timeout> checking if command still outstanding */ 1240 timeout = jiffies + msecs_to_jiffies(timeout); 1241 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL]) 1242 & (1 << MTIP_TAG_INTERNAL)) 1243 && time_before(jiffies, timeout)) { 1244 if (mtip_check_surprise_removal(dd->pdev)) { 1245 rv = -ENXIO; 1246 goto exec_ic_exit; 1247 } 1248 if ((fis->command != ATA_CMD_STANDBYNOW1) && 1249 test_bit(MTIP_DDF_REMOVE_PENDING_BIT, 1250 &dd->dd_flag)) { 1251 rv = -ENXIO; 1252 goto exec_ic_exit; 1253 } 1254 port_stat = readl(port->mmio + PORT_IRQ_STAT); 1255 if (!port_stat) 1256 continue; 1257 1258 if (port_stat & PORT_IRQ_ERR) { 1259 dev_err(&dd->pdev->dev, 1260 "Internal command [%02X] failed\n", 1261 fis->command); 1262 mtip_device_reset(dd); 1263 rv = -EIO; 1264 goto exec_ic_exit; 1265 } else { 1266 writel(port_stat, port->mmio + PORT_IRQ_STAT); 1267 hba_stat = readl(dd->mmio + HOST_IRQ_STAT); 1268 if (hba_stat) 1269 writel(hba_stat, 1270 dd->mmio + HOST_IRQ_STAT); 1271 } 1272 break; 1273 } 1274 } 1275 1276 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL]) 1277 & (1 << MTIP_TAG_INTERNAL)) { 1278 rv = -ENXIO; 1279 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) { 1280 mtip_device_reset(dd); 1281 rv = -EAGAIN; 1282 } 1283 } 1284 exec_ic_exit: 1285 /* Clear the allocated and active bits for the internal command. */ 1286 mtip_put_int_command(dd, int_cmd); 1287 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags); 1288 if (rv >= 0 && mtip_pause_ncq(port, fis)) { 1289 /* NCQ paused */ 1290 return rv; 1291 } 1292 wake_up_interruptible(&port->svc_wait); 1293 1294 return rv; 1295 } 1296 1297 /* 1298 * Byte-swap ATA ID strings. 1299 * 1300 * ATA identify data contains strings in byte-swapped 16-bit words. 1301 * They must be swapped (on all architectures) to be usable as C strings. 1302 * This function swaps bytes in-place. 1303 * 1304 * @buf The buffer location of the string 1305 * @len The number of bytes to swap 1306 * 1307 * return value 1308 * None 1309 */ 1310 static inline void ata_swap_string(u16 *buf, unsigned int len) 1311 { 1312 int i; 1313 for (i = 0; i < (len/2); i++) 1314 be16_to_cpus(&buf[i]); 1315 } 1316 1317 static void mtip_set_timeout(struct driver_data *dd, 1318 struct host_to_dev_fis *fis, 1319 unsigned int *timeout, u8 erasemode) 1320 { 1321 switch (fis->command) { 1322 case ATA_CMD_DOWNLOAD_MICRO: 1323 *timeout = 120000; /* 2 minutes */ 1324 break; 1325 case ATA_CMD_SEC_ERASE_UNIT: 1326 case 0xFC: 1327 if (erasemode) 1328 *timeout = ((*(dd->port->identify + 90) * 2) * 60000); 1329 else 1330 *timeout = ((*(dd->port->identify + 89) * 2) * 60000); 1331 break; 1332 case ATA_CMD_STANDBYNOW1: 1333 *timeout = 120000; /* 2 minutes */ 1334 break; 1335 case 0xF7: 1336 case 0xFA: 1337 *timeout = 60000; /* 60 seconds */ 1338 break; 1339 case ATA_CMD_SMART: 1340 *timeout = 15000; /* 15 seconds */ 1341 break; 1342 default: 1343 *timeout = MTIP_IOCTL_CMD_TIMEOUT_MS; 1344 break; 1345 } 1346 } 1347 1348 /* 1349 * Request the device identity information. 1350 * 1351 * If a user space buffer is not specified, i.e. is NULL, the 1352 * identify information is still read from the drive and placed 1353 * into the identify data buffer (@e port->identify) in the 1354 * port data structure. 1355 * When the identify buffer contains valid identify information @e 1356 * port->identify_valid is non-zero. 1357 * 1358 * @port Pointer to the port structure. 1359 * @user_buffer A user space buffer where the identify data should be 1360 * copied. 1361 * 1362 * return value 1363 * 0 Command completed successfully. 1364 * -EFAULT An error occurred while coping data to the user buffer. 1365 * -1 Command failed. 1366 */ 1367 static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer) 1368 { 1369 int rv = 0; 1370 struct host_to_dev_fis fis; 1371 1372 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag)) 1373 return -EFAULT; 1374 1375 /* Build the FIS. */ 1376 memset(&fis, 0, sizeof(struct host_to_dev_fis)); 1377 fis.type = 0x27; 1378 fis.opts = 1 << 7; 1379 fis.command = ATA_CMD_ID_ATA; 1380 1381 /* Set the identify information as invalid. */ 1382 port->identify_valid = 0; 1383 1384 /* Clear the identify information. */ 1385 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS); 1386 1387 /* Execute the command. */ 1388 if (mtip_exec_internal_command(port, 1389 &fis, 1390 5, 1391 port->identify_dma, 1392 sizeof(u16) * ATA_ID_WORDS, 1393 0, 1394 GFP_KERNEL, 1395 MTIP_INT_CMD_TIMEOUT_MS) 1396 < 0) { 1397 rv = -1; 1398 goto out; 1399 } 1400 1401 /* 1402 * Perform any necessary byte-swapping. Yes, the kernel does in fact 1403 * perform field-sensitive swapping on the string fields. 1404 * See the kernel use of ata_id_string() for proof of this. 1405 */ 1406 #ifdef __LITTLE_ENDIAN 1407 ata_swap_string(port->identify + 27, 40); /* model string*/ 1408 ata_swap_string(port->identify + 23, 8); /* firmware string*/ 1409 ata_swap_string(port->identify + 10, 20); /* serial# string*/ 1410 #else 1411 { 1412 int i; 1413 for (i = 0; i < ATA_ID_WORDS; i++) 1414 port->identify[i] = le16_to_cpu(port->identify[i]); 1415 } 1416 #endif 1417 1418 /* Check security locked state */ 1419 if (port->identify[128] & 0x4) 1420 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag); 1421 else 1422 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag); 1423 1424 #ifdef MTIP_TRIM /* Disabling TRIM support temporarily */ 1425 /* Demux ID.DRAT & ID.RZAT to determine trim support */ 1426 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5)) 1427 port->dd->trim_supp = true; 1428 else 1429 #endif 1430 port->dd->trim_supp = false; 1431 1432 /* Set the identify buffer as valid. */ 1433 port->identify_valid = 1; 1434 1435 if (user_buffer) { 1436 if (copy_to_user( 1437 user_buffer, 1438 port->identify, 1439 ATA_ID_WORDS * sizeof(u16))) { 1440 rv = -EFAULT; 1441 goto out; 1442 } 1443 } 1444 1445 out: 1446 return rv; 1447 } 1448 1449 /* 1450 * Issue a standby immediate command to the device. 1451 * 1452 * @port Pointer to the port structure. 1453 * 1454 * return value 1455 * 0 Command was executed successfully. 1456 * -1 An error occurred while executing the command. 1457 */ 1458 static int mtip_standby_immediate(struct mtip_port *port) 1459 { 1460 int rv; 1461 struct host_to_dev_fis fis; 1462 unsigned long start; 1463 unsigned int timeout; 1464 1465 /* Build the FIS. */ 1466 memset(&fis, 0, sizeof(struct host_to_dev_fis)); 1467 fis.type = 0x27; 1468 fis.opts = 1 << 7; 1469 fis.command = ATA_CMD_STANDBYNOW1; 1470 1471 mtip_set_timeout(port->dd, &fis, &timeout, 0); 1472 1473 start = jiffies; 1474 rv = mtip_exec_internal_command(port, 1475 &fis, 1476 5, 1477 0, 1478 0, 1479 0, 1480 GFP_ATOMIC, 1481 timeout); 1482 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n", 1483 jiffies_to_msecs(jiffies - start)); 1484 if (rv) 1485 dev_warn(&port->dd->pdev->dev, 1486 "STANDBY IMMEDIATE command failed.\n"); 1487 1488 return rv; 1489 } 1490 1491 /* 1492 * Issue a READ LOG EXT command to the device. 1493 * 1494 * @port pointer to the port structure. 1495 * @page page number to fetch 1496 * @buffer pointer to buffer 1497 * @buffer_dma dma address corresponding to @buffer 1498 * @sectors page length to fetch, in sectors 1499 * 1500 * return value 1501 * @rv return value from mtip_exec_internal_command() 1502 */ 1503 static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer, 1504 dma_addr_t buffer_dma, unsigned int sectors) 1505 { 1506 struct host_to_dev_fis fis; 1507 1508 memset(&fis, 0, sizeof(struct host_to_dev_fis)); 1509 fis.type = 0x27; 1510 fis.opts = 1 << 7; 1511 fis.command = ATA_CMD_READ_LOG_EXT; 1512 fis.sect_count = sectors & 0xFF; 1513 fis.sect_cnt_ex = (sectors >> 8) & 0xFF; 1514 fis.lba_low = page; 1515 fis.lba_mid = 0; 1516 fis.device = ATA_DEVICE_OBS; 1517 1518 memset(buffer, 0, sectors * ATA_SECT_SIZE); 1519 1520 return mtip_exec_internal_command(port, 1521 &fis, 1522 5, 1523 buffer_dma, 1524 sectors * ATA_SECT_SIZE, 1525 0, 1526 GFP_ATOMIC, 1527 MTIP_INT_CMD_TIMEOUT_MS); 1528 } 1529 1530 /* 1531 * Issue a SMART READ DATA command to the device. 1532 * 1533 * @port pointer to the port structure. 1534 * @buffer pointer to buffer 1535 * @buffer_dma dma address corresponding to @buffer 1536 * 1537 * return value 1538 * @rv return value from mtip_exec_internal_command() 1539 */ 1540 static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer, 1541 dma_addr_t buffer_dma) 1542 { 1543 struct host_to_dev_fis fis; 1544 1545 memset(&fis, 0, sizeof(struct host_to_dev_fis)); 1546 fis.type = 0x27; 1547 fis.opts = 1 << 7; 1548 fis.command = ATA_CMD_SMART; 1549 fis.features = 0xD0; 1550 fis.sect_count = 1; 1551 fis.lba_mid = 0x4F; 1552 fis.lba_hi = 0xC2; 1553 fis.device = ATA_DEVICE_OBS; 1554 1555 return mtip_exec_internal_command(port, 1556 &fis, 1557 5, 1558 buffer_dma, 1559 ATA_SECT_SIZE, 1560 0, 1561 GFP_ATOMIC, 1562 15000); 1563 } 1564 1565 /* 1566 * Get the value of a smart attribute 1567 * 1568 * @port pointer to the port structure 1569 * @id attribute number 1570 * @attrib pointer to return attrib information corresponding to @id 1571 * 1572 * return value 1573 * -EINVAL NULL buffer passed or unsupported attribute @id. 1574 * -EPERM Identify data not valid, SMART not supported or not enabled 1575 */ 1576 static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id, 1577 struct smart_attr *attrib) 1578 { 1579 int rv, i; 1580 struct smart_attr *pattr; 1581 1582 if (!attrib) 1583 return -EINVAL; 1584 1585 if (!port->identify_valid) { 1586 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n"); 1587 return -EPERM; 1588 } 1589 if (!(port->identify[82] & 0x1)) { 1590 dev_warn(&port->dd->pdev->dev, "SMART not supported\n"); 1591 return -EPERM; 1592 } 1593 if (!(port->identify[85] & 0x1)) { 1594 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n"); 1595 return -EPERM; 1596 } 1597 1598 memset(port->smart_buf, 0, ATA_SECT_SIZE); 1599 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma); 1600 if (rv) { 1601 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n"); 1602 return rv; 1603 } 1604 1605 pattr = (struct smart_attr *)(port->smart_buf + 2); 1606 for (i = 0; i < 29; i++, pattr++) 1607 if (pattr->attr_id == id) { 1608 memcpy(attrib, pattr, sizeof(struct smart_attr)); 1609 break; 1610 } 1611 1612 if (i == 29) { 1613 dev_warn(&port->dd->pdev->dev, 1614 "Query for invalid SMART attribute ID\n"); 1615 rv = -EINVAL; 1616 } 1617 1618 return rv; 1619 } 1620 1621 /* 1622 * Trim unused sectors 1623 * 1624 * @dd pointer to driver_data structure 1625 * @lba starting lba 1626 * @len # of 512b sectors to trim 1627 * 1628 * return value 1629 * -ENOMEM Out of dma memory 1630 * -EINVAL Invalid parameters passed in, trim not supported 1631 * -EIO Error submitting trim request to hw 1632 */ 1633 static int mtip_send_trim(struct driver_data *dd, unsigned int lba, 1634 unsigned int len) 1635 { 1636 int i, rv = 0; 1637 u64 tlba, tlen, sect_left; 1638 struct mtip_trim_entry *buf; 1639 dma_addr_t dma_addr; 1640 struct host_to_dev_fis fis; 1641 1642 if (!len || dd->trim_supp == false) 1643 return -EINVAL; 1644 1645 /* Trim request too big */ 1646 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES)); 1647 1648 /* Trim request not aligned on 4k boundary */ 1649 WARN_ON(len % 8 != 0); 1650 1651 /* Warn if vu_trim structure is too big */ 1652 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE); 1653 1654 /* Allocate a DMA buffer for the trim structure */ 1655 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr, 1656 GFP_KERNEL); 1657 if (!buf) 1658 return -ENOMEM; 1659 memset(buf, 0, ATA_SECT_SIZE); 1660 1661 for (i = 0, sect_left = len, tlba = lba; 1662 i < MTIP_MAX_TRIM_ENTRIES && sect_left; 1663 i++) { 1664 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ? 1665 MTIP_MAX_TRIM_ENTRY_LEN : 1666 sect_left); 1667 buf[i].lba = __force_bit2int cpu_to_le32(tlba); 1668 buf[i].range = __force_bit2int cpu_to_le16(tlen); 1669 tlba += tlen; 1670 sect_left -= tlen; 1671 } 1672 WARN_ON(sect_left != 0); 1673 1674 /* Build the fis */ 1675 memset(&fis, 0, sizeof(struct host_to_dev_fis)); 1676 fis.type = 0x27; 1677 fis.opts = 1 << 7; 1678 fis.command = 0xfb; 1679 fis.features = 0x60; 1680 fis.sect_count = 1; 1681 fis.device = ATA_DEVICE_OBS; 1682 1683 if (mtip_exec_internal_command(dd->port, 1684 &fis, 1685 5, 1686 dma_addr, 1687 ATA_SECT_SIZE, 1688 0, 1689 GFP_KERNEL, 1690 MTIP_TRIM_TIMEOUT_MS) < 0) 1691 rv = -EIO; 1692 1693 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr); 1694 return rv; 1695 } 1696 1697 /* 1698 * Get the drive capacity. 1699 * 1700 * @dd Pointer to the device data structure. 1701 * @sectors Pointer to the variable that will receive the sector count. 1702 * 1703 * return value 1704 * 1 Capacity was returned successfully. 1705 * 0 The identify information is invalid. 1706 */ 1707 static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors) 1708 { 1709 struct mtip_port *port = dd->port; 1710 u64 total, raw0, raw1, raw2, raw3; 1711 raw0 = port->identify[100]; 1712 raw1 = port->identify[101]; 1713 raw2 = port->identify[102]; 1714 raw3 = port->identify[103]; 1715 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48; 1716 *sectors = total; 1717 return (bool) !!port->identify_valid; 1718 } 1719 1720 /* 1721 * Display the identify command data. 1722 * 1723 * @port Pointer to the port data structure. 1724 * 1725 * return value 1726 * None 1727 */ 1728 static void mtip_dump_identify(struct mtip_port *port) 1729 { 1730 sector_t sectors; 1731 unsigned short revid; 1732 char cbuf[42]; 1733 1734 if (!port->identify_valid) 1735 return; 1736 1737 strlcpy(cbuf, (char *)(port->identify+10), 21); 1738 dev_info(&port->dd->pdev->dev, 1739 "Serial No.: %s\n", cbuf); 1740 1741 strlcpy(cbuf, (char *)(port->identify+23), 9); 1742 dev_info(&port->dd->pdev->dev, 1743 "Firmware Ver.: %s\n", cbuf); 1744 1745 strlcpy(cbuf, (char *)(port->identify+27), 41); 1746 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf); 1747 1748 dev_info(&port->dd->pdev->dev, "Security: %04x %s\n", 1749 port->identify[128], 1750 port->identify[128] & 0x4 ? "(LOCKED)" : ""); 1751 1752 if (mtip_hw_get_capacity(port->dd, §ors)) 1753 dev_info(&port->dd->pdev->dev, 1754 "Capacity: %llu sectors (%llu MB)\n", 1755 (u64)sectors, 1756 ((u64)sectors) * ATA_SECT_SIZE >> 20); 1757 1758 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid); 1759 switch (revid & 0xFF) { 1760 case 0x1: 1761 strlcpy(cbuf, "A0", 3); 1762 break; 1763 case 0x3: 1764 strlcpy(cbuf, "A2", 3); 1765 break; 1766 default: 1767 strlcpy(cbuf, "?", 2); 1768 break; 1769 } 1770 dev_info(&port->dd->pdev->dev, 1771 "Card Type: %s\n", cbuf); 1772 } 1773 1774 /* 1775 * Map the commands scatter list into the command table. 1776 * 1777 * @command Pointer to the command. 1778 * @nents Number of scatter list entries. 1779 * 1780 * return value 1781 * None 1782 */ 1783 static inline void fill_command_sg(struct driver_data *dd, 1784 struct mtip_cmd *command, 1785 int nents) 1786 { 1787 int n; 1788 unsigned int dma_len; 1789 struct mtip_cmd_sg *command_sg; 1790 struct scatterlist *sg = command->sg; 1791 1792 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ; 1793 1794 for (n = 0; n < nents; n++) { 1795 dma_len = sg_dma_len(sg); 1796 if (dma_len > 0x400000) 1797 dev_err(&dd->pdev->dev, 1798 "DMA segment length truncated\n"); 1799 command_sg->info = __force_bit2int 1800 cpu_to_le32((dma_len-1) & 0x3FFFFF); 1801 command_sg->dba = __force_bit2int 1802 cpu_to_le32(sg_dma_address(sg)); 1803 command_sg->dba_upper = __force_bit2int 1804 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16); 1805 command_sg++; 1806 sg++; 1807 } 1808 } 1809 1810 /* 1811 * @brief Execute a drive command. 1812 * 1813 * return value 0 The command completed successfully. 1814 * return value -1 An error occurred while executing the command. 1815 */ 1816 static int exec_drive_task(struct mtip_port *port, u8 *command) 1817 { 1818 struct host_to_dev_fis fis; 1819 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG); 1820 unsigned int to; 1821 1822 /* Build the FIS. */ 1823 memset(&fis, 0, sizeof(struct host_to_dev_fis)); 1824 fis.type = 0x27; 1825 fis.opts = 1 << 7; 1826 fis.command = command[0]; 1827 fis.features = command[1]; 1828 fis.sect_count = command[2]; 1829 fis.sector = command[3]; 1830 fis.cyl_low = command[4]; 1831 fis.cyl_hi = command[5]; 1832 fis.device = command[6] & ~0x10; /* Clear the dev bit*/ 1833 1834 mtip_set_timeout(port->dd, &fis, &to, 0); 1835 1836 dbg_printk(MTIP_DRV_NAME " %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n", 1837 __func__, 1838 command[0], 1839 command[1], 1840 command[2], 1841 command[3], 1842 command[4], 1843 command[5], 1844 command[6]); 1845 1846 /* Execute the command. */ 1847 if (mtip_exec_internal_command(port, 1848 &fis, 1849 5, 1850 0, 1851 0, 1852 0, 1853 GFP_KERNEL, 1854 to) < 0) { 1855 return -1; 1856 } 1857 1858 command[0] = reply->command; /* Status*/ 1859 command[1] = reply->features; /* Error*/ 1860 command[4] = reply->cyl_low; 1861 command[5] = reply->cyl_hi; 1862 1863 dbg_printk(MTIP_DRV_NAME " %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n", 1864 __func__, 1865 command[0], 1866 command[1], 1867 command[4], 1868 command[5]); 1869 1870 return 0; 1871 } 1872 1873 /* 1874 * @brief Execute a drive command. 1875 * 1876 * @param port Pointer to the port data structure. 1877 * @param command Pointer to the user specified command parameters. 1878 * @param user_buffer Pointer to the user space buffer where read sector 1879 * data should be copied. 1880 * 1881 * return value 0 The command completed successfully. 1882 * return value -EFAULT An error occurred while copying the completion 1883 * data to the user space buffer. 1884 * return value -1 An error occurred while executing the command. 1885 */ 1886 static int exec_drive_command(struct mtip_port *port, u8 *command, 1887 void __user *user_buffer) 1888 { 1889 struct host_to_dev_fis fis; 1890 struct host_to_dev_fis *reply; 1891 u8 *buf = NULL; 1892 dma_addr_t dma_addr = 0; 1893 int rv = 0, xfer_sz = command[3]; 1894 unsigned int to; 1895 1896 if (xfer_sz) { 1897 if (!user_buffer) 1898 return -EFAULT; 1899 1900 buf = dmam_alloc_coherent(&port->dd->pdev->dev, 1901 ATA_SECT_SIZE * xfer_sz, 1902 &dma_addr, 1903 GFP_KERNEL); 1904 if (!buf) { 1905 dev_err(&port->dd->pdev->dev, 1906 "Memory allocation failed (%d bytes)\n", 1907 ATA_SECT_SIZE * xfer_sz); 1908 return -ENOMEM; 1909 } 1910 memset(buf, 0, ATA_SECT_SIZE * xfer_sz); 1911 } 1912 1913 /* Build the FIS. */ 1914 memset(&fis, 0, sizeof(struct host_to_dev_fis)); 1915 fis.type = 0x27; 1916 fis.opts = 1 << 7; 1917 fis.command = command[0]; 1918 fis.features = command[2]; 1919 fis.sect_count = command[3]; 1920 if (fis.command == ATA_CMD_SMART) { 1921 fis.sector = command[1]; 1922 fis.cyl_low = 0x4F; 1923 fis.cyl_hi = 0xC2; 1924 } 1925 1926 mtip_set_timeout(port->dd, &fis, &to, 0); 1927 1928 if (xfer_sz) 1929 reply = (port->rxfis + RX_FIS_PIO_SETUP); 1930 else 1931 reply = (port->rxfis + RX_FIS_D2H_REG); 1932 1933 dbg_printk(MTIP_DRV_NAME 1934 " %s: User Command: cmd %x, sect %x, " 1935 "feat %x, sectcnt %x\n", 1936 __func__, 1937 command[0], 1938 command[1], 1939 command[2], 1940 command[3]); 1941 1942 /* Execute the command. */ 1943 if (mtip_exec_internal_command(port, 1944 &fis, 1945 5, 1946 (xfer_sz ? dma_addr : 0), 1947 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0), 1948 0, 1949 GFP_KERNEL, 1950 to) 1951 < 0) { 1952 rv = -EFAULT; 1953 goto exit_drive_command; 1954 } 1955 1956 /* Collect the completion status. */ 1957 command[0] = reply->command; /* Status*/ 1958 command[1] = reply->features; /* Error*/ 1959 command[2] = reply->sect_count; 1960 1961 dbg_printk(MTIP_DRV_NAME 1962 " %s: Completion Status: stat %x, " 1963 "err %x, nsect %x\n", 1964 __func__, 1965 command[0], 1966 command[1], 1967 command[2]); 1968 1969 if (xfer_sz) { 1970 if (copy_to_user(user_buffer, 1971 buf, 1972 ATA_SECT_SIZE * command[3])) { 1973 rv = -EFAULT; 1974 goto exit_drive_command; 1975 } 1976 } 1977 exit_drive_command: 1978 if (buf) 1979 dmam_free_coherent(&port->dd->pdev->dev, 1980 ATA_SECT_SIZE * xfer_sz, buf, dma_addr); 1981 return rv; 1982 } 1983 1984 /* 1985 * Indicates whether a command has a single sector payload. 1986 * 1987 * @command passed to the device to perform the certain event. 1988 * @features passed to the device to perform the certain event. 1989 * 1990 * return value 1991 * 1 command is one that always has a single sector payload, 1992 * regardless of the value in the Sector Count field. 1993 * 0 otherwise 1994 * 1995 */ 1996 static unsigned int implicit_sector(unsigned char command, 1997 unsigned char features) 1998 { 1999 unsigned int rv = 0; 2000 2001 /* list of commands that have an implicit sector count of 1 */ 2002 switch (command) { 2003 case ATA_CMD_SEC_SET_PASS: 2004 case ATA_CMD_SEC_UNLOCK: 2005 case ATA_CMD_SEC_ERASE_PREP: 2006 case ATA_CMD_SEC_ERASE_UNIT: 2007 case ATA_CMD_SEC_FREEZE_LOCK: 2008 case ATA_CMD_SEC_DISABLE_PASS: 2009 case ATA_CMD_PMP_READ: 2010 case ATA_CMD_PMP_WRITE: 2011 rv = 1; 2012 break; 2013 case ATA_CMD_SET_MAX: 2014 if (features == ATA_SET_MAX_UNLOCK) 2015 rv = 1; 2016 break; 2017 case ATA_CMD_SMART: 2018 if ((features == ATA_SMART_READ_VALUES) || 2019 (features == ATA_SMART_READ_THRESHOLDS)) 2020 rv = 1; 2021 break; 2022 case ATA_CMD_CONF_OVERLAY: 2023 if ((features == ATA_DCO_IDENTIFY) || 2024 (features == ATA_DCO_SET)) 2025 rv = 1; 2026 break; 2027 } 2028 return rv; 2029 } 2030 2031 /* 2032 * Executes a taskfile 2033 * See ide_taskfile_ioctl() for derivation 2034 */ 2035 static int exec_drive_taskfile(struct driver_data *dd, 2036 void __user *buf, 2037 ide_task_request_t *req_task, 2038 int outtotal) 2039 { 2040 struct host_to_dev_fis fis; 2041 struct host_to_dev_fis *reply; 2042 u8 *outbuf = NULL; 2043 u8 *inbuf = NULL; 2044 dma_addr_t outbuf_dma = 0; 2045 dma_addr_t inbuf_dma = 0; 2046 dma_addr_t dma_buffer = 0; 2047 int err = 0; 2048 unsigned int taskin = 0; 2049 unsigned int taskout = 0; 2050 u8 nsect = 0; 2051 unsigned int timeout; 2052 unsigned int force_single_sector; 2053 unsigned int transfer_size; 2054 unsigned long task_file_data; 2055 int intotal = outtotal + req_task->out_size; 2056 int erasemode = 0; 2057 2058 taskout = req_task->out_size; 2059 taskin = req_task->in_size; 2060 /* 130560 = 512 * 0xFF*/ 2061 if (taskin > 130560 || taskout > 130560) 2062 return -EINVAL; 2063 2064 if (taskout) { 2065 outbuf = memdup_user(buf + outtotal, taskout); 2066 if (IS_ERR(outbuf)) 2067 return PTR_ERR(outbuf); 2068 2069 outbuf_dma = pci_map_single(dd->pdev, 2070 outbuf, 2071 taskout, 2072 DMA_TO_DEVICE); 2073 if (pci_dma_mapping_error(dd->pdev, outbuf_dma)) { 2074 err = -ENOMEM; 2075 goto abort; 2076 } 2077 dma_buffer = outbuf_dma; 2078 } 2079 2080 if (taskin) { 2081 inbuf = memdup_user(buf + intotal, taskin); 2082 if (IS_ERR(inbuf)) { 2083 err = PTR_ERR(inbuf); 2084 inbuf = NULL; 2085 goto abort; 2086 } 2087 inbuf_dma = pci_map_single(dd->pdev, 2088 inbuf, 2089 taskin, DMA_FROM_DEVICE); 2090 if (pci_dma_mapping_error(dd->pdev, inbuf_dma)) { 2091 err = -ENOMEM; 2092 goto abort; 2093 } 2094 dma_buffer = inbuf_dma; 2095 } 2096 2097 /* only supports PIO and non-data commands from this ioctl. */ 2098 switch (req_task->data_phase) { 2099 case TASKFILE_OUT: 2100 nsect = taskout / ATA_SECT_SIZE; 2101 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP); 2102 break; 2103 case TASKFILE_IN: 2104 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP); 2105 break; 2106 case TASKFILE_NO_DATA: 2107 reply = (dd->port->rxfis + RX_FIS_D2H_REG); 2108 break; 2109 default: 2110 err = -EINVAL; 2111 goto abort; 2112 } 2113 2114 /* Build the FIS. */ 2115 memset(&fis, 0, sizeof(struct host_to_dev_fis)); 2116 2117 fis.type = 0x27; 2118 fis.opts = 1 << 7; 2119 fis.command = req_task->io_ports[7]; 2120 fis.features = req_task->io_ports[1]; 2121 fis.sect_count = req_task->io_ports[2]; 2122 fis.lba_low = req_task->io_ports[3]; 2123 fis.lba_mid = req_task->io_ports[4]; 2124 fis.lba_hi = req_task->io_ports[5]; 2125 /* Clear the dev bit*/ 2126 fis.device = req_task->io_ports[6] & ~0x10; 2127 2128 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) { 2129 req_task->in_flags.all = 2130 IDE_TASKFILE_STD_IN_FLAGS | 2131 (IDE_HOB_STD_IN_FLAGS << 8); 2132 fis.lba_low_ex = req_task->hob_ports[3]; 2133 fis.lba_mid_ex = req_task->hob_ports[4]; 2134 fis.lba_hi_ex = req_task->hob_ports[5]; 2135 fis.features_ex = req_task->hob_ports[1]; 2136 fis.sect_cnt_ex = req_task->hob_ports[2]; 2137 2138 } else { 2139 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS; 2140 } 2141 2142 force_single_sector = implicit_sector(fis.command, fis.features); 2143 2144 if ((taskin || taskout) && (!fis.sect_count)) { 2145 if (nsect) 2146 fis.sect_count = nsect; 2147 else { 2148 if (!force_single_sector) { 2149 dev_warn(&dd->pdev->dev, 2150 "data movement but " 2151 "sect_count is 0\n"); 2152 err = -EINVAL; 2153 goto abort; 2154 } 2155 } 2156 } 2157 2158 dbg_printk(MTIP_DRV_NAME 2159 " %s: cmd %x, feat %x, nsect %x," 2160 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x," 2161 " head/dev %x\n", 2162 __func__, 2163 fis.command, 2164 fis.features, 2165 fis.sect_count, 2166 fis.lba_low, 2167 fis.lba_mid, 2168 fis.lba_hi, 2169 fis.device); 2170 2171 /* check for erase mode support during secure erase.*/ 2172 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf && 2173 (outbuf[0] & MTIP_SEC_ERASE_MODE)) { 2174 erasemode = 1; 2175 } 2176 2177 mtip_set_timeout(dd, &fis, &timeout, erasemode); 2178 2179 /* Determine the correct transfer size.*/ 2180 if (force_single_sector) 2181 transfer_size = ATA_SECT_SIZE; 2182 else 2183 transfer_size = ATA_SECT_SIZE * fis.sect_count; 2184 2185 /* Execute the command.*/ 2186 if (mtip_exec_internal_command(dd->port, 2187 &fis, 2188 5, 2189 dma_buffer, 2190 transfer_size, 2191 0, 2192 GFP_KERNEL, 2193 timeout) < 0) { 2194 err = -EIO; 2195 goto abort; 2196 } 2197 2198 task_file_data = readl(dd->port->mmio+PORT_TFDATA); 2199 2200 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) { 2201 reply = dd->port->rxfis + RX_FIS_PIO_SETUP; 2202 req_task->io_ports[7] = reply->control; 2203 } else { 2204 reply = dd->port->rxfis + RX_FIS_D2H_REG; 2205 req_task->io_ports[7] = reply->command; 2206 } 2207 2208 /* reclaim the DMA buffers.*/ 2209 if (inbuf_dma) 2210 pci_unmap_single(dd->pdev, inbuf_dma, 2211 taskin, DMA_FROM_DEVICE); 2212 if (outbuf_dma) 2213 pci_unmap_single(dd->pdev, outbuf_dma, 2214 taskout, DMA_TO_DEVICE); 2215 inbuf_dma = 0; 2216 outbuf_dma = 0; 2217 2218 /* return the ATA registers to the caller.*/ 2219 req_task->io_ports[1] = reply->features; 2220 req_task->io_ports[2] = reply->sect_count; 2221 req_task->io_ports[3] = reply->lba_low; 2222 req_task->io_ports[4] = reply->lba_mid; 2223 req_task->io_ports[5] = reply->lba_hi; 2224 req_task->io_ports[6] = reply->device; 2225 2226 if (req_task->out_flags.all & 1) { 2227 2228 req_task->hob_ports[3] = reply->lba_low_ex; 2229 req_task->hob_ports[4] = reply->lba_mid_ex; 2230 req_task->hob_ports[5] = reply->lba_hi_ex; 2231 req_task->hob_ports[1] = reply->features_ex; 2232 req_task->hob_ports[2] = reply->sect_cnt_ex; 2233 } 2234 dbg_printk(MTIP_DRV_NAME 2235 " %s: Completion: stat %x," 2236 "err %x, sect_cnt %x, lbalo %x," 2237 "lbamid %x, lbahi %x, dev %x\n", 2238 __func__, 2239 req_task->io_ports[7], 2240 req_task->io_ports[1], 2241 req_task->io_ports[2], 2242 req_task->io_ports[3], 2243 req_task->io_ports[4], 2244 req_task->io_ports[5], 2245 req_task->io_ports[6]); 2246 2247 if (taskout) { 2248 if (copy_to_user(buf + outtotal, outbuf, taskout)) { 2249 err = -EFAULT; 2250 goto abort; 2251 } 2252 } 2253 if (taskin) { 2254 if (copy_to_user(buf + intotal, inbuf, taskin)) { 2255 err = -EFAULT; 2256 goto abort; 2257 } 2258 } 2259 abort: 2260 if (inbuf_dma) 2261 pci_unmap_single(dd->pdev, inbuf_dma, 2262 taskin, DMA_FROM_DEVICE); 2263 if (outbuf_dma) 2264 pci_unmap_single(dd->pdev, outbuf_dma, 2265 taskout, DMA_TO_DEVICE); 2266 kfree(outbuf); 2267 kfree(inbuf); 2268 2269 return err; 2270 } 2271 2272 /* 2273 * Handle IOCTL calls from the Block Layer. 2274 * 2275 * This function is called by the Block Layer when it receives an IOCTL 2276 * command that it does not understand. If the IOCTL command is not supported 2277 * this function returns -ENOTTY. 2278 * 2279 * @dd Pointer to the driver data structure. 2280 * @cmd IOCTL command passed from the Block Layer. 2281 * @arg IOCTL argument passed from the Block Layer. 2282 * 2283 * return value 2284 * 0 The IOCTL completed successfully. 2285 * -ENOTTY The specified command is not supported. 2286 * -EFAULT An error occurred copying data to a user space buffer. 2287 * -EIO An error occurred while executing the command. 2288 */ 2289 static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd, 2290 unsigned long arg) 2291 { 2292 switch (cmd) { 2293 case HDIO_GET_IDENTITY: 2294 { 2295 if (copy_to_user((void __user *)arg, dd->port->identify, 2296 sizeof(u16) * ATA_ID_WORDS)) 2297 return -EFAULT; 2298 break; 2299 } 2300 case HDIO_DRIVE_CMD: 2301 { 2302 u8 drive_command[4]; 2303 2304 /* Copy the user command info to our buffer. */ 2305 if (copy_from_user(drive_command, 2306 (void __user *) arg, 2307 sizeof(drive_command))) 2308 return -EFAULT; 2309 2310 /* Execute the drive command. */ 2311 if (exec_drive_command(dd->port, 2312 drive_command, 2313 (void __user *) (arg+4))) 2314 return -EIO; 2315 2316 /* Copy the status back to the users buffer. */ 2317 if (copy_to_user((void __user *) arg, 2318 drive_command, 2319 sizeof(drive_command))) 2320 return -EFAULT; 2321 2322 break; 2323 } 2324 case HDIO_DRIVE_TASK: 2325 { 2326 u8 drive_command[7]; 2327 2328 /* Copy the user command info to our buffer. */ 2329 if (copy_from_user(drive_command, 2330 (void __user *) arg, 2331 sizeof(drive_command))) 2332 return -EFAULT; 2333 2334 /* Execute the drive command. */ 2335 if (exec_drive_task(dd->port, drive_command)) 2336 return -EIO; 2337 2338 /* Copy the status back to the users buffer. */ 2339 if (copy_to_user((void __user *) arg, 2340 drive_command, 2341 sizeof(drive_command))) 2342 return -EFAULT; 2343 2344 break; 2345 } 2346 case HDIO_DRIVE_TASKFILE: { 2347 ide_task_request_t req_task; 2348 int ret, outtotal; 2349 2350 if (copy_from_user(&req_task, (void __user *) arg, 2351 sizeof(req_task))) 2352 return -EFAULT; 2353 2354 outtotal = sizeof(req_task); 2355 2356 ret = exec_drive_taskfile(dd, (void __user *) arg, 2357 &req_task, outtotal); 2358 2359 if (copy_to_user((void __user *) arg, &req_task, 2360 sizeof(req_task))) 2361 return -EFAULT; 2362 2363 return ret; 2364 } 2365 2366 default: 2367 return -EINVAL; 2368 } 2369 return 0; 2370 } 2371 2372 /* 2373 * Submit an IO to the hw 2374 * 2375 * This function is called by the block layer to issue an io 2376 * to the device. Upon completion, the callback function will 2377 * be called with the data parameter passed as the callback data. 2378 * 2379 * @dd Pointer to the driver data structure. 2380 * @start First sector to read. 2381 * @nsect Number of sectors to read. 2382 * @nents Number of entries in scatter list for the read command. 2383 * @tag The tag of this read command. 2384 * @callback Pointer to the function that should be called 2385 * when the read completes. 2386 * @data Callback data passed to the callback function 2387 * when the read completes. 2388 * @dir Direction (read or write) 2389 * 2390 * return value 2391 * None 2392 */ 2393 static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq, 2394 struct mtip_cmd *command, int nents, 2395 struct blk_mq_hw_ctx *hctx) 2396 { 2397 struct host_to_dev_fis *fis; 2398 struct mtip_port *port = dd->port; 2399 int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE; 2400 u64 start = blk_rq_pos(rq); 2401 unsigned int nsect = blk_rq_sectors(rq); 2402 2403 /* Map the scatter list for DMA access */ 2404 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir); 2405 2406 prefetch(&port->flags); 2407 2408 command->scatter_ents = nents; 2409 2410 /* 2411 * The number of retries for this command before it is 2412 * reported as a failure to the upper layers. 2413 */ 2414 command->retries = MTIP_MAX_RETRIES; 2415 2416 /* Fill out fis */ 2417 fis = command->command; 2418 fis->type = 0x27; 2419 fis->opts = 1 << 7; 2420 if (dma_dir == DMA_FROM_DEVICE) 2421 fis->command = ATA_CMD_FPDMA_READ; 2422 else 2423 fis->command = ATA_CMD_FPDMA_WRITE; 2424 fis->lba_low = start & 0xFF; 2425 fis->lba_mid = (start >> 8) & 0xFF; 2426 fis->lba_hi = (start >> 16) & 0xFF; 2427 fis->lba_low_ex = (start >> 24) & 0xFF; 2428 fis->lba_mid_ex = (start >> 32) & 0xFF; 2429 fis->lba_hi_ex = (start >> 40) & 0xFF; 2430 fis->device = 1 << 6; 2431 fis->features = nsect & 0xFF; 2432 fis->features_ex = (nsect >> 8) & 0xFF; 2433 fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5)); 2434 fis->sect_cnt_ex = 0; 2435 fis->control = 0; 2436 fis->res2 = 0; 2437 fis->res3 = 0; 2438 fill_command_sg(dd, command, nents); 2439 2440 if (unlikely(command->unaligned)) 2441 fis->device |= 1 << 7; 2442 2443 /* Populate the command header */ 2444 command->command_header->opts = 2445 __force_bit2int cpu_to_le32( 2446 (nents << 16) | 5 | AHCI_CMD_PREFETCH); 2447 command->command_header->byte_count = 0; 2448 2449 /* 2450 * Set the completion function and data for the command 2451 * within this layer. 2452 */ 2453 command->comp_data = dd; 2454 command->comp_func = mtip_async_complete; 2455 command->direction = dma_dir; 2456 2457 /* 2458 * To prevent this command from being issued 2459 * if an internal command is in progress or error handling is active. 2460 */ 2461 if (unlikely(port->flags & MTIP_PF_PAUSE_IO)) { 2462 set_bit(rq->tag, port->cmds_to_issue); 2463 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags); 2464 return; 2465 } 2466 2467 /* Issue the command to the hardware */ 2468 mtip_issue_ncq_command(port, rq->tag); 2469 } 2470 2471 /* 2472 * Sysfs status dump. 2473 * 2474 * @dev Pointer to the device structure, passed by the kernrel. 2475 * @attr Pointer to the device_attribute structure passed by the kernel. 2476 * @buf Pointer to the char buffer that will receive the stats info. 2477 * 2478 * return value 2479 * The size, in bytes, of the data copied into buf. 2480 */ 2481 static ssize_t mtip_hw_show_status(struct device *dev, 2482 struct device_attribute *attr, 2483 char *buf) 2484 { 2485 struct driver_data *dd = dev_to_disk(dev)->private_data; 2486 int size = 0; 2487 2488 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag)) 2489 size += sprintf(buf, "%s", "thermal_shutdown\n"); 2490 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag)) 2491 size += sprintf(buf, "%s", "write_protect\n"); 2492 else 2493 size += sprintf(buf, "%s", "online\n"); 2494 2495 return size; 2496 } 2497 2498 static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL); 2499 2500 /* debugsfs entries */ 2501 2502 static ssize_t show_device_status(struct device_driver *drv, char *buf) 2503 { 2504 int size = 0; 2505 struct driver_data *dd, *tmp; 2506 unsigned long flags; 2507 char id_buf[42]; 2508 u16 status = 0; 2509 2510 spin_lock_irqsave(&dev_lock, flags); 2511 size += sprintf(&buf[size], "Devices Present:\n"); 2512 list_for_each_entry_safe(dd, tmp, &online_list, online_list) { 2513 if (dd->pdev) { 2514 if (dd->port && 2515 dd->port->identify && 2516 dd->port->identify_valid) { 2517 strlcpy(id_buf, 2518 (char *) (dd->port->identify + 10), 21); 2519 status = *(dd->port->identify + 141); 2520 } else { 2521 memset(id_buf, 0, 42); 2522 status = 0; 2523 } 2524 2525 if (dd->port && 2526 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) { 2527 size += sprintf(&buf[size], 2528 " device %s %s (ftl rebuild %d %%)\n", 2529 dev_name(&dd->pdev->dev), 2530 id_buf, 2531 status); 2532 } else { 2533 size += sprintf(&buf[size], 2534 " device %s %s\n", 2535 dev_name(&dd->pdev->dev), 2536 id_buf); 2537 } 2538 } 2539 } 2540 2541 size += sprintf(&buf[size], "Devices Being Removed:\n"); 2542 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) { 2543 if (dd->pdev) { 2544 if (dd->port && 2545 dd->port->identify && 2546 dd->port->identify_valid) { 2547 strlcpy(id_buf, 2548 (char *) (dd->port->identify+10), 21); 2549 status = *(dd->port->identify + 141); 2550 } else { 2551 memset(id_buf, 0, 42); 2552 status = 0; 2553 } 2554 2555 if (dd->port && 2556 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) { 2557 size += sprintf(&buf[size], 2558 " device %s %s (ftl rebuild %d %%)\n", 2559 dev_name(&dd->pdev->dev), 2560 id_buf, 2561 status); 2562 } else { 2563 size += sprintf(&buf[size], 2564 " device %s %s\n", 2565 dev_name(&dd->pdev->dev), 2566 id_buf); 2567 } 2568 } 2569 } 2570 spin_unlock_irqrestore(&dev_lock, flags); 2571 2572 return size; 2573 } 2574 2575 static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf, 2576 size_t len, loff_t *offset) 2577 { 2578 struct driver_data *dd = (struct driver_data *)f->private_data; 2579 int size = *offset; 2580 char *buf; 2581 int rv = 0; 2582 2583 if (!len || *offset) 2584 return 0; 2585 2586 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL); 2587 if (!buf) { 2588 dev_err(&dd->pdev->dev, 2589 "Memory allocation: status buffer\n"); 2590 return -ENOMEM; 2591 } 2592 2593 size += show_device_status(NULL, buf); 2594 2595 *offset = size <= len ? size : len; 2596 size = copy_to_user(ubuf, buf, *offset); 2597 if (size) 2598 rv = -EFAULT; 2599 2600 kfree(buf); 2601 return rv ? rv : *offset; 2602 } 2603 2604 static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf, 2605 size_t len, loff_t *offset) 2606 { 2607 struct driver_data *dd = (struct driver_data *)f->private_data; 2608 char *buf; 2609 u32 group_allocated; 2610 int size = *offset; 2611 int n, rv = 0; 2612 2613 if (!len || size) 2614 return 0; 2615 2616 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL); 2617 if (!buf) { 2618 dev_err(&dd->pdev->dev, 2619 "Memory allocation: register buffer\n"); 2620 return -ENOMEM; 2621 } 2622 2623 size += sprintf(&buf[size], "H/ S ACTive : [ 0x"); 2624 2625 for (n = dd->slot_groups-1; n >= 0; n--) 2626 size += sprintf(&buf[size], "%08X ", 2627 readl(dd->port->s_active[n])); 2628 2629 size += sprintf(&buf[size], "]\n"); 2630 size += sprintf(&buf[size], "H/ Command Issue : [ 0x"); 2631 2632 for (n = dd->slot_groups-1; n >= 0; n--) 2633 size += sprintf(&buf[size], "%08X ", 2634 readl(dd->port->cmd_issue[n])); 2635 2636 size += sprintf(&buf[size], "]\n"); 2637 size += sprintf(&buf[size], "H/ Completed : [ 0x"); 2638 2639 for (n = dd->slot_groups-1; n >= 0; n--) 2640 size += sprintf(&buf[size], "%08X ", 2641 readl(dd->port->completed[n])); 2642 2643 size += sprintf(&buf[size], "]\n"); 2644 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n", 2645 readl(dd->port->mmio + PORT_IRQ_STAT)); 2646 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n", 2647 readl(dd->mmio + HOST_IRQ_STAT)); 2648 size += sprintf(&buf[size], "\n"); 2649 2650 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x"); 2651 2652 for (n = dd->slot_groups-1; n >= 0; n--) { 2653 if (sizeof(long) > sizeof(u32)) 2654 group_allocated = 2655 dd->port->cmds_to_issue[n/2] >> (32*(n&1)); 2656 else 2657 group_allocated = dd->port->cmds_to_issue[n]; 2658 size += sprintf(&buf[size], "%08X ", group_allocated); 2659 } 2660 size += sprintf(&buf[size], "]\n"); 2661 2662 *offset = size <= len ? size : len; 2663 size = copy_to_user(ubuf, buf, *offset); 2664 if (size) 2665 rv = -EFAULT; 2666 2667 kfree(buf); 2668 return rv ? rv : *offset; 2669 } 2670 2671 static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf, 2672 size_t len, loff_t *offset) 2673 { 2674 struct driver_data *dd = (struct driver_data *)f->private_data; 2675 char *buf; 2676 int size = *offset; 2677 int rv = 0; 2678 2679 if (!len || size) 2680 return 0; 2681 2682 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL); 2683 if (!buf) { 2684 dev_err(&dd->pdev->dev, 2685 "Memory allocation: flag buffer\n"); 2686 return -ENOMEM; 2687 } 2688 2689 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n", 2690 dd->port->flags); 2691 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n", 2692 dd->dd_flag); 2693 2694 *offset = size <= len ? size : len; 2695 size = copy_to_user(ubuf, buf, *offset); 2696 if (size) 2697 rv = -EFAULT; 2698 2699 kfree(buf); 2700 return rv ? rv : *offset; 2701 } 2702 2703 static const struct file_operations mtip_device_status_fops = { 2704 .owner = THIS_MODULE, 2705 .open = simple_open, 2706 .read = mtip_hw_read_device_status, 2707 .llseek = no_llseek, 2708 }; 2709 2710 static const struct file_operations mtip_regs_fops = { 2711 .owner = THIS_MODULE, 2712 .open = simple_open, 2713 .read = mtip_hw_read_registers, 2714 .llseek = no_llseek, 2715 }; 2716 2717 static const struct file_operations mtip_flags_fops = { 2718 .owner = THIS_MODULE, 2719 .open = simple_open, 2720 .read = mtip_hw_read_flags, 2721 .llseek = no_llseek, 2722 }; 2723 2724 /* 2725 * Create the sysfs related attributes. 2726 * 2727 * @dd Pointer to the driver data structure. 2728 * @kobj Pointer to the kobj for the block device. 2729 * 2730 * return value 2731 * 0 Operation completed successfully. 2732 * -EINVAL Invalid parameter. 2733 */ 2734 static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj) 2735 { 2736 if (!kobj || !dd) 2737 return -EINVAL; 2738 2739 if (sysfs_create_file(kobj, &dev_attr_status.attr)) 2740 dev_warn(&dd->pdev->dev, 2741 "Error creating 'status' sysfs entry\n"); 2742 return 0; 2743 } 2744 2745 /* 2746 * Remove the sysfs related attributes. 2747 * 2748 * @dd Pointer to the driver data structure. 2749 * @kobj Pointer to the kobj for the block device. 2750 * 2751 * return value 2752 * 0 Operation completed successfully. 2753 * -EINVAL Invalid parameter. 2754 */ 2755 static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj) 2756 { 2757 if (!kobj || !dd) 2758 return -EINVAL; 2759 2760 sysfs_remove_file(kobj, &dev_attr_status.attr); 2761 2762 return 0; 2763 } 2764 2765 static int mtip_hw_debugfs_init(struct driver_data *dd) 2766 { 2767 if (!dfs_parent) 2768 return -1; 2769 2770 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent); 2771 if (IS_ERR_OR_NULL(dd->dfs_node)) { 2772 dev_warn(&dd->pdev->dev, 2773 "Error creating node %s under debugfs\n", 2774 dd->disk->disk_name); 2775 dd->dfs_node = NULL; 2776 return -1; 2777 } 2778 2779 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd, 2780 &mtip_flags_fops); 2781 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd, 2782 &mtip_regs_fops); 2783 2784 return 0; 2785 } 2786 2787 static void mtip_hw_debugfs_exit(struct driver_data *dd) 2788 { 2789 if (dd->dfs_node) 2790 debugfs_remove_recursive(dd->dfs_node); 2791 } 2792 2793 /* 2794 * Perform any init/resume time hardware setup 2795 * 2796 * @dd Pointer to the driver data structure. 2797 * 2798 * return value 2799 * None 2800 */ 2801 static inline void hba_setup(struct driver_data *dd) 2802 { 2803 u32 hwdata; 2804 hwdata = readl(dd->mmio + HOST_HSORG); 2805 2806 /* interrupt bug workaround: use only 1 IS bit.*/ 2807 writel(hwdata | 2808 HSORG_DISABLE_SLOTGRP_INTR | 2809 HSORG_DISABLE_SLOTGRP_PXIS, 2810 dd->mmio + HOST_HSORG); 2811 } 2812 2813 static int mtip_device_unaligned_constrained(struct driver_data *dd) 2814 { 2815 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0); 2816 } 2817 2818 /* 2819 * Detect the details of the product, and store anything needed 2820 * into the driver data structure. This includes product type and 2821 * version and number of slot groups. 2822 * 2823 * @dd Pointer to the driver data structure. 2824 * 2825 * return value 2826 * None 2827 */ 2828 static void mtip_detect_product(struct driver_data *dd) 2829 { 2830 u32 hwdata; 2831 unsigned int rev, slotgroups; 2832 2833 /* 2834 * HBA base + 0xFC [15:0] - vendor-specific hardware interface 2835 * info register: 2836 * [15:8] hardware/software interface rev# 2837 * [ 3] asic-style interface 2838 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style). 2839 */ 2840 hwdata = readl(dd->mmio + HOST_HSORG); 2841 2842 dd->product_type = MTIP_PRODUCT_UNKNOWN; 2843 dd->slot_groups = 1; 2844 2845 if (hwdata & 0x8) { 2846 dd->product_type = MTIP_PRODUCT_ASICFPGA; 2847 rev = (hwdata & HSORG_HWREV) >> 8; 2848 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1; 2849 dev_info(&dd->pdev->dev, 2850 "ASIC-FPGA design, HS rev 0x%x, " 2851 "%i slot groups [%i slots]\n", 2852 rev, 2853 slotgroups, 2854 slotgroups * 32); 2855 2856 if (slotgroups > MTIP_MAX_SLOT_GROUPS) { 2857 dev_warn(&dd->pdev->dev, 2858 "Warning: driver only supports " 2859 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS); 2860 slotgroups = MTIP_MAX_SLOT_GROUPS; 2861 } 2862 dd->slot_groups = slotgroups; 2863 return; 2864 } 2865 2866 dev_warn(&dd->pdev->dev, "Unrecognized product id\n"); 2867 } 2868 2869 /* 2870 * Blocking wait for FTL rebuild to complete 2871 * 2872 * @dd Pointer to the DRIVER_DATA structure. 2873 * 2874 * return value 2875 * 0 FTL rebuild completed successfully 2876 * -EFAULT FTL rebuild error/timeout/interruption 2877 */ 2878 static int mtip_ftl_rebuild_poll(struct driver_data *dd) 2879 { 2880 unsigned long timeout, cnt = 0, start; 2881 2882 dev_warn(&dd->pdev->dev, 2883 "FTL rebuild in progress. Polling for completion.\n"); 2884 2885 start = jiffies; 2886 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS); 2887 2888 do { 2889 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, 2890 &dd->dd_flag))) 2891 return -EFAULT; 2892 if (mtip_check_surprise_removal(dd->pdev)) 2893 return -EFAULT; 2894 2895 if (mtip_get_identify(dd->port, NULL) < 0) 2896 return -EFAULT; 2897 2898 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) == 2899 MTIP_FTL_REBUILD_MAGIC) { 2900 ssleep(1); 2901 /* Print message every 3 minutes */ 2902 if (cnt++ >= 180) { 2903 dev_warn(&dd->pdev->dev, 2904 "FTL rebuild in progress (%d secs).\n", 2905 jiffies_to_msecs(jiffies - start) / 1000); 2906 cnt = 0; 2907 } 2908 } else { 2909 dev_warn(&dd->pdev->dev, 2910 "FTL rebuild complete (%d secs).\n", 2911 jiffies_to_msecs(jiffies - start) / 1000); 2912 mtip_block_initialize(dd); 2913 return 0; 2914 } 2915 } while (time_before(jiffies, timeout)); 2916 2917 /* Check for timeout */ 2918 dev_err(&dd->pdev->dev, 2919 "Timed out waiting for FTL rebuild to complete (%d secs).\n", 2920 jiffies_to_msecs(jiffies - start) / 1000); 2921 return -EFAULT; 2922 } 2923 2924 static void mtip_softirq_done_fn(struct request *rq) 2925 { 2926 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq); 2927 struct driver_data *dd = rq->q->queuedata; 2928 2929 /* Unmap the DMA scatter list entries */ 2930 dma_unmap_sg(&dd->pdev->dev, cmd->sg, cmd->scatter_ents, 2931 cmd->direction); 2932 2933 if (unlikely(cmd->unaligned)) 2934 up(&dd->port->cmd_slot_unal); 2935 2936 blk_mq_end_request(rq, cmd->status); 2937 } 2938 2939 static void mtip_abort_cmd(struct request *req, void *data, 2940 bool reserved) 2941 { 2942 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(req); 2943 struct driver_data *dd = data; 2944 2945 dbg_printk(MTIP_DRV_NAME " Aborting request, tag = %d\n", req->tag); 2946 2947 clear_bit(req->tag, dd->port->cmds_to_issue); 2948 cmd->status = -EIO; 2949 mtip_softirq_done_fn(req); 2950 } 2951 2952 static void mtip_queue_cmd(struct request *req, void *data, 2953 bool reserved) 2954 { 2955 struct driver_data *dd = data; 2956 2957 set_bit(req->tag, dd->port->cmds_to_issue); 2958 blk_abort_request(req); 2959 } 2960 2961 /* 2962 * service thread to issue queued commands 2963 * 2964 * @data Pointer to the driver data structure. 2965 * 2966 * return value 2967 * 0 2968 */ 2969 2970 static int mtip_service_thread(void *data) 2971 { 2972 struct driver_data *dd = (struct driver_data *)data; 2973 unsigned long slot, slot_start, slot_wrap, to; 2974 unsigned int num_cmd_slots = dd->slot_groups * 32; 2975 struct mtip_port *port = dd->port; 2976 2977 while (1) { 2978 if (kthread_should_stop() || 2979 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags)) 2980 goto st_out; 2981 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags); 2982 2983 /* 2984 * the condition is to check neither an internal command is 2985 * is in progress nor error handling is active 2986 */ 2987 wait_event_interruptible(port->svc_wait, (port->flags) && 2988 (port->flags & MTIP_PF_SVC_THD_WORK)); 2989 2990 if (kthread_should_stop() || 2991 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags)) 2992 goto st_out; 2993 2994 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, 2995 &dd->dd_flag))) 2996 goto st_out; 2997 2998 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags); 2999 3000 restart_eh: 3001 /* Demux bits: start with error handling */ 3002 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) { 3003 mtip_handle_tfe(dd); 3004 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags); 3005 } 3006 3007 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) 3008 goto restart_eh; 3009 3010 if (test_bit(MTIP_PF_TO_ACTIVE_BIT, &port->flags)) { 3011 to = jiffies + msecs_to_jiffies(5000); 3012 3013 do { 3014 mdelay(100); 3015 } while (atomic_read(&dd->irq_workers_active) != 0 && 3016 time_before(jiffies, to)); 3017 3018 if (atomic_read(&dd->irq_workers_active) != 0) 3019 dev_warn(&dd->pdev->dev, 3020 "Completion workers still active!"); 3021 3022 spin_lock(dd->queue->queue_lock); 3023 blk_mq_tagset_busy_iter(&dd->tags, 3024 mtip_queue_cmd, dd); 3025 spin_unlock(dd->queue->queue_lock); 3026 3027 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &dd->port->flags); 3028 3029 if (mtip_device_reset(dd)) 3030 blk_mq_tagset_busy_iter(&dd->tags, 3031 mtip_abort_cmd, dd); 3032 3033 clear_bit(MTIP_PF_TO_ACTIVE_BIT, &dd->port->flags); 3034 } 3035 3036 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) { 3037 slot = 1; 3038 /* used to restrict the loop to one iteration */ 3039 slot_start = num_cmd_slots; 3040 slot_wrap = 0; 3041 while (1) { 3042 slot = find_next_bit(port->cmds_to_issue, 3043 num_cmd_slots, slot); 3044 if (slot_wrap == 1) { 3045 if ((slot_start >= slot) || 3046 (slot >= num_cmd_slots)) 3047 break; 3048 } 3049 if (unlikely(slot_start == num_cmd_slots)) 3050 slot_start = slot; 3051 3052 if (unlikely(slot == num_cmd_slots)) { 3053 slot = 1; 3054 slot_wrap = 1; 3055 continue; 3056 } 3057 3058 /* Issue the command to the hardware */ 3059 mtip_issue_ncq_command(port, slot); 3060 3061 clear_bit(slot, port->cmds_to_issue); 3062 } 3063 3064 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags); 3065 } 3066 3067 if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) { 3068 if (mtip_ftl_rebuild_poll(dd) == 0) 3069 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags); 3070 } 3071 } 3072 3073 st_out: 3074 return 0; 3075 } 3076 3077 /* 3078 * DMA region teardown 3079 * 3080 * @dd Pointer to driver_data structure 3081 * 3082 * return value 3083 * None 3084 */ 3085 static void mtip_dma_free(struct driver_data *dd) 3086 { 3087 struct mtip_port *port = dd->port; 3088 3089 if (port->block1) 3090 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ, 3091 port->block1, port->block1_dma); 3092 3093 if (port->command_list) { 3094 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ, 3095 port->command_list, port->command_list_dma); 3096 } 3097 } 3098 3099 /* 3100 * DMA region setup 3101 * 3102 * @dd Pointer to driver_data structure 3103 * 3104 * return value 3105 * -ENOMEM Not enough free DMA region space to initialize driver 3106 */ 3107 static int mtip_dma_alloc(struct driver_data *dd) 3108 { 3109 struct mtip_port *port = dd->port; 3110 3111 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */ 3112 port->block1 = 3113 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ, 3114 &port->block1_dma, GFP_KERNEL); 3115 if (!port->block1) 3116 return -ENOMEM; 3117 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ); 3118 3119 /* Allocate dma memory for command list */ 3120 port->command_list = 3121 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ, 3122 &port->command_list_dma, GFP_KERNEL); 3123 if (!port->command_list) { 3124 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ, 3125 port->block1, port->block1_dma); 3126 port->block1 = NULL; 3127 port->block1_dma = 0; 3128 return -ENOMEM; 3129 } 3130 memset(port->command_list, 0, AHCI_CMD_TBL_SZ); 3131 3132 /* Setup all pointers into first DMA region */ 3133 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET; 3134 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET; 3135 port->identify = port->block1 + AHCI_IDFY_OFFSET; 3136 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET; 3137 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET; 3138 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET; 3139 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET; 3140 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET; 3141 3142 return 0; 3143 } 3144 3145 static int mtip_hw_get_identify(struct driver_data *dd) 3146 { 3147 struct smart_attr attr242; 3148 unsigned char *buf; 3149 int rv; 3150 3151 if (mtip_get_identify(dd->port, NULL) < 0) 3152 return -EFAULT; 3153 3154 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) == 3155 MTIP_FTL_REBUILD_MAGIC) { 3156 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags); 3157 return MTIP_FTL_REBUILD_MAGIC; 3158 } 3159 mtip_dump_identify(dd->port); 3160 3161 /* check write protect, over temp and rebuild statuses */ 3162 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ, 3163 dd->port->log_buf, 3164 dd->port->log_buf_dma, 1); 3165 if (rv) { 3166 dev_warn(&dd->pdev->dev, 3167 "Error in READ LOG EXT (10h) command\n"); 3168 /* non-critical error, don't fail the load */ 3169 } else { 3170 buf = (unsigned char *)dd->port->log_buf; 3171 if (buf[259] & 0x1) { 3172 dev_info(&dd->pdev->dev, 3173 "Write protect bit is set.\n"); 3174 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag); 3175 } 3176 if (buf[288] == 0xF7) { 3177 dev_info(&dd->pdev->dev, 3178 "Exceeded Tmax, drive in thermal shutdown.\n"); 3179 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag); 3180 } 3181 if (buf[288] == 0xBF) { 3182 dev_info(&dd->pdev->dev, 3183 "Drive indicates rebuild has failed.\n"); 3184 set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag); 3185 } 3186 } 3187 3188 /* get write protect progess */ 3189 memset(&attr242, 0, sizeof(struct smart_attr)); 3190 if (mtip_get_smart_attr(dd->port, 242, &attr242)) 3191 dev_warn(&dd->pdev->dev, 3192 "Unable to check write protect progress\n"); 3193 else 3194 dev_info(&dd->pdev->dev, 3195 "Write protect progress: %u%% (%u blocks)\n", 3196 attr242.cur, le32_to_cpu(attr242.data)); 3197 3198 return rv; 3199 } 3200 3201 /* 3202 * Called once for each card. 3203 * 3204 * @dd Pointer to the driver data structure. 3205 * 3206 * return value 3207 * 0 on success, else an error code. 3208 */ 3209 static int mtip_hw_init(struct driver_data *dd) 3210 { 3211 int i; 3212 int rv; 3213 unsigned int num_command_slots; 3214 unsigned long timeout, timetaken; 3215 3216 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR]; 3217 3218 mtip_detect_product(dd); 3219 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) { 3220 rv = -EIO; 3221 goto out1; 3222 } 3223 num_command_slots = dd->slot_groups * 32; 3224 3225 hba_setup(dd); 3226 3227 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL, 3228 dd->numa_node); 3229 if (!dd->port) { 3230 dev_err(&dd->pdev->dev, 3231 "Memory allocation: port structure\n"); 3232 return -ENOMEM; 3233 } 3234 3235 /* Continue workqueue setup */ 3236 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++) 3237 dd->work[i].port = dd->port; 3238 3239 /* Enable unaligned IO constraints for some devices */ 3240 if (mtip_device_unaligned_constrained(dd)) 3241 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS; 3242 else 3243 dd->unal_qdepth = 0; 3244 3245 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth); 3246 3247 /* Spinlock to prevent concurrent issue */ 3248 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++) 3249 spin_lock_init(&dd->port->cmd_issue_lock[i]); 3250 3251 /* Set the port mmio base address. */ 3252 dd->port->mmio = dd->mmio + PORT_OFFSET; 3253 dd->port->dd = dd; 3254 3255 /* DMA allocations */ 3256 rv = mtip_dma_alloc(dd); 3257 if (rv < 0) 3258 goto out1; 3259 3260 /* Setup the pointers to the extended s_active and CI registers. */ 3261 for (i = 0; i < dd->slot_groups; i++) { 3262 dd->port->s_active[i] = 3263 dd->port->mmio + i*0x80 + PORT_SCR_ACT; 3264 dd->port->cmd_issue[i] = 3265 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE; 3266 dd->port->completed[i] = 3267 dd->port->mmio + i*0x80 + PORT_SDBV; 3268 } 3269 3270 timetaken = jiffies; 3271 timeout = jiffies + msecs_to_jiffies(30000); 3272 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) && 3273 time_before(jiffies, timeout)) { 3274 mdelay(100); 3275 } 3276 if (unlikely(mtip_check_surprise_removal(dd->pdev))) { 3277 timetaken = jiffies - timetaken; 3278 dev_warn(&dd->pdev->dev, 3279 "Surprise removal detected at %u ms\n", 3280 jiffies_to_msecs(timetaken)); 3281 rv = -ENODEV; 3282 goto out2 ; 3283 } 3284 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) { 3285 timetaken = jiffies - timetaken; 3286 dev_warn(&dd->pdev->dev, 3287 "Removal detected at %u ms\n", 3288 jiffies_to_msecs(timetaken)); 3289 rv = -EFAULT; 3290 goto out2; 3291 } 3292 3293 /* Conditionally reset the HBA. */ 3294 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) { 3295 if (mtip_hba_reset(dd) < 0) { 3296 dev_err(&dd->pdev->dev, 3297 "Card did not reset within timeout\n"); 3298 rv = -EIO; 3299 goto out2; 3300 } 3301 } else { 3302 /* Clear any pending interrupts on the HBA */ 3303 writel(readl(dd->mmio + HOST_IRQ_STAT), 3304 dd->mmio + HOST_IRQ_STAT); 3305 } 3306 3307 mtip_init_port(dd->port); 3308 mtip_start_port(dd->port); 3309 3310 /* Setup the ISR and enable interrupts. */ 3311 rv = devm_request_irq(&dd->pdev->dev, 3312 dd->pdev->irq, 3313 mtip_irq_handler, 3314 IRQF_SHARED, 3315 dev_driver_string(&dd->pdev->dev), 3316 dd); 3317 3318 if (rv) { 3319 dev_err(&dd->pdev->dev, 3320 "Unable to allocate IRQ %d\n", dd->pdev->irq); 3321 goto out2; 3322 } 3323 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding)); 3324 3325 /* Enable interrupts on the HBA. */ 3326 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN, 3327 dd->mmio + HOST_CTL); 3328 3329 init_waitqueue_head(&dd->port->svc_wait); 3330 3331 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) { 3332 rv = -EFAULT; 3333 goto out3; 3334 } 3335 3336 return rv; 3337 3338 out3: 3339 /* Disable interrupts on the HBA. */ 3340 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN, 3341 dd->mmio + HOST_CTL); 3342 3343 /* Release the IRQ. */ 3344 irq_set_affinity_hint(dd->pdev->irq, NULL); 3345 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd); 3346 3347 out2: 3348 mtip_deinit_port(dd->port); 3349 mtip_dma_free(dd); 3350 3351 out1: 3352 /* Free the memory allocated for the for structure. */ 3353 kfree(dd->port); 3354 3355 return rv; 3356 } 3357 3358 static int mtip_standby_drive(struct driver_data *dd) 3359 { 3360 int rv = 0; 3361 3362 if (dd->sr || !dd->port) 3363 return -ENODEV; 3364 /* 3365 * Send standby immediate (E0h) to the drive so that it 3366 * saves its state. 3367 */ 3368 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) && 3369 !test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag) && 3370 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)) { 3371 rv = mtip_standby_immediate(dd->port); 3372 if (rv) 3373 dev_warn(&dd->pdev->dev, 3374 "STANDBY IMMEDIATE failed\n"); 3375 } 3376 return rv; 3377 } 3378 3379 /* 3380 * Called to deinitialize an interface. 3381 * 3382 * @dd Pointer to the driver data structure. 3383 * 3384 * return value 3385 * 0 3386 */ 3387 static int mtip_hw_exit(struct driver_data *dd) 3388 { 3389 if (!dd->sr) { 3390 /* de-initialize the port. */ 3391 mtip_deinit_port(dd->port); 3392 3393 /* Disable interrupts on the HBA. */ 3394 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN, 3395 dd->mmio + HOST_CTL); 3396 } 3397 3398 /* Release the IRQ. */ 3399 irq_set_affinity_hint(dd->pdev->irq, NULL); 3400 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd); 3401 msleep(1000); 3402 3403 /* Free dma regions */ 3404 mtip_dma_free(dd); 3405 3406 /* Free the memory allocated for the for structure. */ 3407 kfree(dd->port); 3408 dd->port = NULL; 3409 3410 return 0; 3411 } 3412 3413 /* 3414 * Issue a Standby Immediate command to the device. 3415 * 3416 * This function is called by the Block Layer just before the 3417 * system powers off during a shutdown. 3418 * 3419 * @dd Pointer to the driver data structure. 3420 * 3421 * return value 3422 * 0 3423 */ 3424 static int mtip_hw_shutdown(struct driver_data *dd) 3425 { 3426 /* 3427 * Send standby immediate (E0h) to the drive so that it 3428 * saves its state. 3429 */ 3430 mtip_standby_drive(dd); 3431 3432 return 0; 3433 } 3434 3435 /* 3436 * Suspend function 3437 * 3438 * This function is called by the Block Layer just before the 3439 * system hibernates. 3440 * 3441 * @dd Pointer to the driver data structure. 3442 * 3443 * return value 3444 * 0 Suspend was successful 3445 * -EFAULT Suspend was not successful 3446 */ 3447 static int mtip_hw_suspend(struct driver_data *dd) 3448 { 3449 /* 3450 * Send standby immediate (E0h) to the drive 3451 * so that it saves its state. 3452 */ 3453 if (mtip_standby_drive(dd) != 0) { 3454 dev_err(&dd->pdev->dev, 3455 "Failed standby-immediate command\n"); 3456 return -EFAULT; 3457 } 3458 3459 /* Disable interrupts on the HBA.*/ 3460 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN, 3461 dd->mmio + HOST_CTL); 3462 mtip_deinit_port(dd->port); 3463 3464 return 0; 3465 } 3466 3467 /* 3468 * Resume function 3469 * 3470 * This function is called by the Block Layer as the 3471 * system resumes. 3472 * 3473 * @dd Pointer to the driver data structure. 3474 * 3475 * return value 3476 * 0 Resume was successful 3477 * -EFAULT Resume was not successful 3478 */ 3479 static int mtip_hw_resume(struct driver_data *dd) 3480 { 3481 /* Perform any needed hardware setup steps */ 3482 hba_setup(dd); 3483 3484 /* Reset the HBA */ 3485 if (mtip_hba_reset(dd) != 0) { 3486 dev_err(&dd->pdev->dev, 3487 "Unable to reset the HBA\n"); 3488 return -EFAULT; 3489 } 3490 3491 /* 3492 * Enable the port, DMA engine, and FIS reception specific 3493 * h/w in controller. 3494 */ 3495 mtip_init_port(dd->port); 3496 mtip_start_port(dd->port); 3497 3498 /* Enable interrupts on the HBA.*/ 3499 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN, 3500 dd->mmio + HOST_CTL); 3501 3502 return 0; 3503 } 3504 3505 /* 3506 * Helper function for reusing disk name 3507 * upon hot insertion. 3508 */ 3509 static int rssd_disk_name_format(char *prefix, 3510 int index, 3511 char *buf, 3512 int buflen) 3513 { 3514 const int base = 'z' - 'a' + 1; 3515 char *begin = buf + strlen(prefix); 3516 char *end = buf + buflen; 3517 char *p; 3518 int unit; 3519 3520 p = end - 1; 3521 *p = '\0'; 3522 unit = base; 3523 do { 3524 if (p == begin) 3525 return -EINVAL; 3526 *--p = 'a' + (index % unit); 3527 index = (index / unit) - 1; 3528 } while (index >= 0); 3529 3530 memmove(begin, p, end - p); 3531 memcpy(buf, prefix, strlen(prefix)); 3532 3533 return 0; 3534 } 3535 3536 /* 3537 * Block layer IOCTL handler. 3538 * 3539 * @dev Pointer to the block_device structure. 3540 * @mode ignored 3541 * @cmd IOCTL command passed from the user application. 3542 * @arg Argument passed from the user application. 3543 * 3544 * return value 3545 * 0 IOCTL completed successfully. 3546 * -ENOTTY IOCTL not supported or invalid driver data 3547 * structure pointer. 3548 */ 3549 static int mtip_block_ioctl(struct block_device *dev, 3550 fmode_t mode, 3551 unsigned cmd, 3552 unsigned long arg) 3553 { 3554 struct driver_data *dd = dev->bd_disk->private_data; 3555 3556 if (!capable(CAP_SYS_ADMIN)) 3557 return -EACCES; 3558 3559 if (!dd) 3560 return -ENOTTY; 3561 3562 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) 3563 return -ENOTTY; 3564 3565 switch (cmd) { 3566 case BLKFLSBUF: 3567 return -ENOTTY; 3568 default: 3569 return mtip_hw_ioctl(dd, cmd, arg); 3570 } 3571 } 3572 3573 #ifdef CONFIG_COMPAT 3574 /* 3575 * Block layer compat IOCTL handler. 3576 * 3577 * @dev Pointer to the block_device structure. 3578 * @mode ignored 3579 * @cmd IOCTL command passed from the user application. 3580 * @arg Argument passed from the user application. 3581 * 3582 * return value 3583 * 0 IOCTL completed successfully. 3584 * -ENOTTY IOCTL not supported or invalid driver data 3585 * structure pointer. 3586 */ 3587 static int mtip_block_compat_ioctl(struct block_device *dev, 3588 fmode_t mode, 3589 unsigned cmd, 3590 unsigned long arg) 3591 { 3592 struct driver_data *dd = dev->bd_disk->private_data; 3593 3594 if (!capable(CAP_SYS_ADMIN)) 3595 return -EACCES; 3596 3597 if (!dd) 3598 return -ENOTTY; 3599 3600 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) 3601 return -ENOTTY; 3602 3603 switch (cmd) { 3604 case BLKFLSBUF: 3605 return -ENOTTY; 3606 case HDIO_DRIVE_TASKFILE: { 3607 struct mtip_compat_ide_task_request_s __user *compat_req_task; 3608 ide_task_request_t req_task; 3609 int compat_tasksize, outtotal, ret; 3610 3611 compat_tasksize = 3612 sizeof(struct mtip_compat_ide_task_request_s); 3613 3614 compat_req_task = 3615 (struct mtip_compat_ide_task_request_s __user *) arg; 3616 3617 if (copy_from_user(&req_task, (void __user *) arg, 3618 compat_tasksize - (2 * sizeof(compat_long_t)))) 3619 return -EFAULT; 3620 3621 if (get_user(req_task.out_size, &compat_req_task->out_size)) 3622 return -EFAULT; 3623 3624 if (get_user(req_task.in_size, &compat_req_task->in_size)) 3625 return -EFAULT; 3626 3627 outtotal = sizeof(struct mtip_compat_ide_task_request_s); 3628 3629 ret = exec_drive_taskfile(dd, (void __user *) arg, 3630 &req_task, outtotal); 3631 3632 if (copy_to_user((void __user *) arg, &req_task, 3633 compat_tasksize - 3634 (2 * sizeof(compat_long_t)))) 3635 return -EFAULT; 3636 3637 if (put_user(req_task.out_size, &compat_req_task->out_size)) 3638 return -EFAULT; 3639 3640 if (put_user(req_task.in_size, &compat_req_task->in_size)) 3641 return -EFAULT; 3642 3643 return ret; 3644 } 3645 default: 3646 return mtip_hw_ioctl(dd, cmd, arg); 3647 } 3648 } 3649 #endif 3650 3651 /* 3652 * Obtain the geometry of the device. 3653 * 3654 * You may think that this function is obsolete, but some applications, 3655 * fdisk for example still used CHS values. This function describes the 3656 * device as having 224 heads and 56 sectors per cylinder. These values are 3657 * chosen so that each cylinder is aligned on a 4KB boundary. Since a 3658 * partition is described in terms of a start and end cylinder this means 3659 * that each partition is also 4KB aligned. Non-aligned partitions adversely 3660 * affects performance. 3661 * 3662 * @dev Pointer to the block_device strucutre. 3663 * @geo Pointer to a hd_geometry structure. 3664 * 3665 * return value 3666 * 0 Operation completed successfully. 3667 * -ENOTTY An error occurred while reading the drive capacity. 3668 */ 3669 static int mtip_block_getgeo(struct block_device *dev, 3670 struct hd_geometry *geo) 3671 { 3672 struct driver_data *dd = dev->bd_disk->private_data; 3673 sector_t capacity; 3674 3675 if (!dd) 3676 return -ENOTTY; 3677 3678 if (!(mtip_hw_get_capacity(dd, &capacity))) { 3679 dev_warn(&dd->pdev->dev, 3680 "Could not get drive capacity.\n"); 3681 return -ENOTTY; 3682 } 3683 3684 geo->heads = 224; 3685 geo->sectors = 56; 3686 sector_div(capacity, (geo->heads * geo->sectors)); 3687 geo->cylinders = capacity; 3688 return 0; 3689 } 3690 3691 static int mtip_block_open(struct block_device *dev, fmode_t mode) 3692 { 3693 struct driver_data *dd; 3694 3695 if (dev && dev->bd_disk) { 3696 dd = (struct driver_data *) dev->bd_disk->private_data; 3697 3698 if (dd) { 3699 if (test_bit(MTIP_DDF_REMOVAL_BIT, 3700 &dd->dd_flag)) { 3701 return -ENODEV; 3702 } 3703 return 0; 3704 } 3705 } 3706 return -ENODEV; 3707 } 3708 3709 static void mtip_block_release(struct gendisk *disk, fmode_t mode) 3710 { 3711 } 3712 3713 /* 3714 * Block device operation function. 3715 * 3716 * This structure contains pointers to the functions required by the block 3717 * layer. 3718 */ 3719 static const struct block_device_operations mtip_block_ops = { 3720 .open = mtip_block_open, 3721 .release = mtip_block_release, 3722 .ioctl = mtip_block_ioctl, 3723 #ifdef CONFIG_COMPAT 3724 .compat_ioctl = mtip_block_compat_ioctl, 3725 #endif 3726 .getgeo = mtip_block_getgeo, 3727 .owner = THIS_MODULE 3728 }; 3729 3730 static inline bool is_se_active(struct driver_data *dd) 3731 { 3732 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT, &dd->port->flags))) { 3733 if (dd->port->ic_pause_timer) { 3734 unsigned long to = dd->port->ic_pause_timer + 3735 msecs_to_jiffies(1000); 3736 if (time_after(jiffies, to)) { 3737 clear_bit(MTIP_PF_SE_ACTIVE_BIT, 3738 &dd->port->flags); 3739 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag); 3740 dd->port->ic_pause_timer = 0; 3741 wake_up_interruptible(&dd->port->svc_wait); 3742 return false; 3743 } 3744 } 3745 return true; 3746 } 3747 return false; 3748 } 3749 3750 /* 3751 * Block layer make request function. 3752 * 3753 * This function is called by the kernel to process a BIO for 3754 * the P320 device. 3755 * 3756 * @queue Pointer to the request queue. Unused other than to obtain 3757 * the driver data structure. 3758 * @rq Pointer to the request. 3759 * 3760 */ 3761 static int mtip_submit_request(struct blk_mq_hw_ctx *hctx, struct request *rq) 3762 { 3763 struct driver_data *dd = hctx->queue->queuedata; 3764 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq); 3765 unsigned int nents; 3766 3767 if (is_se_active(dd)) 3768 return -ENODATA; 3769 3770 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) { 3771 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, 3772 &dd->dd_flag))) { 3773 return -ENXIO; 3774 } 3775 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) { 3776 return -ENODATA; 3777 } 3778 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT, 3779 &dd->dd_flag) && 3780 rq_data_dir(rq))) { 3781 return -ENODATA; 3782 } 3783 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag) || 3784 test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag))) 3785 return -ENODATA; 3786 } 3787 3788 if (req_op(rq) == REQ_OP_DISCARD) { 3789 int err; 3790 3791 err = mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq)); 3792 blk_mq_end_request(rq, err); 3793 return 0; 3794 } 3795 3796 /* Create the scatter list for this request. */ 3797 nents = blk_rq_map_sg(hctx->queue, rq, cmd->sg); 3798 3799 /* Issue the read/write. */ 3800 mtip_hw_submit_io(dd, rq, cmd, nents, hctx); 3801 return 0; 3802 } 3803 3804 static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx, 3805 struct request *rq) 3806 { 3807 struct driver_data *dd = hctx->queue->queuedata; 3808 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq); 3809 3810 if (rq_data_dir(rq) == READ || !dd->unal_qdepth) 3811 return false; 3812 3813 /* 3814 * If unaligned depth must be limited on this controller, mark it 3815 * as unaligned if the IO isn't on a 4k boundary (start of length). 3816 */ 3817 if (blk_rq_sectors(rq) <= 64) { 3818 if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7)) 3819 cmd->unaligned = 1; 3820 } 3821 3822 if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal)) 3823 return true; 3824 3825 return false; 3826 } 3827 3828 static int mtip_queue_rq(struct blk_mq_hw_ctx *hctx, 3829 const struct blk_mq_queue_data *bd) 3830 { 3831 struct request *rq = bd->rq; 3832 int ret; 3833 3834 mtip_init_cmd_header(rq); 3835 3836 if (unlikely(mtip_check_unal_depth(hctx, rq))) 3837 return BLK_MQ_RQ_QUEUE_BUSY; 3838 3839 blk_mq_start_request(rq); 3840 3841 ret = mtip_submit_request(hctx, rq); 3842 if (likely(!ret)) 3843 return BLK_MQ_RQ_QUEUE_OK; 3844 3845 return BLK_MQ_RQ_QUEUE_ERROR; 3846 } 3847 3848 static void mtip_free_cmd(void *data, struct request *rq, 3849 unsigned int hctx_idx, unsigned int request_idx) 3850 { 3851 struct driver_data *dd = data; 3852 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq); 3853 3854 if (!cmd->command) 3855 return; 3856 3857 dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ, 3858 cmd->command, cmd->command_dma); 3859 } 3860 3861 static int mtip_init_cmd(void *data, struct request *rq, unsigned int hctx_idx, 3862 unsigned int request_idx, unsigned int numa_node) 3863 { 3864 struct driver_data *dd = data; 3865 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq); 3866 3867 /* 3868 * For flush requests, request_idx starts at the end of the 3869 * tag space. Since we don't support FLUSH/FUA, simply return 3870 * 0 as there's nothing to be done. 3871 */ 3872 if (request_idx >= MTIP_MAX_COMMAND_SLOTS) 3873 return 0; 3874 3875 cmd->command = dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ, 3876 &cmd->command_dma, GFP_KERNEL); 3877 if (!cmd->command) 3878 return -ENOMEM; 3879 3880 memset(cmd->command, 0, CMD_DMA_ALLOC_SZ); 3881 3882 sg_init_table(cmd->sg, MTIP_MAX_SG); 3883 return 0; 3884 } 3885 3886 static enum blk_eh_timer_return mtip_cmd_timeout(struct request *req, 3887 bool reserved) 3888 { 3889 struct driver_data *dd = req->q->queuedata; 3890 3891 if (reserved) 3892 goto exit_handler; 3893 3894 if (test_bit(req->tag, dd->port->cmds_to_issue)) 3895 goto exit_handler; 3896 3897 if (test_and_set_bit(MTIP_PF_TO_ACTIVE_BIT, &dd->port->flags)) 3898 goto exit_handler; 3899 3900 wake_up_interruptible(&dd->port->svc_wait); 3901 exit_handler: 3902 return BLK_EH_RESET_TIMER; 3903 } 3904 3905 static const struct blk_mq_ops mtip_mq_ops = { 3906 .queue_rq = mtip_queue_rq, 3907 .init_request = mtip_init_cmd, 3908 .exit_request = mtip_free_cmd, 3909 .complete = mtip_softirq_done_fn, 3910 .timeout = mtip_cmd_timeout, 3911 }; 3912 3913 /* 3914 * Block layer initialization function. 3915 * 3916 * This function is called once by the PCI layer for each P320 3917 * device that is connected to the system. 3918 * 3919 * @dd Pointer to the driver data structure. 3920 * 3921 * return value 3922 * 0 on success else an error code. 3923 */ 3924 static int mtip_block_initialize(struct driver_data *dd) 3925 { 3926 int rv = 0, wait_for_rebuild = 0; 3927 sector_t capacity; 3928 unsigned int index = 0; 3929 struct kobject *kobj; 3930 3931 if (dd->disk) 3932 goto skip_create_disk; /* hw init done, before rebuild */ 3933 3934 if (mtip_hw_init(dd)) { 3935 rv = -EINVAL; 3936 goto protocol_init_error; 3937 } 3938 3939 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node); 3940 if (dd->disk == NULL) { 3941 dev_err(&dd->pdev->dev, 3942 "Unable to allocate gendisk structure\n"); 3943 rv = -EINVAL; 3944 goto alloc_disk_error; 3945 } 3946 3947 /* Generate the disk name, implemented same as in sd.c */ 3948 do { 3949 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL)) { 3950 rv = -ENOMEM; 3951 goto ida_get_error; 3952 } 3953 3954 spin_lock(&rssd_index_lock); 3955 rv = ida_get_new(&rssd_index_ida, &index); 3956 spin_unlock(&rssd_index_lock); 3957 } while (rv == -EAGAIN); 3958 3959 if (rv) 3960 goto ida_get_error; 3961 3962 rv = rssd_disk_name_format("rssd", 3963 index, 3964 dd->disk->disk_name, 3965 DISK_NAME_LEN); 3966 if (rv) 3967 goto disk_index_error; 3968 3969 dd->disk->major = dd->major; 3970 dd->disk->first_minor = index * MTIP_MAX_MINORS; 3971 dd->disk->minors = MTIP_MAX_MINORS; 3972 dd->disk->fops = &mtip_block_ops; 3973 dd->disk->private_data = dd; 3974 dd->index = index; 3975 3976 mtip_hw_debugfs_init(dd); 3977 3978 memset(&dd->tags, 0, sizeof(dd->tags)); 3979 dd->tags.ops = &mtip_mq_ops; 3980 dd->tags.nr_hw_queues = 1; 3981 dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS; 3982 dd->tags.reserved_tags = 1; 3983 dd->tags.cmd_size = sizeof(struct mtip_cmd); 3984 dd->tags.numa_node = dd->numa_node; 3985 dd->tags.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_NO_SCHED; 3986 dd->tags.driver_data = dd; 3987 dd->tags.timeout = MTIP_NCQ_CMD_TIMEOUT_MS; 3988 3989 rv = blk_mq_alloc_tag_set(&dd->tags); 3990 if (rv) { 3991 dev_err(&dd->pdev->dev, 3992 "Unable to allocate request queue\n"); 3993 goto block_queue_alloc_tag_error; 3994 } 3995 3996 /* Allocate the request queue. */ 3997 dd->queue = blk_mq_init_queue(&dd->tags); 3998 if (IS_ERR(dd->queue)) { 3999 dev_err(&dd->pdev->dev, 4000 "Unable to allocate request queue\n"); 4001 rv = -ENOMEM; 4002 goto block_queue_alloc_init_error; 4003 } 4004 4005 dd->disk->queue = dd->queue; 4006 dd->queue->queuedata = dd; 4007 4008 skip_create_disk: 4009 /* Initialize the protocol layer. */ 4010 wait_for_rebuild = mtip_hw_get_identify(dd); 4011 if (wait_for_rebuild < 0) { 4012 dev_err(&dd->pdev->dev, 4013 "Protocol layer initialization failed\n"); 4014 rv = -EINVAL; 4015 goto init_hw_cmds_error; 4016 } 4017 4018 /* 4019 * if rebuild pending, start the service thread, and delay the block 4020 * queue creation and device_add_disk() 4021 */ 4022 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC) 4023 goto start_service_thread; 4024 4025 /* Set device limits. */ 4026 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags); 4027 clear_bit(QUEUE_FLAG_ADD_RANDOM, &dd->queue->queue_flags); 4028 blk_queue_max_segments(dd->queue, MTIP_MAX_SG); 4029 blk_queue_physical_block_size(dd->queue, 4096); 4030 blk_queue_max_hw_sectors(dd->queue, 0xffff); 4031 blk_queue_max_segment_size(dd->queue, 0x400000); 4032 blk_queue_io_min(dd->queue, 4096); 4033 blk_queue_bounce_limit(dd->queue, dd->pdev->dma_mask); 4034 4035 /* Signal trim support */ 4036 if (dd->trim_supp == true) { 4037 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags); 4038 dd->queue->limits.discard_granularity = 4096; 4039 blk_queue_max_discard_sectors(dd->queue, 4040 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES); 4041 } 4042 4043 /* Set the capacity of the device in 512 byte sectors. */ 4044 if (!(mtip_hw_get_capacity(dd, &capacity))) { 4045 dev_warn(&dd->pdev->dev, 4046 "Could not read drive capacity\n"); 4047 rv = -EIO; 4048 goto read_capacity_error; 4049 } 4050 set_capacity(dd->disk, capacity); 4051 4052 /* Enable the block device and add it to /dev */ 4053 device_add_disk(&dd->pdev->dev, dd->disk); 4054 4055 dd->bdev = bdget_disk(dd->disk, 0); 4056 /* 4057 * Now that the disk is active, initialize any sysfs attributes 4058 * managed by the protocol layer. 4059 */ 4060 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj); 4061 if (kobj) { 4062 mtip_hw_sysfs_init(dd, kobj); 4063 kobject_put(kobj); 4064 } 4065 4066 if (dd->mtip_svc_handler) { 4067 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag); 4068 return rv; /* service thread created for handling rebuild */ 4069 } 4070 4071 start_service_thread: 4072 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread, 4073 dd, dd->numa_node, 4074 "mtip_svc_thd_%02d", index); 4075 4076 if (IS_ERR(dd->mtip_svc_handler)) { 4077 dev_err(&dd->pdev->dev, "service thread failed to start\n"); 4078 dd->mtip_svc_handler = NULL; 4079 rv = -EFAULT; 4080 goto kthread_run_error; 4081 } 4082 wake_up_process(dd->mtip_svc_handler); 4083 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC) 4084 rv = wait_for_rebuild; 4085 4086 return rv; 4087 4088 kthread_run_error: 4089 bdput(dd->bdev); 4090 dd->bdev = NULL; 4091 4092 /* Delete our gendisk. This also removes the device from /dev */ 4093 del_gendisk(dd->disk); 4094 4095 read_capacity_error: 4096 init_hw_cmds_error: 4097 blk_cleanup_queue(dd->queue); 4098 block_queue_alloc_init_error: 4099 blk_mq_free_tag_set(&dd->tags); 4100 block_queue_alloc_tag_error: 4101 mtip_hw_debugfs_exit(dd); 4102 disk_index_error: 4103 spin_lock(&rssd_index_lock); 4104 ida_remove(&rssd_index_ida, index); 4105 spin_unlock(&rssd_index_lock); 4106 4107 ida_get_error: 4108 put_disk(dd->disk); 4109 4110 alloc_disk_error: 4111 mtip_hw_exit(dd); /* De-initialize the protocol layer. */ 4112 4113 protocol_init_error: 4114 return rv; 4115 } 4116 4117 static void mtip_no_dev_cleanup(struct request *rq, void *data, bool reserv) 4118 { 4119 struct driver_data *dd = (struct driver_data *)data; 4120 struct mtip_cmd *cmd; 4121 4122 if (likely(!reserv)) { 4123 cmd = blk_mq_rq_to_pdu(rq); 4124 cmd->status = -ENODEV; 4125 blk_mq_complete_request(rq); 4126 } else if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &dd->port->flags)) { 4127 4128 cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL); 4129 if (cmd->comp_func) 4130 cmd->comp_func(dd->port, MTIP_TAG_INTERNAL, 4131 cmd, -ENODEV); 4132 } 4133 } 4134 4135 /* 4136 * Block layer deinitialization function. 4137 * 4138 * Called by the PCI layer as each P320 device is removed. 4139 * 4140 * @dd Pointer to the driver data structure. 4141 * 4142 * return value 4143 * 0 4144 */ 4145 static int mtip_block_remove(struct driver_data *dd) 4146 { 4147 struct kobject *kobj; 4148 4149 mtip_hw_debugfs_exit(dd); 4150 4151 if (dd->mtip_svc_handler) { 4152 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags); 4153 wake_up_interruptible(&dd->port->svc_wait); 4154 kthread_stop(dd->mtip_svc_handler); 4155 } 4156 4157 /* Clean up the sysfs attributes, if created */ 4158 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) { 4159 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj); 4160 if (kobj) { 4161 mtip_hw_sysfs_exit(dd, kobj); 4162 kobject_put(kobj); 4163 } 4164 } 4165 4166 if (!dd->sr) { 4167 /* 4168 * Explicitly wait here for IOs to quiesce, 4169 * as mtip_standby_drive usually won't wait for IOs. 4170 */ 4171 if (!mtip_quiesce_io(dd->port, MTIP_QUIESCE_IO_TIMEOUT_MS, 4172 GFP_KERNEL)) 4173 mtip_standby_drive(dd); 4174 } 4175 else 4176 dev_info(&dd->pdev->dev, "device %s surprise removal\n", 4177 dd->disk->disk_name); 4178 4179 blk_freeze_queue_start(dd->queue); 4180 blk_mq_stop_hw_queues(dd->queue); 4181 blk_mq_tagset_busy_iter(&dd->tags, mtip_no_dev_cleanup, dd); 4182 4183 /* 4184 * Delete our gendisk structure. This also removes the device 4185 * from /dev 4186 */ 4187 if (dd->bdev) { 4188 bdput(dd->bdev); 4189 dd->bdev = NULL; 4190 } 4191 if (dd->disk) { 4192 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) 4193 del_gendisk(dd->disk); 4194 if (dd->disk->queue) { 4195 blk_cleanup_queue(dd->queue); 4196 blk_mq_free_tag_set(&dd->tags); 4197 dd->queue = NULL; 4198 } 4199 put_disk(dd->disk); 4200 } 4201 dd->disk = NULL; 4202 4203 spin_lock(&rssd_index_lock); 4204 ida_remove(&rssd_index_ida, dd->index); 4205 spin_unlock(&rssd_index_lock); 4206 4207 /* De-initialize the protocol layer. */ 4208 mtip_hw_exit(dd); 4209 4210 return 0; 4211 } 4212 4213 /* 4214 * Function called by the PCI layer when just before the 4215 * machine shuts down. 4216 * 4217 * If a protocol layer shutdown function is present it will be called 4218 * by this function. 4219 * 4220 * @dd Pointer to the driver data structure. 4221 * 4222 * return value 4223 * 0 4224 */ 4225 static int mtip_block_shutdown(struct driver_data *dd) 4226 { 4227 mtip_hw_shutdown(dd); 4228 4229 /* Delete our gendisk structure, and cleanup the blk queue. */ 4230 if (dd->disk) { 4231 dev_info(&dd->pdev->dev, 4232 "Shutting down %s ...\n", dd->disk->disk_name); 4233 4234 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) 4235 del_gendisk(dd->disk); 4236 if (dd->disk->queue) { 4237 blk_cleanup_queue(dd->queue); 4238 blk_mq_free_tag_set(&dd->tags); 4239 } 4240 put_disk(dd->disk); 4241 dd->disk = NULL; 4242 dd->queue = NULL; 4243 } 4244 4245 spin_lock(&rssd_index_lock); 4246 ida_remove(&rssd_index_ida, dd->index); 4247 spin_unlock(&rssd_index_lock); 4248 return 0; 4249 } 4250 4251 static int mtip_block_suspend(struct driver_data *dd) 4252 { 4253 dev_info(&dd->pdev->dev, 4254 "Suspending %s ...\n", dd->disk->disk_name); 4255 mtip_hw_suspend(dd); 4256 return 0; 4257 } 4258 4259 static int mtip_block_resume(struct driver_data *dd) 4260 { 4261 dev_info(&dd->pdev->dev, "Resuming %s ...\n", 4262 dd->disk->disk_name); 4263 mtip_hw_resume(dd); 4264 return 0; 4265 } 4266 4267 static void drop_cpu(int cpu) 4268 { 4269 cpu_use[cpu]--; 4270 } 4271 4272 static int get_least_used_cpu_on_node(int node) 4273 { 4274 int cpu, least_used_cpu, least_cnt; 4275 const struct cpumask *node_mask; 4276 4277 node_mask = cpumask_of_node(node); 4278 least_used_cpu = cpumask_first(node_mask); 4279 least_cnt = cpu_use[least_used_cpu]; 4280 cpu = least_used_cpu; 4281 4282 for_each_cpu(cpu, node_mask) { 4283 if (cpu_use[cpu] < least_cnt) { 4284 least_used_cpu = cpu; 4285 least_cnt = cpu_use[cpu]; 4286 } 4287 } 4288 cpu_use[least_used_cpu]++; 4289 return least_used_cpu; 4290 } 4291 4292 /* Helper for selecting a node in round robin mode */ 4293 static inline int mtip_get_next_rr_node(void) 4294 { 4295 static int next_node = -1; 4296 4297 if (next_node == -1) { 4298 next_node = first_online_node; 4299 return next_node; 4300 } 4301 4302 next_node = next_online_node(next_node); 4303 if (next_node == MAX_NUMNODES) 4304 next_node = first_online_node; 4305 return next_node; 4306 } 4307 4308 static DEFINE_HANDLER(0); 4309 static DEFINE_HANDLER(1); 4310 static DEFINE_HANDLER(2); 4311 static DEFINE_HANDLER(3); 4312 static DEFINE_HANDLER(4); 4313 static DEFINE_HANDLER(5); 4314 static DEFINE_HANDLER(6); 4315 static DEFINE_HANDLER(7); 4316 4317 static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev) 4318 { 4319 int pos; 4320 unsigned short pcie_dev_ctrl; 4321 4322 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP); 4323 if (pos) { 4324 pci_read_config_word(pdev, 4325 pos + PCI_EXP_DEVCTL, 4326 &pcie_dev_ctrl); 4327 if (pcie_dev_ctrl & (1 << 11) || 4328 pcie_dev_ctrl & (1 << 4)) { 4329 dev_info(&dd->pdev->dev, 4330 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n", 4331 pdev->vendor, pdev->device); 4332 pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN | 4333 PCI_EXP_DEVCTL_RELAX_EN); 4334 pci_write_config_word(pdev, 4335 pos + PCI_EXP_DEVCTL, 4336 pcie_dev_ctrl); 4337 } 4338 } 4339 } 4340 4341 static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev) 4342 { 4343 /* 4344 * This workaround is specific to AMD/ATI chipset with a PCI upstream 4345 * device with device id 0x5aXX 4346 */ 4347 if (pdev->bus && pdev->bus->self) { 4348 if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI && 4349 ((pdev->bus->self->device & 0xff00) == 0x5a00)) { 4350 mtip_disable_link_opts(dd, pdev->bus->self); 4351 } else { 4352 /* Check further up the topology */ 4353 struct pci_dev *parent_dev = pdev->bus->self; 4354 if (parent_dev->bus && 4355 parent_dev->bus->parent && 4356 parent_dev->bus->parent->self && 4357 parent_dev->bus->parent->self->vendor == 4358 PCI_VENDOR_ID_ATI && 4359 (parent_dev->bus->parent->self->device & 4360 0xff00) == 0x5a00) { 4361 mtip_disable_link_opts(dd, 4362 parent_dev->bus->parent->self); 4363 } 4364 } 4365 } 4366 } 4367 4368 /* 4369 * Called for each supported PCI device detected. 4370 * 4371 * This function allocates the private data structure, enables the 4372 * PCI device and then calls the block layer initialization function. 4373 * 4374 * return value 4375 * 0 on success else an error code. 4376 */ 4377 static int mtip_pci_probe(struct pci_dev *pdev, 4378 const struct pci_device_id *ent) 4379 { 4380 int rv = 0; 4381 struct driver_data *dd = NULL; 4382 char cpu_list[256]; 4383 const struct cpumask *node_mask; 4384 int cpu, i = 0, j = 0; 4385 int my_node = NUMA_NO_NODE; 4386 unsigned long flags; 4387 4388 /* Allocate memory for this devices private data. */ 4389 my_node = pcibus_to_node(pdev->bus); 4390 if (my_node != NUMA_NO_NODE) { 4391 if (!node_online(my_node)) 4392 my_node = mtip_get_next_rr_node(); 4393 } else { 4394 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n"); 4395 my_node = mtip_get_next_rr_node(); 4396 } 4397 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n", 4398 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev), 4399 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id()); 4400 4401 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node); 4402 if (dd == NULL) { 4403 dev_err(&pdev->dev, 4404 "Unable to allocate memory for driver data\n"); 4405 return -ENOMEM; 4406 } 4407 4408 /* Attach the private data to this PCI device. */ 4409 pci_set_drvdata(pdev, dd); 4410 4411 rv = pcim_enable_device(pdev); 4412 if (rv < 0) { 4413 dev_err(&pdev->dev, "Unable to enable device\n"); 4414 goto iomap_err; 4415 } 4416 4417 /* Map BAR5 to memory. */ 4418 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME); 4419 if (rv < 0) { 4420 dev_err(&pdev->dev, "Unable to map regions\n"); 4421 goto iomap_err; 4422 } 4423 4424 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) { 4425 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); 4426 4427 if (rv) { 4428 rv = pci_set_consistent_dma_mask(pdev, 4429 DMA_BIT_MASK(32)); 4430 if (rv) { 4431 dev_warn(&pdev->dev, 4432 "64-bit DMA enable failed\n"); 4433 goto setmask_err; 4434 } 4435 } 4436 } 4437 4438 /* Copy the info we may need later into the private data structure. */ 4439 dd->major = mtip_major; 4440 dd->instance = instance; 4441 dd->pdev = pdev; 4442 dd->numa_node = my_node; 4443 4444 INIT_LIST_HEAD(&dd->online_list); 4445 INIT_LIST_HEAD(&dd->remove_list); 4446 4447 memset(dd->workq_name, 0, 32); 4448 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance); 4449 4450 dd->isr_workq = create_workqueue(dd->workq_name); 4451 if (!dd->isr_workq) { 4452 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance); 4453 rv = -ENOMEM; 4454 goto block_initialize_err; 4455 } 4456 4457 memset(cpu_list, 0, sizeof(cpu_list)); 4458 4459 node_mask = cpumask_of_node(dd->numa_node); 4460 if (!cpumask_empty(node_mask)) { 4461 for_each_cpu(cpu, node_mask) 4462 { 4463 snprintf(&cpu_list[j], 256 - j, "%d ", cpu); 4464 j = strlen(cpu_list); 4465 } 4466 4467 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n", 4468 dd->numa_node, 4469 topology_physical_package_id(cpumask_first(node_mask)), 4470 nr_cpus_node(dd->numa_node), 4471 cpu_list); 4472 } else 4473 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n"); 4474 4475 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node); 4476 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n", 4477 cpu_to_node(dd->isr_binding), dd->isr_binding); 4478 4479 /* first worker context always runs in ISR */ 4480 dd->work[0].cpu_binding = dd->isr_binding; 4481 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node); 4482 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node); 4483 dd->work[3].cpu_binding = dd->work[0].cpu_binding; 4484 dd->work[4].cpu_binding = dd->work[1].cpu_binding; 4485 dd->work[5].cpu_binding = dd->work[2].cpu_binding; 4486 dd->work[6].cpu_binding = dd->work[2].cpu_binding; 4487 dd->work[7].cpu_binding = dd->work[1].cpu_binding; 4488 4489 /* Log the bindings */ 4490 for_each_present_cpu(cpu) { 4491 memset(cpu_list, 0, sizeof(cpu_list)); 4492 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) { 4493 if (dd->work[i].cpu_binding == cpu) { 4494 snprintf(&cpu_list[j], 256 - j, "%d ", i); 4495 j = strlen(cpu_list); 4496 } 4497 } 4498 if (j) 4499 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list); 4500 } 4501 4502 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0); 4503 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1); 4504 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2); 4505 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3); 4506 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4); 4507 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5); 4508 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6); 4509 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7); 4510 4511 pci_set_master(pdev); 4512 rv = pci_enable_msi(pdev); 4513 if (rv) { 4514 dev_warn(&pdev->dev, 4515 "Unable to enable MSI interrupt.\n"); 4516 goto msi_initialize_err; 4517 } 4518 4519 mtip_fix_ero_nosnoop(dd, pdev); 4520 4521 /* Initialize the block layer. */ 4522 rv = mtip_block_initialize(dd); 4523 if (rv < 0) { 4524 dev_err(&pdev->dev, 4525 "Unable to initialize block layer\n"); 4526 goto block_initialize_err; 4527 } 4528 4529 /* 4530 * Increment the instance count so that each device has a unique 4531 * instance number. 4532 */ 4533 instance++; 4534 if (rv != MTIP_FTL_REBUILD_MAGIC) 4535 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag); 4536 else 4537 rv = 0; /* device in rebuild state, return 0 from probe */ 4538 4539 /* Add to online list even if in ftl rebuild */ 4540 spin_lock_irqsave(&dev_lock, flags); 4541 list_add(&dd->online_list, &online_list); 4542 spin_unlock_irqrestore(&dev_lock, flags); 4543 4544 goto done; 4545 4546 block_initialize_err: 4547 pci_disable_msi(pdev); 4548 4549 msi_initialize_err: 4550 if (dd->isr_workq) { 4551 flush_workqueue(dd->isr_workq); 4552 destroy_workqueue(dd->isr_workq); 4553 drop_cpu(dd->work[0].cpu_binding); 4554 drop_cpu(dd->work[1].cpu_binding); 4555 drop_cpu(dd->work[2].cpu_binding); 4556 } 4557 setmask_err: 4558 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR); 4559 4560 iomap_err: 4561 kfree(dd); 4562 pci_set_drvdata(pdev, NULL); 4563 return rv; 4564 done: 4565 return rv; 4566 } 4567 4568 /* 4569 * Called for each probed device when the device is removed or the 4570 * driver is unloaded. 4571 * 4572 * return value 4573 * None 4574 */ 4575 static void mtip_pci_remove(struct pci_dev *pdev) 4576 { 4577 struct driver_data *dd = pci_get_drvdata(pdev); 4578 unsigned long flags, to; 4579 4580 set_bit(MTIP_DDF_REMOVAL_BIT, &dd->dd_flag); 4581 4582 spin_lock_irqsave(&dev_lock, flags); 4583 list_del_init(&dd->online_list); 4584 list_add(&dd->remove_list, &removing_list); 4585 spin_unlock_irqrestore(&dev_lock, flags); 4586 4587 mtip_check_surprise_removal(pdev); 4588 synchronize_irq(dd->pdev->irq); 4589 4590 /* Spin until workers are done */ 4591 to = jiffies + msecs_to_jiffies(4000); 4592 do { 4593 msleep(20); 4594 } while (atomic_read(&dd->irq_workers_active) != 0 && 4595 time_before(jiffies, to)); 4596 4597 if (!dd->sr) 4598 fsync_bdev(dd->bdev); 4599 4600 if (atomic_read(&dd->irq_workers_active) != 0) { 4601 dev_warn(&dd->pdev->dev, 4602 "Completion workers still active!\n"); 4603 } 4604 4605 blk_set_queue_dying(dd->queue); 4606 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag); 4607 4608 /* Clean up the block layer. */ 4609 mtip_block_remove(dd); 4610 4611 if (dd->isr_workq) { 4612 flush_workqueue(dd->isr_workq); 4613 destroy_workqueue(dd->isr_workq); 4614 drop_cpu(dd->work[0].cpu_binding); 4615 drop_cpu(dd->work[1].cpu_binding); 4616 drop_cpu(dd->work[2].cpu_binding); 4617 } 4618 4619 pci_disable_msi(pdev); 4620 4621 spin_lock_irqsave(&dev_lock, flags); 4622 list_del_init(&dd->remove_list); 4623 spin_unlock_irqrestore(&dev_lock, flags); 4624 4625 kfree(dd); 4626 4627 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR); 4628 pci_set_drvdata(pdev, NULL); 4629 } 4630 4631 /* 4632 * Called for each probed device when the device is suspended. 4633 * 4634 * return value 4635 * 0 Success 4636 * <0 Error 4637 */ 4638 static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg) 4639 { 4640 int rv = 0; 4641 struct driver_data *dd = pci_get_drvdata(pdev); 4642 4643 if (!dd) { 4644 dev_err(&pdev->dev, 4645 "Driver private datastructure is NULL\n"); 4646 return -EFAULT; 4647 } 4648 4649 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag); 4650 4651 /* Disable ports & interrupts then send standby immediate */ 4652 rv = mtip_block_suspend(dd); 4653 if (rv < 0) { 4654 dev_err(&pdev->dev, 4655 "Failed to suspend controller\n"); 4656 return rv; 4657 } 4658 4659 /* 4660 * Save the pci config space to pdev structure & 4661 * disable the device 4662 */ 4663 pci_save_state(pdev); 4664 pci_disable_device(pdev); 4665 4666 /* Move to Low power state*/ 4667 pci_set_power_state(pdev, PCI_D3hot); 4668 4669 return rv; 4670 } 4671 4672 /* 4673 * Called for each probed device when the device is resumed. 4674 * 4675 * return value 4676 * 0 Success 4677 * <0 Error 4678 */ 4679 static int mtip_pci_resume(struct pci_dev *pdev) 4680 { 4681 int rv = 0; 4682 struct driver_data *dd; 4683 4684 dd = pci_get_drvdata(pdev); 4685 if (!dd) { 4686 dev_err(&pdev->dev, 4687 "Driver private datastructure is NULL\n"); 4688 return -EFAULT; 4689 } 4690 4691 /* Move the device to active State */ 4692 pci_set_power_state(pdev, PCI_D0); 4693 4694 /* Restore PCI configuration space */ 4695 pci_restore_state(pdev); 4696 4697 /* Enable the PCI device*/ 4698 rv = pcim_enable_device(pdev); 4699 if (rv < 0) { 4700 dev_err(&pdev->dev, 4701 "Failed to enable card during resume\n"); 4702 goto err; 4703 } 4704 pci_set_master(pdev); 4705 4706 /* 4707 * Calls hbaReset, initPort, & startPort function 4708 * then enables interrupts 4709 */ 4710 rv = mtip_block_resume(dd); 4711 if (rv < 0) 4712 dev_err(&pdev->dev, "Unable to resume\n"); 4713 4714 err: 4715 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag); 4716 4717 return rv; 4718 } 4719 4720 /* 4721 * Shutdown routine 4722 * 4723 * return value 4724 * None 4725 */ 4726 static void mtip_pci_shutdown(struct pci_dev *pdev) 4727 { 4728 struct driver_data *dd = pci_get_drvdata(pdev); 4729 if (dd) 4730 mtip_block_shutdown(dd); 4731 } 4732 4733 /* Table of device ids supported by this driver. */ 4734 static const struct pci_device_id mtip_pci_tbl[] = { 4735 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) }, 4736 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) }, 4737 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) }, 4738 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) }, 4739 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) }, 4740 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) }, 4741 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) }, 4742 { 0 } 4743 }; 4744 4745 /* Structure that describes the PCI driver functions. */ 4746 static struct pci_driver mtip_pci_driver = { 4747 .name = MTIP_DRV_NAME, 4748 .id_table = mtip_pci_tbl, 4749 .probe = mtip_pci_probe, 4750 .remove = mtip_pci_remove, 4751 .suspend = mtip_pci_suspend, 4752 .resume = mtip_pci_resume, 4753 .shutdown = mtip_pci_shutdown, 4754 }; 4755 4756 MODULE_DEVICE_TABLE(pci, mtip_pci_tbl); 4757 4758 /* 4759 * Module initialization function. 4760 * 4761 * Called once when the module is loaded. This function allocates a major 4762 * block device number to the Cyclone devices and registers the PCI layer 4763 * of the driver. 4764 * 4765 * Return value 4766 * 0 on success else error code. 4767 */ 4768 static int __init mtip_init(void) 4769 { 4770 int error; 4771 4772 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n"); 4773 4774 spin_lock_init(&dev_lock); 4775 4776 INIT_LIST_HEAD(&online_list); 4777 INIT_LIST_HEAD(&removing_list); 4778 4779 /* Allocate a major block device number to use with this driver. */ 4780 error = register_blkdev(0, MTIP_DRV_NAME); 4781 if (error <= 0) { 4782 pr_err("Unable to register block device (%d)\n", 4783 error); 4784 return -EBUSY; 4785 } 4786 mtip_major = error; 4787 4788 dfs_parent = debugfs_create_dir("rssd", NULL); 4789 if (IS_ERR_OR_NULL(dfs_parent)) { 4790 pr_warn("Error creating debugfs parent\n"); 4791 dfs_parent = NULL; 4792 } 4793 if (dfs_parent) { 4794 dfs_device_status = debugfs_create_file("device_status", 4795 S_IRUGO, dfs_parent, NULL, 4796 &mtip_device_status_fops); 4797 if (IS_ERR_OR_NULL(dfs_device_status)) { 4798 pr_err("Error creating device_status node\n"); 4799 dfs_device_status = NULL; 4800 } 4801 } 4802 4803 /* Register our PCI operations. */ 4804 error = pci_register_driver(&mtip_pci_driver); 4805 if (error) { 4806 debugfs_remove(dfs_parent); 4807 unregister_blkdev(mtip_major, MTIP_DRV_NAME); 4808 } 4809 4810 return error; 4811 } 4812 4813 /* 4814 * Module de-initialization function. 4815 * 4816 * Called once when the module is unloaded. This function deallocates 4817 * the major block device number allocated by mtip_init() and 4818 * unregisters the PCI layer of the driver. 4819 * 4820 * Return value 4821 * none 4822 */ 4823 static void __exit mtip_exit(void) 4824 { 4825 /* Release the allocated major block device number. */ 4826 unregister_blkdev(mtip_major, MTIP_DRV_NAME); 4827 4828 /* Unregister the PCI driver. */ 4829 pci_unregister_driver(&mtip_pci_driver); 4830 4831 debugfs_remove_recursive(dfs_parent); 4832 } 4833 4834 MODULE_AUTHOR("Micron Technology, Inc"); 4835 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver"); 4836 MODULE_LICENSE("GPL"); 4837 MODULE_VERSION(MTIP_DRV_VERSION); 4838 4839 module_init(mtip_init); 4840 module_exit(mtip_exit); 4841