1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the Micron P320 SSD
4 * Copyright (C) 2011 Micron Technology, Inc.
6 * Portions of this code were derived from works subjected to the
8 * Copyright (C) 2009 Integrated Device Technology, Inc.
11 #include <linux/pci.h>
12 #include <linux/interrupt.h>
13 #include <linux/ata.h>
14 #include <linux/delay.h>
15 #include <linux/hdreg.h>
16 #include <linux/uaccess.h>
17 #include <linux/random.h>
18 #include <linux/smp.h>
19 #include <linux/compat.h>
21 #include <linux/module.h>
22 #include <linux/blkdev.h>
23 #include <linux/blk-mq.h>
24 #include <linux/bio.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/idr.h>
27 #include <linux/kthread.h>
28 #include <../drivers/ata/ahci.h>
29 #include <linux/export.h>
30 #include <linux/debugfs.h>
31 #include <linux/prefetch.h>
32 #include <linux/numa.h>
35 #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
37 /* DMA region containing RX Fis, Identify, RLE10, and SMART buffers */
38 #define AHCI_RX_FIS_SZ 0x100
39 #define AHCI_RX_FIS_OFFSET 0x0
40 #define AHCI_IDFY_SZ ATA_SECT_SIZE
41 #define AHCI_IDFY_OFFSET 0x400
42 #define AHCI_SECTBUF_SZ ATA_SECT_SIZE
43 #define AHCI_SECTBUF_OFFSET 0x800
44 #define AHCI_SMARTBUF_SZ ATA_SECT_SIZE
45 #define AHCI_SMARTBUF_OFFSET 0xC00
46 /* 0x100 + 0x200 + 0x200 + 0x200 is smaller than 4k but we pad it out */
47 #define BLOCK_DMA_ALLOC_SZ 4096
49 /* DMA region containing command table (should be 8192 bytes) */
50 #define AHCI_CMD_SLOT_SZ sizeof(struct mtip_cmd_hdr)
51 #define AHCI_CMD_TBL_SZ (MTIP_MAX_COMMAND_SLOTS * AHCI_CMD_SLOT_SZ)
52 #define AHCI_CMD_TBL_OFFSET 0x0
54 /* DMA region per command (contains header and SGL) */
55 #define AHCI_CMD_TBL_HDR_SZ 0x80
56 #define AHCI_CMD_TBL_HDR_OFFSET 0x0
57 #define AHCI_CMD_TBL_SGL_SZ (MTIP_MAX_SG * sizeof(struct mtip_cmd_sg))
58 #define AHCI_CMD_TBL_SGL_OFFSET AHCI_CMD_TBL_HDR_SZ
59 #define CMD_DMA_ALLOC_SZ (AHCI_CMD_TBL_SGL_SZ + AHCI_CMD_TBL_HDR_SZ)
62 #define HOST_CAP_NZDMA (1 << 19)
63 #define HOST_HSORG 0xFC
64 #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
65 #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
66 #define HSORG_HWREV 0xFF00
67 #define HSORG_STYLE 0x8
68 #define HSORG_SLOTGROUPS 0x7
70 #define PORT_COMMAND_ISSUE 0x38
71 #define PORT_SDBV 0x7C
73 #define PORT_OFFSET 0x100
74 #define PORT_MEM_SIZE 0x80
76 #define PORT_IRQ_ERR \
77 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
78 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
79 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
81 #define PORT_IRQ_LEGACY \
82 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
83 #define PORT_IRQ_HANDLED \
84 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
85 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
86 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
87 #define DEF_PORT_IRQ \
88 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
91 #define MTIP_PRODUCT_UNKNOWN 0x00
92 #define MTIP_PRODUCT_ASICFPGA 0x11
94 /* Device instance number, incremented each time a device is probed. */
97 static LIST_HEAD(online_list);
98 static LIST_HEAD(removing_list);
99 static DEFINE_SPINLOCK(dev_lock);
102 * Global variable used to hold the major block device number
103 * allocated in mtip_init().
105 static int mtip_major;
106 static struct dentry *dfs_parent;
107 static struct dentry *dfs_device_status;
109 static u32 cpu_use[NR_CPUS];
111 static DEFINE_IDA(rssd_index_ida);
113 static int mtip_block_initialize(struct driver_data *dd);
116 struct mtip_compat_ide_task_request_s {
119 ide_reg_valid_t out_flags;
120 ide_reg_valid_t in_flags;
123 compat_ulong_t out_size;
124 compat_ulong_t in_size;
129 * This function check_for_surprise_removal is called
130 * while card is removed from the system and it will
131 * read the vendor id from the configuration space
133 * @pdev Pointer to the pci_dev structure.
136 * true if device removed, else false
138 static bool mtip_check_surprise_removal(struct driver_data *dd)
145 /* Read the vendorID from the configuration space */
146 pci_read_config_word(dd->pdev, 0x00, &vendor_id);
147 if (vendor_id == 0xFFFF) {
150 blk_queue_flag_set(QUEUE_FLAG_DEAD, dd->queue);
152 dev_warn(&dd->pdev->dev,
153 "%s: dd->queue is NULL\n", __func__);
154 return true; /* device removed */
157 return false; /* device present */
160 static struct mtip_cmd *mtip_cmd_from_tag(struct driver_data *dd,
163 return blk_mq_rq_to_pdu(blk_mq_tag_to_rq(dd->tags.tags[0], tag));
167 * Reset the HBA (without sleeping)
169 * @dd Pointer to the driver data structure.
172 * 0 The reset was successful.
173 * -1 The HBA Reset bit did not clear.
175 static int mtip_hba_reset(struct driver_data *dd)
177 unsigned long timeout;
179 /* Set the reset bit */
180 writel(HOST_RESET, dd->mmio + HOST_CTL);
183 readl(dd->mmio + HOST_CTL);
186 * Spin for up to 10 seconds waiting for reset acknowledgement. Spec
187 * is 1 sec but in LUN failure conditions, up to 10 secs are required
189 timeout = jiffies + msecs_to_jiffies(10000);
192 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
195 } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
196 && time_before(jiffies, timeout));
198 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
205 * Issue a command to the hardware.
207 * Set the appropriate bit in the s_active and Command Issue hardware
208 * registers, causing hardware command processing to begin.
210 * @port Pointer to the port structure.
211 * @tag The tag of the command to be issued.
216 static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
218 int group = tag >> 5;
220 /* guard SACT and CI registers */
221 spin_lock(&port->cmd_issue_lock[group]);
222 writel((1 << MTIP_TAG_BIT(tag)),
223 port->s_active[MTIP_TAG_INDEX(tag)]);
224 writel((1 << MTIP_TAG_BIT(tag)),
225 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
226 spin_unlock(&port->cmd_issue_lock[group]);
230 * Enable/disable the reception of FIS
232 * @port Pointer to the port data structure
233 * @enable 1 to enable, 0 to disable
236 * Previous state: 1 enabled, 0 disabled
238 static int mtip_enable_fis(struct mtip_port *port, int enable)
242 /* enable FIS reception */
243 tmp = readl(port->mmio + PORT_CMD);
245 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
247 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
250 readl(port->mmio + PORT_CMD);
252 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
256 * Enable/disable the DMA engine
258 * @port Pointer to the port data structure
259 * @enable 1 to enable, 0 to disable
262 * Previous state: 1 enabled, 0 disabled.
264 static int mtip_enable_engine(struct mtip_port *port, int enable)
268 /* enable FIS reception */
269 tmp = readl(port->mmio + PORT_CMD);
271 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
273 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
275 readl(port->mmio + PORT_CMD);
276 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
280 * Enables the port DMA engine and FIS reception.
285 static inline void mtip_start_port(struct mtip_port *port)
287 /* Enable FIS reception */
288 mtip_enable_fis(port, 1);
290 /* Enable the DMA engine */
291 mtip_enable_engine(port, 1);
295 * Deinitialize a port by disabling port interrupts, the DMA engine,
298 * @port Pointer to the port structure
303 static inline void mtip_deinit_port(struct mtip_port *port)
305 /* Disable interrupts on this port */
306 writel(0, port->mmio + PORT_IRQ_MASK);
308 /* Disable the DMA engine */
309 mtip_enable_engine(port, 0);
311 /* Disable FIS reception */
312 mtip_enable_fis(port, 0);
318 * This function deinitializes the port by calling mtip_deinit_port() and
319 * then initializes it by setting the command header and RX FIS addresses,
320 * clearing the SError register and any pending port interrupts before
321 * re-enabling the default set of port interrupts.
323 * @port Pointer to the port structure.
328 static void mtip_init_port(struct mtip_port *port)
331 mtip_deinit_port(port);
333 /* Program the command list base and FIS base addresses */
334 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
335 writel((port->command_list_dma >> 16) >> 16,
336 port->mmio + PORT_LST_ADDR_HI);
337 writel((port->rxfis_dma >> 16) >> 16,
338 port->mmio + PORT_FIS_ADDR_HI);
339 set_bit(MTIP_PF_HOST_CAP_64, &port->flags);
342 writel(port->command_list_dma & 0xFFFFFFFF,
343 port->mmio + PORT_LST_ADDR);
344 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
347 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
349 /* reset the completed registers.*/
350 for (i = 0; i < port->dd->slot_groups; i++)
351 writel(0xFFFFFFFF, port->completed[i]);
353 /* Clear any pending interrupts for this port */
354 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
356 /* Clear any pending interrupts on the HBA. */
357 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
358 port->dd->mmio + HOST_IRQ_STAT);
360 /* Enable port interrupts */
361 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
367 * @port Pointer to the port data structure.
372 static void mtip_restart_port(struct mtip_port *port)
374 unsigned long timeout;
376 /* Disable the DMA engine */
377 mtip_enable_engine(port, 0);
379 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
380 timeout = jiffies + msecs_to_jiffies(500);
381 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
382 && time_before(jiffies, timeout))
385 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
389 * Chip quirk: escalate to hba reset if
390 * PxCMD.CR not clear after 500 ms
392 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
393 dev_warn(&port->dd->pdev->dev,
394 "PxCMD.CR not clear, escalating reset\n");
396 if (mtip_hba_reset(port->dd))
397 dev_err(&port->dd->pdev->dev,
398 "HBA reset escalation failed.\n");
400 /* 30 ms delay before com reset to quiesce chip */
404 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
407 writel(readl(port->mmio + PORT_SCR_CTL) |
408 1, port->mmio + PORT_SCR_CTL);
409 readl(port->mmio + PORT_SCR_CTL);
411 /* Wait 1 ms to quiesce chip function */
412 timeout = jiffies + msecs_to_jiffies(1);
413 while (time_before(jiffies, timeout))
416 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
419 /* Clear PxSCTL.DET */
420 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
421 port->mmio + PORT_SCR_CTL);
422 readl(port->mmio + PORT_SCR_CTL);
424 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
425 timeout = jiffies + msecs_to_jiffies(500);
426 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
427 && time_before(jiffies, timeout))
430 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
433 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
434 dev_warn(&port->dd->pdev->dev,
435 "COM reset failed\n");
437 mtip_init_port(port);
438 mtip_start_port(port);
442 static int mtip_device_reset(struct driver_data *dd)
446 if (mtip_check_surprise_removal(dd))
449 if (mtip_hba_reset(dd) < 0)
453 mtip_init_port(dd->port);
454 mtip_start_port(dd->port);
456 /* Enable interrupts on the HBA. */
457 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
458 dd->mmio + HOST_CTL);
463 * Helper function for tag logging
465 static void print_tags(struct driver_data *dd,
467 unsigned long *tagbits,
470 unsigned char tagmap[128];
471 int group, tagmap_len = 0;
473 memset(tagmap, 0, sizeof(tagmap));
474 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
475 tagmap_len += sprintf(tagmap + tagmap_len, "%016lX ",
477 dev_warn(&dd->pdev->dev,
478 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
481 static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
482 dma_addr_t buffer_dma, unsigned int sectors);
483 static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
484 struct smart_attr *attrib);
486 static void mtip_complete_command(struct mtip_cmd *cmd, blk_status_t status)
488 struct request *req = blk_mq_rq_from_pdu(cmd);
490 cmd->status = status;
491 if (likely(!blk_should_fake_timeout(req->q)))
492 blk_mq_complete_request(req);
498 * @dd Pointer to the DRIVER_DATA structure.
503 static void mtip_handle_tfe(struct driver_data *dd)
505 int group, tag, bit, reissue, rv;
506 struct mtip_port *port;
507 struct mtip_cmd *cmd;
509 struct host_to_dev_fis *fis;
510 unsigned long tagaccum[SLOTBITS_IN_LONGS];
511 unsigned int cmd_cnt = 0;
513 char *fail_reason = NULL;
514 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
516 dev_warn(&dd->pdev->dev, "Taskfile error\n");
520 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
521 cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
522 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
523 mtip_complete_command(cmd, BLK_STS_IOERR);
527 /* clear the tag accumulator */
528 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
530 /* Loop through all the groups */
531 for (group = 0; group < dd->slot_groups; group++) {
532 completed = readl(port->completed[group]);
534 dev_warn(&dd->pdev->dev, "g=%u, comp=%x\n", group, completed);
536 /* clear completed status register in the hardware.*/
537 writel(completed, port->completed[group]);
539 /* Process successfully completed commands */
540 for (bit = 0; bit < 32 && completed; bit++) {
541 if (!(completed & (1<<bit)))
543 tag = (group << 5) + bit;
545 /* Skip the internal command slot */
546 if (tag == MTIP_TAG_INTERNAL)
549 cmd = mtip_cmd_from_tag(dd, tag);
550 mtip_complete_command(cmd, 0);
551 set_bit(tag, tagaccum);
556 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
558 /* Restart the port */
560 mtip_restart_port(port);
562 /* Trying to determine the cause of the error */
563 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
565 dd->port->log_buf_dma, 1);
567 dev_warn(&dd->pdev->dev,
568 "Error in READ LOG EXT (10h) command\n");
569 /* non-critical error, don't fail the load */
571 buf = (unsigned char *)dd->port->log_buf;
572 if (buf[259] & 0x1) {
573 dev_info(&dd->pdev->dev,
574 "Write protect bit is set.\n");
575 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
576 fail_all_ncq_write = 1;
577 fail_reason = "write protect";
579 if (buf[288] == 0xF7) {
580 dev_info(&dd->pdev->dev,
581 "Exceeded Tmax, drive in thermal shutdown.\n");
582 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
583 fail_all_ncq_cmds = 1;
584 fail_reason = "thermal shutdown";
586 if (buf[288] == 0xBF) {
587 set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag);
588 dev_info(&dd->pdev->dev,
589 "Drive indicates rebuild has failed. Secure erase required.\n");
590 fail_all_ncq_cmds = 1;
591 fail_reason = "rebuild failed";
595 /* clear the tag accumulator */
596 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
598 /* Loop through all the groups */
599 for (group = 0; group < dd->slot_groups; group++) {
600 for (bit = 0; bit < 32; bit++) {
602 tag = (group << 5) + bit;
603 cmd = mtip_cmd_from_tag(dd, tag);
605 fis = (struct host_to_dev_fis *)cmd->command;
607 /* Should re-issue? */
608 if (tag == MTIP_TAG_INTERNAL ||
609 fis->command == ATA_CMD_SET_FEATURES)
612 if (fail_all_ncq_cmds ||
613 (fail_all_ncq_write &&
614 fis->command == ATA_CMD_FPDMA_WRITE)) {
615 dev_warn(&dd->pdev->dev,
616 " Fail: %s w/tag %d [%s].\n",
617 fis->command == ATA_CMD_FPDMA_WRITE ?
620 fail_reason != NULL ?
621 fail_reason : "unknown");
622 mtip_complete_command(cmd, BLK_STS_MEDIUM);
628 * First check if this command has
629 * exceeded its retries.
631 if (reissue && (cmd->retries-- > 0)) {
633 set_bit(tag, tagaccum);
635 /* Re-issue the command. */
636 mtip_issue_ncq_command(port, tag);
641 /* Retire a command that will not be reissued */
642 dev_warn(&port->dd->pdev->dev,
643 "retiring tag %d\n", tag);
645 mtip_complete_command(cmd, BLK_STS_IOERR);
648 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
652 * Handle a set device bits interrupt
654 static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
657 struct driver_data *dd = port->dd;
659 struct mtip_cmd *command;
662 WARN_ON_ONCE(!completed);
665 /* clear completed status register in the hardware.*/
666 writel(completed, port->completed[group]);
668 /* Process completed commands. */
669 for (bit = 0; (bit < 32) && completed; bit++) {
670 if (completed & 0x01) {
671 tag = (group << 5) | bit;
673 /* skip internal command slot. */
674 if (unlikely(tag == MTIP_TAG_INTERNAL))
677 command = mtip_cmd_from_tag(dd, tag);
678 mtip_complete_command(command, 0);
683 /* If last, re-enable interrupts */
684 if (atomic_dec_return(&dd->irq_workers_active) == 0)
685 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
689 * Process legacy pio and d2h interrupts
691 static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
693 struct mtip_port *port = dd->port;
694 struct mtip_cmd *cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
696 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) && cmd) {
697 int group = MTIP_TAG_INDEX(MTIP_TAG_INTERNAL);
698 int status = readl(port->cmd_issue[group]);
700 if (!(status & (1 << MTIP_TAG_BIT(MTIP_TAG_INTERNAL))))
701 mtip_complete_command(cmd, 0);
706 * Demux and handle errors
708 static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
710 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
711 dev_warn(&dd->pdev->dev,
712 "Clearing PxSERR.DIAG.x\n");
713 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
716 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
717 dev_warn(&dd->pdev->dev,
718 "Clearing PxSERR.DIAG.n\n");
719 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
722 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
723 dev_warn(&dd->pdev->dev,
724 "Port stat errors %x unhandled\n",
725 (port_stat & ~PORT_IRQ_HANDLED));
726 if (mtip_check_surprise_removal(dd))
729 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR))) {
730 set_bit(MTIP_PF_EH_ACTIVE_BIT, &dd->port->flags);
731 wake_up_interruptible(&dd->port->svc_wait);
735 static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
737 struct driver_data *dd = (struct driver_data *) data;
738 struct mtip_port *port = dd->port;
739 u32 hba_stat, port_stat;
741 int do_irq_enable = 1, i, workers;
742 struct mtip_work *twork;
744 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
748 /* Acknowledge the interrupt status on the port.*/
749 port_stat = readl(port->mmio + PORT_IRQ_STAT);
750 if (unlikely(port_stat == 0xFFFFFFFF)) {
751 mtip_check_surprise_removal(dd);
754 writel(port_stat, port->mmio + PORT_IRQ_STAT);
756 /* Demux port status */
757 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
759 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
761 /* Start at 1: group zero is always local? */
762 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
764 twork = &dd->work[i];
765 twork->completed = readl(port->completed[i]);
766 if (twork->completed)
770 atomic_set(&dd->irq_workers_active, workers);
772 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
773 twork = &dd->work[i];
774 if (twork->completed)
781 if (likely(dd->work[0].completed))
782 mtip_workq_sdbfx(port, 0,
783 dd->work[0].completed);
787 * Chip quirk: SDB interrupt but nothing
794 if (unlikely(port_stat & PORT_IRQ_ERR)) {
795 if (unlikely(mtip_check_surprise_removal(dd))) {
796 /* don't proceed further */
799 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
803 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
806 if (unlikely(port_stat & PORT_IRQ_LEGACY))
807 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
810 /* acknowledge interrupt */
811 if (unlikely(do_irq_enable))
812 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
818 * HBA interrupt subroutine.
821 * @instance Pointer to the driver data structure.
824 * IRQ_HANDLED A HBA interrupt was pending and handled.
825 * IRQ_NONE This interrupt was not for the HBA.
827 static irqreturn_t mtip_irq_handler(int irq, void *instance)
829 struct driver_data *dd = instance;
831 return mtip_handle_irq(dd);
834 static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
836 writel(1 << MTIP_TAG_BIT(tag), port->cmd_issue[MTIP_TAG_INDEX(tag)]);
839 static bool mtip_pause_ncq(struct mtip_port *port,
840 struct host_to_dev_fis *fis)
842 unsigned long task_file_data;
844 task_file_data = readl(port->mmio+PORT_TFDATA);
845 if ((task_file_data & 1))
848 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
849 port->ic_pause_timer = jiffies;
851 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
852 (fis->features == 0x03)) {
853 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
854 port->ic_pause_timer = jiffies;
856 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
857 ((fis->command == 0xFC) &&
858 (fis->features == 0x27 || fis->features == 0x72 ||
859 fis->features == 0x62 || fis->features == 0x26))) {
860 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
861 clear_bit(MTIP_DDF_REBUILD_FAILED_BIT, &port->dd->dd_flag);
862 /* Com reset after secure erase or lowlevel format */
863 mtip_restart_port(port);
864 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
871 static bool mtip_commands_active(struct mtip_port *port)
877 * Ignore s_active bit 0 of array element 0.
878 * This bit will always be set
880 active = readl(port->s_active[0]) & 0xFFFFFFFE;
881 for (n = 1; n < port->dd->slot_groups; n++)
882 active |= readl(port->s_active[n]);
888 * Wait for port to quiesce
890 * @port Pointer to port data structure
891 * @timeout Max duration to wait (ms)
895 * -EBUSY Commands still active
897 static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
902 blk_mq_quiesce_queue(port->dd->queue);
904 to = jiffies + msecs_to_jiffies(timeout);
906 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
907 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
909 continue; /* svc thd is actively issuing commands */
914 if (mtip_check_surprise_removal(port->dd))
917 active = mtip_commands_active(port);
920 } while (time_before(jiffies, to));
922 blk_mq_unquiesce_queue(port->dd->queue);
923 return active ? -EBUSY : 0;
925 blk_mq_unquiesce_queue(port->dd->queue);
929 struct mtip_int_cmd {
937 * Execute an internal command and wait for the completion.
939 * @port Pointer to the port data structure.
940 * @fis Pointer to the FIS that describes the command.
941 * @fis_len Length in WORDS of the FIS.
942 * @buffer DMA accessible for command data.
943 * @buf_len Length, in bytes, of the data buffer.
944 * @opts Command header options, excluding the FIS length
945 * and the number of PRD entries.
946 * @timeout Time in ms to wait for the command to complete.
949 * 0 Command completed successfully.
950 * -EFAULT The buffer address is not correctly aligned.
951 * -EBUSY Internal command or other IO in progress.
952 * -EAGAIN Time out waiting for command to complete.
954 static int mtip_exec_internal_command(struct mtip_port *port,
955 struct host_to_dev_fis *fis,
960 unsigned long timeout)
962 struct mtip_cmd *int_cmd;
963 struct driver_data *dd = port->dd;
965 struct mtip_int_cmd icmd = {
973 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
974 if (buffer & 0x00000007) {
975 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
979 if (mtip_check_surprise_removal(dd))
982 rq = blk_mq_alloc_request(dd->queue, REQ_OP_DRV_IN, BLK_MQ_REQ_RESERVED);
984 dbg_printk(MTIP_DRV_NAME "Unable to allocate tag for PIO cmd\n");
988 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
990 if (fis->command == ATA_CMD_SEC_ERASE_PREP)
991 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
993 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
995 if (fis->command != ATA_CMD_STANDBYNOW1) {
996 /* wait for io to complete if non atomic */
997 if (mtip_quiesce_io(port, MTIP_QUIESCE_IO_TIMEOUT_MS) < 0) {
998 dev_warn(&dd->pdev->dev, "Failed to quiesce IO\n");
999 blk_mq_free_request(rq);
1000 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1001 wake_up_interruptible(&port->svc_wait);
1006 /* Copy the command to the command table */
1007 int_cmd = blk_mq_rq_to_pdu(rq);
1008 int_cmd->icmd = &icmd;
1009 memcpy(int_cmd->command, fis, fis_len*4);
1011 rq->timeout = timeout;
1013 /* insert request and run queue */
1014 blk_execute_rq(rq, true);
1016 if (int_cmd->status) {
1017 dev_err(&dd->pdev->dev, "Internal command [%02X] failed %d\n",
1018 fis->command, int_cmd->status);
1021 if (mtip_check_surprise_removal(dd) ||
1022 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1024 dev_err(&dd->pdev->dev,
1025 "Internal command [%02X] wait returned due to SR\n",
1030 mtip_device_reset(dd); /* recover from timeout issue */
1035 if (readl(port->cmd_issue[MTIP_TAG_INDEX(MTIP_TAG_INTERNAL)])
1036 & (1 << MTIP_TAG_BIT(MTIP_TAG_INTERNAL))) {
1038 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1039 mtip_device_reset(dd);
1044 /* Clear the allocated and active bits for the internal command. */
1045 blk_mq_free_request(rq);
1046 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1047 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1051 wake_up_interruptible(&port->svc_wait);
1057 * Byte-swap ATA ID strings.
1059 * ATA identify data contains strings in byte-swapped 16-bit words.
1060 * They must be swapped (on all architectures) to be usable as C strings.
1061 * This function swaps bytes in-place.
1063 * @buf The buffer location of the string
1064 * @len The number of bytes to swap
1069 static inline void ata_swap_string(u16 *buf, unsigned int len)
1072 for (i = 0; i < (len/2); i++)
1073 be16_to_cpus(&buf[i]);
1076 static void mtip_set_timeout(struct driver_data *dd,
1077 struct host_to_dev_fis *fis,
1078 unsigned int *timeout, u8 erasemode)
1080 switch (fis->command) {
1081 case ATA_CMD_DOWNLOAD_MICRO:
1082 *timeout = 120000; /* 2 minutes */
1084 case ATA_CMD_SEC_ERASE_UNIT:
1087 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
1089 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
1091 case ATA_CMD_STANDBYNOW1:
1092 *timeout = 120000; /* 2 minutes */
1096 *timeout = 60000; /* 60 seconds */
1099 *timeout = 15000; /* 15 seconds */
1102 *timeout = MTIP_IOCTL_CMD_TIMEOUT_MS;
1108 * Request the device identity information.
1110 * If a user space buffer is not specified, i.e. is NULL, the
1111 * identify information is still read from the drive and placed
1112 * into the identify data buffer (@e port->identify) in the
1113 * port data structure.
1114 * When the identify buffer contains valid identify information @e
1115 * port->identify_valid is non-zero.
1117 * @port Pointer to the port structure.
1118 * @user_buffer A user space buffer where the identify data should be
1122 * 0 Command completed successfully.
1123 * -EFAULT An error occurred while coping data to the user buffer.
1124 * -1 Command failed.
1126 static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1129 struct host_to_dev_fis fis;
1131 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
1134 /* Build the FIS. */
1135 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1138 fis.command = ATA_CMD_ID_ATA;
1140 /* Set the identify information as invalid. */
1141 port->identify_valid = 0;
1143 /* Clear the identify information. */
1144 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1146 /* Execute the command. */
1147 if (mtip_exec_internal_command(port,
1151 sizeof(u16) * ATA_ID_WORDS,
1153 MTIP_INT_CMD_TIMEOUT_MS)
1160 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1161 * perform field-sensitive swapping on the string fields.
1162 * See the kernel use of ata_id_string() for proof of this.
1164 #ifdef __LITTLE_ENDIAN
1165 ata_swap_string(port->identify + 27, 40); /* model string*/
1166 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1167 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1171 for (i = 0; i < ATA_ID_WORDS; i++)
1172 port->identify[i] = le16_to_cpu(port->identify[i]);
1176 /* Check security locked state */
1177 if (port->identify[128] & 0x4)
1178 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1180 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1182 /* Set the identify buffer as valid. */
1183 port->identify_valid = 1;
1189 ATA_ID_WORDS * sizeof(u16))) {
1200 * Issue a standby immediate command to the device.
1202 * @port Pointer to the port structure.
1205 * 0 Command was executed successfully.
1206 * -1 An error occurred while executing the command.
1208 static int mtip_standby_immediate(struct mtip_port *port)
1211 struct host_to_dev_fis fis;
1212 unsigned long __maybe_unused start;
1213 unsigned int timeout;
1215 /* Build the FIS. */
1216 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1219 fis.command = ATA_CMD_STANDBYNOW1;
1221 mtip_set_timeout(port->dd, &fis, &timeout, 0);
1224 rv = mtip_exec_internal_command(port,
1231 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1232 jiffies_to_msecs(jiffies - start));
1234 dev_warn(&port->dd->pdev->dev,
1235 "STANDBY IMMEDIATE command failed.\n");
1241 * Issue a READ LOG EXT command to the device.
1243 * @port pointer to the port structure.
1244 * @page page number to fetch
1245 * @buffer pointer to buffer
1246 * @buffer_dma dma address corresponding to @buffer
1247 * @sectors page length to fetch, in sectors
1250 * @rv return value from mtip_exec_internal_command()
1252 static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1253 dma_addr_t buffer_dma, unsigned int sectors)
1255 struct host_to_dev_fis fis;
1257 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1260 fis.command = ATA_CMD_READ_LOG_EXT;
1261 fis.sect_count = sectors & 0xFF;
1262 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1265 fis.device = ATA_DEVICE_OBS;
1267 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1269 return mtip_exec_internal_command(port,
1273 sectors * ATA_SECT_SIZE,
1275 MTIP_INT_CMD_TIMEOUT_MS);
1279 * Issue a SMART READ DATA command to the device.
1281 * @port pointer to the port structure.
1282 * @buffer pointer to buffer
1283 * @buffer_dma dma address corresponding to @buffer
1286 * @rv return value from mtip_exec_internal_command()
1288 static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1289 dma_addr_t buffer_dma)
1291 struct host_to_dev_fis fis;
1293 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1296 fis.command = ATA_CMD_SMART;
1297 fis.features = 0xD0;
1301 fis.device = ATA_DEVICE_OBS;
1303 return mtip_exec_internal_command(port,
1313 * Get the value of a smart attribute
1315 * @port pointer to the port structure
1316 * @id attribute number
1317 * @attrib pointer to return attrib information corresponding to @id
1320 * -EINVAL NULL buffer passed or unsupported attribute @id.
1321 * -EPERM Identify data not valid, SMART not supported or not enabled
1323 static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1324 struct smart_attr *attrib)
1327 struct smart_attr *pattr;
1332 if (!port->identify_valid) {
1333 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1336 if (!(port->identify[82] & 0x1)) {
1337 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1340 if (!(port->identify[85] & 0x1)) {
1341 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1345 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1346 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1348 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1352 pattr = (struct smart_attr *)(port->smart_buf + 2);
1353 for (i = 0; i < 29; i++, pattr++)
1354 if (pattr->attr_id == id) {
1355 memcpy(attrib, pattr, sizeof(struct smart_attr));
1360 dev_warn(&port->dd->pdev->dev,
1361 "Query for invalid SMART attribute ID\n");
1369 * Get the drive capacity.
1371 * @dd Pointer to the device data structure.
1372 * @sectors Pointer to the variable that will receive the sector count.
1375 * 1 Capacity was returned successfully.
1376 * 0 The identify information is invalid.
1378 static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
1380 struct mtip_port *port = dd->port;
1381 u64 total, raw0, raw1, raw2, raw3;
1382 raw0 = port->identify[100];
1383 raw1 = port->identify[101];
1384 raw2 = port->identify[102];
1385 raw3 = port->identify[103];
1386 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1388 return (bool) !!port->identify_valid;
1392 * Display the identify command data.
1394 * @port Pointer to the port data structure.
1399 static void mtip_dump_identify(struct mtip_port *port)
1402 unsigned short revid;
1405 if (!port->identify_valid)
1408 strlcpy(cbuf, (char *)(port->identify+10), 21);
1409 dev_info(&port->dd->pdev->dev,
1410 "Serial No.: %s\n", cbuf);
1412 strlcpy(cbuf, (char *)(port->identify+23), 9);
1413 dev_info(&port->dd->pdev->dev,
1414 "Firmware Ver.: %s\n", cbuf);
1416 strlcpy(cbuf, (char *)(port->identify+27), 41);
1417 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1419 dev_info(&port->dd->pdev->dev, "Security: %04x %s\n",
1420 port->identify[128],
1421 port->identify[128] & 0x4 ? "(LOCKED)" : "");
1423 if (mtip_hw_get_capacity(port->dd, §ors))
1424 dev_info(&port->dd->pdev->dev,
1425 "Capacity: %llu sectors (%llu MB)\n",
1427 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1429 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
1430 switch (revid & 0xFF) {
1432 strlcpy(cbuf, "A0", 3);
1435 strlcpy(cbuf, "A2", 3);
1438 strlcpy(cbuf, "?", 2);
1441 dev_info(&port->dd->pdev->dev,
1442 "Card Type: %s\n", cbuf);
1446 * Map the commands scatter list into the command table.
1448 * @command Pointer to the command.
1449 * @nents Number of scatter list entries.
1454 static inline void fill_command_sg(struct driver_data *dd,
1455 struct mtip_cmd *command,
1459 unsigned int dma_len;
1460 struct mtip_cmd_sg *command_sg;
1461 struct scatterlist *sg;
1463 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1465 for_each_sg(command->sg, sg, nents, n) {
1466 dma_len = sg_dma_len(sg);
1467 if (dma_len > 0x400000)
1468 dev_err(&dd->pdev->dev,
1469 "DMA segment length truncated\n");
1470 command_sg->info = cpu_to_le32((dma_len-1) & 0x3FFFFF);
1471 command_sg->dba = cpu_to_le32(sg_dma_address(sg));
1472 command_sg->dba_upper =
1473 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
1479 * @brief Execute a drive command.
1481 * return value 0 The command completed successfully.
1482 * return value -1 An error occurred while executing the command.
1484 static int exec_drive_task(struct mtip_port *port, u8 *command)
1486 struct host_to_dev_fis fis;
1487 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1490 /* Build the FIS. */
1491 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1494 fis.command = command[0];
1495 fis.features = command[1];
1496 fis.sect_count = command[2];
1497 fis.sector = command[3];
1498 fis.cyl_low = command[4];
1499 fis.cyl_hi = command[5];
1500 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1502 mtip_set_timeout(port->dd, &fis, &to, 0);
1504 dbg_printk(MTIP_DRV_NAME " %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
1514 /* Execute the command. */
1515 if (mtip_exec_internal_command(port,
1525 command[0] = reply->command; /* Status*/
1526 command[1] = reply->features; /* Error*/
1527 command[4] = reply->cyl_low;
1528 command[5] = reply->cyl_hi;
1530 dbg_printk(MTIP_DRV_NAME " %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
1541 * @brief Execute a drive command.
1543 * @param port Pointer to the port data structure.
1544 * @param command Pointer to the user specified command parameters.
1545 * @param user_buffer Pointer to the user space buffer where read sector
1546 * data should be copied.
1548 * return value 0 The command completed successfully.
1549 * return value -EFAULT An error occurred while copying the completion
1550 * data to the user space buffer.
1551 * return value -1 An error occurred while executing the command.
1553 static int exec_drive_command(struct mtip_port *port, u8 *command,
1554 void __user *user_buffer)
1556 struct host_to_dev_fis fis;
1557 struct host_to_dev_fis *reply;
1559 dma_addr_t dma_addr = 0;
1560 int rv = 0, xfer_sz = command[3];
1567 buf = dma_alloc_coherent(&port->dd->pdev->dev,
1568 ATA_SECT_SIZE * xfer_sz,
1572 dev_err(&port->dd->pdev->dev,
1573 "Memory allocation failed (%d bytes)\n",
1574 ATA_SECT_SIZE * xfer_sz);
1579 /* Build the FIS. */
1580 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1583 fis.command = command[0];
1584 fis.features = command[2];
1585 fis.sect_count = command[3];
1586 if (fis.command == ATA_CMD_SMART) {
1587 fis.sector = command[1];
1592 mtip_set_timeout(port->dd, &fis, &to, 0);
1595 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1597 reply = (port->rxfis + RX_FIS_D2H_REG);
1599 dbg_printk(MTIP_DRV_NAME
1600 " %s: User Command: cmd %x, sect %x, "
1601 "feat %x, sectcnt %x\n",
1608 /* Execute the command. */
1609 if (mtip_exec_internal_command(port,
1612 (xfer_sz ? dma_addr : 0),
1613 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
1618 goto exit_drive_command;
1621 /* Collect the completion status. */
1622 command[0] = reply->command; /* Status*/
1623 command[1] = reply->features; /* Error*/
1624 command[2] = reply->sect_count;
1626 dbg_printk(MTIP_DRV_NAME
1627 " %s: Completion Status: stat %x, "
1628 "err %x, nsect %x\n",
1635 if (copy_to_user(user_buffer,
1637 ATA_SECT_SIZE * command[3])) {
1639 goto exit_drive_command;
1644 dma_free_coherent(&port->dd->pdev->dev,
1645 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1650 * Indicates whether a command has a single sector payload.
1652 * @command passed to the device to perform the certain event.
1653 * @features passed to the device to perform the certain event.
1656 * 1 command is one that always has a single sector payload,
1657 * regardless of the value in the Sector Count field.
1661 static unsigned int implicit_sector(unsigned char command,
1662 unsigned char features)
1664 unsigned int rv = 0;
1666 /* list of commands that have an implicit sector count of 1 */
1668 case ATA_CMD_SEC_SET_PASS:
1669 case ATA_CMD_SEC_UNLOCK:
1670 case ATA_CMD_SEC_ERASE_PREP:
1671 case ATA_CMD_SEC_ERASE_UNIT:
1672 case ATA_CMD_SEC_FREEZE_LOCK:
1673 case ATA_CMD_SEC_DISABLE_PASS:
1674 case ATA_CMD_PMP_READ:
1675 case ATA_CMD_PMP_WRITE:
1678 case ATA_CMD_SET_MAX:
1679 if (features == ATA_SET_MAX_UNLOCK)
1683 if ((features == ATA_SMART_READ_VALUES) ||
1684 (features == ATA_SMART_READ_THRESHOLDS))
1687 case ATA_CMD_CONF_OVERLAY:
1688 if ((features == ATA_DCO_IDENTIFY) ||
1689 (features == ATA_DCO_SET))
1697 * Executes a taskfile
1698 * See ide_taskfile_ioctl() for derivation
1700 static int exec_drive_taskfile(struct driver_data *dd,
1702 ide_task_request_t *req_task,
1705 struct host_to_dev_fis fis;
1706 struct host_to_dev_fis *reply;
1709 dma_addr_t outbuf_dma = 0;
1710 dma_addr_t inbuf_dma = 0;
1711 dma_addr_t dma_buffer = 0;
1713 unsigned int taskin = 0;
1714 unsigned int taskout = 0;
1716 unsigned int timeout;
1717 unsigned int force_single_sector;
1718 unsigned int transfer_size;
1719 unsigned long task_file_data;
1720 int intotal = outtotal + req_task->out_size;
1723 taskout = req_task->out_size;
1724 taskin = req_task->in_size;
1725 /* 130560 = 512 * 0xFF*/
1726 if (taskin > 130560 || taskout > 130560)
1730 outbuf = memdup_user(buf + outtotal, taskout);
1732 return PTR_ERR(outbuf);
1734 outbuf_dma = dma_map_single(&dd->pdev->dev, outbuf,
1735 taskout, DMA_TO_DEVICE);
1736 if (dma_mapping_error(&dd->pdev->dev, outbuf_dma)) {
1740 dma_buffer = outbuf_dma;
1744 inbuf = memdup_user(buf + intotal, taskin);
1745 if (IS_ERR(inbuf)) {
1746 err = PTR_ERR(inbuf);
1750 inbuf_dma = dma_map_single(&dd->pdev->dev, inbuf,
1751 taskin, DMA_FROM_DEVICE);
1752 if (dma_mapping_error(&dd->pdev->dev, inbuf_dma)) {
1756 dma_buffer = inbuf_dma;
1759 /* only supports PIO and non-data commands from this ioctl. */
1760 switch (req_task->data_phase) {
1762 nsect = taskout / ATA_SECT_SIZE;
1763 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1766 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1768 case TASKFILE_NO_DATA:
1769 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
1776 /* Build the FIS. */
1777 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1781 fis.command = req_task->io_ports[7];
1782 fis.features = req_task->io_ports[1];
1783 fis.sect_count = req_task->io_ports[2];
1784 fis.lba_low = req_task->io_ports[3];
1785 fis.lba_mid = req_task->io_ports[4];
1786 fis.lba_hi = req_task->io_ports[5];
1787 /* Clear the dev bit*/
1788 fis.device = req_task->io_ports[6] & ~0x10;
1790 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
1791 req_task->in_flags.all =
1792 IDE_TASKFILE_STD_IN_FLAGS |
1793 (IDE_HOB_STD_IN_FLAGS << 8);
1794 fis.lba_low_ex = req_task->hob_ports[3];
1795 fis.lba_mid_ex = req_task->hob_ports[4];
1796 fis.lba_hi_ex = req_task->hob_ports[5];
1797 fis.features_ex = req_task->hob_ports[1];
1798 fis.sect_cnt_ex = req_task->hob_ports[2];
1801 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
1804 force_single_sector = implicit_sector(fis.command, fis.features);
1806 if ((taskin || taskout) && (!fis.sect_count)) {
1808 fis.sect_count = nsect;
1810 if (!force_single_sector) {
1811 dev_warn(&dd->pdev->dev,
1812 "data movement but "
1813 "sect_count is 0\n");
1820 dbg_printk(MTIP_DRV_NAME
1821 " %s: cmd %x, feat %x, nsect %x,"
1822 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
1833 /* check for erase mode support during secure erase.*/
1834 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
1835 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
1839 mtip_set_timeout(dd, &fis, &timeout, erasemode);
1841 /* Determine the correct transfer size.*/
1842 if (force_single_sector)
1843 transfer_size = ATA_SECT_SIZE;
1845 transfer_size = ATA_SECT_SIZE * fis.sect_count;
1847 /* Execute the command.*/
1848 if (mtip_exec_internal_command(dd->port,
1859 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
1861 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
1862 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
1863 req_task->io_ports[7] = reply->control;
1865 reply = dd->port->rxfis + RX_FIS_D2H_REG;
1866 req_task->io_ports[7] = reply->command;
1869 /* reclaim the DMA buffers.*/
1871 dma_unmap_single(&dd->pdev->dev, inbuf_dma, taskin,
1874 dma_unmap_single(&dd->pdev->dev, outbuf_dma, taskout,
1879 /* return the ATA registers to the caller.*/
1880 req_task->io_ports[1] = reply->features;
1881 req_task->io_ports[2] = reply->sect_count;
1882 req_task->io_ports[3] = reply->lba_low;
1883 req_task->io_ports[4] = reply->lba_mid;
1884 req_task->io_ports[5] = reply->lba_hi;
1885 req_task->io_ports[6] = reply->device;
1887 if (req_task->out_flags.all & 1) {
1889 req_task->hob_ports[3] = reply->lba_low_ex;
1890 req_task->hob_ports[4] = reply->lba_mid_ex;
1891 req_task->hob_ports[5] = reply->lba_hi_ex;
1892 req_task->hob_ports[1] = reply->features_ex;
1893 req_task->hob_ports[2] = reply->sect_cnt_ex;
1895 dbg_printk(MTIP_DRV_NAME
1896 " %s: Completion: stat %x,"
1897 "err %x, sect_cnt %x, lbalo %x,"
1898 "lbamid %x, lbahi %x, dev %x\n",
1900 req_task->io_ports[7],
1901 req_task->io_ports[1],
1902 req_task->io_ports[2],
1903 req_task->io_ports[3],
1904 req_task->io_ports[4],
1905 req_task->io_ports[5],
1906 req_task->io_ports[6]);
1909 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
1915 if (copy_to_user(buf + intotal, inbuf, taskin)) {
1922 dma_unmap_single(&dd->pdev->dev, inbuf_dma, taskin,
1925 dma_unmap_single(&dd->pdev->dev, outbuf_dma, taskout,
1934 * Handle IOCTL calls from the Block Layer.
1936 * This function is called by the Block Layer when it receives an IOCTL
1937 * command that it does not understand. If the IOCTL command is not supported
1938 * this function returns -ENOTTY.
1940 * @dd Pointer to the driver data structure.
1941 * @cmd IOCTL command passed from the Block Layer.
1942 * @arg IOCTL argument passed from the Block Layer.
1945 * 0 The IOCTL completed successfully.
1946 * -ENOTTY The specified command is not supported.
1947 * -EFAULT An error occurred copying data to a user space buffer.
1948 * -EIO An error occurred while executing the command.
1950 static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
1954 case HDIO_GET_IDENTITY:
1956 if (copy_to_user((void __user *)arg, dd->port->identify,
1957 sizeof(u16) * ATA_ID_WORDS))
1961 case HDIO_DRIVE_CMD:
1963 u8 drive_command[4];
1965 /* Copy the user command info to our buffer. */
1966 if (copy_from_user(drive_command,
1967 (void __user *) arg,
1968 sizeof(drive_command)))
1971 /* Execute the drive command. */
1972 if (exec_drive_command(dd->port,
1974 (void __user *) (arg+4)))
1977 /* Copy the status back to the users buffer. */
1978 if (copy_to_user((void __user *) arg,
1980 sizeof(drive_command)))
1985 case HDIO_DRIVE_TASK:
1987 u8 drive_command[7];
1989 /* Copy the user command info to our buffer. */
1990 if (copy_from_user(drive_command,
1991 (void __user *) arg,
1992 sizeof(drive_command)))
1995 /* Execute the drive command. */
1996 if (exec_drive_task(dd->port, drive_command))
1999 /* Copy the status back to the users buffer. */
2000 if (copy_to_user((void __user *) arg,
2002 sizeof(drive_command)))
2007 case HDIO_DRIVE_TASKFILE: {
2008 ide_task_request_t req_task;
2011 if (copy_from_user(&req_task, (void __user *) arg,
2015 outtotal = sizeof(req_task);
2017 ret = exec_drive_taskfile(dd, (void __user *) arg,
2018 &req_task, outtotal);
2020 if (copy_to_user((void __user *) arg, &req_task,
2034 * Submit an IO to the hw
2036 * This function is called by the block layer to issue an io
2037 * to the device. Upon completion, the callback function will
2038 * be called with the data parameter passed as the callback data.
2040 * @dd Pointer to the driver data structure.
2041 * @start First sector to read.
2042 * @nsect Number of sectors to read.
2043 * @tag The tag of this read command.
2044 * @callback Pointer to the function that should be called
2045 * when the read completes.
2046 * @data Callback data passed to the callback function
2047 * when the read completes.
2048 * @dir Direction (read or write)
2053 static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq,
2054 struct mtip_cmd *command,
2055 struct blk_mq_hw_ctx *hctx)
2057 struct mtip_cmd_hdr *hdr =
2058 dd->port->command_list + sizeof(struct mtip_cmd_hdr) * rq->tag;
2059 struct host_to_dev_fis *fis;
2060 struct mtip_port *port = dd->port;
2061 int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2062 u64 start = blk_rq_pos(rq);
2063 unsigned int nsect = blk_rq_sectors(rq);
2066 /* Map the scatter list for DMA access */
2067 nents = blk_rq_map_sg(hctx->queue, rq, command->sg);
2068 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
2070 prefetch(&port->flags);
2072 command->scatter_ents = nents;
2075 * The number of retries for this command before it is
2076 * reported as a failure to the upper layers.
2078 command->retries = MTIP_MAX_RETRIES;
2081 fis = command->command;
2084 if (dma_dir == DMA_FROM_DEVICE)
2085 fis->command = ATA_CMD_FPDMA_READ;
2087 fis->command = ATA_CMD_FPDMA_WRITE;
2088 fis->lba_low = start & 0xFF;
2089 fis->lba_mid = (start >> 8) & 0xFF;
2090 fis->lba_hi = (start >> 16) & 0xFF;
2091 fis->lba_low_ex = (start >> 24) & 0xFF;
2092 fis->lba_mid_ex = (start >> 32) & 0xFF;
2093 fis->lba_hi_ex = (start >> 40) & 0xFF;
2094 fis->device = 1 << 6;
2095 fis->features = nsect & 0xFF;
2096 fis->features_ex = (nsect >> 8) & 0xFF;
2097 fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5));
2098 fis->sect_cnt_ex = 0;
2102 fill_command_sg(dd, command, nents);
2104 if (unlikely(command->unaligned))
2105 fis->device |= 1 << 7;
2107 /* Populate the command header */
2108 hdr->ctba = cpu_to_le32(command->command_dma & 0xFFFFFFFF);
2109 if (test_bit(MTIP_PF_HOST_CAP_64, &dd->port->flags))
2110 hdr->ctbau = cpu_to_le32((command->command_dma >> 16) >> 16);
2111 hdr->opts = cpu_to_le32((nents << 16) | 5 | AHCI_CMD_PREFETCH);
2112 hdr->byte_count = 0;
2114 command->direction = dma_dir;
2117 * To prevent this command from being issued
2118 * if an internal command is in progress or error handling is active.
2120 if (unlikely(port->flags & MTIP_PF_PAUSE_IO)) {
2121 set_bit(rq->tag, port->cmds_to_issue);
2122 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
2126 /* Issue the command to the hardware */
2127 mtip_issue_ncq_command(port, rq->tag);
2131 * Sysfs status dump.
2133 * @dev Pointer to the device structure, passed by the kernrel.
2134 * @attr Pointer to the device_attribute structure passed by the kernel.
2135 * @buf Pointer to the char buffer that will receive the stats info.
2138 * The size, in bytes, of the data copied into buf.
2140 static ssize_t mtip_hw_show_status(struct device *dev,
2141 struct device_attribute *attr,
2144 struct driver_data *dd = dev_to_disk(dev)->private_data;
2147 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
2148 size += sprintf(buf, "%s", "thermal_shutdown\n");
2149 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
2150 size += sprintf(buf, "%s", "write_protect\n");
2152 size += sprintf(buf, "%s", "online\n");
2157 static DEVICE_ATTR(status, 0444, mtip_hw_show_status, NULL);
2159 static struct attribute *mtip_disk_attrs[] = {
2160 &dev_attr_status.attr,
2164 static const struct attribute_group mtip_disk_attr_group = {
2165 .attrs = mtip_disk_attrs,
2168 static const struct attribute_group *mtip_disk_attr_groups[] = {
2169 &mtip_disk_attr_group,
2173 /* debugsfs entries */
2175 static ssize_t show_device_status(struct device_driver *drv, char *buf)
2178 struct driver_data *dd, *tmp;
2179 unsigned long flags;
2183 spin_lock_irqsave(&dev_lock, flags);
2184 size += sprintf(&buf[size], "Devices Present:\n");
2185 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
2188 dd->port->identify &&
2189 dd->port->identify_valid) {
2191 (char *) (dd->port->identify + 10), 21);
2192 status = *(dd->port->identify + 141);
2194 memset(id_buf, 0, 42);
2199 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2200 size += sprintf(&buf[size],
2201 " device %s %s (ftl rebuild %d %%)\n",
2202 dev_name(&dd->pdev->dev),
2206 size += sprintf(&buf[size],
2208 dev_name(&dd->pdev->dev),
2214 size += sprintf(&buf[size], "Devices Being Removed:\n");
2215 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
2218 dd->port->identify &&
2219 dd->port->identify_valid) {
2221 (char *) (dd->port->identify+10), 21);
2222 status = *(dd->port->identify + 141);
2224 memset(id_buf, 0, 42);
2229 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2230 size += sprintf(&buf[size],
2231 " device %s %s (ftl rebuild %d %%)\n",
2232 dev_name(&dd->pdev->dev),
2236 size += sprintf(&buf[size],
2238 dev_name(&dd->pdev->dev),
2243 spin_unlock_irqrestore(&dev_lock, flags);
2248 static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2249 size_t len, loff_t *offset)
2255 if (!len || *offset)
2258 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2262 size += show_device_status(NULL, buf);
2264 *offset = size <= len ? size : len;
2265 size = copy_to_user(ubuf, buf, *offset);
2270 return rv ? rv : *offset;
2273 static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2274 size_t len, loff_t *offset)
2276 struct driver_data *dd = (struct driver_data *)f->private_data;
2278 u32 group_allocated;
2285 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2289 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2291 for (n = dd->slot_groups-1; n >= 0; n--)
2292 size += sprintf(&buf[size], "%08X ",
2293 readl(dd->port->s_active[n]));
2295 size += sprintf(&buf[size], "]\n");
2296 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2298 for (n = dd->slot_groups-1; n >= 0; n--)
2299 size += sprintf(&buf[size], "%08X ",
2300 readl(dd->port->cmd_issue[n]));
2302 size += sprintf(&buf[size], "]\n");
2303 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2305 for (n = dd->slot_groups-1; n >= 0; n--)
2306 size += sprintf(&buf[size], "%08X ",
2307 readl(dd->port->completed[n]));
2309 size += sprintf(&buf[size], "]\n");
2310 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2311 readl(dd->port->mmio + PORT_IRQ_STAT));
2312 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2313 readl(dd->mmio + HOST_IRQ_STAT));
2314 size += sprintf(&buf[size], "\n");
2316 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2318 for (n = dd->slot_groups-1; n >= 0; n--) {
2319 if (sizeof(long) > sizeof(u32))
2321 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2323 group_allocated = dd->port->cmds_to_issue[n];
2324 size += sprintf(&buf[size], "%08X ", group_allocated);
2326 size += sprintf(&buf[size], "]\n");
2328 *offset = size <= len ? size : len;
2329 size = copy_to_user(ubuf, buf, *offset);
2334 return rv ? rv : *offset;
2337 static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2338 size_t len, loff_t *offset)
2340 struct driver_data *dd = (struct driver_data *)f->private_data;
2348 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2352 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2354 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2357 *offset = size <= len ? size : len;
2358 size = copy_to_user(ubuf, buf, *offset);
2363 return rv ? rv : *offset;
2366 static const struct file_operations mtip_device_status_fops = {
2367 .owner = THIS_MODULE,
2368 .open = simple_open,
2369 .read = mtip_hw_read_device_status,
2370 .llseek = no_llseek,
2373 static const struct file_operations mtip_regs_fops = {
2374 .owner = THIS_MODULE,
2375 .open = simple_open,
2376 .read = mtip_hw_read_registers,
2377 .llseek = no_llseek,
2380 static const struct file_operations mtip_flags_fops = {
2381 .owner = THIS_MODULE,
2382 .open = simple_open,
2383 .read = mtip_hw_read_flags,
2384 .llseek = no_llseek,
2387 static int mtip_hw_debugfs_init(struct driver_data *dd)
2392 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2393 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2394 dev_warn(&dd->pdev->dev,
2395 "Error creating node %s under debugfs\n",
2396 dd->disk->disk_name);
2397 dd->dfs_node = NULL;
2401 debugfs_create_file("flags", 0444, dd->dfs_node, dd, &mtip_flags_fops);
2402 debugfs_create_file("registers", 0444, dd->dfs_node, dd,
2408 static void mtip_hw_debugfs_exit(struct driver_data *dd)
2410 debugfs_remove_recursive(dd->dfs_node);
2414 * Perform any init/resume time hardware setup
2416 * @dd Pointer to the driver data structure.
2421 static inline void hba_setup(struct driver_data *dd)
2424 hwdata = readl(dd->mmio + HOST_HSORG);
2426 /* interrupt bug workaround: use only 1 IS bit.*/
2428 HSORG_DISABLE_SLOTGRP_INTR |
2429 HSORG_DISABLE_SLOTGRP_PXIS,
2430 dd->mmio + HOST_HSORG);
2433 static int mtip_device_unaligned_constrained(struct driver_data *dd)
2435 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
2439 * Detect the details of the product, and store anything needed
2440 * into the driver data structure. This includes product type and
2441 * version and number of slot groups.
2443 * @dd Pointer to the driver data structure.
2448 static void mtip_detect_product(struct driver_data *dd)
2451 unsigned int rev, slotgroups;
2454 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2456 * [15:8] hardware/software interface rev#
2457 * [ 3] asic-style interface
2458 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2460 hwdata = readl(dd->mmio + HOST_HSORG);
2462 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2463 dd->slot_groups = 1;
2466 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2467 rev = (hwdata & HSORG_HWREV) >> 8;
2468 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2469 dev_info(&dd->pdev->dev,
2470 "ASIC-FPGA design, HS rev 0x%x, "
2471 "%i slot groups [%i slots]\n",
2476 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2477 dev_warn(&dd->pdev->dev,
2478 "Warning: driver only supports "
2479 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2480 slotgroups = MTIP_MAX_SLOT_GROUPS;
2482 dd->slot_groups = slotgroups;
2486 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2490 * Blocking wait for FTL rebuild to complete
2492 * @dd Pointer to the DRIVER_DATA structure.
2495 * 0 FTL rebuild completed successfully
2496 * -EFAULT FTL rebuild error/timeout/interruption
2498 static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2500 unsigned long timeout, cnt = 0, start;
2502 dev_warn(&dd->pdev->dev,
2503 "FTL rebuild in progress. Polling for completion.\n");
2506 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2509 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
2512 if (mtip_check_surprise_removal(dd))
2515 if (mtip_get_identify(dd->port, NULL) < 0)
2518 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2519 MTIP_FTL_REBUILD_MAGIC) {
2521 /* Print message every 3 minutes */
2523 dev_warn(&dd->pdev->dev,
2524 "FTL rebuild in progress (%d secs).\n",
2525 jiffies_to_msecs(jiffies - start) / 1000);
2529 dev_warn(&dd->pdev->dev,
2530 "FTL rebuild complete (%d secs).\n",
2531 jiffies_to_msecs(jiffies - start) / 1000);
2532 mtip_block_initialize(dd);
2535 } while (time_before(jiffies, timeout));
2537 /* Check for timeout */
2538 dev_err(&dd->pdev->dev,
2539 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2540 jiffies_to_msecs(jiffies - start) / 1000);
2544 static void mtip_softirq_done_fn(struct request *rq)
2546 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
2547 struct driver_data *dd = rq->q->queuedata;
2549 /* Unmap the DMA scatter list entries */
2550 dma_unmap_sg(&dd->pdev->dev, cmd->sg, cmd->scatter_ents,
2553 if (unlikely(cmd->unaligned))
2554 atomic_inc(&dd->port->cmd_slot_unal);
2556 blk_mq_end_request(rq, cmd->status);
2559 static bool mtip_abort_cmd(struct request *req, void *data, bool reserved)
2561 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(req);
2562 struct driver_data *dd = data;
2564 dbg_printk(MTIP_DRV_NAME " Aborting request, tag = %d\n", req->tag);
2566 clear_bit(req->tag, dd->port->cmds_to_issue);
2567 cmd->status = BLK_STS_IOERR;
2568 mtip_softirq_done_fn(req);
2572 static bool mtip_queue_cmd(struct request *req, void *data, bool reserved)
2574 struct driver_data *dd = data;
2576 set_bit(req->tag, dd->port->cmds_to_issue);
2577 blk_abort_request(req);
2582 * service thread to issue queued commands
2584 * @data Pointer to the driver data structure.
2590 static int mtip_service_thread(void *data)
2592 struct driver_data *dd = (struct driver_data *)data;
2593 unsigned long slot, slot_start, slot_wrap, to;
2594 unsigned int num_cmd_slots = dd->slot_groups * 32;
2595 struct mtip_port *port = dd->port;
2598 if (kthread_should_stop() ||
2599 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2601 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2604 * the condition is to check neither an internal command is
2605 * is in progress nor error handling is active
2607 wait_event_interruptible(port->svc_wait, (port->flags) &&
2608 (port->flags & MTIP_PF_SVC_THD_WORK));
2610 if (kthread_should_stop() ||
2611 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2614 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
2618 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2621 /* Demux bits: start with error handling */
2622 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) {
2623 mtip_handle_tfe(dd);
2624 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
2627 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags))
2630 if (test_bit(MTIP_PF_TO_ACTIVE_BIT, &port->flags)) {
2631 to = jiffies + msecs_to_jiffies(5000);
2635 } while (atomic_read(&dd->irq_workers_active) != 0 &&
2636 time_before(jiffies, to));
2638 if (atomic_read(&dd->irq_workers_active) != 0)
2639 dev_warn(&dd->pdev->dev,
2640 "Completion workers still active!");
2642 blk_mq_quiesce_queue(dd->queue);
2644 blk_mq_tagset_busy_iter(&dd->tags, mtip_queue_cmd, dd);
2646 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &dd->port->flags);
2648 if (mtip_device_reset(dd))
2649 blk_mq_tagset_busy_iter(&dd->tags,
2650 mtip_abort_cmd, dd);
2652 clear_bit(MTIP_PF_TO_ACTIVE_BIT, &dd->port->flags);
2654 blk_mq_unquiesce_queue(dd->queue);
2657 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
2659 /* used to restrict the loop to one iteration */
2660 slot_start = num_cmd_slots;
2663 slot = find_next_bit(port->cmds_to_issue,
2664 num_cmd_slots, slot);
2665 if (slot_wrap == 1) {
2666 if ((slot_start >= slot) ||
2667 (slot >= num_cmd_slots))
2670 if (unlikely(slot_start == num_cmd_slots))
2673 if (unlikely(slot == num_cmd_slots)) {
2679 /* Issue the command to the hardware */
2680 mtip_issue_ncq_command(port, slot);
2682 clear_bit(slot, port->cmds_to_issue);
2685 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
2688 if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
2689 if (mtip_ftl_rebuild_poll(dd) == 0)
2690 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
2699 * DMA region teardown
2701 * @dd Pointer to driver_data structure
2706 static void mtip_dma_free(struct driver_data *dd)
2708 struct mtip_port *port = dd->port;
2711 dma_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
2712 port->block1, port->block1_dma);
2714 if (port->command_list) {
2715 dma_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
2716 port->command_list, port->command_list_dma);
2723 * @dd Pointer to driver_data structure
2726 * -ENOMEM Not enough free DMA region space to initialize driver
2728 static int mtip_dma_alloc(struct driver_data *dd)
2730 struct mtip_port *port = dd->port;
2732 /* Allocate dma memory for RX Fis, Identify, and Sector Buffer */
2734 dma_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
2735 &port->block1_dma, GFP_KERNEL);
2739 /* Allocate dma memory for command list */
2740 port->command_list =
2741 dma_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
2742 &port->command_list_dma, GFP_KERNEL);
2743 if (!port->command_list) {
2744 dma_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
2745 port->block1, port->block1_dma);
2746 port->block1 = NULL;
2747 port->block1_dma = 0;
2751 /* Setup all pointers into first DMA region */
2752 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
2753 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
2754 port->identify = port->block1 + AHCI_IDFY_OFFSET;
2755 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
2756 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
2757 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
2758 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
2759 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
2764 static int mtip_hw_get_identify(struct driver_data *dd)
2766 struct smart_attr attr242;
2770 if (mtip_get_identify(dd->port, NULL) < 0)
2773 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2774 MTIP_FTL_REBUILD_MAGIC) {
2775 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
2776 return MTIP_FTL_REBUILD_MAGIC;
2778 mtip_dump_identify(dd->port);
2780 /* check write protect, over temp and rebuild statuses */
2781 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
2783 dd->port->log_buf_dma, 1);
2785 dev_warn(&dd->pdev->dev,
2786 "Error in READ LOG EXT (10h) command\n");
2787 /* non-critical error, don't fail the load */
2789 buf = (unsigned char *)dd->port->log_buf;
2790 if (buf[259] & 0x1) {
2791 dev_info(&dd->pdev->dev,
2792 "Write protect bit is set.\n");
2793 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
2795 if (buf[288] == 0xF7) {
2796 dev_info(&dd->pdev->dev,
2797 "Exceeded Tmax, drive in thermal shutdown.\n");
2798 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
2800 if (buf[288] == 0xBF) {
2801 dev_info(&dd->pdev->dev,
2802 "Drive indicates rebuild has failed.\n");
2803 set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag);
2807 /* get write protect progess */
2808 memset(&attr242, 0, sizeof(struct smart_attr));
2809 if (mtip_get_smart_attr(dd->port, 242, &attr242))
2810 dev_warn(&dd->pdev->dev,
2811 "Unable to check write protect progress\n");
2813 dev_info(&dd->pdev->dev,
2814 "Write protect progress: %u%% (%u blocks)\n",
2815 attr242.cur, le32_to_cpu(attr242.data));
2821 * Called once for each card.
2823 * @dd Pointer to the driver data structure.
2826 * 0 on success, else an error code.
2828 static int mtip_hw_init(struct driver_data *dd)
2832 unsigned long timeout, timetaken;
2834 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
2836 mtip_detect_product(dd);
2837 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
2844 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
2849 /* Continue workqueue setup */
2850 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
2851 dd->work[i].port = dd->port;
2853 /* Enable unaligned IO constraints for some devices */
2854 if (mtip_device_unaligned_constrained(dd))
2855 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
2857 dd->unal_qdepth = 0;
2859 atomic_set(&dd->port->cmd_slot_unal, dd->unal_qdepth);
2861 /* Spinlock to prevent concurrent issue */
2862 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
2863 spin_lock_init(&dd->port->cmd_issue_lock[i]);
2865 /* Set the port mmio base address. */
2866 dd->port->mmio = dd->mmio + PORT_OFFSET;
2869 /* DMA allocations */
2870 rv = mtip_dma_alloc(dd);
2874 /* Setup the pointers to the extended s_active and CI registers. */
2875 for (i = 0; i < dd->slot_groups; i++) {
2876 dd->port->s_active[i] =
2877 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
2878 dd->port->cmd_issue[i] =
2879 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
2880 dd->port->completed[i] =
2881 dd->port->mmio + i*0x80 + PORT_SDBV;
2884 timetaken = jiffies;
2885 timeout = jiffies + msecs_to_jiffies(30000);
2886 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
2887 time_before(jiffies, timeout)) {
2890 if (unlikely(mtip_check_surprise_removal(dd))) {
2891 timetaken = jiffies - timetaken;
2892 dev_warn(&dd->pdev->dev,
2893 "Surprise removal detected at %u ms\n",
2894 jiffies_to_msecs(timetaken));
2898 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
2899 timetaken = jiffies - timetaken;
2900 dev_warn(&dd->pdev->dev,
2901 "Removal detected at %u ms\n",
2902 jiffies_to_msecs(timetaken));
2907 /* Conditionally reset the HBA. */
2908 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
2909 if (mtip_hba_reset(dd) < 0) {
2910 dev_err(&dd->pdev->dev,
2911 "Card did not reset within timeout\n");
2916 /* Clear any pending interrupts on the HBA */
2917 writel(readl(dd->mmio + HOST_IRQ_STAT),
2918 dd->mmio + HOST_IRQ_STAT);
2921 mtip_init_port(dd->port);
2922 mtip_start_port(dd->port);
2924 /* Setup the ISR and enable interrupts. */
2925 rv = request_irq(dd->pdev->irq, mtip_irq_handler, IRQF_SHARED,
2926 dev_driver_string(&dd->pdev->dev), dd);
2928 dev_err(&dd->pdev->dev,
2929 "Unable to allocate IRQ %d\n", dd->pdev->irq);
2932 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
2934 /* Enable interrupts on the HBA. */
2935 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2936 dd->mmio + HOST_CTL);
2938 init_waitqueue_head(&dd->port->svc_wait);
2940 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
2948 /* Disable interrupts on the HBA. */
2949 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2950 dd->mmio + HOST_CTL);
2952 /* Release the IRQ. */
2953 irq_set_affinity_hint(dd->pdev->irq, NULL);
2954 free_irq(dd->pdev->irq, dd);
2957 mtip_deinit_port(dd->port);
2961 /* Free the memory allocated for the for structure. */
2967 static int mtip_standby_drive(struct driver_data *dd)
2971 if (dd->sr || !dd->port)
2974 * Send standby immediate (E0h) to the drive so that it
2977 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
2978 !test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag) &&
2979 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)) {
2980 rv = mtip_standby_immediate(dd->port);
2982 dev_warn(&dd->pdev->dev,
2983 "STANDBY IMMEDIATE failed\n");
2989 * Called to deinitialize an interface.
2991 * @dd Pointer to the driver data structure.
2996 static int mtip_hw_exit(struct driver_data *dd)
2999 /* de-initialize the port. */
3000 mtip_deinit_port(dd->port);
3002 /* Disable interrupts on the HBA. */
3003 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3004 dd->mmio + HOST_CTL);
3007 /* Release the IRQ. */
3008 irq_set_affinity_hint(dd->pdev->irq, NULL);
3009 free_irq(dd->pdev->irq, dd);
3012 /* Free dma regions */
3015 /* Free the memory allocated for the for structure. */
3023 * Issue a Standby Immediate command to the device.
3025 * This function is called by the Block Layer just before the
3026 * system powers off during a shutdown.
3028 * @dd Pointer to the driver data structure.
3033 static int mtip_hw_shutdown(struct driver_data *dd)
3036 * Send standby immediate (E0h) to the drive so that it
3039 mtip_standby_drive(dd);
3047 * This function is called by the Block Layer just before the
3048 * system hibernates.
3050 * @dd Pointer to the driver data structure.
3053 * 0 Suspend was successful
3054 * -EFAULT Suspend was not successful
3056 static int mtip_hw_suspend(struct driver_data *dd)
3059 * Send standby immediate (E0h) to the drive
3060 * so that it saves its state.
3062 if (mtip_standby_drive(dd) != 0) {
3063 dev_err(&dd->pdev->dev,
3064 "Failed standby-immediate command\n");
3068 /* Disable interrupts on the HBA.*/
3069 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3070 dd->mmio + HOST_CTL);
3071 mtip_deinit_port(dd->port);
3079 * This function is called by the Block Layer as the
3082 * @dd Pointer to the driver data structure.
3085 * 0 Resume was successful
3086 * -EFAULT Resume was not successful
3088 static int mtip_hw_resume(struct driver_data *dd)
3090 /* Perform any needed hardware setup steps */
3094 if (mtip_hba_reset(dd) != 0) {
3095 dev_err(&dd->pdev->dev,
3096 "Unable to reset the HBA\n");
3101 * Enable the port, DMA engine, and FIS reception specific
3102 * h/w in controller.
3104 mtip_init_port(dd->port);
3105 mtip_start_port(dd->port);
3107 /* Enable interrupts on the HBA.*/
3108 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3109 dd->mmio + HOST_CTL);
3115 * Helper function for reusing disk name
3116 * upon hot insertion.
3118 static int rssd_disk_name_format(char *prefix,
3123 const int base = 'z' - 'a' + 1;
3124 char *begin = buf + strlen(prefix);
3125 char *end = buf + buflen;
3135 *--p = 'a' + (index % unit);
3136 index = (index / unit) - 1;
3137 } while (index >= 0);
3139 memmove(begin, p, end - p);
3140 memcpy(buf, prefix, strlen(prefix));
3146 * Block layer IOCTL handler.
3148 * @dev Pointer to the block_device structure.
3150 * @cmd IOCTL command passed from the user application.
3151 * @arg Argument passed from the user application.
3154 * 0 IOCTL completed successfully.
3155 * -ENOTTY IOCTL not supported or invalid driver data
3156 * structure pointer.
3158 static int mtip_block_ioctl(struct block_device *dev,
3163 struct driver_data *dd = dev->bd_disk->private_data;
3165 if (!capable(CAP_SYS_ADMIN))
3171 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
3178 return mtip_hw_ioctl(dd, cmd, arg);
3182 #ifdef CONFIG_COMPAT
3184 * Block layer compat IOCTL handler.
3186 * @dev Pointer to the block_device structure.
3188 * @cmd IOCTL command passed from the user application.
3189 * @arg Argument passed from the user application.
3192 * 0 IOCTL completed successfully.
3193 * -ENOTTY IOCTL not supported or invalid driver data
3194 * structure pointer.
3196 static int mtip_block_compat_ioctl(struct block_device *dev,
3201 struct driver_data *dd = dev->bd_disk->private_data;
3203 if (!capable(CAP_SYS_ADMIN))
3209 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
3215 case HDIO_DRIVE_TASKFILE: {
3216 struct mtip_compat_ide_task_request_s __user *compat_req_task;
3217 ide_task_request_t req_task;
3218 int compat_tasksize, outtotal, ret;
3221 sizeof(struct mtip_compat_ide_task_request_s);
3224 (struct mtip_compat_ide_task_request_s __user *) arg;
3226 if (copy_from_user(&req_task, (void __user *) arg,
3227 compat_tasksize - (2 * sizeof(compat_long_t))))
3230 if (get_user(req_task.out_size, &compat_req_task->out_size))
3233 if (get_user(req_task.in_size, &compat_req_task->in_size))
3236 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3238 ret = exec_drive_taskfile(dd, (void __user *) arg,
3239 &req_task, outtotal);
3241 if (copy_to_user((void __user *) arg, &req_task,
3243 (2 * sizeof(compat_long_t))))
3246 if (put_user(req_task.out_size, &compat_req_task->out_size))
3249 if (put_user(req_task.in_size, &compat_req_task->in_size))
3255 return mtip_hw_ioctl(dd, cmd, arg);
3261 * Obtain the geometry of the device.
3263 * You may think that this function is obsolete, but some applications,
3264 * fdisk for example still used CHS values. This function describes the
3265 * device as having 224 heads and 56 sectors per cylinder. These values are
3266 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3267 * partition is described in terms of a start and end cylinder this means
3268 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3269 * affects performance.
3271 * @dev Pointer to the block_device strucutre.
3272 * @geo Pointer to a hd_geometry structure.
3275 * 0 Operation completed successfully.
3276 * -ENOTTY An error occurred while reading the drive capacity.
3278 static int mtip_block_getgeo(struct block_device *dev,
3279 struct hd_geometry *geo)
3281 struct driver_data *dd = dev->bd_disk->private_data;
3287 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3288 dev_warn(&dd->pdev->dev,
3289 "Could not get drive capacity.\n");
3295 sector_div(capacity, (geo->heads * geo->sectors));
3296 geo->cylinders = capacity;
3300 static int mtip_block_open(struct block_device *dev, fmode_t mode)
3302 struct driver_data *dd;
3304 if (dev && dev->bd_disk) {
3305 dd = (struct driver_data *) dev->bd_disk->private_data;
3308 if (test_bit(MTIP_DDF_REMOVAL_BIT,
3318 static void mtip_block_release(struct gendisk *disk, fmode_t mode)
3323 * Block device operation function.
3325 * This structure contains pointers to the functions required by the block
3328 static const struct block_device_operations mtip_block_ops = {
3329 .open = mtip_block_open,
3330 .release = mtip_block_release,
3331 .ioctl = mtip_block_ioctl,
3332 #ifdef CONFIG_COMPAT
3333 .compat_ioctl = mtip_block_compat_ioctl,
3335 .getgeo = mtip_block_getgeo,
3336 .owner = THIS_MODULE
3339 static inline bool is_se_active(struct driver_data *dd)
3341 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT, &dd->port->flags))) {
3342 if (dd->port->ic_pause_timer) {
3343 unsigned long to = dd->port->ic_pause_timer +
3344 msecs_to_jiffies(1000);
3345 if (time_after(jiffies, to)) {
3346 clear_bit(MTIP_PF_SE_ACTIVE_BIT,
3348 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
3349 dd->port->ic_pause_timer = 0;
3350 wake_up_interruptible(&dd->port->svc_wait);
3359 static inline bool is_stopped(struct driver_data *dd, struct request *rq)
3361 if (likely(!(dd->dd_flag & MTIP_DDF_STOP_IO)))
3364 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
3366 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
3368 if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag) &&
3371 if (test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))
3373 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag))
3379 static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx,
3382 struct driver_data *dd = hctx->queue->queuedata;
3383 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3385 if (rq_data_dir(rq) == READ || !dd->unal_qdepth)
3389 * If unaligned depth must be limited on this controller, mark it
3390 * as unaligned if the IO isn't on a 4k boundary (start of length).
3392 if (blk_rq_sectors(rq) <= 64) {
3393 if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7))
3397 if (cmd->unaligned && atomic_dec_if_positive(&dd->port->cmd_slot_unal) >= 0)
3403 static blk_status_t mtip_issue_reserved_cmd(struct blk_mq_hw_ctx *hctx,
3406 struct driver_data *dd = hctx->queue->queuedata;
3407 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3408 struct mtip_int_cmd *icmd = cmd->icmd;
3409 struct mtip_cmd_hdr *hdr =
3410 dd->port->command_list + sizeof(struct mtip_cmd_hdr) * rq->tag;
3411 struct mtip_cmd_sg *command_sg;
3413 if (mtip_commands_active(dd->port))
3414 return BLK_STS_DEV_RESOURCE;
3416 hdr->ctba = cpu_to_le32(cmd->command_dma & 0xFFFFFFFF);
3417 if (test_bit(MTIP_PF_HOST_CAP_64, &dd->port->flags))
3418 hdr->ctbau = cpu_to_le32((cmd->command_dma >> 16) >> 16);
3419 /* Populate the SG list */
3420 hdr->opts = cpu_to_le32(icmd->opts | icmd->fis_len);
3421 if (icmd->buf_len) {
3422 command_sg = cmd->command + AHCI_CMD_TBL_HDR_SZ;
3424 command_sg->info = cpu_to_le32((icmd->buf_len-1) & 0x3FFFFF);
3425 command_sg->dba = cpu_to_le32(icmd->buffer & 0xFFFFFFFF);
3426 command_sg->dba_upper =
3427 cpu_to_le32((icmd->buffer >> 16) >> 16);
3429 hdr->opts |= cpu_to_le32((1 << 16));
3432 /* Populate the command header */
3433 hdr->byte_count = 0;
3435 blk_mq_start_request(rq);
3436 mtip_issue_non_ncq_command(dd->port, rq->tag);
3440 static blk_status_t mtip_queue_rq(struct blk_mq_hw_ctx *hctx,
3441 const struct blk_mq_queue_data *bd)
3443 struct driver_data *dd = hctx->queue->queuedata;
3444 struct request *rq = bd->rq;
3445 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3447 if (blk_rq_is_passthrough(rq))
3448 return mtip_issue_reserved_cmd(hctx, rq);
3450 if (unlikely(mtip_check_unal_depth(hctx, rq)))
3451 return BLK_STS_DEV_RESOURCE;
3453 if (is_se_active(dd) || is_stopped(dd, rq))
3454 return BLK_STS_IOERR;
3456 blk_mq_start_request(rq);
3458 mtip_hw_submit_io(dd, rq, cmd, hctx);
3462 static void mtip_free_cmd(struct blk_mq_tag_set *set, struct request *rq,
3463 unsigned int hctx_idx)
3465 struct driver_data *dd = set->driver_data;
3466 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3471 dma_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ, cmd->command,
3475 static int mtip_init_cmd(struct blk_mq_tag_set *set, struct request *rq,
3476 unsigned int hctx_idx, unsigned int numa_node)
3478 struct driver_data *dd = set->driver_data;
3479 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3481 cmd->command = dma_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3482 &cmd->command_dma, GFP_KERNEL);
3486 sg_init_table(cmd->sg, MTIP_MAX_SG);
3490 static enum blk_eh_timer_return mtip_cmd_timeout(struct request *req,
3493 struct driver_data *dd = req->q->queuedata;
3496 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(req);
3498 cmd->status = BLK_STS_TIMEOUT;
3499 blk_mq_complete_request(req);
3503 if (test_bit(req->tag, dd->port->cmds_to_issue))
3506 if (test_and_set_bit(MTIP_PF_TO_ACTIVE_BIT, &dd->port->flags))
3509 wake_up_interruptible(&dd->port->svc_wait);
3511 return BLK_EH_RESET_TIMER;
3514 static const struct blk_mq_ops mtip_mq_ops = {
3515 .queue_rq = mtip_queue_rq,
3516 .init_request = mtip_init_cmd,
3517 .exit_request = mtip_free_cmd,
3518 .complete = mtip_softirq_done_fn,
3519 .timeout = mtip_cmd_timeout,
3523 * Block layer initialization function.
3525 * This function is called once by the PCI layer for each P320
3526 * device that is connected to the system.
3528 * @dd Pointer to the driver data structure.
3531 * 0 on success else an error code.
3533 static int mtip_block_initialize(struct driver_data *dd)
3535 int rv = 0, wait_for_rebuild = 0;
3537 unsigned int index = 0;
3540 goto skip_create_disk; /* hw init done, before rebuild */
3542 if (mtip_hw_init(dd)) {
3544 goto protocol_init_error;
3547 memset(&dd->tags, 0, sizeof(dd->tags));
3548 dd->tags.ops = &mtip_mq_ops;
3549 dd->tags.nr_hw_queues = 1;
3550 dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS;
3551 dd->tags.reserved_tags = 1;
3552 dd->tags.cmd_size = sizeof(struct mtip_cmd);
3553 dd->tags.numa_node = dd->numa_node;
3554 dd->tags.flags = BLK_MQ_F_SHOULD_MERGE;
3555 dd->tags.driver_data = dd;
3556 dd->tags.timeout = MTIP_NCQ_CMD_TIMEOUT_MS;
3558 rv = blk_mq_alloc_tag_set(&dd->tags);
3560 dev_err(&dd->pdev->dev,
3561 "Unable to allocate request queue\n");
3562 goto block_queue_alloc_tag_error;
3565 dd->disk = blk_mq_alloc_disk(&dd->tags, dd);
3566 if (IS_ERR(dd->disk)) {
3567 dev_err(&dd->pdev->dev,
3568 "Unable to allocate request queue\n");
3570 goto block_queue_alloc_init_error;
3572 dd->queue = dd->disk->queue;
3574 rv = ida_alloc(&rssd_index_ida, GFP_KERNEL);
3579 rv = rssd_disk_name_format("rssd",
3581 dd->disk->disk_name,
3584 goto disk_index_error;
3586 dd->disk->major = dd->major;
3587 dd->disk->first_minor = index * MTIP_MAX_MINORS;
3588 dd->disk->minors = MTIP_MAX_MINORS;
3589 dd->disk->fops = &mtip_block_ops;
3590 dd->disk->private_data = dd;
3593 mtip_hw_debugfs_init(dd);
3596 /* Initialize the protocol layer. */
3597 wait_for_rebuild = mtip_hw_get_identify(dd);
3598 if (wait_for_rebuild < 0) {
3599 dev_err(&dd->pdev->dev,
3600 "Protocol layer initialization failed\n");
3602 goto init_hw_cmds_error;
3606 * if rebuild pending, start the service thread, and delay the block
3607 * queue creation and device_add_disk()
3609 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3610 goto start_service_thread;
3612 /* Set device limits. */
3613 blk_queue_flag_set(QUEUE_FLAG_NONROT, dd->queue);
3614 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, dd->queue);
3615 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3616 blk_queue_physical_block_size(dd->queue, 4096);
3617 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3618 blk_queue_max_segment_size(dd->queue, 0x400000);
3619 dma_set_max_seg_size(&dd->pdev->dev, 0x400000);
3620 blk_queue_io_min(dd->queue, 4096);
3622 /* Set the capacity of the device in 512 byte sectors. */
3623 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3624 dev_warn(&dd->pdev->dev,
3625 "Could not read drive capacity\n");
3627 goto read_capacity_error;
3629 set_capacity(dd->disk, capacity);
3631 /* Enable the block device and add it to /dev */
3632 rv = device_add_disk(&dd->pdev->dev, dd->disk, mtip_disk_attr_groups);
3634 goto read_capacity_error;
3636 if (dd->mtip_svc_handler) {
3637 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
3638 return rv; /* service thread created for handling rebuild */
3641 start_service_thread:
3642 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
3644 "mtip_svc_thd_%02d", index);
3646 if (IS_ERR(dd->mtip_svc_handler)) {
3647 dev_err(&dd->pdev->dev, "service thread failed to start\n");
3648 dd->mtip_svc_handler = NULL;
3650 goto kthread_run_error;
3652 wake_up_process(dd->mtip_svc_handler);
3653 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3654 rv = wait_for_rebuild;
3659 /* Delete our gendisk. This also removes the device from /dev */
3660 del_gendisk(dd->disk);
3661 read_capacity_error:
3663 mtip_hw_debugfs_exit(dd);
3665 ida_free(&rssd_index_ida, index);
3667 blk_cleanup_disk(dd->disk);
3668 block_queue_alloc_init_error:
3669 blk_mq_free_tag_set(&dd->tags);
3670 block_queue_alloc_tag_error:
3671 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
3672 protocol_init_error:
3676 static bool mtip_no_dev_cleanup(struct request *rq, void *data, bool reserv)
3678 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3680 cmd->status = BLK_STS_IOERR;
3681 blk_mq_complete_request(rq);
3686 * Block layer deinitialization function.
3688 * Called by the PCI layer as each P320 device is removed.
3690 * @dd Pointer to the driver data structure.
3695 static int mtip_block_remove(struct driver_data *dd)
3697 mtip_hw_debugfs_exit(dd);
3699 if (dd->mtip_svc_handler) {
3700 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
3701 wake_up_interruptible(&dd->port->svc_wait);
3702 kthread_stop(dd->mtip_svc_handler);
3707 * Explicitly wait here for IOs to quiesce,
3708 * as mtip_standby_drive usually won't wait for IOs.
3710 if (!mtip_quiesce_io(dd->port, MTIP_QUIESCE_IO_TIMEOUT_MS))
3711 mtip_standby_drive(dd);
3714 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
3715 dd->disk->disk_name);
3717 blk_freeze_queue_start(dd->queue);
3718 blk_mq_quiesce_queue(dd->queue);
3719 blk_mq_tagset_busy_iter(&dd->tags, mtip_no_dev_cleanup, dd);
3720 blk_mq_unquiesce_queue(dd->queue);
3723 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
3724 del_gendisk(dd->disk);
3725 if (dd->disk->queue) {
3726 blk_cleanup_queue(dd->queue);
3727 blk_mq_free_tag_set(&dd->tags);
3734 ida_free(&rssd_index_ida, dd->index);
3736 /* De-initialize the protocol layer. */
3743 * Function called by the PCI layer when just before the
3744 * machine shuts down.
3746 * If a protocol layer shutdown function is present it will be called
3749 * @dd Pointer to the driver data structure.
3754 static int mtip_block_shutdown(struct driver_data *dd)
3756 mtip_hw_shutdown(dd);
3758 /* Delete our gendisk structure, and cleanup the blk queue. */
3760 dev_info(&dd->pdev->dev,
3761 "Shutting down %s ...\n", dd->disk->disk_name);
3763 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
3764 del_gendisk(dd->disk);
3765 if (dd->disk->queue) {
3766 blk_cleanup_queue(dd->queue);
3767 blk_mq_free_tag_set(&dd->tags);
3774 ida_free(&rssd_index_ida, dd->index);
3778 static int mtip_block_suspend(struct driver_data *dd)
3780 dev_info(&dd->pdev->dev,
3781 "Suspending %s ...\n", dd->disk->disk_name);
3782 mtip_hw_suspend(dd);
3786 static int mtip_block_resume(struct driver_data *dd)
3788 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
3789 dd->disk->disk_name);
3794 static void drop_cpu(int cpu)
3799 static int get_least_used_cpu_on_node(int node)
3801 int cpu, least_used_cpu, least_cnt;
3802 const struct cpumask *node_mask;
3804 node_mask = cpumask_of_node(node);
3805 least_used_cpu = cpumask_first(node_mask);
3806 least_cnt = cpu_use[least_used_cpu];
3807 cpu = least_used_cpu;
3809 for_each_cpu(cpu, node_mask) {
3810 if (cpu_use[cpu] < least_cnt) {
3811 least_used_cpu = cpu;
3812 least_cnt = cpu_use[cpu];
3815 cpu_use[least_used_cpu]++;
3816 return least_used_cpu;
3819 /* Helper for selecting a node in round robin mode */
3820 static inline int mtip_get_next_rr_node(void)
3822 static int next_node = NUMA_NO_NODE;
3824 if (next_node == NUMA_NO_NODE) {
3825 next_node = first_online_node;
3829 next_node = next_online_node(next_node);
3830 if (next_node == MAX_NUMNODES)
3831 next_node = first_online_node;
3835 static DEFINE_HANDLER(0);
3836 static DEFINE_HANDLER(1);
3837 static DEFINE_HANDLER(2);
3838 static DEFINE_HANDLER(3);
3839 static DEFINE_HANDLER(4);
3840 static DEFINE_HANDLER(5);
3841 static DEFINE_HANDLER(6);
3842 static DEFINE_HANDLER(7);
3844 static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
3846 unsigned short pcie_dev_ctrl;
3848 if (pci_is_pcie(pdev)) {
3849 pcie_capability_read_word(pdev, PCI_EXP_DEVCTL, &pcie_dev_ctrl);
3850 if (pcie_dev_ctrl & PCI_EXP_DEVCTL_NOSNOOP_EN ||
3851 pcie_dev_ctrl & PCI_EXP_DEVCTL_RELAX_EN) {
3852 dev_info(&dd->pdev->dev,
3853 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
3854 pdev->vendor, pdev->device);
3855 pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
3856 PCI_EXP_DEVCTL_RELAX_EN);
3857 pcie_capability_write_word(pdev, PCI_EXP_DEVCTL,
3863 static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev)
3866 * This workaround is specific to AMD/ATI chipset with a PCI upstream
3867 * device with device id 0x5aXX
3869 if (pdev->bus && pdev->bus->self) {
3870 if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI &&
3871 ((pdev->bus->self->device & 0xff00) == 0x5a00)) {
3872 mtip_disable_link_opts(dd, pdev->bus->self);
3874 /* Check further up the topology */
3875 struct pci_dev *parent_dev = pdev->bus->self;
3876 if (parent_dev->bus &&
3877 parent_dev->bus->parent &&
3878 parent_dev->bus->parent->self &&
3879 parent_dev->bus->parent->self->vendor ==
3880 PCI_VENDOR_ID_ATI &&
3881 (parent_dev->bus->parent->self->device &
3882 0xff00) == 0x5a00) {
3883 mtip_disable_link_opts(dd,
3884 parent_dev->bus->parent->self);
3891 * Called for each supported PCI device detected.
3893 * This function allocates the private data structure, enables the
3894 * PCI device and then calls the block layer initialization function.
3897 * 0 on success else an error code.
3899 static int mtip_pci_probe(struct pci_dev *pdev,
3900 const struct pci_device_id *ent)
3903 struct driver_data *dd = NULL;
3905 const struct cpumask *node_mask;
3906 int cpu, i = 0, j = 0;
3907 int my_node = NUMA_NO_NODE;
3908 unsigned long flags;
3910 /* Allocate memory for this devices private data. */
3911 my_node = pcibus_to_node(pdev->bus);
3912 if (my_node != NUMA_NO_NODE) {
3913 if (!node_online(my_node))
3914 my_node = mtip_get_next_rr_node();
3916 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
3917 my_node = mtip_get_next_rr_node();
3919 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
3920 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
3921 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
3923 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
3927 /* Attach the private data to this PCI device. */
3928 pci_set_drvdata(pdev, dd);
3930 rv = pcim_enable_device(pdev);
3932 dev_err(&pdev->dev, "Unable to enable device\n");
3936 /* Map BAR5 to memory. */
3937 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
3939 dev_err(&pdev->dev, "Unable to map regions\n");
3943 rv = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
3945 dev_warn(&pdev->dev, "64-bit DMA enable failed\n");
3949 /* Copy the info we may need later into the private data structure. */
3950 dd->major = mtip_major;
3951 dd->instance = instance;
3953 dd->numa_node = my_node;
3955 INIT_LIST_HEAD(&dd->online_list);
3956 INIT_LIST_HEAD(&dd->remove_list);
3958 memset(dd->workq_name, 0, 32);
3959 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
3961 dd->isr_workq = create_workqueue(dd->workq_name);
3962 if (!dd->isr_workq) {
3963 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
3968 memset(cpu_list, 0, sizeof(cpu_list));
3970 node_mask = cpumask_of_node(dd->numa_node);
3971 if (!cpumask_empty(node_mask)) {
3972 for_each_cpu(cpu, node_mask)
3974 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
3975 j = strlen(cpu_list);
3978 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
3980 topology_physical_package_id(cpumask_first(node_mask)),
3981 nr_cpus_node(dd->numa_node),
3984 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
3986 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
3987 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
3988 cpu_to_node(dd->isr_binding), dd->isr_binding);
3990 /* first worker context always runs in ISR */
3991 dd->work[0].cpu_binding = dd->isr_binding;
3992 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
3993 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
3994 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
3995 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
3996 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
3997 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
3998 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4000 /* Log the bindings */
4001 for_each_present_cpu(cpu) {
4002 memset(cpu_list, 0, sizeof(cpu_list));
4003 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4004 if (dd->work[i].cpu_binding == cpu) {
4005 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4006 j = strlen(cpu_list);
4010 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4013 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4014 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4015 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4016 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4017 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4018 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4019 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4020 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4022 pci_set_master(pdev);
4023 rv = pci_enable_msi(pdev);
4025 dev_warn(&pdev->dev,
4026 "Unable to enable MSI interrupt.\n");
4027 goto msi_initialize_err;
4030 mtip_fix_ero_nosnoop(dd, pdev);
4032 /* Initialize the block layer. */
4033 rv = mtip_block_initialize(dd);
4036 "Unable to initialize block layer\n");
4037 goto block_initialize_err;
4041 * Increment the instance count so that each device has a unique
4045 if (rv != MTIP_FTL_REBUILD_MAGIC)
4046 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
4048 rv = 0; /* device in rebuild state, return 0 from probe */
4050 /* Add to online list even if in ftl rebuild */
4051 spin_lock_irqsave(&dev_lock, flags);
4052 list_add(&dd->online_list, &online_list);
4053 spin_unlock_irqrestore(&dev_lock, flags);
4057 block_initialize_err:
4058 pci_disable_msi(pdev);
4061 if (dd->isr_workq) {
4062 destroy_workqueue(dd->isr_workq);
4063 drop_cpu(dd->work[0].cpu_binding);
4064 drop_cpu(dd->work[1].cpu_binding);
4065 drop_cpu(dd->work[2].cpu_binding);
4068 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4072 pci_set_drvdata(pdev, NULL);
4079 * Called for each probed device when the device is removed or the
4080 * driver is unloaded.
4085 static void mtip_pci_remove(struct pci_dev *pdev)
4087 struct driver_data *dd = pci_get_drvdata(pdev);
4088 unsigned long flags, to;
4090 set_bit(MTIP_DDF_REMOVAL_BIT, &dd->dd_flag);
4092 spin_lock_irqsave(&dev_lock, flags);
4093 list_del_init(&dd->online_list);
4094 list_add(&dd->remove_list, &removing_list);
4095 spin_unlock_irqrestore(&dev_lock, flags);
4097 mtip_check_surprise_removal(dd);
4098 synchronize_irq(dd->pdev->irq);
4100 /* Spin until workers are done */
4101 to = jiffies + msecs_to_jiffies(4000);
4104 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4105 time_before(jiffies, to));
4107 if (atomic_read(&dd->irq_workers_active) != 0) {
4108 dev_warn(&dd->pdev->dev,
4109 "Completion workers still active!\n");
4112 blk_mark_disk_dead(dd->disk);
4113 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
4115 /* Clean up the block layer. */
4116 mtip_block_remove(dd);
4118 if (dd->isr_workq) {
4119 destroy_workqueue(dd->isr_workq);
4120 drop_cpu(dd->work[0].cpu_binding);
4121 drop_cpu(dd->work[1].cpu_binding);
4122 drop_cpu(dd->work[2].cpu_binding);
4125 pci_disable_msi(pdev);
4127 spin_lock_irqsave(&dev_lock, flags);
4128 list_del_init(&dd->remove_list);
4129 spin_unlock_irqrestore(&dev_lock, flags);
4133 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4134 pci_set_drvdata(pdev, NULL);
4138 * Called for each probed device when the device is suspended.
4144 static int __maybe_unused mtip_pci_suspend(struct device *dev)
4147 struct driver_data *dd = dev_get_drvdata(dev);
4149 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
4151 /* Disable ports & interrupts then send standby immediate */
4152 rv = mtip_block_suspend(dd);
4154 dev_err(dev, "Failed to suspend controller\n");
4160 * Called for each probed device when the device is resumed.
4166 static int __maybe_unused mtip_pci_resume(struct device *dev)
4169 struct driver_data *dd = dev_get_drvdata(dev);
4172 * Calls hbaReset, initPort, & startPort function
4173 * then enables interrupts
4175 rv = mtip_block_resume(dd);
4177 dev_err(dev, "Unable to resume\n");
4179 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
4190 static void mtip_pci_shutdown(struct pci_dev *pdev)
4192 struct driver_data *dd = pci_get_drvdata(pdev);
4194 mtip_block_shutdown(dd);
4197 /* Table of device ids supported by this driver. */
4198 static const struct pci_device_id mtip_pci_tbl[] = {
4199 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4200 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4201 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4202 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4203 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4204 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4205 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
4209 static SIMPLE_DEV_PM_OPS(mtip_pci_pm_ops, mtip_pci_suspend, mtip_pci_resume);
4211 /* Structure that describes the PCI driver functions. */
4212 static struct pci_driver mtip_pci_driver = {
4213 .name = MTIP_DRV_NAME,
4214 .id_table = mtip_pci_tbl,
4215 .probe = mtip_pci_probe,
4216 .remove = mtip_pci_remove,
4217 .driver.pm = &mtip_pci_pm_ops,
4218 .shutdown = mtip_pci_shutdown,
4221 MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4224 * Module initialization function.
4226 * Called once when the module is loaded. This function allocates a major
4227 * block device number to the Cyclone devices and registers the PCI layer
4231 * 0 on success else error code.
4233 static int __init mtip_init(void)
4237 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
4239 /* Allocate a major block device number to use with this driver. */
4240 error = register_blkdev(0, MTIP_DRV_NAME);
4242 pr_err("Unable to register block device (%d)\n",
4248 dfs_parent = debugfs_create_dir("rssd", NULL);
4249 if (IS_ERR_OR_NULL(dfs_parent)) {
4250 pr_warn("Error creating debugfs parent\n");
4254 dfs_device_status = debugfs_create_file("device_status",
4255 0444, dfs_parent, NULL,
4256 &mtip_device_status_fops);
4257 if (IS_ERR_OR_NULL(dfs_device_status)) {
4258 pr_err("Error creating device_status node\n");
4259 dfs_device_status = NULL;
4263 /* Register our PCI operations. */
4264 error = pci_register_driver(&mtip_pci_driver);
4266 debugfs_remove(dfs_parent);
4267 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4274 * Module de-initialization function.
4276 * Called once when the module is unloaded. This function deallocates
4277 * the major block device number allocated by mtip_init() and
4278 * unregisters the PCI layer of the driver.
4283 static void __exit mtip_exit(void)
4285 /* Release the allocated major block device number. */
4286 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4288 /* Unregister the PCI driver. */
4289 pci_unregister_driver(&mtip_pci_driver);
4291 debugfs_remove_recursive(dfs_parent);
4294 MODULE_AUTHOR("Micron Technology, Inc");
4295 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4296 MODULE_LICENSE("GPL");
4297 MODULE_VERSION(MTIP_DRV_VERSION);
4299 module_init(mtip_init);
4300 module_exit(mtip_exit);