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;
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));
575 num_command_slots = port->dd->slot_groups * 32;
577 for (tag = 0; tag < num_command_slots; tag++) {
579 * Skip internal command slot as it has
580 * its own timeout mechanism
582 if (tag == MTIP_TAG_INTERNAL)
585 if (atomic_read(&port->commands[tag].active) &&
586 (time_after(jiffies, port->commands[tag].comp_time))) {
590 command = &port->commands[tag];
591 fis = (struct host_to_dev_fis *) command->command;
593 set_bit(tag, tagaccum);
596 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
599 * Clear the completed bit. This should prevent
600 * any interrupt handlers from trying to retire
603 writel(1 << bit, port->completed[group]);
605 /* Call the async completion callback. */
606 if (likely(command->async_callback))
607 command->async_callback(command->async_data,
609 command->async_callback = NULL;
610 command->comp_func = NULL;
612 /* Unmap the DMA scatter list entries */
613 dma_unmap_sg(&port->dd->pdev->dev,
615 command->scatter_ents,
619 * Clear the allocated bit and active tag for the
622 atomic_set(&port->commands[tag].active, 0);
623 release_slot(port, tag);
629 if (cmdto_cnt && !test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
630 print_tags(port->dd, "timed out", tagaccum, cmdto_cnt);
632 mtip_restart_port(port);
633 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
634 wake_up_interruptible(&port->svc_wait);
637 if (port->ic_pause_timer) {
638 to = port->ic_pause_timer + msecs_to_jiffies(1000);
639 if (time_after(jiffies, to)) {
640 if (!test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
641 port->ic_pause_timer = 0;
642 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
643 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
644 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
645 wake_up_interruptible(&port->svc_wait);
652 /* Restart the timer */
653 mod_timer(&port->cmd_timer,
654 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
658 * IO completion function.
660 * This completion function is called by the driver ISR when a
661 * command that was issued by the kernel completes. It first calls the
662 * asynchronous completion function which normally calls back into the block
663 * layer passing the asynchronous callback data, then unmaps the
664 * scatter list associated with the completed command, and finally
665 * clears the allocated bit associated with the completed command.
667 * @port Pointer to the port data structure.
668 * @tag Tag of the command.
669 * @data Pointer to driver_data.
670 * @status Completion status.
675 static void mtip_async_complete(struct mtip_port *port,
680 struct mtip_cmd *command;
681 struct driver_data *dd = data;
682 int cb_status = status ? -EIO : 0;
684 if (unlikely(!dd) || unlikely(!port))
687 command = &port->commands[tag];
689 if (unlikely(status == PORT_IRQ_TF_ERR)) {
690 dev_warn(&port->dd->pdev->dev,
691 "Command tag %d failed due to TFE\n", tag);
694 /* Upper layer callback */
695 if (likely(command->async_callback))
696 command->async_callback(command->async_data, cb_status);
698 command->async_callback = NULL;
699 command->comp_func = NULL;
701 /* Unmap the DMA scatter list entries */
702 dma_unmap_sg(&dd->pdev->dev,
704 command->scatter_ents,
707 /* Clear the allocated and active bits for the command */
708 atomic_set(&port->commands[tag].active, 0);
709 release_slot(port, tag);
715 * Internal command completion callback function.
717 * This function is normally called by the driver ISR when an internal
718 * command completed. This function signals the command completion by
719 * calling complete().
721 * @port Pointer to the port data structure.
722 * @tag Tag of the command that has completed.
723 * @data Pointer to a completion structure.
724 * @status Completion status.
729 static void mtip_completion(struct mtip_port *port,
734 struct mtip_cmd *command = &port->commands[tag];
735 struct completion *waiting = data;
736 if (unlikely(status == PORT_IRQ_TF_ERR))
737 dev_warn(&port->dd->pdev->dev,
738 "Internal command %d completed with TFE\n", tag);
740 command->async_callback = NULL;
741 command->comp_func = NULL;
746 static void mtip_null_completion(struct mtip_port *port,
754 static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
755 dma_addr_t buffer_dma, unsigned int sectors);
756 static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
757 struct smart_attr *attrib);
761 * @dd Pointer to the DRIVER_DATA structure.
766 static void mtip_handle_tfe(struct driver_data *dd)
768 int group, tag, bit, reissue, rv;
769 struct mtip_port *port;
770 struct mtip_cmd *cmd;
772 struct host_to_dev_fis *fis;
773 unsigned long tagaccum[SLOTBITS_IN_LONGS];
774 unsigned int cmd_cnt = 0;
776 char *fail_reason = NULL;
777 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
779 dev_warn(&dd->pdev->dev, "Taskfile error\n");
783 /* Stop the timer to prevent command timeouts. */
784 del_timer(&port->cmd_timer);
785 set_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
787 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
788 test_bit(MTIP_TAG_INTERNAL, port->allocated)) {
789 cmd = &port->commands[MTIP_TAG_INTERNAL];
790 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
792 atomic_inc(&cmd->active); /* active > 1 indicates error */
793 if (cmd->comp_data && cmd->comp_func) {
794 cmd->comp_func(port, MTIP_TAG_INTERNAL,
795 cmd->comp_data, PORT_IRQ_TF_ERR);
797 goto handle_tfe_exit;
800 /* clear the tag accumulator */
801 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
803 /* Loop through all the groups */
804 for (group = 0; group < dd->slot_groups; group++) {
805 completed = readl(port->completed[group]);
807 /* clear completed status register in the hardware.*/
808 writel(completed, port->completed[group]);
810 /* Process successfully completed commands */
811 for (bit = 0; bit < 32 && completed; bit++) {
812 if (!(completed & (1<<bit)))
814 tag = (group << 5) + bit;
816 /* Skip the internal command slot */
817 if (tag == MTIP_TAG_INTERNAL)
820 cmd = &port->commands[tag];
821 if (likely(cmd->comp_func)) {
822 set_bit(tag, tagaccum);
824 atomic_set(&cmd->active, 0);
830 dev_err(&port->dd->pdev->dev,
831 "Missing completion func for tag %d",
833 if (mtip_check_surprise_removal(dd->pdev)) {
834 mtip_command_cleanup(dd);
835 /* don't proceed further */
842 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
844 /* Restart the port */
846 mtip_restart_port(port);
848 /* Trying to determine the cause of the error */
849 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
851 dd->port->log_buf_dma, 1);
853 dev_warn(&dd->pdev->dev,
854 "Error in READ LOG EXT (10h) command\n");
855 /* non-critical error, don't fail the load */
857 buf = (unsigned char *)dd->port->log_buf;
858 if (buf[259] & 0x1) {
859 dev_info(&dd->pdev->dev,
860 "Write protect bit is set.\n");
861 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
862 fail_all_ncq_write = 1;
863 fail_reason = "write protect";
865 if (buf[288] == 0xF7) {
866 dev_info(&dd->pdev->dev,
867 "Exceeded Tmax, drive in thermal shutdown.\n");
868 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
869 fail_all_ncq_cmds = 1;
870 fail_reason = "thermal shutdown";
872 if (buf[288] == 0xBF) {
873 dev_info(&dd->pdev->dev,
874 "Drive indicates rebuild has failed.\n");
875 fail_all_ncq_cmds = 1;
876 fail_reason = "rebuild failed";
880 /* clear the tag accumulator */
881 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
883 /* Loop through all the groups */
884 for (group = 0; group < dd->slot_groups; group++) {
885 for (bit = 0; bit < 32; bit++) {
887 tag = (group << 5) + bit;
888 cmd = &port->commands[tag];
890 /* If the active bit is set re-issue the command */
891 if (atomic_read(&cmd->active) == 0)
894 fis = (struct host_to_dev_fis *)cmd->command;
896 /* Should re-issue? */
897 if (tag == MTIP_TAG_INTERNAL ||
898 fis->command == ATA_CMD_SET_FEATURES)
901 if (fail_all_ncq_cmds ||
902 (fail_all_ncq_write &&
903 fis->command == ATA_CMD_FPDMA_WRITE)) {
904 dev_warn(&dd->pdev->dev,
905 " Fail: %s w/tag %d [%s].\n",
906 fis->command == ATA_CMD_FPDMA_WRITE ?
909 fail_reason != NULL ?
910 fail_reason : "unknown");
911 atomic_set(&cmd->active, 0);
912 if (cmd->comp_func) {
913 cmd->comp_func(port, tag,
922 * First check if this command has
923 * exceeded its retries.
925 if (reissue && (cmd->retries-- > 0)) {
927 set_bit(tag, tagaccum);
929 /* Re-issue the command. */
930 mtip_issue_ncq_command(port, tag);
935 /* Retire a command that will not be reissued */
936 dev_warn(&port->dd->pdev->dev,
937 "retiring tag %d\n", tag);
938 atomic_set(&cmd->active, 0);
947 dev_warn(&port->dd->pdev->dev,
948 "Bad completion for tag %d\n",
952 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
955 /* clear eh_active */
956 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
957 wake_up_interruptible(&port->svc_wait);
959 mod_timer(&port->cmd_timer,
960 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
964 * Handle a set device bits interrupt
966 static inline void mtip_process_sdbf(struct driver_data *dd)
968 struct mtip_port *port = dd->port;
971 struct mtip_cmd *command;
973 /* walk all bits in all slot groups */
974 for (group = 0; group < dd->slot_groups; group++) {
975 completed = readl(port->completed[group]);
979 /* clear completed status register in the hardware.*/
980 writel(completed, port->completed[group]);
982 /* Process completed commands. */
984 (bit < 32) && completed;
985 bit++, completed >>= 1) {
986 if (completed & 0x01) {
987 tag = (group << 5) | bit;
989 /* skip internal command slot. */
990 if (unlikely(tag == MTIP_TAG_INTERNAL))
993 command = &port->commands[tag];
994 /* make internal callback */
995 if (likely(command->comp_func)) {
1002 dev_warn(&dd->pdev->dev,
1007 if (mtip_check_surprise_removal(
1009 mtip_command_cleanup(dd);
1019 * Process legacy pio and d2h interrupts
1021 static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
1023 struct mtip_port *port = dd->port;
1024 struct mtip_cmd *cmd = &port->commands[MTIP_TAG_INTERNAL];
1026 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
1027 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1028 & (1 << MTIP_TAG_INTERNAL))) {
1029 if (cmd->comp_func) {
1030 cmd->comp_func(port,
1042 * Demux and handle errors
1044 static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
1046 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR)))
1047 mtip_handle_tfe(dd);
1049 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
1050 dev_warn(&dd->pdev->dev,
1051 "Clearing PxSERR.DIAG.x\n");
1052 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
1055 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
1056 dev_warn(&dd->pdev->dev,
1057 "Clearing PxSERR.DIAG.n\n");
1058 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
1061 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
1062 dev_warn(&dd->pdev->dev,
1063 "Port stat errors %x unhandled\n",
1064 (port_stat & ~PORT_IRQ_HANDLED));
1068 static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
1070 struct driver_data *dd = (struct driver_data *) data;
1071 struct mtip_port *port = dd->port;
1072 u32 hba_stat, port_stat;
1075 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1079 /* Acknowledge the interrupt status on the port.*/
1080 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1081 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1083 /* Demux port status */
1084 if (likely(port_stat & PORT_IRQ_SDB_FIS))
1085 mtip_process_sdbf(dd);
1087 if (unlikely(port_stat & PORT_IRQ_ERR)) {
1088 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
1089 mtip_command_cleanup(dd);
1090 /* don't proceed further */
1093 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1097 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
1100 if (unlikely(port_stat & PORT_IRQ_LEGACY))
1101 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
1104 /* acknowledge interrupt */
1105 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
1111 * Wrapper for mtip_handle_irq
1112 * (ignores return code)
1114 static void mtip_tasklet(unsigned long data)
1116 mtip_handle_irq((struct driver_data *) data);
1120 * HBA interrupt subroutine.
1123 * @instance Pointer to the driver data structure.
1126 * IRQ_HANDLED A HBA interrupt was pending and handled.
1127 * IRQ_NONE This interrupt was not for the HBA.
1129 static irqreturn_t mtip_irq_handler(int irq, void *instance)
1131 struct driver_data *dd = instance;
1132 tasklet_schedule(&dd->tasklet);
1136 static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
1138 atomic_set(&port->commands[tag].active, 1);
1139 writel(1 << MTIP_TAG_BIT(tag),
1140 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
1143 static bool mtip_pause_ncq(struct mtip_port *port,
1144 struct host_to_dev_fis *fis)
1146 struct host_to_dev_fis *reply;
1147 unsigned long task_file_data;
1149 reply = port->rxfis + RX_FIS_D2H_REG;
1150 task_file_data = readl(port->mmio+PORT_TFDATA);
1152 if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
1153 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1155 if ((task_file_data & 1))
1158 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
1159 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1160 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1161 port->ic_pause_timer = jiffies;
1163 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1164 (fis->features == 0x03)) {
1165 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1166 port->ic_pause_timer = jiffies;
1168 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1169 ((fis->command == 0xFC) &&
1170 (fis->features == 0x27 || fis->features == 0x72 ||
1171 fis->features == 0x62 || fis->features == 0x26))) {
1172 /* Com reset after secure erase or lowlevel format */
1173 mtip_restart_port(port);
1181 * Wait for port to quiesce
1183 * @port Pointer to port data structure
1184 * @timeout Max duration to wait (ms)
1188 * -EBUSY Commands still active
1190 static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1194 unsigned int active = 1;
1196 to = jiffies + msecs_to_jiffies(timeout);
1198 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1199 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
1201 continue; /* svc thd is actively issuing commands */
1203 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
1206 * Ignore s_active bit 0 of array element 0.
1207 * This bit will always be set
1209 active = readl(port->s_active[0]) & 0xFFFFFFFE;
1210 for (n = 1; n < port->dd->slot_groups; n++)
1211 active |= readl(port->s_active[n]);
1217 } while (time_before(jiffies, to));
1219 return active ? -EBUSY : 0;
1223 * Execute an internal command and wait for the completion.
1225 * @port Pointer to the port data structure.
1226 * @fis Pointer to the FIS that describes the command.
1227 * @fis_len Length in WORDS of the FIS.
1228 * @buffer DMA accessible for command data.
1229 * @buf_len Length, in bytes, of the data buffer.
1230 * @opts Command header options, excluding the FIS length
1231 * and the number of PRD entries.
1232 * @timeout Time in ms to wait for the command to complete.
1235 * 0 Command completed successfully.
1236 * -EFAULT The buffer address is not correctly aligned.
1237 * -EBUSY Internal command or other IO in progress.
1238 * -EAGAIN Time out waiting for command to complete.
1240 static int mtip_exec_internal_command(struct mtip_port *port,
1241 struct host_to_dev_fis *fis,
1247 unsigned long timeout)
1249 struct mtip_cmd_sg *command_sg;
1250 DECLARE_COMPLETION_ONSTACK(wait);
1251 int rv = 0, ready2go = 1;
1252 struct mtip_cmd *int_cmd = &port->commands[MTIP_TAG_INTERNAL];
1255 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1256 if (buffer & 0x00000007) {
1257 dev_err(&port->dd->pdev->dev,
1258 "SG buffer is not 8 byte aligned\n");
1262 to = jiffies + msecs_to_jiffies(timeout);
1264 ready2go = !test_and_set_bit(MTIP_TAG_INTERNAL,
1269 } while (time_before(jiffies, to));
1271 dev_warn(&port->dd->pdev->dev,
1272 "Internal cmd active. new cmd [%02X]\n", fis->command);
1275 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1276 port->ic_pause_timer = 0;
1278 if (fis->command == ATA_CMD_SEC_ERASE_UNIT)
1279 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1280 else if (fis->command == ATA_CMD_DOWNLOAD_MICRO)
1281 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1283 if (atomic == GFP_KERNEL) {
1284 if (fis->command != ATA_CMD_STANDBYNOW1) {
1285 /* wait for io to complete if non atomic */
1286 if (mtip_quiesce_io(port, 5000) < 0) {
1287 dev_warn(&port->dd->pdev->dev,
1288 "Failed to quiesce IO\n");
1289 release_slot(port, MTIP_TAG_INTERNAL);
1290 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1291 wake_up_interruptible(&port->svc_wait);
1296 /* Set the completion function and data for the command. */
1297 int_cmd->comp_data = &wait;
1298 int_cmd->comp_func = mtip_completion;
1301 /* Clear completion - we're going to poll */
1302 int_cmd->comp_data = NULL;
1303 int_cmd->comp_func = mtip_null_completion;
1306 /* Copy the command to the command table */
1307 memcpy(int_cmd->command, fis, fis_len*4);
1309 /* Populate the SG list */
1310 int_cmd->command_header->opts =
1311 __force_bit2int cpu_to_le32(opts | fis_len);
1313 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1316 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1318 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1319 command_sg->dba_upper =
1320 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
1322 int_cmd->command_header->opts |=
1323 __force_bit2int cpu_to_le32((1 << 16));
1326 /* Populate the command header */
1327 int_cmd->command_header->byte_count = 0;
1329 /* Issue the command to the hardware */
1330 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1332 /* Poll if atomic, wait_for_completion otherwise */
1333 if (atomic == GFP_KERNEL) {
1334 /* Wait for the command to complete or timeout. */
1335 if (wait_for_completion_timeout(
1337 msecs_to_jiffies(timeout)) == 0) {
1338 dev_err(&port->dd->pdev->dev,
1339 "Internal command did not complete [%d] "
1340 "within timeout of %lu ms\n",
1342 if (mtip_check_surprise_removal(port->dd->pdev) ||
1343 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1344 &port->dd->dd_flag)) {
1351 /* Spin for <timeout> checking if command still outstanding */
1352 timeout = jiffies + msecs_to_jiffies(timeout);
1353 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1354 & (1 << MTIP_TAG_INTERNAL))
1355 && time_before(jiffies, timeout)) {
1356 if (mtip_check_surprise_removal(port->dd->pdev)) {
1360 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
1361 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1362 &port->dd->dd_flag)) {
1366 if (readl(port->mmio + PORT_IRQ_STAT) & PORT_IRQ_ERR) {
1367 atomic_inc(&int_cmd->active); /* error */
1373 if (atomic_read(&int_cmd->active) > 1) {
1374 dev_err(&port->dd->pdev->dev,
1375 "Internal command [%02X] failed\n", fis->command);
1378 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1379 & (1 << MTIP_TAG_INTERNAL)) {
1381 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1382 &port->dd->dd_flag)) {
1383 mtip_restart_port(port);
1388 /* Clear the allocated and active bits for the internal command. */
1389 atomic_set(&int_cmd->active, 0);
1390 release_slot(port, MTIP_TAG_INTERNAL);
1391 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1395 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1396 wake_up_interruptible(&port->svc_wait);
1402 * Byte-swap ATA ID strings.
1404 * ATA identify data contains strings in byte-swapped 16-bit words.
1405 * They must be swapped (on all architectures) to be usable as C strings.
1406 * This function swaps bytes in-place.
1408 * @buf The buffer location of the string
1409 * @len The number of bytes to swap
1414 static inline void ata_swap_string(u16 *buf, unsigned int len)
1417 for (i = 0; i < (len/2); i++)
1418 be16_to_cpus(&buf[i]);
1422 * Request the device identity information.
1424 * If a user space buffer is not specified, i.e. is NULL, the
1425 * identify information is still read from the drive and placed
1426 * into the identify data buffer (@e port->identify) in the
1427 * port data structure.
1428 * When the identify buffer contains valid identify information @e
1429 * port->identify_valid is non-zero.
1431 * @port Pointer to the port structure.
1432 * @user_buffer A user space buffer where the identify data should be
1436 * 0 Command completed successfully.
1437 * -EFAULT An error occurred while coping data to the user buffer.
1438 * -1 Command failed.
1440 static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1443 struct host_to_dev_fis fis;
1445 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
1448 /* Build the FIS. */
1449 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1452 fis.command = ATA_CMD_ID_ATA;
1454 /* Set the identify information as invalid. */
1455 port->identify_valid = 0;
1457 /* Clear the identify information. */
1458 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1460 /* Execute the command. */
1461 if (mtip_exec_internal_command(port,
1465 sizeof(u16) * ATA_ID_WORDS,
1468 MTIP_INTERNAL_COMMAND_TIMEOUT_MS)
1475 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1476 * perform field-sensitive swapping on the string fields.
1477 * See the kernel use of ata_id_string() for proof of this.
1479 #ifdef __LITTLE_ENDIAN
1480 ata_swap_string(port->identify + 27, 40); /* model string*/
1481 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1482 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1486 for (i = 0; i < ATA_ID_WORDS; i++)
1487 port->identify[i] = le16_to_cpu(port->identify[i]);
1491 /* Set the identify buffer as valid. */
1492 port->identify_valid = 1;
1498 ATA_ID_WORDS * sizeof(u16))) {
1509 * Issue a standby immediate command to the device.
1511 * @port Pointer to the port structure.
1514 * 0 Command was executed successfully.
1515 * -1 An error occurred while executing the command.
1517 static int mtip_standby_immediate(struct mtip_port *port)
1520 struct host_to_dev_fis fis;
1521 unsigned long start;
1523 /* Build the FIS. */
1524 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1527 fis.command = ATA_CMD_STANDBYNOW1;
1530 rv = mtip_exec_internal_command(port,
1538 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1539 jiffies_to_msecs(jiffies - start));
1541 dev_warn(&port->dd->pdev->dev,
1542 "STANDBY IMMEDIATE command failed.\n");
1548 * Issue a READ LOG EXT command to the device.
1550 * @port pointer to the port structure.
1551 * @page page number to fetch
1552 * @buffer pointer to buffer
1553 * @buffer_dma dma address corresponding to @buffer
1554 * @sectors page length to fetch, in sectors
1557 * @rv return value from mtip_exec_internal_command()
1559 static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1560 dma_addr_t buffer_dma, unsigned int sectors)
1562 struct host_to_dev_fis fis;
1564 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1567 fis.command = ATA_CMD_READ_LOG_EXT;
1568 fis.sect_count = sectors & 0xFF;
1569 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1572 fis.device = ATA_DEVICE_OBS;
1574 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1576 return mtip_exec_internal_command(port,
1580 sectors * ATA_SECT_SIZE,
1583 MTIP_INTERNAL_COMMAND_TIMEOUT_MS);
1587 * Issue a SMART READ DATA command to the device.
1589 * @port pointer to the port structure.
1590 * @buffer pointer to buffer
1591 * @buffer_dma dma address corresponding to @buffer
1594 * @rv return value from mtip_exec_internal_command()
1596 static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1597 dma_addr_t buffer_dma)
1599 struct host_to_dev_fis fis;
1601 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1604 fis.command = ATA_CMD_SMART;
1605 fis.features = 0xD0;
1609 fis.device = ATA_DEVICE_OBS;
1611 return mtip_exec_internal_command(port,
1622 * Get the value of a smart attribute
1624 * @port pointer to the port structure
1625 * @id attribute number
1626 * @attrib pointer to return attrib information corresponding to @id
1629 * -EINVAL NULL buffer passed or unsupported attribute @id.
1630 * -EPERM Identify data not valid, SMART not supported or not enabled
1632 static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1633 struct smart_attr *attrib)
1636 struct smart_attr *pattr;
1641 if (!port->identify_valid) {
1642 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1645 if (!(port->identify[82] & 0x1)) {
1646 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1649 if (!(port->identify[85] & 0x1)) {
1650 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1654 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1655 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1657 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1661 pattr = (struct smart_attr *)(port->smart_buf + 2);
1662 for (i = 0; i < 29; i++, pattr++)
1663 if (pattr->attr_id == id) {
1664 memcpy(attrib, pattr, sizeof(struct smart_attr));
1669 dev_warn(&port->dd->pdev->dev,
1670 "Query for invalid SMART attribute ID\n");
1678 * Get the drive capacity.
1680 * @dd Pointer to the device data structure.
1681 * @sectors Pointer to the variable that will receive the sector count.
1684 * 1 Capacity was returned successfully.
1685 * 0 The identify information is invalid.
1687 static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
1689 struct mtip_port *port = dd->port;
1690 u64 total, raw0, raw1, raw2, raw3;
1691 raw0 = port->identify[100];
1692 raw1 = port->identify[101];
1693 raw2 = port->identify[102];
1694 raw3 = port->identify[103];
1695 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1697 return (bool) !!port->identify_valid;
1703 * Resets the HBA by setting the HBA Reset bit in the Global
1704 * HBA Control register. After setting the HBA Reset bit the
1705 * function waits for 1 second before reading the HBA Reset
1706 * bit to make sure it has cleared. If HBA Reset is not clear
1707 * an error is returned. Cannot be used in non-blockable
1710 * @dd Pointer to the driver data structure.
1713 * 0 The reset was successful.
1714 * -1 The HBA Reset bit did not clear.
1716 static int mtip_hba_reset(struct driver_data *dd)
1718 mtip_deinit_port(dd->port);
1720 /* Set the reset bit */
1721 writel(HOST_RESET, dd->mmio + HOST_CTL);
1724 readl(dd->mmio + HOST_CTL);
1726 /* Wait for reset to clear */
1729 /* Check the bit has cleared */
1730 if (readl(dd->mmio + HOST_CTL) & HOST_RESET) {
1731 dev_err(&dd->pdev->dev,
1732 "Reset bit did not clear.\n");
1740 * Display the identify command data.
1742 * @port Pointer to the port data structure.
1747 static void mtip_dump_identify(struct mtip_port *port)
1750 unsigned short revid;
1753 if (!port->identify_valid)
1756 strlcpy(cbuf, (char *)(port->identify+10), 21);
1757 dev_info(&port->dd->pdev->dev,
1758 "Serial No.: %s\n", cbuf);
1760 strlcpy(cbuf, (char *)(port->identify+23), 9);
1761 dev_info(&port->dd->pdev->dev,
1762 "Firmware Ver.: %s\n", cbuf);
1764 strlcpy(cbuf, (char *)(port->identify+27), 41);
1765 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1767 if (mtip_hw_get_capacity(port->dd, §ors))
1768 dev_info(&port->dd->pdev->dev,
1769 "Capacity: %llu sectors (%llu MB)\n",
1771 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1773 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
1774 switch (revid & 0xFF) {
1776 strlcpy(cbuf, "A0", 3);
1779 strlcpy(cbuf, "A2", 3);
1782 strlcpy(cbuf, "?", 2);
1785 dev_info(&port->dd->pdev->dev,
1786 "Card Type: %s\n", cbuf);
1790 * Map the commands scatter list into the command table.
1792 * @command Pointer to the command.
1793 * @nents Number of scatter list entries.
1798 static inline void fill_command_sg(struct driver_data *dd,
1799 struct mtip_cmd *command,
1803 unsigned int dma_len;
1804 struct mtip_cmd_sg *command_sg;
1805 struct scatterlist *sg = command->sg;
1807 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1809 for (n = 0; n < nents; n++) {
1810 dma_len = sg_dma_len(sg);
1811 if (dma_len > 0x400000)
1812 dev_err(&dd->pdev->dev,
1813 "DMA segment length truncated\n");
1814 command_sg->info = __force_bit2int
1815 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1816 command_sg->dba = __force_bit2int
1817 cpu_to_le32(sg_dma_address(sg));
1818 command_sg->dba_upper = __force_bit2int
1819 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
1826 * @brief Execute a drive command.
1828 * return value 0 The command completed successfully.
1829 * return value -1 An error occurred while executing the command.
1831 static int exec_drive_task(struct mtip_port *port, u8 *command)
1833 struct host_to_dev_fis fis;
1834 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
1836 /* Build the FIS. */
1837 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1840 fis.command = command[0];
1841 fis.features = command[1];
1842 fis.sect_count = command[2];
1843 fis.sector = command[3];
1844 fis.cyl_low = command[4];
1845 fis.cyl_hi = command[5];
1846 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1848 dbg_printk(MTIP_DRV_NAME " %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
1858 /* Execute the command. */
1859 if (mtip_exec_internal_command(port,
1866 MTIP_IOCTL_COMMAND_TIMEOUT_MS) < 0) {
1870 command[0] = reply->command; /* Status*/
1871 command[1] = reply->features; /* Error*/
1872 command[4] = reply->cyl_low;
1873 command[5] = reply->cyl_hi;
1875 dbg_printk(MTIP_DRV_NAME " %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
1886 * @brief Execute a drive command.
1888 * @param port Pointer to the port data structure.
1889 * @param command Pointer to the user specified command parameters.
1890 * @param user_buffer Pointer to the user space buffer where read sector
1891 * data should be copied.
1893 * return value 0 The command completed successfully.
1894 * return value -EFAULT An error occurred while copying the completion
1895 * data to the user space buffer.
1896 * return value -1 An error occurred while executing the command.
1898 static int exec_drive_command(struct mtip_port *port, u8 *command,
1899 void __user *user_buffer)
1901 struct host_to_dev_fis fis;
1902 struct host_to_dev_fis *reply;
1904 dma_addr_t dma_addr = 0;
1905 int rv = 0, xfer_sz = command[3];
1911 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1912 ATA_SECT_SIZE * xfer_sz,
1916 dev_err(&port->dd->pdev->dev,
1917 "Memory allocation failed (%d bytes)\n",
1918 ATA_SECT_SIZE * xfer_sz);
1921 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1924 /* Build the FIS. */
1925 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1928 fis.command = command[0];
1929 fis.features = command[2];
1930 fis.sect_count = command[3];
1931 if (fis.command == ATA_CMD_SMART) {
1932 fis.sector = command[1];
1938 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1940 reply = (port->rxfis + RX_FIS_D2H_REG);
1942 dbg_printk(MTIP_DRV_NAME
1943 " %s: User Command: cmd %x, sect %x, "
1944 "feat %x, sectcnt %x\n",
1951 /* Execute the command. */
1952 if (mtip_exec_internal_command(port,
1955 (xfer_sz ? dma_addr : 0),
1956 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
1959 MTIP_IOCTL_COMMAND_TIMEOUT_MS)
1962 goto exit_drive_command;
1965 /* Collect the completion status. */
1966 command[0] = reply->command; /* Status*/
1967 command[1] = reply->features; /* Error*/
1968 command[2] = reply->sect_count;
1970 dbg_printk(MTIP_DRV_NAME
1971 " %s: Completion Status: stat %x, "
1972 "err %x, nsect %x\n",
1979 if (copy_to_user(user_buffer,
1981 ATA_SECT_SIZE * command[3])) {
1983 goto exit_drive_command;
1988 dmam_free_coherent(&port->dd->pdev->dev,
1989 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1994 * Indicates whether a command has a single sector payload.
1996 * @command passed to the device to perform the certain event.
1997 * @features passed to the device to perform the certain event.
2000 * 1 command is one that always has a single sector payload,
2001 * regardless of the value in the Sector Count field.
2005 static unsigned int implicit_sector(unsigned char command,
2006 unsigned char features)
2008 unsigned int rv = 0;
2010 /* list of commands that have an implicit sector count of 1 */
2012 case ATA_CMD_SEC_SET_PASS:
2013 case ATA_CMD_SEC_UNLOCK:
2014 case ATA_CMD_SEC_ERASE_PREP:
2015 case ATA_CMD_SEC_ERASE_UNIT:
2016 case ATA_CMD_SEC_FREEZE_LOCK:
2017 case ATA_CMD_SEC_DISABLE_PASS:
2018 case ATA_CMD_PMP_READ:
2019 case ATA_CMD_PMP_WRITE:
2022 case ATA_CMD_SET_MAX:
2023 if (features == ATA_SET_MAX_UNLOCK)
2027 if ((features == ATA_SMART_READ_VALUES) ||
2028 (features == ATA_SMART_READ_THRESHOLDS))
2031 case ATA_CMD_CONF_OVERLAY:
2032 if ((features == ATA_DCO_IDENTIFY) ||
2033 (features == ATA_DCO_SET))
2039 static void mtip_set_timeout(struct driver_data *dd,
2040 struct host_to_dev_fis *fis,
2041 unsigned int *timeout, u8 erasemode)
2043 switch (fis->command) {
2044 case ATA_CMD_DOWNLOAD_MICRO:
2045 *timeout = 120000; /* 2 minutes */
2047 case ATA_CMD_SEC_ERASE_UNIT:
2050 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
2052 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
2054 case ATA_CMD_STANDBYNOW1:
2055 *timeout = 120000; /* 2 minutes */
2059 *timeout = 60000; /* 60 seconds */
2062 *timeout = 15000; /* 15 seconds */
2065 *timeout = MTIP_IOCTL_COMMAND_TIMEOUT_MS;
2071 * Executes a taskfile
2072 * See ide_taskfile_ioctl() for derivation
2074 static int exec_drive_taskfile(struct driver_data *dd,
2076 ide_task_request_t *req_task,
2079 struct host_to_dev_fis fis;
2080 struct host_to_dev_fis *reply;
2083 dma_addr_t outbuf_dma = 0;
2084 dma_addr_t inbuf_dma = 0;
2085 dma_addr_t dma_buffer = 0;
2087 unsigned int taskin = 0;
2088 unsigned int taskout = 0;
2090 unsigned int timeout;
2091 unsigned int force_single_sector;
2092 unsigned int transfer_size;
2093 unsigned long task_file_data;
2094 int intotal = outtotal + req_task->out_size;
2097 taskout = req_task->out_size;
2098 taskin = req_task->in_size;
2099 /* 130560 = 512 * 0xFF*/
2100 if (taskin > 130560 || taskout > 130560) {
2106 outbuf = kzalloc(taskout, GFP_KERNEL);
2107 if (outbuf == NULL) {
2111 if (copy_from_user(outbuf, buf + outtotal, taskout)) {
2115 outbuf_dma = pci_map_single(dd->pdev,
2119 if (outbuf_dma == 0) {
2123 dma_buffer = outbuf_dma;
2127 inbuf = kzalloc(taskin, GFP_KERNEL);
2128 if (inbuf == NULL) {
2133 if (copy_from_user(inbuf, buf + intotal, taskin)) {
2137 inbuf_dma = pci_map_single(dd->pdev,
2139 taskin, DMA_FROM_DEVICE);
2140 if (inbuf_dma == 0) {
2144 dma_buffer = inbuf_dma;
2147 /* only supports PIO and non-data commands from this ioctl. */
2148 switch (req_task->data_phase) {
2150 nsect = taskout / ATA_SECT_SIZE;
2151 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2154 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2156 case TASKFILE_NO_DATA:
2157 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2164 /* Build the FIS. */
2165 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2169 fis.command = req_task->io_ports[7];
2170 fis.features = req_task->io_ports[1];
2171 fis.sect_count = req_task->io_ports[2];
2172 fis.lba_low = req_task->io_ports[3];
2173 fis.lba_mid = req_task->io_ports[4];
2174 fis.lba_hi = req_task->io_ports[5];
2175 /* Clear the dev bit*/
2176 fis.device = req_task->io_ports[6] & ~0x10;
2178 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2179 req_task->in_flags.all =
2180 IDE_TASKFILE_STD_IN_FLAGS |
2181 (IDE_HOB_STD_IN_FLAGS << 8);
2182 fis.lba_low_ex = req_task->hob_ports[3];
2183 fis.lba_mid_ex = req_task->hob_ports[4];
2184 fis.lba_hi_ex = req_task->hob_ports[5];
2185 fis.features_ex = req_task->hob_ports[1];
2186 fis.sect_cnt_ex = req_task->hob_ports[2];
2189 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2192 force_single_sector = implicit_sector(fis.command, fis.features);
2194 if ((taskin || taskout) && (!fis.sect_count)) {
2196 fis.sect_count = nsect;
2198 if (!force_single_sector) {
2199 dev_warn(&dd->pdev->dev,
2200 "data movement but "
2201 "sect_count is 0\n");
2208 dbg_printk(MTIP_DRV_NAME
2209 " %s: cmd %x, feat %x, nsect %x,"
2210 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2221 /* check for erase mode support during secure erase.*/
2222 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2223 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
2227 mtip_set_timeout(dd, &fis, &timeout, erasemode);
2229 /* Determine the correct transfer size.*/
2230 if (force_single_sector)
2231 transfer_size = ATA_SECT_SIZE;
2233 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2235 /* Execute the command.*/
2236 if (mtip_exec_internal_command(dd->port,
2248 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2250 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2251 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2252 req_task->io_ports[7] = reply->control;
2254 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2255 req_task->io_ports[7] = reply->command;
2258 /* reclaim the DMA buffers.*/
2260 pci_unmap_single(dd->pdev, inbuf_dma,
2261 taskin, DMA_FROM_DEVICE);
2263 pci_unmap_single(dd->pdev, outbuf_dma,
2264 taskout, DMA_TO_DEVICE);
2268 /* return the ATA registers to the caller.*/
2269 req_task->io_ports[1] = reply->features;
2270 req_task->io_ports[2] = reply->sect_count;
2271 req_task->io_ports[3] = reply->lba_low;
2272 req_task->io_ports[4] = reply->lba_mid;
2273 req_task->io_ports[5] = reply->lba_hi;
2274 req_task->io_ports[6] = reply->device;
2276 if (req_task->out_flags.all & 1) {
2278 req_task->hob_ports[3] = reply->lba_low_ex;
2279 req_task->hob_ports[4] = reply->lba_mid_ex;
2280 req_task->hob_ports[5] = reply->lba_hi_ex;
2281 req_task->hob_ports[1] = reply->features_ex;
2282 req_task->hob_ports[2] = reply->sect_cnt_ex;
2284 dbg_printk(MTIP_DRV_NAME
2285 " %s: Completion: stat %x,"
2286 "err %x, sect_cnt %x, lbalo %x,"
2287 "lbamid %x, lbahi %x, dev %x\n",
2289 req_task->io_ports[7],
2290 req_task->io_ports[1],
2291 req_task->io_ports[2],
2292 req_task->io_ports[3],
2293 req_task->io_ports[4],
2294 req_task->io_ports[5],
2295 req_task->io_ports[6]);
2298 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2304 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2311 pci_unmap_single(dd->pdev, inbuf_dma,
2312 taskin, DMA_FROM_DEVICE);
2314 pci_unmap_single(dd->pdev, outbuf_dma,
2315 taskout, DMA_TO_DEVICE);
2323 * Handle IOCTL calls from the Block Layer.
2325 * This function is called by the Block Layer when it receives an IOCTL
2326 * command that it does not understand. If the IOCTL command is not supported
2327 * this function returns -ENOTTY.
2329 * @dd Pointer to the driver data structure.
2330 * @cmd IOCTL command passed from the Block Layer.
2331 * @arg IOCTL argument passed from the Block Layer.
2334 * 0 The IOCTL completed successfully.
2335 * -ENOTTY The specified command is not supported.
2336 * -EFAULT An error occurred copying data to a user space buffer.
2337 * -EIO An error occurred while executing the command.
2339 static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2343 case HDIO_GET_IDENTITY:
2345 if (copy_to_user((void __user *)arg, dd->port->identify,
2346 sizeof(u16) * ATA_ID_WORDS))
2350 case HDIO_DRIVE_CMD:
2352 u8 drive_command[4];
2354 /* Copy the user command info to our buffer. */
2355 if (copy_from_user(drive_command,
2356 (void __user *) arg,
2357 sizeof(drive_command)))
2360 /* Execute the drive command. */
2361 if (exec_drive_command(dd->port,
2363 (void __user *) (arg+4)))
2366 /* Copy the status back to the users buffer. */
2367 if (copy_to_user((void __user *) arg,
2369 sizeof(drive_command)))
2374 case HDIO_DRIVE_TASK:
2376 u8 drive_command[7];
2378 /* Copy the user command info to our buffer. */
2379 if (copy_from_user(drive_command,
2380 (void __user *) arg,
2381 sizeof(drive_command)))
2384 /* Execute the drive command. */
2385 if (exec_drive_task(dd->port, drive_command))
2388 /* Copy the status back to the users buffer. */
2389 if (copy_to_user((void __user *) arg,
2391 sizeof(drive_command)))
2396 case HDIO_DRIVE_TASKFILE: {
2397 ide_task_request_t req_task;
2400 if (copy_from_user(&req_task, (void __user *) arg,
2404 outtotal = sizeof(req_task);
2406 ret = exec_drive_taskfile(dd, (void __user *) arg,
2407 &req_task, outtotal);
2409 if (copy_to_user((void __user *) arg, &req_task,
2423 * Submit an IO to the hw
2425 * This function is called by the block layer to issue an io
2426 * to the device. Upon completion, the callback function will
2427 * be called with the data parameter passed as the callback data.
2429 * @dd Pointer to the driver data structure.
2430 * @start First sector to read.
2431 * @nsect Number of sectors to read.
2432 * @nents Number of entries in scatter list for the read command.
2433 * @tag The tag of this read command.
2434 * @callback Pointer to the function that should be called
2435 * when the read completes.
2436 * @data Callback data passed to the callback function
2437 * when the read completes.
2438 * @dir Direction (read or write)
2443 static void mtip_hw_submit_io(struct driver_data *dd, sector_t sector,
2444 int nsect, int nents, int tag, void *callback,
2445 void *data, int dir)
2447 struct host_to_dev_fis *fis;
2448 struct mtip_port *port = dd->port;
2449 struct mtip_cmd *command = &port->commands[tag];
2450 int dma_dir = (dir == READ) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2453 /* Map the scatter list for DMA access */
2454 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
2456 command->scatter_ents = nents;
2459 * The number of retries for this command before it is
2460 * reported as a failure to the upper layers.
2462 command->retries = MTIP_MAX_RETRIES;
2465 fis = command->command;
2469 (dir == READ ? ATA_CMD_FPDMA_READ : ATA_CMD_FPDMA_WRITE);
2470 fis->lba_low = start & 0xFF;
2471 fis->lba_mid = (start >> 8) & 0xFF;
2472 fis->lba_hi = (start >> 16) & 0xFF;
2473 fis->lba_low_ex = (start >> 24) & 0xFF;
2474 fis->lba_mid_ex = (start >> 32) & 0xFF;
2475 fis->lba_hi_ex = (start >> 40) & 0xFF;
2476 fis->device = 1 << 6;
2477 fis->features = nsect & 0xFF;
2478 fis->features_ex = (nsect >> 8) & 0xFF;
2479 fis->sect_count = ((tag << 3) | (tag >> 5));
2480 fis->sect_cnt_ex = 0;
2484 fill_command_sg(dd, command, nents);
2486 /* Populate the command header */
2487 command->command_header->opts =
2488 __force_bit2int cpu_to_le32(
2489 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
2490 command->command_header->byte_count = 0;
2493 * Set the completion function and data for the command
2494 * within this layer.
2496 command->comp_data = dd;
2497 command->comp_func = mtip_async_complete;
2498 command->direction = dma_dir;
2501 * Set the completion function and data for the command passed
2502 * from the upper layer.
2504 command->async_data = data;
2505 command->async_callback = callback;
2508 * To prevent this command from being issued
2509 * if an internal command is in progress or error handling is active.
2511 if (port->flags & MTIP_PF_PAUSE_IO) {
2512 set_bit(tag, port->cmds_to_issue);
2513 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
2517 /* Issue the command to the hardware */
2518 mtip_issue_ncq_command(port, tag);
2524 * Release a command slot.
2526 * @dd Pointer to the driver data structure.
2532 static void mtip_hw_release_scatterlist(struct driver_data *dd, int tag)
2534 release_slot(dd->port, tag);
2538 * Obtain a command slot and return its associated scatter list.
2540 * @dd Pointer to the driver data structure.
2541 * @tag Pointer to an int that will receive the allocated command
2545 * Pointer to the scatter list for the allocated command slot
2546 * or NULL if no command slots are available.
2548 static struct scatterlist *mtip_hw_get_scatterlist(struct driver_data *dd,
2552 * It is possible that, even with this semaphore, a thread
2553 * may think that no command slots are available. Therefore, we
2554 * need to make an attempt to get_slot().
2556 down(&dd->port->cmd_slot);
2557 *tag = get_slot(dd->port);
2559 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
2560 up(&dd->port->cmd_slot);
2563 if (unlikely(*tag < 0)) {
2564 up(&dd->port->cmd_slot);
2568 return dd->port->commands[*tag].sg;
2572 * Sysfs status dump.
2574 * @dev Pointer to the device structure, passed by the kernrel.
2575 * @attr Pointer to the device_attribute structure passed by the kernel.
2576 * @buf Pointer to the char buffer that will receive the stats info.
2579 * The size, in bytes, of the data copied into buf.
2581 static ssize_t mtip_hw_show_status(struct device *dev,
2582 struct device_attribute *attr,
2585 struct driver_data *dd = dev_to_disk(dev)->private_data;
2588 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
2589 size += sprintf(buf, "%s", "thermal_shutdown\n");
2590 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
2591 size += sprintf(buf, "%s", "write_protect\n");
2593 size += sprintf(buf, "%s", "online\n");
2598 static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
2600 static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2601 size_t len, loff_t *offset)
2603 struct driver_data *dd = (struct driver_data *)f->private_data;
2604 char buf[MTIP_DFS_MAX_BUF_SIZE];
2605 u32 group_allocated;
2612 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2614 for (n = dd->slot_groups-1; n >= 0; n--)
2615 size += sprintf(&buf[size], "%08X ",
2616 readl(dd->port->s_active[n]));
2618 size += sprintf(&buf[size], "]\n");
2619 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2621 for (n = dd->slot_groups-1; n >= 0; n--)
2622 size += sprintf(&buf[size], "%08X ",
2623 readl(dd->port->cmd_issue[n]));
2625 size += sprintf(&buf[size], "]\n");
2626 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2628 for (n = dd->slot_groups-1; n >= 0; n--)
2629 size += sprintf(&buf[size], "%08X ",
2630 readl(dd->port->completed[n]));
2632 size += sprintf(&buf[size], "]\n");
2633 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2634 readl(dd->port->mmio + PORT_IRQ_STAT));
2635 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2636 readl(dd->mmio + HOST_IRQ_STAT));
2637 size += sprintf(&buf[size], "\n");
2639 size += sprintf(&buf[size], "L/ Allocated : [ 0x");
2641 for (n = dd->slot_groups-1; n >= 0; n--) {
2642 if (sizeof(long) > sizeof(u32))
2644 dd->port->allocated[n/2] >> (32*(n&1));
2646 group_allocated = dd->port->allocated[n];
2647 size += sprintf(&buf[size], "%08X ", group_allocated);
2649 size += sprintf(&buf[size], "]\n");
2651 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2653 for (n = dd->slot_groups-1; n >= 0; n--) {
2654 if (sizeof(long) > sizeof(u32))
2656 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2658 group_allocated = dd->port->cmds_to_issue[n];
2659 size += sprintf(&buf[size], "%08X ", group_allocated);
2661 size += sprintf(&buf[size], "]\n");
2663 *offset = size <= len ? size : len;
2664 size = copy_to_user(ubuf, buf, *offset);
2671 static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2672 size_t len, loff_t *offset)
2674 struct driver_data *dd = (struct driver_data *)f->private_data;
2675 char buf[MTIP_DFS_MAX_BUF_SIZE];
2681 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2683 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2686 *offset = size <= len ? size : len;
2687 size = copy_to_user(ubuf, buf, *offset);
2694 static const struct file_operations mtip_regs_fops = {
2695 .owner = THIS_MODULE,
2696 .open = simple_open,
2697 .read = mtip_hw_read_registers,
2698 .llseek = no_llseek,
2701 static const struct file_operations mtip_flags_fops = {
2702 .owner = THIS_MODULE,
2703 .open = simple_open,
2704 .read = mtip_hw_read_flags,
2705 .llseek = no_llseek,
2709 * Create the sysfs related attributes.
2711 * @dd Pointer to the driver data structure.
2712 * @kobj Pointer to the kobj for the block device.
2715 * 0 Operation completed successfully.
2716 * -EINVAL Invalid parameter.
2718 static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
2723 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2724 dev_warn(&dd->pdev->dev,
2725 "Error creating 'status' sysfs entry\n");
2730 * Remove the sysfs related attributes.
2732 * @dd Pointer to the driver data structure.
2733 * @kobj Pointer to the kobj for the block device.
2736 * 0 Operation completed successfully.
2737 * -EINVAL Invalid parameter.
2739 static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
2744 sysfs_remove_file(kobj, &dev_attr_status.attr);
2749 static int mtip_hw_debugfs_init(struct driver_data *dd)
2754 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2755 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2756 dev_warn(&dd->pdev->dev,
2757 "Error creating node %s under debugfs\n",
2758 dd->disk->disk_name);
2759 dd->dfs_node = NULL;
2763 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2765 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2771 static void mtip_hw_debugfs_exit(struct driver_data *dd)
2773 debugfs_remove_recursive(dd->dfs_node);
2778 * Perform any init/resume time hardware setup
2780 * @dd Pointer to the driver data structure.
2785 static inline void hba_setup(struct driver_data *dd)
2788 hwdata = readl(dd->mmio + HOST_HSORG);
2790 /* interrupt bug workaround: use only 1 IS bit.*/
2792 HSORG_DISABLE_SLOTGRP_INTR |
2793 HSORG_DISABLE_SLOTGRP_PXIS,
2794 dd->mmio + HOST_HSORG);
2798 * Detect the details of the product, and store anything needed
2799 * into the driver data structure. This includes product type and
2800 * version and number of slot groups.
2802 * @dd Pointer to the driver data structure.
2807 static void mtip_detect_product(struct driver_data *dd)
2810 unsigned int rev, slotgroups;
2813 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2815 * [15:8] hardware/software interface rev#
2816 * [ 3] asic-style interface
2817 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2819 hwdata = readl(dd->mmio + HOST_HSORG);
2821 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2822 dd->slot_groups = 1;
2825 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2826 rev = (hwdata & HSORG_HWREV) >> 8;
2827 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2828 dev_info(&dd->pdev->dev,
2829 "ASIC-FPGA design, HS rev 0x%x, "
2830 "%i slot groups [%i slots]\n",
2835 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2836 dev_warn(&dd->pdev->dev,
2837 "Warning: driver only supports "
2838 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2839 slotgroups = MTIP_MAX_SLOT_GROUPS;
2841 dd->slot_groups = slotgroups;
2845 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2849 * Blocking wait for FTL rebuild to complete
2851 * @dd Pointer to the DRIVER_DATA structure.
2854 * 0 FTL rebuild completed successfully
2855 * -EFAULT FTL rebuild error/timeout/interruption
2857 static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2859 unsigned long timeout, cnt = 0, start;
2861 dev_warn(&dd->pdev->dev,
2862 "FTL rebuild in progress. Polling for completion.\n");
2865 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2868 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
2871 if (mtip_check_surprise_removal(dd->pdev))
2874 if (mtip_get_identify(dd->port, NULL) < 0)
2877 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2878 MTIP_FTL_REBUILD_MAGIC) {
2880 /* Print message every 3 minutes */
2882 dev_warn(&dd->pdev->dev,
2883 "FTL rebuild in progress (%d secs).\n",
2884 jiffies_to_msecs(jiffies - start) / 1000);
2888 dev_warn(&dd->pdev->dev,
2889 "FTL rebuild complete (%d secs).\n",
2890 jiffies_to_msecs(jiffies - start) / 1000);
2891 mtip_block_initialize(dd);
2895 } while (time_before(jiffies, timeout));
2897 /* Check for timeout */
2898 dev_err(&dd->pdev->dev,
2899 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2900 jiffies_to_msecs(jiffies - start) / 1000);
2905 * service thread to issue queued commands
2907 * @data Pointer to the driver data structure.
2913 static int mtip_service_thread(void *data)
2915 struct driver_data *dd = (struct driver_data *)data;
2916 unsigned long slot, slot_start, slot_wrap;
2917 unsigned int num_cmd_slots = dd->slot_groups * 32;
2918 struct mtip_port *port = dd->port;
2922 * the condition is to check neither an internal command is
2923 * is in progress nor error handling is active
2925 wait_event_interruptible(port->svc_wait, (port->flags) &&
2926 !(port->flags & MTIP_PF_PAUSE_IO));
2928 if (kthread_should_stop())
2931 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
2935 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2936 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
2938 /* used to restrict the loop to one iteration */
2939 slot_start = num_cmd_slots;
2942 slot = find_next_bit(port->cmds_to_issue,
2943 num_cmd_slots, slot);
2944 if (slot_wrap == 1) {
2945 if ((slot_start >= slot) ||
2946 (slot >= num_cmd_slots))
2949 if (unlikely(slot_start == num_cmd_slots))
2952 if (unlikely(slot == num_cmd_slots)) {
2958 /* Issue the command to the hardware */
2959 mtip_issue_ncq_command(port, slot);
2961 clear_bit(slot, port->cmds_to_issue);
2964 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
2965 } else if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
2966 if (!mtip_ftl_rebuild_poll(dd))
2967 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
2969 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
2971 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2973 if (test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2980 * Called once for each card.
2982 * @dd Pointer to the driver data structure.
2985 * 0 on success, else an error code.
2987 static int mtip_hw_init(struct driver_data *dd)
2991 unsigned int num_command_slots;
2992 unsigned long timeout, timetaken;
2994 struct smart_attr attr242;
2996 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
2998 mtip_detect_product(dd);
2999 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3003 num_command_slots = dd->slot_groups * 32;
3007 tasklet_init(&dd->tasklet, mtip_tasklet, (unsigned long)dd);
3009 dd->port = kzalloc(sizeof(struct mtip_port), GFP_KERNEL);
3011 dev_err(&dd->pdev->dev,
3012 "Memory allocation: port structure\n");
3016 /* Counting semaphore to track command slot usage */
3017 sema_init(&dd->port->cmd_slot, num_command_slots - 1);
3019 /* Spinlock to prevent concurrent issue */
3020 spin_lock_init(&dd->port->cmd_issue_lock);
3022 /* Set the port mmio base address. */
3023 dd->port->mmio = dd->mmio + PORT_OFFSET;
3026 /* Allocate memory for the command list. */
3027 dd->port->command_list =
3028 dmam_alloc_coherent(&dd->pdev->dev,
3029 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
3030 &dd->port->command_list_dma,
3032 if (!dd->port->command_list) {
3033 dev_err(&dd->pdev->dev,
3034 "Memory allocation: command list\n");
3039 /* Clear the memory we have allocated. */
3040 memset(dd->port->command_list,
3042 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4));
3044 /* Setup the addresse of the RX FIS. */
3045 dd->port->rxfis = dd->port->command_list + HW_CMD_SLOT_SZ;
3046 dd->port->rxfis_dma = dd->port->command_list_dma + HW_CMD_SLOT_SZ;
3048 /* Setup the address of the command tables. */
3049 dd->port->command_table = dd->port->rxfis + AHCI_RX_FIS_SZ;
3050 dd->port->command_tbl_dma = dd->port->rxfis_dma + AHCI_RX_FIS_SZ;
3052 /* Setup the address of the identify data. */
3053 dd->port->identify = dd->port->command_table +
3055 dd->port->identify_dma = dd->port->command_tbl_dma +
3058 /* Setup the address of the sector buffer - for some non-ncq cmds */
3059 dd->port->sector_buffer = (void *) dd->port->identify + ATA_SECT_SIZE;
3060 dd->port->sector_buffer_dma = dd->port->identify_dma + ATA_SECT_SIZE;
3062 /* Setup the address of the log buf - for read log command */
3063 dd->port->log_buf = (void *)dd->port->sector_buffer + ATA_SECT_SIZE;
3064 dd->port->log_buf_dma = dd->port->sector_buffer_dma + ATA_SECT_SIZE;
3066 /* Setup the address of the smart buf - for smart read data command */
3067 dd->port->smart_buf = (void *)dd->port->log_buf + ATA_SECT_SIZE;
3068 dd->port->smart_buf_dma = dd->port->log_buf_dma + ATA_SECT_SIZE;
3071 /* Point the command headers at the command tables. */
3072 for (i = 0; i < num_command_slots; i++) {
3073 dd->port->commands[i].command_header =
3074 dd->port->command_list +
3075 (sizeof(struct mtip_cmd_hdr) * i);
3076 dd->port->commands[i].command_header_dma =
3077 dd->port->command_list_dma +
3078 (sizeof(struct mtip_cmd_hdr) * i);
3080 dd->port->commands[i].command =
3081 dd->port->command_table + (HW_CMD_TBL_SZ * i);
3082 dd->port->commands[i].command_dma =
3083 dd->port->command_tbl_dma + (HW_CMD_TBL_SZ * i);
3085 if (readl(dd->mmio + HOST_CAP) & HOST_CAP_64)
3086 dd->port->commands[i].command_header->ctbau =
3087 __force_bit2int cpu_to_le32(
3088 (dd->port->commands[i].command_dma >> 16) >> 16);
3089 dd->port->commands[i].command_header->ctba =
3090 __force_bit2int cpu_to_le32(
3091 dd->port->commands[i].command_dma & 0xFFFFFFFF);
3094 * If this is not done, a bug is reported by the stock
3095 * FC11 i386. Due to the fact that it has lots of kernel
3096 * debugging enabled.
3098 sg_init_table(dd->port->commands[i].sg, MTIP_MAX_SG);
3100 /* Mark all commands as currently inactive.*/
3101 atomic_set(&dd->port->commands[i].active, 0);
3104 /* Setup the pointers to the extended s_active and CI registers. */
3105 for (i = 0; i < dd->slot_groups; i++) {
3106 dd->port->s_active[i] =
3107 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3108 dd->port->cmd_issue[i] =
3109 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3110 dd->port->completed[i] =
3111 dd->port->mmio + i*0x80 + PORT_SDBV;
3114 timetaken = jiffies;
3115 timeout = jiffies + msecs_to_jiffies(30000);
3116 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3117 time_before(jiffies, timeout)) {
3120 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3121 timetaken = jiffies - timetaken;
3122 dev_warn(&dd->pdev->dev,
3123 "Surprise removal detected at %u ms\n",
3124 jiffies_to_msecs(timetaken));
3128 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
3129 timetaken = jiffies - timetaken;
3130 dev_warn(&dd->pdev->dev,
3131 "Removal detected at %u ms\n",
3132 jiffies_to_msecs(timetaken));
3137 /* Conditionally reset the HBA. */
3138 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3139 if (mtip_hba_reset(dd) < 0) {
3140 dev_err(&dd->pdev->dev,
3141 "Card did not reset within timeout\n");
3146 /* Clear any pending interrupts on the HBA */
3147 writel(readl(dd->mmio + HOST_IRQ_STAT),
3148 dd->mmio + HOST_IRQ_STAT);
3151 mtip_init_port(dd->port);
3152 mtip_start_port(dd->port);
3154 /* Setup the ISR and enable interrupts. */
3155 rv = devm_request_irq(&dd->pdev->dev,
3159 dev_driver_string(&dd->pdev->dev),
3163 dev_err(&dd->pdev->dev,
3164 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3168 /* Enable interrupts on the HBA. */
3169 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3170 dd->mmio + HOST_CTL);
3172 init_timer(&dd->port->cmd_timer);
3173 init_waitqueue_head(&dd->port->svc_wait);
3175 dd->port->cmd_timer.data = (unsigned long int) dd->port;
3176 dd->port->cmd_timer.function = mtip_timeout_function;
3177 mod_timer(&dd->port->cmd_timer,
3178 jiffies + msecs_to_jiffies(MTIP_TIMEOUT_CHECK_PERIOD));
3181 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
3186 if (mtip_get_identify(dd->port, NULL) < 0) {
3191 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3192 MTIP_FTL_REBUILD_MAGIC) {
3193 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3194 return MTIP_FTL_REBUILD_MAGIC;
3196 mtip_dump_identify(dd->port);
3198 /* check write protect, over temp and rebuild statuses */
3199 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3201 dd->port->log_buf_dma, 1);
3203 dev_warn(&dd->pdev->dev,
3204 "Error in READ LOG EXT (10h) command\n");
3205 /* non-critical error, don't fail the load */
3207 buf = (unsigned char *)dd->port->log_buf;
3208 if (buf[259] & 0x1) {
3209 dev_info(&dd->pdev->dev,
3210 "Write protect bit is set.\n");
3211 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3213 if (buf[288] == 0xF7) {
3214 dev_info(&dd->pdev->dev,
3215 "Exceeded Tmax, drive in thermal shutdown.\n");
3216 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3218 if (buf[288] == 0xBF) {
3219 dev_info(&dd->pdev->dev,
3220 "Drive indicates rebuild has failed.\n");
3225 /* get write protect progess */
3226 memset(&attr242, 0, sizeof(struct smart_attr));
3227 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3228 dev_warn(&dd->pdev->dev,
3229 "Unable to check write protect progress\n");
3231 dev_info(&dd->pdev->dev,
3232 "Write protect progress: %u%% (%u blocks)\n",
3233 attr242.cur, le32_to_cpu(attr242.data));
3237 del_timer_sync(&dd->port->cmd_timer);
3239 /* Disable interrupts on the HBA. */
3240 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3241 dd->mmio + HOST_CTL);
3243 /*Release the IRQ. */
3244 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3247 mtip_deinit_port(dd->port);
3249 /* Free the command/command header memory. */
3250 dmam_free_coherent(&dd->pdev->dev,
3251 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
3252 dd->port->command_list,
3253 dd->port->command_list_dma);
3255 /* Free the memory allocated for the for structure. */
3262 * Called to deinitialize an interface.
3264 * @dd Pointer to the driver data structure.
3269 static int mtip_hw_exit(struct driver_data *dd)
3272 * Send standby immediate (E0h) to the drive so that it
3275 if (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
3277 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags))
3278 if (mtip_standby_immediate(dd->port))
3279 dev_warn(&dd->pdev->dev,
3280 "STANDBY IMMEDIATE failed\n");
3282 /* de-initialize the port. */
3283 mtip_deinit_port(dd->port);
3285 /* Disable interrupts on the HBA. */
3286 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3287 dd->mmio + HOST_CTL);
3290 del_timer_sync(&dd->port->cmd_timer);
3292 /* Release the IRQ. */
3293 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3295 /* Stop the bottom half tasklet. */
3296 tasklet_kill(&dd->tasklet);
3298 /* Free the command/command header memory. */
3299 dmam_free_coherent(&dd->pdev->dev,
3300 HW_PORT_PRIV_DMA_SZ + (ATA_SECT_SIZE * 4),
3301 dd->port->command_list,
3302 dd->port->command_list_dma);
3303 /* Free the memory allocated for the for structure. */
3310 * Issue a Standby Immediate command to the device.
3312 * This function is called by the Block Layer just before the
3313 * system powers off during a shutdown.
3315 * @dd Pointer to the driver data structure.
3320 static int mtip_hw_shutdown(struct driver_data *dd)
3323 * Send standby immediate (E0h) to the drive so that it
3326 mtip_standby_immediate(dd->port);
3334 * This function is called by the Block Layer just before the
3335 * system hibernates.
3337 * @dd Pointer to the driver data structure.
3340 * 0 Suspend was successful
3341 * -EFAULT Suspend was not successful
3343 static int mtip_hw_suspend(struct driver_data *dd)
3346 * Send standby immediate (E0h) to the drive
3347 * so that it saves its state.
3349 if (mtip_standby_immediate(dd->port) != 0) {
3350 dev_err(&dd->pdev->dev,
3351 "Failed standby-immediate command\n");
3355 /* Disable interrupts on the HBA.*/
3356 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3357 dd->mmio + HOST_CTL);
3358 mtip_deinit_port(dd->port);
3366 * This function is called by the Block Layer as the
3369 * @dd Pointer to the driver data structure.
3372 * 0 Resume was successful
3373 * -EFAULT Resume was not successful
3375 static int mtip_hw_resume(struct driver_data *dd)
3377 /* Perform any needed hardware setup steps */
3381 if (mtip_hba_reset(dd) != 0) {
3382 dev_err(&dd->pdev->dev,
3383 "Unable to reset the HBA\n");
3388 * Enable the port, DMA engine, and FIS reception specific
3389 * h/w in controller.
3391 mtip_init_port(dd->port);
3392 mtip_start_port(dd->port);
3394 /* Enable interrupts on the HBA.*/
3395 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3396 dd->mmio + HOST_CTL);
3402 * Helper function for reusing disk name
3403 * upon hot insertion.
3405 static int rssd_disk_name_format(char *prefix,
3410 const int base = 'z' - 'a' + 1;
3411 char *begin = buf + strlen(prefix);
3412 char *end = buf + buflen;
3422 *--p = 'a' + (index % unit);
3423 index = (index / unit) - 1;
3424 } while (index >= 0);
3426 memmove(begin, p, end - p);
3427 memcpy(buf, prefix, strlen(prefix));
3433 * Block layer IOCTL handler.
3435 * @dev Pointer to the block_device structure.
3437 * @cmd IOCTL command passed from the user application.
3438 * @arg Argument passed from the user application.
3441 * 0 IOCTL completed successfully.
3442 * -ENOTTY IOCTL not supported or invalid driver data
3443 * structure pointer.
3445 static int mtip_block_ioctl(struct block_device *dev,
3450 struct driver_data *dd = dev->bd_disk->private_data;
3452 if (!capable(CAP_SYS_ADMIN))
3458 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
3465 return mtip_hw_ioctl(dd, cmd, arg);
3469 #ifdef CONFIG_COMPAT
3471 * Block layer compat IOCTL handler.
3473 * @dev Pointer to the block_device structure.
3475 * @cmd IOCTL command passed from the user application.
3476 * @arg Argument passed from the user application.
3479 * 0 IOCTL completed successfully.
3480 * -ENOTTY IOCTL not supported or invalid driver data
3481 * structure pointer.
3483 static int mtip_block_compat_ioctl(struct block_device *dev,
3488 struct driver_data *dd = dev->bd_disk->private_data;
3490 if (!capable(CAP_SYS_ADMIN))
3496 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
3502 case HDIO_DRIVE_TASKFILE: {
3503 struct mtip_compat_ide_task_request_s __user *compat_req_task;
3504 ide_task_request_t req_task;
3505 int compat_tasksize, outtotal, ret;
3508 sizeof(struct mtip_compat_ide_task_request_s);
3511 (struct mtip_compat_ide_task_request_s __user *) arg;
3513 if (copy_from_user(&req_task, (void __user *) arg,
3514 compat_tasksize - (2 * sizeof(compat_long_t))))
3517 if (get_user(req_task.out_size, &compat_req_task->out_size))
3520 if (get_user(req_task.in_size, &compat_req_task->in_size))
3523 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3525 ret = exec_drive_taskfile(dd, (void __user *) arg,
3526 &req_task, outtotal);
3528 if (copy_to_user((void __user *) arg, &req_task,
3530 (2 * sizeof(compat_long_t))))
3533 if (put_user(req_task.out_size, &compat_req_task->out_size))
3536 if (put_user(req_task.in_size, &compat_req_task->in_size))
3542 return mtip_hw_ioctl(dd, cmd, arg);
3548 * Obtain the geometry of the device.
3550 * You may think that this function is obsolete, but some applications,
3551 * fdisk for example still used CHS values. This function describes the
3552 * device as having 224 heads and 56 sectors per cylinder. These values are
3553 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3554 * partition is described in terms of a start and end cylinder this means
3555 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3556 * affects performance.
3558 * @dev Pointer to the block_device strucutre.
3559 * @geo Pointer to a hd_geometry structure.
3562 * 0 Operation completed successfully.
3563 * -ENOTTY An error occurred while reading the drive capacity.
3565 static int mtip_block_getgeo(struct block_device *dev,
3566 struct hd_geometry *geo)
3568 struct driver_data *dd = dev->bd_disk->private_data;
3574 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3575 dev_warn(&dd->pdev->dev,
3576 "Could not get drive capacity.\n");
3582 sector_div(capacity, (geo->heads * geo->sectors));
3583 geo->cylinders = capacity;
3588 * Block device operation function.
3590 * This structure contains pointers to the functions required by the block
3593 static const struct block_device_operations mtip_block_ops = {
3594 .ioctl = mtip_block_ioctl,
3595 #ifdef CONFIG_COMPAT
3596 .compat_ioctl = mtip_block_compat_ioctl,
3598 .getgeo = mtip_block_getgeo,
3599 .owner = THIS_MODULE
3603 * Block layer make request function.
3605 * This function is called by the kernel to process a BIO for
3608 * @queue Pointer to the request queue. Unused other than to obtain
3609 * the driver data structure.
3610 * @bio Pointer to the BIO.
3613 static void mtip_make_request(struct request_queue *queue, struct bio *bio)
3615 struct driver_data *dd = queue->queuedata;
3616 struct scatterlist *sg;
3617 struct bio_vec *bvec;
3621 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3622 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3624 bio_endio(bio, -ENXIO);
3627 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
3628 bio_endio(bio, -ENODATA);
3631 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3633 bio_data_dir(bio))) {
3634 bio_endio(bio, -ENODATA);
3637 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))) {
3638 bio_endio(bio, -ENODATA);
3643 if (unlikely(!bio_has_data(bio))) {
3644 blk_queue_flush(queue, 0);
3649 sg = mtip_hw_get_scatterlist(dd, &tag);
3650 if (likely(sg != NULL)) {
3651 blk_queue_bounce(queue, &bio);
3653 if (unlikely((bio)->bi_vcnt > MTIP_MAX_SG)) {
3654 dev_warn(&dd->pdev->dev,
3655 "Maximum number of SGL entries exceeded\n");
3657 mtip_hw_release_scatterlist(dd, tag);
3661 /* Create the scatter list for this bio. */
3662 bio_for_each_segment(bvec, bio, nents) {
3663 sg_set_page(&sg[nents],
3669 /* Issue the read/write. */
3670 mtip_hw_submit_io(dd,
3683 * Block layer initialization function.
3685 * This function is called once by the PCI layer for each P320
3686 * device that is connected to the system.
3688 * @dd Pointer to the driver data structure.
3691 * 0 on success else an error code.
3693 static int mtip_block_initialize(struct driver_data *dd)
3695 int rv = 0, wait_for_rebuild = 0;
3697 unsigned int index = 0;
3698 struct kobject *kobj;
3699 unsigned char thd_name[16];
3702 goto skip_create_disk; /* hw init done, before rebuild */
3704 /* Initialize the protocol layer. */
3705 wait_for_rebuild = mtip_hw_init(dd);
3706 if (wait_for_rebuild < 0) {
3707 dev_err(&dd->pdev->dev,
3708 "Protocol layer initialization failed\n");
3710 goto protocol_init_error;
3713 dd->disk = alloc_disk(MTIP_MAX_MINORS);
3714 if (dd->disk == NULL) {
3715 dev_err(&dd->pdev->dev,
3716 "Unable to allocate gendisk structure\n");
3718 goto alloc_disk_error;
3721 /* Generate the disk name, implemented same as in sd.c */
3723 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3726 spin_lock(&rssd_index_lock);
3727 rv = ida_get_new(&rssd_index_ida, &index);
3728 spin_unlock(&rssd_index_lock);
3729 } while (rv == -EAGAIN);
3734 rv = rssd_disk_name_format("rssd",
3736 dd->disk->disk_name,
3739 goto disk_index_error;
3741 dd->disk->driverfs_dev = &dd->pdev->dev;
3742 dd->disk->major = dd->major;
3743 dd->disk->first_minor = dd->instance * MTIP_MAX_MINORS;
3744 dd->disk->fops = &mtip_block_ops;
3745 dd->disk->private_data = dd;
3749 * if rebuild pending, start the service thread, and delay the block
3750 * queue creation and add_disk()
3752 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3753 goto start_service_thread;
3756 /* Allocate the request queue. */
3757 dd->queue = blk_alloc_queue(GFP_KERNEL);
3758 if (dd->queue == NULL) {
3759 dev_err(&dd->pdev->dev,
3760 "Unable to allocate request queue\n");
3762 goto block_queue_alloc_init_error;
3765 /* Attach our request function to the request queue. */
3766 blk_queue_make_request(dd->queue, mtip_make_request);
3768 dd->disk->queue = dd->queue;
3769 dd->queue->queuedata = dd;
3771 /* Set device limits. */
3772 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
3773 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3774 blk_queue_physical_block_size(dd->queue, 4096);
3775 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3776 blk_queue_max_segment_size(dd->queue, 0x400000);
3777 blk_queue_io_min(dd->queue, 4096);
3780 * write back cache is not supported in the device. FUA depends on
3781 * write back cache support, hence setting flush support to zero.
3783 blk_queue_flush(dd->queue, 0);
3785 /* Set the capacity of the device in 512 byte sectors. */
3786 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3787 dev_warn(&dd->pdev->dev,
3788 "Could not read drive capacity\n");
3790 goto read_capacity_error;
3792 set_capacity(dd->disk, capacity);
3794 /* Enable the block device and add it to /dev */
3798 * Now that the disk is active, initialize any sysfs attributes
3799 * managed by the protocol layer.
3801 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3803 mtip_hw_sysfs_init(dd, kobj);
3806 mtip_hw_debugfs_init(dd);
3808 if (dd->mtip_svc_handler) {
3809 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
3810 return rv; /* service thread created for handling rebuild */
3813 start_service_thread:
3814 sprintf(thd_name, "mtip_svc_thd_%02d", index);
3816 dd->mtip_svc_handler = kthread_run(mtip_service_thread,
3819 if (IS_ERR(dd->mtip_svc_handler)) {
3820 dev_err(&dd->pdev->dev, "service thread failed to start\n");
3821 dd->mtip_svc_handler = NULL;
3823 goto kthread_run_error;
3826 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3827 rv = wait_for_rebuild;
3832 mtip_hw_debugfs_exit(dd);
3834 /* Delete our gendisk. This also removes the device from /dev */
3835 del_gendisk(dd->disk);
3837 read_capacity_error:
3838 blk_cleanup_queue(dd->queue);
3840 block_queue_alloc_init_error:
3842 spin_lock(&rssd_index_lock);
3843 ida_remove(&rssd_index_ida, index);
3844 spin_unlock(&rssd_index_lock);
3850 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
3852 protocol_init_error:
3857 * Block layer deinitialization function.
3859 * Called by the PCI layer as each P320 device is removed.
3861 * @dd Pointer to the driver data structure.
3866 static int mtip_block_remove(struct driver_data *dd)
3868 struct kobject *kobj;
3870 if (dd->mtip_svc_handler) {
3871 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
3872 wake_up_interruptible(&dd->port->svc_wait);
3873 kthread_stop(dd->mtip_svc_handler);
3876 /* Clean up the sysfs attributes, if created */
3877 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
3878 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3880 mtip_hw_sysfs_exit(dd, kobj);
3884 mtip_hw_debugfs_exit(dd);
3887 * Delete our gendisk structure. This also removes the device
3890 del_gendisk(dd->disk);
3892 spin_lock(&rssd_index_lock);
3893 ida_remove(&rssd_index_ida, dd->index);
3894 spin_unlock(&rssd_index_lock);
3896 blk_cleanup_queue(dd->queue);
3900 /* De-initialize the protocol layer. */
3907 * Function called by the PCI layer when just before the
3908 * machine shuts down.
3910 * If a protocol layer shutdown function is present it will be called
3913 * @dd Pointer to the driver data structure.
3918 static int mtip_block_shutdown(struct driver_data *dd)
3920 dev_info(&dd->pdev->dev,
3921 "Shutting down %s ...\n", dd->disk->disk_name);
3923 /* Delete our gendisk structure, and cleanup the blk queue. */
3924 del_gendisk(dd->disk);
3926 spin_lock(&rssd_index_lock);
3927 ida_remove(&rssd_index_ida, dd->index);
3928 spin_unlock(&rssd_index_lock);
3930 blk_cleanup_queue(dd->queue);
3934 mtip_hw_shutdown(dd);
3938 static int mtip_block_suspend(struct driver_data *dd)
3940 dev_info(&dd->pdev->dev,
3941 "Suspending %s ...\n", dd->disk->disk_name);
3942 mtip_hw_suspend(dd);
3946 static int mtip_block_resume(struct driver_data *dd)
3948 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
3949 dd->disk->disk_name);
3955 * Called for each supported PCI device detected.
3957 * This function allocates the private data structure, enables the
3958 * PCI device and then calls the block layer initialization function.
3961 * 0 on success else an error code.
3963 static int mtip_pci_probe(struct pci_dev *pdev,
3964 const struct pci_device_id *ent)
3967 struct driver_data *dd = NULL;
3969 /* Allocate memory for this devices private data. */
3970 dd = kzalloc(sizeof(struct driver_data), GFP_KERNEL);
3973 "Unable to allocate memory for driver data\n");
3977 /* Attach the private data to this PCI device. */
3978 pci_set_drvdata(pdev, dd);
3980 rv = pcim_enable_device(pdev);
3982 dev_err(&pdev->dev, "Unable to enable device\n");
3986 /* Map BAR5 to memory. */
3987 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
3989 dev_err(&pdev->dev, "Unable to map regions\n");
3993 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
3994 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
3997 rv = pci_set_consistent_dma_mask(pdev,
4000 dev_warn(&pdev->dev,
4001 "64-bit DMA enable failed\n");
4007 pci_set_master(pdev);
4009 if (pci_enable_msi(pdev)) {
4010 dev_warn(&pdev->dev,
4011 "Unable to enable MSI interrupt.\n");
4012 goto block_initialize_err;
4015 /* Copy the info we may need later into the private data structure. */
4016 dd->major = mtip_major;
4017 dd->instance = instance;
4020 /* Initialize the block layer. */
4021 rv = mtip_block_initialize(dd);
4024 "Unable to initialize block layer\n");
4025 goto block_initialize_err;
4029 * Increment the instance count so that each device has a unique
4033 if (rv != MTIP_FTL_REBUILD_MAGIC)
4034 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
4037 block_initialize_err:
4038 pci_disable_msi(pdev);
4041 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4045 pci_set_drvdata(pdev, NULL);
4052 * Called for each probed device when the device is removed or the
4053 * driver is unloaded.
4058 static void mtip_pci_remove(struct pci_dev *pdev)
4060 struct driver_data *dd = pci_get_drvdata(pdev);
4063 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
4065 if (mtip_check_surprise_removal(pdev)) {
4066 while (!test_bit(MTIP_DDF_CLEANUP_BIT, &dd->dd_flag)) {
4069 if (counter == 10) {
4070 /* Cleanup the outstanding commands */
4071 mtip_command_cleanup(dd);
4077 /* Clean up the block layer. */
4078 mtip_block_remove(dd);
4080 pci_disable_msi(pdev);
4083 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4087 * Called for each probed device when the device is suspended.
4093 static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4096 struct driver_data *dd = pci_get_drvdata(pdev);
4100 "Driver private datastructure is NULL\n");
4104 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
4106 /* Disable ports & interrupts then send standby immediate */
4107 rv = mtip_block_suspend(dd);
4110 "Failed to suspend controller\n");
4115 * Save the pci config space to pdev structure &
4116 * disable the device
4118 pci_save_state(pdev);
4119 pci_disable_device(pdev);
4121 /* Move to Low power state*/
4122 pci_set_power_state(pdev, PCI_D3hot);
4128 * Called for each probed device when the device is resumed.
4134 static int mtip_pci_resume(struct pci_dev *pdev)
4137 struct driver_data *dd;
4139 dd = pci_get_drvdata(pdev);
4142 "Driver private datastructure is NULL\n");
4146 /* Move the device to active State */
4147 pci_set_power_state(pdev, PCI_D0);
4149 /* Restore PCI configuration space */
4150 pci_restore_state(pdev);
4152 /* Enable the PCI device*/
4153 rv = pcim_enable_device(pdev);
4156 "Failed to enable card during resume\n");
4159 pci_set_master(pdev);
4162 * Calls hbaReset, initPort, & startPort function
4163 * then enables interrupts
4165 rv = mtip_block_resume(dd);
4167 dev_err(&pdev->dev, "Unable to resume\n");
4170 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
4181 static void mtip_pci_shutdown(struct pci_dev *pdev)
4183 struct driver_data *dd = pci_get_drvdata(pdev);
4185 mtip_block_shutdown(dd);
4188 /* Table of device ids supported by this driver. */
4189 static DEFINE_PCI_DEVICE_TABLE(mtip_pci_tbl) = {
4190 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4191 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4192 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4193 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4194 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4195 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4196 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
4200 /* Structure that describes the PCI driver functions. */
4201 static struct pci_driver mtip_pci_driver = {
4202 .name = MTIP_DRV_NAME,
4203 .id_table = mtip_pci_tbl,
4204 .probe = mtip_pci_probe,
4205 .remove = mtip_pci_remove,
4206 .suspend = mtip_pci_suspend,
4207 .resume = mtip_pci_resume,
4208 .shutdown = mtip_pci_shutdown,
4211 MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4214 * Module initialization function.
4216 * Called once when the module is loaded. This function allocates a major
4217 * block device number to the Cyclone devices and registers the PCI layer
4221 * 0 on success else error code.
4223 static int __init mtip_init(void)
4227 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
4229 /* Allocate a major block device number to use with this driver. */
4230 error = register_blkdev(0, MTIP_DRV_NAME);
4232 pr_err("Unable to register block device (%d)\n",
4239 dfs_parent = debugfs_create_dir("rssd", NULL);
4240 if (IS_ERR_OR_NULL(dfs_parent)) {
4241 pr_warn("Error creating debugfs parent\n");
4246 /* Register our PCI operations. */
4247 error = pci_register_driver(&mtip_pci_driver);
4249 debugfs_remove(dfs_parent);
4250 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4257 * Module de-initialization function.
4259 * Called once when the module is unloaded. This function deallocates
4260 * the major block device number allocated by mtip_init() and
4261 * unregisters the PCI layer of the driver.
4266 static void __exit mtip_exit(void)
4268 debugfs_remove_recursive(dfs_parent);
4270 /* Release the allocated major block device number. */
4271 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4273 /* Unregister the PCI driver. */
4274 pci_unregister_driver(&mtip_pci_driver);
4277 MODULE_AUTHOR("Micron Technology, Inc");
4278 MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4279 MODULE_LICENSE("GPL");
4280 MODULE_VERSION(MTIP_DRV_VERSION);
4282 module_init(mtip_init);
4283 module_exit(mtip_exit);