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 unsigned char compat)
1628 struct host_to_dev_fis fis;
1629 struct host_to_dev_fis *reply;
1630 ide_task_request_t *req_task;
1633 dma_addr_t outbuf_dma = 0;
1634 dma_addr_t inbuf_dma = 0;
1635 dma_addr_t dma_buffer = 0;
1637 int tasksize = sizeof(struct ide_task_request_s);
1638 unsigned int taskin = 0;
1639 unsigned int taskout = 0;
1641 char __user *buf = (char __user *)arg;
1642 unsigned int timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1643 unsigned int force_single_sector;
1644 unsigned int transfer_size;
1645 unsigned long task_file_data;
1646 int intotal, outtotal;
1647 #ifdef CONFIG_COMPAT
1648 struct mtip_compat_ide_task_request_s *compat_req_task = NULL;
1649 int compat_tasksize = sizeof(struct mtip_compat_ide_task_request_s);
1653 req_task = kzalloc(tasksize, GFP_KERNEL);
1654 if (req_task == NULL)
1658 #ifdef CONFIG_COMPAT
1660 (struct mtip_compat_ide_task_request_s __user *) arg;
1662 if (copy_from_user(req_task, buf,
1664 (2 * sizeof(compat_long_t)))) {
1669 if (get_user(req_task->out_size, &compat_req_task->out_size)) {
1674 if (get_user(req_task->in_size, &compat_req_task->in_size)) {
1679 outtotal = compat_tasksize;
1680 intotal = compat_tasksize + req_task->out_size;
1686 if (copy_from_user(req_task, buf, tasksize)) {
1692 outtotal = tasksize;
1693 intotal = tasksize + req_task->out_size;
1696 taskout = req_task->out_size;
1697 taskin = req_task->in_size;
1698 /* 130560 = 512 * 0xFF*/
1699 if (taskin > 130560 || taskout > 130560) {
1705 outbuf = kzalloc(taskout, GFP_KERNEL);
1706 if (outbuf == NULL) {
1710 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
1714 outbuf_dma = pci_map_single(dd->pdev,
1718 if (outbuf_dma == 0) {
1722 dma_buffer = outbuf_dma;
1726 inbuf = kzalloc(taskin, GFP_KERNEL);
1727 if (inbuf == NULL) {
1732 if (copy_from_user(inbuf, buf + intotal, taskin)) {
1736 inbuf_dma = pci_map_single(dd->pdev,
1738 taskin, DMA_FROM_DEVICE);
1739 if (inbuf_dma == 0) {
1743 dma_buffer = inbuf_dma;
1746 /* only supports PIO and non-data commands from this ioctl. */
1747 switch (req_task->data_phase) {
1749 nsect = taskout / ATA_SECT_SIZE;
1750 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1753 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1755 case TASKFILE_NO_DATA:
1756 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
1763 /* Lock the internal command semaphore. */
1764 down_write(&dd->internal_sem);
1766 /* Build the FIS. */
1767 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1771 fis.command = req_task->io_ports[7];
1772 fis.features = req_task->io_ports[1];
1773 fis.sect_count = req_task->io_ports[2];
1774 fis.lba_low = req_task->io_ports[3];
1775 fis.lba_mid = req_task->io_ports[4];
1776 fis.lba_hi = req_task->io_ports[5];
1777 /* Clear the dev bit*/
1778 fis.device = req_task->io_ports[6] & ~0x10;
1780 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
1781 req_task->in_flags.all =
1782 IDE_TASKFILE_STD_IN_FLAGS |
1783 (IDE_HOB_STD_IN_FLAGS << 8);
1784 fis.lba_low_ex = req_task->hob_ports[3];
1785 fis.lba_mid_ex = req_task->hob_ports[4];
1786 fis.lba_hi_ex = req_task->hob_ports[5];
1787 fis.features_ex = req_task->hob_ports[1];
1788 fis.sect_cnt_ex = req_task->hob_ports[2];
1791 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
1794 force_single_sector = implicit_sector(fis.command, fis.features);
1796 if ((taskin || taskout) && (!fis.sect_count)) {
1798 fis.sect_count = nsect;
1800 if (!force_single_sector) {
1801 dev_warn(&dd->pdev->dev,
1802 "data movement but "
1803 "sect_count is 0\n");
1804 up_write(&dd->internal_sem);
1811 dbg_printk(MTIP_DRV_NAME
1812 "taskfile: cmd %x, feat %x, nsect %x,"
1813 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
1823 switch (fis.command) {
1824 case 0x92: /* Change timeout for Download Microcode to 60 seconds.*/
1827 case 0xf4: /* Change timeout for Security Erase Unit to 4 minutes.*/
1830 case 0xe0: /* Change timeout for standby immediate to 10 seconds.*/
1833 case 0xf7: /* Change timeout for vendor unique command to 10 secs */
1836 case 0xfa: /* Change timeout for vendor unique command to 10 secs */
1840 timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1844 /* Determine the correct transfer size.*/
1845 if (force_single_sector)
1846 transfer_size = ATA_SECT_SIZE;
1848 transfer_size = ATA_SECT_SIZE * fis.sect_count;
1850 /* Execute the command.*/
1851 if (mtip_exec_internal_command(dd->port,
1859 up_write(&dd->internal_sem);
1864 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
1866 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
1867 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
1868 req_task->io_ports[7] = reply->control;
1870 reply = dd->port->rxfis + RX_FIS_D2H_REG;
1871 req_task->io_ports[7] = reply->command;
1874 /* reclaim the DMA buffers.*/
1876 pci_unmap_single(dd->pdev, inbuf_dma,
1877 taskin, DMA_FROM_DEVICE);
1879 pci_unmap_single(dd->pdev, outbuf_dma,
1880 taskout, DMA_TO_DEVICE);
1884 /* return the ATA registers to the caller.*/
1885 req_task->io_ports[1] = reply->features;
1886 req_task->io_ports[2] = reply->sect_count;
1887 req_task->io_ports[3] = reply->lba_low;
1888 req_task->io_ports[4] = reply->lba_mid;
1889 req_task->io_ports[5] = reply->lba_hi;
1890 req_task->io_ports[6] = reply->device;
1892 if (req_task->out_flags.all & 1) {
1894 req_task->hob_ports[3] = reply->lba_low_ex;
1895 req_task->hob_ports[4] = reply->lba_mid_ex;
1896 req_task->hob_ports[5] = reply->lba_hi_ex;
1897 req_task->hob_ports[1] = reply->features_ex;
1898 req_task->hob_ports[2] = reply->sect_cnt_ex;
1901 /* Com rest after secure erase or lowlevel format */
1902 if (((fis.command == 0xF4) ||
1903 ((fis.command == 0xFC) &&
1904 (fis.features == 0x27 || fis.features == 0x72 ||
1905 fis.features == 0x62 || fis.features == 0x26))) &&
1906 !(reply->command & 1)) {
1907 mtip_restart_port(dd->port);
1910 dbg_printk(MTIP_DRV_NAME
1911 "%s: Completion: stat %x,"
1912 "err %x, sect_cnt %x, lbalo %x,"
1913 "lbamid %x, lbahi %x, dev %x\n",
1915 req_task->io_ports[7],
1916 req_task->io_ports[1],
1917 req_task->io_ports[2],
1918 req_task->io_ports[3],
1919 req_task->io_ports[4],
1920 req_task->io_ports[5],
1921 req_task->io_ports[6]);
1923 up_write(&dd->internal_sem);
1926 #ifdef CONFIG_COMPAT
1927 if (copy_to_user(buf, req_task,
1929 (2 * sizeof(compat_long_t)))) {
1933 if (put_user(req_task->out_size,
1934 &compat_req_task->out_size)) {
1938 if (put_user(req_task->in_size, &compat_req_task->in_size)) {
1944 if (copy_to_user(buf, req_task, tasksize)) {
1950 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
1956 if (copy_to_user(buf + intotal, inbuf, taskin)) {
1963 pci_unmap_single(dd->pdev, inbuf_dma,
1964 taskin, DMA_FROM_DEVICE);
1966 pci_unmap_single(dd->pdev, outbuf_dma,
1967 taskout, DMA_TO_DEVICE);
1976 * Handle IOCTL calls from the Block Layer.
1978 * This function is called by the Block Layer when it receives an IOCTL
1979 * command that it does not understand. If the IOCTL command is not supported
1980 * this function returns -ENOTTY.
1982 * @dd Pointer to the driver data structure.
1983 * @cmd IOCTL command passed from the Block Layer.
1984 * @arg IOCTL argument passed from the Block Layer.
1987 * 0 The IOCTL completed successfully.
1988 * -ENOTTY The specified command is not supported.
1989 * -EFAULT An error occurred copying data to a user space buffer.
1990 * -EIO An error occurred while executing the command.
1992 int mtip_hw_ioctl(struct driver_data *dd,
1995 unsigned char compat)
1998 case HDIO_GET_IDENTITY:
1999 if (mtip_get_identify(dd->port, (void __user *) arg) < 0) {
2000 dev_warn(&dd->pdev->dev,
2001 "Unable to read identity\n");
2006 case HDIO_DRIVE_CMD:
2008 u8 drive_command[4];
2010 /* Copy the user command info to our buffer. */
2011 if (copy_from_user(drive_command,
2012 (void __user *) arg,
2013 sizeof(drive_command)))
2016 /* Execute the drive command. */
2017 if (exec_drive_command(dd->port,
2019 (void __user *) (arg+4)))
2022 /* Copy the status back to the users buffer. */
2023 if (copy_to_user((void __user *) arg,
2025 sizeof(drive_command)))
2030 case HDIO_DRIVE_TASK:
2032 u8 drive_command[7];
2034 /* Copy the user command info to our buffer. */
2035 if (copy_from_user(drive_command,
2036 (void __user *) arg,
2037 sizeof(drive_command)))
2040 /* Execute the drive command. */
2041 if (exec_drive_task(dd->port, drive_command))
2044 /* Copy the status back to the users buffer. */
2045 if (copy_to_user((void __user *) arg,
2047 sizeof(drive_command)))
2052 case HDIO_DRIVE_TASKFILE:
2053 return exec_drive_taskfile(dd, arg, compat);
2062 * Submit an IO to the hw
2064 * This function is called by the block layer to issue an io
2065 * to the device. Upon completion, the callback function will
2066 * be called with the data parameter passed as the callback data.
2068 * @dd Pointer to the driver data structure.
2069 * @start First sector to read.
2070 * @nsect Number of sectors to read.
2071 * @nents Number of entries in scatter list for the read command.
2072 * @tag The tag of this read command.
2073 * @callback Pointer to the function that should be called
2074 * when the read completes.
2075 * @data Callback data passed to the callback function
2076 * when the read completes.
2077 * @barrier If non-zero, this command must be completed before
2078 * issuing any other commands.
2079 * @dir Direction (read or write)
2084 void mtip_hw_submit_io(struct driver_data *dd,
2094 struct host_to_dev_fis *fis;
2095 struct mtip_port *port = dd->port;
2096 struct mtip_cmd *command = &port->commands[tag];
2098 /* Map the scatter list for DMA access */
2100 nents = dma_map_sg(&dd->pdev->dev, command->sg,
2101 nents, DMA_FROM_DEVICE);
2103 nents = dma_map_sg(&dd->pdev->dev, command->sg,
2104 nents, DMA_TO_DEVICE);
2106 command->scatter_ents = nents;
2109 * The number of retries for this command before it is
2110 * reported as a failure to the upper layers.
2112 command->retries = MTIP_MAX_RETRIES;
2115 fis = command->command;
2119 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
2120 *((unsigned int *) &fis->lba_low) = (start & 0xffffff);
2121 *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xffffff);
2122 fis->device = 1 << 6;
2124 fis->device |= FUA_BIT;
2125 fis->features = nsect & 0xff;
2126 fis->features_ex = (nsect >> 8) & 0xff;
2127 fis->sect_count = ((tag << 3) | (tag >> 5));
2128 fis->sect_cnt_ex = 0;
2132 fill_command_sg(dd, command, nents);
2134 /* Populate the command header */
2135 command->command_header->opts = cpu_to_le32(
2136 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
2137 command->command_header->byte_count = 0;
2140 * Set the completion function and data for the command
2141 * within this layer.
2143 command->comp_data = dd;
2144 command->comp_func = mtip_async_complete;
2145 command->direction = (dir == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
2148 * Set the completion function and data for the command passed
2149 * from the upper layer.
2151 command->async_data = data;
2152 command->async_callback = callback;
2155 * Lock used to prevent this command from being issued
2156 * if an internal command is in progress.
2158 down_read(&port->dd->internal_sem);
2160 /* Issue the command to the hardware */
2161 mtip_issue_ncq_command(port, tag);
2163 /* Set the command's timeout value.*/
2164 port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
2165 MTIP_NCQ_COMMAND_TIMEOUT_MS);
2167 up_read(&port->dd->internal_sem);
2171 * Release a command slot.
2173 * @dd Pointer to the driver data structure.
2179 void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
2181 release_slot(dd->port, tag);
2185 * Obtain a command slot and return its associated scatter list.
2187 * @dd Pointer to the driver data structure.
2188 * @tag Pointer to an int that will receive the allocated command
2192 * Pointer to the scatter list for the allocated command slot
2193 * or NULL if no command slots are available.
2195 struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2199 * It is possible that, even with this semaphore, a thread
2200 * may think that no command slots are available. Therefore, we
2201 * need to make an attempt to get_slot().
2203 down(&dd->port->cmd_slot);
2204 *tag = get_slot(dd->port);
2206 if (unlikely(*tag < 0))
2209 return dd->port->commands[*tag].sg;
2213 * Sysfs register/status dump.
2215 * @dev Pointer to the device structure, passed by the kernrel.
2216 * @attr Pointer to the device_attribute structure passed by the kernel.
2217 * @buf Pointer to the char buffer that will receive the stats info.
2220 * The size, in bytes, of the data copied into buf.
2222 static ssize_t hw_show_registers(struct device *dev,
2223 struct device_attribute *attr,
2226 u32 group_allocated;
2227 struct driver_data *dd = dev_to_disk(dev)->private_data;
2231 size += sprintf(&buf[size], "%s:\ns_active:\n", __func__);
2233 for (n = 0; n < dd->slot_groups; n++)
2234 size += sprintf(&buf[size], "0x%08x\n",
2235 readl(dd->port->s_active[n]));
2237 size += sprintf(&buf[size], "Command Issue:\n");
2239 for (n = 0; n < dd->slot_groups; n++)
2240 size += sprintf(&buf[size], "0x%08x\n",
2241 readl(dd->port->cmd_issue[n]));
2243 size += sprintf(&buf[size], "Allocated:\n");
2245 for (n = 0; n < dd->slot_groups; n++) {
2246 if (sizeof(long) > sizeof(u32))
2248 dd->port->allocated[n/2] >> (32*(n&1));
2250 group_allocated = dd->port->allocated[n];
2251 size += sprintf(&buf[size], "0x%08x\n",
2255 size += sprintf(&buf[size], "completed:\n");
2257 for (n = 0; n < dd->slot_groups; n++)
2258 size += sprintf(&buf[size], "0x%08x\n",
2259 readl(dd->port->completed[n]));
2261 size += sprintf(&buf[size], "PORT_IRQ_STAT 0x%08x\n",
2262 readl(dd->port->mmio + PORT_IRQ_STAT));
2263 size += sprintf(&buf[size], "HOST_IRQ_STAT 0x%08x\n",
2264 readl(dd->mmio + HOST_IRQ_STAT));
2268 static DEVICE_ATTR(registers, S_IRUGO, hw_show_registers, NULL);
2271 * Create the sysfs related attributes.
2273 * @dd Pointer to the driver data structure.
2274 * @kobj Pointer to the kobj for the block device.
2277 * 0 Operation completed successfully.
2278 * -EINVAL Invalid parameter.
2280 int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
2285 if (sysfs_create_file(kobj, &dev_attr_registers.attr))
2286 dev_warn(&dd->pdev->dev,
2287 "Error creating registers sysfs entry\n");
2292 * Remove the sysfs related attributes.
2294 * @dd Pointer to the driver data structure.
2295 * @kobj Pointer to the kobj for the block device.
2298 * 0 Operation completed successfully.
2299 * -EINVAL Invalid parameter.
2301 int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
2306 sysfs_remove_file(kobj, &dev_attr_registers.attr);
2312 * Perform any init/resume time hardware setup
2314 * @dd Pointer to the driver data structure.
2319 static inline void hba_setup(struct driver_data *dd)
2322 hwdata = readl(dd->mmio + HOST_HSORG);
2324 /* interrupt bug workaround: use only 1 IS bit.*/
2326 HSORG_DISABLE_SLOTGRP_INTR |
2327 HSORG_DISABLE_SLOTGRP_PXIS,
2328 dd->mmio + HOST_HSORG);
2332 * Detect the details of the product, and store anything needed
2333 * into the driver data structure. This includes product type and
2334 * version and number of slot groups.
2336 * @dd Pointer to the driver data structure.
2341 static void mtip_detect_product(struct driver_data *dd)
2344 unsigned int rev, slotgroups;
2347 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2349 * [15:8] hardware/software interface rev#
2350 * [ 3] asic-style interface
2351 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2353 hwdata = readl(dd->mmio + HOST_HSORG);
2355 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2356 dd->slot_groups = 1;
2359 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2360 rev = (hwdata & HSORG_HWREV) >> 8;
2361 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2362 dev_info(&dd->pdev->dev,
2363 "ASIC-FPGA design, HS rev 0x%x, "
2364 "%i slot groups [%i slots]\n",
2369 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2370 dev_warn(&dd->pdev->dev,
2371 "Warning: driver only supports "
2372 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2373 slotgroups = MTIP_MAX_SLOT_GROUPS;
2375 dd->slot_groups = slotgroups;
2379 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2383 * Blocking wait for FTL rebuild to complete
2385 * @dd Pointer to the DRIVER_DATA structure.
2388 * 0 FTL rebuild completed successfully
2389 * -EFAULT FTL rebuild error/timeout/interruption
2391 static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2393 unsigned long timeout, cnt = 0, start;
2395 dev_warn(&dd->pdev->dev,
2396 "FTL rebuild in progress. Polling for completion.\n");
2399 dd->ftlrebuildflag = 1;
2400 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2403 #ifdef CONFIG_HOTPLUG
2404 if (mtip_check_surprise_removal(dd->pdev))
2407 if (mtip_get_identify(dd->port, NULL) < 0)
2410 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2411 MTIP_FTL_REBUILD_MAGIC) {
2413 /* Print message every 3 minutes */
2415 dev_warn(&dd->pdev->dev,
2416 "FTL rebuild in progress (%d secs).\n",
2417 jiffies_to_msecs(jiffies - start) / 1000);
2421 dev_warn(&dd->pdev->dev,
2422 "FTL rebuild complete (%d secs).\n",
2423 jiffies_to_msecs(jiffies - start) / 1000);
2424 dd->ftlrebuildflag = 0;
2428 } while (time_before(jiffies, timeout));
2430 /* Check for timeout */
2431 if (dd->ftlrebuildflag) {
2432 dev_err(&dd->pdev->dev,
2433 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2434 jiffies_to_msecs(jiffies - start) / 1000);
2442 * Called once for each card.
2444 * @dd Pointer to the driver data structure.
2447 * 0 on success, else an error code.
2449 int mtip_hw_init(struct driver_data *dd)
2453 unsigned int num_command_slots;
2455 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
2457 mtip_detect_product(dd);
2458 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
2462 num_command_slots = dd->slot_groups * 32;
2467 * Initialize the internal semaphore
2468 * Use a rw semaphore to enable prioritization of
2469 * mgmnt ioctl traffic during heavy IO load
2471 init_rwsem(&dd->internal_sem);
2473 tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
2475 dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
2477 dev_err(&dd->pdev->dev,
2478 "Memory allocation: port structure\n");
2482 /* Counting semaphore to track command slot usage */
2483 sema_init(&dd->port->cmd_slot, num_command_slots - 1);
2485 /* Spinlock to prevent concurrent issue */
2486 spin_lock_init(&dd->port->cmd_issue_lock);
2488 /* Set the port mmio base address. */
2489 dd->port->mmio = dd->mmio + PORT_OFFSET;
2492 /* Allocate memory for the command list. */
2493 dd->port->command_list =
2494 dmam_alloc_coherent(&dd->pdev->dev,
2495 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2496 &dd->port->command_list_dma,
2498 if (!dd->port->command_list) {
2499 dev_err(&dd->pdev->dev,
2500 "Memory allocation: command list\n");
2505 /* Clear the memory we have allocated. */
2506 memset(dd->port->command_list,
2508 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2));
2510 /* Setup the addresse of the RX FIS. */
2511 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
2512 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
2514 /* Setup the address of the command tables. */
2515 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
2516 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
2518 /* Setup the address of the identify data. */
2519 dd->port->identify = dd->port->command_table +
2521 dd->port->identify_dma = dd->port->command_tbl_dma +
2524 /* Setup the address of the sector buffer. */
2525 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
2526 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
2528 /* Point the command headers at the command tables. */
2529 for (i = 0; i < num_command_slots; i++) {
2530 dd->port->commands[i].command_header =
2531 dd->port->command_list +
2532 (sizeof(struct mtip_cmd_hdr) * i);
2533 dd->port->commands[i].command_header_dma =
2534 dd->port->command_list_dma +
2535 (sizeof(struct mtip_cmd_hdr) * i);
2537 dd->port->commands[i].command =
2538 dd->port->command_table + (HW_CMD_TBL_SZ * i);
2539 dd->port->commands[i].command_dma =
2540 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
2542 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
2543 dd->port->commands[i].command_header->ctbau =
2545 (dd->port->commands[i].command_dma >> 16) >> 16);
2546 dd->port->commands[i].command_header->ctba = cpu_to_le32(
2547 dd->port->commands[i].command_dma & 0xffffffff);
2550 * If this is not done, a bug is reported by the stock
2551 * FC11 i386. Due to the fact that it has lots of kernel
2552 * debugging enabled.
2554 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
2556 /* Mark all commands as currently inactive.*/
2557 atomic_set(&dd->port->commands[i].active, 0);
2560 /* Setup the pointers to the extended s_active and CI registers. */
2561 for (i = 0; i < dd->slot_groups; i++) {
2562 dd->port->s_active[i] =
2563 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
2564 dd->port->cmd_issue[i] =
2565 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
2566 dd->port->completed[i] =
2567 dd->port->mmio + i*0x80 + PORT_SDBV;
2570 /* Reset the HBA. */
2571 if (mtip_hba_reset(dd) < 0) {
2572 dev_err(&dd->pdev->dev,
2573 "Card did not reset within timeout\n");
2578 mtip_init_port(dd->port);
2579 mtip_start_port(dd->port);
2581 /* Setup the ISR and enable interrupts. */
2582 rv = devm_request_irq(&dd->pdev->dev,
2586 dev_driver_string(&dd->pdev->dev),
2590 dev_err(&dd->pdev->dev,
2591 "Unable to allocate IRQ %d\n", dd->pdev->irq);
2595 /* Enable interrupts on the HBA. */
2596 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2597 dd->mmio + HOST_CTL);
2599 init_timer(&dd->port->cmd_timer);
2600 dd->port->cmd_timer.data = (unsigned long int) dd->port;
2601 dd->port->cmd_timer.function = mtip_timeout_function;
2602 mod_timer(&dd->port->cmd_timer,
2603 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
2605 if (mtip_get_identify(dd->port, NULL) < 0) {
2609 mtip_dump_identify(dd->port);
2611 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2612 MTIP_FTL_REBUILD_MAGIC) {
2613 return mtip_ftl_rebuild_poll(dd);
2618 del_timer_sync(&dd->port->cmd_timer);
2620 /* Disable interrupts on the HBA. */
2621 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2622 dd->mmio + HOST_CTL);
2624 /*Release the IRQ. */
2625 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
2628 mtip_deinit_port(dd->port);
2630 /* Free the command/command header memory. */
2631 dmam_free_coherent(&dd->pdev->dev,
2632 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2633 dd->port->command_list,
2634 dd->port->command_list_dma);
2636 /* Free the memory allocated for the for structure. */
2643 * Called to deinitialize an interface.
2645 * @dd Pointer to the driver data structure.
2650 int mtip_hw_exit(struct driver_data *dd)
2653 * Send standby immediate (E0h) to the drive so that it
2656 if (atomic_read(&dd->drv_cleanup_done) != true) {
2658 mtip_standby_immediate(dd->port);
2660 /* de-initialize the port. */
2661 mtip_deinit_port(dd->port);
2663 /* Disable interrupts on the HBA. */
2664 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2665 dd->mmio + HOST_CTL);
2668 del_timer_sync(&dd->port->cmd_timer);
2670 /* Stop the bottom half tasklet. */
2671 tasklet_kill(&dd->tasklet);
2673 /* Release the IRQ. */
2674 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
2676 /* Free the command/command header memory. */
2677 dmam_free_coherent(&dd->pdev->dev,
2678 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2679 dd->port->command_list,
2680 dd->port->command_list_dma);
2681 /* Free the memory allocated for the for structure. */
2688 * Issue a Standby Immediate command to the device.
2690 * This function is called by the Block Layer just before the
2691 * system powers off during a shutdown.
2693 * @dd Pointer to the driver data structure.
2698 int mtip_hw_shutdown(struct driver_data *dd)
2701 * Send standby immediate (E0h) to the drive so that it
2704 mtip_standby_immediate(dd->port);
2712 * This function is called by the Block Layer just before the
2713 * system hibernates.
2715 * @dd Pointer to the driver data structure.
2718 * 0 Suspend was successful
2719 * -EFAULT Suspend was not successful
2721 int mtip_hw_suspend(struct driver_data *dd)
2724 * Send standby immediate (E0h) to the drive
2725 * so that it saves its state.
2727 if (mtip_standby_immediate(dd->port) != 0) {
2728 dev_err(&dd->pdev->dev,
2729 "Failed standby-immediate command\n");
2733 /* Disable interrupts on the HBA.*/
2734 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2735 dd->mmio + HOST_CTL);
2736 mtip_deinit_port(dd->port);
2744 * This function is called by the Block Layer as the
2747 * @dd Pointer to the driver data structure.
2750 * 0 Resume was successful
2751 * -EFAULT Resume was not successful
2753 int mtip_hw_resume(struct driver_data *dd)
2755 /* Perform any needed hardware setup steps */
2759 if (mtip_hba_reset(dd) != 0) {
2760 dev_err(&dd->pdev->dev,
2761 "Unable to reset the HBA\n");
2766 * Enable the port, DMA engine, and FIS reception specific
2767 * h/w in controller.
2769 mtip_init_port(dd->port);
2770 mtip_start_port(dd->port);
2772 /* Enable interrupts on the HBA.*/
2773 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2774 dd->mmio + HOST_CTL);
2780 * This function is called for clean the pending command in the
2781 * command slot during the surprise removal of device and return
2782 * error to the upper layer.
2784 * @dd Pointer to the DRIVER_DATA structure.
2789 void mtip_command_cleanup(struct driver_data *dd)
2791 int group = 0, commandslot = 0, commandindex = 0;
2792 struct mtip_cmd *command;
2793 struct mtip_port *port = dd->port;
2795 for (group = 0; group < 4; group++) {
2796 for (commandslot = 0; commandslot < 32; commandslot++) {
2797 if (!(port->allocated[group] & (1 << commandslot)))
2800 commandindex = group << 5 | commandslot;
2801 command = &port->commands[commandindex];
2803 if (atomic_read(&command->active)
2804 && (command->async_callback)) {
2805 command->async_callback(command->async_data,
2807 command->async_callback = NULL;
2808 command->async_data = NULL;
2811 dma_unmap_sg(&port->dd->pdev->dev,
2813 command->scatter_ents,
2814 command->direction);
2818 up(&port->cmd_slot);
2820 atomic_set(&dd->drv_cleanup_done, true);
2824 * Helper function for reusing disk name
2825 * upon hot insertion.
2827 static int rssd_disk_name_format(char *prefix,
2832 const int base = 'z' - 'a' + 1;
2833 char *begin = buf + strlen(prefix);
2834 char *end = buf + buflen;
2844 *--p = 'a' + (index % unit);
2845 index = (index / unit) - 1;
2846 } while (index >= 0);
2848 memmove(begin, p, end - p);
2849 memcpy(buf, prefix, strlen(prefix));
2855 * Block layer IOCTL handler.
2857 * @dev Pointer to the block_device structure.
2859 * @cmd IOCTL command passed from the user application.
2860 * @arg Argument passed from the user application.
2863 * 0 IOCTL completed successfully.
2864 * -ENOTTY IOCTL not supported or invalid driver data
2865 * structure pointer.
2867 static int mtip_block_ioctl(struct block_device *dev,
2872 struct driver_data *dd = dev->bd_disk->private_data;
2874 if (!capable(CAP_SYS_ADMIN))
2884 return mtip_hw_ioctl(dd, cmd, arg, 0);
2888 #ifdef CONFIG_COMPAT
2890 * Block layer compat IOCTL handler.
2892 * @dev Pointer to the block_device structure.
2894 * @cmd IOCTL command passed from the user application.
2895 * @arg Argument passed from the user application.
2898 * 0 IOCTL completed successfully.
2899 * -ENOTTY IOCTL not supported or invalid driver data
2900 * structure pointer.
2902 static int mtip_block_compat_ioctl(struct block_device *dev,
2907 struct driver_data *dd = dev->bd_disk->private_data;
2909 if (!capable(CAP_SYS_ADMIN))
2919 return mtip_hw_ioctl(dd, cmd, arg, 1);
2925 * Obtain the geometry of the device.
2927 * You may think that this function is obsolete, but some applications,
2928 * fdisk for example still used CHS values. This function describes the
2929 * device as having 224 heads and 56 sectors per cylinder. These values are
2930 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
2931 * partition is described in terms of a start and end cylinder this means
2932 * that each partition is also 4KB aligned. Non-aligned partitions adversely
2933 * affects performance.
2935 * @dev Pointer to the block_device strucutre.
2936 * @geo Pointer to a hd_geometry structure.
2939 * 0 Operation completed successfully.
2940 * -ENOTTY An error occurred while reading the drive capacity.
2942 static int mtip_block_getgeo(struct block_device *dev,
2943 struct hd_geometry *geo)
2945 struct driver_data *dd = dev->bd_disk->private_data;
2951 if (!(mtip_hw_get_capacity(dd, &capacity))) {
2952 dev_warn(&dd->pdev->dev,
2953 "Could not get drive capacity.\n");
2959 #if BITS_PER_LONG == 64
2960 geo->cylinders = capacity / (geo->heads * geo->sectors);
2962 do_div(capacity, (geo->heads * geo->sectors));
2963 geo->cylinders = capacity;
2969 * Block device operation function.
2971 * This structure contains pointers to the functions required by the block
2974 static const struct block_device_operations mtip_block_ops = {
2975 .ioctl = mtip_block_ioctl,
2976 #ifdef CONFIG_COMPAT
2977 .compat_ioctl = mtip_block_compat_ioctl,
2979 .getgeo = mtip_block_getgeo,
2980 .owner = THIS_MODULE
2984 * Block layer make request function.
2986 * This function is called by the kernel to process a BIO for
2989 * @queue Pointer to the request queue. Unused other than to obtain
2990 * the driver data structure.
2991 * @bio Pointer to the BIO.
2996 static int mtip_make_request(struct request_queue *queue, struct bio *bio)
2998 struct driver_data *dd = queue->queuedata;
2999 struct scatterlist *sg;
3000 struct bio_vec *bvec;
3004 if (unlikely(!bio_has_data(bio))) {
3005 blk_queue_flush(queue, 0);
3010 if (unlikely(atomic_read(&dd->eh_active))) {
3011 bio_endio(bio, -EBUSY);
3015 sg = mtip_hw_get_scatterlist(dd, &tag);
3016 if (likely(sg != NULL)) {
3017 blk_queue_bounce(queue, &bio);
3019 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3020 dev_warn(&dd->pdev->dev,
3021 "Maximum number of SGL entries exceeded");
3023 mtip_hw_release_scatterlist(dd, tag);
3027 /* Create the scatter list for this bio. */
3028 bio_for_each_segment(bvec, bio, nents) {
3029 sg_set_page(&sg[nents],
3035 /* Issue the read/write. */
3036 mtip_hw_submit_io(dd,
3043 bio->bi_rw & REQ_FLUSH,
3053 * Block layer initialization function.
3055 * This function is called once by the PCI layer for each P320
3056 * device that is connected to the system.
3058 * @dd Pointer to the driver data structure.
3061 * 0 on success else an error code.
3063 int mtip_block_initialize(struct driver_data *dd)
3067 unsigned int index = 0;
3068 struct kobject *kobj;
3070 /* Initialize the protocol layer. */
3071 rv = mtip_hw_init(dd);
3073 dev_err(&dd->pdev->dev,
3074 "Protocol layer initialization failed\n");
3076 goto protocol_init_error;
3079 /* Allocate the request queue. */
3080 dd->queue = blk_alloc_queue(GFP_KERNEL);
3081 if (dd->queue == NULL) {
3082 dev_err(&dd->pdev->dev,
3083 "Unable to allocate request queue\n");
3085 goto block_queue_alloc_init_error;
3088 /* Attach our request function to the request queue. */
3089 blk_queue_make_request(dd->queue, mtip_make_request);
3091 /* Set device limits. */
3092 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3093 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3094 blk_queue_physical_block_size(dd->queue, 4096);
3095 blk_queue_io_min(dd->queue, 4096);
3097 dd->disk = alloc_disk(MTIP_MAX_MINORS);
3098 if (dd->disk == NULL) {
3099 dev_err(&dd->pdev->dev,
3100 "Unable to allocate gendisk structure\n");
3102 goto alloc_disk_error;
3105 /* Generate the disk name, implemented same as in sd.c */
3107 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3110 spin_lock(&rssd_index_lock);
3111 rv = ida_get_new(&rssd_index_ida, &index);
3112 spin_unlock(&rssd_index_lock);
3113 } while (rv == -EAGAIN);
3118 rv = rssd_disk_name_format("rssd",
3120 dd->disk->disk_name,
3123 goto disk_index_error;
3125 dd->disk->driverfs_dev = &dd->pdev->dev;
3126 dd->disk->major = dd->major;
3127 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3128 dd->disk->fops = &mtip_block_ops;
3129 dd->disk->queue = dd->queue;
3130 dd->disk->private_data = dd;
3131 dd->queue->queuedata = dd;
3134 /* Set the capacity of the device in 512 byte sectors. */
3135 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3136 dev_warn(&dd->pdev->dev,
3137 "Could not read drive capacity\n");
3139 goto read_capacity_error;
3141 set_capacity(dd->disk, capacity);
3143 /* Enable the block device and add it to /dev */
3147 * Now that the disk is active, initialize any sysfs attributes
3148 * managed by the protocol layer.
3150 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3152 mtip_hw_sysfs_init(dd, kobj);
3158 read_capacity_error:
3160 * Delete our gendisk structure. This also removes the device
3163 del_gendisk(dd->disk);
3166 spin_lock(&rssd_index_lock);
3167 ida_remove(&rssd_index_ida, index);
3168 spin_unlock(&rssd_index_lock);
3174 blk_cleanup_queue(dd->queue);
3176 block_queue_alloc_init_error:
3177 /* De-initialize the protocol layer. */
3180 protocol_init_error:
3185 * Block layer deinitialization function.
3187 * Called by the PCI layer as each P320 device is removed.
3189 * @dd Pointer to the driver data structure.
3194 int mtip_block_remove(struct driver_data *dd)
3196 struct kobject *kobj;
3197 /* Clean up the sysfs attributes managed by the protocol layer. */
3198 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3200 mtip_hw_sysfs_exit(dd, kobj);
3205 * Delete our gendisk structure. This also removes the device
3208 del_gendisk(dd->disk);
3209 blk_cleanup_queue(dd->queue);
3213 /* De-initialize the protocol layer. */
3220 * Function called by the PCI layer when just before the
3221 * machine shuts down.
3223 * If a protocol layer shutdown function is present it will be called
3226 * @dd Pointer to the driver data structure.
3231 int mtip_block_shutdown(struct driver_data *dd)
3233 dev_info(&dd->pdev->dev,
3234 "Shutting down %s ...\n", dd->disk->disk_name);
3236 /* Delete our gendisk structure, and cleanup the blk queue. */
3237 del_gendisk(dd->disk);
3238 blk_cleanup_queue(dd->queue);
3242 mtip_hw_shutdown(dd);
3246 int mtip_block_suspend(struct driver_data *dd)
3248 dev_info(&dd->pdev->dev,
3249 "Suspending %s ...\n", dd->disk->disk_name);
3250 mtip_hw_suspend(dd);
3254 int mtip_block_resume(struct driver_data *dd)
3256 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
3257 dd->disk->disk_name);
3263 * Called for each supported PCI device detected.
3265 * This function allocates the private data structure, enables the
3266 * PCI device and then calls the block layer initialization function.
3269 * 0 on success else an error code.
3271 static int mtip_pci_probe(struct pci_dev *pdev,
3272 const struct pci_device_id *ent)
3275 struct driver_data *dd = NULL;
3277 /* Allocate memory for this devices private data. */
3278 dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
3281 "Unable to allocate memory for driver data\n");
3285 /* Set the atomic variable as 1 in case of SRSI */
3286 atomic_set(&dd->drv_cleanup_done, true);
3288 atomic_set(&dd->resumeflag, false);
3289 atomic_set(&dd->eh_active, 0);
3291 /* Attach the private data to this PCI device. */
3292 pci_set_drvdata(pdev, dd);
3294 rv = pcim_enable_device(pdev);
3296 dev_err(&pdev->dev, "Unable to enable device\n");
3300 /* Map BAR5 to memory. */
3301 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
3303 dev_err(&pdev->dev, "Unable to map regions\n");
3307 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
3308 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3311 rv = pci_set_consistent_dma_mask(pdev,
3314 dev_warn(&pdev->dev,
3315 "64-bit DMA enable failed\n");
3321 pci_set_master(pdev);
3323 if (pci_enable_msi(pdev)) {
3324 dev_warn(&pdev->dev,
3325 "Unable to enable MSI interrupt.\n");
3326 goto block_initialize_err;
3329 /* Copy the info we may need later into the private data structure. */
3330 dd->major = mtip_major;
3331 dd->protocol = ent->driver_data;
3332 dd->instance = instance;
3335 /* Initialize the block layer. */
3336 rv = mtip_block_initialize(dd);
3339 "Unable to initialize block layer\n");
3340 goto block_initialize_err;
3344 * Increment the instance count so that each device has a unique
3351 block_initialize_err:
3352 pci_disable_msi(pdev);
3355 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3359 pci_set_drvdata(pdev, NULL);
3362 /* Set the atomic variable as 0 in case of SRSI */
3363 atomic_set(&dd->drv_cleanup_done, true);
3369 * Called for each probed device when the device is removed or the
3370 * driver is unloaded.
3375 static void mtip_pci_remove(struct pci_dev *pdev)
3377 struct driver_data *dd = pci_get_drvdata(pdev);
3380 if (mtip_check_surprise_removal(pdev)) {
3381 while (atomic_read(&dd->drv_cleanup_done) == false) {
3384 if (counter == 10) {
3385 /* Cleanup the outstanding commands */
3386 mtip_command_cleanup(dd);
3391 /* Set the atomic variable as 1 in case of SRSI */
3392 atomic_set(&dd->drv_cleanup_done, true);
3394 /* Clean up the block layer. */
3395 mtip_block_remove(dd);
3397 pci_disable_msi(pdev);
3400 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3404 * Called for each probed device when the device is suspended.
3410 static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
3413 struct driver_data *dd = pci_get_drvdata(pdev);
3417 "Driver private datastructure is NULL\n");
3421 atomic_set(&dd->resumeflag, true);
3423 /* Disable ports & interrupts then send standby immediate */
3424 rv = mtip_block_suspend(dd);
3427 "Failed to suspend controller\n");
3432 * Save the pci config space to pdev structure &
3433 * disable the device
3435 pci_save_state(pdev);
3436 pci_disable_device(pdev);
3438 /* Move to Low power state*/
3439 pci_set_power_state(pdev, PCI_D3hot);
3445 * Called for each probed device when the device is resumed.
3451 static int mtip_pci_resume(struct pci_dev *pdev)
3454 struct driver_data *dd;
3456 dd = pci_get_drvdata(pdev);
3459 "Driver private datastructure is NULL\n");
3463 /* Move the device to active State */
3464 pci_set_power_state(pdev, PCI_D0);
3466 /* Restore PCI configuration space */
3467 pci_restore_state(pdev);
3469 /* Enable the PCI device*/
3470 rv = pcim_enable_device(pdev);
3473 "Failed to enable card during resume\n");
3476 pci_set_master(pdev);
3479 * Calls hbaReset, initPort, & startPort function
3480 * then enables interrupts
3482 rv = mtip_block_resume(dd);
3484 dev_err(&pdev->dev, "Unable to resume\n");
3487 atomic_set(&dd->resumeflag, false);
3498 static void mtip_pci_shutdown(struct pci_dev *pdev)
3500 struct driver_data *dd = pci_get_drvdata(pdev);
3502 mtip_block_shutdown(dd);
3506 * This function check_for_surprise_removal is called
3507 * while card is removed from the system and it will
3508 * read the vendor id from the configration space
3510 * @pdev Pointer to the pci_dev structure.
3513 * true if device removed, else false
3515 bool mtip_check_surprise_removal(struct pci_dev *pdev)
3519 /* Read the vendorID from the configuration space */
3520 pci_read_config_word(pdev, 0x00, &vendor_id);
3521 if (vendor_id == 0xFFFF)
3522 return true; /* device removed */
3524 return false; /* device present */
3527 /* Table of device ids supported by this driver. */
3528 static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
3529 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320_DEVICE_ID) },
3533 /* Structure that describes the PCI driver functions. */
3534 struct pci_driver mtip_pci_driver = {
3535 .name = MTIP_DRV_NAME,
3536 .id_table = mtip_pci_tbl,
3537 .probe = mtip_pci_probe,
3538 .remove = mtip_pci_remove,
3539 .suspend = mtip_pci_suspend,
3540 .resume = mtip_pci_resume,
3541 .shutdown = mtip_pci_shutdown,
3544 MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
3547 * Module initialization function.
3549 * Called once when the module is loaded. This function allocates a major
3550 * block device number to the Cyclone devices and registers the PCI layer
3554 * 0 on success else error code.
3556 static int __init mtip_init(void)
3558 printk(KERN_INFO MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
3560 /* Allocate a major block device number to use with this driver. */
3561 mtip_major = register_blkdev(0, MTIP_DRV_NAME);
3562 if (mtip_major < 0) {
3563 printk(KERN_ERR "Unable to register block device (%d)\n",
3568 /* Register our PCI operations. */
3569 return pci_register_driver(&mtip_pci_driver);
3573 * Module de-initialization function.
3575 * Called once when the module is unloaded. This function deallocates
3576 * the major block device number allocated by mtip_init() and
3577 * unregisters the PCI layer of the driver.
3582 static void __exit mtip_exit(void)
3584 /* Release the allocated major block device number. */
3585 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
3587 /* Unregister the PCI driver. */
3588 pci_unregister_driver(&mtip_pci_driver);
3591 MODULE_AUTHOR("Micron Technology, Inc");
3592 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
3593 MODULE_LICENSE("GPL");
3594 MODULE_VERSION(MTIP_DRV_VERSION);
3596 module_init(mtip_init);
3597 module_exit(mtip_exit);