2 * Driver for the Micron P320 SSD
3 * Copyright (C) 2011 Micron Technology, Inc.
5 * Portions of this code were derived from works subjected to the
7 * Copyright (C) 2009 Integrated Device Technology, Inc.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
21 #include <linux/pci.h>
22 #include <linux/interrupt.h>
23 #include <linux/ata.h>
24 #include <linux/delay.h>
25 #include <linux/hdreg.h>
26 #include <linux/uaccess.h>
27 #include <linux/random.h>
28 #include <linux/smp.h>
29 #include <linux/compat.h>
31 #include <linux/module.h>
32 #include <linux/genhd.h>
33 #include <linux/blkdev.h>
34 #include <linux/bio.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/idr.h>
37 #include <linux/kthread.h>
38 #include <../drivers/ata/ahci.h>
41 #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
42 #define HW_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (MTIP_MAX_SG * 16))
43 #define HW_CMD_TBL_AR_SZ (HW_CMD_TBL_SZ * MTIP_MAX_COMMAND_SLOTS)
44 #define HW_PORT_PRIV_DMA_SZ \
45 (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
47 #define HOST_HSORG 0xFC
48 #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
49 #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
50 #define HSORG_HWREV 0xFF00
51 #define HSORG_STYLE 0x8
52 #define HSORG_SLOTGROUPS 0x7
54 #define PORT_COMMAND_ISSUE 0x38
55 #define PORT_SDBV 0x7C
57 #define PORT_OFFSET 0x100
58 #define PORT_MEM_SIZE 0x80
60 #define PORT_IRQ_ERR \
61 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
62 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
63 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
65 #define PORT_IRQ_LEGACY \
66 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
67 #define PORT_IRQ_HANDLED \
68 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
69 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
70 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
71 #define DEF_PORT_IRQ \
72 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
75 #define MTIP_PRODUCT_UNKNOWN 0x00
76 #define MTIP_PRODUCT_ASICFPGA 0x11
78 /* Device instance number, incremented each time a device is probed. */
82 * Global variable used to hold the major block device number
83 * allocated in mtip_init().
85 static int mtip_major;
87 static DEFINE_SPINLOCK(rssd_index_lock);
88 static DEFINE_IDA(rssd_index_ida);
90 static int mtip_block_initialize(struct driver_data *dd);
93 struct mtip_compat_ide_task_request_s {
96 ide_reg_valid_t out_flags;
97 ide_reg_valid_t in_flags;
100 compat_ulong_t out_size;
101 compat_ulong_t in_size;
106 * This function check_for_surprise_removal is called
107 * while card is removed from the system and it will
108 * read the vendor id from the configration space
110 * @pdev Pointer to the pci_dev structure.
113 * true if device removed, else false
115 static bool mtip_check_surprise_removal(struct pci_dev *pdev)
119 /* Read the vendorID from the configuration space */
120 pci_read_config_word(pdev, 0x00, &vendor_id);
121 if (vendor_id == 0xFFFF)
122 return true; /* device removed */
124 return false; /* device present */
128 * This function is called for clean the pending command in the
129 * command slot during the surprise removal of device and return
130 * error to the upper layer.
132 * @dd Pointer to the DRIVER_DATA structure.
137 static void mtip_command_cleanup(struct driver_data *dd)
139 int group = 0, commandslot = 0, commandindex = 0;
140 struct mtip_cmd *command;
141 struct mtip_port *port = dd->port;
143 for (group = 0; group < 4; group++) {
144 for (commandslot = 0; commandslot < 32; commandslot++) {
145 if (!(port->allocated[group] & (1 << commandslot)))
148 commandindex = group << 5 | commandslot;
149 command = &port->commands[commandindex];
151 if (atomic_read(&command->active)
152 && (command->async_callback)) {
153 command->async_callback(command->async_data,
155 command->async_callback = NULL;
156 command->async_data = NULL;
159 dma_unmap_sg(&port->dd->pdev->dev,
161 command->scatter_ents,
168 atomic_set(&dd->drv_cleanup_done, true);
172 * Obtain an empty command slot.
174 * This function needs to be reentrant since it could be called
175 * at the same time on multiple CPUs. The allocation of the
176 * command slot must be atomic.
178 * @port Pointer to the port data structure.
181 * >= 0 Index of command slot obtained.
182 * -1 No command slots available.
184 static int get_slot(struct mtip_port *port)
187 unsigned int num_command_slots = port->dd->slot_groups * 32;
190 * Try 10 times, because there is a small race here.
191 * that's ok, because it's still cheaper than a lock.
193 * Race: Since this section is not protected by lock, same bit
194 * could be chosen by different process contexts running in
195 * different processor. So instead of costly lock, we are going
198 for (i = 0; i < 10; i++) {
199 slot = find_next_zero_bit(port->allocated,
200 num_command_slots, 1);
201 if ((slot < num_command_slots) &&
202 (!test_and_set_bit(slot, port->allocated)))
205 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
207 if (mtip_check_surprise_removal(port->dd->pdev)) {
208 /* Device not present, clean outstanding commands */
209 mtip_command_cleanup(port->dd);
215 * Release a command slot.
217 * @port Pointer to the port data structure.
218 * @tag Tag of command to release
223 static inline void release_slot(struct mtip_port *port, int tag)
225 smp_mb__before_clear_bit();
226 clear_bit(tag, port->allocated);
227 smp_mb__after_clear_bit();
231 * Reset the HBA (without sleeping)
233 * Just like hba_reset, except does not call sleep, so can be
234 * run from interrupt/tasklet context.
236 * @dd Pointer to the driver data structure.
239 * 0 The reset was successful.
240 * -1 The HBA Reset bit did not clear.
242 static int hba_reset_nosleep(struct driver_data *dd)
244 unsigned long timeout;
246 /* Chip quirk: quiesce any chip function */
249 /* Set the reset bit */
250 writel(HOST_RESET, dd->mmio + HOST_CTL);
253 readl(dd->mmio + HOST_CTL);
256 * Wait 10ms then spin for up to 1 second
257 * waiting for reset acknowledgement
259 timeout = jiffies + msecs_to_jiffies(1000);
261 while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
262 && time_before(jiffies, timeout))
265 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
272 * Issue a command to the hardware.
274 * Set the appropriate bit in the s_active and Command Issue hardware
275 * registers, causing hardware command processing to begin.
277 * @port Pointer to the port structure.
278 * @tag The tag of the command to be issued.
283 static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
285 unsigned long flags = 0;
287 atomic_set(&port->commands[tag].active, 1);
289 spin_lock_irqsave(&port->cmd_issue_lock, flags);
291 writel((1 << MTIP_TAG_BIT(tag)),
292 port->s_active[MTIP_TAG_INDEX(tag)]);
293 writel((1 << MTIP_TAG_BIT(tag)),
294 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
296 spin_unlock_irqrestore(&port->cmd_issue_lock, flags);
300 * Enable/disable the reception of FIS
302 * @port Pointer to the port data structure
303 * @enable 1 to enable, 0 to disable
306 * Previous state: 1 enabled, 0 disabled
308 static int mtip_enable_fis(struct mtip_port *port, int enable)
312 /* enable FIS reception */
313 tmp = readl(port->mmio + PORT_CMD);
315 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
317 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
320 readl(port->mmio + PORT_CMD);
322 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
326 * Enable/disable the DMA engine
328 * @port Pointer to the port data structure
329 * @enable 1 to enable, 0 to disable
332 * Previous state: 1 enabled, 0 disabled.
334 static int mtip_enable_engine(struct mtip_port *port, int enable)
338 /* enable FIS reception */
339 tmp = readl(port->mmio + PORT_CMD);
341 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
343 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
345 readl(port->mmio + PORT_CMD);
346 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
350 * Enables the port DMA engine and FIS reception.
355 static inline void mtip_start_port(struct mtip_port *port)
357 /* Enable FIS reception */
358 mtip_enable_fis(port, 1);
360 /* Enable the DMA engine */
361 mtip_enable_engine(port, 1);
365 * Deinitialize a port by disabling port interrupts, the DMA engine,
368 * @port Pointer to the port structure
373 static inline void mtip_deinit_port(struct mtip_port *port)
375 /* Disable interrupts on this port */
376 writel(0, port->mmio + PORT_IRQ_MASK);
378 /* Disable the DMA engine */
379 mtip_enable_engine(port, 0);
381 /* Disable FIS reception */
382 mtip_enable_fis(port, 0);
388 * This function deinitializes the port by calling mtip_deinit_port() and
389 * then initializes it by setting the command header and RX FIS addresses,
390 * clearing the SError register and any pending port interrupts before
391 * re-enabling the default set of port interrupts.
393 * @port Pointer to the port structure.
398 static void mtip_init_port(struct mtip_port *port)
401 mtip_deinit_port(port);
403 /* Program the command list base and FIS base addresses */
404 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
405 writel((port->command_list_dma >> 16) >> 16,
406 port->mmio + PORT_LST_ADDR_HI);
407 writel((port->rxfis_dma >> 16) >> 16,
408 port->mmio + PORT_FIS_ADDR_HI);
411 writel(port->command_list_dma & 0xFFFFFFFF,
412 port->mmio + PORT_LST_ADDR);
413 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
416 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
418 /* reset the completed registers.*/
419 for (i = 0; i < port->dd->slot_groups; i++)
420 writel(0xFFFFFFFF, port->completed[i]);
422 /* Clear any pending interrupts for this port */
423 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
425 /* Enable port interrupts */
426 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
432 * @port Pointer to the port data structure.
437 static void mtip_restart_port(struct mtip_port *port)
439 unsigned long timeout;
441 /* Disable the DMA engine */
442 mtip_enable_engine(port, 0);
444 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
445 timeout = jiffies + msecs_to_jiffies(500);
446 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
447 && time_before(jiffies, timeout))
451 * Chip quirk: escalate to hba reset if
452 * PxCMD.CR not clear after 500 ms
454 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
455 dev_warn(&port->dd->pdev->dev,
456 "PxCMD.CR not clear, escalating reset\n");
458 if (hba_reset_nosleep(port->dd))
459 dev_err(&port->dd->pdev->dev,
460 "HBA reset escalation failed.\n");
462 /* 30 ms delay before com reset to quiesce chip */
466 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
469 writel(readl(port->mmio + PORT_SCR_CTL) |
470 1, port->mmio + PORT_SCR_CTL);
471 readl(port->mmio + PORT_SCR_CTL);
473 /* Wait 1 ms to quiesce chip function */
474 timeout = jiffies + msecs_to_jiffies(1);
475 while (time_before(jiffies, timeout))
478 /* Clear PxSCTL.DET */
479 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
480 port->mmio + PORT_SCR_CTL);
481 readl(port->mmio + PORT_SCR_CTL);
483 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
484 timeout = jiffies + msecs_to_jiffies(500);
485 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
486 && time_before(jiffies, timeout))
489 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
490 dev_warn(&port->dd->pdev->dev,
491 "COM reset failed\n");
493 /* Clear SError, the PxSERR.DIAG.x should be set so clear it */
494 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
496 /* Enable the DMA engine */
497 mtip_enable_engine(port, 1);
501 * Called periodically to see if any read/write commands are
502 * taking too long to complete.
504 * @data Pointer to the PORT data structure.
509 static void mtip_timeout_function(unsigned long int data)
511 struct mtip_port *port = (struct mtip_port *) data;
512 struct host_to_dev_fis *fis;
513 struct mtip_cmd *command;
514 int tag, cmdto_cnt = 0;
515 unsigned int bit, group;
516 unsigned int num_command_slots = port->dd->slot_groups * 32;
521 if (atomic_read(&port->dd->resumeflag) == true) {
522 mod_timer(&port->cmd_timer,
523 jiffies + msecs_to_jiffies(30000));
527 for (tag = 0; tag < num_command_slots; tag++) {
529 * Skip internal command slot as it has
530 * its own timeout mechanism
532 if (tag == MTIP_TAG_INTERNAL)
535 if (atomic_read(&port->commands[tag].active) &&
536 (time_after(jiffies, port->commands[tag].comp_time))) {
540 command = &port->commands[tag];
541 fis = (struct host_to_dev_fis *) command->command;
543 dev_warn(&port->dd->pdev->dev,
544 "Timeout for command tag %d\n", tag);
548 set_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
551 * Clear the completed bit. This should prevent
552 * any interrupt handlers from trying to retire
555 writel(1 << bit, port->completed[group]);
557 /* Call the async completion callback. */
558 if (likely(command->async_callback))
559 command->async_callback(command->async_data,
561 command->async_callback = NULL;
562 command->comp_func = NULL;
564 /* Unmap the DMA scatter list entries */
565 dma_unmap_sg(&port->dd->pdev->dev,
567 command->scatter_ents,
571 * Clear the allocated bit and active tag for the
574 atomic_set(&port->commands[tag].active, 0);
575 release_slot(port, tag);
582 dev_warn(&port->dd->pdev->dev,
583 "%d commands timed out: restarting port",
585 mtip_restart_port(port);
586 clear_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
587 wake_up_interruptible(&port->svc_wait);
590 /* Restart the timer */
591 mod_timer(&port->cmd_timer,
592 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
596 * IO completion function.
598 * This completion function is called by the driver ISR when a
599 * command that was issued by the kernel completes. It first calls the
600 * asynchronous completion function which normally calls back into the block
601 * layer passing the asynchronous callback data, then unmaps the
602 * scatter list associated with the completed command, and finally
603 * clears the allocated bit associated with the completed command.
605 * @port Pointer to the port data structure.
606 * @tag Tag of the command.
607 * @data Pointer to driver_data.
608 * @status Completion status.
613 static void mtip_async_complete(struct mtip_port *port,
618 struct mtip_cmd *command;
619 struct driver_data *dd = data;
620 int cb_status = status ? -EIO : 0;
622 if (unlikely(!dd) || unlikely(!port))
625 command = &port->commands[tag];
627 if (unlikely(status == PORT_IRQ_TF_ERR)) {
628 dev_warn(&port->dd->pdev->dev,
629 "Command tag %d failed due to TFE\n", tag);
632 /* Upper layer callback */
633 if (likely(command->async_callback))
634 command->async_callback(command->async_data, cb_status);
636 command->async_callback = NULL;
637 command->comp_func = NULL;
639 /* Unmap the DMA scatter list entries */
640 dma_unmap_sg(&dd->pdev->dev,
642 command->scatter_ents,
645 /* Clear the allocated and active bits for the command */
646 atomic_set(&port->commands[tag].active, 0);
647 release_slot(port, tag);
653 * Internal command completion callback function.
655 * This function is normally called by the driver ISR when an internal
656 * command completed. This function signals the command completion by
657 * calling complete().
659 * @port Pointer to the port data structure.
660 * @tag Tag of the command that has completed.
661 * @data Pointer to a completion structure.
662 * @status Completion status.
667 static void mtip_completion(struct mtip_port *port,
672 struct mtip_cmd *command = &port->commands[tag];
673 struct completion *waiting = data;
674 if (unlikely(status == PORT_IRQ_TF_ERR))
675 dev_warn(&port->dd->pdev->dev,
676 "Internal command %d completed with TFE\n", tag);
678 command->async_callback = NULL;
679 command->comp_func = NULL;
685 * Helper function for tag logging
687 static void print_tags(struct driver_data *dd,
689 unsigned long *tagbits)
691 unsigned int tag, count = 0;
693 for (tag = 0; tag < (dd->slot_groups) * 32; tag++) {
694 if (test_bit(tag, tagbits))
698 dev_info(&dd->pdev->dev, "%s [%i tags]\n", msg, count);
704 * @dd Pointer to the DRIVER_DATA structure.
709 static void mtip_handle_tfe(struct driver_data *dd)
711 int group, tag, bit, reissue;
712 struct mtip_port *port;
713 struct mtip_cmd *command;
715 struct host_to_dev_fis *fis;
716 unsigned long tagaccum[SLOTBITS_IN_LONGS];
718 dev_warn(&dd->pdev->dev, "Taskfile error\n");
722 /* Stop the timer to prevent command timeouts. */
723 del_timer(&port->cmd_timer);
726 set_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
728 /* Loop through all the groups */
729 for (group = 0; group < dd->slot_groups; group++) {
730 completed = readl(port->completed[group]);
732 /* clear completed status register in the hardware.*/
733 writel(completed, port->completed[group]);
735 /* clear the tag accumulator */
736 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
738 /* Process successfully completed commands */
739 for (bit = 0; bit < 32 && completed; bit++) {
740 if (!(completed & (1<<bit)))
742 tag = (group << 5) + bit;
744 /* Skip the internal command slot */
745 if (tag == MTIP_TAG_INTERNAL)
748 command = &port->commands[tag];
749 if (likely(command->comp_func)) {
750 set_bit(tag, tagaccum);
751 atomic_set(&port->commands[tag].active, 0);
752 command->comp_func(port,
757 dev_err(&port->dd->pdev->dev,
758 "Missing completion func for tag %d",
760 if (mtip_check_surprise_removal(dd->pdev)) {
761 mtip_command_cleanup(dd);
762 /* don't proceed further */
768 print_tags(dd, "TFE tags completed:", tagaccum);
770 /* Restart the port */
772 mtip_restart_port(port);
774 /* clear the tag accumulator */
775 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
777 /* Loop through all the groups */
778 for (group = 0; group < dd->slot_groups; group++) {
779 for (bit = 0; bit < 32; bit++) {
781 tag = (group << 5) + bit;
783 /* If the active bit is set re-issue the command */
784 if (atomic_read(&port->commands[tag].active) == 0)
787 fis = (struct host_to_dev_fis *)
788 port->commands[tag].command;
790 /* Should re-issue? */
791 if (tag == MTIP_TAG_INTERNAL ||
792 fis->command == ATA_CMD_SET_FEATURES)
796 * First check if this command has
797 * exceeded its retries.
800 (port->commands[tag].retries-- > 0)) {
802 set_bit(tag, tagaccum);
804 /* Update the timeout value. */
805 port->commands[tag].comp_time =
806 jiffies + msecs_to_jiffies(
807 MTIP_NCQ_COMMAND_TIMEOUT_MS);
808 /* Re-issue the command. */
809 mtip_issue_ncq_command(port, tag);
814 /* Retire a command that will not be reissued */
815 dev_warn(&port->dd->pdev->dev,
816 "retiring tag %d\n", tag);
817 atomic_set(&port->commands[tag].active, 0);
819 if (port->commands[tag].comp_func)
820 port->commands[tag].comp_func(
823 port->commands[tag].comp_data,
826 dev_warn(&port->dd->pdev->dev,
827 "Bad completion for tag %d\n",
831 print_tags(dd, "TFE tags reissued:", tagaccum);
833 /* clear eh_active */
834 clear_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
835 wake_up_interruptible(&port->svc_wait);
837 mod_timer(&port->cmd_timer,
838 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
842 * Handle a set device bits interrupt
844 static inline void mtip_process_sdbf(struct driver_data *dd)
846 struct mtip_port *port = dd->port;
849 struct mtip_cmd *command;
851 /* walk all bits in all slot groups */
852 for (group = 0; group < dd->slot_groups; group++) {
853 completed = readl(port->completed[group]);
855 /* clear completed status register in the hardware.*/
856 writel(completed, port->completed[group]);
858 /* Process completed commands. */
860 (bit < 32) && completed;
861 bit++, completed >>= 1) {
862 if (completed & 0x01) {
863 tag = (group << 5) | bit;
865 /* skip internal command slot. */
866 if (unlikely(tag == MTIP_TAG_INTERNAL))
869 command = &port->commands[tag];
870 /* make internal callback */
871 if (likely(command->comp_func)) {
878 dev_warn(&dd->pdev->dev,
883 if (mtip_check_surprise_removal(
885 mtip_command_cleanup(dd);
895 * Process legacy pio and d2h interrupts
897 static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
899 struct mtip_port *port = dd->port;
900 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
902 if (test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) &&
903 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
904 & (1 << MTIP_TAG_INTERNAL))) {
905 if (cmd->comp_func) {
914 dev_warn(&dd->pdev->dev, "IRQ status 0x%x ignored.\n", port_stat);
920 * Demux and handle errors
922 static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
924 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
927 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
928 dev_warn(&dd->pdev->dev,
929 "Clearing PxSERR.DIAG.x\n");
930 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
933 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
934 dev_warn(&dd->pdev->dev,
935 "Clearing PxSERR.DIAG.n\n");
936 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
939 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
940 dev_warn(&dd->pdev->dev,
941 "Port stat errors %x unhandled\n",
942 (port_stat & ~PORT_IRQ_HANDLED));
946 static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
948 struct driver_data *dd = (struct driver_data *) data;
949 struct mtip_port *port = dd->port;
950 u32 hba_stat, port_stat;
953 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
957 /* Acknowledge the interrupt status on the port.*/
958 port_stat = readl(port->mmio + PORT_IRQ_STAT);
959 writel(port_stat, port->mmio + PORT_IRQ_STAT);
961 /* Demux port status */
962 if (likely(port_stat & PORT_IRQ_SDB_FIS))
963 mtip_process_sdbf(dd);
965 if (unlikely(port_stat & PORT_IRQ_ERR)) {
966 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
967 mtip_command_cleanup(dd);
968 /* don't proceed further */
972 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
975 if (unlikely(port_stat & PORT_IRQ_LEGACY))
976 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
979 /* acknowledge interrupt */
980 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
986 * Wrapper for mtip_handle_irq
987 * (ignores return code)
989 static void mtip_tasklet(unsigned long data)
991 mtip_handle_irq((struct driver_data *) data);
995 * HBA interrupt subroutine.
998 * @instance Pointer to the driver data structure.
1001 * IRQ_HANDLED A HBA interrupt was pending and handled.
1002 * IRQ_NONE This interrupt was not for the HBA.
1004 static irqreturn_t mtip_irq_handler(int irq, void *instance)
1006 struct driver_data *dd = instance;
1007 tasklet_schedule(&dd->tasklet);
1011 static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1013 atomic_set(&port->commands[tag].active, 1);
1014 writel(1 << MTIP_TAG_BIT(tag),
1015 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1019 * Wait for port to quiesce
1021 * @port Pointer to port data structure
1022 * @timeout Max duration to wait (ms)
1026 * -EBUSY Commands still active
1028 static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1032 unsigned int active = 1;
1034 to = jiffies + msecs_to_jiffies(timeout);
1036 if (test_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags) &&
1037 test_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags)) {
1039 continue; /* svc thd is actively issuing commands */
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 * @fis_len Length in WORDS of the FIS.
1064 * @buffer DMA accessible for command data.
1065 * @buf_len 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 set_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
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 clear_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
1112 wake_up_interruptible(&port->svc_wait);
1116 /* Set the completion function and data for the command. */
1117 int_cmd->comp_data = &wait;
1118 int_cmd->comp_func = mtip_completion;
1121 /* Clear completion - we're going to poll */
1122 int_cmd->comp_data = NULL;
1123 int_cmd->comp_func = NULL;
1126 /* Copy the command to the command table */
1127 memcpy(int_cmd->command, fis, fis_len*4);
1129 /* Populate the SG list */
1130 int_cmd->command_header->opts =
1131 __force_bit2int cpu_to_le32(opts | fis_len);
1133 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1136 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1138 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1139 command_sg->dba_upper =
1140 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
1142 int_cmd->command_header->opts |=
1143 __force_bit2int cpu_to_le32((1 << 16));
1146 /* Populate the command header */
1147 int_cmd->command_header->byte_count = 0;
1149 /* Issue the command to the hardware */
1150 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1152 /* Poll if atomic, wait_for_completion otherwise */
1153 if (atomic == GFP_KERNEL) {
1154 /* Wait for the command to complete or timeout. */
1155 if (wait_for_completion_timeout(
1157 msecs_to_jiffies(timeout)) == 0) {
1158 dev_err(&port->dd->pdev->dev,
1159 "Internal command did not complete [%d] "
1160 "within timeout of %lu ms\n",
1165 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1166 & (1 << MTIP_TAG_INTERNAL)) {
1167 dev_warn(&port->dd->pdev->dev,
1168 "Retiring internal command but CI is 1.\n");
1172 /* Spin for <timeout> checking if command still outstanding */
1173 timeout = jiffies + msecs_to_jiffies(timeout);
1176 port->cmd_issue[MTIP_TAG_INTERNAL])
1177 & (1 << MTIP_TAG_INTERNAL))
1178 && time_before(jiffies, timeout))
1181 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1182 & (1 << MTIP_TAG_INTERNAL)) {
1183 dev_err(&port->dd->pdev->dev,
1184 "Internal command did not complete [%d]\n",
1190 /* Clear the allocated and active bits for the internal command. */
1191 atomic_set(&int_cmd->active, 0);
1192 release_slot(port, MTIP_TAG_INTERNAL);
1193 clear_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
1194 wake_up_interruptible(&port->svc_wait);
1200 * Byte-swap ATA ID strings.
1202 * ATA identify data contains strings in byte-swapped 16-bit words.
1203 * They must be swapped (on all architectures) to be usable as C strings.
1204 * This function swaps bytes in-place.
1206 * @buf The buffer location of the string
1207 * @len The number of bytes to swap
1212 static inline void ata_swap_string(u16 *buf, unsigned int len)
1215 for (i = 0; i < (len/2); i++)
1216 be16_to_cpus(&buf[i]);
1220 * Request the device identity information.
1222 * If a user space buffer is not specified, i.e. is NULL, the
1223 * identify information is still read from the drive and placed
1224 * into the identify data buffer (@e port->identify) in the
1225 * port data structure.
1226 * When the identify buffer contains valid identify information @e
1227 * port->identify_valid is non-zero.
1229 * @port Pointer to the port structure.
1230 * @user_buffer A user space buffer where the identify data should be
1234 * 0 Command completed successfully.
1235 * -EFAULT An error occurred while coping data to the user buffer.
1236 * -1 Command failed.
1238 static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1241 struct host_to_dev_fis fis;
1243 /* Build the FIS. */
1244 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1247 fis.command = ATA_CMD_ID_ATA;
1249 /* Set the identify information as invalid. */
1250 port->identify_valid = 0;
1252 /* Clear the identify information. */
1253 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1255 /* Execute the command. */
1256 if (mtip_exec_internal_command(port,
1260 sizeof(u16) * ATA_ID_WORDS,
1263 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1270 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1271 * perform field-sensitive swapping on the string fields.
1272 * See the kernel use of ata_id_string() for proof of this.
1274 #ifdef __LITTLE_ENDIAN
1275 ata_swap_string(port->identify + 27, 40); /* model string*/
1276 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1277 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1281 for (i = 0; i < ATA_ID_WORDS; i++)
1282 port->identify[i] = le16_to_cpu(port->identify[i]);
1286 /* Set the identify buffer as valid. */
1287 port->identify_valid = 1;
1293 ATA_ID_WORDS * sizeof(u16))) {
1304 * Issue a standby immediate command to the device.
1306 * @port Pointer to the port structure.
1309 * 0 Command was executed successfully.
1310 * -1 An error occurred while executing the command.
1312 static int mtip_standby_immediate(struct mtip_port *port)
1315 struct host_to_dev_fis fis;
1317 /* Build the FIS. */
1318 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1321 fis.command = ATA_CMD_STANDBYNOW1;
1323 /* Execute the command. Use a 15-second timeout for large drives. */
1324 rv = mtip_exec_internal_command(port,
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 = __force_bit2int
1474 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1475 command_sg->dba = __force_bit2int
1476 cpu_to_le32(sg_dma_address(sg));
1477 command_sg->dba_upper = __force_bit2int
1478 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
1485 * @brief Execute a drive command.
1487 * return value 0 The command completed successfully.
1488 * return value -1 An error occurred while executing the command.
1490 static int exec_drive_task(struct mtip_port *port, u8 *command)
1492 struct host_to_dev_fis fis;
1493 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1495 /* Build the FIS. */
1496 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1499 fis.command = command[0];
1500 fis.features = command[1];
1501 fis.sect_count = command[2];
1502 fis.sector = command[3];
1503 fis.cyl_low = command[4];
1504 fis.cyl_hi = command[5];
1505 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1508 dbg_printk(MTIP_DRV_NAME "%s: User Command: cmd %x, feat %x, "
1509 "nsect %x, sect %x, lcyl %x, "
1510 "hcyl %x, sel %x\n",
1520 /* Execute the command. */
1521 if (mtip_exec_internal_command(port,
1528 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
1532 command[0] = reply->command; /* Status*/
1533 command[1] = reply->features; /* Error*/
1534 command[4] = reply->cyl_low;
1535 command[5] = reply->cyl_hi;
1537 dbg_printk(MTIP_DRV_NAME "%s: Completion Status: stat %x, "
1538 "err %x , cyl_lo %x cyl_hi %x\n",
1549 * @brief Execute a drive command.
1551 * @param port Pointer to the port data structure.
1552 * @param command Pointer to the user specified command parameters.
1553 * @param user_buffer Pointer to the user space buffer where read sector
1554 * data should be copied.
1556 * return value 0 The command completed successfully.
1557 * return value -EFAULT An error occurred while copying the completion
1558 * data to the user space buffer.
1559 * return value -1 An error occurred while executing the command.
1561 static int exec_drive_command(struct mtip_port *port, u8 *command,
1562 void __user *user_buffer)
1564 struct host_to_dev_fis fis;
1565 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1567 /* Build the FIS. */
1568 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1571 fis.command = command[0];
1572 fis.features = command[2];
1573 fis.sect_count = command[3];
1574 if (fis.command == ATA_CMD_SMART) {
1575 fis.sector = command[1];
1580 dbg_printk(MTIP_DRV_NAME
1581 "%s: User Command: cmd %x, sect %x, "
1582 "feat %x, sectcnt %x\n",
1589 memset(port->sector_buffer, 0x00, ATA_SECT_SIZE);
1591 /* Execute the command. */
1592 if (mtip_exec_internal_command(port,
1595 port->sector_buffer_dma,
1596 (command[3] != 0) ? ATA_SECT_SIZE : 0,
1599 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
1604 /* Collect the completion status. */
1605 command[0] = reply->command; /* Status*/
1606 command[1] = reply->features; /* Error*/
1607 command[2] = command[3];
1609 dbg_printk(MTIP_DRV_NAME
1610 "%s: Completion Status: stat %x, "
1617 if (user_buffer && command[3]) {
1618 if (copy_to_user(user_buffer,
1619 port->sector_buffer,
1620 ATA_SECT_SIZE * command[3])) {
1629 * Indicates whether a command has a single sector payload.
1631 * @command passed to the device to perform the certain event.
1632 * @features passed to the device to perform the certain event.
1635 * 1 command is one that always has a single sector payload,
1636 * regardless of the value in the Sector Count field.
1640 static unsigned int implicit_sector(unsigned char command,
1641 unsigned char features)
1643 unsigned int rv = 0;
1645 /* list of commands that have an implicit sector count of 1 */
1647 case ATA_CMD_SEC_SET_PASS:
1648 case ATA_CMD_SEC_UNLOCK:
1649 case ATA_CMD_SEC_ERASE_PREP:
1650 case ATA_CMD_SEC_ERASE_UNIT:
1651 case ATA_CMD_SEC_FREEZE_LOCK:
1652 case ATA_CMD_SEC_DISABLE_PASS:
1653 case ATA_CMD_PMP_READ:
1654 case ATA_CMD_PMP_WRITE:
1657 case ATA_CMD_SET_MAX:
1658 if (features == ATA_SET_MAX_UNLOCK)
1662 if ((features == ATA_SMART_READ_VALUES) ||
1663 (features == ATA_SMART_READ_THRESHOLDS))
1666 case ATA_CMD_CONF_OVERLAY:
1667 if ((features == ATA_DCO_IDENTIFY) ||
1668 (features == ATA_DCO_SET))
1676 * Executes a taskfile
1677 * See ide_taskfile_ioctl() for derivation
1679 static int exec_drive_taskfile(struct driver_data *dd,
1681 ide_task_request_t *req_task,
1684 struct host_to_dev_fis fis;
1685 struct host_to_dev_fis *reply;
1688 dma_addr_t outbuf_dma = 0;
1689 dma_addr_t inbuf_dma = 0;
1690 dma_addr_t dma_buffer = 0;
1692 unsigned int taskin = 0;
1693 unsigned int taskout = 0;
1695 unsigned int timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1696 unsigned int force_single_sector;
1697 unsigned int transfer_size;
1698 unsigned long task_file_data;
1699 int intotal = outtotal + req_task->out_size;
1701 taskout = req_task->out_size;
1702 taskin = req_task->in_size;
1703 /* 130560 = 512 * 0xFF*/
1704 if (taskin > 130560 || taskout > 130560) {
1710 outbuf = kzalloc(taskout, GFP_KERNEL);
1711 if (outbuf == NULL) {
1715 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
1719 outbuf_dma = pci_map_single(dd->pdev,
1723 if (outbuf_dma == 0) {
1727 dma_buffer = outbuf_dma;
1731 inbuf = kzalloc(taskin, GFP_KERNEL);
1732 if (inbuf == NULL) {
1737 if (copy_from_user(inbuf, buf + intotal, taskin)) {
1741 inbuf_dma = pci_map_single(dd->pdev,
1743 taskin, DMA_FROM_DEVICE);
1744 if (inbuf_dma == 0) {
1748 dma_buffer = inbuf_dma;
1751 /* only supports PIO and non-data commands from this ioctl. */
1752 switch (req_task->data_phase) {
1754 nsect = taskout / ATA_SECT_SIZE;
1755 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1758 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1760 case TASKFILE_NO_DATA:
1761 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
1768 /* Build the FIS. */
1769 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1773 fis.command = req_task->io_ports[7];
1774 fis.features = req_task->io_ports[1];
1775 fis.sect_count = req_task->io_ports[2];
1776 fis.lba_low = req_task->io_ports[3];
1777 fis.lba_mid = req_task->io_ports[4];
1778 fis.lba_hi = req_task->io_ports[5];
1779 /* Clear the dev bit*/
1780 fis.device = req_task->io_ports[6] & ~0x10;
1782 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
1783 req_task->in_flags.all =
1784 IDE_TASKFILE_STD_IN_FLAGS |
1785 (IDE_HOB_STD_IN_FLAGS << 8);
1786 fis.lba_low_ex = req_task->hob_ports[3];
1787 fis.lba_mid_ex = req_task->hob_ports[4];
1788 fis.lba_hi_ex = req_task->hob_ports[5];
1789 fis.features_ex = req_task->hob_ports[1];
1790 fis.sect_cnt_ex = req_task->hob_ports[2];
1793 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
1796 force_single_sector = implicit_sector(fis.command, fis.features);
1798 if ((taskin || taskout) && (!fis.sect_count)) {
1800 fis.sect_count = nsect;
1802 if (!force_single_sector) {
1803 dev_warn(&dd->pdev->dev,
1804 "data movement but "
1805 "sect_count is 0\n");
1812 dbg_printk(MTIP_DRV_NAME
1813 "taskfile: cmd %x, feat %x, nsect %x,"
1814 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
1824 switch (fis.command) {
1825 case ATA_CMD_DOWNLOAD_MICRO:
1826 /* Change timeout for Download Microcode to 60 seconds.*/
1829 case ATA_CMD_SEC_ERASE_UNIT:
1830 /* Change timeout for Security Erase Unit to 4 minutes.*/
1833 case ATA_CMD_STANDBYNOW1:
1834 /* Change timeout for standby immediate to 10 seconds.*/
1839 /* Change timeout for vendor unique command to 10 secs */
1843 /* Change timeout for vendor unique command to 10 secs */
1847 timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1851 /* Determine the correct transfer size.*/
1852 if (force_single_sector)
1853 transfer_size = ATA_SECT_SIZE;
1855 transfer_size = ATA_SECT_SIZE * fis.sect_count;
1857 /* Execute the command.*/
1858 if (mtip_exec_internal_command(dd->port,
1870 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
1872 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
1873 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
1874 req_task->io_ports[7] = reply->control;
1876 reply = dd->port->rxfis + RX_FIS_D2H_REG;
1877 req_task->io_ports[7] = reply->command;
1880 /* reclaim the DMA buffers.*/
1882 pci_unmap_single(dd->pdev, inbuf_dma,
1883 taskin, DMA_FROM_DEVICE);
1885 pci_unmap_single(dd->pdev, outbuf_dma,
1886 taskout, DMA_TO_DEVICE);
1890 /* return the ATA registers to the caller.*/
1891 req_task->io_ports[1] = reply->features;
1892 req_task->io_ports[2] = reply->sect_count;
1893 req_task->io_ports[3] = reply->lba_low;
1894 req_task->io_ports[4] = reply->lba_mid;
1895 req_task->io_ports[5] = reply->lba_hi;
1896 req_task->io_ports[6] = reply->device;
1898 if (req_task->out_flags.all & 1) {
1900 req_task->hob_ports[3] = reply->lba_low_ex;
1901 req_task->hob_ports[4] = reply->lba_mid_ex;
1902 req_task->hob_ports[5] = reply->lba_hi_ex;
1903 req_task->hob_ports[1] = reply->features_ex;
1904 req_task->hob_ports[2] = reply->sect_cnt_ex;
1907 /* Com rest after secure erase or lowlevel format */
1908 if (((fis.command == ATA_CMD_SEC_ERASE_UNIT) ||
1909 ((fis.command == 0xFC) &&
1910 (fis.features == 0x27 || fis.features == 0x72 ||
1911 fis.features == 0x62 || fis.features == 0x26))) &&
1912 !(reply->command & 1)) {
1913 mtip_restart_port(dd->port);
1916 dbg_printk(MTIP_DRV_NAME
1917 "%s: Completion: stat %x,"
1918 "err %x, sect_cnt %x, lbalo %x,"
1919 "lbamid %x, lbahi %x, dev %x\n",
1921 req_task->io_ports[7],
1922 req_task->io_ports[1],
1923 req_task->io_ports[2],
1924 req_task->io_ports[3],
1925 req_task->io_ports[4],
1926 req_task->io_ports[5],
1927 req_task->io_ports[6]);
1930 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
1936 if (copy_to_user(buf + intotal, inbuf, taskin)) {
1943 pci_unmap_single(dd->pdev, inbuf_dma,
1944 taskin, DMA_FROM_DEVICE);
1946 pci_unmap_single(dd->pdev, outbuf_dma,
1947 taskout, DMA_TO_DEVICE);
1955 * Handle IOCTL calls from the Block Layer.
1957 * This function is called by the Block Layer when it receives an IOCTL
1958 * command that it does not understand. If the IOCTL command is not supported
1959 * this function returns -ENOTTY.
1961 * @dd Pointer to the driver data structure.
1962 * @cmd IOCTL command passed from the Block Layer.
1963 * @arg IOCTL argument passed from the Block Layer.
1966 * 0 The IOCTL completed successfully.
1967 * -ENOTTY The specified command is not supported.
1968 * -EFAULT An error occurred copying data to a user space buffer.
1969 * -EIO An error occurred while executing the command.
1971 static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
1975 case HDIO_GET_IDENTITY:
1976 if (mtip_get_identify(dd->port, (void __user *) arg) < 0) {
1977 dev_warn(&dd->pdev->dev,
1978 "Unable to read identity\n");
1983 case HDIO_DRIVE_CMD:
1985 u8 drive_command[4];
1987 /* Copy the user command info to our buffer. */
1988 if (copy_from_user(drive_command,
1989 (void __user *) arg,
1990 sizeof(drive_command)))
1993 /* Execute the drive command. */
1994 if (exec_drive_command(dd->port,
1996 (void __user *) (arg+4)))
1999 /* Copy the status back to the users buffer. */
2000 if (copy_to_user((void __user *) arg,
2002 sizeof(drive_command)))
2007 case HDIO_DRIVE_TASK:
2009 u8 drive_command[7];
2011 /* Copy the user command info to our buffer. */
2012 if (copy_from_user(drive_command,
2013 (void __user *) arg,
2014 sizeof(drive_command)))
2017 /* Execute the drive command. */
2018 if (exec_drive_task(dd->port, drive_command))
2021 /* Copy the status back to the users buffer. */
2022 if (copy_to_user((void __user *) arg,
2024 sizeof(drive_command)))
2029 case HDIO_DRIVE_TASKFILE: {
2030 ide_task_request_t req_task;
2033 if (copy_from_user(&req_task, (void __user *) arg,
2037 outtotal = sizeof(req_task);
2039 ret = exec_drive_taskfile(dd, (void __user *) arg,
2040 &req_task, outtotal);
2042 if (copy_to_user((void __user *) arg, &req_task,
2056 * Submit an IO to the hw
2058 * This function is called by the block layer to issue an io
2059 * to the device. Upon completion, the callback function will
2060 * be called with the data parameter passed as the callback data.
2062 * @dd Pointer to the driver data structure.
2063 * @start First sector to read.
2064 * @nsect Number of sectors to read.
2065 * @nents Number of entries in scatter list for the read command.
2066 * @tag The tag of this read command.
2067 * @callback Pointer to the function that should be called
2068 * when the read completes.
2069 * @data Callback data passed to the callback function
2070 * when the read completes.
2071 * @barrier If non-zero, this command must be completed before
2072 * issuing any other commands.
2073 * @dir Direction (read or write)
2078 static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
2079 int nsect, int nents, int tag, void *callback,
2080 void *data, int barrier, int dir)
2082 struct host_to_dev_fis *fis;
2083 struct mtip_port *port = dd->port;
2084 struct mtip_cmd *command = &port->commands[tag];
2086 /* Map the scatter list for DMA access */
2088 nents = dma_map_sg(&dd->pdev->dev, command->sg,
2089 nents, DMA_FROM_DEVICE);
2091 nents = dma_map_sg(&dd->pdev->dev, command->sg,
2092 nents, DMA_TO_DEVICE);
2094 command->scatter_ents = nents;
2097 * The number of retries for this command before it is
2098 * reported as a failure to the upper layers.
2100 command->retries = MTIP_MAX_RETRIES;
2103 fis = command->command;
2107 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
2108 *((unsigned int *) &fis->lba_low) = (start & 0xFFFFFF);
2109 *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xFFFFFF);
2110 fis->device = 1 << 6;
2112 fis->device |= FUA_BIT;
2113 fis->features = nsect & 0xFF;
2114 fis->features_ex = (nsect >> 8) & 0xFF;
2115 fis->sect_count = ((tag << 3) | (tag >> 5));
2116 fis->sect_cnt_ex = 0;
2120 fill_command_sg(dd, command, nents);
2122 /* Populate the command header */
2123 command->command_header->opts =
2124 __force_bit2int cpu_to_le32(
2125 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
2126 command->command_header->byte_count = 0;
2129 * Set the completion function and data for the command
2130 * within this layer.
2132 command->comp_data = dd;
2133 command->comp_func = mtip_async_complete;
2134 command->direction = (dir == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
2137 * Set the completion function and data for the command passed
2138 * from the upper layer.
2140 command->async_data = data;
2141 command->async_callback = callback;
2144 * To prevent this command from being issued
2145 * if an internal command is in progress or error handling is active.
2147 if (unlikely(test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) ||
2148 test_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags))) {
2149 set_bit(tag, port->cmds_to_issue);
2150 set_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags);
2154 /* Issue the command to the hardware */
2155 mtip_issue_ncq_command(port, tag);
2157 /* Set the command's timeout value.*/
2158 port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
2159 MTIP_NCQ_COMMAND_TIMEOUT_MS);
2163 * Release a command slot.
2165 * @dd Pointer to the driver data structure.
2171 static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
2173 release_slot(dd->port, tag);
2177 * Obtain a command slot and return its associated scatter list.
2179 * @dd Pointer to the driver data structure.
2180 * @tag Pointer to an int that will receive the allocated command
2184 * Pointer to the scatter list for the allocated command slot
2185 * or NULL if no command slots are available.
2187 static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2191 * It is possible that, even with this semaphore, a thread
2192 * may think that no command slots are available. Therefore, we
2193 * need to make an attempt to get_slot().
2195 down(&dd->port->cmd_slot);
2196 *tag = get_slot(dd->port);
2198 if (unlikely(*tag < 0))
2201 return dd->port->commands[*tag].sg;
2205 * Sysfs register/status dump.
2207 * @dev Pointer to the device structure, passed by the kernrel.
2208 * @attr Pointer to the device_attribute structure passed by the kernel.
2209 * @buf Pointer to the char buffer that will receive the stats info.
2212 * The size, in bytes, of the data copied into buf.
2214 static ssize_t hw_show_registers(struct device *dev,
2215 struct device_attribute *attr,
2218 u32 group_allocated;
2219 struct driver_data *dd = dev_to_disk(dev)->private_data;
2223 size += sprintf(&buf[size], "%s:\ns_active:\n", __func__);
2225 for (n = 0; n < dd->slot_groups; n++)
2226 size += sprintf(&buf[size], "0x%08x\n",
2227 readl(dd->port->s_active[n]));
2229 size += sprintf(&buf[size], "Command Issue:\n");
2231 for (n = 0; n < dd->slot_groups; n++)
2232 size += sprintf(&buf[size], "0x%08x\n",
2233 readl(dd->port->cmd_issue[n]));
2235 size += sprintf(&buf[size], "Allocated:\n");
2237 for (n = 0; n < dd->slot_groups; n++) {
2238 if (sizeof(long) > sizeof(u32))
2240 dd->port->allocated[n/2] >> (32*(n&1));
2242 group_allocated = dd->port->allocated[n];
2243 size += sprintf(&buf[size], "0x%08x\n",
2247 size += sprintf(&buf[size], "completed:\n");
2249 for (n = 0; n < dd->slot_groups; n++)
2250 size += sprintf(&buf[size], "0x%08x\n",
2251 readl(dd->port->completed[n]));
2253 size += sprintf(&buf[size], "PORT_IRQ_STAT 0x%08x\n",
2254 readl(dd->port->mmio + PORT_IRQ_STAT));
2255 size += sprintf(&buf[size], "HOST_IRQ_STAT 0x%08x\n",
2256 readl(dd->mmio + HOST_IRQ_STAT));
2260 static DEVICE_ATTR(registers, S_IRUGO, hw_show_registers, NULL);
2263 * Create the sysfs related attributes.
2265 * @dd Pointer to the driver data structure.
2266 * @kobj Pointer to the kobj for the block device.
2269 * 0 Operation completed successfully.
2270 * -EINVAL Invalid parameter.
2272 static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
2277 if (sysfs_create_file(kobj, &dev_attr_registers.attr))
2278 dev_warn(&dd->pdev->dev,
2279 "Error creating registers sysfs entry\n");
2284 * Remove the sysfs related attributes.
2286 * @dd Pointer to the driver data structure.
2287 * @kobj Pointer to the kobj for the block device.
2290 * 0 Operation completed successfully.
2291 * -EINVAL Invalid parameter.
2293 static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
2298 sysfs_remove_file(kobj, &dev_attr_registers.attr);
2304 * Perform any init/resume time hardware setup
2306 * @dd Pointer to the driver data structure.
2311 static inline void hba_setup(struct driver_data *dd)
2314 hwdata = readl(dd->mmio + HOST_HSORG);
2316 /* interrupt bug workaround: use only 1 IS bit.*/
2318 HSORG_DISABLE_SLOTGRP_INTR |
2319 HSORG_DISABLE_SLOTGRP_PXIS,
2320 dd->mmio + HOST_HSORG);
2324 * Detect the details of the product, and store anything needed
2325 * into the driver data structure. This includes product type and
2326 * version and number of slot groups.
2328 * @dd Pointer to the driver data structure.
2333 static void mtip_detect_product(struct driver_data *dd)
2336 unsigned int rev, slotgroups;
2339 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2341 * [15:8] hardware/software interface rev#
2342 * [ 3] asic-style interface
2343 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2345 hwdata = readl(dd->mmio + HOST_HSORG);
2347 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2348 dd->slot_groups = 1;
2351 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2352 rev = (hwdata & HSORG_HWREV) >> 8;
2353 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2354 dev_info(&dd->pdev->dev,
2355 "ASIC-FPGA design, HS rev 0x%x, "
2356 "%i slot groups [%i slots]\n",
2361 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2362 dev_warn(&dd->pdev->dev,
2363 "Warning: driver only supports "
2364 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2365 slotgroups = MTIP_MAX_SLOT_GROUPS;
2367 dd->slot_groups = slotgroups;
2371 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2375 * Blocking wait for FTL rebuild to complete
2377 * @dd Pointer to the DRIVER_DATA structure.
2380 * 0 FTL rebuild completed successfully
2381 * -EFAULT FTL rebuild error/timeout/interruption
2383 static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2385 unsigned long timeout, cnt = 0, start;
2387 dev_warn(&dd->pdev->dev,
2388 "FTL rebuild in progress. Polling for completion.\n");
2391 dd->ftlrebuildflag = 1;
2392 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2395 if (mtip_check_surprise_removal(dd->pdev))
2398 if (mtip_get_identify(dd->port, NULL) < 0)
2401 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2402 MTIP_FTL_REBUILD_MAGIC) {
2404 /* Print message every 3 minutes */
2406 dev_warn(&dd->pdev->dev,
2407 "FTL rebuild in progress (%d secs).\n",
2408 jiffies_to_msecs(jiffies - start) / 1000);
2412 dev_warn(&dd->pdev->dev,
2413 "FTL rebuild complete (%d secs).\n",
2414 jiffies_to_msecs(jiffies - start) / 1000);
2415 dd->ftlrebuildflag = 0;
2416 mtip_block_initialize(dd);
2420 } while (time_before(jiffies, timeout));
2422 /* Check for timeout */
2423 if (dd->ftlrebuildflag) {
2424 dev_err(&dd->pdev->dev,
2425 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2426 jiffies_to_msecs(jiffies - start) / 1000);
2434 * service thread to issue queued commands
2436 * @data Pointer to the driver data structure.
2442 static int mtip_service_thread(void *data)
2444 struct driver_data *dd = (struct driver_data *)data;
2445 unsigned long slot, slot_start, slot_wrap;
2446 unsigned int num_cmd_slots = dd->slot_groups * 32;
2447 struct mtip_port *port = dd->port;
2451 * the condition is to check neither an internal command is
2452 * is in progress nor error handling is active
2454 wait_event_interruptible(port->svc_wait, (port->flags) &&
2455 !test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) &&
2456 !test_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags));
2458 if (kthread_should_stop())
2461 set_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags);
2462 if (test_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags)) {
2464 /* used to restrict the loop to one iteration */
2465 slot_start = num_cmd_slots;
2468 slot = find_next_bit(port->cmds_to_issue,
2469 num_cmd_slots, slot);
2470 if (slot_wrap == 1) {
2471 if ((slot_start >= slot) ||
2472 (slot >= num_cmd_slots))
2475 if (unlikely(slot_start == num_cmd_slots))
2478 if (unlikely(slot == num_cmd_slots)) {
2484 /* Issue the command to the hardware */
2485 mtip_issue_ncq_command(port, slot);
2487 /* Set the command's timeout value.*/
2488 port->commands[slot].comp_time = jiffies +
2489 msecs_to_jiffies(MTIP_NCQ_COMMAND_TIMEOUT_MS);
2491 clear_bit(slot, port->cmds_to_issue);
2494 clear_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags);
2495 } else if (test_bit(MTIP_FLAG_REBUILD_BIT, &port->flags)) {
2496 mtip_ftl_rebuild_poll(dd);
2497 clear_bit(MTIP_FLAG_REBUILD_BIT, &port->flags);
2499 clear_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags);
2501 if (test_bit(MTIP_FLAG_SVC_THD_SHOULD_STOP_BIT, &port->flags))
2508 * Called once for each card.
2510 * @dd Pointer to the driver data structure.
2513 * 0 on success, else an error code.
2515 static int mtip_hw_init(struct driver_data *dd)
2519 unsigned int num_command_slots;
2521 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
2523 mtip_detect_product(dd);
2524 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
2528 num_command_slots = dd->slot_groups * 32;
2532 tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
2534 dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
2536 dev_err(&dd->pdev->dev,
2537 "Memory allocation: port structure\n");
2541 /* Counting semaphore to track command slot usage */
2542 sema_init(&dd->port->cmd_slot, num_command_slots - 1);
2544 /* Spinlock to prevent concurrent issue */
2545 spin_lock_init(&dd->port->cmd_issue_lock);
2547 /* Set the port mmio base address. */
2548 dd->port->mmio = dd->mmio + PORT_OFFSET;
2551 /* Allocate memory for the command list. */
2552 dd->port->command_list =
2553 dmam_alloc_coherent(&dd->pdev->dev,
2554 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2555 &dd->port->command_list_dma,
2557 if (!dd->port->command_list) {
2558 dev_err(&dd->pdev->dev,
2559 "Memory allocation: command list\n");
2564 /* Clear the memory we have allocated. */
2565 memset(dd->port->command_list,
2567 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2));
2569 /* Setup the addresse of the RX FIS. */
2570 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
2571 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
2573 /* Setup the address of the command tables. */
2574 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
2575 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
2577 /* Setup the address of the identify data. */
2578 dd->port->identify = dd->port->command_table +
2580 dd->port->identify_dma = dd->port->command_tbl_dma +
2583 /* Setup the address of the sector buffer. */
2584 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
2585 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
2587 /* Point the command headers at the command tables. */
2588 for (i = 0; i < num_command_slots; i++) {
2589 dd->port->commands[i].command_header =
2590 dd->port->command_list +
2591 (sizeof(struct mtip_cmd_hdr) * i);
2592 dd->port->commands[i].command_header_dma =
2593 dd->port->command_list_dma +
2594 (sizeof(struct mtip_cmd_hdr) * i);
2596 dd->port->commands[i].command =
2597 dd->port->command_table + (HW_CMD_TBL_SZ * i);
2598 dd->port->commands[i].command_dma =
2599 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
2601 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
2602 dd->port->commands[i].command_header->ctbau =
2603 __force_bit2int cpu_to_le32(
2604 (dd->port->commands[i].command_dma >> 16) >> 16);
2605 dd->port->commands[i].command_header->ctba =
2606 __force_bit2int cpu_to_le32(
2607 dd->port->commands[i].command_dma & 0xFFFFFFFF);
2610 * If this is not done, a bug is reported by the stock
2611 * FC11 i386. Due to the fact that it has lots of kernel
2612 * debugging enabled.
2614 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
2616 /* Mark all commands as currently inactive.*/
2617 atomic_set(&dd->port->commands[i].active, 0);
2620 /* Setup the pointers to the extended s_active and CI registers. */
2621 for (i = 0; i < dd->slot_groups; i++) {
2622 dd->port->s_active[i] =
2623 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
2624 dd->port->cmd_issue[i] =
2625 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
2626 dd->port->completed[i] =
2627 dd->port->mmio + i*0x80 + PORT_SDBV;
2630 /* Reset the HBA. */
2631 if (mtip_hba_reset(dd) < 0) {
2632 dev_err(&dd->pdev->dev,
2633 "Card did not reset within timeout\n");
2638 mtip_init_port(dd->port);
2639 mtip_start_port(dd->port);
2641 /* Setup the ISR and enable interrupts. */
2642 rv = devm_request_irq(&dd->pdev->dev,
2646 dev_driver_string(&dd->pdev->dev),
2650 dev_err(&dd->pdev->dev,
2651 "Unable to allocate IRQ %d\n", dd->pdev->irq);
2655 /* Enable interrupts on the HBA. */
2656 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2657 dd->mmio + HOST_CTL);
2659 init_timer(&dd->port->cmd_timer);
2660 init_waitqueue_head(&dd->port->svc_wait);
2662 dd->port->cmd_timer.data = (unsigned long int) dd->port;
2663 dd->port->cmd_timer.function = mtip_timeout_function;
2664 mod_timer(&dd->port->cmd_timer,
2665 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
2667 if (mtip_get_identify(dd->port, NULL) < 0) {
2672 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2673 MTIP_FTL_REBUILD_MAGIC) {
2674 set_bit(MTIP_FLAG_REBUILD_BIT, &dd->port->flags);
2675 return MTIP_FTL_REBUILD_MAGIC;
2677 mtip_dump_identify(dd->port);
2681 del_timer_sync(&dd->port->cmd_timer);
2683 /* Disable interrupts on the HBA. */
2684 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2685 dd->mmio + HOST_CTL);
2687 /*Release the IRQ. */
2688 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
2691 mtip_deinit_port(dd->port);
2693 /* Free the command/command header memory. */
2694 dmam_free_coherent(&dd->pdev->dev,
2695 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2696 dd->port->command_list,
2697 dd->port->command_list_dma);
2699 /* Free the memory allocated for the for structure. */
2706 * Called to deinitialize an interface.
2708 * @dd Pointer to the driver data structure.
2713 static int mtip_hw_exit(struct driver_data *dd)
2716 * Send standby immediate (E0h) to the drive so that it
2719 if (atomic_read(&dd->drv_cleanup_done) != true) {
2721 mtip_standby_immediate(dd->port);
2723 /* de-initialize the port. */
2724 mtip_deinit_port(dd->port);
2726 /* Disable interrupts on the HBA. */
2727 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2728 dd->mmio + HOST_CTL);
2731 del_timer_sync(&dd->port->cmd_timer);
2733 /* Release the IRQ. */
2734 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
2736 /* Stop the bottom half tasklet. */
2737 tasklet_kill(&dd->tasklet);
2739 /* Free the command/command header memory. */
2740 dmam_free_coherent(&dd->pdev->dev,
2741 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2742 dd->port->command_list,
2743 dd->port->command_list_dma);
2744 /* Free the memory allocated for the for structure. */
2751 * Issue a Standby Immediate command to the device.
2753 * This function is called by the Block Layer just before the
2754 * system powers off during a shutdown.
2756 * @dd Pointer to the driver data structure.
2761 static int mtip_hw_shutdown(struct driver_data *dd)
2764 * Send standby immediate (E0h) to the drive so that it
2767 mtip_standby_immediate(dd->port);
2775 * This function is called by the Block Layer just before the
2776 * system hibernates.
2778 * @dd Pointer to the driver data structure.
2781 * 0 Suspend was successful
2782 * -EFAULT Suspend was not successful
2784 static int mtip_hw_suspend(struct driver_data *dd)
2787 * Send standby immediate (E0h) to the drive
2788 * so that it saves its state.
2790 if (mtip_standby_immediate(dd->port) != 0) {
2791 dev_err(&dd->pdev->dev,
2792 "Failed standby-immediate command\n");
2796 /* Disable interrupts on the HBA.*/
2797 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2798 dd->mmio + HOST_CTL);
2799 mtip_deinit_port(dd->port);
2807 * This function is called by the Block Layer as the
2810 * @dd Pointer to the driver data structure.
2813 * 0 Resume was successful
2814 * -EFAULT Resume was not successful
2816 static int mtip_hw_resume(struct driver_data *dd)
2818 /* Perform any needed hardware setup steps */
2822 if (mtip_hba_reset(dd) != 0) {
2823 dev_err(&dd->pdev->dev,
2824 "Unable to reset the HBA\n");
2829 * Enable the port, DMA engine, and FIS reception specific
2830 * h/w in controller.
2832 mtip_init_port(dd->port);
2833 mtip_start_port(dd->port);
2835 /* Enable interrupts on the HBA.*/
2836 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2837 dd->mmio + HOST_CTL);
2843 * Helper function for reusing disk name
2844 * upon hot insertion.
2846 static int rssd_disk_name_format(char *prefix,
2851 const int base = 'z' - 'a' + 1;
2852 char *begin = buf + strlen(prefix);
2853 char *end = buf + buflen;
2863 *--p = 'a' + (index % unit);
2864 index = (index / unit) - 1;
2865 } while (index >= 0);
2867 memmove(begin, p, end - p);
2868 memcpy(buf, prefix, strlen(prefix));
2874 * Block layer IOCTL handler.
2876 * @dev Pointer to the block_device structure.
2878 * @cmd IOCTL command passed from the user application.
2879 * @arg Argument passed from the user application.
2882 * 0 IOCTL completed successfully.
2883 * -ENOTTY IOCTL not supported or invalid driver data
2884 * structure pointer.
2886 static int mtip_block_ioctl(struct block_device *dev,
2891 struct driver_data *dd = dev->bd_disk->private_data;
2893 if (!capable(CAP_SYS_ADMIN))
2903 return mtip_hw_ioctl(dd, cmd, arg);
2907 #ifdef CONFIG_COMPAT
2909 * Block layer compat IOCTL handler.
2911 * @dev Pointer to the block_device structure.
2913 * @cmd IOCTL command passed from the user application.
2914 * @arg Argument passed from the user application.
2917 * 0 IOCTL completed successfully.
2918 * -ENOTTY IOCTL not supported or invalid driver data
2919 * structure pointer.
2921 static int mtip_block_compat_ioctl(struct block_device *dev,
2926 struct driver_data *dd = dev->bd_disk->private_data;
2928 if (!capable(CAP_SYS_ADMIN))
2937 case HDIO_DRIVE_TASKFILE: {
2938 struct mtip_compat_ide_task_request_s __user *compat_req_task;
2939 ide_task_request_t req_task;
2940 int compat_tasksize, outtotal, ret;
2943 sizeof(struct mtip_compat_ide_task_request_s);
2946 (struct mtip_compat_ide_task_request_s __user *) arg;
2948 if (copy_from_user(&req_task, (void __user *) arg,
2949 compat_tasksize - (2 * sizeof(compat_long_t))))
2952 if (get_user(req_task.out_size, &compat_req_task->out_size))
2955 if (get_user(req_task.in_size, &compat_req_task->in_size))
2958 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
2960 ret = exec_drive_taskfile(dd, (void __user *) arg,
2961 &req_task, outtotal);
2963 if (copy_to_user((void __user *) arg, &req_task,
2965 (2 * sizeof(compat_long_t))))
2968 if (put_user(req_task.out_size, &compat_req_task->out_size))
2971 if (put_user(req_task.in_size, &compat_req_task->in_size))
2977 return mtip_hw_ioctl(dd, cmd, arg);
2983 * Obtain the geometry of the device.
2985 * You may think that this function is obsolete, but some applications,
2986 * fdisk for example still used CHS values. This function describes the
2987 * device as having 224 heads and 56 sectors per cylinder. These values are
2988 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
2989 * partition is described in terms of a start and end cylinder this means
2990 * that each partition is also 4KB aligned. Non-aligned partitions adversely
2991 * affects performance.
2993 * @dev Pointer to the block_device strucutre.
2994 * @geo Pointer to a hd_geometry structure.
2997 * 0 Operation completed successfully.
2998 * -ENOTTY An error occurred while reading the drive capacity.
3000 static int mtip_block_getgeo(struct block_device *dev,
3001 struct hd_geometry *geo)
3003 struct driver_data *dd = dev->bd_disk->private_data;
3009 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3010 dev_warn(&dd->pdev->dev,
3011 "Could not get drive capacity.\n");
3017 sector_div(capacity, (geo->heads * geo->sectors));
3018 geo->cylinders = capacity;
3023 * Block device operation function.
3025 * This structure contains pointers to the functions required by the block
3028 static const struct block_device_operations mtip_block_ops = {
3029 .ioctl = mtip_block_ioctl,
3030 #ifdef CONFIG_COMPAT
3031 .compat_ioctl = mtip_block_compat_ioctl,
3033 .getgeo = mtip_block_getgeo,
3034 .owner = THIS_MODULE
3038 * Block layer make request function.
3040 * This function is called by the kernel to process a BIO for
3043 * @queue Pointer to the request queue. Unused other than to obtain
3044 * the driver data structure.
3045 * @bio Pointer to the BIO.
3048 static void mtip_make_request(struct request_queue *queue, struct bio *bio)
3050 struct driver_data *dd = queue->queuedata;
3051 struct scatterlist *sg;
3052 struct bio_vec *bvec;
3056 if (unlikely(!bio_has_data(bio))) {
3057 blk_queue_flush(queue, 0);
3062 sg = mtip_hw_get_scatterlist(dd, &tag);
3063 if (likely(sg != NULL)) {
3064 blk_queue_bounce(queue, &bio);
3066 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3067 dev_warn(&dd->pdev->dev,
3068 "Maximum number of SGL entries exceeded");
3070 mtip_hw_release_scatterlist(dd, tag);
3074 /* Create the scatter list for this bio. */
3075 bio_for_each_segment(bvec, bio, nents) {
3076 sg_set_page(&sg[nents],
3082 /* Issue the read/write. */
3083 mtip_hw_submit_io(dd,
3090 bio->bi_rw & REQ_FUA,
3097 * Block layer initialization function.
3099 * This function is called once by the PCI layer for each P320
3100 * device that is connected to the system.
3102 * @dd Pointer to the driver data structure.
3105 * 0 on success else an error code.
3107 static int mtip_block_initialize(struct driver_data *dd)
3109 int rv = 0, wait_for_rebuild = 0;
3111 unsigned int index = 0;
3112 struct kobject *kobj;
3113 unsigned char thd_name[16];
3116 goto skip_create_disk; /* hw init done, before rebuild */
3118 /* Initialize the protocol layer. */
3119 wait_for_rebuild = mtip_hw_init(dd);
3120 if (wait_for_rebuild < 0) {
3121 dev_err(&dd->pdev->dev,
3122 "Protocol layer initialization failed\n");
3124 goto protocol_init_error;
3127 dd->disk = alloc_disk(MTIP_MAX_MINORS);
3128 if (dd->disk == NULL) {
3129 dev_err(&dd->pdev->dev,
3130 "Unable to allocate gendisk structure\n");
3132 goto alloc_disk_error;
3135 /* Generate the disk name, implemented same as in sd.c */
3137 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3140 spin_lock(&rssd_index_lock);
3141 rv = ida_get_new(&rssd_index_ida, &index);
3142 spin_unlock(&rssd_index_lock);
3143 } while (rv == -EAGAIN);
3148 rv = rssd_disk_name_format("rssd",
3150 dd->disk->disk_name,
3153 goto disk_index_error;
3155 dd->disk->driverfs_dev = &dd->pdev->dev;
3156 dd->disk->major = dd->major;
3157 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3158 dd->disk->fops = &mtip_block_ops;
3159 dd->disk->private_data = dd;
3163 * if rebuild pending, start the service thread, and delay the block
3164 * queue creation and add_disk()
3166 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3167 goto start_service_thread;
3170 /* Allocate the request queue. */
3171 dd->queue = blk_alloc_queue(GFP_KERNEL);
3172 if (dd->queue == NULL) {
3173 dev_err(&dd->pdev->dev,
3174 "Unable to allocate request queue\n");
3176 goto block_queue_alloc_init_error;
3179 /* Attach our request function to the request queue. */
3180 blk_queue_make_request(dd->queue, mtip_make_request);
3182 dd->disk->queue = dd->queue;
3183 dd->queue->queuedata = dd;
3185 /* Set device limits. */
3186 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3187 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3188 blk_queue_physical_block_size(dd->queue, 4096);
3189 blk_queue_io_min(dd->queue, 4096);
3190 blk_queue_flush(dd->queue, 0);
3192 /* Set the capacity of the device in 512 byte sectors. */
3193 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3194 dev_warn(&dd->pdev->dev,
3195 "Could not read drive capacity\n");
3197 goto read_capacity_error;
3199 set_capacity(dd->disk, capacity);
3201 /* Enable the block device and add it to /dev */
3205 * Now that the disk is active, initialize any sysfs attributes
3206 * managed by the protocol layer.
3208 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3210 mtip_hw_sysfs_init(dd, kobj);
3214 if (dd->mtip_svc_handler)
3215 return rv; /* service thread created for handling rebuild */
3217 start_service_thread:
3218 sprintf(thd_name, "mtip_svc_thd_%02d", index);
3220 dd->mtip_svc_handler = kthread_run(mtip_service_thread,
3223 if (IS_ERR(dd->mtip_svc_handler)) {
3224 printk(KERN_ERR "mtip32xx: service thread failed to start\n");
3225 dd->mtip_svc_handler = NULL;
3227 goto kthread_run_error;
3233 /* Delete our gendisk. This also removes the device from /dev */
3234 del_gendisk(dd->disk);
3236 read_capacity_error:
3237 blk_cleanup_queue(dd->queue);
3239 block_queue_alloc_init_error:
3241 spin_lock(&rssd_index_lock);
3242 ida_remove(&rssd_index_ida, index);
3243 spin_unlock(&rssd_index_lock);
3249 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
3251 protocol_init_error:
3256 * Block layer deinitialization function.
3258 * Called by the PCI layer as each P320 device is removed.
3260 * @dd Pointer to the driver data structure.
3265 static int mtip_block_remove(struct driver_data *dd)
3267 struct kobject *kobj;
3269 if (dd->mtip_svc_handler) {
3270 set_bit(MTIP_FLAG_SVC_THD_SHOULD_STOP_BIT, &dd->port->flags);
3271 wake_up_interruptible(&dd->port->svc_wait);
3272 kthread_stop(dd->mtip_svc_handler);
3275 /* Clean up the sysfs attributes managed by the protocol layer. */
3276 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3278 mtip_hw_sysfs_exit(dd, kobj);
3283 * Delete our gendisk structure. This also removes the device
3286 del_gendisk(dd->disk);
3287 blk_cleanup_queue(dd->queue);
3291 /* De-initialize the protocol layer. */
3298 * Function called by the PCI layer when just before the
3299 * machine shuts down.
3301 * If a protocol layer shutdown function is present it will be called
3304 * @dd Pointer to the driver data structure.
3309 static int mtip_block_shutdown(struct driver_data *dd)
3311 dev_info(&dd->pdev->dev,
3312 "Shutting down %s ...\n", dd->disk->disk_name);
3314 /* Delete our gendisk structure, and cleanup the blk queue. */
3315 del_gendisk(dd->disk);
3316 blk_cleanup_queue(dd->queue);
3320 mtip_hw_shutdown(dd);
3324 static int mtip_block_suspend(struct driver_data *dd)
3326 dev_info(&dd->pdev->dev,
3327 "Suspending %s ...\n", dd->disk->disk_name);
3328 mtip_hw_suspend(dd);
3332 static int mtip_block_resume(struct driver_data *dd)
3334 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
3335 dd->disk->disk_name);
3341 * Called for each supported PCI device detected.
3343 * This function allocates the private data structure, enables the
3344 * PCI device and then calls the block layer initialization function.
3347 * 0 on success else an error code.
3349 static int mtip_pci_probe(struct pci_dev *pdev,
3350 const struct pci_device_id *ent)
3353 struct driver_data *dd = NULL;
3355 /* Allocate memory for this devices private data. */
3356 dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
3359 "Unable to allocate memory for driver data\n");
3363 /* Set the atomic variable as 1 in case of SRSI */
3364 atomic_set(&dd->drv_cleanup_done, true);
3366 atomic_set(&dd->resumeflag, false);
3368 /* Attach the private data to this PCI device. */
3369 pci_set_drvdata(pdev, dd);
3371 rv = pcim_enable_device(pdev);
3373 dev_err(&pdev->dev, "Unable to enable device\n");
3377 /* Map BAR5 to memory. */
3378 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
3380 dev_err(&pdev->dev, "Unable to map regions\n");
3384 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
3385 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3388 rv = pci_set_consistent_dma_mask(pdev,
3391 dev_warn(&pdev->dev,
3392 "64-bit DMA enable failed\n");
3398 pci_set_master(pdev);
3400 if (pci_enable_msi(pdev)) {
3401 dev_warn(&pdev->dev,
3402 "Unable to enable MSI interrupt.\n");
3403 goto block_initialize_err;
3406 /* Copy the info we may need later into the private data structure. */
3407 dd->major = mtip_major;
3408 dd->instance = instance;
3411 /* Initialize the block layer. */
3412 rv = mtip_block_initialize(dd);
3415 "Unable to initialize block layer\n");
3416 goto block_initialize_err;
3420 * Increment the instance count so that each device has a unique
3427 block_initialize_err:
3428 pci_disable_msi(pdev);
3431 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3435 pci_set_drvdata(pdev, NULL);
3438 /* Set the atomic variable as 0 in case of SRSI */
3439 atomic_set(&dd->drv_cleanup_done, true);
3445 * Called for each probed device when the device is removed or the
3446 * driver is unloaded.
3451 static void mtip_pci_remove(struct pci_dev *pdev)
3453 struct driver_data *dd = pci_get_drvdata(pdev);
3456 if (mtip_check_surprise_removal(pdev)) {
3457 while (atomic_read(&dd->drv_cleanup_done) == false) {
3460 if (counter == 10) {
3461 /* Cleanup the outstanding commands */
3462 mtip_command_cleanup(dd);
3467 /* Set the atomic variable as 1 in case of SRSI */
3468 atomic_set(&dd->drv_cleanup_done, true);
3470 /* Clean up the block layer. */
3471 mtip_block_remove(dd);
3473 pci_disable_msi(pdev);
3476 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
3480 * Called for each probed device when the device is suspended.
3486 static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
3489 struct driver_data *dd = pci_get_drvdata(pdev);
3493 "Driver private datastructure is NULL\n");
3497 atomic_set(&dd->resumeflag, true);
3499 /* Disable ports & interrupts then send standby immediate */
3500 rv = mtip_block_suspend(dd);
3503 "Failed to suspend controller\n");
3508 * Save the pci config space to pdev structure &
3509 * disable the device
3511 pci_save_state(pdev);
3512 pci_disable_device(pdev);
3514 /* Move to Low power state*/
3515 pci_set_power_state(pdev, PCI_D3hot);
3521 * Called for each probed device when the device is resumed.
3527 static int mtip_pci_resume(struct pci_dev *pdev)
3530 struct driver_data *dd;
3532 dd = pci_get_drvdata(pdev);
3535 "Driver private datastructure is NULL\n");
3539 /* Move the device to active State */
3540 pci_set_power_state(pdev, PCI_D0);
3542 /* Restore PCI configuration space */
3543 pci_restore_state(pdev);
3545 /* Enable the PCI device*/
3546 rv = pcim_enable_device(pdev);
3549 "Failed to enable card during resume\n");
3552 pci_set_master(pdev);
3555 * Calls hbaReset, initPort, & startPort function
3556 * then enables interrupts
3558 rv = mtip_block_resume(dd);
3560 dev_err(&pdev->dev, "Unable to resume\n");
3563 atomic_set(&dd->resumeflag, false);
3574 static void mtip_pci_shutdown(struct pci_dev *pdev)
3576 struct driver_data *dd = pci_get_drvdata(pdev);
3578 mtip_block_shutdown(dd);
3581 /* Table of device ids supported by this driver. */
3582 static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
3583 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320_DEVICE_ID) },
3587 /* Structure that describes the PCI driver functions. */
3588 static struct pci_driver mtip_pci_driver = {
3589 .name = MTIP_DRV_NAME,
3590 .id_table = mtip_pci_tbl,
3591 .probe = mtip_pci_probe,
3592 .remove = mtip_pci_remove,
3593 .suspend = mtip_pci_suspend,
3594 .resume = mtip_pci_resume,
3595 .shutdown = mtip_pci_shutdown,
3598 MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
3601 * Module initialization function.
3603 * Called once when the module is loaded. This function allocates a major
3604 * block device number to the Cyclone devices and registers the PCI layer
3608 * 0 on success else error code.
3610 static int __init mtip_init(void)
3612 printk(KERN_INFO MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
3614 /* Allocate a major block device number to use with this driver. */
3615 mtip_major = register_blkdev(0, MTIP_DRV_NAME);
3616 if (mtip_major < 0) {
3617 printk(KERN_ERR "Unable to register block device (%d)\n",
3622 /* Register our PCI operations. */
3623 return pci_register_driver(&mtip_pci_driver);
3627 * Module de-initialization function.
3629 * Called once when the module is unloaded. This function deallocates
3630 * the major block device number allocated by mtip_init() and
3631 * unregisters the PCI layer of the driver.
3636 static void __exit mtip_exit(void)
3638 /* Release the allocated major block device number. */
3639 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
3641 /* Unregister the PCI driver. */
3642 pci_unregister_driver(&mtip_pci_driver);
3645 MODULE_AUTHOR("Micron Technology, Inc");
3646 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
3647 MODULE_LICENSE("GPL");
3648 MODULE_VERSION(MTIP_DRV_VERSION);
3650 module_init(mtip_init);
3651 module_exit(mtip_exit);