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/bio.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/idr.h>
37 #include <linux/kthread.h>
38 #include <../drivers/ata/ahci.h>
39 #include <linux/export.h>
40 #include <linux/debugfs.h>
43 #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
44 #define HW_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (MTIP_MAX_SG * 16))
45 #define HW_CMD_TBL_AR_SZ (HW_CMD_TBL_SZ * MTIP_MAX_COMMAND_SLOTS)
46 #define HW_PORT_PRIV_DMA_SZ \
47 (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
49 #define HOST_CAP_NZDMA (1 << 19)
50 #define HOST_HSORG 0xFC
51 #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
52 #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
53 #define HSORG_HWREV 0xFF00
54 #define HSORG_STYLE 0x8
55 #define HSORG_SLOTGROUPS 0x7
57 #define PORT_COMMAND_ISSUE 0x38
58 #define PORT_SDBV 0x7C
60 #define PORT_OFFSET 0x100
61 #define PORT_MEM_SIZE 0x80
63 #define PORT_IRQ_ERR \
64 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
65 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
66 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
68 #define PORT_IRQ_LEGACY \
69 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
70 #define PORT_IRQ_HANDLED \
71 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
72 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
73 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
74 #define DEF_PORT_IRQ \
75 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
78 #define MTIP_PRODUCT_UNKNOWN 0x00
79 #define MTIP_PRODUCT_ASICFPGA 0x11
81 /* Device instance number, incremented each time a device is probed. */
85 * Global variable used to hold the major block device number
86 * allocated in mtip_init().
88 static int mtip_major
;
89 static struct dentry
*dfs_parent
;
91 static u32 cpu_use
[NR_CPUS
];
93 static DEFINE_SPINLOCK(rssd_index_lock
);
94 static DEFINE_IDA(rssd_index_ida
);
96 static int mtip_block_initialize(struct driver_data
*dd
);
99 struct mtip_compat_ide_task_request_s
{
102 ide_reg_valid_t out_flags
;
103 ide_reg_valid_t in_flags
;
106 compat_ulong_t out_size
;
107 compat_ulong_t in_size
;
112 * This function check_for_surprise_removal is called
113 * while card is removed from the system and it will
114 * read the vendor id from the configration space
116 * @pdev Pointer to the pci_dev structure.
119 * true if device removed, else false
121 static bool mtip_check_surprise_removal(struct pci_dev
*pdev
)
125 /* Read the vendorID from the configuration space */
126 pci_read_config_word(pdev
, 0x00, &vendor_id
);
127 if (vendor_id
== 0xFFFF)
128 return true; /* device removed */
130 return false; /* device present */
134 * This function is called for clean the pending command in the
135 * command slot during the surprise removal of device and return
136 * error to the upper layer.
138 * @dd Pointer to the DRIVER_DATA structure.
143 static void mtip_command_cleanup(struct driver_data
*dd
)
145 int group
= 0, commandslot
= 0, commandindex
= 0;
146 struct mtip_cmd
*command
;
147 struct mtip_port
*port
= dd
->port
;
148 static int in_progress
;
155 for (group
= 0; group
< 4; group
++) {
156 for (commandslot
= 0; commandslot
< 32; commandslot
++) {
157 if (!(port
->allocated
[group
] & (1 << commandslot
)))
160 commandindex
= group
<< 5 | commandslot
;
161 command
= &port
->commands
[commandindex
];
163 if (atomic_read(&command
->active
)
164 && (command
->async_callback
)) {
165 command
->async_callback(command
->async_data
,
167 command
->async_callback
= NULL
;
168 command
->async_data
= NULL
;
171 dma_unmap_sg(&port
->dd
->pdev
->dev
,
173 command
->scatter_ents
,
180 set_bit(MTIP_DDF_CLEANUP_BIT
, &dd
->dd_flag
);
185 * Obtain an empty command slot.
187 * This function needs to be reentrant since it could be called
188 * at the same time on multiple CPUs. The allocation of the
189 * command slot must be atomic.
191 * @port Pointer to the port data structure.
194 * >= 0 Index of command slot obtained.
195 * -1 No command slots available.
197 static int get_slot(struct mtip_port
*port
)
200 unsigned int num_command_slots
= port
->dd
->slot_groups
* 32;
203 * Try 10 times, because there is a small race here.
204 * that's ok, because it's still cheaper than a lock.
206 * Race: Since this section is not protected by lock, same bit
207 * could be chosen by different process contexts running in
208 * different processor. So instead of costly lock, we are going
211 for (i
= 0; i
< 10; i
++) {
212 slot
= find_next_zero_bit(port
->allocated
,
213 num_command_slots
, 1);
214 if ((slot
< num_command_slots
) &&
215 (!test_and_set_bit(slot
, port
->allocated
)))
218 dev_warn(&port
->dd
->pdev
->dev
, "Failed to get a tag.\n");
220 if (mtip_check_surprise_removal(port
->dd
->pdev
)) {
221 /* Device not present, clean outstanding commands */
222 mtip_command_cleanup(port
->dd
);
228 * Release a command slot.
230 * @port Pointer to the port data structure.
231 * @tag Tag of command to release
236 static inline void release_slot(struct mtip_port
*port
, int tag
)
238 smp_mb__before_clear_bit();
239 clear_bit(tag
, port
->allocated
);
240 smp_mb__after_clear_bit();
244 * Reset the HBA (without sleeping)
246 * Just like hba_reset, except does not call sleep, so can be
247 * run from interrupt/tasklet context.
249 * @dd Pointer to the driver data structure.
252 * 0 The reset was successful.
253 * -1 The HBA Reset bit did not clear.
255 static int hba_reset_nosleep(struct driver_data
*dd
)
257 unsigned long timeout
;
259 /* Chip quirk: quiesce any chip function */
262 /* Set the reset bit */
263 writel(HOST_RESET
, dd
->mmio
+ HOST_CTL
);
266 readl(dd
->mmio
+ HOST_CTL
);
269 * Wait 10ms then spin for up to 1 second
270 * waiting for reset acknowledgement
272 timeout
= jiffies
+ msecs_to_jiffies(1000);
274 while ((readl(dd
->mmio
+ HOST_CTL
) & HOST_RESET
)
275 && time_before(jiffies
, timeout
))
278 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))
281 if (readl(dd
->mmio
+ HOST_CTL
) & HOST_RESET
)
288 * Issue a command to the hardware.
290 * Set the appropriate bit in the s_active and Command Issue hardware
291 * registers, causing hardware command processing to begin.
293 * @port Pointer to the port structure.
294 * @tag The tag of the command to be issued.
299 static inline void mtip_issue_ncq_command(struct mtip_port
*port
, int tag
)
301 int group
= tag
>> 5;
303 atomic_set(&port
->commands
[tag
].active
, 1);
305 /* guard SACT and CI registers */
306 spin_lock(&port
->cmd_issue_lock
[group
]);
307 writel((1 << MTIP_TAG_BIT(tag
)),
308 port
->s_active
[MTIP_TAG_INDEX(tag
)]);
309 writel((1 << MTIP_TAG_BIT(tag
)),
310 port
->cmd_issue
[MTIP_TAG_INDEX(tag
)]);
311 spin_unlock(&port
->cmd_issue_lock
[group
]);
313 /* Set the command's timeout value.*/
314 port
->commands
[tag
].comp_time
= jiffies
+ msecs_to_jiffies(
315 MTIP_NCQ_COMMAND_TIMEOUT_MS
);
319 * Enable/disable the reception of FIS
321 * @port Pointer to the port data structure
322 * @enable 1 to enable, 0 to disable
325 * Previous state: 1 enabled, 0 disabled
327 static int mtip_enable_fis(struct mtip_port
*port
, int enable
)
331 /* enable FIS reception */
332 tmp
= readl(port
->mmio
+ PORT_CMD
);
334 writel(tmp
| PORT_CMD_FIS_RX
, port
->mmio
+ PORT_CMD
);
336 writel(tmp
& ~PORT_CMD_FIS_RX
, port
->mmio
+ PORT_CMD
);
339 readl(port
->mmio
+ PORT_CMD
);
341 return (((tmp
& PORT_CMD_FIS_RX
) == PORT_CMD_FIS_RX
));
345 * Enable/disable the DMA engine
347 * @port Pointer to the port data structure
348 * @enable 1 to enable, 0 to disable
351 * Previous state: 1 enabled, 0 disabled.
353 static int mtip_enable_engine(struct mtip_port
*port
, int enable
)
357 /* enable FIS reception */
358 tmp
= readl(port
->mmio
+ PORT_CMD
);
360 writel(tmp
| PORT_CMD_START
, port
->mmio
+ PORT_CMD
);
362 writel(tmp
& ~PORT_CMD_START
, port
->mmio
+ PORT_CMD
);
364 readl(port
->mmio
+ PORT_CMD
);
365 return (((tmp
& PORT_CMD_START
) == PORT_CMD_START
));
369 * Enables the port DMA engine and FIS reception.
374 static inline void mtip_start_port(struct mtip_port
*port
)
376 /* Enable FIS reception */
377 mtip_enable_fis(port
, 1);
379 /* Enable the DMA engine */
380 mtip_enable_engine(port
, 1);
384 * Deinitialize a port by disabling port interrupts, the DMA engine,
387 * @port Pointer to the port structure
392 static inline void mtip_deinit_port(struct mtip_port
*port
)
394 /* Disable interrupts on this port */
395 writel(0, port
->mmio
+ PORT_IRQ_MASK
);
397 /* Disable the DMA engine */
398 mtip_enable_engine(port
, 0);
400 /* Disable FIS reception */
401 mtip_enable_fis(port
, 0);
407 * This function deinitializes the port by calling mtip_deinit_port() and
408 * then initializes it by setting the command header and RX FIS addresses,
409 * clearing the SError register and any pending port interrupts before
410 * re-enabling the default set of port interrupts.
412 * @port Pointer to the port structure.
417 static void mtip_init_port(struct mtip_port
*port
)
420 mtip_deinit_port(port
);
422 /* Program the command list base and FIS base addresses */
423 if (readl(port
->dd
->mmio
+ HOST_CAP
) & HOST_CAP_64
) {
424 writel((port
->command_list_dma
>> 16) >> 16,
425 port
->mmio
+ PORT_LST_ADDR_HI
);
426 writel((port
->rxfis_dma
>> 16) >> 16,
427 port
->mmio
+ PORT_FIS_ADDR_HI
);
430 writel(port
->command_list_dma
& 0xFFFFFFFF,
431 port
->mmio
+ PORT_LST_ADDR
);
432 writel(port
->rxfis_dma
& 0xFFFFFFFF, port
->mmio
+ PORT_FIS_ADDR
);
435 writel(readl(port
->mmio
+ PORT_SCR_ERR
), port
->mmio
+ PORT_SCR_ERR
);
437 /* reset the completed registers.*/
438 for (i
= 0; i
< port
->dd
->slot_groups
; i
++)
439 writel(0xFFFFFFFF, port
->completed
[i
]);
441 /* Clear any pending interrupts for this port */
442 writel(readl(port
->mmio
+ PORT_IRQ_STAT
), port
->mmio
+ PORT_IRQ_STAT
);
444 /* Clear any pending interrupts on the HBA. */
445 writel(readl(port
->dd
->mmio
+ HOST_IRQ_STAT
),
446 port
->dd
->mmio
+ HOST_IRQ_STAT
);
448 /* Enable port interrupts */
449 writel(DEF_PORT_IRQ
, port
->mmio
+ PORT_IRQ_MASK
);
455 * @port Pointer to the port data structure.
460 static void mtip_restart_port(struct mtip_port
*port
)
462 unsigned long timeout
;
464 /* Disable the DMA engine */
465 mtip_enable_engine(port
, 0);
467 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
468 timeout
= jiffies
+ msecs_to_jiffies(500);
469 while ((readl(port
->mmio
+ PORT_CMD
) & PORT_CMD_LIST_ON
)
470 && time_before(jiffies
, timeout
))
473 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
477 * Chip quirk: escalate to hba reset if
478 * PxCMD.CR not clear after 500 ms
480 if (readl(port
->mmio
+ PORT_CMD
) & PORT_CMD_LIST_ON
) {
481 dev_warn(&port
->dd
->pdev
->dev
,
482 "PxCMD.CR not clear, escalating reset\n");
484 if (hba_reset_nosleep(port
->dd
))
485 dev_err(&port
->dd
->pdev
->dev
,
486 "HBA reset escalation failed.\n");
488 /* 30 ms delay before com reset to quiesce chip */
492 dev_warn(&port
->dd
->pdev
->dev
, "Issuing COM reset\n");
495 writel(readl(port
->mmio
+ PORT_SCR_CTL
) |
496 1, port
->mmio
+ PORT_SCR_CTL
);
497 readl(port
->mmio
+ PORT_SCR_CTL
);
499 /* Wait 1 ms to quiesce chip function */
500 timeout
= jiffies
+ msecs_to_jiffies(1);
501 while (time_before(jiffies
, timeout
))
504 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
507 /* Clear PxSCTL.DET */
508 writel(readl(port
->mmio
+ PORT_SCR_CTL
) & ~1,
509 port
->mmio
+ PORT_SCR_CTL
);
510 readl(port
->mmio
+ PORT_SCR_CTL
);
512 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
513 timeout
= jiffies
+ msecs_to_jiffies(500);
514 while (((readl(port
->mmio
+ PORT_SCR_STAT
) & 0x01) == 0)
515 && time_before(jiffies
, timeout
))
518 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
521 if ((readl(port
->mmio
+ PORT_SCR_STAT
) & 0x01) == 0)
522 dev_warn(&port
->dd
->pdev
->dev
,
523 "COM reset failed\n");
525 mtip_init_port(port
);
526 mtip_start_port(port
);
531 * Helper function for tag logging
533 static void print_tags(struct driver_data
*dd
,
535 unsigned long *tagbits
,
538 unsigned char tagmap
[128];
539 int group
, tagmap_len
= 0;
541 memset(tagmap
, 0, sizeof(tagmap
));
542 for (group
= SLOTBITS_IN_LONGS
; group
> 0; group
--)
543 tagmap_len
= sprintf(tagmap
+ tagmap_len
, "%016lX ",
545 dev_warn(&dd
->pdev
->dev
,
546 "%d command(s) %s: tagmap [%s]", cnt
, msg
, tagmap
);
550 * Called periodically to see if any read/write commands are
551 * taking too long to complete.
553 * @data Pointer to the PORT data structure.
558 static void mtip_timeout_function(unsigned long int data
)
560 struct mtip_port
*port
= (struct mtip_port
*) data
;
561 struct host_to_dev_fis
*fis
;
562 struct mtip_cmd
*command
;
563 int tag
, cmdto_cnt
= 0;
564 unsigned int bit
, group
;
565 unsigned int num_command_slots
;
566 unsigned long to
, tagaccum
[SLOTBITS_IN_LONGS
];
571 if (test_bit(MTIP_DDF_RESUME_BIT
, &port
->dd
->dd_flag
)) {
572 mod_timer(&port
->cmd_timer
,
573 jiffies
+ msecs_to_jiffies(30000));
576 /* clear the tag accumulator */
577 memset(tagaccum
, 0, SLOTBITS_IN_LONGS
* sizeof(long));
578 num_command_slots
= port
->dd
->slot_groups
* 32;
580 for (tag
= 0; tag
< num_command_slots
; tag
++) {
582 * Skip internal command slot as it has
583 * its own timeout mechanism
585 if (tag
== MTIP_TAG_INTERNAL
)
588 if (atomic_read(&port
->commands
[tag
].active
) &&
589 (time_after(jiffies
, port
->commands
[tag
].comp_time
))) {
593 command
= &port
->commands
[tag
];
594 fis
= (struct host_to_dev_fis
*) command
->command
;
596 set_bit(tag
, tagaccum
);
599 set_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
);
602 * Clear the completed bit. This should prevent
603 * any interrupt handlers from trying to retire
606 writel(1 << bit
, port
->completed
[group
]);
608 /* Call the async completion callback. */
609 if (likely(command
->async_callback
))
610 command
->async_callback(command
->async_data
,
612 command
->async_callback
= NULL
;
613 command
->comp_func
= NULL
;
615 /* Unmap the DMA scatter list entries */
616 dma_unmap_sg(&port
->dd
->pdev
->dev
,
618 command
->scatter_ents
,
622 * Clear the allocated bit and active tag for the
625 atomic_set(&port
->commands
[tag
].active
, 0);
626 release_slot(port
, tag
);
632 if (cmdto_cnt
&& !test_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
)) {
633 print_tags(port
->dd
, "timed out", tagaccum
, cmdto_cnt
);
635 mtip_restart_port(port
);
636 clear_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
);
637 wake_up_interruptible(&port
->svc_wait
);
640 if (port
->ic_pause_timer
) {
641 to
= port
->ic_pause_timer
+ msecs_to_jiffies(1000);
642 if (time_after(jiffies
, to
)) {
643 if (!test_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
)) {
644 port
->ic_pause_timer
= 0;
645 clear_bit(MTIP_PF_SE_ACTIVE_BIT
, &port
->flags
);
646 clear_bit(MTIP_PF_DM_ACTIVE_BIT
, &port
->flags
);
647 clear_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
648 wake_up_interruptible(&port
->svc_wait
);
655 /* Restart the timer */
656 mod_timer(&port
->cmd_timer
,
657 jiffies
+ msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD
));
661 * IO completion function.
663 * This completion function is called by the driver ISR when a
664 * command that was issued by the kernel completes. It first calls the
665 * asynchronous completion function which normally calls back into the block
666 * layer passing the asynchronous callback data, then unmaps the
667 * scatter list associated with the completed command, and finally
668 * clears the allocated bit associated with the completed command.
670 * @port Pointer to the port data structure.
671 * @tag Tag of the command.
672 * @data Pointer to driver_data.
673 * @status Completion status.
678 static void mtip_async_complete(struct mtip_port
*port
,
683 struct mtip_cmd
*command
;
684 struct driver_data
*dd
= data
;
685 int cb_status
= status
? -EIO
: 0;
687 if (unlikely(!dd
) || unlikely(!port
))
690 command
= &port
->commands
[tag
];
692 if (unlikely(status
== PORT_IRQ_TF_ERR
)) {
693 dev_warn(&port
->dd
->pdev
->dev
,
694 "Command tag %d failed due to TFE\n", tag
);
697 /* Upper layer callback */
698 if (likely(command
->async_callback
))
699 command
->async_callback(command
->async_data
, cb_status
);
701 command
->async_callback
= NULL
;
702 command
->comp_func
= NULL
;
704 /* Unmap the DMA scatter list entries */
705 dma_unmap_sg(&dd
->pdev
->dev
,
707 command
->scatter_ents
,
710 /* Clear the allocated and active bits for the command */
711 atomic_set(&port
->commands
[tag
].active
, 0);
712 release_slot(port
, tag
);
718 * Internal command completion callback function.
720 * This function is normally called by the driver ISR when an internal
721 * command completed. This function signals the command completion by
722 * calling complete().
724 * @port Pointer to the port data structure.
725 * @tag Tag of the command that has completed.
726 * @data Pointer to a completion structure.
727 * @status Completion status.
732 static void mtip_completion(struct mtip_port
*port
,
737 struct mtip_cmd
*command
= &port
->commands
[tag
];
738 struct completion
*waiting
= data
;
739 if (unlikely(status
== PORT_IRQ_TF_ERR
))
740 dev_warn(&port
->dd
->pdev
->dev
,
741 "Internal command %d completed with TFE\n", tag
);
743 command
->async_callback
= NULL
;
744 command
->comp_func
= NULL
;
749 static void mtip_null_completion(struct mtip_port
*port
,
757 static int mtip_read_log_page(struct mtip_port
*port
, u8 page
, u16
*buffer
,
758 dma_addr_t buffer_dma
, unsigned int sectors
);
759 static int mtip_get_smart_attr(struct mtip_port
*port
, unsigned int id
,
760 struct smart_attr
*attrib
);
764 * @dd Pointer to the DRIVER_DATA structure.
769 static void mtip_handle_tfe(struct driver_data
*dd
)
771 int group
, tag
, bit
, reissue
, rv
;
772 struct mtip_port
*port
;
773 struct mtip_cmd
*cmd
;
775 struct host_to_dev_fis
*fis
;
776 unsigned long tagaccum
[SLOTBITS_IN_LONGS
];
777 unsigned int cmd_cnt
= 0;
779 char *fail_reason
= NULL
;
780 int fail_all_ncq_write
= 0, fail_all_ncq_cmds
= 0;
782 dev_warn(&dd
->pdev
->dev
, "Taskfile error\n");
786 /* Stop the timer to prevent command timeouts. */
787 del_timer(&port
->cmd_timer
);
788 set_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
);
790 if (test_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
) &&
791 test_bit(MTIP_TAG_INTERNAL
, port
->allocated
)) {
792 cmd
= &port
->commands
[MTIP_TAG_INTERNAL
];
793 dbg_printk(MTIP_DRV_NAME
" TFE for the internal command\n");
795 atomic_inc(&cmd
->active
); /* active > 1 indicates error */
796 if (cmd
->comp_data
&& cmd
->comp_func
) {
797 cmd
->comp_func(port
, MTIP_TAG_INTERNAL
,
798 cmd
->comp_data
, PORT_IRQ_TF_ERR
);
800 goto handle_tfe_exit
;
803 /* clear the tag accumulator */
804 memset(tagaccum
, 0, SLOTBITS_IN_LONGS
* sizeof(long));
806 /* Loop through all the groups */
807 for (group
= 0; group
< dd
->slot_groups
; group
++) {
808 completed
= readl(port
->completed
[group
]);
810 /* clear completed status register in the hardware.*/
811 writel(completed
, port
->completed
[group
]);
813 /* Process successfully completed commands */
814 for (bit
= 0; bit
< 32 && completed
; bit
++) {
815 if (!(completed
& (1<<bit
)))
817 tag
= (group
<< 5) + bit
;
819 /* Skip the internal command slot */
820 if (tag
== MTIP_TAG_INTERNAL
)
823 cmd
= &port
->commands
[tag
];
824 if (likely(cmd
->comp_func
)) {
825 set_bit(tag
, tagaccum
);
827 atomic_set(&cmd
->active
, 0);
833 dev_err(&port
->dd
->pdev
->dev
,
834 "Missing completion func for tag %d",
836 if (mtip_check_surprise_removal(dd
->pdev
)) {
837 mtip_command_cleanup(dd
);
838 /* don't proceed further */
845 print_tags(dd
, "completed (TFE)", tagaccum
, cmd_cnt
);
847 /* Restart the port */
849 mtip_restart_port(port
);
851 /* Trying to determine the cause of the error */
852 rv
= mtip_read_log_page(dd
->port
, ATA_LOG_SATA_NCQ
,
854 dd
->port
->log_buf_dma
, 1);
856 dev_warn(&dd
->pdev
->dev
,
857 "Error in READ LOG EXT (10h) command\n");
858 /* non-critical error, don't fail the load */
860 buf
= (unsigned char *)dd
->port
->log_buf
;
861 if (buf
[259] & 0x1) {
862 dev_info(&dd
->pdev
->dev
,
863 "Write protect bit is set.\n");
864 set_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
);
865 fail_all_ncq_write
= 1;
866 fail_reason
= "write protect";
868 if (buf
[288] == 0xF7) {
869 dev_info(&dd
->pdev
->dev
,
870 "Exceeded Tmax, drive in thermal shutdown.\n");
871 set_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
);
872 fail_all_ncq_cmds
= 1;
873 fail_reason
= "thermal shutdown";
875 if (buf
[288] == 0xBF) {
876 dev_info(&dd
->pdev
->dev
,
877 "Drive indicates rebuild has failed.\n");
878 fail_all_ncq_cmds
= 1;
879 fail_reason
= "rebuild failed";
883 /* clear the tag accumulator */
884 memset(tagaccum
, 0, SLOTBITS_IN_LONGS
* sizeof(long));
886 /* Loop through all the groups */
887 for (group
= 0; group
< dd
->slot_groups
; group
++) {
888 for (bit
= 0; bit
< 32; bit
++) {
890 tag
= (group
<< 5) + bit
;
891 cmd
= &port
->commands
[tag
];
893 /* If the active bit is set re-issue the command */
894 if (atomic_read(&cmd
->active
) == 0)
897 fis
= (struct host_to_dev_fis
*)cmd
->command
;
899 /* Should re-issue? */
900 if (tag
== MTIP_TAG_INTERNAL
||
901 fis
->command
== ATA_CMD_SET_FEATURES
)
904 if (fail_all_ncq_cmds
||
905 (fail_all_ncq_write
&&
906 fis
->command
== ATA_CMD_FPDMA_WRITE
)) {
907 dev_warn(&dd
->pdev
->dev
,
908 " Fail: %s w/tag %d [%s].\n",
909 fis
->command
== ATA_CMD_FPDMA_WRITE
?
912 fail_reason
!= NULL
?
913 fail_reason
: "unknown");
914 atomic_set(&cmd
->active
, 0);
915 if (cmd
->comp_func
) {
916 cmd
->comp_func(port
, tag
,
925 * First check if this command has
926 * exceeded its retries.
928 if (reissue
&& (cmd
->retries
-- > 0)) {
930 set_bit(tag
, tagaccum
);
932 /* Re-issue the command. */
933 mtip_issue_ncq_command(port
, tag
);
938 /* Retire a command that will not be reissued */
939 dev_warn(&port
->dd
->pdev
->dev
,
940 "retiring tag %d\n", tag
);
941 atomic_set(&cmd
->active
, 0);
950 dev_warn(&port
->dd
->pdev
->dev
,
951 "Bad completion for tag %d\n",
955 print_tags(dd
, "reissued (TFE)", tagaccum
, cmd_cnt
);
958 /* clear eh_active */
959 clear_bit(MTIP_PF_EH_ACTIVE_BIT
, &port
->flags
);
960 wake_up_interruptible(&port
->svc_wait
);
962 mod_timer(&port
->cmd_timer
,
963 jiffies
+ msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD
));
967 * Handle a set device bits interrupt
969 static inline void mtip_workq_sdbfx(struct mtip_port
*port
, int group
,
972 struct driver_data
*dd
= port
->dd
;
974 struct mtip_cmd
*command
;
977 WARN_ON_ONCE(!completed
);
980 /* clear completed status register in the hardware.*/
981 writel(completed
, port
->completed
[group
]);
983 /* Process completed commands. */
984 for (bit
= 0; (bit
< 32) && completed
; bit
++) {
985 if (completed
& 0x01) {
986 tag
= (group
<< 5) | bit
;
988 /* skip internal command slot. */
989 if (unlikely(tag
== MTIP_TAG_INTERNAL
))
992 command
= &port
->commands
[tag
];
993 /* make internal callback */
994 if (likely(command
->comp_func
)) {
1001 dev_warn(&dd
->pdev
->dev
,
1006 if (mtip_check_surprise_removal(
1008 mtip_command_cleanup(dd
);
1016 /* If last, re-enable interrupts */
1017 if (atomic_dec_return(&dd
->irq_workers_active
) == 0)
1018 writel(0xffffffff, dd
->mmio
+ HOST_IRQ_STAT
);
1022 * Process legacy pio and d2h interrupts
1024 static inline void mtip_process_legacy(struct driver_data
*dd
, u32 port_stat
)
1026 struct mtip_port
*port
= dd
->port
;
1027 struct mtip_cmd
*cmd
= &port
->commands
[MTIP_TAG_INTERNAL
];
1029 if (test_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
) &&
1030 (cmd
!= NULL
) && !(readl(port
->cmd_issue
[MTIP_TAG_INTERNAL
])
1031 & (1 << MTIP_TAG_INTERNAL
))) {
1032 if (cmd
->comp_func
) {
1033 cmd
->comp_func(port
,
1045 * Demux and handle errors
1047 static inline void mtip_process_errors(struct driver_data
*dd
, u32 port_stat
)
1049 if (likely(port_stat
& (PORT_IRQ_TF_ERR
| PORT_IRQ_IF_ERR
)))
1050 mtip_handle_tfe(dd
);
1052 if (unlikely(port_stat
& PORT_IRQ_CONNECT
)) {
1053 dev_warn(&dd
->pdev
->dev
,
1054 "Clearing PxSERR.DIAG.x\n");
1055 writel((1 << 26), dd
->port
->mmio
+ PORT_SCR_ERR
);
1058 if (unlikely(port_stat
& PORT_IRQ_PHYRDY
)) {
1059 dev_warn(&dd
->pdev
->dev
,
1060 "Clearing PxSERR.DIAG.n\n");
1061 writel((1 << 16), dd
->port
->mmio
+ PORT_SCR_ERR
);
1064 if (unlikely(port_stat
& ~PORT_IRQ_HANDLED
)) {
1065 dev_warn(&dd
->pdev
->dev
,
1066 "Port stat errors %x unhandled\n",
1067 (port_stat
& ~PORT_IRQ_HANDLED
));
1071 static inline irqreturn_t
mtip_handle_irq(struct driver_data
*data
)
1073 struct driver_data
*dd
= (struct driver_data
*) data
;
1074 struct mtip_port
*port
= dd
->port
;
1075 u32 hba_stat
, port_stat
;
1077 int do_irq_enable
= 1, i
, workers
;
1078 struct mtip_work
*twork
;
1080 hba_stat
= readl(dd
->mmio
+ HOST_IRQ_STAT
);
1084 /* Acknowledge the interrupt status on the port.*/
1085 port_stat
= readl(port
->mmio
+ PORT_IRQ_STAT
);
1086 writel(port_stat
, port
->mmio
+ PORT_IRQ_STAT
);
1088 /* Demux port status */
1089 if (likely(port_stat
& PORT_IRQ_SDB_FIS
)) {
1091 WARN_ON_ONCE(atomic_read(&dd
->irq_workers_active
) != 0);
1093 /* Start at 1: group zero is always local? */
1094 for (i
= 0, workers
= 0; i
< MTIP_MAX_SLOT_GROUPS
;
1096 twork
= &dd
->work
[i
];
1097 twork
->completed
= readl(port
->completed
[i
]);
1098 if (twork
->completed
)
1102 atomic_set(&dd
->irq_workers_active
, workers
);
1104 for (i
= 1; i
< MTIP_MAX_SLOT_GROUPS
; i
++) {
1105 twork
= &dd
->work
[i
];
1106 if (twork
->completed
)
1113 if (likely(dd
->work
[0].completed
))
1114 mtip_workq_sdbfx(port
, 0,
1115 dd
->work
[0].completed
);
1119 * Chip quirk: SDB interrupt but nothing
1126 if (unlikely(port_stat
& PORT_IRQ_ERR
)) {
1127 if (unlikely(mtip_check_surprise_removal(dd
->pdev
))) {
1128 mtip_command_cleanup(dd
);
1129 /* don't proceed further */
1132 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
1136 mtip_process_errors(dd
, port_stat
& PORT_IRQ_ERR
);
1139 if (unlikely(port_stat
& PORT_IRQ_LEGACY
))
1140 mtip_process_legacy(dd
, port_stat
& PORT_IRQ_LEGACY
);
1143 /* acknowledge interrupt */
1144 if (unlikely(do_irq_enable
))
1145 writel(hba_stat
, dd
->mmio
+ HOST_IRQ_STAT
);
1151 * HBA interrupt subroutine.
1154 * @instance Pointer to the driver data structure.
1157 * IRQ_HANDLED A HBA interrupt was pending and handled.
1158 * IRQ_NONE This interrupt was not for the HBA.
1160 static irqreturn_t
mtip_irq_handler(int irq
, void *instance
)
1162 struct driver_data
*dd
= instance
;
1164 return mtip_handle_irq(dd
);
1167 static void mtip_issue_non_ncq_command(struct mtip_port
*port
, int tag
)
1169 atomic_set(&port
->commands
[tag
].active
, 1);
1170 writel(1 << MTIP_TAG_BIT(tag
),
1171 port
->cmd_issue
[MTIP_TAG_INDEX(tag
)]);
1174 static bool mtip_pause_ncq(struct mtip_port
*port
,
1175 struct host_to_dev_fis
*fis
)
1177 struct host_to_dev_fis
*reply
;
1178 unsigned long task_file_data
;
1180 reply
= port
->rxfis
+ RX_FIS_D2H_REG
;
1181 task_file_data
= readl(port
->mmio
+PORT_TFDATA
);
1183 if (fis
->command
== ATA_CMD_SEC_ERASE_UNIT
)
1184 clear_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
1186 if ((task_file_data
& 1))
1189 if (fis
->command
== ATA_CMD_SEC_ERASE_PREP
) {
1190 set_bit(MTIP_PF_SE_ACTIVE_BIT
, &port
->flags
);
1191 set_bit(MTIP_DDF_SEC_LOCK_BIT
, &port
->dd
->dd_flag
);
1192 port
->ic_pause_timer
= jiffies
;
1194 } else if ((fis
->command
== ATA_CMD_DOWNLOAD_MICRO
) &&
1195 (fis
->features
== 0x03)) {
1196 set_bit(MTIP_PF_DM_ACTIVE_BIT
, &port
->flags
);
1197 port
->ic_pause_timer
= jiffies
;
1199 } else if ((fis
->command
== ATA_CMD_SEC_ERASE_UNIT
) ||
1200 ((fis
->command
== 0xFC) &&
1201 (fis
->features
== 0x27 || fis
->features
== 0x72 ||
1202 fis
->features
== 0x62 || fis
->features
== 0x26))) {
1203 /* Com reset after secure erase or lowlevel format */
1204 mtip_restart_port(port
);
1212 * Wait for port to quiesce
1214 * @port Pointer to port data structure
1215 * @timeout Max duration to wait (ms)
1219 * -EBUSY Commands still active
1221 static int mtip_quiesce_io(struct mtip_port
*port
, unsigned long timeout
)
1225 unsigned int active
= 1;
1227 to
= jiffies
+ msecs_to_jiffies(timeout
);
1229 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
) &&
1230 test_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
)) {
1232 continue; /* svc thd is actively issuing commands */
1234 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
1237 * Ignore s_active bit 0 of array element 0.
1238 * This bit will always be set
1240 active
= readl(port
->s_active
[0]) & 0xFFFFFFFE;
1241 for (n
= 1; n
< port
->dd
->slot_groups
; n
++)
1242 active
|= readl(port
->s_active
[n
]);
1248 } while (time_before(jiffies
, to
));
1250 return active
? -EBUSY
: 0;
1254 * Execute an internal command and wait for the completion.
1256 * @port Pointer to the port data structure.
1257 * @fis Pointer to the FIS that describes the command.
1258 * @fis_len Length in WORDS of the FIS.
1259 * @buffer DMA accessible for command data.
1260 * @buf_len Length, in bytes, of the data buffer.
1261 * @opts Command header options, excluding the FIS length
1262 * and the number of PRD entries.
1263 * @timeout Time in ms to wait for the command to complete.
1266 * 0 Command completed successfully.
1267 * -EFAULT The buffer address is not correctly aligned.
1268 * -EBUSY Internal command or other IO in progress.
1269 * -EAGAIN Time out waiting for command to complete.
1271 static int mtip_exec_internal_command(struct mtip_port
*port
,
1272 struct host_to_dev_fis
*fis
,
1278 unsigned long timeout
)
1280 struct mtip_cmd_sg
*command_sg
;
1281 DECLARE_COMPLETION_ONSTACK(wait
);
1282 int rv
= 0, ready2go
= 1;
1283 struct mtip_cmd
*int_cmd
= &port
->commands
[MTIP_TAG_INTERNAL
];
1286 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1287 if (buffer
& 0x00000007) {
1288 dev_err(&port
->dd
->pdev
->dev
,
1289 "SG buffer is not 8 byte aligned\n");
1293 to
= jiffies
+ msecs_to_jiffies(timeout
);
1295 ready2go
= !test_and_set_bit(MTIP_TAG_INTERNAL
,
1300 } while (time_before(jiffies
, to
));
1302 dev_warn(&port
->dd
->pdev
->dev
,
1303 "Internal cmd active. new cmd [%02X]\n", fis
->command
);
1306 set_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1307 port
->ic_pause_timer
= 0;
1309 if (fis
->command
== ATA_CMD_SEC_ERASE_UNIT
)
1310 clear_bit(MTIP_PF_SE_ACTIVE_BIT
, &port
->flags
);
1311 else if (fis
->command
== ATA_CMD_DOWNLOAD_MICRO
)
1312 clear_bit(MTIP_PF_DM_ACTIVE_BIT
, &port
->flags
);
1314 if (atomic
== GFP_KERNEL
) {
1315 if (fis
->command
!= ATA_CMD_STANDBYNOW1
) {
1316 /* wait for io to complete if non atomic */
1317 if (mtip_quiesce_io(port
, 5000) < 0) {
1318 dev_warn(&port
->dd
->pdev
->dev
,
1319 "Failed to quiesce IO\n");
1320 release_slot(port
, MTIP_TAG_INTERNAL
);
1321 clear_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1322 wake_up_interruptible(&port
->svc_wait
);
1327 /* Set the completion function and data for the command. */
1328 int_cmd
->comp_data
= &wait
;
1329 int_cmd
->comp_func
= mtip_completion
;
1332 /* Clear completion - we're going to poll */
1333 int_cmd
->comp_data
= NULL
;
1334 int_cmd
->comp_func
= mtip_null_completion
;
1337 /* Copy the command to the command table */
1338 memcpy(int_cmd
->command
, fis
, fis_len
*4);
1340 /* Populate the SG list */
1341 int_cmd
->command_header
->opts
=
1342 __force_bit2int
cpu_to_le32(opts
| fis_len
);
1344 command_sg
= int_cmd
->command
+ AHCI_CMD_TBL_HDR_SZ
;
1347 __force_bit2int
cpu_to_le32((buf_len
-1) & 0x3FFFFF);
1349 __force_bit2int
cpu_to_le32(buffer
& 0xFFFFFFFF);
1350 command_sg
->dba_upper
=
1351 __force_bit2int
cpu_to_le32((buffer
>> 16) >> 16);
1353 int_cmd
->command_header
->opts
|=
1354 __force_bit2int
cpu_to_le32((1 << 16));
1357 /* Populate the command header */
1358 int_cmd
->command_header
->byte_count
= 0;
1360 /* Issue the command to the hardware */
1361 mtip_issue_non_ncq_command(port
, MTIP_TAG_INTERNAL
);
1363 /* Poll if atomic, wait_for_completion otherwise */
1364 if (atomic
== GFP_KERNEL
) {
1365 /* Wait for the command to complete or timeout. */
1366 if (wait_for_completion_timeout(
1368 msecs_to_jiffies(timeout
)) == 0) {
1369 dev_err(&port
->dd
->pdev
->dev
,
1370 "Internal command did not complete [%d] "
1371 "within timeout of %lu ms\n",
1373 if (mtip_check_surprise_removal(port
->dd
->pdev
) ||
1374 test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
1375 &port
->dd
->dd_flag
)) {
1382 /* Spin for <timeout> checking if command still outstanding */
1383 timeout
= jiffies
+ msecs_to_jiffies(timeout
);
1384 while ((readl(port
->cmd_issue
[MTIP_TAG_INTERNAL
])
1385 & (1 << MTIP_TAG_INTERNAL
))
1386 && time_before(jiffies
, timeout
)) {
1387 if (mtip_check_surprise_removal(port
->dd
->pdev
)) {
1391 if ((fis
->command
!= ATA_CMD_STANDBYNOW1
) &&
1392 test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
1393 &port
->dd
->dd_flag
)) {
1397 if (readl(port
->mmio
+ PORT_IRQ_STAT
) & PORT_IRQ_ERR
) {
1398 atomic_inc(&int_cmd
->active
); /* error */
1404 if (atomic_read(&int_cmd
->active
) > 1) {
1405 dev_err(&port
->dd
->pdev
->dev
,
1406 "Internal command [%02X] failed\n", fis
->command
);
1409 if (readl(port
->cmd_issue
[MTIP_TAG_INTERNAL
])
1410 & (1 << MTIP_TAG_INTERNAL
)) {
1412 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
1413 &port
->dd
->dd_flag
)) {
1414 mtip_restart_port(port
);
1419 /* Clear the allocated and active bits for the internal command. */
1420 atomic_set(&int_cmd
->active
, 0);
1421 release_slot(port
, MTIP_TAG_INTERNAL
);
1422 if (rv
>= 0 && mtip_pause_ncq(port
, fis
)) {
1426 clear_bit(MTIP_PF_IC_ACTIVE_BIT
, &port
->flags
);
1427 wake_up_interruptible(&port
->svc_wait
);
1433 * Byte-swap ATA ID strings.
1435 * ATA identify data contains strings in byte-swapped 16-bit words.
1436 * They must be swapped (on all architectures) to be usable as C strings.
1437 * This function swaps bytes in-place.
1439 * @buf The buffer location of the string
1440 * @len The number of bytes to swap
1445 static inline void ata_swap_string(u16
*buf
, unsigned int len
)
1448 for (i
= 0; i
< (len
/2); i
++)
1449 be16_to_cpus(&buf
[i
]);
1453 * Request the device identity information.
1455 * If a user space buffer is not specified, i.e. is NULL, the
1456 * identify information is still read from the drive and placed
1457 * into the identify data buffer (@e port->identify) in the
1458 * port data structure.
1459 * When the identify buffer contains valid identify information @e
1460 * port->identify_valid is non-zero.
1462 * @port Pointer to the port structure.
1463 * @user_buffer A user space buffer where the identify data should be
1467 * 0 Command completed successfully.
1468 * -EFAULT An error occurred while coping data to the user buffer.
1469 * -1 Command failed.
1471 static int mtip_get_identify(struct mtip_port
*port
, void __user
*user_buffer
)
1474 struct host_to_dev_fis fis
;
1476 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &port
->dd
->dd_flag
))
1479 /* Build the FIS. */
1480 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1483 fis
.command
= ATA_CMD_ID_ATA
;
1485 /* Set the identify information as invalid. */
1486 port
->identify_valid
= 0;
1488 /* Clear the identify information. */
1489 memset(port
->identify
, 0, sizeof(u16
) * ATA_ID_WORDS
);
1491 /* Execute the command. */
1492 if (mtip_exec_internal_command(port
,
1496 sizeof(u16
) * ATA_ID_WORDS
,
1499 MTIP_INTERNAL_COMMAND_TIMEOUT_MS
)
1506 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1507 * perform field-sensitive swapping on the string fields.
1508 * See the kernel use of ata_id_string() for proof of this.
1510 #ifdef __LITTLE_ENDIAN
1511 ata_swap_string(port
->identify
+ 27, 40); /* model string*/
1512 ata_swap_string(port
->identify
+ 23, 8); /* firmware string*/
1513 ata_swap_string(port
->identify
+ 10, 20); /* serial# string*/
1517 for (i
= 0; i
< ATA_ID_WORDS
; i
++)
1518 port
->identify
[i
] = le16_to_cpu(port
->identify
[i
]);
1522 /* Set the identify buffer as valid. */
1523 port
->identify_valid
= 1;
1529 ATA_ID_WORDS
* sizeof(u16
))) {
1540 * Issue a standby immediate command to the device.
1542 * @port Pointer to the port structure.
1545 * 0 Command was executed successfully.
1546 * -1 An error occurred while executing the command.
1548 static int mtip_standby_immediate(struct mtip_port
*port
)
1551 struct host_to_dev_fis fis
;
1552 unsigned long start
;
1554 /* Build the FIS. */
1555 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1558 fis
.command
= ATA_CMD_STANDBYNOW1
;
1561 rv
= mtip_exec_internal_command(port
,
1569 dbg_printk(MTIP_DRV_NAME
"Time taken to complete standby cmd: %d ms\n",
1570 jiffies_to_msecs(jiffies
- start
));
1572 dev_warn(&port
->dd
->pdev
->dev
,
1573 "STANDBY IMMEDIATE command failed.\n");
1579 * Issue a READ LOG EXT command to the device.
1581 * @port pointer to the port structure.
1582 * @page page number to fetch
1583 * @buffer pointer to buffer
1584 * @buffer_dma dma address corresponding to @buffer
1585 * @sectors page length to fetch, in sectors
1588 * @rv return value from mtip_exec_internal_command()
1590 static int mtip_read_log_page(struct mtip_port
*port
, u8 page
, u16
*buffer
,
1591 dma_addr_t buffer_dma
, unsigned int sectors
)
1593 struct host_to_dev_fis fis
;
1595 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1598 fis
.command
= ATA_CMD_READ_LOG_EXT
;
1599 fis
.sect_count
= sectors
& 0xFF;
1600 fis
.sect_cnt_ex
= (sectors
>> 8) & 0xFF;
1603 fis
.device
= ATA_DEVICE_OBS
;
1605 memset(buffer
, 0, sectors
* ATA_SECT_SIZE
);
1607 return mtip_exec_internal_command(port
,
1611 sectors
* ATA_SECT_SIZE
,
1614 MTIP_INTERNAL_COMMAND_TIMEOUT_MS
);
1618 * Issue a SMART READ DATA command to the device.
1620 * @port pointer to the port structure.
1621 * @buffer pointer to buffer
1622 * @buffer_dma dma address corresponding to @buffer
1625 * @rv return value from mtip_exec_internal_command()
1627 static int mtip_get_smart_data(struct mtip_port
*port
, u8
*buffer
,
1628 dma_addr_t buffer_dma
)
1630 struct host_to_dev_fis fis
;
1632 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1635 fis
.command
= ATA_CMD_SMART
;
1636 fis
.features
= 0xD0;
1640 fis
.device
= ATA_DEVICE_OBS
;
1642 return mtip_exec_internal_command(port
,
1653 * Get the value of a smart attribute
1655 * @port pointer to the port structure
1656 * @id attribute number
1657 * @attrib pointer to return attrib information corresponding to @id
1660 * -EINVAL NULL buffer passed or unsupported attribute @id.
1661 * -EPERM Identify data not valid, SMART not supported or not enabled
1663 static int mtip_get_smart_attr(struct mtip_port
*port
, unsigned int id
,
1664 struct smart_attr
*attrib
)
1667 struct smart_attr
*pattr
;
1672 if (!port
->identify_valid
) {
1673 dev_warn(&port
->dd
->pdev
->dev
, "IDENTIFY DATA not valid\n");
1676 if (!(port
->identify
[82] & 0x1)) {
1677 dev_warn(&port
->dd
->pdev
->dev
, "SMART not supported\n");
1680 if (!(port
->identify
[85] & 0x1)) {
1681 dev_warn(&port
->dd
->pdev
->dev
, "SMART not enabled\n");
1685 memset(port
->smart_buf
, 0, ATA_SECT_SIZE
);
1686 rv
= mtip_get_smart_data(port
, port
->smart_buf
, port
->smart_buf_dma
);
1688 dev_warn(&port
->dd
->pdev
->dev
, "Failed to ge SMART data\n");
1692 pattr
= (struct smart_attr
*)(port
->smart_buf
+ 2);
1693 for (i
= 0; i
< 29; i
++, pattr
++)
1694 if (pattr
->attr_id
== id
) {
1695 memcpy(attrib
, pattr
, sizeof(struct smart_attr
));
1700 dev_warn(&port
->dd
->pdev
->dev
,
1701 "Query for invalid SMART attribute ID\n");
1709 * Get the drive capacity.
1711 * @dd Pointer to the device data structure.
1712 * @sectors Pointer to the variable that will receive the sector count.
1715 * 1 Capacity was returned successfully.
1716 * 0 The identify information is invalid.
1718 static bool mtip_hw_get_capacity(struct driver_data
*dd
, sector_t
*sectors
)
1720 struct mtip_port
*port
= dd
->port
;
1721 u64 total
, raw0
, raw1
, raw2
, raw3
;
1722 raw0
= port
->identify
[100];
1723 raw1
= port
->identify
[101];
1724 raw2
= port
->identify
[102];
1725 raw3
= port
->identify
[103];
1726 total
= raw0
| raw1
<<16 | raw2
<<32 | raw3
<<48;
1728 return (bool) !!port
->identify_valid
;
1734 * Resets the HBA by setting the HBA Reset bit in the Global
1735 * HBA Control register. After setting the HBA Reset bit the
1736 * function waits for 1 second before reading the HBA Reset
1737 * bit to make sure it has cleared. If HBA Reset is not clear
1738 * an error is returned. Cannot be used in non-blockable
1741 * @dd Pointer to the driver data structure.
1744 * 0 The reset was successful.
1745 * -1 The HBA Reset bit did not clear.
1747 static int mtip_hba_reset(struct driver_data
*dd
)
1749 mtip_deinit_port(dd
->port
);
1751 /* Set the reset bit */
1752 writel(HOST_RESET
, dd
->mmio
+ HOST_CTL
);
1755 readl(dd
->mmio
+ HOST_CTL
);
1757 /* Wait for reset to clear */
1760 /* Check the bit has cleared */
1761 if (readl(dd
->mmio
+ HOST_CTL
) & HOST_RESET
) {
1762 dev_err(&dd
->pdev
->dev
,
1763 "Reset bit did not clear.\n");
1771 * Display the identify command data.
1773 * @port Pointer to the port data structure.
1778 static void mtip_dump_identify(struct mtip_port
*port
)
1781 unsigned short revid
;
1784 if (!port
->identify_valid
)
1787 strlcpy(cbuf
, (char *)(port
->identify
+10), 21);
1788 dev_info(&port
->dd
->pdev
->dev
,
1789 "Serial No.: %s\n", cbuf
);
1791 strlcpy(cbuf
, (char *)(port
->identify
+23), 9);
1792 dev_info(&port
->dd
->pdev
->dev
,
1793 "Firmware Ver.: %s\n", cbuf
);
1795 strlcpy(cbuf
, (char *)(port
->identify
+27), 41);
1796 dev_info(&port
->dd
->pdev
->dev
, "Model: %s\n", cbuf
);
1798 if (mtip_hw_get_capacity(port
->dd
, §ors
))
1799 dev_info(&port
->dd
->pdev
->dev
,
1800 "Capacity: %llu sectors (%llu MB)\n",
1802 ((u64
)sectors
) * ATA_SECT_SIZE
>> 20);
1804 pci_read_config_word(port
->dd
->pdev
, PCI_REVISION_ID
, &revid
);
1805 switch (revid
& 0xFF) {
1807 strlcpy(cbuf
, "A0", 3);
1810 strlcpy(cbuf
, "A2", 3);
1813 strlcpy(cbuf
, "?", 2);
1816 dev_info(&port
->dd
->pdev
->dev
,
1817 "Card Type: %s\n", cbuf
);
1821 * Map the commands scatter list into the command table.
1823 * @command Pointer to the command.
1824 * @nents Number of scatter list entries.
1829 static inline void fill_command_sg(struct driver_data
*dd
,
1830 struct mtip_cmd
*command
,
1834 unsigned int dma_len
;
1835 struct mtip_cmd_sg
*command_sg
;
1836 struct scatterlist
*sg
= command
->sg
;
1838 command_sg
= command
->command
+ AHCI_CMD_TBL_HDR_SZ
;
1840 for (n
= 0; n
< nents
; n
++) {
1841 dma_len
= sg_dma_len(sg
);
1842 if (dma_len
> 0x400000)
1843 dev_err(&dd
->pdev
->dev
,
1844 "DMA segment length truncated\n");
1845 command_sg
->info
= __force_bit2int
1846 cpu_to_le32((dma_len
-1) & 0x3FFFFF);
1847 command_sg
->dba
= __force_bit2int
1848 cpu_to_le32(sg_dma_address(sg
));
1849 command_sg
->dba_upper
= __force_bit2int
1850 cpu_to_le32((sg_dma_address(sg
) >> 16) >> 16);
1857 * @brief Execute a drive command.
1859 * return value 0 The command completed successfully.
1860 * return value -1 An error occurred while executing the command.
1862 static int exec_drive_task(struct mtip_port
*port
, u8
*command
)
1864 struct host_to_dev_fis fis
;
1865 struct host_to_dev_fis
*reply
= (port
->rxfis
+ RX_FIS_D2H_REG
);
1867 /* Build the FIS. */
1868 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1871 fis
.command
= command
[0];
1872 fis
.features
= command
[1];
1873 fis
.sect_count
= command
[2];
1874 fis
.sector
= command
[3];
1875 fis
.cyl_low
= command
[4];
1876 fis
.cyl_hi
= command
[5];
1877 fis
.device
= command
[6] & ~0x10; /* Clear the dev bit*/
1879 dbg_printk(MTIP_DRV_NAME
" %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
1889 /* Execute the command. */
1890 if (mtip_exec_internal_command(port
,
1897 MTIP_IOCTL_COMMAND_TIMEOUT_MS
) < 0) {
1901 command
[0] = reply
->command
; /* Status*/
1902 command
[1] = reply
->features
; /* Error*/
1903 command
[4] = reply
->cyl_low
;
1904 command
[5] = reply
->cyl_hi
;
1906 dbg_printk(MTIP_DRV_NAME
" %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
1917 * @brief Execute a drive command.
1919 * @param port Pointer to the port data structure.
1920 * @param command Pointer to the user specified command parameters.
1921 * @param user_buffer Pointer to the user space buffer where read sector
1922 * data should be copied.
1924 * return value 0 The command completed successfully.
1925 * return value -EFAULT An error occurred while copying the completion
1926 * data to the user space buffer.
1927 * return value -1 An error occurred while executing the command.
1929 static int exec_drive_command(struct mtip_port
*port
, u8
*command
,
1930 void __user
*user_buffer
)
1932 struct host_to_dev_fis fis
;
1933 struct host_to_dev_fis
*reply
;
1935 dma_addr_t dma_addr
= 0;
1936 int rv
= 0, xfer_sz
= command
[3];
1942 buf
= dmam_alloc_coherent(&port
->dd
->pdev
->dev
,
1943 ATA_SECT_SIZE
* xfer_sz
,
1947 dev_err(&port
->dd
->pdev
->dev
,
1948 "Memory allocation failed (%d bytes)\n",
1949 ATA_SECT_SIZE
* xfer_sz
);
1952 memset(buf
, 0, ATA_SECT_SIZE
* xfer_sz
);
1955 /* Build the FIS. */
1956 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
1959 fis
.command
= command
[0];
1960 fis
.features
= command
[2];
1961 fis
.sect_count
= command
[3];
1962 if (fis
.command
== ATA_CMD_SMART
) {
1963 fis
.sector
= command
[1];
1969 reply
= (port
->rxfis
+ RX_FIS_PIO_SETUP
);
1971 reply
= (port
->rxfis
+ RX_FIS_D2H_REG
);
1973 dbg_printk(MTIP_DRV_NAME
1974 " %s: User Command: cmd %x, sect %x, "
1975 "feat %x, sectcnt %x\n",
1982 /* Execute the command. */
1983 if (mtip_exec_internal_command(port
,
1986 (xfer_sz
? dma_addr
: 0),
1987 (xfer_sz
? ATA_SECT_SIZE
* xfer_sz
: 0),
1990 MTIP_IOCTL_COMMAND_TIMEOUT_MS
)
1993 goto exit_drive_command
;
1996 /* Collect the completion status. */
1997 command
[0] = reply
->command
; /* Status*/
1998 command
[1] = reply
->features
; /* Error*/
1999 command
[2] = reply
->sect_count
;
2001 dbg_printk(MTIP_DRV_NAME
2002 " %s: Completion Status: stat %x, "
2003 "err %x, nsect %x\n",
2010 if (copy_to_user(user_buffer
,
2012 ATA_SECT_SIZE
* command
[3])) {
2014 goto exit_drive_command
;
2019 dmam_free_coherent(&port
->dd
->pdev
->dev
,
2020 ATA_SECT_SIZE
* xfer_sz
, buf
, dma_addr
);
2025 * Indicates whether a command has a single sector payload.
2027 * @command passed to the device to perform the certain event.
2028 * @features passed to the device to perform the certain event.
2031 * 1 command is one that always has a single sector payload,
2032 * regardless of the value in the Sector Count field.
2036 static unsigned int implicit_sector(unsigned char command
,
2037 unsigned char features
)
2039 unsigned int rv
= 0;
2041 /* list of commands that have an implicit sector count of 1 */
2043 case ATA_CMD_SEC_SET_PASS
:
2044 case ATA_CMD_SEC_UNLOCK
:
2045 case ATA_CMD_SEC_ERASE_PREP
:
2046 case ATA_CMD_SEC_ERASE_UNIT
:
2047 case ATA_CMD_SEC_FREEZE_LOCK
:
2048 case ATA_CMD_SEC_DISABLE_PASS
:
2049 case ATA_CMD_PMP_READ
:
2050 case ATA_CMD_PMP_WRITE
:
2053 case ATA_CMD_SET_MAX
:
2054 if (features
== ATA_SET_MAX_UNLOCK
)
2058 if ((features
== ATA_SMART_READ_VALUES
) ||
2059 (features
== ATA_SMART_READ_THRESHOLDS
))
2062 case ATA_CMD_CONF_OVERLAY
:
2063 if ((features
== ATA_DCO_IDENTIFY
) ||
2064 (features
== ATA_DCO_SET
))
2070 static void mtip_set_timeout(struct driver_data
*dd
,
2071 struct host_to_dev_fis
*fis
,
2072 unsigned int *timeout
, u8 erasemode
)
2074 switch (fis
->command
) {
2075 case ATA_CMD_DOWNLOAD_MICRO
:
2076 *timeout
= 120000; /* 2 minutes */
2078 case ATA_CMD_SEC_ERASE_UNIT
:
2081 *timeout
= ((*(dd
->port
->identify
+ 90) * 2) * 60000);
2083 *timeout
= ((*(dd
->port
->identify
+ 89) * 2) * 60000);
2085 case ATA_CMD_STANDBYNOW1
:
2086 *timeout
= 120000; /* 2 minutes */
2090 *timeout
= 60000; /* 60 seconds */
2093 *timeout
= 15000; /* 15 seconds */
2096 *timeout
= MTIP_IOCTL_COMMAND_TIMEOUT_MS
;
2102 * Executes a taskfile
2103 * See ide_taskfile_ioctl() for derivation
2105 static int exec_drive_taskfile(struct driver_data
*dd
,
2107 ide_task_request_t
*req_task
,
2110 struct host_to_dev_fis fis
;
2111 struct host_to_dev_fis
*reply
;
2114 dma_addr_t outbuf_dma
= 0;
2115 dma_addr_t inbuf_dma
= 0;
2116 dma_addr_t dma_buffer
= 0;
2118 unsigned int taskin
= 0;
2119 unsigned int taskout
= 0;
2121 unsigned int timeout
;
2122 unsigned int force_single_sector
;
2123 unsigned int transfer_size
;
2124 unsigned long task_file_data
;
2125 int intotal
= outtotal
+ req_task
->out_size
;
2128 taskout
= req_task
->out_size
;
2129 taskin
= req_task
->in_size
;
2130 /* 130560 = 512 * 0xFF*/
2131 if (taskin
> 130560 || taskout
> 130560) {
2137 outbuf
= kzalloc(taskout
, GFP_KERNEL
);
2138 if (outbuf
== NULL
) {
2142 if (copy_from_user(outbuf
, buf
+ outtotal
, taskout
)) {
2146 outbuf_dma
= pci_map_single(dd
->pdev
,
2150 if (outbuf_dma
== 0) {
2154 dma_buffer
= outbuf_dma
;
2158 inbuf
= kzalloc(taskin
, GFP_KERNEL
);
2159 if (inbuf
== NULL
) {
2164 if (copy_from_user(inbuf
, buf
+ intotal
, taskin
)) {
2168 inbuf_dma
= pci_map_single(dd
->pdev
,
2170 taskin
, DMA_FROM_DEVICE
);
2171 if (inbuf_dma
== 0) {
2175 dma_buffer
= inbuf_dma
;
2178 /* only supports PIO and non-data commands from this ioctl. */
2179 switch (req_task
->data_phase
) {
2181 nsect
= taskout
/ ATA_SECT_SIZE
;
2182 reply
= (dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
);
2185 reply
= (dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
);
2187 case TASKFILE_NO_DATA
:
2188 reply
= (dd
->port
->rxfis
+ RX_FIS_D2H_REG
);
2195 /* Build the FIS. */
2196 memset(&fis
, 0, sizeof(struct host_to_dev_fis
));
2200 fis
.command
= req_task
->io_ports
[7];
2201 fis
.features
= req_task
->io_ports
[1];
2202 fis
.sect_count
= req_task
->io_ports
[2];
2203 fis
.lba_low
= req_task
->io_ports
[3];
2204 fis
.lba_mid
= req_task
->io_ports
[4];
2205 fis
.lba_hi
= req_task
->io_ports
[5];
2206 /* Clear the dev bit*/
2207 fis
.device
= req_task
->io_ports
[6] & ~0x10;
2209 if ((req_task
->in_flags
.all
== 0) && (req_task
->out_flags
.all
& 1)) {
2210 req_task
->in_flags
.all
=
2211 IDE_TASKFILE_STD_IN_FLAGS
|
2212 (IDE_HOB_STD_IN_FLAGS
<< 8);
2213 fis
.lba_low_ex
= req_task
->hob_ports
[3];
2214 fis
.lba_mid_ex
= req_task
->hob_ports
[4];
2215 fis
.lba_hi_ex
= req_task
->hob_ports
[5];
2216 fis
.features_ex
= req_task
->hob_ports
[1];
2217 fis
.sect_cnt_ex
= req_task
->hob_ports
[2];
2220 req_task
->in_flags
.all
= IDE_TASKFILE_STD_IN_FLAGS
;
2223 force_single_sector
= implicit_sector(fis
.command
, fis
.features
);
2225 if ((taskin
|| taskout
) && (!fis
.sect_count
)) {
2227 fis
.sect_count
= nsect
;
2229 if (!force_single_sector
) {
2230 dev_warn(&dd
->pdev
->dev
,
2231 "data movement but "
2232 "sect_count is 0\n");
2239 dbg_printk(MTIP_DRV_NAME
2240 " %s: cmd %x, feat %x, nsect %x,"
2241 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2252 /* check for erase mode support during secure erase.*/
2253 if ((fis
.command
== ATA_CMD_SEC_ERASE_UNIT
) && outbuf
&&
2254 (outbuf
[0] & MTIP_SEC_ERASE_MODE
)) {
2258 mtip_set_timeout(dd
, &fis
, &timeout
, erasemode
);
2260 /* Determine the correct transfer size.*/
2261 if (force_single_sector
)
2262 transfer_size
= ATA_SECT_SIZE
;
2264 transfer_size
= ATA_SECT_SIZE
* fis
.sect_count
;
2266 /* Execute the command.*/
2267 if (mtip_exec_internal_command(dd
->port
,
2279 task_file_data
= readl(dd
->port
->mmio
+PORT_TFDATA
);
2281 if ((req_task
->data_phase
== TASKFILE_IN
) && !(task_file_data
& 1)) {
2282 reply
= dd
->port
->rxfis
+ RX_FIS_PIO_SETUP
;
2283 req_task
->io_ports
[7] = reply
->control
;
2285 reply
= dd
->port
->rxfis
+ RX_FIS_D2H_REG
;
2286 req_task
->io_ports
[7] = reply
->command
;
2289 /* reclaim the DMA buffers.*/
2291 pci_unmap_single(dd
->pdev
, inbuf_dma
,
2292 taskin
, DMA_FROM_DEVICE
);
2294 pci_unmap_single(dd
->pdev
, outbuf_dma
,
2295 taskout
, DMA_TO_DEVICE
);
2299 /* return the ATA registers to the caller.*/
2300 req_task
->io_ports
[1] = reply
->features
;
2301 req_task
->io_ports
[2] = reply
->sect_count
;
2302 req_task
->io_ports
[3] = reply
->lba_low
;
2303 req_task
->io_ports
[4] = reply
->lba_mid
;
2304 req_task
->io_ports
[5] = reply
->lba_hi
;
2305 req_task
->io_ports
[6] = reply
->device
;
2307 if (req_task
->out_flags
.all
& 1) {
2309 req_task
->hob_ports
[3] = reply
->lba_low_ex
;
2310 req_task
->hob_ports
[4] = reply
->lba_mid_ex
;
2311 req_task
->hob_ports
[5] = reply
->lba_hi_ex
;
2312 req_task
->hob_ports
[1] = reply
->features_ex
;
2313 req_task
->hob_ports
[2] = reply
->sect_cnt_ex
;
2315 dbg_printk(MTIP_DRV_NAME
2316 " %s: Completion: stat %x,"
2317 "err %x, sect_cnt %x, lbalo %x,"
2318 "lbamid %x, lbahi %x, dev %x\n",
2320 req_task
->io_ports
[7],
2321 req_task
->io_ports
[1],
2322 req_task
->io_ports
[2],
2323 req_task
->io_ports
[3],
2324 req_task
->io_ports
[4],
2325 req_task
->io_ports
[5],
2326 req_task
->io_ports
[6]);
2329 if (copy_to_user(buf
+ outtotal
, outbuf
, taskout
)) {
2335 if (copy_to_user(buf
+ intotal
, inbuf
, taskin
)) {
2342 pci_unmap_single(dd
->pdev
, inbuf_dma
,
2343 taskin
, DMA_FROM_DEVICE
);
2345 pci_unmap_single(dd
->pdev
, outbuf_dma
,
2346 taskout
, DMA_TO_DEVICE
);
2354 * Handle IOCTL calls from the Block Layer.
2356 * This function is called by the Block Layer when it receives an IOCTL
2357 * command that it does not understand. If the IOCTL command is not supported
2358 * this function returns -ENOTTY.
2360 * @dd Pointer to the driver data structure.
2361 * @cmd IOCTL command passed from the Block Layer.
2362 * @arg IOCTL argument passed from the Block Layer.
2365 * 0 The IOCTL completed successfully.
2366 * -ENOTTY The specified command is not supported.
2367 * -EFAULT An error occurred copying data to a user space buffer.
2368 * -EIO An error occurred while executing the command.
2370 static int mtip_hw_ioctl(struct driver_data
*dd
, unsigned int cmd
,
2374 case HDIO_GET_IDENTITY
:
2376 if (copy_to_user((void __user
*)arg
, dd
->port
->identify
,
2377 sizeof(u16
) * ATA_ID_WORDS
))
2381 case HDIO_DRIVE_CMD
:
2383 u8 drive_command
[4];
2385 /* Copy the user command info to our buffer. */
2386 if (copy_from_user(drive_command
,
2387 (void __user
*) arg
,
2388 sizeof(drive_command
)))
2391 /* Execute the drive command. */
2392 if (exec_drive_command(dd
->port
,
2394 (void __user
*) (arg
+4)))
2397 /* Copy the status back to the users buffer. */
2398 if (copy_to_user((void __user
*) arg
,
2400 sizeof(drive_command
)))
2405 case HDIO_DRIVE_TASK
:
2407 u8 drive_command
[7];
2409 /* Copy the user command info to our buffer. */
2410 if (copy_from_user(drive_command
,
2411 (void __user
*) arg
,
2412 sizeof(drive_command
)))
2415 /* Execute the drive command. */
2416 if (exec_drive_task(dd
->port
, drive_command
))
2419 /* Copy the status back to the users buffer. */
2420 if (copy_to_user((void __user
*) arg
,
2422 sizeof(drive_command
)))
2427 case HDIO_DRIVE_TASKFILE
: {
2428 ide_task_request_t req_task
;
2431 if (copy_from_user(&req_task
, (void __user
*) arg
,
2435 outtotal
= sizeof(req_task
);
2437 ret
= exec_drive_taskfile(dd
, (void __user
*) arg
,
2438 &req_task
, outtotal
);
2440 if (copy_to_user((void __user
*) arg
, &req_task
,
2454 * Submit an IO to the hw
2456 * This function is called by the block layer to issue an io
2457 * to the device. Upon completion, the callback function will
2458 * be called with the data parameter passed as the callback data.
2460 * @dd Pointer to the driver data structure.
2461 * @start First sector to read.
2462 * @nsect Number of sectors to read.
2463 * @nents Number of entries in scatter list for the read command.
2464 * @tag The tag of this read command.
2465 * @callback Pointer to the function that should be called
2466 * when the read completes.
2467 * @data Callback data passed to the callback function
2468 * when the read completes.
2469 * @dir Direction (read or write)
2474 static void mtip_hw_submit_io(struct driver_data
*dd
, sector_t sector
,
2475 int nsect
, int nents
, int tag
, void *callback
,
2476 void *data
, int dir
)
2478 struct host_to_dev_fis
*fis
;
2479 struct mtip_port
*port
= dd
->port
;
2480 struct mtip_cmd
*command
= &port
->commands
[tag
];
2481 int dma_dir
= (dir
== READ
) ? DMA_FROM_DEVICE
: DMA_TO_DEVICE
;
2484 /* Map the scatter list for DMA access */
2485 nents
= dma_map_sg(&dd
->pdev
->dev
, command
->sg
, nents
, dma_dir
);
2487 command
->scatter_ents
= nents
;
2490 * The number of retries for this command before it is
2491 * reported as a failure to the upper layers.
2493 command
->retries
= MTIP_MAX_RETRIES
;
2496 fis
= command
->command
;
2500 (dir
== READ
? ATA_CMD_FPDMA_READ
: ATA_CMD_FPDMA_WRITE
);
2501 fis
->lba_low
= start
& 0xFF;
2502 fis
->lba_mid
= (start
>> 8) & 0xFF;
2503 fis
->lba_hi
= (start
>> 16) & 0xFF;
2504 fis
->lba_low_ex
= (start
>> 24) & 0xFF;
2505 fis
->lba_mid_ex
= (start
>> 32) & 0xFF;
2506 fis
->lba_hi_ex
= (start
>> 40) & 0xFF;
2507 fis
->device
= 1 << 6;
2508 fis
->features
= nsect
& 0xFF;
2509 fis
->features_ex
= (nsect
>> 8) & 0xFF;
2510 fis
->sect_count
= ((tag
<< 3) | (tag
>> 5));
2511 fis
->sect_cnt_ex
= 0;
2515 fill_command_sg(dd
, command
, nents
);
2517 /* Populate the command header */
2518 command
->command_header
->opts
=
2519 __force_bit2int
cpu_to_le32(
2520 (nents
<< 16) | 5 | AHCI_CMD_PREFETCH
);
2521 command
->command_header
->byte_count
= 0;
2524 * Set the completion function and data for the command
2525 * within this layer.
2527 command
->comp_data
= dd
;
2528 command
->comp_func
= mtip_async_complete
;
2529 command
->direction
= dma_dir
;
2532 * Set the completion function and data for the command passed
2533 * from the upper layer.
2535 command
->async_data
= data
;
2536 command
->async_callback
= callback
;
2539 * To prevent this command from being issued
2540 * if an internal command is in progress or error handling is active.
2542 if (port
->flags
& MTIP_PF_PAUSE_IO
) {
2543 set_bit(tag
, port
->cmds_to_issue
);
2544 set_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
);
2548 /* Issue the command to the hardware */
2549 mtip_issue_ncq_command(port
, tag
);
2555 * Release a command slot.
2557 * @dd Pointer to the driver data structure.
2563 static void mtip_hw_release_scatterlist(struct driver_data
*dd
, int tag
)
2565 release_slot(dd
->port
, tag
);
2569 * Obtain a command slot and return its associated scatter list.
2571 * @dd Pointer to the driver data structure.
2572 * @tag Pointer to an int that will receive the allocated command
2576 * Pointer to the scatter list for the allocated command slot
2577 * or NULL if no command slots are available.
2579 static struct scatterlist
*mtip_hw_get_scatterlist(struct driver_data
*dd
,
2583 * It is possible that, even with this semaphore, a thread
2584 * may think that no command slots are available. Therefore, we
2585 * need to make an attempt to get_slot().
2587 down(&dd
->port
->cmd_slot
);
2588 *tag
= get_slot(dd
->port
);
2590 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))) {
2591 up(&dd
->port
->cmd_slot
);
2594 if (unlikely(*tag
< 0)) {
2595 up(&dd
->port
->cmd_slot
);
2599 return dd
->port
->commands
[*tag
].sg
;
2603 * Sysfs status dump.
2605 * @dev Pointer to the device structure, passed by the kernrel.
2606 * @attr Pointer to the device_attribute structure passed by the kernel.
2607 * @buf Pointer to the char buffer that will receive the stats info.
2610 * The size, in bytes, of the data copied into buf.
2612 static ssize_t
mtip_hw_show_status(struct device
*dev
,
2613 struct device_attribute
*attr
,
2616 struct driver_data
*dd
= dev_to_disk(dev
)->private_data
;
2619 if (test_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
))
2620 size
+= sprintf(buf
, "%s", "thermal_shutdown\n");
2621 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
))
2622 size
+= sprintf(buf
, "%s", "write_protect\n");
2624 size
+= sprintf(buf
, "%s", "online\n");
2629 static DEVICE_ATTR(status
, S_IRUGO
, mtip_hw_show_status
, NULL
);
2631 static ssize_t
mtip_hw_read_registers(struct file
*f
, char __user
*ubuf
,
2632 size_t len
, loff_t
*offset
)
2634 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2635 char buf
[MTIP_DFS_MAX_BUF_SIZE
];
2636 u32 group_allocated
;
2643 size
+= sprintf(&buf
[size
], "H/ S ACTive : [ 0x");
2645 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2646 size
+= sprintf(&buf
[size
], "%08X ",
2647 readl(dd
->port
->s_active
[n
]));
2649 size
+= sprintf(&buf
[size
], "]\n");
2650 size
+= sprintf(&buf
[size
], "H/ Command Issue : [ 0x");
2652 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2653 size
+= sprintf(&buf
[size
], "%08X ",
2654 readl(dd
->port
->cmd_issue
[n
]));
2656 size
+= sprintf(&buf
[size
], "]\n");
2657 size
+= sprintf(&buf
[size
], "H/ Completed : [ 0x");
2659 for (n
= dd
->slot_groups
-1; n
>= 0; n
--)
2660 size
+= sprintf(&buf
[size
], "%08X ",
2661 readl(dd
->port
->completed
[n
]));
2663 size
+= sprintf(&buf
[size
], "]\n");
2664 size
+= sprintf(&buf
[size
], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2665 readl(dd
->port
->mmio
+ PORT_IRQ_STAT
));
2666 size
+= sprintf(&buf
[size
], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2667 readl(dd
->mmio
+ HOST_IRQ_STAT
));
2668 size
+= sprintf(&buf
[size
], "\n");
2670 size
+= sprintf(&buf
[size
], "L/ Allocated : [ 0x");
2672 for (n
= dd
->slot_groups
-1; n
>= 0; n
--) {
2673 if (sizeof(long) > sizeof(u32
))
2675 dd
->port
->allocated
[n
/2] >> (32*(n
&1));
2677 group_allocated
= dd
->port
->allocated
[n
];
2678 size
+= sprintf(&buf
[size
], "%08X ", group_allocated
);
2680 size
+= sprintf(&buf
[size
], "]\n");
2682 size
+= sprintf(&buf
[size
], "L/ Commands in Q : [ 0x");
2684 for (n
= dd
->slot_groups
-1; n
>= 0; n
--) {
2685 if (sizeof(long) > sizeof(u32
))
2687 dd
->port
->cmds_to_issue
[n
/2] >> (32*(n
&1));
2689 group_allocated
= dd
->port
->cmds_to_issue
[n
];
2690 size
+= sprintf(&buf
[size
], "%08X ", group_allocated
);
2692 size
+= sprintf(&buf
[size
], "]\n");
2694 *offset
= size
<= len
? size
: len
;
2695 size
= copy_to_user(ubuf
, buf
, *offset
);
2702 static ssize_t
mtip_hw_read_flags(struct file
*f
, char __user
*ubuf
,
2703 size_t len
, loff_t
*offset
)
2705 struct driver_data
*dd
= (struct driver_data
*)f
->private_data
;
2706 char buf
[MTIP_DFS_MAX_BUF_SIZE
];
2712 size
+= sprintf(&buf
[size
], "Flag-port : [ %08lX ]\n",
2714 size
+= sprintf(&buf
[size
], "Flag-dd : [ %08lX ]\n",
2717 *offset
= size
<= len
? size
: len
;
2718 size
= copy_to_user(ubuf
, buf
, *offset
);
2725 static const struct file_operations mtip_regs_fops
= {
2726 .owner
= THIS_MODULE
,
2727 .open
= simple_open
,
2728 .read
= mtip_hw_read_registers
,
2729 .llseek
= no_llseek
,
2732 static const struct file_operations mtip_flags_fops
= {
2733 .owner
= THIS_MODULE
,
2734 .open
= simple_open
,
2735 .read
= mtip_hw_read_flags
,
2736 .llseek
= no_llseek
,
2740 * Create the sysfs related attributes.
2742 * @dd Pointer to the driver data structure.
2743 * @kobj Pointer to the kobj for the block device.
2746 * 0 Operation completed successfully.
2747 * -EINVAL Invalid parameter.
2749 static int mtip_hw_sysfs_init(struct driver_data
*dd
, struct kobject
*kobj
)
2754 if (sysfs_create_file(kobj
, &dev_attr_status
.attr
))
2755 dev_warn(&dd
->pdev
->dev
,
2756 "Error creating 'status' sysfs entry\n");
2761 * Remove the sysfs related attributes.
2763 * @dd Pointer to the driver data structure.
2764 * @kobj Pointer to the kobj for the block device.
2767 * 0 Operation completed successfully.
2768 * -EINVAL Invalid parameter.
2770 static int mtip_hw_sysfs_exit(struct driver_data
*dd
, struct kobject
*kobj
)
2775 sysfs_remove_file(kobj
, &dev_attr_status
.attr
);
2780 static int mtip_hw_debugfs_init(struct driver_data
*dd
)
2785 dd
->dfs_node
= debugfs_create_dir(dd
->disk
->disk_name
, dfs_parent
);
2786 if (IS_ERR_OR_NULL(dd
->dfs_node
)) {
2787 dev_warn(&dd
->pdev
->dev
,
2788 "Error creating node %s under debugfs\n",
2789 dd
->disk
->disk_name
);
2790 dd
->dfs_node
= NULL
;
2794 debugfs_create_file("flags", S_IRUGO
, dd
->dfs_node
, dd
,
2796 debugfs_create_file("registers", S_IRUGO
, dd
->dfs_node
, dd
,
2802 static void mtip_hw_debugfs_exit(struct driver_data
*dd
)
2804 debugfs_remove_recursive(dd
->dfs_node
);
2809 * Perform any init/resume time hardware setup
2811 * @dd Pointer to the driver data structure.
2816 static inline void hba_setup(struct driver_data
*dd
)
2819 hwdata
= readl(dd
->mmio
+ HOST_HSORG
);
2821 /* interrupt bug workaround: use only 1 IS bit.*/
2823 HSORG_DISABLE_SLOTGRP_INTR
|
2824 HSORG_DISABLE_SLOTGRP_PXIS
,
2825 dd
->mmio
+ HOST_HSORG
);
2829 * Detect the details of the product, and store anything needed
2830 * into the driver data structure. This includes product type and
2831 * version and number of slot groups.
2833 * @dd Pointer to the driver data structure.
2838 static void mtip_detect_product(struct driver_data
*dd
)
2841 unsigned int rev
, slotgroups
;
2844 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2846 * [15:8] hardware/software interface rev#
2847 * [ 3] asic-style interface
2848 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2850 hwdata
= readl(dd
->mmio
+ HOST_HSORG
);
2852 dd
->product_type
= MTIP_PRODUCT_UNKNOWN
;
2853 dd
->slot_groups
= 1;
2856 dd
->product_type
= MTIP_PRODUCT_ASICFPGA
;
2857 rev
= (hwdata
& HSORG_HWREV
) >> 8;
2858 slotgroups
= (hwdata
& HSORG_SLOTGROUPS
) + 1;
2859 dev_info(&dd
->pdev
->dev
,
2860 "ASIC-FPGA design, HS rev 0x%x, "
2861 "%i slot groups [%i slots]\n",
2866 if (slotgroups
> MTIP_MAX_SLOT_GROUPS
) {
2867 dev_warn(&dd
->pdev
->dev
,
2868 "Warning: driver only supports "
2869 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS
);
2870 slotgroups
= MTIP_MAX_SLOT_GROUPS
;
2872 dd
->slot_groups
= slotgroups
;
2876 dev_warn(&dd
->pdev
->dev
, "Unrecognized product id\n");
2880 * Blocking wait for FTL rebuild to complete
2882 * @dd Pointer to the DRIVER_DATA structure.
2885 * 0 FTL rebuild completed successfully
2886 * -EFAULT FTL rebuild error/timeout/interruption
2888 static int mtip_ftl_rebuild_poll(struct driver_data
*dd
)
2890 unsigned long timeout
, cnt
= 0, start
;
2892 dev_warn(&dd
->pdev
->dev
,
2893 "FTL rebuild in progress. Polling for completion.\n");
2896 timeout
= jiffies
+ msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS
);
2899 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
2902 if (mtip_check_surprise_removal(dd
->pdev
))
2905 if (mtip_get_identify(dd
->port
, NULL
) < 0)
2908 if (*(dd
->port
->identify
+ MTIP_FTL_REBUILD_OFFSET
) ==
2909 MTIP_FTL_REBUILD_MAGIC
) {
2911 /* Print message every 3 minutes */
2913 dev_warn(&dd
->pdev
->dev
,
2914 "FTL rebuild in progress (%d secs).\n",
2915 jiffies_to_msecs(jiffies
- start
) / 1000);
2919 dev_warn(&dd
->pdev
->dev
,
2920 "FTL rebuild complete (%d secs).\n",
2921 jiffies_to_msecs(jiffies
- start
) / 1000);
2922 mtip_block_initialize(dd
);
2926 } while (time_before(jiffies
, timeout
));
2928 /* Check for timeout */
2929 dev_err(&dd
->pdev
->dev
,
2930 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2931 jiffies_to_msecs(jiffies
- start
) / 1000);
2936 * service thread to issue queued commands
2938 * @data Pointer to the driver data structure.
2944 static int mtip_service_thread(void *data
)
2946 struct driver_data
*dd
= (struct driver_data
*)data
;
2947 unsigned long slot
, slot_start
, slot_wrap
;
2948 unsigned int num_cmd_slots
= dd
->slot_groups
* 32;
2949 struct mtip_port
*port
= dd
->port
;
2953 * the condition is to check neither an internal command is
2954 * is in progress nor error handling is active
2956 wait_event_interruptible(port
->svc_wait
, (port
->flags
) &&
2957 !(port
->flags
& MTIP_PF_PAUSE_IO
));
2959 if (kthread_should_stop())
2962 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
2966 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
);
2967 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
)) {
2969 /* used to restrict the loop to one iteration */
2970 slot_start
= num_cmd_slots
;
2973 slot
= find_next_bit(port
->cmds_to_issue
,
2974 num_cmd_slots
, slot
);
2975 if (slot_wrap
== 1) {
2976 if ((slot_start
>= slot
) ||
2977 (slot
>= num_cmd_slots
))
2980 if (unlikely(slot_start
== num_cmd_slots
))
2983 if (unlikely(slot
== num_cmd_slots
)) {
2989 /* Issue the command to the hardware */
2990 mtip_issue_ncq_command(port
, slot
);
2992 clear_bit(slot
, port
->cmds_to_issue
);
2995 clear_bit(MTIP_PF_ISSUE_CMDS_BIT
, &port
->flags
);
2996 } else if (test_bit(MTIP_PF_REBUILD_BIT
, &port
->flags
)) {
2997 if (!mtip_ftl_rebuild_poll(dd
))
2998 set_bit(MTIP_DDF_REBUILD_FAILED_BIT
,
3000 clear_bit(MTIP_PF_REBUILD_BIT
, &port
->flags
);
3002 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT
, &port
->flags
);
3004 if (test_bit(MTIP_PF_SVC_THD_STOP_BIT
, &port
->flags
))
3011 * Called once for each card.
3013 * @dd Pointer to the driver data structure.
3016 * 0 on success, else an error code.
3018 static int mtip_hw_init(struct driver_data
*dd
)
3022 unsigned int num_command_slots
;
3023 unsigned long timeout
, timetaken
;
3025 struct smart_attr attr242
;
3027 dd
->mmio
= pcim_iomap_table(dd
->pdev
)[MTIP_ABAR
];
3029 mtip_detect_product(dd
);
3030 if (dd
->product_type
== MTIP_PRODUCT_UNKNOWN
) {
3034 num_command_slots
= dd
->slot_groups
* 32;
3038 dd
->port
= kzalloc_node(sizeof(struct mtip_port
), GFP_KERNEL
,
3041 dev_err(&dd
->pdev
->dev
,
3042 "Memory allocation: port structure\n");
3046 /* Continue workqueue setup */
3047 for (i
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++)
3048 dd
->work
[i
].port
= dd
->port
;
3050 /* Counting semaphore to track command slot usage */
3051 sema_init(&dd
->port
->cmd_slot
, num_command_slots
- 1);
3053 /* Spinlock to prevent concurrent issue */
3054 for (i
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++)
3055 spin_lock_init(&dd
->port
->cmd_issue_lock
[i
]);
3057 /* Set the port mmio base address. */
3058 dd
->port
->mmio
= dd
->mmio
+ PORT_OFFSET
;
3061 /* Allocate memory for the command list. */
3062 dd
->port
->command_list
=
3063 dmam_alloc_coherent(&dd
->pdev
->dev
,
3064 HW_PORT_PRIV_DMA_SZ
+ (ATA_SECT_SIZE
* 4),
3065 &dd
->port
->command_list_dma
,
3067 if (!dd
->port
->command_list
) {
3068 dev_err(&dd
->pdev
->dev
,
3069 "Memory allocation: command list\n");
3074 /* Clear the memory we have allocated. */
3075 memset(dd
->port
->command_list
,
3077 HW_PORT_PRIV_DMA_SZ
+ (ATA_SECT_SIZE
* 4));
3079 /* Setup the addresse of the RX FIS. */
3080 dd
->port
->rxfis
= dd
->port
->command_list
+ HW_CMD_SLOT_SZ
;
3081 dd
->port
->rxfis_dma
= dd
->port
->command_list_dma
+ HW_CMD_SLOT_SZ
;
3083 /* Setup the address of the command tables. */
3084 dd
->port
->command_table
= dd
->port
->rxfis
+ AHCI_RX_FIS_SZ
;
3085 dd
->port
->command_tbl_dma
= dd
->port
->rxfis_dma
+ AHCI_RX_FIS_SZ
;
3087 /* Setup the address of the identify data. */
3088 dd
->port
->identify
= dd
->port
->command_table
+
3090 dd
->port
->identify_dma
= dd
->port
->command_tbl_dma
+
3093 /* Setup the address of the sector buffer - for some non-ncq cmds */
3094 dd
->port
->sector_buffer
= (void *) dd
->port
->identify
+ ATA_SECT_SIZE
;
3095 dd
->port
->sector_buffer_dma
= dd
->port
->identify_dma
+ ATA_SECT_SIZE
;
3097 /* Setup the address of the log buf - for read log command */
3098 dd
->port
->log_buf
= (void *)dd
->port
->sector_buffer
+ ATA_SECT_SIZE
;
3099 dd
->port
->log_buf_dma
= dd
->port
->sector_buffer_dma
+ ATA_SECT_SIZE
;
3101 /* Setup the address of the smart buf - for smart read data command */
3102 dd
->port
->smart_buf
= (void *)dd
->port
->log_buf
+ ATA_SECT_SIZE
;
3103 dd
->port
->smart_buf_dma
= dd
->port
->log_buf_dma
+ ATA_SECT_SIZE
;
3106 /* Point the command headers at the command tables. */
3107 for (i
= 0; i
< num_command_slots
; i
++) {
3108 dd
->port
->commands
[i
].command_header
=
3109 dd
->port
->command_list
+
3110 (sizeof(struct mtip_cmd_hdr
) * i
);
3111 dd
->port
->commands
[i
].command_header_dma
=
3112 dd
->port
->command_list_dma
+
3113 (sizeof(struct mtip_cmd_hdr
) * i
);
3115 dd
->port
->commands
[i
].command
=
3116 dd
->port
->command_table
+ (HW_CMD_TBL_SZ
* i
);
3117 dd
->port
->commands
[i
].command_dma
=
3118 dd
->port
->command_tbl_dma
+ (HW_CMD_TBL_SZ
* i
);
3120 if (readl(dd
->mmio
+ HOST_CAP
) & HOST_CAP_64
)
3121 dd
->port
->commands
[i
].command_header
->ctbau
=
3122 __force_bit2int
cpu_to_le32(
3123 (dd
->port
->commands
[i
].command_dma
>> 16) >> 16);
3124 dd
->port
->commands
[i
].command_header
->ctba
=
3125 __force_bit2int
cpu_to_le32(
3126 dd
->port
->commands
[i
].command_dma
& 0xFFFFFFFF);
3129 * If this is not done, a bug is reported by the stock
3130 * FC11 i386. Due to the fact that it has lots of kernel
3131 * debugging enabled.
3133 sg_init_table(dd
->port
->commands
[i
].sg
, MTIP_MAX_SG
);
3135 /* Mark all commands as currently inactive.*/
3136 atomic_set(&dd
->port
->commands
[i
].active
, 0);
3139 /* Setup the pointers to the extended s_active and CI registers. */
3140 for (i
= 0; i
< dd
->slot_groups
; i
++) {
3141 dd
->port
->s_active
[i
] =
3142 dd
->port
->mmio
+ i
*0x80 + PORT_SCR_ACT
;
3143 dd
->port
->cmd_issue
[i
] =
3144 dd
->port
->mmio
+ i
*0x80 + PORT_COMMAND_ISSUE
;
3145 dd
->port
->completed
[i
] =
3146 dd
->port
->mmio
+ i
*0x80 + PORT_SDBV
;
3149 timetaken
= jiffies
;
3150 timeout
= jiffies
+ msecs_to_jiffies(30000);
3151 while (((readl(dd
->port
->mmio
+ PORT_SCR_STAT
) & 0x0F) != 0x03) &&
3152 time_before(jiffies
, timeout
)) {
3155 if (unlikely(mtip_check_surprise_removal(dd
->pdev
))) {
3156 timetaken
= jiffies
- timetaken
;
3157 dev_warn(&dd
->pdev
->dev
,
3158 "Surprise removal detected at %u ms\n",
3159 jiffies_to_msecs(timetaken
));
3163 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
))) {
3164 timetaken
= jiffies
- timetaken
;
3165 dev_warn(&dd
->pdev
->dev
,
3166 "Removal detected at %u ms\n",
3167 jiffies_to_msecs(timetaken
));
3172 /* Conditionally reset the HBA. */
3173 if (!(readl(dd
->mmio
+ HOST_CAP
) & HOST_CAP_NZDMA
)) {
3174 if (mtip_hba_reset(dd
) < 0) {
3175 dev_err(&dd
->pdev
->dev
,
3176 "Card did not reset within timeout\n");
3181 /* Clear any pending interrupts on the HBA */
3182 writel(readl(dd
->mmio
+ HOST_IRQ_STAT
),
3183 dd
->mmio
+ HOST_IRQ_STAT
);
3186 mtip_init_port(dd
->port
);
3187 mtip_start_port(dd
->port
);
3189 /* Setup the ISR and enable interrupts. */
3190 rv
= devm_request_irq(&dd
->pdev
->dev
,
3194 dev_driver_string(&dd
->pdev
->dev
),
3198 dev_err(&dd
->pdev
->dev
,
3199 "Unable to allocate IRQ %d\n", dd
->pdev
->irq
);
3202 irq_set_affinity_hint(dd
->pdev
->irq
, get_cpu_mask(dd
->isr_binding
));
3204 /* Enable interrupts on the HBA. */
3205 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
3206 dd
->mmio
+ HOST_CTL
);
3208 init_timer(&dd
->port
->cmd_timer
);
3209 init_waitqueue_head(&dd
->port
->svc_wait
);
3211 dd
->port
->cmd_timer
.data
= (unsigned long int) dd
->port
;
3212 dd
->port
->cmd_timer
.function
= mtip_timeout_function
;
3213 mod_timer(&dd
->port
->cmd_timer
,
3214 jiffies
+ msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD
));
3217 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)) {
3222 if (mtip_get_identify(dd
->port
, NULL
) < 0) {
3227 if (*(dd
->port
->identify
+ MTIP_FTL_REBUILD_OFFSET
) ==
3228 MTIP_FTL_REBUILD_MAGIC
) {
3229 set_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
);
3230 return MTIP_FTL_REBUILD_MAGIC
;
3232 mtip_dump_identify(dd
->port
);
3234 /* check write protect, over temp and rebuild statuses */
3235 rv
= mtip_read_log_page(dd
->port
, ATA_LOG_SATA_NCQ
,
3237 dd
->port
->log_buf_dma
, 1);
3239 dev_warn(&dd
->pdev
->dev
,
3240 "Error in READ LOG EXT (10h) command\n");
3241 /* non-critical error, don't fail the load */
3243 buf
= (unsigned char *)dd
->port
->log_buf
;
3244 if (buf
[259] & 0x1) {
3245 dev_info(&dd
->pdev
->dev
,
3246 "Write protect bit is set.\n");
3247 set_bit(MTIP_DDF_WRITE_PROTECT_BIT
, &dd
->dd_flag
);
3249 if (buf
[288] == 0xF7) {
3250 dev_info(&dd
->pdev
->dev
,
3251 "Exceeded Tmax, drive in thermal shutdown.\n");
3252 set_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
);
3254 if (buf
[288] == 0xBF) {
3255 dev_info(&dd
->pdev
->dev
,
3256 "Drive indicates rebuild has failed.\n");
3261 /* get write protect progess */
3262 memset(&attr242
, 0, sizeof(struct smart_attr
));
3263 if (mtip_get_smart_attr(dd
->port
, 242, &attr242
))
3264 dev_warn(&dd
->pdev
->dev
,
3265 "Unable to check write protect progress\n");
3267 dev_info(&dd
->pdev
->dev
,
3268 "Write protect progress: %u%% (%u blocks)\n",
3269 attr242
.cur
, le32_to_cpu(attr242
.data
));
3273 del_timer_sync(&dd
->port
->cmd_timer
);
3275 /* Disable interrupts on the HBA. */
3276 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3277 dd
->mmio
+ HOST_CTL
);
3279 /* Release the IRQ. */
3280 irq_set_affinity_hint(dd
->pdev
->irq
, NULL
);
3281 devm_free_irq(&dd
->pdev
->dev
, dd
->pdev
->irq
, dd
);
3284 mtip_deinit_port(dd
->port
);
3286 /* Free the command/command header memory. */
3287 dmam_free_coherent(&dd
->pdev
->dev
,
3288 HW_PORT_PRIV_DMA_SZ
+ (ATA_SECT_SIZE
* 4),
3289 dd
->port
->command_list
,
3290 dd
->port
->command_list_dma
);
3292 /* Free the memory allocated for the for structure. */
3299 * Called to deinitialize an interface.
3301 * @dd Pointer to the driver data structure.
3306 static int mtip_hw_exit(struct driver_data
*dd
)
3309 * Send standby immediate (E0h) to the drive so that it
3312 if (!test_bit(MTIP_DDF_CLEANUP_BIT
, &dd
->dd_flag
)) {
3314 if (!test_bit(MTIP_PF_REBUILD_BIT
, &dd
->port
->flags
))
3315 if (mtip_standby_immediate(dd
->port
))
3316 dev_warn(&dd
->pdev
->dev
,
3317 "STANDBY IMMEDIATE failed\n");
3319 /* de-initialize the port. */
3320 mtip_deinit_port(dd
->port
);
3322 /* Disable interrupts on the HBA. */
3323 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3324 dd
->mmio
+ HOST_CTL
);
3327 del_timer_sync(&dd
->port
->cmd_timer
);
3329 /* Release the IRQ. */
3330 irq_set_affinity_hint(dd
->pdev
->irq
, NULL
);
3331 devm_free_irq(&dd
->pdev
->dev
, dd
->pdev
->irq
, dd
);
3333 /* Free the command/command header memory. */
3334 dmam_free_coherent(&dd
->pdev
->dev
,
3335 HW_PORT_PRIV_DMA_SZ
+ (ATA_SECT_SIZE
* 4),
3336 dd
->port
->command_list
,
3337 dd
->port
->command_list_dma
);
3338 /* Free the memory allocated for the for structure. */
3345 * Issue a Standby Immediate command to the device.
3347 * This function is called by the Block Layer just before the
3348 * system powers off during a shutdown.
3350 * @dd Pointer to the driver data structure.
3355 static int mtip_hw_shutdown(struct driver_data
*dd
)
3358 * Send standby immediate (E0h) to the drive so that it
3361 mtip_standby_immediate(dd
->port
);
3369 * This function is called by the Block Layer just before the
3370 * system hibernates.
3372 * @dd Pointer to the driver data structure.
3375 * 0 Suspend was successful
3376 * -EFAULT Suspend was not successful
3378 static int mtip_hw_suspend(struct driver_data
*dd
)
3381 * Send standby immediate (E0h) to the drive
3382 * so that it saves its state.
3384 if (mtip_standby_immediate(dd
->port
) != 0) {
3385 dev_err(&dd
->pdev
->dev
,
3386 "Failed standby-immediate command\n");
3390 /* Disable interrupts on the HBA.*/
3391 writel(readl(dd
->mmio
+ HOST_CTL
) & ~HOST_IRQ_EN
,
3392 dd
->mmio
+ HOST_CTL
);
3393 mtip_deinit_port(dd
->port
);
3401 * This function is called by the Block Layer as the
3404 * @dd Pointer to the driver data structure.
3407 * 0 Resume was successful
3408 * -EFAULT Resume was not successful
3410 static int mtip_hw_resume(struct driver_data
*dd
)
3412 /* Perform any needed hardware setup steps */
3416 if (mtip_hba_reset(dd
) != 0) {
3417 dev_err(&dd
->pdev
->dev
,
3418 "Unable to reset the HBA\n");
3423 * Enable the port, DMA engine, and FIS reception specific
3424 * h/w in controller.
3426 mtip_init_port(dd
->port
);
3427 mtip_start_port(dd
->port
);
3429 /* Enable interrupts on the HBA.*/
3430 writel(readl(dd
->mmio
+ HOST_CTL
) | HOST_IRQ_EN
,
3431 dd
->mmio
+ HOST_CTL
);
3437 * Helper function for reusing disk name
3438 * upon hot insertion.
3440 static int rssd_disk_name_format(char *prefix
,
3445 const int base
= 'z' - 'a' + 1;
3446 char *begin
= buf
+ strlen(prefix
);
3447 char *end
= buf
+ buflen
;
3457 *--p
= 'a' + (index
% unit
);
3458 index
= (index
/ unit
) - 1;
3459 } while (index
>= 0);
3461 memmove(begin
, p
, end
- p
);
3462 memcpy(buf
, prefix
, strlen(prefix
));
3468 * Block layer IOCTL handler.
3470 * @dev Pointer to the block_device structure.
3472 * @cmd IOCTL command passed from the user application.
3473 * @arg Argument passed from the user application.
3476 * 0 IOCTL completed successfully.
3477 * -ENOTTY IOCTL not supported or invalid driver data
3478 * structure pointer.
3480 static int mtip_block_ioctl(struct block_device
*dev
,
3485 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3487 if (!capable(CAP_SYS_ADMIN
))
3493 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)))
3500 return mtip_hw_ioctl(dd
, cmd
, arg
);
3504 #ifdef CONFIG_COMPAT
3506 * Block layer compat IOCTL handler.
3508 * @dev Pointer to the block_device structure.
3510 * @cmd IOCTL command passed from the user application.
3511 * @arg Argument passed from the user application.
3514 * 0 IOCTL completed successfully.
3515 * -ENOTTY IOCTL not supported or invalid driver data
3516 * structure pointer.
3518 static int mtip_block_compat_ioctl(struct block_device
*dev
,
3523 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3525 if (!capable(CAP_SYS_ADMIN
))
3531 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
)))
3537 case HDIO_DRIVE_TASKFILE
: {
3538 struct mtip_compat_ide_task_request_s __user
*compat_req_task
;
3539 ide_task_request_t req_task
;
3540 int compat_tasksize
, outtotal
, ret
;
3543 sizeof(struct mtip_compat_ide_task_request_s
);
3546 (struct mtip_compat_ide_task_request_s __user
*) arg
;
3548 if (copy_from_user(&req_task
, (void __user
*) arg
,
3549 compat_tasksize
- (2 * sizeof(compat_long_t
))))
3552 if (get_user(req_task
.out_size
, &compat_req_task
->out_size
))
3555 if (get_user(req_task
.in_size
, &compat_req_task
->in_size
))
3558 outtotal
= sizeof(struct mtip_compat_ide_task_request_s
);
3560 ret
= exec_drive_taskfile(dd
, (void __user
*) arg
,
3561 &req_task
, outtotal
);
3563 if (copy_to_user((void __user
*) arg
, &req_task
,
3565 (2 * sizeof(compat_long_t
))))
3568 if (put_user(req_task
.out_size
, &compat_req_task
->out_size
))
3571 if (put_user(req_task
.in_size
, &compat_req_task
->in_size
))
3577 return mtip_hw_ioctl(dd
, cmd
, arg
);
3583 * Obtain the geometry of the device.
3585 * You may think that this function is obsolete, but some applications,
3586 * fdisk for example still used CHS values. This function describes the
3587 * device as having 224 heads and 56 sectors per cylinder. These values are
3588 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3589 * partition is described in terms of a start and end cylinder this means
3590 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3591 * affects performance.
3593 * @dev Pointer to the block_device strucutre.
3594 * @geo Pointer to a hd_geometry structure.
3597 * 0 Operation completed successfully.
3598 * -ENOTTY An error occurred while reading the drive capacity.
3600 static int mtip_block_getgeo(struct block_device
*dev
,
3601 struct hd_geometry
*geo
)
3603 struct driver_data
*dd
= dev
->bd_disk
->private_data
;
3609 if (!(mtip_hw_get_capacity(dd
, &capacity
))) {
3610 dev_warn(&dd
->pdev
->dev
,
3611 "Could not get drive capacity.\n");
3617 sector_div(capacity
, (geo
->heads
* geo
->sectors
));
3618 geo
->cylinders
= capacity
;
3623 * Block device operation function.
3625 * This structure contains pointers to the functions required by the block
3628 static const struct block_device_operations mtip_block_ops
= {
3629 .ioctl
= mtip_block_ioctl
,
3630 #ifdef CONFIG_COMPAT
3631 .compat_ioctl
= mtip_block_compat_ioctl
,
3633 .getgeo
= mtip_block_getgeo
,
3634 .owner
= THIS_MODULE
3638 * Block layer make request function.
3640 * This function is called by the kernel to process a BIO for
3643 * @queue Pointer to the request queue. Unused other than to obtain
3644 * the driver data structure.
3645 * @bio Pointer to the BIO.
3648 static void mtip_make_request(struct request_queue
*queue
, struct bio
*bio
)
3650 struct driver_data
*dd
= queue
->queuedata
;
3651 struct scatterlist
*sg
;
3652 struct bio_vec
*bvec
;
3656 if (unlikely(dd
->dd_flag
& MTIP_DDF_STOP_IO
)) {
3657 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT
,
3659 bio_endio(bio
, -ENXIO
);
3662 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT
, &dd
->dd_flag
))) {
3663 bio_endio(bio
, -ENODATA
);
3666 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT
,
3668 bio_data_dir(bio
))) {
3669 bio_endio(bio
, -ENODATA
);
3672 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT
, &dd
->dd_flag
))) {
3673 bio_endio(bio
, -ENODATA
);
3678 if (unlikely(!bio_has_data(bio
))) {
3679 blk_queue_flush(queue
, 0);
3684 sg
= mtip_hw_get_scatterlist(dd
, &tag
);
3685 if (likely(sg
!= NULL
)) {
3686 blk_queue_bounce(queue
, &bio
);
3688 if (unlikely((bio
)->bi_vcnt
> MTIP_MAX_SG
)) {
3689 dev_warn(&dd
->pdev
->dev
,
3690 "Maximum number of SGL entries exceeded\n");
3692 mtip_hw_release_scatterlist(dd
, tag
);
3696 /* Create the scatter list for this bio. */
3697 bio_for_each_segment(bvec
, bio
, nents
) {
3698 sg_set_page(&sg
[nents
],
3704 /* Issue the read/write. */
3705 mtip_hw_submit_io(dd
,
3718 * Block layer initialization function.
3720 * This function is called once by the PCI layer for each P320
3721 * device that is connected to the system.
3723 * @dd Pointer to the driver data structure.
3726 * 0 on success else an error code.
3728 static int mtip_block_initialize(struct driver_data
*dd
)
3730 int rv
= 0, wait_for_rebuild
= 0;
3732 unsigned int index
= 0;
3733 struct kobject
*kobj
;
3734 unsigned char thd_name
[16];
3737 goto skip_create_disk
; /* hw init done, before rebuild */
3739 /* Initialize the protocol layer. */
3740 wait_for_rebuild
= mtip_hw_init(dd
);
3741 if (wait_for_rebuild
< 0) {
3742 dev_err(&dd
->pdev
->dev
,
3743 "Protocol layer initialization failed\n");
3745 goto protocol_init_error
;
3748 dd
->disk
= alloc_disk_node(MTIP_MAX_MINORS
, dd
->numa_node
);
3749 if (dd
->disk
== NULL
) {
3750 dev_err(&dd
->pdev
->dev
,
3751 "Unable to allocate gendisk structure\n");
3753 goto alloc_disk_error
;
3756 /* Generate the disk name, implemented same as in sd.c */
3758 if (!ida_pre_get(&rssd_index_ida
, GFP_KERNEL
))
3761 spin_lock(&rssd_index_lock
);
3762 rv
= ida_get_new(&rssd_index_ida
, &index
);
3763 spin_unlock(&rssd_index_lock
);
3764 } while (rv
== -EAGAIN
);
3769 rv
= rssd_disk_name_format("rssd",
3771 dd
->disk
->disk_name
,
3774 goto disk_index_error
;
3776 dd
->disk
->driverfs_dev
= &dd
->pdev
->dev
;
3777 dd
->disk
->major
= dd
->major
;
3778 dd
->disk
->first_minor
= dd
->instance
* MTIP_MAX_MINORS
;
3779 dd
->disk
->fops
= &mtip_block_ops
;
3780 dd
->disk
->private_data
= dd
;
3784 * if rebuild pending, start the service thread, and delay the block
3785 * queue creation and add_disk()
3787 if (wait_for_rebuild
== MTIP_FTL_REBUILD_MAGIC
)
3788 goto start_service_thread
;
3791 /* Allocate the request queue. */
3792 dd
->queue
= blk_alloc_queue_node(GFP_KERNEL
, dd
->numa_node
);
3793 if (dd
->queue
== NULL
) {
3794 dev_err(&dd
->pdev
->dev
,
3795 "Unable to allocate request queue\n");
3797 goto block_queue_alloc_init_error
;
3800 /* Attach our request function to the request queue. */
3801 blk_queue_make_request(dd
->queue
, mtip_make_request
);
3803 dd
->disk
->queue
= dd
->queue
;
3804 dd
->queue
->queuedata
= dd
;
3806 /* Set device limits. */
3807 set_bit(QUEUE_FLAG_NONROT
, &dd
->queue
->queue_flags
);
3808 blk_queue_max_segments(dd
->queue
, MTIP_MAX_SG
);
3809 blk_queue_physical_block_size(dd
->queue
, 4096);
3810 blk_queue_max_hw_sectors(dd
->queue
, 0xffff);
3811 blk_queue_max_segment_size(dd
->queue
, 0x400000);
3812 blk_queue_io_min(dd
->queue
, 4096);
3815 * write back cache is not supported in the device. FUA depends on
3816 * write back cache support, hence setting flush support to zero.
3818 blk_queue_flush(dd
->queue
, 0);
3820 /* Set the capacity of the device in 512 byte sectors. */
3821 if (!(mtip_hw_get_capacity(dd
, &capacity
))) {
3822 dev_warn(&dd
->pdev
->dev
,
3823 "Could not read drive capacity\n");
3825 goto read_capacity_error
;
3827 set_capacity(dd
->disk
, capacity
);
3829 /* Enable the block device and add it to /dev */
3833 * Now that the disk is active, initialize any sysfs attributes
3834 * managed by the protocol layer.
3836 kobj
= kobject_get(&disk_to_dev(dd
->disk
)->kobj
);
3838 mtip_hw_sysfs_init(dd
, kobj
);
3841 mtip_hw_debugfs_init(dd
);
3843 if (dd
->mtip_svc_handler
) {
3844 set_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
);
3845 return rv
; /* service thread created for handling rebuild */
3848 start_service_thread
:
3849 sprintf(thd_name
, "mtip_svc_thd_%02d", index
);
3850 dd
->mtip_svc_handler
= kthread_create_on_node(mtip_service_thread
,
3851 dd
, dd
->numa_node
, thd_name
);
3853 if (IS_ERR(dd
->mtip_svc_handler
)) {
3854 dev_err(&dd
->pdev
->dev
, "service thread failed to start\n");
3855 dd
->mtip_svc_handler
= NULL
;
3857 goto kthread_run_error
;
3859 wake_up_process(dd
->mtip_svc_handler
);
3860 if (wait_for_rebuild
== MTIP_FTL_REBUILD_MAGIC
)
3861 rv
= wait_for_rebuild
;
3866 mtip_hw_debugfs_exit(dd
);
3868 /* Delete our gendisk. This also removes the device from /dev */
3869 del_gendisk(dd
->disk
);
3871 read_capacity_error
:
3872 blk_cleanup_queue(dd
->queue
);
3874 block_queue_alloc_init_error
:
3876 spin_lock(&rssd_index_lock
);
3877 ida_remove(&rssd_index_ida
, index
);
3878 spin_unlock(&rssd_index_lock
);
3884 mtip_hw_exit(dd
); /* De-initialize the protocol layer. */
3886 protocol_init_error
:
3891 * Block layer deinitialization function.
3893 * Called by the PCI layer as each P320 device is removed.
3895 * @dd Pointer to the driver data structure.
3900 static int mtip_block_remove(struct driver_data
*dd
)
3902 struct kobject
*kobj
;
3904 if (dd
->mtip_svc_handler
) {
3905 set_bit(MTIP_PF_SVC_THD_STOP_BIT
, &dd
->port
->flags
);
3906 wake_up_interruptible(&dd
->port
->svc_wait
);
3907 kthread_stop(dd
->mtip_svc_handler
);
3910 /* Clean up the sysfs attributes, if created */
3911 if (test_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
)) {
3912 kobj
= kobject_get(&disk_to_dev(dd
->disk
)->kobj
);
3914 mtip_hw_sysfs_exit(dd
, kobj
);
3918 mtip_hw_debugfs_exit(dd
);
3921 * Delete our gendisk structure. This also removes the device
3924 del_gendisk(dd
->disk
);
3926 spin_lock(&rssd_index_lock
);
3927 ida_remove(&rssd_index_ida
, dd
->index
);
3928 spin_unlock(&rssd_index_lock
);
3930 blk_cleanup_queue(dd
->queue
);
3934 /* De-initialize the protocol layer. */
3941 * Function called by the PCI layer when just before the
3942 * machine shuts down.
3944 * If a protocol layer shutdown function is present it will be called
3947 * @dd Pointer to the driver data structure.
3952 static int mtip_block_shutdown(struct driver_data
*dd
)
3954 dev_info(&dd
->pdev
->dev
,
3955 "Shutting down %s ...\n", dd
->disk
->disk_name
);
3957 /* Delete our gendisk structure, and cleanup the blk queue. */
3958 del_gendisk(dd
->disk
);
3960 spin_lock(&rssd_index_lock
);
3961 ida_remove(&rssd_index_ida
, dd
->index
);
3962 spin_unlock(&rssd_index_lock
);
3964 blk_cleanup_queue(dd
->queue
);
3968 mtip_hw_shutdown(dd
);
3972 static int mtip_block_suspend(struct driver_data
*dd
)
3974 dev_info(&dd
->pdev
->dev
,
3975 "Suspending %s ...\n", dd
->disk
->disk_name
);
3976 mtip_hw_suspend(dd
);
3980 static int mtip_block_resume(struct driver_data
*dd
)
3982 dev_info(&dd
->pdev
->dev
, "Resuming %s ...\n",
3983 dd
->disk
->disk_name
);
3988 static void drop_cpu(int cpu
)
3993 static int get_least_used_cpu_on_node(int node
)
3995 int cpu
, least_used_cpu
, least_cnt
;
3996 const struct cpumask
*node_mask
;
3998 node_mask
= cpumask_of_node(node
);
3999 least_used_cpu
= cpumask_first(node_mask
);
4000 least_cnt
= cpu_use
[least_used_cpu
];
4001 cpu
= least_used_cpu
;
4003 for_each_cpu(cpu
, node_mask
) {
4004 if (cpu_use
[cpu
] < least_cnt
) {
4005 least_used_cpu
= cpu
;
4006 least_cnt
= cpu_use
[cpu
];
4009 cpu_use
[least_used_cpu
]++;
4010 return least_used_cpu
;
4013 /* Helper for selecting a node in round robin mode */
4014 static inline int mtip_get_next_rr_node(void)
4016 static int next_node
= -1;
4018 if (next_node
== -1) {
4019 next_node
= first_online_node
;
4023 next_node
= next_online_node(next_node
);
4024 if (next_node
== MAX_NUMNODES
)
4025 next_node
= first_online_node
;
4039 * Called for each supported PCI device detected.
4041 * This function allocates the private data structure, enables the
4042 * PCI device and then calls the block layer initialization function.
4045 * 0 on success else an error code.
4047 static int mtip_pci_probe(struct pci_dev
*pdev
,
4048 const struct pci_device_id
*ent
)
4051 struct driver_data
*dd
= NULL
;
4053 const struct cpumask
*node_mask
;
4054 int cpu
, i
= 0, j
= 0;
4055 int my_node
= NUMA_NO_NODE
;
4057 /* Allocate memory for this devices private data. */
4058 my_node
= pcibus_to_node(pdev
->bus
);
4059 if (my_node
!= NUMA_NO_NODE
) {
4060 if (!node_online(my_node
))
4061 my_node
= mtip_get_next_rr_node();
4063 dev_info(&pdev
->dev
, "Kernel not reporting proximity, choosing a node\n");
4064 my_node
= mtip_get_next_rr_node();
4066 dev_info(&pdev
->dev
, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4067 my_node
, pcibus_to_node(pdev
->bus
), dev_to_node(&pdev
->dev
),
4068 cpu_to_node(smp_processor_id()), smp_processor_id());
4070 dd
= kzalloc_node(sizeof(struct driver_data
), GFP_KERNEL
, my_node
);
4073 "Unable to allocate memory for driver data\n");
4077 /* Attach the private data to this PCI device. */
4078 pci_set_drvdata(pdev
, dd
);
4080 rv
= pcim_enable_device(pdev
);
4082 dev_err(&pdev
->dev
, "Unable to enable device\n");
4086 /* Map BAR5 to memory. */
4087 rv
= pcim_iomap_regions(pdev
, 1 << MTIP_ABAR
, MTIP_DRV_NAME
);
4089 dev_err(&pdev
->dev
, "Unable to map regions\n");
4093 if (!pci_set_dma_mask(pdev
, DMA_BIT_MASK(64))) {
4094 rv
= pci_set_consistent_dma_mask(pdev
, DMA_BIT_MASK(64));
4097 rv
= pci_set_consistent_dma_mask(pdev
,
4100 dev_warn(&pdev
->dev
,
4101 "64-bit DMA enable failed\n");
4107 /* Copy the info we may need later into the private data structure. */
4108 dd
->major
= mtip_major
;
4109 dd
->instance
= instance
;
4111 dd
->numa_node
= my_node
;
4113 memset(dd
->workq_name
, 0, 32);
4114 snprintf(dd
->workq_name
, 31, "mtipq%d", dd
->instance
);
4116 dd
->isr_workq
= create_workqueue(dd
->workq_name
);
4117 if (!dd
->isr_workq
) {
4118 dev_warn(&pdev
->dev
, "Can't create wq %d\n", dd
->instance
);
4119 goto block_initialize_err
;
4122 memset(cpu_list
, 0, sizeof(cpu_list
));
4124 node_mask
= cpumask_of_node(dd
->numa_node
);
4125 if (!cpumask_empty(node_mask
)) {
4126 for_each_cpu(cpu
, node_mask
)
4128 snprintf(&cpu_list
[j
], 256 - j
, "%d ", cpu
);
4129 j
= strlen(cpu_list
);
4132 dev_info(&pdev
->dev
, "Node %d on package %d has %d cpu(s): %s\n",
4134 topology_physical_package_id(cpumask_first(node_mask
)),
4135 nr_cpus_node(dd
->numa_node
),
4138 dev_dbg(&pdev
->dev
, "mtip32xx: node_mask empty\n");
4140 dd
->isr_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4141 dev_info(&pdev
->dev
, "Initial IRQ binding node:cpu %d:%d\n",
4142 cpu_to_node(dd
->isr_binding
), dd
->isr_binding
);
4144 /* first worker context always runs in ISR */
4145 dd
->work
[0].cpu_binding
= dd
->isr_binding
;
4146 dd
->work
[1].cpu_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4147 dd
->work
[2].cpu_binding
= get_least_used_cpu_on_node(dd
->numa_node
);
4148 dd
->work
[3].cpu_binding
= dd
->work
[0].cpu_binding
;
4149 dd
->work
[4].cpu_binding
= dd
->work
[1].cpu_binding
;
4150 dd
->work
[5].cpu_binding
= dd
->work
[2].cpu_binding
;
4151 dd
->work
[6].cpu_binding
= dd
->work
[2].cpu_binding
;
4152 dd
->work
[7].cpu_binding
= dd
->work
[1].cpu_binding
;
4154 /* Log the bindings */
4155 for_each_present_cpu(cpu
) {
4156 memset(cpu_list
, 0, sizeof(cpu_list
));
4157 for (i
= 0, j
= 0; i
< MTIP_MAX_SLOT_GROUPS
; i
++) {
4158 if (dd
->work
[i
].cpu_binding
== cpu
) {
4159 snprintf(&cpu_list
[j
], 256 - j
, "%d ", i
);
4160 j
= strlen(cpu_list
);
4164 dev_info(&pdev
->dev
, "CPU %d: WQs %s\n", cpu
, cpu_list
);
4167 INIT_WORK(&dd
->work
[0].work
, mtip_workq_sdbf0
);
4168 INIT_WORK(&dd
->work
[1].work
, mtip_workq_sdbf1
);
4169 INIT_WORK(&dd
->work
[2].work
, mtip_workq_sdbf2
);
4170 INIT_WORK(&dd
->work
[3].work
, mtip_workq_sdbf3
);
4171 INIT_WORK(&dd
->work
[4].work
, mtip_workq_sdbf4
);
4172 INIT_WORK(&dd
->work
[5].work
, mtip_workq_sdbf5
);
4173 INIT_WORK(&dd
->work
[6].work
, mtip_workq_sdbf6
);
4174 INIT_WORK(&dd
->work
[7].work
, mtip_workq_sdbf7
);
4176 pci_set_master(pdev
);
4177 if (pci_enable_msi(pdev
)) {
4178 dev_warn(&pdev
->dev
,
4179 "Unable to enable MSI interrupt.\n");
4180 goto block_initialize_err
;
4183 /* Initialize the block layer. */
4184 rv
= mtip_block_initialize(dd
);
4187 "Unable to initialize block layer\n");
4188 goto block_initialize_err
;
4192 * Increment the instance count so that each device has a unique
4196 if (rv
!= MTIP_FTL_REBUILD_MAGIC
)
4197 set_bit(MTIP_DDF_INIT_DONE_BIT
, &dd
->dd_flag
);
4200 block_initialize_err
:
4201 pci_disable_msi(pdev
);
4202 if (dd
->isr_workq
) {
4203 flush_workqueue(dd
->isr_workq
);
4204 destroy_workqueue(dd
->isr_workq
);
4205 drop_cpu(dd
->work
[0].cpu_binding
);
4206 drop_cpu(dd
->work
[1].cpu_binding
);
4207 drop_cpu(dd
->work
[2].cpu_binding
);
4210 pcim_iounmap_regions(pdev
, 1 << MTIP_ABAR
);
4214 pci_set_drvdata(pdev
, NULL
);
4221 * Called for each probed device when the device is removed or the
4222 * driver is unloaded.
4227 static void mtip_pci_remove(struct pci_dev
*pdev
)
4229 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4232 set_bit(MTIP_DDF_REMOVE_PENDING_BIT
, &dd
->dd_flag
);
4234 if (mtip_check_surprise_removal(pdev
)) {
4235 while (!test_bit(MTIP_DDF_CLEANUP_BIT
, &dd
->dd_flag
)) {
4238 if (counter
== 10) {
4239 /* Cleanup the outstanding commands */
4240 mtip_command_cleanup(dd
);
4246 /* Clean up the block layer. */
4247 mtip_block_remove(dd
);
4249 if (dd
->isr_workq
) {
4250 flush_workqueue(dd
->isr_workq
);
4251 destroy_workqueue(dd
->isr_workq
);
4252 drop_cpu(dd
->work
[0].cpu_binding
);
4253 drop_cpu(dd
->work
[1].cpu_binding
);
4254 drop_cpu(dd
->work
[2].cpu_binding
);
4257 pci_disable_msi(pdev
);
4260 pcim_iounmap_regions(pdev
, 1 << MTIP_ABAR
);
4264 * Called for each probed device when the device is suspended.
4270 static int mtip_pci_suspend(struct pci_dev
*pdev
, pm_message_t mesg
)
4273 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4277 "Driver private datastructure is NULL\n");
4281 set_bit(MTIP_DDF_RESUME_BIT
, &dd
->dd_flag
);
4283 /* Disable ports & interrupts then send standby immediate */
4284 rv
= mtip_block_suspend(dd
);
4287 "Failed to suspend controller\n");
4292 * Save the pci config space to pdev structure &
4293 * disable the device
4295 pci_save_state(pdev
);
4296 pci_disable_device(pdev
);
4298 /* Move to Low power state*/
4299 pci_set_power_state(pdev
, PCI_D3hot
);
4305 * Called for each probed device when the device is resumed.
4311 static int mtip_pci_resume(struct pci_dev
*pdev
)
4314 struct driver_data
*dd
;
4316 dd
= pci_get_drvdata(pdev
);
4319 "Driver private datastructure is NULL\n");
4323 /* Move the device to active State */
4324 pci_set_power_state(pdev
, PCI_D0
);
4326 /* Restore PCI configuration space */
4327 pci_restore_state(pdev
);
4329 /* Enable the PCI device*/
4330 rv
= pcim_enable_device(pdev
);
4333 "Failed to enable card during resume\n");
4336 pci_set_master(pdev
);
4339 * Calls hbaReset, initPort, & startPort function
4340 * then enables interrupts
4342 rv
= mtip_block_resume(dd
);
4344 dev_err(&pdev
->dev
, "Unable to resume\n");
4347 clear_bit(MTIP_DDF_RESUME_BIT
, &dd
->dd_flag
);
4358 static void mtip_pci_shutdown(struct pci_dev
*pdev
)
4360 struct driver_data
*dd
= pci_get_drvdata(pdev
);
4362 mtip_block_shutdown(dd
);
4365 /* Table of device ids supported by this driver. */
4366 static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl
) = {
4367 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320H_DEVICE_ID
) },
4368 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320M_DEVICE_ID
) },
4369 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P320S_DEVICE_ID
) },
4370 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P325M_DEVICE_ID
) },
4371 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P420H_DEVICE_ID
) },
4372 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P420M_DEVICE_ID
) },
4373 { PCI_DEVICE(PCI_VENDOR_ID_MICRON
, P425M_DEVICE_ID
) },
4377 /* Structure that describes the PCI driver functions. */
4378 static struct pci_driver mtip_pci_driver
= {
4379 .name
= MTIP_DRV_NAME
,
4380 .id_table
= mtip_pci_tbl
,
4381 .probe
= mtip_pci_probe
,
4382 .remove
= mtip_pci_remove
,
4383 .suspend
= mtip_pci_suspend
,
4384 .resume
= mtip_pci_resume
,
4385 .shutdown
= mtip_pci_shutdown
,
4388 MODULE_DEVICE_TABLE(pci
, mtip_pci_tbl
);
4391 * Module initialization function.
4393 * Called once when the module is loaded. This function allocates a major
4394 * block device number to the Cyclone devices and registers the PCI layer
4398 * 0 on success else error code.
4400 static int __init
mtip_init(void)
4404 pr_info(MTIP_DRV_NAME
" Version " MTIP_DRV_VERSION
"\n");
4406 /* Allocate a major block device number to use with this driver. */
4407 error
= register_blkdev(0, MTIP_DRV_NAME
);
4409 pr_err("Unable to register block device (%d)\n",
4416 dfs_parent
= debugfs_create_dir("rssd", NULL
);
4417 if (IS_ERR_OR_NULL(dfs_parent
)) {
4418 pr_warn("Error creating debugfs parent\n");
4423 /* Register our PCI operations. */
4424 error
= pci_register_driver(&mtip_pci_driver
);
4426 debugfs_remove(dfs_parent
);
4427 unregister_blkdev(mtip_major
, MTIP_DRV_NAME
);
4434 * Module de-initialization function.
4436 * Called once when the module is unloaded. This function deallocates
4437 * the major block device number allocated by mtip_init() and
4438 * unregisters the PCI layer of the driver.
4443 static void __exit
mtip_exit(void)
4445 debugfs_remove_recursive(dfs_parent
);
4447 /* Release the allocated major block device number. */
4448 unregister_blkdev(mtip_major
, MTIP_DRV_NAME
);
4450 /* Unregister the PCI driver. */
4451 pci_unregister_driver(&mtip_pci_driver
);
4454 MODULE_AUTHOR("Micron Technology, Inc");
4455 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4456 MODULE_LICENSE("GPL");
4457 MODULE_VERSION(MTIP_DRV_VERSION
);
4459 module_init(mtip_init
);
4460 module_exit(mtip_exit
);