blob: be9ad9ca4fe165cf76eafa8d011f610989937bd8 [file] [log] [blame]
Sam Bradshaw88523a62011-08-30 08:34:26 -06001/*
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 Axboe0e838c62011-09-28 07:35:40 -060031#include <linux/module.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060032#include <linux/genhd.h>
33#include <linux/blkdev.h>
Jens Axboeffc771b2014-05-09 09:42:02 -060034#include <linux/blk-mq.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060035#include <linux/bio.h>
36#include <linux/dma-mapping.h>
37#include <linux/idr.h>
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +010038#include <linux/kthread.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060039#include <../drivers/ata/ahci.h>
Asai Thambi S P45038362012-04-09 08:35:38 +020040#include <linux/export.h>
Asai Thambi S P7b421d22012-06-04 12:44:02 -070041#include <linux/debugfs.h>
Sam Bradshawf45c40a2014-06-06 13:28:48 -060042#include <linux/prefetch.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060043#include "mtip32xx.h"
44
45#define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
Sam Bradshaw188b9f42014-01-15 10:14:57 -080046
47/* DMA region containing RX Fis, Identify, RLE10, and SMART buffers */
48#define AHCI_RX_FIS_SZ 0x100
49#define AHCI_RX_FIS_OFFSET 0x0
50#define AHCI_IDFY_SZ ATA_SECT_SIZE
51#define AHCI_IDFY_OFFSET 0x400
52#define AHCI_SECTBUF_SZ ATA_SECT_SIZE
53#define AHCI_SECTBUF_OFFSET 0x800
54#define AHCI_SMARTBUF_SZ ATA_SECT_SIZE
55#define AHCI_SMARTBUF_OFFSET 0xC00
56/* 0x100 + 0x200 + 0x200 + 0x200 is smaller than 4k but we pad it out */
57#define BLOCK_DMA_ALLOC_SZ 4096
58
59/* DMA region containing command table (should be 8192 bytes) */
60#define AHCI_CMD_SLOT_SZ sizeof(struct mtip_cmd_hdr)
61#define AHCI_CMD_TBL_SZ (MTIP_MAX_COMMAND_SLOTS * AHCI_CMD_SLOT_SZ)
62#define AHCI_CMD_TBL_OFFSET 0x0
63
64/* DMA region per command (contains header and SGL) */
65#define AHCI_CMD_TBL_HDR_SZ 0x80
66#define AHCI_CMD_TBL_HDR_OFFSET 0x0
67#define AHCI_CMD_TBL_SGL_SZ (MTIP_MAX_SG * sizeof(struct mtip_cmd_sg))
68#define AHCI_CMD_TBL_SGL_OFFSET AHCI_CMD_TBL_HDR_SZ
69#define CMD_DMA_ALLOC_SZ (AHCI_CMD_TBL_SGL_SZ + AHCI_CMD_TBL_HDR_SZ)
70
Sam Bradshaw88523a62011-08-30 08:34:26 -060071
Asai Thambi S P45038362012-04-09 08:35:38 +020072#define HOST_CAP_NZDMA (1 << 19)
Sam Bradshaw88523a62011-08-30 08:34:26 -060073#define HOST_HSORG 0xFC
74#define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
75#define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
76#define HSORG_HWREV 0xFF00
77#define HSORG_STYLE 0x8
78#define HSORG_SLOTGROUPS 0x7
79
80#define PORT_COMMAND_ISSUE 0x38
81#define PORT_SDBV 0x7C
82
83#define PORT_OFFSET 0x100
84#define PORT_MEM_SIZE 0x80
85
86#define PORT_IRQ_ERR \
87 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
88 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
89 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
90 PORT_IRQ_OVERFLOW)
91#define PORT_IRQ_LEGACY \
92 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
93#define PORT_IRQ_HANDLED \
94 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
95 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
96 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
97#define DEF_PORT_IRQ \
98 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
99
100/* product numbers */
101#define MTIP_PRODUCT_UNKNOWN 0x00
102#define MTIP_PRODUCT_ASICFPGA 0x11
103
104/* Device instance number, incremented each time a device is probed. */
105static int instance;
106
Zhu Yanjun9e35fdc2016-01-05 18:39:04 +0800107static struct list_head online_list;
108static struct list_head removing_list;
109static spinlock_t dev_lock;
Asai Thambi S P0caff002013-04-03 19:56:21 +0530110
Sam Bradshaw88523a62011-08-30 08:34:26 -0600111/*
112 * Global variable used to hold the major block device number
113 * allocated in mtip_init().
114 */
Jens Axboe3ff147d2011-09-27 21:33:53 -0600115static int mtip_major;
Asai Thambi S P7b421d22012-06-04 12:44:02 -0700116static struct dentry *dfs_parent;
Asai Thambi S P0caff002013-04-03 19:56:21 +0530117static struct dentry *dfs_device_status;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600118
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800119static u32 cpu_use[NR_CPUS];
120
Sam Bradshaw88523a62011-08-30 08:34:26 -0600121static DEFINE_SPINLOCK(rssd_index_lock);
122static DEFINE_IDA(rssd_index_ida);
123
Asai Thambi S P62ee8c12012-01-04 22:01:32 +0100124static int mtip_block_initialize(struct driver_data *dd);
125
Jens Axboe16d02c02011-09-27 15:50:01 -0600126#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -0600127struct mtip_compat_ide_task_request_s {
128 __u8 io_ports[8];
129 __u8 hob_ports[8];
130 ide_reg_valid_t out_flags;
131 ide_reg_valid_t in_flags;
132 int data_phase;
133 int req_cmd;
134 compat_ulong_t out_size;
135 compat_ulong_t in_size;
136};
Jens Axboe16d02c02011-09-27 15:50:01 -0600137#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -0600138
Sam Bradshaw88523a62011-08-30 08:34:26 -0600139/*
Jens Axboe63166682011-09-27 21:27:43 -0600140 * This function check_for_surprise_removal is called
141 * while card is removed from the system and it will
142 * read the vendor id from the configration space
143 *
144 * @pdev Pointer to the pci_dev structure.
145 *
146 * return value
147 * true if device removed, else false
148 */
149static bool mtip_check_surprise_removal(struct pci_dev *pdev)
150{
151 u16 vendor_id = 0;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600152 struct driver_data *dd = pci_get_drvdata(pdev);
153
154 if (dd->sr)
155 return true;
Jens Axboe63166682011-09-27 21:27:43 -0600156
157 /* Read the vendorID from the configuration space */
158 pci_read_config_word(pdev, 0x00, &vendor_id);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600159 if (vendor_id == 0xFFFF) {
160 dd->sr = true;
161 if (dd->queue)
162 set_bit(QUEUE_FLAG_DEAD, &dd->queue->queue_flags);
163 else
164 dev_warn(&dd->pdev->dev,
165 "%s: dd->queue is NULL\n", __func__);
Jens Axboe63166682011-09-27 21:27:43 -0600166 return true; /* device removed */
Jens Axboe63166682011-09-27 21:27:43 -0600167 }
168
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600169 return false; /* device present */
Jens Axboe63166682011-09-27 21:27:43 -0600170}
171
Jens Axboeffc771b2014-05-09 09:42:02 -0600172static struct mtip_cmd *mtip_get_int_command(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600173{
Jens Axboeffc771b2014-05-09 09:42:02 -0600174 struct request *rq;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600175
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100176 rq = blk_mq_alloc_request(dd->queue, 0, BLK_MQ_REQ_RESERVED);
Jens Axboeffc771b2014-05-09 09:42:02 -0600177 return blk_mq_rq_to_pdu(rq);
178}
Sam Bradshaw88523a62011-08-30 08:34:26 -0600179
Jens Axboeffc771b2014-05-09 09:42:02 -0600180static void mtip_put_int_command(struct driver_data *dd, struct mtip_cmd *cmd)
181{
182 blk_put_request(blk_mq_rq_from_pdu(cmd));
Sam Bradshaw88523a62011-08-30 08:34:26 -0600183}
184
185/*
Jens Axboeffc771b2014-05-09 09:42:02 -0600186 * Once we add support for one hctx per mtip group, this will change a bit
Sam Bradshaw88523a62011-08-30 08:34:26 -0600187 */
Jens Axboeffc771b2014-05-09 09:42:02 -0600188static struct request *mtip_rq_from_tag(struct driver_data *dd,
189 unsigned int tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600190{
Jens Axboe0e62f512014-06-04 10:23:49 -0600191 struct blk_mq_hw_ctx *hctx = dd->queue->queue_hw_ctx[0];
192
193 return blk_mq_tag_to_rq(hctx->tags, tag);
Jens Axboeffc771b2014-05-09 09:42:02 -0600194}
195
196static struct mtip_cmd *mtip_cmd_from_tag(struct driver_data *dd,
197 unsigned int tag)
198{
199 struct request *rq = mtip_rq_from_tag(dd, tag);
200
201 return blk_mq_rq_to_pdu(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600202}
203
204/*
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600205 * IO completion function.
206 *
207 * This completion function is called by the driver ISR when a
208 * command that was issued by the kernel completes. It first calls the
209 * asynchronous completion function which normally calls back into the block
210 * layer passing the asynchronous callback data, then unmaps the
211 * scatter list associated with the completed command, and finally
212 * clears the allocated bit associated with the completed command.
213 *
214 * @port Pointer to the port data structure.
215 * @tag Tag of the command.
216 * @data Pointer to driver_data.
217 * @status Completion status.
218 *
219 * return value
220 * None
221 */
222static void mtip_async_complete(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600223 int tag, struct mtip_cmd *cmd, int status)
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600224{
Jens Axboeffc771b2014-05-09 09:42:02 -0600225 struct driver_data *dd = port->dd;
226 struct request *rq;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600227
228 if (unlikely(!dd) || unlikely(!port))
229 return;
230
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600231 if (unlikely(status == PORT_IRQ_TF_ERR)) {
232 dev_warn(&port->dd->pdev->dev,
233 "Command tag %d failed due to TFE\n", tag);
234 }
235
Jens Axboeffc771b2014-05-09 09:42:02 -0600236 /* Unmap the DMA scatter list entries */
237 dma_unmap_sg(&dd->pdev->dev, cmd->sg, cmd->scatter_ents, cmd->direction);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600238
Jens Axboeffc771b2014-05-09 09:42:02 -0600239 rq = mtip_rq_from_tag(dd, tag);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600240
Jens Axboeffc771b2014-05-09 09:42:02 -0600241 if (unlikely(cmd->unaligned))
242 up(&port->cmd_slot_unal);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600243
Christoph Hellwigc8a446a2014-09-13 16:40:10 -0700244 blk_mq_end_request(rq, status ? -EIO : 0);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600245}
246
247/*
Jens Axboe63166682011-09-27 21:27:43 -0600248 * Reset the HBA (without sleeping)
249 *
Jens Axboe63166682011-09-27 21:27:43 -0600250 * @dd Pointer to the driver data structure.
251 *
252 * return value
253 * 0 The reset was successful.
254 * -1 The HBA Reset bit did not clear.
255 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530256static int mtip_hba_reset(struct driver_data *dd)
Jens Axboe63166682011-09-27 21:27:43 -0600257{
258 unsigned long timeout;
259
Jens Axboe63166682011-09-27 21:27:43 -0600260 /* Set the reset bit */
261 writel(HOST_RESET, dd->mmio + HOST_CTL);
262
263 /* Flush */
264 readl(dd->mmio + HOST_CTL);
265
Asai Thambi SP2f17d712015-05-11 15:57:16 -0700266 /*
267 * Spin for up to 10 seconds waiting for reset acknowledgement. Spec
268 * is 1 sec but in LUN failure conditions, up to 10 secs are required
269 */
270 timeout = jiffies + msecs_to_jiffies(10000);
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530271 do {
272 mdelay(10);
273 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
274 return -1;
Jens Axboe63166682011-09-27 21:27:43 -0600275
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530276 } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
277 && time_before(jiffies, timeout));
Asai Thambi S P45038362012-04-09 08:35:38 +0200278
Jens Axboe63166682011-09-27 21:27:43 -0600279 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
280 return -1;
281
282 return 0;
283}
284
285/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600286 * Issue a command to the hardware.
287 *
288 * Set the appropriate bit in the s_active and Command Issue hardware
289 * registers, causing hardware command processing to begin.
290 *
291 * @port Pointer to the port structure.
292 * @tag The tag of the command to be issued.
293 *
294 * return value
295 * None
296 */
297static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
298{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800299 int group = tag >> 5;
300
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800301 /* guard SACT and CI registers */
302 spin_lock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600303 writel((1 << MTIP_TAG_BIT(tag)),
304 port->s_active[MTIP_TAG_INDEX(tag)]);
305 writel((1 << MTIP_TAG_BIT(tag)),
306 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800307 spin_unlock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600308}
309
310/*
Jens Axboe63166682011-09-27 21:27:43 -0600311 * Enable/disable the reception of FIS
312 *
313 * @port Pointer to the port data structure
314 * @enable 1 to enable, 0 to disable
315 *
316 * return value
317 * Previous state: 1 enabled, 0 disabled
318 */
319static int mtip_enable_fis(struct mtip_port *port, int enable)
320{
321 u32 tmp;
322
323 /* enable FIS reception */
324 tmp = readl(port->mmio + PORT_CMD);
325 if (enable)
326 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
327 else
328 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
329
330 /* Flush */
331 readl(port->mmio + PORT_CMD);
332
333 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
334}
335
336/*
337 * Enable/disable the DMA engine
338 *
339 * @port Pointer to the port data structure
340 * @enable 1 to enable, 0 to disable
341 *
342 * return value
343 * Previous state: 1 enabled, 0 disabled.
344 */
345static int mtip_enable_engine(struct mtip_port *port, int enable)
346{
347 u32 tmp;
348
349 /* enable FIS reception */
350 tmp = readl(port->mmio + PORT_CMD);
351 if (enable)
352 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
353 else
354 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
355
356 readl(port->mmio + PORT_CMD);
357 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
358}
359
360/*
361 * Enables the port DMA engine and FIS reception.
362 *
363 * return value
364 * None
365 */
366static inline void mtip_start_port(struct mtip_port *port)
367{
368 /* Enable FIS reception */
369 mtip_enable_fis(port, 1);
370
371 /* Enable the DMA engine */
372 mtip_enable_engine(port, 1);
373}
374
375/*
376 * Deinitialize a port by disabling port interrupts, the DMA engine,
377 * and FIS reception.
378 *
379 * @port Pointer to the port structure
380 *
381 * return value
382 * None
383 */
384static inline void mtip_deinit_port(struct mtip_port *port)
385{
386 /* Disable interrupts on this port */
387 writel(0, port->mmio + PORT_IRQ_MASK);
388
389 /* Disable the DMA engine */
390 mtip_enable_engine(port, 0);
391
392 /* Disable FIS reception */
393 mtip_enable_fis(port, 0);
394}
395
396/*
397 * Initialize a port.
398 *
399 * This function deinitializes the port by calling mtip_deinit_port() and
400 * then initializes it by setting the command header and RX FIS addresses,
401 * clearing the SError register and any pending port interrupts before
402 * re-enabling the default set of port interrupts.
403 *
404 * @port Pointer to the port structure.
405 *
406 * return value
407 * None
408 */
409static void mtip_init_port(struct mtip_port *port)
410{
411 int i;
412 mtip_deinit_port(port);
413
414 /* Program the command list base and FIS base addresses */
415 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
416 writel((port->command_list_dma >> 16) >> 16,
417 port->mmio + PORT_LST_ADDR_HI);
418 writel((port->rxfis_dma >> 16) >> 16,
419 port->mmio + PORT_FIS_ADDR_HI);
420 }
421
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100422 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600423 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100424 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600425
426 /* Clear SError */
427 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
428
429 /* reset the completed registers.*/
430 for (i = 0; i < port->dd->slot_groups; i++)
431 writel(0xFFFFFFFF, port->completed[i]);
432
433 /* Clear any pending interrupts for this port */
Asai Thambi S P6bb688c2012-05-29 18:40:45 -0700434 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
Jens Axboe63166682011-09-27 21:27:43 -0600435
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100436 /* Clear any pending interrupts on the HBA. */
437 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
438 port->dd->mmio + HOST_IRQ_STAT);
439
Jens Axboe63166682011-09-27 21:27:43 -0600440 /* Enable port interrupts */
441 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
442}
443
444/*
445 * Restart a port
446 *
447 * @port Pointer to the port data structure.
448 *
449 * return value
450 * None
451 */
452static void mtip_restart_port(struct mtip_port *port)
453{
454 unsigned long timeout;
455
456 /* Disable the DMA engine */
457 mtip_enable_engine(port, 0);
458
459 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
460 timeout = jiffies + msecs_to_jiffies(500);
461 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
462 && time_before(jiffies, timeout))
463 ;
464
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200465 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200466 return;
467
Jens Axboe63166682011-09-27 21:27:43 -0600468 /*
469 * Chip quirk: escalate to hba reset if
470 * PxCMD.CR not clear after 500 ms
471 */
472 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
473 dev_warn(&port->dd->pdev->dev,
474 "PxCMD.CR not clear, escalating reset\n");
475
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530476 if (mtip_hba_reset(port->dd))
Jens Axboe63166682011-09-27 21:27:43 -0600477 dev_err(&port->dd->pdev->dev,
478 "HBA reset escalation failed.\n");
479
480 /* 30 ms delay before com reset to quiesce chip */
481 mdelay(30);
482 }
483
484 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
485
486 /* Set PxSCTL.DET */
487 writel(readl(port->mmio + PORT_SCR_CTL) |
488 1, port->mmio + PORT_SCR_CTL);
489 readl(port->mmio + PORT_SCR_CTL);
490
491 /* Wait 1 ms to quiesce chip function */
492 timeout = jiffies + msecs_to_jiffies(1);
493 while (time_before(jiffies, timeout))
494 ;
495
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200496 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200497 return;
498
Jens Axboe63166682011-09-27 21:27:43 -0600499 /* Clear PxSCTL.DET */
500 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
501 port->mmio + PORT_SCR_CTL);
502 readl(port->mmio + PORT_SCR_CTL);
503
504 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
505 timeout = jiffies + msecs_to_jiffies(500);
506 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
507 && time_before(jiffies, timeout))
508 ;
509
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200510 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200511 return;
512
Jens Axboe63166682011-09-27 21:27:43 -0600513 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
514 dev_warn(&port->dd->pdev->dev,
515 "COM reset failed\n");
516
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100517 mtip_init_port(port);
518 mtip_start_port(port);
Jens Axboe63166682011-09-27 21:27:43 -0600519
Jens Axboe63166682011-09-27 21:27:43 -0600520}
521
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530522static int mtip_device_reset(struct driver_data *dd)
523{
524 int rv = 0;
525
526 if (mtip_check_surprise_removal(dd->pdev))
527 return 0;
528
529 if (mtip_hba_reset(dd) < 0)
530 rv = -EFAULT;
531
532 mdelay(1);
533 mtip_init_port(dd->port);
534 mtip_start_port(dd->port);
535
536 /* Enable interrupts on the HBA. */
537 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
538 dd->mmio + HOST_CTL);
539 return rv;
540}
541
Jens Axboe63166682011-09-27 21:27:43 -0600542/*
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200543 * Helper function for tag logging
544 */
545static void print_tags(struct driver_data *dd,
546 char *msg,
547 unsigned long *tagbits,
548 int cnt)
549{
550 unsigned char tagmap[128];
551 int group, tagmap_len = 0;
552
553 memset(tagmap, 0, sizeof(tagmap));
554 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
Jens Axboeffc771b2014-05-09 09:42:02 -0600555 tagmap_len += sprintf(tagmap + tagmap_len, "%016lX ",
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200556 tagbits[group-1]);
557 dev_warn(&dd->pdev->dev,
558 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
559}
560
561/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600562 * Internal command completion callback function.
563 *
564 * This function is normally called by the driver ISR when an internal
565 * command completed. This function signals the command completion by
566 * calling complete().
567 *
568 * @port Pointer to the port data structure.
569 * @tag Tag of the command that has completed.
570 * @data Pointer to a completion structure.
571 * @status Completion status.
572 *
573 * return value
574 * None
575 */
576static void mtip_completion(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600577 int tag, struct mtip_cmd *command, int status)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600578{
Jens Axboeffc771b2014-05-09 09:42:02 -0600579 struct completion *waiting = command->comp_data;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600580 if (unlikely(status == PORT_IRQ_TF_ERR))
581 dev_warn(&port->dd->pdev->dev,
582 "Internal command %d completed with TFE\n", tag);
583
Sam Bradshaw88523a62011-08-30 08:34:26 -0600584 complete(waiting);
585}
586
Asai Thambi S P8182b492012-04-09 08:35:38 +0200587static void mtip_null_completion(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600588 int tag, struct mtip_cmd *command, int status)
Asai Thambi S P8182b492012-04-09 08:35:38 +0200589{
Asai Thambi S P8182b492012-04-09 08:35:38 +0200590}
591
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200592static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
593 dma_addr_t buffer_dma, unsigned int sectors);
594static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
595 struct smart_attr *attrib);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600596/*
597 * Handle an error.
598 *
599 * @dd Pointer to the DRIVER_DATA structure.
600 *
601 * return value
602 * None
603 */
604static void mtip_handle_tfe(struct driver_data *dd)
605{
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200606 int group, tag, bit, reissue, rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600607 struct mtip_port *port;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200608 struct mtip_cmd *cmd;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600609 u32 completed;
610 struct host_to_dev_fis *fis;
611 unsigned long tagaccum[SLOTBITS_IN_LONGS];
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200612 unsigned int cmd_cnt = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200613 unsigned char *buf;
614 char *fail_reason = NULL;
615 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600616
617 dev_warn(&dd->pdev->dev, "Taskfile error\n");
618
619 port = dd->port;
620
Asai Thambi SPa7806fa2015-05-11 15:49:28 -0700621 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600622 cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700623 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
624
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700625 if (cmd->comp_data && cmd->comp_func) {
626 cmd->comp_func(port, MTIP_TAG_INTERNAL,
Jens Axboeffc771b2014-05-09 09:42:02 -0600627 cmd, PORT_IRQ_TF_ERR);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700628 }
Asai Thambi SPe35b9472016-02-24 21:16:21 -0800629 return;
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700630 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600631
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200632 /* clear the tag accumulator */
633 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
634
Sam Bradshaw88523a62011-08-30 08:34:26 -0600635 /* Loop through all the groups */
636 for (group = 0; group < dd->slot_groups; group++) {
637 completed = readl(port->completed[group]);
638
Jens Axboeffc771b2014-05-09 09:42:02 -0600639 dev_warn(&dd->pdev->dev, "g=%u, comp=%x\n", group, completed);
640
Sam Bradshaw88523a62011-08-30 08:34:26 -0600641 /* clear completed status register in the hardware.*/
642 writel(completed, port->completed[group]);
643
Sam Bradshaw88523a62011-08-30 08:34:26 -0600644 /* Process successfully completed commands */
645 for (bit = 0; bit < 32 && completed; bit++) {
646 if (!(completed & (1<<bit)))
647 continue;
648 tag = (group << 5) + bit;
649
650 /* Skip the internal command slot */
651 if (tag == MTIP_TAG_INTERNAL)
652 continue;
653
Jens Axboeffc771b2014-05-09 09:42:02 -0600654 cmd = mtip_cmd_from_tag(dd, tag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200655 if (likely(cmd->comp_func)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600656 set_bit(tag, tagaccum);
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200657 cmd_cnt++;
Jens Axboeffc771b2014-05-09 09:42:02 -0600658 cmd->comp_func(port, tag, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600659 } else {
660 dev_err(&port->dd->pdev->dev,
661 "Missing completion func for tag %d",
662 tag);
663 if (mtip_check_surprise_removal(dd->pdev)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600664 /* don't proceed further */
665 return;
666 }
667 }
668 }
669 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200670
671 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600672
673 /* Restart the port */
674 mdelay(20);
675 mtip_restart_port(port);
676
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200677 /* Trying to determine the cause of the error */
678 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
679 dd->port->log_buf,
680 dd->port->log_buf_dma, 1);
681 if (rv) {
682 dev_warn(&dd->pdev->dev,
683 "Error in READ LOG EXT (10h) command\n");
684 /* non-critical error, don't fail the load */
685 } else {
686 buf = (unsigned char *)dd->port->log_buf;
687 if (buf[259] & 0x1) {
688 dev_info(&dd->pdev->dev,
689 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200690 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200691 fail_all_ncq_write = 1;
692 fail_reason = "write protect";
693 }
694 if (buf[288] == 0xF7) {
695 dev_info(&dd->pdev->dev,
696 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200697 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200698 fail_all_ncq_cmds = 1;
699 fail_reason = "thermal shutdown";
700 }
701 if (buf[288] == 0xBF) {
Sam Bradshaw26d58052013-10-03 10:18:05 -0700702 set_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200703 dev_info(&dd->pdev->dev,
Sam Bradshaw26d58052013-10-03 10:18:05 -0700704 "Drive indicates rebuild has failed. Secure erase required.\n");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200705 fail_all_ncq_cmds = 1;
706 fail_reason = "rebuild failed";
707 }
708 }
709
Sam Bradshaw88523a62011-08-30 08:34:26 -0600710 /* clear the tag accumulator */
711 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
712
713 /* Loop through all the groups */
714 for (group = 0; group < dd->slot_groups; group++) {
715 for (bit = 0; bit < 32; bit++) {
716 reissue = 1;
717 tag = (group << 5) + bit;
Jens Axboeffc771b2014-05-09 09:42:02 -0600718 cmd = mtip_cmd_from_tag(dd, tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600719
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200720 fis = (struct host_to_dev_fis *)cmd->command;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600721
722 /* Should re-issue? */
723 if (tag == MTIP_TAG_INTERNAL ||
724 fis->command == ATA_CMD_SET_FEATURES)
725 reissue = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200726 else {
727 if (fail_all_ncq_cmds ||
728 (fail_all_ncq_write &&
729 fis->command == ATA_CMD_FPDMA_WRITE)) {
730 dev_warn(&dd->pdev->dev,
731 " Fail: %s w/tag %d [%s].\n",
732 fis->command == ATA_CMD_FPDMA_WRITE ?
733 "write" : "read",
734 tag,
735 fail_reason != NULL ?
736 fail_reason : "unknown");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200737 if (cmd->comp_func) {
738 cmd->comp_func(port, tag,
Jens Axboeffc771b2014-05-09 09:42:02 -0600739 cmd, -ENODATA);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200740 }
741 continue;
742 }
743 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600744
745 /*
746 * First check if this command has
747 * exceeded its retries.
748 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200749 if (reissue && (cmd->retries-- > 0)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600750
751 set_bit(tag, tagaccum);
752
Sam Bradshaw88523a62011-08-30 08:34:26 -0600753 /* Re-issue the command. */
754 mtip_issue_ncq_command(port, tag);
755
756 continue;
757 }
758
759 /* Retire a command that will not be reissued */
760 dev_warn(&port->dd->pdev->dev,
761 "retiring tag %d\n", tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600762
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200763 if (cmd->comp_func)
Jens Axboeffc771b2014-05-09 09:42:02 -0600764 cmd->comp_func(port, tag, cmd, PORT_IRQ_TF_ERR);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600765 else
766 dev_warn(&port->dd->pdev->dev,
767 "Bad completion for tag %d\n",
768 tag);
769 }
770 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200771 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600772}
773
774/*
775 * Handle a set device bits interrupt
776 */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800777static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
778 u32 completed)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600779{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800780 struct driver_data *dd = port->dd;
781 int tag, bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600782 struct mtip_cmd *command;
783
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800784 if (!completed) {
785 WARN_ON_ONCE(!completed);
786 return;
787 }
788 /* clear completed status register in the hardware.*/
789 writel(completed, port->completed[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600790
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800791 /* Process completed commands. */
792 for (bit = 0; (bit < 32) && completed; bit++) {
793 if (completed & 0x01) {
794 tag = (group << 5) | bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600795
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800796 /* skip internal command slot. */
797 if (unlikely(tag == MTIP_TAG_INTERNAL))
798 continue;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600799
Jens Axboeffc771b2014-05-09 09:42:02 -0600800 command = mtip_cmd_from_tag(dd, tag);
801 if (likely(command->comp_func))
802 command->comp_func(port, tag, command, 0);
803 else {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600804 dev_dbg(&dd->pdev->dev,
805 "Null completion for tag %d",
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800806 tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600807
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800808 if (mtip_check_surprise_removal(
809 dd->pdev)) {
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800810 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600811 }
812 }
813 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800814 completed >>= 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600815 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800816
817 /* If last, re-enable interrupts */
818 if (atomic_dec_return(&dd->irq_workers_active) == 0)
819 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600820}
821
822/*
823 * Process legacy pio and d2h interrupts
824 */
825static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
826{
827 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -0600828 struct mtip_cmd *cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600829
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200830 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100831 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
Sam Bradshaw88523a62011-08-30 08:34:26 -0600832 & (1 << MTIP_TAG_INTERNAL))) {
833 if (cmd->comp_func) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600834 cmd->comp_func(port, MTIP_TAG_INTERNAL, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600835 return;
836 }
837 }
838
Sam Bradshaw88523a62011-08-30 08:34:26 -0600839 return;
840}
841
842/*
843 * Demux and handle errors
844 */
845static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
846{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600847
848 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
849 dev_warn(&dd->pdev->dev,
850 "Clearing PxSERR.DIAG.x\n");
851 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
852 }
853
854 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
855 dev_warn(&dd->pdev->dev,
856 "Clearing PxSERR.DIAG.n\n");
857 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
858 }
859
860 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
861 dev_warn(&dd->pdev->dev,
862 "Port stat errors %x unhandled\n",
863 (port_stat & ~PORT_IRQ_HANDLED));
Asai Thambi S P9b204fb2014-05-20 10:48:56 -0700864 if (mtip_check_surprise_removal(dd->pdev))
865 return;
866 }
867 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR))) {
868 set_bit(MTIP_PF_EH_ACTIVE_BIT, &dd->port->flags);
869 wake_up_interruptible(&dd->port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600870 }
871}
872
873static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
874{
875 struct driver_data *dd = (struct driver_data *) data;
876 struct mtip_port *port = dd->port;
877 u32 hba_stat, port_stat;
878 int rv = IRQ_NONE;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800879 int do_irq_enable = 1, i, workers;
880 struct mtip_work *twork;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600881
882 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
883 if (hba_stat) {
884 rv = IRQ_HANDLED;
885
886 /* Acknowledge the interrupt status on the port.*/
887 port_stat = readl(port->mmio + PORT_IRQ_STAT);
Asai Thambi SP2132a542015-05-11 15:53:18 -0700888 if (unlikely(port_stat == 0xFFFFFFFF)) {
889 mtip_check_surprise_removal(dd->pdev);
890 return IRQ_HANDLED;
891 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600892 writel(port_stat, port->mmio + PORT_IRQ_STAT);
893
894 /* Demux port status */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800895 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
896 do_irq_enable = 0;
897 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
898
899 /* Start at 1: group zero is always local? */
900 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
901 i++) {
902 twork = &dd->work[i];
903 twork->completed = readl(port->completed[i]);
904 if (twork->completed)
905 workers++;
906 }
907
908 atomic_set(&dd->irq_workers_active, workers);
909 if (workers) {
910 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
911 twork = &dd->work[i];
912 if (twork->completed)
913 queue_work_on(
914 twork->cpu_binding,
915 dd->isr_workq,
916 &twork->work);
917 }
918
919 if (likely(dd->work[0].completed))
920 mtip_workq_sdbfx(port, 0,
921 dd->work[0].completed);
922
923 } else {
924 /*
925 * Chip quirk: SDB interrupt but nothing
926 * to complete
927 */
928 do_irq_enable = 1;
929 }
930 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600931
932 if (unlikely(port_stat & PORT_IRQ_ERR)) {
933 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600934 /* don't proceed further */
935 return IRQ_HANDLED;
936 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200937 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +0200938 &dd->dd_flag))
939 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600940
941 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
942 }
943
944 if (unlikely(port_stat & PORT_IRQ_LEGACY))
945 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
946 }
947
948 /* acknowledge interrupt */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800949 if (unlikely(do_irq_enable))
950 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600951
952 return rv;
953}
954
955/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600956 * HBA interrupt subroutine.
957 *
958 * @irq IRQ number.
959 * @instance Pointer to the driver data structure.
960 *
961 * return value
962 * IRQ_HANDLED A HBA interrupt was pending and handled.
963 * IRQ_NONE This interrupt was not for the HBA.
964 */
965static irqreturn_t mtip_irq_handler(int irq, void *instance)
966{
967 struct driver_data *dd = instance;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800968
969 return mtip_handle_irq(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600970}
971
972static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
973{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600974 writel(1 << MTIP_TAG_BIT(tag),
975 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
976}
977
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200978static bool mtip_pause_ncq(struct mtip_port *port,
979 struct host_to_dev_fis *fis)
980{
981 struct host_to_dev_fis *reply;
982 unsigned long task_file_data;
983
984 reply = port->rxfis + RX_FIS_D2H_REG;
985 task_file_data = readl(port->mmio+PORT_TFDATA);
986
Asai Thambi S P12a166c2012-09-05 22:01:36 +0530987 if ((task_file_data & 1))
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200988 return false;
989
990 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200991 port->ic_pause_timer = jiffies;
992 return true;
993 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
994 (fis->features == 0x03)) {
995 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
996 port->ic_pause_timer = jiffies;
997 return true;
998 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
999 ((fis->command == 0xFC) &&
1000 (fis->features == 0x27 || fis->features == 0x72 ||
1001 fis->features == 0x62 || fis->features == 0x26))) {
Asai Thambi SPee04bed2015-05-11 15:50:50 -07001002 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001003 /* Com reset after secure erase or lowlevel format */
1004 mtip_restart_port(port);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001005 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001006 return false;
1007 }
1008
1009 return false;
1010}
1011
Sam Bradshaw88523a62011-08-30 08:34:26 -06001012/*
1013 * Wait for port to quiesce
1014 *
1015 * @port Pointer to port data structure
1016 * @timeout Max duration to wait (ms)
1017 *
1018 * return value
1019 * 0 Success
1020 * -EBUSY Commands still active
1021 */
1022static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
1023{
1024 unsigned long to;
Dan Carpenter3e54a3d2011-11-24 12:59:00 +01001025 unsigned int n;
1026 unsigned int active = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001027
Jens Axboe9acf03c2014-05-14 08:22:56 -06001028 blk_mq_stop_hw_queues(port->dd->queue);
1029
Sam Bradshaw88523a62011-08-30 08:34:26 -06001030 to = jiffies + msecs_to_jiffies(timeout);
1031 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001032 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
1033 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001034 msleep(20);
1035 continue; /* svc thd is actively issuing commands */
1036 }
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001037
1038 msleep(100);
1039 if (mtip_check_surprise_removal(port->dd->pdev))
1040 goto err_fault;
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001041 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Jens Axboe9acf03c2014-05-14 08:22:56 -06001042 goto err_fault;
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001043
Sam Bradshaw88523a62011-08-30 08:34:26 -06001044 /*
1045 * Ignore s_active bit 0 of array element 0.
1046 * This bit will always be set
1047 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001048 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001049 for (n = 1; n < port->dd->slot_groups; n++)
1050 active |= readl(port->s_active[n]);
1051
1052 if (!active)
1053 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001054 } while (time_before(jiffies, to));
1055
Jens Axboe9acf03c2014-05-14 08:22:56 -06001056 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001057 return active ? -EBUSY : 0;
Jens Axboe9acf03c2014-05-14 08:22:56 -06001058err_fault:
1059 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
1060 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001061}
1062
1063/*
1064 * Execute an internal command and wait for the completion.
1065 *
1066 * @port Pointer to the port data structure.
1067 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001068 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001069 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001070 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001071 * @opts Command header options, excluding the FIS length
1072 * and the number of PRD entries.
1073 * @timeout Time in ms to wait for the command to complete.
1074 *
1075 * return value
1076 * 0 Command completed successfully.
1077 * -EFAULT The buffer address is not correctly aligned.
1078 * -EBUSY Internal command or other IO in progress.
1079 * -EAGAIN Time out waiting for command to complete.
1080 */
1081static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001082 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001083 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001084 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001085 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001086 u32 opts,
1087 gfp_t atomic,
1088 unsigned long timeout)
1089{
1090 struct mtip_cmd_sg *command_sg;
1091 DECLARE_COMPLETION_ONSTACK(wait);
Jens Axboeffc771b2014-05-09 09:42:02 -06001092 struct mtip_cmd *int_cmd;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301093 struct driver_data *dd = port->dd;
Jens Axboeffc771b2014-05-09 09:42:02 -06001094 int rv = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001095
1096 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1097 if (buffer & 0x00000007) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301098 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001099 return -EFAULT;
1100 }
1101
Jens Axboeffc771b2014-05-09 09:42:02 -06001102 int_cmd = mtip_get_int_command(dd);
1103
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001104 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001105
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001106 if (fis->command == ATA_CMD_SEC_ERASE_PREP)
1107 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1108
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301109 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001110
1111 if (atomic == GFP_KERNEL) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001112 if (fis->command != ATA_CMD_STANDBYNOW1) {
1113 /* wait for io to complete if non atomic */
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001114 if (mtip_quiesce_io(port,
1115 MTIP_QUIESCE_IO_TIMEOUT_MS) < 0) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301116 dev_warn(&dd->pdev->dev,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001117 "Failed to quiesce IO\n");
Jens Axboeffc771b2014-05-09 09:42:02 -06001118 mtip_put_int_command(dd, int_cmd);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001119 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001120 wake_up_interruptible(&port->svc_wait);
1121 return -EBUSY;
1122 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001123 }
1124
1125 /* Set the completion function and data for the command. */
1126 int_cmd->comp_data = &wait;
1127 int_cmd->comp_func = mtip_completion;
1128
1129 } else {
1130 /* Clear completion - we're going to poll */
1131 int_cmd->comp_data = NULL;
Asai Thambi S P8182b492012-04-09 08:35:38 +02001132 int_cmd->comp_func = mtip_null_completion;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001133 }
1134
1135 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001136 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001137
1138 /* Populate the SG list */
1139 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001140 __force_bit2int cpu_to_le32(opts | fis_len);
1141 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001142 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1143
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001144 command_sg->info =
1145 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1146 command_sg->dba =
1147 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1148 command_sg->dba_upper =
1149 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001150
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001151 int_cmd->command_header->opts |=
1152 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001153 }
1154
1155 /* Populate the command header */
1156 int_cmd->command_header->byte_count = 0;
1157
1158 /* Issue the command to the hardware */
1159 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1160
Sam Bradshaw88523a62011-08-30 08:34:26 -06001161 if (atomic == GFP_KERNEL) {
1162 /* Wait for the command to complete or timeout. */
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001163 if ((rv = wait_for_completion_interruptible_timeout(
Sam Bradshaw88523a62011-08-30 08:34:26 -06001164 &wait,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001165 msecs_to_jiffies(timeout))) <= 0) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301166 if (rv == -ERESTARTSYS) { /* interrupted */
1167 dev_err(&dd->pdev->dev,
1168 "Internal command [%02X] was interrupted after %lu ms\n",
1169 fis->command, timeout);
1170 rv = -EINTR;
1171 goto exec_ic_exit;
1172 } else if (rv == 0) /* timeout */
1173 dev_err(&dd->pdev->dev,
1174 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1175 fis->command, timeout);
1176 else
1177 dev_err(&dd->pdev->dev,
1178 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1179 fis->command, rv, timeout);
1180
1181 if (mtip_check_surprise_removal(dd->pdev) ||
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001182 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301183 &dd->dd_flag)) {
1184 dev_err(&dd->pdev->dev,
1185 "Internal command [%02X] wait returned due to SR\n",
1186 fis->command);
Asai Thambi S P45038362012-04-09 08:35:38 +02001187 rv = -ENXIO;
1188 goto exec_ic_exit;
1189 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301190 mtip_device_reset(dd); /* recover from timeout issue */
Sam Bradshaw88523a62011-08-30 08:34:26 -06001191 rv = -EAGAIN;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301192 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001193 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001194 } else {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301195 u32 hba_stat, port_stat;
1196
Sam Bradshaw88523a62011-08-30 08:34:26 -06001197 /* Spin for <timeout> checking if command still outstanding */
1198 timeout = jiffies + msecs_to_jiffies(timeout);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001199 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1200 & (1 << MTIP_TAG_INTERNAL))
1201 && time_before(jiffies, timeout)) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301202 if (mtip_check_surprise_removal(dd->pdev)) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001203 rv = -ENXIO;
1204 goto exec_ic_exit;
1205 }
1206 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001207 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301208 &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02001209 rv = -ENXIO;
1210 goto exec_ic_exit;
1211 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301212 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1213 if (!port_stat)
1214 continue;
1215
1216 if (port_stat & PORT_IRQ_ERR) {
1217 dev_err(&dd->pdev->dev,
1218 "Internal command [%02X] failed\n",
1219 fis->command);
1220 mtip_device_reset(dd);
1221 rv = -EIO;
1222 goto exec_ic_exit;
1223 } else {
1224 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1225 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1226 if (hba_stat)
1227 writel(hba_stat,
1228 dd->mmio + HOST_IRQ_STAT);
Asai Thambi S P45038362012-04-09 08:35:38 +02001229 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301230 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001231 }
1232 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001233
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001234 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1235 & (1 << MTIP_TAG_INTERNAL)) {
1236 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301237 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1238 mtip_device_reset(dd);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001239 rv = -EAGAIN;
1240 }
1241 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001242exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001243 /* Clear the allocated and active bits for the internal command. */
Jens Axboeffc771b2014-05-09 09:42:02 -06001244 mtip_put_int_command(dd, int_cmd);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001245 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001246 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1247 /* NCQ paused */
1248 return rv;
1249 }
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001250 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001251
1252 return rv;
1253}
1254
1255/*
1256 * Byte-swap ATA ID strings.
1257 *
1258 * ATA identify data contains strings in byte-swapped 16-bit words.
1259 * They must be swapped (on all architectures) to be usable as C strings.
1260 * This function swaps bytes in-place.
1261 *
1262 * @buf The buffer location of the string
1263 * @len The number of bytes to swap
1264 *
1265 * return value
1266 * None
1267 */
1268static inline void ata_swap_string(u16 *buf, unsigned int len)
1269{
1270 int i;
1271 for (i = 0; i < (len/2); i++)
1272 be16_to_cpus(&buf[i]);
1273}
1274
Asai Thambi S P670a6412014-04-16 20:27:54 -07001275static void mtip_set_timeout(struct driver_data *dd,
1276 struct host_to_dev_fis *fis,
1277 unsigned int *timeout, u8 erasemode)
1278{
1279 switch (fis->command) {
1280 case ATA_CMD_DOWNLOAD_MICRO:
1281 *timeout = 120000; /* 2 minutes */
1282 break;
1283 case ATA_CMD_SEC_ERASE_UNIT:
1284 case 0xFC:
1285 if (erasemode)
1286 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
1287 else
1288 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
1289 break;
1290 case ATA_CMD_STANDBYNOW1:
1291 *timeout = 120000; /* 2 minutes */
1292 break;
1293 case 0xF7:
1294 case 0xFA:
1295 *timeout = 60000; /* 60 seconds */
1296 break;
1297 case ATA_CMD_SMART:
1298 *timeout = 15000; /* 15 seconds */
1299 break;
1300 default:
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001301 *timeout = MTIP_IOCTL_CMD_TIMEOUT_MS;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001302 break;
1303 }
1304}
1305
Sam Bradshaw88523a62011-08-30 08:34:26 -06001306/*
1307 * Request the device identity information.
1308 *
1309 * If a user space buffer is not specified, i.e. is NULL, the
1310 * identify information is still read from the drive and placed
1311 * into the identify data buffer (@e port->identify) in the
1312 * port data structure.
1313 * When the identify buffer contains valid identify information @e
1314 * port->identify_valid is non-zero.
1315 *
1316 * @port Pointer to the port structure.
1317 * @user_buffer A user space buffer where the identify data should be
1318 * copied.
1319 *
1320 * return value
1321 * 0 Command completed successfully.
1322 * -EFAULT An error occurred while coping data to the user buffer.
1323 * -1 Command failed.
1324 */
1325static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1326{
1327 int rv = 0;
1328 struct host_to_dev_fis fis;
1329
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001330 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001331 return -EFAULT;
1332
Sam Bradshaw88523a62011-08-30 08:34:26 -06001333 /* Build the FIS. */
1334 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1335 fis.type = 0x27;
1336 fis.opts = 1 << 7;
1337 fis.command = ATA_CMD_ID_ATA;
1338
1339 /* Set the identify information as invalid. */
1340 port->identify_valid = 0;
1341
1342 /* Clear the identify information. */
1343 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1344
1345 /* Execute the command. */
1346 if (mtip_exec_internal_command(port,
1347 &fis,
1348 5,
1349 port->identify_dma,
1350 sizeof(u16) * ATA_ID_WORDS,
1351 0,
1352 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001353 MTIP_INT_CMD_TIMEOUT_MS)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001354 < 0) {
1355 rv = -1;
1356 goto out;
1357 }
1358
1359 /*
1360 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1361 * perform field-sensitive swapping on the string fields.
1362 * See the kernel use of ata_id_string() for proof of this.
1363 */
1364#ifdef __LITTLE_ENDIAN
1365 ata_swap_string(port->identify + 27, 40); /* model string*/
1366 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1367 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1368#else
1369 {
1370 int i;
1371 for (i = 0; i < ATA_ID_WORDS; i++)
1372 port->identify[i] = le16_to_cpu(port->identify[i]);
1373 }
1374#endif
1375
Sam Bradshaw26d58052013-10-03 10:18:05 -07001376 /* Check security locked state */
1377 if (port->identify[128] & 0x4)
1378 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1379 else
1380 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1381
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301382#ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
Asai Thambi S P15283462013-01-11 14:41:34 +01001383 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1384 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1385 port->dd->trim_supp = true;
1386 else
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301387#endif
Asai Thambi S P15283462013-01-11 14:41:34 +01001388 port->dd->trim_supp = false;
1389
Sam Bradshaw88523a62011-08-30 08:34:26 -06001390 /* Set the identify buffer as valid. */
1391 port->identify_valid = 1;
1392
1393 if (user_buffer) {
1394 if (copy_to_user(
1395 user_buffer,
1396 port->identify,
1397 ATA_ID_WORDS * sizeof(u16))) {
1398 rv = -EFAULT;
1399 goto out;
1400 }
1401 }
1402
1403out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001404 return rv;
1405}
1406
1407/*
1408 * Issue a standby immediate command to the device.
1409 *
1410 * @port Pointer to the port structure.
1411 *
1412 * return value
1413 * 0 Command was executed successfully.
1414 * -1 An error occurred while executing the command.
1415 */
1416static int mtip_standby_immediate(struct mtip_port *port)
1417{
1418 int rv;
1419 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001420 unsigned long start;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001421 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001422
Sam Bradshaw88523a62011-08-30 08:34:26 -06001423 /* Build the FIS. */
1424 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1425 fis.type = 0x27;
1426 fis.opts = 1 << 7;
1427 fis.command = ATA_CMD_STANDBYNOW1;
1428
Asai Thambi S P670a6412014-04-16 20:27:54 -07001429 mtip_set_timeout(port->dd, &fis, &timeout, 0);
1430
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001431 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001432 rv = mtip_exec_internal_command(port,
1433 &fis,
1434 5,
1435 0,
1436 0,
1437 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001438 GFP_ATOMIC,
Asai Thambi S P670a6412014-04-16 20:27:54 -07001439 timeout);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001440 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1441 jiffies_to_msecs(jiffies - start));
1442 if (rv)
1443 dev_warn(&port->dd->pdev->dev,
1444 "STANDBY IMMEDIATE command failed.\n");
1445
1446 return rv;
1447}
1448
1449/*
1450 * Issue a READ LOG EXT command to the device.
1451 *
1452 * @port pointer to the port structure.
1453 * @page page number to fetch
1454 * @buffer pointer to buffer
1455 * @buffer_dma dma address corresponding to @buffer
1456 * @sectors page length to fetch, in sectors
1457 *
1458 * return value
1459 * @rv return value from mtip_exec_internal_command()
1460 */
1461static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1462 dma_addr_t buffer_dma, unsigned int sectors)
1463{
1464 struct host_to_dev_fis fis;
1465
1466 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1467 fis.type = 0x27;
1468 fis.opts = 1 << 7;
1469 fis.command = ATA_CMD_READ_LOG_EXT;
1470 fis.sect_count = sectors & 0xFF;
1471 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1472 fis.lba_low = page;
1473 fis.lba_mid = 0;
1474 fis.device = ATA_DEVICE_OBS;
1475
1476 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1477
1478 return mtip_exec_internal_command(port,
1479 &fis,
1480 5,
1481 buffer_dma,
1482 sectors * ATA_SECT_SIZE,
1483 0,
1484 GFP_ATOMIC,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001485 MTIP_INT_CMD_TIMEOUT_MS);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001486}
1487
1488/*
1489 * Issue a SMART READ DATA command to the device.
1490 *
1491 * @port pointer to the port structure.
1492 * @buffer pointer to buffer
1493 * @buffer_dma dma address corresponding to @buffer
1494 *
1495 * return value
1496 * @rv return value from mtip_exec_internal_command()
1497 */
1498static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1499 dma_addr_t buffer_dma)
1500{
1501 struct host_to_dev_fis fis;
1502
1503 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1504 fis.type = 0x27;
1505 fis.opts = 1 << 7;
1506 fis.command = ATA_CMD_SMART;
1507 fis.features = 0xD0;
1508 fis.sect_count = 1;
1509 fis.lba_mid = 0x4F;
1510 fis.lba_hi = 0xC2;
1511 fis.device = ATA_DEVICE_OBS;
1512
1513 return mtip_exec_internal_command(port,
1514 &fis,
1515 5,
1516 buffer_dma,
1517 ATA_SECT_SIZE,
1518 0,
1519 GFP_ATOMIC,
1520 15000);
1521}
1522
1523/*
1524 * Get the value of a smart attribute
1525 *
1526 * @port pointer to the port structure
1527 * @id attribute number
1528 * @attrib pointer to return attrib information corresponding to @id
1529 *
1530 * return value
1531 * -EINVAL NULL buffer passed or unsupported attribute @id.
1532 * -EPERM Identify data not valid, SMART not supported or not enabled
1533 */
1534static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1535 struct smart_attr *attrib)
1536{
1537 int rv, i;
1538 struct smart_attr *pattr;
1539
1540 if (!attrib)
1541 return -EINVAL;
1542
1543 if (!port->identify_valid) {
1544 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1545 return -EPERM;
1546 }
1547 if (!(port->identify[82] & 0x1)) {
1548 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1549 return -EPERM;
1550 }
1551 if (!(port->identify[85] & 0x1)) {
1552 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1553 return -EPERM;
1554 }
1555
1556 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1557 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1558 if (rv) {
1559 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1560 return rv;
1561 }
1562
1563 pattr = (struct smart_attr *)(port->smart_buf + 2);
1564 for (i = 0; i < 29; i++, pattr++)
1565 if (pattr->attr_id == id) {
1566 memcpy(attrib, pattr, sizeof(struct smart_attr));
1567 break;
1568 }
1569
1570 if (i == 29) {
1571 dev_warn(&port->dd->pdev->dev,
1572 "Query for invalid SMART attribute ID\n");
1573 rv = -EINVAL;
1574 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001575
Sam Bradshaw88523a62011-08-30 08:34:26 -06001576 return rv;
1577}
1578
1579/*
Asai Thambi S P15283462013-01-11 14:41:34 +01001580 * Trim unused sectors
1581 *
1582 * @dd pointer to driver_data structure
1583 * @lba starting lba
1584 * @len # of 512b sectors to trim
1585 *
1586 * return value
1587 * -ENOMEM Out of dma memory
1588 * -EINVAL Invalid parameters passed in, trim not supported
1589 * -EIO Error submitting trim request to hw
1590 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301591static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
1592 unsigned int len)
Asai Thambi S P15283462013-01-11 14:41:34 +01001593{
1594 int i, rv = 0;
1595 u64 tlba, tlen, sect_left;
1596 struct mtip_trim_entry *buf;
1597 dma_addr_t dma_addr;
1598 struct host_to_dev_fis fis;
1599
1600 if (!len || dd->trim_supp == false)
1601 return -EINVAL;
1602
1603 /* Trim request too big */
1604 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1605
1606 /* Trim request not aligned on 4k boundary */
1607 WARN_ON(len % 8 != 0);
1608
1609 /* Warn if vu_trim structure is too big */
1610 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1611
1612 /* Allocate a DMA buffer for the trim structure */
1613 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1614 GFP_KERNEL);
1615 if (!buf)
1616 return -ENOMEM;
1617 memset(buf, 0, ATA_SECT_SIZE);
1618
1619 for (i = 0, sect_left = len, tlba = lba;
1620 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1621 i++) {
1622 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1623 MTIP_MAX_TRIM_ENTRY_LEN :
1624 sect_left);
1625 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1626 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1627 tlba += tlen;
1628 sect_left -= tlen;
1629 }
1630 WARN_ON(sect_left != 0);
1631
1632 /* Build the fis */
1633 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1634 fis.type = 0x27;
1635 fis.opts = 1 << 7;
1636 fis.command = 0xfb;
1637 fis.features = 0x60;
1638 fis.sect_count = 1;
1639 fis.device = ATA_DEVICE_OBS;
1640
1641 if (mtip_exec_internal_command(dd->port,
1642 &fis,
1643 5,
1644 dma_addr,
1645 ATA_SECT_SIZE,
1646 0,
1647 GFP_KERNEL,
1648 MTIP_TRIM_TIMEOUT_MS) < 0)
1649 rv = -EIO;
1650
1651 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1652 return rv;
1653}
1654
1655/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001656 * Get the drive capacity.
1657 *
1658 * @dd Pointer to the device data structure.
1659 * @sectors Pointer to the variable that will receive the sector count.
1660 *
1661 * return value
1662 * 1 Capacity was returned successfully.
1663 * 0 The identify information is invalid.
1664 */
Jens Axboe63166682011-09-27 21:27:43 -06001665static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001666{
1667 struct mtip_port *port = dd->port;
1668 u64 total, raw0, raw1, raw2, raw3;
1669 raw0 = port->identify[100];
1670 raw1 = port->identify[101];
1671 raw2 = port->identify[102];
1672 raw3 = port->identify[103];
1673 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1674 *sectors = total;
1675 return (bool) !!port->identify_valid;
1676}
1677
1678/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001679 * Display the identify command data.
1680 *
1681 * @port Pointer to the port data structure.
1682 *
1683 * return value
1684 * None
1685 */
1686static void mtip_dump_identify(struct mtip_port *port)
1687{
1688 sector_t sectors;
1689 unsigned short revid;
1690 char cbuf[42];
1691
1692 if (!port->identify_valid)
1693 return;
1694
1695 strlcpy(cbuf, (char *)(port->identify+10), 21);
1696 dev_info(&port->dd->pdev->dev,
1697 "Serial No.: %s\n", cbuf);
1698
1699 strlcpy(cbuf, (char *)(port->identify+23), 9);
1700 dev_info(&port->dd->pdev->dev,
1701 "Firmware Ver.: %s\n", cbuf);
1702
1703 strlcpy(cbuf, (char *)(port->identify+27), 41);
1704 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1705
Sam Bradshaw26d58052013-10-03 10:18:05 -07001706 dev_info(&port->dd->pdev->dev, "Security: %04x %s\n",
1707 port->identify[128],
1708 port->identify[128] & 0x4 ? "(LOCKED)" : "");
1709
Sam Bradshaw88523a62011-08-30 08:34:26 -06001710 if (mtip_hw_get_capacity(port->dd, &sectors))
1711 dev_info(&port->dd->pdev->dev,
1712 "Capacity: %llu sectors (%llu MB)\n",
1713 (u64)sectors,
1714 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1715
1716 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001717 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001718 case 0x1:
1719 strlcpy(cbuf, "A0", 3);
1720 break;
1721 case 0x3:
1722 strlcpy(cbuf, "A2", 3);
1723 break;
1724 default:
1725 strlcpy(cbuf, "?", 2);
1726 break;
1727 }
1728 dev_info(&port->dd->pdev->dev,
1729 "Card Type: %s\n", cbuf);
1730}
1731
1732/*
1733 * Map the commands scatter list into the command table.
1734 *
1735 * @command Pointer to the command.
1736 * @nents Number of scatter list entries.
1737 *
1738 * return value
1739 * None
1740 */
1741static inline void fill_command_sg(struct driver_data *dd,
1742 struct mtip_cmd *command,
1743 int nents)
1744{
1745 int n;
1746 unsigned int dma_len;
1747 struct mtip_cmd_sg *command_sg;
1748 struct scatterlist *sg = command->sg;
1749
1750 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1751
1752 for (n = 0; n < nents; n++) {
1753 dma_len = sg_dma_len(sg);
1754 if (dma_len > 0x400000)
1755 dev_err(&dd->pdev->dev,
1756 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001757 command_sg->info = __force_bit2int
1758 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1759 command_sg->dba = __force_bit2int
1760 cpu_to_le32(sg_dma_address(sg));
1761 command_sg->dba_upper = __force_bit2int
1762 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001763 command_sg++;
1764 sg++;
1765 }
1766}
1767
1768/*
1769 * @brief Execute a drive command.
1770 *
1771 * return value 0 The command completed successfully.
1772 * return value -1 An error occurred while executing the command.
1773 */
Jens Axboe63166682011-09-27 21:27:43 -06001774static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001775{
1776 struct host_to_dev_fis fis;
1777 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001778 unsigned int to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001779
Sam Bradshaw88523a62011-08-30 08:34:26 -06001780 /* Build the FIS. */
1781 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1782 fis.type = 0x27;
1783 fis.opts = 1 << 7;
1784 fis.command = command[0];
1785 fis.features = command[1];
1786 fis.sect_count = command[2];
1787 fis.sector = command[3];
1788 fis.cyl_low = command[4];
1789 fis.cyl_hi = command[5];
1790 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1791
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001792 mtip_set_timeout(port->dd, &fis, &to, 0);
1793
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001794 dbg_printk(MTIP_DRV_NAME " %s: User Command: cmd %x, feat %x, nsect %x, sect %x, lcyl %x, hcyl %x, sel %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001795 __func__,
1796 command[0],
1797 command[1],
1798 command[2],
1799 command[3],
1800 command[4],
1801 command[5],
1802 command[6]);
1803
1804 /* Execute the command. */
1805 if (mtip_exec_internal_command(port,
1806 &fis,
1807 5,
1808 0,
1809 0,
1810 0,
1811 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001812 to) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001813 return -1;
1814 }
1815
1816 command[0] = reply->command; /* Status*/
1817 command[1] = reply->features; /* Error*/
1818 command[4] = reply->cyl_low;
1819 command[5] = reply->cyl_hi;
1820
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001821 dbg_printk(MTIP_DRV_NAME " %s: Completion Status: stat %x, err %x , cyl_lo %x cyl_hi %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001822 __func__,
1823 command[0],
1824 command[1],
1825 command[4],
1826 command[5]);
1827
Sam Bradshaw88523a62011-08-30 08:34:26 -06001828 return 0;
1829}
1830
1831/*
1832 * @brief Execute a drive command.
1833 *
1834 * @param port Pointer to the port data structure.
1835 * @param command Pointer to the user specified command parameters.
1836 * @param user_buffer Pointer to the user space buffer where read sector
1837 * data should be copied.
1838 *
1839 * return value 0 The command completed successfully.
1840 * return value -EFAULT An error occurred while copying the completion
1841 * data to the user space buffer.
1842 * return value -1 An error occurred while executing the command.
1843 */
Jens Axboe63166682011-09-27 21:27:43 -06001844static int exec_drive_command(struct mtip_port *port, u8 *command,
1845 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001846{
1847 struct host_to_dev_fis fis;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001848 struct host_to_dev_fis *reply;
1849 u8 *buf = NULL;
1850 dma_addr_t dma_addr = 0;
1851 int rv = 0, xfer_sz = command[3];
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001852 unsigned int to;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001853
1854 if (xfer_sz) {
David Milburn97651ea2012-09-12 14:06:12 -05001855 if (!user_buffer)
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001856 return -EFAULT;
1857
1858 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1859 ATA_SECT_SIZE * xfer_sz,
1860 &dma_addr,
1861 GFP_KERNEL);
1862 if (!buf) {
1863 dev_err(&port->dd->pdev->dev,
1864 "Memory allocation failed (%d bytes)\n",
1865 ATA_SECT_SIZE * xfer_sz);
1866 return -ENOMEM;
1867 }
1868 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1869 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001870
Sam Bradshaw88523a62011-08-30 08:34:26 -06001871 /* Build the FIS. */
1872 memset(&fis, 0, sizeof(struct host_to_dev_fis));
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001873 fis.type = 0x27;
1874 fis.opts = 1 << 7;
1875 fis.command = command[0];
Sam Bradshaw88523a62011-08-30 08:34:26 -06001876 fis.features = command[2];
1877 fis.sect_count = command[3];
1878 if (fis.command == ATA_CMD_SMART) {
1879 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001880 fis.cyl_low = 0x4F;
1881 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001882 }
1883
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001884 mtip_set_timeout(port->dd, &fis, &to, 0);
1885
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001886 if (xfer_sz)
1887 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1888 else
1889 reply = (port->rxfis + RX_FIS_D2H_REG);
1890
Sam Bradshaw88523a62011-08-30 08:34:26 -06001891 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001892 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06001893 "feat %x, sectcnt %x\n",
1894 __func__,
1895 command[0],
1896 command[1],
1897 command[2],
1898 command[3]);
1899
Sam Bradshaw88523a62011-08-30 08:34:26 -06001900 /* Execute the command. */
1901 if (mtip_exec_internal_command(port,
1902 &fis,
1903 5,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001904 (xfer_sz ? dma_addr : 0),
1905 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
Sam Bradshaw88523a62011-08-30 08:34:26 -06001906 0,
1907 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001908 to)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001909 < 0) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001910 rv = -EFAULT;
1911 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001912 }
1913
1914 /* Collect the completion status. */
1915 command[0] = reply->command; /* Status*/
1916 command[1] = reply->features; /* Error*/
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001917 command[2] = reply->sect_count;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001918
1919 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001920 " %s: Completion Status: stat %x, "
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001921 "err %x, nsect %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001922 __func__,
1923 command[0],
1924 command[1],
1925 command[2]);
1926
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001927 if (xfer_sz) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001928 if (copy_to_user(user_buffer,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001929 buf,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001930 ATA_SECT_SIZE * command[3])) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001931 rv = -EFAULT;
1932 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001933 }
1934 }
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001935exit_drive_command:
1936 if (buf)
1937 dmam_free_coherent(&port->dd->pdev->dev,
1938 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1939 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001940}
1941
1942/*
1943 * Indicates whether a command has a single sector payload.
1944 *
1945 * @command passed to the device to perform the certain event.
1946 * @features passed to the device to perform the certain event.
1947 *
1948 * return value
1949 * 1 command is one that always has a single sector payload,
1950 * regardless of the value in the Sector Count field.
1951 * 0 otherwise
1952 *
1953 */
1954static unsigned int implicit_sector(unsigned char command,
1955 unsigned char features)
1956{
1957 unsigned int rv = 0;
1958
1959 /* list of commands that have an implicit sector count of 1 */
1960 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001961 case ATA_CMD_SEC_SET_PASS:
1962 case ATA_CMD_SEC_UNLOCK:
1963 case ATA_CMD_SEC_ERASE_PREP:
1964 case ATA_CMD_SEC_ERASE_UNIT:
1965 case ATA_CMD_SEC_FREEZE_LOCK:
1966 case ATA_CMD_SEC_DISABLE_PASS:
1967 case ATA_CMD_PMP_READ:
1968 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001969 rv = 1;
1970 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001971 case ATA_CMD_SET_MAX:
1972 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001973 rv = 1;
1974 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001975 case ATA_CMD_SMART:
1976 if ((features == ATA_SMART_READ_VALUES) ||
1977 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001978 rv = 1;
1979 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001980 case ATA_CMD_CONF_OVERLAY:
1981 if ((features == ATA_DCO_IDENTIFY) ||
1982 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001983 rv = 1;
1984 break;
1985 }
1986 return rv;
1987}
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07001988
Sam Bradshaw88523a62011-08-30 08:34:26 -06001989/*
1990 * Executes a taskfile
1991 * See ide_taskfile_ioctl() for derivation
1992 */
1993static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06001994 void __user *buf,
1995 ide_task_request_t *req_task,
1996 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001997{
1998 struct host_to_dev_fis fis;
1999 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002000 u8 *outbuf = NULL;
2001 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06002002 dma_addr_t outbuf_dma = 0;
2003 dma_addr_t inbuf_dma = 0;
2004 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002005 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002006 unsigned int taskin = 0;
2007 unsigned int taskout = 0;
2008 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002009 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002010 unsigned int force_single_sector;
2011 unsigned int transfer_size;
2012 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06002013 int intotal = outtotal + req_task->out_size;
Selvan Mani4453bc82012-09-27 14:36:43 +02002014 int erasemode = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002015
2016 taskout = req_task->out_size;
2017 taskin = req_task->in_size;
2018 /* 130560 = 512 * 0xFF*/
2019 if (taskin > 130560 || taskout > 130560) {
2020 err = -EINVAL;
2021 goto abort;
2022 }
2023
2024 if (taskout) {
Al Viro8ed60102016-01-02 14:56:33 -05002025 outbuf = memdup_user(buf + outtotal, taskout);
2026 if (IS_ERR(outbuf)) {
2027 err = PTR_ERR(outbuf);
2028 outbuf = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002029 goto abort;
2030 }
2031 outbuf_dma = pci_map_single(dd->pdev,
2032 outbuf,
2033 taskout,
2034 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002035 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002036 err = -ENOMEM;
2037 goto abort;
2038 }
2039 dma_buffer = outbuf_dma;
2040 }
2041
2042 if (taskin) {
Al Viro8ed60102016-01-02 14:56:33 -05002043 inbuf = memdup_user(buf + intotal, taskin);
2044 if (IS_ERR(inbuf)) {
2045 err = PTR_ERR(inbuf);
2046 inbuf = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002047 goto abort;
2048 }
2049 inbuf_dma = pci_map_single(dd->pdev,
2050 inbuf,
2051 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002052 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002053 err = -ENOMEM;
2054 goto abort;
2055 }
2056 dma_buffer = inbuf_dma;
2057 }
2058
2059 /* only supports PIO and non-data commands from this ioctl. */
2060 switch (req_task->data_phase) {
2061 case TASKFILE_OUT:
2062 nsect = taskout / ATA_SECT_SIZE;
2063 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2064 break;
2065 case TASKFILE_IN:
2066 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2067 break;
2068 case TASKFILE_NO_DATA:
2069 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2070 break;
2071 default:
2072 err = -EINVAL;
2073 goto abort;
2074 }
2075
Sam Bradshaw88523a62011-08-30 08:34:26 -06002076 /* Build the FIS. */
2077 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2078
2079 fis.type = 0x27;
2080 fis.opts = 1 << 7;
2081 fis.command = req_task->io_ports[7];
2082 fis.features = req_task->io_ports[1];
2083 fis.sect_count = req_task->io_ports[2];
2084 fis.lba_low = req_task->io_ports[3];
2085 fis.lba_mid = req_task->io_ports[4];
2086 fis.lba_hi = req_task->io_ports[5];
2087 /* Clear the dev bit*/
2088 fis.device = req_task->io_ports[6] & ~0x10;
2089
2090 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2091 req_task->in_flags.all =
2092 IDE_TASKFILE_STD_IN_FLAGS |
2093 (IDE_HOB_STD_IN_FLAGS << 8);
2094 fis.lba_low_ex = req_task->hob_ports[3];
2095 fis.lba_mid_ex = req_task->hob_ports[4];
2096 fis.lba_hi_ex = req_task->hob_ports[5];
2097 fis.features_ex = req_task->hob_ports[1];
2098 fis.sect_cnt_ex = req_task->hob_ports[2];
2099
2100 } else {
2101 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2102 }
2103
2104 force_single_sector = implicit_sector(fis.command, fis.features);
2105
2106 if ((taskin || taskout) && (!fis.sect_count)) {
2107 if (nsect)
2108 fis.sect_count = nsect;
2109 else {
2110 if (!force_single_sector) {
2111 dev_warn(&dd->pdev->dev,
2112 "data movement but "
2113 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002114 err = -EINVAL;
2115 goto abort;
2116 }
2117 }
2118 }
2119
2120 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002121 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002122 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2123 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002124 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002125 fis.command,
2126 fis.features,
2127 fis.sect_count,
2128 fis.lba_low,
2129 fis.lba_mid,
2130 fis.lba_hi,
2131 fis.device);
2132
Selvan Mani4453bc82012-09-27 14:36:43 +02002133 /* check for erase mode support during secure erase.*/
Selvan Mani32087952012-11-07 06:03:37 -07002134 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2135 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
Selvan Mani4453bc82012-09-27 14:36:43 +02002136 erasemode = 1;
2137 }
2138
2139 mtip_set_timeout(dd, &fis, &timeout, erasemode);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002140
2141 /* Determine the correct transfer size.*/
2142 if (force_single_sector)
2143 transfer_size = ATA_SECT_SIZE;
2144 else
2145 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2146
2147 /* Execute the command.*/
2148 if (mtip_exec_internal_command(dd->port,
2149 &fis,
2150 5,
2151 dma_buffer,
2152 transfer_size,
2153 0,
2154 GFP_KERNEL,
2155 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002156 err = -EIO;
2157 goto abort;
2158 }
2159
2160 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2161
2162 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2163 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2164 req_task->io_ports[7] = reply->control;
2165 } else {
2166 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2167 req_task->io_ports[7] = reply->command;
2168 }
2169
2170 /* reclaim the DMA buffers.*/
2171 if (inbuf_dma)
2172 pci_unmap_single(dd->pdev, inbuf_dma,
2173 taskin, DMA_FROM_DEVICE);
2174 if (outbuf_dma)
2175 pci_unmap_single(dd->pdev, outbuf_dma,
2176 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002177 inbuf_dma = 0;
2178 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002179
2180 /* return the ATA registers to the caller.*/
2181 req_task->io_ports[1] = reply->features;
2182 req_task->io_ports[2] = reply->sect_count;
2183 req_task->io_ports[3] = reply->lba_low;
2184 req_task->io_ports[4] = reply->lba_mid;
2185 req_task->io_ports[5] = reply->lba_hi;
2186 req_task->io_ports[6] = reply->device;
2187
2188 if (req_task->out_flags.all & 1) {
2189
2190 req_task->hob_ports[3] = reply->lba_low_ex;
2191 req_task->hob_ports[4] = reply->lba_mid_ex;
2192 req_task->hob_ports[5] = reply->lba_hi_ex;
2193 req_task->hob_ports[1] = reply->features_ex;
2194 req_task->hob_ports[2] = reply->sect_cnt_ex;
2195 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002196 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002197 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002198 "err %x, sect_cnt %x, lbalo %x,"
2199 "lbamid %x, lbahi %x, dev %x\n",
2200 __func__,
2201 req_task->io_ports[7],
2202 req_task->io_ports[1],
2203 req_task->io_ports[2],
2204 req_task->io_ports[3],
2205 req_task->io_ports[4],
2206 req_task->io_ports[5],
2207 req_task->io_ports[6]);
2208
Sam Bradshaw88523a62011-08-30 08:34:26 -06002209 if (taskout) {
2210 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2211 err = -EFAULT;
2212 goto abort;
2213 }
2214 }
2215 if (taskin) {
2216 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2217 err = -EFAULT;
2218 goto abort;
2219 }
2220 }
2221abort:
2222 if (inbuf_dma)
2223 pci_unmap_single(dd->pdev, inbuf_dma,
2224 taskin, DMA_FROM_DEVICE);
2225 if (outbuf_dma)
2226 pci_unmap_single(dd->pdev, outbuf_dma,
2227 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002228 kfree(outbuf);
2229 kfree(inbuf);
2230
2231 return err;
2232}
2233
2234/*
2235 * Handle IOCTL calls from the Block Layer.
2236 *
2237 * This function is called by the Block Layer when it receives an IOCTL
2238 * command that it does not understand. If the IOCTL command is not supported
2239 * this function returns -ENOTTY.
2240 *
2241 * @dd Pointer to the driver data structure.
2242 * @cmd IOCTL command passed from the Block Layer.
2243 * @arg IOCTL argument passed from the Block Layer.
2244 *
2245 * return value
2246 * 0 The IOCTL completed successfully.
2247 * -ENOTTY The specified command is not supported.
2248 * -EFAULT An error occurred copying data to a user space buffer.
2249 * -EIO An error occurred while executing the command.
2250 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002251static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2252 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002253{
2254 switch (cmd) {
2255 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002256 {
2257 if (copy_to_user((void __user *)arg, dd->port->identify,
2258 sizeof(u16) * ATA_ID_WORDS))
2259 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002260 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002261 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002262 case HDIO_DRIVE_CMD:
2263 {
2264 u8 drive_command[4];
2265
2266 /* Copy the user command info to our buffer. */
2267 if (copy_from_user(drive_command,
2268 (void __user *) arg,
2269 sizeof(drive_command)))
2270 return -EFAULT;
2271
2272 /* Execute the drive command. */
2273 if (exec_drive_command(dd->port,
2274 drive_command,
2275 (void __user *) (arg+4)))
2276 return -EIO;
2277
2278 /* Copy the status back to the users buffer. */
2279 if (copy_to_user((void __user *) arg,
2280 drive_command,
2281 sizeof(drive_command)))
2282 return -EFAULT;
2283
2284 break;
2285 }
2286 case HDIO_DRIVE_TASK:
2287 {
2288 u8 drive_command[7];
2289
2290 /* Copy the user command info to our buffer. */
2291 if (copy_from_user(drive_command,
2292 (void __user *) arg,
2293 sizeof(drive_command)))
2294 return -EFAULT;
2295
2296 /* Execute the drive command. */
2297 if (exec_drive_task(dd->port, drive_command))
2298 return -EIO;
2299
2300 /* Copy the status back to the users buffer. */
2301 if (copy_to_user((void __user *) arg,
2302 drive_command,
2303 sizeof(drive_command)))
2304 return -EFAULT;
2305
2306 break;
2307 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002308 case HDIO_DRIVE_TASKFILE: {
2309 ide_task_request_t req_task;
2310 int ret, outtotal;
2311
2312 if (copy_from_user(&req_task, (void __user *) arg,
2313 sizeof(req_task)))
2314 return -EFAULT;
2315
2316 outtotal = sizeof(req_task);
2317
2318 ret = exec_drive_taskfile(dd, (void __user *) arg,
2319 &req_task, outtotal);
2320
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002321 if (copy_to_user((void __user *) arg, &req_task,
2322 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002323 return -EFAULT;
2324
2325 return ret;
2326 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002327
2328 default:
2329 return -EINVAL;
2330 }
2331 return 0;
2332}
2333
2334/*
2335 * Submit an IO to the hw
2336 *
2337 * This function is called by the block layer to issue an io
2338 * to the device. Upon completion, the callback function will
2339 * be called with the data parameter passed as the callback data.
2340 *
2341 * @dd Pointer to the driver data structure.
2342 * @start First sector to read.
2343 * @nsect Number of sectors to read.
2344 * @nents Number of entries in scatter list for the read command.
2345 * @tag The tag of this read command.
2346 * @callback Pointer to the function that should be called
2347 * when the read completes.
2348 * @data Callback data passed to the callback function
2349 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002350 * @dir Direction (read or write)
2351 *
2352 * return value
2353 * None
2354 */
Jens Axboeffc771b2014-05-09 09:42:02 -06002355static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq,
2356 struct mtip_cmd *command, int nents,
2357 struct blk_mq_hw_ctx *hctx)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002358{
2359 struct host_to_dev_fis *fis;
2360 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -06002361 int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2362 u64 start = blk_rq_pos(rq);
2363 unsigned int nsect = blk_rq_sectors(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002364
2365 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002366 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002367
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002368 prefetch(&port->flags);
2369
Sam Bradshaw88523a62011-08-30 08:34:26 -06002370 command->scatter_ents = nents;
2371
2372 /*
2373 * The number of retries for this command before it is
2374 * reported as a failure to the upper layers.
2375 */
2376 command->retries = MTIP_MAX_RETRIES;
2377
2378 /* Fill out fis */
2379 fis = command->command;
2380 fis->type = 0x27;
2381 fis->opts = 1 << 7;
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002382 if (dma_dir == DMA_FROM_DEVICE)
Jens Axboeffc771b2014-05-09 09:42:02 -06002383 fis->command = ATA_CMD_FPDMA_READ;
2384 else
2385 fis->command = ATA_CMD_FPDMA_WRITE;
Selvan Manieda45312012-11-07 06:03:53 -07002386 fis->lba_low = start & 0xFF;
2387 fis->lba_mid = (start >> 8) & 0xFF;
2388 fis->lba_hi = (start >> 16) & 0xFF;
2389 fis->lba_low_ex = (start >> 24) & 0xFF;
2390 fis->lba_mid_ex = (start >> 32) & 0xFF;
2391 fis->lba_hi_ex = (start >> 40) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002392 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002393 fis->features = nsect & 0xFF;
2394 fis->features_ex = (nsect >> 8) & 0xFF;
Jens Axboeffc771b2014-05-09 09:42:02 -06002395 fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002396 fis->sect_cnt_ex = 0;
2397 fis->control = 0;
2398 fis->res2 = 0;
2399 fis->res3 = 0;
2400 fill_command_sg(dd, command, nents);
2401
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002402 if (unlikely(command->unaligned))
Asai Thambi S P2077d942013-04-29 21:19:49 +02002403 fis->device |= 1 << 7;
2404
Sam Bradshaw88523a62011-08-30 08:34:26 -06002405 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002406 command->command_header->opts =
2407 __force_bit2int cpu_to_le32(
2408 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002409 command->command_header->byte_count = 0;
2410
2411 /*
2412 * Set the completion function and data for the command
2413 * within this layer.
2414 */
2415 command->comp_data = dd;
2416 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002417 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002418
2419 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002420 * To prevent this command from being issued
2421 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002422 */
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002423 if (unlikely(port->flags & MTIP_PF_PAUSE_IO)) {
Jens Axboeffc771b2014-05-09 09:42:02 -06002424 set_bit(rq->tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002425 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002426 return;
2427 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002428
2429 /* Issue the command to the hardware */
Jens Axboeffc771b2014-05-09 09:42:02 -06002430 mtip_issue_ncq_command(port, rq->tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002431}
2432
2433/*
Asai Thambi S P7412ff12012-06-04 12:43:03 -07002434 * Sysfs status dump.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002435 *
2436 * @dev Pointer to the device structure, passed by the kernrel.
2437 * @attr Pointer to the device_attribute structure passed by the kernel.
2438 * @buf Pointer to the char buffer that will receive the stats info.
2439 *
2440 * return value
2441 * The size, in bytes, of the data copied into buf.
2442 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002443static ssize_t mtip_hw_show_status(struct device *dev,
2444 struct device_attribute *attr,
2445 char *buf)
2446{
2447 struct driver_data *dd = dev_to_disk(dev)->private_data;
2448 int size = 0;
2449
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002450 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002451 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002452 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002453 size += sprintf(buf, "%s", "write_protect\n");
2454 else
2455 size += sprintf(buf, "%s", "online\n");
2456
2457 return size;
2458}
2459
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002460static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002461
Asai Thambi S P0caff002013-04-03 19:56:21 +05302462/* debugsfs entries */
2463
2464static ssize_t show_device_status(struct device_driver *drv, char *buf)
2465{
2466 int size = 0;
2467 struct driver_data *dd, *tmp;
2468 unsigned long flags;
2469 char id_buf[42];
2470 u16 status = 0;
2471
2472 spin_lock_irqsave(&dev_lock, flags);
2473 size += sprintf(&buf[size], "Devices Present:\n");
2474 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002475 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302476 if (dd->port &&
2477 dd->port->identify &&
2478 dd->port->identify_valid) {
2479 strlcpy(id_buf,
2480 (char *) (dd->port->identify + 10), 21);
2481 status = *(dd->port->identify + 141);
2482 } else {
2483 memset(id_buf, 0, 42);
2484 status = 0;
2485 }
2486
2487 if (dd->port &&
2488 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2489 size += sprintf(&buf[size],
2490 " device %s %s (ftl rebuild %d %%)\n",
2491 dev_name(&dd->pdev->dev),
2492 id_buf,
2493 status);
2494 } else {
2495 size += sprintf(&buf[size],
2496 " device %s %s\n",
2497 dev_name(&dd->pdev->dev),
2498 id_buf);
2499 }
2500 }
2501 }
2502
2503 size += sprintf(&buf[size], "Devices Being Removed:\n");
2504 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002505 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302506 if (dd->port &&
2507 dd->port->identify &&
2508 dd->port->identify_valid) {
2509 strlcpy(id_buf,
2510 (char *) (dd->port->identify+10), 21);
2511 status = *(dd->port->identify + 141);
2512 } else {
2513 memset(id_buf, 0, 42);
2514 status = 0;
2515 }
2516
2517 if (dd->port &&
2518 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2519 size += sprintf(&buf[size],
2520 " device %s %s (ftl rebuild %d %%)\n",
2521 dev_name(&dd->pdev->dev),
2522 id_buf,
2523 status);
2524 } else {
2525 size += sprintf(&buf[size],
2526 " device %s %s\n",
2527 dev_name(&dd->pdev->dev),
2528 id_buf);
2529 }
2530 }
2531 }
2532 spin_unlock_irqrestore(&dev_lock, flags);
2533
2534 return size;
2535}
2536
2537static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2538 size_t len, loff_t *offset)
2539{
David Milburnc8afd0d2013-05-23 16:23:45 -05002540 struct driver_data *dd = (struct driver_data *)f->private_data;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302541 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002542 char *buf;
2543 int rv = 0;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302544
2545 if (!len || *offset)
2546 return 0;
2547
David Milburnc8afd0d2013-05-23 16:23:45 -05002548 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2549 if (!buf) {
2550 dev_err(&dd->pdev->dev,
2551 "Memory allocation: status buffer\n");
2552 return -ENOMEM;
2553 }
2554
Asai Thambi S P0caff002013-04-03 19:56:21 +05302555 size += show_device_status(NULL, buf);
2556
2557 *offset = size <= len ? size : len;
2558 size = copy_to_user(ubuf, buf, *offset);
2559 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002560 rv = -EFAULT;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302561
David Milburnc8afd0d2013-05-23 16:23:45 -05002562 kfree(buf);
2563 return rv ? rv : *offset;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302564}
2565
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002566static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2567 size_t len, loff_t *offset)
2568{
2569 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002570 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002571 u32 group_allocated;
2572 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002573 int n, rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002574
2575 if (!len || size)
2576 return 0;
2577
David Milburnc8afd0d2013-05-23 16:23:45 -05002578 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2579 if (!buf) {
2580 dev_err(&dd->pdev->dev,
2581 "Memory allocation: register buffer\n");
2582 return -ENOMEM;
2583 }
2584
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002585 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2586
2587 for (n = dd->slot_groups-1; n >= 0; n--)
2588 size += sprintf(&buf[size], "%08X ",
2589 readl(dd->port->s_active[n]));
2590
2591 size += sprintf(&buf[size], "]\n");
2592 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2593
2594 for (n = dd->slot_groups-1; n >= 0; n--)
2595 size += sprintf(&buf[size], "%08X ",
2596 readl(dd->port->cmd_issue[n]));
2597
2598 size += sprintf(&buf[size], "]\n");
2599 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2600
2601 for (n = dd->slot_groups-1; n >= 0; n--)
2602 size += sprintf(&buf[size], "%08X ",
2603 readl(dd->port->completed[n]));
2604
2605 size += sprintf(&buf[size], "]\n");
2606 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2607 readl(dd->port->mmio + PORT_IRQ_STAT));
2608 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2609 readl(dd->mmio + HOST_IRQ_STAT));
2610 size += sprintf(&buf[size], "\n");
2611
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002612 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2613
2614 for (n = dd->slot_groups-1; n >= 0; n--) {
2615 if (sizeof(long) > sizeof(u32))
2616 group_allocated =
2617 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2618 else
2619 group_allocated = dd->port->cmds_to_issue[n];
2620 size += sprintf(&buf[size], "%08X ", group_allocated);
2621 }
2622 size += sprintf(&buf[size], "]\n");
2623
2624 *offset = size <= len ? size : len;
2625 size = copy_to_user(ubuf, buf, *offset);
2626 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002627 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002628
David Milburnc8afd0d2013-05-23 16:23:45 -05002629 kfree(buf);
2630 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002631}
2632
2633static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2634 size_t len, loff_t *offset)
2635{
2636 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002637 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002638 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002639 int rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002640
2641 if (!len || size)
2642 return 0;
2643
David Milburnc8afd0d2013-05-23 16:23:45 -05002644 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2645 if (!buf) {
2646 dev_err(&dd->pdev->dev,
2647 "Memory allocation: flag buffer\n");
2648 return -ENOMEM;
2649 }
2650
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002651 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2652 dd->port->flags);
2653 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2654 dd->dd_flag);
2655
2656 *offset = size <= len ? size : len;
2657 size = copy_to_user(ubuf, buf, *offset);
2658 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002659 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002660
David Milburnc8afd0d2013-05-23 16:23:45 -05002661 kfree(buf);
2662 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002663}
2664
Asai Thambi S P0caff002013-04-03 19:56:21 +05302665static const struct file_operations mtip_device_status_fops = {
2666 .owner = THIS_MODULE,
2667 .open = simple_open,
2668 .read = mtip_hw_read_device_status,
2669 .llseek = no_llseek,
2670};
2671
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002672static const struct file_operations mtip_regs_fops = {
2673 .owner = THIS_MODULE,
2674 .open = simple_open,
2675 .read = mtip_hw_read_registers,
2676 .llseek = no_llseek,
2677};
2678
2679static const struct file_operations mtip_flags_fops = {
2680 .owner = THIS_MODULE,
2681 .open = simple_open,
2682 .read = mtip_hw_read_flags,
2683 .llseek = no_llseek,
2684};
2685
Sam Bradshaw88523a62011-08-30 08:34:26 -06002686/*
2687 * Create the sysfs related attributes.
2688 *
2689 * @dd Pointer to the driver data structure.
2690 * @kobj Pointer to the kobj for the block device.
2691 *
2692 * return value
2693 * 0 Operation completed successfully.
2694 * -EINVAL Invalid parameter.
2695 */
Jens Axboe63166682011-09-27 21:27:43 -06002696static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002697{
2698 if (!kobj || !dd)
2699 return -EINVAL;
2700
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002701 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2702 dev_warn(&dd->pdev->dev,
2703 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002704 return 0;
2705}
2706
2707/*
2708 * Remove the sysfs related attributes.
2709 *
2710 * @dd Pointer to the driver data structure.
2711 * @kobj Pointer to the kobj for the block device.
2712 *
2713 * return value
2714 * 0 Operation completed successfully.
2715 * -EINVAL Invalid parameter.
2716 */
Jens Axboe63166682011-09-27 21:27:43 -06002717static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002718{
2719 if (!kobj || !dd)
2720 return -EINVAL;
2721
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002722 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002723
2724 return 0;
2725}
2726
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002727static int mtip_hw_debugfs_init(struct driver_data *dd)
2728{
2729 if (!dfs_parent)
2730 return -1;
2731
2732 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2733 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2734 dev_warn(&dd->pdev->dev,
2735 "Error creating node %s under debugfs\n",
2736 dd->disk->disk_name);
2737 dd->dfs_node = NULL;
2738 return -1;
2739 }
2740
2741 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2742 &mtip_flags_fops);
2743 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2744 &mtip_regs_fops);
2745
2746 return 0;
2747}
2748
2749static void mtip_hw_debugfs_exit(struct driver_data *dd)
2750{
Sam Bradshaw974a51a2013-05-15 10:04:34 +02002751 if (dd->dfs_node)
2752 debugfs_remove_recursive(dd->dfs_node);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002753}
2754
Sam Bradshaw88523a62011-08-30 08:34:26 -06002755/*
2756 * Perform any init/resume time hardware setup
2757 *
2758 * @dd Pointer to the driver data structure.
2759 *
2760 * return value
2761 * None
2762 */
2763static inline void hba_setup(struct driver_data *dd)
2764{
2765 u32 hwdata;
2766 hwdata = readl(dd->mmio + HOST_HSORG);
2767
2768 /* interrupt bug workaround: use only 1 IS bit.*/
2769 writel(hwdata |
2770 HSORG_DISABLE_SLOTGRP_INTR |
2771 HSORG_DISABLE_SLOTGRP_PXIS,
2772 dd->mmio + HOST_HSORG);
2773}
2774
Asai Thambi S P2077d942013-04-29 21:19:49 +02002775static int mtip_device_unaligned_constrained(struct driver_data *dd)
2776{
2777 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
2778}
2779
Sam Bradshaw88523a62011-08-30 08:34:26 -06002780/*
2781 * Detect the details of the product, and store anything needed
2782 * into the driver data structure. This includes product type and
2783 * version and number of slot groups.
2784 *
2785 * @dd Pointer to the driver data structure.
2786 *
2787 * return value
2788 * None
2789 */
2790static void mtip_detect_product(struct driver_data *dd)
2791{
2792 u32 hwdata;
2793 unsigned int rev, slotgroups;
2794
2795 /*
2796 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2797 * info register:
2798 * [15:8] hardware/software interface rev#
2799 * [ 3] asic-style interface
2800 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2801 */
2802 hwdata = readl(dd->mmio + HOST_HSORG);
2803
2804 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2805 dd->slot_groups = 1;
2806
2807 if (hwdata & 0x8) {
2808 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2809 rev = (hwdata & HSORG_HWREV) >> 8;
2810 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2811 dev_info(&dd->pdev->dev,
2812 "ASIC-FPGA design, HS rev 0x%x, "
2813 "%i slot groups [%i slots]\n",
2814 rev,
2815 slotgroups,
2816 slotgroups * 32);
2817
2818 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2819 dev_warn(&dd->pdev->dev,
2820 "Warning: driver only supports "
2821 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2822 slotgroups = MTIP_MAX_SLOT_GROUPS;
2823 }
2824 dd->slot_groups = slotgroups;
2825 return;
2826 }
2827
2828 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2829}
2830
2831/*
2832 * Blocking wait for FTL rebuild to complete
2833 *
2834 * @dd Pointer to the DRIVER_DATA structure.
2835 *
2836 * return value
2837 * 0 FTL rebuild completed successfully
2838 * -EFAULT FTL rebuild error/timeout/interruption
2839 */
2840static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2841{
2842 unsigned long timeout, cnt = 0, start;
2843
2844 dev_warn(&dd->pdev->dev,
2845 "FTL rebuild in progress. Polling for completion.\n");
2846
2847 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002848 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2849
2850 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002851 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002852 &dd->dd_flag)))
2853 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002854 if (mtip_check_surprise_removal(dd->pdev))
2855 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002856
Sam Bradshaw88523a62011-08-30 08:34:26 -06002857 if (mtip_get_identify(dd->port, NULL) < 0)
2858 return -EFAULT;
2859
2860 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2861 MTIP_FTL_REBUILD_MAGIC) {
2862 ssleep(1);
2863 /* Print message every 3 minutes */
2864 if (cnt++ >= 180) {
2865 dev_warn(&dd->pdev->dev,
2866 "FTL rebuild in progress (%d secs).\n",
2867 jiffies_to_msecs(jiffies - start) / 1000);
2868 cnt = 0;
2869 }
2870 } else {
2871 dev_warn(&dd->pdev->dev,
2872 "FTL rebuild complete (%d secs).\n",
2873 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002874 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002875 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002876 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002877 } while (time_before(jiffies, timeout));
2878
2879 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002880 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002881 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2882 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002883 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002884}
2885
2886/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002887 * service thread to issue queued commands
2888 *
2889 * @data Pointer to the driver data structure.
2890 *
2891 * return value
2892 * 0
2893 */
2894
2895static int mtip_service_thread(void *data)
2896{
2897 struct driver_data *dd = (struct driver_data *)data;
2898 unsigned long slot, slot_start, slot_wrap;
2899 unsigned int num_cmd_slots = dd->slot_groups * 32;
2900 struct mtip_port *port = dd->port;
2901
2902 while (1) {
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002903 if (kthread_should_stop() ||
2904 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2905 goto st_out;
2906 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2907
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002908 /*
2909 * the condition is to check neither an internal command is
2910 * is in progress nor error handling is active
2911 */
2912 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi SPcfc05bd2016-02-24 21:16:00 -08002913 (port->flags & MTIP_PF_SVC_THD_WORK));
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002914
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002915 if (kthread_should_stop() ||
2916 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2917 goto st_out;
2918
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002919 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002920 &dd->dd_flag)))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002921 goto st_out;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002922
Asai Thambi SPcfc05bd2016-02-24 21:16:00 -08002923 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2924
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002925restart_eh:
2926 /* Demux bits: start with error handling */
2927 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) {
2928 mtip_handle_tfe(dd);
2929 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
2930 }
2931
2932 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags))
2933 goto restart_eh;
2934
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002935 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002936 slot = 1;
2937 /* used to restrict the loop to one iteration */
2938 slot_start = num_cmd_slots;
2939 slot_wrap = 0;
2940 while (1) {
2941 slot = find_next_bit(port->cmds_to_issue,
2942 num_cmd_slots, slot);
2943 if (slot_wrap == 1) {
2944 if ((slot_start >= slot) ||
2945 (slot >= num_cmd_slots))
2946 break;
2947 }
2948 if (unlikely(slot_start == num_cmd_slots))
2949 slot_start = slot;
2950
2951 if (unlikely(slot == num_cmd_slots)) {
2952 slot = 1;
2953 slot_wrap = 1;
2954 continue;
2955 }
2956
2957 /* Issue the command to the hardware */
2958 mtip_issue_ncq_command(port, slot);
2959
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002960 clear_bit(slot, port->cmds_to_issue);
2961 }
2962
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002963 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002964 }
2965
2966 if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002967 if (mtip_ftl_rebuild_poll(dd) < 0)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002968 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
Asai Thambi S P8182b492012-04-09 08:35:38 +02002969 &dd->dd_flag);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002970 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002971 }
2972 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002973
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002974st_out:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002975 return 0;
2976}
2977
2978/*
Sam Bradshaw188b9f42014-01-15 10:14:57 -08002979 * DMA region teardown
2980 *
2981 * @dd Pointer to driver_data structure
2982 *
2983 * return value
2984 * None
2985 */
2986static void mtip_dma_free(struct driver_data *dd)
2987{
Sam Bradshaw188b9f42014-01-15 10:14:57 -08002988 struct mtip_port *port = dd->port;
2989
2990 if (port->block1)
2991 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
2992 port->block1, port->block1_dma);
2993
2994 if (port->command_list) {
2995 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
2996 port->command_list, port->command_list_dma);
2997 }
Sam Bradshaw188b9f42014-01-15 10:14:57 -08002998}
2999
3000/*
3001 * DMA region setup
3002 *
3003 * @dd Pointer to driver_data structure
3004 *
3005 * return value
3006 * -ENOMEM Not enough free DMA region space to initialize driver
3007 */
3008static int mtip_dma_alloc(struct driver_data *dd)
3009{
3010 struct mtip_port *port = dd->port;
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003011
3012 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
3013 port->block1 =
3014 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3015 &port->block1_dma, GFP_KERNEL);
3016 if (!port->block1)
3017 return -ENOMEM;
3018 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
3019
3020 /* Allocate dma memory for command list */
3021 port->command_list =
3022 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3023 &port->command_list_dma, GFP_KERNEL);
3024 if (!port->command_list) {
3025 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3026 port->block1, port->block1_dma);
3027 port->block1 = NULL;
3028 port->block1_dma = 0;
3029 return -ENOMEM;
3030 }
3031 memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
3032
3033 /* Setup all pointers into first DMA region */
3034 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
3035 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
3036 port->identify = port->block1 + AHCI_IDFY_OFFSET;
3037 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
3038 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
3039 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
3040 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
3041 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
3042
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003043 return 0;
3044}
3045
Jens Axboeffc771b2014-05-09 09:42:02 -06003046static int mtip_hw_get_identify(struct driver_data *dd)
3047{
3048 struct smart_attr attr242;
3049 unsigned char *buf;
3050 int rv;
3051
3052 if (mtip_get_identify(dd->port, NULL) < 0)
3053 return -EFAULT;
3054
3055 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3056 MTIP_FTL_REBUILD_MAGIC) {
3057 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3058 return MTIP_FTL_REBUILD_MAGIC;
3059 }
3060 mtip_dump_identify(dd->port);
3061
3062 /* check write protect, over temp and rebuild statuses */
3063 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3064 dd->port->log_buf,
3065 dd->port->log_buf_dma, 1);
3066 if (rv) {
3067 dev_warn(&dd->pdev->dev,
3068 "Error in READ LOG EXT (10h) command\n");
3069 /* non-critical error, don't fail the load */
3070 } else {
3071 buf = (unsigned char *)dd->port->log_buf;
3072 if (buf[259] & 0x1) {
3073 dev_info(&dd->pdev->dev,
3074 "Write protect bit is set.\n");
3075 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3076 }
3077 if (buf[288] == 0xF7) {
3078 dev_info(&dd->pdev->dev,
3079 "Exceeded Tmax, drive in thermal shutdown.\n");
3080 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3081 }
3082 if (buf[288] == 0xBF) {
3083 dev_info(&dd->pdev->dev,
3084 "Drive indicates rebuild has failed.\n");
3085 /* TODO */
3086 }
3087 }
3088
3089 /* get write protect progess */
3090 memset(&attr242, 0, sizeof(struct smart_attr));
3091 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3092 dev_warn(&dd->pdev->dev,
3093 "Unable to check write protect progress\n");
3094 else
3095 dev_info(&dd->pdev->dev,
3096 "Write protect progress: %u%% (%u blocks)\n",
3097 attr242.cur, le32_to_cpu(attr242.data));
3098
3099 return rv;
3100}
3101
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003102/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003103 * Called once for each card.
3104 *
3105 * @dd Pointer to the driver data structure.
3106 *
3107 * return value
3108 * 0 on success, else an error code.
3109 */
Jens Axboe63166682011-09-27 21:27:43 -06003110static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003111{
3112 int i;
3113 int rv;
3114 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02003115 unsigned long timeout, timetaken;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003116
3117 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3118
3119 mtip_detect_product(dd);
3120 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3121 rv = -EIO;
3122 goto out1;
3123 }
3124 num_command_slots = dd->slot_groups * 32;
3125
3126 hba_setup(dd);
3127
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003128 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3129 dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003130 if (!dd->port) {
3131 dev_err(&dd->pdev->dev,
3132 "Memory allocation: port structure\n");
3133 return -ENOMEM;
3134 }
3135
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003136 /* Continue workqueue setup */
3137 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3138 dd->work[i].port = dd->port;
3139
Asai Thambi S P2077d942013-04-29 21:19:49 +02003140 /* Enable unaligned IO constraints for some devices */
3141 if (mtip_device_unaligned_constrained(dd))
3142 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3143 else
3144 dd->unal_qdepth = 0;
3145
Asai Thambi S P2077d942013-04-29 21:19:49 +02003146 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003147
3148 /* Spinlock to prevent concurrent issue */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003149 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3150 spin_lock_init(&dd->port->cmd_issue_lock[i]);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003151
3152 /* Set the port mmio base address. */
3153 dd->port->mmio = dd->mmio + PORT_OFFSET;
3154 dd->port->dd = dd;
3155
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003156 /* DMA allocations */
3157 rv = mtip_dma_alloc(dd);
3158 if (rv < 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003159 goto out1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003160
3161 /* Setup the pointers to the extended s_active and CI registers. */
3162 for (i = 0; i < dd->slot_groups; i++) {
3163 dd->port->s_active[i] =
3164 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3165 dd->port->cmd_issue[i] =
3166 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3167 dd->port->completed[i] =
3168 dd->port->mmio + i*0x80 + PORT_SDBV;
3169 }
3170
Asai Thambi S P45038362012-04-09 08:35:38 +02003171 timetaken = jiffies;
3172 timeout = jiffies + msecs_to_jiffies(30000);
3173 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3174 time_before(jiffies, timeout)) {
3175 mdelay(100);
3176 }
3177 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3178 timetaken = jiffies - timetaken;
3179 dev_warn(&dd->pdev->dev,
3180 "Surprise removal detected at %u ms\n",
3181 jiffies_to_msecs(timetaken));
3182 rv = -ENODEV;
3183 goto out2 ;
3184 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003185 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003186 timetaken = jiffies - timetaken;
3187 dev_warn(&dd->pdev->dev,
3188 "Removal detected at %u ms\n",
3189 jiffies_to_msecs(timetaken));
3190 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003191 goto out2;
3192 }
3193
Asai Thambi S P45038362012-04-09 08:35:38 +02003194 /* Conditionally reset the HBA. */
3195 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3196 if (mtip_hba_reset(dd) < 0) {
3197 dev_err(&dd->pdev->dev,
3198 "Card did not reset within timeout\n");
3199 rv = -EIO;
3200 goto out2;
3201 }
3202 } else {
3203 /* Clear any pending interrupts on the HBA */
3204 writel(readl(dd->mmio + HOST_IRQ_STAT),
3205 dd->mmio + HOST_IRQ_STAT);
3206 }
3207
Sam Bradshaw88523a62011-08-30 08:34:26 -06003208 mtip_init_port(dd->port);
3209 mtip_start_port(dd->port);
3210
3211 /* Setup the ISR and enable interrupts. */
3212 rv = devm_request_irq(&dd->pdev->dev,
3213 dd->pdev->irq,
3214 mtip_irq_handler,
3215 IRQF_SHARED,
3216 dev_driver_string(&dd->pdev->dev),
3217 dd);
3218
3219 if (rv) {
3220 dev_err(&dd->pdev->dev,
3221 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3222 goto out2;
3223 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003224 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003225
3226 /* Enable interrupts on the HBA. */
3227 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3228 dd->mmio + HOST_CTL);
3229
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003230 init_waitqueue_head(&dd->port->svc_wait);
3231
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003232 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003233 rv = -EFAULT;
3234 goto out3;
3235 }
3236
Sam Bradshaw88523a62011-08-30 08:34:26 -06003237 return rv;
3238
3239out3:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003240 /* Disable interrupts on the HBA. */
3241 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3242 dd->mmio + HOST_CTL);
3243
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003244 /* Release the IRQ. */
3245 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003246 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3247
3248out2:
3249 mtip_deinit_port(dd->port);
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003250 mtip_dma_free(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003251
Sam Bradshaw88523a62011-08-30 08:34:26 -06003252out1:
3253 /* Free the memory allocated for the for structure. */
3254 kfree(dd->port);
3255
3256 return rv;
3257}
3258
Jens Axboeffc771b2014-05-09 09:42:02 -06003259static void mtip_standby_drive(struct driver_data *dd)
3260{
3261 if (dd->sr)
3262 return;
3263
3264 /*
3265 * Send standby immediate (E0h) to the drive so that it
3266 * saves its state.
3267 */
3268 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
3269 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))
3270 if (mtip_standby_immediate(dd->port))
3271 dev_warn(&dd->pdev->dev,
3272 "STANDBY IMMEDIATE failed\n");
3273}
3274
Sam Bradshaw88523a62011-08-30 08:34:26 -06003275/*
3276 * Called to deinitialize an interface.
3277 *
3278 * @dd Pointer to the driver data structure.
3279 *
3280 * return value
3281 * 0
3282 */
Jens Axboe63166682011-09-27 21:27:43 -06003283static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003284{
3285 /*
3286 * Send standby immediate (E0h) to the drive so that it
3287 * saves its state.
3288 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003289 if (!dd->sr) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003290 /* de-initialize the port. */
3291 mtip_deinit_port(dd->port);
3292
3293 /* Disable interrupts on the HBA. */
3294 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3295 dd->mmio + HOST_CTL);
3296 }
3297
Sam Bradshaw88523a62011-08-30 08:34:26 -06003298 /* Release the IRQ. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003299 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003300 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
Asai Thambi SP2132a542015-05-11 15:53:18 -07003301 msleep(1000);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003302
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003303 /* Free dma regions */
3304 mtip_dma_free(dd);
3305
Sam Bradshaw88523a62011-08-30 08:34:26 -06003306 /* Free the memory allocated for the for structure. */
3307 kfree(dd->port);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003308 dd->port = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003309
3310 return 0;
3311}
3312
3313/*
3314 * Issue a Standby Immediate command to the device.
3315 *
3316 * This function is called by the Block Layer just before the
3317 * system powers off during a shutdown.
3318 *
3319 * @dd Pointer to the driver data structure.
3320 *
3321 * return value
3322 * 0
3323 */
Jens Axboe63166682011-09-27 21:27:43 -06003324static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003325{
3326 /*
3327 * Send standby immediate (E0h) to the drive so that it
3328 * saves its state.
3329 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003330 if (!dd->sr && dd->port)
3331 mtip_standby_immediate(dd->port);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003332
3333 return 0;
3334}
3335
3336/*
3337 * Suspend function
3338 *
3339 * This function is called by the Block Layer just before the
3340 * system hibernates.
3341 *
3342 * @dd Pointer to the driver data structure.
3343 *
3344 * return value
3345 * 0 Suspend was successful
3346 * -EFAULT Suspend was not successful
3347 */
Jens Axboe63166682011-09-27 21:27:43 -06003348static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003349{
3350 /*
3351 * Send standby immediate (E0h) to the drive
3352 * so that it saves its state.
3353 */
3354 if (mtip_standby_immediate(dd->port) != 0) {
3355 dev_err(&dd->pdev->dev,
3356 "Failed standby-immediate command\n");
3357 return -EFAULT;
3358 }
3359
3360 /* Disable interrupts on the HBA.*/
3361 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3362 dd->mmio + HOST_CTL);
3363 mtip_deinit_port(dd->port);
3364
3365 return 0;
3366}
3367
3368/*
3369 * Resume function
3370 *
3371 * This function is called by the Block Layer as the
3372 * system resumes.
3373 *
3374 * @dd Pointer to the driver data structure.
3375 *
3376 * return value
3377 * 0 Resume was successful
3378 * -EFAULT Resume was not successful
3379 */
Jens Axboe63166682011-09-27 21:27:43 -06003380static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003381{
3382 /* Perform any needed hardware setup steps */
3383 hba_setup(dd);
3384
3385 /* Reset the HBA */
3386 if (mtip_hba_reset(dd) != 0) {
3387 dev_err(&dd->pdev->dev,
3388 "Unable to reset the HBA\n");
3389 return -EFAULT;
3390 }
3391
3392 /*
3393 * Enable the port, DMA engine, and FIS reception specific
3394 * h/w in controller.
3395 */
3396 mtip_init_port(dd->port);
3397 mtip_start_port(dd->port);
3398
3399 /* Enable interrupts on the HBA.*/
3400 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3401 dd->mmio + HOST_CTL);
3402
3403 return 0;
3404}
3405
3406/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003407 * Helper function for reusing disk name
3408 * upon hot insertion.
3409 */
3410static int rssd_disk_name_format(char *prefix,
3411 int index,
3412 char *buf,
3413 int buflen)
3414{
3415 const int base = 'z' - 'a' + 1;
3416 char *begin = buf + strlen(prefix);
3417 char *end = buf + buflen;
3418 char *p;
3419 int unit;
3420
3421 p = end - 1;
3422 *p = '\0';
3423 unit = base;
3424 do {
3425 if (p == begin)
3426 return -EINVAL;
3427 *--p = 'a' + (index % unit);
3428 index = (index / unit) - 1;
3429 } while (index >= 0);
3430
3431 memmove(begin, p, end - p);
3432 memcpy(buf, prefix, strlen(prefix));
3433
3434 return 0;
3435}
3436
3437/*
3438 * Block layer IOCTL handler.
3439 *
3440 * @dev Pointer to the block_device structure.
3441 * @mode ignored
3442 * @cmd IOCTL command passed from the user application.
3443 * @arg Argument passed from the user application.
3444 *
3445 * return value
3446 * 0 IOCTL completed successfully.
3447 * -ENOTTY IOCTL not supported or invalid driver data
3448 * structure pointer.
3449 */
3450static int mtip_block_ioctl(struct block_device *dev,
3451 fmode_t mode,
3452 unsigned cmd,
3453 unsigned long arg)
3454{
3455 struct driver_data *dd = dev->bd_disk->private_data;
3456
3457 if (!capable(CAP_SYS_ADMIN))
3458 return -EACCES;
3459
3460 if (!dd)
3461 return -ENOTTY;
3462
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003463 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003464 return -ENOTTY;
3465
Sam Bradshaw88523a62011-08-30 08:34:26 -06003466 switch (cmd) {
3467 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003468 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003469 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003470 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003471 }
3472}
3473
Jens Axboe16d02c02011-09-27 15:50:01 -06003474#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003475/*
3476 * Block layer compat IOCTL handler.
3477 *
3478 * @dev Pointer to the block_device structure.
3479 * @mode ignored
3480 * @cmd IOCTL command passed from the user application.
3481 * @arg Argument passed from the user application.
3482 *
3483 * return value
3484 * 0 IOCTL completed successfully.
3485 * -ENOTTY IOCTL not supported or invalid driver data
3486 * structure pointer.
3487 */
3488static int mtip_block_compat_ioctl(struct block_device *dev,
3489 fmode_t mode,
3490 unsigned cmd,
3491 unsigned long arg)
3492{
3493 struct driver_data *dd = dev->bd_disk->private_data;
3494
3495 if (!capable(CAP_SYS_ADMIN))
3496 return -EACCES;
3497
3498 if (!dd)
3499 return -ENOTTY;
3500
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003501 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003502 return -ENOTTY;
3503
Sam Bradshaw88523a62011-08-30 08:34:26 -06003504 switch (cmd) {
3505 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003506 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003507 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003508 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003509 ide_task_request_t req_task;
3510 int compat_tasksize, outtotal, ret;
3511
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003512 compat_tasksize =
3513 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003514
3515 compat_req_task =
3516 (struct mtip_compat_ide_task_request_s __user *) arg;
3517
3518 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003519 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003520 return -EFAULT;
3521
3522 if (get_user(req_task.out_size, &compat_req_task->out_size))
3523 return -EFAULT;
3524
3525 if (get_user(req_task.in_size, &compat_req_task->in_size))
3526 return -EFAULT;
3527
3528 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3529
3530 ret = exec_drive_taskfile(dd, (void __user *) arg,
3531 &req_task, outtotal);
3532
3533 if (copy_to_user((void __user *) arg, &req_task,
3534 compat_tasksize -
3535 (2 * sizeof(compat_long_t))))
3536 return -EFAULT;
3537
3538 if (put_user(req_task.out_size, &compat_req_task->out_size))
3539 return -EFAULT;
3540
3541 if (put_user(req_task.in_size, &compat_req_task->in_size))
3542 return -EFAULT;
3543
3544 return ret;
3545 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003546 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003547 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003548 }
3549}
Jens Axboe16d02c02011-09-27 15:50:01 -06003550#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003551
3552/*
3553 * Obtain the geometry of the device.
3554 *
3555 * You may think that this function is obsolete, but some applications,
3556 * fdisk for example still used CHS values. This function describes the
3557 * device as having 224 heads and 56 sectors per cylinder. These values are
3558 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3559 * partition is described in terms of a start and end cylinder this means
3560 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3561 * affects performance.
3562 *
3563 * @dev Pointer to the block_device strucutre.
3564 * @geo Pointer to a hd_geometry structure.
3565 *
3566 * return value
3567 * 0 Operation completed successfully.
3568 * -ENOTTY An error occurred while reading the drive capacity.
3569 */
3570static int mtip_block_getgeo(struct block_device *dev,
3571 struct hd_geometry *geo)
3572{
3573 struct driver_data *dd = dev->bd_disk->private_data;
3574 sector_t capacity;
3575
3576 if (!dd)
3577 return -ENOTTY;
3578
3579 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3580 dev_warn(&dd->pdev->dev,
3581 "Could not get drive capacity.\n");
3582 return -ENOTTY;
3583 }
3584
3585 geo->heads = 224;
3586 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003587 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003588 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003589 return 0;
3590}
3591
3592/*
3593 * Block device operation function.
3594 *
3595 * This structure contains pointers to the functions required by the block
3596 * layer.
3597 */
3598static const struct block_device_operations mtip_block_ops = {
3599 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003600#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003601 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003602#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003603 .getgeo = mtip_block_getgeo,
3604 .owner = THIS_MODULE
3605};
3606
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003607static inline bool is_se_active(struct driver_data *dd)
3608{
3609 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT, &dd->port->flags))) {
3610 if (dd->port->ic_pause_timer) {
3611 unsigned long to = dd->port->ic_pause_timer +
3612 msecs_to_jiffies(1000);
3613 if (time_after(jiffies, to)) {
3614 clear_bit(MTIP_PF_SE_ACTIVE_BIT,
3615 &dd->port->flags);
3616 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
3617 dd->port->ic_pause_timer = 0;
3618 wake_up_interruptible(&dd->port->svc_wait);
3619 return false;
3620 }
3621 }
3622 return true;
3623 }
3624 return false;
3625}
3626
Sam Bradshaw88523a62011-08-30 08:34:26 -06003627/*
3628 * Block layer make request function.
3629 *
3630 * This function is called by the kernel to process a BIO for
3631 * the P320 device.
3632 *
3633 * @queue Pointer to the request queue. Unused other than to obtain
3634 * the driver data structure.
Jens Axboeffc771b2014-05-09 09:42:02 -06003635 * @rq Pointer to the request.
Sam Bradshaw88523a62011-08-30 08:34:26 -06003636 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003637 */
Jens Axboeffc771b2014-05-09 09:42:02 -06003638static int mtip_submit_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003639{
Jens Axboeffc771b2014-05-09 09:42:02 -06003640 struct driver_data *dd = hctx->queue->queuedata;
3641 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3642 unsigned int nents;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003643
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003644 if (is_se_active(dd))
3645 return -ENODATA;
3646
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003647 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3648 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3649 &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003650 return -ENXIO;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003651 }
3652 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003653 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003654 }
3655 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3656 &dd->dd_flag) &&
Jens Axboeffc771b2014-05-09 09:42:02 -06003657 rq_data_dir(rq))) {
3658 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003659 }
Jens Axboeffc771b2014-05-09 09:42:02 -06003660 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)))
3661 return -ENODATA;
3662 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag))
3663 return -ENXIO;
Asai Thambi S P45038362012-04-09 08:35:38 +02003664 }
3665
Jens Axboeffc771b2014-05-09 09:42:02 -06003666 if (rq->cmd_flags & REQ_DISCARD) {
3667 int err;
3668
3669 err = mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq));
Christoph Hellwigc8a446a2014-09-13 16:40:10 -07003670 blk_mq_end_request(rq, err);
Jens Axboeffc771b2014-05-09 09:42:02 -06003671 return 0;
Asai Thambi S P15283462013-01-11 14:41:34 +01003672 }
3673
Jens Axboeffc771b2014-05-09 09:42:02 -06003674 /* Create the scatter list for this request. */
3675 nents = blk_rq_map_sg(hctx->queue, rq, cmd->sg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003676
Jens Axboeffc771b2014-05-09 09:42:02 -06003677 /* Issue the read/write. */
3678 mtip_hw_submit_io(dd, rq, cmd, nents, hctx);
3679 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003680}
3681
Jens Axboeffc771b2014-05-09 09:42:02 -06003682static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx,
3683 struct request *rq)
3684{
3685 struct driver_data *dd = hctx->queue->queuedata;
3686 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3687
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003688 if (rq_data_dir(rq) == READ || !dd->unal_qdepth)
Jens Axboeffc771b2014-05-09 09:42:02 -06003689 return false;
3690
3691 /*
3692 * If unaligned depth must be limited on this controller, mark it
3693 * as unaligned if the IO isn't on a 4k boundary (start of length).
3694 */
3695 if (blk_rq_sectors(rq) <= 64) {
3696 if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7))
3697 cmd->unaligned = 1;
3698 }
3699
3700 if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal))
3701 return true;
3702
3703 return false;
3704}
3705
Jens Axboe74c45052014-10-29 11:14:52 -06003706static int mtip_queue_rq(struct blk_mq_hw_ctx *hctx,
3707 const struct blk_mq_queue_data *bd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003708{
Jens Axboe74c45052014-10-29 11:14:52 -06003709 struct request *rq = bd->rq;
Jens Axboeffc771b2014-05-09 09:42:02 -06003710 int ret;
3711
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003712 if (unlikely(mtip_check_unal_depth(hctx, rq)))
Jens Axboeffc771b2014-05-09 09:42:02 -06003713 return BLK_MQ_RQ_QUEUE_BUSY;
3714
Christoph Hellwige2490072014-09-13 16:40:09 -07003715 blk_mq_start_request(rq);
3716
Jens Axboeffc771b2014-05-09 09:42:02 -06003717 ret = mtip_submit_request(hctx, rq);
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003718 if (likely(!ret))
Jens Axboeffc771b2014-05-09 09:42:02 -06003719 return BLK_MQ_RQ_QUEUE_OK;
3720
3721 rq->errors = ret;
3722 return BLK_MQ_RQ_QUEUE_ERROR;
3723}
3724
3725static void mtip_free_cmd(void *data, struct request *rq,
3726 unsigned int hctx_idx, unsigned int request_idx)
3727{
3728 struct driver_data *dd = data;
3729 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3730
3731 if (!cmd->command)
3732 return;
3733
3734 dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3735 cmd->command, cmd->command_dma);
3736}
3737
3738static int mtip_init_cmd(void *data, struct request *rq, unsigned int hctx_idx,
3739 unsigned int request_idx, unsigned int numa_node)
3740{
3741 struct driver_data *dd = data;
3742 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3743 u32 host_cap_64 = readl(dd->mmio + HOST_CAP) & HOST_CAP_64;
3744
Jeff Moyer74c9c912015-07-29 10:22:50 -04003745 /*
3746 * For flush requests, request_idx starts at the end of the
3747 * tag space. Since we don't support FLUSH/FUA, simply return
3748 * 0 as there's nothing to be done.
3749 */
3750 if (request_idx >= MTIP_MAX_COMMAND_SLOTS)
3751 return 0;
3752
Jens Axboeffc771b2014-05-09 09:42:02 -06003753 cmd->command = dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3754 &cmd->command_dma, GFP_KERNEL);
3755 if (!cmd->command)
3756 return -ENOMEM;
3757
3758 memset(cmd->command, 0, CMD_DMA_ALLOC_SZ);
3759
3760 /* Point the command headers at the command tables. */
3761 cmd->command_header = dd->port->command_list +
3762 (sizeof(struct mtip_cmd_hdr) * request_idx);
3763 cmd->command_header_dma = dd->port->command_list_dma +
3764 (sizeof(struct mtip_cmd_hdr) * request_idx);
3765
3766 if (host_cap_64)
3767 cmd->command_header->ctbau = __force_bit2int cpu_to_le32((cmd->command_dma >> 16) >> 16);
3768
3769 cmd->command_header->ctba = __force_bit2int cpu_to_le32(cmd->command_dma & 0xFFFFFFFF);
3770
3771 sg_init_table(cmd->sg, MTIP_MAX_SG);
3772 return 0;
3773}
3774
3775static struct blk_mq_ops mtip_mq_ops = {
3776 .queue_rq = mtip_queue_rq,
3777 .map_queue = blk_mq_map_queue,
Jens Axboeffc771b2014-05-09 09:42:02 -06003778 .init_request = mtip_init_cmd,
3779 .exit_request = mtip_free_cmd,
3780};
3781
Sam Bradshaw88523a62011-08-30 08:34:26 -06003782/*
3783 * Block layer initialization function.
3784 *
3785 * This function is called once by the PCI layer for each P320
3786 * device that is connected to the system.
3787 *
3788 * @dd Pointer to the driver data structure.
3789 *
3790 * return value
3791 * 0 on success else an error code.
3792 */
Jens Axboe63166682011-09-27 21:27:43 -06003793static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003794{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003795 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003796 sector_t capacity;
3797 unsigned int index = 0;
3798 struct kobject *kobj;
3799
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003800 if (dd->disk)
3801 goto skip_create_disk; /* hw init done, before rebuild */
3802
Jens Axboeffc771b2014-05-09 09:42:02 -06003803 if (mtip_hw_init(dd)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003804 rv = -EINVAL;
3805 goto protocol_init_error;
3806 }
3807
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003808 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003809 if (dd->disk == NULL) {
3810 dev_err(&dd->pdev->dev,
3811 "Unable to allocate gendisk structure\n");
3812 rv = -EINVAL;
3813 goto alloc_disk_error;
3814 }
3815
3816 /* Generate the disk name, implemented same as in sd.c */
3817 do {
3818 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3819 goto ida_get_error;
3820
3821 spin_lock(&rssd_index_lock);
3822 rv = ida_get_new(&rssd_index_ida, &index);
3823 spin_unlock(&rssd_index_lock);
3824 } while (rv == -EAGAIN);
3825
3826 if (rv)
3827 goto ida_get_error;
3828
3829 rv = rssd_disk_name_format("rssd",
3830 index,
3831 dd->disk->disk_name,
3832 DISK_NAME_LEN);
3833 if (rv)
3834 goto disk_index_error;
3835
3836 dd->disk->driverfs_dev = &dd->pdev->dev;
3837 dd->disk->major = dd->major;
Asai Thambi SP75787262015-05-11 15:55:26 -07003838 dd->disk->first_minor = index * MTIP_MAX_MINORS;
3839 dd->disk->minors = MTIP_MAX_MINORS;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003840 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003841 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003842 dd->index = index;
3843
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003844 mtip_hw_debugfs_init(dd);
3845
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003846skip_create_disk:
Jens Axboeffc771b2014-05-09 09:42:02 -06003847 memset(&dd->tags, 0, sizeof(dd->tags));
3848 dd->tags.ops = &mtip_mq_ops;
3849 dd->tags.nr_hw_queues = 1;
3850 dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS;
3851 dd->tags.reserved_tags = 1;
3852 dd->tags.cmd_size = sizeof(struct mtip_cmd);
3853 dd->tags.numa_node = dd->numa_node;
3854 dd->tags.flags = BLK_MQ_F_SHOULD_MERGE;
3855 dd->tags.driver_data = dd;
3856
3857 rv = blk_mq_alloc_tag_set(&dd->tags);
3858 if (rv) {
3859 dev_err(&dd->pdev->dev,
3860 "Unable to allocate request queue\n");
Jens Axboeffc771b2014-05-09 09:42:02 -06003861 goto block_queue_alloc_init_error;
3862 }
3863
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003864 /* Allocate the request queue. */
Jens Axboeffc771b2014-05-09 09:42:02 -06003865 dd->queue = blk_mq_init_queue(&dd->tags);
Dan Carpentera8a642c2014-05-14 15:54:18 +03003866 if (IS_ERR(dd->queue)) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003867 dev_err(&dd->pdev->dev,
3868 "Unable to allocate request queue\n");
3869 rv = -ENOMEM;
3870 goto block_queue_alloc_init_error;
3871 }
3872
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003873 dd->disk->queue = dd->queue;
3874 dd->queue->queuedata = dd;
3875
Jens Axboeffc771b2014-05-09 09:42:02 -06003876 /* Initialize the protocol layer. */
3877 wait_for_rebuild = mtip_hw_get_identify(dd);
3878 if (wait_for_rebuild < 0) {
3879 dev_err(&dd->pdev->dev,
3880 "Protocol layer initialization failed\n");
3881 rv = -EINVAL;
3882 goto init_hw_cmds_error;
3883 }
3884
3885 /*
3886 * if rebuild pending, start the service thread, and delay the block
3887 * queue creation and add_disk()
3888 */
3889 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3890 goto start_service_thread;
3891
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003892 /* Set device limits. */
3893 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
Mike Snitzerb277da02014-10-04 10:55:32 -06003894 clear_bit(QUEUE_FLAG_ADD_RANDOM, &dd->queue->queue_flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003895 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3896 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003897 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3898 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003899 blk_queue_io_min(dd->queue, 4096);
Felipe Franciosi1044b1b2014-03-13 14:34:20 +00003900 blk_queue_bounce_limit(dd->queue, dd->pdev->dma_mask);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003901
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01003902 /*
3903 * write back cache is not supported in the device. FUA depends on
3904 * write back cache support, hence setting flush support to zero.
3905 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003906 blk_queue_flush(dd->queue, 0);
3907
Asai Thambi S P15283462013-01-11 14:41:34 +01003908 /* Signal trim support */
3909 if (dd->trim_supp == true) {
3910 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
3911 dd->queue->limits.discard_granularity = 4096;
3912 blk_queue_max_discard_sectors(dd->queue,
3913 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
3914 dd->queue->limits.discard_zeroes_data = 0;
3915 }
3916
Sam Bradshaw88523a62011-08-30 08:34:26 -06003917 /* Set the capacity of the device in 512 byte sectors. */
3918 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3919 dev_warn(&dd->pdev->dev,
3920 "Could not read drive capacity\n");
3921 rv = -EIO;
3922 goto read_capacity_error;
3923 }
3924 set_capacity(dd->disk, capacity);
3925
3926 /* Enable the block device and add it to /dev */
3927 add_disk(dd->disk);
3928
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003929 dd->bdev = bdget_disk(dd->disk, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003930 /*
3931 * Now that the disk is active, initialize any sysfs attributes
3932 * managed by the protocol layer.
3933 */
3934 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3935 if (kobj) {
3936 mtip_hw_sysfs_init(dd, kobj);
3937 kobject_put(kobj);
3938 }
3939
Asai Thambi S P45038362012-04-09 08:35:38 +02003940 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003941 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003942 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02003943 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003944
3945start_service_thread:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003946 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
Rasmus Villemoes8aeea032015-11-20 10:46:49 +01003947 dd, dd->numa_node,
3948 "mtip_svc_thd_%02d", index);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003949
3950 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003951 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003952 dd->mtip_svc_handler = NULL;
3953 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003954 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003955 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003956 wake_up_process(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02003957 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3958 rv = wait_for_rebuild;
3959
Sam Bradshaw88523a62011-08-30 08:34:26 -06003960 return rv;
3961
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003962kthread_run_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003963 bdput(dd->bdev);
3964 dd->bdev = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07003965
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003966 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003967 del_gendisk(dd->disk);
3968
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003969read_capacity_error:
Jens Axboeffc771b2014-05-09 09:42:02 -06003970init_hw_cmds_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003971 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06003972 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003973block_queue_alloc_init_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003974 mtip_hw_debugfs_exit(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003975disk_index_error:
3976 spin_lock(&rssd_index_lock);
3977 ida_remove(&rssd_index_ida, index);
3978 spin_unlock(&rssd_index_lock);
3979
3980ida_get_error:
3981 put_disk(dd->disk);
3982
3983alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003984 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003985
3986protocol_init_error:
3987 return rv;
3988}
3989
3990/*
3991 * Block layer deinitialization function.
3992 *
3993 * Called by the PCI layer as each P320 device is removed.
3994 *
3995 * @dd Pointer to the driver data structure.
3996 *
3997 * return value
3998 * 0
3999 */
Jens Axboe63166682011-09-27 21:27:43 -06004000static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004001{
4002 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004003
Asai Thambi SP2132a542015-05-11 15:53:18 -07004004 mtip_hw_debugfs_exit(dd);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004005
Asai Thambi SP2132a542015-05-11 15:53:18 -07004006 if (dd->mtip_svc_handler) {
4007 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4008 wake_up_interruptible(&dd->port->svc_wait);
4009 kthread_stop(dd->mtip_svc_handler);
4010 }
4011
4012 /* Clean up the sysfs attributes, if created */
4013 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4014 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4015 if (kobj) {
4016 mtip_hw_sysfs_exit(dd, kobj);
4017 kobject_put(kobj);
Asai Thambi S P45038362012-04-09 08:35:38 +02004018 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004019 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004020
Asai Thambi SP2132a542015-05-11 15:53:18 -07004021 if (!dd->sr)
Jens Axboeffc771b2014-05-09 09:42:02 -06004022 mtip_standby_drive(dd);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004023 else
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004024 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
4025 dd->disk->disk_name);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004026
4027 /*
4028 * Delete our gendisk structure. This also removes the device
4029 * from /dev
4030 */
4031 if (dd->bdev) {
4032 bdput(dd->bdev);
4033 dd->bdev = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004034 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004035 if (dd->disk) {
4036 del_gendisk(dd->disk);
4037 if (dd->disk->queue) {
4038 blk_cleanup_queue(dd->queue);
4039 blk_mq_free_tag_set(&dd->tags);
4040 dd->queue = NULL;
4041 }
4042 put_disk(dd->disk);
4043 }
4044 dd->disk = NULL;
4045
4046 spin_lock(&rssd_index_lock);
4047 ida_remove(&rssd_index_ida, dd->index);
4048 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004049
4050 /* De-initialize the protocol layer. */
4051 mtip_hw_exit(dd);
4052
4053 return 0;
4054}
4055
4056/*
4057 * Function called by the PCI layer when just before the
4058 * machine shuts down.
4059 *
4060 * If a protocol layer shutdown function is present it will be called
4061 * by this function.
4062 *
4063 * @dd Pointer to the driver data structure.
4064 *
4065 * return value
4066 * 0
4067 */
Jens Axboe63166682011-09-27 21:27:43 -06004068static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004069{
Jens Axboeffc771b2014-05-09 09:42:02 -06004070 mtip_hw_shutdown(dd);
4071
Sam Bradshaw88523a62011-08-30 08:34:26 -06004072 /* Delete our gendisk structure, and cleanup the blk queue. */
Asai Thambi S P58c49df32013-01-11 18:47:12 +05304073 if (dd->disk) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304074 dev_info(&dd->pdev->dev,
4075 "Shutting down %s ...\n", dd->disk->disk_name);
Asai Thambi S P58c49df32013-01-11 18:47:12 +05304076
Asai Thambi SP02b48262015-05-11 15:48:00 -07004077 del_gendisk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304078 if (dd->disk->queue) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304079 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004080 blk_mq_free_tag_set(&dd->tags);
Asai Thambi SP02b48262015-05-11 15:48:00 -07004081 }
4082 put_disk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304083 dd->disk = NULL;
4084 dd->queue = NULL;
4085 }
Asai Thambi S P8182b492012-04-09 08:35:38 +02004086
4087 spin_lock(&rssd_index_lock);
4088 ida_remove(&rssd_index_ida, dd->index);
4089 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004090 return 0;
4091}
4092
Jens Axboe63166682011-09-27 21:27:43 -06004093static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004094{
4095 dev_info(&dd->pdev->dev,
4096 "Suspending %s ...\n", dd->disk->disk_name);
4097 mtip_hw_suspend(dd);
4098 return 0;
4099}
4100
Jens Axboe63166682011-09-27 21:27:43 -06004101static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004102{
4103 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4104 dd->disk->disk_name);
4105 mtip_hw_resume(dd);
4106 return 0;
4107}
4108
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004109static void drop_cpu(int cpu)
4110{
4111 cpu_use[cpu]--;
4112}
4113
4114static int get_least_used_cpu_on_node(int node)
4115{
4116 int cpu, least_used_cpu, least_cnt;
4117 const struct cpumask *node_mask;
4118
4119 node_mask = cpumask_of_node(node);
4120 least_used_cpu = cpumask_first(node_mask);
4121 least_cnt = cpu_use[least_used_cpu];
4122 cpu = least_used_cpu;
4123
4124 for_each_cpu(cpu, node_mask) {
4125 if (cpu_use[cpu] < least_cnt) {
4126 least_used_cpu = cpu;
4127 least_cnt = cpu_use[cpu];
4128 }
4129 }
4130 cpu_use[least_used_cpu]++;
4131 return least_used_cpu;
4132}
4133
4134/* Helper for selecting a node in round robin mode */
4135static inline int mtip_get_next_rr_node(void)
4136{
4137 static int next_node = -1;
4138
4139 if (next_node == -1) {
4140 next_node = first_online_node;
4141 return next_node;
4142 }
4143
4144 next_node = next_online_node(next_node);
4145 if (next_node == MAX_NUMNODES)
4146 next_node = first_online_node;
4147 return next_node;
4148}
4149
Fengguang Wu25bac122013-01-12 15:31:40 +08004150static DEFINE_HANDLER(0);
4151static DEFINE_HANDLER(1);
4152static DEFINE_HANDLER(2);
4153static DEFINE_HANDLER(3);
4154static DEFINE_HANDLER(4);
4155static DEFINE_HANDLER(5);
4156static DEFINE_HANDLER(6);
4157static DEFINE_HANDLER(7);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004158
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004159static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
4160{
4161 int pos;
4162 unsigned short pcie_dev_ctrl;
4163
4164 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
4165 if (pos) {
4166 pci_read_config_word(pdev,
4167 pos + PCI_EXP_DEVCTL,
4168 &pcie_dev_ctrl);
4169 if (pcie_dev_ctrl & (1 << 11) ||
4170 pcie_dev_ctrl & (1 << 4)) {
4171 dev_info(&dd->pdev->dev,
4172 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4173 pdev->vendor, pdev->device);
4174 pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
4175 PCI_EXP_DEVCTL_RELAX_EN);
4176 pci_write_config_word(pdev,
4177 pos + PCI_EXP_DEVCTL,
4178 pcie_dev_ctrl);
4179 }
4180 }
4181}
4182
4183static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev)
4184{
4185 /*
4186 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4187 * device with device id 0x5aXX
4188 */
4189 if (pdev->bus && pdev->bus->self) {
4190 if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI &&
4191 ((pdev->bus->self->device & 0xff00) == 0x5a00)) {
4192 mtip_disable_link_opts(dd, pdev->bus->self);
4193 } else {
4194 /* Check further up the topology */
4195 struct pci_dev *parent_dev = pdev->bus->self;
4196 if (parent_dev->bus &&
4197 parent_dev->bus->parent &&
4198 parent_dev->bus->parent->self &&
4199 parent_dev->bus->parent->self->vendor ==
4200 PCI_VENDOR_ID_ATI &&
4201 (parent_dev->bus->parent->self->device &
4202 0xff00) == 0x5a00) {
4203 mtip_disable_link_opts(dd,
4204 parent_dev->bus->parent->self);
4205 }
4206 }
4207 }
4208}
4209
Sam Bradshaw88523a62011-08-30 08:34:26 -06004210/*
4211 * Called for each supported PCI device detected.
4212 *
4213 * This function allocates the private data structure, enables the
4214 * PCI device and then calls the block layer initialization function.
4215 *
4216 * return value
4217 * 0 on success else an error code.
4218 */
4219static int mtip_pci_probe(struct pci_dev *pdev,
4220 const struct pci_device_id *ent)
4221{
4222 int rv = 0;
4223 struct driver_data *dd = NULL;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004224 char cpu_list[256];
4225 const struct cpumask *node_mask;
4226 int cpu, i = 0, j = 0;
4227 int my_node = NUMA_NO_NODE;
Asai Thambi S P0caff002013-04-03 19:56:21 +05304228 unsigned long flags;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004229
4230 /* Allocate memory for this devices private data. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004231 my_node = pcibus_to_node(pdev->bus);
4232 if (my_node != NUMA_NO_NODE) {
4233 if (!node_online(my_node))
4234 my_node = mtip_get_next_rr_node();
4235 } else {
4236 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4237 my_node = mtip_get_next_rr_node();
4238 }
4239 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4240 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
Jens Axboe7f328902014-03-10 14:29:37 -06004241 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004242
4243 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004244 if (dd == NULL) {
4245 dev_err(&pdev->dev,
4246 "Unable to allocate memory for driver data\n");
4247 return -ENOMEM;
4248 }
4249
Sam Bradshaw88523a62011-08-30 08:34:26 -06004250 /* Attach the private data to this PCI device. */
4251 pci_set_drvdata(pdev, dd);
4252
4253 rv = pcim_enable_device(pdev);
4254 if (rv < 0) {
4255 dev_err(&pdev->dev, "Unable to enable device\n");
4256 goto iomap_err;
4257 }
4258
4259 /* Map BAR5 to memory. */
4260 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4261 if (rv < 0) {
4262 dev_err(&pdev->dev, "Unable to map regions\n");
4263 goto iomap_err;
4264 }
4265
4266 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4267 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4268
4269 if (rv) {
4270 rv = pci_set_consistent_dma_mask(pdev,
4271 DMA_BIT_MASK(32));
4272 if (rv) {
4273 dev_warn(&pdev->dev,
4274 "64-bit DMA enable failed\n");
4275 goto setmask_err;
4276 }
4277 }
4278 }
4279
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004280 /* Copy the info we may need later into the private data structure. */
4281 dd->major = mtip_major;
4282 dd->instance = instance;
4283 dd->pdev = pdev;
4284 dd->numa_node = my_node;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004285
Asai Thambi S P0caff002013-04-03 19:56:21 +05304286 INIT_LIST_HEAD(&dd->online_list);
4287 INIT_LIST_HEAD(&dd->remove_list);
4288
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004289 memset(dd->workq_name, 0, 32);
4290 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4291
4292 dd->isr_workq = create_workqueue(dd->workq_name);
4293 if (!dd->isr_workq) {
4294 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
Wei Yongjund137c832013-03-22 08:58:23 -06004295 rv = -ENOMEM;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004296 goto block_initialize_err;
4297 }
4298
4299 memset(cpu_list, 0, sizeof(cpu_list));
4300
4301 node_mask = cpumask_of_node(dd->numa_node);
4302 if (!cpumask_empty(node_mask)) {
4303 for_each_cpu(cpu, node_mask)
4304 {
4305 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4306 j = strlen(cpu_list);
4307 }
4308
4309 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4310 dd->numa_node,
4311 topology_physical_package_id(cpumask_first(node_mask)),
4312 nr_cpus_node(dd->numa_node),
4313 cpu_list);
4314 } else
4315 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4316
4317 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4318 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4319 cpu_to_node(dd->isr_binding), dd->isr_binding);
4320
4321 /* first worker context always runs in ISR */
4322 dd->work[0].cpu_binding = dd->isr_binding;
4323 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4324 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4325 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4326 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4327 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4328 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4329 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4330
4331 /* Log the bindings */
4332 for_each_present_cpu(cpu) {
4333 memset(cpu_list, 0, sizeof(cpu_list));
4334 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4335 if (dd->work[i].cpu_binding == cpu) {
4336 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4337 j = strlen(cpu_list);
4338 }
4339 }
4340 if (j)
4341 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4342 }
4343
4344 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4345 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4346 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4347 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4348 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4349 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4350 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4351 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4352
4353 pci_set_master(pdev);
Wei Yongjund137c832013-03-22 08:58:23 -06004354 rv = pci_enable_msi(pdev);
4355 if (rv) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004356 dev_warn(&pdev->dev,
4357 "Unable to enable MSI interrupt.\n");
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004358 goto msi_initialize_err;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004359 }
4360
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004361 mtip_fix_ero_nosnoop(dd, pdev);
4362
Sam Bradshaw88523a62011-08-30 08:34:26 -06004363 /* Initialize the block layer. */
4364 rv = mtip_block_initialize(dd);
4365 if (rv < 0) {
4366 dev_err(&pdev->dev,
4367 "Unable to initialize block layer\n");
4368 goto block_initialize_err;
4369 }
4370
4371 /*
4372 * Increment the instance count so that each device has a unique
4373 * instance number.
4374 */
4375 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02004376 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004377 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P6b06d352013-04-03 19:54:35 +05304378 else
4379 rv = 0; /* device in rebuild state, return 0 from probe */
Asai Thambi S P0caff002013-04-03 19:56:21 +05304380
4381 /* Add to online list even if in ftl rebuild */
4382 spin_lock_irqsave(&dev_lock, flags);
4383 list_add(&dd->online_list, &online_list);
4384 spin_unlock_irqrestore(&dev_lock, flags);
4385
Sam Bradshaw88523a62011-08-30 08:34:26 -06004386 goto done;
4387
4388block_initialize_err:
4389 pci_disable_msi(pdev);
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004390
4391msi_initialize_err:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004392 if (dd->isr_workq) {
4393 flush_workqueue(dd->isr_workq);
4394 destroy_workqueue(dd->isr_workq);
4395 drop_cpu(dd->work[0].cpu_binding);
4396 drop_cpu(dd->work[1].cpu_binding);
4397 drop_cpu(dd->work[2].cpu_binding);
4398 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004399setmask_err:
4400 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4401
4402iomap_err:
4403 kfree(dd);
4404 pci_set_drvdata(pdev, NULL);
4405 return rv;
4406done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06004407 return rv;
4408}
4409
4410/*
4411 * Called for each probed device when the device is removed or the
4412 * driver is unloaded.
4413 *
4414 * return value
4415 * None
4416 */
4417static void mtip_pci_remove(struct pci_dev *pdev)
4418{
4419 struct driver_data *dd = pci_get_drvdata(pdev);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004420 unsigned long flags, to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004421
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004422 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02004423
Asai Thambi S P0caff002013-04-03 19:56:21 +05304424 spin_lock_irqsave(&dev_lock, flags);
4425 list_del_init(&dd->online_list);
4426 list_add(&dd->remove_list, &removing_list);
4427 spin_unlock_irqrestore(&dev_lock, flags);
4428
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004429 mtip_check_surprise_removal(pdev);
4430 synchronize_irq(dd->pdev->irq);
4431
4432 /* Spin until workers are done */
4433 to = jiffies + msecs_to_jiffies(4000);
4434 do {
4435 msleep(20);
4436 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4437 time_before(jiffies, to));
4438
4439 if (atomic_read(&dd->irq_workers_active) != 0) {
4440 dev_warn(&dd->pdev->dev,
4441 "Completion workers still active!\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004442 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004443
Asai Thambi SP2132a542015-05-11 15:53:18 -07004444 blk_mq_stop_hw_queues(dd->queue);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004445 /* Clean up the block layer. */
4446 mtip_block_remove(dd);
4447
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004448 if (dd->isr_workq) {
4449 flush_workqueue(dd->isr_workq);
4450 destroy_workqueue(dd->isr_workq);
4451 drop_cpu(dd->work[0].cpu_binding);
4452 drop_cpu(dd->work[1].cpu_binding);
4453 drop_cpu(dd->work[2].cpu_binding);
4454 }
4455
Sam Bradshaw88523a62011-08-30 08:34:26 -06004456 pci_disable_msi(pdev);
4457
Asai Thambi S P0caff002013-04-03 19:56:21 +05304458 spin_lock_irqsave(&dev_lock, flags);
4459 list_del_init(&dd->remove_list);
4460 spin_unlock_irqrestore(&dev_lock, flags);
4461
Asai Thambi SP2132a542015-05-11 15:53:18 -07004462 kfree(dd);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004463
Sam Bradshaw88523a62011-08-30 08:34:26 -06004464 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004465 pci_set_drvdata(pdev, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004466}
4467
4468/*
4469 * Called for each probed device when the device is suspended.
4470 *
4471 * return value
4472 * 0 Success
4473 * <0 Error
4474 */
4475static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4476{
4477 int rv = 0;
4478 struct driver_data *dd = pci_get_drvdata(pdev);
4479
4480 if (!dd) {
4481 dev_err(&pdev->dev,
4482 "Driver private datastructure is NULL\n");
4483 return -EFAULT;
4484 }
4485
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004486 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004487
4488 /* Disable ports & interrupts then send standby immediate */
4489 rv = mtip_block_suspend(dd);
4490 if (rv < 0) {
4491 dev_err(&pdev->dev,
4492 "Failed to suspend controller\n");
4493 return rv;
4494 }
4495
4496 /*
4497 * Save the pci config space to pdev structure &
4498 * disable the device
4499 */
4500 pci_save_state(pdev);
4501 pci_disable_device(pdev);
4502
4503 /* Move to Low power state*/
4504 pci_set_power_state(pdev, PCI_D3hot);
4505
4506 return rv;
4507}
4508
4509/*
4510 * Called for each probed device when the device is resumed.
4511 *
4512 * return value
4513 * 0 Success
4514 * <0 Error
4515 */
4516static int mtip_pci_resume(struct pci_dev *pdev)
4517{
4518 int rv = 0;
4519 struct driver_data *dd;
4520
4521 dd = pci_get_drvdata(pdev);
4522 if (!dd) {
4523 dev_err(&pdev->dev,
4524 "Driver private datastructure is NULL\n");
4525 return -EFAULT;
4526 }
4527
4528 /* Move the device to active State */
4529 pci_set_power_state(pdev, PCI_D0);
4530
4531 /* Restore PCI configuration space */
4532 pci_restore_state(pdev);
4533
4534 /* Enable the PCI device*/
4535 rv = pcim_enable_device(pdev);
4536 if (rv < 0) {
4537 dev_err(&pdev->dev,
4538 "Failed to enable card during resume\n");
4539 goto err;
4540 }
4541 pci_set_master(pdev);
4542
4543 /*
4544 * Calls hbaReset, initPort, & startPort function
4545 * then enables interrupts
4546 */
4547 rv = mtip_block_resume(dd);
4548 if (rv < 0)
4549 dev_err(&pdev->dev, "Unable to resume\n");
4550
4551err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004552 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004553
4554 return rv;
4555}
4556
4557/*
4558 * Shutdown routine
4559 *
4560 * return value
4561 * None
4562 */
4563static void mtip_pci_shutdown(struct pci_dev *pdev)
4564{
4565 struct driver_data *dd = pci_get_drvdata(pdev);
4566 if (dd)
4567 mtip_block_shutdown(dd);
4568}
4569
Sam Bradshaw88523a62011-08-30 08:34:26 -06004570/* Table of device ids supported by this driver. */
Benoit Taine9baa3c32014-08-08 15:56:03 +02004571static const struct pci_device_id mtip_pci_tbl[] = {
Asai Thambi S P1a131452012-09-05 22:00:38 +05304572 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4573 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4574 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4575 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4576 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4577 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4578 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
Sam Bradshaw88523a62011-08-30 08:34:26 -06004579 { 0 }
4580};
4581
4582/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004583static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004584 .name = MTIP_DRV_NAME,
4585 .id_table = mtip_pci_tbl,
4586 .probe = mtip_pci_probe,
4587 .remove = mtip_pci_remove,
4588 .suspend = mtip_pci_suspend,
4589 .resume = mtip_pci_resume,
4590 .shutdown = mtip_pci_shutdown,
4591};
4592
4593MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4594
4595/*
4596 * Module initialization function.
4597 *
4598 * Called once when the module is loaded. This function allocates a major
4599 * block device number to the Cyclone devices and registers the PCI layer
4600 * of the driver.
4601 *
4602 * Return value
4603 * 0 on success else error code.
4604 */
4605static int __init mtip_init(void)
4606{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004607 int error;
4608
Asai Thambi S P45422e72012-09-05 22:03:56 +05304609 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004610
Asai Thambi S P0caff002013-04-03 19:56:21 +05304611 spin_lock_init(&dev_lock);
4612
4613 INIT_LIST_HEAD(&online_list);
4614 INIT_LIST_HEAD(&removing_list);
4615
Sam Bradshaw88523a62011-08-30 08:34:26 -06004616 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004617 error = register_blkdev(0, MTIP_DRV_NAME);
4618 if (error <= 0) {
Asai Thambi S P45422e72012-09-05 22:03:56 +05304619 pr_err("Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004620 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004621 return -EBUSY;
4622 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004623 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004624
Asai Thambi S P0caff002013-04-03 19:56:21 +05304625 dfs_parent = debugfs_create_dir("rssd", NULL);
4626 if (IS_ERR_OR_NULL(dfs_parent)) {
4627 pr_warn("Error creating debugfs parent\n");
4628 dfs_parent = NULL;
4629 }
4630 if (dfs_parent) {
4631 dfs_device_status = debugfs_create_file("device_status",
4632 S_IRUGO, dfs_parent, NULL,
4633 &mtip_device_status_fops);
4634 if (IS_ERR_OR_NULL(dfs_device_status)) {
4635 pr_err("Error creating device_status node\n");
4636 dfs_device_status = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004637 }
4638 }
4639
Sam Bradshaw88523a62011-08-30 08:34:26 -06004640 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004641 error = pci_register_driver(&mtip_pci_driver);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004642 if (error) {
4643 debugfs_remove(dfs_parent);
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004644 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004645 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004646
4647 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004648}
4649
4650/*
4651 * Module de-initialization function.
4652 *
4653 * Called once when the module is unloaded. This function deallocates
4654 * the major block device number allocated by mtip_init() and
4655 * unregisters the PCI layer of the driver.
4656 *
4657 * Return value
4658 * none
4659 */
4660static void __exit mtip_exit(void)
4661{
Sam Bradshaw88523a62011-08-30 08:34:26 -06004662 /* Release the allocated major block device number. */
4663 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4664
4665 /* Unregister the PCI driver. */
4666 pci_unregister_driver(&mtip_pci_driver);
Asai Thambi S Paf5ded82014-04-16 20:30:16 -07004667
4668 debugfs_remove_recursive(dfs_parent);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004669}
4670
4671MODULE_AUTHOR("Micron Technology, Inc");
4672MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4673MODULE_LICENSE("GPL");
4674MODULE_VERSION(MTIP_DRV_VERSION);
4675
4676module_init(mtip_init);
4677module_exit(mtip_exit);