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)
45 /* DMA region containing RX Fis, Identify, RLE10, and SMART buffers */
46 #define AHCI_RX_FIS_SZ 0x100
47 #define AHCI_RX_FIS_OFFSET 0x0
48 #define AHCI_IDFY_SZ ATA_SECT_SIZE
49 #define AHCI_IDFY_OFFSET 0x400
50 #define AHCI_SECTBUF_SZ ATA_SECT_SIZE
51 #define AHCI_SECTBUF_OFFSET 0x800
52 #define AHCI_SMARTBUF_SZ ATA_SECT_SIZE
53 #define AHCI_SMARTBUF_OFFSET 0xC00
54 /* 0x100 + 0x200 + 0x200 + 0x200 is smaller than 4k but we pad it out */
55 #define BLOCK_DMA_ALLOC_SZ 4096
57 /* DMA region containing command table (should be 8192 bytes) */
58 #define AHCI_CMD_SLOT_SZ sizeof(struct mtip_cmd_hdr)
59 #define AHCI_CMD_TBL_SZ (MTIP_MAX_COMMAND_SLOTS * AHCI_CMD_SLOT_SZ)
60 #define AHCI_CMD_TBL_OFFSET 0x0
62 /* DMA region per command (contains header and SGL) */
63 #define AHCI_CMD_TBL_HDR_SZ 0x80
64 #define AHCI_CMD_TBL_HDR_OFFSET 0x0
65 #define AHCI_CMD_TBL_SGL_SZ (MTIP_MAX_SG * sizeof(struct mtip_cmd_sg))
66 #define AHCI_CMD_TBL_SGL_OFFSET AHCI_CMD_TBL_HDR_SZ
67 #define CMD_DMA_ALLOC_SZ (AHCI_CMD_TBL_SGL_SZ + AHCI_CMD_TBL_HDR_SZ)
70 #define HOST_CAP_NZDMA (1 << 19)
71 #define HOST_HSORG 0xFC
72 #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
73 #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
74 #define HSORG_HWREV 0xFF00
75 #define HSORG_STYLE 0x8
76 #define HSORG_SLOTGROUPS 0x7
78 #define PORT_COMMAND_ISSUE 0x38
79 #define PORT_SDBV 0x7C
81 #define PORT_OFFSET 0x100
82 #define PORT_MEM_SIZE 0x80
84 #define PORT_IRQ_ERR \
85 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
86 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
87 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
89 #define PORT_IRQ_LEGACY \
90 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
91 #define PORT_IRQ_HANDLED \
92 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
93 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
94 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
95 #define DEF_PORT_IRQ \
96 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
99 #define MTIP_PRODUCT_UNKNOWN 0x00
100 #define MTIP_PRODUCT_ASICFPGA 0x11
102 /* Device instance number, incremented each time a device is probed. */
105 struct list_head online_list;
106 struct list_head removing_list;
110 * Global variable used to hold the major block device number
111 * allocated in mtip_init().
113 static int mtip_major;
114 static struct dentry *dfs_parent;
115 static struct dentry *dfs_device_status;
117 static u32 cpu_use[NR_CPUS];
119 static DEFINE_SPINLOCK(rssd_index_lock);
120 static DEFINE_IDA(rssd_index_ida);
122 static int mtip_block_initialize(struct driver_data *dd);
125 struct mtip_compat_ide_task_request_s {
128 ide_reg_valid_t out_flags;
129 ide_reg_valid_t in_flags;
132 compat_ulong_t out_size;
133 compat_ulong_t in_size;
138 * This function check_for_surprise_removal is called
139 * while card is removed from the system and it will
140 * read the vendor id from the configration space
142 * @pdev Pointer to the pci_dev structure.
145 * true if device removed, else false
147 static bool mtip_check_surprise_removal(struct pci_dev *pdev)
150 struct driver_data *dd = pci_get_drvdata(pdev);
155 /* Read the vendorID from the configuration space */
156 pci_read_config_word(pdev, 0x00, &vendor_id);
157 if (vendor_id == 0xFFFF) {
160 set_bit(QUEUE_FLAG_DEAD, &dd->queue->queue_flags);
162 dev_warn(&dd->pdev->dev,
163 "%s: dd->queue is NULL\n", __func__);
165 set_bit(MTIP_PF_SR_CLEANUP_BIT, &dd->port->flags);
166 wake_up_interruptible(&dd->port->svc_wait);
168 dev_warn(&dd->pdev->dev,
169 "%s: dd->port is NULL\n", __func__);
170 return true; /* device removed */
173 return false; /* device present */
177 * Obtain an empty command slot.
179 * This function needs to be reentrant since it could be called
180 * at the same time on multiple CPUs. The allocation of the
181 * command slot must be atomic.
183 * @port Pointer to the port data structure.
186 * >= 0 Index of command slot obtained.
187 * -1 No command slots available.
189 static int get_slot(struct mtip_port *port)
192 unsigned int num_command_slots = port->dd->slot_groups * 32;
195 * Try 10 times, because there is a small race here.
196 * that's ok, because it's still cheaper than a lock.
198 * Race: Since this section is not protected by lock, same bit
199 * could be chosen by different process contexts running in
200 * different processor. So instead of costly lock, we are going
203 for (i = 0; i < 10; i++) {
204 slot = find_next_zero_bit(port->allocated,
205 num_command_slots, 1);
206 if ((slot < num_command_slots) &&
207 (!test_and_set_bit(slot, port->allocated)))
210 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
212 mtip_check_surprise_removal(port->dd->pdev);
217 * Release a command slot.
219 * @port Pointer to the port data structure.
220 * @tag Tag of command to release
225 static inline void release_slot(struct mtip_port *port, int tag)
227 smp_mb__before_clear_bit();
228 clear_bit(tag, port->allocated);
229 smp_mb__after_clear_bit();
233 * IO completion function.
235 * This completion function is called by the driver ISR when a
236 * command that was issued by the kernel completes. It first calls the
237 * asynchronous completion function which normally calls back into the block
238 * layer passing the asynchronous callback data, then unmaps the
239 * scatter list associated with the completed command, and finally
240 * clears the allocated bit associated with the completed command.
242 * @port Pointer to the port data structure.
243 * @tag Tag of the command.
244 * @data Pointer to driver_data.
245 * @status Completion status.
250 static void mtip_async_complete(struct mtip_port *port,
255 struct mtip_cmd *command;
256 struct driver_data *dd = data;
257 int cb_status = status ? -EIO : 0;
259 if (unlikely(!dd) || unlikely(!port))
262 command = &port->commands[tag];
264 if (unlikely(status == PORT_IRQ_TF_ERR)) {
265 dev_warn(&port->dd->pdev->dev,
266 "Command tag %d failed due to TFE\n", tag);
269 /* Upper layer callback */
270 if (likely(command->async_callback))
271 command->async_callback(command->async_data, cb_status);
273 command->async_callback = NULL;
274 command->comp_func = NULL;
276 /* Unmap the DMA scatter list entries */
277 dma_unmap_sg(&dd->pdev->dev,
279 command->scatter_ents,
282 /* Clear the allocated and active bits for the command */
283 atomic_set(&port->commands[tag].active, 0);
284 release_slot(port, tag);
290 * This function is called for clean the pending command in the
291 * command slot during the surprise removal of device and return
292 * error to the upper layer.
294 * @dd Pointer to the DRIVER_DATA structure.
299 static void mtip_command_cleanup(struct driver_data *dd)
302 struct mtip_cmd *cmd;
303 struct mtip_port *port = dd->port;
304 unsigned int num_cmd_slots = dd->slot_groups * 32;
306 if (!test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
312 cmd = &port->commands[MTIP_TAG_INTERNAL];
313 if (atomic_read(&cmd->active))
314 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL]) &
315 (1 << MTIP_TAG_INTERNAL))
317 cmd->comp_func(port, MTIP_TAG_INTERNAL,
318 cmd->comp_data, -ENODEV);
321 tag = find_next_bit(port->allocated, num_cmd_slots, tag);
322 if (tag >= num_cmd_slots)
325 cmd = &port->commands[tag];
326 if (atomic_read(&cmd->active))
327 mtip_async_complete(port, tag, dd, -ENODEV);
330 set_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag);
334 * Reset the HBA (without sleeping)
336 * @dd Pointer to the driver data structure.
339 * 0 The reset was successful.
340 * -1 The HBA Reset bit did not clear.
342 static int mtip_hba_reset(struct driver_data *dd)
344 unsigned long timeout;
346 /* Set the reset bit */
347 writel(HOST_RESET, dd->mmio + HOST_CTL);
350 readl(dd->mmio + HOST_CTL);
352 /* Spin for up to 2 seconds, waiting for reset acknowledgement */
353 timeout = jiffies + msecs_to_jiffies(2000);
356 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
359 } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
360 && time_before(jiffies, timeout));
362 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
369 * Issue a command to the hardware.
371 * Set the appropriate bit in the s_active and Command Issue hardware
372 * registers, causing hardware command processing to begin.
374 * @port Pointer to the port structure.
375 * @tag The tag of the command to be issued.
380 static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
382 int group = tag >> 5;
384 atomic_set(&port->commands[tag].active, 1);
386 /* guard SACT and CI registers */
387 spin_lock(&port->cmd_issue_lock[group]);
388 writel((1 << MTIP_TAG_BIT(tag)),
389 port->s_active[MTIP_TAG_INDEX(tag)]);
390 writel((1 << MTIP_TAG_BIT(tag)),
391 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
392 spin_unlock(&port->cmd_issue_lock[group]);
394 /* Set the command's timeout value.*/
395 port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
396 MTIP_NCQ_COMMAND_TIMEOUT_MS);
400 * Enable/disable the reception of FIS
402 * @port Pointer to the port data structure
403 * @enable 1 to enable, 0 to disable
406 * Previous state: 1 enabled, 0 disabled
408 static int mtip_enable_fis(struct mtip_port *port, int enable)
412 /* enable FIS reception */
413 tmp = readl(port->mmio + PORT_CMD);
415 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
417 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
420 readl(port->mmio + PORT_CMD);
422 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
426 * Enable/disable the DMA engine
428 * @port Pointer to the port data structure
429 * @enable 1 to enable, 0 to disable
432 * Previous state: 1 enabled, 0 disabled.
434 static int mtip_enable_engine(struct mtip_port *port, int enable)
438 /* enable FIS reception */
439 tmp = readl(port->mmio + PORT_CMD);
441 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
443 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
445 readl(port->mmio + PORT_CMD);
446 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
450 * Enables the port DMA engine and FIS reception.
455 static inline void mtip_start_port(struct mtip_port *port)
457 /* Enable FIS reception */
458 mtip_enable_fis(port, 1);
460 /* Enable the DMA engine */
461 mtip_enable_engine(port, 1);
465 * Deinitialize a port by disabling port interrupts, the DMA engine,
468 * @port Pointer to the port structure
473 static inline void mtip_deinit_port(struct mtip_port *port)
475 /* Disable interrupts on this port */
476 writel(0, port->mmio + PORT_IRQ_MASK);
478 /* Disable the DMA engine */
479 mtip_enable_engine(port, 0);
481 /* Disable FIS reception */
482 mtip_enable_fis(port, 0);
488 * This function deinitializes the port by calling mtip_deinit_port() and
489 * then initializes it by setting the command header and RX FIS addresses,
490 * clearing the SError register and any pending port interrupts before
491 * re-enabling the default set of port interrupts.
493 * @port Pointer to the port structure.
498 static void mtip_init_port(struct mtip_port *port)
501 mtip_deinit_port(port);
503 /* Program the command list base and FIS base addresses */
504 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
505 writel((port->command_list_dma >> 16) >> 16,
506 port->mmio + PORT_LST_ADDR_HI);
507 writel((port->rxfis_dma >> 16) >> 16,
508 port->mmio + PORT_FIS_ADDR_HI);
511 writel(port->command_list_dma & 0xFFFFFFFF,
512 port->mmio + PORT_LST_ADDR);
513 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
516 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
518 /* reset the completed registers.*/
519 for (i = 0; i < port->dd->slot_groups; i++)
520 writel(0xFFFFFFFF, port->completed[i]);
522 /* Clear any pending interrupts for this port */
523 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
525 /* Clear any pending interrupts on the HBA. */
526 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
527 port->dd->mmio + HOST_IRQ_STAT);
529 /* Enable port interrupts */
530 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
536 * @port Pointer to the port data structure.
541 static void mtip_restart_port(struct mtip_port *port)
543 unsigned long timeout;
545 /* Disable the DMA engine */
546 mtip_enable_engine(port, 0);
548 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
549 timeout = jiffies + msecs_to_jiffies(500);
550 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
551 && time_before(jiffies, timeout))
554 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
558 * Chip quirk: escalate to hba reset if
559 * PxCMD.CR not clear after 500 ms
561 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
562 dev_warn(&port->dd->pdev->dev,
563 "PxCMD.CR not clear, escalating reset\n");
565 if (mtip_hba_reset(port->dd))
566 dev_err(&port->dd->pdev->dev,
567 "HBA reset escalation failed.\n");
569 /* 30 ms delay before com reset to quiesce chip */
573 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
576 writel(readl(port->mmio + PORT_SCR_CTL) |
577 1, port->mmio + PORT_SCR_CTL);
578 readl(port->mmio + PORT_SCR_CTL);
580 /* Wait 1 ms to quiesce chip function */
581 timeout = jiffies + msecs_to_jiffies(1);
582 while (time_before(jiffies, timeout))
585 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
588 /* Clear PxSCTL.DET */
589 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
590 port->mmio + PORT_SCR_CTL);
591 readl(port->mmio + PORT_SCR_CTL);
593 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
594 timeout = jiffies + msecs_to_jiffies(500);
595 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
596 && time_before(jiffies, timeout))
599 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
602 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
603 dev_warn(&port->dd->pdev->dev,
604 "COM reset failed\n");
606 mtip_init_port(port);
607 mtip_start_port(port);
611 static int mtip_device_reset(struct driver_data *dd)
615 if (mtip_check_surprise_removal(dd->pdev))
618 if (mtip_hba_reset(dd) < 0)
622 mtip_init_port(dd->port);
623 mtip_start_port(dd->port);
625 /* Enable interrupts on the HBA. */
626 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
627 dd->mmio + HOST_CTL);
632 * Helper function for tag logging
634 static void print_tags(struct driver_data *dd,
636 unsigned long *tagbits,
639 unsigned char tagmap[128];
640 int group, tagmap_len = 0;
642 memset(tagmap, 0, sizeof(tagmap));
643 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
644 tagmap_len = sprintf(tagmap + tagmap_len, "%016lX ",
646 dev_warn(&dd->pdev->dev,
647 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
651 * Called periodically to see if any read/write commands are
652 * taking too long to complete.
654 * @data Pointer to the PORT data structure.
659 static void mtip_timeout_function(unsigned long int data)
661 struct mtip_port *port = (struct mtip_port *) data;
662 struct host_to_dev_fis *fis;
663 struct mtip_cmd *command;
664 int tag, cmdto_cnt = 0;
665 unsigned int bit, group;
666 unsigned int num_command_slots;
667 unsigned long to, tagaccum[SLOTBITS_IN_LONGS];
672 if (unlikely(port->dd->sr))
675 if (test_bit(MTIP_DDF_RESUME_BIT, &port->dd->dd_flag)) {
676 mod_timer(&port->cmd_timer,
677 jiffies + msecs_to_jiffies(30000));
680 /* clear the tag accumulator */
681 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
682 num_command_slots = port->dd->slot_groups * 32;
684 for (tag = 0; tag < num_command_slots; tag++) {
686 * Skip internal command slot as it has
687 * its own timeout mechanism
689 if (tag == MTIP_TAG_INTERNAL)
692 if (atomic_read(&port->commands[tag].active) &&
693 (time_after(jiffies, port->commands[tag].comp_time))) {
697 command = &port->commands[tag];
698 fis = (struct host_to_dev_fis *) command->command;
700 set_bit(tag, tagaccum);
703 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
706 * Clear the completed bit. This should prevent
707 * any interrupt handlers from trying to retire
710 writel(1 << bit, port->completed[group]);
712 /* Call the async completion callback. */
713 if (likely(command->async_callback))
714 command->async_callback(command->async_data,
716 command->async_callback = NULL;
717 command->comp_func = NULL;
719 /* Unmap the DMA scatter list entries */
720 dma_unmap_sg(&port->dd->pdev->dev,
722 command->scatter_ents,
726 * Clear the allocated bit and active tag for the
729 atomic_set(&port->commands[tag].active, 0);
730 release_slot(port, tag);
737 print_tags(port->dd, "timed out", tagaccum, cmdto_cnt);
738 if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
739 mtip_device_reset(port->dd);
740 wake_up_interruptible(&port->svc_wait);
742 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
745 if (port->ic_pause_timer) {
746 to = port->ic_pause_timer + msecs_to_jiffies(1000);
747 if (time_after(jiffies, to)) {
748 if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
749 port->ic_pause_timer = 0;
750 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
751 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
752 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
753 wake_up_interruptible(&port->svc_wait);
760 /* Restart the timer */
761 mod_timer(&port->cmd_timer,
762 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
766 * Internal command completion callback function.
768 * This function is normally called by the driver ISR when an internal
769 * command completed. This function signals the command completion by
770 * calling complete().
772 * @port Pointer to the port data structure.
773 * @tag Tag of the command that has completed.
774 * @data Pointer to a completion structure.
775 * @status Completion status.
780 static void mtip_completion(struct mtip_port *port,
785 struct mtip_cmd *command = &port->commands[tag];
786 struct completion *waiting = data;
787 if (unlikely(status == PORT_IRQ_TF_ERR))
788 dev_warn(&port->dd->pdev->dev,
789 "Internal command %d completed with TFE\n", tag);
791 command->async_callback = NULL;
792 command->comp_func = NULL;
797 static void mtip_null_completion(struct mtip_port *port,
805 static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
806 dma_addr_t buffer_dma, unsigned int sectors);
807 static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
808 struct smart_attr *attrib);
812 * @dd Pointer to the DRIVER_DATA structure.
817 static void mtip_handle_tfe(struct driver_data *dd)
819 int group, tag, bit, reissue, rv;
820 struct mtip_port *port;
821 struct mtip_cmd *cmd;
823 struct host_to_dev_fis *fis;
824 unsigned long tagaccum[SLOTBITS_IN_LONGS];
825 unsigned int cmd_cnt = 0;
827 char *fail_reason = NULL;
828 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
830 dev_warn(&dd->pdev->dev, "Taskfile error\n");
834 /* Stop the timer to prevent command timeouts. */
835 del_timer(&port->cmd_timer);
836 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
838 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
839 test_bit(MTIP_TAG_INTERNAL, port->allocated)) {
840 cmd = &port->commands[MTIP_TAG_INTERNAL];
841 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
843 atomic_inc(&cmd->active); /* active > 1 indicates error */
844 if (cmd->comp_data && cmd->comp_func) {
845 cmd->comp_func(port, MTIP_TAG_INTERNAL,
846 cmd->comp_data, PORT_IRQ_TF_ERR);
848 goto handle_tfe_exit;
851 /* clear the tag accumulator */
852 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
854 /* Loop through all the groups */
855 for (group = 0; group < dd->slot_groups; group++) {
856 completed = readl(port->completed[group]);
858 /* clear completed status register in the hardware.*/
859 writel(completed, port->completed[group]);
861 /* Process successfully completed commands */
862 for (bit = 0; bit < 32 && completed; bit++) {
863 if (!(completed & (1<<bit)))
865 tag = (group << 5) + bit;
867 /* Skip the internal command slot */
868 if (tag == MTIP_TAG_INTERNAL)
871 cmd = &port->commands[tag];
872 if (likely(cmd->comp_func)) {
873 set_bit(tag, tagaccum);
875 atomic_set(&cmd->active, 0);
881 dev_err(&port->dd->pdev->dev,
882 "Missing completion func for tag %d",
884 if (mtip_check_surprise_removal(dd->pdev)) {
885 /* don't proceed further */
892 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
894 /* Restart the port */
896 mtip_restart_port(port);
898 /* Trying to determine the cause of the error */
899 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
901 dd->port->log_buf_dma, 1);
903 dev_warn(&dd->pdev->dev,
904 "Error in READ LOG EXT (10h) command\n");
905 /* non-critical error, don't fail the load */
907 buf = (unsigned char *)dd->port->log_buf;
908 if (buf[259] & 0x1) {
909 dev_info(&dd->pdev->dev,
910 "Write protect bit is set.\n");
911 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
912 fail_all_ncq_write = 1;
913 fail_reason = "write protect";
915 if (buf[288] == 0xF7) {
916 dev_info(&dd->pdev->dev,
917 "Exceeded Tmax, drive in thermal shutdown.\n");
918 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
919 fail_all_ncq_cmds = 1;
920 fail_reason = "thermal shutdown";
922 if (buf[288] == 0xBF) {
923 dev_info(&dd->pdev->dev,
924 "Drive indicates rebuild has failed.\n");
925 fail_all_ncq_cmds = 1;
926 fail_reason = "rebuild failed";
930 /* clear the tag accumulator */
931 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
933 /* Loop through all the groups */
934 for (group = 0; group < dd->slot_groups; group++) {
935 for (bit = 0; bit < 32; bit++) {
937 tag = (group << 5) + bit;
938 cmd = &port->commands[tag];
940 /* If the active bit is set re-issue the command */
941 if (atomic_read(&cmd->active) == 0)
944 fis = (struct host_to_dev_fis *)cmd->command;
946 /* Should re-issue? */
947 if (tag == MTIP_TAG_INTERNAL ||
948 fis->command == ATA_CMD_SET_FEATURES)
951 if (fail_all_ncq_cmds ||
952 (fail_all_ncq_write &&
953 fis->command == ATA_CMD_FPDMA_WRITE)) {
954 dev_warn(&dd->pdev->dev,
955 " Fail: %s w/tag %d [%s].\n",
956 fis->command == ATA_CMD_FPDMA_WRITE ?
959 fail_reason != NULL ?
960 fail_reason : "unknown");
961 atomic_set(&cmd->active, 0);
962 if (cmd->comp_func) {
963 cmd->comp_func(port, tag,
972 * First check if this command has
973 * exceeded its retries.
975 if (reissue && (cmd->retries-- > 0)) {
977 set_bit(tag, tagaccum);
979 /* Re-issue the command. */
980 mtip_issue_ncq_command(port, tag);
985 /* Retire a command that will not be reissued */
986 dev_warn(&port->dd->pdev->dev,
987 "retiring tag %d\n", tag);
988 atomic_set(&cmd->active, 0);
997 dev_warn(&port->dd->pdev->dev,
998 "Bad completion for tag %d\n",
1002 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
1005 /* clear eh_active */
1006 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
1007 wake_up_interruptible(&port->svc_wait);
1009 mod_timer(&port->cmd_timer,
1010 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
1014 * Handle a set device bits interrupt
1016 static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
1019 struct driver_data *dd = port->dd;
1021 struct mtip_cmd *command;
1024 WARN_ON_ONCE(!completed);
1027 /* clear completed status register in the hardware.*/
1028 writel(completed, port->completed[group]);
1030 /* Process completed commands. */
1031 for (bit = 0; (bit < 32) && completed; bit++) {
1032 if (completed & 0x01) {
1033 tag = (group << 5) | bit;
1035 /* skip internal command slot. */
1036 if (unlikely(tag == MTIP_TAG_INTERNAL))
1039 command = &port->commands[tag];
1040 /* make internal callback */
1041 if (likely(command->comp_func)) {
1048 dev_dbg(&dd->pdev->dev,
1049 "Null completion for tag %d",
1052 if (mtip_check_surprise_removal(
1061 /* If last, re-enable interrupts */
1062 if (atomic_dec_return(&dd->irq_workers_active) == 0)
1063 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
1067 * Process legacy pio and d2h interrupts
1069 static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
1071 struct mtip_port *port = dd->port;
1072 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
1074 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
1075 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1076 & (1 << MTIP_TAG_INTERNAL))) {
1077 if (cmd->comp_func) {
1078 cmd->comp_func(port,
1090 * Demux and handle errors
1092 static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
1094 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
1095 mtip_handle_tfe(dd);
1097 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
1098 dev_warn(&dd->pdev->dev,
1099 "Clearing PxSERR.DIAG.x\n");
1100 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
1103 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
1104 dev_warn(&dd->pdev->dev,
1105 "Clearing PxSERR.DIAG.n\n");
1106 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
1109 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
1110 dev_warn(&dd->pdev->dev,
1111 "Port stat errors %x unhandled\n",
1112 (port_stat & ~PORT_IRQ_HANDLED));
1116 static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
1118 struct driver_data *dd = (struct driver_data *) data;
1119 struct mtip_port *port = dd->port;
1120 u32 hba_stat, port_stat;
1122 int do_irq_enable = 1, i, workers;
1123 struct mtip_work *twork;
1125 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1129 /* Acknowledge the interrupt status on the port.*/
1130 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1131 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1133 /* Demux port status */
1134 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
1136 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
1138 /* Start at 1: group zero is always local? */
1139 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
1141 twork = &dd->work[i];
1142 twork->completed = readl(port->completed[i]);
1143 if (twork->completed)
1147 atomic_set(&dd->irq_workers_active, workers);
1149 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
1150 twork = &dd->work[i];
1151 if (twork->completed)
1158 if (likely(dd->work[0].completed))
1159 mtip_workq_sdbfx(port, 0,
1160 dd->work[0].completed);
1164 * Chip quirk: SDB interrupt but nothing
1171 if (unlikely(port_stat & PORT_IRQ_ERR)) {
1172 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
1173 /* don't proceed further */
1176 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1180 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
1183 if (unlikely(port_stat & PORT_IRQ_LEGACY))
1184 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
1187 /* acknowledge interrupt */
1188 if (unlikely(do_irq_enable))
1189 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
1195 * HBA interrupt subroutine.
1198 * @instance Pointer to the driver data structure.
1201 * IRQ_HANDLED A HBA interrupt was pending and handled.
1202 * IRQ_NONE This interrupt was not for the HBA.
1204 static irqreturn_t mtip_irq_handler(int irq, void *instance)
1206 struct driver_data *dd = instance;
1208 return mtip_handle_irq(dd);
1211 static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1213 atomic_set(&port->commands[tag].active, 1);
1214 writel(1 << MTIP_TAG_BIT(tag),
1215 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1218 static bool mtip_pause_ncq(struct mtip_port *port,
1219 struct host_to_dev_fis *fis)
1221 struct host_to_dev_fis *reply;
1222 unsigned long task_file_data;
1224 reply = port->rxfis + RX_FIS_D2H_REG;
1225 task_file_data = readl(port->mmio+PORT_TFDATA);
1227 if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
1228 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1230 if ((task_file_data & 1))
1233 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
1234 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1235 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1236 port->ic_pause_timer = jiffies;
1238 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1239 (fis->features == 0x03)) {
1240 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1241 port->ic_pause_timer = jiffies;
1243 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1244 ((fis->command == 0xFC) &&
1245 (fis->features == 0x27 || fis->features == 0x72 ||
1246 fis->features == 0x62 || fis->features == 0x26))) {
1247 /* Com reset after secure erase or lowlevel format */
1248 mtip_restart_port(port);
1256 * Wait for port to quiesce
1258 * @port Pointer to port data structure
1259 * @timeout Max duration to wait (ms)
1263 * -EBUSY Commands still active
1265 static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1269 unsigned int active = 1;
1271 to = jiffies + msecs_to_jiffies(timeout);
1273 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1274 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
1276 continue; /* svc thd is actively issuing commands */
1278 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
1281 * Ignore s_active bit 0 of array element 0.
1282 * This bit will always be set
1284 active = readl(port->s_active[0]) & 0xFFFFFFFE;
1285 for (n = 1; n < port->dd->slot_groups; n++)
1286 active |= readl(port->s_active[n]);
1292 } while (time_before(jiffies, to));
1294 return active ? -EBUSY : 0;
1298 * Execute an internal command and wait for the completion.
1300 * @port Pointer to the port data structure.
1301 * @fis Pointer to the FIS that describes the command.
1302 * @fis_len Length in WORDS of the FIS.
1303 * @buffer DMA accessible for command data.
1304 * @buf_len Length, in bytes, of the data buffer.
1305 * @opts Command header options, excluding the FIS length
1306 * and the number of PRD entries.
1307 * @timeout Time in ms to wait for the command to complete.
1310 * 0 Command completed successfully.
1311 * -EFAULT The buffer address is not correctly aligned.
1312 * -EBUSY Internal command or other IO in progress.
1313 * -EAGAIN Time out waiting for command to complete.
1315 static int mtip_exec_internal_command(struct mtip_port *port,
1316 struct host_to_dev_fis *fis,
1322 unsigned long timeout)
1324 struct mtip_cmd_sg *command_sg;
1325 DECLARE_COMPLETION_ONSTACK(wait);
1326 int rv = 0, ready2go = 1;
1327 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
1329 struct driver_data *dd = port->dd;
1331 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1332 if (buffer & 0x00000007) {
1333 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
1337 to = jiffies + msecs_to_jiffies(timeout);
1339 ready2go = !test_and_set_bit(MTIP_TAG_INTERNAL,
1344 } while (time_before(jiffies, to));
1346 dev_warn(&dd->pdev->dev,
1347 "Internal cmd active. new cmd [%02X]\n", fis->command);
1350 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1351 port->ic_pause_timer = 0;
1353 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1354 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1356 if (atomic == GFP_KERNEL) {
1357 if (fis->command != ATA_CMD_STANDBYNOW1) {
1358 /* wait for io to complete if non atomic */
1359 if (mtip_quiesce_io(port, 5000) < 0) {
1360 dev_warn(&dd->pdev->dev,
1361 "Failed to quiesce IO\n");
1362 release_slot(port, MTIP_TAG_INTERNAL);
1363 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1364 wake_up_interruptible(&port->svc_wait);
1369 /* Set the completion function and data for the command. */
1370 int_cmd->comp_data = &wait;
1371 int_cmd->comp_func = mtip_completion;
1374 /* Clear completion - we're going to poll */
1375 int_cmd->comp_data = NULL;
1376 int_cmd->comp_func = mtip_null_completion;
1379 /* Copy the command to the command table */
1380 memcpy(int_cmd->command, fis, fis_len*4);
1382 /* Populate the SG list */
1383 int_cmd->command_header->opts =
1384 __force_bit2int cpu_to_le32(opts | fis_len);
1386 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1389 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1391 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1392 command_sg->dba_upper =
1393 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
1395 int_cmd->command_header->opts |=
1396 __force_bit2int cpu_to_le32((1 << 16));
1399 /* Populate the command header */
1400 int_cmd->command_header->byte_count = 0;
1402 /* Issue the command to the hardware */
1403 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1405 if (atomic == GFP_KERNEL) {
1406 /* Wait for the command to complete or timeout. */
1407 if (wait_for_completion_interruptible_timeout(
1409 msecs_to_jiffies(timeout)) <= 0) {
1410 if (rv == -ERESTARTSYS) { /* interrupted */
1411 dev_err(&dd->pdev->dev,
1412 "Internal command [%02X] was interrupted after %lu ms\n",
1413 fis->command, timeout);
1416 } else if (rv == 0) /* timeout */
1417 dev_err(&dd->pdev->dev,
1418 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1419 fis->command, timeout);
1421 dev_err(&dd->pdev->dev,
1422 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1423 fis->command, rv, timeout);
1425 if (mtip_check_surprise_removal(dd->pdev) ||
1426 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1428 dev_err(&dd->pdev->dev,
1429 "Internal command [%02X] wait returned due to SR\n",
1434 mtip_device_reset(dd); /* recover from timeout issue */
1439 u32 hba_stat, port_stat;
1441 /* Spin for <timeout> checking if command still outstanding */
1442 timeout = jiffies + msecs_to_jiffies(timeout);
1443 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1444 & (1 << MTIP_TAG_INTERNAL))
1445 && time_before(jiffies, timeout)) {
1446 if (mtip_check_surprise_removal(dd->pdev)) {
1450 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
1451 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1456 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1460 if (port_stat & PORT_IRQ_ERR) {
1461 dev_err(&dd->pdev->dev,
1462 "Internal command [%02X] failed\n",
1464 mtip_device_reset(dd);
1468 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1469 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1472 dd->mmio + HOST_IRQ_STAT);
1478 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1479 & (1 << MTIP_TAG_INTERNAL)) {
1481 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1482 mtip_device_reset(dd);
1487 /* Clear the allocated and active bits for the internal command. */
1488 atomic_set(&int_cmd->active, 0);
1489 release_slot(port, MTIP_TAG_INTERNAL);
1490 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1494 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1495 wake_up_interruptible(&port->svc_wait);
1501 * Byte-swap ATA ID strings.
1503 * ATA identify data contains strings in byte-swapped 16-bit words.
1504 * They must be swapped (on all architectures) to be usable as C strings.
1505 * This function swaps bytes in-place.
1507 * @buf The buffer location of the string
1508 * @len The number of bytes to swap
1513 static inline void ata_swap_string(u16 *buf, unsigned int len)
1516 for (i = 0; i < (len/2); i++)
1517 be16_to_cpus(&buf[i]);
1521 * Request the device identity information.
1523 * If a user space buffer is not specified, i.e. is NULL, the
1524 * identify information is still read from the drive and placed
1525 * into the identify data buffer (@e port->identify) in the
1526 * port data structure.
1527 * When the identify buffer contains valid identify information @e
1528 * port->identify_valid is non-zero.
1530 * @port Pointer to the port structure.
1531 * @user_buffer A user space buffer where the identify data should be
1535 * 0 Command completed successfully.
1536 * -EFAULT An error occurred while coping data to the user buffer.
1537 * -1 Command failed.
1539 static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1542 struct host_to_dev_fis fis;
1544 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
1547 /* Build the FIS. */
1548 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1551 fis.command = ATA_CMD_ID_ATA;
1553 /* Set the identify information as invalid. */
1554 port->identify_valid = 0;
1556 /* Clear the identify information. */
1557 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1559 /* Execute the command. */
1560 if (mtip_exec_internal_command(port,
1564 sizeof(u16) * ATA_ID_WORDS,
1567 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1574 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1575 * perform field-sensitive swapping on the string fields.
1576 * See the kernel use of ata_id_string() for proof of this.
1578 #ifdef __LITTLE_ENDIAN
1579 ata_swap_string(port->identify + 27, 40); /* model string*/
1580 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1581 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1585 for (i = 0; i < ATA_ID_WORDS; i++)
1586 port->identify[i] = le16_to_cpu(port->identify[i]);
1590 #ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
1591 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1592 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1593 port->dd->trim_supp = true;
1596 port->dd->trim_supp = false;
1598 /* Set the identify buffer as valid. */
1599 port->identify_valid = 1;
1605 ATA_ID_WORDS * sizeof(u16))) {
1616 * Issue a standby immediate command to the device.
1618 * @port Pointer to the port structure.
1621 * 0 Command was executed successfully.
1622 * -1 An error occurred while executing the command.
1624 static int mtip_standby_immediate(struct mtip_port *port)
1627 struct host_to_dev_fis fis;
1628 unsigned long start;
1630 /* Build the FIS. */
1631 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1634 fis.command = ATA_CMD_STANDBYNOW1;
1637 rv = mtip_exec_internal_command(port,
1645 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1646 jiffies_to_msecs(jiffies - start));
1648 dev_warn(&port->dd->pdev->dev,
1649 "STANDBY IMMEDIATE command failed.\n");
1655 * Issue a READ LOG EXT command to the device.
1657 * @port pointer to the port structure.
1658 * @page page number to fetch
1659 * @buffer pointer to buffer
1660 * @buffer_dma dma address corresponding to @buffer
1661 * @sectors page length to fetch, in sectors
1664 * @rv return value from mtip_exec_internal_command()
1666 static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1667 dma_addr_t buffer_dma, unsigned int sectors)
1669 struct host_to_dev_fis fis;
1671 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1674 fis.command = ATA_CMD_READ_LOG_EXT;
1675 fis.sect_count = sectors & 0xFF;
1676 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1679 fis.device = ATA_DEVICE_OBS;
1681 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1683 return mtip_exec_internal_command(port,
1687 sectors * ATA_SECT_SIZE,
1690 MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
1694 * Issue a SMART READ DATA command to the device.
1696 * @port pointer to the port structure.
1697 * @buffer pointer to buffer
1698 * @buffer_dma dma address corresponding to @buffer
1701 * @rv return value from mtip_exec_internal_command()
1703 static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1704 dma_addr_t buffer_dma)
1706 struct host_to_dev_fis fis;
1708 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1711 fis.command = ATA_CMD_SMART;
1712 fis.features = 0xD0;
1716 fis.device = ATA_DEVICE_OBS;
1718 return mtip_exec_internal_command(port,
1729 * Get the value of a smart attribute
1731 * @port pointer to the port structure
1732 * @id attribute number
1733 * @attrib pointer to return attrib information corresponding to @id
1736 * -EINVAL NULL buffer passed or unsupported attribute @id.
1737 * -EPERM Identify data not valid, SMART not supported or not enabled
1739 static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1740 struct smart_attr *attrib)
1743 struct smart_attr *pattr;
1748 if (!port->identify_valid) {
1749 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1752 if (!(port->identify[82] & 0x1)) {
1753 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1756 if (!(port->identify[85] & 0x1)) {
1757 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1761 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1762 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1764 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1768 pattr = (struct smart_attr *)(port->smart_buf + 2);
1769 for (i = 0; i < 29; i++, pattr++)
1770 if (pattr->attr_id == id) {
1771 memcpy(attrib, pattr, sizeof(struct smart_attr));
1776 dev_warn(&port->dd->pdev->dev,
1777 "Query for invalid SMART attribute ID\n");
1785 * Trim unused sectors
1787 * @dd pointer to driver_data structure
1789 * @len # of 512b sectors to trim
1792 * -ENOMEM Out of dma memory
1793 * -EINVAL Invalid parameters passed in, trim not supported
1794 * -EIO Error submitting trim request to hw
1796 static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
1800 u64 tlba, tlen, sect_left;
1801 struct mtip_trim_entry *buf;
1802 dma_addr_t dma_addr;
1803 struct host_to_dev_fis fis;
1805 if (!len || dd->trim_supp == false)
1808 /* Trim request too big */
1809 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1811 /* Trim request not aligned on 4k boundary */
1812 WARN_ON(len % 8 != 0);
1814 /* Warn if vu_trim structure is too big */
1815 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1817 /* Allocate a DMA buffer for the trim structure */
1818 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1822 memset(buf, 0, ATA_SECT_SIZE);
1824 for (i = 0, sect_left = len, tlba = lba;
1825 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1827 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1828 MTIP_MAX_TRIM_ENTRY_LEN :
1830 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1831 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1835 WARN_ON(sect_left != 0);
1838 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1842 fis.features = 0x60;
1844 fis.device = ATA_DEVICE_OBS;
1846 if (mtip_exec_internal_command(dd->port,
1853 MTIP_TRIM_TIMEOUT_MS) < 0)
1856 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1861 * Get the drive capacity.
1863 * @dd Pointer to the device data structure.
1864 * @sectors Pointer to the variable that will receive the sector count.
1867 * 1 Capacity was returned successfully.
1868 * 0 The identify information is invalid.
1870 static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
1872 struct mtip_port *port = dd->port;
1873 u64 total, raw0, raw1, raw2, raw3;
1874 raw0 = port->identify[100];
1875 raw1 = port->identify[101];
1876 raw2 = port->identify[102];
1877 raw3 = port->identify[103];
1878 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1880 return (bool) !!port->identify_valid;
1884 * Display the identify command data.
1886 * @port Pointer to the port data structure.
1891 static void mtip_dump_identify(struct mtip_port *port)
1894 unsigned short revid;
1897 if (!port->identify_valid)
1900 strlcpy(cbuf, (char *)(port->identify+10), 21);
1901 dev_info(&port->dd->pdev->dev,
1902 "Serial No.: %s\n", cbuf);
1904 strlcpy(cbuf, (char *)(port->identify+23), 9);
1905 dev_info(&port->dd->pdev->dev,
1906 "Firmware Ver.: %s\n", cbuf);
1908 strlcpy(cbuf, (char *)(port->identify+27), 41);
1909 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1911 if (mtip_hw_get_capacity(port->dd, §ors))
1912 dev_info(&port->dd->pdev->dev,
1913 "Capacity: %llu sectors (%llu MB)\n",
1915 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1917 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
1918 switch (revid & 0xFF) {
1920 strlcpy(cbuf, "A0", 3);
1923 strlcpy(cbuf, "A2", 3);
1926 strlcpy(cbuf, "?", 2);
1929 dev_info(&port->dd->pdev->dev,
1930 "Card Type: %s\n", cbuf);
1934 * Map the commands scatter list into the command table.
1936 * @command Pointer to the command.
1937 * @nents Number of scatter list entries.
1942 static inline void fill_command_sg(struct driver_data *dd,
1943 struct mtip_cmd *command,
1947 unsigned int dma_len;
1948 struct mtip_cmd_sg *command_sg;
1949 struct scatterlist *sg = command->sg;
1951 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1953 for (n = 0; n < nents; n++) {
1954 dma_len = sg_dma_len(sg);
1955 if (dma_len > 0x400000)
1956 dev_err(&dd->pdev->dev,
1957 "DMA segment length truncated\n");
1958 command_sg->info = __force_bit2int
1959 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1960 command_sg->dba = __force_bit2int
1961 cpu_to_le32(sg_dma_address(sg));
1962 command_sg->dba_upper = __force_bit2int
1963 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
1970 * @brief Execute a drive command.
1972 * return value 0 The command completed successfully.
1973 * return value -1 An error occurred while executing the command.
1975 static int exec_drive_task(struct mtip_port *port, u8 *command)
1977 struct host_to_dev_fis fis;
1978 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1980 /* Build the FIS. */
1981 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1984 fis.command = command[0];
1985 fis.features = command[1];
1986 fis.sect_count = command[2];
1987 fis.sector = command[3];
1988 fis.cyl_low = command[4];
1989 fis.cyl_hi = command[5];
1990 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1992 dbg_printk(MTIP_DRV_NAME " %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
2002 /* Execute the command. */
2003 if (mtip_exec_internal_command(port,
2010 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
2014 command[0] = reply->command; /* Status*/
2015 command[1] = reply->features; /* Error*/
2016 command[4] = reply->cyl_low;
2017 command[5] = reply->cyl_hi;
2019 dbg_printk(MTIP_DRV_NAME " %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
2030 * @brief Execute a drive command.
2032 * @param port Pointer to the port data structure.
2033 * @param command Pointer to the user specified command parameters.
2034 * @param user_buffer Pointer to the user space buffer where read sector
2035 * data should be copied.
2037 * return value 0 The command completed successfully.
2038 * return value -EFAULT An error occurred while copying the completion
2039 * data to the user space buffer.
2040 * return value -1 An error occurred while executing the command.
2042 static int exec_drive_command(struct mtip_port *port, u8 *command,
2043 void __user *user_buffer)
2045 struct host_to_dev_fis fis;
2046 struct host_to_dev_fis *reply;
2048 dma_addr_t dma_addr = 0;
2049 int rv = 0, xfer_sz = command[3];
2055 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
2056 ATA_SECT_SIZE * xfer_sz,
2060 dev_err(&port->dd->pdev->dev,
2061 "Memory allocation failed (%d bytes)\n",
2062 ATA_SECT_SIZE * xfer_sz);
2065 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
2068 /* Build the FIS. */
2069 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2072 fis.command = command[0];
2073 fis.features = command[2];
2074 fis.sect_count = command[3];
2075 if (fis.command == ATA_CMD_SMART) {
2076 fis.sector = command[1];
2082 reply = (port->rxfis + RX_FIS_PIO_SETUP);
2084 reply = (port->rxfis + RX_FIS_D2H_REG);
2086 dbg_printk(MTIP_DRV_NAME
2087 " %s: User Command: cmd %x, sect %x, "
2088 "feat %x, sectcnt %x\n",
2095 /* Execute the command. */
2096 if (mtip_exec_internal_command(port,
2099 (xfer_sz ? dma_addr : 0),
2100 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
2103 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
2106 goto exit_drive_command;
2109 /* Collect the completion status. */
2110 command[0] = reply->command; /* Status*/
2111 command[1] = reply->features; /* Error*/
2112 command[2] = reply->sect_count;
2114 dbg_printk(MTIP_DRV_NAME
2115 " %s: Completion Status: stat %x, "
2116 "err %x, nsect %x\n",
2123 if (copy_to_user(user_buffer,
2125 ATA_SECT_SIZE * command[3])) {
2127 goto exit_drive_command;
2132 dmam_free_coherent(&port->dd->pdev->dev,
2133 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
2138 * Indicates whether a command has a single sector payload.
2140 * @command passed to the device to perform the certain event.
2141 * @features passed to the device to perform the certain event.
2144 * 1 command is one that always has a single sector payload,
2145 * regardless of the value in the Sector Count field.
2149 static unsigned int implicit_sector(unsigned char command,
2150 unsigned char features)
2152 unsigned int rv = 0;
2154 /* list of commands that have an implicit sector count of 1 */
2156 case ATA_CMD_SEC_SET_PASS:
2157 case ATA_CMD_SEC_UNLOCK:
2158 case ATA_CMD_SEC_ERASE_PREP:
2159 case ATA_CMD_SEC_ERASE_UNIT:
2160 case ATA_CMD_SEC_FREEZE_LOCK:
2161 case ATA_CMD_SEC_DISABLE_PASS:
2162 case ATA_CMD_PMP_READ:
2163 case ATA_CMD_PMP_WRITE:
2166 case ATA_CMD_SET_MAX:
2167 if (features == ATA_SET_MAX_UNLOCK)
2171 if ((features == ATA_SMART_READ_VALUES) ||
2172 (features == ATA_SMART_READ_THRESHOLDS))
2175 case ATA_CMD_CONF_OVERLAY:
2176 if ((features == ATA_DCO_IDENTIFY) ||
2177 (features == ATA_DCO_SET))
2183 static void mtip_set_timeout(struct driver_data *dd,
2184 struct host_to_dev_fis *fis,
2185 unsigned int *timeout, u8 erasemode)
2187 switch (fis->command) {
2188 case ATA_CMD_DOWNLOAD_MICRO:
2189 *timeout = 120000; /* 2 minutes */
2191 case ATA_CMD_SEC_ERASE_UNIT:
2194 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
2196 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
2198 case ATA_CMD_STANDBYNOW1:
2199 *timeout = 120000; /* 2 minutes */
2203 *timeout = 60000; /* 60 seconds */
2206 *timeout = 15000; /* 15 seconds */
2209 *timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
2215 * Executes a taskfile
2216 * See ide_taskfile_ioctl() for derivation
2218 static int exec_drive_taskfile(struct driver_data *dd,
2220 ide_task_request_t *req_task,
2223 struct host_to_dev_fis fis;
2224 struct host_to_dev_fis *reply;
2227 dma_addr_t outbuf_dma = 0;
2228 dma_addr_t inbuf_dma = 0;
2229 dma_addr_t dma_buffer = 0;
2231 unsigned int taskin = 0;
2232 unsigned int taskout = 0;
2234 unsigned int timeout;
2235 unsigned int force_single_sector;
2236 unsigned int transfer_size;
2237 unsigned long task_file_data;
2238 int intotal = outtotal + req_task->out_size;
2241 taskout = req_task->out_size;
2242 taskin = req_task->in_size;
2243 /* 130560 = 512 * 0xFF*/
2244 if (taskin > 130560 || taskout > 130560) {
2250 outbuf = kzalloc(taskout, GFP_KERNEL);
2251 if (outbuf == NULL) {
2255 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
2259 outbuf_dma = pci_map_single(dd->pdev,
2263 if (outbuf_dma == 0) {
2267 dma_buffer = outbuf_dma;
2271 inbuf = kzalloc(taskin, GFP_KERNEL);
2272 if (inbuf == NULL) {
2277 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2281 inbuf_dma = pci_map_single(dd->pdev,
2283 taskin, DMA_FROM_DEVICE);
2284 if (inbuf_dma == 0) {
2288 dma_buffer = inbuf_dma;
2291 /* only supports PIO and non-data commands from this ioctl. */
2292 switch (req_task->data_phase) {
2294 nsect = taskout / ATA_SECT_SIZE;
2295 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2298 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2300 case TASKFILE_NO_DATA:
2301 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2308 /* Build the FIS. */
2309 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2313 fis.command = req_task->io_ports[7];
2314 fis.features = req_task->io_ports[1];
2315 fis.sect_count = req_task->io_ports[2];
2316 fis.lba_low = req_task->io_ports[3];
2317 fis.lba_mid = req_task->io_ports[4];
2318 fis.lba_hi = req_task->io_ports[5];
2319 /* Clear the dev bit*/
2320 fis.device = req_task->io_ports[6] & ~0x10;
2322 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2323 req_task->in_flags.all =
2324 IDE_TASKFILE_STD_IN_FLAGS |
2325 (IDE_HOB_STD_IN_FLAGS << 8);
2326 fis.lba_low_ex = req_task->hob_ports[3];
2327 fis.lba_mid_ex = req_task->hob_ports[4];
2328 fis.lba_hi_ex = req_task->hob_ports[5];
2329 fis.features_ex = req_task->hob_ports[1];
2330 fis.sect_cnt_ex = req_task->hob_ports[2];
2333 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2336 force_single_sector = implicit_sector(fis.command, fis.features);
2338 if ((taskin || taskout) && (!fis.sect_count)) {
2340 fis.sect_count = nsect;
2342 if (!force_single_sector) {
2343 dev_warn(&dd->pdev->dev,
2344 "data movement but "
2345 "sect_count is 0\n");
2352 dbg_printk(MTIP_DRV_NAME
2353 " %s: cmd %x, feat %x, nsect %x,"
2354 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2365 /* check for erase mode support during secure erase.*/
2366 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2367 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
2371 mtip_set_timeout(dd, &fis, &timeout, erasemode);
2373 /* Determine the correct transfer size.*/
2374 if (force_single_sector)
2375 transfer_size = ATA_SECT_SIZE;
2377 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2379 /* Execute the command.*/
2380 if (mtip_exec_internal_command(dd->port,
2392 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2394 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2395 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2396 req_task->io_ports[7] = reply->control;
2398 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2399 req_task->io_ports[7] = reply->command;
2402 /* reclaim the DMA buffers.*/
2404 pci_unmap_single(dd->pdev, inbuf_dma,
2405 taskin, DMA_FROM_DEVICE);
2407 pci_unmap_single(dd->pdev, outbuf_dma,
2408 taskout, DMA_TO_DEVICE);
2412 /* return the ATA registers to the caller.*/
2413 req_task->io_ports[1] = reply->features;
2414 req_task->io_ports[2] = reply->sect_count;
2415 req_task->io_ports[3] = reply->lba_low;
2416 req_task->io_ports[4] = reply->lba_mid;
2417 req_task->io_ports[5] = reply->lba_hi;
2418 req_task->io_ports[6] = reply->device;
2420 if (req_task->out_flags.all & 1) {
2422 req_task->hob_ports[3] = reply->lba_low_ex;
2423 req_task->hob_ports[4] = reply->lba_mid_ex;
2424 req_task->hob_ports[5] = reply->lba_hi_ex;
2425 req_task->hob_ports[1] = reply->features_ex;
2426 req_task->hob_ports[2] = reply->sect_cnt_ex;
2428 dbg_printk(MTIP_DRV_NAME
2429 " %s: Completion: stat %x,"
2430 "err %x, sect_cnt %x, lbalo %x,"
2431 "lbamid %x, lbahi %x, dev %x\n",
2433 req_task->io_ports[7],
2434 req_task->io_ports[1],
2435 req_task->io_ports[2],
2436 req_task->io_ports[3],
2437 req_task->io_ports[4],
2438 req_task->io_ports[5],
2439 req_task->io_ports[6]);
2442 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2448 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2455 pci_unmap_single(dd->pdev, inbuf_dma,
2456 taskin, DMA_FROM_DEVICE);
2458 pci_unmap_single(dd->pdev, outbuf_dma,
2459 taskout, DMA_TO_DEVICE);
2467 * Handle IOCTL calls from the Block Layer.
2469 * This function is called by the Block Layer when it receives an IOCTL
2470 * command that it does not understand. If the IOCTL command is not supported
2471 * this function returns -ENOTTY.
2473 * @dd Pointer to the driver data structure.
2474 * @cmd IOCTL command passed from the Block Layer.
2475 * @arg IOCTL argument passed from the Block Layer.
2478 * 0 The IOCTL completed successfully.
2479 * -ENOTTY The specified command is not supported.
2480 * -EFAULT An error occurred copying data to a user space buffer.
2481 * -EIO An error occurred while executing the command.
2483 static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2487 case HDIO_GET_IDENTITY:
2489 if (copy_to_user((void __user *)arg, dd->port->identify,
2490 sizeof(u16) * ATA_ID_WORDS))
2494 case HDIO_DRIVE_CMD:
2496 u8 drive_command[4];
2498 /* Copy the user command info to our buffer. */
2499 if (copy_from_user(drive_command,
2500 (void __user *) arg,
2501 sizeof(drive_command)))
2504 /* Execute the drive command. */
2505 if (exec_drive_command(dd->port,
2507 (void __user *) (arg+4)))
2510 /* Copy the status back to the users buffer. */
2511 if (copy_to_user((void __user *) arg,
2513 sizeof(drive_command)))
2518 case HDIO_DRIVE_TASK:
2520 u8 drive_command[7];
2522 /* Copy the user command info to our buffer. */
2523 if (copy_from_user(drive_command,
2524 (void __user *) arg,
2525 sizeof(drive_command)))
2528 /* Execute the drive command. */
2529 if (exec_drive_task(dd->port, drive_command))
2532 /* Copy the status back to the users buffer. */
2533 if (copy_to_user((void __user *) arg,
2535 sizeof(drive_command)))
2540 case HDIO_DRIVE_TASKFILE: {
2541 ide_task_request_t req_task;
2544 if (copy_from_user(&req_task, (void __user *) arg,
2548 outtotal = sizeof(req_task);
2550 ret = exec_drive_taskfile(dd, (void __user *) arg,
2551 &req_task, outtotal);
2553 if (copy_to_user((void __user *) arg, &req_task,
2567 * Submit an IO to the hw
2569 * This function is called by the block layer to issue an io
2570 * to the device. Upon completion, the callback function will
2571 * be called with the data parameter passed as the callback data.
2573 * @dd Pointer to the driver data structure.
2574 * @start First sector to read.
2575 * @nsect Number of sectors to read.
2576 * @nents Number of entries in scatter list for the read command.
2577 * @tag The tag of this read command.
2578 * @callback Pointer to the function that should be called
2579 * when the read completes.
2580 * @data Callback data passed to the callback function
2581 * when the read completes.
2582 * @dir Direction (read or write)
2587 static void mtip_hw_submit_io(struct driver_data *dd, sector_t sector,
2588 int nsect, int nents, int tag, void *callback,
2589 void *data, int dir, int unaligned)
2591 struct host_to_dev_fis *fis;
2592 struct mtip_port *port = dd->port;
2593 struct mtip_cmd *command = &port->commands[tag];
2594 int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2597 /* Map the scatter list for DMA access */
2598 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
2600 command->scatter_ents = nents;
2602 command->unaligned = unaligned;
2604 * The number of retries for this command before it is
2605 * reported as a failure to the upper layers.
2607 command->retries = MTIP_MAX_RETRIES;
2610 fis = command->command;
2614 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
2615 fis->lba_low = start & 0xFF;
2616 fis->lba_mid = (start >> 8) & 0xFF;
2617 fis->lba_hi = (start >> 16) & 0xFF;
2618 fis->lba_low_ex = (start >> 24) & 0xFF;
2619 fis->lba_mid_ex = (start >> 32) & 0xFF;
2620 fis->lba_hi_ex = (start >> 40) & 0xFF;
2621 fis->device = 1 << 6;
2622 fis->features = nsect & 0xFF;
2623 fis->features_ex = (nsect >> 8) & 0xFF;
2624 fis->sect_count = ((tag << 3) | (tag >> 5));
2625 fis->sect_cnt_ex = 0;
2629 fill_command_sg(dd, command, nents);
2632 fis->device |= 1 << 7;
2634 /* Populate the command header */
2635 command->command_header->opts =
2636 __force_bit2int cpu_to_le32(
2637 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
2638 command->command_header->byte_count = 0;
2641 * Set the completion function and data for the command
2642 * within this layer.
2644 command->comp_data = dd;
2645 command->comp_func = mtip_async_complete;
2646 command->direction = dma_dir;
2649 * Set the completion function and data for the command passed
2650 * from the upper layer.
2652 command->async_data = data;
2653 command->async_callback = callback;
2656 * To prevent this command from being issued
2657 * if an internal command is in progress or error handling is active.
2659 if (port->flags & MTIP_PF_PAUSE_IO) {
2660 set_bit(tag, port->cmds_to_issue);
2661 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
2665 /* Issue the command to the hardware */
2666 mtip_issue_ncq_command(port, tag);
2672 * Release a command slot.
2674 * @dd Pointer to the driver data structure.
2680 static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag,
2683 struct semaphore *sem = unaligned ? &dd->port->cmd_slot_unal :
2684 &dd->port->cmd_slot;
2685 release_slot(dd->port, tag);
2690 * Obtain a command slot and return its associated scatter list.
2692 * @dd Pointer to the driver data structure.
2693 * @tag Pointer to an int that will receive the allocated command
2697 * Pointer to the scatter list for the allocated command slot
2698 * or NULL if no command slots are available.
2700 static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2701 int *tag, int unaligned)
2703 struct semaphore *sem = unaligned ? &dd->port->cmd_slot_unal :
2704 &dd->port->cmd_slot;
2707 * It is possible that, even with this semaphore, a thread
2708 * may think that no command slots are available. Therefore, we
2709 * need to make an attempt to get_slot().
2712 *tag = get_slot(dd->port);
2714 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
2718 if (unlikely(*tag < 0)) {
2723 return dd->port->commands[*tag].sg;
2727 * Sysfs status dump.
2729 * @dev Pointer to the device structure, passed by the kernrel.
2730 * @attr Pointer to the device_attribute structure passed by the kernel.
2731 * @buf Pointer to the char buffer that will receive the stats info.
2734 * The size, in bytes, of the data copied into buf.
2736 static ssize_t mtip_hw_show_status(struct device *dev,
2737 struct device_attribute *attr,
2740 struct driver_data *dd = dev_to_disk(dev)->private_data;
2743 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
2744 size += sprintf(buf, "%s", "thermal_shutdown\n");
2745 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
2746 size += sprintf(buf, "%s", "write_protect\n");
2748 size += sprintf(buf, "%s", "online\n");
2753 static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
2755 /* debugsfs entries */
2757 static ssize_t show_device_status(struct device_driver *drv, char *buf)
2760 struct driver_data *dd, *tmp;
2761 unsigned long flags;
2765 spin_lock_irqsave(&dev_lock, flags);
2766 size += sprintf(&buf[size], "Devices Present:\n");
2767 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
2770 dd->port->identify &&
2771 dd->port->identify_valid) {
2773 (char *) (dd->port->identify + 10), 21);
2774 status = *(dd->port->identify + 141);
2776 memset(id_buf, 0, 42);
2781 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2782 size += sprintf(&buf[size],
2783 " device %s %s (ftl rebuild %d %%)\n",
2784 dev_name(&dd->pdev->dev),
2788 size += sprintf(&buf[size],
2790 dev_name(&dd->pdev->dev),
2796 size += sprintf(&buf[size], "Devices Being Removed:\n");
2797 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
2800 dd->port->identify &&
2801 dd->port->identify_valid) {
2803 (char *) (dd->port->identify+10), 21);
2804 status = *(dd->port->identify + 141);
2806 memset(id_buf, 0, 42);
2811 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2812 size += sprintf(&buf[size],
2813 " device %s %s (ftl rebuild %d %%)\n",
2814 dev_name(&dd->pdev->dev),
2818 size += sprintf(&buf[size],
2820 dev_name(&dd->pdev->dev),
2825 spin_unlock_irqrestore(&dev_lock, flags);
2830 static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2831 size_t len, loff_t *offset)
2833 struct driver_data *dd = (struct driver_data *)f->private_data;
2838 if (!len || *offset)
2841 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2843 dev_err(&dd->pdev->dev,
2844 "Memory allocation: status buffer\n");
2848 size += show_device_status(NULL, buf);
2850 *offset = size <= len ? size : len;
2851 size = copy_to_user(ubuf, buf, *offset);
2856 return rv ? rv : *offset;
2859 static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2860 size_t len, loff_t *offset)
2862 struct driver_data *dd = (struct driver_data *)f->private_data;
2864 u32 group_allocated;
2871 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2873 dev_err(&dd->pdev->dev,
2874 "Memory allocation: register buffer\n");
2878 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2880 for (n = dd->slot_groups-1; n >= 0; n--)
2881 size += sprintf(&buf[size], "%08X ",
2882 readl(dd->port->s_active[n]));
2884 size += sprintf(&buf[size], "]\n");
2885 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2887 for (n = dd->slot_groups-1; n >= 0; n--)
2888 size += sprintf(&buf[size], "%08X ",
2889 readl(dd->port->cmd_issue[n]));
2891 size += sprintf(&buf[size], "]\n");
2892 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2894 for (n = dd->slot_groups-1; n >= 0; n--)
2895 size += sprintf(&buf[size], "%08X ",
2896 readl(dd->port->completed[n]));
2898 size += sprintf(&buf[size], "]\n");
2899 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2900 readl(dd->port->mmio + PORT_IRQ_STAT));
2901 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2902 readl(dd->mmio + HOST_IRQ_STAT));
2903 size += sprintf(&buf[size], "\n");
2905 size += sprintf(&buf[size], "L/ Allocated : [ 0x");
2907 for (n = dd->slot_groups-1; n >= 0; n--) {
2908 if (sizeof(long) > sizeof(u32))
2910 dd->port->allocated[n/2] >> (32*(n&1));
2912 group_allocated = dd->port->allocated[n];
2913 size += sprintf(&buf[size], "%08X ", group_allocated);
2915 size += sprintf(&buf[size], "]\n");
2917 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2919 for (n = dd->slot_groups-1; n >= 0; n--) {
2920 if (sizeof(long) > sizeof(u32))
2922 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2924 group_allocated = dd->port->cmds_to_issue[n];
2925 size += sprintf(&buf[size], "%08X ", group_allocated);
2927 size += sprintf(&buf[size], "]\n");
2929 *offset = size <= len ? size : len;
2930 size = copy_to_user(ubuf, buf, *offset);
2935 return rv ? rv : *offset;
2938 static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2939 size_t len, loff_t *offset)
2941 struct driver_data *dd = (struct driver_data *)f->private_data;
2949 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2951 dev_err(&dd->pdev->dev,
2952 "Memory allocation: flag buffer\n");
2956 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2958 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2961 *offset = size <= len ? size : len;
2962 size = copy_to_user(ubuf, buf, *offset);
2967 return rv ? rv : *offset;
2970 static const struct file_operations mtip_device_status_fops = {
2971 .owner = THIS_MODULE,
2972 .open = simple_open,
2973 .read = mtip_hw_read_device_status,
2974 .llseek = no_llseek,
2977 static const struct file_operations mtip_regs_fops = {
2978 .owner = THIS_MODULE,
2979 .open = simple_open,
2980 .read = mtip_hw_read_registers,
2981 .llseek = no_llseek,
2984 static const struct file_operations mtip_flags_fops = {
2985 .owner = THIS_MODULE,
2986 .open = simple_open,
2987 .read = mtip_hw_read_flags,
2988 .llseek = no_llseek,
2992 * Create 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_init(struct driver_data *dd, struct kobject *kobj)
3006 if (sysfs_create_file(kobj, &dev_attr_status.attr))
3007 dev_warn(&dd->pdev->dev,
3008 "Error creating 'status' sysfs entry\n");
3013 * Remove the sysfs related attributes.
3015 * @dd Pointer to the driver data structure.
3016 * @kobj Pointer to the kobj for the block device.
3019 * 0 Operation completed successfully.
3020 * -EINVAL Invalid parameter.
3022 static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
3027 sysfs_remove_file(kobj, &dev_attr_status.attr);
3032 static int mtip_hw_debugfs_init(struct driver_data *dd)
3037 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
3038 if (IS_ERR_OR_NULL(dd->dfs_node)) {
3039 dev_warn(&dd->pdev->dev,
3040 "Error creating node %s under debugfs\n",
3041 dd->disk->disk_name);
3042 dd->dfs_node = NULL;
3046 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
3048 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
3054 static void mtip_hw_debugfs_exit(struct driver_data *dd)
3057 debugfs_remove_recursive(dd->dfs_node);
3060 static int mtip_free_orphan(struct driver_data *dd)
3062 struct kobject *kobj;
3065 if (dd->bdev->bd_holders >= 1)
3072 mtip_hw_debugfs_exit(dd);
3074 spin_lock(&rssd_index_lock);
3075 ida_remove(&rssd_index_ida, dd->index);
3076 spin_unlock(&rssd_index_lock);
3078 if (!test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag) &&
3079 test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)) {
3083 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3085 mtip_hw_sysfs_exit(dd, kobj);
3088 del_gendisk(dd->disk);
3092 dd->queue->queuedata = NULL;
3093 blk_cleanup_queue(dd->queue);
3102 * Perform any init/resume time hardware setup
3104 * @dd Pointer to the driver data structure.
3109 static inline void hba_setup(struct driver_data *dd)
3112 hwdata = readl(dd->mmio + HOST_HSORG);
3114 /* interrupt bug workaround: use only 1 IS bit.*/
3116 HSORG_DISABLE_SLOTGRP_INTR |
3117 HSORG_DISABLE_SLOTGRP_PXIS,
3118 dd->mmio + HOST_HSORG);
3121 static int mtip_device_unaligned_constrained(struct driver_data *dd)
3123 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
3127 * Detect the details of the product, and store anything needed
3128 * into the driver data structure. This includes product type and
3129 * version and number of slot groups.
3131 * @dd Pointer to the driver data structure.
3136 static void mtip_detect_product(struct driver_data *dd)
3139 unsigned int rev, slotgroups;
3142 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
3144 * [15:8] hardware/software interface rev#
3145 * [ 3] asic-style interface
3146 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
3148 hwdata = readl(dd->mmio + HOST_HSORG);
3150 dd->product_type = MTIP_PRODUCT_UNKNOWN;
3151 dd->slot_groups = 1;
3154 dd->product_type = MTIP_PRODUCT_ASICFPGA;
3155 rev = (hwdata & HSORG_HWREV) >> 8;
3156 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
3157 dev_info(&dd->pdev->dev,
3158 "ASIC-FPGA design, HS rev 0x%x, "
3159 "%i slot groups [%i slots]\n",
3164 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
3165 dev_warn(&dd->pdev->dev,
3166 "Warning: driver only supports "
3167 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
3168 slotgroups = MTIP_MAX_SLOT_GROUPS;
3170 dd->slot_groups = slotgroups;
3174 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
3178 * Blocking wait for FTL rebuild to complete
3180 * @dd Pointer to the DRIVER_DATA structure.
3183 * 0 FTL rebuild completed successfully
3184 * -EFAULT FTL rebuild error/timeout/interruption
3186 static int mtip_ftl_rebuild_poll(struct driver_data *dd)
3188 unsigned long timeout, cnt = 0, start;
3190 dev_warn(&dd->pdev->dev,
3191 "FTL rebuild in progress. Polling for completion.\n");
3194 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
3197 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3200 if (mtip_check_surprise_removal(dd->pdev))
3203 if (mtip_get_identify(dd->port, NULL) < 0)
3206 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3207 MTIP_FTL_REBUILD_MAGIC) {
3209 /* Print message every 3 minutes */
3211 dev_warn(&dd->pdev->dev,
3212 "FTL rebuild in progress (%d secs).\n",
3213 jiffies_to_msecs(jiffies - start) / 1000);
3217 dev_warn(&dd->pdev->dev,
3218 "FTL rebuild complete (%d secs).\n",
3219 jiffies_to_msecs(jiffies - start) / 1000);
3220 mtip_block_initialize(dd);
3224 } while (time_before(jiffies, timeout));
3226 /* Check for timeout */
3227 dev_err(&dd->pdev->dev,
3228 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
3229 jiffies_to_msecs(jiffies - start) / 1000);
3234 * service thread to issue queued commands
3236 * @data Pointer to the driver data structure.
3242 static int mtip_service_thread(void *data)
3244 struct driver_data *dd = (struct driver_data *)data;
3245 unsigned long slot, slot_start, slot_wrap;
3246 unsigned int num_cmd_slots = dd->slot_groups * 32;
3247 struct mtip_port *port = dd->port;
3252 * the condition is to check neither an internal command is
3253 * is in progress nor error handling is active
3255 wait_event_interruptible(port->svc_wait, (port->flags) &&
3256 !(port->flags & MTIP_PF_PAUSE_IO));
3258 if (kthread_should_stop())
3261 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
3263 /* If I am an orphan, start self cleanup */
3264 if (test_bit(MTIP_PF_SR_CLEANUP_BIT, &port->flags))
3267 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3271 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
3273 /* used to restrict the loop to one iteration */
3274 slot_start = num_cmd_slots;
3277 slot = find_next_bit(port->cmds_to_issue,
3278 num_cmd_slots, slot);
3279 if (slot_wrap == 1) {
3280 if ((slot_start >= slot) ||
3281 (slot >= num_cmd_slots))
3284 if (unlikely(slot_start == num_cmd_slots))
3287 if (unlikely(slot == num_cmd_slots)) {
3293 /* Issue the command to the hardware */
3294 mtip_issue_ncq_command(port, slot);
3296 clear_bit(slot, port->cmds_to_issue);
3299 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
3300 } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
3301 if (mtip_ftl_rebuild_poll(dd) < 0)
3302 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
3304 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
3306 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
3308 if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
3312 /* wait for pci remove to exit */
3314 if (test_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag))
3316 msleep_interruptible(1000);
3317 if (kthread_should_stop())
3322 ret = mtip_free_orphan(dd);
3324 /* NOTE: All data structures are invalid, do not
3325 * access any here */
3328 msleep_interruptible(1000);
3329 if (kthread_should_stop())
3337 * DMA region teardown
3339 * @dd Pointer to driver_data structure
3344 static void mtip_dma_free(struct driver_data *dd)
3347 struct mtip_port *port = dd->port;
3350 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3351 port->block1, port->block1_dma);
3353 if (port->command_list) {
3354 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3355 port->command_list, port->command_list_dma);
3358 for (i = 0; i < MTIP_MAX_COMMAND_SLOTS; i++) {
3359 if (port->commands[i].command)
3360 dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3361 port->commands[i].command,
3362 port->commands[i].command_dma);
3369 * @dd Pointer to driver_data structure
3372 * -ENOMEM Not enough free DMA region space to initialize driver
3374 static int mtip_dma_alloc(struct driver_data *dd)
3376 struct mtip_port *port = dd->port;
3378 u32 host_cap_64 = readl(dd->mmio + HOST_CAP) & HOST_CAP_64;
3380 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
3382 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3383 &port->block1_dma, GFP_KERNEL);
3386 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
3388 /* Allocate dma memory for command list */
3389 port->command_list =
3390 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3391 &port->command_list_dma, GFP_KERNEL);
3392 if (!port->command_list) {
3393 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3394 port->block1, port->block1_dma);
3395 port->block1 = NULL;
3396 port->block1_dma = 0;
3399 memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
3401 /* Setup all pointers into first DMA region */
3402 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
3403 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
3404 port->identify = port->block1 + AHCI_IDFY_OFFSET;
3405 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
3406 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
3407 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
3408 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
3409 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
3411 /* Setup per command SGL DMA region */
3413 /* Point the command headers at the command tables */
3414 for (i = 0; i < MTIP_MAX_COMMAND_SLOTS; i++) {
3415 port->commands[i].command =
3416 dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3417 &port->commands[i].command_dma, GFP_KERNEL);
3418 if (!port->commands[i].command) {
3423 memset(port->commands[i].command, 0, CMD_DMA_ALLOC_SZ);
3425 port->commands[i].command_header = port->command_list +
3426 (sizeof(struct mtip_cmd_hdr) * i);
3427 port->commands[i].command_header_dma =
3428 dd->port->command_list_dma +
3429 (sizeof(struct mtip_cmd_hdr) * i);
3432 port->commands[i].command_header->ctbau =
3433 __force_bit2int cpu_to_le32(
3434 (port->commands[i].command_dma >> 16) >> 16);
3436 port->commands[i].command_header->ctba =
3437 __force_bit2int cpu_to_le32(
3438 port->commands[i].command_dma & 0xFFFFFFFF);
3440 sg_init_table(port->commands[i].sg, MTIP_MAX_SG);
3442 /* Mark command as currently inactive */
3443 atomic_set(&dd->port->commands[i].active, 0);
3449 * Called once for each card.
3451 * @dd Pointer to the driver data structure.
3454 * 0 on success, else an error code.
3456 static int mtip_hw_init(struct driver_data *dd)
3460 unsigned int num_command_slots;
3461 unsigned long timeout, timetaken;
3463 struct smart_attr attr242;
3465 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3467 mtip_detect_product(dd);
3468 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3472 num_command_slots = dd->slot_groups * 32;
3476 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3479 dev_err(&dd->pdev->dev,
3480 "Memory allocation: port structure\n");
3484 /* Continue workqueue setup */
3485 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3486 dd->work[i].port = dd->port;
3488 /* Enable unaligned IO constraints for some devices */
3489 if (mtip_device_unaligned_constrained(dd))
3490 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3492 dd->unal_qdepth = 0;
3494 /* Counting semaphore to track command slot usage */
3495 sema_init(&dd->port->cmd_slot, num_command_slots - 1 - dd->unal_qdepth);
3496 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
3498 /* Spinlock to prevent concurrent issue */
3499 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3500 spin_lock_init(&dd->port->cmd_issue_lock[i]);
3502 /* Set the port mmio base address. */
3503 dd->port->mmio = dd->mmio + PORT_OFFSET;
3506 /* DMA allocations */
3507 rv = mtip_dma_alloc(dd);
3511 /* Setup the pointers to the extended s_active and CI registers. */
3512 for (i = 0; i < dd->slot_groups; i++) {
3513 dd->port->s_active[i] =
3514 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3515 dd->port->cmd_issue[i] =
3516 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3517 dd->port->completed[i] =
3518 dd->port->mmio + i*0x80 + PORT_SDBV;
3521 timetaken = jiffies;
3522 timeout = jiffies + msecs_to_jiffies(30000);
3523 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3524 time_before(jiffies, timeout)) {
3527 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3528 timetaken = jiffies - timetaken;
3529 dev_warn(&dd->pdev->dev,
3530 "Surprise removal detected at %u ms\n",
3531 jiffies_to_msecs(timetaken));
3535 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
3536 timetaken = jiffies - timetaken;
3537 dev_warn(&dd->pdev->dev,
3538 "Removal detected at %u ms\n",
3539 jiffies_to_msecs(timetaken));
3544 /* Conditionally reset the HBA. */
3545 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3546 if (mtip_hba_reset(dd) < 0) {
3547 dev_err(&dd->pdev->dev,
3548 "Card did not reset within timeout\n");
3553 /* Clear any pending interrupts on the HBA */
3554 writel(readl(dd->mmio + HOST_IRQ_STAT),
3555 dd->mmio + HOST_IRQ_STAT);
3558 mtip_init_port(dd->port);
3559 mtip_start_port(dd->port);
3561 /* Setup the ISR and enable interrupts. */
3562 rv = devm_request_irq(&dd->pdev->dev,
3566 dev_driver_string(&dd->pdev->dev),
3570 dev_err(&dd->pdev->dev,
3571 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3574 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
3576 /* Enable interrupts on the HBA. */
3577 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3578 dd->mmio + HOST_CTL);
3580 init_timer(&dd->port->cmd_timer);
3581 init_waitqueue_head(&dd->port->svc_wait);
3583 dd->port->cmd_timer.data = (unsigned long int) dd->port;
3584 dd->port->cmd_timer.function = mtip_timeout_function;
3585 mod_timer(&dd->port->cmd_timer,
3586 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
3589 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
3594 if (mtip_get_identify(dd->port, NULL) < 0) {
3598 mtip_dump_identify(dd->port);
3600 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3601 MTIP_FTL_REBUILD_MAGIC) {
3602 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3603 return MTIP_FTL_REBUILD_MAGIC;
3606 /* check write protect, over temp and rebuild statuses */
3607 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3609 dd->port->log_buf_dma, 1);
3611 dev_warn(&dd->pdev->dev,
3612 "Error in READ LOG EXT (10h) command\n");
3613 /* non-critical error, don't fail the load */
3615 buf = (unsigned char *)dd->port->log_buf;
3616 if (buf[259] & 0x1) {
3617 dev_info(&dd->pdev->dev,
3618 "Write protect bit is set.\n");
3619 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3621 if (buf[288] == 0xF7) {
3622 dev_info(&dd->pdev->dev,
3623 "Exceeded Tmax, drive in thermal shutdown.\n");
3624 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3626 if (buf[288] == 0xBF) {
3627 dev_info(&dd->pdev->dev,
3628 "Drive is in security locked state.\n");
3629 set_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
3633 /* get write protect progess */
3634 memset(&attr242, 0, sizeof(struct smart_attr));
3635 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3636 dev_warn(&dd->pdev->dev,
3637 "Unable to check write protect progress\n");
3639 dev_info(&dd->pdev->dev,
3640 "Write protect progress: %u%% (%u blocks)\n",
3641 attr242.cur, le32_to_cpu(attr242.data));
3645 del_timer_sync(&dd->port->cmd_timer);
3647 /* Disable interrupts on the HBA. */
3648 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3649 dd->mmio + HOST_CTL);
3651 /* Release the IRQ. */
3652 irq_set_affinity_hint(dd->pdev->irq, NULL);
3653 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3656 mtip_deinit_port(dd->port);
3660 /* Free the memory allocated for the for structure. */
3667 * Called to deinitialize an interface.
3669 * @dd Pointer to the driver data structure.
3674 static int mtip_hw_exit(struct driver_data *dd)
3677 * Send standby immediate (E0h) to the drive so that it
3681 if (!test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag))
3682 if (mtip_standby_immediate(dd->port))
3683 dev_warn(&dd->pdev->dev,
3684 "STANDBY IMMEDIATE failed\n");
3686 /* de-initialize the port. */
3687 mtip_deinit_port(dd->port);
3689 /* Disable interrupts on the HBA. */
3690 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3691 dd->mmio + HOST_CTL);
3694 del_timer_sync(&dd->port->cmd_timer);
3696 /* Release the IRQ. */
3697 irq_set_affinity_hint(dd->pdev->irq, NULL);
3698 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3700 /* Free dma regions */
3703 /* Free the memory allocated for the for structure. */
3711 * Issue a Standby Immediate command to the device.
3713 * This function is called by the Block Layer just before the
3714 * system powers off during a shutdown.
3716 * @dd Pointer to the driver data structure.
3721 static int mtip_hw_shutdown(struct driver_data *dd)
3724 * Send standby immediate (E0h) to the drive so that it
3727 if (!dd->sr && dd->port)
3728 mtip_standby_immediate(dd->port);
3736 * This function is called by the Block Layer just before the
3737 * system hibernates.
3739 * @dd Pointer to the driver data structure.
3742 * 0 Suspend was successful
3743 * -EFAULT Suspend was not successful
3745 static int mtip_hw_suspend(struct driver_data *dd)
3748 * Send standby immediate (E0h) to the drive
3749 * so that it saves its state.
3751 if (mtip_standby_immediate(dd->port) != 0) {
3752 dev_err(&dd->pdev->dev,
3753 "Failed standby-immediate command\n");
3757 /* Disable interrupts on the HBA.*/
3758 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3759 dd->mmio + HOST_CTL);
3760 mtip_deinit_port(dd->port);
3768 * This function is called by the Block Layer as the
3771 * @dd Pointer to the driver data structure.
3774 * 0 Resume was successful
3775 * -EFAULT Resume was not successful
3777 static int mtip_hw_resume(struct driver_data *dd)
3779 /* Perform any needed hardware setup steps */
3783 if (mtip_hba_reset(dd) != 0) {
3784 dev_err(&dd->pdev->dev,
3785 "Unable to reset the HBA\n");
3790 * Enable the port, DMA engine, and FIS reception specific
3791 * h/w in controller.
3793 mtip_init_port(dd->port);
3794 mtip_start_port(dd->port);
3796 /* Enable interrupts on the HBA.*/
3797 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3798 dd->mmio + HOST_CTL);
3804 * Helper function for reusing disk name
3805 * upon hot insertion.
3807 static int rssd_disk_name_format(char *prefix,
3812 const int base = 'z' - 'a' + 1;
3813 char *begin = buf + strlen(prefix);
3814 char *end = buf + buflen;
3824 *--p = 'a' + (index % unit);
3825 index = (index / unit) - 1;
3826 } while (index >= 0);
3828 memmove(begin, p, end - p);
3829 memcpy(buf, prefix, strlen(prefix));
3835 * Block layer IOCTL handler.
3837 * @dev Pointer to the block_device structure.
3839 * @cmd IOCTL command passed from the user application.
3840 * @arg Argument passed from the user application.
3843 * 0 IOCTL completed successfully.
3844 * -ENOTTY IOCTL not supported or invalid driver data
3845 * structure pointer.
3847 static int mtip_block_ioctl(struct block_device *dev,
3852 struct driver_data *dd = dev->bd_disk->private_data;
3854 if (!capable(CAP_SYS_ADMIN))
3860 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
3867 return mtip_hw_ioctl(dd, cmd, arg);
3871 #ifdef CONFIG_COMPAT
3873 * Block layer compat IOCTL handler.
3875 * @dev Pointer to the block_device structure.
3877 * @cmd IOCTL command passed from the user application.
3878 * @arg Argument passed from the user application.
3881 * 0 IOCTL completed successfully.
3882 * -ENOTTY IOCTL not supported or invalid driver data
3883 * structure pointer.
3885 static int mtip_block_compat_ioctl(struct block_device *dev,
3890 struct driver_data *dd = dev->bd_disk->private_data;
3892 if (!capable(CAP_SYS_ADMIN))
3898 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
3904 case HDIO_DRIVE_TASKFILE: {
3905 struct mtip_compat_ide_task_request_s __user *compat_req_task;
3906 ide_task_request_t req_task;
3907 int compat_tasksize, outtotal, ret;
3910 sizeof(struct mtip_compat_ide_task_request_s);
3913 (struct mtip_compat_ide_task_request_s __user *) arg;
3915 if (copy_from_user(&req_task, (void __user *) arg,
3916 compat_tasksize - (2 * sizeof(compat_long_t))))
3919 if (get_user(req_task.out_size, &compat_req_task->out_size))
3922 if (get_user(req_task.in_size, &compat_req_task->in_size))
3925 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3927 ret = exec_drive_taskfile(dd, (void __user *) arg,
3928 &req_task, outtotal);
3930 if (copy_to_user((void __user *) arg, &req_task,
3932 (2 * sizeof(compat_long_t))))
3935 if (put_user(req_task.out_size, &compat_req_task->out_size))
3938 if (put_user(req_task.in_size, &compat_req_task->in_size))
3944 return mtip_hw_ioctl(dd, cmd, arg);
3950 * Obtain the geometry of the device.
3952 * You may think that this function is obsolete, but some applications,
3953 * fdisk for example still used CHS values. This function describes the
3954 * device as having 224 heads and 56 sectors per cylinder. These values are
3955 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3956 * partition is described in terms of a start and end cylinder this means
3957 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3958 * affects performance.
3960 * @dev Pointer to the block_device strucutre.
3961 * @geo Pointer to a hd_geometry structure.
3964 * 0 Operation completed successfully.
3965 * -ENOTTY An error occurred while reading the drive capacity.
3967 static int mtip_block_getgeo(struct block_device *dev,
3968 struct hd_geometry *geo)
3970 struct driver_data *dd = dev->bd_disk->private_data;
3976 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3977 dev_warn(&dd->pdev->dev,
3978 "Could not get drive capacity.\n");
3984 sector_div(capacity, (geo->heads * geo->sectors));
3985 geo->cylinders = capacity;
3990 * Block device operation function.
3992 * This structure contains pointers to the functions required by the block
3995 static const struct block_device_operations mtip_block_ops = {
3996 .ioctl = mtip_block_ioctl,
3997 #ifdef CONFIG_COMPAT
3998 .compat_ioctl = mtip_block_compat_ioctl,
4000 .getgeo = mtip_block_getgeo,
4001 .owner = THIS_MODULE
4005 * Block layer make request function.
4007 * This function is called by the kernel to process a BIO for
4010 * @queue Pointer to the request queue. Unused other than to obtain
4011 * the driver data structure.
4012 * @bio Pointer to the BIO.
4015 static void mtip_make_request(struct request_queue *queue, struct bio *bio)
4017 struct driver_data *dd = queue->queuedata;
4018 struct scatterlist *sg;
4019 struct bio_vec bvec;
4020 struct bvec_iter iter;
4022 int tag = 0, unaligned = 0;
4024 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
4025 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
4027 bio_endio(bio, -ENXIO);
4030 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
4031 bio_endio(bio, -ENODATA);
4034 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
4036 bio_data_dir(bio))) {
4037 bio_endio(bio, -ENODATA);
4040 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))) {
4041 bio_endio(bio, -ENODATA);
4044 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)) {
4045 bio_endio(bio, -ENXIO);
4050 if (unlikely(bio->bi_rw & REQ_DISCARD)) {
4051 bio_endio(bio, mtip_send_trim(dd, bio->bi_iter.bi_sector,
4056 if (unlikely(!bio_has_data(bio))) {
4057 blk_queue_flush(queue, 0);
4062 if (bio_data_dir(bio) == WRITE && bio_sectors(bio) <= 64 &&
4064 if (bio->bi_iter.bi_sector % 8 != 0)
4065 /* Unaligned on 4k boundaries */
4067 else if (bio_sectors(bio) % 8 != 0) /* Aligned but not 4k/8k */
4071 sg = mtip_hw_get_scatterlist(dd, &tag, unaligned);
4072 if (likely(sg != NULL)) {
4073 blk_queue_bounce(queue, &bio);
4075 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
4076 dev_warn(&dd->pdev->dev,
4077 "Maximum number of SGL entries exceeded\n");
4079 mtip_hw_release_scatterlist(dd, tag, unaligned);
4083 /* Create the scatter list for this bio. */
4084 bio_for_each_segment(bvec, bio, iter) {
4085 sg_set_page(&sg[nents],
4092 /* Issue the read/write. */
4093 mtip_hw_submit_io(dd,
4094 bio->bi_iter.bi_sector,
4107 * Block layer initialization function.
4109 * This function is called once by the PCI layer for each P320
4110 * device that is connected to the system.
4112 * @dd Pointer to the driver data structure.
4115 * 0 on success else an error code.
4117 static int mtip_block_initialize(struct driver_data *dd)
4119 int rv = 0, wait_for_rebuild = 0;
4121 unsigned int index = 0;
4122 struct kobject *kobj;
4123 unsigned char thd_name[16];
4126 goto skip_create_disk; /* hw init done, before rebuild */
4128 /* Initialize the protocol layer. */
4129 wait_for_rebuild = mtip_hw_init(dd);
4130 if (wait_for_rebuild < 0) {
4131 dev_err(&dd->pdev->dev,
4132 "Protocol layer initialization failed\n");
4134 goto protocol_init_error;
4137 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
4138 if (dd->disk == NULL) {
4139 dev_err(&dd->pdev->dev,
4140 "Unable to allocate gendisk structure\n");
4142 goto alloc_disk_error;
4145 /* Generate the disk name, implemented same as in sd.c */
4147 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
4150 spin_lock(&rssd_index_lock);
4151 rv = ida_get_new(&rssd_index_ida, &index);
4152 spin_unlock(&rssd_index_lock);
4153 } while (rv == -EAGAIN);
4158 rv = rssd_disk_name_format("rssd",
4160 dd->disk->disk_name,
4163 goto disk_index_error;
4165 dd->disk->driverfs_dev = &dd->pdev->dev;
4166 dd->disk->major = dd->major;
4167 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
4168 dd->disk->fops = &mtip_block_ops;
4169 dd->disk->private_data = dd;
4172 mtip_hw_debugfs_init(dd);
4175 * if rebuild pending, start the service thread, and delay the block
4176 * queue creation and add_disk()
4178 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
4179 goto start_service_thread;
4182 /* Allocate the request queue. */
4183 dd->queue = blk_alloc_queue_node(GFP_KERNEL, dd->numa_node);
4184 if (dd->queue == NULL) {
4185 dev_err(&dd->pdev->dev,
4186 "Unable to allocate request queue\n");
4188 goto block_queue_alloc_init_error;
4191 /* Attach our request function to the request queue. */
4192 blk_queue_make_request(dd->queue, mtip_make_request);
4194 dd->disk->queue = dd->queue;
4195 dd->queue->queuedata = dd;
4197 /* Set device limits. */
4198 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
4199 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
4200 blk_queue_physical_block_size(dd->queue, 4096);
4201 blk_queue_max_hw_sectors(dd->queue, 0xffff);
4202 blk_queue_max_segment_size(dd->queue, 0x400000);
4203 blk_queue_io_min(dd->queue, 4096);
4206 * write back cache is not supported in the device. FUA depends on
4207 * write back cache support, hence setting flush support to zero.
4209 blk_queue_flush(dd->queue, 0);
4211 /* Signal trim support */
4212 if (dd->trim_supp == true) {
4213 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
4214 dd->queue->limits.discard_granularity = 4096;
4215 blk_queue_max_discard_sectors(dd->queue,
4216 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
4217 dd->queue->limits.discard_zeroes_data = 0;
4220 /* Set the capacity of the device in 512 byte sectors. */
4221 if (!(mtip_hw_get_capacity(dd, &capacity))) {
4222 dev_warn(&dd->pdev->dev,
4223 "Could not read drive capacity\n");
4225 goto read_capacity_error;
4227 set_capacity(dd->disk, capacity);
4229 /* Enable the block device and add it to /dev */
4232 dd->bdev = bdget_disk(dd->disk, 0);
4234 * Now that the disk is active, initialize any sysfs attributes
4235 * managed by the protocol layer.
4237 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4239 mtip_hw_sysfs_init(dd, kobj);
4243 if (dd->mtip_svc_handler) {
4244 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
4245 return rv; /* service thread created for handling rebuild */
4248 start_service_thread:
4249 sprintf(thd_name, "mtip_svc_thd_%02d", index);
4250 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
4251 dd, dd->numa_node, "%s",
4254 if (IS_ERR(dd->mtip_svc_handler)) {
4255 dev_err(&dd->pdev->dev, "service thread failed to start\n");
4256 dd->mtip_svc_handler = NULL;
4258 goto kthread_run_error;
4260 wake_up_process(dd->mtip_svc_handler);
4261 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
4262 rv = wait_for_rebuild;
4270 /* Delete our gendisk. This also removes the device from /dev */
4271 del_gendisk(dd->disk);
4273 read_capacity_error:
4274 blk_cleanup_queue(dd->queue);
4276 block_queue_alloc_init_error:
4277 mtip_hw_debugfs_exit(dd);
4279 spin_lock(&rssd_index_lock);
4280 ida_remove(&rssd_index_ida, index);
4281 spin_unlock(&rssd_index_lock);
4287 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
4289 protocol_init_error:
4294 * Block layer deinitialization function.
4296 * Called by the PCI layer as each P320 device is removed.
4298 * @dd Pointer to the driver data structure.
4303 static int mtip_block_remove(struct driver_data *dd)
4305 struct kobject *kobj;
4308 mtip_hw_debugfs_exit(dd);
4310 if (dd->mtip_svc_handler) {
4311 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4312 wake_up_interruptible(&dd->port->svc_wait);
4313 kthread_stop(dd->mtip_svc_handler);
4316 /* Clean up the sysfs attributes, if created */
4317 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4318 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4320 mtip_hw_sysfs_exit(dd, kobj);
4325 * Delete our gendisk structure. This also removes the device
4333 if (dd->disk->queue) {
4334 del_gendisk(dd->disk);
4335 blk_cleanup_queue(dd->queue);
4342 spin_lock(&rssd_index_lock);
4343 ida_remove(&rssd_index_ida, dd->index);
4344 spin_unlock(&rssd_index_lock);
4346 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
4347 dd->disk->disk_name);
4350 /* De-initialize the protocol layer. */
4357 * Function called by the PCI layer when just before the
4358 * machine shuts down.
4360 * If a protocol layer shutdown function is present it will be called
4363 * @dd Pointer to the driver data structure.
4368 static int mtip_block_shutdown(struct driver_data *dd)
4370 /* Delete our gendisk structure, and cleanup the blk queue. */
4372 dev_info(&dd->pdev->dev,
4373 "Shutting down %s ...\n", dd->disk->disk_name);
4375 if (dd->disk->queue) {
4376 del_gendisk(dd->disk);
4377 blk_cleanup_queue(dd->queue);
4384 spin_lock(&rssd_index_lock);
4385 ida_remove(&rssd_index_ida, dd->index);
4386 spin_unlock(&rssd_index_lock);
4388 mtip_hw_shutdown(dd);
4392 static int mtip_block_suspend(struct driver_data *dd)
4394 dev_info(&dd->pdev->dev,
4395 "Suspending %s ...\n", dd->disk->disk_name);
4396 mtip_hw_suspend(dd);
4400 static int mtip_block_resume(struct driver_data *dd)
4402 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4403 dd->disk->disk_name);
4408 static void drop_cpu(int cpu)
4413 static int get_least_used_cpu_on_node(int node)
4415 int cpu, least_used_cpu, least_cnt;
4416 const struct cpumask *node_mask;
4418 node_mask = cpumask_of_node(node);
4419 least_used_cpu = cpumask_first(node_mask);
4420 least_cnt = cpu_use[least_used_cpu];
4421 cpu = least_used_cpu;
4423 for_each_cpu(cpu, node_mask) {
4424 if (cpu_use[cpu] < least_cnt) {
4425 least_used_cpu = cpu;
4426 least_cnt = cpu_use[cpu];
4429 cpu_use[least_used_cpu]++;
4430 return least_used_cpu;
4433 /* Helper for selecting a node in round robin mode */
4434 static inline int mtip_get_next_rr_node(void)
4436 static int next_node = -1;
4438 if (next_node == -1) {
4439 next_node = first_online_node;
4443 next_node = next_online_node(next_node);
4444 if (next_node == MAX_NUMNODES)
4445 next_node = first_online_node;
4449 static DEFINE_HANDLER(0);
4450 static DEFINE_HANDLER(1);
4451 static DEFINE_HANDLER(2);
4452 static DEFINE_HANDLER(3);
4453 static DEFINE_HANDLER(4);
4454 static DEFINE_HANDLER(5);
4455 static DEFINE_HANDLER(6);
4456 static DEFINE_HANDLER(7);
4459 * Called for each supported PCI device detected.
4461 * This function allocates the private data structure, enables the
4462 * PCI device and then calls the block layer initialization function.
4465 * 0 on success else an error code.
4467 static int mtip_pci_probe(struct pci_dev *pdev,
4468 const struct pci_device_id *ent)
4471 struct driver_data *dd = NULL;
4473 const struct cpumask *node_mask;
4474 int cpu, i = 0, j = 0;
4475 int my_node = NUMA_NO_NODE;
4476 unsigned long flags;
4478 /* Allocate memory for this devices private data. */
4479 my_node = pcibus_to_node(pdev->bus);
4480 if (my_node != NUMA_NO_NODE) {
4481 if (!node_online(my_node))
4482 my_node = mtip_get_next_rr_node();
4484 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4485 my_node = mtip_get_next_rr_node();
4487 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4488 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
4489 cpu_to_node(smp_processor_id()), smp_processor_id());
4491 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
4494 "Unable to allocate memory for driver data\n");
4498 /* Attach the private data to this PCI device. */
4499 pci_set_drvdata(pdev, dd);
4501 rv = pcim_enable_device(pdev);
4503 dev_err(&pdev->dev, "Unable to enable device\n");
4507 /* Map BAR5 to memory. */
4508 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4510 dev_err(&pdev->dev, "Unable to map regions\n");
4514 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4515 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4518 rv = pci_set_consistent_dma_mask(pdev,
4521 dev_warn(&pdev->dev,
4522 "64-bit DMA enable failed\n");
4528 /* Copy the info we may need later into the private data structure. */
4529 dd->major = mtip_major;
4530 dd->instance = instance;
4532 dd->numa_node = my_node;
4534 INIT_LIST_HEAD(&dd->online_list);
4535 INIT_LIST_HEAD(&dd->remove_list);
4537 memset(dd->workq_name, 0, 32);
4538 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4540 dd->isr_workq = create_workqueue(dd->workq_name);
4541 if (!dd->isr_workq) {
4542 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
4544 goto block_initialize_err;
4547 memset(cpu_list, 0, sizeof(cpu_list));
4549 node_mask = cpumask_of_node(dd->numa_node);
4550 if (!cpumask_empty(node_mask)) {
4551 for_each_cpu(cpu, node_mask)
4553 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4554 j = strlen(cpu_list);
4557 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4559 topology_physical_package_id(cpumask_first(node_mask)),
4560 nr_cpus_node(dd->numa_node),
4563 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4565 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4566 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4567 cpu_to_node(dd->isr_binding), dd->isr_binding);
4569 /* first worker context always runs in ISR */
4570 dd->work[0].cpu_binding = dd->isr_binding;
4571 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4572 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4573 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4574 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4575 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4576 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4577 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4579 /* Log the bindings */
4580 for_each_present_cpu(cpu) {
4581 memset(cpu_list, 0, sizeof(cpu_list));
4582 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4583 if (dd->work[i].cpu_binding == cpu) {
4584 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4585 j = strlen(cpu_list);
4589 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4592 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4593 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4594 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4595 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4596 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4597 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4598 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4599 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4601 pci_set_master(pdev);
4602 rv = pci_enable_msi(pdev);
4604 dev_warn(&pdev->dev,
4605 "Unable to enable MSI interrupt.\n");
4606 goto block_initialize_err;
4609 /* Initialize the block layer. */
4610 rv = mtip_block_initialize(dd);
4613 "Unable to initialize block layer\n");
4614 goto block_initialize_err;
4618 * Increment the instance count so that each device has a unique
4622 if (rv != MTIP_FTL_REBUILD_MAGIC)
4623 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
4625 rv = 0; /* device in rebuild state, return 0 from probe */
4627 /* Add to online list even if in ftl rebuild */
4628 spin_lock_irqsave(&dev_lock, flags);
4629 list_add(&dd->online_list, &online_list);
4630 spin_unlock_irqrestore(&dev_lock, flags);
4634 block_initialize_err:
4635 pci_disable_msi(pdev);
4636 if (dd->isr_workq) {
4637 flush_workqueue(dd->isr_workq);
4638 destroy_workqueue(dd->isr_workq);
4639 drop_cpu(dd->work[0].cpu_binding);
4640 drop_cpu(dd->work[1].cpu_binding);
4641 drop_cpu(dd->work[2].cpu_binding);
4644 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4648 pci_set_drvdata(pdev, NULL);
4655 * Called for each probed device when the device is removed or the
4656 * driver is unloaded.
4661 static void mtip_pci_remove(struct pci_dev *pdev)
4663 struct driver_data *dd = pci_get_drvdata(pdev);
4664 unsigned long flags, to;
4666 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
4668 spin_lock_irqsave(&dev_lock, flags);
4669 list_del_init(&dd->online_list);
4670 list_add(&dd->remove_list, &removing_list);
4671 spin_unlock_irqrestore(&dev_lock, flags);
4673 mtip_check_surprise_removal(pdev);
4674 synchronize_irq(dd->pdev->irq);
4676 /* Spin until workers are done */
4677 to = jiffies + msecs_to_jiffies(4000);
4680 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4681 time_before(jiffies, to));
4683 if (atomic_read(&dd->irq_workers_active) != 0) {
4684 dev_warn(&dd->pdev->dev,
4685 "Completion workers still active!\n");
4687 /* Cleanup the outstanding commands */
4688 mtip_command_cleanup(dd);
4690 /* Clean up the block layer. */
4691 mtip_block_remove(dd);
4693 if (dd->isr_workq) {
4694 flush_workqueue(dd->isr_workq);
4695 destroy_workqueue(dd->isr_workq);
4696 drop_cpu(dd->work[0].cpu_binding);
4697 drop_cpu(dd->work[1].cpu_binding);
4698 drop_cpu(dd->work[2].cpu_binding);
4701 pci_disable_msi(pdev);
4703 spin_lock_irqsave(&dev_lock, flags);
4704 list_del_init(&dd->remove_list);
4705 spin_unlock_irqrestore(&dev_lock, flags);
4710 set_bit(MTIP_DDF_REMOVE_DONE_BIT, &dd->dd_flag);
4712 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4713 pci_set_drvdata(pdev, NULL);
4719 * Called for each probed device when the device is suspended.
4725 static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4728 struct driver_data *dd = pci_get_drvdata(pdev);
4732 "Driver private datastructure is NULL\n");
4736 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
4738 /* Disable ports & interrupts then send standby immediate */
4739 rv = mtip_block_suspend(dd);
4742 "Failed to suspend controller\n");
4747 * Save the pci config space to pdev structure &
4748 * disable the device
4750 pci_save_state(pdev);
4751 pci_disable_device(pdev);
4753 /* Move to Low power state*/
4754 pci_set_power_state(pdev, PCI_D3hot);
4760 * Called for each probed device when the device is resumed.
4766 static int mtip_pci_resume(struct pci_dev *pdev)
4769 struct driver_data *dd;
4771 dd = pci_get_drvdata(pdev);
4774 "Driver private datastructure is NULL\n");
4778 /* Move the device to active State */
4779 pci_set_power_state(pdev, PCI_D0);
4781 /* Restore PCI configuration space */
4782 pci_restore_state(pdev);
4784 /* Enable the PCI device*/
4785 rv = pcim_enable_device(pdev);
4788 "Failed to enable card during resume\n");
4791 pci_set_master(pdev);
4794 * Calls hbaReset, initPort, & startPort function
4795 * then enables interrupts
4797 rv = mtip_block_resume(dd);
4799 dev_err(&pdev->dev, "Unable to resume\n");
4802 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
4813 static void mtip_pci_shutdown(struct pci_dev *pdev)
4815 struct driver_data *dd = pci_get_drvdata(pdev);
4817 mtip_block_shutdown(dd);
4820 /* Table of device ids supported by this driver. */
4821 static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
4822 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4823 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4824 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4825 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4826 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4827 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4828 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
4832 /* Structure that describes the PCI driver functions. */
4833 static struct pci_driver mtip_pci_driver = {
4834 .name = MTIP_DRV_NAME,
4835 .id_table = mtip_pci_tbl,
4836 .probe = mtip_pci_probe,
4837 .remove = mtip_pci_remove,
4838 .suspend = mtip_pci_suspend,
4839 .resume = mtip_pci_resume,
4840 .shutdown = mtip_pci_shutdown,
4843 MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4846 * Module initialization function.
4848 * Called once when the module is loaded. This function allocates a major
4849 * block device number to the Cyclone devices and registers the PCI layer
4853 * 0 on success else error code.
4855 static int __init mtip_init(void)
4859 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
4861 spin_lock_init(&dev_lock);
4863 INIT_LIST_HEAD(&online_list);
4864 INIT_LIST_HEAD(&removing_list);
4866 /* Allocate a major block device number to use with this driver. */
4867 error = register_blkdev(0, MTIP_DRV_NAME);
4869 pr_err("Unable to register block device (%d)\n",
4875 dfs_parent = debugfs_create_dir("rssd", NULL);
4876 if (IS_ERR_OR_NULL(dfs_parent)) {
4877 pr_warn("Error creating debugfs parent\n");
4881 dfs_device_status = debugfs_create_file("device_status",
4882 S_IRUGO, dfs_parent, NULL,
4883 &mtip_device_status_fops);
4884 if (IS_ERR_OR_NULL(dfs_device_status)) {
4885 pr_err("Error creating device_status node\n");
4886 dfs_device_status = NULL;
4890 /* Register our PCI operations. */
4891 error = pci_register_driver(&mtip_pci_driver);
4893 debugfs_remove(dfs_parent);
4894 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4901 * Module de-initialization function.
4903 * Called once when the module is unloaded. This function deallocates
4904 * the major block device number allocated by mtip_init() and
4905 * unregisters the PCI layer of the driver.
4910 static void __exit mtip_exit(void)
4912 debugfs_remove_recursive(dfs_parent);
4914 /* Release the allocated major block device number. */
4915 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4917 /* Unregister the PCI driver. */
4918 pci_unregister_driver(&mtip_pci_driver);
4921 MODULE_AUTHOR("Micron Technology, Inc");
4922 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4923 MODULE_LICENSE("GPL");
4924 MODULE_VERSION(MTIP_DRV_VERSION);
4926 module_init(mtip_init);
4927 module_exit(mtip_exit);