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