1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Driver for the Micron P320 SSD
4 * Copyright (C) 2011 Micron Technology, Inc.
6 * Portions of this code were derived from works subjected to the
8 * Copyright (C) 2009 Integrated Device Technology, Inc.
11 #include <linux/pci.h>
12 #include <linux/interrupt.h>
13 #include <linux/ata.h>
14 #include <linux/delay.h>
15 #include <linux/hdreg.h>
16 #include <linux/uaccess.h>
17 #include <linux/random.h>
18 #include <linux/smp.h>
19 #include <linux/compat.h>
21 #include <linux/module.h>
22 #include <linux/blkdev.h>
23 #include <linux/blk-mq.h>
24 #include <linux/bio.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/idr.h>
27 #include <linux/kthread.h>
28 #include <../drivers/ata/ahci.h>
29 #include <linux/export.h>
30 #include <linux/debugfs.h>
31 #include <linux/prefetch.h>
32 #include <linux/numa.h>
35 #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
37 /* DMA region containing RX Fis, Identify, RLE10, and SMART buffers */
38 #define AHCI_RX_FIS_SZ 0x100
39 #define AHCI_RX_FIS_OFFSET 0x0
40 #define AHCI_IDFY_SZ ATA_SECT_SIZE
41 #define AHCI_IDFY_OFFSET 0x400
42 #define AHCI_SECTBUF_SZ ATA_SECT_SIZE
43 #define AHCI_SECTBUF_OFFSET 0x800
44 #define AHCI_SMARTBUF_SZ ATA_SECT_SIZE
45 #define AHCI_SMARTBUF_OFFSET 0xC00
46 /* 0x100 + 0x200 + 0x200 + 0x200 is smaller than 4k but we pad it out */
47 #define BLOCK_DMA_ALLOC_SZ 4096
49 /* DMA region containing command table (should be 8192 bytes) */
50 #define AHCI_CMD_SLOT_SZ sizeof(struct mtip_cmd_hdr)
51 #define AHCI_CMD_TBL_SZ (MTIP_MAX_COMMAND_SLOTS * AHCI_CMD_SLOT_SZ)
52 #define AHCI_CMD_TBL_OFFSET 0x0
54 /* DMA region per command (contains header and SGL) */
55 #define AHCI_CMD_TBL_HDR_SZ 0x80
56 #define AHCI_CMD_TBL_HDR_OFFSET 0x0
57 #define AHCI_CMD_TBL_SGL_SZ (MTIP_MAX_SG * sizeof(struct mtip_cmd_sg))
58 #define AHCI_CMD_TBL_SGL_OFFSET AHCI_CMD_TBL_HDR_SZ
59 #define CMD_DMA_ALLOC_SZ (AHCI_CMD_TBL_SGL_SZ + AHCI_CMD_TBL_HDR_SZ)
62 #define HOST_CAP_NZDMA (1 << 19)
63 #define HOST_HSORG 0xFC
64 #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
65 #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
66 #define HSORG_HWREV 0xFF00
67 #define HSORG_STYLE 0x8
68 #define HSORG_SLOTGROUPS 0x7
70 #define PORT_COMMAND_ISSUE 0x38
71 #define PORT_SDBV 0x7C
73 #define PORT_OFFSET 0x100
74 #define PORT_MEM_SIZE 0x80
76 #define PORT_IRQ_ERR \
77 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
78 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
79 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
81 #define PORT_IRQ_LEGACY \
82 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
83 #define PORT_IRQ_HANDLED \
84 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
85 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
86 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
87 #define DEF_PORT_IRQ \
88 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
91 #define MTIP_PRODUCT_UNKNOWN 0x00
92 #define MTIP_PRODUCT_ASICFPGA 0x11
94 /* Device instance number, incremented each time a device is probed. */
98 * Global variable used to hold the major block device number
99 * allocated in mtip_init().
101 static int mtip_major
;
102 static struct dentry
*dfs_parent
;
104 static u32 cpu_use
[NR_CPUS
];
106 static DEFINE_IDA(rssd_index_ida
);
108 static int mtip_block_initialize(struct driver_data
*dd
);
111 struct mtip_compat_ide_task_request_s
{
114 ide_reg_valid_t out_flags
;
115 ide_reg_valid_t in_flags
;
118 compat_ulong_t out_size
;
119 compat_ulong_t in_size
;
124 * This function check_for_surprise_removal is called
125 * while card is removed from the system and it will
126 * read the vendor id from the configuration space
128 * @pdev Pointer to the pci_dev structure.
131 * true if device removed, else false
133 static bool mtip_check_surprise_removal(struct driver_data
*dd
)
140 /* Read the vendorID from the configuration space */
141 pci_read_config_word(dd
->pdev
, 0x00, &vendor_id
);
142 if (vendor_id
== 0xFFFF) {
145 blk_mark_disk_dead(dd
->disk
);
146 return true; /* device removed */
149 return false; /* device present */
152 static struct mtip_cmd
*mtip_cmd_from_tag(struct driver_data
*dd
,
155 return blk_mq_rq_to_pdu(blk_mq_tag_to_rq(dd
->tags
.tags
[0], tag
));
159 * Reset the HBA (without sleeping)
161 * @dd Pointer to the driver data structure.
164 * 0 The reset was successful.
165 * -1 The HBA Reset bit did not clear.
167 static int mtip_hba_reset(struct driver_data
*dd
)
169 unsigned long timeout
;
171 /* Set the reset bit */
172 writel(HOST_RESET
, dd
->mmio
+ HOST_CTL
);
175 readl(dd
->mmio
+ HOST_CTL
);
178 * Spin for up to 10 seconds waiting for reset acknowledgement. Spec
179 * is 1 sec but in LUN failure conditions, up to 10 secs are required
181 timeout
= jiffies
+ msecs_to_jiffies(10000);
184 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))
187 } while ((readl(dd
->mmio
+ HOST_CTL
) & HOST_RESET
)
188 && time_before(jiffies
, timeout
));
190 if (readl(dd
->mmio
+ HOST_CTL
) & HOST_RESET
)
197 * Issue a command to the hardware.
199 * Set the appropriate bit in the s_active and Command Issue hardware
200 * registers, causing hardware command processing to begin.
202 * @port Pointer to the port structure.
203 * @tag The tag of the command to be issued.
208 static inline void mtip_issue_ncq_command(struct mtip_port
*port
, int tag
)
210 int group
= tag
>> 5;
212 /* guard SACT and CI registers */
213 spin_lock(&port
->cmd_issue_lock
[group
]);
214 writel((1 << MTIP_TAG_BIT(tag
)),
215 port
->s_active
[MTIP_TAG_INDEX(tag
)]);
216 writel((1 << MTIP_TAG_BIT(tag
)),
217 port
->cmd_issue
[MTIP_TAG_INDEX(tag
)]);
218 spin_unlock(&port
->cmd_issue_lock
[group
]);
222 * Enable/disable the reception of FIS
224 * @port Pointer to the port data structure
225 * @enable 1 to enable, 0 to disable
228 * Previous state: 1 enabled, 0 disabled
230 static int mtip_enable_fis(struct mtip_port
*port
, int enable
)
234 /* enable FIS reception */
235 tmp
= readl(port
->mmio
+ PORT_CMD
);
237 writel(tmp
| PORT_CMD_FIS_RX
, port
->mmio
+ PORT_CMD
);
239 writel(tmp
& ~PORT_CMD_FIS_RX
, port
->mmio
+ PORT_CMD
);
242 readl(port
->mmio
+ PORT_CMD
);
244 return (((tmp
& PORT_CMD_FIS_RX
) == PORT_CMD_FIS_RX
));
248 * Enable/disable the DMA engine
250 * @port Pointer to the port data structure
251 * @enable 1 to enable, 0 to disable
254 * Previous state: 1 enabled, 0 disabled.
256 static int mtip_enable_engine(struct mtip_port
*port
, int enable
)
260 /* enable FIS reception */
261 tmp
= readl(port
->mmio
+ PORT_CMD
);
263 writel(tmp
| PORT_CMD_START
, port
->mmio
+ PORT_CMD
);
265 writel(tmp
& ~PORT_CMD_START
, port
->mmio
+ PORT_CMD
);
267 readl(port
->mmio
+ PORT_CMD
);
268 return (((tmp
& PORT_CMD_START
) == PORT_CMD_START
));
272 * Enables the port DMA engine and FIS reception.
277 static inline void mtip_start_port(struct mtip_port
*port
)
279 /* Enable FIS reception */
280 mtip_enable_fis(port
, 1);
282 /* Enable the DMA engine */
283 mtip_enable_engine(port
, 1);
287 * Deinitialize a port by disabling port interrupts, the DMA engine,
290 * @port Pointer to the port structure
295 static inline void mtip_deinit_port(struct mtip_port
*port
)
297 /* Disable interrupts on this port */
298 writel(0, port
->mmio
+ PORT_IRQ_MASK
);
300 /* Disable the DMA engine */
301 mtip_enable_engine(port
, 0);
303 /* Disable FIS reception */
304 mtip_enable_fis(port
, 0);
310 * This function deinitializes the port by calling mtip_deinit_port() and
311 * then initializes it by setting the command header and RX FIS addresses,
312 * clearing the SError register and any pending port interrupts before
313 * re-enabling the default set of port interrupts.
315 * @port Pointer to the port structure.
320 static void mtip_init_port(struct mtip_port
*port
)
323 mtip_deinit_port(port
);
325 /* Program the command list base and FIS base addresses */
326 if (readl(port
->dd
->mmio
+ HOST_CAP
) & HOST_CAP_64
) {
327 writel((port
->command_list_dma
>> 16) >> 16,
328 port
->mmio
+ PORT_LST_ADDR_HI
);
329 writel((port
->rxfis_dma
>> 16) >> 16,
330 port
->mmio
+ PORT_FIS_ADDR_HI
);
331 set_bit(MTIP_PF_HOST_CAP_64
, &port
->flags
);
334 writel(port
->command_list_dma
& 0xFFFFFFFF,
335 port
->mmio
+ PORT_LST_ADDR
);
336 writel(port
->rxfis_dma
& 0xFFFFFFFF, port
->mmio
+ PORT_FIS_ADDR
);
339 writel(readl(port
->mmio
+ PORT_SCR_ERR
), port
->mmio
+ PORT_SCR_ERR
);
341 /* reset the completed registers.*/
342 for (i
= 0; i
< port
->dd
->slot_groups
; i
++)
343 writel(0xFFFFFFFF, port
->completed
[i
]);
345 /* Clear any pending interrupts for this port */
346 writel(readl(port
->mmio
+ PORT_IRQ_STAT
), port
->mmio
+ PORT_IRQ_STAT
);
348 /* Clear any pending interrupts on the HBA. */
349 writel(readl(port
->dd
->mmio
+ HOST_IRQ_STAT
),
350 port
->dd
->mmio
+ HOST_IRQ_STAT
);
352 /* Enable port interrupts */
353 writel(DEF_PORT_IRQ
, port
->mmio
+ PORT_IRQ_MASK
);
359 * @port Pointer to the port data structure.
364 static void mtip_restart_port(struct mtip_port
*port
)
366 unsigned long timeout
;
368 /* Disable the DMA engine */
369 mtip_enable_engine(port
, 0);
371 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
372 timeout
= jiffies
+ msecs_to_jiffies(500);
373 while ((readl(port
->mmio
+ PORT_CMD
) & PORT_CMD_LIST_ON
)
374 && time_before(jiffies
, timeout
))
377 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
381 * Chip quirk: escalate to hba reset if
382 * PxCMD.CR not clear after 500 ms
384 if (readl(port
->mmio
+ PORT_CMD
) & PORT_CMD_LIST_ON
) {
385 dev_warn(&port
->dd
->pdev
->dev
,
386 "PxCMD.CR not clear, escalating reset\n");
388 if (mtip_hba_reset(port
->dd
))
389 dev_err(&port
->dd
->pdev
->dev
,
390 "HBA reset escalation failed.\n");
392 /* 30 ms delay before com reset to quiesce chip */
396 dev_warn(&port
->dd
->pdev
->dev
, "Issuing COM reset\n");
399 writel(readl(port
->mmio
+ PORT_SCR_CTL
) |
400 1, port
->mmio
+ PORT_SCR_CTL
);
401 readl(port
->mmio
+ PORT_SCR_CTL
);
403 /* Wait 1 ms to quiesce chip function */
404 timeout
= jiffies
+ msecs_to_jiffies(1);
405 while (time_before(jiffies
, timeout
))
408 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
411 /* Clear PxSCTL.DET */
412 writel(readl(port
->mmio
+ PORT_SCR_CTL
) & ~1,
413 port
->mmio
+ PORT_SCR_CTL
);
414 readl(port
->mmio
+ PORT_SCR_CTL
);
416 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
417 timeout
= jiffies
+ msecs_to_jiffies(500);
418 while (((readl(port
->mmio
+ PORT_SCR_STAT
) & 0x01) == 0)
419 && time_before(jiffies
, timeout
))
422 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
425 if ((readl(port
->mmio
+ PORT_SCR_STAT
) & 0x01) == 0)
426 dev_warn(&port
->dd
->pdev
->dev
,
427 "COM reset failed\n");
429 mtip_init_port(port
);
430 mtip_start_port(port
);
434 static int mtip_device_reset(struct driver_data
*dd
)
438 if (mtip_check_surprise_removal(dd
))
441 if (mtip_hba_reset(dd
) < 0)
445 mtip_init_port(dd
->port
);
446 mtip_start_port(dd
->port
);
448 /* Enable interrupts on the HBA. */
449 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
450 dd
->mmio
+ HOST_CTL
);
455 * Helper function for tag logging
457 static void print_tags(struct driver_data
*dd
,
459 unsigned long *tagbits
,
462 unsigned char tagmap
[128];
463 int group
, tagmap_len
= 0;
465 memset(tagmap
, 0, sizeof(tagmap
));
466 for (group
= SLOTBITS_IN_LONGS
; group
> 0; group
--)
467 tagmap_len
+= sprintf(tagmap
+ tagmap_len
, "%016lX ",
469 dev_warn(&dd
->pdev
->dev
,
470 "%d command(s) %s: tagmap [%s]", cnt
, msg
, tagmap
);
473 static int mtip_read_log_page(struct mtip_port
*port
, u8 page
, u16
*buffer
,
474 dma_addr_t buffer_dma
, unsigned int sectors
);
475 static int mtip_get_smart_attr(struct mtip_port
*port
, unsigned int id
,
476 struct smart_attr
*attrib
);
478 static void mtip_complete_command(struct mtip_cmd
*cmd
, blk_status_t status
)
480 struct request
*req
= blk_mq_rq_from_pdu(cmd
);
482 cmd
->status
= status
;
483 if (likely(!blk_should_fake_timeout(req
->q
)))
484 blk_mq_complete_request(req
);
490 * @dd Pointer to the DRIVER_DATA structure.
495 static void mtip_handle_tfe(struct driver_data
*dd
)
497 int group
, tag
, bit
, reissue
, rv
;
498 struct mtip_port
*port
;
499 struct mtip_cmd
*cmd
;
501 struct host_to_dev_fis
*fis
;
502 unsigned long tagaccum
[SLOTBITS_IN_LONGS
];
503 unsigned int cmd_cnt
= 0;
505 char *fail_reason
= NULL
;
506 int fail_all_ncq_write
= 0, fail_all_ncq_cmds
= 0;
508 dev_warn(&dd
->pdev
->dev
, "Taskfile error\n");
512 if (test_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
)) {
513 cmd
= mtip_cmd_from_tag(dd
, MTIP_TAG_INTERNAL
);
514 dbg_printk(MTIP_DRV_NAME
" TFE for the internal command\n");
515 mtip_complete_command(cmd
, BLK_STS_IOERR
);
519 /* clear the tag accumulator */
520 memset(tagaccum
, 0, SLOTBITS_IN_LONGS
* sizeof(long));
522 /* Loop through all the groups */
523 for (group
= 0; group
< dd
->slot_groups
; group
++) {
524 completed
= readl(port
->completed
[group
]);
526 dev_warn(&dd
->pdev
->dev
, "g=%u, comp=%x\n", group
, completed
);
528 /* clear completed status register in the hardware.*/
529 writel(completed
, port
->completed
[group
]);
531 /* Process successfully completed commands */
532 for (bit
= 0; bit
< 32 && completed
; bit
++) {
533 if (!(completed
& (1<<bit
)))
535 tag
= (group
<< 5) + bit
;
537 /* Skip the internal command slot */
538 if (tag
== MTIP_TAG_INTERNAL
)
541 cmd
= mtip_cmd_from_tag(dd
, tag
);
542 mtip_complete_command(cmd
, 0);
543 set_bit(tag
, tagaccum
);
548 print_tags(dd
, "completed (TFE)", tagaccum
, cmd_cnt
);
550 /* Restart the port */
552 mtip_restart_port(port
);
554 /* Trying to determine the cause of the error */
555 rv
= mtip_read_log_page(dd
->port
, ATA_LOG_SATA_NCQ
,
557 dd
->port
->log_buf_dma
, 1);
559 dev_warn(&dd
->pdev
->dev
,
560 "Error in READ LOG EXT (10h) command\n");
561 /* non-critical error, don't fail the load */
563 buf
= (unsigned char *)dd
->port
->log_buf
;
564 if (buf
[259] & 0x1) {
565 dev_info(&dd
->pdev
->dev
,
566 "Write protect bit is set.\n");
567 set_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
);
568 fail_all_ncq_write
= 1;
569 fail_reason
= "write protect";
571 if (buf
[288] == 0xF7) {
572 dev_info(&dd
->pdev
->dev
,
573 "Exceeded Tmax, drive in thermal shutdown.\n");
574 set_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
);
575 fail_all_ncq_cmds
= 1;
576 fail_reason
= "thermal shutdown";
578 if (buf
[288] == 0xBF) {
579 set_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
);
580 dev_info(&dd
->pdev
->dev
,
581 "Drive indicates rebuild has failed. Secure erase required.\n");
582 fail_all_ncq_cmds
= 1;
583 fail_reason
= "rebuild failed";
587 /* clear the tag accumulator */
588 memset(tagaccum
, 0, SLOTBITS_IN_LONGS
* sizeof(long));
590 /* Loop through all the groups */
591 for (group
= 0; group
< dd
->slot_groups
; group
++) {
592 for (bit
= 0; bit
< 32; bit
++) {
594 tag
= (group
<< 5) + bit
;
595 cmd
= mtip_cmd_from_tag(dd
, tag
);
597 fis
= (struct host_to_dev_fis
*)cmd
->command
;
599 /* Should re-issue? */
600 if (tag
== MTIP_TAG_INTERNAL
||
601 fis
->command
== ATA_CMD_SET_FEATURES
)
604 if (fail_all_ncq_cmds
||
605 (fail_all_ncq_write
&&
606 fis
->command
== ATA_CMD_FPDMA_WRITE
)) {
607 dev_warn(&dd
->pdev
->dev
,
608 " Fail: %s w/tag %d [%s].\n",
609 fis
->command
== ATA_CMD_FPDMA_WRITE
?
612 fail_reason
!= NULL
?
613 fail_reason
: "unknown");
614 mtip_complete_command(cmd
, BLK_STS_MEDIUM
);
620 * First check if this command has
621 * exceeded its retries.
623 if (reissue
&& (cmd
->retries
-- > 0)) {
625 set_bit(tag
, tagaccum
);
627 /* Re-issue the command. */
628 mtip_issue_ncq_command(port
, tag
);
633 /* Retire a command that will not be reissued */
634 dev_warn(&port
->dd
->pdev
->dev
,
635 "retiring tag %d\n", tag
);
637 mtip_complete_command(cmd
, BLK_STS_IOERR
);
640 print_tags(dd
, "reissued (TFE)", tagaccum
, cmd_cnt
);
644 * Handle a set device bits interrupt
646 static inline void mtip_workq_sdbfx(struct mtip_port
*port
, int group
,
649 struct driver_data
*dd
= port
->dd
;
651 struct mtip_cmd
*command
;
654 WARN_ON_ONCE(!completed
);
657 /* clear completed status register in the hardware.*/
658 writel(completed
, port
->completed
[group
]);
660 /* Process completed commands. */
661 for (bit
= 0; (bit
< 32) && completed
; bit
++) {
662 if (completed
& 0x01) {
663 tag
= (group
<< 5) | bit
;
665 /* skip internal command slot. */
666 if (unlikely(tag
== MTIP_TAG_INTERNAL
))
669 command
= mtip_cmd_from_tag(dd
, tag
);
670 mtip_complete_command(command
, 0);
675 /* If last, re-enable interrupts */
676 if (atomic_dec_return(&dd
->irq_workers_active
) == 0)
677 writel(0xffffffff, dd
->mmio
+ HOST_IRQ_STAT
);
681 * Process legacy pio and d2h interrupts
683 static inline void mtip_process_legacy(struct driver_data
*dd
, u32 port_stat
)
685 struct mtip_port
*port
= dd
->port
;
686 struct mtip_cmd
*cmd
= mtip_cmd_from_tag(dd
, MTIP_TAG_INTERNAL
);
688 if (test_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
) && cmd
) {
689 int group
= MTIP_TAG_INDEX(MTIP_TAG_INTERNAL
);
690 int status
= readl(port
->cmd_issue
[group
]);
692 if (!(status
& (1 << MTIP_TAG_BIT(MTIP_TAG_INTERNAL
))))
693 mtip_complete_command(cmd
, 0);
698 * Demux and handle errors
700 static inline void mtip_process_errors(struct driver_data
*dd
, u32 port_stat
)
702 if (unlikely(port_stat
& PORT_IRQ_CONNECT
)) {
703 dev_warn(&dd
->pdev
->dev
,
704 "Clearing PxSERR.DIAG.x\n");
705 writel((1 << 26), dd
->port
->mmio
+ PORT_SCR_ERR
);
708 if (unlikely(port_stat
& PORT_IRQ_PHYRDY
)) {
709 dev_warn(&dd
->pdev
->dev
,
710 "Clearing PxSERR.DIAG.n\n");
711 writel((1 << 16), dd
->port
->mmio
+ PORT_SCR_ERR
);
714 if (unlikely(port_stat
& ~PORT_IRQ_HANDLED
)) {
715 dev_warn(&dd
->pdev
->dev
,
716 "Port stat errors %x unhandled\n",
717 (port_stat
& ~PORT_IRQ_HANDLED
));
718 if (mtip_check_surprise_removal(dd
))
721 if (likely(port_stat
& (PORT_IRQ_TF_ERR
| PORT_IRQ_IF_ERR
))) {
722 set_bit(MTIP_PF_EH_ACTIVE_BIT
, &dd
->port
->flags
);
723 wake_up_interruptible(&dd
->port
->svc_wait
);
727 static inline irqreturn_t
mtip_handle_irq(struct driver_data
*data
)
729 struct driver_data
*dd
= (struct driver_data
*) data
;
730 struct mtip_port
*port
= dd
->port
;
731 u32 hba_stat
, port_stat
;
733 int do_irq_enable
= 1, i
, workers
;
734 struct mtip_work
*twork
;
736 hba_stat
= readl(dd
->mmio
+ HOST_IRQ_STAT
);
740 /* Acknowledge the interrupt status on the port.*/
741 port_stat
= readl(port
->mmio
+ PORT_IRQ_STAT
);
742 if (unlikely(port_stat
== 0xFFFFFFFF)) {
743 mtip_check_surprise_removal(dd
);
746 writel(port_stat
, port
->mmio
+ PORT_IRQ_STAT
);
748 /* Demux port status */
749 if (likely(port_stat
& PORT_IRQ_SDB_FIS
)) {
751 WARN_ON_ONCE(atomic_read(&dd
->irq_workers_active
) != 0);
753 /* Start at 1: group zero is always local? */
754 for (i
= 0, workers
= 0; i
< MTIP_MAX_SLOT_GROUPS
;
756 twork
= &dd
->work
[i
];
757 twork
->completed
= readl(port
->completed
[i
]);
758 if (twork
->completed
)
762 atomic_set(&dd
->irq_workers_active
, workers
);
764 for (i
= 1; i
< MTIP_MAX_SLOT_GROUPS
; i
++) {
765 twork
= &dd
->work
[i
];
766 if (twork
->completed
)
773 if (likely(dd
->work
[0].completed
))
774 mtip_workq_sdbfx(port
, 0,
775 dd
->work
[0].completed
);
779 * Chip quirk: SDB interrupt but nothing
786 if (unlikely(port_stat
& PORT_IRQ_ERR
)) {
787 if (unlikely(mtip_check_surprise_removal(dd
))) {
788 /* don't proceed further */
791 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
795 mtip_process_errors(dd
, port_stat
& PORT_IRQ_ERR
);
798 if (unlikely(port_stat
& PORT_IRQ_LEGACY
))
799 mtip_process_legacy(dd
, port_stat
& PORT_IRQ_LEGACY
);
802 /* acknowledge interrupt */
803 if (unlikely(do_irq_enable
))
804 writel(hba_stat
, dd
->mmio
+ HOST_IRQ_STAT
);
810 * HBA interrupt subroutine.
813 * @instance Pointer to the driver data structure.
816 * IRQ_HANDLED A HBA interrupt was pending and handled.
817 * IRQ_NONE This interrupt was not for the HBA.
819 static irqreturn_t
mtip_irq_handler(int irq
, void *instance
)
821 struct driver_data
*dd
= instance
;
823 return mtip_handle_irq(dd
);
826 static void mtip_issue_non_ncq_command(struct mtip_port
*port
, int tag
)
828 writel(1 << MTIP_TAG_BIT(tag
), port
->cmd_issue
[MTIP_TAG_INDEX(tag
)]);
831 static bool mtip_pause_ncq(struct mtip_port
*port
,
832 struct host_to_dev_fis
*fis
)
834 unsigned long task_file_data
;
836 task_file_data
= readl(port
->mmio
+PORT_TFDATA
);
837 if ((task_file_data
& 1))
840 if (fis
->command
== ATA_CMD_SEC_ERASE_PREP
) {
841 port
->ic_pause_timer
= jiffies
;
843 } else if ((fis
->command
== ATA_CMD_DOWNLOAD_MICRO
) &&
844 (fis
->features
== 0x03)) {
845 set_bit(MTIP_PF_DM_ACTIVE_BIT
, &port
->flags
);
846 port
->ic_pause_timer
= jiffies
;
848 } else if ((fis
->command
== ATA_CMD_SEC_ERASE_UNIT
) ||
849 ((fis
->command
== 0xFC) &&
850 (fis
->features
== 0x27 || fis
->features
== 0x72 ||
851 fis
->features
== 0x62 || fis
->features
== 0x26))) {
852 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
853 clear_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &port
->dd
->dd_flag
);
854 /* Com reset after secure erase or lowlevel format */
855 mtip_restart_port(port
);
856 clear_bit(MTIP_PF_SE_ACTIVE_BIT
, &port
->flags
);
863 static bool mtip_commands_active(struct mtip_port
*port
)
869 * Ignore s_active bit 0 of array element 0.
870 * This bit will always be set
872 active
= readl(port
->s_active
[0]) & 0xFFFFFFFE;
873 for (n
= 1; n
< port
->dd
->slot_groups
; n
++)
874 active
|= readl(port
->s_active
[n
]);
880 * Wait for port to quiesce
882 * @port Pointer to port data structure
883 * @timeout Max duration to wait (ms)
887 * -EBUSY Commands still active
889 static int mtip_quiesce_io(struct mtip_port
*port
, unsigned long timeout
)
894 blk_mq_quiesce_queue(port
->dd
->queue
);
896 to
= jiffies
+ msecs_to_jiffies(timeout
);
898 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
) &&
899 test_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
)) {
901 continue; /* svc thd is actively issuing commands */
906 if (mtip_check_surprise_removal(port
->dd
))
909 active
= mtip_commands_active(port
);
912 } while (time_before(jiffies
, to
));
914 blk_mq_unquiesce_queue(port
->dd
->queue
);
915 return active
? -EBUSY
: 0;
917 blk_mq_unquiesce_queue(port
->dd
->queue
);
921 struct mtip_int_cmd
{
929 * Execute an internal command and wait for the completion.
931 * @port Pointer to the port data structure.
932 * @fis Pointer to the FIS that describes the command.
933 * @fis_len Length in WORDS of the FIS.
934 * @buffer DMA accessible for command data.
935 * @buf_len Length, in bytes, of the data buffer.
936 * @opts Command header options, excluding the FIS length
937 * and the number of PRD entries.
938 * @timeout Time in ms to wait for the command to complete.
941 * 0 Command completed successfully.
942 * -EFAULT The buffer address is not correctly aligned.
943 * -EBUSY Internal command or other IO in progress.
944 * -EAGAIN Time out waiting for command to complete.
946 static int mtip_exec_internal_command(struct mtip_port
*port
,
947 struct host_to_dev_fis
*fis
,
952 unsigned long timeout
)
954 struct mtip_cmd
*int_cmd
;
955 struct driver_data
*dd
= port
->dd
;
957 struct mtip_int_cmd icmd
= {
965 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
966 if (buffer
& 0x00000007) {
967 dev_err(&dd
->pdev
->dev
, "SG buffer is not 8 byte aligned\n");
971 if (mtip_check_surprise_removal(dd
))
974 rq
= blk_mq_alloc_request(dd
->queue
, REQ_OP_DRV_IN
, BLK_MQ_REQ_RESERVED
);
976 dbg_printk(MTIP_DRV_NAME
"Unable to allocate tag for PIO cmd\n");
980 set_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
982 if (fis
->command
== ATA_CMD_SEC_ERASE_PREP
)
983 set_bit(MTIP_PF_SE_ACTIVE_BIT
, &port
->flags
);
985 clear_bit(MTIP_PF_DM_ACTIVE_BIT
, &port
->flags
);
987 if (fis
->command
!= ATA_CMD_STANDBYNOW1
) {
988 /* wait for io to complete if non atomic */
989 if (mtip_quiesce_io(port
, MTIP_QUIESCE_IO_TIMEOUT_MS
) < 0) {
990 dev_warn(&dd
->pdev
->dev
, "Failed to quiesce IO\n");
991 blk_mq_free_request(rq
);
992 clear_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
993 wake_up_interruptible(&port
->svc_wait
);
998 /* Copy the command to the command table */
999 int_cmd
= blk_mq_rq_to_pdu(rq
);
1000 int_cmd
->icmd
= &icmd
;
1001 memcpy(int_cmd
->command
, fis
, fis_len
*4);
1003 rq
->timeout
= timeout
;
1005 /* insert request and run queue */
1006 blk_execute_rq(rq
, true);
1008 if (int_cmd
->status
) {
1009 dev_err(&dd
->pdev
->dev
, "Internal command [%02X] failed %d\n",
1010 fis
->command
, int_cmd
->status
);
1013 if (mtip_check_surprise_removal(dd
) ||
1014 test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
1016 dev_err(&dd
->pdev
->dev
,
1017 "Internal command [%02X] wait returned due to SR\n",
1022 mtip_device_reset(dd
); /* recover from timeout issue */
1027 if (readl(port
->cmd_issue
[MTIP_TAG_INDEX(MTIP_TAG_INTERNAL
)])
1028 & (1 << MTIP_TAG_BIT(MTIP_TAG_INTERNAL
))) {
1030 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)) {
1031 mtip_device_reset(dd
);
1036 /* Clear the allocated and active bits for the internal command. */
1037 blk_mq_free_request(rq
);
1038 clear_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1039 if (rv
>= 0 && mtip_pause_ncq(port
, fis
)) {
1043 wake_up_interruptible(&port
->svc_wait
);
1049 * Byte-swap ATA ID strings.
1051 * ATA identify data contains strings in byte-swapped 16-bit words.
1052 * They must be swapped (on all architectures) to be usable as C strings.
1053 * This function swaps bytes in-place.
1055 * @buf The buffer location of the string
1056 * @len The number of bytes to swap
1061 static inline void ata_swap_string(u16
*buf
, unsigned int len
)
1064 for (i
= 0; i
< (len
/2); i
++)
1065 be16_to_cpus(&buf
[i
]);
1068 static void mtip_set_timeout(struct driver_data
*dd
,
1069 struct host_to_dev_fis
*fis
,
1070 unsigned int *timeout
, u8 erasemode
)
1072 switch (fis
->command
) {
1073 case ATA_CMD_DOWNLOAD_MICRO
:
1074 *timeout
= 120000; /* 2 minutes */
1076 case ATA_CMD_SEC_ERASE_UNIT
:
1079 *timeout
= ((*(dd
->port
->identify
+ 90) * 2) * 60000);
1081 *timeout
= ((*(dd
->port
->identify
+ 89) * 2) * 60000);
1083 case ATA_CMD_STANDBYNOW1
:
1084 *timeout
= 120000; /* 2 minutes */
1088 *timeout
= 60000; /* 60 seconds */
1091 *timeout
= 15000; /* 15 seconds */
1094 *timeout
= MTIP_IOCTL_CMD_TIMEOUT_MS
;
1100 * Request the device identity information.
1102 * If a user space buffer is not specified, i.e. is NULL, the
1103 * identify information is still read from the drive and placed
1104 * into the identify data buffer (@e port->identify) in the
1105 * port data structure.
1106 * When the identify buffer contains valid identify information @e
1107 * port->identify_valid is non-zero.
1109 * @port Pointer to the port structure.
1110 * @user_buffer A user space buffer where the identify data should be
1114 * 0 Command completed successfully.
1115 * -EFAULT An error occurred while coping data to the user buffer.
1116 * -1 Command failed.
1118 static int mtip_get_identify(struct mtip_port
*port
, void __user
*user_buffer
)
1121 struct host_to_dev_fis fis
;
1123 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
1126 /* Build the FIS. */
1127 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1130 fis
.command
= ATA_CMD_ID_ATA
;
1132 /* Set the identify information as invalid. */
1133 port
->identify_valid
= 0;
1135 /* Clear the identify information. */
1136 memset(port
->identify
, 0, sizeof(u16
) * ATA_ID_WORDS
);
1138 /* Execute the command. */
1139 if (mtip_exec_internal_command(port
,
1143 sizeof(u16
) * ATA_ID_WORDS
,
1145 MTIP_INT_CMD_TIMEOUT_MS
)
1152 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1153 * perform field-sensitive swapping on the string fields.
1154 * See the kernel use of ata_id_string() for proof of this.
1156 #ifdef __LITTLE_ENDIAN
1157 ata_swap_string(port
->identify
+ 27, 40); /* model string*/
1158 ata_swap_string(port
->identify
+ 23, 8); /* firmware string*/
1159 ata_swap_string(port
->identify
+ 10, 20); /* serial# string*/
1163 for (i
= 0; i
< ATA_ID_WORDS
; i
++)
1164 port
->identify
[i
] = le16_to_cpu(port
->identify
[i
]);
1168 /* Check security locked state */
1169 if (port
->identify
[128] & 0x4)
1170 set_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
1172 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
1174 /* Set the identify buffer as valid. */
1175 port
->identify_valid
= 1;
1181 ATA_ID_WORDS
* sizeof(u16
))) {
1192 * Issue a standby immediate command to the device.
1194 * @port Pointer to the port structure.
1197 * 0 Command was executed successfully.
1198 * -1 An error occurred while executing the command.
1200 static int mtip_standby_immediate(struct mtip_port
*port
)
1203 struct host_to_dev_fis fis
;
1204 unsigned long __maybe_unused start
;
1205 unsigned int timeout
;
1207 /* Build the FIS. */
1208 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1211 fis
.command
= ATA_CMD_STANDBYNOW1
;
1213 mtip_set_timeout(port
->dd
, &fis
, &timeout
, 0);
1216 rv
= mtip_exec_internal_command(port
,
1223 dbg_printk(MTIP_DRV_NAME
"Time taken to complete standby cmd: %d ms\n",
1224 jiffies_to_msecs(jiffies
- start
));
1226 dev_warn(&port
->dd
->pdev
->dev
,
1227 "STANDBY IMMEDIATE command failed.\n");
1233 * Issue a READ LOG EXT command to the device.
1235 * @port pointer to the port structure.
1236 * @page page number to fetch
1237 * @buffer pointer to buffer
1238 * @buffer_dma dma address corresponding to @buffer
1239 * @sectors page length to fetch, in sectors
1242 * @rv return value from mtip_exec_internal_command()
1244 static int mtip_read_log_page(struct mtip_port
*port
, u8 page
, u16
*buffer
,
1245 dma_addr_t buffer_dma
, unsigned int sectors
)
1247 struct host_to_dev_fis fis
;
1249 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1252 fis
.command
= ATA_CMD_READ_LOG_EXT
;
1253 fis
.sect_count
= sectors
& 0xFF;
1254 fis
.sect_cnt_ex
= (sectors
>> 8) & 0xFF;
1257 fis
.device
= ATA_DEVICE_OBS
;
1259 memset(buffer
, 0, sectors
* ATA_SECT_SIZE
);
1261 return mtip_exec_internal_command(port
,
1265 sectors
* ATA_SECT_SIZE
,
1267 MTIP_INT_CMD_TIMEOUT_MS
);
1271 * Issue a SMART READ DATA command to the device.
1273 * @port pointer to the port structure.
1274 * @buffer pointer to buffer
1275 * @buffer_dma dma address corresponding to @buffer
1278 * @rv return value from mtip_exec_internal_command()
1280 static int mtip_get_smart_data(struct mtip_port
*port
, u8
*buffer
,
1281 dma_addr_t buffer_dma
)
1283 struct host_to_dev_fis fis
;
1285 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1288 fis
.command
= ATA_CMD_SMART
;
1289 fis
.features
= 0xD0;
1293 fis
.device
= ATA_DEVICE_OBS
;
1295 return mtip_exec_internal_command(port
,
1305 * Get the value of a smart attribute
1307 * @port pointer to the port structure
1308 * @id attribute number
1309 * @attrib pointer to return attrib information corresponding to @id
1312 * -EINVAL NULL buffer passed or unsupported attribute @id.
1313 * -EPERM Identify data not valid, SMART not supported or not enabled
1315 static int mtip_get_smart_attr(struct mtip_port
*port
, unsigned int id
,
1316 struct smart_attr
*attrib
)
1319 struct smart_attr
*pattr
;
1324 if (!port
->identify_valid
) {
1325 dev_warn(&port
->dd
->pdev
->dev
, "IDENTIFY DATA not valid\n");
1328 if (!(port
->identify
[82] & 0x1)) {
1329 dev_warn(&port
->dd
->pdev
->dev
, "SMART not supported\n");
1332 if (!(port
->identify
[85] & 0x1)) {
1333 dev_warn(&port
->dd
->pdev
->dev
, "SMART not enabled\n");
1337 memset(port
->smart_buf
, 0, ATA_SECT_SIZE
);
1338 rv
= mtip_get_smart_data(port
, port
->smart_buf
, port
->smart_buf_dma
);
1340 dev_warn(&port
->dd
->pdev
->dev
, "Failed to ge SMART data\n");
1344 pattr
= (struct smart_attr
*)(port
->smart_buf
+ 2);
1345 for (i
= 0; i
< 29; i
++, pattr
++)
1346 if (pattr
->attr_id
== id
) {
1347 memcpy(attrib
, pattr
, sizeof(struct smart_attr
));
1352 dev_warn(&port
->dd
->pdev
->dev
,
1353 "Query for invalid SMART attribute ID\n");
1361 * Get the drive capacity.
1363 * @dd Pointer to the device data structure.
1364 * @sectors Pointer to the variable that will receive the sector count.
1367 * 1 Capacity was returned successfully.
1368 * 0 The identify information is invalid.
1370 static bool mtip_hw_get_capacity(struct driver_data
*dd
, sector_t
*sectors
)
1372 struct mtip_port
*port
= dd
->port
;
1373 u64 total
, raw0
, raw1
, raw2
, raw3
;
1374 raw0
= port
->identify
[100];
1375 raw1
= port
->identify
[101];
1376 raw2
= port
->identify
[102];
1377 raw3
= port
->identify
[103];
1378 total
= raw0
| raw1
<<16 | raw2
<<32 | raw3
<<48;
1380 return (bool) !!port
->identify_valid
;
1384 * Display the identify command data.
1386 * @port Pointer to the port data structure.
1391 static void mtip_dump_identify(struct mtip_port
*port
)
1394 unsigned short revid
;
1397 if (!port
->identify_valid
)
1400 strscpy(cbuf
, (char *)(port
->identify
+ 10), 21);
1401 dev_info(&port
->dd
->pdev
->dev
,
1402 "Serial No.: %s\n", cbuf
);
1404 strscpy(cbuf
, (char *)(port
->identify
+ 23), 9);
1405 dev_info(&port
->dd
->pdev
->dev
,
1406 "Firmware Ver.: %s\n", cbuf
);
1408 strscpy(cbuf
, (char *)(port
->identify
+ 27), 41);
1409 dev_info(&port
->dd
->pdev
->dev
, "Model: %s\n", cbuf
);
1411 dev_info(&port
->dd
->pdev
->dev
, "Security: %04x %s\n",
1412 port
->identify
[128],
1413 port
->identify
[128] & 0x4 ? "(LOCKED)" : "");
1415 if (mtip_hw_get_capacity(port
->dd
, §ors
))
1416 dev_info(&port
->dd
->pdev
->dev
,
1417 "Capacity: %llu sectors (%llu MB)\n",
1419 ((u64
)sectors
) * ATA_SECT_SIZE
>> 20);
1421 pci_read_config_word(port
->dd
->pdev
, PCI_REVISION_ID
, &revid
);
1422 switch (revid
& 0xFF) {
1424 strscpy(cbuf
, "A0", 3);
1427 strscpy(cbuf
, "A2", 3);
1430 strscpy(cbuf
, "?", 2);
1433 dev_info(&port
->dd
->pdev
->dev
,
1434 "Card Type: %s\n", cbuf
);
1438 * Map the commands scatter list into the command table.
1440 * @command Pointer to the command.
1441 * @nents Number of scatter list entries.
1446 static inline void fill_command_sg(struct driver_data
*dd
,
1447 struct mtip_cmd
*command
,
1451 unsigned int dma_len
;
1452 struct mtip_cmd_sg
*command_sg
;
1453 struct scatterlist
*sg
;
1455 command_sg
= command
->command
+ AHCI_CMD_TBL_HDR_SZ
;
1457 for_each_sg(command
->sg
, sg
, nents
, n
) {
1458 dma_len
= sg_dma_len(sg
);
1459 if (dma_len
> 0x400000)
1460 dev_err(&dd
->pdev
->dev
,
1461 "DMA segment length truncated\n");
1462 command_sg
->info
= cpu_to_le32((dma_len
-1) & 0x3FFFFF);
1463 command_sg
->dba
= cpu_to_le32(sg_dma_address(sg
));
1464 command_sg
->dba_upper
=
1465 cpu_to_le32((sg_dma_address(sg
) >> 16) >> 16);
1471 * @brief Execute a drive command.
1473 * return value 0 The command completed successfully.
1474 * return value -1 An error occurred while executing the command.
1476 static int exec_drive_task(struct mtip_port
*port
, u8
*command
)
1478 struct host_to_dev_fis fis
;
1479 struct host_to_dev_fis
*reply
= (port
->rxfis
+ RX_FIS_D2H_REG
);
1482 /* Build the FIS. */
1483 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1486 fis
.command
= command
[0];
1487 fis
.features
= command
[1];
1488 fis
.sect_count
= command
[2];
1489 fis
.sector
= command
[3];
1490 fis
.cyl_low
= command
[4];
1491 fis
.cyl_hi
= command
[5];
1492 fis
.device
= command
[6] & ~0x10; /* Clear the dev bit*/
1494 mtip_set_timeout(port
->dd
, &fis
, &to
, 0);
1496 dbg_printk(MTIP_DRV_NAME
" %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
1506 /* Execute the command. */
1507 if (mtip_exec_internal_command(port
,
1517 command
[0] = reply
->command
; /* Status*/
1518 command
[1] = reply
->features
; /* Error*/
1519 command
[4] = reply
->cyl_low
;
1520 command
[5] = reply
->cyl_hi
;
1522 dbg_printk(MTIP_DRV_NAME
" %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
1533 * @brief Execute a drive command.
1535 * @param port Pointer to the port data structure.
1536 * @param command Pointer to the user specified command parameters.
1537 * @param user_buffer Pointer to the user space buffer where read sector
1538 * data should be copied.
1540 * return value 0 The command completed successfully.
1541 * return value -EFAULT An error occurred while copying the completion
1542 * data to the user space buffer.
1543 * return value -1 An error occurred while executing the command.
1545 static int exec_drive_command(struct mtip_port
*port
, u8
*command
,
1546 void __user
*user_buffer
)
1548 struct host_to_dev_fis fis
;
1549 struct host_to_dev_fis
*reply
;
1551 dma_addr_t dma_addr
= 0;
1552 int rv
= 0, xfer_sz
= command
[3];
1559 buf
= dma_alloc_coherent(&port
->dd
->pdev
->dev
,
1560 ATA_SECT_SIZE
* xfer_sz
,
1564 dev_err(&port
->dd
->pdev
->dev
,
1565 "Memory allocation failed (%d bytes)\n",
1566 ATA_SECT_SIZE
* xfer_sz
);
1571 /* Build the FIS. */
1572 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1575 fis
.command
= command
[0];
1576 fis
.features
= command
[2];
1577 fis
.sect_count
= command
[3];
1578 if (fis
.command
== ATA_CMD_SMART
) {
1579 fis
.sector
= command
[1];
1584 mtip_set_timeout(port
->dd
, &fis
, &to
, 0);
1587 reply
= (port
->rxfis
+ RX_FIS_PIO_SETUP
);
1589 reply
= (port
->rxfis
+ RX_FIS_D2H_REG
);
1591 dbg_printk(MTIP_DRV_NAME
1592 " %s: User Command: cmd %x, sect %x, "
1593 "feat %x, sectcnt %x\n",
1600 /* Execute the command. */
1601 if (mtip_exec_internal_command(port
,
1604 (xfer_sz
? dma_addr
: 0),
1605 (xfer_sz
? ATA_SECT_SIZE
* xfer_sz
: 0),
1610 goto exit_drive_command
;
1613 /* Collect the completion status. */
1614 command
[0] = reply
->command
; /* Status*/
1615 command
[1] = reply
->features
; /* Error*/
1616 command
[2] = reply
->sect_count
;
1618 dbg_printk(MTIP_DRV_NAME
1619 " %s: Completion Status: stat %x, "
1620 "err %x, nsect %x\n",
1627 if (copy_to_user(user_buffer
,
1629 ATA_SECT_SIZE
* command
[3])) {
1631 goto exit_drive_command
;
1636 dma_free_coherent(&port
->dd
->pdev
->dev
,
1637 ATA_SECT_SIZE
* xfer_sz
, buf
, dma_addr
);
1642 * Indicates whether a command has a single sector payload.
1644 * @command passed to the device to perform the certain event.
1645 * @features passed to the device to perform the certain event.
1648 * 1 command is one that always has a single sector payload,
1649 * regardless of the value in the Sector Count field.
1653 static unsigned int implicit_sector(unsigned char command
,
1654 unsigned char features
)
1656 unsigned int rv
= 0;
1658 /* list of commands that have an implicit sector count of 1 */
1660 case ATA_CMD_SEC_SET_PASS
:
1661 case ATA_CMD_SEC_UNLOCK
:
1662 case ATA_CMD_SEC_ERASE_PREP
:
1663 case ATA_CMD_SEC_ERASE_UNIT
:
1664 case ATA_CMD_SEC_FREEZE_LOCK
:
1665 case ATA_CMD_SEC_DISABLE_PASS
:
1666 case ATA_CMD_PMP_READ
:
1667 case ATA_CMD_PMP_WRITE
:
1670 case ATA_CMD_SET_MAX
:
1671 if (features
== ATA_SET_MAX_UNLOCK
)
1675 if ((features
== ATA_SMART_READ_VALUES
) ||
1676 (features
== ATA_SMART_READ_THRESHOLDS
))
1679 case ATA_CMD_CONF_OVERLAY
:
1680 if ((features
== ATA_DCO_IDENTIFY
) ||
1681 (features
== ATA_DCO_SET
))
1689 * Executes a taskfile
1690 * See ide_taskfile_ioctl() for derivation
1692 static int exec_drive_taskfile(struct driver_data
*dd
,
1694 ide_task_request_t
*req_task
,
1697 struct host_to_dev_fis fis
;
1698 struct host_to_dev_fis
*reply
;
1701 dma_addr_t outbuf_dma
= 0;
1702 dma_addr_t inbuf_dma
= 0;
1703 dma_addr_t dma_buffer
= 0;
1705 unsigned int taskin
= 0;
1706 unsigned int taskout
= 0;
1708 unsigned int timeout
;
1709 unsigned int force_single_sector
;
1710 unsigned int transfer_size
;
1711 unsigned long task_file_data
;
1712 int intotal
= outtotal
+ req_task
->out_size
;
1715 taskout
= req_task
->out_size
;
1716 taskin
= req_task
->in_size
;
1717 /* 130560 = 512 * 0xFF*/
1718 if (taskin
> 130560 || taskout
> 130560)
1722 outbuf
= memdup_user(buf
+ outtotal
, taskout
);
1724 return PTR_ERR(outbuf
);
1726 outbuf_dma
= dma_map_single(&dd
->pdev
->dev
, outbuf
,
1727 taskout
, DMA_TO_DEVICE
);
1728 if (dma_mapping_error(&dd
->pdev
->dev
, outbuf_dma
)) {
1732 dma_buffer
= outbuf_dma
;
1736 inbuf
= memdup_user(buf
+ intotal
, taskin
);
1737 if (IS_ERR(inbuf
)) {
1738 err
= PTR_ERR(inbuf
);
1742 inbuf_dma
= dma_map_single(&dd
->pdev
->dev
, inbuf
,
1743 taskin
, DMA_FROM_DEVICE
);
1744 if (dma_mapping_error(&dd
->pdev
->dev
, inbuf_dma
)) {
1748 dma_buffer
= inbuf_dma
;
1751 /* only supports PIO and non-data commands from this ioctl. */
1752 switch (req_task
->data_phase
) {
1754 nsect
= taskout
/ ATA_SECT_SIZE
;
1755 reply
= (dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
);
1758 reply
= (dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
);
1760 case TASKFILE_NO_DATA
:
1761 reply
= (dd
->port
->rxfis
+ RX_FIS_D2H_REG
);
1768 /* Build the FIS. */
1769 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1773 fis
.command
= req_task
->io_ports
[7];
1774 fis
.features
= req_task
->io_ports
[1];
1775 fis
.sect_count
= req_task
->io_ports
[2];
1776 fis
.lba_low
= req_task
->io_ports
[3];
1777 fis
.lba_mid
= req_task
->io_ports
[4];
1778 fis
.lba_hi
= req_task
->io_ports
[5];
1779 /* Clear the dev bit*/
1780 fis
.device
= req_task
->io_ports
[6] & ~0x10;
1782 if ((req_task
->in_flags
.all
== 0) && (req_task
->out_flags
.all
& 1)) {
1783 req_task
->in_flags
.all
=
1784 IDE_TASKFILE_STD_IN_FLAGS
|
1785 (IDE_HOB_STD_IN_FLAGS
<< 8);
1786 fis
.lba_low_ex
= req_task
->hob_ports
[3];
1787 fis
.lba_mid_ex
= req_task
->hob_ports
[4];
1788 fis
.lba_hi_ex
= req_task
->hob_ports
[5];
1789 fis
.features_ex
= req_task
->hob_ports
[1];
1790 fis
.sect_cnt_ex
= req_task
->hob_ports
[2];
1793 req_task
->in_flags
.all
= IDE_TASKFILE_STD_IN_FLAGS
;
1796 force_single_sector
= implicit_sector(fis
.command
, fis
.features
);
1798 if ((taskin
|| taskout
) && (!fis
.sect_count
)) {
1800 fis
.sect_count
= nsect
;
1802 if (!force_single_sector
) {
1803 dev_warn(&dd
->pdev
->dev
,
1804 "data movement but "
1805 "sect_count is 0\n");
1812 dbg_printk(MTIP_DRV_NAME
1813 " %s: cmd %x, feat %x, nsect %x,"
1814 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
1825 /* check for erase mode support during secure erase.*/
1826 if ((fis
.command
== ATA_CMD_SEC_ERASE_UNIT
) && outbuf
&&
1827 (outbuf
[0] & MTIP_SEC_ERASE_MODE
)) {
1831 mtip_set_timeout(dd
, &fis
, &timeout
, erasemode
);
1833 /* Determine the correct transfer size.*/
1834 if (force_single_sector
)
1835 transfer_size
= ATA_SECT_SIZE
;
1837 transfer_size
= ATA_SECT_SIZE
* fis
.sect_count
;
1839 /* Execute the command.*/
1840 if (mtip_exec_internal_command(dd
->port
,
1851 task_file_data
= readl(dd
->port
->mmio
+PORT_TFDATA
);
1853 if ((req_task
->data_phase
== TASKFILE_IN
) && !(task_file_data
& 1)) {
1854 reply
= dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
;
1855 req_task
->io_ports
[7] = reply
->control
;
1857 reply
= dd
->port
->rxfis
+ RX_FIS_D2H_REG
;
1858 req_task
->io_ports
[7] = reply
->command
;
1861 /* reclaim the DMA buffers.*/
1863 dma_unmap_single(&dd
->pdev
->dev
, inbuf_dma
, taskin
,
1866 dma_unmap_single(&dd
->pdev
->dev
, outbuf_dma
, taskout
,
1871 /* return the ATA registers to the caller.*/
1872 req_task
->io_ports
[1] = reply
->features
;
1873 req_task
->io_ports
[2] = reply
->sect_count
;
1874 req_task
->io_ports
[3] = reply
->lba_low
;
1875 req_task
->io_ports
[4] = reply
->lba_mid
;
1876 req_task
->io_ports
[5] = reply
->lba_hi
;
1877 req_task
->io_ports
[6] = reply
->device
;
1879 if (req_task
->out_flags
.all
& 1) {
1881 req_task
->hob_ports
[3] = reply
->lba_low_ex
;
1882 req_task
->hob_ports
[4] = reply
->lba_mid_ex
;
1883 req_task
->hob_ports
[5] = reply
->lba_hi_ex
;
1884 req_task
->hob_ports
[1] = reply
->features_ex
;
1885 req_task
->hob_ports
[2] = reply
->sect_cnt_ex
;
1887 dbg_printk(MTIP_DRV_NAME
1888 " %s: Completion: stat %x,"
1889 "err %x, sect_cnt %x, lbalo %x,"
1890 "lbamid %x, lbahi %x, dev %x\n",
1892 req_task
->io_ports
[7],
1893 req_task
->io_ports
[1],
1894 req_task
->io_ports
[2],
1895 req_task
->io_ports
[3],
1896 req_task
->io_ports
[4],
1897 req_task
->io_ports
[5],
1898 req_task
->io_ports
[6]);
1901 if (copy_to_user(buf
+ outtotal
, outbuf
, taskout
)) {
1907 if (copy_to_user(buf
+ intotal
, inbuf
, taskin
)) {
1914 dma_unmap_single(&dd
->pdev
->dev
, inbuf_dma
, taskin
,
1917 dma_unmap_single(&dd
->pdev
->dev
, outbuf_dma
, taskout
,
1926 * Handle IOCTL calls from the Block Layer.
1928 * This function is called by the Block Layer when it receives an IOCTL
1929 * command that it does not understand. If the IOCTL command is not supported
1930 * this function returns -ENOTTY.
1932 * @dd Pointer to the driver data structure.
1933 * @cmd IOCTL command passed from the Block Layer.
1934 * @arg IOCTL argument passed from the Block Layer.
1937 * 0 The IOCTL completed successfully.
1938 * -ENOTTY The specified command is not supported.
1939 * -EFAULT An error occurred copying data to a user space buffer.
1940 * -EIO An error occurred while executing the command.
1942 static int mtip_hw_ioctl(struct driver_data
*dd
, unsigned int cmd
,
1946 case HDIO_GET_IDENTITY
:
1948 if (copy_to_user((void __user
*)arg
, dd
->port
->identify
,
1949 sizeof(u16
) * ATA_ID_WORDS
))
1953 case HDIO_DRIVE_CMD
:
1955 u8 drive_command
[4];
1957 /* Copy the user command info to our buffer. */
1958 if (copy_from_user(drive_command
,
1959 (void __user
*) arg
,
1960 sizeof(drive_command
)))
1963 /* Execute the drive command. */
1964 if (exec_drive_command(dd
->port
,
1966 (void __user
*) (arg
+4)))
1969 /* Copy the status back to the users buffer. */
1970 if (copy_to_user((void __user
*) arg
,
1972 sizeof(drive_command
)))
1977 case HDIO_DRIVE_TASK
:
1979 u8 drive_command
[7];
1981 /* Copy the user command info to our buffer. */
1982 if (copy_from_user(drive_command
,
1983 (void __user
*) arg
,
1984 sizeof(drive_command
)))
1987 /* Execute the drive command. */
1988 if (exec_drive_task(dd
->port
, drive_command
))
1991 /* Copy the status back to the users buffer. */
1992 if (copy_to_user((void __user
*) arg
,
1994 sizeof(drive_command
)))
1999 case HDIO_DRIVE_TASKFILE
: {
2000 ide_task_request_t req_task
;
2003 if (copy_from_user(&req_task
, (void __user
*) arg
,
2007 outtotal
= sizeof(req_task
);
2009 ret
= exec_drive_taskfile(dd
, (void __user
*) arg
,
2010 &req_task
, outtotal
);
2012 if (copy_to_user((void __user
*) arg
, &req_task
,
2026 * Submit an IO to the hw
2028 * This function is called by the block layer to issue an io
2029 * to the device. Upon completion, the callback function will
2030 * be called with the data parameter passed as the callback data.
2032 * @dd Pointer to the driver data structure.
2033 * @start First sector to read.
2034 * @nsect Number of sectors to read.
2035 * @tag The tag of this read command.
2036 * @callback Pointer to the function that should be called
2037 * when the read completes.
2038 * @data Callback data passed to the callback function
2039 * when the read completes.
2040 * @dir Direction (read or write)
2045 static void mtip_hw_submit_io(struct driver_data
*dd
, struct request
*rq
,
2046 struct mtip_cmd
*command
,
2047 struct blk_mq_hw_ctx
*hctx
)
2049 struct mtip_cmd_hdr
*hdr
=
2050 dd
->port
->command_list
+ sizeof(struct mtip_cmd_hdr
) * rq
->tag
;
2051 struct host_to_dev_fis
*fis
;
2052 struct mtip_port
*port
= dd
->port
;
2053 int dma_dir
= rq_data_dir(rq
) == READ
? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
2054 u64 start
= blk_rq_pos(rq
);
2055 unsigned int nsect
= blk_rq_sectors(rq
);
2058 /* Map the scatter list for DMA access */
2059 nents
= blk_rq_map_sg(hctx
->queue
, rq
, command
->sg
);
2060 nents
= dma_map_sg(&dd
->pdev
->dev
, command
->sg
, nents
, dma_dir
);
2062 prefetch(&port
->flags
);
2064 command
->scatter_ents
= nents
;
2067 * The number of retries for this command before it is
2068 * reported as a failure to the upper layers.
2070 command
->retries
= MTIP_MAX_RETRIES
;
2073 fis
= command
->command
;
2076 if (dma_dir
== DMA_FROM_DEVICE
)
2077 fis
->command
= ATA_CMD_FPDMA_READ
;
2079 fis
->command
= ATA_CMD_FPDMA_WRITE
;
2080 fis
->lba_low
= start
& 0xFF;
2081 fis
->lba_mid
= (start
>> 8) & 0xFF;
2082 fis
->lba_hi
= (start
>> 16) & 0xFF;
2083 fis
->lba_low_ex
= (start
>> 24) & 0xFF;
2084 fis
->lba_mid_ex
= (start
>> 32) & 0xFF;
2085 fis
->lba_hi_ex
= (start
>> 40) & 0xFF;
2086 fis
->device
= 1 << 6;
2087 fis
->features
= nsect
& 0xFF;
2088 fis
->features_ex
= (nsect
>> 8) & 0xFF;
2089 fis
->sect_count
= ((rq
->tag
<< 3) | (rq
->tag
>> 5));
2090 fis
->sect_cnt_ex
= 0;
2094 fill_command_sg(dd
, command
, nents
);
2096 if (unlikely(command
->unaligned
))
2097 fis
->device
|= 1 << 7;
2099 /* Populate the command header */
2100 hdr
->ctba
= cpu_to_le32(command
->command_dma
& 0xFFFFFFFF);
2101 if (test_bit(MTIP_PF_HOST_CAP_64
, &dd
->port
->flags
))
2102 hdr
->ctbau
= cpu_to_le32((command
->command_dma
>> 16) >> 16);
2103 hdr
->opts
= cpu_to_le32((nents
<< 16) | 5 | AHCI_CMD_PREFETCH
);
2104 hdr
->byte_count
= 0;
2106 command
->direction
= dma_dir
;
2109 * To prevent this command from being issued
2110 * if an internal command is in progress or error handling is active.
2112 if (unlikely(port
->flags
& MTIP_PF_PAUSE_IO
)) {
2113 set_bit(rq
->tag
, port
->cmds_to_issue
);
2114 set_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
);
2118 /* Issue the command to the hardware */
2119 mtip_issue_ncq_command(port
, rq
->tag
);
2123 * Sysfs status dump.
2125 * @dev Pointer to the device structure, passed by the kernrel.
2126 * @attr Pointer to the device_attribute structure passed by the kernel.
2127 * @buf Pointer to the char buffer that will receive the stats info.
2130 * The size, in bytes, of the data copied into buf.
2132 static ssize_t
mtip_hw_show_status(struct device
*dev
,
2133 struct device_attribute
*attr
,
2136 struct driver_data
*dd
= dev_to_disk(dev
)->private_data
;
2139 if (test_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
))
2140 size
+= sprintf(buf
, "%s", "thermal_shutdown\n");
2141 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
))
2142 size
+= sprintf(buf
, "%s", "write_protect\n");
2144 size
+= sprintf(buf
, "%s", "online\n");
2149 static DEVICE_ATTR(status
, 0444, mtip_hw_show_status
, NULL
);
2151 static struct attribute
*mtip_disk_attrs
[] = {
2152 &dev_attr_status
.attr
,
2156 static const struct attribute_group mtip_disk_attr_group
= {
2157 .attrs
= mtip_disk_attrs
,
2160 static const struct attribute_group
*mtip_disk_attr_groups
[] = {
2161 &mtip_disk_attr_group
,
2165 static ssize_t
mtip_hw_read_registers(struct file
*f
, char __user
*ubuf
,
2166 size_t len
, loff_t
*offset
)
2168 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2170 u32 group_allocated
;
2177 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2181 size
+= sprintf(&buf
[size
], "H/ S ACTive : [ 0x");
2183 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2184 size
+= sprintf(&buf
[size
], "%08X ",
2185 readl(dd
->port
->s_active
[n
]));
2187 size
+= sprintf(&buf
[size
], "]\n");
2188 size
+= sprintf(&buf
[size
], "H/ Command Issue : [ 0x");
2190 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2191 size
+= sprintf(&buf
[size
], "%08X ",
2192 readl(dd
->port
->cmd_issue
[n
]));
2194 size
+= sprintf(&buf
[size
], "]\n");
2195 size
+= sprintf(&buf
[size
], "H/ Completed : [ 0x");
2197 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2198 size
+= sprintf(&buf
[size
], "%08X ",
2199 readl(dd
->port
->completed
[n
]));
2201 size
+= sprintf(&buf
[size
], "]\n");
2202 size
+= sprintf(&buf
[size
], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2203 readl(dd
->port
->mmio
+ PORT_IRQ_STAT
));
2204 size
+= sprintf(&buf
[size
], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2205 readl(dd
->mmio
+ HOST_IRQ_STAT
));
2206 size
+= sprintf(&buf
[size
], "\n");
2208 size
+= sprintf(&buf
[size
], "L/ Commands in Q : [ 0x");
2210 for (n
= dd
->slot_groups
-1; n
>= 0; n
--) {
2211 if (sizeof(long) > sizeof(u32
))
2213 dd
->port
->cmds_to_issue
[n
/2] >> (32*(n
&1));
2215 group_allocated
= dd
->port
->cmds_to_issue
[n
];
2216 size
+= sprintf(&buf
[size
], "%08X ", group_allocated
);
2218 size
+= sprintf(&buf
[size
], "]\n");
2220 *offset
= size
<= len
? size
: len
;
2221 size
= copy_to_user(ubuf
, buf
, *offset
);
2226 return rv
? rv
: *offset
;
2229 static ssize_t
mtip_hw_read_flags(struct file
*f
, char __user
*ubuf
,
2230 size_t len
, loff_t
*offset
)
2232 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2240 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2244 size
+= sprintf(&buf
[size
], "Flag-port : [ %08lX ]\n",
2246 size
+= sprintf(&buf
[size
], "Flag-dd : [ %08lX ]\n",
2249 *offset
= size
<= len
? size
: len
;
2250 size
= copy_to_user(ubuf
, buf
, *offset
);
2255 return rv
? rv
: *offset
;
2258 static const struct file_operations mtip_regs_fops
= {
2259 .owner
= THIS_MODULE
,
2260 .open
= simple_open
,
2261 .read
= mtip_hw_read_registers
,
2264 static const struct file_operations mtip_flags_fops
= {
2265 .owner
= THIS_MODULE
,
2266 .open
= simple_open
,
2267 .read
= mtip_hw_read_flags
,
2270 static void mtip_hw_debugfs_init(struct driver_data
*dd
)
2272 dd
->dfs_node
= debugfs_create_dir(dd
->disk
->disk_name
, dfs_parent
);
2273 debugfs_create_file("flags", 0444, dd
->dfs_node
, dd
, &mtip_flags_fops
);
2274 debugfs_create_file("registers", 0444, dd
->dfs_node
, dd
,
2278 static void mtip_hw_debugfs_exit(struct driver_data
*dd
)
2280 debugfs_remove_recursive(dd
->dfs_node
);
2284 * Perform any init/resume time hardware setup
2286 * @dd Pointer to the driver data structure.
2291 static inline void hba_setup(struct driver_data
*dd
)
2294 hwdata
= readl(dd
->mmio
+ HOST_HSORG
);
2296 /* interrupt bug workaround: use only 1 IS bit.*/
2298 HSORG_DISABLE_SLOTGRP_INTR
|
2299 HSORG_DISABLE_SLOTGRP_PXIS
,
2300 dd
->mmio
+ HOST_HSORG
);
2303 static int mtip_device_unaligned_constrained(struct driver_data
*dd
)
2305 return (dd
->pdev
->device
== P420M_DEVICE_ID
? 1 : 0);
2309 * Detect the details of the product, and store anything needed
2310 * into the driver data structure. This includes product type and
2311 * version and number of slot groups.
2313 * @dd Pointer to the driver data structure.
2318 static void mtip_detect_product(struct driver_data
*dd
)
2321 unsigned int rev
, slotgroups
;
2324 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2326 * [15:8] hardware/software interface rev#
2327 * [ 3] asic-style interface
2328 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2330 hwdata
= readl(dd
->mmio
+ HOST_HSORG
);
2332 dd
->product_type
= MTIP_PRODUCT_UNKNOWN
;
2333 dd
->slot_groups
= 1;
2336 dd
->product_type
= MTIP_PRODUCT_ASICFPGA
;
2337 rev
= (hwdata
& HSORG_HWREV
) >> 8;
2338 slotgroups
= (hwdata
& HSORG_SLOTGROUPS
) + 1;
2339 dev_info(&dd
->pdev
->dev
,
2340 "ASIC-FPGA design, HS rev 0x%x, "
2341 "%i slot groups [%i slots]\n",
2346 if (slotgroups
> MTIP_MAX_SLOT_GROUPS
) {
2347 dev_warn(&dd
->pdev
->dev
,
2348 "Warning: driver only supports "
2349 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS
);
2350 slotgroups
= MTIP_MAX_SLOT_GROUPS
;
2352 dd
->slot_groups
= slotgroups
;
2356 dev_warn(&dd
->pdev
->dev
, "Unrecognized product id\n");
2360 * Blocking wait for FTL rebuild to complete
2362 * @dd Pointer to the DRIVER_DATA structure.
2365 * 0 FTL rebuild completed successfully
2366 * -EFAULT FTL rebuild error/timeout/interruption
2368 static int mtip_ftl_rebuild_poll(struct driver_data
*dd
)
2370 unsigned long timeout
, cnt
= 0, start
;
2372 dev_warn(&dd
->pdev
->dev
,
2373 "FTL rebuild in progress. Polling for completion.\n");
2376 timeout
= jiffies
+ msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS
);
2379 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
2382 if (mtip_check_surprise_removal(dd
))
2385 if (mtip_get_identify(dd
->port
, NULL
) < 0)
2388 if (*(dd
->port
->identify
+ MTIP_FTL_REBUILD_OFFSET
) ==
2389 MTIP_FTL_REBUILD_MAGIC
) {
2391 /* Print message every 3 minutes */
2393 dev_warn(&dd
->pdev
->dev
,
2394 "FTL rebuild in progress (%d secs).\n",
2395 jiffies_to_msecs(jiffies
- start
) / 1000);
2399 dev_warn(&dd
->pdev
->dev
,
2400 "FTL rebuild complete (%d secs).\n",
2401 jiffies_to_msecs(jiffies
- start
) / 1000);
2402 mtip_block_initialize(dd
);
2405 } while (time_before(jiffies
, timeout
));
2407 /* Check for timeout */
2408 dev_err(&dd
->pdev
->dev
,
2409 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2410 jiffies_to_msecs(jiffies
- start
) / 1000);
2414 static void mtip_softirq_done_fn(struct request
*rq
)
2416 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
2417 struct driver_data
*dd
= rq
->q
->queuedata
;
2419 /* Unmap the DMA scatter list entries */
2420 dma_unmap_sg(&dd
->pdev
->dev
, cmd
->sg
, cmd
->scatter_ents
,
2423 if (unlikely(cmd
->unaligned
))
2424 atomic_inc(&dd
->port
->cmd_slot_unal
);
2426 blk_mq_end_request(rq
, cmd
->status
);
2429 static bool mtip_abort_cmd(struct request
*req
, void *data
)
2431 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
2432 struct driver_data
*dd
= data
;
2434 dbg_printk(MTIP_DRV_NAME
" Aborting request, tag = %d\n", req
->tag
);
2436 clear_bit(req
->tag
, dd
->port
->cmds_to_issue
);
2437 cmd
->status
= BLK_STS_IOERR
;
2438 mtip_softirq_done_fn(req
);
2442 static bool mtip_queue_cmd(struct request
*req
, void *data
)
2444 struct driver_data
*dd
= data
;
2446 set_bit(req
->tag
, dd
->port
->cmds_to_issue
);
2447 blk_abort_request(req
);
2452 * service thread to issue queued commands
2454 * @data Pointer to the driver data structure.
2460 static int mtip_service_thread(void *data
)
2462 struct driver_data
*dd
= (struct driver_data
*)data
;
2463 unsigned long slot
, slot_start
, slot_wrap
, to
;
2464 unsigned int num_cmd_slots
= dd
->slot_groups
* 32;
2465 struct mtip_port
*port
= dd
->port
;
2468 if (kthread_should_stop() ||
2469 test_bit(MTIP_PF_SVC_THD_STOP_BIT
, &port
->flags
))
2471 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
);
2474 * the condition is to check neither an internal command is
2475 * is in progress nor error handling is active
2477 wait_event_interruptible(port
->svc_wait
, (port
->flags
) &&
2478 (port
->flags
& MTIP_PF_SVC_THD_WORK
));
2480 if (kthread_should_stop() ||
2481 test_bit(MTIP_PF_SVC_THD_STOP_BIT
, &port
->flags
))
2484 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
2488 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
);
2491 /* Demux bits: start with error handling */
2492 if (test_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
)) {
2493 mtip_handle_tfe(dd
);
2494 clear_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
);
2497 if (test_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
))
2500 if (test_bit(MTIP_PF_TO_ACTIVE_BIT
, &port
->flags
)) {
2501 to
= jiffies
+ msecs_to_jiffies(5000);
2505 } while (atomic_read(&dd
->irq_workers_active
) != 0 &&
2506 time_before(jiffies
, to
));
2508 if (atomic_read(&dd
->irq_workers_active
) != 0)
2509 dev_warn(&dd
->pdev
->dev
,
2510 "Completion workers still active!");
2512 blk_mq_quiesce_queue(dd
->queue
);
2514 blk_mq_tagset_busy_iter(&dd
->tags
, mtip_queue_cmd
, dd
);
2516 set_bit(MTIP_PF_ISSUE_CMDS_BIT
, &dd
->port
->flags
);
2518 if (mtip_device_reset(dd
))
2519 blk_mq_tagset_busy_iter(&dd
->tags
,
2520 mtip_abort_cmd
, dd
);
2522 clear_bit(MTIP_PF_TO_ACTIVE_BIT
, &dd
->port
->flags
);
2524 blk_mq_unquiesce_queue(dd
->queue
);
2527 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
)) {
2529 /* used to restrict the loop to one iteration */
2530 slot_start
= num_cmd_slots
;
2533 slot
= find_next_bit(port
->cmds_to_issue
,
2534 num_cmd_slots
, slot
);
2535 if (slot_wrap
== 1) {
2536 if ((slot_start
>= slot
) ||
2537 (slot
>= num_cmd_slots
))
2540 if (unlikely(slot_start
== num_cmd_slots
))
2543 if (unlikely(slot
== num_cmd_slots
)) {
2549 /* Issue the command to the hardware */
2550 mtip_issue_ncq_command(port
, slot
);
2552 clear_bit(slot
, port
->cmds_to_issue
);
2555 clear_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
);
2558 if (test_bit(MTIP_PF_REBUILD_BIT
, &port
->flags
)) {
2559 if (mtip_ftl_rebuild_poll(dd
) == 0)
2560 clear_bit(MTIP_PF_REBUILD_BIT
, &port
->flags
);
2569 * DMA region teardown
2571 * @dd Pointer to driver_data structure
2576 static void mtip_dma_free(struct driver_data
*dd
)
2578 struct mtip_port
*port
= dd
->port
;
2581 dma_free_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
2582 port
->block1
, port
->block1_dma
);
2584 if (port
->command_list
) {
2585 dma_free_coherent(&dd
->pdev
->dev
, AHCI_CMD_TBL_SZ
,
2586 port
->command_list
, port
->command_list_dma
);
2593 * @dd Pointer to driver_data structure
2596 * -ENOMEM Not enough free DMA region space to initialize driver
2598 static int mtip_dma_alloc(struct driver_data
*dd
)
2600 struct mtip_port
*port
= dd
->port
;
2602 /* Allocate dma memory for RX Fis, Identify, and Sector Buffer */
2604 dma_alloc_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
2605 &port
->block1_dma
, GFP_KERNEL
);
2609 /* Allocate dma memory for command list */
2610 port
->command_list
=
2611 dma_alloc_coherent(&dd
->pdev
->dev
, AHCI_CMD_TBL_SZ
,
2612 &port
->command_list_dma
, GFP_KERNEL
);
2613 if (!port
->command_list
) {
2614 dma_free_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
2615 port
->block1
, port
->block1_dma
);
2616 port
->block1
= NULL
;
2617 port
->block1_dma
= 0;
2621 /* Setup all pointers into first DMA region */
2622 port
->rxfis
= port
->block1
+ AHCI_RX_FIS_OFFSET
;
2623 port
->rxfis_dma
= port
->block1_dma
+ AHCI_RX_FIS_OFFSET
;
2624 port
->identify
= port
->block1
+ AHCI_IDFY_OFFSET
;
2625 port
->identify_dma
= port
->block1_dma
+ AHCI_IDFY_OFFSET
;
2626 port
->log_buf
= port
->block1
+ AHCI_SECTBUF_OFFSET
;
2627 port
->log_buf_dma
= port
->block1_dma
+ AHCI_SECTBUF_OFFSET
;
2628 port
->smart_buf
= port
->block1
+ AHCI_SMARTBUF_OFFSET
;
2629 port
->smart_buf_dma
= port
->block1_dma
+ AHCI_SMARTBUF_OFFSET
;
2634 static int mtip_hw_get_identify(struct driver_data
*dd
)
2636 struct smart_attr attr242
;
2640 if (mtip_get_identify(dd
->port
, NULL
) < 0)
2643 if (*(dd
->port
->identify
+ MTIP_FTL_REBUILD_OFFSET
) ==
2644 MTIP_FTL_REBUILD_MAGIC
) {
2645 set_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
);
2646 return MTIP_FTL_REBUILD_MAGIC
;
2648 mtip_dump_identify(dd
->port
);
2650 /* check write protect, over temp and rebuild statuses */
2651 rv
= mtip_read_log_page(dd
->port
, ATA_LOG_SATA_NCQ
,
2653 dd
->port
->log_buf_dma
, 1);
2655 dev_warn(&dd
->pdev
->dev
,
2656 "Error in READ LOG EXT (10h) command\n");
2657 /* non-critical error, don't fail the load */
2659 buf
= (unsigned char *)dd
->port
->log_buf
;
2660 if (buf
[259] & 0x1) {
2661 dev_info(&dd
->pdev
->dev
,
2662 "Write protect bit is set.\n");
2663 set_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
);
2665 if (buf
[288] == 0xF7) {
2666 dev_info(&dd
->pdev
->dev
,
2667 "Exceeded Tmax, drive in thermal shutdown.\n");
2668 set_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
);
2670 if (buf
[288] == 0xBF) {
2671 dev_info(&dd
->pdev
->dev
,
2672 "Drive indicates rebuild has failed.\n");
2673 set_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
);
2677 /* get write protect progess */
2678 memset(&attr242
, 0, sizeof(struct smart_attr
));
2679 if (mtip_get_smart_attr(dd
->port
, 242, &attr242
))
2680 dev_warn(&dd
->pdev
->dev
,
2681 "Unable to check write protect progress\n");
2683 dev_info(&dd
->pdev
->dev
,
2684 "Write protect progress: %u%% (%u blocks)\n",
2685 attr242
.cur
, le32_to_cpu(attr242
.data
));
2691 * Called once for each card.
2693 * @dd Pointer to the driver data structure.
2696 * 0 on success, else an error code.
2698 static int mtip_hw_init(struct driver_data
*dd
)
2702 unsigned long timeout
, timetaken
;
2704 dd
->mmio
= pcim_iomap_region(dd
->pdev
, MTIP_ABAR
, MTIP_DRV_NAME
);
2705 if (IS_ERR(dd
->mmio
)) {
2706 dev_err(&dd
->pdev
->dev
, "Unable to request / ioremap PCI region\n");
2707 return PTR_ERR(dd
->mmio
);
2711 mtip_detect_product(dd
);
2712 if (dd
->product_type
== MTIP_PRODUCT_UNKNOWN
) {
2719 dd
->port
= kzalloc_node(sizeof(struct mtip_port
), GFP_KERNEL
,
2724 /* Continue workqueue setup */
2725 for (i
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++)
2726 dd
->work
[i
].port
= dd
->port
;
2728 /* Enable unaligned IO constraints for some devices */
2729 if (mtip_device_unaligned_constrained(dd
))
2730 dd
->unal_qdepth
= MTIP_MAX_UNALIGNED_SLOTS
;
2732 dd
->unal_qdepth
= 0;
2734 atomic_set(&dd
->port
->cmd_slot_unal
, dd
->unal_qdepth
);
2736 /* Spinlock to prevent concurrent issue */
2737 for (i
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++)
2738 spin_lock_init(&dd
->port
->cmd_issue_lock
[i
]);
2740 /* Set the port mmio base address. */
2741 dd
->port
->mmio
= dd
->mmio
+ PORT_OFFSET
;
2744 /* DMA allocations */
2745 rv
= mtip_dma_alloc(dd
);
2749 /* Setup the pointers to the extended s_active and CI registers. */
2750 for (i
= 0; i
< dd
->slot_groups
; i
++) {
2751 dd
->port
->s_active
[i
] =
2752 dd
->port
->mmio
+ i
*0x80 + PORT_SCR_ACT
;
2753 dd
->port
->cmd_issue
[i
] =
2754 dd
->port
->mmio
+ i
*0x80 + PORT_COMMAND_ISSUE
;
2755 dd
->port
->completed
[i
] =
2756 dd
->port
->mmio
+ i
*0x80 + PORT_SDBV
;
2759 timetaken
= jiffies
;
2760 timeout
= jiffies
+ msecs_to_jiffies(30000);
2761 while (((readl(dd
->port
->mmio
+ PORT_SCR_STAT
) & 0x0F) != 0x03) &&
2762 time_before(jiffies
, timeout
)) {
2765 if (unlikely(mtip_check_surprise_removal(dd
))) {
2766 timetaken
= jiffies
- timetaken
;
2767 dev_warn(&dd
->pdev
->dev
,
2768 "Surprise removal detected at %u ms\n",
2769 jiffies_to_msecs(timetaken
));
2773 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))) {
2774 timetaken
= jiffies
- timetaken
;
2775 dev_warn(&dd
->pdev
->dev
,
2776 "Removal detected at %u ms\n",
2777 jiffies_to_msecs(timetaken
));
2782 /* Conditionally reset the HBA. */
2783 if (!(readl(dd
->mmio
+ HOST_CAP
) & HOST_CAP_NZDMA
)) {
2784 if (mtip_hba_reset(dd
) < 0) {
2785 dev_err(&dd
->pdev
->dev
,
2786 "Card did not reset within timeout\n");
2791 /* Clear any pending interrupts on the HBA */
2792 writel(readl(dd
->mmio
+ HOST_IRQ_STAT
),
2793 dd
->mmio
+ HOST_IRQ_STAT
);
2796 mtip_init_port(dd
->port
);
2797 mtip_start_port(dd
->port
);
2799 /* Setup the ISR and enable interrupts. */
2800 rv
= request_irq(dd
->pdev
->irq
, mtip_irq_handler
, IRQF_SHARED
,
2801 dev_driver_string(&dd
->pdev
->dev
), dd
);
2803 dev_err(&dd
->pdev
->dev
,
2804 "Unable to allocate IRQ %d\n", dd
->pdev
->irq
);
2807 irq_set_affinity_hint(dd
->pdev
->irq
, get_cpu_mask(dd
->isr_binding
));
2809 /* Enable interrupts on the HBA. */
2810 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
2811 dd
->mmio
+ HOST_CTL
);
2813 init_waitqueue_head(&dd
->port
->svc_wait
);
2815 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)) {
2823 /* Disable interrupts on the HBA. */
2824 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
2825 dd
->mmio
+ HOST_CTL
);
2827 /* Release the IRQ. */
2828 irq_set_affinity_hint(dd
->pdev
->irq
, NULL
);
2829 free_irq(dd
->pdev
->irq
, dd
);
2832 mtip_deinit_port(dd
->port
);
2836 /* Free the memory allocated for the for structure. */
2842 static int mtip_standby_drive(struct driver_data
*dd
)
2846 if (dd
->sr
|| !dd
->port
)
2849 * Send standby immediate (E0h) to the drive so that it
2852 if (!test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
) &&
2853 !test_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
) &&
2854 !test_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
)) {
2855 rv
= mtip_standby_immediate(dd
->port
);
2857 dev_warn(&dd
->pdev
->dev
,
2858 "STANDBY IMMEDIATE failed\n");
2864 * Called to deinitialize an interface.
2866 * @dd Pointer to the driver data structure.
2871 static int mtip_hw_exit(struct driver_data
*dd
)
2874 /* de-initialize the port. */
2875 mtip_deinit_port(dd
->port
);
2877 /* Disable interrupts on the HBA. */
2878 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
2879 dd
->mmio
+ HOST_CTL
);
2882 /* Release the IRQ. */
2883 irq_set_affinity_hint(dd
->pdev
->irq
, NULL
);
2884 free_irq(dd
->pdev
->irq
, dd
);
2887 /* Free dma regions */
2890 /* Free the memory allocated for the for structure. */
2898 * Issue a Standby Immediate command to the device.
2900 * This function is called by the Block Layer just before the
2901 * system powers off during a shutdown.
2903 * @dd Pointer to the driver data structure.
2908 static int mtip_hw_shutdown(struct driver_data
*dd
)
2911 * Send standby immediate (E0h) to the drive so that it
2914 mtip_standby_drive(dd
);
2922 * This function is called by the Block Layer just before the
2923 * system hibernates.
2925 * @dd Pointer to the driver data structure.
2928 * 0 Suspend was successful
2929 * -EFAULT Suspend was not successful
2931 static int mtip_hw_suspend(struct driver_data
*dd
)
2934 * Send standby immediate (E0h) to the drive
2935 * so that it saves its state.
2937 if (mtip_standby_drive(dd
) != 0) {
2938 dev_err(&dd
->pdev
->dev
,
2939 "Failed standby-immediate command\n");
2943 /* Disable interrupts on the HBA.*/
2944 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
2945 dd
->mmio
+ HOST_CTL
);
2946 mtip_deinit_port(dd
->port
);
2954 * This function is called by the Block Layer as the
2957 * @dd Pointer to the driver data structure.
2960 * 0 Resume was successful
2961 * -EFAULT Resume was not successful
2963 static int mtip_hw_resume(struct driver_data
*dd
)
2965 /* Perform any needed hardware setup steps */
2969 if (mtip_hba_reset(dd
) != 0) {
2970 dev_err(&dd
->pdev
->dev
,
2971 "Unable to reset the HBA\n");
2976 * Enable the port, DMA engine, and FIS reception specific
2977 * h/w in controller.
2979 mtip_init_port(dd
->port
);
2980 mtip_start_port(dd
->port
);
2982 /* Enable interrupts on the HBA.*/
2983 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
2984 dd
->mmio
+ HOST_CTL
);
2990 * Helper function for reusing disk name
2991 * upon hot insertion.
2993 static int rssd_disk_name_format(char *prefix
,
2998 const int base
= 'z' - 'a' + 1;
2999 char *begin
= buf
+ strlen(prefix
);
3000 char *end
= buf
+ buflen
;
3010 *--p
= 'a' + (index
% unit
);
3011 index
= (index
/ unit
) - 1;
3012 } while (index
>= 0);
3014 memmove(begin
, p
, end
- p
);
3015 memcpy(buf
, prefix
, strlen(prefix
));
3021 * Block layer IOCTL handler.
3023 * @dev Pointer to the block_device structure.
3025 * @cmd IOCTL command passed from the user application.
3026 * @arg Argument passed from the user application.
3029 * 0 IOCTL completed successfully.
3030 * -ENOTTY IOCTL not supported or invalid driver data
3031 * structure pointer.
3033 static int mtip_block_ioctl(struct block_device
*dev
,
3038 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3040 if (!capable(CAP_SYS_ADMIN
))
3046 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)))
3053 return mtip_hw_ioctl(dd
, cmd
, arg
);
3057 #ifdef CONFIG_COMPAT
3059 * Block layer compat IOCTL handler.
3061 * @dev Pointer to the block_device structure.
3063 * @cmd IOCTL command passed from the user application.
3064 * @arg Argument passed from the user application.
3067 * 0 IOCTL completed successfully.
3068 * -ENOTTY IOCTL not supported or invalid driver data
3069 * structure pointer.
3071 static int mtip_block_compat_ioctl(struct block_device
*dev
,
3076 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3078 if (!capable(CAP_SYS_ADMIN
))
3084 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)))
3090 case HDIO_DRIVE_TASKFILE
: {
3091 struct mtip_compat_ide_task_request_s __user
*compat_req_task
;
3092 ide_task_request_t req_task
;
3093 int compat_tasksize
, outtotal
, ret
;
3096 sizeof(struct mtip_compat_ide_task_request_s
);
3099 (struct mtip_compat_ide_task_request_s __user
*) arg
;
3101 if (copy_from_user(&req_task
, (void __user
*) arg
,
3102 compat_tasksize
- (2 * sizeof(compat_long_t
))))
3105 if (get_user(req_task
.out_size
, &compat_req_task
->out_size
))
3108 if (get_user(req_task
.in_size
, &compat_req_task
->in_size
))
3111 outtotal
= sizeof(struct mtip_compat_ide_task_request_s
);
3113 ret
= exec_drive_taskfile(dd
, (void __user
*) arg
,
3114 &req_task
, outtotal
);
3116 if (copy_to_user((void __user
*) arg
, &req_task
,
3118 (2 * sizeof(compat_long_t
))))
3121 if (put_user(req_task
.out_size
, &compat_req_task
->out_size
))
3124 if (put_user(req_task
.in_size
, &compat_req_task
->in_size
))
3130 return mtip_hw_ioctl(dd
, cmd
, arg
);
3136 * Obtain the geometry of the device.
3138 * You may think that this function is obsolete, but some applications,
3139 * fdisk for example still used CHS values. This function describes the
3140 * device as having 224 heads and 56 sectors per cylinder. These values are
3141 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3142 * partition is described in terms of a start and end cylinder this means
3143 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3144 * affects performance.
3146 * @dev Pointer to the block_device strucutre.
3147 * @geo Pointer to a hd_geometry structure.
3150 * 0 Operation completed successfully.
3151 * -ENOTTY An error occurred while reading the drive capacity.
3153 static int mtip_block_getgeo(struct block_device
*dev
,
3154 struct hd_geometry
*geo
)
3156 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3162 if (!(mtip_hw_get_capacity(dd
, &capacity
))) {
3163 dev_warn(&dd
->pdev
->dev
,
3164 "Could not get drive capacity.\n");
3170 sector_div(capacity
, (geo
->heads
* geo
->sectors
));
3171 geo
->cylinders
= capacity
;
3175 static void mtip_block_free_disk(struct gendisk
*disk
)
3177 struct driver_data
*dd
= disk
->private_data
;
3179 ida_free(&rssd_index_ida
, dd
->index
);
3184 * Block device operation function.
3186 * This structure contains pointers to the functions required by the block
3189 static const struct block_device_operations mtip_block_ops
= {
3190 .ioctl
= mtip_block_ioctl
,
3191 #ifdef CONFIG_COMPAT
3192 .compat_ioctl
= mtip_block_compat_ioctl
,
3194 .getgeo
= mtip_block_getgeo
,
3195 .free_disk
= mtip_block_free_disk
,
3196 .owner
= THIS_MODULE
3199 static inline bool is_se_active(struct driver_data
*dd
)
3201 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT
, &dd
->port
->flags
))) {
3202 if (dd
->port
->ic_pause_timer
) {
3203 unsigned long to
= dd
->port
->ic_pause_timer
+
3204 msecs_to_jiffies(1000);
3205 if (time_after(jiffies
, to
)) {
3206 clear_bit(MTIP_PF_SE_ACTIVE_BIT
,
3208 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
);
3209 dd
->port
->ic_pause_timer
= 0;
3210 wake_up_interruptible(&dd
->port
->svc_wait
);
3219 static inline bool is_stopped(struct driver_data
*dd
, struct request
*rq
)
3221 if (likely(!(dd
->dd_flag
& MTIP_DDF_STOP_IO
)))
3224 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))
3226 if (test_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
))
3228 if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
) &&
3231 if (test_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
))
3233 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
))
3239 static bool mtip_check_unal_depth(struct blk_mq_hw_ctx
*hctx
,
3242 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3243 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3245 if (rq_data_dir(rq
) == READ
|| !dd
->unal_qdepth
)
3249 * If unaligned depth must be limited on this controller, mark it
3250 * as unaligned if the IO isn't on a 4k boundary (start of length).
3252 if (blk_rq_sectors(rq
) <= 64) {
3253 if ((blk_rq_pos(rq
) & 7) || (blk_rq_sectors(rq
) & 7))
3257 if (cmd
->unaligned
&& atomic_dec_if_positive(&dd
->port
->cmd_slot_unal
) >= 0)
3263 static blk_status_t
mtip_issue_reserved_cmd(struct blk_mq_hw_ctx
*hctx
,
3266 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3267 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3268 struct mtip_int_cmd
*icmd
= cmd
->icmd
;
3269 struct mtip_cmd_hdr
*hdr
=
3270 dd
->port
->command_list
+ sizeof(struct mtip_cmd_hdr
) * rq
->tag
;
3271 struct mtip_cmd_sg
*command_sg
;
3273 if (mtip_commands_active(dd
->port
))
3274 return BLK_STS_DEV_RESOURCE
;
3276 hdr
->ctba
= cpu_to_le32(cmd
->command_dma
& 0xFFFFFFFF);
3277 if (test_bit(MTIP_PF_HOST_CAP_64
, &dd
->port
->flags
))
3278 hdr
->ctbau
= cpu_to_le32((cmd
->command_dma
>> 16) >> 16);
3279 /* Populate the SG list */
3280 hdr
->opts
= cpu_to_le32(icmd
->opts
| icmd
->fis_len
);
3281 if (icmd
->buf_len
) {
3282 command_sg
= cmd
->command
+ AHCI_CMD_TBL_HDR_SZ
;
3284 command_sg
->info
= cpu_to_le32((icmd
->buf_len
-1) & 0x3FFFFF);
3285 command_sg
->dba
= cpu_to_le32(icmd
->buffer
& 0xFFFFFFFF);
3286 command_sg
->dba_upper
=
3287 cpu_to_le32((icmd
->buffer
>> 16) >> 16);
3289 hdr
->opts
|= cpu_to_le32((1 << 16));
3292 /* Populate the command header */
3293 hdr
->byte_count
= 0;
3295 blk_mq_start_request(rq
);
3296 mtip_issue_non_ncq_command(dd
->port
, rq
->tag
);
3300 static blk_status_t
mtip_queue_rq(struct blk_mq_hw_ctx
*hctx
,
3301 const struct blk_mq_queue_data
*bd
)
3303 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3304 struct request
*rq
= bd
->rq
;
3305 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3307 if (blk_rq_is_passthrough(rq
))
3308 return mtip_issue_reserved_cmd(hctx
, rq
);
3310 if (unlikely(mtip_check_unal_depth(hctx
, rq
)))
3311 return BLK_STS_DEV_RESOURCE
;
3313 if (is_se_active(dd
) || is_stopped(dd
, rq
))
3314 return BLK_STS_IOERR
;
3316 blk_mq_start_request(rq
);
3318 mtip_hw_submit_io(dd
, rq
, cmd
, hctx
);
3322 static void mtip_free_cmd(struct blk_mq_tag_set
*set
, struct request
*rq
,
3323 unsigned int hctx_idx
)
3325 struct driver_data
*dd
= set
->driver_data
;
3326 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3331 dma_free_coherent(&dd
->pdev
->dev
, CMD_DMA_ALLOC_SZ
, cmd
->command
,
3335 static int mtip_init_cmd(struct blk_mq_tag_set
*set
, struct request
*rq
,
3336 unsigned int hctx_idx
, unsigned int numa_node
)
3338 struct driver_data
*dd
= set
->driver_data
;
3339 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3341 cmd
->command
= dma_alloc_coherent(&dd
->pdev
->dev
, CMD_DMA_ALLOC_SZ
,
3342 &cmd
->command_dma
, GFP_KERNEL
);
3346 sg_init_table(cmd
->sg
, MTIP_MAX_SG
);
3350 static enum blk_eh_timer_return
mtip_cmd_timeout(struct request
*req
)
3352 struct driver_data
*dd
= req
->q
->queuedata
;
3354 if (blk_mq_is_reserved_rq(req
)) {
3355 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
3357 cmd
->status
= BLK_STS_TIMEOUT
;
3358 blk_mq_complete_request(req
);
3362 if (test_bit(req
->tag
, dd
->port
->cmds_to_issue
))
3365 if (test_and_set_bit(MTIP_PF_TO_ACTIVE_BIT
, &dd
->port
->flags
))
3368 wake_up_interruptible(&dd
->port
->svc_wait
);
3370 return BLK_EH_RESET_TIMER
;
3373 static const struct blk_mq_ops mtip_mq_ops
= {
3374 .queue_rq
= mtip_queue_rq
,
3375 .init_request
= mtip_init_cmd
,
3376 .exit_request
= mtip_free_cmd
,
3377 .complete
= mtip_softirq_done_fn
,
3378 .timeout
= mtip_cmd_timeout
,
3382 * Block layer initialization function.
3384 * This function is called once by the PCI layer for each P320
3385 * device that is connected to the system.
3387 * @dd Pointer to the driver data structure.
3390 * 0 on success else an error code.
3392 static int mtip_block_initialize(struct driver_data
*dd
)
3394 struct queue_limits lim
= {
3395 .physical_block_size
= 4096,
3396 .max_hw_sectors
= 0xffff,
3397 .max_segments
= MTIP_MAX_SG
,
3398 .max_segment_size
= 0x400000,
3400 int rv
= 0, wait_for_rebuild
= 0;
3402 unsigned int index
= 0;
3405 goto skip_create_disk
; /* hw init done, before rebuild */
3407 if (mtip_hw_init(dd
)) {
3409 goto protocol_init_error
;
3412 memset(&dd
->tags
, 0, sizeof(dd
->tags
));
3413 dd
->tags
.ops
= &mtip_mq_ops
;
3414 dd
->tags
.nr_hw_queues
= 1;
3415 dd
->tags
.queue_depth
= MTIP_MAX_COMMAND_SLOTS
;
3416 dd
->tags
.reserved_tags
= 1;
3417 dd
->tags
.cmd_size
= sizeof(struct mtip_cmd
);
3418 dd
->tags
.numa_node
= dd
->numa_node
;
3419 dd
->tags
.flags
= BLK_MQ_F_SHOULD_MERGE
;
3420 dd
->tags
.driver_data
= dd
;
3421 dd
->tags
.timeout
= MTIP_NCQ_CMD_TIMEOUT_MS
;
3423 rv
= blk_mq_alloc_tag_set(&dd
->tags
);
3425 dev_err(&dd
->pdev
->dev
,
3426 "Unable to allocate request queue\n");
3427 goto block_queue_alloc_tag_error
;
3430 dd
->disk
= blk_mq_alloc_disk(&dd
->tags
, &lim
, dd
);
3431 if (IS_ERR(dd
->disk
)) {
3432 dev_err(&dd
->pdev
->dev
,
3433 "Unable to allocate request queue\n");
3435 goto block_queue_alloc_init_error
;
3437 dd
->queue
= dd
->disk
->queue
;
3439 rv
= ida_alloc(&rssd_index_ida
, GFP_KERNEL
);
3444 rv
= rssd_disk_name_format("rssd",
3446 dd
->disk
->disk_name
,
3449 goto disk_index_error
;
3451 dd
->disk
->major
= dd
->major
;
3452 dd
->disk
->first_minor
= index
* MTIP_MAX_MINORS
;
3453 dd
->disk
->minors
= MTIP_MAX_MINORS
;
3454 dd
->disk
->fops
= &mtip_block_ops
;
3455 dd
->disk
->private_data
= dd
;
3458 mtip_hw_debugfs_init(dd
);
3461 /* Initialize the protocol layer. */
3462 wait_for_rebuild
= mtip_hw_get_identify(dd
);
3463 if (wait_for_rebuild
< 0) {
3464 dev_err(&dd
->pdev
->dev
,
3465 "Protocol layer initialization failed\n");
3467 goto init_hw_cmds_error
;
3471 * if rebuild pending, start the service thread, and delay the block
3472 * queue creation and device_add_disk()
3474 if (wait_for_rebuild
== MTIP_FTL_REBUILD_MAGIC
)
3475 goto start_service_thread
;
3477 /* Set device limits. */
3478 dma_set_max_seg_size(&dd
->pdev
->dev
, 0x400000);
3480 /* Set the capacity of the device in 512 byte sectors. */
3481 if (!(mtip_hw_get_capacity(dd
, &capacity
))) {
3482 dev_warn(&dd
->pdev
->dev
,
3483 "Could not read drive capacity\n");
3485 goto read_capacity_error
;
3487 set_capacity(dd
->disk
, capacity
);
3489 /* Enable the block device and add it to /dev */
3490 rv
= device_add_disk(&dd
->pdev
->dev
, dd
->disk
, mtip_disk_attr_groups
);
3492 goto read_capacity_error
;
3494 if (dd
->mtip_svc_handler
) {
3495 set_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
);
3496 return rv
; /* service thread created for handling rebuild */
3499 start_service_thread
:
3500 dd
->mtip_svc_handler
= kthread_create_on_node(mtip_service_thread
,
3502 "mtip_svc_thd_%02d", index
);
3504 if (IS_ERR(dd
->mtip_svc_handler
)) {
3505 dev_err(&dd
->pdev
->dev
, "service thread failed to start\n");
3506 dd
->mtip_svc_handler
= NULL
;
3508 goto kthread_run_error
;
3510 wake_up_process(dd
->mtip_svc_handler
);
3511 if (wait_for_rebuild
== MTIP_FTL_REBUILD_MAGIC
)
3512 rv
= wait_for_rebuild
;
3517 /* Delete our gendisk. This also removes the device from /dev */
3518 del_gendisk(dd
->disk
);
3519 read_capacity_error
:
3521 mtip_hw_debugfs_exit(dd
);
3523 ida_free(&rssd_index_ida
, index
);
3526 block_queue_alloc_init_error
:
3527 blk_mq_free_tag_set(&dd
->tags
);
3528 block_queue_alloc_tag_error
:
3529 mtip_hw_exit(dd
); /* De-initialize the protocol layer. */
3530 protocol_init_error
:
3535 * Function called by the PCI layer when just before the
3536 * machine shuts down.
3538 * If a protocol layer shutdown function is present it will be called
3541 * @dd Pointer to the driver data structure.
3546 static int mtip_block_shutdown(struct driver_data
*dd
)
3548 mtip_hw_shutdown(dd
);
3550 dev_info(&dd
->pdev
->dev
,
3551 "Shutting down %s ...\n", dd
->disk
->disk_name
);
3553 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
))
3554 del_gendisk(dd
->disk
);
3556 blk_mq_free_tag_set(&dd
->tags
);
3561 static int mtip_block_suspend(struct driver_data
*dd
)
3563 dev_info(&dd
->pdev
->dev
,
3564 "Suspending %s ...\n", dd
->disk
->disk_name
);
3565 mtip_hw_suspend(dd
);
3569 static int mtip_block_resume(struct driver_data
*dd
)
3571 dev_info(&dd
->pdev
->dev
, "Resuming %s ...\n",
3572 dd
->disk
->disk_name
);
3577 static void drop_cpu(int cpu
)
3582 static int get_least_used_cpu_on_node(int node
)
3584 int cpu
, least_used_cpu
, least_cnt
;
3585 const struct cpumask
*node_mask
;
3587 node_mask
= cpumask_of_node(node
);
3588 least_used_cpu
= cpumask_first(node_mask
);
3589 least_cnt
= cpu_use
[least_used_cpu
];
3590 cpu
= least_used_cpu
;
3592 for_each_cpu(cpu
, node_mask
) {
3593 if (cpu_use
[cpu
] < least_cnt
) {
3594 least_used_cpu
= cpu
;
3595 least_cnt
= cpu_use
[cpu
];
3598 cpu_use
[least_used_cpu
]++;
3599 return least_used_cpu
;
3602 /* Helper for selecting a node in round robin mode */
3603 static inline int mtip_get_next_rr_node(void)
3605 static int next_node
= NUMA_NO_NODE
;
3607 if (next_node
== NUMA_NO_NODE
) {
3608 next_node
= first_online_node
;
3612 next_node
= next_online_node(next_node
);
3613 if (next_node
== MAX_NUMNODES
)
3614 next_node
= first_online_node
;
3618 static DEFINE_HANDLER(0);
3619 static DEFINE_HANDLER(1);
3620 static DEFINE_HANDLER(2);
3621 static DEFINE_HANDLER(3);
3622 static DEFINE_HANDLER(4);
3623 static DEFINE_HANDLER(5);
3624 static DEFINE_HANDLER(6);
3625 static DEFINE_HANDLER(7);
3627 static void mtip_disable_link_opts(struct driver_data
*dd
, struct pci_dev
*pdev
)
3629 unsigned short pcie_dev_ctrl
;
3631 if (pci_is_pcie(pdev
)) {
3632 pcie_capability_read_word(pdev
, PCI_EXP_DEVCTL
, &pcie_dev_ctrl
);
3633 if (pcie_dev_ctrl
& PCI_EXP_DEVCTL_NOSNOOP_EN
||
3634 pcie_dev_ctrl
& PCI_EXP_DEVCTL_RELAX_EN
) {
3635 dev_info(&dd
->pdev
->dev
,
3636 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
3637 pdev
->vendor
, pdev
->device
);
3638 pcie_dev_ctrl
&= ~(PCI_EXP_DEVCTL_NOSNOOP_EN
|
3639 PCI_EXP_DEVCTL_RELAX_EN
);
3640 pcie_capability_write_word(pdev
, PCI_EXP_DEVCTL
,
3646 static void mtip_fix_ero_nosnoop(struct driver_data
*dd
, struct pci_dev
*pdev
)
3649 * This workaround is specific to AMD/ATI chipset with a PCI upstream
3650 * device with device id 0x5aXX
3652 if (pdev
->bus
&& pdev
->bus
->self
) {
3653 if (pdev
->bus
->self
->vendor
== PCI_VENDOR_ID_ATI
&&
3654 ((pdev
->bus
->self
->device
& 0xff00) == 0x5a00)) {
3655 mtip_disable_link_opts(dd
, pdev
->bus
->self
);
3657 /* Check further up the topology */
3658 struct pci_dev
*parent_dev
= pdev
->bus
->self
;
3659 if (parent_dev
->bus
&&
3660 parent_dev
->bus
->parent
&&
3661 parent_dev
->bus
->parent
->self
&&
3662 parent_dev
->bus
->parent
->self
->vendor
==
3663 PCI_VENDOR_ID_ATI
&&
3664 (parent_dev
->bus
->parent
->self
->device
&
3665 0xff00) == 0x5a00) {
3666 mtip_disable_link_opts(dd
,
3667 parent_dev
->bus
->parent
->self
);
3674 * Called for each supported PCI device detected.
3676 * This function allocates the private data structure, enables the
3677 * PCI device and then calls the block layer initialization function.
3680 * 0 on success else an error code.
3682 static int mtip_pci_probe(struct pci_dev
*pdev
,
3683 const struct pci_device_id
*ent
)
3686 struct driver_data
*dd
= NULL
;
3688 const struct cpumask
*node_mask
;
3689 int cpu
, i
= 0, j
= 0;
3690 int my_node
= NUMA_NO_NODE
;
3692 /* Allocate memory for this devices private data. */
3693 my_node
= pcibus_to_node(pdev
->bus
);
3694 if (my_node
!= NUMA_NO_NODE
) {
3695 if (!node_online(my_node
))
3696 my_node
= mtip_get_next_rr_node();
3698 dev_info(&pdev
->dev
, "Kernel not reporting proximity, choosing a node\n");
3699 my_node
= mtip_get_next_rr_node();
3701 dev_info(&pdev
->dev
, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
3702 my_node
, pcibus_to_node(pdev
->bus
), dev_to_node(&pdev
->dev
),
3703 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
3705 dd
= kzalloc_node(sizeof(struct driver_data
), GFP_KERNEL
, my_node
);
3709 /* Attach the private data to this PCI device. */
3710 pci_set_drvdata(pdev
, dd
);
3712 rv
= pcim_enable_device(pdev
);
3714 dev_err(&pdev
->dev
, "Unable to enable device\n");
3718 rv
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(64));
3720 dev_warn(&pdev
->dev
, "64-bit DMA enable failed\n");
3724 /* Copy the info we may need later into the private data structure. */
3725 dd
->major
= mtip_major
;
3726 dd
->instance
= instance
;
3728 dd
->numa_node
= my_node
;
3730 memset(dd
->workq_name
, 0, 32);
3731 snprintf(dd
->workq_name
, 31, "mtipq%d", dd
->instance
);
3733 dd
->isr_workq
= create_workqueue(dd
->workq_name
);
3734 if (!dd
->isr_workq
) {
3735 dev_warn(&pdev
->dev
, "Can't create wq %d\n", dd
->instance
);
3740 memset(cpu_list
, 0, sizeof(cpu_list
));
3742 node_mask
= cpumask_of_node(dd
->numa_node
);
3743 if (!cpumask_empty(node_mask
)) {
3744 for_each_cpu(cpu
, node_mask
)
3746 snprintf(&cpu_list
[j
], 256 - j
, "%d ", cpu
);
3747 j
= strlen(cpu_list
);
3750 dev_info(&pdev
->dev
, "Node %d on package %d has %d cpu(s): %s\n",
3752 topology_physical_package_id(cpumask_first(node_mask
)),
3753 nr_cpus_node(dd
->numa_node
),
3756 dev_dbg(&pdev
->dev
, "mtip32xx: node_mask empty\n");
3758 dd
->isr_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
3759 dev_info(&pdev
->dev
, "Initial IRQ binding node:cpu %d:%d\n",
3760 cpu_to_node(dd
->isr_binding
), dd
->isr_binding
);
3762 /* first worker context always runs in ISR */
3763 dd
->work
[0].cpu_binding
= dd
->isr_binding
;
3764 dd
->work
[1].cpu_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
3765 dd
->work
[2].cpu_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
3766 dd
->work
[3].cpu_binding
= dd
->work
[0].cpu_binding
;
3767 dd
->work
[4].cpu_binding
= dd
->work
[1].cpu_binding
;
3768 dd
->work
[5].cpu_binding
= dd
->work
[2].cpu_binding
;
3769 dd
->work
[6].cpu_binding
= dd
->work
[2].cpu_binding
;
3770 dd
->work
[7].cpu_binding
= dd
->work
[1].cpu_binding
;
3772 /* Log the bindings */
3773 for_each_present_cpu(cpu
) {
3774 memset(cpu_list
, 0, sizeof(cpu_list
));
3775 for (i
= 0, j
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++) {
3776 if (dd
->work
[i
].cpu_binding
== cpu
) {
3777 snprintf(&cpu_list
[j
], 256 - j
, "%d ", i
);
3778 j
= strlen(cpu_list
);
3782 dev_info(&pdev
->dev
, "CPU %d: WQs %s\n", cpu
, cpu_list
);
3785 INIT_WORK(&dd
->work
[0].work
, mtip_workq_sdbf0
);
3786 INIT_WORK(&dd
->work
[1].work
, mtip_workq_sdbf1
);
3787 INIT_WORK(&dd
->work
[2].work
, mtip_workq_sdbf2
);
3788 INIT_WORK(&dd
->work
[3].work
, mtip_workq_sdbf3
);
3789 INIT_WORK(&dd
->work
[4].work
, mtip_workq_sdbf4
);
3790 INIT_WORK(&dd
->work
[5].work
, mtip_workq_sdbf5
);
3791 INIT_WORK(&dd
->work
[6].work
, mtip_workq_sdbf6
);
3792 INIT_WORK(&dd
->work
[7].work
, mtip_workq_sdbf7
);
3794 pci_set_master(pdev
);
3795 rv
= pci_enable_msi(pdev
);
3797 dev_warn(&pdev
->dev
,
3798 "Unable to enable MSI interrupt.\n");
3799 goto msi_initialize_err
;
3802 mtip_fix_ero_nosnoop(dd
, pdev
);
3804 /* Initialize the block layer. */
3805 rv
= mtip_block_initialize(dd
);
3808 "Unable to initialize block layer\n");
3809 goto block_initialize_err
;
3813 * Increment the instance count so that each device has a unique
3817 if (rv
!= MTIP_FTL_REBUILD_MAGIC
)
3818 set_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
);
3820 rv
= 0; /* device in rebuild state, return 0 from probe */
3824 block_initialize_err
:
3825 pci_disable_msi(pdev
);
3828 if (dd
->isr_workq
) {
3829 destroy_workqueue(dd
->isr_workq
);
3830 drop_cpu(dd
->work
[0].cpu_binding
);
3831 drop_cpu(dd
->work
[1].cpu_binding
);
3832 drop_cpu(dd
->work
[2].cpu_binding
);
3835 pcim_iounmap_regions(pdev
, 1 << MTIP_ABAR
);
3839 pci_set_drvdata(pdev
, NULL
);
3846 * Called for each probed device when the device is removed or the
3847 * driver is unloaded.
3852 static void mtip_pci_remove(struct pci_dev
*pdev
)
3854 struct driver_data
*dd
= pci_get_drvdata(pdev
);
3857 mtip_check_surprise_removal(dd
);
3858 synchronize_irq(dd
->pdev
->irq
);
3860 /* Spin until workers are done */
3861 to
= jiffies
+ msecs_to_jiffies(4000);
3864 } while (atomic_read(&dd
->irq_workers_active
) != 0 &&
3865 time_before(jiffies
, to
));
3867 if (atomic_read(&dd
->irq_workers_active
) != 0) {
3868 dev_warn(&dd
->pdev
->dev
,
3869 "Completion workers still active!\n");
3872 set_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
);
3874 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
))
3875 del_gendisk(dd
->disk
);
3877 mtip_hw_debugfs_exit(dd
);
3879 if (dd
->mtip_svc_handler
) {
3880 set_bit(MTIP_PF_SVC_THD_STOP_BIT
, &dd
->port
->flags
);
3881 wake_up_interruptible(&dd
->port
->svc_wait
);
3882 kthread_stop(dd
->mtip_svc_handler
);
3887 * Explicitly wait here for IOs to quiesce,
3888 * as mtip_standby_drive usually won't wait for IOs.
3890 if (!mtip_quiesce_io(dd
->port
, MTIP_QUIESCE_IO_TIMEOUT_MS
))
3891 mtip_standby_drive(dd
);
3894 dev_info(&dd
->pdev
->dev
, "device %s surprise removal\n",
3895 dd
->disk
->disk_name
);
3897 blk_mq_free_tag_set(&dd
->tags
);
3899 /* De-initialize the protocol layer. */
3902 if (dd
->isr_workq
) {
3903 destroy_workqueue(dd
->isr_workq
);
3904 drop_cpu(dd
->work
[0].cpu_binding
);
3905 drop_cpu(dd
->work
[1].cpu_binding
);
3906 drop_cpu(dd
->work
[2].cpu_binding
);
3909 pci_disable_msi(pdev
);
3911 pcim_iounmap_regions(pdev
, 1 << MTIP_ABAR
);
3912 pci_set_drvdata(pdev
, NULL
);
3918 * Called for each probed device when the device is suspended.
3924 static int __maybe_unused
mtip_pci_suspend(struct device
*dev
)
3927 struct driver_data
*dd
= dev_get_drvdata(dev
);
3929 set_bit(MTIP_DDF_RESUME_BIT
, &dd
->dd_flag
);
3931 /* Disable ports & interrupts then send standby immediate */
3932 rv
= mtip_block_suspend(dd
);
3934 dev_err(dev
, "Failed to suspend controller\n");
3940 * Called for each probed device when the device is resumed.
3946 static int __maybe_unused
mtip_pci_resume(struct device
*dev
)
3949 struct driver_data
*dd
= dev_get_drvdata(dev
);
3952 * Calls hbaReset, initPort, & startPort function
3953 * then enables interrupts
3955 rv
= mtip_block_resume(dd
);
3957 dev_err(dev
, "Unable to resume\n");
3959 clear_bit(MTIP_DDF_RESUME_BIT
, &dd
->dd_flag
);
3970 static void mtip_pci_shutdown(struct pci_dev
*pdev
)
3972 struct driver_data
*dd
= pci_get_drvdata(pdev
);
3974 mtip_block_shutdown(dd
);
3977 /* Table of device ids supported by this driver. */
3978 static const struct pci_device_id mtip_pci_tbl
[] = {
3979 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320H_DEVICE_ID
) },
3980 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320M_DEVICE_ID
) },
3981 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320S_DEVICE_ID
) },
3982 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P325M_DEVICE_ID
) },
3983 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P420H_DEVICE_ID
) },
3984 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P420M_DEVICE_ID
) },
3985 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P425M_DEVICE_ID
) },
3989 static SIMPLE_DEV_PM_OPS(mtip_pci_pm_ops
, mtip_pci_suspend
, mtip_pci_resume
);
3991 /* Structure that describes the PCI driver functions. */
3992 static struct pci_driver mtip_pci_driver
= {
3993 .name
= MTIP_DRV_NAME
,
3994 .id_table
= mtip_pci_tbl
,
3995 .probe
= mtip_pci_probe
,
3996 .remove
= mtip_pci_remove
,
3997 .driver
.pm
= &mtip_pci_pm_ops
,
3998 .shutdown
= mtip_pci_shutdown
,
4001 MODULE_DEVICE_TABLE(pci
, mtip_pci_tbl
);
4004 * Module initialization function.
4006 * Called once when the module is loaded. This function allocates a major
4007 * block device number to the Cyclone devices and registers the PCI layer
4011 * 0 on success else error code.
4013 static int __init
mtip_init(void)
4017 pr_info(MTIP_DRV_NAME
" Version " MTIP_DRV_VERSION
"\n");
4019 /* Allocate a major block device number to use with this driver. */
4020 error
= register_blkdev(0, MTIP_DRV_NAME
);
4022 pr_err("Unable to register block device (%d)\n",
4028 dfs_parent
= debugfs_create_dir("rssd", NULL
);
4030 /* Register our PCI operations. */
4031 error
= pci_register_driver(&mtip_pci_driver
);
4033 debugfs_remove(dfs_parent
);
4034 unregister_blkdev(mtip_major
, MTIP_DRV_NAME
);
4041 * Module de-initialization function.
4043 * Called once when the module is unloaded. This function deallocates
4044 * the major block device number allocated by mtip_init() and
4045 * unregisters the PCI layer of the driver.
4050 static void __exit
mtip_exit(void)
4052 /* Release the allocated major block device number. */
4053 unregister_blkdev(mtip_major
, MTIP_DRV_NAME
);
4055 /* Unregister the PCI driver. */
4056 pci_unregister_driver(&mtip_pci_driver
);
4058 debugfs_remove_recursive(dfs_parent
);
4061 MODULE_AUTHOR("Micron Technology, Inc");
4062 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4063 MODULE_LICENSE("GPL");
4064 MODULE_VERSION(MTIP_DRV_VERSION
);
4066 module_init(mtip_init
);
4067 module_exit(mtip_exit
);