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/genhd.h>
23 #include <linux/blkdev.h>
24 #include <linux/blk-mq.h>
25 #include <linux/bio.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/idr.h>
28 #include <linux/kthread.h>
29 #include <../drivers/ata/ahci.h>
30 #include <linux/export.h>
31 #include <linux/debugfs.h>
32 #include <linux/prefetch.h>
33 #include <linux/numa.h>
36 #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
38 /* DMA region containing RX Fis, Identify, RLE10, and SMART buffers */
39 #define AHCI_RX_FIS_SZ 0x100
40 #define AHCI_RX_FIS_OFFSET 0x0
41 #define AHCI_IDFY_SZ ATA_SECT_SIZE
42 #define AHCI_IDFY_OFFSET 0x400
43 #define AHCI_SECTBUF_SZ ATA_SECT_SIZE
44 #define AHCI_SECTBUF_OFFSET 0x800
45 #define AHCI_SMARTBUF_SZ ATA_SECT_SIZE
46 #define AHCI_SMARTBUF_OFFSET 0xC00
47 /* 0x100 + 0x200 + 0x200 + 0x200 is smaller than 4k but we pad it out */
48 #define BLOCK_DMA_ALLOC_SZ 4096
50 /* DMA region containing command table (should be 8192 bytes) */
51 #define AHCI_CMD_SLOT_SZ sizeof(struct mtip_cmd_hdr)
52 #define AHCI_CMD_TBL_SZ (MTIP_MAX_COMMAND_SLOTS * AHCI_CMD_SLOT_SZ)
53 #define AHCI_CMD_TBL_OFFSET 0x0
55 /* DMA region per command (contains header and SGL) */
56 #define AHCI_CMD_TBL_HDR_SZ 0x80
57 #define AHCI_CMD_TBL_HDR_OFFSET 0x0
58 #define AHCI_CMD_TBL_SGL_SZ (MTIP_MAX_SG * sizeof(struct mtip_cmd_sg))
59 #define AHCI_CMD_TBL_SGL_OFFSET AHCI_CMD_TBL_HDR_SZ
60 #define CMD_DMA_ALLOC_SZ (AHCI_CMD_TBL_SGL_SZ + AHCI_CMD_TBL_HDR_SZ)
63 #define HOST_CAP_NZDMA (1 << 19)
64 #define HOST_HSORG 0xFC
65 #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
66 #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
67 #define HSORG_HWREV 0xFF00
68 #define HSORG_STYLE 0x8
69 #define HSORG_SLOTGROUPS 0x7
71 #define PORT_COMMAND_ISSUE 0x38
72 #define PORT_SDBV 0x7C
74 #define PORT_OFFSET 0x100
75 #define PORT_MEM_SIZE 0x80
77 #define PORT_IRQ_ERR \
78 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
79 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
80 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
82 #define PORT_IRQ_LEGACY \
83 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
84 #define PORT_IRQ_HANDLED \
85 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
86 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
87 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
88 #define DEF_PORT_IRQ \
89 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
92 #define MTIP_PRODUCT_UNKNOWN 0x00
93 #define MTIP_PRODUCT_ASICFPGA 0x11
95 /* Device instance number, incremented each time a device is probed. */
98 static struct list_head online_list
;
99 static struct list_head removing_list
;
100 static spinlock_t dev_lock
;
103 * Global variable used to hold the major block device number
104 * allocated in mtip_init().
106 static int mtip_major
;
107 static struct dentry
*dfs_parent
;
108 static struct dentry
*dfs_device_status
;
110 static u32 cpu_use
[NR_CPUS
];
112 static DEFINE_IDA(rssd_index_ida
);
114 static int mtip_block_initialize(struct driver_data
*dd
);
117 struct mtip_compat_ide_task_request_s
{
120 ide_reg_valid_t out_flags
;
121 ide_reg_valid_t in_flags
;
124 compat_ulong_t out_size
;
125 compat_ulong_t in_size
;
130 * This function check_for_surprise_removal is called
131 * while card is removed from the system and it will
132 * read the vendor id from the configuration space
134 * @pdev Pointer to the pci_dev structure.
137 * true if device removed, else false
139 static bool mtip_check_surprise_removal(struct pci_dev
*pdev
)
142 struct driver_data
*dd
= pci_get_drvdata(pdev
);
147 /* Read the vendorID from the configuration space */
148 pci_read_config_word(pdev
, 0x00, &vendor_id
);
149 if (vendor_id
== 0xFFFF) {
152 blk_queue_flag_set(QUEUE_FLAG_DEAD
, dd
->queue
);
154 dev_warn(&dd
->pdev
->dev
,
155 "%s: dd->queue is NULL\n", __func__
);
156 return true; /* device removed */
159 return false; /* device present */
162 static struct mtip_cmd
*mtip_cmd_from_tag(struct driver_data
*dd
,
165 struct blk_mq_hw_ctx
*hctx
= dd
->queue
->queue_hw_ctx
[0];
167 return blk_mq_rq_to_pdu(blk_mq_tag_to_rq(hctx
->tags
, tag
));
171 * Reset the HBA (without sleeping)
173 * @dd Pointer to the driver data structure.
176 * 0 The reset was successful.
177 * -1 The HBA Reset bit did not clear.
179 static int mtip_hba_reset(struct driver_data
*dd
)
181 unsigned long timeout
;
183 /* Set the reset bit */
184 writel(HOST_RESET
, dd
->mmio
+ HOST_CTL
);
187 readl(dd
->mmio
+ HOST_CTL
);
190 * Spin for up to 10 seconds waiting for reset acknowledgement. Spec
191 * is 1 sec but in LUN failure conditions, up to 10 secs are required
193 timeout
= jiffies
+ msecs_to_jiffies(10000);
196 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))
199 } while ((readl(dd
->mmio
+ HOST_CTL
) & HOST_RESET
)
200 && time_before(jiffies
, timeout
));
202 if (readl(dd
->mmio
+ HOST_CTL
) & HOST_RESET
)
209 * Issue a command to the hardware.
211 * Set the appropriate bit in the s_active and Command Issue hardware
212 * registers, causing hardware command processing to begin.
214 * @port Pointer to the port structure.
215 * @tag The tag of the command to be issued.
220 static inline void mtip_issue_ncq_command(struct mtip_port
*port
, int tag
)
222 int group
= tag
>> 5;
224 /* guard SACT and CI registers */
225 spin_lock(&port
->cmd_issue_lock
[group
]);
226 writel((1 << MTIP_TAG_BIT(tag
)),
227 port
->s_active
[MTIP_TAG_INDEX(tag
)]);
228 writel((1 << MTIP_TAG_BIT(tag
)),
229 port
->cmd_issue
[MTIP_TAG_INDEX(tag
)]);
230 spin_unlock(&port
->cmd_issue_lock
[group
]);
234 * Enable/disable the reception of FIS
236 * @port Pointer to the port data structure
237 * @enable 1 to enable, 0 to disable
240 * Previous state: 1 enabled, 0 disabled
242 static int mtip_enable_fis(struct mtip_port
*port
, int enable
)
246 /* enable FIS reception */
247 tmp
= readl(port
->mmio
+ PORT_CMD
);
249 writel(tmp
| PORT_CMD_FIS_RX
, port
->mmio
+ PORT_CMD
);
251 writel(tmp
& ~PORT_CMD_FIS_RX
, port
->mmio
+ PORT_CMD
);
254 readl(port
->mmio
+ PORT_CMD
);
256 return (((tmp
& PORT_CMD_FIS_RX
) == PORT_CMD_FIS_RX
));
260 * Enable/disable the DMA engine
262 * @port Pointer to the port data structure
263 * @enable 1 to enable, 0 to disable
266 * Previous state: 1 enabled, 0 disabled.
268 static int mtip_enable_engine(struct mtip_port
*port
, int enable
)
272 /* enable FIS reception */
273 tmp
= readl(port
->mmio
+ PORT_CMD
);
275 writel(tmp
| PORT_CMD_START
, port
->mmio
+ PORT_CMD
);
277 writel(tmp
& ~PORT_CMD_START
, port
->mmio
+ PORT_CMD
);
279 readl(port
->mmio
+ PORT_CMD
);
280 return (((tmp
& PORT_CMD_START
) == PORT_CMD_START
));
284 * Enables the port DMA engine and FIS reception.
289 static inline void mtip_start_port(struct mtip_port
*port
)
291 /* Enable FIS reception */
292 mtip_enable_fis(port
, 1);
294 /* Enable the DMA engine */
295 mtip_enable_engine(port
, 1);
299 * Deinitialize a port by disabling port interrupts, the DMA engine,
302 * @port Pointer to the port structure
307 static inline void mtip_deinit_port(struct mtip_port
*port
)
309 /* Disable interrupts on this port */
310 writel(0, port
->mmio
+ PORT_IRQ_MASK
);
312 /* Disable the DMA engine */
313 mtip_enable_engine(port
, 0);
315 /* Disable FIS reception */
316 mtip_enable_fis(port
, 0);
322 * This function deinitializes the port by calling mtip_deinit_port() and
323 * then initializes it by setting the command header and RX FIS addresses,
324 * clearing the SError register and any pending port interrupts before
325 * re-enabling the default set of port interrupts.
327 * @port Pointer to the port structure.
332 static void mtip_init_port(struct mtip_port
*port
)
335 mtip_deinit_port(port
);
337 /* Program the command list base and FIS base addresses */
338 if (readl(port
->dd
->mmio
+ HOST_CAP
) & HOST_CAP_64
) {
339 writel((port
->command_list_dma
>> 16) >> 16,
340 port
->mmio
+ PORT_LST_ADDR_HI
);
341 writel((port
->rxfis_dma
>> 16) >> 16,
342 port
->mmio
+ PORT_FIS_ADDR_HI
);
343 set_bit(MTIP_PF_HOST_CAP_64
, &port
->flags
);
346 writel(port
->command_list_dma
& 0xFFFFFFFF,
347 port
->mmio
+ PORT_LST_ADDR
);
348 writel(port
->rxfis_dma
& 0xFFFFFFFF, port
->mmio
+ PORT_FIS_ADDR
);
351 writel(readl(port
->mmio
+ PORT_SCR_ERR
), port
->mmio
+ PORT_SCR_ERR
);
353 /* reset the completed registers.*/
354 for (i
= 0; i
< port
->dd
->slot_groups
; i
++)
355 writel(0xFFFFFFFF, port
->completed
[i
]);
357 /* Clear any pending interrupts for this port */
358 writel(readl(port
->mmio
+ PORT_IRQ_STAT
), port
->mmio
+ PORT_IRQ_STAT
);
360 /* Clear any pending interrupts on the HBA. */
361 writel(readl(port
->dd
->mmio
+ HOST_IRQ_STAT
),
362 port
->dd
->mmio
+ HOST_IRQ_STAT
);
364 /* Enable port interrupts */
365 writel(DEF_PORT_IRQ
, port
->mmio
+ PORT_IRQ_MASK
);
371 * @port Pointer to the port data structure.
376 static void mtip_restart_port(struct mtip_port
*port
)
378 unsigned long timeout
;
380 /* Disable the DMA engine */
381 mtip_enable_engine(port
, 0);
383 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
384 timeout
= jiffies
+ msecs_to_jiffies(500);
385 while ((readl(port
->mmio
+ PORT_CMD
) & PORT_CMD_LIST_ON
)
386 && time_before(jiffies
, timeout
))
389 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
393 * Chip quirk: escalate to hba reset if
394 * PxCMD.CR not clear after 500 ms
396 if (readl(port
->mmio
+ PORT_CMD
) & PORT_CMD_LIST_ON
) {
397 dev_warn(&port
->dd
->pdev
->dev
,
398 "PxCMD.CR not clear, escalating reset\n");
400 if (mtip_hba_reset(port
->dd
))
401 dev_err(&port
->dd
->pdev
->dev
,
402 "HBA reset escalation failed.\n");
404 /* 30 ms delay before com reset to quiesce chip */
408 dev_warn(&port
->dd
->pdev
->dev
, "Issuing COM reset\n");
411 writel(readl(port
->mmio
+ PORT_SCR_CTL
) |
412 1, port
->mmio
+ PORT_SCR_CTL
);
413 readl(port
->mmio
+ PORT_SCR_CTL
);
415 /* Wait 1 ms to quiesce chip function */
416 timeout
= jiffies
+ msecs_to_jiffies(1);
417 while (time_before(jiffies
, timeout
))
420 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
423 /* Clear PxSCTL.DET */
424 writel(readl(port
->mmio
+ PORT_SCR_CTL
) & ~1,
425 port
->mmio
+ PORT_SCR_CTL
);
426 readl(port
->mmio
+ PORT_SCR_CTL
);
428 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
429 timeout
= jiffies
+ msecs_to_jiffies(500);
430 while (((readl(port
->mmio
+ PORT_SCR_STAT
) & 0x01) == 0)
431 && time_before(jiffies
, timeout
))
434 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
437 if ((readl(port
->mmio
+ PORT_SCR_STAT
) & 0x01) == 0)
438 dev_warn(&port
->dd
->pdev
->dev
,
439 "COM reset failed\n");
441 mtip_init_port(port
);
442 mtip_start_port(port
);
446 static int mtip_device_reset(struct driver_data
*dd
)
450 if (mtip_check_surprise_removal(dd
->pdev
))
453 if (mtip_hba_reset(dd
) < 0)
457 mtip_init_port(dd
->port
);
458 mtip_start_port(dd
->port
);
460 /* Enable interrupts on the HBA. */
461 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
462 dd
->mmio
+ HOST_CTL
);
467 * Helper function for tag logging
469 static void print_tags(struct driver_data
*dd
,
471 unsigned long *tagbits
,
474 unsigned char tagmap
[128];
475 int group
, tagmap_len
= 0;
477 memset(tagmap
, 0, sizeof(tagmap
));
478 for (group
= SLOTBITS_IN_LONGS
; group
> 0; group
--)
479 tagmap_len
+= sprintf(tagmap
+ tagmap_len
, "%016lX ",
481 dev_warn(&dd
->pdev
->dev
,
482 "%d command(s) %s: tagmap [%s]", cnt
, msg
, tagmap
);
485 static int mtip_read_log_page(struct mtip_port
*port
, u8 page
, u16
*buffer
,
486 dma_addr_t buffer_dma
, unsigned int sectors
);
487 static int mtip_get_smart_attr(struct mtip_port
*port
, unsigned int id
,
488 struct smart_attr
*attrib
);
490 static void mtip_complete_command(struct mtip_cmd
*cmd
, blk_status_t status
)
492 struct request
*req
= blk_mq_rq_from_pdu(cmd
);
494 cmd
->status
= status
;
495 blk_mq_complete_request(req
);
501 * @dd Pointer to the DRIVER_DATA structure.
506 static void mtip_handle_tfe(struct driver_data
*dd
)
508 int group
, tag
, bit
, reissue
, rv
;
509 struct mtip_port
*port
;
510 struct mtip_cmd
*cmd
;
512 struct host_to_dev_fis
*fis
;
513 unsigned long tagaccum
[SLOTBITS_IN_LONGS
];
514 unsigned int cmd_cnt
= 0;
516 char *fail_reason
= NULL
;
517 int fail_all_ncq_write
= 0, fail_all_ncq_cmds
= 0;
519 dev_warn(&dd
->pdev
->dev
, "Taskfile error\n");
523 if (test_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
)) {
524 cmd
= mtip_cmd_from_tag(dd
, MTIP_TAG_INTERNAL
);
525 dbg_printk(MTIP_DRV_NAME
" TFE for the internal command\n");
526 mtip_complete_command(cmd
, BLK_STS_IOERR
);
530 /* clear the tag accumulator */
531 memset(tagaccum
, 0, SLOTBITS_IN_LONGS
* sizeof(long));
533 /* Loop through all the groups */
534 for (group
= 0; group
< dd
->slot_groups
; group
++) {
535 completed
= readl(port
->completed
[group
]);
537 dev_warn(&dd
->pdev
->dev
, "g=%u, comp=%x\n", group
, completed
);
539 /* clear completed status register in the hardware.*/
540 writel(completed
, port
->completed
[group
]);
542 /* Process successfully completed commands */
543 for (bit
= 0; bit
< 32 && completed
; bit
++) {
544 if (!(completed
& (1<<bit
)))
546 tag
= (group
<< 5) + bit
;
548 /* Skip the internal command slot */
549 if (tag
== MTIP_TAG_INTERNAL
)
552 cmd
= mtip_cmd_from_tag(dd
, tag
);
553 mtip_complete_command(cmd
, 0);
554 set_bit(tag
, tagaccum
);
559 print_tags(dd
, "completed (TFE)", tagaccum
, cmd_cnt
);
561 /* Restart the port */
563 mtip_restart_port(port
);
565 /* Trying to determine the cause of the error */
566 rv
= mtip_read_log_page(dd
->port
, ATA_LOG_SATA_NCQ
,
568 dd
->port
->log_buf_dma
, 1);
570 dev_warn(&dd
->pdev
->dev
,
571 "Error in READ LOG EXT (10h) command\n");
572 /* non-critical error, don't fail the load */
574 buf
= (unsigned char *)dd
->port
->log_buf
;
575 if (buf
[259] & 0x1) {
576 dev_info(&dd
->pdev
->dev
,
577 "Write protect bit is set.\n");
578 set_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
);
579 fail_all_ncq_write
= 1;
580 fail_reason
= "write protect";
582 if (buf
[288] == 0xF7) {
583 dev_info(&dd
->pdev
->dev
,
584 "Exceeded Tmax, drive in thermal shutdown.\n");
585 set_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
);
586 fail_all_ncq_cmds
= 1;
587 fail_reason
= "thermal shutdown";
589 if (buf
[288] == 0xBF) {
590 set_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
);
591 dev_info(&dd
->pdev
->dev
,
592 "Drive indicates rebuild has failed. Secure erase required.\n");
593 fail_all_ncq_cmds
= 1;
594 fail_reason
= "rebuild failed";
598 /* clear the tag accumulator */
599 memset(tagaccum
, 0, SLOTBITS_IN_LONGS
* sizeof(long));
601 /* Loop through all the groups */
602 for (group
= 0; group
< dd
->slot_groups
; group
++) {
603 for (bit
= 0; bit
< 32; bit
++) {
605 tag
= (group
<< 5) + bit
;
606 cmd
= mtip_cmd_from_tag(dd
, tag
);
608 fis
= (struct host_to_dev_fis
*)cmd
->command
;
610 /* Should re-issue? */
611 if (tag
== MTIP_TAG_INTERNAL
||
612 fis
->command
== ATA_CMD_SET_FEATURES
)
615 if (fail_all_ncq_cmds
||
616 (fail_all_ncq_write
&&
617 fis
->command
== ATA_CMD_FPDMA_WRITE
)) {
618 dev_warn(&dd
->pdev
->dev
,
619 " Fail: %s w/tag %d [%s].\n",
620 fis
->command
== ATA_CMD_FPDMA_WRITE
?
623 fail_reason
!= NULL
?
624 fail_reason
: "unknown");
625 mtip_complete_command(cmd
, BLK_STS_MEDIUM
);
631 * First check if this command has
632 * exceeded its retries.
634 if (reissue
&& (cmd
->retries
-- > 0)) {
636 set_bit(tag
, tagaccum
);
638 /* Re-issue the command. */
639 mtip_issue_ncq_command(port
, tag
);
644 /* Retire a command that will not be reissued */
645 dev_warn(&port
->dd
->pdev
->dev
,
646 "retiring tag %d\n", tag
);
648 mtip_complete_command(cmd
, BLK_STS_IOERR
);
651 print_tags(dd
, "reissued (TFE)", tagaccum
, cmd_cnt
);
655 * Handle a set device bits interrupt
657 static inline void mtip_workq_sdbfx(struct mtip_port
*port
, int group
,
660 struct driver_data
*dd
= port
->dd
;
662 struct mtip_cmd
*command
;
665 WARN_ON_ONCE(!completed
);
668 /* clear completed status register in the hardware.*/
669 writel(completed
, port
->completed
[group
]);
671 /* Process completed commands. */
672 for (bit
= 0; (bit
< 32) && completed
; bit
++) {
673 if (completed
& 0x01) {
674 tag
= (group
<< 5) | bit
;
676 /* skip internal command slot. */
677 if (unlikely(tag
== MTIP_TAG_INTERNAL
))
680 command
= mtip_cmd_from_tag(dd
, tag
);
681 mtip_complete_command(command
, 0);
686 /* If last, re-enable interrupts */
687 if (atomic_dec_return(&dd
->irq_workers_active
) == 0)
688 writel(0xffffffff, dd
->mmio
+ HOST_IRQ_STAT
);
692 * Process legacy pio and d2h interrupts
694 static inline void mtip_process_legacy(struct driver_data
*dd
, u32 port_stat
)
696 struct mtip_port
*port
= dd
->port
;
697 struct mtip_cmd
*cmd
= mtip_cmd_from_tag(dd
, MTIP_TAG_INTERNAL
);
699 if (test_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
) && cmd
) {
700 int group
= MTIP_TAG_INDEX(MTIP_TAG_INTERNAL
);
701 int status
= readl(port
->cmd_issue
[group
]);
703 if (!(status
& (1 << MTIP_TAG_BIT(MTIP_TAG_INTERNAL
))))
704 mtip_complete_command(cmd
, 0);
709 * Demux and handle errors
711 static inline void mtip_process_errors(struct driver_data
*dd
, u32 port_stat
)
713 if (unlikely(port_stat
& PORT_IRQ_CONNECT
)) {
714 dev_warn(&dd
->pdev
->dev
,
715 "Clearing PxSERR.DIAG.x\n");
716 writel((1 << 26), dd
->port
->mmio
+ PORT_SCR_ERR
);
719 if (unlikely(port_stat
& PORT_IRQ_PHYRDY
)) {
720 dev_warn(&dd
->pdev
->dev
,
721 "Clearing PxSERR.DIAG.n\n");
722 writel((1 << 16), dd
->port
->mmio
+ PORT_SCR_ERR
);
725 if (unlikely(port_stat
& ~PORT_IRQ_HANDLED
)) {
726 dev_warn(&dd
->pdev
->dev
,
727 "Port stat errors %x unhandled\n",
728 (port_stat
& ~PORT_IRQ_HANDLED
));
729 if (mtip_check_surprise_removal(dd
->pdev
))
732 if (likely(port_stat
& (PORT_IRQ_TF_ERR
| PORT_IRQ_IF_ERR
))) {
733 set_bit(MTIP_PF_EH_ACTIVE_BIT
, &dd
->port
->flags
);
734 wake_up_interruptible(&dd
->port
->svc_wait
);
738 static inline irqreturn_t
mtip_handle_irq(struct driver_data
*data
)
740 struct driver_data
*dd
= (struct driver_data
*) data
;
741 struct mtip_port
*port
= dd
->port
;
742 u32 hba_stat
, port_stat
;
744 int do_irq_enable
= 1, i
, workers
;
745 struct mtip_work
*twork
;
747 hba_stat
= readl(dd
->mmio
+ HOST_IRQ_STAT
);
751 /* Acknowledge the interrupt status on the port.*/
752 port_stat
= readl(port
->mmio
+ PORT_IRQ_STAT
);
753 if (unlikely(port_stat
== 0xFFFFFFFF)) {
754 mtip_check_surprise_removal(dd
->pdev
);
757 writel(port_stat
, port
->mmio
+ PORT_IRQ_STAT
);
759 /* Demux port status */
760 if (likely(port_stat
& PORT_IRQ_SDB_FIS
)) {
762 WARN_ON_ONCE(atomic_read(&dd
->irq_workers_active
) != 0);
764 /* Start at 1: group zero is always local? */
765 for (i
= 0, workers
= 0; i
< MTIP_MAX_SLOT_GROUPS
;
767 twork
= &dd
->work
[i
];
768 twork
->completed
= readl(port
->completed
[i
]);
769 if (twork
->completed
)
773 atomic_set(&dd
->irq_workers_active
, workers
);
775 for (i
= 1; i
< MTIP_MAX_SLOT_GROUPS
; i
++) {
776 twork
= &dd
->work
[i
];
777 if (twork
->completed
)
784 if (likely(dd
->work
[0].completed
))
785 mtip_workq_sdbfx(port
, 0,
786 dd
->work
[0].completed
);
790 * Chip quirk: SDB interrupt but nothing
797 if (unlikely(port_stat
& PORT_IRQ_ERR
)) {
798 if (unlikely(mtip_check_surprise_removal(dd
->pdev
))) {
799 /* don't proceed further */
802 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
806 mtip_process_errors(dd
, port_stat
& PORT_IRQ_ERR
);
809 if (unlikely(port_stat
& PORT_IRQ_LEGACY
))
810 mtip_process_legacy(dd
, port_stat
& PORT_IRQ_LEGACY
);
813 /* acknowledge interrupt */
814 if (unlikely(do_irq_enable
))
815 writel(hba_stat
, dd
->mmio
+ HOST_IRQ_STAT
);
821 * HBA interrupt subroutine.
824 * @instance Pointer to the driver data structure.
827 * IRQ_HANDLED A HBA interrupt was pending and handled.
828 * IRQ_NONE This interrupt was not for the HBA.
830 static irqreturn_t
mtip_irq_handler(int irq
, void *instance
)
832 struct driver_data
*dd
= instance
;
834 return mtip_handle_irq(dd
);
837 static void mtip_issue_non_ncq_command(struct mtip_port
*port
, int tag
)
839 writel(1 << MTIP_TAG_BIT(tag
), port
->cmd_issue
[MTIP_TAG_INDEX(tag
)]);
842 static bool mtip_pause_ncq(struct mtip_port
*port
,
843 struct host_to_dev_fis
*fis
)
845 unsigned long task_file_data
;
847 task_file_data
= readl(port
->mmio
+PORT_TFDATA
);
848 if ((task_file_data
& 1))
851 if (fis
->command
== ATA_CMD_SEC_ERASE_PREP
) {
852 port
->ic_pause_timer
= jiffies
;
854 } else if ((fis
->command
== ATA_CMD_DOWNLOAD_MICRO
) &&
855 (fis
->features
== 0x03)) {
856 set_bit(MTIP_PF_DM_ACTIVE_BIT
, &port
->flags
);
857 port
->ic_pause_timer
= jiffies
;
859 } else if ((fis
->command
== ATA_CMD_SEC_ERASE_UNIT
) ||
860 ((fis
->command
== 0xFC) &&
861 (fis
->features
== 0x27 || fis
->features
== 0x72 ||
862 fis
->features
== 0x62 || fis
->features
== 0x26))) {
863 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
864 clear_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &port
->dd
->dd_flag
);
865 /* Com reset after secure erase or lowlevel format */
866 mtip_restart_port(port
);
867 clear_bit(MTIP_PF_SE_ACTIVE_BIT
, &port
->flags
);
874 static bool mtip_commands_active(struct mtip_port
*port
)
880 * Ignore s_active bit 0 of array element 0.
881 * This bit will always be set
883 active
= readl(port
->s_active
[0]) & 0xFFFFFFFE;
884 for (n
= 1; n
< port
->dd
->slot_groups
; n
++)
885 active
|= readl(port
->s_active
[n
]);
891 * Wait for port to quiesce
893 * @port Pointer to port data structure
894 * @timeout Max duration to wait (ms)
898 * -EBUSY Commands still active
900 static int mtip_quiesce_io(struct mtip_port
*port
, unsigned long timeout
)
905 blk_mq_quiesce_queue(port
->dd
->queue
);
907 to
= jiffies
+ msecs_to_jiffies(timeout
);
909 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
) &&
910 test_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
)) {
912 continue; /* svc thd is actively issuing commands */
917 if (mtip_check_surprise_removal(port
->dd
->pdev
))
920 active
= mtip_commands_active(port
);
923 } while (time_before(jiffies
, to
));
925 blk_mq_unquiesce_queue(port
->dd
->queue
);
926 return active
? -EBUSY
: 0;
928 blk_mq_unquiesce_queue(port
->dd
->queue
);
932 struct mtip_int_cmd
{
940 * Execute an internal command and wait for the completion.
942 * @port Pointer to the port data structure.
943 * @fis Pointer to the FIS that describes the command.
944 * @fis_len Length in WORDS of the FIS.
945 * @buffer DMA accessible for command data.
946 * @buf_len Length, in bytes, of the data buffer.
947 * @opts Command header options, excluding the FIS length
948 * and the number of PRD entries.
949 * @timeout Time in ms to wait for the command to complete.
952 * 0 Command completed successfully.
953 * -EFAULT The buffer address is not correctly aligned.
954 * -EBUSY Internal command or other IO in progress.
955 * -EAGAIN Time out waiting for command to complete.
957 static int mtip_exec_internal_command(struct mtip_port
*port
,
958 struct host_to_dev_fis
*fis
,
963 unsigned long timeout
)
965 struct mtip_cmd
*int_cmd
;
966 struct driver_data
*dd
= port
->dd
;
968 struct mtip_int_cmd icmd
= {
976 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
977 if (buffer
& 0x00000007) {
978 dev_err(&dd
->pdev
->dev
, "SG buffer is not 8 byte aligned\n");
982 if (mtip_check_surprise_removal(dd
->pdev
))
985 rq
= blk_mq_alloc_request(dd
->queue
, REQ_OP_DRV_IN
, BLK_MQ_REQ_RESERVED
);
987 dbg_printk(MTIP_DRV_NAME
"Unable to allocate tag for PIO cmd\n");
991 set_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
993 if (fis
->command
== ATA_CMD_SEC_ERASE_PREP
)
994 set_bit(MTIP_PF_SE_ACTIVE_BIT
, &port
->flags
);
996 clear_bit(MTIP_PF_DM_ACTIVE_BIT
, &port
->flags
);
998 if (fis
->command
!= ATA_CMD_STANDBYNOW1
) {
999 /* wait for io to complete if non atomic */
1000 if (mtip_quiesce_io(port
, MTIP_QUIESCE_IO_TIMEOUT_MS
) < 0) {
1001 dev_warn(&dd
->pdev
->dev
, "Failed to quiesce IO\n");
1002 blk_mq_free_request(rq
);
1003 clear_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1004 wake_up_interruptible(&port
->svc_wait
);
1009 /* Copy the command to the command table */
1010 int_cmd
= blk_mq_rq_to_pdu(rq
);
1011 int_cmd
->icmd
= &icmd
;
1012 memcpy(int_cmd
->command
, fis
, fis_len
*4);
1014 rq
->timeout
= timeout
;
1016 /* insert request and run queue */
1017 blk_execute_rq(rq
->q
, NULL
, rq
, true);
1019 if (int_cmd
->status
) {
1020 dev_err(&dd
->pdev
->dev
, "Internal command [%02X] failed %d\n",
1021 fis
->command
, int_cmd
->status
);
1024 if (mtip_check_surprise_removal(dd
->pdev
) ||
1025 test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
1027 dev_err(&dd
->pdev
->dev
,
1028 "Internal command [%02X] wait returned due to SR\n",
1033 mtip_device_reset(dd
); /* recover from timeout issue */
1038 if (readl(port
->cmd_issue
[MTIP_TAG_INDEX(MTIP_TAG_INTERNAL
)])
1039 & (1 << MTIP_TAG_BIT(MTIP_TAG_INTERNAL
))) {
1041 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)) {
1042 mtip_device_reset(dd
);
1047 /* Clear the allocated and active bits for the internal command. */
1048 blk_mq_free_request(rq
);
1049 clear_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1050 if (rv
>= 0 && mtip_pause_ncq(port
, fis
)) {
1054 wake_up_interruptible(&port
->svc_wait
);
1060 * Byte-swap ATA ID strings.
1062 * ATA identify data contains strings in byte-swapped 16-bit words.
1063 * They must be swapped (on all architectures) to be usable as C strings.
1064 * This function swaps bytes in-place.
1066 * @buf The buffer location of the string
1067 * @len The number of bytes to swap
1072 static inline void ata_swap_string(u16
*buf
, unsigned int len
)
1075 for (i
= 0; i
< (len
/2); i
++)
1076 be16_to_cpus(&buf
[i
]);
1079 static void mtip_set_timeout(struct driver_data
*dd
,
1080 struct host_to_dev_fis
*fis
,
1081 unsigned int *timeout
, u8 erasemode
)
1083 switch (fis
->command
) {
1084 case ATA_CMD_DOWNLOAD_MICRO
:
1085 *timeout
= 120000; /* 2 minutes */
1087 case ATA_CMD_SEC_ERASE_UNIT
:
1090 *timeout
= ((*(dd
->port
->identify
+ 90) * 2) * 60000);
1092 *timeout
= ((*(dd
->port
->identify
+ 89) * 2) * 60000);
1094 case ATA_CMD_STANDBYNOW1
:
1095 *timeout
= 120000; /* 2 minutes */
1099 *timeout
= 60000; /* 60 seconds */
1102 *timeout
= 15000; /* 15 seconds */
1105 *timeout
= MTIP_IOCTL_CMD_TIMEOUT_MS
;
1111 * Request the device identity information.
1113 * If a user space buffer is not specified, i.e. is NULL, the
1114 * identify information is still read from the drive and placed
1115 * into the identify data buffer (@e port->identify) in the
1116 * port data structure.
1117 * When the identify buffer contains valid identify information @e
1118 * port->identify_valid is non-zero.
1120 * @port Pointer to the port structure.
1121 * @user_buffer A user space buffer where the identify data should be
1125 * 0 Command completed successfully.
1126 * -EFAULT An error occurred while coping data to the user buffer.
1127 * -1 Command failed.
1129 static int mtip_get_identify(struct mtip_port
*port
, void __user
*user_buffer
)
1132 struct host_to_dev_fis fis
;
1134 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
1137 /* Build the FIS. */
1138 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1141 fis
.command
= ATA_CMD_ID_ATA
;
1143 /* Set the identify information as invalid. */
1144 port
->identify_valid
= 0;
1146 /* Clear the identify information. */
1147 memset(port
->identify
, 0, sizeof(u16
) * ATA_ID_WORDS
);
1149 /* Execute the command. */
1150 if (mtip_exec_internal_command(port
,
1154 sizeof(u16
) * ATA_ID_WORDS
,
1156 MTIP_INT_CMD_TIMEOUT_MS
)
1163 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1164 * perform field-sensitive swapping on the string fields.
1165 * See the kernel use of ata_id_string() for proof of this.
1167 #ifdef __LITTLE_ENDIAN
1168 ata_swap_string(port
->identify
+ 27, 40); /* model string*/
1169 ata_swap_string(port
->identify
+ 23, 8); /* firmware string*/
1170 ata_swap_string(port
->identify
+ 10, 20); /* serial# string*/
1174 for (i
= 0; i
< ATA_ID_WORDS
; i
++)
1175 port
->identify
[i
] = le16_to_cpu(port
->identify
[i
]);
1179 /* Check security locked state */
1180 if (port
->identify
[128] & 0x4)
1181 set_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
1183 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
1185 /* Set the identify buffer as valid. */
1186 port
->identify_valid
= 1;
1192 ATA_ID_WORDS
* sizeof(u16
))) {
1203 * Issue a standby immediate command to the device.
1205 * @port Pointer to the port structure.
1208 * 0 Command was executed successfully.
1209 * -1 An error occurred while executing the command.
1211 static int mtip_standby_immediate(struct mtip_port
*port
)
1214 struct host_to_dev_fis fis
;
1215 unsigned long start
;
1216 unsigned int timeout
;
1218 /* Build the FIS. */
1219 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1222 fis
.command
= ATA_CMD_STANDBYNOW1
;
1224 mtip_set_timeout(port
->dd
, &fis
, &timeout
, 0);
1227 rv
= mtip_exec_internal_command(port
,
1234 dbg_printk(MTIP_DRV_NAME
"Time taken to complete standby cmd: %d ms\n",
1235 jiffies_to_msecs(jiffies
- start
));
1237 dev_warn(&port
->dd
->pdev
->dev
,
1238 "STANDBY IMMEDIATE command failed.\n");
1244 * Issue a READ LOG EXT command to the device.
1246 * @port pointer to the port structure.
1247 * @page page number to fetch
1248 * @buffer pointer to buffer
1249 * @buffer_dma dma address corresponding to @buffer
1250 * @sectors page length to fetch, in sectors
1253 * @rv return value from mtip_exec_internal_command()
1255 static int mtip_read_log_page(struct mtip_port
*port
, u8 page
, u16
*buffer
,
1256 dma_addr_t buffer_dma
, unsigned int sectors
)
1258 struct host_to_dev_fis fis
;
1260 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1263 fis
.command
= ATA_CMD_READ_LOG_EXT
;
1264 fis
.sect_count
= sectors
& 0xFF;
1265 fis
.sect_cnt_ex
= (sectors
>> 8) & 0xFF;
1268 fis
.device
= ATA_DEVICE_OBS
;
1270 memset(buffer
, 0, sectors
* ATA_SECT_SIZE
);
1272 return mtip_exec_internal_command(port
,
1276 sectors
* ATA_SECT_SIZE
,
1278 MTIP_INT_CMD_TIMEOUT_MS
);
1282 * Issue a SMART READ DATA command to the device.
1284 * @port pointer to the port structure.
1285 * @buffer pointer to buffer
1286 * @buffer_dma dma address corresponding to @buffer
1289 * @rv return value from mtip_exec_internal_command()
1291 static int mtip_get_smart_data(struct mtip_port
*port
, u8
*buffer
,
1292 dma_addr_t buffer_dma
)
1294 struct host_to_dev_fis fis
;
1296 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1299 fis
.command
= ATA_CMD_SMART
;
1300 fis
.features
= 0xD0;
1304 fis
.device
= ATA_DEVICE_OBS
;
1306 return mtip_exec_internal_command(port
,
1316 * Get the value of a smart attribute
1318 * @port pointer to the port structure
1319 * @id attribute number
1320 * @attrib pointer to return attrib information corresponding to @id
1323 * -EINVAL NULL buffer passed or unsupported attribute @id.
1324 * -EPERM Identify data not valid, SMART not supported or not enabled
1326 static int mtip_get_smart_attr(struct mtip_port
*port
, unsigned int id
,
1327 struct smart_attr
*attrib
)
1330 struct smart_attr
*pattr
;
1335 if (!port
->identify_valid
) {
1336 dev_warn(&port
->dd
->pdev
->dev
, "IDENTIFY DATA not valid\n");
1339 if (!(port
->identify
[82] & 0x1)) {
1340 dev_warn(&port
->dd
->pdev
->dev
, "SMART not supported\n");
1343 if (!(port
->identify
[85] & 0x1)) {
1344 dev_warn(&port
->dd
->pdev
->dev
, "SMART not enabled\n");
1348 memset(port
->smart_buf
, 0, ATA_SECT_SIZE
);
1349 rv
= mtip_get_smart_data(port
, port
->smart_buf
, port
->smart_buf_dma
);
1351 dev_warn(&port
->dd
->pdev
->dev
, "Failed to ge SMART data\n");
1355 pattr
= (struct smart_attr
*)(port
->smart_buf
+ 2);
1356 for (i
= 0; i
< 29; i
++, pattr
++)
1357 if (pattr
->attr_id
== id
) {
1358 memcpy(attrib
, pattr
, sizeof(struct smart_attr
));
1363 dev_warn(&port
->dd
->pdev
->dev
,
1364 "Query for invalid SMART attribute ID\n");
1372 * Get the drive capacity.
1374 * @dd Pointer to the device data structure.
1375 * @sectors Pointer to the variable that will receive the sector count.
1378 * 1 Capacity was returned successfully.
1379 * 0 The identify information is invalid.
1381 static bool mtip_hw_get_capacity(struct driver_data
*dd
, sector_t
*sectors
)
1383 struct mtip_port
*port
= dd
->port
;
1384 u64 total
, raw0
, raw1
, raw2
, raw3
;
1385 raw0
= port
->identify
[100];
1386 raw1
= port
->identify
[101];
1387 raw2
= port
->identify
[102];
1388 raw3
= port
->identify
[103];
1389 total
= raw0
| raw1
<<16 | raw2
<<32 | raw3
<<48;
1391 return (bool) !!port
->identify_valid
;
1395 * Display the identify command data.
1397 * @port Pointer to the port data structure.
1402 static void mtip_dump_identify(struct mtip_port
*port
)
1405 unsigned short revid
;
1408 if (!port
->identify_valid
)
1411 strlcpy(cbuf
, (char *)(port
->identify
+10), 21);
1412 dev_info(&port
->dd
->pdev
->dev
,
1413 "Serial No.: %s\n", cbuf
);
1415 strlcpy(cbuf
, (char *)(port
->identify
+23), 9);
1416 dev_info(&port
->dd
->pdev
->dev
,
1417 "Firmware Ver.: %s\n", cbuf
);
1419 strlcpy(cbuf
, (char *)(port
->identify
+27), 41);
1420 dev_info(&port
->dd
->pdev
->dev
, "Model: %s\n", cbuf
);
1422 dev_info(&port
->dd
->pdev
->dev
, "Security: %04x %s\n",
1423 port
->identify
[128],
1424 port
->identify
[128] & 0x4 ? "(LOCKED)" : "");
1426 if (mtip_hw_get_capacity(port
->dd
, §ors
))
1427 dev_info(&port
->dd
->pdev
->dev
,
1428 "Capacity: %llu sectors (%llu MB)\n",
1430 ((u64
)sectors
) * ATA_SECT_SIZE
>> 20);
1432 pci_read_config_word(port
->dd
->pdev
, PCI_REVISION_ID
, &revid
);
1433 switch (revid
& 0xFF) {
1435 strlcpy(cbuf
, "A0", 3);
1438 strlcpy(cbuf
, "A2", 3);
1441 strlcpy(cbuf
, "?", 2);
1444 dev_info(&port
->dd
->pdev
->dev
,
1445 "Card Type: %s\n", cbuf
);
1449 * Map the commands scatter list into the command table.
1451 * @command Pointer to the command.
1452 * @nents Number of scatter list entries.
1457 static inline void fill_command_sg(struct driver_data
*dd
,
1458 struct mtip_cmd
*command
,
1462 unsigned int dma_len
;
1463 struct mtip_cmd_sg
*command_sg
;
1464 struct scatterlist
*sg
;
1466 command_sg
= command
->command
+ AHCI_CMD_TBL_HDR_SZ
;
1468 for_each_sg(command
->sg
, sg
, nents
, n
) {
1469 dma_len
= sg_dma_len(sg
);
1470 if (dma_len
> 0x400000)
1471 dev_err(&dd
->pdev
->dev
,
1472 "DMA segment length truncated\n");
1473 command_sg
->info
= cpu_to_le32((dma_len
-1) & 0x3FFFFF);
1474 command_sg
->dba
= cpu_to_le32(sg_dma_address(sg
));
1475 command_sg
->dba_upper
=
1476 cpu_to_le32((sg_dma_address(sg
) >> 16) >> 16);
1482 * @brief Execute a drive command.
1484 * return value 0 The command completed successfully.
1485 * return value -1 An error occurred while executing the command.
1487 static int exec_drive_task(struct mtip_port
*port
, u8
*command
)
1489 struct host_to_dev_fis fis
;
1490 struct host_to_dev_fis
*reply
= (port
->rxfis
+ RX_FIS_D2H_REG
);
1493 /* Build the FIS. */
1494 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1497 fis
.command
= command
[0];
1498 fis
.features
= command
[1];
1499 fis
.sect_count
= command
[2];
1500 fis
.sector
= command
[3];
1501 fis
.cyl_low
= command
[4];
1502 fis
.cyl_hi
= command
[5];
1503 fis
.device
= command
[6] & ~0x10; /* Clear the dev bit*/
1505 mtip_set_timeout(port
->dd
, &fis
, &to
, 0);
1507 dbg_printk(MTIP_DRV_NAME
" %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
1517 /* Execute the command. */
1518 if (mtip_exec_internal_command(port
,
1528 command
[0] = reply
->command
; /* Status*/
1529 command
[1] = reply
->features
; /* Error*/
1530 command
[4] = reply
->cyl_low
;
1531 command
[5] = reply
->cyl_hi
;
1533 dbg_printk(MTIP_DRV_NAME
" %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
1544 * @brief Execute a drive command.
1546 * @param port Pointer to the port data structure.
1547 * @param command Pointer to the user specified command parameters.
1548 * @param user_buffer Pointer to the user space buffer where read sector
1549 * data should be copied.
1551 * return value 0 The command completed successfully.
1552 * return value -EFAULT An error occurred while copying the completion
1553 * data to the user space buffer.
1554 * return value -1 An error occurred while executing the command.
1556 static int exec_drive_command(struct mtip_port
*port
, u8
*command
,
1557 void __user
*user_buffer
)
1559 struct host_to_dev_fis fis
;
1560 struct host_to_dev_fis
*reply
;
1562 dma_addr_t dma_addr
= 0;
1563 int rv
= 0, xfer_sz
= command
[3];
1570 buf
= dma_alloc_coherent(&port
->dd
->pdev
->dev
,
1571 ATA_SECT_SIZE
* xfer_sz
,
1575 dev_err(&port
->dd
->pdev
->dev
,
1576 "Memory allocation failed (%d bytes)\n",
1577 ATA_SECT_SIZE
* xfer_sz
);
1582 /* Build the FIS. */
1583 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1586 fis
.command
= command
[0];
1587 fis
.features
= command
[2];
1588 fis
.sect_count
= command
[3];
1589 if (fis
.command
== ATA_CMD_SMART
) {
1590 fis
.sector
= command
[1];
1595 mtip_set_timeout(port
->dd
, &fis
, &to
, 0);
1598 reply
= (port
->rxfis
+ RX_FIS_PIO_SETUP
);
1600 reply
= (port
->rxfis
+ RX_FIS_D2H_REG
);
1602 dbg_printk(MTIP_DRV_NAME
1603 " %s: User Command: cmd %x, sect %x, "
1604 "feat %x, sectcnt %x\n",
1611 /* Execute the command. */
1612 if (mtip_exec_internal_command(port
,
1615 (xfer_sz
? dma_addr
: 0),
1616 (xfer_sz
? ATA_SECT_SIZE
* xfer_sz
: 0),
1621 goto exit_drive_command
;
1624 /* Collect the completion status. */
1625 command
[0] = reply
->command
; /* Status*/
1626 command
[1] = reply
->features
; /* Error*/
1627 command
[2] = reply
->sect_count
;
1629 dbg_printk(MTIP_DRV_NAME
1630 " %s: Completion Status: stat %x, "
1631 "err %x, nsect %x\n",
1638 if (copy_to_user(user_buffer
,
1640 ATA_SECT_SIZE
* command
[3])) {
1642 goto exit_drive_command
;
1647 dma_free_coherent(&port
->dd
->pdev
->dev
,
1648 ATA_SECT_SIZE
* xfer_sz
, buf
, dma_addr
);
1653 * Indicates whether a command has a single sector payload.
1655 * @command passed to the device to perform the certain event.
1656 * @features passed to the device to perform the certain event.
1659 * 1 command is one that always has a single sector payload,
1660 * regardless of the value in the Sector Count field.
1664 static unsigned int implicit_sector(unsigned char command
,
1665 unsigned char features
)
1667 unsigned int rv
= 0;
1669 /* list of commands that have an implicit sector count of 1 */
1671 case ATA_CMD_SEC_SET_PASS
:
1672 case ATA_CMD_SEC_UNLOCK
:
1673 case ATA_CMD_SEC_ERASE_PREP
:
1674 case ATA_CMD_SEC_ERASE_UNIT
:
1675 case ATA_CMD_SEC_FREEZE_LOCK
:
1676 case ATA_CMD_SEC_DISABLE_PASS
:
1677 case ATA_CMD_PMP_READ
:
1678 case ATA_CMD_PMP_WRITE
:
1681 case ATA_CMD_SET_MAX
:
1682 if (features
== ATA_SET_MAX_UNLOCK
)
1686 if ((features
== ATA_SMART_READ_VALUES
) ||
1687 (features
== ATA_SMART_READ_THRESHOLDS
))
1690 case ATA_CMD_CONF_OVERLAY
:
1691 if ((features
== ATA_DCO_IDENTIFY
) ||
1692 (features
== ATA_DCO_SET
))
1700 * Executes a taskfile
1701 * See ide_taskfile_ioctl() for derivation
1703 static int exec_drive_taskfile(struct driver_data
*dd
,
1705 ide_task_request_t
*req_task
,
1708 struct host_to_dev_fis fis
;
1709 struct host_to_dev_fis
*reply
;
1712 dma_addr_t outbuf_dma
= 0;
1713 dma_addr_t inbuf_dma
= 0;
1714 dma_addr_t dma_buffer
= 0;
1716 unsigned int taskin
= 0;
1717 unsigned int taskout
= 0;
1719 unsigned int timeout
;
1720 unsigned int force_single_sector
;
1721 unsigned int transfer_size
;
1722 unsigned long task_file_data
;
1723 int intotal
= outtotal
+ req_task
->out_size
;
1726 taskout
= req_task
->out_size
;
1727 taskin
= req_task
->in_size
;
1728 /* 130560 = 512 * 0xFF*/
1729 if (taskin
> 130560 || taskout
> 130560)
1733 outbuf
= memdup_user(buf
+ outtotal
, taskout
);
1735 return PTR_ERR(outbuf
);
1737 outbuf_dma
= dma_map_single(&dd
->pdev
->dev
, outbuf
,
1738 taskout
, DMA_TO_DEVICE
);
1739 if (dma_mapping_error(&dd
->pdev
->dev
, outbuf_dma
)) {
1743 dma_buffer
= outbuf_dma
;
1747 inbuf
= memdup_user(buf
+ intotal
, taskin
);
1748 if (IS_ERR(inbuf
)) {
1749 err
= PTR_ERR(inbuf
);
1753 inbuf_dma
= dma_map_single(&dd
->pdev
->dev
, inbuf
,
1754 taskin
, DMA_FROM_DEVICE
);
1755 if (dma_mapping_error(&dd
->pdev
->dev
, inbuf_dma
)) {
1759 dma_buffer
= inbuf_dma
;
1762 /* only supports PIO and non-data commands from this ioctl. */
1763 switch (req_task
->data_phase
) {
1765 nsect
= taskout
/ ATA_SECT_SIZE
;
1766 reply
= (dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
);
1769 reply
= (dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
);
1771 case TASKFILE_NO_DATA
:
1772 reply
= (dd
->port
->rxfis
+ RX_FIS_D2H_REG
);
1779 /* Build the FIS. */
1780 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1784 fis
.command
= req_task
->io_ports
[7];
1785 fis
.features
= req_task
->io_ports
[1];
1786 fis
.sect_count
= req_task
->io_ports
[2];
1787 fis
.lba_low
= req_task
->io_ports
[3];
1788 fis
.lba_mid
= req_task
->io_ports
[4];
1789 fis
.lba_hi
= req_task
->io_ports
[5];
1790 /* Clear the dev bit*/
1791 fis
.device
= req_task
->io_ports
[6] & ~0x10;
1793 if ((req_task
->in_flags
.all
== 0) && (req_task
->out_flags
.all
& 1)) {
1794 req_task
->in_flags
.all
=
1795 IDE_TASKFILE_STD_IN_FLAGS
|
1796 (IDE_HOB_STD_IN_FLAGS
<< 8);
1797 fis
.lba_low_ex
= req_task
->hob_ports
[3];
1798 fis
.lba_mid_ex
= req_task
->hob_ports
[4];
1799 fis
.lba_hi_ex
= req_task
->hob_ports
[5];
1800 fis
.features_ex
= req_task
->hob_ports
[1];
1801 fis
.sect_cnt_ex
= req_task
->hob_ports
[2];
1804 req_task
->in_flags
.all
= IDE_TASKFILE_STD_IN_FLAGS
;
1807 force_single_sector
= implicit_sector(fis
.command
, fis
.features
);
1809 if ((taskin
|| taskout
) && (!fis
.sect_count
)) {
1811 fis
.sect_count
= nsect
;
1813 if (!force_single_sector
) {
1814 dev_warn(&dd
->pdev
->dev
,
1815 "data movement but "
1816 "sect_count is 0\n");
1823 dbg_printk(MTIP_DRV_NAME
1824 " %s: cmd %x, feat %x, nsect %x,"
1825 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
1836 /* check for erase mode support during secure erase.*/
1837 if ((fis
.command
== ATA_CMD_SEC_ERASE_UNIT
) && outbuf
&&
1838 (outbuf
[0] & MTIP_SEC_ERASE_MODE
)) {
1842 mtip_set_timeout(dd
, &fis
, &timeout
, erasemode
);
1844 /* Determine the correct transfer size.*/
1845 if (force_single_sector
)
1846 transfer_size
= ATA_SECT_SIZE
;
1848 transfer_size
= ATA_SECT_SIZE
* fis
.sect_count
;
1850 /* Execute the command.*/
1851 if (mtip_exec_internal_command(dd
->port
,
1862 task_file_data
= readl(dd
->port
->mmio
+PORT_TFDATA
);
1864 if ((req_task
->data_phase
== TASKFILE_IN
) && !(task_file_data
& 1)) {
1865 reply
= dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
;
1866 req_task
->io_ports
[7] = reply
->control
;
1868 reply
= dd
->port
->rxfis
+ RX_FIS_D2H_REG
;
1869 req_task
->io_ports
[7] = reply
->command
;
1872 /* reclaim the DMA buffers.*/
1874 dma_unmap_single(&dd
->pdev
->dev
, inbuf_dma
, taskin
,
1877 dma_unmap_single(&dd
->pdev
->dev
, outbuf_dma
, taskout
,
1882 /* return the ATA registers to the caller.*/
1883 req_task
->io_ports
[1] = reply
->features
;
1884 req_task
->io_ports
[2] = reply
->sect_count
;
1885 req_task
->io_ports
[3] = reply
->lba_low
;
1886 req_task
->io_ports
[4] = reply
->lba_mid
;
1887 req_task
->io_ports
[5] = reply
->lba_hi
;
1888 req_task
->io_ports
[6] = reply
->device
;
1890 if (req_task
->out_flags
.all
& 1) {
1892 req_task
->hob_ports
[3] = reply
->lba_low_ex
;
1893 req_task
->hob_ports
[4] = reply
->lba_mid_ex
;
1894 req_task
->hob_ports
[5] = reply
->lba_hi_ex
;
1895 req_task
->hob_ports
[1] = reply
->features_ex
;
1896 req_task
->hob_ports
[2] = reply
->sect_cnt_ex
;
1898 dbg_printk(MTIP_DRV_NAME
1899 " %s: Completion: stat %x,"
1900 "err %x, sect_cnt %x, lbalo %x,"
1901 "lbamid %x, lbahi %x, dev %x\n",
1903 req_task
->io_ports
[7],
1904 req_task
->io_ports
[1],
1905 req_task
->io_ports
[2],
1906 req_task
->io_ports
[3],
1907 req_task
->io_ports
[4],
1908 req_task
->io_ports
[5],
1909 req_task
->io_ports
[6]);
1912 if (copy_to_user(buf
+ outtotal
, outbuf
, taskout
)) {
1918 if (copy_to_user(buf
+ intotal
, inbuf
, taskin
)) {
1925 dma_unmap_single(&dd
->pdev
->dev
, inbuf_dma
, taskin
,
1928 dma_unmap_single(&dd
->pdev
->dev
, outbuf_dma
, taskout
,
1937 * Handle IOCTL calls from the Block Layer.
1939 * This function is called by the Block Layer when it receives an IOCTL
1940 * command that it does not understand. If the IOCTL command is not supported
1941 * this function returns -ENOTTY.
1943 * @dd Pointer to the driver data structure.
1944 * @cmd IOCTL command passed from the Block Layer.
1945 * @arg IOCTL argument passed from the Block Layer.
1948 * 0 The IOCTL completed successfully.
1949 * -ENOTTY The specified command is not supported.
1950 * -EFAULT An error occurred copying data to a user space buffer.
1951 * -EIO An error occurred while executing the command.
1953 static int mtip_hw_ioctl(struct driver_data
*dd
, unsigned int cmd
,
1957 case HDIO_GET_IDENTITY
:
1959 if (copy_to_user((void __user
*)arg
, dd
->port
->identify
,
1960 sizeof(u16
) * ATA_ID_WORDS
))
1964 case HDIO_DRIVE_CMD
:
1966 u8 drive_command
[4];
1968 /* Copy the user command info to our buffer. */
1969 if (copy_from_user(drive_command
,
1970 (void __user
*) arg
,
1971 sizeof(drive_command
)))
1974 /* Execute the drive command. */
1975 if (exec_drive_command(dd
->port
,
1977 (void __user
*) (arg
+4)))
1980 /* Copy the status back to the users buffer. */
1981 if (copy_to_user((void __user
*) arg
,
1983 sizeof(drive_command
)))
1988 case HDIO_DRIVE_TASK
:
1990 u8 drive_command
[7];
1992 /* Copy the user command info to our buffer. */
1993 if (copy_from_user(drive_command
,
1994 (void __user
*) arg
,
1995 sizeof(drive_command
)))
1998 /* Execute the drive command. */
1999 if (exec_drive_task(dd
->port
, drive_command
))
2002 /* Copy the status back to the users buffer. */
2003 if (copy_to_user((void __user
*) arg
,
2005 sizeof(drive_command
)))
2010 case HDIO_DRIVE_TASKFILE
: {
2011 ide_task_request_t req_task
;
2014 if (copy_from_user(&req_task
, (void __user
*) arg
,
2018 outtotal
= sizeof(req_task
);
2020 ret
= exec_drive_taskfile(dd
, (void __user
*) arg
,
2021 &req_task
, outtotal
);
2023 if (copy_to_user((void __user
*) arg
, &req_task
,
2037 * Submit an IO to the hw
2039 * This function is called by the block layer to issue an io
2040 * to the device. Upon completion, the callback function will
2041 * be called with the data parameter passed as the callback data.
2043 * @dd Pointer to the driver data structure.
2044 * @start First sector to read.
2045 * @nsect Number of sectors to read.
2046 * @tag The tag of this read command.
2047 * @callback Pointer to the function that should be called
2048 * when the read completes.
2049 * @data Callback data passed to the callback function
2050 * when the read completes.
2051 * @dir Direction (read or write)
2056 static void mtip_hw_submit_io(struct driver_data
*dd
, struct request
*rq
,
2057 struct mtip_cmd
*command
,
2058 struct blk_mq_hw_ctx
*hctx
)
2060 struct mtip_cmd_hdr
*hdr
=
2061 dd
->port
->command_list
+ sizeof(struct mtip_cmd_hdr
) * rq
->tag
;
2062 struct host_to_dev_fis
*fis
;
2063 struct mtip_port
*port
= dd
->port
;
2064 int dma_dir
= rq_data_dir(rq
) == READ
? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
2065 u64 start
= blk_rq_pos(rq
);
2066 unsigned int nsect
= blk_rq_sectors(rq
);
2069 /* Map the scatter list for DMA access */
2070 nents
= blk_rq_map_sg(hctx
->queue
, rq
, command
->sg
);
2071 nents
= dma_map_sg(&dd
->pdev
->dev
, command
->sg
, nents
, dma_dir
);
2073 prefetch(&port
->flags
);
2075 command
->scatter_ents
= nents
;
2078 * The number of retries for this command before it is
2079 * reported as a failure to the upper layers.
2081 command
->retries
= MTIP_MAX_RETRIES
;
2084 fis
= command
->command
;
2087 if (dma_dir
== DMA_FROM_DEVICE
)
2088 fis
->command
= ATA_CMD_FPDMA_READ
;
2090 fis
->command
= ATA_CMD_FPDMA_WRITE
;
2091 fis
->lba_low
= start
& 0xFF;
2092 fis
->lba_mid
= (start
>> 8) & 0xFF;
2093 fis
->lba_hi
= (start
>> 16) & 0xFF;
2094 fis
->lba_low_ex
= (start
>> 24) & 0xFF;
2095 fis
->lba_mid_ex
= (start
>> 32) & 0xFF;
2096 fis
->lba_hi_ex
= (start
>> 40) & 0xFF;
2097 fis
->device
= 1 << 6;
2098 fis
->features
= nsect
& 0xFF;
2099 fis
->features_ex
= (nsect
>> 8) & 0xFF;
2100 fis
->sect_count
= ((rq
->tag
<< 3) | (rq
->tag
>> 5));
2101 fis
->sect_cnt_ex
= 0;
2105 fill_command_sg(dd
, command
, nents
);
2107 if (unlikely(command
->unaligned
))
2108 fis
->device
|= 1 << 7;
2110 /* Populate the command header */
2111 hdr
->ctba
= cpu_to_le32(command
->command_dma
& 0xFFFFFFFF);
2112 if (test_bit(MTIP_PF_HOST_CAP_64
, &dd
->port
->flags
))
2113 hdr
->ctbau
= cpu_to_le32((command
->command_dma
>> 16) >> 16);
2114 hdr
->opts
= cpu_to_le32((nents
<< 16) | 5 | AHCI_CMD_PREFETCH
);
2115 hdr
->byte_count
= 0;
2117 command
->direction
= dma_dir
;
2120 * To prevent this command from being issued
2121 * if an internal command is in progress or error handling is active.
2123 if (unlikely(port
->flags
& MTIP_PF_PAUSE_IO
)) {
2124 set_bit(rq
->tag
, port
->cmds_to_issue
);
2125 set_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
);
2129 /* Issue the command to the hardware */
2130 mtip_issue_ncq_command(port
, rq
->tag
);
2134 * Sysfs status dump.
2136 * @dev Pointer to the device structure, passed by the kernrel.
2137 * @attr Pointer to the device_attribute structure passed by the kernel.
2138 * @buf Pointer to the char buffer that will receive the stats info.
2141 * The size, in bytes, of the data copied into buf.
2143 static ssize_t
mtip_hw_show_status(struct device
*dev
,
2144 struct device_attribute
*attr
,
2147 struct driver_data
*dd
= dev_to_disk(dev
)->private_data
;
2150 if (test_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
))
2151 size
+= sprintf(buf
, "%s", "thermal_shutdown\n");
2152 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
))
2153 size
+= sprintf(buf
, "%s", "write_protect\n");
2155 size
+= sprintf(buf
, "%s", "online\n");
2160 static DEVICE_ATTR(status
, 0444, mtip_hw_show_status
, NULL
);
2162 /* debugsfs entries */
2164 static ssize_t
show_device_status(struct device_driver
*drv
, char *buf
)
2167 struct driver_data
*dd
, *tmp
;
2168 unsigned long flags
;
2172 spin_lock_irqsave(&dev_lock
, flags
);
2173 size
+= sprintf(&buf
[size
], "Devices Present:\n");
2174 list_for_each_entry_safe(dd
, tmp
, &online_list
, online_list
) {
2177 dd
->port
->identify
&&
2178 dd
->port
->identify_valid
) {
2180 (char *) (dd
->port
->identify
+ 10), 21);
2181 status
= *(dd
->port
->identify
+ 141);
2183 memset(id_buf
, 0, 42);
2188 test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
)) {
2189 size
+= sprintf(&buf
[size
],
2190 " device %s %s (ftl rebuild %d %%)\n",
2191 dev_name(&dd
->pdev
->dev
),
2195 size
+= sprintf(&buf
[size
],
2197 dev_name(&dd
->pdev
->dev
),
2203 size
+= sprintf(&buf
[size
], "Devices Being Removed:\n");
2204 list_for_each_entry_safe(dd
, tmp
, &removing_list
, remove_list
) {
2207 dd
->port
->identify
&&
2208 dd
->port
->identify_valid
) {
2210 (char *) (dd
->port
->identify
+10), 21);
2211 status
= *(dd
->port
->identify
+ 141);
2213 memset(id_buf
, 0, 42);
2218 test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
)) {
2219 size
+= sprintf(&buf
[size
],
2220 " device %s %s (ftl rebuild %d %%)\n",
2221 dev_name(&dd
->pdev
->dev
),
2225 size
+= sprintf(&buf
[size
],
2227 dev_name(&dd
->pdev
->dev
),
2232 spin_unlock_irqrestore(&dev_lock
, flags
);
2237 static ssize_t
mtip_hw_read_device_status(struct file
*f
, char __user
*ubuf
,
2238 size_t len
, loff_t
*offset
)
2240 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2245 if (!len
|| *offset
)
2248 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2250 dev_err(&dd
->pdev
->dev
,
2251 "Memory allocation: status buffer\n");
2255 size
+= show_device_status(NULL
, buf
);
2257 *offset
= size
<= len
? size
: len
;
2258 size
= copy_to_user(ubuf
, buf
, *offset
);
2263 return rv
? rv
: *offset
;
2266 static ssize_t
mtip_hw_read_registers(struct file
*f
, char __user
*ubuf
,
2267 size_t len
, loff_t
*offset
)
2269 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2271 u32 group_allocated
;
2278 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2280 dev_err(&dd
->pdev
->dev
,
2281 "Memory allocation: register buffer\n");
2285 size
+= sprintf(&buf
[size
], "H/ S ACTive : [ 0x");
2287 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2288 size
+= sprintf(&buf
[size
], "%08X ",
2289 readl(dd
->port
->s_active
[n
]));
2291 size
+= sprintf(&buf
[size
], "]\n");
2292 size
+= sprintf(&buf
[size
], "H/ Command Issue : [ 0x");
2294 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2295 size
+= sprintf(&buf
[size
], "%08X ",
2296 readl(dd
->port
->cmd_issue
[n
]));
2298 size
+= sprintf(&buf
[size
], "]\n");
2299 size
+= sprintf(&buf
[size
], "H/ Completed : [ 0x");
2301 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2302 size
+= sprintf(&buf
[size
], "%08X ",
2303 readl(dd
->port
->completed
[n
]));
2305 size
+= sprintf(&buf
[size
], "]\n");
2306 size
+= sprintf(&buf
[size
], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2307 readl(dd
->port
->mmio
+ PORT_IRQ_STAT
));
2308 size
+= sprintf(&buf
[size
], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2309 readl(dd
->mmio
+ HOST_IRQ_STAT
));
2310 size
+= sprintf(&buf
[size
], "\n");
2312 size
+= sprintf(&buf
[size
], "L/ Commands in Q : [ 0x");
2314 for (n
= dd
->slot_groups
-1; n
>= 0; n
--) {
2315 if (sizeof(long) > sizeof(u32
))
2317 dd
->port
->cmds_to_issue
[n
/2] >> (32*(n
&1));
2319 group_allocated
= dd
->port
->cmds_to_issue
[n
];
2320 size
+= sprintf(&buf
[size
], "%08X ", group_allocated
);
2322 size
+= sprintf(&buf
[size
], "]\n");
2324 *offset
= size
<= len
? size
: len
;
2325 size
= copy_to_user(ubuf
, buf
, *offset
);
2330 return rv
? rv
: *offset
;
2333 static ssize_t
mtip_hw_read_flags(struct file
*f
, char __user
*ubuf
,
2334 size_t len
, loff_t
*offset
)
2336 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2344 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2346 dev_err(&dd
->pdev
->dev
,
2347 "Memory allocation: flag buffer\n");
2351 size
+= sprintf(&buf
[size
], "Flag-port : [ %08lX ]\n",
2353 size
+= sprintf(&buf
[size
], "Flag-dd : [ %08lX ]\n",
2356 *offset
= size
<= len
? size
: len
;
2357 size
= copy_to_user(ubuf
, buf
, *offset
);
2362 return rv
? rv
: *offset
;
2365 static const struct file_operations mtip_device_status_fops
= {
2366 .owner
= THIS_MODULE
,
2367 .open
= simple_open
,
2368 .read
= mtip_hw_read_device_status
,
2369 .llseek
= no_llseek
,
2372 static const struct file_operations mtip_regs_fops
= {
2373 .owner
= THIS_MODULE
,
2374 .open
= simple_open
,
2375 .read
= mtip_hw_read_registers
,
2376 .llseek
= no_llseek
,
2379 static const struct file_operations mtip_flags_fops
= {
2380 .owner
= THIS_MODULE
,
2381 .open
= simple_open
,
2382 .read
= mtip_hw_read_flags
,
2383 .llseek
= no_llseek
,
2387 * Create the sysfs related attributes.
2389 * @dd Pointer to the driver data structure.
2390 * @kobj Pointer to the kobj for the block device.
2393 * 0 Operation completed successfully.
2394 * -EINVAL Invalid parameter.
2396 static int mtip_hw_sysfs_init(struct driver_data
*dd
, struct kobject
*kobj
)
2401 if (sysfs_create_file(kobj
, &dev_attr_status
.attr
))
2402 dev_warn(&dd
->pdev
->dev
,
2403 "Error creating 'status' sysfs entry\n");
2408 * Remove the sysfs related attributes.
2410 * @dd Pointer to the driver data structure.
2411 * @kobj Pointer to the kobj for the block device.
2414 * 0 Operation completed successfully.
2415 * -EINVAL Invalid parameter.
2417 static int mtip_hw_sysfs_exit(struct driver_data
*dd
, struct kobject
*kobj
)
2422 sysfs_remove_file(kobj
, &dev_attr_status
.attr
);
2427 static int mtip_hw_debugfs_init(struct driver_data
*dd
)
2432 dd
->dfs_node
= debugfs_create_dir(dd
->disk
->disk_name
, dfs_parent
);
2433 if (IS_ERR_OR_NULL(dd
->dfs_node
)) {
2434 dev_warn(&dd
->pdev
->dev
,
2435 "Error creating node %s under debugfs\n",
2436 dd
->disk
->disk_name
);
2437 dd
->dfs_node
= NULL
;
2441 debugfs_create_file("flags", 0444, dd
->dfs_node
, dd
, &mtip_flags_fops
);
2442 debugfs_create_file("registers", 0444, dd
->dfs_node
, dd
,
2448 static void mtip_hw_debugfs_exit(struct driver_data
*dd
)
2450 debugfs_remove_recursive(dd
->dfs_node
);
2454 * Perform any init/resume time hardware setup
2456 * @dd Pointer to the driver data structure.
2461 static inline void hba_setup(struct driver_data
*dd
)
2464 hwdata
= readl(dd
->mmio
+ HOST_HSORG
);
2466 /* interrupt bug workaround: use only 1 IS bit.*/
2468 HSORG_DISABLE_SLOTGRP_INTR
|
2469 HSORG_DISABLE_SLOTGRP_PXIS
,
2470 dd
->mmio
+ HOST_HSORG
);
2473 static int mtip_device_unaligned_constrained(struct driver_data
*dd
)
2475 return (dd
->pdev
->device
== P420M_DEVICE_ID
? 1 : 0);
2479 * Detect the details of the product, and store anything needed
2480 * into the driver data structure. This includes product type and
2481 * version and number of slot groups.
2483 * @dd Pointer to the driver data structure.
2488 static void mtip_detect_product(struct driver_data
*dd
)
2491 unsigned int rev
, slotgroups
;
2494 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2496 * [15:8] hardware/software interface rev#
2497 * [ 3] asic-style interface
2498 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2500 hwdata
= readl(dd
->mmio
+ HOST_HSORG
);
2502 dd
->product_type
= MTIP_PRODUCT_UNKNOWN
;
2503 dd
->slot_groups
= 1;
2506 dd
->product_type
= MTIP_PRODUCT_ASICFPGA
;
2507 rev
= (hwdata
& HSORG_HWREV
) >> 8;
2508 slotgroups
= (hwdata
& HSORG_SLOTGROUPS
) + 1;
2509 dev_info(&dd
->pdev
->dev
,
2510 "ASIC-FPGA design, HS rev 0x%x, "
2511 "%i slot groups [%i slots]\n",
2516 if (slotgroups
> MTIP_MAX_SLOT_GROUPS
) {
2517 dev_warn(&dd
->pdev
->dev
,
2518 "Warning: driver only supports "
2519 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS
);
2520 slotgroups
= MTIP_MAX_SLOT_GROUPS
;
2522 dd
->slot_groups
= slotgroups
;
2526 dev_warn(&dd
->pdev
->dev
, "Unrecognized product id\n");
2530 * Blocking wait for FTL rebuild to complete
2532 * @dd Pointer to the DRIVER_DATA structure.
2535 * 0 FTL rebuild completed successfully
2536 * -EFAULT FTL rebuild error/timeout/interruption
2538 static int mtip_ftl_rebuild_poll(struct driver_data
*dd
)
2540 unsigned long timeout
, cnt
= 0, start
;
2542 dev_warn(&dd
->pdev
->dev
,
2543 "FTL rebuild in progress. Polling for completion.\n");
2546 timeout
= jiffies
+ msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS
);
2549 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
2552 if (mtip_check_surprise_removal(dd
->pdev
))
2555 if (mtip_get_identify(dd
->port
, NULL
) < 0)
2558 if (*(dd
->port
->identify
+ MTIP_FTL_REBUILD_OFFSET
) ==
2559 MTIP_FTL_REBUILD_MAGIC
) {
2561 /* Print message every 3 minutes */
2563 dev_warn(&dd
->pdev
->dev
,
2564 "FTL rebuild in progress (%d secs).\n",
2565 jiffies_to_msecs(jiffies
- start
) / 1000);
2569 dev_warn(&dd
->pdev
->dev
,
2570 "FTL rebuild complete (%d secs).\n",
2571 jiffies_to_msecs(jiffies
- start
) / 1000);
2572 mtip_block_initialize(dd
);
2575 } while (time_before(jiffies
, timeout
));
2577 /* Check for timeout */
2578 dev_err(&dd
->pdev
->dev
,
2579 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2580 jiffies_to_msecs(jiffies
- start
) / 1000);
2584 static void mtip_softirq_done_fn(struct request
*rq
)
2586 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
2587 struct driver_data
*dd
= rq
->q
->queuedata
;
2589 /* Unmap the DMA scatter list entries */
2590 dma_unmap_sg(&dd
->pdev
->dev
, cmd
->sg
, cmd
->scatter_ents
,
2593 if (unlikely(cmd
->unaligned
))
2594 atomic_inc(&dd
->port
->cmd_slot_unal
);
2596 blk_mq_end_request(rq
, cmd
->status
);
2599 static bool mtip_abort_cmd(struct request
*req
, void *data
, bool reserved
)
2601 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
2602 struct driver_data
*dd
= data
;
2604 dbg_printk(MTIP_DRV_NAME
" Aborting request, tag = %d\n", req
->tag
);
2606 clear_bit(req
->tag
, dd
->port
->cmds_to_issue
);
2607 cmd
->status
= BLK_STS_IOERR
;
2608 mtip_softirq_done_fn(req
);
2612 static bool mtip_queue_cmd(struct request
*req
, void *data
, bool reserved
)
2614 struct driver_data
*dd
= data
;
2616 set_bit(req
->tag
, dd
->port
->cmds_to_issue
);
2617 blk_abort_request(req
);
2622 * service thread to issue queued commands
2624 * @data Pointer to the driver data structure.
2630 static int mtip_service_thread(void *data
)
2632 struct driver_data
*dd
= (struct driver_data
*)data
;
2633 unsigned long slot
, slot_start
, slot_wrap
, to
;
2634 unsigned int num_cmd_slots
= dd
->slot_groups
* 32;
2635 struct mtip_port
*port
= dd
->port
;
2638 if (kthread_should_stop() ||
2639 test_bit(MTIP_PF_SVC_THD_STOP_BIT
, &port
->flags
))
2641 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
);
2644 * the condition is to check neither an internal command is
2645 * is in progress nor error handling is active
2647 wait_event_interruptible(port
->svc_wait
, (port
->flags
) &&
2648 (port
->flags
& MTIP_PF_SVC_THD_WORK
));
2650 if (kthread_should_stop() ||
2651 test_bit(MTIP_PF_SVC_THD_STOP_BIT
, &port
->flags
))
2654 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
2658 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
);
2661 /* Demux bits: start with error handling */
2662 if (test_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
)) {
2663 mtip_handle_tfe(dd
);
2664 clear_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
);
2667 if (test_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
))
2670 if (test_bit(MTIP_PF_TO_ACTIVE_BIT
, &port
->flags
)) {
2671 to
= jiffies
+ msecs_to_jiffies(5000);
2675 } while (atomic_read(&dd
->irq_workers_active
) != 0 &&
2676 time_before(jiffies
, to
));
2678 if (atomic_read(&dd
->irq_workers_active
) != 0)
2679 dev_warn(&dd
->pdev
->dev
,
2680 "Completion workers still active!");
2682 blk_mq_quiesce_queue(dd
->queue
);
2684 blk_mq_tagset_busy_iter(&dd
->tags
, mtip_queue_cmd
, dd
);
2686 set_bit(MTIP_PF_ISSUE_CMDS_BIT
, &dd
->port
->flags
);
2688 if (mtip_device_reset(dd
))
2689 blk_mq_tagset_busy_iter(&dd
->tags
,
2690 mtip_abort_cmd
, dd
);
2692 clear_bit(MTIP_PF_TO_ACTIVE_BIT
, &dd
->port
->flags
);
2694 blk_mq_unquiesce_queue(dd
->queue
);
2697 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
)) {
2699 /* used to restrict the loop to one iteration */
2700 slot_start
= num_cmd_slots
;
2703 slot
= find_next_bit(port
->cmds_to_issue
,
2704 num_cmd_slots
, slot
);
2705 if (slot_wrap
== 1) {
2706 if ((slot_start
>= slot
) ||
2707 (slot
>= num_cmd_slots
))
2710 if (unlikely(slot_start
== num_cmd_slots
))
2713 if (unlikely(slot
== num_cmd_slots
)) {
2719 /* Issue the command to the hardware */
2720 mtip_issue_ncq_command(port
, slot
);
2722 clear_bit(slot
, port
->cmds_to_issue
);
2725 clear_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
);
2728 if (test_bit(MTIP_PF_REBUILD_BIT
, &port
->flags
)) {
2729 if (mtip_ftl_rebuild_poll(dd
) == 0)
2730 clear_bit(MTIP_PF_REBUILD_BIT
, &port
->flags
);
2739 * DMA region teardown
2741 * @dd Pointer to driver_data structure
2746 static void mtip_dma_free(struct driver_data
*dd
)
2748 struct mtip_port
*port
= dd
->port
;
2751 dma_free_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
2752 port
->block1
, port
->block1_dma
);
2754 if (port
->command_list
) {
2755 dma_free_coherent(&dd
->pdev
->dev
, AHCI_CMD_TBL_SZ
,
2756 port
->command_list
, port
->command_list_dma
);
2763 * @dd Pointer to driver_data structure
2766 * -ENOMEM Not enough free DMA region space to initialize driver
2768 static int mtip_dma_alloc(struct driver_data
*dd
)
2770 struct mtip_port
*port
= dd
->port
;
2772 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
2774 dma_alloc_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
2775 &port
->block1_dma
, GFP_KERNEL
);
2779 /* Allocate dma memory for command list */
2780 port
->command_list
=
2781 dma_alloc_coherent(&dd
->pdev
->dev
, AHCI_CMD_TBL_SZ
,
2782 &port
->command_list_dma
, GFP_KERNEL
);
2783 if (!port
->command_list
) {
2784 dma_free_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
2785 port
->block1
, port
->block1_dma
);
2786 port
->block1
= NULL
;
2787 port
->block1_dma
= 0;
2791 /* Setup all pointers into first DMA region */
2792 port
->rxfis
= port
->block1
+ AHCI_RX_FIS_OFFSET
;
2793 port
->rxfis_dma
= port
->block1_dma
+ AHCI_RX_FIS_OFFSET
;
2794 port
->identify
= port
->block1
+ AHCI_IDFY_OFFSET
;
2795 port
->identify_dma
= port
->block1_dma
+ AHCI_IDFY_OFFSET
;
2796 port
->log_buf
= port
->block1
+ AHCI_SECTBUF_OFFSET
;
2797 port
->log_buf_dma
= port
->block1_dma
+ AHCI_SECTBUF_OFFSET
;
2798 port
->smart_buf
= port
->block1
+ AHCI_SMARTBUF_OFFSET
;
2799 port
->smart_buf_dma
= port
->block1_dma
+ AHCI_SMARTBUF_OFFSET
;
2804 static int mtip_hw_get_identify(struct driver_data
*dd
)
2806 struct smart_attr attr242
;
2810 if (mtip_get_identify(dd
->port
, NULL
) < 0)
2813 if (*(dd
->port
->identify
+ MTIP_FTL_REBUILD_OFFSET
) ==
2814 MTIP_FTL_REBUILD_MAGIC
) {
2815 set_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
);
2816 return MTIP_FTL_REBUILD_MAGIC
;
2818 mtip_dump_identify(dd
->port
);
2820 /* check write protect, over temp and rebuild statuses */
2821 rv
= mtip_read_log_page(dd
->port
, ATA_LOG_SATA_NCQ
,
2823 dd
->port
->log_buf_dma
, 1);
2825 dev_warn(&dd
->pdev
->dev
,
2826 "Error in READ LOG EXT (10h) command\n");
2827 /* non-critical error, don't fail the load */
2829 buf
= (unsigned char *)dd
->port
->log_buf
;
2830 if (buf
[259] & 0x1) {
2831 dev_info(&dd
->pdev
->dev
,
2832 "Write protect bit is set.\n");
2833 set_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
);
2835 if (buf
[288] == 0xF7) {
2836 dev_info(&dd
->pdev
->dev
,
2837 "Exceeded Tmax, drive in thermal shutdown.\n");
2838 set_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
);
2840 if (buf
[288] == 0xBF) {
2841 dev_info(&dd
->pdev
->dev
,
2842 "Drive indicates rebuild has failed.\n");
2843 set_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
);
2847 /* get write protect progess */
2848 memset(&attr242
, 0, sizeof(struct smart_attr
));
2849 if (mtip_get_smart_attr(dd
->port
, 242, &attr242
))
2850 dev_warn(&dd
->pdev
->dev
,
2851 "Unable to check write protect progress\n");
2853 dev_info(&dd
->pdev
->dev
,
2854 "Write protect progress: %u%% (%u blocks)\n",
2855 attr242
.cur
, le32_to_cpu(attr242
.data
));
2861 * Called once for each card.
2863 * @dd Pointer to the driver data structure.
2866 * 0 on success, else an error code.
2868 static int mtip_hw_init(struct driver_data
*dd
)
2872 unsigned long timeout
, timetaken
;
2874 dd
->mmio
= pcim_iomap_table(dd
->pdev
)[MTIP_ABAR
];
2876 mtip_detect_product(dd
);
2877 if (dd
->product_type
== MTIP_PRODUCT_UNKNOWN
) {
2884 dd
->port
= kzalloc_node(sizeof(struct mtip_port
), GFP_KERNEL
,
2887 dev_err(&dd
->pdev
->dev
,
2888 "Memory allocation: port structure\n");
2892 /* Continue workqueue setup */
2893 for (i
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++)
2894 dd
->work
[i
].port
= dd
->port
;
2896 /* Enable unaligned IO constraints for some devices */
2897 if (mtip_device_unaligned_constrained(dd
))
2898 dd
->unal_qdepth
= MTIP_MAX_UNALIGNED_SLOTS
;
2900 dd
->unal_qdepth
= 0;
2902 atomic_set(&dd
->port
->cmd_slot_unal
, dd
->unal_qdepth
);
2904 /* Spinlock to prevent concurrent issue */
2905 for (i
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++)
2906 spin_lock_init(&dd
->port
->cmd_issue_lock
[i
]);
2908 /* Set the port mmio base address. */
2909 dd
->port
->mmio
= dd
->mmio
+ PORT_OFFSET
;
2912 /* DMA allocations */
2913 rv
= mtip_dma_alloc(dd
);
2917 /* Setup the pointers to the extended s_active and CI registers. */
2918 for (i
= 0; i
< dd
->slot_groups
; i
++) {
2919 dd
->port
->s_active
[i
] =
2920 dd
->port
->mmio
+ i
*0x80 + PORT_SCR_ACT
;
2921 dd
->port
->cmd_issue
[i
] =
2922 dd
->port
->mmio
+ i
*0x80 + PORT_COMMAND_ISSUE
;
2923 dd
->port
->completed
[i
] =
2924 dd
->port
->mmio
+ i
*0x80 + PORT_SDBV
;
2927 timetaken
= jiffies
;
2928 timeout
= jiffies
+ msecs_to_jiffies(30000);
2929 while (((readl(dd
->port
->mmio
+ PORT_SCR_STAT
) & 0x0F) != 0x03) &&
2930 time_before(jiffies
, timeout
)) {
2933 if (unlikely(mtip_check_surprise_removal(dd
->pdev
))) {
2934 timetaken
= jiffies
- timetaken
;
2935 dev_warn(&dd
->pdev
->dev
,
2936 "Surprise removal detected at %u ms\n",
2937 jiffies_to_msecs(timetaken
));
2941 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))) {
2942 timetaken
= jiffies
- timetaken
;
2943 dev_warn(&dd
->pdev
->dev
,
2944 "Removal detected at %u ms\n",
2945 jiffies_to_msecs(timetaken
));
2950 /* Conditionally reset the HBA. */
2951 if (!(readl(dd
->mmio
+ HOST_CAP
) & HOST_CAP_NZDMA
)) {
2952 if (mtip_hba_reset(dd
) < 0) {
2953 dev_err(&dd
->pdev
->dev
,
2954 "Card did not reset within timeout\n");
2959 /* Clear any pending interrupts on the HBA */
2960 writel(readl(dd
->mmio
+ HOST_IRQ_STAT
),
2961 dd
->mmio
+ HOST_IRQ_STAT
);
2964 mtip_init_port(dd
->port
);
2965 mtip_start_port(dd
->port
);
2967 /* Setup the ISR and enable interrupts. */
2968 rv
= request_irq(dd
->pdev
->irq
, mtip_irq_handler
, IRQF_SHARED
,
2969 dev_driver_string(&dd
->pdev
->dev
), dd
);
2971 dev_err(&dd
->pdev
->dev
,
2972 "Unable to allocate IRQ %d\n", dd
->pdev
->irq
);
2975 irq_set_affinity_hint(dd
->pdev
->irq
, get_cpu_mask(dd
->isr_binding
));
2977 /* Enable interrupts on the HBA. */
2978 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
2979 dd
->mmio
+ HOST_CTL
);
2981 init_waitqueue_head(&dd
->port
->svc_wait
);
2983 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)) {
2991 /* Disable interrupts on the HBA. */
2992 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
2993 dd
->mmio
+ HOST_CTL
);
2995 /* Release the IRQ. */
2996 irq_set_affinity_hint(dd
->pdev
->irq
, NULL
);
2997 free_irq(dd
->pdev
->irq
, dd
);
3000 mtip_deinit_port(dd
->port
);
3004 /* Free the memory allocated for the for structure. */
3010 static int mtip_standby_drive(struct driver_data
*dd
)
3014 if (dd
->sr
|| !dd
->port
)
3017 * Send standby immediate (E0h) to the drive so that it
3020 if (!test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
) &&
3021 !test_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
) &&
3022 !test_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
)) {
3023 rv
= mtip_standby_immediate(dd
->port
);
3025 dev_warn(&dd
->pdev
->dev
,
3026 "STANDBY IMMEDIATE failed\n");
3032 * Called to deinitialize an interface.
3034 * @dd Pointer to the driver data structure.
3039 static int mtip_hw_exit(struct driver_data
*dd
)
3042 /* de-initialize the port. */
3043 mtip_deinit_port(dd
->port
);
3045 /* Disable interrupts on the HBA. */
3046 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3047 dd
->mmio
+ HOST_CTL
);
3050 /* Release the IRQ. */
3051 irq_set_affinity_hint(dd
->pdev
->irq
, NULL
);
3052 free_irq(dd
->pdev
->irq
, dd
);
3055 /* Free dma regions */
3058 /* Free the memory allocated for the for structure. */
3066 * Issue a Standby Immediate command to the device.
3068 * This function is called by the Block Layer just before the
3069 * system powers off during a shutdown.
3071 * @dd Pointer to the driver data structure.
3076 static int mtip_hw_shutdown(struct driver_data
*dd
)
3079 * Send standby immediate (E0h) to the drive so that it
3082 mtip_standby_drive(dd
);
3090 * This function is called by the Block Layer just before the
3091 * system hibernates.
3093 * @dd Pointer to the driver data structure.
3096 * 0 Suspend was successful
3097 * -EFAULT Suspend was not successful
3099 static int mtip_hw_suspend(struct driver_data
*dd
)
3102 * Send standby immediate (E0h) to the drive
3103 * so that it saves its state.
3105 if (mtip_standby_drive(dd
) != 0) {
3106 dev_err(&dd
->pdev
->dev
,
3107 "Failed standby-immediate command\n");
3111 /* Disable interrupts on the HBA.*/
3112 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3113 dd
->mmio
+ HOST_CTL
);
3114 mtip_deinit_port(dd
->port
);
3122 * This function is called by the Block Layer as the
3125 * @dd Pointer to the driver data structure.
3128 * 0 Resume was successful
3129 * -EFAULT Resume was not successful
3131 static int mtip_hw_resume(struct driver_data
*dd
)
3133 /* Perform any needed hardware setup steps */
3137 if (mtip_hba_reset(dd
) != 0) {
3138 dev_err(&dd
->pdev
->dev
,
3139 "Unable to reset the HBA\n");
3144 * Enable the port, DMA engine, and FIS reception specific
3145 * h/w in controller.
3147 mtip_init_port(dd
->port
);
3148 mtip_start_port(dd
->port
);
3150 /* Enable interrupts on the HBA.*/
3151 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
3152 dd
->mmio
+ HOST_CTL
);
3158 * Helper function for reusing disk name
3159 * upon hot insertion.
3161 static int rssd_disk_name_format(char *prefix
,
3166 const int base
= 'z' - 'a' + 1;
3167 char *begin
= buf
+ strlen(prefix
);
3168 char *end
= buf
+ buflen
;
3178 *--p
= 'a' + (index
% unit
);
3179 index
= (index
/ unit
) - 1;
3180 } while (index
>= 0);
3182 memmove(begin
, p
, end
- p
);
3183 memcpy(buf
, prefix
, strlen(prefix
));
3189 * Block layer IOCTL handler.
3191 * @dev Pointer to the block_device structure.
3193 * @cmd IOCTL command passed from the user application.
3194 * @arg Argument passed from the user application.
3197 * 0 IOCTL completed successfully.
3198 * -ENOTTY IOCTL not supported or invalid driver data
3199 * structure pointer.
3201 static int mtip_block_ioctl(struct block_device
*dev
,
3206 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3208 if (!capable(CAP_SYS_ADMIN
))
3214 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)))
3221 return mtip_hw_ioctl(dd
, cmd
, arg
);
3225 #ifdef CONFIG_COMPAT
3227 * Block layer compat IOCTL handler.
3229 * @dev Pointer to the block_device structure.
3231 * @cmd IOCTL command passed from the user application.
3232 * @arg Argument passed from the user application.
3235 * 0 IOCTL completed successfully.
3236 * -ENOTTY IOCTL not supported or invalid driver data
3237 * structure pointer.
3239 static int mtip_block_compat_ioctl(struct block_device
*dev
,
3244 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3246 if (!capable(CAP_SYS_ADMIN
))
3252 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)))
3258 case HDIO_DRIVE_TASKFILE
: {
3259 struct mtip_compat_ide_task_request_s __user
*compat_req_task
;
3260 ide_task_request_t req_task
;
3261 int compat_tasksize
, outtotal
, ret
;
3264 sizeof(struct mtip_compat_ide_task_request_s
);
3267 (struct mtip_compat_ide_task_request_s __user
*) arg
;
3269 if (copy_from_user(&req_task
, (void __user
*) arg
,
3270 compat_tasksize
- (2 * sizeof(compat_long_t
))))
3273 if (get_user(req_task
.out_size
, &compat_req_task
->out_size
))
3276 if (get_user(req_task
.in_size
, &compat_req_task
->in_size
))
3279 outtotal
= sizeof(struct mtip_compat_ide_task_request_s
);
3281 ret
= exec_drive_taskfile(dd
, (void __user
*) arg
,
3282 &req_task
, outtotal
);
3284 if (copy_to_user((void __user
*) arg
, &req_task
,
3286 (2 * sizeof(compat_long_t
))))
3289 if (put_user(req_task
.out_size
, &compat_req_task
->out_size
))
3292 if (put_user(req_task
.in_size
, &compat_req_task
->in_size
))
3298 return mtip_hw_ioctl(dd
, cmd
, arg
);
3304 * Obtain the geometry of the device.
3306 * You may think that this function is obsolete, but some applications,
3307 * fdisk for example still used CHS values. This function describes the
3308 * device as having 224 heads and 56 sectors per cylinder. These values are
3309 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3310 * partition is described in terms of a start and end cylinder this means
3311 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3312 * affects performance.
3314 * @dev Pointer to the block_device strucutre.
3315 * @geo Pointer to a hd_geometry structure.
3318 * 0 Operation completed successfully.
3319 * -ENOTTY An error occurred while reading the drive capacity.
3321 static int mtip_block_getgeo(struct block_device
*dev
,
3322 struct hd_geometry
*geo
)
3324 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3330 if (!(mtip_hw_get_capacity(dd
, &capacity
))) {
3331 dev_warn(&dd
->pdev
->dev
,
3332 "Could not get drive capacity.\n");
3338 sector_div(capacity
, (geo
->heads
* geo
->sectors
));
3339 geo
->cylinders
= capacity
;
3343 static int mtip_block_open(struct block_device
*dev
, fmode_t mode
)
3345 struct driver_data
*dd
;
3347 if (dev
&& dev
->bd_disk
) {
3348 dd
= (struct driver_data
*) dev
->bd_disk
->private_data
;
3351 if (test_bit(MTIP_DDF_REMOVAL_BIT
,
3361 static void mtip_block_release(struct gendisk
*disk
, fmode_t mode
)
3366 * Block device operation function.
3368 * This structure contains pointers to the functions required by the block
3371 static const struct block_device_operations mtip_block_ops
= {
3372 .open
= mtip_block_open
,
3373 .release
= mtip_block_release
,
3374 .ioctl
= mtip_block_ioctl
,
3375 #ifdef CONFIG_COMPAT
3376 .compat_ioctl
= mtip_block_compat_ioctl
,
3378 .getgeo
= mtip_block_getgeo
,
3379 .owner
= THIS_MODULE
3382 static inline bool is_se_active(struct driver_data
*dd
)
3384 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT
, &dd
->port
->flags
))) {
3385 if (dd
->port
->ic_pause_timer
) {
3386 unsigned long to
= dd
->port
->ic_pause_timer
+
3387 msecs_to_jiffies(1000);
3388 if (time_after(jiffies
, to
)) {
3389 clear_bit(MTIP_PF_SE_ACTIVE_BIT
,
3391 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
);
3392 dd
->port
->ic_pause_timer
= 0;
3393 wake_up_interruptible(&dd
->port
->svc_wait
);
3402 static inline bool is_stopped(struct driver_data
*dd
, struct request
*rq
)
3404 if (likely(!(dd
->dd_flag
& MTIP_DDF_STOP_IO
)))
3407 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))
3409 if (test_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
))
3411 if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
) &&
3414 if (test_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
))
3416 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
))
3422 static bool mtip_check_unal_depth(struct blk_mq_hw_ctx
*hctx
,
3425 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3426 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3428 if (rq_data_dir(rq
) == READ
|| !dd
->unal_qdepth
)
3432 * If unaligned depth must be limited on this controller, mark it
3433 * as unaligned if the IO isn't on a 4k boundary (start of length).
3435 if (blk_rq_sectors(rq
) <= 64) {
3436 if ((blk_rq_pos(rq
) & 7) || (blk_rq_sectors(rq
) & 7))
3440 if (cmd
->unaligned
&& atomic_dec_if_positive(&dd
->port
->cmd_slot_unal
) >= 0)
3446 static blk_status_t
mtip_issue_reserved_cmd(struct blk_mq_hw_ctx
*hctx
,
3449 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3450 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3451 struct mtip_int_cmd
*icmd
= cmd
->icmd
;
3452 struct mtip_cmd_hdr
*hdr
=
3453 dd
->port
->command_list
+ sizeof(struct mtip_cmd_hdr
) * rq
->tag
;
3454 struct mtip_cmd_sg
*command_sg
;
3456 if (mtip_commands_active(dd
->port
))
3457 return BLK_STS_DEV_RESOURCE
;
3459 hdr
->ctba
= cpu_to_le32(cmd
->command_dma
& 0xFFFFFFFF);
3460 if (test_bit(MTIP_PF_HOST_CAP_64
, &dd
->port
->flags
))
3461 hdr
->ctbau
= cpu_to_le32((cmd
->command_dma
>> 16) >> 16);
3462 /* Populate the SG list */
3463 hdr
->opts
= cpu_to_le32(icmd
->opts
| icmd
->fis_len
);
3464 if (icmd
->buf_len
) {
3465 command_sg
= cmd
->command
+ AHCI_CMD_TBL_HDR_SZ
;
3467 command_sg
->info
= cpu_to_le32((icmd
->buf_len
-1) & 0x3FFFFF);
3468 command_sg
->dba
= cpu_to_le32(icmd
->buffer
& 0xFFFFFFFF);
3469 command_sg
->dba_upper
=
3470 cpu_to_le32((icmd
->buffer
>> 16) >> 16);
3472 hdr
->opts
|= cpu_to_le32((1 << 16));
3475 /* Populate the command header */
3476 hdr
->byte_count
= 0;
3478 blk_mq_start_request(rq
);
3479 mtip_issue_non_ncq_command(dd
->port
, rq
->tag
);
3483 static blk_status_t
mtip_queue_rq(struct blk_mq_hw_ctx
*hctx
,
3484 const struct blk_mq_queue_data
*bd
)
3486 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3487 struct request
*rq
= bd
->rq
;
3488 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3490 if (blk_rq_is_passthrough(rq
))
3491 return mtip_issue_reserved_cmd(hctx
, rq
);
3493 if (unlikely(mtip_check_unal_depth(hctx
, rq
)))
3494 return BLK_STS_DEV_RESOURCE
;
3496 if (is_se_active(dd
) || is_stopped(dd
, rq
))
3497 return BLK_STS_IOERR
;
3499 blk_mq_start_request(rq
);
3501 mtip_hw_submit_io(dd
, rq
, cmd
, hctx
);
3505 static void mtip_free_cmd(struct blk_mq_tag_set
*set
, struct request
*rq
,
3506 unsigned int hctx_idx
)
3508 struct driver_data
*dd
= set
->driver_data
;
3509 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3514 dma_free_coherent(&dd
->pdev
->dev
, CMD_DMA_ALLOC_SZ
, cmd
->command
,
3518 static int mtip_init_cmd(struct blk_mq_tag_set
*set
, struct request
*rq
,
3519 unsigned int hctx_idx
, unsigned int numa_node
)
3521 struct driver_data
*dd
= set
->driver_data
;
3522 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3524 cmd
->command
= dma_alloc_coherent(&dd
->pdev
->dev
, CMD_DMA_ALLOC_SZ
,
3525 &cmd
->command_dma
, GFP_KERNEL
);
3529 sg_init_table(cmd
->sg
, MTIP_MAX_SG
);
3533 static enum blk_eh_timer_return
mtip_cmd_timeout(struct request
*req
,
3536 struct driver_data
*dd
= req
->q
->queuedata
;
3539 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
3541 cmd
->status
= BLK_STS_TIMEOUT
;
3542 blk_mq_complete_request(req
);
3546 if (test_bit(req
->tag
, dd
->port
->cmds_to_issue
))
3549 if (test_and_set_bit(MTIP_PF_TO_ACTIVE_BIT
, &dd
->port
->flags
))
3552 wake_up_interruptible(&dd
->port
->svc_wait
);
3554 return BLK_EH_RESET_TIMER
;
3557 static const struct blk_mq_ops mtip_mq_ops
= {
3558 .queue_rq
= mtip_queue_rq
,
3559 .init_request
= mtip_init_cmd
,
3560 .exit_request
= mtip_free_cmd
,
3561 .complete
= mtip_softirq_done_fn
,
3562 .timeout
= mtip_cmd_timeout
,
3566 * Block layer initialization function.
3568 * This function is called once by the PCI layer for each P320
3569 * device that is connected to the system.
3571 * @dd Pointer to the driver data structure.
3574 * 0 on success else an error code.
3576 static int mtip_block_initialize(struct driver_data
*dd
)
3578 int rv
= 0, wait_for_rebuild
= 0;
3580 unsigned int index
= 0;
3581 struct kobject
*kobj
;
3584 goto skip_create_disk
; /* hw init done, before rebuild */
3586 if (mtip_hw_init(dd
)) {
3588 goto protocol_init_error
;
3591 dd
->disk
= alloc_disk_node(MTIP_MAX_MINORS
, dd
->numa_node
);
3592 if (dd
->disk
== NULL
) {
3593 dev_err(&dd
->pdev
->dev
,
3594 "Unable to allocate gendisk structure\n");
3596 goto alloc_disk_error
;
3599 rv
= ida_alloc(&rssd_index_ida
, GFP_KERNEL
);
3604 rv
= rssd_disk_name_format("rssd",
3606 dd
->disk
->disk_name
,
3609 goto disk_index_error
;
3611 dd
->disk
->major
= dd
->major
;
3612 dd
->disk
->first_minor
= index
* MTIP_MAX_MINORS
;
3613 dd
->disk
->minors
= MTIP_MAX_MINORS
;
3614 dd
->disk
->fops
= &mtip_block_ops
;
3615 dd
->disk
->private_data
= dd
;
3618 mtip_hw_debugfs_init(dd
);
3620 memset(&dd
->tags
, 0, sizeof(dd
->tags
));
3621 dd
->tags
.ops
= &mtip_mq_ops
;
3622 dd
->tags
.nr_hw_queues
= 1;
3623 dd
->tags
.queue_depth
= MTIP_MAX_COMMAND_SLOTS
;
3624 dd
->tags
.reserved_tags
= 1;
3625 dd
->tags
.cmd_size
= sizeof(struct mtip_cmd
);
3626 dd
->tags
.numa_node
= dd
->numa_node
;
3627 dd
->tags
.flags
= BLK_MQ_F_SHOULD_MERGE
;
3628 dd
->tags
.driver_data
= dd
;
3629 dd
->tags
.timeout
= MTIP_NCQ_CMD_TIMEOUT_MS
;
3631 rv
= blk_mq_alloc_tag_set(&dd
->tags
);
3633 dev_err(&dd
->pdev
->dev
,
3634 "Unable to allocate request queue\n");
3635 goto block_queue_alloc_tag_error
;
3638 /* Allocate the request queue. */
3639 dd
->queue
= blk_mq_init_queue(&dd
->tags
);
3640 if (IS_ERR(dd
->queue
)) {
3641 dev_err(&dd
->pdev
->dev
,
3642 "Unable to allocate request queue\n");
3644 goto block_queue_alloc_init_error
;
3647 dd
->disk
->queue
= dd
->queue
;
3648 dd
->queue
->queuedata
= dd
;
3651 /* Initialize the protocol layer. */
3652 wait_for_rebuild
= mtip_hw_get_identify(dd
);
3653 if (wait_for_rebuild
< 0) {
3654 dev_err(&dd
->pdev
->dev
,
3655 "Protocol layer initialization failed\n");
3657 goto init_hw_cmds_error
;
3661 * if rebuild pending, start the service thread, and delay the block
3662 * queue creation and device_add_disk()
3664 if (wait_for_rebuild
== MTIP_FTL_REBUILD_MAGIC
)
3665 goto start_service_thread
;
3667 /* Set device limits. */
3668 blk_queue_flag_set(QUEUE_FLAG_NONROT
, dd
->queue
);
3669 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM
, dd
->queue
);
3670 blk_queue_max_segments(dd
->queue
, MTIP_MAX_SG
);
3671 blk_queue_physical_block_size(dd
->queue
, 4096);
3672 blk_queue_max_hw_sectors(dd
->queue
, 0xffff);
3673 blk_queue_max_segment_size(dd
->queue
, 0x400000);
3674 dma_set_max_seg_size(&dd
->pdev
->dev
, 0x400000);
3675 blk_queue_io_min(dd
->queue
, 4096);
3677 /* Set the capacity of the device in 512 byte sectors. */
3678 if (!(mtip_hw_get_capacity(dd
, &capacity
))) {
3679 dev_warn(&dd
->pdev
->dev
,
3680 "Could not read drive capacity\n");
3682 goto read_capacity_error
;
3684 set_capacity(dd
->disk
, capacity
);
3686 /* Enable the block device and add it to /dev */
3687 device_add_disk(&dd
->pdev
->dev
, dd
->disk
, NULL
);
3689 dd
->bdev
= bdget_disk(dd
->disk
, 0);
3691 * Now that the disk is active, initialize any sysfs attributes
3692 * managed by the protocol layer.
3694 kobj
= kobject_get(&disk_to_dev(dd
->disk
)->kobj
);
3696 mtip_hw_sysfs_init(dd
, kobj
);
3700 if (dd
->mtip_svc_handler
) {
3701 set_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
);
3702 return rv
; /* service thread created for handling rebuild */
3705 start_service_thread
:
3706 dd
->mtip_svc_handler
= kthread_create_on_node(mtip_service_thread
,
3708 "mtip_svc_thd_%02d", index
);
3710 if (IS_ERR(dd
->mtip_svc_handler
)) {
3711 dev_err(&dd
->pdev
->dev
, "service thread failed to start\n");
3712 dd
->mtip_svc_handler
= NULL
;
3714 goto kthread_run_error
;
3716 wake_up_process(dd
->mtip_svc_handler
);
3717 if (wait_for_rebuild
== MTIP_FTL_REBUILD_MAGIC
)
3718 rv
= wait_for_rebuild
;
3726 /* Delete our gendisk. This also removes the device from /dev */
3727 del_gendisk(dd
->disk
);
3729 read_capacity_error
:
3731 blk_cleanup_queue(dd
->queue
);
3732 block_queue_alloc_init_error
:
3733 blk_mq_free_tag_set(&dd
->tags
);
3734 block_queue_alloc_tag_error
:
3735 mtip_hw_debugfs_exit(dd
);
3737 ida_free(&rssd_index_ida
, index
);
3743 mtip_hw_exit(dd
); /* De-initialize the protocol layer. */
3745 protocol_init_error
:
3749 static bool mtip_no_dev_cleanup(struct request
*rq
, void *data
, bool reserv
)
3751 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3753 cmd
->status
= BLK_STS_IOERR
;
3754 blk_mq_complete_request(rq
);
3759 * Block layer deinitialization function.
3761 * Called by the PCI layer as each P320 device is removed.
3763 * @dd Pointer to the driver data structure.
3768 static int mtip_block_remove(struct driver_data
*dd
)
3770 struct kobject
*kobj
;
3772 mtip_hw_debugfs_exit(dd
);
3774 if (dd
->mtip_svc_handler
) {
3775 set_bit(MTIP_PF_SVC_THD_STOP_BIT
, &dd
->port
->flags
);
3776 wake_up_interruptible(&dd
->port
->svc_wait
);
3777 kthread_stop(dd
->mtip_svc_handler
);
3780 /* Clean up the sysfs attributes, if created */
3781 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
)) {
3782 kobj
= kobject_get(&disk_to_dev(dd
->disk
)->kobj
);
3784 mtip_hw_sysfs_exit(dd
, kobj
);
3791 * Explicitly wait here for IOs to quiesce,
3792 * as mtip_standby_drive usually won't wait for IOs.
3794 if (!mtip_quiesce_io(dd
->port
, MTIP_QUIESCE_IO_TIMEOUT_MS
))
3795 mtip_standby_drive(dd
);
3798 dev_info(&dd
->pdev
->dev
, "device %s surprise removal\n",
3799 dd
->disk
->disk_name
);
3801 blk_freeze_queue_start(dd
->queue
);
3802 blk_mq_quiesce_queue(dd
->queue
);
3803 blk_mq_tagset_busy_iter(&dd
->tags
, mtip_no_dev_cleanup
, dd
);
3804 blk_mq_unquiesce_queue(dd
->queue
);
3807 * Delete our gendisk structure. This also removes the device
3815 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
))
3816 del_gendisk(dd
->disk
);
3817 if (dd
->disk
->queue
) {
3818 blk_cleanup_queue(dd
->queue
);
3819 blk_mq_free_tag_set(&dd
->tags
);
3826 ida_free(&rssd_index_ida
, dd
->index
);
3828 /* De-initialize the protocol layer. */
3835 * Function called by the PCI layer when just before the
3836 * machine shuts down.
3838 * If a protocol layer shutdown function is present it will be called
3841 * @dd Pointer to the driver data structure.
3846 static int mtip_block_shutdown(struct driver_data
*dd
)
3848 mtip_hw_shutdown(dd
);
3850 /* Delete our gendisk structure, and cleanup the blk queue. */
3852 dev_info(&dd
->pdev
->dev
,
3853 "Shutting down %s ...\n", dd
->disk
->disk_name
);
3855 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
))
3856 del_gendisk(dd
->disk
);
3857 if (dd
->disk
->queue
) {
3858 blk_cleanup_queue(dd
->queue
);
3859 blk_mq_free_tag_set(&dd
->tags
);
3866 ida_free(&rssd_index_ida
, dd
->index
);
3870 static int mtip_block_suspend(struct driver_data
*dd
)
3872 dev_info(&dd
->pdev
->dev
,
3873 "Suspending %s ...\n", dd
->disk
->disk_name
);
3874 mtip_hw_suspend(dd
);
3878 static int mtip_block_resume(struct driver_data
*dd
)
3880 dev_info(&dd
->pdev
->dev
, "Resuming %s ...\n",
3881 dd
->disk
->disk_name
);
3886 static void drop_cpu(int cpu
)
3891 static int get_least_used_cpu_on_node(int node
)
3893 int cpu
, least_used_cpu
, least_cnt
;
3894 const struct cpumask
*node_mask
;
3896 node_mask
= cpumask_of_node(node
);
3897 least_used_cpu
= cpumask_first(node_mask
);
3898 least_cnt
= cpu_use
[least_used_cpu
];
3899 cpu
= least_used_cpu
;
3901 for_each_cpu(cpu
, node_mask
) {
3902 if (cpu_use
[cpu
] < least_cnt
) {
3903 least_used_cpu
= cpu
;
3904 least_cnt
= cpu_use
[cpu
];
3907 cpu_use
[least_used_cpu
]++;
3908 return least_used_cpu
;
3911 /* Helper for selecting a node in round robin mode */
3912 static inline int mtip_get_next_rr_node(void)
3914 static int next_node
= NUMA_NO_NODE
;
3916 if (next_node
== NUMA_NO_NODE
) {
3917 next_node
= first_online_node
;
3921 next_node
= next_online_node(next_node
);
3922 if (next_node
== MAX_NUMNODES
)
3923 next_node
= first_online_node
;
3927 static DEFINE_HANDLER(0);
3928 static DEFINE_HANDLER(1);
3929 static DEFINE_HANDLER(2);
3930 static DEFINE_HANDLER(3);
3931 static DEFINE_HANDLER(4);
3932 static DEFINE_HANDLER(5);
3933 static DEFINE_HANDLER(6);
3934 static DEFINE_HANDLER(7);
3936 static void mtip_disable_link_opts(struct driver_data
*dd
, struct pci_dev
*pdev
)
3939 unsigned short pcie_dev_ctrl
;
3941 pos
= pci_find_capability(pdev
, PCI_CAP_ID_EXP
);
3943 pci_read_config_word(pdev
,
3944 pos
+ PCI_EXP_DEVCTL
,
3946 if (pcie_dev_ctrl
& (1 << 11) ||
3947 pcie_dev_ctrl
& (1 << 4)) {
3948 dev_info(&dd
->pdev
->dev
,
3949 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
3950 pdev
->vendor
, pdev
->device
);
3951 pcie_dev_ctrl
&= ~(PCI_EXP_DEVCTL_NOSNOOP_EN
|
3952 PCI_EXP_DEVCTL_RELAX_EN
);
3953 pci_write_config_word(pdev
,
3954 pos
+ PCI_EXP_DEVCTL
,
3960 static void mtip_fix_ero_nosnoop(struct driver_data
*dd
, struct pci_dev
*pdev
)
3963 * This workaround is specific to AMD/ATI chipset with a PCI upstream
3964 * device with device id 0x5aXX
3966 if (pdev
->bus
&& pdev
->bus
->self
) {
3967 if (pdev
->bus
->self
->vendor
== PCI_VENDOR_ID_ATI
&&
3968 ((pdev
->bus
->self
->device
& 0xff00) == 0x5a00)) {
3969 mtip_disable_link_opts(dd
, pdev
->bus
->self
);
3971 /* Check further up the topology */
3972 struct pci_dev
*parent_dev
= pdev
->bus
->self
;
3973 if (parent_dev
->bus
&&
3974 parent_dev
->bus
->parent
&&
3975 parent_dev
->bus
->parent
->self
&&
3976 parent_dev
->bus
->parent
->self
->vendor
==
3977 PCI_VENDOR_ID_ATI
&&
3978 (parent_dev
->bus
->parent
->self
->device
&
3979 0xff00) == 0x5a00) {
3980 mtip_disable_link_opts(dd
,
3981 parent_dev
->bus
->parent
->self
);
3988 * Called for each supported PCI device detected.
3990 * This function allocates the private data structure, enables the
3991 * PCI device and then calls the block layer initialization function.
3994 * 0 on success else an error code.
3996 static int mtip_pci_probe(struct pci_dev
*pdev
,
3997 const struct pci_device_id
*ent
)
4000 struct driver_data
*dd
= NULL
;
4002 const struct cpumask
*node_mask
;
4003 int cpu
, i
= 0, j
= 0;
4004 int my_node
= NUMA_NO_NODE
;
4005 unsigned long flags
;
4007 /* Allocate memory for this devices private data. */
4008 my_node
= pcibus_to_node(pdev
->bus
);
4009 if (my_node
!= NUMA_NO_NODE
) {
4010 if (!node_online(my_node
))
4011 my_node
= mtip_get_next_rr_node();
4013 dev_info(&pdev
->dev
, "Kernel not reporting proximity, choosing a node\n");
4014 my_node
= mtip_get_next_rr_node();
4016 dev_info(&pdev
->dev
, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4017 my_node
, pcibus_to_node(pdev
->bus
), dev_to_node(&pdev
->dev
),
4018 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
4020 dd
= kzalloc_node(sizeof(struct driver_data
), GFP_KERNEL
, my_node
);
4023 "Unable to allocate memory for driver data\n");
4027 /* Attach the private data to this PCI device. */
4028 pci_set_drvdata(pdev
, dd
);
4030 rv
= pcim_enable_device(pdev
);
4032 dev_err(&pdev
->dev
, "Unable to enable device\n");
4036 /* Map BAR5 to memory. */
4037 rv
= pcim_iomap_regions(pdev
, 1 << MTIP_ABAR
, MTIP_DRV_NAME
);
4039 dev_err(&pdev
->dev
, "Unable to map regions\n");
4043 rv
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(64));
4045 dev_warn(&pdev
->dev
, "64-bit DMA enable failed\n");
4049 /* Copy the info we may need later into the private data structure. */
4050 dd
->major
= mtip_major
;
4051 dd
->instance
= instance
;
4053 dd
->numa_node
= my_node
;
4055 INIT_LIST_HEAD(&dd
->online_list
);
4056 INIT_LIST_HEAD(&dd
->remove_list
);
4058 memset(dd
->workq_name
, 0, 32);
4059 snprintf(dd
->workq_name
, 31, "mtipq%d", dd
->instance
);
4061 dd
->isr_workq
= create_workqueue(dd
->workq_name
);
4062 if (!dd
->isr_workq
) {
4063 dev_warn(&pdev
->dev
, "Can't create wq %d\n", dd
->instance
);
4068 memset(cpu_list
, 0, sizeof(cpu_list
));
4070 node_mask
= cpumask_of_node(dd
->numa_node
);
4071 if (!cpumask_empty(node_mask
)) {
4072 for_each_cpu(cpu
, node_mask
)
4074 snprintf(&cpu_list
[j
], 256 - j
, "%d ", cpu
);
4075 j
= strlen(cpu_list
);
4078 dev_info(&pdev
->dev
, "Node %d on package %d has %d cpu(s): %s\n",
4080 topology_physical_package_id(cpumask_first(node_mask
)),
4081 nr_cpus_node(dd
->numa_node
),
4084 dev_dbg(&pdev
->dev
, "mtip32xx: node_mask empty\n");
4086 dd
->isr_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4087 dev_info(&pdev
->dev
, "Initial IRQ binding node:cpu %d:%d\n",
4088 cpu_to_node(dd
->isr_binding
), dd
->isr_binding
);
4090 /* first worker context always runs in ISR */
4091 dd
->work
[0].cpu_binding
= dd
->isr_binding
;
4092 dd
->work
[1].cpu_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4093 dd
->work
[2].cpu_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4094 dd
->work
[3].cpu_binding
= dd
->work
[0].cpu_binding
;
4095 dd
->work
[4].cpu_binding
= dd
->work
[1].cpu_binding
;
4096 dd
->work
[5].cpu_binding
= dd
->work
[2].cpu_binding
;
4097 dd
->work
[6].cpu_binding
= dd
->work
[2].cpu_binding
;
4098 dd
->work
[7].cpu_binding
= dd
->work
[1].cpu_binding
;
4100 /* Log the bindings */
4101 for_each_present_cpu(cpu
) {
4102 memset(cpu_list
, 0, sizeof(cpu_list
));
4103 for (i
= 0, j
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++) {
4104 if (dd
->work
[i
].cpu_binding
== cpu
) {
4105 snprintf(&cpu_list
[j
], 256 - j
, "%d ", i
);
4106 j
= strlen(cpu_list
);
4110 dev_info(&pdev
->dev
, "CPU %d: WQs %s\n", cpu
, cpu_list
);
4113 INIT_WORK(&dd
->work
[0].work
, mtip_workq_sdbf0
);
4114 INIT_WORK(&dd
->work
[1].work
, mtip_workq_sdbf1
);
4115 INIT_WORK(&dd
->work
[2].work
, mtip_workq_sdbf2
);
4116 INIT_WORK(&dd
->work
[3].work
, mtip_workq_sdbf3
);
4117 INIT_WORK(&dd
->work
[4].work
, mtip_workq_sdbf4
);
4118 INIT_WORK(&dd
->work
[5].work
, mtip_workq_sdbf5
);
4119 INIT_WORK(&dd
->work
[6].work
, mtip_workq_sdbf6
);
4120 INIT_WORK(&dd
->work
[7].work
, mtip_workq_sdbf7
);
4122 pci_set_master(pdev
);
4123 rv
= pci_enable_msi(pdev
);
4125 dev_warn(&pdev
->dev
,
4126 "Unable to enable MSI interrupt.\n");
4127 goto msi_initialize_err
;
4130 mtip_fix_ero_nosnoop(dd
, pdev
);
4132 /* Initialize the block layer. */
4133 rv
= mtip_block_initialize(dd
);
4136 "Unable to initialize block layer\n");
4137 goto block_initialize_err
;
4141 * Increment the instance count so that each device has a unique
4145 if (rv
!= MTIP_FTL_REBUILD_MAGIC
)
4146 set_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
);
4148 rv
= 0; /* device in rebuild state, return 0 from probe */
4150 /* Add to online list even if in ftl rebuild */
4151 spin_lock_irqsave(&dev_lock
, flags
);
4152 list_add(&dd
->online_list
, &online_list
);
4153 spin_unlock_irqrestore(&dev_lock
, flags
);
4157 block_initialize_err
:
4158 pci_disable_msi(pdev
);
4161 if (dd
->isr_workq
) {
4162 flush_workqueue(dd
->isr_workq
);
4163 destroy_workqueue(dd
->isr_workq
);
4164 drop_cpu(dd
->work
[0].cpu_binding
);
4165 drop_cpu(dd
->work
[1].cpu_binding
);
4166 drop_cpu(dd
->work
[2].cpu_binding
);
4169 pcim_iounmap_regions(pdev
, 1 << MTIP_ABAR
);
4173 pci_set_drvdata(pdev
, NULL
);
4180 * Called for each probed device when the device is removed or the
4181 * driver is unloaded.
4186 static void mtip_pci_remove(struct pci_dev
*pdev
)
4188 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4189 unsigned long flags
, to
;
4191 set_bit(MTIP_DDF_REMOVAL_BIT
, &dd
->dd_flag
);
4193 spin_lock_irqsave(&dev_lock
, flags
);
4194 list_del_init(&dd
->online_list
);
4195 list_add(&dd
->remove_list
, &removing_list
);
4196 spin_unlock_irqrestore(&dev_lock
, flags
);
4198 mtip_check_surprise_removal(pdev
);
4199 synchronize_irq(dd
->pdev
->irq
);
4201 /* Spin until workers are done */
4202 to
= jiffies
+ msecs_to_jiffies(4000);
4205 } while (atomic_read(&dd
->irq_workers_active
) != 0 &&
4206 time_before(jiffies
, to
));
4209 fsync_bdev(dd
->bdev
);
4211 if (atomic_read(&dd
->irq_workers_active
) != 0) {
4212 dev_warn(&dd
->pdev
->dev
,
4213 "Completion workers still active!\n");
4216 blk_set_queue_dying(dd
->queue
);
4217 set_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
);
4219 /* Clean up the block layer. */
4220 mtip_block_remove(dd
);
4222 if (dd
->isr_workq
) {
4223 flush_workqueue(dd
->isr_workq
);
4224 destroy_workqueue(dd
->isr_workq
);
4225 drop_cpu(dd
->work
[0].cpu_binding
);
4226 drop_cpu(dd
->work
[1].cpu_binding
);
4227 drop_cpu(dd
->work
[2].cpu_binding
);
4230 pci_disable_msi(pdev
);
4232 spin_lock_irqsave(&dev_lock
, flags
);
4233 list_del_init(&dd
->remove_list
);
4234 spin_unlock_irqrestore(&dev_lock
, flags
);
4238 pcim_iounmap_regions(pdev
, 1 << MTIP_ABAR
);
4239 pci_set_drvdata(pdev
, NULL
);
4243 * Called for each probed device when the device is suspended.
4249 static int mtip_pci_suspend(struct pci_dev
*pdev
, pm_message_t mesg
)
4252 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4256 "Driver private datastructure is NULL\n");
4260 set_bit(MTIP_DDF_RESUME_BIT
, &dd
->dd_flag
);
4262 /* Disable ports & interrupts then send standby immediate */
4263 rv
= mtip_block_suspend(dd
);
4266 "Failed to suspend controller\n");
4271 * Save the pci config space to pdev structure &
4272 * disable the device
4274 pci_save_state(pdev
);
4275 pci_disable_device(pdev
);
4277 /* Move to Low power state*/
4278 pci_set_power_state(pdev
, PCI_D3hot
);
4284 * Called for each probed device when the device is resumed.
4290 static int mtip_pci_resume(struct pci_dev
*pdev
)
4293 struct driver_data
*dd
;
4295 dd
= pci_get_drvdata(pdev
);
4298 "Driver private datastructure is NULL\n");
4302 /* Move the device to active State */
4303 pci_set_power_state(pdev
, PCI_D0
);
4305 /* Restore PCI configuration space */
4306 pci_restore_state(pdev
);
4308 /* Enable the PCI device*/
4309 rv
= pcim_enable_device(pdev
);
4312 "Failed to enable card during resume\n");
4315 pci_set_master(pdev
);
4318 * Calls hbaReset, initPort, & startPort function
4319 * then enables interrupts
4321 rv
= mtip_block_resume(dd
);
4323 dev_err(&pdev
->dev
, "Unable to resume\n");
4326 clear_bit(MTIP_DDF_RESUME_BIT
, &dd
->dd_flag
);
4337 static void mtip_pci_shutdown(struct pci_dev
*pdev
)
4339 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4341 mtip_block_shutdown(dd
);
4344 /* Table of device ids supported by this driver. */
4345 static const struct pci_device_id mtip_pci_tbl
[] = {
4346 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320H_DEVICE_ID
) },
4347 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320M_DEVICE_ID
) },
4348 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320S_DEVICE_ID
) },
4349 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P325M_DEVICE_ID
) },
4350 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P420H_DEVICE_ID
) },
4351 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P420M_DEVICE_ID
) },
4352 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P425M_DEVICE_ID
) },
4356 /* Structure that describes the PCI driver functions. */
4357 static struct pci_driver mtip_pci_driver
= {
4358 .name
= MTIP_DRV_NAME
,
4359 .id_table
= mtip_pci_tbl
,
4360 .probe
= mtip_pci_probe
,
4361 .remove
= mtip_pci_remove
,
4362 .suspend
= mtip_pci_suspend
,
4363 .resume
= mtip_pci_resume
,
4364 .shutdown
= mtip_pci_shutdown
,
4367 MODULE_DEVICE_TABLE(pci
, mtip_pci_tbl
);
4370 * Module initialization function.
4372 * Called once when the module is loaded. This function allocates a major
4373 * block device number to the Cyclone devices and registers the PCI layer
4377 * 0 on success else error code.
4379 static int __init
mtip_init(void)
4383 pr_info(MTIP_DRV_NAME
" Version " MTIP_DRV_VERSION
"\n");
4385 spin_lock_init(&dev_lock
);
4387 INIT_LIST_HEAD(&online_list
);
4388 INIT_LIST_HEAD(&removing_list
);
4390 /* Allocate a major block device number to use with this driver. */
4391 error
= register_blkdev(0, MTIP_DRV_NAME
);
4393 pr_err("Unable to register block device (%d)\n",
4399 dfs_parent
= debugfs_create_dir("rssd", NULL
);
4400 if (IS_ERR_OR_NULL(dfs_parent
)) {
4401 pr_warn("Error creating debugfs parent\n");
4405 dfs_device_status
= debugfs_create_file("device_status",
4406 0444, dfs_parent
, NULL
,
4407 &mtip_device_status_fops
);
4408 if (IS_ERR_OR_NULL(dfs_device_status
)) {
4409 pr_err("Error creating device_status node\n");
4410 dfs_device_status
= NULL
;
4414 /* Register our PCI operations. */
4415 error
= pci_register_driver(&mtip_pci_driver
);
4417 debugfs_remove(dfs_parent
);
4418 unregister_blkdev(mtip_major
, MTIP_DRV_NAME
);
4425 * Module de-initialization function.
4427 * Called once when the module is unloaded. This function deallocates
4428 * the major block device number allocated by mtip_init() and
4429 * unregisters the PCI layer of the driver.
4434 static void __exit
mtip_exit(void)
4436 /* Release the allocated major block device number. */
4437 unregister_blkdev(mtip_major
, MTIP_DRV_NAME
);
4439 /* Unregister the PCI driver. */
4440 pci_unregister_driver(&mtip_pci_driver
);
4442 debugfs_remove_recursive(dfs_parent
);
4445 MODULE_AUTHOR("Micron Technology, Inc");
4446 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4447 MODULE_LICENSE("GPL");
4448 MODULE_VERSION(MTIP_DRV_VERSION
);
4450 module_init(mtip_init
);
4451 module_exit(mtip_exit
);