blob: 3204623f746ac61f8deb8d6ebca21b3b59116680 [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
Ming Leia4e84aa2017-04-27 07:45:18 -0600172/* we have to use runtime tag to setup command header */
173static void mtip_init_cmd_header(struct request *rq)
174{
175 struct driver_data *dd = rq->q->queuedata;
176 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
177 u32 host_cap_64 = readl(dd->mmio + HOST_CAP) & HOST_CAP_64;
178
179 /* Point the command headers at the command tables. */
180 cmd->command_header = dd->port->command_list +
181 (sizeof(struct mtip_cmd_hdr) * rq->tag);
182 cmd->command_header_dma = dd->port->command_list_dma +
183 (sizeof(struct mtip_cmd_hdr) * rq->tag);
184
185 if (host_cap_64)
186 cmd->command_header->ctbau = __force_bit2int cpu_to_le32((cmd->command_dma >> 16) >> 16);
187
188 cmd->command_header->ctba = __force_bit2int cpu_to_le32(cmd->command_dma & 0xFFFFFFFF);
189}
190
Jens Axboeffc771b2014-05-09 09:42:02 -0600191static struct mtip_cmd *mtip_get_int_command(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600192{
Jens Axboeffc771b2014-05-09 09:42:02 -0600193 struct request *rq;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600194
Asai Thambi SP008e56d2016-02-24 21:21:20 -0800195 if (mtip_check_surprise_removal(dd->pdev))
196 return NULL;
197
Jens Axboe3f5e6a32017-04-28 10:45:08 -0600198 rq = blk_mq_alloc_request(dd->queue, REQ_OP_DRV_IN, BLK_MQ_REQ_RESERVED);
Asai Thambi SP008e56d2016-02-24 21:21:20 -0800199 if (IS_ERR(rq))
200 return NULL;
201
Ming Leia4e84aa2017-04-27 07:45:18 -0600202 /* Internal cmd isn't submitted via .queue_rq */
203 mtip_init_cmd_header(rq);
204
Jens Axboeffc771b2014-05-09 09:42:02 -0600205 return blk_mq_rq_to_pdu(rq);
206}
Sam Bradshaw88523a62011-08-30 08:34:26 -0600207
Jens Axboeffc771b2014-05-09 09:42:02 -0600208static void mtip_put_int_command(struct driver_data *dd, struct mtip_cmd *cmd)
209{
210 blk_put_request(blk_mq_rq_from_pdu(cmd));
Sam Bradshaw88523a62011-08-30 08:34:26 -0600211}
212
213/*
Jens Axboeffc771b2014-05-09 09:42:02 -0600214 * Once we add support for one hctx per mtip group, this will change a bit
Sam Bradshaw88523a62011-08-30 08:34:26 -0600215 */
Jens Axboeffc771b2014-05-09 09:42:02 -0600216static struct request *mtip_rq_from_tag(struct driver_data *dd,
217 unsigned int tag)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600218{
Jens Axboe0e62f512014-06-04 10:23:49 -0600219 struct blk_mq_hw_ctx *hctx = dd->queue->queue_hw_ctx[0];
220
221 return blk_mq_tag_to_rq(hctx->tags, tag);
Jens Axboeffc771b2014-05-09 09:42:02 -0600222}
223
224static struct mtip_cmd *mtip_cmd_from_tag(struct driver_data *dd,
225 unsigned int tag)
226{
227 struct request *rq = mtip_rq_from_tag(dd, tag);
228
229 return blk_mq_rq_to_pdu(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600230}
231
232/*
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600233 * IO completion function.
234 *
235 * This completion function is called by the driver ISR when a
236 * command that was issued by the kernel completes. It first calls the
237 * asynchronous completion function which normally calls back into the block
238 * layer passing the asynchronous callback data, then unmaps the
239 * scatter list associated with the completed command, and finally
240 * clears the allocated bit associated with the completed command.
241 *
242 * @port Pointer to the port data structure.
243 * @tag Tag of the command.
244 * @data Pointer to driver_data.
245 * @status Completion status.
246 *
247 * return value
248 * None
249 */
250static void mtip_async_complete(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600251 int tag, struct mtip_cmd *cmd, int status)
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600252{
Jens Axboeffc771b2014-05-09 09:42:02 -0600253 struct driver_data *dd = port->dd;
254 struct request *rq;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600255
256 if (unlikely(!dd) || unlikely(!port))
257 return;
258
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600259 if (unlikely(status == PORT_IRQ_TF_ERR)) {
260 dev_warn(&port->dd->pdev->dev,
261 "Command tag %d failed due to TFE\n", tag);
262 }
263
Jens Axboeffc771b2014-05-09 09:42:02 -0600264 rq = mtip_rq_from_tag(dd, tag);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600265
Christoph Hellwig4dda4732017-04-20 16:03:07 +0200266 cmd->status = status;
Christoph Hellwig08e00292017-04-20 16:03:09 +0200267 blk_mq_complete_request(rq);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600268}
269
270/*
Jens Axboe63166682011-09-27 21:27:43 -0600271 * Reset the HBA (without sleeping)
272 *
Jens Axboe63166682011-09-27 21:27:43 -0600273 * @dd Pointer to the driver data structure.
274 *
275 * return value
276 * 0 The reset was successful.
277 * -1 The HBA Reset bit did not clear.
278 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530279static int mtip_hba_reset(struct driver_data *dd)
Jens Axboe63166682011-09-27 21:27:43 -0600280{
281 unsigned long timeout;
282
Jens Axboe63166682011-09-27 21:27:43 -0600283 /* Set the reset bit */
284 writel(HOST_RESET, dd->mmio + HOST_CTL);
285
286 /* Flush */
287 readl(dd->mmio + HOST_CTL);
288
Asai Thambi SP2f17d712015-05-11 15:57:16 -0700289 /*
290 * Spin for up to 10 seconds waiting for reset acknowledgement. Spec
291 * is 1 sec but in LUN failure conditions, up to 10 secs are required
292 */
293 timeout = jiffies + msecs_to_jiffies(10000);
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530294 do {
295 mdelay(10);
296 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
297 return -1;
Jens Axboe63166682011-09-27 21:27:43 -0600298
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530299 } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
300 && time_before(jiffies, timeout));
Asai Thambi S P45038362012-04-09 08:35:38 +0200301
Jens Axboe63166682011-09-27 21:27:43 -0600302 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
303 return -1;
304
305 return 0;
306}
307
308/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600309 * Issue a command to the hardware.
310 *
311 * Set the appropriate bit in the s_active and Command Issue hardware
312 * registers, causing hardware command processing to begin.
313 *
314 * @port Pointer to the port structure.
315 * @tag The tag of the command to be issued.
316 *
317 * return value
318 * None
319 */
320static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
321{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800322 int group = tag >> 5;
323
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800324 /* guard SACT and CI registers */
325 spin_lock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600326 writel((1 << MTIP_TAG_BIT(tag)),
327 port->s_active[MTIP_TAG_INDEX(tag)]);
328 writel((1 << MTIP_TAG_BIT(tag)),
329 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800330 spin_unlock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600331}
332
333/*
Jens Axboe63166682011-09-27 21:27:43 -0600334 * Enable/disable the reception of FIS
335 *
336 * @port Pointer to the port data structure
337 * @enable 1 to enable, 0 to disable
338 *
339 * return value
340 * Previous state: 1 enabled, 0 disabled
341 */
342static int mtip_enable_fis(struct mtip_port *port, int enable)
343{
344 u32 tmp;
345
346 /* enable FIS reception */
347 tmp = readl(port->mmio + PORT_CMD);
348 if (enable)
349 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
350 else
351 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
352
353 /* Flush */
354 readl(port->mmio + PORT_CMD);
355
356 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
357}
358
359/*
360 * Enable/disable the DMA engine
361 *
362 * @port Pointer to the port data structure
363 * @enable 1 to enable, 0 to disable
364 *
365 * return value
366 * Previous state: 1 enabled, 0 disabled.
367 */
368static int mtip_enable_engine(struct mtip_port *port, int enable)
369{
370 u32 tmp;
371
372 /* enable FIS reception */
373 tmp = readl(port->mmio + PORT_CMD);
374 if (enable)
375 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
376 else
377 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
378
379 readl(port->mmio + PORT_CMD);
380 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
381}
382
383/*
384 * Enables the port DMA engine and FIS reception.
385 *
386 * return value
387 * None
388 */
389static inline void mtip_start_port(struct mtip_port *port)
390{
391 /* Enable FIS reception */
392 mtip_enable_fis(port, 1);
393
394 /* Enable the DMA engine */
395 mtip_enable_engine(port, 1);
396}
397
398/*
399 * Deinitialize a port by disabling port interrupts, the DMA engine,
400 * and FIS reception.
401 *
402 * @port Pointer to the port structure
403 *
404 * return value
405 * None
406 */
407static inline void mtip_deinit_port(struct mtip_port *port)
408{
409 /* Disable interrupts on this port */
410 writel(0, port->mmio + PORT_IRQ_MASK);
411
412 /* Disable the DMA engine */
413 mtip_enable_engine(port, 0);
414
415 /* Disable FIS reception */
416 mtip_enable_fis(port, 0);
417}
418
419/*
420 * Initialize a port.
421 *
422 * This function deinitializes the port by calling mtip_deinit_port() and
423 * then initializes it by setting the command header and RX FIS addresses,
424 * clearing the SError register and any pending port interrupts before
425 * re-enabling the default set of port interrupts.
426 *
427 * @port Pointer to the port structure.
428 *
429 * return value
430 * None
431 */
432static void mtip_init_port(struct mtip_port *port)
433{
434 int i;
435 mtip_deinit_port(port);
436
437 /* Program the command list base and FIS base addresses */
438 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
439 writel((port->command_list_dma >> 16) >> 16,
440 port->mmio + PORT_LST_ADDR_HI);
441 writel((port->rxfis_dma >> 16) >> 16,
442 port->mmio + PORT_FIS_ADDR_HI);
443 }
444
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100445 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600446 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100447 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600448
449 /* Clear SError */
450 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
451
452 /* reset the completed registers.*/
453 for (i = 0; i < port->dd->slot_groups; i++)
454 writel(0xFFFFFFFF, port->completed[i]);
455
456 /* Clear any pending interrupts for this port */
Asai Thambi S P6bb688c2012-05-29 18:40:45 -0700457 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
Jens Axboe63166682011-09-27 21:27:43 -0600458
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100459 /* Clear any pending interrupts on the HBA. */
460 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
461 port->dd->mmio + HOST_IRQ_STAT);
462
Jens Axboe63166682011-09-27 21:27:43 -0600463 /* Enable port interrupts */
464 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
465}
466
467/*
468 * Restart a port
469 *
470 * @port Pointer to the port data structure.
471 *
472 * return value
473 * None
474 */
475static void mtip_restart_port(struct mtip_port *port)
476{
477 unsigned long timeout;
478
479 /* Disable the DMA engine */
480 mtip_enable_engine(port, 0);
481
482 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
483 timeout = jiffies + msecs_to_jiffies(500);
484 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
485 && time_before(jiffies, timeout))
486 ;
487
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200488 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200489 return;
490
Jens Axboe63166682011-09-27 21:27:43 -0600491 /*
492 * Chip quirk: escalate to hba reset if
493 * PxCMD.CR not clear after 500 ms
494 */
495 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
496 dev_warn(&port->dd->pdev->dev,
497 "PxCMD.CR not clear, escalating reset\n");
498
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530499 if (mtip_hba_reset(port->dd))
Jens Axboe63166682011-09-27 21:27:43 -0600500 dev_err(&port->dd->pdev->dev,
501 "HBA reset escalation failed.\n");
502
503 /* 30 ms delay before com reset to quiesce chip */
504 mdelay(30);
505 }
506
507 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
508
509 /* Set PxSCTL.DET */
510 writel(readl(port->mmio + PORT_SCR_CTL) |
511 1, port->mmio + PORT_SCR_CTL);
512 readl(port->mmio + PORT_SCR_CTL);
513
514 /* Wait 1 ms to quiesce chip function */
515 timeout = jiffies + msecs_to_jiffies(1);
516 while (time_before(jiffies, timeout))
517 ;
518
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200519 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200520 return;
521
Jens Axboe63166682011-09-27 21:27:43 -0600522 /* Clear PxSCTL.DET */
523 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
524 port->mmio + PORT_SCR_CTL);
525 readl(port->mmio + PORT_SCR_CTL);
526
527 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
528 timeout = jiffies + msecs_to_jiffies(500);
529 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
530 && time_before(jiffies, timeout))
531 ;
532
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200533 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200534 return;
535
Jens Axboe63166682011-09-27 21:27:43 -0600536 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
537 dev_warn(&port->dd->pdev->dev,
538 "COM reset failed\n");
539
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100540 mtip_init_port(port);
541 mtip_start_port(port);
Jens Axboe63166682011-09-27 21:27:43 -0600542
Jens Axboe63166682011-09-27 21:27:43 -0600543}
544
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530545static int mtip_device_reset(struct driver_data *dd)
546{
547 int rv = 0;
548
549 if (mtip_check_surprise_removal(dd->pdev))
550 return 0;
551
552 if (mtip_hba_reset(dd) < 0)
553 rv = -EFAULT;
554
555 mdelay(1);
556 mtip_init_port(dd->port);
557 mtip_start_port(dd->port);
558
559 /* Enable interrupts on the HBA. */
560 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
561 dd->mmio + HOST_CTL);
562 return rv;
563}
564
Jens Axboe63166682011-09-27 21:27:43 -0600565/*
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200566 * Helper function for tag logging
567 */
568static void print_tags(struct driver_data *dd,
569 char *msg,
570 unsigned long *tagbits,
571 int cnt)
572{
573 unsigned char tagmap[128];
574 int group, tagmap_len = 0;
575
576 memset(tagmap, 0, sizeof(tagmap));
577 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
Jens Axboeffc771b2014-05-09 09:42:02 -0600578 tagmap_len += sprintf(tagmap + tagmap_len, "%016lX ",
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200579 tagbits[group-1]);
580 dev_warn(&dd->pdev->dev,
581 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
582}
583
584/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600585 * Internal command completion callback function.
586 *
587 * This function is normally called by the driver ISR when an internal
588 * command completed. This function signals the command completion by
589 * calling complete().
590 *
591 * @port Pointer to the port data structure.
592 * @tag Tag of the command that has completed.
593 * @data Pointer to a completion structure.
594 * @status Completion status.
595 *
596 * return value
597 * None
598 */
599static void mtip_completion(struct mtip_port *port,
Jens Axboeffc771b2014-05-09 09:42:02 -0600600 int tag, struct mtip_cmd *command, int status)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600601{
Jens Axboeffc771b2014-05-09 09:42:02 -0600602 struct completion *waiting = command->comp_data;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600603 if (unlikely(status == PORT_IRQ_TF_ERR))
604 dev_warn(&port->dd->pdev->dev,
605 "Internal command %d completed with TFE\n", tag);
606
Asai Thambi SP008e56d2016-02-24 21:21:20 -0800607 command->comp_func = NULL;
608 command->comp_data = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600609 complete(waiting);
610}
611
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200612static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
613 dma_addr_t buffer_dma, unsigned int sectors);
614static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
615 struct smart_attr *attrib);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600616/*
617 * Handle an error.
618 *
619 * @dd Pointer to the DRIVER_DATA structure.
620 *
621 * return value
622 * None
623 */
624static void mtip_handle_tfe(struct driver_data *dd)
625{
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200626 int group, tag, bit, reissue, rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600627 struct mtip_port *port;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200628 struct mtip_cmd *cmd;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600629 u32 completed;
630 struct host_to_dev_fis *fis;
631 unsigned long tagaccum[SLOTBITS_IN_LONGS];
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200632 unsigned int cmd_cnt = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200633 unsigned char *buf;
634 char *fail_reason = NULL;
635 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600636
637 dev_warn(&dd->pdev->dev, "Taskfile error\n");
638
639 port = dd->port;
640
Asai Thambi SPa7806fa2015-05-11 15:49:28 -0700641 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600642 cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700643 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
644
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700645 if (cmd->comp_data && cmd->comp_func) {
646 cmd->comp_func(port, MTIP_TAG_INTERNAL,
Jens Axboeffc771b2014-05-09 09:42:02 -0600647 cmd, PORT_IRQ_TF_ERR);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700648 }
Asai Thambi SPe35b9472016-02-24 21:16:21 -0800649 return;
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700650 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600651
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200652 /* clear the tag accumulator */
653 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
654
Sam Bradshaw88523a62011-08-30 08:34:26 -0600655 /* Loop through all the groups */
656 for (group = 0; group < dd->slot_groups; group++) {
657 completed = readl(port->completed[group]);
658
Jens Axboeffc771b2014-05-09 09:42:02 -0600659 dev_warn(&dd->pdev->dev, "g=%u, comp=%x\n", group, completed);
660
Sam Bradshaw88523a62011-08-30 08:34:26 -0600661 /* clear completed status register in the hardware.*/
662 writel(completed, port->completed[group]);
663
Sam Bradshaw88523a62011-08-30 08:34:26 -0600664 /* Process successfully completed commands */
665 for (bit = 0; bit < 32 && completed; bit++) {
666 if (!(completed & (1<<bit)))
667 continue;
668 tag = (group << 5) + bit;
669
670 /* Skip the internal command slot */
671 if (tag == MTIP_TAG_INTERNAL)
672 continue;
673
Jens Axboeffc771b2014-05-09 09:42:02 -0600674 cmd = mtip_cmd_from_tag(dd, tag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200675 if (likely(cmd->comp_func)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600676 set_bit(tag, tagaccum);
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200677 cmd_cnt++;
Jens Axboeffc771b2014-05-09 09:42:02 -0600678 cmd->comp_func(port, tag, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600679 } else {
680 dev_err(&port->dd->pdev->dev,
681 "Missing completion func for tag %d",
682 tag);
683 if (mtip_check_surprise_removal(dd->pdev)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600684 /* don't proceed further */
685 return;
686 }
687 }
688 }
689 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200690
691 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600692
693 /* Restart the port */
694 mdelay(20);
695 mtip_restart_port(port);
696
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200697 /* Trying to determine the cause of the error */
698 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
699 dd->port->log_buf,
700 dd->port->log_buf_dma, 1);
701 if (rv) {
702 dev_warn(&dd->pdev->dev,
703 "Error in READ LOG EXT (10h) command\n");
704 /* non-critical error, don't fail the load */
705 } else {
706 buf = (unsigned char *)dd->port->log_buf;
707 if (buf[259] & 0x1) {
708 dev_info(&dd->pdev->dev,
709 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200710 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200711 fail_all_ncq_write = 1;
712 fail_reason = "write protect";
713 }
714 if (buf[288] == 0xF7) {
715 dev_info(&dd->pdev->dev,
716 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200717 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200718 fail_all_ncq_cmds = 1;
719 fail_reason = "thermal shutdown";
720 }
721 if (buf[288] == 0xBF) {
Asai Thambi SPaae4a032016-02-24 21:18:20 -0800722 set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200723 dev_info(&dd->pdev->dev,
Sam Bradshaw26d58052013-10-03 10:18:05 -0700724 "Drive indicates rebuild has failed. Secure erase required.\n");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200725 fail_all_ncq_cmds = 1;
726 fail_reason = "rebuild failed";
727 }
728 }
729
Sam Bradshaw88523a62011-08-30 08:34:26 -0600730 /* clear the tag accumulator */
731 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
732
733 /* Loop through all the groups */
734 for (group = 0; group < dd->slot_groups; group++) {
735 for (bit = 0; bit < 32; bit++) {
736 reissue = 1;
737 tag = (group << 5) + bit;
Jens Axboeffc771b2014-05-09 09:42:02 -0600738 cmd = mtip_cmd_from_tag(dd, tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600739
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200740 fis = (struct host_to_dev_fis *)cmd->command;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600741
742 /* Should re-issue? */
743 if (tag == MTIP_TAG_INTERNAL ||
744 fis->command == ATA_CMD_SET_FEATURES)
745 reissue = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200746 else {
747 if (fail_all_ncq_cmds ||
748 (fail_all_ncq_write &&
749 fis->command == ATA_CMD_FPDMA_WRITE)) {
750 dev_warn(&dd->pdev->dev,
751 " Fail: %s w/tag %d [%s].\n",
752 fis->command == ATA_CMD_FPDMA_WRITE ?
753 "write" : "read",
754 tag,
755 fail_reason != NULL ?
756 fail_reason : "unknown");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200757 if (cmd->comp_func) {
758 cmd->comp_func(port, tag,
Jens Axboeffc771b2014-05-09 09:42:02 -0600759 cmd, -ENODATA);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200760 }
761 continue;
762 }
763 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600764
765 /*
766 * First check if this command has
767 * exceeded its retries.
768 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200769 if (reissue && (cmd->retries-- > 0)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600770
771 set_bit(tag, tagaccum);
772
Sam Bradshaw88523a62011-08-30 08:34:26 -0600773 /* Re-issue the command. */
774 mtip_issue_ncq_command(port, tag);
775
776 continue;
777 }
778
779 /* Retire a command that will not be reissued */
780 dev_warn(&port->dd->pdev->dev,
781 "retiring tag %d\n", tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600782
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200783 if (cmd->comp_func)
Jens Axboeffc771b2014-05-09 09:42:02 -0600784 cmd->comp_func(port, tag, cmd, PORT_IRQ_TF_ERR);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600785 else
786 dev_warn(&port->dd->pdev->dev,
787 "Bad completion for tag %d\n",
788 tag);
789 }
790 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200791 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600792}
793
794/*
795 * Handle a set device bits interrupt
796 */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800797static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
798 u32 completed)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600799{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800800 struct driver_data *dd = port->dd;
801 int tag, bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600802 struct mtip_cmd *command;
803
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800804 if (!completed) {
805 WARN_ON_ONCE(!completed);
806 return;
807 }
808 /* clear completed status register in the hardware.*/
809 writel(completed, port->completed[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600810
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800811 /* Process completed commands. */
812 for (bit = 0; (bit < 32) && completed; bit++) {
813 if (completed & 0x01) {
814 tag = (group << 5) | bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600815
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800816 /* skip internal command slot. */
817 if (unlikely(tag == MTIP_TAG_INTERNAL))
818 continue;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600819
Jens Axboeffc771b2014-05-09 09:42:02 -0600820 command = mtip_cmd_from_tag(dd, tag);
821 if (likely(command->comp_func))
822 command->comp_func(port, tag, command, 0);
823 else {
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600824 dev_dbg(&dd->pdev->dev,
825 "Null completion for tag %d",
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800826 tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600827
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800828 if (mtip_check_surprise_removal(
829 dd->pdev)) {
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800830 return;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600831 }
832 }
833 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800834 completed >>= 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600835 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800836
837 /* If last, re-enable interrupts */
838 if (atomic_dec_return(&dd->irq_workers_active) == 0)
839 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600840}
841
842/*
843 * Process legacy pio and d2h interrupts
844 */
845static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
846{
847 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -0600848 struct mtip_cmd *cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600849
Jens Axboe994ff072017-05-02 09:53:04 -0600850 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) && cmd) {
851 int group = MTIP_TAG_INDEX(MTIP_TAG_INTERNAL);
852 int status = readl(port->cmd_issue[group]);
853
854 if (!(status & (1 << MTIP_TAG_BIT(MTIP_TAG_INTERNAL)))) {
855 if (cmd->comp_func)
856 cmd->comp_func(port, MTIP_TAG_INTERNAL, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600857 }
858 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600859}
860
861/*
862 * Demux and handle errors
863 */
864static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
865{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600866
867 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
868 dev_warn(&dd->pdev->dev,
869 "Clearing PxSERR.DIAG.x\n");
870 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
871 }
872
873 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
874 dev_warn(&dd->pdev->dev,
875 "Clearing PxSERR.DIAG.n\n");
876 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
877 }
878
879 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
880 dev_warn(&dd->pdev->dev,
881 "Port stat errors %x unhandled\n",
882 (port_stat & ~PORT_IRQ_HANDLED));
Asai Thambi S P9b204fb2014-05-20 10:48:56 -0700883 if (mtip_check_surprise_removal(dd->pdev))
884 return;
885 }
886 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR))) {
887 set_bit(MTIP_PF_EH_ACTIVE_BIT, &dd->port->flags);
888 wake_up_interruptible(&dd->port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600889 }
890}
891
892static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
893{
894 struct driver_data *dd = (struct driver_data *) data;
895 struct mtip_port *port = dd->port;
896 u32 hba_stat, port_stat;
897 int rv = IRQ_NONE;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800898 int do_irq_enable = 1, i, workers;
899 struct mtip_work *twork;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600900
901 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
902 if (hba_stat) {
903 rv = IRQ_HANDLED;
904
905 /* Acknowledge the interrupt status on the port.*/
906 port_stat = readl(port->mmio + PORT_IRQ_STAT);
Asai Thambi SP2132a542015-05-11 15:53:18 -0700907 if (unlikely(port_stat == 0xFFFFFFFF)) {
908 mtip_check_surprise_removal(dd->pdev);
909 return IRQ_HANDLED;
910 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600911 writel(port_stat, port->mmio + PORT_IRQ_STAT);
912
913 /* Demux port status */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800914 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
915 do_irq_enable = 0;
916 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
917
918 /* Start at 1: group zero is always local? */
919 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
920 i++) {
921 twork = &dd->work[i];
922 twork->completed = readl(port->completed[i]);
923 if (twork->completed)
924 workers++;
925 }
926
927 atomic_set(&dd->irq_workers_active, workers);
928 if (workers) {
929 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
930 twork = &dd->work[i];
931 if (twork->completed)
932 queue_work_on(
933 twork->cpu_binding,
934 dd->isr_workq,
935 &twork->work);
936 }
937
938 if (likely(dd->work[0].completed))
939 mtip_workq_sdbfx(port, 0,
940 dd->work[0].completed);
941
942 } else {
943 /*
944 * Chip quirk: SDB interrupt but nothing
945 * to complete
946 */
947 do_irq_enable = 1;
948 }
949 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600950
951 if (unlikely(port_stat & PORT_IRQ_ERR)) {
952 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600953 /* don't proceed further */
954 return IRQ_HANDLED;
955 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200956 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +0200957 &dd->dd_flag))
958 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600959
960 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
961 }
962
963 if (unlikely(port_stat & PORT_IRQ_LEGACY))
964 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
965 }
966
967 /* acknowledge interrupt */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800968 if (unlikely(do_irq_enable))
969 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600970
971 return rv;
972}
973
974/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600975 * HBA interrupt subroutine.
976 *
977 * @irq IRQ number.
978 * @instance Pointer to the driver data structure.
979 *
980 * return value
981 * IRQ_HANDLED A HBA interrupt was pending and handled.
982 * IRQ_NONE This interrupt was not for the HBA.
983 */
984static irqreturn_t mtip_irq_handler(int irq, void *instance)
985{
986 struct driver_data *dd = instance;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800987
988 return mtip_handle_irq(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600989}
990
991static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
992{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600993 writel(1 << MTIP_TAG_BIT(tag),
994 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
995}
996
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200997static bool mtip_pause_ncq(struct mtip_port *port,
998 struct host_to_dev_fis *fis)
999{
1000 struct host_to_dev_fis *reply;
1001 unsigned long task_file_data;
1002
1003 reply = port->rxfis + RX_FIS_D2H_REG;
1004 task_file_data = readl(port->mmio+PORT_TFDATA);
1005
Asai Thambi S P12a166c2012-09-05 22:01:36 +05301006 if ((task_file_data & 1))
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001007 return false;
1008
1009 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001010 port->ic_pause_timer = jiffies;
1011 return true;
1012 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1013 (fis->features == 0x03)) {
1014 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1015 port->ic_pause_timer = jiffies;
1016 return true;
1017 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1018 ((fis->command == 0xFC) &&
1019 (fis->features == 0x27 || fis->features == 0x72 ||
1020 fis->features == 0x62 || fis->features == 0x26))) {
Asai Thambi SPee04bed2015-05-11 15:50:50 -07001021 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
Asai Thambi SPaae4a032016-02-24 21:18:20 -08001022 clear_bit(MTIP_DDF_REBUILD_FAILED_BIT, &port->dd->dd_flag);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001023 /* Com reset after secure erase or lowlevel format */
1024 mtip_restart_port(port);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001025 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001026 return false;
1027 }
1028
1029 return false;
1030}
1031
Jens Axboebaed5482017-04-28 07:54:06 -06001032static bool mtip_commands_active(struct mtip_port *port)
1033{
1034 unsigned int active;
1035 unsigned int n;
1036
1037 /*
1038 * Ignore s_active bit 0 of array element 0.
1039 * This bit will always be set
1040 */
1041 active = readl(port->s_active[0]) & 0xFFFFFFFE;
1042 for (n = 1; n < port->dd->slot_groups; n++)
1043 active |= readl(port->s_active[n]);
1044
1045 return active != 0;
1046}
1047
Sam Bradshaw88523a62011-08-30 08:34:26 -06001048/*
1049 * Wait for port to quiesce
1050 *
1051 * @port Pointer to port data structure
1052 * @timeout Max duration to wait (ms)
1053 *
1054 * return value
1055 * 0 Success
1056 * -EBUSY Commands still active
1057 */
Jens Axboe8afdd942017-04-27 16:32:41 -06001058static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001059{
1060 unsigned long to;
Jens Axboebaed5482017-04-28 07:54:06 -06001061 bool active = true;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001062
Jens Axboe9acf03c2014-05-14 08:22:56 -06001063 blk_mq_stop_hw_queues(port->dd->queue);
1064
Sam Bradshaw88523a62011-08-30 08:34:26 -06001065 to = jiffies + msecs_to_jiffies(timeout);
1066 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001067 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
Jens Axboe8afdd942017-04-27 16:32:41 -06001068 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001069 msleep(20);
1070 continue; /* svc thd is actively issuing commands */
1071 }
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001072
Jens Axboe8afdd942017-04-27 16:32:41 -06001073 msleep(100);
Asai Thambi SP008e56d2016-02-24 21:21:20 -08001074
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001075 if (mtip_check_surprise_removal(port->dd->pdev))
1076 goto err_fault;
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001077
Jens Axboebaed5482017-04-28 07:54:06 -06001078 active = mtip_commands_active(port);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001079 if (!active)
1080 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001081 } while (time_before(jiffies, to));
1082
Jens Axboe9acf03c2014-05-14 08:22:56 -06001083 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001084 return active ? -EBUSY : 0;
Jens Axboe9acf03c2014-05-14 08:22:56 -06001085err_fault:
1086 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
1087 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001088}
1089
Jens Axboe3f5e6a32017-04-28 10:45:08 -06001090struct mtip_int_cmd {
1091 int fis_len;
1092 dma_addr_t buffer;
1093 int buf_len;
1094 u32 opts;
1095};
1096
Sam Bradshaw88523a62011-08-30 08:34:26 -06001097/*
1098 * Execute an internal command and wait for the completion.
1099 *
1100 * @port Pointer to the port data structure.
1101 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001102 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001103 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001104 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001105 * @opts Command header options, excluding the FIS length
1106 * and the number of PRD entries.
1107 * @timeout Time in ms to wait for the command to complete.
1108 *
1109 * return value
1110 * 0 Command completed successfully.
1111 * -EFAULT The buffer address is not correctly aligned.
1112 * -EBUSY Internal command or other IO in progress.
1113 * -EAGAIN Time out waiting for command to complete.
1114 */
1115static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001116 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001117 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001118 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001119 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001120 u32 opts,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001121 unsigned long timeout)
1122{
Sam Bradshaw88523a62011-08-30 08:34:26 -06001123 DECLARE_COMPLETION_ONSTACK(wait);
Jens Axboeffc771b2014-05-09 09:42:02 -06001124 struct mtip_cmd *int_cmd;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301125 struct driver_data *dd = port->dd;
Jens Axboe3f5e6a32017-04-28 10:45:08 -06001126 struct request *rq;
1127 struct mtip_int_cmd icmd = {
1128 .fis_len = fis_len,
1129 .buffer = buffer,
1130 .buf_len = buf_len,
1131 .opts = opts
1132 };
Jens Axboeffc771b2014-05-09 09:42:02 -06001133 int rv = 0;
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001134 unsigned long start;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001135
1136 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1137 if (buffer & 0x00000007) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301138 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001139 return -EFAULT;
1140 }
1141
Jens Axboeffc771b2014-05-09 09:42:02 -06001142 int_cmd = mtip_get_int_command(dd);
Asai Thambi SP008e56d2016-02-24 21:21:20 -08001143 if (!int_cmd) {
1144 dbg_printk(MTIP_DRV_NAME "Unable to allocate tag for PIO cmd\n");
1145 return -EFAULT;
1146 }
Jens Axboe3f5e6a32017-04-28 10:45:08 -06001147 rq = blk_mq_rq_from_pdu(int_cmd);
1148 rq->end_io_data = &icmd;
Jens Axboeffc771b2014-05-09 09:42:02 -06001149
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001150 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001151
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001152 if (fis->command == ATA_CMD_SEC_ERASE_PREP)
1153 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1154
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301155 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001156
Jens Axboe0f6422a2017-04-27 16:29:26 -06001157 if (fis->command != ATA_CMD_STANDBYNOW1) {
1158 /* wait for io to complete if non atomic */
Jens Axboe8afdd942017-04-27 16:32:41 -06001159 if (mtip_quiesce_io(port, MTIP_QUIESCE_IO_TIMEOUT_MS) < 0) {
Jens Axboe0f6422a2017-04-27 16:29:26 -06001160 dev_warn(&dd->pdev->dev, "Failed to quiesce IO\n");
1161 mtip_put_int_command(dd, int_cmd);
1162 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1163 wake_up_interruptible(&port->svc_wait);
1164 return -EBUSY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001165 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001166 }
1167
Jens Axboe0f6422a2017-04-27 16:29:26 -06001168 /* Set the completion function and data for the command. */
1169 int_cmd->comp_data = &wait;
1170 int_cmd->comp_func = mtip_completion;
1171
Sam Bradshaw88523a62011-08-30 08:34:26 -06001172 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001173 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001174
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001175 start = jiffies;
Jens Axboe3f5e6a32017-04-28 10:45:08 -06001176 rq->timeout = timeout;
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001177
Jens Axboe3f5e6a32017-04-28 10:45:08 -06001178 /* insert request and run queue */
1179 blk_execute_rq_nowait(rq->q, NULL, rq, true, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001180
Jens Axboe3f5e6a32017-04-28 10:45:08 -06001181 wait_for_completion(&wait);
1182 rv = int_cmd->status;
1183
1184 if (rv < 0) {
Jens Axboe0f6422a2017-04-27 16:29:26 -06001185 if (rv == -ERESTARTSYS) { /* interrupted */
1186 dev_err(&dd->pdev->dev,
1187 "Internal command [%02X] was interrupted after %u ms\n",
1188 fis->command,
1189 jiffies_to_msecs(jiffies - start));
1190 rv = -EINTR;
1191 goto exec_ic_exit;
1192 } else if (rv == 0) /* timeout */
1193 dev_err(&dd->pdev->dev,
1194 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1195 fis->command, timeout);
1196 else
1197 dev_err(&dd->pdev->dev,
1198 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1199 fis->command, rv, timeout);
Asai Thambi SPaae4a032016-02-24 21:18:20 -08001200
Jens Axboe0f6422a2017-04-27 16:29:26 -06001201 if (mtip_check_surprise_removal(dd->pdev) ||
1202 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1203 &dd->dd_flag)) {
1204 dev_err(&dd->pdev->dev,
1205 "Internal command [%02X] wait returned due to SR\n",
1206 fis->command);
1207 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301208 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001209 }
Jens Axboe0f6422a2017-04-27 16:29:26 -06001210 mtip_device_reset(dd); /* recover from timeout issue */
1211 rv = -EAGAIN;
1212 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001213 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001214
Jens Axboe994ff072017-05-02 09:53:04 -06001215 if (readl(port->cmd_issue[MTIP_TAG_INDEX(MTIP_TAG_INTERNAL)])
1216 & (1 << MTIP_TAG_BIT(MTIP_TAG_INTERNAL))) {
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001217 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301218 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1219 mtip_device_reset(dd);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001220 rv = -EAGAIN;
1221 }
1222 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001223exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001224 /* Clear the allocated and active bits for the internal command. */
Jens Axboeffc771b2014-05-09 09:42:02 -06001225 mtip_put_int_command(dd, int_cmd);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001226 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001227 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1228 /* NCQ paused */
1229 return rv;
1230 }
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001231 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001232
1233 return rv;
1234}
1235
1236/*
1237 * Byte-swap ATA ID strings.
1238 *
1239 * ATA identify data contains strings in byte-swapped 16-bit words.
1240 * They must be swapped (on all architectures) to be usable as C strings.
1241 * This function swaps bytes in-place.
1242 *
1243 * @buf The buffer location of the string
1244 * @len The number of bytes to swap
1245 *
1246 * return value
1247 * None
1248 */
1249static inline void ata_swap_string(u16 *buf, unsigned int len)
1250{
1251 int i;
1252 for (i = 0; i < (len/2); i++)
1253 be16_to_cpus(&buf[i]);
1254}
1255
Asai Thambi S P670a6412014-04-16 20:27:54 -07001256static void mtip_set_timeout(struct driver_data *dd,
1257 struct host_to_dev_fis *fis,
1258 unsigned int *timeout, u8 erasemode)
1259{
1260 switch (fis->command) {
1261 case ATA_CMD_DOWNLOAD_MICRO:
1262 *timeout = 120000; /* 2 minutes */
1263 break;
1264 case ATA_CMD_SEC_ERASE_UNIT:
1265 case 0xFC:
1266 if (erasemode)
1267 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
1268 else
1269 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
1270 break;
1271 case ATA_CMD_STANDBYNOW1:
1272 *timeout = 120000; /* 2 minutes */
1273 break;
1274 case 0xF7:
1275 case 0xFA:
1276 *timeout = 60000; /* 60 seconds */
1277 break;
1278 case ATA_CMD_SMART:
1279 *timeout = 15000; /* 15 seconds */
1280 break;
1281 default:
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001282 *timeout = MTIP_IOCTL_CMD_TIMEOUT_MS;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001283 break;
1284 }
1285}
1286
Sam Bradshaw88523a62011-08-30 08:34:26 -06001287/*
1288 * Request the device identity information.
1289 *
1290 * If a user space buffer is not specified, i.e. is NULL, the
1291 * identify information is still read from the drive and placed
1292 * into the identify data buffer (@e port->identify) in the
1293 * port data structure.
1294 * When the identify buffer contains valid identify information @e
1295 * port->identify_valid is non-zero.
1296 *
1297 * @port Pointer to the port structure.
1298 * @user_buffer A user space buffer where the identify data should be
1299 * copied.
1300 *
1301 * return value
1302 * 0 Command completed successfully.
1303 * -EFAULT An error occurred while coping data to the user buffer.
1304 * -1 Command failed.
1305 */
1306static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1307{
1308 int rv = 0;
1309 struct host_to_dev_fis fis;
1310
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001311 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001312 return -EFAULT;
1313
Sam Bradshaw88523a62011-08-30 08:34:26 -06001314 /* Build the FIS. */
1315 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1316 fis.type = 0x27;
1317 fis.opts = 1 << 7;
1318 fis.command = ATA_CMD_ID_ATA;
1319
1320 /* Set the identify information as invalid. */
1321 port->identify_valid = 0;
1322
1323 /* Clear the identify information. */
1324 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1325
1326 /* Execute the command. */
1327 if (mtip_exec_internal_command(port,
1328 &fis,
1329 5,
1330 port->identify_dma,
1331 sizeof(u16) * ATA_ID_WORDS,
1332 0,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001333 MTIP_INT_CMD_TIMEOUT_MS)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001334 < 0) {
1335 rv = -1;
1336 goto out;
1337 }
1338
1339 /*
1340 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1341 * perform field-sensitive swapping on the string fields.
1342 * See the kernel use of ata_id_string() for proof of this.
1343 */
1344#ifdef __LITTLE_ENDIAN
1345 ata_swap_string(port->identify + 27, 40); /* model string*/
1346 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1347 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1348#else
1349 {
1350 int i;
1351 for (i = 0; i < ATA_ID_WORDS; i++)
1352 port->identify[i] = le16_to_cpu(port->identify[i]);
1353 }
1354#endif
1355
Sam Bradshaw26d58052013-10-03 10:18:05 -07001356 /* Check security locked state */
1357 if (port->identify[128] & 0x4)
1358 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1359 else
1360 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1361
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301362#ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
Asai Thambi S P15283462013-01-11 14:41:34 +01001363 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1364 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1365 port->dd->trim_supp = true;
1366 else
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301367#endif
Asai Thambi S P15283462013-01-11 14:41:34 +01001368 port->dd->trim_supp = false;
1369
Sam Bradshaw88523a62011-08-30 08:34:26 -06001370 /* Set the identify buffer as valid. */
1371 port->identify_valid = 1;
1372
1373 if (user_buffer) {
1374 if (copy_to_user(
1375 user_buffer,
1376 port->identify,
1377 ATA_ID_WORDS * sizeof(u16))) {
1378 rv = -EFAULT;
1379 goto out;
1380 }
1381 }
1382
1383out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001384 return rv;
1385}
1386
1387/*
1388 * Issue a standby immediate command to the device.
1389 *
1390 * @port Pointer to the port structure.
1391 *
1392 * return value
1393 * 0 Command was executed successfully.
1394 * -1 An error occurred while executing the command.
1395 */
1396static int mtip_standby_immediate(struct mtip_port *port)
1397{
1398 int rv;
1399 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001400 unsigned long start;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001401 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001402
Sam Bradshaw88523a62011-08-30 08:34:26 -06001403 /* Build the FIS. */
1404 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1405 fis.type = 0x27;
1406 fis.opts = 1 << 7;
1407 fis.command = ATA_CMD_STANDBYNOW1;
1408
Asai Thambi S P670a6412014-04-16 20:27:54 -07001409 mtip_set_timeout(port->dd, &fis, &timeout, 0);
1410
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001411 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001412 rv = mtip_exec_internal_command(port,
1413 &fis,
1414 5,
1415 0,
1416 0,
1417 0,
Asai Thambi S P670a6412014-04-16 20:27:54 -07001418 timeout);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001419 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1420 jiffies_to_msecs(jiffies - start));
1421 if (rv)
1422 dev_warn(&port->dd->pdev->dev,
1423 "STANDBY IMMEDIATE command failed.\n");
1424
1425 return rv;
1426}
1427
1428/*
1429 * Issue a READ LOG EXT command to the device.
1430 *
1431 * @port pointer to the port structure.
1432 * @page page number to fetch
1433 * @buffer pointer to buffer
1434 * @buffer_dma dma address corresponding to @buffer
1435 * @sectors page length to fetch, in sectors
1436 *
1437 * return value
1438 * @rv return value from mtip_exec_internal_command()
1439 */
1440static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1441 dma_addr_t buffer_dma, unsigned int sectors)
1442{
1443 struct host_to_dev_fis fis;
1444
1445 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1446 fis.type = 0x27;
1447 fis.opts = 1 << 7;
1448 fis.command = ATA_CMD_READ_LOG_EXT;
1449 fis.sect_count = sectors & 0xFF;
1450 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1451 fis.lba_low = page;
1452 fis.lba_mid = 0;
1453 fis.device = ATA_DEVICE_OBS;
1454
1455 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1456
1457 return mtip_exec_internal_command(port,
1458 &fis,
1459 5,
1460 buffer_dma,
1461 sectors * ATA_SECT_SIZE,
1462 0,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001463 MTIP_INT_CMD_TIMEOUT_MS);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001464}
1465
1466/*
1467 * Issue a SMART READ DATA command to the device.
1468 *
1469 * @port pointer to the port structure.
1470 * @buffer pointer to buffer
1471 * @buffer_dma dma address corresponding to @buffer
1472 *
1473 * return value
1474 * @rv return value from mtip_exec_internal_command()
1475 */
1476static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1477 dma_addr_t buffer_dma)
1478{
1479 struct host_to_dev_fis fis;
1480
1481 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1482 fis.type = 0x27;
1483 fis.opts = 1 << 7;
1484 fis.command = ATA_CMD_SMART;
1485 fis.features = 0xD0;
1486 fis.sect_count = 1;
1487 fis.lba_mid = 0x4F;
1488 fis.lba_hi = 0xC2;
1489 fis.device = ATA_DEVICE_OBS;
1490
1491 return mtip_exec_internal_command(port,
1492 &fis,
1493 5,
1494 buffer_dma,
1495 ATA_SECT_SIZE,
1496 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001497 15000);
1498}
1499
1500/*
1501 * Get the value of a smart attribute
1502 *
1503 * @port pointer to the port structure
1504 * @id attribute number
1505 * @attrib pointer to return attrib information corresponding to @id
1506 *
1507 * return value
1508 * -EINVAL NULL buffer passed or unsupported attribute @id.
1509 * -EPERM Identify data not valid, SMART not supported or not enabled
1510 */
1511static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1512 struct smart_attr *attrib)
1513{
1514 int rv, i;
1515 struct smart_attr *pattr;
1516
1517 if (!attrib)
1518 return -EINVAL;
1519
1520 if (!port->identify_valid) {
1521 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1522 return -EPERM;
1523 }
1524 if (!(port->identify[82] & 0x1)) {
1525 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1526 return -EPERM;
1527 }
1528 if (!(port->identify[85] & 0x1)) {
1529 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1530 return -EPERM;
1531 }
1532
1533 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1534 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1535 if (rv) {
1536 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1537 return rv;
1538 }
1539
1540 pattr = (struct smart_attr *)(port->smart_buf + 2);
1541 for (i = 0; i < 29; i++, pattr++)
1542 if (pattr->attr_id == id) {
1543 memcpy(attrib, pattr, sizeof(struct smart_attr));
1544 break;
1545 }
1546
1547 if (i == 29) {
1548 dev_warn(&port->dd->pdev->dev,
1549 "Query for invalid SMART attribute ID\n");
1550 rv = -EINVAL;
1551 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001552
Sam Bradshaw88523a62011-08-30 08:34:26 -06001553 return rv;
1554}
1555
1556/*
Asai Thambi S P15283462013-01-11 14:41:34 +01001557 * Trim unused sectors
1558 *
1559 * @dd pointer to driver_data structure
1560 * @lba starting lba
1561 * @len # of 512b sectors to trim
1562 *
1563 * return value
1564 * -ENOMEM Out of dma memory
1565 * -EINVAL Invalid parameters passed in, trim not supported
1566 * -EIO Error submitting trim request to hw
1567 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301568static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
1569 unsigned int len)
Asai Thambi S P15283462013-01-11 14:41:34 +01001570{
1571 int i, rv = 0;
1572 u64 tlba, tlen, sect_left;
1573 struct mtip_trim_entry *buf;
1574 dma_addr_t dma_addr;
1575 struct host_to_dev_fis fis;
1576
1577 if (!len || dd->trim_supp == false)
1578 return -EINVAL;
1579
1580 /* Trim request too big */
1581 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1582
1583 /* Trim request not aligned on 4k boundary */
1584 WARN_ON(len % 8 != 0);
1585
1586 /* Warn if vu_trim structure is too big */
1587 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1588
1589 /* Allocate a DMA buffer for the trim structure */
1590 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1591 GFP_KERNEL);
1592 if (!buf)
1593 return -ENOMEM;
1594 memset(buf, 0, ATA_SECT_SIZE);
1595
1596 for (i = 0, sect_left = len, tlba = lba;
1597 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1598 i++) {
1599 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1600 MTIP_MAX_TRIM_ENTRY_LEN :
1601 sect_left);
1602 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1603 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1604 tlba += tlen;
1605 sect_left -= tlen;
1606 }
1607 WARN_ON(sect_left != 0);
1608
1609 /* Build the fis */
1610 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1611 fis.type = 0x27;
1612 fis.opts = 1 << 7;
1613 fis.command = 0xfb;
1614 fis.features = 0x60;
1615 fis.sect_count = 1;
1616 fis.device = ATA_DEVICE_OBS;
1617
1618 if (mtip_exec_internal_command(dd->port,
1619 &fis,
1620 5,
1621 dma_addr,
1622 ATA_SECT_SIZE,
1623 0,
Asai Thambi S P15283462013-01-11 14:41:34 +01001624 MTIP_TRIM_TIMEOUT_MS) < 0)
1625 rv = -EIO;
1626
1627 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1628 return rv;
1629}
1630
1631/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001632 * Get the drive capacity.
1633 *
1634 * @dd Pointer to the device data structure.
1635 * @sectors Pointer to the variable that will receive the sector count.
1636 *
1637 * return value
1638 * 1 Capacity was returned successfully.
1639 * 0 The identify information is invalid.
1640 */
Jens Axboe63166682011-09-27 21:27:43 -06001641static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001642{
1643 struct mtip_port *port = dd->port;
1644 u64 total, raw0, raw1, raw2, raw3;
1645 raw0 = port->identify[100];
1646 raw1 = port->identify[101];
1647 raw2 = port->identify[102];
1648 raw3 = port->identify[103];
1649 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1650 *sectors = total;
1651 return (bool) !!port->identify_valid;
1652}
1653
1654/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001655 * Display the identify command data.
1656 *
1657 * @port Pointer to the port data structure.
1658 *
1659 * return value
1660 * None
1661 */
1662static void mtip_dump_identify(struct mtip_port *port)
1663{
1664 sector_t sectors;
1665 unsigned short revid;
1666 char cbuf[42];
1667
1668 if (!port->identify_valid)
1669 return;
1670
1671 strlcpy(cbuf, (char *)(port->identify+10), 21);
1672 dev_info(&port->dd->pdev->dev,
1673 "Serial No.: %s\n", cbuf);
1674
1675 strlcpy(cbuf, (char *)(port->identify+23), 9);
1676 dev_info(&port->dd->pdev->dev,
1677 "Firmware Ver.: %s\n", cbuf);
1678
1679 strlcpy(cbuf, (char *)(port->identify+27), 41);
1680 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1681
Sam Bradshaw26d58052013-10-03 10:18:05 -07001682 dev_info(&port->dd->pdev->dev, "Security: %04x %s\n",
1683 port->identify[128],
1684 port->identify[128] & 0x4 ? "(LOCKED)" : "");
1685
Sam Bradshaw88523a62011-08-30 08:34:26 -06001686 if (mtip_hw_get_capacity(port->dd, &sectors))
1687 dev_info(&port->dd->pdev->dev,
1688 "Capacity: %llu sectors (%llu MB)\n",
1689 (u64)sectors,
1690 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1691
1692 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001693 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001694 case 0x1:
1695 strlcpy(cbuf, "A0", 3);
1696 break;
1697 case 0x3:
1698 strlcpy(cbuf, "A2", 3);
1699 break;
1700 default:
1701 strlcpy(cbuf, "?", 2);
1702 break;
1703 }
1704 dev_info(&port->dd->pdev->dev,
1705 "Card Type: %s\n", cbuf);
1706}
1707
1708/*
1709 * Map the commands scatter list into the command table.
1710 *
1711 * @command Pointer to the command.
1712 * @nents Number of scatter list entries.
1713 *
1714 * return value
1715 * None
1716 */
1717static inline void fill_command_sg(struct driver_data *dd,
1718 struct mtip_cmd *command,
1719 int nents)
1720{
1721 int n;
1722 unsigned int dma_len;
1723 struct mtip_cmd_sg *command_sg;
1724 struct scatterlist *sg = command->sg;
1725
1726 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1727
1728 for (n = 0; n < nents; n++) {
1729 dma_len = sg_dma_len(sg);
1730 if (dma_len > 0x400000)
1731 dev_err(&dd->pdev->dev,
1732 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001733 command_sg->info = __force_bit2int
1734 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1735 command_sg->dba = __force_bit2int
1736 cpu_to_le32(sg_dma_address(sg));
1737 command_sg->dba_upper = __force_bit2int
1738 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001739 command_sg++;
1740 sg++;
1741 }
1742}
1743
1744/*
1745 * @brief Execute a drive command.
1746 *
1747 * return value 0 The command completed successfully.
1748 * return value -1 An error occurred while executing the command.
1749 */
Jens Axboe63166682011-09-27 21:27:43 -06001750static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001751{
1752 struct host_to_dev_fis fis;
1753 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001754 unsigned int to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001755
Sam Bradshaw88523a62011-08-30 08:34:26 -06001756 /* Build the FIS. */
1757 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1758 fis.type = 0x27;
1759 fis.opts = 1 << 7;
1760 fis.command = command[0];
1761 fis.features = command[1];
1762 fis.sect_count = command[2];
1763 fis.sector = command[3];
1764 fis.cyl_low = command[4];
1765 fis.cyl_hi = command[5];
1766 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1767
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001768 mtip_set_timeout(port->dd, &fis, &to, 0);
1769
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001770 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 -06001771 __func__,
1772 command[0],
1773 command[1],
1774 command[2],
1775 command[3],
1776 command[4],
1777 command[5],
1778 command[6]);
1779
1780 /* Execute the command. */
1781 if (mtip_exec_internal_command(port,
1782 &fis,
1783 5,
1784 0,
1785 0,
1786 0,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001787 to) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001788 return -1;
1789 }
1790
1791 command[0] = reply->command; /* Status*/
1792 command[1] = reply->features; /* Error*/
1793 command[4] = reply->cyl_low;
1794 command[5] = reply->cyl_hi;
1795
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001796 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 -06001797 __func__,
1798 command[0],
1799 command[1],
1800 command[4],
1801 command[5]);
1802
Sam Bradshaw88523a62011-08-30 08:34:26 -06001803 return 0;
1804}
1805
1806/*
1807 * @brief Execute a drive command.
1808 *
1809 * @param port Pointer to the port data structure.
1810 * @param command Pointer to the user specified command parameters.
1811 * @param user_buffer Pointer to the user space buffer where read sector
1812 * data should be copied.
1813 *
1814 * return value 0 The command completed successfully.
1815 * return value -EFAULT An error occurred while copying the completion
1816 * data to the user space buffer.
1817 * return value -1 An error occurred while executing the command.
1818 */
Jens Axboe63166682011-09-27 21:27:43 -06001819static int exec_drive_command(struct mtip_port *port, u8 *command,
1820 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001821{
1822 struct host_to_dev_fis fis;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001823 struct host_to_dev_fis *reply;
1824 u8 *buf = NULL;
1825 dma_addr_t dma_addr = 0;
1826 int rv = 0, xfer_sz = command[3];
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001827 unsigned int to;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001828
1829 if (xfer_sz) {
David Milburn97651ea2012-09-12 14:06:12 -05001830 if (!user_buffer)
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001831 return -EFAULT;
1832
1833 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1834 ATA_SECT_SIZE * xfer_sz,
1835 &dma_addr,
1836 GFP_KERNEL);
1837 if (!buf) {
1838 dev_err(&port->dd->pdev->dev,
1839 "Memory allocation failed (%d bytes)\n",
1840 ATA_SECT_SIZE * xfer_sz);
1841 return -ENOMEM;
1842 }
1843 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1844 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001845
Sam Bradshaw88523a62011-08-30 08:34:26 -06001846 /* Build the FIS. */
1847 memset(&fis, 0, sizeof(struct host_to_dev_fis));
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001848 fis.type = 0x27;
1849 fis.opts = 1 << 7;
1850 fis.command = command[0];
Sam Bradshaw88523a62011-08-30 08:34:26 -06001851 fis.features = command[2];
1852 fis.sect_count = command[3];
1853 if (fis.command == ATA_CMD_SMART) {
1854 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001855 fis.cyl_low = 0x4F;
1856 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001857 }
1858
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001859 mtip_set_timeout(port->dd, &fis, &to, 0);
1860
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001861 if (xfer_sz)
1862 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1863 else
1864 reply = (port->rxfis + RX_FIS_D2H_REG);
1865
Sam Bradshaw88523a62011-08-30 08:34:26 -06001866 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001867 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06001868 "feat %x, sectcnt %x\n",
1869 __func__,
1870 command[0],
1871 command[1],
1872 command[2],
1873 command[3]);
1874
Sam Bradshaw88523a62011-08-30 08:34:26 -06001875 /* Execute the command. */
1876 if (mtip_exec_internal_command(port,
1877 &fis,
1878 5,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001879 (xfer_sz ? dma_addr : 0),
1880 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
Sam Bradshaw88523a62011-08-30 08:34:26 -06001881 0,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001882 to)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001883 < 0) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001884 rv = -EFAULT;
1885 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001886 }
1887
1888 /* Collect the completion status. */
1889 command[0] = reply->command; /* Status*/
1890 command[1] = reply->features; /* Error*/
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001891 command[2] = reply->sect_count;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001892
1893 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001894 " %s: Completion Status: stat %x, "
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001895 "err %x, nsect %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001896 __func__,
1897 command[0],
1898 command[1],
1899 command[2]);
1900
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001901 if (xfer_sz) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001902 if (copy_to_user(user_buffer,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001903 buf,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001904 ATA_SECT_SIZE * command[3])) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001905 rv = -EFAULT;
1906 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001907 }
1908 }
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001909exit_drive_command:
1910 if (buf)
1911 dmam_free_coherent(&port->dd->pdev->dev,
1912 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1913 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001914}
1915
1916/*
1917 * Indicates whether a command has a single sector payload.
1918 *
1919 * @command passed to the device to perform the certain event.
1920 * @features passed to the device to perform the certain event.
1921 *
1922 * return value
1923 * 1 command is one that always has a single sector payload,
1924 * regardless of the value in the Sector Count field.
1925 * 0 otherwise
1926 *
1927 */
1928static unsigned int implicit_sector(unsigned char command,
1929 unsigned char features)
1930{
1931 unsigned int rv = 0;
1932
1933 /* list of commands that have an implicit sector count of 1 */
1934 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001935 case ATA_CMD_SEC_SET_PASS:
1936 case ATA_CMD_SEC_UNLOCK:
1937 case ATA_CMD_SEC_ERASE_PREP:
1938 case ATA_CMD_SEC_ERASE_UNIT:
1939 case ATA_CMD_SEC_FREEZE_LOCK:
1940 case ATA_CMD_SEC_DISABLE_PASS:
1941 case ATA_CMD_PMP_READ:
1942 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001943 rv = 1;
1944 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001945 case ATA_CMD_SET_MAX:
1946 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001947 rv = 1;
1948 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001949 case ATA_CMD_SMART:
1950 if ((features == ATA_SMART_READ_VALUES) ||
1951 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001952 rv = 1;
1953 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001954 case ATA_CMD_CONF_OVERLAY:
1955 if ((features == ATA_DCO_IDENTIFY) ||
1956 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001957 rv = 1;
1958 break;
1959 }
1960 return rv;
1961}
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07001962
Sam Bradshaw88523a62011-08-30 08:34:26 -06001963/*
1964 * Executes a taskfile
1965 * See ide_taskfile_ioctl() for derivation
1966 */
1967static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06001968 void __user *buf,
1969 ide_task_request_t *req_task,
1970 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001971{
1972 struct host_to_dev_fis fis;
1973 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001974 u8 *outbuf = NULL;
1975 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06001976 dma_addr_t outbuf_dma = 0;
1977 dma_addr_t inbuf_dma = 0;
1978 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001979 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001980 unsigned int taskin = 0;
1981 unsigned int taskout = 0;
1982 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07001983 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001984 unsigned int force_single_sector;
1985 unsigned int transfer_size;
1986 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06001987 int intotal = outtotal + req_task->out_size;
Selvan Mani4453bc82012-09-27 14:36:43 +02001988 int erasemode = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001989
1990 taskout = req_task->out_size;
1991 taskin = req_task->in_size;
1992 /* 130560 = 512 * 0xFF*/
Sachin Shuklab425b022016-11-11 14:34:51 +05301993 if (taskin > 130560 || taskout > 130560)
1994 return -EINVAL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001995
1996 if (taskout) {
Al Viro8ed60102016-01-02 14:56:33 -05001997 outbuf = memdup_user(buf + outtotal, taskout);
Sachin Shuklab425b022016-11-11 14:34:51 +05301998 if (IS_ERR(outbuf))
1999 return PTR_ERR(outbuf);
2000
Sam Bradshaw88523a62011-08-30 08:34:26 -06002001 outbuf_dma = pci_map_single(dd->pdev,
2002 outbuf,
2003 taskout,
2004 DMA_TO_DEVICE);
Alexey Khoroshilov5173cb82016-03-19 01:35:54 +03002005 if (pci_dma_mapping_error(dd->pdev, outbuf_dma)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002006 err = -ENOMEM;
2007 goto abort;
2008 }
2009 dma_buffer = outbuf_dma;
2010 }
2011
2012 if (taskin) {
Al Viro8ed60102016-01-02 14:56:33 -05002013 inbuf = memdup_user(buf + intotal, taskin);
2014 if (IS_ERR(inbuf)) {
2015 err = PTR_ERR(inbuf);
2016 inbuf = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002017 goto abort;
2018 }
2019 inbuf_dma = pci_map_single(dd->pdev,
2020 inbuf,
2021 taskin, DMA_FROM_DEVICE);
Alexey Khoroshilov5173cb82016-03-19 01:35:54 +03002022 if (pci_dma_mapping_error(dd->pdev, inbuf_dma)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002023 err = -ENOMEM;
2024 goto abort;
2025 }
2026 dma_buffer = inbuf_dma;
2027 }
2028
2029 /* only supports PIO and non-data commands from this ioctl. */
2030 switch (req_task->data_phase) {
2031 case TASKFILE_OUT:
2032 nsect = taskout / ATA_SECT_SIZE;
2033 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2034 break;
2035 case TASKFILE_IN:
2036 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2037 break;
2038 case TASKFILE_NO_DATA:
2039 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2040 break;
2041 default:
2042 err = -EINVAL;
2043 goto abort;
2044 }
2045
Sam Bradshaw88523a62011-08-30 08:34:26 -06002046 /* Build the FIS. */
2047 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2048
2049 fis.type = 0x27;
2050 fis.opts = 1 << 7;
2051 fis.command = req_task->io_ports[7];
2052 fis.features = req_task->io_ports[1];
2053 fis.sect_count = req_task->io_ports[2];
2054 fis.lba_low = req_task->io_ports[3];
2055 fis.lba_mid = req_task->io_ports[4];
2056 fis.lba_hi = req_task->io_ports[5];
2057 /* Clear the dev bit*/
2058 fis.device = req_task->io_ports[6] & ~0x10;
2059
2060 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2061 req_task->in_flags.all =
2062 IDE_TASKFILE_STD_IN_FLAGS |
2063 (IDE_HOB_STD_IN_FLAGS << 8);
2064 fis.lba_low_ex = req_task->hob_ports[3];
2065 fis.lba_mid_ex = req_task->hob_ports[4];
2066 fis.lba_hi_ex = req_task->hob_ports[5];
2067 fis.features_ex = req_task->hob_ports[1];
2068 fis.sect_cnt_ex = req_task->hob_ports[2];
2069
2070 } else {
2071 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2072 }
2073
2074 force_single_sector = implicit_sector(fis.command, fis.features);
2075
2076 if ((taskin || taskout) && (!fis.sect_count)) {
2077 if (nsect)
2078 fis.sect_count = nsect;
2079 else {
2080 if (!force_single_sector) {
2081 dev_warn(&dd->pdev->dev,
2082 "data movement but "
2083 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002084 err = -EINVAL;
2085 goto abort;
2086 }
2087 }
2088 }
2089
2090 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002091 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002092 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2093 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002094 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002095 fis.command,
2096 fis.features,
2097 fis.sect_count,
2098 fis.lba_low,
2099 fis.lba_mid,
2100 fis.lba_hi,
2101 fis.device);
2102
Selvan Mani4453bc82012-09-27 14:36:43 +02002103 /* check for erase mode support during secure erase.*/
Selvan Mani32087952012-11-07 06:03:37 -07002104 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2105 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
Selvan Mani4453bc82012-09-27 14:36:43 +02002106 erasemode = 1;
2107 }
2108
2109 mtip_set_timeout(dd, &fis, &timeout, erasemode);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002110
2111 /* Determine the correct transfer size.*/
2112 if (force_single_sector)
2113 transfer_size = ATA_SECT_SIZE;
2114 else
2115 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2116
2117 /* Execute the command.*/
2118 if (mtip_exec_internal_command(dd->port,
2119 &fis,
2120 5,
2121 dma_buffer,
2122 transfer_size,
2123 0,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002124 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002125 err = -EIO;
2126 goto abort;
2127 }
2128
2129 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2130
2131 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2132 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2133 req_task->io_ports[7] = reply->control;
2134 } else {
2135 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2136 req_task->io_ports[7] = reply->command;
2137 }
2138
2139 /* reclaim the DMA buffers.*/
2140 if (inbuf_dma)
2141 pci_unmap_single(dd->pdev, inbuf_dma,
2142 taskin, DMA_FROM_DEVICE);
2143 if (outbuf_dma)
2144 pci_unmap_single(dd->pdev, outbuf_dma,
2145 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002146 inbuf_dma = 0;
2147 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002148
2149 /* return the ATA registers to the caller.*/
2150 req_task->io_ports[1] = reply->features;
2151 req_task->io_ports[2] = reply->sect_count;
2152 req_task->io_ports[3] = reply->lba_low;
2153 req_task->io_ports[4] = reply->lba_mid;
2154 req_task->io_ports[5] = reply->lba_hi;
2155 req_task->io_ports[6] = reply->device;
2156
2157 if (req_task->out_flags.all & 1) {
2158
2159 req_task->hob_ports[3] = reply->lba_low_ex;
2160 req_task->hob_ports[4] = reply->lba_mid_ex;
2161 req_task->hob_ports[5] = reply->lba_hi_ex;
2162 req_task->hob_ports[1] = reply->features_ex;
2163 req_task->hob_ports[2] = reply->sect_cnt_ex;
2164 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002165 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002166 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002167 "err %x, sect_cnt %x, lbalo %x,"
2168 "lbamid %x, lbahi %x, dev %x\n",
2169 __func__,
2170 req_task->io_ports[7],
2171 req_task->io_ports[1],
2172 req_task->io_ports[2],
2173 req_task->io_ports[3],
2174 req_task->io_ports[4],
2175 req_task->io_ports[5],
2176 req_task->io_ports[6]);
2177
Sam Bradshaw88523a62011-08-30 08:34:26 -06002178 if (taskout) {
2179 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2180 err = -EFAULT;
2181 goto abort;
2182 }
2183 }
2184 if (taskin) {
2185 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2186 err = -EFAULT;
2187 goto abort;
2188 }
2189 }
2190abort:
2191 if (inbuf_dma)
2192 pci_unmap_single(dd->pdev, inbuf_dma,
2193 taskin, DMA_FROM_DEVICE);
2194 if (outbuf_dma)
2195 pci_unmap_single(dd->pdev, outbuf_dma,
2196 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002197 kfree(outbuf);
2198 kfree(inbuf);
2199
2200 return err;
2201}
2202
2203/*
2204 * Handle IOCTL calls from the Block Layer.
2205 *
2206 * This function is called by the Block Layer when it receives an IOCTL
2207 * command that it does not understand. If the IOCTL command is not supported
2208 * this function returns -ENOTTY.
2209 *
2210 * @dd Pointer to the driver data structure.
2211 * @cmd IOCTL command passed from the Block Layer.
2212 * @arg IOCTL argument passed from the Block Layer.
2213 *
2214 * return value
2215 * 0 The IOCTL completed successfully.
2216 * -ENOTTY The specified command is not supported.
2217 * -EFAULT An error occurred copying data to a user space buffer.
2218 * -EIO An error occurred while executing the command.
2219 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002220static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2221 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002222{
2223 switch (cmd) {
2224 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002225 {
2226 if (copy_to_user((void __user *)arg, dd->port->identify,
2227 sizeof(u16) * ATA_ID_WORDS))
2228 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002229 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002230 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002231 case HDIO_DRIVE_CMD:
2232 {
2233 u8 drive_command[4];
2234
2235 /* Copy the user command info to our buffer. */
2236 if (copy_from_user(drive_command,
2237 (void __user *) arg,
2238 sizeof(drive_command)))
2239 return -EFAULT;
2240
2241 /* Execute the drive command. */
2242 if (exec_drive_command(dd->port,
2243 drive_command,
2244 (void __user *) (arg+4)))
2245 return -EIO;
2246
2247 /* Copy the status back to the users buffer. */
2248 if (copy_to_user((void __user *) arg,
2249 drive_command,
2250 sizeof(drive_command)))
2251 return -EFAULT;
2252
2253 break;
2254 }
2255 case HDIO_DRIVE_TASK:
2256 {
2257 u8 drive_command[7];
2258
2259 /* Copy the user command info to our buffer. */
2260 if (copy_from_user(drive_command,
2261 (void __user *) arg,
2262 sizeof(drive_command)))
2263 return -EFAULT;
2264
2265 /* Execute the drive command. */
2266 if (exec_drive_task(dd->port, drive_command))
2267 return -EIO;
2268
2269 /* Copy the status back to the users buffer. */
2270 if (copy_to_user((void __user *) arg,
2271 drive_command,
2272 sizeof(drive_command)))
2273 return -EFAULT;
2274
2275 break;
2276 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002277 case HDIO_DRIVE_TASKFILE: {
2278 ide_task_request_t req_task;
2279 int ret, outtotal;
2280
2281 if (copy_from_user(&req_task, (void __user *) arg,
2282 sizeof(req_task)))
2283 return -EFAULT;
2284
2285 outtotal = sizeof(req_task);
2286
2287 ret = exec_drive_taskfile(dd, (void __user *) arg,
2288 &req_task, outtotal);
2289
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002290 if (copy_to_user((void __user *) arg, &req_task,
2291 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002292 return -EFAULT;
2293
2294 return ret;
2295 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002296
2297 default:
2298 return -EINVAL;
2299 }
2300 return 0;
2301}
2302
2303/*
2304 * Submit an IO to the hw
2305 *
2306 * This function is called by the block layer to issue an io
2307 * to the device. Upon completion, the callback function will
2308 * be called with the data parameter passed as the callback data.
2309 *
2310 * @dd Pointer to the driver data structure.
2311 * @start First sector to read.
2312 * @nsect Number of sectors to read.
2313 * @nents Number of entries in scatter list for the read command.
2314 * @tag The tag of this read command.
2315 * @callback Pointer to the function that should be called
2316 * when the read completes.
2317 * @data Callback data passed to the callback function
2318 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002319 * @dir Direction (read or write)
2320 *
2321 * return value
2322 * None
2323 */
Jens Axboeffc771b2014-05-09 09:42:02 -06002324static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq,
2325 struct mtip_cmd *command, int nents,
2326 struct blk_mq_hw_ctx *hctx)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002327{
2328 struct host_to_dev_fis *fis;
2329 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -06002330 int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2331 u64 start = blk_rq_pos(rq);
2332 unsigned int nsect = blk_rq_sectors(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002333
2334 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002335 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002336
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002337 prefetch(&port->flags);
2338
Sam Bradshaw88523a62011-08-30 08:34:26 -06002339 command->scatter_ents = nents;
2340
2341 /*
2342 * The number of retries for this command before it is
2343 * reported as a failure to the upper layers.
2344 */
2345 command->retries = MTIP_MAX_RETRIES;
2346
2347 /* Fill out fis */
2348 fis = command->command;
2349 fis->type = 0x27;
2350 fis->opts = 1 << 7;
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002351 if (dma_dir == DMA_FROM_DEVICE)
Jens Axboeffc771b2014-05-09 09:42:02 -06002352 fis->command = ATA_CMD_FPDMA_READ;
2353 else
2354 fis->command = ATA_CMD_FPDMA_WRITE;
Selvan Manieda45312012-11-07 06:03:53 -07002355 fis->lba_low = start & 0xFF;
2356 fis->lba_mid = (start >> 8) & 0xFF;
2357 fis->lba_hi = (start >> 16) & 0xFF;
2358 fis->lba_low_ex = (start >> 24) & 0xFF;
2359 fis->lba_mid_ex = (start >> 32) & 0xFF;
2360 fis->lba_hi_ex = (start >> 40) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002361 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002362 fis->features = nsect & 0xFF;
2363 fis->features_ex = (nsect >> 8) & 0xFF;
Jens Axboeffc771b2014-05-09 09:42:02 -06002364 fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002365 fis->sect_cnt_ex = 0;
2366 fis->control = 0;
2367 fis->res2 = 0;
2368 fis->res3 = 0;
2369 fill_command_sg(dd, command, nents);
2370
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002371 if (unlikely(command->unaligned))
Asai Thambi S P2077d942013-04-29 21:19:49 +02002372 fis->device |= 1 << 7;
2373
Sam Bradshaw88523a62011-08-30 08:34:26 -06002374 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002375 command->command_header->opts =
2376 __force_bit2int cpu_to_le32(
2377 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002378 command->command_header->byte_count = 0;
2379
2380 /*
2381 * Set the completion function and data for the command
2382 * within this layer.
2383 */
2384 command->comp_data = dd;
2385 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002386 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002387
2388 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002389 * To prevent this command from being issued
2390 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002391 */
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002392 if (unlikely(port->flags & MTIP_PF_PAUSE_IO)) {
Jens Axboeffc771b2014-05-09 09:42:02 -06002393 set_bit(rq->tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002394 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002395 return;
2396 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002397
2398 /* Issue the command to the hardware */
Jens Axboeffc771b2014-05-09 09:42:02 -06002399 mtip_issue_ncq_command(port, rq->tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002400}
2401
2402/*
Asai Thambi S P7412ff12012-06-04 12:43:03 -07002403 * Sysfs status dump.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002404 *
2405 * @dev Pointer to the device structure, passed by the kernrel.
2406 * @attr Pointer to the device_attribute structure passed by the kernel.
2407 * @buf Pointer to the char buffer that will receive the stats info.
2408 *
2409 * return value
2410 * The size, in bytes, of the data copied into buf.
2411 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002412static ssize_t mtip_hw_show_status(struct device *dev,
2413 struct device_attribute *attr,
2414 char *buf)
2415{
2416 struct driver_data *dd = dev_to_disk(dev)->private_data;
2417 int size = 0;
2418
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002419 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002420 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002421 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002422 size += sprintf(buf, "%s", "write_protect\n");
2423 else
2424 size += sprintf(buf, "%s", "online\n");
2425
2426 return size;
2427}
2428
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002429static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002430
Asai Thambi S P0caff002013-04-03 19:56:21 +05302431/* debugsfs entries */
2432
2433static ssize_t show_device_status(struct device_driver *drv, char *buf)
2434{
2435 int size = 0;
2436 struct driver_data *dd, *tmp;
2437 unsigned long flags;
2438 char id_buf[42];
2439 u16 status = 0;
2440
2441 spin_lock_irqsave(&dev_lock, flags);
2442 size += sprintf(&buf[size], "Devices Present:\n");
2443 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002444 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302445 if (dd->port &&
2446 dd->port->identify &&
2447 dd->port->identify_valid) {
2448 strlcpy(id_buf,
2449 (char *) (dd->port->identify + 10), 21);
2450 status = *(dd->port->identify + 141);
2451 } else {
2452 memset(id_buf, 0, 42);
2453 status = 0;
2454 }
2455
2456 if (dd->port &&
2457 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2458 size += sprintf(&buf[size],
2459 " device %s %s (ftl rebuild %d %%)\n",
2460 dev_name(&dd->pdev->dev),
2461 id_buf,
2462 status);
2463 } else {
2464 size += sprintf(&buf[size],
2465 " device %s %s\n",
2466 dev_name(&dd->pdev->dev),
2467 id_buf);
2468 }
2469 }
2470 }
2471
2472 size += sprintf(&buf[size], "Devices Being Removed:\n");
2473 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002474 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302475 if (dd->port &&
2476 dd->port->identify &&
2477 dd->port->identify_valid) {
2478 strlcpy(id_buf,
2479 (char *) (dd->port->identify+10), 21);
2480 status = *(dd->port->identify + 141);
2481 } else {
2482 memset(id_buf, 0, 42);
2483 status = 0;
2484 }
2485
2486 if (dd->port &&
2487 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2488 size += sprintf(&buf[size],
2489 " device %s %s (ftl rebuild %d %%)\n",
2490 dev_name(&dd->pdev->dev),
2491 id_buf,
2492 status);
2493 } else {
2494 size += sprintf(&buf[size],
2495 " device %s %s\n",
2496 dev_name(&dd->pdev->dev),
2497 id_buf);
2498 }
2499 }
2500 }
2501 spin_unlock_irqrestore(&dev_lock, flags);
2502
2503 return size;
2504}
2505
2506static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2507 size_t len, loff_t *offset)
2508{
David Milburnc8afd0d2013-05-23 16:23:45 -05002509 struct driver_data *dd = (struct driver_data *)f->private_data;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302510 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002511 char *buf;
2512 int rv = 0;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302513
2514 if (!len || *offset)
2515 return 0;
2516
David Milburnc8afd0d2013-05-23 16:23:45 -05002517 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2518 if (!buf) {
2519 dev_err(&dd->pdev->dev,
2520 "Memory allocation: status buffer\n");
2521 return -ENOMEM;
2522 }
2523
Asai Thambi S P0caff002013-04-03 19:56:21 +05302524 size += show_device_status(NULL, buf);
2525
2526 *offset = size <= len ? size : len;
2527 size = copy_to_user(ubuf, buf, *offset);
2528 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002529 rv = -EFAULT;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302530
David Milburnc8afd0d2013-05-23 16:23:45 -05002531 kfree(buf);
2532 return rv ? rv : *offset;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302533}
2534
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002535static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2536 size_t len, loff_t *offset)
2537{
2538 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002539 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002540 u32 group_allocated;
2541 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002542 int n, rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002543
2544 if (!len || size)
2545 return 0;
2546
David Milburnc8afd0d2013-05-23 16:23:45 -05002547 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2548 if (!buf) {
2549 dev_err(&dd->pdev->dev,
2550 "Memory allocation: register buffer\n");
2551 return -ENOMEM;
2552 }
2553
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002554 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2555
2556 for (n = dd->slot_groups-1; n >= 0; n--)
2557 size += sprintf(&buf[size], "%08X ",
2558 readl(dd->port->s_active[n]));
2559
2560 size += sprintf(&buf[size], "]\n");
2561 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2562
2563 for (n = dd->slot_groups-1; n >= 0; n--)
2564 size += sprintf(&buf[size], "%08X ",
2565 readl(dd->port->cmd_issue[n]));
2566
2567 size += sprintf(&buf[size], "]\n");
2568 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2569
2570 for (n = dd->slot_groups-1; n >= 0; n--)
2571 size += sprintf(&buf[size], "%08X ",
2572 readl(dd->port->completed[n]));
2573
2574 size += sprintf(&buf[size], "]\n");
2575 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2576 readl(dd->port->mmio + PORT_IRQ_STAT));
2577 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2578 readl(dd->mmio + HOST_IRQ_STAT));
2579 size += sprintf(&buf[size], "\n");
2580
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002581 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2582
2583 for (n = dd->slot_groups-1; n >= 0; n--) {
2584 if (sizeof(long) > sizeof(u32))
2585 group_allocated =
2586 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2587 else
2588 group_allocated = dd->port->cmds_to_issue[n];
2589 size += sprintf(&buf[size], "%08X ", group_allocated);
2590 }
2591 size += sprintf(&buf[size], "]\n");
2592
2593 *offset = size <= len ? size : len;
2594 size = copy_to_user(ubuf, buf, *offset);
2595 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002596 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002597
David Milburnc8afd0d2013-05-23 16:23:45 -05002598 kfree(buf);
2599 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002600}
2601
2602static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2603 size_t len, loff_t *offset)
2604{
2605 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002606 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002607 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002608 int rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002609
2610 if (!len || size)
2611 return 0;
2612
David Milburnc8afd0d2013-05-23 16:23:45 -05002613 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2614 if (!buf) {
2615 dev_err(&dd->pdev->dev,
2616 "Memory allocation: flag buffer\n");
2617 return -ENOMEM;
2618 }
2619
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002620 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2621 dd->port->flags);
2622 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2623 dd->dd_flag);
2624
2625 *offset = size <= len ? size : len;
2626 size = copy_to_user(ubuf, buf, *offset);
2627 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002628 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002629
David Milburnc8afd0d2013-05-23 16:23:45 -05002630 kfree(buf);
2631 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002632}
2633
Asai Thambi S P0caff002013-04-03 19:56:21 +05302634static const struct file_operations mtip_device_status_fops = {
2635 .owner = THIS_MODULE,
2636 .open = simple_open,
2637 .read = mtip_hw_read_device_status,
2638 .llseek = no_llseek,
2639};
2640
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002641static const struct file_operations mtip_regs_fops = {
2642 .owner = THIS_MODULE,
2643 .open = simple_open,
2644 .read = mtip_hw_read_registers,
2645 .llseek = no_llseek,
2646};
2647
2648static const struct file_operations mtip_flags_fops = {
2649 .owner = THIS_MODULE,
2650 .open = simple_open,
2651 .read = mtip_hw_read_flags,
2652 .llseek = no_llseek,
2653};
2654
Sam Bradshaw88523a62011-08-30 08:34:26 -06002655/*
2656 * Create the sysfs related attributes.
2657 *
2658 * @dd Pointer to the driver data structure.
2659 * @kobj Pointer to the kobj for the block device.
2660 *
2661 * return value
2662 * 0 Operation completed successfully.
2663 * -EINVAL Invalid parameter.
2664 */
Jens Axboe63166682011-09-27 21:27:43 -06002665static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002666{
2667 if (!kobj || !dd)
2668 return -EINVAL;
2669
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002670 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2671 dev_warn(&dd->pdev->dev,
2672 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002673 return 0;
2674}
2675
2676/*
2677 * Remove the sysfs related attributes.
2678 *
2679 * @dd Pointer to the driver data structure.
2680 * @kobj Pointer to the kobj for the block device.
2681 *
2682 * return value
2683 * 0 Operation completed successfully.
2684 * -EINVAL Invalid parameter.
2685 */
Jens Axboe63166682011-09-27 21:27:43 -06002686static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002687{
2688 if (!kobj || !dd)
2689 return -EINVAL;
2690
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002691 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002692
2693 return 0;
2694}
2695
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002696static int mtip_hw_debugfs_init(struct driver_data *dd)
2697{
2698 if (!dfs_parent)
2699 return -1;
2700
2701 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2702 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2703 dev_warn(&dd->pdev->dev,
2704 "Error creating node %s under debugfs\n",
2705 dd->disk->disk_name);
2706 dd->dfs_node = NULL;
2707 return -1;
2708 }
2709
2710 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2711 &mtip_flags_fops);
2712 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2713 &mtip_regs_fops);
2714
2715 return 0;
2716}
2717
2718static void mtip_hw_debugfs_exit(struct driver_data *dd)
2719{
Sam Bradshaw974a51a2013-05-15 10:04:34 +02002720 if (dd->dfs_node)
2721 debugfs_remove_recursive(dd->dfs_node);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002722}
2723
Sam Bradshaw88523a62011-08-30 08:34:26 -06002724/*
2725 * Perform any init/resume time hardware setup
2726 *
2727 * @dd Pointer to the driver data structure.
2728 *
2729 * return value
2730 * None
2731 */
2732static inline void hba_setup(struct driver_data *dd)
2733{
2734 u32 hwdata;
2735 hwdata = readl(dd->mmio + HOST_HSORG);
2736
2737 /* interrupt bug workaround: use only 1 IS bit.*/
2738 writel(hwdata |
2739 HSORG_DISABLE_SLOTGRP_INTR |
2740 HSORG_DISABLE_SLOTGRP_PXIS,
2741 dd->mmio + HOST_HSORG);
2742}
2743
Asai Thambi S P2077d942013-04-29 21:19:49 +02002744static int mtip_device_unaligned_constrained(struct driver_data *dd)
2745{
2746 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
2747}
2748
Sam Bradshaw88523a62011-08-30 08:34:26 -06002749/*
2750 * Detect the details of the product, and store anything needed
2751 * into the driver data structure. This includes product type and
2752 * version and number of slot groups.
2753 *
2754 * @dd Pointer to the driver data structure.
2755 *
2756 * return value
2757 * None
2758 */
2759static void mtip_detect_product(struct driver_data *dd)
2760{
2761 u32 hwdata;
2762 unsigned int rev, slotgroups;
2763
2764 /*
2765 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2766 * info register:
2767 * [15:8] hardware/software interface rev#
2768 * [ 3] asic-style interface
2769 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2770 */
2771 hwdata = readl(dd->mmio + HOST_HSORG);
2772
2773 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2774 dd->slot_groups = 1;
2775
2776 if (hwdata & 0x8) {
2777 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2778 rev = (hwdata & HSORG_HWREV) >> 8;
2779 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2780 dev_info(&dd->pdev->dev,
2781 "ASIC-FPGA design, HS rev 0x%x, "
2782 "%i slot groups [%i slots]\n",
2783 rev,
2784 slotgroups,
2785 slotgroups * 32);
2786
2787 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2788 dev_warn(&dd->pdev->dev,
2789 "Warning: driver only supports "
2790 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2791 slotgroups = MTIP_MAX_SLOT_GROUPS;
2792 }
2793 dd->slot_groups = slotgroups;
2794 return;
2795 }
2796
2797 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2798}
2799
2800/*
2801 * Blocking wait for FTL rebuild to complete
2802 *
2803 * @dd Pointer to the DRIVER_DATA structure.
2804 *
2805 * return value
2806 * 0 FTL rebuild completed successfully
2807 * -EFAULT FTL rebuild error/timeout/interruption
2808 */
2809static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2810{
2811 unsigned long timeout, cnt = 0, start;
2812
2813 dev_warn(&dd->pdev->dev,
2814 "FTL rebuild in progress. Polling for completion.\n");
2815
2816 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002817 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2818
2819 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002820 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002821 &dd->dd_flag)))
2822 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002823 if (mtip_check_surprise_removal(dd->pdev))
2824 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002825
Sam Bradshaw88523a62011-08-30 08:34:26 -06002826 if (mtip_get_identify(dd->port, NULL) < 0)
2827 return -EFAULT;
2828
2829 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2830 MTIP_FTL_REBUILD_MAGIC) {
2831 ssleep(1);
2832 /* Print message every 3 minutes */
2833 if (cnt++ >= 180) {
2834 dev_warn(&dd->pdev->dev,
2835 "FTL rebuild in progress (%d secs).\n",
2836 jiffies_to_msecs(jiffies - start) / 1000);
2837 cnt = 0;
2838 }
2839 } else {
2840 dev_warn(&dd->pdev->dev,
2841 "FTL rebuild complete (%d secs).\n",
2842 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002843 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002844 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002845 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002846 } while (time_before(jiffies, timeout));
2847
2848 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002849 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002850 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2851 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002852 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002853}
2854
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002855static void mtip_softirq_done_fn(struct request *rq)
2856{
2857 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
2858 struct driver_data *dd = rq->q->queuedata;
2859
2860 /* Unmap the DMA scatter list entries */
2861 dma_unmap_sg(&dd->pdev->dev, cmd->sg, cmd->scatter_ents,
2862 cmd->direction);
2863
2864 if (unlikely(cmd->unaligned))
2865 up(&dd->port->cmd_slot_unal);
2866
Christoph Hellwig4dda4732017-04-20 16:03:07 +02002867 blk_mq_end_request(rq, cmd->status);
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002868}
2869
2870static void mtip_abort_cmd(struct request *req, void *data,
2871 bool reserved)
2872{
Christoph Hellwig4dda4732017-04-20 16:03:07 +02002873 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(req);
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002874 struct driver_data *dd = data;
2875
2876 dbg_printk(MTIP_DRV_NAME " Aborting request, tag = %d\n", req->tag);
2877
2878 clear_bit(req->tag, dd->port->cmds_to_issue);
Christoph Hellwig4dda4732017-04-20 16:03:07 +02002879 cmd->status = -EIO;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002880 mtip_softirq_done_fn(req);
2881}
2882
2883static void mtip_queue_cmd(struct request *req, void *data,
2884 bool reserved)
2885{
2886 struct driver_data *dd = data;
2887
2888 set_bit(req->tag, dd->port->cmds_to_issue);
2889 blk_abort_request(req);
2890}
2891
Sam Bradshaw88523a62011-08-30 08:34:26 -06002892/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002893 * service thread to issue queued commands
2894 *
2895 * @data Pointer to the driver data structure.
2896 *
2897 * return value
2898 * 0
2899 */
2900
2901static int mtip_service_thread(void *data)
2902{
2903 struct driver_data *dd = (struct driver_data *)data;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002904 unsigned long slot, slot_start, slot_wrap, to;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002905 unsigned int num_cmd_slots = dd->slot_groups * 32;
2906 struct mtip_port *port = dd->port;
2907
2908 while (1) {
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002909 if (kthread_should_stop() ||
2910 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2911 goto st_out;
2912 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2913
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002914 /*
2915 * the condition is to check neither an internal command is
2916 * is in progress nor error handling is active
2917 */
2918 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi SPcfc05bd2016-02-24 21:16:00 -08002919 (port->flags & MTIP_PF_SVC_THD_WORK));
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002920
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002921 if (kthread_should_stop() ||
2922 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2923 goto st_out;
2924
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002925 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002926 &dd->dd_flag)))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002927 goto st_out;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002928
Asai Thambi SPcfc05bd2016-02-24 21:16:00 -08002929 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2930
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002931restart_eh:
2932 /* Demux bits: start with error handling */
2933 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) {
2934 mtip_handle_tfe(dd);
2935 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
2936 }
2937
2938 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags))
2939 goto restart_eh;
2940
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002941 if (test_bit(MTIP_PF_TO_ACTIVE_BIT, &port->flags)) {
2942 to = jiffies + msecs_to_jiffies(5000);
2943
2944 do {
2945 mdelay(100);
2946 } while (atomic_read(&dd->irq_workers_active) != 0 &&
2947 time_before(jiffies, to));
2948
2949 if (atomic_read(&dd->irq_workers_active) != 0)
2950 dev_warn(&dd->pdev->dev,
2951 "Completion workers still active!");
2952
2953 spin_lock(dd->queue->queue_lock);
Keith Busch6d125de2016-03-10 13:58:48 +02002954 blk_mq_tagset_busy_iter(&dd->tags,
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002955 mtip_queue_cmd, dd);
2956 spin_unlock(dd->queue->queue_lock);
2957
2958 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &dd->port->flags);
2959
2960 if (mtip_device_reset(dd))
Keith Busch6d125de2016-03-10 13:58:48 +02002961 blk_mq_tagset_busy_iter(&dd->tags,
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002962 mtip_abort_cmd, dd);
2963
2964 clear_bit(MTIP_PF_TO_ACTIVE_BIT, &dd->port->flags);
2965 }
2966
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002967 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002968 slot = 1;
2969 /* used to restrict the loop to one iteration */
2970 slot_start = num_cmd_slots;
2971 slot_wrap = 0;
2972 while (1) {
2973 slot = find_next_bit(port->cmds_to_issue,
2974 num_cmd_slots, slot);
2975 if (slot_wrap == 1) {
2976 if ((slot_start >= slot) ||
2977 (slot >= num_cmd_slots))
2978 break;
2979 }
2980 if (unlikely(slot_start == num_cmd_slots))
2981 slot_start = slot;
2982
2983 if (unlikely(slot == num_cmd_slots)) {
2984 slot = 1;
2985 slot_wrap = 1;
2986 continue;
2987 }
2988
2989 /* Issue the command to the hardware */
2990 mtip_issue_ncq_command(port, slot);
2991
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002992 clear_bit(slot, port->cmds_to_issue);
2993 }
2994
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002995 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002996 }
2997
2998 if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08002999 if (mtip_ftl_rebuild_poll(dd) == 0)
3000 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003001 }
3002 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003003
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003004st_out:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003005 return 0;
3006}
3007
3008/*
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003009 * DMA region teardown
3010 *
3011 * @dd Pointer to driver_data structure
3012 *
3013 * return value
3014 * None
3015 */
3016static void mtip_dma_free(struct driver_data *dd)
3017{
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003018 struct mtip_port *port = dd->port;
3019
3020 if (port->block1)
3021 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3022 port->block1, port->block1_dma);
3023
3024 if (port->command_list) {
3025 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3026 port->command_list, port->command_list_dma);
3027 }
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003028}
3029
3030/*
3031 * DMA region setup
3032 *
3033 * @dd Pointer to driver_data structure
3034 *
3035 * return value
3036 * -ENOMEM Not enough free DMA region space to initialize driver
3037 */
3038static int mtip_dma_alloc(struct driver_data *dd)
3039{
3040 struct mtip_port *port = dd->port;
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003041
3042 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
3043 port->block1 =
3044 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3045 &port->block1_dma, GFP_KERNEL);
3046 if (!port->block1)
3047 return -ENOMEM;
3048 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
3049
3050 /* Allocate dma memory for command list */
3051 port->command_list =
3052 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3053 &port->command_list_dma, GFP_KERNEL);
3054 if (!port->command_list) {
3055 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3056 port->block1, port->block1_dma);
3057 port->block1 = NULL;
3058 port->block1_dma = 0;
3059 return -ENOMEM;
3060 }
3061 memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
3062
3063 /* Setup all pointers into first DMA region */
3064 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
3065 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
3066 port->identify = port->block1 + AHCI_IDFY_OFFSET;
3067 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
3068 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
3069 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
3070 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
3071 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
3072
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003073 return 0;
3074}
3075
Jens Axboeffc771b2014-05-09 09:42:02 -06003076static int mtip_hw_get_identify(struct driver_data *dd)
3077{
3078 struct smart_attr attr242;
3079 unsigned char *buf;
3080 int rv;
3081
3082 if (mtip_get_identify(dd->port, NULL) < 0)
3083 return -EFAULT;
3084
3085 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3086 MTIP_FTL_REBUILD_MAGIC) {
3087 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3088 return MTIP_FTL_REBUILD_MAGIC;
3089 }
3090 mtip_dump_identify(dd->port);
3091
3092 /* check write protect, over temp and rebuild statuses */
3093 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3094 dd->port->log_buf,
3095 dd->port->log_buf_dma, 1);
3096 if (rv) {
3097 dev_warn(&dd->pdev->dev,
3098 "Error in READ LOG EXT (10h) command\n");
3099 /* non-critical error, don't fail the load */
3100 } else {
3101 buf = (unsigned char *)dd->port->log_buf;
3102 if (buf[259] & 0x1) {
3103 dev_info(&dd->pdev->dev,
3104 "Write protect bit is set.\n");
3105 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3106 }
3107 if (buf[288] == 0xF7) {
3108 dev_info(&dd->pdev->dev,
3109 "Exceeded Tmax, drive in thermal shutdown.\n");
3110 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3111 }
3112 if (buf[288] == 0xBF) {
3113 dev_info(&dd->pdev->dev,
3114 "Drive indicates rebuild has failed.\n");
Asai Thambi SPaae4a032016-02-24 21:18:20 -08003115 set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag);
Jens Axboeffc771b2014-05-09 09:42:02 -06003116 }
3117 }
3118
3119 /* get write protect progess */
3120 memset(&attr242, 0, sizeof(struct smart_attr));
3121 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3122 dev_warn(&dd->pdev->dev,
3123 "Unable to check write protect progress\n");
3124 else
3125 dev_info(&dd->pdev->dev,
3126 "Write protect progress: %u%% (%u blocks)\n",
3127 attr242.cur, le32_to_cpu(attr242.data));
3128
3129 return rv;
3130}
3131
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003132/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003133 * Called once for each card.
3134 *
3135 * @dd Pointer to the driver data structure.
3136 *
3137 * return value
3138 * 0 on success, else an error code.
3139 */
Jens Axboe63166682011-09-27 21:27:43 -06003140static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003141{
3142 int i;
3143 int rv;
3144 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02003145 unsigned long timeout, timetaken;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003146
3147 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3148
3149 mtip_detect_product(dd);
3150 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3151 rv = -EIO;
3152 goto out1;
3153 }
3154 num_command_slots = dd->slot_groups * 32;
3155
3156 hba_setup(dd);
3157
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003158 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3159 dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003160 if (!dd->port) {
3161 dev_err(&dd->pdev->dev,
3162 "Memory allocation: port structure\n");
3163 return -ENOMEM;
3164 }
3165
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003166 /* Continue workqueue setup */
3167 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3168 dd->work[i].port = dd->port;
3169
Asai Thambi S P2077d942013-04-29 21:19:49 +02003170 /* Enable unaligned IO constraints for some devices */
3171 if (mtip_device_unaligned_constrained(dd))
3172 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3173 else
3174 dd->unal_qdepth = 0;
3175
Asai Thambi S P2077d942013-04-29 21:19:49 +02003176 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003177
3178 /* Spinlock to prevent concurrent issue */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003179 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3180 spin_lock_init(&dd->port->cmd_issue_lock[i]);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003181
3182 /* Set the port mmio base address. */
3183 dd->port->mmio = dd->mmio + PORT_OFFSET;
3184 dd->port->dd = dd;
3185
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003186 /* DMA allocations */
3187 rv = mtip_dma_alloc(dd);
3188 if (rv < 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003189 goto out1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003190
3191 /* Setup the pointers to the extended s_active and CI registers. */
3192 for (i = 0; i < dd->slot_groups; i++) {
3193 dd->port->s_active[i] =
3194 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3195 dd->port->cmd_issue[i] =
3196 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3197 dd->port->completed[i] =
3198 dd->port->mmio + i*0x80 + PORT_SDBV;
3199 }
3200
Asai Thambi S P45038362012-04-09 08:35:38 +02003201 timetaken = jiffies;
3202 timeout = jiffies + msecs_to_jiffies(30000);
3203 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3204 time_before(jiffies, timeout)) {
3205 mdelay(100);
3206 }
3207 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3208 timetaken = jiffies - timetaken;
3209 dev_warn(&dd->pdev->dev,
3210 "Surprise removal detected at %u ms\n",
3211 jiffies_to_msecs(timetaken));
3212 rv = -ENODEV;
3213 goto out2 ;
3214 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003215 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003216 timetaken = jiffies - timetaken;
3217 dev_warn(&dd->pdev->dev,
3218 "Removal detected at %u ms\n",
3219 jiffies_to_msecs(timetaken));
3220 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003221 goto out2;
3222 }
3223
Asai Thambi S P45038362012-04-09 08:35:38 +02003224 /* Conditionally reset the HBA. */
3225 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3226 if (mtip_hba_reset(dd) < 0) {
3227 dev_err(&dd->pdev->dev,
3228 "Card did not reset within timeout\n");
3229 rv = -EIO;
3230 goto out2;
3231 }
3232 } else {
3233 /* Clear any pending interrupts on the HBA */
3234 writel(readl(dd->mmio + HOST_IRQ_STAT),
3235 dd->mmio + HOST_IRQ_STAT);
3236 }
3237
Sam Bradshaw88523a62011-08-30 08:34:26 -06003238 mtip_init_port(dd->port);
3239 mtip_start_port(dd->port);
3240
3241 /* Setup the ISR and enable interrupts. */
3242 rv = devm_request_irq(&dd->pdev->dev,
3243 dd->pdev->irq,
3244 mtip_irq_handler,
3245 IRQF_SHARED,
3246 dev_driver_string(&dd->pdev->dev),
3247 dd);
3248
3249 if (rv) {
3250 dev_err(&dd->pdev->dev,
3251 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3252 goto out2;
3253 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003254 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003255
3256 /* Enable interrupts on the HBA. */
3257 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3258 dd->mmio + HOST_CTL);
3259
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003260 init_waitqueue_head(&dd->port->svc_wait);
3261
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003262 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003263 rv = -EFAULT;
3264 goto out3;
3265 }
3266
Sam Bradshaw88523a62011-08-30 08:34:26 -06003267 return rv;
3268
3269out3:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003270 /* Disable interrupts on the HBA. */
3271 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3272 dd->mmio + HOST_CTL);
3273
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003274 /* Release the IRQ. */
3275 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003276 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3277
3278out2:
3279 mtip_deinit_port(dd->port);
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003280 mtip_dma_free(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003281
Sam Bradshaw88523a62011-08-30 08:34:26 -06003282out1:
3283 /* Free the memory allocated for the for structure. */
3284 kfree(dd->port);
3285
3286 return rv;
3287}
3288
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003289static int mtip_standby_drive(struct driver_data *dd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003290{
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003291 int rv = 0;
Jens Axboeffc771b2014-05-09 09:42:02 -06003292
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003293 if (dd->sr || !dd->port)
3294 return -ENODEV;
Jens Axboeffc771b2014-05-09 09:42:02 -06003295 /*
3296 * Send standby immediate (E0h) to the drive so that it
3297 * saves its state.
3298 */
3299 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003300 !test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag) &&
3301 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)) {
3302 rv = mtip_standby_immediate(dd->port);
3303 if (rv)
Jens Axboeffc771b2014-05-09 09:42:02 -06003304 dev_warn(&dd->pdev->dev,
3305 "STANDBY IMMEDIATE failed\n");
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003306 }
3307 return rv;
Jens Axboeffc771b2014-05-09 09:42:02 -06003308}
3309
Sam Bradshaw88523a62011-08-30 08:34:26 -06003310/*
3311 * Called to deinitialize an interface.
3312 *
3313 * @dd Pointer to the driver data structure.
3314 *
3315 * return value
3316 * 0
3317 */
Jens Axboe63166682011-09-27 21:27:43 -06003318static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003319{
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003320 if (!dd->sr) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003321 /* de-initialize the port. */
3322 mtip_deinit_port(dd->port);
3323
3324 /* Disable interrupts on the HBA. */
3325 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3326 dd->mmio + HOST_CTL);
3327 }
3328
Sam Bradshaw88523a62011-08-30 08:34:26 -06003329 /* Release the IRQ. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003330 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003331 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
Asai Thambi SP2132a542015-05-11 15:53:18 -07003332 msleep(1000);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003333
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003334 /* Free dma regions */
3335 mtip_dma_free(dd);
3336
Sam Bradshaw88523a62011-08-30 08:34:26 -06003337 /* Free the memory allocated for the for structure. */
3338 kfree(dd->port);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003339 dd->port = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003340
3341 return 0;
3342}
3343
3344/*
3345 * Issue a Standby Immediate command to the device.
3346 *
3347 * This function is called by the Block Layer just before the
3348 * system powers off during a shutdown.
3349 *
3350 * @dd Pointer to the driver data structure.
3351 *
3352 * return value
3353 * 0
3354 */
Jens Axboe63166682011-09-27 21:27:43 -06003355static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003356{
3357 /*
3358 * Send standby immediate (E0h) to the drive so that it
3359 * saves its state.
3360 */
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003361 mtip_standby_drive(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003362
3363 return 0;
3364}
3365
3366/*
3367 * Suspend function
3368 *
3369 * This function is called by the Block Layer just before the
3370 * system hibernates.
3371 *
3372 * @dd Pointer to the driver data structure.
3373 *
3374 * return value
3375 * 0 Suspend was successful
3376 * -EFAULT Suspend was not successful
3377 */
Jens Axboe63166682011-09-27 21:27:43 -06003378static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003379{
3380 /*
3381 * Send standby immediate (E0h) to the drive
3382 * so that it saves its state.
3383 */
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003384 if (mtip_standby_drive(dd) != 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003385 dev_err(&dd->pdev->dev,
3386 "Failed standby-immediate command\n");
3387 return -EFAULT;
3388 }
3389
3390 /* Disable interrupts on the HBA.*/
3391 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3392 dd->mmio + HOST_CTL);
3393 mtip_deinit_port(dd->port);
3394
3395 return 0;
3396}
3397
3398/*
3399 * Resume function
3400 *
3401 * This function is called by the Block Layer as the
3402 * system resumes.
3403 *
3404 * @dd Pointer to the driver data structure.
3405 *
3406 * return value
3407 * 0 Resume was successful
3408 * -EFAULT Resume was not successful
3409 */
Jens Axboe63166682011-09-27 21:27:43 -06003410static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003411{
3412 /* Perform any needed hardware setup steps */
3413 hba_setup(dd);
3414
3415 /* Reset the HBA */
3416 if (mtip_hba_reset(dd) != 0) {
3417 dev_err(&dd->pdev->dev,
3418 "Unable to reset the HBA\n");
3419 return -EFAULT;
3420 }
3421
3422 /*
3423 * Enable the port, DMA engine, and FIS reception specific
3424 * h/w in controller.
3425 */
3426 mtip_init_port(dd->port);
3427 mtip_start_port(dd->port);
3428
3429 /* Enable interrupts on the HBA.*/
3430 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3431 dd->mmio + HOST_CTL);
3432
3433 return 0;
3434}
3435
3436/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003437 * Helper function for reusing disk name
3438 * upon hot insertion.
3439 */
3440static int rssd_disk_name_format(char *prefix,
3441 int index,
3442 char *buf,
3443 int buflen)
3444{
3445 const int base = 'z' - 'a' + 1;
3446 char *begin = buf + strlen(prefix);
3447 char *end = buf + buflen;
3448 char *p;
3449 int unit;
3450
3451 p = end - 1;
3452 *p = '\0';
3453 unit = base;
3454 do {
3455 if (p == begin)
3456 return -EINVAL;
3457 *--p = 'a' + (index % unit);
3458 index = (index / unit) - 1;
3459 } while (index >= 0);
3460
3461 memmove(begin, p, end - p);
3462 memcpy(buf, prefix, strlen(prefix));
3463
3464 return 0;
3465}
3466
3467/*
3468 * Block layer IOCTL handler.
3469 *
3470 * @dev Pointer to the block_device structure.
3471 * @mode ignored
3472 * @cmd IOCTL command passed from the user application.
3473 * @arg Argument passed from the user application.
3474 *
3475 * return value
3476 * 0 IOCTL completed successfully.
3477 * -ENOTTY IOCTL not supported or invalid driver data
3478 * structure pointer.
3479 */
3480static int mtip_block_ioctl(struct block_device *dev,
3481 fmode_t mode,
3482 unsigned cmd,
3483 unsigned long arg)
3484{
3485 struct driver_data *dd = dev->bd_disk->private_data;
3486
3487 if (!capable(CAP_SYS_ADMIN))
3488 return -EACCES;
3489
3490 if (!dd)
3491 return -ENOTTY;
3492
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003493 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003494 return -ENOTTY;
3495
Sam Bradshaw88523a62011-08-30 08:34:26 -06003496 switch (cmd) {
3497 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003498 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003499 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003500 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003501 }
3502}
3503
Jens Axboe16d02c02011-09-27 15:50:01 -06003504#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003505/*
3506 * Block layer compat IOCTL handler.
3507 *
3508 * @dev Pointer to the block_device structure.
3509 * @mode ignored
3510 * @cmd IOCTL command passed from the user application.
3511 * @arg Argument passed from the user application.
3512 *
3513 * return value
3514 * 0 IOCTL completed successfully.
3515 * -ENOTTY IOCTL not supported or invalid driver data
3516 * structure pointer.
3517 */
3518static int mtip_block_compat_ioctl(struct block_device *dev,
3519 fmode_t mode,
3520 unsigned cmd,
3521 unsigned long arg)
3522{
3523 struct driver_data *dd = dev->bd_disk->private_data;
3524
3525 if (!capable(CAP_SYS_ADMIN))
3526 return -EACCES;
3527
3528 if (!dd)
3529 return -ENOTTY;
3530
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003531 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003532 return -ENOTTY;
3533
Sam Bradshaw88523a62011-08-30 08:34:26 -06003534 switch (cmd) {
3535 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003536 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003537 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003538 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003539 ide_task_request_t req_task;
3540 int compat_tasksize, outtotal, ret;
3541
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003542 compat_tasksize =
3543 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003544
3545 compat_req_task =
3546 (struct mtip_compat_ide_task_request_s __user *) arg;
3547
3548 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003549 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003550 return -EFAULT;
3551
3552 if (get_user(req_task.out_size, &compat_req_task->out_size))
3553 return -EFAULT;
3554
3555 if (get_user(req_task.in_size, &compat_req_task->in_size))
3556 return -EFAULT;
3557
3558 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3559
3560 ret = exec_drive_taskfile(dd, (void __user *) arg,
3561 &req_task, outtotal);
3562
3563 if (copy_to_user((void __user *) arg, &req_task,
3564 compat_tasksize -
3565 (2 * sizeof(compat_long_t))))
3566 return -EFAULT;
3567
3568 if (put_user(req_task.out_size, &compat_req_task->out_size))
3569 return -EFAULT;
3570
3571 if (put_user(req_task.in_size, &compat_req_task->in_size))
3572 return -EFAULT;
3573
3574 return ret;
3575 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003576 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003577 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003578 }
3579}
Jens Axboe16d02c02011-09-27 15:50:01 -06003580#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003581
3582/*
3583 * Obtain the geometry of the device.
3584 *
3585 * You may think that this function is obsolete, but some applications,
3586 * fdisk for example still used CHS values. This function describes the
3587 * device as having 224 heads and 56 sectors per cylinder. These values are
3588 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3589 * partition is described in terms of a start and end cylinder this means
3590 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3591 * affects performance.
3592 *
3593 * @dev Pointer to the block_device strucutre.
3594 * @geo Pointer to a hd_geometry structure.
3595 *
3596 * return value
3597 * 0 Operation completed successfully.
3598 * -ENOTTY An error occurred while reading the drive capacity.
3599 */
3600static int mtip_block_getgeo(struct block_device *dev,
3601 struct hd_geometry *geo)
3602{
3603 struct driver_data *dd = dev->bd_disk->private_data;
3604 sector_t capacity;
3605
3606 if (!dd)
3607 return -ENOTTY;
3608
3609 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3610 dev_warn(&dd->pdev->dev,
3611 "Could not get drive capacity.\n");
3612 return -ENOTTY;
3613 }
3614
3615 geo->heads = 224;
3616 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003617 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003618 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003619 return 0;
3620}
3621
Asai Thambi SP51c65702016-02-24 21:18:10 -08003622static int mtip_block_open(struct block_device *dev, fmode_t mode)
3623{
3624 struct driver_data *dd;
3625
3626 if (dev && dev->bd_disk) {
3627 dd = (struct driver_data *) dev->bd_disk->private_data;
3628
3629 if (dd) {
3630 if (test_bit(MTIP_DDF_REMOVAL_BIT,
3631 &dd->dd_flag)) {
3632 return -ENODEV;
3633 }
3634 return 0;
3635 }
3636 }
3637 return -ENODEV;
3638}
3639
Baoyou Xie99e6b872016-08-26 14:08:53 +08003640static void mtip_block_release(struct gendisk *disk, fmode_t mode)
Asai Thambi SP51c65702016-02-24 21:18:10 -08003641{
3642}
3643
Sam Bradshaw88523a62011-08-30 08:34:26 -06003644/*
3645 * Block device operation function.
3646 *
3647 * This structure contains pointers to the functions required by the block
3648 * layer.
3649 */
3650static const struct block_device_operations mtip_block_ops = {
Asai Thambi SP51c65702016-02-24 21:18:10 -08003651 .open = mtip_block_open,
3652 .release = mtip_block_release,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003653 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003654#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003655 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003656#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003657 .getgeo = mtip_block_getgeo,
3658 .owner = THIS_MODULE
3659};
3660
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003661static inline bool is_se_active(struct driver_data *dd)
3662{
3663 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT, &dd->port->flags))) {
3664 if (dd->port->ic_pause_timer) {
3665 unsigned long to = dd->port->ic_pause_timer +
3666 msecs_to_jiffies(1000);
3667 if (time_after(jiffies, to)) {
3668 clear_bit(MTIP_PF_SE_ACTIVE_BIT,
3669 &dd->port->flags);
3670 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
3671 dd->port->ic_pause_timer = 0;
3672 wake_up_interruptible(&dd->port->svc_wait);
3673 return false;
3674 }
3675 }
3676 return true;
3677 }
3678 return false;
3679}
3680
Sam Bradshaw88523a62011-08-30 08:34:26 -06003681/*
3682 * Block layer make request function.
3683 *
3684 * This function is called by the kernel to process a BIO for
3685 * the P320 device.
3686 *
3687 * @queue Pointer to the request queue. Unused other than to obtain
3688 * the driver data structure.
Jens Axboeffc771b2014-05-09 09:42:02 -06003689 * @rq Pointer to the request.
Sam Bradshaw88523a62011-08-30 08:34:26 -06003690 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003691 */
Jens Axboeffc771b2014-05-09 09:42:02 -06003692static int mtip_submit_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003693{
Jens Axboeffc771b2014-05-09 09:42:02 -06003694 struct driver_data *dd = hctx->queue->queuedata;
3695 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3696 unsigned int nents;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003697
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003698 if (is_se_active(dd))
3699 return -ENODATA;
3700
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003701 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3702 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3703 &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003704 return -ENXIO;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003705 }
3706 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003707 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003708 }
3709 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3710 &dd->dd_flag) &&
Jens Axboeffc771b2014-05-09 09:42:02 -06003711 rq_data_dir(rq))) {
3712 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003713 }
Asai Thambi SPaae4a032016-02-24 21:18:20 -08003714 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag) ||
3715 test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)))
Jens Axboeffc771b2014-05-09 09:42:02 -06003716 return -ENODATA;
Asai Thambi S P45038362012-04-09 08:35:38 +02003717 }
3718
Mike Christiec2df40d2016-06-05 14:32:17 -05003719 if (req_op(rq) == REQ_OP_DISCARD) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003720 int err;
3721
3722 err = mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq));
Christoph Hellwigc8a446a2014-09-13 16:40:10 -07003723 blk_mq_end_request(rq, err);
Jens Axboeffc771b2014-05-09 09:42:02 -06003724 return 0;
Asai Thambi S P15283462013-01-11 14:41:34 +01003725 }
3726
Jens Axboeffc771b2014-05-09 09:42:02 -06003727 /* Create the scatter list for this request. */
3728 nents = blk_rq_map_sg(hctx->queue, rq, cmd->sg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003729
Jens Axboeffc771b2014-05-09 09:42:02 -06003730 /* Issue the read/write. */
3731 mtip_hw_submit_io(dd, rq, cmd, nents, hctx);
3732 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003733}
3734
Jens Axboeffc771b2014-05-09 09:42:02 -06003735static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx,
3736 struct request *rq)
3737{
3738 struct driver_data *dd = hctx->queue->queuedata;
3739 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3740
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003741 if (rq_data_dir(rq) == READ || !dd->unal_qdepth)
Jens Axboeffc771b2014-05-09 09:42:02 -06003742 return false;
3743
3744 /*
3745 * If unaligned depth must be limited on this controller, mark it
3746 * as unaligned if the IO isn't on a 4k boundary (start of length).
3747 */
3748 if (blk_rq_sectors(rq) <= 64) {
3749 if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7))
3750 cmd->unaligned = 1;
3751 }
3752
3753 if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal))
3754 return true;
3755
3756 return false;
3757}
3758
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003759static int mtip_issue_reserved_cmd(struct blk_mq_hw_ctx *hctx,
3760 struct request *rq)
3761{
3762 struct driver_data *dd = hctx->queue->queuedata;
3763 struct mtip_int_cmd *icmd = rq->end_io_data;
3764 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3765 struct mtip_cmd_sg *command_sg;
3766
3767 if (mtip_commands_active(dd->port))
3768 return BLK_MQ_RQ_QUEUE_BUSY;
3769
3770 rq->end_io_data = NULL;
3771
3772 /* Populate the SG list */
3773 cmd->command_header->opts =
3774 __force_bit2int cpu_to_le32(icmd->opts | icmd->fis_len);
3775 if (icmd->buf_len) {
3776 command_sg = cmd->command + AHCI_CMD_TBL_HDR_SZ;
3777
3778 command_sg->info =
3779 __force_bit2int cpu_to_le32((icmd->buf_len-1) & 0x3FFFFF);
3780 command_sg->dba =
3781 __force_bit2int cpu_to_le32(icmd->buffer & 0xFFFFFFFF);
3782 command_sg->dba_upper =
3783 __force_bit2int cpu_to_le32((icmd->buffer >> 16) >> 16);
3784
3785 cmd->command_header->opts |=
3786 __force_bit2int cpu_to_le32((1 << 16));
3787 }
3788
3789 /* Populate the command header */
3790 cmd->command_header->byte_count = 0;
3791
3792 blk_mq_start_request(rq);
3793 mtip_issue_non_ncq_command(dd->port, rq->tag);
3794 return BLK_MQ_RQ_QUEUE_OK;
3795}
3796
Jens Axboe74c45052014-10-29 11:14:52 -06003797static int mtip_queue_rq(struct blk_mq_hw_ctx *hctx,
3798 const struct blk_mq_queue_data *bd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003799{
Jens Axboe74c45052014-10-29 11:14:52 -06003800 struct request *rq = bd->rq;
Jens Axboeffc771b2014-05-09 09:42:02 -06003801 int ret;
3802
Ming Leia4e84aa2017-04-27 07:45:18 -06003803 mtip_init_cmd_header(rq);
3804
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003805 if (blk_rq_is_passthrough(rq))
3806 return mtip_issue_reserved_cmd(hctx, rq);
3807
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003808 if (unlikely(mtip_check_unal_depth(hctx, rq)))
Jens Axboeffc771b2014-05-09 09:42:02 -06003809 return BLK_MQ_RQ_QUEUE_BUSY;
3810
Christoph Hellwige2490072014-09-13 16:40:09 -07003811 blk_mq_start_request(rq);
3812
Jens Axboeffc771b2014-05-09 09:42:02 -06003813 ret = mtip_submit_request(hctx, rq);
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003814 if (likely(!ret))
Jens Axboeffc771b2014-05-09 09:42:02 -06003815 return BLK_MQ_RQ_QUEUE_OK;
3816
Jens Axboeffc771b2014-05-09 09:42:02 -06003817 return BLK_MQ_RQ_QUEUE_ERROR;
3818}
3819
Christoph Hellwigd6296d392017-05-01 10:19:08 -06003820static void mtip_free_cmd(struct blk_mq_tag_set *set, struct request *rq,
3821 unsigned int hctx_idx)
Jens Axboeffc771b2014-05-09 09:42:02 -06003822{
Christoph Hellwigd6296d392017-05-01 10:19:08 -06003823 struct driver_data *dd = set->driver_data;
Jens Axboeffc771b2014-05-09 09:42:02 -06003824 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3825
3826 if (!cmd->command)
3827 return;
3828
3829 dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3830 cmd->command, cmd->command_dma);
3831}
3832
Christoph Hellwigd6296d392017-05-01 10:19:08 -06003833static int mtip_init_cmd(struct blk_mq_tag_set *set, struct request *rq,
3834 unsigned int hctx_idx, unsigned int numa_node)
Jens Axboeffc771b2014-05-09 09:42:02 -06003835{
Christoph Hellwigd6296d392017-05-01 10:19:08 -06003836 struct driver_data *dd = set->driver_data;
Jens Axboeffc771b2014-05-09 09:42:02 -06003837 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
Jens Axboeffc771b2014-05-09 09:42:02 -06003838
3839 cmd->command = dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3840 &cmd->command_dma, GFP_KERNEL);
3841 if (!cmd->command)
3842 return -ENOMEM;
3843
3844 memset(cmd->command, 0, CMD_DMA_ALLOC_SZ);
3845
Jens Axboeffc771b2014-05-09 09:42:02 -06003846 sg_init_table(cmd->sg, MTIP_MAX_SG);
3847 return 0;
3848}
3849
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003850static enum blk_eh_timer_return mtip_cmd_timeout(struct request *req,
3851 bool reserved)
3852{
3853 struct driver_data *dd = req->q->queuedata;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003854
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003855 if (reserved) {
3856 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(req);
3857
3858 cmd->status = -ETIME;
3859 if (cmd->comp_func)
3860 cmd->comp_func(dd->port, MTIP_TAG_INTERNAL, cmd, -ETIME);
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003861 goto exit_handler;
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003862 }
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003863
3864 if (test_bit(req->tag, dd->port->cmds_to_issue))
3865 goto exit_handler;
3866
3867 if (test_and_set_bit(MTIP_PF_TO_ACTIVE_BIT, &dd->port->flags))
3868 goto exit_handler;
3869
3870 wake_up_interruptible(&dd->port->svc_wait);
3871exit_handler:
Jens Axboe90beb2e2016-03-04 08:15:48 -07003872 return BLK_EH_RESET_TIMER;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003873}
3874
Eric Biggersf363b082017-03-30 13:39:16 -07003875static const struct blk_mq_ops mtip_mq_ops = {
Jens Axboeffc771b2014-05-09 09:42:02 -06003876 .queue_rq = mtip_queue_rq,
Jens Axboeffc771b2014-05-09 09:42:02 -06003877 .init_request = mtip_init_cmd,
3878 .exit_request = mtip_free_cmd,
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003879 .complete = mtip_softirq_done_fn,
3880 .timeout = mtip_cmd_timeout,
Jens Axboeffc771b2014-05-09 09:42:02 -06003881};
3882
Sam Bradshaw88523a62011-08-30 08:34:26 -06003883/*
3884 * Block layer initialization function.
3885 *
3886 * This function is called once by the PCI layer for each P320
3887 * device that is connected to the system.
3888 *
3889 * @dd Pointer to the driver data structure.
3890 *
3891 * return value
3892 * 0 on success else an error code.
3893 */
Jens Axboe63166682011-09-27 21:27:43 -06003894static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003895{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003896 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003897 sector_t capacity;
3898 unsigned int index = 0;
3899 struct kobject *kobj;
3900
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003901 if (dd->disk)
3902 goto skip_create_disk; /* hw init done, before rebuild */
3903
Jens Axboeffc771b2014-05-09 09:42:02 -06003904 if (mtip_hw_init(dd)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003905 rv = -EINVAL;
3906 goto protocol_init_error;
3907 }
3908
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003909 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003910 if (dd->disk == NULL) {
3911 dev_err(&dd->pdev->dev,
3912 "Unable to allocate gendisk structure\n");
3913 rv = -EINVAL;
3914 goto alloc_disk_error;
3915 }
3916
3917 /* Generate the disk name, implemented same as in sd.c */
3918 do {
Pan Bian5b0e34e2016-12-01 10:10:46 +08003919 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL)) {
3920 rv = -ENOMEM;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003921 goto ida_get_error;
Pan Bian5b0e34e2016-12-01 10:10:46 +08003922 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003923
3924 spin_lock(&rssd_index_lock);
3925 rv = ida_get_new(&rssd_index_ida, &index);
3926 spin_unlock(&rssd_index_lock);
3927 } while (rv == -EAGAIN);
3928
3929 if (rv)
3930 goto ida_get_error;
3931
3932 rv = rssd_disk_name_format("rssd",
3933 index,
3934 dd->disk->disk_name,
3935 DISK_NAME_LEN);
3936 if (rv)
3937 goto disk_index_error;
3938
Sam Bradshaw88523a62011-08-30 08:34:26 -06003939 dd->disk->major = dd->major;
Asai Thambi SP75787262015-05-11 15:55:26 -07003940 dd->disk->first_minor = index * MTIP_MAX_MINORS;
3941 dd->disk->minors = MTIP_MAX_MINORS;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003942 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003943 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003944 dd->index = index;
3945
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003946 mtip_hw_debugfs_init(dd);
3947
Jens Axboeffc771b2014-05-09 09:42:02 -06003948 memset(&dd->tags, 0, sizeof(dd->tags));
3949 dd->tags.ops = &mtip_mq_ops;
3950 dd->tags.nr_hw_queues = 1;
3951 dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS;
3952 dd->tags.reserved_tags = 1;
3953 dd->tags.cmd_size = sizeof(struct mtip_cmd);
3954 dd->tags.numa_node = dd->numa_node;
Jens Axboea800ce82017-04-27 16:46:02 -06003955 dd->tags.flags = BLK_MQ_F_SHOULD_MERGE;
Jens Axboeffc771b2014-05-09 09:42:02 -06003956 dd->tags.driver_data = dd;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003957 dd->tags.timeout = MTIP_NCQ_CMD_TIMEOUT_MS;
Jens Axboeffc771b2014-05-09 09:42:02 -06003958
3959 rv = blk_mq_alloc_tag_set(&dd->tags);
3960 if (rv) {
3961 dev_err(&dd->pdev->dev,
3962 "Unable to allocate request queue\n");
Asai Thambi SP008e56d2016-02-24 21:21:20 -08003963 goto block_queue_alloc_tag_error;
Jens Axboeffc771b2014-05-09 09:42:02 -06003964 }
3965
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003966 /* Allocate the request queue. */
Jens Axboeffc771b2014-05-09 09:42:02 -06003967 dd->queue = blk_mq_init_queue(&dd->tags);
Dan Carpentera8a642c2014-05-14 15:54:18 +03003968 if (IS_ERR(dd->queue)) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003969 dev_err(&dd->pdev->dev,
3970 "Unable to allocate request queue\n");
3971 rv = -ENOMEM;
3972 goto block_queue_alloc_init_error;
3973 }
3974
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003975 dd->disk->queue = dd->queue;
3976 dd->queue->queuedata = dd;
3977
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08003978skip_create_disk:
Jens Axboeffc771b2014-05-09 09:42:02 -06003979 /* Initialize the protocol layer. */
3980 wait_for_rebuild = mtip_hw_get_identify(dd);
3981 if (wait_for_rebuild < 0) {
3982 dev_err(&dd->pdev->dev,
3983 "Protocol layer initialization failed\n");
3984 rv = -EINVAL;
3985 goto init_hw_cmds_error;
3986 }
3987
3988 /*
3989 * if rebuild pending, start the service thread, and delay the block
Dan Williams0d52c7562016-06-15 19:44:20 -07003990 * queue creation and device_add_disk()
Jens Axboeffc771b2014-05-09 09:42:02 -06003991 */
3992 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3993 goto start_service_thread;
3994
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003995 /* Set device limits. */
3996 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
Mike Snitzerb277da02014-10-04 10:55:32 -06003997 clear_bit(QUEUE_FLAG_ADD_RANDOM, &dd->queue->queue_flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003998 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3999 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07004000 blk_queue_max_hw_sectors(dd->queue, 0xffff);
4001 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004002 blk_queue_io_min(dd->queue, 4096);
Felipe Franciosi1044b1b2014-03-13 14:34:20 +00004003 blk_queue_bounce_limit(dd->queue, dd->pdev->dma_mask);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07004004
Asai Thambi S P15283462013-01-11 14:41:34 +01004005 /* Signal trim support */
4006 if (dd->trim_supp == true) {
4007 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
4008 dd->queue->limits.discard_granularity = 4096;
4009 blk_queue_max_discard_sectors(dd->queue,
4010 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
Asai Thambi S P15283462013-01-11 14:41:34 +01004011 }
4012
Sam Bradshaw88523a62011-08-30 08:34:26 -06004013 /* Set the capacity of the device in 512 byte sectors. */
4014 if (!(mtip_hw_get_capacity(dd, &capacity))) {
4015 dev_warn(&dd->pdev->dev,
4016 "Could not read drive capacity\n");
4017 rv = -EIO;
4018 goto read_capacity_error;
4019 }
4020 set_capacity(dd->disk, capacity);
4021
4022 /* Enable the block device and add it to /dev */
Dan Williams0d52c7562016-06-15 19:44:20 -07004023 device_add_disk(&dd->pdev->dev, dd->disk);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004024
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004025 dd->bdev = bdget_disk(dd->disk, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004026 /*
4027 * Now that the disk is active, initialize any sysfs attributes
4028 * managed by the protocol layer.
4029 */
4030 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4031 if (kobj) {
4032 mtip_hw_sysfs_init(dd, kobj);
4033 kobject_put(kobj);
4034 }
4035
Asai Thambi S P45038362012-04-09 08:35:38 +02004036 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004037 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004038 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02004039 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004040
4041start_service_thread:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004042 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
Rasmus Villemoes8aeea032015-11-20 10:46:49 +01004043 dd, dd->numa_node,
4044 "mtip_svc_thd_%02d", index);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004045
4046 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02004047 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004048 dd->mtip_svc_handler = NULL;
4049 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004050 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004051 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004052 wake_up_process(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02004053 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
4054 rv = wait_for_rebuild;
4055
Sam Bradshaw88523a62011-08-30 08:34:26 -06004056 return rv;
4057
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004058kthread_run_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004059 bdput(dd->bdev);
4060 dd->bdev = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004061
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004062 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004063 del_gendisk(dd->disk);
4064
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004065read_capacity_error:
Jens Axboeffc771b2014-05-09 09:42:02 -06004066init_hw_cmds_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004067 blk_cleanup_queue(dd->queue);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004068block_queue_alloc_init_error:
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004069 blk_mq_free_tag_set(&dd->tags);
4070block_queue_alloc_tag_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004071 mtip_hw_debugfs_exit(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004072disk_index_error:
4073 spin_lock(&rssd_index_lock);
4074 ida_remove(&rssd_index_ida, index);
4075 spin_unlock(&rssd_index_lock);
4076
4077ida_get_error:
4078 put_disk(dd->disk);
4079
4080alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004081 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004082
4083protocol_init_error:
4084 return rv;
4085}
4086
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004087static void mtip_no_dev_cleanup(struct request *rq, void *data, bool reserv)
4088{
4089 struct driver_data *dd = (struct driver_data *)data;
4090 struct mtip_cmd *cmd;
4091
Christoph Hellwig4dda4732017-04-20 16:03:07 +02004092 if (likely(!reserv)) {
Jens Axboe95c55ff2017-04-21 08:46:44 -06004093 cmd = blk_mq_rq_to_pdu(rq);
Christoph Hellwig4dda4732017-04-20 16:03:07 +02004094 cmd->status = -ENODEV;
Christoph Hellwig08e00292017-04-20 16:03:09 +02004095 blk_mq_complete_request(rq);
Christoph Hellwig4dda4732017-04-20 16:03:07 +02004096 } else if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &dd->port->flags)) {
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004097
4098 cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Jens Axboe3f5e6a32017-04-28 10:45:08 -06004099 cmd->status = -ENODEV;
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004100 if (cmd->comp_func)
4101 cmd->comp_func(dd->port, MTIP_TAG_INTERNAL,
4102 cmd, -ENODEV);
4103 }
4104}
4105
Sam Bradshaw88523a62011-08-30 08:34:26 -06004106/*
4107 * Block layer deinitialization function.
4108 *
4109 * Called by the PCI layer as each P320 device is removed.
4110 *
4111 * @dd Pointer to the driver data structure.
4112 *
4113 * return value
4114 * 0
4115 */
Jens Axboe63166682011-09-27 21:27:43 -06004116static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004117{
4118 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004119
Asai Thambi SP2132a542015-05-11 15:53:18 -07004120 mtip_hw_debugfs_exit(dd);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004121
Asai Thambi SP2132a542015-05-11 15:53:18 -07004122 if (dd->mtip_svc_handler) {
4123 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4124 wake_up_interruptible(&dd->port->svc_wait);
4125 kthread_stop(dd->mtip_svc_handler);
4126 }
4127
4128 /* Clean up the sysfs attributes, if created */
4129 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4130 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4131 if (kobj) {
4132 mtip_hw_sysfs_exit(dd, kobj);
4133 kobject_put(kobj);
Asai Thambi S P45038362012-04-09 08:35:38 +02004134 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004135 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004136
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004137 if (!dd->sr) {
4138 /*
4139 * Explicitly wait here for IOs to quiesce,
4140 * as mtip_standby_drive usually won't wait for IOs.
4141 */
Jens Axboe8afdd942017-04-27 16:32:41 -06004142 if (!mtip_quiesce_io(dd->port, MTIP_QUIESCE_IO_TIMEOUT_MS))
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004143 mtip_standby_drive(dd);
4144 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004145 else
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004146 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
4147 dd->disk->disk_name);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004148
Ming Lei1671d522017-03-27 20:06:57 +08004149 blk_freeze_queue_start(dd->queue);
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004150 blk_mq_stop_hw_queues(dd->queue);
Keith Busch6d125de2016-03-10 13:58:48 +02004151 blk_mq_tagset_busy_iter(&dd->tags, mtip_no_dev_cleanup, dd);
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004152
Asai Thambi SP2132a542015-05-11 15:53:18 -07004153 /*
4154 * Delete our gendisk structure. This also removes the device
4155 * from /dev
4156 */
4157 if (dd->bdev) {
4158 bdput(dd->bdev);
4159 dd->bdev = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004160 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004161 if (dd->disk) {
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08004162 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
4163 del_gendisk(dd->disk);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004164 if (dd->disk->queue) {
4165 blk_cleanup_queue(dd->queue);
4166 blk_mq_free_tag_set(&dd->tags);
4167 dd->queue = NULL;
4168 }
4169 put_disk(dd->disk);
4170 }
4171 dd->disk = NULL;
4172
4173 spin_lock(&rssd_index_lock);
4174 ida_remove(&rssd_index_ida, dd->index);
4175 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004176
4177 /* De-initialize the protocol layer. */
4178 mtip_hw_exit(dd);
4179
4180 return 0;
4181}
4182
4183/*
4184 * Function called by the PCI layer when just before the
4185 * machine shuts down.
4186 *
4187 * If a protocol layer shutdown function is present it will be called
4188 * by this function.
4189 *
4190 * @dd Pointer to the driver data structure.
4191 *
4192 * return value
4193 * 0
4194 */
Jens Axboe63166682011-09-27 21:27:43 -06004195static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004196{
Jens Axboeffc771b2014-05-09 09:42:02 -06004197 mtip_hw_shutdown(dd);
4198
Sam Bradshaw88523a62011-08-30 08:34:26 -06004199 /* Delete our gendisk structure, and cleanup the blk queue. */
Asai Thambi S P58c49df2013-01-11 18:47:12 +05304200 if (dd->disk) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304201 dev_info(&dd->pdev->dev,
4202 "Shutting down %s ...\n", dd->disk->disk_name);
Asai Thambi S P58c49df2013-01-11 18:47:12 +05304203
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08004204 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
4205 del_gendisk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304206 if (dd->disk->queue) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304207 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004208 blk_mq_free_tag_set(&dd->tags);
Asai Thambi SP02b48262015-05-11 15:48:00 -07004209 }
4210 put_disk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304211 dd->disk = NULL;
4212 dd->queue = NULL;
4213 }
Asai Thambi S P8182b492012-04-09 08:35:38 +02004214
4215 spin_lock(&rssd_index_lock);
4216 ida_remove(&rssd_index_ida, dd->index);
4217 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004218 return 0;
4219}
4220
Jens Axboe63166682011-09-27 21:27:43 -06004221static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004222{
4223 dev_info(&dd->pdev->dev,
4224 "Suspending %s ...\n", dd->disk->disk_name);
4225 mtip_hw_suspend(dd);
4226 return 0;
4227}
4228
Jens Axboe63166682011-09-27 21:27:43 -06004229static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004230{
4231 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4232 dd->disk->disk_name);
4233 mtip_hw_resume(dd);
4234 return 0;
4235}
4236
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004237static void drop_cpu(int cpu)
4238{
4239 cpu_use[cpu]--;
4240}
4241
4242static int get_least_used_cpu_on_node(int node)
4243{
4244 int cpu, least_used_cpu, least_cnt;
4245 const struct cpumask *node_mask;
4246
4247 node_mask = cpumask_of_node(node);
4248 least_used_cpu = cpumask_first(node_mask);
4249 least_cnt = cpu_use[least_used_cpu];
4250 cpu = least_used_cpu;
4251
4252 for_each_cpu(cpu, node_mask) {
4253 if (cpu_use[cpu] < least_cnt) {
4254 least_used_cpu = cpu;
4255 least_cnt = cpu_use[cpu];
4256 }
4257 }
4258 cpu_use[least_used_cpu]++;
4259 return least_used_cpu;
4260}
4261
4262/* Helper for selecting a node in round robin mode */
4263static inline int mtip_get_next_rr_node(void)
4264{
4265 static int next_node = -1;
4266
4267 if (next_node == -1) {
4268 next_node = first_online_node;
4269 return next_node;
4270 }
4271
4272 next_node = next_online_node(next_node);
4273 if (next_node == MAX_NUMNODES)
4274 next_node = first_online_node;
4275 return next_node;
4276}
4277
Fengguang Wu25bac122013-01-12 15:31:40 +08004278static DEFINE_HANDLER(0);
4279static DEFINE_HANDLER(1);
4280static DEFINE_HANDLER(2);
4281static DEFINE_HANDLER(3);
4282static DEFINE_HANDLER(4);
4283static DEFINE_HANDLER(5);
4284static DEFINE_HANDLER(6);
4285static DEFINE_HANDLER(7);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004286
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004287static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
4288{
4289 int pos;
4290 unsigned short pcie_dev_ctrl;
4291
4292 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
4293 if (pos) {
4294 pci_read_config_word(pdev,
4295 pos + PCI_EXP_DEVCTL,
4296 &pcie_dev_ctrl);
4297 if (pcie_dev_ctrl & (1 << 11) ||
4298 pcie_dev_ctrl & (1 << 4)) {
4299 dev_info(&dd->pdev->dev,
4300 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4301 pdev->vendor, pdev->device);
4302 pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
4303 PCI_EXP_DEVCTL_RELAX_EN);
4304 pci_write_config_word(pdev,
4305 pos + PCI_EXP_DEVCTL,
4306 pcie_dev_ctrl);
4307 }
4308 }
4309}
4310
4311static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev)
4312{
4313 /*
4314 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4315 * device with device id 0x5aXX
4316 */
4317 if (pdev->bus && pdev->bus->self) {
4318 if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI &&
4319 ((pdev->bus->self->device & 0xff00) == 0x5a00)) {
4320 mtip_disable_link_opts(dd, pdev->bus->self);
4321 } else {
4322 /* Check further up the topology */
4323 struct pci_dev *parent_dev = pdev->bus->self;
4324 if (parent_dev->bus &&
4325 parent_dev->bus->parent &&
4326 parent_dev->bus->parent->self &&
4327 parent_dev->bus->parent->self->vendor ==
4328 PCI_VENDOR_ID_ATI &&
4329 (parent_dev->bus->parent->self->device &
4330 0xff00) == 0x5a00) {
4331 mtip_disable_link_opts(dd,
4332 parent_dev->bus->parent->self);
4333 }
4334 }
4335 }
4336}
4337
Sam Bradshaw88523a62011-08-30 08:34:26 -06004338/*
4339 * Called for each supported PCI device detected.
4340 *
4341 * This function allocates the private data structure, enables the
4342 * PCI device and then calls the block layer initialization function.
4343 *
4344 * return value
4345 * 0 on success else an error code.
4346 */
4347static int mtip_pci_probe(struct pci_dev *pdev,
4348 const struct pci_device_id *ent)
4349{
4350 int rv = 0;
4351 struct driver_data *dd = NULL;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004352 char cpu_list[256];
4353 const struct cpumask *node_mask;
4354 int cpu, i = 0, j = 0;
4355 int my_node = NUMA_NO_NODE;
Asai Thambi S P0caff002013-04-03 19:56:21 +05304356 unsigned long flags;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004357
4358 /* Allocate memory for this devices private data. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004359 my_node = pcibus_to_node(pdev->bus);
4360 if (my_node != NUMA_NO_NODE) {
4361 if (!node_online(my_node))
4362 my_node = mtip_get_next_rr_node();
4363 } else {
4364 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4365 my_node = mtip_get_next_rr_node();
4366 }
4367 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4368 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
Jens Axboe7f328902014-03-10 14:29:37 -06004369 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004370
4371 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004372 if (dd == NULL) {
4373 dev_err(&pdev->dev,
4374 "Unable to allocate memory for driver data\n");
4375 return -ENOMEM;
4376 }
4377
Sam Bradshaw88523a62011-08-30 08:34:26 -06004378 /* Attach the private data to this PCI device. */
4379 pci_set_drvdata(pdev, dd);
4380
4381 rv = pcim_enable_device(pdev);
4382 if (rv < 0) {
4383 dev_err(&pdev->dev, "Unable to enable device\n");
4384 goto iomap_err;
4385 }
4386
4387 /* Map BAR5 to memory. */
4388 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4389 if (rv < 0) {
4390 dev_err(&pdev->dev, "Unable to map regions\n");
4391 goto iomap_err;
4392 }
4393
4394 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4395 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4396
4397 if (rv) {
4398 rv = pci_set_consistent_dma_mask(pdev,
4399 DMA_BIT_MASK(32));
4400 if (rv) {
4401 dev_warn(&pdev->dev,
4402 "64-bit DMA enable failed\n");
4403 goto setmask_err;
4404 }
4405 }
4406 }
4407
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004408 /* Copy the info we may need later into the private data structure. */
4409 dd->major = mtip_major;
4410 dd->instance = instance;
4411 dd->pdev = pdev;
4412 dd->numa_node = my_node;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004413
Asai Thambi S P0caff002013-04-03 19:56:21 +05304414 INIT_LIST_HEAD(&dd->online_list);
4415 INIT_LIST_HEAD(&dd->remove_list);
4416
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004417 memset(dd->workq_name, 0, 32);
4418 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4419
4420 dd->isr_workq = create_workqueue(dd->workq_name);
4421 if (!dd->isr_workq) {
4422 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
Wei Yongjund137c832013-03-22 08:58:23 -06004423 rv = -ENOMEM;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004424 goto block_initialize_err;
4425 }
4426
4427 memset(cpu_list, 0, sizeof(cpu_list));
4428
4429 node_mask = cpumask_of_node(dd->numa_node);
4430 if (!cpumask_empty(node_mask)) {
4431 for_each_cpu(cpu, node_mask)
4432 {
4433 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4434 j = strlen(cpu_list);
4435 }
4436
4437 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4438 dd->numa_node,
4439 topology_physical_package_id(cpumask_first(node_mask)),
4440 nr_cpus_node(dd->numa_node),
4441 cpu_list);
4442 } else
4443 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4444
4445 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4446 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4447 cpu_to_node(dd->isr_binding), dd->isr_binding);
4448
4449 /* first worker context always runs in ISR */
4450 dd->work[0].cpu_binding = dd->isr_binding;
4451 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4452 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4453 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4454 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4455 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4456 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4457 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4458
4459 /* Log the bindings */
4460 for_each_present_cpu(cpu) {
4461 memset(cpu_list, 0, sizeof(cpu_list));
4462 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4463 if (dd->work[i].cpu_binding == cpu) {
4464 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4465 j = strlen(cpu_list);
4466 }
4467 }
4468 if (j)
4469 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4470 }
4471
4472 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4473 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4474 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4475 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4476 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4477 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4478 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4479 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4480
4481 pci_set_master(pdev);
Wei Yongjund137c832013-03-22 08:58:23 -06004482 rv = pci_enable_msi(pdev);
4483 if (rv) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004484 dev_warn(&pdev->dev,
4485 "Unable to enable MSI interrupt.\n");
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004486 goto msi_initialize_err;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004487 }
4488
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004489 mtip_fix_ero_nosnoop(dd, pdev);
4490
Sam Bradshaw88523a62011-08-30 08:34:26 -06004491 /* Initialize the block layer. */
4492 rv = mtip_block_initialize(dd);
4493 if (rv < 0) {
4494 dev_err(&pdev->dev,
4495 "Unable to initialize block layer\n");
4496 goto block_initialize_err;
4497 }
4498
4499 /*
4500 * Increment the instance count so that each device has a unique
4501 * instance number.
4502 */
4503 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02004504 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004505 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P6b06d352013-04-03 19:54:35 +05304506 else
4507 rv = 0; /* device in rebuild state, return 0 from probe */
Asai Thambi S P0caff002013-04-03 19:56:21 +05304508
4509 /* Add to online list even if in ftl rebuild */
4510 spin_lock_irqsave(&dev_lock, flags);
4511 list_add(&dd->online_list, &online_list);
4512 spin_unlock_irqrestore(&dev_lock, flags);
4513
Sam Bradshaw88523a62011-08-30 08:34:26 -06004514 goto done;
4515
4516block_initialize_err:
4517 pci_disable_msi(pdev);
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004518
4519msi_initialize_err:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004520 if (dd->isr_workq) {
4521 flush_workqueue(dd->isr_workq);
4522 destroy_workqueue(dd->isr_workq);
4523 drop_cpu(dd->work[0].cpu_binding);
4524 drop_cpu(dd->work[1].cpu_binding);
4525 drop_cpu(dd->work[2].cpu_binding);
4526 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004527setmask_err:
4528 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4529
4530iomap_err:
4531 kfree(dd);
4532 pci_set_drvdata(pdev, NULL);
4533 return rv;
4534done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06004535 return rv;
4536}
4537
4538/*
4539 * Called for each probed device when the device is removed or the
4540 * driver is unloaded.
4541 *
4542 * return value
4543 * None
4544 */
4545static void mtip_pci_remove(struct pci_dev *pdev)
4546{
4547 struct driver_data *dd = pci_get_drvdata(pdev);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004548 unsigned long flags, to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004549
Asai Thambi SP51c65702016-02-24 21:18:10 -08004550 set_bit(MTIP_DDF_REMOVAL_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02004551
Asai Thambi S P0caff002013-04-03 19:56:21 +05304552 spin_lock_irqsave(&dev_lock, flags);
4553 list_del_init(&dd->online_list);
4554 list_add(&dd->remove_list, &removing_list);
4555 spin_unlock_irqrestore(&dev_lock, flags);
4556
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004557 mtip_check_surprise_removal(pdev);
4558 synchronize_irq(dd->pdev->irq);
4559
4560 /* Spin until workers are done */
4561 to = jiffies + msecs_to_jiffies(4000);
4562 do {
4563 msleep(20);
4564 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4565 time_before(jiffies, to));
4566
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004567 if (!dd->sr)
4568 fsync_bdev(dd->bdev);
Asai Thambi SP51c65702016-02-24 21:18:10 -08004569
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004570 if (atomic_read(&dd->irq_workers_active) != 0) {
4571 dev_warn(&dd->pdev->dev,
4572 "Completion workers still active!\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004573 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004574
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004575 blk_set_queue_dying(dd->queue);
Asai Thambi SP51c65702016-02-24 21:18:10 -08004576 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
4577
Sam Bradshaw88523a62011-08-30 08:34:26 -06004578 /* Clean up the block layer. */
4579 mtip_block_remove(dd);
4580
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004581 if (dd->isr_workq) {
4582 flush_workqueue(dd->isr_workq);
4583 destroy_workqueue(dd->isr_workq);
4584 drop_cpu(dd->work[0].cpu_binding);
4585 drop_cpu(dd->work[1].cpu_binding);
4586 drop_cpu(dd->work[2].cpu_binding);
4587 }
4588
Sam Bradshaw88523a62011-08-30 08:34:26 -06004589 pci_disable_msi(pdev);
4590
Asai Thambi S P0caff002013-04-03 19:56:21 +05304591 spin_lock_irqsave(&dev_lock, flags);
4592 list_del_init(&dd->remove_list);
4593 spin_unlock_irqrestore(&dev_lock, flags);
4594
Asai Thambi SP2132a542015-05-11 15:53:18 -07004595 kfree(dd);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004596
Sam Bradshaw88523a62011-08-30 08:34:26 -06004597 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004598 pci_set_drvdata(pdev, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004599}
4600
4601/*
4602 * Called for each probed device when the device is suspended.
4603 *
4604 * return value
4605 * 0 Success
4606 * <0 Error
4607 */
4608static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4609{
4610 int rv = 0;
4611 struct driver_data *dd = pci_get_drvdata(pdev);
4612
4613 if (!dd) {
4614 dev_err(&pdev->dev,
4615 "Driver private datastructure is NULL\n");
4616 return -EFAULT;
4617 }
4618
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004619 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004620
4621 /* Disable ports & interrupts then send standby immediate */
4622 rv = mtip_block_suspend(dd);
4623 if (rv < 0) {
4624 dev_err(&pdev->dev,
4625 "Failed to suspend controller\n");
4626 return rv;
4627 }
4628
4629 /*
4630 * Save the pci config space to pdev structure &
4631 * disable the device
4632 */
4633 pci_save_state(pdev);
4634 pci_disable_device(pdev);
4635
4636 /* Move to Low power state*/
4637 pci_set_power_state(pdev, PCI_D3hot);
4638
4639 return rv;
4640}
4641
4642/*
4643 * Called for each probed device when the device is resumed.
4644 *
4645 * return value
4646 * 0 Success
4647 * <0 Error
4648 */
4649static int mtip_pci_resume(struct pci_dev *pdev)
4650{
4651 int rv = 0;
4652 struct driver_data *dd;
4653
4654 dd = pci_get_drvdata(pdev);
4655 if (!dd) {
4656 dev_err(&pdev->dev,
4657 "Driver private datastructure is NULL\n");
4658 return -EFAULT;
4659 }
4660
4661 /* Move the device to active State */
4662 pci_set_power_state(pdev, PCI_D0);
4663
4664 /* Restore PCI configuration space */
4665 pci_restore_state(pdev);
4666
4667 /* Enable the PCI device*/
4668 rv = pcim_enable_device(pdev);
4669 if (rv < 0) {
4670 dev_err(&pdev->dev,
4671 "Failed to enable card during resume\n");
4672 goto err;
4673 }
4674 pci_set_master(pdev);
4675
4676 /*
4677 * Calls hbaReset, initPort, & startPort function
4678 * then enables interrupts
4679 */
4680 rv = mtip_block_resume(dd);
4681 if (rv < 0)
4682 dev_err(&pdev->dev, "Unable to resume\n");
4683
4684err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004685 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004686
4687 return rv;
4688}
4689
4690/*
4691 * Shutdown routine
4692 *
4693 * return value
4694 * None
4695 */
4696static void mtip_pci_shutdown(struct pci_dev *pdev)
4697{
4698 struct driver_data *dd = pci_get_drvdata(pdev);
4699 if (dd)
4700 mtip_block_shutdown(dd);
4701}
4702
Sam Bradshaw88523a62011-08-30 08:34:26 -06004703/* Table of device ids supported by this driver. */
Benoit Taine9baa3c32014-08-08 15:56:03 +02004704static const struct pci_device_id mtip_pci_tbl[] = {
Asai Thambi S P1a131452012-09-05 22:00:38 +05304705 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4706 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4707 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4708 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4709 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4710 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4711 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
Sam Bradshaw88523a62011-08-30 08:34:26 -06004712 { 0 }
4713};
4714
4715/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004716static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004717 .name = MTIP_DRV_NAME,
4718 .id_table = mtip_pci_tbl,
4719 .probe = mtip_pci_probe,
4720 .remove = mtip_pci_remove,
4721 .suspend = mtip_pci_suspend,
4722 .resume = mtip_pci_resume,
4723 .shutdown = mtip_pci_shutdown,
4724};
4725
4726MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4727
4728/*
4729 * Module initialization function.
4730 *
4731 * Called once when the module is loaded. This function allocates a major
4732 * block device number to the Cyclone devices and registers the PCI layer
4733 * of the driver.
4734 *
4735 * Return value
4736 * 0 on success else error code.
4737 */
4738static int __init mtip_init(void)
4739{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004740 int error;
4741
Asai Thambi S P45422e72012-09-05 22:03:56 +05304742 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004743
Asai Thambi S P0caff002013-04-03 19:56:21 +05304744 spin_lock_init(&dev_lock);
4745
4746 INIT_LIST_HEAD(&online_list);
4747 INIT_LIST_HEAD(&removing_list);
4748
Sam Bradshaw88523a62011-08-30 08:34:26 -06004749 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004750 error = register_blkdev(0, MTIP_DRV_NAME);
4751 if (error <= 0) {
Asai Thambi S P45422e72012-09-05 22:03:56 +05304752 pr_err("Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004753 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004754 return -EBUSY;
4755 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004756 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004757
Asai Thambi S P0caff002013-04-03 19:56:21 +05304758 dfs_parent = debugfs_create_dir("rssd", NULL);
4759 if (IS_ERR_OR_NULL(dfs_parent)) {
4760 pr_warn("Error creating debugfs parent\n");
4761 dfs_parent = NULL;
4762 }
4763 if (dfs_parent) {
4764 dfs_device_status = debugfs_create_file("device_status",
4765 S_IRUGO, dfs_parent, NULL,
4766 &mtip_device_status_fops);
4767 if (IS_ERR_OR_NULL(dfs_device_status)) {
4768 pr_err("Error creating device_status node\n");
4769 dfs_device_status = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004770 }
4771 }
4772
Sam Bradshaw88523a62011-08-30 08:34:26 -06004773 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004774 error = pci_register_driver(&mtip_pci_driver);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004775 if (error) {
4776 debugfs_remove(dfs_parent);
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004777 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004778 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004779
4780 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004781}
4782
4783/*
4784 * Module de-initialization function.
4785 *
4786 * Called once when the module is unloaded. This function deallocates
4787 * the major block device number allocated by mtip_init() and
4788 * unregisters the PCI layer of the driver.
4789 *
4790 * Return value
4791 * none
4792 */
4793static void __exit mtip_exit(void)
4794{
Sam Bradshaw88523a62011-08-30 08:34:26 -06004795 /* Release the allocated major block device number. */
4796 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4797
4798 /* Unregister the PCI driver. */
4799 pci_unregister_driver(&mtip_pci_driver);
Asai Thambi S Paf5ded82014-04-16 20:30:16 -07004800
4801 debugfs_remove_recursive(dfs_parent);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004802}
4803
4804MODULE_AUTHOR("Micron Technology, Inc");
4805MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4806MODULE_LICENSE("GPL");
4807MODULE_VERSION(MTIP_DRV_VERSION);
4808
4809module_init(mtip_init);
4810module_exit(mtip_exit);