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)
129 struct driver_data *dd = pci_get_drvdata(pdev);
134 /* Read the vendorID from the configuration space */
135 pci_read_config_word(pdev, 0x00, &vendor_id);
136 if (vendor_id == 0xFFFF) {
139 set_bit(QUEUE_FLAG_DEAD, &dd->queue->queue_flags);
141 dev_warn(&dd->pdev->dev,
142 "%s: dd->queue is NULL\n", __func__);
144 set_bit(MTIP_PF_SR_CLEANUP_BIT, &dd->port->flags);
145 wake_up_interruptible(&dd->port->svc_wait);
147 dev_warn(&dd->pdev->dev,
148 "%s: dd->port is NULL\n", __func__);
149 return true; /* device removed */
152 return false; /* device present */
156 * Obtain an empty command slot.
158 * This function needs to be reentrant since it could be called
159 * at the same time on multiple CPUs. The allocation of the
160 * command slot must be atomic.
162 * @port Pointer to the port data structure.
165 * >= 0 Index of command slot obtained.
166 * -1 No command slots available.
168 static int get_slot(struct mtip_port *port)
171 unsigned int num_command_slots = port->dd->slot_groups * 32;
174 * Try 10 times, because there is a small race here.
175 * that's ok, because it's still cheaper than a lock.
177 * Race: Since this section is not protected by lock, same bit
178 * could be chosen by different process contexts running in
179 * different processor. So instead of costly lock, we are going
182 for (i = 0; i < 10; i++) {
183 slot = find_next_zero_bit(port->allocated,
184 num_command_slots, 1);
185 if ((slot < num_command_slots) &&
186 (!test_and_set_bit(slot, port->allocated)))
189 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
191 mtip_check_surprise_removal(port->dd->pdev);
196 * Release a command slot.
198 * @port Pointer to the port data structure.
199 * @tag Tag of command to release
204 static inline void release_slot(struct mtip_port *port, int tag)
206 smp_mb__before_clear_bit();
207 clear_bit(tag, port->allocated);
208 smp_mb__after_clear_bit();
212 * IO completion function.
214 * This completion function is called by the driver ISR when a
215 * command that was issued by the kernel completes. It first calls the
216 * asynchronous completion function which normally calls back into the block
217 * layer passing the asynchronous callback data, then unmaps the
218 * scatter list associated with the completed command, and finally
219 * clears the allocated bit associated with the completed command.
221 * @port Pointer to the port data structure.
222 * @tag Tag of the command.
223 * @data Pointer to driver_data.
224 * @status Completion status.
229 static void mtip_async_complete(struct mtip_port *port,
234 struct mtip_cmd *command;
235 struct driver_data *dd = data;
236 int cb_status = status ? -EIO : 0;
238 if (unlikely(!dd) || unlikely(!port))
241 command = &port->commands[tag];
243 if (unlikely(status == PORT_IRQ_TF_ERR)) {
244 dev_warn(&port->dd->pdev->dev,
245 "Command tag %d failed due to TFE\n", tag);
248 /* Upper layer callback */
249 if (likely(command->async_callback))
250 command->async_callback(command->async_data, cb_status);
252 command->async_callback = NULL;
253 command->comp_func = NULL;
255 /* Unmap the DMA scatter list entries */
256 dma_unmap_sg(&dd->pdev->dev,
258 command->scatter_ents,
261 /* Clear the allocated and active bits for the command */
262 atomic_set(&port->commands[tag].active, 0);
263 release_slot(port, tag);
269 * This function is called for clean the pending command in the
270 * command slot during the surprise removal of device and return
271 * error to the upper layer.
273 * @dd Pointer to the DRIVER_DATA structure.
278 static void mtip_command_cleanup(struct driver_data *dd)
281 struct mtip_cmd *cmd;
282 struct mtip_port *port = dd->port;
283 unsigned int num_cmd_slots = dd->slot_groups * 32;
285 if (!test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
291 cmd = &port->commands[MTIP_TAG_INTERNAL];
292 if (atomic_read(&cmd->active))
293 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL]) &
294 (1 << MTIP_TAG_INTERNAL))
296 cmd->comp_func(port, MTIP_TAG_INTERNAL,
297 cmd->comp_data, -ENODEV);
300 tag = find_next_bit(port->allocated, num_cmd_slots, tag);
301 if (tag >= num_cmd_slots)
304 cmd = &port->commands[tag];
305 if (atomic_read(&cmd->active))
306 mtip_async_complete(port, tag, dd, -ENODEV);
309 set_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag);
313 * Reset the HBA (without sleeping)
315 * @dd Pointer to the driver data structure.
318 * 0 The reset was successful.
319 * -1 The HBA Reset bit did not clear.
321 static int mtip_hba_reset(struct driver_data *dd)
323 unsigned long timeout;
325 /* Set the reset bit */
326 writel(HOST_RESET, dd->mmio + HOST_CTL);
329 readl(dd->mmio + HOST_CTL);
331 /* Spin for up to 2 seconds, waiting for reset acknowledgement */
332 timeout = jiffies + msecs_to_jiffies(2000);
335 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
338 } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
339 && time_before(jiffies, timeout));
341 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
348 * Issue a command to the hardware.
350 * Set the appropriate bit in the s_active and Command Issue hardware
351 * registers, causing hardware command processing to begin.
353 * @port Pointer to the port structure.
354 * @tag The tag of the command to be issued.
359 static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
361 int group = tag >> 5;
363 atomic_set(&port->commands[tag].active, 1);
365 /* guard SACT and CI registers */
366 spin_lock(&port->cmd_issue_lock[group]);
367 writel((1 << MTIP_TAG_BIT(tag)),
368 port->s_active[MTIP_TAG_INDEX(tag)]);
369 writel((1 << MTIP_TAG_BIT(tag)),
370 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
371 spin_unlock(&port->cmd_issue_lock[group]);
373 /* Set the command's timeout value.*/
374 port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
375 MTIP_NCQ_COMMAND_TIMEOUT_MS);
379 * Enable/disable the reception of FIS
381 * @port Pointer to the port data structure
382 * @enable 1 to enable, 0 to disable
385 * Previous state: 1 enabled, 0 disabled
387 static int mtip_enable_fis(struct mtip_port *port, int enable)
391 /* enable FIS reception */
392 tmp = readl(port->mmio + PORT_CMD);
394 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
396 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
399 readl(port->mmio + PORT_CMD);
401 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
405 * Enable/disable the DMA engine
407 * @port Pointer to the port data structure
408 * @enable 1 to enable, 0 to disable
411 * Previous state: 1 enabled, 0 disabled.
413 static int mtip_enable_engine(struct mtip_port *port, int enable)
417 /* enable FIS reception */
418 tmp = readl(port->mmio + PORT_CMD);
420 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
422 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
424 readl(port->mmio + PORT_CMD);
425 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
429 * Enables the port DMA engine and FIS reception.
434 static inline void mtip_start_port(struct mtip_port *port)
436 /* Enable FIS reception */
437 mtip_enable_fis(port, 1);
439 /* Enable the DMA engine */
440 mtip_enable_engine(port, 1);
444 * Deinitialize a port by disabling port interrupts, the DMA engine,
447 * @port Pointer to the port structure
452 static inline void mtip_deinit_port(struct mtip_port *port)
454 /* Disable interrupts on this port */
455 writel(0, port->mmio + PORT_IRQ_MASK);
457 /* Disable the DMA engine */
458 mtip_enable_engine(port, 0);
460 /* Disable FIS reception */
461 mtip_enable_fis(port, 0);
467 * This function deinitializes the port by calling mtip_deinit_port() and
468 * then initializes it by setting the command header and RX FIS addresses,
469 * clearing the SError register and any pending port interrupts before
470 * re-enabling the default set of port interrupts.
472 * @port Pointer to the port structure.
477 static void mtip_init_port(struct mtip_port *port)
480 mtip_deinit_port(port);
482 /* Program the command list base and FIS base addresses */
483 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
484 writel((port->command_list_dma >> 16) >> 16,
485 port->mmio + PORT_LST_ADDR_HI);
486 writel((port->rxfis_dma >> 16) >> 16,
487 port->mmio + PORT_FIS_ADDR_HI);
490 writel(port->command_list_dma & 0xFFFFFFFF,
491 port->mmio + PORT_LST_ADDR);
492 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
495 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
497 /* reset the completed registers.*/
498 for (i = 0; i < port->dd->slot_groups; i++)
499 writel(0xFFFFFFFF, port->completed[i]);
501 /* Clear any pending interrupts for this port */
502 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
504 /* Clear any pending interrupts on the HBA. */
505 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
506 port->dd->mmio + HOST_IRQ_STAT);
508 /* Enable port interrupts */
509 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
515 * @port Pointer to the port data structure.
520 static void mtip_restart_port(struct mtip_port *port)
522 unsigned long timeout;
524 /* Disable the DMA engine */
525 mtip_enable_engine(port, 0);
527 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
528 timeout = jiffies + msecs_to_jiffies(500);
529 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
530 && time_before(jiffies, timeout))
533 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
537 * Chip quirk: escalate to hba reset if
538 * PxCMD.CR not clear after 500 ms
540 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
541 dev_warn(&port->dd->pdev->dev,
542 "PxCMD.CR not clear, escalating reset\n");
544 if (mtip_hba_reset(port->dd))
545 dev_err(&port->dd->pdev->dev,
546 "HBA reset escalation failed.\n");
548 /* 30 ms delay before com reset to quiesce chip */
552 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
555 writel(readl(port->mmio + PORT_SCR_CTL) |
556 1, port->mmio + PORT_SCR_CTL);
557 readl(port->mmio + PORT_SCR_CTL);
559 /* Wait 1 ms to quiesce chip function */
560 timeout = jiffies + msecs_to_jiffies(1);
561 while (time_before(jiffies, timeout))
564 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
567 /* Clear PxSCTL.DET */
568 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
569 port->mmio + PORT_SCR_CTL);
570 readl(port->mmio + PORT_SCR_CTL);
572 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
573 timeout = jiffies + msecs_to_jiffies(500);
574 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
575 && time_before(jiffies, timeout))
578 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
581 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
582 dev_warn(&port->dd->pdev->dev,
583 "COM reset failed\n");
585 mtip_init_port(port);
586 mtip_start_port(port);
590 static int mtip_device_reset(struct driver_data *dd)
594 if (mtip_check_surprise_removal(dd->pdev))
597 if (mtip_hba_reset(dd) < 0)
601 mtip_init_port(dd->port);
602 mtip_start_port(dd->port);
604 /* Enable interrupts on the HBA. */
605 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
606 dd->mmio + HOST_CTL);
611 * Helper function for tag logging
613 static void print_tags(struct driver_data *dd,
615 unsigned long *tagbits,
618 unsigned char tagmap[128];
619 int group, tagmap_len = 0;
621 memset(tagmap, 0, sizeof(tagmap));
622 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
623 tagmap_len = sprintf(tagmap + tagmap_len, "%016lX ",
625 dev_warn(&dd->pdev->dev,
626 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
630 * Called periodically to see if any read/write commands are
631 * taking too long to complete.
633 * @data Pointer to the PORT data structure.
638 static void mtip_timeout_function(unsigned long int data)
640 struct mtip_port *port = (struct mtip_port *) data;
641 struct host_to_dev_fis *fis;
642 struct mtip_cmd *command;
643 int tag, cmdto_cnt = 0;
644 unsigned int bit, group;
645 unsigned int num_command_slots;
646 unsigned long to, tagaccum[SLOTBITS_IN_LONGS];
651 if (unlikely(port->dd->sr))
654 if (test_bit(MTIP_DDF_RESUME_BIT, &port->dd->dd_flag)) {
655 mod_timer(&port->cmd_timer,
656 jiffies + msecs_to_jiffies(30000));
659 /* clear the tag accumulator */
660 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
661 num_command_slots = port->dd->slot_groups * 32;
663 for (tag = 0; tag < num_command_slots; tag++) {
665 * Skip internal command slot as it has
666 * its own timeout mechanism
668 if (tag == MTIP_TAG_INTERNAL)
671 if (atomic_read(&port->commands[tag].active) &&
672 (time_after(jiffies, port->commands[tag].comp_time))) {
676 command = &port->commands[tag];
677 fis = (struct host_to_dev_fis *) command->command;
679 set_bit(tag, tagaccum);
682 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
685 * Clear the completed bit. This should prevent
686 * any interrupt handlers from trying to retire
689 writel(1 << bit, port->completed[group]);
691 /* Call the async completion callback. */
692 if (likely(command->async_callback))
693 command->async_callback(command->async_data,
695 command->async_callback = NULL;
696 command->comp_func = NULL;
698 /* Unmap the DMA scatter list entries */
699 dma_unmap_sg(&port->dd->pdev->dev,
701 command->scatter_ents,
705 * Clear the allocated bit and active tag for the
708 atomic_set(&port->commands[tag].active, 0);
709 release_slot(port, tag);
716 print_tags(port->dd, "timed out", tagaccum, cmdto_cnt);
717 if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
718 mtip_device_reset(port->dd);
719 wake_up_interruptible(&port->svc_wait);
721 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
724 if (port->ic_pause_timer) {
725 to = port->ic_pause_timer + msecs_to_jiffies(1000);
726 if (time_after(jiffies, to)) {
727 if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
728 port->ic_pause_timer = 0;
729 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
730 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
731 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
732 wake_up_interruptible(&port->svc_wait);
739 /* Restart the timer */
740 mod_timer(&port->cmd_timer,
741 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
745 * Internal command completion callback function.
747 * This function is normally called by the driver ISR when an internal
748 * command completed. This function signals the command completion by
749 * calling complete().
751 * @port Pointer to the port data structure.
752 * @tag Tag of the command that has completed.
753 * @data Pointer to a completion structure.
754 * @status Completion status.
759 static void mtip_completion(struct mtip_port *port,
764 struct mtip_cmd *command = &port->commands[tag];
765 struct completion *waiting = data;
766 if (unlikely(status == PORT_IRQ_TF_ERR))
767 dev_warn(&port->dd->pdev->dev,
768 "Internal command %d completed with TFE\n", tag);
770 command->async_callback = NULL;
771 command->comp_func = NULL;
776 static void mtip_null_completion(struct mtip_port *port,
784 static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
785 dma_addr_t buffer_dma, unsigned int sectors);
786 static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
787 struct smart_attr *attrib);
791 * @dd Pointer to the DRIVER_DATA structure.
796 static void mtip_handle_tfe(struct driver_data *dd)
798 int group, tag, bit, reissue, rv;
799 struct mtip_port *port;
800 struct mtip_cmd *cmd;
802 struct host_to_dev_fis *fis;
803 unsigned long tagaccum[SLOTBITS_IN_LONGS];
804 unsigned int cmd_cnt = 0;
806 char *fail_reason = NULL;
807 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
809 dev_warn(&dd->pdev->dev, "Taskfile error\n");
813 /* Stop the timer to prevent command timeouts. */
814 del_timer(&port->cmd_timer);
815 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
817 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
818 test_bit(MTIP_TAG_INTERNAL, port->allocated)) {
819 cmd = &port->commands[MTIP_TAG_INTERNAL];
820 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
822 atomic_inc(&cmd->active); /* active > 1 indicates error */
823 if (cmd->comp_data && cmd->comp_func) {
824 cmd->comp_func(port, MTIP_TAG_INTERNAL,
825 cmd->comp_data, PORT_IRQ_TF_ERR);
827 goto handle_tfe_exit;
830 /* clear the tag accumulator */
831 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
833 /* Loop through all the groups */
834 for (group = 0; group < dd->slot_groups; group++) {
835 completed = readl(port->completed[group]);
837 /* clear completed status register in the hardware.*/
838 writel(completed, port->completed[group]);
840 /* Process successfully completed commands */
841 for (bit = 0; bit < 32 && completed; bit++) {
842 if (!(completed & (1<<bit)))
844 tag = (group << 5) + bit;
846 /* Skip the internal command slot */
847 if (tag == MTIP_TAG_INTERNAL)
850 cmd = &port->commands[tag];
851 if (likely(cmd->comp_func)) {
852 set_bit(tag, tagaccum);
854 atomic_set(&cmd->active, 0);
860 dev_err(&port->dd->pdev->dev,
861 "Missing completion func for tag %d",
863 if (mtip_check_surprise_removal(dd->pdev)) {
864 /* don't proceed further */
871 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
873 /* Restart the port */
875 mtip_restart_port(port);
877 /* Trying to determine the cause of the error */
878 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
880 dd->port->log_buf_dma, 1);
882 dev_warn(&dd->pdev->dev,
883 "Error in READ LOG EXT (10h) command\n");
884 /* non-critical error, don't fail the load */
886 buf = (unsigned char *)dd->port->log_buf;
887 if (buf[259] & 0x1) {
888 dev_info(&dd->pdev->dev,
889 "Write protect bit is set.\n");
890 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
891 fail_all_ncq_write = 1;
892 fail_reason = "write protect";
894 if (buf[288] == 0xF7) {
895 dev_info(&dd->pdev->dev,
896 "Exceeded Tmax, drive in thermal shutdown.\n");
897 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
898 fail_all_ncq_cmds = 1;
899 fail_reason = "thermal shutdown";
901 if (buf[288] == 0xBF) {
902 dev_info(&dd->pdev->dev,
903 "Drive indicates rebuild has failed.\n");
904 fail_all_ncq_cmds = 1;
905 fail_reason = "rebuild failed";
909 /* clear the tag accumulator */
910 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
912 /* Loop through all the groups */
913 for (group = 0; group < dd->slot_groups; group++) {
914 for (bit = 0; bit < 32; bit++) {
916 tag = (group << 5) + bit;
917 cmd = &port->commands[tag];
919 /* If the active bit is set re-issue the command */
920 if (atomic_read(&cmd->active) == 0)
923 fis = (struct host_to_dev_fis *)cmd->command;
925 /* Should re-issue? */
926 if (tag == MTIP_TAG_INTERNAL ||
927 fis->command == ATA_CMD_SET_FEATURES)
930 if (fail_all_ncq_cmds ||
931 (fail_all_ncq_write &&
932 fis->command == ATA_CMD_FPDMA_WRITE)) {
933 dev_warn(&dd->pdev->dev,
934 " Fail: %s w/tag %d [%s].\n",
935 fis->command == ATA_CMD_FPDMA_WRITE ?
938 fail_reason != NULL ?
939 fail_reason : "unknown");
940 atomic_set(&cmd->active, 0);
941 if (cmd->comp_func) {
942 cmd->comp_func(port, tag,
951 * First check if this command has
952 * exceeded its retries.
954 if (reissue && (cmd->retries-- > 0)) {
956 set_bit(tag, tagaccum);
958 /* Re-issue the command. */
959 mtip_issue_ncq_command(port, tag);
964 /* Retire a command that will not be reissued */
965 dev_warn(&port->dd->pdev->dev,
966 "retiring tag %d\n", tag);
967 atomic_set(&cmd->active, 0);
976 dev_warn(&port->dd->pdev->dev,
977 "Bad completion for tag %d\n",
981 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
984 /* clear eh_active */
985 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
986 wake_up_interruptible(&port->svc_wait);
988 mod_timer(&port->cmd_timer,
989 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
993 * Handle a set device bits interrupt
995 static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
998 struct driver_data *dd = port->dd;
1000 struct mtip_cmd *command;
1003 WARN_ON_ONCE(!completed);
1006 /* clear completed status register in the hardware.*/
1007 writel(completed, port->completed[group]);
1009 /* Process completed commands. */
1010 for (bit = 0; (bit < 32) && completed; bit++) {
1011 if (completed & 0x01) {
1012 tag = (group << 5) | bit;
1014 /* skip internal command slot. */
1015 if (unlikely(tag == MTIP_TAG_INTERNAL))
1018 command = &port->commands[tag];
1019 /* make internal callback */
1020 if (likely(command->comp_func)) {
1027 dev_dbg(&dd->pdev->dev,
1028 "Null completion for tag %d",
1031 if (mtip_check_surprise_removal(
1040 /* If last, re-enable interrupts */
1041 if (atomic_dec_return(&dd->irq_workers_active) == 0)
1042 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
1046 * Process legacy pio and d2h interrupts
1048 static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
1050 struct mtip_port *port = dd->port;
1051 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
1053 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
1054 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1055 & (1 << MTIP_TAG_INTERNAL))) {
1056 if (cmd->comp_func) {
1057 cmd->comp_func(port,
1069 * Demux and handle errors
1071 static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
1073 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
1074 mtip_handle_tfe(dd);
1076 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
1077 dev_warn(&dd->pdev->dev,
1078 "Clearing PxSERR.DIAG.x\n");
1079 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
1082 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
1083 dev_warn(&dd->pdev->dev,
1084 "Clearing PxSERR.DIAG.n\n");
1085 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
1088 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
1089 dev_warn(&dd->pdev->dev,
1090 "Port stat errors %x unhandled\n",
1091 (port_stat & ~PORT_IRQ_HANDLED));
1095 static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
1097 struct driver_data *dd = (struct driver_data *) data;
1098 struct mtip_port *port = dd->port;
1099 u32 hba_stat, port_stat;
1101 int do_irq_enable = 1, i, workers;
1102 struct mtip_work *twork;
1104 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1108 /* Acknowledge the interrupt status on the port.*/
1109 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1110 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1112 /* Demux port status */
1113 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
1115 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
1117 /* Start at 1: group zero is always local? */
1118 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
1120 twork = &dd->work[i];
1121 twork->completed = readl(port->completed[i]);
1122 if (twork->completed)
1126 atomic_set(&dd->irq_workers_active, workers);
1128 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
1129 twork = &dd->work[i];
1130 if (twork->completed)
1137 if (likely(dd->work[0].completed))
1138 mtip_workq_sdbfx(port, 0,
1139 dd->work[0].completed);
1143 * Chip quirk: SDB interrupt but nothing
1150 if (unlikely(port_stat & PORT_IRQ_ERR)) {
1151 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
1152 /* don't proceed further */
1155 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1159 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
1162 if (unlikely(port_stat & PORT_IRQ_LEGACY))
1163 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
1166 /* acknowledge interrupt */
1167 if (unlikely(do_irq_enable))
1168 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
1174 * HBA interrupt subroutine.
1177 * @instance Pointer to the driver data structure.
1180 * IRQ_HANDLED A HBA interrupt was pending and handled.
1181 * IRQ_NONE This interrupt was not for the HBA.
1183 static irqreturn_t mtip_irq_handler(int irq, void *instance)
1185 struct driver_data *dd = instance;
1187 return mtip_handle_irq(dd);
1190 static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1192 atomic_set(&port->commands[tag].active, 1);
1193 writel(1 << MTIP_TAG_BIT(tag),
1194 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1197 static bool mtip_pause_ncq(struct mtip_port *port,
1198 struct host_to_dev_fis *fis)
1200 struct host_to_dev_fis *reply;
1201 unsigned long task_file_data;
1203 reply = port->rxfis + RX_FIS_D2H_REG;
1204 task_file_data = readl(port->mmio+PORT_TFDATA);
1206 if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
1207 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1209 if ((task_file_data & 1))
1212 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
1213 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1214 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1215 port->ic_pause_timer = jiffies;
1217 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1218 (fis->features == 0x03)) {
1219 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1220 port->ic_pause_timer = jiffies;
1222 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1223 ((fis->command == 0xFC) &&
1224 (fis->features == 0x27 || fis->features == 0x72 ||
1225 fis->features == 0x62 || fis->features == 0x26))) {
1226 /* Com reset after secure erase or lowlevel format */
1227 mtip_restart_port(port);
1235 * Wait for port to quiesce
1237 * @port Pointer to port data structure
1238 * @timeout Max duration to wait (ms)
1242 * -EBUSY Commands still active
1244 static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1248 unsigned int active = 1;
1250 to = jiffies + msecs_to_jiffies(timeout);
1252 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1253 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
1255 continue; /* svc thd is actively issuing commands */
1257 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
1260 * Ignore s_active bit 0 of array element 0.
1261 * This bit will always be set
1263 active = readl(port->s_active[0]) & 0xFFFFFFFE;
1264 for (n = 1; n < port->dd->slot_groups; n++)
1265 active |= readl(port->s_active[n]);
1271 } while (time_before(jiffies, to));
1273 return active ? -EBUSY : 0;
1277 * Execute an internal command and wait for the completion.
1279 * @port Pointer to the port data structure.
1280 * @fis Pointer to the FIS that describes the command.
1281 * @fis_len Length in WORDS of the FIS.
1282 * @buffer DMA accessible for command data.
1283 * @buf_len Length, in bytes, of the data buffer.
1284 * @opts Command header options, excluding the FIS length
1285 * and the number of PRD entries.
1286 * @timeout Time in ms to wait for the command to complete.
1289 * 0 Command completed successfully.
1290 * -EFAULT The buffer address is not correctly aligned.
1291 * -EBUSY Internal command or other IO in progress.
1292 * -EAGAIN Time out waiting for command to complete.
1294 static int mtip_exec_internal_command(struct mtip_port *port,
1295 struct host_to_dev_fis *fis,
1301 unsigned long timeout)
1303 struct mtip_cmd_sg *command_sg;
1304 DECLARE_COMPLETION_ONSTACK(wait);
1305 int rv = 0, ready2go = 1;
1306 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
1308 struct driver_data *dd = port->dd;
1310 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1311 if (buffer & 0x00000007) {
1312 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
1316 to = jiffies + msecs_to_jiffies(timeout);
1318 ready2go = !test_and_set_bit(MTIP_TAG_INTERNAL,
1323 } while (time_before(jiffies, to));
1325 dev_warn(&dd->pdev->dev,
1326 "Internal cmd active. new cmd [%02X]\n", fis->command);
1329 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1330 port->ic_pause_timer = 0;
1332 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1333 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1335 if (atomic == GFP_KERNEL) {
1336 if (fis->command != ATA_CMD_STANDBYNOW1) {
1337 /* wait for io to complete if non atomic */
1338 if (mtip_quiesce_io(port, 5000) < 0) {
1339 dev_warn(&dd->pdev->dev,
1340 "Failed to quiesce IO\n");
1341 release_slot(port, MTIP_TAG_INTERNAL);
1342 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1343 wake_up_interruptible(&port->svc_wait);
1348 /* Set the completion function and data for the command. */
1349 int_cmd->comp_data = &wait;
1350 int_cmd->comp_func = mtip_completion;
1353 /* Clear completion - we're going to poll */
1354 int_cmd->comp_data = NULL;
1355 int_cmd->comp_func = mtip_null_completion;
1358 /* Copy the command to the command table */
1359 memcpy(int_cmd->command, fis, fis_len*4);
1361 /* Populate the SG list */
1362 int_cmd->command_header->opts =
1363 __force_bit2int cpu_to_le32(opts | fis_len);
1365 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1368 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1370 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1371 command_sg->dba_upper =
1372 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
1374 int_cmd->command_header->opts |=
1375 __force_bit2int cpu_to_le32((1 << 16));
1378 /* Populate the command header */
1379 int_cmd->command_header->byte_count = 0;
1381 /* Issue the command to the hardware */
1382 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1384 if (atomic == GFP_KERNEL) {
1385 /* Wait for the command to complete or timeout. */
1386 if (wait_for_completion_interruptible_timeout(
1388 msecs_to_jiffies(timeout)) <= 0) {
1389 if (rv == -ERESTARTSYS) { /* interrupted */
1390 dev_err(&dd->pdev->dev,
1391 "Internal command [%02X] was interrupted after %lu ms\n",
1392 fis->command, timeout);
1395 } else if (rv == 0) /* timeout */
1396 dev_err(&dd->pdev->dev,
1397 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1398 fis->command, timeout);
1400 dev_err(&dd->pdev->dev,
1401 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1402 fis->command, rv, timeout);
1404 if (mtip_check_surprise_removal(dd->pdev) ||
1405 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1407 dev_err(&dd->pdev->dev,
1408 "Internal command [%02X] wait returned due to SR\n",
1413 mtip_device_reset(dd); /* recover from timeout issue */
1418 u32 hba_stat, port_stat;
1420 /* Spin for <timeout> checking if command still outstanding */
1421 timeout = jiffies + msecs_to_jiffies(timeout);
1422 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1423 & (1 << MTIP_TAG_INTERNAL))
1424 && time_before(jiffies, timeout)) {
1425 if (mtip_check_surprise_removal(dd->pdev)) {
1429 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
1430 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1435 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1439 if (port_stat & PORT_IRQ_ERR) {
1440 dev_err(&dd->pdev->dev,
1441 "Internal command [%02X] failed\n",
1443 mtip_device_reset(dd);
1447 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1448 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1451 dd->mmio + HOST_IRQ_STAT);
1457 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1458 & (1 << MTIP_TAG_INTERNAL)) {
1460 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1461 mtip_device_reset(dd);
1466 /* Clear the allocated and active bits for the internal command. */
1467 atomic_set(&int_cmd->active, 0);
1468 release_slot(port, MTIP_TAG_INTERNAL);
1469 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1473 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1474 wake_up_interruptible(&port->svc_wait);
1480 * Byte-swap ATA ID strings.
1482 * ATA identify data contains strings in byte-swapped 16-bit words.
1483 * They must be swapped (on all architectures) to be usable as C strings.
1484 * This function swaps bytes in-place.
1486 * @buf The buffer location of the string
1487 * @len The number of bytes to swap
1492 static inline void ata_swap_string(u16 *buf, unsigned int len)
1495 for (i = 0; i < (len/2); i++)
1496 be16_to_cpus(&buf[i]);
1500 * Request the device identity information.
1502 * If a user space buffer is not specified, i.e. is NULL, the
1503 * identify information is still read from the drive and placed
1504 * into the identify data buffer (@e port->identify) in the
1505 * port data structure.
1506 * When the identify buffer contains valid identify information @e
1507 * port->identify_valid is non-zero.
1509 * @port Pointer to the port structure.
1510 * @user_buffer A user space buffer where the identify data should be
1514 * 0 Command completed successfully.
1515 * -EFAULT An error occurred while coping data to the user buffer.
1516 * -1 Command failed.
1518 static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1521 struct host_to_dev_fis fis;
1523 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
1526 /* Build the FIS. */
1527 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1530 fis.command = ATA_CMD_ID_ATA;
1532 /* Set the identify information as invalid. */
1533 port->identify_valid = 0;
1535 /* Clear the identify information. */
1536 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1538 /* Execute the command. */
1539 if (mtip_exec_internal_command(port,
1543 sizeof(u16) * ATA_ID_WORDS,
1546 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1553 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1554 * perform field-sensitive swapping on the string fields.
1555 * See the kernel use of ata_id_string() for proof of this.
1557 #ifdef __LITTLE_ENDIAN
1558 ata_swap_string(port->identify + 27, 40); /* model string*/
1559 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1560 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1564 for (i = 0; i < ATA_ID_WORDS; i++)
1565 port->identify[i] = le16_to_cpu(port->identify[i]);
1569 #ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
1570 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1571 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1572 port->dd->trim_supp = true;
1575 port->dd->trim_supp = false;
1577 /* Set the identify buffer as valid. */
1578 port->identify_valid = 1;
1584 ATA_ID_WORDS * sizeof(u16))) {
1595 * Issue a standby immediate command to the device.
1597 * @port Pointer to the port structure.
1600 * 0 Command was executed successfully.
1601 * -1 An error occurred while executing the command.
1603 static int mtip_standby_immediate(struct mtip_port *port)
1606 struct host_to_dev_fis fis;
1607 unsigned long start;
1609 /* Build the FIS. */
1610 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1613 fis.command = ATA_CMD_STANDBYNOW1;
1616 rv = mtip_exec_internal_command(port,
1624 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1625 jiffies_to_msecs(jiffies - start));
1627 dev_warn(&port->dd->pdev->dev,
1628 "STANDBY IMMEDIATE command failed.\n");
1634 * Issue a READ LOG EXT command to the device.
1636 * @port pointer to the port structure.
1637 * @page page number to fetch
1638 * @buffer pointer to buffer
1639 * @buffer_dma dma address corresponding to @buffer
1640 * @sectors page length to fetch, in sectors
1643 * @rv return value from mtip_exec_internal_command()
1645 static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1646 dma_addr_t buffer_dma, unsigned int sectors)
1648 struct host_to_dev_fis fis;
1650 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1653 fis.command = ATA_CMD_READ_LOG_EXT;
1654 fis.sect_count = sectors & 0xFF;
1655 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1658 fis.device = ATA_DEVICE_OBS;
1660 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1662 return mtip_exec_internal_command(port,
1666 sectors * ATA_SECT_SIZE,
1669 MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
1673 * Issue a SMART READ DATA command to the device.
1675 * @port pointer to the port structure.
1676 * @buffer pointer to buffer
1677 * @buffer_dma dma address corresponding to @buffer
1680 * @rv return value from mtip_exec_internal_command()
1682 static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1683 dma_addr_t buffer_dma)
1685 struct host_to_dev_fis fis;
1687 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1690 fis.command = ATA_CMD_SMART;
1691 fis.features = 0xD0;
1695 fis.device = ATA_DEVICE_OBS;
1697 return mtip_exec_internal_command(port,
1708 * Get the value of a smart attribute
1710 * @port pointer to the port structure
1711 * @id attribute number
1712 * @attrib pointer to return attrib information corresponding to @id
1715 * -EINVAL NULL buffer passed or unsupported attribute @id.
1716 * -EPERM Identify data not valid, SMART not supported or not enabled
1718 static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1719 struct smart_attr *attrib)
1722 struct smart_attr *pattr;
1727 if (!port->identify_valid) {
1728 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1731 if (!(port->identify[82] & 0x1)) {
1732 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1735 if (!(port->identify[85] & 0x1)) {
1736 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1740 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1741 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1743 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1747 pattr = (struct smart_attr *)(port->smart_buf + 2);
1748 for (i = 0; i < 29; i++, pattr++)
1749 if (pattr->attr_id == id) {
1750 memcpy(attrib, pattr, sizeof(struct smart_attr));
1755 dev_warn(&port->dd->pdev->dev,
1756 "Query for invalid SMART attribute ID\n");
1764 * Trim unused sectors
1766 * @dd pointer to driver_data structure
1768 * @len # of 512b sectors to trim
1771 * -ENOMEM Out of dma memory
1772 * -EINVAL Invalid parameters passed in, trim not supported
1773 * -EIO Error submitting trim request to hw
1775 static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
1779 u64 tlba, tlen, sect_left;
1780 struct mtip_trim_entry *buf;
1781 dma_addr_t dma_addr;
1782 struct host_to_dev_fis fis;
1784 if (!len || dd->trim_supp == false)
1787 /* Trim request too big */
1788 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1790 /* Trim request not aligned on 4k boundary */
1791 WARN_ON(len % 8 != 0);
1793 /* Warn if vu_trim structure is too big */
1794 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1796 /* Allocate a DMA buffer for the trim structure */
1797 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1801 memset(buf, 0, ATA_SECT_SIZE);
1803 for (i = 0, sect_left = len, tlba = lba;
1804 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1806 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1807 MTIP_MAX_TRIM_ENTRY_LEN :
1809 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1810 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1814 WARN_ON(sect_left != 0);
1817 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1821 fis.features = 0x60;
1823 fis.device = ATA_DEVICE_OBS;
1825 if (mtip_exec_internal_command(dd->port,
1832 MTIP_TRIM_TIMEOUT_MS) < 0)
1835 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1840 * Get the drive capacity.
1842 * @dd Pointer to the device data structure.
1843 * @sectors Pointer to the variable that will receive the sector count.
1846 * 1 Capacity was returned successfully.
1847 * 0 The identify information is invalid.
1849 static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
1851 struct mtip_port *port = dd->port;
1852 u64 total, raw0, raw1, raw2, raw3;
1853 raw0 = port->identify[100];
1854 raw1 = port->identify[101];
1855 raw2 = port->identify[102];
1856 raw3 = port->identify[103];
1857 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1859 return (bool) !!port->identify_valid;
1863 * Display the identify command data.
1865 * @port Pointer to the port data structure.
1870 static void mtip_dump_identify(struct mtip_port *port)
1873 unsigned short revid;
1876 if (!port->identify_valid)
1879 strlcpy(cbuf, (char *)(port->identify+10), 21);
1880 dev_info(&port->dd->pdev->dev,
1881 "Serial No.: %s\n", cbuf);
1883 strlcpy(cbuf, (char *)(port->identify+23), 9);
1884 dev_info(&port->dd->pdev->dev,
1885 "Firmware Ver.: %s\n", cbuf);
1887 strlcpy(cbuf, (char *)(port->identify+27), 41);
1888 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1890 if (mtip_hw_get_capacity(port->dd, §ors))
1891 dev_info(&port->dd->pdev->dev,
1892 "Capacity: %llu sectors (%llu MB)\n",
1894 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1896 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
1897 switch (revid & 0xFF) {
1899 strlcpy(cbuf, "A0", 3);
1902 strlcpy(cbuf, "A2", 3);
1905 strlcpy(cbuf, "?", 2);
1908 dev_info(&port->dd->pdev->dev,
1909 "Card Type: %s\n", cbuf);
1913 * Map the commands scatter list into the command table.
1915 * @command Pointer to the command.
1916 * @nents Number of scatter list entries.
1921 static inline void fill_command_sg(struct driver_data *dd,
1922 struct mtip_cmd *command,
1926 unsigned int dma_len;
1927 struct mtip_cmd_sg *command_sg;
1928 struct scatterlist *sg = command->sg;
1930 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1932 for (n = 0; n < nents; n++) {
1933 dma_len = sg_dma_len(sg);
1934 if (dma_len > 0x400000)
1935 dev_err(&dd->pdev->dev,
1936 "DMA segment length truncated\n");
1937 command_sg->info = __force_bit2int
1938 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1939 command_sg->dba = __force_bit2int
1940 cpu_to_le32(sg_dma_address(sg));
1941 command_sg->dba_upper = __force_bit2int
1942 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
1949 * @brief Execute a drive command.
1951 * return value 0 The command completed successfully.
1952 * return value -1 An error occurred while executing the command.
1954 static int exec_drive_task(struct mtip_port *port, u8 *command)
1956 struct host_to_dev_fis fis;
1957 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1959 /* Build the FIS. */
1960 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1963 fis.command = command[0];
1964 fis.features = command[1];
1965 fis.sect_count = command[2];
1966 fis.sector = command[3];
1967 fis.cyl_low = command[4];
1968 fis.cyl_hi = command[5];
1969 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1971 dbg_printk(MTIP_DRV_NAME " %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
1981 /* Execute the command. */
1982 if (mtip_exec_internal_command(port,
1989 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
1993 command[0] = reply->command; /* Status*/
1994 command[1] = reply->features; /* Error*/
1995 command[4] = reply->cyl_low;
1996 command[5] = reply->cyl_hi;
1998 dbg_printk(MTIP_DRV_NAME " %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
2009 * @brief Execute a drive command.
2011 * @param port Pointer to the port data structure.
2012 * @param command Pointer to the user specified command parameters.
2013 * @param user_buffer Pointer to the user space buffer where read sector
2014 * data should be copied.
2016 * return value 0 The command completed successfully.
2017 * return value -EFAULT An error occurred while copying the completion
2018 * data to the user space buffer.
2019 * return value -1 An error occurred while executing the command.
2021 static int exec_drive_command(struct mtip_port *port, u8 *command,
2022 void __user *user_buffer)
2024 struct host_to_dev_fis fis;
2025 struct host_to_dev_fis *reply;
2027 dma_addr_t dma_addr = 0;
2028 int rv = 0, xfer_sz = command[3];
2034 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
2035 ATA_SECT_SIZE * xfer_sz,
2039 dev_err(&port->dd->pdev->dev,
2040 "Memory allocation failed (%d bytes)\n",
2041 ATA_SECT_SIZE * xfer_sz);
2044 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
2047 /* Build the FIS. */
2048 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2051 fis.command = command[0];
2052 fis.features = command[2];
2053 fis.sect_count = command[3];
2054 if (fis.command == ATA_CMD_SMART) {
2055 fis.sector = command[1];
2061 reply = (port->rxfis + RX_FIS_PIO_SETUP);
2063 reply = (port->rxfis + RX_FIS_D2H_REG);
2065 dbg_printk(MTIP_DRV_NAME
2066 " %s: User Command: cmd %x, sect %x, "
2067 "feat %x, sectcnt %x\n",
2074 /* Execute the command. */
2075 if (mtip_exec_internal_command(port,
2078 (xfer_sz ? dma_addr : 0),
2079 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
2082 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
2085 goto exit_drive_command;
2088 /* Collect the completion status. */
2089 command[0] = reply->command; /* Status*/
2090 command[1] = reply->features; /* Error*/
2091 command[2] = reply->sect_count;
2093 dbg_printk(MTIP_DRV_NAME
2094 " %s: Completion Status: stat %x, "
2095 "err %x, nsect %x\n",
2102 if (copy_to_user(user_buffer,
2104 ATA_SECT_SIZE * command[3])) {
2106 goto exit_drive_command;
2111 dmam_free_coherent(&port->dd->pdev->dev,
2112 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
2117 * Indicates whether a command has a single sector payload.
2119 * @command passed to the device to perform the certain event.
2120 * @features passed to the device to perform the certain event.
2123 * 1 command is one that always has a single sector payload,
2124 * regardless of the value in the Sector Count field.
2128 static unsigned int implicit_sector(unsigned char command,
2129 unsigned char features)
2131 unsigned int rv = 0;
2133 /* list of commands that have an implicit sector count of 1 */
2135 case ATA_CMD_SEC_SET_PASS:
2136 case ATA_CMD_SEC_UNLOCK:
2137 case ATA_CMD_SEC_ERASE_PREP:
2138 case ATA_CMD_SEC_ERASE_UNIT:
2139 case ATA_CMD_SEC_FREEZE_LOCK:
2140 case ATA_CMD_SEC_DISABLE_PASS:
2141 case ATA_CMD_PMP_READ:
2142 case ATA_CMD_PMP_WRITE:
2145 case ATA_CMD_SET_MAX:
2146 if (features == ATA_SET_MAX_UNLOCK)
2150 if ((features == ATA_SMART_READ_VALUES) ||
2151 (features == ATA_SMART_READ_THRESHOLDS))
2154 case ATA_CMD_CONF_OVERLAY:
2155 if ((features == ATA_DCO_IDENTIFY) ||
2156 (features == ATA_DCO_SET))
2162 static void mtip_set_timeout(struct driver_data *dd,
2163 struct host_to_dev_fis *fis,
2164 unsigned int *timeout, u8 erasemode)
2166 switch (fis->command) {
2167 case ATA_CMD_DOWNLOAD_MICRO:
2168 *timeout = 120000; /* 2 minutes */
2170 case ATA_CMD_SEC_ERASE_UNIT:
2173 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
2175 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
2177 case ATA_CMD_STANDBYNOW1:
2178 *timeout = 120000; /* 2 minutes */
2182 *timeout = 60000; /* 60 seconds */
2185 *timeout = 15000; /* 15 seconds */
2188 *timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
2194 * Executes a taskfile
2195 * See ide_taskfile_ioctl() for derivation
2197 static int exec_drive_taskfile(struct driver_data *dd,
2199 ide_task_request_t *req_task,
2202 struct host_to_dev_fis fis;
2203 struct host_to_dev_fis *reply;
2206 dma_addr_t outbuf_dma = 0;
2207 dma_addr_t inbuf_dma = 0;
2208 dma_addr_t dma_buffer = 0;
2210 unsigned int taskin = 0;
2211 unsigned int taskout = 0;
2213 unsigned int timeout;
2214 unsigned int force_single_sector;
2215 unsigned int transfer_size;
2216 unsigned long task_file_data;
2217 int intotal = outtotal + req_task->out_size;
2220 taskout = req_task->out_size;
2221 taskin = req_task->in_size;
2222 /* 130560 = 512 * 0xFF*/
2223 if (taskin > 130560 || taskout > 130560) {
2229 outbuf = kzalloc(taskout, GFP_KERNEL);
2230 if (outbuf == NULL) {
2234 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
2238 outbuf_dma = pci_map_single(dd->pdev,
2242 if (outbuf_dma == 0) {
2246 dma_buffer = outbuf_dma;
2250 inbuf = kzalloc(taskin, GFP_KERNEL);
2251 if (inbuf == NULL) {
2256 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2260 inbuf_dma = pci_map_single(dd->pdev,
2262 taskin, DMA_FROM_DEVICE);
2263 if (inbuf_dma == 0) {
2267 dma_buffer = inbuf_dma;
2270 /* only supports PIO and non-data commands from this ioctl. */
2271 switch (req_task->data_phase) {
2273 nsect = taskout / ATA_SECT_SIZE;
2274 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2277 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2279 case TASKFILE_NO_DATA:
2280 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2287 /* Build the FIS. */
2288 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2292 fis.command = req_task->io_ports[7];
2293 fis.features = req_task->io_ports[1];
2294 fis.sect_count = req_task->io_ports[2];
2295 fis.lba_low = req_task->io_ports[3];
2296 fis.lba_mid = req_task->io_ports[4];
2297 fis.lba_hi = req_task->io_ports[5];
2298 /* Clear the dev bit*/
2299 fis.device = req_task->io_ports[6] & ~0x10;
2301 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2302 req_task->in_flags.all =
2303 IDE_TASKFILE_STD_IN_FLAGS |
2304 (IDE_HOB_STD_IN_FLAGS << 8);
2305 fis.lba_low_ex = req_task->hob_ports[3];
2306 fis.lba_mid_ex = req_task->hob_ports[4];
2307 fis.lba_hi_ex = req_task->hob_ports[5];
2308 fis.features_ex = req_task->hob_ports[1];
2309 fis.sect_cnt_ex = req_task->hob_ports[2];
2312 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2315 force_single_sector = implicit_sector(fis.command, fis.features);
2317 if ((taskin || taskout) && (!fis.sect_count)) {
2319 fis.sect_count = nsect;
2321 if (!force_single_sector) {
2322 dev_warn(&dd->pdev->dev,
2323 "data movement but "
2324 "sect_count is 0\n");
2331 dbg_printk(MTIP_DRV_NAME
2332 " %s: cmd %x, feat %x, nsect %x,"
2333 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2344 /* check for erase mode support during secure erase.*/
2345 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2346 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
2350 mtip_set_timeout(dd, &fis, &timeout, erasemode);
2352 /* Determine the correct transfer size.*/
2353 if (force_single_sector)
2354 transfer_size = ATA_SECT_SIZE;
2356 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2358 /* Execute the command.*/
2359 if (mtip_exec_internal_command(dd->port,
2371 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2373 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2374 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2375 req_task->io_ports[7] = reply->control;
2377 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2378 req_task->io_ports[7] = reply->command;
2381 /* reclaim the DMA buffers.*/
2383 pci_unmap_single(dd->pdev, inbuf_dma,
2384 taskin, DMA_FROM_DEVICE);
2386 pci_unmap_single(dd->pdev, outbuf_dma,
2387 taskout, DMA_TO_DEVICE);
2391 /* return the ATA registers to the caller.*/
2392 req_task->io_ports[1] = reply->features;
2393 req_task->io_ports[2] = reply->sect_count;
2394 req_task->io_ports[3] = reply->lba_low;
2395 req_task->io_ports[4] = reply->lba_mid;
2396 req_task->io_ports[5] = reply->lba_hi;
2397 req_task->io_ports[6] = reply->device;
2399 if (req_task->out_flags.all & 1) {
2401 req_task->hob_ports[3] = reply->lba_low_ex;
2402 req_task->hob_ports[4] = reply->lba_mid_ex;
2403 req_task->hob_ports[5] = reply->lba_hi_ex;
2404 req_task->hob_ports[1] = reply->features_ex;
2405 req_task->hob_ports[2] = reply->sect_cnt_ex;
2407 dbg_printk(MTIP_DRV_NAME
2408 " %s: Completion: stat %x,"
2409 "err %x, sect_cnt %x, lbalo %x,"
2410 "lbamid %x, lbahi %x, dev %x\n",
2412 req_task->io_ports[7],
2413 req_task->io_ports[1],
2414 req_task->io_ports[2],
2415 req_task->io_ports[3],
2416 req_task->io_ports[4],
2417 req_task->io_ports[5],
2418 req_task->io_ports[6]);
2421 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2427 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2434 pci_unmap_single(dd->pdev, inbuf_dma,
2435 taskin, DMA_FROM_DEVICE);
2437 pci_unmap_single(dd->pdev, outbuf_dma,
2438 taskout, DMA_TO_DEVICE);
2446 * Handle IOCTL calls from the Block Layer.
2448 * This function is called by the Block Layer when it receives an IOCTL
2449 * command that it does not understand. If the IOCTL command is not supported
2450 * this function returns -ENOTTY.
2452 * @dd Pointer to the driver data structure.
2453 * @cmd IOCTL command passed from the Block Layer.
2454 * @arg IOCTL argument passed from the Block Layer.
2457 * 0 The IOCTL completed successfully.
2458 * -ENOTTY The specified command is not supported.
2459 * -EFAULT An error occurred copying data to a user space buffer.
2460 * -EIO An error occurred while executing the command.
2462 static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2466 case HDIO_GET_IDENTITY:
2468 if (copy_to_user((void __user *)arg, dd->port->identify,
2469 sizeof(u16) * ATA_ID_WORDS))
2473 case HDIO_DRIVE_CMD:
2475 u8 drive_command[4];
2477 /* Copy the user command info to our buffer. */
2478 if (copy_from_user(drive_command,
2479 (void __user *) arg,
2480 sizeof(drive_command)))
2483 /* Execute the drive command. */
2484 if (exec_drive_command(dd->port,
2486 (void __user *) (arg+4)))
2489 /* Copy the status back to the users buffer. */
2490 if (copy_to_user((void __user *) arg,
2492 sizeof(drive_command)))
2497 case HDIO_DRIVE_TASK:
2499 u8 drive_command[7];
2501 /* Copy the user command info to our buffer. */
2502 if (copy_from_user(drive_command,
2503 (void __user *) arg,
2504 sizeof(drive_command)))
2507 /* Execute the drive command. */
2508 if (exec_drive_task(dd->port, drive_command))
2511 /* Copy the status back to the users buffer. */
2512 if (copy_to_user((void __user *) arg,
2514 sizeof(drive_command)))
2519 case HDIO_DRIVE_TASKFILE: {
2520 ide_task_request_t req_task;
2523 if (copy_from_user(&req_task, (void __user *) arg,
2527 outtotal = sizeof(req_task);
2529 ret = exec_drive_taskfile(dd, (void __user *) arg,
2530 &req_task, outtotal);
2532 if (copy_to_user((void __user *) arg, &req_task,
2546 * Submit an IO to the hw
2548 * This function is called by the block layer to issue an io
2549 * to the device. Upon completion, the callback function will
2550 * be called with the data parameter passed as the callback data.
2552 * @dd Pointer to the driver data structure.
2553 * @start First sector to read.
2554 * @nsect Number of sectors to read.
2555 * @nents Number of entries in scatter list for the read command.
2556 * @tag The tag of this read command.
2557 * @callback Pointer to the function that should be called
2558 * when the read completes.
2559 * @data Callback data passed to the callback function
2560 * when the read completes.
2561 * @dir Direction (read or write)
2566 static void mtip_hw_submit_io(struct driver_data *dd, sector_t sector,
2567 int nsect, int nents, int tag, void *callback,
2568 void *data, int dir, int unaligned)
2570 struct host_to_dev_fis *fis;
2571 struct mtip_port *port = dd->port;
2572 struct mtip_cmd *command = &port->commands[tag];
2573 int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2576 /* Map the scatter list for DMA access */
2577 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
2579 command->scatter_ents = nents;
2581 command->unaligned = unaligned;
2583 * The number of retries for this command before it is
2584 * reported as a failure to the upper layers.
2586 command->retries = MTIP_MAX_RETRIES;
2589 fis = command->command;
2593 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
2594 fis->lba_low = start & 0xFF;
2595 fis->lba_mid = (start >> 8) & 0xFF;
2596 fis->lba_hi = (start >> 16) & 0xFF;
2597 fis->lba_low_ex = (start >> 24) & 0xFF;
2598 fis->lba_mid_ex = (start >> 32) & 0xFF;
2599 fis->lba_hi_ex = (start >> 40) & 0xFF;
2600 fis->device = 1 << 6;
2601 fis->features = nsect & 0xFF;
2602 fis->features_ex = (nsect >> 8) & 0xFF;
2603 fis->sect_count = ((tag << 3) | (tag >> 5));
2604 fis->sect_cnt_ex = 0;
2608 fill_command_sg(dd, command, nents);
2611 fis->device |= 1 << 7;
2613 /* Populate the command header */
2614 command->command_header->opts =
2615 __force_bit2int cpu_to_le32(
2616 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
2617 command->command_header->byte_count = 0;
2620 * Set the completion function and data for the command
2621 * within this layer.
2623 command->comp_data = dd;
2624 command->comp_func = mtip_async_complete;
2625 command->direction = dma_dir;
2628 * Set the completion function and data for the command passed
2629 * from the upper layer.
2631 command->async_data = data;
2632 command->async_callback = callback;
2635 * To prevent this command from being issued
2636 * if an internal command is in progress or error handling is active.
2638 if (port->flags & MTIP_PF_PAUSE_IO) {
2639 set_bit(tag, port->cmds_to_issue);
2640 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
2644 /* Issue the command to the hardware */
2645 mtip_issue_ncq_command(port, tag);
2651 * Release a command slot.
2653 * @dd Pointer to the driver data structure.
2659 static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag,
2662 struct semaphore *sem = unaligned ? &dd->port->cmd_slot_unal :
2663 &dd->port->cmd_slot;
2664 release_slot(dd->port, tag);
2669 * Obtain a command slot and return its associated scatter list.
2671 * @dd Pointer to the driver data structure.
2672 * @tag Pointer to an int that will receive the allocated command
2676 * Pointer to the scatter list for the allocated command slot
2677 * or NULL if no command slots are available.
2679 static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2680 int *tag, int unaligned)
2682 struct semaphore *sem = unaligned ? &dd->port->cmd_slot_unal :
2683 &dd->port->cmd_slot;
2686 * It is possible that, even with this semaphore, a thread
2687 * may think that no command slots are available. Therefore, we
2688 * need to make an attempt to get_slot().
2691 *tag = get_slot(dd->port);
2693 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
2697 if (unlikely(*tag < 0)) {
2702 return dd->port->commands[*tag].sg;
2706 * Sysfs status dump.
2708 * @dev Pointer to the device structure, passed by the kernrel.
2709 * @attr Pointer to the device_attribute structure passed by the kernel.
2710 * @buf Pointer to the char buffer that will receive the stats info.
2713 * The size, in bytes, of the data copied into buf.
2715 static ssize_t mtip_hw_show_status(struct device *dev,
2716 struct device_attribute *attr,
2719 struct driver_data *dd = dev_to_disk(dev)->private_data;
2722 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
2723 size += sprintf(buf, "%s", "thermal_shutdown\n");
2724 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
2725 size += sprintf(buf, "%s", "write_protect\n");
2727 size += sprintf(buf, "%s", "online\n");
2732 static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
2734 /* debugsfs entries */
2736 static ssize_t show_device_status(struct device_driver *drv, char *buf)
2739 struct driver_data *dd, *tmp;
2740 unsigned long flags;
2744 spin_lock_irqsave(&dev_lock, flags);
2745 size += sprintf(&buf[size], "Devices Present:\n");
2746 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
2749 dd->port->identify &&
2750 dd->port->identify_valid) {
2752 (char *) (dd->port->identify + 10), 21);
2753 status = *(dd->port->identify + 141);
2755 memset(id_buf, 0, 42);
2760 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2761 size += sprintf(&buf[size],
2762 " device %s %s (ftl rebuild %d %%)\n",
2763 dev_name(&dd->pdev->dev),
2767 size += sprintf(&buf[size],
2769 dev_name(&dd->pdev->dev),
2775 size += sprintf(&buf[size], "Devices Being Removed:\n");
2776 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
2779 dd->port->identify &&
2780 dd->port->identify_valid) {
2782 (char *) (dd->port->identify+10), 21);
2783 status = *(dd->port->identify + 141);
2785 memset(id_buf, 0, 42);
2790 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2791 size += sprintf(&buf[size],
2792 " device %s %s (ftl rebuild %d %%)\n",
2793 dev_name(&dd->pdev->dev),
2797 size += sprintf(&buf[size],
2799 dev_name(&dd->pdev->dev),
2804 spin_unlock_irqrestore(&dev_lock, flags);
2809 static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2810 size_t len, loff_t *offset)
2812 struct driver_data *dd = (struct driver_data *)f->private_data;
2817 if (!len || *offset)
2820 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2822 dev_err(&dd->pdev->dev,
2823 "Memory allocation: status buffer\n");
2827 size += show_device_status(NULL, buf);
2829 *offset = size <= len ? size : len;
2830 size = copy_to_user(ubuf, buf, *offset);
2835 return rv ? rv : *offset;
2838 static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2839 size_t len, loff_t *offset)
2841 struct driver_data *dd = (struct driver_data *)f->private_data;
2843 u32 group_allocated;
2850 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2852 dev_err(&dd->pdev->dev,
2853 "Memory allocation: register buffer\n");
2857 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2859 for (n = dd->slot_groups-1; n >= 0; n--)
2860 size += sprintf(&buf[size], "%08X ",
2861 readl(dd->port->s_active[n]));
2863 size += sprintf(&buf[size], "]\n");
2864 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2866 for (n = dd->slot_groups-1; n >= 0; n--)
2867 size += sprintf(&buf[size], "%08X ",
2868 readl(dd->port->cmd_issue[n]));
2870 size += sprintf(&buf[size], "]\n");
2871 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2873 for (n = dd->slot_groups-1; n >= 0; n--)
2874 size += sprintf(&buf[size], "%08X ",
2875 readl(dd->port->completed[n]));
2877 size += sprintf(&buf[size], "]\n");
2878 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2879 readl(dd->port->mmio + PORT_IRQ_STAT));
2880 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2881 readl(dd->mmio + HOST_IRQ_STAT));
2882 size += sprintf(&buf[size], "\n");
2884 size += sprintf(&buf[size], "L/ Allocated : [ 0x");
2886 for (n = dd->slot_groups-1; n >= 0; n--) {
2887 if (sizeof(long) > sizeof(u32))
2889 dd->port->allocated[n/2] >> (32*(n&1));
2891 group_allocated = dd->port->allocated[n];
2892 size += sprintf(&buf[size], "%08X ", group_allocated);
2894 size += sprintf(&buf[size], "]\n");
2896 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2898 for (n = dd->slot_groups-1; n >= 0; n--) {
2899 if (sizeof(long) > sizeof(u32))
2901 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2903 group_allocated = dd->port->cmds_to_issue[n];
2904 size += sprintf(&buf[size], "%08X ", group_allocated);
2906 size += sprintf(&buf[size], "]\n");
2908 *offset = size <= len ? size : len;
2909 size = copy_to_user(ubuf, buf, *offset);
2914 return rv ? rv : *offset;
2917 static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2918 size_t len, loff_t *offset)
2920 struct driver_data *dd = (struct driver_data *)f->private_data;
2928 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2930 dev_err(&dd->pdev->dev,
2931 "Memory allocation: flag buffer\n");
2935 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2937 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2940 *offset = size <= len ? size : len;
2941 size = copy_to_user(ubuf, buf, *offset);
2946 return rv ? rv : *offset;
2949 static const struct file_operations mtip_device_status_fops = {
2950 .owner = THIS_MODULE,
2951 .open = simple_open,
2952 .read = mtip_hw_read_device_status,
2953 .llseek = no_llseek,
2956 static const struct file_operations mtip_regs_fops = {
2957 .owner = THIS_MODULE,
2958 .open = simple_open,
2959 .read = mtip_hw_read_registers,
2960 .llseek = no_llseek,
2963 static const struct file_operations mtip_flags_fops = {
2964 .owner = THIS_MODULE,
2965 .open = simple_open,
2966 .read = mtip_hw_read_flags,
2967 .llseek = no_llseek,
2971 * Create the sysfs related attributes.
2973 * @dd Pointer to the driver data structure.
2974 * @kobj Pointer to the kobj for the block device.
2977 * 0 Operation completed successfully.
2978 * -EINVAL Invalid parameter.
2980 static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
2985 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2986 dev_warn(&dd->pdev->dev,
2987 "Error creating 'status' sysfs entry\n");
2992 * Remove the sysfs related attributes.
2994 * @dd Pointer to the driver data structure.
2995 * @kobj Pointer to the kobj for the block device.
2998 * 0 Operation completed successfully.
2999 * -EINVAL Invalid parameter.
3001 static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
3006 sysfs_remove_file(kobj, &dev_attr_status.attr);
3011 static int mtip_hw_debugfs_init(struct driver_data *dd)
3016 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
3017 if (IS_ERR_OR_NULL(dd->dfs_node)) {
3018 dev_warn(&dd->pdev->dev,
3019 "Error creating node %s under debugfs\n",
3020 dd->disk->disk_name);
3021 dd->dfs_node = NULL;
3025 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
3027 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
3033 static void mtip_hw_debugfs_exit(struct driver_data *dd)
3036 debugfs_remove_recursive(dd->dfs_node);
3039 static int mtip_free_orphan(struct driver_data *dd)
3041 struct kobject *kobj;
3044 if (dd->bdev->bd_holders >= 1)
3051 mtip_hw_debugfs_exit(dd);
3053 spin_lock(&rssd_index_lock);
3054 ida_remove(&rssd_index_ida, dd->index);
3055 spin_unlock(&rssd_index_lock);
3057 if (!test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag) &&
3058 test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)) {
3062 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3064 mtip_hw_sysfs_exit(dd, kobj);
3067 del_gendisk(dd->disk);
3071 dd->queue->queuedata = NULL;
3072 blk_cleanup_queue(dd->queue);
3081 * Perform any init/resume time hardware setup
3083 * @dd Pointer to the driver data structure.
3088 static inline void hba_setup(struct driver_data *dd)
3091 hwdata = readl(dd->mmio + HOST_HSORG);
3093 /* interrupt bug workaround: use only 1 IS bit.*/
3095 HSORG_DISABLE_SLOTGRP_INTR |
3096 HSORG_DISABLE_SLOTGRP_PXIS,
3097 dd->mmio + HOST_HSORG);
3100 static int mtip_device_unaligned_constrained(struct driver_data *dd)
3102 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
3106 * Detect the details of the product, and store anything needed
3107 * into the driver data structure. This includes product type and
3108 * version and number of slot groups.
3110 * @dd Pointer to the driver data structure.
3115 static void mtip_detect_product(struct driver_data *dd)
3118 unsigned int rev, slotgroups;
3121 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
3123 * [15:8] hardware/software interface rev#
3124 * [ 3] asic-style interface
3125 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
3127 hwdata = readl(dd->mmio + HOST_HSORG);
3129 dd->product_type = MTIP_PRODUCT_UNKNOWN;
3130 dd->slot_groups = 1;
3133 dd->product_type = MTIP_PRODUCT_ASICFPGA;
3134 rev = (hwdata & HSORG_HWREV) >> 8;
3135 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
3136 dev_info(&dd->pdev->dev,
3137 "ASIC-FPGA design, HS rev 0x%x, "
3138 "%i slot groups [%i slots]\n",
3143 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
3144 dev_warn(&dd->pdev->dev,
3145 "Warning: driver only supports "
3146 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
3147 slotgroups = MTIP_MAX_SLOT_GROUPS;
3149 dd->slot_groups = slotgroups;
3153 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
3157 * Blocking wait for FTL rebuild to complete
3159 * @dd Pointer to the DRIVER_DATA structure.
3162 * 0 FTL rebuild completed successfully
3163 * -EFAULT FTL rebuild error/timeout/interruption
3165 static int mtip_ftl_rebuild_poll(struct driver_data *dd)
3167 unsigned long timeout, cnt = 0, start;
3169 dev_warn(&dd->pdev->dev,
3170 "FTL rebuild in progress. Polling for completion.\n");
3173 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
3176 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3179 if (mtip_check_surprise_removal(dd->pdev))
3182 if (mtip_get_identify(dd->port, NULL) < 0)
3185 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3186 MTIP_FTL_REBUILD_MAGIC) {
3188 /* Print message every 3 minutes */
3190 dev_warn(&dd->pdev->dev,
3191 "FTL rebuild in progress (%d secs).\n",
3192 jiffies_to_msecs(jiffies - start) / 1000);
3196 dev_warn(&dd->pdev->dev,
3197 "FTL rebuild complete (%d secs).\n",
3198 jiffies_to_msecs(jiffies - start) / 1000);
3199 mtip_block_initialize(dd);
3203 } while (time_before(jiffies, timeout));
3205 /* Check for timeout */
3206 dev_err(&dd->pdev->dev,
3207 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
3208 jiffies_to_msecs(jiffies - start) / 1000);
3213 * service thread to issue queued commands
3215 * @data Pointer to the driver data structure.
3221 static int mtip_service_thread(void *data)
3223 struct driver_data *dd = (struct driver_data *)data;
3224 unsigned long slot, slot_start, slot_wrap;
3225 unsigned int num_cmd_slots = dd->slot_groups * 32;
3226 struct mtip_port *port = dd->port;
3231 * the condition is to check neither an internal command is
3232 * is in progress nor error handling is active
3234 wait_event_interruptible(port->svc_wait, (port->flags) &&
3235 !(port->flags & MTIP_PF_PAUSE_IO));
3237 if (kthread_should_stop())
3240 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
3242 /* If I am an orphan, start self cleanup */
3243 if (test_bit(MTIP_PF_SR_CLEANUP_BIT, &port->flags))
3246 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3250 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
3252 /* used to restrict the loop to one iteration */
3253 slot_start = num_cmd_slots;
3256 slot = find_next_bit(port->cmds_to_issue,
3257 num_cmd_slots, slot);
3258 if (slot_wrap == 1) {
3259 if ((slot_start >= slot) ||
3260 (slot >= num_cmd_slots))
3263 if (unlikely(slot_start == num_cmd_slots))
3266 if (unlikely(slot == num_cmd_slots)) {
3272 /* Issue the command to the hardware */
3273 mtip_issue_ncq_command(port, slot);
3275 clear_bit(slot, port->cmds_to_issue);
3278 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
3279 } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
3280 if (mtip_ftl_rebuild_poll(dd) < 0)
3281 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
3283 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
3285 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
3287 if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
3291 /* wait for pci remove to exit */
3293 if (test_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag))
3295 msleep_interruptible(1000);
3296 if (kthread_should_stop())
3301 ret = mtip_free_orphan(dd);
3303 /* NOTE: All data structures are invalid, do not
3304 * access any here */
3307 msleep_interruptible(1000);
3308 if (kthread_should_stop())
3316 * Called once for each card.
3318 * @dd Pointer to the driver data structure.
3321 * 0 on success, else an error code.
3323 static int mtip_hw_init(struct driver_data *dd)
3327 unsigned int num_command_slots;
3328 unsigned long timeout, timetaken;
3330 struct smart_attr attr242;
3332 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3334 mtip_detect_product(dd);
3335 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3339 num_command_slots = dd->slot_groups * 32;
3343 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3346 dev_err(&dd->pdev->dev,
3347 "Memory allocation: port structure\n");
3351 /* Continue workqueue setup */
3352 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3353 dd->work[i].port = dd->port;
3355 /* Enable unaligned IO constraints for some devices */
3356 if (mtip_device_unaligned_constrained(dd))
3357 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3359 dd->unal_qdepth = 0;
3361 /* Counting semaphore to track command slot usage */
3362 sema_init(&dd->port->cmd_slot, num_command_slots - 1 - dd->unal_qdepth);
3363 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
3365 /* Spinlock to prevent concurrent issue */
3366 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3367 spin_lock_init(&dd->port->cmd_issue_lock[i]);
3369 /* Set the port mmio base address. */
3370 dd->port->mmio = dd->mmio + PORT_OFFSET;
3373 /* Allocate memory for the command list. */
3374 dd->port->command_list =
3375 dmam_alloc_coherent(&dd->pdev->dev,
3376 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
3377 &dd->port->command_list_dma,
3379 if (!dd->port->command_list) {
3380 dev_err(&dd->pdev->dev,
3381 "Memory allocation: command list\n");
3386 /* Clear the memory we have allocated. */
3387 memset(dd->port->command_list,
3389 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4));
3391 /* Setup the addresse of the RX FIS. */
3392 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
3393 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
3395 /* Setup the address of the command tables. */
3396 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
3397 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
3399 /* Setup the address of the identify data. */
3400 dd->port->identify = dd->port->command_table +
3402 dd->port->identify_dma = dd->port->command_tbl_dma +
3405 /* Setup the address of the sector buffer - for some non-ncq cmds */
3406 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
3407 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
3409 /* Setup the address of the log buf - for read log command */
3410 dd->port->log_buf = (void *)dd->port->sector_buffer + ATA_SECT_SIZE;
3411 dd->port->log_buf_dma = dd->port->sector_buffer_dma + ATA_SECT_SIZE;
3413 /* Setup the address of the smart buf - for smart read data command */
3414 dd->port->smart_buf = (void *)dd->port->log_buf + ATA_SECT_SIZE;
3415 dd->port->smart_buf_dma = dd->port->log_buf_dma + ATA_SECT_SIZE;
3418 /* Point the command headers at the command tables. */
3419 for (i = 0; i < num_command_slots; i++) {
3420 dd->port->commands[i].command_header =
3421 dd->port->command_list +
3422 (sizeof(struct mtip_cmd_hdr) * i);
3423 dd->port->commands[i].command_header_dma =
3424 dd->port->command_list_dma +
3425 (sizeof(struct mtip_cmd_hdr) * i);
3427 dd->port->commands[i].command =
3428 dd->port->command_table + (HW_CMD_TBL_SZ * i);
3429 dd->port->commands[i].command_dma =
3430 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
3432 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
3433 dd->port->commands[i].command_header->ctbau =
3434 __force_bit2int cpu_to_le32(
3435 (dd->port->commands[i].command_dma >> 16) >> 16);
3436 dd->port->commands[i].command_header->ctba =
3437 __force_bit2int cpu_to_le32(
3438 dd->port->commands[i].command_dma & 0xFFFFFFFF);
3441 * If this is not done, a bug is reported by the stock
3442 * FC11 i386. Due to the fact that it has lots of kernel
3443 * debugging enabled.
3445 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
3447 /* Mark all commands as currently inactive.*/
3448 atomic_set(&dd->port->commands[i].active, 0);
3451 /* Setup the pointers to the extended s_active and CI registers. */
3452 for (i = 0; i < dd->slot_groups; i++) {
3453 dd->port->s_active[i] =
3454 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3455 dd->port->cmd_issue[i] =
3456 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3457 dd->port->completed[i] =
3458 dd->port->mmio + i*0x80 + PORT_SDBV;
3461 timetaken = jiffies;
3462 timeout = jiffies + msecs_to_jiffies(30000);
3463 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3464 time_before(jiffies, timeout)) {
3467 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3468 timetaken = jiffies - timetaken;
3469 dev_warn(&dd->pdev->dev,
3470 "Surprise removal detected at %u ms\n",
3471 jiffies_to_msecs(timetaken));
3475 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
3476 timetaken = jiffies - timetaken;
3477 dev_warn(&dd->pdev->dev,
3478 "Removal detected at %u ms\n",
3479 jiffies_to_msecs(timetaken));
3484 /* Conditionally reset the HBA. */
3485 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3486 if (mtip_hba_reset(dd) < 0) {
3487 dev_err(&dd->pdev->dev,
3488 "Card did not reset within timeout\n");
3493 /* Clear any pending interrupts on the HBA */
3494 writel(readl(dd->mmio + HOST_IRQ_STAT),
3495 dd->mmio + HOST_IRQ_STAT);
3498 mtip_init_port(dd->port);
3499 mtip_start_port(dd->port);
3501 /* Setup the ISR and enable interrupts. */
3502 rv = devm_request_irq(&dd->pdev->dev,
3506 dev_driver_string(&dd->pdev->dev),
3510 dev_err(&dd->pdev->dev,
3511 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3514 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
3516 /* Enable interrupts on the HBA. */
3517 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3518 dd->mmio + HOST_CTL);
3520 init_timer(&dd->port->cmd_timer);
3521 init_waitqueue_head(&dd->port->svc_wait);
3523 dd->port->cmd_timer.data = (unsigned long int) dd->port;
3524 dd->port->cmd_timer.function = mtip_timeout_function;
3525 mod_timer(&dd->port->cmd_timer,
3526 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
3529 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
3534 if (mtip_get_identify(dd->port, NULL) < 0) {
3538 mtip_dump_identify(dd->port);
3540 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3541 MTIP_FTL_REBUILD_MAGIC) {
3542 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3543 return MTIP_FTL_REBUILD_MAGIC;
3546 /* check write protect, over temp and rebuild statuses */
3547 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3549 dd->port->log_buf_dma, 1);
3551 dev_warn(&dd->pdev->dev,
3552 "Error in READ LOG EXT (10h) command\n");
3553 /* non-critical error, don't fail the load */
3555 buf = (unsigned char *)dd->port->log_buf;
3556 if (buf[259] & 0x1) {
3557 dev_info(&dd->pdev->dev,
3558 "Write protect bit is set.\n");
3559 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3561 if (buf[288] == 0xF7) {
3562 dev_info(&dd->pdev->dev,
3563 "Exceeded Tmax, drive in thermal shutdown.\n");
3564 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3566 if (buf[288] == 0xBF) {
3567 dev_info(&dd->pdev->dev,
3568 "Drive is in security locked state.\n");
3569 set_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
3573 /* get write protect progess */
3574 memset(&attr242, 0, sizeof(struct smart_attr));
3575 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3576 dev_warn(&dd->pdev->dev,
3577 "Unable to check write protect progress\n");
3579 dev_info(&dd->pdev->dev,
3580 "Write protect progress: %u%% (%u blocks)\n",
3581 attr242.cur, le32_to_cpu(attr242.data));
3585 del_timer_sync(&dd->port->cmd_timer);
3587 /* Disable interrupts on the HBA. */
3588 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3589 dd->mmio + HOST_CTL);
3591 /* Release the IRQ. */
3592 irq_set_affinity_hint(dd->pdev->irq, NULL);
3593 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3596 mtip_deinit_port(dd->port);
3598 /* Free the command/command header memory. */
3599 dmam_free_coherent(&dd->pdev->dev,
3600 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
3601 dd->port->command_list,
3602 dd->port->command_list_dma);
3604 /* Free the memory allocated for the for structure. */
3611 * Called to deinitialize an interface.
3613 * @dd Pointer to the driver data structure.
3618 static int mtip_hw_exit(struct driver_data *dd)
3621 * Send standby immediate (E0h) to the drive so that it
3625 if (!test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag))
3626 if (mtip_standby_immediate(dd->port))
3627 dev_warn(&dd->pdev->dev,
3628 "STANDBY IMMEDIATE failed\n");
3630 /* de-initialize the port. */
3631 mtip_deinit_port(dd->port);
3633 /* Disable interrupts on the HBA. */
3634 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3635 dd->mmio + HOST_CTL);
3638 del_timer_sync(&dd->port->cmd_timer);
3640 /* Release the IRQ. */
3641 irq_set_affinity_hint(dd->pdev->irq, NULL);
3642 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3644 /* Free the command/command header memory. */
3645 dmam_free_coherent(&dd->pdev->dev,
3646 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
3647 dd->port->command_list,
3648 dd->port->command_list_dma);
3649 /* Free the memory allocated for the for structure. */
3657 * Issue a Standby Immediate command to the device.
3659 * This function is called by the Block Layer just before the
3660 * system powers off during a shutdown.
3662 * @dd Pointer to the driver data structure.
3667 static int mtip_hw_shutdown(struct driver_data *dd)
3670 * Send standby immediate (E0h) to the drive so that it
3673 if (!dd->sr && dd->port)
3674 mtip_standby_immediate(dd->port);
3682 * This function is called by the Block Layer just before the
3683 * system hibernates.
3685 * @dd Pointer to the driver data structure.
3688 * 0 Suspend was successful
3689 * -EFAULT Suspend was not successful
3691 static int mtip_hw_suspend(struct driver_data *dd)
3694 * Send standby immediate (E0h) to the drive
3695 * so that it saves its state.
3697 if (mtip_standby_immediate(dd->port) != 0) {
3698 dev_err(&dd->pdev->dev,
3699 "Failed standby-immediate command\n");
3703 /* Disable interrupts on the HBA.*/
3704 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3705 dd->mmio + HOST_CTL);
3706 mtip_deinit_port(dd->port);
3714 * This function is called by the Block Layer as the
3717 * @dd Pointer to the driver data structure.
3720 * 0 Resume was successful
3721 * -EFAULT Resume was not successful
3723 static int mtip_hw_resume(struct driver_data *dd)
3725 /* Perform any needed hardware setup steps */
3729 if (mtip_hba_reset(dd) != 0) {
3730 dev_err(&dd->pdev->dev,
3731 "Unable to reset the HBA\n");
3736 * Enable the port, DMA engine, and FIS reception specific
3737 * h/w in controller.
3739 mtip_init_port(dd->port);
3740 mtip_start_port(dd->port);
3742 /* Enable interrupts on the HBA.*/
3743 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3744 dd->mmio + HOST_CTL);
3750 * Helper function for reusing disk name
3751 * upon hot insertion.
3753 static int rssd_disk_name_format(char *prefix,
3758 const int base = 'z' - 'a' + 1;
3759 char *begin = buf + strlen(prefix);
3760 char *end = buf + buflen;
3770 *--p = 'a' + (index % unit);
3771 index = (index / unit) - 1;
3772 } while (index >= 0);
3774 memmove(begin, p, end - p);
3775 memcpy(buf, prefix, strlen(prefix));
3781 * Block layer IOCTL handler.
3783 * @dev Pointer to the block_device structure.
3785 * @cmd IOCTL command passed from the user application.
3786 * @arg Argument passed from the user application.
3789 * 0 IOCTL completed successfully.
3790 * -ENOTTY IOCTL not supported or invalid driver data
3791 * structure pointer.
3793 static int mtip_block_ioctl(struct block_device *dev,
3798 struct driver_data *dd = dev->bd_disk->private_data;
3800 if (!capable(CAP_SYS_ADMIN))
3806 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
3813 return mtip_hw_ioctl(dd, cmd, arg);
3817 #ifdef CONFIG_COMPAT
3819 * Block layer compat IOCTL handler.
3821 * @dev Pointer to the block_device structure.
3823 * @cmd IOCTL command passed from the user application.
3824 * @arg Argument passed from the user application.
3827 * 0 IOCTL completed successfully.
3828 * -ENOTTY IOCTL not supported or invalid driver data
3829 * structure pointer.
3831 static int mtip_block_compat_ioctl(struct block_device *dev,
3836 struct driver_data *dd = dev->bd_disk->private_data;
3838 if (!capable(CAP_SYS_ADMIN))
3844 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
3850 case HDIO_DRIVE_TASKFILE: {
3851 struct mtip_compat_ide_task_request_s __user *compat_req_task;
3852 ide_task_request_t req_task;
3853 int compat_tasksize, outtotal, ret;
3856 sizeof(struct mtip_compat_ide_task_request_s);
3859 (struct mtip_compat_ide_task_request_s __user *) arg;
3861 if (copy_from_user(&req_task, (void __user *) arg,
3862 compat_tasksize - (2 * sizeof(compat_long_t))))
3865 if (get_user(req_task.out_size, &compat_req_task->out_size))
3868 if (get_user(req_task.in_size, &compat_req_task->in_size))
3871 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3873 ret = exec_drive_taskfile(dd, (void __user *) arg,
3874 &req_task, outtotal);
3876 if (copy_to_user((void __user *) arg, &req_task,
3878 (2 * sizeof(compat_long_t))))
3881 if (put_user(req_task.out_size, &compat_req_task->out_size))
3884 if (put_user(req_task.in_size, &compat_req_task->in_size))
3890 return mtip_hw_ioctl(dd, cmd, arg);
3896 * Obtain the geometry of the device.
3898 * You may think that this function is obsolete, but some applications,
3899 * fdisk for example still used CHS values. This function describes the
3900 * device as having 224 heads and 56 sectors per cylinder. These values are
3901 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3902 * partition is described in terms of a start and end cylinder this means
3903 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3904 * affects performance.
3906 * @dev Pointer to the block_device strucutre.
3907 * @geo Pointer to a hd_geometry structure.
3910 * 0 Operation completed successfully.
3911 * -ENOTTY An error occurred while reading the drive capacity.
3913 static int mtip_block_getgeo(struct block_device *dev,
3914 struct hd_geometry *geo)
3916 struct driver_data *dd = dev->bd_disk->private_data;
3922 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3923 dev_warn(&dd->pdev->dev,
3924 "Could not get drive capacity.\n");
3930 sector_div(capacity, (geo->heads * geo->sectors));
3931 geo->cylinders = capacity;
3936 * Block device operation function.
3938 * This structure contains pointers to the functions required by the block
3941 static const struct block_device_operations mtip_block_ops = {
3942 .ioctl = mtip_block_ioctl,
3943 #ifdef CONFIG_COMPAT
3944 .compat_ioctl = mtip_block_compat_ioctl,
3946 .getgeo = mtip_block_getgeo,
3947 .owner = THIS_MODULE
3951 * Block layer make request function.
3953 * This function is called by the kernel to process a BIO for
3956 * @queue Pointer to the request queue. Unused other than to obtain
3957 * the driver data structure.
3958 * @bio Pointer to the BIO.
3961 static void mtip_make_request(struct request_queue *queue, struct bio *bio)
3963 struct driver_data *dd = queue->queuedata;
3964 struct scatterlist *sg;
3965 struct bio_vec bvec;
3966 struct bvec_iter iter;
3968 int tag = 0, unaligned = 0;
3970 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3971 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3973 bio_endio(bio, -ENXIO);
3976 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
3977 bio_endio(bio, -ENODATA);
3980 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3982 bio_data_dir(bio))) {
3983 bio_endio(bio, -ENODATA);
3986 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))) {
3987 bio_endio(bio, -ENODATA);
3990 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)) {
3991 bio_endio(bio, -ENXIO);
3996 if (unlikely(bio->bi_rw & REQ_DISCARD)) {
3997 bio_endio(bio, mtip_send_trim(dd, bio->bi_iter.bi_sector,
4002 if (unlikely(!bio_has_data(bio))) {
4003 blk_queue_flush(queue, 0);
4008 if (bio_data_dir(bio) == WRITE && bio_sectors(bio) <= 64 &&
4010 if (bio->bi_iter.bi_sector % 8 != 0)
4011 /* Unaligned on 4k boundaries */
4013 else if (bio_sectors(bio) % 8 != 0) /* Aligned but not 4k/8k */
4017 sg = mtip_hw_get_scatterlist(dd, &tag, unaligned);
4018 if (likely(sg != NULL)) {
4019 blk_queue_bounce(queue, &bio);
4021 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
4022 dev_warn(&dd->pdev->dev,
4023 "Maximum number of SGL entries exceeded\n");
4025 mtip_hw_release_scatterlist(dd, tag, unaligned);
4029 /* Create the scatter list for this bio. */
4030 bio_for_each_segment(bvec, bio, iter) {
4031 sg_set_page(&sg[nents],
4038 /* Issue the read/write. */
4039 mtip_hw_submit_io(dd,
4040 bio->bi_iter.bi_sector,
4053 * Block layer initialization function.
4055 * This function is called once by the PCI layer for each P320
4056 * device that is connected to the system.
4058 * @dd Pointer to the driver data structure.
4061 * 0 on success else an error code.
4063 static int mtip_block_initialize(struct driver_data *dd)
4065 int rv = 0, wait_for_rebuild = 0;
4067 unsigned int index = 0;
4068 struct kobject *kobj;
4069 unsigned char thd_name[16];
4072 goto skip_create_disk; /* hw init done, before rebuild */
4074 /* Initialize the protocol layer. */
4075 wait_for_rebuild = mtip_hw_init(dd);
4076 if (wait_for_rebuild < 0) {
4077 dev_err(&dd->pdev->dev,
4078 "Protocol layer initialization failed\n");
4080 goto protocol_init_error;
4083 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
4084 if (dd->disk == NULL) {
4085 dev_err(&dd->pdev->dev,
4086 "Unable to allocate gendisk structure\n");
4088 goto alloc_disk_error;
4091 /* Generate the disk name, implemented same as in sd.c */
4093 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
4096 spin_lock(&rssd_index_lock);
4097 rv = ida_get_new(&rssd_index_ida, &index);
4098 spin_unlock(&rssd_index_lock);
4099 } while (rv == -EAGAIN);
4104 rv = rssd_disk_name_format("rssd",
4106 dd->disk->disk_name,
4109 goto disk_index_error;
4111 dd->disk->driverfs_dev = &dd->pdev->dev;
4112 dd->disk->major = dd->major;
4113 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
4114 dd->disk->fops = &mtip_block_ops;
4115 dd->disk->private_data = dd;
4118 mtip_hw_debugfs_init(dd);
4121 * if rebuild pending, start the service thread, and delay the block
4122 * queue creation and add_disk()
4124 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
4125 goto start_service_thread;
4128 /* Allocate the request queue. */
4129 dd->queue = blk_alloc_queue_node(GFP_KERNEL, dd->numa_node);
4130 if (dd->queue == NULL) {
4131 dev_err(&dd->pdev->dev,
4132 "Unable to allocate request queue\n");
4134 goto block_queue_alloc_init_error;
4137 /* Attach our request function to the request queue. */
4138 blk_queue_make_request(dd->queue, mtip_make_request);
4140 dd->disk->queue = dd->queue;
4141 dd->queue->queuedata = dd;
4143 /* Set device limits. */
4144 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
4145 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
4146 blk_queue_physical_block_size(dd->queue, 4096);
4147 blk_queue_max_hw_sectors(dd->queue, 0xffff);
4148 blk_queue_max_segment_size(dd->queue, 0x400000);
4149 blk_queue_io_min(dd->queue, 4096);
4152 * write back cache is not supported in the device. FUA depends on
4153 * write back cache support, hence setting flush support to zero.
4155 blk_queue_flush(dd->queue, 0);
4157 /* Signal trim support */
4158 if (dd->trim_supp == true) {
4159 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
4160 dd->queue->limits.discard_granularity = 4096;
4161 blk_queue_max_discard_sectors(dd->queue,
4162 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
4163 dd->queue->limits.discard_zeroes_data = 0;
4166 /* Set the capacity of the device in 512 byte sectors. */
4167 if (!(mtip_hw_get_capacity(dd, &capacity))) {
4168 dev_warn(&dd->pdev->dev,
4169 "Could not read drive capacity\n");
4171 goto read_capacity_error;
4173 set_capacity(dd->disk, capacity);
4175 /* Enable the block device and add it to /dev */
4178 dd->bdev = bdget_disk(dd->disk, 0);
4180 * Now that the disk is active, initialize any sysfs attributes
4181 * managed by the protocol layer.
4183 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4185 mtip_hw_sysfs_init(dd, kobj);
4189 if (dd->mtip_svc_handler) {
4190 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
4191 return rv; /* service thread created for handling rebuild */
4194 start_service_thread:
4195 sprintf(thd_name, "mtip_svc_thd_%02d", index);
4196 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
4197 dd, dd->numa_node, "%s",
4200 if (IS_ERR(dd->mtip_svc_handler)) {
4201 dev_err(&dd->pdev->dev, "service thread failed to start\n");
4202 dd->mtip_svc_handler = NULL;
4204 goto kthread_run_error;
4206 wake_up_process(dd->mtip_svc_handler);
4207 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
4208 rv = wait_for_rebuild;
4216 /* Delete our gendisk. This also removes the device from /dev */
4217 del_gendisk(dd->disk);
4219 read_capacity_error:
4220 blk_cleanup_queue(dd->queue);
4222 block_queue_alloc_init_error:
4223 mtip_hw_debugfs_exit(dd);
4225 spin_lock(&rssd_index_lock);
4226 ida_remove(&rssd_index_ida, index);
4227 spin_unlock(&rssd_index_lock);
4233 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
4235 protocol_init_error:
4240 * Block layer deinitialization function.
4242 * Called by the PCI layer as each P320 device is removed.
4244 * @dd Pointer to the driver data structure.
4249 static int mtip_block_remove(struct driver_data *dd)
4251 struct kobject *kobj;
4254 mtip_hw_debugfs_exit(dd);
4256 if (dd->mtip_svc_handler) {
4257 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4258 wake_up_interruptible(&dd->port->svc_wait);
4259 kthread_stop(dd->mtip_svc_handler);
4262 /* Clean up the sysfs attributes, if created */
4263 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4264 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4266 mtip_hw_sysfs_exit(dd, kobj);
4271 * Delete our gendisk structure. This also removes the device
4279 if (dd->disk->queue) {
4280 del_gendisk(dd->disk);
4281 blk_cleanup_queue(dd->queue);
4288 spin_lock(&rssd_index_lock);
4289 ida_remove(&rssd_index_ida, dd->index);
4290 spin_unlock(&rssd_index_lock);
4292 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
4293 dd->disk->disk_name);
4296 /* De-initialize the protocol layer. */
4303 * Function called by the PCI layer when just before the
4304 * machine shuts down.
4306 * If a protocol layer shutdown function is present it will be called
4309 * @dd Pointer to the driver data structure.
4314 static int mtip_block_shutdown(struct driver_data *dd)
4316 /* Delete our gendisk structure, and cleanup the blk queue. */
4318 dev_info(&dd->pdev->dev,
4319 "Shutting down %s ...\n", dd->disk->disk_name);
4321 if (dd->disk->queue) {
4322 del_gendisk(dd->disk);
4323 blk_cleanup_queue(dd->queue);
4330 spin_lock(&rssd_index_lock);
4331 ida_remove(&rssd_index_ida, dd->index);
4332 spin_unlock(&rssd_index_lock);
4334 mtip_hw_shutdown(dd);
4338 static int mtip_block_suspend(struct driver_data *dd)
4340 dev_info(&dd->pdev->dev,
4341 "Suspending %s ...\n", dd->disk->disk_name);
4342 mtip_hw_suspend(dd);
4346 static int mtip_block_resume(struct driver_data *dd)
4348 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4349 dd->disk->disk_name);
4354 static void drop_cpu(int cpu)
4359 static int get_least_used_cpu_on_node(int node)
4361 int cpu, least_used_cpu, least_cnt;
4362 const struct cpumask *node_mask;
4364 node_mask = cpumask_of_node(node);
4365 least_used_cpu = cpumask_first(node_mask);
4366 least_cnt = cpu_use[least_used_cpu];
4367 cpu = least_used_cpu;
4369 for_each_cpu(cpu, node_mask) {
4370 if (cpu_use[cpu] < least_cnt) {
4371 least_used_cpu = cpu;
4372 least_cnt = cpu_use[cpu];
4375 cpu_use[least_used_cpu]++;
4376 return least_used_cpu;
4379 /* Helper for selecting a node in round robin mode */
4380 static inline int mtip_get_next_rr_node(void)
4382 static int next_node = -1;
4384 if (next_node == -1) {
4385 next_node = first_online_node;
4389 next_node = next_online_node(next_node);
4390 if (next_node == MAX_NUMNODES)
4391 next_node = first_online_node;
4395 static DEFINE_HANDLER(0);
4396 static DEFINE_HANDLER(1);
4397 static DEFINE_HANDLER(2);
4398 static DEFINE_HANDLER(3);
4399 static DEFINE_HANDLER(4);
4400 static DEFINE_HANDLER(5);
4401 static DEFINE_HANDLER(6);
4402 static DEFINE_HANDLER(7);
4405 * Called for each supported PCI device detected.
4407 * This function allocates the private data structure, enables the
4408 * PCI device and then calls the block layer initialization function.
4411 * 0 on success else an error code.
4413 static int mtip_pci_probe(struct pci_dev *pdev,
4414 const struct pci_device_id *ent)
4417 struct driver_data *dd = NULL;
4419 const struct cpumask *node_mask;
4420 int cpu, i = 0, j = 0;
4421 int my_node = NUMA_NO_NODE;
4422 unsigned long flags;
4424 /* Allocate memory for this devices private data. */
4425 my_node = pcibus_to_node(pdev->bus);
4426 if (my_node != NUMA_NO_NODE) {
4427 if (!node_online(my_node))
4428 my_node = mtip_get_next_rr_node();
4430 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4431 my_node = mtip_get_next_rr_node();
4433 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4434 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
4435 cpu_to_node(smp_processor_id()), smp_processor_id());
4437 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
4440 "Unable to allocate memory for driver data\n");
4444 /* Attach the private data to this PCI device. */
4445 pci_set_drvdata(pdev, dd);
4447 rv = pcim_enable_device(pdev);
4449 dev_err(&pdev->dev, "Unable to enable device\n");
4453 /* Map BAR5 to memory. */
4454 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4456 dev_err(&pdev->dev, "Unable to map regions\n");
4460 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4461 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4464 rv = pci_set_consistent_dma_mask(pdev,
4467 dev_warn(&pdev->dev,
4468 "64-bit DMA enable failed\n");
4474 /* Copy the info we may need later into the private data structure. */
4475 dd->major = mtip_major;
4476 dd->instance = instance;
4478 dd->numa_node = my_node;
4480 INIT_LIST_HEAD(&dd->online_list);
4481 INIT_LIST_HEAD(&dd->remove_list);
4483 memset(dd->workq_name, 0, 32);
4484 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4486 dd->isr_workq = create_workqueue(dd->workq_name);
4487 if (!dd->isr_workq) {
4488 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
4490 goto block_initialize_err;
4493 memset(cpu_list, 0, sizeof(cpu_list));
4495 node_mask = cpumask_of_node(dd->numa_node);
4496 if (!cpumask_empty(node_mask)) {
4497 for_each_cpu(cpu, node_mask)
4499 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4500 j = strlen(cpu_list);
4503 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4505 topology_physical_package_id(cpumask_first(node_mask)),
4506 nr_cpus_node(dd->numa_node),
4509 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4511 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4512 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4513 cpu_to_node(dd->isr_binding), dd->isr_binding);
4515 /* first worker context always runs in ISR */
4516 dd->work[0].cpu_binding = dd->isr_binding;
4517 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4518 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4519 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4520 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4521 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4522 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4523 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4525 /* Log the bindings */
4526 for_each_present_cpu(cpu) {
4527 memset(cpu_list, 0, sizeof(cpu_list));
4528 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4529 if (dd->work[i].cpu_binding == cpu) {
4530 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4531 j = strlen(cpu_list);
4535 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4538 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4539 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4540 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4541 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4542 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4543 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4544 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4545 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4547 pci_set_master(pdev);
4548 rv = pci_enable_msi(pdev);
4550 dev_warn(&pdev->dev,
4551 "Unable to enable MSI interrupt.\n");
4552 goto block_initialize_err;
4555 /* Initialize the block layer. */
4556 rv = mtip_block_initialize(dd);
4559 "Unable to initialize block layer\n");
4560 goto block_initialize_err;
4564 * Increment the instance count so that each device has a unique
4568 if (rv != MTIP_FTL_REBUILD_MAGIC)
4569 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
4571 rv = 0; /* device in rebuild state, return 0 from probe */
4573 /* Add to online list even if in ftl rebuild */
4574 spin_lock_irqsave(&dev_lock, flags);
4575 list_add(&dd->online_list, &online_list);
4576 spin_unlock_irqrestore(&dev_lock, flags);
4580 block_initialize_err:
4581 pci_disable_msi(pdev);
4582 if (dd->isr_workq) {
4583 flush_workqueue(dd->isr_workq);
4584 destroy_workqueue(dd->isr_workq);
4585 drop_cpu(dd->work[0].cpu_binding);
4586 drop_cpu(dd->work[1].cpu_binding);
4587 drop_cpu(dd->work[2].cpu_binding);
4590 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4594 pci_set_drvdata(pdev, NULL);
4601 * Called for each probed device when the device is removed or the
4602 * driver is unloaded.
4607 static void mtip_pci_remove(struct pci_dev *pdev)
4609 struct driver_data *dd = pci_get_drvdata(pdev);
4610 unsigned long flags, to;
4612 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
4614 spin_lock_irqsave(&dev_lock, flags);
4615 list_del_init(&dd->online_list);
4616 list_add(&dd->remove_list, &removing_list);
4617 spin_unlock_irqrestore(&dev_lock, flags);
4619 mtip_check_surprise_removal(pdev);
4620 synchronize_irq(dd->pdev->irq);
4622 /* Spin until workers are done */
4623 to = jiffies + msecs_to_jiffies(4000);
4626 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4627 time_before(jiffies, to));
4629 if (atomic_read(&dd->irq_workers_active) != 0) {
4630 dev_warn(&dd->pdev->dev,
4631 "Completion workers still active!\n");
4633 /* Cleanup the outstanding commands */
4634 mtip_command_cleanup(dd);
4636 /* Clean up the block layer. */
4637 mtip_block_remove(dd);
4639 if (dd->isr_workq) {
4640 flush_workqueue(dd->isr_workq);
4641 destroy_workqueue(dd->isr_workq);
4642 drop_cpu(dd->work[0].cpu_binding);
4643 drop_cpu(dd->work[1].cpu_binding);
4644 drop_cpu(dd->work[2].cpu_binding);
4647 pci_disable_msi(pdev);
4649 spin_lock_irqsave(&dev_lock, flags);
4650 list_del_init(&dd->remove_list);
4651 spin_unlock_irqrestore(&dev_lock, flags);
4656 set_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag);
4658 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4659 pci_set_drvdata(pdev, NULL);
4665 * Called for each probed device when the device is suspended.
4671 static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4674 struct driver_data *dd = pci_get_drvdata(pdev);
4678 "Driver private datastructure is NULL\n");
4682 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
4684 /* Disable ports & interrupts then send standby immediate */
4685 rv = mtip_block_suspend(dd);
4688 "Failed to suspend controller\n");
4693 * Save the pci config space to pdev structure &
4694 * disable the device
4696 pci_save_state(pdev);
4697 pci_disable_device(pdev);
4699 /* Move to Low power state*/
4700 pci_set_power_state(pdev, PCI_D3hot);
4706 * Called for each probed device when the device is resumed.
4712 static int mtip_pci_resume(struct pci_dev *pdev)
4715 struct driver_data *dd;
4717 dd = pci_get_drvdata(pdev);
4720 "Driver private datastructure is NULL\n");
4724 /* Move the device to active State */
4725 pci_set_power_state(pdev, PCI_D0);
4727 /* Restore PCI configuration space */
4728 pci_restore_state(pdev);
4730 /* Enable the PCI device*/
4731 rv = pcim_enable_device(pdev);
4734 "Failed to enable card during resume\n");
4737 pci_set_master(pdev);
4740 * Calls hbaReset, initPort, & startPort function
4741 * then enables interrupts
4743 rv = mtip_block_resume(dd);
4745 dev_err(&pdev->dev, "Unable to resume\n");
4748 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
4759 static void mtip_pci_shutdown(struct pci_dev *pdev)
4761 struct driver_data *dd = pci_get_drvdata(pdev);
4763 mtip_block_shutdown(dd);
4766 /* Table of device ids supported by this driver. */
4767 static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
4768 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4769 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4770 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4771 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4772 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4773 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4774 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
4778 /* Structure that describes the PCI driver functions. */
4779 static struct pci_driver mtip_pci_driver = {
4780 .name = MTIP_DRV_NAME,
4781 .id_table = mtip_pci_tbl,
4782 .probe = mtip_pci_probe,
4783 .remove = mtip_pci_remove,
4784 .suspend = mtip_pci_suspend,
4785 .resume = mtip_pci_resume,
4786 .shutdown = mtip_pci_shutdown,
4789 MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4792 * Module initialization function.
4794 * Called once when the module is loaded. This function allocates a major
4795 * block device number to the Cyclone devices and registers the PCI layer
4799 * 0 on success else error code.
4801 static int __init mtip_init(void)
4805 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
4807 spin_lock_init(&dev_lock);
4809 INIT_LIST_HEAD(&online_list);
4810 INIT_LIST_HEAD(&removing_list);
4812 /* Allocate a major block device number to use with this driver. */
4813 error = register_blkdev(0, MTIP_DRV_NAME);
4815 pr_err("Unable to register block device (%d)\n",
4821 dfs_parent = debugfs_create_dir("rssd", NULL);
4822 if (IS_ERR_OR_NULL(dfs_parent)) {
4823 pr_warn("Error creating debugfs parent\n");
4827 dfs_device_status = debugfs_create_file("device_status",
4828 S_IRUGO, dfs_parent, NULL,
4829 &mtip_device_status_fops);
4830 if (IS_ERR_OR_NULL(dfs_device_status)) {
4831 pr_err("Error creating device_status node\n");
4832 dfs_device_status = NULL;
4836 /* Register our PCI operations. */
4837 error = pci_register_driver(&mtip_pci_driver);
4839 debugfs_remove(dfs_parent);
4840 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4847 * Module de-initialization function.
4849 * Called once when the module is unloaded. This function deallocates
4850 * the major block device number allocated by mtip_init() and
4851 * unregisters the PCI layer of the driver.
4856 static void __exit mtip_exit(void)
4858 debugfs_remove_recursive(dfs_parent);
4860 /* Release the allocated major block device number. */
4861 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4863 /* Unregister the PCI driver. */
4864 pci_unregister_driver(&mtip_pci_driver);
4867 MODULE_AUTHOR("Micron Technology, Inc");
4868 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4869 MODULE_LICENSE("GPL");
4870 MODULE_VERSION(MTIP_DRV_VERSION);
4872 module_init(mtip_init);
4873 module_exit(mtip_exit);