blob: 9ae4cc5c61ee7de5a32adf69f90430e5e9bf69ea [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;
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001095 unsigned long start;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001096
1097 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1098 if (buffer & 0x00000007) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301099 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001100 return -EFAULT;
1101 }
1102
Jens Axboeffc771b2014-05-09 09:42:02 -06001103 int_cmd = mtip_get_int_command(dd);
1104
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001105 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001106
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001107 if (fis->command == ATA_CMD_SEC_ERASE_PREP)
1108 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1109
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301110 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001111
1112 if (atomic == GFP_KERNEL) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001113 if (fis->command != ATA_CMD_STANDBYNOW1) {
1114 /* wait for io to complete if non atomic */
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001115 if (mtip_quiesce_io(port,
1116 MTIP_QUIESCE_IO_TIMEOUT_MS) < 0) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301117 dev_warn(&dd->pdev->dev,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001118 "Failed to quiesce IO\n");
Jens Axboeffc771b2014-05-09 09:42:02 -06001119 mtip_put_int_command(dd, int_cmd);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001120 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001121 wake_up_interruptible(&port->svc_wait);
1122 return -EBUSY;
1123 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001124 }
1125
1126 /* Set the completion function and data for the command. */
1127 int_cmd->comp_data = &wait;
1128 int_cmd->comp_func = mtip_completion;
1129
1130 } else {
1131 /* Clear completion - we're going to poll */
1132 int_cmd->comp_data = NULL;
Asai Thambi S P8182b492012-04-09 08:35:38 +02001133 int_cmd->comp_func = mtip_null_completion;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001134 }
1135
1136 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001137 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001138
1139 /* Populate the SG list */
1140 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001141 __force_bit2int cpu_to_le32(opts | fis_len);
1142 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001143 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1144
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001145 command_sg->info =
1146 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1147 command_sg->dba =
1148 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1149 command_sg->dba_upper =
1150 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001151
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001152 int_cmd->command_header->opts |=
1153 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001154 }
1155
1156 /* Populate the command header */
1157 int_cmd->command_header->byte_count = 0;
1158
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001159 start = jiffies;
1160
Sam Bradshaw88523a62011-08-30 08:34:26 -06001161 /* Issue the command to the hardware */
1162 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1163
Sam Bradshaw88523a62011-08-30 08:34:26 -06001164 if (atomic == GFP_KERNEL) {
1165 /* Wait for the command to complete or timeout. */
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001166 if ((rv = wait_for_completion_interruptible_timeout(
Sam Bradshaw88523a62011-08-30 08:34:26 -06001167 &wait,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001168 msecs_to_jiffies(timeout))) <= 0) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301169 if (rv == -ERESTARTSYS) { /* interrupted */
1170 dev_err(&dd->pdev->dev,
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001171 "Internal command [%02X] was interrupted after %u ms\n",
1172 fis->command,
1173 jiffies_to_msecs(jiffies - start));
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301174 rv = -EINTR;
1175 goto exec_ic_exit;
1176 } else if (rv == 0) /* timeout */
1177 dev_err(&dd->pdev->dev,
1178 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1179 fis->command, timeout);
1180 else
1181 dev_err(&dd->pdev->dev,
1182 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1183 fis->command, rv, timeout);
1184
1185 if (mtip_check_surprise_removal(dd->pdev) ||
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001186 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301187 &dd->dd_flag)) {
1188 dev_err(&dd->pdev->dev,
1189 "Internal command [%02X] wait returned due to SR\n",
1190 fis->command);
Asai Thambi S P45038362012-04-09 08:35:38 +02001191 rv = -ENXIO;
1192 goto exec_ic_exit;
1193 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301194 mtip_device_reset(dd); /* recover from timeout issue */
Sam Bradshaw88523a62011-08-30 08:34:26 -06001195 rv = -EAGAIN;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301196 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001197 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001198 } else {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301199 u32 hba_stat, port_stat;
1200
Sam Bradshaw88523a62011-08-30 08:34:26 -06001201 /* Spin for <timeout> checking if command still outstanding */
1202 timeout = jiffies + msecs_to_jiffies(timeout);
Asai Thambi S P8182b492012-04-09 08:35:38 +02001203 while ((readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1204 & (1 << MTIP_TAG_INTERNAL))
1205 && time_before(jiffies, timeout)) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301206 if (mtip_check_surprise_removal(dd->pdev)) {
Asai Thambi S P8182b492012-04-09 08:35:38 +02001207 rv = -ENXIO;
1208 goto exec_ic_exit;
1209 }
1210 if ((fis->command != ATA_CMD_STANDBYNOW1) &&
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001211 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301212 &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02001213 rv = -ENXIO;
1214 goto exec_ic_exit;
1215 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301216 port_stat = readl(port->mmio + PORT_IRQ_STAT);
1217 if (!port_stat)
1218 continue;
1219
1220 if (port_stat & PORT_IRQ_ERR) {
1221 dev_err(&dd->pdev->dev,
1222 "Internal command [%02X] failed\n",
1223 fis->command);
1224 mtip_device_reset(dd);
1225 rv = -EIO;
1226 goto exec_ic_exit;
1227 } else {
1228 writel(port_stat, port->mmio + PORT_IRQ_STAT);
1229 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
1230 if (hba_stat)
1231 writel(hba_stat,
1232 dd->mmio + HOST_IRQ_STAT);
Asai Thambi S P45038362012-04-09 08:35:38 +02001233 }
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301234 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001235 }
1236 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001237
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001238 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1239 & (1 << MTIP_TAG_INTERNAL)) {
1240 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301241 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1242 mtip_device_reset(dd);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001243 rv = -EAGAIN;
1244 }
1245 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001246exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001247 /* Clear the allocated and active bits for the internal command. */
Jens Axboeffc771b2014-05-09 09:42:02 -06001248 mtip_put_int_command(dd, int_cmd);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001249 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001250 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1251 /* NCQ paused */
1252 return rv;
1253 }
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001254 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001255
1256 return rv;
1257}
1258
1259/*
1260 * Byte-swap ATA ID strings.
1261 *
1262 * ATA identify data contains strings in byte-swapped 16-bit words.
1263 * They must be swapped (on all architectures) to be usable as C strings.
1264 * This function swaps bytes in-place.
1265 *
1266 * @buf The buffer location of the string
1267 * @len The number of bytes to swap
1268 *
1269 * return value
1270 * None
1271 */
1272static inline void ata_swap_string(u16 *buf, unsigned int len)
1273{
1274 int i;
1275 for (i = 0; i < (len/2); i++)
1276 be16_to_cpus(&buf[i]);
1277}
1278
Asai Thambi S P670a6412014-04-16 20:27:54 -07001279static void mtip_set_timeout(struct driver_data *dd,
1280 struct host_to_dev_fis *fis,
1281 unsigned int *timeout, u8 erasemode)
1282{
1283 switch (fis->command) {
1284 case ATA_CMD_DOWNLOAD_MICRO:
1285 *timeout = 120000; /* 2 minutes */
1286 break;
1287 case ATA_CMD_SEC_ERASE_UNIT:
1288 case 0xFC:
1289 if (erasemode)
1290 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
1291 else
1292 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
1293 break;
1294 case ATA_CMD_STANDBYNOW1:
1295 *timeout = 120000; /* 2 minutes */
1296 break;
1297 case 0xF7:
1298 case 0xFA:
1299 *timeout = 60000; /* 60 seconds */
1300 break;
1301 case ATA_CMD_SMART:
1302 *timeout = 15000; /* 15 seconds */
1303 break;
1304 default:
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001305 *timeout = MTIP_IOCTL_CMD_TIMEOUT_MS;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001306 break;
1307 }
1308}
1309
Sam Bradshaw88523a62011-08-30 08:34:26 -06001310/*
1311 * Request the device identity information.
1312 *
1313 * If a user space buffer is not specified, i.e. is NULL, the
1314 * identify information is still read from the drive and placed
1315 * into the identify data buffer (@e port->identify) in the
1316 * port data structure.
1317 * When the identify buffer contains valid identify information @e
1318 * port->identify_valid is non-zero.
1319 *
1320 * @port Pointer to the port structure.
1321 * @user_buffer A user space buffer where the identify data should be
1322 * copied.
1323 *
1324 * return value
1325 * 0 Command completed successfully.
1326 * -EFAULT An error occurred while coping data to the user buffer.
1327 * -1 Command failed.
1328 */
1329static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1330{
1331 int rv = 0;
1332 struct host_to_dev_fis fis;
1333
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001334 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001335 return -EFAULT;
1336
Sam Bradshaw88523a62011-08-30 08:34:26 -06001337 /* Build the FIS. */
1338 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1339 fis.type = 0x27;
1340 fis.opts = 1 << 7;
1341 fis.command = ATA_CMD_ID_ATA;
1342
1343 /* Set the identify information as invalid. */
1344 port->identify_valid = 0;
1345
1346 /* Clear the identify information. */
1347 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1348
1349 /* Execute the command. */
1350 if (mtip_exec_internal_command(port,
1351 &fis,
1352 5,
1353 port->identify_dma,
1354 sizeof(u16) * ATA_ID_WORDS,
1355 0,
1356 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001357 MTIP_INT_CMD_TIMEOUT_MS)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001358 < 0) {
1359 rv = -1;
1360 goto out;
1361 }
1362
1363 /*
1364 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1365 * perform field-sensitive swapping on the string fields.
1366 * See the kernel use of ata_id_string() for proof of this.
1367 */
1368#ifdef __LITTLE_ENDIAN
1369 ata_swap_string(port->identify + 27, 40); /* model string*/
1370 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1371 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1372#else
1373 {
1374 int i;
1375 for (i = 0; i < ATA_ID_WORDS; i++)
1376 port->identify[i] = le16_to_cpu(port->identify[i]);
1377 }
1378#endif
1379
Sam Bradshaw26d58052013-10-03 10:18:05 -07001380 /* Check security locked state */
1381 if (port->identify[128] & 0x4)
1382 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1383 else
1384 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1385
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301386#ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
Asai Thambi S P15283462013-01-11 14:41:34 +01001387 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1388 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1389 port->dd->trim_supp = true;
1390 else
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301391#endif
Asai Thambi S P15283462013-01-11 14:41:34 +01001392 port->dd->trim_supp = false;
1393
Sam Bradshaw88523a62011-08-30 08:34:26 -06001394 /* Set the identify buffer as valid. */
1395 port->identify_valid = 1;
1396
1397 if (user_buffer) {
1398 if (copy_to_user(
1399 user_buffer,
1400 port->identify,
1401 ATA_ID_WORDS * sizeof(u16))) {
1402 rv = -EFAULT;
1403 goto out;
1404 }
1405 }
1406
1407out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001408 return rv;
1409}
1410
1411/*
1412 * Issue a standby immediate command to the device.
1413 *
1414 * @port Pointer to the port structure.
1415 *
1416 * return value
1417 * 0 Command was executed successfully.
1418 * -1 An error occurred while executing the command.
1419 */
1420static int mtip_standby_immediate(struct mtip_port *port)
1421{
1422 int rv;
1423 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001424 unsigned long start;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001425 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001426
Sam Bradshaw88523a62011-08-30 08:34:26 -06001427 /* Build the FIS. */
1428 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1429 fis.type = 0x27;
1430 fis.opts = 1 << 7;
1431 fis.command = ATA_CMD_STANDBYNOW1;
1432
Asai Thambi S P670a6412014-04-16 20:27:54 -07001433 mtip_set_timeout(port->dd, &fis, &timeout, 0);
1434
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001435 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001436 rv = mtip_exec_internal_command(port,
1437 &fis,
1438 5,
1439 0,
1440 0,
1441 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001442 GFP_ATOMIC,
Asai Thambi S P670a6412014-04-16 20:27:54 -07001443 timeout);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001444 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1445 jiffies_to_msecs(jiffies - start));
1446 if (rv)
1447 dev_warn(&port->dd->pdev->dev,
1448 "STANDBY IMMEDIATE command failed.\n");
1449
1450 return rv;
1451}
1452
1453/*
1454 * Issue a READ LOG EXT command to the device.
1455 *
1456 * @port pointer to the port structure.
1457 * @page page number to fetch
1458 * @buffer pointer to buffer
1459 * @buffer_dma dma address corresponding to @buffer
1460 * @sectors page length to fetch, in sectors
1461 *
1462 * return value
1463 * @rv return value from mtip_exec_internal_command()
1464 */
1465static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1466 dma_addr_t buffer_dma, unsigned int sectors)
1467{
1468 struct host_to_dev_fis fis;
1469
1470 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1471 fis.type = 0x27;
1472 fis.opts = 1 << 7;
1473 fis.command = ATA_CMD_READ_LOG_EXT;
1474 fis.sect_count = sectors & 0xFF;
1475 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1476 fis.lba_low = page;
1477 fis.lba_mid = 0;
1478 fis.device = ATA_DEVICE_OBS;
1479
1480 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1481
1482 return mtip_exec_internal_command(port,
1483 &fis,
1484 5,
1485 buffer_dma,
1486 sectors * ATA_SECT_SIZE,
1487 0,
1488 GFP_ATOMIC,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001489 MTIP_INT_CMD_TIMEOUT_MS);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001490}
1491
1492/*
1493 * Issue a SMART READ DATA command to the device.
1494 *
1495 * @port pointer to the port structure.
1496 * @buffer pointer to buffer
1497 * @buffer_dma dma address corresponding to @buffer
1498 *
1499 * return value
1500 * @rv return value from mtip_exec_internal_command()
1501 */
1502static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1503 dma_addr_t buffer_dma)
1504{
1505 struct host_to_dev_fis fis;
1506
1507 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1508 fis.type = 0x27;
1509 fis.opts = 1 << 7;
1510 fis.command = ATA_CMD_SMART;
1511 fis.features = 0xD0;
1512 fis.sect_count = 1;
1513 fis.lba_mid = 0x4F;
1514 fis.lba_hi = 0xC2;
1515 fis.device = ATA_DEVICE_OBS;
1516
1517 return mtip_exec_internal_command(port,
1518 &fis,
1519 5,
1520 buffer_dma,
1521 ATA_SECT_SIZE,
1522 0,
1523 GFP_ATOMIC,
1524 15000);
1525}
1526
1527/*
1528 * Get the value of a smart attribute
1529 *
1530 * @port pointer to the port structure
1531 * @id attribute number
1532 * @attrib pointer to return attrib information corresponding to @id
1533 *
1534 * return value
1535 * -EINVAL NULL buffer passed or unsupported attribute @id.
1536 * -EPERM Identify data not valid, SMART not supported or not enabled
1537 */
1538static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1539 struct smart_attr *attrib)
1540{
1541 int rv, i;
1542 struct smart_attr *pattr;
1543
1544 if (!attrib)
1545 return -EINVAL;
1546
1547 if (!port->identify_valid) {
1548 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1549 return -EPERM;
1550 }
1551 if (!(port->identify[82] & 0x1)) {
1552 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1553 return -EPERM;
1554 }
1555 if (!(port->identify[85] & 0x1)) {
1556 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1557 return -EPERM;
1558 }
1559
1560 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1561 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1562 if (rv) {
1563 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1564 return rv;
1565 }
1566
1567 pattr = (struct smart_attr *)(port->smart_buf + 2);
1568 for (i = 0; i < 29; i++, pattr++)
1569 if (pattr->attr_id == id) {
1570 memcpy(attrib, pattr, sizeof(struct smart_attr));
1571 break;
1572 }
1573
1574 if (i == 29) {
1575 dev_warn(&port->dd->pdev->dev,
1576 "Query for invalid SMART attribute ID\n");
1577 rv = -EINVAL;
1578 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001579
Sam Bradshaw88523a62011-08-30 08:34:26 -06001580 return rv;
1581}
1582
1583/*
Asai Thambi S P15283462013-01-11 14:41:34 +01001584 * Trim unused sectors
1585 *
1586 * @dd pointer to driver_data structure
1587 * @lba starting lba
1588 * @len # of 512b sectors to trim
1589 *
1590 * return value
1591 * -ENOMEM Out of dma memory
1592 * -EINVAL Invalid parameters passed in, trim not supported
1593 * -EIO Error submitting trim request to hw
1594 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301595static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
1596 unsigned int len)
Asai Thambi S P15283462013-01-11 14:41:34 +01001597{
1598 int i, rv = 0;
1599 u64 tlba, tlen, sect_left;
1600 struct mtip_trim_entry *buf;
1601 dma_addr_t dma_addr;
1602 struct host_to_dev_fis fis;
1603
1604 if (!len || dd->trim_supp == false)
1605 return -EINVAL;
1606
1607 /* Trim request too big */
1608 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1609
1610 /* Trim request not aligned on 4k boundary */
1611 WARN_ON(len % 8 != 0);
1612
1613 /* Warn if vu_trim structure is too big */
1614 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1615
1616 /* Allocate a DMA buffer for the trim structure */
1617 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1618 GFP_KERNEL);
1619 if (!buf)
1620 return -ENOMEM;
1621 memset(buf, 0, ATA_SECT_SIZE);
1622
1623 for (i = 0, sect_left = len, tlba = lba;
1624 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1625 i++) {
1626 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1627 MTIP_MAX_TRIM_ENTRY_LEN :
1628 sect_left);
1629 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1630 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1631 tlba += tlen;
1632 sect_left -= tlen;
1633 }
1634 WARN_ON(sect_left != 0);
1635
1636 /* Build the fis */
1637 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1638 fis.type = 0x27;
1639 fis.opts = 1 << 7;
1640 fis.command = 0xfb;
1641 fis.features = 0x60;
1642 fis.sect_count = 1;
1643 fis.device = ATA_DEVICE_OBS;
1644
1645 if (mtip_exec_internal_command(dd->port,
1646 &fis,
1647 5,
1648 dma_addr,
1649 ATA_SECT_SIZE,
1650 0,
1651 GFP_KERNEL,
1652 MTIP_TRIM_TIMEOUT_MS) < 0)
1653 rv = -EIO;
1654
1655 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1656 return rv;
1657}
1658
1659/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001660 * Get the drive capacity.
1661 *
1662 * @dd Pointer to the device data structure.
1663 * @sectors Pointer to the variable that will receive the sector count.
1664 *
1665 * return value
1666 * 1 Capacity was returned successfully.
1667 * 0 The identify information is invalid.
1668 */
Jens Axboe63166682011-09-27 21:27:43 -06001669static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001670{
1671 struct mtip_port *port = dd->port;
1672 u64 total, raw0, raw1, raw2, raw3;
1673 raw0 = port->identify[100];
1674 raw1 = port->identify[101];
1675 raw2 = port->identify[102];
1676 raw3 = port->identify[103];
1677 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1678 *sectors = total;
1679 return (bool) !!port->identify_valid;
1680}
1681
1682/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001683 * Display the identify command data.
1684 *
1685 * @port Pointer to the port data structure.
1686 *
1687 * return value
1688 * None
1689 */
1690static void mtip_dump_identify(struct mtip_port *port)
1691{
1692 sector_t sectors;
1693 unsigned short revid;
1694 char cbuf[42];
1695
1696 if (!port->identify_valid)
1697 return;
1698
1699 strlcpy(cbuf, (char *)(port->identify+10), 21);
1700 dev_info(&port->dd->pdev->dev,
1701 "Serial No.: %s\n", cbuf);
1702
1703 strlcpy(cbuf, (char *)(port->identify+23), 9);
1704 dev_info(&port->dd->pdev->dev,
1705 "Firmware Ver.: %s\n", cbuf);
1706
1707 strlcpy(cbuf, (char *)(port->identify+27), 41);
1708 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1709
Sam Bradshaw26d58052013-10-03 10:18:05 -07001710 dev_info(&port->dd->pdev->dev, "Security: %04x %s\n",
1711 port->identify[128],
1712 port->identify[128] & 0x4 ? "(LOCKED)" : "");
1713
Sam Bradshaw88523a62011-08-30 08:34:26 -06001714 if (mtip_hw_get_capacity(port->dd, &sectors))
1715 dev_info(&port->dd->pdev->dev,
1716 "Capacity: %llu sectors (%llu MB)\n",
1717 (u64)sectors,
1718 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1719
1720 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001721 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001722 case 0x1:
1723 strlcpy(cbuf, "A0", 3);
1724 break;
1725 case 0x3:
1726 strlcpy(cbuf, "A2", 3);
1727 break;
1728 default:
1729 strlcpy(cbuf, "?", 2);
1730 break;
1731 }
1732 dev_info(&port->dd->pdev->dev,
1733 "Card Type: %s\n", cbuf);
1734}
1735
1736/*
1737 * Map the commands scatter list into the command table.
1738 *
1739 * @command Pointer to the command.
1740 * @nents Number of scatter list entries.
1741 *
1742 * return value
1743 * None
1744 */
1745static inline void fill_command_sg(struct driver_data *dd,
1746 struct mtip_cmd *command,
1747 int nents)
1748{
1749 int n;
1750 unsigned int dma_len;
1751 struct mtip_cmd_sg *command_sg;
1752 struct scatterlist *sg = command->sg;
1753
1754 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1755
1756 for (n = 0; n < nents; n++) {
1757 dma_len = sg_dma_len(sg);
1758 if (dma_len > 0x400000)
1759 dev_err(&dd->pdev->dev,
1760 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001761 command_sg->info = __force_bit2int
1762 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1763 command_sg->dba = __force_bit2int
1764 cpu_to_le32(sg_dma_address(sg));
1765 command_sg->dba_upper = __force_bit2int
1766 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001767 command_sg++;
1768 sg++;
1769 }
1770}
1771
1772/*
1773 * @brief Execute a drive command.
1774 *
1775 * return value 0 The command completed successfully.
1776 * return value -1 An error occurred while executing the command.
1777 */
Jens Axboe63166682011-09-27 21:27:43 -06001778static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001779{
1780 struct host_to_dev_fis fis;
1781 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001782 unsigned int to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001783
Sam Bradshaw88523a62011-08-30 08:34:26 -06001784 /* Build the FIS. */
1785 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1786 fis.type = 0x27;
1787 fis.opts = 1 << 7;
1788 fis.command = command[0];
1789 fis.features = command[1];
1790 fis.sect_count = command[2];
1791 fis.sector = command[3];
1792 fis.cyl_low = command[4];
1793 fis.cyl_hi = command[5];
1794 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1795
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001796 mtip_set_timeout(port->dd, &fis, &to, 0);
1797
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001798 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 -06001799 __func__,
1800 command[0],
1801 command[1],
1802 command[2],
1803 command[3],
1804 command[4],
1805 command[5],
1806 command[6]);
1807
1808 /* Execute the command. */
1809 if (mtip_exec_internal_command(port,
1810 &fis,
1811 5,
1812 0,
1813 0,
1814 0,
1815 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001816 to) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001817 return -1;
1818 }
1819
1820 command[0] = reply->command; /* Status*/
1821 command[1] = reply->features; /* Error*/
1822 command[4] = reply->cyl_low;
1823 command[5] = reply->cyl_hi;
1824
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001825 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 -06001826 __func__,
1827 command[0],
1828 command[1],
1829 command[4],
1830 command[5]);
1831
Sam Bradshaw88523a62011-08-30 08:34:26 -06001832 return 0;
1833}
1834
1835/*
1836 * @brief Execute a drive command.
1837 *
1838 * @param port Pointer to the port data structure.
1839 * @param command Pointer to the user specified command parameters.
1840 * @param user_buffer Pointer to the user space buffer where read sector
1841 * data should be copied.
1842 *
1843 * return value 0 The command completed successfully.
1844 * return value -EFAULT An error occurred while copying the completion
1845 * data to the user space buffer.
1846 * return value -1 An error occurred while executing the command.
1847 */
Jens Axboe63166682011-09-27 21:27:43 -06001848static int exec_drive_command(struct mtip_port *port, u8 *command,
1849 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001850{
1851 struct host_to_dev_fis fis;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001852 struct host_to_dev_fis *reply;
1853 u8 *buf = NULL;
1854 dma_addr_t dma_addr = 0;
1855 int rv = 0, xfer_sz = command[3];
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001856 unsigned int to;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001857
1858 if (xfer_sz) {
David Milburn97651ea2012-09-12 14:06:12 -05001859 if (!user_buffer)
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001860 return -EFAULT;
1861
1862 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1863 ATA_SECT_SIZE * xfer_sz,
1864 &dma_addr,
1865 GFP_KERNEL);
1866 if (!buf) {
1867 dev_err(&port->dd->pdev->dev,
1868 "Memory allocation failed (%d bytes)\n",
1869 ATA_SECT_SIZE * xfer_sz);
1870 return -ENOMEM;
1871 }
1872 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1873 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001874
Sam Bradshaw88523a62011-08-30 08:34:26 -06001875 /* Build the FIS. */
1876 memset(&fis, 0, sizeof(struct host_to_dev_fis));
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001877 fis.type = 0x27;
1878 fis.opts = 1 << 7;
1879 fis.command = command[0];
Sam Bradshaw88523a62011-08-30 08:34:26 -06001880 fis.features = command[2];
1881 fis.sect_count = command[3];
1882 if (fis.command == ATA_CMD_SMART) {
1883 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001884 fis.cyl_low = 0x4F;
1885 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001886 }
1887
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001888 mtip_set_timeout(port->dd, &fis, &to, 0);
1889
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001890 if (xfer_sz)
1891 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1892 else
1893 reply = (port->rxfis + RX_FIS_D2H_REG);
1894
Sam Bradshaw88523a62011-08-30 08:34:26 -06001895 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001896 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06001897 "feat %x, sectcnt %x\n",
1898 __func__,
1899 command[0],
1900 command[1],
1901 command[2],
1902 command[3]);
1903
Sam Bradshaw88523a62011-08-30 08:34:26 -06001904 /* Execute the command. */
1905 if (mtip_exec_internal_command(port,
1906 &fis,
1907 5,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001908 (xfer_sz ? dma_addr : 0),
1909 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
Sam Bradshaw88523a62011-08-30 08:34:26 -06001910 0,
1911 GFP_KERNEL,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001912 to)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001913 < 0) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001914 rv = -EFAULT;
1915 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001916 }
1917
1918 /* Collect the completion status. */
1919 command[0] = reply->command; /* Status*/
1920 command[1] = reply->features; /* Error*/
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001921 command[2] = reply->sect_count;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001922
1923 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001924 " %s: Completion Status: stat %x, "
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001925 "err %x, nsect %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001926 __func__,
1927 command[0],
1928 command[1],
1929 command[2]);
1930
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001931 if (xfer_sz) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001932 if (copy_to_user(user_buffer,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001933 buf,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001934 ATA_SECT_SIZE * command[3])) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001935 rv = -EFAULT;
1936 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001937 }
1938 }
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001939exit_drive_command:
1940 if (buf)
1941 dmam_free_coherent(&port->dd->pdev->dev,
1942 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1943 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001944}
1945
1946/*
1947 * Indicates whether a command has a single sector payload.
1948 *
1949 * @command passed to the device to perform the certain event.
1950 * @features passed to the device to perform the certain event.
1951 *
1952 * return value
1953 * 1 command is one that always has a single sector payload,
1954 * regardless of the value in the Sector Count field.
1955 * 0 otherwise
1956 *
1957 */
1958static unsigned int implicit_sector(unsigned char command,
1959 unsigned char features)
1960{
1961 unsigned int rv = 0;
1962
1963 /* list of commands that have an implicit sector count of 1 */
1964 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001965 case ATA_CMD_SEC_SET_PASS:
1966 case ATA_CMD_SEC_UNLOCK:
1967 case ATA_CMD_SEC_ERASE_PREP:
1968 case ATA_CMD_SEC_ERASE_UNIT:
1969 case ATA_CMD_SEC_FREEZE_LOCK:
1970 case ATA_CMD_SEC_DISABLE_PASS:
1971 case ATA_CMD_PMP_READ:
1972 case ATA_CMD_PMP_WRITE:
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_SET_MAX:
1976 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001977 rv = 1;
1978 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001979 case ATA_CMD_SMART:
1980 if ((features == ATA_SMART_READ_VALUES) ||
1981 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001982 rv = 1;
1983 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001984 case ATA_CMD_CONF_OVERLAY:
1985 if ((features == ATA_DCO_IDENTIFY) ||
1986 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001987 rv = 1;
1988 break;
1989 }
1990 return rv;
1991}
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07001992
Sam Bradshaw88523a62011-08-30 08:34:26 -06001993/*
1994 * Executes a taskfile
1995 * See ide_taskfile_ioctl() for derivation
1996 */
1997static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06001998 void __user *buf,
1999 ide_task_request_t *req_task,
2000 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002001{
2002 struct host_to_dev_fis fis;
2003 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002004 u8 *outbuf = NULL;
2005 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06002006 dma_addr_t outbuf_dma = 0;
2007 dma_addr_t inbuf_dma = 0;
2008 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002009 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002010 unsigned int taskin = 0;
2011 unsigned int taskout = 0;
2012 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07002013 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002014 unsigned int force_single_sector;
2015 unsigned int transfer_size;
2016 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06002017 int intotal = outtotal + req_task->out_size;
Selvan Mani4453bc82012-09-27 14:36:43 +02002018 int erasemode = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002019
2020 taskout = req_task->out_size;
2021 taskin = req_task->in_size;
2022 /* 130560 = 512 * 0xFF*/
2023 if (taskin > 130560 || taskout > 130560) {
2024 err = -EINVAL;
2025 goto abort;
2026 }
2027
2028 if (taskout) {
Al Viro8ed60102016-01-02 14:56:33 -05002029 outbuf = memdup_user(buf + outtotal, taskout);
2030 if (IS_ERR(outbuf)) {
2031 err = PTR_ERR(outbuf);
2032 outbuf = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002033 goto abort;
2034 }
2035 outbuf_dma = pci_map_single(dd->pdev,
2036 outbuf,
2037 taskout,
2038 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002039 if (outbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002040 err = -ENOMEM;
2041 goto abort;
2042 }
2043 dma_buffer = outbuf_dma;
2044 }
2045
2046 if (taskin) {
Al Viro8ed60102016-01-02 14:56:33 -05002047 inbuf = memdup_user(buf + intotal, taskin);
2048 if (IS_ERR(inbuf)) {
2049 err = PTR_ERR(inbuf);
2050 inbuf = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002051 goto abort;
2052 }
2053 inbuf_dma = pci_map_single(dd->pdev,
2054 inbuf,
2055 taskin, DMA_FROM_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002056 if (inbuf_dma == 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002057 err = -ENOMEM;
2058 goto abort;
2059 }
2060 dma_buffer = inbuf_dma;
2061 }
2062
2063 /* only supports PIO and non-data commands from this ioctl. */
2064 switch (req_task->data_phase) {
2065 case TASKFILE_OUT:
2066 nsect = taskout / ATA_SECT_SIZE;
2067 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2068 break;
2069 case TASKFILE_IN:
2070 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2071 break;
2072 case TASKFILE_NO_DATA:
2073 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2074 break;
2075 default:
2076 err = -EINVAL;
2077 goto abort;
2078 }
2079
Sam Bradshaw88523a62011-08-30 08:34:26 -06002080 /* Build the FIS. */
2081 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2082
2083 fis.type = 0x27;
2084 fis.opts = 1 << 7;
2085 fis.command = req_task->io_ports[7];
2086 fis.features = req_task->io_ports[1];
2087 fis.sect_count = req_task->io_ports[2];
2088 fis.lba_low = req_task->io_ports[3];
2089 fis.lba_mid = req_task->io_ports[4];
2090 fis.lba_hi = req_task->io_ports[5];
2091 /* Clear the dev bit*/
2092 fis.device = req_task->io_ports[6] & ~0x10;
2093
2094 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2095 req_task->in_flags.all =
2096 IDE_TASKFILE_STD_IN_FLAGS |
2097 (IDE_HOB_STD_IN_FLAGS << 8);
2098 fis.lba_low_ex = req_task->hob_ports[3];
2099 fis.lba_mid_ex = req_task->hob_ports[4];
2100 fis.lba_hi_ex = req_task->hob_ports[5];
2101 fis.features_ex = req_task->hob_ports[1];
2102 fis.sect_cnt_ex = req_task->hob_ports[2];
2103
2104 } else {
2105 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2106 }
2107
2108 force_single_sector = implicit_sector(fis.command, fis.features);
2109
2110 if ((taskin || taskout) && (!fis.sect_count)) {
2111 if (nsect)
2112 fis.sect_count = nsect;
2113 else {
2114 if (!force_single_sector) {
2115 dev_warn(&dd->pdev->dev,
2116 "data movement but "
2117 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002118 err = -EINVAL;
2119 goto abort;
2120 }
2121 }
2122 }
2123
2124 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002125 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002126 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2127 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002128 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002129 fis.command,
2130 fis.features,
2131 fis.sect_count,
2132 fis.lba_low,
2133 fis.lba_mid,
2134 fis.lba_hi,
2135 fis.device);
2136
Selvan Mani4453bc82012-09-27 14:36:43 +02002137 /* check for erase mode support during secure erase.*/
Selvan Mani32087952012-11-07 06:03:37 -07002138 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2139 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
Selvan Mani4453bc82012-09-27 14:36:43 +02002140 erasemode = 1;
2141 }
2142
2143 mtip_set_timeout(dd, &fis, &timeout, erasemode);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002144
2145 /* Determine the correct transfer size.*/
2146 if (force_single_sector)
2147 transfer_size = ATA_SECT_SIZE;
2148 else
2149 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2150
2151 /* Execute the command.*/
2152 if (mtip_exec_internal_command(dd->port,
2153 &fis,
2154 5,
2155 dma_buffer,
2156 transfer_size,
2157 0,
2158 GFP_KERNEL,
2159 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002160 err = -EIO;
2161 goto abort;
2162 }
2163
2164 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2165
2166 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2167 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2168 req_task->io_ports[7] = reply->control;
2169 } else {
2170 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2171 req_task->io_ports[7] = reply->command;
2172 }
2173
2174 /* reclaim the DMA buffers.*/
2175 if (inbuf_dma)
2176 pci_unmap_single(dd->pdev, inbuf_dma,
2177 taskin, DMA_FROM_DEVICE);
2178 if (outbuf_dma)
2179 pci_unmap_single(dd->pdev, outbuf_dma,
2180 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002181 inbuf_dma = 0;
2182 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002183
2184 /* return the ATA registers to the caller.*/
2185 req_task->io_ports[1] = reply->features;
2186 req_task->io_ports[2] = reply->sect_count;
2187 req_task->io_ports[3] = reply->lba_low;
2188 req_task->io_ports[4] = reply->lba_mid;
2189 req_task->io_ports[5] = reply->lba_hi;
2190 req_task->io_ports[6] = reply->device;
2191
2192 if (req_task->out_flags.all & 1) {
2193
2194 req_task->hob_ports[3] = reply->lba_low_ex;
2195 req_task->hob_ports[4] = reply->lba_mid_ex;
2196 req_task->hob_ports[5] = reply->lba_hi_ex;
2197 req_task->hob_ports[1] = reply->features_ex;
2198 req_task->hob_ports[2] = reply->sect_cnt_ex;
2199 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002200 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002201 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002202 "err %x, sect_cnt %x, lbalo %x,"
2203 "lbamid %x, lbahi %x, dev %x\n",
2204 __func__,
2205 req_task->io_ports[7],
2206 req_task->io_ports[1],
2207 req_task->io_ports[2],
2208 req_task->io_ports[3],
2209 req_task->io_ports[4],
2210 req_task->io_ports[5],
2211 req_task->io_ports[6]);
2212
Sam Bradshaw88523a62011-08-30 08:34:26 -06002213 if (taskout) {
2214 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2215 err = -EFAULT;
2216 goto abort;
2217 }
2218 }
2219 if (taskin) {
2220 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2221 err = -EFAULT;
2222 goto abort;
2223 }
2224 }
2225abort:
2226 if (inbuf_dma)
2227 pci_unmap_single(dd->pdev, inbuf_dma,
2228 taskin, DMA_FROM_DEVICE);
2229 if (outbuf_dma)
2230 pci_unmap_single(dd->pdev, outbuf_dma,
2231 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002232 kfree(outbuf);
2233 kfree(inbuf);
2234
2235 return err;
2236}
2237
2238/*
2239 * Handle IOCTL calls from the Block Layer.
2240 *
2241 * This function is called by the Block Layer when it receives an IOCTL
2242 * command that it does not understand. If the IOCTL command is not supported
2243 * this function returns -ENOTTY.
2244 *
2245 * @dd Pointer to the driver data structure.
2246 * @cmd IOCTL command passed from the Block Layer.
2247 * @arg IOCTL argument passed from the Block Layer.
2248 *
2249 * return value
2250 * 0 The IOCTL completed successfully.
2251 * -ENOTTY The specified command is not supported.
2252 * -EFAULT An error occurred copying data to a user space buffer.
2253 * -EIO An error occurred while executing the command.
2254 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002255static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2256 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002257{
2258 switch (cmd) {
2259 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002260 {
2261 if (copy_to_user((void __user *)arg, dd->port->identify,
2262 sizeof(u16) * ATA_ID_WORDS))
2263 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002264 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002265 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002266 case HDIO_DRIVE_CMD:
2267 {
2268 u8 drive_command[4];
2269
2270 /* Copy the user command info to our buffer. */
2271 if (copy_from_user(drive_command,
2272 (void __user *) arg,
2273 sizeof(drive_command)))
2274 return -EFAULT;
2275
2276 /* Execute the drive command. */
2277 if (exec_drive_command(dd->port,
2278 drive_command,
2279 (void __user *) (arg+4)))
2280 return -EIO;
2281
2282 /* Copy the status back to the users buffer. */
2283 if (copy_to_user((void __user *) arg,
2284 drive_command,
2285 sizeof(drive_command)))
2286 return -EFAULT;
2287
2288 break;
2289 }
2290 case HDIO_DRIVE_TASK:
2291 {
2292 u8 drive_command[7];
2293
2294 /* Copy the user command info to our buffer. */
2295 if (copy_from_user(drive_command,
2296 (void __user *) arg,
2297 sizeof(drive_command)))
2298 return -EFAULT;
2299
2300 /* Execute the drive command. */
2301 if (exec_drive_task(dd->port, drive_command))
2302 return -EIO;
2303
2304 /* Copy the status back to the users buffer. */
2305 if (copy_to_user((void __user *) arg,
2306 drive_command,
2307 sizeof(drive_command)))
2308 return -EFAULT;
2309
2310 break;
2311 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002312 case HDIO_DRIVE_TASKFILE: {
2313 ide_task_request_t req_task;
2314 int ret, outtotal;
2315
2316 if (copy_from_user(&req_task, (void __user *) arg,
2317 sizeof(req_task)))
2318 return -EFAULT;
2319
2320 outtotal = sizeof(req_task);
2321
2322 ret = exec_drive_taskfile(dd, (void __user *) arg,
2323 &req_task, outtotal);
2324
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002325 if (copy_to_user((void __user *) arg, &req_task,
2326 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002327 return -EFAULT;
2328
2329 return ret;
2330 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002331
2332 default:
2333 return -EINVAL;
2334 }
2335 return 0;
2336}
2337
2338/*
2339 * Submit an IO to the hw
2340 *
2341 * This function is called by the block layer to issue an io
2342 * to the device. Upon completion, the callback function will
2343 * be called with the data parameter passed as the callback data.
2344 *
2345 * @dd Pointer to the driver data structure.
2346 * @start First sector to read.
2347 * @nsect Number of sectors to read.
2348 * @nents Number of entries in scatter list for the read command.
2349 * @tag The tag of this read command.
2350 * @callback Pointer to the function that should be called
2351 * when the read completes.
2352 * @data Callback data passed to the callback function
2353 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002354 * @dir Direction (read or write)
2355 *
2356 * return value
2357 * None
2358 */
Jens Axboeffc771b2014-05-09 09:42:02 -06002359static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq,
2360 struct mtip_cmd *command, int nents,
2361 struct blk_mq_hw_ctx *hctx)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002362{
2363 struct host_to_dev_fis *fis;
2364 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -06002365 int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2366 u64 start = blk_rq_pos(rq);
2367 unsigned int nsect = blk_rq_sectors(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002368
2369 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002370 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002371
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002372 prefetch(&port->flags);
2373
Sam Bradshaw88523a62011-08-30 08:34:26 -06002374 command->scatter_ents = nents;
2375
2376 /*
2377 * The number of retries for this command before it is
2378 * reported as a failure to the upper layers.
2379 */
2380 command->retries = MTIP_MAX_RETRIES;
2381
2382 /* Fill out fis */
2383 fis = command->command;
2384 fis->type = 0x27;
2385 fis->opts = 1 << 7;
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002386 if (dma_dir == DMA_FROM_DEVICE)
Jens Axboeffc771b2014-05-09 09:42:02 -06002387 fis->command = ATA_CMD_FPDMA_READ;
2388 else
2389 fis->command = ATA_CMD_FPDMA_WRITE;
Selvan Manieda45312012-11-07 06:03:53 -07002390 fis->lba_low = start & 0xFF;
2391 fis->lba_mid = (start >> 8) & 0xFF;
2392 fis->lba_hi = (start >> 16) & 0xFF;
2393 fis->lba_low_ex = (start >> 24) & 0xFF;
2394 fis->lba_mid_ex = (start >> 32) & 0xFF;
2395 fis->lba_hi_ex = (start >> 40) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002396 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002397 fis->features = nsect & 0xFF;
2398 fis->features_ex = (nsect >> 8) & 0xFF;
Jens Axboeffc771b2014-05-09 09:42:02 -06002399 fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002400 fis->sect_cnt_ex = 0;
2401 fis->control = 0;
2402 fis->res2 = 0;
2403 fis->res3 = 0;
2404 fill_command_sg(dd, command, nents);
2405
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002406 if (unlikely(command->unaligned))
Asai Thambi S P2077d942013-04-29 21:19:49 +02002407 fis->device |= 1 << 7;
2408
Sam Bradshaw88523a62011-08-30 08:34:26 -06002409 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002410 command->command_header->opts =
2411 __force_bit2int cpu_to_le32(
2412 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002413 command->command_header->byte_count = 0;
2414
2415 /*
2416 * Set the completion function and data for the command
2417 * within this layer.
2418 */
2419 command->comp_data = dd;
2420 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002421 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002422
2423 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002424 * To prevent this command from being issued
2425 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002426 */
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002427 if (unlikely(port->flags & MTIP_PF_PAUSE_IO)) {
Jens Axboeffc771b2014-05-09 09:42:02 -06002428 set_bit(rq->tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002429 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002430 return;
2431 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002432
2433 /* Issue the command to the hardware */
Jens Axboeffc771b2014-05-09 09:42:02 -06002434 mtip_issue_ncq_command(port, rq->tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002435}
2436
2437/*
Asai Thambi S P7412ff12012-06-04 12:43:03 -07002438 * Sysfs status dump.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002439 *
2440 * @dev Pointer to the device structure, passed by the kernrel.
2441 * @attr Pointer to the device_attribute structure passed by the kernel.
2442 * @buf Pointer to the char buffer that will receive the stats info.
2443 *
2444 * return value
2445 * The size, in bytes, of the data copied into buf.
2446 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002447static ssize_t mtip_hw_show_status(struct device *dev,
2448 struct device_attribute *attr,
2449 char *buf)
2450{
2451 struct driver_data *dd = dev_to_disk(dev)->private_data;
2452 int size = 0;
2453
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002454 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002455 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002456 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002457 size += sprintf(buf, "%s", "write_protect\n");
2458 else
2459 size += sprintf(buf, "%s", "online\n");
2460
2461 return size;
2462}
2463
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002464static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002465
Asai Thambi S P0caff002013-04-03 19:56:21 +05302466/* debugsfs entries */
2467
2468static ssize_t show_device_status(struct device_driver *drv, char *buf)
2469{
2470 int size = 0;
2471 struct driver_data *dd, *tmp;
2472 unsigned long flags;
2473 char id_buf[42];
2474 u16 status = 0;
2475
2476 spin_lock_irqsave(&dev_lock, flags);
2477 size += sprintf(&buf[size], "Devices Present:\n");
2478 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002479 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302480 if (dd->port &&
2481 dd->port->identify &&
2482 dd->port->identify_valid) {
2483 strlcpy(id_buf,
2484 (char *) (dd->port->identify + 10), 21);
2485 status = *(dd->port->identify + 141);
2486 } else {
2487 memset(id_buf, 0, 42);
2488 status = 0;
2489 }
2490
2491 if (dd->port &&
2492 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2493 size += sprintf(&buf[size],
2494 " device %s %s (ftl rebuild %d %%)\n",
2495 dev_name(&dd->pdev->dev),
2496 id_buf,
2497 status);
2498 } else {
2499 size += sprintf(&buf[size],
2500 " device %s %s\n",
2501 dev_name(&dd->pdev->dev),
2502 id_buf);
2503 }
2504 }
2505 }
2506
2507 size += sprintf(&buf[size], "Devices Being Removed:\n");
2508 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002509 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302510 if (dd->port &&
2511 dd->port->identify &&
2512 dd->port->identify_valid) {
2513 strlcpy(id_buf,
2514 (char *) (dd->port->identify+10), 21);
2515 status = *(dd->port->identify + 141);
2516 } else {
2517 memset(id_buf, 0, 42);
2518 status = 0;
2519 }
2520
2521 if (dd->port &&
2522 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2523 size += sprintf(&buf[size],
2524 " device %s %s (ftl rebuild %d %%)\n",
2525 dev_name(&dd->pdev->dev),
2526 id_buf,
2527 status);
2528 } else {
2529 size += sprintf(&buf[size],
2530 " device %s %s\n",
2531 dev_name(&dd->pdev->dev),
2532 id_buf);
2533 }
2534 }
2535 }
2536 spin_unlock_irqrestore(&dev_lock, flags);
2537
2538 return size;
2539}
2540
2541static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2542 size_t len, loff_t *offset)
2543{
David Milburnc8afd0d2013-05-23 16:23:45 -05002544 struct driver_data *dd = (struct driver_data *)f->private_data;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302545 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002546 char *buf;
2547 int rv = 0;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302548
2549 if (!len || *offset)
2550 return 0;
2551
David Milburnc8afd0d2013-05-23 16:23:45 -05002552 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2553 if (!buf) {
2554 dev_err(&dd->pdev->dev,
2555 "Memory allocation: status buffer\n");
2556 return -ENOMEM;
2557 }
2558
Asai Thambi S P0caff002013-04-03 19:56:21 +05302559 size += show_device_status(NULL, buf);
2560
2561 *offset = size <= len ? size : len;
2562 size = copy_to_user(ubuf, buf, *offset);
2563 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002564 rv = -EFAULT;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302565
David Milburnc8afd0d2013-05-23 16:23:45 -05002566 kfree(buf);
2567 return rv ? rv : *offset;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302568}
2569
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002570static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2571 size_t len, loff_t *offset)
2572{
2573 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002574 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002575 u32 group_allocated;
2576 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002577 int n, rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002578
2579 if (!len || size)
2580 return 0;
2581
David Milburnc8afd0d2013-05-23 16:23:45 -05002582 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2583 if (!buf) {
2584 dev_err(&dd->pdev->dev,
2585 "Memory allocation: register buffer\n");
2586 return -ENOMEM;
2587 }
2588
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002589 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2590
2591 for (n = dd->slot_groups-1; n >= 0; n--)
2592 size += sprintf(&buf[size], "%08X ",
2593 readl(dd->port->s_active[n]));
2594
2595 size += sprintf(&buf[size], "]\n");
2596 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2597
2598 for (n = dd->slot_groups-1; n >= 0; n--)
2599 size += sprintf(&buf[size], "%08X ",
2600 readl(dd->port->cmd_issue[n]));
2601
2602 size += sprintf(&buf[size], "]\n");
2603 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2604
2605 for (n = dd->slot_groups-1; n >= 0; n--)
2606 size += sprintf(&buf[size], "%08X ",
2607 readl(dd->port->completed[n]));
2608
2609 size += sprintf(&buf[size], "]\n");
2610 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2611 readl(dd->port->mmio + PORT_IRQ_STAT));
2612 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2613 readl(dd->mmio + HOST_IRQ_STAT));
2614 size += sprintf(&buf[size], "\n");
2615
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002616 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2617
2618 for (n = dd->slot_groups-1; n >= 0; n--) {
2619 if (sizeof(long) > sizeof(u32))
2620 group_allocated =
2621 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2622 else
2623 group_allocated = dd->port->cmds_to_issue[n];
2624 size += sprintf(&buf[size], "%08X ", group_allocated);
2625 }
2626 size += sprintf(&buf[size], "]\n");
2627
2628 *offset = size <= len ? size : len;
2629 size = copy_to_user(ubuf, buf, *offset);
2630 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002631 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002632
David Milburnc8afd0d2013-05-23 16:23:45 -05002633 kfree(buf);
2634 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002635}
2636
2637static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2638 size_t len, loff_t *offset)
2639{
2640 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002641 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002642 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002643 int rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002644
2645 if (!len || size)
2646 return 0;
2647
David Milburnc8afd0d2013-05-23 16:23:45 -05002648 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2649 if (!buf) {
2650 dev_err(&dd->pdev->dev,
2651 "Memory allocation: flag buffer\n");
2652 return -ENOMEM;
2653 }
2654
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002655 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2656 dd->port->flags);
2657 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2658 dd->dd_flag);
2659
2660 *offset = size <= len ? size : len;
2661 size = copy_to_user(ubuf, buf, *offset);
2662 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002663 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002664
David Milburnc8afd0d2013-05-23 16:23:45 -05002665 kfree(buf);
2666 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002667}
2668
Asai Thambi S P0caff002013-04-03 19:56:21 +05302669static const struct file_operations mtip_device_status_fops = {
2670 .owner = THIS_MODULE,
2671 .open = simple_open,
2672 .read = mtip_hw_read_device_status,
2673 .llseek = no_llseek,
2674};
2675
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002676static const struct file_operations mtip_regs_fops = {
2677 .owner = THIS_MODULE,
2678 .open = simple_open,
2679 .read = mtip_hw_read_registers,
2680 .llseek = no_llseek,
2681};
2682
2683static const struct file_operations mtip_flags_fops = {
2684 .owner = THIS_MODULE,
2685 .open = simple_open,
2686 .read = mtip_hw_read_flags,
2687 .llseek = no_llseek,
2688};
2689
Sam Bradshaw88523a62011-08-30 08:34:26 -06002690/*
2691 * Create the sysfs related attributes.
2692 *
2693 * @dd Pointer to the driver data structure.
2694 * @kobj Pointer to the kobj for the block device.
2695 *
2696 * return value
2697 * 0 Operation completed successfully.
2698 * -EINVAL Invalid parameter.
2699 */
Jens Axboe63166682011-09-27 21:27:43 -06002700static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002701{
2702 if (!kobj || !dd)
2703 return -EINVAL;
2704
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002705 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2706 dev_warn(&dd->pdev->dev,
2707 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002708 return 0;
2709}
2710
2711/*
2712 * Remove the sysfs related attributes.
2713 *
2714 * @dd Pointer to the driver data structure.
2715 * @kobj Pointer to the kobj for the block device.
2716 *
2717 * return value
2718 * 0 Operation completed successfully.
2719 * -EINVAL Invalid parameter.
2720 */
Jens Axboe63166682011-09-27 21:27:43 -06002721static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002722{
2723 if (!kobj || !dd)
2724 return -EINVAL;
2725
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002726 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002727
2728 return 0;
2729}
2730
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002731static int mtip_hw_debugfs_init(struct driver_data *dd)
2732{
2733 if (!dfs_parent)
2734 return -1;
2735
2736 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2737 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2738 dev_warn(&dd->pdev->dev,
2739 "Error creating node %s under debugfs\n",
2740 dd->disk->disk_name);
2741 dd->dfs_node = NULL;
2742 return -1;
2743 }
2744
2745 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2746 &mtip_flags_fops);
2747 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2748 &mtip_regs_fops);
2749
2750 return 0;
2751}
2752
2753static void mtip_hw_debugfs_exit(struct driver_data *dd)
2754{
Sam Bradshaw974a51a2013-05-15 10:04:34 +02002755 if (dd->dfs_node)
2756 debugfs_remove_recursive(dd->dfs_node);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002757}
2758
Sam Bradshaw88523a62011-08-30 08:34:26 -06002759/*
2760 * Perform any init/resume time hardware setup
2761 *
2762 * @dd Pointer to the driver data structure.
2763 *
2764 * return value
2765 * None
2766 */
2767static inline void hba_setup(struct driver_data *dd)
2768{
2769 u32 hwdata;
2770 hwdata = readl(dd->mmio + HOST_HSORG);
2771
2772 /* interrupt bug workaround: use only 1 IS bit.*/
2773 writel(hwdata |
2774 HSORG_DISABLE_SLOTGRP_INTR |
2775 HSORG_DISABLE_SLOTGRP_PXIS,
2776 dd->mmio + HOST_HSORG);
2777}
2778
Asai Thambi S P2077d942013-04-29 21:19:49 +02002779static int mtip_device_unaligned_constrained(struct driver_data *dd)
2780{
2781 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
2782}
2783
Sam Bradshaw88523a62011-08-30 08:34:26 -06002784/*
2785 * Detect the details of the product, and store anything needed
2786 * into the driver data structure. This includes product type and
2787 * version and number of slot groups.
2788 *
2789 * @dd Pointer to the driver data structure.
2790 *
2791 * return value
2792 * None
2793 */
2794static void mtip_detect_product(struct driver_data *dd)
2795{
2796 u32 hwdata;
2797 unsigned int rev, slotgroups;
2798
2799 /*
2800 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2801 * info register:
2802 * [15:8] hardware/software interface rev#
2803 * [ 3] asic-style interface
2804 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2805 */
2806 hwdata = readl(dd->mmio + HOST_HSORG);
2807
2808 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2809 dd->slot_groups = 1;
2810
2811 if (hwdata & 0x8) {
2812 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2813 rev = (hwdata & HSORG_HWREV) >> 8;
2814 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2815 dev_info(&dd->pdev->dev,
2816 "ASIC-FPGA design, HS rev 0x%x, "
2817 "%i slot groups [%i slots]\n",
2818 rev,
2819 slotgroups,
2820 slotgroups * 32);
2821
2822 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2823 dev_warn(&dd->pdev->dev,
2824 "Warning: driver only supports "
2825 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2826 slotgroups = MTIP_MAX_SLOT_GROUPS;
2827 }
2828 dd->slot_groups = slotgroups;
2829 return;
2830 }
2831
2832 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2833}
2834
2835/*
2836 * Blocking wait for FTL rebuild to complete
2837 *
2838 * @dd Pointer to the DRIVER_DATA structure.
2839 *
2840 * return value
2841 * 0 FTL rebuild completed successfully
2842 * -EFAULT FTL rebuild error/timeout/interruption
2843 */
2844static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2845{
2846 unsigned long timeout, cnt = 0, start;
2847
2848 dev_warn(&dd->pdev->dev,
2849 "FTL rebuild in progress. Polling for completion.\n");
2850
2851 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002852 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2853
2854 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002855 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002856 &dd->dd_flag)))
2857 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002858 if (mtip_check_surprise_removal(dd->pdev))
2859 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002860
Sam Bradshaw88523a62011-08-30 08:34:26 -06002861 if (mtip_get_identify(dd->port, NULL) < 0)
2862 return -EFAULT;
2863
2864 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2865 MTIP_FTL_REBUILD_MAGIC) {
2866 ssleep(1);
2867 /* Print message every 3 minutes */
2868 if (cnt++ >= 180) {
2869 dev_warn(&dd->pdev->dev,
2870 "FTL rebuild in progress (%d secs).\n",
2871 jiffies_to_msecs(jiffies - start) / 1000);
2872 cnt = 0;
2873 }
2874 } else {
2875 dev_warn(&dd->pdev->dev,
2876 "FTL rebuild complete (%d secs).\n",
2877 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002878 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002879 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002880 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002881 } while (time_before(jiffies, timeout));
2882
2883 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002884 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002885 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2886 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002887 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002888}
2889
2890/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002891 * service thread to issue queued commands
2892 *
2893 * @data Pointer to the driver data structure.
2894 *
2895 * return value
2896 * 0
2897 */
2898
2899static int mtip_service_thread(void *data)
2900{
2901 struct driver_data *dd = (struct driver_data *)data;
2902 unsigned long slot, slot_start, slot_wrap;
2903 unsigned int num_cmd_slots = dd->slot_groups * 32;
2904 struct mtip_port *port = dd->port;
2905
2906 while (1) {
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002907 if (kthread_should_stop() ||
2908 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2909 goto st_out;
2910 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2911
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002912 /*
2913 * the condition is to check neither an internal command is
2914 * is in progress nor error handling is active
2915 */
2916 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi SPcfc05bd2016-02-24 21:16:00 -08002917 (port->flags & MTIP_PF_SVC_THD_WORK));
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002918
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002919 if (kthread_should_stop() ||
2920 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2921 goto st_out;
2922
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002923 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002924 &dd->dd_flag)))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002925 goto st_out;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002926
Asai Thambi SPcfc05bd2016-02-24 21:16:00 -08002927 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2928
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002929restart_eh:
2930 /* Demux bits: start with error handling */
2931 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) {
2932 mtip_handle_tfe(dd);
2933 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
2934 }
2935
2936 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags))
2937 goto restart_eh;
2938
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002939 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002940 slot = 1;
2941 /* used to restrict the loop to one iteration */
2942 slot_start = num_cmd_slots;
2943 slot_wrap = 0;
2944 while (1) {
2945 slot = find_next_bit(port->cmds_to_issue,
2946 num_cmd_slots, slot);
2947 if (slot_wrap == 1) {
2948 if ((slot_start >= slot) ||
2949 (slot >= num_cmd_slots))
2950 break;
2951 }
2952 if (unlikely(slot_start == num_cmd_slots))
2953 slot_start = slot;
2954
2955 if (unlikely(slot == num_cmd_slots)) {
2956 slot = 1;
2957 slot_wrap = 1;
2958 continue;
2959 }
2960
2961 /* Issue the command to the hardware */
2962 mtip_issue_ncq_command(port, slot);
2963
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002964 clear_bit(slot, port->cmds_to_issue);
2965 }
2966
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002967 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002968 }
2969
2970 if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002971 if (mtip_ftl_rebuild_poll(dd) < 0)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002972 set_bit(MTIP_DDF_REBUILD_FAILED_BIT,
Asai Thambi S P8182b492012-04-09 08:35:38 +02002973 &dd->dd_flag);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002974 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002975 }
2976 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002977
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002978st_out:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002979 return 0;
2980}
2981
2982/*
Sam Bradshaw188b9f42014-01-15 10:14:57 -08002983 * DMA region teardown
2984 *
2985 * @dd Pointer to driver_data structure
2986 *
2987 * return value
2988 * None
2989 */
2990static void mtip_dma_free(struct driver_data *dd)
2991{
Sam Bradshaw188b9f42014-01-15 10:14:57 -08002992 struct mtip_port *port = dd->port;
2993
2994 if (port->block1)
2995 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
2996 port->block1, port->block1_dma);
2997
2998 if (port->command_list) {
2999 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3000 port->command_list, port->command_list_dma);
3001 }
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003002}
3003
3004/*
3005 * DMA region setup
3006 *
3007 * @dd Pointer to driver_data structure
3008 *
3009 * return value
3010 * -ENOMEM Not enough free DMA region space to initialize driver
3011 */
3012static int mtip_dma_alloc(struct driver_data *dd)
3013{
3014 struct mtip_port *port = dd->port;
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003015
3016 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
3017 port->block1 =
3018 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3019 &port->block1_dma, GFP_KERNEL);
3020 if (!port->block1)
3021 return -ENOMEM;
3022 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
3023
3024 /* Allocate dma memory for command list */
3025 port->command_list =
3026 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3027 &port->command_list_dma, GFP_KERNEL);
3028 if (!port->command_list) {
3029 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3030 port->block1, port->block1_dma);
3031 port->block1 = NULL;
3032 port->block1_dma = 0;
3033 return -ENOMEM;
3034 }
3035 memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
3036
3037 /* Setup all pointers into first DMA region */
3038 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
3039 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
3040 port->identify = port->block1 + AHCI_IDFY_OFFSET;
3041 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
3042 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
3043 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
3044 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
3045 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
3046
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003047 return 0;
3048}
3049
Jens Axboeffc771b2014-05-09 09:42:02 -06003050static int mtip_hw_get_identify(struct driver_data *dd)
3051{
3052 struct smart_attr attr242;
3053 unsigned char *buf;
3054 int rv;
3055
3056 if (mtip_get_identify(dd->port, NULL) < 0)
3057 return -EFAULT;
3058
3059 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3060 MTIP_FTL_REBUILD_MAGIC) {
3061 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3062 return MTIP_FTL_REBUILD_MAGIC;
3063 }
3064 mtip_dump_identify(dd->port);
3065
3066 /* check write protect, over temp and rebuild statuses */
3067 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3068 dd->port->log_buf,
3069 dd->port->log_buf_dma, 1);
3070 if (rv) {
3071 dev_warn(&dd->pdev->dev,
3072 "Error in READ LOG EXT (10h) command\n");
3073 /* non-critical error, don't fail the load */
3074 } else {
3075 buf = (unsigned char *)dd->port->log_buf;
3076 if (buf[259] & 0x1) {
3077 dev_info(&dd->pdev->dev,
3078 "Write protect bit is set.\n");
3079 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3080 }
3081 if (buf[288] == 0xF7) {
3082 dev_info(&dd->pdev->dev,
3083 "Exceeded Tmax, drive in thermal shutdown.\n");
3084 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3085 }
3086 if (buf[288] == 0xBF) {
3087 dev_info(&dd->pdev->dev,
3088 "Drive indicates rebuild has failed.\n");
3089 /* TODO */
3090 }
3091 }
3092
3093 /* get write protect progess */
3094 memset(&attr242, 0, sizeof(struct smart_attr));
3095 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3096 dev_warn(&dd->pdev->dev,
3097 "Unable to check write protect progress\n");
3098 else
3099 dev_info(&dd->pdev->dev,
3100 "Write protect progress: %u%% (%u blocks)\n",
3101 attr242.cur, le32_to_cpu(attr242.data));
3102
3103 return rv;
3104}
3105
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003106/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003107 * Called once for each card.
3108 *
3109 * @dd Pointer to the driver data structure.
3110 *
3111 * return value
3112 * 0 on success, else an error code.
3113 */
Jens Axboe63166682011-09-27 21:27:43 -06003114static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003115{
3116 int i;
3117 int rv;
3118 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02003119 unsigned long timeout, timetaken;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003120
3121 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3122
3123 mtip_detect_product(dd);
3124 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3125 rv = -EIO;
3126 goto out1;
3127 }
3128 num_command_slots = dd->slot_groups * 32;
3129
3130 hba_setup(dd);
3131
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003132 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3133 dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003134 if (!dd->port) {
3135 dev_err(&dd->pdev->dev,
3136 "Memory allocation: port structure\n");
3137 return -ENOMEM;
3138 }
3139
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003140 /* Continue workqueue setup */
3141 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3142 dd->work[i].port = dd->port;
3143
Asai Thambi S P2077d942013-04-29 21:19:49 +02003144 /* Enable unaligned IO constraints for some devices */
3145 if (mtip_device_unaligned_constrained(dd))
3146 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3147 else
3148 dd->unal_qdepth = 0;
3149
Asai Thambi S P2077d942013-04-29 21:19:49 +02003150 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003151
3152 /* Spinlock to prevent concurrent issue */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003153 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3154 spin_lock_init(&dd->port->cmd_issue_lock[i]);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003155
3156 /* Set the port mmio base address. */
3157 dd->port->mmio = dd->mmio + PORT_OFFSET;
3158 dd->port->dd = dd;
3159
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003160 /* DMA allocations */
3161 rv = mtip_dma_alloc(dd);
3162 if (rv < 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003163 goto out1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003164
3165 /* Setup the pointers to the extended s_active and CI registers. */
3166 for (i = 0; i < dd->slot_groups; i++) {
3167 dd->port->s_active[i] =
3168 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3169 dd->port->cmd_issue[i] =
3170 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3171 dd->port->completed[i] =
3172 dd->port->mmio + i*0x80 + PORT_SDBV;
3173 }
3174
Asai Thambi S P45038362012-04-09 08:35:38 +02003175 timetaken = jiffies;
3176 timeout = jiffies + msecs_to_jiffies(30000);
3177 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3178 time_before(jiffies, timeout)) {
3179 mdelay(100);
3180 }
3181 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3182 timetaken = jiffies - timetaken;
3183 dev_warn(&dd->pdev->dev,
3184 "Surprise removal detected at %u ms\n",
3185 jiffies_to_msecs(timetaken));
3186 rv = -ENODEV;
3187 goto out2 ;
3188 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003189 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003190 timetaken = jiffies - timetaken;
3191 dev_warn(&dd->pdev->dev,
3192 "Removal detected at %u ms\n",
3193 jiffies_to_msecs(timetaken));
3194 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003195 goto out2;
3196 }
3197
Asai Thambi S P45038362012-04-09 08:35:38 +02003198 /* Conditionally reset the HBA. */
3199 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3200 if (mtip_hba_reset(dd) < 0) {
3201 dev_err(&dd->pdev->dev,
3202 "Card did not reset within timeout\n");
3203 rv = -EIO;
3204 goto out2;
3205 }
3206 } else {
3207 /* Clear any pending interrupts on the HBA */
3208 writel(readl(dd->mmio + HOST_IRQ_STAT),
3209 dd->mmio + HOST_IRQ_STAT);
3210 }
3211
Sam Bradshaw88523a62011-08-30 08:34:26 -06003212 mtip_init_port(dd->port);
3213 mtip_start_port(dd->port);
3214
3215 /* Setup the ISR and enable interrupts. */
3216 rv = devm_request_irq(&dd->pdev->dev,
3217 dd->pdev->irq,
3218 mtip_irq_handler,
3219 IRQF_SHARED,
3220 dev_driver_string(&dd->pdev->dev),
3221 dd);
3222
3223 if (rv) {
3224 dev_err(&dd->pdev->dev,
3225 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3226 goto out2;
3227 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003228 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003229
3230 /* Enable interrupts on the HBA. */
3231 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3232 dd->mmio + HOST_CTL);
3233
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003234 init_waitqueue_head(&dd->port->svc_wait);
3235
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003236 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003237 rv = -EFAULT;
3238 goto out3;
3239 }
3240
Sam Bradshaw88523a62011-08-30 08:34:26 -06003241 return rv;
3242
3243out3:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003244 /* Disable interrupts on the HBA. */
3245 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3246 dd->mmio + HOST_CTL);
3247
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003248 /* Release the IRQ. */
3249 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003250 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3251
3252out2:
3253 mtip_deinit_port(dd->port);
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003254 mtip_dma_free(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003255
Sam Bradshaw88523a62011-08-30 08:34:26 -06003256out1:
3257 /* Free the memory allocated for the for structure. */
3258 kfree(dd->port);
3259
3260 return rv;
3261}
3262
Jens Axboeffc771b2014-05-09 09:42:02 -06003263static void mtip_standby_drive(struct driver_data *dd)
3264{
3265 if (dd->sr)
3266 return;
3267
3268 /*
3269 * Send standby immediate (E0h) to the drive so that it
3270 * saves its state.
3271 */
3272 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
3273 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))
3274 if (mtip_standby_immediate(dd->port))
3275 dev_warn(&dd->pdev->dev,
3276 "STANDBY IMMEDIATE failed\n");
3277}
3278
Sam Bradshaw88523a62011-08-30 08:34:26 -06003279/*
3280 * Called to deinitialize an interface.
3281 *
3282 * @dd Pointer to the driver data structure.
3283 *
3284 * return value
3285 * 0
3286 */
Jens Axboe63166682011-09-27 21:27:43 -06003287static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003288{
3289 /*
3290 * Send standby immediate (E0h) to the drive so that it
3291 * saves its state.
3292 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003293 if (!dd->sr) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003294 /* de-initialize the port. */
3295 mtip_deinit_port(dd->port);
3296
3297 /* Disable interrupts on the HBA. */
3298 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3299 dd->mmio + HOST_CTL);
3300 }
3301
Sam Bradshaw88523a62011-08-30 08:34:26 -06003302 /* Release the IRQ. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003303 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003304 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
Asai Thambi SP2132a542015-05-11 15:53:18 -07003305 msleep(1000);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003306
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003307 /* Free dma regions */
3308 mtip_dma_free(dd);
3309
Sam Bradshaw88523a62011-08-30 08:34:26 -06003310 /* Free the memory allocated for the for structure. */
3311 kfree(dd->port);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003312 dd->port = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003313
3314 return 0;
3315}
3316
3317/*
3318 * Issue a Standby Immediate command to the device.
3319 *
3320 * This function is called by the Block Layer just before the
3321 * system powers off during a shutdown.
3322 *
3323 * @dd Pointer to the driver data structure.
3324 *
3325 * return value
3326 * 0
3327 */
Jens Axboe63166682011-09-27 21:27:43 -06003328static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003329{
3330 /*
3331 * Send standby immediate (E0h) to the drive so that it
3332 * saves its state.
3333 */
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003334 if (!dd->sr && dd->port)
3335 mtip_standby_immediate(dd->port);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003336
3337 return 0;
3338}
3339
3340/*
3341 * Suspend function
3342 *
3343 * This function is called by the Block Layer just before the
3344 * system hibernates.
3345 *
3346 * @dd Pointer to the driver data structure.
3347 *
3348 * return value
3349 * 0 Suspend was successful
3350 * -EFAULT Suspend was not successful
3351 */
Jens Axboe63166682011-09-27 21:27:43 -06003352static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003353{
3354 /*
3355 * Send standby immediate (E0h) to the drive
3356 * so that it saves its state.
3357 */
3358 if (mtip_standby_immediate(dd->port) != 0) {
3359 dev_err(&dd->pdev->dev,
3360 "Failed standby-immediate command\n");
3361 return -EFAULT;
3362 }
3363
3364 /* Disable interrupts on the HBA.*/
3365 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3366 dd->mmio + HOST_CTL);
3367 mtip_deinit_port(dd->port);
3368
3369 return 0;
3370}
3371
3372/*
3373 * Resume function
3374 *
3375 * This function is called by the Block Layer as the
3376 * system resumes.
3377 *
3378 * @dd Pointer to the driver data structure.
3379 *
3380 * return value
3381 * 0 Resume was successful
3382 * -EFAULT Resume was not successful
3383 */
Jens Axboe63166682011-09-27 21:27:43 -06003384static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003385{
3386 /* Perform any needed hardware setup steps */
3387 hba_setup(dd);
3388
3389 /* Reset the HBA */
3390 if (mtip_hba_reset(dd) != 0) {
3391 dev_err(&dd->pdev->dev,
3392 "Unable to reset the HBA\n");
3393 return -EFAULT;
3394 }
3395
3396 /*
3397 * Enable the port, DMA engine, and FIS reception specific
3398 * h/w in controller.
3399 */
3400 mtip_init_port(dd->port);
3401 mtip_start_port(dd->port);
3402
3403 /* Enable interrupts on the HBA.*/
3404 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3405 dd->mmio + HOST_CTL);
3406
3407 return 0;
3408}
3409
3410/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003411 * Helper function for reusing disk name
3412 * upon hot insertion.
3413 */
3414static int rssd_disk_name_format(char *prefix,
3415 int index,
3416 char *buf,
3417 int buflen)
3418{
3419 const int base = 'z' - 'a' + 1;
3420 char *begin = buf + strlen(prefix);
3421 char *end = buf + buflen;
3422 char *p;
3423 int unit;
3424
3425 p = end - 1;
3426 *p = '\0';
3427 unit = base;
3428 do {
3429 if (p == begin)
3430 return -EINVAL;
3431 *--p = 'a' + (index % unit);
3432 index = (index / unit) - 1;
3433 } while (index >= 0);
3434
3435 memmove(begin, p, end - p);
3436 memcpy(buf, prefix, strlen(prefix));
3437
3438 return 0;
3439}
3440
3441/*
3442 * Block layer IOCTL handler.
3443 *
3444 * @dev Pointer to the block_device structure.
3445 * @mode ignored
3446 * @cmd IOCTL command passed from the user application.
3447 * @arg Argument passed from the user application.
3448 *
3449 * return value
3450 * 0 IOCTL completed successfully.
3451 * -ENOTTY IOCTL not supported or invalid driver data
3452 * structure pointer.
3453 */
3454static int mtip_block_ioctl(struct block_device *dev,
3455 fmode_t mode,
3456 unsigned cmd,
3457 unsigned long arg)
3458{
3459 struct driver_data *dd = dev->bd_disk->private_data;
3460
3461 if (!capable(CAP_SYS_ADMIN))
3462 return -EACCES;
3463
3464 if (!dd)
3465 return -ENOTTY;
3466
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003467 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003468 return -ENOTTY;
3469
Sam Bradshaw88523a62011-08-30 08:34:26 -06003470 switch (cmd) {
3471 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003472 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003473 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003474 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003475 }
3476}
3477
Jens Axboe16d02c02011-09-27 15:50:01 -06003478#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003479/*
3480 * Block layer compat IOCTL handler.
3481 *
3482 * @dev Pointer to the block_device structure.
3483 * @mode ignored
3484 * @cmd IOCTL command passed from the user application.
3485 * @arg Argument passed from the user application.
3486 *
3487 * return value
3488 * 0 IOCTL completed successfully.
3489 * -ENOTTY IOCTL not supported or invalid driver data
3490 * structure pointer.
3491 */
3492static int mtip_block_compat_ioctl(struct block_device *dev,
3493 fmode_t mode,
3494 unsigned cmd,
3495 unsigned long arg)
3496{
3497 struct driver_data *dd = dev->bd_disk->private_data;
3498
3499 if (!capable(CAP_SYS_ADMIN))
3500 return -EACCES;
3501
3502 if (!dd)
3503 return -ENOTTY;
3504
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003505 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003506 return -ENOTTY;
3507
Sam Bradshaw88523a62011-08-30 08:34:26 -06003508 switch (cmd) {
3509 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003510 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003511 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003512 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003513 ide_task_request_t req_task;
3514 int compat_tasksize, outtotal, ret;
3515
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003516 compat_tasksize =
3517 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003518
3519 compat_req_task =
3520 (struct mtip_compat_ide_task_request_s __user *) arg;
3521
3522 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003523 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003524 return -EFAULT;
3525
3526 if (get_user(req_task.out_size, &compat_req_task->out_size))
3527 return -EFAULT;
3528
3529 if (get_user(req_task.in_size, &compat_req_task->in_size))
3530 return -EFAULT;
3531
3532 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3533
3534 ret = exec_drive_taskfile(dd, (void __user *) arg,
3535 &req_task, outtotal);
3536
3537 if (copy_to_user((void __user *) arg, &req_task,
3538 compat_tasksize -
3539 (2 * sizeof(compat_long_t))))
3540 return -EFAULT;
3541
3542 if (put_user(req_task.out_size, &compat_req_task->out_size))
3543 return -EFAULT;
3544
3545 if (put_user(req_task.in_size, &compat_req_task->in_size))
3546 return -EFAULT;
3547
3548 return ret;
3549 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003550 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003551 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003552 }
3553}
Jens Axboe16d02c02011-09-27 15:50:01 -06003554#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003555
3556/*
3557 * Obtain the geometry of the device.
3558 *
3559 * You may think that this function is obsolete, but some applications,
3560 * fdisk for example still used CHS values. This function describes the
3561 * device as having 224 heads and 56 sectors per cylinder. These values are
3562 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3563 * partition is described in terms of a start and end cylinder this means
3564 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3565 * affects performance.
3566 *
3567 * @dev Pointer to the block_device strucutre.
3568 * @geo Pointer to a hd_geometry structure.
3569 *
3570 * return value
3571 * 0 Operation completed successfully.
3572 * -ENOTTY An error occurred while reading the drive capacity.
3573 */
3574static int mtip_block_getgeo(struct block_device *dev,
3575 struct hd_geometry *geo)
3576{
3577 struct driver_data *dd = dev->bd_disk->private_data;
3578 sector_t capacity;
3579
3580 if (!dd)
3581 return -ENOTTY;
3582
3583 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3584 dev_warn(&dd->pdev->dev,
3585 "Could not get drive capacity.\n");
3586 return -ENOTTY;
3587 }
3588
3589 geo->heads = 224;
3590 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003591 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003592 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003593 return 0;
3594}
3595
3596/*
3597 * Block device operation function.
3598 *
3599 * This structure contains pointers to the functions required by the block
3600 * layer.
3601 */
3602static const struct block_device_operations mtip_block_ops = {
3603 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003604#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003605 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003606#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003607 .getgeo = mtip_block_getgeo,
3608 .owner = THIS_MODULE
3609};
3610
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003611static inline bool is_se_active(struct driver_data *dd)
3612{
3613 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT, &dd->port->flags))) {
3614 if (dd->port->ic_pause_timer) {
3615 unsigned long to = dd->port->ic_pause_timer +
3616 msecs_to_jiffies(1000);
3617 if (time_after(jiffies, to)) {
3618 clear_bit(MTIP_PF_SE_ACTIVE_BIT,
3619 &dd->port->flags);
3620 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
3621 dd->port->ic_pause_timer = 0;
3622 wake_up_interruptible(&dd->port->svc_wait);
3623 return false;
3624 }
3625 }
3626 return true;
3627 }
3628 return false;
3629}
3630
Sam Bradshaw88523a62011-08-30 08:34:26 -06003631/*
3632 * Block layer make request function.
3633 *
3634 * This function is called by the kernel to process a BIO for
3635 * the P320 device.
3636 *
3637 * @queue Pointer to the request queue. Unused other than to obtain
3638 * the driver data structure.
Jens Axboeffc771b2014-05-09 09:42:02 -06003639 * @rq Pointer to the request.
Sam Bradshaw88523a62011-08-30 08:34:26 -06003640 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003641 */
Jens Axboeffc771b2014-05-09 09:42:02 -06003642static int mtip_submit_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003643{
Jens Axboeffc771b2014-05-09 09:42:02 -06003644 struct driver_data *dd = hctx->queue->queuedata;
3645 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3646 unsigned int nents;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003647
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003648 if (is_se_active(dd))
3649 return -ENODATA;
3650
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003651 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3652 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3653 &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003654 return -ENXIO;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003655 }
3656 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003657 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003658 }
3659 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3660 &dd->dd_flag) &&
Jens Axboeffc771b2014-05-09 09:42:02 -06003661 rq_data_dir(rq))) {
3662 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003663 }
Jens Axboeffc771b2014-05-09 09:42:02 -06003664 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)))
3665 return -ENODATA;
3666 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag))
3667 return -ENXIO;
Asai Thambi S P45038362012-04-09 08:35:38 +02003668 }
3669
Jens Axboeffc771b2014-05-09 09:42:02 -06003670 if (rq->cmd_flags & REQ_DISCARD) {
3671 int err;
3672
3673 err = mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq));
Christoph Hellwigc8a446a2014-09-13 16:40:10 -07003674 blk_mq_end_request(rq, err);
Jens Axboeffc771b2014-05-09 09:42:02 -06003675 return 0;
Asai Thambi S P15283462013-01-11 14:41:34 +01003676 }
3677
Jens Axboeffc771b2014-05-09 09:42:02 -06003678 /* Create the scatter list for this request. */
3679 nents = blk_rq_map_sg(hctx->queue, rq, cmd->sg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003680
Jens Axboeffc771b2014-05-09 09:42:02 -06003681 /* Issue the read/write. */
3682 mtip_hw_submit_io(dd, rq, cmd, nents, hctx);
3683 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003684}
3685
Jens Axboeffc771b2014-05-09 09:42:02 -06003686static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx,
3687 struct request *rq)
3688{
3689 struct driver_data *dd = hctx->queue->queuedata;
3690 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3691
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003692 if (rq_data_dir(rq) == READ || !dd->unal_qdepth)
Jens Axboeffc771b2014-05-09 09:42:02 -06003693 return false;
3694
3695 /*
3696 * If unaligned depth must be limited on this controller, mark it
3697 * as unaligned if the IO isn't on a 4k boundary (start of length).
3698 */
3699 if (blk_rq_sectors(rq) <= 64) {
3700 if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7))
3701 cmd->unaligned = 1;
3702 }
3703
3704 if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal))
3705 return true;
3706
3707 return false;
3708}
3709
Jens Axboe74c45052014-10-29 11:14:52 -06003710static int mtip_queue_rq(struct blk_mq_hw_ctx *hctx,
3711 const struct blk_mq_queue_data *bd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003712{
Jens Axboe74c45052014-10-29 11:14:52 -06003713 struct request *rq = bd->rq;
Jens Axboeffc771b2014-05-09 09:42:02 -06003714 int ret;
3715
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003716 if (unlikely(mtip_check_unal_depth(hctx, rq)))
Jens Axboeffc771b2014-05-09 09:42:02 -06003717 return BLK_MQ_RQ_QUEUE_BUSY;
3718
Christoph Hellwige2490072014-09-13 16:40:09 -07003719 blk_mq_start_request(rq);
3720
Jens Axboeffc771b2014-05-09 09:42:02 -06003721 ret = mtip_submit_request(hctx, rq);
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003722 if (likely(!ret))
Jens Axboeffc771b2014-05-09 09:42:02 -06003723 return BLK_MQ_RQ_QUEUE_OK;
3724
3725 rq->errors = ret;
3726 return BLK_MQ_RQ_QUEUE_ERROR;
3727}
3728
3729static void mtip_free_cmd(void *data, struct request *rq,
3730 unsigned int hctx_idx, unsigned int request_idx)
3731{
3732 struct driver_data *dd = data;
3733 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3734
3735 if (!cmd->command)
3736 return;
3737
3738 dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3739 cmd->command, cmd->command_dma);
3740}
3741
3742static int mtip_init_cmd(void *data, struct request *rq, unsigned int hctx_idx,
3743 unsigned int request_idx, unsigned int numa_node)
3744{
3745 struct driver_data *dd = data;
3746 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3747 u32 host_cap_64 = readl(dd->mmio + HOST_CAP) & HOST_CAP_64;
3748
Jeff Moyer74c9c912015-07-29 10:22:50 -04003749 /*
3750 * For flush requests, request_idx starts at the end of the
3751 * tag space. Since we don't support FLUSH/FUA, simply return
3752 * 0 as there's nothing to be done.
3753 */
3754 if (request_idx >= MTIP_MAX_COMMAND_SLOTS)
3755 return 0;
3756
Jens Axboeffc771b2014-05-09 09:42:02 -06003757 cmd->command = dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3758 &cmd->command_dma, GFP_KERNEL);
3759 if (!cmd->command)
3760 return -ENOMEM;
3761
3762 memset(cmd->command, 0, CMD_DMA_ALLOC_SZ);
3763
3764 /* Point the command headers at the command tables. */
3765 cmd->command_header = dd->port->command_list +
3766 (sizeof(struct mtip_cmd_hdr) * request_idx);
3767 cmd->command_header_dma = dd->port->command_list_dma +
3768 (sizeof(struct mtip_cmd_hdr) * request_idx);
3769
3770 if (host_cap_64)
3771 cmd->command_header->ctbau = __force_bit2int cpu_to_le32((cmd->command_dma >> 16) >> 16);
3772
3773 cmd->command_header->ctba = __force_bit2int cpu_to_le32(cmd->command_dma & 0xFFFFFFFF);
3774
3775 sg_init_table(cmd->sg, MTIP_MAX_SG);
3776 return 0;
3777}
3778
3779static struct blk_mq_ops mtip_mq_ops = {
3780 .queue_rq = mtip_queue_rq,
3781 .map_queue = blk_mq_map_queue,
Jens Axboeffc771b2014-05-09 09:42:02 -06003782 .init_request = mtip_init_cmd,
3783 .exit_request = mtip_free_cmd,
3784};
3785
Sam Bradshaw88523a62011-08-30 08:34:26 -06003786/*
3787 * Block layer initialization function.
3788 *
3789 * This function is called once by the PCI layer for each P320
3790 * device that is connected to the system.
3791 *
3792 * @dd Pointer to the driver data structure.
3793 *
3794 * return value
3795 * 0 on success else an error code.
3796 */
Jens Axboe63166682011-09-27 21:27:43 -06003797static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003798{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003799 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003800 sector_t capacity;
3801 unsigned int index = 0;
3802 struct kobject *kobj;
3803
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003804 if (dd->disk)
3805 goto skip_create_disk; /* hw init done, before rebuild */
3806
Jens Axboeffc771b2014-05-09 09:42:02 -06003807 if (mtip_hw_init(dd)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003808 rv = -EINVAL;
3809 goto protocol_init_error;
3810 }
3811
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003812 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003813 if (dd->disk == NULL) {
3814 dev_err(&dd->pdev->dev,
3815 "Unable to allocate gendisk structure\n");
3816 rv = -EINVAL;
3817 goto alloc_disk_error;
3818 }
3819
3820 /* Generate the disk name, implemented same as in sd.c */
3821 do {
3822 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL))
3823 goto ida_get_error;
3824
3825 spin_lock(&rssd_index_lock);
3826 rv = ida_get_new(&rssd_index_ida, &index);
3827 spin_unlock(&rssd_index_lock);
3828 } while (rv == -EAGAIN);
3829
3830 if (rv)
3831 goto ida_get_error;
3832
3833 rv = rssd_disk_name_format("rssd",
3834 index,
3835 dd->disk->disk_name,
3836 DISK_NAME_LEN);
3837 if (rv)
3838 goto disk_index_error;
3839
3840 dd->disk->driverfs_dev = &dd->pdev->dev;
3841 dd->disk->major = dd->major;
Asai Thambi SP75787262015-05-11 15:55:26 -07003842 dd->disk->first_minor = index * MTIP_MAX_MINORS;
3843 dd->disk->minors = MTIP_MAX_MINORS;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003844 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003845 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003846 dd->index = index;
3847
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003848 mtip_hw_debugfs_init(dd);
3849
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003850skip_create_disk:
Jens Axboeffc771b2014-05-09 09:42:02 -06003851 memset(&dd->tags, 0, sizeof(dd->tags));
3852 dd->tags.ops = &mtip_mq_ops;
3853 dd->tags.nr_hw_queues = 1;
3854 dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS;
3855 dd->tags.reserved_tags = 1;
3856 dd->tags.cmd_size = sizeof(struct mtip_cmd);
3857 dd->tags.numa_node = dd->numa_node;
3858 dd->tags.flags = BLK_MQ_F_SHOULD_MERGE;
3859 dd->tags.driver_data = dd;
3860
3861 rv = blk_mq_alloc_tag_set(&dd->tags);
3862 if (rv) {
3863 dev_err(&dd->pdev->dev,
3864 "Unable to allocate request queue\n");
Jens Axboeffc771b2014-05-09 09:42:02 -06003865 goto block_queue_alloc_init_error;
3866 }
3867
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003868 /* Allocate the request queue. */
Jens Axboeffc771b2014-05-09 09:42:02 -06003869 dd->queue = blk_mq_init_queue(&dd->tags);
Dan Carpentera8a642c2014-05-14 15:54:18 +03003870 if (IS_ERR(dd->queue)) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003871 dev_err(&dd->pdev->dev,
3872 "Unable to allocate request queue\n");
3873 rv = -ENOMEM;
3874 goto block_queue_alloc_init_error;
3875 }
3876
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003877 dd->disk->queue = dd->queue;
3878 dd->queue->queuedata = dd;
3879
Jens Axboeffc771b2014-05-09 09:42:02 -06003880 /* Initialize the protocol layer. */
3881 wait_for_rebuild = mtip_hw_get_identify(dd);
3882 if (wait_for_rebuild < 0) {
3883 dev_err(&dd->pdev->dev,
3884 "Protocol layer initialization failed\n");
3885 rv = -EINVAL;
3886 goto init_hw_cmds_error;
3887 }
3888
3889 /*
3890 * if rebuild pending, start the service thread, and delay the block
3891 * queue creation and add_disk()
3892 */
3893 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3894 goto start_service_thread;
3895
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003896 /* Set device limits. */
3897 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
Mike Snitzerb277da02014-10-04 10:55:32 -06003898 clear_bit(QUEUE_FLAG_ADD_RANDOM, &dd->queue->queue_flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003899 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3900 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003901 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3902 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003903 blk_queue_io_min(dd->queue, 4096);
Felipe Franciosi1044b1b2014-03-13 14:34:20 +00003904 blk_queue_bounce_limit(dd->queue, dd->pdev->dma_mask);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003905
Asai Thambi S P4e8670e2012-02-07 07:54:31 +01003906 /*
3907 * write back cache is not supported in the device. FUA depends on
3908 * write back cache support, hence setting flush support to zero.
3909 */
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003910 blk_queue_flush(dd->queue, 0);
3911
Asai Thambi S P15283462013-01-11 14:41:34 +01003912 /* Signal trim support */
3913 if (dd->trim_supp == true) {
3914 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
3915 dd->queue->limits.discard_granularity = 4096;
3916 blk_queue_max_discard_sectors(dd->queue,
3917 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
3918 dd->queue->limits.discard_zeroes_data = 0;
3919 }
3920
Sam Bradshaw88523a62011-08-30 08:34:26 -06003921 /* Set the capacity of the device in 512 byte sectors. */
3922 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3923 dev_warn(&dd->pdev->dev,
3924 "Could not read drive capacity\n");
3925 rv = -EIO;
3926 goto read_capacity_error;
3927 }
3928 set_capacity(dd->disk, capacity);
3929
3930 /* Enable the block device and add it to /dev */
3931 add_disk(dd->disk);
3932
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003933 dd->bdev = bdget_disk(dd->disk, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003934 /*
3935 * Now that the disk is active, initialize any sysfs attributes
3936 * managed by the protocol layer.
3937 */
3938 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3939 if (kobj) {
3940 mtip_hw_sysfs_init(dd, kobj);
3941 kobject_put(kobj);
3942 }
3943
Asai Thambi S P45038362012-04-09 08:35:38 +02003944 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003945 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003946 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02003947 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003948
3949start_service_thread:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003950 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
Rasmus Villemoes8aeea032015-11-20 10:46:49 +01003951 dd, dd->numa_node,
3952 "mtip_svc_thd_%02d", index);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003953
3954 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003955 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003956 dd->mtip_svc_handler = NULL;
3957 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003958 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003959 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003960 wake_up_process(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02003961 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3962 rv = wait_for_rebuild;
3963
Sam Bradshaw88523a62011-08-30 08:34:26 -06003964 return rv;
3965
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003966kthread_run_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003967 bdput(dd->bdev);
3968 dd->bdev = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07003969
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003970 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003971 del_gendisk(dd->disk);
3972
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003973read_capacity_error:
Jens Axboeffc771b2014-05-09 09:42:02 -06003974init_hw_cmds_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003975 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06003976 blk_mq_free_tag_set(&dd->tags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003977block_queue_alloc_init_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003978 mtip_hw_debugfs_exit(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003979disk_index_error:
3980 spin_lock(&rssd_index_lock);
3981 ida_remove(&rssd_index_ida, index);
3982 spin_unlock(&rssd_index_lock);
3983
3984ida_get_error:
3985 put_disk(dd->disk);
3986
3987alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003988 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003989
3990protocol_init_error:
3991 return rv;
3992}
3993
3994/*
3995 * Block layer deinitialization function.
3996 *
3997 * Called by the PCI layer as each P320 device is removed.
3998 *
3999 * @dd Pointer to the driver data structure.
4000 *
4001 * return value
4002 * 0
4003 */
Jens Axboe63166682011-09-27 21:27:43 -06004004static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004005{
4006 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004007
Asai Thambi SP2132a542015-05-11 15:53:18 -07004008 mtip_hw_debugfs_exit(dd);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004009
Asai Thambi SP2132a542015-05-11 15:53:18 -07004010 if (dd->mtip_svc_handler) {
4011 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4012 wake_up_interruptible(&dd->port->svc_wait);
4013 kthread_stop(dd->mtip_svc_handler);
4014 }
4015
4016 /* Clean up the sysfs attributes, if created */
4017 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4018 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4019 if (kobj) {
4020 mtip_hw_sysfs_exit(dd, kobj);
4021 kobject_put(kobj);
Asai Thambi S P45038362012-04-09 08:35:38 +02004022 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004023 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004024
Asai Thambi SP2132a542015-05-11 15:53:18 -07004025 if (!dd->sr)
Jens Axboeffc771b2014-05-09 09:42:02 -06004026 mtip_standby_drive(dd);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004027 else
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004028 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
4029 dd->disk->disk_name);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004030
4031 /*
4032 * Delete our gendisk structure. This also removes the device
4033 * from /dev
4034 */
4035 if (dd->bdev) {
4036 bdput(dd->bdev);
4037 dd->bdev = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004038 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004039 if (dd->disk) {
4040 del_gendisk(dd->disk);
4041 if (dd->disk->queue) {
4042 blk_cleanup_queue(dd->queue);
4043 blk_mq_free_tag_set(&dd->tags);
4044 dd->queue = NULL;
4045 }
4046 put_disk(dd->disk);
4047 }
4048 dd->disk = NULL;
4049
4050 spin_lock(&rssd_index_lock);
4051 ida_remove(&rssd_index_ida, dd->index);
4052 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004053
4054 /* De-initialize the protocol layer. */
4055 mtip_hw_exit(dd);
4056
4057 return 0;
4058}
4059
4060/*
4061 * Function called by the PCI layer when just before the
4062 * machine shuts down.
4063 *
4064 * If a protocol layer shutdown function is present it will be called
4065 * by this function.
4066 *
4067 * @dd Pointer to the driver data structure.
4068 *
4069 * return value
4070 * 0
4071 */
Jens Axboe63166682011-09-27 21:27:43 -06004072static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004073{
Jens Axboeffc771b2014-05-09 09:42:02 -06004074 mtip_hw_shutdown(dd);
4075
Sam Bradshaw88523a62011-08-30 08:34:26 -06004076 /* Delete our gendisk structure, and cleanup the blk queue. */
Asai Thambi S P58c49df32013-01-11 18:47:12 +05304077 if (dd->disk) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304078 dev_info(&dd->pdev->dev,
4079 "Shutting down %s ...\n", dd->disk->disk_name);
Asai Thambi S P58c49df32013-01-11 18:47:12 +05304080
Asai Thambi SP02b48262015-05-11 15:48:00 -07004081 del_gendisk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304082 if (dd->disk->queue) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304083 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004084 blk_mq_free_tag_set(&dd->tags);
Asai Thambi SP02b48262015-05-11 15:48:00 -07004085 }
4086 put_disk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304087 dd->disk = NULL;
4088 dd->queue = NULL;
4089 }
Asai Thambi S P8182b492012-04-09 08:35:38 +02004090
4091 spin_lock(&rssd_index_lock);
4092 ida_remove(&rssd_index_ida, dd->index);
4093 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004094 return 0;
4095}
4096
Jens Axboe63166682011-09-27 21:27:43 -06004097static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004098{
4099 dev_info(&dd->pdev->dev,
4100 "Suspending %s ...\n", dd->disk->disk_name);
4101 mtip_hw_suspend(dd);
4102 return 0;
4103}
4104
Jens Axboe63166682011-09-27 21:27:43 -06004105static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004106{
4107 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4108 dd->disk->disk_name);
4109 mtip_hw_resume(dd);
4110 return 0;
4111}
4112
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004113static void drop_cpu(int cpu)
4114{
4115 cpu_use[cpu]--;
4116}
4117
4118static int get_least_used_cpu_on_node(int node)
4119{
4120 int cpu, least_used_cpu, least_cnt;
4121 const struct cpumask *node_mask;
4122
4123 node_mask = cpumask_of_node(node);
4124 least_used_cpu = cpumask_first(node_mask);
4125 least_cnt = cpu_use[least_used_cpu];
4126 cpu = least_used_cpu;
4127
4128 for_each_cpu(cpu, node_mask) {
4129 if (cpu_use[cpu] < least_cnt) {
4130 least_used_cpu = cpu;
4131 least_cnt = cpu_use[cpu];
4132 }
4133 }
4134 cpu_use[least_used_cpu]++;
4135 return least_used_cpu;
4136}
4137
4138/* Helper for selecting a node in round robin mode */
4139static inline int mtip_get_next_rr_node(void)
4140{
4141 static int next_node = -1;
4142
4143 if (next_node == -1) {
4144 next_node = first_online_node;
4145 return next_node;
4146 }
4147
4148 next_node = next_online_node(next_node);
4149 if (next_node == MAX_NUMNODES)
4150 next_node = first_online_node;
4151 return next_node;
4152}
4153
Fengguang Wu25bac122013-01-12 15:31:40 +08004154static DEFINE_HANDLER(0);
4155static DEFINE_HANDLER(1);
4156static DEFINE_HANDLER(2);
4157static DEFINE_HANDLER(3);
4158static DEFINE_HANDLER(4);
4159static DEFINE_HANDLER(5);
4160static DEFINE_HANDLER(6);
4161static DEFINE_HANDLER(7);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004162
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004163static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
4164{
4165 int pos;
4166 unsigned short pcie_dev_ctrl;
4167
4168 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
4169 if (pos) {
4170 pci_read_config_word(pdev,
4171 pos + PCI_EXP_DEVCTL,
4172 &pcie_dev_ctrl);
4173 if (pcie_dev_ctrl & (1 << 11) ||
4174 pcie_dev_ctrl & (1 << 4)) {
4175 dev_info(&dd->pdev->dev,
4176 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4177 pdev->vendor, pdev->device);
4178 pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
4179 PCI_EXP_DEVCTL_RELAX_EN);
4180 pci_write_config_word(pdev,
4181 pos + PCI_EXP_DEVCTL,
4182 pcie_dev_ctrl);
4183 }
4184 }
4185}
4186
4187static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev)
4188{
4189 /*
4190 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4191 * device with device id 0x5aXX
4192 */
4193 if (pdev->bus && pdev->bus->self) {
4194 if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI &&
4195 ((pdev->bus->self->device & 0xff00) == 0x5a00)) {
4196 mtip_disable_link_opts(dd, pdev->bus->self);
4197 } else {
4198 /* Check further up the topology */
4199 struct pci_dev *parent_dev = pdev->bus->self;
4200 if (parent_dev->bus &&
4201 parent_dev->bus->parent &&
4202 parent_dev->bus->parent->self &&
4203 parent_dev->bus->parent->self->vendor ==
4204 PCI_VENDOR_ID_ATI &&
4205 (parent_dev->bus->parent->self->device &
4206 0xff00) == 0x5a00) {
4207 mtip_disable_link_opts(dd,
4208 parent_dev->bus->parent->self);
4209 }
4210 }
4211 }
4212}
4213
Sam Bradshaw88523a62011-08-30 08:34:26 -06004214/*
4215 * Called for each supported PCI device detected.
4216 *
4217 * This function allocates the private data structure, enables the
4218 * PCI device and then calls the block layer initialization function.
4219 *
4220 * return value
4221 * 0 on success else an error code.
4222 */
4223static int mtip_pci_probe(struct pci_dev *pdev,
4224 const struct pci_device_id *ent)
4225{
4226 int rv = 0;
4227 struct driver_data *dd = NULL;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004228 char cpu_list[256];
4229 const struct cpumask *node_mask;
4230 int cpu, i = 0, j = 0;
4231 int my_node = NUMA_NO_NODE;
Asai Thambi S P0caff002013-04-03 19:56:21 +05304232 unsigned long flags;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004233
4234 /* Allocate memory for this devices private data. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004235 my_node = pcibus_to_node(pdev->bus);
4236 if (my_node != NUMA_NO_NODE) {
4237 if (!node_online(my_node))
4238 my_node = mtip_get_next_rr_node();
4239 } else {
4240 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4241 my_node = mtip_get_next_rr_node();
4242 }
4243 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4244 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
Jens Axboe7f328902014-03-10 14:29:37 -06004245 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004246
4247 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004248 if (dd == NULL) {
4249 dev_err(&pdev->dev,
4250 "Unable to allocate memory for driver data\n");
4251 return -ENOMEM;
4252 }
4253
Sam Bradshaw88523a62011-08-30 08:34:26 -06004254 /* Attach the private data to this PCI device. */
4255 pci_set_drvdata(pdev, dd);
4256
4257 rv = pcim_enable_device(pdev);
4258 if (rv < 0) {
4259 dev_err(&pdev->dev, "Unable to enable device\n");
4260 goto iomap_err;
4261 }
4262
4263 /* Map BAR5 to memory. */
4264 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4265 if (rv < 0) {
4266 dev_err(&pdev->dev, "Unable to map regions\n");
4267 goto iomap_err;
4268 }
4269
4270 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4271 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4272
4273 if (rv) {
4274 rv = pci_set_consistent_dma_mask(pdev,
4275 DMA_BIT_MASK(32));
4276 if (rv) {
4277 dev_warn(&pdev->dev,
4278 "64-bit DMA enable failed\n");
4279 goto setmask_err;
4280 }
4281 }
4282 }
4283
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004284 /* Copy the info we may need later into the private data structure. */
4285 dd->major = mtip_major;
4286 dd->instance = instance;
4287 dd->pdev = pdev;
4288 dd->numa_node = my_node;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004289
Asai Thambi S P0caff002013-04-03 19:56:21 +05304290 INIT_LIST_HEAD(&dd->online_list);
4291 INIT_LIST_HEAD(&dd->remove_list);
4292
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004293 memset(dd->workq_name, 0, 32);
4294 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4295
4296 dd->isr_workq = create_workqueue(dd->workq_name);
4297 if (!dd->isr_workq) {
4298 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
Wei Yongjund137c832013-03-22 08:58:23 -06004299 rv = -ENOMEM;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004300 goto block_initialize_err;
4301 }
4302
4303 memset(cpu_list, 0, sizeof(cpu_list));
4304
4305 node_mask = cpumask_of_node(dd->numa_node);
4306 if (!cpumask_empty(node_mask)) {
4307 for_each_cpu(cpu, node_mask)
4308 {
4309 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4310 j = strlen(cpu_list);
4311 }
4312
4313 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4314 dd->numa_node,
4315 topology_physical_package_id(cpumask_first(node_mask)),
4316 nr_cpus_node(dd->numa_node),
4317 cpu_list);
4318 } else
4319 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4320
4321 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4322 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4323 cpu_to_node(dd->isr_binding), dd->isr_binding);
4324
4325 /* first worker context always runs in ISR */
4326 dd->work[0].cpu_binding = dd->isr_binding;
4327 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4328 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4329 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4330 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4331 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4332 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4333 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4334
4335 /* Log the bindings */
4336 for_each_present_cpu(cpu) {
4337 memset(cpu_list, 0, sizeof(cpu_list));
4338 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4339 if (dd->work[i].cpu_binding == cpu) {
4340 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4341 j = strlen(cpu_list);
4342 }
4343 }
4344 if (j)
4345 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4346 }
4347
4348 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4349 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4350 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4351 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4352 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4353 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4354 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4355 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4356
4357 pci_set_master(pdev);
Wei Yongjund137c832013-03-22 08:58:23 -06004358 rv = pci_enable_msi(pdev);
4359 if (rv) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004360 dev_warn(&pdev->dev,
4361 "Unable to enable MSI interrupt.\n");
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004362 goto msi_initialize_err;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004363 }
4364
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004365 mtip_fix_ero_nosnoop(dd, pdev);
4366
Sam Bradshaw88523a62011-08-30 08:34:26 -06004367 /* Initialize the block layer. */
4368 rv = mtip_block_initialize(dd);
4369 if (rv < 0) {
4370 dev_err(&pdev->dev,
4371 "Unable to initialize block layer\n");
4372 goto block_initialize_err;
4373 }
4374
4375 /*
4376 * Increment the instance count so that each device has a unique
4377 * instance number.
4378 */
4379 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02004380 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004381 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P6b06d352013-04-03 19:54:35 +05304382 else
4383 rv = 0; /* device in rebuild state, return 0 from probe */
Asai Thambi S P0caff002013-04-03 19:56:21 +05304384
4385 /* Add to online list even if in ftl rebuild */
4386 spin_lock_irqsave(&dev_lock, flags);
4387 list_add(&dd->online_list, &online_list);
4388 spin_unlock_irqrestore(&dev_lock, flags);
4389
Sam Bradshaw88523a62011-08-30 08:34:26 -06004390 goto done;
4391
4392block_initialize_err:
4393 pci_disable_msi(pdev);
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004394
4395msi_initialize_err:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004396 if (dd->isr_workq) {
4397 flush_workqueue(dd->isr_workq);
4398 destroy_workqueue(dd->isr_workq);
4399 drop_cpu(dd->work[0].cpu_binding);
4400 drop_cpu(dd->work[1].cpu_binding);
4401 drop_cpu(dd->work[2].cpu_binding);
4402 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004403setmask_err:
4404 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4405
4406iomap_err:
4407 kfree(dd);
4408 pci_set_drvdata(pdev, NULL);
4409 return rv;
4410done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06004411 return rv;
4412}
4413
4414/*
4415 * Called for each probed device when the device is removed or the
4416 * driver is unloaded.
4417 *
4418 * return value
4419 * None
4420 */
4421static void mtip_pci_remove(struct pci_dev *pdev)
4422{
4423 struct driver_data *dd = pci_get_drvdata(pdev);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004424 unsigned long flags, to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004425
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004426 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02004427
Asai Thambi S P0caff002013-04-03 19:56:21 +05304428 spin_lock_irqsave(&dev_lock, flags);
4429 list_del_init(&dd->online_list);
4430 list_add(&dd->remove_list, &removing_list);
4431 spin_unlock_irqrestore(&dev_lock, flags);
4432
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004433 mtip_check_surprise_removal(pdev);
4434 synchronize_irq(dd->pdev->irq);
4435
4436 /* Spin until workers are done */
4437 to = jiffies + msecs_to_jiffies(4000);
4438 do {
4439 msleep(20);
4440 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4441 time_before(jiffies, to));
4442
4443 if (atomic_read(&dd->irq_workers_active) != 0) {
4444 dev_warn(&dd->pdev->dev,
4445 "Completion workers still active!\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004446 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004447
Asai Thambi SP2132a542015-05-11 15:53:18 -07004448 blk_mq_stop_hw_queues(dd->queue);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004449 /* Clean up the block layer. */
4450 mtip_block_remove(dd);
4451
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004452 if (dd->isr_workq) {
4453 flush_workqueue(dd->isr_workq);
4454 destroy_workqueue(dd->isr_workq);
4455 drop_cpu(dd->work[0].cpu_binding);
4456 drop_cpu(dd->work[1].cpu_binding);
4457 drop_cpu(dd->work[2].cpu_binding);
4458 }
4459
Sam Bradshaw88523a62011-08-30 08:34:26 -06004460 pci_disable_msi(pdev);
4461
Asai Thambi S P0caff002013-04-03 19:56:21 +05304462 spin_lock_irqsave(&dev_lock, flags);
4463 list_del_init(&dd->remove_list);
4464 spin_unlock_irqrestore(&dev_lock, flags);
4465
Asai Thambi SP2132a542015-05-11 15:53:18 -07004466 kfree(dd);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004467
Sam Bradshaw88523a62011-08-30 08:34:26 -06004468 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004469 pci_set_drvdata(pdev, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004470}
4471
4472/*
4473 * Called for each probed device when the device is suspended.
4474 *
4475 * return value
4476 * 0 Success
4477 * <0 Error
4478 */
4479static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4480{
4481 int rv = 0;
4482 struct driver_data *dd = pci_get_drvdata(pdev);
4483
4484 if (!dd) {
4485 dev_err(&pdev->dev,
4486 "Driver private datastructure is NULL\n");
4487 return -EFAULT;
4488 }
4489
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004490 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004491
4492 /* Disable ports & interrupts then send standby immediate */
4493 rv = mtip_block_suspend(dd);
4494 if (rv < 0) {
4495 dev_err(&pdev->dev,
4496 "Failed to suspend controller\n");
4497 return rv;
4498 }
4499
4500 /*
4501 * Save the pci config space to pdev structure &
4502 * disable the device
4503 */
4504 pci_save_state(pdev);
4505 pci_disable_device(pdev);
4506
4507 /* Move to Low power state*/
4508 pci_set_power_state(pdev, PCI_D3hot);
4509
4510 return rv;
4511}
4512
4513/*
4514 * Called for each probed device when the device is resumed.
4515 *
4516 * return value
4517 * 0 Success
4518 * <0 Error
4519 */
4520static int mtip_pci_resume(struct pci_dev *pdev)
4521{
4522 int rv = 0;
4523 struct driver_data *dd;
4524
4525 dd = pci_get_drvdata(pdev);
4526 if (!dd) {
4527 dev_err(&pdev->dev,
4528 "Driver private datastructure is NULL\n");
4529 return -EFAULT;
4530 }
4531
4532 /* Move the device to active State */
4533 pci_set_power_state(pdev, PCI_D0);
4534
4535 /* Restore PCI configuration space */
4536 pci_restore_state(pdev);
4537
4538 /* Enable the PCI device*/
4539 rv = pcim_enable_device(pdev);
4540 if (rv < 0) {
4541 dev_err(&pdev->dev,
4542 "Failed to enable card during resume\n");
4543 goto err;
4544 }
4545 pci_set_master(pdev);
4546
4547 /*
4548 * Calls hbaReset, initPort, & startPort function
4549 * then enables interrupts
4550 */
4551 rv = mtip_block_resume(dd);
4552 if (rv < 0)
4553 dev_err(&pdev->dev, "Unable to resume\n");
4554
4555err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004556 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004557
4558 return rv;
4559}
4560
4561/*
4562 * Shutdown routine
4563 *
4564 * return value
4565 * None
4566 */
4567static void mtip_pci_shutdown(struct pci_dev *pdev)
4568{
4569 struct driver_data *dd = pci_get_drvdata(pdev);
4570 if (dd)
4571 mtip_block_shutdown(dd);
4572}
4573
Sam Bradshaw88523a62011-08-30 08:34:26 -06004574/* Table of device ids supported by this driver. */
Benoit Taine9baa3c32014-08-08 15:56:03 +02004575static const struct pci_device_id mtip_pci_tbl[] = {
Asai Thambi S P1a131452012-09-05 22:00:38 +05304576 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4577 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4578 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4579 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4580 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4581 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4582 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
Sam Bradshaw88523a62011-08-30 08:34:26 -06004583 { 0 }
4584};
4585
4586/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004587static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004588 .name = MTIP_DRV_NAME,
4589 .id_table = mtip_pci_tbl,
4590 .probe = mtip_pci_probe,
4591 .remove = mtip_pci_remove,
4592 .suspend = mtip_pci_suspend,
4593 .resume = mtip_pci_resume,
4594 .shutdown = mtip_pci_shutdown,
4595};
4596
4597MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4598
4599/*
4600 * Module initialization function.
4601 *
4602 * Called once when the module is loaded. This function allocates a major
4603 * block device number to the Cyclone devices and registers the PCI layer
4604 * of the driver.
4605 *
4606 * Return value
4607 * 0 on success else error code.
4608 */
4609static int __init mtip_init(void)
4610{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004611 int error;
4612
Asai Thambi S P45422e72012-09-05 22:03:56 +05304613 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004614
Asai Thambi S P0caff002013-04-03 19:56:21 +05304615 spin_lock_init(&dev_lock);
4616
4617 INIT_LIST_HEAD(&online_list);
4618 INIT_LIST_HEAD(&removing_list);
4619
Sam Bradshaw88523a62011-08-30 08:34:26 -06004620 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004621 error = register_blkdev(0, MTIP_DRV_NAME);
4622 if (error <= 0) {
Asai Thambi S P45422e72012-09-05 22:03:56 +05304623 pr_err("Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004624 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004625 return -EBUSY;
4626 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004627 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004628
Asai Thambi S P0caff002013-04-03 19:56:21 +05304629 dfs_parent = debugfs_create_dir("rssd", NULL);
4630 if (IS_ERR_OR_NULL(dfs_parent)) {
4631 pr_warn("Error creating debugfs parent\n");
4632 dfs_parent = NULL;
4633 }
4634 if (dfs_parent) {
4635 dfs_device_status = debugfs_create_file("device_status",
4636 S_IRUGO, dfs_parent, NULL,
4637 &mtip_device_status_fops);
4638 if (IS_ERR_OR_NULL(dfs_device_status)) {
4639 pr_err("Error creating device_status node\n");
4640 dfs_device_status = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004641 }
4642 }
4643
Sam Bradshaw88523a62011-08-30 08:34:26 -06004644 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004645 error = pci_register_driver(&mtip_pci_driver);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004646 if (error) {
4647 debugfs_remove(dfs_parent);
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004648 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004649 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004650
4651 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004652}
4653
4654/*
4655 * Module de-initialization function.
4656 *
4657 * Called once when the module is unloaded. This function deallocates
4658 * the major block device number allocated by mtip_init() and
4659 * unregisters the PCI layer of the driver.
4660 *
4661 * Return value
4662 * none
4663 */
4664static void __exit mtip_exit(void)
4665{
Sam Bradshaw88523a62011-08-30 08:34:26 -06004666 /* Release the allocated major block device number. */
4667 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4668
4669 /* Unregister the PCI driver. */
4670 pci_unregister_driver(&mtip_pci_driver);
Asai Thambi S Paf5ded82014-04-16 20:30:16 -07004671
4672 debugfs_remove_recursive(dfs_parent);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004673}
4674
4675MODULE_AUTHOR("Micron Technology, Inc");
4676MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4677MODULE_LICENSE("GPL");
4678MODULE_VERSION(MTIP_DRV_VERSION);
4679
4680module_init(mtip_init);
4681module_exit(mtip_exit);