blob: d81d797ee65dd50de704286735b4592152aee4be [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
Christoph Hellwig6f3b0e82015-11-26 09:13:05 +0100198 rq = blk_mq_alloc_request(dd->queue, 0, 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
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200850 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) &&
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100851 (cmd != NULL) && !(readl(port->cmd_issue[MTIP_TAG_INTERNAL])
Sam Bradshaw88523a62011-08-30 08:34:26 -0600852 & (1 << MTIP_TAG_INTERNAL))) {
853 if (cmd->comp_func) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600854 cmd->comp_func(port, MTIP_TAG_INTERNAL, cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600855 return;
856 }
857 }
858
Sam Bradshaw88523a62011-08-30 08:34:26 -0600859 return;
860}
861
862/*
863 * Demux and handle errors
864 */
865static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
866{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600867
868 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
869 dev_warn(&dd->pdev->dev,
870 "Clearing PxSERR.DIAG.x\n");
871 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
872 }
873
874 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
875 dev_warn(&dd->pdev->dev,
876 "Clearing PxSERR.DIAG.n\n");
877 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
878 }
879
880 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
881 dev_warn(&dd->pdev->dev,
882 "Port stat errors %x unhandled\n",
883 (port_stat & ~PORT_IRQ_HANDLED));
Asai Thambi S P9b204fb2014-05-20 10:48:56 -0700884 if (mtip_check_surprise_removal(dd->pdev))
885 return;
886 }
887 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR))) {
888 set_bit(MTIP_PF_EH_ACTIVE_BIT, &dd->port->flags);
889 wake_up_interruptible(&dd->port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600890 }
891}
892
893static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
894{
895 struct driver_data *dd = (struct driver_data *) data;
896 struct mtip_port *port = dd->port;
897 u32 hba_stat, port_stat;
898 int rv = IRQ_NONE;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800899 int do_irq_enable = 1, i, workers;
900 struct mtip_work *twork;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600901
902 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
903 if (hba_stat) {
904 rv = IRQ_HANDLED;
905
906 /* Acknowledge the interrupt status on the port.*/
907 port_stat = readl(port->mmio + PORT_IRQ_STAT);
Asai Thambi SP2132a542015-05-11 15:53:18 -0700908 if (unlikely(port_stat == 0xFFFFFFFF)) {
909 mtip_check_surprise_removal(dd->pdev);
910 return IRQ_HANDLED;
911 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600912 writel(port_stat, port->mmio + PORT_IRQ_STAT);
913
914 /* Demux port status */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800915 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
916 do_irq_enable = 0;
917 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
918
919 /* Start at 1: group zero is always local? */
920 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
921 i++) {
922 twork = &dd->work[i];
923 twork->completed = readl(port->completed[i]);
924 if (twork->completed)
925 workers++;
926 }
927
928 atomic_set(&dd->irq_workers_active, workers);
929 if (workers) {
930 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
931 twork = &dd->work[i];
932 if (twork->completed)
933 queue_work_on(
934 twork->cpu_binding,
935 dd->isr_workq,
936 &twork->work);
937 }
938
939 if (likely(dd->work[0].completed))
940 mtip_workq_sdbfx(port, 0,
941 dd->work[0].completed);
942
943 } else {
944 /*
945 * Chip quirk: SDB interrupt but nothing
946 * to complete
947 */
948 do_irq_enable = 1;
949 }
950 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600951
952 if (unlikely(port_stat & PORT_IRQ_ERR)) {
953 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600954 /* don't proceed further */
955 return IRQ_HANDLED;
956 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200957 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +0200958 &dd->dd_flag))
959 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600960
961 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
962 }
963
964 if (unlikely(port_stat & PORT_IRQ_LEGACY))
965 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
966 }
967
968 /* acknowledge interrupt */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800969 if (unlikely(do_irq_enable))
970 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600971
972 return rv;
973}
974
975/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600976 * HBA interrupt subroutine.
977 *
978 * @irq IRQ number.
979 * @instance Pointer to the driver data structure.
980 *
981 * return value
982 * IRQ_HANDLED A HBA interrupt was pending and handled.
983 * IRQ_NONE This interrupt was not for the HBA.
984 */
985static irqreturn_t mtip_irq_handler(int irq, void *instance)
986{
987 struct driver_data *dd = instance;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800988
989 return mtip_handle_irq(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600990}
991
992static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
993{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600994 writel(1 << MTIP_TAG_BIT(tag),
995 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
996}
997
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200998static bool mtip_pause_ncq(struct mtip_port *port,
999 struct host_to_dev_fis *fis)
1000{
1001 struct host_to_dev_fis *reply;
1002 unsigned long task_file_data;
1003
1004 reply = port->rxfis + RX_FIS_D2H_REG;
1005 task_file_data = readl(port->mmio+PORT_TFDATA);
1006
Asai Thambi S P12a166c2012-09-05 22:01:36 +05301007 if ((task_file_data & 1))
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001008 return false;
1009
1010 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001011 port->ic_pause_timer = jiffies;
1012 return true;
1013 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
1014 (fis->features == 0x03)) {
1015 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
1016 port->ic_pause_timer = jiffies;
1017 return true;
1018 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
1019 ((fis->command == 0xFC) &&
1020 (fis->features == 0x27 || fis->features == 0x72 ||
1021 fis->features == 0x62 || fis->features == 0x26))) {
Asai Thambi SPee04bed2015-05-11 15:50:50 -07001022 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
Asai Thambi SPaae4a032016-02-24 21:18:20 -08001023 clear_bit(MTIP_DDF_REBUILD_FAILED_BIT, &port->dd->dd_flag);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001024 /* Com reset after secure erase or lowlevel format */
1025 mtip_restart_port(port);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001026 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001027 return false;
1028 }
1029
1030 return false;
1031}
1032
Sam Bradshaw88523a62011-08-30 08:34:26 -06001033/*
1034 * Wait for port to quiesce
1035 *
1036 * @port Pointer to port data structure
1037 * @timeout Max duration to wait (ms)
Asai Thambi SP008e56d2016-02-24 21:21:20 -08001038 * @atomic gfp_t flag to indicate blockable context or not
Sam Bradshaw88523a62011-08-30 08:34:26 -06001039 *
1040 * return value
1041 * 0 Success
1042 * -EBUSY Commands still active
1043 */
Asai Thambi SP008e56d2016-02-24 21:21:20 -08001044static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout,
1045 gfp_t atomic)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001046{
1047 unsigned long to;
Dan Carpenter3e54a3d2011-11-24 12:59:00 +01001048 unsigned int n;
1049 unsigned int active = 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001050
Jens Axboe9acf03c2014-05-14 08:22:56 -06001051 blk_mq_stop_hw_queues(port->dd->queue);
1052
Sam Bradshaw88523a62011-08-30 08:34:26 -06001053 to = jiffies + msecs_to_jiffies(timeout);
1054 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001055 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
Asai Thambi SP008e56d2016-02-24 21:21:20 -08001056 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags) &&
1057 atomic == GFP_KERNEL) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001058 msleep(20);
1059 continue; /* svc thd is actively issuing commands */
1060 }
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001061
Asai Thambi SP008e56d2016-02-24 21:21:20 -08001062 if (atomic == GFP_KERNEL)
1063 msleep(100);
1064 else {
1065 cpu_relax();
1066 udelay(100);
1067 }
1068
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001069 if (mtip_check_surprise_removal(port->dd->pdev))
1070 goto err_fault;
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001071
Sam Bradshaw88523a62011-08-30 08:34:26 -06001072 /*
1073 * Ignore s_active bit 0 of array element 0.
1074 * This bit will always be set
1075 */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001076 active = readl(port->s_active[0]) & 0xFFFFFFFE;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001077 for (n = 1; n < port->dd->slot_groups; n++)
1078 active |= readl(port->s_active[n]);
1079
1080 if (!active)
1081 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001082 } while (time_before(jiffies, to));
1083
Jens Axboe9acf03c2014-05-14 08:22:56 -06001084 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001085 return active ? -EBUSY : 0;
Jens Axboe9acf03c2014-05-14 08:22:56 -06001086err_fault:
1087 blk_mq_start_stopped_hw_queues(port->dd->queue, true);
1088 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001089}
1090
1091/*
1092 * Execute an internal command and wait for the completion.
1093 *
1094 * @port Pointer to the port data structure.
1095 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001096 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001097 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001098 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -06001099 * @opts Command header options, excluding the FIS length
1100 * and the number of PRD entries.
1101 * @timeout Time in ms to wait for the command to complete.
1102 *
1103 * return value
1104 * 0 Command completed successfully.
1105 * -EFAULT The buffer address is not correctly aligned.
1106 * -EBUSY Internal command or other IO in progress.
1107 * -EAGAIN Time out waiting for command to complete.
1108 */
1109static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001110 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001111 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001112 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001113 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001114 u32 opts,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001115 unsigned long timeout)
1116{
1117 struct mtip_cmd_sg *command_sg;
1118 DECLARE_COMPLETION_ONSTACK(wait);
Jens Axboeffc771b2014-05-09 09:42:02 -06001119 struct mtip_cmd *int_cmd;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301120 struct driver_data *dd = port->dd;
Jens Axboeffc771b2014-05-09 09:42:02 -06001121 int rv = 0;
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001122 unsigned long start;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001123
1124 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1125 if (buffer & 0x00000007) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301126 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001127 return -EFAULT;
1128 }
1129
Jens Axboeffc771b2014-05-09 09:42:02 -06001130 int_cmd = mtip_get_int_command(dd);
Asai Thambi SP008e56d2016-02-24 21:21:20 -08001131 if (!int_cmd) {
1132 dbg_printk(MTIP_DRV_NAME "Unable to allocate tag for PIO cmd\n");
1133 return -EFAULT;
1134 }
Jens Axboeffc771b2014-05-09 09:42:02 -06001135
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001136 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001137
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001138 if (fis->command == ATA_CMD_SEC_ERASE_PREP)
1139 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1140
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301141 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001142
Jens Axboe0f6422a2017-04-27 16:29:26 -06001143 if (fis->command != ATA_CMD_STANDBYNOW1) {
1144 /* wait for io to complete if non atomic */
1145 if (mtip_quiesce_io(port,
1146 MTIP_QUIESCE_IO_TIMEOUT_MS, GFP_KERNEL) < 0) {
1147 dev_warn(&dd->pdev->dev, "Failed to quiesce IO\n");
1148 mtip_put_int_command(dd, int_cmd);
1149 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1150 wake_up_interruptible(&port->svc_wait);
1151 return -EBUSY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001152 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001153 }
1154
Jens Axboe0f6422a2017-04-27 16:29:26 -06001155 /* Set the completion function and data for the command. */
1156 int_cmd->comp_data = &wait;
1157 int_cmd->comp_func = mtip_completion;
1158
Sam Bradshaw88523a62011-08-30 08:34:26 -06001159 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001160 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001161
1162 /* Populate the SG list */
1163 int_cmd->command_header->opts =
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001164 __force_bit2int cpu_to_le32(opts | fis_len);
1165 if (buf_len) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001166 command_sg = int_cmd->command + AHCI_CMD_TBL_HDR_SZ;
1167
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001168 command_sg->info =
1169 __force_bit2int cpu_to_le32((buf_len-1) & 0x3FFFFF);
1170 command_sg->dba =
1171 __force_bit2int cpu_to_le32(buffer & 0xFFFFFFFF);
1172 command_sg->dba_upper =
1173 __force_bit2int cpu_to_le32((buffer >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001174
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001175 int_cmd->command_header->opts |=
1176 __force_bit2int cpu_to_le32((1 << 16));
Sam Bradshaw88523a62011-08-30 08:34:26 -06001177 }
1178
1179 /* Populate the command header */
1180 int_cmd->command_header->byte_count = 0;
1181
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001182 start = jiffies;
1183
Sam Bradshaw88523a62011-08-30 08:34:26 -06001184 /* Issue the command to the hardware */
1185 mtip_issue_non_ncq_command(port, MTIP_TAG_INTERNAL);
1186
Jens Axboe0f6422a2017-04-27 16:29:26 -06001187 /* Wait for the command to complete or timeout. */
1188 rv = wait_for_completion_interruptible_timeout(&wait,
1189 msecs_to_jiffies(timeout));
1190 if (rv <= 0) {
1191 if (rv == -ERESTARTSYS) { /* interrupted */
1192 dev_err(&dd->pdev->dev,
1193 "Internal command [%02X] was interrupted after %u ms\n",
1194 fis->command,
1195 jiffies_to_msecs(jiffies - start));
1196 rv = -EINTR;
1197 goto exec_ic_exit;
1198 } else if (rv == 0) /* timeout */
1199 dev_err(&dd->pdev->dev,
1200 "Internal command did not complete [%02X] within timeout of %lu ms\n",
1201 fis->command, timeout);
1202 else
1203 dev_err(&dd->pdev->dev,
1204 "Internal command [%02X] wait returned code [%d] after %lu ms - unhandled\n",
1205 fis->command, rv, timeout);
Asai Thambi SPaae4a032016-02-24 21:18:20 -08001206
Jens Axboe0f6422a2017-04-27 16:29:26 -06001207 if (mtip_check_surprise_removal(dd->pdev) ||
1208 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1209 &dd->dd_flag)) {
1210 dev_err(&dd->pdev->dev,
1211 "Internal command [%02X] wait returned due to SR\n",
1212 fis->command);
1213 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301214 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001215 }
Jens Axboe0f6422a2017-04-27 16:29:26 -06001216 mtip_device_reset(dd); /* recover from timeout issue */
1217 rv = -EAGAIN;
1218 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001219 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001220
Jens Axboe0f6422a2017-04-27 16:29:26 -06001221 rv = 0;
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001222 if (readl(port->cmd_issue[MTIP_TAG_INTERNAL])
1223 & (1 << MTIP_TAG_INTERNAL)) {
1224 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301225 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1226 mtip_device_reset(dd);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001227 rv = -EAGAIN;
1228 }
1229 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001230exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001231 /* Clear the allocated and active bits for the internal command. */
Jens Axboeffc771b2014-05-09 09:42:02 -06001232 mtip_put_int_command(dd, int_cmd);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001233 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001234 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1235 /* NCQ paused */
1236 return rv;
1237 }
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001238 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001239
1240 return rv;
1241}
1242
1243/*
1244 * Byte-swap ATA ID strings.
1245 *
1246 * ATA identify data contains strings in byte-swapped 16-bit words.
1247 * They must be swapped (on all architectures) to be usable as C strings.
1248 * This function swaps bytes in-place.
1249 *
1250 * @buf The buffer location of the string
1251 * @len The number of bytes to swap
1252 *
1253 * return value
1254 * None
1255 */
1256static inline void ata_swap_string(u16 *buf, unsigned int len)
1257{
1258 int i;
1259 for (i = 0; i < (len/2); i++)
1260 be16_to_cpus(&buf[i]);
1261}
1262
Asai Thambi S P670a6412014-04-16 20:27:54 -07001263static void mtip_set_timeout(struct driver_data *dd,
1264 struct host_to_dev_fis *fis,
1265 unsigned int *timeout, u8 erasemode)
1266{
1267 switch (fis->command) {
1268 case ATA_CMD_DOWNLOAD_MICRO:
1269 *timeout = 120000; /* 2 minutes */
1270 break;
1271 case ATA_CMD_SEC_ERASE_UNIT:
1272 case 0xFC:
1273 if (erasemode)
1274 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
1275 else
1276 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
1277 break;
1278 case ATA_CMD_STANDBYNOW1:
1279 *timeout = 120000; /* 2 minutes */
1280 break;
1281 case 0xF7:
1282 case 0xFA:
1283 *timeout = 60000; /* 60 seconds */
1284 break;
1285 case ATA_CMD_SMART:
1286 *timeout = 15000; /* 15 seconds */
1287 break;
1288 default:
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001289 *timeout = MTIP_IOCTL_CMD_TIMEOUT_MS;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001290 break;
1291 }
1292}
1293
Sam Bradshaw88523a62011-08-30 08:34:26 -06001294/*
1295 * Request the device identity information.
1296 *
1297 * If a user space buffer is not specified, i.e. is NULL, the
1298 * identify information is still read from the drive and placed
1299 * into the identify data buffer (@e port->identify) in the
1300 * port data structure.
1301 * When the identify buffer contains valid identify information @e
1302 * port->identify_valid is non-zero.
1303 *
1304 * @port Pointer to the port structure.
1305 * @user_buffer A user space buffer where the identify data should be
1306 * copied.
1307 *
1308 * return value
1309 * 0 Command completed successfully.
1310 * -EFAULT An error occurred while coping data to the user buffer.
1311 * -1 Command failed.
1312 */
1313static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1314{
1315 int rv = 0;
1316 struct host_to_dev_fis fis;
1317
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001318 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001319 return -EFAULT;
1320
Sam Bradshaw88523a62011-08-30 08:34:26 -06001321 /* Build the FIS. */
1322 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1323 fis.type = 0x27;
1324 fis.opts = 1 << 7;
1325 fis.command = ATA_CMD_ID_ATA;
1326
1327 /* Set the identify information as invalid. */
1328 port->identify_valid = 0;
1329
1330 /* Clear the identify information. */
1331 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1332
1333 /* Execute the command. */
1334 if (mtip_exec_internal_command(port,
1335 &fis,
1336 5,
1337 port->identify_dma,
1338 sizeof(u16) * ATA_ID_WORDS,
1339 0,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001340 MTIP_INT_CMD_TIMEOUT_MS)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001341 < 0) {
1342 rv = -1;
1343 goto out;
1344 }
1345
1346 /*
1347 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1348 * perform field-sensitive swapping on the string fields.
1349 * See the kernel use of ata_id_string() for proof of this.
1350 */
1351#ifdef __LITTLE_ENDIAN
1352 ata_swap_string(port->identify + 27, 40); /* model string*/
1353 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1354 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1355#else
1356 {
1357 int i;
1358 for (i = 0; i < ATA_ID_WORDS; i++)
1359 port->identify[i] = le16_to_cpu(port->identify[i]);
1360 }
1361#endif
1362
Sam Bradshaw26d58052013-10-03 10:18:05 -07001363 /* Check security locked state */
1364 if (port->identify[128] & 0x4)
1365 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1366 else
1367 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1368
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301369#ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
Asai Thambi S P15283462013-01-11 14:41:34 +01001370 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1371 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1372 port->dd->trim_supp = true;
1373 else
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301374#endif
Asai Thambi S P15283462013-01-11 14:41:34 +01001375 port->dd->trim_supp = false;
1376
Sam Bradshaw88523a62011-08-30 08:34:26 -06001377 /* Set the identify buffer as valid. */
1378 port->identify_valid = 1;
1379
1380 if (user_buffer) {
1381 if (copy_to_user(
1382 user_buffer,
1383 port->identify,
1384 ATA_ID_WORDS * sizeof(u16))) {
1385 rv = -EFAULT;
1386 goto out;
1387 }
1388 }
1389
1390out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001391 return rv;
1392}
1393
1394/*
1395 * Issue a standby immediate command to the device.
1396 *
1397 * @port Pointer to the port structure.
1398 *
1399 * return value
1400 * 0 Command was executed successfully.
1401 * -1 An error occurred while executing the command.
1402 */
1403static int mtip_standby_immediate(struct mtip_port *port)
1404{
1405 int rv;
1406 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001407 unsigned long start;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001408 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001409
Sam Bradshaw88523a62011-08-30 08:34:26 -06001410 /* Build the FIS. */
1411 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1412 fis.type = 0x27;
1413 fis.opts = 1 << 7;
1414 fis.command = ATA_CMD_STANDBYNOW1;
1415
Asai Thambi S P670a6412014-04-16 20:27:54 -07001416 mtip_set_timeout(port->dd, &fis, &timeout, 0);
1417
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001418 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001419 rv = mtip_exec_internal_command(port,
1420 &fis,
1421 5,
1422 0,
1423 0,
1424 0,
Asai Thambi S P670a6412014-04-16 20:27:54 -07001425 timeout);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001426 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1427 jiffies_to_msecs(jiffies - start));
1428 if (rv)
1429 dev_warn(&port->dd->pdev->dev,
1430 "STANDBY IMMEDIATE command failed.\n");
1431
1432 return rv;
1433}
1434
1435/*
1436 * Issue a READ LOG EXT command to the device.
1437 *
1438 * @port pointer to the port structure.
1439 * @page page number to fetch
1440 * @buffer pointer to buffer
1441 * @buffer_dma dma address corresponding to @buffer
1442 * @sectors page length to fetch, in sectors
1443 *
1444 * return value
1445 * @rv return value from mtip_exec_internal_command()
1446 */
1447static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1448 dma_addr_t buffer_dma, unsigned int sectors)
1449{
1450 struct host_to_dev_fis fis;
1451
1452 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1453 fis.type = 0x27;
1454 fis.opts = 1 << 7;
1455 fis.command = ATA_CMD_READ_LOG_EXT;
1456 fis.sect_count = sectors & 0xFF;
1457 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1458 fis.lba_low = page;
1459 fis.lba_mid = 0;
1460 fis.device = ATA_DEVICE_OBS;
1461
1462 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1463
1464 return mtip_exec_internal_command(port,
1465 &fis,
1466 5,
1467 buffer_dma,
1468 sectors * ATA_SECT_SIZE,
1469 0,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001470 MTIP_INT_CMD_TIMEOUT_MS);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001471}
1472
1473/*
1474 * Issue a SMART READ DATA command to the device.
1475 *
1476 * @port pointer to the port structure.
1477 * @buffer pointer to buffer
1478 * @buffer_dma dma address corresponding to @buffer
1479 *
1480 * return value
1481 * @rv return value from mtip_exec_internal_command()
1482 */
1483static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1484 dma_addr_t buffer_dma)
1485{
1486 struct host_to_dev_fis fis;
1487
1488 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1489 fis.type = 0x27;
1490 fis.opts = 1 << 7;
1491 fis.command = ATA_CMD_SMART;
1492 fis.features = 0xD0;
1493 fis.sect_count = 1;
1494 fis.lba_mid = 0x4F;
1495 fis.lba_hi = 0xC2;
1496 fis.device = ATA_DEVICE_OBS;
1497
1498 return mtip_exec_internal_command(port,
1499 &fis,
1500 5,
1501 buffer_dma,
1502 ATA_SECT_SIZE,
1503 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001504 15000);
1505}
1506
1507/*
1508 * Get the value of a smart attribute
1509 *
1510 * @port pointer to the port structure
1511 * @id attribute number
1512 * @attrib pointer to return attrib information corresponding to @id
1513 *
1514 * return value
1515 * -EINVAL NULL buffer passed or unsupported attribute @id.
1516 * -EPERM Identify data not valid, SMART not supported or not enabled
1517 */
1518static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1519 struct smart_attr *attrib)
1520{
1521 int rv, i;
1522 struct smart_attr *pattr;
1523
1524 if (!attrib)
1525 return -EINVAL;
1526
1527 if (!port->identify_valid) {
1528 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1529 return -EPERM;
1530 }
1531 if (!(port->identify[82] & 0x1)) {
1532 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1533 return -EPERM;
1534 }
1535 if (!(port->identify[85] & 0x1)) {
1536 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1537 return -EPERM;
1538 }
1539
1540 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1541 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1542 if (rv) {
1543 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1544 return rv;
1545 }
1546
1547 pattr = (struct smart_attr *)(port->smart_buf + 2);
1548 for (i = 0; i < 29; i++, pattr++)
1549 if (pattr->attr_id == id) {
1550 memcpy(attrib, pattr, sizeof(struct smart_attr));
1551 break;
1552 }
1553
1554 if (i == 29) {
1555 dev_warn(&port->dd->pdev->dev,
1556 "Query for invalid SMART attribute ID\n");
1557 rv = -EINVAL;
1558 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001559
Sam Bradshaw88523a62011-08-30 08:34:26 -06001560 return rv;
1561}
1562
1563/*
Asai Thambi S P15283462013-01-11 14:41:34 +01001564 * Trim unused sectors
1565 *
1566 * @dd pointer to driver_data structure
1567 * @lba starting lba
1568 * @len # of 512b sectors to trim
1569 *
1570 * return value
1571 * -ENOMEM Out of dma memory
1572 * -EINVAL Invalid parameters passed in, trim not supported
1573 * -EIO Error submitting trim request to hw
1574 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301575static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
1576 unsigned int len)
Asai Thambi S P15283462013-01-11 14:41:34 +01001577{
1578 int i, rv = 0;
1579 u64 tlba, tlen, sect_left;
1580 struct mtip_trim_entry *buf;
1581 dma_addr_t dma_addr;
1582 struct host_to_dev_fis fis;
1583
1584 if (!len || dd->trim_supp == false)
1585 return -EINVAL;
1586
1587 /* Trim request too big */
1588 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1589
1590 /* Trim request not aligned on 4k boundary */
1591 WARN_ON(len % 8 != 0);
1592
1593 /* Warn if vu_trim structure is too big */
1594 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1595
1596 /* Allocate a DMA buffer for the trim structure */
1597 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1598 GFP_KERNEL);
1599 if (!buf)
1600 return -ENOMEM;
1601 memset(buf, 0, ATA_SECT_SIZE);
1602
1603 for (i = 0, sect_left = len, tlba = lba;
1604 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1605 i++) {
1606 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1607 MTIP_MAX_TRIM_ENTRY_LEN :
1608 sect_left);
1609 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1610 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1611 tlba += tlen;
1612 sect_left -= tlen;
1613 }
1614 WARN_ON(sect_left != 0);
1615
1616 /* Build the fis */
1617 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1618 fis.type = 0x27;
1619 fis.opts = 1 << 7;
1620 fis.command = 0xfb;
1621 fis.features = 0x60;
1622 fis.sect_count = 1;
1623 fis.device = ATA_DEVICE_OBS;
1624
1625 if (mtip_exec_internal_command(dd->port,
1626 &fis,
1627 5,
1628 dma_addr,
1629 ATA_SECT_SIZE,
1630 0,
Asai Thambi S P15283462013-01-11 14:41:34 +01001631 MTIP_TRIM_TIMEOUT_MS) < 0)
1632 rv = -EIO;
1633
1634 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1635 return rv;
1636}
1637
1638/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001639 * Get the drive capacity.
1640 *
1641 * @dd Pointer to the device data structure.
1642 * @sectors Pointer to the variable that will receive the sector count.
1643 *
1644 * return value
1645 * 1 Capacity was returned successfully.
1646 * 0 The identify information is invalid.
1647 */
Jens Axboe63166682011-09-27 21:27:43 -06001648static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001649{
1650 struct mtip_port *port = dd->port;
1651 u64 total, raw0, raw1, raw2, raw3;
1652 raw0 = port->identify[100];
1653 raw1 = port->identify[101];
1654 raw2 = port->identify[102];
1655 raw3 = port->identify[103];
1656 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1657 *sectors = total;
1658 return (bool) !!port->identify_valid;
1659}
1660
1661/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001662 * Display the identify command data.
1663 *
1664 * @port Pointer to the port data structure.
1665 *
1666 * return value
1667 * None
1668 */
1669static void mtip_dump_identify(struct mtip_port *port)
1670{
1671 sector_t sectors;
1672 unsigned short revid;
1673 char cbuf[42];
1674
1675 if (!port->identify_valid)
1676 return;
1677
1678 strlcpy(cbuf, (char *)(port->identify+10), 21);
1679 dev_info(&port->dd->pdev->dev,
1680 "Serial No.: %s\n", cbuf);
1681
1682 strlcpy(cbuf, (char *)(port->identify+23), 9);
1683 dev_info(&port->dd->pdev->dev,
1684 "Firmware Ver.: %s\n", cbuf);
1685
1686 strlcpy(cbuf, (char *)(port->identify+27), 41);
1687 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1688
Sam Bradshaw26d58052013-10-03 10:18:05 -07001689 dev_info(&port->dd->pdev->dev, "Security: %04x %s\n",
1690 port->identify[128],
1691 port->identify[128] & 0x4 ? "(LOCKED)" : "");
1692
Sam Bradshaw88523a62011-08-30 08:34:26 -06001693 if (mtip_hw_get_capacity(port->dd, &sectors))
1694 dev_info(&port->dd->pdev->dev,
1695 "Capacity: %llu sectors (%llu MB)\n",
1696 (u64)sectors,
1697 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1698
1699 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001700 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001701 case 0x1:
1702 strlcpy(cbuf, "A0", 3);
1703 break;
1704 case 0x3:
1705 strlcpy(cbuf, "A2", 3);
1706 break;
1707 default:
1708 strlcpy(cbuf, "?", 2);
1709 break;
1710 }
1711 dev_info(&port->dd->pdev->dev,
1712 "Card Type: %s\n", cbuf);
1713}
1714
1715/*
1716 * Map the commands scatter list into the command table.
1717 *
1718 * @command Pointer to the command.
1719 * @nents Number of scatter list entries.
1720 *
1721 * return value
1722 * None
1723 */
1724static inline void fill_command_sg(struct driver_data *dd,
1725 struct mtip_cmd *command,
1726 int nents)
1727{
1728 int n;
1729 unsigned int dma_len;
1730 struct mtip_cmd_sg *command_sg;
1731 struct scatterlist *sg = command->sg;
1732
1733 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1734
1735 for (n = 0; n < nents; n++) {
1736 dma_len = sg_dma_len(sg);
1737 if (dma_len > 0x400000)
1738 dev_err(&dd->pdev->dev,
1739 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001740 command_sg->info = __force_bit2int
1741 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1742 command_sg->dba = __force_bit2int
1743 cpu_to_le32(sg_dma_address(sg));
1744 command_sg->dba_upper = __force_bit2int
1745 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001746 command_sg++;
1747 sg++;
1748 }
1749}
1750
1751/*
1752 * @brief Execute a drive command.
1753 *
1754 * return value 0 The command completed successfully.
1755 * return value -1 An error occurred while executing the command.
1756 */
Jens Axboe63166682011-09-27 21:27:43 -06001757static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001758{
1759 struct host_to_dev_fis fis;
1760 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001761 unsigned int to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001762
Sam Bradshaw88523a62011-08-30 08:34:26 -06001763 /* Build the FIS. */
1764 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1765 fis.type = 0x27;
1766 fis.opts = 1 << 7;
1767 fis.command = command[0];
1768 fis.features = command[1];
1769 fis.sect_count = command[2];
1770 fis.sector = command[3];
1771 fis.cyl_low = command[4];
1772 fis.cyl_hi = command[5];
1773 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1774
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001775 mtip_set_timeout(port->dd, &fis, &to, 0);
1776
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001777 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 -06001778 __func__,
1779 command[0],
1780 command[1],
1781 command[2],
1782 command[3],
1783 command[4],
1784 command[5],
1785 command[6]);
1786
1787 /* Execute the command. */
1788 if (mtip_exec_internal_command(port,
1789 &fis,
1790 5,
1791 0,
1792 0,
1793 0,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001794 to) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001795 return -1;
1796 }
1797
1798 command[0] = reply->command; /* Status*/
1799 command[1] = reply->features; /* Error*/
1800 command[4] = reply->cyl_low;
1801 command[5] = reply->cyl_hi;
1802
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001803 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 -06001804 __func__,
1805 command[0],
1806 command[1],
1807 command[4],
1808 command[5]);
1809
Sam Bradshaw88523a62011-08-30 08:34:26 -06001810 return 0;
1811}
1812
1813/*
1814 * @brief Execute a drive command.
1815 *
1816 * @param port Pointer to the port data structure.
1817 * @param command Pointer to the user specified command parameters.
1818 * @param user_buffer Pointer to the user space buffer where read sector
1819 * data should be copied.
1820 *
1821 * return value 0 The command completed successfully.
1822 * return value -EFAULT An error occurred while copying the completion
1823 * data to the user space buffer.
1824 * return value -1 An error occurred while executing the command.
1825 */
Jens Axboe63166682011-09-27 21:27:43 -06001826static int exec_drive_command(struct mtip_port *port, u8 *command,
1827 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001828{
1829 struct host_to_dev_fis fis;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001830 struct host_to_dev_fis *reply;
1831 u8 *buf = NULL;
1832 dma_addr_t dma_addr = 0;
1833 int rv = 0, xfer_sz = command[3];
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001834 unsigned int to;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001835
1836 if (xfer_sz) {
David Milburn97651ea2012-09-12 14:06:12 -05001837 if (!user_buffer)
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001838 return -EFAULT;
1839
1840 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1841 ATA_SECT_SIZE * xfer_sz,
1842 &dma_addr,
1843 GFP_KERNEL);
1844 if (!buf) {
1845 dev_err(&port->dd->pdev->dev,
1846 "Memory allocation failed (%d bytes)\n",
1847 ATA_SECT_SIZE * xfer_sz);
1848 return -ENOMEM;
1849 }
1850 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1851 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001852
Sam Bradshaw88523a62011-08-30 08:34:26 -06001853 /* Build the FIS. */
1854 memset(&fis, 0, sizeof(struct host_to_dev_fis));
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001855 fis.type = 0x27;
1856 fis.opts = 1 << 7;
1857 fis.command = command[0];
Sam Bradshaw88523a62011-08-30 08:34:26 -06001858 fis.features = command[2];
1859 fis.sect_count = command[3];
1860 if (fis.command == ATA_CMD_SMART) {
1861 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001862 fis.cyl_low = 0x4F;
1863 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001864 }
1865
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001866 mtip_set_timeout(port->dd, &fis, &to, 0);
1867
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001868 if (xfer_sz)
1869 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1870 else
1871 reply = (port->rxfis + RX_FIS_D2H_REG);
1872
Sam Bradshaw88523a62011-08-30 08:34:26 -06001873 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001874 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06001875 "feat %x, sectcnt %x\n",
1876 __func__,
1877 command[0],
1878 command[1],
1879 command[2],
1880 command[3]);
1881
Sam Bradshaw88523a62011-08-30 08:34:26 -06001882 /* Execute the command. */
1883 if (mtip_exec_internal_command(port,
1884 &fis,
1885 5,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001886 (xfer_sz ? dma_addr : 0),
1887 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
Sam Bradshaw88523a62011-08-30 08:34:26 -06001888 0,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001889 to)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001890 < 0) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001891 rv = -EFAULT;
1892 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001893 }
1894
1895 /* Collect the completion status. */
1896 command[0] = reply->command; /* Status*/
1897 command[1] = reply->features; /* Error*/
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001898 command[2] = reply->sect_count;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001899
1900 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001901 " %s: Completion Status: stat %x, "
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001902 "err %x, nsect %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001903 __func__,
1904 command[0],
1905 command[1],
1906 command[2]);
1907
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001908 if (xfer_sz) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001909 if (copy_to_user(user_buffer,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001910 buf,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001911 ATA_SECT_SIZE * command[3])) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001912 rv = -EFAULT;
1913 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001914 }
1915 }
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001916exit_drive_command:
1917 if (buf)
1918 dmam_free_coherent(&port->dd->pdev->dev,
1919 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1920 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001921}
1922
1923/*
1924 * Indicates whether a command has a single sector payload.
1925 *
1926 * @command passed to the device to perform the certain event.
1927 * @features passed to the device to perform the certain event.
1928 *
1929 * return value
1930 * 1 command is one that always has a single sector payload,
1931 * regardless of the value in the Sector Count field.
1932 * 0 otherwise
1933 *
1934 */
1935static unsigned int implicit_sector(unsigned char command,
1936 unsigned char features)
1937{
1938 unsigned int rv = 0;
1939
1940 /* list of commands that have an implicit sector count of 1 */
1941 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001942 case ATA_CMD_SEC_SET_PASS:
1943 case ATA_CMD_SEC_UNLOCK:
1944 case ATA_CMD_SEC_ERASE_PREP:
1945 case ATA_CMD_SEC_ERASE_UNIT:
1946 case ATA_CMD_SEC_FREEZE_LOCK:
1947 case ATA_CMD_SEC_DISABLE_PASS:
1948 case ATA_CMD_PMP_READ:
1949 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001950 rv = 1;
1951 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001952 case ATA_CMD_SET_MAX:
1953 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001954 rv = 1;
1955 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001956 case ATA_CMD_SMART:
1957 if ((features == ATA_SMART_READ_VALUES) ||
1958 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001959 rv = 1;
1960 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001961 case ATA_CMD_CONF_OVERLAY:
1962 if ((features == ATA_DCO_IDENTIFY) ||
1963 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001964 rv = 1;
1965 break;
1966 }
1967 return rv;
1968}
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07001969
Sam Bradshaw88523a62011-08-30 08:34:26 -06001970/*
1971 * Executes a taskfile
1972 * See ide_taskfile_ioctl() for derivation
1973 */
1974static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06001975 void __user *buf,
1976 ide_task_request_t *req_task,
1977 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001978{
1979 struct host_to_dev_fis fis;
1980 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001981 u8 *outbuf = NULL;
1982 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06001983 dma_addr_t outbuf_dma = 0;
1984 dma_addr_t inbuf_dma = 0;
1985 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001986 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001987 unsigned int taskin = 0;
1988 unsigned int taskout = 0;
1989 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07001990 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001991 unsigned int force_single_sector;
1992 unsigned int transfer_size;
1993 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06001994 int intotal = outtotal + req_task->out_size;
Selvan Mani4453bc82012-09-27 14:36:43 +02001995 int erasemode = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001996
1997 taskout = req_task->out_size;
1998 taskin = req_task->in_size;
1999 /* 130560 = 512 * 0xFF*/
Sachin Shuklab425b022016-11-11 14:34:51 +05302000 if (taskin > 130560 || taskout > 130560)
2001 return -EINVAL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002002
2003 if (taskout) {
Al Viro8ed60102016-01-02 14:56:33 -05002004 outbuf = memdup_user(buf + outtotal, taskout);
Sachin Shuklab425b022016-11-11 14:34:51 +05302005 if (IS_ERR(outbuf))
2006 return PTR_ERR(outbuf);
2007
Sam Bradshaw88523a62011-08-30 08:34:26 -06002008 outbuf_dma = pci_map_single(dd->pdev,
2009 outbuf,
2010 taskout,
2011 DMA_TO_DEVICE);
Alexey Khoroshilov5173cb82016-03-19 01:35:54 +03002012 if (pci_dma_mapping_error(dd->pdev, outbuf_dma)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002013 err = -ENOMEM;
2014 goto abort;
2015 }
2016 dma_buffer = outbuf_dma;
2017 }
2018
2019 if (taskin) {
Al Viro8ed60102016-01-02 14:56:33 -05002020 inbuf = memdup_user(buf + intotal, taskin);
2021 if (IS_ERR(inbuf)) {
2022 err = PTR_ERR(inbuf);
2023 inbuf = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002024 goto abort;
2025 }
2026 inbuf_dma = pci_map_single(dd->pdev,
2027 inbuf,
2028 taskin, DMA_FROM_DEVICE);
Alexey Khoroshilov5173cb82016-03-19 01:35:54 +03002029 if (pci_dma_mapping_error(dd->pdev, inbuf_dma)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002030 err = -ENOMEM;
2031 goto abort;
2032 }
2033 dma_buffer = inbuf_dma;
2034 }
2035
2036 /* only supports PIO and non-data commands from this ioctl. */
2037 switch (req_task->data_phase) {
2038 case TASKFILE_OUT:
2039 nsect = taskout / ATA_SECT_SIZE;
2040 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2041 break;
2042 case TASKFILE_IN:
2043 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
2044 break;
2045 case TASKFILE_NO_DATA:
2046 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
2047 break;
2048 default:
2049 err = -EINVAL;
2050 goto abort;
2051 }
2052
Sam Bradshaw88523a62011-08-30 08:34:26 -06002053 /* Build the FIS. */
2054 memset(&fis, 0, sizeof(struct host_to_dev_fis));
2055
2056 fis.type = 0x27;
2057 fis.opts = 1 << 7;
2058 fis.command = req_task->io_ports[7];
2059 fis.features = req_task->io_ports[1];
2060 fis.sect_count = req_task->io_ports[2];
2061 fis.lba_low = req_task->io_ports[3];
2062 fis.lba_mid = req_task->io_ports[4];
2063 fis.lba_hi = req_task->io_ports[5];
2064 /* Clear the dev bit*/
2065 fis.device = req_task->io_ports[6] & ~0x10;
2066
2067 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
2068 req_task->in_flags.all =
2069 IDE_TASKFILE_STD_IN_FLAGS |
2070 (IDE_HOB_STD_IN_FLAGS << 8);
2071 fis.lba_low_ex = req_task->hob_ports[3];
2072 fis.lba_mid_ex = req_task->hob_ports[4];
2073 fis.lba_hi_ex = req_task->hob_ports[5];
2074 fis.features_ex = req_task->hob_ports[1];
2075 fis.sect_cnt_ex = req_task->hob_ports[2];
2076
2077 } else {
2078 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
2079 }
2080
2081 force_single_sector = implicit_sector(fis.command, fis.features);
2082
2083 if ((taskin || taskout) && (!fis.sect_count)) {
2084 if (nsect)
2085 fis.sect_count = nsect;
2086 else {
2087 if (!force_single_sector) {
2088 dev_warn(&dd->pdev->dev,
2089 "data movement but "
2090 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002091 err = -EINVAL;
2092 goto abort;
2093 }
2094 }
2095 }
2096
2097 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002098 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002099 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
2100 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002101 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002102 fis.command,
2103 fis.features,
2104 fis.sect_count,
2105 fis.lba_low,
2106 fis.lba_mid,
2107 fis.lba_hi,
2108 fis.device);
2109
Selvan Mani4453bc82012-09-27 14:36:43 +02002110 /* check for erase mode support during secure erase.*/
Selvan Mani32087952012-11-07 06:03:37 -07002111 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
2112 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
Selvan Mani4453bc82012-09-27 14:36:43 +02002113 erasemode = 1;
2114 }
2115
2116 mtip_set_timeout(dd, &fis, &timeout, erasemode);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002117
2118 /* Determine the correct transfer size.*/
2119 if (force_single_sector)
2120 transfer_size = ATA_SECT_SIZE;
2121 else
2122 transfer_size = ATA_SECT_SIZE * fis.sect_count;
2123
2124 /* Execute the command.*/
2125 if (mtip_exec_internal_command(dd->port,
2126 &fis,
2127 5,
2128 dma_buffer,
2129 transfer_size,
2130 0,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002131 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06002132 err = -EIO;
2133 goto abort;
2134 }
2135
2136 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
2137
2138 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
2139 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
2140 req_task->io_ports[7] = reply->control;
2141 } else {
2142 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2143 req_task->io_ports[7] = reply->command;
2144 }
2145
2146 /* reclaim the DMA buffers.*/
2147 if (inbuf_dma)
2148 pci_unmap_single(dd->pdev, inbuf_dma,
2149 taskin, DMA_FROM_DEVICE);
2150 if (outbuf_dma)
2151 pci_unmap_single(dd->pdev, outbuf_dma,
2152 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002153 inbuf_dma = 0;
2154 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002155
2156 /* return the ATA registers to the caller.*/
2157 req_task->io_ports[1] = reply->features;
2158 req_task->io_ports[2] = reply->sect_count;
2159 req_task->io_ports[3] = reply->lba_low;
2160 req_task->io_ports[4] = reply->lba_mid;
2161 req_task->io_ports[5] = reply->lba_hi;
2162 req_task->io_ports[6] = reply->device;
2163
2164 if (req_task->out_flags.all & 1) {
2165
2166 req_task->hob_ports[3] = reply->lba_low_ex;
2167 req_task->hob_ports[4] = reply->lba_mid_ex;
2168 req_task->hob_ports[5] = reply->lba_hi_ex;
2169 req_task->hob_ports[1] = reply->features_ex;
2170 req_task->hob_ports[2] = reply->sect_cnt_ex;
2171 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002172 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002173 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002174 "err %x, sect_cnt %x, lbalo %x,"
2175 "lbamid %x, lbahi %x, dev %x\n",
2176 __func__,
2177 req_task->io_ports[7],
2178 req_task->io_ports[1],
2179 req_task->io_ports[2],
2180 req_task->io_ports[3],
2181 req_task->io_ports[4],
2182 req_task->io_ports[5],
2183 req_task->io_ports[6]);
2184
Sam Bradshaw88523a62011-08-30 08:34:26 -06002185 if (taskout) {
2186 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2187 err = -EFAULT;
2188 goto abort;
2189 }
2190 }
2191 if (taskin) {
2192 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2193 err = -EFAULT;
2194 goto abort;
2195 }
2196 }
2197abort:
2198 if (inbuf_dma)
2199 pci_unmap_single(dd->pdev, inbuf_dma,
2200 taskin, DMA_FROM_DEVICE);
2201 if (outbuf_dma)
2202 pci_unmap_single(dd->pdev, outbuf_dma,
2203 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002204 kfree(outbuf);
2205 kfree(inbuf);
2206
2207 return err;
2208}
2209
2210/*
2211 * Handle IOCTL calls from the Block Layer.
2212 *
2213 * This function is called by the Block Layer when it receives an IOCTL
2214 * command that it does not understand. If the IOCTL command is not supported
2215 * this function returns -ENOTTY.
2216 *
2217 * @dd Pointer to the driver data structure.
2218 * @cmd IOCTL command passed from the Block Layer.
2219 * @arg IOCTL argument passed from the Block Layer.
2220 *
2221 * return value
2222 * 0 The IOCTL completed successfully.
2223 * -ENOTTY The specified command is not supported.
2224 * -EFAULT An error occurred copying data to a user space buffer.
2225 * -EIO An error occurred while executing the command.
2226 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002227static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2228 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002229{
2230 switch (cmd) {
2231 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002232 {
2233 if (copy_to_user((void __user *)arg, dd->port->identify,
2234 sizeof(u16) * ATA_ID_WORDS))
2235 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002236 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002237 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002238 case HDIO_DRIVE_CMD:
2239 {
2240 u8 drive_command[4];
2241
2242 /* Copy the user command info to our buffer. */
2243 if (copy_from_user(drive_command,
2244 (void __user *) arg,
2245 sizeof(drive_command)))
2246 return -EFAULT;
2247
2248 /* Execute the drive command. */
2249 if (exec_drive_command(dd->port,
2250 drive_command,
2251 (void __user *) (arg+4)))
2252 return -EIO;
2253
2254 /* Copy the status back to the users buffer. */
2255 if (copy_to_user((void __user *) arg,
2256 drive_command,
2257 sizeof(drive_command)))
2258 return -EFAULT;
2259
2260 break;
2261 }
2262 case HDIO_DRIVE_TASK:
2263 {
2264 u8 drive_command[7];
2265
2266 /* Copy the user command info to our buffer. */
2267 if (copy_from_user(drive_command,
2268 (void __user *) arg,
2269 sizeof(drive_command)))
2270 return -EFAULT;
2271
2272 /* Execute the drive command. */
2273 if (exec_drive_task(dd->port, drive_command))
2274 return -EIO;
2275
2276 /* Copy the status back to the users buffer. */
2277 if (copy_to_user((void __user *) arg,
2278 drive_command,
2279 sizeof(drive_command)))
2280 return -EFAULT;
2281
2282 break;
2283 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002284 case HDIO_DRIVE_TASKFILE: {
2285 ide_task_request_t req_task;
2286 int ret, outtotal;
2287
2288 if (copy_from_user(&req_task, (void __user *) arg,
2289 sizeof(req_task)))
2290 return -EFAULT;
2291
2292 outtotal = sizeof(req_task);
2293
2294 ret = exec_drive_taskfile(dd, (void __user *) arg,
2295 &req_task, outtotal);
2296
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002297 if (copy_to_user((void __user *) arg, &req_task,
2298 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002299 return -EFAULT;
2300
2301 return ret;
2302 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002303
2304 default:
2305 return -EINVAL;
2306 }
2307 return 0;
2308}
2309
2310/*
2311 * Submit an IO to the hw
2312 *
2313 * This function is called by the block layer to issue an io
2314 * to the device. Upon completion, the callback function will
2315 * be called with the data parameter passed as the callback data.
2316 *
2317 * @dd Pointer to the driver data structure.
2318 * @start First sector to read.
2319 * @nsect Number of sectors to read.
2320 * @nents Number of entries in scatter list for the read command.
2321 * @tag The tag of this read command.
2322 * @callback Pointer to the function that should be called
2323 * when the read completes.
2324 * @data Callback data passed to the callback function
2325 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002326 * @dir Direction (read or write)
2327 *
2328 * return value
2329 * None
2330 */
Jens Axboeffc771b2014-05-09 09:42:02 -06002331static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq,
2332 struct mtip_cmd *command, int nents,
2333 struct blk_mq_hw_ctx *hctx)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002334{
2335 struct host_to_dev_fis *fis;
2336 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -06002337 int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2338 u64 start = blk_rq_pos(rq);
2339 unsigned int nsect = blk_rq_sectors(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002340
2341 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002342 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002343
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002344 prefetch(&port->flags);
2345
Sam Bradshaw88523a62011-08-30 08:34:26 -06002346 command->scatter_ents = nents;
2347
2348 /*
2349 * The number of retries for this command before it is
2350 * reported as a failure to the upper layers.
2351 */
2352 command->retries = MTIP_MAX_RETRIES;
2353
2354 /* Fill out fis */
2355 fis = command->command;
2356 fis->type = 0x27;
2357 fis->opts = 1 << 7;
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002358 if (dma_dir == DMA_FROM_DEVICE)
Jens Axboeffc771b2014-05-09 09:42:02 -06002359 fis->command = ATA_CMD_FPDMA_READ;
2360 else
2361 fis->command = ATA_CMD_FPDMA_WRITE;
Selvan Manieda45312012-11-07 06:03:53 -07002362 fis->lba_low = start & 0xFF;
2363 fis->lba_mid = (start >> 8) & 0xFF;
2364 fis->lba_hi = (start >> 16) & 0xFF;
2365 fis->lba_low_ex = (start >> 24) & 0xFF;
2366 fis->lba_mid_ex = (start >> 32) & 0xFF;
2367 fis->lba_hi_ex = (start >> 40) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002368 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002369 fis->features = nsect & 0xFF;
2370 fis->features_ex = (nsect >> 8) & 0xFF;
Jens Axboeffc771b2014-05-09 09:42:02 -06002371 fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002372 fis->sect_cnt_ex = 0;
2373 fis->control = 0;
2374 fis->res2 = 0;
2375 fis->res3 = 0;
2376 fill_command_sg(dd, command, nents);
2377
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002378 if (unlikely(command->unaligned))
Asai Thambi S P2077d942013-04-29 21:19:49 +02002379 fis->device |= 1 << 7;
2380
Sam Bradshaw88523a62011-08-30 08:34:26 -06002381 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002382 command->command_header->opts =
2383 __force_bit2int cpu_to_le32(
2384 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002385 command->command_header->byte_count = 0;
2386
2387 /*
2388 * Set the completion function and data for the command
2389 * within this layer.
2390 */
2391 command->comp_data = dd;
2392 command->comp_func = mtip_async_complete;
Asai Thambi S P45038362012-04-09 08:35:38 +02002393 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002394
2395 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002396 * To prevent this command from being issued
2397 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002398 */
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002399 if (unlikely(port->flags & MTIP_PF_PAUSE_IO)) {
Jens Axboeffc771b2014-05-09 09:42:02 -06002400 set_bit(rq->tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002401 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002402 return;
2403 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002404
2405 /* Issue the command to the hardware */
Jens Axboeffc771b2014-05-09 09:42:02 -06002406 mtip_issue_ncq_command(port, rq->tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002407}
2408
2409/*
Asai Thambi S P7412ff12012-06-04 12:43:03 -07002410 * Sysfs status dump.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002411 *
2412 * @dev Pointer to the device structure, passed by the kernrel.
2413 * @attr Pointer to the device_attribute structure passed by the kernel.
2414 * @buf Pointer to the char buffer that will receive the stats info.
2415 *
2416 * return value
2417 * The size, in bytes, of the data copied into buf.
2418 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002419static ssize_t mtip_hw_show_status(struct device *dev,
2420 struct device_attribute *attr,
2421 char *buf)
2422{
2423 struct driver_data *dd = dev_to_disk(dev)->private_data;
2424 int size = 0;
2425
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002426 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002427 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002428 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002429 size += sprintf(buf, "%s", "write_protect\n");
2430 else
2431 size += sprintf(buf, "%s", "online\n");
2432
2433 return size;
2434}
2435
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002436static DEVICE_ATTR(status, S_IRUGO, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002437
Asai Thambi S P0caff002013-04-03 19:56:21 +05302438/* debugsfs entries */
2439
2440static ssize_t show_device_status(struct device_driver *drv, char *buf)
2441{
2442 int size = 0;
2443 struct driver_data *dd, *tmp;
2444 unsigned long flags;
2445 char id_buf[42];
2446 u16 status = 0;
2447
2448 spin_lock_irqsave(&dev_lock, flags);
2449 size += sprintf(&buf[size], "Devices Present:\n");
2450 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002451 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302452 if (dd->port &&
2453 dd->port->identify &&
2454 dd->port->identify_valid) {
2455 strlcpy(id_buf,
2456 (char *) (dd->port->identify + 10), 21);
2457 status = *(dd->port->identify + 141);
2458 } else {
2459 memset(id_buf, 0, 42);
2460 status = 0;
2461 }
2462
2463 if (dd->port &&
2464 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2465 size += sprintf(&buf[size],
2466 " device %s %s (ftl rebuild %d %%)\n",
2467 dev_name(&dd->pdev->dev),
2468 id_buf,
2469 status);
2470 } else {
2471 size += sprintf(&buf[size],
2472 " device %s %s\n",
2473 dev_name(&dd->pdev->dev),
2474 id_buf);
2475 }
2476 }
2477 }
2478
2479 size += sprintf(&buf[size], "Devices Being Removed:\n");
2480 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002481 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302482 if (dd->port &&
2483 dd->port->identify &&
2484 dd->port->identify_valid) {
2485 strlcpy(id_buf,
2486 (char *) (dd->port->identify+10), 21);
2487 status = *(dd->port->identify + 141);
2488 } else {
2489 memset(id_buf, 0, 42);
2490 status = 0;
2491 }
2492
2493 if (dd->port &&
2494 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2495 size += sprintf(&buf[size],
2496 " device %s %s (ftl rebuild %d %%)\n",
2497 dev_name(&dd->pdev->dev),
2498 id_buf,
2499 status);
2500 } else {
2501 size += sprintf(&buf[size],
2502 " device %s %s\n",
2503 dev_name(&dd->pdev->dev),
2504 id_buf);
2505 }
2506 }
2507 }
2508 spin_unlock_irqrestore(&dev_lock, flags);
2509
2510 return size;
2511}
2512
2513static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2514 size_t len, loff_t *offset)
2515{
David Milburnc8afd0d2013-05-23 16:23:45 -05002516 struct driver_data *dd = (struct driver_data *)f->private_data;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302517 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002518 char *buf;
2519 int rv = 0;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302520
2521 if (!len || *offset)
2522 return 0;
2523
David Milburnc8afd0d2013-05-23 16:23:45 -05002524 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2525 if (!buf) {
2526 dev_err(&dd->pdev->dev,
2527 "Memory allocation: status buffer\n");
2528 return -ENOMEM;
2529 }
2530
Asai Thambi S P0caff002013-04-03 19:56:21 +05302531 size += show_device_status(NULL, buf);
2532
2533 *offset = size <= len ? size : len;
2534 size = copy_to_user(ubuf, buf, *offset);
2535 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002536 rv = -EFAULT;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302537
David Milburnc8afd0d2013-05-23 16:23:45 -05002538 kfree(buf);
2539 return rv ? rv : *offset;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302540}
2541
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002542static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2543 size_t len, loff_t *offset)
2544{
2545 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002546 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002547 u32 group_allocated;
2548 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002549 int n, rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002550
2551 if (!len || size)
2552 return 0;
2553
David Milburnc8afd0d2013-05-23 16:23:45 -05002554 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2555 if (!buf) {
2556 dev_err(&dd->pdev->dev,
2557 "Memory allocation: register buffer\n");
2558 return -ENOMEM;
2559 }
2560
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002561 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2562
2563 for (n = dd->slot_groups-1; n >= 0; n--)
2564 size += sprintf(&buf[size], "%08X ",
2565 readl(dd->port->s_active[n]));
2566
2567 size += sprintf(&buf[size], "]\n");
2568 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2569
2570 for (n = dd->slot_groups-1; n >= 0; n--)
2571 size += sprintf(&buf[size], "%08X ",
2572 readl(dd->port->cmd_issue[n]));
2573
2574 size += sprintf(&buf[size], "]\n");
2575 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2576
2577 for (n = dd->slot_groups-1; n >= 0; n--)
2578 size += sprintf(&buf[size], "%08X ",
2579 readl(dd->port->completed[n]));
2580
2581 size += sprintf(&buf[size], "]\n");
2582 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2583 readl(dd->port->mmio + PORT_IRQ_STAT));
2584 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2585 readl(dd->mmio + HOST_IRQ_STAT));
2586 size += sprintf(&buf[size], "\n");
2587
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002588 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2589
2590 for (n = dd->slot_groups-1; n >= 0; n--) {
2591 if (sizeof(long) > sizeof(u32))
2592 group_allocated =
2593 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2594 else
2595 group_allocated = dd->port->cmds_to_issue[n];
2596 size += sprintf(&buf[size], "%08X ", group_allocated);
2597 }
2598 size += sprintf(&buf[size], "]\n");
2599
2600 *offset = size <= len ? size : len;
2601 size = copy_to_user(ubuf, buf, *offset);
2602 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002603 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002604
David Milburnc8afd0d2013-05-23 16:23:45 -05002605 kfree(buf);
2606 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002607}
2608
2609static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2610 size_t len, loff_t *offset)
2611{
2612 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002613 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002614 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002615 int rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002616
2617 if (!len || size)
2618 return 0;
2619
David Milburnc8afd0d2013-05-23 16:23:45 -05002620 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2621 if (!buf) {
2622 dev_err(&dd->pdev->dev,
2623 "Memory allocation: flag buffer\n");
2624 return -ENOMEM;
2625 }
2626
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002627 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2628 dd->port->flags);
2629 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2630 dd->dd_flag);
2631
2632 *offset = size <= len ? size : len;
2633 size = copy_to_user(ubuf, buf, *offset);
2634 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002635 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002636
David Milburnc8afd0d2013-05-23 16:23:45 -05002637 kfree(buf);
2638 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002639}
2640
Asai Thambi S P0caff002013-04-03 19:56:21 +05302641static const struct file_operations mtip_device_status_fops = {
2642 .owner = THIS_MODULE,
2643 .open = simple_open,
2644 .read = mtip_hw_read_device_status,
2645 .llseek = no_llseek,
2646};
2647
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002648static const struct file_operations mtip_regs_fops = {
2649 .owner = THIS_MODULE,
2650 .open = simple_open,
2651 .read = mtip_hw_read_registers,
2652 .llseek = no_llseek,
2653};
2654
2655static const struct file_operations mtip_flags_fops = {
2656 .owner = THIS_MODULE,
2657 .open = simple_open,
2658 .read = mtip_hw_read_flags,
2659 .llseek = no_llseek,
2660};
2661
Sam Bradshaw88523a62011-08-30 08:34:26 -06002662/*
2663 * Create the sysfs related attributes.
2664 *
2665 * @dd Pointer to the driver data structure.
2666 * @kobj Pointer to the kobj for the block device.
2667 *
2668 * return value
2669 * 0 Operation completed successfully.
2670 * -EINVAL Invalid parameter.
2671 */
Jens Axboe63166682011-09-27 21:27:43 -06002672static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002673{
2674 if (!kobj || !dd)
2675 return -EINVAL;
2676
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002677 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2678 dev_warn(&dd->pdev->dev,
2679 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002680 return 0;
2681}
2682
2683/*
2684 * Remove the sysfs related attributes.
2685 *
2686 * @dd Pointer to the driver data structure.
2687 * @kobj Pointer to the kobj for the block device.
2688 *
2689 * return value
2690 * 0 Operation completed successfully.
2691 * -EINVAL Invalid parameter.
2692 */
Jens Axboe63166682011-09-27 21:27:43 -06002693static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002694{
2695 if (!kobj || !dd)
2696 return -EINVAL;
2697
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002698 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002699
2700 return 0;
2701}
2702
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002703static int mtip_hw_debugfs_init(struct driver_data *dd)
2704{
2705 if (!dfs_parent)
2706 return -1;
2707
2708 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2709 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2710 dev_warn(&dd->pdev->dev,
2711 "Error creating node %s under debugfs\n",
2712 dd->disk->disk_name);
2713 dd->dfs_node = NULL;
2714 return -1;
2715 }
2716
2717 debugfs_create_file("flags", S_IRUGO, dd->dfs_node, dd,
2718 &mtip_flags_fops);
2719 debugfs_create_file("registers", S_IRUGO, dd->dfs_node, dd,
2720 &mtip_regs_fops);
2721
2722 return 0;
2723}
2724
2725static void mtip_hw_debugfs_exit(struct driver_data *dd)
2726{
Sam Bradshaw974a51a2013-05-15 10:04:34 +02002727 if (dd->dfs_node)
2728 debugfs_remove_recursive(dd->dfs_node);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002729}
2730
Sam Bradshaw88523a62011-08-30 08:34:26 -06002731/*
2732 * Perform any init/resume time hardware setup
2733 *
2734 * @dd Pointer to the driver data structure.
2735 *
2736 * return value
2737 * None
2738 */
2739static inline void hba_setup(struct driver_data *dd)
2740{
2741 u32 hwdata;
2742 hwdata = readl(dd->mmio + HOST_HSORG);
2743
2744 /* interrupt bug workaround: use only 1 IS bit.*/
2745 writel(hwdata |
2746 HSORG_DISABLE_SLOTGRP_INTR |
2747 HSORG_DISABLE_SLOTGRP_PXIS,
2748 dd->mmio + HOST_HSORG);
2749}
2750
Asai Thambi S P2077d942013-04-29 21:19:49 +02002751static int mtip_device_unaligned_constrained(struct driver_data *dd)
2752{
2753 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
2754}
2755
Sam Bradshaw88523a62011-08-30 08:34:26 -06002756/*
2757 * Detect the details of the product, and store anything needed
2758 * into the driver data structure. This includes product type and
2759 * version and number of slot groups.
2760 *
2761 * @dd Pointer to the driver data structure.
2762 *
2763 * return value
2764 * None
2765 */
2766static void mtip_detect_product(struct driver_data *dd)
2767{
2768 u32 hwdata;
2769 unsigned int rev, slotgroups;
2770
2771 /*
2772 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2773 * info register:
2774 * [15:8] hardware/software interface rev#
2775 * [ 3] asic-style interface
2776 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2777 */
2778 hwdata = readl(dd->mmio + HOST_HSORG);
2779
2780 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2781 dd->slot_groups = 1;
2782
2783 if (hwdata & 0x8) {
2784 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2785 rev = (hwdata & HSORG_HWREV) >> 8;
2786 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2787 dev_info(&dd->pdev->dev,
2788 "ASIC-FPGA design, HS rev 0x%x, "
2789 "%i slot groups [%i slots]\n",
2790 rev,
2791 slotgroups,
2792 slotgroups * 32);
2793
2794 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2795 dev_warn(&dd->pdev->dev,
2796 "Warning: driver only supports "
2797 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2798 slotgroups = MTIP_MAX_SLOT_GROUPS;
2799 }
2800 dd->slot_groups = slotgroups;
2801 return;
2802 }
2803
2804 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2805}
2806
2807/*
2808 * Blocking wait for FTL rebuild to complete
2809 *
2810 * @dd Pointer to the DRIVER_DATA structure.
2811 *
2812 * return value
2813 * 0 FTL rebuild completed successfully
2814 * -EFAULT FTL rebuild error/timeout/interruption
2815 */
2816static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2817{
2818 unsigned long timeout, cnt = 0, start;
2819
2820 dev_warn(&dd->pdev->dev,
2821 "FTL rebuild in progress. Polling for completion.\n");
2822
2823 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002824 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2825
2826 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002827 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002828 &dd->dd_flag)))
2829 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002830 if (mtip_check_surprise_removal(dd->pdev))
2831 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002832
Sam Bradshaw88523a62011-08-30 08:34:26 -06002833 if (mtip_get_identify(dd->port, NULL) < 0)
2834 return -EFAULT;
2835
2836 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2837 MTIP_FTL_REBUILD_MAGIC) {
2838 ssleep(1);
2839 /* Print message every 3 minutes */
2840 if (cnt++ >= 180) {
2841 dev_warn(&dd->pdev->dev,
2842 "FTL rebuild in progress (%d secs).\n",
2843 jiffies_to_msecs(jiffies - start) / 1000);
2844 cnt = 0;
2845 }
2846 } else {
2847 dev_warn(&dd->pdev->dev,
2848 "FTL rebuild complete (%d secs).\n",
2849 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002850 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002851 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002852 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002853 } while (time_before(jiffies, timeout));
2854
2855 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002856 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002857 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2858 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002859 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002860}
2861
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002862static void mtip_softirq_done_fn(struct request *rq)
2863{
2864 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
2865 struct driver_data *dd = rq->q->queuedata;
2866
2867 /* Unmap the DMA scatter list entries */
2868 dma_unmap_sg(&dd->pdev->dev, cmd->sg, cmd->scatter_ents,
2869 cmd->direction);
2870
2871 if (unlikely(cmd->unaligned))
2872 up(&dd->port->cmd_slot_unal);
2873
Christoph Hellwig4dda4732017-04-20 16:03:07 +02002874 blk_mq_end_request(rq, cmd->status);
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002875}
2876
2877static void mtip_abort_cmd(struct request *req, void *data,
2878 bool reserved)
2879{
Christoph Hellwig4dda4732017-04-20 16:03:07 +02002880 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(req);
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002881 struct driver_data *dd = data;
2882
2883 dbg_printk(MTIP_DRV_NAME " Aborting request, tag = %d\n", req->tag);
2884
2885 clear_bit(req->tag, dd->port->cmds_to_issue);
Christoph Hellwig4dda4732017-04-20 16:03:07 +02002886 cmd->status = -EIO;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002887 mtip_softirq_done_fn(req);
2888}
2889
2890static void mtip_queue_cmd(struct request *req, void *data,
2891 bool reserved)
2892{
2893 struct driver_data *dd = data;
2894
2895 set_bit(req->tag, dd->port->cmds_to_issue);
2896 blk_abort_request(req);
2897}
2898
Sam Bradshaw88523a62011-08-30 08:34:26 -06002899/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002900 * service thread to issue queued commands
2901 *
2902 * @data Pointer to the driver data structure.
2903 *
2904 * return value
2905 * 0
2906 */
2907
2908static int mtip_service_thread(void *data)
2909{
2910 struct driver_data *dd = (struct driver_data *)data;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002911 unsigned long slot, slot_start, slot_wrap, to;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002912 unsigned int num_cmd_slots = dd->slot_groups * 32;
2913 struct mtip_port *port = dd->port;
2914
2915 while (1) {
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002916 if (kthread_should_stop() ||
2917 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2918 goto st_out;
2919 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2920
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002921 /*
2922 * the condition is to check neither an internal command is
2923 * is in progress nor error handling is active
2924 */
2925 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi SPcfc05bd2016-02-24 21:16:00 -08002926 (port->flags & MTIP_PF_SVC_THD_WORK));
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002927
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002928 if (kthread_should_stop() ||
2929 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2930 goto st_out;
2931
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002932 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002933 &dd->dd_flag)))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002934 goto st_out;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002935
Asai Thambi SPcfc05bd2016-02-24 21:16:00 -08002936 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2937
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002938restart_eh:
2939 /* Demux bits: start with error handling */
2940 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) {
2941 mtip_handle_tfe(dd);
2942 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
2943 }
2944
2945 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags))
2946 goto restart_eh;
2947
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002948 if (test_bit(MTIP_PF_TO_ACTIVE_BIT, &port->flags)) {
2949 to = jiffies + msecs_to_jiffies(5000);
2950
2951 do {
2952 mdelay(100);
2953 } while (atomic_read(&dd->irq_workers_active) != 0 &&
2954 time_before(jiffies, to));
2955
2956 if (atomic_read(&dd->irq_workers_active) != 0)
2957 dev_warn(&dd->pdev->dev,
2958 "Completion workers still active!");
2959
2960 spin_lock(dd->queue->queue_lock);
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_queue_cmd, dd);
2963 spin_unlock(dd->queue->queue_lock);
2964
2965 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &dd->port->flags);
2966
2967 if (mtip_device_reset(dd))
Keith Busch6d125de2016-03-10 13:58:48 +02002968 blk_mq_tagset_busy_iter(&dd->tags,
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002969 mtip_abort_cmd, dd);
2970
2971 clear_bit(MTIP_PF_TO_ACTIVE_BIT, &dd->port->flags);
2972 }
2973
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002974 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002975 slot = 1;
2976 /* used to restrict the loop to one iteration */
2977 slot_start = num_cmd_slots;
2978 slot_wrap = 0;
2979 while (1) {
2980 slot = find_next_bit(port->cmds_to_issue,
2981 num_cmd_slots, slot);
2982 if (slot_wrap == 1) {
2983 if ((slot_start >= slot) ||
2984 (slot >= num_cmd_slots))
2985 break;
2986 }
2987 if (unlikely(slot_start == num_cmd_slots))
2988 slot_start = slot;
2989
2990 if (unlikely(slot == num_cmd_slots)) {
2991 slot = 1;
2992 slot_wrap = 1;
2993 continue;
2994 }
2995
2996 /* Issue the command to the hardware */
2997 mtip_issue_ncq_command(port, slot);
2998
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002999 clear_bit(slot, port->cmds_to_issue);
3000 }
3001
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003002 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07003003 }
3004
3005 if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08003006 if (mtip_ftl_rebuild_poll(dd) == 0)
3007 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003008 }
3009 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003010
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003011st_out:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003012 return 0;
3013}
3014
3015/*
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003016 * DMA region teardown
3017 *
3018 * @dd Pointer to driver_data structure
3019 *
3020 * return value
3021 * None
3022 */
3023static void mtip_dma_free(struct driver_data *dd)
3024{
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003025 struct mtip_port *port = dd->port;
3026
3027 if (port->block1)
3028 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3029 port->block1, port->block1_dma);
3030
3031 if (port->command_list) {
3032 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3033 port->command_list, port->command_list_dma);
3034 }
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003035}
3036
3037/*
3038 * DMA region setup
3039 *
3040 * @dd Pointer to driver_data structure
3041 *
3042 * return value
3043 * -ENOMEM Not enough free DMA region space to initialize driver
3044 */
3045static int mtip_dma_alloc(struct driver_data *dd)
3046{
3047 struct mtip_port *port = dd->port;
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003048
3049 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
3050 port->block1 =
3051 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3052 &port->block1_dma, GFP_KERNEL);
3053 if (!port->block1)
3054 return -ENOMEM;
3055 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
3056
3057 /* Allocate dma memory for command list */
3058 port->command_list =
3059 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
3060 &port->command_list_dma, GFP_KERNEL);
3061 if (!port->command_list) {
3062 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
3063 port->block1, port->block1_dma);
3064 port->block1 = NULL;
3065 port->block1_dma = 0;
3066 return -ENOMEM;
3067 }
3068 memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
3069
3070 /* Setup all pointers into first DMA region */
3071 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
3072 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
3073 port->identify = port->block1 + AHCI_IDFY_OFFSET;
3074 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
3075 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
3076 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
3077 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
3078 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
3079
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003080 return 0;
3081}
3082
Jens Axboeffc771b2014-05-09 09:42:02 -06003083static int mtip_hw_get_identify(struct driver_data *dd)
3084{
3085 struct smart_attr attr242;
3086 unsigned char *buf;
3087 int rv;
3088
3089 if (mtip_get_identify(dd->port, NULL) < 0)
3090 return -EFAULT;
3091
3092 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
3093 MTIP_FTL_REBUILD_MAGIC) {
3094 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
3095 return MTIP_FTL_REBUILD_MAGIC;
3096 }
3097 mtip_dump_identify(dd->port);
3098
3099 /* check write protect, over temp and rebuild statuses */
3100 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
3101 dd->port->log_buf,
3102 dd->port->log_buf_dma, 1);
3103 if (rv) {
3104 dev_warn(&dd->pdev->dev,
3105 "Error in READ LOG EXT (10h) command\n");
3106 /* non-critical error, don't fail the load */
3107 } else {
3108 buf = (unsigned char *)dd->port->log_buf;
3109 if (buf[259] & 0x1) {
3110 dev_info(&dd->pdev->dev,
3111 "Write protect bit is set.\n");
3112 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
3113 }
3114 if (buf[288] == 0xF7) {
3115 dev_info(&dd->pdev->dev,
3116 "Exceeded Tmax, drive in thermal shutdown.\n");
3117 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
3118 }
3119 if (buf[288] == 0xBF) {
3120 dev_info(&dd->pdev->dev,
3121 "Drive indicates rebuild has failed.\n");
Asai Thambi SPaae4a032016-02-24 21:18:20 -08003122 set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag);
Jens Axboeffc771b2014-05-09 09:42:02 -06003123 }
3124 }
3125
3126 /* get write protect progess */
3127 memset(&attr242, 0, sizeof(struct smart_attr));
3128 if (mtip_get_smart_attr(dd->port, 242, &attr242))
3129 dev_warn(&dd->pdev->dev,
3130 "Unable to check write protect progress\n");
3131 else
3132 dev_info(&dd->pdev->dev,
3133 "Write protect progress: %u%% (%u blocks)\n",
3134 attr242.cur, le32_to_cpu(attr242.data));
3135
3136 return rv;
3137}
3138
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003139/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003140 * Called once for each card.
3141 *
3142 * @dd Pointer to the driver data structure.
3143 *
3144 * return value
3145 * 0 on success, else an error code.
3146 */
Jens Axboe63166682011-09-27 21:27:43 -06003147static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003148{
3149 int i;
3150 int rv;
3151 unsigned int num_command_slots;
Asai Thambi S P45038362012-04-09 08:35:38 +02003152 unsigned long timeout, timetaken;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003153
3154 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3155
3156 mtip_detect_product(dd);
3157 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3158 rv = -EIO;
3159 goto out1;
3160 }
3161 num_command_slots = dd->slot_groups * 32;
3162
3163 hba_setup(dd);
3164
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003165 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3166 dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003167 if (!dd->port) {
3168 dev_err(&dd->pdev->dev,
3169 "Memory allocation: port structure\n");
3170 return -ENOMEM;
3171 }
3172
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003173 /* Continue workqueue setup */
3174 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3175 dd->work[i].port = dd->port;
3176
Asai Thambi S P2077d942013-04-29 21:19:49 +02003177 /* Enable unaligned IO constraints for some devices */
3178 if (mtip_device_unaligned_constrained(dd))
3179 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3180 else
3181 dd->unal_qdepth = 0;
3182
Asai Thambi S P2077d942013-04-29 21:19:49 +02003183 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003184
3185 /* Spinlock to prevent concurrent issue */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003186 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3187 spin_lock_init(&dd->port->cmd_issue_lock[i]);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003188
3189 /* Set the port mmio base address. */
3190 dd->port->mmio = dd->mmio + PORT_OFFSET;
3191 dd->port->dd = dd;
3192
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003193 /* DMA allocations */
3194 rv = mtip_dma_alloc(dd);
3195 if (rv < 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003196 goto out1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003197
3198 /* Setup the pointers to the extended s_active and CI registers. */
3199 for (i = 0; i < dd->slot_groups; i++) {
3200 dd->port->s_active[i] =
3201 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3202 dd->port->cmd_issue[i] =
3203 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3204 dd->port->completed[i] =
3205 dd->port->mmio + i*0x80 + PORT_SDBV;
3206 }
3207
Asai Thambi S P45038362012-04-09 08:35:38 +02003208 timetaken = jiffies;
3209 timeout = jiffies + msecs_to_jiffies(30000);
3210 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3211 time_before(jiffies, timeout)) {
3212 mdelay(100);
3213 }
3214 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3215 timetaken = jiffies - timetaken;
3216 dev_warn(&dd->pdev->dev,
3217 "Surprise removal detected at %u ms\n",
3218 jiffies_to_msecs(timetaken));
3219 rv = -ENODEV;
3220 goto out2 ;
3221 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003222 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003223 timetaken = jiffies - timetaken;
3224 dev_warn(&dd->pdev->dev,
3225 "Removal detected at %u ms\n",
3226 jiffies_to_msecs(timetaken));
3227 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003228 goto out2;
3229 }
3230
Asai Thambi S P45038362012-04-09 08:35:38 +02003231 /* Conditionally reset the HBA. */
3232 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3233 if (mtip_hba_reset(dd) < 0) {
3234 dev_err(&dd->pdev->dev,
3235 "Card did not reset within timeout\n");
3236 rv = -EIO;
3237 goto out2;
3238 }
3239 } else {
3240 /* Clear any pending interrupts on the HBA */
3241 writel(readl(dd->mmio + HOST_IRQ_STAT),
3242 dd->mmio + HOST_IRQ_STAT);
3243 }
3244
Sam Bradshaw88523a62011-08-30 08:34:26 -06003245 mtip_init_port(dd->port);
3246 mtip_start_port(dd->port);
3247
3248 /* Setup the ISR and enable interrupts. */
3249 rv = devm_request_irq(&dd->pdev->dev,
3250 dd->pdev->irq,
3251 mtip_irq_handler,
3252 IRQF_SHARED,
3253 dev_driver_string(&dd->pdev->dev),
3254 dd);
3255
3256 if (rv) {
3257 dev_err(&dd->pdev->dev,
3258 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3259 goto out2;
3260 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003261 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003262
3263 /* Enable interrupts on the HBA. */
3264 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3265 dd->mmio + HOST_CTL);
3266
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003267 init_waitqueue_head(&dd->port->svc_wait);
3268
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003269 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003270 rv = -EFAULT;
3271 goto out3;
3272 }
3273
Sam Bradshaw88523a62011-08-30 08:34:26 -06003274 return rv;
3275
3276out3:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003277 /* Disable interrupts on the HBA. */
3278 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3279 dd->mmio + HOST_CTL);
3280
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003281 /* Release the IRQ. */
3282 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003283 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3284
3285out2:
3286 mtip_deinit_port(dd->port);
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003287 mtip_dma_free(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003288
Sam Bradshaw88523a62011-08-30 08:34:26 -06003289out1:
3290 /* Free the memory allocated for the for structure. */
3291 kfree(dd->port);
3292
3293 return rv;
3294}
3295
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003296static int mtip_standby_drive(struct driver_data *dd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003297{
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003298 int rv = 0;
Jens Axboeffc771b2014-05-09 09:42:02 -06003299
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003300 if (dd->sr || !dd->port)
3301 return -ENODEV;
Jens Axboeffc771b2014-05-09 09:42:02 -06003302 /*
3303 * Send standby immediate (E0h) to the drive so that it
3304 * saves its state.
3305 */
3306 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003307 !test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag) &&
3308 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)) {
3309 rv = mtip_standby_immediate(dd->port);
3310 if (rv)
Jens Axboeffc771b2014-05-09 09:42:02 -06003311 dev_warn(&dd->pdev->dev,
3312 "STANDBY IMMEDIATE failed\n");
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003313 }
3314 return rv;
Jens Axboeffc771b2014-05-09 09:42:02 -06003315}
3316
Sam Bradshaw88523a62011-08-30 08:34:26 -06003317/*
3318 * Called to deinitialize an interface.
3319 *
3320 * @dd Pointer to the driver data structure.
3321 *
3322 * return value
3323 * 0
3324 */
Jens Axboe63166682011-09-27 21:27:43 -06003325static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003326{
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003327 if (!dd->sr) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003328 /* de-initialize the port. */
3329 mtip_deinit_port(dd->port);
3330
3331 /* Disable interrupts on the HBA. */
3332 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3333 dd->mmio + HOST_CTL);
3334 }
3335
Sam Bradshaw88523a62011-08-30 08:34:26 -06003336 /* Release the IRQ. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003337 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003338 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
Asai Thambi SP2132a542015-05-11 15:53:18 -07003339 msleep(1000);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003340
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003341 /* Free dma regions */
3342 mtip_dma_free(dd);
3343
Sam Bradshaw88523a62011-08-30 08:34:26 -06003344 /* Free the memory allocated for the for structure. */
3345 kfree(dd->port);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003346 dd->port = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003347
3348 return 0;
3349}
3350
3351/*
3352 * Issue a Standby Immediate command to the device.
3353 *
3354 * This function is called by the Block Layer just before the
3355 * system powers off during a shutdown.
3356 *
3357 * @dd Pointer to the driver data structure.
3358 *
3359 * return value
3360 * 0
3361 */
Jens Axboe63166682011-09-27 21:27:43 -06003362static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003363{
3364 /*
3365 * Send standby immediate (E0h) to the drive so that it
3366 * saves its state.
3367 */
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003368 mtip_standby_drive(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003369
3370 return 0;
3371}
3372
3373/*
3374 * Suspend function
3375 *
3376 * This function is called by the Block Layer just before the
3377 * system hibernates.
3378 *
3379 * @dd Pointer to the driver data structure.
3380 *
3381 * return value
3382 * 0 Suspend was successful
3383 * -EFAULT Suspend was not successful
3384 */
Jens Axboe63166682011-09-27 21:27:43 -06003385static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003386{
3387 /*
3388 * Send standby immediate (E0h) to the drive
3389 * so that it saves its state.
3390 */
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003391 if (mtip_standby_drive(dd) != 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003392 dev_err(&dd->pdev->dev,
3393 "Failed standby-immediate command\n");
3394 return -EFAULT;
3395 }
3396
3397 /* Disable interrupts on the HBA.*/
3398 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3399 dd->mmio + HOST_CTL);
3400 mtip_deinit_port(dd->port);
3401
3402 return 0;
3403}
3404
3405/*
3406 * Resume function
3407 *
3408 * This function is called by the Block Layer as the
3409 * system resumes.
3410 *
3411 * @dd Pointer to the driver data structure.
3412 *
3413 * return value
3414 * 0 Resume was successful
3415 * -EFAULT Resume was not successful
3416 */
Jens Axboe63166682011-09-27 21:27:43 -06003417static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003418{
3419 /* Perform any needed hardware setup steps */
3420 hba_setup(dd);
3421
3422 /* Reset the HBA */
3423 if (mtip_hba_reset(dd) != 0) {
3424 dev_err(&dd->pdev->dev,
3425 "Unable to reset the HBA\n");
3426 return -EFAULT;
3427 }
3428
3429 /*
3430 * Enable the port, DMA engine, and FIS reception specific
3431 * h/w in controller.
3432 */
3433 mtip_init_port(dd->port);
3434 mtip_start_port(dd->port);
3435
3436 /* Enable interrupts on the HBA.*/
3437 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3438 dd->mmio + HOST_CTL);
3439
3440 return 0;
3441}
3442
3443/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003444 * Helper function for reusing disk name
3445 * upon hot insertion.
3446 */
3447static int rssd_disk_name_format(char *prefix,
3448 int index,
3449 char *buf,
3450 int buflen)
3451{
3452 const int base = 'z' - 'a' + 1;
3453 char *begin = buf + strlen(prefix);
3454 char *end = buf + buflen;
3455 char *p;
3456 int unit;
3457
3458 p = end - 1;
3459 *p = '\0';
3460 unit = base;
3461 do {
3462 if (p == begin)
3463 return -EINVAL;
3464 *--p = 'a' + (index % unit);
3465 index = (index / unit) - 1;
3466 } while (index >= 0);
3467
3468 memmove(begin, p, end - p);
3469 memcpy(buf, prefix, strlen(prefix));
3470
3471 return 0;
3472}
3473
3474/*
3475 * Block layer IOCTL handler.
3476 *
3477 * @dev Pointer to the block_device structure.
3478 * @mode ignored
3479 * @cmd IOCTL command passed from the user application.
3480 * @arg Argument passed from the user application.
3481 *
3482 * return value
3483 * 0 IOCTL completed successfully.
3484 * -ENOTTY IOCTL not supported or invalid driver data
3485 * structure pointer.
3486 */
3487static int mtip_block_ioctl(struct block_device *dev,
3488 fmode_t mode,
3489 unsigned cmd,
3490 unsigned long arg)
3491{
3492 struct driver_data *dd = dev->bd_disk->private_data;
3493
3494 if (!capable(CAP_SYS_ADMIN))
3495 return -EACCES;
3496
3497 if (!dd)
3498 return -ENOTTY;
3499
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003500 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003501 return -ENOTTY;
3502
Sam Bradshaw88523a62011-08-30 08:34:26 -06003503 switch (cmd) {
3504 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003505 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003506 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003507 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003508 }
3509}
3510
Jens Axboe16d02c02011-09-27 15:50:01 -06003511#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003512/*
3513 * Block layer compat IOCTL handler.
3514 *
3515 * @dev Pointer to the block_device structure.
3516 * @mode ignored
3517 * @cmd IOCTL command passed from the user application.
3518 * @arg Argument passed from the user application.
3519 *
3520 * return value
3521 * 0 IOCTL completed successfully.
3522 * -ENOTTY IOCTL not supported or invalid driver data
3523 * structure pointer.
3524 */
3525static int mtip_block_compat_ioctl(struct block_device *dev,
3526 fmode_t mode,
3527 unsigned cmd,
3528 unsigned long arg)
3529{
3530 struct driver_data *dd = dev->bd_disk->private_data;
3531
3532 if (!capable(CAP_SYS_ADMIN))
3533 return -EACCES;
3534
3535 if (!dd)
3536 return -ENOTTY;
3537
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003538 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003539 return -ENOTTY;
3540
Sam Bradshaw88523a62011-08-30 08:34:26 -06003541 switch (cmd) {
3542 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003543 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003544 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003545 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003546 ide_task_request_t req_task;
3547 int compat_tasksize, outtotal, ret;
3548
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003549 compat_tasksize =
3550 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003551
3552 compat_req_task =
3553 (struct mtip_compat_ide_task_request_s __user *) arg;
3554
3555 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003556 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003557 return -EFAULT;
3558
3559 if (get_user(req_task.out_size, &compat_req_task->out_size))
3560 return -EFAULT;
3561
3562 if (get_user(req_task.in_size, &compat_req_task->in_size))
3563 return -EFAULT;
3564
3565 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3566
3567 ret = exec_drive_taskfile(dd, (void __user *) arg,
3568 &req_task, outtotal);
3569
3570 if (copy_to_user((void __user *) arg, &req_task,
3571 compat_tasksize -
3572 (2 * sizeof(compat_long_t))))
3573 return -EFAULT;
3574
3575 if (put_user(req_task.out_size, &compat_req_task->out_size))
3576 return -EFAULT;
3577
3578 if (put_user(req_task.in_size, &compat_req_task->in_size))
3579 return -EFAULT;
3580
3581 return ret;
3582 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003583 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003584 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003585 }
3586}
Jens Axboe16d02c02011-09-27 15:50:01 -06003587#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003588
3589/*
3590 * Obtain the geometry of the device.
3591 *
3592 * You may think that this function is obsolete, but some applications,
3593 * fdisk for example still used CHS values. This function describes the
3594 * device as having 224 heads and 56 sectors per cylinder. These values are
3595 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3596 * partition is described in terms of a start and end cylinder this means
3597 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3598 * affects performance.
3599 *
3600 * @dev Pointer to the block_device strucutre.
3601 * @geo Pointer to a hd_geometry structure.
3602 *
3603 * return value
3604 * 0 Operation completed successfully.
3605 * -ENOTTY An error occurred while reading the drive capacity.
3606 */
3607static int mtip_block_getgeo(struct block_device *dev,
3608 struct hd_geometry *geo)
3609{
3610 struct driver_data *dd = dev->bd_disk->private_data;
3611 sector_t capacity;
3612
3613 if (!dd)
3614 return -ENOTTY;
3615
3616 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3617 dev_warn(&dd->pdev->dev,
3618 "Could not get drive capacity.\n");
3619 return -ENOTTY;
3620 }
3621
3622 geo->heads = 224;
3623 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003624 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003625 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003626 return 0;
3627}
3628
Asai Thambi SP51c65702016-02-24 21:18:10 -08003629static int mtip_block_open(struct block_device *dev, fmode_t mode)
3630{
3631 struct driver_data *dd;
3632
3633 if (dev && dev->bd_disk) {
3634 dd = (struct driver_data *) dev->bd_disk->private_data;
3635
3636 if (dd) {
3637 if (test_bit(MTIP_DDF_REMOVAL_BIT,
3638 &dd->dd_flag)) {
3639 return -ENODEV;
3640 }
3641 return 0;
3642 }
3643 }
3644 return -ENODEV;
3645}
3646
Baoyou Xie99e6b872016-08-26 14:08:53 +08003647static void mtip_block_release(struct gendisk *disk, fmode_t mode)
Asai Thambi SP51c65702016-02-24 21:18:10 -08003648{
3649}
3650
Sam Bradshaw88523a62011-08-30 08:34:26 -06003651/*
3652 * Block device operation function.
3653 *
3654 * This structure contains pointers to the functions required by the block
3655 * layer.
3656 */
3657static const struct block_device_operations mtip_block_ops = {
Asai Thambi SP51c65702016-02-24 21:18:10 -08003658 .open = mtip_block_open,
3659 .release = mtip_block_release,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003660 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003661#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003662 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003663#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003664 .getgeo = mtip_block_getgeo,
3665 .owner = THIS_MODULE
3666};
3667
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003668static inline bool is_se_active(struct driver_data *dd)
3669{
3670 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT, &dd->port->flags))) {
3671 if (dd->port->ic_pause_timer) {
3672 unsigned long to = dd->port->ic_pause_timer +
3673 msecs_to_jiffies(1000);
3674 if (time_after(jiffies, to)) {
3675 clear_bit(MTIP_PF_SE_ACTIVE_BIT,
3676 &dd->port->flags);
3677 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
3678 dd->port->ic_pause_timer = 0;
3679 wake_up_interruptible(&dd->port->svc_wait);
3680 return false;
3681 }
3682 }
3683 return true;
3684 }
3685 return false;
3686}
3687
Sam Bradshaw88523a62011-08-30 08:34:26 -06003688/*
3689 * Block layer make request function.
3690 *
3691 * This function is called by the kernel to process a BIO for
3692 * the P320 device.
3693 *
3694 * @queue Pointer to the request queue. Unused other than to obtain
3695 * the driver data structure.
Jens Axboeffc771b2014-05-09 09:42:02 -06003696 * @rq Pointer to the request.
Sam Bradshaw88523a62011-08-30 08:34:26 -06003697 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003698 */
Jens Axboeffc771b2014-05-09 09:42:02 -06003699static int mtip_submit_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003700{
Jens Axboeffc771b2014-05-09 09:42:02 -06003701 struct driver_data *dd = hctx->queue->queuedata;
3702 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3703 unsigned int nents;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003704
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003705 if (is_se_active(dd))
3706 return -ENODATA;
3707
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003708 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3709 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3710 &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003711 return -ENXIO;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003712 }
3713 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003714 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003715 }
3716 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3717 &dd->dd_flag) &&
Jens Axboeffc771b2014-05-09 09:42:02 -06003718 rq_data_dir(rq))) {
3719 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003720 }
Asai Thambi SPaae4a032016-02-24 21:18:20 -08003721 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag) ||
3722 test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)))
Jens Axboeffc771b2014-05-09 09:42:02 -06003723 return -ENODATA;
Asai Thambi S P45038362012-04-09 08:35:38 +02003724 }
3725
Mike Christiec2df40d2016-06-05 14:32:17 -05003726 if (req_op(rq) == REQ_OP_DISCARD) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003727 int err;
3728
3729 err = mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq));
Christoph Hellwigc8a446a2014-09-13 16:40:10 -07003730 blk_mq_end_request(rq, err);
Jens Axboeffc771b2014-05-09 09:42:02 -06003731 return 0;
Asai Thambi S P15283462013-01-11 14:41:34 +01003732 }
3733
Jens Axboeffc771b2014-05-09 09:42:02 -06003734 /* Create the scatter list for this request. */
3735 nents = blk_rq_map_sg(hctx->queue, rq, cmd->sg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003736
Jens Axboeffc771b2014-05-09 09:42:02 -06003737 /* Issue the read/write. */
3738 mtip_hw_submit_io(dd, rq, cmd, nents, hctx);
3739 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003740}
3741
Jens Axboeffc771b2014-05-09 09:42:02 -06003742static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx,
3743 struct request *rq)
3744{
3745 struct driver_data *dd = hctx->queue->queuedata;
3746 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3747
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003748 if (rq_data_dir(rq) == READ || !dd->unal_qdepth)
Jens Axboeffc771b2014-05-09 09:42:02 -06003749 return false;
3750
3751 /*
3752 * If unaligned depth must be limited on this controller, mark it
3753 * as unaligned if the IO isn't on a 4k boundary (start of length).
3754 */
3755 if (blk_rq_sectors(rq) <= 64) {
3756 if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7))
3757 cmd->unaligned = 1;
3758 }
3759
3760 if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal))
3761 return true;
3762
3763 return false;
3764}
3765
Jens Axboe74c45052014-10-29 11:14:52 -06003766static int mtip_queue_rq(struct blk_mq_hw_ctx *hctx,
3767 const struct blk_mq_queue_data *bd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003768{
Jens Axboe74c45052014-10-29 11:14:52 -06003769 struct request *rq = bd->rq;
Jens Axboeffc771b2014-05-09 09:42:02 -06003770 int ret;
3771
Ming Leia4e84aa2017-04-27 07:45:18 -06003772 mtip_init_cmd_header(rq);
3773
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003774 if (unlikely(mtip_check_unal_depth(hctx, rq)))
Jens Axboeffc771b2014-05-09 09:42:02 -06003775 return BLK_MQ_RQ_QUEUE_BUSY;
3776
Christoph Hellwige2490072014-09-13 16:40:09 -07003777 blk_mq_start_request(rq);
3778
Jens Axboeffc771b2014-05-09 09:42:02 -06003779 ret = mtip_submit_request(hctx, rq);
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003780 if (likely(!ret))
Jens Axboeffc771b2014-05-09 09:42:02 -06003781 return BLK_MQ_RQ_QUEUE_OK;
3782
Jens Axboeffc771b2014-05-09 09:42:02 -06003783 return BLK_MQ_RQ_QUEUE_ERROR;
3784}
3785
3786static void mtip_free_cmd(void *data, struct request *rq,
3787 unsigned int hctx_idx, unsigned int request_idx)
3788{
3789 struct driver_data *dd = data;
3790 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3791
3792 if (!cmd->command)
3793 return;
3794
3795 dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3796 cmd->command, cmd->command_dma);
3797}
3798
3799static int mtip_init_cmd(void *data, struct request *rq, unsigned int hctx_idx,
3800 unsigned int request_idx, unsigned int numa_node)
3801{
3802 struct driver_data *dd = data;
3803 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
Jens Axboeffc771b2014-05-09 09:42:02 -06003804
Jeff Moyer74c9c912015-07-29 10:22:50 -04003805 /*
3806 * For flush requests, request_idx starts at the end of the
3807 * tag space. Since we don't support FLUSH/FUA, simply return
3808 * 0 as there's nothing to be done.
3809 */
3810 if (request_idx >= MTIP_MAX_COMMAND_SLOTS)
3811 return 0;
3812
Jens Axboeffc771b2014-05-09 09:42:02 -06003813 cmd->command = dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3814 &cmd->command_dma, GFP_KERNEL);
3815 if (!cmd->command)
3816 return -ENOMEM;
3817
3818 memset(cmd->command, 0, CMD_DMA_ALLOC_SZ);
3819
Jens Axboeffc771b2014-05-09 09:42:02 -06003820 sg_init_table(cmd->sg, MTIP_MAX_SG);
3821 return 0;
3822}
3823
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003824static enum blk_eh_timer_return mtip_cmd_timeout(struct request *req,
3825 bool reserved)
3826{
3827 struct driver_data *dd = req->q->queuedata;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003828
3829 if (reserved)
3830 goto exit_handler;
3831
3832 if (test_bit(req->tag, dd->port->cmds_to_issue))
3833 goto exit_handler;
3834
3835 if (test_and_set_bit(MTIP_PF_TO_ACTIVE_BIT, &dd->port->flags))
3836 goto exit_handler;
3837
3838 wake_up_interruptible(&dd->port->svc_wait);
3839exit_handler:
Jens Axboe90beb2e2016-03-04 08:15:48 -07003840 return BLK_EH_RESET_TIMER;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003841}
3842
Eric Biggersf363b082017-03-30 13:39:16 -07003843static const struct blk_mq_ops mtip_mq_ops = {
Jens Axboeffc771b2014-05-09 09:42:02 -06003844 .queue_rq = mtip_queue_rq,
Jens Axboeffc771b2014-05-09 09:42:02 -06003845 .init_request = mtip_init_cmd,
3846 .exit_request = mtip_free_cmd,
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003847 .complete = mtip_softirq_done_fn,
3848 .timeout = mtip_cmd_timeout,
Jens Axboeffc771b2014-05-09 09:42:02 -06003849};
3850
Sam Bradshaw88523a62011-08-30 08:34:26 -06003851/*
3852 * Block layer initialization function.
3853 *
3854 * This function is called once by the PCI layer for each P320
3855 * device that is connected to the system.
3856 *
3857 * @dd Pointer to the driver data structure.
3858 *
3859 * return value
3860 * 0 on success else an error code.
3861 */
Jens Axboe63166682011-09-27 21:27:43 -06003862static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003863{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003864 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003865 sector_t capacity;
3866 unsigned int index = 0;
3867 struct kobject *kobj;
3868
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003869 if (dd->disk)
3870 goto skip_create_disk; /* hw init done, before rebuild */
3871
Jens Axboeffc771b2014-05-09 09:42:02 -06003872 if (mtip_hw_init(dd)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003873 rv = -EINVAL;
3874 goto protocol_init_error;
3875 }
3876
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003877 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003878 if (dd->disk == NULL) {
3879 dev_err(&dd->pdev->dev,
3880 "Unable to allocate gendisk structure\n");
3881 rv = -EINVAL;
3882 goto alloc_disk_error;
3883 }
3884
3885 /* Generate the disk name, implemented same as in sd.c */
3886 do {
Pan Bian5b0e34e2016-12-01 10:10:46 +08003887 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL)) {
3888 rv = -ENOMEM;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003889 goto ida_get_error;
Pan Bian5b0e34e2016-12-01 10:10:46 +08003890 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003891
3892 spin_lock(&rssd_index_lock);
3893 rv = ida_get_new(&rssd_index_ida, &index);
3894 spin_unlock(&rssd_index_lock);
3895 } while (rv == -EAGAIN);
3896
3897 if (rv)
3898 goto ida_get_error;
3899
3900 rv = rssd_disk_name_format("rssd",
3901 index,
3902 dd->disk->disk_name,
3903 DISK_NAME_LEN);
3904 if (rv)
3905 goto disk_index_error;
3906
Sam Bradshaw88523a62011-08-30 08:34:26 -06003907 dd->disk->major = dd->major;
Asai Thambi SP75787262015-05-11 15:55:26 -07003908 dd->disk->first_minor = index * MTIP_MAX_MINORS;
3909 dd->disk->minors = MTIP_MAX_MINORS;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003910 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003911 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003912 dd->index = index;
3913
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003914 mtip_hw_debugfs_init(dd);
3915
Jens Axboeffc771b2014-05-09 09:42:02 -06003916 memset(&dd->tags, 0, sizeof(dd->tags));
3917 dd->tags.ops = &mtip_mq_ops;
3918 dd->tags.nr_hw_queues = 1;
3919 dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS;
3920 dd->tags.reserved_tags = 1;
3921 dd->tags.cmd_size = sizeof(struct mtip_cmd);
3922 dd->tags.numa_node = dd->numa_node;
Ming Lei4981d042017-04-15 20:38:23 +08003923 dd->tags.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_NO_SCHED;
Jens Axboeffc771b2014-05-09 09:42:02 -06003924 dd->tags.driver_data = dd;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003925 dd->tags.timeout = MTIP_NCQ_CMD_TIMEOUT_MS;
Jens Axboeffc771b2014-05-09 09:42:02 -06003926
3927 rv = blk_mq_alloc_tag_set(&dd->tags);
3928 if (rv) {
3929 dev_err(&dd->pdev->dev,
3930 "Unable to allocate request queue\n");
Asai Thambi SP008e56d2016-02-24 21:21:20 -08003931 goto block_queue_alloc_tag_error;
Jens Axboeffc771b2014-05-09 09:42:02 -06003932 }
3933
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003934 /* Allocate the request queue. */
Jens Axboeffc771b2014-05-09 09:42:02 -06003935 dd->queue = blk_mq_init_queue(&dd->tags);
Dan Carpentera8a642c2014-05-14 15:54:18 +03003936 if (IS_ERR(dd->queue)) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003937 dev_err(&dd->pdev->dev,
3938 "Unable to allocate request queue\n");
3939 rv = -ENOMEM;
3940 goto block_queue_alloc_init_error;
3941 }
3942
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003943 dd->disk->queue = dd->queue;
3944 dd->queue->queuedata = dd;
3945
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08003946skip_create_disk:
Jens Axboeffc771b2014-05-09 09:42:02 -06003947 /* Initialize the protocol layer. */
3948 wait_for_rebuild = mtip_hw_get_identify(dd);
3949 if (wait_for_rebuild < 0) {
3950 dev_err(&dd->pdev->dev,
3951 "Protocol layer initialization failed\n");
3952 rv = -EINVAL;
3953 goto init_hw_cmds_error;
3954 }
3955
3956 /*
3957 * if rebuild pending, start the service thread, and delay the block
Dan Williams0d52c7562016-06-15 19:44:20 -07003958 * queue creation and device_add_disk()
Jens Axboeffc771b2014-05-09 09:42:02 -06003959 */
3960 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3961 goto start_service_thread;
3962
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003963 /* Set device limits. */
3964 set_bit(QUEUE_FLAG_NONROT, &dd->queue->queue_flags);
Mike Snitzerb277da02014-10-04 10:55:32 -06003965 clear_bit(QUEUE_FLAG_ADD_RANDOM, &dd->queue->queue_flags);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003966 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3967 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003968 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3969 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003970 blk_queue_io_min(dd->queue, 4096);
Felipe Franciosi1044b1b2014-03-13 14:34:20 +00003971 blk_queue_bounce_limit(dd->queue, dd->pdev->dma_mask);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003972
Asai Thambi S P15283462013-01-11 14:41:34 +01003973 /* Signal trim support */
3974 if (dd->trim_supp == true) {
3975 set_bit(QUEUE_FLAG_DISCARD, &dd->queue->queue_flags);
3976 dd->queue->limits.discard_granularity = 4096;
3977 blk_queue_max_discard_sectors(dd->queue,
3978 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
Asai Thambi S P15283462013-01-11 14:41:34 +01003979 }
3980
Sam Bradshaw88523a62011-08-30 08:34:26 -06003981 /* Set the capacity of the device in 512 byte sectors. */
3982 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3983 dev_warn(&dd->pdev->dev,
3984 "Could not read drive capacity\n");
3985 rv = -EIO;
3986 goto read_capacity_error;
3987 }
3988 set_capacity(dd->disk, capacity);
3989
3990 /* Enable the block device and add it to /dev */
Dan Williams0d52c7562016-06-15 19:44:20 -07003991 device_add_disk(&dd->pdev->dev, dd->disk);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003992
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003993 dd->bdev = bdget_disk(dd->disk, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003994 /*
3995 * Now that the disk is active, initialize any sysfs attributes
3996 * managed by the protocol layer.
3997 */
3998 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3999 if (kobj) {
4000 mtip_hw_sysfs_init(dd, kobj);
4001 kobject_put(kobj);
4002 }
4003
Asai Thambi S P45038362012-04-09 08:35:38 +02004004 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004005 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004006 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02004007 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004008
4009start_service_thread:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004010 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
Rasmus Villemoes8aeea032015-11-20 10:46:49 +01004011 dd, dd->numa_node,
4012 "mtip_svc_thd_%02d", index);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004013
4014 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02004015 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004016 dd->mtip_svc_handler = NULL;
4017 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004018 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004019 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004020 wake_up_process(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02004021 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
4022 rv = wait_for_rebuild;
4023
Sam Bradshaw88523a62011-08-30 08:34:26 -06004024 return rv;
4025
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004026kthread_run_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004027 bdput(dd->bdev);
4028 dd->bdev = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004029
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004030 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004031 del_gendisk(dd->disk);
4032
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004033read_capacity_error:
Jens Axboeffc771b2014-05-09 09:42:02 -06004034init_hw_cmds_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004035 blk_cleanup_queue(dd->queue);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004036block_queue_alloc_init_error:
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004037 blk_mq_free_tag_set(&dd->tags);
4038block_queue_alloc_tag_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004039 mtip_hw_debugfs_exit(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004040disk_index_error:
4041 spin_lock(&rssd_index_lock);
4042 ida_remove(&rssd_index_ida, index);
4043 spin_unlock(&rssd_index_lock);
4044
4045ida_get_error:
4046 put_disk(dd->disk);
4047
4048alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01004049 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06004050
4051protocol_init_error:
4052 return rv;
4053}
4054
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004055static void mtip_no_dev_cleanup(struct request *rq, void *data, bool reserv)
4056{
4057 struct driver_data *dd = (struct driver_data *)data;
4058 struct mtip_cmd *cmd;
4059
Christoph Hellwig4dda4732017-04-20 16:03:07 +02004060 if (likely(!reserv)) {
Jens Axboe95c55ff2017-04-21 08:46:44 -06004061 cmd = blk_mq_rq_to_pdu(rq);
Christoph Hellwig4dda4732017-04-20 16:03:07 +02004062 cmd->status = -ENODEV;
Christoph Hellwig08e00292017-04-20 16:03:09 +02004063 blk_mq_complete_request(rq);
Christoph Hellwig4dda4732017-04-20 16:03:07 +02004064 } else if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &dd->port->flags)) {
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004065
4066 cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
4067 if (cmd->comp_func)
4068 cmd->comp_func(dd->port, MTIP_TAG_INTERNAL,
4069 cmd, -ENODEV);
4070 }
4071}
4072
Sam Bradshaw88523a62011-08-30 08:34:26 -06004073/*
4074 * Block layer deinitialization function.
4075 *
4076 * Called by the PCI layer as each P320 device is removed.
4077 *
4078 * @dd Pointer to the driver data structure.
4079 *
4080 * return value
4081 * 0
4082 */
Jens Axboe63166682011-09-27 21:27:43 -06004083static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004084{
4085 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004086
Asai Thambi SP2132a542015-05-11 15:53:18 -07004087 mtip_hw_debugfs_exit(dd);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01004088
Asai Thambi SP2132a542015-05-11 15:53:18 -07004089 if (dd->mtip_svc_handler) {
4090 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
4091 wake_up_interruptible(&dd->port->svc_wait);
4092 kthread_stop(dd->mtip_svc_handler);
4093 }
4094
4095 /* Clean up the sysfs attributes, if created */
4096 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
4097 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
4098 if (kobj) {
4099 mtip_hw_sysfs_exit(dd, kobj);
4100 kobject_put(kobj);
Asai Thambi S P45038362012-04-09 08:35:38 +02004101 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004102 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004103
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004104 if (!dd->sr) {
4105 /*
4106 * Explicitly wait here for IOs to quiesce,
4107 * as mtip_standby_drive usually won't wait for IOs.
4108 */
4109 if (!mtip_quiesce_io(dd->port, MTIP_QUIESCE_IO_TIMEOUT_MS,
4110 GFP_KERNEL))
4111 mtip_standby_drive(dd);
4112 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004113 else
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004114 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
4115 dd->disk->disk_name);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004116
Ming Lei1671d522017-03-27 20:06:57 +08004117 blk_freeze_queue_start(dd->queue);
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004118 blk_mq_stop_hw_queues(dd->queue);
Keith Busch6d125de2016-03-10 13:58:48 +02004119 blk_mq_tagset_busy_iter(&dd->tags, mtip_no_dev_cleanup, dd);
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004120
Asai Thambi SP2132a542015-05-11 15:53:18 -07004121 /*
4122 * Delete our gendisk structure. This also removes the device
4123 * from /dev
4124 */
4125 if (dd->bdev) {
4126 bdput(dd->bdev);
4127 dd->bdev = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004128 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004129 if (dd->disk) {
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08004130 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
4131 del_gendisk(dd->disk);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004132 if (dd->disk->queue) {
4133 blk_cleanup_queue(dd->queue);
4134 blk_mq_free_tag_set(&dd->tags);
4135 dd->queue = NULL;
4136 }
4137 put_disk(dd->disk);
4138 }
4139 dd->disk = NULL;
4140
4141 spin_lock(&rssd_index_lock);
4142 ida_remove(&rssd_index_ida, dd->index);
4143 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004144
4145 /* De-initialize the protocol layer. */
4146 mtip_hw_exit(dd);
4147
4148 return 0;
4149}
4150
4151/*
4152 * Function called by the PCI layer when just before the
4153 * machine shuts down.
4154 *
4155 * If a protocol layer shutdown function is present it will be called
4156 * by this function.
4157 *
4158 * @dd Pointer to the driver data structure.
4159 *
4160 * return value
4161 * 0
4162 */
Jens Axboe63166682011-09-27 21:27:43 -06004163static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004164{
Jens Axboeffc771b2014-05-09 09:42:02 -06004165 mtip_hw_shutdown(dd);
4166
Sam Bradshaw88523a62011-08-30 08:34:26 -06004167 /* Delete our gendisk structure, and cleanup the blk queue. */
Asai Thambi S P58c49df2013-01-11 18:47:12 +05304168 if (dd->disk) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304169 dev_info(&dd->pdev->dev,
4170 "Shutting down %s ...\n", dd->disk->disk_name);
Asai Thambi S P58c49df2013-01-11 18:47:12 +05304171
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08004172 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
4173 del_gendisk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304174 if (dd->disk->queue) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304175 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004176 blk_mq_free_tag_set(&dd->tags);
Asai Thambi SP02b48262015-05-11 15:48:00 -07004177 }
4178 put_disk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304179 dd->disk = NULL;
4180 dd->queue = NULL;
4181 }
Asai Thambi S P8182b492012-04-09 08:35:38 +02004182
4183 spin_lock(&rssd_index_lock);
4184 ida_remove(&rssd_index_ida, dd->index);
4185 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004186 return 0;
4187}
4188
Jens Axboe63166682011-09-27 21:27:43 -06004189static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004190{
4191 dev_info(&dd->pdev->dev,
4192 "Suspending %s ...\n", dd->disk->disk_name);
4193 mtip_hw_suspend(dd);
4194 return 0;
4195}
4196
Jens Axboe63166682011-09-27 21:27:43 -06004197static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004198{
4199 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4200 dd->disk->disk_name);
4201 mtip_hw_resume(dd);
4202 return 0;
4203}
4204
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004205static void drop_cpu(int cpu)
4206{
4207 cpu_use[cpu]--;
4208}
4209
4210static int get_least_used_cpu_on_node(int node)
4211{
4212 int cpu, least_used_cpu, least_cnt;
4213 const struct cpumask *node_mask;
4214
4215 node_mask = cpumask_of_node(node);
4216 least_used_cpu = cpumask_first(node_mask);
4217 least_cnt = cpu_use[least_used_cpu];
4218 cpu = least_used_cpu;
4219
4220 for_each_cpu(cpu, node_mask) {
4221 if (cpu_use[cpu] < least_cnt) {
4222 least_used_cpu = cpu;
4223 least_cnt = cpu_use[cpu];
4224 }
4225 }
4226 cpu_use[least_used_cpu]++;
4227 return least_used_cpu;
4228}
4229
4230/* Helper for selecting a node in round robin mode */
4231static inline int mtip_get_next_rr_node(void)
4232{
4233 static int next_node = -1;
4234
4235 if (next_node == -1) {
4236 next_node = first_online_node;
4237 return next_node;
4238 }
4239
4240 next_node = next_online_node(next_node);
4241 if (next_node == MAX_NUMNODES)
4242 next_node = first_online_node;
4243 return next_node;
4244}
4245
Fengguang Wu25bac122013-01-12 15:31:40 +08004246static DEFINE_HANDLER(0);
4247static DEFINE_HANDLER(1);
4248static DEFINE_HANDLER(2);
4249static DEFINE_HANDLER(3);
4250static DEFINE_HANDLER(4);
4251static DEFINE_HANDLER(5);
4252static DEFINE_HANDLER(6);
4253static DEFINE_HANDLER(7);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004254
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004255static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
4256{
4257 int pos;
4258 unsigned short pcie_dev_ctrl;
4259
4260 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
4261 if (pos) {
4262 pci_read_config_word(pdev,
4263 pos + PCI_EXP_DEVCTL,
4264 &pcie_dev_ctrl);
4265 if (pcie_dev_ctrl & (1 << 11) ||
4266 pcie_dev_ctrl & (1 << 4)) {
4267 dev_info(&dd->pdev->dev,
4268 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4269 pdev->vendor, pdev->device);
4270 pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
4271 PCI_EXP_DEVCTL_RELAX_EN);
4272 pci_write_config_word(pdev,
4273 pos + PCI_EXP_DEVCTL,
4274 pcie_dev_ctrl);
4275 }
4276 }
4277}
4278
4279static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev)
4280{
4281 /*
4282 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4283 * device with device id 0x5aXX
4284 */
4285 if (pdev->bus && pdev->bus->self) {
4286 if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI &&
4287 ((pdev->bus->self->device & 0xff00) == 0x5a00)) {
4288 mtip_disable_link_opts(dd, pdev->bus->self);
4289 } else {
4290 /* Check further up the topology */
4291 struct pci_dev *parent_dev = pdev->bus->self;
4292 if (parent_dev->bus &&
4293 parent_dev->bus->parent &&
4294 parent_dev->bus->parent->self &&
4295 parent_dev->bus->parent->self->vendor ==
4296 PCI_VENDOR_ID_ATI &&
4297 (parent_dev->bus->parent->self->device &
4298 0xff00) == 0x5a00) {
4299 mtip_disable_link_opts(dd,
4300 parent_dev->bus->parent->self);
4301 }
4302 }
4303 }
4304}
4305
Sam Bradshaw88523a62011-08-30 08:34:26 -06004306/*
4307 * Called for each supported PCI device detected.
4308 *
4309 * This function allocates the private data structure, enables the
4310 * PCI device and then calls the block layer initialization function.
4311 *
4312 * return value
4313 * 0 on success else an error code.
4314 */
4315static int mtip_pci_probe(struct pci_dev *pdev,
4316 const struct pci_device_id *ent)
4317{
4318 int rv = 0;
4319 struct driver_data *dd = NULL;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004320 char cpu_list[256];
4321 const struct cpumask *node_mask;
4322 int cpu, i = 0, j = 0;
4323 int my_node = NUMA_NO_NODE;
Asai Thambi S P0caff002013-04-03 19:56:21 +05304324 unsigned long flags;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004325
4326 /* Allocate memory for this devices private data. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004327 my_node = pcibus_to_node(pdev->bus);
4328 if (my_node != NUMA_NO_NODE) {
4329 if (!node_online(my_node))
4330 my_node = mtip_get_next_rr_node();
4331 } else {
4332 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4333 my_node = mtip_get_next_rr_node();
4334 }
4335 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4336 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
Jens Axboe7f328902014-03-10 14:29:37 -06004337 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004338
4339 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004340 if (dd == NULL) {
4341 dev_err(&pdev->dev,
4342 "Unable to allocate memory for driver data\n");
4343 return -ENOMEM;
4344 }
4345
Sam Bradshaw88523a62011-08-30 08:34:26 -06004346 /* Attach the private data to this PCI device. */
4347 pci_set_drvdata(pdev, dd);
4348
4349 rv = pcim_enable_device(pdev);
4350 if (rv < 0) {
4351 dev_err(&pdev->dev, "Unable to enable device\n");
4352 goto iomap_err;
4353 }
4354
4355 /* Map BAR5 to memory. */
4356 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4357 if (rv < 0) {
4358 dev_err(&pdev->dev, "Unable to map regions\n");
4359 goto iomap_err;
4360 }
4361
4362 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4363 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4364
4365 if (rv) {
4366 rv = pci_set_consistent_dma_mask(pdev,
4367 DMA_BIT_MASK(32));
4368 if (rv) {
4369 dev_warn(&pdev->dev,
4370 "64-bit DMA enable failed\n");
4371 goto setmask_err;
4372 }
4373 }
4374 }
4375
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004376 /* Copy the info we may need later into the private data structure. */
4377 dd->major = mtip_major;
4378 dd->instance = instance;
4379 dd->pdev = pdev;
4380 dd->numa_node = my_node;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004381
Asai Thambi S P0caff002013-04-03 19:56:21 +05304382 INIT_LIST_HEAD(&dd->online_list);
4383 INIT_LIST_HEAD(&dd->remove_list);
4384
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004385 memset(dd->workq_name, 0, 32);
4386 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4387
4388 dd->isr_workq = create_workqueue(dd->workq_name);
4389 if (!dd->isr_workq) {
4390 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
Wei Yongjund137c832013-03-22 08:58:23 -06004391 rv = -ENOMEM;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004392 goto block_initialize_err;
4393 }
4394
4395 memset(cpu_list, 0, sizeof(cpu_list));
4396
4397 node_mask = cpumask_of_node(dd->numa_node);
4398 if (!cpumask_empty(node_mask)) {
4399 for_each_cpu(cpu, node_mask)
4400 {
4401 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4402 j = strlen(cpu_list);
4403 }
4404
4405 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4406 dd->numa_node,
4407 topology_physical_package_id(cpumask_first(node_mask)),
4408 nr_cpus_node(dd->numa_node),
4409 cpu_list);
4410 } else
4411 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4412
4413 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4414 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4415 cpu_to_node(dd->isr_binding), dd->isr_binding);
4416
4417 /* first worker context always runs in ISR */
4418 dd->work[0].cpu_binding = dd->isr_binding;
4419 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4420 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4421 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4422 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4423 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4424 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4425 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4426
4427 /* Log the bindings */
4428 for_each_present_cpu(cpu) {
4429 memset(cpu_list, 0, sizeof(cpu_list));
4430 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4431 if (dd->work[i].cpu_binding == cpu) {
4432 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4433 j = strlen(cpu_list);
4434 }
4435 }
4436 if (j)
4437 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4438 }
4439
4440 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4441 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4442 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4443 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4444 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4445 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4446 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4447 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4448
4449 pci_set_master(pdev);
Wei Yongjund137c832013-03-22 08:58:23 -06004450 rv = pci_enable_msi(pdev);
4451 if (rv) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004452 dev_warn(&pdev->dev,
4453 "Unable to enable MSI interrupt.\n");
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004454 goto msi_initialize_err;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004455 }
4456
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004457 mtip_fix_ero_nosnoop(dd, pdev);
4458
Sam Bradshaw88523a62011-08-30 08:34:26 -06004459 /* Initialize the block layer. */
4460 rv = mtip_block_initialize(dd);
4461 if (rv < 0) {
4462 dev_err(&pdev->dev,
4463 "Unable to initialize block layer\n");
4464 goto block_initialize_err;
4465 }
4466
4467 /*
4468 * Increment the instance count so that each device has a unique
4469 * instance number.
4470 */
4471 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02004472 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004473 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P6b06d352013-04-03 19:54:35 +05304474 else
4475 rv = 0; /* device in rebuild state, return 0 from probe */
Asai Thambi S P0caff002013-04-03 19:56:21 +05304476
4477 /* Add to online list even if in ftl rebuild */
4478 spin_lock_irqsave(&dev_lock, flags);
4479 list_add(&dd->online_list, &online_list);
4480 spin_unlock_irqrestore(&dev_lock, flags);
4481
Sam Bradshaw88523a62011-08-30 08:34:26 -06004482 goto done;
4483
4484block_initialize_err:
4485 pci_disable_msi(pdev);
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004486
4487msi_initialize_err:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004488 if (dd->isr_workq) {
4489 flush_workqueue(dd->isr_workq);
4490 destroy_workqueue(dd->isr_workq);
4491 drop_cpu(dd->work[0].cpu_binding);
4492 drop_cpu(dd->work[1].cpu_binding);
4493 drop_cpu(dd->work[2].cpu_binding);
4494 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004495setmask_err:
4496 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4497
4498iomap_err:
4499 kfree(dd);
4500 pci_set_drvdata(pdev, NULL);
4501 return rv;
4502done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06004503 return rv;
4504}
4505
4506/*
4507 * Called for each probed device when the device is removed or the
4508 * driver is unloaded.
4509 *
4510 * return value
4511 * None
4512 */
4513static void mtip_pci_remove(struct pci_dev *pdev)
4514{
4515 struct driver_data *dd = pci_get_drvdata(pdev);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004516 unsigned long flags, to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004517
Asai Thambi SP51c65702016-02-24 21:18:10 -08004518 set_bit(MTIP_DDF_REMOVAL_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02004519
Asai Thambi S P0caff002013-04-03 19:56:21 +05304520 spin_lock_irqsave(&dev_lock, flags);
4521 list_del_init(&dd->online_list);
4522 list_add(&dd->remove_list, &removing_list);
4523 spin_unlock_irqrestore(&dev_lock, flags);
4524
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004525 mtip_check_surprise_removal(pdev);
4526 synchronize_irq(dd->pdev->irq);
4527
4528 /* Spin until workers are done */
4529 to = jiffies + msecs_to_jiffies(4000);
4530 do {
4531 msleep(20);
4532 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4533 time_before(jiffies, to));
4534
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004535 if (!dd->sr)
4536 fsync_bdev(dd->bdev);
Asai Thambi SP51c65702016-02-24 21:18:10 -08004537
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004538 if (atomic_read(&dd->irq_workers_active) != 0) {
4539 dev_warn(&dd->pdev->dev,
4540 "Completion workers still active!\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004541 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004542
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004543 blk_set_queue_dying(dd->queue);
Asai Thambi SP51c65702016-02-24 21:18:10 -08004544 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
4545
Sam Bradshaw88523a62011-08-30 08:34:26 -06004546 /* Clean up the block layer. */
4547 mtip_block_remove(dd);
4548
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004549 if (dd->isr_workq) {
4550 flush_workqueue(dd->isr_workq);
4551 destroy_workqueue(dd->isr_workq);
4552 drop_cpu(dd->work[0].cpu_binding);
4553 drop_cpu(dd->work[1].cpu_binding);
4554 drop_cpu(dd->work[2].cpu_binding);
4555 }
4556
Sam Bradshaw88523a62011-08-30 08:34:26 -06004557 pci_disable_msi(pdev);
4558
Asai Thambi S P0caff002013-04-03 19:56:21 +05304559 spin_lock_irqsave(&dev_lock, flags);
4560 list_del_init(&dd->remove_list);
4561 spin_unlock_irqrestore(&dev_lock, flags);
4562
Asai Thambi SP2132a542015-05-11 15:53:18 -07004563 kfree(dd);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004564
Sam Bradshaw88523a62011-08-30 08:34:26 -06004565 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004566 pci_set_drvdata(pdev, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004567}
4568
4569/*
4570 * Called for each probed device when the device is suspended.
4571 *
4572 * return value
4573 * 0 Success
4574 * <0 Error
4575 */
4576static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4577{
4578 int rv = 0;
4579 struct driver_data *dd = pci_get_drvdata(pdev);
4580
4581 if (!dd) {
4582 dev_err(&pdev->dev,
4583 "Driver private datastructure is NULL\n");
4584 return -EFAULT;
4585 }
4586
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004587 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004588
4589 /* Disable ports & interrupts then send standby immediate */
4590 rv = mtip_block_suspend(dd);
4591 if (rv < 0) {
4592 dev_err(&pdev->dev,
4593 "Failed to suspend controller\n");
4594 return rv;
4595 }
4596
4597 /*
4598 * Save the pci config space to pdev structure &
4599 * disable the device
4600 */
4601 pci_save_state(pdev);
4602 pci_disable_device(pdev);
4603
4604 /* Move to Low power state*/
4605 pci_set_power_state(pdev, PCI_D3hot);
4606
4607 return rv;
4608}
4609
4610/*
4611 * Called for each probed device when the device is resumed.
4612 *
4613 * return value
4614 * 0 Success
4615 * <0 Error
4616 */
4617static int mtip_pci_resume(struct pci_dev *pdev)
4618{
4619 int rv = 0;
4620 struct driver_data *dd;
4621
4622 dd = pci_get_drvdata(pdev);
4623 if (!dd) {
4624 dev_err(&pdev->dev,
4625 "Driver private datastructure is NULL\n");
4626 return -EFAULT;
4627 }
4628
4629 /* Move the device to active State */
4630 pci_set_power_state(pdev, PCI_D0);
4631
4632 /* Restore PCI configuration space */
4633 pci_restore_state(pdev);
4634
4635 /* Enable the PCI device*/
4636 rv = pcim_enable_device(pdev);
4637 if (rv < 0) {
4638 dev_err(&pdev->dev,
4639 "Failed to enable card during resume\n");
4640 goto err;
4641 }
4642 pci_set_master(pdev);
4643
4644 /*
4645 * Calls hbaReset, initPort, & startPort function
4646 * then enables interrupts
4647 */
4648 rv = mtip_block_resume(dd);
4649 if (rv < 0)
4650 dev_err(&pdev->dev, "Unable to resume\n");
4651
4652err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004653 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004654
4655 return rv;
4656}
4657
4658/*
4659 * Shutdown routine
4660 *
4661 * return value
4662 * None
4663 */
4664static void mtip_pci_shutdown(struct pci_dev *pdev)
4665{
4666 struct driver_data *dd = pci_get_drvdata(pdev);
4667 if (dd)
4668 mtip_block_shutdown(dd);
4669}
4670
Sam Bradshaw88523a62011-08-30 08:34:26 -06004671/* Table of device ids supported by this driver. */
Benoit Taine9baa3c32014-08-08 15:56:03 +02004672static const struct pci_device_id mtip_pci_tbl[] = {
Asai Thambi S P1a131452012-09-05 22:00:38 +05304673 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4674 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4675 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4676 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4677 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4678 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4679 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
Sam Bradshaw88523a62011-08-30 08:34:26 -06004680 { 0 }
4681};
4682
4683/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004684static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004685 .name = MTIP_DRV_NAME,
4686 .id_table = mtip_pci_tbl,
4687 .probe = mtip_pci_probe,
4688 .remove = mtip_pci_remove,
4689 .suspend = mtip_pci_suspend,
4690 .resume = mtip_pci_resume,
4691 .shutdown = mtip_pci_shutdown,
4692};
4693
4694MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4695
4696/*
4697 * Module initialization function.
4698 *
4699 * Called once when the module is loaded. This function allocates a major
4700 * block device number to the Cyclone devices and registers the PCI layer
4701 * of the driver.
4702 *
4703 * Return value
4704 * 0 on success else error code.
4705 */
4706static int __init mtip_init(void)
4707{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004708 int error;
4709
Asai Thambi S P45422e72012-09-05 22:03:56 +05304710 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004711
Asai Thambi S P0caff002013-04-03 19:56:21 +05304712 spin_lock_init(&dev_lock);
4713
4714 INIT_LIST_HEAD(&online_list);
4715 INIT_LIST_HEAD(&removing_list);
4716
Sam Bradshaw88523a62011-08-30 08:34:26 -06004717 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004718 error = register_blkdev(0, MTIP_DRV_NAME);
4719 if (error <= 0) {
Asai Thambi S P45422e72012-09-05 22:03:56 +05304720 pr_err("Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004721 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004722 return -EBUSY;
4723 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004724 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004725
Asai Thambi S P0caff002013-04-03 19:56:21 +05304726 dfs_parent = debugfs_create_dir("rssd", NULL);
4727 if (IS_ERR_OR_NULL(dfs_parent)) {
4728 pr_warn("Error creating debugfs parent\n");
4729 dfs_parent = NULL;
4730 }
4731 if (dfs_parent) {
4732 dfs_device_status = debugfs_create_file("device_status",
4733 S_IRUGO, dfs_parent, NULL,
4734 &mtip_device_status_fops);
4735 if (IS_ERR_OR_NULL(dfs_device_status)) {
4736 pr_err("Error creating device_status node\n");
4737 dfs_device_status = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004738 }
4739 }
4740
Sam Bradshaw88523a62011-08-30 08:34:26 -06004741 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004742 error = pci_register_driver(&mtip_pci_driver);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004743 if (error) {
4744 debugfs_remove(dfs_parent);
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004745 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004746 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004747
4748 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004749}
4750
4751/*
4752 * Module de-initialization function.
4753 *
4754 * Called once when the module is unloaded. This function deallocates
4755 * the major block device number allocated by mtip_init() and
4756 * unregisters the PCI layer of the driver.
4757 *
4758 * Return value
4759 * none
4760 */
4761static void __exit mtip_exit(void)
4762{
Sam Bradshaw88523a62011-08-30 08:34:26 -06004763 /* Release the allocated major block device number. */
4764 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4765
4766 /* Unregister the PCI driver. */
4767 pci_unregister_driver(&mtip_pci_driver);
Asai Thambi S Paf5ded82014-04-16 20:30:16 -07004768
4769 debugfs_remove_recursive(dfs_parent);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004770}
4771
4772MODULE_AUTHOR("Micron Technology, Inc");
4773MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4774MODULE_LICENSE("GPL");
4775MODULE_VERSION(MTIP_DRV_VERSION);
4776
4777module_init(mtip_init);
4778module_exit(mtip_exit);