2 * Driver for the Micron P320 SSD
3 * Copyright (C) 2011 Micron Technology, Inc.
5 * Portions of this code were derived from works subjected to the
7 * Copyright (C) 2009 Integrated Device Technology, Inc.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
21 #include <linux/pci.h>
22 #include <linux/interrupt.h>
23 #include <linux/ata.h>
24 #include <linux/delay.h>
25 #include <linux/hdreg.h>
26 #include <linux/uaccess.h>
27 #include <linux/random.h>
28 #include <linux/smp.h>
29 #include <linux/compat.h>
31 #include <linux/module.h>
32 #include <linux/genhd.h>
33 #include <linux/blkdev.h>
34 #include <linux/bio.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/idr.h>
37 #include <linux/kthread.h>
38 #include <../drivers/ata/ahci.h>
39 #include <linux/export.h>
40 #include <linux/debugfs.h>
43 #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
44 #define HW_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (MTIP_MAX_SG * 16))
45 #define HW_CMD_TBL_AR_SZ (HW_CMD_TBL_SZ * MTIP_MAX_COMMAND_SLOTS)
46 #define HW_PORT_PRIV_DMA_SZ \
47 (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
49 #define HOST_CAP_NZDMA (1 << 19)
50 #define HOST_HSORG 0xFC
51 #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
52 #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
53 #define HSORG_HWREV 0xFF00
54 #define HSORG_STYLE 0x8
55 #define HSORG_SLOTGROUPS 0x7
57 #define PORT_COMMAND_ISSUE 0x38
58 #define PORT_SDBV 0x7C
60 #define PORT_OFFSET 0x100
61 #define PORT_MEM_SIZE 0x80
63 #define PORT_IRQ_ERR \
64 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
65 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
66 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
68 #define PORT_IRQ_LEGACY \
69 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
70 #define PORT_IRQ_HANDLED \
71 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
72 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
73 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
74 #define DEF_PORT_IRQ \
75 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
78 #define MTIP_PRODUCT_UNKNOWN 0x00
79 #define MTIP_PRODUCT_ASICFPGA 0x11
81 /* Device instance number, incremented each time a device is probed. */
85 * Global variable used to hold the major block device number
86 * allocated in mtip_init().
88 static int mtip_major;
89 static struct dentry *dfs_parent;
91 static u32 cpu_use[NR_CPUS];
93 static DEFINE_SPINLOCK(rssd_index_lock);
94 static DEFINE_IDA(rssd_index_ida);
96 static int mtip_block_initialize(struct driver_data *dd);
99 struct mtip_compat_ide_task_request_s {
102 ide_reg_valid_t out_flags;
103 ide_reg_valid_t in_flags;
106 compat_ulong_t out_size;
107 compat_ulong_t in_size;
112 * This function check_for_surprise_removal is called
113 * while card is removed from the system and it will
114 * read the vendor id from the configration space
116 * @pdev Pointer to the pci_dev structure.
119 * true if device removed, else false
121 static bool mtip_check_surprise_removal(struct pci_dev *pdev)
125 /* Read the vendorID from the configuration space */
126 pci_read_config_word(pdev, 0x00, &vendor_id);
127 if (vendor_id == 0xFFFF)
128 return true; /* device removed */
130 return false; /* device present */
134 * This function is called for clean the pending command in the
135 * command slot during the surprise removal of device and return
136 * error to the upper layer.
138 * @dd Pointer to the DRIVER_DATA structure.
143 static void mtip_command_cleanup(struct driver_data *dd)
145 int group = 0, commandslot = 0, commandindex = 0;
146 struct mtip_cmd *command;
147 struct mtip_port *port = dd->port;
148 static int in_progress;
155 for (group = 0; group < 4; group++) {
156 for (commandslot = 0; commandslot < 32; commandslot++) {
157 if (!(port->allocated[group] & (1 << commandslot)))
160 commandindex = group << 5 | commandslot;
161 command = &port->commands[commandindex];
163 if (atomic_read(&command->active)
164 && (command->async_callback)) {
165 command->async_callback(command->async_data,
167 command->async_callback = NULL;
168 command->async_data = NULL;
171 dma_unmap_sg(&port->dd->pdev->dev,
173 command->scatter_ents,
180 set_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag);
185 * Obtain an empty command slot.
187 * This function needs to be reentrant since it could be called
188 * at the same time on multiple CPUs. The allocation of the
189 * command slot must be atomic.
191 * @port Pointer to the port data structure.
194 * >= 0 Index of command slot obtained.
195 * -1 No command slots available.
197 static int get_slot(struct mtip_port *port)
200 unsigned int num_command_slots = port->dd->slot_groups * 32;
203 * Try 10 times, because there is a small race here.
204 * that's ok, because it's still cheaper than a lock.
206 * Race: Since this section is not protected by lock, same bit
207 * could be chosen by different process contexts running in
208 * different processor. So instead of costly lock, we are going
211 for (i = 0; i < 10; i++) {
212 slot = find_next_zero_bit(port->allocated,
213 num_command_slots, 1);
214 if ((slot < num_command_slots) &&
215 (!test_and_set_bit(slot, port->allocated)))
218 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
220 if (mtip_check_surprise_removal(port->dd->pdev)) {
221 /* Device not present, clean outstanding commands */
222 mtip_command_cleanup(port->dd);
228 * Release a command slot.
230 * @port Pointer to the port data structure.
231 * @tag Tag of command to release
236 static inline void release_slot(struct mtip_port *port, int tag)
238 smp_mb__before_clear_bit();
239 clear_bit(tag, port->allocated);
240 smp_mb__after_clear_bit();
244 * Reset the HBA (without sleeping)
246 * Just like hba_reset, except does not call sleep, so can be
247 * run from interrupt/tasklet context.
249 * @dd Pointer to the driver data structure.
252 * 0 The reset was successful.
253 * -1 The HBA Reset bit did not clear.
255 static int hba_reset_nosleep(struct driver_data *dd)
257 unsigned long timeout;
259 /* Chip quirk: quiesce any chip function */
262 /* Set the reset bit */
263 writel(HOST_RESET, dd->mmio + HOST_CTL);
266 readl(dd->mmio + HOST_CTL);
269 * Wait 10ms then spin for up to 1 second
270 * waiting for reset acknowledgement
272 timeout = jiffies + msecs_to_jiffies(1000);
274 while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
275 && time_before(jiffies, timeout))
278 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
281 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
288 * Issue a command to the hardware.
290 * Set the appropriate bit in the s_active and Command Issue hardware
291 * registers, causing hardware command processing to begin.
293 * @port Pointer to the port structure.
294 * @tag The tag of the command to be issued.
299 static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
301 int group = tag >> 5;
303 atomic_set(&port->commands[tag].active, 1);
305 /* guard SACT and CI registers */
306 spin_lock(&port->cmd_issue_lock[group]);
307 writel((1 << MTIP_TAG_BIT(tag)),
308 port->s_active[MTIP_TAG_INDEX(tag)]);
309 writel((1 << MTIP_TAG_BIT(tag)),
310 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
311 spin_unlock(&port->cmd_issue_lock[group]);
313 /* Set the command's timeout value.*/
314 port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
315 MTIP_NCQ_COMMAND_TIMEOUT_MS);
319 * Enable/disable the reception of FIS
321 * @port Pointer to the port data structure
322 * @enable 1 to enable, 0 to disable
325 * Previous state: 1 enabled, 0 disabled
327 static int mtip_enable_fis(struct mtip_port *port, int enable)
331 /* enable FIS reception */
332 tmp = readl(port->mmio + PORT_CMD);
334 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
336 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
339 readl(port->mmio + PORT_CMD);
341 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
345 * Enable/disable the DMA engine
347 * @port Pointer to the port data structure
348 * @enable 1 to enable, 0 to disable
351 * Previous state: 1 enabled, 0 disabled.
353 static int mtip_enable_engine(struct mtip_port *port, int enable)
357 /* enable FIS reception */
358 tmp = readl(port->mmio + PORT_CMD);
360 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
362 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
364 readl(port->mmio + PORT_CMD);
365 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
369 * Enables the port DMA engine and FIS reception.
374 static inline void mtip_start_port(struct mtip_port *port)
376 /* Enable FIS reception */
377 mtip_enable_fis(port, 1);
379 /* Enable the DMA engine */
380 mtip_enable_engine(port, 1);
384 * Deinitialize a port by disabling port interrupts, the DMA engine,
387 * @port Pointer to the port structure
392 static inline void mtip_deinit_port(struct mtip_port *port)
394 /* Disable interrupts on this port */
395 writel(0, port->mmio + PORT_IRQ_MASK);
397 /* Disable the DMA engine */
398 mtip_enable_engine(port, 0);
400 /* Disable FIS reception */
401 mtip_enable_fis(port, 0);
407 * This function deinitializes the port by calling mtip_deinit_port() and
408 * then initializes it by setting the command header and RX FIS addresses,
409 * clearing the SError register and any pending port interrupts before
410 * re-enabling the default set of port interrupts.
412 * @port Pointer to the port structure.
417 static void mtip_init_port(struct mtip_port *port)
420 mtip_deinit_port(port);
422 /* Program the command list base and FIS base addresses */
423 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
424 writel((port->command_list_dma >> 16) >> 16,
425 port->mmio + PORT_LST_ADDR_HI);
426 writel((port->rxfis_dma >> 16) >> 16,
427 port->mmio + PORT_FIS_ADDR_HI);
430 writel(port->command_list_dma & 0xFFFFFFFF,
431 port->mmio + PORT_LST_ADDR);
432 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
435 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
437 /* reset the completed registers.*/
438 for (i = 0; i < port->dd->slot_groups; i++)
439 writel(0xFFFFFFFF, port->completed[i]);
441 /* Clear any pending interrupts for this port */
442 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
444 /* Clear any pending interrupts on the HBA. */
445 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
446 port->dd->mmio + HOST_IRQ_STAT);
448 /* Enable port interrupts */
449 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
455 * @port Pointer to the port data structure.
460 static void mtip_restart_port(struct mtip_port *port)
462 unsigned long timeout;
464 /* Disable the DMA engine */
465 mtip_enable_engine(port, 0);
467 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
468 timeout = jiffies + msecs_to_jiffies(500);
469 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
470 && time_before(jiffies, timeout))
473 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
477 * Chip quirk: escalate to hba reset if
478 * PxCMD.CR not clear after 500 ms
480 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
481 dev_warn(&port->dd->pdev->dev,
482 "PxCMD.CR not clear, escalating reset\n");
484 if (hba_reset_nosleep(port->dd))
485 dev_err(&port->dd->pdev->dev,
486 "HBA reset escalation failed.\n");
488 /* 30 ms delay before com reset to quiesce chip */
492 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
495 writel(readl(port->mmio + PORT_SCR_CTL) |
496 1, port->mmio + PORT_SCR_CTL);
497 readl(port->mmio + PORT_SCR_CTL);
499 /* Wait 1 ms to quiesce chip function */
500 timeout = jiffies + msecs_to_jiffies(1);
501 while (time_before(jiffies, timeout))
504 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
507 /* Clear PxSCTL.DET */
508 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
509 port->mmio + PORT_SCR_CTL);
510 readl(port->mmio + PORT_SCR_CTL);
512 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
513 timeout = jiffies + msecs_to_jiffies(500);
514 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
515 && time_before(jiffies, timeout))
518 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
521 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
522 dev_warn(&port->dd->pdev->dev,
523 "COM reset failed\n");
525 mtip_init_port(port);
526 mtip_start_port(port);
531 * Helper function for tag logging
533 static void print_tags(struct driver_data *dd,
535 unsigned long *tagbits,
538 unsigned char tagmap[128];
539 int group, tagmap_len = 0;
541 memset(tagmap, 0, sizeof(tagmap));
542 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
543 tagmap_len = sprintf(tagmap + tagmap_len, "%016lX ",
545 dev_warn(&dd->pdev->dev,
546 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
550 * Called periodically to see if any read/write commands are
551 * taking too long to complete.
553 * @data Pointer to the PORT data structure.
558 static void mtip_timeout_function(unsigned long int data)
560 struct mtip_port *port = (struct mtip_port *) data;
561 struct host_to_dev_fis *fis;
562 struct mtip_cmd *command;
563 int tag, cmdto_cnt = 0;
564 unsigned int bit, group;
565 unsigned int num_command_slots;
566 unsigned long to, tagaccum[SLOTBITS_IN_LONGS];
571 if (test_bit(MTIP_DDF_RESUME_BIT, &port->dd->dd_flag)) {
572 mod_timer(&port->cmd_timer,
573 jiffies + msecs_to_jiffies(30000));
576 /* clear the tag accumulator */
577 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
578 num_command_slots = port->dd->slot_groups * 32;
580 for (tag = 0; tag < num_command_slots; tag++) {
582 * Skip internal command slot as it has
583 * its own timeout mechanism
585 if (tag == MTIP_TAG_INTERNAL)
588 if (atomic_read(&port->commands[tag].active) &&
589 (time_after(jiffies, port->commands[tag].comp_time))) {
593 command = &port->commands[tag];
594 fis = (struct host_to_dev_fis *) command->command;
596 set_bit(tag, tagaccum);
599 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
602 * Clear the completed bit. This should prevent
603 * any interrupt handlers from trying to retire
606 writel(1 << bit, port->completed[group]);
608 /* Call the async completion callback. */
609 if (likely(command->async_callback))
610 command->async_callback(command->async_data,
612 command->async_callback = NULL;
613 command->comp_func = NULL;
615 /* Unmap the DMA scatter list entries */
616 dma_unmap_sg(&port->dd->pdev->dev,
618 command->scatter_ents,
622 * Clear the allocated bit and active tag for the
625 atomic_set(&port->commands[tag].active, 0);
626 release_slot(port, tag);
633 print_tags(port->dd, "timed out", tagaccum, cmdto_cnt);
634 if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
635 mtip_restart_port(port);
636 wake_up_interruptible(&port->svc_wait);
638 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
641 if (port->ic_pause_timer) {
642 to = port->ic_pause_timer + msecs_to_jiffies(1000);
643 if (time_after(jiffies, to)) {
644 if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
645 port->ic_pause_timer = 0;
646 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
647 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
648 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
649 wake_up_interruptible(&port->svc_wait);
656 /* Restart the timer */
657 mod_timer(&port->cmd_timer,
658 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
662 * IO completion function.
664 * This completion function is called by the driver ISR when a
665 * command that was issued by the kernel completes. It first calls the
666 * asynchronous completion function which normally calls back into the block
667 * layer passing the asynchronous callback data, then unmaps the
668 * scatter list associated with the completed command, and finally
669 * clears the allocated bit associated with the completed command.
671 * @port Pointer to the port data structure.
672 * @tag Tag of the command.
673 * @data Pointer to driver_data.
674 * @status Completion status.
679 static void mtip_async_complete(struct mtip_port *port,
684 struct mtip_cmd *command;
685 struct driver_data *dd = data;
686 int cb_status = status ? -EIO : 0;
688 if (unlikely(!dd) || unlikely(!port))
691 command = &port->commands[tag];
693 if (unlikely(status == PORT_IRQ_TF_ERR)) {
694 dev_warn(&port->dd->pdev->dev,
695 "Command tag %d failed due to TFE\n", tag);
698 /* Upper layer callback */
699 if (likely(command->async_callback))
700 command->async_callback(command->async_data, cb_status);
702 command->async_callback = NULL;
703 command->comp_func = NULL;
705 /* Unmap the DMA scatter list entries */
706 dma_unmap_sg(&dd->pdev->dev,
708 command->scatter_ents,
711 /* Clear the allocated and active bits for the command */
712 atomic_set(&port->commands[tag].active, 0);
713 release_slot(port, tag);
719 * Internal command completion callback function.
721 * This function is normally called by the driver ISR when an internal
722 * command completed. This function signals the command completion by
723 * calling complete().
725 * @port Pointer to the port data structure.
726 * @tag Tag of the command that has completed.
727 * @data Pointer to a completion structure.
728 * @status Completion status.
733 static void mtip_completion(struct mtip_port *port,
738 struct mtip_cmd *command = &port->commands[tag];
739 struct completion *waiting = data;
740 if (unlikely(status == PORT_IRQ_TF_ERR))
741 dev_warn(&port->dd->pdev->dev,
742 "Internal command %d completed with TFE\n", tag);
744 command->async_callback = NULL;
745 command->comp_func = NULL;
750 static void mtip_null_completion(struct mtip_port *port,
758 static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
759 dma_addr_t buffer_dma, unsigned int sectors);
760 static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
761 struct smart_attr *attrib);
765 * @dd Pointer to the DRIVER_DATA structure.
770 static void mtip_handle_tfe(struct driver_data *dd)
772 int group, tag, bit, reissue, rv;
773 struct mtip_port *port;
774 struct mtip_cmd *cmd;
776 struct host_to_dev_fis *fis;
777 unsigned long tagaccum[SLOTBITS_IN_LONGS];
778 unsigned int cmd_cnt = 0;
780 char *fail_reason = NULL;
781 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
783 dev_warn(&dd->pdev->dev, "Taskfile error\n");
787 /* Stop the timer to prevent command timeouts. */
788 del_timer(&port->cmd_timer);
789 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
791 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
792 test_bit(MTIP_TAG_INTERNAL, port->allocated)) {
793 cmd = &port->commands[MTIP_TAG_INTERNAL];
794 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
796 atomic_inc(&cmd->active); /* active > 1 indicates error */
797 if (cmd->comp_data && cmd->comp_func) {
798 cmd->comp_func(port, MTIP_TAG_INTERNAL,
799 cmd->comp_data, PORT_IRQ_TF_ERR);
801 goto handle_tfe_exit;
804 /* clear the tag accumulator */
805 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
807 /* Loop through all the groups */
808 for (group = 0; group < dd->slot_groups; group++) {
809 completed = readl(port->completed[group]);
811 /* clear completed status register in the hardware.*/
812 writel(completed, port->completed[group]);
814 /* Process successfully completed commands */
815 for (bit = 0; bit < 32 && completed; bit++) {
816 if (!(completed & (1<<bit)))
818 tag = (group << 5) + bit;
820 /* Skip the internal command slot */
821 if (tag == MTIP_TAG_INTERNAL)
824 cmd = &port->commands[tag];
825 if (likely(cmd->comp_func)) {
826 set_bit(tag, tagaccum);
828 atomic_set(&cmd->active, 0);
834 dev_err(&port->dd->pdev->dev,
835 "Missing completion func for tag %d",
837 if (mtip_check_surprise_removal(dd->pdev)) {
838 mtip_command_cleanup(dd);
839 /* don't proceed further */
846 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
848 /* Restart the port */
850 mtip_restart_port(port);
852 /* Trying to determine the cause of the error */
853 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
855 dd->port->log_buf_dma, 1);
857 dev_warn(&dd->pdev->dev,
858 "Error in READ LOG EXT (10h) command\n");
859 /* non-critical error, don't fail the load */
861 buf = (unsigned char *)dd->port->log_buf;
862 if (buf[259] & 0x1) {
863 dev_info(&dd->pdev->dev,
864 "Write protect bit is set.\n");
865 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
866 fail_all_ncq_write = 1;
867 fail_reason = "write protect";
869 if (buf[288] == 0xF7) {
870 dev_info(&dd->pdev->dev,
871 "Exceeded Tmax, drive in thermal shutdown.\n");
872 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
873 fail_all_ncq_cmds = 1;
874 fail_reason = "thermal shutdown";
876 if (buf[288] == 0xBF) {
877 dev_info(&dd->pdev->dev,
878 "Drive indicates rebuild has failed.\n");
879 fail_all_ncq_cmds = 1;
880 fail_reason = "rebuild failed";
884 /* clear the tag accumulator */
885 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
887 /* Loop through all the groups */
888 for (group = 0; group < dd->slot_groups; group++) {
889 for (bit = 0; bit < 32; bit++) {
891 tag = (group << 5) + bit;
892 cmd = &port->commands[tag];
894 /* If the active bit is set re-issue the command */
895 if (atomic_read(&cmd->active) == 0)
898 fis = (struct host_to_dev_fis *)cmd->command;
900 /* Should re-issue? */
901 if (tag == MTIP_TAG_INTERNAL ||
902 fis->command == ATA_CMD_SET_FEATURES)
905 if (fail_all_ncq_cmds ||
906 (fail_all_ncq_write &&
907 fis->command == ATA_CMD_FPDMA_WRITE)) {
908 dev_warn(&dd->pdev->dev,
909 " Fail: %s w/tag %d [%s].\n",
910 fis->command == ATA_CMD_FPDMA_WRITE ?
913 fail_reason != NULL ?
914 fail_reason : "unknown");
915 atomic_set(&cmd->active, 0);
916 if (cmd->comp_func) {
917 cmd->comp_func(port, tag,
926 * First check if this command has
927 * exceeded its retries.
929 if (reissue && (cmd->retries-- > 0)) {
931 set_bit(tag, tagaccum);
933 /* Re-issue the command. */
934 mtip_issue_ncq_command(port, tag);
939 /* Retire a command that will not be reissued */
940 dev_warn(&port->dd->pdev->dev,
941 "retiring tag %d\n", tag);
942 atomic_set(&cmd->active, 0);
951 dev_warn(&port->dd->pdev->dev,
952 "Bad completion for tag %d\n",
956 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
959 /* clear eh_active */
960 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
961 wake_up_interruptible(&port->svc_wait);
963 mod_timer(&port->cmd_timer,
964 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
968 * Handle a set device bits interrupt
970 static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
973 struct driver_data *dd = port->dd;
975 struct mtip_cmd *command;
978 WARN_ON_ONCE(!completed);
981 /* clear completed status register in the hardware.*/
982 writel(completed, port->completed[group]);
984 /* Process completed commands. */
985 for (bit = 0; (bit < 32) && completed; bit++) {
986 if (completed & 0x01) {
987 tag = (group << 5) | bit;
989 /* skip internal command slot. */
990 if (unlikely(tag == MTIP_TAG_INTERNAL))
993 command = &port->commands[tag];
994 /* make internal callback */
995 if (likely(command->comp_func)) {
1002 dev_warn(&dd->pdev->dev,
1007 if (mtip_check_surprise_removal(
1009 mtip_command_cleanup(dd);
1017 /* If last, re-enable interrupts */
1018 if (atomic_dec_return(&dd->irq_workers_active) == 0)
1019 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
1023 * Process legacy pio and d2h interrupts
1025 static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
1027 struct mtip_port *port = dd->port;
1028 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
1030 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
1031 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1032 & (1 << MTIP_TAG_INTERNAL))) {
1033 if (cmd->comp_func) {
1034 cmd->comp_func(port,
1046 * Demux and handle errors
1048 static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
1050 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
1051 mtip_handle_tfe(dd);
1053 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
1054 dev_warn(&dd->pdev->dev,
1055 "Clearing PxSERR.DIAG.x\n");
1056 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
1059 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
1060 dev_warn(&dd->pdev->dev,
1061 "Clearing PxSERR.DIAG.n\n");
1062 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
1065 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
1066 dev_warn(&dd->pdev->dev,
1067 "Port stat errors %x unhandled\n",
1068 (port_stat & ~PORT_IRQ_HANDLED));
1072 static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
1074 struct driver_data *dd = (struct driver_data *) data;
1075 struct mtip_port *port = dd->port;
1076 u32 hba_stat, port_stat;
1078 int do_irq_enable = 1, i, workers;
1079 struct mtip_work *twork;
1081 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1085 /* Acknowledge the interrupt status on the port.*/
1086 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1087 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1089 /* Demux port status */
1090 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
1092 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
1094 /* Start at 1: group zero is always local? */
1095 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
1097 twork = &dd->work[i];
1098 twork->completed = readl(port->completed[i]);
1099 if (twork->completed)
1103 atomic_set(&dd->irq_workers_active, workers);
1105 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
1106 twork = &dd->work[i];
1107 if (twork->completed)
1114 if (likely(dd->work[0].completed))
1115 mtip_workq_sdbfx(port, 0,
1116 dd->work[0].completed);
1120 * Chip quirk: SDB interrupt but nothing
1127 if (unlikely(port_stat & PORT_IRQ_ERR)) {
1128 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
1129 mtip_command_cleanup(dd);
1130 /* don't proceed further */
1133 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1137 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
1140 if (unlikely(port_stat & PORT_IRQ_LEGACY))
1141 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
1144 /* acknowledge interrupt */
1145 if (unlikely(do_irq_enable))
1146 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
1152 * HBA interrupt subroutine.
1155 * @instance Pointer to the driver data structure.
1158 * IRQ_HANDLED A HBA interrupt was pending and handled.
1159 * IRQ_NONE This interrupt was not for the HBA.
1161 static irqreturn_t mtip_irq_handler(int irq, void *instance)
1163 struct driver_data *dd = instance;
1165 return mtip_handle_irq(dd);
1168 static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1170 atomic_set(&port->commands[tag].active, 1);
1171 writel(1 << MTIP_TAG_BIT(tag),
1172 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1175 static bool mtip_pause_ncq(struct mtip_port *port,
1176 struct host_to_dev_fis *fis)
1178 struct host_to_dev_fis *reply;
1179 unsigned long task_file_data;
1181 reply = port->rxfis + RX_FIS_D2H_REG;
1182 task_file_data = readl(port->mmio+PORT_TFDATA);
1184 if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
1185 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1187 if ((task_file_data & 1))
1190 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
1191 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1192 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1193 port->ic_pause_timer = jiffies;
1195 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1196 (fis->features == 0x03)) {
1197 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1198 port->ic_pause_timer = jiffies;
1200 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1201 ((fis->command == 0xFC) &&
1202 (fis->features == 0x27 || fis->features == 0x72 ||
1203 fis->features == 0x62 || fis->features == 0x26))) {
1204 /* Com reset after secure erase or lowlevel format */
1205 mtip_restart_port(port);
1213 * Wait for port to quiesce
1215 * @port Pointer to port data structure
1216 * @timeout Max duration to wait (ms)
1220 * -EBUSY Commands still active
1222 static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1226 unsigned int active = 1;
1228 to = jiffies + msecs_to_jiffies(timeout);
1230 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1231 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
1233 continue; /* svc thd is actively issuing commands */
1235 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
1238 * Ignore s_active bit 0 of array element 0.
1239 * This bit will always be set
1241 active = readl(port->s_active[0]) & 0xFFFFFFFE;
1242 for (n = 1; n < port->dd->slot_groups; n++)
1243 active |= readl(port->s_active[n]);
1249 } while (time_before(jiffies, to));
1251 return active ? -EBUSY : 0;
1255 * Execute an internal command and wait for the completion.
1257 * @port Pointer to the port data structure.
1258 * @fis Pointer to the FIS that describes the command.
1259 * @fis_len Length in WORDS of the FIS.
1260 * @buffer DMA accessible for command data.
1261 * @buf_len Length, in bytes, of the data buffer.
1262 * @opts Command header options, excluding the FIS length
1263 * and the number of PRD entries.
1264 * @timeout Time in ms to wait for the command to complete.
1267 * 0 Command completed successfully.
1268 * -EFAULT The buffer address is not correctly aligned.
1269 * -EBUSY Internal command or other IO in progress.
1270 * -EAGAIN Time out waiting for command to complete.
1272 static int mtip_exec_internal_command(struct mtip_port *port,
1273 struct host_to_dev_fis *fis,
1279 unsigned long timeout)
1281 struct mtip_cmd_sg *command_sg;
1282 DECLARE_COMPLETION_ONSTACK(wait);
1283 int rv = 0, ready2go = 1;
1284 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
1287 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1288 if (buffer & 0x00000007) {
1289 dev_err(&port->dd->pdev->dev,
1290 "SG buffer is not 8 byte aligned\n");
1294 to = jiffies + msecs_to_jiffies(timeout);
1296 ready2go = !test_and_set_bit(MTIP_TAG_INTERNAL,
1301 } while (time_before(jiffies, to));
1303 dev_warn(&port->dd->pdev->dev,
1304 "Internal cmd active. new cmd [%02X]\n", fis->command);
1307 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1308 port->ic_pause_timer = 0;
1310 if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
1311 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1312 else if (fis->command == ATA_CMD_DOWNLOAD_MICRO)
1313 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1315 if (atomic == GFP_KERNEL) {
1316 if (fis->command != ATA_CMD_STANDBYNOW1) {
1317 /* wait for io to complete if non atomic */
1318 if (mtip_quiesce_io(port, 5000) < 0) {
1319 dev_warn(&port->dd->pdev->dev,
1320 "Failed to quiesce IO\n");
1321 release_slot(port, MTIP_TAG_INTERNAL);
1322 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1323 wake_up_interruptible(&port->svc_wait);
1328 /* Set the completion function and data for the command. */
1329 int_cmd->comp_data = &wait;
1330 int_cmd->comp_func = mtip_completion;
1333 /* Clear completion - we're going to poll */
1334 int_cmd->comp_data = NULL;
1335 int_cmd->comp_func = mtip_null_completion;
1338 /* Copy the command to the command table */
1339 memcpy(int_cmd->command, fis, fis_len*4);
1341 /* Populate the SG list */
1342 int_cmd->command_header->opts =
1343 __force_bit2int cpu_to_le32(opts | fis_len);
1345 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1348 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1350 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1351 command_sg->dba_upper =
1352 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
1354 int_cmd->command_header->opts |=
1355 __force_bit2int cpu_to_le32((1 << 16));
1358 /* Populate the command header */
1359 int_cmd->command_header->byte_count = 0;
1361 /* Issue the command to the hardware */
1362 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1364 /* Poll if atomic, wait_for_completion otherwise */
1365 if (atomic == GFP_KERNEL) {
1366 /* Wait for the command to complete or timeout. */
1367 if (wait_for_completion_timeout(
1369 msecs_to_jiffies(timeout)) == 0) {
1370 dev_err(&port->dd->pdev->dev,
1371 "Internal command did not complete [%d] "
1372 "within timeout of %lu ms\n",
1374 if (mtip_check_surprise_removal(port->dd->pdev) ||
1375 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1376 &port->dd->dd_flag)) {
1383 /* Spin for <timeout> checking if command still outstanding */
1384 timeout = jiffies + msecs_to_jiffies(timeout);
1385 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1386 & (1 << MTIP_TAG_INTERNAL))
1387 && time_before(jiffies, timeout)) {
1388 if (mtip_check_surprise_removal(port->dd->pdev)) {
1392 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
1393 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1394 &port->dd->dd_flag)) {
1398 if (readl(port->mmio + PORT_IRQ_STAT) & PORT_IRQ_ERR) {
1399 atomic_inc(&int_cmd->active); /* error */
1405 if (atomic_read(&int_cmd->active) > 1) {
1406 dev_err(&port->dd->pdev->dev,
1407 "Internal command [%02X] failed\n", fis->command);
1410 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1411 & (1 << MTIP_TAG_INTERNAL)) {
1413 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1414 &port->dd->dd_flag)) {
1415 mtip_restart_port(port);
1420 /* Clear the allocated and active bits for the internal command. */
1421 atomic_set(&int_cmd->active, 0);
1422 release_slot(port, MTIP_TAG_INTERNAL);
1423 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1427 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1428 wake_up_interruptible(&port->svc_wait);
1434 * Byte-swap ATA ID strings.
1436 * ATA identify data contains strings in byte-swapped 16-bit words.
1437 * They must be swapped (on all architectures) to be usable as C strings.
1438 * This function swaps bytes in-place.
1440 * @buf The buffer location of the string
1441 * @len The number of bytes to swap
1446 static inline void ata_swap_string(u16 *buf, unsigned int len)
1449 for (i = 0; i < (len/2); i++)
1450 be16_to_cpus(&buf[i]);
1454 * Request the device identity information.
1456 * If a user space buffer is not specified, i.e. is NULL, the
1457 * identify information is still read from the drive and placed
1458 * into the identify data buffer (@e port->identify) in the
1459 * port data structure.
1460 * When the identify buffer contains valid identify information @e
1461 * port->identify_valid is non-zero.
1463 * @port Pointer to the port structure.
1464 * @user_buffer A user space buffer where the identify data should be
1468 * 0 Command completed successfully.
1469 * -EFAULT An error occurred while coping data to the user buffer.
1470 * -1 Command failed.
1472 static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1475 struct host_to_dev_fis fis;
1477 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
1480 /* Build the FIS. */
1481 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1484 fis.command = ATA_CMD_ID_ATA;
1486 /* Set the identify information as invalid. */
1487 port->identify_valid = 0;
1489 /* Clear the identify information. */
1490 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1492 /* Execute the command. */
1493 if (mtip_exec_internal_command(port,
1497 sizeof(u16) * ATA_ID_WORDS,
1500 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1507 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1508 * perform field-sensitive swapping on the string fields.
1509 * See the kernel use of ata_id_string() for proof of this.
1511 #ifdef __LITTLE_ENDIAN
1512 ata_swap_string(port->identify + 27, 40); /* model string*/
1513 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1514 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1518 for (i = 0; i < ATA_ID_WORDS; i++)
1519 port->identify[i] = le16_to_cpu(port->identify[i]);
1523 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1524 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1525 port->dd->trim_supp = true;
1527 port->dd->trim_supp = false;
1529 /* Set the identify buffer as valid. */
1530 port->identify_valid = 1;
1536 ATA_ID_WORDS * sizeof(u16))) {
1547 * Issue a standby immediate command to the device.
1549 * @port Pointer to the port structure.
1552 * 0 Command was executed successfully.
1553 * -1 An error occurred while executing the command.
1555 static int mtip_standby_immediate(struct mtip_port *port)
1558 struct host_to_dev_fis fis;
1559 unsigned long start;
1561 /* Build the FIS. */
1562 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1565 fis.command = ATA_CMD_STANDBYNOW1;
1568 rv = mtip_exec_internal_command(port,
1576 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1577 jiffies_to_msecs(jiffies - start));
1579 dev_warn(&port->dd->pdev->dev,
1580 "STANDBY IMMEDIATE command failed.\n");
1586 * Issue a READ LOG EXT command to the device.
1588 * @port pointer to the port structure.
1589 * @page page number to fetch
1590 * @buffer pointer to buffer
1591 * @buffer_dma dma address corresponding to @buffer
1592 * @sectors page length to fetch, in sectors
1595 * @rv return value from mtip_exec_internal_command()
1597 static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1598 dma_addr_t buffer_dma, unsigned int sectors)
1600 struct host_to_dev_fis fis;
1602 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1605 fis.command = ATA_CMD_READ_LOG_EXT;
1606 fis.sect_count = sectors & 0xFF;
1607 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1610 fis.device = ATA_DEVICE_OBS;
1612 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1614 return mtip_exec_internal_command(port,
1618 sectors * ATA_SECT_SIZE,
1621 MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
1625 * Issue a SMART READ DATA command to the device.
1627 * @port pointer to the port structure.
1628 * @buffer pointer to buffer
1629 * @buffer_dma dma address corresponding to @buffer
1632 * @rv return value from mtip_exec_internal_command()
1634 static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1635 dma_addr_t buffer_dma)
1637 struct host_to_dev_fis fis;
1639 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1642 fis.command = ATA_CMD_SMART;
1643 fis.features = 0xD0;
1647 fis.device = ATA_DEVICE_OBS;
1649 return mtip_exec_internal_command(port,
1660 * Get the value of a smart attribute
1662 * @port pointer to the port structure
1663 * @id attribute number
1664 * @attrib pointer to return attrib information corresponding to @id
1667 * -EINVAL NULL buffer passed or unsupported attribute @id.
1668 * -EPERM Identify data not valid, SMART not supported or not enabled
1670 static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1671 struct smart_attr *attrib)
1674 struct smart_attr *pattr;
1679 if (!port->identify_valid) {
1680 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1683 if (!(port->identify[82] & 0x1)) {
1684 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1687 if (!(port->identify[85] & 0x1)) {
1688 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1692 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1693 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1695 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1699 pattr = (struct smart_attr *)(port->smart_buf + 2);
1700 for (i = 0; i < 29; i++, pattr++)
1701 if (pattr->attr_id == id) {
1702 memcpy(attrib, pattr, sizeof(struct smart_attr));
1707 dev_warn(&port->dd->pdev->dev,
1708 "Query for invalid SMART attribute ID\n");
1716 * Trim unused sectors
1718 * @dd pointer to driver_data structure
1720 * @len # of 512b sectors to trim
1723 * -ENOMEM Out of dma memory
1724 * -EINVAL Invalid parameters passed in, trim not supported
1725 * -EIO Error submitting trim request to hw
1727 static int mtip_send_trim(struct driver_data *dd, unsigned int lba, unsigned int len)
1730 u64 tlba, tlen, sect_left;
1731 struct mtip_trim_entry *buf;
1732 dma_addr_t dma_addr;
1733 struct host_to_dev_fis fis;
1735 if (!len || dd->trim_supp == false)
1738 /* Trim request too big */
1739 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1741 /* Trim request not aligned on 4k boundary */
1742 WARN_ON(len % 8 != 0);
1744 /* Warn if vu_trim structure is too big */
1745 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1747 /* Allocate a DMA buffer for the trim structure */
1748 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1752 memset(buf, 0, ATA_SECT_SIZE);
1754 for (i = 0, sect_left = len, tlba = lba;
1755 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1757 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1758 MTIP_MAX_TRIM_ENTRY_LEN :
1760 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1761 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1765 WARN_ON(sect_left != 0);
1768 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1772 fis.features = 0x60;
1774 fis.device = ATA_DEVICE_OBS;
1776 if (mtip_exec_internal_command(dd->port,
1783 MTIP_TRIM_TIMEOUT_MS) < 0)
1786 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1791 * Get the drive capacity.
1793 * @dd Pointer to the device data structure.
1794 * @sectors Pointer to the variable that will receive the sector count.
1797 * 1 Capacity was returned successfully.
1798 * 0 The identify information is invalid.
1800 static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
1802 struct mtip_port *port = dd->port;
1803 u64 total, raw0, raw1, raw2, raw3;
1804 raw0 = port->identify[100];
1805 raw1 = port->identify[101];
1806 raw2 = port->identify[102];
1807 raw3 = port->identify[103];
1808 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1810 return (bool) !!port->identify_valid;
1816 * Resets the HBA by setting the HBA Reset bit in the Global
1817 * HBA Control register. After setting the HBA Reset bit the
1818 * function waits for 1 second before reading the HBA Reset
1819 * bit to make sure it has cleared. If HBA Reset is not clear
1820 * an error is returned. Cannot be used in non-blockable
1823 * @dd Pointer to the driver data structure.
1826 * 0 The reset was successful.
1827 * -1 The HBA Reset bit did not clear.
1829 static int mtip_hba_reset(struct driver_data *dd)
1831 mtip_deinit_port(dd->port);
1833 /* Set the reset bit */
1834 writel(HOST_RESET, dd->mmio + HOST_CTL);
1837 readl(dd->mmio + HOST_CTL);
1839 /* Wait for reset to clear */
1842 /* Check the bit has cleared */
1843 if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
1844 dev_err(&dd->pdev->dev,
1845 "Reset bit did not clear.\n");
1853 * Display the identify command data.
1855 * @port Pointer to the port data structure.
1860 static void mtip_dump_identify(struct mtip_port *port)
1863 unsigned short revid;
1866 if (!port->identify_valid)
1869 strlcpy(cbuf, (char *)(port->identify+10), 21);
1870 dev_info(&port->dd->pdev->dev,
1871 "Serial No.: %s\n", cbuf);
1873 strlcpy(cbuf, (char *)(port->identify+23), 9);
1874 dev_info(&port->dd->pdev->dev,
1875 "Firmware Ver.: %s\n", cbuf);
1877 strlcpy(cbuf, (char *)(port->identify+27), 41);
1878 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1880 if (mtip_hw_get_capacity(port->dd, §ors))
1881 dev_info(&port->dd->pdev->dev,
1882 "Capacity: %llu sectors (%llu MB)\n",
1884 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1886 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
1887 switch (revid & 0xFF) {
1889 strlcpy(cbuf, "A0", 3);
1892 strlcpy(cbuf, "A2", 3);
1895 strlcpy(cbuf, "?", 2);
1898 dev_info(&port->dd->pdev->dev,
1899 "Card Type: %s\n", cbuf);
1903 * Map the commands scatter list into the command table.
1905 * @command Pointer to the command.
1906 * @nents Number of scatter list entries.
1911 static inline void fill_command_sg(struct driver_data *dd,
1912 struct mtip_cmd *command,
1916 unsigned int dma_len;
1917 struct mtip_cmd_sg *command_sg;
1918 struct scatterlist *sg = command->sg;
1920 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1922 for (n = 0; n < nents; n++) {
1923 dma_len = sg_dma_len(sg);
1924 if (dma_len > 0x400000)
1925 dev_err(&dd->pdev->dev,
1926 "DMA segment length truncated\n");
1927 command_sg->info = __force_bit2int
1928 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1929 command_sg->dba = __force_bit2int
1930 cpu_to_le32(sg_dma_address(sg));
1931 command_sg->dba_upper = __force_bit2int
1932 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
1939 * @brief Execute a drive command.
1941 * return value 0 The command completed successfully.
1942 * return value -1 An error occurred while executing the command.
1944 static int exec_drive_task(struct mtip_port *port, u8 *command)
1946 struct host_to_dev_fis fis;
1947 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1949 /* Build the FIS. */
1950 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1953 fis.command = command[0];
1954 fis.features = command[1];
1955 fis.sect_count = command[2];
1956 fis.sector = command[3];
1957 fis.cyl_low = command[4];
1958 fis.cyl_hi = command[5];
1959 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1961 dbg_printk(MTIP_DRV_NAME " %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
1971 /* Execute the command. */
1972 if (mtip_exec_internal_command(port,
1979 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
1983 command[0] = reply->command; /* Status*/
1984 command[1] = reply->features; /* Error*/
1985 command[4] = reply->cyl_low;
1986 command[5] = reply->cyl_hi;
1988 dbg_printk(MTIP_DRV_NAME " %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
1999 * @brief Execute a drive command.
2001 * @param port Pointer to the port data structure.
2002 * @param command Pointer to the user specified command parameters.
2003 * @param user_buffer Pointer to the user space buffer where read sector
2004 * data should be copied.
2006 * return value 0 The command completed successfully.
2007 * return value -EFAULT An error occurred while copying the completion
2008 * data to the user space buffer.
2009 * return value -1 An error occurred while executing the command.
2011 static int exec_drive_command(struct mtip_port *port, u8 *command,
2012 void __user *user_buffer)
2014 struct host_to_dev_fis fis;
2015 struct host_to_dev_fis *reply;
2017 dma_addr_t dma_addr = 0;
2018 int rv = 0, xfer_sz = command[3];
2024 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
2025 ATA_SECT_SIZE * xfer_sz,
2029 dev_err(&port->dd->pdev->dev,
2030 "Memory allocation failed (%d bytes)\n",
2031 ATA_SECT_SIZE * xfer_sz);
2034 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
2037 /* Build the FIS. */
2038 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2041 fis.command = command[0];
2042 fis.features = command[2];
2043 fis.sect_count = command[3];
2044 if (fis.command == ATA_CMD_SMART) {
2045 fis.sector = command[1];
2051 reply = (port->rxfis + RX_FIS_PIO_SETUP);
2053 reply = (port->rxfis + RX_FIS_D2H_REG);
2055 dbg_printk(MTIP_DRV_NAME
2056 " %s: User Command: cmd %x, sect %x, "
2057 "feat %x, sectcnt %x\n",
2064 /* Execute the command. */
2065 if (mtip_exec_internal_command(port,
2068 (xfer_sz ? dma_addr : 0),
2069 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
2072 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
2075 goto exit_drive_command;
2078 /* Collect the completion status. */
2079 command[0] = reply->command; /* Status*/
2080 command[1] = reply->features; /* Error*/
2081 command[2] = reply->sect_count;
2083 dbg_printk(MTIP_DRV_NAME
2084 " %s: Completion Status: stat %x, "
2085 "err %x, nsect %x\n",
2092 if (copy_to_user(user_buffer,
2094 ATA_SECT_SIZE * command[3])) {
2096 goto exit_drive_command;
2101 dmam_free_coherent(&port->dd->pdev->dev,
2102 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
2107 * Indicates whether a command has a single sector payload.
2109 * @command passed to the device to perform the certain event.
2110 * @features passed to the device to perform the certain event.
2113 * 1 command is one that always has a single sector payload,
2114 * regardless of the value in the Sector Count field.
2118 static unsigned int implicit_sector(unsigned char command,
2119 unsigned char features)
2121 unsigned int rv = 0;
2123 /* list of commands that have an implicit sector count of 1 */
2125 case ATA_CMD_SEC_SET_PASS:
2126 case ATA_CMD_SEC_UNLOCK:
2127 case ATA_CMD_SEC_ERASE_PREP:
2128 case ATA_CMD_SEC_ERASE_UNIT:
2129 case ATA_CMD_SEC_FREEZE_LOCK:
2130 case ATA_CMD_SEC_DISABLE_PASS:
2131 case ATA_CMD_PMP_READ:
2132 case ATA_CMD_PMP_WRITE:
2135 case ATA_CMD_SET_MAX:
2136 if (features == ATA_SET_MAX_UNLOCK)
2140 if ((features == ATA_SMART_READ_VALUES) ||
2141 (features == ATA_SMART_READ_THRESHOLDS))
2144 case ATA_CMD_CONF_OVERLAY:
2145 if ((features == ATA_DCO_IDENTIFY) ||
2146 (features == ATA_DCO_SET))
2152 static void mtip_set_timeout(struct driver_data *dd,
2153 struct host_to_dev_fis *fis,
2154 unsigned int *timeout, u8 erasemode)
2156 switch (fis->command) {
2157 case ATA_CMD_DOWNLOAD_MICRO:
2158 *timeout = 120000; /* 2 minutes */
2160 case ATA_CMD_SEC_ERASE_UNIT:
2163 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
2165 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
2167 case ATA_CMD_STANDBYNOW1:
2168 *timeout = 120000; /* 2 minutes */
2172 *timeout = 60000; /* 60 seconds */
2175 *timeout = 15000; /* 15 seconds */
2178 *timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
2184 * Executes a taskfile
2185 * See ide_taskfile_ioctl() for derivation
2187 static int exec_drive_taskfile(struct driver_data *dd,
2189 ide_task_request_t *req_task,
2192 struct host_to_dev_fis fis;
2193 struct host_to_dev_fis *reply;
2196 dma_addr_t outbuf_dma = 0;
2197 dma_addr_t inbuf_dma = 0;
2198 dma_addr_t dma_buffer = 0;
2200 unsigned int taskin = 0;
2201 unsigned int taskout = 0;
2203 unsigned int timeout;
2204 unsigned int force_single_sector;
2205 unsigned int transfer_size;
2206 unsigned long task_file_data;
2207 int intotal = outtotal + req_task->out_size;
2210 taskout = req_task->out_size;
2211 taskin = req_task->in_size;
2212 /* 130560 = 512 * 0xFF*/
2213 if (taskin > 130560 || taskout > 130560) {
2219 outbuf = kzalloc(taskout, GFP_KERNEL);
2220 if (outbuf == NULL) {
2224 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
2228 outbuf_dma = pci_map_single(dd->pdev,
2232 if (outbuf_dma == 0) {
2236 dma_buffer = outbuf_dma;
2240 inbuf = kzalloc(taskin, GFP_KERNEL);
2241 if (inbuf == NULL) {
2246 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2250 inbuf_dma = pci_map_single(dd->pdev,
2252 taskin, DMA_FROM_DEVICE);
2253 if (inbuf_dma == 0) {
2257 dma_buffer = inbuf_dma;
2260 /* only supports PIO and non-data commands from this ioctl. */
2261 switch (req_task->data_phase) {
2263 nsect = taskout / ATA_SECT_SIZE;
2264 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2267 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2269 case TASKFILE_NO_DATA:
2270 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2277 /* Build the FIS. */
2278 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2282 fis.command = req_task->io_ports[7];
2283 fis.features = req_task->io_ports[1];
2284 fis.sect_count = req_task->io_ports[2];
2285 fis.lba_low = req_task->io_ports[3];
2286 fis.lba_mid = req_task->io_ports[4];
2287 fis.lba_hi = req_task->io_ports[5];
2288 /* Clear the dev bit*/
2289 fis.device = req_task->io_ports[6] & ~0x10;
2291 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2292 req_task->in_flags.all =
2293 IDE_TASKFILE_STD_IN_FLAGS |
2294 (IDE_HOB_STD_IN_FLAGS << 8);
2295 fis.lba_low_ex = req_task->hob_ports[3];
2296 fis.lba_mid_ex = req_task->hob_ports[4];
2297 fis.lba_hi_ex = req_task->hob_ports[5];
2298 fis.features_ex = req_task->hob_ports[1];
2299 fis.sect_cnt_ex = req_task->hob_ports[2];
2302 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2305 force_single_sector = implicit_sector(fis.command, fis.features);
2307 if ((taskin || taskout) && (!fis.sect_count)) {
2309 fis.sect_count = nsect;
2311 if (!force_single_sector) {
2312 dev_warn(&dd->pdev->dev,
2313 "data movement but "
2314 "sect_count is 0\n");
2321 dbg_printk(MTIP_DRV_NAME
2322 " %s: cmd %x, feat %x, nsect %x,"
2323 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2334 /* check for erase mode support during secure erase.*/
2335 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2336 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
2340 mtip_set_timeout(dd, &fis, &timeout, erasemode);
2342 /* Determine the correct transfer size.*/
2343 if (force_single_sector)
2344 transfer_size = ATA_SECT_SIZE;
2346 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2348 /* Execute the command.*/
2349 if (mtip_exec_internal_command(dd->port,
2361 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2363 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2364 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2365 req_task->io_ports[7] = reply->control;
2367 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2368 req_task->io_ports[7] = reply->command;
2371 /* reclaim the DMA buffers.*/
2373 pci_unmap_single(dd->pdev, inbuf_dma,
2374 taskin, DMA_FROM_DEVICE);
2376 pci_unmap_single(dd->pdev, outbuf_dma,
2377 taskout, DMA_TO_DEVICE);
2381 /* return the ATA registers to the caller.*/
2382 req_task->io_ports[1] = reply->features;
2383 req_task->io_ports[2] = reply->sect_count;
2384 req_task->io_ports[3] = reply->lba_low;
2385 req_task->io_ports[4] = reply->lba_mid;
2386 req_task->io_ports[5] = reply->lba_hi;
2387 req_task->io_ports[6] = reply->device;
2389 if (req_task->out_flags.all & 1) {
2391 req_task->hob_ports[3] = reply->lba_low_ex;
2392 req_task->hob_ports[4] = reply->lba_mid_ex;
2393 req_task->hob_ports[5] = reply->lba_hi_ex;
2394 req_task->hob_ports[1] = reply->features_ex;
2395 req_task->hob_ports[2] = reply->sect_cnt_ex;
2397 dbg_printk(MTIP_DRV_NAME
2398 " %s: Completion: stat %x,"
2399 "err %x, sect_cnt %x, lbalo %x,"
2400 "lbamid %x, lbahi %x, dev %x\n",
2402 req_task->io_ports[7],
2403 req_task->io_ports[1],
2404 req_task->io_ports[2],
2405 req_task->io_ports[3],
2406 req_task->io_ports[4],
2407 req_task->io_ports[5],
2408 req_task->io_ports[6]);
2411 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2417 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2424 pci_unmap_single(dd->pdev, inbuf_dma,
2425 taskin, DMA_FROM_DEVICE);
2427 pci_unmap_single(dd->pdev, outbuf_dma,
2428 taskout, DMA_TO_DEVICE);
2436 * Handle IOCTL calls from the Block Layer.
2438 * This function is called by the Block Layer when it receives an IOCTL
2439 * command that it does not understand. If the IOCTL command is not supported
2440 * this function returns -ENOTTY.
2442 * @dd Pointer to the driver data structure.
2443 * @cmd IOCTL command passed from the Block Layer.
2444 * @arg IOCTL argument passed from the Block Layer.
2447 * 0 The IOCTL completed successfully.
2448 * -ENOTTY The specified command is not supported.
2449 * -EFAULT An error occurred copying data to a user space buffer.
2450 * -EIO An error occurred while executing the command.
2452 static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2456 case HDIO_GET_IDENTITY:
2458 if (copy_to_user((void __user *)arg, dd->port->identify,
2459 sizeof(u16) * ATA_ID_WORDS))
2463 case HDIO_DRIVE_CMD:
2465 u8 drive_command[4];
2467 /* Copy the user command info to our buffer. */
2468 if (copy_from_user(drive_command,
2469 (void __user *) arg,
2470 sizeof(drive_command)))
2473 /* Execute the drive command. */
2474 if (exec_drive_command(dd->port,
2476 (void __user *) (arg+4)))
2479 /* Copy the status back to the users buffer. */
2480 if (copy_to_user((void __user *) arg,
2482 sizeof(drive_command)))
2487 case HDIO_DRIVE_TASK:
2489 u8 drive_command[7];
2491 /* Copy the user command info to our buffer. */
2492 if (copy_from_user(drive_command,
2493 (void __user *) arg,
2494 sizeof(drive_command)))
2497 /* Execute the drive command. */
2498 if (exec_drive_task(dd->port, drive_command))
2501 /* Copy the status back to the users buffer. */
2502 if (copy_to_user((void __user *) arg,
2504 sizeof(drive_command)))
2509 case HDIO_DRIVE_TASKFILE: {
2510 ide_task_request_t req_task;
2513 if (copy_from_user(&req_task, (void __user *) arg,
2517 outtotal = sizeof(req_task);
2519 ret = exec_drive_taskfile(dd, (void __user *) arg,
2520 &req_task, outtotal);
2522 if (copy_to_user((void __user *) arg, &req_task,
2536 * Submit an IO to the hw
2538 * This function is called by the block layer to issue an io
2539 * to the device. Upon completion, the callback function will
2540 * be called with the data parameter passed as the callback data.
2542 * @dd Pointer to the driver data structure.
2543 * @start First sector to read.
2544 * @nsect Number of sectors to read.
2545 * @nents Number of entries in scatter list for the read command.
2546 * @tag The tag of this read command.
2547 * @callback Pointer to the function that should be called
2548 * when the read completes.
2549 * @data Callback data passed to the callback function
2550 * when the read completes.
2551 * @dir Direction (read or write)
2556 static void mtip_hw_submit_io(struct driver_data *dd, sector_t sector,
2557 int nsect, int nents, int tag, void *callback,
2558 void *data, int dir)
2560 struct host_to_dev_fis *fis;
2561 struct mtip_port *port = dd->port;
2562 struct mtip_cmd *command = &port->commands[tag];
2563 int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2566 /* Map the scatter list for DMA access */
2567 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
2569 command->scatter_ents = nents;
2572 * The number of retries for this command before it is
2573 * reported as a failure to the upper layers.
2575 command->retries = MTIP_MAX_RETRIES;
2578 fis = command->command;
2582 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
2583 fis->lba_low = start & 0xFF;
2584 fis->lba_mid = (start >> 8) & 0xFF;
2585 fis->lba_hi = (start >> 16) & 0xFF;
2586 fis->lba_low_ex = (start >> 24) & 0xFF;
2587 fis->lba_mid_ex = (start >> 32) & 0xFF;
2588 fis->lba_hi_ex = (start >> 40) & 0xFF;
2589 fis->device = 1 << 6;
2590 fis->features = nsect & 0xFF;
2591 fis->features_ex = (nsect >> 8) & 0xFF;
2592 fis->sect_count = ((tag << 3) | (tag >> 5));
2593 fis->sect_cnt_ex = 0;
2597 fill_command_sg(dd, command, nents);
2599 /* Populate the command header */
2600 command->command_header->opts =
2601 __force_bit2int cpu_to_le32(
2602 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
2603 command->command_header->byte_count = 0;
2606 * Set the completion function and data for the command
2607 * within this layer.
2609 command->comp_data = dd;
2610 command->comp_func = mtip_async_complete;
2611 command->direction = dma_dir;
2614 * Set the completion function and data for the command passed
2615 * from the upper layer.
2617 command->async_data = data;
2618 command->async_callback = callback;
2621 * To prevent this command from being issued
2622 * if an internal command is in progress or error handling is active.
2624 if (port->flags & MTIP_PF_PAUSE_IO) {
2625 set_bit(tag, port->cmds_to_issue);
2626 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
2630 /* Issue the command to the hardware */
2631 mtip_issue_ncq_command(port, tag);
2637 * Release a command slot.
2639 * @dd Pointer to the driver data structure.
2645 static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
2647 release_slot(dd->port, tag);
2651 * Obtain a command slot and return its associated scatter list.
2653 * @dd Pointer to the driver data structure.
2654 * @tag Pointer to an int that will receive the allocated command
2658 * Pointer to the scatter list for the allocated command slot
2659 * or NULL if no command slots are available.
2661 static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2665 * It is possible that, even with this semaphore, a thread
2666 * may think that no command slots are available. Therefore, we
2667 * need to make an attempt to get_slot().
2669 down(&dd->port->cmd_slot);
2670 *tag = get_slot(dd->port);
2672 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
2673 up(&dd->port->cmd_slot);
2676 if (unlikely(*tag < 0)) {
2677 up(&dd->port->cmd_slot);
2681 return dd->port->commands[*tag].sg;
2685 * Sysfs status dump.
2687 * @dev Pointer to the device structure, passed by the kernrel.
2688 * @attr Pointer to the device_attribute structure passed by the kernel.
2689 * @buf Pointer to the char buffer that will receive the stats info.
2692 * The size, in bytes, of the data copied into buf.
2694 static ssize_t mtip_hw_show_status(struct device *dev,
2695 struct device_attribute *attr,
2698 struct driver_data *dd = dev_to_disk(dev)->private_data;
2701 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
2702 size += sprintf(buf, "%s", "thermal_shutdown\n");
2703 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
2704 size += sprintf(buf, "%s", "write_protect\n");
2706 size += sprintf(buf, "%s", "online\n");
2711 static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
2713 static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2714 size_t len, loff_t *offset)
2716 struct driver_data *dd = (struct driver_data *)f->private_data;
2717 char buf[MTIP_DFS_MAX_BUF_SIZE];
2718 u32 group_allocated;
2725 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2727 for (n = dd->slot_groups-1; n >= 0; n--)
2728 size += sprintf(&buf[size], "%08X ",
2729 readl(dd->port->s_active[n]));
2731 size += sprintf(&buf[size], "]\n");
2732 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2734 for (n = dd->slot_groups-1; n >= 0; n--)
2735 size += sprintf(&buf[size], "%08X ",
2736 readl(dd->port->cmd_issue[n]));
2738 size += sprintf(&buf[size], "]\n");
2739 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2741 for (n = dd->slot_groups-1; n >= 0; n--)
2742 size += sprintf(&buf[size], "%08X ",
2743 readl(dd->port->completed[n]));
2745 size += sprintf(&buf[size], "]\n");
2746 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2747 readl(dd->port->mmio + PORT_IRQ_STAT));
2748 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2749 readl(dd->mmio + HOST_IRQ_STAT));
2750 size += sprintf(&buf[size], "\n");
2752 size += sprintf(&buf[size], "L/ Allocated : [ 0x");
2754 for (n = dd->slot_groups-1; n >= 0; n--) {
2755 if (sizeof(long) > sizeof(u32))
2757 dd->port->allocated[n/2] >> (32*(n&1));
2759 group_allocated = dd->port->allocated[n];
2760 size += sprintf(&buf[size], "%08X ", group_allocated);
2762 size += sprintf(&buf[size], "]\n");
2764 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2766 for (n = dd->slot_groups-1; n >= 0; n--) {
2767 if (sizeof(long) > sizeof(u32))
2769 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2771 group_allocated = dd->port->cmds_to_issue[n];
2772 size += sprintf(&buf[size], "%08X ", group_allocated);
2774 size += sprintf(&buf[size], "]\n");
2776 *offset = size <= len ? size : len;
2777 size = copy_to_user(ubuf, buf, *offset);
2784 static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2785 size_t len, loff_t *offset)
2787 struct driver_data *dd = (struct driver_data *)f->private_data;
2788 char buf[MTIP_DFS_MAX_BUF_SIZE];
2794 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2796 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2799 *offset = size <= len ? size : len;
2800 size = copy_to_user(ubuf, buf, *offset);
2807 static const struct file_operations mtip_regs_fops = {
2808 .owner = THIS_MODULE,
2809 .open = simple_open,
2810 .read = mtip_hw_read_registers,
2811 .llseek = no_llseek,
2814 static const struct file_operations mtip_flags_fops = {
2815 .owner = THIS_MODULE,
2816 .open = simple_open,
2817 .read = mtip_hw_read_flags,
2818 .llseek = no_llseek,
2822 * Create the sysfs related attributes.
2824 * @dd Pointer to the driver data structure.
2825 * @kobj Pointer to the kobj for the block device.
2828 * 0 Operation completed successfully.
2829 * -EINVAL Invalid parameter.
2831 static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
2836 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2837 dev_warn(&dd->pdev->dev,
2838 "Error creating 'status' sysfs entry\n");
2843 * Remove the sysfs related attributes.
2845 * @dd Pointer to the driver data structure.
2846 * @kobj Pointer to the kobj for the block device.
2849 * 0 Operation completed successfully.
2850 * -EINVAL Invalid parameter.
2852 static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
2857 sysfs_remove_file(kobj, &dev_attr_status.attr);
2862 static int mtip_hw_debugfs_init(struct driver_data *dd)
2867 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2868 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2869 dev_warn(&dd->pdev->dev,
2870 "Error creating node %s under debugfs\n",
2871 dd->disk->disk_name);
2872 dd->dfs_node = NULL;
2876 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2878 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2884 static void mtip_hw_debugfs_exit(struct driver_data *dd)
2886 debugfs_remove_recursive(dd->dfs_node);
2891 * Perform any init/resume time hardware setup
2893 * @dd Pointer to the driver data structure.
2898 static inline void hba_setup(struct driver_data *dd)
2901 hwdata = readl(dd->mmio + HOST_HSORG);
2903 /* interrupt bug workaround: use only 1 IS bit.*/
2905 HSORG_DISABLE_SLOTGRP_INTR |
2906 HSORG_DISABLE_SLOTGRP_PXIS,
2907 dd->mmio + HOST_HSORG);
2911 * Detect the details of the product, and store anything needed
2912 * into the driver data structure. This includes product type and
2913 * version and number of slot groups.
2915 * @dd Pointer to the driver data structure.
2920 static void mtip_detect_product(struct driver_data *dd)
2923 unsigned int rev, slotgroups;
2926 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2928 * [15:8] hardware/software interface rev#
2929 * [ 3] asic-style interface
2930 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2932 hwdata = readl(dd->mmio + HOST_HSORG);
2934 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2935 dd->slot_groups = 1;
2938 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2939 rev = (hwdata & HSORG_HWREV) >> 8;
2940 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2941 dev_info(&dd->pdev->dev,
2942 "ASIC-FPGA design, HS rev 0x%x, "
2943 "%i slot groups [%i slots]\n",
2948 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2949 dev_warn(&dd->pdev->dev,
2950 "Warning: driver only supports "
2951 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2952 slotgroups = MTIP_MAX_SLOT_GROUPS;
2954 dd->slot_groups = slotgroups;
2958 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2962 * Blocking wait for FTL rebuild to complete
2964 * @dd Pointer to the DRIVER_DATA structure.
2967 * 0 FTL rebuild completed successfully
2968 * -EFAULT FTL rebuild error/timeout/interruption
2970 static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2972 unsigned long timeout, cnt = 0, start;
2974 dev_warn(&dd->pdev->dev,
2975 "FTL rebuild in progress. Polling for completion.\n");
2978 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2981 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
2984 if (mtip_check_surprise_removal(dd->pdev))
2987 if (mtip_get_identify(dd->port, NULL) < 0)
2990 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2991 MTIP_FTL_REBUILD_MAGIC) {
2993 /* Print message every 3 minutes */
2995 dev_warn(&dd->pdev->dev,
2996 "FTL rebuild in progress (%d secs).\n",
2997 jiffies_to_msecs(jiffies - start) / 1000);
3001 dev_warn(&dd->pdev->dev,
3002 "FTL rebuild complete (%d secs).\n",
3003 jiffies_to_msecs(jiffies - start) / 1000);
3004 mtip_block_initialize(dd);
3008 } while (time_before(jiffies, timeout));
3010 /* Check for timeout */
3011 dev_err(&dd->pdev->dev,
3012 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
3013 jiffies_to_msecs(jiffies - start) / 1000);
3018 * service thread to issue queued commands
3020 * @data Pointer to the driver data structure.
3026 static int mtip_service_thread(void *data)
3028 struct driver_data *dd = (struct driver_data *)data;
3029 unsigned long slot, slot_start, slot_wrap;
3030 unsigned int num_cmd_slots = dd->slot_groups * 32;
3031 struct mtip_port *port = dd->port;
3035 * the condition is to check neither an internal command is
3036 * is in progress nor error handling is active
3038 wait_event_interruptible(port->svc_wait, (port->flags) &&
3039 !(port->flags & MTIP_PF_PAUSE_IO));
3041 if (kthread_should_stop())
3044 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3048 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
3049 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
3051 /* used to restrict the loop to one iteration */
3052 slot_start = num_cmd_slots;
3055 slot = find_next_bit(port->cmds_to_issue,
3056 num_cmd_slots, slot);
3057 if (slot_wrap == 1) {
3058 if ((slot_start >= slot) ||
3059 (slot >= num_cmd_slots))
3062 if (unlikely(slot_start == num_cmd_slots))
3065 if (unlikely(slot == num_cmd_slots)) {
3071 /* Issue the command to the hardware */
3072 mtip_issue_ncq_command(port, slot);
3074 clear_bit(slot, port->cmds_to_issue);
3077 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
3078 } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
3079 if (!mtip_ftl_rebuild_poll(dd))
3080 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
3082 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
3084 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
3086 if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
3093 * Called once for each card.
3095 * @dd Pointer to the driver data structure.
3098 * 0 on success, else an error code.
3100 static int mtip_hw_init(struct driver_data *dd)
3104 unsigned int num_command_slots;
3105 unsigned long timeout, timetaken;
3107 struct smart_attr attr242;
3109 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3111 mtip_detect_product(dd);
3112 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3116 num_command_slots = dd->slot_groups * 32;
3120 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3123 dev_err(&dd->pdev->dev,
3124 "Memory allocation: port structure\n");
3128 /* Continue workqueue setup */
3129 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3130 dd->work[i].port = dd->port;
3132 /* Counting semaphore to track command slot usage */
3133 sema_init(&dd->port->cmd_slot, num_command_slots - 1);
3135 /* Spinlock to prevent concurrent issue */
3136 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3137 spin_lock_init(&dd->port->cmd_issue_lock[i]);
3139 /* Set the port mmio base address. */
3140 dd->port->mmio = dd->mmio + PORT_OFFSET;
3143 /* Allocate memory for the command list. */
3144 dd->port->command_list =
3145 dmam_alloc_coherent(&dd->pdev->dev,
3146 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
3147 &dd->port->command_list_dma,
3149 if (!dd->port->command_list) {
3150 dev_err(&dd->pdev->dev,
3151 "Memory allocation: command list\n");
3156 /* Clear the memory we have allocated. */
3157 memset(dd->port->command_list,
3159 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4));
3161 /* Setup the addresse of the RX FIS. */
3162 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
3163 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
3165 /* Setup the address of the command tables. */
3166 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
3167 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
3169 /* Setup the address of the identify data. */
3170 dd->port->identify = dd->port->command_table +
3172 dd->port->identify_dma = dd->port->command_tbl_dma +
3175 /* Setup the address of the sector buffer - for some non-ncq cmds */
3176 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
3177 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
3179 /* Setup the address of the log buf - for read log command */
3180 dd->port->log_buf = (void *)dd->port->sector_buffer + ATA_SECT_SIZE;
3181 dd->port->log_buf_dma = dd->port->sector_buffer_dma + ATA_SECT_SIZE;
3183 /* Setup the address of the smart buf - for smart read data command */
3184 dd->port->smart_buf = (void *)dd->port->log_buf + ATA_SECT_SIZE;
3185 dd->port->smart_buf_dma = dd->port->log_buf_dma + ATA_SECT_SIZE;
3188 /* Point the command headers at the command tables. */
3189 for (i = 0; i < num_command_slots; i++) {
3190 dd->port->commands[i].command_header =
3191 dd->port->command_list +
3192 (sizeof(struct mtip_cmd_hdr) * i);
3193 dd->port->commands[i].command_header_dma =
3194 dd->port->command_list_dma +
3195 (sizeof(struct mtip_cmd_hdr) * i);
3197 dd->port->commands[i].command =
3198 dd->port->command_table + (HW_CMD_TBL_SZ * i);
3199 dd->port->commands[i].command_dma =
3200 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
3202 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
3203 dd->port->commands[i].command_header->ctbau =
3204 __force_bit2int cpu_to_le32(
3205 (dd->port->commands[i].command_dma >> 16) >> 16);
3206 dd->port->commands[i].command_header->ctba =
3207 __force_bit2int cpu_to_le32(
3208 dd->port->commands[i].command_dma & 0xFFFFFFFF);
3211 * If this is not done, a bug is reported by the stock
3212 * FC11 i386. Due to the fact that it has lots of kernel
3213 * debugging enabled.
3215 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
3217 /* Mark all commands as currently inactive.*/
3218 atomic_set(&dd->port->commands[i].active, 0);
3221 /* Setup the pointers to the extended s_active and CI registers. */
3222 for (i = 0; i < dd->slot_groups; i++) {
3223 dd->port->s_active[i] =
3224 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3225 dd->port->cmd_issue[i] =
3226 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3227 dd->port->completed[i] =
3228 dd->port->mmio + i*0x80 + PORT_SDBV;
3231 timetaken = jiffies;
3232 timeout = jiffies + msecs_to_jiffies(30000);
3233 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3234 time_before(jiffies, timeout)) {
3237 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3238 timetaken = jiffies - timetaken;
3239 dev_warn(&dd->pdev->dev,
3240 "Surprise removal detected at %u ms\n",
3241 jiffies_to_msecs(timetaken));
3245 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
3246 timetaken = jiffies - timetaken;
3247 dev_warn(&dd->pdev->dev,
3248 "Removal detected at %u ms\n",
3249 jiffies_to_msecs(timetaken));
3254 /* Conditionally reset the HBA. */
3255 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3256 if (mtip_hba_reset(dd) < 0) {
3257 dev_err(&dd->pdev->dev,
3258 "Card did not reset within timeout\n");
3263 /* Clear any pending interrupts on the HBA */
3264 writel(readl(dd->mmio + HOST_IRQ_STAT),
3265 dd->mmio + HOST_IRQ_STAT);
3268 mtip_init_port(dd->port);
3269 mtip_start_port(dd->port);
3271 /* Setup the ISR and enable interrupts. */
3272 rv = devm_request_irq(&dd->pdev->dev,
3276 dev_driver_string(&dd->pdev->dev),
3280 dev_err(&dd->pdev->dev,
3281 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3284 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
3286 /* Enable interrupts on the HBA. */
3287 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3288 dd->mmio + HOST_CTL);
3290 init_timer(&dd->port->cmd_timer);
3291 init_waitqueue_head(&dd->port->svc_wait);
3293 dd->port->cmd_timer.data = (unsigned long int) dd->port;
3294 dd->port->cmd_timer.function = mtip_timeout_function;
3295 mod_timer(&dd->port->cmd_timer,
3296 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
3299 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
3304 if (mtip_get_identify(dd->port, NULL) < 0) {
3309 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3310 MTIP_FTL_REBUILD_MAGIC) {
3311 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3312 return MTIP_FTL_REBUILD_MAGIC;
3314 mtip_dump_identify(dd->port);
3316 /* check write protect, over temp and rebuild statuses */
3317 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3319 dd->port->log_buf_dma, 1);
3321 dev_warn(&dd->pdev->dev,
3322 "Error in READ LOG EXT (10h) command\n");
3323 /* non-critical error, don't fail the load */
3325 buf = (unsigned char *)dd->port->log_buf;
3326 if (buf[259] & 0x1) {
3327 dev_info(&dd->pdev->dev,
3328 "Write protect bit is set.\n");
3329 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3331 if (buf[288] == 0xF7) {
3332 dev_info(&dd->pdev->dev,
3333 "Exceeded Tmax, drive in thermal shutdown.\n");
3334 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3336 if (buf[288] == 0xBF) {
3337 dev_info(&dd->pdev->dev,
3338 "Drive indicates rebuild has failed.\n");
3343 /* get write protect progess */
3344 memset(&attr242, 0, sizeof(struct smart_attr));
3345 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3346 dev_warn(&dd->pdev->dev,
3347 "Unable to check write protect progress\n");
3349 dev_info(&dd->pdev->dev,
3350 "Write protect progress: %u%% (%u blocks)\n",
3351 attr242.cur, le32_to_cpu(attr242.data));
3355 del_timer_sync(&dd->port->cmd_timer);
3357 /* Disable interrupts on the HBA. */
3358 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3359 dd->mmio + HOST_CTL);
3361 /* Release the IRQ. */
3362 irq_set_affinity_hint(dd->pdev->irq, NULL);
3363 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3366 mtip_deinit_port(dd->port);
3368 /* Free the command/command header memory. */
3369 dmam_free_coherent(&dd->pdev->dev,
3370 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
3371 dd->port->command_list,
3372 dd->port->command_list_dma);
3374 /* Free the memory allocated for the for structure. */
3381 * Called to deinitialize an interface.
3383 * @dd Pointer to the driver data structure.
3388 static int mtip_hw_exit(struct driver_data *dd)
3391 * Send standby immediate (E0h) to the drive so that it
3394 if (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
3396 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags))
3397 if (mtip_standby_immediate(dd->port))
3398 dev_warn(&dd->pdev->dev,
3399 "STANDBY IMMEDIATE failed\n");
3401 /* de-initialize the port. */
3402 mtip_deinit_port(dd->port);
3404 /* Disable interrupts on the HBA. */
3405 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3406 dd->mmio + HOST_CTL);
3409 del_timer_sync(&dd->port->cmd_timer);
3411 /* Release the IRQ. */
3412 irq_set_affinity_hint(dd->pdev->irq, NULL);
3413 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3415 /* Free the command/command header memory. */
3416 dmam_free_coherent(&dd->pdev->dev,
3417 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
3418 dd->port->command_list,
3419 dd->port->command_list_dma);
3420 /* Free the memory allocated for the for structure. */
3427 * Issue a Standby Immediate command to the device.
3429 * This function is called by the Block Layer just before the
3430 * system powers off during a shutdown.
3432 * @dd Pointer to the driver data structure.
3437 static int mtip_hw_shutdown(struct driver_data *dd)
3440 * Send standby immediate (E0h) to the drive so that it
3443 mtip_standby_immediate(dd->port);
3451 * This function is called by the Block Layer just before the
3452 * system hibernates.
3454 * @dd Pointer to the driver data structure.
3457 * 0 Suspend was successful
3458 * -EFAULT Suspend was not successful
3460 static int mtip_hw_suspend(struct driver_data *dd)
3463 * Send standby immediate (E0h) to the drive
3464 * so that it saves its state.
3466 if (mtip_standby_immediate(dd->port) != 0) {
3467 dev_err(&dd->pdev->dev,
3468 "Failed standby-immediate command\n");
3472 /* Disable interrupts on the HBA.*/
3473 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3474 dd->mmio + HOST_CTL);
3475 mtip_deinit_port(dd->port);
3483 * This function is called by the Block Layer as the
3486 * @dd Pointer to the driver data structure.
3489 * 0 Resume was successful
3490 * -EFAULT Resume was not successful
3492 static int mtip_hw_resume(struct driver_data *dd)
3494 /* Perform any needed hardware setup steps */
3498 if (mtip_hba_reset(dd) != 0) {
3499 dev_err(&dd->pdev->dev,
3500 "Unable to reset the HBA\n");
3505 * Enable the port, DMA engine, and FIS reception specific
3506 * h/w in controller.
3508 mtip_init_port(dd->port);
3509 mtip_start_port(dd->port);
3511 /* Enable interrupts on the HBA.*/
3512 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3513 dd->mmio + HOST_CTL);
3519 * Helper function for reusing disk name
3520 * upon hot insertion.
3522 static int rssd_disk_name_format(char *prefix,
3527 const int base = 'z' - 'a' + 1;
3528 char *begin = buf + strlen(prefix);
3529 char *end = buf + buflen;
3539 *--p = 'a' + (index % unit);
3540 index = (index / unit) - 1;
3541 } while (index >= 0);
3543 memmove(begin, p, end - p);
3544 memcpy(buf, prefix, strlen(prefix));
3550 * Block layer IOCTL handler.
3552 * @dev Pointer to the block_device structure.
3554 * @cmd IOCTL command passed from the user application.
3555 * @arg Argument passed from the user application.
3558 * 0 IOCTL completed successfully.
3559 * -ENOTTY IOCTL not supported or invalid driver data
3560 * structure pointer.
3562 static int mtip_block_ioctl(struct block_device *dev,
3567 struct driver_data *dd = dev->bd_disk->private_data;
3569 if (!capable(CAP_SYS_ADMIN))
3575 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
3582 return mtip_hw_ioctl(dd, cmd, arg);
3586 #ifdef CONFIG_COMPAT
3588 * Block layer compat IOCTL handler.
3590 * @dev Pointer to the block_device structure.
3592 * @cmd IOCTL command passed from the user application.
3593 * @arg Argument passed from the user application.
3596 * 0 IOCTL completed successfully.
3597 * -ENOTTY IOCTL not supported or invalid driver data
3598 * structure pointer.
3600 static int mtip_block_compat_ioctl(struct block_device *dev,
3605 struct driver_data *dd = dev->bd_disk->private_data;
3607 if (!capable(CAP_SYS_ADMIN))
3613 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
3619 case HDIO_DRIVE_TASKFILE: {
3620 struct mtip_compat_ide_task_request_s __user *compat_req_task;
3621 ide_task_request_t req_task;
3622 int compat_tasksize, outtotal, ret;
3625 sizeof(struct mtip_compat_ide_task_request_s);
3628 (struct mtip_compat_ide_task_request_s __user *) arg;
3630 if (copy_from_user(&req_task, (void __user *) arg,
3631 compat_tasksize - (2 * sizeof(compat_long_t))))
3634 if (get_user(req_task.out_size, &compat_req_task->out_size))
3637 if (get_user(req_task.in_size, &compat_req_task->in_size))
3640 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3642 ret = exec_drive_taskfile(dd, (void __user *) arg,
3643 &req_task, outtotal);
3645 if (copy_to_user((void __user *) arg, &req_task,
3647 (2 * sizeof(compat_long_t))))
3650 if (put_user(req_task.out_size, &compat_req_task->out_size))
3653 if (put_user(req_task.in_size, &compat_req_task->in_size))
3659 return mtip_hw_ioctl(dd, cmd, arg);
3665 * Obtain the geometry of the device.
3667 * You may think that this function is obsolete, but some applications,
3668 * fdisk for example still used CHS values. This function describes the
3669 * device as having 224 heads and 56 sectors per cylinder. These values are
3670 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3671 * partition is described in terms of a start and end cylinder this means
3672 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3673 * affects performance.
3675 * @dev Pointer to the block_device strucutre.
3676 * @geo Pointer to a hd_geometry structure.
3679 * 0 Operation completed successfully.
3680 * -ENOTTY An error occurred while reading the drive capacity.
3682 static int mtip_block_getgeo(struct block_device *dev,
3683 struct hd_geometry *geo)
3685 struct driver_data *dd = dev->bd_disk->private_data;
3691 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3692 dev_warn(&dd->pdev->dev,
3693 "Could not get drive capacity.\n");
3699 sector_div(capacity, (geo->heads * geo->sectors));
3700 geo->cylinders = capacity;
3705 * Block device operation function.
3707 * This structure contains pointers to the functions required by the block
3710 static const struct block_device_operations mtip_block_ops = {
3711 .ioctl = mtip_block_ioctl,
3712 #ifdef CONFIG_COMPAT
3713 .compat_ioctl = mtip_block_compat_ioctl,
3715 .getgeo = mtip_block_getgeo,
3716 .owner = THIS_MODULE
3720 * Block layer make request function.
3722 * This function is called by the kernel to process a BIO for
3725 * @queue Pointer to the request queue. Unused other than to obtain
3726 * the driver data structure.
3727 * @bio Pointer to the BIO.
3730 static void mtip_make_request(struct request_queue *queue, struct bio *bio)
3732 struct driver_data *dd = queue->queuedata;
3733 struct scatterlist *sg;
3734 struct bio_vec *bvec;
3738 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3739 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3741 bio_endio(bio, -ENXIO);
3744 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
3745 bio_endio(bio, -ENODATA);
3748 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3750 bio_data_dir(bio))) {
3751 bio_endio(bio, -ENODATA);
3754 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))) {
3755 bio_endio(bio, -ENODATA);
3760 if (unlikely(bio->bi_rw & REQ_DISCARD)) {
3761 bio_endio(bio, mtip_send_trim(dd, bio->bi_sector,
3766 if (unlikely(!bio_has_data(bio))) {
3767 blk_queue_flush(queue, 0);
3772 sg = mtip_hw_get_scatterlist(dd, &tag);
3773 if (likely(sg != NULL)) {
3774 blk_queue_bounce(queue, &bio);
3776 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3777 dev_warn(&dd->pdev->dev,
3778 "Maximum number of SGL entries exceeded\n");
3780 mtip_hw_release_scatterlist(dd, tag);
3784 /* Create the scatter list for this bio. */
3785 bio_for_each_segment(bvec, bio, nents) {
3786 sg_set_page(&sg[nents],
3792 /* Issue the read/write. */
3793 mtip_hw_submit_io(dd,
3806 * Block layer initialization function.
3808 * This function is called once by the PCI layer for each P320
3809 * device that is connected to the system.
3811 * @dd Pointer to the driver data structure.
3814 * 0 on success else an error code.
3816 static int mtip_block_initialize(struct driver_data *dd)
3818 int rv = 0, wait_for_rebuild = 0;
3820 unsigned int index = 0;
3821 struct kobject *kobj;
3822 unsigned char thd_name[16];
3825 goto skip_create_disk; /* hw init done, before rebuild */
3827 /* Initialize the protocol layer. */
3828 wait_for_rebuild = mtip_hw_init(dd);
3829 if (wait_for_rebuild < 0) {
3830 dev_err(&dd->pdev->dev,
3831 "Protocol layer initialization failed\n");
3833 goto protocol_init_error;
3836 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
3837 if (dd->disk == NULL) {
3838 dev_err(&dd->pdev->dev,
3839 "Unable to allocate gendisk structure\n");
3841 goto alloc_disk_error;
3844 /* Generate the disk name, implemented same as in sd.c */
3846 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3849 spin_lock(&rssd_index_lock);
3850 rv = ida_get_new(&rssd_index_ida, &index);
3851 spin_unlock(&rssd_index_lock);
3852 } while (rv == -EAGAIN);
3857 rv = rssd_disk_name_format("rssd",
3859 dd->disk->disk_name,
3862 goto disk_index_error;
3864 dd->disk->driverfs_dev = &dd->pdev->dev;
3865 dd->disk->major = dd->major;
3866 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3867 dd->disk->fops = &mtip_block_ops;
3868 dd->disk->private_data = dd;
3872 * if rebuild pending, start the service thread, and delay the block
3873 * queue creation and add_disk()
3875 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3876 goto start_service_thread;
3879 /* Allocate the request queue. */
3880 dd->queue = blk_alloc_queue_node(GFP_KERNEL, dd->numa_node);
3881 if (dd->queue == NULL) {
3882 dev_err(&dd->pdev->dev,
3883 "Unable to allocate request queue\n");
3885 goto block_queue_alloc_init_error;
3888 /* Attach our request function to the request queue. */
3889 blk_queue_make_request(dd->queue, mtip_make_request);
3891 dd->disk->queue = dd->queue;
3892 dd->queue->queuedata = dd;
3894 /* Set device limits. */
3895 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3896 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3897 blk_queue_physical_block_size(dd->queue, 4096);
3898 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3899 blk_queue_max_segment_size(dd->queue, 0x400000);
3900 blk_queue_io_min(dd->queue, 4096);
3903 * write back cache is not supported in the device. FUA depends on
3904 * write back cache support, hence setting flush support to zero.
3906 blk_queue_flush(dd->queue, 0);
3908 /* Signal trim support */
3909 if (dd->trim_supp == true) {
3910 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
3911 dd->queue->limits.discard_granularity = 4096;
3912 blk_queue_max_discard_sectors(dd->queue,
3913 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
3914 dd->queue->limits.discard_zeroes_data = 0;
3917 /* Set the capacity of the device in 512 byte sectors. */
3918 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3919 dev_warn(&dd->pdev->dev,
3920 "Could not read drive capacity\n");
3922 goto read_capacity_error;
3924 set_capacity(dd->disk, capacity);
3926 /* Enable the block device and add it to /dev */
3930 * Now that the disk is active, initialize any sysfs attributes
3931 * managed by the protocol layer.
3933 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3935 mtip_hw_sysfs_init(dd, kobj);
3938 mtip_hw_debugfs_init(dd);
3940 if (dd->mtip_svc_handler) {
3941 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
3942 return rv; /* service thread created for handling rebuild */
3945 start_service_thread:
3946 sprintf(thd_name, "mtip_svc_thd_%02d", index);
3947 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
3948 dd, dd->numa_node, thd_name);
3950 if (IS_ERR(dd->mtip_svc_handler)) {
3951 dev_err(&dd->pdev->dev, "service thread failed to start\n");
3952 dd->mtip_svc_handler = NULL;
3954 goto kthread_run_error;
3956 wake_up_process(dd->mtip_svc_handler);
3957 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3958 rv = wait_for_rebuild;
3963 mtip_hw_debugfs_exit(dd);
3965 /* Delete our gendisk. This also removes the device from /dev */
3966 del_gendisk(dd->disk);
3968 read_capacity_error:
3969 blk_cleanup_queue(dd->queue);
3971 block_queue_alloc_init_error:
3973 spin_lock(&rssd_index_lock);
3974 ida_remove(&rssd_index_ida, index);
3975 spin_unlock(&rssd_index_lock);
3981 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
3983 protocol_init_error:
3988 * Block layer deinitialization function.
3990 * Called by the PCI layer as each P320 device is removed.
3992 * @dd Pointer to the driver data structure.
3997 static int mtip_block_remove(struct driver_data *dd)
3999 struct kobject *kobj;
4001 if (dd->mtip_svc_handler) {
4002 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4003 wake_up_interruptible(&dd->port->svc_wait);
4004 kthread_stop(dd->mtip_svc_handler);
4007 /* Clean up the sysfs attributes, if created */
4008 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4009 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4011 mtip_hw_sysfs_exit(dd, kobj);
4015 mtip_hw_debugfs_exit(dd);
4018 * Delete our gendisk structure. This also removes the device
4022 if (dd->disk->queue)
4023 del_gendisk(dd->disk);
4028 spin_lock(&rssd_index_lock);
4029 ida_remove(&rssd_index_ida, dd->index);
4030 spin_unlock(&rssd_index_lock);
4032 blk_cleanup_queue(dd->queue);
4036 /* De-initialize the protocol layer. */
4043 * Function called by the PCI layer when just before the
4044 * machine shuts down.
4046 * If a protocol layer shutdown function is present it will be called
4049 * @dd Pointer to the driver data structure.
4054 static int mtip_block_shutdown(struct driver_data *dd)
4056 dev_info(&dd->pdev->dev,
4057 "Shutting down %s ...\n", dd->disk->disk_name);
4059 /* Delete our gendisk structure, and cleanup the blk queue. */
4061 if (dd->disk->queue)
4062 del_gendisk(dd->disk);
4068 spin_lock(&rssd_index_lock);
4069 ida_remove(&rssd_index_ida, dd->index);
4070 spin_unlock(&rssd_index_lock);
4072 blk_cleanup_queue(dd->queue);
4076 mtip_hw_shutdown(dd);
4080 static int mtip_block_suspend(struct driver_data *dd)
4082 dev_info(&dd->pdev->dev,
4083 "Suspending %s ...\n", dd->disk->disk_name);
4084 mtip_hw_suspend(dd);
4088 static int mtip_block_resume(struct driver_data *dd)
4090 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4091 dd->disk->disk_name);
4096 static void drop_cpu(int cpu)
4101 static int get_least_used_cpu_on_node(int node)
4103 int cpu, least_used_cpu, least_cnt;
4104 const struct cpumask *node_mask;
4106 node_mask = cpumask_of_node(node);
4107 least_used_cpu = cpumask_first(node_mask);
4108 least_cnt = cpu_use[least_used_cpu];
4109 cpu = least_used_cpu;
4111 for_each_cpu(cpu, node_mask) {
4112 if (cpu_use[cpu] < least_cnt) {
4113 least_used_cpu = cpu;
4114 least_cnt = cpu_use[cpu];
4117 cpu_use[least_used_cpu]++;
4118 return least_used_cpu;
4121 /* Helper for selecting a node in round robin mode */
4122 static inline int mtip_get_next_rr_node(void)
4124 static int next_node = -1;
4126 if (next_node == -1) {
4127 next_node = first_online_node;
4131 next_node = next_online_node(next_node);
4132 if (next_node == MAX_NUMNODES)
4133 next_node = first_online_node;
4137 static DEFINE_HANDLER(0);
4138 static DEFINE_HANDLER(1);
4139 static DEFINE_HANDLER(2);
4140 static DEFINE_HANDLER(3);
4141 static DEFINE_HANDLER(4);
4142 static DEFINE_HANDLER(5);
4143 static DEFINE_HANDLER(6);
4144 static DEFINE_HANDLER(7);
4147 * Called for each supported PCI device detected.
4149 * This function allocates the private data structure, enables the
4150 * PCI device and then calls the block layer initialization function.
4153 * 0 on success else an error code.
4155 static int mtip_pci_probe(struct pci_dev *pdev,
4156 const struct pci_device_id *ent)
4159 struct driver_data *dd = NULL;
4161 const struct cpumask *node_mask;
4162 int cpu, i = 0, j = 0;
4163 int my_node = NUMA_NO_NODE;
4165 /* Allocate memory for this devices private data. */
4166 my_node = pcibus_to_node(pdev->bus);
4167 if (my_node != NUMA_NO_NODE) {
4168 if (!node_online(my_node))
4169 my_node = mtip_get_next_rr_node();
4171 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4172 my_node = mtip_get_next_rr_node();
4174 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4175 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
4176 cpu_to_node(smp_processor_id()), smp_processor_id());
4178 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
4181 "Unable to allocate memory for driver data\n");
4185 /* Attach the private data to this PCI device. */
4186 pci_set_drvdata(pdev, dd);
4188 rv = pcim_enable_device(pdev);
4190 dev_err(&pdev->dev, "Unable to enable device\n");
4194 /* Map BAR5 to memory. */
4195 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4197 dev_err(&pdev->dev, "Unable to map regions\n");
4201 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4202 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4205 rv = pci_set_consistent_dma_mask(pdev,
4208 dev_warn(&pdev->dev,
4209 "64-bit DMA enable failed\n");
4215 /* Copy the info we may need later into the private data structure. */
4216 dd->major = mtip_major;
4217 dd->instance = instance;
4219 dd->numa_node = my_node;
4221 memset(dd->workq_name, 0, 32);
4222 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4224 dd->isr_workq = create_workqueue(dd->workq_name);
4225 if (!dd->isr_workq) {
4226 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
4227 goto block_initialize_err;
4230 memset(cpu_list, 0, sizeof(cpu_list));
4232 node_mask = cpumask_of_node(dd->numa_node);
4233 if (!cpumask_empty(node_mask)) {
4234 for_each_cpu(cpu, node_mask)
4236 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4237 j = strlen(cpu_list);
4240 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4242 topology_physical_package_id(cpumask_first(node_mask)),
4243 nr_cpus_node(dd->numa_node),
4246 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4248 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4249 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4250 cpu_to_node(dd->isr_binding), dd->isr_binding);
4252 /* first worker context always runs in ISR */
4253 dd->work[0].cpu_binding = dd->isr_binding;
4254 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4255 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4256 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4257 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4258 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4259 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4260 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4262 /* Log the bindings */
4263 for_each_present_cpu(cpu) {
4264 memset(cpu_list, 0, sizeof(cpu_list));
4265 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4266 if (dd->work[i].cpu_binding == cpu) {
4267 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4268 j = strlen(cpu_list);
4272 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4275 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4276 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4277 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4278 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4279 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4280 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4281 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4282 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4284 pci_set_master(pdev);
4285 if (pci_enable_msi(pdev)) {
4286 dev_warn(&pdev->dev,
4287 "Unable to enable MSI interrupt.\n");
4288 goto block_initialize_err;
4291 /* Initialize the block layer. */
4292 rv = mtip_block_initialize(dd);
4295 "Unable to initialize block layer\n");
4296 goto block_initialize_err;
4300 * Increment the instance count so that each device has a unique
4304 if (rv != MTIP_FTL_REBUILD_MAGIC)
4305 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
4308 block_initialize_err:
4309 pci_disable_msi(pdev);
4310 if (dd->isr_workq) {
4311 flush_workqueue(dd->isr_workq);
4312 destroy_workqueue(dd->isr_workq);
4313 drop_cpu(dd->work[0].cpu_binding);
4314 drop_cpu(dd->work[1].cpu_binding);
4315 drop_cpu(dd->work[2].cpu_binding);
4318 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4322 pci_set_drvdata(pdev, NULL);
4329 * Called for each probed device when the device is removed or the
4330 * driver is unloaded.
4335 static void mtip_pci_remove(struct pci_dev *pdev)
4337 struct driver_data *dd = pci_get_drvdata(pdev);
4340 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
4342 if (mtip_check_surprise_removal(pdev)) {
4343 while (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
4346 if (counter == 10) {
4347 /* Cleanup the outstanding commands */
4348 mtip_command_cleanup(dd);
4354 /* Clean up the block layer. */
4355 mtip_block_remove(dd);
4357 if (dd->isr_workq) {
4358 flush_workqueue(dd->isr_workq);
4359 destroy_workqueue(dd->isr_workq);
4360 drop_cpu(dd->work[0].cpu_binding);
4361 drop_cpu(dd->work[1].cpu_binding);
4362 drop_cpu(dd->work[2].cpu_binding);
4365 pci_disable_msi(pdev);
4368 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4372 * Called for each probed device when the device is suspended.
4378 static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4381 struct driver_data *dd = pci_get_drvdata(pdev);
4385 "Driver private datastructure is NULL\n");
4389 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
4391 /* Disable ports & interrupts then send standby immediate */
4392 rv = mtip_block_suspend(dd);
4395 "Failed to suspend controller\n");
4400 * Save the pci config space to pdev structure &
4401 * disable the device
4403 pci_save_state(pdev);
4404 pci_disable_device(pdev);
4406 /* Move to Low power state*/
4407 pci_set_power_state(pdev, PCI_D3hot);
4413 * Called for each probed device when the device is resumed.
4419 static int mtip_pci_resume(struct pci_dev *pdev)
4422 struct driver_data *dd;
4424 dd = pci_get_drvdata(pdev);
4427 "Driver private datastructure is NULL\n");
4431 /* Move the device to active State */
4432 pci_set_power_state(pdev, PCI_D0);
4434 /* Restore PCI configuration space */
4435 pci_restore_state(pdev);
4437 /* Enable the PCI device*/
4438 rv = pcim_enable_device(pdev);
4441 "Failed to enable card during resume\n");
4444 pci_set_master(pdev);
4447 * Calls hbaReset, initPort, & startPort function
4448 * then enables interrupts
4450 rv = mtip_block_resume(dd);
4452 dev_err(&pdev->dev, "Unable to resume\n");
4455 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
4466 static void mtip_pci_shutdown(struct pci_dev *pdev)
4468 struct driver_data *dd = pci_get_drvdata(pdev);
4470 mtip_block_shutdown(dd);
4473 /* Table of device ids supported by this driver. */
4474 static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
4475 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4476 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4477 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4478 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4479 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4480 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4481 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
4485 /* Structure that describes the PCI driver functions. */
4486 static struct pci_driver mtip_pci_driver = {
4487 .name = MTIP_DRV_NAME,
4488 .id_table = mtip_pci_tbl,
4489 .probe = mtip_pci_probe,
4490 .remove = mtip_pci_remove,
4491 .suspend = mtip_pci_suspend,
4492 .resume = mtip_pci_resume,
4493 .shutdown = mtip_pci_shutdown,
4496 MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4499 * Module initialization function.
4501 * Called once when the module is loaded. This function allocates a major
4502 * block device number to the Cyclone devices and registers the PCI layer
4506 * 0 on success else error code.
4508 static int __init mtip_init(void)
4512 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
4514 /* Allocate a major block device number to use with this driver. */
4515 error = register_blkdev(0, MTIP_DRV_NAME);
4517 pr_err("Unable to register block device (%d)\n",
4524 dfs_parent = debugfs_create_dir("rssd", NULL);
4525 if (IS_ERR_OR_NULL(dfs_parent)) {
4526 pr_warn("Error creating debugfs parent\n");
4531 /* Register our PCI operations. */
4532 error = pci_register_driver(&mtip_pci_driver);
4534 debugfs_remove(dfs_parent);
4535 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4542 * Module de-initialization function.
4544 * Called once when the module is unloaded. This function deallocates
4545 * the major block device number allocated by mtip_init() and
4546 * unregisters the PCI layer of the driver.
4551 static void __exit mtip_exit(void)
4553 debugfs_remove_recursive(dfs_parent);
4555 /* Release the allocated major block device number. */
4556 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4558 /* Unregister the PCI driver. */
4559 pci_unregister_driver(&mtip_pci_driver);
4562 MODULE_AUTHOR("Micron Technology, Inc");
4563 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4564 MODULE_LICENSE("GPL");
4565 MODULE_VERSION(MTIP_DRV_VERSION);
4567 module_init(mtip_init);
4568 module_exit(mtip_exit);