Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the Micron P320 SSD |
| 3 | * Copyright (C) 2011 Micron Technology, Inc. |
| 4 | * |
| 5 | * Portions of this code were derived from works subjected to the |
| 6 | * following copyright: |
| 7 | * Copyright (C) 2009 Integrated Device Technology, Inc. |
| 8 | * |
| 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. |
| 13 | * |
| 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. |
| 18 | * |
| 19 | */ |
| 20 | |
| 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> |
| 30 | #include <linux/fs.h> |
Jens Axboe | 0e838c6 | 2011-09-28 07:35:40 -0600 | [diff] [blame] | 31 | #include <linux/module.h> |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 32 | #include <linux/genhd.h> |
| 33 | #include <linux/blkdev.h> |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 34 | #include <linux/blk-mq.h> |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 35 | #include <linux/bio.h> |
| 36 | #include <linux/dma-mapping.h> |
| 37 | #include <linux/idr.h> |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 38 | #include <linux/kthread.h> |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 39 | #include <../drivers/ata/ahci.h> |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 40 | #include <linux/export.h> |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 41 | #include <linux/debugfs.h> |
Sam Bradshaw | f45c40a | 2014-06-06 13:28:48 -0600 | [diff] [blame] | 42 | #include <linux/prefetch.h> |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 43 | #include "mtip32xx.h" |
| 44 | |
| 45 | #define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32) |
Sam Bradshaw | 188b9f4 | 2014-01-15 10:14:57 -0800 | [diff] [blame] | 46 | |
| 47 | /* DMA region containing RX Fis, Identify, RLE10, and SMART buffers */ |
| 48 | #define AHCI_RX_FIS_SZ 0x100 |
| 49 | #define AHCI_RX_FIS_OFFSET 0x0 |
| 50 | #define AHCI_IDFY_SZ ATA_SECT_SIZE |
| 51 | #define AHCI_IDFY_OFFSET 0x400 |
| 52 | #define AHCI_SECTBUF_SZ ATA_SECT_SIZE |
| 53 | #define AHCI_SECTBUF_OFFSET 0x800 |
| 54 | #define AHCI_SMARTBUF_SZ ATA_SECT_SIZE |
| 55 | #define AHCI_SMARTBUF_OFFSET 0xC00 |
| 56 | /* 0x100 + 0x200 + 0x200 + 0x200 is smaller than 4k but we pad it out */ |
| 57 | #define BLOCK_DMA_ALLOC_SZ 4096 |
| 58 | |
| 59 | /* DMA region containing command table (should be 8192 bytes) */ |
| 60 | #define AHCI_CMD_SLOT_SZ sizeof(struct mtip_cmd_hdr) |
| 61 | #define AHCI_CMD_TBL_SZ (MTIP_MAX_COMMAND_SLOTS * AHCI_CMD_SLOT_SZ) |
| 62 | #define AHCI_CMD_TBL_OFFSET 0x0 |
| 63 | |
| 64 | /* DMA region per command (contains header and SGL) */ |
| 65 | #define AHCI_CMD_TBL_HDR_SZ 0x80 |
| 66 | #define AHCI_CMD_TBL_HDR_OFFSET 0x0 |
| 67 | #define AHCI_CMD_TBL_SGL_SZ (MTIP_MAX_SG * sizeof(struct mtip_cmd_sg)) |
| 68 | #define AHCI_CMD_TBL_SGL_OFFSET AHCI_CMD_TBL_HDR_SZ |
| 69 | #define CMD_DMA_ALLOC_SZ (AHCI_CMD_TBL_SGL_SZ + AHCI_CMD_TBL_HDR_SZ) |
| 70 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 71 | |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 72 | #define HOST_CAP_NZDMA (1 << 19) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 73 | #define HOST_HSORG 0xFC |
| 74 | #define HSORG_DISABLE_SLOTGRP_INTR (1<<24) |
| 75 | #define HSORG_DISABLE_SLOTGRP_PXIS (1<<16) |
| 76 | #define HSORG_HWREV 0xFF00 |
| 77 | #define HSORG_STYLE 0x8 |
| 78 | #define HSORG_SLOTGROUPS 0x7 |
| 79 | |
| 80 | #define PORT_COMMAND_ISSUE 0x38 |
| 81 | #define PORT_SDBV 0x7C |
| 82 | |
| 83 | #define PORT_OFFSET 0x100 |
| 84 | #define PORT_MEM_SIZE 0x80 |
| 85 | |
| 86 | #define PORT_IRQ_ERR \ |
| 87 | (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \ |
| 88 | PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \ |
| 89 | PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \ |
| 90 | PORT_IRQ_OVERFLOW) |
| 91 | #define PORT_IRQ_LEGACY \ |
| 92 | (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS) |
| 93 | #define PORT_IRQ_HANDLED \ |
| 94 | (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \ |
| 95 | PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \ |
| 96 | PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY) |
| 97 | #define DEF_PORT_IRQ \ |
| 98 | (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS) |
| 99 | |
| 100 | /* product numbers */ |
| 101 | #define MTIP_PRODUCT_UNKNOWN 0x00 |
| 102 | #define MTIP_PRODUCT_ASICFPGA 0x11 |
| 103 | |
| 104 | /* Device instance number, incremented each time a device is probed. */ |
| 105 | static int instance; |
| 106 | |
Zhu Yanjun | 9e35fdc | 2016-01-05 18:39:04 +0800 | [diff] [blame] | 107 | static struct list_head online_list; |
| 108 | static struct list_head removing_list; |
| 109 | static spinlock_t dev_lock; |
Asai Thambi S P | 0caff00 | 2013-04-03 19:56:21 +0530 | [diff] [blame] | 110 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 111 | /* |
| 112 | * Global variable used to hold the major block device number |
| 113 | * allocated in mtip_init(). |
| 114 | */ |
Jens Axboe | 3ff147d | 2011-09-27 21:33:53 -0600 | [diff] [blame] | 115 | static int mtip_major; |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 116 | static struct dentry *dfs_parent; |
Asai Thambi S P | 0caff00 | 2013-04-03 19:56:21 +0530 | [diff] [blame] | 117 | static struct dentry *dfs_device_status; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 118 | |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 119 | static u32 cpu_use[NR_CPUS]; |
| 120 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 121 | static DEFINE_SPINLOCK(rssd_index_lock); |
| 122 | static DEFINE_IDA(rssd_index_ida); |
| 123 | |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 124 | static int mtip_block_initialize(struct driver_data *dd); |
| 125 | |
Jens Axboe | 16d02c0 | 2011-09-27 15:50:01 -0600 | [diff] [blame] | 126 | #ifdef CONFIG_COMPAT |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 127 | struct mtip_compat_ide_task_request_s { |
| 128 | __u8 io_ports[8]; |
| 129 | __u8 hob_ports[8]; |
| 130 | ide_reg_valid_t out_flags; |
| 131 | ide_reg_valid_t in_flags; |
| 132 | int data_phase; |
| 133 | int req_cmd; |
| 134 | compat_ulong_t out_size; |
| 135 | compat_ulong_t in_size; |
| 136 | }; |
Jens Axboe | 16d02c0 | 2011-09-27 15:50:01 -0600 | [diff] [blame] | 137 | #endif |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 138 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 139 | /* |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 140 | * This function check_for_surprise_removal is called |
| 141 | * while card is removed from the system and it will |
| 142 | * read the vendor id from the configration space |
| 143 | * |
| 144 | * @pdev Pointer to the pci_dev structure. |
| 145 | * |
| 146 | * return value |
| 147 | * true if device removed, else false |
| 148 | */ |
| 149 | static bool mtip_check_surprise_removal(struct pci_dev *pdev) |
| 150 | { |
| 151 | u16 vendor_id = 0; |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 152 | struct driver_data *dd = pci_get_drvdata(pdev); |
| 153 | |
| 154 | if (dd->sr) |
| 155 | return true; |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 156 | |
| 157 | /* Read the vendorID from the configuration space */ |
| 158 | pci_read_config_word(pdev, 0x00, &vendor_id); |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 159 | if (vendor_id == 0xFFFF) { |
| 160 | dd->sr = true; |
| 161 | if (dd->queue) |
| 162 | set_bit(QUEUE_FLAG_DEAD, &dd->queue->queue_flags); |
| 163 | else |
| 164 | dev_warn(&dd->pdev->dev, |
| 165 | "%s: dd->queue is NULL\n", __func__); |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 166 | return true; /* device removed */ |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 167 | } |
| 168 | |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 169 | return false; /* device present */ |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 170 | } |
| 171 | |
Ming Lei | a4e84aa | 2017-04-27 07:45:18 -0600 | [diff] [blame^] | 172 | /* we have to use runtime tag to setup command header */ |
| 173 | static void mtip_init_cmd_header(struct request *rq) |
| 174 | { |
| 175 | struct driver_data *dd = rq->q->queuedata; |
| 176 | struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq); |
| 177 | u32 host_cap_64 = readl(dd->mmio + HOST_CAP) & HOST_CAP_64; |
| 178 | |
| 179 | /* Point the command headers at the command tables. */ |
| 180 | cmd->command_header = dd->port->command_list + |
| 181 | (sizeof(struct mtip_cmd_hdr) * rq->tag); |
| 182 | cmd->command_header_dma = dd->port->command_list_dma + |
| 183 | (sizeof(struct mtip_cmd_hdr) * rq->tag); |
| 184 | |
| 185 | if (host_cap_64) |
| 186 | cmd->command_header->ctbau = __force_bit2int cpu_to_le32((cmd->command_dma >> 16) >> 16); |
| 187 | |
| 188 | cmd->command_header->ctba = __force_bit2int cpu_to_le32(cmd->command_dma & 0xFFFFFFFF); |
| 189 | } |
| 190 | |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 191 | static struct mtip_cmd *mtip_get_int_command(struct driver_data *dd) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 192 | { |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 193 | struct request *rq; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 194 | |
Asai Thambi SP | 008e56d | 2016-02-24 21:21:20 -0800 | [diff] [blame] | 195 | if (mtip_check_surprise_removal(dd->pdev)) |
| 196 | return NULL; |
| 197 | |
Christoph Hellwig | 6f3b0e8 | 2015-11-26 09:13:05 +0100 | [diff] [blame] | 198 | rq = blk_mq_alloc_request(dd->queue, 0, BLK_MQ_REQ_RESERVED); |
Asai Thambi SP | 008e56d | 2016-02-24 21:21:20 -0800 | [diff] [blame] | 199 | if (IS_ERR(rq)) |
| 200 | return NULL; |
| 201 | |
Ming Lei | a4e84aa | 2017-04-27 07:45:18 -0600 | [diff] [blame^] | 202 | /* Internal cmd isn't submitted via .queue_rq */ |
| 203 | mtip_init_cmd_header(rq); |
| 204 | |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 205 | return blk_mq_rq_to_pdu(rq); |
| 206 | } |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 207 | |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 208 | static void mtip_put_int_command(struct driver_data *dd, struct mtip_cmd *cmd) |
| 209 | { |
| 210 | blk_put_request(blk_mq_rq_from_pdu(cmd)); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 214 | * Once we add support for one hctx per mtip group, this will change a bit |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 215 | */ |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 216 | static struct request *mtip_rq_from_tag(struct driver_data *dd, |
| 217 | unsigned int tag) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 218 | { |
Jens Axboe | 0e62f51 | 2014-06-04 10:23:49 -0600 | [diff] [blame] | 219 | struct blk_mq_hw_ctx *hctx = dd->queue->queue_hw_ctx[0]; |
| 220 | |
| 221 | return blk_mq_tag_to_rq(hctx->tags, tag); |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | static struct mtip_cmd *mtip_cmd_from_tag(struct driver_data *dd, |
| 225 | unsigned int tag) |
| 226 | { |
| 227 | struct request *rq = mtip_rq_from_tag(dd, tag); |
| 228 | |
| 229 | return blk_mq_rq_to_pdu(rq); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | /* |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 233 | * IO completion function. |
| 234 | * |
| 235 | * This completion function is called by the driver ISR when a |
| 236 | * command that was issued by the kernel completes. It first calls the |
| 237 | * asynchronous completion function which normally calls back into the block |
| 238 | * layer passing the asynchronous callback data, then unmaps the |
| 239 | * scatter list associated with the completed command, and finally |
| 240 | * clears the allocated bit associated with the completed command. |
| 241 | * |
| 242 | * @port Pointer to the port data structure. |
| 243 | * @tag Tag of the command. |
| 244 | * @data Pointer to driver_data. |
| 245 | * @status Completion status. |
| 246 | * |
| 247 | * return value |
| 248 | * None |
| 249 | */ |
| 250 | static void mtip_async_complete(struct mtip_port *port, |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 251 | int tag, struct mtip_cmd *cmd, int status) |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 252 | { |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 253 | struct driver_data *dd = port->dd; |
| 254 | struct request *rq; |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 255 | |
| 256 | if (unlikely(!dd) || unlikely(!port)) |
| 257 | return; |
| 258 | |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 259 | if (unlikely(status == PORT_IRQ_TF_ERR)) { |
| 260 | dev_warn(&port->dd->pdev->dev, |
| 261 | "Command tag %d failed due to TFE\n", tag); |
| 262 | } |
| 263 | |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 264 | rq = mtip_rq_from_tag(dd, tag); |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 265 | |
Christoph Hellwig | 4dda473 | 2017-04-20 16:03:07 +0200 | [diff] [blame] | 266 | cmd->status = status; |
Christoph Hellwig | 08e0029 | 2017-04-20 16:03:09 +0200 | [diff] [blame] | 267 | blk_mq_complete_request(rq); |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | /* |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 271 | * Reset the HBA (without sleeping) |
| 272 | * |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 273 | * @dd Pointer to the driver data structure. |
| 274 | * |
| 275 | * return value |
| 276 | * 0 The reset was successful. |
| 277 | * -1 The HBA Reset bit did not clear. |
| 278 | */ |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 279 | static int mtip_hba_reset(struct driver_data *dd) |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 280 | { |
| 281 | unsigned long timeout; |
| 282 | |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 283 | /* Set the reset bit */ |
| 284 | writel(HOST_RESET, dd->mmio + HOST_CTL); |
| 285 | |
| 286 | /* Flush */ |
| 287 | readl(dd->mmio + HOST_CTL); |
| 288 | |
Asai Thambi SP | 2f17d71 | 2015-05-11 15:57:16 -0700 | [diff] [blame] | 289 | /* |
| 290 | * Spin for up to 10 seconds waiting for reset acknowledgement. Spec |
| 291 | * is 1 sec but in LUN failure conditions, up to 10 secs are required |
| 292 | */ |
| 293 | timeout = jiffies + msecs_to_jiffies(10000); |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 294 | do { |
| 295 | mdelay(10); |
| 296 | if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) |
| 297 | return -1; |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 298 | |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 299 | } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET) |
| 300 | && time_before(jiffies, timeout)); |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 301 | |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 302 | if (readl(dd->mmio + HOST_CTL) & HOST_RESET) |
| 303 | return -1; |
| 304 | |
| 305 | return 0; |
| 306 | } |
| 307 | |
| 308 | /* |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 309 | * Issue a command to the hardware. |
| 310 | * |
| 311 | * Set the appropriate bit in the s_active and Command Issue hardware |
| 312 | * registers, causing hardware command processing to begin. |
| 313 | * |
| 314 | * @port Pointer to the port structure. |
| 315 | * @tag The tag of the command to be issued. |
| 316 | * |
| 317 | * return value |
| 318 | * None |
| 319 | */ |
| 320 | static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag) |
| 321 | { |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 322 | int group = tag >> 5; |
| 323 | |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 324 | /* guard SACT and CI registers */ |
| 325 | spin_lock(&port->cmd_issue_lock[group]); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 326 | writel((1 << MTIP_TAG_BIT(tag)), |
| 327 | port->s_active[MTIP_TAG_INDEX(tag)]); |
| 328 | writel((1 << MTIP_TAG_BIT(tag)), |
| 329 | port->cmd_issue[MTIP_TAG_INDEX(tag)]); |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 330 | spin_unlock(&port->cmd_issue_lock[group]); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | /* |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 334 | * Enable/disable the reception of FIS |
| 335 | * |
| 336 | * @port Pointer to the port data structure |
| 337 | * @enable 1 to enable, 0 to disable |
| 338 | * |
| 339 | * return value |
| 340 | * Previous state: 1 enabled, 0 disabled |
| 341 | */ |
| 342 | static int mtip_enable_fis(struct mtip_port *port, int enable) |
| 343 | { |
| 344 | u32 tmp; |
| 345 | |
| 346 | /* enable FIS reception */ |
| 347 | tmp = readl(port->mmio + PORT_CMD); |
| 348 | if (enable) |
| 349 | writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD); |
| 350 | else |
| 351 | writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD); |
| 352 | |
| 353 | /* Flush */ |
| 354 | readl(port->mmio + PORT_CMD); |
| 355 | |
| 356 | return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX)); |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Enable/disable the DMA engine |
| 361 | * |
| 362 | * @port Pointer to the port data structure |
| 363 | * @enable 1 to enable, 0 to disable |
| 364 | * |
| 365 | * return value |
| 366 | * Previous state: 1 enabled, 0 disabled. |
| 367 | */ |
| 368 | static int mtip_enable_engine(struct mtip_port *port, int enable) |
| 369 | { |
| 370 | u32 tmp; |
| 371 | |
| 372 | /* enable FIS reception */ |
| 373 | tmp = readl(port->mmio + PORT_CMD); |
| 374 | if (enable) |
| 375 | writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD); |
| 376 | else |
| 377 | writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD); |
| 378 | |
| 379 | readl(port->mmio + PORT_CMD); |
| 380 | return (((tmp & PORT_CMD_START) == PORT_CMD_START)); |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Enables the port DMA engine and FIS reception. |
| 385 | * |
| 386 | * return value |
| 387 | * None |
| 388 | */ |
| 389 | static inline void mtip_start_port(struct mtip_port *port) |
| 390 | { |
| 391 | /* Enable FIS reception */ |
| 392 | mtip_enable_fis(port, 1); |
| 393 | |
| 394 | /* Enable the DMA engine */ |
| 395 | mtip_enable_engine(port, 1); |
| 396 | } |
| 397 | |
| 398 | /* |
| 399 | * Deinitialize a port by disabling port interrupts, the DMA engine, |
| 400 | * and FIS reception. |
| 401 | * |
| 402 | * @port Pointer to the port structure |
| 403 | * |
| 404 | * return value |
| 405 | * None |
| 406 | */ |
| 407 | static inline void mtip_deinit_port(struct mtip_port *port) |
| 408 | { |
| 409 | /* Disable interrupts on this port */ |
| 410 | writel(0, port->mmio + PORT_IRQ_MASK); |
| 411 | |
| 412 | /* Disable the DMA engine */ |
| 413 | mtip_enable_engine(port, 0); |
| 414 | |
| 415 | /* Disable FIS reception */ |
| 416 | mtip_enable_fis(port, 0); |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Initialize a port. |
| 421 | * |
| 422 | * This function deinitializes the port by calling mtip_deinit_port() and |
| 423 | * then initializes it by setting the command header and RX FIS addresses, |
| 424 | * clearing the SError register and any pending port interrupts before |
| 425 | * re-enabling the default set of port interrupts. |
| 426 | * |
| 427 | * @port Pointer to the port structure. |
| 428 | * |
| 429 | * return value |
| 430 | * None |
| 431 | */ |
| 432 | static void mtip_init_port(struct mtip_port *port) |
| 433 | { |
| 434 | int i; |
| 435 | mtip_deinit_port(port); |
| 436 | |
| 437 | /* Program the command list base and FIS base addresses */ |
| 438 | if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) { |
| 439 | writel((port->command_list_dma >> 16) >> 16, |
| 440 | port->mmio + PORT_LST_ADDR_HI); |
| 441 | writel((port->rxfis_dma >> 16) >> 16, |
| 442 | port->mmio + PORT_FIS_ADDR_HI); |
| 443 | } |
| 444 | |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 445 | writel(port->command_list_dma & 0xFFFFFFFF, |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 446 | port->mmio + PORT_LST_ADDR); |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 447 | writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR); |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 448 | |
| 449 | /* Clear SError */ |
| 450 | writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR); |
| 451 | |
| 452 | /* reset the completed registers.*/ |
| 453 | for (i = 0; i < port->dd->slot_groups; i++) |
| 454 | writel(0xFFFFFFFF, port->completed[i]); |
| 455 | |
| 456 | /* Clear any pending interrupts for this port */ |
Asai Thambi S P | 6bb688c | 2012-05-29 18:40:45 -0700 | [diff] [blame] | 457 | writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT); |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 458 | |
Asai Thambi S P | 22be2e6 | 2012-03-23 12:33:03 +0100 | [diff] [blame] | 459 | /* Clear any pending interrupts on the HBA. */ |
| 460 | writel(readl(port->dd->mmio + HOST_IRQ_STAT), |
| 461 | port->dd->mmio + HOST_IRQ_STAT); |
| 462 | |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 463 | /* Enable port interrupts */ |
| 464 | writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK); |
| 465 | } |
| 466 | |
| 467 | /* |
| 468 | * Restart a port |
| 469 | * |
| 470 | * @port Pointer to the port data structure. |
| 471 | * |
| 472 | * return value |
| 473 | * None |
| 474 | */ |
| 475 | static void mtip_restart_port(struct mtip_port *port) |
| 476 | { |
| 477 | unsigned long timeout; |
| 478 | |
| 479 | /* Disable the DMA engine */ |
| 480 | mtip_enable_engine(port, 0); |
| 481 | |
| 482 | /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */ |
| 483 | timeout = jiffies + msecs_to_jiffies(500); |
| 484 | while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) |
| 485 | && time_before(jiffies, timeout)) |
| 486 | ; |
| 487 | |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 488 | if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag)) |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 489 | return; |
| 490 | |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 491 | /* |
| 492 | * Chip quirk: escalate to hba reset if |
| 493 | * PxCMD.CR not clear after 500 ms |
| 494 | */ |
| 495 | if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) { |
| 496 | dev_warn(&port->dd->pdev->dev, |
| 497 | "PxCMD.CR not clear, escalating reset\n"); |
| 498 | |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 499 | if (mtip_hba_reset(port->dd)) |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 500 | dev_err(&port->dd->pdev->dev, |
| 501 | "HBA reset escalation failed.\n"); |
| 502 | |
| 503 | /* 30 ms delay before com reset to quiesce chip */ |
| 504 | mdelay(30); |
| 505 | } |
| 506 | |
| 507 | dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n"); |
| 508 | |
| 509 | /* Set PxSCTL.DET */ |
| 510 | writel(readl(port->mmio + PORT_SCR_CTL) | |
| 511 | 1, port->mmio + PORT_SCR_CTL); |
| 512 | readl(port->mmio + PORT_SCR_CTL); |
| 513 | |
| 514 | /* Wait 1 ms to quiesce chip function */ |
| 515 | timeout = jiffies + msecs_to_jiffies(1); |
| 516 | while (time_before(jiffies, timeout)) |
| 517 | ; |
| 518 | |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 519 | if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag)) |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 520 | return; |
| 521 | |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 522 | /* Clear PxSCTL.DET */ |
| 523 | writel(readl(port->mmio + PORT_SCR_CTL) & ~1, |
| 524 | port->mmio + PORT_SCR_CTL); |
| 525 | readl(port->mmio + PORT_SCR_CTL); |
| 526 | |
| 527 | /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */ |
| 528 | timeout = jiffies + msecs_to_jiffies(500); |
| 529 | while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0) |
| 530 | && time_before(jiffies, timeout)) |
| 531 | ; |
| 532 | |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 533 | if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag)) |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 534 | return; |
| 535 | |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 536 | if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0) |
| 537 | dev_warn(&port->dd->pdev->dev, |
| 538 | "COM reset failed\n"); |
| 539 | |
Asai Thambi S P | 22be2e6 | 2012-03-23 12:33:03 +0100 | [diff] [blame] | 540 | mtip_init_port(port); |
| 541 | mtip_start_port(port); |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 542 | |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 543 | } |
| 544 | |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 545 | static int mtip_device_reset(struct driver_data *dd) |
| 546 | { |
| 547 | int rv = 0; |
| 548 | |
| 549 | if (mtip_check_surprise_removal(dd->pdev)) |
| 550 | return 0; |
| 551 | |
| 552 | if (mtip_hba_reset(dd) < 0) |
| 553 | rv = -EFAULT; |
| 554 | |
| 555 | mdelay(1); |
| 556 | mtip_init_port(dd->port); |
| 557 | mtip_start_port(dd->port); |
| 558 | |
| 559 | /* Enable interrupts on the HBA. */ |
| 560 | writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN, |
| 561 | dd->mmio + HOST_CTL); |
| 562 | return rv; |
| 563 | } |
| 564 | |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 565 | /* |
Asai Thambi S P | 95fea2f | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 566 | * Helper function for tag logging |
| 567 | */ |
| 568 | static void print_tags(struct driver_data *dd, |
| 569 | char *msg, |
| 570 | unsigned long *tagbits, |
| 571 | int cnt) |
| 572 | { |
| 573 | unsigned char tagmap[128]; |
| 574 | int group, tagmap_len = 0; |
| 575 | |
| 576 | memset(tagmap, 0, sizeof(tagmap)); |
| 577 | for (group = SLOTBITS_IN_LONGS; group > 0; group--) |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 578 | tagmap_len += sprintf(tagmap + tagmap_len, "%016lX ", |
Asai Thambi S P | 95fea2f | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 579 | tagbits[group-1]); |
| 580 | dev_warn(&dd->pdev->dev, |
| 581 | "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap); |
| 582 | } |
| 583 | |
| 584 | /* |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 585 | * Internal command completion callback function. |
| 586 | * |
| 587 | * This function is normally called by the driver ISR when an internal |
| 588 | * command completed. This function signals the command completion by |
| 589 | * calling complete(). |
| 590 | * |
| 591 | * @port Pointer to the port data structure. |
| 592 | * @tag Tag of the command that has completed. |
| 593 | * @data Pointer to a completion structure. |
| 594 | * @status Completion status. |
| 595 | * |
| 596 | * return value |
| 597 | * None |
| 598 | */ |
| 599 | static void mtip_completion(struct mtip_port *port, |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 600 | int tag, struct mtip_cmd *command, int status) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 601 | { |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 602 | struct completion *waiting = command->comp_data; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 603 | if (unlikely(status == PORT_IRQ_TF_ERR)) |
| 604 | dev_warn(&port->dd->pdev->dev, |
| 605 | "Internal command %d completed with TFE\n", tag); |
| 606 | |
Asai Thambi SP | 008e56d | 2016-02-24 21:21:20 -0800 | [diff] [blame] | 607 | command->comp_func = NULL; |
| 608 | command->comp_data = NULL; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 609 | complete(waiting); |
| 610 | } |
| 611 | |
Asai Thambi S P | 8182b49 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 612 | static void mtip_null_completion(struct mtip_port *port, |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 613 | int tag, struct mtip_cmd *command, int status) |
Asai Thambi S P | 8182b49 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 614 | { |
Asai Thambi S P | 8182b49 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 615 | } |
| 616 | |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 617 | static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer, |
| 618 | dma_addr_t buffer_dma, unsigned int sectors); |
| 619 | static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id, |
| 620 | struct smart_attr *attrib); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 621 | /* |
| 622 | * Handle an error. |
| 623 | * |
| 624 | * @dd Pointer to the DRIVER_DATA structure. |
| 625 | * |
| 626 | * return value |
| 627 | * None |
| 628 | */ |
| 629 | static void mtip_handle_tfe(struct driver_data *dd) |
| 630 | { |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 631 | int group, tag, bit, reissue, rv; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 632 | struct mtip_port *port; |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 633 | struct mtip_cmd *cmd; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 634 | u32 completed; |
| 635 | struct host_to_dev_fis *fis; |
| 636 | unsigned long tagaccum[SLOTBITS_IN_LONGS]; |
Asai Thambi S P | 95fea2f | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 637 | unsigned int cmd_cnt = 0; |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 638 | unsigned char *buf; |
| 639 | char *fail_reason = NULL; |
| 640 | int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 641 | |
| 642 | dev_warn(&dd->pdev->dev, "Taskfile error\n"); |
| 643 | |
| 644 | port = dd->port; |
| 645 | |
Asai Thambi SP | a7806fa | 2015-05-11 15:49:28 -0700 | [diff] [blame] | 646 | if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) { |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 647 | cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL); |
Asai Thambi S P | d02e1f0 | 2012-05-29 18:42:27 -0700 | [diff] [blame] | 648 | dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n"); |
| 649 | |
Asai Thambi S P | d02e1f0 | 2012-05-29 18:42:27 -0700 | [diff] [blame] | 650 | if (cmd->comp_data && cmd->comp_func) { |
| 651 | cmd->comp_func(port, MTIP_TAG_INTERNAL, |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 652 | cmd, PORT_IRQ_TF_ERR); |
Asai Thambi S P | d02e1f0 | 2012-05-29 18:42:27 -0700 | [diff] [blame] | 653 | } |
Asai Thambi SP | e35b947 | 2016-02-24 21:16:21 -0800 | [diff] [blame] | 654 | return; |
Asai Thambi S P | d02e1f0 | 2012-05-29 18:42:27 -0700 | [diff] [blame] | 655 | } |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 656 | |
Asai Thambi S P | 95fea2f | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 657 | /* clear the tag accumulator */ |
| 658 | memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long)); |
| 659 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 660 | /* Loop through all the groups */ |
| 661 | for (group = 0; group < dd->slot_groups; group++) { |
| 662 | completed = readl(port->completed[group]); |
| 663 | |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 664 | dev_warn(&dd->pdev->dev, "g=%u, comp=%x\n", group, completed); |
| 665 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 666 | /* clear completed status register in the hardware.*/ |
| 667 | writel(completed, port->completed[group]); |
| 668 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 669 | /* Process successfully completed commands */ |
| 670 | for (bit = 0; bit < 32 && completed; bit++) { |
| 671 | if (!(completed & (1<<bit))) |
| 672 | continue; |
| 673 | tag = (group << 5) + bit; |
| 674 | |
| 675 | /* Skip the internal command slot */ |
| 676 | if (tag == MTIP_TAG_INTERNAL) |
| 677 | continue; |
| 678 | |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 679 | cmd = mtip_cmd_from_tag(dd, tag); |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 680 | if (likely(cmd->comp_func)) { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 681 | set_bit(tag, tagaccum); |
Asai Thambi S P | 95fea2f | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 682 | cmd_cnt++; |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 683 | cmd->comp_func(port, tag, cmd, 0); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 684 | } else { |
| 685 | dev_err(&port->dd->pdev->dev, |
| 686 | "Missing completion func for tag %d", |
| 687 | tag); |
| 688 | if (mtip_check_surprise_removal(dd->pdev)) { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 689 | /* don't proceed further */ |
| 690 | return; |
| 691 | } |
| 692 | } |
| 693 | } |
| 694 | } |
Asai Thambi S P | 95fea2f | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 695 | |
| 696 | print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 697 | |
| 698 | /* Restart the port */ |
| 699 | mdelay(20); |
| 700 | mtip_restart_port(port); |
| 701 | |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 702 | /* Trying to determine the cause of the error */ |
| 703 | rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ, |
| 704 | dd->port->log_buf, |
| 705 | dd->port->log_buf_dma, 1); |
| 706 | if (rv) { |
| 707 | dev_warn(&dd->pdev->dev, |
| 708 | "Error in READ LOG EXT (10h) command\n"); |
| 709 | /* non-critical error, don't fail the load */ |
| 710 | } else { |
| 711 | buf = (unsigned char *)dd->port->log_buf; |
| 712 | if (buf[259] & 0x1) { |
| 713 | dev_info(&dd->pdev->dev, |
| 714 | "Write protect bit is set.\n"); |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 715 | set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag); |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 716 | fail_all_ncq_write = 1; |
| 717 | fail_reason = "write protect"; |
| 718 | } |
| 719 | if (buf[288] == 0xF7) { |
| 720 | dev_info(&dd->pdev->dev, |
| 721 | "Exceeded Tmax, drive in thermal shutdown.\n"); |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 722 | set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag); |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 723 | fail_all_ncq_cmds = 1; |
| 724 | fail_reason = "thermal shutdown"; |
| 725 | } |
| 726 | if (buf[288] == 0xBF) { |
Asai Thambi SP | aae4a03 | 2016-02-24 21:18:20 -0800 | [diff] [blame] | 727 | set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag); |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 728 | dev_info(&dd->pdev->dev, |
Sam Bradshaw | 26d5805 | 2013-10-03 10:18:05 -0700 | [diff] [blame] | 729 | "Drive indicates rebuild has failed. Secure erase required.\n"); |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 730 | fail_all_ncq_cmds = 1; |
| 731 | fail_reason = "rebuild failed"; |
| 732 | } |
| 733 | } |
| 734 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 735 | /* clear the tag accumulator */ |
| 736 | memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long)); |
| 737 | |
| 738 | /* Loop through all the groups */ |
| 739 | for (group = 0; group < dd->slot_groups; group++) { |
| 740 | for (bit = 0; bit < 32; bit++) { |
| 741 | reissue = 1; |
| 742 | tag = (group << 5) + bit; |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 743 | cmd = mtip_cmd_from_tag(dd, tag); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 744 | |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 745 | fis = (struct host_to_dev_fis *)cmd->command; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 746 | |
| 747 | /* Should re-issue? */ |
| 748 | if (tag == MTIP_TAG_INTERNAL || |
| 749 | fis->command == ATA_CMD_SET_FEATURES) |
| 750 | reissue = 0; |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 751 | else { |
| 752 | if (fail_all_ncq_cmds || |
| 753 | (fail_all_ncq_write && |
| 754 | fis->command == ATA_CMD_FPDMA_WRITE)) { |
| 755 | dev_warn(&dd->pdev->dev, |
| 756 | " Fail: %s w/tag %d [%s].\n", |
| 757 | fis->command == ATA_CMD_FPDMA_WRITE ? |
| 758 | "write" : "read", |
| 759 | tag, |
| 760 | fail_reason != NULL ? |
| 761 | fail_reason : "unknown"); |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 762 | if (cmd->comp_func) { |
| 763 | cmd->comp_func(port, tag, |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 764 | cmd, -ENODATA); |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 765 | } |
| 766 | continue; |
| 767 | } |
| 768 | } |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 769 | |
| 770 | /* |
| 771 | * First check if this command has |
| 772 | * exceeded its retries. |
| 773 | */ |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 774 | if (reissue && (cmd->retries-- > 0)) { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 775 | |
| 776 | set_bit(tag, tagaccum); |
| 777 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 778 | /* Re-issue the command. */ |
| 779 | mtip_issue_ncq_command(port, tag); |
| 780 | |
| 781 | continue; |
| 782 | } |
| 783 | |
| 784 | /* Retire a command that will not be reissued */ |
| 785 | dev_warn(&port->dd->pdev->dev, |
| 786 | "retiring tag %d\n", tag); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 787 | |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 788 | if (cmd->comp_func) |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 789 | cmd->comp_func(port, tag, cmd, PORT_IRQ_TF_ERR); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 790 | else |
| 791 | dev_warn(&port->dd->pdev->dev, |
| 792 | "Bad completion for tag %d\n", |
| 793 | tag); |
| 794 | } |
| 795 | } |
Asai Thambi S P | 95fea2f | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 796 | print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 797 | } |
| 798 | |
| 799 | /* |
| 800 | * Handle a set device bits interrupt |
| 801 | */ |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 802 | static inline void mtip_workq_sdbfx(struct mtip_port *port, int group, |
| 803 | u32 completed) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 804 | { |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 805 | struct driver_data *dd = port->dd; |
| 806 | int tag, bit; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 807 | struct mtip_cmd *command; |
| 808 | |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 809 | if (!completed) { |
| 810 | WARN_ON_ONCE(!completed); |
| 811 | return; |
| 812 | } |
| 813 | /* clear completed status register in the hardware.*/ |
| 814 | writel(completed, port->completed[group]); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 815 | |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 816 | /* Process completed commands. */ |
| 817 | for (bit = 0; (bit < 32) && completed; bit++) { |
| 818 | if (completed & 0x01) { |
| 819 | tag = (group << 5) | bit; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 820 | |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 821 | /* skip internal command slot. */ |
| 822 | if (unlikely(tag == MTIP_TAG_INTERNAL)) |
| 823 | continue; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 824 | |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 825 | command = mtip_cmd_from_tag(dd, tag); |
| 826 | if (likely(command->comp_func)) |
| 827 | command->comp_func(port, tag, command, 0); |
| 828 | else { |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 829 | dev_dbg(&dd->pdev->dev, |
| 830 | "Null completion for tag %d", |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 831 | tag); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 832 | |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 833 | if (mtip_check_surprise_removal( |
| 834 | dd->pdev)) { |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 835 | return; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 836 | } |
| 837 | } |
| 838 | } |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 839 | completed >>= 1; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 840 | } |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 841 | |
| 842 | /* If last, re-enable interrupts */ |
| 843 | if (atomic_dec_return(&dd->irq_workers_active) == 0) |
| 844 | writel(0xffffffff, dd->mmio + HOST_IRQ_STAT); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 845 | } |
| 846 | |
| 847 | /* |
| 848 | * Process legacy pio and d2h interrupts |
| 849 | */ |
| 850 | static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat) |
| 851 | { |
| 852 | struct mtip_port *port = dd->port; |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 853 | struct mtip_cmd *cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 854 | |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 855 | if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) && |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 856 | (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL]) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 857 | & (1 << MTIP_TAG_INTERNAL))) { |
| 858 | if (cmd->comp_func) { |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 859 | cmd->comp_func(port, MTIP_TAG_INTERNAL, cmd, 0); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 860 | return; |
| 861 | } |
| 862 | } |
| 863 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 864 | return; |
| 865 | } |
| 866 | |
| 867 | /* |
| 868 | * Demux and handle errors |
| 869 | */ |
| 870 | static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat) |
| 871 | { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 872 | |
| 873 | if (unlikely(port_stat & PORT_IRQ_CONNECT)) { |
| 874 | dev_warn(&dd->pdev->dev, |
| 875 | "Clearing PxSERR.DIAG.x\n"); |
| 876 | writel((1 << 26), dd->port->mmio + PORT_SCR_ERR); |
| 877 | } |
| 878 | |
| 879 | if (unlikely(port_stat & PORT_IRQ_PHYRDY)) { |
| 880 | dev_warn(&dd->pdev->dev, |
| 881 | "Clearing PxSERR.DIAG.n\n"); |
| 882 | writel((1 << 16), dd->port->mmio + PORT_SCR_ERR); |
| 883 | } |
| 884 | |
| 885 | if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) { |
| 886 | dev_warn(&dd->pdev->dev, |
| 887 | "Port stat errors %x unhandled\n", |
| 888 | (port_stat & ~PORT_IRQ_HANDLED)); |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 889 | if (mtip_check_surprise_removal(dd->pdev)) |
| 890 | return; |
| 891 | } |
| 892 | if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR))) { |
| 893 | set_bit(MTIP_PF_EH_ACTIVE_BIT, &dd->port->flags); |
| 894 | wake_up_interruptible(&dd->port->svc_wait); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
| 898 | static inline irqreturn_t mtip_handle_irq(struct driver_data *data) |
| 899 | { |
| 900 | struct driver_data *dd = (struct driver_data *) data; |
| 901 | struct mtip_port *port = dd->port; |
| 902 | u32 hba_stat, port_stat; |
| 903 | int rv = IRQ_NONE; |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 904 | int do_irq_enable = 1, i, workers; |
| 905 | struct mtip_work *twork; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 906 | |
| 907 | hba_stat = readl(dd->mmio + HOST_IRQ_STAT); |
| 908 | if (hba_stat) { |
| 909 | rv = IRQ_HANDLED; |
| 910 | |
| 911 | /* Acknowledge the interrupt status on the port.*/ |
| 912 | port_stat = readl(port->mmio + PORT_IRQ_STAT); |
Asai Thambi SP | 2132a54 | 2015-05-11 15:53:18 -0700 | [diff] [blame] | 913 | if (unlikely(port_stat == 0xFFFFFFFF)) { |
| 914 | mtip_check_surprise_removal(dd->pdev); |
| 915 | return IRQ_HANDLED; |
| 916 | } |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 917 | writel(port_stat, port->mmio + PORT_IRQ_STAT); |
| 918 | |
| 919 | /* Demux port status */ |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 920 | if (likely(port_stat & PORT_IRQ_SDB_FIS)) { |
| 921 | do_irq_enable = 0; |
| 922 | WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0); |
| 923 | |
| 924 | /* Start at 1: group zero is always local? */ |
| 925 | for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS; |
| 926 | i++) { |
| 927 | twork = &dd->work[i]; |
| 928 | twork->completed = readl(port->completed[i]); |
| 929 | if (twork->completed) |
| 930 | workers++; |
| 931 | } |
| 932 | |
| 933 | atomic_set(&dd->irq_workers_active, workers); |
| 934 | if (workers) { |
| 935 | for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) { |
| 936 | twork = &dd->work[i]; |
| 937 | if (twork->completed) |
| 938 | queue_work_on( |
| 939 | twork->cpu_binding, |
| 940 | dd->isr_workq, |
| 941 | &twork->work); |
| 942 | } |
| 943 | |
| 944 | if (likely(dd->work[0].completed)) |
| 945 | mtip_workq_sdbfx(port, 0, |
| 946 | dd->work[0].completed); |
| 947 | |
| 948 | } else { |
| 949 | /* |
| 950 | * Chip quirk: SDB interrupt but nothing |
| 951 | * to complete |
| 952 | */ |
| 953 | do_irq_enable = 1; |
| 954 | } |
| 955 | } |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 956 | |
| 957 | if (unlikely(port_stat & PORT_IRQ_ERR)) { |
| 958 | if (unlikely(mtip_check_surprise_removal(dd->pdev))) { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 959 | /* don't proceed further */ |
| 960 | return IRQ_HANDLED; |
| 961 | } |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 962 | if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 963 | &dd->dd_flag)) |
| 964 | return rv; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 965 | |
| 966 | mtip_process_errors(dd, port_stat & PORT_IRQ_ERR); |
| 967 | } |
| 968 | |
| 969 | if (unlikely(port_stat & PORT_IRQ_LEGACY)) |
| 970 | mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY); |
| 971 | } |
| 972 | |
| 973 | /* acknowledge interrupt */ |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 974 | if (unlikely(do_irq_enable)) |
| 975 | writel(hba_stat, dd->mmio + HOST_IRQ_STAT); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 976 | |
| 977 | return rv; |
| 978 | } |
| 979 | |
| 980 | /* |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 981 | * HBA interrupt subroutine. |
| 982 | * |
| 983 | * @irq IRQ number. |
| 984 | * @instance Pointer to the driver data structure. |
| 985 | * |
| 986 | * return value |
| 987 | * IRQ_HANDLED A HBA interrupt was pending and handled. |
| 988 | * IRQ_NONE This interrupt was not for the HBA. |
| 989 | */ |
| 990 | static irqreturn_t mtip_irq_handler(int irq, void *instance) |
| 991 | { |
| 992 | struct driver_data *dd = instance; |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 993 | |
| 994 | return mtip_handle_irq(dd); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag) |
| 998 | { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 999 | writel(1 << MTIP_TAG_BIT(tag), |
| 1000 | port->cmd_issue[MTIP_TAG_INDEX(tag)]); |
| 1001 | } |
| 1002 | |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 1003 | static bool mtip_pause_ncq(struct mtip_port *port, |
| 1004 | struct host_to_dev_fis *fis) |
| 1005 | { |
| 1006 | struct host_to_dev_fis *reply; |
| 1007 | unsigned long task_file_data; |
| 1008 | |
| 1009 | reply = port->rxfis + RX_FIS_D2H_REG; |
| 1010 | task_file_data = readl(port->mmio+PORT_TFDATA); |
| 1011 | |
Asai Thambi S P | 12a166c | 2012-09-05 22:01:36 +0530 | [diff] [blame] | 1012 | if ((task_file_data & 1)) |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 1013 | return false; |
| 1014 | |
| 1015 | if (fis->command == ATA_CMD_SEC_ERASE_PREP) { |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 1016 | port->ic_pause_timer = jiffies; |
| 1017 | return true; |
| 1018 | } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) && |
| 1019 | (fis->features == 0x03)) { |
| 1020 | set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags); |
| 1021 | port->ic_pause_timer = jiffies; |
| 1022 | return true; |
| 1023 | } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) || |
| 1024 | ((fis->command == 0xFC) && |
| 1025 | (fis->features == 0x27 || fis->features == 0x72 || |
| 1026 | fis->features == 0x62 || fis->features == 0x26))) { |
Asai Thambi SP | ee04bed | 2015-05-11 15:50:50 -0700 | [diff] [blame] | 1027 | clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag); |
Asai Thambi SP | aae4a03 | 2016-02-24 21:18:20 -0800 | [diff] [blame] | 1028 | clear_bit(MTIP_DDF_REBUILD_FAILED_BIT, &port->dd->dd_flag); |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 1029 | /* Com reset after secure erase or lowlevel format */ |
| 1030 | mtip_restart_port(port); |
Asai Thambi SP | 686d8e0 | 2015-05-11 15:51:27 -0700 | [diff] [blame] | 1031 | clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags); |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 1032 | return false; |
| 1033 | } |
| 1034 | |
| 1035 | return false; |
| 1036 | } |
| 1037 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1038 | /* |
| 1039 | * Wait for port to quiesce |
| 1040 | * |
| 1041 | * @port Pointer to port data structure |
| 1042 | * @timeout Max duration to wait (ms) |
Asai Thambi SP | 008e56d | 2016-02-24 21:21:20 -0800 | [diff] [blame] | 1043 | * @atomic gfp_t flag to indicate blockable context or not |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1044 | * |
| 1045 | * return value |
| 1046 | * 0 Success |
| 1047 | * -EBUSY Commands still active |
| 1048 | */ |
Asai Thambi SP | 008e56d | 2016-02-24 21:21:20 -0800 | [diff] [blame] | 1049 | static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout, |
| 1050 | gfp_t atomic) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1051 | { |
| 1052 | unsigned long to; |
Dan Carpenter | 3e54a3d | 2011-11-24 12:59:00 +0100 | [diff] [blame] | 1053 | unsigned int n; |
| 1054 | unsigned int active = 1; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1055 | |
Jens Axboe | 9acf03c | 2014-05-14 08:22:56 -0600 | [diff] [blame] | 1056 | blk_mq_stop_hw_queues(port->dd->queue); |
| 1057 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1058 | to = jiffies + msecs_to_jiffies(timeout); |
| 1059 | do { |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1060 | if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) && |
Asai Thambi SP | 008e56d | 2016-02-24 21:21:20 -0800 | [diff] [blame] | 1061 | test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags) && |
| 1062 | atomic == GFP_KERNEL) { |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 1063 | msleep(20); |
| 1064 | continue; /* svc thd is actively issuing commands */ |
| 1065 | } |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 1066 | |
Asai Thambi SP | 008e56d | 2016-02-24 21:21:20 -0800 | [diff] [blame] | 1067 | if (atomic == GFP_KERNEL) |
| 1068 | msleep(100); |
| 1069 | else { |
| 1070 | cpu_relax(); |
| 1071 | udelay(100); |
| 1072 | } |
| 1073 | |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 1074 | if (mtip_check_surprise_removal(port->dd->pdev)) |
| 1075 | goto err_fault; |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 1076 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1077 | /* |
| 1078 | * Ignore s_active bit 0 of array element 0. |
| 1079 | * This bit will always be set |
| 1080 | */ |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 1081 | active = readl(port->s_active[0]) & 0xFFFFFFFE; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1082 | for (n = 1; n < port->dd->slot_groups; n++) |
| 1083 | active |= readl(port->s_active[n]); |
| 1084 | |
| 1085 | if (!active) |
| 1086 | break; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1087 | } while (time_before(jiffies, to)); |
| 1088 | |
Jens Axboe | 9acf03c | 2014-05-14 08:22:56 -0600 | [diff] [blame] | 1089 | blk_mq_start_stopped_hw_queues(port->dd->queue, true); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1090 | return active ? -EBUSY : 0; |
Jens Axboe | 9acf03c | 2014-05-14 08:22:56 -0600 | [diff] [blame] | 1091 | err_fault: |
| 1092 | blk_mq_start_stopped_hw_queues(port->dd->queue, true); |
| 1093 | return -EFAULT; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | /* |
| 1097 | * Execute an internal command and wait for the completion. |
| 1098 | * |
| 1099 | * @port Pointer to the port data structure. |
| 1100 | * @fis Pointer to the FIS that describes the command. |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 1101 | * @fis_len Length in WORDS of the FIS. |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1102 | * @buffer DMA accessible for command data. |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 1103 | * @buf_len Length, in bytes, of the data buffer. |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1104 | * @opts Command header options, excluding the FIS length |
| 1105 | * and the number of PRD entries. |
| 1106 | * @timeout Time in ms to wait for the command to complete. |
| 1107 | * |
| 1108 | * return value |
| 1109 | * 0 Command completed successfully. |
| 1110 | * -EFAULT The buffer address is not correctly aligned. |
| 1111 | * -EBUSY Internal command or other IO in progress. |
| 1112 | * -EAGAIN Time out waiting for command to complete. |
| 1113 | */ |
| 1114 | static int mtip_exec_internal_command(struct mtip_port *port, |
Asai Thambi S P | 8182b49 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1115 | struct host_to_dev_fis *fis, |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 1116 | int fis_len, |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1117 | dma_addr_t buffer, |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 1118 | int buf_len, |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1119 | u32 opts, |
| 1120 | gfp_t atomic, |
| 1121 | unsigned long timeout) |
| 1122 | { |
| 1123 | struct mtip_cmd_sg *command_sg; |
| 1124 | DECLARE_COMPLETION_ONSTACK(wait); |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 1125 | struct mtip_cmd *int_cmd; |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 1126 | struct driver_data *dd = port->dd; |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 1127 | int rv = 0; |
Asai Thambi SP | 5b7e0a8 | 2016-02-24 21:16:38 -0800 | [diff] [blame] | 1128 | unsigned long start; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1129 | |
| 1130 | /* Make sure the buffer is 8 byte aligned. This is asic specific. */ |
| 1131 | if (buffer & 0x00000007) { |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 1132 | dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n"); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1133 | return -EFAULT; |
| 1134 | } |
| 1135 | |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 1136 | int_cmd = mtip_get_int_command(dd); |
Asai Thambi SP | 008e56d | 2016-02-24 21:21:20 -0800 | [diff] [blame] | 1137 | if (!int_cmd) { |
| 1138 | dbg_printk(MTIP_DRV_NAME "Unable to allocate tag for PIO cmd\n"); |
| 1139 | return -EFAULT; |
| 1140 | } |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 1141 | |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1142 | set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags); |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 1143 | |
Asai Thambi SP | 686d8e0 | 2015-05-11 15:51:27 -0700 | [diff] [blame] | 1144 | if (fis->command == ATA_CMD_SEC_ERASE_PREP) |
| 1145 | set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags); |
| 1146 | |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 1147 | clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1148 | |
| 1149 | if (atomic == GFP_KERNEL) { |
Asai Thambi S P | 8182b49 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1150 | if (fis->command != ATA_CMD_STANDBYNOW1) { |
| 1151 | /* wait for io to complete if non atomic */ |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 1152 | if (mtip_quiesce_io(port, |
Asai Thambi SP | 008e56d | 2016-02-24 21:21:20 -0800 | [diff] [blame] | 1153 | MTIP_QUIESCE_IO_TIMEOUT_MS, atomic) < 0) { |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 1154 | dev_warn(&dd->pdev->dev, |
Asai Thambi S P | 8182b49 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1155 | "Failed to quiesce IO\n"); |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 1156 | mtip_put_int_command(dd, int_cmd); |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1157 | clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags); |
Asai Thambi S P | 8182b49 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1158 | wake_up_interruptible(&port->svc_wait); |
| 1159 | return -EBUSY; |
| 1160 | } |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1161 | } |
| 1162 | |
| 1163 | /* Set the completion function and data for the command. */ |
| 1164 | int_cmd->comp_data = &wait; |
| 1165 | int_cmd->comp_func = mtip_completion; |
| 1166 | |
| 1167 | } else { |
| 1168 | /* Clear completion - we're going to poll */ |
| 1169 | int_cmd->comp_data = NULL; |
Asai Thambi S P | 8182b49 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1170 | int_cmd->comp_func = mtip_null_completion; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1171 | } |
| 1172 | |
| 1173 | /* Copy the command to the command table */ |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 1174 | memcpy(int_cmd->command, fis, fis_len*4); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1175 | |
| 1176 | /* Populate the SG list */ |
| 1177 | int_cmd->command_header->opts = |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 1178 | __force_bit2int cpu_to_le32(opts | fis_len); |
| 1179 | if (buf_len) { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1180 | command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ; |
| 1181 | |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 1182 | command_sg->info = |
| 1183 | __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF); |
| 1184 | command_sg->dba = |
| 1185 | __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF); |
| 1186 | command_sg->dba_upper = |
| 1187 | __force_bit2int cpu_to_le32((buffer >> 16) >> 16); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1188 | |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 1189 | int_cmd->command_header->opts |= |
| 1190 | __force_bit2int cpu_to_le32((1 << 16)); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | /* Populate the command header */ |
| 1194 | int_cmd->command_header->byte_count = 0; |
| 1195 | |
Asai Thambi SP | 5b7e0a8 | 2016-02-24 21:16:38 -0800 | [diff] [blame] | 1196 | start = jiffies; |
| 1197 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1198 | /* Issue the command to the hardware */ |
| 1199 | mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL); |
| 1200 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1201 | if (atomic == GFP_KERNEL) { |
| 1202 | /* Wait for the command to complete or timeout. */ |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 1203 | if ((rv = wait_for_completion_interruptible_timeout( |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1204 | &wait, |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 1205 | msecs_to_jiffies(timeout))) <= 0) { |
Asai Thambi SP | aae4a03 | 2016-02-24 21:18:20 -0800 | [diff] [blame] | 1206 | |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 1207 | if (rv == -ERESTARTSYS) { /* interrupted */ |
| 1208 | dev_err(&dd->pdev->dev, |
Asai Thambi SP | 5b7e0a8 | 2016-02-24 21:16:38 -0800 | [diff] [blame] | 1209 | "Internal command [%02X] was interrupted after %u ms\n", |
| 1210 | fis->command, |
| 1211 | jiffies_to_msecs(jiffies - start)); |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 1212 | rv = -EINTR; |
| 1213 | goto exec_ic_exit; |
| 1214 | } else if (rv == 0) /* timeout */ |
| 1215 | dev_err(&dd->pdev->dev, |
| 1216 | "Internal command did not complete [%02X] within timeout of %lu ms\n", |
| 1217 | fis->command, timeout); |
| 1218 | else |
| 1219 | dev_err(&dd->pdev->dev, |
| 1220 | "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n", |
| 1221 | fis->command, rv, timeout); |
| 1222 | |
| 1223 | if (mtip_check_surprise_removal(dd->pdev) || |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1224 | test_bit(MTIP_DDF_REMOVE_PENDING_BIT, |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 1225 | &dd->dd_flag)) { |
| 1226 | dev_err(&dd->pdev->dev, |
| 1227 | "Internal command [%02X] wait returned due to SR\n", |
| 1228 | fis->command); |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1229 | rv = -ENXIO; |
| 1230 | goto exec_ic_exit; |
| 1231 | } |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 1232 | mtip_device_reset(dd); /* recover from timeout issue */ |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1233 | rv = -EAGAIN; |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 1234 | goto exec_ic_exit; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1235 | } |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1236 | } else { |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 1237 | u32 hba_stat, port_stat; |
| 1238 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1239 | /* Spin for <timeout> checking if command still outstanding */ |
| 1240 | timeout = jiffies + msecs_to_jiffies(timeout); |
Asai Thambi S P | 8182b49 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1241 | while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL]) |
| 1242 | & (1 << MTIP_TAG_INTERNAL)) |
| 1243 | && time_before(jiffies, timeout)) { |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 1244 | if (mtip_check_surprise_removal(dd->pdev)) { |
Asai Thambi S P | 8182b49 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1245 | rv = -ENXIO; |
| 1246 | goto exec_ic_exit; |
| 1247 | } |
| 1248 | if ((fis->command != ATA_CMD_STANDBYNOW1) && |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1249 | test_bit(MTIP_DDF_REMOVE_PENDING_BIT, |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 1250 | &dd->dd_flag)) { |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1251 | rv = -ENXIO; |
| 1252 | goto exec_ic_exit; |
| 1253 | } |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 1254 | port_stat = readl(port->mmio + PORT_IRQ_STAT); |
| 1255 | if (!port_stat) |
| 1256 | continue; |
| 1257 | |
| 1258 | if (port_stat & PORT_IRQ_ERR) { |
| 1259 | dev_err(&dd->pdev->dev, |
| 1260 | "Internal command [%02X] failed\n", |
| 1261 | fis->command); |
| 1262 | mtip_device_reset(dd); |
| 1263 | rv = -EIO; |
| 1264 | goto exec_ic_exit; |
| 1265 | } else { |
| 1266 | writel(port_stat, port->mmio + PORT_IRQ_STAT); |
| 1267 | hba_stat = readl(dd->mmio + HOST_IRQ_STAT); |
| 1268 | if (hba_stat) |
| 1269 | writel(hba_stat, |
| 1270 | dd->mmio + HOST_IRQ_STAT); |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1271 | } |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 1272 | break; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1273 | } |
| 1274 | } |
Asai Thambi S P | d02e1f0 | 2012-05-29 18:42:27 -0700 | [diff] [blame] | 1275 | |
Asai Thambi S P | d02e1f0 | 2012-05-29 18:42:27 -0700 | [diff] [blame] | 1276 | if (readl(port->cmd_issue[MTIP_TAG_INTERNAL]) |
| 1277 | & (1 << MTIP_TAG_INTERNAL)) { |
| 1278 | rv = -ENXIO; |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 1279 | if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) { |
| 1280 | mtip_device_reset(dd); |
Asai Thambi S P | d02e1f0 | 2012-05-29 18:42:27 -0700 | [diff] [blame] | 1281 | rv = -EAGAIN; |
| 1282 | } |
| 1283 | } |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1284 | exec_ic_exit: |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1285 | /* Clear the allocated and active bits for the internal command. */ |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 1286 | mtip_put_int_command(dd, int_cmd); |
Asai Thambi SP | 686d8e0 | 2015-05-11 15:51:27 -0700 | [diff] [blame] | 1287 | clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags); |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 1288 | if (rv >= 0 && mtip_pause_ncq(port, fis)) { |
| 1289 | /* NCQ paused */ |
| 1290 | return rv; |
| 1291 | } |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 1292 | wake_up_interruptible(&port->svc_wait); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1293 | |
| 1294 | return rv; |
| 1295 | } |
| 1296 | |
| 1297 | /* |
| 1298 | * Byte-swap ATA ID strings. |
| 1299 | * |
| 1300 | * ATA identify data contains strings in byte-swapped 16-bit words. |
| 1301 | * They must be swapped (on all architectures) to be usable as C strings. |
| 1302 | * This function swaps bytes in-place. |
| 1303 | * |
| 1304 | * @buf The buffer location of the string |
| 1305 | * @len The number of bytes to swap |
| 1306 | * |
| 1307 | * return value |
| 1308 | * None |
| 1309 | */ |
| 1310 | static inline void ata_swap_string(u16 *buf, unsigned int len) |
| 1311 | { |
| 1312 | int i; |
| 1313 | for (i = 0; i < (len/2); i++) |
| 1314 | be16_to_cpus(&buf[i]); |
| 1315 | } |
| 1316 | |
Asai Thambi S P | 670a641 | 2014-04-16 20:27:54 -0700 | [diff] [blame] | 1317 | static void mtip_set_timeout(struct driver_data *dd, |
| 1318 | struct host_to_dev_fis *fis, |
| 1319 | unsigned int *timeout, u8 erasemode) |
| 1320 | { |
| 1321 | switch (fis->command) { |
| 1322 | case ATA_CMD_DOWNLOAD_MICRO: |
| 1323 | *timeout = 120000; /* 2 minutes */ |
| 1324 | break; |
| 1325 | case ATA_CMD_SEC_ERASE_UNIT: |
| 1326 | case 0xFC: |
| 1327 | if (erasemode) |
| 1328 | *timeout = ((*(dd->port->identify + 90) * 2) * 60000); |
| 1329 | else |
| 1330 | *timeout = ((*(dd->port->identify + 89) * 2) * 60000); |
| 1331 | break; |
| 1332 | case ATA_CMD_STANDBYNOW1: |
| 1333 | *timeout = 120000; /* 2 minutes */ |
| 1334 | break; |
| 1335 | case 0xF7: |
| 1336 | case 0xFA: |
| 1337 | *timeout = 60000; /* 60 seconds */ |
| 1338 | break; |
| 1339 | case ATA_CMD_SMART: |
| 1340 | *timeout = 15000; /* 15 seconds */ |
| 1341 | break; |
| 1342 | default: |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 1343 | *timeout = MTIP_IOCTL_CMD_TIMEOUT_MS; |
Asai Thambi S P | 670a641 | 2014-04-16 20:27:54 -0700 | [diff] [blame] | 1344 | break; |
| 1345 | } |
| 1346 | } |
| 1347 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1348 | /* |
| 1349 | * Request the device identity information. |
| 1350 | * |
| 1351 | * If a user space buffer is not specified, i.e. is NULL, the |
| 1352 | * identify information is still read from the drive and placed |
| 1353 | * into the identify data buffer (@e port->identify) in the |
| 1354 | * port data structure. |
| 1355 | * When the identify buffer contains valid identify information @e |
| 1356 | * port->identify_valid is non-zero. |
| 1357 | * |
| 1358 | * @port Pointer to the port structure. |
| 1359 | * @user_buffer A user space buffer where the identify data should be |
| 1360 | * copied. |
| 1361 | * |
| 1362 | * return value |
| 1363 | * 0 Command completed successfully. |
| 1364 | * -EFAULT An error occurred while coping data to the user buffer. |
| 1365 | * -1 Command failed. |
| 1366 | */ |
| 1367 | static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer) |
| 1368 | { |
| 1369 | int rv = 0; |
| 1370 | struct host_to_dev_fis fis; |
| 1371 | |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1372 | if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag)) |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1373 | return -EFAULT; |
| 1374 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1375 | /* Build the FIS. */ |
| 1376 | memset(&fis, 0, sizeof(struct host_to_dev_fis)); |
| 1377 | fis.type = 0x27; |
| 1378 | fis.opts = 1 << 7; |
| 1379 | fis.command = ATA_CMD_ID_ATA; |
| 1380 | |
| 1381 | /* Set the identify information as invalid. */ |
| 1382 | port->identify_valid = 0; |
| 1383 | |
| 1384 | /* Clear the identify information. */ |
| 1385 | memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS); |
| 1386 | |
| 1387 | /* Execute the command. */ |
| 1388 | if (mtip_exec_internal_command(port, |
| 1389 | &fis, |
| 1390 | 5, |
| 1391 | port->identify_dma, |
| 1392 | sizeof(u16) * ATA_ID_WORDS, |
| 1393 | 0, |
| 1394 | GFP_KERNEL, |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 1395 | MTIP_INT_CMD_TIMEOUT_MS) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1396 | < 0) { |
| 1397 | rv = -1; |
| 1398 | goto out; |
| 1399 | } |
| 1400 | |
| 1401 | /* |
| 1402 | * Perform any necessary byte-swapping. Yes, the kernel does in fact |
| 1403 | * perform field-sensitive swapping on the string fields. |
| 1404 | * See the kernel use of ata_id_string() for proof of this. |
| 1405 | */ |
| 1406 | #ifdef __LITTLE_ENDIAN |
| 1407 | ata_swap_string(port->identify + 27, 40); /* model string*/ |
| 1408 | ata_swap_string(port->identify + 23, 8); /* firmware string*/ |
| 1409 | ata_swap_string(port->identify + 10, 20); /* serial# string*/ |
| 1410 | #else |
| 1411 | { |
| 1412 | int i; |
| 1413 | for (i = 0; i < ATA_ID_WORDS; i++) |
| 1414 | port->identify[i] = le16_to_cpu(port->identify[i]); |
| 1415 | } |
| 1416 | #endif |
| 1417 | |
Sam Bradshaw | 26d5805 | 2013-10-03 10:18:05 -0700 | [diff] [blame] | 1418 | /* Check security locked state */ |
| 1419 | if (port->identify[128] & 0x4) |
| 1420 | set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag); |
| 1421 | else |
| 1422 | clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag); |
| 1423 | |
Asai Thambi S P | 68466cb | 2013-04-12 23:58:02 +0530 | [diff] [blame] | 1424 | #ifdef MTIP_TRIM /* Disabling TRIM support temporarily */ |
Asai Thambi S P | 1528346 | 2013-01-11 14:41:34 +0100 | [diff] [blame] | 1425 | /* Demux ID.DRAT & ID.RZAT to determine trim support */ |
| 1426 | if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5)) |
| 1427 | port->dd->trim_supp = true; |
| 1428 | else |
Asai Thambi S P | 68466cb | 2013-04-12 23:58:02 +0530 | [diff] [blame] | 1429 | #endif |
Asai Thambi S P | 1528346 | 2013-01-11 14:41:34 +0100 | [diff] [blame] | 1430 | port->dd->trim_supp = false; |
| 1431 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1432 | /* Set the identify buffer as valid. */ |
| 1433 | port->identify_valid = 1; |
| 1434 | |
| 1435 | if (user_buffer) { |
| 1436 | if (copy_to_user( |
| 1437 | user_buffer, |
| 1438 | port->identify, |
| 1439 | ATA_ID_WORDS * sizeof(u16))) { |
| 1440 | rv = -EFAULT; |
| 1441 | goto out; |
| 1442 | } |
| 1443 | } |
| 1444 | |
| 1445 | out: |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1446 | return rv; |
| 1447 | } |
| 1448 | |
| 1449 | /* |
| 1450 | * Issue a standby immediate command to the device. |
| 1451 | * |
| 1452 | * @port Pointer to the port structure. |
| 1453 | * |
| 1454 | * return value |
| 1455 | * 0 Command was executed successfully. |
| 1456 | * -1 An error occurred while executing the command. |
| 1457 | */ |
| 1458 | static int mtip_standby_immediate(struct mtip_port *port) |
| 1459 | { |
| 1460 | int rv; |
| 1461 | struct host_to_dev_fis fis; |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1462 | unsigned long start; |
Asai Thambi S P | 670a641 | 2014-04-16 20:27:54 -0700 | [diff] [blame] | 1463 | unsigned int timeout; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1464 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1465 | /* Build the FIS. */ |
| 1466 | memset(&fis, 0, sizeof(struct host_to_dev_fis)); |
| 1467 | fis.type = 0x27; |
| 1468 | fis.opts = 1 << 7; |
| 1469 | fis.command = ATA_CMD_STANDBYNOW1; |
| 1470 | |
Asai Thambi S P | 670a641 | 2014-04-16 20:27:54 -0700 | [diff] [blame] | 1471 | mtip_set_timeout(port->dd, &fis, &timeout, 0); |
| 1472 | |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1473 | start = jiffies; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1474 | rv = mtip_exec_internal_command(port, |
| 1475 | &fis, |
| 1476 | 5, |
| 1477 | 0, |
| 1478 | 0, |
| 1479 | 0, |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1480 | GFP_ATOMIC, |
Asai Thambi S P | 670a641 | 2014-04-16 20:27:54 -0700 | [diff] [blame] | 1481 | timeout); |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1482 | dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n", |
| 1483 | jiffies_to_msecs(jiffies - start)); |
| 1484 | if (rv) |
| 1485 | dev_warn(&port->dd->pdev->dev, |
| 1486 | "STANDBY IMMEDIATE command failed.\n"); |
| 1487 | |
| 1488 | return rv; |
| 1489 | } |
| 1490 | |
| 1491 | /* |
| 1492 | * Issue a READ LOG EXT command to the device. |
| 1493 | * |
| 1494 | * @port pointer to the port structure. |
| 1495 | * @page page number to fetch |
| 1496 | * @buffer pointer to buffer |
| 1497 | * @buffer_dma dma address corresponding to @buffer |
| 1498 | * @sectors page length to fetch, in sectors |
| 1499 | * |
| 1500 | * return value |
| 1501 | * @rv return value from mtip_exec_internal_command() |
| 1502 | */ |
| 1503 | static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer, |
| 1504 | dma_addr_t buffer_dma, unsigned int sectors) |
| 1505 | { |
| 1506 | struct host_to_dev_fis fis; |
| 1507 | |
| 1508 | memset(&fis, 0, sizeof(struct host_to_dev_fis)); |
| 1509 | fis.type = 0x27; |
| 1510 | fis.opts = 1 << 7; |
| 1511 | fis.command = ATA_CMD_READ_LOG_EXT; |
| 1512 | fis.sect_count = sectors & 0xFF; |
| 1513 | fis.sect_cnt_ex = (sectors >> 8) & 0xFF; |
| 1514 | fis.lba_low = page; |
| 1515 | fis.lba_mid = 0; |
| 1516 | fis.device = ATA_DEVICE_OBS; |
| 1517 | |
| 1518 | memset(buffer, 0, sectors * ATA_SECT_SIZE); |
| 1519 | |
| 1520 | return mtip_exec_internal_command(port, |
| 1521 | &fis, |
| 1522 | 5, |
| 1523 | buffer_dma, |
| 1524 | sectors * ATA_SECT_SIZE, |
| 1525 | 0, |
| 1526 | GFP_ATOMIC, |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 1527 | MTIP_INT_CMD_TIMEOUT_MS); |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | /* |
| 1531 | * Issue a SMART READ DATA command to the device. |
| 1532 | * |
| 1533 | * @port pointer to the port structure. |
| 1534 | * @buffer pointer to buffer |
| 1535 | * @buffer_dma dma address corresponding to @buffer |
| 1536 | * |
| 1537 | * return value |
| 1538 | * @rv return value from mtip_exec_internal_command() |
| 1539 | */ |
| 1540 | static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer, |
| 1541 | dma_addr_t buffer_dma) |
| 1542 | { |
| 1543 | struct host_to_dev_fis fis; |
| 1544 | |
| 1545 | memset(&fis, 0, sizeof(struct host_to_dev_fis)); |
| 1546 | fis.type = 0x27; |
| 1547 | fis.opts = 1 << 7; |
| 1548 | fis.command = ATA_CMD_SMART; |
| 1549 | fis.features = 0xD0; |
| 1550 | fis.sect_count = 1; |
| 1551 | fis.lba_mid = 0x4F; |
| 1552 | fis.lba_hi = 0xC2; |
| 1553 | fis.device = ATA_DEVICE_OBS; |
| 1554 | |
| 1555 | return mtip_exec_internal_command(port, |
| 1556 | &fis, |
| 1557 | 5, |
| 1558 | buffer_dma, |
| 1559 | ATA_SECT_SIZE, |
| 1560 | 0, |
| 1561 | GFP_ATOMIC, |
| 1562 | 15000); |
| 1563 | } |
| 1564 | |
| 1565 | /* |
| 1566 | * Get the value of a smart attribute |
| 1567 | * |
| 1568 | * @port pointer to the port structure |
| 1569 | * @id attribute number |
| 1570 | * @attrib pointer to return attrib information corresponding to @id |
| 1571 | * |
| 1572 | * return value |
| 1573 | * -EINVAL NULL buffer passed or unsupported attribute @id. |
| 1574 | * -EPERM Identify data not valid, SMART not supported or not enabled |
| 1575 | */ |
| 1576 | static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id, |
| 1577 | struct smart_attr *attrib) |
| 1578 | { |
| 1579 | int rv, i; |
| 1580 | struct smart_attr *pattr; |
| 1581 | |
| 1582 | if (!attrib) |
| 1583 | return -EINVAL; |
| 1584 | |
| 1585 | if (!port->identify_valid) { |
| 1586 | dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n"); |
| 1587 | return -EPERM; |
| 1588 | } |
| 1589 | if (!(port->identify[82] & 0x1)) { |
| 1590 | dev_warn(&port->dd->pdev->dev, "SMART not supported\n"); |
| 1591 | return -EPERM; |
| 1592 | } |
| 1593 | if (!(port->identify[85] & 0x1)) { |
| 1594 | dev_warn(&port->dd->pdev->dev, "SMART not enabled\n"); |
| 1595 | return -EPERM; |
| 1596 | } |
| 1597 | |
| 1598 | memset(port->smart_buf, 0, ATA_SECT_SIZE); |
| 1599 | rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma); |
| 1600 | if (rv) { |
| 1601 | dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n"); |
| 1602 | return rv; |
| 1603 | } |
| 1604 | |
| 1605 | pattr = (struct smart_attr *)(port->smart_buf + 2); |
| 1606 | for (i = 0; i < 29; i++, pattr++) |
| 1607 | if (pattr->attr_id == id) { |
| 1608 | memcpy(attrib, pattr, sizeof(struct smart_attr)); |
| 1609 | break; |
| 1610 | } |
| 1611 | |
| 1612 | if (i == 29) { |
| 1613 | dev_warn(&port->dd->pdev->dev, |
| 1614 | "Query for invalid SMART attribute ID\n"); |
| 1615 | rv = -EINVAL; |
| 1616 | } |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1617 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1618 | return rv; |
| 1619 | } |
| 1620 | |
| 1621 | /* |
Asai Thambi S P | 1528346 | 2013-01-11 14:41:34 +0100 | [diff] [blame] | 1622 | * Trim unused sectors |
| 1623 | * |
| 1624 | * @dd pointer to driver_data structure |
| 1625 | * @lba starting lba |
| 1626 | * @len # of 512b sectors to trim |
| 1627 | * |
| 1628 | * return value |
| 1629 | * -ENOMEM Out of dma memory |
| 1630 | * -EINVAL Invalid parameters passed in, trim not supported |
| 1631 | * -EIO Error submitting trim request to hw |
| 1632 | */ |
Asai Thambi S P | d0d096b | 2013-04-03 19:53:07 +0530 | [diff] [blame] | 1633 | static int mtip_send_trim(struct driver_data *dd, unsigned int lba, |
| 1634 | unsigned int len) |
Asai Thambi S P | 1528346 | 2013-01-11 14:41:34 +0100 | [diff] [blame] | 1635 | { |
| 1636 | int i, rv = 0; |
| 1637 | u64 tlba, tlen, sect_left; |
| 1638 | struct mtip_trim_entry *buf; |
| 1639 | dma_addr_t dma_addr; |
| 1640 | struct host_to_dev_fis fis; |
| 1641 | |
| 1642 | if (!len || dd->trim_supp == false) |
| 1643 | return -EINVAL; |
| 1644 | |
| 1645 | /* Trim request too big */ |
| 1646 | WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES)); |
| 1647 | |
| 1648 | /* Trim request not aligned on 4k boundary */ |
| 1649 | WARN_ON(len % 8 != 0); |
| 1650 | |
| 1651 | /* Warn if vu_trim structure is too big */ |
| 1652 | WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE); |
| 1653 | |
| 1654 | /* Allocate a DMA buffer for the trim structure */ |
| 1655 | buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr, |
| 1656 | GFP_KERNEL); |
| 1657 | if (!buf) |
| 1658 | return -ENOMEM; |
| 1659 | memset(buf, 0, ATA_SECT_SIZE); |
| 1660 | |
| 1661 | for (i = 0, sect_left = len, tlba = lba; |
| 1662 | i < MTIP_MAX_TRIM_ENTRIES && sect_left; |
| 1663 | i++) { |
| 1664 | tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ? |
| 1665 | MTIP_MAX_TRIM_ENTRY_LEN : |
| 1666 | sect_left); |
| 1667 | buf[i].lba = __force_bit2int cpu_to_le32(tlba); |
| 1668 | buf[i].range = __force_bit2int cpu_to_le16(tlen); |
| 1669 | tlba += tlen; |
| 1670 | sect_left -= tlen; |
| 1671 | } |
| 1672 | WARN_ON(sect_left != 0); |
| 1673 | |
| 1674 | /* Build the fis */ |
| 1675 | memset(&fis, 0, sizeof(struct host_to_dev_fis)); |
| 1676 | fis.type = 0x27; |
| 1677 | fis.opts = 1 << 7; |
| 1678 | fis.command = 0xfb; |
| 1679 | fis.features = 0x60; |
| 1680 | fis.sect_count = 1; |
| 1681 | fis.device = ATA_DEVICE_OBS; |
| 1682 | |
| 1683 | if (mtip_exec_internal_command(dd->port, |
| 1684 | &fis, |
| 1685 | 5, |
| 1686 | dma_addr, |
| 1687 | ATA_SECT_SIZE, |
| 1688 | 0, |
| 1689 | GFP_KERNEL, |
| 1690 | MTIP_TRIM_TIMEOUT_MS) < 0) |
| 1691 | rv = -EIO; |
| 1692 | |
| 1693 | dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr); |
| 1694 | return rv; |
| 1695 | } |
| 1696 | |
| 1697 | /* |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1698 | * Get the drive capacity. |
| 1699 | * |
| 1700 | * @dd Pointer to the device data structure. |
| 1701 | * @sectors Pointer to the variable that will receive the sector count. |
| 1702 | * |
| 1703 | * return value |
| 1704 | * 1 Capacity was returned successfully. |
| 1705 | * 0 The identify information is invalid. |
| 1706 | */ |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 1707 | static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1708 | { |
| 1709 | struct mtip_port *port = dd->port; |
| 1710 | u64 total, raw0, raw1, raw2, raw3; |
| 1711 | raw0 = port->identify[100]; |
| 1712 | raw1 = port->identify[101]; |
| 1713 | raw2 = port->identify[102]; |
| 1714 | raw3 = port->identify[103]; |
| 1715 | total = raw0 | raw1<<16 | raw2<<32 | raw3<<48; |
| 1716 | *sectors = total; |
| 1717 | return (bool) !!port->identify_valid; |
| 1718 | } |
| 1719 | |
| 1720 | /* |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1721 | * Display the identify command data. |
| 1722 | * |
| 1723 | * @port Pointer to the port data structure. |
| 1724 | * |
| 1725 | * return value |
| 1726 | * None |
| 1727 | */ |
| 1728 | static void mtip_dump_identify(struct mtip_port *port) |
| 1729 | { |
| 1730 | sector_t sectors; |
| 1731 | unsigned short revid; |
| 1732 | char cbuf[42]; |
| 1733 | |
| 1734 | if (!port->identify_valid) |
| 1735 | return; |
| 1736 | |
| 1737 | strlcpy(cbuf, (char *)(port->identify+10), 21); |
| 1738 | dev_info(&port->dd->pdev->dev, |
| 1739 | "Serial No.: %s\n", cbuf); |
| 1740 | |
| 1741 | strlcpy(cbuf, (char *)(port->identify+23), 9); |
| 1742 | dev_info(&port->dd->pdev->dev, |
| 1743 | "Firmware Ver.: %s\n", cbuf); |
| 1744 | |
| 1745 | strlcpy(cbuf, (char *)(port->identify+27), 41); |
| 1746 | dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf); |
| 1747 | |
Sam Bradshaw | 26d5805 | 2013-10-03 10:18:05 -0700 | [diff] [blame] | 1748 | dev_info(&port->dd->pdev->dev, "Security: %04x %s\n", |
| 1749 | port->identify[128], |
| 1750 | port->identify[128] & 0x4 ? "(LOCKED)" : ""); |
| 1751 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1752 | if (mtip_hw_get_capacity(port->dd, §ors)) |
| 1753 | dev_info(&port->dd->pdev->dev, |
| 1754 | "Capacity: %llu sectors (%llu MB)\n", |
| 1755 | (u64)sectors, |
| 1756 | ((u64)sectors) * ATA_SECT_SIZE >> 20); |
| 1757 | |
| 1758 | pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid); |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 1759 | switch (revid & 0xFF) { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1760 | case 0x1: |
| 1761 | strlcpy(cbuf, "A0", 3); |
| 1762 | break; |
| 1763 | case 0x3: |
| 1764 | strlcpy(cbuf, "A2", 3); |
| 1765 | break; |
| 1766 | default: |
| 1767 | strlcpy(cbuf, "?", 2); |
| 1768 | break; |
| 1769 | } |
| 1770 | dev_info(&port->dd->pdev->dev, |
| 1771 | "Card Type: %s\n", cbuf); |
| 1772 | } |
| 1773 | |
| 1774 | /* |
| 1775 | * Map the commands scatter list into the command table. |
| 1776 | * |
| 1777 | * @command Pointer to the command. |
| 1778 | * @nents Number of scatter list entries. |
| 1779 | * |
| 1780 | * return value |
| 1781 | * None |
| 1782 | */ |
| 1783 | static inline void fill_command_sg(struct driver_data *dd, |
| 1784 | struct mtip_cmd *command, |
| 1785 | int nents) |
| 1786 | { |
| 1787 | int n; |
| 1788 | unsigned int dma_len; |
| 1789 | struct mtip_cmd_sg *command_sg; |
| 1790 | struct scatterlist *sg = command->sg; |
| 1791 | |
| 1792 | command_sg = command->command + AHCI_CMD_TBL_HDR_SZ; |
| 1793 | |
| 1794 | for (n = 0; n < nents; n++) { |
| 1795 | dma_len = sg_dma_len(sg); |
| 1796 | if (dma_len > 0x400000) |
| 1797 | dev_err(&dd->pdev->dev, |
| 1798 | "DMA segment length truncated\n"); |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 1799 | command_sg->info = __force_bit2int |
| 1800 | cpu_to_le32((dma_len-1) & 0x3FFFFF); |
| 1801 | command_sg->dba = __force_bit2int |
| 1802 | cpu_to_le32(sg_dma_address(sg)); |
| 1803 | command_sg->dba_upper = __force_bit2int |
| 1804 | cpu_to_le32((sg_dma_address(sg) >> 16) >> 16); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1805 | command_sg++; |
| 1806 | sg++; |
| 1807 | } |
| 1808 | } |
| 1809 | |
| 1810 | /* |
| 1811 | * @brief Execute a drive command. |
| 1812 | * |
| 1813 | * return value 0 The command completed successfully. |
| 1814 | * return value -1 An error occurred while executing the command. |
| 1815 | */ |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 1816 | static int exec_drive_task(struct mtip_port *port, u8 *command) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1817 | { |
| 1818 | struct host_to_dev_fis fis; |
| 1819 | struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG); |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 1820 | unsigned int to; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1821 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1822 | /* Build the FIS. */ |
| 1823 | memset(&fis, 0, sizeof(struct host_to_dev_fis)); |
| 1824 | fis.type = 0x27; |
| 1825 | fis.opts = 1 << 7; |
| 1826 | fis.command = command[0]; |
| 1827 | fis.features = command[1]; |
| 1828 | fis.sect_count = command[2]; |
| 1829 | fis.sector = command[3]; |
| 1830 | fis.cyl_low = command[4]; |
| 1831 | fis.cyl_hi = command[5]; |
| 1832 | fis.device = command[6] & ~0x10; /* Clear the dev bit*/ |
| 1833 | |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 1834 | mtip_set_timeout(port->dd, &fis, &to, 0); |
| 1835 | |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 1836 | dbg_printk(MTIP_DRV_NAME " %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n", |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1837 | __func__, |
| 1838 | command[0], |
| 1839 | command[1], |
| 1840 | command[2], |
| 1841 | command[3], |
| 1842 | command[4], |
| 1843 | command[5], |
| 1844 | command[6]); |
| 1845 | |
| 1846 | /* Execute the command. */ |
| 1847 | if (mtip_exec_internal_command(port, |
| 1848 | &fis, |
| 1849 | 5, |
| 1850 | 0, |
| 1851 | 0, |
| 1852 | 0, |
| 1853 | GFP_KERNEL, |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 1854 | to) < 0) { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1855 | return -1; |
| 1856 | } |
| 1857 | |
| 1858 | command[0] = reply->command; /* Status*/ |
| 1859 | command[1] = reply->features; /* Error*/ |
| 1860 | command[4] = reply->cyl_low; |
| 1861 | command[5] = reply->cyl_hi; |
| 1862 | |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 1863 | dbg_printk(MTIP_DRV_NAME " %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n", |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1864 | __func__, |
| 1865 | command[0], |
| 1866 | command[1], |
| 1867 | command[4], |
| 1868 | command[5]); |
| 1869 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1870 | return 0; |
| 1871 | } |
| 1872 | |
| 1873 | /* |
| 1874 | * @brief Execute a drive command. |
| 1875 | * |
| 1876 | * @param port Pointer to the port data structure. |
| 1877 | * @param command Pointer to the user specified command parameters. |
| 1878 | * @param user_buffer Pointer to the user space buffer where read sector |
| 1879 | * data should be copied. |
| 1880 | * |
| 1881 | * return value 0 The command completed successfully. |
| 1882 | * return value -EFAULT An error occurred while copying the completion |
| 1883 | * data to the user space buffer. |
| 1884 | * return value -1 An error occurred while executing the command. |
| 1885 | */ |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 1886 | static int exec_drive_command(struct mtip_port *port, u8 *command, |
| 1887 | void __user *user_buffer) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1888 | { |
| 1889 | struct host_to_dev_fis fis; |
Asai Thambi S P | e602878f | 2012-05-29 18:43:31 -0700 | [diff] [blame] | 1890 | struct host_to_dev_fis *reply; |
| 1891 | u8 *buf = NULL; |
| 1892 | dma_addr_t dma_addr = 0; |
| 1893 | int rv = 0, xfer_sz = command[3]; |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 1894 | unsigned int to; |
Asai Thambi S P | e602878f | 2012-05-29 18:43:31 -0700 | [diff] [blame] | 1895 | |
| 1896 | if (xfer_sz) { |
David Milburn | 97651ea | 2012-09-12 14:06:12 -0500 | [diff] [blame] | 1897 | if (!user_buffer) |
Asai Thambi S P | e602878f | 2012-05-29 18:43:31 -0700 | [diff] [blame] | 1898 | return -EFAULT; |
| 1899 | |
| 1900 | buf = dmam_alloc_coherent(&port->dd->pdev->dev, |
| 1901 | ATA_SECT_SIZE * xfer_sz, |
| 1902 | &dma_addr, |
| 1903 | GFP_KERNEL); |
| 1904 | if (!buf) { |
| 1905 | dev_err(&port->dd->pdev->dev, |
| 1906 | "Memory allocation failed (%d bytes)\n", |
| 1907 | ATA_SECT_SIZE * xfer_sz); |
| 1908 | return -ENOMEM; |
| 1909 | } |
| 1910 | memset(buf, 0, ATA_SECT_SIZE * xfer_sz); |
| 1911 | } |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1912 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1913 | /* Build the FIS. */ |
| 1914 | memset(&fis, 0, sizeof(struct host_to_dev_fis)); |
Asai Thambi S P | e602878f | 2012-05-29 18:43:31 -0700 | [diff] [blame] | 1915 | fis.type = 0x27; |
| 1916 | fis.opts = 1 << 7; |
| 1917 | fis.command = command[0]; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1918 | fis.features = command[2]; |
| 1919 | fis.sect_count = command[3]; |
| 1920 | if (fis.command == ATA_CMD_SMART) { |
| 1921 | fis.sector = command[1]; |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 1922 | fis.cyl_low = 0x4F; |
| 1923 | fis.cyl_hi = 0xC2; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1924 | } |
| 1925 | |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 1926 | mtip_set_timeout(port->dd, &fis, &to, 0); |
| 1927 | |
Asai Thambi S P | e602878f | 2012-05-29 18:43:31 -0700 | [diff] [blame] | 1928 | if (xfer_sz) |
| 1929 | reply = (port->rxfis + RX_FIS_PIO_SETUP); |
| 1930 | else |
| 1931 | reply = (port->rxfis + RX_FIS_D2H_REG); |
| 1932 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1933 | dbg_printk(MTIP_DRV_NAME |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 1934 | " %s: User Command: cmd %x, sect %x, " |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1935 | "feat %x, sectcnt %x\n", |
| 1936 | __func__, |
| 1937 | command[0], |
| 1938 | command[1], |
| 1939 | command[2], |
| 1940 | command[3]); |
| 1941 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1942 | /* Execute the command. */ |
| 1943 | if (mtip_exec_internal_command(port, |
| 1944 | &fis, |
| 1945 | 5, |
Asai Thambi S P | e602878f | 2012-05-29 18:43:31 -0700 | [diff] [blame] | 1946 | (xfer_sz ? dma_addr : 0), |
| 1947 | (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0), |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1948 | 0, |
| 1949 | GFP_KERNEL, |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 1950 | to) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1951 | < 0) { |
Asai Thambi S P | e602878f | 2012-05-29 18:43:31 -0700 | [diff] [blame] | 1952 | rv = -EFAULT; |
| 1953 | goto exit_drive_command; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1954 | } |
| 1955 | |
| 1956 | /* Collect the completion status. */ |
| 1957 | command[0] = reply->command; /* Status*/ |
| 1958 | command[1] = reply->features; /* Error*/ |
Asai Thambi S P | e602878f | 2012-05-29 18:43:31 -0700 | [diff] [blame] | 1959 | command[2] = reply->sect_count; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1960 | |
| 1961 | dbg_printk(MTIP_DRV_NAME |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 1962 | " %s: Completion Status: stat %x, " |
Asai Thambi S P | e602878f | 2012-05-29 18:43:31 -0700 | [diff] [blame] | 1963 | "err %x, nsect %x\n", |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1964 | __func__, |
| 1965 | command[0], |
| 1966 | command[1], |
| 1967 | command[2]); |
| 1968 | |
Asai Thambi S P | e602878f | 2012-05-29 18:43:31 -0700 | [diff] [blame] | 1969 | if (xfer_sz) { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1970 | if (copy_to_user(user_buffer, |
Asai Thambi S P | e602878f | 2012-05-29 18:43:31 -0700 | [diff] [blame] | 1971 | buf, |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1972 | ATA_SECT_SIZE * command[3])) { |
Asai Thambi S P | e602878f | 2012-05-29 18:43:31 -0700 | [diff] [blame] | 1973 | rv = -EFAULT; |
| 1974 | goto exit_drive_command; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1975 | } |
| 1976 | } |
Asai Thambi S P | e602878f | 2012-05-29 18:43:31 -0700 | [diff] [blame] | 1977 | exit_drive_command: |
| 1978 | if (buf) |
| 1979 | dmam_free_coherent(&port->dd->pdev->dev, |
| 1980 | ATA_SECT_SIZE * xfer_sz, buf, dma_addr); |
| 1981 | return rv; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | /* |
| 1985 | * Indicates whether a command has a single sector payload. |
| 1986 | * |
| 1987 | * @command passed to the device to perform the certain event. |
| 1988 | * @features passed to the device to perform the certain event. |
| 1989 | * |
| 1990 | * return value |
| 1991 | * 1 command is one that always has a single sector payload, |
| 1992 | * regardless of the value in the Sector Count field. |
| 1993 | * 0 otherwise |
| 1994 | * |
| 1995 | */ |
| 1996 | static unsigned int implicit_sector(unsigned char command, |
| 1997 | unsigned char features) |
| 1998 | { |
| 1999 | unsigned int rv = 0; |
| 2000 | |
| 2001 | /* list of commands that have an implicit sector count of 1 */ |
| 2002 | switch (command) { |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 2003 | case ATA_CMD_SEC_SET_PASS: |
| 2004 | case ATA_CMD_SEC_UNLOCK: |
| 2005 | case ATA_CMD_SEC_ERASE_PREP: |
| 2006 | case ATA_CMD_SEC_ERASE_UNIT: |
| 2007 | case ATA_CMD_SEC_FREEZE_LOCK: |
| 2008 | case ATA_CMD_SEC_DISABLE_PASS: |
| 2009 | case ATA_CMD_PMP_READ: |
| 2010 | case ATA_CMD_PMP_WRITE: |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2011 | rv = 1; |
| 2012 | break; |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 2013 | case ATA_CMD_SET_MAX: |
| 2014 | if (features == ATA_SET_MAX_UNLOCK) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2015 | rv = 1; |
| 2016 | break; |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 2017 | case ATA_CMD_SMART: |
| 2018 | if ((features == ATA_SMART_READ_VALUES) || |
| 2019 | (features == ATA_SMART_READ_THRESHOLDS)) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2020 | rv = 1; |
| 2021 | break; |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 2022 | case ATA_CMD_CONF_OVERLAY: |
| 2023 | if ((features == ATA_DCO_IDENTIFY) || |
| 2024 | (features == ATA_DCO_SET)) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2025 | rv = 1; |
| 2026 | break; |
| 2027 | } |
| 2028 | return rv; |
| 2029 | } |
Asai Thambi S P | 2df7aa9 | 2012-05-29 18:41:23 -0700 | [diff] [blame] | 2030 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2031 | /* |
| 2032 | * Executes a taskfile |
| 2033 | * See ide_taskfile_ioctl() for derivation |
| 2034 | */ |
| 2035 | static int exec_drive_taskfile(struct driver_data *dd, |
Jens Axboe | ef0f158 | 2011-09-27 21:19:53 -0600 | [diff] [blame] | 2036 | void __user *buf, |
| 2037 | ide_task_request_t *req_task, |
| 2038 | int outtotal) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2039 | { |
| 2040 | struct host_to_dev_fis fis; |
| 2041 | struct host_to_dev_fis *reply; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2042 | u8 *outbuf = NULL; |
| 2043 | u8 *inbuf = NULL; |
Jens Axboe | 16d02c0 | 2011-09-27 15:50:01 -0600 | [diff] [blame] | 2044 | dma_addr_t outbuf_dma = 0; |
| 2045 | dma_addr_t inbuf_dma = 0; |
| 2046 | dma_addr_t dma_buffer = 0; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2047 | int err = 0; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2048 | unsigned int taskin = 0; |
| 2049 | unsigned int taskout = 0; |
| 2050 | u8 nsect = 0; |
Asai Thambi S P | 2df7aa9 | 2012-05-29 18:41:23 -0700 | [diff] [blame] | 2051 | unsigned int timeout; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2052 | unsigned int force_single_sector; |
| 2053 | unsigned int transfer_size; |
| 2054 | unsigned long task_file_data; |
Jens Axboe | ef0f158 | 2011-09-27 21:19:53 -0600 | [diff] [blame] | 2055 | int intotal = outtotal + req_task->out_size; |
Selvan Mani | 4453bc8 | 2012-09-27 14:36:43 +0200 | [diff] [blame] | 2056 | int erasemode = 0; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2057 | |
| 2058 | taskout = req_task->out_size; |
| 2059 | taskin = req_task->in_size; |
| 2060 | /* 130560 = 512 * 0xFF*/ |
Sachin Shukla | b425b02 | 2016-11-11 14:34:51 +0530 | [diff] [blame] | 2061 | if (taskin > 130560 || taskout > 130560) |
| 2062 | return -EINVAL; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2063 | |
| 2064 | if (taskout) { |
Al Viro | 8ed6010 | 2016-01-02 14:56:33 -0500 | [diff] [blame] | 2065 | outbuf = memdup_user(buf + outtotal, taskout); |
Sachin Shukla | b425b02 | 2016-11-11 14:34:51 +0530 | [diff] [blame] | 2066 | if (IS_ERR(outbuf)) |
| 2067 | return PTR_ERR(outbuf); |
| 2068 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2069 | outbuf_dma = pci_map_single(dd->pdev, |
| 2070 | outbuf, |
| 2071 | taskout, |
| 2072 | DMA_TO_DEVICE); |
Alexey Khoroshilov | 5173cb8 | 2016-03-19 01:35:54 +0300 | [diff] [blame] | 2073 | if (pci_dma_mapping_error(dd->pdev, outbuf_dma)) { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2074 | err = -ENOMEM; |
| 2075 | goto abort; |
| 2076 | } |
| 2077 | dma_buffer = outbuf_dma; |
| 2078 | } |
| 2079 | |
| 2080 | if (taskin) { |
Al Viro | 8ed6010 | 2016-01-02 14:56:33 -0500 | [diff] [blame] | 2081 | inbuf = memdup_user(buf + intotal, taskin); |
| 2082 | if (IS_ERR(inbuf)) { |
| 2083 | err = PTR_ERR(inbuf); |
| 2084 | inbuf = NULL; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2085 | goto abort; |
| 2086 | } |
| 2087 | inbuf_dma = pci_map_single(dd->pdev, |
| 2088 | inbuf, |
| 2089 | taskin, DMA_FROM_DEVICE); |
Alexey Khoroshilov | 5173cb8 | 2016-03-19 01:35:54 +0300 | [diff] [blame] | 2090 | if (pci_dma_mapping_error(dd->pdev, inbuf_dma)) { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2091 | err = -ENOMEM; |
| 2092 | goto abort; |
| 2093 | } |
| 2094 | dma_buffer = inbuf_dma; |
| 2095 | } |
| 2096 | |
| 2097 | /* only supports PIO and non-data commands from this ioctl. */ |
| 2098 | switch (req_task->data_phase) { |
| 2099 | case TASKFILE_OUT: |
| 2100 | nsect = taskout / ATA_SECT_SIZE; |
| 2101 | reply = (dd->port->rxfis + RX_FIS_PIO_SETUP); |
| 2102 | break; |
| 2103 | case TASKFILE_IN: |
| 2104 | reply = (dd->port->rxfis + RX_FIS_PIO_SETUP); |
| 2105 | break; |
| 2106 | case TASKFILE_NO_DATA: |
| 2107 | reply = (dd->port->rxfis + RX_FIS_D2H_REG); |
| 2108 | break; |
| 2109 | default: |
| 2110 | err = -EINVAL; |
| 2111 | goto abort; |
| 2112 | } |
| 2113 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2114 | /* Build the FIS. */ |
| 2115 | memset(&fis, 0, sizeof(struct host_to_dev_fis)); |
| 2116 | |
| 2117 | fis.type = 0x27; |
| 2118 | fis.opts = 1 << 7; |
| 2119 | fis.command = req_task->io_ports[7]; |
| 2120 | fis.features = req_task->io_ports[1]; |
| 2121 | fis.sect_count = req_task->io_ports[2]; |
| 2122 | fis.lba_low = req_task->io_ports[3]; |
| 2123 | fis.lba_mid = req_task->io_ports[4]; |
| 2124 | fis.lba_hi = req_task->io_ports[5]; |
| 2125 | /* Clear the dev bit*/ |
| 2126 | fis.device = req_task->io_ports[6] & ~0x10; |
| 2127 | |
| 2128 | if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) { |
| 2129 | req_task->in_flags.all = |
| 2130 | IDE_TASKFILE_STD_IN_FLAGS | |
| 2131 | (IDE_HOB_STD_IN_FLAGS << 8); |
| 2132 | fis.lba_low_ex = req_task->hob_ports[3]; |
| 2133 | fis.lba_mid_ex = req_task->hob_ports[4]; |
| 2134 | fis.lba_hi_ex = req_task->hob_ports[5]; |
| 2135 | fis.features_ex = req_task->hob_ports[1]; |
| 2136 | fis.sect_cnt_ex = req_task->hob_ports[2]; |
| 2137 | |
| 2138 | } else { |
| 2139 | req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS; |
| 2140 | } |
| 2141 | |
| 2142 | force_single_sector = implicit_sector(fis.command, fis.features); |
| 2143 | |
| 2144 | if ((taskin || taskout) && (!fis.sect_count)) { |
| 2145 | if (nsect) |
| 2146 | fis.sect_count = nsect; |
| 2147 | else { |
| 2148 | if (!force_single_sector) { |
| 2149 | dev_warn(&dd->pdev->dev, |
| 2150 | "data movement but " |
| 2151 | "sect_count is 0\n"); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2152 | err = -EINVAL; |
| 2153 | goto abort; |
| 2154 | } |
| 2155 | } |
| 2156 | } |
| 2157 | |
| 2158 | dbg_printk(MTIP_DRV_NAME |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 2159 | " %s: cmd %x, feat %x, nsect %x," |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2160 | " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x," |
| 2161 | " head/dev %x\n", |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 2162 | __func__, |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2163 | fis.command, |
| 2164 | fis.features, |
| 2165 | fis.sect_count, |
| 2166 | fis.lba_low, |
| 2167 | fis.lba_mid, |
| 2168 | fis.lba_hi, |
| 2169 | fis.device); |
| 2170 | |
Selvan Mani | 4453bc8 | 2012-09-27 14:36:43 +0200 | [diff] [blame] | 2171 | /* check for erase mode support during secure erase.*/ |
Selvan Mani | 3208795 | 2012-11-07 06:03:37 -0700 | [diff] [blame] | 2172 | if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf && |
| 2173 | (outbuf[0] & MTIP_SEC_ERASE_MODE)) { |
Selvan Mani | 4453bc8 | 2012-09-27 14:36:43 +0200 | [diff] [blame] | 2174 | erasemode = 1; |
| 2175 | } |
| 2176 | |
| 2177 | mtip_set_timeout(dd, &fis, &timeout, erasemode); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2178 | |
| 2179 | /* Determine the correct transfer size.*/ |
| 2180 | if (force_single_sector) |
| 2181 | transfer_size = ATA_SECT_SIZE; |
| 2182 | else |
| 2183 | transfer_size = ATA_SECT_SIZE * fis.sect_count; |
| 2184 | |
| 2185 | /* Execute the command.*/ |
| 2186 | if (mtip_exec_internal_command(dd->port, |
| 2187 | &fis, |
| 2188 | 5, |
| 2189 | dma_buffer, |
| 2190 | transfer_size, |
| 2191 | 0, |
| 2192 | GFP_KERNEL, |
| 2193 | timeout) < 0) { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2194 | err = -EIO; |
| 2195 | goto abort; |
| 2196 | } |
| 2197 | |
| 2198 | task_file_data = readl(dd->port->mmio+PORT_TFDATA); |
| 2199 | |
| 2200 | if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) { |
| 2201 | reply = dd->port->rxfis + RX_FIS_PIO_SETUP; |
| 2202 | req_task->io_ports[7] = reply->control; |
| 2203 | } else { |
| 2204 | reply = dd->port->rxfis + RX_FIS_D2H_REG; |
| 2205 | req_task->io_ports[7] = reply->command; |
| 2206 | } |
| 2207 | |
| 2208 | /* reclaim the DMA buffers.*/ |
| 2209 | if (inbuf_dma) |
| 2210 | pci_unmap_single(dd->pdev, inbuf_dma, |
| 2211 | taskin, DMA_FROM_DEVICE); |
| 2212 | if (outbuf_dma) |
| 2213 | pci_unmap_single(dd->pdev, outbuf_dma, |
| 2214 | taskout, DMA_TO_DEVICE); |
Jens Axboe | 16d02c0 | 2011-09-27 15:50:01 -0600 | [diff] [blame] | 2215 | inbuf_dma = 0; |
| 2216 | outbuf_dma = 0; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2217 | |
| 2218 | /* return the ATA registers to the caller.*/ |
| 2219 | req_task->io_ports[1] = reply->features; |
| 2220 | req_task->io_ports[2] = reply->sect_count; |
| 2221 | req_task->io_ports[3] = reply->lba_low; |
| 2222 | req_task->io_ports[4] = reply->lba_mid; |
| 2223 | req_task->io_ports[5] = reply->lba_hi; |
| 2224 | req_task->io_ports[6] = reply->device; |
| 2225 | |
| 2226 | if (req_task->out_flags.all & 1) { |
| 2227 | |
| 2228 | req_task->hob_ports[3] = reply->lba_low_ex; |
| 2229 | req_task->hob_ports[4] = reply->lba_mid_ex; |
| 2230 | req_task->hob_ports[5] = reply->lba_hi_ex; |
| 2231 | req_task->hob_ports[1] = reply->features_ex; |
| 2232 | req_task->hob_ports[2] = reply->sect_cnt_ex; |
| 2233 | } |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2234 | dbg_printk(MTIP_DRV_NAME |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 2235 | " %s: Completion: stat %x," |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2236 | "err %x, sect_cnt %x, lbalo %x," |
| 2237 | "lbamid %x, lbahi %x, dev %x\n", |
| 2238 | __func__, |
| 2239 | req_task->io_ports[7], |
| 2240 | req_task->io_ports[1], |
| 2241 | req_task->io_ports[2], |
| 2242 | req_task->io_ports[3], |
| 2243 | req_task->io_ports[4], |
| 2244 | req_task->io_ports[5], |
| 2245 | req_task->io_ports[6]); |
| 2246 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2247 | if (taskout) { |
| 2248 | if (copy_to_user(buf + outtotal, outbuf, taskout)) { |
| 2249 | err = -EFAULT; |
| 2250 | goto abort; |
| 2251 | } |
| 2252 | } |
| 2253 | if (taskin) { |
| 2254 | if (copy_to_user(buf + intotal, inbuf, taskin)) { |
| 2255 | err = -EFAULT; |
| 2256 | goto abort; |
| 2257 | } |
| 2258 | } |
| 2259 | abort: |
| 2260 | if (inbuf_dma) |
| 2261 | pci_unmap_single(dd->pdev, inbuf_dma, |
| 2262 | taskin, DMA_FROM_DEVICE); |
| 2263 | if (outbuf_dma) |
| 2264 | pci_unmap_single(dd->pdev, outbuf_dma, |
| 2265 | taskout, DMA_TO_DEVICE); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2266 | kfree(outbuf); |
| 2267 | kfree(inbuf); |
| 2268 | |
| 2269 | return err; |
| 2270 | } |
| 2271 | |
| 2272 | /* |
| 2273 | * Handle IOCTL calls from the Block Layer. |
| 2274 | * |
| 2275 | * This function is called by the Block Layer when it receives an IOCTL |
| 2276 | * command that it does not understand. If the IOCTL command is not supported |
| 2277 | * this function returns -ENOTTY. |
| 2278 | * |
| 2279 | * @dd Pointer to the driver data structure. |
| 2280 | * @cmd IOCTL command passed from the Block Layer. |
| 2281 | * @arg IOCTL argument passed from the Block Layer. |
| 2282 | * |
| 2283 | * return value |
| 2284 | * 0 The IOCTL completed successfully. |
| 2285 | * -ENOTTY The specified command is not supported. |
| 2286 | * -EFAULT An error occurred copying data to a user space buffer. |
| 2287 | * -EIO An error occurred while executing the command. |
| 2288 | */ |
Jens Axboe | ef0f158 | 2011-09-27 21:19:53 -0600 | [diff] [blame] | 2289 | static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd, |
| 2290 | unsigned long arg) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2291 | { |
| 2292 | switch (cmd) { |
| 2293 | case HDIO_GET_IDENTITY: |
Asai Thambi S P | 971890f | 2012-05-29 18:41:47 -0700 | [diff] [blame] | 2294 | { |
| 2295 | if (copy_to_user((void __user *)arg, dd->port->identify, |
| 2296 | sizeof(u16) * ATA_ID_WORDS)) |
| 2297 | return -EFAULT; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2298 | break; |
Asai Thambi S P | 971890f | 2012-05-29 18:41:47 -0700 | [diff] [blame] | 2299 | } |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2300 | case HDIO_DRIVE_CMD: |
| 2301 | { |
| 2302 | u8 drive_command[4]; |
| 2303 | |
| 2304 | /* Copy the user command info to our buffer. */ |
| 2305 | if (copy_from_user(drive_command, |
| 2306 | (void __user *) arg, |
| 2307 | sizeof(drive_command))) |
| 2308 | return -EFAULT; |
| 2309 | |
| 2310 | /* Execute the drive command. */ |
| 2311 | if (exec_drive_command(dd->port, |
| 2312 | drive_command, |
| 2313 | (void __user *) (arg+4))) |
| 2314 | return -EIO; |
| 2315 | |
| 2316 | /* Copy the status back to the users buffer. */ |
| 2317 | if (copy_to_user((void __user *) arg, |
| 2318 | drive_command, |
| 2319 | sizeof(drive_command))) |
| 2320 | return -EFAULT; |
| 2321 | |
| 2322 | break; |
| 2323 | } |
| 2324 | case HDIO_DRIVE_TASK: |
| 2325 | { |
| 2326 | u8 drive_command[7]; |
| 2327 | |
| 2328 | /* Copy the user command info to our buffer. */ |
| 2329 | if (copy_from_user(drive_command, |
| 2330 | (void __user *) arg, |
| 2331 | sizeof(drive_command))) |
| 2332 | return -EFAULT; |
| 2333 | |
| 2334 | /* Execute the drive command. */ |
| 2335 | if (exec_drive_task(dd->port, drive_command)) |
| 2336 | return -EIO; |
| 2337 | |
| 2338 | /* Copy the status back to the users buffer. */ |
| 2339 | if (copy_to_user((void __user *) arg, |
| 2340 | drive_command, |
| 2341 | sizeof(drive_command))) |
| 2342 | return -EFAULT; |
| 2343 | |
| 2344 | break; |
| 2345 | } |
Jens Axboe | ef0f158 | 2011-09-27 21:19:53 -0600 | [diff] [blame] | 2346 | case HDIO_DRIVE_TASKFILE: { |
| 2347 | ide_task_request_t req_task; |
| 2348 | int ret, outtotal; |
| 2349 | |
| 2350 | if (copy_from_user(&req_task, (void __user *) arg, |
| 2351 | sizeof(req_task))) |
| 2352 | return -EFAULT; |
| 2353 | |
| 2354 | outtotal = sizeof(req_task); |
| 2355 | |
| 2356 | ret = exec_drive_taskfile(dd, (void __user *) arg, |
| 2357 | &req_task, outtotal); |
| 2358 | |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 2359 | if (copy_to_user((void __user *) arg, &req_task, |
| 2360 | sizeof(req_task))) |
Jens Axboe | ef0f158 | 2011-09-27 21:19:53 -0600 | [diff] [blame] | 2361 | return -EFAULT; |
| 2362 | |
| 2363 | return ret; |
| 2364 | } |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2365 | |
| 2366 | default: |
| 2367 | return -EINVAL; |
| 2368 | } |
| 2369 | return 0; |
| 2370 | } |
| 2371 | |
| 2372 | /* |
| 2373 | * Submit an IO to the hw |
| 2374 | * |
| 2375 | * This function is called by the block layer to issue an io |
| 2376 | * to the device. Upon completion, the callback function will |
| 2377 | * be called with the data parameter passed as the callback data. |
| 2378 | * |
| 2379 | * @dd Pointer to the driver data structure. |
| 2380 | * @start First sector to read. |
| 2381 | * @nsect Number of sectors to read. |
| 2382 | * @nents Number of entries in scatter list for the read command. |
| 2383 | * @tag The tag of this read command. |
| 2384 | * @callback Pointer to the function that should be called |
| 2385 | * when the read completes. |
| 2386 | * @data Callback data passed to the callback function |
| 2387 | * when the read completes. |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2388 | * @dir Direction (read or write) |
| 2389 | * |
| 2390 | * return value |
| 2391 | * None |
| 2392 | */ |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 2393 | static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq, |
| 2394 | struct mtip_cmd *command, int nents, |
| 2395 | struct blk_mq_hw_ctx *hctx) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2396 | { |
| 2397 | struct host_to_dev_fis *fis; |
| 2398 | struct mtip_port *port = dd->port; |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 2399 | int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE; |
| 2400 | u64 start = blk_rq_pos(rq); |
| 2401 | unsigned int nsect = blk_rq_sectors(rq); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2402 | |
| 2403 | /* Map the scatter list for DMA access */ |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 2404 | nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2405 | |
Sam Bradshaw | f45c40a | 2014-06-06 13:28:48 -0600 | [diff] [blame] | 2406 | prefetch(&port->flags); |
| 2407 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2408 | command->scatter_ents = nents; |
| 2409 | |
| 2410 | /* |
| 2411 | * The number of retries for this command before it is |
| 2412 | * reported as a failure to the upper layers. |
| 2413 | */ |
| 2414 | command->retries = MTIP_MAX_RETRIES; |
| 2415 | |
| 2416 | /* Fill out fis */ |
| 2417 | fis = command->command; |
| 2418 | fis->type = 0x27; |
| 2419 | fis->opts = 1 << 7; |
Sam Bradshaw | f45c40a | 2014-06-06 13:28:48 -0600 | [diff] [blame] | 2420 | if (dma_dir == DMA_FROM_DEVICE) |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 2421 | fis->command = ATA_CMD_FPDMA_READ; |
| 2422 | else |
| 2423 | fis->command = ATA_CMD_FPDMA_WRITE; |
Selvan Mani | eda4531 | 2012-11-07 06:03:53 -0700 | [diff] [blame] | 2424 | fis->lba_low = start & 0xFF; |
| 2425 | fis->lba_mid = (start >> 8) & 0xFF; |
| 2426 | fis->lba_hi = (start >> 16) & 0xFF; |
| 2427 | fis->lba_low_ex = (start >> 24) & 0xFF; |
| 2428 | fis->lba_mid_ex = (start >> 32) & 0xFF; |
| 2429 | fis->lba_hi_ex = (start >> 40) & 0xFF; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2430 | fis->device = 1 << 6; |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 2431 | fis->features = nsect & 0xFF; |
| 2432 | fis->features_ex = (nsect >> 8) & 0xFF; |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 2433 | fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5)); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2434 | fis->sect_cnt_ex = 0; |
| 2435 | fis->control = 0; |
| 2436 | fis->res2 = 0; |
| 2437 | fis->res3 = 0; |
| 2438 | fill_command_sg(dd, command, nents); |
| 2439 | |
Sam Bradshaw | f45c40a | 2014-06-06 13:28:48 -0600 | [diff] [blame] | 2440 | if (unlikely(command->unaligned)) |
Asai Thambi S P | 2077d94 | 2013-04-29 21:19:49 +0200 | [diff] [blame] | 2441 | fis->device |= 1 << 7; |
| 2442 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2443 | /* Populate the command header */ |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 2444 | command->command_header->opts = |
| 2445 | __force_bit2int cpu_to_le32( |
| 2446 | (nents << 16) | 5 | AHCI_CMD_PREFETCH); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2447 | command->command_header->byte_count = 0; |
| 2448 | |
| 2449 | /* |
| 2450 | * Set the completion function and data for the command |
| 2451 | * within this layer. |
| 2452 | */ |
| 2453 | command->comp_data = dd; |
| 2454 | command->comp_func = mtip_async_complete; |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 2455 | command->direction = dma_dir; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2456 | |
| 2457 | /* |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 2458 | * To prevent this command from being issued |
| 2459 | * if an internal command is in progress or error handling is active. |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2460 | */ |
Sam Bradshaw | f45c40a | 2014-06-06 13:28:48 -0600 | [diff] [blame] | 2461 | if (unlikely(port->flags & MTIP_PF_PAUSE_IO)) { |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 2462 | set_bit(rq->tag, port->cmds_to_issue); |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 2463 | set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags); |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 2464 | return; |
| 2465 | } |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2466 | |
| 2467 | /* Issue the command to the hardware */ |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 2468 | mtip_issue_ncq_command(port, rq->tag); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2469 | } |
| 2470 | |
| 2471 | /* |
Asai Thambi S P | 7412ff1 | 2012-06-04 12:43:03 -0700 | [diff] [blame] | 2472 | * Sysfs status dump. |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2473 | * |
| 2474 | * @dev Pointer to the device structure, passed by the kernrel. |
| 2475 | * @attr Pointer to the device_attribute structure passed by the kernel. |
| 2476 | * @buf Pointer to the char buffer that will receive the stats info. |
| 2477 | * |
| 2478 | * return value |
| 2479 | * The size, in bytes, of the data copied into buf. |
| 2480 | */ |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 2481 | static ssize_t mtip_hw_show_status(struct device *dev, |
| 2482 | struct device_attribute *attr, |
| 2483 | char *buf) |
| 2484 | { |
| 2485 | struct driver_data *dd = dev_to_disk(dev)->private_data; |
| 2486 | int size = 0; |
| 2487 | |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 2488 | if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag)) |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 2489 | size += sprintf(buf, "%s", "thermal_shutdown\n"); |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 2490 | else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag)) |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 2491 | size += sprintf(buf, "%s", "write_protect\n"); |
| 2492 | else |
| 2493 | size += sprintf(buf, "%s", "online\n"); |
| 2494 | |
| 2495 | return size; |
| 2496 | } |
| 2497 | |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 2498 | static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2499 | |
Asai Thambi S P | 0caff00 | 2013-04-03 19:56:21 +0530 | [diff] [blame] | 2500 | /* debugsfs entries */ |
| 2501 | |
| 2502 | static ssize_t show_device_status(struct device_driver *drv, char *buf) |
| 2503 | { |
| 2504 | int size = 0; |
| 2505 | struct driver_data *dd, *tmp; |
| 2506 | unsigned long flags; |
| 2507 | char id_buf[42]; |
| 2508 | u16 status = 0; |
| 2509 | |
| 2510 | spin_lock_irqsave(&dev_lock, flags); |
| 2511 | size += sprintf(&buf[size], "Devices Present:\n"); |
| 2512 | list_for_each_entry_safe(dd, tmp, &online_list, online_list) { |
Jens Axboe | c66bb3f | 2013-04-04 09:03:41 +0200 | [diff] [blame] | 2513 | if (dd->pdev) { |
Asai Thambi S P | 0caff00 | 2013-04-03 19:56:21 +0530 | [diff] [blame] | 2514 | if (dd->port && |
| 2515 | dd->port->identify && |
| 2516 | dd->port->identify_valid) { |
| 2517 | strlcpy(id_buf, |
| 2518 | (char *) (dd->port->identify + 10), 21); |
| 2519 | status = *(dd->port->identify + 141); |
| 2520 | } else { |
| 2521 | memset(id_buf, 0, 42); |
| 2522 | status = 0; |
| 2523 | } |
| 2524 | |
| 2525 | if (dd->port && |
| 2526 | test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) { |
| 2527 | size += sprintf(&buf[size], |
| 2528 | " device %s %s (ftl rebuild %d %%)\n", |
| 2529 | dev_name(&dd->pdev->dev), |
| 2530 | id_buf, |
| 2531 | status); |
| 2532 | } else { |
| 2533 | size += sprintf(&buf[size], |
| 2534 | " device %s %s\n", |
| 2535 | dev_name(&dd->pdev->dev), |
| 2536 | id_buf); |
| 2537 | } |
| 2538 | } |
| 2539 | } |
| 2540 | |
| 2541 | size += sprintf(&buf[size], "Devices Being Removed:\n"); |
| 2542 | list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) { |
Jens Axboe | c66bb3f | 2013-04-04 09:03:41 +0200 | [diff] [blame] | 2543 | if (dd->pdev) { |
Asai Thambi S P | 0caff00 | 2013-04-03 19:56:21 +0530 | [diff] [blame] | 2544 | if (dd->port && |
| 2545 | dd->port->identify && |
| 2546 | dd->port->identify_valid) { |
| 2547 | strlcpy(id_buf, |
| 2548 | (char *) (dd->port->identify+10), 21); |
| 2549 | status = *(dd->port->identify + 141); |
| 2550 | } else { |
| 2551 | memset(id_buf, 0, 42); |
| 2552 | status = 0; |
| 2553 | } |
| 2554 | |
| 2555 | if (dd->port && |
| 2556 | test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) { |
| 2557 | size += sprintf(&buf[size], |
| 2558 | " device %s %s (ftl rebuild %d %%)\n", |
| 2559 | dev_name(&dd->pdev->dev), |
| 2560 | id_buf, |
| 2561 | status); |
| 2562 | } else { |
| 2563 | size += sprintf(&buf[size], |
| 2564 | " device %s %s\n", |
| 2565 | dev_name(&dd->pdev->dev), |
| 2566 | id_buf); |
| 2567 | } |
| 2568 | } |
| 2569 | } |
| 2570 | spin_unlock_irqrestore(&dev_lock, flags); |
| 2571 | |
| 2572 | return size; |
| 2573 | } |
| 2574 | |
| 2575 | static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf, |
| 2576 | size_t len, loff_t *offset) |
| 2577 | { |
David Milburn | c8afd0d | 2013-05-23 16:23:45 -0500 | [diff] [blame] | 2578 | struct driver_data *dd = (struct driver_data *)f->private_data; |
Asai Thambi S P | 0caff00 | 2013-04-03 19:56:21 +0530 | [diff] [blame] | 2579 | int size = *offset; |
David Milburn | c8afd0d | 2013-05-23 16:23:45 -0500 | [diff] [blame] | 2580 | char *buf; |
| 2581 | int rv = 0; |
Asai Thambi S P | 0caff00 | 2013-04-03 19:56:21 +0530 | [diff] [blame] | 2582 | |
| 2583 | if (!len || *offset) |
| 2584 | return 0; |
| 2585 | |
David Milburn | c8afd0d | 2013-05-23 16:23:45 -0500 | [diff] [blame] | 2586 | buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL); |
| 2587 | if (!buf) { |
| 2588 | dev_err(&dd->pdev->dev, |
| 2589 | "Memory allocation: status buffer\n"); |
| 2590 | return -ENOMEM; |
| 2591 | } |
| 2592 | |
Asai Thambi S P | 0caff00 | 2013-04-03 19:56:21 +0530 | [diff] [blame] | 2593 | size += show_device_status(NULL, buf); |
| 2594 | |
| 2595 | *offset = size <= len ? size : len; |
| 2596 | size = copy_to_user(ubuf, buf, *offset); |
| 2597 | if (size) |
David Milburn | c8afd0d | 2013-05-23 16:23:45 -0500 | [diff] [blame] | 2598 | rv = -EFAULT; |
Asai Thambi S P | 0caff00 | 2013-04-03 19:56:21 +0530 | [diff] [blame] | 2599 | |
David Milburn | c8afd0d | 2013-05-23 16:23:45 -0500 | [diff] [blame] | 2600 | kfree(buf); |
| 2601 | return rv ? rv : *offset; |
Asai Thambi S P | 0caff00 | 2013-04-03 19:56:21 +0530 | [diff] [blame] | 2602 | } |
| 2603 | |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 2604 | static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf, |
| 2605 | size_t len, loff_t *offset) |
| 2606 | { |
| 2607 | struct driver_data *dd = (struct driver_data *)f->private_data; |
David Milburn | c8afd0d | 2013-05-23 16:23:45 -0500 | [diff] [blame] | 2608 | char *buf; |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 2609 | u32 group_allocated; |
| 2610 | int size = *offset; |
David Milburn | c8afd0d | 2013-05-23 16:23:45 -0500 | [diff] [blame] | 2611 | int n, rv = 0; |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 2612 | |
| 2613 | if (!len || size) |
| 2614 | return 0; |
| 2615 | |
David Milburn | c8afd0d | 2013-05-23 16:23:45 -0500 | [diff] [blame] | 2616 | buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL); |
| 2617 | if (!buf) { |
| 2618 | dev_err(&dd->pdev->dev, |
| 2619 | "Memory allocation: register buffer\n"); |
| 2620 | return -ENOMEM; |
| 2621 | } |
| 2622 | |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 2623 | size += sprintf(&buf[size], "H/ S ACTive : [ 0x"); |
| 2624 | |
| 2625 | for (n = dd->slot_groups-1; n >= 0; n--) |
| 2626 | size += sprintf(&buf[size], "%08X ", |
| 2627 | readl(dd->port->s_active[n])); |
| 2628 | |
| 2629 | size += sprintf(&buf[size], "]\n"); |
| 2630 | size += sprintf(&buf[size], "H/ Command Issue : [ 0x"); |
| 2631 | |
| 2632 | for (n = dd->slot_groups-1; n >= 0; n--) |
| 2633 | size += sprintf(&buf[size], "%08X ", |
| 2634 | readl(dd->port->cmd_issue[n])); |
| 2635 | |
| 2636 | size += sprintf(&buf[size], "]\n"); |
| 2637 | size += sprintf(&buf[size], "H/ Completed : [ 0x"); |
| 2638 | |
| 2639 | for (n = dd->slot_groups-1; n >= 0; n--) |
| 2640 | size += sprintf(&buf[size], "%08X ", |
| 2641 | readl(dd->port->completed[n])); |
| 2642 | |
| 2643 | size += sprintf(&buf[size], "]\n"); |
| 2644 | size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n", |
| 2645 | readl(dd->port->mmio + PORT_IRQ_STAT)); |
| 2646 | size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n", |
| 2647 | readl(dd->mmio + HOST_IRQ_STAT)); |
| 2648 | size += sprintf(&buf[size], "\n"); |
| 2649 | |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 2650 | size += sprintf(&buf[size], "L/ Commands in Q : [ 0x"); |
| 2651 | |
| 2652 | for (n = dd->slot_groups-1; n >= 0; n--) { |
| 2653 | if (sizeof(long) > sizeof(u32)) |
| 2654 | group_allocated = |
| 2655 | dd->port->cmds_to_issue[n/2] >> (32*(n&1)); |
| 2656 | else |
| 2657 | group_allocated = dd->port->cmds_to_issue[n]; |
| 2658 | size += sprintf(&buf[size], "%08X ", group_allocated); |
| 2659 | } |
| 2660 | size += sprintf(&buf[size], "]\n"); |
| 2661 | |
| 2662 | *offset = size <= len ? size : len; |
| 2663 | size = copy_to_user(ubuf, buf, *offset); |
| 2664 | if (size) |
David Milburn | c8afd0d | 2013-05-23 16:23:45 -0500 | [diff] [blame] | 2665 | rv = -EFAULT; |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 2666 | |
David Milburn | c8afd0d | 2013-05-23 16:23:45 -0500 | [diff] [blame] | 2667 | kfree(buf); |
| 2668 | return rv ? rv : *offset; |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 2669 | } |
| 2670 | |
| 2671 | static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf, |
| 2672 | size_t len, loff_t *offset) |
| 2673 | { |
| 2674 | struct driver_data *dd = (struct driver_data *)f->private_data; |
David Milburn | c8afd0d | 2013-05-23 16:23:45 -0500 | [diff] [blame] | 2675 | char *buf; |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 2676 | int size = *offset; |
David Milburn | c8afd0d | 2013-05-23 16:23:45 -0500 | [diff] [blame] | 2677 | int rv = 0; |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 2678 | |
| 2679 | if (!len || size) |
| 2680 | return 0; |
| 2681 | |
David Milburn | c8afd0d | 2013-05-23 16:23:45 -0500 | [diff] [blame] | 2682 | buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL); |
| 2683 | if (!buf) { |
| 2684 | dev_err(&dd->pdev->dev, |
| 2685 | "Memory allocation: flag buffer\n"); |
| 2686 | return -ENOMEM; |
| 2687 | } |
| 2688 | |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 2689 | size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n", |
| 2690 | dd->port->flags); |
| 2691 | size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n", |
| 2692 | dd->dd_flag); |
| 2693 | |
| 2694 | *offset = size <= len ? size : len; |
| 2695 | size = copy_to_user(ubuf, buf, *offset); |
| 2696 | if (size) |
David Milburn | c8afd0d | 2013-05-23 16:23:45 -0500 | [diff] [blame] | 2697 | rv = -EFAULT; |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 2698 | |
David Milburn | c8afd0d | 2013-05-23 16:23:45 -0500 | [diff] [blame] | 2699 | kfree(buf); |
| 2700 | return rv ? rv : *offset; |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 2701 | } |
| 2702 | |
Asai Thambi S P | 0caff00 | 2013-04-03 19:56:21 +0530 | [diff] [blame] | 2703 | static const struct file_operations mtip_device_status_fops = { |
| 2704 | .owner = THIS_MODULE, |
| 2705 | .open = simple_open, |
| 2706 | .read = mtip_hw_read_device_status, |
| 2707 | .llseek = no_llseek, |
| 2708 | }; |
| 2709 | |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 2710 | static const struct file_operations mtip_regs_fops = { |
| 2711 | .owner = THIS_MODULE, |
| 2712 | .open = simple_open, |
| 2713 | .read = mtip_hw_read_registers, |
| 2714 | .llseek = no_llseek, |
| 2715 | }; |
| 2716 | |
| 2717 | static const struct file_operations mtip_flags_fops = { |
| 2718 | .owner = THIS_MODULE, |
| 2719 | .open = simple_open, |
| 2720 | .read = mtip_hw_read_flags, |
| 2721 | .llseek = no_llseek, |
| 2722 | }; |
| 2723 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2724 | /* |
| 2725 | * Create the sysfs related attributes. |
| 2726 | * |
| 2727 | * @dd Pointer to the driver data structure. |
| 2728 | * @kobj Pointer to the kobj for the block device. |
| 2729 | * |
| 2730 | * return value |
| 2731 | * 0 Operation completed successfully. |
| 2732 | * -EINVAL Invalid parameter. |
| 2733 | */ |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 2734 | static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2735 | { |
| 2736 | if (!kobj || !dd) |
| 2737 | return -EINVAL; |
| 2738 | |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 2739 | if (sysfs_create_file(kobj, &dev_attr_status.attr)) |
| 2740 | dev_warn(&dd->pdev->dev, |
| 2741 | "Error creating 'status' sysfs entry\n"); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2742 | return 0; |
| 2743 | } |
| 2744 | |
| 2745 | /* |
| 2746 | * Remove the sysfs related attributes. |
| 2747 | * |
| 2748 | * @dd Pointer to the driver data structure. |
| 2749 | * @kobj Pointer to the kobj for the block device. |
| 2750 | * |
| 2751 | * return value |
| 2752 | * 0 Operation completed successfully. |
| 2753 | * -EINVAL Invalid parameter. |
| 2754 | */ |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 2755 | static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2756 | { |
| 2757 | if (!kobj || !dd) |
| 2758 | return -EINVAL; |
| 2759 | |
Asai Thambi S P | f658721 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 2760 | sysfs_remove_file(kobj, &dev_attr_status.attr); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2761 | |
| 2762 | return 0; |
| 2763 | } |
| 2764 | |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 2765 | static int mtip_hw_debugfs_init(struct driver_data *dd) |
| 2766 | { |
| 2767 | if (!dfs_parent) |
| 2768 | return -1; |
| 2769 | |
| 2770 | dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent); |
| 2771 | if (IS_ERR_OR_NULL(dd->dfs_node)) { |
| 2772 | dev_warn(&dd->pdev->dev, |
| 2773 | "Error creating node %s under debugfs\n", |
| 2774 | dd->disk->disk_name); |
| 2775 | dd->dfs_node = NULL; |
| 2776 | return -1; |
| 2777 | } |
| 2778 | |
| 2779 | debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd, |
| 2780 | &mtip_flags_fops); |
| 2781 | debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd, |
| 2782 | &mtip_regs_fops); |
| 2783 | |
| 2784 | return 0; |
| 2785 | } |
| 2786 | |
| 2787 | static void mtip_hw_debugfs_exit(struct driver_data *dd) |
| 2788 | { |
Sam Bradshaw | 974a51a | 2013-05-15 10:04:34 +0200 | [diff] [blame] | 2789 | if (dd->dfs_node) |
| 2790 | debugfs_remove_recursive(dd->dfs_node); |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 2791 | } |
| 2792 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2793 | /* |
| 2794 | * Perform any init/resume time hardware setup |
| 2795 | * |
| 2796 | * @dd Pointer to the driver data structure. |
| 2797 | * |
| 2798 | * return value |
| 2799 | * None |
| 2800 | */ |
| 2801 | static inline void hba_setup(struct driver_data *dd) |
| 2802 | { |
| 2803 | u32 hwdata; |
| 2804 | hwdata = readl(dd->mmio + HOST_HSORG); |
| 2805 | |
| 2806 | /* interrupt bug workaround: use only 1 IS bit.*/ |
| 2807 | writel(hwdata | |
| 2808 | HSORG_DISABLE_SLOTGRP_INTR | |
| 2809 | HSORG_DISABLE_SLOTGRP_PXIS, |
| 2810 | dd->mmio + HOST_HSORG); |
| 2811 | } |
| 2812 | |
Asai Thambi S P | 2077d94 | 2013-04-29 21:19:49 +0200 | [diff] [blame] | 2813 | static int mtip_device_unaligned_constrained(struct driver_data *dd) |
| 2814 | { |
| 2815 | return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0); |
| 2816 | } |
| 2817 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2818 | /* |
| 2819 | * Detect the details of the product, and store anything needed |
| 2820 | * into the driver data structure. This includes product type and |
| 2821 | * version and number of slot groups. |
| 2822 | * |
| 2823 | * @dd Pointer to the driver data structure. |
| 2824 | * |
| 2825 | * return value |
| 2826 | * None |
| 2827 | */ |
| 2828 | static void mtip_detect_product(struct driver_data *dd) |
| 2829 | { |
| 2830 | u32 hwdata; |
| 2831 | unsigned int rev, slotgroups; |
| 2832 | |
| 2833 | /* |
| 2834 | * HBA base + 0xFC [15:0] - vendor-specific hardware interface |
| 2835 | * info register: |
| 2836 | * [15:8] hardware/software interface rev# |
| 2837 | * [ 3] asic-style interface |
| 2838 | * [ 2:0] number of slot groups, minus 1 (only valid for asic-style). |
| 2839 | */ |
| 2840 | hwdata = readl(dd->mmio + HOST_HSORG); |
| 2841 | |
| 2842 | dd->product_type = MTIP_PRODUCT_UNKNOWN; |
| 2843 | dd->slot_groups = 1; |
| 2844 | |
| 2845 | if (hwdata & 0x8) { |
| 2846 | dd->product_type = MTIP_PRODUCT_ASICFPGA; |
| 2847 | rev = (hwdata & HSORG_HWREV) >> 8; |
| 2848 | slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1; |
| 2849 | dev_info(&dd->pdev->dev, |
| 2850 | "ASIC-FPGA design, HS rev 0x%x, " |
| 2851 | "%i slot groups [%i slots]\n", |
| 2852 | rev, |
| 2853 | slotgroups, |
| 2854 | slotgroups * 32); |
| 2855 | |
| 2856 | if (slotgroups > MTIP_MAX_SLOT_GROUPS) { |
| 2857 | dev_warn(&dd->pdev->dev, |
| 2858 | "Warning: driver only supports " |
| 2859 | "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS); |
| 2860 | slotgroups = MTIP_MAX_SLOT_GROUPS; |
| 2861 | } |
| 2862 | dd->slot_groups = slotgroups; |
| 2863 | return; |
| 2864 | } |
| 2865 | |
| 2866 | dev_warn(&dd->pdev->dev, "Unrecognized product id\n"); |
| 2867 | } |
| 2868 | |
| 2869 | /* |
| 2870 | * Blocking wait for FTL rebuild to complete |
| 2871 | * |
| 2872 | * @dd Pointer to the DRIVER_DATA structure. |
| 2873 | * |
| 2874 | * return value |
| 2875 | * 0 FTL rebuild completed successfully |
| 2876 | * -EFAULT FTL rebuild error/timeout/interruption |
| 2877 | */ |
| 2878 | static int mtip_ftl_rebuild_poll(struct driver_data *dd) |
| 2879 | { |
| 2880 | unsigned long timeout, cnt = 0, start; |
| 2881 | |
| 2882 | dev_warn(&dd->pdev->dev, |
| 2883 | "FTL rebuild in progress. Polling for completion.\n"); |
| 2884 | |
| 2885 | start = jiffies; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2886 | timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS); |
| 2887 | |
| 2888 | do { |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 2889 | if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 2890 | &dd->dd_flag))) |
| 2891 | return -EFAULT; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2892 | if (mtip_check_surprise_removal(dd->pdev)) |
| 2893 | return -EFAULT; |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 2894 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2895 | if (mtip_get_identify(dd->port, NULL) < 0) |
| 2896 | return -EFAULT; |
| 2897 | |
| 2898 | if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) == |
| 2899 | MTIP_FTL_REBUILD_MAGIC) { |
| 2900 | ssleep(1); |
| 2901 | /* Print message every 3 minutes */ |
| 2902 | if (cnt++ >= 180) { |
| 2903 | dev_warn(&dd->pdev->dev, |
| 2904 | "FTL rebuild in progress (%d secs).\n", |
| 2905 | jiffies_to_msecs(jiffies - start) / 1000); |
| 2906 | cnt = 0; |
| 2907 | } |
| 2908 | } else { |
| 2909 | dev_warn(&dd->pdev->dev, |
| 2910 | "FTL rebuild complete (%d secs).\n", |
| 2911 | jiffies_to_msecs(jiffies - start) / 1000); |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 2912 | mtip_block_initialize(dd); |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 2913 | return 0; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2914 | } |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2915 | } while (time_before(jiffies, timeout)); |
| 2916 | |
| 2917 | /* Check for timeout */ |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 2918 | dev_err(&dd->pdev->dev, |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2919 | "Timed out waiting for FTL rebuild to complete (%d secs).\n", |
| 2920 | jiffies_to_msecs(jiffies - start) / 1000); |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 2921 | return -EFAULT; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2922 | } |
| 2923 | |
Asai Thambi SP | abb0ccd | 2016-02-24 21:21:13 -0800 | [diff] [blame] | 2924 | static void mtip_softirq_done_fn(struct request *rq) |
| 2925 | { |
| 2926 | struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq); |
| 2927 | struct driver_data *dd = rq->q->queuedata; |
| 2928 | |
| 2929 | /* Unmap the DMA scatter list entries */ |
| 2930 | dma_unmap_sg(&dd->pdev->dev, cmd->sg, cmd->scatter_ents, |
| 2931 | cmd->direction); |
| 2932 | |
| 2933 | if (unlikely(cmd->unaligned)) |
| 2934 | up(&dd->port->cmd_slot_unal); |
| 2935 | |
Christoph Hellwig | 4dda473 | 2017-04-20 16:03:07 +0200 | [diff] [blame] | 2936 | blk_mq_end_request(rq, cmd->status); |
Asai Thambi SP | abb0ccd | 2016-02-24 21:21:13 -0800 | [diff] [blame] | 2937 | } |
| 2938 | |
| 2939 | static void mtip_abort_cmd(struct request *req, void *data, |
| 2940 | bool reserved) |
| 2941 | { |
Christoph Hellwig | 4dda473 | 2017-04-20 16:03:07 +0200 | [diff] [blame] | 2942 | struct mtip_cmd *cmd = blk_mq_rq_to_pdu(req); |
Asai Thambi SP | abb0ccd | 2016-02-24 21:21:13 -0800 | [diff] [blame] | 2943 | struct driver_data *dd = data; |
| 2944 | |
| 2945 | dbg_printk(MTIP_DRV_NAME " Aborting request, tag = %d\n", req->tag); |
| 2946 | |
| 2947 | clear_bit(req->tag, dd->port->cmds_to_issue); |
Christoph Hellwig | 4dda473 | 2017-04-20 16:03:07 +0200 | [diff] [blame] | 2948 | cmd->status = -EIO; |
Asai Thambi SP | abb0ccd | 2016-02-24 21:21:13 -0800 | [diff] [blame] | 2949 | mtip_softirq_done_fn(req); |
| 2950 | } |
| 2951 | |
| 2952 | static void mtip_queue_cmd(struct request *req, void *data, |
| 2953 | bool reserved) |
| 2954 | { |
| 2955 | struct driver_data *dd = data; |
| 2956 | |
| 2957 | set_bit(req->tag, dd->port->cmds_to_issue); |
| 2958 | blk_abort_request(req); |
| 2959 | } |
| 2960 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 2961 | /* |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 2962 | * service thread to issue queued commands |
| 2963 | * |
| 2964 | * @data Pointer to the driver data structure. |
| 2965 | * |
| 2966 | * return value |
| 2967 | * 0 |
| 2968 | */ |
| 2969 | |
| 2970 | static int mtip_service_thread(void *data) |
| 2971 | { |
| 2972 | struct driver_data *dd = (struct driver_data *)data; |
Asai Thambi SP | abb0ccd | 2016-02-24 21:21:13 -0800 | [diff] [blame] | 2973 | unsigned long slot, slot_start, slot_wrap, to; |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 2974 | unsigned int num_cmd_slots = dd->slot_groups * 32; |
| 2975 | struct mtip_port *port = dd->port; |
| 2976 | |
| 2977 | while (1) { |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 2978 | if (kthread_should_stop() || |
| 2979 | test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags)) |
| 2980 | goto st_out; |
| 2981 | clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags); |
| 2982 | |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 2983 | /* |
| 2984 | * the condition is to check neither an internal command is |
| 2985 | * is in progress nor error handling is active |
| 2986 | */ |
| 2987 | wait_event_interruptible(port->svc_wait, (port->flags) && |
Asai Thambi SP | cfc05bd | 2016-02-24 21:16:00 -0800 | [diff] [blame] | 2988 | (port->flags & MTIP_PF_SVC_THD_WORK)); |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 2989 | |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 2990 | if (kthread_should_stop() || |
| 2991 | test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags)) |
| 2992 | goto st_out; |
| 2993 | |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 2994 | if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 2995 | &dd->dd_flag))) |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 2996 | goto st_out; |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 2997 | |
Asai Thambi SP | cfc05bd | 2016-02-24 21:16:00 -0800 | [diff] [blame] | 2998 | set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags); |
| 2999 | |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 3000 | restart_eh: |
| 3001 | /* Demux bits: start with error handling */ |
| 3002 | if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) { |
| 3003 | mtip_handle_tfe(dd); |
| 3004 | clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags); |
| 3005 | } |
| 3006 | |
| 3007 | if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) |
| 3008 | goto restart_eh; |
| 3009 | |
Asai Thambi SP | abb0ccd | 2016-02-24 21:21:13 -0800 | [diff] [blame] | 3010 | if (test_bit(MTIP_PF_TO_ACTIVE_BIT, &port->flags)) { |
| 3011 | to = jiffies + msecs_to_jiffies(5000); |
| 3012 | |
| 3013 | do { |
| 3014 | mdelay(100); |
| 3015 | } while (atomic_read(&dd->irq_workers_active) != 0 && |
| 3016 | time_before(jiffies, to)); |
| 3017 | |
| 3018 | if (atomic_read(&dd->irq_workers_active) != 0) |
| 3019 | dev_warn(&dd->pdev->dev, |
| 3020 | "Completion workers still active!"); |
| 3021 | |
| 3022 | spin_lock(dd->queue->queue_lock); |
Keith Busch | 6d125de | 2016-03-10 13:58:48 +0200 | [diff] [blame] | 3023 | blk_mq_tagset_busy_iter(&dd->tags, |
Asai Thambi SP | abb0ccd | 2016-02-24 21:21:13 -0800 | [diff] [blame] | 3024 | mtip_queue_cmd, dd); |
| 3025 | spin_unlock(dd->queue->queue_lock); |
| 3026 | |
| 3027 | set_bit(MTIP_PF_ISSUE_CMDS_BIT, &dd->port->flags); |
| 3028 | |
| 3029 | if (mtip_device_reset(dd)) |
Keith Busch | 6d125de | 2016-03-10 13:58:48 +0200 | [diff] [blame] | 3030 | blk_mq_tagset_busy_iter(&dd->tags, |
Asai Thambi SP | abb0ccd | 2016-02-24 21:21:13 -0800 | [diff] [blame] | 3031 | mtip_abort_cmd, dd); |
| 3032 | |
| 3033 | clear_bit(MTIP_PF_TO_ACTIVE_BIT, &dd->port->flags); |
| 3034 | } |
| 3035 | |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 3036 | if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) { |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 3037 | slot = 1; |
| 3038 | /* used to restrict the loop to one iteration */ |
| 3039 | slot_start = num_cmd_slots; |
| 3040 | slot_wrap = 0; |
| 3041 | while (1) { |
| 3042 | slot = find_next_bit(port->cmds_to_issue, |
| 3043 | num_cmd_slots, slot); |
| 3044 | if (slot_wrap == 1) { |
| 3045 | if ((slot_start >= slot) || |
| 3046 | (slot >= num_cmd_slots)) |
| 3047 | break; |
| 3048 | } |
| 3049 | if (unlikely(slot_start == num_cmd_slots)) |
| 3050 | slot_start = slot; |
| 3051 | |
| 3052 | if (unlikely(slot == num_cmd_slots)) { |
| 3053 | slot = 1; |
| 3054 | slot_wrap = 1; |
| 3055 | continue; |
| 3056 | } |
| 3057 | |
| 3058 | /* Issue the command to the hardware */ |
| 3059 | mtip_issue_ncq_command(port, slot); |
| 3060 | |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 3061 | clear_bit(slot, port->cmds_to_issue); |
| 3062 | } |
| 3063 | |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 3064 | clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags); |
Asai Thambi S P | 9b204fb | 2014-05-20 10:48:56 -0700 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) { |
Asai Thambi SP | 59cf70e | 2016-02-24 21:17:47 -0800 | [diff] [blame] | 3068 | if (mtip_ftl_rebuild_poll(dd) == 0) |
| 3069 | clear_bit(MTIP_PF_REBUILD_BIT, &port->flags); |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 3070 | } |
| 3071 | } |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 3072 | |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 3073 | st_out: |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 3074 | return 0; |
| 3075 | } |
| 3076 | |
| 3077 | /* |
Sam Bradshaw | 188b9f4 | 2014-01-15 10:14:57 -0800 | [diff] [blame] | 3078 | * DMA region teardown |
| 3079 | * |
| 3080 | * @dd Pointer to driver_data structure |
| 3081 | * |
| 3082 | * return value |
| 3083 | * None |
| 3084 | */ |
| 3085 | static void mtip_dma_free(struct driver_data *dd) |
| 3086 | { |
Sam Bradshaw | 188b9f4 | 2014-01-15 10:14:57 -0800 | [diff] [blame] | 3087 | struct mtip_port *port = dd->port; |
| 3088 | |
| 3089 | if (port->block1) |
| 3090 | dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ, |
| 3091 | port->block1, port->block1_dma); |
| 3092 | |
| 3093 | if (port->command_list) { |
| 3094 | dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ, |
| 3095 | port->command_list, port->command_list_dma); |
| 3096 | } |
Sam Bradshaw | 188b9f4 | 2014-01-15 10:14:57 -0800 | [diff] [blame] | 3097 | } |
| 3098 | |
| 3099 | /* |
| 3100 | * DMA region setup |
| 3101 | * |
| 3102 | * @dd Pointer to driver_data structure |
| 3103 | * |
| 3104 | * return value |
| 3105 | * -ENOMEM Not enough free DMA region space to initialize driver |
| 3106 | */ |
| 3107 | static int mtip_dma_alloc(struct driver_data *dd) |
| 3108 | { |
| 3109 | struct mtip_port *port = dd->port; |
Sam Bradshaw | 188b9f4 | 2014-01-15 10:14:57 -0800 | [diff] [blame] | 3110 | |
| 3111 | /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */ |
| 3112 | port->block1 = |
| 3113 | dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ, |
| 3114 | &port->block1_dma, GFP_KERNEL); |
| 3115 | if (!port->block1) |
| 3116 | return -ENOMEM; |
| 3117 | memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ); |
| 3118 | |
| 3119 | /* Allocate dma memory for command list */ |
| 3120 | port->command_list = |
| 3121 | dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ, |
| 3122 | &port->command_list_dma, GFP_KERNEL); |
| 3123 | if (!port->command_list) { |
| 3124 | dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ, |
| 3125 | port->block1, port->block1_dma); |
| 3126 | port->block1 = NULL; |
| 3127 | port->block1_dma = 0; |
| 3128 | return -ENOMEM; |
| 3129 | } |
| 3130 | memset(port->command_list, 0, AHCI_CMD_TBL_SZ); |
| 3131 | |
| 3132 | /* Setup all pointers into first DMA region */ |
| 3133 | port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET; |
| 3134 | port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET; |
| 3135 | port->identify = port->block1 + AHCI_IDFY_OFFSET; |
| 3136 | port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET; |
| 3137 | port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET; |
| 3138 | port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET; |
| 3139 | port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET; |
| 3140 | port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET; |
| 3141 | |
Sam Bradshaw | 188b9f4 | 2014-01-15 10:14:57 -0800 | [diff] [blame] | 3142 | return 0; |
| 3143 | } |
| 3144 | |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3145 | static int mtip_hw_get_identify(struct driver_data *dd) |
| 3146 | { |
| 3147 | struct smart_attr attr242; |
| 3148 | unsigned char *buf; |
| 3149 | int rv; |
| 3150 | |
| 3151 | if (mtip_get_identify(dd->port, NULL) < 0) |
| 3152 | return -EFAULT; |
| 3153 | |
| 3154 | if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) == |
| 3155 | MTIP_FTL_REBUILD_MAGIC) { |
| 3156 | set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags); |
| 3157 | return MTIP_FTL_REBUILD_MAGIC; |
| 3158 | } |
| 3159 | mtip_dump_identify(dd->port); |
| 3160 | |
| 3161 | /* check write protect, over temp and rebuild statuses */ |
| 3162 | rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ, |
| 3163 | dd->port->log_buf, |
| 3164 | dd->port->log_buf_dma, 1); |
| 3165 | if (rv) { |
| 3166 | dev_warn(&dd->pdev->dev, |
| 3167 | "Error in READ LOG EXT (10h) command\n"); |
| 3168 | /* non-critical error, don't fail the load */ |
| 3169 | } else { |
| 3170 | buf = (unsigned char *)dd->port->log_buf; |
| 3171 | if (buf[259] & 0x1) { |
| 3172 | dev_info(&dd->pdev->dev, |
| 3173 | "Write protect bit is set.\n"); |
| 3174 | set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag); |
| 3175 | } |
| 3176 | if (buf[288] == 0xF7) { |
| 3177 | dev_info(&dd->pdev->dev, |
| 3178 | "Exceeded Tmax, drive in thermal shutdown.\n"); |
| 3179 | set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag); |
| 3180 | } |
| 3181 | if (buf[288] == 0xBF) { |
| 3182 | dev_info(&dd->pdev->dev, |
| 3183 | "Drive indicates rebuild has failed.\n"); |
Asai Thambi SP | aae4a03 | 2016-02-24 21:18:20 -0800 | [diff] [blame] | 3184 | set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag); |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3185 | } |
| 3186 | } |
| 3187 | |
| 3188 | /* get write protect progess */ |
| 3189 | memset(&attr242, 0, sizeof(struct smart_attr)); |
| 3190 | if (mtip_get_smart_attr(dd->port, 242, &attr242)) |
| 3191 | dev_warn(&dd->pdev->dev, |
| 3192 | "Unable to check write protect progress\n"); |
| 3193 | else |
| 3194 | dev_info(&dd->pdev->dev, |
| 3195 | "Write protect progress: %u%% (%u blocks)\n", |
| 3196 | attr242.cur, le32_to_cpu(attr242.data)); |
| 3197 | |
| 3198 | return rv; |
| 3199 | } |
| 3200 | |
Sam Bradshaw | 188b9f4 | 2014-01-15 10:14:57 -0800 | [diff] [blame] | 3201 | /* |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3202 | * Called once for each card. |
| 3203 | * |
| 3204 | * @dd Pointer to the driver data structure. |
| 3205 | * |
| 3206 | * return value |
| 3207 | * 0 on success, else an error code. |
| 3208 | */ |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 3209 | static int mtip_hw_init(struct driver_data *dd) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3210 | { |
| 3211 | int i; |
| 3212 | int rv; |
| 3213 | unsigned int num_command_slots; |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 3214 | unsigned long timeout, timetaken; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3215 | |
| 3216 | dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR]; |
| 3217 | |
| 3218 | mtip_detect_product(dd); |
| 3219 | if (dd->product_type == MTIP_PRODUCT_UNKNOWN) { |
| 3220 | rv = -EIO; |
| 3221 | goto out1; |
| 3222 | } |
| 3223 | num_command_slots = dd->slot_groups * 32; |
| 3224 | |
| 3225 | hba_setup(dd); |
| 3226 | |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 3227 | dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL, |
| 3228 | dd->numa_node); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3229 | if (!dd->port) { |
| 3230 | dev_err(&dd->pdev->dev, |
| 3231 | "Memory allocation: port structure\n"); |
| 3232 | return -ENOMEM; |
| 3233 | } |
| 3234 | |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 3235 | /* Continue workqueue setup */ |
| 3236 | for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++) |
| 3237 | dd->work[i].port = dd->port; |
| 3238 | |
Asai Thambi S P | 2077d94 | 2013-04-29 21:19:49 +0200 | [diff] [blame] | 3239 | /* Enable unaligned IO constraints for some devices */ |
| 3240 | if (mtip_device_unaligned_constrained(dd)) |
| 3241 | dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS; |
| 3242 | else |
| 3243 | dd->unal_qdepth = 0; |
| 3244 | |
Asai Thambi S P | 2077d94 | 2013-04-29 21:19:49 +0200 | [diff] [blame] | 3245 | sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3246 | |
| 3247 | /* Spinlock to prevent concurrent issue */ |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 3248 | for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++) |
| 3249 | spin_lock_init(&dd->port->cmd_issue_lock[i]); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3250 | |
| 3251 | /* Set the port mmio base address. */ |
| 3252 | dd->port->mmio = dd->mmio + PORT_OFFSET; |
| 3253 | dd->port->dd = dd; |
| 3254 | |
Sam Bradshaw | 188b9f4 | 2014-01-15 10:14:57 -0800 | [diff] [blame] | 3255 | /* DMA allocations */ |
| 3256 | rv = mtip_dma_alloc(dd); |
| 3257 | if (rv < 0) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3258 | goto out1; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3259 | |
| 3260 | /* Setup the pointers to the extended s_active and CI registers. */ |
| 3261 | for (i = 0; i < dd->slot_groups; i++) { |
| 3262 | dd->port->s_active[i] = |
| 3263 | dd->port->mmio + i*0x80 + PORT_SCR_ACT; |
| 3264 | dd->port->cmd_issue[i] = |
| 3265 | dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE; |
| 3266 | dd->port->completed[i] = |
| 3267 | dd->port->mmio + i*0x80 + PORT_SDBV; |
| 3268 | } |
| 3269 | |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 3270 | timetaken = jiffies; |
| 3271 | timeout = jiffies + msecs_to_jiffies(30000); |
| 3272 | while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) && |
| 3273 | time_before(jiffies, timeout)) { |
| 3274 | mdelay(100); |
| 3275 | } |
| 3276 | if (unlikely(mtip_check_surprise_removal(dd->pdev))) { |
| 3277 | timetaken = jiffies - timetaken; |
| 3278 | dev_warn(&dd->pdev->dev, |
| 3279 | "Surprise removal detected at %u ms\n", |
| 3280 | jiffies_to_msecs(timetaken)); |
| 3281 | rv = -ENODEV; |
| 3282 | goto out2 ; |
| 3283 | } |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 3284 | if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) { |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 3285 | timetaken = jiffies - timetaken; |
| 3286 | dev_warn(&dd->pdev->dev, |
| 3287 | "Removal detected at %u ms\n", |
| 3288 | jiffies_to_msecs(timetaken)); |
| 3289 | rv = -EFAULT; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3290 | goto out2; |
| 3291 | } |
| 3292 | |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 3293 | /* Conditionally reset the HBA. */ |
| 3294 | if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) { |
| 3295 | if (mtip_hba_reset(dd) < 0) { |
| 3296 | dev_err(&dd->pdev->dev, |
| 3297 | "Card did not reset within timeout\n"); |
| 3298 | rv = -EIO; |
| 3299 | goto out2; |
| 3300 | } |
| 3301 | } else { |
| 3302 | /* Clear any pending interrupts on the HBA */ |
| 3303 | writel(readl(dd->mmio + HOST_IRQ_STAT), |
| 3304 | dd->mmio + HOST_IRQ_STAT); |
| 3305 | } |
| 3306 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3307 | mtip_init_port(dd->port); |
| 3308 | mtip_start_port(dd->port); |
| 3309 | |
| 3310 | /* Setup the ISR and enable interrupts. */ |
| 3311 | rv = devm_request_irq(&dd->pdev->dev, |
| 3312 | dd->pdev->irq, |
| 3313 | mtip_irq_handler, |
| 3314 | IRQF_SHARED, |
| 3315 | dev_driver_string(&dd->pdev->dev), |
| 3316 | dd); |
| 3317 | |
| 3318 | if (rv) { |
| 3319 | dev_err(&dd->pdev->dev, |
| 3320 | "Unable to allocate IRQ %d\n", dd->pdev->irq); |
| 3321 | goto out2; |
| 3322 | } |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 3323 | irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding)); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3324 | |
| 3325 | /* Enable interrupts on the HBA. */ |
| 3326 | writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN, |
| 3327 | dd->mmio + HOST_CTL); |
| 3328 | |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 3329 | init_waitqueue_head(&dd->port->svc_wait); |
| 3330 | |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 3331 | if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) { |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 3332 | rv = -EFAULT; |
| 3333 | goto out3; |
| 3334 | } |
| 3335 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3336 | return rv; |
| 3337 | |
| 3338 | out3: |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3339 | /* Disable interrupts on the HBA. */ |
| 3340 | writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN, |
| 3341 | dd->mmio + HOST_CTL); |
| 3342 | |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 3343 | /* Release the IRQ. */ |
| 3344 | irq_set_affinity_hint(dd->pdev->irq, NULL); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3345 | devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd); |
| 3346 | |
| 3347 | out2: |
| 3348 | mtip_deinit_port(dd->port); |
Sam Bradshaw | 188b9f4 | 2014-01-15 10:14:57 -0800 | [diff] [blame] | 3349 | mtip_dma_free(dd); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3350 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3351 | out1: |
| 3352 | /* Free the memory allocated for the for structure. */ |
| 3353 | kfree(dd->port); |
| 3354 | |
| 3355 | return rv; |
| 3356 | } |
| 3357 | |
Asai Thambi SP | d8a18d2 | 2016-02-24 21:17:32 -0800 | [diff] [blame] | 3358 | static int mtip_standby_drive(struct driver_data *dd) |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3359 | { |
Asai Thambi SP | d8a18d2 | 2016-02-24 21:17:32 -0800 | [diff] [blame] | 3360 | int rv = 0; |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3361 | |
Asai Thambi SP | d8a18d2 | 2016-02-24 21:17:32 -0800 | [diff] [blame] | 3362 | if (dd->sr || !dd->port) |
| 3363 | return -ENODEV; |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3364 | /* |
| 3365 | * Send standby immediate (E0h) to the drive so that it |
| 3366 | * saves its state. |
| 3367 | */ |
| 3368 | if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) && |
Asai Thambi SP | d8a18d2 | 2016-02-24 21:17:32 -0800 | [diff] [blame] | 3369 | !test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag) && |
| 3370 | !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)) { |
| 3371 | rv = mtip_standby_immediate(dd->port); |
| 3372 | if (rv) |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3373 | dev_warn(&dd->pdev->dev, |
| 3374 | "STANDBY IMMEDIATE failed\n"); |
Asai Thambi SP | d8a18d2 | 2016-02-24 21:17:32 -0800 | [diff] [blame] | 3375 | } |
| 3376 | return rv; |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3377 | } |
| 3378 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3379 | /* |
| 3380 | * Called to deinitialize an interface. |
| 3381 | * |
| 3382 | * @dd Pointer to the driver data structure. |
| 3383 | * |
| 3384 | * return value |
| 3385 | * 0 |
| 3386 | */ |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 3387 | static int mtip_hw_exit(struct driver_data *dd) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3388 | { |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 3389 | if (!dd->sr) { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3390 | /* de-initialize the port. */ |
| 3391 | mtip_deinit_port(dd->port); |
| 3392 | |
| 3393 | /* Disable interrupts on the HBA. */ |
| 3394 | writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN, |
| 3395 | dd->mmio + HOST_CTL); |
| 3396 | } |
| 3397 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3398 | /* Release the IRQ. */ |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 3399 | irq_set_affinity_hint(dd->pdev->irq, NULL); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3400 | devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd); |
Asai Thambi SP | 2132a54 | 2015-05-11 15:53:18 -0700 | [diff] [blame] | 3401 | msleep(1000); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3402 | |
Sam Bradshaw | 188b9f4 | 2014-01-15 10:14:57 -0800 | [diff] [blame] | 3403 | /* Free dma regions */ |
| 3404 | mtip_dma_free(dd); |
| 3405 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3406 | /* Free the memory allocated for the for structure. */ |
| 3407 | kfree(dd->port); |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 3408 | dd->port = NULL; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3409 | |
| 3410 | return 0; |
| 3411 | } |
| 3412 | |
| 3413 | /* |
| 3414 | * Issue a Standby Immediate command to the device. |
| 3415 | * |
| 3416 | * This function is called by the Block Layer just before the |
| 3417 | * system powers off during a shutdown. |
| 3418 | * |
| 3419 | * @dd Pointer to the driver data structure. |
| 3420 | * |
| 3421 | * return value |
| 3422 | * 0 |
| 3423 | */ |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 3424 | static int mtip_hw_shutdown(struct driver_data *dd) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3425 | { |
| 3426 | /* |
| 3427 | * Send standby immediate (E0h) to the drive so that it |
| 3428 | * saves its state. |
| 3429 | */ |
Asai Thambi SP | d8a18d2 | 2016-02-24 21:17:32 -0800 | [diff] [blame] | 3430 | mtip_standby_drive(dd); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3431 | |
| 3432 | return 0; |
| 3433 | } |
| 3434 | |
| 3435 | /* |
| 3436 | * Suspend function |
| 3437 | * |
| 3438 | * This function is called by the Block Layer just before the |
| 3439 | * system hibernates. |
| 3440 | * |
| 3441 | * @dd Pointer to the driver data structure. |
| 3442 | * |
| 3443 | * return value |
| 3444 | * 0 Suspend was successful |
| 3445 | * -EFAULT Suspend was not successful |
| 3446 | */ |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 3447 | static int mtip_hw_suspend(struct driver_data *dd) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3448 | { |
| 3449 | /* |
| 3450 | * Send standby immediate (E0h) to the drive |
| 3451 | * so that it saves its state. |
| 3452 | */ |
Asai Thambi SP | d8a18d2 | 2016-02-24 21:17:32 -0800 | [diff] [blame] | 3453 | if (mtip_standby_drive(dd) != 0) { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3454 | dev_err(&dd->pdev->dev, |
| 3455 | "Failed standby-immediate command\n"); |
| 3456 | return -EFAULT; |
| 3457 | } |
| 3458 | |
| 3459 | /* Disable interrupts on the HBA.*/ |
| 3460 | writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN, |
| 3461 | dd->mmio + HOST_CTL); |
| 3462 | mtip_deinit_port(dd->port); |
| 3463 | |
| 3464 | return 0; |
| 3465 | } |
| 3466 | |
| 3467 | /* |
| 3468 | * Resume function |
| 3469 | * |
| 3470 | * This function is called by the Block Layer as the |
| 3471 | * system resumes. |
| 3472 | * |
| 3473 | * @dd Pointer to the driver data structure. |
| 3474 | * |
| 3475 | * return value |
| 3476 | * 0 Resume was successful |
| 3477 | * -EFAULT Resume was not successful |
| 3478 | */ |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 3479 | static int mtip_hw_resume(struct driver_data *dd) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3480 | { |
| 3481 | /* Perform any needed hardware setup steps */ |
| 3482 | hba_setup(dd); |
| 3483 | |
| 3484 | /* Reset the HBA */ |
| 3485 | if (mtip_hba_reset(dd) != 0) { |
| 3486 | dev_err(&dd->pdev->dev, |
| 3487 | "Unable to reset the HBA\n"); |
| 3488 | return -EFAULT; |
| 3489 | } |
| 3490 | |
| 3491 | /* |
| 3492 | * Enable the port, DMA engine, and FIS reception specific |
| 3493 | * h/w in controller. |
| 3494 | */ |
| 3495 | mtip_init_port(dd->port); |
| 3496 | mtip_start_port(dd->port); |
| 3497 | |
| 3498 | /* Enable interrupts on the HBA.*/ |
| 3499 | writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN, |
| 3500 | dd->mmio + HOST_CTL); |
| 3501 | |
| 3502 | return 0; |
| 3503 | } |
| 3504 | |
| 3505 | /* |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3506 | * Helper function for reusing disk name |
| 3507 | * upon hot insertion. |
| 3508 | */ |
| 3509 | static int rssd_disk_name_format(char *prefix, |
| 3510 | int index, |
| 3511 | char *buf, |
| 3512 | int buflen) |
| 3513 | { |
| 3514 | const int base = 'z' - 'a' + 1; |
| 3515 | char *begin = buf + strlen(prefix); |
| 3516 | char *end = buf + buflen; |
| 3517 | char *p; |
| 3518 | int unit; |
| 3519 | |
| 3520 | p = end - 1; |
| 3521 | *p = '\0'; |
| 3522 | unit = base; |
| 3523 | do { |
| 3524 | if (p == begin) |
| 3525 | return -EINVAL; |
| 3526 | *--p = 'a' + (index % unit); |
| 3527 | index = (index / unit) - 1; |
| 3528 | } while (index >= 0); |
| 3529 | |
| 3530 | memmove(begin, p, end - p); |
| 3531 | memcpy(buf, prefix, strlen(prefix)); |
| 3532 | |
| 3533 | return 0; |
| 3534 | } |
| 3535 | |
| 3536 | /* |
| 3537 | * Block layer IOCTL handler. |
| 3538 | * |
| 3539 | * @dev Pointer to the block_device structure. |
| 3540 | * @mode ignored |
| 3541 | * @cmd IOCTL command passed from the user application. |
| 3542 | * @arg Argument passed from the user application. |
| 3543 | * |
| 3544 | * return value |
| 3545 | * 0 IOCTL completed successfully. |
| 3546 | * -ENOTTY IOCTL not supported or invalid driver data |
| 3547 | * structure pointer. |
| 3548 | */ |
| 3549 | static int mtip_block_ioctl(struct block_device *dev, |
| 3550 | fmode_t mode, |
| 3551 | unsigned cmd, |
| 3552 | unsigned long arg) |
| 3553 | { |
| 3554 | struct driver_data *dd = dev->bd_disk->private_data; |
| 3555 | |
| 3556 | if (!capable(CAP_SYS_ADMIN)) |
| 3557 | return -EACCES; |
| 3558 | |
| 3559 | if (!dd) |
| 3560 | return -ENOTTY; |
| 3561 | |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 3562 | if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 3563 | return -ENOTTY; |
| 3564 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3565 | switch (cmd) { |
| 3566 | case BLKFLSBUF: |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 3567 | return -ENOTTY; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3568 | default: |
Jens Axboe | ef0f158 | 2011-09-27 21:19:53 -0600 | [diff] [blame] | 3569 | return mtip_hw_ioctl(dd, cmd, arg); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3570 | } |
| 3571 | } |
| 3572 | |
Jens Axboe | 16d02c0 | 2011-09-27 15:50:01 -0600 | [diff] [blame] | 3573 | #ifdef CONFIG_COMPAT |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3574 | /* |
| 3575 | * Block layer compat IOCTL handler. |
| 3576 | * |
| 3577 | * @dev Pointer to the block_device structure. |
| 3578 | * @mode ignored |
| 3579 | * @cmd IOCTL command passed from the user application. |
| 3580 | * @arg Argument passed from the user application. |
| 3581 | * |
| 3582 | * return value |
| 3583 | * 0 IOCTL completed successfully. |
| 3584 | * -ENOTTY IOCTL not supported or invalid driver data |
| 3585 | * structure pointer. |
| 3586 | */ |
| 3587 | static int mtip_block_compat_ioctl(struct block_device *dev, |
| 3588 | fmode_t mode, |
| 3589 | unsigned cmd, |
| 3590 | unsigned long arg) |
| 3591 | { |
| 3592 | struct driver_data *dd = dev->bd_disk->private_data; |
| 3593 | |
| 3594 | if (!capable(CAP_SYS_ADMIN)) |
| 3595 | return -EACCES; |
| 3596 | |
| 3597 | if (!dd) |
| 3598 | return -ENOTTY; |
| 3599 | |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 3600 | if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 3601 | return -ENOTTY; |
| 3602 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3603 | switch (cmd) { |
| 3604 | case BLKFLSBUF: |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 3605 | return -ENOTTY; |
Jens Axboe | ef0f158 | 2011-09-27 21:19:53 -0600 | [diff] [blame] | 3606 | case HDIO_DRIVE_TASKFILE: { |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 3607 | struct mtip_compat_ide_task_request_s __user *compat_req_task; |
Jens Axboe | ef0f158 | 2011-09-27 21:19:53 -0600 | [diff] [blame] | 3608 | ide_task_request_t req_task; |
| 3609 | int compat_tasksize, outtotal, ret; |
| 3610 | |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 3611 | compat_tasksize = |
| 3612 | sizeof(struct mtip_compat_ide_task_request_s); |
Jens Axboe | ef0f158 | 2011-09-27 21:19:53 -0600 | [diff] [blame] | 3613 | |
| 3614 | compat_req_task = |
| 3615 | (struct mtip_compat_ide_task_request_s __user *) arg; |
| 3616 | |
| 3617 | if (copy_from_user(&req_task, (void __user *) arg, |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 3618 | compat_tasksize - (2 * sizeof(compat_long_t)))) |
Jens Axboe | ef0f158 | 2011-09-27 21:19:53 -0600 | [diff] [blame] | 3619 | return -EFAULT; |
| 3620 | |
| 3621 | if (get_user(req_task.out_size, &compat_req_task->out_size)) |
| 3622 | return -EFAULT; |
| 3623 | |
| 3624 | if (get_user(req_task.in_size, &compat_req_task->in_size)) |
| 3625 | return -EFAULT; |
| 3626 | |
| 3627 | outtotal = sizeof(struct mtip_compat_ide_task_request_s); |
| 3628 | |
| 3629 | ret = exec_drive_taskfile(dd, (void __user *) arg, |
| 3630 | &req_task, outtotal); |
| 3631 | |
| 3632 | if (copy_to_user((void __user *) arg, &req_task, |
| 3633 | compat_tasksize - |
| 3634 | (2 * sizeof(compat_long_t)))) |
| 3635 | return -EFAULT; |
| 3636 | |
| 3637 | if (put_user(req_task.out_size, &compat_req_task->out_size)) |
| 3638 | return -EFAULT; |
| 3639 | |
| 3640 | if (put_user(req_task.in_size, &compat_req_task->in_size)) |
| 3641 | return -EFAULT; |
| 3642 | |
| 3643 | return ret; |
| 3644 | } |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3645 | default: |
Jens Axboe | ef0f158 | 2011-09-27 21:19:53 -0600 | [diff] [blame] | 3646 | return mtip_hw_ioctl(dd, cmd, arg); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3647 | } |
| 3648 | } |
Jens Axboe | 16d02c0 | 2011-09-27 15:50:01 -0600 | [diff] [blame] | 3649 | #endif |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3650 | |
| 3651 | /* |
| 3652 | * Obtain the geometry of the device. |
| 3653 | * |
| 3654 | * You may think that this function is obsolete, but some applications, |
| 3655 | * fdisk for example still used CHS values. This function describes the |
| 3656 | * device as having 224 heads and 56 sectors per cylinder. These values are |
| 3657 | * chosen so that each cylinder is aligned on a 4KB boundary. Since a |
| 3658 | * partition is described in terms of a start and end cylinder this means |
| 3659 | * that each partition is also 4KB aligned. Non-aligned partitions adversely |
| 3660 | * affects performance. |
| 3661 | * |
| 3662 | * @dev Pointer to the block_device strucutre. |
| 3663 | * @geo Pointer to a hd_geometry structure. |
| 3664 | * |
| 3665 | * return value |
| 3666 | * 0 Operation completed successfully. |
| 3667 | * -ENOTTY An error occurred while reading the drive capacity. |
| 3668 | */ |
| 3669 | static int mtip_block_getgeo(struct block_device *dev, |
| 3670 | struct hd_geometry *geo) |
| 3671 | { |
| 3672 | struct driver_data *dd = dev->bd_disk->private_data; |
| 3673 | sector_t capacity; |
| 3674 | |
| 3675 | if (!dd) |
| 3676 | return -ENOTTY; |
| 3677 | |
| 3678 | if (!(mtip_hw_get_capacity(dd, &capacity))) { |
| 3679 | dev_warn(&dd->pdev->dev, |
| 3680 | "Could not get drive capacity.\n"); |
| 3681 | return -ENOTTY; |
| 3682 | } |
| 3683 | |
| 3684 | geo->heads = 224; |
| 3685 | geo->sectors = 56; |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 3686 | sector_div(capacity, (geo->heads * geo->sectors)); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3687 | geo->cylinders = capacity; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3688 | return 0; |
| 3689 | } |
| 3690 | |
Asai Thambi SP | 51c6570 | 2016-02-24 21:18:10 -0800 | [diff] [blame] | 3691 | static int mtip_block_open(struct block_device *dev, fmode_t mode) |
| 3692 | { |
| 3693 | struct driver_data *dd; |
| 3694 | |
| 3695 | if (dev && dev->bd_disk) { |
| 3696 | dd = (struct driver_data *) dev->bd_disk->private_data; |
| 3697 | |
| 3698 | if (dd) { |
| 3699 | if (test_bit(MTIP_DDF_REMOVAL_BIT, |
| 3700 | &dd->dd_flag)) { |
| 3701 | return -ENODEV; |
| 3702 | } |
| 3703 | return 0; |
| 3704 | } |
| 3705 | } |
| 3706 | return -ENODEV; |
| 3707 | } |
| 3708 | |
Baoyou Xie | 99e6b87 | 2016-08-26 14:08:53 +0800 | [diff] [blame] | 3709 | static void mtip_block_release(struct gendisk *disk, fmode_t mode) |
Asai Thambi SP | 51c6570 | 2016-02-24 21:18:10 -0800 | [diff] [blame] | 3710 | { |
| 3711 | } |
| 3712 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3713 | /* |
| 3714 | * Block device operation function. |
| 3715 | * |
| 3716 | * This structure contains pointers to the functions required by the block |
| 3717 | * layer. |
| 3718 | */ |
| 3719 | static const struct block_device_operations mtip_block_ops = { |
Asai Thambi SP | 51c6570 | 2016-02-24 21:18:10 -0800 | [diff] [blame] | 3720 | .open = mtip_block_open, |
| 3721 | .release = mtip_block_release, |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3722 | .ioctl = mtip_block_ioctl, |
Jens Axboe | 16d02c0 | 2011-09-27 15:50:01 -0600 | [diff] [blame] | 3723 | #ifdef CONFIG_COMPAT |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3724 | .compat_ioctl = mtip_block_compat_ioctl, |
Jens Axboe | 16d02c0 | 2011-09-27 15:50:01 -0600 | [diff] [blame] | 3725 | #endif |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3726 | .getgeo = mtip_block_getgeo, |
| 3727 | .owner = THIS_MODULE |
| 3728 | }; |
| 3729 | |
Asai Thambi SP | 686d8e0 | 2015-05-11 15:51:27 -0700 | [diff] [blame] | 3730 | static inline bool is_se_active(struct driver_data *dd) |
| 3731 | { |
| 3732 | if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT, &dd->port->flags))) { |
| 3733 | if (dd->port->ic_pause_timer) { |
| 3734 | unsigned long to = dd->port->ic_pause_timer + |
| 3735 | msecs_to_jiffies(1000); |
| 3736 | if (time_after(jiffies, to)) { |
| 3737 | clear_bit(MTIP_PF_SE_ACTIVE_BIT, |
| 3738 | &dd->port->flags); |
| 3739 | clear_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag); |
| 3740 | dd->port->ic_pause_timer = 0; |
| 3741 | wake_up_interruptible(&dd->port->svc_wait); |
| 3742 | return false; |
| 3743 | } |
| 3744 | } |
| 3745 | return true; |
| 3746 | } |
| 3747 | return false; |
| 3748 | } |
| 3749 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3750 | /* |
| 3751 | * Block layer make request function. |
| 3752 | * |
| 3753 | * This function is called by the kernel to process a BIO for |
| 3754 | * the P320 device. |
| 3755 | * |
| 3756 | * @queue Pointer to the request queue. Unused other than to obtain |
| 3757 | * the driver data structure. |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3758 | * @rq Pointer to the request. |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3759 | * |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3760 | */ |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3761 | static int mtip_submit_request(struct blk_mq_hw_ctx *hctx, struct request *rq) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3762 | { |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3763 | struct driver_data *dd = hctx->queue->queuedata; |
| 3764 | struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq); |
| 3765 | unsigned int nents; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3766 | |
Asai Thambi SP | 686d8e0 | 2015-05-11 15:51:27 -0700 | [diff] [blame] | 3767 | if (is_se_active(dd)) |
| 3768 | return -ENODATA; |
| 3769 | |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 3770 | if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) { |
| 3771 | if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, |
| 3772 | &dd->dd_flag))) { |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3773 | return -ENXIO; |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 3774 | } |
| 3775 | if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) { |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3776 | return -ENODATA; |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 3777 | } |
| 3778 | if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT, |
| 3779 | &dd->dd_flag) && |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3780 | rq_data_dir(rq))) { |
| 3781 | return -ENODATA; |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 3782 | } |
Asai Thambi SP | aae4a03 | 2016-02-24 21:18:20 -0800 | [diff] [blame] | 3783 | if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag) || |
| 3784 | test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag))) |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3785 | return -ENODATA; |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 3786 | } |
| 3787 | |
Mike Christie | c2df40d | 2016-06-05 14:32:17 -0500 | [diff] [blame] | 3788 | if (req_op(rq) == REQ_OP_DISCARD) { |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3789 | int err; |
| 3790 | |
| 3791 | err = mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq)); |
Christoph Hellwig | c8a446a | 2014-09-13 16:40:10 -0700 | [diff] [blame] | 3792 | blk_mq_end_request(rq, err); |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3793 | return 0; |
Asai Thambi S P | 1528346 | 2013-01-11 14:41:34 +0100 | [diff] [blame] | 3794 | } |
| 3795 | |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3796 | /* Create the scatter list for this request. */ |
| 3797 | nents = blk_rq_map_sg(hctx->queue, rq, cmd->sg); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3798 | |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3799 | /* Issue the read/write. */ |
| 3800 | mtip_hw_submit_io(dd, rq, cmd, nents, hctx); |
| 3801 | return 0; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3802 | } |
| 3803 | |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3804 | static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx, |
| 3805 | struct request *rq) |
| 3806 | { |
| 3807 | struct driver_data *dd = hctx->queue->queuedata; |
| 3808 | struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq); |
| 3809 | |
Sam Bradshaw | f45c40a | 2014-06-06 13:28:48 -0600 | [diff] [blame] | 3810 | if (rq_data_dir(rq) == READ || !dd->unal_qdepth) |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3811 | return false; |
| 3812 | |
| 3813 | /* |
| 3814 | * If unaligned depth must be limited on this controller, mark it |
| 3815 | * as unaligned if the IO isn't on a 4k boundary (start of length). |
| 3816 | */ |
| 3817 | if (blk_rq_sectors(rq) <= 64) { |
| 3818 | if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7)) |
| 3819 | cmd->unaligned = 1; |
| 3820 | } |
| 3821 | |
| 3822 | if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal)) |
| 3823 | return true; |
| 3824 | |
| 3825 | return false; |
| 3826 | } |
| 3827 | |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 3828 | static int mtip_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 3829 | const struct blk_mq_queue_data *bd) |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3830 | { |
Jens Axboe | 74c4505 | 2014-10-29 11:14:52 -0600 | [diff] [blame] | 3831 | struct request *rq = bd->rq; |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3832 | int ret; |
| 3833 | |
Ming Lei | a4e84aa | 2017-04-27 07:45:18 -0600 | [diff] [blame^] | 3834 | mtip_init_cmd_header(rq); |
| 3835 | |
Sam Bradshaw | f45c40a | 2014-06-06 13:28:48 -0600 | [diff] [blame] | 3836 | if (unlikely(mtip_check_unal_depth(hctx, rq))) |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3837 | return BLK_MQ_RQ_QUEUE_BUSY; |
| 3838 | |
Christoph Hellwig | e249007 | 2014-09-13 16:40:09 -0700 | [diff] [blame] | 3839 | blk_mq_start_request(rq); |
| 3840 | |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3841 | ret = mtip_submit_request(hctx, rq); |
Sam Bradshaw | f45c40a | 2014-06-06 13:28:48 -0600 | [diff] [blame] | 3842 | if (likely(!ret)) |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3843 | return BLK_MQ_RQ_QUEUE_OK; |
| 3844 | |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3845 | return BLK_MQ_RQ_QUEUE_ERROR; |
| 3846 | } |
| 3847 | |
| 3848 | static void mtip_free_cmd(void *data, struct request *rq, |
| 3849 | unsigned int hctx_idx, unsigned int request_idx) |
| 3850 | { |
| 3851 | struct driver_data *dd = data; |
| 3852 | struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq); |
| 3853 | |
| 3854 | if (!cmd->command) |
| 3855 | return; |
| 3856 | |
| 3857 | dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ, |
| 3858 | cmd->command, cmd->command_dma); |
| 3859 | } |
| 3860 | |
| 3861 | static int mtip_init_cmd(void *data, struct request *rq, unsigned int hctx_idx, |
| 3862 | unsigned int request_idx, unsigned int numa_node) |
| 3863 | { |
| 3864 | struct driver_data *dd = data; |
| 3865 | struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq); |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3866 | |
Jeff Moyer | 74c9c91 | 2015-07-29 10:22:50 -0400 | [diff] [blame] | 3867 | /* |
| 3868 | * For flush requests, request_idx starts at the end of the |
| 3869 | * tag space. Since we don't support FLUSH/FUA, simply return |
| 3870 | * 0 as there's nothing to be done. |
| 3871 | */ |
| 3872 | if (request_idx >= MTIP_MAX_COMMAND_SLOTS) |
| 3873 | return 0; |
| 3874 | |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3875 | cmd->command = dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ, |
| 3876 | &cmd->command_dma, GFP_KERNEL); |
| 3877 | if (!cmd->command) |
| 3878 | return -ENOMEM; |
| 3879 | |
| 3880 | memset(cmd->command, 0, CMD_DMA_ALLOC_SZ); |
| 3881 | |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3882 | sg_init_table(cmd->sg, MTIP_MAX_SG); |
| 3883 | return 0; |
| 3884 | } |
| 3885 | |
Asai Thambi SP | abb0ccd | 2016-02-24 21:21:13 -0800 | [diff] [blame] | 3886 | static enum blk_eh_timer_return mtip_cmd_timeout(struct request *req, |
| 3887 | bool reserved) |
| 3888 | { |
| 3889 | struct driver_data *dd = req->q->queuedata; |
Asai Thambi SP | abb0ccd | 2016-02-24 21:21:13 -0800 | [diff] [blame] | 3890 | |
| 3891 | if (reserved) |
| 3892 | goto exit_handler; |
| 3893 | |
| 3894 | if (test_bit(req->tag, dd->port->cmds_to_issue)) |
| 3895 | goto exit_handler; |
| 3896 | |
| 3897 | if (test_and_set_bit(MTIP_PF_TO_ACTIVE_BIT, &dd->port->flags)) |
| 3898 | goto exit_handler; |
| 3899 | |
| 3900 | wake_up_interruptible(&dd->port->svc_wait); |
| 3901 | exit_handler: |
Jens Axboe | 90beb2e | 2016-03-04 08:15:48 -0700 | [diff] [blame] | 3902 | return BLK_EH_RESET_TIMER; |
Asai Thambi SP | abb0ccd | 2016-02-24 21:21:13 -0800 | [diff] [blame] | 3903 | } |
| 3904 | |
Eric Biggers | f363b08 | 2017-03-30 13:39:16 -0700 | [diff] [blame] | 3905 | static const struct blk_mq_ops mtip_mq_ops = { |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3906 | .queue_rq = mtip_queue_rq, |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3907 | .init_request = mtip_init_cmd, |
| 3908 | .exit_request = mtip_free_cmd, |
Asai Thambi SP | abb0ccd | 2016-02-24 21:21:13 -0800 | [diff] [blame] | 3909 | .complete = mtip_softirq_done_fn, |
| 3910 | .timeout = mtip_cmd_timeout, |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3911 | }; |
| 3912 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3913 | /* |
| 3914 | * Block layer initialization function. |
| 3915 | * |
| 3916 | * This function is called once by the PCI layer for each P320 |
| 3917 | * device that is connected to the system. |
| 3918 | * |
| 3919 | * @dd Pointer to the driver data structure. |
| 3920 | * |
| 3921 | * return value |
| 3922 | * 0 on success else an error code. |
| 3923 | */ |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 3924 | static int mtip_block_initialize(struct driver_data *dd) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3925 | { |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 3926 | int rv = 0, wait_for_rebuild = 0; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3927 | sector_t capacity; |
| 3928 | unsigned int index = 0; |
| 3929 | struct kobject *kobj; |
| 3930 | |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 3931 | if (dd->disk) |
| 3932 | goto skip_create_disk; /* hw init done, before rebuild */ |
| 3933 | |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3934 | if (mtip_hw_init(dd)) { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3935 | rv = -EINVAL; |
| 3936 | goto protocol_init_error; |
| 3937 | } |
| 3938 | |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 3939 | dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3940 | if (dd->disk == NULL) { |
| 3941 | dev_err(&dd->pdev->dev, |
| 3942 | "Unable to allocate gendisk structure\n"); |
| 3943 | rv = -EINVAL; |
| 3944 | goto alloc_disk_error; |
| 3945 | } |
| 3946 | |
| 3947 | /* Generate the disk name, implemented same as in sd.c */ |
| 3948 | do { |
Pan Bian | 5b0e34e | 2016-12-01 10:10:46 +0800 | [diff] [blame] | 3949 | if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL)) { |
| 3950 | rv = -ENOMEM; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3951 | goto ida_get_error; |
Pan Bian | 5b0e34e | 2016-12-01 10:10:46 +0800 | [diff] [blame] | 3952 | } |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3953 | |
| 3954 | spin_lock(&rssd_index_lock); |
| 3955 | rv = ida_get_new(&rssd_index_ida, &index); |
| 3956 | spin_unlock(&rssd_index_lock); |
| 3957 | } while (rv == -EAGAIN); |
| 3958 | |
| 3959 | if (rv) |
| 3960 | goto ida_get_error; |
| 3961 | |
| 3962 | rv = rssd_disk_name_format("rssd", |
| 3963 | index, |
| 3964 | dd->disk->disk_name, |
| 3965 | DISK_NAME_LEN); |
| 3966 | if (rv) |
| 3967 | goto disk_index_error; |
| 3968 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3969 | dd->disk->major = dd->major; |
Asai Thambi SP | 7578726 | 2015-05-11 15:55:26 -0700 | [diff] [blame] | 3970 | dd->disk->first_minor = index * MTIP_MAX_MINORS; |
| 3971 | dd->disk->minors = MTIP_MAX_MINORS; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3972 | dd->disk->fops = &mtip_block_ops; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3973 | dd->disk->private_data = dd; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 3974 | dd->index = index; |
| 3975 | |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 3976 | mtip_hw_debugfs_init(dd); |
| 3977 | |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3978 | memset(&dd->tags, 0, sizeof(dd->tags)); |
| 3979 | dd->tags.ops = &mtip_mq_ops; |
| 3980 | dd->tags.nr_hw_queues = 1; |
| 3981 | dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS; |
| 3982 | dd->tags.reserved_tags = 1; |
| 3983 | dd->tags.cmd_size = sizeof(struct mtip_cmd); |
| 3984 | dd->tags.numa_node = dd->numa_node; |
| 3985 | dd->tags.flags = BLK_MQ_F_SHOULD_MERGE; |
| 3986 | dd->tags.driver_data = dd; |
Asai Thambi SP | abb0ccd | 2016-02-24 21:21:13 -0800 | [diff] [blame] | 3987 | dd->tags.timeout = MTIP_NCQ_CMD_TIMEOUT_MS; |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3988 | |
| 3989 | rv = blk_mq_alloc_tag_set(&dd->tags); |
| 3990 | if (rv) { |
| 3991 | dev_err(&dd->pdev->dev, |
| 3992 | "Unable to allocate request queue\n"); |
Asai Thambi SP | 008e56d | 2016-02-24 21:21:20 -0800 | [diff] [blame] | 3993 | goto block_queue_alloc_tag_error; |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3994 | } |
| 3995 | |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 3996 | /* Allocate the request queue. */ |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 3997 | dd->queue = blk_mq_init_queue(&dd->tags); |
Dan Carpenter | a8a642c | 2014-05-14 15:54:18 +0300 | [diff] [blame] | 3998 | if (IS_ERR(dd->queue)) { |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 3999 | dev_err(&dd->pdev->dev, |
| 4000 | "Unable to allocate request queue\n"); |
| 4001 | rv = -ENOMEM; |
| 4002 | goto block_queue_alloc_init_error; |
| 4003 | } |
| 4004 | |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 4005 | dd->disk->queue = dd->queue; |
| 4006 | dd->queue->queuedata = dd; |
| 4007 | |
Asai Thambi SP | 59cf70e | 2016-02-24 21:17:47 -0800 | [diff] [blame] | 4008 | skip_create_disk: |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 4009 | /* Initialize the protocol layer. */ |
| 4010 | wait_for_rebuild = mtip_hw_get_identify(dd); |
| 4011 | if (wait_for_rebuild < 0) { |
| 4012 | dev_err(&dd->pdev->dev, |
| 4013 | "Protocol layer initialization failed\n"); |
| 4014 | rv = -EINVAL; |
| 4015 | goto init_hw_cmds_error; |
| 4016 | } |
| 4017 | |
| 4018 | /* |
| 4019 | * if rebuild pending, start the service thread, and delay the block |
Dan Williams | 0d52c756 | 2016-06-15 19:44:20 -0700 | [diff] [blame] | 4020 | * queue creation and device_add_disk() |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 4021 | */ |
| 4022 | if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC) |
| 4023 | goto start_service_thread; |
| 4024 | |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 4025 | /* Set device limits. */ |
| 4026 | set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags); |
Mike Snitzer | b277da0 | 2014-10-04 10:55:32 -0600 | [diff] [blame] | 4027 | clear_bit(QUEUE_FLAG_ADD_RANDOM, &dd->queue->queue_flags); |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 4028 | blk_queue_max_segments(dd->queue, MTIP_MAX_SG); |
| 4029 | blk_queue_physical_block_size(dd->queue, 4096); |
Asai Thambi S P | 6c8ab69 | 2012-05-29 18:42:51 -0700 | [diff] [blame] | 4030 | blk_queue_max_hw_sectors(dd->queue, 0xffff); |
| 4031 | blk_queue_max_segment_size(dd->queue, 0x400000); |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 4032 | blk_queue_io_min(dd->queue, 4096); |
Felipe Franciosi | 1044b1b | 2014-03-13 14:34:20 +0000 | [diff] [blame] | 4033 | blk_queue_bounce_limit(dd->queue, dd->pdev->dma_mask); |
Asai Thambi S P | 6c8ab69 | 2012-05-29 18:42:51 -0700 | [diff] [blame] | 4034 | |
Asai Thambi S P | 1528346 | 2013-01-11 14:41:34 +0100 | [diff] [blame] | 4035 | /* Signal trim support */ |
| 4036 | if (dd->trim_supp == true) { |
| 4037 | set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags); |
| 4038 | dd->queue->limits.discard_granularity = 4096; |
| 4039 | blk_queue_max_discard_sectors(dd->queue, |
| 4040 | MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES); |
Asai Thambi S P | 1528346 | 2013-01-11 14:41:34 +0100 | [diff] [blame] | 4041 | } |
| 4042 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4043 | /* Set the capacity of the device in 512 byte sectors. */ |
| 4044 | if (!(mtip_hw_get_capacity(dd, &capacity))) { |
| 4045 | dev_warn(&dd->pdev->dev, |
| 4046 | "Could not read drive capacity\n"); |
| 4047 | rv = -EIO; |
| 4048 | goto read_capacity_error; |
| 4049 | } |
| 4050 | set_capacity(dd->disk, capacity); |
| 4051 | |
| 4052 | /* Enable the block device and add it to /dev */ |
Dan Williams | 0d52c756 | 2016-06-15 19:44:20 -0700 | [diff] [blame] | 4053 | device_add_disk(&dd->pdev->dev, dd->disk); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4054 | |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 4055 | dd->bdev = bdget_disk(dd->disk, 0); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4056 | /* |
| 4057 | * Now that the disk is active, initialize any sysfs attributes |
| 4058 | * managed by the protocol layer. |
| 4059 | */ |
| 4060 | kobj = kobject_get(&disk_to_dev(dd->disk)->kobj); |
| 4061 | if (kobj) { |
| 4062 | mtip_hw_sysfs_init(dd, kobj); |
| 4063 | kobject_put(kobj); |
| 4064 | } |
| 4065 | |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 4066 | if (dd->mtip_svc_handler) { |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 4067 | set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag); |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 4068 | return rv; /* service thread created for handling rebuild */ |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 4069 | } |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 4070 | |
| 4071 | start_service_thread: |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 4072 | dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread, |
Rasmus Villemoes | 8aeea03 | 2015-11-20 10:46:49 +0100 | [diff] [blame] | 4073 | dd, dd->numa_node, |
| 4074 | "mtip_svc_thd_%02d", index); |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 4075 | |
| 4076 | if (IS_ERR(dd->mtip_svc_handler)) { |
Asai Thambi S P | c74b0f5 | 2012-04-09 08:35:39 +0200 | [diff] [blame] | 4077 | dev_err(&dd->pdev->dev, "service thread failed to start\n"); |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 4078 | dd->mtip_svc_handler = NULL; |
| 4079 | rv = -EFAULT; |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 4080 | goto kthread_run_error; |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 4081 | } |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 4082 | wake_up_process(dd->mtip_svc_handler); |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 4083 | if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC) |
| 4084 | rv = wait_for_rebuild; |
| 4085 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4086 | return rv; |
| 4087 | |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 4088 | kthread_run_error: |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 4089 | bdput(dd->bdev); |
| 4090 | dd->bdev = NULL; |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 4091 | |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 4092 | /* Delete our gendisk. This also removes the device from /dev */ |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4093 | del_gendisk(dd->disk); |
| 4094 | |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 4095 | read_capacity_error: |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 4096 | init_hw_cmds_error: |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 4097 | blk_cleanup_queue(dd->queue); |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 4098 | block_queue_alloc_init_error: |
Asai Thambi SP | 008e56d | 2016-02-24 21:21:20 -0800 | [diff] [blame] | 4099 | blk_mq_free_tag_set(&dd->tags); |
| 4100 | block_queue_alloc_tag_error: |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 4101 | mtip_hw_debugfs_exit(dd); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4102 | disk_index_error: |
| 4103 | spin_lock(&rssd_index_lock); |
| 4104 | ida_remove(&rssd_index_ida, index); |
| 4105 | spin_unlock(&rssd_index_lock); |
| 4106 | |
| 4107 | ida_get_error: |
| 4108 | put_disk(dd->disk); |
| 4109 | |
| 4110 | alloc_disk_error: |
Asai Thambi S P | 62ee8c1 | 2012-01-04 22:01:32 +0100 | [diff] [blame] | 4111 | mtip_hw_exit(dd); /* De-initialize the protocol layer. */ |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4112 | |
| 4113 | protocol_init_error: |
| 4114 | return rv; |
| 4115 | } |
| 4116 | |
Asai Thambi SP | 008e56d | 2016-02-24 21:21:20 -0800 | [diff] [blame] | 4117 | static void mtip_no_dev_cleanup(struct request *rq, void *data, bool reserv) |
| 4118 | { |
| 4119 | struct driver_data *dd = (struct driver_data *)data; |
| 4120 | struct mtip_cmd *cmd; |
| 4121 | |
Christoph Hellwig | 4dda473 | 2017-04-20 16:03:07 +0200 | [diff] [blame] | 4122 | if (likely(!reserv)) { |
Jens Axboe | 95c55ff | 2017-04-21 08:46:44 -0600 | [diff] [blame] | 4123 | cmd = blk_mq_rq_to_pdu(rq); |
Christoph Hellwig | 4dda473 | 2017-04-20 16:03:07 +0200 | [diff] [blame] | 4124 | cmd->status = -ENODEV; |
Christoph Hellwig | 08e0029 | 2017-04-20 16:03:09 +0200 | [diff] [blame] | 4125 | blk_mq_complete_request(rq); |
Christoph Hellwig | 4dda473 | 2017-04-20 16:03:07 +0200 | [diff] [blame] | 4126 | } else if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &dd->port->flags)) { |
Asai Thambi SP | 008e56d | 2016-02-24 21:21:20 -0800 | [diff] [blame] | 4127 | |
| 4128 | cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL); |
| 4129 | if (cmd->comp_func) |
| 4130 | cmd->comp_func(dd->port, MTIP_TAG_INTERNAL, |
| 4131 | cmd, -ENODEV); |
| 4132 | } |
| 4133 | } |
| 4134 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4135 | /* |
| 4136 | * Block layer deinitialization function. |
| 4137 | * |
| 4138 | * Called by the PCI layer as each P320 device is removed. |
| 4139 | * |
| 4140 | * @dd Pointer to the driver data structure. |
| 4141 | * |
| 4142 | * return value |
| 4143 | * 0 |
| 4144 | */ |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 4145 | static int mtip_block_remove(struct driver_data *dd) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4146 | { |
| 4147 | struct kobject *kobj; |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 4148 | |
Asai Thambi SP | 2132a54 | 2015-05-11 15:53:18 -0700 | [diff] [blame] | 4149 | mtip_hw_debugfs_exit(dd); |
Asai Thambi S P | 60ec0ee | 2011-11-23 08:29:24 +0100 | [diff] [blame] | 4150 | |
Asai Thambi SP | 2132a54 | 2015-05-11 15:53:18 -0700 | [diff] [blame] | 4151 | if (dd->mtip_svc_handler) { |
| 4152 | set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags); |
| 4153 | wake_up_interruptible(&dd->port->svc_wait); |
| 4154 | kthread_stop(dd->mtip_svc_handler); |
| 4155 | } |
| 4156 | |
| 4157 | /* Clean up the sysfs attributes, if created */ |
| 4158 | if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) { |
| 4159 | kobj = kobject_get(&disk_to_dev(dd->disk)->kobj); |
| 4160 | if (kobj) { |
| 4161 | mtip_hw_sysfs_exit(dd, kobj); |
| 4162 | kobject_put(kobj); |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 4163 | } |
Asai Thambi SP | 2132a54 | 2015-05-11 15:53:18 -0700 | [diff] [blame] | 4164 | } |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 4165 | |
Asai Thambi SP | 008e56d | 2016-02-24 21:21:20 -0800 | [diff] [blame] | 4166 | if (!dd->sr) { |
| 4167 | /* |
| 4168 | * Explicitly wait here for IOs to quiesce, |
| 4169 | * as mtip_standby_drive usually won't wait for IOs. |
| 4170 | */ |
| 4171 | if (!mtip_quiesce_io(dd->port, MTIP_QUIESCE_IO_TIMEOUT_MS, |
| 4172 | GFP_KERNEL)) |
| 4173 | mtip_standby_drive(dd); |
| 4174 | } |
Asai Thambi SP | 2132a54 | 2015-05-11 15:53:18 -0700 | [diff] [blame] | 4175 | else |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 4176 | dev_info(&dd->pdev->dev, "device %s surprise removal\n", |
| 4177 | dd->disk->disk_name); |
Asai Thambi SP | 2132a54 | 2015-05-11 15:53:18 -0700 | [diff] [blame] | 4178 | |
Ming Lei | 1671d52 | 2017-03-27 20:06:57 +0800 | [diff] [blame] | 4179 | blk_freeze_queue_start(dd->queue); |
Asai Thambi SP | 008e56d | 2016-02-24 21:21:20 -0800 | [diff] [blame] | 4180 | blk_mq_stop_hw_queues(dd->queue); |
Keith Busch | 6d125de | 2016-03-10 13:58:48 +0200 | [diff] [blame] | 4181 | blk_mq_tagset_busy_iter(&dd->tags, mtip_no_dev_cleanup, dd); |
Asai Thambi SP | 008e56d | 2016-02-24 21:21:20 -0800 | [diff] [blame] | 4182 | |
Asai Thambi SP | 2132a54 | 2015-05-11 15:53:18 -0700 | [diff] [blame] | 4183 | /* |
| 4184 | * Delete our gendisk structure. This also removes the device |
| 4185 | * from /dev |
| 4186 | */ |
| 4187 | if (dd->bdev) { |
| 4188 | bdput(dd->bdev); |
| 4189 | dd->bdev = NULL; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4190 | } |
Asai Thambi SP | 2132a54 | 2015-05-11 15:53:18 -0700 | [diff] [blame] | 4191 | if (dd->disk) { |
Asai Thambi SP | 59cf70e | 2016-02-24 21:17:47 -0800 | [diff] [blame] | 4192 | if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) |
| 4193 | del_gendisk(dd->disk); |
Asai Thambi SP | 2132a54 | 2015-05-11 15:53:18 -0700 | [diff] [blame] | 4194 | if (dd->disk->queue) { |
| 4195 | blk_cleanup_queue(dd->queue); |
| 4196 | blk_mq_free_tag_set(&dd->tags); |
| 4197 | dd->queue = NULL; |
| 4198 | } |
| 4199 | put_disk(dd->disk); |
| 4200 | } |
| 4201 | dd->disk = NULL; |
| 4202 | |
| 4203 | spin_lock(&rssd_index_lock); |
| 4204 | ida_remove(&rssd_index_ida, dd->index); |
| 4205 | spin_unlock(&rssd_index_lock); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4206 | |
| 4207 | /* De-initialize the protocol layer. */ |
| 4208 | mtip_hw_exit(dd); |
| 4209 | |
| 4210 | return 0; |
| 4211 | } |
| 4212 | |
| 4213 | /* |
| 4214 | * Function called by the PCI layer when just before the |
| 4215 | * machine shuts down. |
| 4216 | * |
| 4217 | * If a protocol layer shutdown function is present it will be called |
| 4218 | * by this function. |
| 4219 | * |
| 4220 | * @dd Pointer to the driver data structure. |
| 4221 | * |
| 4222 | * return value |
| 4223 | * 0 |
| 4224 | */ |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 4225 | static int mtip_block_shutdown(struct driver_data *dd) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4226 | { |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 4227 | mtip_hw_shutdown(dd); |
| 4228 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4229 | /* Delete our gendisk structure, and cleanup the blk queue. */ |
Asai Thambi S P | 58c49df | 2013-01-11 18:47:12 +0530 | [diff] [blame] | 4230 | if (dd->disk) { |
Asai Thambi S P | 5a79e1a | 2013-04-12 23:57:17 +0530 | [diff] [blame] | 4231 | dev_info(&dd->pdev->dev, |
| 4232 | "Shutting down %s ...\n", dd->disk->disk_name); |
Asai Thambi S P | 58c49df | 2013-01-11 18:47:12 +0530 | [diff] [blame] | 4233 | |
Asai Thambi SP | 59cf70e | 2016-02-24 21:17:47 -0800 | [diff] [blame] | 4234 | if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) |
| 4235 | del_gendisk(dd->disk); |
Asai Thambi S P | 5a79e1a | 2013-04-12 23:57:17 +0530 | [diff] [blame] | 4236 | if (dd->disk->queue) { |
Asai Thambi S P | 5a79e1a | 2013-04-12 23:57:17 +0530 | [diff] [blame] | 4237 | blk_cleanup_queue(dd->queue); |
Jens Axboe | ffc771b | 2014-05-09 09:42:02 -0600 | [diff] [blame] | 4238 | blk_mq_free_tag_set(&dd->tags); |
Asai Thambi SP | 02b4826 | 2015-05-11 15:48:00 -0700 | [diff] [blame] | 4239 | } |
| 4240 | put_disk(dd->disk); |
Asai Thambi S P | 5a79e1a | 2013-04-12 23:57:17 +0530 | [diff] [blame] | 4241 | dd->disk = NULL; |
| 4242 | dd->queue = NULL; |
| 4243 | } |
Asai Thambi S P | 8182b49 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 4244 | |
| 4245 | spin_lock(&rssd_index_lock); |
| 4246 | ida_remove(&rssd_index_ida, dd->index); |
| 4247 | spin_unlock(&rssd_index_lock); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4248 | return 0; |
| 4249 | } |
| 4250 | |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 4251 | static int mtip_block_suspend(struct driver_data *dd) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4252 | { |
| 4253 | dev_info(&dd->pdev->dev, |
| 4254 | "Suspending %s ...\n", dd->disk->disk_name); |
| 4255 | mtip_hw_suspend(dd); |
| 4256 | return 0; |
| 4257 | } |
| 4258 | |
Jens Axboe | 6316668 | 2011-09-27 21:27:43 -0600 | [diff] [blame] | 4259 | static int mtip_block_resume(struct driver_data *dd) |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4260 | { |
| 4261 | dev_info(&dd->pdev->dev, "Resuming %s ...\n", |
| 4262 | dd->disk->disk_name); |
| 4263 | mtip_hw_resume(dd); |
| 4264 | return 0; |
| 4265 | } |
| 4266 | |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 4267 | static void drop_cpu(int cpu) |
| 4268 | { |
| 4269 | cpu_use[cpu]--; |
| 4270 | } |
| 4271 | |
| 4272 | static int get_least_used_cpu_on_node(int node) |
| 4273 | { |
| 4274 | int cpu, least_used_cpu, least_cnt; |
| 4275 | const struct cpumask *node_mask; |
| 4276 | |
| 4277 | node_mask = cpumask_of_node(node); |
| 4278 | least_used_cpu = cpumask_first(node_mask); |
| 4279 | least_cnt = cpu_use[least_used_cpu]; |
| 4280 | cpu = least_used_cpu; |
| 4281 | |
| 4282 | for_each_cpu(cpu, node_mask) { |
| 4283 | if (cpu_use[cpu] < least_cnt) { |
| 4284 | least_used_cpu = cpu; |
| 4285 | least_cnt = cpu_use[cpu]; |
| 4286 | } |
| 4287 | } |
| 4288 | cpu_use[least_used_cpu]++; |
| 4289 | return least_used_cpu; |
| 4290 | } |
| 4291 | |
| 4292 | /* Helper for selecting a node in round robin mode */ |
| 4293 | static inline int mtip_get_next_rr_node(void) |
| 4294 | { |
| 4295 | static int next_node = -1; |
| 4296 | |
| 4297 | if (next_node == -1) { |
| 4298 | next_node = first_online_node; |
| 4299 | return next_node; |
| 4300 | } |
| 4301 | |
| 4302 | next_node = next_online_node(next_node); |
| 4303 | if (next_node == MAX_NUMNODES) |
| 4304 | next_node = first_online_node; |
| 4305 | return next_node; |
| 4306 | } |
| 4307 | |
Fengguang Wu | 25bac12 | 2013-01-12 15:31:40 +0800 | [diff] [blame] | 4308 | static DEFINE_HANDLER(0); |
| 4309 | static DEFINE_HANDLER(1); |
| 4310 | static DEFINE_HANDLER(2); |
| 4311 | static DEFINE_HANDLER(3); |
| 4312 | static DEFINE_HANDLER(4); |
| 4313 | static DEFINE_HANDLER(5); |
| 4314 | static DEFINE_HANDLER(6); |
| 4315 | static DEFINE_HANDLER(7); |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 4316 | |
Asai Thambi S P | d1e714d | 2014-03-13 18:45:15 -0700 | [diff] [blame] | 4317 | static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev) |
| 4318 | { |
| 4319 | int pos; |
| 4320 | unsigned short pcie_dev_ctrl; |
| 4321 | |
| 4322 | pos = pci_find_capability(pdev, PCI_CAP_ID_EXP); |
| 4323 | if (pos) { |
| 4324 | pci_read_config_word(pdev, |
| 4325 | pos + PCI_EXP_DEVCTL, |
| 4326 | &pcie_dev_ctrl); |
| 4327 | if (pcie_dev_ctrl & (1 << 11) || |
| 4328 | pcie_dev_ctrl & (1 << 4)) { |
| 4329 | dev_info(&dd->pdev->dev, |
| 4330 | "Disabling ERO/No-Snoop on bridge device %04x:%04x\n", |
| 4331 | pdev->vendor, pdev->device); |
| 4332 | pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN | |
| 4333 | PCI_EXP_DEVCTL_RELAX_EN); |
| 4334 | pci_write_config_word(pdev, |
| 4335 | pos + PCI_EXP_DEVCTL, |
| 4336 | pcie_dev_ctrl); |
| 4337 | } |
| 4338 | } |
| 4339 | } |
| 4340 | |
| 4341 | static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev) |
| 4342 | { |
| 4343 | /* |
| 4344 | * This workaround is specific to AMD/ATI chipset with a PCI upstream |
| 4345 | * device with device id 0x5aXX |
| 4346 | */ |
| 4347 | if (pdev->bus && pdev->bus->self) { |
| 4348 | if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI && |
| 4349 | ((pdev->bus->self->device & 0xff00) == 0x5a00)) { |
| 4350 | mtip_disable_link_opts(dd, pdev->bus->self); |
| 4351 | } else { |
| 4352 | /* Check further up the topology */ |
| 4353 | struct pci_dev *parent_dev = pdev->bus->self; |
| 4354 | if (parent_dev->bus && |
| 4355 | parent_dev->bus->parent && |
| 4356 | parent_dev->bus->parent->self && |
| 4357 | parent_dev->bus->parent->self->vendor == |
| 4358 | PCI_VENDOR_ID_ATI && |
| 4359 | (parent_dev->bus->parent->self->device & |
| 4360 | 0xff00) == 0x5a00) { |
| 4361 | mtip_disable_link_opts(dd, |
| 4362 | parent_dev->bus->parent->self); |
| 4363 | } |
| 4364 | } |
| 4365 | } |
| 4366 | } |
| 4367 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4368 | /* |
| 4369 | * Called for each supported PCI device detected. |
| 4370 | * |
| 4371 | * This function allocates the private data structure, enables the |
| 4372 | * PCI device and then calls the block layer initialization function. |
| 4373 | * |
| 4374 | * return value |
| 4375 | * 0 on success else an error code. |
| 4376 | */ |
| 4377 | static int mtip_pci_probe(struct pci_dev *pdev, |
| 4378 | const struct pci_device_id *ent) |
| 4379 | { |
| 4380 | int rv = 0; |
| 4381 | struct driver_data *dd = NULL; |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 4382 | char cpu_list[256]; |
| 4383 | const struct cpumask *node_mask; |
| 4384 | int cpu, i = 0, j = 0; |
| 4385 | int my_node = NUMA_NO_NODE; |
Asai Thambi S P | 0caff00 | 2013-04-03 19:56:21 +0530 | [diff] [blame] | 4386 | unsigned long flags; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4387 | |
| 4388 | /* Allocate memory for this devices private data. */ |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 4389 | my_node = pcibus_to_node(pdev->bus); |
| 4390 | if (my_node != NUMA_NO_NODE) { |
| 4391 | if (!node_online(my_node)) |
| 4392 | my_node = mtip_get_next_rr_node(); |
| 4393 | } else { |
| 4394 | dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n"); |
| 4395 | my_node = mtip_get_next_rr_node(); |
| 4396 | } |
| 4397 | dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n", |
| 4398 | my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev), |
Jens Axboe | 7f32890 | 2014-03-10 14:29:37 -0600 | [diff] [blame] | 4399 | cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id()); |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 4400 | |
| 4401 | dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4402 | if (dd == NULL) { |
| 4403 | dev_err(&pdev->dev, |
| 4404 | "Unable to allocate memory for driver data\n"); |
| 4405 | return -ENOMEM; |
| 4406 | } |
| 4407 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4408 | /* Attach the private data to this PCI device. */ |
| 4409 | pci_set_drvdata(pdev, dd); |
| 4410 | |
| 4411 | rv = pcim_enable_device(pdev); |
| 4412 | if (rv < 0) { |
| 4413 | dev_err(&pdev->dev, "Unable to enable device\n"); |
| 4414 | goto iomap_err; |
| 4415 | } |
| 4416 | |
| 4417 | /* Map BAR5 to memory. */ |
| 4418 | rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME); |
| 4419 | if (rv < 0) { |
| 4420 | dev_err(&pdev->dev, "Unable to map regions\n"); |
| 4421 | goto iomap_err; |
| 4422 | } |
| 4423 | |
| 4424 | if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) { |
| 4425 | rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64)); |
| 4426 | |
| 4427 | if (rv) { |
| 4428 | rv = pci_set_consistent_dma_mask(pdev, |
| 4429 | DMA_BIT_MASK(32)); |
| 4430 | if (rv) { |
| 4431 | dev_warn(&pdev->dev, |
| 4432 | "64-bit DMA enable failed\n"); |
| 4433 | goto setmask_err; |
| 4434 | } |
| 4435 | } |
| 4436 | } |
| 4437 | |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 4438 | /* Copy the info we may need later into the private data structure. */ |
| 4439 | dd->major = mtip_major; |
| 4440 | dd->instance = instance; |
| 4441 | dd->pdev = pdev; |
| 4442 | dd->numa_node = my_node; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4443 | |
Asai Thambi S P | 0caff00 | 2013-04-03 19:56:21 +0530 | [diff] [blame] | 4444 | INIT_LIST_HEAD(&dd->online_list); |
| 4445 | INIT_LIST_HEAD(&dd->remove_list); |
| 4446 | |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 4447 | memset(dd->workq_name, 0, 32); |
| 4448 | snprintf(dd->workq_name, 31, "mtipq%d", dd->instance); |
| 4449 | |
| 4450 | dd->isr_workq = create_workqueue(dd->workq_name); |
| 4451 | if (!dd->isr_workq) { |
| 4452 | dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance); |
Wei Yongjun | d137c83 | 2013-03-22 08:58:23 -0600 | [diff] [blame] | 4453 | rv = -ENOMEM; |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 4454 | goto block_initialize_err; |
| 4455 | } |
| 4456 | |
| 4457 | memset(cpu_list, 0, sizeof(cpu_list)); |
| 4458 | |
| 4459 | node_mask = cpumask_of_node(dd->numa_node); |
| 4460 | if (!cpumask_empty(node_mask)) { |
| 4461 | for_each_cpu(cpu, node_mask) |
| 4462 | { |
| 4463 | snprintf(&cpu_list[j], 256 - j, "%d ", cpu); |
| 4464 | j = strlen(cpu_list); |
| 4465 | } |
| 4466 | |
| 4467 | dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n", |
| 4468 | dd->numa_node, |
| 4469 | topology_physical_package_id(cpumask_first(node_mask)), |
| 4470 | nr_cpus_node(dd->numa_node), |
| 4471 | cpu_list); |
| 4472 | } else |
| 4473 | dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n"); |
| 4474 | |
| 4475 | dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node); |
| 4476 | dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n", |
| 4477 | cpu_to_node(dd->isr_binding), dd->isr_binding); |
| 4478 | |
| 4479 | /* first worker context always runs in ISR */ |
| 4480 | dd->work[0].cpu_binding = dd->isr_binding; |
| 4481 | dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node); |
| 4482 | dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node); |
| 4483 | dd->work[3].cpu_binding = dd->work[0].cpu_binding; |
| 4484 | dd->work[4].cpu_binding = dd->work[1].cpu_binding; |
| 4485 | dd->work[5].cpu_binding = dd->work[2].cpu_binding; |
| 4486 | dd->work[6].cpu_binding = dd->work[2].cpu_binding; |
| 4487 | dd->work[7].cpu_binding = dd->work[1].cpu_binding; |
| 4488 | |
| 4489 | /* Log the bindings */ |
| 4490 | for_each_present_cpu(cpu) { |
| 4491 | memset(cpu_list, 0, sizeof(cpu_list)); |
| 4492 | for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) { |
| 4493 | if (dd->work[i].cpu_binding == cpu) { |
| 4494 | snprintf(&cpu_list[j], 256 - j, "%d ", i); |
| 4495 | j = strlen(cpu_list); |
| 4496 | } |
| 4497 | } |
| 4498 | if (j) |
| 4499 | dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list); |
| 4500 | } |
| 4501 | |
| 4502 | INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0); |
| 4503 | INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1); |
| 4504 | INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2); |
| 4505 | INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3); |
| 4506 | INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4); |
| 4507 | INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5); |
| 4508 | INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6); |
| 4509 | INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7); |
| 4510 | |
| 4511 | pci_set_master(pdev); |
Wei Yongjun | d137c83 | 2013-03-22 08:58:23 -0600 | [diff] [blame] | 4512 | rv = pci_enable_msi(pdev); |
| 4513 | if (rv) { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4514 | dev_warn(&pdev->dev, |
| 4515 | "Unable to enable MSI interrupt.\n"); |
Alexander Gordeev | cf91f39 | 2014-02-19 09:58:15 +0100 | [diff] [blame] | 4516 | goto msi_initialize_err; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4517 | } |
| 4518 | |
Asai Thambi S P | d1e714d | 2014-03-13 18:45:15 -0700 | [diff] [blame] | 4519 | mtip_fix_ero_nosnoop(dd, pdev); |
| 4520 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4521 | /* Initialize the block layer. */ |
| 4522 | rv = mtip_block_initialize(dd); |
| 4523 | if (rv < 0) { |
| 4524 | dev_err(&pdev->dev, |
| 4525 | "Unable to initialize block layer\n"); |
| 4526 | goto block_initialize_err; |
| 4527 | } |
| 4528 | |
| 4529 | /* |
| 4530 | * Increment the instance count so that each device has a unique |
| 4531 | * instance number. |
| 4532 | */ |
| 4533 | instance++; |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 4534 | if (rv != MTIP_FTL_REBUILD_MAGIC) |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 4535 | set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag); |
Asai Thambi S P | 6b06d35 | 2013-04-03 19:54:35 +0530 | [diff] [blame] | 4536 | else |
| 4537 | rv = 0; /* device in rebuild state, return 0 from probe */ |
Asai Thambi S P | 0caff00 | 2013-04-03 19:56:21 +0530 | [diff] [blame] | 4538 | |
| 4539 | /* Add to online list even if in ftl rebuild */ |
| 4540 | spin_lock_irqsave(&dev_lock, flags); |
| 4541 | list_add(&dd->online_list, &online_list); |
| 4542 | spin_unlock_irqrestore(&dev_lock, flags); |
| 4543 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4544 | goto done; |
| 4545 | |
| 4546 | block_initialize_err: |
| 4547 | pci_disable_msi(pdev); |
Alexander Gordeev | cf91f39 | 2014-02-19 09:58:15 +0100 | [diff] [blame] | 4548 | |
| 4549 | msi_initialize_err: |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 4550 | if (dd->isr_workq) { |
| 4551 | flush_workqueue(dd->isr_workq); |
| 4552 | destroy_workqueue(dd->isr_workq); |
| 4553 | drop_cpu(dd->work[0].cpu_binding); |
| 4554 | drop_cpu(dd->work[1].cpu_binding); |
| 4555 | drop_cpu(dd->work[2].cpu_binding); |
| 4556 | } |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4557 | setmask_err: |
| 4558 | pcim_iounmap_regions(pdev, 1 << MTIP_ABAR); |
| 4559 | |
| 4560 | iomap_err: |
| 4561 | kfree(dd); |
| 4562 | pci_set_drvdata(pdev, NULL); |
| 4563 | return rv; |
| 4564 | done: |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4565 | return rv; |
| 4566 | } |
| 4567 | |
| 4568 | /* |
| 4569 | * Called for each probed device when the device is removed or the |
| 4570 | * driver is unloaded. |
| 4571 | * |
| 4572 | * return value |
| 4573 | * None |
| 4574 | */ |
| 4575 | static void mtip_pci_remove(struct pci_dev *pdev) |
| 4576 | { |
| 4577 | struct driver_data *dd = pci_get_drvdata(pdev); |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 4578 | unsigned long flags, to; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4579 | |
Asai Thambi SP | 51c6570 | 2016-02-24 21:18:10 -0800 | [diff] [blame] | 4580 | set_bit(MTIP_DDF_REMOVAL_BIT, &dd->dd_flag); |
Asai Thambi S P | 4503836 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 4581 | |
Asai Thambi S P | 0caff00 | 2013-04-03 19:56:21 +0530 | [diff] [blame] | 4582 | spin_lock_irqsave(&dev_lock, flags); |
| 4583 | list_del_init(&dd->online_list); |
| 4584 | list_add(&dd->remove_list, &removing_list); |
| 4585 | spin_unlock_irqrestore(&dev_lock, flags); |
| 4586 | |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 4587 | mtip_check_surprise_removal(pdev); |
| 4588 | synchronize_irq(dd->pdev->irq); |
| 4589 | |
| 4590 | /* Spin until workers are done */ |
| 4591 | to = jiffies + msecs_to_jiffies(4000); |
| 4592 | do { |
| 4593 | msleep(20); |
| 4594 | } while (atomic_read(&dd->irq_workers_active) != 0 && |
| 4595 | time_before(jiffies, to)); |
| 4596 | |
Asai Thambi SP | 008e56d | 2016-02-24 21:21:20 -0800 | [diff] [blame] | 4597 | if (!dd->sr) |
| 4598 | fsync_bdev(dd->bdev); |
Asai Thambi SP | 51c6570 | 2016-02-24 21:18:10 -0800 | [diff] [blame] | 4599 | |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 4600 | if (atomic_read(&dd->irq_workers_active) != 0) { |
| 4601 | dev_warn(&dd->pdev->dev, |
| 4602 | "Completion workers still active!\n"); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4603 | } |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4604 | |
Asai Thambi SP | 008e56d | 2016-02-24 21:21:20 -0800 | [diff] [blame] | 4605 | blk_set_queue_dying(dd->queue); |
Asai Thambi SP | 51c6570 | 2016-02-24 21:18:10 -0800 | [diff] [blame] | 4606 | set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag); |
| 4607 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4608 | /* Clean up the block layer. */ |
| 4609 | mtip_block_remove(dd); |
| 4610 | |
Asai Thambi S P | 16c906e5 | 2012-12-20 07:46:25 -0800 | [diff] [blame] | 4611 | if (dd->isr_workq) { |
| 4612 | flush_workqueue(dd->isr_workq); |
| 4613 | destroy_workqueue(dd->isr_workq); |
| 4614 | drop_cpu(dd->work[0].cpu_binding); |
| 4615 | drop_cpu(dd->work[1].cpu_binding); |
| 4616 | drop_cpu(dd->work[2].cpu_binding); |
| 4617 | } |
| 4618 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4619 | pci_disable_msi(pdev); |
| 4620 | |
Asai Thambi S P | 0caff00 | 2013-04-03 19:56:21 +0530 | [diff] [blame] | 4621 | spin_lock_irqsave(&dev_lock, flags); |
| 4622 | list_del_init(&dd->remove_list); |
| 4623 | spin_unlock_irqrestore(&dev_lock, flags); |
| 4624 | |
Asai Thambi SP | 2132a54 | 2015-05-11 15:53:18 -0700 | [diff] [blame] | 4625 | kfree(dd); |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 4626 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4627 | pcim_iounmap_regions(pdev, 1 << MTIP_ABAR); |
Asai Thambi S P | 8f8b899 | 2013-09-11 13:14:42 -0600 | [diff] [blame] | 4628 | pci_set_drvdata(pdev, NULL); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | /* |
| 4632 | * Called for each probed device when the device is suspended. |
| 4633 | * |
| 4634 | * return value |
| 4635 | * 0 Success |
| 4636 | * <0 Error |
| 4637 | */ |
| 4638 | static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg) |
| 4639 | { |
| 4640 | int rv = 0; |
| 4641 | struct driver_data *dd = pci_get_drvdata(pdev); |
| 4642 | |
| 4643 | if (!dd) { |
| 4644 | dev_err(&pdev->dev, |
| 4645 | "Driver private datastructure is NULL\n"); |
| 4646 | return -EFAULT; |
| 4647 | } |
| 4648 | |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 4649 | set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4650 | |
| 4651 | /* Disable ports & interrupts then send standby immediate */ |
| 4652 | rv = mtip_block_suspend(dd); |
| 4653 | if (rv < 0) { |
| 4654 | dev_err(&pdev->dev, |
| 4655 | "Failed to suspend controller\n"); |
| 4656 | return rv; |
| 4657 | } |
| 4658 | |
| 4659 | /* |
| 4660 | * Save the pci config space to pdev structure & |
| 4661 | * disable the device |
| 4662 | */ |
| 4663 | pci_save_state(pdev); |
| 4664 | pci_disable_device(pdev); |
| 4665 | |
| 4666 | /* Move to Low power state*/ |
| 4667 | pci_set_power_state(pdev, PCI_D3hot); |
| 4668 | |
| 4669 | return rv; |
| 4670 | } |
| 4671 | |
| 4672 | /* |
| 4673 | * Called for each probed device when the device is resumed. |
| 4674 | * |
| 4675 | * return value |
| 4676 | * 0 Success |
| 4677 | * <0 Error |
| 4678 | */ |
| 4679 | static int mtip_pci_resume(struct pci_dev *pdev) |
| 4680 | { |
| 4681 | int rv = 0; |
| 4682 | struct driver_data *dd; |
| 4683 | |
| 4684 | dd = pci_get_drvdata(pdev); |
| 4685 | if (!dd) { |
| 4686 | dev_err(&pdev->dev, |
| 4687 | "Driver private datastructure is NULL\n"); |
| 4688 | return -EFAULT; |
| 4689 | } |
| 4690 | |
| 4691 | /* Move the device to active State */ |
| 4692 | pci_set_power_state(pdev, PCI_D0); |
| 4693 | |
| 4694 | /* Restore PCI configuration space */ |
| 4695 | pci_restore_state(pdev); |
| 4696 | |
| 4697 | /* Enable the PCI device*/ |
| 4698 | rv = pcim_enable_device(pdev); |
| 4699 | if (rv < 0) { |
| 4700 | dev_err(&pdev->dev, |
| 4701 | "Failed to enable card during resume\n"); |
| 4702 | goto err; |
| 4703 | } |
| 4704 | pci_set_master(pdev); |
| 4705 | |
| 4706 | /* |
| 4707 | * Calls hbaReset, initPort, & startPort function |
| 4708 | * then enables interrupts |
| 4709 | */ |
| 4710 | rv = mtip_block_resume(dd); |
| 4711 | if (rv < 0) |
| 4712 | dev_err(&pdev->dev, "Unable to resume\n"); |
| 4713 | |
| 4714 | err: |
Asai Thambi S P | 8a857a8 | 2012-04-09 08:35:38 +0200 | [diff] [blame] | 4715 | clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4716 | |
| 4717 | return rv; |
| 4718 | } |
| 4719 | |
| 4720 | /* |
| 4721 | * Shutdown routine |
| 4722 | * |
| 4723 | * return value |
| 4724 | * None |
| 4725 | */ |
| 4726 | static void mtip_pci_shutdown(struct pci_dev *pdev) |
| 4727 | { |
| 4728 | struct driver_data *dd = pci_get_drvdata(pdev); |
| 4729 | if (dd) |
| 4730 | mtip_block_shutdown(dd); |
| 4731 | } |
| 4732 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4733 | /* Table of device ids supported by this driver. */ |
Benoit Taine | 9baa3c3 | 2014-08-08 15:56:03 +0200 | [diff] [blame] | 4734 | static const struct pci_device_id mtip_pci_tbl[] = { |
Asai Thambi S P | 1a13145 | 2012-09-05 22:00:38 +0530 | [diff] [blame] | 4735 | { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) }, |
| 4736 | { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) }, |
| 4737 | { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) }, |
| 4738 | { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) }, |
| 4739 | { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) }, |
| 4740 | { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) }, |
| 4741 | { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) }, |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4742 | { 0 } |
| 4743 | }; |
| 4744 | |
| 4745 | /* Structure that describes the PCI driver functions. */ |
Jens Axboe | 3ff147d | 2011-09-27 21:33:53 -0600 | [diff] [blame] | 4746 | static struct pci_driver mtip_pci_driver = { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4747 | .name = MTIP_DRV_NAME, |
| 4748 | .id_table = mtip_pci_tbl, |
| 4749 | .probe = mtip_pci_probe, |
| 4750 | .remove = mtip_pci_remove, |
| 4751 | .suspend = mtip_pci_suspend, |
| 4752 | .resume = mtip_pci_resume, |
| 4753 | .shutdown = mtip_pci_shutdown, |
| 4754 | }; |
| 4755 | |
| 4756 | MODULE_DEVICE_TABLE(pci, mtip_pci_tbl); |
| 4757 | |
| 4758 | /* |
| 4759 | * Module initialization function. |
| 4760 | * |
| 4761 | * Called once when the module is loaded. This function allocates a major |
| 4762 | * block device number to the Cyclone devices and registers the PCI layer |
| 4763 | * of the driver. |
| 4764 | * |
| 4765 | * Return value |
| 4766 | * 0 on success else error code. |
| 4767 | */ |
| 4768 | static int __init mtip_init(void) |
| 4769 | { |
Ryosuke Saito | 6d27f09 | 2012-04-05 08:09:34 -0600 | [diff] [blame] | 4770 | int error; |
| 4771 | |
Asai Thambi S P | 45422e7 | 2012-09-05 22:03:56 +0530 | [diff] [blame] | 4772 | pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n"); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4773 | |
Asai Thambi S P | 0caff00 | 2013-04-03 19:56:21 +0530 | [diff] [blame] | 4774 | spin_lock_init(&dev_lock); |
| 4775 | |
| 4776 | INIT_LIST_HEAD(&online_list); |
| 4777 | INIT_LIST_HEAD(&removing_list); |
| 4778 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4779 | /* Allocate a major block device number to use with this driver. */ |
Ryosuke Saito | 6d27f09 | 2012-04-05 08:09:34 -0600 | [diff] [blame] | 4780 | error = register_blkdev(0, MTIP_DRV_NAME); |
| 4781 | if (error <= 0) { |
Asai Thambi S P | 45422e7 | 2012-09-05 22:03:56 +0530 | [diff] [blame] | 4782 | pr_err("Unable to register block device (%d)\n", |
Ryosuke Saito | 6d27f09 | 2012-04-05 08:09:34 -0600 | [diff] [blame] | 4783 | error); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4784 | return -EBUSY; |
| 4785 | } |
Ryosuke Saito | 6d27f09 | 2012-04-05 08:09:34 -0600 | [diff] [blame] | 4786 | mtip_major = error; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4787 | |
Asai Thambi S P | 0caff00 | 2013-04-03 19:56:21 +0530 | [diff] [blame] | 4788 | dfs_parent = debugfs_create_dir("rssd", NULL); |
| 4789 | if (IS_ERR_OR_NULL(dfs_parent)) { |
| 4790 | pr_warn("Error creating debugfs parent\n"); |
| 4791 | dfs_parent = NULL; |
| 4792 | } |
| 4793 | if (dfs_parent) { |
| 4794 | dfs_device_status = debugfs_create_file("device_status", |
| 4795 | S_IRUGO, dfs_parent, NULL, |
| 4796 | &mtip_device_status_fops); |
| 4797 | if (IS_ERR_OR_NULL(dfs_device_status)) { |
| 4798 | pr_err("Error creating device_status node\n"); |
| 4799 | dfs_device_status = NULL; |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 4800 | } |
| 4801 | } |
| 4802 | |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4803 | /* Register our PCI operations. */ |
Ryosuke Saito | 6d27f09 | 2012-04-05 08:09:34 -0600 | [diff] [blame] | 4804 | error = pci_register_driver(&mtip_pci_driver); |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 4805 | if (error) { |
| 4806 | debugfs_remove(dfs_parent); |
Ryosuke Saito | 6d27f09 | 2012-04-05 08:09:34 -0600 | [diff] [blame] | 4807 | unregister_blkdev(mtip_major, MTIP_DRV_NAME); |
Asai Thambi S P | 7b421d2 | 2012-06-04 12:44:02 -0700 | [diff] [blame] | 4808 | } |
Ryosuke Saito | 6d27f09 | 2012-04-05 08:09:34 -0600 | [diff] [blame] | 4809 | |
| 4810 | return error; |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4811 | } |
| 4812 | |
| 4813 | /* |
| 4814 | * Module de-initialization function. |
| 4815 | * |
| 4816 | * Called once when the module is unloaded. This function deallocates |
| 4817 | * the major block device number allocated by mtip_init() and |
| 4818 | * unregisters the PCI layer of the driver. |
| 4819 | * |
| 4820 | * Return value |
| 4821 | * none |
| 4822 | */ |
| 4823 | static void __exit mtip_exit(void) |
| 4824 | { |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4825 | /* Release the allocated major block device number. */ |
| 4826 | unregister_blkdev(mtip_major, MTIP_DRV_NAME); |
| 4827 | |
| 4828 | /* Unregister the PCI driver. */ |
| 4829 | pci_unregister_driver(&mtip_pci_driver); |
Asai Thambi S P | af5ded8 | 2014-04-16 20:30:16 -0700 | [diff] [blame] | 4830 | |
| 4831 | debugfs_remove_recursive(dfs_parent); |
Sam Bradshaw | 88523a6 | 2011-08-30 08:34:26 -0600 | [diff] [blame] | 4832 | } |
| 4833 | |
| 4834 | MODULE_AUTHOR("Micron Technology, Inc"); |
| 4835 | MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver"); |
| 4836 | MODULE_LICENSE("GPL"); |
| 4837 | MODULE_VERSION(MTIP_DRV_VERSION); |
| 4838 | |
| 4839 | module_init(mtip_init); |
| 4840 | module_exit(mtip_exit); |