2 * Driver for the Micron P320 SSD
3 * Copyright (C) 2011 Micron Technology, Inc.
5 * Portions of this code were derived from works subjected to the
7 * Copyright (C) 2009 Integrated Device Technology, Inc.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
21 #include <linux/pci.h>
22 #include <linux/interrupt.h>
23 #include <linux/ata.h>
24 #include <linux/delay.h>
25 #include <linux/hdreg.h>
26 #include <linux/uaccess.h>
27 #include <linux/random.h>
28 #include <linux/smp.h>
29 #include <linux/compat.h>
31 #include <linux/module.h>
32 #include <linux/genhd.h>
33 #include <linux/blkdev.h>
34 #include <linux/bio.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/idr.h>
37 #include <../drivers/ata/ahci.h>
40 #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
41 #define HW_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (MTIP_MAX_SG * 16))
42 #define HW_CMD_TBL_AR_SZ (HW_CMD_TBL_SZ * MTIP_MAX_COMMAND_SLOTS)
43 #define HW_PORT_PRIV_DMA_SZ \
44 (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
46 #define HOST_HSORG 0xFC
47 #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
48 #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
49 #define HSORG_HWREV 0xFF00
50 #define HSORG_STYLE 0x8
51 #define HSORG_SLOTGROUPS 0x7
53 #define PORT_COMMAND_ISSUE 0x38
54 #define PORT_SDBV 0x7C
56 #define PORT_OFFSET 0x100
57 #define PORT_MEM_SIZE 0x80
59 #define PORT_IRQ_ERR \
60 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
61 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
62 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
64 #define PORT_IRQ_LEGACY \
65 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
66 #define PORT_IRQ_HANDLED \
67 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
68 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
69 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
70 #define DEF_PORT_IRQ \
71 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
74 #define MTIP_PRODUCT_UNKNOWN 0x00
75 #define MTIP_PRODUCT_ASICFPGA 0x11
77 /* Device instance number, incremented each time a device is probed. */
81 * Global variable used to hold the major block device number
82 * allocated in mtip_init().
84 static int mtip_major;
86 static DEFINE_SPINLOCK(rssd_index_lock);
87 static DEFINE_IDA(rssd_index_ida);
90 struct mtip_compat_ide_task_request_s {
93 ide_reg_valid_t out_flags;
94 ide_reg_valid_t in_flags;
97 compat_ulong_t out_size;
98 compat_ulong_t in_size;
102 static int mtip_exec_internal_command(struct mtip_port *port,
109 unsigned long timeout);
112 * This function check_for_surprise_removal is called
113 * while card is removed from the system and it will
114 * read the vendor id from the configration space
116 * @pdev Pointer to the pci_dev structure.
119 * true if device removed, else false
121 static bool mtip_check_surprise_removal(struct pci_dev *pdev)
125 /* Read the vendorID from the configuration space */
126 pci_read_config_word(pdev, 0x00, &vendor_id);
127 if (vendor_id == 0xFFFF)
128 return true; /* device removed */
130 return false; /* device present */
134 * This function is called for clean the pending command in the
135 * command slot during the surprise removal of device and return
136 * error to the upper layer.
138 * @dd Pointer to the DRIVER_DATA structure.
143 static void mtip_command_cleanup(struct driver_data *dd)
145 int group = 0, commandslot = 0, commandindex = 0;
146 struct mtip_cmd *command;
147 struct mtip_port *port = dd->port;
149 for (group = 0; group < 4; group++) {
150 for (commandslot = 0; commandslot < 32; commandslot++) {
151 if (!(port->allocated[group] & (1 << commandslot)))
154 commandindex = group << 5 | commandslot;
155 command = &port->commands[commandindex];
157 if (atomic_read(&command->active)
158 && (command->async_callback)) {
159 command->async_callback(command->async_data,
161 command->async_callback = NULL;
162 command->async_data = NULL;
165 dma_unmap_sg(&port->dd->pdev->dev,
167 command->scatter_ents,
174 atomic_set(&dd->drv_cleanup_done, true);
178 * Obtain an empty command slot.
180 * This function needs to be reentrant since it could be called
181 * at the same time on multiple CPUs. The allocation of the
182 * command slot must be atomic.
184 * @port Pointer to the port data structure.
187 * >= 0 Index of command slot obtained.
188 * -1 No command slots available.
190 static int get_slot(struct mtip_port *port)
193 unsigned int num_command_slots = port->dd->slot_groups * 32;
196 * Try 10 times, because there is a small race here.
197 * that's ok, because it's still cheaper than a lock.
199 * Race: Since this section is not protected by lock, same bit
200 * could be chosen by different process contexts running in
201 * different processor. So instead of costly lock, we are going
204 for (i = 0; i < 10; i++) {
205 slot = find_next_zero_bit(port->allocated,
206 num_command_slots, 1);
207 if ((slot < num_command_slots) &&
208 (!test_and_set_bit(slot, port->allocated)))
211 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
213 if (mtip_check_surprise_removal(port->dd->pdev)) {
214 /* Device not present, clean outstanding commands */
215 mtip_command_cleanup(port->dd);
221 * Release a command slot.
223 * @port Pointer to the port data structure.
224 * @tag Tag of command to release
229 static inline void release_slot(struct mtip_port *port, int tag)
231 smp_mb__before_clear_bit();
232 clear_bit(tag, port->allocated);
233 smp_mb__after_clear_bit();
237 * Reset the HBA (without sleeping)
239 * Just like hba_reset, except does not call sleep, so can be
240 * run from interrupt/tasklet context.
242 * @dd Pointer to the driver data structure.
245 * 0 The reset was successful.
246 * -1 The HBA Reset bit did not clear.
248 static int hba_reset_nosleep(struct driver_data *dd)
250 unsigned long timeout;
252 /* Chip quirk: quiesce any chip function */
255 /* Set the reset bit */
256 writel(HOST_RESET, dd->mmio + HOST_CTL);
259 readl(dd->mmio + HOST_CTL);
262 * Wait 10ms then spin for up to 1 second
263 * waiting for reset acknowledgement
265 timeout = jiffies + msecs_to_jiffies(1000);
267 while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
268 && time_before(jiffies, timeout))
271 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
278 * Issue a command to the hardware.
280 * Set the appropriate bit in the s_active and Command Issue hardware
281 * registers, causing hardware command processing to begin.
283 * @port Pointer to the port structure.
284 * @tag The tag of the command to be issued.
289 static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
291 unsigned long flags = 0;
293 atomic_set(&port->commands[tag].active, 1);
295 spin_lock_irqsave(&port->cmd_issue_lock, flags);
297 writel((1 << MTIP_TAG_BIT(tag)),
298 port->s_active[MTIP_TAG_INDEX(tag)]);
299 writel((1 << MTIP_TAG_BIT(tag)),
300 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
302 spin_unlock_irqrestore(&port->cmd_issue_lock, flags);
306 * Enable/disable the reception of FIS
308 * @port Pointer to the port data structure
309 * @enable 1 to enable, 0 to disable
312 * Previous state: 1 enabled, 0 disabled
314 static int mtip_enable_fis(struct mtip_port *port, int enable)
318 /* enable FIS reception */
319 tmp = readl(port->mmio + PORT_CMD);
321 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
323 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
326 readl(port->mmio + PORT_CMD);
328 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
332 * Enable/disable the DMA engine
334 * @port Pointer to the port data structure
335 * @enable 1 to enable, 0 to disable
338 * Previous state: 1 enabled, 0 disabled.
340 static int mtip_enable_engine(struct mtip_port *port, int enable)
344 /* enable FIS reception */
345 tmp = readl(port->mmio + PORT_CMD);
347 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
349 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
351 readl(port->mmio + PORT_CMD);
352 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
356 * Enables the port DMA engine and FIS reception.
361 static inline void mtip_start_port(struct mtip_port *port)
363 /* Enable FIS reception */
364 mtip_enable_fis(port, 1);
366 /* Enable the DMA engine */
367 mtip_enable_engine(port, 1);
371 * Deinitialize a port by disabling port interrupts, the DMA engine,
374 * @port Pointer to the port structure
379 static inline void mtip_deinit_port(struct mtip_port *port)
381 /* Disable interrupts on this port */
382 writel(0, port->mmio + PORT_IRQ_MASK);
384 /* Disable the DMA engine */
385 mtip_enable_engine(port, 0);
387 /* Disable FIS reception */
388 mtip_enable_fis(port, 0);
394 * This function deinitializes the port by calling mtip_deinit_port() and
395 * then initializes it by setting the command header and RX FIS addresses,
396 * clearing the SError register and any pending port interrupts before
397 * re-enabling the default set of port interrupts.
399 * @port Pointer to the port structure.
404 static void mtip_init_port(struct mtip_port *port)
407 mtip_deinit_port(port);
409 /* Program the command list base and FIS base addresses */
410 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
411 writel((port->command_list_dma >> 16) >> 16,
412 port->mmio + PORT_LST_ADDR_HI);
413 writel((port->rxfis_dma >> 16) >> 16,
414 port->mmio + PORT_FIS_ADDR_HI);
417 writel(port->command_list_dma & 0xffffffff,
418 port->mmio + PORT_LST_ADDR);
419 writel(port->rxfis_dma & 0xffffffff, port->mmio + PORT_FIS_ADDR);
422 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
424 /* reset the completed registers.*/
425 for (i = 0; i < port->dd->slot_groups; i++)
426 writel(0xFFFFFFFF, port->completed[i]);
428 /* Clear any pending interrupts for this port */
429 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
431 /* Enable port interrupts */
432 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
438 * @port Pointer to the port data structure.
443 static void mtip_restart_port(struct mtip_port *port)
445 unsigned long timeout;
447 /* Disable the DMA engine */
448 mtip_enable_engine(port, 0);
450 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
451 timeout = jiffies + msecs_to_jiffies(500);
452 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
453 && time_before(jiffies, timeout))
457 * Chip quirk: escalate to hba reset if
458 * PxCMD.CR not clear after 500 ms
460 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
461 dev_warn(&port->dd->pdev->dev,
462 "PxCMD.CR not clear, escalating reset\n");
464 if (hba_reset_nosleep(port->dd))
465 dev_err(&port->dd->pdev->dev,
466 "HBA reset escalation failed.\n");
468 /* 30 ms delay before com reset to quiesce chip */
472 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
475 writel(readl(port->mmio + PORT_SCR_CTL) |
476 1, port->mmio + PORT_SCR_CTL);
477 readl(port->mmio + PORT_SCR_CTL);
479 /* Wait 1 ms to quiesce chip function */
480 timeout = jiffies + msecs_to_jiffies(1);
481 while (time_before(jiffies, timeout))
484 /* Clear PxSCTL.DET */
485 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
486 port->mmio + PORT_SCR_CTL);
487 readl(port->mmio + PORT_SCR_CTL);
489 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
490 timeout = jiffies + msecs_to_jiffies(500);
491 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
492 && time_before(jiffies, timeout))
495 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
496 dev_warn(&port->dd->pdev->dev,
497 "COM reset failed\n");
499 /* Clear SError, the PxSERR.DIAG.x should be set so clear it */
500 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
502 /* Enable the DMA engine */
503 mtip_enable_engine(port, 1);
507 * Called periodically to see if any read/write commands are
508 * taking too long to complete.
510 * @data Pointer to the PORT data structure.
515 static void mtip_timeout_function(unsigned long int data)
517 struct mtip_port *port = (struct mtip_port *) data;
518 struct host_to_dev_fis *fis;
519 struct mtip_cmd *command;
520 int tag, cmdto_cnt = 0;
521 unsigned int bit, group;
522 unsigned int num_command_slots = port->dd->slot_groups * 32;
527 if (atomic_read(&port->dd->resumeflag) == true) {
528 mod_timer(&port->cmd_timer,
529 jiffies + msecs_to_jiffies(30000));
533 for (tag = 0; tag < num_command_slots; tag++) {
535 * Skip internal command slot as it has
536 * its own timeout mechanism
538 if (tag == MTIP_TAG_INTERNAL)
541 if (atomic_read(&port->commands[tag].active) &&
542 (time_after(jiffies, port->commands[tag].comp_time))) {
546 command = &port->commands[tag];
547 fis = (struct host_to_dev_fis *) command->command;
549 dev_warn(&port->dd->pdev->dev,
550 "Timeout for command tag %d\n", tag);
554 atomic_inc(&port->dd->eh_active);
557 * Clear the completed bit. This should prevent
558 * any interrupt handlers from trying to retire
561 writel(1 << bit, port->completed[group]);
563 /* Call the async completion callback. */
564 if (likely(command->async_callback))
565 command->async_callback(command->async_data,
567 command->async_callback = NULL;
568 command->comp_func = NULL;
570 /* Unmap the DMA scatter list entries */
571 dma_unmap_sg(&port->dd->pdev->dev,
573 command->scatter_ents,
577 * Clear the allocated bit and active tag for the
580 atomic_set(&port->commands[tag].active, 0);
581 release_slot(port, tag);
588 dev_warn(&port->dd->pdev->dev,
589 "%d commands timed out: restarting port",
591 mtip_restart_port(port);
592 atomic_dec(&port->dd->eh_active);
595 /* Restart the timer */
596 mod_timer(&port->cmd_timer,
597 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
601 * IO completion function.
603 * This completion function is called by the driver ISR when a
604 * command that was issued by the kernel completes. It first calls the
605 * asynchronous completion function which normally calls back into the block
606 * layer passing the asynchronous callback data, then unmaps the
607 * scatter list associated with the completed command, and finally
608 * clears the allocated bit associated with the completed command.
610 * @port Pointer to the port data structure.
611 * @tag Tag of the command.
612 * @data Pointer to driver_data.
613 * @status Completion status.
618 static void mtip_async_complete(struct mtip_port *port,
623 struct mtip_cmd *command;
624 struct driver_data *dd = data;
625 int cb_status = status ? -EIO : 0;
627 if (unlikely(!dd) || unlikely(!port))
630 command = &port->commands[tag];
632 if (unlikely(status == PORT_IRQ_TF_ERR)) {
633 dev_warn(&port->dd->pdev->dev,
634 "Command tag %d failed due to TFE\n", tag);
637 /* Upper layer callback */
638 if (likely(command->async_callback))
639 command->async_callback(command->async_data, cb_status);
641 command->async_callback = NULL;
642 command->comp_func = NULL;
644 /* Unmap the DMA scatter list entries */
645 dma_unmap_sg(&dd->pdev->dev,
647 command->scatter_ents,
650 /* Clear the allocated and active bits for the command */
651 atomic_set(&port->commands[tag].active, 0);
652 release_slot(port, tag);
658 * Internal command completion callback function.
660 * This function is normally called by the driver ISR when an internal
661 * command completed. This function signals the command completion by
662 * calling complete().
664 * @port Pointer to the port data structure.
665 * @tag Tag of the command that has completed.
666 * @data Pointer to a completion structure.
667 * @status Completion status.
672 static void mtip_completion(struct mtip_port *port,
677 struct mtip_cmd *command = &port->commands[tag];
678 struct completion *waiting = data;
679 if (unlikely(status == PORT_IRQ_TF_ERR))
680 dev_warn(&port->dd->pdev->dev,
681 "Internal command %d completed with TFE\n", tag);
683 command->async_callback = NULL;
684 command->comp_func = NULL;
690 * Helper function for tag logging
692 static void print_tags(struct driver_data *dd,
694 unsigned long *tagbits)
696 unsigned int tag, count = 0;
698 for (tag = 0; tag < (dd->slot_groups) * 32; tag++) {
699 if (test_bit(tag, tagbits))
703 dev_info(&dd->pdev->dev, "%s [%i tags]\n", msg, count);
709 * @dd Pointer to the DRIVER_DATA structure.
714 static void mtip_handle_tfe(struct driver_data *dd)
716 int group, tag, bit, reissue;
717 struct mtip_port *port;
718 struct mtip_cmd *command;
720 struct host_to_dev_fis *fis;
721 unsigned long tagaccum[SLOTBITS_IN_LONGS];
723 dev_warn(&dd->pdev->dev, "Taskfile error\n");
727 /* Stop the timer to prevent command timeouts. */
728 del_timer(&port->cmd_timer);
731 atomic_inc(&dd->eh_active);
733 /* Loop through all the groups */
734 for (group = 0; group < dd->slot_groups; group++) {
735 completed = readl(port->completed[group]);
737 /* clear completed status register in the hardware.*/
738 writel(completed, port->completed[group]);
740 /* clear the tag accumulator */
741 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
743 /* Process successfully completed commands */
744 for (bit = 0; bit < 32 && completed; bit++) {
745 if (!(completed & (1<<bit)))
747 tag = (group << 5) + bit;
749 /* Skip the internal command slot */
750 if (tag == MTIP_TAG_INTERNAL)
753 command = &port->commands[tag];
754 if (likely(command->comp_func)) {
755 set_bit(tag, tagaccum);
756 atomic_set(&port->commands[tag].active, 0);
757 command->comp_func(port,
762 dev_err(&port->dd->pdev->dev,
763 "Missing completion func for tag %d",
765 if (mtip_check_surprise_removal(dd->pdev)) {
766 mtip_command_cleanup(dd);
767 /* don't proceed further */
773 print_tags(dd, "TFE tags completed:", tagaccum);
775 /* Restart the port */
777 mtip_restart_port(port);
779 /* clear the tag accumulator */
780 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
782 /* Loop through all the groups */
783 for (group = 0; group < dd->slot_groups; group++) {
784 for (bit = 0; bit < 32; bit++) {
786 tag = (group << 5) + bit;
788 /* If the active bit is set re-issue the command */
789 if (atomic_read(&port->commands[tag].active) == 0)
792 fis = (struct host_to_dev_fis *)
793 port->commands[tag].command;
795 /* Should re-issue? */
796 if (tag == MTIP_TAG_INTERNAL ||
797 fis->command == ATA_CMD_SET_FEATURES)
801 * First check if this command has
802 * exceeded its retries.
805 (port->commands[tag].retries-- > 0)) {
807 set_bit(tag, tagaccum);
809 /* Update the timeout value. */
810 port->commands[tag].comp_time =
811 jiffies + msecs_to_jiffies(
812 MTIP_NCQ_COMMAND_TIMEOUT_MS);
813 /* Re-issue the command. */
814 mtip_issue_ncq_command(port, tag);
819 /* Retire a command that will not be reissued */
820 dev_warn(&port->dd->pdev->dev,
821 "retiring tag %d\n", tag);
822 atomic_set(&port->commands[tag].active, 0);
824 if (port->commands[tag].comp_func)
825 port->commands[tag].comp_func(
828 port->commands[tag].comp_data,
831 dev_warn(&port->dd->pdev->dev,
832 "Bad completion for tag %d\n",
836 print_tags(dd, "TFE tags reissued:", tagaccum);
838 /* Decrement eh_active */
839 atomic_dec(&dd->eh_active);
841 mod_timer(&port->cmd_timer,
842 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
846 * Handle a set device bits interrupt
848 static inline void mtip_process_sdbf(struct driver_data *dd)
850 struct mtip_port *port = dd->port;
853 struct mtip_cmd *command;
855 /* walk all bits in all slot groups */
856 for (group = 0; group < dd->slot_groups; group++) {
857 completed = readl(port->completed[group]);
859 /* clear completed status register in the hardware.*/
860 writel(completed, port->completed[group]);
862 /* Process completed commands. */
864 (bit < 32) && completed;
865 bit++, completed >>= 1) {
866 if (completed & 0x01) {
867 tag = (group << 5) | bit;
869 /* skip internal command slot. */
870 if (unlikely(tag == MTIP_TAG_INTERNAL))
873 command = &port->commands[tag];
875 /* make internal callback */
876 if (likely(command->comp_func)) {
883 dev_warn(&dd->pdev->dev,
888 if (mtip_check_surprise_removal(
890 mtip_command_cleanup(dd);
900 * Process legacy pio and d2h interrupts
902 static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
904 struct mtip_port *port = dd->port;
905 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
907 if (port->internal_cmd_in_progress &&
909 !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
910 & (1 << MTIP_TAG_INTERNAL))) {
911 if (cmd->comp_func) {
920 dev_warn(&dd->pdev->dev, "IRQ status 0x%x ignored.\n", port_stat);
926 * Demux and handle errors
928 static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
930 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
933 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
934 dev_warn(&dd->pdev->dev,
935 "Clearing PxSERR.DIAG.x\n");
936 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
939 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
940 dev_warn(&dd->pdev->dev,
941 "Clearing PxSERR.DIAG.n\n");
942 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
945 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
946 dev_warn(&dd->pdev->dev,
947 "Port stat errors %x unhandled\n",
948 (port_stat & ~PORT_IRQ_HANDLED));
952 static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
954 struct driver_data *dd = (struct driver_data *) data;
955 struct mtip_port *port = dd->port;
956 u32 hba_stat, port_stat;
959 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
963 /* Acknowledge the interrupt status on the port.*/
964 port_stat = readl(port->mmio + PORT_IRQ_STAT);
965 writel(port_stat, port->mmio + PORT_IRQ_STAT);
967 /* Demux port status */
968 if (likely(port_stat & PORT_IRQ_SDB_FIS))
969 mtip_process_sdbf(dd);
971 if (unlikely(port_stat & PORT_IRQ_ERR)) {
972 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
973 mtip_command_cleanup(dd);
974 /* don't proceed further */
978 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
981 if (unlikely(port_stat & PORT_IRQ_LEGACY))
982 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
985 /* acknowledge interrupt */
986 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
992 * Wrapper for mtip_handle_irq
993 * (ignores return code)
995 static void mtip_tasklet(unsigned long data)
997 mtip_handle_irq((struct driver_data *) data);
1001 * HBA interrupt subroutine.
1004 * @instance Pointer to the driver data structure.
1007 * IRQ_HANDLED A HBA interrupt was pending and handled.
1008 * IRQ_NONE This interrupt was not for the HBA.
1010 static irqreturn_t mtip_irq_handler(int irq, void *instance)
1012 struct driver_data *dd = instance;
1013 tasklet_schedule(&dd->tasklet);
1017 static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1019 atomic_set(&port->commands[tag].active, 1);
1020 writel(1 << MTIP_TAG_BIT(tag),
1021 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1025 * Wait for port to quiesce
1027 * @port Pointer to port data structure
1028 * @timeout Max duration to wait (ms)
1032 * -EBUSY Commands still active
1034 static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1037 unsigned int n, active;
1039 to = jiffies + msecs_to_jiffies(timeout);
1042 * Ignore s_active bit 0 of array element 0.
1043 * This bit will always be set
1045 active = readl(port->s_active[0]) & 0xfffffffe;
1046 for (n = 1; n < port->dd->slot_groups; n++)
1047 active |= readl(port->s_active[n]);
1053 } while (time_before(jiffies, to));
1055 return active ? -EBUSY : 0;
1059 * Execute an internal command and wait for the completion.
1061 * @port Pointer to the port data structure.
1062 * @fis Pointer to the FIS that describes the command.
1063 * @fisLen Length in WORDS of the FIS.
1064 * @buffer DMA accessible for command data.
1065 * @bufLen Length, in bytes, of the data buffer.
1066 * @opts Command header options, excluding the FIS length
1067 * and the number of PRD entries.
1068 * @timeout Time in ms to wait for the command to complete.
1071 * 0 Command completed successfully.
1072 * -EFAULT The buffer address is not correctly aligned.
1073 * -EBUSY Internal command or other IO in progress.
1074 * -EAGAIN Time out waiting for command to complete.
1076 static int mtip_exec_internal_command(struct mtip_port *port,
1083 unsigned long timeout)
1085 struct mtip_cmd_sg *command_sg;
1086 DECLARE_COMPLETION_ONSTACK(wait);
1088 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
1090 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1091 if (buffer & 0x00000007) {
1092 dev_err(&port->dd->pdev->dev,
1093 "SG buffer is not 8 byte aligned\n");
1097 /* Only one internal command should be running at a time */
1098 if (test_and_set_bit(MTIP_TAG_INTERNAL, port->allocated)) {
1099 dev_warn(&port->dd->pdev->dev,
1100 "Internal command already active\n");
1103 port->internal_cmd_in_progress = 1;
1105 if (atomic == GFP_KERNEL) {
1106 /* wait for io to complete if non atomic */
1107 if (mtip_quiesce_io(port, 5000) < 0) {
1108 dev_warn(&port->dd->pdev->dev,
1109 "Failed to quiesce IO\n");
1110 release_slot(port, MTIP_TAG_INTERNAL);
1111 port->internal_cmd_in_progress = 0;
1115 /* Set the completion function and data for the command. */
1116 int_cmd->comp_data = &wait;
1117 int_cmd->comp_func = mtip_completion;
1120 /* Clear completion - we're going to poll */
1121 int_cmd->comp_data = NULL;
1122 int_cmd->comp_func = NULL;
1125 /* Copy the command to the command table */
1126 memcpy(int_cmd->command, fis, fisLen*4);
1128 /* Populate the SG list */
1129 int_cmd->command_header->opts =
1130 cpu_to_le32(opts | fisLen);
1132 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1134 command_sg->info = cpu_to_le32((bufLen-1) & 0x3fffff);
1135 command_sg->dba = cpu_to_le32(buffer & 0xffffffff);
1136 command_sg->dba_upper = cpu_to_le32((buffer >> 16) >> 16);
1138 int_cmd->command_header->opts |= cpu_to_le32((1 << 16));
1141 /* Populate the command header */
1142 int_cmd->command_header->byte_count = 0;
1144 /* Issue the command to the hardware */
1145 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1147 /* Poll if atomic, wait_for_completion otherwise */
1148 if (atomic == GFP_KERNEL) {
1149 /* Wait for the command to complete or timeout. */
1150 if (wait_for_completion_timeout(
1152 msecs_to_jiffies(timeout)) == 0) {
1153 dev_err(&port->dd->pdev->dev,
1154 "Internal command did not complete [%d]\n",
1159 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1160 & (1 << MTIP_TAG_INTERNAL)) {
1161 dev_warn(&port->dd->pdev->dev,
1162 "Retiring internal command but CI is 1.\n");
1166 /* Spin for <timeout> checking if command still outstanding */
1167 timeout = jiffies + msecs_to_jiffies(timeout);
1170 port->cmd_issue[MTIP_TAG_INTERNAL])
1171 & (1 << MTIP_TAG_INTERNAL))
1172 && time_before(jiffies, timeout))
1175 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1176 & (1 << MTIP_TAG_INTERNAL)) {
1177 dev_err(&port->dd->pdev->dev,
1178 "Internal command did not complete [%d]\n",
1184 /* Clear the allocated and active bits for the internal command. */
1185 atomic_set(&int_cmd->active, 0);
1186 release_slot(port, MTIP_TAG_INTERNAL);
1187 port->internal_cmd_in_progress = 0;
1193 * Byte-swap ATA ID strings.
1195 * ATA identify data contains strings in byte-swapped 16-bit words.
1196 * They must be swapped (on all architectures) to be usable as C strings.
1197 * This function swaps bytes in-place.
1199 * @buf The buffer location of the string
1200 * @len The number of bytes to swap
1205 static inline void ata_swap_string(u16 *buf, unsigned int len)
1208 for (i = 0; i < (len/2); i++)
1209 be16_to_cpus(&buf[i]);
1213 * Request the device identity information.
1215 * If a user space buffer is not specified, i.e. is NULL, the
1216 * identify information is still read from the drive and placed
1217 * into the identify data buffer (@e port->identify) in the
1218 * port data structure.
1219 * When the identify buffer contains valid identify information @e
1220 * port->identify_valid is non-zero.
1222 * @port Pointer to the port structure.
1223 * @user_buffer A user space buffer where the identify data should be
1227 * 0 Command completed successfully.
1228 * -EFAULT An error occurred while coping data to the user buffer.
1229 * -1 Command failed.
1231 static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1234 struct host_to_dev_fis fis;
1236 down_write(&port->dd->internal_sem);
1238 /* Build the FIS. */
1239 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1242 fis.command = ATA_CMD_ID_ATA;
1244 /* Set the identify information as invalid. */
1245 port->identify_valid = 0;
1247 /* Clear the identify information. */
1248 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1250 /* Execute the command. */
1251 if (mtip_exec_internal_command(port,
1255 sizeof(u16) * ATA_ID_WORDS,
1258 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1265 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1266 * perform field-sensitive swapping on the string fields.
1267 * See the kernel use of ata_id_string() for proof of this.
1269 #ifdef __LITTLE_ENDIAN
1270 ata_swap_string(port->identify + 27, 40); /* model string*/
1271 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1272 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1276 for (i = 0; i < ATA_ID_WORDS; i++)
1277 port->identify[i] = le16_to_cpu(port->identify[i]);
1281 /* Set the identify buffer as valid. */
1282 port->identify_valid = 1;
1288 ATA_ID_WORDS * sizeof(u16))) {
1295 up_write(&port->dd->internal_sem);
1300 * Issue a standby immediate command to the device.
1302 * @port Pointer to the port structure.
1305 * 0 Command was executed successfully.
1306 * -1 An error occurred while executing the command.
1308 static int mtip_standby_immediate(struct mtip_port *port)
1311 struct host_to_dev_fis fis;
1313 down_write(&port->dd->internal_sem);
1315 /* Build the FIS. */
1316 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1319 fis.command = ATA_CMD_STANDBYNOW1;
1321 /* Execute the command. Use a 15-second timeout for large drives. */
1322 rv = mtip_exec_internal_command(port,
1331 up_write(&port->dd->internal_sem);
1337 * Get the drive capacity.
1339 * @dd Pointer to the device data structure.
1340 * @sectors Pointer to the variable that will receive the sector count.
1343 * 1 Capacity was returned successfully.
1344 * 0 The identify information is invalid.
1346 static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
1348 struct mtip_port *port = dd->port;
1349 u64 total, raw0, raw1, raw2, raw3;
1350 raw0 = port->identify[100];
1351 raw1 = port->identify[101];
1352 raw2 = port->identify[102];
1353 raw3 = port->identify[103];
1354 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1356 return (bool) !!port->identify_valid;
1362 * Resets the HBA by setting the HBA Reset bit in the Global
1363 * HBA Control register. After setting the HBA Reset bit the
1364 * function waits for 1 second before reading the HBA Reset
1365 * bit to make sure it has cleared. If HBA Reset is not clear
1366 * an error is returned. Cannot be used in non-blockable
1369 * @dd Pointer to the driver data structure.
1372 * 0 The reset was successful.
1373 * -1 The HBA Reset bit did not clear.
1375 static int mtip_hba_reset(struct driver_data *dd)
1377 mtip_deinit_port(dd->port);
1379 /* Set the reset bit */
1380 writel(HOST_RESET, dd->mmio + HOST_CTL);
1383 readl(dd->mmio + HOST_CTL);
1385 /* Wait for reset to clear */
1388 /* Check the bit has cleared */
1389 if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
1390 dev_err(&dd->pdev->dev,
1391 "Reset bit did not clear.\n");
1399 * Display the identify command data.
1401 * @port Pointer to the port data structure.
1406 static void mtip_dump_identify(struct mtip_port *port)
1409 unsigned short revid;
1412 if (!port->identify_valid)
1415 strlcpy(cbuf, (char *)(port->identify+10), 21);
1416 dev_info(&port->dd->pdev->dev,
1417 "Serial No.: %s\n", cbuf);
1419 strlcpy(cbuf, (char *)(port->identify+23), 9);
1420 dev_info(&port->dd->pdev->dev,
1421 "Firmware Ver.: %s\n", cbuf);
1423 strlcpy(cbuf, (char *)(port->identify+27), 41);
1424 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1426 if (mtip_hw_get_capacity(port->dd, §ors))
1427 dev_info(&port->dd->pdev->dev,
1428 "Capacity: %llu sectors (%llu MB)\n",
1430 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1432 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
1433 switch (revid & 0xff) {
1435 strlcpy(cbuf, "A0", 3);
1438 strlcpy(cbuf, "A2", 3);
1441 strlcpy(cbuf, "?", 2);
1444 dev_info(&port->dd->pdev->dev,
1445 "Card Type: %s\n", cbuf);
1449 * Map the commands scatter list into the command table.
1451 * @command Pointer to the command.
1452 * @nents Number of scatter list entries.
1457 static inline void fill_command_sg(struct driver_data *dd,
1458 struct mtip_cmd *command,
1462 unsigned int dma_len;
1463 struct mtip_cmd_sg *command_sg;
1464 struct scatterlist *sg = command->sg;
1466 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1468 for (n = 0; n < nents; n++) {
1469 dma_len = sg_dma_len(sg);
1470 if (dma_len > 0x400000)
1471 dev_err(&dd->pdev->dev,
1472 "DMA segment length truncated\n");
1473 command_sg->info = cpu_to_le32((dma_len-1) & 0x3fffff);
1474 #if (BITS_PER_LONG == 64)
1475 *((unsigned long *) &command_sg->dba) =
1476 cpu_to_le64(sg_dma_address(sg));
1478 command_sg->dba = cpu_to_le32(sg_dma_address(sg));
1479 command_sg->dba_upper =
1480 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
1488 * @brief Execute a drive command.
1490 * return value 0 The command completed successfully.
1491 * return value -1 An error occurred while executing the command.
1493 static int exec_drive_task(struct mtip_port *port, u8 *command)
1495 struct host_to_dev_fis fis;
1496 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1498 /* Lock the internal command semaphore. */
1499 down_write(&port->dd->internal_sem);
1501 /* Build the FIS. */
1502 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1505 fis.command = command[0];
1506 fis.features = command[1];
1507 fis.sect_count = command[2];
1508 fis.sector = command[3];
1509 fis.cyl_low = command[4];
1510 fis.cyl_hi = command[5];
1511 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1514 dbg_printk(MTIP_DRV_NAME "%s: User Command: cmd %x, feat %x, "
1515 "nsect %x, sect %x, lcyl %x, "
1516 "hcyl %x, sel %x\n",
1526 /* Execute the command. */
1527 if (mtip_exec_internal_command(port,
1534 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
1535 up_write(&port->dd->internal_sem);
1539 command[0] = reply->command; /* Status*/
1540 command[1] = reply->features; /* Error*/
1541 command[4] = reply->cyl_low;
1542 command[5] = reply->cyl_hi;
1544 dbg_printk(MTIP_DRV_NAME "%s: Completion Status: stat %x, "
1545 "err %x , cyl_lo %x cyl_hi %x\n",
1552 up_write(&port->dd->internal_sem);
1557 * @brief Execute a drive command.
1559 * @param port Pointer to the port data structure.
1560 * @param command Pointer to the user specified command parameters.
1561 * @param user_buffer Pointer to the user space buffer where read sector
1562 * data should be copied.
1564 * return value 0 The command completed successfully.
1565 * return value -EFAULT An error occurred while copying the completion
1566 * data to the user space buffer.
1567 * return value -1 An error occurred while executing the command.
1569 static int exec_drive_command(struct mtip_port *port, u8 *command,
1570 void __user *user_buffer)
1572 struct host_to_dev_fis fis;
1573 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1575 /* Lock the internal command semaphore. */
1576 down_write(&port->dd->internal_sem);
1578 /* Build the FIS. */
1579 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1582 fis.command = command[0];
1583 fis.features = command[2];
1584 fis.sect_count = command[3];
1585 if (fis.command == ATA_CMD_SMART) {
1586 fis.sector = command[1];
1591 dbg_printk(MTIP_DRV_NAME
1592 "%s: User Command: cmd %x, sect %x, "
1593 "feat %x, sectcnt %x\n",
1600 memset(port->sector_buffer, 0x00, ATA_SECT_SIZE);
1602 /* Execute the command. */
1603 if (mtip_exec_internal_command(port,
1606 port->sector_buffer_dma,
1607 (command[3] != 0) ? ATA_SECT_SIZE : 0,
1610 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
1612 up_write(&port->dd->internal_sem);
1616 /* Collect the completion status. */
1617 command[0] = reply->command; /* Status*/
1618 command[1] = reply->features; /* Error*/
1619 command[2] = command[3];
1621 dbg_printk(MTIP_DRV_NAME
1622 "%s: Completion Status: stat %x, "
1629 if (user_buffer && command[3]) {
1630 if (copy_to_user(user_buffer,
1631 port->sector_buffer,
1632 ATA_SECT_SIZE * command[3])) {
1633 up_write(&port->dd->internal_sem);
1638 up_write(&port->dd->internal_sem);
1643 * Indicates whether a command has a single sector payload.
1645 * @command passed to the device to perform the certain event.
1646 * @features passed to the device to perform the certain event.
1649 * 1 command is one that always has a single sector payload,
1650 * regardless of the value in the Sector Count field.
1654 static unsigned int implicit_sector(unsigned char command,
1655 unsigned char features)
1657 unsigned int rv = 0;
1659 /* list of commands that have an implicit sector count of 1 */
1672 if (features == 0x03)
1676 if ((features == 0xD0) || (features == 0xD1))
1680 if ((features == 0xC2) || (features == 0xC3))
1688 * Executes a taskfile
1689 * See ide_taskfile_ioctl() for derivation
1691 static int exec_drive_taskfile(struct driver_data *dd,
1693 ide_task_request_t *req_task,
1696 struct host_to_dev_fis fis;
1697 struct host_to_dev_fis *reply;
1700 dma_addr_t outbuf_dma = 0;
1701 dma_addr_t inbuf_dma = 0;
1702 dma_addr_t dma_buffer = 0;
1704 unsigned int taskin = 0;
1705 unsigned int taskout = 0;
1707 unsigned int timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1708 unsigned int force_single_sector;
1709 unsigned int transfer_size;
1710 unsigned long task_file_data;
1711 int intotal = outtotal + req_task->out_size;
1713 taskout = req_task->out_size;
1714 taskin = req_task->in_size;
1715 /* 130560 = 512 * 0xFF*/
1716 if (taskin > 130560 || taskout > 130560) {
1722 outbuf = kzalloc(taskout, GFP_KERNEL);
1723 if (outbuf == NULL) {
1727 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
1731 outbuf_dma = pci_map_single(dd->pdev,
1735 if (outbuf_dma == 0) {
1739 dma_buffer = outbuf_dma;
1743 inbuf = kzalloc(taskin, GFP_KERNEL);
1744 if (inbuf == NULL) {
1749 if (copy_from_user(inbuf, buf + intotal, taskin)) {
1753 inbuf_dma = pci_map_single(dd->pdev,
1755 taskin, DMA_FROM_DEVICE);
1756 if (inbuf_dma == 0) {
1760 dma_buffer = inbuf_dma;
1763 /* only supports PIO and non-data commands from this ioctl. */
1764 switch (req_task->data_phase) {
1766 nsect = taskout / ATA_SECT_SIZE;
1767 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1770 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1772 case TASKFILE_NO_DATA:
1773 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
1780 /* Lock the internal command semaphore. */
1781 down_write(&dd->internal_sem);
1783 /* Build the FIS. */
1784 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1788 fis.command = req_task->io_ports[7];
1789 fis.features = req_task->io_ports[1];
1790 fis.sect_count = req_task->io_ports[2];
1791 fis.lba_low = req_task->io_ports[3];
1792 fis.lba_mid = req_task->io_ports[4];
1793 fis.lba_hi = req_task->io_ports[5];
1794 /* Clear the dev bit*/
1795 fis.device = req_task->io_ports[6] & ~0x10;
1797 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
1798 req_task->in_flags.all =
1799 IDE_TASKFILE_STD_IN_FLAGS |
1800 (IDE_HOB_STD_IN_FLAGS << 8);
1801 fis.lba_low_ex = req_task->hob_ports[3];
1802 fis.lba_mid_ex = req_task->hob_ports[4];
1803 fis.lba_hi_ex = req_task->hob_ports[5];
1804 fis.features_ex = req_task->hob_ports[1];
1805 fis.sect_cnt_ex = req_task->hob_ports[2];
1808 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
1811 force_single_sector = implicit_sector(fis.command, fis.features);
1813 if ((taskin || taskout) && (!fis.sect_count)) {
1815 fis.sect_count = nsect;
1817 if (!force_single_sector) {
1818 dev_warn(&dd->pdev->dev,
1819 "data movement but "
1820 "sect_count is 0\n");
1821 up_write(&dd->internal_sem);
1828 dbg_printk(MTIP_DRV_NAME
1829 "taskfile: cmd %x, feat %x, nsect %x,"
1830 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
1840 switch (fis.command) {
1841 case 0x92: /* Change timeout for Download Microcode to 60 seconds.*/
1844 case 0xf4: /* Change timeout for Security Erase Unit to 4 minutes.*/
1847 case 0xe0: /* Change timeout for standby immediate to 10 seconds.*/
1850 case 0xf7: /* Change timeout for vendor unique command to 10 secs */
1853 case 0xfa: /* Change timeout for vendor unique command to 10 secs */
1857 timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1861 /* Determine the correct transfer size.*/
1862 if (force_single_sector)
1863 transfer_size = ATA_SECT_SIZE;
1865 transfer_size = ATA_SECT_SIZE * fis.sect_count;
1867 /* Execute the command.*/
1868 if (mtip_exec_internal_command(dd->port,
1876 up_write(&dd->internal_sem);
1881 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
1883 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
1884 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
1885 req_task->io_ports[7] = reply->control;
1887 reply = dd->port->rxfis + RX_FIS_D2H_REG;
1888 req_task->io_ports[7] = reply->command;
1891 /* reclaim the DMA buffers.*/
1893 pci_unmap_single(dd->pdev, inbuf_dma,
1894 taskin, DMA_FROM_DEVICE);
1896 pci_unmap_single(dd->pdev, outbuf_dma,
1897 taskout, DMA_TO_DEVICE);
1901 /* return the ATA registers to the caller.*/
1902 req_task->io_ports[1] = reply->features;
1903 req_task->io_ports[2] = reply->sect_count;
1904 req_task->io_ports[3] = reply->lba_low;
1905 req_task->io_ports[4] = reply->lba_mid;
1906 req_task->io_ports[5] = reply->lba_hi;
1907 req_task->io_ports[6] = reply->device;
1909 if (req_task->out_flags.all & 1) {
1911 req_task->hob_ports[3] = reply->lba_low_ex;
1912 req_task->hob_ports[4] = reply->lba_mid_ex;
1913 req_task->hob_ports[5] = reply->lba_hi_ex;
1914 req_task->hob_ports[1] = reply->features_ex;
1915 req_task->hob_ports[2] = reply->sect_cnt_ex;
1918 /* Com rest after secure erase or lowlevel format */
1919 if (((fis.command == 0xF4) ||
1920 ((fis.command == 0xFC) &&
1921 (fis.features == 0x27 || fis.features == 0x72 ||
1922 fis.features == 0x62 || fis.features == 0x26))) &&
1923 !(reply->command & 1)) {
1924 mtip_restart_port(dd->port);
1927 dbg_printk(MTIP_DRV_NAME
1928 "%s: Completion: stat %x,"
1929 "err %x, sect_cnt %x, lbalo %x,"
1930 "lbamid %x, lbahi %x, dev %x\n",
1932 req_task->io_ports[7],
1933 req_task->io_ports[1],
1934 req_task->io_ports[2],
1935 req_task->io_ports[3],
1936 req_task->io_ports[4],
1937 req_task->io_ports[5],
1938 req_task->io_ports[6]);
1940 up_write(&dd->internal_sem);
1943 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
1949 if (copy_to_user(buf + intotal, inbuf, taskin)) {
1956 pci_unmap_single(dd->pdev, inbuf_dma,
1957 taskin, DMA_FROM_DEVICE);
1959 pci_unmap_single(dd->pdev, outbuf_dma,
1960 taskout, DMA_TO_DEVICE);
1968 * Handle IOCTL calls from the Block Layer.
1970 * This function is called by the Block Layer when it receives an IOCTL
1971 * command that it does not understand. If the IOCTL command is not supported
1972 * this function returns -ENOTTY.
1974 * @dd Pointer to the driver data structure.
1975 * @cmd IOCTL command passed from the Block Layer.
1976 * @arg IOCTL argument passed from the Block Layer.
1979 * 0 The IOCTL completed successfully.
1980 * -ENOTTY The specified command is not supported.
1981 * -EFAULT An error occurred copying data to a user space buffer.
1982 * -EIO An error occurred while executing the command.
1984 static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
1988 case HDIO_GET_IDENTITY:
1989 if (mtip_get_identify(dd->port, (void __user *) arg) < 0) {
1990 dev_warn(&dd->pdev->dev,
1991 "Unable to read identity\n");
1996 case HDIO_DRIVE_CMD:
1998 u8 drive_command[4];
2000 /* Copy the user command info to our buffer. */
2001 if (copy_from_user(drive_command,
2002 (void __user *) arg,
2003 sizeof(drive_command)))
2006 /* Execute the drive command. */
2007 if (exec_drive_command(dd->port,
2009 (void __user *) (arg+4)))
2012 /* Copy the status back to the users buffer. */
2013 if (copy_to_user((void __user *) arg,
2015 sizeof(drive_command)))
2020 case HDIO_DRIVE_TASK:
2022 u8 drive_command[7];
2024 /* Copy the user command info to our buffer. */
2025 if (copy_from_user(drive_command,
2026 (void __user *) arg,
2027 sizeof(drive_command)))
2030 /* Execute the drive command. */
2031 if (exec_drive_task(dd->port, drive_command))
2034 /* Copy the status back to the users buffer. */
2035 if (copy_to_user((void __user *) arg,
2037 sizeof(drive_command)))
2042 case HDIO_DRIVE_TASKFILE: {
2043 ide_task_request_t req_task;
2046 if (copy_from_user(&req_task, (void __user *) arg,
2050 outtotal = sizeof(req_task);
2052 ret = exec_drive_taskfile(dd, (void __user *) arg,
2053 &req_task, outtotal);
2055 if (copy_to_user((void __user *) arg, &req_task, sizeof(req_task)))
2068 * Submit an IO to the hw
2070 * This function is called by the block layer to issue an io
2071 * to the device. Upon completion, the callback function will
2072 * be called with the data parameter passed as the callback data.
2074 * @dd Pointer to the driver data structure.
2075 * @start First sector to read.
2076 * @nsect Number of sectors to read.
2077 * @nents Number of entries in scatter list for the read command.
2078 * @tag The tag of this read command.
2079 * @callback Pointer to the function that should be called
2080 * when the read completes.
2081 * @data Callback data passed to the callback function
2082 * when the read completes.
2083 * @barrier If non-zero, this command must be completed before
2084 * issuing any other commands.
2085 * @dir Direction (read or write)
2090 static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
2091 int nsect, int nents, int tag, void *callback,
2092 void *data, int barrier, int dir)
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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 static 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 * Helper function for reusing disk name
2781 * upon hot insertion.
2783 static int rssd_disk_name_format(char *prefix,
2788 const int base = 'z' - 'a' + 1;
2789 char *begin = buf + strlen(prefix);
2790 char *end = buf + buflen;
2800 *--p = 'a' + (index % unit);
2801 index = (index / unit) - 1;
2802 } while (index >= 0);
2804 memmove(begin, p, end - p);
2805 memcpy(buf, prefix, strlen(prefix));
2811 * Block layer IOCTL handler.
2813 * @dev Pointer to the block_device structure.
2815 * @cmd IOCTL command passed from the user application.
2816 * @arg Argument passed from the user application.
2819 * 0 IOCTL completed successfully.
2820 * -ENOTTY IOCTL not supported or invalid driver data
2821 * structure pointer.
2823 static int mtip_block_ioctl(struct block_device *dev,
2828 struct driver_data *dd = dev->bd_disk->private_data;
2830 if (!capable(CAP_SYS_ADMIN))
2840 return mtip_hw_ioctl(dd, cmd, arg);
2844 #ifdef CONFIG_COMPAT
2846 * Block layer compat IOCTL handler.
2848 * @dev Pointer to the block_device structure.
2850 * @cmd IOCTL command passed from the user application.
2851 * @arg Argument passed from the user application.
2854 * 0 IOCTL completed successfully.
2855 * -ENOTTY IOCTL not supported or invalid driver data
2856 * structure pointer.
2858 static int mtip_block_compat_ioctl(struct block_device *dev,
2863 struct driver_data *dd = dev->bd_disk->private_data;
2865 if (!capable(CAP_SYS_ADMIN))
2874 case HDIO_DRIVE_TASKFILE: {
2875 struct mtip_compat_ide_task_request_s *compat_req_task;
2876 ide_task_request_t req_task;
2877 int compat_tasksize, outtotal, ret;
2879 compat_tasksize = sizeof(struct mtip_compat_ide_task_request_s);
2882 (struct mtip_compat_ide_task_request_s __user *) arg;
2884 if (copy_from_user(&req_task, (void __user *) arg,
2885 compat_tasksize - (2 * sizeof(compat_long_t))))
2888 if (get_user(req_task.out_size, &compat_req_task->out_size))
2891 if (get_user(req_task.in_size, &compat_req_task->in_size))
2894 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
2896 ret = exec_drive_taskfile(dd, (void __user *) arg,
2897 &req_task, outtotal);
2899 if (copy_to_user((void __user *) arg, &req_task,
2901 (2 * sizeof(compat_long_t))))
2904 if (put_user(req_task.out_size, &compat_req_task->out_size))
2907 if (put_user(req_task.in_size, &compat_req_task->in_size))
2913 return mtip_hw_ioctl(dd, cmd, arg);
2919 * Obtain the geometry of the device.
2921 * You may think that this function is obsolete, but some applications,
2922 * fdisk for example still used CHS values. This function describes the
2923 * device as having 224 heads and 56 sectors per cylinder. These values are
2924 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
2925 * partition is described in terms of a start and end cylinder this means
2926 * that each partition is also 4KB aligned. Non-aligned partitions adversely
2927 * affects performance.
2929 * @dev Pointer to the block_device strucutre.
2930 * @geo Pointer to a hd_geometry structure.
2933 * 0 Operation completed successfully.
2934 * -ENOTTY An error occurred while reading the drive capacity.
2936 static int mtip_block_getgeo(struct block_device *dev,
2937 struct hd_geometry *geo)
2939 struct driver_data *dd = dev->bd_disk->private_data;
2945 if (!(mtip_hw_get_capacity(dd, &capacity))) {
2946 dev_warn(&dd->pdev->dev,
2947 "Could not get drive capacity.\n");
2953 #if BITS_PER_LONG == 64
2954 geo->cylinders = capacity / (geo->heads * geo->sectors);
2956 do_div(capacity, (geo->heads * geo->sectors));
2957 geo->cylinders = capacity;
2963 * Block device operation function.
2965 * This structure contains pointers to the functions required by the block
2968 static const struct block_device_operations mtip_block_ops = {
2969 .ioctl = mtip_block_ioctl,
2970 #ifdef CONFIG_COMPAT
2971 .compat_ioctl = mtip_block_compat_ioctl,
2973 .getgeo = mtip_block_getgeo,
2974 .owner = THIS_MODULE
2978 * Block layer make request function.
2980 * This function is called by the kernel to process a BIO for
2983 * @queue Pointer to the request queue. Unused other than to obtain
2984 * the driver data structure.
2985 * @bio Pointer to the BIO.
2990 static int mtip_make_request(struct request_queue *queue, struct bio *bio)
2992 struct driver_data *dd = queue->queuedata;
2993 struct scatterlist *sg;
2994 struct bio_vec *bvec;
2998 if (unlikely(!bio_has_data(bio))) {
2999 blk_queue_flush(queue, 0);
3004 if (unlikely(atomic_read(&dd->eh_active))) {
3005 bio_endio(bio, -EBUSY);
3009 sg = mtip_hw_get_scatterlist(dd, &tag);
3010 if (likely(sg != NULL)) {
3011 blk_queue_bounce(queue, &bio);
3013 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3014 dev_warn(&dd->pdev->dev,
3015 "Maximum number of SGL entries exceeded");
3017 mtip_hw_release_scatterlist(dd, tag);
3021 /* Create the scatter list for this bio. */
3022 bio_for_each_segment(bvec, bio, nents) {
3023 sg_set_page(&sg[nents],
3029 /* Issue the read/write. */
3030 mtip_hw_submit_io(dd,
3037 bio->bi_rw & REQ_FLUSH,
3047 * Block layer initialization function.
3049 * This function is called once by the PCI layer for each P320
3050 * device that is connected to the system.
3052 * @dd Pointer to the driver data structure.
3055 * 0 on success else an error code.
3057 static int mtip_block_initialize(struct driver_data *dd)
3061 unsigned int index = 0;
3062 struct kobject *kobj;
3064 /* Initialize the protocol layer. */
3065 rv = mtip_hw_init(dd);
3067 dev_err(&dd->pdev->dev,
3068 "Protocol layer initialization failed\n");
3070 goto protocol_init_error;
3073 /* Allocate the request queue. */
3074 dd->queue = blk_alloc_queue(GFP_KERNEL);
3075 if (dd->queue == NULL) {
3076 dev_err(&dd->pdev->dev,
3077 "Unable to allocate request queue\n");
3079 goto block_queue_alloc_init_error;
3082 /* Attach our request function to the request queue. */
3083 blk_queue_make_request(dd->queue, mtip_make_request);
3085 /* Set device limits. */
3086 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3087 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3088 blk_queue_physical_block_size(dd->queue, 4096);
3089 blk_queue_io_min(dd->queue, 4096);
3091 dd->disk = alloc_disk(MTIP_MAX_MINORS);
3092 if (dd->disk == NULL) {
3093 dev_err(&dd->pdev->dev,
3094 "Unable to allocate gendisk structure\n");
3096 goto alloc_disk_error;
3099 /* Generate the disk name, implemented same as in sd.c */
3101 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3104 spin_lock(&rssd_index_lock);
3105 rv = ida_get_new(&rssd_index_ida, &index);
3106 spin_unlock(&rssd_index_lock);
3107 } while (rv == -EAGAIN);
3112 rv = rssd_disk_name_format("rssd",
3114 dd->disk->disk_name,
3117 goto disk_index_error;
3119 dd->disk->driverfs_dev = &dd->pdev->dev;
3120 dd->disk->major = dd->major;
3121 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3122 dd->disk->fops = &mtip_block_ops;
3123 dd->disk->queue = dd->queue;
3124 dd->disk->private_data = dd;
3125 dd->queue->queuedata = dd;
3128 /* Set the capacity of the device in 512 byte sectors. */
3129 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3130 dev_warn(&dd->pdev->dev,
3131 "Could not read drive capacity\n");
3133 goto read_capacity_error;
3135 set_capacity(dd->disk, capacity);
3137 /* Enable the block device and add it to /dev */
3141 * Now that the disk is active, initialize any sysfs attributes
3142 * managed by the protocol layer.
3144 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3146 mtip_hw_sysfs_init(dd, kobj);
3152 read_capacity_error:
3154 * Delete our gendisk structure. This also removes the device
3157 del_gendisk(dd->disk);
3160 spin_lock(&rssd_index_lock);
3161 ida_remove(&rssd_index_ida, index);
3162 spin_unlock(&rssd_index_lock);
3168 blk_cleanup_queue(dd->queue);
3170 block_queue_alloc_init_error:
3171 /* De-initialize the protocol layer. */
3174 protocol_init_error:
3179 * Block layer deinitialization function.
3181 * Called by the PCI layer as each P320 device is removed.
3183 * @dd Pointer to the driver data structure.
3188 static int mtip_block_remove(struct driver_data *dd)
3190 struct kobject *kobj;
3191 /* Clean up the sysfs attributes managed by the protocol layer. */
3192 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3194 mtip_hw_sysfs_exit(dd, kobj);
3199 * Delete our gendisk structure. This also removes the device
3202 del_gendisk(dd->disk);
3203 blk_cleanup_queue(dd->queue);
3207 /* De-initialize the protocol layer. */
3214 * Function called by the PCI layer when just before the
3215 * machine shuts down.
3217 * If a protocol layer shutdown function is present it will be called
3220 * @dd Pointer to the driver data structure.
3225 static int mtip_block_shutdown(struct driver_data *dd)
3227 dev_info(&dd->pdev->dev,
3228 "Shutting down %s ...\n", dd->disk->disk_name);
3230 /* Delete our gendisk structure, and cleanup the blk queue. */
3231 del_gendisk(dd->disk);
3232 blk_cleanup_queue(dd->queue);
3236 mtip_hw_shutdown(dd);
3240 static int mtip_block_suspend(struct driver_data *dd)
3242 dev_info(&dd->pdev->dev,
3243 "Suspending %s ...\n", dd->disk->disk_name);
3244 mtip_hw_suspend(dd);
3248 static int mtip_block_resume(struct driver_data *dd)
3250 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
3251 dd->disk->disk_name);
3257 * Called for each supported PCI device detected.
3259 * This function allocates the private data structure, enables the
3260 * PCI device and then calls the block layer initialization function.
3263 * 0 on success else an error code.
3265 static int mtip_pci_probe(struct pci_dev *pdev,
3266 const struct pci_device_id *ent)
3269 struct driver_data *dd = NULL;
3271 /* Allocate memory for this devices private data. */
3272 dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
3275 "Unable to allocate memory for driver data\n");
3279 /* Set the atomic variable as 1 in case of SRSI */
3280 atomic_set(&dd->drv_cleanup_done, true);
3282 atomic_set(&dd->resumeflag, false);
3283 atomic_set(&dd->eh_active, 0);
3285 /* Attach the private data to this PCI device. */
3286 pci_set_drvdata(pdev, dd);
3288 rv = pcim_enable_device(pdev);
3290 dev_err(&pdev->dev, "Unable to enable device\n");
3294 /* Map BAR5 to memory. */
3295 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
3297 dev_err(&pdev->dev, "Unable to map regions\n");
3301 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
3302 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3305 rv = pci_set_consistent_dma_mask(pdev,
3308 dev_warn(&pdev->dev,
3309 "64-bit DMA enable failed\n");
3315 pci_set_master(pdev);
3317 if (pci_enable_msi(pdev)) {
3318 dev_warn(&pdev->dev,
3319 "Unable to enable MSI interrupt.\n");
3320 goto block_initialize_err;
3323 /* Copy the info we may need later into the private data structure. */
3324 dd->major = mtip_major;
3325 dd->protocol = ent->driver_data;
3326 dd->instance = instance;
3329 /* Initialize the block layer. */
3330 rv = mtip_block_initialize(dd);
3333 "Unable to initialize block layer\n");
3334 goto block_initialize_err;
3338 * Increment the instance count so that each device has a unique
3345 block_initialize_err:
3346 pci_disable_msi(pdev);
3349 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3353 pci_set_drvdata(pdev, NULL);
3356 /* Set the atomic variable as 0 in case of SRSI */
3357 atomic_set(&dd->drv_cleanup_done, true);
3363 * Called for each probed device when the device is removed or the
3364 * driver is unloaded.
3369 static void mtip_pci_remove(struct pci_dev *pdev)
3371 struct driver_data *dd = pci_get_drvdata(pdev);
3374 if (mtip_check_surprise_removal(pdev)) {
3375 while (atomic_read(&dd->drv_cleanup_done) == false) {
3378 if (counter == 10) {
3379 /* Cleanup the outstanding commands */
3380 mtip_command_cleanup(dd);
3385 /* Set the atomic variable as 1 in case of SRSI */
3386 atomic_set(&dd->drv_cleanup_done, true);
3388 /* Clean up the block layer. */
3389 mtip_block_remove(dd);
3391 pci_disable_msi(pdev);
3394 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3398 * Called for each probed device when the device is suspended.
3404 static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
3407 struct driver_data *dd = pci_get_drvdata(pdev);
3411 "Driver private datastructure is NULL\n");
3415 atomic_set(&dd->resumeflag, true);
3417 /* Disable ports & interrupts then send standby immediate */
3418 rv = mtip_block_suspend(dd);
3421 "Failed to suspend controller\n");
3426 * Save the pci config space to pdev structure &
3427 * disable the device
3429 pci_save_state(pdev);
3430 pci_disable_device(pdev);
3432 /* Move to Low power state*/
3433 pci_set_power_state(pdev, PCI_D3hot);
3439 * Called for each probed device when the device is resumed.
3445 static int mtip_pci_resume(struct pci_dev *pdev)
3448 struct driver_data *dd;
3450 dd = pci_get_drvdata(pdev);
3453 "Driver private datastructure is NULL\n");
3457 /* Move the device to active State */
3458 pci_set_power_state(pdev, PCI_D0);
3460 /* Restore PCI configuration space */
3461 pci_restore_state(pdev);
3463 /* Enable the PCI device*/
3464 rv = pcim_enable_device(pdev);
3467 "Failed to enable card during resume\n");
3470 pci_set_master(pdev);
3473 * Calls hbaReset, initPort, & startPort function
3474 * then enables interrupts
3476 rv = mtip_block_resume(dd);
3478 dev_err(&pdev->dev, "Unable to resume\n");
3481 atomic_set(&dd->resumeflag, false);
3492 static void mtip_pci_shutdown(struct pci_dev *pdev)
3494 struct driver_data *dd = pci_get_drvdata(pdev);
3496 mtip_block_shutdown(dd);
3499 /* Table of device ids supported by this driver. */
3500 static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
3501 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320_DEVICE_ID) },
3505 /* Structure that describes the PCI driver functions. */
3506 static struct pci_driver mtip_pci_driver = {
3507 .name = MTIP_DRV_NAME,
3508 .id_table = mtip_pci_tbl,
3509 .probe = mtip_pci_probe,
3510 .remove = mtip_pci_remove,
3511 .suspend = mtip_pci_suspend,
3512 .resume = mtip_pci_resume,
3513 .shutdown = mtip_pci_shutdown,
3516 MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
3519 * Module initialization function.
3521 * Called once when the module is loaded. This function allocates a major
3522 * block device number to the Cyclone devices and registers the PCI layer
3526 * 0 on success else error code.
3528 static int __init mtip_init(void)
3530 printk(KERN_INFO MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
3532 /* Allocate a major block device number to use with this driver. */
3533 mtip_major = register_blkdev(0, MTIP_DRV_NAME);
3534 if (mtip_major < 0) {
3535 printk(KERN_ERR "Unable to register block device (%d)\n",
3540 /* Register our PCI operations. */
3541 return pci_register_driver(&mtip_pci_driver);
3545 * Module de-initialization function.
3547 * Called once when the module is unloaded. This function deallocates
3548 * the major block device number allocated by mtip_init() and
3549 * unregisters the PCI layer of the driver.
3554 static void __exit mtip_exit(void)
3556 /* Release the allocated major block device number. */
3557 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
3559 /* Unregister the PCI driver. */
3560 pci_unregister_driver(&mtip_pci_driver);
3563 MODULE_AUTHOR("Micron Technology, Inc");
3564 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
3565 MODULE_LICENSE("GPL");
3566 MODULE_VERSION(MTIP_DRV_VERSION);
3568 module_init(mtip_init);
3569 module_exit(mtip_exit);