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>
39 #include <linux/export.h>
40 #include <linux/debugfs.h>
43 #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
44 #define HW_CMD_TBL_SZ (AHCI_CMD_TBL_HDR_SZ + (MTIP_MAX_SG * 16))
45 #define HW_CMD_TBL_AR_SZ (HW_CMD_TBL_SZ * MTIP_MAX_COMMAND_SLOTS)
46 #define HW_PORT_PRIV_DMA_SZ \
47 (HW_CMD_SLOT_SZ + HW_CMD_TBL_AR_SZ + AHCI_RX_FIS_SZ)
49 #define HOST_CAP_NZDMA (1 << 19)
50 #define HOST_HSORG 0xFC
51 #define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
52 #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
53 #define HSORG_HWREV 0xFF00
54 #define HSORG_STYLE 0x8
55 #define HSORG_SLOTGROUPS 0x7
57 #define PORT_COMMAND_ISSUE 0x38
58 #define PORT_SDBV 0x7C
60 #define PORT_OFFSET 0x100
61 #define PORT_MEM_SIZE 0x80
63 #define PORT_IRQ_ERR \
64 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
65 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
66 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
68 #define PORT_IRQ_LEGACY \
69 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
70 #define PORT_IRQ_HANDLED \
71 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
72 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
73 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
74 #define DEF_PORT_IRQ \
75 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
78 #define MTIP_PRODUCT_UNKNOWN 0x00
79 #define MTIP_PRODUCT_ASICFPGA 0x11
81 /* Device instance number, incremented each time a device is probed. */
85 * Global variable used to hold the major block device number
86 * allocated in mtip_init().
88 static int mtip_major;
89 static struct dentry *dfs_parent;
91 static DEFINE_SPINLOCK(rssd_index_lock);
92 static DEFINE_IDA(rssd_index_ida);
94 static int mtip_block_initialize(struct driver_data *dd);
97 struct mtip_compat_ide_task_request_s {
100 ide_reg_valid_t out_flags;
101 ide_reg_valid_t in_flags;
104 compat_ulong_t out_size;
105 compat_ulong_t in_size;
110 * This function check_for_surprise_removal is called
111 * while card is removed from the system and it will
112 * read the vendor id from the configration space
114 * @pdev Pointer to the pci_dev structure.
117 * true if device removed, else false
119 static bool mtip_check_surprise_removal(struct pci_dev *pdev)
123 /* Read the vendorID from the configuration space */
124 pci_read_config_word(pdev, 0x00, &vendor_id);
125 if (vendor_id == 0xFFFF)
126 return true; /* device removed */
128 return false; /* device present */
132 * This function is called for clean the pending command in the
133 * command slot during the surprise removal of device and return
134 * error to the upper layer.
136 * @dd Pointer to the DRIVER_DATA structure.
141 static void mtip_command_cleanup(struct driver_data *dd)
143 int group = 0, commandslot = 0, commandindex = 0;
144 struct mtip_cmd *command;
145 struct mtip_port *port = dd->port;
146 static int in_progress;
153 for (group = 0; group < 4; group++) {
154 for (commandslot = 0; commandslot < 32; commandslot++) {
155 if (!(port->allocated[group] & (1 << commandslot)))
158 commandindex = group << 5 | commandslot;
159 command = &port->commands[commandindex];
161 if (atomic_read(&command->active)
162 && (command->async_callback)) {
163 command->async_callback(command->async_data,
165 command->async_callback = NULL;
166 command->async_data = NULL;
169 dma_unmap_sg(&port->dd->pdev->dev,
171 command->scatter_ents,
178 set_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag);
183 * Obtain an empty command slot.
185 * This function needs to be reentrant since it could be called
186 * at the same time on multiple CPUs. The allocation of the
187 * command slot must be atomic.
189 * @port Pointer to the port data structure.
192 * >= 0 Index of command slot obtained.
193 * -1 No command slots available.
195 static int get_slot(struct mtip_port *port)
198 unsigned int num_command_slots = port->dd->slot_groups * 32;
201 * Try 10 times, because there is a small race here.
202 * that's ok, because it's still cheaper than a lock.
204 * Race: Since this section is not protected by lock, same bit
205 * could be chosen by different process contexts running in
206 * different processor. So instead of costly lock, we are going
209 for (i = 0; i < 10; i++) {
210 slot = find_next_zero_bit(port->allocated,
211 num_command_slots, 1);
212 if ((slot < num_command_slots) &&
213 (!test_and_set_bit(slot, port->allocated)))
216 dev_warn(&port->dd->pdev->dev, "Failed to get a tag.\n");
218 if (mtip_check_surprise_removal(port->dd->pdev)) {
219 /* Device not present, clean outstanding commands */
220 mtip_command_cleanup(port->dd);
226 * Release a command slot.
228 * @port Pointer to the port data structure.
229 * @tag Tag of command to release
234 static inline void release_slot(struct mtip_port *port, int tag)
236 smp_mb__before_clear_bit();
237 clear_bit(tag, port->allocated);
238 smp_mb__after_clear_bit();
242 * Reset the HBA (without sleeping)
244 * Just like hba_reset, except does not call sleep, so can be
245 * run from interrupt/tasklet context.
247 * @dd Pointer to the driver data structure.
250 * 0 The reset was successful.
251 * -1 The HBA Reset bit did not clear.
253 static int hba_reset_nosleep(struct driver_data *dd)
255 unsigned long timeout;
257 /* Chip quirk: quiesce any chip function */
260 /* Set the reset bit */
261 writel(HOST_RESET, dd->mmio + HOST_CTL);
264 readl(dd->mmio + HOST_CTL);
267 * Wait 10ms then spin for up to 1 second
268 * waiting for reset acknowledgement
270 timeout = jiffies + msecs_to_jiffies(1000);
272 while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
273 && time_before(jiffies, timeout))
276 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
279 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
286 * Issue a command to the hardware.
288 * Set the appropriate bit in the s_active and Command Issue hardware
289 * registers, causing hardware command processing to begin.
291 * @port Pointer to the port structure.
292 * @tag The tag of the command to be issued.
297 static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
299 atomic_set(&port->commands[tag].active, 1);
301 spin_lock(&port->cmd_issue_lock);
303 writel((1 << MTIP_TAG_BIT(tag)),
304 port->s_active[MTIP_TAG_INDEX(tag)]);
305 writel((1 << MTIP_TAG_BIT(tag)),
306 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
308 spin_unlock(&port->cmd_issue_lock);
310 /* Set the command's timeout value.*/
311 port->commands[tag].comp_time = jiffies + msecs_to_jiffies(
312 MTIP_NCQ_COMMAND_TIMEOUT_MS);
316 * Enable/disable the reception of FIS
318 * @port Pointer to the port data structure
319 * @enable 1 to enable, 0 to disable
322 * Previous state: 1 enabled, 0 disabled
324 static int mtip_enable_fis(struct mtip_port *port, int enable)
328 /* enable FIS reception */
329 tmp = readl(port->mmio + PORT_CMD);
331 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
333 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
336 readl(port->mmio + PORT_CMD);
338 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
342 * Enable/disable the DMA engine
344 * @port Pointer to the port data structure
345 * @enable 1 to enable, 0 to disable
348 * Previous state: 1 enabled, 0 disabled.
350 static int mtip_enable_engine(struct mtip_port *port, int enable)
354 /* enable FIS reception */
355 tmp = readl(port->mmio + PORT_CMD);
357 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
359 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
361 readl(port->mmio + PORT_CMD);
362 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
366 * Enables the port DMA engine and FIS reception.
371 static inline void mtip_start_port(struct mtip_port *port)
373 /* Enable FIS reception */
374 mtip_enable_fis(port, 1);
376 /* Enable the DMA engine */
377 mtip_enable_engine(port, 1);
381 * Deinitialize a port by disabling port interrupts, the DMA engine,
384 * @port Pointer to the port structure
389 static inline void mtip_deinit_port(struct mtip_port *port)
391 /* Disable interrupts on this port */
392 writel(0, port->mmio + PORT_IRQ_MASK);
394 /* Disable the DMA engine */
395 mtip_enable_engine(port, 0);
397 /* Disable FIS reception */
398 mtip_enable_fis(port, 0);
404 * This function deinitializes the port by calling mtip_deinit_port() and
405 * then initializes it by setting the command header and RX FIS addresses,
406 * clearing the SError register and any pending port interrupts before
407 * re-enabling the default set of port interrupts.
409 * @port Pointer to the port structure.
414 static void mtip_init_port(struct mtip_port *port)
417 mtip_deinit_port(port);
419 /* Program the command list base and FIS base addresses */
420 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
421 writel((port->command_list_dma >> 16) >> 16,
422 port->mmio + PORT_LST_ADDR_HI);
423 writel((port->rxfis_dma >> 16) >> 16,
424 port->mmio + PORT_FIS_ADDR_HI);
427 writel(port->command_list_dma & 0xFFFFFFFF,
428 port->mmio + PORT_LST_ADDR);
429 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
432 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
434 /* reset the completed registers.*/
435 for (i = 0; i < port->dd->slot_groups; i++)
436 writel(0xFFFFFFFF, port->completed[i]);
438 /* Clear any pending interrupts for this port */
439 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
441 /* Clear any pending interrupts on the HBA. */
442 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
443 port->dd->mmio + HOST_IRQ_STAT);
445 /* Enable port interrupts */
446 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
452 * @port Pointer to the port data structure.
457 static void mtip_restart_port(struct mtip_port *port)
459 unsigned long timeout;
461 /* Disable the DMA engine */
462 mtip_enable_engine(port, 0);
464 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
465 timeout = jiffies + msecs_to_jiffies(500);
466 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
467 && time_before(jiffies, timeout))
470 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
474 * Chip quirk: escalate to hba reset if
475 * PxCMD.CR not clear after 500 ms
477 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
478 dev_warn(&port->dd->pdev->dev,
479 "PxCMD.CR not clear, escalating reset\n");
481 if (hba_reset_nosleep(port->dd))
482 dev_err(&port->dd->pdev->dev,
483 "HBA reset escalation failed.\n");
485 /* 30 ms delay before com reset to quiesce chip */
489 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
492 writel(readl(port->mmio + PORT_SCR_CTL) |
493 1, port->mmio + PORT_SCR_CTL);
494 readl(port->mmio + PORT_SCR_CTL);
496 /* Wait 1 ms to quiesce chip function */
497 timeout = jiffies + msecs_to_jiffies(1);
498 while (time_before(jiffies, timeout))
501 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
504 /* Clear PxSCTL.DET */
505 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
506 port->mmio + PORT_SCR_CTL);
507 readl(port->mmio + PORT_SCR_CTL);
509 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
510 timeout = jiffies + msecs_to_jiffies(500);
511 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
512 && time_before(jiffies, timeout))
515 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
518 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
519 dev_warn(&port->dd->pdev->dev,
520 "COM reset failed\n");
522 mtip_init_port(port);
523 mtip_start_port(port);
528 * Helper function for tag logging
530 static void print_tags(struct driver_data *dd,
532 unsigned long *tagbits,
535 unsigned char tagmap[128];
536 int group, tagmap_len = 0;
538 memset(tagmap, 0, sizeof(tagmap));
539 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
540 tagmap_len = sprintf(tagmap + tagmap_len, "%016lX ",
542 dev_warn(&dd->pdev->dev,
543 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
547 * Called periodically to see if any read/write commands are
548 * taking too long to complete.
550 * @data Pointer to the PORT data structure.
555 static void mtip_timeout_function(unsigned long int data)
557 struct mtip_port *port = (struct mtip_port *) data;
558 struct host_to_dev_fis *fis;
559 struct mtip_cmd *command;
560 int tag, cmdto_cnt = 0;
561 unsigned int bit, group;
562 unsigned int num_command_slots = port->dd->slot_groups * 32;
563 unsigned long to, tagaccum[SLOTBITS_IN_LONGS];
568 if (test_bit(MTIP_DDF_RESUME_BIT, &port->dd->dd_flag)) {
569 mod_timer(&port->cmd_timer,
570 jiffies + msecs_to_jiffies(30000));
573 /* clear the tag accumulator */
574 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
576 for (tag = 0; tag < num_command_slots; tag++) {
578 * Skip internal command slot as it has
579 * its own timeout mechanism
581 if (tag == MTIP_TAG_INTERNAL)
584 if (atomic_read(&port->commands[tag].active) &&
585 (time_after(jiffies, port->commands[tag].comp_time))) {
589 command = &port->commands[tag];
590 fis = (struct host_to_dev_fis *) command->command;
592 set_bit(tag, tagaccum);
595 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
598 * Clear the completed bit. This should prevent
599 * any interrupt handlers from trying to retire
602 writel(1 << bit, port->completed[group]);
604 /* Call the async completion callback. */
605 if (likely(command->async_callback))
606 command->async_callback(command->async_data,
608 command->async_callback = NULL;
609 command->comp_func = NULL;
611 /* Unmap the DMA scatter list entries */
612 dma_unmap_sg(&port->dd->pdev->dev,
614 command->scatter_ents,
618 * Clear the allocated bit and active tag for the
621 atomic_set(&port->commands[tag].active, 0);
622 release_slot(port, tag);
628 if (cmdto_cnt && !test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
629 print_tags(port->dd, "timed out", tagaccum, cmdto_cnt);
631 mtip_restart_port(port);
632 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
633 wake_up_interruptible(&port->svc_wait);
636 if (port->ic_pause_timer) {
637 to = port->ic_pause_timer + msecs_to_jiffies(1000);
638 if (time_after(jiffies, to)) {
639 if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
640 port->ic_pause_timer = 0;
641 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
642 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
643 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
644 wake_up_interruptible(&port->svc_wait);
651 /* Restart the timer */
652 mod_timer(&port->cmd_timer,
653 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
657 * IO completion function.
659 * This completion function is called by the driver ISR when a
660 * command that was issued by the kernel completes. It first calls the
661 * asynchronous completion function which normally calls back into the block
662 * layer passing the asynchronous callback data, then unmaps the
663 * scatter list associated with the completed command, and finally
664 * clears the allocated bit associated with the completed command.
666 * @port Pointer to the port data structure.
667 * @tag Tag of the command.
668 * @data Pointer to driver_data.
669 * @status Completion status.
674 static void mtip_async_complete(struct mtip_port *port,
679 struct mtip_cmd *command;
680 struct driver_data *dd = data;
681 int cb_status = status ? -EIO : 0;
683 if (unlikely(!dd) || unlikely(!port))
686 command = &port->commands[tag];
688 if (unlikely(status == PORT_IRQ_TF_ERR)) {
689 dev_warn(&port->dd->pdev->dev,
690 "Command tag %d failed due to TFE\n", tag);
693 /* Upper layer callback */
694 if (likely(command->async_callback))
695 command->async_callback(command->async_data, cb_status);
697 command->async_callback = NULL;
698 command->comp_func = NULL;
700 /* Unmap the DMA scatter list entries */
701 dma_unmap_sg(&dd->pdev->dev,
703 command->scatter_ents,
706 /* Clear the allocated and active bits for the command */
707 atomic_set(&port->commands[tag].active, 0);
708 release_slot(port, tag);
714 * Internal command completion callback function.
716 * This function is normally called by the driver ISR when an internal
717 * command completed. This function signals the command completion by
718 * calling complete().
720 * @port Pointer to the port data structure.
721 * @tag Tag of the command that has completed.
722 * @data Pointer to a completion structure.
723 * @status Completion status.
728 static void mtip_completion(struct mtip_port *port,
733 struct mtip_cmd *command = &port->commands[tag];
734 struct completion *waiting = data;
735 if (unlikely(status == PORT_IRQ_TF_ERR))
736 dev_warn(&port->dd->pdev->dev,
737 "Internal command %d completed with TFE\n", tag);
739 command->async_callback = NULL;
740 command->comp_func = NULL;
745 static void mtip_null_completion(struct mtip_port *port,
753 static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
754 dma_addr_t buffer_dma, unsigned int sectors);
755 static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
756 struct smart_attr *attrib);
760 * @dd Pointer to the DRIVER_DATA structure.
765 static void mtip_handle_tfe(struct driver_data *dd)
767 int group, tag, bit, reissue, rv;
768 struct mtip_port *port;
769 struct mtip_cmd *cmd;
771 struct host_to_dev_fis *fis;
772 unsigned long tagaccum[SLOTBITS_IN_LONGS];
773 unsigned int cmd_cnt = 0;
775 char *fail_reason = NULL;
776 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
778 dev_warn(&dd->pdev->dev, "Taskfile error\n");
782 /* Stop the timer to prevent command timeouts. */
783 del_timer(&port->cmd_timer);
784 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
786 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
787 test_bit(MTIP_TAG_INTERNAL, port->allocated)) {
788 cmd = &port->commands[MTIP_TAG_INTERNAL];
789 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
791 atomic_inc(&cmd->active); /* active > 1 indicates error */
792 if (cmd->comp_data && cmd->comp_func) {
793 cmd->comp_func(port, MTIP_TAG_INTERNAL,
794 cmd->comp_data, PORT_IRQ_TF_ERR);
796 goto handle_tfe_exit;
799 /* clear the tag accumulator */
800 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
802 /* Loop through all the groups */
803 for (group = 0; group < dd->slot_groups; group++) {
804 completed = readl(port->completed[group]);
806 /* clear completed status register in the hardware.*/
807 writel(completed, port->completed[group]);
809 /* Process successfully completed commands */
810 for (bit = 0; bit < 32 && completed; bit++) {
811 if (!(completed & (1<<bit)))
813 tag = (group << 5) + bit;
815 /* Skip the internal command slot */
816 if (tag == MTIP_TAG_INTERNAL)
819 cmd = &port->commands[tag];
820 if (likely(cmd->comp_func)) {
821 set_bit(tag, tagaccum);
823 atomic_set(&cmd->active, 0);
829 dev_err(&port->dd->pdev->dev,
830 "Missing completion func for tag %d",
832 if (mtip_check_surprise_removal(dd->pdev)) {
833 mtip_command_cleanup(dd);
834 /* don't proceed further */
841 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
843 /* Restart the port */
845 mtip_restart_port(port);
847 /* Trying to determine the cause of the error */
848 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
850 dd->port->log_buf_dma, 1);
852 dev_warn(&dd->pdev->dev,
853 "Error in READ LOG EXT (10h) command\n");
854 /* non-critical error, don't fail the load */
856 buf = (unsigned char *)dd->port->log_buf;
857 if (buf[259] & 0x1) {
858 dev_info(&dd->pdev->dev,
859 "Write protect bit is set.\n");
860 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
861 fail_all_ncq_write = 1;
862 fail_reason = "write protect";
864 if (buf[288] == 0xF7) {
865 dev_info(&dd->pdev->dev,
866 "Exceeded Tmax, drive in thermal shutdown.\n");
867 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
868 fail_all_ncq_cmds = 1;
869 fail_reason = "thermal shutdown";
871 if (buf[288] == 0xBF) {
872 dev_info(&dd->pdev->dev,
873 "Drive indicates rebuild has failed.\n");
874 fail_all_ncq_cmds = 1;
875 fail_reason = "rebuild failed";
879 /* clear the tag accumulator */
880 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
882 /* Loop through all the groups */
883 for (group = 0; group < dd->slot_groups; group++) {
884 for (bit = 0; bit < 32; bit++) {
886 tag = (group << 5) + bit;
887 cmd = &port->commands[tag];
889 /* If the active bit is set re-issue the command */
890 if (atomic_read(&cmd->active) == 0)
893 fis = (struct host_to_dev_fis *)cmd->command;
895 /* Should re-issue? */
896 if (tag == MTIP_TAG_INTERNAL ||
897 fis->command == ATA_CMD_SET_FEATURES)
900 if (fail_all_ncq_cmds ||
901 (fail_all_ncq_write &&
902 fis->command == ATA_CMD_FPDMA_WRITE)) {
903 dev_warn(&dd->pdev->dev,
904 " Fail: %s w/tag %d [%s].\n",
905 fis->command == ATA_CMD_FPDMA_WRITE ?
908 fail_reason != NULL ?
909 fail_reason : "unknown");
910 atomic_set(&cmd->active, 0);
911 if (cmd->comp_func) {
912 cmd->comp_func(port, tag,
921 * First check if this command has
922 * exceeded its retries.
924 if (reissue && (cmd->retries-- > 0)) {
926 set_bit(tag, tagaccum);
928 /* Re-issue the command. */
929 mtip_issue_ncq_command(port, tag);
934 /* Retire a command that will not be reissued */
935 dev_warn(&port->dd->pdev->dev,
936 "retiring tag %d\n", tag);
937 atomic_set(&cmd->active, 0);
946 dev_warn(&port->dd->pdev->dev,
947 "Bad completion for tag %d\n",
951 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
954 /* clear eh_active */
955 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
956 wake_up_interruptible(&port->svc_wait);
958 mod_timer(&port->cmd_timer,
959 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
963 * Handle a set device bits interrupt
965 static inline void mtip_process_sdbf(struct driver_data *dd)
967 struct mtip_port *port = dd->port;
970 struct mtip_cmd *command;
972 /* walk all bits in all slot groups */
973 for (group = 0; group < dd->slot_groups; group++) {
974 completed = readl(port->completed[group]);
978 /* clear completed status register in the hardware.*/
979 writel(completed, port->completed[group]);
981 /* Process completed commands. */
983 (bit < 32) && completed;
984 bit++, completed >>= 1) {
985 if (completed & 0x01) {
986 tag = (group << 5) | bit;
988 /* skip internal command slot. */
989 if (unlikely(tag == MTIP_TAG_INTERNAL))
992 command = &port->commands[tag];
993 /* make internal callback */
994 if (likely(command->comp_func)) {
1001 dev_warn(&dd->pdev->dev,
1006 if (mtip_check_surprise_removal(
1008 mtip_command_cleanup(dd);
1018 * Process legacy pio and d2h interrupts
1020 static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
1022 struct mtip_port *port = dd->port;
1023 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
1025 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
1026 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1027 & (1 << MTIP_TAG_INTERNAL))) {
1028 if (cmd->comp_func) {
1029 cmd->comp_func(port,
1041 * Demux and handle errors
1043 static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
1045 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
1046 mtip_handle_tfe(dd);
1048 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
1049 dev_warn(&dd->pdev->dev,
1050 "Clearing PxSERR.DIAG.x\n");
1051 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
1054 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
1055 dev_warn(&dd->pdev->dev,
1056 "Clearing PxSERR.DIAG.n\n");
1057 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
1060 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
1061 dev_warn(&dd->pdev->dev,
1062 "Port stat errors %x unhandled\n",
1063 (port_stat & ~PORT_IRQ_HANDLED));
1067 static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
1069 struct driver_data *dd = (struct driver_data *) data;
1070 struct mtip_port *port = dd->port;
1071 u32 hba_stat, port_stat;
1074 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1078 /* Acknowledge the interrupt status on the port.*/
1079 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1080 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1082 /* Demux port status */
1083 if (likely(port_stat & PORT_IRQ_SDB_FIS))
1084 mtip_process_sdbf(dd);
1086 if (unlikely(port_stat & PORT_IRQ_ERR)) {
1087 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
1088 mtip_command_cleanup(dd);
1089 /* don't proceed further */
1092 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1096 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
1099 if (unlikely(port_stat & PORT_IRQ_LEGACY))
1100 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
1103 /* acknowledge interrupt */
1104 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
1110 * Wrapper for mtip_handle_irq
1111 * (ignores return code)
1113 static void mtip_tasklet(unsigned long data)
1115 mtip_handle_irq((struct driver_data *) data);
1119 * HBA interrupt subroutine.
1122 * @instance Pointer to the driver data structure.
1125 * IRQ_HANDLED A HBA interrupt was pending and handled.
1126 * IRQ_NONE This interrupt was not for the HBA.
1128 static irqreturn_t mtip_irq_handler(int irq, void *instance)
1130 struct driver_data *dd = instance;
1131 tasklet_schedule(&dd->tasklet);
1135 static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1137 atomic_set(&port->commands[tag].active, 1);
1138 writel(1 << MTIP_TAG_BIT(tag),
1139 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1142 static bool mtip_pause_ncq(struct mtip_port *port,
1143 struct host_to_dev_fis *fis)
1145 struct host_to_dev_fis *reply;
1146 unsigned long task_file_data;
1148 reply = port->rxfis + RX_FIS_D2H_REG;
1149 task_file_data = readl(port->mmio+PORT_TFDATA);
1151 if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
1152 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1154 if ((task_file_data & 1))
1157 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
1158 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1159 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1160 port->ic_pause_timer = jiffies;
1162 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1163 (fis->features == 0x03)) {
1164 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1165 port->ic_pause_timer = jiffies;
1167 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1168 ((fis->command == 0xFC) &&
1169 (fis->features == 0x27 || fis->features == 0x72 ||
1170 fis->features == 0x62 || fis->features == 0x26))) {
1171 /* Com reset after secure erase or lowlevel format */
1172 mtip_restart_port(port);
1180 * Wait for port to quiesce
1182 * @port Pointer to port data structure
1183 * @timeout Max duration to wait (ms)
1187 * -EBUSY Commands still active
1189 static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1193 unsigned int active = 1;
1195 to = jiffies + msecs_to_jiffies(timeout);
1197 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1198 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
1200 continue; /* svc thd is actively issuing commands */
1202 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
1205 * Ignore s_active bit 0 of array element 0.
1206 * This bit will always be set
1208 active = readl(port->s_active[0]) & 0xFFFFFFFE;
1209 for (n = 1; n < port->dd->slot_groups; n++)
1210 active |= readl(port->s_active[n]);
1216 } while (time_before(jiffies, to));
1218 return active ? -EBUSY : 0;
1222 * Execute an internal command and wait for the completion.
1224 * @port Pointer to the port data structure.
1225 * @fis Pointer to the FIS that describes the command.
1226 * @fis_len Length in WORDS of the FIS.
1227 * @buffer DMA accessible for command data.
1228 * @buf_len Length, in bytes, of the data buffer.
1229 * @opts Command header options, excluding the FIS length
1230 * and the number of PRD entries.
1231 * @timeout Time in ms to wait for the command to complete.
1234 * 0 Command completed successfully.
1235 * -EFAULT The buffer address is not correctly aligned.
1236 * -EBUSY Internal command or other IO in progress.
1237 * -EAGAIN Time out waiting for command to complete.
1239 static int mtip_exec_internal_command(struct mtip_port *port,
1240 struct host_to_dev_fis *fis,
1246 unsigned long timeout)
1248 struct mtip_cmd_sg *command_sg;
1249 DECLARE_COMPLETION_ONSTACK(wait);
1250 int rv = 0, ready2go = 1;
1251 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
1254 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1255 if (buffer & 0x00000007) {
1256 dev_err(&port->dd->pdev->dev,
1257 "SG buffer is not 8 byte aligned\n");
1261 to = jiffies + msecs_to_jiffies(timeout);
1263 ready2go = !test_and_set_bit(MTIP_TAG_INTERNAL,
1268 } while (time_before(jiffies, to));
1270 dev_warn(&port->dd->pdev->dev,
1271 "Internal cmd active. new cmd [%02X]\n", fis->command);
1274 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1275 port->ic_pause_timer = 0;
1277 if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
1278 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1279 else if (fis->command == ATA_CMD_DOWNLOAD_MICRO)
1280 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1282 if (atomic == GFP_KERNEL) {
1283 if (fis->command != ATA_CMD_STANDBYNOW1) {
1284 /* wait for io to complete if non atomic */
1285 if (mtip_quiesce_io(port, 5000) < 0) {
1286 dev_warn(&port->dd->pdev->dev,
1287 "Failed to quiesce IO\n");
1288 release_slot(port, MTIP_TAG_INTERNAL);
1289 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1290 wake_up_interruptible(&port->svc_wait);
1295 /* Set the completion function and data for the command. */
1296 int_cmd->comp_data = &wait;
1297 int_cmd->comp_func = mtip_completion;
1300 /* Clear completion - we're going to poll */
1301 int_cmd->comp_data = NULL;
1302 int_cmd->comp_func = mtip_null_completion;
1305 /* Copy the command to the command table */
1306 memcpy(int_cmd->command, fis, fis_len*4);
1308 /* Populate the SG list */
1309 int_cmd->command_header->opts =
1310 __force_bit2int cpu_to_le32(opts | fis_len);
1312 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1315 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1317 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1318 command_sg->dba_upper =
1319 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
1321 int_cmd->command_header->opts |=
1322 __force_bit2int cpu_to_le32((1 << 16));
1325 /* Populate the command header */
1326 int_cmd->command_header->byte_count = 0;
1328 /* Issue the command to the hardware */
1329 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1331 /* Poll if atomic, wait_for_completion otherwise */
1332 if (atomic == GFP_KERNEL) {
1333 /* Wait for the command to complete or timeout. */
1334 if (wait_for_completion_timeout(
1336 msecs_to_jiffies(timeout)) == 0) {
1337 dev_err(&port->dd->pdev->dev,
1338 "Internal command did not complete [%d] "
1339 "within timeout of %lu ms\n",
1341 if (mtip_check_surprise_removal(port->dd->pdev) ||
1342 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1343 &port->dd->dd_flag)) {
1350 /* Spin for <timeout> checking if command still outstanding */
1351 timeout = jiffies + msecs_to_jiffies(timeout);
1352 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1353 & (1 << MTIP_TAG_INTERNAL))
1354 && time_before(jiffies, timeout)) {
1355 if (mtip_check_surprise_removal(port->dd->pdev)) {
1359 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
1360 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1361 &port->dd->dd_flag)) {
1365 if (readl(port->mmio + PORT_IRQ_STAT) & PORT_IRQ_ERR) {
1366 atomic_inc(&int_cmd->active); /* error */
1372 if (atomic_read(&int_cmd->active) > 1) {
1373 dev_err(&port->dd->pdev->dev,
1374 "Internal command [%02X] failed\n", fis->command);
1377 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1378 & (1 << MTIP_TAG_INTERNAL)) {
1380 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1381 &port->dd->dd_flag)) {
1382 mtip_restart_port(port);
1387 /* Clear the allocated and active bits for the internal command. */
1388 atomic_set(&int_cmd->active, 0);
1389 release_slot(port, MTIP_TAG_INTERNAL);
1390 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1394 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1395 wake_up_interruptible(&port->svc_wait);
1401 * Byte-swap ATA ID strings.
1403 * ATA identify data contains strings in byte-swapped 16-bit words.
1404 * They must be swapped (on all architectures) to be usable as C strings.
1405 * This function swaps bytes in-place.
1407 * @buf The buffer location of the string
1408 * @len The number of bytes to swap
1413 static inline void ata_swap_string(u16 *buf, unsigned int len)
1416 for (i = 0; i < (len/2); i++)
1417 be16_to_cpus(&buf[i]);
1421 * Request the device identity information.
1423 * If a user space buffer is not specified, i.e. is NULL, the
1424 * identify information is still read from the drive and placed
1425 * into the identify data buffer (@e port->identify) in the
1426 * port data structure.
1427 * When the identify buffer contains valid identify information @e
1428 * port->identify_valid is non-zero.
1430 * @port Pointer to the port structure.
1431 * @user_buffer A user space buffer where the identify data should be
1435 * 0 Command completed successfully.
1436 * -EFAULT An error occurred while coping data to the user buffer.
1437 * -1 Command failed.
1439 static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1442 struct host_to_dev_fis fis;
1444 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
1447 /* Build the FIS. */
1448 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1451 fis.command = ATA_CMD_ID_ATA;
1453 /* Set the identify information as invalid. */
1454 port->identify_valid = 0;
1456 /* Clear the identify information. */
1457 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1459 /* Execute the command. */
1460 if (mtip_exec_internal_command(port,
1464 sizeof(u16) * ATA_ID_WORDS,
1467 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1474 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1475 * perform field-sensitive swapping on the string fields.
1476 * See the kernel use of ata_id_string() for proof of this.
1478 #ifdef __LITTLE_ENDIAN
1479 ata_swap_string(port->identify + 27, 40); /* model string*/
1480 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1481 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1485 for (i = 0; i < ATA_ID_WORDS; i++)
1486 port->identify[i] = le16_to_cpu(port->identify[i]);
1490 /* Set the identify buffer as valid. */
1491 port->identify_valid = 1;
1497 ATA_ID_WORDS * sizeof(u16))) {
1508 * Issue a standby immediate command to the device.
1510 * @port Pointer to the port structure.
1513 * 0 Command was executed successfully.
1514 * -1 An error occurred while executing the command.
1516 static int mtip_standby_immediate(struct mtip_port *port)
1519 struct host_to_dev_fis fis;
1520 unsigned long start;
1522 /* Build the FIS. */
1523 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1526 fis.command = ATA_CMD_STANDBYNOW1;
1529 rv = mtip_exec_internal_command(port,
1537 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1538 jiffies_to_msecs(jiffies - start));
1540 dev_warn(&port->dd->pdev->dev,
1541 "STANDBY IMMEDIATE command failed.\n");
1547 * Issue a READ LOG EXT command to the device.
1549 * @port pointer to the port structure.
1550 * @page page number to fetch
1551 * @buffer pointer to buffer
1552 * @buffer_dma dma address corresponding to @buffer
1553 * @sectors page length to fetch, in sectors
1556 * @rv return value from mtip_exec_internal_command()
1558 static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1559 dma_addr_t buffer_dma, unsigned int sectors)
1561 struct host_to_dev_fis fis;
1563 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1566 fis.command = ATA_CMD_READ_LOG_EXT;
1567 fis.sect_count = sectors & 0xFF;
1568 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1571 fis.device = ATA_DEVICE_OBS;
1573 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1575 return mtip_exec_internal_command(port,
1579 sectors * ATA_SECT_SIZE,
1582 MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
1586 * Issue a SMART READ DATA command to the device.
1588 * @port pointer to the port structure.
1589 * @buffer pointer to buffer
1590 * @buffer_dma dma address corresponding to @buffer
1593 * @rv return value from mtip_exec_internal_command()
1595 static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1596 dma_addr_t buffer_dma)
1598 struct host_to_dev_fis fis;
1600 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1603 fis.command = ATA_CMD_SMART;
1604 fis.features = 0xD0;
1608 fis.device = ATA_DEVICE_OBS;
1610 return mtip_exec_internal_command(port,
1621 * Get the value of a smart attribute
1623 * @port pointer to the port structure
1624 * @id attribute number
1625 * @attrib pointer to return attrib information corresponding to @id
1628 * -EINVAL NULL buffer passed or unsupported attribute @id.
1629 * -EPERM Identify data not valid, SMART not supported or not enabled
1631 static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1632 struct smart_attr *attrib)
1635 struct smart_attr *pattr;
1640 if (!port->identify_valid) {
1641 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1644 if (!(port->identify[82] & 0x1)) {
1645 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1648 if (!(port->identify[85] & 0x1)) {
1649 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1653 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1654 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1656 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1660 pattr = (struct smart_attr *)(port->smart_buf + 2);
1661 for (i = 0; i < 29; i++, pattr++)
1662 if (pattr->attr_id == id) {
1663 memcpy(attrib, pattr, sizeof(struct smart_attr));
1668 dev_warn(&port->dd->pdev->dev,
1669 "Query for invalid SMART attribute ID\n");
1677 * Get the drive capacity.
1679 * @dd Pointer to the device data structure.
1680 * @sectors Pointer to the variable that will receive the sector count.
1683 * 1 Capacity was returned successfully.
1684 * 0 The identify information is invalid.
1686 static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
1688 struct mtip_port *port = dd->port;
1689 u64 total, raw0, raw1, raw2, raw3;
1690 raw0 = port->identify[100];
1691 raw1 = port->identify[101];
1692 raw2 = port->identify[102];
1693 raw3 = port->identify[103];
1694 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1696 return (bool) !!port->identify_valid;
1702 * Resets the HBA by setting the HBA Reset bit in the Global
1703 * HBA Control register. After setting the HBA Reset bit the
1704 * function waits for 1 second before reading the HBA Reset
1705 * bit to make sure it has cleared. If HBA Reset is not clear
1706 * an error is returned. Cannot be used in non-blockable
1709 * @dd Pointer to the driver data structure.
1712 * 0 The reset was successful.
1713 * -1 The HBA Reset bit did not clear.
1715 static int mtip_hba_reset(struct driver_data *dd)
1717 mtip_deinit_port(dd->port);
1719 /* Set the reset bit */
1720 writel(HOST_RESET, dd->mmio + HOST_CTL);
1723 readl(dd->mmio + HOST_CTL);
1725 /* Wait for reset to clear */
1728 /* Check the bit has cleared */
1729 if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
1730 dev_err(&dd->pdev->dev,
1731 "Reset bit did not clear.\n");
1739 * Display the identify command data.
1741 * @port Pointer to the port data structure.
1746 static void mtip_dump_identify(struct mtip_port *port)
1749 unsigned short revid;
1752 if (!port->identify_valid)
1755 strlcpy(cbuf, (char *)(port->identify+10), 21);
1756 dev_info(&port->dd->pdev->dev,
1757 "Serial No.: %s\n", cbuf);
1759 strlcpy(cbuf, (char *)(port->identify+23), 9);
1760 dev_info(&port->dd->pdev->dev,
1761 "Firmware Ver.: %s\n", cbuf);
1763 strlcpy(cbuf, (char *)(port->identify+27), 41);
1764 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1766 if (mtip_hw_get_capacity(port->dd, §ors))
1767 dev_info(&port->dd->pdev->dev,
1768 "Capacity: %llu sectors (%llu MB)\n",
1770 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1772 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
1773 switch (revid & 0xFF) {
1775 strlcpy(cbuf, "A0", 3);
1778 strlcpy(cbuf, "A2", 3);
1781 strlcpy(cbuf, "?", 2);
1784 dev_info(&port->dd->pdev->dev,
1785 "Card Type: %s\n", cbuf);
1789 * Map the commands scatter list into the command table.
1791 * @command Pointer to the command.
1792 * @nents Number of scatter list entries.
1797 static inline void fill_command_sg(struct driver_data *dd,
1798 struct mtip_cmd *command,
1802 unsigned int dma_len;
1803 struct mtip_cmd_sg *command_sg;
1804 struct scatterlist *sg = command->sg;
1806 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1808 for (n = 0; n < nents; n++) {
1809 dma_len = sg_dma_len(sg);
1810 if (dma_len > 0x400000)
1811 dev_err(&dd->pdev->dev,
1812 "DMA segment length truncated\n");
1813 command_sg->info = __force_bit2int
1814 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1815 command_sg->dba = __force_bit2int
1816 cpu_to_le32(sg_dma_address(sg));
1817 command_sg->dba_upper = __force_bit2int
1818 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
1825 * @brief Execute a drive command.
1827 * return value 0 The command completed successfully.
1828 * return value -1 An error occurred while executing the command.
1830 static int exec_drive_task(struct mtip_port *port, u8 *command)
1832 struct host_to_dev_fis fis;
1833 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1835 /* Build the FIS. */
1836 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1839 fis.command = command[0];
1840 fis.features = command[1];
1841 fis.sect_count = command[2];
1842 fis.sector = command[3];
1843 fis.cyl_low = command[4];
1844 fis.cyl_hi = command[5];
1845 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1847 dbg_printk(MTIP_DRV_NAME " %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
1857 /* Execute the command. */
1858 if (mtip_exec_internal_command(port,
1865 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
1869 command[0] = reply->command; /* Status*/
1870 command[1] = reply->features; /* Error*/
1871 command[4] = reply->cyl_low;
1872 command[5] = reply->cyl_hi;
1874 dbg_printk(MTIP_DRV_NAME " %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
1885 * @brief Execute a drive command.
1887 * @param port Pointer to the port data structure.
1888 * @param command Pointer to the user specified command parameters.
1889 * @param user_buffer Pointer to the user space buffer where read sector
1890 * data should be copied.
1892 * return value 0 The command completed successfully.
1893 * return value -EFAULT An error occurred while copying the completion
1894 * data to the user space buffer.
1895 * return value -1 An error occurred while executing the command.
1897 static int exec_drive_command(struct mtip_port *port, u8 *command,
1898 void __user *user_buffer)
1900 struct host_to_dev_fis fis;
1901 struct host_to_dev_fis *reply;
1903 dma_addr_t dma_addr = 0;
1904 int rv = 0, xfer_sz = command[3];
1910 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1911 ATA_SECT_SIZE * xfer_sz,
1915 dev_err(&port->dd->pdev->dev,
1916 "Memory allocation failed (%d bytes)\n",
1917 ATA_SECT_SIZE * xfer_sz);
1920 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1923 /* Build the FIS. */
1924 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1927 fis.command = command[0];
1928 fis.features = command[2];
1929 fis.sect_count = command[3];
1930 if (fis.command == ATA_CMD_SMART) {
1931 fis.sector = command[1];
1937 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1939 reply = (port->rxfis + RX_FIS_D2H_REG);
1941 dbg_printk(MTIP_DRV_NAME
1942 " %s: User Command: cmd %x, sect %x, "
1943 "feat %x, sectcnt %x\n",
1950 /* Execute the command. */
1951 if (mtip_exec_internal_command(port,
1954 (xfer_sz ? dma_addr : 0),
1955 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
1958 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
1961 goto exit_drive_command;
1964 /* Collect the completion status. */
1965 command[0] = reply->command; /* Status*/
1966 command[1] = reply->features; /* Error*/
1967 command[2] = reply->sect_count;
1969 dbg_printk(MTIP_DRV_NAME
1970 " %s: Completion Status: stat %x, "
1971 "err %x, nsect %x\n",
1978 if (copy_to_user(user_buffer,
1980 ATA_SECT_SIZE * command[3])) {
1982 goto exit_drive_command;
1987 dmam_free_coherent(&port->dd->pdev->dev,
1988 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1993 * Indicates whether a command has a single sector payload.
1995 * @command passed to the device to perform the certain event.
1996 * @features passed to the device to perform the certain event.
1999 * 1 command is one that always has a single sector payload,
2000 * regardless of the value in the Sector Count field.
2004 static unsigned int implicit_sector(unsigned char command,
2005 unsigned char features)
2007 unsigned int rv = 0;
2009 /* list of commands that have an implicit sector count of 1 */
2011 case ATA_CMD_SEC_SET_PASS:
2012 case ATA_CMD_SEC_UNLOCK:
2013 case ATA_CMD_SEC_ERASE_PREP:
2014 case ATA_CMD_SEC_ERASE_UNIT:
2015 case ATA_CMD_SEC_FREEZE_LOCK:
2016 case ATA_CMD_SEC_DISABLE_PASS:
2017 case ATA_CMD_PMP_READ:
2018 case ATA_CMD_PMP_WRITE:
2021 case ATA_CMD_SET_MAX:
2022 if (features == ATA_SET_MAX_UNLOCK)
2026 if ((features == ATA_SMART_READ_VALUES) ||
2027 (features == ATA_SMART_READ_THRESHOLDS))
2030 case ATA_CMD_CONF_OVERLAY:
2031 if ((features == ATA_DCO_IDENTIFY) ||
2032 (features == ATA_DCO_SET))
2039 static void mtip_set_timeout(struct host_to_dev_fis *fis, unsigned int *timeout)
2041 switch (fis->command) {
2042 case ATA_CMD_DOWNLOAD_MICRO:
2043 *timeout = 120000; /* 2 minutes */
2045 case ATA_CMD_SEC_ERASE_UNIT:
2047 *timeout = 240000; /* 4 minutes */
2049 case ATA_CMD_STANDBYNOW1:
2050 *timeout = 120000; /* 2 minutes */
2054 *timeout = 60000; /* 60 seconds */
2057 *timeout = 15000; /* 15 seconds */
2060 *timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
2066 * Executes a taskfile
2067 * See ide_taskfile_ioctl() for derivation
2069 static int exec_drive_taskfile(struct driver_data *dd,
2071 ide_task_request_t *req_task,
2074 struct host_to_dev_fis fis;
2075 struct host_to_dev_fis *reply;
2078 dma_addr_t outbuf_dma = 0;
2079 dma_addr_t inbuf_dma = 0;
2080 dma_addr_t dma_buffer = 0;
2082 unsigned int taskin = 0;
2083 unsigned int taskout = 0;
2085 unsigned int timeout;
2086 unsigned int force_single_sector;
2087 unsigned int transfer_size;
2088 unsigned long task_file_data;
2089 int intotal = outtotal + req_task->out_size;
2091 taskout = req_task->out_size;
2092 taskin = req_task->in_size;
2093 /* 130560 = 512 * 0xFF*/
2094 if (taskin > 130560 || taskout > 130560) {
2100 outbuf = kzalloc(taskout, GFP_KERNEL);
2101 if (outbuf == NULL) {
2105 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
2109 outbuf_dma = pci_map_single(dd->pdev,
2113 if (outbuf_dma == 0) {
2117 dma_buffer = outbuf_dma;
2121 inbuf = kzalloc(taskin, GFP_KERNEL);
2122 if (inbuf == NULL) {
2127 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2131 inbuf_dma = pci_map_single(dd->pdev,
2133 taskin, DMA_FROM_DEVICE);
2134 if (inbuf_dma == 0) {
2138 dma_buffer = inbuf_dma;
2141 /* only supports PIO and non-data commands from this ioctl. */
2142 switch (req_task->data_phase) {
2144 nsect = taskout / ATA_SECT_SIZE;
2145 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2148 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2150 case TASKFILE_NO_DATA:
2151 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2158 /* Build the FIS. */
2159 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2163 fis.command = req_task->io_ports[7];
2164 fis.features = req_task->io_ports[1];
2165 fis.sect_count = req_task->io_ports[2];
2166 fis.lba_low = req_task->io_ports[3];
2167 fis.lba_mid = req_task->io_ports[4];
2168 fis.lba_hi = req_task->io_ports[5];
2169 /* Clear the dev bit*/
2170 fis.device = req_task->io_ports[6] & ~0x10;
2172 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2173 req_task->in_flags.all =
2174 IDE_TASKFILE_STD_IN_FLAGS |
2175 (IDE_HOB_STD_IN_FLAGS << 8);
2176 fis.lba_low_ex = req_task->hob_ports[3];
2177 fis.lba_mid_ex = req_task->hob_ports[4];
2178 fis.lba_hi_ex = req_task->hob_ports[5];
2179 fis.features_ex = req_task->hob_ports[1];
2180 fis.sect_cnt_ex = req_task->hob_ports[2];
2183 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2186 force_single_sector = implicit_sector(fis.command, fis.features);
2188 if ((taskin || taskout) && (!fis.sect_count)) {
2190 fis.sect_count = nsect;
2192 if (!force_single_sector) {
2193 dev_warn(&dd->pdev->dev,
2194 "data movement but "
2195 "sect_count is 0\n");
2202 dbg_printk(MTIP_DRV_NAME
2203 " %s: cmd %x, feat %x, nsect %x,"
2204 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2215 mtip_set_timeout(&fis, &timeout);
2217 /* Determine the correct transfer size.*/
2218 if (force_single_sector)
2219 transfer_size = ATA_SECT_SIZE;
2221 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2223 /* Execute the command.*/
2224 if (mtip_exec_internal_command(dd->port,
2236 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2238 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2239 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2240 req_task->io_ports[7] = reply->control;
2242 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2243 req_task->io_ports[7] = reply->command;
2246 /* reclaim the DMA buffers.*/
2248 pci_unmap_single(dd->pdev, inbuf_dma,
2249 taskin, DMA_FROM_DEVICE);
2251 pci_unmap_single(dd->pdev, outbuf_dma,
2252 taskout, DMA_TO_DEVICE);
2256 /* return the ATA registers to the caller.*/
2257 req_task->io_ports[1] = reply->features;
2258 req_task->io_ports[2] = reply->sect_count;
2259 req_task->io_ports[3] = reply->lba_low;
2260 req_task->io_ports[4] = reply->lba_mid;
2261 req_task->io_ports[5] = reply->lba_hi;
2262 req_task->io_ports[6] = reply->device;
2264 if (req_task->out_flags.all & 1) {
2266 req_task->hob_ports[3] = reply->lba_low_ex;
2267 req_task->hob_ports[4] = reply->lba_mid_ex;
2268 req_task->hob_ports[5] = reply->lba_hi_ex;
2269 req_task->hob_ports[1] = reply->features_ex;
2270 req_task->hob_ports[2] = reply->sect_cnt_ex;
2272 dbg_printk(MTIP_DRV_NAME
2273 " %s: Completion: stat %x,"
2274 "err %x, sect_cnt %x, lbalo %x,"
2275 "lbamid %x, lbahi %x, dev %x\n",
2277 req_task->io_ports[7],
2278 req_task->io_ports[1],
2279 req_task->io_ports[2],
2280 req_task->io_ports[3],
2281 req_task->io_ports[4],
2282 req_task->io_ports[5],
2283 req_task->io_ports[6]);
2286 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2292 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2299 pci_unmap_single(dd->pdev, inbuf_dma,
2300 taskin, DMA_FROM_DEVICE);
2302 pci_unmap_single(dd->pdev, outbuf_dma,
2303 taskout, DMA_TO_DEVICE);
2311 * Handle IOCTL calls from the Block Layer.
2313 * This function is called by the Block Layer when it receives an IOCTL
2314 * command that it does not understand. If the IOCTL command is not supported
2315 * this function returns -ENOTTY.
2317 * @dd Pointer to the driver data structure.
2318 * @cmd IOCTL command passed from the Block Layer.
2319 * @arg IOCTL argument passed from the Block Layer.
2322 * 0 The IOCTL completed successfully.
2323 * -ENOTTY The specified command is not supported.
2324 * -EFAULT An error occurred copying data to a user space buffer.
2325 * -EIO An error occurred while executing the command.
2327 static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2331 case HDIO_GET_IDENTITY:
2333 if (copy_to_user((void __user *)arg, dd->port->identify,
2334 sizeof(u16) * ATA_ID_WORDS))
2338 case HDIO_DRIVE_CMD:
2340 u8 drive_command[4];
2342 /* Copy the user command info to our buffer. */
2343 if (copy_from_user(drive_command,
2344 (void __user *) arg,
2345 sizeof(drive_command)))
2348 /* Execute the drive command. */
2349 if (exec_drive_command(dd->port,
2351 (void __user *) (arg+4)))
2354 /* Copy the status back to the users buffer. */
2355 if (copy_to_user((void __user *) arg,
2357 sizeof(drive_command)))
2362 case HDIO_DRIVE_TASK:
2364 u8 drive_command[7];
2366 /* Copy the user command info to our buffer. */
2367 if (copy_from_user(drive_command,
2368 (void __user *) arg,
2369 sizeof(drive_command)))
2372 /* Execute the drive command. */
2373 if (exec_drive_task(dd->port, drive_command))
2376 /* Copy the status back to the users buffer. */
2377 if (copy_to_user((void __user *) arg,
2379 sizeof(drive_command)))
2384 case HDIO_DRIVE_TASKFILE: {
2385 ide_task_request_t req_task;
2388 if (copy_from_user(&req_task, (void __user *) arg,
2392 outtotal = sizeof(req_task);
2394 ret = exec_drive_taskfile(dd, (void __user *) arg,
2395 &req_task, outtotal);
2397 if (copy_to_user((void __user *) arg, &req_task,
2411 * Submit an IO to the hw
2413 * This function is called by the block layer to issue an io
2414 * to the device. Upon completion, the callback function will
2415 * be called with the data parameter passed as the callback data.
2417 * @dd Pointer to the driver data structure.
2418 * @start First sector to read.
2419 * @nsect Number of sectors to read.
2420 * @nents Number of entries in scatter list for the read command.
2421 * @tag The tag of this read command.
2422 * @callback Pointer to the function that should be called
2423 * when the read completes.
2424 * @data Callback data passed to the callback function
2425 * when the read completes.
2426 * @dir Direction (read or write)
2431 static void mtip_hw_submit_io(struct driver_data *dd, sector_t start,
2432 int nsect, int nents, int tag, void *callback,
2433 void *data, int dir)
2435 struct host_to_dev_fis *fis;
2436 struct mtip_port *port = dd->port;
2437 struct mtip_cmd *command = &port->commands[tag];
2438 int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2440 /* Map the scatter list for DMA access */
2441 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
2443 command->scatter_ents = nents;
2446 * The number of retries for this command before it is
2447 * reported as a failure to the upper layers.
2449 command->retries = MTIP_MAX_RETRIES;
2452 fis = command->command;
2456 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
2457 *((unsigned int *) &fis->lba_low) = (start & 0xFFFFFF);
2458 *((unsigned int *) &fis->lba_low_ex) = ((start >> 24) & 0xFFFFFF);
2459 fis->device = 1 << 6;
2460 fis->features = nsect & 0xFF;
2461 fis->features_ex = (nsect >> 8) & 0xFF;
2462 fis->sect_count = ((tag << 3) | (tag >> 5));
2463 fis->sect_cnt_ex = 0;
2467 fill_command_sg(dd, command, nents);
2469 /* Populate the command header */
2470 command->command_header->opts =
2471 __force_bit2int cpu_to_le32(
2472 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
2473 command->command_header->byte_count = 0;
2476 * Set the completion function and data for the command
2477 * within this layer.
2479 command->comp_data = dd;
2480 command->comp_func = mtip_async_complete;
2481 command->direction = dma_dir;
2484 * Set the completion function and data for the command passed
2485 * from the upper layer.
2487 command->async_data = data;
2488 command->async_callback = callback;
2491 * To prevent this command from being issued
2492 * if an internal command is in progress or error handling is active.
2494 if (port->flags & MTIP_PF_PAUSE_IO) {
2495 set_bit(tag, port->cmds_to_issue);
2496 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
2500 /* Issue the command to the hardware */
2501 mtip_issue_ncq_command(port, tag);
2507 * Release a command slot.
2509 * @dd Pointer to the driver data structure.
2515 static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
2517 release_slot(dd->port, tag);
2521 * Obtain a command slot and return its associated scatter list.
2523 * @dd Pointer to the driver data structure.
2524 * @tag Pointer to an int that will receive the allocated command
2528 * Pointer to the scatter list for the allocated command slot
2529 * or NULL if no command slots are available.
2531 static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2535 * It is possible that, even with this semaphore, a thread
2536 * may think that no command slots are available. Therefore, we
2537 * need to make an attempt to get_slot().
2539 down(&dd->port->cmd_slot);
2540 *tag = get_slot(dd->port);
2542 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
2543 up(&dd->port->cmd_slot);
2546 if (unlikely(*tag < 0)) {
2547 up(&dd->port->cmd_slot);
2551 return dd->port->commands[*tag].sg;
2555 * Sysfs status dump.
2557 * @dev Pointer to the device structure, passed by the kernrel.
2558 * @attr Pointer to the device_attribute structure passed by the kernel.
2559 * @buf Pointer to the char buffer that will receive the stats info.
2562 * The size, in bytes, of the data copied into buf.
2564 static ssize_t mtip_hw_show_status(struct device *dev,
2565 struct device_attribute *attr,
2568 struct driver_data *dd = dev_to_disk(dev)->private_data;
2571 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
2572 size += sprintf(buf, "%s", "thermal_shutdown\n");
2573 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
2574 size += sprintf(buf, "%s", "write_protect\n");
2576 size += sprintf(buf, "%s", "online\n");
2581 static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
2583 static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2584 size_t len, loff_t *offset)
2586 struct driver_data *dd = (struct driver_data *)f->private_data;
2587 char buf[MTIP_DFS_MAX_BUF_SIZE];
2588 u32 group_allocated;
2595 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2597 for (n = dd->slot_groups-1; n >= 0; n--)
2598 size += sprintf(&buf[size], "%08X ",
2599 readl(dd->port->s_active[n]));
2601 size += sprintf(&buf[size], "]\n");
2602 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2604 for (n = dd->slot_groups-1; n >= 0; n--)
2605 size += sprintf(&buf[size], "%08X ",
2606 readl(dd->port->cmd_issue[n]));
2608 size += sprintf(&buf[size], "]\n");
2609 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2611 for (n = dd->slot_groups-1; n >= 0; n--)
2612 size += sprintf(&buf[size], "%08X ",
2613 readl(dd->port->completed[n]));
2615 size += sprintf(&buf[size], "]\n");
2616 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2617 readl(dd->port->mmio + PORT_IRQ_STAT));
2618 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2619 readl(dd->mmio + HOST_IRQ_STAT));
2620 size += sprintf(&buf[size], "\n");
2622 size += sprintf(&buf[size], "L/ Allocated : [ 0x");
2624 for (n = dd->slot_groups-1; n >= 0; n--) {
2625 if (sizeof(long) > sizeof(u32))
2627 dd->port->allocated[n/2] >> (32*(n&1));
2629 group_allocated = dd->port->allocated[n];
2630 size += sprintf(&buf[size], "%08X ", group_allocated);
2632 size += sprintf(&buf[size], "]\n");
2634 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2636 for (n = dd->slot_groups-1; n >= 0; n--) {
2637 if (sizeof(long) > sizeof(u32))
2639 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2641 group_allocated = dd->port->cmds_to_issue[n];
2642 size += sprintf(&buf[size], "%08X ", group_allocated);
2644 size += sprintf(&buf[size], "]\n");
2646 *offset = size <= len ? size : len;
2647 size = copy_to_user(ubuf, buf, *offset);
2654 static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2655 size_t len, loff_t *offset)
2657 struct driver_data *dd = (struct driver_data *)f->private_data;
2658 char buf[MTIP_DFS_MAX_BUF_SIZE];
2664 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2666 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2669 *offset = size <= len ? size : len;
2670 size = copy_to_user(ubuf, buf, *offset);
2677 static const struct file_operations mtip_regs_fops = {
2678 .owner = THIS_MODULE,
2679 .open = simple_open,
2680 .read = mtip_hw_read_registers,
2681 .llseek = no_llseek,
2684 static const struct file_operations mtip_flags_fops = {
2685 .owner = THIS_MODULE,
2686 .open = simple_open,
2687 .read = mtip_hw_read_flags,
2688 .llseek = no_llseek,
2692 * Create the sysfs related attributes.
2694 * @dd Pointer to the driver data structure.
2695 * @kobj Pointer to the kobj for the block device.
2698 * 0 Operation completed successfully.
2699 * -EINVAL Invalid parameter.
2701 static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
2706 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2707 dev_warn(&dd->pdev->dev,
2708 "Error creating 'status' sysfs entry\n");
2713 * Remove the sysfs related attributes.
2715 * @dd Pointer to the driver data structure.
2716 * @kobj Pointer to the kobj for the block device.
2719 * 0 Operation completed successfully.
2720 * -EINVAL Invalid parameter.
2722 static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
2727 sysfs_remove_file(kobj, &dev_attr_status.attr);
2732 static int mtip_hw_debugfs_init(struct driver_data *dd)
2737 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2738 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2739 dev_warn(&dd->pdev->dev,
2740 "Error creating node %s under debugfs\n",
2741 dd->disk->disk_name);
2742 dd->dfs_node = NULL;
2746 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2748 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2754 static void mtip_hw_debugfs_exit(struct driver_data *dd)
2756 debugfs_remove_recursive(dd->dfs_node);
2761 * Perform any init/resume time hardware setup
2763 * @dd Pointer to the driver data structure.
2768 static inline void hba_setup(struct driver_data *dd)
2771 hwdata = readl(dd->mmio + HOST_HSORG);
2773 /* interrupt bug workaround: use only 1 IS bit.*/
2775 HSORG_DISABLE_SLOTGRP_INTR |
2776 HSORG_DISABLE_SLOTGRP_PXIS,
2777 dd->mmio + HOST_HSORG);
2781 * Detect the details of the product, and store anything needed
2782 * into the driver data structure. This includes product type and
2783 * version and number of slot groups.
2785 * @dd Pointer to the driver data structure.
2790 static void mtip_detect_product(struct driver_data *dd)
2793 unsigned int rev, slotgroups;
2796 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2798 * [15:8] hardware/software interface rev#
2799 * [ 3] asic-style interface
2800 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2802 hwdata = readl(dd->mmio + HOST_HSORG);
2804 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2805 dd->slot_groups = 1;
2808 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2809 rev = (hwdata & HSORG_HWREV) >> 8;
2810 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2811 dev_info(&dd->pdev->dev,
2812 "ASIC-FPGA design, HS rev 0x%x, "
2813 "%i slot groups [%i slots]\n",
2818 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2819 dev_warn(&dd->pdev->dev,
2820 "Warning: driver only supports "
2821 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2822 slotgroups = MTIP_MAX_SLOT_GROUPS;
2824 dd->slot_groups = slotgroups;
2828 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2832 * Blocking wait for FTL rebuild to complete
2834 * @dd Pointer to the DRIVER_DATA structure.
2837 * 0 FTL rebuild completed successfully
2838 * -EFAULT FTL rebuild error/timeout/interruption
2840 static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2842 unsigned long timeout, cnt = 0, start;
2844 dev_warn(&dd->pdev->dev,
2845 "FTL rebuild in progress. Polling for completion.\n");
2848 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2851 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
2854 if (mtip_check_surprise_removal(dd->pdev))
2857 if (mtip_get_identify(dd->port, NULL) < 0)
2860 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2861 MTIP_FTL_REBUILD_MAGIC) {
2863 /* Print message every 3 minutes */
2865 dev_warn(&dd->pdev->dev,
2866 "FTL rebuild in progress (%d secs).\n",
2867 jiffies_to_msecs(jiffies - start) / 1000);
2871 dev_warn(&dd->pdev->dev,
2872 "FTL rebuild complete (%d secs).\n",
2873 jiffies_to_msecs(jiffies - start) / 1000);
2874 mtip_block_initialize(dd);
2878 } while (time_before(jiffies, timeout));
2880 /* Check for timeout */
2881 dev_err(&dd->pdev->dev,
2882 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2883 jiffies_to_msecs(jiffies - start) / 1000);
2888 * service thread to issue queued commands
2890 * @data Pointer to the driver data structure.
2896 static int mtip_service_thread(void *data)
2898 struct driver_data *dd = (struct driver_data *)data;
2899 unsigned long slot, slot_start, slot_wrap;
2900 unsigned int num_cmd_slots = dd->slot_groups * 32;
2901 struct mtip_port *port = dd->port;
2905 * the condition is to check neither an internal command is
2906 * is in progress nor error handling is active
2908 wait_event_interruptible(port->svc_wait, (port->flags) &&
2909 !(port->flags & MTIP_PF_PAUSE_IO));
2911 if (kthread_should_stop())
2914 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
2918 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2919 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
2921 /* used to restrict the loop to one iteration */
2922 slot_start = num_cmd_slots;
2925 slot = find_next_bit(port->cmds_to_issue,
2926 num_cmd_slots, slot);
2927 if (slot_wrap == 1) {
2928 if ((slot_start >= slot) ||
2929 (slot >= num_cmd_slots))
2932 if (unlikely(slot_start == num_cmd_slots))
2935 if (unlikely(slot == num_cmd_slots)) {
2941 /* Issue the command to the hardware */
2942 mtip_issue_ncq_command(port, slot);
2944 clear_bit(slot, port->cmds_to_issue);
2947 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
2948 } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
2949 if (!mtip_ftl_rebuild_poll(dd))
2950 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
2952 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
2954 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2956 if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2963 * Called once for each card.
2965 * @dd Pointer to the driver data structure.
2968 * 0 on success, else an error code.
2970 static int mtip_hw_init(struct driver_data *dd)
2974 unsigned int num_command_slots;
2975 unsigned long timeout, timetaken;
2977 struct smart_attr attr242;
2979 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
2981 mtip_detect_product(dd);
2982 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
2986 num_command_slots = dd->slot_groups * 32;
2990 tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
2992 dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
2994 dev_err(&dd->pdev->dev,
2995 "Memory allocation: port structure\n");
2999 /* Counting semaphore to track command slot usage */
3000 sema_init(&dd->port->cmd_slot, num_command_slots - 1);
3002 /* Spinlock to prevent concurrent issue */
3003 spin_lock_init(&dd->port->cmd_issue_lock);
3005 /* Set the port mmio base address. */
3006 dd->port->mmio = dd->mmio + PORT_OFFSET;
3009 /* Allocate memory for the command list. */
3010 dd->port->command_list =
3011 dmam_alloc_coherent(&dd->pdev->dev,
3012 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
3013 &dd->port->command_list_dma,
3015 if (!dd->port->command_list) {
3016 dev_err(&dd->pdev->dev,
3017 "Memory allocation: command list\n");
3022 /* Clear the memory we have allocated. */
3023 memset(dd->port->command_list,
3025 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4));
3027 /* Setup the addresse of the RX FIS. */
3028 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
3029 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
3031 /* Setup the address of the command tables. */
3032 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
3033 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
3035 /* Setup the address of the identify data. */
3036 dd->port->identify = dd->port->command_table +
3038 dd->port->identify_dma = dd->port->command_tbl_dma +
3041 /* Setup the address of the sector buffer - for some non-ncq cmds */
3042 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
3043 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
3045 /* Setup the address of the log buf - for read log command */
3046 dd->port->log_buf = (void *)dd->port->sector_buffer + ATA_SECT_SIZE;
3047 dd->port->log_buf_dma = dd->port->sector_buffer_dma + ATA_SECT_SIZE;
3049 /* Setup the address of the smart buf - for smart read data command */
3050 dd->port->smart_buf = (void *)dd->port->log_buf + ATA_SECT_SIZE;
3051 dd->port->smart_buf_dma = dd->port->log_buf_dma + ATA_SECT_SIZE;
3054 /* Point the command headers at the command tables. */
3055 for (i = 0; i < num_command_slots; i++) {
3056 dd->port->commands[i].command_header =
3057 dd->port->command_list +
3058 (sizeof(struct mtip_cmd_hdr) * i);
3059 dd->port->commands[i].command_header_dma =
3060 dd->port->command_list_dma +
3061 (sizeof(struct mtip_cmd_hdr) * i);
3063 dd->port->commands[i].command =
3064 dd->port->command_table + (HW_CMD_TBL_SZ * i);
3065 dd->port->commands[i].command_dma =
3066 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
3068 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
3069 dd->port->commands[i].command_header->ctbau =
3070 __force_bit2int cpu_to_le32(
3071 (dd->port->commands[i].command_dma >> 16) >> 16);
3072 dd->port->commands[i].command_header->ctba =
3073 __force_bit2int cpu_to_le32(
3074 dd->port->commands[i].command_dma & 0xFFFFFFFF);
3077 * If this is not done, a bug is reported by the stock
3078 * FC11 i386. Due to the fact that it has lots of kernel
3079 * debugging enabled.
3081 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
3083 /* Mark all commands as currently inactive.*/
3084 atomic_set(&dd->port->commands[i].active, 0);
3087 /* Setup the pointers to the extended s_active and CI registers. */
3088 for (i = 0; i < dd->slot_groups; i++) {
3089 dd->port->s_active[i] =
3090 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3091 dd->port->cmd_issue[i] =
3092 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3093 dd->port->completed[i] =
3094 dd->port->mmio + i*0x80 + PORT_SDBV;
3097 timetaken = jiffies;
3098 timeout = jiffies + msecs_to_jiffies(30000);
3099 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3100 time_before(jiffies, timeout)) {
3103 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3104 timetaken = jiffies - timetaken;
3105 dev_warn(&dd->pdev->dev,
3106 "Surprise removal detected at %u ms\n",
3107 jiffies_to_msecs(timetaken));
3111 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
3112 timetaken = jiffies - timetaken;
3113 dev_warn(&dd->pdev->dev,
3114 "Removal detected at %u ms\n",
3115 jiffies_to_msecs(timetaken));
3120 /* Conditionally reset the HBA. */
3121 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3122 if (mtip_hba_reset(dd) < 0) {
3123 dev_err(&dd->pdev->dev,
3124 "Card did not reset within timeout\n");
3129 /* Clear any pending interrupts on the HBA */
3130 writel(readl(dd->mmio + HOST_IRQ_STAT),
3131 dd->mmio + HOST_IRQ_STAT);
3134 mtip_init_port(dd->port);
3135 mtip_start_port(dd->port);
3137 /* Setup the ISR and enable interrupts. */
3138 rv = devm_request_irq(&dd->pdev->dev,
3142 dev_driver_string(&dd->pdev->dev),
3146 dev_err(&dd->pdev->dev,
3147 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3151 /* Enable interrupts on the HBA. */
3152 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3153 dd->mmio + HOST_CTL);
3155 init_timer(&dd->port->cmd_timer);
3156 init_waitqueue_head(&dd->port->svc_wait);
3158 dd->port->cmd_timer.data = (unsigned long int) dd->port;
3159 dd->port->cmd_timer.function = mtip_timeout_function;
3160 mod_timer(&dd->port->cmd_timer,
3161 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
3164 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
3169 if (mtip_get_identify(dd->port, NULL) < 0) {
3174 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3175 MTIP_FTL_REBUILD_MAGIC) {
3176 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3177 return MTIP_FTL_REBUILD_MAGIC;
3179 mtip_dump_identify(dd->port);
3181 /* check write protect, over temp and rebuild statuses */
3182 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3184 dd->port->log_buf_dma, 1);
3186 dev_warn(&dd->pdev->dev,
3187 "Error in READ LOG EXT (10h) command\n");
3188 /* non-critical error, don't fail the load */
3190 buf = (unsigned char *)dd->port->log_buf;
3191 if (buf[259] & 0x1) {
3192 dev_info(&dd->pdev->dev,
3193 "Write protect bit is set.\n");
3194 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3196 if (buf[288] == 0xF7) {
3197 dev_info(&dd->pdev->dev,
3198 "Exceeded Tmax, drive in thermal shutdown.\n");
3199 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3201 if (buf[288] == 0xBF) {
3202 dev_info(&dd->pdev->dev,
3203 "Drive indicates rebuild has failed.\n");
3208 /* get write protect progess */
3209 memset(&attr242, 0, sizeof(struct smart_attr));
3210 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3211 dev_warn(&dd->pdev->dev,
3212 "Unable to check write protect progress\n");
3214 dev_info(&dd->pdev->dev,
3215 "Write protect progress: %u%% (%u blocks)\n",
3216 attr242.cur, le32_to_cpu(attr242.data));
3220 del_timer_sync(&dd->port->cmd_timer);
3222 /* Disable interrupts on the HBA. */
3223 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3224 dd->mmio + HOST_CTL);
3226 /*Release the IRQ. */
3227 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3230 mtip_deinit_port(dd->port);
3232 /* Free the command/command header memory. */
3233 dmam_free_coherent(&dd->pdev->dev,
3234 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
3235 dd->port->command_list,
3236 dd->port->command_list_dma);
3238 /* Free the memory allocated for the for structure. */
3245 * Called to deinitialize an interface.
3247 * @dd Pointer to the driver data structure.
3252 static int mtip_hw_exit(struct driver_data *dd)
3255 * Send standby immediate (E0h) to the drive so that it
3258 if (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
3260 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags))
3261 if (mtip_standby_immediate(dd->port))
3262 dev_warn(&dd->pdev->dev,
3263 "STANDBY IMMEDIATE failed\n");
3265 /* de-initialize the port. */
3266 mtip_deinit_port(dd->port);
3268 /* Disable interrupts on the HBA. */
3269 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3270 dd->mmio + HOST_CTL);
3273 del_timer_sync(&dd->port->cmd_timer);
3275 /* Release the IRQ. */
3276 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3278 /* Stop the bottom half tasklet. */
3279 tasklet_kill(&dd->tasklet);
3281 /* Free the command/command header memory. */
3282 dmam_free_coherent(&dd->pdev->dev,
3283 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
3284 dd->port->command_list,
3285 dd->port->command_list_dma);
3286 /* Free the memory allocated for the for structure. */
3293 * Issue a Standby Immediate command to the device.
3295 * This function is called by the Block Layer just before the
3296 * system powers off during a shutdown.
3298 * @dd Pointer to the driver data structure.
3303 static int mtip_hw_shutdown(struct driver_data *dd)
3306 * Send standby immediate (E0h) to the drive so that it
3309 mtip_standby_immediate(dd->port);
3317 * This function is called by the Block Layer just before the
3318 * system hibernates.
3320 * @dd Pointer to the driver data structure.
3323 * 0 Suspend was successful
3324 * -EFAULT Suspend was not successful
3326 static int mtip_hw_suspend(struct driver_data *dd)
3329 * Send standby immediate (E0h) to the drive
3330 * so that it saves its state.
3332 if (mtip_standby_immediate(dd->port) != 0) {
3333 dev_err(&dd->pdev->dev,
3334 "Failed standby-immediate command\n");
3338 /* Disable interrupts on the HBA.*/
3339 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3340 dd->mmio + HOST_CTL);
3341 mtip_deinit_port(dd->port);
3349 * This function is called by the Block Layer as the
3352 * @dd Pointer to the driver data structure.
3355 * 0 Resume was successful
3356 * -EFAULT Resume was not successful
3358 static int mtip_hw_resume(struct driver_data *dd)
3360 /* Perform any needed hardware setup steps */
3364 if (mtip_hba_reset(dd) != 0) {
3365 dev_err(&dd->pdev->dev,
3366 "Unable to reset the HBA\n");
3371 * Enable the port, DMA engine, and FIS reception specific
3372 * h/w in controller.
3374 mtip_init_port(dd->port);
3375 mtip_start_port(dd->port);
3377 /* Enable interrupts on the HBA.*/
3378 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3379 dd->mmio + HOST_CTL);
3385 * Helper function for reusing disk name
3386 * upon hot insertion.
3388 static int rssd_disk_name_format(char *prefix,
3393 const int base = 'z' - 'a' + 1;
3394 char *begin = buf + strlen(prefix);
3395 char *end = buf + buflen;
3405 *--p = 'a' + (index % unit);
3406 index = (index / unit) - 1;
3407 } while (index >= 0);
3409 memmove(begin, p, end - p);
3410 memcpy(buf, prefix, strlen(prefix));
3416 * Block layer IOCTL handler.
3418 * @dev Pointer to the block_device structure.
3420 * @cmd IOCTL command passed from the user application.
3421 * @arg Argument passed from the user application.
3424 * 0 IOCTL completed successfully.
3425 * -ENOTTY IOCTL not supported or invalid driver data
3426 * structure pointer.
3428 static int mtip_block_ioctl(struct block_device *dev,
3433 struct driver_data *dd = dev->bd_disk->private_data;
3435 if (!capable(CAP_SYS_ADMIN))
3441 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
3448 return mtip_hw_ioctl(dd, cmd, arg);
3452 #ifdef CONFIG_COMPAT
3454 * Block layer compat IOCTL handler.
3456 * @dev Pointer to the block_device structure.
3458 * @cmd IOCTL command passed from the user application.
3459 * @arg Argument passed from the user application.
3462 * 0 IOCTL completed successfully.
3463 * -ENOTTY IOCTL not supported or invalid driver data
3464 * structure pointer.
3466 static int mtip_block_compat_ioctl(struct block_device *dev,
3471 struct driver_data *dd = dev->bd_disk->private_data;
3473 if (!capable(CAP_SYS_ADMIN))
3479 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
3485 case HDIO_DRIVE_TASKFILE: {
3486 struct mtip_compat_ide_task_request_s __user *compat_req_task;
3487 ide_task_request_t req_task;
3488 int compat_tasksize, outtotal, ret;
3491 sizeof(struct mtip_compat_ide_task_request_s);
3494 (struct mtip_compat_ide_task_request_s __user *) arg;
3496 if (copy_from_user(&req_task, (void __user *) arg,
3497 compat_tasksize - (2 * sizeof(compat_long_t))))
3500 if (get_user(req_task.out_size, &compat_req_task->out_size))
3503 if (get_user(req_task.in_size, &compat_req_task->in_size))
3506 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3508 ret = exec_drive_taskfile(dd, (void __user *) arg,
3509 &req_task, outtotal);
3511 if (copy_to_user((void __user *) arg, &req_task,
3513 (2 * sizeof(compat_long_t))))
3516 if (put_user(req_task.out_size, &compat_req_task->out_size))
3519 if (put_user(req_task.in_size, &compat_req_task->in_size))
3525 return mtip_hw_ioctl(dd, cmd, arg);
3531 * Obtain the geometry of the device.
3533 * You may think that this function is obsolete, but some applications,
3534 * fdisk for example still used CHS values. This function describes the
3535 * device as having 224 heads and 56 sectors per cylinder. These values are
3536 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3537 * partition is described in terms of a start and end cylinder this means
3538 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3539 * affects performance.
3541 * @dev Pointer to the block_device strucutre.
3542 * @geo Pointer to a hd_geometry structure.
3545 * 0 Operation completed successfully.
3546 * -ENOTTY An error occurred while reading the drive capacity.
3548 static int mtip_block_getgeo(struct block_device *dev,
3549 struct hd_geometry *geo)
3551 struct driver_data *dd = dev->bd_disk->private_data;
3557 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3558 dev_warn(&dd->pdev->dev,
3559 "Could not get drive capacity.\n");
3565 sector_div(capacity, (geo->heads * geo->sectors));
3566 geo->cylinders = capacity;
3571 * Block device operation function.
3573 * This structure contains pointers to the functions required by the block
3576 static const struct block_device_operations mtip_block_ops = {
3577 .ioctl = mtip_block_ioctl,
3578 #ifdef CONFIG_COMPAT
3579 .compat_ioctl = mtip_block_compat_ioctl,
3581 .getgeo = mtip_block_getgeo,
3582 .owner = THIS_MODULE
3586 * Block layer make request function.
3588 * This function is called by the kernel to process a BIO for
3591 * @queue Pointer to the request queue. Unused other than to obtain
3592 * the driver data structure.
3593 * @bio Pointer to the BIO.
3596 static void mtip_make_request(struct request_queue *queue, struct bio *bio)
3598 struct driver_data *dd = queue->queuedata;
3599 struct scatterlist *sg;
3600 struct bio_vec *bvec;
3604 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3605 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3607 bio_endio(bio, -ENXIO);
3610 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
3611 bio_endio(bio, -ENODATA);
3614 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3616 bio_data_dir(bio))) {
3617 bio_endio(bio, -ENODATA);
3620 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))) {
3621 bio_endio(bio, -ENODATA);
3626 if (unlikely(!bio_has_data(bio))) {
3627 blk_queue_flush(queue, 0);
3632 sg = mtip_hw_get_scatterlist(dd, &tag);
3633 if (likely(sg != NULL)) {
3634 blk_queue_bounce(queue, &bio);
3636 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3637 dev_warn(&dd->pdev->dev,
3638 "Maximum number of SGL entries exceeded\n");
3640 mtip_hw_release_scatterlist(dd, tag);
3644 /* Create the scatter list for this bio. */
3645 bio_for_each_segment(bvec, bio, nents) {
3646 sg_set_page(&sg[nents],
3652 /* Issue the read/write. */
3653 mtip_hw_submit_io(dd,
3666 * Block layer initialization function.
3668 * This function is called once by the PCI layer for each P320
3669 * device that is connected to the system.
3671 * @dd Pointer to the driver data structure.
3674 * 0 on success else an error code.
3676 static int mtip_block_initialize(struct driver_data *dd)
3678 int rv = 0, wait_for_rebuild = 0;
3680 unsigned int index = 0;
3681 struct kobject *kobj;
3682 unsigned char thd_name[16];
3685 goto skip_create_disk; /* hw init done, before rebuild */
3687 /* Initialize the protocol layer. */
3688 wait_for_rebuild = mtip_hw_init(dd);
3689 if (wait_for_rebuild < 0) {
3690 dev_err(&dd->pdev->dev,
3691 "Protocol layer initialization failed\n");
3693 goto protocol_init_error;
3696 dd->disk = alloc_disk(MTIP_MAX_MINORS);
3697 if (dd->disk == NULL) {
3698 dev_err(&dd->pdev->dev,
3699 "Unable to allocate gendisk structure\n");
3701 goto alloc_disk_error;
3704 /* Generate the disk name, implemented same as in sd.c */
3706 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3709 spin_lock(&rssd_index_lock);
3710 rv = ida_get_new(&rssd_index_ida, &index);
3711 spin_unlock(&rssd_index_lock);
3712 } while (rv == -EAGAIN);
3717 rv = rssd_disk_name_format("rssd",
3719 dd->disk->disk_name,
3722 goto disk_index_error;
3724 dd->disk->driverfs_dev = &dd->pdev->dev;
3725 dd->disk->major = dd->major;
3726 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3727 dd->disk->fops = &mtip_block_ops;
3728 dd->disk->private_data = dd;
3732 * if rebuild pending, start the service thread, and delay the block
3733 * queue creation and add_disk()
3735 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3736 goto start_service_thread;
3739 /* Allocate the request queue. */
3740 dd->queue = blk_alloc_queue(GFP_KERNEL);
3741 if (dd->queue == NULL) {
3742 dev_err(&dd->pdev->dev,
3743 "Unable to allocate request queue\n");
3745 goto block_queue_alloc_init_error;
3748 /* Attach our request function to the request queue. */
3749 blk_queue_make_request(dd->queue, mtip_make_request);
3751 dd->disk->queue = dd->queue;
3752 dd->queue->queuedata = dd;
3754 /* Set device limits. */
3755 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3756 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3757 blk_queue_physical_block_size(dd->queue, 4096);
3758 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3759 blk_queue_max_segment_size(dd->queue, 0x400000);
3760 blk_queue_io_min(dd->queue, 4096);
3763 * write back cache is not supported in the device. FUA depends on
3764 * write back cache support, hence setting flush support to zero.
3766 blk_queue_flush(dd->queue, 0);
3768 /* Set the capacity of the device in 512 byte sectors. */
3769 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3770 dev_warn(&dd->pdev->dev,
3771 "Could not read drive capacity\n");
3773 goto read_capacity_error;
3775 set_capacity(dd->disk, capacity);
3777 /* Enable the block device and add it to /dev */
3781 * Now that the disk is active, initialize any sysfs attributes
3782 * managed by the protocol layer.
3784 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3786 mtip_hw_sysfs_init(dd, kobj);
3789 mtip_hw_debugfs_init(dd);
3791 if (dd->mtip_svc_handler) {
3792 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
3793 return rv; /* service thread created for handling rebuild */
3796 start_service_thread:
3797 sprintf(thd_name, "mtip_svc_thd_%02d", index);
3799 dd->mtip_svc_handler = kthread_run(mtip_service_thread,
3802 if (IS_ERR(dd->mtip_svc_handler)) {
3803 dev_err(&dd->pdev->dev, "service thread failed to start\n");
3804 dd->mtip_svc_handler = NULL;
3806 goto kthread_run_error;
3809 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3810 rv = wait_for_rebuild;
3815 mtip_hw_debugfs_exit(dd);
3817 /* Delete our gendisk. This also removes the device from /dev */
3818 del_gendisk(dd->disk);
3820 read_capacity_error:
3821 blk_cleanup_queue(dd->queue);
3823 block_queue_alloc_init_error:
3825 spin_lock(&rssd_index_lock);
3826 ida_remove(&rssd_index_ida, index);
3827 spin_unlock(&rssd_index_lock);
3833 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
3835 protocol_init_error:
3840 * Block layer deinitialization function.
3842 * Called by the PCI layer as each P320 device is removed.
3844 * @dd Pointer to the driver data structure.
3849 static int mtip_block_remove(struct driver_data *dd)
3851 struct kobject *kobj;
3853 if (dd->mtip_svc_handler) {
3854 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
3855 wake_up_interruptible(&dd->port->svc_wait);
3856 kthread_stop(dd->mtip_svc_handler);
3859 /* Clean up the sysfs attributes, if created */
3860 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
3861 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3863 mtip_hw_sysfs_exit(dd, kobj);
3867 mtip_hw_debugfs_exit(dd);
3870 * Delete our gendisk structure. This also removes the device
3873 del_gendisk(dd->disk);
3875 spin_lock(&rssd_index_lock);
3876 ida_remove(&rssd_index_ida, dd->index);
3877 spin_unlock(&rssd_index_lock);
3879 blk_cleanup_queue(dd->queue);
3883 /* De-initialize the protocol layer. */
3890 * Function called by the PCI layer when just before the
3891 * machine shuts down.
3893 * If a protocol layer shutdown function is present it will be called
3896 * @dd Pointer to the driver data structure.
3901 static int mtip_block_shutdown(struct driver_data *dd)
3903 dev_info(&dd->pdev->dev,
3904 "Shutting down %s ...\n", dd->disk->disk_name);
3906 /* Delete our gendisk structure, and cleanup the blk queue. */
3907 del_gendisk(dd->disk);
3909 spin_lock(&rssd_index_lock);
3910 ida_remove(&rssd_index_ida, dd->index);
3911 spin_unlock(&rssd_index_lock);
3913 blk_cleanup_queue(dd->queue);
3917 mtip_hw_shutdown(dd);
3921 static int mtip_block_suspend(struct driver_data *dd)
3923 dev_info(&dd->pdev->dev,
3924 "Suspending %s ...\n", dd->disk->disk_name);
3925 mtip_hw_suspend(dd);
3929 static int mtip_block_resume(struct driver_data *dd)
3931 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
3932 dd->disk->disk_name);
3938 * Called for each supported PCI device detected.
3940 * This function allocates the private data structure, enables the
3941 * PCI device and then calls the block layer initialization function.
3944 * 0 on success else an error code.
3946 static int mtip_pci_probe(struct pci_dev *pdev,
3947 const struct pci_device_id *ent)
3950 struct driver_data *dd = NULL;
3952 /* Allocate memory for this devices private data. */
3953 dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
3956 "Unable to allocate memory for driver data\n");
3960 /* Attach the private data to this PCI device. */
3961 pci_set_drvdata(pdev, dd);
3963 rv = pcim_enable_device(pdev);
3965 dev_err(&pdev->dev, "Unable to enable device\n");
3969 /* Map BAR5 to memory. */
3970 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
3972 dev_err(&pdev->dev, "Unable to map regions\n");
3976 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
3977 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3980 rv = pci_set_consistent_dma_mask(pdev,
3983 dev_warn(&pdev->dev,
3984 "64-bit DMA enable failed\n");
3990 pci_set_master(pdev);
3992 if (pci_enable_msi(pdev)) {
3993 dev_warn(&pdev->dev,
3994 "Unable to enable MSI interrupt.\n");
3995 goto block_initialize_err;
3998 /* Copy the info we may need later into the private data structure. */
3999 dd->major = mtip_major;
4000 dd->instance = instance;
4003 /* Initialize the block layer. */
4004 rv = mtip_block_initialize(dd);
4007 "Unable to initialize block layer\n");
4008 goto block_initialize_err;
4012 * Increment the instance count so that each device has a unique
4016 if (rv != MTIP_FTL_REBUILD_MAGIC)
4017 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
4020 block_initialize_err:
4021 pci_disable_msi(pdev);
4024 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4028 pci_set_drvdata(pdev, NULL);
4035 * Called for each probed device when the device is removed or the
4036 * driver is unloaded.
4041 static void mtip_pci_remove(struct pci_dev *pdev)
4043 struct driver_data *dd = pci_get_drvdata(pdev);
4046 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
4048 if (mtip_check_surprise_removal(pdev)) {
4049 while (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
4052 if (counter == 10) {
4053 /* Cleanup the outstanding commands */
4054 mtip_command_cleanup(dd);
4060 /* Clean up the block layer. */
4061 mtip_block_remove(dd);
4063 pci_disable_msi(pdev);
4066 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4070 * Called for each probed device when the device is suspended.
4076 static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4079 struct driver_data *dd = pci_get_drvdata(pdev);
4083 "Driver private datastructure is NULL\n");
4087 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
4089 /* Disable ports & interrupts then send standby immediate */
4090 rv = mtip_block_suspend(dd);
4093 "Failed to suspend controller\n");
4098 * Save the pci config space to pdev structure &
4099 * disable the device
4101 pci_save_state(pdev);
4102 pci_disable_device(pdev);
4104 /* Move to Low power state*/
4105 pci_set_power_state(pdev, PCI_D3hot);
4111 * Called for each probed device when the device is resumed.
4117 static int mtip_pci_resume(struct pci_dev *pdev)
4120 struct driver_data *dd;
4122 dd = pci_get_drvdata(pdev);
4125 "Driver private datastructure is NULL\n");
4129 /* Move the device to active State */
4130 pci_set_power_state(pdev, PCI_D0);
4132 /* Restore PCI configuration space */
4133 pci_restore_state(pdev);
4135 /* Enable the PCI device*/
4136 rv = pcim_enable_device(pdev);
4139 "Failed to enable card during resume\n");
4142 pci_set_master(pdev);
4145 * Calls hbaReset, initPort, & startPort function
4146 * then enables interrupts
4148 rv = mtip_block_resume(dd);
4150 dev_err(&pdev->dev, "Unable to resume\n");
4153 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
4164 static void mtip_pci_shutdown(struct pci_dev *pdev)
4166 struct driver_data *dd = pci_get_drvdata(pdev);
4168 mtip_block_shutdown(dd);
4171 /* Table of device ids supported by this driver. */
4172 static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
4173 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4174 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4175 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4176 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4177 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4178 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4179 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
4183 /* Structure that describes the PCI driver functions. */
4184 static struct pci_driver mtip_pci_driver = {
4185 .name = MTIP_DRV_NAME,
4186 .id_table = mtip_pci_tbl,
4187 .probe = mtip_pci_probe,
4188 .remove = mtip_pci_remove,
4189 .suspend = mtip_pci_suspend,
4190 .resume = mtip_pci_resume,
4191 .shutdown = mtip_pci_shutdown,
4194 MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4197 * Module initialization function.
4199 * Called once when the module is loaded. This function allocates a major
4200 * block device number to the Cyclone devices and registers the PCI layer
4204 * 0 on success else error code.
4206 static int __init mtip_init(void)
4210 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
4212 /* Allocate a major block device number to use with this driver. */
4213 error = register_blkdev(0, MTIP_DRV_NAME);
4215 pr_err("Unable to register block device (%d)\n",
4222 dfs_parent = debugfs_create_dir("rssd", NULL);
4223 if (IS_ERR_OR_NULL(dfs_parent)) {
4224 pr_warn("Error creating debugfs parent\n");
4229 /* Register our PCI operations. */
4230 error = pci_register_driver(&mtip_pci_driver);
4232 debugfs_remove(dfs_parent);
4233 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4240 * Module de-initialization function.
4242 * Called once when the module is unloaded. This function deallocates
4243 * the major block device number allocated by mtip_init() and
4244 * unregisters the PCI layer of the driver.
4249 static void __exit mtip_exit(void)
4251 debugfs_remove_recursive(dfs_parent);
4253 /* Release the allocated major block device number. */
4254 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4256 /* Unregister the PCI driver. */
4257 pci_unregister_driver(&mtip_pci_driver);
4260 MODULE_AUTHOR("Micron Technology, Inc");
4261 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4262 MODULE_LICENSE("GPL");
4263 MODULE_VERSION(MTIP_DRV_VERSION);
4265 module_init(mtip_init);
4266 module_exit(mtip_exit);