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 if (likely(!blk_should_fake_timeout(req
->q
)))
496 blk_mq_complete_request(req
);
502 * @dd Pointer to the DRIVER_DATA structure.
507 static void mtip_handle_tfe(struct driver_data
*dd
)
509 int group
, tag
, bit
, reissue
, rv
;
510 struct mtip_port
*port
;
511 struct mtip_cmd
*cmd
;
513 struct host_to_dev_fis
*fis
;
514 unsigned long tagaccum
[SLOTBITS_IN_LONGS
];
515 unsigned int cmd_cnt
= 0;
517 char *fail_reason
= NULL
;
518 int fail_all_ncq_write
= 0, fail_all_ncq_cmds
= 0;
520 dev_warn(&dd
->pdev
->dev
, "Taskfile error\n");
524 if (test_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
)) {
525 cmd
= mtip_cmd_from_tag(dd
, MTIP_TAG_INTERNAL
);
526 dbg_printk(MTIP_DRV_NAME
" TFE for the internal command\n");
527 mtip_complete_command(cmd
, BLK_STS_IOERR
);
531 /* clear the tag accumulator */
532 memset(tagaccum
, 0, SLOTBITS_IN_LONGS
* sizeof(long));
534 /* Loop through all the groups */
535 for (group
= 0; group
< dd
->slot_groups
; group
++) {
536 completed
= readl(port
->completed
[group
]);
538 dev_warn(&dd
->pdev
->dev
, "g=%u, comp=%x\n", group
, completed
);
540 /* clear completed status register in the hardware.*/
541 writel(completed
, port
->completed
[group
]);
543 /* Process successfully completed commands */
544 for (bit
= 0; bit
< 32 && completed
; bit
++) {
545 if (!(completed
& (1<<bit
)))
547 tag
= (group
<< 5) + bit
;
549 /* Skip the internal command slot */
550 if (tag
== MTIP_TAG_INTERNAL
)
553 cmd
= mtip_cmd_from_tag(dd
, tag
);
554 mtip_complete_command(cmd
, 0);
555 set_bit(tag
, tagaccum
);
560 print_tags(dd
, "completed (TFE)", tagaccum
, cmd_cnt
);
562 /* Restart the port */
564 mtip_restart_port(port
);
566 /* Trying to determine the cause of the error */
567 rv
= mtip_read_log_page(dd
->port
, ATA_LOG_SATA_NCQ
,
569 dd
->port
->log_buf_dma
, 1);
571 dev_warn(&dd
->pdev
->dev
,
572 "Error in READ LOG EXT (10h) command\n");
573 /* non-critical error, don't fail the load */
575 buf
= (unsigned char *)dd
->port
->log_buf
;
576 if (buf
[259] & 0x1) {
577 dev_info(&dd
->pdev
->dev
,
578 "Write protect bit is set.\n");
579 set_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
);
580 fail_all_ncq_write
= 1;
581 fail_reason
= "write protect";
583 if (buf
[288] == 0xF7) {
584 dev_info(&dd
->pdev
->dev
,
585 "Exceeded Tmax, drive in thermal shutdown.\n");
586 set_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
);
587 fail_all_ncq_cmds
= 1;
588 fail_reason
= "thermal shutdown";
590 if (buf
[288] == 0xBF) {
591 set_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
);
592 dev_info(&dd
->pdev
->dev
,
593 "Drive indicates rebuild has failed. Secure erase required.\n");
594 fail_all_ncq_cmds
= 1;
595 fail_reason
= "rebuild failed";
599 /* clear the tag accumulator */
600 memset(tagaccum
, 0, SLOTBITS_IN_LONGS
* sizeof(long));
602 /* Loop through all the groups */
603 for (group
= 0; group
< dd
->slot_groups
; group
++) {
604 for (bit
= 0; bit
< 32; bit
++) {
606 tag
= (group
<< 5) + bit
;
607 cmd
= mtip_cmd_from_tag(dd
, tag
);
609 fis
= (struct host_to_dev_fis
*)cmd
->command
;
611 /* Should re-issue? */
612 if (tag
== MTIP_TAG_INTERNAL
||
613 fis
->command
== ATA_CMD_SET_FEATURES
)
616 if (fail_all_ncq_cmds
||
617 (fail_all_ncq_write
&&
618 fis
->command
== ATA_CMD_FPDMA_WRITE
)) {
619 dev_warn(&dd
->pdev
->dev
,
620 " Fail: %s w/tag %d [%s].\n",
621 fis
->command
== ATA_CMD_FPDMA_WRITE
?
624 fail_reason
!= NULL
?
625 fail_reason
: "unknown");
626 mtip_complete_command(cmd
, BLK_STS_MEDIUM
);
632 * First check if this command has
633 * exceeded its retries.
635 if (reissue
&& (cmd
->retries
-- > 0)) {
637 set_bit(tag
, tagaccum
);
639 /* Re-issue the command. */
640 mtip_issue_ncq_command(port
, tag
);
645 /* Retire a command that will not be reissued */
646 dev_warn(&port
->dd
->pdev
->dev
,
647 "retiring tag %d\n", tag
);
649 mtip_complete_command(cmd
, BLK_STS_IOERR
);
652 print_tags(dd
, "reissued (TFE)", tagaccum
, cmd_cnt
);
656 * Handle a set device bits interrupt
658 static inline void mtip_workq_sdbfx(struct mtip_port
*port
, int group
,
661 struct driver_data
*dd
= port
->dd
;
663 struct mtip_cmd
*command
;
666 WARN_ON_ONCE(!completed
);
669 /* clear completed status register in the hardware.*/
670 writel(completed
, port
->completed
[group
]);
672 /* Process completed commands. */
673 for (bit
= 0; (bit
< 32) && completed
; bit
++) {
674 if (completed
& 0x01) {
675 tag
= (group
<< 5) | bit
;
677 /* skip internal command slot. */
678 if (unlikely(tag
== MTIP_TAG_INTERNAL
))
681 command
= mtip_cmd_from_tag(dd
, tag
);
682 mtip_complete_command(command
, 0);
687 /* If last, re-enable interrupts */
688 if (atomic_dec_return(&dd
->irq_workers_active
) == 0)
689 writel(0xffffffff, dd
->mmio
+ HOST_IRQ_STAT
);
693 * Process legacy pio and d2h interrupts
695 static inline void mtip_process_legacy(struct driver_data
*dd
, u32 port_stat
)
697 struct mtip_port
*port
= dd
->port
;
698 struct mtip_cmd
*cmd
= mtip_cmd_from_tag(dd
, MTIP_TAG_INTERNAL
);
700 if (test_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
) && cmd
) {
701 int group
= MTIP_TAG_INDEX(MTIP_TAG_INTERNAL
);
702 int status
= readl(port
->cmd_issue
[group
]);
704 if (!(status
& (1 << MTIP_TAG_BIT(MTIP_TAG_INTERNAL
))))
705 mtip_complete_command(cmd
, 0);
710 * Demux and handle errors
712 static inline void mtip_process_errors(struct driver_data
*dd
, u32 port_stat
)
714 if (unlikely(port_stat
& PORT_IRQ_CONNECT
)) {
715 dev_warn(&dd
->pdev
->dev
,
716 "Clearing PxSERR.DIAG.x\n");
717 writel((1 << 26), dd
->port
->mmio
+ PORT_SCR_ERR
);
720 if (unlikely(port_stat
& PORT_IRQ_PHYRDY
)) {
721 dev_warn(&dd
->pdev
->dev
,
722 "Clearing PxSERR.DIAG.n\n");
723 writel((1 << 16), dd
->port
->mmio
+ PORT_SCR_ERR
);
726 if (unlikely(port_stat
& ~PORT_IRQ_HANDLED
)) {
727 dev_warn(&dd
->pdev
->dev
,
728 "Port stat errors %x unhandled\n",
729 (port_stat
& ~PORT_IRQ_HANDLED
));
730 if (mtip_check_surprise_removal(dd
->pdev
))
733 if (likely(port_stat
& (PORT_IRQ_TF_ERR
| PORT_IRQ_IF_ERR
))) {
734 set_bit(MTIP_PF_EH_ACTIVE_BIT
, &dd
->port
->flags
);
735 wake_up_interruptible(&dd
->port
->svc_wait
);
739 static inline irqreturn_t
mtip_handle_irq(struct driver_data
*data
)
741 struct driver_data
*dd
= (struct driver_data
*) data
;
742 struct mtip_port
*port
= dd
->port
;
743 u32 hba_stat
, port_stat
;
745 int do_irq_enable
= 1, i
, workers
;
746 struct mtip_work
*twork
;
748 hba_stat
= readl(dd
->mmio
+ HOST_IRQ_STAT
);
752 /* Acknowledge the interrupt status on the port.*/
753 port_stat
= readl(port
->mmio
+ PORT_IRQ_STAT
);
754 if (unlikely(port_stat
== 0xFFFFFFFF)) {
755 mtip_check_surprise_removal(dd
->pdev
);
758 writel(port_stat
, port
->mmio
+ PORT_IRQ_STAT
);
760 /* Demux port status */
761 if (likely(port_stat
& PORT_IRQ_SDB_FIS
)) {
763 WARN_ON_ONCE(atomic_read(&dd
->irq_workers_active
) != 0);
765 /* Start at 1: group zero is always local? */
766 for (i
= 0, workers
= 0; i
< MTIP_MAX_SLOT_GROUPS
;
768 twork
= &dd
->work
[i
];
769 twork
->completed
= readl(port
->completed
[i
]);
770 if (twork
->completed
)
774 atomic_set(&dd
->irq_workers_active
, workers
);
776 for (i
= 1; i
< MTIP_MAX_SLOT_GROUPS
; i
++) {
777 twork
= &dd
->work
[i
];
778 if (twork
->completed
)
785 if (likely(dd
->work
[0].completed
))
786 mtip_workq_sdbfx(port
, 0,
787 dd
->work
[0].completed
);
791 * Chip quirk: SDB interrupt but nothing
798 if (unlikely(port_stat
& PORT_IRQ_ERR
)) {
799 if (unlikely(mtip_check_surprise_removal(dd
->pdev
))) {
800 /* don't proceed further */
803 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
807 mtip_process_errors(dd
, port_stat
& PORT_IRQ_ERR
);
810 if (unlikely(port_stat
& PORT_IRQ_LEGACY
))
811 mtip_process_legacy(dd
, port_stat
& PORT_IRQ_LEGACY
);
814 /* acknowledge interrupt */
815 if (unlikely(do_irq_enable
))
816 writel(hba_stat
, dd
->mmio
+ HOST_IRQ_STAT
);
822 * HBA interrupt subroutine.
825 * @instance Pointer to the driver data structure.
828 * IRQ_HANDLED A HBA interrupt was pending and handled.
829 * IRQ_NONE This interrupt was not for the HBA.
831 static irqreturn_t
mtip_irq_handler(int irq
, void *instance
)
833 struct driver_data
*dd
= instance
;
835 return mtip_handle_irq(dd
);
838 static void mtip_issue_non_ncq_command(struct mtip_port
*port
, int tag
)
840 writel(1 << MTIP_TAG_BIT(tag
), port
->cmd_issue
[MTIP_TAG_INDEX(tag
)]);
843 static bool mtip_pause_ncq(struct mtip_port
*port
,
844 struct host_to_dev_fis
*fis
)
846 unsigned long task_file_data
;
848 task_file_data
= readl(port
->mmio
+PORT_TFDATA
);
849 if ((task_file_data
& 1))
852 if (fis
->command
== ATA_CMD_SEC_ERASE_PREP
) {
853 port
->ic_pause_timer
= jiffies
;
855 } else if ((fis
->command
== ATA_CMD_DOWNLOAD_MICRO
) &&
856 (fis
->features
== 0x03)) {
857 set_bit(MTIP_PF_DM_ACTIVE_BIT
, &port
->flags
);
858 port
->ic_pause_timer
= jiffies
;
860 } else if ((fis
->command
== ATA_CMD_SEC_ERASE_UNIT
) ||
861 ((fis
->command
== 0xFC) &&
862 (fis
->features
== 0x27 || fis
->features
== 0x72 ||
863 fis
->features
== 0x62 || fis
->features
== 0x26))) {
864 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
865 clear_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &port
->dd
->dd_flag
);
866 /* Com reset after secure erase or lowlevel format */
867 mtip_restart_port(port
);
868 clear_bit(MTIP_PF_SE_ACTIVE_BIT
, &port
->flags
);
875 static bool mtip_commands_active(struct mtip_port
*port
)
881 * Ignore s_active bit 0 of array element 0.
882 * This bit will always be set
884 active
= readl(port
->s_active
[0]) & 0xFFFFFFFE;
885 for (n
= 1; n
< port
->dd
->slot_groups
; n
++)
886 active
|= readl(port
->s_active
[n
]);
892 * Wait for port to quiesce
894 * @port Pointer to port data structure
895 * @timeout Max duration to wait (ms)
899 * -EBUSY Commands still active
901 static int mtip_quiesce_io(struct mtip_port
*port
, unsigned long timeout
)
906 blk_mq_quiesce_queue(port
->dd
->queue
);
908 to
= jiffies
+ msecs_to_jiffies(timeout
);
910 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
) &&
911 test_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
)) {
913 continue; /* svc thd is actively issuing commands */
918 if (mtip_check_surprise_removal(port
->dd
->pdev
))
921 active
= mtip_commands_active(port
);
924 } while (time_before(jiffies
, to
));
926 blk_mq_unquiesce_queue(port
->dd
->queue
);
927 return active
? -EBUSY
: 0;
929 blk_mq_unquiesce_queue(port
->dd
->queue
);
933 struct mtip_int_cmd
{
941 * Execute an internal command and wait for the completion.
943 * @port Pointer to the port data structure.
944 * @fis Pointer to the FIS that describes the command.
945 * @fis_len Length in WORDS of the FIS.
946 * @buffer DMA accessible for command data.
947 * @buf_len Length, in bytes, of the data buffer.
948 * @opts Command header options, excluding the FIS length
949 * and the number of PRD entries.
950 * @timeout Time in ms to wait for the command to complete.
953 * 0 Command completed successfully.
954 * -EFAULT The buffer address is not correctly aligned.
955 * -EBUSY Internal command or other IO in progress.
956 * -EAGAIN Time out waiting for command to complete.
958 static int mtip_exec_internal_command(struct mtip_port
*port
,
959 struct host_to_dev_fis
*fis
,
964 unsigned long timeout
)
966 struct mtip_cmd
*int_cmd
;
967 struct driver_data
*dd
= port
->dd
;
969 struct mtip_int_cmd icmd
= {
977 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
978 if (buffer
& 0x00000007) {
979 dev_err(&dd
->pdev
->dev
, "SG buffer is not 8 byte aligned\n");
983 if (mtip_check_surprise_removal(dd
->pdev
))
986 rq
= blk_mq_alloc_request(dd
->queue
, REQ_OP_DRV_IN
, BLK_MQ_REQ_RESERVED
);
988 dbg_printk(MTIP_DRV_NAME
"Unable to allocate tag for PIO cmd\n");
992 set_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
994 if (fis
->command
== ATA_CMD_SEC_ERASE_PREP
)
995 set_bit(MTIP_PF_SE_ACTIVE_BIT
, &port
->flags
);
997 clear_bit(MTIP_PF_DM_ACTIVE_BIT
, &port
->flags
);
999 if (fis
->command
!= ATA_CMD_STANDBYNOW1
) {
1000 /* wait for io to complete if non atomic */
1001 if (mtip_quiesce_io(port
, MTIP_QUIESCE_IO_TIMEOUT_MS
) < 0) {
1002 dev_warn(&dd
->pdev
->dev
, "Failed to quiesce IO\n");
1003 blk_mq_free_request(rq
);
1004 clear_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1005 wake_up_interruptible(&port
->svc_wait
);
1010 /* Copy the command to the command table */
1011 int_cmd
= blk_mq_rq_to_pdu(rq
);
1012 int_cmd
->icmd
= &icmd
;
1013 memcpy(int_cmd
->command
, fis
, fis_len
*4);
1015 rq
->timeout
= timeout
;
1017 /* insert request and run queue */
1018 blk_execute_rq(rq
->q
, NULL
, rq
, true);
1020 if (int_cmd
->status
) {
1021 dev_err(&dd
->pdev
->dev
, "Internal command [%02X] failed %d\n",
1022 fis
->command
, int_cmd
->status
);
1025 if (mtip_check_surprise_removal(dd
->pdev
) ||
1026 test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
1028 dev_err(&dd
->pdev
->dev
,
1029 "Internal command [%02X] wait returned due to SR\n",
1034 mtip_device_reset(dd
); /* recover from timeout issue */
1039 if (readl(port
->cmd_issue
[MTIP_TAG_INDEX(MTIP_TAG_INTERNAL
)])
1040 & (1 << MTIP_TAG_BIT(MTIP_TAG_INTERNAL
))) {
1042 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)) {
1043 mtip_device_reset(dd
);
1048 /* Clear the allocated and active bits for the internal command. */
1049 blk_mq_free_request(rq
);
1050 clear_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1051 if (rv
>= 0 && mtip_pause_ncq(port
, fis
)) {
1055 wake_up_interruptible(&port
->svc_wait
);
1061 * Byte-swap ATA ID strings.
1063 * ATA identify data contains strings in byte-swapped 16-bit words.
1064 * They must be swapped (on all architectures) to be usable as C strings.
1065 * This function swaps bytes in-place.
1067 * @buf The buffer location of the string
1068 * @len The number of bytes to swap
1073 static inline void ata_swap_string(u16
*buf
, unsigned int len
)
1076 for (i
= 0; i
< (len
/2); i
++)
1077 be16_to_cpus(&buf
[i
]);
1080 static void mtip_set_timeout(struct driver_data
*dd
,
1081 struct host_to_dev_fis
*fis
,
1082 unsigned int *timeout
, u8 erasemode
)
1084 switch (fis
->command
) {
1085 case ATA_CMD_DOWNLOAD_MICRO
:
1086 *timeout
= 120000; /* 2 minutes */
1088 case ATA_CMD_SEC_ERASE_UNIT
:
1091 *timeout
= ((*(dd
->port
->identify
+ 90) * 2) * 60000);
1093 *timeout
= ((*(dd
->port
->identify
+ 89) * 2) * 60000);
1095 case ATA_CMD_STANDBYNOW1
:
1096 *timeout
= 120000; /* 2 minutes */
1100 *timeout
= 60000; /* 60 seconds */
1103 *timeout
= 15000; /* 15 seconds */
1106 *timeout
= MTIP_IOCTL_CMD_TIMEOUT_MS
;
1112 * Request the device identity information.
1114 * If a user space buffer is not specified, i.e. is NULL, the
1115 * identify information is still read from the drive and placed
1116 * into the identify data buffer (@e port->identify) in the
1117 * port data structure.
1118 * When the identify buffer contains valid identify information @e
1119 * port->identify_valid is non-zero.
1121 * @port Pointer to the port structure.
1122 * @user_buffer A user space buffer where the identify data should be
1126 * 0 Command completed successfully.
1127 * -EFAULT An error occurred while coping data to the user buffer.
1128 * -1 Command failed.
1130 static int mtip_get_identify(struct mtip_port
*port
, void __user
*user_buffer
)
1133 struct host_to_dev_fis fis
;
1135 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
1138 /* Build the FIS. */
1139 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1142 fis
.command
= ATA_CMD_ID_ATA
;
1144 /* Set the identify information as invalid. */
1145 port
->identify_valid
= 0;
1147 /* Clear the identify information. */
1148 memset(port
->identify
, 0, sizeof(u16
) * ATA_ID_WORDS
);
1150 /* Execute the command. */
1151 if (mtip_exec_internal_command(port
,
1155 sizeof(u16
) * ATA_ID_WORDS
,
1157 MTIP_INT_CMD_TIMEOUT_MS
)
1164 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1165 * perform field-sensitive swapping on the string fields.
1166 * See the kernel use of ata_id_string() for proof of this.
1168 #ifdef __LITTLE_ENDIAN
1169 ata_swap_string(port
->identify
+ 27, 40); /* model string*/
1170 ata_swap_string(port
->identify
+ 23, 8); /* firmware string*/
1171 ata_swap_string(port
->identify
+ 10, 20); /* serial# string*/
1175 for (i
= 0; i
< ATA_ID_WORDS
; i
++)
1176 port
->identify
[i
] = le16_to_cpu(port
->identify
[i
]);
1180 /* Check security locked state */
1181 if (port
->identify
[128] & 0x4)
1182 set_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
1184 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
1186 /* Set the identify buffer as valid. */
1187 port
->identify_valid
= 1;
1193 ATA_ID_WORDS
* sizeof(u16
))) {
1204 * Issue a standby immediate command to the device.
1206 * @port Pointer to the port structure.
1209 * 0 Command was executed successfully.
1210 * -1 An error occurred while executing the command.
1212 static int mtip_standby_immediate(struct mtip_port
*port
)
1215 struct host_to_dev_fis fis
;
1216 unsigned long start
;
1217 unsigned int timeout
;
1219 /* Build the FIS. */
1220 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1223 fis
.command
= ATA_CMD_STANDBYNOW1
;
1225 mtip_set_timeout(port
->dd
, &fis
, &timeout
, 0);
1228 rv
= mtip_exec_internal_command(port
,
1235 dbg_printk(MTIP_DRV_NAME
"Time taken to complete standby cmd: %d ms\n",
1236 jiffies_to_msecs(jiffies
- start
));
1238 dev_warn(&port
->dd
->pdev
->dev
,
1239 "STANDBY IMMEDIATE command failed.\n");
1245 * Issue a READ LOG EXT command to the device.
1247 * @port pointer to the port structure.
1248 * @page page number to fetch
1249 * @buffer pointer to buffer
1250 * @buffer_dma dma address corresponding to @buffer
1251 * @sectors page length to fetch, in sectors
1254 * @rv return value from mtip_exec_internal_command()
1256 static int mtip_read_log_page(struct mtip_port
*port
, u8 page
, u16
*buffer
,
1257 dma_addr_t buffer_dma
, unsigned int sectors
)
1259 struct host_to_dev_fis fis
;
1261 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1264 fis
.command
= ATA_CMD_READ_LOG_EXT
;
1265 fis
.sect_count
= sectors
& 0xFF;
1266 fis
.sect_cnt_ex
= (sectors
>> 8) & 0xFF;
1269 fis
.device
= ATA_DEVICE_OBS
;
1271 memset(buffer
, 0, sectors
* ATA_SECT_SIZE
);
1273 return mtip_exec_internal_command(port
,
1277 sectors
* ATA_SECT_SIZE
,
1279 MTIP_INT_CMD_TIMEOUT_MS
);
1283 * Issue a SMART READ DATA command to the device.
1285 * @port pointer to the port structure.
1286 * @buffer pointer to buffer
1287 * @buffer_dma dma address corresponding to @buffer
1290 * @rv return value from mtip_exec_internal_command()
1292 static int mtip_get_smart_data(struct mtip_port
*port
, u8
*buffer
,
1293 dma_addr_t buffer_dma
)
1295 struct host_to_dev_fis fis
;
1297 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1300 fis
.command
= ATA_CMD_SMART
;
1301 fis
.features
= 0xD0;
1305 fis
.device
= ATA_DEVICE_OBS
;
1307 return mtip_exec_internal_command(port
,
1317 * Get the value of a smart attribute
1319 * @port pointer to the port structure
1320 * @id attribute number
1321 * @attrib pointer to return attrib information corresponding to @id
1324 * -EINVAL NULL buffer passed or unsupported attribute @id.
1325 * -EPERM Identify data not valid, SMART not supported or not enabled
1327 static int mtip_get_smart_attr(struct mtip_port
*port
, unsigned int id
,
1328 struct smart_attr
*attrib
)
1331 struct smart_attr
*pattr
;
1336 if (!port
->identify_valid
) {
1337 dev_warn(&port
->dd
->pdev
->dev
, "IDENTIFY DATA not valid\n");
1340 if (!(port
->identify
[82] & 0x1)) {
1341 dev_warn(&port
->dd
->pdev
->dev
, "SMART not supported\n");
1344 if (!(port
->identify
[85] & 0x1)) {
1345 dev_warn(&port
->dd
->pdev
->dev
, "SMART not enabled\n");
1349 memset(port
->smart_buf
, 0, ATA_SECT_SIZE
);
1350 rv
= mtip_get_smart_data(port
, port
->smart_buf
, port
->smart_buf_dma
);
1352 dev_warn(&port
->dd
->pdev
->dev
, "Failed to ge SMART data\n");
1356 pattr
= (struct smart_attr
*)(port
->smart_buf
+ 2);
1357 for (i
= 0; i
< 29; i
++, pattr
++)
1358 if (pattr
->attr_id
== id
) {
1359 memcpy(attrib
, pattr
, sizeof(struct smart_attr
));
1364 dev_warn(&port
->dd
->pdev
->dev
,
1365 "Query for invalid SMART attribute ID\n");
1373 * Get the drive capacity.
1375 * @dd Pointer to the device data structure.
1376 * @sectors Pointer to the variable that will receive the sector count.
1379 * 1 Capacity was returned successfully.
1380 * 0 The identify information is invalid.
1382 static bool mtip_hw_get_capacity(struct driver_data
*dd
, sector_t
*sectors
)
1384 struct mtip_port
*port
= dd
->port
;
1385 u64 total
, raw0
, raw1
, raw2
, raw3
;
1386 raw0
= port
->identify
[100];
1387 raw1
= port
->identify
[101];
1388 raw2
= port
->identify
[102];
1389 raw3
= port
->identify
[103];
1390 total
= raw0
| raw1
<<16 | raw2
<<32 | raw3
<<48;
1392 return (bool) !!port
->identify_valid
;
1396 * Display the identify command data.
1398 * @port Pointer to the port data structure.
1403 static void mtip_dump_identify(struct mtip_port
*port
)
1406 unsigned short revid
;
1409 if (!port
->identify_valid
)
1412 strlcpy(cbuf
, (char *)(port
->identify
+10), 21);
1413 dev_info(&port
->dd
->pdev
->dev
,
1414 "Serial No.: %s\n", cbuf
);
1416 strlcpy(cbuf
, (char *)(port
->identify
+23), 9);
1417 dev_info(&port
->dd
->pdev
->dev
,
1418 "Firmware Ver.: %s\n", cbuf
);
1420 strlcpy(cbuf
, (char *)(port
->identify
+27), 41);
1421 dev_info(&port
->dd
->pdev
->dev
, "Model: %s\n", cbuf
);
1423 dev_info(&port
->dd
->pdev
->dev
, "Security: %04x %s\n",
1424 port
->identify
[128],
1425 port
->identify
[128] & 0x4 ? "(LOCKED)" : "");
1427 if (mtip_hw_get_capacity(port
->dd
, §ors
))
1428 dev_info(&port
->dd
->pdev
->dev
,
1429 "Capacity: %llu sectors (%llu MB)\n",
1431 ((u64
)sectors
) * ATA_SECT_SIZE
>> 20);
1433 pci_read_config_word(port
->dd
->pdev
, PCI_REVISION_ID
, &revid
);
1434 switch (revid
& 0xFF) {
1436 strlcpy(cbuf
, "A0", 3);
1439 strlcpy(cbuf
, "A2", 3);
1442 strlcpy(cbuf
, "?", 2);
1445 dev_info(&port
->dd
->pdev
->dev
,
1446 "Card Type: %s\n", cbuf
);
1450 * Map the commands scatter list into the command table.
1452 * @command Pointer to the command.
1453 * @nents Number of scatter list entries.
1458 static inline void fill_command_sg(struct driver_data
*dd
,
1459 struct mtip_cmd
*command
,
1463 unsigned int dma_len
;
1464 struct mtip_cmd_sg
*command_sg
;
1465 struct scatterlist
*sg
;
1467 command_sg
= command
->command
+ AHCI_CMD_TBL_HDR_SZ
;
1469 for_each_sg(command
->sg
, sg
, nents
, n
) {
1470 dma_len
= sg_dma_len(sg
);
1471 if (dma_len
> 0x400000)
1472 dev_err(&dd
->pdev
->dev
,
1473 "DMA segment length truncated\n");
1474 command_sg
->info
= cpu_to_le32((dma_len
-1) & 0x3FFFFF);
1475 command_sg
->dba
= cpu_to_le32(sg_dma_address(sg
));
1476 command_sg
->dba_upper
=
1477 cpu_to_le32((sg_dma_address(sg
) >> 16) >> 16);
1483 * @brief Execute a drive command.
1485 * return value 0 The command completed successfully.
1486 * return value -1 An error occurred while executing the command.
1488 static int exec_drive_task(struct mtip_port
*port
, u8
*command
)
1490 struct host_to_dev_fis fis
;
1491 struct host_to_dev_fis
*reply
= (port
->rxfis
+ RX_FIS_D2H_REG
);
1494 /* Build the FIS. */
1495 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1498 fis
.command
= command
[0];
1499 fis
.features
= command
[1];
1500 fis
.sect_count
= command
[2];
1501 fis
.sector
= command
[3];
1502 fis
.cyl_low
= command
[4];
1503 fis
.cyl_hi
= command
[5];
1504 fis
.device
= command
[6] & ~0x10; /* Clear the dev bit*/
1506 mtip_set_timeout(port
->dd
, &fis
, &to
, 0);
1508 dbg_printk(MTIP_DRV_NAME
" %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
1518 /* Execute the command. */
1519 if (mtip_exec_internal_command(port
,
1529 command
[0] = reply
->command
; /* Status*/
1530 command
[1] = reply
->features
; /* Error*/
1531 command
[4] = reply
->cyl_low
;
1532 command
[5] = reply
->cyl_hi
;
1534 dbg_printk(MTIP_DRV_NAME
" %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
1545 * @brief Execute a drive command.
1547 * @param port Pointer to the port data structure.
1548 * @param command Pointer to the user specified command parameters.
1549 * @param user_buffer Pointer to the user space buffer where read sector
1550 * data should be copied.
1552 * return value 0 The command completed successfully.
1553 * return value -EFAULT An error occurred while copying the completion
1554 * data to the user space buffer.
1555 * return value -1 An error occurred while executing the command.
1557 static int exec_drive_command(struct mtip_port
*port
, u8
*command
,
1558 void __user
*user_buffer
)
1560 struct host_to_dev_fis fis
;
1561 struct host_to_dev_fis
*reply
;
1563 dma_addr_t dma_addr
= 0;
1564 int rv
= 0, xfer_sz
= command
[3];
1571 buf
= dma_alloc_coherent(&port
->dd
->pdev
->dev
,
1572 ATA_SECT_SIZE
* xfer_sz
,
1576 dev_err(&port
->dd
->pdev
->dev
,
1577 "Memory allocation failed (%d bytes)\n",
1578 ATA_SECT_SIZE
* xfer_sz
);
1583 /* Build the FIS. */
1584 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1587 fis
.command
= command
[0];
1588 fis
.features
= command
[2];
1589 fis
.sect_count
= command
[3];
1590 if (fis
.command
== ATA_CMD_SMART
) {
1591 fis
.sector
= command
[1];
1596 mtip_set_timeout(port
->dd
, &fis
, &to
, 0);
1599 reply
= (port
->rxfis
+ RX_FIS_PIO_SETUP
);
1601 reply
= (port
->rxfis
+ RX_FIS_D2H_REG
);
1603 dbg_printk(MTIP_DRV_NAME
1604 " %s: User Command: cmd %x, sect %x, "
1605 "feat %x, sectcnt %x\n",
1612 /* Execute the command. */
1613 if (mtip_exec_internal_command(port
,
1616 (xfer_sz
? dma_addr
: 0),
1617 (xfer_sz
? ATA_SECT_SIZE
* xfer_sz
: 0),
1622 goto exit_drive_command
;
1625 /* Collect the completion status. */
1626 command
[0] = reply
->command
; /* Status*/
1627 command
[1] = reply
->features
; /* Error*/
1628 command
[2] = reply
->sect_count
;
1630 dbg_printk(MTIP_DRV_NAME
1631 " %s: Completion Status: stat %x, "
1632 "err %x, nsect %x\n",
1639 if (copy_to_user(user_buffer
,
1641 ATA_SECT_SIZE
* command
[3])) {
1643 goto exit_drive_command
;
1648 dma_free_coherent(&port
->dd
->pdev
->dev
,
1649 ATA_SECT_SIZE
* xfer_sz
, buf
, dma_addr
);
1654 * Indicates whether a command has a single sector payload.
1656 * @command passed to the device to perform the certain event.
1657 * @features passed to the device to perform the certain event.
1660 * 1 command is one that always has a single sector payload,
1661 * regardless of the value in the Sector Count field.
1665 static unsigned int implicit_sector(unsigned char command
,
1666 unsigned char features
)
1668 unsigned int rv
= 0;
1670 /* list of commands that have an implicit sector count of 1 */
1672 case ATA_CMD_SEC_SET_PASS
:
1673 case ATA_CMD_SEC_UNLOCK
:
1674 case ATA_CMD_SEC_ERASE_PREP
:
1675 case ATA_CMD_SEC_ERASE_UNIT
:
1676 case ATA_CMD_SEC_FREEZE_LOCK
:
1677 case ATA_CMD_SEC_DISABLE_PASS
:
1678 case ATA_CMD_PMP_READ
:
1679 case ATA_CMD_PMP_WRITE
:
1682 case ATA_CMD_SET_MAX
:
1683 if (features
== ATA_SET_MAX_UNLOCK
)
1687 if ((features
== ATA_SMART_READ_VALUES
) ||
1688 (features
== ATA_SMART_READ_THRESHOLDS
))
1691 case ATA_CMD_CONF_OVERLAY
:
1692 if ((features
== ATA_DCO_IDENTIFY
) ||
1693 (features
== ATA_DCO_SET
))
1701 * Executes a taskfile
1702 * See ide_taskfile_ioctl() for derivation
1704 static int exec_drive_taskfile(struct driver_data
*dd
,
1706 ide_task_request_t
*req_task
,
1709 struct host_to_dev_fis fis
;
1710 struct host_to_dev_fis
*reply
;
1713 dma_addr_t outbuf_dma
= 0;
1714 dma_addr_t inbuf_dma
= 0;
1715 dma_addr_t dma_buffer
= 0;
1717 unsigned int taskin
= 0;
1718 unsigned int taskout
= 0;
1720 unsigned int timeout
;
1721 unsigned int force_single_sector
;
1722 unsigned int transfer_size
;
1723 unsigned long task_file_data
;
1724 int intotal
= outtotal
+ req_task
->out_size
;
1727 taskout
= req_task
->out_size
;
1728 taskin
= req_task
->in_size
;
1729 /* 130560 = 512 * 0xFF*/
1730 if (taskin
> 130560 || taskout
> 130560)
1734 outbuf
= memdup_user(buf
+ outtotal
, taskout
);
1736 return PTR_ERR(outbuf
);
1738 outbuf_dma
= dma_map_single(&dd
->pdev
->dev
, outbuf
,
1739 taskout
, DMA_TO_DEVICE
);
1740 if (dma_mapping_error(&dd
->pdev
->dev
, outbuf_dma
)) {
1744 dma_buffer
= outbuf_dma
;
1748 inbuf
= memdup_user(buf
+ intotal
, taskin
);
1749 if (IS_ERR(inbuf
)) {
1750 err
= PTR_ERR(inbuf
);
1754 inbuf_dma
= dma_map_single(&dd
->pdev
->dev
, inbuf
,
1755 taskin
, DMA_FROM_DEVICE
);
1756 if (dma_mapping_error(&dd
->pdev
->dev
, inbuf_dma
)) {
1760 dma_buffer
= inbuf_dma
;
1763 /* only supports PIO and non-data commands from this ioctl. */
1764 switch (req_task
->data_phase
) {
1766 nsect
= taskout
/ ATA_SECT_SIZE
;
1767 reply
= (dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
);
1770 reply
= (dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
);
1772 case TASKFILE_NO_DATA
:
1773 reply
= (dd
->port
->rxfis
+ RX_FIS_D2H_REG
);
1780 /* Build the FIS. */
1781 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1785 fis
.command
= req_task
->io_ports
[7];
1786 fis
.features
= req_task
->io_ports
[1];
1787 fis
.sect_count
= req_task
->io_ports
[2];
1788 fis
.lba_low
= req_task
->io_ports
[3];
1789 fis
.lba_mid
= req_task
->io_ports
[4];
1790 fis
.lba_hi
= req_task
->io_ports
[5];
1791 /* Clear the dev bit*/
1792 fis
.device
= req_task
->io_ports
[6] & ~0x10;
1794 if ((req_task
->in_flags
.all
== 0) && (req_task
->out_flags
.all
& 1)) {
1795 req_task
->in_flags
.all
=
1796 IDE_TASKFILE_STD_IN_FLAGS
|
1797 (IDE_HOB_STD_IN_FLAGS
<< 8);
1798 fis
.lba_low_ex
= req_task
->hob_ports
[3];
1799 fis
.lba_mid_ex
= req_task
->hob_ports
[4];
1800 fis
.lba_hi_ex
= req_task
->hob_ports
[5];
1801 fis
.features_ex
= req_task
->hob_ports
[1];
1802 fis
.sect_cnt_ex
= req_task
->hob_ports
[2];
1805 req_task
->in_flags
.all
= IDE_TASKFILE_STD_IN_FLAGS
;
1808 force_single_sector
= implicit_sector(fis
.command
, fis
.features
);
1810 if ((taskin
|| taskout
) && (!fis
.sect_count
)) {
1812 fis
.sect_count
= nsect
;
1814 if (!force_single_sector
) {
1815 dev_warn(&dd
->pdev
->dev
,
1816 "data movement but "
1817 "sect_count is 0\n");
1824 dbg_printk(MTIP_DRV_NAME
1825 " %s: cmd %x, feat %x, nsect %x,"
1826 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
1837 /* check for erase mode support during secure erase.*/
1838 if ((fis
.command
== ATA_CMD_SEC_ERASE_UNIT
) && outbuf
&&
1839 (outbuf
[0] & MTIP_SEC_ERASE_MODE
)) {
1843 mtip_set_timeout(dd
, &fis
, &timeout
, erasemode
);
1845 /* Determine the correct transfer size.*/
1846 if (force_single_sector
)
1847 transfer_size
= ATA_SECT_SIZE
;
1849 transfer_size
= ATA_SECT_SIZE
* fis
.sect_count
;
1851 /* Execute the command.*/
1852 if (mtip_exec_internal_command(dd
->port
,
1863 task_file_data
= readl(dd
->port
->mmio
+PORT_TFDATA
);
1865 if ((req_task
->data_phase
== TASKFILE_IN
) && !(task_file_data
& 1)) {
1866 reply
= dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
;
1867 req_task
->io_ports
[7] = reply
->control
;
1869 reply
= dd
->port
->rxfis
+ RX_FIS_D2H_REG
;
1870 req_task
->io_ports
[7] = reply
->command
;
1873 /* reclaim the DMA buffers.*/
1875 dma_unmap_single(&dd
->pdev
->dev
, inbuf_dma
, taskin
,
1878 dma_unmap_single(&dd
->pdev
->dev
, outbuf_dma
, taskout
,
1883 /* return the ATA registers to the caller.*/
1884 req_task
->io_ports
[1] = reply
->features
;
1885 req_task
->io_ports
[2] = reply
->sect_count
;
1886 req_task
->io_ports
[3] = reply
->lba_low
;
1887 req_task
->io_ports
[4] = reply
->lba_mid
;
1888 req_task
->io_ports
[5] = reply
->lba_hi
;
1889 req_task
->io_ports
[6] = reply
->device
;
1891 if (req_task
->out_flags
.all
& 1) {
1893 req_task
->hob_ports
[3] = reply
->lba_low_ex
;
1894 req_task
->hob_ports
[4] = reply
->lba_mid_ex
;
1895 req_task
->hob_ports
[5] = reply
->lba_hi_ex
;
1896 req_task
->hob_ports
[1] = reply
->features_ex
;
1897 req_task
->hob_ports
[2] = reply
->sect_cnt_ex
;
1899 dbg_printk(MTIP_DRV_NAME
1900 " %s: Completion: stat %x,"
1901 "err %x, sect_cnt %x, lbalo %x,"
1902 "lbamid %x, lbahi %x, dev %x\n",
1904 req_task
->io_ports
[7],
1905 req_task
->io_ports
[1],
1906 req_task
->io_ports
[2],
1907 req_task
->io_ports
[3],
1908 req_task
->io_ports
[4],
1909 req_task
->io_ports
[5],
1910 req_task
->io_ports
[6]);
1913 if (copy_to_user(buf
+ outtotal
, outbuf
, taskout
)) {
1919 if (copy_to_user(buf
+ intotal
, inbuf
, taskin
)) {
1926 dma_unmap_single(&dd
->pdev
->dev
, inbuf_dma
, taskin
,
1929 dma_unmap_single(&dd
->pdev
->dev
, outbuf_dma
, taskout
,
1938 * Handle IOCTL calls from the Block Layer.
1940 * This function is called by the Block Layer when it receives an IOCTL
1941 * command that it does not understand. If the IOCTL command is not supported
1942 * this function returns -ENOTTY.
1944 * @dd Pointer to the driver data structure.
1945 * @cmd IOCTL command passed from the Block Layer.
1946 * @arg IOCTL argument passed from the Block Layer.
1949 * 0 The IOCTL completed successfully.
1950 * -ENOTTY The specified command is not supported.
1951 * -EFAULT An error occurred copying data to a user space buffer.
1952 * -EIO An error occurred while executing the command.
1954 static int mtip_hw_ioctl(struct driver_data
*dd
, unsigned int cmd
,
1958 case HDIO_GET_IDENTITY
:
1960 if (copy_to_user((void __user
*)arg
, dd
->port
->identify
,
1961 sizeof(u16
) * ATA_ID_WORDS
))
1965 case HDIO_DRIVE_CMD
:
1967 u8 drive_command
[4];
1969 /* Copy the user command info to our buffer. */
1970 if (copy_from_user(drive_command
,
1971 (void __user
*) arg
,
1972 sizeof(drive_command
)))
1975 /* Execute the drive command. */
1976 if (exec_drive_command(dd
->port
,
1978 (void __user
*) (arg
+4)))
1981 /* Copy the status back to the users buffer. */
1982 if (copy_to_user((void __user
*) arg
,
1984 sizeof(drive_command
)))
1989 case HDIO_DRIVE_TASK
:
1991 u8 drive_command
[7];
1993 /* Copy the user command info to our buffer. */
1994 if (copy_from_user(drive_command
,
1995 (void __user
*) arg
,
1996 sizeof(drive_command
)))
1999 /* Execute the drive command. */
2000 if (exec_drive_task(dd
->port
, drive_command
))
2003 /* Copy the status back to the users buffer. */
2004 if (copy_to_user((void __user
*) arg
,
2006 sizeof(drive_command
)))
2011 case HDIO_DRIVE_TASKFILE
: {
2012 ide_task_request_t req_task
;
2015 if (copy_from_user(&req_task
, (void __user
*) arg
,
2019 outtotal
= sizeof(req_task
);
2021 ret
= exec_drive_taskfile(dd
, (void __user
*) arg
,
2022 &req_task
, outtotal
);
2024 if (copy_to_user((void __user
*) arg
, &req_task
,
2038 * Submit an IO to the hw
2040 * This function is called by the block layer to issue an io
2041 * to the device. Upon completion, the callback function will
2042 * be called with the data parameter passed as the callback data.
2044 * @dd Pointer to the driver data structure.
2045 * @start First sector to read.
2046 * @nsect Number of sectors to read.
2047 * @tag The tag of this read command.
2048 * @callback Pointer to the function that should be called
2049 * when the read completes.
2050 * @data Callback data passed to the callback function
2051 * when the read completes.
2052 * @dir Direction (read or write)
2057 static void mtip_hw_submit_io(struct driver_data
*dd
, struct request
*rq
,
2058 struct mtip_cmd
*command
,
2059 struct blk_mq_hw_ctx
*hctx
)
2061 struct mtip_cmd_hdr
*hdr
=
2062 dd
->port
->command_list
+ sizeof(struct mtip_cmd_hdr
) * rq
->tag
;
2063 struct host_to_dev_fis
*fis
;
2064 struct mtip_port
*port
= dd
->port
;
2065 int dma_dir
= rq_data_dir(rq
) == READ
? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
2066 u64 start
= blk_rq_pos(rq
);
2067 unsigned int nsect
= blk_rq_sectors(rq
);
2070 /* Map the scatter list for DMA access */
2071 nents
= blk_rq_map_sg(hctx
->queue
, rq
, command
->sg
);
2072 nents
= dma_map_sg(&dd
->pdev
->dev
, command
->sg
, nents
, dma_dir
);
2074 prefetch(&port
->flags
);
2076 command
->scatter_ents
= nents
;
2079 * The number of retries for this command before it is
2080 * reported as a failure to the upper layers.
2082 command
->retries
= MTIP_MAX_RETRIES
;
2085 fis
= command
->command
;
2088 if (dma_dir
== DMA_FROM_DEVICE
)
2089 fis
->command
= ATA_CMD_FPDMA_READ
;
2091 fis
->command
= ATA_CMD_FPDMA_WRITE
;
2092 fis
->lba_low
= start
& 0xFF;
2093 fis
->lba_mid
= (start
>> 8) & 0xFF;
2094 fis
->lba_hi
= (start
>> 16) & 0xFF;
2095 fis
->lba_low_ex
= (start
>> 24) & 0xFF;
2096 fis
->lba_mid_ex
= (start
>> 32) & 0xFF;
2097 fis
->lba_hi_ex
= (start
>> 40) & 0xFF;
2098 fis
->device
= 1 << 6;
2099 fis
->features
= nsect
& 0xFF;
2100 fis
->features_ex
= (nsect
>> 8) & 0xFF;
2101 fis
->sect_count
= ((rq
->tag
<< 3) | (rq
->tag
>> 5));
2102 fis
->sect_cnt_ex
= 0;
2106 fill_command_sg(dd
, command
, nents
);
2108 if (unlikely(command
->unaligned
))
2109 fis
->device
|= 1 << 7;
2111 /* Populate the command header */
2112 hdr
->ctba
= cpu_to_le32(command
->command_dma
& 0xFFFFFFFF);
2113 if (test_bit(MTIP_PF_HOST_CAP_64
, &dd
->port
->flags
))
2114 hdr
->ctbau
= cpu_to_le32((command
->command_dma
>> 16) >> 16);
2115 hdr
->opts
= cpu_to_le32((nents
<< 16) | 5 | AHCI_CMD_PREFETCH
);
2116 hdr
->byte_count
= 0;
2118 command
->direction
= dma_dir
;
2121 * To prevent this command from being issued
2122 * if an internal command is in progress or error handling is active.
2124 if (unlikely(port
->flags
& MTIP_PF_PAUSE_IO
)) {
2125 set_bit(rq
->tag
, port
->cmds_to_issue
);
2126 set_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
);
2130 /* Issue the command to the hardware */
2131 mtip_issue_ncq_command(port
, rq
->tag
);
2135 * Sysfs status dump.
2137 * @dev Pointer to the device structure, passed by the kernrel.
2138 * @attr Pointer to the device_attribute structure passed by the kernel.
2139 * @buf Pointer to the char buffer that will receive the stats info.
2142 * The size, in bytes, of the data copied into buf.
2144 static ssize_t
mtip_hw_show_status(struct device
*dev
,
2145 struct device_attribute
*attr
,
2148 struct driver_data
*dd
= dev_to_disk(dev
)->private_data
;
2151 if (test_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
))
2152 size
+= sprintf(buf
, "%s", "thermal_shutdown\n");
2153 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
))
2154 size
+= sprintf(buf
, "%s", "write_protect\n");
2156 size
+= sprintf(buf
, "%s", "online\n");
2161 static DEVICE_ATTR(status
, 0444, mtip_hw_show_status
, NULL
);
2163 /* debugsfs entries */
2165 static ssize_t
show_device_status(struct device_driver
*drv
, char *buf
)
2168 struct driver_data
*dd
, *tmp
;
2169 unsigned long flags
;
2173 spin_lock_irqsave(&dev_lock
, flags
);
2174 size
+= sprintf(&buf
[size
], "Devices Present:\n");
2175 list_for_each_entry_safe(dd
, tmp
, &online_list
, online_list
) {
2178 dd
->port
->identify
&&
2179 dd
->port
->identify_valid
) {
2181 (char *) (dd
->port
->identify
+ 10), 21);
2182 status
= *(dd
->port
->identify
+ 141);
2184 memset(id_buf
, 0, 42);
2189 test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
)) {
2190 size
+= sprintf(&buf
[size
],
2191 " device %s %s (ftl rebuild %d %%)\n",
2192 dev_name(&dd
->pdev
->dev
),
2196 size
+= sprintf(&buf
[size
],
2198 dev_name(&dd
->pdev
->dev
),
2204 size
+= sprintf(&buf
[size
], "Devices Being Removed:\n");
2205 list_for_each_entry_safe(dd
, tmp
, &removing_list
, remove_list
) {
2208 dd
->port
->identify
&&
2209 dd
->port
->identify_valid
) {
2211 (char *) (dd
->port
->identify
+10), 21);
2212 status
= *(dd
->port
->identify
+ 141);
2214 memset(id_buf
, 0, 42);
2219 test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
)) {
2220 size
+= sprintf(&buf
[size
],
2221 " device %s %s (ftl rebuild %d %%)\n",
2222 dev_name(&dd
->pdev
->dev
),
2226 size
+= sprintf(&buf
[size
],
2228 dev_name(&dd
->pdev
->dev
),
2233 spin_unlock_irqrestore(&dev_lock
, flags
);
2238 static ssize_t
mtip_hw_read_device_status(struct file
*f
, char __user
*ubuf
,
2239 size_t len
, loff_t
*offset
)
2241 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2246 if (!len
|| *offset
)
2249 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2251 dev_err(&dd
->pdev
->dev
,
2252 "Memory allocation: status buffer\n");
2256 size
+= show_device_status(NULL
, buf
);
2258 *offset
= size
<= len
? size
: len
;
2259 size
= copy_to_user(ubuf
, buf
, *offset
);
2264 return rv
? rv
: *offset
;
2267 static ssize_t
mtip_hw_read_registers(struct file
*f
, char __user
*ubuf
,
2268 size_t len
, loff_t
*offset
)
2270 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2272 u32 group_allocated
;
2279 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2281 dev_err(&dd
->pdev
->dev
,
2282 "Memory allocation: register buffer\n");
2286 size
+= sprintf(&buf
[size
], "H/ S ACTive : [ 0x");
2288 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2289 size
+= sprintf(&buf
[size
], "%08X ",
2290 readl(dd
->port
->s_active
[n
]));
2292 size
+= sprintf(&buf
[size
], "]\n");
2293 size
+= sprintf(&buf
[size
], "H/ Command Issue : [ 0x");
2295 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2296 size
+= sprintf(&buf
[size
], "%08X ",
2297 readl(dd
->port
->cmd_issue
[n
]));
2299 size
+= sprintf(&buf
[size
], "]\n");
2300 size
+= sprintf(&buf
[size
], "H/ Completed : [ 0x");
2302 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2303 size
+= sprintf(&buf
[size
], "%08X ",
2304 readl(dd
->port
->completed
[n
]));
2306 size
+= sprintf(&buf
[size
], "]\n");
2307 size
+= sprintf(&buf
[size
], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2308 readl(dd
->port
->mmio
+ PORT_IRQ_STAT
));
2309 size
+= sprintf(&buf
[size
], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2310 readl(dd
->mmio
+ HOST_IRQ_STAT
));
2311 size
+= sprintf(&buf
[size
], "\n");
2313 size
+= sprintf(&buf
[size
], "L/ Commands in Q : [ 0x");
2315 for (n
= dd
->slot_groups
-1; n
>= 0; n
--) {
2316 if (sizeof(long) > sizeof(u32
))
2318 dd
->port
->cmds_to_issue
[n
/2] >> (32*(n
&1));
2320 group_allocated
= dd
->port
->cmds_to_issue
[n
];
2321 size
+= sprintf(&buf
[size
], "%08X ", group_allocated
);
2323 size
+= sprintf(&buf
[size
], "]\n");
2325 *offset
= size
<= len
? size
: len
;
2326 size
= copy_to_user(ubuf
, buf
, *offset
);
2331 return rv
? rv
: *offset
;
2334 static ssize_t
mtip_hw_read_flags(struct file
*f
, char __user
*ubuf
,
2335 size_t len
, loff_t
*offset
)
2337 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2345 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2347 dev_err(&dd
->pdev
->dev
,
2348 "Memory allocation: flag buffer\n");
2352 size
+= sprintf(&buf
[size
], "Flag-port : [ %08lX ]\n",
2354 size
+= sprintf(&buf
[size
], "Flag-dd : [ %08lX ]\n",
2357 *offset
= size
<= len
? size
: len
;
2358 size
= copy_to_user(ubuf
, buf
, *offset
);
2363 return rv
? rv
: *offset
;
2366 static const struct file_operations mtip_device_status_fops
= {
2367 .owner
= THIS_MODULE
,
2368 .open
= simple_open
,
2369 .read
= mtip_hw_read_device_status
,
2370 .llseek
= no_llseek
,
2373 static const struct file_operations mtip_regs_fops
= {
2374 .owner
= THIS_MODULE
,
2375 .open
= simple_open
,
2376 .read
= mtip_hw_read_registers
,
2377 .llseek
= no_llseek
,
2380 static const struct file_operations mtip_flags_fops
= {
2381 .owner
= THIS_MODULE
,
2382 .open
= simple_open
,
2383 .read
= mtip_hw_read_flags
,
2384 .llseek
= no_llseek
,
2388 * Create the sysfs related attributes.
2390 * @dd Pointer to the driver data structure.
2391 * @kobj Pointer to the kobj for the block device.
2394 * 0 Operation completed successfully.
2395 * -EINVAL Invalid parameter.
2397 static int mtip_hw_sysfs_init(struct driver_data
*dd
, struct kobject
*kobj
)
2402 if (sysfs_create_file(kobj
, &dev_attr_status
.attr
))
2403 dev_warn(&dd
->pdev
->dev
,
2404 "Error creating 'status' sysfs entry\n");
2409 * Remove the sysfs related attributes.
2411 * @dd Pointer to the driver data structure.
2412 * @kobj Pointer to the kobj for the block device.
2415 * 0 Operation completed successfully.
2416 * -EINVAL Invalid parameter.
2418 static int mtip_hw_sysfs_exit(struct driver_data
*dd
, struct kobject
*kobj
)
2423 sysfs_remove_file(kobj
, &dev_attr_status
.attr
);
2428 static int mtip_hw_debugfs_init(struct driver_data
*dd
)
2433 dd
->dfs_node
= debugfs_create_dir(dd
->disk
->disk_name
, dfs_parent
);
2434 if (IS_ERR_OR_NULL(dd
->dfs_node
)) {
2435 dev_warn(&dd
->pdev
->dev
,
2436 "Error creating node %s under debugfs\n",
2437 dd
->disk
->disk_name
);
2438 dd
->dfs_node
= NULL
;
2442 debugfs_create_file("flags", 0444, dd
->dfs_node
, dd
, &mtip_flags_fops
);
2443 debugfs_create_file("registers", 0444, dd
->dfs_node
, dd
,
2449 static void mtip_hw_debugfs_exit(struct driver_data
*dd
)
2451 debugfs_remove_recursive(dd
->dfs_node
);
2455 * Perform any init/resume time hardware setup
2457 * @dd Pointer to the driver data structure.
2462 static inline void hba_setup(struct driver_data
*dd
)
2465 hwdata
= readl(dd
->mmio
+ HOST_HSORG
);
2467 /* interrupt bug workaround: use only 1 IS bit.*/
2469 HSORG_DISABLE_SLOTGRP_INTR
|
2470 HSORG_DISABLE_SLOTGRP_PXIS
,
2471 dd
->mmio
+ HOST_HSORG
);
2474 static int mtip_device_unaligned_constrained(struct driver_data
*dd
)
2476 return (dd
->pdev
->device
== P420M_DEVICE_ID
? 1 : 0);
2480 * Detect the details of the product, and store anything needed
2481 * into the driver data structure. This includes product type and
2482 * version and number of slot groups.
2484 * @dd Pointer to the driver data structure.
2489 static void mtip_detect_product(struct driver_data
*dd
)
2492 unsigned int rev
, slotgroups
;
2495 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2497 * [15:8] hardware/software interface rev#
2498 * [ 3] asic-style interface
2499 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2501 hwdata
= readl(dd
->mmio
+ HOST_HSORG
);
2503 dd
->product_type
= MTIP_PRODUCT_UNKNOWN
;
2504 dd
->slot_groups
= 1;
2507 dd
->product_type
= MTIP_PRODUCT_ASICFPGA
;
2508 rev
= (hwdata
& HSORG_HWREV
) >> 8;
2509 slotgroups
= (hwdata
& HSORG_SLOTGROUPS
) + 1;
2510 dev_info(&dd
->pdev
->dev
,
2511 "ASIC-FPGA design, HS rev 0x%x, "
2512 "%i slot groups [%i slots]\n",
2517 if (slotgroups
> MTIP_MAX_SLOT_GROUPS
) {
2518 dev_warn(&dd
->pdev
->dev
,
2519 "Warning: driver only supports "
2520 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS
);
2521 slotgroups
= MTIP_MAX_SLOT_GROUPS
;
2523 dd
->slot_groups
= slotgroups
;
2527 dev_warn(&dd
->pdev
->dev
, "Unrecognized product id\n");
2531 * Blocking wait for FTL rebuild to complete
2533 * @dd Pointer to the DRIVER_DATA structure.
2536 * 0 FTL rebuild completed successfully
2537 * -EFAULT FTL rebuild error/timeout/interruption
2539 static int mtip_ftl_rebuild_poll(struct driver_data
*dd
)
2541 unsigned long timeout
, cnt
= 0, start
;
2543 dev_warn(&dd
->pdev
->dev
,
2544 "FTL rebuild in progress. Polling for completion.\n");
2547 timeout
= jiffies
+ msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS
);
2550 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
2553 if (mtip_check_surprise_removal(dd
->pdev
))
2556 if (mtip_get_identify(dd
->port
, NULL
) < 0)
2559 if (*(dd
->port
->identify
+ MTIP_FTL_REBUILD_OFFSET
) ==
2560 MTIP_FTL_REBUILD_MAGIC
) {
2562 /* Print message every 3 minutes */
2564 dev_warn(&dd
->pdev
->dev
,
2565 "FTL rebuild in progress (%d secs).\n",
2566 jiffies_to_msecs(jiffies
- start
) / 1000);
2570 dev_warn(&dd
->pdev
->dev
,
2571 "FTL rebuild complete (%d secs).\n",
2572 jiffies_to_msecs(jiffies
- start
) / 1000);
2573 mtip_block_initialize(dd
);
2576 } while (time_before(jiffies
, timeout
));
2578 /* Check for timeout */
2579 dev_err(&dd
->pdev
->dev
,
2580 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2581 jiffies_to_msecs(jiffies
- start
) / 1000);
2585 static void mtip_softirq_done_fn(struct request
*rq
)
2587 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
2588 struct driver_data
*dd
= rq
->q
->queuedata
;
2590 /* Unmap the DMA scatter list entries */
2591 dma_unmap_sg(&dd
->pdev
->dev
, cmd
->sg
, cmd
->scatter_ents
,
2594 if (unlikely(cmd
->unaligned
))
2595 atomic_inc(&dd
->port
->cmd_slot_unal
);
2597 blk_mq_end_request(rq
, cmd
->status
);
2600 static bool mtip_abort_cmd(struct request
*req
, void *data
, bool reserved
)
2602 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
2603 struct driver_data
*dd
= data
;
2605 dbg_printk(MTIP_DRV_NAME
" Aborting request, tag = %d\n", req
->tag
);
2607 clear_bit(req
->tag
, dd
->port
->cmds_to_issue
);
2608 cmd
->status
= BLK_STS_IOERR
;
2609 mtip_softirq_done_fn(req
);
2613 static bool mtip_queue_cmd(struct request
*req
, void *data
, bool reserved
)
2615 struct driver_data
*dd
= data
;
2617 set_bit(req
->tag
, dd
->port
->cmds_to_issue
);
2618 blk_abort_request(req
);
2623 * service thread to issue queued commands
2625 * @data Pointer to the driver data structure.
2631 static int mtip_service_thread(void *data
)
2633 struct driver_data
*dd
= (struct driver_data
*)data
;
2634 unsigned long slot
, slot_start
, slot_wrap
, to
;
2635 unsigned int num_cmd_slots
= dd
->slot_groups
* 32;
2636 struct mtip_port
*port
= dd
->port
;
2639 if (kthread_should_stop() ||
2640 test_bit(MTIP_PF_SVC_THD_STOP_BIT
, &port
->flags
))
2642 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
);
2645 * the condition is to check neither an internal command is
2646 * is in progress nor error handling is active
2648 wait_event_interruptible(port
->svc_wait
, (port
->flags
) &&
2649 (port
->flags
& MTIP_PF_SVC_THD_WORK
));
2651 if (kthread_should_stop() ||
2652 test_bit(MTIP_PF_SVC_THD_STOP_BIT
, &port
->flags
))
2655 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
2659 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
);
2662 /* Demux bits: start with error handling */
2663 if (test_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
)) {
2664 mtip_handle_tfe(dd
);
2665 clear_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
);
2668 if (test_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
))
2671 if (test_bit(MTIP_PF_TO_ACTIVE_BIT
, &port
->flags
)) {
2672 to
= jiffies
+ msecs_to_jiffies(5000);
2676 } while (atomic_read(&dd
->irq_workers_active
) != 0 &&
2677 time_before(jiffies
, to
));
2679 if (atomic_read(&dd
->irq_workers_active
) != 0)
2680 dev_warn(&dd
->pdev
->dev
,
2681 "Completion workers still active!");
2683 blk_mq_quiesce_queue(dd
->queue
);
2685 blk_mq_tagset_busy_iter(&dd
->tags
, mtip_queue_cmd
, dd
);
2687 set_bit(MTIP_PF_ISSUE_CMDS_BIT
, &dd
->port
->flags
);
2689 if (mtip_device_reset(dd
))
2690 blk_mq_tagset_busy_iter(&dd
->tags
,
2691 mtip_abort_cmd
, dd
);
2693 clear_bit(MTIP_PF_TO_ACTIVE_BIT
, &dd
->port
->flags
);
2695 blk_mq_unquiesce_queue(dd
->queue
);
2698 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
)) {
2700 /* used to restrict the loop to one iteration */
2701 slot_start
= num_cmd_slots
;
2704 slot
= find_next_bit(port
->cmds_to_issue
,
2705 num_cmd_slots
, slot
);
2706 if (slot_wrap
== 1) {
2707 if ((slot_start
>= slot
) ||
2708 (slot
>= num_cmd_slots
))
2711 if (unlikely(slot_start
== num_cmd_slots
))
2714 if (unlikely(slot
== num_cmd_slots
)) {
2720 /* Issue the command to the hardware */
2721 mtip_issue_ncq_command(port
, slot
);
2723 clear_bit(slot
, port
->cmds_to_issue
);
2726 clear_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
);
2729 if (test_bit(MTIP_PF_REBUILD_BIT
, &port
->flags
)) {
2730 if (mtip_ftl_rebuild_poll(dd
) == 0)
2731 clear_bit(MTIP_PF_REBUILD_BIT
, &port
->flags
);
2740 * DMA region teardown
2742 * @dd Pointer to driver_data structure
2747 static void mtip_dma_free(struct driver_data
*dd
)
2749 struct mtip_port
*port
= dd
->port
;
2752 dma_free_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
2753 port
->block1
, port
->block1_dma
);
2755 if (port
->command_list
) {
2756 dma_free_coherent(&dd
->pdev
->dev
, AHCI_CMD_TBL_SZ
,
2757 port
->command_list
, port
->command_list_dma
);
2764 * @dd Pointer to driver_data structure
2767 * -ENOMEM Not enough free DMA region space to initialize driver
2769 static int mtip_dma_alloc(struct driver_data
*dd
)
2771 struct mtip_port
*port
= dd
->port
;
2773 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
2775 dma_alloc_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
2776 &port
->block1_dma
, GFP_KERNEL
);
2780 /* Allocate dma memory for command list */
2781 port
->command_list
=
2782 dma_alloc_coherent(&dd
->pdev
->dev
, AHCI_CMD_TBL_SZ
,
2783 &port
->command_list_dma
, GFP_KERNEL
);
2784 if (!port
->command_list
) {
2785 dma_free_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
2786 port
->block1
, port
->block1_dma
);
2787 port
->block1
= NULL
;
2788 port
->block1_dma
= 0;
2792 /* Setup all pointers into first DMA region */
2793 port
->rxfis
= port
->block1
+ AHCI_RX_FIS_OFFSET
;
2794 port
->rxfis_dma
= port
->block1_dma
+ AHCI_RX_FIS_OFFSET
;
2795 port
->identify
= port
->block1
+ AHCI_IDFY_OFFSET
;
2796 port
->identify_dma
= port
->block1_dma
+ AHCI_IDFY_OFFSET
;
2797 port
->log_buf
= port
->block1
+ AHCI_SECTBUF_OFFSET
;
2798 port
->log_buf_dma
= port
->block1_dma
+ AHCI_SECTBUF_OFFSET
;
2799 port
->smart_buf
= port
->block1
+ AHCI_SMARTBUF_OFFSET
;
2800 port
->smart_buf_dma
= port
->block1_dma
+ AHCI_SMARTBUF_OFFSET
;
2805 static int mtip_hw_get_identify(struct driver_data
*dd
)
2807 struct smart_attr attr242
;
2811 if (mtip_get_identify(dd
->port
, NULL
) < 0)
2814 if (*(dd
->port
->identify
+ MTIP_FTL_REBUILD_OFFSET
) ==
2815 MTIP_FTL_REBUILD_MAGIC
) {
2816 set_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
);
2817 return MTIP_FTL_REBUILD_MAGIC
;
2819 mtip_dump_identify(dd
->port
);
2821 /* check write protect, over temp and rebuild statuses */
2822 rv
= mtip_read_log_page(dd
->port
, ATA_LOG_SATA_NCQ
,
2824 dd
->port
->log_buf_dma
, 1);
2826 dev_warn(&dd
->pdev
->dev
,
2827 "Error in READ LOG EXT (10h) command\n");
2828 /* non-critical error, don't fail the load */
2830 buf
= (unsigned char *)dd
->port
->log_buf
;
2831 if (buf
[259] & 0x1) {
2832 dev_info(&dd
->pdev
->dev
,
2833 "Write protect bit is set.\n");
2834 set_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
);
2836 if (buf
[288] == 0xF7) {
2837 dev_info(&dd
->pdev
->dev
,
2838 "Exceeded Tmax, drive in thermal shutdown.\n");
2839 set_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
);
2841 if (buf
[288] == 0xBF) {
2842 dev_info(&dd
->pdev
->dev
,
2843 "Drive indicates rebuild has failed.\n");
2844 set_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
);
2848 /* get write protect progess */
2849 memset(&attr242
, 0, sizeof(struct smart_attr
));
2850 if (mtip_get_smart_attr(dd
->port
, 242, &attr242
))
2851 dev_warn(&dd
->pdev
->dev
,
2852 "Unable to check write protect progress\n");
2854 dev_info(&dd
->pdev
->dev
,
2855 "Write protect progress: %u%% (%u blocks)\n",
2856 attr242
.cur
, le32_to_cpu(attr242
.data
));
2862 * Called once for each card.
2864 * @dd Pointer to the driver data structure.
2867 * 0 on success, else an error code.
2869 static int mtip_hw_init(struct driver_data
*dd
)
2873 unsigned long timeout
, timetaken
;
2875 dd
->mmio
= pcim_iomap_table(dd
->pdev
)[MTIP_ABAR
];
2877 mtip_detect_product(dd
);
2878 if (dd
->product_type
== MTIP_PRODUCT_UNKNOWN
) {
2885 dd
->port
= kzalloc_node(sizeof(struct mtip_port
), GFP_KERNEL
,
2888 dev_err(&dd
->pdev
->dev
,
2889 "Memory allocation: port structure\n");
2893 /* Continue workqueue setup */
2894 for (i
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++)
2895 dd
->work
[i
].port
= dd
->port
;
2897 /* Enable unaligned IO constraints for some devices */
2898 if (mtip_device_unaligned_constrained(dd
))
2899 dd
->unal_qdepth
= MTIP_MAX_UNALIGNED_SLOTS
;
2901 dd
->unal_qdepth
= 0;
2903 atomic_set(&dd
->port
->cmd_slot_unal
, dd
->unal_qdepth
);
2905 /* Spinlock to prevent concurrent issue */
2906 for (i
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++)
2907 spin_lock_init(&dd
->port
->cmd_issue_lock
[i
]);
2909 /* Set the port mmio base address. */
2910 dd
->port
->mmio
= dd
->mmio
+ PORT_OFFSET
;
2913 /* DMA allocations */
2914 rv
= mtip_dma_alloc(dd
);
2918 /* Setup the pointers to the extended s_active and CI registers. */
2919 for (i
= 0; i
< dd
->slot_groups
; i
++) {
2920 dd
->port
->s_active
[i
] =
2921 dd
->port
->mmio
+ i
*0x80 + PORT_SCR_ACT
;
2922 dd
->port
->cmd_issue
[i
] =
2923 dd
->port
->mmio
+ i
*0x80 + PORT_COMMAND_ISSUE
;
2924 dd
->port
->completed
[i
] =
2925 dd
->port
->mmio
+ i
*0x80 + PORT_SDBV
;
2928 timetaken
= jiffies
;
2929 timeout
= jiffies
+ msecs_to_jiffies(30000);
2930 while (((readl(dd
->port
->mmio
+ PORT_SCR_STAT
) & 0x0F) != 0x03) &&
2931 time_before(jiffies
, timeout
)) {
2934 if (unlikely(mtip_check_surprise_removal(dd
->pdev
))) {
2935 timetaken
= jiffies
- timetaken
;
2936 dev_warn(&dd
->pdev
->dev
,
2937 "Surprise removal detected at %u ms\n",
2938 jiffies_to_msecs(timetaken
));
2942 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))) {
2943 timetaken
= jiffies
- timetaken
;
2944 dev_warn(&dd
->pdev
->dev
,
2945 "Removal detected at %u ms\n",
2946 jiffies_to_msecs(timetaken
));
2951 /* Conditionally reset the HBA. */
2952 if (!(readl(dd
->mmio
+ HOST_CAP
) & HOST_CAP_NZDMA
)) {
2953 if (mtip_hba_reset(dd
) < 0) {
2954 dev_err(&dd
->pdev
->dev
,
2955 "Card did not reset within timeout\n");
2960 /* Clear any pending interrupts on the HBA */
2961 writel(readl(dd
->mmio
+ HOST_IRQ_STAT
),
2962 dd
->mmio
+ HOST_IRQ_STAT
);
2965 mtip_init_port(dd
->port
);
2966 mtip_start_port(dd
->port
);
2968 /* Setup the ISR and enable interrupts. */
2969 rv
= request_irq(dd
->pdev
->irq
, mtip_irq_handler
, IRQF_SHARED
,
2970 dev_driver_string(&dd
->pdev
->dev
), dd
);
2972 dev_err(&dd
->pdev
->dev
,
2973 "Unable to allocate IRQ %d\n", dd
->pdev
->irq
);
2976 irq_set_affinity_hint(dd
->pdev
->irq
, get_cpu_mask(dd
->isr_binding
));
2978 /* Enable interrupts on the HBA. */
2979 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
2980 dd
->mmio
+ HOST_CTL
);
2982 init_waitqueue_head(&dd
->port
->svc_wait
);
2984 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)) {
2992 /* Disable interrupts on the HBA. */
2993 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
2994 dd
->mmio
+ HOST_CTL
);
2996 /* Release the IRQ. */
2997 irq_set_affinity_hint(dd
->pdev
->irq
, NULL
);
2998 free_irq(dd
->pdev
->irq
, dd
);
3001 mtip_deinit_port(dd
->port
);
3005 /* Free the memory allocated for the for structure. */
3011 static int mtip_standby_drive(struct driver_data
*dd
)
3015 if (dd
->sr
|| !dd
->port
)
3018 * Send standby immediate (E0h) to the drive so that it
3021 if (!test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
) &&
3022 !test_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
) &&
3023 !test_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
)) {
3024 rv
= mtip_standby_immediate(dd
->port
);
3026 dev_warn(&dd
->pdev
->dev
,
3027 "STANDBY IMMEDIATE failed\n");
3033 * Called to deinitialize an interface.
3035 * @dd Pointer to the driver data structure.
3040 static int mtip_hw_exit(struct driver_data
*dd
)
3043 /* de-initialize the port. */
3044 mtip_deinit_port(dd
->port
);
3046 /* Disable interrupts on the HBA. */
3047 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3048 dd
->mmio
+ HOST_CTL
);
3051 /* Release the IRQ. */
3052 irq_set_affinity_hint(dd
->pdev
->irq
, NULL
);
3053 free_irq(dd
->pdev
->irq
, dd
);
3056 /* Free dma regions */
3059 /* Free the memory allocated for the for structure. */
3067 * Issue a Standby Immediate command to the device.
3069 * This function is called by the Block Layer just before the
3070 * system powers off during a shutdown.
3072 * @dd Pointer to the driver data structure.
3077 static int mtip_hw_shutdown(struct driver_data
*dd
)
3080 * Send standby immediate (E0h) to the drive so that it
3083 mtip_standby_drive(dd
);
3091 * This function is called by the Block Layer just before the
3092 * system hibernates.
3094 * @dd Pointer to the driver data structure.
3097 * 0 Suspend was successful
3098 * -EFAULT Suspend was not successful
3100 static int mtip_hw_suspend(struct driver_data
*dd
)
3103 * Send standby immediate (E0h) to the drive
3104 * so that it saves its state.
3106 if (mtip_standby_drive(dd
) != 0) {
3107 dev_err(&dd
->pdev
->dev
,
3108 "Failed standby-immediate command\n");
3112 /* Disable interrupts on the HBA.*/
3113 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3114 dd
->mmio
+ HOST_CTL
);
3115 mtip_deinit_port(dd
->port
);
3123 * This function is called by the Block Layer as the
3126 * @dd Pointer to the driver data structure.
3129 * 0 Resume was successful
3130 * -EFAULT Resume was not successful
3132 static int mtip_hw_resume(struct driver_data
*dd
)
3134 /* Perform any needed hardware setup steps */
3138 if (mtip_hba_reset(dd
) != 0) {
3139 dev_err(&dd
->pdev
->dev
,
3140 "Unable to reset the HBA\n");
3145 * Enable the port, DMA engine, and FIS reception specific
3146 * h/w in controller.
3148 mtip_init_port(dd
->port
);
3149 mtip_start_port(dd
->port
);
3151 /* Enable interrupts on the HBA.*/
3152 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
3153 dd
->mmio
+ HOST_CTL
);
3159 * Helper function for reusing disk name
3160 * upon hot insertion.
3162 static int rssd_disk_name_format(char *prefix
,
3167 const int base
= 'z' - 'a' + 1;
3168 char *begin
= buf
+ strlen(prefix
);
3169 char *end
= buf
+ buflen
;
3179 *--p
= 'a' + (index
% unit
);
3180 index
= (index
/ unit
) - 1;
3181 } while (index
>= 0);
3183 memmove(begin
, p
, end
- p
);
3184 memcpy(buf
, prefix
, strlen(prefix
));
3190 * Block layer IOCTL handler.
3192 * @dev Pointer to the block_device structure.
3194 * @cmd IOCTL command passed from the user application.
3195 * @arg Argument passed from the user application.
3198 * 0 IOCTL completed successfully.
3199 * -ENOTTY IOCTL not supported or invalid driver data
3200 * structure pointer.
3202 static int mtip_block_ioctl(struct block_device
*dev
,
3207 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3209 if (!capable(CAP_SYS_ADMIN
))
3215 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)))
3222 return mtip_hw_ioctl(dd
, cmd
, arg
);
3226 #ifdef CONFIG_COMPAT
3228 * Block layer compat IOCTL handler.
3230 * @dev Pointer to the block_device structure.
3232 * @cmd IOCTL command passed from the user application.
3233 * @arg Argument passed from the user application.
3236 * 0 IOCTL completed successfully.
3237 * -ENOTTY IOCTL not supported or invalid driver data
3238 * structure pointer.
3240 static int mtip_block_compat_ioctl(struct block_device
*dev
,
3245 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3247 if (!capable(CAP_SYS_ADMIN
))
3253 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)))
3259 case HDIO_DRIVE_TASKFILE
: {
3260 struct mtip_compat_ide_task_request_s __user
*compat_req_task
;
3261 ide_task_request_t req_task
;
3262 int compat_tasksize
, outtotal
, ret
;
3265 sizeof(struct mtip_compat_ide_task_request_s
);
3268 (struct mtip_compat_ide_task_request_s __user
*) arg
;
3270 if (copy_from_user(&req_task
, (void __user
*) arg
,
3271 compat_tasksize
- (2 * sizeof(compat_long_t
))))
3274 if (get_user(req_task
.out_size
, &compat_req_task
->out_size
))
3277 if (get_user(req_task
.in_size
, &compat_req_task
->in_size
))
3280 outtotal
= sizeof(struct mtip_compat_ide_task_request_s
);
3282 ret
= exec_drive_taskfile(dd
, (void __user
*) arg
,
3283 &req_task
, outtotal
);
3285 if (copy_to_user((void __user
*) arg
, &req_task
,
3287 (2 * sizeof(compat_long_t
))))
3290 if (put_user(req_task
.out_size
, &compat_req_task
->out_size
))
3293 if (put_user(req_task
.in_size
, &compat_req_task
->in_size
))
3299 return mtip_hw_ioctl(dd
, cmd
, arg
);
3305 * Obtain the geometry of the device.
3307 * You may think that this function is obsolete, but some applications,
3308 * fdisk for example still used CHS values. This function describes the
3309 * device as having 224 heads and 56 sectors per cylinder. These values are
3310 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3311 * partition is described in terms of a start and end cylinder this means
3312 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3313 * affects performance.
3315 * @dev Pointer to the block_device strucutre.
3316 * @geo Pointer to a hd_geometry structure.
3319 * 0 Operation completed successfully.
3320 * -ENOTTY An error occurred while reading the drive capacity.
3322 static int mtip_block_getgeo(struct block_device
*dev
,
3323 struct hd_geometry
*geo
)
3325 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3331 if (!(mtip_hw_get_capacity(dd
, &capacity
))) {
3332 dev_warn(&dd
->pdev
->dev
,
3333 "Could not get drive capacity.\n");
3339 sector_div(capacity
, (geo
->heads
* geo
->sectors
));
3340 geo
->cylinders
= capacity
;
3344 static int mtip_block_open(struct block_device
*dev
, fmode_t mode
)
3346 struct driver_data
*dd
;
3348 if (dev
&& dev
->bd_disk
) {
3349 dd
= (struct driver_data
*) dev
->bd_disk
->private_data
;
3352 if (test_bit(MTIP_DDF_REMOVAL_BIT
,
3362 static void mtip_block_release(struct gendisk
*disk
, fmode_t mode
)
3367 * Block device operation function.
3369 * This structure contains pointers to the functions required by the block
3372 static const struct block_device_operations mtip_block_ops
= {
3373 .open
= mtip_block_open
,
3374 .release
= mtip_block_release
,
3375 .ioctl
= mtip_block_ioctl
,
3376 #ifdef CONFIG_COMPAT
3377 .compat_ioctl
= mtip_block_compat_ioctl
,
3379 .getgeo
= mtip_block_getgeo
,
3380 .owner
= THIS_MODULE
3383 static inline bool is_se_active(struct driver_data
*dd
)
3385 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT
, &dd
->port
->flags
))) {
3386 if (dd
->port
->ic_pause_timer
) {
3387 unsigned long to
= dd
->port
->ic_pause_timer
+
3388 msecs_to_jiffies(1000);
3389 if (time_after(jiffies
, to
)) {
3390 clear_bit(MTIP_PF_SE_ACTIVE_BIT
,
3392 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
);
3393 dd
->port
->ic_pause_timer
= 0;
3394 wake_up_interruptible(&dd
->port
->svc_wait
);
3403 static inline bool is_stopped(struct driver_data
*dd
, struct request
*rq
)
3405 if (likely(!(dd
->dd_flag
& MTIP_DDF_STOP_IO
)))
3408 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))
3410 if (test_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
))
3412 if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
) &&
3415 if (test_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
))
3417 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
))
3423 static bool mtip_check_unal_depth(struct blk_mq_hw_ctx
*hctx
,
3426 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3427 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3429 if (rq_data_dir(rq
) == READ
|| !dd
->unal_qdepth
)
3433 * If unaligned depth must be limited on this controller, mark it
3434 * as unaligned if the IO isn't on a 4k boundary (start of length).
3436 if (blk_rq_sectors(rq
) <= 64) {
3437 if ((blk_rq_pos(rq
) & 7) || (blk_rq_sectors(rq
) & 7))
3441 if (cmd
->unaligned
&& atomic_dec_if_positive(&dd
->port
->cmd_slot_unal
) >= 0)
3447 static blk_status_t
mtip_issue_reserved_cmd(struct blk_mq_hw_ctx
*hctx
,
3450 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3451 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3452 struct mtip_int_cmd
*icmd
= cmd
->icmd
;
3453 struct mtip_cmd_hdr
*hdr
=
3454 dd
->port
->command_list
+ sizeof(struct mtip_cmd_hdr
) * rq
->tag
;
3455 struct mtip_cmd_sg
*command_sg
;
3457 if (mtip_commands_active(dd
->port
))
3458 return BLK_STS_DEV_RESOURCE
;
3460 hdr
->ctba
= cpu_to_le32(cmd
->command_dma
& 0xFFFFFFFF);
3461 if (test_bit(MTIP_PF_HOST_CAP_64
, &dd
->port
->flags
))
3462 hdr
->ctbau
= cpu_to_le32((cmd
->command_dma
>> 16) >> 16);
3463 /* Populate the SG list */
3464 hdr
->opts
= cpu_to_le32(icmd
->opts
| icmd
->fis_len
);
3465 if (icmd
->buf_len
) {
3466 command_sg
= cmd
->command
+ AHCI_CMD_TBL_HDR_SZ
;
3468 command_sg
->info
= cpu_to_le32((icmd
->buf_len
-1) & 0x3FFFFF);
3469 command_sg
->dba
= cpu_to_le32(icmd
->buffer
& 0xFFFFFFFF);
3470 command_sg
->dba_upper
=
3471 cpu_to_le32((icmd
->buffer
>> 16) >> 16);
3473 hdr
->opts
|= cpu_to_le32((1 << 16));
3476 /* Populate the command header */
3477 hdr
->byte_count
= 0;
3479 blk_mq_start_request(rq
);
3480 mtip_issue_non_ncq_command(dd
->port
, rq
->tag
);
3484 static blk_status_t
mtip_queue_rq(struct blk_mq_hw_ctx
*hctx
,
3485 const struct blk_mq_queue_data
*bd
)
3487 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3488 struct request
*rq
= bd
->rq
;
3489 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3491 if (blk_rq_is_passthrough(rq
))
3492 return mtip_issue_reserved_cmd(hctx
, rq
);
3494 if (unlikely(mtip_check_unal_depth(hctx
, rq
)))
3495 return BLK_STS_DEV_RESOURCE
;
3497 if (is_se_active(dd
) || is_stopped(dd
, rq
))
3498 return BLK_STS_IOERR
;
3500 blk_mq_start_request(rq
);
3502 mtip_hw_submit_io(dd
, rq
, cmd
, hctx
);
3506 static void mtip_free_cmd(struct blk_mq_tag_set
*set
, struct request
*rq
,
3507 unsigned int hctx_idx
)
3509 struct driver_data
*dd
= set
->driver_data
;
3510 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3515 dma_free_coherent(&dd
->pdev
->dev
, CMD_DMA_ALLOC_SZ
, cmd
->command
,
3519 static int mtip_init_cmd(struct blk_mq_tag_set
*set
, struct request
*rq
,
3520 unsigned int hctx_idx
, unsigned int numa_node
)
3522 struct driver_data
*dd
= set
->driver_data
;
3523 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3525 cmd
->command
= dma_alloc_coherent(&dd
->pdev
->dev
, CMD_DMA_ALLOC_SZ
,
3526 &cmd
->command_dma
, GFP_KERNEL
);
3530 sg_init_table(cmd
->sg
, MTIP_MAX_SG
);
3534 static enum blk_eh_timer_return
mtip_cmd_timeout(struct request
*req
,
3537 struct driver_data
*dd
= req
->q
->queuedata
;
3540 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
3542 cmd
->status
= BLK_STS_TIMEOUT
;
3543 blk_mq_complete_request(req
);
3547 if (test_bit(req
->tag
, dd
->port
->cmds_to_issue
))
3550 if (test_and_set_bit(MTIP_PF_TO_ACTIVE_BIT
, &dd
->port
->flags
))
3553 wake_up_interruptible(&dd
->port
->svc_wait
);
3555 return BLK_EH_RESET_TIMER
;
3558 static const struct blk_mq_ops mtip_mq_ops
= {
3559 .queue_rq
= mtip_queue_rq
,
3560 .init_request
= mtip_init_cmd
,
3561 .exit_request
= mtip_free_cmd
,
3562 .complete
= mtip_softirq_done_fn
,
3563 .timeout
= mtip_cmd_timeout
,
3567 * Block layer initialization function.
3569 * This function is called once by the PCI layer for each P320
3570 * device that is connected to the system.
3572 * @dd Pointer to the driver data structure.
3575 * 0 on success else an error code.
3577 static int mtip_block_initialize(struct driver_data
*dd
)
3579 int rv
= 0, wait_for_rebuild
= 0;
3581 unsigned int index
= 0;
3582 struct kobject
*kobj
;
3585 goto skip_create_disk
; /* hw init done, before rebuild */
3587 if (mtip_hw_init(dd
)) {
3589 goto protocol_init_error
;
3592 dd
->disk
= alloc_disk_node(MTIP_MAX_MINORS
, dd
->numa_node
);
3593 if (dd
->disk
== NULL
) {
3594 dev_err(&dd
->pdev
->dev
,
3595 "Unable to allocate gendisk structure\n");
3597 goto alloc_disk_error
;
3600 rv
= ida_alloc(&rssd_index_ida
, GFP_KERNEL
);
3605 rv
= rssd_disk_name_format("rssd",
3607 dd
->disk
->disk_name
,
3610 goto disk_index_error
;
3612 dd
->disk
->major
= dd
->major
;
3613 dd
->disk
->first_minor
= index
* MTIP_MAX_MINORS
;
3614 dd
->disk
->minors
= MTIP_MAX_MINORS
;
3615 dd
->disk
->fops
= &mtip_block_ops
;
3616 dd
->disk
->private_data
= dd
;
3619 mtip_hw_debugfs_init(dd
);
3621 memset(&dd
->tags
, 0, sizeof(dd
->tags
));
3622 dd
->tags
.ops
= &mtip_mq_ops
;
3623 dd
->tags
.nr_hw_queues
= 1;
3624 dd
->tags
.queue_depth
= MTIP_MAX_COMMAND_SLOTS
;
3625 dd
->tags
.reserved_tags
= 1;
3626 dd
->tags
.cmd_size
= sizeof(struct mtip_cmd
);
3627 dd
->tags
.numa_node
= dd
->numa_node
;
3628 dd
->tags
.flags
= BLK_MQ_F_SHOULD_MERGE
;
3629 dd
->tags
.driver_data
= dd
;
3630 dd
->tags
.timeout
= MTIP_NCQ_CMD_TIMEOUT_MS
;
3632 rv
= blk_mq_alloc_tag_set(&dd
->tags
);
3634 dev_err(&dd
->pdev
->dev
,
3635 "Unable to allocate request queue\n");
3636 goto block_queue_alloc_tag_error
;
3639 /* Allocate the request queue. */
3640 dd
->queue
= blk_mq_init_queue(&dd
->tags
);
3641 if (IS_ERR(dd
->queue
)) {
3642 dev_err(&dd
->pdev
->dev
,
3643 "Unable to allocate request queue\n");
3645 goto block_queue_alloc_init_error
;
3648 dd
->disk
->queue
= dd
->queue
;
3649 dd
->queue
->queuedata
= dd
;
3652 /* Initialize the protocol layer. */
3653 wait_for_rebuild
= mtip_hw_get_identify(dd
);
3654 if (wait_for_rebuild
< 0) {
3655 dev_err(&dd
->pdev
->dev
,
3656 "Protocol layer initialization failed\n");
3658 goto init_hw_cmds_error
;
3662 * if rebuild pending, start the service thread, and delay the block
3663 * queue creation and device_add_disk()
3665 if (wait_for_rebuild
== MTIP_FTL_REBUILD_MAGIC
)
3666 goto start_service_thread
;
3668 /* Set device limits. */
3669 blk_queue_flag_set(QUEUE_FLAG_NONROT
, dd
->queue
);
3670 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM
, dd
->queue
);
3671 blk_queue_max_segments(dd
->queue
, MTIP_MAX_SG
);
3672 blk_queue_physical_block_size(dd
->queue
, 4096);
3673 blk_queue_max_hw_sectors(dd
->queue
, 0xffff);
3674 blk_queue_max_segment_size(dd
->queue
, 0x400000);
3675 dma_set_max_seg_size(&dd
->pdev
->dev
, 0x400000);
3676 blk_queue_io_min(dd
->queue
, 4096);
3678 /* Set the capacity of the device in 512 byte sectors. */
3679 if (!(mtip_hw_get_capacity(dd
, &capacity
))) {
3680 dev_warn(&dd
->pdev
->dev
,
3681 "Could not read drive capacity\n");
3683 goto read_capacity_error
;
3685 set_capacity(dd
->disk
, capacity
);
3687 /* Enable the block device and add it to /dev */
3688 device_add_disk(&dd
->pdev
->dev
, dd
->disk
, NULL
);
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
;
3723 /* Delete our gendisk. This also removes the device from /dev */
3724 del_gendisk(dd
->disk
);
3726 read_capacity_error
:
3728 blk_cleanup_queue(dd
->queue
);
3729 block_queue_alloc_init_error
:
3730 blk_mq_free_tag_set(&dd
->tags
);
3731 block_queue_alloc_tag_error
:
3732 mtip_hw_debugfs_exit(dd
);
3734 ida_free(&rssd_index_ida
, index
);
3740 mtip_hw_exit(dd
); /* De-initialize the protocol layer. */
3742 protocol_init_error
:
3746 static bool mtip_no_dev_cleanup(struct request
*rq
, void *data
, bool reserv
)
3748 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3750 cmd
->status
= BLK_STS_IOERR
;
3751 blk_mq_complete_request(rq
);
3756 * Block layer deinitialization function.
3758 * Called by the PCI layer as each P320 device is removed.
3760 * @dd Pointer to the driver data structure.
3765 static int mtip_block_remove(struct driver_data
*dd
)
3767 struct kobject
*kobj
;
3769 mtip_hw_debugfs_exit(dd
);
3771 if (dd
->mtip_svc_handler
) {
3772 set_bit(MTIP_PF_SVC_THD_STOP_BIT
, &dd
->port
->flags
);
3773 wake_up_interruptible(&dd
->port
->svc_wait
);
3774 kthread_stop(dd
->mtip_svc_handler
);
3777 /* Clean up the sysfs attributes, if created */
3778 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
)) {
3779 kobj
= kobject_get(&disk_to_dev(dd
->disk
)->kobj
);
3781 mtip_hw_sysfs_exit(dd
, kobj
);
3788 * Explicitly wait here for IOs to quiesce,
3789 * as mtip_standby_drive usually won't wait for IOs.
3791 if (!mtip_quiesce_io(dd
->port
, MTIP_QUIESCE_IO_TIMEOUT_MS
))
3792 mtip_standby_drive(dd
);
3795 dev_info(&dd
->pdev
->dev
, "device %s surprise removal\n",
3796 dd
->disk
->disk_name
);
3798 blk_freeze_queue_start(dd
->queue
);
3799 blk_mq_quiesce_queue(dd
->queue
);
3800 blk_mq_tagset_busy_iter(&dd
->tags
, mtip_no_dev_cleanup
, dd
);
3801 blk_mq_unquiesce_queue(dd
->queue
);
3804 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
))
3805 del_gendisk(dd
->disk
);
3806 if (dd
->disk
->queue
) {
3807 blk_cleanup_queue(dd
->queue
);
3808 blk_mq_free_tag_set(&dd
->tags
);
3815 ida_free(&rssd_index_ida
, dd
->index
);
3817 /* De-initialize the protocol layer. */
3824 * Function called by the PCI layer when just before the
3825 * machine shuts down.
3827 * If a protocol layer shutdown function is present it will be called
3830 * @dd Pointer to the driver data structure.
3835 static int mtip_block_shutdown(struct driver_data
*dd
)
3837 mtip_hw_shutdown(dd
);
3839 /* Delete our gendisk structure, and cleanup the blk queue. */
3841 dev_info(&dd
->pdev
->dev
,
3842 "Shutting down %s ...\n", dd
->disk
->disk_name
);
3844 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
))
3845 del_gendisk(dd
->disk
);
3846 if (dd
->disk
->queue
) {
3847 blk_cleanup_queue(dd
->queue
);
3848 blk_mq_free_tag_set(&dd
->tags
);
3855 ida_free(&rssd_index_ida
, dd
->index
);
3859 static int mtip_block_suspend(struct driver_data
*dd
)
3861 dev_info(&dd
->pdev
->dev
,
3862 "Suspending %s ...\n", dd
->disk
->disk_name
);
3863 mtip_hw_suspend(dd
);
3867 static int mtip_block_resume(struct driver_data
*dd
)
3869 dev_info(&dd
->pdev
->dev
, "Resuming %s ...\n",
3870 dd
->disk
->disk_name
);
3875 static void drop_cpu(int cpu
)
3880 static int get_least_used_cpu_on_node(int node
)
3882 int cpu
, least_used_cpu
, least_cnt
;
3883 const struct cpumask
*node_mask
;
3885 node_mask
= cpumask_of_node(node
);
3886 least_used_cpu
= cpumask_first(node_mask
);
3887 least_cnt
= cpu_use
[least_used_cpu
];
3888 cpu
= least_used_cpu
;
3890 for_each_cpu(cpu
, node_mask
) {
3891 if (cpu_use
[cpu
] < least_cnt
) {
3892 least_used_cpu
= cpu
;
3893 least_cnt
= cpu_use
[cpu
];
3896 cpu_use
[least_used_cpu
]++;
3897 return least_used_cpu
;
3900 /* Helper for selecting a node in round robin mode */
3901 static inline int mtip_get_next_rr_node(void)
3903 static int next_node
= NUMA_NO_NODE
;
3905 if (next_node
== NUMA_NO_NODE
) {
3906 next_node
= first_online_node
;
3910 next_node
= next_online_node(next_node
);
3911 if (next_node
== MAX_NUMNODES
)
3912 next_node
= first_online_node
;
3916 static DEFINE_HANDLER(0);
3917 static DEFINE_HANDLER(1);
3918 static DEFINE_HANDLER(2);
3919 static DEFINE_HANDLER(3);
3920 static DEFINE_HANDLER(4);
3921 static DEFINE_HANDLER(5);
3922 static DEFINE_HANDLER(6);
3923 static DEFINE_HANDLER(7);
3925 static void mtip_disable_link_opts(struct driver_data
*dd
, struct pci_dev
*pdev
)
3928 unsigned short pcie_dev_ctrl
;
3930 pos
= pci_find_capability(pdev
, PCI_CAP_ID_EXP
);
3932 pci_read_config_word(pdev
,
3933 pos
+ PCI_EXP_DEVCTL
,
3935 if (pcie_dev_ctrl
& (1 << 11) ||
3936 pcie_dev_ctrl
& (1 << 4)) {
3937 dev_info(&dd
->pdev
->dev
,
3938 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
3939 pdev
->vendor
, pdev
->device
);
3940 pcie_dev_ctrl
&= ~(PCI_EXP_DEVCTL_NOSNOOP_EN
|
3941 PCI_EXP_DEVCTL_RELAX_EN
);
3942 pci_write_config_word(pdev
,
3943 pos
+ PCI_EXP_DEVCTL
,
3949 static void mtip_fix_ero_nosnoop(struct driver_data
*dd
, struct pci_dev
*pdev
)
3952 * This workaround is specific to AMD/ATI chipset with a PCI upstream
3953 * device with device id 0x5aXX
3955 if (pdev
->bus
&& pdev
->bus
->self
) {
3956 if (pdev
->bus
->self
->vendor
== PCI_VENDOR_ID_ATI
&&
3957 ((pdev
->bus
->self
->device
& 0xff00) == 0x5a00)) {
3958 mtip_disable_link_opts(dd
, pdev
->bus
->self
);
3960 /* Check further up the topology */
3961 struct pci_dev
*parent_dev
= pdev
->bus
->self
;
3962 if (parent_dev
->bus
&&
3963 parent_dev
->bus
->parent
&&
3964 parent_dev
->bus
->parent
->self
&&
3965 parent_dev
->bus
->parent
->self
->vendor
==
3966 PCI_VENDOR_ID_ATI
&&
3967 (parent_dev
->bus
->parent
->self
->device
&
3968 0xff00) == 0x5a00) {
3969 mtip_disable_link_opts(dd
,
3970 parent_dev
->bus
->parent
->self
);
3977 * Called for each supported PCI device detected.
3979 * This function allocates the private data structure, enables the
3980 * PCI device and then calls the block layer initialization function.
3983 * 0 on success else an error code.
3985 static int mtip_pci_probe(struct pci_dev
*pdev
,
3986 const struct pci_device_id
*ent
)
3989 struct driver_data
*dd
= NULL
;
3991 const struct cpumask
*node_mask
;
3992 int cpu
, i
= 0, j
= 0;
3993 int my_node
= NUMA_NO_NODE
;
3994 unsigned long flags
;
3996 /* Allocate memory for this devices private data. */
3997 my_node
= pcibus_to_node(pdev
->bus
);
3998 if (my_node
!= NUMA_NO_NODE
) {
3999 if (!node_online(my_node
))
4000 my_node
= mtip_get_next_rr_node();
4002 dev_info(&pdev
->dev
, "Kernel not reporting proximity, choosing a node\n");
4003 my_node
= mtip_get_next_rr_node();
4005 dev_info(&pdev
->dev
, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4006 my_node
, pcibus_to_node(pdev
->bus
), dev_to_node(&pdev
->dev
),
4007 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
4009 dd
= kzalloc_node(sizeof(struct driver_data
), GFP_KERNEL
, my_node
);
4012 "Unable to allocate memory for driver data\n");
4016 /* Attach the private data to this PCI device. */
4017 pci_set_drvdata(pdev
, dd
);
4019 rv
= pcim_enable_device(pdev
);
4021 dev_err(&pdev
->dev
, "Unable to enable device\n");
4025 /* Map BAR5 to memory. */
4026 rv
= pcim_iomap_regions(pdev
, 1 << MTIP_ABAR
, MTIP_DRV_NAME
);
4028 dev_err(&pdev
->dev
, "Unable to map regions\n");
4032 rv
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(64));
4034 dev_warn(&pdev
->dev
, "64-bit DMA enable failed\n");
4038 /* Copy the info we may need later into the private data structure. */
4039 dd
->major
= mtip_major
;
4040 dd
->instance
= instance
;
4042 dd
->numa_node
= my_node
;
4044 INIT_LIST_HEAD(&dd
->online_list
);
4045 INIT_LIST_HEAD(&dd
->remove_list
);
4047 memset(dd
->workq_name
, 0, 32);
4048 snprintf(dd
->workq_name
, 31, "mtipq%d", dd
->instance
);
4050 dd
->isr_workq
= create_workqueue(dd
->workq_name
);
4051 if (!dd
->isr_workq
) {
4052 dev_warn(&pdev
->dev
, "Can't create wq %d\n", dd
->instance
);
4057 memset(cpu_list
, 0, sizeof(cpu_list
));
4059 node_mask
= cpumask_of_node(dd
->numa_node
);
4060 if (!cpumask_empty(node_mask
)) {
4061 for_each_cpu(cpu
, node_mask
)
4063 snprintf(&cpu_list
[j
], 256 - j
, "%d ", cpu
);
4064 j
= strlen(cpu_list
);
4067 dev_info(&pdev
->dev
, "Node %d on package %d has %d cpu(s): %s\n",
4069 topology_physical_package_id(cpumask_first(node_mask
)),
4070 nr_cpus_node(dd
->numa_node
),
4073 dev_dbg(&pdev
->dev
, "mtip32xx: node_mask empty\n");
4075 dd
->isr_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4076 dev_info(&pdev
->dev
, "Initial IRQ binding node:cpu %d:%d\n",
4077 cpu_to_node(dd
->isr_binding
), dd
->isr_binding
);
4079 /* first worker context always runs in ISR */
4080 dd
->work
[0].cpu_binding
= dd
->isr_binding
;
4081 dd
->work
[1].cpu_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4082 dd
->work
[2].cpu_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4083 dd
->work
[3].cpu_binding
= dd
->work
[0].cpu_binding
;
4084 dd
->work
[4].cpu_binding
= dd
->work
[1].cpu_binding
;
4085 dd
->work
[5].cpu_binding
= dd
->work
[2].cpu_binding
;
4086 dd
->work
[6].cpu_binding
= dd
->work
[2].cpu_binding
;
4087 dd
->work
[7].cpu_binding
= dd
->work
[1].cpu_binding
;
4089 /* Log the bindings */
4090 for_each_present_cpu(cpu
) {
4091 memset(cpu_list
, 0, sizeof(cpu_list
));
4092 for (i
= 0, j
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++) {
4093 if (dd
->work
[i
].cpu_binding
== cpu
) {
4094 snprintf(&cpu_list
[j
], 256 - j
, "%d ", i
);
4095 j
= strlen(cpu_list
);
4099 dev_info(&pdev
->dev
, "CPU %d: WQs %s\n", cpu
, cpu_list
);
4102 INIT_WORK(&dd
->work
[0].work
, mtip_workq_sdbf0
);
4103 INIT_WORK(&dd
->work
[1].work
, mtip_workq_sdbf1
);
4104 INIT_WORK(&dd
->work
[2].work
, mtip_workq_sdbf2
);
4105 INIT_WORK(&dd
->work
[3].work
, mtip_workq_sdbf3
);
4106 INIT_WORK(&dd
->work
[4].work
, mtip_workq_sdbf4
);
4107 INIT_WORK(&dd
->work
[5].work
, mtip_workq_sdbf5
);
4108 INIT_WORK(&dd
->work
[6].work
, mtip_workq_sdbf6
);
4109 INIT_WORK(&dd
->work
[7].work
, mtip_workq_sdbf7
);
4111 pci_set_master(pdev
);
4112 rv
= pci_enable_msi(pdev
);
4114 dev_warn(&pdev
->dev
,
4115 "Unable to enable MSI interrupt.\n");
4116 goto msi_initialize_err
;
4119 mtip_fix_ero_nosnoop(dd
, pdev
);
4121 /* Initialize the block layer. */
4122 rv
= mtip_block_initialize(dd
);
4125 "Unable to initialize block layer\n");
4126 goto block_initialize_err
;
4130 * Increment the instance count so that each device has a unique
4134 if (rv
!= MTIP_FTL_REBUILD_MAGIC
)
4135 set_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
);
4137 rv
= 0; /* device in rebuild state, return 0 from probe */
4139 /* Add to online list even if in ftl rebuild */
4140 spin_lock_irqsave(&dev_lock
, flags
);
4141 list_add(&dd
->online_list
, &online_list
);
4142 spin_unlock_irqrestore(&dev_lock
, flags
);
4146 block_initialize_err
:
4147 pci_disable_msi(pdev
);
4150 if (dd
->isr_workq
) {
4151 flush_workqueue(dd
->isr_workq
);
4152 destroy_workqueue(dd
->isr_workq
);
4153 drop_cpu(dd
->work
[0].cpu_binding
);
4154 drop_cpu(dd
->work
[1].cpu_binding
);
4155 drop_cpu(dd
->work
[2].cpu_binding
);
4158 pcim_iounmap_regions(pdev
, 1 << MTIP_ABAR
);
4162 pci_set_drvdata(pdev
, NULL
);
4169 * Called for each probed device when the device is removed or the
4170 * driver is unloaded.
4175 static void mtip_pci_remove(struct pci_dev
*pdev
)
4177 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4178 unsigned long flags
, to
;
4180 set_bit(MTIP_DDF_REMOVAL_BIT
, &dd
->dd_flag
);
4182 spin_lock_irqsave(&dev_lock
, flags
);
4183 list_del_init(&dd
->online_list
);
4184 list_add(&dd
->remove_list
, &removing_list
);
4185 spin_unlock_irqrestore(&dev_lock
, flags
);
4187 mtip_check_surprise_removal(pdev
);
4188 synchronize_irq(dd
->pdev
->irq
);
4190 /* Spin until workers are done */
4191 to
= jiffies
+ msecs_to_jiffies(4000);
4194 } while (atomic_read(&dd
->irq_workers_active
) != 0 &&
4195 time_before(jiffies
, to
));
4197 if (atomic_read(&dd
->irq_workers_active
) != 0) {
4198 dev_warn(&dd
->pdev
->dev
,
4199 "Completion workers still active!\n");
4202 blk_set_queue_dying(dd
->queue
);
4203 set_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
);
4205 /* Clean up the block layer. */
4206 mtip_block_remove(dd
);
4208 if (dd
->isr_workq
) {
4209 flush_workqueue(dd
->isr_workq
);
4210 destroy_workqueue(dd
->isr_workq
);
4211 drop_cpu(dd
->work
[0].cpu_binding
);
4212 drop_cpu(dd
->work
[1].cpu_binding
);
4213 drop_cpu(dd
->work
[2].cpu_binding
);
4216 pci_disable_msi(pdev
);
4218 spin_lock_irqsave(&dev_lock
, flags
);
4219 list_del_init(&dd
->remove_list
);
4220 spin_unlock_irqrestore(&dev_lock
, flags
);
4224 pcim_iounmap_regions(pdev
, 1 << MTIP_ABAR
);
4225 pci_set_drvdata(pdev
, NULL
);
4229 * Called for each probed device when the device is suspended.
4235 static int mtip_pci_suspend(struct pci_dev
*pdev
, pm_message_t mesg
)
4238 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4242 "Driver private datastructure is NULL\n");
4246 set_bit(MTIP_DDF_RESUME_BIT
, &dd
->dd_flag
);
4248 /* Disable ports & interrupts then send standby immediate */
4249 rv
= mtip_block_suspend(dd
);
4252 "Failed to suspend controller\n");
4257 * Save the pci config space to pdev structure &
4258 * disable the device
4260 pci_save_state(pdev
);
4261 pci_disable_device(pdev
);
4263 /* Move to Low power state*/
4264 pci_set_power_state(pdev
, PCI_D3hot
);
4270 * Called for each probed device when the device is resumed.
4276 static int mtip_pci_resume(struct pci_dev
*pdev
)
4279 struct driver_data
*dd
;
4281 dd
= pci_get_drvdata(pdev
);
4284 "Driver private datastructure is NULL\n");
4288 /* Move the device to active State */
4289 pci_set_power_state(pdev
, PCI_D0
);
4291 /* Restore PCI configuration space */
4292 pci_restore_state(pdev
);
4294 /* Enable the PCI device*/
4295 rv
= pcim_enable_device(pdev
);
4298 "Failed to enable card during resume\n");
4301 pci_set_master(pdev
);
4304 * Calls hbaReset, initPort, & startPort function
4305 * then enables interrupts
4307 rv
= mtip_block_resume(dd
);
4309 dev_err(&pdev
->dev
, "Unable to resume\n");
4312 clear_bit(MTIP_DDF_RESUME_BIT
, &dd
->dd_flag
);
4323 static void mtip_pci_shutdown(struct pci_dev
*pdev
)
4325 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4327 mtip_block_shutdown(dd
);
4330 /* Table of device ids supported by this driver. */
4331 static const struct pci_device_id mtip_pci_tbl
[] = {
4332 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320H_DEVICE_ID
) },
4333 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320M_DEVICE_ID
) },
4334 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320S_DEVICE_ID
) },
4335 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P325M_DEVICE_ID
) },
4336 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P420H_DEVICE_ID
) },
4337 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P420M_DEVICE_ID
) },
4338 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P425M_DEVICE_ID
) },
4342 /* Structure that describes the PCI driver functions. */
4343 static struct pci_driver mtip_pci_driver
= {
4344 .name
= MTIP_DRV_NAME
,
4345 .id_table
= mtip_pci_tbl
,
4346 .probe
= mtip_pci_probe
,
4347 .remove
= mtip_pci_remove
,
4348 .suspend
= mtip_pci_suspend
,
4349 .resume
= mtip_pci_resume
,
4350 .shutdown
= mtip_pci_shutdown
,
4353 MODULE_DEVICE_TABLE(pci
, mtip_pci_tbl
);
4356 * Module initialization function.
4358 * Called once when the module is loaded. This function allocates a major
4359 * block device number to the Cyclone devices and registers the PCI layer
4363 * 0 on success else error code.
4365 static int __init
mtip_init(void)
4369 pr_info(MTIP_DRV_NAME
" Version " MTIP_DRV_VERSION
"\n");
4371 spin_lock_init(&dev_lock
);
4373 INIT_LIST_HEAD(&online_list
);
4374 INIT_LIST_HEAD(&removing_list
);
4376 /* Allocate a major block device number to use with this driver. */
4377 error
= register_blkdev(0, MTIP_DRV_NAME
);
4379 pr_err("Unable to register block device (%d)\n",
4385 dfs_parent
= debugfs_create_dir("rssd", NULL
);
4386 if (IS_ERR_OR_NULL(dfs_parent
)) {
4387 pr_warn("Error creating debugfs parent\n");
4391 dfs_device_status
= debugfs_create_file("device_status",
4392 0444, dfs_parent
, NULL
,
4393 &mtip_device_status_fops
);
4394 if (IS_ERR_OR_NULL(dfs_device_status
)) {
4395 pr_err("Error creating device_status node\n");
4396 dfs_device_status
= NULL
;
4400 /* Register our PCI operations. */
4401 error
= pci_register_driver(&mtip_pci_driver
);
4403 debugfs_remove(dfs_parent
);
4404 unregister_blkdev(mtip_major
, MTIP_DRV_NAME
);
4411 * Module de-initialization function.
4413 * Called once when the module is unloaded. This function deallocates
4414 * the major block device number allocated by mtip_init() and
4415 * unregisters the PCI layer of the driver.
4420 static void __exit
mtip_exit(void)
4422 /* Release the allocated major block device number. */
4423 unregister_blkdev(mtip_major
, MTIP_DRV_NAME
);
4425 /* Unregister the PCI driver. */
4426 pci_unregister_driver(&mtip_pci_driver
);
4428 debugfs_remove_recursive(dfs_parent
);
4431 MODULE_AUTHOR("Micron Technology, Inc");
4432 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4433 MODULE_LICENSE("GPL");
4434 MODULE_VERSION(MTIP_DRV_VERSION
);
4436 module_init(mtip_init
);
4437 module_exit(mtip_exit
);