2 * Driver for the Micron P320 SSD
3 * Copyright (C) 2011 Micron Technology, Inc.
5 * Portions of this code were derived from works subjected to the
7 * Copyright (C) 2009 Integrated Device Technology, Inc.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
21 #include <linux/pci.h>
22 #include <linux/interrupt.h>
23 #include <linux/ata.h>
24 #include <linux/delay.h>
25 #include <linux/hdreg.h>
26 #include <linux/uaccess.h>
27 #include <linux/random.h>
28 #include <linux/smp.h>
29 #include <linux/compat.h>
31 #include <linux/module.h>
32 #include <linux/genhd.h>
33 #include <linux/blkdev.h>
34 #include <linux/blk-mq.h>
35 #include <linux/bio.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/idr.h>
38 #include <linux/kthread.h>
39 #include <../drivers/ata/ahci.h>
40 #include <linux/export.h>
41 #include <linux/debugfs.h>
42 #include <linux/prefetch.h>
45 #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
47 /* DMA region containing RX Fis, Identify, RLE10, and SMART buffers */
48 #define AHCI_RX_FIS_SZ 0x100
49 #define AHCI_RX_FIS_OFFSET 0x0
50 #define AHCI_IDFY_SZ ATA_SECT_SIZE
51 #define AHCI_IDFY_OFFSET 0x400
52 #define AHCI_SECTBUF_SZ ATA_SECT_SIZE
53 #define AHCI_SECTBUF_OFFSET 0x800
54 #define AHCI_SMARTBUF_SZ ATA_SECT_SIZE
55 #define AHCI_SMARTBUF_OFFSET 0xC00
56 /* 0x100 + 0x200 + 0x200 + 0x200 is smaller than 4k but we pad it out */
57 #define BLOCK_DMA_ALLOC_SZ 4096
59 /* DMA region containing command table (should be 8192 bytes) */
60 #define AHCI_CMD_SLOT_SZ sizeof(struct mtip_cmd_hdr)
61 #define AHCI_CMD_TBL_SZ (MTIP_MAX_COMMAND_SLOTS * AHCI_CMD_SLOT_SZ)
62 #define AHCI_CMD_TBL_OFFSET 0x0
64 /* DMA region per command (contains header and SGL) */
65 #define AHCI_CMD_TBL_HDR_SZ 0x80
66 #define AHCI_CMD_TBL_HDR_OFFSET 0x0
67 #define AHCI_CMD_TBL_SGL_SZ (MTIP_MAX_SG * sizeof(struct mtip_cmd_sg))
68 #define AHCI_CMD_TBL_SGL_OFFSET AHCI_CMD_TBL_HDR_SZ
69 #define CMD_DMA_ALLOC_SZ (AHCI_CMD_TBL_SGL_SZ + AHCI_CMD_TBL_HDR_SZ)
72 #define HOST_CAP_NZDMA (1 << 19)
73 #define HOST_HSORG 0xFC
74 #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
75 #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
76 #define HSORG_HWREV 0xFF00
77 #define HSORG_STYLE 0x8
78 #define HSORG_SLOTGROUPS 0x7
80 #define PORT_COMMAND_ISSUE 0x38
81 #define PORT_SDBV 0x7C
83 #define PORT_OFFSET 0x100
84 #define PORT_MEM_SIZE 0x80
86 #define PORT_IRQ_ERR \
87 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
88 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
89 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
91 #define PORT_IRQ_LEGACY \
92 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
93 #define PORT_IRQ_HANDLED \
94 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
95 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
96 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
97 #define DEF_PORT_IRQ \
98 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
100 /* product numbers */
101 #define MTIP_PRODUCT_UNKNOWN 0x00
102 #define MTIP_PRODUCT_ASICFPGA 0x11
104 /* Device instance number, incremented each time a device is probed. */
107 struct list_head online_list;
108 struct list_head removing_list;
112 * Global variable used to hold the major block device number
113 * allocated in mtip_init().
115 static int mtip_major;
116 static struct dentry *dfs_parent;
117 static struct dentry *dfs_device_status;
119 static u32 cpu_use[NR_CPUS];
121 static DEFINE_SPINLOCK(rssd_index_lock);
122 static DEFINE_IDA(rssd_index_ida);
124 static int mtip_block_initialize(struct driver_data *dd);
127 struct mtip_compat_ide_task_request_s {
130 ide_reg_valid_t out_flags;
131 ide_reg_valid_t in_flags;
134 compat_ulong_t out_size;
135 compat_ulong_t in_size;
140 * This function check_for_surprise_removal is called
141 * while card is removed from the system and it will
142 * read the vendor id from the configration space
144 * @pdev Pointer to the pci_dev structure.
147 * true if device removed, else false
149 static bool mtip_check_surprise_removal(struct pci_dev *pdev)
152 struct driver_data *dd = pci_get_drvdata(pdev);
157 /* Read the vendorID from the configuration space */
158 pci_read_config_word(pdev, 0x00, &vendor_id);
159 if (vendor_id == 0xFFFF) {
162 set_bit(QUEUE_FLAG_DEAD, &dd->queue->queue_flags);
164 dev_warn(&dd->pdev->dev,
165 "%s: dd->queue is NULL\n", __func__);
167 set_bit(MTIP_PF_SR_CLEANUP_BIT, &dd->port->flags);
168 wake_up_interruptible(&dd->port->svc_wait);
170 dev_warn(&dd->pdev->dev,
171 "%s: dd->port is NULL\n", __func__);
172 return true; /* device removed */
175 return false; /* device present */
178 static struct mtip_cmd *mtip_get_int_command(struct driver_data *dd)
182 rq = blk_mq_alloc_request(dd->queue, 0, __GFP_WAIT, true);
183 return blk_mq_rq_to_pdu(rq);
186 static void mtip_put_int_command(struct driver_data *dd, struct mtip_cmd *cmd)
188 blk_put_request(blk_mq_rq_from_pdu(cmd));
192 * Once we add support for one hctx per mtip group, this will change a bit
194 static struct request *mtip_rq_from_tag(struct driver_data *dd,
197 struct blk_mq_hw_ctx *hctx = dd->queue->queue_hw_ctx[0];
199 return blk_mq_tag_to_rq(hctx->tags, tag);
202 static struct mtip_cmd *mtip_cmd_from_tag(struct driver_data *dd,
205 struct request *rq = mtip_rq_from_tag(dd, tag);
207 return blk_mq_rq_to_pdu(rq);
211 * IO completion function.
213 * This completion function is called by the driver ISR when a
214 * command that was issued by the kernel completes. It first calls the
215 * asynchronous completion function which normally calls back into the block
216 * layer passing the asynchronous callback data, then unmaps the
217 * scatter list associated with the completed command, and finally
218 * clears the allocated bit associated with the completed command.
220 * @port Pointer to the port data structure.
221 * @tag Tag of the command.
222 * @data Pointer to driver_data.
223 * @status Completion status.
228 static void mtip_async_complete(struct mtip_port *port,
229 int tag, struct mtip_cmd *cmd, int status)
231 struct driver_data *dd = port->dd;
234 if (unlikely(!dd) || unlikely(!port))
237 if (unlikely(status == PORT_IRQ_TF_ERR)) {
238 dev_warn(&port->dd->pdev->dev,
239 "Command tag %d failed due to TFE\n", tag);
242 /* Unmap the DMA scatter list entries */
243 dma_unmap_sg(&dd->pdev->dev, cmd->sg, cmd->scatter_ents, cmd->direction);
245 rq = mtip_rq_from_tag(dd, tag);
247 if (unlikely(cmd->unaligned))
248 up(&port->cmd_slot_unal);
250 blk_mq_end_io(rq, status ? -EIO : 0);
254 * Reset the HBA (without sleeping)
256 * @dd Pointer to the driver data structure.
259 * 0 The reset was successful.
260 * -1 The HBA Reset bit did not clear.
262 static int mtip_hba_reset(struct driver_data *dd)
264 unsigned long timeout;
266 /* Set the reset bit */
267 writel(HOST_RESET, dd->mmio + HOST_CTL);
270 readl(dd->mmio + HOST_CTL);
272 /* Spin for up to 2 seconds, waiting for reset acknowledgement */
273 timeout = jiffies + msecs_to_jiffies(2000);
276 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
279 } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
280 && time_before(jiffies, timeout));
282 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
289 * Issue a command to the hardware.
291 * Set the appropriate bit in the s_active and Command Issue hardware
292 * registers, causing hardware command processing to begin.
294 * @port Pointer to the port structure.
295 * @tag The tag of the command to be issued.
300 static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
302 int group = tag >> 5;
304 /* guard SACT and CI registers */
305 spin_lock(&port->cmd_issue_lock[group]);
306 writel((1 << MTIP_TAG_BIT(tag)),
307 port->s_active[MTIP_TAG_INDEX(tag)]);
308 writel((1 << MTIP_TAG_BIT(tag)),
309 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
310 spin_unlock(&port->cmd_issue_lock[group]);
314 * Enable/disable the reception of FIS
316 * @port Pointer to the port data structure
317 * @enable 1 to enable, 0 to disable
320 * Previous state: 1 enabled, 0 disabled
322 static int mtip_enable_fis(struct mtip_port *port, int enable)
326 /* enable FIS reception */
327 tmp = readl(port->mmio + PORT_CMD);
329 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
331 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
334 readl(port->mmio + PORT_CMD);
336 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
340 * Enable/disable the DMA engine
342 * @port Pointer to the port data structure
343 * @enable 1 to enable, 0 to disable
346 * Previous state: 1 enabled, 0 disabled.
348 static int mtip_enable_engine(struct mtip_port *port, int enable)
352 /* enable FIS reception */
353 tmp = readl(port->mmio + PORT_CMD);
355 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
357 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
359 readl(port->mmio + PORT_CMD);
360 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
364 * Enables the port DMA engine and FIS reception.
369 static inline void mtip_start_port(struct mtip_port *port)
371 /* Enable FIS reception */
372 mtip_enable_fis(port, 1);
374 /* Enable the DMA engine */
375 mtip_enable_engine(port, 1);
379 * Deinitialize a port by disabling port interrupts, the DMA engine,
382 * @port Pointer to the port structure
387 static inline void mtip_deinit_port(struct mtip_port *port)
389 /* Disable interrupts on this port */
390 writel(0, port->mmio + PORT_IRQ_MASK);
392 /* Disable the DMA engine */
393 mtip_enable_engine(port, 0);
395 /* Disable FIS reception */
396 mtip_enable_fis(port, 0);
402 * This function deinitializes the port by calling mtip_deinit_port() and
403 * then initializes it by setting the command header and RX FIS addresses,
404 * clearing the SError register and any pending port interrupts before
405 * re-enabling the default set of port interrupts.
407 * @port Pointer to the port structure.
412 static void mtip_init_port(struct mtip_port *port)
415 mtip_deinit_port(port);
417 /* Program the command list base and FIS base addresses */
418 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
419 writel((port->command_list_dma >> 16) >> 16,
420 port->mmio + PORT_LST_ADDR_HI);
421 writel((port->rxfis_dma >> 16) >> 16,
422 port->mmio + PORT_FIS_ADDR_HI);
425 writel(port->command_list_dma & 0xFFFFFFFF,
426 port->mmio + PORT_LST_ADDR);
427 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
430 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
432 /* reset the completed registers.*/
433 for (i = 0; i < port->dd->slot_groups; i++)
434 writel(0xFFFFFFFF, port->completed[i]);
436 /* Clear any pending interrupts for this port */
437 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
439 /* Clear any pending interrupts on the HBA. */
440 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
441 port->dd->mmio + HOST_IRQ_STAT);
443 /* Enable port interrupts */
444 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
450 * @port Pointer to the port data structure.
455 static void mtip_restart_port(struct mtip_port *port)
457 unsigned long timeout;
459 /* Disable the DMA engine */
460 mtip_enable_engine(port, 0);
462 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
463 timeout = jiffies + msecs_to_jiffies(500);
464 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
465 && time_before(jiffies, timeout))
468 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
472 * Chip quirk: escalate to hba reset if
473 * PxCMD.CR not clear after 500 ms
475 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
476 dev_warn(&port->dd->pdev->dev,
477 "PxCMD.CR not clear, escalating reset\n");
479 if (mtip_hba_reset(port->dd))
480 dev_err(&port->dd->pdev->dev,
481 "HBA reset escalation failed.\n");
483 /* 30 ms delay before com reset to quiesce chip */
487 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
490 writel(readl(port->mmio + PORT_SCR_CTL) |
491 1, port->mmio + PORT_SCR_CTL);
492 readl(port->mmio + PORT_SCR_CTL);
494 /* Wait 1 ms to quiesce chip function */
495 timeout = jiffies + msecs_to_jiffies(1);
496 while (time_before(jiffies, timeout))
499 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
502 /* Clear PxSCTL.DET */
503 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
504 port->mmio + PORT_SCR_CTL);
505 readl(port->mmio + PORT_SCR_CTL);
507 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
508 timeout = jiffies + msecs_to_jiffies(500);
509 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
510 && time_before(jiffies, timeout))
513 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
516 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
517 dev_warn(&port->dd->pdev->dev,
518 "COM reset failed\n");
520 mtip_init_port(port);
521 mtip_start_port(port);
525 static int mtip_device_reset(struct driver_data *dd)
529 if (mtip_check_surprise_removal(dd->pdev))
532 if (mtip_hba_reset(dd) < 0)
536 mtip_init_port(dd->port);
537 mtip_start_port(dd->port);
539 /* Enable interrupts on the HBA. */
540 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
541 dd->mmio + HOST_CTL);
546 * Helper function for tag logging
548 static void print_tags(struct driver_data *dd,
550 unsigned long *tagbits,
553 unsigned char tagmap[128];
554 int group, tagmap_len = 0;
556 memset(tagmap, 0, sizeof(tagmap));
557 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
558 tagmap_len += sprintf(tagmap + tagmap_len, "%016lX ",
560 dev_warn(&dd->pdev->dev,
561 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
565 * Internal command completion callback function.
567 * This function is normally called by the driver ISR when an internal
568 * command completed. This function signals the command completion by
569 * calling complete().
571 * @port Pointer to the port data structure.
572 * @tag Tag of the command that has completed.
573 * @data Pointer to a completion structure.
574 * @status Completion status.
579 static void mtip_completion(struct mtip_port *port,
580 int tag, struct mtip_cmd *command, int status)
582 struct completion *waiting = command->comp_data;
583 if (unlikely(status == PORT_IRQ_TF_ERR))
584 dev_warn(&port->dd->pdev->dev,
585 "Internal command %d completed with TFE\n", tag);
590 static void mtip_null_completion(struct mtip_port *port,
591 int tag, struct mtip_cmd *command, int status)
595 static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
596 dma_addr_t buffer_dma, unsigned int sectors);
597 static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
598 struct smart_attr *attrib);
602 * @dd Pointer to the DRIVER_DATA structure.
607 static void mtip_handle_tfe(struct driver_data *dd)
609 int group, tag, bit, reissue, rv;
610 struct mtip_port *port;
611 struct mtip_cmd *cmd;
613 struct host_to_dev_fis *fis;
614 unsigned long tagaccum[SLOTBITS_IN_LONGS];
615 unsigned int cmd_cnt = 0;
617 char *fail_reason = NULL;
618 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
620 dev_warn(&dd->pdev->dev, "Taskfile error\n");
624 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
626 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
627 test_bit(MTIP_TAG_INTERNAL, port->allocated)) {
628 cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
629 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
631 if (cmd->comp_data && cmd->comp_func) {
632 cmd->comp_func(port, MTIP_TAG_INTERNAL,
633 cmd, PORT_IRQ_TF_ERR);
635 goto handle_tfe_exit;
638 /* clear the tag accumulator */
639 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
641 /* Loop through all the groups */
642 for (group = 0; group < dd->slot_groups; group++) {
643 completed = readl(port->completed[group]);
645 dev_warn(&dd->pdev->dev, "g=%u, comp=%x\n", group, completed);
647 /* clear completed status register in the hardware.*/
648 writel(completed, port->completed[group]);
650 /* Process successfully completed commands */
651 for (bit = 0; bit < 32 && completed; bit++) {
652 if (!(completed & (1<<bit)))
654 tag = (group << 5) + bit;
656 /* Skip the internal command slot */
657 if (tag == MTIP_TAG_INTERNAL)
660 cmd = mtip_cmd_from_tag(dd, tag);
661 if (likely(cmd->comp_func)) {
662 set_bit(tag, tagaccum);
664 cmd->comp_func(port, tag, cmd, 0);
666 dev_err(&port->dd->pdev->dev,
667 "Missing completion func for tag %d",
669 if (mtip_check_surprise_removal(dd->pdev)) {
670 /* don't proceed further */
677 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
679 /* Restart the port */
681 mtip_restart_port(port);
683 /* Trying to determine the cause of the error */
684 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
686 dd->port->log_buf_dma, 1);
688 dev_warn(&dd->pdev->dev,
689 "Error in READ LOG EXT (10h) command\n");
690 /* non-critical error, don't fail the load */
692 buf = (unsigned char *)dd->port->log_buf;
693 if (buf[259] & 0x1) {
694 dev_info(&dd->pdev->dev,
695 "Write protect bit is set.\n");
696 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
697 fail_all_ncq_write = 1;
698 fail_reason = "write protect";
700 if (buf[288] == 0xF7) {
701 dev_info(&dd->pdev->dev,
702 "Exceeded Tmax, drive in thermal shutdown.\n");
703 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
704 fail_all_ncq_cmds = 1;
705 fail_reason = "thermal shutdown";
707 if (buf[288] == 0xBF) {
708 set_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
709 dev_info(&dd->pdev->dev,
710 "Drive indicates rebuild has failed. Secure erase required.\n");
711 fail_all_ncq_cmds = 1;
712 fail_reason = "rebuild failed";
716 /* clear the tag accumulator */
717 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
719 /* Loop through all the groups */
720 for (group = 0; group < dd->slot_groups; group++) {
721 for (bit = 0; bit < 32; bit++) {
723 tag = (group << 5) + bit;
724 cmd = mtip_cmd_from_tag(dd, tag);
726 fis = (struct host_to_dev_fis *)cmd->command;
728 /* Should re-issue? */
729 if (tag == MTIP_TAG_INTERNAL ||
730 fis->command == ATA_CMD_SET_FEATURES)
733 if (fail_all_ncq_cmds ||
734 (fail_all_ncq_write &&
735 fis->command == ATA_CMD_FPDMA_WRITE)) {
736 dev_warn(&dd->pdev->dev,
737 " Fail: %s w/tag %d [%s].\n",
738 fis->command == ATA_CMD_FPDMA_WRITE ?
741 fail_reason != NULL ?
742 fail_reason : "unknown");
743 if (cmd->comp_func) {
744 cmd->comp_func(port, tag,
752 * First check if this command has
753 * exceeded its retries.
755 if (reissue && (cmd->retries-- > 0)) {
757 set_bit(tag, tagaccum);
759 /* Re-issue the command. */
760 mtip_issue_ncq_command(port, tag);
765 /* Retire a command that will not be reissued */
766 dev_warn(&port->dd->pdev->dev,
767 "retiring tag %d\n", tag);
770 cmd->comp_func(port, tag, cmd, PORT_IRQ_TF_ERR);
772 dev_warn(&port->dd->pdev->dev,
773 "Bad completion for tag %d\n",
777 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
780 /* clear eh_active */
781 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
782 wake_up_interruptible(&port->svc_wait);
786 * Handle a set device bits interrupt
788 static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
791 struct driver_data *dd = port->dd;
793 struct mtip_cmd *command;
796 WARN_ON_ONCE(!completed);
799 /* clear completed status register in the hardware.*/
800 writel(completed, port->completed[group]);
802 /* Process completed commands. */
803 for (bit = 0; (bit < 32) && completed; bit++) {
804 if (completed & 0x01) {
805 tag = (group << 5) | bit;
807 /* skip internal command slot. */
808 if (unlikely(tag == MTIP_TAG_INTERNAL))
811 command = mtip_cmd_from_tag(dd, tag);
812 if (likely(command->comp_func))
813 command->comp_func(port, tag, command, 0);
815 dev_dbg(&dd->pdev->dev,
816 "Null completion for tag %d",
819 if (mtip_check_surprise_removal(
828 /* If last, re-enable interrupts */
829 if (atomic_dec_return(&dd->irq_workers_active) == 0)
830 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
834 * Process legacy pio and d2h interrupts
836 static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
838 struct mtip_port *port = dd->port;
839 struct mtip_cmd *cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
841 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
842 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
843 & (1 << MTIP_TAG_INTERNAL))) {
844 if (cmd->comp_func) {
845 cmd->comp_func(port, MTIP_TAG_INTERNAL, cmd, 0);
854 * Demux and handle errors
856 static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
859 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
860 dev_warn(&dd->pdev->dev,
861 "Clearing PxSERR.DIAG.x\n");
862 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
865 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
866 dev_warn(&dd->pdev->dev,
867 "Clearing PxSERR.DIAG.n\n");
868 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
871 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
872 dev_warn(&dd->pdev->dev,
873 "Port stat errors %x unhandled\n",
874 (port_stat & ~PORT_IRQ_HANDLED));
875 if (mtip_check_surprise_removal(dd->pdev))
878 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR))) {
879 set_bit(MTIP_PF_EH_ACTIVE_BIT, &dd->port->flags);
880 wake_up_interruptible(&dd->port->svc_wait);
884 static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
886 struct driver_data *dd = (struct driver_data *) data;
887 struct mtip_port *port = dd->port;
888 u32 hba_stat, port_stat;
890 int do_irq_enable = 1, i, workers;
891 struct mtip_work *twork;
893 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
897 /* Acknowledge the interrupt status on the port.*/
898 port_stat = readl(port->mmio + PORT_IRQ_STAT);
899 writel(port_stat, port->mmio + PORT_IRQ_STAT);
901 /* Demux port status */
902 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
904 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
906 /* Start at 1: group zero is always local? */
907 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
909 twork = &dd->work[i];
910 twork->completed = readl(port->completed[i]);
911 if (twork->completed)
915 atomic_set(&dd->irq_workers_active, workers);
917 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
918 twork = &dd->work[i];
919 if (twork->completed)
926 if (likely(dd->work[0].completed))
927 mtip_workq_sdbfx(port, 0,
928 dd->work[0].completed);
932 * Chip quirk: SDB interrupt but nothing
939 if (unlikely(port_stat & PORT_IRQ_ERR)) {
940 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
941 /* don't proceed further */
944 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
948 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
951 if (unlikely(port_stat & PORT_IRQ_LEGACY))
952 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
955 /* acknowledge interrupt */
956 if (unlikely(do_irq_enable))
957 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
963 * HBA interrupt subroutine.
966 * @instance Pointer to the driver data structure.
969 * IRQ_HANDLED A HBA interrupt was pending and handled.
970 * IRQ_NONE This interrupt was not for the HBA.
972 static irqreturn_t mtip_irq_handler(int irq, void *instance)
974 struct driver_data *dd = instance;
976 return mtip_handle_irq(dd);
979 static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
981 writel(1 << MTIP_TAG_BIT(tag),
982 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
985 static bool mtip_pause_ncq(struct mtip_port *port,
986 struct host_to_dev_fis *fis)
988 struct host_to_dev_fis *reply;
989 unsigned long task_file_data;
991 reply = port->rxfis + RX_FIS_D2H_REG;
992 task_file_data = readl(port->mmio+PORT_TFDATA);
994 if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
995 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
997 if ((task_file_data & 1))
1000 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
1001 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1002 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1003 port->ic_pause_timer = jiffies;
1005 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1006 (fis->features == 0x03)) {
1007 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1008 port->ic_pause_timer = jiffies;
1010 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1011 ((fis->command == 0xFC) &&
1012 (fis->features == 0x27 || fis->features == 0x72 ||
1013 fis->features == 0x62 || fis->features == 0x26))) {
1014 /* Com reset after secure erase or lowlevel format */
1015 mtip_restart_port(port);
1023 * Wait for port to quiesce
1025 * @port Pointer to port data structure
1026 * @timeout Max duration to wait (ms)
1030 * -EBUSY Commands still active
1032 static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1036 unsigned int active = 1;
1038 blk_mq_stop_hw_queues(port->dd->queue);
1040 to = jiffies + msecs_to_jiffies(timeout);
1042 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1043 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
1045 continue; /* svc thd is actively issuing commands */
1049 if (mtip_check_surprise_removal(port->dd->pdev))
1051 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
1055 * Ignore s_active bit 0 of array element 0.
1056 * This bit will always be set
1058 active = readl(port->s_active[0]) & 0xFFFFFFFE;
1059 for (n = 1; n < port->dd->slot_groups; n++)
1060 active |= readl(port->s_active[n]);
1064 } while (time_before(jiffies, to));
1066 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
1067 return active ? -EBUSY : 0;
1069 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
1074 * Execute an internal command and wait for the completion.
1076 * @port Pointer to the port data structure.
1077 * @fis Pointer to the FIS that describes the command.
1078 * @fis_len Length in WORDS of the FIS.
1079 * @buffer DMA accessible for command data.
1080 * @buf_len Length, in bytes, of the data buffer.
1081 * @opts Command header options, excluding the FIS length
1082 * and the number of PRD entries.
1083 * @timeout Time in ms to wait for the command to complete.
1086 * 0 Command completed successfully.
1087 * -EFAULT The buffer address is not correctly aligned.
1088 * -EBUSY Internal command or other IO in progress.
1089 * -EAGAIN Time out waiting for command to complete.
1091 static int mtip_exec_internal_command(struct mtip_port *port,
1092 struct host_to_dev_fis *fis,
1098 unsigned long timeout)
1100 struct mtip_cmd_sg *command_sg;
1101 DECLARE_COMPLETION_ONSTACK(wait);
1102 struct mtip_cmd *int_cmd;
1103 struct driver_data *dd = port->dd;
1106 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1107 if (buffer & 0x00000007) {
1108 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
1112 int_cmd = mtip_get_int_command(dd);
1114 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1115 port->ic_pause_timer = 0;
1117 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1118 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1120 if (atomic == GFP_KERNEL) {
1121 if (fis->command != ATA_CMD_STANDBYNOW1) {
1122 /* wait for io to complete if non atomic */
1123 if (mtip_quiesce_io(port,
1124 MTIP_QUIESCE_IO_TIMEOUT_MS) < 0) {
1125 dev_warn(&dd->pdev->dev,
1126 "Failed to quiesce IO\n");
1127 mtip_put_int_command(dd, int_cmd);
1128 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1129 wake_up_interruptible(&port->svc_wait);
1134 /* Set the completion function and data for the command. */
1135 int_cmd->comp_data = &wait;
1136 int_cmd->comp_func = mtip_completion;
1139 /* Clear completion - we're going to poll */
1140 int_cmd->comp_data = NULL;
1141 int_cmd->comp_func = mtip_null_completion;
1144 /* Copy the command to the command table */
1145 memcpy(int_cmd->command, fis, fis_len*4);
1147 /* Populate the SG list */
1148 int_cmd->command_header->opts =
1149 __force_bit2int cpu_to_le32(opts | fis_len);
1151 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1154 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1156 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1157 command_sg->dba_upper =
1158 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
1160 int_cmd->command_header->opts |=
1161 __force_bit2int cpu_to_le32((1 << 16));
1164 /* Populate the command header */
1165 int_cmd->command_header->byte_count = 0;
1167 /* Issue the command to the hardware */
1168 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1170 if (atomic == GFP_KERNEL) {
1171 /* Wait for the command to complete or timeout. */
1172 if ((rv = wait_for_completion_interruptible_timeout(
1174 msecs_to_jiffies(timeout))) <= 0) {
1175 if (rv == -ERESTARTSYS) { /* interrupted */
1176 dev_err(&dd->pdev->dev,
1177 "Internal command [%02X] was interrupted after %lu ms\n",
1178 fis->command, timeout);
1181 } else if (rv == 0) /* timeout */
1182 dev_err(&dd->pdev->dev,
1183 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1184 fis->command, timeout);
1186 dev_err(&dd->pdev->dev,
1187 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1188 fis->command, rv, timeout);
1190 if (mtip_check_surprise_removal(dd->pdev) ||
1191 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1193 dev_err(&dd->pdev->dev,
1194 "Internal command [%02X] wait returned due to SR\n",
1199 mtip_device_reset(dd); /* recover from timeout issue */
1204 u32 hba_stat, port_stat;
1206 /* Spin for <timeout> checking if command still outstanding */
1207 timeout = jiffies + msecs_to_jiffies(timeout);
1208 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1209 & (1 << MTIP_TAG_INTERNAL))
1210 && time_before(jiffies, timeout)) {
1211 if (mtip_check_surprise_removal(dd->pdev)) {
1215 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
1216 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1221 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1225 if (port_stat & PORT_IRQ_ERR) {
1226 dev_err(&dd->pdev->dev,
1227 "Internal command [%02X] failed\n",
1229 mtip_device_reset(dd);
1233 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1234 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1237 dd->mmio + HOST_IRQ_STAT);
1243 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1244 & (1 << MTIP_TAG_INTERNAL)) {
1246 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1247 mtip_device_reset(dd);
1252 /* Clear the allocated and active bits for the internal command. */
1253 mtip_put_int_command(dd, int_cmd);
1254 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1258 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1259 wake_up_interruptible(&port->svc_wait);
1265 * Byte-swap ATA ID strings.
1267 * ATA identify data contains strings in byte-swapped 16-bit words.
1268 * They must be swapped (on all architectures) to be usable as C strings.
1269 * This function swaps bytes in-place.
1271 * @buf The buffer location of the string
1272 * @len The number of bytes to swap
1277 static inline void ata_swap_string(u16 *buf, unsigned int len)
1280 for (i = 0; i < (len/2); i++)
1281 be16_to_cpus(&buf[i]);
1284 static void mtip_set_timeout(struct driver_data *dd,
1285 struct host_to_dev_fis *fis,
1286 unsigned int *timeout, u8 erasemode)
1288 switch (fis->command) {
1289 case ATA_CMD_DOWNLOAD_MICRO:
1290 *timeout = 120000; /* 2 minutes */
1292 case ATA_CMD_SEC_ERASE_UNIT:
1295 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
1297 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
1299 case ATA_CMD_STANDBYNOW1:
1300 *timeout = 120000; /* 2 minutes */
1304 *timeout = 60000; /* 60 seconds */
1307 *timeout = 15000; /* 15 seconds */
1310 *timeout = MTIP_IOCTL_CMD_TIMEOUT_MS;
1316 * Request the device identity information.
1318 * If a user space buffer is not specified, i.e. is NULL, the
1319 * identify information is still read from the drive and placed
1320 * into the identify data buffer (@e port->identify) in the
1321 * port data structure.
1322 * When the identify buffer contains valid identify information @e
1323 * port->identify_valid is non-zero.
1325 * @port Pointer to the port structure.
1326 * @user_buffer A user space buffer where the identify data should be
1330 * 0 Command completed successfully.
1331 * -EFAULT An error occurred while coping data to the user buffer.
1332 * -1 Command failed.
1334 static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1337 struct host_to_dev_fis fis;
1339 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
1342 /* Build the FIS. */
1343 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1346 fis.command = ATA_CMD_ID_ATA;
1348 /* Set the identify information as invalid. */
1349 port->identify_valid = 0;
1351 /* Clear the identify information. */
1352 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1354 /* Execute the command. */
1355 if (mtip_exec_internal_command(port,
1359 sizeof(u16) * ATA_ID_WORDS,
1362 MTIP_INT_CMD_TIMEOUT_MS)
1369 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1370 * perform field-sensitive swapping on the string fields.
1371 * See the kernel use of ata_id_string() for proof of this.
1373 #ifdef __LITTLE_ENDIAN
1374 ata_swap_string(port->identify + 27, 40); /* model string*/
1375 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1376 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1380 for (i = 0; i < ATA_ID_WORDS; i++)
1381 port->identify[i] = le16_to_cpu(port->identify[i]);
1385 /* Check security locked state */
1386 if (port->identify[128] & 0x4)
1387 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1389 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1391 #ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
1392 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1393 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1394 port->dd->trim_supp = true;
1397 port->dd->trim_supp = false;
1399 /* Set the identify buffer as valid. */
1400 port->identify_valid = 1;
1406 ATA_ID_WORDS * sizeof(u16))) {
1417 * Issue a standby immediate command to the device.
1419 * @port Pointer to the port structure.
1422 * 0 Command was executed successfully.
1423 * -1 An error occurred while executing the command.
1425 static int mtip_standby_immediate(struct mtip_port *port)
1428 struct host_to_dev_fis fis;
1429 unsigned long start;
1430 unsigned int timeout;
1432 /* Build the FIS. */
1433 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1436 fis.command = ATA_CMD_STANDBYNOW1;
1438 mtip_set_timeout(port->dd, &fis, &timeout, 0);
1441 rv = mtip_exec_internal_command(port,
1449 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1450 jiffies_to_msecs(jiffies - start));
1452 dev_warn(&port->dd->pdev->dev,
1453 "STANDBY IMMEDIATE command failed.\n");
1459 * Issue a READ LOG EXT command to the device.
1461 * @port pointer to the port structure.
1462 * @page page number to fetch
1463 * @buffer pointer to buffer
1464 * @buffer_dma dma address corresponding to @buffer
1465 * @sectors page length to fetch, in sectors
1468 * @rv return value from mtip_exec_internal_command()
1470 static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1471 dma_addr_t buffer_dma, unsigned int sectors)
1473 struct host_to_dev_fis fis;
1475 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1478 fis.command = ATA_CMD_READ_LOG_EXT;
1479 fis.sect_count = sectors & 0xFF;
1480 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1483 fis.device = ATA_DEVICE_OBS;
1485 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1487 return mtip_exec_internal_command(port,
1491 sectors * ATA_SECT_SIZE,
1494 MTIP_INT_CMD_TIMEOUT_MS);
1498 * Issue a SMART READ DATA command to the device.
1500 * @port pointer to the port structure.
1501 * @buffer pointer to buffer
1502 * @buffer_dma dma address corresponding to @buffer
1505 * @rv return value from mtip_exec_internal_command()
1507 static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1508 dma_addr_t buffer_dma)
1510 struct host_to_dev_fis fis;
1512 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1515 fis.command = ATA_CMD_SMART;
1516 fis.features = 0xD0;
1520 fis.device = ATA_DEVICE_OBS;
1522 return mtip_exec_internal_command(port,
1533 * Get the value of a smart attribute
1535 * @port pointer to the port structure
1536 * @id attribute number
1537 * @attrib pointer to return attrib information corresponding to @id
1540 * -EINVAL NULL buffer passed or unsupported attribute @id.
1541 * -EPERM Identify data not valid, SMART not supported or not enabled
1543 static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1544 struct smart_attr *attrib)
1547 struct smart_attr *pattr;
1552 if (!port->identify_valid) {
1553 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1556 if (!(port->identify[82] & 0x1)) {
1557 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1560 if (!(port->identify[85] & 0x1)) {
1561 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1565 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1566 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1568 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1572 pattr = (struct smart_attr *)(port->smart_buf + 2);
1573 for (i = 0; i < 29; i++, pattr++)
1574 if (pattr->attr_id == id) {
1575 memcpy(attrib, pattr, sizeof(struct smart_attr));
1580 dev_warn(&port->dd->pdev->dev,
1581 "Query for invalid SMART attribute ID\n");
1589 * Trim unused sectors
1591 * @dd pointer to driver_data structure
1593 * @len # of 512b sectors to trim
1596 * -ENOMEM Out of dma memory
1597 * -EINVAL Invalid parameters passed in, trim not supported
1598 * -EIO Error submitting trim request to hw
1600 static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
1604 u64 tlba, tlen, sect_left;
1605 struct mtip_trim_entry *buf;
1606 dma_addr_t dma_addr;
1607 struct host_to_dev_fis fis;
1609 if (!len || dd->trim_supp == false)
1612 /* Trim request too big */
1613 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1615 /* Trim request not aligned on 4k boundary */
1616 WARN_ON(len % 8 != 0);
1618 /* Warn if vu_trim structure is too big */
1619 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1621 /* Allocate a DMA buffer for the trim structure */
1622 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1626 memset(buf, 0, ATA_SECT_SIZE);
1628 for (i = 0, sect_left = len, tlba = lba;
1629 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1631 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1632 MTIP_MAX_TRIM_ENTRY_LEN :
1634 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1635 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1639 WARN_ON(sect_left != 0);
1642 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1646 fis.features = 0x60;
1648 fis.device = ATA_DEVICE_OBS;
1650 if (mtip_exec_internal_command(dd->port,
1657 MTIP_TRIM_TIMEOUT_MS) < 0)
1660 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1665 * Get the drive capacity.
1667 * @dd Pointer to the device data structure.
1668 * @sectors Pointer to the variable that will receive the sector count.
1671 * 1 Capacity was returned successfully.
1672 * 0 The identify information is invalid.
1674 static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
1676 struct mtip_port *port = dd->port;
1677 u64 total, raw0, raw1, raw2, raw3;
1678 raw0 = port->identify[100];
1679 raw1 = port->identify[101];
1680 raw2 = port->identify[102];
1681 raw3 = port->identify[103];
1682 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1684 return (bool) !!port->identify_valid;
1688 * Display the identify command data.
1690 * @port Pointer to the port data structure.
1695 static void mtip_dump_identify(struct mtip_port *port)
1698 unsigned short revid;
1701 if (!port->identify_valid)
1704 strlcpy(cbuf, (char *)(port->identify+10), 21);
1705 dev_info(&port->dd->pdev->dev,
1706 "Serial No.: %s\n", cbuf);
1708 strlcpy(cbuf, (char *)(port->identify+23), 9);
1709 dev_info(&port->dd->pdev->dev,
1710 "Firmware Ver.: %s\n", cbuf);
1712 strlcpy(cbuf, (char *)(port->identify+27), 41);
1713 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1715 dev_info(&port->dd->pdev->dev, "Security: %04x %s\n",
1716 port->identify[128],
1717 port->identify[128] & 0x4 ? "(LOCKED)" : "");
1719 if (mtip_hw_get_capacity(port->dd, §ors))
1720 dev_info(&port->dd->pdev->dev,
1721 "Capacity: %llu sectors (%llu MB)\n",
1723 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1725 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
1726 switch (revid & 0xFF) {
1728 strlcpy(cbuf, "A0", 3);
1731 strlcpy(cbuf, "A2", 3);
1734 strlcpy(cbuf, "?", 2);
1737 dev_info(&port->dd->pdev->dev,
1738 "Card Type: %s\n", cbuf);
1742 * Map the commands scatter list into the command table.
1744 * @command Pointer to the command.
1745 * @nents Number of scatter list entries.
1750 static inline void fill_command_sg(struct driver_data *dd,
1751 struct mtip_cmd *command,
1755 unsigned int dma_len;
1756 struct mtip_cmd_sg *command_sg;
1757 struct scatterlist *sg = command->sg;
1759 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1761 for (n = 0; n < nents; n++) {
1762 dma_len = sg_dma_len(sg);
1763 if (dma_len > 0x400000)
1764 dev_err(&dd->pdev->dev,
1765 "DMA segment length truncated\n");
1766 command_sg->info = __force_bit2int
1767 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1768 command_sg->dba = __force_bit2int
1769 cpu_to_le32(sg_dma_address(sg));
1770 command_sg->dba_upper = __force_bit2int
1771 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
1778 * @brief Execute a drive command.
1780 * return value 0 The command completed successfully.
1781 * return value -1 An error occurred while executing the command.
1783 static int exec_drive_task(struct mtip_port *port, u8 *command)
1785 struct host_to_dev_fis fis;
1786 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1789 /* Build the FIS. */
1790 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1793 fis.command = command[0];
1794 fis.features = command[1];
1795 fis.sect_count = command[2];
1796 fis.sector = command[3];
1797 fis.cyl_low = command[4];
1798 fis.cyl_hi = command[5];
1799 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1801 mtip_set_timeout(port->dd, &fis, &to, 0);
1803 dbg_printk(MTIP_DRV_NAME " %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
1813 /* Execute the command. */
1814 if (mtip_exec_internal_command(port,
1825 command[0] = reply->command; /* Status*/
1826 command[1] = reply->features; /* Error*/
1827 command[4] = reply->cyl_low;
1828 command[5] = reply->cyl_hi;
1830 dbg_printk(MTIP_DRV_NAME " %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
1841 * @brief Execute a drive command.
1843 * @param port Pointer to the port data structure.
1844 * @param command Pointer to the user specified command parameters.
1845 * @param user_buffer Pointer to the user space buffer where read sector
1846 * data should be copied.
1848 * return value 0 The command completed successfully.
1849 * return value -EFAULT An error occurred while copying the completion
1850 * data to the user space buffer.
1851 * return value -1 An error occurred while executing the command.
1853 static int exec_drive_command(struct mtip_port *port, u8 *command,
1854 void __user *user_buffer)
1856 struct host_to_dev_fis fis;
1857 struct host_to_dev_fis *reply;
1859 dma_addr_t dma_addr = 0;
1860 int rv = 0, xfer_sz = command[3];
1867 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1868 ATA_SECT_SIZE * xfer_sz,
1872 dev_err(&port->dd->pdev->dev,
1873 "Memory allocation failed (%d bytes)\n",
1874 ATA_SECT_SIZE * xfer_sz);
1877 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1880 /* Build the FIS. */
1881 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1884 fis.command = command[0];
1885 fis.features = command[2];
1886 fis.sect_count = command[3];
1887 if (fis.command == ATA_CMD_SMART) {
1888 fis.sector = command[1];
1893 mtip_set_timeout(port->dd, &fis, &to, 0);
1896 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1898 reply = (port->rxfis + RX_FIS_D2H_REG);
1900 dbg_printk(MTIP_DRV_NAME
1901 " %s: User Command: cmd %x, sect %x, "
1902 "feat %x, sectcnt %x\n",
1909 /* Execute the command. */
1910 if (mtip_exec_internal_command(port,
1913 (xfer_sz ? dma_addr : 0),
1914 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
1920 goto exit_drive_command;
1923 /* Collect the completion status. */
1924 command[0] = reply->command; /* Status*/
1925 command[1] = reply->features; /* Error*/
1926 command[2] = reply->sect_count;
1928 dbg_printk(MTIP_DRV_NAME
1929 " %s: Completion Status: stat %x, "
1930 "err %x, nsect %x\n",
1937 if (copy_to_user(user_buffer,
1939 ATA_SECT_SIZE * command[3])) {
1941 goto exit_drive_command;
1946 dmam_free_coherent(&port->dd->pdev->dev,
1947 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1952 * Indicates whether a command has a single sector payload.
1954 * @command passed to the device to perform the certain event.
1955 * @features passed to the device to perform the certain event.
1958 * 1 command is one that always has a single sector payload,
1959 * regardless of the value in the Sector Count field.
1963 static unsigned int implicit_sector(unsigned char command,
1964 unsigned char features)
1966 unsigned int rv = 0;
1968 /* list of commands that have an implicit sector count of 1 */
1970 case ATA_CMD_SEC_SET_PASS:
1971 case ATA_CMD_SEC_UNLOCK:
1972 case ATA_CMD_SEC_ERASE_PREP:
1973 case ATA_CMD_SEC_ERASE_UNIT:
1974 case ATA_CMD_SEC_FREEZE_LOCK:
1975 case ATA_CMD_SEC_DISABLE_PASS:
1976 case ATA_CMD_PMP_READ:
1977 case ATA_CMD_PMP_WRITE:
1980 case ATA_CMD_SET_MAX:
1981 if (features == ATA_SET_MAX_UNLOCK)
1985 if ((features == ATA_SMART_READ_VALUES) ||
1986 (features == ATA_SMART_READ_THRESHOLDS))
1989 case ATA_CMD_CONF_OVERLAY:
1990 if ((features == ATA_DCO_IDENTIFY) ||
1991 (features == ATA_DCO_SET))
1999 * Executes a taskfile
2000 * See ide_taskfile_ioctl() for derivation
2002 static int exec_drive_taskfile(struct driver_data *dd,
2004 ide_task_request_t *req_task,
2007 struct host_to_dev_fis fis;
2008 struct host_to_dev_fis *reply;
2011 dma_addr_t outbuf_dma = 0;
2012 dma_addr_t inbuf_dma = 0;
2013 dma_addr_t dma_buffer = 0;
2015 unsigned int taskin = 0;
2016 unsigned int taskout = 0;
2018 unsigned int timeout;
2019 unsigned int force_single_sector;
2020 unsigned int transfer_size;
2021 unsigned long task_file_data;
2022 int intotal = outtotal + req_task->out_size;
2025 taskout = req_task->out_size;
2026 taskin = req_task->in_size;
2027 /* 130560 = 512 * 0xFF*/
2028 if (taskin > 130560 || taskout > 130560) {
2034 outbuf = kzalloc(taskout, GFP_KERNEL);
2035 if (outbuf == NULL) {
2039 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
2043 outbuf_dma = pci_map_single(dd->pdev,
2047 if (outbuf_dma == 0) {
2051 dma_buffer = outbuf_dma;
2055 inbuf = kzalloc(taskin, GFP_KERNEL);
2056 if (inbuf == NULL) {
2061 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2065 inbuf_dma = pci_map_single(dd->pdev,
2067 taskin, DMA_FROM_DEVICE);
2068 if (inbuf_dma == 0) {
2072 dma_buffer = inbuf_dma;
2075 /* only supports PIO and non-data commands from this ioctl. */
2076 switch (req_task->data_phase) {
2078 nsect = taskout / ATA_SECT_SIZE;
2079 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2082 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2084 case TASKFILE_NO_DATA:
2085 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2092 /* Build the FIS. */
2093 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2097 fis.command = req_task->io_ports[7];
2098 fis.features = req_task->io_ports[1];
2099 fis.sect_count = req_task->io_ports[2];
2100 fis.lba_low = req_task->io_ports[3];
2101 fis.lba_mid = req_task->io_ports[4];
2102 fis.lba_hi = req_task->io_ports[5];
2103 /* Clear the dev bit*/
2104 fis.device = req_task->io_ports[6] & ~0x10;
2106 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2107 req_task->in_flags.all =
2108 IDE_TASKFILE_STD_IN_FLAGS |
2109 (IDE_HOB_STD_IN_FLAGS << 8);
2110 fis.lba_low_ex = req_task->hob_ports[3];
2111 fis.lba_mid_ex = req_task->hob_ports[4];
2112 fis.lba_hi_ex = req_task->hob_ports[5];
2113 fis.features_ex = req_task->hob_ports[1];
2114 fis.sect_cnt_ex = req_task->hob_ports[2];
2117 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2120 force_single_sector = implicit_sector(fis.command, fis.features);
2122 if ((taskin || taskout) && (!fis.sect_count)) {
2124 fis.sect_count = nsect;
2126 if (!force_single_sector) {
2127 dev_warn(&dd->pdev->dev,
2128 "data movement but "
2129 "sect_count is 0\n");
2136 dbg_printk(MTIP_DRV_NAME
2137 " %s: cmd %x, feat %x, nsect %x,"
2138 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2149 /* check for erase mode support during secure erase.*/
2150 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2151 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
2155 mtip_set_timeout(dd, &fis, &timeout, erasemode);
2157 /* Determine the correct transfer size.*/
2158 if (force_single_sector)
2159 transfer_size = ATA_SECT_SIZE;
2161 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2163 /* Execute the command.*/
2164 if (mtip_exec_internal_command(dd->port,
2176 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2178 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2179 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2180 req_task->io_ports[7] = reply->control;
2182 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2183 req_task->io_ports[7] = reply->command;
2186 /* reclaim the DMA buffers.*/
2188 pci_unmap_single(dd->pdev, inbuf_dma,
2189 taskin, DMA_FROM_DEVICE);
2191 pci_unmap_single(dd->pdev, outbuf_dma,
2192 taskout, DMA_TO_DEVICE);
2196 /* return the ATA registers to the caller.*/
2197 req_task->io_ports[1] = reply->features;
2198 req_task->io_ports[2] = reply->sect_count;
2199 req_task->io_ports[3] = reply->lba_low;
2200 req_task->io_ports[4] = reply->lba_mid;
2201 req_task->io_ports[5] = reply->lba_hi;
2202 req_task->io_ports[6] = reply->device;
2204 if (req_task->out_flags.all & 1) {
2206 req_task->hob_ports[3] = reply->lba_low_ex;
2207 req_task->hob_ports[4] = reply->lba_mid_ex;
2208 req_task->hob_ports[5] = reply->lba_hi_ex;
2209 req_task->hob_ports[1] = reply->features_ex;
2210 req_task->hob_ports[2] = reply->sect_cnt_ex;
2212 dbg_printk(MTIP_DRV_NAME
2213 " %s: Completion: stat %x,"
2214 "err %x, sect_cnt %x, lbalo %x,"
2215 "lbamid %x, lbahi %x, dev %x\n",
2217 req_task->io_ports[7],
2218 req_task->io_ports[1],
2219 req_task->io_ports[2],
2220 req_task->io_ports[3],
2221 req_task->io_ports[4],
2222 req_task->io_ports[5],
2223 req_task->io_ports[6]);
2226 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2232 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2239 pci_unmap_single(dd->pdev, inbuf_dma,
2240 taskin, DMA_FROM_DEVICE);
2242 pci_unmap_single(dd->pdev, outbuf_dma,
2243 taskout, DMA_TO_DEVICE);
2251 * Handle IOCTL calls from the Block Layer.
2253 * This function is called by the Block Layer when it receives an IOCTL
2254 * command that it does not understand. If the IOCTL command is not supported
2255 * this function returns -ENOTTY.
2257 * @dd Pointer to the driver data structure.
2258 * @cmd IOCTL command passed from the Block Layer.
2259 * @arg IOCTL argument passed from the Block Layer.
2262 * 0 The IOCTL completed successfully.
2263 * -ENOTTY The specified command is not supported.
2264 * -EFAULT An error occurred copying data to a user space buffer.
2265 * -EIO An error occurred while executing the command.
2267 static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2271 case HDIO_GET_IDENTITY:
2273 if (copy_to_user((void __user *)arg, dd->port->identify,
2274 sizeof(u16) * ATA_ID_WORDS))
2278 case HDIO_DRIVE_CMD:
2280 u8 drive_command[4];
2282 /* Copy the user command info to our buffer. */
2283 if (copy_from_user(drive_command,
2284 (void __user *) arg,
2285 sizeof(drive_command)))
2288 /* Execute the drive command. */
2289 if (exec_drive_command(dd->port,
2291 (void __user *) (arg+4)))
2294 /* Copy the status back to the users buffer. */
2295 if (copy_to_user((void __user *) arg,
2297 sizeof(drive_command)))
2302 case HDIO_DRIVE_TASK:
2304 u8 drive_command[7];
2306 /* Copy the user command info to our buffer. */
2307 if (copy_from_user(drive_command,
2308 (void __user *) arg,
2309 sizeof(drive_command)))
2312 /* Execute the drive command. */
2313 if (exec_drive_task(dd->port, drive_command))
2316 /* Copy the status back to the users buffer. */
2317 if (copy_to_user((void __user *) arg,
2319 sizeof(drive_command)))
2324 case HDIO_DRIVE_TASKFILE: {
2325 ide_task_request_t req_task;
2328 if (copy_from_user(&req_task, (void __user *) arg,
2332 outtotal = sizeof(req_task);
2334 ret = exec_drive_taskfile(dd, (void __user *) arg,
2335 &req_task, outtotal);
2337 if (copy_to_user((void __user *) arg, &req_task,
2351 * Submit an IO to the hw
2353 * This function is called by the block layer to issue an io
2354 * to the device. Upon completion, the callback function will
2355 * be called with the data parameter passed as the callback data.
2357 * @dd Pointer to the driver data structure.
2358 * @start First sector to read.
2359 * @nsect Number of sectors to read.
2360 * @nents Number of entries in scatter list for the read command.
2361 * @tag The tag of this read command.
2362 * @callback Pointer to the function that should be called
2363 * when the read completes.
2364 * @data Callback data passed to the callback function
2365 * when the read completes.
2366 * @dir Direction (read or write)
2371 static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq,
2372 struct mtip_cmd *command, int nents,
2373 struct blk_mq_hw_ctx *hctx)
2375 struct host_to_dev_fis *fis;
2376 struct mtip_port *port = dd->port;
2377 int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2378 u64 start = blk_rq_pos(rq);
2379 unsigned int nsect = blk_rq_sectors(rq);
2381 /* Map the scatter list for DMA access */
2382 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
2384 prefetch(&port->flags);
2386 command->scatter_ents = nents;
2389 * The number of retries for this command before it is
2390 * reported as a failure to the upper layers.
2392 command->retries = MTIP_MAX_RETRIES;
2395 fis = command->command;
2398 if (dma_dir == DMA_FROM_DEVICE)
2399 fis->command = ATA_CMD_FPDMA_READ;
2401 fis->command = ATA_CMD_FPDMA_WRITE;
2402 fis->lba_low = start & 0xFF;
2403 fis->lba_mid = (start >> 8) & 0xFF;
2404 fis->lba_hi = (start >> 16) & 0xFF;
2405 fis->lba_low_ex = (start >> 24) & 0xFF;
2406 fis->lba_mid_ex = (start >> 32) & 0xFF;
2407 fis->lba_hi_ex = (start >> 40) & 0xFF;
2408 fis->device = 1 << 6;
2409 fis->features = nsect & 0xFF;
2410 fis->features_ex = (nsect >> 8) & 0xFF;
2411 fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5));
2412 fis->sect_cnt_ex = 0;
2416 fill_command_sg(dd, command, nents);
2418 if (unlikely(command->unaligned))
2419 fis->device |= 1 << 7;
2421 /* Populate the command header */
2422 command->command_header->opts =
2423 __force_bit2int cpu_to_le32(
2424 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
2425 command->command_header->byte_count = 0;
2428 * Set the completion function and data for the command
2429 * within this layer.
2431 command->comp_data = dd;
2432 command->comp_func = mtip_async_complete;
2433 command->direction = dma_dir;
2436 * To prevent this command from being issued
2437 * if an internal command is in progress or error handling is active.
2439 if (unlikely(port->flags & MTIP_PF_PAUSE_IO)) {
2440 set_bit(rq->tag, port->cmds_to_issue);
2441 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
2445 /* Issue the command to the hardware */
2446 mtip_issue_ncq_command(port, rq->tag);
2450 * Sysfs status dump.
2452 * @dev Pointer to the device structure, passed by the kernrel.
2453 * @attr Pointer to the device_attribute structure passed by the kernel.
2454 * @buf Pointer to the char buffer that will receive the stats info.
2457 * The size, in bytes, of the data copied into buf.
2459 static ssize_t mtip_hw_show_status(struct device *dev,
2460 struct device_attribute *attr,
2463 struct driver_data *dd = dev_to_disk(dev)->private_data;
2466 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
2467 size += sprintf(buf, "%s", "thermal_shutdown\n");
2468 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
2469 size += sprintf(buf, "%s", "write_protect\n");
2471 size += sprintf(buf, "%s", "online\n");
2476 static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
2478 /* debugsfs entries */
2480 static ssize_t show_device_status(struct device_driver *drv, char *buf)
2483 struct driver_data *dd, *tmp;
2484 unsigned long flags;
2488 spin_lock_irqsave(&dev_lock, flags);
2489 size += sprintf(&buf[size], "Devices Present:\n");
2490 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
2493 dd->port->identify &&
2494 dd->port->identify_valid) {
2496 (char *) (dd->port->identify + 10), 21);
2497 status = *(dd->port->identify + 141);
2499 memset(id_buf, 0, 42);
2504 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2505 size += sprintf(&buf[size],
2506 " device %s %s (ftl rebuild %d %%)\n",
2507 dev_name(&dd->pdev->dev),
2511 size += sprintf(&buf[size],
2513 dev_name(&dd->pdev->dev),
2519 size += sprintf(&buf[size], "Devices Being Removed:\n");
2520 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
2523 dd->port->identify &&
2524 dd->port->identify_valid) {
2526 (char *) (dd->port->identify+10), 21);
2527 status = *(dd->port->identify + 141);
2529 memset(id_buf, 0, 42);
2534 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2535 size += sprintf(&buf[size],
2536 " device %s %s (ftl rebuild %d %%)\n",
2537 dev_name(&dd->pdev->dev),
2541 size += sprintf(&buf[size],
2543 dev_name(&dd->pdev->dev),
2548 spin_unlock_irqrestore(&dev_lock, flags);
2553 static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2554 size_t len, loff_t *offset)
2556 struct driver_data *dd = (struct driver_data *)f->private_data;
2561 if (!len || *offset)
2564 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2566 dev_err(&dd->pdev->dev,
2567 "Memory allocation: status buffer\n");
2571 size += show_device_status(NULL, buf);
2573 *offset = size <= len ? size : len;
2574 size = copy_to_user(ubuf, buf, *offset);
2579 return rv ? rv : *offset;
2582 static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2583 size_t len, loff_t *offset)
2585 struct driver_data *dd = (struct driver_data *)f->private_data;
2587 u32 group_allocated;
2594 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2596 dev_err(&dd->pdev->dev,
2597 "Memory allocation: register buffer\n");
2601 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2603 for (n = dd->slot_groups-1; n >= 0; n--)
2604 size += sprintf(&buf[size], "%08X ",
2605 readl(dd->port->s_active[n]));
2607 size += sprintf(&buf[size], "]\n");
2608 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2610 for (n = dd->slot_groups-1; n >= 0; n--)
2611 size += sprintf(&buf[size], "%08X ",
2612 readl(dd->port->cmd_issue[n]));
2614 size += sprintf(&buf[size], "]\n");
2615 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2617 for (n = dd->slot_groups-1; n >= 0; n--)
2618 size += sprintf(&buf[size], "%08X ",
2619 readl(dd->port->completed[n]));
2621 size += sprintf(&buf[size], "]\n");
2622 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2623 readl(dd->port->mmio + PORT_IRQ_STAT));
2624 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2625 readl(dd->mmio + HOST_IRQ_STAT));
2626 size += sprintf(&buf[size], "\n");
2628 size += sprintf(&buf[size], "L/ Allocated : [ 0x");
2630 for (n = dd->slot_groups-1; n >= 0; n--) {
2631 if (sizeof(long) > sizeof(u32))
2633 dd->port->allocated[n/2] >> (32*(n&1));
2635 group_allocated = dd->port->allocated[n];
2636 size += sprintf(&buf[size], "%08X ", group_allocated);
2638 size += sprintf(&buf[size], "]\n");
2640 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2642 for (n = dd->slot_groups-1; n >= 0; n--) {
2643 if (sizeof(long) > sizeof(u32))
2645 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2647 group_allocated = dd->port->cmds_to_issue[n];
2648 size += sprintf(&buf[size], "%08X ", group_allocated);
2650 size += sprintf(&buf[size], "]\n");
2652 *offset = size <= len ? size : len;
2653 size = copy_to_user(ubuf, buf, *offset);
2658 return rv ? rv : *offset;
2661 static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2662 size_t len, loff_t *offset)
2664 struct driver_data *dd = (struct driver_data *)f->private_data;
2672 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2674 dev_err(&dd->pdev->dev,
2675 "Memory allocation: flag buffer\n");
2679 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2681 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2684 *offset = size <= len ? size : len;
2685 size = copy_to_user(ubuf, buf, *offset);
2690 return rv ? rv : *offset;
2693 static const struct file_operations mtip_device_status_fops = {
2694 .owner = THIS_MODULE,
2695 .open = simple_open,
2696 .read = mtip_hw_read_device_status,
2697 .llseek = no_llseek,
2700 static const struct file_operations mtip_regs_fops = {
2701 .owner = THIS_MODULE,
2702 .open = simple_open,
2703 .read = mtip_hw_read_registers,
2704 .llseek = no_llseek,
2707 static const struct file_operations mtip_flags_fops = {
2708 .owner = THIS_MODULE,
2709 .open = simple_open,
2710 .read = mtip_hw_read_flags,
2711 .llseek = no_llseek,
2715 * Create the sysfs related attributes.
2717 * @dd Pointer to the driver data structure.
2718 * @kobj Pointer to the kobj for the block device.
2721 * 0 Operation completed successfully.
2722 * -EINVAL Invalid parameter.
2724 static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
2729 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2730 dev_warn(&dd->pdev->dev,
2731 "Error creating 'status' sysfs entry\n");
2736 * Remove the sysfs related attributes.
2738 * @dd Pointer to the driver data structure.
2739 * @kobj Pointer to the kobj for the block device.
2742 * 0 Operation completed successfully.
2743 * -EINVAL Invalid parameter.
2745 static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
2750 sysfs_remove_file(kobj, &dev_attr_status.attr);
2755 static int mtip_hw_debugfs_init(struct driver_data *dd)
2760 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2761 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2762 dev_warn(&dd->pdev->dev,
2763 "Error creating node %s under debugfs\n",
2764 dd->disk->disk_name);
2765 dd->dfs_node = NULL;
2769 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2771 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2777 static void mtip_hw_debugfs_exit(struct driver_data *dd)
2780 debugfs_remove_recursive(dd->dfs_node);
2783 static int mtip_free_orphan(struct driver_data *dd)
2785 struct kobject *kobj;
2788 if (dd->bdev->bd_holders >= 1)
2795 mtip_hw_debugfs_exit(dd);
2797 spin_lock(&rssd_index_lock);
2798 ida_remove(&rssd_index_ida, dd->index);
2799 spin_unlock(&rssd_index_lock);
2801 if (!test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag) &&
2802 test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)) {
2806 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
2808 mtip_hw_sysfs_exit(dd, kobj);
2811 del_gendisk(dd->disk);
2815 dd->queue->queuedata = NULL;
2816 blk_cleanup_queue(dd->queue);
2817 blk_mq_free_tag_set(&dd->tags);
2826 * Perform any init/resume time hardware setup
2828 * @dd Pointer to the driver data structure.
2833 static inline void hba_setup(struct driver_data *dd)
2836 hwdata = readl(dd->mmio + HOST_HSORG);
2838 /* interrupt bug workaround: use only 1 IS bit.*/
2840 HSORG_DISABLE_SLOTGRP_INTR |
2841 HSORG_DISABLE_SLOTGRP_PXIS,
2842 dd->mmio + HOST_HSORG);
2845 static int mtip_device_unaligned_constrained(struct driver_data *dd)
2847 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
2851 * Detect the details of the product, and store anything needed
2852 * into the driver data structure. This includes product type and
2853 * version and number of slot groups.
2855 * @dd Pointer to the driver data structure.
2860 static void mtip_detect_product(struct driver_data *dd)
2863 unsigned int rev, slotgroups;
2866 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2868 * [15:8] hardware/software interface rev#
2869 * [ 3] asic-style interface
2870 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2872 hwdata = readl(dd->mmio + HOST_HSORG);
2874 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2875 dd->slot_groups = 1;
2878 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2879 rev = (hwdata & HSORG_HWREV) >> 8;
2880 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2881 dev_info(&dd->pdev->dev,
2882 "ASIC-FPGA design, HS rev 0x%x, "
2883 "%i slot groups [%i slots]\n",
2888 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2889 dev_warn(&dd->pdev->dev,
2890 "Warning: driver only supports "
2891 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2892 slotgroups = MTIP_MAX_SLOT_GROUPS;
2894 dd->slot_groups = slotgroups;
2898 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2902 * Blocking wait for FTL rebuild to complete
2904 * @dd Pointer to the DRIVER_DATA structure.
2907 * 0 FTL rebuild completed successfully
2908 * -EFAULT FTL rebuild error/timeout/interruption
2910 static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2912 unsigned long timeout, cnt = 0, start;
2914 dev_warn(&dd->pdev->dev,
2915 "FTL rebuild in progress. Polling for completion.\n");
2918 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2921 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
2924 if (mtip_check_surprise_removal(dd->pdev))
2927 if (mtip_get_identify(dd->port, NULL) < 0)
2930 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2931 MTIP_FTL_REBUILD_MAGIC) {
2933 /* Print message every 3 minutes */
2935 dev_warn(&dd->pdev->dev,
2936 "FTL rebuild in progress (%d secs).\n",
2937 jiffies_to_msecs(jiffies - start) / 1000);
2941 dev_warn(&dd->pdev->dev,
2942 "FTL rebuild complete (%d secs).\n",
2943 jiffies_to_msecs(jiffies - start) / 1000);
2944 mtip_block_initialize(dd);
2948 } while (time_before(jiffies, timeout));
2950 /* Check for timeout */
2951 dev_err(&dd->pdev->dev,
2952 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2953 jiffies_to_msecs(jiffies - start) / 1000);
2958 * service thread to issue queued commands
2960 * @data Pointer to the driver data structure.
2966 static int mtip_service_thread(void *data)
2968 struct driver_data *dd = (struct driver_data *)data;
2969 unsigned long slot, slot_start, slot_wrap;
2970 unsigned int num_cmd_slots = dd->slot_groups * 32;
2971 struct mtip_port *port = dd->port;
2975 if (kthread_should_stop() ||
2976 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2978 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2981 * the condition is to check neither an internal command is
2982 * is in progress nor error handling is active
2984 wait_event_interruptible(port->svc_wait, (port->flags) &&
2985 !(port->flags & MTIP_PF_PAUSE_IO));
2987 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2989 if (kthread_should_stop() ||
2990 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2993 /* If I am an orphan, start self cleanup */
2994 if (test_bit(MTIP_PF_SR_CLEANUP_BIT, &port->flags))
2997 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3002 /* Demux bits: start with error handling */
3003 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) {
3004 mtip_handle_tfe(dd);
3005 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
3008 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags))
3011 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
3013 /* used to restrict the loop to one iteration */
3014 slot_start = num_cmd_slots;
3017 slot = find_next_bit(port->cmds_to_issue,
3018 num_cmd_slots, slot);
3019 if (slot_wrap == 1) {
3020 if ((slot_start >= slot) ||
3021 (slot >= num_cmd_slots))
3024 if (unlikely(slot_start == num_cmd_slots))
3027 if (unlikely(slot == num_cmd_slots)) {
3033 /* Issue the command to the hardware */
3034 mtip_issue_ncq_command(port, slot);
3036 clear_bit(slot, port->cmds_to_issue);
3039 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
3042 if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
3043 if (mtip_ftl_rebuild_poll(dd) < 0)
3044 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
3046 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
3050 /* wait for pci remove to exit */
3052 if (test_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag))
3054 msleep_interruptible(1000);
3055 if (kthread_should_stop())
3060 ret = mtip_free_orphan(dd);
3062 /* NOTE: All data structures are invalid, do not
3063 * access any here */
3066 msleep_interruptible(1000);
3067 if (kthread_should_stop())
3075 * DMA region teardown
3077 * @dd Pointer to driver_data structure
3082 static void mtip_dma_free(struct driver_data *dd)
3084 struct mtip_port *port = dd->port;
3087 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3088 port->block1, port->block1_dma);
3090 if (port->command_list) {
3091 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3092 port->command_list, port->command_list_dma);
3099 * @dd Pointer to driver_data structure
3102 * -ENOMEM Not enough free DMA region space to initialize driver
3104 static int mtip_dma_alloc(struct driver_data *dd)
3106 struct mtip_port *port = dd->port;
3108 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
3110 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3111 &port->block1_dma, GFP_KERNEL);
3114 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
3116 /* Allocate dma memory for command list */
3117 port->command_list =
3118 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3119 &port->command_list_dma, GFP_KERNEL);
3120 if (!port->command_list) {
3121 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3122 port->block1, port->block1_dma);
3123 port->block1 = NULL;
3124 port->block1_dma = 0;
3127 memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
3129 /* Setup all pointers into first DMA region */
3130 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
3131 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
3132 port->identify = port->block1 + AHCI_IDFY_OFFSET;
3133 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
3134 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
3135 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
3136 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
3137 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
3142 static int mtip_hw_get_identify(struct driver_data *dd)
3144 struct smart_attr attr242;
3148 if (mtip_get_identify(dd->port, NULL) < 0)
3151 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3152 MTIP_FTL_REBUILD_MAGIC) {
3153 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3154 return MTIP_FTL_REBUILD_MAGIC;
3156 mtip_dump_identify(dd->port);
3158 /* check write protect, over temp and rebuild statuses */
3159 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3161 dd->port->log_buf_dma, 1);
3163 dev_warn(&dd->pdev->dev,
3164 "Error in READ LOG EXT (10h) command\n");
3165 /* non-critical error, don't fail the load */
3167 buf = (unsigned char *)dd->port->log_buf;
3168 if (buf[259] & 0x1) {
3169 dev_info(&dd->pdev->dev,
3170 "Write protect bit is set.\n");
3171 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3173 if (buf[288] == 0xF7) {
3174 dev_info(&dd->pdev->dev,
3175 "Exceeded Tmax, drive in thermal shutdown.\n");
3176 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3178 if (buf[288] == 0xBF) {
3179 dev_info(&dd->pdev->dev,
3180 "Drive indicates rebuild has failed.\n");
3185 /* get write protect progess */
3186 memset(&attr242, 0, sizeof(struct smart_attr));
3187 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3188 dev_warn(&dd->pdev->dev,
3189 "Unable to check write protect progress\n");
3191 dev_info(&dd->pdev->dev,
3192 "Write protect progress: %u%% (%u blocks)\n",
3193 attr242.cur, le32_to_cpu(attr242.data));
3199 * Called once for each card.
3201 * @dd Pointer to the driver data structure.
3204 * 0 on success, else an error code.
3206 static int mtip_hw_init(struct driver_data *dd)
3210 unsigned int num_command_slots;
3211 unsigned long timeout, timetaken;
3213 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3215 mtip_detect_product(dd);
3216 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3220 num_command_slots = dd->slot_groups * 32;
3224 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3227 dev_err(&dd->pdev->dev,
3228 "Memory allocation: port structure\n");
3232 /* Continue workqueue setup */
3233 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3234 dd->work[i].port = dd->port;
3236 /* Enable unaligned IO constraints for some devices */
3237 if (mtip_device_unaligned_constrained(dd))
3238 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3240 dd->unal_qdepth = 0;
3242 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
3244 /* Spinlock to prevent concurrent issue */
3245 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3246 spin_lock_init(&dd->port->cmd_issue_lock[i]);
3248 /* Set the port mmio base address. */
3249 dd->port->mmio = dd->mmio + PORT_OFFSET;
3252 /* DMA allocations */
3253 rv = mtip_dma_alloc(dd);
3257 /* Setup the pointers to the extended s_active and CI registers. */
3258 for (i = 0; i < dd->slot_groups; i++) {
3259 dd->port->s_active[i] =
3260 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3261 dd->port->cmd_issue[i] =
3262 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3263 dd->port->completed[i] =
3264 dd->port->mmio + i*0x80 + PORT_SDBV;
3267 timetaken = jiffies;
3268 timeout = jiffies + msecs_to_jiffies(30000);
3269 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3270 time_before(jiffies, timeout)) {
3273 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3274 timetaken = jiffies - timetaken;
3275 dev_warn(&dd->pdev->dev,
3276 "Surprise removal detected at %u ms\n",
3277 jiffies_to_msecs(timetaken));
3281 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
3282 timetaken = jiffies - timetaken;
3283 dev_warn(&dd->pdev->dev,
3284 "Removal detected at %u ms\n",
3285 jiffies_to_msecs(timetaken));
3290 /* Conditionally reset the HBA. */
3291 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3292 if (mtip_hba_reset(dd) < 0) {
3293 dev_err(&dd->pdev->dev,
3294 "Card did not reset within timeout\n");
3299 /* Clear any pending interrupts on the HBA */
3300 writel(readl(dd->mmio + HOST_IRQ_STAT),
3301 dd->mmio + HOST_IRQ_STAT);
3304 mtip_init_port(dd->port);
3305 mtip_start_port(dd->port);
3307 /* Setup the ISR and enable interrupts. */
3308 rv = devm_request_irq(&dd->pdev->dev,
3312 dev_driver_string(&dd->pdev->dev),
3316 dev_err(&dd->pdev->dev,
3317 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3320 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
3322 /* Enable interrupts on the HBA. */
3323 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3324 dd->mmio + HOST_CTL);
3326 init_waitqueue_head(&dd->port->svc_wait);
3328 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
3336 /* Disable interrupts on the HBA. */
3337 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3338 dd->mmio + HOST_CTL);
3340 /* Release the IRQ. */
3341 irq_set_affinity_hint(dd->pdev->irq, NULL);
3342 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3345 mtip_deinit_port(dd->port);
3349 /* Free the memory allocated for the for structure. */
3355 static void mtip_standby_drive(struct driver_data *dd)
3361 * Send standby immediate (E0h) to the drive so that it
3364 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
3365 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))
3366 if (mtip_standby_immediate(dd->port))
3367 dev_warn(&dd->pdev->dev,
3368 "STANDBY IMMEDIATE failed\n");
3372 * Called to deinitialize an interface.
3374 * @dd Pointer to the driver data structure.
3379 static int mtip_hw_exit(struct driver_data *dd)
3382 * Send standby immediate (E0h) to the drive so that it
3386 /* de-initialize the port. */
3387 mtip_deinit_port(dd->port);
3389 /* Disable interrupts on the HBA. */
3390 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3391 dd->mmio + HOST_CTL);
3394 /* Release the IRQ. */
3395 irq_set_affinity_hint(dd->pdev->irq, NULL);
3396 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3398 /* Free dma regions */
3401 /* Free the memory allocated for the for structure. */
3409 * Issue a Standby Immediate command to the device.
3411 * This function is called by the Block Layer just before the
3412 * system powers off during a shutdown.
3414 * @dd Pointer to the driver data structure.
3419 static int mtip_hw_shutdown(struct driver_data *dd)
3422 * Send standby immediate (E0h) to the drive so that it
3425 if (!dd->sr && dd->port)
3426 mtip_standby_immediate(dd->port);
3434 * This function is called by the Block Layer just before the
3435 * system hibernates.
3437 * @dd Pointer to the driver data structure.
3440 * 0 Suspend was successful
3441 * -EFAULT Suspend was not successful
3443 static int mtip_hw_suspend(struct driver_data *dd)
3446 * Send standby immediate (E0h) to the drive
3447 * so that it saves its state.
3449 if (mtip_standby_immediate(dd->port) != 0) {
3450 dev_err(&dd->pdev->dev,
3451 "Failed standby-immediate command\n");
3455 /* Disable interrupts on the HBA.*/
3456 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3457 dd->mmio + HOST_CTL);
3458 mtip_deinit_port(dd->port);
3466 * This function is called by the Block Layer as the
3469 * @dd Pointer to the driver data structure.
3472 * 0 Resume was successful
3473 * -EFAULT Resume was not successful
3475 static int mtip_hw_resume(struct driver_data *dd)
3477 /* Perform any needed hardware setup steps */
3481 if (mtip_hba_reset(dd) != 0) {
3482 dev_err(&dd->pdev->dev,
3483 "Unable to reset the HBA\n");
3488 * Enable the port, DMA engine, and FIS reception specific
3489 * h/w in controller.
3491 mtip_init_port(dd->port);
3492 mtip_start_port(dd->port);
3494 /* Enable interrupts on the HBA.*/
3495 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3496 dd->mmio + HOST_CTL);
3502 * Helper function for reusing disk name
3503 * upon hot insertion.
3505 static int rssd_disk_name_format(char *prefix,
3510 const int base = 'z' - 'a' + 1;
3511 char *begin = buf + strlen(prefix);
3512 char *end = buf + buflen;
3522 *--p = 'a' + (index % unit);
3523 index = (index / unit) - 1;
3524 } while (index >= 0);
3526 memmove(begin, p, end - p);
3527 memcpy(buf, prefix, strlen(prefix));
3533 * Block layer IOCTL handler.
3535 * @dev Pointer to the block_device structure.
3537 * @cmd IOCTL command passed from the user application.
3538 * @arg Argument passed from the user application.
3541 * 0 IOCTL completed successfully.
3542 * -ENOTTY IOCTL not supported or invalid driver data
3543 * structure pointer.
3545 static int mtip_block_ioctl(struct block_device *dev,
3550 struct driver_data *dd = dev->bd_disk->private_data;
3552 if (!capable(CAP_SYS_ADMIN))
3558 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
3565 return mtip_hw_ioctl(dd, cmd, arg);
3569 #ifdef CONFIG_COMPAT
3571 * Block layer compat IOCTL handler.
3573 * @dev Pointer to the block_device structure.
3575 * @cmd IOCTL command passed from the user application.
3576 * @arg Argument passed from the user application.
3579 * 0 IOCTL completed successfully.
3580 * -ENOTTY IOCTL not supported or invalid driver data
3581 * structure pointer.
3583 static int mtip_block_compat_ioctl(struct block_device *dev,
3588 struct driver_data *dd = dev->bd_disk->private_data;
3590 if (!capable(CAP_SYS_ADMIN))
3596 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
3602 case HDIO_DRIVE_TASKFILE: {
3603 struct mtip_compat_ide_task_request_s __user *compat_req_task;
3604 ide_task_request_t req_task;
3605 int compat_tasksize, outtotal, ret;
3608 sizeof(struct mtip_compat_ide_task_request_s);
3611 (struct mtip_compat_ide_task_request_s __user *) arg;
3613 if (copy_from_user(&req_task, (void __user *) arg,
3614 compat_tasksize - (2 * sizeof(compat_long_t))))
3617 if (get_user(req_task.out_size, &compat_req_task->out_size))
3620 if (get_user(req_task.in_size, &compat_req_task->in_size))
3623 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3625 ret = exec_drive_taskfile(dd, (void __user *) arg,
3626 &req_task, outtotal);
3628 if (copy_to_user((void __user *) arg, &req_task,
3630 (2 * sizeof(compat_long_t))))
3633 if (put_user(req_task.out_size, &compat_req_task->out_size))
3636 if (put_user(req_task.in_size, &compat_req_task->in_size))
3642 return mtip_hw_ioctl(dd, cmd, arg);
3648 * Obtain the geometry of the device.
3650 * You may think that this function is obsolete, but some applications,
3651 * fdisk for example still used CHS values. This function describes the
3652 * device as having 224 heads and 56 sectors per cylinder. These values are
3653 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3654 * partition is described in terms of a start and end cylinder this means
3655 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3656 * affects performance.
3658 * @dev Pointer to the block_device strucutre.
3659 * @geo Pointer to a hd_geometry structure.
3662 * 0 Operation completed successfully.
3663 * -ENOTTY An error occurred while reading the drive capacity.
3665 static int mtip_block_getgeo(struct block_device *dev,
3666 struct hd_geometry *geo)
3668 struct driver_data *dd = dev->bd_disk->private_data;
3674 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3675 dev_warn(&dd->pdev->dev,
3676 "Could not get drive capacity.\n");
3682 sector_div(capacity, (geo->heads * geo->sectors));
3683 geo->cylinders = capacity;
3688 * Block device operation function.
3690 * This structure contains pointers to the functions required by the block
3693 static const struct block_device_operations mtip_block_ops = {
3694 .ioctl = mtip_block_ioctl,
3695 #ifdef CONFIG_COMPAT
3696 .compat_ioctl = mtip_block_compat_ioctl,
3698 .getgeo = mtip_block_getgeo,
3699 .owner = THIS_MODULE
3703 * Block layer make request function.
3705 * This function is called by the kernel to process a BIO for
3708 * @queue Pointer to the request queue. Unused other than to obtain
3709 * the driver data structure.
3710 * @rq Pointer to the request.
3713 static int mtip_submit_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
3715 struct driver_data *dd = hctx->queue->queuedata;
3716 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3719 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3720 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3724 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
3727 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3732 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)))
3734 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag))
3738 if (rq->cmd_flags & REQ_DISCARD) {
3741 err = mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq));
3742 blk_mq_end_io(rq, err);
3746 /* Create the scatter list for this request. */
3747 nents = blk_rq_map_sg(hctx->queue, rq, cmd->sg);
3749 /* Issue the read/write. */
3750 mtip_hw_submit_io(dd, rq, cmd, nents, hctx);
3754 static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx,
3757 struct driver_data *dd = hctx->queue->queuedata;
3758 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3760 if (rq_data_dir(rq) == READ || !dd->unal_qdepth)
3764 * If unaligned depth must be limited on this controller, mark it
3765 * as unaligned if the IO isn't on a 4k boundary (start of length).
3767 if (blk_rq_sectors(rq) <= 64) {
3768 if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7))
3772 if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal))
3778 static int mtip_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *rq)
3782 if (unlikely(mtip_check_unal_depth(hctx, rq)))
3783 return BLK_MQ_RQ_QUEUE_BUSY;
3785 ret = mtip_submit_request(hctx, rq);
3787 return BLK_MQ_RQ_QUEUE_OK;
3790 return BLK_MQ_RQ_QUEUE_ERROR;
3793 static void mtip_free_cmd(void *data, struct request *rq,
3794 unsigned int hctx_idx, unsigned int request_idx)
3796 struct driver_data *dd = data;
3797 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3802 dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3803 cmd->command, cmd->command_dma);
3806 static int mtip_init_cmd(void *data, struct request *rq, unsigned int hctx_idx,
3807 unsigned int request_idx, unsigned int numa_node)
3809 struct driver_data *dd = data;
3810 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3811 u32 host_cap_64 = readl(dd->mmio + HOST_CAP) & HOST_CAP_64;
3813 cmd->command = dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3814 &cmd->command_dma, GFP_KERNEL);
3818 memset(cmd->command, 0, CMD_DMA_ALLOC_SZ);
3820 /* Point the command headers at the command tables. */
3821 cmd->command_header = dd->port->command_list +
3822 (sizeof(struct mtip_cmd_hdr) * request_idx);
3823 cmd->command_header_dma = dd->port->command_list_dma +
3824 (sizeof(struct mtip_cmd_hdr) * request_idx);
3827 cmd->command_header->ctbau = __force_bit2int cpu_to_le32((cmd->command_dma >> 16) >> 16);
3829 cmd->command_header->ctba = __force_bit2int cpu_to_le32(cmd->command_dma & 0xFFFFFFFF);
3831 sg_init_table(cmd->sg, MTIP_MAX_SG);
3835 static struct blk_mq_ops mtip_mq_ops = {
3836 .queue_rq = mtip_queue_rq,
3837 .map_queue = blk_mq_map_queue,
3838 .init_request = mtip_init_cmd,
3839 .exit_request = mtip_free_cmd,
3843 * Block layer initialization function.
3845 * This function is called once by the PCI layer for each P320
3846 * device that is connected to the system.
3848 * @dd Pointer to the driver data structure.
3851 * 0 on success else an error code.
3853 static int mtip_block_initialize(struct driver_data *dd)
3855 int rv = 0, wait_for_rebuild = 0;
3857 unsigned int index = 0;
3858 struct kobject *kobj;
3859 unsigned char thd_name[16];
3862 goto skip_create_disk; /* hw init done, before rebuild */
3864 if (mtip_hw_init(dd)) {
3866 goto protocol_init_error;
3869 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
3870 if (dd->disk == NULL) {
3871 dev_err(&dd->pdev->dev,
3872 "Unable to allocate gendisk structure\n");
3874 goto alloc_disk_error;
3877 /* Generate the disk name, implemented same as in sd.c */
3879 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3882 spin_lock(&rssd_index_lock);
3883 rv = ida_get_new(&rssd_index_ida, &index);
3884 spin_unlock(&rssd_index_lock);
3885 } while (rv == -EAGAIN);
3890 rv = rssd_disk_name_format("rssd",
3892 dd->disk->disk_name,
3895 goto disk_index_error;
3897 dd->disk->driverfs_dev = &dd->pdev->dev;
3898 dd->disk->major = dd->major;
3899 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3900 dd->disk->fops = &mtip_block_ops;
3901 dd->disk->private_data = dd;
3904 mtip_hw_debugfs_init(dd);
3907 memset(&dd->tags, 0, sizeof(dd->tags));
3908 dd->tags.ops = &mtip_mq_ops;
3909 dd->tags.nr_hw_queues = 1;
3910 dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS;
3911 dd->tags.reserved_tags = 1;
3912 dd->tags.cmd_size = sizeof(struct mtip_cmd);
3913 dd->tags.numa_node = dd->numa_node;
3914 dd->tags.flags = BLK_MQ_F_SHOULD_MERGE;
3915 dd->tags.driver_data = dd;
3917 rv = blk_mq_alloc_tag_set(&dd->tags);
3919 dev_err(&dd->pdev->dev,
3920 "Unable to allocate request queue\n");
3921 goto block_queue_alloc_init_error;
3924 /* Allocate the request queue. */
3925 dd->queue = blk_mq_init_queue(&dd->tags);
3926 if (IS_ERR(dd->queue)) {
3927 dev_err(&dd->pdev->dev,
3928 "Unable to allocate request queue\n");
3930 goto block_queue_alloc_init_error;
3933 dd->disk->queue = dd->queue;
3934 dd->queue->queuedata = dd;
3936 /* Initialize the protocol layer. */
3937 wait_for_rebuild = mtip_hw_get_identify(dd);
3938 if (wait_for_rebuild < 0) {
3939 dev_err(&dd->pdev->dev,
3940 "Protocol layer initialization failed\n");
3942 goto init_hw_cmds_error;
3946 * if rebuild pending, start the service thread, and delay the block
3947 * queue creation and add_disk()
3949 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3950 goto start_service_thread;
3952 /* Set device limits. */
3953 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3954 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3955 blk_queue_physical_block_size(dd->queue, 4096);
3956 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3957 blk_queue_max_segment_size(dd->queue, 0x400000);
3958 blk_queue_io_min(dd->queue, 4096);
3959 blk_queue_bounce_limit(dd->queue, dd->pdev->dma_mask);
3962 * write back cache is not supported in the device. FUA depends on
3963 * write back cache support, hence setting flush support to zero.
3965 blk_queue_flush(dd->queue, 0);
3967 /* Signal trim support */
3968 if (dd->trim_supp == true) {
3969 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
3970 dd->queue->limits.discard_granularity = 4096;
3971 blk_queue_max_discard_sectors(dd->queue,
3972 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
3973 dd->queue->limits.discard_zeroes_data = 0;
3976 /* Set the capacity of the device in 512 byte sectors. */
3977 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3978 dev_warn(&dd->pdev->dev,
3979 "Could not read drive capacity\n");
3981 goto read_capacity_error;
3983 set_capacity(dd->disk, capacity);
3985 /* Enable the block device and add it to /dev */
3988 dd->bdev = bdget_disk(dd->disk, 0);
3990 * Now that the disk is active, initialize any sysfs attributes
3991 * managed by the protocol layer.
3993 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3995 mtip_hw_sysfs_init(dd, kobj);
3999 if (dd->mtip_svc_handler) {
4000 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
4001 return rv; /* service thread created for handling rebuild */
4004 start_service_thread:
4005 sprintf(thd_name, "mtip_svc_thd_%02d", index);
4006 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
4007 dd, dd->numa_node, "%s",
4010 if (IS_ERR(dd->mtip_svc_handler)) {
4011 dev_err(&dd->pdev->dev, "service thread failed to start\n");
4012 dd->mtip_svc_handler = NULL;
4014 goto kthread_run_error;
4016 wake_up_process(dd->mtip_svc_handler);
4017 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
4018 rv = wait_for_rebuild;
4026 /* Delete our gendisk. This also removes the device from /dev */
4027 del_gendisk(dd->disk);
4029 read_capacity_error:
4031 blk_cleanup_queue(dd->queue);
4032 blk_mq_free_tag_set(&dd->tags);
4033 block_queue_alloc_init_error:
4034 mtip_hw_debugfs_exit(dd);
4036 spin_lock(&rssd_index_lock);
4037 ida_remove(&rssd_index_ida, index);
4038 spin_unlock(&rssd_index_lock);
4044 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
4046 protocol_init_error:
4051 * Block layer deinitialization function.
4053 * Called by the PCI layer as each P320 device is removed.
4055 * @dd Pointer to the driver data structure.
4060 static int mtip_block_remove(struct driver_data *dd)
4062 struct kobject *kobj;
4065 mtip_hw_debugfs_exit(dd);
4067 if (dd->mtip_svc_handler) {
4068 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4069 wake_up_interruptible(&dd->port->svc_wait);
4070 kthread_stop(dd->mtip_svc_handler);
4073 /* Clean up the sysfs attributes, if created */
4074 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4075 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4077 mtip_hw_sysfs_exit(dd, kobj);
4082 mtip_standby_drive(dd);
4085 * Delete our gendisk structure. This also removes the device
4093 if (dd->disk->queue) {
4094 del_gendisk(dd->disk);
4095 blk_cleanup_queue(dd->queue);
4096 blk_mq_free_tag_set(&dd->tags);
4103 spin_lock(&rssd_index_lock);
4104 ida_remove(&rssd_index_ida, dd->index);
4105 spin_unlock(&rssd_index_lock);
4107 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
4108 dd->disk->disk_name);
4111 /* De-initialize the protocol layer. */
4118 * Function called by the PCI layer when just before the
4119 * machine shuts down.
4121 * If a protocol layer shutdown function is present it will be called
4124 * @dd Pointer to the driver data structure.
4129 static int mtip_block_shutdown(struct driver_data *dd)
4131 mtip_hw_shutdown(dd);
4133 /* Delete our gendisk structure, and cleanup the blk queue. */
4135 dev_info(&dd->pdev->dev,
4136 "Shutting down %s ...\n", dd->disk->disk_name);
4138 if (dd->disk->queue) {
4139 del_gendisk(dd->disk);
4140 blk_cleanup_queue(dd->queue);
4141 blk_mq_free_tag_set(&dd->tags);
4148 spin_lock(&rssd_index_lock);
4149 ida_remove(&rssd_index_ida, dd->index);
4150 spin_unlock(&rssd_index_lock);
4154 static int mtip_block_suspend(struct driver_data *dd)
4156 dev_info(&dd->pdev->dev,
4157 "Suspending %s ...\n", dd->disk->disk_name);
4158 mtip_hw_suspend(dd);
4162 static int mtip_block_resume(struct driver_data *dd)
4164 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4165 dd->disk->disk_name);
4170 static void drop_cpu(int cpu)
4175 static int get_least_used_cpu_on_node(int node)
4177 int cpu, least_used_cpu, least_cnt;
4178 const struct cpumask *node_mask;
4180 node_mask = cpumask_of_node(node);
4181 least_used_cpu = cpumask_first(node_mask);
4182 least_cnt = cpu_use[least_used_cpu];
4183 cpu = least_used_cpu;
4185 for_each_cpu(cpu, node_mask) {
4186 if (cpu_use[cpu] < least_cnt) {
4187 least_used_cpu = cpu;
4188 least_cnt = cpu_use[cpu];
4191 cpu_use[least_used_cpu]++;
4192 return least_used_cpu;
4195 /* Helper for selecting a node in round robin mode */
4196 static inline int mtip_get_next_rr_node(void)
4198 static int next_node = -1;
4200 if (next_node == -1) {
4201 next_node = first_online_node;
4205 next_node = next_online_node(next_node);
4206 if (next_node == MAX_NUMNODES)
4207 next_node = first_online_node;
4211 static DEFINE_HANDLER(0);
4212 static DEFINE_HANDLER(1);
4213 static DEFINE_HANDLER(2);
4214 static DEFINE_HANDLER(3);
4215 static DEFINE_HANDLER(4);
4216 static DEFINE_HANDLER(5);
4217 static DEFINE_HANDLER(6);
4218 static DEFINE_HANDLER(7);
4220 static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
4223 unsigned short pcie_dev_ctrl;
4225 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
4227 pci_read_config_word(pdev,
4228 pos + PCI_EXP_DEVCTL,
4230 if (pcie_dev_ctrl & (1 << 11) ||
4231 pcie_dev_ctrl & (1 << 4)) {
4232 dev_info(&dd->pdev->dev,
4233 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4234 pdev->vendor, pdev->device);
4235 pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
4236 PCI_EXP_DEVCTL_RELAX_EN);
4237 pci_write_config_word(pdev,
4238 pos + PCI_EXP_DEVCTL,
4244 static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev)
4247 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4248 * device with device id 0x5aXX
4250 if (pdev->bus && pdev->bus->self) {
4251 if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI &&
4252 ((pdev->bus->self->device & 0xff00) == 0x5a00)) {
4253 mtip_disable_link_opts(dd, pdev->bus->self);
4255 /* Check further up the topology */
4256 struct pci_dev *parent_dev = pdev->bus->self;
4257 if (parent_dev->bus &&
4258 parent_dev->bus->parent &&
4259 parent_dev->bus->parent->self &&
4260 parent_dev->bus->parent->self->vendor ==
4261 PCI_VENDOR_ID_ATI &&
4262 (parent_dev->bus->parent->self->device &
4263 0xff00) == 0x5a00) {
4264 mtip_disable_link_opts(dd,
4265 parent_dev->bus->parent->self);
4272 * Called for each supported PCI device detected.
4274 * This function allocates the private data structure, enables the
4275 * PCI device and then calls the block layer initialization function.
4278 * 0 on success else an error code.
4280 static int mtip_pci_probe(struct pci_dev *pdev,
4281 const struct pci_device_id *ent)
4284 struct driver_data *dd = NULL;
4286 const struct cpumask *node_mask;
4287 int cpu, i = 0, j = 0;
4288 int my_node = NUMA_NO_NODE;
4289 unsigned long flags;
4291 /* Allocate memory for this devices private data. */
4292 my_node = pcibus_to_node(pdev->bus);
4293 if (my_node != NUMA_NO_NODE) {
4294 if (!node_online(my_node))
4295 my_node = mtip_get_next_rr_node();
4297 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4298 my_node = mtip_get_next_rr_node();
4300 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4301 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
4302 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
4304 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
4307 "Unable to allocate memory for driver data\n");
4311 /* Attach the private data to this PCI device. */
4312 pci_set_drvdata(pdev, dd);
4314 rv = pcim_enable_device(pdev);
4316 dev_err(&pdev->dev, "Unable to enable device\n");
4320 /* Map BAR5 to memory. */
4321 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4323 dev_err(&pdev->dev, "Unable to map regions\n");
4327 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4328 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4331 rv = pci_set_consistent_dma_mask(pdev,
4334 dev_warn(&pdev->dev,
4335 "64-bit DMA enable failed\n");
4341 /* Copy the info we may need later into the private data structure. */
4342 dd->major = mtip_major;
4343 dd->instance = instance;
4345 dd->numa_node = my_node;
4347 INIT_LIST_HEAD(&dd->online_list);
4348 INIT_LIST_HEAD(&dd->remove_list);
4350 memset(dd->workq_name, 0, 32);
4351 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4353 dd->isr_workq = create_workqueue(dd->workq_name);
4354 if (!dd->isr_workq) {
4355 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
4357 goto block_initialize_err;
4360 memset(cpu_list, 0, sizeof(cpu_list));
4362 node_mask = cpumask_of_node(dd->numa_node);
4363 if (!cpumask_empty(node_mask)) {
4364 for_each_cpu(cpu, node_mask)
4366 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4367 j = strlen(cpu_list);
4370 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4372 topology_physical_package_id(cpumask_first(node_mask)),
4373 nr_cpus_node(dd->numa_node),
4376 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4378 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4379 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4380 cpu_to_node(dd->isr_binding), dd->isr_binding);
4382 /* first worker context always runs in ISR */
4383 dd->work[0].cpu_binding = dd->isr_binding;
4384 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4385 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4386 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4387 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4388 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4389 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4390 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4392 /* Log the bindings */
4393 for_each_present_cpu(cpu) {
4394 memset(cpu_list, 0, sizeof(cpu_list));
4395 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4396 if (dd->work[i].cpu_binding == cpu) {
4397 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4398 j = strlen(cpu_list);
4402 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4405 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4406 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4407 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4408 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4409 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4410 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4411 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4412 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4414 pci_set_master(pdev);
4415 rv = pci_enable_msi(pdev);
4417 dev_warn(&pdev->dev,
4418 "Unable to enable MSI interrupt.\n");
4419 goto msi_initialize_err;
4422 mtip_fix_ero_nosnoop(dd, pdev);
4424 /* Initialize the block layer. */
4425 rv = mtip_block_initialize(dd);
4428 "Unable to initialize block layer\n");
4429 goto block_initialize_err;
4433 * Increment the instance count so that each device has a unique
4437 if (rv != MTIP_FTL_REBUILD_MAGIC)
4438 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
4440 rv = 0; /* device in rebuild state, return 0 from probe */
4442 /* Add to online list even if in ftl rebuild */
4443 spin_lock_irqsave(&dev_lock, flags);
4444 list_add(&dd->online_list, &online_list);
4445 spin_unlock_irqrestore(&dev_lock, flags);
4449 block_initialize_err:
4450 pci_disable_msi(pdev);
4453 if (dd->isr_workq) {
4454 flush_workqueue(dd->isr_workq);
4455 destroy_workqueue(dd->isr_workq);
4456 drop_cpu(dd->work[0].cpu_binding);
4457 drop_cpu(dd->work[1].cpu_binding);
4458 drop_cpu(dd->work[2].cpu_binding);
4461 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4465 pci_set_drvdata(pdev, NULL);
4472 * Called for each probed device when the device is removed or the
4473 * driver is unloaded.
4478 static void mtip_pci_remove(struct pci_dev *pdev)
4480 struct driver_data *dd = pci_get_drvdata(pdev);
4481 unsigned long flags, to;
4483 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
4485 spin_lock_irqsave(&dev_lock, flags);
4486 list_del_init(&dd->online_list);
4487 list_add(&dd->remove_list, &removing_list);
4488 spin_unlock_irqrestore(&dev_lock, flags);
4490 mtip_check_surprise_removal(pdev);
4491 synchronize_irq(dd->pdev->irq);
4493 /* Spin until workers are done */
4494 to = jiffies + msecs_to_jiffies(4000);
4497 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4498 time_before(jiffies, to));
4500 if (atomic_read(&dd->irq_workers_active) != 0) {
4501 dev_warn(&dd->pdev->dev,
4502 "Completion workers still active!\n");
4505 /* Clean up the block layer. */
4506 mtip_block_remove(dd);
4508 if (dd->isr_workq) {
4509 flush_workqueue(dd->isr_workq);
4510 destroy_workqueue(dd->isr_workq);
4511 drop_cpu(dd->work[0].cpu_binding);
4512 drop_cpu(dd->work[1].cpu_binding);
4513 drop_cpu(dd->work[2].cpu_binding);
4516 pci_disable_msi(pdev);
4518 spin_lock_irqsave(&dev_lock, flags);
4519 list_del_init(&dd->remove_list);
4520 spin_unlock_irqrestore(&dev_lock, flags);
4525 set_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag);
4527 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4528 pci_set_drvdata(pdev, NULL);
4532 * Called for each probed device when the device is suspended.
4538 static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4541 struct driver_data *dd = pci_get_drvdata(pdev);
4545 "Driver private datastructure is NULL\n");
4549 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
4551 /* Disable ports & interrupts then send standby immediate */
4552 rv = mtip_block_suspend(dd);
4555 "Failed to suspend controller\n");
4560 * Save the pci config space to pdev structure &
4561 * disable the device
4563 pci_save_state(pdev);
4564 pci_disable_device(pdev);
4566 /* Move to Low power state*/
4567 pci_set_power_state(pdev, PCI_D3hot);
4573 * Called for each probed device when the device is resumed.
4579 static int mtip_pci_resume(struct pci_dev *pdev)
4582 struct driver_data *dd;
4584 dd = pci_get_drvdata(pdev);
4587 "Driver private datastructure is NULL\n");
4591 /* Move the device to active State */
4592 pci_set_power_state(pdev, PCI_D0);
4594 /* Restore PCI configuration space */
4595 pci_restore_state(pdev);
4597 /* Enable the PCI device*/
4598 rv = pcim_enable_device(pdev);
4601 "Failed to enable card during resume\n");
4604 pci_set_master(pdev);
4607 * Calls hbaReset, initPort, & startPort function
4608 * then enables interrupts
4610 rv = mtip_block_resume(dd);
4612 dev_err(&pdev->dev, "Unable to resume\n");
4615 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
4626 static void mtip_pci_shutdown(struct pci_dev *pdev)
4628 struct driver_data *dd = pci_get_drvdata(pdev);
4630 mtip_block_shutdown(dd);
4633 /* Table of device ids supported by this driver. */
4634 static const struct pci_device_id mtip_pci_tbl[] = {
4635 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4636 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4637 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4638 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4639 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4640 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4641 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
4645 /* Structure that describes the PCI driver functions. */
4646 static struct pci_driver mtip_pci_driver = {
4647 .name = MTIP_DRV_NAME,
4648 .id_table = mtip_pci_tbl,
4649 .probe = mtip_pci_probe,
4650 .remove = mtip_pci_remove,
4651 .suspend = mtip_pci_suspend,
4652 .resume = mtip_pci_resume,
4653 .shutdown = mtip_pci_shutdown,
4656 MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4659 * Module initialization function.
4661 * Called once when the module is loaded. This function allocates a major
4662 * block device number to the Cyclone devices and registers the PCI layer
4666 * 0 on success else error code.
4668 static int __init mtip_init(void)
4672 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
4674 spin_lock_init(&dev_lock);
4676 INIT_LIST_HEAD(&online_list);
4677 INIT_LIST_HEAD(&removing_list);
4679 /* Allocate a major block device number to use with this driver. */
4680 error = register_blkdev(0, MTIP_DRV_NAME);
4682 pr_err("Unable to register block device (%d)\n",
4688 dfs_parent = debugfs_create_dir("rssd", NULL);
4689 if (IS_ERR_OR_NULL(dfs_parent)) {
4690 pr_warn("Error creating debugfs parent\n");
4694 dfs_device_status = debugfs_create_file("device_status",
4695 S_IRUGO, dfs_parent, NULL,
4696 &mtip_device_status_fops);
4697 if (IS_ERR_OR_NULL(dfs_device_status)) {
4698 pr_err("Error creating device_status node\n");
4699 dfs_device_status = NULL;
4703 /* Register our PCI operations. */
4704 error = pci_register_driver(&mtip_pci_driver);
4706 debugfs_remove(dfs_parent);
4707 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4714 * Module de-initialization function.
4716 * Called once when the module is unloaded. This function deallocates
4717 * the major block device number allocated by mtip_init() and
4718 * unregisters the PCI layer of the driver.
4723 static void __exit mtip_exit(void)
4725 /* Release the allocated major block device number. */
4726 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4728 /* Unregister the PCI driver. */
4729 pci_unregister_driver(&mtip_pci_driver);
4731 debugfs_remove_recursive(dfs_parent);
4734 MODULE_AUTHOR("Micron Technology, Inc");
4735 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4736 MODULE_LICENSE("GPL");
4737 MODULE_VERSION(MTIP_DRV_VERSION);
4739 module_init(mtip_init);
4740 module_exit(mtip_exit);