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/genhd.h>
32 #include <linux/blkdev.h>
33 #include <linux/bio.h>
34 #include <linux/dma-mapping.h>
35 #include <linux/idr.h>
36 #include <../drivers/ata/ahci.h>
39 #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
40 #define HW_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (MTIP_MAX_SG * 16))
41 #define HW_CMD_TBL_AR_SZ (HW_CMD_TBL_SZ * MTIP_MAX_COMMAND_SLOTS)
42 #define HW_PORT_PRIV_DMA_SZ \
43 (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
45 #define HOST_HSORG 0xFC
46 #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
47 #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
48 #define HSORG_HWREV 0xFF00
49 #define HSORG_STYLE 0x8
50 #define HSORG_SLOTGROUPS 0x7
52 #define PORT_COMMAND_ISSUE 0x38
53 #define PORT_SDBV 0x7C
55 #define PORT_OFFSET 0x100
56 #define PORT_MEM_SIZE 0x80
58 #define PORT_IRQ_ERR \
59 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
60 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
61 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
63 #define PORT_IRQ_LEGACY \
64 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
65 #define PORT_IRQ_HANDLED \
66 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
67 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
68 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
69 #define DEF_PORT_IRQ \
70 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
73 #define MTIP_PRODUCT_UNKNOWN 0x00
74 #define MTIP_PRODUCT_ASICFPGA 0x11
76 /* Device instance number, incremented each time a device is probed. */
80 * Global variable used to hold the major block device number
81 * allocated in mtip_init().
85 static DEFINE_SPINLOCK(rssd_index_lock);
86 static DEFINE_IDA(rssd_index_ida);
89 struct mtip_compat_ide_task_request_s {
92 ide_reg_valid_t out_flags;
93 ide_reg_valid_t in_flags;
96 compat_ulong_t out_size;
97 compat_ulong_t in_size;
101 static int mtip_exec_internal_command(struct mtip_port *port,
108 unsigned long timeout);
111 * Obtain an empty command slot.
113 * This function needs to be reentrant since it could be called
114 * at the same time on multiple CPUs. The allocation of the
115 * command slot must be atomic.
117 * @port Pointer to the port data structure.
120 * >= 0 Index of command slot obtained.
121 * -1 No command slots available.
123 static int get_slot(struct mtip_port *port)
126 unsigned int num_command_slots = port->dd->slot_groups * 32;
129 * Try 10 times, because there is a small race here.
130 * that's ok, because it's still cheaper than a lock.
132 * Race: Since this section is not protected by lock, same bit
133 * could be chosen by different process contexts running in
134 * different processor. So instead of costly lock, we are going
137 for (i = 0; i < 10; i++) {
138 slot = find_next_zero_bit(port->allocated,
139 num_command_slots, 1);
140 if ((slot < num_command_slots) &&
141 (!test_and_set_bit(slot, port->allocated)))
144 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
146 if (mtip_check_surprise_removal(port->dd->pdev)) {
147 /* Device not present, clean outstanding commands */
148 mtip_command_cleanup(port->dd);
154 * Release a command slot.
156 * @port Pointer to the port data structure.
157 * @tag Tag of command to release
162 static inline void release_slot(struct mtip_port *port, int tag)
164 smp_mb__before_clear_bit();
165 clear_bit(tag, port->allocated);
166 smp_mb__after_clear_bit();
170 * Issue a command to the hardware.
172 * Set the appropriate bit in the s_active and Command Issue hardware
173 * registers, causing hardware command processing to begin.
175 * @port Pointer to the port structure.
176 * @tag The tag of the command to be issued.
181 static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
183 unsigned long flags = 0;
185 atomic_set(&port->commands[tag].active, 1);
187 spin_lock_irqsave(&port->cmd_issue_lock, flags);
189 writel((1 << MTIP_TAG_BIT(tag)),
190 port->s_active[MTIP_TAG_INDEX(tag)]);
191 writel((1 << MTIP_TAG_BIT(tag)),
192 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
194 spin_unlock_irqrestore(&port->cmd_issue_lock, flags);
198 * Called periodically to see if any read/write commands are
199 * taking too long to complete.
201 * @data Pointer to the PORT data structure.
206 void mtip_timeout_function(unsigned long int data)
208 struct mtip_port *port = (struct mtip_port *) data;
209 struct host_to_dev_fis *fis;
210 struct mtip_cmd *command;
211 int tag, cmdto_cnt = 0;
212 unsigned int bit, group;
213 unsigned int num_command_slots = port->dd->slot_groups * 32;
218 if (atomic_read(&port->dd->resumeflag) == true) {
219 mod_timer(&port->cmd_timer,
220 jiffies + msecs_to_jiffies(30000));
224 for (tag = 0; tag < num_command_slots; tag++) {
226 * Skip internal command slot as it has
227 * its own timeout mechanism
229 if (tag == MTIP_TAG_INTERNAL)
232 if (atomic_read(&port->commands[tag].active) &&
233 (time_after(jiffies, port->commands[tag].comp_time))) {
237 command = &port->commands[tag];
238 fis = (struct host_to_dev_fis *) command->command;
240 dev_warn(&port->dd->pdev->dev,
241 "Timeout for command tag %d\n", tag);
245 atomic_inc(&port->dd->eh_active);
248 * Clear the completed bit. This should prevent
249 * any interrupt handlers from trying to retire
252 writel(1 << bit, port->completed[group]);
254 /* Call the async completion callback. */
255 if (likely(command->async_callback))
256 command->async_callback(command->async_data,
258 command->async_callback = NULL;
259 command->comp_func = NULL;
261 /* Unmap the DMA scatter list entries */
262 dma_unmap_sg(&port->dd->pdev->dev,
264 command->scatter_ents,
268 * Clear the allocated bit and active tag for the
271 atomic_set(&port->commands[tag].active, 0);
272 release_slot(port, tag);
279 dev_warn(&port->dd->pdev->dev,
280 "%d commands timed out: restarting port",
282 mtip_restart_port(port);
283 atomic_dec(&port->dd->eh_active);
286 /* Restart the timer */
287 mod_timer(&port->cmd_timer,
288 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
292 * IO completion function.
294 * This completion function is called by the driver ISR when a
295 * command that was issued by the kernel completes. It first calls the
296 * asynchronous completion function which normally calls back into the block
297 * layer passing the asynchronous callback data, then unmaps the
298 * scatter list associated with the completed command, and finally
299 * clears the allocated bit associated with the completed command.
301 * @port Pointer to the port data structure.
302 * @tag Tag of the command.
303 * @data Pointer to driver_data.
304 * @status Completion status.
309 static void mtip_async_complete(struct mtip_port *port,
314 struct mtip_cmd *command;
315 struct driver_data *dd = data;
316 int cb_status = status ? -EIO : 0;
318 if (unlikely(!dd) || unlikely(!port))
321 command = &port->commands[tag];
323 if (unlikely(status == PORT_IRQ_TF_ERR)) {
324 dev_warn(&port->dd->pdev->dev,
325 "Command tag %d failed due to TFE\n", tag);
328 /* Upper layer callback */
329 if (likely(command->async_callback))
330 command->async_callback(command->async_data, cb_status);
332 command->async_callback = NULL;
333 command->comp_func = NULL;
335 /* Unmap the DMA scatter list entries */
336 dma_unmap_sg(&dd->pdev->dev,
338 command->scatter_ents,
341 /* Clear the allocated and active bits for the command */
342 atomic_set(&port->commands[tag].active, 0);
343 release_slot(port, tag);
349 * Internal command completion callback function.
351 * This function is normally called by the driver ISR when an internal
352 * command completed. This function signals the command completion by
353 * calling complete().
355 * @port Pointer to the port data structure.
356 * @tag Tag of the command that has completed.
357 * @data Pointer to a completion structure.
358 * @status Completion status.
363 static void mtip_completion(struct mtip_port *port,
368 struct mtip_cmd *command = &port->commands[tag];
369 struct completion *waiting = data;
370 if (unlikely(status == PORT_IRQ_TF_ERR))
371 dev_warn(&port->dd->pdev->dev,
372 "Internal command %d completed with TFE\n", tag);
374 command->async_callback = NULL;
375 command->comp_func = NULL;
381 * Enable/disable the reception of FIS
383 * @port Pointer to the port data structure
384 * @enable 1 to enable, 0 to disable
387 * Previous state: 1 enabled, 0 disabled
389 static int mtip_enable_fis(struct mtip_port *port, int enable)
393 /* enable FIS reception */
394 tmp = readl(port->mmio + PORT_CMD);
396 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
398 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
401 readl(port->mmio + PORT_CMD);
403 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
407 * Enable/disable the DMA engine
409 * @port Pointer to the port data structure
410 * @enable 1 to enable, 0 to disable
413 * Previous state: 1 enabled, 0 disabled.
415 static int mtip_enable_engine(struct mtip_port *port, int enable)
419 /* enable FIS reception */
420 tmp = readl(port->mmio + PORT_CMD);
422 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
424 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
426 readl(port->mmio + PORT_CMD);
427 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
431 * Enables the port DMA engine and FIS reception.
436 static inline void mtip_start_port(struct mtip_port *port)
438 /* Enable FIS reception */
439 mtip_enable_fis(port, 1);
441 /* Enable the DMA engine */
442 mtip_enable_engine(port, 1);
446 * Deinitialize a port by disabling port interrupts, the DMA engine,
449 * @port Pointer to the port structure
454 static inline void mtip_deinit_port(struct mtip_port *port)
456 /* Disable interrupts on this port */
457 writel(0, port->mmio + PORT_IRQ_MASK);
459 /* Disable the DMA engine */
460 mtip_enable_engine(port, 0);
462 /* Disable FIS reception */
463 mtip_enable_fis(port, 0);
469 * This function deinitializes the port by calling mtip_deinit_port() and
470 * then initializes it by setting the command header and RX FIS addresses,
471 * clearing the SError register and any pending port interrupts before
472 * re-enabling the default set of port interrupts.
474 * @port Pointer to the port structure.
479 static void mtip_init_port(struct mtip_port *port)
482 mtip_deinit_port(port);
484 /* Program the command list base and FIS base addresses */
485 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
486 writel((port->command_list_dma >> 16) >> 16,
487 port->mmio + PORT_LST_ADDR_HI);
488 writel((port->rxfis_dma >> 16) >> 16,
489 port->mmio + PORT_FIS_ADDR_HI);
492 writel(port->command_list_dma & 0xffffffff,
493 port->mmio + PORT_LST_ADDR);
494 writel(port->rxfis_dma & 0xffffffff, port->mmio + PORT_FIS_ADDR);
497 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
499 /* reset the completed registers.*/
500 for (i = 0; i < port->dd->slot_groups; i++)
501 writel(0xFFFFFFFF, port->completed[i]);
503 /* Clear any pending interrupts for this port */
504 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
506 /* Enable port interrupts */
507 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
511 * Reset the HBA (without sleeping)
513 * Just like hba_reset, except does not call sleep, so can be
514 * run from interrupt/tasklet context.
516 * @dd Pointer to the driver data structure.
519 * 0 The reset was successful.
520 * -1 The HBA Reset bit did not clear.
522 int hba_reset_nosleep(struct driver_data *dd)
524 unsigned long timeout;
526 /* Chip quirk: quiesce any chip function */
529 /* Set the reset bit */
530 writel(HOST_RESET, dd->mmio + HOST_CTL);
533 readl(dd->mmio + HOST_CTL);
536 * Wait 10ms then spin for up to 1 second
537 * waiting for reset acknowledgement
539 timeout = jiffies + msecs_to_jiffies(1000);
541 while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
542 && time_before(jiffies, timeout))
545 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
554 * @port Pointer to the port data structure.
559 void mtip_restart_port(struct mtip_port *port)
561 unsigned long timeout;
563 /* Disable the DMA engine */
564 mtip_enable_engine(port, 0);
566 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
567 timeout = jiffies + msecs_to_jiffies(500);
568 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
569 && time_before(jiffies, timeout))
573 * Chip quirk: escalate to hba reset if
574 * PxCMD.CR not clear after 500 ms
576 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
577 dev_warn(&port->dd->pdev->dev,
578 "PxCMD.CR not clear, escalating reset\n");
580 if (hba_reset_nosleep(port->dd))
581 dev_err(&port->dd->pdev->dev,
582 "HBA reset escalation failed.\n");
584 /* 30 ms delay before com reset to quiesce chip */
588 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
591 writel(readl(port->mmio + PORT_SCR_CTL) |
592 1, port->mmio + PORT_SCR_CTL);
593 readl(port->mmio + PORT_SCR_CTL);
595 /* Wait 1 ms to quiesce chip function */
596 timeout = jiffies + msecs_to_jiffies(1);
597 while (time_before(jiffies, timeout))
600 /* Clear PxSCTL.DET */
601 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
602 port->mmio + PORT_SCR_CTL);
603 readl(port->mmio + PORT_SCR_CTL);
605 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
606 timeout = jiffies + msecs_to_jiffies(500);
607 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
608 && time_before(jiffies, timeout))
611 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
612 dev_warn(&port->dd->pdev->dev,
613 "COM reset failed\n");
615 /* Clear SError, the PxSERR.DIAG.x should be set so clear it */
616 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
618 /* Enable the DMA engine */
619 mtip_enable_engine(port, 1);
623 * Helper function for tag logging
625 static void print_tags(struct driver_data *dd,
627 unsigned long *tagbits)
629 unsigned int tag, count = 0;
631 for (tag = 0; tag < (dd->slot_groups) * 32; tag++) {
632 if (test_bit(tag, tagbits))
636 dev_info(&dd->pdev->dev, "%s [%i tags]\n", msg, count);
642 * @dd Pointer to the DRIVER_DATA structure.
647 static void mtip_handle_tfe(struct driver_data *dd)
649 int group, tag, bit, reissue;
650 struct mtip_port *port;
651 struct mtip_cmd *command;
653 struct host_to_dev_fis *fis;
654 unsigned long tagaccum[SLOTBITS_IN_LONGS];
656 dev_warn(&dd->pdev->dev, "Taskfile error\n");
660 /* Stop the timer to prevent command timeouts. */
661 del_timer(&port->cmd_timer);
664 atomic_inc(&dd->eh_active);
666 /* Loop through all the groups */
667 for (group = 0; group < dd->slot_groups; group++) {
668 completed = readl(port->completed[group]);
670 /* clear completed status register in the hardware.*/
671 writel(completed, port->completed[group]);
673 /* clear the tag accumulator */
674 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
676 /* Process successfully completed commands */
677 for (bit = 0; bit < 32 && completed; bit++) {
678 if (!(completed & (1<<bit)))
680 tag = (group << 5) + bit;
682 /* Skip the internal command slot */
683 if (tag == MTIP_TAG_INTERNAL)
686 command = &port->commands[tag];
687 if (likely(command->comp_func)) {
688 set_bit(tag, tagaccum);
689 atomic_set(&port->commands[tag].active, 0);
690 command->comp_func(port,
695 dev_err(&port->dd->pdev->dev,
696 "Missing completion func for tag %d",
698 if (mtip_check_surprise_removal(dd->pdev)) {
699 mtip_command_cleanup(dd);
700 /* don't proceed further */
706 print_tags(dd, "TFE tags completed:", tagaccum);
708 /* Restart the port */
710 mtip_restart_port(port);
712 /* clear the tag accumulator */
713 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
715 /* Loop through all the groups */
716 for (group = 0; group < dd->slot_groups; group++) {
717 for (bit = 0; bit < 32; bit++) {
719 tag = (group << 5) + bit;
721 /* If the active bit is set re-issue the command */
722 if (atomic_read(&port->commands[tag].active) == 0)
725 fis = (struct host_to_dev_fis *)
726 port->commands[tag].command;
728 /* Should re-issue? */
729 if (tag == MTIP_TAG_INTERNAL ||
730 fis->command == ATA_CMD_SET_FEATURES)
734 * First check if this command has
735 * exceeded its retries.
738 (port->commands[tag].retries-- > 0)) {
740 set_bit(tag, tagaccum);
742 /* Update the timeout value. */
743 port->commands[tag].comp_time =
744 jiffies + msecs_to_jiffies(
745 MTIP_NCQ_COMMAND_TIMEOUT_MS);
746 /* Re-issue the command. */
747 mtip_issue_ncq_command(port, tag);
752 /* Retire a command that will not be reissued */
753 dev_warn(&port->dd->pdev->dev,
754 "retiring tag %d\n", tag);
755 atomic_set(&port->commands[tag].active, 0);
757 if (port->commands[tag].comp_func)
758 port->commands[tag].comp_func(
761 port->commands[tag].comp_data,
764 dev_warn(&port->dd->pdev->dev,
765 "Bad completion for tag %d\n",
769 print_tags(dd, "TFE tags reissued:", tagaccum);
771 /* Decrement eh_active */
772 atomic_dec(&dd->eh_active);
774 mod_timer(&port->cmd_timer,
775 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
779 * Handle a set device bits interrupt
781 static inline void mtip_process_sdbf(struct driver_data *dd)
783 struct mtip_port *port = dd->port;
786 struct mtip_cmd *command;
788 /* walk all bits in all slot groups */
789 for (group = 0; group < dd->slot_groups; group++) {
790 completed = readl(port->completed[group]);
792 /* clear completed status register in the hardware.*/
793 writel(completed, port->completed[group]);
795 /* Process completed commands. */
797 (bit < 32) && completed;
798 bit++, completed >>= 1) {
799 if (completed & 0x01) {
800 tag = (group << 5) | bit;
802 /* skip internal command slot. */
803 if (unlikely(tag == MTIP_TAG_INTERNAL))
806 command = &port->commands[tag];
808 /* make internal callback */
809 if (likely(command->comp_func)) {
816 dev_warn(&dd->pdev->dev,
821 if (mtip_check_surprise_removal(
823 mtip_command_cleanup(dd);
833 * Process legacy pio and d2h interrupts
835 static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
837 struct mtip_port *port = dd->port;
838 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
840 if (port->internal_cmd_in_progress &&
842 !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
843 & (1 << MTIP_TAG_INTERNAL))) {
844 if (cmd->comp_func) {
853 dev_warn(&dd->pdev->dev, "IRQ status 0x%x ignored.\n", port_stat);
859 * Demux and handle errors
861 static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
863 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
866 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
867 dev_warn(&dd->pdev->dev,
868 "Clearing PxSERR.DIAG.x\n");
869 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
872 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
873 dev_warn(&dd->pdev->dev,
874 "Clearing PxSERR.DIAG.n\n");
875 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
878 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
879 dev_warn(&dd->pdev->dev,
880 "Port stat errors %x unhandled\n",
881 (port_stat & ~PORT_IRQ_HANDLED));
885 static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
887 struct driver_data *dd = (struct driver_data *) data;
888 struct mtip_port *port = dd->port;
889 u32 hba_stat, port_stat;
892 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
896 /* Acknowledge the interrupt status on the port.*/
897 port_stat = readl(port->mmio + PORT_IRQ_STAT);
898 writel(port_stat, port->mmio + PORT_IRQ_STAT);
900 /* Demux port status */
901 if (likely(port_stat & PORT_IRQ_SDB_FIS))
902 mtip_process_sdbf(dd);
904 if (unlikely(port_stat & PORT_IRQ_ERR)) {
905 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
906 mtip_command_cleanup(dd);
907 /* don't proceed further */
911 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
914 if (unlikely(port_stat & PORT_IRQ_LEGACY))
915 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
918 /* acknowledge interrupt */
919 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
925 * Wrapper for mtip_handle_irq
926 * (ignores return code)
928 static void mtip_tasklet(unsigned long data)
930 mtip_handle_irq((struct driver_data *) data);
934 * HBA interrupt subroutine.
937 * @instance Pointer to the driver data structure.
940 * IRQ_HANDLED A HBA interrupt was pending and handled.
941 * IRQ_NONE This interrupt was not for the HBA.
943 static irqreturn_t mtip_irq_handler(int irq, void *instance)
945 struct driver_data *dd = instance;
946 tasklet_schedule(&dd->tasklet);
950 static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
952 atomic_set(&port->commands[tag].active, 1);
953 writel(1 << MTIP_TAG_BIT(tag),
954 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
958 * Wait for port to quiesce
960 * @port Pointer to port data structure
961 * @timeout Max duration to wait (ms)
965 * -EBUSY Commands still active
967 static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
970 unsigned int n, active;
972 to = jiffies + msecs_to_jiffies(timeout);
975 * Ignore s_active bit 0 of array element 0.
976 * This bit will always be set
978 active = readl(port->s_active[0]) & 0xfffffffe;
979 for (n = 1; n < port->dd->slot_groups; n++)
980 active |= readl(port->s_active[n]);
986 } while (time_before(jiffies, to));
988 return active ? -EBUSY : 0;
992 * Execute an internal command and wait for the completion.
994 * @port Pointer to the port data structure.
995 * @fis Pointer to the FIS that describes the command.
996 * @fisLen Length in WORDS of the FIS.
997 * @buffer DMA accessible for command data.
998 * @bufLen Length, in bytes, of the data buffer.
999 * @opts Command header options, excluding the FIS length
1000 * and the number of PRD entries.
1001 * @timeout Time in ms to wait for the command to complete.
1004 * 0 Command completed successfully.
1005 * -EFAULT The buffer address is not correctly aligned.
1006 * -EBUSY Internal command or other IO in progress.
1007 * -EAGAIN Time out waiting for command to complete.
1009 static int mtip_exec_internal_command(struct mtip_port *port,
1016 unsigned long timeout)
1018 struct mtip_cmd_sg *command_sg;
1019 DECLARE_COMPLETION_ONSTACK(wait);
1021 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
1023 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1024 if (buffer & 0x00000007) {
1025 dev_err(&port->dd->pdev->dev,
1026 "SG buffer is not 8 byte aligned\n");
1030 /* Only one internal command should be running at a time */
1031 if (test_and_set_bit(MTIP_TAG_INTERNAL, port->allocated)) {
1032 dev_warn(&port->dd->pdev->dev,
1033 "Internal command already active\n");
1036 port->internal_cmd_in_progress = 1;
1038 if (atomic == GFP_KERNEL) {
1039 /* wait for io to complete if non atomic */
1040 if (mtip_quiesce_io(port, 5000) < 0) {
1041 dev_warn(&port->dd->pdev->dev,
1042 "Failed to quiesce IO\n");
1043 release_slot(port, MTIP_TAG_INTERNAL);
1044 port->internal_cmd_in_progress = 0;
1048 /* Set the completion function and data for the command. */
1049 int_cmd->comp_data = &wait;
1050 int_cmd->comp_func = mtip_completion;
1053 /* Clear completion - we're going to poll */
1054 int_cmd->comp_data = NULL;
1055 int_cmd->comp_func = NULL;
1058 /* Copy the command to the command table */
1059 memcpy(int_cmd->command, fis, fisLen*4);
1061 /* Populate the SG list */
1062 int_cmd->command_header->opts =
1063 cpu_to_le32(opts | fisLen);
1065 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1067 command_sg->info = cpu_to_le32((bufLen-1) & 0x3fffff);
1068 command_sg->dba = cpu_to_le32(buffer & 0xffffffff);
1069 command_sg->dba_upper = cpu_to_le32((buffer >> 16) >> 16);
1071 int_cmd->command_header->opts |= cpu_to_le32((1 << 16));
1074 /* Populate the command header */
1075 int_cmd->command_header->byte_count = 0;
1077 /* Issue the command to the hardware */
1078 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1080 /* Poll if atomic, wait_for_completion otherwise */
1081 if (atomic == GFP_KERNEL) {
1082 /* Wait for the command to complete or timeout. */
1083 if (wait_for_completion_timeout(
1085 msecs_to_jiffies(timeout)) == 0) {
1086 dev_err(&port->dd->pdev->dev,
1087 "Internal command did not complete [%d]\n",
1092 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1093 & (1 << MTIP_TAG_INTERNAL)) {
1094 dev_warn(&port->dd->pdev->dev,
1095 "Retiring internal command but CI is 1.\n");
1099 /* Spin for <timeout> checking if command still outstanding */
1100 timeout = jiffies + msecs_to_jiffies(timeout);
1103 port->cmd_issue[MTIP_TAG_INTERNAL])
1104 & (1 << MTIP_TAG_INTERNAL))
1105 && time_before(jiffies, timeout))
1108 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1109 & (1 << MTIP_TAG_INTERNAL)) {
1110 dev_err(&port->dd->pdev->dev,
1111 "Internal command did not complete [%d]\n",
1117 /* Clear the allocated and active bits for the internal command. */
1118 atomic_set(&int_cmd->active, 0);
1119 release_slot(port, MTIP_TAG_INTERNAL);
1120 port->internal_cmd_in_progress = 0;
1126 * Byte-swap ATA ID strings.
1128 * ATA identify data contains strings in byte-swapped 16-bit words.
1129 * They must be swapped (on all architectures) to be usable as C strings.
1130 * This function swaps bytes in-place.
1132 * @buf The buffer location of the string
1133 * @len The number of bytes to swap
1138 static inline void ata_swap_string(u16 *buf, unsigned int len)
1141 for (i = 0; i < (len/2); i++)
1142 be16_to_cpus(&buf[i]);
1146 * Request the device identity information.
1148 * If a user space buffer is not specified, i.e. is NULL, the
1149 * identify information is still read from the drive and placed
1150 * into the identify data buffer (@e port->identify) in the
1151 * port data structure.
1152 * When the identify buffer contains valid identify information @e
1153 * port->identify_valid is non-zero.
1155 * @port Pointer to the port structure.
1156 * @user_buffer A user space buffer where the identify data should be
1160 * 0 Command completed successfully.
1161 * -EFAULT An error occurred while coping data to the user buffer.
1162 * -1 Command failed.
1164 static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1167 struct host_to_dev_fis fis;
1169 down_write(&port->dd->internal_sem);
1171 /* Build the FIS. */
1172 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1175 fis.command = ATA_CMD_ID_ATA;
1177 /* Set the identify information as invalid. */
1178 port->identify_valid = 0;
1180 /* Clear the identify information. */
1181 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1183 /* Execute the command. */
1184 if (mtip_exec_internal_command(port,
1188 sizeof(u16) * ATA_ID_WORDS,
1191 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1198 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1199 * perform field-sensitive swapping on the string fields.
1200 * See the kernel use of ata_id_string() for proof of this.
1202 #ifdef __LITTLE_ENDIAN
1203 ata_swap_string(port->identify + 27, 40); /* model string*/
1204 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1205 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1209 for (i = 0; i < ATA_ID_WORDS; i++)
1210 port->identify[i] = le16_to_cpu(port->identify[i]);
1214 /* Set the identify buffer as valid. */
1215 port->identify_valid = 1;
1221 ATA_ID_WORDS * sizeof(u16))) {
1228 up_write(&port->dd->internal_sem);
1233 * Issue a standby immediate command to the device.
1235 * @port Pointer to the port structure.
1238 * 0 Command was executed successfully.
1239 * -1 An error occurred while executing the command.
1241 static int mtip_standby_immediate(struct mtip_port *port)
1244 struct host_to_dev_fis fis;
1246 down_write(&port->dd->internal_sem);
1248 /* Build the FIS. */
1249 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1252 fis.command = ATA_CMD_STANDBYNOW1;
1254 /* Execute the command. Use a 15-second timeout for large drives. */
1255 rv = mtip_exec_internal_command(port,
1264 up_write(&port->dd->internal_sem);
1270 * Get the drive capacity.
1272 * @dd Pointer to the device data structure.
1273 * @sectors Pointer to the variable that will receive the sector count.
1276 * 1 Capacity was returned successfully.
1277 * 0 The identify information is invalid.
1279 bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
1281 struct mtip_port *port = dd->port;
1282 u64 total, raw0, raw1, raw2, raw3;
1283 raw0 = port->identify[100];
1284 raw1 = port->identify[101];
1285 raw2 = port->identify[102];
1286 raw3 = port->identify[103];
1287 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1289 return (bool) !!port->identify_valid;
1295 * Resets the HBA by setting the HBA Reset bit in the Global
1296 * HBA Control register. After setting the HBA Reset bit the
1297 * function waits for 1 second before reading the HBA Reset
1298 * bit to make sure it has cleared. If HBA Reset is not clear
1299 * an error is returned. Cannot be used in non-blockable
1302 * @dd Pointer to the driver data structure.
1305 * 0 The reset was successful.
1306 * -1 The HBA Reset bit did not clear.
1308 static int mtip_hba_reset(struct driver_data *dd)
1310 mtip_deinit_port(dd->port);
1312 /* Set the reset bit */
1313 writel(HOST_RESET, dd->mmio + HOST_CTL);
1316 readl(dd->mmio + HOST_CTL);
1318 /* Wait for reset to clear */
1321 /* Check the bit has cleared */
1322 if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
1323 dev_err(&dd->pdev->dev,
1324 "Reset bit did not clear.\n");
1332 * Display the identify command data.
1334 * @port Pointer to the port data structure.
1339 static void mtip_dump_identify(struct mtip_port *port)
1342 unsigned short revid;
1345 if (!port->identify_valid)
1348 strlcpy(cbuf, (char *)(port->identify+10), 21);
1349 dev_info(&port->dd->pdev->dev,
1350 "Serial No.: %s\n", cbuf);
1352 strlcpy(cbuf, (char *)(port->identify+23), 9);
1353 dev_info(&port->dd->pdev->dev,
1354 "Firmware Ver.: %s\n", cbuf);
1356 strlcpy(cbuf, (char *)(port->identify+27), 41);
1357 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1359 if (mtip_hw_get_capacity(port->dd, §ors))
1360 dev_info(&port->dd->pdev->dev,
1361 "Capacity: %llu sectors (%llu MB)\n",
1363 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1365 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
1366 switch (revid & 0xff) {
1368 strlcpy(cbuf, "A0", 3);
1371 strlcpy(cbuf, "A2", 3);
1374 strlcpy(cbuf, "?", 2);
1377 dev_info(&port->dd->pdev->dev,
1378 "Card Type: %s\n", cbuf);
1382 * Map the commands scatter list into the command table.
1384 * @command Pointer to the command.
1385 * @nents Number of scatter list entries.
1390 static inline void fill_command_sg(struct driver_data *dd,
1391 struct mtip_cmd *command,
1395 unsigned int dma_len;
1396 struct mtip_cmd_sg *command_sg;
1397 struct scatterlist *sg = command->sg;
1399 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1401 for (n = 0; n < nents; n++) {
1402 dma_len = sg_dma_len(sg);
1403 if (dma_len > 0x400000)
1404 dev_err(&dd->pdev->dev,
1405 "DMA segment length truncated\n");
1406 command_sg->info = cpu_to_le32((dma_len-1) & 0x3fffff);
1407 #if (BITS_PER_LONG == 64)
1408 *((unsigned long *) &command_sg->dba) =
1409 cpu_to_le64(sg_dma_address(sg));
1411 command_sg->dba = cpu_to_le32(sg_dma_address(sg));
1412 command_sg->dba_upper =
1413 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
1421 * @brief Execute a drive command.
1423 * return value 0 The command completed successfully.
1424 * return value -1 An error occurred while executing the command.
1426 int exec_drive_task(struct mtip_port *port, u8 *command)
1428 struct host_to_dev_fis fis;
1429 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1431 /* Lock the internal command semaphore. */
1432 down_write(&port->dd->internal_sem);
1434 /* Build the FIS. */
1435 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1438 fis.command = command[0];
1439 fis.features = command[1];
1440 fis.sect_count = command[2];
1441 fis.sector = command[3];
1442 fis.cyl_low = command[4];
1443 fis.cyl_hi = command[5];
1444 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1447 dbg_printk(MTIP_DRV_NAME "%s: User Command: cmd %x, feat %x, "
1448 "nsect %x, sect %x, lcyl %x, "
1449 "hcyl %x, sel %x\n",
1459 /* Execute the command. */
1460 if (mtip_exec_internal_command(port,
1467 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
1468 up_write(&port->dd->internal_sem);
1472 command[0] = reply->command; /* Status*/
1473 command[1] = reply->features; /* Error*/
1474 command[4] = reply->cyl_low;
1475 command[5] = reply->cyl_hi;
1477 dbg_printk(MTIP_DRV_NAME "%s: Completion Status: stat %x, "
1478 "err %x , cyl_lo %x cyl_hi %x\n",
1485 up_write(&port->dd->internal_sem);
1490 * @brief Execute a drive command.
1492 * @param port Pointer to the port data structure.
1493 * @param command Pointer to the user specified command parameters.
1494 * @param user_buffer Pointer to the user space buffer where read sector
1495 * data should be copied.
1497 * return value 0 The command completed successfully.
1498 * return value -EFAULT An error occurred while copying the completion
1499 * data to the user space buffer.
1500 * return value -1 An error occurred while executing the command.
1502 int exec_drive_command(struct mtip_port *port, u8 *command,
1503 void __user *user_buffer)
1505 struct host_to_dev_fis fis;
1506 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1508 /* Lock the internal command semaphore. */
1509 down_write(&port->dd->internal_sem);
1511 /* Build the FIS. */
1512 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1515 fis.command = command[0];
1516 fis.features = command[2];
1517 fis.sect_count = command[3];
1518 if (fis.command == ATA_CMD_SMART) {
1519 fis.sector = command[1];
1524 dbg_printk(MTIP_DRV_NAME
1525 "%s: User Command: cmd %x, sect %x, "
1526 "feat %x, sectcnt %x\n",
1533 memset(port->sector_buffer, 0x00, ATA_SECT_SIZE);
1535 /* Execute the command. */
1536 if (mtip_exec_internal_command(port,
1539 port->sector_buffer_dma,
1540 (command[3] != 0) ? ATA_SECT_SIZE : 0,
1543 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
1545 up_write(&port->dd->internal_sem);
1549 /* Collect the completion status. */
1550 command[0] = reply->command; /* Status*/
1551 command[1] = reply->features; /* Error*/
1552 command[2] = command[3];
1554 dbg_printk(MTIP_DRV_NAME
1555 "%s: Completion Status: stat %x, "
1562 if (user_buffer && command[3]) {
1563 if (copy_to_user(user_buffer,
1564 port->sector_buffer,
1565 ATA_SECT_SIZE * command[3])) {
1566 up_write(&port->dd->internal_sem);
1571 up_write(&port->dd->internal_sem);
1576 * Indicates whether a command has a single sector payload.
1578 * @command passed to the device to perform the certain event.
1579 * @features passed to the device to perform the certain event.
1582 * 1 command is one that always has a single sector payload,
1583 * regardless of the value in the Sector Count field.
1587 static unsigned int implicit_sector(unsigned char command,
1588 unsigned char features)
1590 unsigned int rv = 0;
1592 /* list of commands that have an implicit sector count of 1 */
1605 if (features == 0x03)
1609 if ((features == 0xD0) || (features == 0xD1))
1613 if ((features == 0xC2) || (features == 0xC3))
1621 * Executes a taskfile
1622 * See ide_taskfile_ioctl() for derivation
1624 static int exec_drive_taskfile(struct driver_data *dd,
1626 ide_task_request_t *req_task,
1629 struct host_to_dev_fis fis;
1630 struct host_to_dev_fis *reply;
1633 dma_addr_t outbuf_dma = 0;
1634 dma_addr_t inbuf_dma = 0;
1635 dma_addr_t dma_buffer = 0;
1637 unsigned int taskin = 0;
1638 unsigned int taskout = 0;
1640 unsigned int timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1641 unsigned int force_single_sector;
1642 unsigned int transfer_size;
1643 unsigned long task_file_data;
1644 int intotal = outtotal + req_task->out_size;
1646 taskout = req_task->out_size;
1647 taskin = req_task->in_size;
1648 /* 130560 = 512 * 0xFF*/
1649 if (taskin > 130560 || taskout > 130560) {
1655 outbuf = kzalloc(taskout, GFP_KERNEL);
1656 if (outbuf == NULL) {
1660 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
1664 outbuf_dma = pci_map_single(dd->pdev,
1668 if (outbuf_dma == 0) {
1672 dma_buffer = outbuf_dma;
1676 inbuf = kzalloc(taskin, GFP_KERNEL);
1677 if (inbuf == NULL) {
1682 if (copy_from_user(inbuf, buf + intotal, taskin)) {
1686 inbuf_dma = pci_map_single(dd->pdev,
1688 taskin, DMA_FROM_DEVICE);
1689 if (inbuf_dma == 0) {
1693 dma_buffer = inbuf_dma;
1696 /* only supports PIO and non-data commands from this ioctl. */
1697 switch (req_task->data_phase) {
1699 nsect = taskout / ATA_SECT_SIZE;
1700 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1703 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1705 case TASKFILE_NO_DATA:
1706 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
1713 /* Lock the internal command semaphore. */
1714 down_write(&dd->internal_sem);
1716 /* Build the FIS. */
1717 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1721 fis.command = req_task->io_ports[7];
1722 fis.features = req_task->io_ports[1];
1723 fis.sect_count = req_task->io_ports[2];
1724 fis.lba_low = req_task->io_ports[3];
1725 fis.lba_mid = req_task->io_ports[4];
1726 fis.lba_hi = req_task->io_ports[5];
1727 /* Clear the dev bit*/
1728 fis.device = req_task->io_ports[6] & ~0x10;
1730 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
1731 req_task->in_flags.all =
1732 IDE_TASKFILE_STD_IN_FLAGS |
1733 (IDE_HOB_STD_IN_FLAGS << 8);
1734 fis.lba_low_ex = req_task->hob_ports[3];
1735 fis.lba_mid_ex = req_task->hob_ports[4];
1736 fis.lba_hi_ex = req_task->hob_ports[5];
1737 fis.features_ex = req_task->hob_ports[1];
1738 fis.sect_cnt_ex = req_task->hob_ports[2];
1741 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
1744 force_single_sector = implicit_sector(fis.command, fis.features);
1746 if ((taskin || taskout) && (!fis.sect_count)) {
1748 fis.sect_count = nsect;
1750 if (!force_single_sector) {
1751 dev_warn(&dd->pdev->dev,
1752 "data movement but "
1753 "sect_count is 0\n");
1754 up_write(&dd->internal_sem);
1761 dbg_printk(MTIP_DRV_NAME
1762 "taskfile: cmd %x, feat %x, nsect %x,"
1763 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
1773 switch (fis.command) {
1774 case 0x92: /* Change timeout for Download Microcode to 60 seconds.*/
1777 case 0xf4: /* Change timeout for Security Erase Unit to 4 minutes.*/
1780 case 0xe0: /* Change timeout for standby immediate to 10 seconds.*/
1783 case 0xf7: /* Change timeout for vendor unique command to 10 secs */
1786 case 0xfa: /* Change timeout for vendor unique command to 10 secs */
1790 timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1794 /* Determine the correct transfer size.*/
1795 if (force_single_sector)
1796 transfer_size = ATA_SECT_SIZE;
1798 transfer_size = ATA_SECT_SIZE * fis.sect_count;
1800 /* Execute the command.*/
1801 if (mtip_exec_internal_command(dd->port,
1809 up_write(&dd->internal_sem);
1814 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
1816 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
1817 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
1818 req_task->io_ports[7] = reply->control;
1820 reply = dd->port->rxfis + RX_FIS_D2H_REG;
1821 req_task->io_ports[7] = reply->command;
1824 /* reclaim the DMA buffers.*/
1826 pci_unmap_single(dd->pdev, inbuf_dma,
1827 taskin, DMA_FROM_DEVICE);
1829 pci_unmap_single(dd->pdev, outbuf_dma,
1830 taskout, DMA_TO_DEVICE);
1834 /* return the ATA registers to the caller.*/
1835 req_task->io_ports[1] = reply->features;
1836 req_task->io_ports[2] = reply->sect_count;
1837 req_task->io_ports[3] = reply->lba_low;
1838 req_task->io_ports[4] = reply->lba_mid;
1839 req_task->io_ports[5] = reply->lba_hi;
1840 req_task->io_ports[6] = reply->device;
1842 if (req_task->out_flags.all & 1) {
1844 req_task->hob_ports[3] = reply->lba_low_ex;
1845 req_task->hob_ports[4] = reply->lba_mid_ex;
1846 req_task->hob_ports[5] = reply->lba_hi_ex;
1847 req_task->hob_ports[1] = reply->features_ex;
1848 req_task->hob_ports[2] = reply->sect_cnt_ex;
1851 /* Com rest after secure erase or lowlevel format */
1852 if (((fis.command == 0xF4) ||
1853 ((fis.command == 0xFC) &&
1854 (fis.features == 0x27 || fis.features == 0x72 ||
1855 fis.features == 0x62 || fis.features == 0x26))) &&
1856 !(reply->command & 1)) {
1857 mtip_restart_port(dd->port);
1860 dbg_printk(MTIP_DRV_NAME
1861 "%s: Completion: stat %x,"
1862 "err %x, sect_cnt %x, lbalo %x,"
1863 "lbamid %x, lbahi %x, dev %x\n",
1865 req_task->io_ports[7],
1866 req_task->io_ports[1],
1867 req_task->io_ports[2],
1868 req_task->io_ports[3],
1869 req_task->io_ports[4],
1870 req_task->io_ports[5],
1871 req_task->io_ports[6]);
1873 up_write(&dd->internal_sem);
1876 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
1882 if (copy_to_user(buf + intotal, inbuf, taskin)) {
1889 pci_unmap_single(dd->pdev, inbuf_dma,
1890 taskin, DMA_FROM_DEVICE);
1892 pci_unmap_single(dd->pdev, outbuf_dma,
1893 taskout, DMA_TO_DEVICE);
1901 * Handle IOCTL calls from the Block Layer.
1903 * This function is called by the Block Layer when it receives an IOCTL
1904 * command that it does not understand. If the IOCTL command is not supported
1905 * this function returns -ENOTTY.
1907 * @dd Pointer to the driver data structure.
1908 * @cmd IOCTL command passed from the Block Layer.
1909 * @arg IOCTL argument passed from the Block Layer.
1912 * 0 The IOCTL completed successfully.
1913 * -ENOTTY The specified command is not supported.
1914 * -EFAULT An error occurred copying data to a user space buffer.
1915 * -EIO An error occurred while executing the command.
1917 static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
1921 case HDIO_GET_IDENTITY:
1922 if (mtip_get_identify(dd->port, (void __user *) arg) < 0) {
1923 dev_warn(&dd->pdev->dev,
1924 "Unable to read identity\n");
1929 case HDIO_DRIVE_CMD:
1931 u8 drive_command[4];
1933 /* Copy the user command info to our buffer. */
1934 if (copy_from_user(drive_command,
1935 (void __user *) arg,
1936 sizeof(drive_command)))
1939 /* Execute the drive command. */
1940 if (exec_drive_command(dd->port,
1942 (void __user *) (arg+4)))
1945 /* Copy the status back to the users buffer. */
1946 if (copy_to_user((void __user *) arg,
1948 sizeof(drive_command)))
1953 case HDIO_DRIVE_TASK:
1955 u8 drive_command[7];
1957 /* Copy the user command info to our buffer. */
1958 if (copy_from_user(drive_command,
1959 (void __user *) arg,
1960 sizeof(drive_command)))
1963 /* Execute the drive command. */
1964 if (exec_drive_task(dd->port, drive_command))
1967 /* Copy the status back to the users buffer. */
1968 if (copy_to_user((void __user *) arg,
1970 sizeof(drive_command)))
1975 case HDIO_DRIVE_TASKFILE: {
1976 ide_task_request_t req_task;
1979 if (copy_from_user(&req_task, (void __user *) arg,
1983 outtotal = sizeof(req_task);
1985 ret = exec_drive_taskfile(dd, (void __user *) arg,
1986 &req_task, outtotal);
1988 if (copy_to_user((void __user *) arg, &req_task, sizeof(req_task)))
2001 * Submit an IO to the hw
2003 * This function is called by the block layer to issue an io
2004 * to the device. Upon completion, the callback function will
2005 * be called with the data parameter passed as the callback data.
2007 * @dd Pointer to the driver data structure.
2008 * @start First sector to read.
2009 * @nsect Number of sectors to read.
2010 * @nents Number of entries in scatter list for the read command.
2011 * @tag The tag of this read command.
2012 * @callback Pointer to the function that should be called
2013 * when the read completes.
2014 * @data Callback data passed to the callback function
2015 * when the read completes.
2016 * @barrier If non-zero, this command must be completed before
2017 * issuing any other commands.
2018 * @dir Direction (read or write)
2023 void mtip_hw_submit_io(struct driver_data *dd,
2033 struct host_to_dev_fis *fis;
2034 struct mtip_port *port = dd->port;
2035 struct mtip_cmd *command = &port->commands[tag];
2037 /* Map the scatter list for DMA access */
2039 nents = dma_map_sg(&dd->pdev->dev, command->sg,
2040 nents, DMA_FROM_DEVICE);
2042 nents = dma_map_sg(&dd->pdev->dev, command->sg,
2043 nents, DMA_TO_DEVICE);
2045 command->scatter_ents = nents;
2048 * The number of retries for this command before it is
2049 * reported as a failure to the upper layers.
2051 command->retries = MTIP_MAX_RETRIES;
2054 fis = command->command;
2058 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
2059 *((unsigned int *) &fis->lba_low) = (start & 0xffffff);
2060 *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xffffff);
2061 fis->device = 1 << 6;
2063 fis->device |= FUA_BIT;
2064 fis->features = nsect & 0xff;
2065 fis->features_ex = (nsect >> 8) & 0xff;
2066 fis->sect_count = ((tag << 3) | (tag >> 5));
2067 fis->sect_cnt_ex = 0;
2071 fill_command_sg(dd, command, nents);
2073 /* Populate the command header */
2074 command->command_header->opts = cpu_to_le32(
2075 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
2076 command->command_header->byte_count = 0;
2079 * Set the completion function and data for the command
2080 * within this layer.
2082 command->comp_data = dd;
2083 command->comp_func = mtip_async_complete;
2084 command->direction = (dir == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
2087 * Set the completion function and data for the command passed
2088 * from the upper layer.
2090 command->async_data = data;
2091 command->async_callback = callback;
2094 * Lock used to prevent this command from being issued
2095 * if an internal command is in progress.
2097 down_read(&port->dd->internal_sem);
2099 /* Issue the command to the hardware */
2100 mtip_issue_ncq_command(port, tag);
2102 /* Set the command's timeout value.*/
2103 port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
2104 MTIP_NCQ_COMMAND_TIMEOUT_MS);
2106 up_read(&port->dd->internal_sem);
2110 * Release a command slot.
2112 * @dd Pointer to the driver data structure.
2118 void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
2120 release_slot(dd->port, tag);
2124 * Obtain a command slot and return its associated scatter list.
2126 * @dd Pointer to the driver data structure.
2127 * @tag Pointer to an int that will receive the allocated command
2131 * Pointer to the scatter list for the allocated command slot
2132 * or NULL if no command slots are available.
2134 struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2138 * It is possible that, even with this semaphore, a thread
2139 * may think that no command slots are available. Therefore, we
2140 * need to make an attempt to get_slot().
2142 down(&dd->port->cmd_slot);
2143 *tag = get_slot(dd->port);
2145 if (unlikely(*tag < 0))
2148 return dd->port->commands[*tag].sg;
2152 * Sysfs register/status dump.
2154 * @dev Pointer to the device structure, passed by the kernrel.
2155 * @attr Pointer to the device_attribute structure passed by the kernel.
2156 * @buf Pointer to the char buffer that will receive the stats info.
2159 * The size, in bytes, of the data copied into buf.
2161 static ssize_t hw_show_registers(struct device *dev,
2162 struct device_attribute *attr,
2165 u32 group_allocated;
2166 struct driver_data *dd = dev_to_disk(dev)->private_data;
2170 size += sprintf(&buf[size], "%s:\ns_active:\n", __func__);
2172 for (n = 0; n < dd->slot_groups; n++)
2173 size += sprintf(&buf[size], "0x%08x\n",
2174 readl(dd->port->s_active[n]));
2176 size += sprintf(&buf[size], "Command Issue:\n");
2178 for (n = 0; n < dd->slot_groups; n++)
2179 size += sprintf(&buf[size], "0x%08x\n",
2180 readl(dd->port->cmd_issue[n]));
2182 size += sprintf(&buf[size], "Allocated:\n");
2184 for (n = 0; n < dd->slot_groups; n++) {
2185 if (sizeof(long) > sizeof(u32))
2187 dd->port->allocated[n/2] >> (32*(n&1));
2189 group_allocated = dd->port->allocated[n];
2190 size += sprintf(&buf[size], "0x%08x\n",
2194 size += sprintf(&buf[size], "completed:\n");
2196 for (n = 0; n < dd->slot_groups; n++)
2197 size += sprintf(&buf[size], "0x%08x\n",
2198 readl(dd->port->completed[n]));
2200 size += sprintf(&buf[size], "PORT_IRQ_STAT 0x%08x\n",
2201 readl(dd->port->mmio + PORT_IRQ_STAT));
2202 size += sprintf(&buf[size], "HOST_IRQ_STAT 0x%08x\n",
2203 readl(dd->mmio + HOST_IRQ_STAT));
2207 static DEVICE_ATTR(registers, S_IRUGO, hw_show_registers, NULL);
2210 * Create the sysfs related attributes.
2212 * @dd Pointer to the driver data structure.
2213 * @kobj Pointer to the kobj for the block device.
2216 * 0 Operation completed successfully.
2217 * -EINVAL Invalid parameter.
2219 int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
2224 if (sysfs_create_file(kobj, &dev_attr_registers.attr))
2225 dev_warn(&dd->pdev->dev,
2226 "Error creating registers sysfs entry\n");
2231 * Remove the sysfs related attributes.
2233 * @dd Pointer to the driver data structure.
2234 * @kobj Pointer to the kobj for the block device.
2237 * 0 Operation completed successfully.
2238 * -EINVAL Invalid parameter.
2240 int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
2245 sysfs_remove_file(kobj, &dev_attr_registers.attr);
2251 * Perform any init/resume time hardware setup
2253 * @dd Pointer to the driver data structure.
2258 static inline void hba_setup(struct driver_data *dd)
2261 hwdata = readl(dd->mmio + HOST_HSORG);
2263 /* interrupt bug workaround: use only 1 IS bit.*/
2265 HSORG_DISABLE_SLOTGRP_INTR |
2266 HSORG_DISABLE_SLOTGRP_PXIS,
2267 dd->mmio + HOST_HSORG);
2271 * Detect the details of the product, and store anything needed
2272 * into the driver data structure. This includes product type and
2273 * version and number of slot groups.
2275 * @dd Pointer to the driver data structure.
2280 static void mtip_detect_product(struct driver_data *dd)
2283 unsigned int rev, slotgroups;
2286 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2288 * [15:8] hardware/software interface rev#
2289 * [ 3] asic-style interface
2290 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2292 hwdata = readl(dd->mmio + HOST_HSORG);
2294 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2295 dd->slot_groups = 1;
2298 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2299 rev = (hwdata & HSORG_HWREV) >> 8;
2300 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2301 dev_info(&dd->pdev->dev,
2302 "ASIC-FPGA design, HS rev 0x%x, "
2303 "%i slot groups [%i slots]\n",
2308 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2309 dev_warn(&dd->pdev->dev,
2310 "Warning: driver only supports "
2311 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2312 slotgroups = MTIP_MAX_SLOT_GROUPS;
2314 dd->slot_groups = slotgroups;
2318 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2322 * Blocking wait for FTL rebuild to complete
2324 * @dd Pointer to the DRIVER_DATA structure.
2327 * 0 FTL rebuild completed successfully
2328 * -EFAULT FTL rebuild error/timeout/interruption
2330 static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2332 unsigned long timeout, cnt = 0, start;
2334 dev_warn(&dd->pdev->dev,
2335 "FTL rebuild in progress. Polling for completion.\n");
2338 dd->ftlrebuildflag = 1;
2339 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2342 #ifdef CONFIG_HOTPLUG
2343 if (mtip_check_surprise_removal(dd->pdev))
2346 if (mtip_get_identify(dd->port, NULL) < 0)
2349 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2350 MTIP_FTL_REBUILD_MAGIC) {
2352 /* Print message every 3 minutes */
2354 dev_warn(&dd->pdev->dev,
2355 "FTL rebuild in progress (%d secs).\n",
2356 jiffies_to_msecs(jiffies - start) / 1000);
2360 dev_warn(&dd->pdev->dev,
2361 "FTL rebuild complete (%d secs).\n",
2362 jiffies_to_msecs(jiffies - start) / 1000);
2363 dd->ftlrebuildflag = 0;
2367 } while (time_before(jiffies, timeout));
2369 /* Check for timeout */
2370 if (dd->ftlrebuildflag) {
2371 dev_err(&dd->pdev->dev,
2372 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2373 jiffies_to_msecs(jiffies - start) / 1000);
2381 * Called once for each card.
2383 * @dd Pointer to the driver data structure.
2386 * 0 on success, else an error code.
2388 int mtip_hw_init(struct driver_data *dd)
2392 unsigned int num_command_slots;
2394 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
2396 mtip_detect_product(dd);
2397 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
2401 num_command_slots = dd->slot_groups * 32;
2406 * Initialize the internal semaphore
2407 * Use a rw semaphore to enable prioritization of
2408 * mgmnt ioctl traffic during heavy IO load
2410 init_rwsem(&dd->internal_sem);
2412 tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
2414 dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
2416 dev_err(&dd->pdev->dev,
2417 "Memory allocation: port structure\n");
2421 /* Counting semaphore to track command slot usage */
2422 sema_init(&dd->port->cmd_slot, num_command_slots - 1);
2424 /* Spinlock to prevent concurrent issue */
2425 spin_lock_init(&dd->port->cmd_issue_lock);
2427 /* Set the port mmio base address. */
2428 dd->port->mmio = dd->mmio + PORT_OFFSET;
2431 /* Allocate memory for the command list. */
2432 dd->port->command_list =
2433 dmam_alloc_coherent(&dd->pdev->dev,
2434 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2435 &dd->port->command_list_dma,
2437 if (!dd->port->command_list) {
2438 dev_err(&dd->pdev->dev,
2439 "Memory allocation: command list\n");
2444 /* Clear the memory we have allocated. */
2445 memset(dd->port->command_list,
2447 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2));
2449 /* Setup the addresse of the RX FIS. */
2450 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
2451 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
2453 /* Setup the address of the command tables. */
2454 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
2455 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
2457 /* Setup the address of the identify data. */
2458 dd->port->identify = dd->port->command_table +
2460 dd->port->identify_dma = dd->port->command_tbl_dma +
2463 /* Setup the address of the sector buffer. */
2464 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
2465 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
2467 /* Point the command headers at the command tables. */
2468 for (i = 0; i < num_command_slots; i++) {
2469 dd->port->commands[i].command_header =
2470 dd->port->command_list +
2471 (sizeof(struct mtip_cmd_hdr) * i);
2472 dd->port->commands[i].command_header_dma =
2473 dd->port->command_list_dma +
2474 (sizeof(struct mtip_cmd_hdr) * i);
2476 dd->port->commands[i].command =
2477 dd->port->command_table + (HW_CMD_TBL_SZ * i);
2478 dd->port->commands[i].command_dma =
2479 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
2481 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
2482 dd->port->commands[i].command_header->ctbau =
2484 (dd->port->commands[i].command_dma >> 16) >> 16);
2485 dd->port->commands[i].command_header->ctba = cpu_to_le32(
2486 dd->port->commands[i].command_dma & 0xffffffff);
2489 * If this is not done, a bug is reported by the stock
2490 * FC11 i386. Due to the fact that it has lots of kernel
2491 * debugging enabled.
2493 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
2495 /* Mark all commands as currently inactive.*/
2496 atomic_set(&dd->port->commands[i].active, 0);
2499 /* Setup the pointers to the extended s_active and CI registers. */
2500 for (i = 0; i < dd->slot_groups; i++) {
2501 dd->port->s_active[i] =
2502 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
2503 dd->port->cmd_issue[i] =
2504 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
2505 dd->port->completed[i] =
2506 dd->port->mmio + i*0x80 + PORT_SDBV;
2509 /* Reset the HBA. */
2510 if (mtip_hba_reset(dd) < 0) {
2511 dev_err(&dd->pdev->dev,
2512 "Card did not reset within timeout\n");
2517 mtip_init_port(dd->port);
2518 mtip_start_port(dd->port);
2520 /* Setup the ISR and enable interrupts. */
2521 rv = devm_request_irq(&dd->pdev->dev,
2525 dev_driver_string(&dd->pdev->dev),
2529 dev_err(&dd->pdev->dev,
2530 "Unable to allocate IRQ %d\n", dd->pdev->irq);
2534 /* Enable interrupts on the HBA. */
2535 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2536 dd->mmio + HOST_CTL);
2538 init_timer(&dd->port->cmd_timer);
2539 dd->port->cmd_timer.data = (unsigned long int) dd->port;
2540 dd->port->cmd_timer.function = mtip_timeout_function;
2541 mod_timer(&dd->port->cmd_timer,
2542 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
2544 if (mtip_get_identify(dd->port, NULL) < 0) {
2548 mtip_dump_identify(dd->port);
2550 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2551 MTIP_FTL_REBUILD_MAGIC) {
2552 return mtip_ftl_rebuild_poll(dd);
2557 del_timer_sync(&dd->port->cmd_timer);
2559 /* Disable interrupts on the HBA. */
2560 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2561 dd->mmio + HOST_CTL);
2563 /*Release the IRQ. */
2564 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
2567 mtip_deinit_port(dd->port);
2569 /* Free the command/command header memory. */
2570 dmam_free_coherent(&dd->pdev->dev,
2571 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2572 dd->port->command_list,
2573 dd->port->command_list_dma);
2575 /* Free the memory allocated for the for structure. */
2582 * Called to deinitialize an interface.
2584 * @dd Pointer to the driver data structure.
2589 int mtip_hw_exit(struct driver_data *dd)
2592 * Send standby immediate (E0h) to the drive so that it
2595 if (atomic_read(&dd->drv_cleanup_done) != true) {
2597 mtip_standby_immediate(dd->port);
2599 /* de-initialize the port. */
2600 mtip_deinit_port(dd->port);
2602 /* Disable interrupts on the HBA. */
2603 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2604 dd->mmio + HOST_CTL);
2607 del_timer_sync(&dd->port->cmd_timer);
2609 /* Stop the bottom half tasklet. */
2610 tasklet_kill(&dd->tasklet);
2612 /* Release the IRQ. */
2613 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
2615 /* Free the command/command header memory. */
2616 dmam_free_coherent(&dd->pdev->dev,
2617 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2618 dd->port->command_list,
2619 dd->port->command_list_dma);
2620 /* Free the memory allocated for the for structure. */
2627 * Issue a Standby Immediate command to the device.
2629 * This function is called by the Block Layer just before the
2630 * system powers off during a shutdown.
2632 * @dd Pointer to the driver data structure.
2637 int mtip_hw_shutdown(struct driver_data *dd)
2640 * Send standby immediate (E0h) to the drive so that it
2643 mtip_standby_immediate(dd->port);
2651 * This function is called by the Block Layer just before the
2652 * system hibernates.
2654 * @dd Pointer to the driver data structure.
2657 * 0 Suspend was successful
2658 * -EFAULT Suspend was not successful
2660 int mtip_hw_suspend(struct driver_data *dd)
2663 * Send standby immediate (E0h) to the drive
2664 * so that it saves its state.
2666 if (mtip_standby_immediate(dd->port) != 0) {
2667 dev_err(&dd->pdev->dev,
2668 "Failed standby-immediate command\n");
2672 /* Disable interrupts on the HBA.*/
2673 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2674 dd->mmio + HOST_CTL);
2675 mtip_deinit_port(dd->port);
2683 * This function is called by the Block Layer as the
2686 * @dd Pointer to the driver data structure.
2689 * 0 Resume was successful
2690 * -EFAULT Resume was not successful
2692 int mtip_hw_resume(struct driver_data *dd)
2694 /* Perform any needed hardware setup steps */
2698 if (mtip_hba_reset(dd) != 0) {
2699 dev_err(&dd->pdev->dev,
2700 "Unable to reset the HBA\n");
2705 * Enable the port, DMA engine, and FIS reception specific
2706 * h/w in controller.
2708 mtip_init_port(dd->port);
2709 mtip_start_port(dd->port);
2711 /* Enable interrupts on the HBA.*/
2712 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2713 dd->mmio + HOST_CTL);
2719 * This function is called for clean the pending command in the
2720 * command slot during the surprise removal of device and return
2721 * error to the upper layer.
2723 * @dd Pointer to the DRIVER_DATA structure.
2728 void mtip_command_cleanup(struct driver_data *dd)
2730 int group = 0, commandslot = 0, commandindex = 0;
2731 struct mtip_cmd *command;
2732 struct mtip_port *port = dd->port;
2734 for (group = 0; group < 4; group++) {
2735 for (commandslot = 0; commandslot < 32; commandslot++) {
2736 if (!(port->allocated[group] & (1 << commandslot)))
2739 commandindex = group << 5 | commandslot;
2740 command = &port->commands[commandindex];
2742 if (atomic_read(&command->active)
2743 && (command->async_callback)) {
2744 command->async_callback(command->async_data,
2746 command->async_callback = NULL;
2747 command->async_data = NULL;
2750 dma_unmap_sg(&port->dd->pdev->dev,
2752 command->scatter_ents,
2753 command->direction);
2757 up(&port->cmd_slot);
2759 atomic_set(&dd->drv_cleanup_done, true);
2763 * Helper function for reusing disk name
2764 * upon hot insertion.
2766 static int rssd_disk_name_format(char *prefix,
2771 const int base = 'z' - 'a' + 1;
2772 char *begin = buf + strlen(prefix);
2773 char *end = buf + buflen;
2783 *--p = 'a' + (index % unit);
2784 index = (index / unit) - 1;
2785 } while (index >= 0);
2787 memmove(begin, p, end - p);
2788 memcpy(buf, prefix, strlen(prefix));
2794 * Block layer IOCTL handler.
2796 * @dev Pointer to the block_device structure.
2798 * @cmd IOCTL command passed from the user application.
2799 * @arg Argument passed from the user application.
2802 * 0 IOCTL completed successfully.
2803 * -ENOTTY IOCTL not supported or invalid driver data
2804 * structure pointer.
2806 static int mtip_block_ioctl(struct block_device *dev,
2811 struct driver_data *dd = dev->bd_disk->private_data;
2813 if (!capable(CAP_SYS_ADMIN))
2823 return mtip_hw_ioctl(dd, cmd, arg);
2827 #ifdef CONFIG_COMPAT
2829 * Block layer compat IOCTL handler.
2831 * @dev Pointer to the block_device structure.
2833 * @cmd IOCTL command passed from the user application.
2834 * @arg Argument passed from the user application.
2837 * 0 IOCTL completed successfully.
2838 * -ENOTTY IOCTL not supported or invalid driver data
2839 * structure pointer.
2841 static int mtip_block_compat_ioctl(struct block_device *dev,
2846 struct driver_data *dd = dev->bd_disk->private_data;
2848 if (!capable(CAP_SYS_ADMIN))
2857 case HDIO_DRIVE_TASKFILE: {
2858 struct mtip_compat_ide_task_request_s *compat_req_task;
2859 ide_task_request_t req_task;
2860 int compat_tasksize, outtotal, ret;
2862 compat_tasksize = sizeof(struct mtip_compat_ide_task_request_s);
2865 (struct mtip_compat_ide_task_request_s __user *) arg;
2867 if (copy_from_user(&req_task, (void __user *) arg,
2868 compat_tasksize - (2 * sizeof(compat_long_t))))
2871 if (get_user(req_task.out_size, &compat_req_task->out_size))
2874 if (get_user(req_task.in_size, &compat_req_task->in_size))
2877 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
2879 ret = exec_drive_taskfile(dd, (void __user *) arg,
2880 &req_task, outtotal);
2882 if (copy_to_user((void __user *) arg, &req_task,
2884 (2 * sizeof(compat_long_t))))
2887 if (put_user(req_task.out_size, &compat_req_task->out_size))
2890 if (put_user(req_task.in_size, &compat_req_task->in_size))
2896 return mtip_hw_ioctl(dd, cmd, arg);
2902 * Obtain the geometry of the device.
2904 * You may think that this function is obsolete, but some applications,
2905 * fdisk for example still used CHS values. This function describes the
2906 * device as having 224 heads and 56 sectors per cylinder. These values are
2907 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
2908 * partition is described in terms of a start and end cylinder this means
2909 * that each partition is also 4KB aligned. Non-aligned partitions adversely
2910 * affects performance.
2912 * @dev Pointer to the block_device strucutre.
2913 * @geo Pointer to a hd_geometry structure.
2916 * 0 Operation completed successfully.
2917 * -ENOTTY An error occurred while reading the drive capacity.
2919 static int mtip_block_getgeo(struct block_device *dev,
2920 struct hd_geometry *geo)
2922 struct driver_data *dd = dev->bd_disk->private_data;
2928 if (!(mtip_hw_get_capacity(dd, &capacity))) {
2929 dev_warn(&dd->pdev->dev,
2930 "Could not get drive capacity.\n");
2936 #if BITS_PER_LONG == 64
2937 geo->cylinders = capacity / (geo->heads * geo->sectors);
2939 do_div(capacity, (geo->heads * geo->sectors));
2940 geo->cylinders = capacity;
2946 * Block device operation function.
2948 * This structure contains pointers to the functions required by the block
2951 static const struct block_device_operations mtip_block_ops = {
2952 .ioctl = mtip_block_ioctl,
2953 #ifdef CONFIG_COMPAT
2954 .compat_ioctl = mtip_block_compat_ioctl,
2956 .getgeo = mtip_block_getgeo,
2957 .owner = THIS_MODULE
2961 * Block layer make request function.
2963 * This function is called by the kernel to process a BIO for
2966 * @queue Pointer to the request queue. Unused other than to obtain
2967 * the driver data structure.
2968 * @bio Pointer to the BIO.
2973 static int mtip_make_request(struct request_queue *queue, struct bio *bio)
2975 struct driver_data *dd = queue->queuedata;
2976 struct scatterlist *sg;
2977 struct bio_vec *bvec;
2981 if (unlikely(!bio_has_data(bio))) {
2982 blk_queue_flush(queue, 0);
2987 if (unlikely(atomic_read(&dd->eh_active))) {
2988 bio_endio(bio, -EBUSY);
2992 sg = mtip_hw_get_scatterlist(dd, &tag);
2993 if (likely(sg != NULL)) {
2994 blk_queue_bounce(queue, &bio);
2996 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
2997 dev_warn(&dd->pdev->dev,
2998 "Maximum number of SGL entries exceeded");
3000 mtip_hw_release_scatterlist(dd, tag);
3004 /* Create the scatter list for this bio. */
3005 bio_for_each_segment(bvec, bio, nents) {
3006 sg_set_page(&sg[nents],
3012 /* Issue the read/write. */
3013 mtip_hw_submit_io(dd,
3020 bio->bi_rw & REQ_FLUSH,
3030 * Block layer initialization function.
3032 * This function is called once by the PCI layer for each P320
3033 * device that is connected to the system.
3035 * @dd Pointer to the driver data structure.
3038 * 0 on success else an error code.
3040 int mtip_block_initialize(struct driver_data *dd)
3044 unsigned int index = 0;
3045 struct kobject *kobj;
3047 /* Initialize the protocol layer. */
3048 rv = mtip_hw_init(dd);
3050 dev_err(&dd->pdev->dev,
3051 "Protocol layer initialization failed\n");
3053 goto protocol_init_error;
3056 /* Allocate the request queue. */
3057 dd->queue = blk_alloc_queue(GFP_KERNEL);
3058 if (dd->queue == NULL) {
3059 dev_err(&dd->pdev->dev,
3060 "Unable to allocate request queue\n");
3062 goto block_queue_alloc_init_error;
3065 /* Attach our request function to the request queue. */
3066 blk_queue_make_request(dd->queue, mtip_make_request);
3068 /* Set device limits. */
3069 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3070 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3071 blk_queue_physical_block_size(dd->queue, 4096);
3072 blk_queue_io_min(dd->queue, 4096);
3074 dd->disk = alloc_disk(MTIP_MAX_MINORS);
3075 if (dd->disk == NULL) {
3076 dev_err(&dd->pdev->dev,
3077 "Unable to allocate gendisk structure\n");
3079 goto alloc_disk_error;
3082 /* Generate the disk name, implemented same as in sd.c */
3084 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3087 spin_lock(&rssd_index_lock);
3088 rv = ida_get_new(&rssd_index_ida, &index);
3089 spin_unlock(&rssd_index_lock);
3090 } while (rv == -EAGAIN);
3095 rv = rssd_disk_name_format("rssd",
3097 dd->disk->disk_name,
3100 goto disk_index_error;
3102 dd->disk->driverfs_dev = &dd->pdev->dev;
3103 dd->disk->major = dd->major;
3104 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3105 dd->disk->fops = &mtip_block_ops;
3106 dd->disk->queue = dd->queue;
3107 dd->disk->private_data = dd;
3108 dd->queue->queuedata = dd;
3111 /* Set the capacity of the device in 512 byte sectors. */
3112 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3113 dev_warn(&dd->pdev->dev,
3114 "Could not read drive capacity\n");
3116 goto read_capacity_error;
3118 set_capacity(dd->disk, capacity);
3120 /* Enable the block device and add it to /dev */
3124 * Now that the disk is active, initialize any sysfs attributes
3125 * managed by the protocol layer.
3127 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3129 mtip_hw_sysfs_init(dd, kobj);
3135 read_capacity_error:
3137 * Delete our gendisk structure. This also removes the device
3140 del_gendisk(dd->disk);
3143 spin_lock(&rssd_index_lock);
3144 ida_remove(&rssd_index_ida, index);
3145 spin_unlock(&rssd_index_lock);
3151 blk_cleanup_queue(dd->queue);
3153 block_queue_alloc_init_error:
3154 /* De-initialize the protocol layer. */
3157 protocol_init_error:
3162 * Block layer deinitialization function.
3164 * Called by the PCI layer as each P320 device is removed.
3166 * @dd Pointer to the driver data structure.
3171 int mtip_block_remove(struct driver_data *dd)
3173 struct kobject *kobj;
3174 /* Clean up the sysfs attributes managed by the protocol layer. */
3175 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3177 mtip_hw_sysfs_exit(dd, kobj);
3182 * Delete our gendisk structure. This also removes the device
3185 del_gendisk(dd->disk);
3186 blk_cleanup_queue(dd->queue);
3190 /* De-initialize the protocol layer. */
3197 * Function called by the PCI layer when just before the
3198 * machine shuts down.
3200 * If a protocol layer shutdown function is present it will be called
3203 * @dd Pointer to the driver data structure.
3208 int mtip_block_shutdown(struct driver_data *dd)
3210 dev_info(&dd->pdev->dev,
3211 "Shutting down %s ...\n", dd->disk->disk_name);
3213 /* Delete our gendisk structure, and cleanup the blk queue. */
3214 del_gendisk(dd->disk);
3215 blk_cleanup_queue(dd->queue);
3219 mtip_hw_shutdown(dd);
3223 int mtip_block_suspend(struct driver_data *dd)
3225 dev_info(&dd->pdev->dev,
3226 "Suspending %s ...\n", dd->disk->disk_name);
3227 mtip_hw_suspend(dd);
3231 int mtip_block_resume(struct driver_data *dd)
3233 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
3234 dd->disk->disk_name);
3240 * Called for each supported PCI device detected.
3242 * This function allocates the private data structure, enables the
3243 * PCI device and then calls the block layer initialization function.
3246 * 0 on success else an error code.
3248 static int mtip_pci_probe(struct pci_dev *pdev,
3249 const struct pci_device_id *ent)
3252 struct driver_data *dd = NULL;
3254 /* Allocate memory for this devices private data. */
3255 dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
3258 "Unable to allocate memory for driver data\n");
3262 /* Set the atomic variable as 1 in case of SRSI */
3263 atomic_set(&dd->drv_cleanup_done, true);
3265 atomic_set(&dd->resumeflag, false);
3266 atomic_set(&dd->eh_active, 0);
3268 /* Attach the private data to this PCI device. */
3269 pci_set_drvdata(pdev, dd);
3271 rv = pcim_enable_device(pdev);
3273 dev_err(&pdev->dev, "Unable to enable device\n");
3277 /* Map BAR5 to memory. */
3278 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
3280 dev_err(&pdev->dev, "Unable to map regions\n");
3284 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
3285 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3288 rv = pci_set_consistent_dma_mask(pdev,
3291 dev_warn(&pdev->dev,
3292 "64-bit DMA enable failed\n");
3298 pci_set_master(pdev);
3300 if (pci_enable_msi(pdev)) {
3301 dev_warn(&pdev->dev,
3302 "Unable to enable MSI interrupt.\n");
3303 goto block_initialize_err;
3306 /* Copy the info we may need later into the private data structure. */
3307 dd->major = mtip_major;
3308 dd->protocol = ent->driver_data;
3309 dd->instance = instance;
3312 /* Initialize the block layer. */
3313 rv = mtip_block_initialize(dd);
3316 "Unable to initialize block layer\n");
3317 goto block_initialize_err;
3321 * Increment the instance count so that each device has a unique
3328 block_initialize_err:
3329 pci_disable_msi(pdev);
3332 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3336 pci_set_drvdata(pdev, NULL);
3339 /* Set the atomic variable as 0 in case of SRSI */
3340 atomic_set(&dd->drv_cleanup_done, true);
3346 * Called for each probed device when the device is removed or the
3347 * driver is unloaded.
3352 static void mtip_pci_remove(struct pci_dev *pdev)
3354 struct driver_data *dd = pci_get_drvdata(pdev);
3357 if (mtip_check_surprise_removal(pdev)) {
3358 while (atomic_read(&dd->drv_cleanup_done) == false) {
3361 if (counter == 10) {
3362 /* Cleanup the outstanding commands */
3363 mtip_command_cleanup(dd);
3368 /* Set the atomic variable as 1 in case of SRSI */
3369 atomic_set(&dd->drv_cleanup_done, true);
3371 /* Clean up the block layer. */
3372 mtip_block_remove(dd);
3374 pci_disable_msi(pdev);
3377 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3381 * Called for each probed device when the device is suspended.
3387 static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
3390 struct driver_data *dd = pci_get_drvdata(pdev);
3394 "Driver private datastructure is NULL\n");
3398 atomic_set(&dd->resumeflag, true);
3400 /* Disable ports & interrupts then send standby immediate */
3401 rv = mtip_block_suspend(dd);
3404 "Failed to suspend controller\n");
3409 * Save the pci config space to pdev structure &
3410 * disable the device
3412 pci_save_state(pdev);
3413 pci_disable_device(pdev);
3415 /* Move to Low power state*/
3416 pci_set_power_state(pdev, PCI_D3hot);
3422 * Called for each probed device when the device is resumed.
3428 static int mtip_pci_resume(struct pci_dev *pdev)
3431 struct driver_data *dd;
3433 dd = pci_get_drvdata(pdev);
3436 "Driver private datastructure is NULL\n");
3440 /* Move the device to active State */
3441 pci_set_power_state(pdev, PCI_D0);
3443 /* Restore PCI configuration space */
3444 pci_restore_state(pdev);
3446 /* Enable the PCI device*/
3447 rv = pcim_enable_device(pdev);
3450 "Failed to enable card during resume\n");
3453 pci_set_master(pdev);
3456 * Calls hbaReset, initPort, & startPort function
3457 * then enables interrupts
3459 rv = mtip_block_resume(dd);
3461 dev_err(&pdev->dev, "Unable to resume\n");
3464 atomic_set(&dd->resumeflag, false);
3475 static void mtip_pci_shutdown(struct pci_dev *pdev)
3477 struct driver_data *dd = pci_get_drvdata(pdev);
3479 mtip_block_shutdown(dd);
3483 * This function check_for_surprise_removal is called
3484 * while card is removed from the system and it will
3485 * read the vendor id from the configration space
3487 * @pdev Pointer to the pci_dev structure.
3490 * true if device removed, else false
3492 bool mtip_check_surprise_removal(struct pci_dev *pdev)
3496 /* Read the vendorID from the configuration space */
3497 pci_read_config_word(pdev, 0x00, &vendor_id);
3498 if (vendor_id == 0xFFFF)
3499 return true; /* device removed */
3501 return false; /* device present */
3504 /* Table of device ids supported by this driver. */
3505 static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
3506 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320_DEVICE_ID) },
3510 /* Structure that describes the PCI driver functions. */
3511 struct pci_driver mtip_pci_driver = {
3512 .name = MTIP_DRV_NAME,
3513 .id_table = mtip_pci_tbl,
3514 .probe = mtip_pci_probe,
3515 .remove = mtip_pci_remove,
3516 .suspend = mtip_pci_suspend,
3517 .resume = mtip_pci_resume,
3518 .shutdown = mtip_pci_shutdown,
3521 MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
3524 * Module initialization function.
3526 * Called once when the module is loaded. This function allocates a major
3527 * block device number to the Cyclone devices and registers the PCI layer
3531 * 0 on success else error code.
3533 static int __init mtip_init(void)
3535 printk(KERN_INFO MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
3537 /* Allocate a major block device number to use with this driver. */
3538 mtip_major = register_blkdev(0, MTIP_DRV_NAME);
3539 if (mtip_major < 0) {
3540 printk(KERN_ERR "Unable to register block device (%d)\n",
3545 /* Register our PCI operations. */
3546 return pci_register_driver(&mtip_pci_driver);
3550 * Module de-initialization function.
3552 * Called once when the module is unloaded. This function deallocates
3553 * the major block device number allocated by mtip_init() and
3554 * unregisters the PCI layer of the driver.
3559 static void __exit mtip_exit(void)
3561 /* Release the allocated major block device number. */
3562 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
3564 /* Unregister the PCI driver. */
3565 pci_unregister_driver(&mtip_pci_driver);
3568 MODULE_AUTHOR("Micron Technology, Inc");
3569 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
3570 MODULE_LICENSE("GPL");
3571 MODULE_VERSION(MTIP_DRV_VERSION);
3573 module_init(mtip_init);
3574 module_exit(mtip_exit);