2 * Driver for the Micron P320 SSD
3 * Copyright (C) 2011 Micron Technology, Inc.
5 * Portions of this code were derived from works subjected to the
7 * Copyright (C) 2009 Integrated Device Technology, Inc.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
21 #include <linux/pci.h>
22 #include <linux/interrupt.h>
23 #include <linux/ata.h>
24 #include <linux/delay.h>
25 #include <linux/hdreg.h>
26 #include <linux/uaccess.h>
27 #include <linux/random.h>
28 #include <linux/smp.h>
29 #include <linux/compat.h>
31 #include <linux/module.h>
32 #include <linux/genhd.h>
33 #include <linux/blkdev.h>
34 #include <linux/blk-mq.h>
35 #include <linux/bio.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/idr.h>
38 #include <linux/kthread.h>
39 #include <../drivers/ata/ahci.h>
40 #include <linux/export.h>
41 #include <linux/debugfs.h>
42 #include <linux/prefetch.h>
45 #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
47 /* DMA region containing RX Fis, Identify, RLE10, and SMART buffers */
48 #define AHCI_RX_FIS_SZ 0x100
49 #define AHCI_RX_FIS_OFFSET 0x0
50 #define AHCI_IDFY_SZ ATA_SECT_SIZE
51 #define AHCI_IDFY_OFFSET 0x400
52 #define AHCI_SECTBUF_SZ ATA_SECT_SIZE
53 #define AHCI_SECTBUF_OFFSET 0x800
54 #define AHCI_SMARTBUF_SZ ATA_SECT_SIZE
55 #define AHCI_SMARTBUF_OFFSET 0xC00
56 /* 0x100 + 0x200 + 0x200 + 0x200 is smaller than 4k but we pad it out */
57 #define BLOCK_DMA_ALLOC_SZ 4096
59 /* DMA region containing command table (should be 8192 bytes) */
60 #define AHCI_CMD_SLOT_SZ sizeof(struct mtip_cmd_hdr)
61 #define AHCI_CMD_TBL_SZ (MTIP_MAX_COMMAND_SLOTS * AHCI_CMD_SLOT_SZ)
62 #define AHCI_CMD_TBL_OFFSET 0x0
64 /* DMA region per command (contains header and SGL) */
65 #define AHCI_CMD_TBL_HDR_SZ 0x80
66 #define AHCI_CMD_TBL_HDR_OFFSET 0x0
67 #define AHCI_CMD_TBL_SGL_SZ (MTIP_MAX_SG * sizeof(struct mtip_cmd_sg))
68 #define AHCI_CMD_TBL_SGL_OFFSET AHCI_CMD_TBL_HDR_SZ
69 #define CMD_DMA_ALLOC_SZ (AHCI_CMD_TBL_SGL_SZ + AHCI_CMD_TBL_HDR_SZ)
72 #define HOST_CAP_NZDMA (1 << 19)
73 #define HOST_HSORG 0xFC
74 #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
75 #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
76 #define HSORG_HWREV 0xFF00
77 #define HSORG_STYLE 0x8
78 #define HSORG_SLOTGROUPS 0x7
80 #define PORT_COMMAND_ISSUE 0x38
81 #define PORT_SDBV 0x7C
83 #define PORT_OFFSET 0x100
84 #define PORT_MEM_SIZE 0x80
86 #define PORT_IRQ_ERR \
87 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
88 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
89 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
91 #define PORT_IRQ_LEGACY \
92 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
93 #define PORT_IRQ_HANDLED \
94 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
95 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
96 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
97 #define DEF_PORT_IRQ \
98 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
100 /* product numbers */
101 #define MTIP_PRODUCT_UNKNOWN 0x00
102 #define MTIP_PRODUCT_ASICFPGA 0x11
104 /* Device instance number, incremented each time a device is probed. */
107 static struct list_head online_list
;
108 static struct list_head removing_list
;
109 static spinlock_t dev_lock
;
112 * Global variable used to hold the major block device number
113 * allocated in mtip_init().
115 static int mtip_major
;
116 static struct dentry
*dfs_parent
;
117 static struct dentry
*dfs_device_status
;
119 static u32 cpu_use
[NR_CPUS
];
121 static DEFINE_SPINLOCK(rssd_index_lock
);
122 static DEFINE_IDA(rssd_index_ida
);
124 static int mtip_block_initialize(struct driver_data
*dd
);
127 struct mtip_compat_ide_task_request_s
{
130 ide_reg_valid_t out_flags
;
131 ide_reg_valid_t in_flags
;
134 compat_ulong_t out_size
;
135 compat_ulong_t in_size
;
140 * This function check_for_surprise_removal is called
141 * while card is removed from the system and it will
142 * read the vendor id from the configration space
144 * @pdev Pointer to the pci_dev structure.
147 * true if device removed, else false
149 static bool mtip_check_surprise_removal(struct pci_dev
*pdev
)
152 struct driver_data
*dd
= pci_get_drvdata(pdev
);
157 /* Read the vendorID from the configuration space */
158 pci_read_config_word(pdev
, 0x00, &vendor_id
);
159 if (vendor_id
== 0xFFFF) {
162 set_bit(QUEUE_FLAG_DEAD
, &dd
->queue
->queue_flags
);
164 dev_warn(&dd
->pdev
->dev
,
165 "%s: dd->queue is NULL\n", __func__
);
166 return true; /* device removed */
169 return false; /* device present */
172 /* we have to use runtime tag to setup command header */
173 static void mtip_init_cmd_header(struct request
*rq
)
175 struct driver_data
*dd
= rq
->q
->queuedata
;
176 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
177 u32 host_cap_64
= readl(dd
->mmio
+ HOST_CAP
) & HOST_CAP_64
;
179 /* Point the command headers at the command tables. */
180 cmd
->command_header
= dd
->port
->command_list
+
181 (sizeof(struct mtip_cmd_hdr
) * rq
->tag
);
182 cmd
->command_header_dma
= dd
->port
->command_list_dma
+
183 (sizeof(struct mtip_cmd_hdr
) * rq
->tag
);
186 cmd
->command_header
->ctbau
= __force_bit2int
cpu_to_le32((cmd
->command_dma
>> 16) >> 16);
188 cmd
->command_header
->ctba
= __force_bit2int
cpu_to_le32(cmd
->command_dma
& 0xFFFFFFFF);
191 static struct mtip_cmd
*mtip_get_int_command(struct driver_data
*dd
)
195 if (mtip_check_surprise_removal(dd
->pdev
))
198 rq
= blk_mq_alloc_request(dd
->queue
, REQ_OP_DRV_IN
, BLK_MQ_REQ_RESERVED
);
202 /* Internal cmd isn't submitted via .queue_rq */
203 mtip_init_cmd_header(rq
);
205 return blk_mq_rq_to_pdu(rq
);
208 static struct mtip_cmd
*mtip_cmd_from_tag(struct driver_data
*dd
,
211 struct blk_mq_hw_ctx
*hctx
= dd
->queue
->queue_hw_ctx
[0];
213 return blk_mq_rq_to_pdu(blk_mq_tag_to_rq(hctx
->tags
, tag
));
217 * Reset the HBA (without sleeping)
219 * @dd Pointer to the driver data structure.
222 * 0 The reset was successful.
223 * -1 The HBA Reset bit did not clear.
225 static int mtip_hba_reset(struct driver_data
*dd
)
227 unsigned long timeout
;
229 /* Set the reset bit */
230 writel(HOST_RESET
, dd
->mmio
+ HOST_CTL
);
233 readl(dd
->mmio
+ HOST_CTL
);
236 * Spin for up to 10 seconds waiting for reset acknowledgement. Spec
237 * is 1 sec but in LUN failure conditions, up to 10 secs are required
239 timeout
= jiffies
+ msecs_to_jiffies(10000);
242 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))
245 } while ((readl(dd
->mmio
+ HOST_CTL
) & HOST_RESET
)
246 && time_before(jiffies
, timeout
));
248 if (readl(dd
->mmio
+ HOST_CTL
) & HOST_RESET
)
255 * Issue a command to the hardware.
257 * Set the appropriate bit in the s_active and Command Issue hardware
258 * registers, causing hardware command processing to begin.
260 * @port Pointer to the port structure.
261 * @tag The tag of the command to be issued.
266 static inline void mtip_issue_ncq_command(struct mtip_port
*port
, int tag
)
268 int group
= tag
>> 5;
270 /* guard SACT and CI registers */
271 spin_lock(&port
->cmd_issue_lock
[group
]);
272 writel((1 << MTIP_TAG_BIT(tag
)),
273 port
->s_active
[MTIP_TAG_INDEX(tag
)]);
274 writel((1 << MTIP_TAG_BIT(tag
)),
275 port
->cmd_issue
[MTIP_TAG_INDEX(tag
)]);
276 spin_unlock(&port
->cmd_issue_lock
[group
]);
280 * Enable/disable the reception of FIS
282 * @port Pointer to the port data structure
283 * @enable 1 to enable, 0 to disable
286 * Previous state: 1 enabled, 0 disabled
288 static int mtip_enable_fis(struct mtip_port
*port
, int enable
)
292 /* enable FIS reception */
293 tmp
= readl(port
->mmio
+ PORT_CMD
);
295 writel(tmp
| PORT_CMD_FIS_RX
, port
->mmio
+ PORT_CMD
);
297 writel(tmp
& ~PORT_CMD_FIS_RX
, port
->mmio
+ PORT_CMD
);
300 readl(port
->mmio
+ PORT_CMD
);
302 return (((tmp
& PORT_CMD_FIS_RX
) == PORT_CMD_FIS_RX
));
306 * Enable/disable the DMA engine
308 * @port Pointer to the port data structure
309 * @enable 1 to enable, 0 to disable
312 * Previous state: 1 enabled, 0 disabled.
314 static int mtip_enable_engine(struct mtip_port
*port
, int enable
)
318 /* enable FIS reception */
319 tmp
= readl(port
->mmio
+ PORT_CMD
);
321 writel(tmp
| PORT_CMD_START
, port
->mmio
+ PORT_CMD
);
323 writel(tmp
& ~PORT_CMD_START
, port
->mmio
+ PORT_CMD
);
325 readl(port
->mmio
+ PORT_CMD
);
326 return (((tmp
& PORT_CMD_START
) == PORT_CMD_START
));
330 * Enables the port DMA engine and FIS reception.
335 static inline void mtip_start_port(struct mtip_port
*port
)
337 /* Enable FIS reception */
338 mtip_enable_fis(port
, 1);
340 /* Enable the DMA engine */
341 mtip_enable_engine(port
, 1);
345 * Deinitialize a port by disabling port interrupts, the DMA engine,
348 * @port Pointer to the port structure
353 static inline void mtip_deinit_port(struct mtip_port
*port
)
355 /* Disable interrupts on this port */
356 writel(0, port
->mmio
+ PORT_IRQ_MASK
);
358 /* Disable the DMA engine */
359 mtip_enable_engine(port
, 0);
361 /* Disable FIS reception */
362 mtip_enable_fis(port
, 0);
368 * This function deinitializes the port by calling mtip_deinit_port() and
369 * then initializes it by setting the command header and RX FIS addresses,
370 * clearing the SError register and any pending port interrupts before
371 * re-enabling the default set of port interrupts.
373 * @port Pointer to the port structure.
378 static void mtip_init_port(struct mtip_port
*port
)
381 mtip_deinit_port(port
);
383 /* Program the command list base and FIS base addresses */
384 if (readl(port
->dd
->mmio
+ HOST_CAP
) & HOST_CAP_64
) {
385 writel((port
->command_list_dma
>> 16) >> 16,
386 port
->mmio
+ PORT_LST_ADDR_HI
);
387 writel((port
->rxfis_dma
>> 16) >> 16,
388 port
->mmio
+ PORT_FIS_ADDR_HI
);
391 writel(port
->command_list_dma
& 0xFFFFFFFF,
392 port
->mmio
+ PORT_LST_ADDR
);
393 writel(port
->rxfis_dma
& 0xFFFFFFFF, port
->mmio
+ PORT_FIS_ADDR
);
396 writel(readl(port
->mmio
+ PORT_SCR_ERR
), port
->mmio
+ PORT_SCR_ERR
);
398 /* reset the completed registers.*/
399 for (i
= 0; i
< port
->dd
->slot_groups
; i
++)
400 writel(0xFFFFFFFF, port
->completed
[i
]);
402 /* Clear any pending interrupts for this port */
403 writel(readl(port
->mmio
+ PORT_IRQ_STAT
), port
->mmio
+ PORT_IRQ_STAT
);
405 /* Clear any pending interrupts on the HBA. */
406 writel(readl(port
->dd
->mmio
+ HOST_IRQ_STAT
),
407 port
->dd
->mmio
+ HOST_IRQ_STAT
);
409 /* Enable port interrupts */
410 writel(DEF_PORT_IRQ
, port
->mmio
+ PORT_IRQ_MASK
);
416 * @port Pointer to the port data structure.
421 static void mtip_restart_port(struct mtip_port
*port
)
423 unsigned long timeout
;
425 /* Disable the DMA engine */
426 mtip_enable_engine(port
, 0);
428 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
429 timeout
= jiffies
+ msecs_to_jiffies(500);
430 while ((readl(port
->mmio
+ PORT_CMD
) & PORT_CMD_LIST_ON
)
431 && time_before(jiffies
, timeout
))
434 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
438 * Chip quirk: escalate to hba reset if
439 * PxCMD.CR not clear after 500 ms
441 if (readl(port
->mmio
+ PORT_CMD
) & PORT_CMD_LIST_ON
) {
442 dev_warn(&port
->dd
->pdev
->dev
,
443 "PxCMD.CR not clear, escalating reset\n");
445 if (mtip_hba_reset(port
->dd
))
446 dev_err(&port
->dd
->pdev
->dev
,
447 "HBA reset escalation failed.\n");
449 /* 30 ms delay before com reset to quiesce chip */
453 dev_warn(&port
->dd
->pdev
->dev
, "Issuing COM reset\n");
456 writel(readl(port
->mmio
+ PORT_SCR_CTL
) |
457 1, port
->mmio
+ PORT_SCR_CTL
);
458 readl(port
->mmio
+ PORT_SCR_CTL
);
460 /* Wait 1 ms to quiesce chip function */
461 timeout
= jiffies
+ msecs_to_jiffies(1);
462 while (time_before(jiffies
, timeout
))
465 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
468 /* Clear PxSCTL.DET */
469 writel(readl(port
->mmio
+ PORT_SCR_CTL
) & ~1,
470 port
->mmio
+ PORT_SCR_CTL
);
471 readl(port
->mmio
+ PORT_SCR_CTL
);
473 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
474 timeout
= jiffies
+ msecs_to_jiffies(500);
475 while (((readl(port
->mmio
+ PORT_SCR_STAT
) & 0x01) == 0)
476 && time_before(jiffies
, timeout
))
479 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
482 if ((readl(port
->mmio
+ PORT_SCR_STAT
) & 0x01) == 0)
483 dev_warn(&port
->dd
->pdev
->dev
,
484 "COM reset failed\n");
486 mtip_init_port(port
);
487 mtip_start_port(port
);
491 static int mtip_device_reset(struct driver_data
*dd
)
495 if (mtip_check_surprise_removal(dd
->pdev
))
498 if (mtip_hba_reset(dd
) < 0)
502 mtip_init_port(dd
->port
);
503 mtip_start_port(dd
->port
);
505 /* Enable interrupts on the HBA. */
506 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
507 dd
->mmio
+ HOST_CTL
);
512 * Helper function for tag logging
514 static void print_tags(struct driver_data
*dd
,
516 unsigned long *tagbits
,
519 unsigned char tagmap
[128];
520 int group
, tagmap_len
= 0;
522 memset(tagmap
, 0, sizeof(tagmap
));
523 for (group
= SLOTBITS_IN_LONGS
; group
> 0; group
--)
524 tagmap_len
+= sprintf(tagmap
+ tagmap_len
, "%016lX ",
526 dev_warn(&dd
->pdev
->dev
,
527 "%d command(s) %s: tagmap [%s]", cnt
, msg
, tagmap
);
530 static int mtip_read_log_page(struct mtip_port
*port
, u8 page
, u16
*buffer
,
531 dma_addr_t buffer_dma
, unsigned int sectors
);
532 static int mtip_get_smart_attr(struct mtip_port
*port
, unsigned int id
,
533 struct smart_attr
*attrib
);
535 static void mtip_complete_command(struct mtip_cmd
*cmd
, blk_status_t status
)
537 struct request
*req
= blk_mq_rq_from_pdu(cmd
);
539 cmd
->status
= status
;
540 blk_mq_complete_request(req
);
546 * @dd Pointer to the DRIVER_DATA structure.
551 static void mtip_handle_tfe(struct driver_data
*dd
)
553 int group
, tag
, bit
, reissue
, rv
;
554 struct mtip_port
*port
;
555 struct mtip_cmd
*cmd
;
557 struct host_to_dev_fis
*fis
;
558 unsigned long tagaccum
[SLOTBITS_IN_LONGS
];
559 unsigned int cmd_cnt
= 0;
561 char *fail_reason
= NULL
;
562 int fail_all_ncq_write
= 0, fail_all_ncq_cmds
= 0;
564 dev_warn(&dd
->pdev
->dev
, "Taskfile error\n");
568 if (test_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
)) {
569 cmd
= mtip_cmd_from_tag(dd
, MTIP_TAG_INTERNAL
);
570 dbg_printk(MTIP_DRV_NAME
" TFE for the internal command\n");
571 mtip_complete_command(cmd
, BLK_STS_IOERR
);
575 /* clear the tag accumulator */
576 memset(tagaccum
, 0, SLOTBITS_IN_LONGS
* sizeof(long));
578 /* Loop through all the groups */
579 for (group
= 0; group
< dd
->slot_groups
; group
++) {
580 completed
= readl(port
->completed
[group
]);
582 dev_warn(&dd
->pdev
->dev
, "g=%u, comp=%x\n", group
, completed
);
584 /* clear completed status register in the hardware.*/
585 writel(completed
, port
->completed
[group
]);
587 /* Process successfully completed commands */
588 for (bit
= 0; bit
< 32 && completed
; bit
++) {
589 if (!(completed
& (1<<bit
)))
591 tag
= (group
<< 5) + bit
;
593 /* Skip the internal command slot */
594 if (tag
== MTIP_TAG_INTERNAL
)
597 cmd
= mtip_cmd_from_tag(dd
, tag
);
598 mtip_complete_command(cmd
, 0);
599 set_bit(tag
, tagaccum
);
604 print_tags(dd
, "completed (TFE)", tagaccum
, cmd_cnt
);
606 /* Restart the port */
608 mtip_restart_port(port
);
610 /* Trying to determine the cause of the error */
611 rv
= mtip_read_log_page(dd
->port
, ATA_LOG_SATA_NCQ
,
613 dd
->port
->log_buf_dma
, 1);
615 dev_warn(&dd
->pdev
->dev
,
616 "Error in READ LOG EXT (10h) command\n");
617 /* non-critical error, don't fail the load */
619 buf
= (unsigned char *)dd
->port
->log_buf
;
620 if (buf
[259] & 0x1) {
621 dev_info(&dd
->pdev
->dev
,
622 "Write protect bit is set.\n");
623 set_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
);
624 fail_all_ncq_write
= 1;
625 fail_reason
= "write protect";
627 if (buf
[288] == 0xF7) {
628 dev_info(&dd
->pdev
->dev
,
629 "Exceeded Tmax, drive in thermal shutdown.\n");
630 set_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
);
631 fail_all_ncq_cmds
= 1;
632 fail_reason
= "thermal shutdown";
634 if (buf
[288] == 0xBF) {
635 set_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
);
636 dev_info(&dd
->pdev
->dev
,
637 "Drive indicates rebuild has failed. Secure erase required.\n");
638 fail_all_ncq_cmds
= 1;
639 fail_reason
= "rebuild failed";
643 /* clear the tag accumulator */
644 memset(tagaccum
, 0, SLOTBITS_IN_LONGS
* sizeof(long));
646 /* Loop through all the groups */
647 for (group
= 0; group
< dd
->slot_groups
; group
++) {
648 for (bit
= 0; bit
< 32; bit
++) {
650 tag
= (group
<< 5) + bit
;
651 cmd
= mtip_cmd_from_tag(dd
, tag
);
653 fis
= (struct host_to_dev_fis
*)cmd
->command
;
655 /* Should re-issue? */
656 if (tag
== MTIP_TAG_INTERNAL
||
657 fis
->command
== ATA_CMD_SET_FEATURES
)
660 if (fail_all_ncq_cmds
||
661 (fail_all_ncq_write
&&
662 fis
->command
== ATA_CMD_FPDMA_WRITE
)) {
663 dev_warn(&dd
->pdev
->dev
,
664 " Fail: %s w/tag %d [%s].\n",
665 fis
->command
== ATA_CMD_FPDMA_WRITE
?
668 fail_reason
!= NULL
?
669 fail_reason
: "unknown");
670 mtip_complete_command(cmd
, BLK_STS_MEDIUM
);
676 * First check if this command has
677 * exceeded its retries.
679 if (reissue
&& (cmd
->retries
-- > 0)) {
681 set_bit(tag
, tagaccum
);
683 /* Re-issue the command. */
684 mtip_issue_ncq_command(port
, tag
);
689 /* Retire a command that will not be reissued */
690 dev_warn(&port
->dd
->pdev
->dev
,
691 "retiring tag %d\n", tag
);
693 mtip_complete_command(cmd
, BLK_STS_IOERR
);
696 print_tags(dd
, "reissued (TFE)", tagaccum
, cmd_cnt
);
700 * Handle a set device bits interrupt
702 static inline void mtip_workq_sdbfx(struct mtip_port
*port
, int group
,
705 struct driver_data
*dd
= port
->dd
;
707 struct mtip_cmd
*command
;
710 WARN_ON_ONCE(!completed
);
713 /* clear completed status register in the hardware.*/
714 writel(completed
, port
->completed
[group
]);
716 /* Process completed commands. */
717 for (bit
= 0; (bit
< 32) && completed
; bit
++) {
718 if (completed
& 0x01) {
719 tag
= (group
<< 5) | bit
;
721 /* skip internal command slot. */
722 if (unlikely(tag
== MTIP_TAG_INTERNAL
))
725 command
= mtip_cmd_from_tag(dd
, tag
);
726 mtip_complete_command(command
, 0);
731 /* If last, re-enable interrupts */
732 if (atomic_dec_return(&dd
->irq_workers_active
) == 0)
733 writel(0xffffffff, dd
->mmio
+ HOST_IRQ_STAT
);
737 * Process legacy pio and d2h interrupts
739 static inline void mtip_process_legacy(struct driver_data
*dd
, u32 port_stat
)
741 struct mtip_port
*port
= dd
->port
;
742 struct mtip_cmd
*cmd
= mtip_cmd_from_tag(dd
, MTIP_TAG_INTERNAL
);
744 if (test_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
) && cmd
) {
745 int group
= MTIP_TAG_INDEX(MTIP_TAG_INTERNAL
);
746 int status
= readl(port
->cmd_issue
[group
]);
748 if (!(status
& (1 << MTIP_TAG_BIT(MTIP_TAG_INTERNAL
))))
749 mtip_complete_command(cmd
, 0);
754 * Demux and handle errors
756 static inline void mtip_process_errors(struct driver_data
*dd
, u32 port_stat
)
758 if (unlikely(port_stat
& PORT_IRQ_CONNECT
)) {
759 dev_warn(&dd
->pdev
->dev
,
760 "Clearing PxSERR.DIAG.x\n");
761 writel((1 << 26), dd
->port
->mmio
+ PORT_SCR_ERR
);
764 if (unlikely(port_stat
& PORT_IRQ_PHYRDY
)) {
765 dev_warn(&dd
->pdev
->dev
,
766 "Clearing PxSERR.DIAG.n\n");
767 writel((1 << 16), dd
->port
->mmio
+ PORT_SCR_ERR
);
770 if (unlikely(port_stat
& ~PORT_IRQ_HANDLED
)) {
771 dev_warn(&dd
->pdev
->dev
,
772 "Port stat errors %x unhandled\n",
773 (port_stat
& ~PORT_IRQ_HANDLED
));
774 if (mtip_check_surprise_removal(dd
->pdev
))
777 if (likely(port_stat
& (PORT_IRQ_TF_ERR
| PORT_IRQ_IF_ERR
))) {
778 set_bit(MTIP_PF_EH_ACTIVE_BIT
, &dd
->port
->flags
);
779 wake_up_interruptible(&dd
->port
->svc_wait
);
783 static inline irqreturn_t
mtip_handle_irq(struct driver_data
*data
)
785 struct driver_data
*dd
= (struct driver_data
*) data
;
786 struct mtip_port
*port
= dd
->port
;
787 u32 hba_stat
, port_stat
;
789 int do_irq_enable
= 1, i
, workers
;
790 struct mtip_work
*twork
;
792 hba_stat
= readl(dd
->mmio
+ HOST_IRQ_STAT
);
796 /* Acknowledge the interrupt status on the port.*/
797 port_stat
= readl(port
->mmio
+ PORT_IRQ_STAT
);
798 if (unlikely(port_stat
== 0xFFFFFFFF)) {
799 mtip_check_surprise_removal(dd
->pdev
);
802 writel(port_stat
, port
->mmio
+ PORT_IRQ_STAT
);
804 /* Demux port status */
805 if (likely(port_stat
& PORT_IRQ_SDB_FIS
)) {
807 WARN_ON_ONCE(atomic_read(&dd
->irq_workers_active
) != 0);
809 /* Start at 1: group zero is always local? */
810 for (i
= 0, workers
= 0; i
< MTIP_MAX_SLOT_GROUPS
;
812 twork
= &dd
->work
[i
];
813 twork
->completed
= readl(port
->completed
[i
]);
814 if (twork
->completed
)
818 atomic_set(&dd
->irq_workers_active
, workers
);
820 for (i
= 1; i
< MTIP_MAX_SLOT_GROUPS
; i
++) {
821 twork
= &dd
->work
[i
];
822 if (twork
->completed
)
829 if (likely(dd
->work
[0].completed
))
830 mtip_workq_sdbfx(port
, 0,
831 dd
->work
[0].completed
);
835 * Chip quirk: SDB interrupt but nothing
842 if (unlikely(port_stat
& PORT_IRQ_ERR
)) {
843 if (unlikely(mtip_check_surprise_removal(dd
->pdev
))) {
844 /* don't proceed further */
847 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
851 mtip_process_errors(dd
, port_stat
& PORT_IRQ_ERR
);
854 if (unlikely(port_stat
& PORT_IRQ_LEGACY
))
855 mtip_process_legacy(dd
, port_stat
& PORT_IRQ_LEGACY
);
858 /* acknowledge interrupt */
859 if (unlikely(do_irq_enable
))
860 writel(hba_stat
, dd
->mmio
+ HOST_IRQ_STAT
);
866 * HBA interrupt subroutine.
869 * @instance Pointer to the driver data structure.
872 * IRQ_HANDLED A HBA interrupt was pending and handled.
873 * IRQ_NONE This interrupt was not for the HBA.
875 static irqreturn_t
mtip_irq_handler(int irq
, void *instance
)
877 struct driver_data
*dd
= instance
;
879 return mtip_handle_irq(dd
);
882 static void mtip_issue_non_ncq_command(struct mtip_port
*port
, int tag
)
884 writel(1 << MTIP_TAG_BIT(tag
), port
->cmd_issue
[MTIP_TAG_INDEX(tag
)]);
887 static bool mtip_pause_ncq(struct mtip_port
*port
,
888 struct host_to_dev_fis
*fis
)
890 struct host_to_dev_fis
*reply
;
891 unsigned long task_file_data
;
893 reply
= port
->rxfis
+ RX_FIS_D2H_REG
;
894 task_file_data
= readl(port
->mmio
+PORT_TFDATA
);
896 if ((task_file_data
& 1))
899 if (fis
->command
== ATA_CMD_SEC_ERASE_PREP
) {
900 port
->ic_pause_timer
= jiffies
;
902 } else if ((fis
->command
== ATA_CMD_DOWNLOAD_MICRO
) &&
903 (fis
->features
== 0x03)) {
904 set_bit(MTIP_PF_DM_ACTIVE_BIT
, &port
->flags
);
905 port
->ic_pause_timer
= jiffies
;
907 } else if ((fis
->command
== ATA_CMD_SEC_ERASE_UNIT
) ||
908 ((fis
->command
== 0xFC) &&
909 (fis
->features
== 0x27 || fis
->features
== 0x72 ||
910 fis
->features
== 0x62 || fis
->features
== 0x26))) {
911 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
912 clear_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &port
->dd
->dd_flag
);
913 /* Com reset after secure erase or lowlevel format */
914 mtip_restart_port(port
);
915 clear_bit(MTIP_PF_SE_ACTIVE_BIT
, &port
->flags
);
922 static bool mtip_commands_active(struct mtip_port
*port
)
928 * Ignore s_active bit 0 of array element 0.
929 * This bit will always be set
931 active
= readl(port
->s_active
[0]) & 0xFFFFFFFE;
932 for (n
= 1; n
< port
->dd
->slot_groups
; n
++)
933 active
|= readl(port
->s_active
[n
]);
939 * Wait for port to quiesce
941 * @port Pointer to port data structure
942 * @timeout Max duration to wait (ms)
946 * -EBUSY Commands still active
948 static int mtip_quiesce_io(struct mtip_port
*port
, unsigned long timeout
)
953 blk_mq_stop_hw_queues(port
->dd
->queue
);
955 to
= jiffies
+ msecs_to_jiffies(timeout
);
957 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
) &&
958 test_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
)) {
960 continue; /* svc thd is actively issuing commands */
965 if (mtip_check_surprise_removal(port
->dd
->pdev
))
968 active
= mtip_commands_active(port
);
971 } while (time_before(jiffies
, to
));
973 blk_mq_start_stopped_hw_queues(port
->dd
->queue
, true);
974 return active
? -EBUSY
: 0;
976 blk_mq_start_stopped_hw_queues(port
->dd
->queue
, true);
980 struct mtip_int_cmd
{
988 * Execute an internal command and wait for the completion.
990 * @port Pointer to the port data structure.
991 * @fis Pointer to the FIS that describes the command.
992 * @fis_len Length in WORDS of the FIS.
993 * @buffer DMA accessible for command data.
994 * @buf_len Length, in bytes, of the data buffer.
995 * @opts Command header options, excluding the FIS length
996 * and the number of PRD entries.
997 * @timeout Time in ms to wait for the command to complete.
1000 * 0 Command completed successfully.
1001 * -EFAULT The buffer address is not correctly aligned.
1002 * -EBUSY Internal command or other IO in progress.
1003 * -EAGAIN Time out waiting for command to complete.
1005 static int mtip_exec_internal_command(struct mtip_port
*port
,
1006 struct host_to_dev_fis
*fis
,
1011 unsigned long timeout
)
1013 struct mtip_cmd
*int_cmd
;
1014 struct driver_data
*dd
= port
->dd
;
1016 struct mtip_int_cmd icmd
= {
1023 unsigned long start
;
1025 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1026 if (buffer
& 0x00000007) {
1027 dev_err(&dd
->pdev
->dev
, "SG buffer is not 8 byte aligned\n");
1031 int_cmd
= mtip_get_int_command(dd
);
1033 dbg_printk(MTIP_DRV_NAME
"Unable to allocate tag for PIO cmd\n");
1036 rq
= blk_mq_rq_from_pdu(int_cmd
);
1037 rq
->special
= &icmd
;
1039 set_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1041 if (fis
->command
== ATA_CMD_SEC_ERASE_PREP
)
1042 set_bit(MTIP_PF_SE_ACTIVE_BIT
, &port
->flags
);
1044 clear_bit(MTIP_PF_DM_ACTIVE_BIT
, &port
->flags
);
1046 if (fis
->command
!= ATA_CMD_STANDBYNOW1
) {
1047 /* wait for io to complete if non atomic */
1048 if (mtip_quiesce_io(port
, MTIP_QUIESCE_IO_TIMEOUT_MS
) < 0) {
1049 dev_warn(&dd
->pdev
->dev
, "Failed to quiesce IO\n");
1050 blk_mq_free_request(rq
);
1051 clear_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1052 wake_up_interruptible(&port
->svc_wait
);
1057 /* Copy the command to the command table */
1058 memcpy(int_cmd
->command
, fis
, fis_len
*4);
1061 rq
->timeout
= timeout
;
1063 /* insert request and run queue */
1064 blk_execute_rq(rq
->q
, NULL
, rq
, true);
1066 rv
= int_cmd
->status
;
1068 if (rv
== -ERESTARTSYS
) { /* interrupted */
1069 dev_err(&dd
->pdev
->dev
,
1070 "Internal command [%02X] was interrupted after %u ms\n",
1072 jiffies_to_msecs(jiffies
- start
));
1075 } else if (rv
== 0) /* timeout */
1076 dev_err(&dd
->pdev
->dev
,
1077 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1078 fis
->command
, timeout
);
1080 dev_err(&dd
->pdev
->dev
,
1081 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1082 fis
->command
, rv
, timeout
);
1084 if (mtip_check_surprise_removal(dd
->pdev
) ||
1085 test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
1087 dev_err(&dd
->pdev
->dev
,
1088 "Internal command [%02X] wait returned due to SR\n",
1093 mtip_device_reset(dd
); /* recover from timeout issue */
1098 if (readl(port
->cmd_issue
[MTIP_TAG_INDEX(MTIP_TAG_INTERNAL
)])
1099 & (1 << MTIP_TAG_BIT(MTIP_TAG_INTERNAL
))) {
1101 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)) {
1102 mtip_device_reset(dd
);
1107 /* Clear the allocated and active bits for the internal command. */
1108 blk_mq_free_request(rq
);
1109 clear_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1110 if (rv
>= 0 && mtip_pause_ncq(port
, fis
)) {
1114 wake_up_interruptible(&port
->svc_wait
);
1120 * Byte-swap ATA ID strings.
1122 * ATA identify data contains strings in byte-swapped 16-bit words.
1123 * They must be swapped (on all architectures) to be usable as C strings.
1124 * This function swaps bytes in-place.
1126 * @buf The buffer location of the string
1127 * @len The number of bytes to swap
1132 static inline void ata_swap_string(u16
*buf
, unsigned int len
)
1135 for (i
= 0; i
< (len
/2); i
++)
1136 be16_to_cpus(&buf
[i
]);
1139 static void mtip_set_timeout(struct driver_data
*dd
,
1140 struct host_to_dev_fis
*fis
,
1141 unsigned int *timeout
, u8 erasemode
)
1143 switch (fis
->command
) {
1144 case ATA_CMD_DOWNLOAD_MICRO
:
1145 *timeout
= 120000; /* 2 minutes */
1147 case ATA_CMD_SEC_ERASE_UNIT
:
1150 *timeout
= ((*(dd
->port
->identify
+ 90) * 2) * 60000);
1152 *timeout
= ((*(dd
->port
->identify
+ 89) * 2) * 60000);
1154 case ATA_CMD_STANDBYNOW1
:
1155 *timeout
= 120000; /* 2 minutes */
1159 *timeout
= 60000; /* 60 seconds */
1162 *timeout
= 15000; /* 15 seconds */
1165 *timeout
= MTIP_IOCTL_CMD_TIMEOUT_MS
;
1171 * Request the device identity information.
1173 * If a user space buffer is not specified, i.e. is NULL, the
1174 * identify information is still read from the drive and placed
1175 * into the identify data buffer (@e port->identify) in the
1176 * port data structure.
1177 * When the identify buffer contains valid identify information @e
1178 * port->identify_valid is non-zero.
1180 * @port Pointer to the port structure.
1181 * @user_buffer A user space buffer where the identify data should be
1185 * 0 Command completed successfully.
1186 * -EFAULT An error occurred while coping data to the user buffer.
1187 * -1 Command failed.
1189 static int mtip_get_identify(struct mtip_port
*port
, void __user
*user_buffer
)
1192 struct host_to_dev_fis fis
;
1194 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
1197 /* Build the FIS. */
1198 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1201 fis
.command
= ATA_CMD_ID_ATA
;
1203 /* Set the identify information as invalid. */
1204 port
->identify_valid
= 0;
1206 /* Clear the identify information. */
1207 memset(port
->identify
, 0, sizeof(u16
) * ATA_ID_WORDS
);
1209 /* Execute the command. */
1210 if (mtip_exec_internal_command(port
,
1214 sizeof(u16
) * ATA_ID_WORDS
,
1216 MTIP_INT_CMD_TIMEOUT_MS
)
1223 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1224 * perform field-sensitive swapping on the string fields.
1225 * See the kernel use of ata_id_string() for proof of this.
1227 #ifdef __LITTLE_ENDIAN
1228 ata_swap_string(port
->identify
+ 27, 40); /* model string*/
1229 ata_swap_string(port
->identify
+ 23, 8); /* firmware string*/
1230 ata_swap_string(port
->identify
+ 10, 20); /* serial# string*/
1234 for (i
= 0; i
< ATA_ID_WORDS
; i
++)
1235 port
->identify
[i
] = le16_to_cpu(port
->identify
[i
]);
1239 /* Check security locked state */
1240 if (port
->identify
[128] & 0x4)
1241 set_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
1243 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
1245 #ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
1246 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1247 if (port
->identify
[69] & (1 << 14) && port
->identify
[69] & (1 << 5))
1248 port
->dd
->trim_supp
= true;
1251 port
->dd
->trim_supp
= false;
1253 /* Set the identify buffer as valid. */
1254 port
->identify_valid
= 1;
1260 ATA_ID_WORDS
* sizeof(u16
))) {
1271 * Issue a standby immediate command to the device.
1273 * @port Pointer to the port structure.
1276 * 0 Command was executed successfully.
1277 * -1 An error occurred while executing the command.
1279 static int mtip_standby_immediate(struct mtip_port
*port
)
1282 struct host_to_dev_fis fis
;
1283 unsigned long start
;
1284 unsigned int timeout
;
1286 /* Build the FIS. */
1287 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1290 fis
.command
= ATA_CMD_STANDBYNOW1
;
1292 mtip_set_timeout(port
->dd
, &fis
, &timeout
, 0);
1295 rv
= mtip_exec_internal_command(port
,
1302 dbg_printk(MTIP_DRV_NAME
"Time taken to complete standby cmd: %d ms\n",
1303 jiffies_to_msecs(jiffies
- start
));
1305 dev_warn(&port
->dd
->pdev
->dev
,
1306 "STANDBY IMMEDIATE command failed.\n");
1312 * Issue a READ LOG EXT command to the device.
1314 * @port pointer to the port structure.
1315 * @page page number to fetch
1316 * @buffer pointer to buffer
1317 * @buffer_dma dma address corresponding to @buffer
1318 * @sectors page length to fetch, in sectors
1321 * @rv return value from mtip_exec_internal_command()
1323 static int mtip_read_log_page(struct mtip_port
*port
, u8 page
, u16
*buffer
,
1324 dma_addr_t buffer_dma
, unsigned int sectors
)
1326 struct host_to_dev_fis fis
;
1328 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1331 fis
.command
= ATA_CMD_READ_LOG_EXT
;
1332 fis
.sect_count
= sectors
& 0xFF;
1333 fis
.sect_cnt_ex
= (sectors
>> 8) & 0xFF;
1336 fis
.device
= ATA_DEVICE_OBS
;
1338 memset(buffer
, 0, sectors
* ATA_SECT_SIZE
);
1340 return mtip_exec_internal_command(port
,
1344 sectors
* ATA_SECT_SIZE
,
1346 MTIP_INT_CMD_TIMEOUT_MS
);
1350 * Issue a SMART READ DATA command to the device.
1352 * @port pointer to the port structure.
1353 * @buffer pointer to buffer
1354 * @buffer_dma dma address corresponding to @buffer
1357 * @rv return value from mtip_exec_internal_command()
1359 static int mtip_get_smart_data(struct mtip_port
*port
, u8
*buffer
,
1360 dma_addr_t buffer_dma
)
1362 struct host_to_dev_fis fis
;
1364 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1367 fis
.command
= ATA_CMD_SMART
;
1368 fis
.features
= 0xD0;
1372 fis
.device
= ATA_DEVICE_OBS
;
1374 return mtip_exec_internal_command(port
,
1384 * Get the value of a smart attribute
1386 * @port pointer to the port structure
1387 * @id attribute number
1388 * @attrib pointer to return attrib information corresponding to @id
1391 * -EINVAL NULL buffer passed or unsupported attribute @id.
1392 * -EPERM Identify data not valid, SMART not supported or not enabled
1394 static int mtip_get_smart_attr(struct mtip_port
*port
, unsigned int id
,
1395 struct smart_attr
*attrib
)
1398 struct smart_attr
*pattr
;
1403 if (!port
->identify_valid
) {
1404 dev_warn(&port
->dd
->pdev
->dev
, "IDENTIFY DATA not valid\n");
1407 if (!(port
->identify
[82] & 0x1)) {
1408 dev_warn(&port
->dd
->pdev
->dev
, "SMART not supported\n");
1411 if (!(port
->identify
[85] & 0x1)) {
1412 dev_warn(&port
->dd
->pdev
->dev
, "SMART not enabled\n");
1416 memset(port
->smart_buf
, 0, ATA_SECT_SIZE
);
1417 rv
= mtip_get_smart_data(port
, port
->smart_buf
, port
->smart_buf_dma
);
1419 dev_warn(&port
->dd
->pdev
->dev
, "Failed to ge SMART data\n");
1423 pattr
= (struct smart_attr
*)(port
->smart_buf
+ 2);
1424 for (i
= 0; i
< 29; i
++, pattr
++)
1425 if (pattr
->attr_id
== id
) {
1426 memcpy(attrib
, pattr
, sizeof(struct smart_attr
));
1431 dev_warn(&port
->dd
->pdev
->dev
,
1432 "Query for invalid SMART attribute ID\n");
1440 * Trim unused sectors
1442 * @dd pointer to driver_data structure
1444 * @len # of 512b sectors to trim
1447 * -ENOMEM Out of dma memory
1448 * -EINVAL Invalid parameters passed in, trim not supported
1449 * -EIO Error submitting trim request to hw
1451 static int mtip_send_trim(struct driver_data
*dd
, unsigned int lba
,
1455 u64 tlba
, tlen
, sect_left
;
1456 struct mtip_trim_entry
*buf
;
1457 dma_addr_t dma_addr
;
1458 struct host_to_dev_fis fis
;
1460 if (!len
|| dd
->trim_supp
== false)
1463 /* Trim request too big */
1464 WARN_ON(len
> (MTIP_MAX_TRIM_ENTRY_LEN
* MTIP_MAX_TRIM_ENTRIES
));
1466 /* Trim request not aligned on 4k boundary */
1467 WARN_ON(len
% 8 != 0);
1469 /* Warn if vu_trim structure is too big */
1470 WARN_ON(sizeof(struct mtip_trim
) > ATA_SECT_SIZE
);
1472 /* Allocate a DMA buffer for the trim structure */
1473 buf
= dmam_alloc_coherent(&dd
->pdev
->dev
, ATA_SECT_SIZE
, &dma_addr
,
1477 memset(buf
, 0, ATA_SECT_SIZE
);
1479 for (i
= 0, sect_left
= len
, tlba
= lba
;
1480 i
< MTIP_MAX_TRIM_ENTRIES
&& sect_left
;
1482 tlen
= (sect_left
>= MTIP_MAX_TRIM_ENTRY_LEN
?
1483 MTIP_MAX_TRIM_ENTRY_LEN
:
1485 buf
[i
].lba
= __force_bit2int
cpu_to_le32(tlba
);
1486 buf
[i
].range
= __force_bit2int
cpu_to_le16(tlen
);
1490 WARN_ON(sect_left
!= 0);
1493 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1497 fis
.features
= 0x60;
1499 fis
.device
= ATA_DEVICE_OBS
;
1501 if (mtip_exec_internal_command(dd
->port
,
1507 MTIP_TRIM_TIMEOUT_MS
) < 0)
1510 dmam_free_coherent(&dd
->pdev
->dev
, ATA_SECT_SIZE
, buf
, dma_addr
);
1515 * Get the drive capacity.
1517 * @dd Pointer to the device data structure.
1518 * @sectors Pointer to the variable that will receive the sector count.
1521 * 1 Capacity was returned successfully.
1522 * 0 The identify information is invalid.
1524 static bool mtip_hw_get_capacity(struct driver_data
*dd
, sector_t
*sectors
)
1526 struct mtip_port
*port
= dd
->port
;
1527 u64 total
, raw0
, raw1
, raw2
, raw3
;
1528 raw0
= port
->identify
[100];
1529 raw1
= port
->identify
[101];
1530 raw2
= port
->identify
[102];
1531 raw3
= port
->identify
[103];
1532 total
= raw0
| raw1
<<16 | raw2
<<32 | raw3
<<48;
1534 return (bool) !!port
->identify_valid
;
1538 * Display the identify command data.
1540 * @port Pointer to the port data structure.
1545 static void mtip_dump_identify(struct mtip_port
*port
)
1548 unsigned short revid
;
1551 if (!port
->identify_valid
)
1554 strlcpy(cbuf
, (char *)(port
->identify
+10), 21);
1555 dev_info(&port
->dd
->pdev
->dev
,
1556 "Serial No.: %s\n", cbuf
);
1558 strlcpy(cbuf
, (char *)(port
->identify
+23), 9);
1559 dev_info(&port
->dd
->pdev
->dev
,
1560 "Firmware Ver.: %s\n", cbuf
);
1562 strlcpy(cbuf
, (char *)(port
->identify
+27), 41);
1563 dev_info(&port
->dd
->pdev
->dev
, "Model: %s\n", cbuf
);
1565 dev_info(&port
->dd
->pdev
->dev
, "Security: %04x %s\n",
1566 port
->identify
[128],
1567 port
->identify
[128] & 0x4 ? "(LOCKED)" : "");
1569 if (mtip_hw_get_capacity(port
->dd
, §ors
))
1570 dev_info(&port
->dd
->pdev
->dev
,
1571 "Capacity: %llu sectors (%llu MB)\n",
1573 ((u64
)sectors
) * ATA_SECT_SIZE
>> 20);
1575 pci_read_config_word(port
->dd
->pdev
, PCI_REVISION_ID
, &revid
);
1576 switch (revid
& 0xFF) {
1578 strlcpy(cbuf
, "A0", 3);
1581 strlcpy(cbuf
, "A2", 3);
1584 strlcpy(cbuf
, "?", 2);
1587 dev_info(&port
->dd
->pdev
->dev
,
1588 "Card Type: %s\n", cbuf
);
1592 * Map the commands scatter list into the command table.
1594 * @command Pointer to the command.
1595 * @nents Number of scatter list entries.
1600 static inline void fill_command_sg(struct driver_data
*dd
,
1601 struct mtip_cmd
*command
,
1605 unsigned int dma_len
;
1606 struct mtip_cmd_sg
*command_sg
;
1607 struct scatterlist
*sg
= command
->sg
;
1609 command_sg
= command
->command
+ AHCI_CMD_TBL_HDR_SZ
;
1611 for (n
= 0; n
< nents
; n
++) {
1612 dma_len
= sg_dma_len(sg
);
1613 if (dma_len
> 0x400000)
1614 dev_err(&dd
->pdev
->dev
,
1615 "DMA segment length truncated\n");
1616 command_sg
->info
= __force_bit2int
1617 cpu_to_le32((dma_len
-1) & 0x3FFFFF);
1618 command_sg
->dba
= __force_bit2int
1619 cpu_to_le32(sg_dma_address(sg
));
1620 command_sg
->dba_upper
= __force_bit2int
1621 cpu_to_le32((sg_dma_address(sg
) >> 16) >> 16);
1628 * @brief Execute a drive command.
1630 * return value 0 The command completed successfully.
1631 * return value -1 An error occurred while executing the command.
1633 static int exec_drive_task(struct mtip_port
*port
, u8
*command
)
1635 struct host_to_dev_fis fis
;
1636 struct host_to_dev_fis
*reply
= (port
->rxfis
+ RX_FIS_D2H_REG
);
1639 /* Build the FIS. */
1640 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1643 fis
.command
= command
[0];
1644 fis
.features
= command
[1];
1645 fis
.sect_count
= command
[2];
1646 fis
.sector
= command
[3];
1647 fis
.cyl_low
= command
[4];
1648 fis
.cyl_hi
= command
[5];
1649 fis
.device
= command
[6] & ~0x10; /* Clear the dev bit*/
1651 mtip_set_timeout(port
->dd
, &fis
, &to
, 0);
1653 dbg_printk(MTIP_DRV_NAME
" %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
1663 /* Execute the command. */
1664 if (mtip_exec_internal_command(port
,
1674 command
[0] = reply
->command
; /* Status*/
1675 command
[1] = reply
->features
; /* Error*/
1676 command
[4] = reply
->cyl_low
;
1677 command
[5] = reply
->cyl_hi
;
1679 dbg_printk(MTIP_DRV_NAME
" %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
1690 * @brief Execute a drive command.
1692 * @param port Pointer to the port data structure.
1693 * @param command Pointer to the user specified command parameters.
1694 * @param user_buffer Pointer to the user space buffer where read sector
1695 * data should be copied.
1697 * return value 0 The command completed successfully.
1698 * return value -EFAULT An error occurred while copying the completion
1699 * data to the user space buffer.
1700 * return value -1 An error occurred while executing the command.
1702 static int exec_drive_command(struct mtip_port
*port
, u8
*command
,
1703 void __user
*user_buffer
)
1705 struct host_to_dev_fis fis
;
1706 struct host_to_dev_fis
*reply
;
1708 dma_addr_t dma_addr
= 0;
1709 int rv
= 0, xfer_sz
= command
[3];
1716 buf
= dmam_alloc_coherent(&port
->dd
->pdev
->dev
,
1717 ATA_SECT_SIZE
* xfer_sz
,
1721 dev_err(&port
->dd
->pdev
->dev
,
1722 "Memory allocation failed (%d bytes)\n",
1723 ATA_SECT_SIZE
* xfer_sz
);
1726 memset(buf
, 0, ATA_SECT_SIZE
* xfer_sz
);
1729 /* Build the FIS. */
1730 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1733 fis
.command
= command
[0];
1734 fis
.features
= command
[2];
1735 fis
.sect_count
= command
[3];
1736 if (fis
.command
== ATA_CMD_SMART
) {
1737 fis
.sector
= command
[1];
1742 mtip_set_timeout(port
->dd
, &fis
, &to
, 0);
1745 reply
= (port
->rxfis
+ RX_FIS_PIO_SETUP
);
1747 reply
= (port
->rxfis
+ RX_FIS_D2H_REG
);
1749 dbg_printk(MTIP_DRV_NAME
1750 " %s: User Command: cmd %x, sect %x, "
1751 "feat %x, sectcnt %x\n",
1758 /* Execute the command. */
1759 if (mtip_exec_internal_command(port
,
1762 (xfer_sz
? dma_addr
: 0),
1763 (xfer_sz
? ATA_SECT_SIZE
* xfer_sz
: 0),
1768 goto exit_drive_command
;
1771 /* Collect the completion status. */
1772 command
[0] = reply
->command
; /* Status*/
1773 command
[1] = reply
->features
; /* Error*/
1774 command
[2] = reply
->sect_count
;
1776 dbg_printk(MTIP_DRV_NAME
1777 " %s: Completion Status: stat %x, "
1778 "err %x, nsect %x\n",
1785 if (copy_to_user(user_buffer
,
1787 ATA_SECT_SIZE
* command
[3])) {
1789 goto exit_drive_command
;
1794 dmam_free_coherent(&port
->dd
->pdev
->dev
,
1795 ATA_SECT_SIZE
* xfer_sz
, buf
, dma_addr
);
1800 * Indicates whether a command has a single sector payload.
1802 * @command passed to the device to perform the certain event.
1803 * @features passed to the device to perform the certain event.
1806 * 1 command is one that always has a single sector payload,
1807 * regardless of the value in the Sector Count field.
1811 static unsigned int implicit_sector(unsigned char command
,
1812 unsigned char features
)
1814 unsigned int rv
= 0;
1816 /* list of commands that have an implicit sector count of 1 */
1818 case ATA_CMD_SEC_SET_PASS
:
1819 case ATA_CMD_SEC_UNLOCK
:
1820 case ATA_CMD_SEC_ERASE_PREP
:
1821 case ATA_CMD_SEC_ERASE_UNIT
:
1822 case ATA_CMD_SEC_FREEZE_LOCK
:
1823 case ATA_CMD_SEC_DISABLE_PASS
:
1824 case ATA_CMD_PMP_READ
:
1825 case ATA_CMD_PMP_WRITE
:
1828 case ATA_CMD_SET_MAX
:
1829 if (features
== ATA_SET_MAX_UNLOCK
)
1833 if ((features
== ATA_SMART_READ_VALUES
) ||
1834 (features
== ATA_SMART_READ_THRESHOLDS
))
1837 case ATA_CMD_CONF_OVERLAY
:
1838 if ((features
== ATA_DCO_IDENTIFY
) ||
1839 (features
== ATA_DCO_SET
))
1847 * Executes a taskfile
1848 * See ide_taskfile_ioctl() for derivation
1850 static int exec_drive_taskfile(struct driver_data
*dd
,
1852 ide_task_request_t
*req_task
,
1855 struct host_to_dev_fis fis
;
1856 struct host_to_dev_fis
*reply
;
1859 dma_addr_t outbuf_dma
= 0;
1860 dma_addr_t inbuf_dma
= 0;
1861 dma_addr_t dma_buffer
= 0;
1863 unsigned int taskin
= 0;
1864 unsigned int taskout
= 0;
1866 unsigned int timeout
;
1867 unsigned int force_single_sector
;
1868 unsigned int transfer_size
;
1869 unsigned long task_file_data
;
1870 int intotal
= outtotal
+ req_task
->out_size
;
1873 taskout
= req_task
->out_size
;
1874 taskin
= req_task
->in_size
;
1875 /* 130560 = 512 * 0xFF*/
1876 if (taskin
> 130560 || taskout
> 130560)
1880 outbuf
= memdup_user(buf
+ outtotal
, taskout
);
1882 return PTR_ERR(outbuf
);
1884 outbuf_dma
= pci_map_single(dd
->pdev
,
1888 if (pci_dma_mapping_error(dd
->pdev
, outbuf_dma
)) {
1892 dma_buffer
= outbuf_dma
;
1896 inbuf
= memdup_user(buf
+ intotal
, taskin
);
1897 if (IS_ERR(inbuf
)) {
1898 err
= PTR_ERR(inbuf
);
1902 inbuf_dma
= pci_map_single(dd
->pdev
,
1904 taskin
, DMA_FROM_DEVICE
);
1905 if (pci_dma_mapping_error(dd
->pdev
, inbuf_dma
)) {
1909 dma_buffer
= inbuf_dma
;
1912 /* only supports PIO and non-data commands from this ioctl. */
1913 switch (req_task
->data_phase
) {
1915 nsect
= taskout
/ ATA_SECT_SIZE
;
1916 reply
= (dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
);
1919 reply
= (dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
);
1921 case TASKFILE_NO_DATA
:
1922 reply
= (dd
->port
->rxfis
+ RX_FIS_D2H_REG
);
1929 /* Build the FIS. */
1930 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1934 fis
.command
= req_task
->io_ports
[7];
1935 fis
.features
= req_task
->io_ports
[1];
1936 fis
.sect_count
= req_task
->io_ports
[2];
1937 fis
.lba_low
= req_task
->io_ports
[3];
1938 fis
.lba_mid
= req_task
->io_ports
[4];
1939 fis
.lba_hi
= req_task
->io_ports
[5];
1940 /* Clear the dev bit*/
1941 fis
.device
= req_task
->io_ports
[6] & ~0x10;
1943 if ((req_task
->in_flags
.all
== 0) && (req_task
->out_flags
.all
& 1)) {
1944 req_task
->in_flags
.all
=
1945 IDE_TASKFILE_STD_IN_FLAGS
|
1946 (IDE_HOB_STD_IN_FLAGS
<< 8);
1947 fis
.lba_low_ex
= req_task
->hob_ports
[3];
1948 fis
.lba_mid_ex
= req_task
->hob_ports
[4];
1949 fis
.lba_hi_ex
= req_task
->hob_ports
[5];
1950 fis
.features_ex
= req_task
->hob_ports
[1];
1951 fis
.sect_cnt_ex
= req_task
->hob_ports
[2];
1954 req_task
->in_flags
.all
= IDE_TASKFILE_STD_IN_FLAGS
;
1957 force_single_sector
= implicit_sector(fis
.command
, fis
.features
);
1959 if ((taskin
|| taskout
) && (!fis
.sect_count
)) {
1961 fis
.sect_count
= nsect
;
1963 if (!force_single_sector
) {
1964 dev_warn(&dd
->pdev
->dev
,
1965 "data movement but "
1966 "sect_count is 0\n");
1973 dbg_printk(MTIP_DRV_NAME
1974 " %s: cmd %x, feat %x, nsect %x,"
1975 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
1986 /* check for erase mode support during secure erase.*/
1987 if ((fis
.command
== ATA_CMD_SEC_ERASE_UNIT
) && outbuf
&&
1988 (outbuf
[0] & MTIP_SEC_ERASE_MODE
)) {
1992 mtip_set_timeout(dd
, &fis
, &timeout
, erasemode
);
1994 /* Determine the correct transfer size.*/
1995 if (force_single_sector
)
1996 transfer_size
= ATA_SECT_SIZE
;
1998 transfer_size
= ATA_SECT_SIZE
* fis
.sect_count
;
2000 /* Execute the command.*/
2001 if (mtip_exec_internal_command(dd
->port
,
2012 task_file_data
= readl(dd
->port
->mmio
+PORT_TFDATA
);
2014 if ((req_task
->data_phase
== TASKFILE_IN
) && !(task_file_data
& 1)) {
2015 reply
= dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
;
2016 req_task
->io_ports
[7] = reply
->control
;
2018 reply
= dd
->port
->rxfis
+ RX_FIS_D2H_REG
;
2019 req_task
->io_ports
[7] = reply
->command
;
2022 /* reclaim the DMA buffers.*/
2024 pci_unmap_single(dd
->pdev
, inbuf_dma
,
2025 taskin
, DMA_FROM_DEVICE
);
2027 pci_unmap_single(dd
->pdev
, outbuf_dma
,
2028 taskout
, DMA_TO_DEVICE
);
2032 /* return the ATA registers to the caller.*/
2033 req_task
->io_ports
[1] = reply
->features
;
2034 req_task
->io_ports
[2] = reply
->sect_count
;
2035 req_task
->io_ports
[3] = reply
->lba_low
;
2036 req_task
->io_ports
[4] = reply
->lba_mid
;
2037 req_task
->io_ports
[5] = reply
->lba_hi
;
2038 req_task
->io_ports
[6] = reply
->device
;
2040 if (req_task
->out_flags
.all
& 1) {
2042 req_task
->hob_ports
[3] = reply
->lba_low_ex
;
2043 req_task
->hob_ports
[4] = reply
->lba_mid_ex
;
2044 req_task
->hob_ports
[5] = reply
->lba_hi_ex
;
2045 req_task
->hob_ports
[1] = reply
->features_ex
;
2046 req_task
->hob_ports
[2] = reply
->sect_cnt_ex
;
2048 dbg_printk(MTIP_DRV_NAME
2049 " %s: Completion: stat %x,"
2050 "err %x, sect_cnt %x, lbalo %x,"
2051 "lbamid %x, lbahi %x, dev %x\n",
2053 req_task
->io_ports
[7],
2054 req_task
->io_ports
[1],
2055 req_task
->io_ports
[2],
2056 req_task
->io_ports
[3],
2057 req_task
->io_ports
[4],
2058 req_task
->io_ports
[5],
2059 req_task
->io_ports
[6]);
2062 if (copy_to_user(buf
+ outtotal
, outbuf
, taskout
)) {
2068 if (copy_to_user(buf
+ intotal
, inbuf
, taskin
)) {
2075 pci_unmap_single(dd
->pdev
, inbuf_dma
,
2076 taskin
, DMA_FROM_DEVICE
);
2078 pci_unmap_single(dd
->pdev
, outbuf_dma
,
2079 taskout
, DMA_TO_DEVICE
);
2087 * Handle IOCTL calls from the Block Layer.
2089 * This function is called by the Block Layer when it receives an IOCTL
2090 * command that it does not understand. If the IOCTL command is not supported
2091 * this function returns -ENOTTY.
2093 * @dd Pointer to the driver data structure.
2094 * @cmd IOCTL command passed from the Block Layer.
2095 * @arg IOCTL argument passed from the Block Layer.
2098 * 0 The IOCTL completed successfully.
2099 * -ENOTTY The specified command is not supported.
2100 * -EFAULT An error occurred copying data to a user space buffer.
2101 * -EIO An error occurred while executing the command.
2103 static int mtip_hw_ioctl(struct driver_data
*dd
, unsigned int cmd
,
2107 case HDIO_GET_IDENTITY
:
2109 if (copy_to_user((void __user
*)arg
, dd
->port
->identify
,
2110 sizeof(u16
) * ATA_ID_WORDS
))
2114 case HDIO_DRIVE_CMD
:
2116 u8 drive_command
[4];
2118 /* Copy the user command info to our buffer. */
2119 if (copy_from_user(drive_command
,
2120 (void __user
*) arg
,
2121 sizeof(drive_command
)))
2124 /* Execute the drive command. */
2125 if (exec_drive_command(dd
->port
,
2127 (void __user
*) (arg
+4)))
2130 /* Copy the status back to the users buffer. */
2131 if (copy_to_user((void __user
*) arg
,
2133 sizeof(drive_command
)))
2138 case HDIO_DRIVE_TASK
:
2140 u8 drive_command
[7];
2142 /* Copy the user command info to our buffer. */
2143 if (copy_from_user(drive_command
,
2144 (void __user
*) arg
,
2145 sizeof(drive_command
)))
2148 /* Execute the drive command. */
2149 if (exec_drive_task(dd
->port
, drive_command
))
2152 /* Copy the status back to the users buffer. */
2153 if (copy_to_user((void __user
*) arg
,
2155 sizeof(drive_command
)))
2160 case HDIO_DRIVE_TASKFILE
: {
2161 ide_task_request_t req_task
;
2164 if (copy_from_user(&req_task
, (void __user
*) arg
,
2168 outtotal
= sizeof(req_task
);
2170 ret
= exec_drive_taskfile(dd
, (void __user
*) arg
,
2171 &req_task
, outtotal
);
2173 if (copy_to_user((void __user
*) arg
, &req_task
,
2187 * Submit an IO to the hw
2189 * This function is called by the block layer to issue an io
2190 * to the device. Upon completion, the callback function will
2191 * be called with the data parameter passed as the callback data.
2193 * @dd Pointer to the driver data structure.
2194 * @start First sector to read.
2195 * @nsect Number of sectors to read.
2196 * @nents Number of entries in scatter list for the read command.
2197 * @tag The tag of this read command.
2198 * @callback Pointer to the function that should be called
2199 * when the read completes.
2200 * @data Callback data passed to the callback function
2201 * when the read completes.
2202 * @dir Direction (read or write)
2207 static void mtip_hw_submit_io(struct driver_data
*dd
, struct request
*rq
,
2208 struct mtip_cmd
*command
, int nents
,
2209 struct blk_mq_hw_ctx
*hctx
)
2211 struct host_to_dev_fis
*fis
;
2212 struct mtip_port
*port
= dd
->port
;
2213 int dma_dir
= rq_data_dir(rq
) == READ
? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
2214 u64 start
= blk_rq_pos(rq
);
2215 unsigned int nsect
= blk_rq_sectors(rq
);
2217 /* Map the scatter list for DMA access */
2218 nents
= dma_map_sg(&dd
->pdev
->dev
, command
->sg
, nents
, dma_dir
);
2220 prefetch(&port
->flags
);
2222 command
->scatter_ents
= nents
;
2225 * The number of retries for this command before it is
2226 * reported as a failure to the upper layers.
2228 command
->retries
= MTIP_MAX_RETRIES
;
2231 fis
= command
->command
;
2234 if (dma_dir
== DMA_FROM_DEVICE
)
2235 fis
->command
= ATA_CMD_FPDMA_READ
;
2237 fis
->command
= ATA_CMD_FPDMA_WRITE
;
2238 fis
->lba_low
= start
& 0xFF;
2239 fis
->lba_mid
= (start
>> 8) & 0xFF;
2240 fis
->lba_hi
= (start
>> 16) & 0xFF;
2241 fis
->lba_low_ex
= (start
>> 24) & 0xFF;
2242 fis
->lba_mid_ex
= (start
>> 32) & 0xFF;
2243 fis
->lba_hi_ex
= (start
>> 40) & 0xFF;
2244 fis
->device
= 1 << 6;
2245 fis
->features
= nsect
& 0xFF;
2246 fis
->features_ex
= (nsect
>> 8) & 0xFF;
2247 fis
->sect_count
= ((rq
->tag
<< 3) | (rq
->tag
>> 5));
2248 fis
->sect_cnt_ex
= 0;
2252 fill_command_sg(dd
, command
, nents
);
2254 if (unlikely(command
->unaligned
))
2255 fis
->device
|= 1 << 7;
2257 /* Populate the command header */
2258 command
->command_header
->opts
=
2259 __force_bit2int
cpu_to_le32(
2260 (nents
<< 16) | 5 | AHCI_CMD_PREFETCH
);
2261 command
->command_header
->byte_count
= 0;
2263 command
->direction
= dma_dir
;
2266 * To prevent this command from being issued
2267 * if an internal command is in progress or error handling is active.
2269 if (unlikely(port
->flags
& MTIP_PF_PAUSE_IO
)) {
2270 set_bit(rq
->tag
, port
->cmds_to_issue
);
2271 set_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
);
2275 /* Issue the command to the hardware */
2276 mtip_issue_ncq_command(port
, rq
->tag
);
2280 * Sysfs status dump.
2282 * @dev Pointer to the device structure, passed by the kernrel.
2283 * @attr Pointer to the device_attribute structure passed by the kernel.
2284 * @buf Pointer to the char buffer that will receive the stats info.
2287 * The size, in bytes, of the data copied into buf.
2289 static ssize_t
mtip_hw_show_status(struct device
*dev
,
2290 struct device_attribute
*attr
,
2293 struct driver_data
*dd
= dev_to_disk(dev
)->private_data
;
2296 if (test_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
))
2297 size
+= sprintf(buf
, "%s", "thermal_shutdown\n");
2298 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
))
2299 size
+= sprintf(buf
, "%s", "write_protect\n");
2301 size
+= sprintf(buf
, "%s", "online\n");
2306 static DEVICE_ATTR(status
, S_IRUGO
, mtip_hw_show_status
, NULL
);
2308 /* debugsfs entries */
2310 static ssize_t
show_device_status(struct device_driver
*drv
, char *buf
)
2313 struct driver_data
*dd
, *tmp
;
2314 unsigned long flags
;
2318 spin_lock_irqsave(&dev_lock
, flags
);
2319 size
+= sprintf(&buf
[size
], "Devices Present:\n");
2320 list_for_each_entry_safe(dd
, tmp
, &online_list
, online_list
) {
2323 dd
->port
->identify
&&
2324 dd
->port
->identify_valid
) {
2326 (char *) (dd
->port
->identify
+ 10), 21);
2327 status
= *(dd
->port
->identify
+ 141);
2329 memset(id_buf
, 0, 42);
2334 test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
)) {
2335 size
+= sprintf(&buf
[size
],
2336 " device %s %s (ftl rebuild %d %%)\n",
2337 dev_name(&dd
->pdev
->dev
),
2341 size
+= sprintf(&buf
[size
],
2343 dev_name(&dd
->pdev
->dev
),
2349 size
+= sprintf(&buf
[size
], "Devices Being Removed:\n");
2350 list_for_each_entry_safe(dd
, tmp
, &removing_list
, remove_list
) {
2353 dd
->port
->identify
&&
2354 dd
->port
->identify_valid
) {
2356 (char *) (dd
->port
->identify
+10), 21);
2357 status
= *(dd
->port
->identify
+ 141);
2359 memset(id_buf
, 0, 42);
2364 test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
)) {
2365 size
+= sprintf(&buf
[size
],
2366 " device %s %s (ftl rebuild %d %%)\n",
2367 dev_name(&dd
->pdev
->dev
),
2371 size
+= sprintf(&buf
[size
],
2373 dev_name(&dd
->pdev
->dev
),
2378 spin_unlock_irqrestore(&dev_lock
, flags
);
2383 static ssize_t
mtip_hw_read_device_status(struct file
*f
, char __user
*ubuf
,
2384 size_t len
, loff_t
*offset
)
2386 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2391 if (!len
|| *offset
)
2394 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2396 dev_err(&dd
->pdev
->dev
,
2397 "Memory allocation: status buffer\n");
2401 size
+= show_device_status(NULL
, buf
);
2403 *offset
= size
<= len
? size
: len
;
2404 size
= copy_to_user(ubuf
, buf
, *offset
);
2409 return rv
? rv
: *offset
;
2412 static ssize_t
mtip_hw_read_registers(struct file
*f
, char __user
*ubuf
,
2413 size_t len
, loff_t
*offset
)
2415 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2417 u32 group_allocated
;
2424 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2426 dev_err(&dd
->pdev
->dev
,
2427 "Memory allocation: register buffer\n");
2431 size
+= sprintf(&buf
[size
], "H/ S ACTive : [ 0x");
2433 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2434 size
+= sprintf(&buf
[size
], "%08X ",
2435 readl(dd
->port
->s_active
[n
]));
2437 size
+= sprintf(&buf
[size
], "]\n");
2438 size
+= sprintf(&buf
[size
], "H/ Command Issue : [ 0x");
2440 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2441 size
+= sprintf(&buf
[size
], "%08X ",
2442 readl(dd
->port
->cmd_issue
[n
]));
2444 size
+= sprintf(&buf
[size
], "]\n");
2445 size
+= sprintf(&buf
[size
], "H/ Completed : [ 0x");
2447 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2448 size
+= sprintf(&buf
[size
], "%08X ",
2449 readl(dd
->port
->completed
[n
]));
2451 size
+= sprintf(&buf
[size
], "]\n");
2452 size
+= sprintf(&buf
[size
], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2453 readl(dd
->port
->mmio
+ PORT_IRQ_STAT
));
2454 size
+= sprintf(&buf
[size
], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2455 readl(dd
->mmio
+ HOST_IRQ_STAT
));
2456 size
+= sprintf(&buf
[size
], "\n");
2458 size
+= sprintf(&buf
[size
], "L/ Commands in Q : [ 0x");
2460 for (n
= dd
->slot_groups
-1; n
>= 0; n
--) {
2461 if (sizeof(long) > sizeof(u32
))
2463 dd
->port
->cmds_to_issue
[n
/2] >> (32*(n
&1));
2465 group_allocated
= dd
->port
->cmds_to_issue
[n
];
2466 size
+= sprintf(&buf
[size
], "%08X ", group_allocated
);
2468 size
+= sprintf(&buf
[size
], "]\n");
2470 *offset
= size
<= len
? size
: len
;
2471 size
= copy_to_user(ubuf
, buf
, *offset
);
2476 return rv
? rv
: *offset
;
2479 static ssize_t
mtip_hw_read_flags(struct file
*f
, char __user
*ubuf
,
2480 size_t len
, loff_t
*offset
)
2482 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2490 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2492 dev_err(&dd
->pdev
->dev
,
2493 "Memory allocation: flag buffer\n");
2497 size
+= sprintf(&buf
[size
], "Flag-port : [ %08lX ]\n",
2499 size
+= sprintf(&buf
[size
], "Flag-dd : [ %08lX ]\n",
2502 *offset
= size
<= len
? size
: len
;
2503 size
= copy_to_user(ubuf
, buf
, *offset
);
2508 return rv
? rv
: *offset
;
2511 static const struct file_operations mtip_device_status_fops
= {
2512 .owner
= THIS_MODULE
,
2513 .open
= simple_open
,
2514 .read
= mtip_hw_read_device_status
,
2515 .llseek
= no_llseek
,
2518 static const struct file_operations mtip_regs_fops
= {
2519 .owner
= THIS_MODULE
,
2520 .open
= simple_open
,
2521 .read
= mtip_hw_read_registers
,
2522 .llseek
= no_llseek
,
2525 static const struct file_operations mtip_flags_fops
= {
2526 .owner
= THIS_MODULE
,
2527 .open
= simple_open
,
2528 .read
= mtip_hw_read_flags
,
2529 .llseek
= no_llseek
,
2533 * Create the sysfs related attributes.
2535 * @dd Pointer to the driver data structure.
2536 * @kobj Pointer to the kobj for the block device.
2539 * 0 Operation completed successfully.
2540 * -EINVAL Invalid parameter.
2542 static int mtip_hw_sysfs_init(struct driver_data
*dd
, struct kobject
*kobj
)
2547 if (sysfs_create_file(kobj
, &dev_attr_status
.attr
))
2548 dev_warn(&dd
->pdev
->dev
,
2549 "Error creating 'status' sysfs entry\n");
2554 * Remove the sysfs related attributes.
2556 * @dd Pointer to the driver data structure.
2557 * @kobj Pointer to the kobj for the block device.
2560 * 0 Operation completed successfully.
2561 * -EINVAL Invalid parameter.
2563 static int mtip_hw_sysfs_exit(struct driver_data
*dd
, struct kobject
*kobj
)
2568 sysfs_remove_file(kobj
, &dev_attr_status
.attr
);
2573 static int mtip_hw_debugfs_init(struct driver_data
*dd
)
2578 dd
->dfs_node
= debugfs_create_dir(dd
->disk
->disk_name
, dfs_parent
);
2579 if (IS_ERR_OR_NULL(dd
->dfs_node
)) {
2580 dev_warn(&dd
->pdev
->dev
,
2581 "Error creating node %s under debugfs\n",
2582 dd
->disk
->disk_name
);
2583 dd
->dfs_node
= NULL
;
2587 debugfs_create_file("flags", S_IRUGO
, dd
->dfs_node
, dd
,
2589 debugfs_create_file("registers", S_IRUGO
, dd
->dfs_node
, dd
,
2595 static void mtip_hw_debugfs_exit(struct driver_data
*dd
)
2598 debugfs_remove_recursive(dd
->dfs_node
);
2602 * Perform any init/resume time hardware setup
2604 * @dd Pointer to the driver data structure.
2609 static inline void hba_setup(struct driver_data
*dd
)
2612 hwdata
= readl(dd
->mmio
+ HOST_HSORG
);
2614 /* interrupt bug workaround: use only 1 IS bit.*/
2616 HSORG_DISABLE_SLOTGRP_INTR
|
2617 HSORG_DISABLE_SLOTGRP_PXIS
,
2618 dd
->mmio
+ HOST_HSORG
);
2621 static int mtip_device_unaligned_constrained(struct driver_data
*dd
)
2623 return (dd
->pdev
->device
== P420M_DEVICE_ID
? 1 : 0);
2627 * Detect the details of the product, and store anything needed
2628 * into the driver data structure. This includes product type and
2629 * version and number of slot groups.
2631 * @dd Pointer to the driver data structure.
2636 static void mtip_detect_product(struct driver_data
*dd
)
2639 unsigned int rev
, slotgroups
;
2642 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2644 * [15:8] hardware/software interface rev#
2645 * [ 3] asic-style interface
2646 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2648 hwdata
= readl(dd
->mmio
+ HOST_HSORG
);
2650 dd
->product_type
= MTIP_PRODUCT_UNKNOWN
;
2651 dd
->slot_groups
= 1;
2654 dd
->product_type
= MTIP_PRODUCT_ASICFPGA
;
2655 rev
= (hwdata
& HSORG_HWREV
) >> 8;
2656 slotgroups
= (hwdata
& HSORG_SLOTGROUPS
) + 1;
2657 dev_info(&dd
->pdev
->dev
,
2658 "ASIC-FPGA design, HS rev 0x%x, "
2659 "%i slot groups [%i slots]\n",
2664 if (slotgroups
> MTIP_MAX_SLOT_GROUPS
) {
2665 dev_warn(&dd
->pdev
->dev
,
2666 "Warning: driver only supports "
2667 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS
);
2668 slotgroups
= MTIP_MAX_SLOT_GROUPS
;
2670 dd
->slot_groups
= slotgroups
;
2674 dev_warn(&dd
->pdev
->dev
, "Unrecognized product id\n");
2678 * Blocking wait for FTL rebuild to complete
2680 * @dd Pointer to the DRIVER_DATA structure.
2683 * 0 FTL rebuild completed successfully
2684 * -EFAULT FTL rebuild error/timeout/interruption
2686 static int mtip_ftl_rebuild_poll(struct driver_data
*dd
)
2688 unsigned long timeout
, cnt
= 0, start
;
2690 dev_warn(&dd
->pdev
->dev
,
2691 "FTL rebuild in progress. Polling for completion.\n");
2694 timeout
= jiffies
+ msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS
);
2697 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
2700 if (mtip_check_surprise_removal(dd
->pdev
))
2703 if (mtip_get_identify(dd
->port
, NULL
) < 0)
2706 if (*(dd
->port
->identify
+ MTIP_FTL_REBUILD_OFFSET
) ==
2707 MTIP_FTL_REBUILD_MAGIC
) {
2709 /* Print message every 3 minutes */
2711 dev_warn(&dd
->pdev
->dev
,
2712 "FTL rebuild in progress (%d secs).\n",
2713 jiffies_to_msecs(jiffies
- start
) / 1000);
2717 dev_warn(&dd
->pdev
->dev
,
2718 "FTL rebuild complete (%d secs).\n",
2719 jiffies_to_msecs(jiffies
- start
) / 1000);
2720 mtip_block_initialize(dd
);
2723 } while (time_before(jiffies
, timeout
));
2725 /* Check for timeout */
2726 dev_err(&dd
->pdev
->dev
,
2727 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2728 jiffies_to_msecs(jiffies
- start
) / 1000);
2732 static void mtip_softirq_done_fn(struct request
*rq
)
2734 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
2735 struct driver_data
*dd
= rq
->q
->queuedata
;
2737 /* Unmap the DMA scatter list entries */
2738 dma_unmap_sg(&dd
->pdev
->dev
, cmd
->sg
, cmd
->scatter_ents
,
2741 if (unlikely(cmd
->unaligned
))
2742 up(&dd
->port
->cmd_slot_unal
);
2744 blk_mq_end_request(rq
, cmd
->status
);
2747 static void mtip_abort_cmd(struct request
*req
, void *data
,
2750 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
2751 struct driver_data
*dd
= data
;
2753 dbg_printk(MTIP_DRV_NAME
" Aborting request, tag = %d\n", req
->tag
);
2755 clear_bit(req
->tag
, dd
->port
->cmds_to_issue
);
2756 cmd
->status
= BLK_STS_IOERR
;
2757 mtip_softirq_done_fn(req
);
2760 static void mtip_queue_cmd(struct request
*req
, void *data
,
2763 struct driver_data
*dd
= data
;
2765 set_bit(req
->tag
, dd
->port
->cmds_to_issue
);
2766 blk_abort_request(req
);
2770 * service thread to issue queued commands
2772 * @data Pointer to the driver data structure.
2778 static int mtip_service_thread(void *data
)
2780 struct driver_data
*dd
= (struct driver_data
*)data
;
2781 unsigned long slot
, slot_start
, slot_wrap
, to
;
2782 unsigned int num_cmd_slots
= dd
->slot_groups
* 32;
2783 struct mtip_port
*port
= dd
->port
;
2786 if (kthread_should_stop() ||
2787 test_bit(MTIP_PF_SVC_THD_STOP_BIT
, &port
->flags
))
2789 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
);
2792 * the condition is to check neither an internal command is
2793 * is in progress nor error handling is active
2795 wait_event_interruptible(port
->svc_wait
, (port
->flags
) &&
2796 (port
->flags
& MTIP_PF_SVC_THD_WORK
));
2798 if (kthread_should_stop() ||
2799 test_bit(MTIP_PF_SVC_THD_STOP_BIT
, &port
->flags
))
2802 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
2806 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
);
2809 /* Demux bits: start with error handling */
2810 if (test_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
)) {
2811 mtip_handle_tfe(dd
);
2812 clear_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
);
2815 if (test_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
))
2818 if (test_bit(MTIP_PF_TO_ACTIVE_BIT
, &port
->flags
)) {
2819 to
= jiffies
+ msecs_to_jiffies(5000);
2823 } while (atomic_read(&dd
->irq_workers_active
) != 0 &&
2824 time_before(jiffies
, to
));
2826 if (atomic_read(&dd
->irq_workers_active
) != 0)
2827 dev_warn(&dd
->pdev
->dev
,
2828 "Completion workers still active!");
2830 spin_lock(dd
->queue
->queue_lock
);
2831 blk_mq_tagset_busy_iter(&dd
->tags
,
2832 mtip_queue_cmd
, dd
);
2833 spin_unlock(dd
->queue
->queue_lock
);
2835 set_bit(MTIP_PF_ISSUE_CMDS_BIT
, &dd
->port
->flags
);
2837 if (mtip_device_reset(dd
))
2838 blk_mq_tagset_busy_iter(&dd
->tags
,
2839 mtip_abort_cmd
, dd
);
2841 clear_bit(MTIP_PF_TO_ACTIVE_BIT
, &dd
->port
->flags
);
2844 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
)) {
2846 /* used to restrict the loop to one iteration */
2847 slot_start
= num_cmd_slots
;
2850 slot
= find_next_bit(port
->cmds_to_issue
,
2851 num_cmd_slots
, slot
);
2852 if (slot_wrap
== 1) {
2853 if ((slot_start
>= slot
) ||
2854 (slot
>= num_cmd_slots
))
2857 if (unlikely(slot_start
== num_cmd_slots
))
2860 if (unlikely(slot
== num_cmd_slots
)) {
2866 /* Issue the command to the hardware */
2867 mtip_issue_ncq_command(port
, slot
);
2869 clear_bit(slot
, port
->cmds_to_issue
);
2872 clear_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
);
2875 if (test_bit(MTIP_PF_REBUILD_BIT
, &port
->flags
)) {
2876 if (mtip_ftl_rebuild_poll(dd
) == 0)
2877 clear_bit(MTIP_PF_REBUILD_BIT
, &port
->flags
);
2886 * DMA region teardown
2888 * @dd Pointer to driver_data structure
2893 static void mtip_dma_free(struct driver_data
*dd
)
2895 struct mtip_port
*port
= dd
->port
;
2898 dmam_free_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
2899 port
->block1
, port
->block1_dma
);
2901 if (port
->command_list
) {
2902 dmam_free_coherent(&dd
->pdev
->dev
, AHCI_CMD_TBL_SZ
,
2903 port
->command_list
, port
->command_list_dma
);
2910 * @dd Pointer to driver_data structure
2913 * -ENOMEM Not enough free DMA region space to initialize driver
2915 static int mtip_dma_alloc(struct driver_data
*dd
)
2917 struct mtip_port
*port
= dd
->port
;
2919 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
2921 dmam_alloc_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
2922 &port
->block1_dma
, GFP_KERNEL
);
2925 memset(port
->block1
, 0, BLOCK_DMA_ALLOC_SZ
);
2927 /* Allocate dma memory for command list */
2928 port
->command_list
=
2929 dmam_alloc_coherent(&dd
->pdev
->dev
, AHCI_CMD_TBL_SZ
,
2930 &port
->command_list_dma
, GFP_KERNEL
);
2931 if (!port
->command_list
) {
2932 dmam_free_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
2933 port
->block1
, port
->block1_dma
);
2934 port
->block1
= NULL
;
2935 port
->block1_dma
= 0;
2938 memset(port
->command_list
, 0, AHCI_CMD_TBL_SZ
);
2940 /* Setup all pointers into first DMA region */
2941 port
->rxfis
= port
->block1
+ AHCI_RX_FIS_OFFSET
;
2942 port
->rxfis_dma
= port
->block1_dma
+ AHCI_RX_FIS_OFFSET
;
2943 port
->identify
= port
->block1
+ AHCI_IDFY_OFFSET
;
2944 port
->identify_dma
= port
->block1_dma
+ AHCI_IDFY_OFFSET
;
2945 port
->log_buf
= port
->block1
+ AHCI_SECTBUF_OFFSET
;
2946 port
->log_buf_dma
= port
->block1_dma
+ AHCI_SECTBUF_OFFSET
;
2947 port
->smart_buf
= port
->block1
+ AHCI_SMARTBUF_OFFSET
;
2948 port
->smart_buf_dma
= port
->block1_dma
+ AHCI_SMARTBUF_OFFSET
;
2953 static int mtip_hw_get_identify(struct driver_data
*dd
)
2955 struct smart_attr attr242
;
2959 if (mtip_get_identify(dd
->port
, NULL
) < 0)
2962 if (*(dd
->port
->identify
+ MTIP_FTL_REBUILD_OFFSET
) ==
2963 MTIP_FTL_REBUILD_MAGIC
) {
2964 set_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
);
2965 return MTIP_FTL_REBUILD_MAGIC
;
2967 mtip_dump_identify(dd
->port
);
2969 /* check write protect, over temp and rebuild statuses */
2970 rv
= mtip_read_log_page(dd
->port
, ATA_LOG_SATA_NCQ
,
2972 dd
->port
->log_buf_dma
, 1);
2974 dev_warn(&dd
->pdev
->dev
,
2975 "Error in READ LOG EXT (10h) command\n");
2976 /* non-critical error, don't fail the load */
2978 buf
= (unsigned char *)dd
->port
->log_buf
;
2979 if (buf
[259] & 0x1) {
2980 dev_info(&dd
->pdev
->dev
,
2981 "Write protect bit is set.\n");
2982 set_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
);
2984 if (buf
[288] == 0xF7) {
2985 dev_info(&dd
->pdev
->dev
,
2986 "Exceeded Tmax, drive in thermal shutdown.\n");
2987 set_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
);
2989 if (buf
[288] == 0xBF) {
2990 dev_info(&dd
->pdev
->dev
,
2991 "Drive indicates rebuild has failed.\n");
2992 set_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
);
2996 /* get write protect progess */
2997 memset(&attr242
, 0, sizeof(struct smart_attr
));
2998 if (mtip_get_smart_attr(dd
->port
, 242, &attr242
))
2999 dev_warn(&dd
->pdev
->dev
,
3000 "Unable to check write protect progress\n");
3002 dev_info(&dd
->pdev
->dev
,
3003 "Write protect progress: %u%% (%u blocks)\n",
3004 attr242
.cur
, le32_to_cpu(attr242
.data
));
3010 * Called once for each card.
3012 * @dd Pointer to the driver data structure.
3015 * 0 on success, else an error code.
3017 static int mtip_hw_init(struct driver_data
*dd
)
3021 unsigned int num_command_slots
;
3022 unsigned long timeout
, timetaken
;
3024 dd
->mmio
= pcim_iomap_table(dd
->pdev
)[MTIP_ABAR
];
3026 mtip_detect_product(dd
);
3027 if (dd
->product_type
== MTIP_PRODUCT_UNKNOWN
) {
3031 num_command_slots
= dd
->slot_groups
* 32;
3035 dd
->port
= kzalloc_node(sizeof(struct mtip_port
), GFP_KERNEL
,
3038 dev_err(&dd
->pdev
->dev
,
3039 "Memory allocation: port structure\n");
3043 /* Continue workqueue setup */
3044 for (i
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++)
3045 dd
->work
[i
].port
= dd
->port
;
3047 /* Enable unaligned IO constraints for some devices */
3048 if (mtip_device_unaligned_constrained(dd
))
3049 dd
->unal_qdepth
= MTIP_MAX_UNALIGNED_SLOTS
;
3051 dd
->unal_qdepth
= 0;
3053 sema_init(&dd
->port
->cmd_slot_unal
, dd
->unal_qdepth
);
3055 /* Spinlock to prevent concurrent issue */
3056 for (i
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++)
3057 spin_lock_init(&dd
->port
->cmd_issue_lock
[i
]);
3059 /* Set the port mmio base address. */
3060 dd
->port
->mmio
= dd
->mmio
+ PORT_OFFSET
;
3063 /* DMA allocations */
3064 rv
= mtip_dma_alloc(dd
);
3068 /* Setup the pointers to the extended s_active and CI registers. */
3069 for (i
= 0; i
< dd
->slot_groups
; i
++) {
3070 dd
->port
->s_active
[i
] =
3071 dd
->port
->mmio
+ i
*0x80 + PORT_SCR_ACT
;
3072 dd
->port
->cmd_issue
[i
] =
3073 dd
->port
->mmio
+ i
*0x80 + PORT_COMMAND_ISSUE
;
3074 dd
->port
->completed
[i
] =
3075 dd
->port
->mmio
+ i
*0x80 + PORT_SDBV
;
3078 timetaken
= jiffies
;
3079 timeout
= jiffies
+ msecs_to_jiffies(30000);
3080 while (((readl(dd
->port
->mmio
+ PORT_SCR_STAT
) & 0x0F) != 0x03) &&
3081 time_before(jiffies
, timeout
)) {
3084 if (unlikely(mtip_check_surprise_removal(dd
->pdev
))) {
3085 timetaken
= jiffies
- timetaken
;
3086 dev_warn(&dd
->pdev
->dev
,
3087 "Surprise removal detected at %u ms\n",
3088 jiffies_to_msecs(timetaken
));
3092 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))) {
3093 timetaken
= jiffies
- timetaken
;
3094 dev_warn(&dd
->pdev
->dev
,
3095 "Removal detected at %u ms\n",
3096 jiffies_to_msecs(timetaken
));
3101 /* Conditionally reset the HBA. */
3102 if (!(readl(dd
->mmio
+ HOST_CAP
) & HOST_CAP_NZDMA
)) {
3103 if (mtip_hba_reset(dd
) < 0) {
3104 dev_err(&dd
->pdev
->dev
,
3105 "Card did not reset within timeout\n");
3110 /* Clear any pending interrupts on the HBA */
3111 writel(readl(dd
->mmio
+ HOST_IRQ_STAT
),
3112 dd
->mmio
+ HOST_IRQ_STAT
);
3115 mtip_init_port(dd
->port
);
3116 mtip_start_port(dd
->port
);
3118 /* Setup the ISR and enable interrupts. */
3119 rv
= devm_request_irq(&dd
->pdev
->dev
,
3123 dev_driver_string(&dd
->pdev
->dev
),
3127 dev_err(&dd
->pdev
->dev
,
3128 "Unable to allocate IRQ %d\n", dd
->pdev
->irq
);
3131 irq_set_affinity_hint(dd
->pdev
->irq
, get_cpu_mask(dd
->isr_binding
));
3133 /* Enable interrupts on the HBA. */
3134 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
3135 dd
->mmio
+ HOST_CTL
);
3137 init_waitqueue_head(&dd
->port
->svc_wait
);
3139 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)) {
3147 /* Disable interrupts on the HBA. */
3148 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3149 dd
->mmio
+ HOST_CTL
);
3151 /* Release the IRQ. */
3152 irq_set_affinity_hint(dd
->pdev
->irq
, NULL
);
3153 devm_free_irq(&dd
->pdev
->dev
, dd
->pdev
->irq
, dd
);
3156 mtip_deinit_port(dd
->port
);
3160 /* Free the memory allocated for the for structure. */
3166 static int mtip_standby_drive(struct driver_data
*dd
)
3170 if (dd
->sr
|| !dd
->port
)
3173 * Send standby immediate (E0h) to the drive so that it
3176 if (!test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
) &&
3177 !test_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
) &&
3178 !test_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
)) {
3179 rv
= mtip_standby_immediate(dd
->port
);
3181 dev_warn(&dd
->pdev
->dev
,
3182 "STANDBY IMMEDIATE failed\n");
3188 * Called to deinitialize an interface.
3190 * @dd Pointer to the driver data structure.
3195 static int mtip_hw_exit(struct driver_data
*dd
)
3198 /* de-initialize the port. */
3199 mtip_deinit_port(dd
->port
);
3201 /* Disable interrupts on the HBA. */
3202 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3203 dd
->mmio
+ HOST_CTL
);
3206 /* Release the IRQ. */
3207 irq_set_affinity_hint(dd
->pdev
->irq
, NULL
);
3208 devm_free_irq(&dd
->pdev
->dev
, dd
->pdev
->irq
, dd
);
3211 /* Free dma regions */
3214 /* Free the memory allocated for the for structure. */
3222 * Issue a Standby Immediate command to the device.
3224 * This function is called by the Block Layer just before the
3225 * system powers off during a shutdown.
3227 * @dd Pointer to the driver data structure.
3232 static int mtip_hw_shutdown(struct driver_data
*dd
)
3235 * Send standby immediate (E0h) to the drive so that it
3238 mtip_standby_drive(dd
);
3246 * This function is called by the Block Layer just before the
3247 * system hibernates.
3249 * @dd Pointer to the driver data structure.
3252 * 0 Suspend was successful
3253 * -EFAULT Suspend was not successful
3255 static int mtip_hw_suspend(struct driver_data
*dd
)
3258 * Send standby immediate (E0h) to the drive
3259 * so that it saves its state.
3261 if (mtip_standby_drive(dd
) != 0) {
3262 dev_err(&dd
->pdev
->dev
,
3263 "Failed standby-immediate command\n");
3267 /* Disable interrupts on the HBA.*/
3268 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3269 dd
->mmio
+ HOST_CTL
);
3270 mtip_deinit_port(dd
->port
);
3278 * This function is called by the Block Layer as the
3281 * @dd Pointer to the driver data structure.
3284 * 0 Resume was successful
3285 * -EFAULT Resume was not successful
3287 static int mtip_hw_resume(struct driver_data
*dd
)
3289 /* Perform any needed hardware setup steps */
3293 if (mtip_hba_reset(dd
) != 0) {
3294 dev_err(&dd
->pdev
->dev
,
3295 "Unable to reset the HBA\n");
3300 * Enable the port, DMA engine, and FIS reception specific
3301 * h/w in controller.
3303 mtip_init_port(dd
->port
);
3304 mtip_start_port(dd
->port
);
3306 /* Enable interrupts on the HBA.*/
3307 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
3308 dd
->mmio
+ HOST_CTL
);
3314 * Helper function for reusing disk name
3315 * upon hot insertion.
3317 static int rssd_disk_name_format(char *prefix
,
3322 const int base
= 'z' - 'a' + 1;
3323 char *begin
= buf
+ strlen(prefix
);
3324 char *end
= buf
+ buflen
;
3334 *--p
= 'a' + (index
% unit
);
3335 index
= (index
/ unit
) - 1;
3336 } while (index
>= 0);
3338 memmove(begin
, p
, end
- p
);
3339 memcpy(buf
, prefix
, strlen(prefix
));
3345 * Block layer IOCTL handler.
3347 * @dev Pointer to the block_device structure.
3349 * @cmd IOCTL command passed from the user application.
3350 * @arg Argument passed from the user application.
3353 * 0 IOCTL completed successfully.
3354 * -ENOTTY IOCTL not supported or invalid driver data
3355 * structure pointer.
3357 static int mtip_block_ioctl(struct block_device
*dev
,
3362 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3364 if (!capable(CAP_SYS_ADMIN
))
3370 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)))
3377 return mtip_hw_ioctl(dd
, cmd
, arg
);
3381 #ifdef CONFIG_COMPAT
3383 * Block layer compat IOCTL handler.
3385 * @dev Pointer to the block_device structure.
3387 * @cmd IOCTL command passed from the user application.
3388 * @arg Argument passed from the user application.
3391 * 0 IOCTL completed successfully.
3392 * -ENOTTY IOCTL not supported or invalid driver data
3393 * structure pointer.
3395 static int mtip_block_compat_ioctl(struct block_device
*dev
,
3400 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3402 if (!capable(CAP_SYS_ADMIN
))
3408 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)))
3414 case HDIO_DRIVE_TASKFILE
: {
3415 struct mtip_compat_ide_task_request_s __user
*compat_req_task
;
3416 ide_task_request_t req_task
;
3417 int compat_tasksize
, outtotal
, ret
;
3420 sizeof(struct mtip_compat_ide_task_request_s
);
3423 (struct mtip_compat_ide_task_request_s __user
*) arg
;
3425 if (copy_from_user(&req_task
, (void __user
*) arg
,
3426 compat_tasksize
- (2 * sizeof(compat_long_t
))))
3429 if (get_user(req_task
.out_size
, &compat_req_task
->out_size
))
3432 if (get_user(req_task
.in_size
, &compat_req_task
->in_size
))
3435 outtotal
= sizeof(struct mtip_compat_ide_task_request_s
);
3437 ret
= exec_drive_taskfile(dd
, (void __user
*) arg
,
3438 &req_task
, outtotal
);
3440 if (copy_to_user((void __user
*) arg
, &req_task
,
3442 (2 * sizeof(compat_long_t
))))
3445 if (put_user(req_task
.out_size
, &compat_req_task
->out_size
))
3448 if (put_user(req_task
.in_size
, &compat_req_task
->in_size
))
3454 return mtip_hw_ioctl(dd
, cmd
, arg
);
3460 * Obtain the geometry of the device.
3462 * You may think that this function is obsolete, but some applications,
3463 * fdisk for example still used CHS values. This function describes the
3464 * device as having 224 heads and 56 sectors per cylinder. These values are
3465 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3466 * partition is described in terms of a start and end cylinder this means
3467 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3468 * affects performance.
3470 * @dev Pointer to the block_device strucutre.
3471 * @geo Pointer to a hd_geometry structure.
3474 * 0 Operation completed successfully.
3475 * -ENOTTY An error occurred while reading the drive capacity.
3477 static int mtip_block_getgeo(struct block_device
*dev
,
3478 struct hd_geometry
*geo
)
3480 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3486 if (!(mtip_hw_get_capacity(dd
, &capacity
))) {
3487 dev_warn(&dd
->pdev
->dev
,
3488 "Could not get drive capacity.\n");
3494 sector_div(capacity
, (geo
->heads
* geo
->sectors
));
3495 geo
->cylinders
= capacity
;
3499 static int mtip_block_open(struct block_device
*dev
, fmode_t mode
)
3501 struct driver_data
*dd
;
3503 if (dev
&& dev
->bd_disk
) {
3504 dd
= (struct driver_data
*) dev
->bd_disk
->private_data
;
3507 if (test_bit(MTIP_DDF_REMOVAL_BIT
,
3517 static void mtip_block_release(struct gendisk
*disk
, fmode_t mode
)
3522 * Block device operation function.
3524 * This structure contains pointers to the functions required by the block
3527 static const struct block_device_operations mtip_block_ops
= {
3528 .open
= mtip_block_open
,
3529 .release
= mtip_block_release
,
3530 .ioctl
= mtip_block_ioctl
,
3531 #ifdef CONFIG_COMPAT
3532 .compat_ioctl
= mtip_block_compat_ioctl
,
3534 .getgeo
= mtip_block_getgeo
,
3535 .owner
= THIS_MODULE
3538 static inline bool is_se_active(struct driver_data
*dd
)
3540 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT
, &dd
->port
->flags
))) {
3541 if (dd
->port
->ic_pause_timer
) {
3542 unsigned long to
= dd
->port
->ic_pause_timer
+
3543 msecs_to_jiffies(1000);
3544 if (time_after(jiffies
, to
)) {
3545 clear_bit(MTIP_PF_SE_ACTIVE_BIT
,
3547 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
);
3548 dd
->port
->ic_pause_timer
= 0;
3549 wake_up_interruptible(&dd
->port
->svc_wait
);
3559 * Block layer make request function.
3561 * This function is called by the kernel to process a BIO for
3564 * @queue Pointer to the request queue. Unused other than to obtain
3565 * the driver data structure.
3566 * @rq Pointer to the request.
3569 static int mtip_submit_request(struct blk_mq_hw_ctx
*hctx
, struct request
*rq
)
3571 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3572 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3575 if (is_se_active(dd
))
3578 if (unlikely(dd
->dd_flag
& MTIP_DDF_STOP_IO
)) {
3579 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
3583 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
))) {
3586 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT
,
3591 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
) ||
3592 test_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
)))
3596 if (req_op(rq
) == REQ_OP_DISCARD
) {
3599 err
= mtip_send_trim(dd
, blk_rq_pos(rq
), blk_rq_sectors(rq
));
3600 blk_mq_end_request(rq
, err
? BLK_STS_IOERR
: BLK_STS_OK
);
3604 /* Create the scatter list for this request. */
3605 nents
= blk_rq_map_sg(hctx
->queue
, rq
, cmd
->sg
);
3607 /* Issue the read/write. */
3608 mtip_hw_submit_io(dd
, rq
, cmd
, nents
, hctx
);
3612 static bool mtip_check_unal_depth(struct blk_mq_hw_ctx
*hctx
,
3615 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3616 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3618 if (rq_data_dir(rq
) == READ
|| !dd
->unal_qdepth
)
3622 * If unaligned depth must be limited on this controller, mark it
3623 * as unaligned if the IO isn't on a 4k boundary (start of length).
3625 if (blk_rq_sectors(rq
) <= 64) {
3626 if ((blk_rq_pos(rq
) & 7) || (blk_rq_sectors(rq
) & 7))
3630 if (cmd
->unaligned
&& down_trylock(&dd
->port
->cmd_slot_unal
))
3636 static int mtip_issue_reserved_cmd(struct blk_mq_hw_ctx
*hctx
,
3639 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3640 struct mtip_int_cmd
*icmd
= rq
->special
;
3641 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3642 struct mtip_cmd_sg
*command_sg
;
3644 if (mtip_commands_active(dd
->port
))
3645 return BLK_MQ_RQ_QUEUE_BUSY
;
3647 /* Populate the SG list */
3648 cmd
->command_header
->opts
=
3649 __force_bit2int
cpu_to_le32(icmd
->opts
| icmd
->fis_len
);
3650 if (icmd
->buf_len
) {
3651 command_sg
= cmd
->command
+ AHCI_CMD_TBL_HDR_SZ
;
3654 __force_bit2int
cpu_to_le32((icmd
->buf_len
-1) & 0x3FFFFF);
3656 __force_bit2int
cpu_to_le32(icmd
->buffer
& 0xFFFFFFFF);
3657 command_sg
->dba_upper
=
3658 __force_bit2int
cpu_to_le32((icmd
->buffer
>> 16) >> 16);
3660 cmd
->command_header
->opts
|=
3661 __force_bit2int
cpu_to_le32((1 << 16));
3664 /* Populate the command header */
3665 cmd
->command_header
->byte_count
= 0;
3667 blk_mq_start_request(rq
);
3668 mtip_issue_non_ncq_command(dd
->port
, rq
->tag
);
3669 return BLK_MQ_RQ_QUEUE_OK
;
3672 static int mtip_queue_rq(struct blk_mq_hw_ctx
*hctx
,
3673 const struct blk_mq_queue_data
*bd
)
3675 struct request
*rq
= bd
->rq
;
3678 mtip_init_cmd_header(rq
);
3680 if (blk_rq_is_passthrough(rq
))
3681 return mtip_issue_reserved_cmd(hctx
, rq
);
3683 if (unlikely(mtip_check_unal_depth(hctx
, rq
)))
3684 return BLK_MQ_RQ_QUEUE_BUSY
;
3686 blk_mq_start_request(rq
);
3688 ret
= mtip_submit_request(hctx
, rq
);
3690 return BLK_MQ_RQ_QUEUE_OK
;
3692 return BLK_MQ_RQ_QUEUE_ERROR
;
3695 static void mtip_free_cmd(struct blk_mq_tag_set
*set
, struct request
*rq
,
3696 unsigned int hctx_idx
)
3698 struct driver_data
*dd
= set
->driver_data
;
3699 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3704 dmam_free_coherent(&dd
->pdev
->dev
, CMD_DMA_ALLOC_SZ
,
3705 cmd
->command
, cmd
->command_dma
);
3708 static int mtip_init_cmd(struct blk_mq_tag_set
*set
, struct request
*rq
,
3709 unsigned int hctx_idx
, unsigned int numa_node
)
3711 struct driver_data
*dd
= set
->driver_data
;
3712 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3714 cmd
->command
= dmam_alloc_coherent(&dd
->pdev
->dev
, CMD_DMA_ALLOC_SZ
,
3715 &cmd
->command_dma
, GFP_KERNEL
);
3719 memset(cmd
->command
, 0, CMD_DMA_ALLOC_SZ
);
3721 sg_init_table(cmd
->sg
, MTIP_MAX_SG
);
3725 static enum blk_eh_timer_return
mtip_cmd_timeout(struct request
*req
,
3728 struct driver_data
*dd
= req
->q
->queuedata
;
3731 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
3733 cmd
->status
= BLK_STS_TIMEOUT
;
3734 return BLK_EH_HANDLED
;
3737 if (test_bit(req
->tag
, dd
->port
->cmds_to_issue
))
3740 if (test_and_set_bit(MTIP_PF_TO_ACTIVE_BIT
, &dd
->port
->flags
))
3743 wake_up_interruptible(&dd
->port
->svc_wait
);
3745 return BLK_EH_RESET_TIMER
;
3748 static const struct blk_mq_ops mtip_mq_ops
= {
3749 .queue_rq
= mtip_queue_rq
,
3750 .init_request
= mtip_init_cmd
,
3751 .exit_request
= mtip_free_cmd
,
3752 .complete
= mtip_softirq_done_fn
,
3753 .timeout
= mtip_cmd_timeout
,
3757 * Block layer initialization function.
3759 * This function is called once by the PCI layer for each P320
3760 * device that is connected to the system.
3762 * @dd Pointer to the driver data structure.
3765 * 0 on success else an error code.
3767 static int mtip_block_initialize(struct driver_data
*dd
)
3769 int rv
= 0, wait_for_rebuild
= 0;
3771 unsigned int index
= 0;
3772 struct kobject
*kobj
;
3775 goto skip_create_disk
; /* hw init done, before rebuild */
3777 if (mtip_hw_init(dd
)) {
3779 goto protocol_init_error
;
3782 dd
->disk
= alloc_disk_node(MTIP_MAX_MINORS
, dd
->numa_node
);
3783 if (dd
->disk
== NULL
) {
3784 dev_err(&dd
->pdev
->dev
,
3785 "Unable to allocate gendisk structure\n");
3787 goto alloc_disk_error
;
3790 /* Generate the disk name, implemented same as in sd.c */
3792 if (!ida_pre_get(&rssd_index_ida
, GFP_KERNEL
)) {
3797 spin_lock(&rssd_index_lock
);
3798 rv
= ida_get_new(&rssd_index_ida
, &index
);
3799 spin_unlock(&rssd_index_lock
);
3800 } while (rv
== -EAGAIN
);
3805 rv
= rssd_disk_name_format("rssd",
3807 dd
->disk
->disk_name
,
3810 goto disk_index_error
;
3812 dd
->disk
->major
= dd
->major
;
3813 dd
->disk
->first_minor
= index
* MTIP_MAX_MINORS
;
3814 dd
->disk
->minors
= MTIP_MAX_MINORS
;
3815 dd
->disk
->fops
= &mtip_block_ops
;
3816 dd
->disk
->private_data
= dd
;
3819 mtip_hw_debugfs_init(dd
);
3821 memset(&dd
->tags
, 0, sizeof(dd
->tags
));
3822 dd
->tags
.ops
= &mtip_mq_ops
;
3823 dd
->tags
.nr_hw_queues
= 1;
3824 dd
->tags
.queue_depth
= MTIP_MAX_COMMAND_SLOTS
;
3825 dd
->tags
.reserved_tags
= 1;
3826 dd
->tags
.cmd_size
= sizeof(struct mtip_cmd
);
3827 dd
->tags
.numa_node
= dd
->numa_node
;
3828 dd
->tags
.flags
= BLK_MQ_F_SHOULD_MERGE
;
3829 dd
->tags
.driver_data
= dd
;
3830 dd
->tags
.timeout
= MTIP_NCQ_CMD_TIMEOUT_MS
;
3832 rv
= blk_mq_alloc_tag_set(&dd
->tags
);
3834 dev_err(&dd
->pdev
->dev
,
3835 "Unable to allocate request queue\n");
3836 goto block_queue_alloc_tag_error
;
3839 /* Allocate the request queue. */
3840 dd
->queue
= blk_mq_init_queue(&dd
->tags
);
3841 if (IS_ERR(dd
->queue
)) {
3842 dev_err(&dd
->pdev
->dev
,
3843 "Unable to allocate request queue\n");
3845 goto block_queue_alloc_init_error
;
3848 dd
->disk
->queue
= dd
->queue
;
3849 dd
->queue
->queuedata
= dd
;
3852 /* Initialize the protocol layer. */
3853 wait_for_rebuild
= mtip_hw_get_identify(dd
);
3854 if (wait_for_rebuild
< 0) {
3855 dev_err(&dd
->pdev
->dev
,
3856 "Protocol layer initialization failed\n");
3858 goto init_hw_cmds_error
;
3862 * if rebuild pending, start the service thread, and delay the block
3863 * queue creation and device_add_disk()
3865 if (wait_for_rebuild
== MTIP_FTL_REBUILD_MAGIC
)
3866 goto start_service_thread
;
3868 /* Set device limits. */
3869 set_bit(QUEUE_FLAG_NONROT
, &dd
->queue
->queue_flags
);
3870 clear_bit(QUEUE_FLAG_ADD_RANDOM
, &dd
->queue
->queue_flags
);
3871 blk_queue_max_segments(dd
->queue
, MTIP_MAX_SG
);
3872 blk_queue_physical_block_size(dd
->queue
, 4096);
3873 blk_queue_max_hw_sectors(dd
->queue
, 0xffff);
3874 blk_queue_max_segment_size(dd
->queue
, 0x400000);
3875 blk_queue_io_min(dd
->queue
, 4096);
3876 blk_queue_bounce_limit(dd
->queue
, dd
->pdev
->dma_mask
);
3878 /* Signal trim support */
3879 if (dd
->trim_supp
== true) {
3880 set_bit(QUEUE_FLAG_DISCARD
, &dd
->queue
->queue_flags
);
3881 dd
->queue
->limits
.discard_granularity
= 4096;
3882 blk_queue_max_discard_sectors(dd
->queue
,
3883 MTIP_MAX_TRIM_ENTRY_LEN
* MTIP_MAX_TRIM_ENTRIES
);
3886 /* Set the capacity of the device in 512 byte sectors. */
3887 if (!(mtip_hw_get_capacity(dd
, &capacity
))) {
3888 dev_warn(&dd
->pdev
->dev
,
3889 "Could not read drive capacity\n");
3891 goto read_capacity_error
;
3893 set_capacity(dd
->disk
, capacity
);
3895 /* Enable the block device and add it to /dev */
3896 device_add_disk(&dd
->pdev
->dev
, dd
->disk
);
3898 dd
->bdev
= bdget_disk(dd
->disk
, 0);
3900 * Now that the disk is active, initialize any sysfs attributes
3901 * managed by the protocol layer.
3903 kobj
= kobject_get(&disk_to_dev(dd
->disk
)->kobj
);
3905 mtip_hw_sysfs_init(dd
, kobj
);
3909 if (dd
->mtip_svc_handler
) {
3910 set_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
);
3911 return rv
; /* service thread created for handling rebuild */
3914 start_service_thread
:
3915 dd
->mtip_svc_handler
= kthread_create_on_node(mtip_service_thread
,
3917 "mtip_svc_thd_%02d", index
);
3919 if (IS_ERR(dd
->mtip_svc_handler
)) {
3920 dev_err(&dd
->pdev
->dev
, "service thread failed to start\n");
3921 dd
->mtip_svc_handler
= NULL
;
3923 goto kthread_run_error
;
3925 wake_up_process(dd
->mtip_svc_handler
);
3926 if (wait_for_rebuild
== MTIP_FTL_REBUILD_MAGIC
)
3927 rv
= wait_for_rebuild
;
3935 /* Delete our gendisk. This also removes the device from /dev */
3936 del_gendisk(dd
->disk
);
3938 read_capacity_error
:
3940 blk_cleanup_queue(dd
->queue
);
3941 block_queue_alloc_init_error
:
3942 blk_mq_free_tag_set(&dd
->tags
);
3943 block_queue_alloc_tag_error
:
3944 mtip_hw_debugfs_exit(dd
);
3946 spin_lock(&rssd_index_lock
);
3947 ida_remove(&rssd_index_ida
, index
);
3948 spin_unlock(&rssd_index_lock
);
3954 mtip_hw_exit(dd
); /* De-initialize the protocol layer. */
3956 protocol_init_error
:
3960 static void mtip_no_dev_cleanup(struct request
*rq
, void *data
, bool reserv
)
3962 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3964 cmd
->status
= BLK_STS_IOERR
;
3965 blk_mq_complete_request(rq
);
3969 * Block layer deinitialization function.
3971 * Called by the PCI layer as each P320 device is removed.
3973 * @dd Pointer to the driver data structure.
3978 static int mtip_block_remove(struct driver_data
*dd
)
3980 struct kobject
*kobj
;
3982 mtip_hw_debugfs_exit(dd
);
3984 if (dd
->mtip_svc_handler
) {
3985 set_bit(MTIP_PF_SVC_THD_STOP_BIT
, &dd
->port
->flags
);
3986 wake_up_interruptible(&dd
->port
->svc_wait
);
3987 kthread_stop(dd
->mtip_svc_handler
);
3990 /* Clean up the sysfs attributes, if created */
3991 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
)) {
3992 kobj
= kobject_get(&disk_to_dev(dd
->disk
)->kobj
);
3994 mtip_hw_sysfs_exit(dd
, kobj
);
4001 * Explicitly wait here for IOs to quiesce,
4002 * as mtip_standby_drive usually won't wait for IOs.
4004 if (!mtip_quiesce_io(dd
->port
, MTIP_QUIESCE_IO_TIMEOUT_MS
))
4005 mtip_standby_drive(dd
);
4008 dev_info(&dd
->pdev
->dev
, "device %s surprise removal\n",
4009 dd
->disk
->disk_name
);
4011 blk_freeze_queue_start(dd
->queue
);
4012 blk_mq_stop_hw_queues(dd
->queue
);
4013 blk_mq_tagset_busy_iter(&dd
->tags
, mtip_no_dev_cleanup
, dd
);
4016 * Delete our gendisk structure. This also removes the device
4024 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
))
4025 del_gendisk(dd
->disk
);
4026 if (dd
->disk
->queue
) {
4027 blk_cleanup_queue(dd
->queue
);
4028 blk_mq_free_tag_set(&dd
->tags
);
4035 spin_lock(&rssd_index_lock
);
4036 ida_remove(&rssd_index_ida
, dd
->index
);
4037 spin_unlock(&rssd_index_lock
);
4039 /* De-initialize the protocol layer. */
4046 * Function called by the PCI layer when just before the
4047 * machine shuts down.
4049 * If a protocol layer shutdown function is present it will be called
4052 * @dd Pointer to the driver data structure.
4057 static int mtip_block_shutdown(struct driver_data
*dd
)
4059 mtip_hw_shutdown(dd
);
4061 /* Delete our gendisk structure, and cleanup the blk queue. */
4063 dev_info(&dd
->pdev
->dev
,
4064 "Shutting down %s ...\n", dd
->disk
->disk_name
);
4066 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
))
4067 del_gendisk(dd
->disk
);
4068 if (dd
->disk
->queue
) {
4069 blk_cleanup_queue(dd
->queue
);
4070 blk_mq_free_tag_set(&dd
->tags
);
4077 spin_lock(&rssd_index_lock
);
4078 ida_remove(&rssd_index_ida
, dd
->index
);
4079 spin_unlock(&rssd_index_lock
);
4083 static int mtip_block_suspend(struct driver_data
*dd
)
4085 dev_info(&dd
->pdev
->dev
,
4086 "Suspending %s ...\n", dd
->disk
->disk_name
);
4087 mtip_hw_suspend(dd
);
4091 static int mtip_block_resume(struct driver_data
*dd
)
4093 dev_info(&dd
->pdev
->dev
, "Resuming %s ...\n",
4094 dd
->disk
->disk_name
);
4099 static void drop_cpu(int cpu
)
4104 static int get_least_used_cpu_on_node(int node
)
4106 int cpu
, least_used_cpu
, least_cnt
;
4107 const struct cpumask
*node_mask
;
4109 node_mask
= cpumask_of_node(node
);
4110 least_used_cpu
= cpumask_first(node_mask
);
4111 least_cnt
= cpu_use
[least_used_cpu
];
4112 cpu
= least_used_cpu
;
4114 for_each_cpu(cpu
, node_mask
) {
4115 if (cpu_use
[cpu
] < least_cnt
) {
4116 least_used_cpu
= cpu
;
4117 least_cnt
= cpu_use
[cpu
];
4120 cpu_use
[least_used_cpu
]++;
4121 return least_used_cpu
;
4124 /* Helper for selecting a node in round robin mode */
4125 static inline int mtip_get_next_rr_node(void)
4127 static int next_node
= -1;
4129 if (next_node
== -1) {
4130 next_node
= first_online_node
;
4134 next_node
= next_online_node(next_node
);
4135 if (next_node
== MAX_NUMNODES
)
4136 next_node
= first_online_node
;
4140 static DEFINE_HANDLER(0);
4141 static DEFINE_HANDLER(1);
4142 static DEFINE_HANDLER(2);
4143 static DEFINE_HANDLER(3);
4144 static DEFINE_HANDLER(4);
4145 static DEFINE_HANDLER(5);
4146 static DEFINE_HANDLER(6);
4147 static DEFINE_HANDLER(7);
4149 static void mtip_disable_link_opts(struct driver_data
*dd
, struct pci_dev
*pdev
)
4152 unsigned short pcie_dev_ctrl
;
4154 pos
= pci_find_capability(pdev
, PCI_CAP_ID_EXP
);
4156 pci_read_config_word(pdev
,
4157 pos
+ PCI_EXP_DEVCTL
,
4159 if (pcie_dev_ctrl
& (1 << 11) ||
4160 pcie_dev_ctrl
& (1 << 4)) {
4161 dev_info(&dd
->pdev
->dev
,
4162 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4163 pdev
->vendor
, pdev
->device
);
4164 pcie_dev_ctrl
&= ~(PCI_EXP_DEVCTL_NOSNOOP_EN
|
4165 PCI_EXP_DEVCTL_RELAX_EN
);
4166 pci_write_config_word(pdev
,
4167 pos
+ PCI_EXP_DEVCTL
,
4173 static void mtip_fix_ero_nosnoop(struct driver_data
*dd
, struct pci_dev
*pdev
)
4176 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4177 * device with device id 0x5aXX
4179 if (pdev
->bus
&& pdev
->bus
->self
) {
4180 if (pdev
->bus
->self
->vendor
== PCI_VENDOR_ID_ATI
&&
4181 ((pdev
->bus
->self
->device
& 0xff00) == 0x5a00)) {
4182 mtip_disable_link_opts(dd
, pdev
->bus
->self
);
4184 /* Check further up the topology */
4185 struct pci_dev
*parent_dev
= pdev
->bus
->self
;
4186 if (parent_dev
->bus
&&
4187 parent_dev
->bus
->parent
&&
4188 parent_dev
->bus
->parent
->self
&&
4189 parent_dev
->bus
->parent
->self
->vendor
==
4190 PCI_VENDOR_ID_ATI
&&
4191 (parent_dev
->bus
->parent
->self
->device
&
4192 0xff00) == 0x5a00) {
4193 mtip_disable_link_opts(dd
,
4194 parent_dev
->bus
->parent
->self
);
4201 * Called for each supported PCI device detected.
4203 * This function allocates the private data structure, enables the
4204 * PCI device and then calls the block layer initialization function.
4207 * 0 on success else an error code.
4209 static int mtip_pci_probe(struct pci_dev
*pdev
,
4210 const struct pci_device_id
*ent
)
4213 struct driver_data
*dd
= NULL
;
4215 const struct cpumask
*node_mask
;
4216 int cpu
, i
= 0, j
= 0;
4217 int my_node
= NUMA_NO_NODE
;
4218 unsigned long flags
;
4220 /* Allocate memory for this devices private data. */
4221 my_node
= pcibus_to_node(pdev
->bus
);
4222 if (my_node
!= NUMA_NO_NODE
) {
4223 if (!node_online(my_node
))
4224 my_node
= mtip_get_next_rr_node();
4226 dev_info(&pdev
->dev
, "Kernel not reporting proximity, choosing a node\n");
4227 my_node
= mtip_get_next_rr_node();
4229 dev_info(&pdev
->dev
, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4230 my_node
, pcibus_to_node(pdev
->bus
), dev_to_node(&pdev
->dev
),
4231 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
4233 dd
= kzalloc_node(sizeof(struct driver_data
), GFP_KERNEL
, my_node
);
4236 "Unable to allocate memory for driver data\n");
4240 /* Attach the private data to this PCI device. */
4241 pci_set_drvdata(pdev
, dd
);
4243 rv
= pcim_enable_device(pdev
);
4245 dev_err(&pdev
->dev
, "Unable to enable device\n");
4249 /* Map BAR5 to memory. */
4250 rv
= pcim_iomap_regions(pdev
, 1 << MTIP_ABAR
, MTIP_DRV_NAME
);
4252 dev_err(&pdev
->dev
, "Unable to map regions\n");
4256 if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(64))) {
4257 rv
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
4260 rv
= pci_set_consistent_dma_mask(pdev
,
4263 dev_warn(&pdev
->dev
,
4264 "64-bit DMA enable failed\n");
4270 /* Copy the info we may need later into the private data structure. */
4271 dd
->major
= mtip_major
;
4272 dd
->instance
= instance
;
4274 dd
->numa_node
= my_node
;
4276 INIT_LIST_HEAD(&dd
->online_list
);
4277 INIT_LIST_HEAD(&dd
->remove_list
);
4279 memset(dd
->workq_name
, 0, 32);
4280 snprintf(dd
->workq_name
, 31, "mtipq%d", dd
->instance
);
4282 dd
->isr_workq
= create_workqueue(dd
->workq_name
);
4283 if (!dd
->isr_workq
) {
4284 dev_warn(&pdev
->dev
, "Can't create wq %d\n", dd
->instance
);
4286 goto block_initialize_err
;
4289 memset(cpu_list
, 0, sizeof(cpu_list
));
4291 node_mask
= cpumask_of_node(dd
->numa_node
);
4292 if (!cpumask_empty(node_mask
)) {
4293 for_each_cpu(cpu
, node_mask
)
4295 snprintf(&cpu_list
[j
], 256 - j
, "%d ", cpu
);
4296 j
= strlen(cpu_list
);
4299 dev_info(&pdev
->dev
, "Node %d on package %d has %d cpu(s): %s\n",
4301 topology_physical_package_id(cpumask_first(node_mask
)),
4302 nr_cpus_node(dd
->numa_node
),
4305 dev_dbg(&pdev
->dev
, "mtip32xx: node_mask empty\n");
4307 dd
->isr_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4308 dev_info(&pdev
->dev
, "Initial IRQ binding node:cpu %d:%d\n",
4309 cpu_to_node(dd
->isr_binding
), dd
->isr_binding
);
4311 /* first worker context always runs in ISR */
4312 dd
->work
[0].cpu_binding
= dd
->isr_binding
;
4313 dd
->work
[1].cpu_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4314 dd
->work
[2].cpu_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4315 dd
->work
[3].cpu_binding
= dd
->work
[0].cpu_binding
;
4316 dd
->work
[4].cpu_binding
= dd
->work
[1].cpu_binding
;
4317 dd
->work
[5].cpu_binding
= dd
->work
[2].cpu_binding
;
4318 dd
->work
[6].cpu_binding
= dd
->work
[2].cpu_binding
;
4319 dd
->work
[7].cpu_binding
= dd
->work
[1].cpu_binding
;
4321 /* Log the bindings */
4322 for_each_present_cpu(cpu
) {
4323 memset(cpu_list
, 0, sizeof(cpu_list
));
4324 for (i
= 0, j
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++) {
4325 if (dd
->work
[i
].cpu_binding
== cpu
) {
4326 snprintf(&cpu_list
[j
], 256 - j
, "%d ", i
);
4327 j
= strlen(cpu_list
);
4331 dev_info(&pdev
->dev
, "CPU %d: WQs %s\n", cpu
, cpu_list
);
4334 INIT_WORK(&dd
->work
[0].work
, mtip_workq_sdbf0
);
4335 INIT_WORK(&dd
->work
[1].work
, mtip_workq_sdbf1
);
4336 INIT_WORK(&dd
->work
[2].work
, mtip_workq_sdbf2
);
4337 INIT_WORK(&dd
->work
[3].work
, mtip_workq_sdbf3
);
4338 INIT_WORK(&dd
->work
[4].work
, mtip_workq_sdbf4
);
4339 INIT_WORK(&dd
->work
[5].work
, mtip_workq_sdbf5
);
4340 INIT_WORK(&dd
->work
[6].work
, mtip_workq_sdbf6
);
4341 INIT_WORK(&dd
->work
[7].work
, mtip_workq_sdbf7
);
4343 pci_set_master(pdev
);
4344 rv
= pci_enable_msi(pdev
);
4346 dev_warn(&pdev
->dev
,
4347 "Unable to enable MSI interrupt.\n");
4348 goto msi_initialize_err
;
4351 mtip_fix_ero_nosnoop(dd
, pdev
);
4353 /* Initialize the block layer. */
4354 rv
= mtip_block_initialize(dd
);
4357 "Unable to initialize block layer\n");
4358 goto block_initialize_err
;
4362 * Increment the instance count so that each device has a unique
4366 if (rv
!= MTIP_FTL_REBUILD_MAGIC
)
4367 set_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
);
4369 rv
= 0; /* device in rebuild state, return 0 from probe */
4371 /* Add to online list even if in ftl rebuild */
4372 spin_lock_irqsave(&dev_lock
, flags
);
4373 list_add(&dd
->online_list
, &online_list
);
4374 spin_unlock_irqrestore(&dev_lock
, flags
);
4378 block_initialize_err
:
4379 pci_disable_msi(pdev
);
4382 if (dd
->isr_workq
) {
4383 flush_workqueue(dd
->isr_workq
);
4384 destroy_workqueue(dd
->isr_workq
);
4385 drop_cpu(dd
->work
[0].cpu_binding
);
4386 drop_cpu(dd
->work
[1].cpu_binding
);
4387 drop_cpu(dd
->work
[2].cpu_binding
);
4390 pcim_iounmap_regions(pdev
, 1 << MTIP_ABAR
);
4394 pci_set_drvdata(pdev
, NULL
);
4401 * Called for each probed device when the device is removed or the
4402 * driver is unloaded.
4407 static void mtip_pci_remove(struct pci_dev
*pdev
)
4409 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4410 unsigned long flags
, to
;
4412 set_bit(MTIP_DDF_REMOVAL_BIT
, &dd
->dd_flag
);
4414 spin_lock_irqsave(&dev_lock
, flags
);
4415 list_del_init(&dd
->online_list
);
4416 list_add(&dd
->remove_list
, &removing_list
);
4417 spin_unlock_irqrestore(&dev_lock
, flags
);
4419 mtip_check_surprise_removal(pdev
);
4420 synchronize_irq(dd
->pdev
->irq
);
4422 /* Spin until workers are done */
4423 to
= jiffies
+ msecs_to_jiffies(4000);
4426 } while (atomic_read(&dd
->irq_workers_active
) != 0 &&
4427 time_before(jiffies
, to
));
4430 fsync_bdev(dd
->bdev
);
4432 if (atomic_read(&dd
->irq_workers_active
) != 0) {
4433 dev_warn(&dd
->pdev
->dev
,
4434 "Completion workers still active!\n");
4437 blk_set_queue_dying(dd
->queue
);
4438 set_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
);
4440 /* Clean up the block layer. */
4441 mtip_block_remove(dd
);
4443 if (dd
->isr_workq
) {
4444 flush_workqueue(dd
->isr_workq
);
4445 destroy_workqueue(dd
->isr_workq
);
4446 drop_cpu(dd
->work
[0].cpu_binding
);
4447 drop_cpu(dd
->work
[1].cpu_binding
);
4448 drop_cpu(dd
->work
[2].cpu_binding
);
4451 pci_disable_msi(pdev
);
4453 spin_lock_irqsave(&dev_lock
, flags
);
4454 list_del_init(&dd
->remove_list
);
4455 spin_unlock_irqrestore(&dev_lock
, flags
);
4459 pcim_iounmap_regions(pdev
, 1 << MTIP_ABAR
);
4460 pci_set_drvdata(pdev
, NULL
);
4464 * Called for each probed device when the device is suspended.
4470 static int mtip_pci_suspend(struct pci_dev
*pdev
, pm_message_t mesg
)
4473 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4477 "Driver private datastructure is NULL\n");
4481 set_bit(MTIP_DDF_RESUME_BIT
, &dd
->dd_flag
);
4483 /* Disable ports & interrupts then send standby immediate */
4484 rv
= mtip_block_suspend(dd
);
4487 "Failed to suspend controller\n");
4492 * Save the pci config space to pdev structure &
4493 * disable the device
4495 pci_save_state(pdev
);
4496 pci_disable_device(pdev
);
4498 /* Move to Low power state*/
4499 pci_set_power_state(pdev
, PCI_D3hot
);
4505 * Called for each probed device when the device is resumed.
4511 static int mtip_pci_resume(struct pci_dev
*pdev
)
4514 struct driver_data
*dd
;
4516 dd
= pci_get_drvdata(pdev
);
4519 "Driver private datastructure is NULL\n");
4523 /* Move the device to active State */
4524 pci_set_power_state(pdev
, PCI_D0
);
4526 /* Restore PCI configuration space */
4527 pci_restore_state(pdev
);
4529 /* Enable the PCI device*/
4530 rv
= pcim_enable_device(pdev
);
4533 "Failed to enable card during resume\n");
4536 pci_set_master(pdev
);
4539 * Calls hbaReset, initPort, & startPort function
4540 * then enables interrupts
4542 rv
= mtip_block_resume(dd
);
4544 dev_err(&pdev
->dev
, "Unable to resume\n");
4547 clear_bit(MTIP_DDF_RESUME_BIT
, &dd
->dd_flag
);
4558 static void mtip_pci_shutdown(struct pci_dev
*pdev
)
4560 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4562 mtip_block_shutdown(dd
);
4565 /* Table of device ids supported by this driver. */
4566 static const struct pci_device_id mtip_pci_tbl
[] = {
4567 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320H_DEVICE_ID
) },
4568 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320M_DEVICE_ID
) },
4569 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320S_DEVICE_ID
) },
4570 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P325M_DEVICE_ID
) },
4571 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P420H_DEVICE_ID
) },
4572 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P420M_DEVICE_ID
) },
4573 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P425M_DEVICE_ID
) },
4577 /* Structure that describes the PCI driver functions. */
4578 static struct pci_driver mtip_pci_driver
= {
4579 .name
= MTIP_DRV_NAME
,
4580 .id_table
= mtip_pci_tbl
,
4581 .probe
= mtip_pci_probe
,
4582 .remove
= mtip_pci_remove
,
4583 .suspend
= mtip_pci_suspend
,
4584 .resume
= mtip_pci_resume
,
4585 .shutdown
= mtip_pci_shutdown
,
4588 MODULE_DEVICE_TABLE(pci
, mtip_pci_tbl
);
4591 * Module initialization function.
4593 * Called once when the module is loaded. This function allocates a major
4594 * block device number to the Cyclone devices and registers the PCI layer
4598 * 0 on success else error code.
4600 static int __init
mtip_init(void)
4604 pr_info(MTIP_DRV_NAME
" Version " MTIP_DRV_VERSION
"\n");
4606 spin_lock_init(&dev_lock
);
4608 INIT_LIST_HEAD(&online_list
);
4609 INIT_LIST_HEAD(&removing_list
);
4611 /* Allocate a major block device number to use with this driver. */
4612 error
= register_blkdev(0, MTIP_DRV_NAME
);
4614 pr_err("Unable to register block device (%d)\n",
4620 dfs_parent
= debugfs_create_dir("rssd", NULL
);
4621 if (IS_ERR_OR_NULL(dfs_parent
)) {
4622 pr_warn("Error creating debugfs parent\n");
4626 dfs_device_status
= debugfs_create_file("device_status",
4627 S_IRUGO
, dfs_parent
, NULL
,
4628 &mtip_device_status_fops
);
4629 if (IS_ERR_OR_NULL(dfs_device_status
)) {
4630 pr_err("Error creating device_status node\n");
4631 dfs_device_status
= NULL
;
4635 /* Register our PCI operations. */
4636 error
= pci_register_driver(&mtip_pci_driver
);
4638 debugfs_remove(dfs_parent
);
4639 unregister_blkdev(mtip_major
, MTIP_DRV_NAME
);
4646 * Module de-initialization function.
4648 * Called once when the module is unloaded. This function deallocates
4649 * the major block device number allocated by mtip_init() and
4650 * unregisters the PCI layer of the driver.
4655 static void __exit
mtip_exit(void)
4657 /* Release the allocated major block device number. */
4658 unregister_blkdev(mtip_major
, MTIP_DRV_NAME
);
4660 /* Unregister the PCI driver. */
4661 pci_unregister_driver(&mtip_pci_driver
);
4663 debugfs_remove_recursive(dfs_parent
);
4666 MODULE_AUTHOR("Micron Technology, Inc");
4667 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4668 MODULE_LICENSE("GPL");
4669 MODULE_VERSION(MTIP_DRV_VERSION
);
4671 module_init(mtip_init
);
4672 module_exit(mtip_exit
);