2 * Driver for the Micron P320 SSD
3 * Copyright (C) 2011 Micron Technology, Inc.
5 * Portions of this code were derived from works subjected to the
7 * Copyright (C) 2009 Integrated Device Technology, Inc.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
21 #include <linux/pci.h>
22 #include <linux/interrupt.h>
23 #include <linux/ata.h>
24 #include <linux/delay.h>
25 #include <linux/hdreg.h>
26 #include <linux/uaccess.h>
27 #include <linux/random.h>
28 #include <linux/smp.h>
29 #include <linux/compat.h>
31 #include <linux/module.h>
32 #include <linux/genhd.h>
33 #include <linux/blkdev.h>
34 #include <linux/blk-mq.h>
35 #include <linux/bio.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/idr.h>
38 #include <linux/kthread.h>
39 #include <../drivers/ata/ahci.h>
40 #include <linux/export.h>
41 #include <linux/debugfs.h>
42 #include <linux/prefetch.h>
45 #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
47 /* DMA region containing RX Fis, Identify, RLE10, and SMART buffers */
48 #define AHCI_RX_FIS_SZ 0x100
49 #define AHCI_RX_FIS_OFFSET 0x0
50 #define AHCI_IDFY_SZ ATA_SECT_SIZE
51 #define AHCI_IDFY_OFFSET 0x400
52 #define AHCI_SECTBUF_SZ ATA_SECT_SIZE
53 #define AHCI_SECTBUF_OFFSET 0x800
54 #define AHCI_SMARTBUF_SZ ATA_SECT_SIZE
55 #define AHCI_SMARTBUF_OFFSET 0xC00
56 /* 0x100 + 0x200 + 0x200 + 0x200 is smaller than 4k but we pad it out */
57 #define BLOCK_DMA_ALLOC_SZ 4096
59 /* DMA region containing command table (should be 8192 bytes) */
60 #define AHCI_CMD_SLOT_SZ sizeof(struct mtip_cmd_hdr)
61 #define AHCI_CMD_TBL_SZ (MTIP_MAX_COMMAND_SLOTS * AHCI_CMD_SLOT_SZ)
62 #define AHCI_CMD_TBL_OFFSET 0x0
64 /* DMA region per command (contains header and SGL) */
65 #define AHCI_CMD_TBL_HDR_SZ 0x80
66 #define AHCI_CMD_TBL_HDR_OFFSET 0x0
67 #define AHCI_CMD_TBL_SGL_SZ (MTIP_MAX_SG * sizeof(struct mtip_cmd_sg))
68 #define AHCI_CMD_TBL_SGL_OFFSET AHCI_CMD_TBL_HDR_SZ
69 #define CMD_DMA_ALLOC_SZ (AHCI_CMD_TBL_SGL_SZ + AHCI_CMD_TBL_HDR_SZ)
72 #define HOST_CAP_NZDMA (1 << 19)
73 #define HOST_HSORG 0xFC
74 #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
75 #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
76 #define HSORG_HWREV 0xFF00
77 #define HSORG_STYLE 0x8
78 #define HSORG_SLOTGROUPS 0x7
80 #define PORT_COMMAND_ISSUE 0x38
81 #define PORT_SDBV 0x7C
83 #define PORT_OFFSET 0x100
84 #define PORT_MEM_SIZE 0x80
86 #define PORT_IRQ_ERR \
87 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
88 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
89 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
91 #define PORT_IRQ_LEGACY \
92 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
93 #define PORT_IRQ_HANDLED \
94 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
95 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
96 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
97 #define DEF_PORT_IRQ \
98 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
100 /* product numbers */
101 #define MTIP_PRODUCT_UNKNOWN 0x00
102 #define MTIP_PRODUCT_ASICFPGA 0x11
104 /* Device instance number, incremented each time a device is probed. */
107 static struct list_head online_list
;
108 static struct list_head removing_list
;
109 static spinlock_t dev_lock
;
112 * Global variable used to hold the major block device number
113 * allocated in mtip_init().
115 static int mtip_major
;
116 static struct dentry
*dfs_parent
;
117 static struct dentry
*dfs_device_status
;
119 static u32 cpu_use
[NR_CPUS
];
121 static DEFINE_SPINLOCK(rssd_index_lock
);
122 static DEFINE_IDA(rssd_index_ida
);
124 static int mtip_block_initialize(struct driver_data
*dd
);
127 struct mtip_compat_ide_task_request_s
{
130 ide_reg_valid_t out_flags
;
131 ide_reg_valid_t in_flags
;
134 compat_ulong_t out_size
;
135 compat_ulong_t in_size
;
140 * This function check_for_surprise_removal is called
141 * while card is removed from the system and it will
142 * read the vendor id from the configration space
144 * @pdev Pointer to the pci_dev structure.
147 * true if device removed, else false
149 static bool mtip_check_surprise_removal(struct pci_dev
*pdev
)
152 struct driver_data
*dd
= pci_get_drvdata(pdev
);
157 /* Read the vendorID from the configuration space */
158 pci_read_config_word(pdev
, 0x00, &vendor_id
);
159 if (vendor_id
== 0xFFFF) {
162 set_bit(QUEUE_FLAG_DEAD
, &dd
->queue
->queue_flags
);
164 dev_warn(&dd
->pdev
->dev
,
165 "%s: dd->queue is NULL\n", __func__
);
166 return true; /* device removed */
169 return false; /* device present */
172 static struct mtip_cmd
*mtip_get_int_command(struct driver_data
*dd
)
176 rq
= blk_mq_alloc_request(dd
->queue
, 0, BLK_MQ_REQ_RESERVED
);
177 return blk_mq_rq_to_pdu(rq
);
180 static void mtip_put_int_command(struct driver_data
*dd
, struct mtip_cmd
*cmd
)
182 blk_put_request(blk_mq_rq_from_pdu(cmd
));
186 * Once we add support for one hctx per mtip group, this will change a bit
188 static struct request
*mtip_rq_from_tag(struct driver_data
*dd
,
191 struct blk_mq_hw_ctx
*hctx
= dd
->queue
->queue_hw_ctx
[0];
193 return blk_mq_tag_to_rq(hctx
->tags
, tag
);
196 static struct mtip_cmd
*mtip_cmd_from_tag(struct driver_data
*dd
,
199 struct request
*rq
= mtip_rq_from_tag(dd
, tag
);
201 return blk_mq_rq_to_pdu(rq
);
205 * IO completion function.
207 * This completion function is called by the driver ISR when a
208 * command that was issued by the kernel completes. It first calls the
209 * asynchronous completion function which normally calls back into the block
210 * layer passing the asynchronous callback data, then unmaps the
211 * scatter list associated with the completed command, and finally
212 * clears the allocated bit associated with the completed command.
214 * @port Pointer to the port data structure.
215 * @tag Tag of the command.
216 * @data Pointer to driver_data.
217 * @status Completion status.
222 static void mtip_async_complete(struct mtip_port
*port
,
223 int tag
, struct mtip_cmd
*cmd
, int status
)
225 struct driver_data
*dd
= port
->dd
;
228 if (unlikely(!dd
) || unlikely(!port
))
231 if (unlikely(status
== PORT_IRQ_TF_ERR
)) {
232 dev_warn(&port
->dd
->pdev
->dev
,
233 "Command tag %d failed due to TFE\n", tag
);
236 /* Unmap the DMA scatter list entries */
237 dma_unmap_sg(&dd
->pdev
->dev
, cmd
->sg
, cmd
->scatter_ents
, cmd
->direction
);
239 rq
= mtip_rq_from_tag(dd
, tag
);
241 if (unlikely(cmd
->unaligned
))
242 up(&port
->cmd_slot_unal
);
244 blk_mq_end_request(rq
, status
? -EIO
: 0);
248 * Reset the HBA (without sleeping)
250 * @dd Pointer to the driver data structure.
253 * 0 The reset was successful.
254 * -1 The HBA Reset bit did not clear.
256 static int mtip_hba_reset(struct driver_data
*dd
)
258 unsigned long timeout
;
260 /* Set the reset bit */
261 writel(HOST_RESET
, dd
->mmio
+ HOST_CTL
);
264 readl(dd
->mmio
+ HOST_CTL
);
267 * Spin for up to 10 seconds waiting for reset acknowledgement. Spec
268 * is 1 sec but in LUN failure conditions, up to 10 secs are required
270 timeout
= jiffies
+ msecs_to_jiffies(10000);
273 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))
276 } while ((readl(dd
->mmio
+ HOST_CTL
) & HOST_RESET
)
277 && time_before(jiffies
, timeout
));
279 if (readl(dd
->mmio
+ HOST_CTL
) & HOST_RESET
)
286 * Issue a command to the hardware.
288 * Set the appropriate bit in the s_active and Command Issue hardware
289 * registers, causing hardware command processing to begin.
291 * @port Pointer to the port structure.
292 * @tag The tag of the command to be issued.
297 static inline void mtip_issue_ncq_command(struct mtip_port
*port
, int tag
)
299 int group
= tag
>> 5;
301 /* guard SACT and CI registers */
302 spin_lock(&port
->cmd_issue_lock
[group
]);
303 writel((1 << MTIP_TAG_BIT(tag
)),
304 port
->s_active
[MTIP_TAG_INDEX(tag
)]);
305 writel((1 << MTIP_TAG_BIT(tag
)),
306 port
->cmd_issue
[MTIP_TAG_INDEX(tag
)]);
307 spin_unlock(&port
->cmd_issue_lock
[group
]);
311 * Enable/disable the reception of FIS
313 * @port Pointer to the port data structure
314 * @enable 1 to enable, 0 to disable
317 * Previous state: 1 enabled, 0 disabled
319 static int mtip_enable_fis(struct mtip_port
*port
, int enable
)
323 /* enable FIS reception */
324 tmp
= readl(port
->mmio
+ PORT_CMD
);
326 writel(tmp
| PORT_CMD_FIS_RX
, port
->mmio
+ PORT_CMD
);
328 writel(tmp
& ~PORT_CMD_FIS_RX
, port
->mmio
+ PORT_CMD
);
331 readl(port
->mmio
+ PORT_CMD
);
333 return (((tmp
& PORT_CMD_FIS_RX
) == PORT_CMD_FIS_RX
));
337 * Enable/disable the DMA engine
339 * @port Pointer to the port data structure
340 * @enable 1 to enable, 0 to disable
343 * Previous state: 1 enabled, 0 disabled.
345 static int mtip_enable_engine(struct mtip_port
*port
, int enable
)
349 /* enable FIS reception */
350 tmp
= readl(port
->mmio
+ PORT_CMD
);
352 writel(tmp
| PORT_CMD_START
, port
->mmio
+ PORT_CMD
);
354 writel(tmp
& ~PORT_CMD_START
, port
->mmio
+ PORT_CMD
);
356 readl(port
->mmio
+ PORT_CMD
);
357 return (((tmp
& PORT_CMD_START
) == PORT_CMD_START
));
361 * Enables the port DMA engine and FIS reception.
366 static inline void mtip_start_port(struct mtip_port
*port
)
368 /* Enable FIS reception */
369 mtip_enable_fis(port
, 1);
371 /* Enable the DMA engine */
372 mtip_enable_engine(port
, 1);
376 * Deinitialize a port by disabling port interrupts, the DMA engine,
379 * @port Pointer to the port structure
384 static inline void mtip_deinit_port(struct mtip_port
*port
)
386 /* Disable interrupts on this port */
387 writel(0, port
->mmio
+ PORT_IRQ_MASK
);
389 /* Disable the DMA engine */
390 mtip_enable_engine(port
, 0);
392 /* Disable FIS reception */
393 mtip_enable_fis(port
, 0);
399 * This function deinitializes the port by calling mtip_deinit_port() and
400 * then initializes it by setting the command header and RX FIS addresses,
401 * clearing the SError register and any pending port interrupts before
402 * re-enabling the default set of port interrupts.
404 * @port Pointer to the port structure.
409 static void mtip_init_port(struct mtip_port
*port
)
412 mtip_deinit_port(port
);
414 /* Program the command list base and FIS base addresses */
415 if (readl(port
->dd
->mmio
+ HOST_CAP
) & HOST_CAP_64
) {
416 writel((port
->command_list_dma
>> 16) >> 16,
417 port
->mmio
+ PORT_LST_ADDR_HI
);
418 writel((port
->rxfis_dma
>> 16) >> 16,
419 port
->mmio
+ PORT_FIS_ADDR_HI
);
422 writel(port
->command_list_dma
& 0xFFFFFFFF,
423 port
->mmio
+ PORT_LST_ADDR
);
424 writel(port
->rxfis_dma
& 0xFFFFFFFF, port
->mmio
+ PORT_FIS_ADDR
);
427 writel(readl(port
->mmio
+ PORT_SCR_ERR
), port
->mmio
+ PORT_SCR_ERR
);
429 /* reset the completed registers.*/
430 for (i
= 0; i
< port
->dd
->slot_groups
; i
++)
431 writel(0xFFFFFFFF, port
->completed
[i
]);
433 /* Clear any pending interrupts for this port */
434 writel(readl(port
->mmio
+ PORT_IRQ_STAT
), port
->mmio
+ PORT_IRQ_STAT
);
436 /* Clear any pending interrupts on the HBA. */
437 writel(readl(port
->dd
->mmio
+ HOST_IRQ_STAT
),
438 port
->dd
->mmio
+ HOST_IRQ_STAT
);
440 /* Enable port interrupts */
441 writel(DEF_PORT_IRQ
, port
->mmio
+ PORT_IRQ_MASK
);
447 * @port Pointer to the port data structure.
452 static void mtip_restart_port(struct mtip_port
*port
)
454 unsigned long timeout
;
456 /* Disable the DMA engine */
457 mtip_enable_engine(port
, 0);
459 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
460 timeout
= jiffies
+ msecs_to_jiffies(500);
461 while ((readl(port
->mmio
+ PORT_CMD
) & PORT_CMD_LIST_ON
)
462 && time_before(jiffies
, timeout
))
465 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
469 * Chip quirk: escalate to hba reset if
470 * PxCMD.CR not clear after 500 ms
472 if (readl(port
->mmio
+ PORT_CMD
) & PORT_CMD_LIST_ON
) {
473 dev_warn(&port
->dd
->pdev
->dev
,
474 "PxCMD.CR not clear, escalating reset\n");
476 if (mtip_hba_reset(port
->dd
))
477 dev_err(&port
->dd
->pdev
->dev
,
478 "HBA reset escalation failed.\n");
480 /* 30 ms delay before com reset to quiesce chip */
484 dev_warn(&port
->dd
->pdev
->dev
, "Issuing COM reset\n");
487 writel(readl(port
->mmio
+ PORT_SCR_CTL
) |
488 1, port
->mmio
+ PORT_SCR_CTL
);
489 readl(port
->mmio
+ PORT_SCR_CTL
);
491 /* Wait 1 ms to quiesce chip function */
492 timeout
= jiffies
+ msecs_to_jiffies(1);
493 while (time_before(jiffies
, timeout
))
496 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
499 /* Clear PxSCTL.DET */
500 writel(readl(port
->mmio
+ PORT_SCR_CTL
) & ~1,
501 port
->mmio
+ PORT_SCR_CTL
);
502 readl(port
->mmio
+ PORT_SCR_CTL
);
504 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
505 timeout
= jiffies
+ msecs_to_jiffies(500);
506 while (((readl(port
->mmio
+ PORT_SCR_STAT
) & 0x01) == 0)
507 && time_before(jiffies
, timeout
))
510 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
513 if ((readl(port
->mmio
+ PORT_SCR_STAT
) & 0x01) == 0)
514 dev_warn(&port
->dd
->pdev
->dev
,
515 "COM reset failed\n");
517 mtip_init_port(port
);
518 mtip_start_port(port
);
522 static int mtip_device_reset(struct driver_data
*dd
)
526 if (mtip_check_surprise_removal(dd
->pdev
))
529 if (mtip_hba_reset(dd
) < 0)
533 mtip_init_port(dd
->port
);
534 mtip_start_port(dd
->port
);
536 /* Enable interrupts on the HBA. */
537 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
538 dd
->mmio
+ HOST_CTL
);
543 * Helper function for tag logging
545 static void print_tags(struct driver_data
*dd
,
547 unsigned long *tagbits
,
550 unsigned char tagmap
[128];
551 int group
, tagmap_len
= 0;
553 memset(tagmap
, 0, sizeof(tagmap
));
554 for (group
= SLOTBITS_IN_LONGS
; group
> 0; group
--)
555 tagmap_len
+= sprintf(tagmap
+ tagmap_len
, "%016lX ",
557 dev_warn(&dd
->pdev
->dev
,
558 "%d command(s) %s: tagmap [%s]", cnt
, msg
, tagmap
);
562 * Internal command completion callback function.
564 * This function is normally called by the driver ISR when an internal
565 * command completed. This function signals the command completion by
566 * calling complete().
568 * @port Pointer to the port data structure.
569 * @tag Tag of the command that has completed.
570 * @data Pointer to a completion structure.
571 * @status Completion status.
576 static void mtip_completion(struct mtip_port
*port
,
577 int tag
, struct mtip_cmd
*command
, int status
)
579 struct completion
*waiting
= command
->comp_data
;
580 if (unlikely(status
== PORT_IRQ_TF_ERR
))
581 dev_warn(&port
->dd
->pdev
->dev
,
582 "Internal command %d completed with TFE\n", tag
);
587 static void mtip_null_completion(struct mtip_port
*port
,
588 int tag
, struct mtip_cmd
*command
, int status
)
592 static int mtip_read_log_page(struct mtip_port
*port
, u8 page
, u16
*buffer
,
593 dma_addr_t buffer_dma
, unsigned int sectors
);
594 static int mtip_get_smart_attr(struct mtip_port
*port
, unsigned int id
,
595 struct smart_attr
*attrib
);
599 * @dd Pointer to the DRIVER_DATA structure.
604 static void mtip_handle_tfe(struct driver_data
*dd
)
606 int group
, tag
, bit
, reissue
, rv
;
607 struct mtip_port
*port
;
608 struct mtip_cmd
*cmd
;
610 struct host_to_dev_fis
*fis
;
611 unsigned long tagaccum
[SLOTBITS_IN_LONGS
];
612 unsigned int cmd_cnt
= 0;
614 char *fail_reason
= NULL
;
615 int fail_all_ncq_write
= 0, fail_all_ncq_cmds
= 0;
617 dev_warn(&dd
->pdev
->dev
, "Taskfile error\n");
621 if (test_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
)) {
622 cmd
= mtip_cmd_from_tag(dd
, MTIP_TAG_INTERNAL
);
623 dbg_printk(MTIP_DRV_NAME
" TFE for the internal command\n");
625 if (cmd
->comp_data
&& cmd
->comp_func
) {
626 cmd
->comp_func(port
, MTIP_TAG_INTERNAL
,
627 cmd
, PORT_IRQ_TF_ERR
);
632 /* clear the tag accumulator */
633 memset(tagaccum
, 0, SLOTBITS_IN_LONGS
* sizeof(long));
635 /* Loop through all the groups */
636 for (group
= 0; group
< dd
->slot_groups
; group
++) {
637 completed
= readl(port
->completed
[group
]);
639 dev_warn(&dd
->pdev
->dev
, "g=%u, comp=%x\n", group
, completed
);
641 /* clear completed status register in the hardware.*/
642 writel(completed
, port
->completed
[group
]);
644 /* Process successfully completed commands */
645 for (bit
= 0; bit
< 32 && completed
; bit
++) {
646 if (!(completed
& (1<<bit
)))
648 tag
= (group
<< 5) + bit
;
650 /* Skip the internal command slot */
651 if (tag
== MTIP_TAG_INTERNAL
)
654 cmd
= mtip_cmd_from_tag(dd
, tag
);
655 if (likely(cmd
->comp_func
)) {
656 set_bit(tag
, tagaccum
);
658 cmd
->comp_func(port
, tag
, cmd
, 0);
660 dev_err(&port
->dd
->pdev
->dev
,
661 "Missing completion func for tag %d",
663 if (mtip_check_surprise_removal(dd
->pdev
)) {
664 /* don't proceed further */
671 print_tags(dd
, "completed (TFE)", tagaccum
, cmd_cnt
);
673 /* Restart the port */
675 mtip_restart_port(port
);
677 /* Trying to determine the cause of the error */
678 rv
= mtip_read_log_page(dd
->port
, ATA_LOG_SATA_NCQ
,
680 dd
->port
->log_buf_dma
, 1);
682 dev_warn(&dd
->pdev
->dev
,
683 "Error in READ LOG EXT (10h) command\n");
684 /* non-critical error, don't fail the load */
686 buf
= (unsigned char *)dd
->port
->log_buf
;
687 if (buf
[259] & 0x1) {
688 dev_info(&dd
->pdev
->dev
,
689 "Write protect bit is set.\n");
690 set_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
);
691 fail_all_ncq_write
= 1;
692 fail_reason
= "write protect";
694 if (buf
[288] == 0xF7) {
695 dev_info(&dd
->pdev
->dev
,
696 "Exceeded Tmax, drive in thermal shutdown.\n");
697 set_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
);
698 fail_all_ncq_cmds
= 1;
699 fail_reason
= "thermal shutdown";
701 if (buf
[288] == 0xBF) {
702 set_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
);
703 dev_info(&dd
->pdev
->dev
,
704 "Drive indicates rebuild has failed. Secure erase required.\n");
705 fail_all_ncq_cmds
= 1;
706 fail_reason
= "rebuild failed";
710 /* clear the tag accumulator */
711 memset(tagaccum
, 0, SLOTBITS_IN_LONGS
* sizeof(long));
713 /* Loop through all the groups */
714 for (group
= 0; group
< dd
->slot_groups
; group
++) {
715 for (bit
= 0; bit
< 32; bit
++) {
717 tag
= (group
<< 5) + bit
;
718 cmd
= mtip_cmd_from_tag(dd
, tag
);
720 fis
= (struct host_to_dev_fis
*)cmd
->command
;
722 /* Should re-issue? */
723 if (tag
== MTIP_TAG_INTERNAL
||
724 fis
->command
== ATA_CMD_SET_FEATURES
)
727 if (fail_all_ncq_cmds
||
728 (fail_all_ncq_write
&&
729 fis
->command
== ATA_CMD_FPDMA_WRITE
)) {
730 dev_warn(&dd
->pdev
->dev
,
731 " Fail: %s w/tag %d [%s].\n",
732 fis
->command
== ATA_CMD_FPDMA_WRITE
?
735 fail_reason
!= NULL
?
736 fail_reason
: "unknown");
737 if (cmd
->comp_func
) {
738 cmd
->comp_func(port
, tag
,
746 * First check if this command has
747 * exceeded its retries.
749 if (reissue
&& (cmd
->retries
-- > 0)) {
751 set_bit(tag
, tagaccum
);
753 /* Re-issue the command. */
754 mtip_issue_ncq_command(port
, tag
);
759 /* Retire a command that will not be reissued */
760 dev_warn(&port
->dd
->pdev
->dev
,
761 "retiring tag %d\n", tag
);
764 cmd
->comp_func(port
, tag
, cmd
, PORT_IRQ_TF_ERR
);
766 dev_warn(&port
->dd
->pdev
->dev
,
767 "Bad completion for tag %d\n",
771 print_tags(dd
, "reissued (TFE)", tagaccum
, cmd_cnt
);
775 * Handle a set device bits interrupt
777 static inline void mtip_workq_sdbfx(struct mtip_port
*port
, int group
,
780 struct driver_data
*dd
= port
->dd
;
782 struct mtip_cmd
*command
;
785 WARN_ON_ONCE(!completed
);
788 /* clear completed status register in the hardware.*/
789 writel(completed
, port
->completed
[group
]);
791 /* Process completed commands. */
792 for (bit
= 0; (bit
< 32) && completed
; bit
++) {
793 if (completed
& 0x01) {
794 tag
= (group
<< 5) | bit
;
796 /* skip internal command slot. */
797 if (unlikely(tag
== MTIP_TAG_INTERNAL
))
800 command
= mtip_cmd_from_tag(dd
, tag
);
801 if (likely(command
->comp_func
))
802 command
->comp_func(port
, tag
, command
, 0);
804 dev_dbg(&dd
->pdev
->dev
,
805 "Null completion for tag %d",
808 if (mtip_check_surprise_removal(
817 /* If last, re-enable interrupts */
818 if (atomic_dec_return(&dd
->irq_workers_active
) == 0)
819 writel(0xffffffff, dd
->mmio
+ HOST_IRQ_STAT
);
823 * Process legacy pio and d2h interrupts
825 static inline void mtip_process_legacy(struct driver_data
*dd
, u32 port_stat
)
827 struct mtip_port
*port
= dd
->port
;
828 struct mtip_cmd
*cmd
= mtip_cmd_from_tag(dd
, MTIP_TAG_INTERNAL
);
830 if (test_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
) &&
831 (cmd
!= NULL
) && !(readl(port
->cmd_issue
[MTIP_TAG_INTERNAL
])
832 & (1 << MTIP_TAG_INTERNAL
))) {
833 if (cmd
->comp_func
) {
834 cmd
->comp_func(port
, MTIP_TAG_INTERNAL
, cmd
, 0);
843 * Demux and handle errors
845 static inline void mtip_process_errors(struct driver_data
*dd
, u32 port_stat
)
848 if (unlikely(port_stat
& PORT_IRQ_CONNECT
)) {
849 dev_warn(&dd
->pdev
->dev
,
850 "Clearing PxSERR.DIAG.x\n");
851 writel((1 << 26), dd
->port
->mmio
+ PORT_SCR_ERR
);
854 if (unlikely(port_stat
& PORT_IRQ_PHYRDY
)) {
855 dev_warn(&dd
->pdev
->dev
,
856 "Clearing PxSERR.DIAG.n\n");
857 writel((1 << 16), dd
->port
->mmio
+ PORT_SCR_ERR
);
860 if (unlikely(port_stat
& ~PORT_IRQ_HANDLED
)) {
861 dev_warn(&dd
->pdev
->dev
,
862 "Port stat errors %x unhandled\n",
863 (port_stat
& ~PORT_IRQ_HANDLED
));
864 if (mtip_check_surprise_removal(dd
->pdev
))
867 if (likely(port_stat
& (PORT_IRQ_TF_ERR
| PORT_IRQ_IF_ERR
))) {
868 set_bit(MTIP_PF_EH_ACTIVE_BIT
, &dd
->port
->flags
);
869 wake_up_interruptible(&dd
->port
->svc_wait
);
873 static inline irqreturn_t
mtip_handle_irq(struct driver_data
*data
)
875 struct driver_data
*dd
= (struct driver_data
*) data
;
876 struct mtip_port
*port
= dd
->port
;
877 u32 hba_stat
, port_stat
;
879 int do_irq_enable
= 1, i
, workers
;
880 struct mtip_work
*twork
;
882 hba_stat
= readl(dd
->mmio
+ HOST_IRQ_STAT
);
886 /* Acknowledge the interrupt status on the port.*/
887 port_stat
= readl(port
->mmio
+ PORT_IRQ_STAT
);
888 if (unlikely(port_stat
== 0xFFFFFFFF)) {
889 mtip_check_surprise_removal(dd
->pdev
);
892 writel(port_stat
, port
->mmio
+ PORT_IRQ_STAT
);
894 /* Demux port status */
895 if (likely(port_stat
& PORT_IRQ_SDB_FIS
)) {
897 WARN_ON_ONCE(atomic_read(&dd
->irq_workers_active
) != 0);
899 /* Start at 1: group zero is always local? */
900 for (i
= 0, workers
= 0; i
< MTIP_MAX_SLOT_GROUPS
;
902 twork
= &dd
->work
[i
];
903 twork
->completed
= readl(port
->completed
[i
]);
904 if (twork
->completed
)
908 atomic_set(&dd
->irq_workers_active
, workers
);
910 for (i
= 1; i
< MTIP_MAX_SLOT_GROUPS
; i
++) {
911 twork
= &dd
->work
[i
];
912 if (twork
->completed
)
919 if (likely(dd
->work
[0].completed
))
920 mtip_workq_sdbfx(port
, 0,
921 dd
->work
[0].completed
);
925 * Chip quirk: SDB interrupt but nothing
932 if (unlikely(port_stat
& PORT_IRQ_ERR
)) {
933 if (unlikely(mtip_check_surprise_removal(dd
->pdev
))) {
934 /* don't proceed further */
937 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
941 mtip_process_errors(dd
, port_stat
& PORT_IRQ_ERR
);
944 if (unlikely(port_stat
& PORT_IRQ_LEGACY
))
945 mtip_process_legacy(dd
, port_stat
& PORT_IRQ_LEGACY
);
948 /* acknowledge interrupt */
949 if (unlikely(do_irq_enable
))
950 writel(hba_stat
, dd
->mmio
+ HOST_IRQ_STAT
);
956 * HBA interrupt subroutine.
959 * @instance Pointer to the driver data structure.
962 * IRQ_HANDLED A HBA interrupt was pending and handled.
963 * IRQ_NONE This interrupt was not for the HBA.
965 static irqreturn_t
mtip_irq_handler(int irq
, void *instance
)
967 struct driver_data
*dd
= instance
;
969 return mtip_handle_irq(dd
);
972 static void mtip_issue_non_ncq_command(struct mtip_port
*port
, int tag
)
974 writel(1 << MTIP_TAG_BIT(tag
),
975 port
->cmd_issue
[MTIP_TAG_INDEX(tag
)]);
978 static bool mtip_pause_ncq(struct mtip_port
*port
,
979 struct host_to_dev_fis
*fis
)
981 struct host_to_dev_fis
*reply
;
982 unsigned long task_file_data
;
984 reply
= port
->rxfis
+ RX_FIS_D2H_REG
;
985 task_file_data
= readl(port
->mmio
+PORT_TFDATA
);
987 if ((task_file_data
& 1))
990 if (fis
->command
== ATA_CMD_SEC_ERASE_PREP
) {
991 port
->ic_pause_timer
= jiffies
;
993 } else if ((fis
->command
== ATA_CMD_DOWNLOAD_MICRO
) &&
994 (fis
->features
== 0x03)) {
995 set_bit(MTIP_PF_DM_ACTIVE_BIT
, &port
->flags
);
996 port
->ic_pause_timer
= jiffies
;
998 } else if ((fis
->command
== ATA_CMD_SEC_ERASE_UNIT
) ||
999 ((fis
->command
== 0xFC) &&
1000 (fis
->features
== 0x27 || fis
->features
== 0x72 ||
1001 fis
->features
== 0x62 || fis
->features
== 0x26))) {
1002 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
1003 /* Com reset after secure erase or lowlevel format */
1004 mtip_restart_port(port
);
1005 clear_bit(MTIP_PF_SE_ACTIVE_BIT
, &port
->flags
);
1013 * Wait for port to quiesce
1015 * @port Pointer to port data structure
1016 * @timeout Max duration to wait (ms)
1020 * -EBUSY Commands still active
1022 static int mtip_quiesce_io(struct mtip_port
*port
, unsigned long timeout
)
1026 unsigned int active
= 1;
1028 blk_mq_stop_hw_queues(port
->dd
->queue
);
1030 to
= jiffies
+ msecs_to_jiffies(timeout
);
1032 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
) &&
1033 test_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
)) {
1035 continue; /* svc thd is actively issuing commands */
1039 if (mtip_check_surprise_removal(port
->dd
->pdev
))
1041 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
1045 * Ignore s_active bit 0 of array element 0.
1046 * This bit will always be set
1048 active
= readl(port
->s_active
[0]) & 0xFFFFFFFE;
1049 for (n
= 1; n
< port
->dd
->slot_groups
; n
++)
1050 active
|= readl(port
->s_active
[n
]);
1054 } while (time_before(jiffies
, to
));
1056 blk_mq_start_stopped_hw_queues(port
->dd
->queue
, true);
1057 return active
? -EBUSY
: 0;
1059 blk_mq_start_stopped_hw_queues(port
->dd
->queue
, true);
1064 * Execute an internal command and wait for the completion.
1066 * @port Pointer to the port data structure.
1067 * @fis Pointer to the FIS that describes the command.
1068 * @fis_len Length in WORDS of the FIS.
1069 * @buffer DMA accessible for command data.
1070 * @buf_len Length, in bytes, of the data buffer.
1071 * @opts Command header options, excluding the FIS length
1072 * and the number of PRD entries.
1073 * @timeout Time in ms to wait for the command to complete.
1076 * 0 Command completed successfully.
1077 * -EFAULT The buffer address is not correctly aligned.
1078 * -EBUSY Internal command or other IO in progress.
1079 * -EAGAIN Time out waiting for command to complete.
1081 static int mtip_exec_internal_command(struct mtip_port
*port
,
1082 struct host_to_dev_fis
*fis
,
1088 unsigned long timeout
)
1090 struct mtip_cmd_sg
*command_sg
;
1091 DECLARE_COMPLETION_ONSTACK(wait
);
1092 struct mtip_cmd
*int_cmd
;
1093 struct driver_data
*dd
= port
->dd
;
1095 unsigned long start
;
1097 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1098 if (buffer
& 0x00000007) {
1099 dev_err(&dd
->pdev
->dev
, "SG buffer is not 8 byte aligned\n");
1103 int_cmd
= mtip_get_int_command(dd
);
1105 set_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1107 if (fis
->command
== ATA_CMD_SEC_ERASE_PREP
)
1108 set_bit(MTIP_PF_SE_ACTIVE_BIT
, &port
->flags
);
1110 clear_bit(MTIP_PF_DM_ACTIVE_BIT
, &port
->flags
);
1112 if (atomic
== GFP_KERNEL
) {
1113 if (fis
->command
!= ATA_CMD_STANDBYNOW1
) {
1114 /* wait for io to complete if non atomic */
1115 if (mtip_quiesce_io(port
,
1116 MTIP_QUIESCE_IO_TIMEOUT_MS
) < 0) {
1117 dev_warn(&dd
->pdev
->dev
,
1118 "Failed to quiesce IO\n");
1119 mtip_put_int_command(dd
, int_cmd
);
1120 clear_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1121 wake_up_interruptible(&port
->svc_wait
);
1126 /* Set the completion function and data for the command. */
1127 int_cmd
->comp_data
= &wait
;
1128 int_cmd
->comp_func
= mtip_completion
;
1131 /* Clear completion - we're going to poll */
1132 int_cmd
->comp_data
= NULL
;
1133 int_cmd
->comp_func
= mtip_null_completion
;
1136 /* Copy the command to the command table */
1137 memcpy(int_cmd
->command
, fis
, fis_len
*4);
1139 /* Populate the SG list */
1140 int_cmd
->command_header
->opts
=
1141 __force_bit2int
cpu_to_le32(opts
| fis_len
);
1143 command_sg
= int_cmd
->command
+ AHCI_CMD_TBL_HDR_SZ
;
1146 __force_bit2int
cpu_to_le32((buf_len
-1) & 0x3FFFFF);
1148 __force_bit2int
cpu_to_le32(buffer
& 0xFFFFFFFF);
1149 command_sg
->dba_upper
=
1150 __force_bit2int
cpu_to_le32((buffer
>> 16) >> 16);
1152 int_cmd
->command_header
->opts
|=
1153 __force_bit2int
cpu_to_le32((1 << 16));
1156 /* Populate the command header */
1157 int_cmd
->command_header
->byte_count
= 0;
1161 /* Issue the command to the hardware */
1162 mtip_issue_non_ncq_command(port
, MTIP_TAG_INTERNAL
);
1164 if (atomic
== GFP_KERNEL
) {
1165 /* Wait for the command to complete or timeout. */
1166 if ((rv
= wait_for_completion_interruptible_timeout(
1168 msecs_to_jiffies(timeout
))) <= 0) {
1169 if (rv
== -ERESTARTSYS
) { /* interrupted */
1170 dev_err(&dd
->pdev
->dev
,
1171 "Internal command [%02X] was interrupted after %u ms\n",
1173 jiffies_to_msecs(jiffies
- start
));
1176 } else if (rv
== 0) /* timeout */
1177 dev_err(&dd
->pdev
->dev
,
1178 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1179 fis
->command
, timeout
);
1181 dev_err(&dd
->pdev
->dev
,
1182 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1183 fis
->command
, rv
, timeout
);
1185 if (mtip_check_surprise_removal(dd
->pdev
) ||
1186 test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
1188 dev_err(&dd
->pdev
->dev
,
1189 "Internal command [%02X] wait returned due to SR\n",
1194 mtip_device_reset(dd
); /* recover from timeout issue */
1199 u32 hba_stat
, port_stat
;
1201 /* Spin for <timeout> checking if command still outstanding */
1202 timeout
= jiffies
+ msecs_to_jiffies(timeout
);
1203 while ((readl(port
->cmd_issue
[MTIP_TAG_INTERNAL
])
1204 & (1 << MTIP_TAG_INTERNAL
))
1205 && time_before(jiffies
, timeout
)) {
1206 if (mtip_check_surprise_removal(dd
->pdev
)) {
1210 if ((fis
->command
!= ATA_CMD_STANDBYNOW1
) &&
1211 test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
1216 port_stat
= readl(port
->mmio
+ PORT_IRQ_STAT
);
1220 if (port_stat
& PORT_IRQ_ERR
) {
1221 dev_err(&dd
->pdev
->dev
,
1222 "Internal command [%02X] failed\n",
1224 mtip_device_reset(dd
);
1228 writel(port_stat
, port
->mmio
+ PORT_IRQ_STAT
);
1229 hba_stat
= readl(dd
->mmio
+ HOST_IRQ_STAT
);
1232 dd
->mmio
+ HOST_IRQ_STAT
);
1238 if (readl(port
->cmd_issue
[MTIP_TAG_INTERNAL
])
1239 & (1 << MTIP_TAG_INTERNAL
)) {
1241 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)) {
1242 mtip_device_reset(dd
);
1247 /* Clear the allocated and active bits for the internal command. */
1248 mtip_put_int_command(dd
, int_cmd
);
1249 clear_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1250 if (rv
>= 0 && mtip_pause_ncq(port
, fis
)) {
1254 wake_up_interruptible(&port
->svc_wait
);
1260 * Byte-swap ATA ID strings.
1262 * ATA identify data contains strings in byte-swapped 16-bit words.
1263 * They must be swapped (on all architectures) to be usable as C strings.
1264 * This function swaps bytes in-place.
1266 * @buf The buffer location of the string
1267 * @len The number of bytes to swap
1272 static inline void ata_swap_string(u16
*buf
, unsigned int len
)
1275 for (i
= 0; i
< (len
/2); i
++)
1276 be16_to_cpus(&buf
[i
]);
1279 static void mtip_set_timeout(struct driver_data
*dd
,
1280 struct host_to_dev_fis
*fis
,
1281 unsigned int *timeout
, u8 erasemode
)
1283 switch (fis
->command
) {
1284 case ATA_CMD_DOWNLOAD_MICRO
:
1285 *timeout
= 120000; /* 2 minutes */
1287 case ATA_CMD_SEC_ERASE_UNIT
:
1290 *timeout
= ((*(dd
->port
->identify
+ 90) * 2) * 60000);
1292 *timeout
= ((*(dd
->port
->identify
+ 89) * 2) * 60000);
1294 case ATA_CMD_STANDBYNOW1
:
1295 *timeout
= 120000; /* 2 minutes */
1299 *timeout
= 60000; /* 60 seconds */
1302 *timeout
= 15000; /* 15 seconds */
1305 *timeout
= MTIP_IOCTL_CMD_TIMEOUT_MS
;
1311 * Request the device identity information.
1313 * If a user space buffer is not specified, i.e. is NULL, the
1314 * identify information is still read from the drive and placed
1315 * into the identify data buffer (@e port->identify) in the
1316 * port data structure.
1317 * When the identify buffer contains valid identify information @e
1318 * port->identify_valid is non-zero.
1320 * @port Pointer to the port structure.
1321 * @user_buffer A user space buffer where the identify data should be
1325 * 0 Command completed successfully.
1326 * -EFAULT An error occurred while coping data to the user buffer.
1327 * -1 Command failed.
1329 static int mtip_get_identify(struct mtip_port
*port
, void __user
*user_buffer
)
1332 struct host_to_dev_fis fis
;
1334 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
1337 /* Build the FIS. */
1338 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1341 fis
.command
= ATA_CMD_ID_ATA
;
1343 /* Set the identify information as invalid. */
1344 port
->identify_valid
= 0;
1346 /* Clear the identify information. */
1347 memset(port
->identify
, 0, sizeof(u16
) * ATA_ID_WORDS
);
1349 /* Execute the command. */
1350 if (mtip_exec_internal_command(port
,
1354 sizeof(u16
) * ATA_ID_WORDS
,
1357 MTIP_INT_CMD_TIMEOUT_MS
)
1364 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1365 * perform field-sensitive swapping on the string fields.
1366 * See the kernel use of ata_id_string() for proof of this.
1368 #ifdef __LITTLE_ENDIAN
1369 ata_swap_string(port
->identify
+ 27, 40); /* model string*/
1370 ata_swap_string(port
->identify
+ 23, 8); /* firmware string*/
1371 ata_swap_string(port
->identify
+ 10, 20); /* serial# string*/
1375 for (i
= 0; i
< ATA_ID_WORDS
; i
++)
1376 port
->identify
[i
] = le16_to_cpu(port
->identify
[i
]);
1380 /* Check security locked state */
1381 if (port
->identify
[128] & 0x4)
1382 set_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
1384 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
1386 #ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
1387 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1388 if (port
->identify
[69] & (1 << 14) && port
->identify
[69] & (1 << 5))
1389 port
->dd
->trim_supp
= true;
1392 port
->dd
->trim_supp
= false;
1394 /* Set the identify buffer as valid. */
1395 port
->identify_valid
= 1;
1401 ATA_ID_WORDS
* sizeof(u16
))) {
1412 * Issue a standby immediate command to the device.
1414 * @port Pointer to the port structure.
1417 * 0 Command was executed successfully.
1418 * -1 An error occurred while executing the command.
1420 static int mtip_standby_immediate(struct mtip_port
*port
)
1423 struct host_to_dev_fis fis
;
1424 unsigned long start
;
1425 unsigned int timeout
;
1427 /* Build the FIS. */
1428 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1431 fis
.command
= ATA_CMD_STANDBYNOW1
;
1433 mtip_set_timeout(port
->dd
, &fis
, &timeout
, 0);
1436 rv
= mtip_exec_internal_command(port
,
1444 dbg_printk(MTIP_DRV_NAME
"Time taken to complete standby cmd: %d ms\n",
1445 jiffies_to_msecs(jiffies
- start
));
1447 dev_warn(&port
->dd
->pdev
->dev
,
1448 "STANDBY IMMEDIATE command failed.\n");
1454 * Issue a READ LOG EXT command to the device.
1456 * @port pointer to the port structure.
1457 * @page page number to fetch
1458 * @buffer pointer to buffer
1459 * @buffer_dma dma address corresponding to @buffer
1460 * @sectors page length to fetch, in sectors
1463 * @rv return value from mtip_exec_internal_command()
1465 static int mtip_read_log_page(struct mtip_port
*port
, u8 page
, u16
*buffer
,
1466 dma_addr_t buffer_dma
, unsigned int sectors
)
1468 struct host_to_dev_fis fis
;
1470 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1473 fis
.command
= ATA_CMD_READ_LOG_EXT
;
1474 fis
.sect_count
= sectors
& 0xFF;
1475 fis
.sect_cnt_ex
= (sectors
>> 8) & 0xFF;
1478 fis
.device
= ATA_DEVICE_OBS
;
1480 memset(buffer
, 0, sectors
* ATA_SECT_SIZE
);
1482 return mtip_exec_internal_command(port
,
1486 sectors
* ATA_SECT_SIZE
,
1489 MTIP_INT_CMD_TIMEOUT_MS
);
1493 * Issue a SMART READ DATA command to the device.
1495 * @port pointer to the port structure.
1496 * @buffer pointer to buffer
1497 * @buffer_dma dma address corresponding to @buffer
1500 * @rv return value from mtip_exec_internal_command()
1502 static int mtip_get_smart_data(struct mtip_port
*port
, u8
*buffer
,
1503 dma_addr_t buffer_dma
)
1505 struct host_to_dev_fis fis
;
1507 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1510 fis
.command
= ATA_CMD_SMART
;
1511 fis
.features
= 0xD0;
1515 fis
.device
= ATA_DEVICE_OBS
;
1517 return mtip_exec_internal_command(port
,
1528 * Get the value of a smart attribute
1530 * @port pointer to the port structure
1531 * @id attribute number
1532 * @attrib pointer to return attrib information corresponding to @id
1535 * -EINVAL NULL buffer passed or unsupported attribute @id.
1536 * -EPERM Identify data not valid, SMART not supported or not enabled
1538 static int mtip_get_smart_attr(struct mtip_port
*port
, unsigned int id
,
1539 struct smart_attr
*attrib
)
1542 struct smart_attr
*pattr
;
1547 if (!port
->identify_valid
) {
1548 dev_warn(&port
->dd
->pdev
->dev
, "IDENTIFY DATA not valid\n");
1551 if (!(port
->identify
[82] & 0x1)) {
1552 dev_warn(&port
->dd
->pdev
->dev
, "SMART not supported\n");
1555 if (!(port
->identify
[85] & 0x1)) {
1556 dev_warn(&port
->dd
->pdev
->dev
, "SMART not enabled\n");
1560 memset(port
->smart_buf
, 0, ATA_SECT_SIZE
);
1561 rv
= mtip_get_smart_data(port
, port
->smart_buf
, port
->smart_buf_dma
);
1563 dev_warn(&port
->dd
->pdev
->dev
, "Failed to ge SMART data\n");
1567 pattr
= (struct smart_attr
*)(port
->smart_buf
+ 2);
1568 for (i
= 0; i
< 29; i
++, pattr
++)
1569 if (pattr
->attr_id
== id
) {
1570 memcpy(attrib
, pattr
, sizeof(struct smart_attr
));
1575 dev_warn(&port
->dd
->pdev
->dev
,
1576 "Query for invalid SMART attribute ID\n");
1584 * Trim unused sectors
1586 * @dd pointer to driver_data structure
1588 * @len # of 512b sectors to trim
1591 * -ENOMEM Out of dma memory
1592 * -EINVAL Invalid parameters passed in, trim not supported
1593 * -EIO Error submitting trim request to hw
1595 static int mtip_send_trim(struct driver_data
*dd
, unsigned int lba
,
1599 u64 tlba
, tlen
, sect_left
;
1600 struct mtip_trim_entry
*buf
;
1601 dma_addr_t dma_addr
;
1602 struct host_to_dev_fis fis
;
1604 if (!len
|| dd
->trim_supp
== false)
1607 /* Trim request too big */
1608 WARN_ON(len
> (MTIP_MAX_TRIM_ENTRY_LEN
* MTIP_MAX_TRIM_ENTRIES
));
1610 /* Trim request not aligned on 4k boundary */
1611 WARN_ON(len
% 8 != 0);
1613 /* Warn if vu_trim structure is too big */
1614 WARN_ON(sizeof(struct mtip_trim
) > ATA_SECT_SIZE
);
1616 /* Allocate a DMA buffer for the trim structure */
1617 buf
= dmam_alloc_coherent(&dd
->pdev
->dev
, ATA_SECT_SIZE
, &dma_addr
,
1621 memset(buf
, 0, ATA_SECT_SIZE
);
1623 for (i
= 0, sect_left
= len
, tlba
= lba
;
1624 i
< MTIP_MAX_TRIM_ENTRIES
&& sect_left
;
1626 tlen
= (sect_left
>= MTIP_MAX_TRIM_ENTRY_LEN
?
1627 MTIP_MAX_TRIM_ENTRY_LEN
:
1629 buf
[i
].lba
= __force_bit2int
cpu_to_le32(tlba
);
1630 buf
[i
].range
= __force_bit2int
cpu_to_le16(tlen
);
1634 WARN_ON(sect_left
!= 0);
1637 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1641 fis
.features
= 0x60;
1643 fis
.device
= ATA_DEVICE_OBS
;
1645 if (mtip_exec_internal_command(dd
->port
,
1652 MTIP_TRIM_TIMEOUT_MS
) < 0)
1655 dmam_free_coherent(&dd
->pdev
->dev
, ATA_SECT_SIZE
, buf
, dma_addr
);
1660 * Get the drive capacity.
1662 * @dd Pointer to the device data structure.
1663 * @sectors Pointer to the variable that will receive the sector count.
1666 * 1 Capacity was returned successfully.
1667 * 0 The identify information is invalid.
1669 static bool mtip_hw_get_capacity(struct driver_data
*dd
, sector_t
*sectors
)
1671 struct mtip_port
*port
= dd
->port
;
1672 u64 total
, raw0
, raw1
, raw2
, raw3
;
1673 raw0
= port
->identify
[100];
1674 raw1
= port
->identify
[101];
1675 raw2
= port
->identify
[102];
1676 raw3
= port
->identify
[103];
1677 total
= raw0
| raw1
<<16 | raw2
<<32 | raw3
<<48;
1679 return (bool) !!port
->identify_valid
;
1683 * Display the identify command data.
1685 * @port Pointer to the port data structure.
1690 static void mtip_dump_identify(struct mtip_port
*port
)
1693 unsigned short revid
;
1696 if (!port
->identify_valid
)
1699 strlcpy(cbuf
, (char *)(port
->identify
+10), 21);
1700 dev_info(&port
->dd
->pdev
->dev
,
1701 "Serial No.: %s\n", cbuf
);
1703 strlcpy(cbuf
, (char *)(port
->identify
+23), 9);
1704 dev_info(&port
->dd
->pdev
->dev
,
1705 "Firmware Ver.: %s\n", cbuf
);
1707 strlcpy(cbuf
, (char *)(port
->identify
+27), 41);
1708 dev_info(&port
->dd
->pdev
->dev
, "Model: %s\n", cbuf
);
1710 dev_info(&port
->dd
->pdev
->dev
, "Security: %04x %s\n",
1711 port
->identify
[128],
1712 port
->identify
[128] & 0x4 ? "(LOCKED)" : "");
1714 if (mtip_hw_get_capacity(port
->dd
, §ors
))
1715 dev_info(&port
->dd
->pdev
->dev
,
1716 "Capacity: %llu sectors (%llu MB)\n",
1718 ((u64
)sectors
) * ATA_SECT_SIZE
>> 20);
1720 pci_read_config_word(port
->dd
->pdev
, PCI_REVISION_ID
, &revid
);
1721 switch (revid
& 0xFF) {
1723 strlcpy(cbuf
, "A0", 3);
1726 strlcpy(cbuf
, "A2", 3);
1729 strlcpy(cbuf
, "?", 2);
1732 dev_info(&port
->dd
->pdev
->dev
,
1733 "Card Type: %s\n", cbuf
);
1737 * Map the commands scatter list into the command table.
1739 * @command Pointer to the command.
1740 * @nents Number of scatter list entries.
1745 static inline void fill_command_sg(struct driver_data
*dd
,
1746 struct mtip_cmd
*command
,
1750 unsigned int dma_len
;
1751 struct mtip_cmd_sg
*command_sg
;
1752 struct scatterlist
*sg
= command
->sg
;
1754 command_sg
= command
->command
+ AHCI_CMD_TBL_HDR_SZ
;
1756 for (n
= 0; n
< nents
; n
++) {
1757 dma_len
= sg_dma_len(sg
);
1758 if (dma_len
> 0x400000)
1759 dev_err(&dd
->pdev
->dev
,
1760 "DMA segment length truncated\n");
1761 command_sg
->info
= __force_bit2int
1762 cpu_to_le32((dma_len
-1) & 0x3FFFFF);
1763 command_sg
->dba
= __force_bit2int
1764 cpu_to_le32(sg_dma_address(sg
));
1765 command_sg
->dba_upper
= __force_bit2int
1766 cpu_to_le32((sg_dma_address(sg
) >> 16) >> 16);
1773 * @brief Execute a drive command.
1775 * return value 0 The command completed successfully.
1776 * return value -1 An error occurred while executing the command.
1778 static int exec_drive_task(struct mtip_port
*port
, u8
*command
)
1780 struct host_to_dev_fis fis
;
1781 struct host_to_dev_fis
*reply
= (port
->rxfis
+ RX_FIS_D2H_REG
);
1784 /* Build the FIS. */
1785 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1788 fis
.command
= command
[0];
1789 fis
.features
= command
[1];
1790 fis
.sect_count
= command
[2];
1791 fis
.sector
= command
[3];
1792 fis
.cyl_low
= command
[4];
1793 fis
.cyl_hi
= command
[5];
1794 fis
.device
= command
[6] & ~0x10; /* Clear the dev bit*/
1796 mtip_set_timeout(port
->dd
, &fis
, &to
, 0);
1798 dbg_printk(MTIP_DRV_NAME
" %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
1808 /* Execute the command. */
1809 if (mtip_exec_internal_command(port
,
1820 command
[0] = reply
->command
; /* Status*/
1821 command
[1] = reply
->features
; /* Error*/
1822 command
[4] = reply
->cyl_low
;
1823 command
[5] = reply
->cyl_hi
;
1825 dbg_printk(MTIP_DRV_NAME
" %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
1836 * @brief Execute a drive command.
1838 * @param port Pointer to the port data structure.
1839 * @param command Pointer to the user specified command parameters.
1840 * @param user_buffer Pointer to the user space buffer where read sector
1841 * data should be copied.
1843 * return value 0 The command completed successfully.
1844 * return value -EFAULT An error occurred while copying the completion
1845 * data to the user space buffer.
1846 * return value -1 An error occurred while executing the command.
1848 static int exec_drive_command(struct mtip_port
*port
, u8
*command
,
1849 void __user
*user_buffer
)
1851 struct host_to_dev_fis fis
;
1852 struct host_to_dev_fis
*reply
;
1854 dma_addr_t dma_addr
= 0;
1855 int rv
= 0, xfer_sz
= command
[3];
1862 buf
= dmam_alloc_coherent(&port
->dd
->pdev
->dev
,
1863 ATA_SECT_SIZE
* xfer_sz
,
1867 dev_err(&port
->dd
->pdev
->dev
,
1868 "Memory allocation failed (%d bytes)\n",
1869 ATA_SECT_SIZE
* xfer_sz
);
1872 memset(buf
, 0, ATA_SECT_SIZE
* xfer_sz
);
1875 /* Build the FIS. */
1876 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1879 fis
.command
= command
[0];
1880 fis
.features
= command
[2];
1881 fis
.sect_count
= command
[3];
1882 if (fis
.command
== ATA_CMD_SMART
) {
1883 fis
.sector
= command
[1];
1888 mtip_set_timeout(port
->dd
, &fis
, &to
, 0);
1891 reply
= (port
->rxfis
+ RX_FIS_PIO_SETUP
);
1893 reply
= (port
->rxfis
+ RX_FIS_D2H_REG
);
1895 dbg_printk(MTIP_DRV_NAME
1896 " %s: User Command: cmd %x, sect %x, "
1897 "feat %x, sectcnt %x\n",
1904 /* Execute the command. */
1905 if (mtip_exec_internal_command(port
,
1908 (xfer_sz
? dma_addr
: 0),
1909 (xfer_sz
? ATA_SECT_SIZE
* xfer_sz
: 0),
1915 goto exit_drive_command
;
1918 /* Collect the completion status. */
1919 command
[0] = reply
->command
; /* Status*/
1920 command
[1] = reply
->features
; /* Error*/
1921 command
[2] = reply
->sect_count
;
1923 dbg_printk(MTIP_DRV_NAME
1924 " %s: Completion Status: stat %x, "
1925 "err %x, nsect %x\n",
1932 if (copy_to_user(user_buffer
,
1934 ATA_SECT_SIZE
* command
[3])) {
1936 goto exit_drive_command
;
1941 dmam_free_coherent(&port
->dd
->pdev
->dev
,
1942 ATA_SECT_SIZE
* xfer_sz
, buf
, dma_addr
);
1947 * Indicates whether a command has a single sector payload.
1949 * @command passed to the device to perform the certain event.
1950 * @features passed to the device to perform the certain event.
1953 * 1 command is one that always has a single sector payload,
1954 * regardless of the value in the Sector Count field.
1958 static unsigned int implicit_sector(unsigned char command
,
1959 unsigned char features
)
1961 unsigned int rv
= 0;
1963 /* list of commands that have an implicit sector count of 1 */
1965 case ATA_CMD_SEC_SET_PASS
:
1966 case ATA_CMD_SEC_UNLOCK
:
1967 case ATA_CMD_SEC_ERASE_PREP
:
1968 case ATA_CMD_SEC_ERASE_UNIT
:
1969 case ATA_CMD_SEC_FREEZE_LOCK
:
1970 case ATA_CMD_SEC_DISABLE_PASS
:
1971 case ATA_CMD_PMP_READ
:
1972 case ATA_CMD_PMP_WRITE
:
1975 case ATA_CMD_SET_MAX
:
1976 if (features
== ATA_SET_MAX_UNLOCK
)
1980 if ((features
== ATA_SMART_READ_VALUES
) ||
1981 (features
== ATA_SMART_READ_THRESHOLDS
))
1984 case ATA_CMD_CONF_OVERLAY
:
1985 if ((features
== ATA_DCO_IDENTIFY
) ||
1986 (features
== ATA_DCO_SET
))
1994 * Executes a taskfile
1995 * See ide_taskfile_ioctl() for derivation
1997 static int exec_drive_taskfile(struct driver_data
*dd
,
1999 ide_task_request_t
*req_task
,
2002 struct host_to_dev_fis fis
;
2003 struct host_to_dev_fis
*reply
;
2006 dma_addr_t outbuf_dma
= 0;
2007 dma_addr_t inbuf_dma
= 0;
2008 dma_addr_t dma_buffer
= 0;
2010 unsigned int taskin
= 0;
2011 unsigned int taskout
= 0;
2013 unsigned int timeout
;
2014 unsigned int force_single_sector
;
2015 unsigned int transfer_size
;
2016 unsigned long task_file_data
;
2017 int intotal
= outtotal
+ req_task
->out_size
;
2020 taskout
= req_task
->out_size
;
2021 taskin
= req_task
->in_size
;
2022 /* 130560 = 512 * 0xFF*/
2023 if (taskin
> 130560 || taskout
> 130560) {
2029 outbuf
= memdup_user(buf
+ outtotal
, taskout
);
2030 if (IS_ERR(outbuf
)) {
2031 err
= PTR_ERR(outbuf
);
2035 outbuf_dma
= pci_map_single(dd
->pdev
,
2039 if (outbuf_dma
== 0) {
2043 dma_buffer
= outbuf_dma
;
2047 inbuf
= memdup_user(buf
+ intotal
, taskin
);
2048 if (IS_ERR(inbuf
)) {
2049 err
= PTR_ERR(inbuf
);
2053 inbuf_dma
= pci_map_single(dd
->pdev
,
2055 taskin
, DMA_FROM_DEVICE
);
2056 if (inbuf_dma
== 0) {
2060 dma_buffer
= inbuf_dma
;
2063 /* only supports PIO and non-data commands from this ioctl. */
2064 switch (req_task
->data_phase
) {
2066 nsect
= taskout
/ ATA_SECT_SIZE
;
2067 reply
= (dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
);
2070 reply
= (dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
);
2072 case TASKFILE_NO_DATA
:
2073 reply
= (dd
->port
->rxfis
+ RX_FIS_D2H_REG
);
2080 /* Build the FIS. */
2081 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
2085 fis
.command
= req_task
->io_ports
[7];
2086 fis
.features
= req_task
->io_ports
[1];
2087 fis
.sect_count
= req_task
->io_ports
[2];
2088 fis
.lba_low
= req_task
->io_ports
[3];
2089 fis
.lba_mid
= req_task
->io_ports
[4];
2090 fis
.lba_hi
= req_task
->io_ports
[5];
2091 /* Clear the dev bit*/
2092 fis
.device
= req_task
->io_ports
[6] & ~0x10;
2094 if ((req_task
->in_flags
.all
== 0) && (req_task
->out_flags
.all
& 1)) {
2095 req_task
->in_flags
.all
=
2096 IDE_TASKFILE_STD_IN_FLAGS
|
2097 (IDE_HOB_STD_IN_FLAGS
<< 8);
2098 fis
.lba_low_ex
= req_task
->hob_ports
[3];
2099 fis
.lba_mid_ex
= req_task
->hob_ports
[4];
2100 fis
.lba_hi_ex
= req_task
->hob_ports
[5];
2101 fis
.features_ex
= req_task
->hob_ports
[1];
2102 fis
.sect_cnt_ex
= req_task
->hob_ports
[2];
2105 req_task
->in_flags
.all
= IDE_TASKFILE_STD_IN_FLAGS
;
2108 force_single_sector
= implicit_sector(fis
.command
, fis
.features
);
2110 if ((taskin
|| taskout
) && (!fis
.sect_count
)) {
2112 fis
.sect_count
= nsect
;
2114 if (!force_single_sector
) {
2115 dev_warn(&dd
->pdev
->dev
,
2116 "data movement but "
2117 "sect_count is 0\n");
2124 dbg_printk(MTIP_DRV_NAME
2125 " %s: cmd %x, feat %x, nsect %x,"
2126 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2137 /* check for erase mode support during secure erase.*/
2138 if ((fis
.command
== ATA_CMD_SEC_ERASE_UNIT
) && outbuf
&&
2139 (outbuf
[0] & MTIP_SEC_ERASE_MODE
)) {
2143 mtip_set_timeout(dd
, &fis
, &timeout
, erasemode
);
2145 /* Determine the correct transfer size.*/
2146 if (force_single_sector
)
2147 transfer_size
= ATA_SECT_SIZE
;
2149 transfer_size
= ATA_SECT_SIZE
* fis
.sect_count
;
2151 /* Execute the command.*/
2152 if (mtip_exec_internal_command(dd
->port
,
2164 task_file_data
= readl(dd
->port
->mmio
+PORT_TFDATA
);
2166 if ((req_task
->data_phase
== TASKFILE_IN
) && !(task_file_data
& 1)) {
2167 reply
= dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
;
2168 req_task
->io_ports
[7] = reply
->control
;
2170 reply
= dd
->port
->rxfis
+ RX_FIS_D2H_REG
;
2171 req_task
->io_ports
[7] = reply
->command
;
2174 /* reclaim the DMA buffers.*/
2176 pci_unmap_single(dd
->pdev
, inbuf_dma
,
2177 taskin
, DMA_FROM_DEVICE
);
2179 pci_unmap_single(dd
->pdev
, outbuf_dma
,
2180 taskout
, DMA_TO_DEVICE
);
2184 /* return the ATA registers to the caller.*/
2185 req_task
->io_ports
[1] = reply
->features
;
2186 req_task
->io_ports
[2] = reply
->sect_count
;
2187 req_task
->io_ports
[3] = reply
->lba_low
;
2188 req_task
->io_ports
[4] = reply
->lba_mid
;
2189 req_task
->io_ports
[5] = reply
->lba_hi
;
2190 req_task
->io_ports
[6] = reply
->device
;
2192 if (req_task
->out_flags
.all
& 1) {
2194 req_task
->hob_ports
[3] = reply
->lba_low_ex
;
2195 req_task
->hob_ports
[4] = reply
->lba_mid_ex
;
2196 req_task
->hob_ports
[5] = reply
->lba_hi_ex
;
2197 req_task
->hob_ports
[1] = reply
->features_ex
;
2198 req_task
->hob_ports
[2] = reply
->sect_cnt_ex
;
2200 dbg_printk(MTIP_DRV_NAME
2201 " %s: Completion: stat %x,"
2202 "err %x, sect_cnt %x, lbalo %x,"
2203 "lbamid %x, lbahi %x, dev %x\n",
2205 req_task
->io_ports
[7],
2206 req_task
->io_ports
[1],
2207 req_task
->io_ports
[2],
2208 req_task
->io_ports
[3],
2209 req_task
->io_ports
[4],
2210 req_task
->io_ports
[5],
2211 req_task
->io_ports
[6]);
2214 if (copy_to_user(buf
+ outtotal
, outbuf
, taskout
)) {
2220 if (copy_to_user(buf
+ intotal
, inbuf
, taskin
)) {
2227 pci_unmap_single(dd
->pdev
, inbuf_dma
,
2228 taskin
, DMA_FROM_DEVICE
);
2230 pci_unmap_single(dd
->pdev
, outbuf_dma
,
2231 taskout
, DMA_TO_DEVICE
);
2239 * Handle IOCTL calls from the Block Layer.
2241 * This function is called by the Block Layer when it receives an IOCTL
2242 * command that it does not understand. If the IOCTL command is not supported
2243 * this function returns -ENOTTY.
2245 * @dd Pointer to the driver data structure.
2246 * @cmd IOCTL command passed from the Block Layer.
2247 * @arg IOCTL argument passed from the Block Layer.
2250 * 0 The IOCTL completed successfully.
2251 * -ENOTTY The specified command is not supported.
2252 * -EFAULT An error occurred copying data to a user space buffer.
2253 * -EIO An error occurred while executing the command.
2255 static int mtip_hw_ioctl(struct driver_data
*dd
, unsigned int cmd
,
2259 case HDIO_GET_IDENTITY
:
2261 if (copy_to_user((void __user
*)arg
, dd
->port
->identify
,
2262 sizeof(u16
) * ATA_ID_WORDS
))
2266 case HDIO_DRIVE_CMD
:
2268 u8 drive_command
[4];
2270 /* Copy the user command info to our buffer. */
2271 if (copy_from_user(drive_command
,
2272 (void __user
*) arg
,
2273 sizeof(drive_command
)))
2276 /* Execute the drive command. */
2277 if (exec_drive_command(dd
->port
,
2279 (void __user
*) (arg
+4)))
2282 /* Copy the status back to the users buffer. */
2283 if (copy_to_user((void __user
*) arg
,
2285 sizeof(drive_command
)))
2290 case HDIO_DRIVE_TASK
:
2292 u8 drive_command
[7];
2294 /* Copy the user command info to our buffer. */
2295 if (copy_from_user(drive_command
,
2296 (void __user
*) arg
,
2297 sizeof(drive_command
)))
2300 /* Execute the drive command. */
2301 if (exec_drive_task(dd
->port
, drive_command
))
2304 /* Copy the status back to the users buffer. */
2305 if (copy_to_user((void __user
*) arg
,
2307 sizeof(drive_command
)))
2312 case HDIO_DRIVE_TASKFILE
: {
2313 ide_task_request_t req_task
;
2316 if (copy_from_user(&req_task
, (void __user
*) arg
,
2320 outtotal
= sizeof(req_task
);
2322 ret
= exec_drive_taskfile(dd
, (void __user
*) arg
,
2323 &req_task
, outtotal
);
2325 if (copy_to_user((void __user
*) arg
, &req_task
,
2339 * Submit an IO to the hw
2341 * This function is called by the block layer to issue an io
2342 * to the device. Upon completion, the callback function will
2343 * be called with the data parameter passed as the callback data.
2345 * @dd Pointer to the driver data structure.
2346 * @start First sector to read.
2347 * @nsect Number of sectors to read.
2348 * @nents Number of entries in scatter list for the read command.
2349 * @tag The tag of this read command.
2350 * @callback Pointer to the function that should be called
2351 * when the read completes.
2352 * @data Callback data passed to the callback function
2353 * when the read completes.
2354 * @dir Direction (read or write)
2359 static void mtip_hw_submit_io(struct driver_data
*dd
, struct request
*rq
,
2360 struct mtip_cmd
*command
, int nents
,
2361 struct blk_mq_hw_ctx
*hctx
)
2363 struct host_to_dev_fis
*fis
;
2364 struct mtip_port
*port
= dd
->port
;
2365 int dma_dir
= rq_data_dir(rq
) == READ
? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
2366 u64 start
= blk_rq_pos(rq
);
2367 unsigned int nsect
= blk_rq_sectors(rq
);
2369 /* Map the scatter list for DMA access */
2370 nents
= dma_map_sg(&dd
->pdev
->dev
, command
->sg
, nents
, dma_dir
);
2372 prefetch(&port
->flags
);
2374 command
->scatter_ents
= nents
;
2377 * The number of retries for this command before it is
2378 * reported as a failure to the upper layers.
2380 command
->retries
= MTIP_MAX_RETRIES
;
2383 fis
= command
->command
;
2386 if (dma_dir
== DMA_FROM_DEVICE
)
2387 fis
->command
= ATA_CMD_FPDMA_READ
;
2389 fis
->command
= ATA_CMD_FPDMA_WRITE
;
2390 fis
->lba_low
= start
& 0xFF;
2391 fis
->lba_mid
= (start
>> 8) & 0xFF;
2392 fis
->lba_hi
= (start
>> 16) & 0xFF;
2393 fis
->lba_low_ex
= (start
>> 24) & 0xFF;
2394 fis
->lba_mid_ex
= (start
>> 32) & 0xFF;
2395 fis
->lba_hi_ex
= (start
>> 40) & 0xFF;
2396 fis
->device
= 1 << 6;
2397 fis
->features
= nsect
& 0xFF;
2398 fis
->features_ex
= (nsect
>> 8) & 0xFF;
2399 fis
->sect_count
= ((rq
->tag
<< 3) | (rq
->tag
>> 5));
2400 fis
->sect_cnt_ex
= 0;
2404 fill_command_sg(dd
, command
, nents
);
2406 if (unlikely(command
->unaligned
))
2407 fis
->device
|= 1 << 7;
2409 /* Populate the command header */
2410 command
->command_header
->opts
=
2411 __force_bit2int
cpu_to_le32(
2412 (nents
<< 16) | 5 | AHCI_CMD_PREFETCH
);
2413 command
->command_header
->byte_count
= 0;
2416 * Set the completion function and data for the command
2417 * within this layer.
2419 command
->comp_data
= dd
;
2420 command
->comp_func
= mtip_async_complete
;
2421 command
->direction
= dma_dir
;
2424 * To prevent this command from being issued
2425 * if an internal command is in progress or error handling is active.
2427 if (unlikely(port
->flags
& MTIP_PF_PAUSE_IO
)) {
2428 set_bit(rq
->tag
, port
->cmds_to_issue
);
2429 set_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
);
2433 /* Issue the command to the hardware */
2434 mtip_issue_ncq_command(port
, rq
->tag
);
2438 * Sysfs status dump.
2440 * @dev Pointer to the device structure, passed by the kernrel.
2441 * @attr Pointer to the device_attribute structure passed by the kernel.
2442 * @buf Pointer to the char buffer that will receive the stats info.
2445 * The size, in bytes, of the data copied into buf.
2447 static ssize_t
mtip_hw_show_status(struct device
*dev
,
2448 struct device_attribute
*attr
,
2451 struct driver_data
*dd
= dev_to_disk(dev
)->private_data
;
2454 if (test_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
))
2455 size
+= sprintf(buf
, "%s", "thermal_shutdown\n");
2456 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
))
2457 size
+= sprintf(buf
, "%s", "write_protect\n");
2459 size
+= sprintf(buf
, "%s", "online\n");
2464 static DEVICE_ATTR(status
, S_IRUGO
, mtip_hw_show_status
, NULL
);
2466 /* debugsfs entries */
2468 static ssize_t
show_device_status(struct device_driver
*drv
, char *buf
)
2471 struct driver_data
*dd
, *tmp
;
2472 unsigned long flags
;
2476 spin_lock_irqsave(&dev_lock
, flags
);
2477 size
+= sprintf(&buf
[size
], "Devices Present:\n");
2478 list_for_each_entry_safe(dd
, tmp
, &online_list
, online_list
) {
2481 dd
->port
->identify
&&
2482 dd
->port
->identify_valid
) {
2484 (char *) (dd
->port
->identify
+ 10), 21);
2485 status
= *(dd
->port
->identify
+ 141);
2487 memset(id_buf
, 0, 42);
2492 test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
)) {
2493 size
+= sprintf(&buf
[size
],
2494 " device %s %s (ftl rebuild %d %%)\n",
2495 dev_name(&dd
->pdev
->dev
),
2499 size
+= sprintf(&buf
[size
],
2501 dev_name(&dd
->pdev
->dev
),
2507 size
+= sprintf(&buf
[size
], "Devices Being Removed:\n");
2508 list_for_each_entry_safe(dd
, tmp
, &removing_list
, remove_list
) {
2511 dd
->port
->identify
&&
2512 dd
->port
->identify_valid
) {
2514 (char *) (dd
->port
->identify
+10), 21);
2515 status
= *(dd
->port
->identify
+ 141);
2517 memset(id_buf
, 0, 42);
2522 test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
)) {
2523 size
+= sprintf(&buf
[size
],
2524 " device %s %s (ftl rebuild %d %%)\n",
2525 dev_name(&dd
->pdev
->dev
),
2529 size
+= sprintf(&buf
[size
],
2531 dev_name(&dd
->pdev
->dev
),
2536 spin_unlock_irqrestore(&dev_lock
, flags
);
2541 static ssize_t
mtip_hw_read_device_status(struct file
*f
, char __user
*ubuf
,
2542 size_t len
, loff_t
*offset
)
2544 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2549 if (!len
|| *offset
)
2552 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2554 dev_err(&dd
->pdev
->dev
,
2555 "Memory allocation: status buffer\n");
2559 size
+= show_device_status(NULL
, buf
);
2561 *offset
= size
<= len
? size
: len
;
2562 size
= copy_to_user(ubuf
, buf
, *offset
);
2567 return rv
? rv
: *offset
;
2570 static ssize_t
mtip_hw_read_registers(struct file
*f
, char __user
*ubuf
,
2571 size_t len
, loff_t
*offset
)
2573 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2575 u32 group_allocated
;
2582 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2584 dev_err(&dd
->pdev
->dev
,
2585 "Memory allocation: register buffer\n");
2589 size
+= sprintf(&buf
[size
], "H/ S ACTive : [ 0x");
2591 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2592 size
+= sprintf(&buf
[size
], "%08X ",
2593 readl(dd
->port
->s_active
[n
]));
2595 size
+= sprintf(&buf
[size
], "]\n");
2596 size
+= sprintf(&buf
[size
], "H/ Command Issue : [ 0x");
2598 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2599 size
+= sprintf(&buf
[size
], "%08X ",
2600 readl(dd
->port
->cmd_issue
[n
]));
2602 size
+= sprintf(&buf
[size
], "]\n");
2603 size
+= sprintf(&buf
[size
], "H/ Completed : [ 0x");
2605 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2606 size
+= sprintf(&buf
[size
], "%08X ",
2607 readl(dd
->port
->completed
[n
]));
2609 size
+= sprintf(&buf
[size
], "]\n");
2610 size
+= sprintf(&buf
[size
], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2611 readl(dd
->port
->mmio
+ PORT_IRQ_STAT
));
2612 size
+= sprintf(&buf
[size
], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2613 readl(dd
->mmio
+ HOST_IRQ_STAT
));
2614 size
+= sprintf(&buf
[size
], "\n");
2616 size
+= sprintf(&buf
[size
], "L/ Commands in Q : [ 0x");
2618 for (n
= dd
->slot_groups
-1; n
>= 0; n
--) {
2619 if (sizeof(long) > sizeof(u32
))
2621 dd
->port
->cmds_to_issue
[n
/2] >> (32*(n
&1));
2623 group_allocated
= dd
->port
->cmds_to_issue
[n
];
2624 size
+= sprintf(&buf
[size
], "%08X ", group_allocated
);
2626 size
+= sprintf(&buf
[size
], "]\n");
2628 *offset
= size
<= len
? size
: len
;
2629 size
= copy_to_user(ubuf
, buf
, *offset
);
2634 return rv
? rv
: *offset
;
2637 static ssize_t
mtip_hw_read_flags(struct file
*f
, char __user
*ubuf
,
2638 size_t len
, loff_t
*offset
)
2640 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2648 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2650 dev_err(&dd
->pdev
->dev
,
2651 "Memory allocation: flag buffer\n");
2655 size
+= sprintf(&buf
[size
], "Flag-port : [ %08lX ]\n",
2657 size
+= sprintf(&buf
[size
], "Flag-dd : [ %08lX ]\n",
2660 *offset
= size
<= len
? size
: len
;
2661 size
= copy_to_user(ubuf
, buf
, *offset
);
2666 return rv
? rv
: *offset
;
2669 static const struct file_operations mtip_device_status_fops
= {
2670 .owner
= THIS_MODULE
,
2671 .open
= simple_open
,
2672 .read
= mtip_hw_read_device_status
,
2673 .llseek
= no_llseek
,
2676 static const struct file_operations mtip_regs_fops
= {
2677 .owner
= THIS_MODULE
,
2678 .open
= simple_open
,
2679 .read
= mtip_hw_read_registers
,
2680 .llseek
= no_llseek
,
2683 static const struct file_operations mtip_flags_fops
= {
2684 .owner
= THIS_MODULE
,
2685 .open
= simple_open
,
2686 .read
= mtip_hw_read_flags
,
2687 .llseek
= no_llseek
,
2691 * Create the sysfs related attributes.
2693 * @dd Pointer to the driver data structure.
2694 * @kobj Pointer to the kobj for the block device.
2697 * 0 Operation completed successfully.
2698 * -EINVAL Invalid parameter.
2700 static int mtip_hw_sysfs_init(struct driver_data
*dd
, struct kobject
*kobj
)
2705 if (sysfs_create_file(kobj
, &dev_attr_status
.attr
))
2706 dev_warn(&dd
->pdev
->dev
,
2707 "Error creating 'status' sysfs entry\n");
2712 * Remove the sysfs related attributes.
2714 * @dd Pointer to the driver data structure.
2715 * @kobj Pointer to the kobj for the block device.
2718 * 0 Operation completed successfully.
2719 * -EINVAL Invalid parameter.
2721 static int mtip_hw_sysfs_exit(struct driver_data
*dd
, struct kobject
*kobj
)
2726 sysfs_remove_file(kobj
, &dev_attr_status
.attr
);
2731 static int mtip_hw_debugfs_init(struct driver_data
*dd
)
2736 dd
->dfs_node
= debugfs_create_dir(dd
->disk
->disk_name
, dfs_parent
);
2737 if (IS_ERR_OR_NULL(dd
->dfs_node
)) {
2738 dev_warn(&dd
->pdev
->dev
,
2739 "Error creating node %s under debugfs\n",
2740 dd
->disk
->disk_name
);
2741 dd
->dfs_node
= NULL
;
2745 debugfs_create_file("flags", S_IRUGO
, dd
->dfs_node
, dd
,
2747 debugfs_create_file("registers", S_IRUGO
, dd
->dfs_node
, dd
,
2753 static void mtip_hw_debugfs_exit(struct driver_data
*dd
)
2756 debugfs_remove_recursive(dd
->dfs_node
);
2760 * Perform any init/resume time hardware setup
2762 * @dd Pointer to the driver data structure.
2767 static inline void hba_setup(struct driver_data
*dd
)
2770 hwdata
= readl(dd
->mmio
+ HOST_HSORG
);
2772 /* interrupt bug workaround: use only 1 IS bit.*/
2774 HSORG_DISABLE_SLOTGRP_INTR
|
2775 HSORG_DISABLE_SLOTGRP_PXIS
,
2776 dd
->mmio
+ HOST_HSORG
);
2779 static int mtip_device_unaligned_constrained(struct driver_data
*dd
)
2781 return (dd
->pdev
->device
== P420M_DEVICE_ID
? 1 : 0);
2785 * Detect the details of the product, and store anything needed
2786 * into the driver data structure. This includes product type and
2787 * version and number of slot groups.
2789 * @dd Pointer to the driver data structure.
2794 static void mtip_detect_product(struct driver_data
*dd
)
2797 unsigned int rev
, slotgroups
;
2800 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2802 * [15:8] hardware/software interface rev#
2803 * [ 3] asic-style interface
2804 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2806 hwdata
= readl(dd
->mmio
+ HOST_HSORG
);
2808 dd
->product_type
= MTIP_PRODUCT_UNKNOWN
;
2809 dd
->slot_groups
= 1;
2812 dd
->product_type
= MTIP_PRODUCT_ASICFPGA
;
2813 rev
= (hwdata
& HSORG_HWREV
) >> 8;
2814 slotgroups
= (hwdata
& HSORG_SLOTGROUPS
) + 1;
2815 dev_info(&dd
->pdev
->dev
,
2816 "ASIC-FPGA design, HS rev 0x%x, "
2817 "%i slot groups [%i slots]\n",
2822 if (slotgroups
> MTIP_MAX_SLOT_GROUPS
) {
2823 dev_warn(&dd
->pdev
->dev
,
2824 "Warning: driver only supports "
2825 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS
);
2826 slotgroups
= MTIP_MAX_SLOT_GROUPS
;
2828 dd
->slot_groups
= slotgroups
;
2832 dev_warn(&dd
->pdev
->dev
, "Unrecognized product id\n");
2836 * Blocking wait for FTL rebuild to complete
2838 * @dd Pointer to the DRIVER_DATA structure.
2841 * 0 FTL rebuild completed successfully
2842 * -EFAULT FTL rebuild error/timeout/interruption
2844 static int mtip_ftl_rebuild_poll(struct driver_data
*dd
)
2846 unsigned long timeout
, cnt
= 0, start
;
2848 dev_warn(&dd
->pdev
->dev
,
2849 "FTL rebuild in progress. Polling for completion.\n");
2852 timeout
= jiffies
+ msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS
);
2855 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
2858 if (mtip_check_surprise_removal(dd
->pdev
))
2861 if (mtip_get_identify(dd
->port
, NULL
) < 0)
2864 if (*(dd
->port
->identify
+ MTIP_FTL_REBUILD_OFFSET
) ==
2865 MTIP_FTL_REBUILD_MAGIC
) {
2867 /* Print message every 3 minutes */
2869 dev_warn(&dd
->pdev
->dev
,
2870 "FTL rebuild in progress (%d secs).\n",
2871 jiffies_to_msecs(jiffies
- start
) / 1000);
2875 dev_warn(&dd
->pdev
->dev
,
2876 "FTL rebuild complete (%d secs).\n",
2877 jiffies_to_msecs(jiffies
- start
) / 1000);
2878 mtip_block_initialize(dd
);
2881 } while (time_before(jiffies
, timeout
));
2883 /* Check for timeout */
2884 dev_err(&dd
->pdev
->dev
,
2885 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2886 jiffies_to_msecs(jiffies
- start
) / 1000);
2891 * service thread to issue queued commands
2893 * @data Pointer to the driver data structure.
2899 static int mtip_service_thread(void *data
)
2901 struct driver_data
*dd
= (struct driver_data
*)data
;
2902 unsigned long slot
, slot_start
, slot_wrap
;
2903 unsigned int num_cmd_slots
= dd
->slot_groups
* 32;
2904 struct mtip_port
*port
= dd
->port
;
2907 if (kthread_should_stop() ||
2908 test_bit(MTIP_PF_SVC_THD_STOP_BIT
, &port
->flags
))
2910 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
);
2913 * the condition is to check neither an internal command is
2914 * is in progress nor error handling is active
2916 wait_event_interruptible(port
->svc_wait
, (port
->flags
) &&
2917 (port
->flags
& MTIP_PF_SVC_THD_WORK
));
2919 if (kthread_should_stop() ||
2920 test_bit(MTIP_PF_SVC_THD_STOP_BIT
, &port
->flags
))
2923 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
2927 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
);
2930 /* Demux bits: start with error handling */
2931 if (test_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
)) {
2932 mtip_handle_tfe(dd
);
2933 clear_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
);
2936 if (test_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
))
2939 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
)) {
2941 /* used to restrict the loop to one iteration */
2942 slot_start
= num_cmd_slots
;
2945 slot
= find_next_bit(port
->cmds_to_issue
,
2946 num_cmd_slots
, slot
);
2947 if (slot_wrap
== 1) {
2948 if ((slot_start
>= slot
) ||
2949 (slot
>= num_cmd_slots
))
2952 if (unlikely(slot_start
== num_cmd_slots
))
2955 if (unlikely(slot
== num_cmd_slots
)) {
2961 /* Issue the command to the hardware */
2962 mtip_issue_ncq_command(port
, slot
);
2964 clear_bit(slot
, port
->cmds_to_issue
);
2967 clear_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
);
2970 if (test_bit(MTIP_PF_REBUILD_BIT
, &port
->flags
)) {
2971 if (mtip_ftl_rebuild_poll(dd
) < 0)
2972 set_bit(MTIP_DDF_REBUILD_FAILED_BIT
,
2974 clear_bit(MTIP_PF_REBUILD_BIT
, &port
->flags
);
2983 * DMA region teardown
2985 * @dd Pointer to driver_data structure
2990 static void mtip_dma_free(struct driver_data
*dd
)
2992 struct mtip_port
*port
= dd
->port
;
2995 dmam_free_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
2996 port
->block1
, port
->block1_dma
);
2998 if (port
->command_list
) {
2999 dmam_free_coherent(&dd
->pdev
->dev
, AHCI_CMD_TBL_SZ
,
3000 port
->command_list
, port
->command_list_dma
);
3007 * @dd Pointer to driver_data structure
3010 * -ENOMEM Not enough free DMA region space to initialize driver
3012 static int mtip_dma_alloc(struct driver_data
*dd
)
3014 struct mtip_port
*port
= dd
->port
;
3016 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
3018 dmam_alloc_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
3019 &port
->block1_dma
, GFP_KERNEL
);
3022 memset(port
->block1
, 0, BLOCK_DMA_ALLOC_SZ
);
3024 /* Allocate dma memory for command list */
3025 port
->command_list
=
3026 dmam_alloc_coherent(&dd
->pdev
->dev
, AHCI_CMD_TBL_SZ
,
3027 &port
->command_list_dma
, GFP_KERNEL
);
3028 if (!port
->command_list
) {
3029 dmam_free_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
3030 port
->block1
, port
->block1_dma
);
3031 port
->block1
= NULL
;
3032 port
->block1_dma
= 0;
3035 memset(port
->command_list
, 0, AHCI_CMD_TBL_SZ
);
3037 /* Setup all pointers into first DMA region */
3038 port
->rxfis
= port
->block1
+ AHCI_RX_FIS_OFFSET
;
3039 port
->rxfis_dma
= port
->block1_dma
+ AHCI_RX_FIS_OFFSET
;
3040 port
->identify
= port
->block1
+ AHCI_IDFY_OFFSET
;
3041 port
->identify_dma
= port
->block1_dma
+ AHCI_IDFY_OFFSET
;
3042 port
->log_buf
= port
->block1
+ AHCI_SECTBUF_OFFSET
;
3043 port
->log_buf_dma
= port
->block1_dma
+ AHCI_SECTBUF_OFFSET
;
3044 port
->smart_buf
= port
->block1
+ AHCI_SMARTBUF_OFFSET
;
3045 port
->smart_buf_dma
= port
->block1_dma
+ AHCI_SMARTBUF_OFFSET
;
3050 static int mtip_hw_get_identify(struct driver_data
*dd
)
3052 struct smart_attr attr242
;
3056 if (mtip_get_identify(dd
->port
, NULL
) < 0)
3059 if (*(dd
->port
->identify
+ MTIP_FTL_REBUILD_OFFSET
) ==
3060 MTIP_FTL_REBUILD_MAGIC
) {
3061 set_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
);
3062 return MTIP_FTL_REBUILD_MAGIC
;
3064 mtip_dump_identify(dd
->port
);
3066 /* check write protect, over temp and rebuild statuses */
3067 rv
= mtip_read_log_page(dd
->port
, ATA_LOG_SATA_NCQ
,
3069 dd
->port
->log_buf_dma
, 1);
3071 dev_warn(&dd
->pdev
->dev
,
3072 "Error in READ LOG EXT (10h) command\n");
3073 /* non-critical error, don't fail the load */
3075 buf
= (unsigned char *)dd
->port
->log_buf
;
3076 if (buf
[259] & 0x1) {
3077 dev_info(&dd
->pdev
->dev
,
3078 "Write protect bit is set.\n");
3079 set_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
);
3081 if (buf
[288] == 0xF7) {
3082 dev_info(&dd
->pdev
->dev
,
3083 "Exceeded Tmax, drive in thermal shutdown.\n");
3084 set_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
);
3086 if (buf
[288] == 0xBF) {
3087 dev_info(&dd
->pdev
->dev
,
3088 "Drive indicates rebuild has failed.\n");
3093 /* get write protect progess */
3094 memset(&attr242
, 0, sizeof(struct smart_attr
));
3095 if (mtip_get_smart_attr(dd
->port
, 242, &attr242
))
3096 dev_warn(&dd
->pdev
->dev
,
3097 "Unable to check write protect progress\n");
3099 dev_info(&dd
->pdev
->dev
,
3100 "Write protect progress: %u%% (%u blocks)\n",
3101 attr242
.cur
, le32_to_cpu(attr242
.data
));
3107 * Called once for each card.
3109 * @dd Pointer to the driver data structure.
3112 * 0 on success, else an error code.
3114 static int mtip_hw_init(struct driver_data
*dd
)
3118 unsigned int num_command_slots
;
3119 unsigned long timeout
, timetaken
;
3121 dd
->mmio
= pcim_iomap_table(dd
->pdev
)[MTIP_ABAR
];
3123 mtip_detect_product(dd
);
3124 if (dd
->product_type
== MTIP_PRODUCT_UNKNOWN
) {
3128 num_command_slots
= dd
->slot_groups
* 32;
3132 dd
->port
= kzalloc_node(sizeof(struct mtip_port
), GFP_KERNEL
,
3135 dev_err(&dd
->pdev
->dev
,
3136 "Memory allocation: port structure\n");
3140 /* Continue workqueue setup */
3141 for (i
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++)
3142 dd
->work
[i
].port
= dd
->port
;
3144 /* Enable unaligned IO constraints for some devices */
3145 if (mtip_device_unaligned_constrained(dd
))
3146 dd
->unal_qdepth
= MTIP_MAX_UNALIGNED_SLOTS
;
3148 dd
->unal_qdepth
= 0;
3150 sema_init(&dd
->port
->cmd_slot_unal
, dd
->unal_qdepth
);
3152 /* Spinlock to prevent concurrent issue */
3153 for (i
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++)
3154 spin_lock_init(&dd
->port
->cmd_issue_lock
[i
]);
3156 /* Set the port mmio base address. */
3157 dd
->port
->mmio
= dd
->mmio
+ PORT_OFFSET
;
3160 /* DMA allocations */
3161 rv
= mtip_dma_alloc(dd
);
3165 /* Setup the pointers to the extended s_active and CI registers. */
3166 for (i
= 0; i
< dd
->slot_groups
; i
++) {
3167 dd
->port
->s_active
[i
] =
3168 dd
->port
->mmio
+ i
*0x80 + PORT_SCR_ACT
;
3169 dd
->port
->cmd_issue
[i
] =
3170 dd
->port
->mmio
+ i
*0x80 + PORT_COMMAND_ISSUE
;
3171 dd
->port
->completed
[i
] =
3172 dd
->port
->mmio
+ i
*0x80 + PORT_SDBV
;
3175 timetaken
= jiffies
;
3176 timeout
= jiffies
+ msecs_to_jiffies(30000);
3177 while (((readl(dd
->port
->mmio
+ PORT_SCR_STAT
) & 0x0F) != 0x03) &&
3178 time_before(jiffies
, timeout
)) {
3181 if (unlikely(mtip_check_surprise_removal(dd
->pdev
))) {
3182 timetaken
= jiffies
- timetaken
;
3183 dev_warn(&dd
->pdev
->dev
,
3184 "Surprise removal detected at %u ms\n",
3185 jiffies_to_msecs(timetaken
));
3189 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))) {
3190 timetaken
= jiffies
- timetaken
;
3191 dev_warn(&dd
->pdev
->dev
,
3192 "Removal detected at %u ms\n",
3193 jiffies_to_msecs(timetaken
));
3198 /* Conditionally reset the HBA. */
3199 if (!(readl(dd
->mmio
+ HOST_CAP
) & HOST_CAP_NZDMA
)) {
3200 if (mtip_hba_reset(dd
) < 0) {
3201 dev_err(&dd
->pdev
->dev
,
3202 "Card did not reset within timeout\n");
3207 /* Clear any pending interrupts on the HBA */
3208 writel(readl(dd
->mmio
+ HOST_IRQ_STAT
),
3209 dd
->mmio
+ HOST_IRQ_STAT
);
3212 mtip_init_port(dd
->port
);
3213 mtip_start_port(dd
->port
);
3215 /* Setup the ISR and enable interrupts. */
3216 rv
= devm_request_irq(&dd
->pdev
->dev
,
3220 dev_driver_string(&dd
->pdev
->dev
),
3224 dev_err(&dd
->pdev
->dev
,
3225 "Unable to allocate IRQ %d\n", dd
->pdev
->irq
);
3228 irq_set_affinity_hint(dd
->pdev
->irq
, get_cpu_mask(dd
->isr_binding
));
3230 /* Enable interrupts on the HBA. */
3231 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
3232 dd
->mmio
+ HOST_CTL
);
3234 init_waitqueue_head(&dd
->port
->svc_wait
);
3236 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)) {
3244 /* Disable interrupts on the HBA. */
3245 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3246 dd
->mmio
+ HOST_CTL
);
3248 /* Release the IRQ. */
3249 irq_set_affinity_hint(dd
->pdev
->irq
, NULL
);
3250 devm_free_irq(&dd
->pdev
->dev
, dd
->pdev
->irq
, dd
);
3253 mtip_deinit_port(dd
->port
);
3257 /* Free the memory allocated for the for structure. */
3263 static void mtip_standby_drive(struct driver_data
*dd
)
3269 * Send standby immediate (E0h) to the drive so that it
3272 if (!test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
) &&
3273 !test_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
))
3274 if (mtip_standby_immediate(dd
->port
))
3275 dev_warn(&dd
->pdev
->dev
,
3276 "STANDBY IMMEDIATE failed\n");
3280 * Called to deinitialize an interface.
3282 * @dd Pointer to the driver data structure.
3287 static int mtip_hw_exit(struct driver_data
*dd
)
3290 * Send standby immediate (E0h) to the drive so that it
3294 /* de-initialize the port. */
3295 mtip_deinit_port(dd
->port
);
3297 /* Disable interrupts on the HBA. */
3298 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3299 dd
->mmio
+ HOST_CTL
);
3302 /* Release the IRQ. */
3303 irq_set_affinity_hint(dd
->pdev
->irq
, NULL
);
3304 devm_free_irq(&dd
->pdev
->dev
, dd
->pdev
->irq
, dd
);
3307 /* Free dma regions */
3310 /* Free the memory allocated for the for structure. */
3318 * Issue a Standby Immediate command to the device.
3320 * This function is called by the Block Layer just before the
3321 * system powers off during a shutdown.
3323 * @dd Pointer to the driver data structure.
3328 static int mtip_hw_shutdown(struct driver_data
*dd
)
3331 * Send standby immediate (E0h) to the drive so that it
3334 if (!dd
->sr
&& dd
->port
)
3335 mtip_standby_immediate(dd
->port
);
3343 * This function is called by the Block Layer just before the
3344 * system hibernates.
3346 * @dd Pointer to the driver data structure.
3349 * 0 Suspend was successful
3350 * -EFAULT Suspend was not successful
3352 static int mtip_hw_suspend(struct driver_data
*dd
)
3355 * Send standby immediate (E0h) to the drive
3356 * so that it saves its state.
3358 if (mtip_standby_immediate(dd
->port
) != 0) {
3359 dev_err(&dd
->pdev
->dev
,
3360 "Failed standby-immediate command\n");
3364 /* Disable interrupts on the HBA.*/
3365 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3366 dd
->mmio
+ HOST_CTL
);
3367 mtip_deinit_port(dd
->port
);
3375 * This function is called by the Block Layer as the
3378 * @dd Pointer to the driver data structure.
3381 * 0 Resume was successful
3382 * -EFAULT Resume was not successful
3384 static int mtip_hw_resume(struct driver_data
*dd
)
3386 /* Perform any needed hardware setup steps */
3390 if (mtip_hba_reset(dd
) != 0) {
3391 dev_err(&dd
->pdev
->dev
,
3392 "Unable to reset the HBA\n");
3397 * Enable the port, DMA engine, and FIS reception specific
3398 * h/w in controller.
3400 mtip_init_port(dd
->port
);
3401 mtip_start_port(dd
->port
);
3403 /* Enable interrupts on the HBA.*/
3404 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
3405 dd
->mmio
+ HOST_CTL
);
3411 * Helper function for reusing disk name
3412 * upon hot insertion.
3414 static int rssd_disk_name_format(char *prefix
,
3419 const int base
= 'z' - 'a' + 1;
3420 char *begin
= buf
+ strlen(prefix
);
3421 char *end
= buf
+ buflen
;
3431 *--p
= 'a' + (index
% unit
);
3432 index
= (index
/ unit
) - 1;
3433 } while (index
>= 0);
3435 memmove(begin
, p
, end
- p
);
3436 memcpy(buf
, prefix
, strlen(prefix
));
3442 * Block layer IOCTL handler.
3444 * @dev Pointer to the block_device structure.
3446 * @cmd IOCTL command passed from the user application.
3447 * @arg Argument passed from the user application.
3450 * 0 IOCTL completed successfully.
3451 * -ENOTTY IOCTL not supported or invalid driver data
3452 * structure pointer.
3454 static int mtip_block_ioctl(struct block_device
*dev
,
3459 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3461 if (!capable(CAP_SYS_ADMIN
))
3467 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)))
3474 return mtip_hw_ioctl(dd
, cmd
, arg
);
3478 #ifdef CONFIG_COMPAT
3480 * Block layer compat IOCTL handler.
3482 * @dev Pointer to the block_device structure.
3484 * @cmd IOCTL command passed from the user application.
3485 * @arg Argument passed from the user application.
3488 * 0 IOCTL completed successfully.
3489 * -ENOTTY IOCTL not supported or invalid driver data
3490 * structure pointer.
3492 static int mtip_block_compat_ioctl(struct block_device
*dev
,
3497 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3499 if (!capable(CAP_SYS_ADMIN
))
3505 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)))
3511 case HDIO_DRIVE_TASKFILE
: {
3512 struct mtip_compat_ide_task_request_s __user
*compat_req_task
;
3513 ide_task_request_t req_task
;
3514 int compat_tasksize
, outtotal
, ret
;
3517 sizeof(struct mtip_compat_ide_task_request_s
);
3520 (struct mtip_compat_ide_task_request_s __user
*) arg
;
3522 if (copy_from_user(&req_task
, (void __user
*) arg
,
3523 compat_tasksize
- (2 * sizeof(compat_long_t
))))
3526 if (get_user(req_task
.out_size
, &compat_req_task
->out_size
))
3529 if (get_user(req_task
.in_size
, &compat_req_task
->in_size
))
3532 outtotal
= sizeof(struct mtip_compat_ide_task_request_s
);
3534 ret
= exec_drive_taskfile(dd
, (void __user
*) arg
,
3535 &req_task
, outtotal
);
3537 if (copy_to_user((void __user
*) arg
, &req_task
,
3539 (2 * sizeof(compat_long_t
))))
3542 if (put_user(req_task
.out_size
, &compat_req_task
->out_size
))
3545 if (put_user(req_task
.in_size
, &compat_req_task
->in_size
))
3551 return mtip_hw_ioctl(dd
, cmd
, arg
);
3557 * Obtain the geometry of the device.
3559 * You may think that this function is obsolete, but some applications,
3560 * fdisk for example still used CHS values. This function describes the
3561 * device as having 224 heads and 56 sectors per cylinder. These values are
3562 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3563 * partition is described in terms of a start and end cylinder this means
3564 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3565 * affects performance.
3567 * @dev Pointer to the block_device strucutre.
3568 * @geo Pointer to a hd_geometry structure.
3571 * 0 Operation completed successfully.
3572 * -ENOTTY An error occurred while reading the drive capacity.
3574 static int mtip_block_getgeo(struct block_device
*dev
,
3575 struct hd_geometry
*geo
)
3577 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3583 if (!(mtip_hw_get_capacity(dd
, &capacity
))) {
3584 dev_warn(&dd
->pdev
->dev
,
3585 "Could not get drive capacity.\n");
3591 sector_div(capacity
, (geo
->heads
* geo
->sectors
));
3592 geo
->cylinders
= capacity
;
3597 * Block device operation function.
3599 * This structure contains pointers to the functions required by the block
3602 static const struct block_device_operations mtip_block_ops
= {
3603 .ioctl
= mtip_block_ioctl
,
3604 #ifdef CONFIG_COMPAT
3605 .compat_ioctl
= mtip_block_compat_ioctl
,
3607 .getgeo
= mtip_block_getgeo
,
3608 .owner
= THIS_MODULE
3611 static inline bool is_se_active(struct driver_data
*dd
)
3613 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT
, &dd
->port
->flags
))) {
3614 if (dd
->port
->ic_pause_timer
) {
3615 unsigned long to
= dd
->port
->ic_pause_timer
+
3616 msecs_to_jiffies(1000);
3617 if (time_after(jiffies
, to
)) {
3618 clear_bit(MTIP_PF_SE_ACTIVE_BIT
,
3620 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
);
3621 dd
->port
->ic_pause_timer
= 0;
3622 wake_up_interruptible(&dd
->port
->svc_wait
);
3632 * Block layer make request function.
3634 * This function is called by the kernel to process a BIO for
3637 * @queue Pointer to the request queue. Unused other than to obtain
3638 * the driver data structure.
3639 * @rq Pointer to the request.
3642 static int mtip_submit_request(struct blk_mq_hw_ctx
*hctx
, struct request
*rq
)
3644 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3645 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3648 if (is_se_active(dd
))
3651 if (unlikely(dd
->dd_flag
& MTIP_DDF_STOP_IO
)) {
3652 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
3656 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
))) {
3659 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT
,
3664 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
)))
3666 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
))
3670 if (rq
->cmd_flags
& REQ_DISCARD
) {
3673 err
= mtip_send_trim(dd
, blk_rq_pos(rq
), blk_rq_sectors(rq
));
3674 blk_mq_end_request(rq
, err
);
3678 /* Create the scatter list for this request. */
3679 nents
= blk_rq_map_sg(hctx
->queue
, rq
, cmd
->sg
);
3681 /* Issue the read/write. */
3682 mtip_hw_submit_io(dd
, rq
, cmd
, nents
, hctx
);
3686 static bool mtip_check_unal_depth(struct blk_mq_hw_ctx
*hctx
,
3689 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3690 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3692 if (rq_data_dir(rq
) == READ
|| !dd
->unal_qdepth
)
3696 * If unaligned depth must be limited on this controller, mark it
3697 * as unaligned if the IO isn't on a 4k boundary (start of length).
3699 if (blk_rq_sectors(rq
) <= 64) {
3700 if ((blk_rq_pos(rq
) & 7) || (blk_rq_sectors(rq
) & 7))
3704 if (cmd
->unaligned
&& down_trylock(&dd
->port
->cmd_slot_unal
))
3710 static int mtip_queue_rq(struct blk_mq_hw_ctx
*hctx
,
3711 const struct blk_mq_queue_data
*bd
)
3713 struct request
*rq
= bd
->rq
;
3716 if (unlikely(mtip_check_unal_depth(hctx
, rq
)))
3717 return BLK_MQ_RQ_QUEUE_BUSY
;
3719 blk_mq_start_request(rq
);
3721 ret
= mtip_submit_request(hctx
, rq
);
3723 return BLK_MQ_RQ_QUEUE_OK
;
3726 return BLK_MQ_RQ_QUEUE_ERROR
;
3729 static void mtip_free_cmd(void *data
, struct request
*rq
,
3730 unsigned int hctx_idx
, unsigned int request_idx
)
3732 struct driver_data
*dd
= data
;
3733 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3738 dmam_free_coherent(&dd
->pdev
->dev
, CMD_DMA_ALLOC_SZ
,
3739 cmd
->command
, cmd
->command_dma
);
3742 static int mtip_init_cmd(void *data
, struct request
*rq
, unsigned int hctx_idx
,
3743 unsigned int request_idx
, unsigned int numa_node
)
3745 struct driver_data
*dd
= data
;
3746 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3747 u32 host_cap_64
= readl(dd
->mmio
+ HOST_CAP
) & HOST_CAP_64
;
3750 * For flush requests, request_idx starts at the end of the
3751 * tag space. Since we don't support FLUSH/FUA, simply return
3752 * 0 as there's nothing to be done.
3754 if (request_idx
>= MTIP_MAX_COMMAND_SLOTS
)
3757 cmd
->command
= dmam_alloc_coherent(&dd
->pdev
->dev
, CMD_DMA_ALLOC_SZ
,
3758 &cmd
->command_dma
, GFP_KERNEL
);
3762 memset(cmd
->command
, 0, CMD_DMA_ALLOC_SZ
);
3764 /* Point the command headers at the command tables. */
3765 cmd
->command_header
= dd
->port
->command_list
+
3766 (sizeof(struct mtip_cmd_hdr
) * request_idx
);
3767 cmd
->command_header_dma
= dd
->port
->command_list_dma
+
3768 (sizeof(struct mtip_cmd_hdr
) * request_idx
);
3771 cmd
->command_header
->ctbau
= __force_bit2int
cpu_to_le32((cmd
->command_dma
>> 16) >> 16);
3773 cmd
->command_header
->ctba
= __force_bit2int
cpu_to_le32(cmd
->command_dma
& 0xFFFFFFFF);
3775 sg_init_table(cmd
->sg
, MTIP_MAX_SG
);
3779 static struct blk_mq_ops mtip_mq_ops
= {
3780 .queue_rq
= mtip_queue_rq
,
3781 .map_queue
= blk_mq_map_queue
,
3782 .init_request
= mtip_init_cmd
,
3783 .exit_request
= mtip_free_cmd
,
3787 * Block layer initialization function.
3789 * This function is called once by the PCI layer for each P320
3790 * device that is connected to the system.
3792 * @dd Pointer to the driver data structure.
3795 * 0 on success else an error code.
3797 static int mtip_block_initialize(struct driver_data
*dd
)
3799 int rv
= 0, wait_for_rebuild
= 0;
3801 unsigned int index
= 0;
3802 struct kobject
*kobj
;
3805 goto skip_create_disk
; /* hw init done, before rebuild */
3807 if (mtip_hw_init(dd
)) {
3809 goto protocol_init_error
;
3812 dd
->disk
= alloc_disk_node(MTIP_MAX_MINORS
, dd
->numa_node
);
3813 if (dd
->disk
== NULL
) {
3814 dev_err(&dd
->pdev
->dev
,
3815 "Unable to allocate gendisk structure\n");
3817 goto alloc_disk_error
;
3820 /* Generate the disk name, implemented same as in sd.c */
3822 if (!ida_pre_get(&rssd_index_ida
, GFP_KERNEL
))
3825 spin_lock(&rssd_index_lock
);
3826 rv
= ida_get_new(&rssd_index_ida
, &index
);
3827 spin_unlock(&rssd_index_lock
);
3828 } while (rv
== -EAGAIN
);
3833 rv
= rssd_disk_name_format("rssd",
3835 dd
->disk
->disk_name
,
3838 goto disk_index_error
;
3840 dd
->disk
->driverfs_dev
= &dd
->pdev
->dev
;
3841 dd
->disk
->major
= dd
->major
;
3842 dd
->disk
->first_minor
= index
* MTIP_MAX_MINORS
;
3843 dd
->disk
->minors
= MTIP_MAX_MINORS
;
3844 dd
->disk
->fops
= &mtip_block_ops
;
3845 dd
->disk
->private_data
= dd
;
3848 mtip_hw_debugfs_init(dd
);
3851 memset(&dd
->tags
, 0, sizeof(dd
->tags
));
3852 dd
->tags
.ops
= &mtip_mq_ops
;
3853 dd
->tags
.nr_hw_queues
= 1;
3854 dd
->tags
.queue_depth
= MTIP_MAX_COMMAND_SLOTS
;
3855 dd
->tags
.reserved_tags
= 1;
3856 dd
->tags
.cmd_size
= sizeof(struct mtip_cmd
);
3857 dd
->tags
.numa_node
= dd
->numa_node
;
3858 dd
->tags
.flags
= BLK_MQ_F_SHOULD_MERGE
;
3859 dd
->tags
.driver_data
= dd
;
3861 rv
= blk_mq_alloc_tag_set(&dd
->tags
);
3863 dev_err(&dd
->pdev
->dev
,
3864 "Unable to allocate request queue\n");
3865 goto block_queue_alloc_init_error
;
3868 /* Allocate the request queue. */
3869 dd
->queue
= blk_mq_init_queue(&dd
->tags
);
3870 if (IS_ERR(dd
->queue
)) {
3871 dev_err(&dd
->pdev
->dev
,
3872 "Unable to allocate request queue\n");
3874 goto block_queue_alloc_init_error
;
3877 dd
->disk
->queue
= dd
->queue
;
3878 dd
->queue
->queuedata
= dd
;
3880 /* Initialize the protocol layer. */
3881 wait_for_rebuild
= mtip_hw_get_identify(dd
);
3882 if (wait_for_rebuild
< 0) {
3883 dev_err(&dd
->pdev
->dev
,
3884 "Protocol layer initialization failed\n");
3886 goto init_hw_cmds_error
;
3890 * if rebuild pending, start the service thread, and delay the block
3891 * queue creation and add_disk()
3893 if (wait_for_rebuild
== MTIP_FTL_REBUILD_MAGIC
)
3894 goto start_service_thread
;
3896 /* Set device limits. */
3897 set_bit(QUEUE_FLAG_NONROT
, &dd
->queue
->queue_flags
);
3898 clear_bit(QUEUE_FLAG_ADD_RANDOM
, &dd
->queue
->queue_flags
);
3899 blk_queue_max_segments(dd
->queue
, MTIP_MAX_SG
);
3900 blk_queue_physical_block_size(dd
->queue
, 4096);
3901 blk_queue_max_hw_sectors(dd
->queue
, 0xffff);
3902 blk_queue_max_segment_size(dd
->queue
, 0x400000);
3903 blk_queue_io_min(dd
->queue
, 4096);
3904 blk_queue_bounce_limit(dd
->queue
, dd
->pdev
->dma_mask
);
3907 * write back cache is not supported in the device. FUA depends on
3908 * write back cache support, hence setting flush support to zero.
3910 blk_queue_flush(dd
->queue
, 0);
3912 /* Signal trim support */
3913 if (dd
->trim_supp
== true) {
3914 set_bit(QUEUE_FLAG_DISCARD
, &dd
->queue
->queue_flags
);
3915 dd
->queue
->limits
.discard_granularity
= 4096;
3916 blk_queue_max_discard_sectors(dd
->queue
,
3917 MTIP_MAX_TRIM_ENTRY_LEN
* MTIP_MAX_TRIM_ENTRIES
);
3918 dd
->queue
->limits
.discard_zeroes_data
= 0;
3921 /* Set the capacity of the device in 512 byte sectors. */
3922 if (!(mtip_hw_get_capacity(dd
, &capacity
))) {
3923 dev_warn(&dd
->pdev
->dev
,
3924 "Could not read drive capacity\n");
3926 goto read_capacity_error
;
3928 set_capacity(dd
->disk
, capacity
);
3930 /* Enable the block device and add it to /dev */
3933 dd
->bdev
= bdget_disk(dd
->disk
, 0);
3935 * Now that the disk is active, initialize any sysfs attributes
3936 * managed by the protocol layer.
3938 kobj
= kobject_get(&disk_to_dev(dd
->disk
)->kobj
);
3940 mtip_hw_sysfs_init(dd
, kobj
);
3944 if (dd
->mtip_svc_handler
) {
3945 set_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
);
3946 return rv
; /* service thread created for handling rebuild */
3949 start_service_thread
:
3950 dd
->mtip_svc_handler
= kthread_create_on_node(mtip_service_thread
,
3952 "mtip_svc_thd_%02d", index
);
3954 if (IS_ERR(dd
->mtip_svc_handler
)) {
3955 dev_err(&dd
->pdev
->dev
, "service thread failed to start\n");
3956 dd
->mtip_svc_handler
= NULL
;
3958 goto kthread_run_error
;
3960 wake_up_process(dd
->mtip_svc_handler
);
3961 if (wait_for_rebuild
== MTIP_FTL_REBUILD_MAGIC
)
3962 rv
= wait_for_rebuild
;
3970 /* Delete our gendisk. This also removes the device from /dev */
3971 del_gendisk(dd
->disk
);
3973 read_capacity_error
:
3975 blk_cleanup_queue(dd
->queue
);
3976 blk_mq_free_tag_set(&dd
->tags
);
3977 block_queue_alloc_init_error
:
3978 mtip_hw_debugfs_exit(dd
);
3980 spin_lock(&rssd_index_lock
);
3981 ida_remove(&rssd_index_ida
, index
);
3982 spin_unlock(&rssd_index_lock
);
3988 mtip_hw_exit(dd
); /* De-initialize the protocol layer. */
3990 protocol_init_error
:
3995 * Block layer deinitialization function.
3997 * Called by the PCI layer as each P320 device is removed.
3999 * @dd Pointer to the driver data structure.
4004 static int mtip_block_remove(struct driver_data
*dd
)
4006 struct kobject
*kobj
;
4008 mtip_hw_debugfs_exit(dd
);
4010 if (dd
->mtip_svc_handler
) {
4011 set_bit(MTIP_PF_SVC_THD_STOP_BIT
, &dd
->port
->flags
);
4012 wake_up_interruptible(&dd
->port
->svc_wait
);
4013 kthread_stop(dd
->mtip_svc_handler
);
4016 /* Clean up the sysfs attributes, if created */
4017 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
)) {
4018 kobj
= kobject_get(&disk_to_dev(dd
->disk
)->kobj
);
4020 mtip_hw_sysfs_exit(dd
, kobj
);
4026 mtip_standby_drive(dd
);
4028 dev_info(&dd
->pdev
->dev
, "device %s surprise removal\n",
4029 dd
->disk
->disk_name
);
4032 * Delete our gendisk structure. This also removes the device
4040 del_gendisk(dd
->disk
);
4041 if (dd
->disk
->queue
) {
4042 blk_cleanup_queue(dd
->queue
);
4043 blk_mq_free_tag_set(&dd
->tags
);
4050 spin_lock(&rssd_index_lock
);
4051 ida_remove(&rssd_index_ida
, dd
->index
);
4052 spin_unlock(&rssd_index_lock
);
4054 /* De-initialize the protocol layer. */
4061 * Function called by the PCI layer when just before the
4062 * machine shuts down.
4064 * If a protocol layer shutdown function is present it will be called
4067 * @dd Pointer to the driver data structure.
4072 static int mtip_block_shutdown(struct driver_data
*dd
)
4074 mtip_hw_shutdown(dd
);
4076 /* Delete our gendisk structure, and cleanup the blk queue. */
4078 dev_info(&dd
->pdev
->dev
,
4079 "Shutting down %s ...\n", dd
->disk
->disk_name
);
4081 del_gendisk(dd
->disk
);
4082 if (dd
->disk
->queue
) {
4083 blk_cleanup_queue(dd
->queue
);
4084 blk_mq_free_tag_set(&dd
->tags
);
4091 spin_lock(&rssd_index_lock
);
4092 ida_remove(&rssd_index_ida
, dd
->index
);
4093 spin_unlock(&rssd_index_lock
);
4097 static int mtip_block_suspend(struct driver_data
*dd
)
4099 dev_info(&dd
->pdev
->dev
,
4100 "Suspending %s ...\n", dd
->disk
->disk_name
);
4101 mtip_hw_suspend(dd
);
4105 static int mtip_block_resume(struct driver_data
*dd
)
4107 dev_info(&dd
->pdev
->dev
, "Resuming %s ...\n",
4108 dd
->disk
->disk_name
);
4113 static void drop_cpu(int cpu
)
4118 static int get_least_used_cpu_on_node(int node
)
4120 int cpu
, least_used_cpu
, least_cnt
;
4121 const struct cpumask
*node_mask
;
4123 node_mask
= cpumask_of_node(node
);
4124 least_used_cpu
= cpumask_first(node_mask
);
4125 least_cnt
= cpu_use
[least_used_cpu
];
4126 cpu
= least_used_cpu
;
4128 for_each_cpu(cpu
, node_mask
) {
4129 if (cpu_use
[cpu
] < least_cnt
) {
4130 least_used_cpu
= cpu
;
4131 least_cnt
= cpu_use
[cpu
];
4134 cpu_use
[least_used_cpu
]++;
4135 return least_used_cpu
;
4138 /* Helper for selecting a node in round robin mode */
4139 static inline int mtip_get_next_rr_node(void)
4141 static int next_node
= -1;
4143 if (next_node
== -1) {
4144 next_node
= first_online_node
;
4148 next_node
= next_online_node(next_node
);
4149 if (next_node
== MAX_NUMNODES
)
4150 next_node
= first_online_node
;
4154 static DEFINE_HANDLER(0);
4155 static DEFINE_HANDLER(1);
4156 static DEFINE_HANDLER(2);
4157 static DEFINE_HANDLER(3);
4158 static DEFINE_HANDLER(4);
4159 static DEFINE_HANDLER(5);
4160 static DEFINE_HANDLER(6);
4161 static DEFINE_HANDLER(7);
4163 static void mtip_disable_link_opts(struct driver_data
*dd
, struct pci_dev
*pdev
)
4166 unsigned short pcie_dev_ctrl
;
4168 pos
= pci_find_capability(pdev
, PCI_CAP_ID_EXP
);
4170 pci_read_config_word(pdev
,
4171 pos
+ PCI_EXP_DEVCTL
,
4173 if (pcie_dev_ctrl
& (1 << 11) ||
4174 pcie_dev_ctrl
& (1 << 4)) {
4175 dev_info(&dd
->pdev
->dev
,
4176 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4177 pdev
->vendor
, pdev
->device
);
4178 pcie_dev_ctrl
&= ~(PCI_EXP_DEVCTL_NOSNOOP_EN
|
4179 PCI_EXP_DEVCTL_RELAX_EN
);
4180 pci_write_config_word(pdev
,
4181 pos
+ PCI_EXP_DEVCTL
,
4187 static void mtip_fix_ero_nosnoop(struct driver_data
*dd
, struct pci_dev
*pdev
)
4190 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4191 * device with device id 0x5aXX
4193 if (pdev
->bus
&& pdev
->bus
->self
) {
4194 if (pdev
->bus
->self
->vendor
== PCI_VENDOR_ID_ATI
&&
4195 ((pdev
->bus
->self
->device
& 0xff00) == 0x5a00)) {
4196 mtip_disable_link_opts(dd
, pdev
->bus
->self
);
4198 /* Check further up the topology */
4199 struct pci_dev
*parent_dev
= pdev
->bus
->self
;
4200 if (parent_dev
->bus
&&
4201 parent_dev
->bus
->parent
&&
4202 parent_dev
->bus
->parent
->self
&&
4203 parent_dev
->bus
->parent
->self
->vendor
==
4204 PCI_VENDOR_ID_ATI
&&
4205 (parent_dev
->bus
->parent
->self
->device
&
4206 0xff00) == 0x5a00) {
4207 mtip_disable_link_opts(dd
,
4208 parent_dev
->bus
->parent
->self
);
4215 * Called for each supported PCI device detected.
4217 * This function allocates the private data structure, enables the
4218 * PCI device and then calls the block layer initialization function.
4221 * 0 on success else an error code.
4223 static int mtip_pci_probe(struct pci_dev
*pdev
,
4224 const struct pci_device_id
*ent
)
4227 struct driver_data
*dd
= NULL
;
4229 const struct cpumask
*node_mask
;
4230 int cpu
, i
= 0, j
= 0;
4231 int my_node
= NUMA_NO_NODE
;
4232 unsigned long flags
;
4234 /* Allocate memory for this devices private data. */
4235 my_node
= pcibus_to_node(pdev
->bus
);
4236 if (my_node
!= NUMA_NO_NODE
) {
4237 if (!node_online(my_node
))
4238 my_node
= mtip_get_next_rr_node();
4240 dev_info(&pdev
->dev
, "Kernel not reporting proximity, choosing a node\n");
4241 my_node
= mtip_get_next_rr_node();
4243 dev_info(&pdev
->dev
, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4244 my_node
, pcibus_to_node(pdev
->bus
), dev_to_node(&pdev
->dev
),
4245 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
4247 dd
= kzalloc_node(sizeof(struct driver_data
), GFP_KERNEL
, my_node
);
4250 "Unable to allocate memory for driver data\n");
4254 /* Attach the private data to this PCI device. */
4255 pci_set_drvdata(pdev
, dd
);
4257 rv
= pcim_enable_device(pdev
);
4259 dev_err(&pdev
->dev
, "Unable to enable device\n");
4263 /* Map BAR5 to memory. */
4264 rv
= pcim_iomap_regions(pdev
, 1 << MTIP_ABAR
, MTIP_DRV_NAME
);
4266 dev_err(&pdev
->dev
, "Unable to map regions\n");
4270 if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(64))) {
4271 rv
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
4274 rv
= pci_set_consistent_dma_mask(pdev
,
4277 dev_warn(&pdev
->dev
,
4278 "64-bit DMA enable failed\n");
4284 /* Copy the info we may need later into the private data structure. */
4285 dd
->major
= mtip_major
;
4286 dd
->instance
= instance
;
4288 dd
->numa_node
= my_node
;
4290 INIT_LIST_HEAD(&dd
->online_list
);
4291 INIT_LIST_HEAD(&dd
->remove_list
);
4293 memset(dd
->workq_name
, 0, 32);
4294 snprintf(dd
->workq_name
, 31, "mtipq%d", dd
->instance
);
4296 dd
->isr_workq
= create_workqueue(dd
->workq_name
);
4297 if (!dd
->isr_workq
) {
4298 dev_warn(&pdev
->dev
, "Can't create wq %d\n", dd
->instance
);
4300 goto block_initialize_err
;
4303 memset(cpu_list
, 0, sizeof(cpu_list
));
4305 node_mask
= cpumask_of_node(dd
->numa_node
);
4306 if (!cpumask_empty(node_mask
)) {
4307 for_each_cpu(cpu
, node_mask
)
4309 snprintf(&cpu_list
[j
], 256 - j
, "%d ", cpu
);
4310 j
= strlen(cpu_list
);
4313 dev_info(&pdev
->dev
, "Node %d on package %d has %d cpu(s): %s\n",
4315 topology_physical_package_id(cpumask_first(node_mask
)),
4316 nr_cpus_node(dd
->numa_node
),
4319 dev_dbg(&pdev
->dev
, "mtip32xx: node_mask empty\n");
4321 dd
->isr_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4322 dev_info(&pdev
->dev
, "Initial IRQ binding node:cpu %d:%d\n",
4323 cpu_to_node(dd
->isr_binding
), dd
->isr_binding
);
4325 /* first worker context always runs in ISR */
4326 dd
->work
[0].cpu_binding
= dd
->isr_binding
;
4327 dd
->work
[1].cpu_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4328 dd
->work
[2].cpu_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4329 dd
->work
[3].cpu_binding
= dd
->work
[0].cpu_binding
;
4330 dd
->work
[4].cpu_binding
= dd
->work
[1].cpu_binding
;
4331 dd
->work
[5].cpu_binding
= dd
->work
[2].cpu_binding
;
4332 dd
->work
[6].cpu_binding
= dd
->work
[2].cpu_binding
;
4333 dd
->work
[7].cpu_binding
= dd
->work
[1].cpu_binding
;
4335 /* Log the bindings */
4336 for_each_present_cpu(cpu
) {
4337 memset(cpu_list
, 0, sizeof(cpu_list
));
4338 for (i
= 0, j
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++) {
4339 if (dd
->work
[i
].cpu_binding
== cpu
) {
4340 snprintf(&cpu_list
[j
], 256 - j
, "%d ", i
);
4341 j
= strlen(cpu_list
);
4345 dev_info(&pdev
->dev
, "CPU %d: WQs %s\n", cpu
, cpu_list
);
4348 INIT_WORK(&dd
->work
[0].work
, mtip_workq_sdbf0
);
4349 INIT_WORK(&dd
->work
[1].work
, mtip_workq_sdbf1
);
4350 INIT_WORK(&dd
->work
[2].work
, mtip_workq_sdbf2
);
4351 INIT_WORK(&dd
->work
[3].work
, mtip_workq_sdbf3
);
4352 INIT_WORK(&dd
->work
[4].work
, mtip_workq_sdbf4
);
4353 INIT_WORK(&dd
->work
[5].work
, mtip_workq_sdbf5
);
4354 INIT_WORK(&dd
->work
[6].work
, mtip_workq_sdbf6
);
4355 INIT_WORK(&dd
->work
[7].work
, mtip_workq_sdbf7
);
4357 pci_set_master(pdev
);
4358 rv
= pci_enable_msi(pdev
);
4360 dev_warn(&pdev
->dev
,
4361 "Unable to enable MSI interrupt.\n");
4362 goto msi_initialize_err
;
4365 mtip_fix_ero_nosnoop(dd
, pdev
);
4367 /* Initialize the block layer. */
4368 rv
= mtip_block_initialize(dd
);
4371 "Unable to initialize block layer\n");
4372 goto block_initialize_err
;
4376 * Increment the instance count so that each device has a unique
4380 if (rv
!= MTIP_FTL_REBUILD_MAGIC
)
4381 set_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
);
4383 rv
= 0; /* device in rebuild state, return 0 from probe */
4385 /* Add to online list even if in ftl rebuild */
4386 spin_lock_irqsave(&dev_lock
, flags
);
4387 list_add(&dd
->online_list
, &online_list
);
4388 spin_unlock_irqrestore(&dev_lock
, flags
);
4392 block_initialize_err
:
4393 pci_disable_msi(pdev
);
4396 if (dd
->isr_workq
) {
4397 flush_workqueue(dd
->isr_workq
);
4398 destroy_workqueue(dd
->isr_workq
);
4399 drop_cpu(dd
->work
[0].cpu_binding
);
4400 drop_cpu(dd
->work
[1].cpu_binding
);
4401 drop_cpu(dd
->work
[2].cpu_binding
);
4404 pcim_iounmap_regions(pdev
, 1 << MTIP_ABAR
);
4408 pci_set_drvdata(pdev
, NULL
);
4415 * Called for each probed device when the device is removed or the
4416 * driver is unloaded.
4421 static void mtip_pci_remove(struct pci_dev
*pdev
)
4423 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4424 unsigned long flags
, to
;
4426 set_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
);
4428 spin_lock_irqsave(&dev_lock
, flags
);
4429 list_del_init(&dd
->online_list
);
4430 list_add(&dd
->remove_list
, &removing_list
);
4431 spin_unlock_irqrestore(&dev_lock
, flags
);
4433 mtip_check_surprise_removal(pdev
);
4434 synchronize_irq(dd
->pdev
->irq
);
4436 /* Spin until workers are done */
4437 to
= jiffies
+ msecs_to_jiffies(4000);
4440 } while (atomic_read(&dd
->irq_workers_active
) != 0 &&
4441 time_before(jiffies
, to
));
4443 if (atomic_read(&dd
->irq_workers_active
) != 0) {
4444 dev_warn(&dd
->pdev
->dev
,
4445 "Completion workers still active!\n");
4448 blk_mq_stop_hw_queues(dd
->queue
);
4449 /* Clean up the block layer. */
4450 mtip_block_remove(dd
);
4452 if (dd
->isr_workq
) {
4453 flush_workqueue(dd
->isr_workq
);
4454 destroy_workqueue(dd
->isr_workq
);
4455 drop_cpu(dd
->work
[0].cpu_binding
);
4456 drop_cpu(dd
->work
[1].cpu_binding
);
4457 drop_cpu(dd
->work
[2].cpu_binding
);
4460 pci_disable_msi(pdev
);
4462 spin_lock_irqsave(&dev_lock
, flags
);
4463 list_del_init(&dd
->remove_list
);
4464 spin_unlock_irqrestore(&dev_lock
, flags
);
4468 pcim_iounmap_regions(pdev
, 1 << MTIP_ABAR
);
4469 pci_set_drvdata(pdev
, NULL
);
4473 * Called for each probed device when the device is suspended.
4479 static int mtip_pci_suspend(struct pci_dev
*pdev
, pm_message_t mesg
)
4482 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4486 "Driver private datastructure is NULL\n");
4490 set_bit(MTIP_DDF_RESUME_BIT
, &dd
->dd_flag
);
4492 /* Disable ports & interrupts then send standby immediate */
4493 rv
= mtip_block_suspend(dd
);
4496 "Failed to suspend controller\n");
4501 * Save the pci config space to pdev structure &
4502 * disable the device
4504 pci_save_state(pdev
);
4505 pci_disable_device(pdev
);
4507 /* Move to Low power state*/
4508 pci_set_power_state(pdev
, PCI_D3hot
);
4514 * Called for each probed device when the device is resumed.
4520 static int mtip_pci_resume(struct pci_dev
*pdev
)
4523 struct driver_data
*dd
;
4525 dd
= pci_get_drvdata(pdev
);
4528 "Driver private datastructure is NULL\n");
4532 /* Move the device to active State */
4533 pci_set_power_state(pdev
, PCI_D0
);
4535 /* Restore PCI configuration space */
4536 pci_restore_state(pdev
);
4538 /* Enable the PCI device*/
4539 rv
= pcim_enable_device(pdev
);
4542 "Failed to enable card during resume\n");
4545 pci_set_master(pdev
);
4548 * Calls hbaReset, initPort, & startPort function
4549 * then enables interrupts
4551 rv
= mtip_block_resume(dd
);
4553 dev_err(&pdev
->dev
, "Unable to resume\n");
4556 clear_bit(MTIP_DDF_RESUME_BIT
, &dd
->dd_flag
);
4567 static void mtip_pci_shutdown(struct pci_dev
*pdev
)
4569 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4571 mtip_block_shutdown(dd
);
4574 /* Table of device ids supported by this driver. */
4575 static const struct pci_device_id mtip_pci_tbl
[] = {
4576 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320H_DEVICE_ID
) },
4577 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320M_DEVICE_ID
) },
4578 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320S_DEVICE_ID
) },
4579 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P325M_DEVICE_ID
) },
4580 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P420H_DEVICE_ID
) },
4581 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P420M_DEVICE_ID
) },
4582 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P425M_DEVICE_ID
) },
4586 /* Structure that describes the PCI driver functions. */
4587 static struct pci_driver mtip_pci_driver
= {
4588 .name
= MTIP_DRV_NAME
,
4589 .id_table
= mtip_pci_tbl
,
4590 .probe
= mtip_pci_probe
,
4591 .remove
= mtip_pci_remove
,
4592 .suspend
= mtip_pci_suspend
,
4593 .resume
= mtip_pci_resume
,
4594 .shutdown
= mtip_pci_shutdown
,
4597 MODULE_DEVICE_TABLE(pci
, mtip_pci_tbl
);
4600 * Module initialization function.
4602 * Called once when the module is loaded. This function allocates a major
4603 * block device number to the Cyclone devices and registers the PCI layer
4607 * 0 on success else error code.
4609 static int __init
mtip_init(void)
4613 pr_info(MTIP_DRV_NAME
" Version " MTIP_DRV_VERSION
"\n");
4615 spin_lock_init(&dev_lock
);
4617 INIT_LIST_HEAD(&online_list
);
4618 INIT_LIST_HEAD(&removing_list
);
4620 /* Allocate a major block device number to use with this driver. */
4621 error
= register_blkdev(0, MTIP_DRV_NAME
);
4623 pr_err("Unable to register block device (%d)\n",
4629 dfs_parent
= debugfs_create_dir("rssd", NULL
);
4630 if (IS_ERR_OR_NULL(dfs_parent
)) {
4631 pr_warn("Error creating debugfs parent\n");
4635 dfs_device_status
= debugfs_create_file("device_status",
4636 S_IRUGO
, dfs_parent
, NULL
,
4637 &mtip_device_status_fops
);
4638 if (IS_ERR_OR_NULL(dfs_device_status
)) {
4639 pr_err("Error creating device_status node\n");
4640 dfs_device_status
= NULL
;
4644 /* Register our PCI operations. */
4645 error
= pci_register_driver(&mtip_pci_driver
);
4647 debugfs_remove(dfs_parent
);
4648 unregister_blkdev(mtip_major
, MTIP_DRV_NAME
);
4655 * Module de-initialization function.
4657 * Called once when the module is unloaded. This function deallocates
4658 * the major block device number allocated by mtip_init() and
4659 * unregisters the PCI layer of the driver.
4664 static void __exit
mtip_exit(void)
4666 /* Release the allocated major block device number. */
4667 unregister_blkdev(mtip_major
, MTIP_DRV_NAME
);
4669 /* Unregister the PCI driver. */
4670 pci_unregister_driver(&mtip_pci_driver
);
4672 debugfs_remove_recursive(dfs_parent
);
4675 MODULE_AUTHOR("Micron Technology, Inc");
4676 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4677 MODULE_LICENSE("GPL");
4678 MODULE_VERSION(MTIP_DRV_VERSION
);
4680 module_init(mtip_init
);
4681 module_exit(mtip_exit
);