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>
43 #include <linux/numa.h>
46 #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
48 /* DMA region containing RX Fis, Identify, RLE10, and SMART buffers */
49 #define AHCI_RX_FIS_SZ 0x100
50 #define AHCI_RX_FIS_OFFSET 0x0
51 #define AHCI_IDFY_SZ ATA_SECT_SIZE
52 #define AHCI_IDFY_OFFSET 0x400
53 #define AHCI_SECTBUF_SZ ATA_SECT_SIZE
54 #define AHCI_SECTBUF_OFFSET 0x800
55 #define AHCI_SMARTBUF_SZ ATA_SECT_SIZE
56 #define AHCI_SMARTBUF_OFFSET 0xC00
57 /* 0x100 + 0x200 + 0x200 + 0x200 is smaller than 4k but we pad it out */
58 #define BLOCK_DMA_ALLOC_SZ 4096
60 /* DMA region containing command table (should be 8192 bytes) */
61 #define AHCI_CMD_SLOT_SZ sizeof(struct mtip_cmd_hdr)
62 #define AHCI_CMD_TBL_SZ (MTIP_MAX_COMMAND_SLOTS * AHCI_CMD_SLOT_SZ)
63 #define AHCI_CMD_TBL_OFFSET 0x0
65 /* DMA region per command (contains header and SGL) */
66 #define AHCI_CMD_TBL_HDR_SZ 0x80
67 #define AHCI_CMD_TBL_HDR_OFFSET 0x0
68 #define AHCI_CMD_TBL_SGL_SZ (MTIP_MAX_SG * sizeof(struct mtip_cmd_sg))
69 #define AHCI_CMD_TBL_SGL_OFFSET AHCI_CMD_TBL_HDR_SZ
70 #define CMD_DMA_ALLOC_SZ (AHCI_CMD_TBL_SGL_SZ + AHCI_CMD_TBL_HDR_SZ)
73 #define HOST_CAP_NZDMA (1 << 19)
74 #define HOST_HSORG 0xFC
75 #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
76 #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
77 #define HSORG_HWREV 0xFF00
78 #define HSORG_STYLE 0x8
79 #define HSORG_SLOTGROUPS 0x7
81 #define PORT_COMMAND_ISSUE 0x38
82 #define PORT_SDBV 0x7C
84 #define PORT_OFFSET 0x100
85 #define PORT_MEM_SIZE 0x80
87 #define PORT_IRQ_ERR \
88 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
89 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
90 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
92 #define PORT_IRQ_LEGACY \
93 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
94 #define PORT_IRQ_HANDLED \
95 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
96 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
97 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
98 #define DEF_PORT_IRQ \
99 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
101 /* product numbers */
102 #define MTIP_PRODUCT_UNKNOWN 0x00
103 #define MTIP_PRODUCT_ASICFPGA 0x11
105 /* Device instance number, incremented each time a device is probed. */
108 static struct list_head online_list
;
109 static struct list_head removing_list
;
110 static spinlock_t dev_lock
;
113 * Global variable used to hold the major block device number
114 * allocated in mtip_init().
116 static int mtip_major
;
117 static struct dentry
*dfs_parent
;
118 static struct dentry
*dfs_device_status
;
120 static u32 cpu_use
[NR_CPUS
];
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 blk_queue_flag_set(QUEUE_FLAG_DEAD
, dd
->queue
);
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 static struct mtip_cmd
*mtip_cmd_from_tag(struct driver_data
*dd
,
175 struct blk_mq_hw_ctx
*hctx
= dd
->queue
->queue_hw_ctx
[0];
177 return blk_mq_rq_to_pdu(blk_mq_tag_to_rq(hctx
->tags
, tag
));
181 * Reset the HBA (without sleeping)
183 * @dd Pointer to the driver data structure.
186 * 0 The reset was successful.
187 * -1 The HBA Reset bit did not clear.
189 static int mtip_hba_reset(struct driver_data
*dd
)
191 unsigned long timeout
;
193 /* Set the reset bit */
194 writel(HOST_RESET
, dd
->mmio
+ HOST_CTL
);
197 readl(dd
->mmio
+ HOST_CTL
);
200 * Spin for up to 10 seconds waiting for reset acknowledgement. Spec
201 * is 1 sec but in LUN failure conditions, up to 10 secs are required
203 timeout
= jiffies
+ msecs_to_jiffies(10000);
206 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))
209 } while ((readl(dd
->mmio
+ HOST_CTL
) & HOST_RESET
)
210 && time_before(jiffies
, timeout
));
212 if (readl(dd
->mmio
+ HOST_CTL
) & HOST_RESET
)
219 * Issue a command to the hardware.
221 * Set the appropriate bit in the s_active and Command Issue hardware
222 * registers, causing hardware command processing to begin.
224 * @port Pointer to the port structure.
225 * @tag The tag of the command to be issued.
230 static inline void mtip_issue_ncq_command(struct mtip_port
*port
, int tag
)
232 int group
= tag
>> 5;
234 /* guard SACT and CI registers */
235 spin_lock(&port
->cmd_issue_lock
[group
]);
236 writel((1 << MTIP_TAG_BIT(tag
)),
237 port
->s_active
[MTIP_TAG_INDEX(tag
)]);
238 writel((1 << MTIP_TAG_BIT(tag
)),
239 port
->cmd_issue
[MTIP_TAG_INDEX(tag
)]);
240 spin_unlock(&port
->cmd_issue_lock
[group
]);
244 * Enable/disable the reception of FIS
246 * @port Pointer to the port data structure
247 * @enable 1 to enable, 0 to disable
250 * Previous state: 1 enabled, 0 disabled
252 static int mtip_enable_fis(struct mtip_port
*port
, int enable
)
256 /* enable FIS reception */
257 tmp
= readl(port
->mmio
+ PORT_CMD
);
259 writel(tmp
| PORT_CMD_FIS_RX
, port
->mmio
+ PORT_CMD
);
261 writel(tmp
& ~PORT_CMD_FIS_RX
, port
->mmio
+ PORT_CMD
);
264 readl(port
->mmio
+ PORT_CMD
);
266 return (((tmp
& PORT_CMD_FIS_RX
) == PORT_CMD_FIS_RX
));
270 * Enable/disable the DMA engine
272 * @port Pointer to the port data structure
273 * @enable 1 to enable, 0 to disable
276 * Previous state: 1 enabled, 0 disabled.
278 static int mtip_enable_engine(struct mtip_port
*port
, int enable
)
282 /* enable FIS reception */
283 tmp
= readl(port
->mmio
+ PORT_CMD
);
285 writel(tmp
| PORT_CMD_START
, port
->mmio
+ PORT_CMD
);
287 writel(tmp
& ~PORT_CMD_START
, port
->mmio
+ PORT_CMD
);
289 readl(port
->mmio
+ PORT_CMD
);
290 return (((tmp
& PORT_CMD_START
) == PORT_CMD_START
));
294 * Enables the port DMA engine and FIS reception.
299 static inline void mtip_start_port(struct mtip_port
*port
)
301 /* Enable FIS reception */
302 mtip_enable_fis(port
, 1);
304 /* Enable the DMA engine */
305 mtip_enable_engine(port
, 1);
309 * Deinitialize a port by disabling port interrupts, the DMA engine,
312 * @port Pointer to the port structure
317 static inline void mtip_deinit_port(struct mtip_port
*port
)
319 /* Disable interrupts on this port */
320 writel(0, port
->mmio
+ PORT_IRQ_MASK
);
322 /* Disable the DMA engine */
323 mtip_enable_engine(port
, 0);
325 /* Disable FIS reception */
326 mtip_enable_fis(port
, 0);
332 * This function deinitializes the port by calling mtip_deinit_port() and
333 * then initializes it by setting the command header and RX FIS addresses,
334 * clearing the SError register and any pending port interrupts before
335 * re-enabling the default set of port interrupts.
337 * @port Pointer to the port structure.
342 static void mtip_init_port(struct mtip_port
*port
)
345 mtip_deinit_port(port
);
347 /* Program the command list base and FIS base addresses */
348 if (readl(port
->dd
->mmio
+ HOST_CAP
) & HOST_CAP_64
) {
349 writel((port
->command_list_dma
>> 16) >> 16,
350 port
->mmio
+ PORT_LST_ADDR_HI
);
351 writel((port
->rxfis_dma
>> 16) >> 16,
352 port
->mmio
+ PORT_FIS_ADDR_HI
);
353 set_bit(MTIP_PF_HOST_CAP_64
, &port
->flags
);
356 writel(port
->command_list_dma
& 0xFFFFFFFF,
357 port
->mmio
+ PORT_LST_ADDR
);
358 writel(port
->rxfis_dma
& 0xFFFFFFFF, port
->mmio
+ PORT_FIS_ADDR
);
361 writel(readl(port
->mmio
+ PORT_SCR_ERR
), port
->mmio
+ PORT_SCR_ERR
);
363 /* reset the completed registers.*/
364 for (i
= 0; i
< port
->dd
->slot_groups
; i
++)
365 writel(0xFFFFFFFF, port
->completed
[i
]);
367 /* Clear any pending interrupts for this port */
368 writel(readl(port
->mmio
+ PORT_IRQ_STAT
), port
->mmio
+ PORT_IRQ_STAT
);
370 /* Clear any pending interrupts on the HBA. */
371 writel(readl(port
->dd
->mmio
+ HOST_IRQ_STAT
),
372 port
->dd
->mmio
+ HOST_IRQ_STAT
);
374 /* Enable port interrupts */
375 writel(DEF_PORT_IRQ
, port
->mmio
+ PORT_IRQ_MASK
);
381 * @port Pointer to the port data structure.
386 static void mtip_restart_port(struct mtip_port
*port
)
388 unsigned long timeout
;
390 /* Disable the DMA engine */
391 mtip_enable_engine(port
, 0);
393 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
394 timeout
= jiffies
+ msecs_to_jiffies(500);
395 while ((readl(port
->mmio
+ PORT_CMD
) & PORT_CMD_LIST_ON
)
396 && time_before(jiffies
, timeout
))
399 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
403 * Chip quirk: escalate to hba reset if
404 * PxCMD.CR not clear after 500 ms
406 if (readl(port
->mmio
+ PORT_CMD
) & PORT_CMD_LIST_ON
) {
407 dev_warn(&port
->dd
->pdev
->dev
,
408 "PxCMD.CR not clear, escalating reset\n");
410 if (mtip_hba_reset(port
->dd
))
411 dev_err(&port
->dd
->pdev
->dev
,
412 "HBA reset escalation failed.\n");
414 /* 30 ms delay before com reset to quiesce chip */
418 dev_warn(&port
->dd
->pdev
->dev
, "Issuing COM reset\n");
421 writel(readl(port
->mmio
+ PORT_SCR_CTL
) |
422 1, port
->mmio
+ PORT_SCR_CTL
);
423 readl(port
->mmio
+ PORT_SCR_CTL
);
425 /* Wait 1 ms to quiesce chip function */
426 timeout
= jiffies
+ msecs_to_jiffies(1);
427 while (time_before(jiffies
, timeout
))
430 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
433 /* Clear PxSCTL.DET */
434 writel(readl(port
->mmio
+ PORT_SCR_CTL
) & ~1,
435 port
->mmio
+ PORT_SCR_CTL
);
436 readl(port
->mmio
+ PORT_SCR_CTL
);
438 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
439 timeout
= jiffies
+ msecs_to_jiffies(500);
440 while (((readl(port
->mmio
+ PORT_SCR_STAT
) & 0x01) == 0)
441 && time_before(jiffies
, timeout
))
444 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
447 if ((readl(port
->mmio
+ PORT_SCR_STAT
) & 0x01) == 0)
448 dev_warn(&port
->dd
->pdev
->dev
,
449 "COM reset failed\n");
451 mtip_init_port(port
);
452 mtip_start_port(port
);
456 static int mtip_device_reset(struct driver_data
*dd
)
460 if (mtip_check_surprise_removal(dd
->pdev
))
463 if (mtip_hba_reset(dd
) < 0)
467 mtip_init_port(dd
->port
);
468 mtip_start_port(dd
->port
);
470 /* Enable interrupts on the HBA. */
471 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
472 dd
->mmio
+ HOST_CTL
);
477 * Helper function for tag logging
479 static void print_tags(struct driver_data
*dd
,
481 unsigned long *tagbits
,
484 unsigned char tagmap
[128];
485 int group
, tagmap_len
= 0;
487 memset(tagmap
, 0, sizeof(tagmap
));
488 for (group
= SLOTBITS_IN_LONGS
; group
> 0; group
--)
489 tagmap_len
+= sprintf(tagmap
+ tagmap_len
, "%016lX ",
491 dev_warn(&dd
->pdev
->dev
,
492 "%d command(s) %s: tagmap [%s]", cnt
, msg
, tagmap
);
495 static int mtip_read_log_page(struct mtip_port
*port
, u8 page
, u16
*buffer
,
496 dma_addr_t buffer_dma
, unsigned int sectors
);
497 static int mtip_get_smart_attr(struct mtip_port
*port
, unsigned int id
,
498 struct smart_attr
*attrib
);
500 static void mtip_complete_command(struct mtip_cmd
*cmd
, blk_status_t status
)
502 struct request
*req
= blk_mq_rq_from_pdu(cmd
);
504 cmd
->status
= status
;
505 blk_mq_complete_request(req
);
511 * @dd Pointer to the DRIVER_DATA structure.
516 static void mtip_handle_tfe(struct driver_data
*dd
)
518 int group
, tag
, bit
, reissue
, rv
;
519 struct mtip_port
*port
;
520 struct mtip_cmd
*cmd
;
522 struct host_to_dev_fis
*fis
;
523 unsigned long tagaccum
[SLOTBITS_IN_LONGS
];
524 unsigned int cmd_cnt
= 0;
526 char *fail_reason
= NULL
;
527 int fail_all_ncq_write
= 0, fail_all_ncq_cmds
= 0;
529 dev_warn(&dd
->pdev
->dev
, "Taskfile error\n");
533 if (test_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
)) {
534 cmd
= mtip_cmd_from_tag(dd
, MTIP_TAG_INTERNAL
);
535 dbg_printk(MTIP_DRV_NAME
" TFE for the internal command\n");
536 mtip_complete_command(cmd
, BLK_STS_IOERR
);
540 /* clear the tag accumulator */
541 memset(tagaccum
, 0, SLOTBITS_IN_LONGS
* sizeof(long));
543 /* Loop through all the groups */
544 for (group
= 0; group
< dd
->slot_groups
; group
++) {
545 completed
= readl(port
->completed
[group
]);
547 dev_warn(&dd
->pdev
->dev
, "g=%u, comp=%x\n", group
, completed
);
549 /* clear completed status register in the hardware.*/
550 writel(completed
, port
->completed
[group
]);
552 /* Process successfully completed commands */
553 for (bit
= 0; bit
< 32 && completed
; bit
++) {
554 if (!(completed
& (1<<bit
)))
556 tag
= (group
<< 5) + bit
;
558 /* Skip the internal command slot */
559 if (tag
== MTIP_TAG_INTERNAL
)
562 cmd
= mtip_cmd_from_tag(dd
, tag
);
563 mtip_complete_command(cmd
, 0);
564 set_bit(tag
, tagaccum
);
569 print_tags(dd
, "completed (TFE)", tagaccum
, cmd_cnt
);
571 /* Restart the port */
573 mtip_restart_port(port
);
575 /* Trying to determine the cause of the error */
576 rv
= mtip_read_log_page(dd
->port
, ATA_LOG_SATA_NCQ
,
578 dd
->port
->log_buf_dma
, 1);
580 dev_warn(&dd
->pdev
->dev
,
581 "Error in READ LOG EXT (10h) command\n");
582 /* non-critical error, don't fail the load */
584 buf
= (unsigned char *)dd
->port
->log_buf
;
585 if (buf
[259] & 0x1) {
586 dev_info(&dd
->pdev
->dev
,
587 "Write protect bit is set.\n");
588 set_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
);
589 fail_all_ncq_write
= 1;
590 fail_reason
= "write protect";
592 if (buf
[288] == 0xF7) {
593 dev_info(&dd
->pdev
->dev
,
594 "Exceeded Tmax, drive in thermal shutdown.\n");
595 set_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
);
596 fail_all_ncq_cmds
= 1;
597 fail_reason
= "thermal shutdown";
599 if (buf
[288] == 0xBF) {
600 set_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
);
601 dev_info(&dd
->pdev
->dev
,
602 "Drive indicates rebuild has failed. Secure erase required.\n");
603 fail_all_ncq_cmds
= 1;
604 fail_reason
= "rebuild failed";
608 /* clear the tag accumulator */
609 memset(tagaccum
, 0, SLOTBITS_IN_LONGS
* sizeof(long));
611 /* Loop through all the groups */
612 for (group
= 0; group
< dd
->slot_groups
; group
++) {
613 for (bit
= 0; bit
< 32; bit
++) {
615 tag
= (group
<< 5) + bit
;
616 cmd
= mtip_cmd_from_tag(dd
, tag
);
618 fis
= (struct host_to_dev_fis
*)cmd
->command
;
620 /* Should re-issue? */
621 if (tag
== MTIP_TAG_INTERNAL
||
622 fis
->command
== ATA_CMD_SET_FEATURES
)
625 if (fail_all_ncq_cmds
||
626 (fail_all_ncq_write
&&
627 fis
->command
== ATA_CMD_FPDMA_WRITE
)) {
628 dev_warn(&dd
->pdev
->dev
,
629 " Fail: %s w/tag %d [%s].\n",
630 fis
->command
== ATA_CMD_FPDMA_WRITE
?
633 fail_reason
!= NULL
?
634 fail_reason
: "unknown");
635 mtip_complete_command(cmd
, BLK_STS_MEDIUM
);
641 * First check if this command has
642 * exceeded its retries.
644 if (reissue
&& (cmd
->retries
-- > 0)) {
646 set_bit(tag
, tagaccum
);
648 /* Re-issue the command. */
649 mtip_issue_ncq_command(port
, tag
);
654 /* Retire a command that will not be reissued */
655 dev_warn(&port
->dd
->pdev
->dev
,
656 "retiring tag %d\n", tag
);
658 mtip_complete_command(cmd
, BLK_STS_IOERR
);
661 print_tags(dd
, "reissued (TFE)", tagaccum
, cmd_cnt
);
665 * Handle a set device bits interrupt
667 static inline void mtip_workq_sdbfx(struct mtip_port
*port
, int group
,
670 struct driver_data
*dd
= port
->dd
;
672 struct mtip_cmd
*command
;
675 WARN_ON_ONCE(!completed
);
678 /* clear completed status register in the hardware.*/
679 writel(completed
, port
->completed
[group
]);
681 /* Process completed commands. */
682 for (bit
= 0; (bit
< 32) && completed
; bit
++) {
683 if (completed
& 0x01) {
684 tag
= (group
<< 5) | bit
;
686 /* skip internal command slot. */
687 if (unlikely(tag
== MTIP_TAG_INTERNAL
))
690 command
= mtip_cmd_from_tag(dd
, tag
);
691 mtip_complete_command(command
, 0);
696 /* If last, re-enable interrupts */
697 if (atomic_dec_return(&dd
->irq_workers_active
) == 0)
698 writel(0xffffffff, dd
->mmio
+ HOST_IRQ_STAT
);
702 * Process legacy pio and d2h interrupts
704 static inline void mtip_process_legacy(struct driver_data
*dd
, u32 port_stat
)
706 struct mtip_port
*port
= dd
->port
;
707 struct mtip_cmd
*cmd
= mtip_cmd_from_tag(dd
, MTIP_TAG_INTERNAL
);
709 if (test_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
) && cmd
) {
710 int group
= MTIP_TAG_INDEX(MTIP_TAG_INTERNAL
);
711 int status
= readl(port
->cmd_issue
[group
]);
713 if (!(status
& (1 << MTIP_TAG_BIT(MTIP_TAG_INTERNAL
))))
714 mtip_complete_command(cmd
, 0);
719 * Demux and handle errors
721 static inline void mtip_process_errors(struct driver_data
*dd
, u32 port_stat
)
723 if (unlikely(port_stat
& PORT_IRQ_CONNECT
)) {
724 dev_warn(&dd
->pdev
->dev
,
725 "Clearing PxSERR.DIAG.x\n");
726 writel((1 << 26), dd
->port
->mmio
+ PORT_SCR_ERR
);
729 if (unlikely(port_stat
& PORT_IRQ_PHYRDY
)) {
730 dev_warn(&dd
->pdev
->dev
,
731 "Clearing PxSERR.DIAG.n\n");
732 writel((1 << 16), dd
->port
->mmio
+ PORT_SCR_ERR
);
735 if (unlikely(port_stat
& ~PORT_IRQ_HANDLED
)) {
736 dev_warn(&dd
->pdev
->dev
,
737 "Port stat errors %x unhandled\n",
738 (port_stat
& ~PORT_IRQ_HANDLED
));
739 if (mtip_check_surprise_removal(dd
->pdev
))
742 if (likely(port_stat
& (PORT_IRQ_TF_ERR
| PORT_IRQ_IF_ERR
))) {
743 set_bit(MTIP_PF_EH_ACTIVE_BIT
, &dd
->port
->flags
);
744 wake_up_interruptible(&dd
->port
->svc_wait
);
748 static inline irqreturn_t
mtip_handle_irq(struct driver_data
*data
)
750 struct driver_data
*dd
= (struct driver_data
*) data
;
751 struct mtip_port
*port
= dd
->port
;
752 u32 hba_stat
, port_stat
;
754 int do_irq_enable
= 1, i
, workers
;
755 struct mtip_work
*twork
;
757 hba_stat
= readl(dd
->mmio
+ HOST_IRQ_STAT
);
761 /* Acknowledge the interrupt status on the port.*/
762 port_stat
= readl(port
->mmio
+ PORT_IRQ_STAT
);
763 if (unlikely(port_stat
== 0xFFFFFFFF)) {
764 mtip_check_surprise_removal(dd
->pdev
);
767 writel(port_stat
, port
->mmio
+ PORT_IRQ_STAT
);
769 /* Demux port status */
770 if (likely(port_stat
& PORT_IRQ_SDB_FIS
)) {
772 WARN_ON_ONCE(atomic_read(&dd
->irq_workers_active
) != 0);
774 /* Start at 1: group zero is always local? */
775 for (i
= 0, workers
= 0; i
< MTIP_MAX_SLOT_GROUPS
;
777 twork
= &dd
->work
[i
];
778 twork
->completed
= readl(port
->completed
[i
]);
779 if (twork
->completed
)
783 atomic_set(&dd
->irq_workers_active
, workers
);
785 for (i
= 1; i
< MTIP_MAX_SLOT_GROUPS
; i
++) {
786 twork
= &dd
->work
[i
];
787 if (twork
->completed
)
794 if (likely(dd
->work
[0].completed
))
795 mtip_workq_sdbfx(port
, 0,
796 dd
->work
[0].completed
);
800 * Chip quirk: SDB interrupt but nothing
807 if (unlikely(port_stat
& PORT_IRQ_ERR
)) {
808 if (unlikely(mtip_check_surprise_removal(dd
->pdev
))) {
809 /* don't proceed further */
812 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
816 mtip_process_errors(dd
, port_stat
& PORT_IRQ_ERR
);
819 if (unlikely(port_stat
& PORT_IRQ_LEGACY
))
820 mtip_process_legacy(dd
, port_stat
& PORT_IRQ_LEGACY
);
823 /* acknowledge interrupt */
824 if (unlikely(do_irq_enable
))
825 writel(hba_stat
, dd
->mmio
+ HOST_IRQ_STAT
);
831 * HBA interrupt subroutine.
834 * @instance Pointer to the driver data structure.
837 * IRQ_HANDLED A HBA interrupt was pending and handled.
838 * IRQ_NONE This interrupt was not for the HBA.
840 static irqreturn_t
mtip_irq_handler(int irq
, void *instance
)
842 struct driver_data
*dd
= instance
;
844 return mtip_handle_irq(dd
);
847 static void mtip_issue_non_ncq_command(struct mtip_port
*port
, int tag
)
849 writel(1 << MTIP_TAG_BIT(tag
), port
->cmd_issue
[MTIP_TAG_INDEX(tag
)]);
852 static bool mtip_pause_ncq(struct mtip_port
*port
,
853 struct host_to_dev_fis
*fis
)
855 unsigned long task_file_data
;
857 task_file_data
= readl(port
->mmio
+PORT_TFDATA
);
858 if ((task_file_data
& 1))
861 if (fis
->command
== ATA_CMD_SEC_ERASE_PREP
) {
862 port
->ic_pause_timer
= jiffies
;
864 } else if ((fis
->command
== ATA_CMD_DOWNLOAD_MICRO
) &&
865 (fis
->features
== 0x03)) {
866 set_bit(MTIP_PF_DM_ACTIVE_BIT
, &port
->flags
);
867 port
->ic_pause_timer
= jiffies
;
869 } else if ((fis
->command
== ATA_CMD_SEC_ERASE_UNIT
) ||
870 ((fis
->command
== 0xFC) &&
871 (fis
->features
== 0x27 || fis
->features
== 0x72 ||
872 fis
->features
== 0x62 || fis
->features
== 0x26))) {
873 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
874 clear_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &port
->dd
->dd_flag
);
875 /* Com reset after secure erase or lowlevel format */
876 mtip_restart_port(port
);
877 clear_bit(MTIP_PF_SE_ACTIVE_BIT
, &port
->flags
);
884 static bool mtip_commands_active(struct mtip_port
*port
)
890 * Ignore s_active bit 0 of array element 0.
891 * This bit will always be set
893 active
= readl(port
->s_active
[0]) & 0xFFFFFFFE;
894 for (n
= 1; n
< port
->dd
->slot_groups
; n
++)
895 active
|= readl(port
->s_active
[n
]);
901 * Wait for port to quiesce
903 * @port Pointer to port data structure
904 * @timeout Max duration to wait (ms)
908 * -EBUSY Commands still active
910 static int mtip_quiesce_io(struct mtip_port
*port
, unsigned long timeout
)
915 blk_mq_quiesce_queue(port
->dd
->queue
);
917 to
= jiffies
+ msecs_to_jiffies(timeout
);
919 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
) &&
920 test_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
)) {
922 continue; /* svc thd is actively issuing commands */
927 if (mtip_check_surprise_removal(port
->dd
->pdev
))
930 active
= mtip_commands_active(port
);
933 } while (time_before(jiffies
, to
));
935 blk_mq_unquiesce_queue(port
->dd
->queue
);
936 return active
? -EBUSY
: 0;
938 blk_mq_unquiesce_queue(port
->dd
->queue
);
942 struct mtip_int_cmd
{
950 * Execute an internal command and wait for the completion.
952 * @port Pointer to the port data structure.
953 * @fis Pointer to the FIS that describes the command.
954 * @fis_len Length in WORDS of the FIS.
955 * @buffer DMA accessible for command data.
956 * @buf_len Length, in bytes, of the data buffer.
957 * @opts Command header options, excluding the FIS length
958 * and the number of PRD entries.
959 * @timeout Time in ms to wait for the command to complete.
962 * 0 Command completed successfully.
963 * -EFAULT The buffer address is not correctly aligned.
964 * -EBUSY Internal command or other IO in progress.
965 * -EAGAIN Time out waiting for command to complete.
967 static int mtip_exec_internal_command(struct mtip_port
*port
,
968 struct host_to_dev_fis
*fis
,
973 unsigned long timeout
)
975 struct mtip_cmd
*int_cmd
;
976 struct driver_data
*dd
= port
->dd
;
978 struct mtip_int_cmd icmd
= {
986 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
987 if (buffer
& 0x00000007) {
988 dev_err(&dd
->pdev
->dev
, "SG buffer is not 8 byte aligned\n");
992 if (mtip_check_surprise_removal(dd
->pdev
))
995 rq
= blk_mq_alloc_request(dd
->queue
, REQ_OP_DRV_IN
, BLK_MQ_REQ_RESERVED
);
997 dbg_printk(MTIP_DRV_NAME
"Unable to allocate tag for PIO cmd\n");
1001 set_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1003 if (fis
->command
== ATA_CMD_SEC_ERASE_PREP
)
1004 set_bit(MTIP_PF_SE_ACTIVE_BIT
, &port
->flags
);
1006 clear_bit(MTIP_PF_DM_ACTIVE_BIT
, &port
->flags
);
1008 if (fis
->command
!= ATA_CMD_STANDBYNOW1
) {
1009 /* wait for io to complete if non atomic */
1010 if (mtip_quiesce_io(port
, MTIP_QUIESCE_IO_TIMEOUT_MS
) < 0) {
1011 dev_warn(&dd
->pdev
->dev
, "Failed to quiesce IO\n");
1012 blk_mq_free_request(rq
);
1013 clear_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1014 wake_up_interruptible(&port
->svc_wait
);
1019 /* Copy the command to the command table */
1020 int_cmd
= blk_mq_rq_to_pdu(rq
);
1021 int_cmd
->icmd
= &icmd
;
1022 memcpy(int_cmd
->command
, fis
, fis_len
*4);
1024 rq
->timeout
= timeout
;
1026 /* insert request and run queue */
1027 blk_execute_rq(rq
->q
, NULL
, rq
, true);
1029 if (int_cmd
->status
) {
1030 dev_err(&dd
->pdev
->dev
, "Internal command [%02X] failed %d\n",
1031 fis
->command
, int_cmd
->status
);
1034 if (mtip_check_surprise_removal(dd
->pdev
) ||
1035 test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
1037 dev_err(&dd
->pdev
->dev
,
1038 "Internal command [%02X] wait returned due to SR\n",
1043 mtip_device_reset(dd
); /* recover from timeout issue */
1048 if (readl(port
->cmd_issue
[MTIP_TAG_INDEX(MTIP_TAG_INTERNAL
)])
1049 & (1 << MTIP_TAG_BIT(MTIP_TAG_INTERNAL
))) {
1051 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)) {
1052 mtip_device_reset(dd
);
1057 /* Clear the allocated and active bits for the internal command. */
1058 blk_mq_free_request(rq
);
1059 clear_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1060 if (rv
>= 0 && mtip_pause_ncq(port
, fis
)) {
1064 wake_up_interruptible(&port
->svc_wait
);
1070 * Byte-swap ATA ID strings.
1072 * ATA identify data contains strings in byte-swapped 16-bit words.
1073 * They must be swapped (on all architectures) to be usable as C strings.
1074 * This function swaps bytes in-place.
1076 * @buf The buffer location of the string
1077 * @len The number of bytes to swap
1082 static inline void ata_swap_string(u16
*buf
, unsigned int len
)
1085 for (i
= 0; i
< (len
/2); i
++)
1086 be16_to_cpus(&buf
[i
]);
1089 static void mtip_set_timeout(struct driver_data
*dd
,
1090 struct host_to_dev_fis
*fis
,
1091 unsigned int *timeout
, u8 erasemode
)
1093 switch (fis
->command
) {
1094 case ATA_CMD_DOWNLOAD_MICRO
:
1095 *timeout
= 120000; /* 2 minutes */
1097 case ATA_CMD_SEC_ERASE_UNIT
:
1100 *timeout
= ((*(dd
->port
->identify
+ 90) * 2) * 60000);
1102 *timeout
= ((*(dd
->port
->identify
+ 89) * 2) * 60000);
1104 case ATA_CMD_STANDBYNOW1
:
1105 *timeout
= 120000; /* 2 minutes */
1109 *timeout
= 60000; /* 60 seconds */
1112 *timeout
= 15000; /* 15 seconds */
1115 *timeout
= MTIP_IOCTL_CMD_TIMEOUT_MS
;
1121 * Request the device identity information.
1123 * If a user space buffer is not specified, i.e. is NULL, the
1124 * identify information is still read from the drive and placed
1125 * into the identify data buffer (@e port->identify) in the
1126 * port data structure.
1127 * When the identify buffer contains valid identify information @e
1128 * port->identify_valid is non-zero.
1130 * @port Pointer to the port structure.
1131 * @user_buffer A user space buffer where the identify data should be
1135 * 0 Command completed successfully.
1136 * -EFAULT An error occurred while coping data to the user buffer.
1137 * -1 Command failed.
1139 static int mtip_get_identify(struct mtip_port
*port
, void __user
*user_buffer
)
1142 struct host_to_dev_fis fis
;
1144 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
1147 /* Build the FIS. */
1148 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1151 fis
.command
= ATA_CMD_ID_ATA
;
1153 /* Set the identify information as invalid. */
1154 port
->identify_valid
= 0;
1156 /* Clear the identify information. */
1157 memset(port
->identify
, 0, sizeof(u16
) * ATA_ID_WORDS
);
1159 /* Execute the command. */
1160 if (mtip_exec_internal_command(port
,
1164 sizeof(u16
) * ATA_ID_WORDS
,
1166 MTIP_INT_CMD_TIMEOUT_MS
)
1173 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1174 * perform field-sensitive swapping on the string fields.
1175 * See the kernel use of ata_id_string() for proof of this.
1177 #ifdef __LITTLE_ENDIAN
1178 ata_swap_string(port
->identify
+ 27, 40); /* model string*/
1179 ata_swap_string(port
->identify
+ 23, 8); /* firmware string*/
1180 ata_swap_string(port
->identify
+ 10, 20); /* serial# string*/
1184 for (i
= 0; i
< ATA_ID_WORDS
; i
++)
1185 port
->identify
[i
] = le16_to_cpu(port
->identify
[i
]);
1189 /* Check security locked state */
1190 if (port
->identify
[128] & 0x4)
1191 set_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
1193 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
1195 #ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
1196 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1197 if (port
->identify
[69] & (1 << 14) && port
->identify
[69] & (1 << 5))
1198 port
->dd
->trim_supp
= true;
1201 port
->dd
->trim_supp
= false;
1203 /* Set the identify buffer as valid. */
1204 port
->identify_valid
= 1;
1210 ATA_ID_WORDS
* sizeof(u16
))) {
1221 * Issue a standby immediate command to the device.
1223 * @port Pointer to the port structure.
1226 * 0 Command was executed successfully.
1227 * -1 An error occurred while executing the command.
1229 static int mtip_standby_immediate(struct mtip_port
*port
)
1232 struct host_to_dev_fis fis
;
1233 unsigned long start
;
1234 unsigned int timeout
;
1236 /* Build the FIS. */
1237 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1240 fis
.command
= ATA_CMD_STANDBYNOW1
;
1242 mtip_set_timeout(port
->dd
, &fis
, &timeout
, 0);
1245 rv
= mtip_exec_internal_command(port
,
1252 dbg_printk(MTIP_DRV_NAME
"Time taken to complete standby cmd: %d ms\n",
1253 jiffies_to_msecs(jiffies
- start
));
1255 dev_warn(&port
->dd
->pdev
->dev
,
1256 "STANDBY IMMEDIATE command failed.\n");
1262 * Issue a READ LOG EXT command to the device.
1264 * @port pointer to the port structure.
1265 * @page page number to fetch
1266 * @buffer pointer to buffer
1267 * @buffer_dma dma address corresponding to @buffer
1268 * @sectors page length to fetch, in sectors
1271 * @rv return value from mtip_exec_internal_command()
1273 static int mtip_read_log_page(struct mtip_port
*port
, u8 page
, u16
*buffer
,
1274 dma_addr_t buffer_dma
, unsigned int sectors
)
1276 struct host_to_dev_fis fis
;
1278 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1281 fis
.command
= ATA_CMD_READ_LOG_EXT
;
1282 fis
.sect_count
= sectors
& 0xFF;
1283 fis
.sect_cnt_ex
= (sectors
>> 8) & 0xFF;
1286 fis
.device
= ATA_DEVICE_OBS
;
1288 memset(buffer
, 0, sectors
* ATA_SECT_SIZE
);
1290 return mtip_exec_internal_command(port
,
1294 sectors
* ATA_SECT_SIZE
,
1296 MTIP_INT_CMD_TIMEOUT_MS
);
1300 * Issue a SMART READ DATA command to the device.
1302 * @port pointer to the port structure.
1303 * @buffer pointer to buffer
1304 * @buffer_dma dma address corresponding to @buffer
1307 * @rv return value from mtip_exec_internal_command()
1309 static int mtip_get_smart_data(struct mtip_port
*port
, u8
*buffer
,
1310 dma_addr_t buffer_dma
)
1312 struct host_to_dev_fis fis
;
1314 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1317 fis
.command
= ATA_CMD_SMART
;
1318 fis
.features
= 0xD0;
1322 fis
.device
= ATA_DEVICE_OBS
;
1324 return mtip_exec_internal_command(port
,
1334 * Get the value of a smart attribute
1336 * @port pointer to the port structure
1337 * @id attribute number
1338 * @attrib pointer to return attrib information corresponding to @id
1341 * -EINVAL NULL buffer passed or unsupported attribute @id.
1342 * -EPERM Identify data not valid, SMART not supported or not enabled
1344 static int mtip_get_smart_attr(struct mtip_port
*port
, unsigned int id
,
1345 struct smart_attr
*attrib
)
1348 struct smart_attr
*pattr
;
1353 if (!port
->identify_valid
) {
1354 dev_warn(&port
->dd
->pdev
->dev
, "IDENTIFY DATA not valid\n");
1357 if (!(port
->identify
[82] & 0x1)) {
1358 dev_warn(&port
->dd
->pdev
->dev
, "SMART not supported\n");
1361 if (!(port
->identify
[85] & 0x1)) {
1362 dev_warn(&port
->dd
->pdev
->dev
, "SMART not enabled\n");
1366 memset(port
->smart_buf
, 0, ATA_SECT_SIZE
);
1367 rv
= mtip_get_smart_data(port
, port
->smart_buf
, port
->smart_buf_dma
);
1369 dev_warn(&port
->dd
->pdev
->dev
, "Failed to ge SMART data\n");
1373 pattr
= (struct smart_attr
*)(port
->smart_buf
+ 2);
1374 for (i
= 0; i
< 29; i
++, pattr
++)
1375 if (pattr
->attr_id
== id
) {
1376 memcpy(attrib
, pattr
, sizeof(struct smart_attr
));
1381 dev_warn(&port
->dd
->pdev
->dev
,
1382 "Query for invalid SMART attribute ID\n");
1390 * Trim unused sectors
1392 * @dd pointer to driver_data structure
1394 * @len # of 512b sectors to trim
1396 static blk_status_t
mtip_send_trim(struct driver_data
*dd
, unsigned int lba
,
1399 u64 tlba
, tlen
, sect_left
;
1400 struct mtip_trim_entry
*buf
;
1401 dma_addr_t dma_addr
;
1402 struct host_to_dev_fis fis
;
1403 blk_status_t ret
= BLK_STS_OK
;
1406 if (!len
|| dd
->trim_supp
== false)
1407 return BLK_STS_IOERR
;
1409 /* Trim request too big */
1410 WARN_ON(len
> (MTIP_MAX_TRIM_ENTRY_LEN
* MTIP_MAX_TRIM_ENTRIES
));
1412 /* Trim request not aligned on 4k boundary */
1413 WARN_ON(len
% 8 != 0);
1415 /* Warn if vu_trim structure is too big */
1416 WARN_ON(sizeof(struct mtip_trim
) > ATA_SECT_SIZE
);
1418 /* Allocate a DMA buffer for the trim structure */
1419 buf
= dma_alloc_coherent(&dd
->pdev
->dev
, ATA_SECT_SIZE
, &dma_addr
,
1422 return BLK_STS_RESOURCE
;
1423 memset(buf
, 0, ATA_SECT_SIZE
);
1425 for (i
= 0, sect_left
= len
, tlba
= lba
;
1426 i
< MTIP_MAX_TRIM_ENTRIES
&& sect_left
;
1428 tlen
= (sect_left
>= MTIP_MAX_TRIM_ENTRY_LEN
?
1429 MTIP_MAX_TRIM_ENTRY_LEN
:
1431 buf
[i
].lba
= cpu_to_le32(tlba
);
1432 buf
[i
].range
= cpu_to_le16(tlen
);
1436 WARN_ON(sect_left
!= 0);
1439 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1443 fis
.features
= 0x60;
1445 fis
.device
= ATA_DEVICE_OBS
;
1447 if (mtip_exec_internal_command(dd
->port
,
1453 MTIP_TRIM_TIMEOUT_MS
) < 0)
1454 ret
= BLK_STS_IOERR
;
1456 dma_free_coherent(&dd
->pdev
->dev
, ATA_SECT_SIZE
, buf
, dma_addr
);
1461 * Get the drive capacity.
1463 * @dd Pointer to the device data structure.
1464 * @sectors Pointer to the variable that will receive the sector count.
1467 * 1 Capacity was returned successfully.
1468 * 0 The identify information is invalid.
1470 static bool mtip_hw_get_capacity(struct driver_data
*dd
, sector_t
*sectors
)
1472 struct mtip_port
*port
= dd
->port
;
1473 u64 total
, raw0
, raw1
, raw2
, raw3
;
1474 raw0
= port
->identify
[100];
1475 raw1
= port
->identify
[101];
1476 raw2
= port
->identify
[102];
1477 raw3
= port
->identify
[103];
1478 total
= raw0
| raw1
<<16 | raw2
<<32 | raw3
<<48;
1480 return (bool) !!port
->identify_valid
;
1484 * Display the identify command data.
1486 * @port Pointer to the port data structure.
1491 static void mtip_dump_identify(struct mtip_port
*port
)
1494 unsigned short revid
;
1497 if (!port
->identify_valid
)
1500 strlcpy(cbuf
, (char *)(port
->identify
+10), 21);
1501 dev_info(&port
->dd
->pdev
->dev
,
1502 "Serial No.: %s\n", cbuf
);
1504 strlcpy(cbuf
, (char *)(port
->identify
+23), 9);
1505 dev_info(&port
->dd
->pdev
->dev
,
1506 "Firmware Ver.: %s\n", cbuf
);
1508 strlcpy(cbuf
, (char *)(port
->identify
+27), 41);
1509 dev_info(&port
->dd
->pdev
->dev
, "Model: %s\n", cbuf
);
1511 dev_info(&port
->dd
->pdev
->dev
, "Security: %04x %s\n",
1512 port
->identify
[128],
1513 port
->identify
[128] & 0x4 ? "(LOCKED)" : "");
1515 if (mtip_hw_get_capacity(port
->dd
, §ors
))
1516 dev_info(&port
->dd
->pdev
->dev
,
1517 "Capacity: %llu sectors (%llu MB)\n",
1519 ((u64
)sectors
) * ATA_SECT_SIZE
>> 20);
1521 pci_read_config_word(port
->dd
->pdev
, PCI_REVISION_ID
, &revid
);
1522 switch (revid
& 0xFF) {
1524 strlcpy(cbuf
, "A0", 3);
1527 strlcpy(cbuf
, "A2", 3);
1530 strlcpy(cbuf
, "?", 2);
1533 dev_info(&port
->dd
->pdev
->dev
,
1534 "Card Type: %s\n", cbuf
);
1538 * Map the commands scatter list into the command table.
1540 * @command Pointer to the command.
1541 * @nents Number of scatter list entries.
1546 static inline void fill_command_sg(struct driver_data
*dd
,
1547 struct mtip_cmd
*command
,
1551 unsigned int dma_len
;
1552 struct mtip_cmd_sg
*command_sg
;
1553 struct scatterlist
*sg
;
1555 command_sg
= command
->command
+ AHCI_CMD_TBL_HDR_SZ
;
1557 for_each_sg(command
->sg
, sg
, nents
, n
) {
1558 dma_len
= sg_dma_len(sg
);
1559 if (dma_len
> 0x400000)
1560 dev_err(&dd
->pdev
->dev
,
1561 "DMA segment length truncated\n");
1562 command_sg
->info
= cpu_to_le32((dma_len
-1) & 0x3FFFFF);
1563 command_sg
->dba
= cpu_to_le32(sg_dma_address(sg
));
1564 command_sg
->dba_upper
=
1565 cpu_to_le32((sg_dma_address(sg
) >> 16) >> 16);
1571 * @brief Execute a drive command.
1573 * return value 0 The command completed successfully.
1574 * return value -1 An error occurred while executing the command.
1576 static int exec_drive_task(struct mtip_port
*port
, u8
*command
)
1578 struct host_to_dev_fis fis
;
1579 struct host_to_dev_fis
*reply
= (port
->rxfis
+ RX_FIS_D2H_REG
);
1582 /* Build the FIS. */
1583 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1586 fis
.command
= command
[0];
1587 fis
.features
= command
[1];
1588 fis
.sect_count
= command
[2];
1589 fis
.sector
= command
[3];
1590 fis
.cyl_low
= command
[4];
1591 fis
.cyl_hi
= command
[5];
1592 fis
.device
= command
[6] & ~0x10; /* Clear the dev bit*/
1594 mtip_set_timeout(port
->dd
, &fis
, &to
, 0);
1596 dbg_printk(MTIP_DRV_NAME
" %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
1606 /* Execute the command. */
1607 if (mtip_exec_internal_command(port
,
1617 command
[0] = reply
->command
; /* Status*/
1618 command
[1] = reply
->features
; /* Error*/
1619 command
[4] = reply
->cyl_low
;
1620 command
[5] = reply
->cyl_hi
;
1622 dbg_printk(MTIP_DRV_NAME
" %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
1633 * @brief Execute a drive command.
1635 * @param port Pointer to the port data structure.
1636 * @param command Pointer to the user specified command parameters.
1637 * @param user_buffer Pointer to the user space buffer where read sector
1638 * data should be copied.
1640 * return value 0 The command completed successfully.
1641 * return value -EFAULT An error occurred while copying the completion
1642 * data to the user space buffer.
1643 * return value -1 An error occurred while executing the command.
1645 static int exec_drive_command(struct mtip_port
*port
, u8
*command
,
1646 void __user
*user_buffer
)
1648 struct host_to_dev_fis fis
;
1649 struct host_to_dev_fis
*reply
;
1651 dma_addr_t dma_addr
= 0;
1652 int rv
= 0, xfer_sz
= command
[3];
1659 buf
= dma_alloc_coherent(&port
->dd
->pdev
->dev
,
1660 ATA_SECT_SIZE
* xfer_sz
,
1664 dev_err(&port
->dd
->pdev
->dev
,
1665 "Memory allocation failed (%d bytes)\n",
1666 ATA_SECT_SIZE
* xfer_sz
);
1669 memset(buf
, 0, ATA_SECT_SIZE
* xfer_sz
);
1672 /* Build the FIS. */
1673 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1676 fis
.command
= command
[0];
1677 fis
.features
= command
[2];
1678 fis
.sect_count
= command
[3];
1679 if (fis
.command
== ATA_CMD_SMART
) {
1680 fis
.sector
= command
[1];
1685 mtip_set_timeout(port
->dd
, &fis
, &to
, 0);
1688 reply
= (port
->rxfis
+ RX_FIS_PIO_SETUP
);
1690 reply
= (port
->rxfis
+ RX_FIS_D2H_REG
);
1692 dbg_printk(MTIP_DRV_NAME
1693 " %s: User Command: cmd %x, sect %x, "
1694 "feat %x, sectcnt %x\n",
1701 /* Execute the command. */
1702 if (mtip_exec_internal_command(port
,
1705 (xfer_sz
? dma_addr
: 0),
1706 (xfer_sz
? ATA_SECT_SIZE
* xfer_sz
: 0),
1711 goto exit_drive_command
;
1714 /* Collect the completion status. */
1715 command
[0] = reply
->command
; /* Status*/
1716 command
[1] = reply
->features
; /* Error*/
1717 command
[2] = reply
->sect_count
;
1719 dbg_printk(MTIP_DRV_NAME
1720 " %s: Completion Status: stat %x, "
1721 "err %x, nsect %x\n",
1728 if (copy_to_user(user_buffer
,
1730 ATA_SECT_SIZE
* command
[3])) {
1732 goto exit_drive_command
;
1737 dma_free_coherent(&port
->dd
->pdev
->dev
,
1738 ATA_SECT_SIZE
* xfer_sz
, buf
, dma_addr
);
1743 * Indicates whether a command has a single sector payload.
1745 * @command passed to the device to perform the certain event.
1746 * @features passed to the device to perform the certain event.
1749 * 1 command is one that always has a single sector payload,
1750 * regardless of the value in the Sector Count field.
1754 static unsigned int implicit_sector(unsigned char command
,
1755 unsigned char features
)
1757 unsigned int rv
= 0;
1759 /* list of commands that have an implicit sector count of 1 */
1761 case ATA_CMD_SEC_SET_PASS
:
1762 case ATA_CMD_SEC_UNLOCK
:
1763 case ATA_CMD_SEC_ERASE_PREP
:
1764 case ATA_CMD_SEC_ERASE_UNIT
:
1765 case ATA_CMD_SEC_FREEZE_LOCK
:
1766 case ATA_CMD_SEC_DISABLE_PASS
:
1767 case ATA_CMD_PMP_READ
:
1768 case ATA_CMD_PMP_WRITE
:
1771 case ATA_CMD_SET_MAX
:
1772 if (features
== ATA_SET_MAX_UNLOCK
)
1776 if ((features
== ATA_SMART_READ_VALUES
) ||
1777 (features
== ATA_SMART_READ_THRESHOLDS
))
1780 case ATA_CMD_CONF_OVERLAY
:
1781 if ((features
== ATA_DCO_IDENTIFY
) ||
1782 (features
== ATA_DCO_SET
))
1790 * Executes a taskfile
1791 * See ide_taskfile_ioctl() for derivation
1793 static int exec_drive_taskfile(struct driver_data
*dd
,
1795 ide_task_request_t
*req_task
,
1798 struct host_to_dev_fis fis
;
1799 struct host_to_dev_fis
*reply
;
1802 dma_addr_t outbuf_dma
= 0;
1803 dma_addr_t inbuf_dma
= 0;
1804 dma_addr_t dma_buffer
= 0;
1806 unsigned int taskin
= 0;
1807 unsigned int taskout
= 0;
1809 unsigned int timeout
;
1810 unsigned int force_single_sector
;
1811 unsigned int transfer_size
;
1812 unsigned long task_file_data
;
1813 int intotal
= outtotal
+ req_task
->out_size
;
1816 taskout
= req_task
->out_size
;
1817 taskin
= req_task
->in_size
;
1818 /* 130560 = 512 * 0xFF*/
1819 if (taskin
> 130560 || taskout
> 130560)
1823 outbuf
= memdup_user(buf
+ outtotal
, taskout
);
1825 return PTR_ERR(outbuf
);
1827 outbuf_dma
= dma_map_single(&dd
->pdev
->dev
, outbuf
,
1828 taskout
, DMA_TO_DEVICE
);
1829 if (dma_mapping_error(&dd
->pdev
->dev
, outbuf_dma
)) {
1833 dma_buffer
= outbuf_dma
;
1837 inbuf
= memdup_user(buf
+ intotal
, taskin
);
1838 if (IS_ERR(inbuf
)) {
1839 err
= PTR_ERR(inbuf
);
1843 inbuf_dma
= dma_map_single(&dd
->pdev
->dev
, inbuf
,
1844 taskin
, DMA_FROM_DEVICE
);
1845 if (dma_mapping_error(&dd
->pdev
->dev
, inbuf_dma
)) {
1849 dma_buffer
= inbuf_dma
;
1852 /* only supports PIO and non-data commands from this ioctl. */
1853 switch (req_task
->data_phase
) {
1855 nsect
= taskout
/ ATA_SECT_SIZE
;
1856 reply
= (dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
);
1859 reply
= (dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
);
1861 case TASKFILE_NO_DATA
:
1862 reply
= (dd
->port
->rxfis
+ RX_FIS_D2H_REG
);
1869 /* Build the FIS. */
1870 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1874 fis
.command
= req_task
->io_ports
[7];
1875 fis
.features
= req_task
->io_ports
[1];
1876 fis
.sect_count
= req_task
->io_ports
[2];
1877 fis
.lba_low
= req_task
->io_ports
[3];
1878 fis
.lba_mid
= req_task
->io_ports
[4];
1879 fis
.lba_hi
= req_task
->io_ports
[5];
1880 /* Clear the dev bit*/
1881 fis
.device
= req_task
->io_ports
[6] & ~0x10;
1883 if ((req_task
->in_flags
.all
== 0) && (req_task
->out_flags
.all
& 1)) {
1884 req_task
->in_flags
.all
=
1885 IDE_TASKFILE_STD_IN_FLAGS
|
1886 (IDE_HOB_STD_IN_FLAGS
<< 8);
1887 fis
.lba_low_ex
= req_task
->hob_ports
[3];
1888 fis
.lba_mid_ex
= req_task
->hob_ports
[4];
1889 fis
.lba_hi_ex
= req_task
->hob_ports
[5];
1890 fis
.features_ex
= req_task
->hob_ports
[1];
1891 fis
.sect_cnt_ex
= req_task
->hob_ports
[2];
1894 req_task
->in_flags
.all
= IDE_TASKFILE_STD_IN_FLAGS
;
1897 force_single_sector
= implicit_sector(fis
.command
, fis
.features
);
1899 if ((taskin
|| taskout
) && (!fis
.sect_count
)) {
1901 fis
.sect_count
= nsect
;
1903 if (!force_single_sector
) {
1904 dev_warn(&dd
->pdev
->dev
,
1905 "data movement but "
1906 "sect_count is 0\n");
1913 dbg_printk(MTIP_DRV_NAME
1914 " %s: cmd %x, feat %x, nsect %x,"
1915 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
1926 /* check for erase mode support during secure erase.*/
1927 if ((fis
.command
== ATA_CMD_SEC_ERASE_UNIT
) && outbuf
&&
1928 (outbuf
[0] & MTIP_SEC_ERASE_MODE
)) {
1932 mtip_set_timeout(dd
, &fis
, &timeout
, erasemode
);
1934 /* Determine the correct transfer size.*/
1935 if (force_single_sector
)
1936 transfer_size
= ATA_SECT_SIZE
;
1938 transfer_size
= ATA_SECT_SIZE
* fis
.sect_count
;
1940 /* Execute the command.*/
1941 if (mtip_exec_internal_command(dd
->port
,
1952 task_file_data
= readl(dd
->port
->mmio
+PORT_TFDATA
);
1954 if ((req_task
->data_phase
== TASKFILE_IN
) && !(task_file_data
& 1)) {
1955 reply
= dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
;
1956 req_task
->io_ports
[7] = reply
->control
;
1958 reply
= dd
->port
->rxfis
+ RX_FIS_D2H_REG
;
1959 req_task
->io_ports
[7] = reply
->command
;
1962 /* reclaim the DMA buffers.*/
1964 dma_unmap_single(&dd
->pdev
->dev
, inbuf_dma
, taskin
,
1967 dma_unmap_single(&dd
->pdev
->dev
, outbuf_dma
, taskout
,
1972 /* return the ATA registers to the caller.*/
1973 req_task
->io_ports
[1] = reply
->features
;
1974 req_task
->io_ports
[2] = reply
->sect_count
;
1975 req_task
->io_ports
[3] = reply
->lba_low
;
1976 req_task
->io_ports
[4] = reply
->lba_mid
;
1977 req_task
->io_ports
[5] = reply
->lba_hi
;
1978 req_task
->io_ports
[6] = reply
->device
;
1980 if (req_task
->out_flags
.all
& 1) {
1982 req_task
->hob_ports
[3] = reply
->lba_low_ex
;
1983 req_task
->hob_ports
[4] = reply
->lba_mid_ex
;
1984 req_task
->hob_ports
[5] = reply
->lba_hi_ex
;
1985 req_task
->hob_ports
[1] = reply
->features_ex
;
1986 req_task
->hob_ports
[2] = reply
->sect_cnt_ex
;
1988 dbg_printk(MTIP_DRV_NAME
1989 " %s: Completion: stat %x,"
1990 "err %x, sect_cnt %x, lbalo %x,"
1991 "lbamid %x, lbahi %x, dev %x\n",
1993 req_task
->io_ports
[7],
1994 req_task
->io_ports
[1],
1995 req_task
->io_ports
[2],
1996 req_task
->io_ports
[3],
1997 req_task
->io_ports
[4],
1998 req_task
->io_ports
[5],
1999 req_task
->io_ports
[6]);
2002 if (copy_to_user(buf
+ outtotal
, outbuf
, taskout
)) {
2008 if (copy_to_user(buf
+ intotal
, inbuf
, taskin
)) {
2015 dma_unmap_single(&dd
->pdev
->dev
, inbuf_dma
, taskin
,
2018 dma_unmap_single(&dd
->pdev
->dev
, outbuf_dma
, taskout
,
2027 * Handle IOCTL calls from the Block Layer.
2029 * This function is called by the Block Layer when it receives an IOCTL
2030 * command that it does not understand. If the IOCTL command is not supported
2031 * this function returns -ENOTTY.
2033 * @dd Pointer to the driver data structure.
2034 * @cmd IOCTL command passed from the Block Layer.
2035 * @arg IOCTL argument passed from the Block Layer.
2038 * 0 The IOCTL completed successfully.
2039 * -ENOTTY The specified command is not supported.
2040 * -EFAULT An error occurred copying data to a user space buffer.
2041 * -EIO An error occurred while executing the command.
2043 static int mtip_hw_ioctl(struct driver_data
*dd
, unsigned int cmd
,
2047 case HDIO_GET_IDENTITY
:
2049 if (copy_to_user((void __user
*)arg
, dd
->port
->identify
,
2050 sizeof(u16
) * ATA_ID_WORDS
))
2054 case HDIO_DRIVE_CMD
:
2056 u8 drive_command
[4];
2058 /* Copy the user command info to our buffer. */
2059 if (copy_from_user(drive_command
,
2060 (void __user
*) arg
,
2061 sizeof(drive_command
)))
2064 /* Execute the drive command. */
2065 if (exec_drive_command(dd
->port
,
2067 (void __user
*) (arg
+4)))
2070 /* Copy the status back to the users buffer. */
2071 if (copy_to_user((void __user
*) arg
,
2073 sizeof(drive_command
)))
2078 case HDIO_DRIVE_TASK
:
2080 u8 drive_command
[7];
2082 /* Copy the user command info to our buffer. */
2083 if (copy_from_user(drive_command
,
2084 (void __user
*) arg
,
2085 sizeof(drive_command
)))
2088 /* Execute the drive command. */
2089 if (exec_drive_task(dd
->port
, drive_command
))
2092 /* Copy the status back to the users buffer. */
2093 if (copy_to_user((void __user
*) arg
,
2095 sizeof(drive_command
)))
2100 case HDIO_DRIVE_TASKFILE
: {
2101 ide_task_request_t req_task
;
2104 if (copy_from_user(&req_task
, (void __user
*) arg
,
2108 outtotal
= sizeof(req_task
);
2110 ret
= exec_drive_taskfile(dd
, (void __user
*) arg
,
2111 &req_task
, outtotal
);
2113 if (copy_to_user((void __user
*) arg
, &req_task
,
2127 * Submit an IO to the hw
2129 * This function is called by the block layer to issue an io
2130 * to the device. Upon completion, the callback function will
2131 * be called with the data parameter passed as the callback data.
2133 * @dd Pointer to the driver data structure.
2134 * @start First sector to read.
2135 * @nsect Number of sectors to read.
2136 * @tag The tag of this read command.
2137 * @callback Pointer to the function that should be called
2138 * when the read completes.
2139 * @data Callback data passed to the callback function
2140 * when the read completes.
2141 * @dir Direction (read or write)
2146 static void mtip_hw_submit_io(struct driver_data
*dd
, struct request
*rq
,
2147 struct mtip_cmd
*command
,
2148 struct blk_mq_hw_ctx
*hctx
)
2150 struct mtip_cmd_hdr
*hdr
=
2151 dd
->port
->command_list
+ sizeof(struct mtip_cmd_hdr
) * rq
->tag
;
2152 struct host_to_dev_fis
*fis
;
2153 struct mtip_port
*port
= dd
->port
;
2154 int dma_dir
= rq_data_dir(rq
) == READ
? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
2155 u64 start
= blk_rq_pos(rq
);
2156 unsigned int nsect
= blk_rq_sectors(rq
);
2159 /* Map the scatter list for DMA access */
2160 nents
= blk_rq_map_sg(hctx
->queue
, rq
, command
->sg
);
2161 nents
= dma_map_sg(&dd
->pdev
->dev
, command
->sg
, nents
, dma_dir
);
2163 prefetch(&port
->flags
);
2165 command
->scatter_ents
= nents
;
2168 * The number of retries for this command before it is
2169 * reported as a failure to the upper layers.
2171 command
->retries
= MTIP_MAX_RETRIES
;
2174 fis
= command
->command
;
2177 if (dma_dir
== DMA_FROM_DEVICE
)
2178 fis
->command
= ATA_CMD_FPDMA_READ
;
2180 fis
->command
= ATA_CMD_FPDMA_WRITE
;
2181 fis
->lba_low
= start
& 0xFF;
2182 fis
->lba_mid
= (start
>> 8) & 0xFF;
2183 fis
->lba_hi
= (start
>> 16) & 0xFF;
2184 fis
->lba_low_ex
= (start
>> 24) & 0xFF;
2185 fis
->lba_mid_ex
= (start
>> 32) & 0xFF;
2186 fis
->lba_hi_ex
= (start
>> 40) & 0xFF;
2187 fis
->device
= 1 << 6;
2188 fis
->features
= nsect
& 0xFF;
2189 fis
->features_ex
= (nsect
>> 8) & 0xFF;
2190 fis
->sect_count
= ((rq
->tag
<< 3) | (rq
->tag
>> 5));
2191 fis
->sect_cnt_ex
= 0;
2195 fill_command_sg(dd
, command
, nents
);
2197 if (unlikely(command
->unaligned
))
2198 fis
->device
|= 1 << 7;
2200 /* Populate the command header */
2201 hdr
->ctba
= cpu_to_le32(command
->command_dma
& 0xFFFFFFFF);
2202 if (test_bit(MTIP_PF_HOST_CAP_64
, &dd
->port
->flags
))
2203 hdr
->ctbau
= cpu_to_le32((command
->command_dma
>> 16) >> 16);
2204 hdr
->opts
= cpu_to_le32((nents
<< 16) | 5 | AHCI_CMD_PREFETCH
);
2205 hdr
->byte_count
= 0;
2207 command
->direction
= dma_dir
;
2210 * To prevent this command from being issued
2211 * if an internal command is in progress or error handling is active.
2213 if (unlikely(port
->flags
& MTIP_PF_PAUSE_IO
)) {
2214 set_bit(rq
->tag
, port
->cmds_to_issue
);
2215 set_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
);
2219 /* Issue the command to the hardware */
2220 mtip_issue_ncq_command(port
, rq
->tag
);
2224 * Sysfs status dump.
2226 * @dev Pointer to the device structure, passed by the kernrel.
2227 * @attr Pointer to the device_attribute structure passed by the kernel.
2228 * @buf Pointer to the char buffer that will receive the stats info.
2231 * The size, in bytes, of the data copied into buf.
2233 static ssize_t
mtip_hw_show_status(struct device
*dev
,
2234 struct device_attribute
*attr
,
2237 struct driver_data
*dd
= dev_to_disk(dev
)->private_data
;
2240 if (test_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
))
2241 size
+= sprintf(buf
, "%s", "thermal_shutdown\n");
2242 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
))
2243 size
+= sprintf(buf
, "%s", "write_protect\n");
2245 size
+= sprintf(buf
, "%s", "online\n");
2250 static DEVICE_ATTR(status
, 0444, mtip_hw_show_status
, NULL
);
2252 /* debugsfs entries */
2254 static ssize_t
show_device_status(struct device_driver
*drv
, char *buf
)
2257 struct driver_data
*dd
, *tmp
;
2258 unsigned long flags
;
2262 spin_lock_irqsave(&dev_lock
, flags
);
2263 size
+= sprintf(&buf
[size
], "Devices Present:\n");
2264 list_for_each_entry_safe(dd
, tmp
, &online_list
, online_list
) {
2267 dd
->port
->identify
&&
2268 dd
->port
->identify_valid
) {
2270 (char *) (dd
->port
->identify
+ 10), 21);
2271 status
= *(dd
->port
->identify
+ 141);
2273 memset(id_buf
, 0, 42);
2278 test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
)) {
2279 size
+= sprintf(&buf
[size
],
2280 " device %s %s (ftl rebuild %d %%)\n",
2281 dev_name(&dd
->pdev
->dev
),
2285 size
+= sprintf(&buf
[size
],
2287 dev_name(&dd
->pdev
->dev
),
2293 size
+= sprintf(&buf
[size
], "Devices Being Removed:\n");
2294 list_for_each_entry_safe(dd
, tmp
, &removing_list
, remove_list
) {
2297 dd
->port
->identify
&&
2298 dd
->port
->identify_valid
) {
2300 (char *) (dd
->port
->identify
+10), 21);
2301 status
= *(dd
->port
->identify
+ 141);
2303 memset(id_buf
, 0, 42);
2308 test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
)) {
2309 size
+= sprintf(&buf
[size
],
2310 " device %s %s (ftl rebuild %d %%)\n",
2311 dev_name(&dd
->pdev
->dev
),
2315 size
+= sprintf(&buf
[size
],
2317 dev_name(&dd
->pdev
->dev
),
2322 spin_unlock_irqrestore(&dev_lock
, flags
);
2327 static ssize_t
mtip_hw_read_device_status(struct file
*f
, char __user
*ubuf
,
2328 size_t len
, loff_t
*offset
)
2330 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2335 if (!len
|| *offset
)
2338 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2340 dev_err(&dd
->pdev
->dev
,
2341 "Memory allocation: status buffer\n");
2345 size
+= show_device_status(NULL
, buf
);
2347 *offset
= size
<= len
? size
: len
;
2348 size
= copy_to_user(ubuf
, buf
, *offset
);
2353 return rv
? rv
: *offset
;
2356 static ssize_t
mtip_hw_read_registers(struct file
*f
, char __user
*ubuf
,
2357 size_t len
, loff_t
*offset
)
2359 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2361 u32 group_allocated
;
2368 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2370 dev_err(&dd
->pdev
->dev
,
2371 "Memory allocation: register buffer\n");
2375 size
+= sprintf(&buf
[size
], "H/ S ACTive : [ 0x");
2377 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2378 size
+= sprintf(&buf
[size
], "%08X ",
2379 readl(dd
->port
->s_active
[n
]));
2381 size
+= sprintf(&buf
[size
], "]\n");
2382 size
+= sprintf(&buf
[size
], "H/ Command Issue : [ 0x");
2384 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2385 size
+= sprintf(&buf
[size
], "%08X ",
2386 readl(dd
->port
->cmd_issue
[n
]));
2388 size
+= sprintf(&buf
[size
], "]\n");
2389 size
+= sprintf(&buf
[size
], "H/ Completed : [ 0x");
2391 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2392 size
+= sprintf(&buf
[size
], "%08X ",
2393 readl(dd
->port
->completed
[n
]));
2395 size
+= sprintf(&buf
[size
], "]\n");
2396 size
+= sprintf(&buf
[size
], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2397 readl(dd
->port
->mmio
+ PORT_IRQ_STAT
));
2398 size
+= sprintf(&buf
[size
], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2399 readl(dd
->mmio
+ HOST_IRQ_STAT
));
2400 size
+= sprintf(&buf
[size
], "\n");
2402 size
+= sprintf(&buf
[size
], "L/ Commands in Q : [ 0x");
2404 for (n
= dd
->slot_groups
-1; n
>= 0; n
--) {
2405 if (sizeof(long) > sizeof(u32
))
2407 dd
->port
->cmds_to_issue
[n
/2] >> (32*(n
&1));
2409 group_allocated
= dd
->port
->cmds_to_issue
[n
];
2410 size
+= sprintf(&buf
[size
], "%08X ", group_allocated
);
2412 size
+= sprintf(&buf
[size
], "]\n");
2414 *offset
= size
<= len
? size
: len
;
2415 size
= copy_to_user(ubuf
, buf
, *offset
);
2420 return rv
? rv
: *offset
;
2423 static ssize_t
mtip_hw_read_flags(struct file
*f
, char __user
*ubuf
,
2424 size_t len
, loff_t
*offset
)
2426 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2434 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2436 dev_err(&dd
->pdev
->dev
,
2437 "Memory allocation: flag buffer\n");
2441 size
+= sprintf(&buf
[size
], "Flag-port : [ %08lX ]\n",
2443 size
+= sprintf(&buf
[size
], "Flag-dd : [ %08lX ]\n",
2446 *offset
= size
<= len
? size
: len
;
2447 size
= copy_to_user(ubuf
, buf
, *offset
);
2452 return rv
? rv
: *offset
;
2455 static const struct file_operations mtip_device_status_fops
= {
2456 .owner
= THIS_MODULE
,
2457 .open
= simple_open
,
2458 .read
= mtip_hw_read_device_status
,
2459 .llseek
= no_llseek
,
2462 static const struct file_operations mtip_regs_fops
= {
2463 .owner
= THIS_MODULE
,
2464 .open
= simple_open
,
2465 .read
= mtip_hw_read_registers
,
2466 .llseek
= no_llseek
,
2469 static const struct file_operations mtip_flags_fops
= {
2470 .owner
= THIS_MODULE
,
2471 .open
= simple_open
,
2472 .read
= mtip_hw_read_flags
,
2473 .llseek
= no_llseek
,
2477 * Create the sysfs related attributes.
2479 * @dd Pointer to the driver data structure.
2480 * @kobj Pointer to the kobj for the block device.
2483 * 0 Operation completed successfully.
2484 * -EINVAL Invalid parameter.
2486 static int mtip_hw_sysfs_init(struct driver_data
*dd
, struct kobject
*kobj
)
2491 if (sysfs_create_file(kobj
, &dev_attr_status
.attr
))
2492 dev_warn(&dd
->pdev
->dev
,
2493 "Error creating 'status' sysfs entry\n");
2498 * Remove the sysfs related attributes.
2500 * @dd Pointer to the driver data structure.
2501 * @kobj Pointer to the kobj for the block device.
2504 * 0 Operation completed successfully.
2505 * -EINVAL Invalid parameter.
2507 static int mtip_hw_sysfs_exit(struct driver_data
*dd
, struct kobject
*kobj
)
2512 sysfs_remove_file(kobj
, &dev_attr_status
.attr
);
2517 static int mtip_hw_debugfs_init(struct driver_data
*dd
)
2522 dd
->dfs_node
= debugfs_create_dir(dd
->disk
->disk_name
, dfs_parent
);
2523 if (IS_ERR_OR_NULL(dd
->dfs_node
)) {
2524 dev_warn(&dd
->pdev
->dev
,
2525 "Error creating node %s under debugfs\n",
2526 dd
->disk
->disk_name
);
2527 dd
->dfs_node
= NULL
;
2531 debugfs_create_file("flags", 0444, dd
->dfs_node
, dd
, &mtip_flags_fops
);
2532 debugfs_create_file("registers", 0444, dd
->dfs_node
, dd
,
2538 static void mtip_hw_debugfs_exit(struct driver_data
*dd
)
2540 debugfs_remove_recursive(dd
->dfs_node
);
2544 * Perform any init/resume time hardware setup
2546 * @dd Pointer to the driver data structure.
2551 static inline void hba_setup(struct driver_data
*dd
)
2554 hwdata
= readl(dd
->mmio
+ HOST_HSORG
);
2556 /* interrupt bug workaround: use only 1 IS bit.*/
2558 HSORG_DISABLE_SLOTGRP_INTR
|
2559 HSORG_DISABLE_SLOTGRP_PXIS
,
2560 dd
->mmio
+ HOST_HSORG
);
2563 static int mtip_device_unaligned_constrained(struct driver_data
*dd
)
2565 return (dd
->pdev
->device
== P420M_DEVICE_ID
? 1 : 0);
2569 * Detect the details of the product, and store anything needed
2570 * into the driver data structure. This includes product type and
2571 * version and number of slot groups.
2573 * @dd Pointer to the driver data structure.
2578 static void mtip_detect_product(struct driver_data
*dd
)
2581 unsigned int rev
, slotgroups
;
2584 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2586 * [15:8] hardware/software interface rev#
2587 * [ 3] asic-style interface
2588 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2590 hwdata
= readl(dd
->mmio
+ HOST_HSORG
);
2592 dd
->product_type
= MTIP_PRODUCT_UNKNOWN
;
2593 dd
->slot_groups
= 1;
2596 dd
->product_type
= MTIP_PRODUCT_ASICFPGA
;
2597 rev
= (hwdata
& HSORG_HWREV
) >> 8;
2598 slotgroups
= (hwdata
& HSORG_SLOTGROUPS
) + 1;
2599 dev_info(&dd
->pdev
->dev
,
2600 "ASIC-FPGA design, HS rev 0x%x, "
2601 "%i slot groups [%i slots]\n",
2606 if (slotgroups
> MTIP_MAX_SLOT_GROUPS
) {
2607 dev_warn(&dd
->pdev
->dev
,
2608 "Warning: driver only supports "
2609 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS
);
2610 slotgroups
= MTIP_MAX_SLOT_GROUPS
;
2612 dd
->slot_groups
= slotgroups
;
2616 dev_warn(&dd
->pdev
->dev
, "Unrecognized product id\n");
2620 * Blocking wait for FTL rebuild to complete
2622 * @dd Pointer to the DRIVER_DATA structure.
2625 * 0 FTL rebuild completed successfully
2626 * -EFAULT FTL rebuild error/timeout/interruption
2628 static int mtip_ftl_rebuild_poll(struct driver_data
*dd
)
2630 unsigned long timeout
, cnt
= 0, start
;
2632 dev_warn(&dd
->pdev
->dev
,
2633 "FTL rebuild in progress. Polling for completion.\n");
2636 timeout
= jiffies
+ msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS
);
2639 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
2642 if (mtip_check_surprise_removal(dd
->pdev
))
2645 if (mtip_get_identify(dd
->port
, NULL
) < 0)
2648 if (*(dd
->port
->identify
+ MTIP_FTL_REBUILD_OFFSET
) ==
2649 MTIP_FTL_REBUILD_MAGIC
) {
2651 /* Print message every 3 minutes */
2653 dev_warn(&dd
->pdev
->dev
,
2654 "FTL rebuild in progress (%d secs).\n",
2655 jiffies_to_msecs(jiffies
- start
) / 1000);
2659 dev_warn(&dd
->pdev
->dev
,
2660 "FTL rebuild complete (%d secs).\n",
2661 jiffies_to_msecs(jiffies
- start
) / 1000);
2662 mtip_block_initialize(dd
);
2665 } while (time_before(jiffies
, timeout
));
2667 /* Check for timeout */
2668 dev_err(&dd
->pdev
->dev
,
2669 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2670 jiffies_to_msecs(jiffies
- start
) / 1000);
2674 static void mtip_softirq_done_fn(struct request
*rq
)
2676 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
2677 struct driver_data
*dd
= rq
->q
->queuedata
;
2679 /* Unmap the DMA scatter list entries */
2680 dma_unmap_sg(&dd
->pdev
->dev
, cmd
->sg
, cmd
->scatter_ents
,
2683 if (unlikely(cmd
->unaligned
))
2684 atomic_inc(&dd
->port
->cmd_slot_unal
);
2686 blk_mq_end_request(rq
, cmd
->status
);
2689 static bool mtip_abort_cmd(struct request
*req
, void *data
, bool reserved
)
2691 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
2692 struct driver_data
*dd
= data
;
2694 dbg_printk(MTIP_DRV_NAME
" Aborting request, tag = %d\n", req
->tag
);
2696 clear_bit(req
->tag
, dd
->port
->cmds_to_issue
);
2697 cmd
->status
= BLK_STS_IOERR
;
2698 mtip_softirq_done_fn(req
);
2702 static bool mtip_queue_cmd(struct request
*req
, void *data
, bool reserved
)
2704 struct driver_data
*dd
= data
;
2706 set_bit(req
->tag
, dd
->port
->cmds_to_issue
);
2707 blk_abort_request(req
);
2712 * service thread to issue queued commands
2714 * @data Pointer to the driver data structure.
2720 static int mtip_service_thread(void *data
)
2722 struct driver_data
*dd
= (struct driver_data
*)data
;
2723 unsigned long slot
, slot_start
, slot_wrap
, to
;
2724 unsigned int num_cmd_slots
= dd
->slot_groups
* 32;
2725 struct mtip_port
*port
= dd
->port
;
2728 if (kthread_should_stop() ||
2729 test_bit(MTIP_PF_SVC_THD_STOP_BIT
, &port
->flags
))
2731 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
);
2734 * the condition is to check neither an internal command is
2735 * is in progress nor error handling is active
2737 wait_event_interruptible(port
->svc_wait
, (port
->flags
) &&
2738 (port
->flags
& MTIP_PF_SVC_THD_WORK
));
2740 if (kthread_should_stop() ||
2741 test_bit(MTIP_PF_SVC_THD_STOP_BIT
, &port
->flags
))
2744 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
2748 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
);
2751 /* Demux bits: start with error handling */
2752 if (test_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
)) {
2753 mtip_handle_tfe(dd
);
2754 clear_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
);
2757 if (test_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
))
2760 if (test_bit(MTIP_PF_TO_ACTIVE_BIT
, &port
->flags
)) {
2761 to
= jiffies
+ msecs_to_jiffies(5000);
2765 } while (atomic_read(&dd
->irq_workers_active
) != 0 &&
2766 time_before(jiffies
, to
));
2768 if (atomic_read(&dd
->irq_workers_active
) != 0)
2769 dev_warn(&dd
->pdev
->dev
,
2770 "Completion workers still active!");
2772 blk_mq_quiesce_queue(dd
->queue
);
2774 blk_mq_tagset_busy_iter(&dd
->tags
, mtip_queue_cmd
, dd
);
2776 set_bit(MTIP_PF_ISSUE_CMDS_BIT
, &dd
->port
->flags
);
2778 if (mtip_device_reset(dd
))
2779 blk_mq_tagset_busy_iter(&dd
->tags
,
2780 mtip_abort_cmd
, dd
);
2782 clear_bit(MTIP_PF_TO_ACTIVE_BIT
, &dd
->port
->flags
);
2784 blk_mq_unquiesce_queue(dd
->queue
);
2787 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
)) {
2789 /* used to restrict the loop to one iteration */
2790 slot_start
= num_cmd_slots
;
2793 slot
= find_next_bit(port
->cmds_to_issue
,
2794 num_cmd_slots
, slot
);
2795 if (slot_wrap
== 1) {
2796 if ((slot_start
>= slot
) ||
2797 (slot
>= num_cmd_slots
))
2800 if (unlikely(slot_start
== num_cmd_slots
))
2803 if (unlikely(slot
== num_cmd_slots
)) {
2809 /* Issue the command to the hardware */
2810 mtip_issue_ncq_command(port
, slot
);
2812 clear_bit(slot
, port
->cmds_to_issue
);
2815 clear_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
);
2818 if (test_bit(MTIP_PF_REBUILD_BIT
, &port
->flags
)) {
2819 if (mtip_ftl_rebuild_poll(dd
) == 0)
2820 clear_bit(MTIP_PF_REBUILD_BIT
, &port
->flags
);
2829 * DMA region teardown
2831 * @dd Pointer to driver_data structure
2836 static void mtip_dma_free(struct driver_data
*dd
)
2838 struct mtip_port
*port
= dd
->port
;
2841 dma_free_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
2842 port
->block1
, port
->block1_dma
);
2844 if (port
->command_list
) {
2845 dma_free_coherent(&dd
->pdev
->dev
, AHCI_CMD_TBL_SZ
,
2846 port
->command_list
, port
->command_list_dma
);
2853 * @dd Pointer to driver_data structure
2856 * -ENOMEM Not enough free DMA region space to initialize driver
2858 static int mtip_dma_alloc(struct driver_data
*dd
)
2860 struct mtip_port
*port
= dd
->port
;
2862 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
2864 dma_alloc_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
2865 &port
->block1_dma
, GFP_KERNEL
);
2868 memset(port
->block1
, 0, BLOCK_DMA_ALLOC_SZ
);
2870 /* Allocate dma memory for command list */
2871 port
->command_list
=
2872 dma_alloc_coherent(&dd
->pdev
->dev
, AHCI_CMD_TBL_SZ
,
2873 &port
->command_list_dma
, GFP_KERNEL
);
2874 if (!port
->command_list
) {
2875 dma_free_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
2876 port
->block1
, port
->block1_dma
);
2877 port
->block1
= NULL
;
2878 port
->block1_dma
= 0;
2881 memset(port
->command_list
, 0, AHCI_CMD_TBL_SZ
);
2883 /* Setup all pointers into first DMA region */
2884 port
->rxfis
= port
->block1
+ AHCI_RX_FIS_OFFSET
;
2885 port
->rxfis_dma
= port
->block1_dma
+ AHCI_RX_FIS_OFFSET
;
2886 port
->identify
= port
->block1
+ AHCI_IDFY_OFFSET
;
2887 port
->identify_dma
= port
->block1_dma
+ AHCI_IDFY_OFFSET
;
2888 port
->log_buf
= port
->block1
+ AHCI_SECTBUF_OFFSET
;
2889 port
->log_buf_dma
= port
->block1_dma
+ AHCI_SECTBUF_OFFSET
;
2890 port
->smart_buf
= port
->block1
+ AHCI_SMARTBUF_OFFSET
;
2891 port
->smart_buf_dma
= port
->block1_dma
+ AHCI_SMARTBUF_OFFSET
;
2896 static int mtip_hw_get_identify(struct driver_data
*dd
)
2898 struct smart_attr attr242
;
2902 if (mtip_get_identify(dd
->port
, NULL
) < 0)
2905 if (*(dd
->port
->identify
+ MTIP_FTL_REBUILD_OFFSET
) ==
2906 MTIP_FTL_REBUILD_MAGIC
) {
2907 set_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
);
2908 return MTIP_FTL_REBUILD_MAGIC
;
2910 mtip_dump_identify(dd
->port
);
2912 /* check write protect, over temp and rebuild statuses */
2913 rv
= mtip_read_log_page(dd
->port
, ATA_LOG_SATA_NCQ
,
2915 dd
->port
->log_buf_dma
, 1);
2917 dev_warn(&dd
->pdev
->dev
,
2918 "Error in READ LOG EXT (10h) command\n");
2919 /* non-critical error, don't fail the load */
2921 buf
= (unsigned char *)dd
->port
->log_buf
;
2922 if (buf
[259] & 0x1) {
2923 dev_info(&dd
->pdev
->dev
,
2924 "Write protect bit is set.\n");
2925 set_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
);
2927 if (buf
[288] == 0xF7) {
2928 dev_info(&dd
->pdev
->dev
,
2929 "Exceeded Tmax, drive in thermal shutdown.\n");
2930 set_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
);
2932 if (buf
[288] == 0xBF) {
2933 dev_info(&dd
->pdev
->dev
,
2934 "Drive indicates rebuild has failed.\n");
2935 set_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
);
2939 /* get write protect progess */
2940 memset(&attr242
, 0, sizeof(struct smart_attr
));
2941 if (mtip_get_smart_attr(dd
->port
, 242, &attr242
))
2942 dev_warn(&dd
->pdev
->dev
,
2943 "Unable to check write protect progress\n");
2945 dev_info(&dd
->pdev
->dev
,
2946 "Write protect progress: %u%% (%u blocks)\n",
2947 attr242
.cur
, le32_to_cpu(attr242
.data
));
2953 * Called once for each card.
2955 * @dd Pointer to the driver data structure.
2958 * 0 on success, else an error code.
2960 static int mtip_hw_init(struct driver_data
*dd
)
2964 unsigned long timeout
, timetaken
;
2966 dd
->mmio
= pcim_iomap_table(dd
->pdev
)[MTIP_ABAR
];
2968 mtip_detect_product(dd
);
2969 if (dd
->product_type
== MTIP_PRODUCT_UNKNOWN
) {
2976 dd
->port
= kzalloc_node(sizeof(struct mtip_port
), GFP_KERNEL
,
2979 dev_err(&dd
->pdev
->dev
,
2980 "Memory allocation: port structure\n");
2984 /* Continue workqueue setup */
2985 for (i
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++)
2986 dd
->work
[i
].port
= dd
->port
;
2988 /* Enable unaligned IO constraints for some devices */
2989 if (mtip_device_unaligned_constrained(dd
))
2990 dd
->unal_qdepth
= MTIP_MAX_UNALIGNED_SLOTS
;
2992 dd
->unal_qdepth
= 0;
2994 atomic_set(&dd
->port
->cmd_slot_unal
, dd
->unal_qdepth
);
2996 /* Spinlock to prevent concurrent issue */
2997 for (i
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++)
2998 spin_lock_init(&dd
->port
->cmd_issue_lock
[i
]);
3000 /* Set the port mmio base address. */
3001 dd
->port
->mmio
= dd
->mmio
+ PORT_OFFSET
;
3004 /* DMA allocations */
3005 rv
= mtip_dma_alloc(dd
);
3009 /* Setup the pointers to the extended s_active and CI registers. */
3010 for (i
= 0; i
< dd
->slot_groups
; i
++) {
3011 dd
->port
->s_active
[i
] =
3012 dd
->port
->mmio
+ i
*0x80 + PORT_SCR_ACT
;
3013 dd
->port
->cmd_issue
[i
] =
3014 dd
->port
->mmio
+ i
*0x80 + PORT_COMMAND_ISSUE
;
3015 dd
->port
->completed
[i
] =
3016 dd
->port
->mmio
+ i
*0x80 + PORT_SDBV
;
3019 timetaken
= jiffies
;
3020 timeout
= jiffies
+ msecs_to_jiffies(30000);
3021 while (((readl(dd
->port
->mmio
+ PORT_SCR_STAT
) & 0x0F) != 0x03) &&
3022 time_before(jiffies
, timeout
)) {
3025 if (unlikely(mtip_check_surprise_removal(dd
->pdev
))) {
3026 timetaken
= jiffies
- timetaken
;
3027 dev_warn(&dd
->pdev
->dev
,
3028 "Surprise removal detected at %u ms\n",
3029 jiffies_to_msecs(timetaken
));
3033 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))) {
3034 timetaken
= jiffies
- timetaken
;
3035 dev_warn(&dd
->pdev
->dev
,
3036 "Removal detected at %u ms\n",
3037 jiffies_to_msecs(timetaken
));
3042 /* Conditionally reset the HBA. */
3043 if (!(readl(dd
->mmio
+ HOST_CAP
) & HOST_CAP_NZDMA
)) {
3044 if (mtip_hba_reset(dd
) < 0) {
3045 dev_err(&dd
->pdev
->dev
,
3046 "Card did not reset within timeout\n");
3051 /* Clear any pending interrupts on the HBA */
3052 writel(readl(dd
->mmio
+ HOST_IRQ_STAT
),
3053 dd
->mmio
+ HOST_IRQ_STAT
);
3056 mtip_init_port(dd
->port
);
3057 mtip_start_port(dd
->port
);
3059 /* Setup the ISR and enable interrupts. */
3060 rv
= request_irq(dd
->pdev
->irq
, mtip_irq_handler
, IRQF_SHARED
,
3061 dev_driver_string(&dd
->pdev
->dev
), dd
);
3063 dev_err(&dd
->pdev
->dev
,
3064 "Unable to allocate IRQ %d\n", dd
->pdev
->irq
);
3067 irq_set_affinity_hint(dd
->pdev
->irq
, get_cpu_mask(dd
->isr_binding
));
3069 /* Enable interrupts on the HBA. */
3070 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
3071 dd
->mmio
+ HOST_CTL
);
3073 init_waitqueue_head(&dd
->port
->svc_wait
);
3075 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)) {
3083 /* Disable interrupts on the HBA. */
3084 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3085 dd
->mmio
+ HOST_CTL
);
3087 /* Release the IRQ. */
3088 irq_set_affinity_hint(dd
->pdev
->irq
, NULL
);
3089 free_irq(dd
->pdev
->irq
, dd
);
3092 mtip_deinit_port(dd
->port
);
3096 /* Free the memory allocated for the for structure. */
3102 static int mtip_standby_drive(struct driver_data
*dd
)
3106 if (dd
->sr
|| !dd
->port
)
3109 * Send standby immediate (E0h) to the drive so that it
3112 if (!test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
) &&
3113 !test_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
) &&
3114 !test_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
)) {
3115 rv
= mtip_standby_immediate(dd
->port
);
3117 dev_warn(&dd
->pdev
->dev
,
3118 "STANDBY IMMEDIATE failed\n");
3124 * Called to deinitialize an interface.
3126 * @dd Pointer to the driver data structure.
3131 static int mtip_hw_exit(struct driver_data
*dd
)
3134 /* de-initialize the port. */
3135 mtip_deinit_port(dd
->port
);
3137 /* Disable interrupts on the HBA. */
3138 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3139 dd
->mmio
+ HOST_CTL
);
3142 /* Release the IRQ. */
3143 irq_set_affinity_hint(dd
->pdev
->irq
, NULL
);
3144 free_irq(dd
->pdev
->irq
, dd
);
3147 /* Free dma regions */
3150 /* Free the memory allocated for the for structure. */
3158 * Issue a Standby Immediate command to the device.
3160 * This function is called by the Block Layer just before the
3161 * system powers off during a shutdown.
3163 * @dd Pointer to the driver data structure.
3168 static int mtip_hw_shutdown(struct driver_data
*dd
)
3171 * Send standby immediate (E0h) to the drive so that it
3174 mtip_standby_drive(dd
);
3182 * This function is called by the Block Layer just before the
3183 * system hibernates.
3185 * @dd Pointer to the driver data structure.
3188 * 0 Suspend was successful
3189 * -EFAULT Suspend was not successful
3191 static int mtip_hw_suspend(struct driver_data
*dd
)
3194 * Send standby immediate (E0h) to the drive
3195 * so that it saves its state.
3197 if (mtip_standby_drive(dd
) != 0) {
3198 dev_err(&dd
->pdev
->dev
,
3199 "Failed standby-immediate command\n");
3203 /* Disable interrupts on the HBA.*/
3204 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3205 dd
->mmio
+ HOST_CTL
);
3206 mtip_deinit_port(dd
->port
);
3214 * This function is called by the Block Layer as the
3217 * @dd Pointer to the driver data structure.
3220 * 0 Resume was successful
3221 * -EFAULT Resume was not successful
3223 static int mtip_hw_resume(struct driver_data
*dd
)
3225 /* Perform any needed hardware setup steps */
3229 if (mtip_hba_reset(dd
) != 0) {
3230 dev_err(&dd
->pdev
->dev
,
3231 "Unable to reset the HBA\n");
3236 * Enable the port, DMA engine, and FIS reception specific
3237 * h/w in controller.
3239 mtip_init_port(dd
->port
);
3240 mtip_start_port(dd
->port
);
3242 /* Enable interrupts on the HBA.*/
3243 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
3244 dd
->mmio
+ HOST_CTL
);
3250 * Helper function for reusing disk name
3251 * upon hot insertion.
3253 static int rssd_disk_name_format(char *prefix
,
3258 const int base
= 'z' - 'a' + 1;
3259 char *begin
= buf
+ strlen(prefix
);
3260 char *end
= buf
+ buflen
;
3270 *--p
= 'a' + (index
% unit
);
3271 index
= (index
/ unit
) - 1;
3272 } while (index
>= 0);
3274 memmove(begin
, p
, end
- p
);
3275 memcpy(buf
, prefix
, strlen(prefix
));
3281 * Block layer IOCTL handler.
3283 * @dev Pointer to the block_device structure.
3285 * @cmd IOCTL command passed from the user application.
3286 * @arg Argument passed from the user application.
3289 * 0 IOCTL completed successfully.
3290 * -ENOTTY IOCTL not supported or invalid driver data
3291 * structure pointer.
3293 static int mtip_block_ioctl(struct block_device
*dev
,
3298 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3300 if (!capable(CAP_SYS_ADMIN
))
3306 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)))
3313 return mtip_hw_ioctl(dd
, cmd
, arg
);
3317 #ifdef CONFIG_COMPAT
3319 * Block layer compat IOCTL handler.
3321 * @dev Pointer to the block_device structure.
3323 * @cmd IOCTL command passed from the user application.
3324 * @arg Argument passed from the user application.
3327 * 0 IOCTL completed successfully.
3328 * -ENOTTY IOCTL not supported or invalid driver data
3329 * structure pointer.
3331 static int mtip_block_compat_ioctl(struct block_device
*dev
,
3336 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3338 if (!capable(CAP_SYS_ADMIN
))
3344 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)))
3350 case HDIO_DRIVE_TASKFILE
: {
3351 struct mtip_compat_ide_task_request_s __user
*compat_req_task
;
3352 ide_task_request_t req_task
;
3353 int compat_tasksize
, outtotal
, ret
;
3356 sizeof(struct mtip_compat_ide_task_request_s
);
3359 (struct mtip_compat_ide_task_request_s __user
*) arg
;
3361 if (copy_from_user(&req_task
, (void __user
*) arg
,
3362 compat_tasksize
- (2 * sizeof(compat_long_t
))))
3365 if (get_user(req_task
.out_size
, &compat_req_task
->out_size
))
3368 if (get_user(req_task
.in_size
, &compat_req_task
->in_size
))
3371 outtotal
= sizeof(struct mtip_compat_ide_task_request_s
);
3373 ret
= exec_drive_taskfile(dd
, (void __user
*) arg
,
3374 &req_task
, outtotal
);
3376 if (copy_to_user((void __user
*) arg
, &req_task
,
3378 (2 * sizeof(compat_long_t
))))
3381 if (put_user(req_task
.out_size
, &compat_req_task
->out_size
))
3384 if (put_user(req_task
.in_size
, &compat_req_task
->in_size
))
3390 return mtip_hw_ioctl(dd
, cmd
, arg
);
3396 * Obtain the geometry of the device.
3398 * You may think that this function is obsolete, but some applications,
3399 * fdisk for example still used CHS values. This function describes the
3400 * device as having 224 heads and 56 sectors per cylinder. These values are
3401 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3402 * partition is described in terms of a start and end cylinder this means
3403 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3404 * affects performance.
3406 * @dev Pointer to the block_device strucutre.
3407 * @geo Pointer to a hd_geometry structure.
3410 * 0 Operation completed successfully.
3411 * -ENOTTY An error occurred while reading the drive capacity.
3413 static int mtip_block_getgeo(struct block_device
*dev
,
3414 struct hd_geometry
*geo
)
3416 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3422 if (!(mtip_hw_get_capacity(dd
, &capacity
))) {
3423 dev_warn(&dd
->pdev
->dev
,
3424 "Could not get drive capacity.\n");
3430 sector_div(capacity
, (geo
->heads
* geo
->sectors
));
3431 geo
->cylinders
= capacity
;
3435 static int mtip_block_open(struct block_device
*dev
, fmode_t mode
)
3437 struct driver_data
*dd
;
3439 if (dev
&& dev
->bd_disk
) {
3440 dd
= (struct driver_data
*) dev
->bd_disk
->private_data
;
3443 if (test_bit(MTIP_DDF_REMOVAL_BIT
,
3453 static void mtip_block_release(struct gendisk
*disk
, fmode_t mode
)
3458 * Block device operation function.
3460 * This structure contains pointers to the functions required by the block
3463 static const struct block_device_operations mtip_block_ops
= {
3464 .open
= mtip_block_open
,
3465 .release
= mtip_block_release
,
3466 .ioctl
= mtip_block_ioctl
,
3467 #ifdef CONFIG_COMPAT
3468 .compat_ioctl
= mtip_block_compat_ioctl
,
3470 .getgeo
= mtip_block_getgeo
,
3471 .owner
= THIS_MODULE
3474 static inline bool is_se_active(struct driver_data
*dd
)
3476 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT
, &dd
->port
->flags
))) {
3477 if (dd
->port
->ic_pause_timer
) {
3478 unsigned long to
= dd
->port
->ic_pause_timer
+
3479 msecs_to_jiffies(1000);
3480 if (time_after(jiffies
, to
)) {
3481 clear_bit(MTIP_PF_SE_ACTIVE_BIT
,
3483 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
);
3484 dd
->port
->ic_pause_timer
= 0;
3485 wake_up_interruptible(&dd
->port
->svc_wait
);
3494 static inline bool is_stopped(struct driver_data
*dd
, struct request
*rq
)
3496 if (likely(!(dd
->dd_flag
& MTIP_DDF_STOP_IO
)))
3499 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))
3501 if (test_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
))
3503 if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
) &&
3506 if (test_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
))
3508 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
))
3514 static bool mtip_check_unal_depth(struct blk_mq_hw_ctx
*hctx
,
3517 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3518 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3520 if (rq_data_dir(rq
) == READ
|| !dd
->unal_qdepth
)
3524 * If unaligned depth must be limited on this controller, mark it
3525 * as unaligned if the IO isn't on a 4k boundary (start of length).
3527 if (blk_rq_sectors(rq
) <= 64) {
3528 if ((blk_rq_pos(rq
) & 7) || (blk_rq_sectors(rq
) & 7))
3532 if (cmd
->unaligned
&& atomic_dec_if_positive(&dd
->port
->cmd_slot_unal
) >= 0)
3538 static blk_status_t
mtip_issue_reserved_cmd(struct blk_mq_hw_ctx
*hctx
,
3541 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3542 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3543 struct mtip_int_cmd
*icmd
= cmd
->icmd
;
3544 struct mtip_cmd_hdr
*hdr
=
3545 dd
->port
->command_list
+ sizeof(struct mtip_cmd_hdr
) * rq
->tag
;
3546 struct mtip_cmd_sg
*command_sg
;
3548 if (mtip_commands_active(dd
->port
))
3549 return BLK_STS_DEV_RESOURCE
;
3551 hdr
->ctba
= cpu_to_le32(cmd
->command_dma
& 0xFFFFFFFF);
3552 if (test_bit(MTIP_PF_HOST_CAP_64
, &dd
->port
->flags
))
3553 hdr
->ctbau
= cpu_to_le32((cmd
->command_dma
>> 16) >> 16);
3554 /* Populate the SG list */
3555 hdr
->opts
= cpu_to_le32(icmd
->opts
| icmd
->fis_len
);
3556 if (icmd
->buf_len
) {
3557 command_sg
= cmd
->command
+ AHCI_CMD_TBL_HDR_SZ
;
3559 command_sg
->info
= cpu_to_le32((icmd
->buf_len
-1) & 0x3FFFFF);
3560 command_sg
->dba
= cpu_to_le32(icmd
->buffer
& 0xFFFFFFFF);
3561 command_sg
->dba_upper
=
3562 cpu_to_le32((icmd
->buffer
>> 16) >> 16);
3564 hdr
->opts
|= cpu_to_le32((1 << 16));
3567 /* Populate the command header */
3568 hdr
->byte_count
= 0;
3570 blk_mq_start_request(rq
);
3571 mtip_issue_non_ncq_command(dd
->port
, rq
->tag
);
3575 static blk_status_t
mtip_queue_rq(struct blk_mq_hw_ctx
*hctx
,
3576 const struct blk_mq_queue_data
*bd
)
3578 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3579 struct request
*rq
= bd
->rq
;
3580 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3582 if (blk_rq_is_passthrough(rq
))
3583 return mtip_issue_reserved_cmd(hctx
, rq
);
3585 if (unlikely(mtip_check_unal_depth(hctx
, rq
)))
3586 return BLK_STS_DEV_RESOURCE
;
3588 if (is_se_active(dd
) || is_stopped(dd
, rq
))
3589 return BLK_STS_IOERR
;
3591 blk_mq_start_request(rq
);
3593 if (req_op(rq
) == REQ_OP_DISCARD
)
3594 return mtip_send_trim(dd
, blk_rq_pos(rq
), blk_rq_sectors(rq
));
3595 mtip_hw_submit_io(dd
, rq
, cmd
, hctx
);
3599 static void mtip_free_cmd(struct blk_mq_tag_set
*set
, struct request
*rq
,
3600 unsigned int hctx_idx
)
3602 struct driver_data
*dd
= set
->driver_data
;
3603 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3608 dma_free_coherent(&dd
->pdev
->dev
, CMD_DMA_ALLOC_SZ
, cmd
->command
,
3612 static int mtip_init_cmd(struct blk_mq_tag_set
*set
, struct request
*rq
,
3613 unsigned int hctx_idx
, unsigned int numa_node
)
3615 struct driver_data
*dd
= set
->driver_data
;
3616 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3618 cmd
->command
= dma_alloc_coherent(&dd
->pdev
->dev
, CMD_DMA_ALLOC_SZ
,
3619 &cmd
->command_dma
, GFP_KERNEL
);
3623 memset(cmd
->command
, 0, CMD_DMA_ALLOC_SZ
);
3625 sg_init_table(cmd
->sg
, MTIP_MAX_SG
);
3629 static enum blk_eh_timer_return
mtip_cmd_timeout(struct request
*req
,
3632 struct driver_data
*dd
= req
->q
->queuedata
;
3635 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(req
);
3637 cmd
->status
= BLK_STS_TIMEOUT
;
3638 blk_mq_complete_request(req
);
3642 if (test_bit(req
->tag
, dd
->port
->cmds_to_issue
))
3645 if (test_and_set_bit(MTIP_PF_TO_ACTIVE_BIT
, &dd
->port
->flags
))
3648 wake_up_interruptible(&dd
->port
->svc_wait
);
3650 return BLK_EH_RESET_TIMER
;
3653 static const struct blk_mq_ops mtip_mq_ops
= {
3654 .queue_rq
= mtip_queue_rq
,
3655 .init_request
= mtip_init_cmd
,
3656 .exit_request
= mtip_free_cmd
,
3657 .complete
= mtip_softirq_done_fn
,
3658 .timeout
= mtip_cmd_timeout
,
3662 * Block layer initialization function.
3664 * This function is called once by the PCI layer for each P320
3665 * device that is connected to the system.
3667 * @dd Pointer to the driver data structure.
3670 * 0 on success else an error code.
3672 static int mtip_block_initialize(struct driver_data
*dd
)
3674 int rv
= 0, wait_for_rebuild
= 0;
3676 unsigned int index
= 0;
3677 struct kobject
*kobj
;
3680 goto skip_create_disk
; /* hw init done, before rebuild */
3682 if (mtip_hw_init(dd
)) {
3684 goto protocol_init_error
;
3687 dd
->disk
= alloc_disk_node(MTIP_MAX_MINORS
, dd
->numa_node
);
3688 if (dd
->disk
== NULL
) {
3689 dev_err(&dd
->pdev
->dev
,
3690 "Unable to allocate gendisk structure\n");
3692 goto alloc_disk_error
;
3695 rv
= ida_alloc(&rssd_index_ida
, GFP_KERNEL
);
3700 rv
= rssd_disk_name_format("rssd",
3702 dd
->disk
->disk_name
,
3705 goto disk_index_error
;
3707 dd
->disk
->major
= dd
->major
;
3708 dd
->disk
->first_minor
= index
* MTIP_MAX_MINORS
;
3709 dd
->disk
->minors
= MTIP_MAX_MINORS
;
3710 dd
->disk
->fops
= &mtip_block_ops
;
3711 dd
->disk
->private_data
= dd
;
3714 mtip_hw_debugfs_init(dd
);
3716 memset(&dd
->tags
, 0, sizeof(dd
->tags
));
3717 dd
->tags
.ops
= &mtip_mq_ops
;
3718 dd
->tags
.nr_hw_queues
= 1;
3719 dd
->tags
.queue_depth
= MTIP_MAX_COMMAND_SLOTS
;
3720 dd
->tags
.reserved_tags
= 1;
3721 dd
->tags
.cmd_size
= sizeof(struct mtip_cmd
);
3722 dd
->tags
.numa_node
= dd
->numa_node
;
3723 dd
->tags
.flags
= BLK_MQ_F_SHOULD_MERGE
;
3724 dd
->tags
.driver_data
= dd
;
3725 dd
->tags
.timeout
= MTIP_NCQ_CMD_TIMEOUT_MS
;
3727 rv
= blk_mq_alloc_tag_set(&dd
->tags
);
3729 dev_err(&dd
->pdev
->dev
,
3730 "Unable to allocate request queue\n");
3731 goto block_queue_alloc_tag_error
;
3734 /* Allocate the request queue. */
3735 dd
->queue
= blk_mq_init_queue(&dd
->tags
);
3736 if (IS_ERR(dd
->queue
)) {
3737 dev_err(&dd
->pdev
->dev
,
3738 "Unable to allocate request queue\n");
3740 goto block_queue_alloc_init_error
;
3743 dd
->disk
->queue
= dd
->queue
;
3744 dd
->queue
->queuedata
= dd
;
3747 /* Initialize the protocol layer. */
3748 wait_for_rebuild
= mtip_hw_get_identify(dd
);
3749 if (wait_for_rebuild
< 0) {
3750 dev_err(&dd
->pdev
->dev
,
3751 "Protocol layer initialization failed\n");
3753 goto init_hw_cmds_error
;
3757 * if rebuild pending, start the service thread, and delay the block
3758 * queue creation and device_add_disk()
3760 if (wait_for_rebuild
== MTIP_FTL_REBUILD_MAGIC
)
3761 goto start_service_thread
;
3763 /* Set device limits. */
3764 blk_queue_flag_set(QUEUE_FLAG_NONROT
, dd
->queue
);
3765 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM
, dd
->queue
);
3766 blk_queue_max_segments(dd
->queue
, MTIP_MAX_SG
);
3767 blk_queue_physical_block_size(dd
->queue
, 4096);
3768 blk_queue_max_hw_sectors(dd
->queue
, 0xffff);
3769 blk_queue_max_segment_size(dd
->queue
, 0x400000);
3770 blk_queue_io_min(dd
->queue
, 4096);
3772 /* Signal trim support */
3773 if (dd
->trim_supp
== true) {
3774 blk_queue_flag_set(QUEUE_FLAG_DISCARD
, dd
->queue
);
3775 dd
->queue
->limits
.discard_granularity
= 4096;
3776 blk_queue_max_discard_sectors(dd
->queue
,
3777 MTIP_MAX_TRIM_ENTRY_LEN
* MTIP_MAX_TRIM_ENTRIES
);
3780 /* Set the capacity of the device in 512 byte sectors. */
3781 if (!(mtip_hw_get_capacity(dd
, &capacity
))) {
3782 dev_warn(&dd
->pdev
->dev
,
3783 "Could not read drive capacity\n");
3785 goto read_capacity_error
;
3787 set_capacity(dd
->disk
, capacity
);
3789 /* Enable the block device and add it to /dev */
3790 device_add_disk(&dd
->pdev
->dev
, dd
->disk
, NULL
);
3792 dd
->bdev
= bdget_disk(dd
->disk
, 0);
3794 * Now that the disk is active, initialize any sysfs attributes
3795 * managed by the protocol layer.
3797 kobj
= kobject_get(&disk_to_dev(dd
->disk
)->kobj
);
3799 mtip_hw_sysfs_init(dd
, kobj
);
3803 if (dd
->mtip_svc_handler
) {
3804 set_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
);
3805 return rv
; /* service thread created for handling rebuild */
3808 start_service_thread
:
3809 dd
->mtip_svc_handler
= kthread_create_on_node(mtip_service_thread
,
3811 "mtip_svc_thd_%02d", index
);
3813 if (IS_ERR(dd
->mtip_svc_handler
)) {
3814 dev_err(&dd
->pdev
->dev
, "service thread failed to start\n");
3815 dd
->mtip_svc_handler
= NULL
;
3817 goto kthread_run_error
;
3819 wake_up_process(dd
->mtip_svc_handler
);
3820 if (wait_for_rebuild
== MTIP_FTL_REBUILD_MAGIC
)
3821 rv
= wait_for_rebuild
;
3829 /* Delete our gendisk. This also removes the device from /dev */
3830 del_gendisk(dd
->disk
);
3832 read_capacity_error
:
3834 blk_cleanup_queue(dd
->queue
);
3835 block_queue_alloc_init_error
:
3836 blk_mq_free_tag_set(&dd
->tags
);
3837 block_queue_alloc_tag_error
:
3838 mtip_hw_debugfs_exit(dd
);
3840 ida_free(&rssd_index_ida
, index
);
3846 mtip_hw_exit(dd
); /* De-initialize the protocol layer. */
3848 protocol_init_error
:
3852 static bool mtip_no_dev_cleanup(struct request
*rq
, void *data
, bool reserv
)
3854 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3856 cmd
->status
= BLK_STS_IOERR
;
3857 blk_mq_complete_request(rq
);
3862 * Block layer deinitialization function.
3864 * Called by the PCI layer as each P320 device is removed.
3866 * @dd Pointer to the driver data structure.
3871 static int mtip_block_remove(struct driver_data
*dd
)
3873 struct kobject
*kobj
;
3875 mtip_hw_debugfs_exit(dd
);
3877 if (dd
->mtip_svc_handler
) {
3878 set_bit(MTIP_PF_SVC_THD_STOP_BIT
, &dd
->port
->flags
);
3879 wake_up_interruptible(&dd
->port
->svc_wait
);
3880 kthread_stop(dd
->mtip_svc_handler
);
3883 /* Clean up the sysfs attributes, if created */
3884 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
)) {
3885 kobj
= kobject_get(&disk_to_dev(dd
->disk
)->kobj
);
3887 mtip_hw_sysfs_exit(dd
, kobj
);
3894 * Explicitly wait here for IOs to quiesce,
3895 * as mtip_standby_drive usually won't wait for IOs.
3897 if (!mtip_quiesce_io(dd
->port
, MTIP_QUIESCE_IO_TIMEOUT_MS
))
3898 mtip_standby_drive(dd
);
3901 dev_info(&dd
->pdev
->dev
, "device %s surprise removal\n",
3902 dd
->disk
->disk_name
);
3904 blk_freeze_queue_start(dd
->queue
);
3905 blk_mq_quiesce_queue(dd
->queue
);
3906 blk_mq_tagset_busy_iter(&dd
->tags
, mtip_no_dev_cleanup
, dd
);
3907 blk_mq_unquiesce_queue(dd
->queue
);
3910 * Delete our gendisk structure. This also removes the device
3918 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
))
3919 del_gendisk(dd
->disk
);
3920 if (dd
->disk
->queue
) {
3921 blk_cleanup_queue(dd
->queue
);
3922 blk_mq_free_tag_set(&dd
->tags
);
3929 ida_free(&rssd_index_ida
, dd
->index
);
3931 /* De-initialize the protocol layer. */
3938 * Function called by the PCI layer when just before the
3939 * machine shuts down.
3941 * If a protocol layer shutdown function is present it will be called
3944 * @dd Pointer to the driver data structure.
3949 static int mtip_block_shutdown(struct driver_data
*dd
)
3951 mtip_hw_shutdown(dd
);
3953 /* Delete our gendisk structure, and cleanup the blk queue. */
3955 dev_info(&dd
->pdev
->dev
,
3956 "Shutting down %s ...\n", dd
->disk
->disk_name
);
3958 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
))
3959 del_gendisk(dd
->disk
);
3960 if (dd
->disk
->queue
) {
3961 blk_cleanup_queue(dd
->queue
);
3962 blk_mq_free_tag_set(&dd
->tags
);
3969 ida_free(&rssd_index_ida
, dd
->index
);
3973 static int mtip_block_suspend(struct driver_data
*dd
)
3975 dev_info(&dd
->pdev
->dev
,
3976 "Suspending %s ...\n", dd
->disk
->disk_name
);
3977 mtip_hw_suspend(dd
);
3981 static int mtip_block_resume(struct driver_data
*dd
)
3983 dev_info(&dd
->pdev
->dev
, "Resuming %s ...\n",
3984 dd
->disk
->disk_name
);
3989 static void drop_cpu(int cpu
)
3994 static int get_least_used_cpu_on_node(int node
)
3996 int cpu
, least_used_cpu
, least_cnt
;
3997 const struct cpumask
*node_mask
;
3999 node_mask
= cpumask_of_node(node
);
4000 least_used_cpu
= cpumask_first(node_mask
);
4001 least_cnt
= cpu_use
[least_used_cpu
];
4002 cpu
= least_used_cpu
;
4004 for_each_cpu(cpu
, node_mask
) {
4005 if (cpu_use
[cpu
] < least_cnt
) {
4006 least_used_cpu
= cpu
;
4007 least_cnt
= cpu_use
[cpu
];
4010 cpu_use
[least_used_cpu
]++;
4011 return least_used_cpu
;
4014 /* Helper for selecting a node in round robin mode */
4015 static inline int mtip_get_next_rr_node(void)
4017 static int next_node
= NUMA_NO_NODE
;
4019 if (next_node
== NUMA_NO_NODE
) {
4020 next_node
= first_online_node
;
4024 next_node
= next_online_node(next_node
);
4025 if (next_node
== MAX_NUMNODES
)
4026 next_node
= first_online_node
;
4030 static DEFINE_HANDLER(0);
4031 static DEFINE_HANDLER(1);
4032 static DEFINE_HANDLER(2);
4033 static DEFINE_HANDLER(3);
4034 static DEFINE_HANDLER(4);
4035 static DEFINE_HANDLER(5);
4036 static DEFINE_HANDLER(6);
4037 static DEFINE_HANDLER(7);
4039 static void mtip_disable_link_opts(struct driver_data
*dd
, struct pci_dev
*pdev
)
4042 unsigned short pcie_dev_ctrl
;
4044 pos
= pci_find_capability(pdev
, PCI_CAP_ID_EXP
);
4046 pci_read_config_word(pdev
,
4047 pos
+ PCI_EXP_DEVCTL
,
4049 if (pcie_dev_ctrl
& (1 << 11) ||
4050 pcie_dev_ctrl
& (1 << 4)) {
4051 dev_info(&dd
->pdev
->dev
,
4052 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4053 pdev
->vendor
, pdev
->device
);
4054 pcie_dev_ctrl
&= ~(PCI_EXP_DEVCTL_NOSNOOP_EN
|
4055 PCI_EXP_DEVCTL_RELAX_EN
);
4056 pci_write_config_word(pdev
,
4057 pos
+ PCI_EXP_DEVCTL
,
4063 static void mtip_fix_ero_nosnoop(struct driver_data
*dd
, struct pci_dev
*pdev
)
4066 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4067 * device with device id 0x5aXX
4069 if (pdev
->bus
&& pdev
->bus
->self
) {
4070 if (pdev
->bus
->self
->vendor
== PCI_VENDOR_ID_ATI
&&
4071 ((pdev
->bus
->self
->device
& 0xff00) == 0x5a00)) {
4072 mtip_disable_link_opts(dd
, pdev
->bus
->self
);
4074 /* Check further up the topology */
4075 struct pci_dev
*parent_dev
= pdev
->bus
->self
;
4076 if (parent_dev
->bus
&&
4077 parent_dev
->bus
->parent
&&
4078 parent_dev
->bus
->parent
->self
&&
4079 parent_dev
->bus
->parent
->self
->vendor
==
4080 PCI_VENDOR_ID_ATI
&&
4081 (parent_dev
->bus
->parent
->self
->device
&
4082 0xff00) == 0x5a00) {
4083 mtip_disable_link_opts(dd
,
4084 parent_dev
->bus
->parent
->self
);
4091 * Called for each supported PCI device detected.
4093 * This function allocates the private data structure, enables the
4094 * PCI device and then calls the block layer initialization function.
4097 * 0 on success else an error code.
4099 static int mtip_pci_probe(struct pci_dev
*pdev
,
4100 const struct pci_device_id
*ent
)
4103 struct driver_data
*dd
= NULL
;
4105 const struct cpumask
*node_mask
;
4106 int cpu
, i
= 0, j
= 0;
4107 int my_node
= NUMA_NO_NODE
;
4108 unsigned long flags
;
4110 /* Allocate memory for this devices private data. */
4111 my_node
= pcibus_to_node(pdev
->bus
);
4112 if (my_node
!= NUMA_NO_NODE
) {
4113 if (!node_online(my_node
))
4114 my_node
= mtip_get_next_rr_node();
4116 dev_info(&pdev
->dev
, "Kernel not reporting proximity, choosing a node\n");
4117 my_node
= mtip_get_next_rr_node();
4119 dev_info(&pdev
->dev
, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4120 my_node
, pcibus_to_node(pdev
->bus
), dev_to_node(&pdev
->dev
),
4121 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
4123 dd
= kzalloc_node(sizeof(struct driver_data
), GFP_KERNEL
, my_node
);
4126 "Unable to allocate memory for driver data\n");
4130 /* Attach the private data to this PCI device. */
4131 pci_set_drvdata(pdev
, dd
);
4133 rv
= pcim_enable_device(pdev
);
4135 dev_err(&pdev
->dev
, "Unable to enable device\n");
4139 /* Map BAR5 to memory. */
4140 rv
= pcim_iomap_regions(pdev
, 1 << MTIP_ABAR
, MTIP_DRV_NAME
);
4142 dev_err(&pdev
->dev
, "Unable to map regions\n");
4146 rv
= dma_set_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(64));
4148 dev_warn(&pdev
->dev
, "64-bit DMA enable failed\n");
4152 /* Copy the info we may need later into the private data structure. */
4153 dd
->major
= mtip_major
;
4154 dd
->instance
= instance
;
4156 dd
->numa_node
= my_node
;
4158 INIT_LIST_HEAD(&dd
->online_list
);
4159 INIT_LIST_HEAD(&dd
->remove_list
);
4161 memset(dd
->workq_name
, 0, 32);
4162 snprintf(dd
->workq_name
, 31, "mtipq%d", dd
->instance
);
4164 dd
->isr_workq
= create_workqueue(dd
->workq_name
);
4165 if (!dd
->isr_workq
) {
4166 dev_warn(&pdev
->dev
, "Can't create wq %d\n", dd
->instance
);
4171 memset(cpu_list
, 0, sizeof(cpu_list
));
4173 node_mask
= cpumask_of_node(dd
->numa_node
);
4174 if (!cpumask_empty(node_mask
)) {
4175 for_each_cpu(cpu
, node_mask
)
4177 snprintf(&cpu_list
[j
], 256 - j
, "%d ", cpu
);
4178 j
= strlen(cpu_list
);
4181 dev_info(&pdev
->dev
, "Node %d on package %d has %d cpu(s): %s\n",
4183 topology_physical_package_id(cpumask_first(node_mask
)),
4184 nr_cpus_node(dd
->numa_node
),
4187 dev_dbg(&pdev
->dev
, "mtip32xx: node_mask empty\n");
4189 dd
->isr_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4190 dev_info(&pdev
->dev
, "Initial IRQ binding node:cpu %d:%d\n",
4191 cpu_to_node(dd
->isr_binding
), dd
->isr_binding
);
4193 /* first worker context always runs in ISR */
4194 dd
->work
[0].cpu_binding
= dd
->isr_binding
;
4195 dd
->work
[1].cpu_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4196 dd
->work
[2].cpu_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4197 dd
->work
[3].cpu_binding
= dd
->work
[0].cpu_binding
;
4198 dd
->work
[4].cpu_binding
= dd
->work
[1].cpu_binding
;
4199 dd
->work
[5].cpu_binding
= dd
->work
[2].cpu_binding
;
4200 dd
->work
[6].cpu_binding
= dd
->work
[2].cpu_binding
;
4201 dd
->work
[7].cpu_binding
= dd
->work
[1].cpu_binding
;
4203 /* Log the bindings */
4204 for_each_present_cpu(cpu
) {
4205 memset(cpu_list
, 0, sizeof(cpu_list
));
4206 for (i
= 0, j
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++) {
4207 if (dd
->work
[i
].cpu_binding
== cpu
) {
4208 snprintf(&cpu_list
[j
], 256 - j
, "%d ", i
);
4209 j
= strlen(cpu_list
);
4213 dev_info(&pdev
->dev
, "CPU %d: WQs %s\n", cpu
, cpu_list
);
4216 INIT_WORK(&dd
->work
[0].work
, mtip_workq_sdbf0
);
4217 INIT_WORK(&dd
->work
[1].work
, mtip_workq_sdbf1
);
4218 INIT_WORK(&dd
->work
[2].work
, mtip_workq_sdbf2
);
4219 INIT_WORK(&dd
->work
[3].work
, mtip_workq_sdbf3
);
4220 INIT_WORK(&dd
->work
[4].work
, mtip_workq_sdbf4
);
4221 INIT_WORK(&dd
->work
[5].work
, mtip_workq_sdbf5
);
4222 INIT_WORK(&dd
->work
[6].work
, mtip_workq_sdbf6
);
4223 INIT_WORK(&dd
->work
[7].work
, mtip_workq_sdbf7
);
4225 pci_set_master(pdev
);
4226 rv
= pci_enable_msi(pdev
);
4228 dev_warn(&pdev
->dev
,
4229 "Unable to enable MSI interrupt.\n");
4230 goto msi_initialize_err
;
4233 mtip_fix_ero_nosnoop(dd
, pdev
);
4235 /* Initialize the block layer. */
4236 rv
= mtip_block_initialize(dd
);
4239 "Unable to initialize block layer\n");
4240 goto block_initialize_err
;
4244 * Increment the instance count so that each device has a unique
4248 if (rv
!= MTIP_FTL_REBUILD_MAGIC
)
4249 set_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
);
4251 rv
= 0; /* device in rebuild state, return 0 from probe */
4253 /* Add to online list even if in ftl rebuild */
4254 spin_lock_irqsave(&dev_lock
, flags
);
4255 list_add(&dd
->online_list
, &online_list
);
4256 spin_unlock_irqrestore(&dev_lock
, flags
);
4260 block_initialize_err
:
4261 pci_disable_msi(pdev
);
4264 if (dd
->isr_workq
) {
4265 flush_workqueue(dd
->isr_workq
);
4266 destroy_workqueue(dd
->isr_workq
);
4267 drop_cpu(dd
->work
[0].cpu_binding
);
4268 drop_cpu(dd
->work
[1].cpu_binding
);
4269 drop_cpu(dd
->work
[2].cpu_binding
);
4272 pcim_iounmap_regions(pdev
, 1 << MTIP_ABAR
);
4276 pci_set_drvdata(pdev
, NULL
);
4283 * Called for each probed device when the device is removed or the
4284 * driver is unloaded.
4289 static void mtip_pci_remove(struct pci_dev
*pdev
)
4291 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4292 unsigned long flags
, to
;
4294 set_bit(MTIP_DDF_REMOVAL_BIT
, &dd
->dd_flag
);
4296 spin_lock_irqsave(&dev_lock
, flags
);
4297 list_del_init(&dd
->online_list
);
4298 list_add(&dd
->remove_list
, &removing_list
);
4299 spin_unlock_irqrestore(&dev_lock
, flags
);
4301 mtip_check_surprise_removal(pdev
);
4302 synchronize_irq(dd
->pdev
->irq
);
4304 /* Spin until workers are done */
4305 to
= jiffies
+ msecs_to_jiffies(4000);
4308 } while (atomic_read(&dd
->irq_workers_active
) != 0 &&
4309 time_before(jiffies
, to
));
4312 fsync_bdev(dd
->bdev
);
4314 if (atomic_read(&dd
->irq_workers_active
) != 0) {
4315 dev_warn(&dd
->pdev
->dev
,
4316 "Completion workers still active!\n");
4319 blk_set_queue_dying(dd
->queue
);
4320 set_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
);
4322 /* Clean up the block layer. */
4323 mtip_block_remove(dd
);
4325 if (dd
->isr_workq
) {
4326 flush_workqueue(dd
->isr_workq
);
4327 destroy_workqueue(dd
->isr_workq
);
4328 drop_cpu(dd
->work
[0].cpu_binding
);
4329 drop_cpu(dd
->work
[1].cpu_binding
);
4330 drop_cpu(dd
->work
[2].cpu_binding
);
4333 pci_disable_msi(pdev
);
4335 spin_lock_irqsave(&dev_lock
, flags
);
4336 list_del_init(&dd
->remove_list
);
4337 spin_unlock_irqrestore(&dev_lock
, flags
);
4341 pcim_iounmap_regions(pdev
, 1 << MTIP_ABAR
);
4342 pci_set_drvdata(pdev
, NULL
);
4346 * Called for each probed device when the device is suspended.
4352 static int mtip_pci_suspend(struct pci_dev
*pdev
, pm_message_t mesg
)
4355 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4359 "Driver private datastructure is NULL\n");
4363 set_bit(MTIP_DDF_RESUME_BIT
, &dd
->dd_flag
);
4365 /* Disable ports & interrupts then send standby immediate */
4366 rv
= mtip_block_suspend(dd
);
4369 "Failed to suspend controller\n");
4374 * Save the pci config space to pdev structure &
4375 * disable the device
4377 pci_save_state(pdev
);
4378 pci_disable_device(pdev
);
4380 /* Move to Low power state*/
4381 pci_set_power_state(pdev
, PCI_D3hot
);
4387 * Called for each probed device when the device is resumed.
4393 static int mtip_pci_resume(struct pci_dev
*pdev
)
4396 struct driver_data
*dd
;
4398 dd
= pci_get_drvdata(pdev
);
4401 "Driver private datastructure is NULL\n");
4405 /* Move the device to active State */
4406 pci_set_power_state(pdev
, PCI_D0
);
4408 /* Restore PCI configuration space */
4409 pci_restore_state(pdev
);
4411 /* Enable the PCI device*/
4412 rv
= pcim_enable_device(pdev
);
4415 "Failed to enable card during resume\n");
4418 pci_set_master(pdev
);
4421 * Calls hbaReset, initPort, & startPort function
4422 * then enables interrupts
4424 rv
= mtip_block_resume(dd
);
4426 dev_err(&pdev
->dev
, "Unable to resume\n");
4429 clear_bit(MTIP_DDF_RESUME_BIT
, &dd
->dd_flag
);
4440 static void mtip_pci_shutdown(struct pci_dev
*pdev
)
4442 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4444 mtip_block_shutdown(dd
);
4447 /* Table of device ids supported by this driver. */
4448 static const struct pci_device_id mtip_pci_tbl
[] = {
4449 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320H_DEVICE_ID
) },
4450 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320M_DEVICE_ID
) },
4451 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320S_DEVICE_ID
) },
4452 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P325M_DEVICE_ID
) },
4453 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P420H_DEVICE_ID
) },
4454 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P420M_DEVICE_ID
) },
4455 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P425M_DEVICE_ID
) },
4459 /* Structure that describes the PCI driver functions. */
4460 static struct pci_driver mtip_pci_driver
= {
4461 .name
= MTIP_DRV_NAME
,
4462 .id_table
= mtip_pci_tbl
,
4463 .probe
= mtip_pci_probe
,
4464 .remove
= mtip_pci_remove
,
4465 .suspend
= mtip_pci_suspend
,
4466 .resume
= mtip_pci_resume
,
4467 .shutdown
= mtip_pci_shutdown
,
4470 MODULE_DEVICE_TABLE(pci
, mtip_pci_tbl
);
4473 * Module initialization function.
4475 * Called once when the module is loaded. This function allocates a major
4476 * block device number to the Cyclone devices and registers the PCI layer
4480 * 0 on success else error code.
4482 static int __init
mtip_init(void)
4486 pr_info(MTIP_DRV_NAME
" Version " MTIP_DRV_VERSION
"\n");
4488 spin_lock_init(&dev_lock
);
4490 INIT_LIST_HEAD(&online_list
);
4491 INIT_LIST_HEAD(&removing_list
);
4493 /* Allocate a major block device number to use with this driver. */
4494 error
= register_blkdev(0, MTIP_DRV_NAME
);
4496 pr_err("Unable to register block device (%d)\n",
4502 dfs_parent
= debugfs_create_dir("rssd", NULL
);
4503 if (IS_ERR_OR_NULL(dfs_parent
)) {
4504 pr_warn("Error creating debugfs parent\n");
4508 dfs_device_status
= debugfs_create_file("device_status",
4509 0444, dfs_parent
, NULL
,
4510 &mtip_device_status_fops
);
4511 if (IS_ERR_OR_NULL(dfs_device_status
)) {
4512 pr_err("Error creating device_status node\n");
4513 dfs_device_status
= NULL
;
4517 /* Register our PCI operations. */
4518 error
= pci_register_driver(&mtip_pci_driver
);
4520 debugfs_remove(dfs_parent
);
4521 unregister_blkdev(mtip_major
, MTIP_DRV_NAME
);
4528 * Module de-initialization function.
4530 * Called once when the module is unloaded. This function deallocates
4531 * the major block device number allocated by mtip_init() and
4532 * unregisters the PCI layer of the driver.
4537 static void __exit
mtip_exit(void)
4539 /* Release the allocated major block device number. */
4540 unregister_blkdev(mtip_major
, MTIP_DRV_NAME
);
4542 /* Unregister the PCI driver. */
4543 pci_unregister_driver(&mtip_pci_driver
);
4545 debugfs_remove_recursive(dfs_parent
);
4548 MODULE_AUTHOR("Micron Technology, Inc");
4549 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4550 MODULE_LICENSE("GPL");
4551 MODULE_VERSION(MTIP_DRV_VERSION
);
4553 module_init(mtip_init
);
4554 module_exit(mtip_exit
);