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 if (mtip_check_surprise_removal(dd
->pdev
))
179 rq
= blk_mq_alloc_request(dd
->queue
, 0, BLK_MQ_REQ_RESERVED
);
183 return blk_mq_rq_to_pdu(rq
);
186 static void mtip_put_int_command(struct driver_data
*dd
, struct mtip_cmd
*cmd
)
188 blk_put_request(blk_mq_rq_from_pdu(cmd
));
192 * Once we add support for one hctx per mtip group, this will change a bit
194 static struct request
*mtip_rq_from_tag(struct driver_data
*dd
,
197 struct blk_mq_hw_ctx
*hctx
= dd
->queue
->queue_hw_ctx
[0];
199 return blk_mq_tag_to_rq(hctx
->tags
, tag
);
202 static struct mtip_cmd
*mtip_cmd_from_tag(struct driver_data
*dd
,
205 struct request
*rq
= mtip_rq_from_tag(dd
, tag
);
207 return blk_mq_rq_to_pdu(rq
);
211 * IO completion function.
213 * This completion function is called by the driver ISR when a
214 * command that was issued by the kernel completes. It first calls the
215 * asynchronous completion function which normally calls back into the block
216 * layer passing the asynchronous callback data, then unmaps the
217 * scatter list associated with the completed command, and finally
218 * clears the allocated bit associated with the completed command.
220 * @port Pointer to the port data structure.
221 * @tag Tag of the command.
222 * @data Pointer to driver_data.
223 * @status Completion status.
228 static void mtip_async_complete(struct mtip_port
*port
,
229 int tag
, struct mtip_cmd
*cmd
, int status
)
231 struct driver_data
*dd
= port
->dd
;
234 if (unlikely(!dd
) || unlikely(!port
))
237 if (unlikely(status
== PORT_IRQ_TF_ERR
)) {
238 dev_warn(&port
->dd
->pdev
->dev
,
239 "Command tag %d failed due to TFE\n", tag
);
242 rq
= mtip_rq_from_tag(dd
, tag
);
244 blk_mq_complete_request(rq
, status
);
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
);
584 command
->comp_func
= NULL
;
585 command
->comp_data
= NULL
;
589 static void mtip_null_completion(struct mtip_port
*port
,
590 int tag
, struct mtip_cmd
*command
, int status
)
594 static int mtip_read_log_page(struct mtip_port
*port
, u8 page
, u16
*buffer
,
595 dma_addr_t buffer_dma
, unsigned int sectors
);
596 static int mtip_get_smart_attr(struct mtip_port
*port
, unsigned int id
,
597 struct smart_attr
*attrib
);
601 * @dd Pointer to the DRIVER_DATA structure.
606 static void mtip_handle_tfe(struct driver_data
*dd
)
608 int group
, tag
, bit
, reissue
, rv
;
609 struct mtip_port
*port
;
610 struct mtip_cmd
*cmd
;
612 struct host_to_dev_fis
*fis
;
613 unsigned long tagaccum
[SLOTBITS_IN_LONGS
];
614 unsigned int cmd_cnt
= 0;
616 char *fail_reason
= NULL
;
617 int fail_all_ncq_write
= 0, fail_all_ncq_cmds
= 0;
619 dev_warn(&dd
->pdev
->dev
, "Taskfile error\n");
623 if (test_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
)) {
624 cmd
= mtip_cmd_from_tag(dd
, MTIP_TAG_INTERNAL
);
625 dbg_printk(MTIP_DRV_NAME
" TFE for the internal command\n");
627 if (cmd
->comp_data
&& cmd
->comp_func
) {
628 cmd
->comp_func(port
, MTIP_TAG_INTERNAL
,
629 cmd
, PORT_IRQ_TF_ERR
);
634 /* clear the tag accumulator */
635 memset(tagaccum
, 0, SLOTBITS_IN_LONGS
* sizeof(long));
637 /* Loop through all the groups */
638 for (group
= 0; group
< dd
->slot_groups
; group
++) {
639 completed
= readl(port
->completed
[group
]);
641 dev_warn(&dd
->pdev
->dev
, "g=%u, comp=%x\n", group
, completed
);
643 /* clear completed status register in the hardware.*/
644 writel(completed
, port
->completed
[group
]);
646 /* Process successfully completed commands */
647 for (bit
= 0; bit
< 32 && completed
; bit
++) {
648 if (!(completed
& (1<<bit
)))
650 tag
= (group
<< 5) + bit
;
652 /* Skip the internal command slot */
653 if (tag
== MTIP_TAG_INTERNAL
)
656 cmd
= mtip_cmd_from_tag(dd
, tag
);
657 if (likely(cmd
->comp_func
)) {
658 set_bit(tag
, tagaccum
);
660 cmd
->comp_func(port
, tag
, cmd
, 0);
662 dev_err(&port
->dd
->pdev
->dev
,
663 "Missing completion func for tag %d",
665 if (mtip_check_surprise_removal(dd
->pdev
)) {
666 /* don't proceed further */
673 print_tags(dd
, "completed (TFE)", tagaccum
, cmd_cnt
);
675 /* Restart the port */
677 mtip_restart_port(port
);
679 /* Trying to determine the cause of the error */
680 rv
= mtip_read_log_page(dd
->port
, ATA_LOG_SATA_NCQ
,
682 dd
->port
->log_buf_dma
, 1);
684 dev_warn(&dd
->pdev
->dev
,
685 "Error in READ LOG EXT (10h) command\n");
686 /* non-critical error, don't fail the load */
688 buf
= (unsigned char *)dd
->port
->log_buf
;
689 if (buf
[259] & 0x1) {
690 dev_info(&dd
->pdev
->dev
,
691 "Write protect bit is set.\n");
692 set_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
);
693 fail_all_ncq_write
= 1;
694 fail_reason
= "write protect";
696 if (buf
[288] == 0xF7) {
697 dev_info(&dd
->pdev
->dev
,
698 "Exceeded Tmax, drive in thermal shutdown.\n");
699 set_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
);
700 fail_all_ncq_cmds
= 1;
701 fail_reason
= "thermal shutdown";
703 if (buf
[288] == 0xBF) {
704 set_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
);
705 dev_info(&dd
->pdev
->dev
,
706 "Drive indicates rebuild has failed. Secure erase required.\n");
707 fail_all_ncq_cmds
= 1;
708 fail_reason
= "rebuild failed";
712 /* clear the tag accumulator */
713 memset(tagaccum
, 0, SLOTBITS_IN_LONGS
* sizeof(long));
715 /* Loop through all the groups */
716 for (group
= 0; group
< dd
->slot_groups
; group
++) {
717 for (bit
= 0; bit
< 32; bit
++) {
719 tag
= (group
<< 5) + bit
;
720 cmd
= mtip_cmd_from_tag(dd
, tag
);
722 fis
= (struct host_to_dev_fis
*)cmd
->command
;
724 /* Should re-issue? */
725 if (tag
== MTIP_TAG_INTERNAL
||
726 fis
->command
== ATA_CMD_SET_FEATURES
)
729 if (fail_all_ncq_cmds
||
730 (fail_all_ncq_write
&&
731 fis
->command
== ATA_CMD_FPDMA_WRITE
)) {
732 dev_warn(&dd
->pdev
->dev
,
733 " Fail: %s w/tag %d [%s].\n",
734 fis
->command
== ATA_CMD_FPDMA_WRITE
?
737 fail_reason
!= NULL
?
738 fail_reason
: "unknown");
739 if (cmd
->comp_func
) {
740 cmd
->comp_func(port
, tag
,
748 * First check if this command has
749 * exceeded its retries.
751 if (reissue
&& (cmd
->retries
-- > 0)) {
753 set_bit(tag
, tagaccum
);
755 /* Re-issue the command. */
756 mtip_issue_ncq_command(port
, tag
);
761 /* Retire a command that will not be reissued */
762 dev_warn(&port
->dd
->pdev
->dev
,
763 "retiring tag %d\n", tag
);
766 cmd
->comp_func(port
, tag
, cmd
, PORT_IRQ_TF_ERR
);
768 dev_warn(&port
->dd
->pdev
->dev
,
769 "Bad completion for tag %d\n",
773 print_tags(dd
, "reissued (TFE)", tagaccum
, cmd_cnt
);
777 * Handle a set device bits interrupt
779 static inline void mtip_workq_sdbfx(struct mtip_port
*port
, int group
,
782 struct driver_data
*dd
= port
->dd
;
784 struct mtip_cmd
*command
;
787 WARN_ON_ONCE(!completed
);
790 /* clear completed status register in the hardware.*/
791 writel(completed
, port
->completed
[group
]);
793 /* Process completed commands. */
794 for (bit
= 0; (bit
< 32) && completed
; bit
++) {
795 if (completed
& 0x01) {
796 tag
= (group
<< 5) | bit
;
798 /* skip internal command slot. */
799 if (unlikely(tag
== MTIP_TAG_INTERNAL
))
802 command
= mtip_cmd_from_tag(dd
, tag
);
803 if (likely(command
->comp_func
))
804 command
->comp_func(port
, tag
, command
, 0);
806 dev_dbg(&dd
->pdev
->dev
,
807 "Null completion for tag %d",
810 if (mtip_check_surprise_removal(
819 /* If last, re-enable interrupts */
820 if (atomic_dec_return(&dd
->irq_workers_active
) == 0)
821 writel(0xffffffff, dd
->mmio
+ HOST_IRQ_STAT
);
825 * Process legacy pio and d2h interrupts
827 static inline void mtip_process_legacy(struct driver_data
*dd
, u32 port_stat
)
829 struct mtip_port
*port
= dd
->port
;
830 struct mtip_cmd
*cmd
= mtip_cmd_from_tag(dd
, MTIP_TAG_INTERNAL
);
832 if (test_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
) &&
833 (cmd
!= NULL
) && !(readl(port
->cmd_issue
[MTIP_TAG_INTERNAL
])
834 & (1 << MTIP_TAG_INTERNAL
))) {
835 if (cmd
->comp_func
) {
836 cmd
->comp_func(port
, MTIP_TAG_INTERNAL
, cmd
, 0);
845 * Demux and handle errors
847 static inline void mtip_process_errors(struct driver_data
*dd
, u32 port_stat
)
850 if (unlikely(port_stat
& PORT_IRQ_CONNECT
)) {
851 dev_warn(&dd
->pdev
->dev
,
852 "Clearing PxSERR.DIAG.x\n");
853 writel((1 << 26), dd
->port
->mmio
+ PORT_SCR_ERR
);
856 if (unlikely(port_stat
& PORT_IRQ_PHYRDY
)) {
857 dev_warn(&dd
->pdev
->dev
,
858 "Clearing PxSERR.DIAG.n\n");
859 writel((1 << 16), dd
->port
->mmio
+ PORT_SCR_ERR
);
862 if (unlikely(port_stat
& ~PORT_IRQ_HANDLED
)) {
863 dev_warn(&dd
->pdev
->dev
,
864 "Port stat errors %x unhandled\n",
865 (port_stat
& ~PORT_IRQ_HANDLED
));
866 if (mtip_check_surprise_removal(dd
->pdev
))
869 if (likely(port_stat
& (PORT_IRQ_TF_ERR
| PORT_IRQ_IF_ERR
))) {
870 set_bit(MTIP_PF_EH_ACTIVE_BIT
, &dd
->port
->flags
);
871 wake_up_interruptible(&dd
->port
->svc_wait
);
875 static inline irqreturn_t
mtip_handle_irq(struct driver_data
*data
)
877 struct driver_data
*dd
= (struct driver_data
*) data
;
878 struct mtip_port
*port
= dd
->port
;
879 u32 hba_stat
, port_stat
;
881 int do_irq_enable
= 1, i
, workers
;
882 struct mtip_work
*twork
;
884 hba_stat
= readl(dd
->mmio
+ HOST_IRQ_STAT
);
888 /* Acknowledge the interrupt status on the port.*/
889 port_stat
= readl(port
->mmio
+ PORT_IRQ_STAT
);
890 if (unlikely(port_stat
== 0xFFFFFFFF)) {
891 mtip_check_surprise_removal(dd
->pdev
);
894 writel(port_stat
, port
->mmio
+ PORT_IRQ_STAT
);
896 /* Demux port status */
897 if (likely(port_stat
& PORT_IRQ_SDB_FIS
)) {
899 WARN_ON_ONCE(atomic_read(&dd
->irq_workers_active
) != 0);
901 /* Start at 1: group zero is always local? */
902 for (i
= 0, workers
= 0; i
< MTIP_MAX_SLOT_GROUPS
;
904 twork
= &dd
->work
[i
];
905 twork
->completed
= readl(port
->completed
[i
]);
906 if (twork
->completed
)
910 atomic_set(&dd
->irq_workers_active
, workers
);
912 for (i
= 1; i
< MTIP_MAX_SLOT_GROUPS
; i
++) {
913 twork
= &dd
->work
[i
];
914 if (twork
->completed
)
921 if (likely(dd
->work
[0].completed
))
922 mtip_workq_sdbfx(port
, 0,
923 dd
->work
[0].completed
);
927 * Chip quirk: SDB interrupt but nothing
934 if (unlikely(port_stat
& PORT_IRQ_ERR
)) {
935 if (unlikely(mtip_check_surprise_removal(dd
->pdev
))) {
936 /* don't proceed further */
939 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
943 mtip_process_errors(dd
, port_stat
& PORT_IRQ_ERR
);
946 if (unlikely(port_stat
& PORT_IRQ_LEGACY
))
947 mtip_process_legacy(dd
, port_stat
& PORT_IRQ_LEGACY
);
950 /* acknowledge interrupt */
951 if (unlikely(do_irq_enable
))
952 writel(hba_stat
, dd
->mmio
+ HOST_IRQ_STAT
);
958 * HBA interrupt subroutine.
961 * @instance Pointer to the driver data structure.
964 * IRQ_HANDLED A HBA interrupt was pending and handled.
965 * IRQ_NONE This interrupt was not for the HBA.
967 static irqreturn_t
mtip_irq_handler(int irq
, void *instance
)
969 struct driver_data
*dd
= instance
;
971 return mtip_handle_irq(dd
);
974 static void mtip_issue_non_ncq_command(struct mtip_port
*port
, int tag
)
976 writel(1 << MTIP_TAG_BIT(tag
),
977 port
->cmd_issue
[MTIP_TAG_INDEX(tag
)]);
980 static bool mtip_pause_ncq(struct mtip_port
*port
,
981 struct host_to_dev_fis
*fis
)
983 struct host_to_dev_fis
*reply
;
984 unsigned long task_file_data
;
986 reply
= port
->rxfis
+ RX_FIS_D2H_REG
;
987 task_file_data
= readl(port
->mmio
+PORT_TFDATA
);
989 if ((task_file_data
& 1))
992 if (fis
->command
== ATA_CMD_SEC_ERASE_PREP
) {
993 port
->ic_pause_timer
= jiffies
;
995 } else if ((fis
->command
== ATA_CMD_DOWNLOAD_MICRO
) &&
996 (fis
->features
== 0x03)) {
997 set_bit(MTIP_PF_DM_ACTIVE_BIT
, &port
->flags
);
998 port
->ic_pause_timer
= jiffies
;
1000 } else if ((fis
->command
== ATA_CMD_SEC_ERASE_UNIT
) ||
1001 ((fis
->command
== 0xFC) &&
1002 (fis
->features
== 0x27 || fis
->features
== 0x72 ||
1003 fis
->features
== 0x62 || fis
->features
== 0x26))) {
1004 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
1005 clear_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &port
->dd
->dd_flag
);
1006 /* Com reset after secure erase or lowlevel format */
1007 mtip_restart_port(port
);
1008 clear_bit(MTIP_PF_SE_ACTIVE_BIT
, &port
->flags
);
1016 * Wait for port to quiesce
1018 * @port Pointer to port data structure
1019 * @timeout Max duration to wait (ms)
1020 * @atomic gfp_t flag to indicate blockable context or not
1024 * -EBUSY Commands still active
1026 static int mtip_quiesce_io(struct mtip_port
*port
, unsigned long timeout
,
1031 unsigned int active
= 1;
1033 blk_mq_stop_hw_queues(port
->dd
->queue
);
1035 to
= jiffies
+ msecs_to_jiffies(timeout
);
1037 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
) &&
1038 test_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
) &&
1039 atomic
== GFP_KERNEL
) {
1041 continue; /* svc thd is actively issuing commands */
1044 if (atomic
== GFP_KERNEL
)
1051 if (mtip_check_surprise_removal(port
->dd
->pdev
))
1055 * Ignore s_active bit 0 of array element 0.
1056 * This bit will always be set
1058 active
= readl(port
->s_active
[0]) & 0xFFFFFFFE;
1059 for (n
= 1; n
< port
->dd
->slot_groups
; n
++)
1060 active
|= readl(port
->s_active
[n
]);
1064 } while (time_before(jiffies
, to
));
1066 blk_mq_start_stopped_hw_queues(port
->dd
->queue
, true);
1067 return active
? -EBUSY
: 0;
1069 blk_mq_start_stopped_hw_queues(port
->dd
->queue
, true);
1074 * Execute an internal command and wait for the completion.
1076 * @port Pointer to the port data structure.
1077 * @fis Pointer to the FIS that describes the command.
1078 * @fis_len Length in WORDS of the FIS.
1079 * @buffer DMA accessible for command data.
1080 * @buf_len Length, in bytes, of the data buffer.
1081 * @opts Command header options, excluding the FIS length
1082 * and the number of PRD entries.
1083 * @timeout Time in ms to wait for the command to complete.
1086 * 0 Command completed successfully.
1087 * -EFAULT The buffer address is not correctly aligned.
1088 * -EBUSY Internal command or other IO in progress.
1089 * -EAGAIN Time out waiting for command to complete.
1091 static int mtip_exec_internal_command(struct mtip_port
*port
,
1092 struct host_to_dev_fis
*fis
,
1098 unsigned long timeout
)
1100 struct mtip_cmd_sg
*command_sg
;
1101 DECLARE_COMPLETION_ONSTACK(wait
);
1102 struct mtip_cmd
*int_cmd
;
1103 struct driver_data
*dd
= port
->dd
;
1105 unsigned long start
;
1107 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1108 if (buffer
& 0x00000007) {
1109 dev_err(&dd
->pdev
->dev
, "SG buffer is not 8 byte aligned\n");
1113 int_cmd
= mtip_get_int_command(dd
);
1115 dbg_printk(MTIP_DRV_NAME
"Unable to allocate tag for PIO cmd\n");
1119 set_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1121 if (fis
->command
== ATA_CMD_SEC_ERASE_PREP
)
1122 set_bit(MTIP_PF_SE_ACTIVE_BIT
, &port
->flags
);
1124 clear_bit(MTIP_PF_DM_ACTIVE_BIT
, &port
->flags
);
1126 if (atomic
== GFP_KERNEL
) {
1127 if (fis
->command
!= ATA_CMD_STANDBYNOW1
) {
1128 /* wait for io to complete if non atomic */
1129 if (mtip_quiesce_io(port
,
1130 MTIP_QUIESCE_IO_TIMEOUT_MS
, atomic
) < 0) {
1131 dev_warn(&dd
->pdev
->dev
,
1132 "Failed to quiesce IO\n");
1133 mtip_put_int_command(dd
, int_cmd
);
1134 clear_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1135 wake_up_interruptible(&port
->svc_wait
);
1140 /* Set the completion function and data for the command. */
1141 int_cmd
->comp_data
= &wait
;
1142 int_cmd
->comp_func
= mtip_completion
;
1145 /* Clear completion - we're going to poll */
1146 int_cmd
->comp_data
= NULL
;
1147 int_cmd
->comp_func
= mtip_null_completion
;
1150 /* Copy the command to the command table */
1151 memcpy(int_cmd
->command
, fis
, fis_len
*4);
1153 /* Populate the SG list */
1154 int_cmd
->command_header
->opts
=
1155 __force_bit2int
cpu_to_le32(opts
| fis_len
);
1157 command_sg
= int_cmd
->command
+ AHCI_CMD_TBL_HDR_SZ
;
1160 __force_bit2int
cpu_to_le32((buf_len
-1) & 0x3FFFFF);
1162 __force_bit2int
cpu_to_le32(buffer
& 0xFFFFFFFF);
1163 command_sg
->dba_upper
=
1164 __force_bit2int
cpu_to_le32((buffer
>> 16) >> 16);
1166 int_cmd
->command_header
->opts
|=
1167 __force_bit2int
cpu_to_le32((1 << 16));
1170 /* Populate the command header */
1171 int_cmd
->command_header
->byte_count
= 0;
1175 /* Issue the command to the hardware */
1176 mtip_issue_non_ncq_command(port
, MTIP_TAG_INTERNAL
);
1178 if (atomic
== GFP_KERNEL
) {
1179 /* Wait for the command to complete or timeout. */
1180 if ((rv
= wait_for_completion_interruptible_timeout(
1182 msecs_to_jiffies(timeout
))) <= 0) {
1184 if (rv
== -ERESTARTSYS
) { /* interrupted */
1185 dev_err(&dd
->pdev
->dev
,
1186 "Internal command [%02X] was interrupted after %u ms\n",
1188 jiffies_to_msecs(jiffies
- start
));
1191 } else if (rv
== 0) /* timeout */
1192 dev_err(&dd
->pdev
->dev
,
1193 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1194 fis
->command
, timeout
);
1196 dev_err(&dd
->pdev
->dev
,
1197 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1198 fis
->command
, rv
, timeout
);
1200 if (mtip_check_surprise_removal(dd
->pdev
) ||
1201 test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
1203 dev_err(&dd
->pdev
->dev
,
1204 "Internal command [%02X] wait returned due to SR\n",
1209 mtip_device_reset(dd
); /* recover from timeout issue */
1214 u32 hba_stat
, port_stat
;
1216 /* Spin for <timeout> checking if command still outstanding */
1217 timeout
= jiffies
+ msecs_to_jiffies(timeout
);
1218 while ((readl(port
->cmd_issue
[MTIP_TAG_INTERNAL
])
1219 & (1 << MTIP_TAG_INTERNAL
))
1220 && time_before(jiffies
, timeout
)) {
1221 if (mtip_check_surprise_removal(dd
->pdev
)) {
1225 if ((fis
->command
!= ATA_CMD_STANDBYNOW1
) &&
1226 test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
1231 port_stat
= readl(port
->mmio
+ PORT_IRQ_STAT
);
1235 if (port_stat
& PORT_IRQ_ERR
) {
1236 dev_err(&dd
->pdev
->dev
,
1237 "Internal command [%02X] failed\n",
1239 mtip_device_reset(dd
);
1243 writel(port_stat
, port
->mmio
+ PORT_IRQ_STAT
);
1244 hba_stat
= readl(dd
->mmio
+ HOST_IRQ_STAT
);
1247 dd
->mmio
+ HOST_IRQ_STAT
);
1253 if (readl(port
->cmd_issue
[MTIP_TAG_INTERNAL
])
1254 & (1 << MTIP_TAG_INTERNAL
)) {
1256 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)) {
1257 mtip_device_reset(dd
);
1262 /* Clear the allocated and active bits for the internal command. */
1263 mtip_put_int_command(dd
, int_cmd
);
1264 clear_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1265 if (rv
>= 0 && mtip_pause_ncq(port
, fis
)) {
1269 wake_up_interruptible(&port
->svc_wait
);
1275 * Byte-swap ATA ID strings.
1277 * ATA identify data contains strings in byte-swapped 16-bit words.
1278 * They must be swapped (on all architectures) to be usable as C strings.
1279 * This function swaps bytes in-place.
1281 * @buf The buffer location of the string
1282 * @len The number of bytes to swap
1287 static inline void ata_swap_string(u16
*buf
, unsigned int len
)
1290 for (i
= 0; i
< (len
/2); i
++)
1291 be16_to_cpus(&buf
[i
]);
1294 static void mtip_set_timeout(struct driver_data
*dd
,
1295 struct host_to_dev_fis
*fis
,
1296 unsigned int *timeout
, u8 erasemode
)
1298 switch (fis
->command
) {
1299 case ATA_CMD_DOWNLOAD_MICRO
:
1300 *timeout
= 120000; /* 2 minutes */
1302 case ATA_CMD_SEC_ERASE_UNIT
:
1305 *timeout
= ((*(dd
->port
->identify
+ 90) * 2) * 60000);
1307 *timeout
= ((*(dd
->port
->identify
+ 89) * 2) * 60000);
1309 case ATA_CMD_STANDBYNOW1
:
1310 *timeout
= 120000; /* 2 minutes */
1314 *timeout
= 60000; /* 60 seconds */
1317 *timeout
= 15000; /* 15 seconds */
1320 *timeout
= MTIP_IOCTL_CMD_TIMEOUT_MS
;
1326 * Request the device identity information.
1328 * If a user space buffer is not specified, i.e. is NULL, the
1329 * identify information is still read from the drive and placed
1330 * into the identify data buffer (@e port->identify) in the
1331 * port data structure.
1332 * When the identify buffer contains valid identify information @e
1333 * port->identify_valid is non-zero.
1335 * @port Pointer to the port structure.
1336 * @user_buffer A user space buffer where the identify data should be
1340 * 0 Command completed successfully.
1341 * -EFAULT An error occurred while coping data to the user buffer.
1342 * -1 Command failed.
1344 static int mtip_get_identify(struct mtip_port
*port
, void __user
*user_buffer
)
1347 struct host_to_dev_fis fis
;
1349 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
1352 /* Build the FIS. */
1353 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1356 fis
.command
= ATA_CMD_ID_ATA
;
1358 /* Set the identify information as invalid. */
1359 port
->identify_valid
= 0;
1361 /* Clear the identify information. */
1362 memset(port
->identify
, 0, sizeof(u16
) * ATA_ID_WORDS
);
1364 /* Execute the command. */
1365 if (mtip_exec_internal_command(port
,
1369 sizeof(u16
) * ATA_ID_WORDS
,
1372 MTIP_INT_CMD_TIMEOUT_MS
)
1379 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1380 * perform field-sensitive swapping on the string fields.
1381 * See the kernel use of ata_id_string() for proof of this.
1383 #ifdef __LITTLE_ENDIAN
1384 ata_swap_string(port
->identify
+ 27, 40); /* model string*/
1385 ata_swap_string(port
->identify
+ 23, 8); /* firmware string*/
1386 ata_swap_string(port
->identify
+ 10, 20); /* serial# string*/
1390 for (i
= 0; i
< ATA_ID_WORDS
; i
++)
1391 port
->identify
[i
] = le16_to_cpu(port
->identify
[i
]);
1395 /* Check security locked state */
1396 if (port
->identify
[128] & 0x4)
1397 set_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
1399 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
1401 #ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
1402 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1403 if (port
->identify
[69] & (1 << 14) && port
->identify
[69] & (1 << 5))
1404 port
->dd
->trim_supp
= true;
1407 port
->dd
->trim_supp
= false;
1409 /* Set the identify buffer as valid. */
1410 port
->identify_valid
= 1;
1416 ATA_ID_WORDS
* sizeof(u16
))) {
1427 * Issue a standby immediate command to the device.
1429 * @port Pointer to the port structure.
1432 * 0 Command was executed successfully.
1433 * -1 An error occurred while executing the command.
1435 static int mtip_standby_immediate(struct mtip_port
*port
)
1438 struct host_to_dev_fis fis
;
1439 unsigned long start
;
1440 unsigned int timeout
;
1442 /* Build the FIS. */
1443 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1446 fis
.command
= ATA_CMD_STANDBYNOW1
;
1448 mtip_set_timeout(port
->dd
, &fis
, &timeout
, 0);
1451 rv
= mtip_exec_internal_command(port
,
1459 dbg_printk(MTIP_DRV_NAME
"Time taken to complete standby cmd: %d ms\n",
1460 jiffies_to_msecs(jiffies
- start
));
1462 dev_warn(&port
->dd
->pdev
->dev
,
1463 "STANDBY IMMEDIATE command failed.\n");
1469 * Issue a READ LOG EXT command to the device.
1471 * @port pointer to the port structure.
1472 * @page page number to fetch
1473 * @buffer pointer to buffer
1474 * @buffer_dma dma address corresponding to @buffer
1475 * @sectors page length to fetch, in sectors
1478 * @rv return value from mtip_exec_internal_command()
1480 static int mtip_read_log_page(struct mtip_port
*port
, u8 page
, u16
*buffer
,
1481 dma_addr_t buffer_dma
, unsigned int sectors
)
1483 struct host_to_dev_fis fis
;
1485 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1488 fis
.command
= ATA_CMD_READ_LOG_EXT
;
1489 fis
.sect_count
= sectors
& 0xFF;
1490 fis
.sect_cnt_ex
= (sectors
>> 8) & 0xFF;
1493 fis
.device
= ATA_DEVICE_OBS
;
1495 memset(buffer
, 0, sectors
* ATA_SECT_SIZE
);
1497 return mtip_exec_internal_command(port
,
1501 sectors
* ATA_SECT_SIZE
,
1504 MTIP_INT_CMD_TIMEOUT_MS
);
1508 * Issue a SMART READ DATA command to the device.
1510 * @port pointer to the port structure.
1511 * @buffer pointer to buffer
1512 * @buffer_dma dma address corresponding to @buffer
1515 * @rv return value from mtip_exec_internal_command()
1517 static int mtip_get_smart_data(struct mtip_port
*port
, u8
*buffer
,
1518 dma_addr_t buffer_dma
)
1520 struct host_to_dev_fis fis
;
1522 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1525 fis
.command
= ATA_CMD_SMART
;
1526 fis
.features
= 0xD0;
1530 fis
.device
= ATA_DEVICE_OBS
;
1532 return mtip_exec_internal_command(port
,
1543 * Get the value of a smart attribute
1545 * @port pointer to the port structure
1546 * @id attribute number
1547 * @attrib pointer to return attrib information corresponding to @id
1550 * -EINVAL NULL buffer passed or unsupported attribute @id.
1551 * -EPERM Identify data not valid, SMART not supported or not enabled
1553 static int mtip_get_smart_attr(struct mtip_port
*port
, unsigned int id
,
1554 struct smart_attr
*attrib
)
1557 struct smart_attr
*pattr
;
1562 if (!port
->identify_valid
) {
1563 dev_warn(&port
->dd
->pdev
->dev
, "IDENTIFY DATA not valid\n");
1566 if (!(port
->identify
[82] & 0x1)) {
1567 dev_warn(&port
->dd
->pdev
->dev
, "SMART not supported\n");
1570 if (!(port
->identify
[85] & 0x1)) {
1571 dev_warn(&port
->dd
->pdev
->dev
, "SMART not enabled\n");
1575 memset(port
->smart_buf
, 0, ATA_SECT_SIZE
);
1576 rv
= mtip_get_smart_data(port
, port
->smart_buf
, port
->smart_buf_dma
);
1578 dev_warn(&port
->dd
->pdev
->dev
, "Failed to ge SMART data\n");
1582 pattr
= (struct smart_attr
*)(port
->smart_buf
+ 2);
1583 for (i
= 0; i
< 29; i
++, pattr
++)
1584 if (pattr
->attr_id
== id
) {
1585 memcpy(attrib
, pattr
, sizeof(struct smart_attr
));
1590 dev_warn(&port
->dd
->pdev
->dev
,
1591 "Query for invalid SMART attribute ID\n");
1599 * Trim unused sectors
1601 * @dd pointer to driver_data structure
1603 * @len # of 512b sectors to trim
1606 * -ENOMEM Out of dma memory
1607 * -EINVAL Invalid parameters passed in, trim not supported
1608 * -EIO Error submitting trim request to hw
1610 static int mtip_send_trim(struct driver_data
*dd
, unsigned int lba
,
1614 u64 tlba
, tlen
, sect_left
;
1615 struct mtip_trim_entry
*buf
;
1616 dma_addr_t dma_addr
;
1617 struct host_to_dev_fis fis
;
1619 if (!len
|| dd
->trim_supp
== false)
1622 /* Trim request too big */
1623 WARN_ON(len
> (MTIP_MAX_TRIM_ENTRY_LEN
* MTIP_MAX_TRIM_ENTRIES
));
1625 /* Trim request not aligned on 4k boundary */
1626 WARN_ON(len
% 8 != 0);
1628 /* Warn if vu_trim structure is too big */
1629 WARN_ON(sizeof(struct mtip_trim
) > ATA_SECT_SIZE
);
1631 /* Allocate a DMA buffer for the trim structure */
1632 buf
= dmam_alloc_coherent(&dd
->pdev
->dev
, ATA_SECT_SIZE
, &dma_addr
,
1636 memset(buf
, 0, ATA_SECT_SIZE
);
1638 for (i
= 0, sect_left
= len
, tlba
= lba
;
1639 i
< MTIP_MAX_TRIM_ENTRIES
&& sect_left
;
1641 tlen
= (sect_left
>= MTIP_MAX_TRIM_ENTRY_LEN
?
1642 MTIP_MAX_TRIM_ENTRY_LEN
:
1644 buf
[i
].lba
= __force_bit2int
cpu_to_le32(tlba
);
1645 buf
[i
].range
= __force_bit2int
cpu_to_le16(tlen
);
1649 WARN_ON(sect_left
!= 0);
1652 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1656 fis
.features
= 0x60;
1658 fis
.device
= ATA_DEVICE_OBS
;
1660 if (mtip_exec_internal_command(dd
->port
,
1667 MTIP_TRIM_TIMEOUT_MS
) < 0)
1670 dmam_free_coherent(&dd
->pdev
->dev
, ATA_SECT_SIZE
, buf
, dma_addr
);
1675 * Get the drive capacity.
1677 * @dd Pointer to the device data structure.
1678 * @sectors Pointer to the variable that will receive the sector count.
1681 * 1 Capacity was returned successfully.
1682 * 0 The identify information is invalid.
1684 static bool mtip_hw_get_capacity(struct driver_data
*dd
, sector_t
*sectors
)
1686 struct mtip_port
*port
= dd
->port
;
1687 u64 total
, raw0
, raw1
, raw2
, raw3
;
1688 raw0
= port
->identify
[100];
1689 raw1
= port
->identify
[101];
1690 raw2
= port
->identify
[102];
1691 raw3
= port
->identify
[103];
1692 total
= raw0
| raw1
<<16 | raw2
<<32 | raw3
<<48;
1694 return (bool) !!port
->identify_valid
;
1698 * Display the identify command data.
1700 * @port Pointer to the port data structure.
1705 static void mtip_dump_identify(struct mtip_port
*port
)
1708 unsigned short revid
;
1711 if (!port
->identify_valid
)
1714 strlcpy(cbuf
, (char *)(port
->identify
+10), 21);
1715 dev_info(&port
->dd
->pdev
->dev
,
1716 "Serial No.: %s\n", cbuf
);
1718 strlcpy(cbuf
, (char *)(port
->identify
+23), 9);
1719 dev_info(&port
->dd
->pdev
->dev
,
1720 "Firmware Ver.: %s\n", cbuf
);
1722 strlcpy(cbuf
, (char *)(port
->identify
+27), 41);
1723 dev_info(&port
->dd
->pdev
->dev
, "Model: %s\n", cbuf
);
1725 dev_info(&port
->dd
->pdev
->dev
, "Security: %04x %s\n",
1726 port
->identify
[128],
1727 port
->identify
[128] & 0x4 ? "(LOCKED)" : "");
1729 if (mtip_hw_get_capacity(port
->dd
, §ors
))
1730 dev_info(&port
->dd
->pdev
->dev
,
1731 "Capacity: %llu sectors (%llu MB)\n",
1733 ((u64
)sectors
) * ATA_SECT_SIZE
>> 20);
1735 pci_read_config_word(port
->dd
->pdev
, PCI_REVISION_ID
, &revid
);
1736 switch (revid
& 0xFF) {
1738 strlcpy(cbuf
, "A0", 3);
1741 strlcpy(cbuf
, "A2", 3);
1744 strlcpy(cbuf
, "?", 2);
1747 dev_info(&port
->dd
->pdev
->dev
,
1748 "Card Type: %s\n", cbuf
);
1752 * Map the commands scatter list into the command table.
1754 * @command Pointer to the command.
1755 * @nents Number of scatter list entries.
1760 static inline void fill_command_sg(struct driver_data
*dd
,
1761 struct mtip_cmd
*command
,
1765 unsigned int dma_len
;
1766 struct mtip_cmd_sg
*command_sg
;
1767 struct scatterlist
*sg
= command
->sg
;
1769 command_sg
= command
->command
+ AHCI_CMD_TBL_HDR_SZ
;
1771 for (n
= 0; n
< nents
; n
++) {
1772 dma_len
= sg_dma_len(sg
);
1773 if (dma_len
> 0x400000)
1774 dev_err(&dd
->pdev
->dev
,
1775 "DMA segment length truncated\n");
1776 command_sg
->info
= __force_bit2int
1777 cpu_to_le32((dma_len
-1) & 0x3FFFFF);
1778 command_sg
->dba
= __force_bit2int
1779 cpu_to_le32(sg_dma_address(sg
));
1780 command_sg
->dba_upper
= __force_bit2int
1781 cpu_to_le32((sg_dma_address(sg
) >> 16) >> 16);
1788 * @brief Execute a drive command.
1790 * return value 0 The command completed successfully.
1791 * return value -1 An error occurred while executing the command.
1793 static int exec_drive_task(struct mtip_port
*port
, u8
*command
)
1795 struct host_to_dev_fis fis
;
1796 struct host_to_dev_fis
*reply
= (port
->rxfis
+ RX_FIS_D2H_REG
);
1799 /* Build the FIS. */
1800 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1803 fis
.command
= command
[0];
1804 fis
.features
= command
[1];
1805 fis
.sect_count
= command
[2];
1806 fis
.sector
= command
[3];
1807 fis
.cyl_low
= command
[4];
1808 fis
.cyl_hi
= command
[5];
1809 fis
.device
= command
[6] & ~0x10; /* Clear the dev bit*/
1811 mtip_set_timeout(port
->dd
, &fis
, &to
, 0);
1813 dbg_printk(MTIP_DRV_NAME
" %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
1823 /* Execute the command. */
1824 if (mtip_exec_internal_command(port
,
1835 command
[0] = reply
->command
; /* Status*/
1836 command
[1] = reply
->features
; /* Error*/
1837 command
[4] = reply
->cyl_low
;
1838 command
[5] = reply
->cyl_hi
;
1840 dbg_printk(MTIP_DRV_NAME
" %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
1851 * @brief Execute a drive command.
1853 * @param port Pointer to the port data structure.
1854 * @param command Pointer to the user specified command parameters.
1855 * @param user_buffer Pointer to the user space buffer where read sector
1856 * data should be copied.
1858 * return value 0 The command completed successfully.
1859 * return value -EFAULT An error occurred while copying the completion
1860 * data to the user space buffer.
1861 * return value -1 An error occurred while executing the command.
1863 static int exec_drive_command(struct mtip_port
*port
, u8
*command
,
1864 void __user
*user_buffer
)
1866 struct host_to_dev_fis fis
;
1867 struct host_to_dev_fis
*reply
;
1869 dma_addr_t dma_addr
= 0;
1870 int rv
= 0, xfer_sz
= command
[3];
1877 buf
= dmam_alloc_coherent(&port
->dd
->pdev
->dev
,
1878 ATA_SECT_SIZE
* xfer_sz
,
1882 dev_err(&port
->dd
->pdev
->dev
,
1883 "Memory allocation failed (%d bytes)\n",
1884 ATA_SECT_SIZE
* xfer_sz
);
1887 memset(buf
, 0, ATA_SECT_SIZE
* xfer_sz
);
1890 /* Build the FIS. */
1891 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1894 fis
.command
= command
[0];
1895 fis
.features
= command
[2];
1896 fis
.sect_count
= command
[3];
1897 if (fis
.command
== ATA_CMD_SMART
) {
1898 fis
.sector
= command
[1];
1903 mtip_set_timeout(port
->dd
, &fis
, &to
, 0);
1906 reply
= (port
->rxfis
+ RX_FIS_PIO_SETUP
);
1908 reply
= (port
->rxfis
+ RX_FIS_D2H_REG
);
1910 dbg_printk(MTIP_DRV_NAME
1911 " %s: User Command: cmd %x, sect %x, "
1912 "feat %x, sectcnt %x\n",
1919 /* Execute the command. */
1920 if (mtip_exec_internal_command(port
,
1923 (xfer_sz
? dma_addr
: 0),
1924 (xfer_sz
? ATA_SECT_SIZE
* xfer_sz
: 0),
1930 goto exit_drive_command
;
1933 /* Collect the completion status. */
1934 command
[0] = reply
->command
; /* Status*/
1935 command
[1] = reply
->features
; /* Error*/
1936 command
[2] = reply
->sect_count
;
1938 dbg_printk(MTIP_DRV_NAME
1939 " %s: Completion Status: stat %x, "
1940 "err %x, nsect %x\n",
1947 if (copy_to_user(user_buffer
,
1949 ATA_SECT_SIZE
* command
[3])) {
1951 goto exit_drive_command
;
1956 dmam_free_coherent(&port
->dd
->pdev
->dev
,
1957 ATA_SECT_SIZE
* xfer_sz
, buf
, dma_addr
);
1962 * Indicates whether a command has a single sector payload.
1964 * @command passed to the device to perform the certain event.
1965 * @features passed to the device to perform the certain event.
1968 * 1 command is one that always has a single sector payload,
1969 * regardless of the value in the Sector Count field.
1973 static unsigned int implicit_sector(unsigned char command
,
1974 unsigned char features
)
1976 unsigned int rv
= 0;
1978 /* list of commands that have an implicit sector count of 1 */
1980 case ATA_CMD_SEC_SET_PASS
:
1981 case ATA_CMD_SEC_UNLOCK
:
1982 case ATA_CMD_SEC_ERASE_PREP
:
1983 case ATA_CMD_SEC_ERASE_UNIT
:
1984 case ATA_CMD_SEC_FREEZE_LOCK
:
1985 case ATA_CMD_SEC_DISABLE_PASS
:
1986 case ATA_CMD_PMP_READ
:
1987 case ATA_CMD_PMP_WRITE
:
1990 case ATA_CMD_SET_MAX
:
1991 if (features
== ATA_SET_MAX_UNLOCK
)
1995 if ((features
== ATA_SMART_READ_VALUES
) ||
1996 (features
== ATA_SMART_READ_THRESHOLDS
))
1999 case ATA_CMD_CONF_OVERLAY
:
2000 if ((features
== ATA_DCO_IDENTIFY
) ||
2001 (features
== ATA_DCO_SET
))
2009 * Executes a taskfile
2010 * See ide_taskfile_ioctl() for derivation
2012 static int exec_drive_taskfile(struct driver_data
*dd
,
2014 ide_task_request_t
*req_task
,
2017 struct host_to_dev_fis fis
;
2018 struct host_to_dev_fis
*reply
;
2021 dma_addr_t outbuf_dma
= 0;
2022 dma_addr_t inbuf_dma
= 0;
2023 dma_addr_t dma_buffer
= 0;
2025 unsigned int taskin
= 0;
2026 unsigned int taskout
= 0;
2028 unsigned int timeout
;
2029 unsigned int force_single_sector
;
2030 unsigned int transfer_size
;
2031 unsigned long task_file_data
;
2032 int intotal
= outtotal
+ req_task
->out_size
;
2035 taskout
= req_task
->out_size
;
2036 taskin
= req_task
->in_size
;
2037 /* 130560 = 512 * 0xFF*/
2038 if (taskin
> 130560 || taskout
> 130560)
2042 outbuf
= memdup_user(buf
+ outtotal
, taskout
);
2044 return PTR_ERR(outbuf
);
2046 outbuf_dma
= pci_map_single(dd
->pdev
,
2050 if (pci_dma_mapping_error(dd
->pdev
, outbuf_dma
)) {
2054 dma_buffer
= outbuf_dma
;
2058 inbuf
= memdup_user(buf
+ intotal
, taskin
);
2059 if (IS_ERR(inbuf
)) {
2060 err
= PTR_ERR(inbuf
);
2064 inbuf_dma
= pci_map_single(dd
->pdev
,
2066 taskin
, DMA_FROM_DEVICE
);
2067 if (pci_dma_mapping_error(dd
->pdev
, inbuf_dma
)) {
2071 dma_buffer
= inbuf_dma
;
2074 /* only supports PIO and non-data commands from this ioctl. */
2075 switch (req_task
->data_phase
) {
2077 nsect
= taskout
/ ATA_SECT_SIZE
;
2078 reply
= (dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
);
2081 reply
= (dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
);
2083 case TASKFILE_NO_DATA
:
2084 reply
= (dd
->port
->rxfis
+ RX_FIS_D2H_REG
);
2091 /* Build the FIS. */
2092 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
2096 fis
.command
= req_task
->io_ports
[7];
2097 fis
.features
= req_task
->io_ports
[1];
2098 fis
.sect_count
= req_task
->io_ports
[2];
2099 fis
.lba_low
= req_task
->io_ports
[3];
2100 fis
.lba_mid
= req_task
->io_ports
[4];
2101 fis
.lba_hi
= req_task
->io_ports
[5];
2102 /* Clear the dev bit*/
2103 fis
.device
= req_task
->io_ports
[6] & ~0x10;
2105 if ((req_task
->in_flags
.all
== 0) && (req_task
->out_flags
.all
& 1)) {
2106 req_task
->in_flags
.all
=
2107 IDE_TASKFILE_STD_IN_FLAGS
|
2108 (IDE_HOB_STD_IN_FLAGS
<< 8);
2109 fis
.lba_low_ex
= req_task
->hob_ports
[3];
2110 fis
.lba_mid_ex
= req_task
->hob_ports
[4];
2111 fis
.lba_hi_ex
= req_task
->hob_ports
[5];
2112 fis
.features_ex
= req_task
->hob_ports
[1];
2113 fis
.sect_cnt_ex
= req_task
->hob_ports
[2];
2116 req_task
->in_flags
.all
= IDE_TASKFILE_STD_IN_FLAGS
;
2119 force_single_sector
= implicit_sector(fis
.command
, fis
.features
);
2121 if ((taskin
|| taskout
) && (!fis
.sect_count
)) {
2123 fis
.sect_count
= nsect
;
2125 if (!force_single_sector
) {
2126 dev_warn(&dd
->pdev
->dev
,
2127 "data movement but "
2128 "sect_count is 0\n");
2135 dbg_printk(MTIP_DRV_NAME
2136 " %s: cmd %x, feat %x, nsect %x,"
2137 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2148 /* check for erase mode support during secure erase.*/
2149 if ((fis
.command
== ATA_CMD_SEC_ERASE_UNIT
) && outbuf
&&
2150 (outbuf
[0] & MTIP_SEC_ERASE_MODE
)) {
2154 mtip_set_timeout(dd
, &fis
, &timeout
, erasemode
);
2156 /* Determine the correct transfer size.*/
2157 if (force_single_sector
)
2158 transfer_size
= ATA_SECT_SIZE
;
2160 transfer_size
= ATA_SECT_SIZE
* fis
.sect_count
;
2162 /* Execute the command.*/
2163 if (mtip_exec_internal_command(dd
->port
,
2175 task_file_data
= readl(dd
->port
->mmio
+PORT_TFDATA
);
2177 if ((req_task
->data_phase
== TASKFILE_IN
) && !(task_file_data
& 1)) {
2178 reply
= dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
;
2179 req_task
->io_ports
[7] = reply
->control
;
2181 reply
= dd
->port
->rxfis
+ RX_FIS_D2H_REG
;
2182 req_task
->io_ports
[7] = reply
->command
;
2185 /* reclaim the DMA buffers.*/
2187 pci_unmap_single(dd
->pdev
, inbuf_dma
,
2188 taskin
, DMA_FROM_DEVICE
);
2190 pci_unmap_single(dd
->pdev
, outbuf_dma
,
2191 taskout
, DMA_TO_DEVICE
);
2195 /* return the ATA registers to the caller.*/
2196 req_task
->io_ports
[1] = reply
->features
;
2197 req_task
->io_ports
[2] = reply
->sect_count
;
2198 req_task
->io_ports
[3] = reply
->lba_low
;
2199 req_task
->io_ports
[4] = reply
->lba_mid
;
2200 req_task
->io_ports
[5] = reply
->lba_hi
;
2201 req_task
->io_ports
[6] = reply
->device
;
2203 if (req_task
->out_flags
.all
& 1) {
2205 req_task
->hob_ports
[3] = reply
->lba_low_ex
;
2206 req_task
->hob_ports
[4] = reply
->lba_mid_ex
;
2207 req_task
->hob_ports
[5] = reply
->lba_hi_ex
;
2208 req_task
->hob_ports
[1] = reply
->features_ex
;
2209 req_task
->hob_ports
[2] = reply
->sect_cnt_ex
;
2211 dbg_printk(MTIP_DRV_NAME
2212 " %s: Completion: stat %x,"
2213 "err %x, sect_cnt %x, lbalo %x,"
2214 "lbamid %x, lbahi %x, dev %x\n",
2216 req_task
->io_ports
[7],
2217 req_task
->io_ports
[1],
2218 req_task
->io_ports
[2],
2219 req_task
->io_ports
[3],
2220 req_task
->io_ports
[4],
2221 req_task
->io_ports
[5],
2222 req_task
->io_ports
[6]);
2225 if (copy_to_user(buf
+ outtotal
, outbuf
, taskout
)) {
2231 if (copy_to_user(buf
+ intotal
, inbuf
, taskin
)) {
2238 pci_unmap_single(dd
->pdev
, inbuf_dma
,
2239 taskin
, DMA_FROM_DEVICE
);
2241 pci_unmap_single(dd
->pdev
, outbuf_dma
,
2242 taskout
, DMA_TO_DEVICE
);
2250 * Handle IOCTL calls from the Block Layer.
2252 * This function is called by the Block Layer when it receives an IOCTL
2253 * command that it does not understand. If the IOCTL command is not supported
2254 * this function returns -ENOTTY.
2256 * @dd Pointer to the driver data structure.
2257 * @cmd IOCTL command passed from the Block Layer.
2258 * @arg IOCTL argument passed from the Block Layer.
2261 * 0 The IOCTL completed successfully.
2262 * -ENOTTY The specified command is not supported.
2263 * -EFAULT An error occurred copying data to a user space buffer.
2264 * -EIO An error occurred while executing the command.
2266 static int mtip_hw_ioctl(struct driver_data
*dd
, unsigned int cmd
,
2270 case HDIO_GET_IDENTITY
:
2272 if (copy_to_user((void __user
*)arg
, dd
->port
->identify
,
2273 sizeof(u16
) * ATA_ID_WORDS
))
2277 case HDIO_DRIVE_CMD
:
2279 u8 drive_command
[4];
2281 /* Copy the user command info to our buffer. */
2282 if (copy_from_user(drive_command
,
2283 (void __user
*) arg
,
2284 sizeof(drive_command
)))
2287 /* Execute the drive command. */
2288 if (exec_drive_command(dd
->port
,
2290 (void __user
*) (arg
+4)))
2293 /* Copy the status back to the users buffer. */
2294 if (copy_to_user((void __user
*) arg
,
2296 sizeof(drive_command
)))
2301 case HDIO_DRIVE_TASK
:
2303 u8 drive_command
[7];
2305 /* Copy the user command info to our buffer. */
2306 if (copy_from_user(drive_command
,
2307 (void __user
*) arg
,
2308 sizeof(drive_command
)))
2311 /* Execute the drive command. */
2312 if (exec_drive_task(dd
->port
, drive_command
))
2315 /* Copy the status back to the users buffer. */
2316 if (copy_to_user((void __user
*) arg
,
2318 sizeof(drive_command
)))
2323 case HDIO_DRIVE_TASKFILE
: {
2324 ide_task_request_t req_task
;
2327 if (copy_from_user(&req_task
, (void __user
*) arg
,
2331 outtotal
= sizeof(req_task
);
2333 ret
= exec_drive_taskfile(dd
, (void __user
*) arg
,
2334 &req_task
, outtotal
);
2336 if (copy_to_user((void __user
*) arg
, &req_task
,
2350 * Submit an IO to the hw
2352 * This function is called by the block layer to issue an io
2353 * to the device. Upon completion, the callback function will
2354 * be called with the data parameter passed as the callback data.
2356 * @dd Pointer to the driver data structure.
2357 * @start First sector to read.
2358 * @nsect Number of sectors to read.
2359 * @nents Number of entries in scatter list for the read command.
2360 * @tag The tag of this read command.
2361 * @callback Pointer to the function that should be called
2362 * when the read completes.
2363 * @data Callback data passed to the callback function
2364 * when the read completes.
2365 * @dir Direction (read or write)
2370 static void mtip_hw_submit_io(struct driver_data
*dd
, struct request
*rq
,
2371 struct mtip_cmd
*command
, int nents
,
2372 struct blk_mq_hw_ctx
*hctx
)
2374 struct host_to_dev_fis
*fis
;
2375 struct mtip_port
*port
= dd
->port
;
2376 int dma_dir
= rq_data_dir(rq
) == READ
? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
2377 u64 start
= blk_rq_pos(rq
);
2378 unsigned int nsect
= blk_rq_sectors(rq
);
2380 /* Map the scatter list for DMA access */
2381 nents
= dma_map_sg(&dd
->pdev
->dev
, command
->sg
, nents
, dma_dir
);
2383 prefetch(&port
->flags
);
2385 command
->scatter_ents
= nents
;
2388 * The number of retries for this command before it is
2389 * reported as a failure to the upper layers.
2391 command
->retries
= MTIP_MAX_RETRIES
;
2394 fis
= command
->command
;
2397 if (dma_dir
== DMA_FROM_DEVICE
)
2398 fis
->command
= ATA_CMD_FPDMA_READ
;
2400 fis
->command
= ATA_CMD_FPDMA_WRITE
;
2401 fis
->lba_low
= start
& 0xFF;
2402 fis
->lba_mid
= (start
>> 8) & 0xFF;
2403 fis
->lba_hi
= (start
>> 16) & 0xFF;
2404 fis
->lba_low_ex
= (start
>> 24) & 0xFF;
2405 fis
->lba_mid_ex
= (start
>> 32) & 0xFF;
2406 fis
->lba_hi_ex
= (start
>> 40) & 0xFF;
2407 fis
->device
= 1 << 6;
2408 fis
->features
= nsect
& 0xFF;
2409 fis
->features_ex
= (nsect
>> 8) & 0xFF;
2410 fis
->sect_count
= ((rq
->tag
<< 3) | (rq
->tag
>> 5));
2411 fis
->sect_cnt_ex
= 0;
2415 fill_command_sg(dd
, command
, nents
);
2417 if (unlikely(command
->unaligned
))
2418 fis
->device
|= 1 << 7;
2420 /* Populate the command header */
2421 command
->command_header
->opts
=
2422 __force_bit2int
cpu_to_le32(
2423 (nents
<< 16) | 5 | AHCI_CMD_PREFETCH
);
2424 command
->command_header
->byte_count
= 0;
2427 * Set the completion function and data for the command
2428 * within this layer.
2430 command
->comp_data
= dd
;
2431 command
->comp_func
= mtip_async_complete
;
2432 command
->direction
= dma_dir
;
2435 * To prevent this command from being issued
2436 * if an internal command is in progress or error handling is active.
2438 if (unlikely(port
->flags
& MTIP_PF_PAUSE_IO
)) {
2439 set_bit(rq
->tag
, port
->cmds_to_issue
);
2440 set_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
);
2444 /* Issue the command to the hardware */
2445 mtip_issue_ncq_command(port
, rq
->tag
);
2449 * Sysfs status dump.
2451 * @dev Pointer to the device structure, passed by the kernrel.
2452 * @attr Pointer to the device_attribute structure passed by the kernel.
2453 * @buf Pointer to the char buffer that will receive the stats info.
2456 * The size, in bytes, of the data copied into buf.
2458 static ssize_t
mtip_hw_show_status(struct device
*dev
,
2459 struct device_attribute
*attr
,
2462 struct driver_data
*dd
= dev_to_disk(dev
)->private_data
;
2465 if (test_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
))
2466 size
+= sprintf(buf
, "%s", "thermal_shutdown\n");
2467 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
))
2468 size
+= sprintf(buf
, "%s", "write_protect\n");
2470 size
+= sprintf(buf
, "%s", "online\n");
2475 static DEVICE_ATTR(status
, S_IRUGO
, mtip_hw_show_status
, NULL
);
2477 /* debugsfs entries */
2479 static ssize_t
show_device_status(struct device_driver
*drv
, char *buf
)
2482 struct driver_data
*dd
, *tmp
;
2483 unsigned long flags
;
2487 spin_lock_irqsave(&dev_lock
, flags
);
2488 size
+= sprintf(&buf
[size
], "Devices Present:\n");
2489 list_for_each_entry_safe(dd
, tmp
, &online_list
, online_list
) {
2492 dd
->port
->identify
&&
2493 dd
->port
->identify_valid
) {
2495 (char *) (dd
->port
->identify
+ 10), 21);
2496 status
= *(dd
->port
->identify
+ 141);
2498 memset(id_buf
, 0, 42);
2503 test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
)) {
2504 size
+= sprintf(&buf
[size
],
2505 " device %s %s (ftl rebuild %d %%)\n",
2506 dev_name(&dd
->pdev
->dev
),
2510 size
+= sprintf(&buf
[size
],
2512 dev_name(&dd
->pdev
->dev
),
2518 size
+= sprintf(&buf
[size
], "Devices Being Removed:\n");
2519 list_for_each_entry_safe(dd
, tmp
, &removing_list
, remove_list
) {
2522 dd
->port
->identify
&&
2523 dd
->port
->identify_valid
) {
2525 (char *) (dd
->port
->identify
+10), 21);
2526 status
= *(dd
->port
->identify
+ 141);
2528 memset(id_buf
, 0, 42);
2533 test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
)) {
2534 size
+= sprintf(&buf
[size
],
2535 " device %s %s (ftl rebuild %d %%)\n",
2536 dev_name(&dd
->pdev
->dev
),
2540 size
+= sprintf(&buf
[size
],
2542 dev_name(&dd
->pdev
->dev
),
2547 spin_unlock_irqrestore(&dev_lock
, flags
);
2552 static ssize_t
mtip_hw_read_device_status(struct file
*f
, char __user
*ubuf
,
2553 size_t len
, loff_t
*offset
)
2555 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2560 if (!len
|| *offset
)
2563 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2565 dev_err(&dd
->pdev
->dev
,
2566 "Memory allocation: status buffer\n");
2570 size
+= show_device_status(NULL
, buf
);
2572 *offset
= size
<= len
? size
: len
;
2573 size
= copy_to_user(ubuf
, buf
, *offset
);
2578 return rv
? rv
: *offset
;
2581 static ssize_t
mtip_hw_read_registers(struct file
*f
, char __user
*ubuf
,
2582 size_t len
, loff_t
*offset
)
2584 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2586 u32 group_allocated
;
2593 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2595 dev_err(&dd
->pdev
->dev
,
2596 "Memory allocation: register buffer\n");
2600 size
+= sprintf(&buf
[size
], "H/ S ACTive : [ 0x");
2602 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2603 size
+= sprintf(&buf
[size
], "%08X ",
2604 readl(dd
->port
->s_active
[n
]));
2606 size
+= sprintf(&buf
[size
], "]\n");
2607 size
+= sprintf(&buf
[size
], "H/ Command Issue : [ 0x");
2609 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2610 size
+= sprintf(&buf
[size
], "%08X ",
2611 readl(dd
->port
->cmd_issue
[n
]));
2613 size
+= sprintf(&buf
[size
], "]\n");
2614 size
+= sprintf(&buf
[size
], "H/ Completed : [ 0x");
2616 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2617 size
+= sprintf(&buf
[size
], "%08X ",
2618 readl(dd
->port
->completed
[n
]));
2620 size
+= sprintf(&buf
[size
], "]\n");
2621 size
+= sprintf(&buf
[size
], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2622 readl(dd
->port
->mmio
+ PORT_IRQ_STAT
));
2623 size
+= sprintf(&buf
[size
], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2624 readl(dd
->mmio
+ HOST_IRQ_STAT
));
2625 size
+= sprintf(&buf
[size
], "\n");
2627 size
+= sprintf(&buf
[size
], "L/ Commands in Q : [ 0x");
2629 for (n
= dd
->slot_groups
-1; n
>= 0; n
--) {
2630 if (sizeof(long) > sizeof(u32
))
2632 dd
->port
->cmds_to_issue
[n
/2] >> (32*(n
&1));
2634 group_allocated
= dd
->port
->cmds_to_issue
[n
];
2635 size
+= sprintf(&buf
[size
], "%08X ", group_allocated
);
2637 size
+= sprintf(&buf
[size
], "]\n");
2639 *offset
= size
<= len
? size
: len
;
2640 size
= copy_to_user(ubuf
, buf
, *offset
);
2645 return rv
? rv
: *offset
;
2648 static ssize_t
mtip_hw_read_flags(struct file
*f
, char __user
*ubuf
,
2649 size_t len
, loff_t
*offset
)
2651 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2659 buf
= kzalloc(MTIP_DFS_MAX_BUF_SIZE
, GFP_KERNEL
);
2661 dev_err(&dd
->pdev
->dev
,
2662 "Memory allocation: flag buffer\n");
2666 size
+= sprintf(&buf
[size
], "Flag-port : [ %08lX ]\n",
2668 size
+= sprintf(&buf
[size
], "Flag-dd : [ %08lX ]\n",
2671 *offset
= size
<= len
? size
: len
;
2672 size
= copy_to_user(ubuf
, buf
, *offset
);
2677 return rv
? rv
: *offset
;
2680 static const struct file_operations mtip_device_status_fops
= {
2681 .owner
= THIS_MODULE
,
2682 .open
= simple_open
,
2683 .read
= mtip_hw_read_device_status
,
2684 .llseek
= no_llseek
,
2687 static const struct file_operations mtip_regs_fops
= {
2688 .owner
= THIS_MODULE
,
2689 .open
= simple_open
,
2690 .read
= mtip_hw_read_registers
,
2691 .llseek
= no_llseek
,
2694 static const struct file_operations mtip_flags_fops
= {
2695 .owner
= THIS_MODULE
,
2696 .open
= simple_open
,
2697 .read
= mtip_hw_read_flags
,
2698 .llseek
= no_llseek
,
2702 * Create the sysfs related attributes.
2704 * @dd Pointer to the driver data structure.
2705 * @kobj Pointer to the kobj for the block device.
2708 * 0 Operation completed successfully.
2709 * -EINVAL Invalid parameter.
2711 static int mtip_hw_sysfs_init(struct driver_data
*dd
, struct kobject
*kobj
)
2716 if (sysfs_create_file(kobj
, &dev_attr_status
.attr
))
2717 dev_warn(&dd
->pdev
->dev
,
2718 "Error creating 'status' sysfs entry\n");
2723 * Remove the sysfs related attributes.
2725 * @dd Pointer to the driver data structure.
2726 * @kobj Pointer to the kobj for the block device.
2729 * 0 Operation completed successfully.
2730 * -EINVAL Invalid parameter.
2732 static int mtip_hw_sysfs_exit(struct driver_data
*dd
, struct kobject
*kobj
)
2737 sysfs_remove_file(kobj
, &dev_attr_status
.attr
);
2742 static int mtip_hw_debugfs_init(struct driver_data
*dd
)
2747 dd
->dfs_node
= debugfs_create_dir(dd
->disk
->disk_name
, dfs_parent
);
2748 if (IS_ERR_OR_NULL(dd
->dfs_node
)) {
2749 dev_warn(&dd
->pdev
->dev
,
2750 "Error creating node %s under debugfs\n",
2751 dd
->disk
->disk_name
);
2752 dd
->dfs_node
= NULL
;
2756 debugfs_create_file("flags", S_IRUGO
, dd
->dfs_node
, dd
,
2758 debugfs_create_file("registers", S_IRUGO
, dd
->dfs_node
, dd
,
2764 static void mtip_hw_debugfs_exit(struct driver_data
*dd
)
2767 debugfs_remove_recursive(dd
->dfs_node
);
2771 * Perform any init/resume time hardware setup
2773 * @dd Pointer to the driver data structure.
2778 static inline void hba_setup(struct driver_data
*dd
)
2781 hwdata
= readl(dd
->mmio
+ HOST_HSORG
);
2783 /* interrupt bug workaround: use only 1 IS bit.*/
2785 HSORG_DISABLE_SLOTGRP_INTR
|
2786 HSORG_DISABLE_SLOTGRP_PXIS
,
2787 dd
->mmio
+ HOST_HSORG
);
2790 static int mtip_device_unaligned_constrained(struct driver_data
*dd
)
2792 return (dd
->pdev
->device
== P420M_DEVICE_ID
? 1 : 0);
2796 * Detect the details of the product, and store anything needed
2797 * into the driver data structure. This includes product type and
2798 * version and number of slot groups.
2800 * @dd Pointer to the driver data structure.
2805 static void mtip_detect_product(struct driver_data
*dd
)
2808 unsigned int rev
, slotgroups
;
2811 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2813 * [15:8] hardware/software interface rev#
2814 * [ 3] asic-style interface
2815 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2817 hwdata
= readl(dd
->mmio
+ HOST_HSORG
);
2819 dd
->product_type
= MTIP_PRODUCT_UNKNOWN
;
2820 dd
->slot_groups
= 1;
2823 dd
->product_type
= MTIP_PRODUCT_ASICFPGA
;
2824 rev
= (hwdata
& HSORG_HWREV
) >> 8;
2825 slotgroups
= (hwdata
& HSORG_SLOTGROUPS
) + 1;
2826 dev_info(&dd
->pdev
->dev
,
2827 "ASIC-FPGA design, HS rev 0x%x, "
2828 "%i slot groups [%i slots]\n",
2833 if (slotgroups
> MTIP_MAX_SLOT_GROUPS
) {
2834 dev_warn(&dd
->pdev
->dev
,
2835 "Warning: driver only supports "
2836 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS
);
2837 slotgroups
= MTIP_MAX_SLOT_GROUPS
;
2839 dd
->slot_groups
= slotgroups
;
2843 dev_warn(&dd
->pdev
->dev
, "Unrecognized product id\n");
2847 * Blocking wait for FTL rebuild to complete
2849 * @dd Pointer to the DRIVER_DATA structure.
2852 * 0 FTL rebuild completed successfully
2853 * -EFAULT FTL rebuild error/timeout/interruption
2855 static int mtip_ftl_rebuild_poll(struct driver_data
*dd
)
2857 unsigned long timeout
, cnt
= 0, start
;
2859 dev_warn(&dd
->pdev
->dev
,
2860 "FTL rebuild in progress. Polling for completion.\n");
2863 timeout
= jiffies
+ msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS
);
2866 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
2869 if (mtip_check_surprise_removal(dd
->pdev
))
2872 if (mtip_get_identify(dd
->port
, NULL
) < 0)
2875 if (*(dd
->port
->identify
+ MTIP_FTL_REBUILD_OFFSET
) ==
2876 MTIP_FTL_REBUILD_MAGIC
) {
2878 /* Print message every 3 minutes */
2880 dev_warn(&dd
->pdev
->dev
,
2881 "FTL rebuild in progress (%d secs).\n",
2882 jiffies_to_msecs(jiffies
- start
) / 1000);
2886 dev_warn(&dd
->pdev
->dev
,
2887 "FTL rebuild complete (%d secs).\n",
2888 jiffies_to_msecs(jiffies
- start
) / 1000);
2889 mtip_block_initialize(dd
);
2892 } while (time_before(jiffies
, timeout
));
2894 /* Check for timeout */
2895 dev_err(&dd
->pdev
->dev
,
2896 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2897 jiffies_to_msecs(jiffies
- start
) / 1000);
2901 static void mtip_softirq_done_fn(struct request
*rq
)
2903 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
2904 struct driver_data
*dd
= rq
->q
->queuedata
;
2906 /* Unmap the DMA scatter list entries */
2907 dma_unmap_sg(&dd
->pdev
->dev
, cmd
->sg
, cmd
->scatter_ents
,
2910 if (unlikely(cmd
->unaligned
))
2911 up(&dd
->port
->cmd_slot_unal
);
2913 blk_mq_end_request(rq
, rq
->errors
);
2916 static void mtip_abort_cmd(struct request
*req
, void *data
,
2919 struct driver_data
*dd
= data
;
2921 dbg_printk(MTIP_DRV_NAME
" Aborting request, tag = %d\n", req
->tag
);
2923 clear_bit(req
->tag
, dd
->port
->cmds_to_issue
);
2925 mtip_softirq_done_fn(req
);
2928 static void mtip_queue_cmd(struct request
*req
, void *data
,
2931 struct driver_data
*dd
= data
;
2933 set_bit(req
->tag
, dd
->port
->cmds_to_issue
);
2934 blk_abort_request(req
);
2938 * service thread to issue queued commands
2940 * @data Pointer to the driver data structure.
2946 static int mtip_service_thread(void *data
)
2948 struct driver_data
*dd
= (struct driver_data
*)data
;
2949 unsigned long slot
, slot_start
, slot_wrap
, to
;
2950 unsigned int num_cmd_slots
= dd
->slot_groups
* 32;
2951 struct mtip_port
*port
= dd
->port
;
2954 if (kthread_should_stop() ||
2955 test_bit(MTIP_PF_SVC_THD_STOP_BIT
, &port
->flags
))
2957 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
);
2960 * the condition is to check neither an internal command is
2961 * is in progress nor error handling is active
2963 wait_event_interruptible(port
->svc_wait
, (port
->flags
) &&
2964 (port
->flags
& MTIP_PF_SVC_THD_WORK
));
2966 if (kthread_should_stop() ||
2967 test_bit(MTIP_PF_SVC_THD_STOP_BIT
, &port
->flags
))
2970 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
2974 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
);
2977 /* Demux bits: start with error handling */
2978 if (test_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
)) {
2979 mtip_handle_tfe(dd
);
2980 clear_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
);
2983 if (test_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
))
2986 if (test_bit(MTIP_PF_TO_ACTIVE_BIT
, &port
->flags
)) {
2987 to
= jiffies
+ msecs_to_jiffies(5000);
2991 } while (atomic_read(&dd
->irq_workers_active
) != 0 &&
2992 time_before(jiffies
, to
));
2994 if (atomic_read(&dd
->irq_workers_active
) != 0)
2995 dev_warn(&dd
->pdev
->dev
,
2996 "Completion workers still active!");
2998 spin_lock(dd
->queue
->queue_lock
);
2999 blk_mq_tagset_busy_iter(&dd
->tags
,
3000 mtip_queue_cmd
, dd
);
3001 spin_unlock(dd
->queue
->queue_lock
);
3003 set_bit(MTIP_PF_ISSUE_CMDS_BIT
, &dd
->port
->flags
);
3005 if (mtip_device_reset(dd
))
3006 blk_mq_tagset_busy_iter(&dd
->tags
,
3007 mtip_abort_cmd
, dd
);
3009 clear_bit(MTIP_PF_TO_ACTIVE_BIT
, &dd
->port
->flags
);
3012 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
)) {
3014 /* used to restrict the loop to one iteration */
3015 slot_start
= num_cmd_slots
;
3018 slot
= find_next_bit(port
->cmds_to_issue
,
3019 num_cmd_slots
, slot
);
3020 if (slot_wrap
== 1) {
3021 if ((slot_start
>= slot
) ||
3022 (slot
>= num_cmd_slots
))
3025 if (unlikely(slot_start
== num_cmd_slots
))
3028 if (unlikely(slot
== num_cmd_slots
)) {
3034 /* Issue the command to the hardware */
3035 mtip_issue_ncq_command(port
, slot
);
3037 clear_bit(slot
, port
->cmds_to_issue
);
3040 clear_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
);
3043 if (test_bit(MTIP_PF_REBUILD_BIT
, &port
->flags
)) {
3044 if (mtip_ftl_rebuild_poll(dd
) == 0)
3045 clear_bit(MTIP_PF_REBUILD_BIT
, &port
->flags
);
3054 * DMA region teardown
3056 * @dd Pointer to driver_data structure
3061 static void mtip_dma_free(struct driver_data
*dd
)
3063 struct mtip_port
*port
= dd
->port
;
3066 dmam_free_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
3067 port
->block1
, port
->block1_dma
);
3069 if (port
->command_list
) {
3070 dmam_free_coherent(&dd
->pdev
->dev
, AHCI_CMD_TBL_SZ
,
3071 port
->command_list
, port
->command_list_dma
);
3078 * @dd Pointer to driver_data structure
3081 * -ENOMEM Not enough free DMA region space to initialize driver
3083 static int mtip_dma_alloc(struct driver_data
*dd
)
3085 struct mtip_port
*port
= dd
->port
;
3087 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
3089 dmam_alloc_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
3090 &port
->block1_dma
, GFP_KERNEL
);
3093 memset(port
->block1
, 0, BLOCK_DMA_ALLOC_SZ
);
3095 /* Allocate dma memory for command list */
3096 port
->command_list
=
3097 dmam_alloc_coherent(&dd
->pdev
->dev
, AHCI_CMD_TBL_SZ
,
3098 &port
->command_list_dma
, GFP_KERNEL
);
3099 if (!port
->command_list
) {
3100 dmam_free_coherent(&dd
->pdev
->dev
, BLOCK_DMA_ALLOC_SZ
,
3101 port
->block1
, port
->block1_dma
);
3102 port
->block1
= NULL
;
3103 port
->block1_dma
= 0;
3106 memset(port
->command_list
, 0, AHCI_CMD_TBL_SZ
);
3108 /* Setup all pointers into first DMA region */
3109 port
->rxfis
= port
->block1
+ AHCI_RX_FIS_OFFSET
;
3110 port
->rxfis_dma
= port
->block1_dma
+ AHCI_RX_FIS_OFFSET
;
3111 port
->identify
= port
->block1
+ AHCI_IDFY_OFFSET
;
3112 port
->identify_dma
= port
->block1_dma
+ AHCI_IDFY_OFFSET
;
3113 port
->log_buf
= port
->block1
+ AHCI_SECTBUF_OFFSET
;
3114 port
->log_buf_dma
= port
->block1_dma
+ AHCI_SECTBUF_OFFSET
;
3115 port
->smart_buf
= port
->block1
+ AHCI_SMARTBUF_OFFSET
;
3116 port
->smart_buf_dma
= port
->block1_dma
+ AHCI_SMARTBUF_OFFSET
;
3121 static int mtip_hw_get_identify(struct driver_data
*dd
)
3123 struct smart_attr attr242
;
3127 if (mtip_get_identify(dd
->port
, NULL
) < 0)
3130 if (*(dd
->port
->identify
+ MTIP_FTL_REBUILD_OFFSET
) ==
3131 MTIP_FTL_REBUILD_MAGIC
) {
3132 set_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
);
3133 return MTIP_FTL_REBUILD_MAGIC
;
3135 mtip_dump_identify(dd
->port
);
3137 /* check write protect, over temp and rebuild statuses */
3138 rv
= mtip_read_log_page(dd
->port
, ATA_LOG_SATA_NCQ
,
3140 dd
->port
->log_buf_dma
, 1);
3142 dev_warn(&dd
->pdev
->dev
,
3143 "Error in READ LOG EXT (10h) command\n");
3144 /* non-critical error, don't fail the load */
3146 buf
= (unsigned char *)dd
->port
->log_buf
;
3147 if (buf
[259] & 0x1) {
3148 dev_info(&dd
->pdev
->dev
,
3149 "Write protect bit is set.\n");
3150 set_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
);
3152 if (buf
[288] == 0xF7) {
3153 dev_info(&dd
->pdev
->dev
,
3154 "Exceeded Tmax, drive in thermal shutdown.\n");
3155 set_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
);
3157 if (buf
[288] == 0xBF) {
3158 dev_info(&dd
->pdev
->dev
,
3159 "Drive indicates rebuild has failed.\n");
3160 set_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
);
3164 /* get write protect progess */
3165 memset(&attr242
, 0, sizeof(struct smart_attr
));
3166 if (mtip_get_smart_attr(dd
->port
, 242, &attr242
))
3167 dev_warn(&dd
->pdev
->dev
,
3168 "Unable to check write protect progress\n");
3170 dev_info(&dd
->pdev
->dev
,
3171 "Write protect progress: %u%% (%u blocks)\n",
3172 attr242
.cur
, le32_to_cpu(attr242
.data
));
3178 * Called once for each card.
3180 * @dd Pointer to the driver data structure.
3183 * 0 on success, else an error code.
3185 static int mtip_hw_init(struct driver_data
*dd
)
3189 unsigned int num_command_slots
;
3190 unsigned long timeout
, timetaken
;
3192 dd
->mmio
= pcim_iomap_table(dd
->pdev
)[MTIP_ABAR
];
3194 mtip_detect_product(dd
);
3195 if (dd
->product_type
== MTIP_PRODUCT_UNKNOWN
) {
3199 num_command_slots
= dd
->slot_groups
* 32;
3203 dd
->port
= kzalloc_node(sizeof(struct mtip_port
), GFP_KERNEL
,
3206 dev_err(&dd
->pdev
->dev
,
3207 "Memory allocation: port structure\n");
3211 /* Continue workqueue setup */
3212 for (i
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++)
3213 dd
->work
[i
].port
= dd
->port
;
3215 /* Enable unaligned IO constraints for some devices */
3216 if (mtip_device_unaligned_constrained(dd
))
3217 dd
->unal_qdepth
= MTIP_MAX_UNALIGNED_SLOTS
;
3219 dd
->unal_qdepth
= 0;
3221 sema_init(&dd
->port
->cmd_slot_unal
, dd
->unal_qdepth
);
3223 /* Spinlock to prevent concurrent issue */
3224 for (i
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++)
3225 spin_lock_init(&dd
->port
->cmd_issue_lock
[i
]);
3227 /* Set the port mmio base address. */
3228 dd
->port
->mmio
= dd
->mmio
+ PORT_OFFSET
;
3231 /* DMA allocations */
3232 rv
= mtip_dma_alloc(dd
);
3236 /* Setup the pointers to the extended s_active and CI registers. */
3237 for (i
= 0; i
< dd
->slot_groups
; i
++) {
3238 dd
->port
->s_active
[i
] =
3239 dd
->port
->mmio
+ i
*0x80 + PORT_SCR_ACT
;
3240 dd
->port
->cmd_issue
[i
] =
3241 dd
->port
->mmio
+ i
*0x80 + PORT_COMMAND_ISSUE
;
3242 dd
->port
->completed
[i
] =
3243 dd
->port
->mmio
+ i
*0x80 + PORT_SDBV
;
3246 timetaken
= jiffies
;
3247 timeout
= jiffies
+ msecs_to_jiffies(30000);
3248 while (((readl(dd
->port
->mmio
+ PORT_SCR_STAT
) & 0x0F) != 0x03) &&
3249 time_before(jiffies
, timeout
)) {
3252 if (unlikely(mtip_check_surprise_removal(dd
->pdev
))) {
3253 timetaken
= jiffies
- timetaken
;
3254 dev_warn(&dd
->pdev
->dev
,
3255 "Surprise removal detected at %u ms\n",
3256 jiffies_to_msecs(timetaken
));
3260 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))) {
3261 timetaken
= jiffies
- timetaken
;
3262 dev_warn(&dd
->pdev
->dev
,
3263 "Removal detected at %u ms\n",
3264 jiffies_to_msecs(timetaken
));
3269 /* Conditionally reset the HBA. */
3270 if (!(readl(dd
->mmio
+ HOST_CAP
) & HOST_CAP_NZDMA
)) {
3271 if (mtip_hba_reset(dd
) < 0) {
3272 dev_err(&dd
->pdev
->dev
,
3273 "Card did not reset within timeout\n");
3278 /* Clear any pending interrupts on the HBA */
3279 writel(readl(dd
->mmio
+ HOST_IRQ_STAT
),
3280 dd
->mmio
+ HOST_IRQ_STAT
);
3283 mtip_init_port(dd
->port
);
3284 mtip_start_port(dd
->port
);
3286 /* Setup the ISR and enable interrupts. */
3287 rv
= devm_request_irq(&dd
->pdev
->dev
,
3291 dev_driver_string(&dd
->pdev
->dev
),
3295 dev_err(&dd
->pdev
->dev
,
3296 "Unable to allocate IRQ %d\n", dd
->pdev
->irq
);
3299 irq_set_affinity_hint(dd
->pdev
->irq
, get_cpu_mask(dd
->isr_binding
));
3301 /* Enable interrupts on the HBA. */
3302 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
3303 dd
->mmio
+ HOST_CTL
);
3305 init_waitqueue_head(&dd
->port
->svc_wait
);
3307 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)) {
3315 /* Disable interrupts on the HBA. */
3316 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3317 dd
->mmio
+ HOST_CTL
);
3319 /* Release the IRQ. */
3320 irq_set_affinity_hint(dd
->pdev
->irq
, NULL
);
3321 devm_free_irq(&dd
->pdev
->dev
, dd
->pdev
->irq
, dd
);
3324 mtip_deinit_port(dd
->port
);
3328 /* Free the memory allocated for the for structure. */
3334 static int mtip_standby_drive(struct driver_data
*dd
)
3338 if (dd
->sr
|| !dd
->port
)
3341 * Send standby immediate (E0h) to the drive so that it
3344 if (!test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
) &&
3345 !test_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
) &&
3346 !test_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
)) {
3347 rv
= mtip_standby_immediate(dd
->port
);
3349 dev_warn(&dd
->pdev
->dev
,
3350 "STANDBY IMMEDIATE failed\n");
3356 * Called to deinitialize an interface.
3358 * @dd Pointer to the driver data structure.
3363 static int mtip_hw_exit(struct driver_data
*dd
)
3366 /* de-initialize the port. */
3367 mtip_deinit_port(dd
->port
);
3369 /* Disable interrupts on the HBA. */
3370 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3371 dd
->mmio
+ HOST_CTL
);
3374 /* Release the IRQ. */
3375 irq_set_affinity_hint(dd
->pdev
->irq
, NULL
);
3376 devm_free_irq(&dd
->pdev
->dev
, dd
->pdev
->irq
, dd
);
3379 /* Free dma regions */
3382 /* Free the memory allocated for the for structure. */
3390 * Issue a Standby Immediate command to the device.
3392 * This function is called by the Block Layer just before the
3393 * system powers off during a shutdown.
3395 * @dd Pointer to the driver data structure.
3400 static int mtip_hw_shutdown(struct driver_data
*dd
)
3403 * Send standby immediate (E0h) to the drive so that it
3406 mtip_standby_drive(dd
);
3414 * This function is called by the Block Layer just before the
3415 * system hibernates.
3417 * @dd Pointer to the driver data structure.
3420 * 0 Suspend was successful
3421 * -EFAULT Suspend was not successful
3423 static int mtip_hw_suspend(struct driver_data
*dd
)
3426 * Send standby immediate (E0h) to the drive
3427 * so that it saves its state.
3429 if (mtip_standby_drive(dd
) != 0) {
3430 dev_err(&dd
->pdev
->dev
,
3431 "Failed standby-immediate command\n");
3435 /* Disable interrupts on the HBA.*/
3436 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3437 dd
->mmio
+ HOST_CTL
);
3438 mtip_deinit_port(dd
->port
);
3446 * This function is called by the Block Layer as the
3449 * @dd Pointer to the driver data structure.
3452 * 0 Resume was successful
3453 * -EFAULT Resume was not successful
3455 static int mtip_hw_resume(struct driver_data
*dd
)
3457 /* Perform any needed hardware setup steps */
3461 if (mtip_hba_reset(dd
) != 0) {
3462 dev_err(&dd
->pdev
->dev
,
3463 "Unable to reset the HBA\n");
3468 * Enable the port, DMA engine, and FIS reception specific
3469 * h/w in controller.
3471 mtip_init_port(dd
->port
);
3472 mtip_start_port(dd
->port
);
3474 /* Enable interrupts on the HBA.*/
3475 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
3476 dd
->mmio
+ HOST_CTL
);
3482 * Helper function for reusing disk name
3483 * upon hot insertion.
3485 static int rssd_disk_name_format(char *prefix
,
3490 const int base
= 'z' - 'a' + 1;
3491 char *begin
= buf
+ strlen(prefix
);
3492 char *end
= buf
+ buflen
;
3502 *--p
= 'a' + (index
% unit
);
3503 index
= (index
/ unit
) - 1;
3504 } while (index
>= 0);
3506 memmove(begin
, p
, end
- p
);
3507 memcpy(buf
, prefix
, strlen(prefix
));
3513 * Block layer IOCTL handler.
3515 * @dev Pointer to the block_device structure.
3517 * @cmd IOCTL command passed from the user application.
3518 * @arg Argument passed from the user application.
3521 * 0 IOCTL completed successfully.
3522 * -ENOTTY IOCTL not supported or invalid driver data
3523 * structure pointer.
3525 static int mtip_block_ioctl(struct block_device
*dev
,
3530 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3532 if (!capable(CAP_SYS_ADMIN
))
3538 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)))
3545 return mtip_hw_ioctl(dd
, cmd
, arg
);
3549 #ifdef CONFIG_COMPAT
3551 * Block layer compat IOCTL handler.
3553 * @dev Pointer to the block_device structure.
3555 * @cmd IOCTL command passed from the user application.
3556 * @arg Argument passed from the user application.
3559 * 0 IOCTL completed successfully.
3560 * -ENOTTY IOCTL not supported or invalid driver data
3561 * structure pointer.
3563 static int mtip_block_compat_ioctl(struct block_device
*dev
,
3568 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3570 if (!capable(CAP_SYS_ADMIN
))
3576 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)))
3582 case HDIO_DRIVE_TASKFILE
: {
3583 struct mtip_compat_ide_task_request_s __user
*compat_req_task
;
3584 ide_task_request_t req_task
;
3585 int compat_tasksize
, outtotal
, ret
;
3588 sizeof(struct mtip_compat_ide_task_request_s
);
3591 (struct mtip_compat_ide_task_request_s __user
*) arg
;
3593 if (copy_from_user(&req_task
, (void __user
*) arg
,
3594 compat_tasksize
- (2 * sizeof(compat_long_t
))))
3597 if (get_user(req_task
.out_size
, &compat_req_task
->out_size
))
3600 if (get_user(req_task
.in_size
, &compat_req_task
->in_size
))
3603 outtotal
= sizeof(struct mtip_compat_ide_task_request_s
);
3605 ret
= exec_drive_taskfile(dd
, (void __user
*) arg
,
3606 &req_task
, outtotal
);
3608 if (copy_to_user((void __user
*) arg
, &req_task
,
3610 (2 * sizeof(compat_long_t
))))
3613 if (put_user(req_task
.out_size
, &compat_req_task
->out_size
))
3616 if (put_user(req_task
.in_size
, &compat_req_task
->in_size
))
3622 return mtip_hw_ioctl(dd
, cmd
, arg
);
3628 * Obtain the geometry of the device.
3630 * You may think that this function is obsolete, but some applications,
3631 * fdisk for example still used CHS values. This function describes the
3632 * device as having 224 heads and 56 sectors per cylinder. These values are
3633 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3634 * partition is described in terms of a start and end cylinder this means
3635 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3636 * affects performance.
3638 * @dev Pointer to the block_device strucutre.
3639 * @geo Pointer to a hd_geometry structure.
3642 * 0 Operation completed successfully.
3643 * -ENOTTY An error occurred while reading the drive capacity.
3645 static int mtip_block_getgeo(struct block_device
*dev
,
3646 struct hd_geometry
*geo
)
3648 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3654 if (!(mtip_hw_get_capacity(dd
, &capacity
))) {
3655 dev_warn(&dd
->pdev
->dev
,
3656 "Could not get drive capacity.\n");
3662 sector_div(capacity
, (geo
->heads
* geo
->sectors
));
3663 geo
->cylinders
= capacity
;
3667 static int mtip_block_open(struct block_device
*dev
, fmode_t mode
)
3669 struct driver_data
*dd
;
3671 if (dev
&& dev
->bd_disk
) {
3672 dd
= (struct driver_data
*) dev
->bd_disk
->private_data
;
3675 if (test_bit(MTIP_DDF_REMOVAL_BIT
,
3685 static void mtip_block_release(struct gendisk
*disk
, fmode_t mode
)
3690 * Block device operation function.
3692 * This structure contains pointers to the functions required by the block
3695 static const struct block_device_operations mtip_block_ops
= {
3696 .open
= mtip_block_open
,
3697 .release
= mtip_block_release
,
3698 .ioctl
= mtip_block_ioctl
,
3699 #ifdef CONFIG_COMPAT
3700 .compat_ioctl
= mtip_block_compat_ioctl
,
3702 .getgeo
= mtip_block_getgeo
,
3703 .owner
= THIS_MODULE
3706 static inline bool is_se_active(struct driver_data
*dd
)
3708 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT
, &dd
->port
->flags
))) {
3709 if (dd
->port
->ic_pause_timer
) {
3710 unsigned long to
= dd
->port
->ic_pause_timer
+
3711 msecs_to_jiffies(1000);
3712 if (time_after(jiffies
, to
)) {
3713 clear_bit(MTIP_PF_SE_ACTIVE_BIT
,
3715 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
);
3716 dd
->port
->ic_pause_timer
= 0;
3717 wake_up_interruptible(&dd
->port
->svc_wait
);
3727 * Block layer make request function.
3729 * This function is called by the kernel to process a BIO for
3732 * @queue Pointer to the request queue. Unused other than to obtain
3733 * the driver data structure.
3734 * @rq Pointer to the request.
3737 static int mtip_submit_request(struct blk_mq_hw_ctx
*hctx
, struct request
*rq
)
3739 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3740 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3743 if (is_se_active(dd
))
3746 if (unlikely(dd
->dd_flag
& MTIP_DDF_STOP_IO
)) {
3747 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
3751 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
))) {
3754 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT
,
3759 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
) ||
3760 test_bit(MTIP_DDF_REBUILD_FAILED_BIT
, &dd
->dd_flag
)))
3764 if (req_op(rq
) == REQ_OP_DISCARD
) {
3767 err
= mtip_send_trim(dd
, blk_rq_pos(rq
), blk_rq_sectors(rq
));
3768 blk_mq_end_request(rq
, err
);
3772 /* Create the scatter list for this request. */
3773 nents
= blk_rq_map_sg(hctx
->queue
, rq
, cmd
->sg
);
3775 /* Issue the read/write. */
3776 mtip_hw_submit_io(dd
, rq
, cmd
, nents
, hctx
);
3780 static bool mtip_check_unal_depth(struct blk_mq_hw_ctx
*hctx
,
3783 struct driver_data
*dd
= hctx
->queue
->queuedata
;
3784 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3786 if (rq_data_dir(rq
) == READ
|| !dd
->unal_qdepth
)
3790 * If unaligned depth must be limited on this controller, mark it
3791 * as unaligned if the IO isn't on a 4k boundary (start of length).
3793 if (blk_rq_sectors(rq
) <= 64) {
3794 if ((blk_rq_pos(rq
) & 7) || (blk_rq_sectors(rq
) & 7))
3798 if (cmd
->unaligned
&& down_trylock(&dd
->port
->cmd_slot_unal
))
3804 static int mtip_queue_rq(struct blk_mq_hw_ctx
*hctx
,
3805 const struct blk_mq_queue_data
*bd
)
3807 struct request
*rq
= bd
->rq
;
3810 if (unlikely(mtip_check_unal_depth(hctx
, rq
)))
3811 return BLK_MQ_RQ_QUEUE_BUSY
;
3813 blk_mq_start_request(rq
);
3815 ret
= mtip_submit_request(hctx
, rq
);
3817 return BLK_MQ_RQ_QUEUE_OK
;
3820 return BLK_MQ_RQ_QUEUE_ERROR
;
3823 static void mtip_free_cmd(void *data
, struct request
*rq
,
3824 unsigned int hctx_idx
, unsigned int request_idx
)
3826 struct driver_data
*dd
= data
;
3827 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3832 dmam_free_coherent(&dd
->pdev
->dev
, CMD_DMA_ALLOC_SZ
,
3833 cmd
->command
, cmd
->command_dma
);
3836 static int mtip_init_cmd(void *data
, struct request
*rq
, unsigned int hctx_idx
,
3837 unsigned int request_idx
, unsigned int numa_node
)
3839 struct driver_data
*dd
= data
;
3840 struct mtip_cmd
*cmd
= blk_mq_rq_to_pdu(rq
);
3841 u32 host_cap_64
= readl(dd
->mmio
+ HOST_CAP
) & HOST_CAP_64
;
3844 * For flush requests, request_idx starts at the end of the
3845 * tag space. Since we don't support FLUSH/FUA, simply return
3846 * 0 as there's nothing to be done.
3848 if (request_idx
>= MTIP_MAX_COMMAND_SLOTS
)
3851 cmd
->command
= dmam_alloc_coherent(&dd
->pdev
->dev
, CMD_DMA_ALLOC_SZ
,
3852 &cmd
->command_dma
, GFP_KERNEL
);
3856 memset(cmd
->command
, 0, CMD_DMA_ALLOC_SZ
);
3858 /* Point the command headers at the command tables. */
3859 cmd
->command_header
= dd
->port
->command_list
+
3860 (sizeof(struct mtip_cmd_hdr
) * request_idx
);
3861 cmd
->command_header_dma
= dd
->port
->command_list_dma
+
3862 (sizeof(struct mtip_cmd_hdr
) * request_idx
);
3865 cmd
->command_header
->ctbau
= __force_bit2int
cpu_to_le32((cmd
->command_dma
>> 16) >> 16);
3867 cmd
->command_header
->ctba
= __force_bit2int
cpu_to_le32(cmd
->command_dma
& 0xFFFFFFFF);
3869 sg_init_table(cmd
->sg
, MTIP_MAX_SG
);
3873 static enum blk_eh_timer_return
mtip_cmd_timeout(struct request
*req
,
3876 struct driver_data
*dd
= req
->q
->queuedata
;
3881 if (test_bit(req
->tag
, dd
->port
->cmds_to_issue
))
3884 if (test_and_set_bit(MTIP_PF_TO_ACTIVE_BIT
, &dd
->port
->flags
))
3887 wake_up_interruptible(&dd
->port
->svc_wait
);
3889 return BLK_EH_RESET_TIMER
;
3892 static struct blk_mq_ops mtip_mq_ops
= {
3893 .queue_rq
= mtip_queue_rq
,
3894 .init_request
= mtip_init_cmd
,
3895 .exit_request
= mtip_free_cmd
,
3896 .complete
= mtip_softirq_done_fn
,
3897 .timeout
= mtip_cmd_timeout
,
3901 * Block layer initialization function.
3903 * This function is called once by the PCI layer for each P320
3904 * device that is connected to the system.
3906 * @dd Pointer to the driver data structure.
3909 * 0 on success else an error code.
3911 static int mtip_block_initialize(struct driver_data
*dd
)
3913 int rv
= 0, wait_for_rebuild
= 0;
3915 unsigned int index
= 0;
3916 struct kobject
*kobj
;
3919 goto skip_create_disk
; /* hw init done, before rebuild */
3921 if (mtip_hw_init(dd
)) {
3923 goto protocol_init_error
;
3926 dd
->disk
= alloc_disk_node(MTIP_MAX_MINORS
, dd
->numa_node
);
3927 if (dd
->disk
== NULL
) {
3928 dev_err(&dd
->pdev
->dev
,
3929 "Unable to allocate gendisk structure\n");
3931 goto alloc_disk_error
;
3934 /* Generate the disk name, implemented same as in sd.c */
3936 if (!ida_pre_get(&rssd_index_ida
, GFP_KERNEL
)) {
3941 spin_lock(&rssd_index_lock
);
3942 rv
= ida_get_new(&rssd_index_ida
, &index
);
3943 spin_unlock(&rssd_index_lock
);
3944 } while (rv
== -EAGAIN
);
3949 rv
= rssd_disk_name_format("rssd",
3951 dd
->disk
->disk_name
,
3954 goto disk_index_error
;
3956 dd
->disk
->major
= dd
->major
;
3957 dd
->disk
->first_minor
= index
* MTIP_MAX_MINORS
;
3958 dd
->disk
->minors
= MTIP_MAX_MINORS
;
3959 dd
->disk
->fops
= &mtip_block_ops
;
3960 dd
->disk
->private_data
= dd
;
3963 mtip_hw_debugfs_init(dd
);
3965 memset(&dd
->tags
, 0, sizeof(dd
->tags
));
3966 dd
->tags
.ops
= &mtip_mq_ops
;
3967 dd
->tags
.nr_hw_queues
= 1;
3968 dd
->tags
.queue_depth
= MTIP_MAX_COMMAND_SLOTS
;
3969 dd
->tags
.reserved_tags
= 1;
3970 dd
->tags
.cmd_size
= sizeof(struct mtip_cmd
);
3971 dd
->tags
.numa_node
= dd
->numa_node
;
3972 dd
->tags
.flags
= BLK_MQ_F_SHOULD_MERGE
;
3973 dd
->tags
.driver_data
= dd
;
3974 dd
->tags
.timeout
= MTIP_NCQ_CMD_TIMEOUT_MS
;
3976 rv
= blk_mq_alloc_tag_set(&dd
->tags
);
3978 dev_err(&dd
->pdev
->dev
,
3979 "Unable to allocate request queue\n");
3980 goto block_queue_alloc_tag_error
;
3983 /* Allocate the request queue. */
3984 dd
->queue
= blk_mq_init_queue(&dd
->tags
);
3985 if (IS_ERR(dd
->queue
)) {
3986 dev_err(&dd
->pdev
->dev
,
3987 "Unable to allocate request queue\n");
3989 goto block_queue_alloc_init_error
;
3992 dd
->disk
->queue
= dd
->queue
;
3993 dd
->queue
->queuedata
= dd
;
3996 /* Initialize the protocol layer. */
3997 wait_for_rebuild
= mtip_hw_get_identify(dd
);
3998 if (wait_for_rebuild
< 0) {
3999 dev_err(&dd
->pdev
->dev
,
4000 "Protocol layer initialization failed\n");
4002 goto init_hw_cmds_error
;
4006 * if rebuild pending, start the service thread, and delay the block
4007 * queue creation and device_add_disk()
4009 if (wait_for_rebuild
== MTIP_FTL_REBUILD_MAGIC
)
4010 goto start_service_thread
;
4012 /* Set device limits. */
4013 set_bit(QUEUE_FLAG_NONROT
, &dd
->queue
->queue_flags
);
4014 clear_bit(QUEUE_FLAG_ADD_RANDOM
, &dd
->queue
->queue_flags
);
4015 blk_queue_max_segments(dd
->queue
, MTIP_MAX_SG
);
4016 blk_queue_physical_block_size(dd
->queue
, 4096);
4017 blk_queue_max_hw_sectors(dd
->queue
, 0xffff);
4018 blk_queue_max_segment_size(dd
->queue
, 0x400000);
4019 blk_queue_io_min(dd
->queue
, 4096);
4020 blk_queue_bounce_limit(dd
->queue
, dd
->pdev
->dma_mask
);
4022 /* Signal trim support */
4023 if (dd
->trim_supp
== true) {
4024 set_bit(QUEUE_FLAG_DISCARD
, &dd
->queue
->queue_flags
);
4025 dd
->queue
->limits
.discard_granularity
= 4096;
4026 blk_queue_max_discard_sectors(dd
->queue
,
4027 MTIP_MAX_TRIM_ENTRY_LEN
* MTIP_MAX_TRIM_ENTRIES
);
4028 dd
->queue
->limits
.discard_zeroes_data
= 0;
4031 /* Set the capacity of the device in 512 byte sectors. */
4032 if (!(mtip_hw_get_capacity(dd
, &capacity
))) {
4033 dev_warn(&dd
->pdev
->dev
,
4034 "Could not read drive capacity\n");
4036 goto read_capacity_error
;
4038 set_capacity(dd
->disk
, capacity
);
4040 /* Enable the block device and add it to /dev */
4041 device_add_disk(&dd
->pdev
->dev
, dd
->disk
);
4043 dd
->bdev
= bdget_disk(dd
->disk
, 0);
4045 * Now that the disk is active, initialize any sysfs attributes
4046 * managed by the protocol layer.
4048 kobj
= kobject_get(&disk_to_dev(dd
->disk
)->kobj
);
4050 mtip_hw_sysfs_init(dd
, kobj
);
4054 if (dd
->mtip_svc_handler
) {
4055 set_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
);
4056 return rv
; /* service thread created for handling rebuild */
4059 start_service_thread
:
4060 dd
->mtip_svc_handler
= kthread_create_on_node(mtip_service_thread
,
4062 "mtip_svc_thd_%02d", index
);
4064 if (IS_ERR(dd
->mtip_svc_handler
)) {
4065 dev_err(&dd
->pdev
->dev
, "service thread failed to start\n");
4066 dd
->mtip_svc_handler
= NULL
;
4068 goto kthread_run_error
;
4070 wake_up_process(dd
->mtip_svc_handler
);
4071 if (wait_for_rebuild
== MTIP_FTL_REBUILD_MAGIC
)
4072 rv
= wait_for_rebuild
;
4080 /* Delete our gendisk. This also removes the device from /dev */
4081 del_gendisk(dd
->disk
);
4083 read_capacity_error
:
4085 blk_cleanup_queue(dd
->queue
);
4086 block_queue_alloc_init_error
:
4087 blk_mq_free_tag_set(&dd
->tags
);
4088 block_queue_alloc_tag_error
:
4089 mtip_hw_debugfs_exit(dd
);
4091 spin_lock(&rssd_index_lock
);
4092 ida_remove(&rssd_index_ida
, index
);
4093 spin_unlock(&rssd_index_lock
);
4099 mtip_hw_exit(dd
); /* De-initialize the protocol layer. */
4101 protocol_init_error
:
4105 static void mtip_no_dev_cleanup(struct request
*rq
, void *data
, bool reserv
)
4107 struct driver_data
*dd
= (struct driver_data
*)data
;
4108 struct mtip_cmd
*cmd
;
4110 if (likely(!reserv
))
4111 blk_mq_complete_request(rq
, -ENODEV
);
4112 else if (test_bit(MTIP_PF_IC_ACTIVE_BIT
, &dd
->port
->flags
)) {
4114 cmd
= mtip_cmd_from_tag(dd
, MTIP_TAG_INTERNAL
);
4116 cmd
->comp_func(dd
->port
, MTIP_TAG_INTERNAL
,
4122 * Block layer deinitialization function.
4124 * Called by the PCI layer as each P320 device is removed.
4126 * @dd Pointer to the driver data structure.
4131 static int mtip_block_remove(struct driver_data
*dd
)
4133 struct kobject
*kobj
;
4135 mtip_hw_debugfs_exit(dd
);
4137 if (dd
->mtip_svc_handler
) {
4138 set_bit(MTIP_PF_SVC_THD_STOP_BIT
, &dd
->port
->flags
);
4139 wake_up_interruptible(&dd
->port
->svc_wait
);
4140 kthread_stop(dd
->mtip_svc_handler
);
4143 /* Clean up the sysfs attributes, if created */
4144 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
)) {
4145 kobj
= kobject_get(&disk_to_dev(dd
->disk
)->kobj
);
4147 mtip_hw_sysfs_exit(dd
, kobj
);
4154 * Explicitly wait here for IOs to quiesce,
4155 * as mtip_standby_drive usually won't wait for IOs.
4157 if (!mtip_quiesce_io(dd
->port
, MTIP_QUIESCE_IO_TIMEOUT_MS
,
4159 mtip_standby_drive(dd
);
4162 dev_info(&dd
->pdev
->dev
, "device %s surprise removal\n",
4163 dd
->disk
->disk_name
);
4165 blk_mq_freeze_queue_start(dd
->queue
);
4166 blk_mq_stop_hw_queues(dd
->queue
);
4167 blk_mq_tagset_busy_iter(&dd
->tags
, mtip_no_dev_cleanup
, dd
);
4170 * Delete our gendisk structure. This also removes the device
4178 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
))
4179 del_gendisk(dd
->disk
);
4180 if (dd
->disk
->queue
) {
4181 blk_cleanup_queue(dd
->queue
);
4182 blk_mq_free_tag_set(&dd
->tags
);
4189 spin_lock(&rssd_index_lock
);
4190 ida_remove(&rssd_index_ida
, dd
->index
);
4191 spin_unlock(&rssd_index_lock
);
4193 /* De-initialize the protocol layer. */
4200 * Function called by the PCI layer when just before the
4201 * machine shuts down.
4203 * If a protocol layer shutdown function is present it will be called
4206 * @dd Pointer to the driver data structure.
4211 static int mtip_block_shutdown(struct driver_data
*dd
)
4213 mtip_hw_shutdown(dd
);
4215 /* Delete our gendisk structure, and cleanup the blk queue. */
4217 dev_info(&dd
->pdev
->dev
,
4218 "Shutting down %s ...\n", dd
->disk
->disk_name
);
4220 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
))
4221 del_gendisk(dd
->disk
);
4222 if (dd
->disk
->queue
) {
4223 blk_cleanup_queue(dd
->queue
);
4224 blk_mq_free_tag_set(&dd
->tags
);
4231 spin_lock(&rssd_index_lock
);
4232 ida_remove(&rssd_index_ida
, dd
->index
);
4233 spin_unlock(&rssd_index_lock
);
4237 static int mtip_block_suspend(struct driver_data
*dd
)
4239 dev_info(&dd
->pdev
->dev
,
4240 "Suspending %s ...\n", dd
->disk
->disk_name
);
4241 mtip_hw_suspend(dd
);
4245 static int mtip_block_resume(struct driver_data
*dd
)
4247 dev_info(&dd
->pdev
->dev
, "Resuming %s ...\n",
4248 dd
->disk
->disk_name
);
4253 static void drop_cpu(int cpu
)
4258 static int get_least_used_cpu_on_node(int node
)
4260 int cpu
, least_used_cpu
, least_cnt
;
4261 const struct cpumask
*node_mask
;
4263 node_mask
= cpumask_of_node(node
);
4264 least_used_cpu
= cpumask_first(node_mask
);
4265 least_cnt
= cpu_use
[least_used_cpu
];
4266 cpu
= least_used_cpu
;
4268 for_each_cpu(cpu
, node_mask
) {
4269 if (cpu_use
[cpu
] < least_cnt
) {
4270 least_used_cpu
= cpu
;
4271 least_cnt
= cpu_use
[cpu
];
4274 cpu_use
[least_used_cpu
]++;
4275 return least_used_cpu
;
4278 /* Helper for selecting a node in round robin mode */
4279 static inline int mtip_get_next_rr_node(void)
4281 static int next_node
= -1;
4283 if (next_node
== -1) {
4284 next_node
= first_online_node
;
4288 next_node
= next_online_node(next_node
);
4289 if (next_node
== MAX_NUMNODES
)
4290 next_node
= first_online_node
;
4294 static DEFINE_HANDLER(0);
4295 static DEFINE_HANDLER(1);
4296 static DEFINE_HANDLER(2);
4297 static DEFINE_HANDLER(3);
4298 static DEFINE_HANDLER(4);
4299 static DEFINE_HANDLER(5);
4300 static DEFINE_HANDLER(6);
4301 static DEFINE_HANDLER(7);
4303 static void mtip_disable_link_opts(struct driver_data
*dd
, struct pci_dev
*pdev
)
4306 unsigned short pcie_dev_ctrl
;
4308 pos
= pci_find_capability(pdev
, PCI_CAP_ID_EXP
);
4310 pci_read_config_word(pdev
,
4311 pos
+ PCI_EXP_DEVCTL
,
4313 if (pcie_dev_ctrl
& (1 << 11) ||
4314 pcie_dev_ctrl
& (1 << 4)) {
4315 dev_info(&dd
->pdev
->dev
,
4316 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4317 pdev
->vendor
, pdev
->device
);
4318 pcie_dev_ctrl
&= ~(PCI_EXP_DEVCTL_NOSNOOP_EN
|
4319 PCI_EXP_DEVCTL_RELAX_EN
);
4320 pci_write_config_word(pdev
,
4321 pos
+ PCI_EXP_DEVCTL
,
4327 static void mtip_fix_ero_nosnoop(struct driver_data
*dd
, struct pci_dev
*pdev
)
4330 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4331 * device with device id 0x5aXX
4333 if (pdev
->bus
&& pdev
->bus
->self
) {
4334 if (pdev
->bus
->self
->vendor
== PCI_VENDOR_ID_ATI
&&
4335 ((pdev
->bus
->self
->device
& 0xff00) == 0x5a00)) {
4336 mtip_disable_link_opts(dd
, pdev
->bus
->self
);
4338 /* Check further up the topology */
4339 struct pci_dev
*parent_dev
= pdev
->bus
->self
;
4340 if (parent_dev
->bus
&&
4341 parent_dev
->bus
->parent
&&
4342 parent_dev
->bus
->parent
->self
&&
4343 parent_dev
->bus
->parent
->self
->vendor
==
4344 PCI_VENDOR_ID_ATI
&&
4345 (parent_dev
->bus
->parent
->self
->device
&
4346 0xff00) == 0x5a00) {
4347 mtip_disable_link_opts(dd
,
4348 parent_dev
->bus
->parent
->self
);
4355 * Called for each supported PCI device detected.
4357 * This function allocates the private data structure, enables the
4358 * PCI device and then calls the block layer initialization function.
4361 * 0 on success else an error code.
4363 static int mtip_pci_probe(struct pci_dev
*pdev
,
4364 const struct pci_device_id
*ent
)
4367 struct driver_data
*dd
= NULL
;
4369 const struct cpumask
*node_mask
;
4370 int cpu
, i
= 0, j
= 0;
4371 int my_node
= NUMA_NO_NODE
;
4372 unsigned long flags
;
4374 /* Allocate memory for this devices private data. */
4375 my_node
= pcibus_to_node(pdev
->bus
);
4376 if (my_node
!= NUMA_NO_NODE
) {
4377 if (!node_online(my_node
))
4378 my_node
= mtip_get_next_rr_node();
4380 dev_info(&pdev
->dev
, "Kernel not reporting proximity, choosing a node\n");
4381 my_node
= mtip_get_next_rr_node();
4383 dev_info(&pdev
->dev
, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4384 my_node
, pcibus_to_node(pdev
->bus
), dev_to_node(&pdev
->dev
),
4385 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
4387 dd
= kzalloc_node(sizeof(struct driver_data
), GFP_KERNEL
, my_node
);
4390 "Unable to allocate memory for driver data\n");
4394 /* Attach the private data to this PCI device. */
4395 pci_set_drvdata(pdev
, dd
);
4397 rv
= pcim_enable_device(pdev
);
4399 dev_err(&pdev
->dev
, "Unable to enable device\n");
4403 /* Map BAR5 to memory. */
4404 rv
= pcim_iomap_regions(pdev
, 1 << MTIP_ABAR
, MTIP_DRV_NAME
);
4406 dev_err(&pdev
->dev
, "Unable to map regions\n");
4410 if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(64))) {
4411 rv
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
4414 rv
= pci_set_consistent_dma_mask(pdev
,
4417 dev_warn(&pdev
->dev
,
4418 "64-bit DMA enable failed\n");
4424 /* Copy the info we may need later into the private data structure. */
4425 dd
->major
= mtip_major
;
4426 dd
->instance
= instance
;
4428 dd
->numa_node
= my_node
;
4430 INIT_LIST_HEAD(&dd
->online_list
);
4431 INIT_LIST_HEAD(&dd
->remove_list
);
4433 memset(dd
->workq_name
, 0, 32);
4434 snprintf(dd
->workq_name
, 31, "mtipq%d", dd
->instance
);
4436 dd
->isr_workq
= create_workqueue(dd
->workq_name
);
4437 if (!dd
->isr_workq
) {
4438 dev_warn(&pdev
->dev
, "Can't create wq %d\n", dd
->instance
);
4440 goto block_initialize_err
;
4443 memset(cpu_list
, 0, sizeof(cpu_list
));
4445 node_mask
= cpumask_of_node(dd
->numa_node
);
4446 if (!cpumask_empty(node_mask
)) {
4447 for_each_cpu(cpu
, node_mask
)
4449 snprintf(&cpu_list
[j
], 256 - j
, "%d ", cpu
);
4450 j
= strlen(cpu_list
);
4453 dev_info(&pdev
->dev
, "Node %d on package %d has %d cpu(s): %s\n",
4455 topology_physical_package_id(cpumask_first(node_mask
)),
4456 nr_cpus_node(dd
->numa_node
),
4459 dev_dbg(&pdev
->dev
, "mtip32xx: node_mask empty\n");
4461 dd
->isr_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4462 dev_info(&pdev
->dev
, "Initial IRQ binding node:cpu %d:%d\n",
4463 cpu_to_node(dd
->isr_binding
), dd
->isr_binding
);
4465 /* first worker context always runs in ISR */
4466 dd
->work
[0].cpu_binding
= dd
->isr_binding
;
4467 dd
->work
[1].cpu_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4468 dd
->work
[2].cpu_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4469 dd
->work
[3].cpu_binding
= dd
->work
[0].cpu_binding
;
4470 dd
->work
[4].cpu_binding
= dd
->work
[1].cpu_binding
;
4471 dd
->work
[5].cpu_binding
= dd
->work
[2].cpu_binding
;
4472 dd
->work
[6].cpu_binding
= dd
->work
[2].cpu_binding
;
4473 dd
->work
[7].cpu_binding
= dd
->work
[1].cpu_binding
;
4475 /* Log the bindings */
4476 for_each_present_cpu(cpu
) {
4477 memset(cpu_list
, 0, sizeof(cpu_list
));
4478 for (i
= 0, j
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++) {
4479 if (dd
->work
[i
].cpu_binding
== cpu
) {
4480 snprintf(&cpu_list
[j
], 256 - j
, "%d ", i
);
4481 j
= strlen(cpu_list
);
4485 dev_info(&pdev
->dev
, "CPU %d: WQs %s\n", cpu
, cpu_list
);
4488 INIT_WORK(&dd
->work
[0].work
, mtip_workq_sdbf0
);
4489 INIT_WORK(&dd
->work
[1].work
, mtip_workq_sdbf1
);
4490 INIT_WORK(&dd
->work
[2].work
, mtip_workq_sdbf2
);
4491 INIT_WORK(&dd
->work
[3].work
, mtip_workq_sdbf3
);
4492 INIT_WORK(&dd
->work
[4].work
, mtip_workq_sdbf4
);
4493 INIT_WORK(&dd
->work
[5].work
, mtip_workq_sdbf5
);
4494 INIT_WORK(&dd
->work
[6].work
, mtip_workq_sdbf6
);
4495 INIT_WORK(&dd
->work
[7].work
, mtip_workq_sdbf7
);
4497 pci_set_master(pdev
);
4498 rv
= pci_enable_msi(pdev
);
4500 dev_warn(&pdev
->dev
,
4501 "Unable to enable MSI interrupt.\n");
4502 goto msi_initialize_err
;
4505 mtip_fix_ero_nosnoop(dd
, pdev
);
4507 /* Initialize the block layer. */
4508 rv
= mtip_block_initialize(dd
);
4511 "Unable to initialize block layer\n");
4512 goto block_initialize_err
;
4516 * Increment the instance count so that each device has a unique
4520 if (rv
!= MTIP_FTL_REBUILD_MAGIC
)
4521 set_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
);
4523 rv
= 0; /* device in rebuild state, return 0 from probe */
4525 /* Add to online list even if in ftl rebuild */
4526 spin_lock_irqsave(&dev_lock
, flags
);
4527 list_add(&dd
->online_list
, &online_list
);
4528 spin_unlock_irqrestore(&dev_lock
, flags
);
4532 block_initialize_err
:
4533 pci_disable_msi(pdev
);
4536 if (dd
->isr_workq
) {
4537 flush_workqueue(dd
->isr_workq
);
4538 destroy_workqueue(dd
->isr_workq
);
4539 drop_cpu(dd
->work
[0].cpu_binding
);
4540 drop_cpu(dd
->work
[1].cpu_binding
);
4541 drop_cpu(dd
->work
[2].cpu_binding
);
4544 pcim_iounmap_regions(pdev
, 1 << MTIP_ABAR
);
4548 pci_set_drvdata(pdev
, NULL
);
4555 * Called for each probed device when the device is removed or the
4556 * driver is unloaded.
4561 static void mtip_pci_remove(struct pci_dev
*pdev
)
4563 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4564 unsigned long flags
, to
;
4566 set_bit(MTIP_DDF_REMOVAL_BIT
, &dd
->dd_flag
);
4568 spin_lock_irqsave(&dev_lock
, flags
);
4569 list_del_init(&dd
->online_list
);
4570 list_add(&dd
->remove_list
, &removing_list
);
4571 spin_unlock_irqrestore(&dev_lock
, flags
);
4573 mtip_check_surprise_removal(pdev
);
4574 synchronize_irq(dd
->pdev
->irq
);
4576 /* Spin until workers are done */
4577 to
= jiffies
+ msecs_to_jiffies(4000);
4580 } while (atomic_read(&dd
->irq_workers_active
) != 0 &&
4581 time_before(jiffies
, to
));
4584 fsync_bdev(dd
->bdev
);
4586 if (atomic_read(&dd
->irq_workers_active
) != 0) {
4587 dev_warn(&dd
->pdev
->dev
,
4588 "Completion workers still active!\n");
4591 blk_set_queue_dying(dd
->queue
);
4592 set_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
);
4594 /* Clean up the block layer. */
4595 mtip_block_remove(dd
);
4597 if (dd
->isr_workq
) {
4598 flush_workqueue(dd
->isr_workq
);
4599 destroy_workqueue(dd
->isr_workq
);
4600 drop_cpu(dd
->work
[0].cpu_binding
);
4601 drop_cpu(dd
->work
[1].cpu_binding
);
4602 drop_cpu(dd
->work
[2].cpu_binding
);
4605 pci_disable_msi(pdev
);
4607 spin_lock_irqsave(&dev_lock
, flags
);
4608 list_del_init(&dd
->remove_list
);
4609 spin_unlock_irqrestore(&dev_lock
, flags
);
4613 pcim_iounmap_regions(pdev
, 1 << MTIP_ABAR
);
4614 pci_set_drvdata(pdev
, NULL
);
4618 * Called for each probed device when the device is suspended.
4624 static int mtip_pci_suspend(struct pci_dev
*pdev
, pm_message_t mesg
)
4627 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4631 "Driver private datastructure is NULL\n");
4635 set_bit(MTIP_DDF_RESUME_BIT
, &dd
->dd_flag
);
4637 /* Disable ports & interrupts then send standby immediate */
4638 rv
= mtip_block_suspend(dd
);
4641 "Failed to suspend controller\n");
4646 * Save the pci config space to pdev structure &
4647 * disable the device
4649 pci_save_state(pdev
);
4650 pci_disable_device(pdev
);
4652 /* Move to Low power state*/
4653 pci_set_power_state(pdev
, PCI_D3hot
);
4659 * Called for each probed device when the device is resumed.
4665 static int mtip_pci_resume(struct pci_dev
*pdev
)
4668 struct driver_data
*dd
;
4670 dd
= pci_get_drvdata(pdev
);
4673 "Driver private datastructure is NULL\n");
4677 /* Move the device to active State */
4678 pci_set_power_state(pdev
, PCI_D0
);
4680 /* Restore PCI configuration space */
4681 pci_restore_state(pdev
);
4683 /* Enable the PCI device*/
4684 rv
= pcim_enable_device(pdev
);
4687 "Failed to enable card during resume\n");
4690 pci_set_master(pdev
);
4693 * Calls hbaReset, initPort, & startPort function
4694 * then enables interrupts
4696 rv
= mtip_block_resume(dd
);
4698 dev_err(&pdev
->dev
, "Unable to resume\n");
4701 clear_bit(MTIP_DDF_RESUME_BIT
, &dd
->dd_flag
);
4712 static void mtip_pci_shutdown(struct pci_dev
*pdev
)
4714 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4716 mtip_block_shutdown(dd
);
4719 /* Table of device ids supported by this driver. */
4720 static const struct pci_device_id mtip_pci_tbl
[] = {
4721 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320H_DEVICE_ID
) },
4722 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320M_DEVICE_ID
) },
4723 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320S_DEVICE_ID
) },
4724 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P325M_DEVICE_ID
) },
4725 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P420H_DEVICE_ID
) },
4726 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P420M_DEVICE_ID
) },
4727 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P425M_DEVICE_ID
) },
4731 /* Structure that describes the PCI driver functions. */
4732 static struct pci_driver mtip_pci_driver
= {
4733 .name
= MTIP_DRV_NAME
,
4734 .id_table
= mtip_pci_tbl
,
4735 .probe
= mtip_pci_probe
,
4736 .remove
= mtip_pci_remove
,
4737 .suspend
= mtip_pci_suspend
,
4738 .resume
= mtip_pci_resume
,
4739 .shutdown
= mtip_pci_shutdown
,
4742 MODULE_DEVICE_TABLE(pci
, mtip_pci_tbl
);
4745 * Module initialization function.
4747 * Called once when the module is loaded. This function allocates a major
4748 * block device number to the Cyclone devices and registers the PCI layer
4752 * 0 on success else error code.
4754 static int __init
mtip_init(void)
4758 pr_info(MTIP_DRV_NAME
" Version " MTIP_DRV_VERSION
"\n");
4760 spin_lock_init(&dev_lock
);
4762 INIT_LIST_HEAD(&online_list
);
4763 INIT_LIST_HEAD(&removing_list
);
4765 /* Allocate a major block device number to use with this driver. */
4766 error
= register_blkdev(0, MTIP_DRV_NAME
);
4768 pr_err("Unable to register block device (%d)\n",
4774 dfs_parent
= debugfs_create_dir("rssd", NULL
);
4775 if (IS_ERR_OR_NULL(dfs_parent
)) {
4776 pr_warn("Error creating debugfs parent\n");
4780 dfs_device_status
= debugfs_create_file("device_status",
4781 S_IRUGO
, dfs_parent
, NULL
,
4782 &mtip_device_status_fops
);
4783 if (IS_ERR_OR_NULL(dfs_device_status
)) {
4784 pr_err("Error creating device_status node\n");
4785 dfs_device_status
= NULL
;
4789 /* Register our PCI operations. */
4790 error
= pci_register_driver(&mtip_pci_driver
);
4792 debugfs_remove(dfs_parent
);
4793 unregister_blkdev(mtip_major
, MTIP_DRV_NAME
);
4800 * Module de-initialization function.
4802 * Called once when the module is unloaded. This function deallocates
4803 * the major block device number allocated by mtip_init() and
4804 * unregisters the PCI layer of the driver.
4809 static void __exit
mtip_exit(void)
4811 /* Release the allocated major block device number. */
4812 unregister_blkdev(mtip_major
, MTIP_DRV_NAME
);
4814 /* Unregister the PCI driver. */
4815 pci_unregister_driver(&mtip_pci_driver
);
4817 debugfs_remove_recursive(dfs_parent
);
4820 MODULE_AUTHOR("Micron Technology, Inc");
4821 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4822 MODULE_LICENSE("GPL");
4823 MODULE_VERSION(MTIP_DRV_VERSION
);
4825 module_init(mtip_init
);
4826 module_exit(mtip_exit
);