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. */
84 struct list_head online_list;
85 struct list_head removing_list;
89 * Global variable used to hold the major block device number
90 * allocated in mtip_init().
92 static int mtip_major;
93 static struct dentry *dfs_parent;
94 static struct dentry *dfs_device_status;
96 static u32 cpu_use[NR_CPUS];
98 static DEFINE_SPINLOCK(rssd_index_lock);
99 static DEFINE_IDA(rssd_index_ida);
101 static int mtip_block_initialize(struct driver_data *dd);
104 struct mtip_compat_ide_task_request_s {
107 ide_reg_valid_t out_flags;
108 ide_reg_valid_t in_flags;
111 compat_ulong_t out_size;
112 compat_ulong_t in_size;
117 * This function check_for_surprise_removal is called
118 * while card is removed from the system and it will
119 * read the vendor id from the configration space
121 * @pdev Pointer to the pci_dev structure.
124 * true if device removed, else false
126 static bool mtip_check_surprise_removal(struct pci_dev *pdev)
130 /* Read the vendorID from the configuration space */
131 pci_read_config_word(pdev, 0x00, &vendor_id);
132 if (vendor_id == 0xFFFF)
133 return true; /* device removed */
135 return false; /* device present */
139 * This function is called for clean the pending command in the
140 * command slot during the surprise removal of device and return
141 * error to the upper layer.
143 * @dd Pointer to the DRIVER_DATA structure.
148 static void mtip_command_cleanup(struct driver_data *dd)
150 int group = 0, commandslot = 0, commandindex = 0;
151 struct mtip_cmd *command;
152 struct mtip_port *port = dd->port;
153 static int in_progress;
160 for (group = 0; group < 4; group++) {
161 for (commandslot = 0; commandslot < 32; commandslot++) {
162 if (!(port->allocated[group] & (1 << commandslot)))
165 commandindex = group << 5 | commandslot;
166 command = &port->commands[commandindex];
168 if (atomic_read(&command->active)
169 && (command->async_callback)) {
170 command->async_callback(command->async_data,
172 command->async_callback = NULL;
173 command->async_data = NULL;
176 dma_unmap_sg(&port->dd->pdev->dev,
178 command->scatter_ents,
185 set_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag);
190 * Obtain an empty command slot.
192 * This function needs to be reentrant since it could be called
193 * at the same time on multiple CPUs. The allocation of the
194 * command slot must be atomic.
196 * @port Pointer to the port data structure.
199 * >= 0 Index of command slot obtained.
200 * -1 No command slots available.
202 static int get_slot(struct mtip_port *port)
205 unsigned int num_command_slots = port->dd->slot_groups * 32;
208 * Try 10 times, because there is a small race here.
209 * that's ok, because it's still cheaper than a lock.
211 * Race: Since this section is not protected by lock, same bit
212 * could be chosen by different process contexts running in
213 * different processor. So instead of costly lock, we are going
216 for (i = 0; i < 10; i++) {
217 slot = find_next_zero_bit(port->allocated,
218 num_command_slots, 1);
219 if ((slot < num_command_slots) &&
220 (!test_and_set_bit(slot, port->allocated)))
223 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
225 if (mtip_check_surprise_removal(port->dd->pdev)) {
226 /* Device not present, clean outstanding commands */
227 mtip_command_cleanup(port->dd);
233 * Release a command slot.
235 * @port Pointer to the port data structure.
236 * @tag Tag of command to release
241 static inline void release_slot(struct mtip_port *port, int tag)
243 smp_mb__before_clear_bit();
244 clear_bit(tag, port->allocated);
245 smp_mb__after_clear_bit();
249 * Reset the HBA (without sleeping)
251 * @dd Pointer to the driver data structure.
254 * 0 The reset was successful.
255 * -1 The HBA Reset bit did not clear.
257 static int mtip_hba_reset(struct driver_data *dd)
259 unsigned long timeout;
261 /* Set the reset bit */
262 writel(HOST_RESET, dd->mmio + HOST_CTL);
265 readl(dd->mmio + HOST_CTL);
267 /* Spin for up to 2 seconds, waiting for reset acknowledgement */
268 timeout = jiffies + msecs_to_jiffies(2000);
271 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
274 } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
275 && time_before(jiffies, timeout));
277 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
284 * Issue a command to the hardware.
286 * Set the appropriate bit in the s_active and Command Issue hardware
287 * registers, causing hardware command processing to begin.
289 * @port Pointer to the port structure.
290 * @tag The tag of the command to be issued.
295 static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
297 int group = tag >> 5;
299 atomic_set(&port->commands[tag].active, 1);
301 /* guard SACT and CI registers */
302 spin_lock(&port->cmd_issue_lock[group]);
303 writel((1 << MTIP_TAG_BIT(tag)),
304 port->s_active[MTIP_TAG_INDEX(tag)]);
305 writel((1 << MTIP_TAG_BIT(tag)),
306 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
307 spin_unlock(&port->cmd_issue_lock[group]);
309 /* Set the command's timeout value.*/
310 port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
311 MTIP_NCQ_COMMAND_TIMEOUT_MS);
315 * Enable/disable the reception of FIS
317 * @port Pointer to the port data structure
318 * @enable 1 to enable, 0 to disable
321 * Previous state: 1 enabled, 0 disabled
323 static int mtip_enable_fis(struct mtip_port *port, int enable)
327 /* enable FIS reception */
328 tmp = readl(port->mmio + PORT_CMD);
330 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
332 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
335 readl(port->mmio + PORT_CMD);
337 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
341 * Enable/disable the DMA engine
343 * @port Pointer to the port data structure
344 * @enable 1 to enable, 0 to disable
347 * Previous state: 1 enabled, 0 disabled.
349 static int mtip_enable_engine(struct mtip_port *port, int enable)
353 /* enable FIS reception */
354 tmp = readl(port->mmio + PORT_CMD);
356 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
358 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
360 readl(port->mmio + PORT_CMD);
361 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
365 * Enables the port DMA engine and FIS reception.
370 static inline void mtip_start_port(struct mtip_port *port)
372 /* Enable FIS reception */
373 mtip_enable_fis(port, 1);
375 /* Enable the DMA engine */
376 mtip_enable_engine(port, 1);
380 * Deinitialize a port by disabling port interrupts, the DMA engine,
383 * @port Pointer to the port structure
388 static inline void mtip_deinit_port(struct mtip_port *port)
390 /* Disable interrupts on this port */
391 writel(0, port->mmio + PORT_IRQ_MASK);
393 /* Disable the DMA engine */
394 mtip_enable_engine(port, 0);
396 /* Disable FIS reception */
397 mtip_enable_fis(port, 0);
403 * This function deinitializes the port by calling mtip_deinit_port() and
404 * then initializes it by setting the command header and RX FIS addresses,
405 * clearing the SError register and any pending port interrupts before
406 * re-enabling the default set of port interrupts.
408 * @port Pointer to the port structure.
413 static void mtip_init_port(struct mtip_port *port)
416 mtip_deinit_port(port);
418 /* Program the command list base and FIS base addresses */
419 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
420 writel((port->command_list_dma >> 16) >> 16,
421 port->mmio + PORT_LST_ADDR_HI);
422 writel((port->rxfis_dma >> 16) >> 16,
423 port->mmio + PORT_FIS_ADDR_HI);
426 writel(port->command_list_dma & 0xFFFFFFFF,
427 port->mmio + PORT_LST_ADDR);
428 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
431 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
433 /* reset the completed registers.*/
434 for (i = 0; i < port->dd->slot_groups; i++)
435 writel(0xFFFFFFFF, port->completed[i]);
437 /* Clear any pending interrupts for this port */
438 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
440 /* Clear any pending interrupts on the HBA. */
441 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
442 port->dd->mmio + HOST_IRQ_STAT);
444 /* Enable port interrupts */
445 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
451 * @port Pointer to the port data structure.
456 static void mtip_restart_port(struct mtip_port *port)
458 unsigned long timeout;
460 /* Disable the DMA engine */
461 mtip_enable_engine(port, 0);
463 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
464 timeout = jiffies + msecs_to_jiffies(500);
465 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
466 && time_before(jiffies, timeout))
469 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
473 * Chip quirk: escalate to hba reset if
474 * PxCMD.CR not clear after 500 ms
476 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
477 dev_warn(&port->dd->pdev->dev,
478 "PxCMD.CR not clear, escalating reset\n");
480 if (mtip_hba_reset(port->dd))
481 dev_err(&port->dd->pdev->dev,
482 "HBA reset escalation failed.\n");
484 /* 30 ms delay before com reset to quiesce chip */
488 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
491 writel(readl(port->mmio + PORT_SCR_CTL) |
492 1, port->mmio + PORT_SCR_CTL);
493 readl(port->mmio + PORT_SCR_CTL);
495 /* Wait 1 ms to quiesce chip function */
496 timeout = jiffies + msecs_to_jiffies(1);
497 while (time_before(jiffies, timeout))
500 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
503 /* Clear PxSCTL.DET */
504 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
505 port->mmio + PORT_SCR_CTL);
506 readl(port->mmio + PORT_SCR_CTL);
508 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
509 timeout = jiffies + msecs_to_jiffies(500);
510 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
511 && time_before(jiffies, timeout))
514 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
517 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
518 dev_warn(&port->dd->pdev->dev,
519 "COM reset failed\n");
521 mtip_init_port(port);
522 mtip_start_port(port);
526 static int mtip_device_reset(struct driver_data *dd)
530 if (mtip_check_surprise_removal(dd->pdev))
533 if (mtip_hba_reset(dd) < 0)
537 mtip_init_port(dd->port);
538 mtip_start_port(dd->port);
540 /* Enable interrupts on the HBA. */
541 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
542 dd->mmio + HOST_CTL);
547 * Helper function for tag logging
549 static void print_tags(struct driver_data *dd,
551 unsigned long *tagbits,
554 unsigned char tagmap[128];
555 int group, tagmap_len = 0;
557 memset(tagmap, 0, sizeof(tagmap));
558 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
559 tagmap_len = sprintf(tagmap + tagmap_len, "%016lX ",
561 dev_warn(&dd->pdev->dev,
562 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
566 * Called periodically to see if any read/write commands are
567 * taking too long to complete.
569 * @data Pointer to the PORT data structure.
574 static void mtip_timeout_function(unsigned long int data)
576 struct mtip_port *port = (struct mtip_port *) data;
577 struct host_to_dev_fis *fis;
578 struct mtip_cmd *command;
579 int tag, cmdto_cnt = 0;
580 unsigned int bit, group;
581 unsigned int num_command_slots;
582 unsigned long to, tagaccum[SLOTBITS_IN_LONGS];
587 if (test_bit(MTIP_DDF_RESUME_BIT, &port->dd->dd_flag)) {
588 mod_timer(&port->cmd_timer,
589 jiffies + msecs_to_jiffies(30000));
592 /* clear the tag accumulator */
593 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
594 num_command_slots = port->dd->slot_groups * 32;
596 for (tag = 0; tag < num_command_slots; tag++) {
598 * Skip internal command slot as it has
599 * its own timeout mechanism
601 if (tag == MTIP_TAG_INTERNAL)
604 if (atomic_read(&port->commands[tag].active) &&
605 (time_after(jiffies, port->commands[tag].comp_time))) {
609 command = &port->commands[tag];
610 fis = (struct host_to_dev_fis *) command->command;
612 set_bit(tag, tagaccum);
615 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
618 * Clear the completed bit. This should prevent
619 * any interrupt handlers from trying to retire
622 writel(1 << bit, port->completed[group]);
624 /* Call the async completion callback. */
625 if (likely(command->async_callback))
626 command->async_callback(command->async_data,
628 command->async_callback = NULL;
629 command->comp_func = NULL;
631 /* Unmap the DMA scatter list entries */
632 dma_unmap_sg(&port->dd->pdev->dev,
634 command->scatter_ents,
638 * Clear the allocated bit and active tag for the
641 atomic_set(&port->commands[tag].active, 0);
642 release_slot(port, tag);
649 print_tags(port->dd, "timed out", tagaccum, cmdto_cnt);
650 if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
651 mtip_device_reset(port->dd);
652 wake_up_interruptible(&port->svc_wait);
654 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
657 if (port->ic_pause_timer) {
658 to = port->ic_pause_timer + msecs_to_jiffies(1000);
659 if (time_after(jiffies, to)) {
660 if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
661 port->ic_pause_timer = 0;
662 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
663 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
664 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
665 wake_up_interruptible(&port->svc_wait);
672 /* Restart the timer */
673 mod_timer(&port->cmd_timer,
674 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
678 * IO completion function.
680 * This completion function is called by the driver ISR when a
681 * command that was issued by the kernel completes. It first calls the
682 * asynchronous completion function which normally calls back into the block
683 * layer passing the asynchronous callback data, then unmaps the
684 * scatter list associated with the completed command, and finally
685 * clears the allocated bit associated with the completed command.
687 * @port Pointer to the port data structure.
688 * @tag Tag of the command.
689 * @data Pointer to driver_data.
690 * @status Completion status.
695 static void mtip_async_complete(struct mtip_port *port,
700 struct mtip_cmd *command;
701 struct driver_data *dd = data;
702 int cb_status = status ? -EIO : 0;
704 if (unlikely(!dd) || unlikely(!port))
707 command = &port->commands[tag];
709 if (unlikely(status == PORT_IRQ_TF_ERR)) {
710 dev_warn(&port->dd->pdev->dev,
711 "Command tag %d failed due to TFE\n", tag);
714 /* Upper layer callback */
715 if (likely(command->async_callback))
716 command->async_callback(command->async_data, cb_status);
718 command->async_callback = NULL;
719 command->comp_func = NULL;
721 /* Unmap the DMA scatter list entries */
722 dma_unmap_sg(&dd->pdev->dev,
724 command->scatter_ents,
727 /* Clear the allocated and active bits for the command */
728 atomic_set(&port->commands[tag].active, 0);
729 release_slot(port, tag);
731 if (unlikely(command->unaligned))
732 up(&port->cmd_slot_unal);
738 * Internal command completion callback function.
740 * This function is normally called by the driver ISR when an internal
741 * command completed. This function signals the command completion by
742 * calling complete().
744 * @port Pointer to the port data structure.
745 * @tag Tag of the command that has completed.
746 * @data Pointer to a completion structure.
747 * @status Completion status.
752 static void mtip_completion(struct mtip_port *port,
757 struct mtip_cmd *command = &port->commands[tag];
758 struct completion *waiting = data;
759 if (unlikely(status == PORT_IRQ_TF_ERR))
760 dev_warn(&port->dd->pdev->dev,
761 "Internal command %d completed with TFE\n", tag);
763 command->async_callback = NULL;
764 command->comp_func = NULL;
769 static void mtip_null_completion(struct mtip_port *port,
777 static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
778 dma_addr_t buffer_dma, unsigned int sectors);
779 static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
780 struct smart_attr *attrib);
784 * @dd Pointer to the DRIVER_DATA structure.
789 static void mtip_handle_tfe(struct driver_data *dd)
791 int group, tag, bit, reissue, rv;
792 struct mtip_port *port;
793 struct mtip_cmd *cmd;
795 struct host_to_dev_fis *fis;
796 unsigned long tagaccum[SLOTBITS_IN_LONGS];
797 unsigned int cmd_cnt = 0;
799 char *fail_reason = NULL;
800 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
802 dev_warn(&dd->pdev->dev, "Taskfile error\n");
806 /* Stop the timer to prevent command timeouts. */
807 del_timer(&port->cmd_timer);
808 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
810 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
811 test_bit(MTIP_TAG_INTERNAL, port->allocated)) {
812 cmd = &port->commands[MTIP_TAG_INTERNAL];
813 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
815 atomic_inc(&cmd->active); /* active > 1 indicates error */
816 if (cmd->comp_data && cmd->comp_func) {
817 cmd->comp_func(port, MTIP_TAG_INTERNAL,
818 cmd->comp_data, PORT_IRQ_TF_ERR);
820 goto handle_tfe_exit;
823 /* clear the tag accumulator */
824 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
826 /* Loop through all the groups */
827 for (group = 0; group < dd->slot_groups; group++) {
828 completed = readl(port->completed[group]);
830 /* clear completed status register in the hardware.*/
831 writel(completed, port->completed[group]);
833 /* Process successfully completed commands */
834 for (bit = 0; bit < 32 && completed; bit++) {
835 if (!(completed & (1<<bit)))
837 tag = (group << 5) + bit;
839 /* Skip the internal command slot */
840 if (tag == MTIP_TAG_INTERNAL)
843 cmd = &port->commands[tag];
844 if (likely(cmd->comp_func)) {
845 set_bit(tag, tagaccum);
847 atomic_set(&cmd->active, 0);
853 dev_err(&port->dd->pdev->dev,
854 "Missing completion func for tag %d",
856 if (mtip_check_surprise_removal(dd->pdev)) {
857 mtip_command_cleanup(dd);
858 /* don't proceed further */
865 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
867 /* Restart the port */
869 mtip_restart_port(port);
871 /* Trying to determine the cause of the error */
872 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
874 dd->port->log_buf_dma, 1);
876 dev_warn(&dd->pdev->dev,
877 "Error in READ LOG EXT (10h) command\n");
878 /* non-critical error, don't fail the load */
880 buf = (unsigned char *)dd->port->log_buf;
881 if (buf[259] & 0x1) {
882 dev_info(&dd->pdev->dev,
883 "Write protect bit is set.\n");
884 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
885 fail_all_ncq_write = 1;
886 fail_reason = "write protect";
888 if (buf[288] == 0xF7) {
889 dev_info(&dd->pdev->dev,
890 "Exceeded Tmax, drive in thermal shutdown.\n");
891 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
892 fail_all_ncq_cmds = 1;
893 fail_reason = "thermal shutdown";
895 if (buf[288] == 0xBF) {
896 dev_info(&dd->pdev->dev,
897 "Drive indicates rebuild has failed.\n");
898 fail_all_ncq_cmds = 1;
899 fail_reason = "rebuild failed";
903 /* clear the tag accumulator */
904 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
906 /* Loop through all the groups */
907 for (group = 0; group < dd->slot_groups; group++) {
908 for (bit = 0; bit < 32; bit++) {
910 tag = (group << 5) + bit;
911 cmd = &port->commands[tag];
913 /* If the active bit is set re-issue the command */
914 if (atomic_read(&cmd->active) == 0)
917 fis = (struct host_to_dev_fis *)cmd->command;
919 /* Should re-issue? */
920 if (tag == MTIP_TAG_INTERNAL ||
921 fis->command == ATA_CMD_SET_FEATURES)
924 if (fail_all_ncq_cmds ||
925 (fail_all_ncq_write &&
926 fis->command == ATA_CMD_FPDMA_WRITE)) {
927 dev_warn(&dd->pdev->dev,
928 " Fail: %s w/tag %d [%s].\n",
929 fis->command == ATA_CMD_FPDMA_WRITE ?
932 fail_reason != NULL ?
933 fail_reason : "unknown");
934 atomic_set(&cmd->active, 0);
935 if (cmd->comp_func) {
936 cmd->comp_func(port, tag,
945 * First check if this command has
946 * exceeded its retries.
948 if (reissue && (cmd->retries-- > 0)) {
950 set_bit(tag, tagaccum);
952 /* Re-issue the command. */
953 mtip_issue_ncq_command(port, tag);
958 /* Retire a command that will not be reissued */
959 dev_warn(&port->dd->pdev->dev,
960 "retiring tag %d\n", tag);
961 atomic_set(&cmd->active, 0);
970 dev_warn(&port->dd->pdev->dev,
971 "Bad completion for tag %d\n",
975 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
978 /* clear eh_active */
979 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
980 wake_up_interruptible(&port->svc_wait);
982 mod_timer(&port->cmd_timer,
983 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
987 * Handle a set device bits interrupt
989 static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
992 struct driver_data *dd = port->dd;
994 struct mtip_cmd *command;
997 WARN_ON_ONCE(!completed);
1000 /* clear completed status register in the hardware.*/
1001 writel(completed, port->completed[group]);
1003 /* Process completed commands. */
1004 for (bit = 0; (bit < 32) && completed; bit++) {
1005 if (completed & 0x01) {
1006 tag = (group << 5) | bit;
1008 /* skip internal command slot. */
1009 if (unlikely(tag == MTIP_TAG_INTERNAL))
1012 command = &port->commands[tag];
1013 /* make internal callback */
1014 if (likely(command->comp_func)) {
1021 dev_warn(&dd->pdev->dev,
1026 if (mtip_check_surprise_removal(
1028 mtip_command_cleanup(dd);
1036 /* If last, re-enable interrupts */
1037 if (atomic_dec_return(&dd->irq_workers_active) == 0)
1038 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
1042 * Process legacy pio and d2h interrupts
1044 static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
1046 struct mtip_port *port = dd->port;
1047 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
1049 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
1050 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1051 & (1 << MTIP_TAG_INTERNAL))) {
1052 if (cmd->comp_func) {
1053 cmd->comp_func(port,
1065 * Demux and handle errors
1067 static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
1069 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
1070 mtip_handle_tfe(dd);
1072 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
1073 dev_warn(&dd->pdev->dev,
1074 "Clearing PxSERR.DIAG.x\n");
1075 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
1078 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
1079 dev_warn(&dd->pdev->dev,
1080 "Clearing PxSERR.DIAG.n\n");
1081 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
1084 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
1085 dev_warn(&dd->pdev->dev,
1086 "Port stat errors %x unhandled\n",
1087 (port_stat & ~PORT_IRQ_HANDLED));
1091 static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
1093 struct driver_data *dd = (struct driver_data *) data;
1094 struct mtip_port *port = dd->port;
1095 u32 hba_stat, port_stat;
1097 int do_irq_enable = 1, i, workers;
1098 struct mtip_work *twork;
1100 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1104 /* Acknowledge the interrupt status on the port.*/
1105 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1106 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1108 /* Demux port status */
1109 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
1111 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
1113 /* Start at 1: group zero is always local? */
1114 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
1116 twork = &dd->work[i];
1117 twork->completed = readl(port->completed[i]);
1118 if (twork->completed)
1122 atomic_set(&dd->irq_workers_active, workers);
1124 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
1125 twork = &dd->work[i];
1126 if (twork->completed)
1133 if (likely(dd->work[0].completed))
1134 mtip_workq_sdbfx(port, 0,
1135 dd->work[0].completed);
1139 * Chip quirk: SDB interrupt but nothing
1146 if (unlikely(port_stat & PORT_IRQ_ERR)) {
1147 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
1148 mtip_command_cleanup(dd);
1149 /* don't proceed further */
1152 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1156 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
1159 if (unlikely(port_stat & PORT_IRQ_LEGACY))
1160 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
1163 /* acknowledge interrupt */
1164 if (unlikely(do_irq_enable))
1165 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
1171 * HBA interrupt subroutine.
1174 * @instance Pointer to the driver data structure.
1177 * IRQ_HANDLED A HBA interrupt was pending and handled.
1178 * IRQ_NONE This interrupt was not for the HBA.
1180 static irqreturn_t mtip_irq_handler(int irq, void *instance)
1182 struct driver_data *dd = instance;
1184 return mtip_handle_irq(dd);
1187 static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1189 atomic_set(&port->commands[tag].active, 1);
1190 writel(1 << MTIP_TAG_BIT(tag),
1191 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1194 static bool mtip_pause_ncq(struct mtip_port *port,
1195 struct host_to_dev_fis *fis)
1197 struct host_to_dev_fis *reply;
1198 unsigned long task_file_data;
1200 reply = port->rxfis + RX_FIS_D2H_REG;
1201 task_file_data = readl(port->mmio+PORT_TFDATA);
1203 if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
1204 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1206 if ((task_file_data & 1))
1209 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
1210 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1211 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1212 port->ic_pause_timer = jiffies;
1214 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1215 (fis->features == 0x03)) {
1216 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1217 port->ic_pause_timer = jiffies;
1219 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1220 ((fis->command == 0xFC) &&
1221 (fis->features == 0x27 || fis->features == 0x72 ||
1222 fis->features == 0x62 || fis->features == 0x26))) {
1223 /* Com reset after secure erase or lowlevel format */
1224 mtip_restart_port(port);
1232 * Wait for port to quiesce
1234 * @port Pointer to port data structure
1235 * @timeout Max duration to wait (ms)
1239 * -EBUSY Commands still active
1241 static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1245 unsigned int active = 1;
1247 to = jiffies + msecs_to_jiffies(timeout);
1249 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1250 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
1252 continue; /* svc thd is actively issuing commands */
1254 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
1257 * Ignore s_active bit 0 of array element 0.
1258 * This bit will always be set
1260 active = readl(port->s_active[0]) & 0xFFFFFFFE;
1261 for (n = 1; n < port->dd->slot_groups; n++)
1262 active |= readl(port->s_active[n]);
1268 } while (time_before(jiffies, to));
1270 return active ? -EBUSY : 0;
1274 * Execute an internal command and wait for the completion.
1276 * @port Pointer to the port data structure.
1277 * @fis Pointer to the FIS that describes the command.
1278 * @fis_len Length in WORDS of the FIS.
1279 * @buffer DMA accessible for command data.
1280 * @buf_len Length, in bytes, of the data buffer.
1281 * @opts Command header options, excluding the FIS length
1282 * and the number of PRD entries.
1283 * @timeout Time in ms to wait for the command to complete.
1286 * 0 Command completed successfully.
1287 * -EFAULT The buffer address is not correctly aligned.
1288 * -EBUSY Internal command or other IO in progress.
1289 * -EAGAIN Time out waiting for command to complete.
1291 static int mtip_exec_internal_command(struct mtip_port *port,
1292 struct host_to_dev_fis *fis,
1298 unsigned long timeout)
1300 struct mtip_cmd_sg *command_sg;
1301 DECLARE_COMPLETION_ONSTACK(wait);
1302 int rv = 0, ready2go = 1;
1303 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
1305 struct driver_data *dd = port->dd;
1307 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1308 if (buffer & 0x00000007) {
1309 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
1313 to = jiffies + msecs_to_jiffies(timeout);
1315 ready2go = !test_and_set_bit(MTIP_TAG_INTERNAL,
1320 } while (time_before(jiffies, to));
1322 dev_warn(&dd->pdev->dev,
1323 "Internal cmd active. new cmd [%02X]\n", fis->command);
1326 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1327 port->ic_pause_timer = 0;
1329 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1330 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1332 if (atomic == GFP_KERNEL) {
1333 if (fis->command != ATA_CMD_STANDBYNOW1) {
1334 /* wait for io to complete if non atomic */
1335 if (mtip_quiesce_io(port, 5000) < 0) {
1336 dev_warn(&dd->pdev->dev,
1337 "Failed to quiesce IO\n");
1338 release_slot(port, MTIP_TAG_INTERNAL);
1339 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1340 wake_up_interruptible(&port->svc_wait);
1345 /* Set the completion function and data for the command. */
1346 int_cmd->comp_data = &wait;
1347 int_cmd->comp_func = mtip_completion;
1350 /* Clear completion - we're going to poll */
1351 int_cmd->comp_data = NULL;
1352 int_cmd->comp_func = mtip_null_completion;
1355 /* Copy the command to the command table */
1356 memcpy(int_cmd->command, fis, fis_len*4);
1358 /* Populate the SG list */
1359 int_cmd->command_header->opts =
1360 __force_bit2int cpu_to_le32(opts | fis_len);
1362 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1365 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1367 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1368 command_sg->dba_upper =
1369 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
1371 int_cmd->command_header->opts |=
1372 __force_bit2int cpu_to_le32((1 << 16));
1375 /* Populate the command header */
1376 int_cmd->command_header->byte_count = 0;
1378 /* Issue the command to the hardware */
1379 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1381 if (atomic == GFP_KERNEL) {
1382 /* Wait for the command to complete or timeout. */
1383 if (wait_for_completion_interruptible_timeout(
1385 msecs_to_jiffies(timeout)) <= 0) {
1386 if (rv == -ERESTARTSYS) { /* interrupted */
1387 dev_err(&dd->pdev->dev,
1388 "Internal command [%02X] was interrupted after %lu ms\n",
1389 fis->command, timeout);
1392 } else if (rv == 0) /* timeout */
1393 dev_err(&dd->pdev->dev,
1394 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1395 fis->command, timeout);
1397 dev_err(&dd->pdev->dev,
1398 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1399 fis->command, rv, timeout);
1401 if (mtip_check_surprise_removal(dd->pdev) ||
1402 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1404 dev_err(&dd->pdev->dev,
1405 "Internal command [%02X] wait returned due to SR\n",
1410 mtip_device_reset(dd); /* recover from timeout issue */
1415 u32 hba_stat, port_stat;
1417 /* Spin for <timeout> checking if command still outstanding */
1418 timeout = jiffies + msecs_to_jiffies(timeout);
1419 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1420 & (1 << MTIP_TAG_INTERNAL))
1421 && time_before(jiffies, timeout)) {
1422 if (mtip_check_surprise_removal(dd->pdev)) {
1426 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
1427 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1432 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1436 if (port_stat & PORT_IRQ_ERR) {
1437 dev_err(&dd->pdev->dev,
1438 "Internal command [%02X] failed\n",
1440 mtip_device_reset(dd);
1444 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1445 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1448 dd->mmio + HOST_IRQ_STAT);
1454 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1455 & (1 << MTIP_TAG_INTERNAL)) {
1457 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1458 mtip_device_reset(dd);
1463 /* Clear the allocated and active bits for the internal command. */
1464 atomic_set(&int_cmd->active, 0);
1465 release_slot(port, MTIP_TAG_INTERNAL);
1466 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1470 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1471 wake_up_interruptible(&port->svc_wait);
1477 * Byte-swap ATA ID strings.
1479 * ATA identify data contains strings in byte-swapped 16-bit words.
1480 * They must be swapped (on all architectures) to be usable as C strings.
1481 * This function swaps bytes in-place.
1483 * @buf The buffer location of the string
1484 * @len The number of bytes to swap
1489 static inline void ata_swap_string(u16 *buf, unsigned int len)
1492 for (i = 0; i < (len/2); i++)
1493 be16_to_cpus(&buf[i]);
1497 * Request the device identity information.
1499 * If a user space buffer is not specified, i.e. is NULL, the
1500 * identify information is still read from the drive and placed
1501 * into the identify data buffer (@e port->identify) in the
1502 * port data structure.
1503 * When the identify buffer contains valid identify information @e
1504 * port->identify_valid is non-zero.
1506 * @port Pointer to the port structure.
1507 * @user_buffer A user space buffer where the identify data should be
1511 * 0 Command completed successfully.
1512 * -EFAULT An error occurred while coping data to the user buffer.
1513 * -1 Command failed.
1515 static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1518 struct host_to_dev_fis fis;
1520 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
1523 /* Build the FIS. */
1524 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1527 fis.command = ATA_CMD_ID_ATA;
1529 /* Set the identify information as invalid. */
1530 port->identify_valid = 0;
1532 /* Clear the identify information. */
1533 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1535 /* Execute the command. */
1536 if (mtip_exec_internal_command(port,
1540 sizeof(u16) * ATA_ID_WORDS,
1543 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1550 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1551 * perform field-sensitive swapping on the string fields.
1552 * See the kernel use of ata_id_string() for proof of this.
1554 #ifdef __LITTLE_ENDIAN
1555 ata_swap_string(port->identify + 27, 40); /* model string*/
1556 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1557 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1561 for (i = 0; i < ATA_ID_WORDS; i++)
1562 port->identify[i] = le16_to_cpu(port->identify[i]);
1566 #ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
1567 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1568 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1569 port->dd->trim_supp = true;
1572 port->dd->trim_supp = false;
1574 /* Set the identify buffer as valid. */
1575 port->identify_valid = 1;
1581 ATA_ID_WORDS * sizeof(u16))) {
1592 * Issue a standby immediate command to the device.
1594 * @port Pointer to the port structure.
1597 * 0 Command was executed successfully.
1598 * -1 An error occurred while executing the command.
1600 static int mtip_standby_immediate(struct mtip_port *port)
1603 struct host_to_dev_fis fis;
1604 unsigned long start;
1606 /* Build the FIS. */
1607 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1610 fis.command = ATA_CMD_STANDBYNOW1;
1613 rv = mtip_exec_internal_command(port,
1621 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1622 jiffies_to_msecs(jiffies - start));
1624 dev_warn(&port->dd->pdev->dev,
1625 "STANDBY IMMEDIATE command failed.\n");
1631 * Issue a READ LOG EXT command to the device.
1633 * @port pointer to the port structure.
1634 * @page page number to fetch
1635 * @buffer pointer to buffer
1636 * @buffer_dma dma address corresponding to @buffer
1637 * @sectors page length to fetch, in sectors
1640 * @rv return value from mtip_exec_internal_command()
1642 static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1643 dma_addr_t buffer_dma, unsigned int sectors)
1645 struct host_to_dev_fis fis;
1647 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1650 fis.command = ATA_CMD_READ_LOG_EXT;
1651 fis.sect_count = sectors & 0xFF;
1652 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1655 fis.device = ATA_DEVICE_OBS;
1657 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1659 return mtip_exec_internal_command(port,
1663 sectors * ATA_SECT_SIZE,
1666 MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
1670 * Issue a SMART READ DATA command to the device.
1672 * @port pointer to the port structure.
1673 * @buffer pointer to buffer
1674 * @buffer_dma dma address corresponding to @buffer
1677 * @rv return value from mtip_exec_internal_command()
1679 static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1680 dma_addr_t buffer_dma)
1682 struct host_to_dev_fis fis;
1684 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1687 fis.command = ATA_CMD_SMART;
1688 fis.features = 0xD0;
1692 fis.device = ATA_DEVICE_OBS;
1694 return mtip_exec_internal_command(port,
1705 * Get the value of a smart attribute
1707 * @port pointer to the port structure
1708 * @id attribute number
1709 * @attrib pointer to return attrib information corresponding to @id
1712 * -EINVAL NULL buffer passed or unsupported attribute @id.
1713 * -EPERM Identify data not valid, SMART not supported or not enabled
1715 static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1716 struct smart_attr *attrib)
1719 struct smart_attr *pattr;
1724 if (!port->identify_valid) {
1725 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1728 if (!(port->identify[82] & 0x1)) {
1729 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1732 if (!(port->identify[85] & 0x1)) {
1733 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1737 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1738 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1740 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1744 pattr = (struct smart_attr *)(port->smart_buf + 2);
1745 for (i = 0; i < 29; i++, pattr++)
1746 if (pattr->attr_id == id) {
1747 memcpy(attrib, pattr, sizeof(struct smart_attr));
1752 dev_warn(&port->dd->pdev->dev,
1753 "Query for invalid SMART attribute ID\n");
1761 * Trim unused sectors
1763 * @dd pointer to driver_data structure
1765 * @len # of 512b sectors to trim
1768 * -ENOMEM Out of dma memory
1769 * -EINVAL Invalid parameters passed in, trim not supported
1770 * -EIO Error submitting trim request to hw
1772 static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
1776 u64 tlba, tlen, sect_left;
1777 struct mtip_trim_entry *buf;
1778 dma_addr_t dma_addr;
1779 struct host_to_dev_fis fis;
1781 if (!len || dd->trim_supp == false)
1784 /* Trim request too big */
1785 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1787 /* Trim request not aligned on 4k boundary */
1788 WARN_ON(len % 8 != 0);
1790 /* Warn if vu_trim structure is too big */
1791 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1793 /* Allocate a DMA buffer for the trim structure */
1794 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1798 memset(buf, 0, ATA_SECT_SIZE);
1800 for (i = 0, sect_left = len, tlba = lba;
1801 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1803 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1804 MTIP_MAX_TRIM_ENTRY_LEN :
1806 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1807 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1811 WARN_ON(sect_left != 0);
1814 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1818 fis.features = 0x60;
1820 fis.device = ATA_DEVICE_OBS;
1822 if (mtip_exec_internal_command(dd->port,
1829 MTIP_TRIM_TIMEOUT_MS) < 0)
1832 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1837 * Get the drive capacity.
1839 * @dd Pointer to the device data structure.
1840 * @sectors Pointer to the variable that will receive the sector count.
1843 * 1 Capacity was returned successfully.
1844 * 0 The identify information is invalid.
1846 static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
1848 struct mtip_port *port = dd->port;
1849 u64 total, raw0, raw1, raw2, raw3;
1850 raw0 = port->identify[100];
1851 raw1 = port->identify[101];
1852 raw2 = port->identify[102];
1853 raw3 = port->identify[103];
1854 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1856 return (bool) !!port->identify_valid;
1860 * Display the identify command data.
1862 * @port Pointer to the port data structure.
1867 static void mtip_dump_identify(struct mtip_port *port)
1870 unsigned short revid;
1873 if (!port->identify_valid)
1876 strlcpy(cbuf, (char *)(port->identify+10), 21);
1877 dev_info(&port->dd->pdev->dev,
1878 "Serial No.: %s\n", cbuf);
1880 strlcpy(cbuf, (char *)(port->identify+23), 9);
1881 dev_info(&port->dd->pdev->dev,
1882 "Firmware Ver.: %s\n", cbuf);
1884 strlcpy(cbuf, (char *)(port->identify+27), 41);
1885 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1887 if (mtip_hw_get_capacity(port->dd, §ors))
1888 dev_info(&port->dd->pdev->dev,
1889 "Capacity: %llu sectors (%llu MB)\n",
1891 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1893 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
1894 switch (revid & 0xFF) {
1896 strlcpy(cbuf, "A0", 3);
1899 strlcpy(cbuf, "A2", 3);
1902 strlcpy(cbuf, "?", 2);
1905 dev_info(&port->dd->pdev->dev,
1906 "Card Type: %s\n", cbuf);
1910 * Map the commands scatter list into the command table.
1912 * @command Pointer to the command.
1913 * @nents Number of scatter list entries.
1918 static inline void fill_command_sg(struct driver_data *dd,
1919 struct mtip_cmd *command,
1923 unsigned int dma_len;
1924 struct mtip_cmd_sg *command_sg;
1925 struct scatterlist *sg = command->sg;
1927 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1929 for (n = 0; n < nents; n++) {
1930 dma_len = sg_dma_len(sg);
1931 if (dma_len > 0x400000)
1932 dev_err(&dd->pdev->dev,
1933 "DMA segment length truncated\n");
1934 command_sg->info = __force_bit2int
1935 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1936 command_sg->dba = __force_bit2int
1937 cpu_to_le32(sg_dma_address(sg));
1938 command_sg->dba_upper = __force_bit2int
1939 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
1946 * @brief Execute a drive command.
1948 * return value 0 The command completed successfully.
1949 * return value -1 An error occurred while executing the command.
1951 static int exec_drive_task(struct mtip_port *port, u8 *command)
1953 struct host_to_dev_fis fis;
1954 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1956 /* Build the FIS. */
1957 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1960 fis.command = command[0];
1961 fis.features = command[1];
1962 fis.sect_count = command[2];
1963 fis.sector = command[3];
1964 fis.cyl_low = command[4];
1965 fis.cyl_hi = command[5];
1966 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1968 dbg_printk(MTIP_DRV_NAME " %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
1978 /* Execute the command. */
1979 if (mtip_exec_internal_command(port,
1986 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
1990 command[0] = reply->command; /* Status*/
1991 command[1] = reply->features; /* Error*/
1992 command[4] = reply->cyl_low;
1993 command[5] = reply->cyl_hi;
1995 dbg_printk(MTIP_DRV_NAME " %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
2006 * @brief Execute a drive command.
2008 * @param port Pointer to the port data structure.
2009 * @param command Pointer to the user specified command parameters.
2010 * @param user_buffer Pointer to the user space buffer where read sector
2011 * data should be copied.
2013 * return value 0 The command completed successfully.
2014 * return value -EFAULT An error occurred while copying the completion
2015 * data to the user space buffer.
2016 * return value -1 An error occurred while executing the command.
2018 static int exec_drive_command(struct mtip_port *port, u8 *command,
2019 void __user *user_buffer)
2021 struct host_to_dev_fis fis;
2022 struct host_to_dev_fis *reply;
2024 dma_addr_t dma_addr = 0;
2025 int rv = 0, xfer_sz = command[3];
2031 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
2032 ATA_SECT_SIZE * xfer_sz,
2036 dev_err(&port->dd->pdev->dev,
2037 "Memory allocation failed (%d bytes)\n",
2038 ATA_SECT_SIZE * xfer_sz);
2041 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
2044 /* Build the FIS. */
2045 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2048 fis.command = command[0];
2049 fis.features = command[2];
2050 fis.sect_count = command[3];
2051 if (fis.command == ATA_CMD_SMART) {
2052 fis.sector = command[1];
2058 reply = (port->rxfis + RX_FIS_PIO_SETUP);
2060 reply = (port->rxfis + RX_FIS_D2H_REG);
2062 dbg_printk(MTIP_DRV_NAME
2063 " %s: User Command: cmd %x, sect %x, "
2064 "feat %x, sectcnt %x\n",
2071 /* Execute the command. */
2072 if (mtip_exec_internal_command(port,
2075 (xfer_sz ? dma_addr : 0),
2076 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
2079 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
2082 goto exit_drive_command;
2085 /* Collect the completion status. */
2086 command[0] = reply->command; /* Status*/
2087 command[1] = reply->features; /* Error*/
2088 command[2] = reply->sect_count;
2090 dbg_printk(MTIP_DRV_NAME
2091 " %s: Completion Status: stat %x, "
2092 "err %x, nsect %x\n",
2099 if (copy_to_user(user_buffer,
2101 ATA_SECT_SIZE * command[3])) {
2103 goto exit_drive_command;
2108 dmam_free_coherent(&port->dd->pdev->dev,
2109 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
2114 * Indicates whether a command has a single sector payload.
2116 * @command passed to the device to perform the certain event.
2117 * @features passed to the device to perform the certain event.
2120 * 1 command is one that always has a single sector payload,
2121 * regardless of the value in the Sector Count field.
2125 static unsigned int implicit_sector(unsigned char command,
2126 unsigned char features)
2128 unsigned int rv = 0;
2130 /* list of commands that have an implicit sector count of 1 */
2132 case ATA_CMD_SEC_SET_PASS:
2133 case ATA_CMD_SEC_UNLOCK:
2134 case ATA_CMD_SEC_ERASE_PREP:
2135 case ATA_CMD_SEC_ERASE_UNIT:
2136 case ATA_CMD_SEC_FREEZE_LOCK:
2137 case ATA_CMD_SEC_DISABLE_PASS:
2138 case ATA_CMD_PMP_READ:
2139 case ATA_CMD_PMP_WRITE:
2142 case ATA_CMD_SET_MAX:
2143 if (features == ATA_SET_MAX_UNLOCK)
2147 if ((features == ATA_SMART_READ_VALUES) ||
2148 (features == ATA_SMART_READ_THRESHOLDS))
2151 case ATA_CMD_CONF_OVERLAY:
2152 if ((features == ATA_DCO_IDENTIFY) ||
2153 (features == ATA_DCO_SET))
2159 static void mtip_set_timeout(struct driver_data *dd,
2160 struct host_to_dev_fis *fis,
2161 unsigned int *timeout, u8 erasemode)
2163 switch (fis->command) {
2164 case ATA_CMD_DOWNLOAD_MICRO:
2165 *timeout = 120000; /* 2 minutes */
2167 case ATA_CMD_SEC_ERASE_UNIT:
2170 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
2172 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
2174 case ATA_CMD_STANDBYNOW1:
2175 *timeout = 120000; /* 2 minutes */
2179 *timeout = 60000; /* 60 seconds */
2182 *timeout = 15000; /* 15 seconds */
2185 *timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
2191 * Executes a taskfile
2192 * See ide_taskfile_ioctl() for derivation
2194 static int exec_drive_taskfile(struct driver_data *dd,
2196 ide_task_request_t *req_task,
2199 struct host_to_dev_fis fis;
2200 struct host_to_dev_fis *reply;
2203 dma_addr_t outbuf_dma = 0;
2204 dma_addr_t inbuf_dma = 0;
2205 dma_addr_t dma_buffer = 0;
2207 unsigned int taskin = 0;
2208 unsigned int taskout = 0;
2210 unsigned int timeout;
2211 unsigned int force_single_sector;
2212 unsigned int transfer_size;
2213 unsigned long task_file_data;
2214 int intotal = outtotal + req_task->out_size;
2217 taskout = req_task->out_size;
2218 taskin = req_task->in_size;
2219 /* 130560 = 512 * 0xFF*/
2220 if (taskin > 130560 || taskout > 130560) {
2226 outbuf = kzalloc(taskout, GFP_KERNEL);
2227 if (outbuf == NULL) {
2231 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
2235 outbuf_dma = pci_map_single(dd->pdev,
2239 if (outbuf_dma == 0) {
2243 dma_buffer = outbuf_dma;
2247 inbuf = kzalloc(taskin, GFP_KERNEL);
2248 if (inbuf == NULL) {
2253 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2257 inbuf_dma = pci_map_single(dd->pdev,
2259 taskin, DMA_FROM_DEVICE);
2260 if (inbuf_dma == 0) {
2264 dma_buffer = inbuf_dma;
2267 /* only supports PIO and non-data commands from this ioctl. */
2268 switch (req_task->data_phase) {
2270 nsect = taskout / ATA_SECT_SIZE;
2271 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2274 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2276 case TASKFILE_NO_DATA:
2277 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2284 /* Build the FIS. */
2285 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2289 fis.command = req_task->io_ports[7];
2290 fis.features = req_task->io_ports[1];
2291 fis.sect_count = req_task->io_ports[2];
2292 fis.lba_low = req_task->io_ports[3];
2293 fis.lba_mid = req_task->io_ports[4];
2294 fis.lba_hi = req_task->io_ports[5];
2295 /* Clear the dev bit*/
2296 fis.device = req_task->io_ports[6] & ~0x10;
2298 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2299 req_task->in_flags.all =
2300 IDE_TASKFILE_STD_IN_FLAGS |
2301 (IDE_HOB_STD_IN_FLAGS << 8);
2302 fis.lba_low_ex = req_task->hob_ports[3];
2303 fis.lba_mid_ex = req_task->hob_ports[4];
2304 fis.lba_hi_ex = req_task->hob_ports[5];
2305 fis.features_ex = req_task->hob_ports[1];
2306 fis.sect_cnt_ex = req_task->hob_ports[2];
2309 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2312 force_single_sector = implicit_sector(fis.command, fis.features);
2314 if ((taskin || taskout) && (!fis.sect_count)) {
2316 fis.sect_count = nsect;
2318 if (!force_single_sector) {
2319 dev_warn(&dd->pdev->dev,
2320 "data movement but "
2321 "sect_count is 0\n");
2328 dbg_printk(MTIP_DRV_NAME
2329 " %s: cmd %x, feat %x, nsect %x,"
2330 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2341 /* check for erase mode support during secure erase.*/
2342 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2343 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
2347 mtip_set_timeout(dd, &fis, &timeout, erasemode);
2349 /* Determine the correct transfer size.*/
2350 if (force_single_sector)
2351 transfer_size = ATA_SECT_SIZE;
2353 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2355 /* Execute the command.*/
2356 if (mtip_exec_internal_command(dd->port,
2368 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2370 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2371 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2372 req_task->io_ports[7] = reply->control;
2374 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2375 req_task->io_ports[7] = reply->command;
2378 /* reclaim the DMA buffers.*/
2380 pci_unmap_single(dd->pdev, inbuf_dma,
2381 taskin, DMA_FROM_DEVICE);
2383 pci_unmap_single(dd->pdev, outbuf_dma,
2384 taskout, DMA_TO_DEVICE);
2388 /* return the ATA registers to the caller.*/
2389 req_task->io_ports[1] = reply->features;
2390 req_task->io_ports[2] = reply->sect_count;
2391 req_task->io_ports[3] = reply->lba_low;
2392 req_task->io_ports[4] = reply->lba_mid;
2393 req_task->io_ports[5] = reply->lba_hi;
2394 req_task->io_ports[6] = reply->device;
2396 if (req_task->out_flags.all & 1) {
2398 req_task->hob_ports[3] = reply->lba_low_ex;
2399 req_task->hob_ports[4] = reply->lba_mid_ex;
2400 req_task->hob_ports[5] = reply->lba_hi_ex;
2401 req_task->hob_ports[1] = reply->features_ex;
2402 req_task->hob_ports[2] = reply->sect_cnt_ex;
2404 dbg_printk(MTIP_DRV_NAME
2405 " %s: Completion: stat %x,"
2406 "err %x, sect_cnt %x, lbalo %x,"
2407 "lbamid %x, lbahi %x, dev %x\n",
2409 req_task->io_ports[7],
2410 req_task->io_ports[1],
2411 req_task->io_ports[2],
2412 req_task->io_ports[3],
2413 req_task->io_ports[4],
2414 req_task->io_ports[5],
2415 req_task->io_ports[6]);
2418 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2424 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2431 pci_unmap_single(dd->pdev, inbuf_dma,
2432 taskin, DMA_FROM_DEVICE);
2434 pci_unmap_single(dd->pdev, outbuf_dma,
2435 taskout, DMA_TO_DEVICE);
2443 * Handle IOCTL calls from the Block Layer.
2445 * This function is called by the Block Layer when it receives an IOCTL
2446 * command that it does not understand. If the IOCTL command is not supported
2447 * this function returns -ENOTTY.
2449 * @dd Pointer to the driver data structure.
2450 * @cmd IOCTL command passed from the Block Layer.
2451 * @arg IOCTL argument passed from the Block Layer.
2454 * 0 The IOCTL completed successfully.
2455 * -ENOTTY The specified command is not supported.
2456 * -EFAULT An error occurred copying data to a user space buffer.
2457 * -EIO An error occurred while executing the command.
2459 static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2463 case HDIO_GET_IDENTITY:
2465 if (copy_to_user((void __user *)arg, dd->port->identify,
2466 sizeof(u16) * ATA_ID_WORDS))
2470 case HDIO_DRIVE_CMD:
2472 u8 drive_command[4];
2474 /* Copy the user command info to our buffer. */
2475 if (copy_from_user(drive_command,
2476 (void __user *) arg,
2477 sizeof(drive_command)))
2480 /* Execute the drive command. */
2481 if (exec_drive_command(dd->port,
2483 (void __user *) (arg+4)))
2486 /* Copy the status back to the users buffer. */
2487 if (copy_to_user((void __user *) arg,
2489 sizeof(drive_command)))
2494 case HDIO_DRIVE_TASK:
2496 u8 drive_command[7];
2498 /* Copy the user command info to our buffer. */
2499 if (copy_from_user(drive_command,
2500 (void __user *) arg,
2501 sizeof(drive_command)))
2504 /* Execute the drive command. */
2505 if (exec_drive_task(dd->port, drive_command))
2508 /* Copy the status back to the users buffer. */
2509 if (copy_to_user((void __user *) arg,
2511 sizeof(drive_command)))
2516 case HDIO_DRIVE_TASKFILE: {
2517 ide_task_request_t req_task;
2520 if (copy_from_user(&req_task, (void __user *) arg,
2524 outtotal = sizeof(req_task);
2526 ret = exec_drive_taskfile(dd, (void __user *) arg,
2527 &req_task, outtotal);
2529 if (copy_to_user((void __user *) arg, &req_task,
2543 * Submit an IO to the hw
2545 * This function is called by the block layer to issue an io
2546 * to the device. Upon completion, the callback function will
2547 * be called with the data parameter passed as the callback data.
2549 * @dd Pointer to the driver data structure.
2550 * @start First sector to read.
2551 * @nsect Number of sectors to read.
2552 * @nents Number of entries in scatter list for the read command.
2553 * @tag The tag of this read command.
2554 * @callback Pointer to the function that should be called
2555 * when the read completes.
2556 * @data Callback data passed to the callback function
2557 * when the read completes.
2558 * @dir Direction (read or write)
2563 static void mtip_hw_submit_io(struct driver_data *dd, sector_t sector,
2564 int nsect, int nents, int tag, void *callback,
2565 void *data, int dir, int unaligned)
2567 struct host_to_dev_fis *fis;
2568 struct mtip_port *port = dd->port;
2569 struct mtip_cmd *command = &port->commands[tag];
2570 int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2573 /* Map the scatter list for DMA access */
2574 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
2576 command->scatter_ents = nents;
2578 command->unaligned = unaligned;
2580 * The number of retries for this command before it is
2581 * reported as a failure to the upper layers.
2583 command->retries = MTIP_MAX_RETRIES;
2586 fis = command->command;
2590 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
2591 fis->lba_low = start & 0xFF;
2592 fis->lba_mid = (start >> 8) & 0xFF;
2593 fis->lba_hi = (start >> 16) & 0xFF;
2594 fis->lba_low_ex = (start >> 24) & 0xFF;
2595 fis->lba_mid_ex = (start >> 32) & 0xFF;
2596 fis->lba_hi_ex = (start >> 40) & 0xFF;
2597 fis->device = 1 << 6;
2598 fis->features = nsect & 0xFF;
2599 fis->features_ex = (nsect >> 8) & 0xFF;
2600 fis->sect_count = ((tag << 3) | (tag >> 5));
2601 fis->sect_cnt_ex = 0;
2605 fill_command_sg(dd, command, nents);
2608 fis->device |= 1 << 7;
2610 /* Populate the command header */
2611 command->command_header->opts =
2612 __force_bit2int cpu_to_le32(
2613 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
2614 command->command_header->byte_count = 0;
2617 * Set the completion function and data for the command
2618 * within this layer.
2620 command->comp_data = dd;
2621 command->comp_func = mtip_async_complete;
2622 command->direction = dma_dir;
2625 * Set the completion function and data for the command passed
2626 * from the upper layer.
2628 command->async_data = data;
2629 command->async_callback = callback;
2632 * To prevent this command from being issued
2633 * if an internal command is in progress or error handling is active.
2635 if (port->flags & MTIP_PF_PAUSE_IO) {
2636 set_bit(tag, port->cmds_to_issue);
2637 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
2641 /* Issue the command to the hardware */
2642 mtip_issue_ncq_command(port, tag);
2648 * Release a command slot.
2650 * @dd Pointer to the driver data structure.
2656 static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag,
2659 struct semaphore *sem = unaligned ? &dd->port->cmd_slot_unal :
2660 &dd->port->cmd_slot;
2661 release_slot(dd->port, tag);
2666 * Obtain a command slot and return its associated scatter list.
2668 * @dd Pointer to the driver data structure.
2669 * @tag Pointer to an int that will receive the allocated command
2673 * Pointer to the scatter list for the allocated command slot
2674 * or NULL if no command slots are available.
2676 static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2677 int *tag, int unaligned)
2679 struct semaphore *sem = unaligned ? &dd->port->cmd_slot_unal :
2680 &dd->port->cmd_slot;
2683 * It is possible that, even with this semaphore, a thread
2684 * may think that no command slots are available. Therefore, we
2685 * need to make an attempt to get_slot().
2688 *tag = get_slot(dd->port);
2690 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
2694 if (unlikely(*tag < 0)) {
2699 return dd->port->commands[*tag].sg;
2703 * Sysfs status dump.
2705 * @dev Pointer to the device structure, passed by the kernrel.
2706 * @attr Pointer to the device_attribute structure passed by the kernel.
2707 * @buf Pointer to the char buffer that will receive the stats info.
2710 * The size, in bytes, of the data copied into buf.
2712 static ssize_t mtip_hw_show_status(struct device *dev,
2713 struct device_attribute *attr,
2716 struct driver_data *dd = dev_to_disk(dev)->private_data;
2719 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
2720 size += sprintf(buf, "%s", "thermal_shutdown\n");
2721 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
2722 size += sprintf(buf, "%s", "write_protect\n");
2724 size += sprintf(buf, "%s", "online\n");
2729 static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
2731 /* debugsfs entries */
2733 static ssize_t show_device_status(struct device_driver *drv, char *buf)
2736 struct driver_data *dd, *tmp;
2737 unsigned long flags;
2741 spin_lock_irqsave(&dev_lock, flags);
2742 size += sprintf(&buf[size], "Devices Present:\n");
2743 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
2746 dd->port->identify &&
2747 dd->port->identify_valid) {
2749 (char *) (dd->port->identify + 10), 21);
2750 status = *(dd->port->identify + 141);
2752 memset(id_buf, 0, 42);
2757 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2758 size += sprintf(&buf[size],
2759 " device %s %s (ftl rebuild %d %%)\n",
2760 dev_name(&dd->pdev->dev),
2764 size += sprintf(&buf[size],
2766 dev_name(&dd->pdev->dev),
2772 size += sprintf(&buf[size], "Devices Being Removed:\n");
2773 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
2776 dd->port->identify &&
2777 dd->port->identify_valid) {
2779 (char *) (dd->port->identify+10), 21);
2780 status = *(dd->port->identify + 141);
2782 memset(id_buf, 0, 42);
2787 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2788 size += sprintf(&buf[size],
2789 " device %s %s (ftl rebuild %d %%)\n",
2790 dev_name(&dd->pdev->dev),
2794 size += sprintf(&buf[size],
2796 dev_name(&dd->pdev->dev),
2801 spin_unlock_irqrestore(&dev_lock, flags);
2806 static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2807 size_t len, loff_t *offset)
2810 char buf[MTIP_DFS_MAX_BUF_SIZE];
2812 if (!len || *offset)
2815 size += show_device_status(NULL, buf);
2817 *offset = size <= len ? size : len;
2818 size = copy_to_user(ubuf, buf, *offset);
2825 static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2826 size_t len, loff_t *offset)
2828 struct driver_data *dd = (struct driver_data *)f->private_data;
2829 char buf[MTIP_DFS_MAX_BUF_SIZE];
2830 u32 group_allocated;
2837 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2839 for (n = dd->slot_groups-1; n >= 0; n--)
2840 size += sprintf(&buf[size], "%08X ",
2841 readl(dd->port->s_active[n]));
2843 size += sprintf(&buf[size], "]\n");
2844 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2846 for (n = dd->slot_groups-1; n >= 0; n--)
2847 size += sprintf(&buf[size], "%08X ",
2848 readl(dd->port->cmd_issue[n]));
2850 size += sprintf(&buf[size], "]\n");
2851 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2853 for (n = dd->slot_groups-1; n >= 0; n--)
2854 size += sprintf(&buf[size], "%08X ",
2855 readl(dd->port->completed[n]));
2857 size += sprintf(&buf[size], "]\n");
2858 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2859 readl(dd->port->mmio + PORT_IRQ_STAT));
2860 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2861 readl(dd->mmio + HOST_IRQ_STAT));
2862 size += sprintf(&buf[size], "\n");
2864 size += sprintf(&buf[size], "L/ Allocated : [ 0x");
2866 for (n = dd->slot_groups-1; n >= 0; n--) {
2867 if (sizeof(long) > sizeof(u32))
2869 dd->port->allocated[n/2] >> (32*(n&1));
2871 group_allocated = dd->port->allocated[n];
2872 size += sprintf(&buf[size], "%08X ", group_allocated);
2874 size += sprintf(&buf[size], "]\n");
2876 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2878 for (n = dd->slot_groups-1; n >= 0; n--) {
2879 if (sizeof(long) > sizeof(u32))
2881 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2883 group_allocated = dd->port->cmds_to_issue[n];
2884 size += sprintf(&buf[size], "%08X ", group_allocated);
2886 size += sprintf(&buf[size], "]\n");
2888 *offset = size <= len ? size : len;
2889 size = copy_to_user(ubuf, buf, *offset);
2896 static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2897 size_t len, loff_t *offset)
2899 struct driver_data *dd = (struct driver_data *)f->private_data;
2900 char buf[MTIP_DFS_MAX_BUF_SIZE];
2906 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2908 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2911 *offset = size <= len ? size : len;
2912 size = copy_to_user(ubuf, buf, *offset);
2919 static const struct file_operations mtip_device_status_fops = {
2920 .owner = THIS_MODULE,
2921 .open = simple_open,
2922 .read = mtip_hw_read_device_status,
2923 .llseek = no_llseek,
2926 static const struct file_operations mtip_regs_fops = {
2927 .owner = THIS_MODULE,
2928 .open = simple_open,
2929 .read = mtip_hw_read_registers,
2930 .llseek = no_llseek,
2933 static const struct file_operations mtip_flags_fops = {
2934 .owner = THIS_MODULE,
2935 .open = simple_open,
2936 .read = mtip_hw_read_flags,
2937 .llseek = no_llseek,
2941 * Create the sysfs related attributes.
2943 * @dd Pointer to the driver data structure.
2944 * @kobj Pointer to the kobj for the block device.
2947 * 0 Operation completed successfully.
2948 * -EINVAL Invalid parameter.
2950 static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
2955 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2956 dev_warn(&dd->pdev->dev,
2957 "Error creating 'status' sysfs entry\n");
2962 * Remove the sysfs related attributes.
2964 * @dd Pointer to the driver data structure.
2965 * @kobj Pointer to the kobj for the block device.
2968 * 0 Operation completed successfully.
2969 * -EINVAL Invalid parameter.
2971 static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
2976 sysfs_remove_file(kobj, &dev_attr_status.attr);
2981 static int mtip_hw_debugfs_init(struct driver_data *dd)
2986 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2987 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2988 dev_warn(&dd->pdev->dev,
2989 "Error creating node %s under debugfs\n",
2990 dd->disk->disk_name);
2991 dd->dfs_node = NULL;
2995 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2997 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
3003 static void mtip_hw_debugfs_exit(struct driver_data *dd)
3005 debugfs_remove_recursive(dd->dfs_node);
3010 * Perform any init/resume time hardware setup
3012 * @dd Pointer to the driver data structure.
3017 static inline void hba_setup(struct driver_data *dd)
3020 hwdata = readl(dd->mmio + HOST_HSORG);
3022 /* interrupt bug workaround: use only 1 IS bit.*/
3024 HSORG_DISABLE_SLOTGRP_INTR |
3025 HSORG_DISABLE_SLOTGRP_PXIS,
3026 dd->mmio + HOST_HSORG);
3029 static int mtip_device_unaligned_constrained(struct driver_data *dd)
3031 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
3035 * Detect the details of the product, and store anything needed
3036 * into the driver data structure. This includes product type and
3037 * version and number of slot groups.
3039 * @dd Pointer to the driver data structure.
3044 static void mtip_detect_product(struct driver_data *dd)
3047 unsigned int rev, slotgroups;
3050 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
3052 * [15:8] hardware/software interface rev#
3053 * [ 3] asic-style interface
3054 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
3056 hwdata = readl(dd->mmio + HOST_HSORG);
3058 dd->product_type = MTIP_PRODUCT_UNKNOWN;
3059 dd->slot_groups = 1;
3062 dd->product_type = MTIP_PRODUCT_ASICFPGA;
3063 rev = (hwdata & HSORG_HWREV) >> 8;
3064 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
3065 dev_info(&dd->pdev->dev,
3066 "ASIC-FPGA design, HS rev 0x%x, "
3067 "%i slot groups [%i slots]\n",
3072 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
3073 dev_warn(&dd->pdev->dev,
3074 "Warning: driver only supports "
3075 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
3076 slotgroups = MTIP_MAX_SLOT_GROUPS;
3078 dd->slot_groups = slotgroups;
3082 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
3086 * Blocking wait for FTL rebuild to complete
3088 * @dd Pointer to the DRIVER_DATA structure.
3091 * 0 FTL rebuild completed successfully
3092 * -EFAULT FTL rebuild error/timeout/interruption
3094 static int mtip_ftl_rebuild_poll(struct driver_data *dd)
3096 unsigned long timeout, cnt = 0, start;
3098 dev_warn(&dd->pdev->dev,
3099 "FTL rebuild in progress. Polling for completion.\n");
3102 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
3105 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3108 if (mtip_check_surprise_removal(dd->pdev))
3111 if (mtip_get_identify(dd->port, NULL) < 0)
3114 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3115 MTIP_FTL_REBUILD_MAGIC) {
3117 /* Print message every 3 minutes */
3119 dev_warn(&dd->pdev->dev,
3120 "FTL rebuild in progress (%d secs).\n",
3121 jiffies_to_msecs(jiffies - start) / 1000);
3125 dev_warn(&dd->pdev->dev,
3126 "FTL rebuild complete (%d secs).\n",
3127 jiffies_to_msecs(jiffies - start) / 1000);
3128 mtip_block_initialize(dd);
3132 } while (time_before(jiffies, timeout));
3134 /* Check for timeout */
3135 dev_err(&dd->pdev->dev,
3136 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
3137 jiffies_to_msecs(jiffies - start) / 1000);
3142 * service thread to issue queued commands
3144 * @data Pointer to the driver data structure.
3150 static int mtip_service_thread(void *data)
3152 struct driver_data *dd = (struct driver_data *)data;
3153 unsigned long slot, slot_start, slot_wrap;
3154 unsigned int num_cmd_slots = dd->slot_groups * 32;
3155 struct mtip_port *port = dd->port;
3159 * the condition is to check neither an internal command is
3160 * is in progress nor error handling is active
3162 wait_event_interruptible(port->svc_wait, (port->flags) &&
3163 !(port->flags & MTIP_PF_PAUSE_IO));
3165 if (kthread_should_stop())
3168 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3172 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
3173 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
3175 /* used to restrict the loop to one iteration */
3176 slot_start = num_cmd_slots;
3179 slot = find_next_bit(port->cmds_to_issue,
3180 num_cmd_slots, slot);
3181 if (slot_wrap == 1) {
3182 if ((slot_start >= slot) ||
3183 (slot >= num_cmd_slots))
3186 if (unlikely(slot_start == num_cmd_slots))
3189 if (unlikely(slot == num_cmd_slots)) {
3195 /* Issue the command to the hardware */
3196 mtip_issue_ncq_command(port, slot);
3198 clear_bit(slot, port->cmds_to_issue);
3201 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
3202 } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
3203 if (!mtip_ftl_rebuild_poll(dd))
3204 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
3206 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
3208 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
3210 if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
3217 * Called once for each card.
3219 * @dd Pointer to the driver data structure.
3222 * 0 on success, else an error code.
3224 static int mtip_hw_init(struct driver_data *dd)
3228 unsigned int num_command_slots;
3229 unsigned long timeout, timetaken;
3231 struct smart_attr attr242;
3233 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3235 mtip_detect_product(dd);
3236 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3240 num_command_slots = dd->slot_groups * 32;
3244 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3247 dev_err(&dd->pdev->dev,
3248 "Memory allocation: port structure\n");
3252 /* Continue workqueue setup */
3253 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3254 dd->work[i].port = dd->port;
3256 /* Enable unaligned IO constraints for some devices */
3257 if (mtip_device_unaligned_constrained(dd))
3258 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3260 dd->unal_qdepth = 0;
3262 /* Counting semaphore to track command slot usage */
3263 sema_init(&dd->port->cmd_slot, num_command_slots - 1 - dd->unal_qdepth);
3264 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
3266 /* Spinlock to prevent concurrent issue */
3267 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3268 spin_lock_init(&dd->port->cmd_issue_lock[i]);
3270 /* Set the port mmio base address. */
3271 dd->port->mmio = dd->mmio + PORT_OFFSET;
3274 /* Allocate memory for the command list. */
3275 dd->port->command_list =
3276 dmam_alloc_coherent(&dd->pdev->dev,
3277 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
3278 &dd->port->command_list_dma,
3280 if (!dd->port->command_list) {
3281 dev_err(&dd->pdev->dev,
3282 "Memory allocation: command list\n");
3287 /* Clear the memory we have allocated. */
3288 memset(dd->port->command_list,
3290 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4));
3292 /* Setup the addresse of the RX FIS. */
3293 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
3294 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
3296 /* Setup the address of the command tables. */
3297 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
3298 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
3300 /* Setup the address of the identify data. */
3301 dd->port->identify = dd->port->command_table +
3303 dd->port->identify_dma = dd->port->command_tbl_dma +
3306 /* Setup the address of the sector buffer - for some non-ncq cmds */
3307 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
3308 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
3310 /* Setup the address of the log buf - for read log command */
3311 dd->port->log_buf = (void *)dd->port->sector_buffer + ATA_SECT_SIZE;
3312 dd->port->log_buf_dma = dd->port->sector_buffer_dma + ATA_SECT_SIZE;
3314 /* Setup the address of the smart buf - for smart read data command */
3315 dd->port->smart_buf = (void *)dd->port->log_buf + ATA_SECT_SIZE;
3316 dd->port->smart_buf_dma = dd->port->log_buf_dma + ATA_SECT_SIZE;
3319 /* Point the command headers at the command tables. */
3320 for (i = 0; i < num_command_slots; i++) {
3321 dd->port->commands[i].command_header =
3322 dd->port->command_list +
3323 (sizeof(struct mtip_cmd_hdr) * i);
3324 dd->port->commands[i].command_header_dma =
3325 dd->port->command_list_dma +
3326 (sizeof(struct mtip_cmd_hdr) * i);
3328 dd->port->commands[i].command =
3329 dd->port->command_table + (HW_CMD_TBL_SZ * i);
3330 dd->port->commands[i].command_dma =
3331 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
3333 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
3334 dd->port->commands[i].command_header->ctbau =
3335 __force_bit2int cpu_to_le32(
3336 (dd->port->commands[i].command_dma >> 16) >> 16);
3337 dd->port->commands[i].command_header->ctba =
3338 __force_bit2int cpu_to_le32(
3339 dd->port->commands[i].command_dma & 0xFFFFFFFF);
3342 * If this is not done, a bug is reported by the stock
3343 * FC11 i386. Due to the fact that it has lots of kernel
3344 * debugging enabled.
3346 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
3348 /* Mark all commands as currently inactive.*/
3349 atomic_set(&dd->port->commands[i].active, 0);
3352 /* Setup the pointers to the extended s_active and CI registers. */
3353 for (i = 0; i < dd->slot_groups; i++) {
3354 dd->port->s_active[i] =
3355 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3356 dd->port->cmd_issue[i] =
3357 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3358 dd->port->completed[i] =
3359 dd->port->mmio + i*0x80 + PORT_SDBV;
3362 timetaken = jiffies;
3363 timeout = jiffies + msecs_to_jiffies(30000);
3364 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3365 time_before(jiffies, timeout)) {
3368 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3369 timetaken = jiffies - timetaken;
3370 dev_warn(&dd->pdev->dev,
3371 "Surprise removal detected at %u ms\n",
3372 jiffies_to_msecs(timetaken));
3376 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
3377 timetaken = jiffies - timetaken;
3378 dev_warn(&dd->pdev->dev,
3379 "Removal detected at %u ms\n",
3380 jiffies_to_msecs(timetaken));
3385 /* Conditionally reset the HBA. */
3386 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3387 if (mtip_hba_reset(dd) < 0) {
3388 dev_err(&dd->pdev->dev,
3389 "Card did not reset within timeout\n");
3394 /* Clear any pending interrupts on the HBA */
3395 writel(readl(dd->mmio + HOST_IRQ_STAT),
3396 dd->mmio + HOST_IRQ_STAT);
3399 mtip_init_port(dd->port);
3400 mtip_start_port(dd->port);
3402 /* Setup the ISR and enable interrupts. */
3403 rv = devm_request_irq(&dd->pdev->dev,
3407 dev_driver_string(&dd->pdev->dev),
3411 dev_err(&dd->pdev->dev,
3412 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3415 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
3417 /* Enable interrupts on the HBA. */
3418 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3419 dd->mmio + HOST_CTL);
3421 init_timer(&dd->port->cmd_timer);
3422 init_waitqueue_head(&dd->port->svc_wait);
3424 dd->port->cmd_timer.data = (unsigned long int) dd->port;
3425 dd->port->cmd_timer.function = mtip_timeout_function;
3426 mod_timer(&dd->port->cmd_timer,
3427 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
3430 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
3435 if (mtip_get_identify(dd->port, NULL) < 0) {
3440 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3441 MTIP_FTL_REBUILD_MAGIC) {
3442 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3443 return MTIP_FTL_REBUILD_MAGIC;
3445 mtip_dump_identify(dd->port);
3447 /* check write protect, over temp and rebuild statuses */
3448 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3450 dd->port->log_buf_dma, 1);
3452 dev_warn(&dd->pdev->dev,
3453 "Error in READ LOG EXT (10h) command\n");
3454 /* non-critical error, don't fail the load */
3456 buf = (unsigned char *)dd->port->log_buf;
3457 if (buf[259] & 0x1) {
3458 dev_info(&dd->pdev->dev,
3459 "Write protect bit is set.\n");
3460 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3462 if (buf[288] == 0xF7) {
3463 dev_info(&dd->pdev->dev,
3464 "Exceeded Tmax, drive in thermal shutdown.\n");
3465 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3467 if (buf[288] == 0xBF) {
3468 dev_info(&dd->pdev->dev,
3469 "Drive indicates rebuild has failed.\n");
3474 /* get write protect progess */
3475 memset(&attr242, 0, sizeof(struct smart_attr));
3476 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3477 dev_warn(&dd->pdev->dev,
3478 "Unable to check write protect progress\n");
3480 dev_info(&dd->pdev->dev,
3481 "Write protect progress: %u%% (%u blocks)\n",
3482 attr242.cur, le32_to_cpu(attr242.data));
3486 del_timer_sync(&dd->port->cmd_timer);
3488 /* Disable interrupts on the HBA. */
3489 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3490 dd->mmio + HOST_CTL);
3492 /* Release the IRQ. */
3493 irq_set_affinity_hint(dd->pdev->irq, NULL);
3494 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3497 mtip_deinit_port(dd->port);
3499 /* Free the command/command header memory. */
3500 dmam_free_coherent(&dd->pdev->dev,
3501 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
3502 dd->port->command_list,
3503 dd->port->command_list_dma);
3505 /* Free the memory allocated for the for structure. */
3512 * Called to deinitialize an interface.
3514 * @dd Pointer to the driver data structure.
3519 static int mtip_hw_exit(struct driver_data *dd)
3522 * Send standby immediate (E0h) to the drive so that it
3525 if (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
3527 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags))
3528 if (mtip_standby_immediate(dd->port))
3529 dev_warn(&dd->pdev->dev,
3530 "STANDBY IMMEDIATE failed\n");
3532 /* de-initialize the port. */
3533 mtip_deinit_port(dd->port);
3535 /* Disable interrupts on the HBA. */
3536 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3537 dd->mmio + HOST_CTL);
3540 del_timer_sync(&dd->port->cmd_timer);
3542 /* Release the IRQ. */
3543 irq_set_affinity_hint(dd->pdev->irq, NULL);
3544 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3546 /* Free the command/command header memory. */
3547 dmam_free_coherent(&dd->pdev->dev,
3548 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
3549 dd->port->command_list,
3550 dd->port->command_list_dma);
3551 /* Free the memory allocated for the for structure. */
3558 * Issue a Standby Immediate command to the device.
3560 * This function is called by the Block Layer just before the
3561 * system powers off during a shutdown.
3563 * @dd Pointer to the driver data structure.
3568 static int mtip_hw_shutdown(struct driver_data *dd)
3571 * Send standby immediate (E0h) to the drive so that it
3574 mtip_standby_immediate(dd->port);
3582 * This function is called by the Block Layer just before the
3583 * system hibernates.
3585 * @dd Pointer to the driver data structure.
3588 * 0 Suspend was successful
3589 * -EFAULT Suspend was not successful
3591 static int mtip_hw_suspend(struct driver_data *dd)
3594 * Send standby immediate (E0h) to the drive
3595 * so that it saves its state.
3597 if (mtip_standby_immediate(dd->port) != 0) {
3598 dev_err(&dd->pdev->dev,
3599 "Failed standby-immediate command\n");
3603 /* Disable interrupts on the HBA.*/
3604 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3605 dd->mmio + HOST_CTL);
3606 mtip_deinit_port(dd->port);
3614 * This function is called by the Block Layer as the
3617 * @dd Pointer to the driver data structure.
3620 * 0 Resume was successful
3621 * -EFAULT Resume was not successful
3623 static int mtip_hw_resume(struct driver_data *dd)
3625 /* Perform any needed hardware setup steps */
3629 if (mtip_hba_reset(dd) != 0) {
3630 dev_err(&dd->pdev->dev,
3631 "Unable to reset the HBA\n");
3636 * Enable the port, DMA engine, and FIS reception specific
3637 * h/w in controller.
3639 mtip_init_port(dd->port);
3640 mtip_start_port(dd->port);
3642 /* Enable interrupts on the HBA.*/
3643 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3644 dd->mmio + HOST_CTL);
3650 * Helper function for reusing disk name
3651 * upon hot insertion.
3653 static int rssd_disk_name_format(char *prefix,
3658 const int base = 'z' - 'a' + 1;
3659 char *begin = buf + strlen(prefix);
3660 char *end = buf + buflen;
3670 *--p = 'a' + (index % unit);
3671 index = (index / unit) - 1;
3672 } while (index >= 0);
3674 memmove(begin, p, end - p);
3675 memcpy(buf, prefix, strlen(prefix));
3681 * Block layer IOCTL handler.
3683 * @dev Pointer to the block_device structure.
3685 * @cmd IOCTL command passed from the user application.
3686 * @arg Argument passed from the user application.
3689 * 0 IOCTL completed successfully.
3690 * -ENOTTY IOCTL not supported or invalid driver data
3691 * structure pointer.
3693 static int mtip_block_ioctl(struct block_device *dev,
3698 struct driver_data *dd = dev->bd_disk->private_data;
3700 if (!capable(CAP_SYS_ADMIN))
3706 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
3713 return mtip_hw_ioctl(dd, cmd, arg);
3717 #ifdef CONFIG_COMPAT
3719 * Block layer compat IOCTL handler.
3721 * @dev Pointer to the block_device structure.
3723 * @cmd IOCTL command passed from the user application.
3724 * @arg Argument passed from the user application.
3727 * 0 IOCTL completed successfully.
3728 * -ENOTTY IOCTL not supported or invalid driver data
3729 * structure pointer.
3731 static int mtip_block_compat_ioctl(struct block_device *dev,
3736 struct driver_data *dd = dev->bd_disk->private_data;
3738 if (!capable(CAP_SYS_ADMIN))
3744 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
3750 case HDIO_DRIVE_TASKFILE: {
3751 struct mtip_compat_ide_task_request_s __user *compat_req_task;
3752 ide_task_request_t req_task;
3753 int compat_tasksize, outtotal, ret;
3756 sizeof(struct mtip_compat_ide_task_request_s);
3759 (struct mtip_compat_ide_task_request_s __user *) arg;
3761 if (copy_from_user(&req_task, (void __user *) arg,
3762 compat_tasksize - (2 * sizeof(compat_long_t))))
3765 if (get_user(req_task.out_size, &compat_req_task->out_size))
3768 if (get_user(req_task.in_size, &compat_req_task->in_size))
3771 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3773 ret = exec_drive_taskfile(dd, (void __user *) arg,
3774 &req_task, outtotal);
3776 if (copy_to_user((void __user *) arg, &req_task,
3778 (2 * sizeof(compat_long_t))))
3781 if (put_user(req_task.out_size, &compat_req_task->out_size))
3784 if (put_user(req_task.in_size, &compat_req_task->in_size))
3790 return mtip_hw_ioctl(dd, cmd, arg);
3796 * Obtain the geometry of the device.
3798 * You may think that this function is obsolete, but some applications,
3799 * fdisk for example still used CHS values. This function describes the
3800 * device as having 224 heads and 56 sectors per cylinder. These values are
3801 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3802 * partition is described in terms of a start and end cylinder this means
3803 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3804 * affects performance.
3806 * @dev Pointer to the block_device strucutre.
3807 * @geo Pointer to a hd_geometry structure.
3810 * 0 Operation completed successfully.
3811 * -ENOTTY An error occurred while reading the drive capacity.
3813 static int mtip_block_getgeo(struct block_device *dev,
3814 struct hd_geometry *geo)
3816 struct driver_data *dd = dev->bd_disk->private_data;
3822 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3823 dev_warn(&dd->pdev->dev,
3824 "Could not get drive capacity.\n");
3830 sector_div(capacity, (geo->heads * geo->sectors));
3831 geo->cylinders = capacity;
3836 * Block device operation function.
3838 * This structure contains pointers to the functions required by the block
3841 static const struct block_device_operations mtip_block_ops = {
3842 .ioctl = mtip_block_ioctl,
3843 #ifdef CONFIG_COMPAT
3844 .compat_ioctl = mtip_block_compat_ioctl,
3846 .getgeo = mtip_block_getgeo,
3847 .owner = THIS_MODULE
3851 * Block layer make request function.
3853 * This function is called by the kernel to process a BIO for
3856 * @queue Pointer to the request queue. Unused other than to obtain
3857 * the driver data structure.
3858 * @bio Pointer to the BIO.
3861 static void mtip_make_request(struct request_queue *queue, struct bio *bio)
3863 struct driver_data *dd = queue->queuedata;
3864 struct scatterlist *sg;
3865 struct bio_vec *bvec;
3867 int tag = 0, unaligned = 0;
3869 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3870 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3872 bio_endio(bio, -ENXIO);
3875 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
3876 bio_endio(bio, -ENODATA);
3879 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3881 bio_data_dir(bio))) {
3882 bio_endio(bio, -ENODATA);
3885 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))) {
3886 bio_endio(bio, -ENODATA);
3891 if (unlikely(bio->bi_rw & REQ_DISCARD)) {
3892 bio_endio(bio, mtip_send_trim(dd, bio->bi_sector,
3897 if (unlikely(!bio_has_data(bio))) {
3898 blk_queue_flush(queue, 0);
3903 if (bio_data_dir(bio) == WRITE && bio_sectors(bio) <= 64 &&
3905 if (bio->bi_sector % 8 != 0) /* Unaligned on 4k boundaries */
3907 else if (bio_sectors(bio) % 8 != 0) /* Aligned but not 4k/8k */
3911 sg = mtip_hw_get_scatterlist(dd, &tag, unaligned);
3912 if (likely(sg != NULL)) {
3913 blk_queue_bounce(queue, &bio);
3915 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3916 dev_warn(&dd->pdev->dev,
3917 "Maximum number of SGL entries exceeded\n");
3919 mtip_hw_release_scatterlist(dd, tag, unaligned);
3923 /* Create the scatter list for this bio. */
3924 bio_for_each_segment(bvec, bio, nents) {
3925 sg_set_page(&sg[nents],
3931 /* Issue the read/write. */
3932 mtip_hw_submit_io(dd,
3946 * Block layer initialization function.
3948 * This function is called once by the PCI layer for each P320
3949 * device that is connected to the system.
3951 * @dd Pointer to the driver data structure.
3954 * 0 on success else an error code.
3956 static int mtip_block_initialize(struct driver_data *dd)
3958 int rv = 0, wait_for_rebuild = 0;
3960 unsigned int index = 0;
3961 struct kobject *kobj;
3962 unsigned char thd_name[16];
3965 goto skip_create_disk; /* hw init done, before rebuild */
3967 /* Initialize the protocol layer. */
3968 wait_for_rebuild = mtip_hw_init(dd);
3969 if (wait_for_rebuild < 0) {
3970 dev_err(&dd->pdev->dev,
3971 "Protocol layer initialization failed\n");
3973 goto protocol_init_error;
3976 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
3977 if (dd->disk == NULL) {
3978 dev_err(&dd->pdev->dev,
3979 "Unable to allocate gendisk structure\n");
3981 goto alloc_disk_error;
3984 /* Generate the disk name, implemented same as in sd.c */
3986 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3989 spin_lock(&rssd_index_lock);
3990 rv = ida_get_new(&rssd_index_ida, &index);
3991 spin_unlock(&rssd_index_lock);
3992 } while (rv == -EAGAIN);
3997 rv = rssd_disk_name_format("rssd",
3999 dd->disk->disk_name,
4002 goto disk_index_error;
4004 dd->disk->driverfs_dev = &dd->pdev->dev;
4005 dd->disk->major = dd->major;
4006 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
4007 dd->disk->fops = &mtip_block_ops;
4008 dd->disk->private_data = dd;
4012 * if rebuild pending, start the service thread, and delay the block
4013 * queue creation and add_disk()
4015 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
4016 goto start_service_thread;
4019 /* Allocate the request queue. */
4020 dd->queue = blk_alloc_queue_node(GFP_KERNEL, dd->numa_node);
4021 if (dd->queue == NULL) {
4022 dev_err(&dd->pdev->dev,
4023 "Unable to allocate request queue\n");
4025 goto block_queue_alloc_init_error;
4028 /* Attach our request function to the request queue. */
4029 blk_queue_make_request(dd->queue, mtip_make_request);
4031 dd->disk->queue = dd->queue;
4032 dd->queue->queuedata = dd;
4034 /* Set device limits. */
4035 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
4036 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
4037 blk_queue_physical_block_size(dd->queue, 4096);
4038 blk_queue_max_hw_sectors(dd->queue, 0xffff);
4039 blk_queue_max_segment_size(dd->queue, 0x400000);
4040 blk_queue_io_min(dd->queue, 4096);
4043 * write back cache is not supported in the device. FUA depends on
4044 * write back cache support, hence setting flush support to zero.
4046 blk_queue_flush(dd->queue, 0);
4048 /* Signal trim support */
4049 if (dd->trim_supp == true) {
4050 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
4051 dd->queue->limits.discard_granularity = 4096;
4052 blk_queue_max_discard_sectors(dd->queue,
4053 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
4054 dd->queue->limits.discard_zeroes_data = 0;
4057 /* Set the capacity of the device in 512 byte sectors. */
4058 if (!(mtip_hw_get_capacity(dd, &capacity))) {
4059 dev_warn(&dd->pdev->dev,
4060 "Could not read drive capacity\n");
4062 goto read_capacity_error;
4064 set_capacity(dd->disk, capacity);
4066 /* Enable the block device and add it to /dev */
4070 * Now that the disk is active, initialize any sysfs attributes
4071 * managed by the protocol layer.
4073 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4075 mtip_hw_sysfs_init(dd, kobj);
4078 mtip_hw_debugfs_init(dd);
4080 if (dd->mtip_svc_handler) {
4081 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
4082 return rv; /* service thread created for handling rebuild */
4085 start_service_thread:
4086 sprintf(thd_name, "mtip_svc_thd_%02d", index);
4087 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
4088 dd, dd->numa_node, thd_name);
4090 if (IS_ERR(dd->mtip_svc_handler)) {
4091 dev_err(&dd->pdev->dev, "service thread failed to start\n");
4092 dd->mtip_svc_handler = NULL;
4094 goto kthread_run_error;
4096 wake_up_process(dd->mtip_svc_handler);
4097 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
4098 rv = wait_for_rebuild;
4103 mtip_hw_debugfs_exit(dd);
4105 /* Delete our gendisk. This also removes the device from /dev */
4106 del_gendisk(dd->disk);
4108 read_capacity_error:
4109 blk_cleanup_queue(dd->queue);
4111 block_queue_alloc_init_error:
4113 spin_lock(&rssd_index_lock);
4114 ida_remove(&rssd_index_ida, index);
4115 spin_unlock(&rssd_index_lock);
4121 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
4123 protocol_init_error:
4128 * Block layer deinitialization function.
4130 * Called by the PCI layer as each P320 device is removed.
4132 * @dd Pointer to the driver data structure.
4137 static int mtip_block_remove(struct driver_data *dd)
4139 struct kobject *kobj;
4141 if (dd->mtip_svc_handler) {
4142 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4143 wake_up_interruptible(&dd->port->svc_wait);
4144 kthread_stop(dd->mtip_svc_handler);
4147 /* Clean up the sysfs attributes, if created */
4148 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4149 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4151 mtip_hw_sysfs_exit(dd, kobj);
4155 mtip_hw_debugfs_exit(dd);
4158 * Delete our gendisk structure. This also removes the device
4162 if (dd->disk->queue)
4163 del_gendisk(dd->disk);
4168 spin_lock(&rssd_index_lock);
4169 ida_remove(&rssd_index_ida, dd->index);
4170 spin_unlock(&rssd_index_lock);
4172 blk_cleanup_queue(dd->queue);
4176 /* De-initialize the protocol layer. */
4183 * Function called by the PCI layer when just before the
4184 * machine shuts down.
4186 * If a protocol layer shutdown function is present it will be called
4189 * @dd Pointer to the driver data structure.
4194 static int mtip_block_shutdown(struct driver_data *dd)
4196 /* Delete our gendisk structure, and cleanup the blk queue. */
4198 dev_info(&dd->pdev->dev,
4199 "Shutting down %s ...\n", dd->disk->disk_name);
4201 if (dd->disk->queue) {
4202 del_gendisk(dd->disk);
4203 blk_cleanup_queue(dd->queue);
4210 spin_lock(&rssd_index_lock);
4211 ida_remove(&rssd_index_ida, dd->index);
4212 spin_unlock(&rssd_index_lock);
4214 mtip_hw_shutdown(dd);
4218 static int mtip_block_suspend(struct driver_data *dd)
4220 dev_info(&dd->pdev->dev,
4221 "Suspending %s ...\n", dd->disk->disk_name);
4222 mtip_hw_suspend(dd);
4226 static int mtip_block_resume(struct driver_data *dd)
4228 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4229 dd->disk->disk_name);
4234 static void drop_cpu(int cpu)
4239 static int get_least_used_cpu_on_node(int node)
4241 int cpu, least_used_cpu, least_cnt;
4242 const struct cpumask *node_mask;
4244 node_mask = cpumask_of_node(node);
4245 least_used_cpu = cpumask_first(node_mask);
4246 least_cnt = cpu_use[least_used_cpu];
4247 cpu = least_used_cpu;
4249 for_each_cpu(cpu, node_mask) {
4250 if (cpu_use[cpu] < least_cnt) {
4251 least_used_cpu = cpu;
4252 least_cnt = cpu_use[cpu];
4255 cpu_use[least_used_cpu]++;
4256 return least_used_cpu;
4259 /* Helper for selecting a node in round robin mode */
4260 static inline int mtip_get_next_rr_node(void)
4262 static int next_node = -1;
4264 if (next_node == -1) {
4265 next_node = first_online_node;
4269 next_node = next_online_node(next_node);
4270 if (next_node == MAX_NUMNODES)
4271 next_node = first_online_node;
4275 static DEFINE_HANDLER(0);
4276 static DEFINE_HANDLER(1);
4277 static DEFINE_HANDLER(2);
4278 static DEFINE_HANDLER(3);
4279 static DEFINE_HANDLER(4);
4280 static DEFINE_HANDLER(5);
4281 static DEFINE_HANDLER(6);
4282 static DEFINE_HANDLER(7);
4285 * Called for each supported PCI device detected.
4287 * This function allocates the private data structure, enables the
4288 * PCI device and then calls the block layer initialization function.
4291 * 0 on success else an error code.
4293 static int mtip_pci_probe(struct pci_dev *pdev,
4294 const struct pci_device_id *ent)
4297 struct driver_data *dd = NULL;
4299 const struct cpumask *node_mask;
4300 int cpu, i = 0, j = 0;
4301 int my_node = NUMA_NO_NODE;
4302 unsigned long flags;
4304 /* Allocate memory for this devices private data. */
4305 my_node = pcibus_to_node(pdev->bus);
4306 if (my_node != NUMA_NO_NODE) {
4307 if (!node_online(my_node))
4308 my_node = mtip_get_next_rr_node();
4310 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4311 my_node = mtip_get_next_rr_node();
4313 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4314 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
4315 cpu_to_node(smp_processor_id()), smp_processor_id());
4317 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
4320 "Unable to allocate memory for driver data\n");
4324 /* Attach the private data to this PCI device. */
4325 pci_set_drvdata(pdev, dd);
4327 rv = pcim_enable_device(pdev);
4329 dev_err(&pdev->dev, "Unable to enable device\n");
4333 /* Map BAR5 to memory. */
4334 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4336 dev_err(&pdev->dev, "Unable to map regions\n");
4340 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4341 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4344 rv = pci_set_consistent_dma_mask(pdev,
4347 dev_warn(&pdev->dev,
4348 "64-bit DMA enable failed\n");
4354 /* Copy the info we may need later into the private data structure. */
4355 dd->major = mtip_major;
4356 dd->instance = instance;
4358 dd->numa_node = my_node;
4360 INIT_LIST_HEAD(&dd->online_list);
4361 INIT_LIST_HEAD(&dd->remove_list);
4363 memset(dd->workq_name, 0, 32);
4364 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4366 dd->isr_workq = create_workqueue(dd->workq_name);
4367 if (!dd->isr_workq) {
4368 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
4370 goto block_initialize_err;
4373 memset(cpu_list, 0, sizeof(cpu_list));
4375 node_mask = cpumask_of_node(dd->numa_node);
4376 if (!cpumask_empty(node_mask)) {
4377 for_each_cpu(cpu, node_mask)
4379 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4380 j = strlen(cpu_list);
4383 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4385 topology_physical_package_id(cpumask_first(node_mask)),
4386 nr_cpus_node(dd->numa_node),
4389 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4391 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4392 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4393 cpu_to_node(dd->isr_binding), dd->isr_binding);
4395 /* first worker context always runs in ISR */
4396 dd->work[0].cpu_binding = dd->isr_binding;
4397 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4398 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4399 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4400 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4401 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4402 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4403 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4405 /* Log the bindings */
4406 for_each_present_cpu(cpu) {
4407 memset(cpu_list, 0, sizeof(cpu_list));
4408 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4409 if (dd->work[i].cpu_binding == cpu) {
4410 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4411 j = strlen(cpu_list);
4415 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4418 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4419 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4420 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4421 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4422 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4423 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4424 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4425 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4427 pci_set_master(pdev);
4428 rv = pci_enable_msi(pdev);
4430 dev_warn(&pdev->dev,
4431 "Unable to enable MSI interrupt.\n");
4432 goto block_initialize_err;
4435 /* Initialize the block layer. */
4436 rv = mtip_block_initialize(dd);
4439 "Unable to initialize block layer\n");
4440 goto block_initialize_err;
4444 * Increment the instance count so that each device has a unique
4448 if (rv != MTIP_FTL_REBUILD_MAGIC)
4449 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
4451 rv = 0; /* device in rebuild state, return 0 from probe */
4453 /* Add to online list even if in ftl rebuild */
4454 spin_lock_irqsave(&dev_lock, flags);
4455 list_add(&dd->online_list, &online_list);
4456 spin_unlock_irqrestore(&dev_lock, flags);
4460 block_initialize_err:
4461 pci_disable_msi(pdev);
4462 if (dd->isr_workq) {
4463 flush_workqueue(dd->isr_workq);
4464 destroy_workqueue(dd->isr_workq);
4465 drop_cpu(dd->work[0].cpu_binding);
4466 drop_cpu(dd->work[1].cpu_binding);
4467 drop_cpu(dd->work[2].cpu_binding);
4470 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4474 pci_set_drvdata(pdev, NULL);
4481 * Called for each probed device when the device is removed or the
4482 * driver is unloaded.
4487 static void mtip_pci_remove(struct pci_dev *pdev)
4489 struct driver_data *dd = pci_get_drvdata(pdev);
4491 unsigned long flags;
4493 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
4495 spin_lock_irqsave(&dev_lock, flags);
4496 list_del_init(&dd->online_list);
4497 list_add(&dd->remove_list, &removing_list);
4498 spin_unlock_irqrestore(&dev_lock, flags);
4500 if (mtip_check_surprise_removal(pdev)) {
4501 while (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
4504 if (counter == 10) {
4505 /* Cleanup the outstanding commands */
4506 mtip_command_cleanup(dd);
4512 /* Clean up the block layer. */
4513 mtip_block_remove(dd);
4515 if (dd->isr_workq) {
4516 flush_workqueue(dd->isr_workq);
4517 destroy_workqueue(dd->isr_workq);
4518 drop_cpu(dd->work[0].cpu_binding);
4519 drop_cpu(dd->work[1].cpu_binding);
4520 drop_cpu(dd->work[2].cpu_binding);
4523 pci_disable_msi(pdev);
4525 spin_lock_irqsave(&dev_lock, flags);
4526 list_del_init(&dd->remove_list);
4527 spin_unlock_irqrestore(&dev_lock, flags);
4530 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4534 * Called for each probed device when the device is suspended.
4540 static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4543 struct driver_data *dd = pci_get_drvdata(pdev);
4547 "Driver private datastructure is NULL\n");
4551 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
4553 /* Disable ports & interrupts then send standby immediate */
4554 rv = mtip_block_suspend(dd);
4557 "Failed to suspend controller\n");
4562 * Save the pci config space to pdev structure &
4563 * disable the device
4565 pci_save_state(pdev);
4566 pci_disable_device(pdev);
4568 /* Move to Low power state*/
4569 pci_set_power_state(pdev, PCI_D3hot);
4575 * Called for each probed device when the device is resumed.
4581 static int mtip_pci_resume(struct pci_dev *pdev)
4584 struct driver_data *dd;
4586 dd = pci_get_drvdata(pdev);
4589 "Driver private datastructure is NULL\n");
4593 /* Move the device to active State */
4594 pci_set_power_state(pdev, PCI_D0);
4596 /* Restore PCI configuration space */
4597 pci_restore_state(pdev);
4599 /* Enable the PCI device*/
4600 rv = pcim_enable_device(pdev);
4603 "Failed to enable card during resume\n");
4606 pci_set_master(pdev);
4609 * Calls hbaReset, initPort, & startPort function
4610 * then enables interrupts
4612 rv = mtip_block_resume(dd);
4614 dev_err(&pdev->dev, "Unable to resume\n");
4617 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
4628 static void mtip_pci_shutdown(struct pci_dev *pdev)
4630 struct driver_data *dd = pci_get_drvdata(pdev);
4632 mtip_block_shutdown(dd);
4635 /* Table of device ids supported by this driver. */
4636 static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
4637 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4638 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4639 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4640 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4641 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4642 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4643 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
4647 /* Structure that describes the PCI driver functions. */
4648 static struct pci_driver mtip_pci_driver = {
4649 .name = MTIP_DRV_NAME,
4650 .id_table = mtip_pci_tbl,
4651 .probe = mtip_pci_probe,
4652 .remove = mtip_pci_remove,
4653 .suspend = mtip_pci_suspend,
4654 .resume = mtip_pci_resume,
4655 .shutdown = mtip_pci_shutdown,
4658 MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4661 * Module initialization function.
4663 * Called once when the module is loaded. This function allocates a major
4664 * block device number to the Cyclone devices and registers the PCI layer
4668 * 0 on success else error code.
4670 static int __init mtip_init(void)
4674 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
4676 spin_lock_init(&dev_lock);
4678 INIT_LIST_HEAD(&online_list);
4679 INIT_LIST_HEAD(&removing_list);
4681 /* Allocate a major block device number to use with this driver. */
4682 error = register_blkdev(0, MTIP_DRV_NAME);
4684 pr_err("Unable to register block device (%d)\n",
4690 dfs_parent = debugfs_create_dir("rssd", NULL);
4691 if (IS_ERR_OR_NULL(dfs_parent)) {
4692 pr_warn("Error creating debugfs parent\n");
4696 dfs_device_status = debugfs_create_file("device_status",
4697 S_IRUGO, dfs_parent, NULL,
4698 &mtip_device_status_fops);
4699 if (IS_ERR_OR_NULL(dfs_device_status)) {
4700 pr_err("Error creating device_status node\n");
4701 dfs_device_status = NULL;
4705 /* Register our PCI operations. */
4706 error = pci_register_driver(&mtip_pci_driver);
4708 debugfs_remove(dfs_parent);
4709 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4716 * Module de-initialization function.
4718 * Called once when the module is unloaded. This function deallocates
4719 * the major block device number allocated by mtip_init() and
4720 * unregisters the PCI layer of the driver.
4725 static void __exit mtip_exit(void)
4727 debugfs_remove_recursive(dfs_parent);
4729 /* Release the allocated major block device number. */
4730 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4732 /* Unregister the PCI driver. */
4733 pci_unregister_driver(&mtip_pci_driver);
4736 MODULE_AUTHOR("Micron Technology, Inc");
4737 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4738 MODULE_LICENSE("GPL");
4739 MODULE_VERSION(MTIP_DRV_VERSION);
4741 module_init(mtip_init);
4742 module_exit(mtip_exit);