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);
91 struct mtip_compat_ide_task_request_s {
94 ide_reg_valid_t out_flags;
95 ide_reg_valid_t in_flags;
98 compat_ulong_t out_size;
99 compat_ulong_t in_size;
104 * This function check_for_surprise_removal is called
105 * while card is removed from the system and it will
106 * read the vendor id from the configration space
108 * @pdev Pointer to the pci_dev structure.
111 * true if device removed, else false
113 static bool mtip_check_surprise_removal(struct pci_dev *pdev)
117 /* Read the vendorID from the configuration space */
118 pci_read_config_word(pdev, 0x00, &vendor_id);
119 if (vendor_id == 0xFFFF)
120 return true; /* device removed */
122 return false; /* device present */
126 * This function is called for clean the pending command in the
127 * command slot during the surprise removal of device and return
128 * error to the upper layer.
130 * @dd Pointer to the DRIVER_DATA structure.
135 static void mtip_command_cleanup(struct driver_data *dd)
137 int group = 0, commandslot = 0, commandindex = 0;
138 struct mtip_cmd *command;
139 struct mtip_port *port = dd->port;
141 for (group = 0; group < 4; group++) {
142 for (commandslot = 0; commandslot < 32; commandslot++) {
143 if (!(port->allocated[group] & (1 << commandslot)))
146 commandindex = group << 5 | commandslot;
147 command = &port->commands[commandindex];
149 if (atomic_read(&command->active)
150 && (command->async_callback)) {
151 command->async_callback(command->async_data,
153 command->async_callback = NULL;
154 command->async_data = NULL;
157 dma_unmap_sg(&port->dd->pdev->dev,
159 command->scatter_ents,
166 atomic_set(&dd->drv_cleanup_done, true);
170 * Obtain an empty command slot.
172 * This function needs to be reentrant since it could be called
173 * at the same time on multiple CPUs. The allocation of the
174 * command slot must be atomic.
176 * @port Pointer to the port data structure.
179 * >= 0 Index of command slot obtained.
180 * -1 No command slots available.
182 static int get_slot(struct mtip_port *port)
185 unsigned int num_command_slots = port->dd->slot_groups * 32;
188 * Try 10 times, because there is a small race here.
189 * that's ok, because it's still cheaper than a lock.
191 * Race: Since this section is not protected by lock, same bit
192 * could be chosen by different process contexts running in
193 * different processor. So instead of costly lock, we are going
196 for (i = 0; i < 10; i++) {
197 slot = find_next_zero_bit(port->allocated,
198 num_command_slots, 1);
199 if ((slot < num_command_slots) &&
200 (!test_and_set_bit(slot, port->allocated)))
203 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
205 if (mtip_check_surprise_removal(port->dd->pdev)) {
206 /* Device not present, clean outstanding commands */
207 mtip_command_cleanup(port->dd);
213 * Release a command slot.
215 * @port Pointer to the port data structure.
216 * @tag Tag of command to release
221 static inline void release_slot(struct mtip_port *port, int tag)
223 smp_mb__before_clear_bit();
224 clear_bit(tag, port->allocated);
225 smp_mb__after_clear_bit();
229 * Reset the HBA (without sleeping)
231 * Just like hba_reset, except does not call sleep, so can be
232 * run from interrupt/tasklet context.
234 * @dd Pointer to the driver data structure.
237 * 0 The reset was successful.
238 * -1 The HBA Reset bit did not clear.
240 static int hba_reset_nosleep(struct driver_data *dd)
242 unsigned long timeout;
244 /* Chip quirk: quiesce any chip function */
247 /* Set the reset bit */
248 writel(HOST_RESET, dd->mmio + HOST_CTL);
251 readl(dd->mmio + HOST_CTL);
254 * Wait 10ms then spin for up to 1 second
255 * waiting for reset acknowledgement
257 timeout = jiffies + msecs_to_jiffies(1000);
259 while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
260 && time_before(jiffies, timeout))
263 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
270 * Issue a command to the hardware.
272 * Set the appropriate bit in the s_active and Command Issue hardware
273 * registers, causing hardware command processing to begin.
275 * @port Pointer to the port structure.
276 * @tag The tag of the command to be issued.
281 static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
283 unsigned long flags = 0;
285 atomic_set(&port->commands[tag].active, 1);
287 spin_lock_irqsave(&port->cmd_issue_lock, flags);
289 writel((1 << MTIP_TAG_BIT(tag)),
290 port->s_active[MTIP_TAG_INDEX(tag)]);
291 writel((1 << MTIP_TAG_BIT(tag)),
292 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
294 spin_unlock_irqrestore(&port->cmd_issue_lock, flags);
298 * Enable/disable the reception of FIS
300 * @port Pointer to the port data structure
301 * @enable 1 to enable, 0 to disable
304 * Previous state: 1 enabled, 0 disabled
306 static int mtip_enable_fis(struct mtip_port *port, int enable)
310 /* enable FIS reception */
311 tmp = readl(port->mmio + PORT_CMD);
313 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
315 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
318 readl(port->mmio + PORT_CMD);
320 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
324 * Enable/disable the DMA engine
326 * @port Pointer to the port data structure
327 * @enable 1 to enable, 0 to disable
330 * Previous state: 1 enabled, 0 disabled.
332 static int mtip_enable_engine(struct mtip_port *port, int enable)
336 /* enable FIS reception */
337 tmp = readl(port->mmio + PORT_CMD);
339 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
341 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
343 readl(port->mmio + PORT_CMD);
344 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
348 * Enables the port DMA engine and FIS reception.
353 static inline void mtip_start_port(struct mtip_port *port)
355 /* Enable FIS reception */
356 mtip_enable_fis(port, 1);
358 /* Enable the DMA engine */
359 mtip_enable_engine(port, 1);
363 * Deinitialize a port by disabling port interrupts, the DMA engine,
366 * @port Pointer to the port structure
371 static inline void mtip_deinit_port(struct mtip_port *port)
373 /* Disable interrupts on this port */
374 writel(0, port->mmio + PORT_IRQ_MASK);
376 /* Disable the DMA engine */
377 mtip_enable_engine(port, 0);
379 /* Disable FIS reception */
380 mtip_enable_fis(port, 0);
386 * This function deinitializes the port by calling mtip_deinit_port() and
387 * then initializes it by setting the command header and RX FIS addresses,
388 * clearing the SError register and any pending port interrupts before
389 * re-enabling the default set of port interrupts.
391 * @port Pointer to the port structure.
396 static void mtip_init_port(struct mtip_port *port)
399 mtip_deinit_port(port);
401 /* Program the command list base and FIS base addresses */
402 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
403 writel((port->command_list_dma >> 16) >> 16,
404 port->mmio + PORT_LST_ADDR_HI);
405 writel((port->rxfis_dma >> 16) >> 16,
406 port->mmio + PORT_FIS_ADDR_HI);
409 writel(port->command_list_dma & 0xFFFFFFFF,
410 port->mmio + PORT_LST_ADDR);
411 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
414 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
416 /* reset the completed registers.*/
417 for (i = 0; i < port->dd->slot_groups; i++)
418 writel(0xFFFFFFFF, port->completed[i]);
420 /* Clear any pending interrupts for this port */
421 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
423 /* Enable port interrupts */
424 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
430 * @port Pointer to the port data structure.
435 static void mtip_restart_port(struct mtip_port *port)
437 unsigned long timeout;
439 /* Disable the DMA engine */
440 mtip_enable_engine(port, 0);
442 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
443 timeout = jiffies + msecs_to_jiffies(500);
444 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
445 && time_before(jiffies, timeout))
449 * Chip quirk: escalate to hba reset if
450 * PxCMD.CR not clear after 500 ms
452 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
453 dev_warn(&port->dd->pdev->dev,
454 "PxCMD.CR not clear, escalating reset\n");
456 if (hba_reset_nosleep(port->dd))
457 dev_err(&port->dd->pdev->dev,
458 "HBA reset escalation failed.\n");
460 /* 30 ms delay before com reset to quiesce chip */
464 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
467 writel(readl(port->mmio + PORT_SCR_CTL) |
468 1, port->mmio + PORT_SCR_CTL);
469 readl(port->mmio + PORT_SCR_CTL);
471 /* Wait 1 ms to quiesce chip function */
472 timeout = jiffies + msecs_to_jiffies(1);
473 while (time_before(jiffies, timeout))
476 /* Clear PxSCTL.DET */
477 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
478 port->mmio + PORT_SCR_CTL);
479 readl(port->mmio + PORT_SCR_CTL);
481 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
482 timeout = jiffies + msecs_to_jiffies(500);
483 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
484 && time_before(jiffies, timeout))
487 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
488 dev_warn(&port->dd->pdev->dev,
489 "COM reset failed\n");
491 /* Clear SError, the PxSERR.DIAG.x should be set so clear it */
492 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
494 /* Enable the DMA engine */
495 mtip_enable_engine(port, 1);
499 * Called periodically to see if any read/write commands are
500 * taking too long to complete.
502 * @data Pointer to the PORT data structure.
507 static void mtip_timeout_function(unsigned long int data)
509 struct mtip_port *port = (struct mtip_port *) data;
510 struct host_to_dev_fis *fis;
511 struct mtip_cmd *command;
512 int tag, cmdto_cnt = 0;
513 unsigned int bit, group;
514 unsigned int num_command_slots = port->dd->slot_groups * 32;
519 if (atomic_read(&port->dd->resumeflag) == true) {
520 mod_timer(&port->cmd_timer,
521 jiffies + msecs_to_jiffies(30000));
525 for (tag = 0; tag < num_command_slots; tag++) {
527 * Skip internal command slot as it has
528 * its own timeout mechanism
530 if (tag == MTIP_TAG_INTERNAL)
533 if (atomic_read(&port->commands[tag].active) &&
534 (time_after(jiffies, port->commands[tag].comp_time))) {
538 command = &port->commands[tag];
539 fis = (struct host_to_dev_fis *) command->command;
541 dev_warn(&port->dd->pdev->dev,
542 "Timeout for command tag %d\n", tag);
546 set_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
549 * Clear the completed bit. This should prevent
550 * any interrupt handlers from trying to retire
553 writel(1 << bit, port->completed[group]);
555 /* Call the async completion callback. */
556 if (likely(command->async_callback))
557 command->async_callback(command->async_data,
559 command->async_callback = NULL;
560 command->comp_func = NULL;
562 /* Unmap the DMA scatter list entries */
563 dma_unmap_sg(&port->dd->pdev->dev,
565 command->scatter_ents,
569 * Clear the allocated bit and active tag for the
572 atomic_set(&port->commands[tag].active, 0);
573 release_slot(port, tag);
580 dev_warn(&port->dd->pdev->dev,
581 "%d commands timed out: restarting port",
583 mtip_restart_port(port);
584 clear_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
585 wake_up_interruptible(&port->svc_wait);
588 /* Restart the timer */
589 mod_timer(&port->cmd_timer,
590 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
594 * IO completion function.
596 * This completion function is called by the driver ISR when a
597 * command that was issued by the kernel completes. It first calls the
598 * asynchronous completion function which normally calls back into the block
599 * layer passing the asynchronous callback data, then unmaps the
600 * scatter list associated with the completed command, and finally
601 * clears the allocated bit associated with the completed command.
603 * @port Pointer to the port data structure.
604 * @tag Tag of the command.
605 * @data Pointer to driver_data.
606 * @status Completion status.
611 static void mtip_async_complete(struct mtip_port *port,
616 struct mtip_cmd *command;
617 struct driver_data *dd = data;
618 int cb_status = status ? -EIO : 0;
620 if (unlikely(!dd) || unlikely(!port))
623 command = &port->commands[tag];
625 if (unlikely(status == PORT_IRQ_TF_ERR)) {
626 dev_warn(&port->dd->pdev->dev,
627 "Command tag %d failed due to TFE\n", tag);
630 /* Upper layer callback */
631 if (likely(command->async_callback))
632 command->async_callback(command->async_data, cb_status);
634 command->async_callback = NULL;
635 command->comp_func = NULL;
637 /* Unmap the DMA scatter list entries */
638 dma_unmap_sg(&dd->pdev->dev,
640 command->scatter_ents,
643 /* Clear the allocated and active bits for the command */
644 atomic_set(&port->commands[tag].active, 0);
645 release_slot(port, tag);
651 * Internal command completion callback function.
653 * This function is normally called by the driver ISR when an internal
654 * command completed. This function signals the command completion by
655 * calling complete().
657 * @port Pointer to the port data structure.
658 * @tag Tag of the command that has completed.
659 * @data Pointer to a completion structure.
660 * @status Completion status.
665 static void mtip_completion(struct mtip_port *port,
670 struct mtip_cmd *command = &port->commands[tag];
671 struct completion *waiting = data;
672 if (unlikely(status == PORT_IRQ_TF_ERR))
673 dev_warn(&port->dd->pdev->dev,
674 "Internal command %d completed with TFE\n", tag);
676 command->async_callback = NULL;
677 command->comp_func = NULL;
683 * Helper function for tag logging
685 static void print_tags(struct driver_data *dd,
687 unsigned long *tagbits)
689 unsigned int tag, count = 0;
691 for (tag = 0; tag < (dd->slot_groups) * 32; tag++) {
692 if (test_bit(tag, tagbits))
696 dev_info(&dd->pdev->dev, "%s [%i tags]\n", msg, count);
702 * @dd Pointer to the DRIVER_DATA structure.
707 static void mtip_handle_tfe(struct driver_data *dd)
709 int group, tag, bit, reissue;
710 struct mtip_port *port;
711 struct mtip_cmd *command;
713 struct host_to_dev_fis *fis;
714 unsigned long tagaccum[SLOTBITS_IN_LONGS];
716 dev_warn(&dd->pdev->dev, "Taskfile error\n");
720 /* Stop the timer to prevent command timeouts. */
721 del_timer(&port->cmd_timer);
724 set_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
726 /* Loop through all the groups */
727 for (group = 0; group < dd->slot_groups; group++) {
728 completed = readl(port->completed[group]);
730 /* clear completed status register in the hardware.*/
731 writel(completed, port->completed[group]);
733 /* clear the tag accumulator */
734 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
736 /* Process successfully completed commands */
737 for (bit = 0; bit < 32 && completed; bit++) {
738 if (!(completed & (1<<bit)))
740 tag = (group << 5) + bit;
742 /* Skip the internal command slot */
743 if (tag == MTIP_TAG_INTERNAL)
746 command = &port->commands[tag];
747 if (likely(command->comp_func)) {
748 set_bit(tag, tagaccum);
749 atomic_set(&port->commands[tag].active, 0);
750 command->comp_func(port,
755 dev_err(&port->dd->pdev->dev,
756 "Missing completion func for tag %d",
758 if (mtip_check_surprise_removal(dd->pdev)) {
759 mtip_command_cleanup(dd);
760 /* don't proceed further */
766 print_tags(dd, "TFE tags completed:", tagaccum);
768 /* Restart the port */
770 mtip_restart_port(port);
772 /* clear the tag accumulator */
773 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
775 /* Loop through all the groups */
776 for (group = 0; group < dd->slot_groups; group++) {
777 for (bit = 0; bit < 32; bit++) {
779 tag = (group << 5) + bit;
781 /* If the active bit is set re-issue the command */
782 if (atomic_read(&port->commands[tag].active) == 0)
785 fis = (struct host_to_dev_fis *)
786 port->commands[tag].command;
788 /* Should re-issue? */
789 if (tag == MTIP_TAG_INTERNAL ||
790 fis->command == ATA_CMD_SET_FEATURES)
794 * First check if this command has
795 * exceeded its retries.
798 (port->commands[tag].retries-- > 0)) {
800 set_bit(tag, tagaccum);
802 /* Update the timeout value. */
803 port->commands[tag].comp_time =
804 jiffies + msecs_to_jiffies(
805 MTIP_NCQ_COMMAND_TIMEOUT_MS);
806 /* Re-issue the command. */
807 mtip_issue_ncq_command(port, tag);
812 /* Retire a command that will not be reissued */
813 dev_warn(&port->dd->pdev->dev,
814 "retiring tag %d\n", tag);
815 atomic_set(&port->commands[tag].active, 0);
817 if (port->commands[tag].comp_func)
818 port->commands[tag].comp_func(
821 port->commands[tag].comp_data,
824 dev_warn(&port->dd->pdev->dev,
825 "Bad completion for tag %d\n",
829 print_tags(dd, "TFE tags reissued:", tagaccum);
831 /* clear eh_active */
832 clear_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags);
833 wake_up_interruptible(&port->svc_wait);
835 mod_timer(&port->cmd_timer,
836 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
840 * Handle a set device bits interrupt
842 static inline void mtip_process_sdbf(struct driver_data *dd)
844 struct mtip_port *port = dd->port;
847 struct mtip_cmd *command;
849 /* walk all bits in all slot groups */
850 for (group = 0; group < dd->slot_groups; group++) {
851 completed = readl(port->completed[group]);
853 /* clear completed status register in the hardware.*/
854 writel(completed, port->completed[group]);
856 /* Process completed commands. */
858 (bit < 32) && completed;
859 bit++, completed >>= 1) {
860 if (completed & 0x01) {
861 tag = (group << 5) | bit;
863 /* skip internal command slot. */
864 if (unlikely(tag == MTIP_TAG_INTERNAL))
867 command = &port->commands[tag];
868 /* make internal callback */
869 if (likely(command->comp_func)) {
876 dev_warn(&dd->pdev->dev,
881 if (mtip_check_surprise_removal(
883 mtip_command_cleanup(dd);
893 * Process legacy pio and d2h interrupts
895 static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
897 struct mtip_port *port = dd->port;
898 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
900 if (test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) &&
901 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
902 & (1 << MTIP_TAG_INTERNAL))) {
903 if (cmd->comp_func) {
912 dev_warn(&dd->pdev->dev, "IRQ status 0x%x ignored.\n", port_stat);
918 * Demux and handle errors
920 static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
922 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
925 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
926 dev_warn(&dd->pdev->dev,
927 "Clearing PxSERR.DIAG.x\n");
928 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
931 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
932 dev_warn(&dd->pdev->dev,
933 "Clearing PxSERR.DIAG.n\n");
934 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
937 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
938 dev_warn(&dd->pdev->dev,
939 "Port stat errors %x unhandled\n",
940 (port_stat & ~PORT_IRQ_HANDLED));
944 static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
946 struct driver_data *dd = (struct driver_data *) data;
947 struct mtip_port *port = dd->port;
948 u32 hba_stat, port_stat;
951 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
955 /* Acknowledge the interrupt status on the port.*/
956 port_stat = readl(port->mmio + PORT_IRQ_STAT);
957 writel(port_stat, port->mmio + PORT_IRQ_STAT);
959 /* Demux port status */
960 if (likely(port_stat & PORT_IRQ_SDB_FIS))
961 mtip_process_sdbf(dd);
963 if (unlikely(port_stat & PORT_IRQ_ERR)) {
964 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
965 mtip_command_cleanup(dd);
966 /* don't proceed further */
970 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
973 if (unlikely(port_stat & PORT_IRQ_LEGACY))
974 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
977 /* acknowledge interrupt */
978 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
984 * Wrapper for mtip_handle_irq
985 * (ignores return code)
987 static void mtip_tasklet(unsigned long data)
989 mtip_handle_irq((struct driver_data *) data);
993 * HBA interrupt subroutine.
996 * @instance Pointer to the driver data structure.
999 * IRQ_HANDLED A HBA interrupt was pending and handled.
1000 * IRQ_NONE This interrupt was not for the HBA.
1002 static irqreturn_t mtip_irq_handler(int irq, void *instance)
1004 struct driver_data *dd = instance;
1005 tasklet_schedule(&dd->tasklet);
1009 static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1011 atomic_set(&port->commands[tag].active, 1);
1012 writel(1 << MTIP_TAG_BIT(tag),
1013 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1017 * Wait for port to quiesce
1019 * @port Pointer to port data structure
1020 * @timeout Max duration to wait (ms)
1024 * -EBUSY Commands still active
1026 static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1029 unsigned int n, active;
1031 to = jiffies + msecs_to_jiffies(timeout);
1033 if (test_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags)) {
1035 continue; /* svc thd is actively issuing commands */
1038 * Ignore s_active bit 0 of array element 0.
1039 * This bit will always be set
1041 active = readl(port->s_active[0]) & 0xFFFFFFFE;
1042 for (n = 1; n < port->dd->slot_groups; n++)
1043 active |= readl(port->s_active[n]);
1049 } while (time_before(jiffies, to));
1051 return active ? -EBUSY : 0;
1055 * Execute an internal command and wait for the completion.
1057 * @port Pointer to the port data structure.
1058 * @fis Pointer to the FIS that describes the command.
1059 * @fis_len Length in WORDS of the FIS.
1060 * @buffer DMA accessible for command data.
1061 * @buf_len Length, in bytes, of the data buffer.
1062 * @opts Command header options, excluding the FIS length
1063 * and the number of PRD entries.
1064 * @timeout Time in ms to wait for the command to complete.
1067 * 0 Command completed successfully.
1068 * -EFAULT The buffer address is not correctly aligned.
1069 * -EBUSY Internal command or other IO in progress.
1070 * -EAGAIN Time out waiting for command to complete.
1072 static int mtip_exec_internal_command(struct mtip_port *port,
1079 unsigned long timeout)
1081 struct mtip_cmd_sg *command_sg;
1082 DECLARE_COMPLETION_ONSTACK(wait);
1084 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
1086 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1087 if (buffer & 0x00000007) {
1088 dev_err(&port->dd->pdev->dev,
1089 "SG buffer is not 8 byte aligned\n");
1093 /* Only one internal command should be running at a time */
1094 if (test_and_set_bit(MTIP_TAG_INTERNAL, port->allocated)) {
1095 dev_warn(&port->dd->pdev->dev,
1096 "Internal command already active\n");
1099 set_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
1101 if (atomic == GFP_KERNEL) {
1102 /* wait for io to complete if non atomic */
1103 if (mtip_quiesce_io(port, 5000) < 0) {
1104 dev_warn(&port->dd->pdev->dev,
1105 "Failed to quiesce IO\n");
1106 release_slot(port, MTIP_TAG_INTERNAL);
1107 clear_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
1108 wake_up_interruptible(&port->svc_wait);
1112 /* Set the completion function and data for the command. */
1113 int_cmd->comp_data = &wait;
1114 int_cmd->comp_func = mtip_completion;
1117 /* Clear completion - we're going to poll */
1118 int_cmd->comp_data = NULL;
1119 int_cmd->comp_func = NULL;
1122 /* Copy the command to the command table */
1123 memcpy(int_cmd->command, fis, fis_len*4);
1125 /* Populate the SG list */
1126 int_cmd->command_header->opts =
1127 __force_bit2int cpu_to_le32(opts | fis_len);
1129 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1132 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1134 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1135 command_sg->dba_upper =
1136 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
1138 int_cmd->command_header->opts |=
1139 __force_bit2int cpu_to_le32((1 << 16));
1142 /* Populate the command header */
1143 int_cmd->command_header->byte_count = 0;
1145 /* Issue the command to the hardware */
1146 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1148 /* Poll if atomic, wait_for_completion otherwise */
1149 if (atomic == GFP_KERNEL) {
1150 /* Wait for the command to complete or timeout. */
1151 if (wait_for_completion_timeout(
1153 msecs_to_jiffies(timeout)) == 0) {
1154 dev_err(&port->dd->pdev->dev,
1155 "Internal command did not complete [%d] "
1156 "within timeout of %lu ms\n",
1161 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1162 & (1 << MTIP_TAG_INTERNAL)) {
1163 dev_warn(&port->dd->pdev->dev,
1164 "Retiring internal command but CI is 1.\n");
1168 /* Spin for <timeout> checking if command still outstanding */
1169 timeout = jiffies + msecs_to_jiffies(timeout);
1172 port->cmd_issue[MTIP_TAG_INTERNAL])
1173 & (1 << MTIP_TAG_INTERNAL))
1174 && time_before(jiffies, timeout))
1177 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1178 & (1 << MTIP_TAG_INTERNAL)) {
1179 dev_err(&port->dd->pdev->dev,
1180 "Internal command did not complete [%d]\n",
1186 /* Clear the allocated and active bits for the internal command. */
1187 atomic_set(&int_cmd->active, 0);
1188 release_slot(port, MTIP_TAG_INTERNAL);
1189 clear_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags);
1190 wake_up_interruptible(&port->svc_wait);
1196 * Byte-swap ATA ID strings.
1198 * ATA identify data contains strings in byte-swapped 16-bit words.
1199 * They must be swapped (on all architectures) to be usable as C strings.
1200 * This function swaps bytes in-place.
1202 * @buf The buffer location of the string
1203 * @len The number of bytes to swap
1208 static inline void ata_swap_string(u16 *buf, unsigned int len)
1211 for (i = 0; i < (len/2); i++)
1212 be16_to_cpus(&buf[i]);
1216 * Request the device identity information.
1218 * If a user space buffer is not specified, i.e. is NULL, the
1219 * identify information is still read from the drive and placed
1220 * into the identify data buffer (@e port->identify) in the
1221 * port data structure.
1222 * When the identify buffer contains valid identify information @e
1223 * port->identify_valid is non-zero.
1225 * @port Pointer to the port structure.
1226 * @user_buffer A user space buffer where the identify data should be
1230 * 0 Command completed successfully.
1231 * -EFAULT An error occurred while coping data to the user buffer.
1232 * -1 Command failed.
1234 static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1237 struct host_to_dev_fis fis;
1239 /* Build the FIS. */
1240 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1243 fis.command = ATA_CMD_ID_ATA;
1245 /* Set the identify information as invalid. */
1246 port->identify_valid = 0;
1248 /* Clear the identify information. */
1249 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1251 /* Execute the command. */
1252 if (mtip_exec_internal_command(port,
1256 sizeof(u16) * ATA_ID_WORDS,
1259 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1266 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1267 * perform field-sensitive swapping on the string fields.
1268 * See the kernel use of ata_id_string() for proof of this.
1270 #ifdef __LITTLE_ENDIAN
1271 ata_swap_string(port->identify + 27, 40); /* model string*/
1272 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1273 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1277 for (i = 0; i < ATA_ID_WORDS; i++)
1278 port->identify[i] = le16_to_cpu(port->identify[i]);
1282 /* Set the identify buffer as valid. */
1283 port->identify_valid = 1;
1289 ATA_ID_WORDS * sizeof(u16))) {
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 /* Build the FIS. */
1314 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1317 fis.command = ATA_CMD_STANDBYNOW1;
1319 /* Execute the command. Use a 15-second timeout for large drives. */
1320 rv = mtip_exec_internal_command(port,
1333 * Get the drive capacity.
1335 * @dd Pointer to the device data structure.
1336 * @sectors Pointer to the variable that will receive the sector count.
1339 * 1 Capacity was returned successfully.
1340 * 0 The identify information is invalid.
1342 static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
1344 struct mtip_port *port = dd->port;
1345 u64 total, raw0, raw1, raw2, raw3;
1346 raw0 = port->identify[100];
1347 raw1 = port->identify[101];
1348 raw2 = port->identify[102];
1349 raw3 = port->identify[103];
1350 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1352 return (bool) !!port->identify_valid;
1358 * Resets the HBA by setting the HBA Reset bit in the Global
1359 * HBA Control register. After setting the HBA Reset bit the
1360 * function waits for 1 second before reading the HBA Reset
1361 * bit to make sure it has cleared. If HBA Reset is not clear
1362 * an error is returned. Cannot be used in non-blockable
1365 * @dd Pointer to the driver data structure.
1368 * 0 The reset was successful.
1369 * -1 The HBA Reset bit did not clear.
1371 static int mtip_hba_reset(struct driver_data *dd)
1373 mtip_deinit_port(dd->port);
1375 /* Set the reset bit */
1376 writel(HOST_RESET, dd->mmio + HOST_CTL);
1379 readl(dd->mmio + HOST_CTL);
1381 /* Wait for reset to clear */
1384 /* Check the bit has cleared */
1385 if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
1386 dev_err(&dd->pdev->dev,
1387 "Reset bit did not clear.\n");
1395 * Display the identify command data.
1397 * @port Pointer to the port data structure.
1402 static void mtip_dump_identify(struct mtip_port *port)
1405 unsigned short revid;
1408 if (!port->identify_valid)
1411 strlcpy(cbuf, (char *)(port->identify+10), 21);
1412 dev_info(&port->dd->pdev->dev,
1413 "Serial No.: %s\n", cbuf);
1415 strlcpy(cbuf, (char *)(port->identify+23), 9);
1416 dev_info(&port->dd->pdev->dev,
1417 "Firmware Ver.: %s\n", cbuf);
1419 strlcpy(cbuf, (char *)(port->identify+27), 41);
1420 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1422 if (mtip_hw_get_capacity(port->dd, §ors))
1423 dev_info(&port->dd->pdev->dev,
1424 "Capacity: %llu sectors (%llu MB)\n",
1426 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1428 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
1429 switch (revid & 0xFF) {
1431 strlcpy(cbuf, "A0", 3);
1434 strlcpy(cbuf, "A2", 3);
1437 strlcpy(cbuf, "?", 2);
1440 dev_info(&port->dd->pdev->dev,
1441 "Card Type: %s\n", cbuf);
1445 * Map the commands scatter list into the command table.
1447 * @command Pointer to the command.
1448 * @nents Number of scatter list entries.
1453 static inline void fill_command_sg(struct driver_data *dd,
1454 struct mtip_cmd *command,
1458 unsigned int dma_len;
1459 struct mtip_cmd_sg *command_sg;
1460 struct scatterlist *sg = command->sg;
1462 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1464 for (n = 0; n < nents; n++) {
1465 dma_len = sg_dma_len(sg);
1466 if (dma_len > 0x400000)
1467 dev_err(&dd->pdev->dev,
1468 "DMA segment length truncated\n");
1469 command_sg->info = __force_bit2int
1470 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1471 command_sg->dba = __force_bit2int
1472 cpu_to_le32(sg_dma_address(sg));
1473 command_sg->dba_upper = __force_bit2int
1474 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
1481 * @brief Execute a drive command.
1483 * return value 0 The command completed successfully.
1484 * return value -1 An error occurred while executing the command.
1486 static int exec_drive_task(struct mtip_port *port, u8 *command)
1488 struct host_to_dev_fis fis;
1489 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1491 /* Build the FIS. */
1492 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1495 fis.command = command[0];
1496 fis.features = command[1];
1497 fis.sect_count = command[2];
1498 fis.sector = command[3];
1499 fis.cyl_low = command[4];
1500 fis.cyl_hi = command[5];
1501 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1504 dbg_printk(MTIP_DRV_NAME "%s: User Command: cmd %x, feat %x, "
1505 "nsect %x, sect %x, lcyl %x, "
1506 "hcyl %x, sel %x\n",
1516 /* Execute the command. */
1517 if (mtip_exec_internal_command(port,
1524 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
1528 command[0] = reply->command; /* Status*/
1529 command[1] = reply->features; /* Error*/
1530 command[4] = reply->cyl_low;
1531 command[5] = reply->cyl_hi;
1533 dbg_printk(MTIP_DRV_NAME "%s: Completion Status: stat %x, "
1534 "err %x , cyl_lo %x cyl_hi %x\n",
1545 * @brief Execute a drive command.
1547 * @param port Pointer to the port data structure.
1548 * @param command Pointer to the user specified command parameters.
1549 * @param user_buffer Pointer to the user space buffer where read sector
1550 * data should be copied.
1552 * return value 0 The command completed successfully.
1553 * return value -EFAULT An error occurred while copying the completion
1554 * data to the user space buffer.
1555 * return value -1 An error occurred while executing the command.
1557 static int exec_drive_command(struct mtip_port *port, u8 *command,
1558 void __user *user_buffer)
1560 struct host_to_dev_fis fis;
1561 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1563 /* Build the FIS. */
1564 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1567 fis.command = command[0];
1568 fis.features = command[2];
1569 fis.sect_count = command[3];
1570 if (fis.command == ATA_CMD_SMART) {
1571 fis.sector = command[1];
1576 dbg_printk(MTIP_DRV_NAME
1577 "%s: User Command: cmd %x, sect %x, "
1578 "feat %x, sectcnt %x\n",
1585 memset(port->sector_buffer, 0x00, ATA_SECT_SIZE);
1587 /* Execute the command. */
1588 if (mtip_exec_internal_command(port,
1591 port->sector_buffer_dma,
1592 (command[3] != 0) ? ATA_SECT_SIZE : 0,
1595 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
1600 /* Collect the completion status. */
1601 command[0] = reply->command; /* Status*/
1602 command[1] = reply->features; /* Error*/
1603 command[2] = command[3];
1605 dbg_printk(MTIP_DRV_NAME
1606 "%s: Completion Status: stat %x, "
1613 if (user_buffer && command[3]) {
1614 if (copy_to_user(user_buffer,
1615 port->sector_buffer,
1616 ATA_SECT_SIZE * command[3])) {
1625 * Indicates whether a command has a single sector payload.
1627 * @command passed to the device to perform the certain event.
1628 * @features passed to the device to perform the certain event.
1631 * 1 command is one that always has a single sector payload,
1632 * regardless of the value in the Sector Count field.
1636 static unsigned int implicit_sector(unsigned char command,
1637 unsigned char features)
1639 unsigned int rv = 0;
1641 /* list of commands that have an implicit sector count of 1 */
1643 case ATA_CMD_SEC_SET_PASS:
1644 case ATA_CMD_SEC_UNLOCK:
1645 case ATA_CMD_SEC_ERASE_PREP:
1646 case ATA_CMD_SEC_ERASE_UNIT:
1647 case ATA_CMD_SEC_FREEZE_LOCK:
1648 case ATA_CMD_SEC_DISABLE_PASS:
1649 case ATA_CMD_PMP_READ:
1650 case ATA_CMD_PMP_WRITE:
1653 case ATA_CMD_SET_MAX:
1654 if (features == ATA_SET_MAX_UNLOCK)
1658 if ((features == ATA_SMART_READ_VALUES) ||
1659 (features == ATA_SMART_READ_THRESHOLDS))
1662 case ATA_CMD_CONF_OVERLAY:
1663 if ((features == ATA_DCO_IDENTIFY) ||
1664 (features == ATA_DCO_SET))
1672 * Executes a taskfile
1673 * See ide_taskfile_ioctl() for derivation
1675 static int exec_drive_taskfile(struct driver_data *dd,
1677 ide_task_request_t *req_task,
1680 struct host_to_dev_fis fis;
1681 struct host_to_dev_fis *reply;
1684 dma_addr_t outbuf_dma = 0;
1685 dma_addr_t inbuf_dma = 0;
1686 dma_addr_t dma_buffer = 0;
1688 unsigned int taskin = 0;
1689 unsigned int taskout = 0;
1691 unsigned int timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1692 unsigned int force_single_sector;
1693 unsigned int transfer_size;
1694 unsigned long task_file_data;
1695 int intotal = outtotal + req_task->out_size;
1697 taskout = req_task->out_size;
1698 taskin = req_task->in_size;
1699 /* 130560 = 512 * 0xFF*/
1700 if (taskin > 130560 || taskout > 130560) {
1706 outbuf = kzalloc(taskout, GFP_KERNEL);
1707 if (outbuf == NULL) {
1711 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
1715 outbuf_dma = pci_map_single(dd->pdev,
1719 if (outbuf_dma == 0) {
1723 dma_buffer = outbuf_dma;
1727 inbuf = kzalloc(taskin, GFP_KERNEL);
1728 if (inbuf == NULL) {
1733 if (copy_from_user(inbuf, buf + intotal, taskin)) {
1737 inbuf_dma = pci_map_single(dd->pdev,
1739 taskin, DMA_FROM_DEVICE);
1740 if (inbuf_dma == 0) {
1744 dma_buffer = inbuf_dma;
1747 /* only supports PIO and non-data commands from this ioctl. */
1748 switch (req_task->data_phase) {
1750 nsect = taskout / ATA_SECT_SIZE;
1751 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1754 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1756 case TASKFILE_NO_DATA:
1757 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
1764 /* Build the FIS. */
1765 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1769 fis.command = req_task->io_ports[7];
1770 fis.features = req_task->io_ports[1];
1771 fis.sect_count = req_task->io_ports[2];
1772 fis.lba_low = req_task->io_ports[3];
1773 fis.lba_mid = req_task->io_ports[4];
1774 fis.lba_hi = req_task->io_ports[5];
1775 /* Clear the dev bit*/
1776 fis.device = req_task->io_ports[6] & ~0x10;
1778 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
1779 req_task->in_flags.all =
1780 IDE_TASKFILE_STD_IN_FLAGS |
1781 (IDE_HOB_STD_IN_FLAGS << 8);
1782 fis.lba_low_ex = req_task->hob_ports[3];
1783 fis.lba_mid_ex = req_task->hob_ports[4];
1784 fis.lba_hi_ex = req_task->hob_ports[5];
1785 fis.features_ex = req_task->hob_ports[1];
1786 fis.sect_cnt_ex = req_task->hob_ports[2];
1789 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
1792 force_single_sector = implicit_sector(fis.command, fis.features);
1794 if ((taskin || taskout) && (!fis.sect_count)) {
1796 fis.sect_count = nsect;
1798 if (!force_single_sector) {
1799 dev_warn(&dd->pdev->dev,
1800 "data movement but "
1801 "sect_count is 0\n");
1808 dbg_printk(MTIP_DRV_NAME
1809 "taskfile: cmd %x, feat %x, nsect %x,"
1810 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
1820 switch (fis.command) {
1821 case ATA_CMD_DOWNLOAD_MICRO:
1822 /* Change timeout for Download Microcode to 60 seconds.*/
1825 case ATA_CMD_SEC_ERASE_UNIT:
1826 /* Change timeout for Security Erase Unit to 4 minutes.*/
1829 case ATA_CMD_STANDBYNOW1:
1830 /* Change timeout for standby immediate to 10 seconds.*/
1835 /* Change timeout for vendor unique command to 10 secs */
1839 /* Change timeout for vendor unique command to 10 secs */
1843 timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
1847 /* Determine the correct transfer size.*/
1848 if (force_single_sector)
1849 transfer_size = ATA_SECT_SIZE;
1851 transfer_size = ATA_SECT_SIZE * fis.sect_count;
1853 /* Execute the command.*/
1854 if (mtip_exec_internal_command(dd->port,
1866 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
1868 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
1869 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
1870 req_task->io_ports[7] = reply->control;
1872 reply = dd->port->rxfis + RX_FIS_D2H_REG;
1873 req_task->io_ports[7] = reply->command;
1876 /* reclaim the DMA buffers.*/
1878 pci_unmap_single(dd->pdev, inbuf_dma,
1879 taskin, DMA_FROM_DEVICE);
1881 pci_unmap_single(dd->pdev, outbuf_dma,
1882 taskout, DMA_TO_DEVICE);
1886 /* return the ATA registers to the caller.*/
1887 req_task->io_ports[1] = reply->features;
1888 req_task->io_ports[2] = reply->sect_count;
1889 req_task->io_ports[3] = reply->lba_low;
1890 req_task->io_ports[4] = reply->lba_mid;
1891 req_task->io_ports[5] = reply->lba_hi;
1892 req_task->io_ports[6] = reply->device;
1894 if (req_task->out_flags.all & 1) {
1896 req_task->hob_ports[3] = reply->lba_low_ex;
1897 req_task->hob_ports[4] = reply->lba_mid_ex;
1898 req_task->hob_ports[5] = reply->lba_hi_ex;
1899 req_task->hob_ports[1] = reply->features_ex;
1900 req_task->hob_ports[2] = reply->sect_cnt_ex;
1903 /* Com rest after secure erase or lowlevel format */
1904 if (((fis.command == ATA_CMD_SEC_ERASE_UNIT) ||
1905 ((fis.command == 0xFC) &&
1906 (fis.features == 0x27 || fis.features == 0x72 ||
1907 fis.features == 0x62 || fis.features == 0x26))) &&
1908 !(reply->command & 1)) {
1909 mtip_restart_port(dd->port);
1912 dbg_printk(MTIP_DRV_NAME
1913 "%s: Completion: stat %x,"
1914 "err %x, sect_cnt %x, lbalo %x,"
1915 "lbamid %x, lbahi %x, dev %x\n",
1917 req_task->io_ports[7],
1918 req_task->io_ports[1],
1919 req_task->io_ports[2],
1920 req_task->io_ports[3],
1921 req_task->io_ports[4],
1922 req_task->io_ports[5],
1923 req_task->io_ports[6]);
1926 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
1932 if (copy_to_user(buf + intotal, inbuf, taskin)) {
1939 pci_unmap_single(dd->pdev, inbuf_dma,
1940 taskin, DMA_FROM_DEVICE);
1942 pci_unmap_single(dd->pdev, outbuf_dma,
1943 taskout, DMA_TO_DEVICE);
1951 * Handle IOCTL calls from the Block Layer.
1953 * This function is called by the Block Layer when it receives an IOCTL
1954 * command that it does not understand. If the IOCTL command is not supported
1955 * this function returns -ENOTTY.
1957 * @dd Pointer to the driver data structure.
1958 * @cmd IOCTL command passed from the Block Layer.
1959 * @arg IOCTL argument passed from the Block Layer.
1962 * 0 The IOCTL completed successfully.
1963 * -ENOTTY The specified command is not supported.
1964 * -EFAULT An error occurred copying data to a user space buffer.
1965 * -EIO An error occurred while executing the command.
1967 static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
1971 case HDIO_GET_IDENTITY:
1972 if (mtip_get_identify(dd->port, (void __user *) arg) < 0) {
1973 dev_warn(&dd->pdev->dev,
1974 "Unable to read identity\n");
1979 case HDIO_DRIVE_CMD:
1981 u8 drive_command[4];
1983 /* Copy the user command info to our buffer. */
1984 if (copy_from_user(drive_command,
1985 (void __user *) arg,
1986 sizeof(drive_command)))
1989 /* Execute the drive command. */
1990 if (exec_drive_command(dd->port,
1992 (void __user *) (arg+4)))
1995 /* Copy the status back to the users buffer. */
1996 if (copy_to_user((void __user *) arg,
1998 sizeof(drive_command)))
2003 case HDIO_DRIVE_TASK:
2005 u8 drive_command[7];
2007 /* Copy the user command info to our buffer. */
2008 if (copy_from_user(drive_command,
2009 (void __user *) arg,
2010 sizeof(drive_command)))
2013 /* Execute the drive command. */
2014 if (exec_drive_task(dd->port, drive_command))
2017 /* Copy the status back to the users buffer. */
2018 if (copy_to_user((void __user *) arg,
2020 sizeof(drive_command)))
2025 case HDIO_DRIVE_TASKFILE: {
2026 ide_task_request_t req_task;
2029 if (copy_from_user(&req_task, (void __user *) arg,
2033 outtotal = sizeof(req_task);
2035 ret = exec_drive_taskfile(dd, (void __user *) arg,
2036 &req_task, outtotal);
2038 if (copy_to_user((void __user *) arg, &req_task,
2052 * Submit an IO to the hw
2054 * This function is called by the block layer to issue an io
2055 * to the device. Upon completion, the callback function will
2056 * be called with the data parameter passed as the callback data.
2058 * @dd Pointer to the driver data structure.
2059 * @start First sector to read.
2060 * @nsect Number of sectors to read.
2061 * @nents Number of entries in scatter list for the read command.
2062 * @tag The tag of this read command.
2063 * @callback Pointer to the function that should be called
2064 * when the read completes.
2065 * @data Callback data passed to the callback function
2066 * when the read completes.
2067 * @barrier If non-zero, this command must be completed before
2068 * issuing any other commands.
2069 * @dir Direction (read or write)
2074 static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
2075 int nsect, int nents, int tag, void *callback,
2076 void *data, int barrier, int dir)
2078 struct host_to_dev_fis *fis;
2079 struct mtip_port *port = dd->port;
2080 struct mtip_cmd *command = &port->commands[tag];
2082 /* Map the scatter list for DMA access */
2084 nents = dma_map_sg(&dd->pdev->dev, command->sg,
2085 nents, DMA_FROM_DEVICE);
2087 nents = dma_map_sg(&dd->pdev->dev, command->sg,
2088 nents, DMA_TO_DEVICE);
2090 command->scatter_ents = nents;
2093 * The number of retries for this command before it is
2094 * reported as a failure to the upper layers.
2096 command->retries = MTIP_MAX_RETRIES;
2099 fis = command->command;
2103 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
2104 *((unsigned int *) &fis->lba_low) = (start & 0xFFFFFF);
2105 *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xFFFFFF);
2106 fis->device = 1 << 6;
2108 fis->device |= FUA_BIT;
2109 fis->features = nsect & 0xFF;
2110 fis->features_ex = (nsect >> 8) & 0xFF;
2111 fis->sect_count = ((tag << 3) | (tag >> 5));
2112 fis->sect_cnt_ex = 0;
2116 fill_command_sg(dd, command, nents);
2118 /* Populate the command header */
2119 command->command_header->opts =
2120 __force_bit2int cpu_to_le32(
2121 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
2122 command->command_header->byte_count = 0;
2125 * Set the completion function and data for the command
2126 * within this layer.
2128 command->comp_data = dd;
2129 command->comp_func = mtip_async_complete;
2130 command->direction = (dir == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE);
2133 * Set the completion function and data for the command passed
2134 * from the upper layer.
2136 command->async_data = data;
2137 command->async_callback = callback;
2140 * To prevent this command from being issued
2141 * if an internal command is in progress or error handling is active.
2143 if (unlikely(test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) ||
2144 test_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags))) {
2145 set_bit(tag, port->cmds_to_issue);
2146 set_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags);
2150 /* Issue the command to the hardware */
2151 mtip_issue_ncq_command(port, tag);
2153 /* Set the command's timeout value.*/
2154 port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
2155 MTIP_NCQ_COMMAND_TIMEOUT_MS);
2159 * Release a command slot.
2161 * @dd Pointer to the driver data structure.
2167 static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
2169 release_slot(dd->port, tag);
2173 * Obtain a command slot and return its associated scatter list.
2175 * @dd Pointer to the driver data structure.
2176 * @tag Pointer to an int that will receive the allocated command
2180 * Pointer to the scatter list for the allocated command slot
2181 * or NULL if no command slots are available.
2183 static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2187 * It is possible that, even with this semaphore, a thread
2188 * may think that no command slots are available. Therefore, we
2189 * need to make an attempt to get_slot().
2191 down(&dd->port->cmd_slot);
2192 *tag = get_slot(dd->port);
2194 if (unlikely(*tag < 0))
2197 return dd->port->commands[*tag].sg;
2201 * Sysfs register/status dump.
2203 * @dev Pointer to the device structure, passed by the kernrel.
2204 * @attr Pointer to the device_attribute structure passed by the kernel.
2205 * @buf Pointer to the char buffer that will receive the stats info.
2208 * The size, in bytes, of the data copied into buf.
2210 static ssize_t hw_show_registers(struct device *dev,
2211 struct device_attribute *attr,
2214 u32 group_allocated;
2215 struct driver_data *dd = dev_to_disk(dev)->private_data;
2219 size += sprintf(&buf[size], "%s:\ns_active:\n", __func__);
2221 for (n = 0; n < dd->slot_groups; n++)
2222 size += sprintf(&buf[size], "0x%08x\n",
2223 readl(dd->port->s_active[n]));
2225 size += sprintf(&buf[size], "Command Issue:\n");
2227 for (n = 0; n < dd->slot_groups; n++)
2228 size += sprintf(&buf[size], "0x%08x\n",
2229 readl(dd->port->cmd_issue[n]));
2231 size += sprintf(&buf[size], "Allocated:\n");
2233 for (n = 0; n < dd->slot_groups; n++) {
2234 if (sizeof(long) > sizeof(u32))
2236 dd->port->allocated[n/2] >> (32*(n&1));
2238 group_allocated = dd->port->allocated[n];
2239 size += sprintf(&buf[size], "0x%08x\n",
2243 size += sprintf(&buf[size], "completed:\n");
2245 for (n = 0; n < dd->slot_groups; n++)
2246 size += sprintf(&buf[size], "0x%08x\n",
2247 readl(dd->port->completed[n]));
2249 size += sprintf(&buf[size], "PORT_IRQ_STAT 0x%08x\n",
2250 readl(dd->port->mmio + PORT_IRQ_STAT));
2251 size += sprintf(&buf[size], "HOST_IRQ_STAT 0x%08x\n",
2252 readl(dd->mmio + HOST_IRQ_STAT));
2256 static DEVICE_ATTR(registers, S_IRUGO, hw_show_registers, NULL);
2259 * Create the sysfs related attributes.
2261 * @dd Pointer to the driver data structure.
2262 * @kobj Pointer to the kobj for the block device.
2265 * 0 Operation completed successfully.
2266 * -EINVAL Invalid parameter.
2268 static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
2273 if (sysfs_create_file(kobj, &dev_attr_registers.attr))
2274 dev_warn(&dd->pdev->dev,
2275 "Error creating registers sysfs entry\n");
2280 * Remove the sysfs related attributes.
2282 * @dd Pointer to the driver data structure.
2283 * @kobj Pointer to the kobj for the block device.
2286 * 0 Operation completed successfully.
2287 * -EINVAL Invalid parameter.
2289 static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
2294 sysfs_remove_file(kobj, &dev_attr_registers.attr);
2300 * Perform any init/resume time hardware setup
2302 * @dd Pointer to the driver data structure.
2307 static inline void hba_setup(struct driver_data *dd)
2310 hwdata = readl(dd->mmio + HOST_HSORG);
2312 /* interrupt bug workaround: use only 1 IS bit.*/
2314 HSORG_DISABLE_SLOTGRP_INTR |
2315 HSORG_DISABLE_SLOTGRP_PXIS,
2316 dd->mmio + HOST_HSORG);
2320 * Detect the details of the product, and store anything needed
2321 * into the driver data structure. This includes product type and
2322 * version and number of slot groups.
2324 * @dd Pointer to the driver data structure.
2329 static void mtip_detect_product(struct driver_data *dd)
2332 unsigned int rev, slotgroups;
2335 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2337 * [15:8] hardware/software interface rev#
2338 * [ 3] asic-style interface
2339 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2341 hwdata = readl(dd->mmio + HOST_HSORG);
2343 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2344 dd->slot_groups = 1;
2347 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2348 rev = (hwdata & HSORG_HWREV) >> 8;
2349 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2350 dev_info(&dd->pdev->dev,
2351 "ASIC-FPGA design, HS rev 0x%x, "
2352 "%i slot groups [%i slots]\n",
2357 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2358 dev_warn(&dd->pdev->dev,
2359 "Warning: driver only supports "
2360 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2361 slotgroups = MTIP_MAX_SLOT_GROUPS;
2363 dd->slot_groups = slotgroups;
2367 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2371 * Blocking wait for FTL rebuild to complete
2373 * @dd Pointer to the DRIVER_DATA structure.
2376 * 0 FTL rebuild completed successfully
2377 * -EFAULT FTL rebuild error/timeout/interruption
2379 static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2381 unsigned long timeout, cnt = 0, start;
2383 dev_warn(&dd->pdev->dev,
2384 "FTL rebuild in progress. Polling for completion.\n");
2387 dd->ftlrebuildflag = 1;
2388 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2391 if (mtip_check_surprise_removal(dd->pdev))
2394 if (mtip_get_identify(dd->port, NULL) < 0)
2397 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2398 MTIP_FTL_REBUILD_MAGIC) {
2400 /* Print message every 3 minutes */
2402 dev_warn(&dd->pdev->dev,
2403 "FTL rebuild in progress (%d secs).\n",
2404 jiffies_to_msecs(jiffies - start) / 1000);
2408 dev_warn(&dd->pdev->dev,
2409 "FTL rebuild complete (%d secs).\n",
2410 jiffies_to_msecs(jiffies - start) / 1000);
2411 dd->ftlrebuildflag = 0;
2415 } while (time_before(jiffies, timeout));
2417 /* Check for timeout */
2418 if (dd->ftlrebuildflag) {
2419 dev_err(&dd->pdev->dev,
2420 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2421 jiffies_to_msecs(jiffies - start) / 1000);
2429 * service thread to issue queued commands
2431 * @data Pointer to the driver data structure.
2437 static int mtip_service_thread(void *data)
2439 struct driver_data *dd = (struct driver_data *)data;
2440 unsigned long slot, slot_start, slot_wrap;
2441 unsigned int num_cmd_slots = dd->slot_groups * 32;
2442 struct mtip_port *port = dd->port;
2446 * the condition is to check neither an internal command is
2447 * is in progress nor error handling is active
2449 wait_event_interruptible(port->svc_wait, (port->flags) &&
2450 !test_bit(MTIP_FLAG_IC_ACTIVE_BIT, &port->flags) &&
2451 !test_bit(MTIP_FLAG_EH_ACTIVE_BIT, &port->flags));
2453 if (kthread_should_stop())
2456 if (test_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags)) {
2457 set_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags);
2459 /* used to restrict the loop to one iteration */
2460 slot_start = num_cmd_slots;
2463 slot = find_next_bit(port->cmds_to_issue,
2464 num_cmd_slots, slot);
2465 if (slot_wrap == 1) {
2466 if ((slot_start >= slot) ||
2467 (slot >= num_cmd_slots))
2470 if (unlikely(slot_start == num_cmd_slots))
2473 if (unlikely(slot == num_cmd_slots)) {
2479 /* Issue the command to the hardware */
2480 mtip_issue_ncq_command(port, slot);
2482 /* Set the command's timeout value.*/
2483 port->commands[slot].comp_time = jiffies +
2484 msecs_to_jiffies(MTIP_NCQ_COMMAND_TIMEOUT_MS);
2486 clear_bit(slot, port->cmds_to_issue);
2489 clear_bit(MTIP_FLAG_ISSUE_CMDS_BIT, &port->flags);
2490 clear_bit(MTIP_FLAG_SVC_THD_ACTIVE_BIT, &port->flags);
2497 * Called once for each card.
2499 * @dd Pointer to the driver data structure.
2502 * 0 on success, else an error code.
2504 static int mtip_hw_init(struct driver_data *dd)
2508 unsigned int num_command_slots;
2510 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
2512 mtip_detect_product(dd);
2513 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
2517 num_command_slots = dd->slot_groups * 32;
2521 tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
2523 dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
2525 dev_err(&dd->pdev->dev,
2526 "Memory allocation: port structure\n");
2530 /* Counting semaphore to track command slot usage */
2531 sema_init(&dd->port->cmd_slot, num_command_slots - 1);
2533 /* Spinlock to prevent concurrent issue */
2534 spin_lock_init(&dd->port->cmd_issue_lock);
2536 /* Set the port mmio base address. */
2537 dd->port->mmio = dd->mmio + PORT_OFFSET;
2540 /* Allocate memory for the command list. */
2541 dd->port->command_list =
2542 dmam_alloc_coherent(&dd->pdev->dev,
2543 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2544 &dd->port->command_list_dma,
2546 if (!dd->port->command_list) {
2547 dev_err(&dd->pdev->dev,
2548 "Memory allocation: command list\n");
2553 /* Clear the memory we have allocated. */
2554 memset(dd->port->command_list,
2556 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2));
2558 /* Setup the addresse of the RX FIS. */
2559 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
2560 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
2562 /* Setup the address of the command tables. */
2563 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
2564 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
2566 /* Setup the address of the identify data. */
2567 dd->port->identify = dd->port->command_table +
2569 dd->port->identify_dma = dd->port->command_tbl_dma +
2572 /* Setup the address of the sector buffer. */
2573 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
2574 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
2576 /* Point the command headers at the command tables. */
2577 for (i = 0; i < num_command_slots; i++) {
2578 dd->port->commands[i].command_header =
2579 dd->port->command_list +
2580 (sizeof(struct mtip_cmd_hdr) * i);
2581 dd->port->commands[i].command_header_dma =
2582 dd->port->command_list_dma +
2583 (sizeof(struct mtip_cmd_hdr) * i);
2585 dd->port->commands[i].command =
2586 dd->port->command_table + (HW_CMD_TBL_SZ * i);
2587 dd->port->commands[i].command_dma =
2588 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
2590 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
2591 dd->port->commands[i].command_header->ctbau =
2592 __force_bit2int cpu_to_le32(
2593 (dd->port->commands[i].command_dma >> 16) >> 16);
2594 dd->port->commands[i].command_header->ctba =
2595 __force_bit2int cpu_to_le32(
2596 dd->port->commands[i].command_dma & 0xFFFFFFFF);
2599 * If this is not done, a bug is reported by the stock
2600 * FC11 i386. Due to the fact that it has lots of kernel
2601 * debugging enabled.
2603 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
2605 /* Mark all commands as currently inactive.*/
2606 atomic_set(&dd->port->commands[i].active, 0);
2609 /* Setup the pointers to the extended s_active and CI registers. */
2610 for (i = 0; i < dd->slot_groups; i++) {
2611 dd->port->s_active[i] =
2612 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
2613 dd->port->cmd_issue[i] =
2614 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
2615 dd->port->completed[i] =
2616 dd->port->mmio + i*0x80 + PORT_SDBV;
2619 /* Reset the HBA. */
2620 if (mtip_hba_reset(dd) < 0) {
2621 dev_err(&dd->pdev->dev,
2622 "Card did not reset within timeout\n");
2627 mtip_init_port(dd->port);
2628 mtip_start_port(dd->port);
2630 /* Setup the ISR and enable interrupts. */
2631 rv = devm_request_irq(&dd->pdev->dev,
2635 dev_driver_string(&dd->pdev->dev),
2639 dev_err(&dd->pdev->dev,
2640 "Unable to allocate IRQ %d\n", dd->pdev->irq);
2644 /* Enable interrupts on the HBA. */
2645 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2646 dd->mmio + HOST_CTL);
2648 init_timer(&dd->port->cmd_timer);
2649 init_waitqueue_head(&dd->port->svc_wait);
2651 dd->port->cmd_timer.data = (unsigned long int) dd->port;
2652 dd->port->cmd_timer.function = mtip_timeout_function;
2653 mod_timer(&dd->port->cmd_timer,
2654 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
2656 if (mtip_get_identify(dd->port, NULL) < 0) {
2660 mtip_dump_identify(dd->port);
2662 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2663 MTIP_FTL_REBUILD_MAGIC) {
2664 return mtip_ftl_rebuild_poll(dd);
2669 del_timer_sync(&dd->port->cmd_timer);
2671 /* Disable interrupts on the HBA. */
2672 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2673 dd->mmio + HOST_CTL);
2675 /*Release the IRQ. */
2676 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
2679 mtip_deinit_port(dd->port);
2681 /* Free the command/command header memory. */
2682 dmam_free_coherent(&dd->pdev->dev,
2683 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2684 dd->port->command_list,
2685 dd->port->command_list_dma);
2687 /* Free the memory allocated for the for structure. */
2694 * Called to deinitialize an interface.
2696 * @dd Pointer to the driver data structure.
2701 static int mtip_hw_exit(struct driver_data *dd)
2704 * Send standby immediate (E0h) to the drive so that it
2707 if (atomic_read(&dd->drv_cleanup_done) != true) {
2709 mtip_standby_immediate(dd->port);
2711 /* de-initialize the port. */
2712 mtip_deinit_port(dd->port);
2714 /* Disable interrupts on the HBA. */
2715 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2716 dd->mmio + HOST_CTL);
2719 del_timer_sync(&dd->port->cmd_timer);
2721 /* Release the IRQ. */
2722 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
2724 /* Stop the bottom half tasklet. */
2725 tasklet_kill(&dd->tasklet);
2727 /* Free the command/command header memory. */
2728 dmam_free_coherent(&dd->pdev->dev,
2729 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 2),
2730 dd->port->command_list,
2731 dd->port->command_list_dma);
2732 /* Free the memory allocated for the for structure. */
2739 * Issue a Standby Immediate command to the device.
2741 * This function is called by the Block Layer just before the
2742 * system powers off during a shutdown.
2744 * @dd Pointer to the driver data structure.
2749 static int mtip_hw_shutdown(struct driver_data *dd)
2752 * Send standby immediate (E0h) to the drive so that it
2755 mtip_standby_immediate(dd->port);
2763 * This function is called by the Block Layer just before the
2764 * system hibernates.
2766 * @dd Pointer to the driver data structure.
2769 * 0 Suspend was successful
2770 * -EFAULT Suspend was not successful
2772 static int mtip_hw_suspend(struct driver_data *dd)
2775 * Send standby immediate (E0h) to the drive
2776 * so that it saves its state.
2778 if (mtip_standby_immediate(dd->port) != 0) {
2779 dev_err(&dd->pdev->dev,
2780 "Failed standby-immediate command\n");
2784 /* Disable interrupts on the HBA.*/
2785 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
2786 dd->mmio + HOST_CTL);
2787 mtip_deinit_port(dd->port);
2795 * This function is called by the Block Layer as the
2798 * @dd Pointer to the driver data structure.
2801 * 0 Resume was successful
2802 * -EFAULT Resume was not successful
2804 static int mtip_hw_resume(struct driver_data *dd)
2806 /* Perform any needed hardware setup steps */
2810 if (mtip_hba_reset(dd) != 0) {
2811 dev_err(&dd->pdev->dev,
2812 "Unable to reset the HBA\n");
2817 * Enable the port, DMA engine, and FIS reception specific
2818 * h/w in controller.
2820 mtip_init_port(dd->port);
2821 mtip_start_port(dd->port);
2823 /* Enable interrupts on the HBA.*/
2824 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
2825 dd->mmio + HOST_CTL);
2831 * Helper function for reusing disk name
2832 * upon hot insertion.
2834 static int rssd_disk_name_format(char *prefix,
2839 const int base = 'z' - 'a' + 1;
2840 char *begin = buf + strlen(prefix);
2841 char *end = buf + buflen;
2851 *--p = 'a' + (index % unit);
2852 index = (index / unit) - 1;
2853 } while (index >= 0);
2855 memmove(begin, p, end - p);
2856 memcpy(buf, prefix, strlen(prefix));
2862 * Block layer IOCTL handler.
2864 * @dev Pointer to the block_device structure.
2866 * @cmd IOCTL command passed from the user application.
2867 * @arg Argument passed from the user application.
2870 * 0 IOCTL completed successfully.
2871 * -ENOTTY IOCTL not supported or invalid driver data
2872 * structure pointer.
2874 static int mtip_block_ioctl(struct block_device *dev,
2879 struct driver_data *dd = dev->bd_disk->private_data;
2881 if (!capable(CAP_SYS_ADMIN))
2891 return mtip_hw_ioctl(dd, cmd, arg);
2895 #ifdef CONFIG_COMPAT
2897 * Block layer compat IOCTL handler.
2899 * @dev Pointer to the block_device structure.
2901 * @cmd IOCTL command passed from the user application.
2902 * @arg Argument passed from the user application.
2905 * 0 IOCTL completed successfully.
2906 * -ENOTTY IOCTL not supported or invalid driver data
2907 * structure pointer.
2909 static int mtip_block_compat_ioctl(struct block_device *dev,
2914 struct driver_data *dd = dev->bd_disk->private_data;
2916 if (!capable(CAP_SYS_ADMIN))
2925 case HDIO_DRIVE_TASKFILE: {
2926 struct mtip_compat_ide_task_request_s __user *compat_req_task;
2927 ide_task_request_t req_task;
2928 int compat_tasksize, outtotal, ret;
2931 sizeof(struct mtip_compat_ide_task_request_s);
2934 (struct mtip_compat_ide_task_request_s __user *) arg;
2936 if (copy_from_user(&req_task, (void __user *) arg,
2937 compat_tasksize - (2 * sizeof(compat_long_t))))
2940 if (get_user(req_task.out_size, &compat_req_task->out_size))
2943 if (get_user(req_task.in_size, &compat_req_task->in_size))
2946 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
2948 ret = exec_drive_taskfile(dd, (void __user *) arg,
2949 &req_task, outtotal);
2951 if (copy_to_user((void __user *) arg, &req_task,
2953 (2 * sizeof(compat_long_t))))
2956 if (put_user(req_task.out_size, &compat_req_task->out_size))
2959 if (put_user(req_task.in_size, &compat_req_task->in_size))
2965 return mtip_hw_ioctl(dd, cmd, arg);
2971 * Obtain the geometry of the device.
2973 * You may think that this function is obsolete, but some applications,
2974 * fdisk for example still used CHS values. This function describes the
2975 * device as having 224 heads and 56 sectors per cylinder. These values are
2976 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
2977 * partition is described in terms of a start and end cylinder this means
2978 * that each partition is also 4KB aligned. Non-aligned partitions adversely
2979 * affects performance.
2981 * @dev Pointer to the block_device strucutre.
2982 * @geo Pointer to a hd_geometry structure.
2985 * 0 Operation completed successfully.
2986 * -ENOTTY An error occurred while reading the drive capacity.
2988 static int mtip_block_getgeo(struct block_device *dev,
2989 struct hd_geometry *geo)
2991 struct driver_data *dd = dev->bd_disk->private_data;
2997 if (!(mtip_hw_get_capacity(dd, &capacity))) {
2998 dev_warn(&dd->pdev->dev,
2999 "Could not get drive capacity.\n");
3005 sector_div(capacity, (geo->heads * geo->sectors));
3006 geo->cylinders = capacity;
3011 * Block device operation function.
3013 * This structure contains pointers to the functions required by the block
3016 static const struct block_device_operations mtip_block_ops = {
3017 .ioctl = mtip_block_ioctl,
3018 #ifdef CONFIG_COMPAT
3019 .compat_ioctl = mtip_block_compat_ioctl,
3021 .getgeo = mtip_block_getgeo,
3022 .owner = THIS_MODULE
3026 * Block layer make request function.
3028 * This function is called by the kernel to process a BIO for
3031 * @queue Pointer to the request queue. Unused other than to obtain
3032 * the driver data structure.
3033 * @bio Pointer to the BIO.
3036 static void mtip_make_request(struct request_queue *queue, struct bio *bio)
3038 struct driver_data *dd = queue->queuedata;
3039 struct scatterlist *sg;
3040 struct bio_vec *bvec;
3044 if (unlikely(!bio_has_data(bio))) {
3045 blk_queue_flush(queue, 0);
3050 sg = mtip_hw_get_scatterlist(dd, &tag);
3051 if (likely(sg != NULL)) {
3052 blk_queue_bounce(queue, &bio);
3054 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3055 dev_warn(&dd->pdev->dev,
3056 "Maximum number of SGL entries exceeded");
3058 mtip_hw_release_scatterlist(dd, tag);
3062 /* Create the scatter list for this bio. */
3063 bio_for_each_segment(bvec, bio, nents) {
3064 sg_set_page(&sg[nents],
3070 /* Issue the read/write. */
3071 mtip_hw_submit_io(dd,
3078 bio->bi_rw & REQ_FUA,
3085 * Block layer initialization function.
3087 * This function is called once by the PCI layer for each P320
3088 * device that is connected to the system.
3090 * @dd Pointer to the driver data structure.
3093 * 0 on success else an error code.
3095 static int mtip_block_initialize(struct driver_data *dd)
3099 unsigned int index = 0;
3100 struct kobject *kobj;
3101 unsigned char thd_name[16];
3103 /* Initialize the protocol layer. */
3104 rv = mtip_hw_init(dd);
3106 dev_err(&dd->pdev->dev,
3107 "Protocol layer initialization failed\n");
3109 goto protocol_init_error;
3112 /* Allocate the request queue. */
3113 dd->queue = blk_alloc_queue(GFP_KERNEL);
3114 if (dd->queue == NULL) {
3115 dev_err(&dd->pdev->dev,
3116 "Unable to allocate request queue\n");
3118 goto block_queue_alloc_init_error;
3121 /* Attach our request function to the request queue. */
3122 blk_queue_make_request(dd->queue, mtip_make_request);
3124 /* Set device limits. */
3125 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3126 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3127 blk_queue_physical_block_size(dd->queue, 4096);
3128 blk_queue_io_min(dd->queue, 4096);
3129 blk_queue_flush(dd->queue, 0);
3131 dd->disk = alloc_disk(MTIP_MAX_MINORS);
3132 if (dd->disk == NULL) {
3133 dev_err(&dd->pdev->dev,
3134 "Unable to allocate gendisk structure\n");
3136 goto alloc_disk_error;
3139 /* Generate the disk name, implemented same as in sd.c */
3141 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3144 spin_lock(&rssd_index_lock);
3145 rv = ida_get_new(&rssd_index_ida, &index);
3146 spin_unlock(&rssd_index_lock);
3147 } while (rv == -EAGAIN);
3152 rv = rssd_disk_name_format("rssd",
3154 dd->disk->disk_name,
3157 goto disk_index_error;
3159 dd->disk->driverfs_dev = &dd->pdev->dev;
3160 dd->disk->major = dd->major;
3161 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3162 dd->disk->fops = &mtip_block_ops;
3163 dd->disk->queue = dd->queue;
3164 dd->disk->private_data = dd;
3165 dd->queue->queuedata = dd;
3168 /* Set the capacity of the device in 512 byte sectors. */
3169 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3170 dev_warn(&dd->pdev->dev,
3171 "Could not read drive capacity\n");
3173 goto read_capacity_error;
3175 set_capacity(dd->disk, capacity);
3177 /* Enable the block device and add it to /dev */
3181 * Now that the disk is active, initialize any sysfs attributes
3182 * managed by the protocol layer.
3184 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3186 mtip_hw_sysfs_init(dd, kobj);
3190 sprintf(thd_name, "mtip_svc_thd_%02d", index);
3192 dd->mtip_svc_handler = kthread_run(mtip_service_thread,
3195 if (IS_ERR(dd->mtip_svc_handler)) {
3196 printk(KERN_ERR "mtip32xx: service thread failed to start\n");
3197 dd->mtip_svc_handler = NULL;
3199 goto read_capacity_error;
3204 read_capacity_error:
3206 * Delete our gendisk structure. This also removes the device