blob: d5277bba0c0217f0f306217ad393f3eeecf9b788 [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_IDA(rssd_index_ida);
122
Asai Thambi S P62ee8c12012-01-04 22:01:32 +0100123static int mtip_block_initialize(struct driver_data *dd);
124
Jens Axboe16d02c02011-09-27 15:50:01 -0600125#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -0600126struct mtip_compat_ide_task_request_s {
127 __u8 io_ports[8];
128 __u8 hob_ports[8];
129 ide_reg_valid_t out_flags;
130 ide_reg_valid_t in_flags;
131 int data_phase;
132 int req_cmd;
133 compat_ulong_t out_size;
134 compat_ulong_t in_size;
135};
Jens Axboe16d02c02011-09-27 15:50:01 -0600136#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -0600137
Sam Bradshaw88523a62011-08-30 08:34:26 -0600138/*
Jens Axboe63166682011-09-27 21:27:43 -0600139 * This function check_for_surprise_removal is called
140 * while card is removed from the system and it will
141 * read the vendor id from the configration space
142 *
143 * @pdev Pointer to the pci_dev structure.
144 *
145 * return value
146 * true if device removed, else false
147 */
148static bool mtip_check_surprise_removal(struct pci_dev *pdev)
149{
150 u16 vendor_id = 0;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600151 struct driver_data *dd = pci_get_drvdata(pdev);
152
153 if (dd->sr)
154 return true;
Jens Axboe63166682011-09-27 21:27:43 -0600155
156 /* Read the vendorID from the configuration space */
157 pci_read_config_word(pdev, 0x00, &vendor_id);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600158 if (vendor_id == 0xFFFF) {
159 dd->sr = true;
160 if (dd->queue)
Bart Van Assche4e699cb2018-03-07 17:10:06 -0800161 blk_queue_flag_set(QUEUE_FLAG_DEAD, dd->queue);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600162 else
163 dev_warn(&dd->pdev->dev,
164 "%s: dd->queue is NULL\n", __func__);
Jens Axboe63166682011-09-27 21:27:43 -0600165 return true; /* device removed */
Jens Axboe63166682011-09-27 21:27:43 -0600166 }
167
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600168 return false; /* device present */
Jens Axboe63166682011-09-27 21:27:43 -0600169}
170
Ming Leia4e84aa2017-04-27 07:45:18 -0600171/* we have to use runtime tag to setup command header */
172static void mtip_init_cmd_header(struct request *rq)
173{
174 struct driver_data *dd = rq->q->queuedata;
175 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
Ming Leia4e84aa2017-04-27 07:45:18 -0600176
177 /* Point the command headers at the command tables. */
178 cmd->command_header = dd->port->command_list +
179 (sizeof(struct mtip_cmd_hdr) * rq->tag);
180 cmd->command_header_dma = dd->port->command_list_dma +
181 (sizeof(struct mtip_cmd_hdr) * rq->tag);
182
Ming Lei8a05aa42017-07-05 12:14:27 +0800183 if (test_bit(MTIP_PF_HOST_CAP_64, &dd->port->flags))
Ming Leia4e84aa2017-04-27 07:45:18 -0600184 cmd->command_header->ctbau = __force_bit2int cpu_to_le32((cmd->command_dma >> 16) >> 16);
185
186 cmd->command_header->ctba = __force_bit2int cpu_to_le32(cmd->command_dma & 0xFFFFFFFF);
187}
188
Jens Axboeffc771b2014-05-09 09:42:02 -0600189static struct mtip_cmd *mtip_get_int_command(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600190{
Jens Axboeffc771b2014-05-09 09:42:02 -0600191 struct request *rq;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600192
Asai Thambi SP008e56d2016-02-24 21:21:20 -0800193 if (mtip_check_surprise_removal(dd->pdev))
194 return NULL;
195
Jens Axboe3f5e6a32017-04-28 10:45:08 -0600196 rq = blk_mq_alloc_request(dd->queue, REQ_OP_DRV_IN, BLK_MQ_REQ_RESERVED);
Asai Thambi SP008e56d2016-02-24 21:21:20 -0800197 if (IS_ERR(rq))
198 return NULL;
199
Ming Leia4e84aa2017-04-27 07:45:18 -0600200 /* Internal cmd isn't submitted via .queue_rq */
201 mtip_init_cmd_header(rq);
202
Jens Axboeffc771b2014-05-09 09:42:02 -0600203 return blk_mq_rq_to_pdu(rq);
204}
Sam Bradshaw88523a62011-08-30 08:34:26 -0600205
Jens Axboeffc771b2014-05-09 09:42:02 -0600206static struct mtip_cmd *mtip_cmd_from_tag(struct driver_data *dd,
207 unsigned int tag)
208{
Jens Axboe6f63503c2017-05-02 09:56:00 -0600209 struct blk_mq_hw_ctx *hctx = dd->queue->queue_hw_ctx[0];
Jens Axboeffc771b2014-05-09 09:42:02 -0600210
Jens Axboe6f63503c2017-05-02 09:56:00 -0600211 return blk_mq_rq_to_pdu(blk_mq_tag_to_rq(hctx->tags, tag));
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600212}
213
214/*
Jens Axboe63166682011-09-27 21:27:43 -0600215 * Reset the HBA (without sleeping)
216 *
Jens Axboe63166682011-09-27 21:27:43 -0600217 * @dd Pointer to the driver data structure.
218 *
219 * return value
220 * 0 The reset was successful.
221 * -1 The HBA Reset bit did not clear.
222 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530223static int mtip_hba_reset(struct driver_data *dd)
Jens Axboe63166682011-09-27 21:27:43 -0600224{
225 unsigned long timeout;
226
Jens Axboe63166682011-09-27 21:27:43 -0600227 /* Set the reset bit */
228 writel(HOST_RESET, dd->mmio + HOST_CTL);
229
230 /* Flush */
231 readl(dd->mmio + HOST_CTL);
232
Asai Thambi SP2f17d712015-05-11 15:57:16 -0700233 /*
234 * Spin for up to 10 seconds waiting for reset acknowledgement. Spec
235 * is 1 sec but in LUN failure conditions, up to 10 secs are required
236 */
237 timeout = jiffies + msecs_to_jiffies(10000);
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530238 do {
239 mdelay(10);
240 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
241 return -1;
Jens Axboe63166682011-09-27 21:27:43 -0600242
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530243 } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
244 && time_before(jiffies, timeout));
Asai Thambi S P45038362012-04-09 08:35:38 +0200245
Jens Axboe63166682011-09-27 21:27:43 -0600246 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
247 return -1;
248
249 return 0;
250}
251
252/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600253 * Issue a command to the hardware.
254 *
255 * Set the appropriate bit in the s_active and Command Issue hardware
256 * registers, causing hardware command processing to begin.
257 *
258 * @port Pointer to the port structure.
259 * @tag The tag of the command to be issued.
260 *
261 * return value
262 * None
263 */
264static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
265{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800266 int group = tag >> 5;
267
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800268 /* guard SACT and CI registers */
269 spin_lock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600270 writel((1 << MTIP_TAG_BIT(tag)),
271 port->s_active[MTIP_TAG_INDEX(tag)]);
272 writel((1 << MTIP_TAG_BIT(tag)),
273 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800274 spin_unlock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600275}
276
277/*
Jens Axboe63166682011-09-27 21:27:43 -0600278 * Enable/disable the reception of FIS
279 *
280 * @port Pointer to the port data structure
281 * @enable 1 to enable, 0 to disable
282 *
283 * return value
284 * Previous state: 1 enabled, 0 disabled
285 */
286static int mtip_enable_fis(struct mtip_port *port, int enable)
287{
288 u32 tmp;
289
290 /* enable FIS reception */
291 tmp = readl(port->mmio + PORT_CMD);
292 if (enable)
293 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
294 else
295 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
296
297 /* Flush */
298 readl(port->mmio + PORT_CMD);
299
300 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
301}
302
303/*
304 * Enable/disable the DMA engine
305 *
306 * @port Pointer to the port data structure
307 * @enable 1 to enable, 0 to disable
308 *
309 * return value
310 * Previous state: 1 enabled, 0 disabled.
311 */
312static int mtip_enable_engine(struct mtip_port *port, int enable)
313{
314 u32 tmp;
315
316 /* enable FIS reception */
317 tmp = readl(port->mmio + PORT_CMD);
318 if (enable)
319 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
320 else
321 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
322
323 readl(port->mmio + PORT_CMD);
324 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
325}
326
327/*
328 * Enables the port DMA engine and FIS reception.
329 *
330 * return value
331 * None
332 */
333static inline void mtip_start_port(struct mtip_port *port)
334{
335 /* Enable FIS reception */
336 mtip_enable_fis(port, 1);
337
338 /* Enable the DMA engine */
339 mtip_enable_engine(port, 1);
340}
341
342/*
343 * Deinitialize a port by disabling port interrupts, the DMA engine,
344 * and FIS reception.
345 *
346 * @port Pointer to the port structure
347 *
348 * return value
349 * None
350 */
351static inline void mtip_deinit_port(struct mtip_port *port)
352{
353 /* Disable interrupts on this port */
354 writel(0, port->mmio + PORT_IRQ_MASK);
355
356 /* Disable the DMA engine */
357 mtip_enable_engine(port, 0);
358
359 /* Disable FIS reception */
360 mtip_enable_fis(port, 0);
361}
362
363/*
364 * Initialize a port.
365 *
366 * This function deinitializes the port by calling mtip_deinit_port() and
367 * then initializes it by setting the command header and RX FIS addresses,
368 * clearing the SError register and any pending port interrupts before
369 * re-enabling the default set of port interrupts.
370 *
371 * @port Pointer to the port structure.
372 *
373 * return value
374 * None
375 */
376static void mtip_init_port(struct mtip_port *port)
377{
378 int i;
379 mtip_deinit_port(port);
380
381 /* Program the command list base and FIS base addresses */
382 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
383 writel((port->command_list_dma >> 16) >> 16,
384 port->mmio + PORT_LST_ADDR_HI);
385 writel((port->rxfis_dma >> 16) >> 16,
386 port->mmio + PORT_FIS_ADDR_HI);
Ming Lei8a05aa42017-07-05 12:14:27 +0800387 set_bit(MTIP_PF_HOST_CAP_64, &port->flags);
Jens Axboe63166682011-09-27 21:27:43 -0600388 }
389
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100390 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600391 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100392 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600393
394 /* Clear SError */
395 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
396
397 /* reset the completed registers.*/
398 for (i = 0; i < port->dd->slot_groups; i++)
399 writel(0xFFFFFFFF, port->completed[i]);
400
401 /* Clear any pending interrupts for this port */
Asai Thambi S P6bb688c2012-05-29 18:40:45 -0700402 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
Jens Axboe63166682011-09-27 21:27:43 -0600403
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100404 /* Clear any pending interrupts on the HBA. */
405 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
406 port->dd->mmio + HOST_IRQ_STAT);
407
Jens Axboe63166682011-09-27 21:27:43 -0600408 /* Enable port interrupts */
409 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
410}
411
412/*
413 * Restart a port
414 *
415 * @port Pointer to the port data structure.
416 *
417 * return value
418 * None
419 */
420static void mtip_restart_port(struct mtip_port *port)
421{
422 unsigned long timeout;
423
424 /* Disable the DMA engine */
425 mtip_enable_engine(port, 0);
426
427 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
428 timeout = jiffies + msecs_to_jiffies(500);
429 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
430 && time_before(jiffies, timeout))
431 ;
432
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200433 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200434 return;
435
Jens Axboe63166682011-09-27 21:27:43 -0600436 /*
437 * Chip quirk: escalate to hba reset if
438 * PxCMD.CR not clear after 500 ms
439 */
440 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
441 dev_warn(&port->dd->pdev->dev,
442 "PxCMD.CR not clear, escalating reset\n");
443
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530444 if (mtip_hba_reset(port->dd))
Jens Axboe63166682011-09-27 21:27:43 -0600445 dev_err(&port->dd->pdev->dev,
446 "HBA reset escalation failed.\n");
447
448 /* 30 ms delay before com reset to quiesce chip */
449 mdelay(30);
450 }
451
452 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
453
454 /* Set PxSCTL.DET */
455 writel(readl(port->mmio + PORT_SCR_CTL) |
456 1, port->mmio + PORT_SCR_CTL);
457 readl(port->mmio + PORT_SCR_CTL);
458
459 /* Wait 1 ms to quiesce chip function */
460 timeout = jiffies + msecs_to_jiffies(1);
461 while (time_before(jiffies, timeout))
462 ;
463
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200464 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200465 return;
466
Jens Axboe63166682011-09-27 21:27:43 -0600467 /* Clear PxSCTL.DET */
468 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
469 port->mmio + PORT_SCR_CTL);
470 readl(port->mmio + PORT_SCR_CTL);
471
472 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
473 timeout = jiffies + msecs_to_jiffies(500);
474 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
475 && time_before(jiffies, timeout))
476 ;
477
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200478 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200479 return;
480
Jens Axboe63166682011-09-27 21:27:43 -0600481 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
482 dev_warn(&port->dd->pdev->dev,
483 "COM reset failed\n");
484
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100485 mtip_init_port(port);
486 mtip_start_port(port);
Jens Axboe63166682011-09-27 21:27:43 -0600487
Jens Axboe63166682011-09-27 21:27:43 -0600488}
489
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530490static int mtip_device_reset(struct driver_data *dd)
491{
492 int rv = 0;
493
494 if (mtip_check_surprise_removal(dd->pdev))
495 return 0;
496
497 if (mtip_hba_reset(dd) < 0)
498 rv = -EFAULT;
499
500 mdelay(1);
501 mtip_init_port(dd->port);
502 mtip_start_port(dd->port);
503
504 /* Enable interrupts on the HBA. */
505 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
506 dd->mmio + HOST_CTL);
507 return rv;
508}
509
Jens Axboe63166682011-09-27 21:27:43 -0600510/*
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200511 * Helper function for tag logging
512 */
513static void print_tags(struct driver_data *dd,
514 char *msg,
515 unsigned long *tagbits,
516 int cnt)
517{
518 unsigned char tagmap[128];
519 int group, tagmap_len = 0;
520
521 memset(tagmap, 0, sizeof(tagmap));
522 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
Jens Axboeffc771b2014-05-09 09:42:02 -0600523 tagmap_len += sprintf(tagmap + tagmap_len, "%016lX ",
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200524 tagbits[group-1]);
525 dev_warn(&dd->pdev->dev,
526 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
527}
528
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200529static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
530 dma_addr_t buffer_dma, unsigned int sectors);
531static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
532 struct smart_attr *attrib);
Jens Axboe6f63503c2017-05-02 09:56:00 -0600533
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200534static void mtip_complete_command(struct mtip_cmd *cmd, blk_status_t status)
Jens Axboe6f63503c2017-05-02 09:56:00 -0600535{
536 struct request *req = blk_mq_rq_from_pdu(cmd);
537
538 cmd->status = status;
539 blk_mq_complete_request(req);
540}
541
Sam Bradshaw88523a62011-08-30 08:34:26 -0600542/*
543 * Handle an error.
544 *
545 * @dd Pointer to the DRIVER_DATA structure.
546 *
547 * return value
548 * None
549 */
550static void mtip_handle_tfe(struct driver_data *dd)
551{
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200552 int group, tag, bit, reissue, rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600553 struct mtip_port *port;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200554 struct mtip_cmd *cmd;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600555 u32 completed;
556 struct host_to_dev_fis *fis;
557 unsigned long tagaccum[SLOTBITS_IN_LONGS];
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200558 unsigned int cmd_cnt = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200559 unsigned char *buf;
560 char *fail_reason = NULL;
561 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600562
563 dev_warn(&dd->pdev->dev, "Taskfile error\n");
564
565 port = dd->port;
566
Asai Thambi SPa7806fa2015-05-11 15:49:28 -0700567 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600568 cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700569 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200570 mtip_complete_command(cmd, BLK_STS_IOERR);
Asai Thambi SPe35b9472016-02-24 21:16:21 -0800571 return;
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700572 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600573
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200574 /* clear the tag accumulator */
575 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
576
Sam Bradshaw88523a62011-08-30 08:34:26 -0600577 /* Loop through all the groups */
578 for (group = 0; group < dd->slot_groups; group++) {
579 completed = readl(port->completed[group]);
580
Jens Axboeffc771b2014-05-09 09:42:02 -0600581 dev_warn(&dd->pdev->dev, "g=%u, comp=%x\n", group, completed);
582
Sam Bradshaw88523a62011-08-30 08:34:26 -0600583 /* clear completed status register in the hardware.*/
584 writel(completed, port->completed[group]);
585
Sam Bradshaw88523a62011-08-30 08:34:26 -0600586 /* Process successfully completed commands */
587 for (bit = 0; bit < 32 && completed; bit++) {
588 if (!(completed & (1<<bit)))
589 continue;
590 tag = (group << 5) + bit;
591
592 /* Skip the internal command slot */
593 if (tag == MTIP_TAG_INTERNAL)
594 continue;
595
Jens Axboeffc771b2014-05-09 09:42:02 -0600596 cmd = mtip_cmd_from_tag(dd, tag);
Jens Axboe6f63503c2017-05-02 09:56:00 -0600597 mtip_complete_command(cmd, 0);
598 set_bit(tag, tagaccum);
599 cmd_cnt++;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600600 }
601 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200602
603 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600604
605 /* Restart the port */
606 mdelay(20);
607 mtip_restart_port(port);
608
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200609 /* Trying to determine the cause of the error */
610 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
611 dd->port->log_buf,
612 dd->port->log_buf_dma, 1);
613 if (rv) {
614 dev_warn(&dd->pdev->dev,
615 "Error in READ LOG EXT (10h) command\n");
616 /* non-critical error, don't fail the load */
617 } else {
618 buf = (unsigned char *)dd->port->log_buf;
619 if (buf[259] & 0x1) {
620 dev_info(&dd->pdev->dev,
621 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200622 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200623 fail_all_ncq_write = 1;
624 fail_reason = "write protect";
625 }
626 if (buf[288] == 0xF7) {
627 dev_info(&dd->pdev->dev,
628 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200629 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200630 fail_all_ncq_cmds = 1;
631 fail_reason = "thermal shutdown";
632 }
633 if (buf[288] == 0xBF) {
Asai Thambi SPaae4a032016-02-24 21:18:20 -0800634 set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200635 dev_info(&dd->pdev->dev,
Sam Bradshaw26d58052013-10-03 10:18:05 -0700636 "Drive indicates rebuild has failed. Secure erase required.\n");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200637 fail_all_ncq_cmds = 1;
638 fail_reason = "rebuild failed";
639 }
640 }
641
Sam Bradshaw88523a62011-08-30 08:34:26 -0600642 /* clear the tag accumulator */
643 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
644
645 /* Loop through all the groups */
646 for (group = 0; group < dd->slot_groups; group++) {
647 for (bit = 0; bit < 32; bit++) {
648 reissue = 1;
649 tag = (group << 5) + bit;
Jens Axboeffc771b2014-05-09 09:42:02 -0600650 cmd = mtip_cmd_from_tag(dd, tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600651
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200652 fis = (struct host_to_dev_fis *)cmd->command;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600653
654 /* Should re-issue? */
655 if (tag == MTIP_TAG_INTERNAL ||
656 fis->command == ATA_CMD_SET_FEATURES)
657 reissue = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200658 else {
659 if (fail_all_ncq_cmds ||
660 (fail_all_ncq_write &&
661 fis->command == ATA_CMD_FPDMA_WRITE)) {
662 dev_warn(&dd->pdev->dev,
663 " Fail: %s w/tag %d [%s].\n",
664 fis->command == ATA_CMD_FPDMA_WRITE ?
665 "write" : "read",
666 tag,
667 fail_reason != NULL ?
668 fail_reason : "unknown");
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200669 mtip_complete_command(cmd, BLK_STS_MEDIUM);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200670 continue;
671 }
672 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600673
674 /*
675 * First check if this command has
676 * exceeded its retries.
677 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200678 if (reissue && (cmd->retries-- > 0)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600679
680 set_bit(tag, tagaccum);
681
Sam Bradshaw88523a62011-08-30 08:34:26 -0600682 /* Re-issue the command. */
683 mtip_issue_ncq_command(port, tag);
684
685 continue;
686 }
687
688 /* Retire a command that will not be reissued */
689 dev_warn(&port->dd->pdev->dev,
690 "retiring tag %d\n", tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600691
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200692 mtip_complete_command(cmd, BLK_STS_IOERR);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600693 }
694 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200695 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600696}
697
698/*
699 * Handle a set device bits interrupt
700 */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800701static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
702 u32 completed)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600703{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800704 struct driver_data *dd = port->dd;
705 int tag, bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600706 struct mtip_cmd *command;
707
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800708 if (!completed) {
709 WARN_ON_ONCE(!completed);
710 return;
711 }
712 /* clear completed status register in the hardware.*/
713 writel(completed, port->completed[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600714
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800715 /* Process completed commands. */
716 for (bit = 0; (bit < 32) && completed; bit++) {
717 if (completed & 0x01) {
718 tag = (group << 5) | bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600719
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800720 /* skip internal command slot. */
721 if (unlikely(tag == MTIP_TAG_INTERNAL))
722 continue;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600723
Jens Axboeffc771b2014-05-09 09:42:02 -0600724 command = mtip_cmd_from_tag(dd, tag);
Jens Axboe6f63503c2017-05-02 09:56:00 -0600725 mtip_complete_command(command, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600726 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800727 completed >>= 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600728 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800729
730 /* If last, re-enable interrupts */
731 if (atomic_dec_return(&dd->irq_workers_active) == 0)
732 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600733}
734
735/*
736 * Process legacy pio and d2h interrupts
737 */
738static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
739{
740 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -0600741 struct mtip_cmd *cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600742
Jens Axboe994ff072017-05-02 09:53:04 -0600743 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) && cmd) {
744 int group = MTIP_TAG_INDEX(MTIP_TAG_INTERNAL);
745 int status = readl(port->cmd_issue[group]);
746
Jens Axboe6f63503c2017-05-02 09:56:00 -0600747 if (!(status & (1 << MTIP_TAG_BIT(MTIP_TAG_INTERNAL))))
748 mtip_complete_command(cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600749 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600750}
751
752/*
753 * Demux and handle errors
754 */
755static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
756{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600757 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
758 dev_warn(&dd->pdev->dev,
759 "Clearing PxSERR.DIAG.x\n");
760 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
761 }
762
763 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
764 dev_warn(&dd->pdev->dev,
765 "Clearing PxSERR.DIAG.n\n");
766 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
767 }
768
769 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
770 dev_warn(&dd->pdev->dev,
771 "Port stat errors %x unhandled\n",
772 (port_stat & ~PORT_IRQ_HANDLED));
Asai Thambi S P9b204fb2014-05-20 10:48:56 -0700773 if (mtip_check_surprise_removal(dd->pdev))
774 return;
775 }
776 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR))) {
777 set_bit(MTIP_PF_EH_ACTIVE_BIT, &dd->port->flags);
778 wake_up_interruptible(&dd->port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600779 }
780}
781
782static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
783{
784 struct driver_data *dd = (struct driver_data *) data;
785 struct mtip_port *port = dd->port;
786 u32 hba_stat, port_stat;
787 int rv = IRQ_NONE;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800788 int do_irq_enable = 1, i, workers;
789 struct mtip_work *twork;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600790
791 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
792 if (hba_stat) {
793 rv = IRQ_HANDLED;
794
795 /* Acknowledge the interrupt status on the port.*/
796 port_stat = readl(port->mmio + PORT_IRQ_STAT);
Asai Thambi SP2132a542015-05-11 15:53:18 -0700797 if (unlikely(port_stat == 0xFFFFFFFF)) {
798 mtip_check_surprise_removal(dd->pdev);
799 return IRQ_HANDLED;
800 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600801 writel(port_stat, port->mmio + PORT_IRQ_STAT);
802
803 /* Demux port status */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800804 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
805 do_irq_enable = 0;
806 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
807
808 /* Start at 1: group zero is always local? */
809 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
810 i++) {
811 twork = &dd->work[i];
812 twork->completed = readl(port->completed[i]);
813 if (twork->completed)
814 workers++;
815 }
816
817 atomic_set(&dd->irq_workers_active, workers);
818 if (workers) {
819 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
820 twork = &dd->work[i];
821 if (twork->completed)
822 queue_work_on(
823 twork->cpu_binding,
824 dd->isr_workq,
825 &twork->work);
826 }
827
828 if (likely(dd->work[0].completed))
829 mtip_workq_sdbfx(port, 0,
830 dd->work[0].completed);
831
832 } else {
833 /*
834 * Chip quirk: SDB interrupt but nothing
835 * to complete
836 */
837 do_irq_enable = 1;
838 }
839 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600840
841 if (unlikely(port_stat & PORT_IRQ_ERR)) {
842 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600843 /* don't proceed further */
844 return IRQ_HANDLED;
845 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200846 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +0200847 &dd->dd_flag))
848 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600849
850 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
851 }
852
853 if (unlikely(port_stat & PORT_IRQ_LEGACY))
854 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
855 }
856
857 /* acknowledge interrupt */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800858 if (unlikely(do_irq_enable))
859 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600860
861 return rv;
862}
863
864/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600865 * HBA interrupt subroutine.
866 *
867 * @irq IRQ number.
868 * @instance Pointer to the driver data structure.
869 *
870 * return value
871 * IRQ_HANDLED A HBA interrupt was pending and handled.
872 * IRQ_NONE This interrupt was not for the HBA.
873 */
874static irqreturn_t mtip_irq_handler(int irq, void *instance)
875{
876 struct driver_data *dd = instance;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800877
878 return mtip_handle_irq(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600879}
880
881static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
882{
Jens Axboe6f63503c2017-05-02 09:56:00 -0600883 writel(1 << MTIP_TAG_BIT(tag), port->cmd_issue[MTIP_TAG_INDEX(tag)]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600884}
885
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200886static bool mtip_pause_ncq(struct mtip_port *port,
887 struct host_to_dev_fis *fis)
888{
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200889 unsigned long task_file_data;
890
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200891 task_file_data = readl(port->mmio+PORT_TFDATA);
Asai Thambi S P12a166c2012-09-05 22:01:36 +0530892 if ((task_file_data & 1))
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200893 return false;
894
895 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200896 port->ic_pause_timer = jiffies;
897 return true;
898 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
899 (fis->features == 0x03)) {
900 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
901 port->ic_pause_timer = jiffies;
902 return true;
903 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
904 ((fis->command == 0xFC) &&
905 (fis->features == 0x27 || fis->features == 0x72 ||
906 fis->features == 0x62 || fis->features == 0x26))) {
Asai Thambi SPee04bed2015-05-11 15:50:50 -0700907 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
Asai Thambi SPaae4a032016-02-24 21:18:20 -0800908 clear_bit(MTIP_DDF_REBUILD_FAILED_BIT, &port->dd->dd_flag);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200909 /* Com reset after secure erase or lowlevel format */
910 mtip_restart_port(port);
Asai Thambi SP686d8e02015-05-11 15:51:27 -0700911 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200912 return false;
913 }
914
915 return false;
916}
917
Jens Axboebaed5482017-04-28 07:54:06 -0600918static bool mtip_commands_active(struct mtip_port *port)
919{
920 unsigned int active;
921 unsigned int n;
922
923 /*
924 * Ignore s_active bit 0 of array element 0.
925 * This bit will always be set
926 */
927 active = readl(port->s_active[0]) & 0xFFFFFFFE;
928 for (n = 1; n < port->dd->slot_groups; n++)
929 active |= readl(port->s_active[n]);
930
931 return active != 0;
932}
933
Sam Bradshaw88523a62011-08-30 08:34:26 -0600934/*
935 * Wait for port to quiesce
936 *
937 * @port Pointer to port data structure
938 * @timeout Max duration to wait (ms)
939 *
940 * return value
941 * 0 Success
942 * -EBUSY Commands still active
943 */
Jens Axboe8afdd942017-04-27 16:32:41 -0600944static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600945{
946 unsigned long to;
Jens Axboebaed5482017-04-28 07:54:06 -0600947 bool active = true;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600948
Sagi Grimberg436c15a2017-07-04 10:00:41 +0300949 blk_mq_quiesce_queue(port->dd->queue);
Jens Axboe9acf03c2014-05-14 08:22:56 -0600950
Sam Bradshaw88523a62011-08-30 08:34:26 -0600951 to = jiffies + msecs_to_jiffies(timeout);
952 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200953 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
Jens Axboe8afdd942017-04-27 16:32:41 -0600954 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100955 msleep(20);
956 continue; /* svc thd is actively issuing commands */
957 }
Asai Thambi S P9b204fb2014-05-20 10:48:56 -0700958
Jens Axboe8afdd942017-04-27 16:32:41 -0600959 msleep(100);
Asai Thambi SP008e56d2016-02-24 21:21:20 -0800960
Asai Thambi S P9b204fb2014-05-20 10:48:56 -0700961 if (mtip_check_surprise_removal(port->dd->pdev))
962 goto err_fault;
Asai Thambi S P9b204fb2014-05-20 10:48:56 -0700963
Jens Axboebaed5482017-04-28 07:54:06 -0600964 active = mtip_commands_active(port);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600965 if (!active)
966 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600967 } while (time_before(jiffies, to));
968
Sagi Grimberg436c15a2017-07-04 10:00:41 +0300969 blk_mq_unquiesce_queue(port->dd->queue);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600970 return active ? -EBUSY : 0;
Jens Axboe9acf03c2014-05-14 08:22:56 -0600971err_fault:
Sagi Grimberg436c15a2017-07-04 10:00:41 +0300972 blk_mq_unquiesce_queue(port->dd->queue);
Jens Axboe9acf03c2014-05-14 08:22:56 -0600973 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600974}
975
Jens Axboe3f5e6a32017-04-28 10:45:08 -0600976struct mtip_int_cmd {
977 int fis_len;
978 dma_addr_t buffer;
979 int buf_len;
980 u32 opts;
981};
982
Sam Bradshaw88523a62011-08-30 08:34:26 -0600983/*
984 * Execute an internal command and wait for the completion.
985 *
986 * @port Pointer to the port data structure.
987 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100988 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -0600989 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100990 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -0600991 * @opts Command header options, excluding the FIS length
992 * and the number of PRD entries.
993 * @timeout Time in ms to wait for the command to complete.
994 *
995 * return value
996 * 0 Command completed successfully.
997 * -EFAULT The buffer address is not correctly aligned.
998 * -EBUSY Internal command or other IO in progress.
999 * -EAGAIN Time out waiting for command to complete.
1000 */
1001static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001002 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001003 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001004 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001005 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001006 u32 opts,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001007 unsigned long timeout)
1008{
Jens Axboeffc771b2014-05-09 09:42:02 -06001009 struct mtip_cmd *int_cmd;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301010 struct driver_data *dd = port->dd;
Jens Axboe3f5e6a32017-04-28 10:45:08 -06001011 struct request *rq;
1012 struct mtip_int_cmd icmd = {
1013 .fis_len = fis_len,
1014 .buffer = buffer,
1015 .buf_len = buf_len,
1016 .opts = opts
1017 };
Jens Axboeffc771b2014-05-09 09:42:02 -06001018 int rv = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001019
1020 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1021 if (buffer & 0x00000007) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301022 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001023 return -EFAULT;
1024 }
1025
Jens Axboeffc771b2014-05-09 09:42:02 -06001026 int_cmd = mtip_get_int_command(dd);
Asai Thambi SP008e56d2016-02-24 21:21:20 -08001027 if (!int_cmd) {
1028 dbg_printk(MTIP_DRV_NAME "Unable to allocate tag for PIO cmd\n");
1029 return -EFAULT;
1030 }
Jens Axboe3f5e6a32017-04-28 10:45:08 -06001031 rq = blk_mq_rq_from_pdu(int_cmd);
Jens Axboe6f63503c2017-05-02 09:56:00 -06001032 rq->special = &icmd;
Jens Axboeffc771b2014-05-09 09:42:02 -06001033
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001034 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001035
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001036 if (fis->command == ATA_CMD_SEC_ERASE_PREP)
1037 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1038
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301039 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001040
Jens Axboe0f6422a2017-04-27 16:29:26 -06001041 if (fis->command != ATA_CMD_STANDBYNOW1) {
1042 /* wait for io to complete if non atomic */
Jens Axboe8afdd942017-04-27 16:32:41 -06001043 if (mtip_quiesce_io(port, MTIP_QUIESCE_IO_TIMEOUT_MS) < 0) {
Jens Axboe0f6422a2017-04-27 16:29:26 -06001044 dev_warn(&dd->pdev->dev, "Failed to quiesce IO\n");
Jens Axboe6f63503c2017-05-02 09:56:00 -06001045 blk_mq_free_request(rq);
Jens Axboe0f6422a2017-04-27 16:29:26 -06001046 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1047 wake_up_interruptible(&port->svc_wait);
1048 return -EBUSY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001049 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001050 }
1051
1052 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001053 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001054
Jens Axboe3f5e6a32017-04-28 10:45:08 -06001055 rq->timeout = timeout;
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001056
Jens Axboe3f5e6a32017-04-28 10:45:08 -06001057 /* insert request and run queue */
Jens Axboe6f63503c2017-05-02 09:56:00 -06001058 blk_execute_rq(rq->q, NULL, rq, true);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001059
Jens Axboe8c66ac6a2017-06-23 09:18:54 -06001060 if (int_cmd->status) {
1061 dev_err(&dd->pdev->dev, "Internal command [%02X] failed %d\n",
1062 fis->command, int_cmd->status);
1063 rv = -EIO;
Asai Thambi SPaae4a032016-02-24 21:18:20 -08001064
Jens Axboe0f6422a2017-04-27 16:29:26 -06001065 if (mtip_check_surprise_removal(dd->pdev) ||
1066 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1067 &dd->dd_flag)) {
1068 dev_err(&dd->pdev->dev,
1069 "Internal command [%02X] wait returned due to SR\n",
1070 fis->command);
1071 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301072 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001073 }
Jens Axboe0f6422a2017-04-27 16:29:26 -06001074 mtip_device_reset(dd); /* recover from timeout issue */
1075 rv = -EAGAIN;
1076 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001077 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001078
Jens Axboe994ff072017-05-02 09:53:04 -06001079 if (readl(port->cmd_issue[MTIP_TAG_INDEX(MTIP_TAG_INTERNAL)])
1080 & (1 << MTIP_TAG_BIT(MTIP_TAG_INTERNAL))) {
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001081 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301082 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1083 mtip_device_reset(dd);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001084 rv = -EAGAIN;
1085 }
1086 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001087exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001088 /* Clear the allocated and active bits for the internal command. */
Jens Axboe6f63503c2017-05-02 09:56:00 -06001089 blk_mq_free_request(rq);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001090 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001091 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1092 /* NCQ paused */
1093 return rv;
1094 }
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001095 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001096
1097 return rv;
1098}
1099
1100/*
1101 * Byte-swap ATA ID strings.
1102 *
1103 * ATA identify data contains strings in byte-swapped 16-bit words.
1104 * They must be swapped (on all architectures) to be usable as C strings.
1105 * This function swaps bytes in-place.
1106 *
1107 * @buf The buffer location of the string
1108 * @len The number of bytes to swap
1109 *
1110 * return value
1111 * None
1112 */
1113static inline void ata_swap_string(u16 *buf, unsigned int len)
1114{
1115 int i;
1116 for (i = 0; i < (len/2); i++)
1117 be16_to_cpus(&buf[i]);
1118}
1119
Asai Thambi S P670a6412014-04-16 20:27:54 -07001120static void mtip_set_timeout(struct driver_data *dd,
1121 struct host_to_dev_fis *fis,
1122 unsigned int *timeout, u8 erasemode)
1123{
1124 switch (fis->command) {
1125 case ATA_CMD_DOWNLOAD_MICRO:
1126 *timeout = 120000; /* 2 minutes */
1127 break;
1128 case ATA_CMD_SEC_ERASE_UNIT:
1129 case 0xFC:
1130 if (erasemode)
1131 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
1132 else
1133 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
1134 break;
1135 case ATA_CMD_STANDBYNOW1:
1136 *timeout = 120000; /* 2 minutes */
1137 break;
1138 case 0xF7:
1139 case 0xFA:
1140 *timeout = 60000; /* 60 seconds */
1141 break;
1142 case ATA_CMD_SMART:
1143 *timeout = 15000; /* 15 seconds */
1144 break;
1145 default:
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001146 *timeout = MTIP_IOCTL_CMD_TIMEOUT_MS;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001147 break;
1148 }
1149}
1150
Sam Bradshaw88523a62011-08-30 08:34:26 -06001151/*
1152 * Request the device identity information.
1153 *
1154 * If a user space buffer is not specified, i.e. is NULL, the
1155 * identify information is still read from the drive and placed
1156 * into the identify data buffer (@e port->identify) in the
1157 * port data structure.
1158 * When the identify buffer contains valid identify information @e
1159 * port->identify_valid is non-zero.
1160 *
1161 * @port Pointer to the port structure.
1162 * @user_buffer A user space buffer where the identify data should be
1163 * copied.
1164 *
1165 * return value
1166 * 0 Command completed successfully.
1167 * -EFAULT An error occurred while coping data to the user buffer.
1168 * -1 Command failed.
1169 */
1170static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1171{
1172 int rv = 0;
1173 struct host_to_dev_fis fis;
1174
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001175 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001176 return -EFAULT;
1177
Sam Bradshaw88523a62011-08-30 08:34:26 -06001178 /* Build the FIS. */
1179 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1180 fis.type = 0x27;
1181 fis.opts = 1 << 7;
1182 fis.command = ATA_CMD_ID_ATA;
1183
1184 /* Set the identify information as invalid. */
1185 port->identify_valid = 0;
1186
1187 /* Clear the identify information. */
1188 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1189
1190 /* Execute the command. */
1191 if (mtip_exec_internal_command(port,
1192 &fis,
1193 5,
1194 port->identify_dma,
1195 sizeof(u16) * ATA_ID_WORDS,
1196 0,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001197 MTIP_INT_CMD_TIMEOUT_MS)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001198 < 0) {
1199 rv = -1;
1200 goto out;
1201 }
1202
1203 /*
1204 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1205 * perform field-sensitive swapping on the string fields.
1206 * See the kernel use of ata_id_string() for proof of this.
1207 */
1208#ifdef __LITTLE_ENDIAN
1209 ata_swap_string(port->identify + 27, 40); /* model string*/
1210 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1211 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1212#else
1213 {
1214 int i;
1215 for (i = 0; i < ATA_ID_WORDS; i++)
1216 port->identify[i] = le16_to_cpu(port->identify[i]);
1217 }
1218#endif
1219
Sam Bradshaw26d58052013-10-03 10:18:05 -07001220 /* Check security locked state */
1221 if (port->identify[128] & 0x4)
1222 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1223 else
1224 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1225
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301226#ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
Asai Thambi S P15283462013-01-11 14:41:34 +01001227 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1228 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1229 port->dd->trim_supp = true;
1230 else
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301231#endif
Asai Thambi S P15283462013-01-11 14:41:34 +01001232 port->dd->trim_supp = false;
1233
Sam Bradshaw88523a62011-08-30 08:34:26 -06001234 /* Set the identify buffer as valid. */
1235 port->identify_valid = 1;
1236
1237 if (user_buffer) {
1238 if (copy_to_user(
1239 user_buffer,
1240 port->identify,
1241 ATA_ID_WORDS * sizeof(u16))) {
1242 rv = -EFAULT;
1243 goto out;
1244 }
1245 }
1246
1247out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001248 return rv;
1249}
1250
1251/*
1252 * Issue a standby immediate command to the device.
1253 *
1254 * @port Pointer to the port structure.
1255 *
1256 * return value
1257 * 0 Command was executed successfully.
1258 * -1 An error occurred while executing the command.
1259 */
1260static int mtip_standby_immediate(struct mtip_port *port)
1261{
1262 int rv;
1263 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001264 unsigned long start;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001265 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001266
Sam Bradshaw88523a62011-08-30 08:34:26 -06001267 /* Build the FIS. */
1268 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1269 fis.type = 0x27;
1270 fis.opts = 1 << 7;
1271 fis.command = ATA_CMD_STANDBYNOW1;
1272
Asai Thambi S P670a6412014-04-16 20:27:54 -07001273 mtip_set_timeout(port->dd, &fis, &timeout, 0);
1274
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001275 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001276 rv = mtip_exec_internal_command(port,
1277 &fis,
1278 5,
1279 0,
1280 0,
1281 0,
Asai Thambi S P670a6412014-04-16 20:27:54 -07001282 timeout);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001283 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1284 jiffies_to_msecs(jiffies - start));
1285 if (rv)
1286 dev_warn(&port->dd->pdev->dev,
1287 "STANDBY IMMEDIATE command failed.\n");
1288
1289 return rv;
1290}
1291
1292/*
1293 * Issue a READ LOG EXT command to the device.
1294 *
1295 * @port pointer to the port structure.
1296 * @page page number to fetch
1297 * @buffer pointer to buffer
1298 * @buffer_dma dma address corresponding to @buffer
1299 * @sectors page length to fetch, in sectors
1300 *
1301 * return value
1302 * @rv return value from mtip_exec_internal_command()
1303 */
1304static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1305 dma_addr_t buffer_dma, unsigned int sectors)
1306{
1307 struct host_to_dev_fis fis;
1308
1309 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1310 fis.type = 0x27;
1311 fis.opts = 1 << 7;
1312 fis.command = ATA_CMD_READ_LOG_EXT;
1313 fis.sect_count = sectors & 0xFF;
1314 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1315 fis.lba_low = page;
1316 fis.lba_mid = 0;
1317 fis.device = ATA_DEVICE_OBS;
1318
1319 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1320
1321 return mtip_exec_internal_command(port,
1322 &fis,
1323 5,
1324 buffer_dma,
1325 sectors * ATA_SECT_SIZE,
1326 0,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001327 MTIP_INT_CMD_TIMEOUT_MS);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001328}
1329
1330/*
1331 * Issue a SMART READ DATA command to the device.
1332 *
1333 * @port pointer to the port structure.
1334 * @buffer pointer to buffer
1335 * @buffer_dma dma address corresponding to @buffer
1336 *
1337 * return value
1338 * @rv return value from mtip_exec_internal_command()
1339 */
1340static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1341 dma_addr_t buffer_dma)
1342{
1343 struct host_to_dev_fis fis;
1344
1345 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1346 fis.type = 0x27;
1347 fis.opts = 1 << 7;
1348 fis.command = ATA_CMD_SMART;
1349 fis.features = 0xD0;
1350 fis.sect_count = 1;
1351 fis.lba_mid = 0x4F;
1352 fis.lba_hi = 0xC2;
1353 fis.device = ATA_DEVICE_OBS;
1354
1355 return mtip_exec_internal_command(port,
1356 &fis,
1357 5,
1358 buffer_dma,
1359 ATA_SECT_SIZE,
1360 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001361 15000);
1362}
1363
1364/*
1365 * Get the value of a smart attribute
1366 *
1367 * @port pointer to the port structure
1368 * @id attribute number
1369 * @attrib pointer to return attrib information corresponding to @id
1370 *
1371 * return value
1372 * -EINVAL NULL buffer passed or unsupported attribute @id.
1373 * -EPERM Identify data not valid, SMART not supported or not enabled
1374 */
1375static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1376 struct smart_attr *attrib)
1377{
1378 int rv, i;
1379 struct smart_attr *pattr;
1380
1381 if (!attrib)
1382 return -EINVAL;
1383
1384 if (!port->identify_valid) {
1385 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1386 return -EPERM;
1387 }
1388 if (!(port->identify[82] & 0x1)) {
1389 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1390 return -EPERM;
1391 }
1392 if (!(port->identify[85] & 0x1)) {
1393 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1394 return -EPERM;
1395 }
1396
1397 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1398 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1399 if (rv) {
1400 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1401 return rv;
1402 }
1403
1404 pattr = (struct smart_attr *)(port->smart_buf + 2);
1405 for (i = 0; i < 29; i++, pattr++)
1406 if (pattr->attr_id == id) {
1407 memcpy(attrib, pattr, sizeof(struct smart_attr));
1408 break;
1409 }
1410
1411 if (i == 29) {
1412 dev_warn(&port->dd->pdev->dev,
1413 "Query for invalid SMART attribute ID\n");
1414 rv = -EINVAL;
1415 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001416
Sam Bradshaw88523a62011-08-30 08:34:26 -06001417 return rv;
1418}
1419
1420/*
Asai Thambi S P15283462013-01-11 14:41:34 +01001421 * Trim unused sectors
1422 *
1423 * @dd pointer to driver_data structure
1424 * @lba starting lba
1425 * @len # of 512b sectors to trim
Asai Thambi S P15283462013-01-11 14:41:34 +01001426 */
Christoph Hellwig81e66172018-11-09 14:48:56 +01001427static blk_status_t mtip_send_trim(struct driver_data *dd, unsigned int lba,
1428 unsigned int len)
Asai Thambi S P15283462013-01-11 14:41:34 +01001429{
Asai Thambi S P15283462013-01-11 14:41:34 +01001430 u64 tlba, tlen, sect_left;
1431 struct mtip_trim_entry *buf;
1432 dma_addr_t dma_addr;
1433 struct host_to_dev_fis fis;
Christoph Hellwig81e66172018-11-09 14:48:56 +01001434 blk_status_t ret = BLK_STS_OK;
1435 int i;
Asai Thambi S P15283462013-01-11 14:41:34 +01001436
1437 if (!len || dd->trim_supp == false)
Christoph Hellwig81e66172018-11-09 14:48:56 +01001438 return BLK_STS_IOERR;
Asai Thambi S P15283462013-01-11 14:41:34 +01001439
1440 /* Trim request too big */
1441 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1442
1443 /* Trim request not aligned on 4k boundary */
1444 WARN_ON(len % 8 != 0);
1445
1446 /* Warn if vu_trim structure is too big */
1447 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1448
1449 /* Allocate a DMA buffer for the trim structure */
1450 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1451 GFP_KERNEL);
1452 if (!buf)
Christoph Hellwig81e66172018-11-09 14:48:56 +01001453 return BLK_STS_RESOURCE;
Asai Thambi S P15283462013-01-11 14:41:34 +01001454 memset(buf, 0, ATA_SECT_SIZE);
1455
1456 for (i = 0, sect_left = len, tlba = lba;
1457 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1458 i++) {
1459 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1460 MTIP_MAX_TRIM_ENTRY_LEN :
1461 sect_left);
1462 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1463 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1464 tlba += tlen;
1465 sect_left -= tlen;
1466 }
1467 WARN_ON(sect_left != 0);
1468
1469 /* Build the fis */
1470 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1471 fis.type = 0x27;
1472 fis.opts = 1 << 7;
1473 fis.command = 0xfb;
1474 fis.features = 0x60;
1475 fis.sect_count = 1;
1476 fis.device = ATA_DEVICE_OBS;
1477
1478 if (mtip_exec_internal_command(dd->port,
1479 &fis,
1480 5,
1481 dma_addr,
1482 ATA_SECT_SIZE,
1483 0,
Asai Thambi S P15283462013-01-11 14:41:34 +01001484 MTIP_TRIM_TIMEOUT_MS) < 0)
Christoph Hellwig81e66172018-11-09 14:48:56 +01001485 ret = BLK_STS_IOERR;
Asai Thambi S P15283462013-01-11 14:41:34 +01001486
1487 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
Christoph Hellwig81e66172018-11-09 14:48:56 +01001488 return ret;
Asai Thambi S P15283462013-01-11 14:41:34 +01001489}
1490
1491/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001492 * Get the drive capacity.
1493 *
1494 * @dd Pointer to the device data structure.
1495 * @sectors Pointer to the variable that will receive the sector count.
1496 *
1497 * return value
1498 * 1 Capacity was returned successfully.
1499 * 0 The identify information is invalid.
1500 */
Jens Axboe63166682011-09-27 21:27:43 -06001501static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001502{
1503 struct mtip_port *port = dd->port;
1504 u64 total, raw0, raw1, raw2, raw3;
1505 raw0 = port->identify[100];
1506 raw1 = port->identify[101];
1507 raw2 = port->identify[102];
1508 raw3 = port->identify[103];
1509 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1510 *sectors = total;
1511 return (bool) !!port->identify_valid;
1512}
1513
1514/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001515 * Display the identify command data.
1516 *
1517 * @port Pointer to the port data structure.
1518 *
1519 * return value
1520 * None
1521 */
1522static void mtip_dump_identify(struct mtip_port *port)
1523{
1524 sector_t sectors;
1525 unsigned short revid;
1526 char cbuf[42];
1527
1528 if (!port->identify_valid)
1529 return;
1530
1531 strlcpy(cbuf, (char *)(port->identify+10), 21);
1532 dev_info(&port->dd->pdev->dev,
1533 "Serial No.: %s\n", cbuf);
1534
1535 strlcpy(cbuf, (char *)(port->identify+23), 9);
1536 dev_info(&port->dd->pdev->dev,
1537 "Firmware Ver.: %s\n", cbuf);
1538
1539 strlcpy(cbuf, (char *)(port->identify+27), 41);
1540 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1541
Sam Bradshaw26d58052013-10-03 10:18:05 -07001542 dev_info(&port->dd->pdev->dev, "Security: %04x %s\n",
1543 port->identify[128],
1544 port->identify[128] & 0x4 ? "(LOCKED)" : "");
1545
Sam Bradshaw88523a62011-08-30 08:34:26 -06001546 if (mtip_hw_get_capacity(port->dd, &sectors))
1547 dev_info(&port->dd->pdev->dev,
1548 "Capacity: %llu sectors (%llu MB)\n",
1549 (u64)sectors,
1550 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1551
1552 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001553 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001554 case 0x1:
1555 strlcpy(cbuf, "A0", 3);
1556 break;
1557 case 0x3:
1558 strlcpy(cbuf, "A2", 3);
1559 break;
1560 default:
1561 strlcpy(cbuf, "?", 2);
1562 break;
1563 }
1564 dev_info(&port->dd->pdev->dev,
1565 "Card Type: %s\n", cbuf);
1566}
1567
1568/*
1569 * Map the commands scatter list into the command table.
1570 *
1571 * @command Pointer to the command.
1572 * @nents Number of scatter list entries.
1573 *
1574 * return value
1575 * None
1576 */
1577static inline void fill_command_sg(struct driver_data *dd,
1578 struct mtip_cmd *command,
1579 int nents)
1580{
1581 int n;
1582 unsigned int dma_len;
1583 struct mtip_cmd_sg *command_sg;
1584 struct scatterlist *sg = command->sg;
1585
1586 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1587
1588 for (n = 0; n < nents; n++) {
1589 dma_len = sg_dma_len(sg);
1590 if (dma_len > 0x400000)
1591 dev_err(&dd->pdev->dev,
1592 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001593 command_sg->info = __force_bit2int
1594 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1595 command_sg->dba = __force_bit2int
1596 cpu_to_le32(sg_dma_address(sg));
1597 command_sg->dba_upper = __force_bit2int
1598 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001599 command_sg++;
1600 sg++;
1601 }
1602}
1603
1604/*
1605 * @brief Execute a drive command.
1606 *
1607 * return value 0 The command completed successfully.
1608 * return value -1 An error occurred while executing the command.
1609 */
Jens Axboe63166682011-09-27 21:27:43 -06001610static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001611{
1612 struct host_to_dev_fis fis;
1613 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001614 unsigned int to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001615
Sam Bradshaw88523a62011-08-30 08:34:26 -06001616 /* 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 = command[0];
1621 fis.features = command[1];
1622 fis.sect_count = command[2];
1623 fis.sector = command[3];
1624 fis.cyl_low = command[4];
1625 fis.cyl_hi = command[5];
1626 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1627
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001628 mtip_set_timeout(port->dd, &fis, &to, 0);
1629
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001630 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 -06001631 __func__,
1632 command[0],
1633 command[1],
1634 command[2],
1635 command[3],
1636 command[4],
1637 command[5],
1638 command[6]);
1639
1640 /* Execute the command. */
1641 if (mtip_exec_internal_command(port,
1642 &fis,
1643 5,
1644 0,
1645 0,
1646 0,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001647 to) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001648 return -1;
1649 }
1650
1651 command[0] = reply->command; /* Status*/
1652 command[1] = reply->features; /* Error*/
1653 command[4] = reply->cyl_low;
1654 command[5] = reply->cyl_hi;
1655
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001656 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 -06001657 __func__,
1658 command[0],
1659 command[1],
1660 command[4],
1661 command[5]);
1662
Sam Bradshaw88523a62011-08-30 08:34:26 -06001663 return 0;
1664}
1665
1666/*
1667 * @brief Execute a drive command.
1668 *
1669 * @param port Pointer to the port data structure.
1670 * @param command Pointer to the user specified command parameters.
1671 * @param user_buffer Pointer to the user space buffer where read sector
1672 * data should be copied.
1673 *
1674 * return value 0 The command completed successfully.
1675 * return value -EFAULT An error occurred while copying the completion
1676 * data to the user space buffer.
1677 * return value -1 An error occurred while executing the command.
1678 */
Jens Axboe63166682011-09-27 21:27:43 -06001679static int exec_drive_command(struct mtip_port *port, u8 *command,
1680 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001681{
1682 struct host_to_dev_fis fis;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001683 struct host_to_dev_fis *reply;
1684 u8 *buf = NULL;
1685 dma_addr_t dma_addr = 0;
1686 int rv = 0, xfer_sz = command[3];
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001687 unsigned int to;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001688
1689 if (xfer_sz) {
David Milburn97651ea2012-09-12 14:06:12 -05001690 if (!user_buffer)
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001691 return -EFAULT;
1692
1693 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1694 ATA_SECT_SIZE * xfer_sz,
1695 &dma_addr,
1696 GFP_KERNEL);
1697 if (!buf) {
1698 dev_err(&port->dd->pdev->dev,
1699 "Memory allocation failed (%d bytes)\n",
1700 ATA_SECT_SIZE * xfer_sz);
1701 return -ENOMEM;
1702 }
1703 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1704 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001705
Sam Bradshaw88523a62011-08-30 08:34:26 -06001706 /* Build the FIS. */
1707 memset(&fis, 0, sizeof(struct host_to_dev_fis));
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001708 fis.type = 0x27;
1709 fis.opts = 1 << 7;
1710 fis.command = command[0];
Sam Bradshaw88523a62011-08-30 08:34:26 -06001711 fis.features = command[2];
1712 fis.sect_count = command[3];
1713 if (fis.command == ATA_CMD_SMART) {
1714 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001715 fis.cyl_low = 0x4F;
1716 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001717 }
1718
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001719 mtip_set_timeout(port->dd, &fis, &to, 0);
1720
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001721 if (xfer_sz)
1722 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1723 else
1724 reply = (port->rxfis + RX_FIS_D2H_REG);
1725
Sam Bradshaw88523a62011-08-30 08:34:26 -06001726 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001727 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06001728 "feat %x, sectcnt %x\n",
1729 __func__,
1730 command[0],
1731 command[1],
1732 command[2],
1733 command[3]);
1734
Sam Bradshaw88523a62011-08-30 08:34:26 -06001735 /* Execute the command. */
1736 if (mtip_exec_internal_command(port,
1737 &fis,
1738 5,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001739 (xfer_sz ? dma_addr : 0),
1740 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
Sam Bradshaw88523a62011-08-30 08:34:26 -06001741 0,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001742 to)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001743 < 0) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001744 rv = -EFAULT;
1745 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001746 }
1747
1748 /* Collect the completion status. */
1749 command[0] = reply->command; /* Status*/
1750 command[1] = reply->features; /* Error*/
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001751 command[2] = reply->sect_count;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001752
1753 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001754 " %s: Completion Status: stat %x, "
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001755 "err %x, nsect %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001756 __func__,
1757 command[0],
1758 command[1],
1759 command[2]);
1760
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001761 if (xfer_sz) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001762 if (copy_to_user(user_buffer,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001763 buf,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001764 ATA_SECT_SIZE * command[3])) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001765 rv = -EFAULT;
1766 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001767 }
1768 }
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001769exit_drive_command:
1770 if (buf)
1771 dmam_free_coherent(&port->dd->pdev->dev,
1772 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1773 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001774}
1775
1776/*
1777 * Indicates whether a command has a single sector payload.
1778 *
1779 * @command passed to the device to perform the certain event.
1780 * @features passed to the device to perform the certain event.
1781 *
1782 * return value
1783 * 1 command is one that always has a single sector payload,
1784 * regardless of the value in the Sector Count field.
1785 * 0 otherwise
1786 *
1787 */
1788static unsigned int implicit_sector(unsigned char command,
1789 unsigned char features)
1790{
1791 unsigned int rv = 0;
1792
1793 /* list of commands that have an implicit sector count of 1 */
1794 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001795 case ATA_CMD_SEC_SET_PASS:
1796 case ATA_CMD_SEC_UNLOCK:
1797 case ATA_CMD_SEC_ERASE_PREP:
1798 case ATA_CMD_SEC_ERASE_UNIT:
1799 case ATA_CMD_SEC_FREEZE_LOCK:
1800 case ATA_CMD_SEC_DISABLE_PASS:
1801 case ATA_CMD_PMP_READ:
1802 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001803 rv = 1;
1804 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001805 case ATA_CMD_SET_MAX:
1806 if (features == ATA_SET_MAX_UNLOCK)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001807 rv = 1;
1808 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001809 case ATA_CMD_SMART:
1810 if ((features == ATA_SMART_READ_VALUES) ||
1811 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001812 rv = 1;
1813 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001814 case ATA_CMD_CONF_OVERLAY:
1815 if ((features == ATA_DCO_IDENTIFY) ||
1816 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001817 rv = 1;
1818 break;
1819 }
1820 return rv;
1821}
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07001822
Sam Bradshaw88523a62011-08-30 08:34:26 -06001823/*
1824 * Executes a taskfile
1825 * See ide_taskfile_ioctl() for derivation
1826 */
1827static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06001828 void __user *buf,
1829 ide_task_request_t *req_task,
1830 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001831{
1832 struct host_to_dev_fis fis;
1833 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001834 u8 *outbuf = NULL;
1835 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06001836 dma_addr_t outbuf_dma = 0;
1837 dma_addr_t inbuf_dma = 0;
1838 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001839 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001840 unsigned int taskin = 0;
1841 unsigned int taskout = 0;
1842 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07001843 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001844 unsigned int force_single_sector;
1845 unsigned int transfer_size;
1846 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06001847 int intotal = outtotal + req_task->out_size;
Selvan Mani4453bc82012-09-27 14:36:43 +02001848 int erasemode = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001849
1850 taskout = req_task->out_size;
1851 taskin = req_task->in_size;
1852 /* 130560 = 512 * 0xFF*/
Sachin Shuklab425b022016-11-11 14:34:51 +05301853 if (taskin > 130560 || taskout > 130560)
1854 return -EINVAL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001855
1856 if (taskout) {
Al Viro8ed60102016-01-02 14:56:33 -05001857 outbuf = memdup_user(buf + outtotal, taskout);
Sachin Shuklab425b022016-11-11 14:34:51 +05301858 if (IS_ERR(outbuf))
1859 return PTR_ERR(outbuf);
1860
Christoph Hellwigee75fa22018-10-18 15:15:16 +02001861 outbuf_dma = dma_map_single(&dd->pdev->dev, outbuf,
1862 taskout, DMA_TO_DEVICE);
1863 if (dma_mapping_error(&dd->pdev->dev, outbuf_dma)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001864 err = -ENOMEM;
1865 goto abort;
1866 }
1867 dma_buffer = outbuf_dma;
1868 }
1869
1870 if (taskin) {
Al Viro8ed60102016-01-02 14:56:33 -05001871 inbuf = memdup_user(buf + intotal, taskin);
1872 if (IS_ERR(inbuf)) {
1873 err = PTR_ERR(inbuf);
1874 inbuf = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001875 goto abort;
1876 }
Christoph Hellwigee75fa22018-10-18 15:15:16 +02001877 inbuf_dma = dma_map_single(&dd->pdev->dev, inbuf,
1878 taskin, DMA_FROM_DEVICE);
1879 if (dma_mapping_error(&dd->pdev->dev, inbuf_dma)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001880 err = -ENOMEM;
1881 goto abort;
1882 }
1883 dma_buffer = inbuf_dma;
1884 }
1885
1886 /* only supports PIO and non-data commands from this ioctl. */
1887 switch (req_task->data_phase) {
1888 case TASKFILE_OUT:
1889 nsect = taskout / ATA_SECT_SIZE;
1890 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1891 break;
1892 case TASKFILE_IN:
1893 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1894 break;
1895 case TASKFILE_NO_DATA:
1896 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
1897 break;
1898 default:
1899 err = -EINVAL;
1900 goto abort;
1901 }
1902
Sam Bradshaw88523a62011-08-30 08:34:26 -06001903 /* Build the FIS. */
1904 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1905
1906 fis.type = 0x27;
1907 fis.opts = 1 << 7;
1908 fis.command = req_task->io_ports[7];
1909 fis.features = req_task->io_ports[1];
1910 fis.sect_count = req_task->io_ports[2];
1911 fis.lba_low = req_task->io_ports[3];
1912 fis.lba_mid = req_task->io_ports[4];
1913 fis.lba_hi = req_task->io_ports[5];
1914 /* Clear the dev bit*/
1915 fis.device = req_task->io_ports[6] & ~0x10;
1916
1917 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
1918 req_task->in_flags.all =
1919 IDE_TASKFILE_STD_IN_FLAGS |
1920 (IDE_HOB_STD_IN_FLAGS << 8);
1921 fis.lba_low_ex = req_task->hob_ports[3];
1922 fis.lba_mid_ex = req_task->hob_ports[4];
1923 fis.lba_hi_ex = req_task->hob_ports[5];
1924 fis.features_ex = req_task->hob_ports[1];
1925 fis.sect_cnt_ex = req_task->hob_ports[2];
1926
1927 } else {
1928 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
1929 }
1930
1931 force_single_sector = implicit_sector(fis.command, fis.features);
1932
1933 if ((taskin || taskout) && (!fis.sect_count)) {
1934 if (nsect)
1935 fis.sect_count = nsect;
1936 else {
1937 if (!force_single_sector) {
1938 dev_warn(&dd->pdev->dev,
1939 "data movement but "
1940 "sect_count is 0\n");
Colin Ian King698b53b2018-10-30 11:29:42 +00001941 err = -EINVAL;
1942 goto abort;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001943 }
1944 }
1945 }
1946
1947 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001948 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06001949 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
1950 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001951 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001952 fis.command,
1953 fis.features,
1954 fis.sect_count,
1955 fis.lba_low,
1956 fis.lba_mid,
1957 fis.lba_hi,
1958 fis.device);
1959
Selvan Mani4453bc82012-09-27 14:36:43 +02001960 /* check for erase mode support during secure erase.*/
Selvan Mani32087952012-11-07 06:03:37 -07001961 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
1962 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
Selvan Mani4453bc82012-09-27 14:36:43 +02001963 erasemode = 1;
1964 }
1965
1966 mtip_set_timeout(dd, &fis, &timeout, erasemode);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001967
1968 /* Determine the correct transfer size.*/
1969 if (force_single_sector)
1970 transfer_size = ATA_SECT_SIZE;
1971 else
1972 transfer_size = ATA_SECT_SIZE * fis.sect_count;
1973
1974 /* Execute the command.*/
1975 if (mtip_exec_internal_command(dd->port,
1976 &fis,
1977 5,
1978 dma_buffer,
1979 transfer_size,
1980 0,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001981 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001982 err = -EIO;
1983 goto abort;
1984 }
1985
1986 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
1987
1988 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
1989 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
1990 req_task->io_ports[7] = reply->control;
1991 } else {
1992 reply = dd->port->rxfis + RX_FIS_D2H_REG;
1993 req_task->io_ports[7] = reply->command;
1994 }
1995
1996 /* reclaim the DMA buffers.*/
1997 if (inbuf_dma)
Christoph Hellwigee75fa22018-10-18 15:15:16 +02001998 dma_unmap_single(&dd->pdev->dev, inbuf_dma, taskin,
1999 DMA_FROM_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002000 if (outbuf_dma)
Christoph Hellwigee75fa22018-10-18 15:15:16 +02002001 dma_unmap_single(&dd->pdev->dev, outbuf_dma, taskout,
2002 DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002003 inbuf_dma = 0;
2004 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002005
2006 /* return the ATA registers to the caller.*/
2007 req_task->io_ports[1] = reply->features;
2008 req_task->io_ports[2] = reply->sect_count;
2009 req_task->io_ports[3] = reply->lba_low;
2010 req_task->io_ports[4] = reply->lba_mid;
2011 req_task->io_ports[5] = reply->lba_hi;
2012 req_task->io_ports[6] = reply->device;
2013
2014 if (req_task->out_flags.all & 1) {
2015
2016 req_task->hob_ports[3] = reply->lba_low_ex;
2017 req_task->hob_ports[4] = reply->lba_mid_ex;
2018 req_task->hob_ports[5] = reply->lba_hi_ex;
2019 req_task->hob_ports[1] = reply->features_ex;
2020 req_task->hob_ports[2] = reply->sect_cnt_ex;
2021 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002022 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002023 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002024 "err %x, sect_cnt %x, lbalo %x,"
2025 "lbamid %x, lbahi %x, dev %x\n",
2026 __func__,
2027 req_task->io_ports[7],
2028 req_task->io_ports[1],
2029 req_task->io_ports[2],
2030 req_task->io_ports[3],
2031 req_task->io_ports[4],
2032 req_task->io_ports[5],
2033 req_task->io_ports[6]);
2034
Sam Bradshaw88523a62011-08-30 08:34:26 -06002035 if (taskout) {
2036 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2037 err = -EFAULT;
2038 goto abort;
2039 }
2040 }
2041 if (taskin) {
2042 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2043 err = -EFAULT;
2044 goto abort;
2045 }
2046 }
2047abort:
2048 if (inbuf_dma)
Christoph Hellwigee75fa22018-10-18 15:15:16 +02002049 dma_unmap_single(&dd->pdev->dev, inbuf_dma, taskin,
2050 DMA_FROM_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002051 if (outbuf_dma)
Christoph Hellwigee75fa22018-10-18 15:15:16 +02002052 dma_unmap_single(&dd->pdev->dev, outbuf_dma, taskout,
2053 DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002054 kfree(outbuf);
2055 kfree(inbuf);
2056
2057 return err;
2058}
2059
2060/*
2061 * Handle IOCTL calls from the Block Layer.
2062 *
2063 * This function is called by the Block Layer when it receives an IOCTL
2064 * command that it does not understand. If the IOCTL command is not supported
2065 * this function returns -ENOTTY.
2066 *
2067 * @dd Pointer to the driver data structure.
2068 * @cmd IOCTL command passed from the Block Layer.
2069 * @arg IOCTL argument passed from the Block Layer.
2070 *
2071 * return value
2072 * 0 The IOCTL completed successfully.
2073 * -ENOTTY The specified command is not supported.
2074 * -EFAULT An error occurred copying data to a user space buffer.
2075 * -EIO An error occurred while executing the command.
2076 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002077static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2078 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002079{
2080 switch (cmd) {
2081 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002082 {
2083 if (copy_to_user((void __user *)arg, dd->port->identify,
2084 sizeof(u16) * ATA_ID_WORDS))
2085 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002086 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002087 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002088 case HDIO_DRIVE_CMD:
2089 {
2090 u8 drive_command[4];
2091
2092 /* Copy the user command info to our buffer. */
2093 if (copy_from_user(drive_command,
2094 (void __user *) arg,
2095 sizeof(drive_command)))
2096 return -EFAULT;
2097
2098 /* Execute the drive command. */
2099 if (exec_drive_command(dd->port,
2100 drive_command,
2101 (void __user *) (arg+4)))
2102 return -EIO;
2103
2104 /* Copy the status back to the users buffer. */
2105 if (copy_to_user((void __user *) arg,
2106 drive_command,
2107 sizeof(drive_command)))
2108 return -EFAULT;
2109
2110 break;
2111 }
2112 case HDIO_DRIVE_TASK:
2113 {
2114 u8 drive_command[7];
2115
2116 /* Copy the user command info to our buffer. */
2117 if (copy_from_user(drive_command,
2118 (void __user *) arg,
2119 sizeof(drive_command)))
2120 return -EFAULT;
2121
2122 /* Execute the drive command. */
2123 if (exec_drive_task(dd->port, drive_command))
2124 return -EIO;
2125
2126 /* Copy the status back to the users buffer. */
2127 if (copy_to_user((void __user *) arg,
2128 drive_command,
2129 sizeof(drive_command)))
2130 return -EFAULT;
2131
2132 break;
2133 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002134 case HDIO_DRIVE_TASKFILE: {
2135 ide_task_request_t req_task;
2136 int ret, outtotal;
2137
2138 if (copy_from_user(&req_task, (void __user *) arg,
2139 sizeof(req_task)))
2140 return -EFAULT;
2141
2142 outtotal = sizeof(req_task);
2143
2144 ret = exec_drive_taskfile(dd, (void __user *) arg,
2145 &req_task, outtotal);
2146
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002147 if (copy_to_user((void __user *) arg, &req_task,
2148 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002149 return -EFAULT;
2150
2151 return ret;
2152 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002153
2154 default:
2155 return -EINVAL;
2156 }
2157 return 0;
2158}
2159
2160/*
2161 * Submit an IO to the hw
2162 *
2163 * This function is called by the block layer to issue an io
2164 * to the device. Upon completion, the callback function will
2165 * be called with the data parameter passed as the callback data.
2166 *
2167 * @dd Pointer to the driver data structure.
2168 * @start First sector to read.
2169 * @nsect Number of sectors to read.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002170 * @tag The tag of this read command.
2171 * @callback Pointer to the function that should be called
2172 * when the read completes.
2173 * @data Callback data passed to the callback function
2174 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002175 * @dir Direction (read or write)
2176 *
2177 * return value
2178 * None
2179 */
Jens Axboeffc771b2014-05-09 09:42:02 -06002180static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq,
Christoph Hellwigb5fa0e92018-11-09 14:48:54 +01002181 struct mtip_cmd *command,
Jens Axboeffc771b2014-05-09 09:42:02 -06002182 struct blk_mq_hw_ctx *hctx)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002183{
2184 struct host_to_dev_fis *fis;
2185 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -06002186 int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2187 u64 start = blk_rq_pos(rq);
2188 unsigned int nsect = blk_rq_sectors(rq);
Christoph Hellwigb5fa0e92018-11-09 14:48:54 +01002189 unsigned int nents;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002190
2191 /* Map the scatter list for DMA access */
Christoph Hellwigb5fa0e92018-11-09 14:48:54 +01002192 nents = blk_rq_map_sg(hctx->queue, rq, command->sg);
Asai Thambi S P45038362012-04-09 08:35:38 +02002193 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002194
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002195 prefetch(&port->flags);
2196
Sam Bradshaw88523a62011-08-30 08:34:26 -06002197 command->scatter_ents = nents;
2198
2199 /*
2200 * The number of retries for this command before it is
2201 * reported as a failure to the upper layers.
2202 */
2203 command->retries = MTIP_MAX_RETRIES;
2204
2205 /* Fill out fis */
2206 fis = command->command;
2207 fis->type = 0x27;
2208 fis->opts = 1 << 7;
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002209 if (dma_dir == DMA_FROM_DEVICE)
Jens Axboeffc771b2014-05-09 09:42:02 -06002210 fis->command = ATA_CMD_FPDMA_READ;
2211 else
2212 fis->command = ATA_CMD_FPDMA_WRITE;
Selvan Manieda45312012-11-07 06:03:53 -07002213 fis->lba_low = start & 0xFF;
2214 fis->lba_mid = (start >> 8) & 0xFF;
2215 fis->lba_hi = (start >> 16) & 0xFF;
2216 fis->lba_low_ex = (start >> 24) & 0xFF;
2217 fis->lba_mid_ex = (start >> 32) & 0xFF;
2218 fis->lba_hi_ex = (start >> 40) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002219 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002220 fis->features = nsect & 0xFF;
2221 fis->features_ex = (nsect >> 8) & 0xFF;
Jens Axboeffc771b2014-05-09 09:42:02 -06002222 fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002223 fis->sect_cnt_ex = 0;
2224 fis->control = 0;
2225 fis->res2 = 0;
2226 fis->res3 = 0;
2227 fill_command_sg(dd, command, nents);
2228
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002229 if (unlikely(command->unaligned))
Asai Thambi S P2077d942013-04-29 21:19:49 +02002230 fis->device |= 1 << 7;
2231
Sam Bradshaw88523a62011-08-30 08:34:26 -06002232 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002233 command->command_header->opts =
2234 __force_bit2int cpu_to_le32(
2235 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002236 command->command_header->byte_count = 0;
2237
Asai Thambi S P45038362012-04-09 08:35:38 +02002238 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002239
2240 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002241 * To prevent this command from being issued
2242 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002243 */
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002244 if (unlikely(port->flags & MTIP_PF_PAUSE_IO)) {
Jens Axboeffc771b2014-05-09 09:42:02 -06002245 set_bit(rq->tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002246 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002247 return;
2248 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002249
2250 /* Issue the command to the hardware */
Jens Axboeffc771b2014-05-09 09:42:02 -06002251 mtip_issue_ncq_command(port, rq->tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002252}
2253
2254/*
Asai Thambi S P7412ff12012-06-04 12:43:03 -07002255 * Sysfs status dump.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002256 *
2257 * @dev Pointer to the device structure, passed by the kernrel.
2258 * @attr Pointer to the device_attribute structure passed by the kernel.
2259 * @buf Pointer to the char buffer that will receive the stats info.
2260 *
2261 * return value
2262 * The size, in bytes, of the data copied into buf.
2263 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002264static ssize_t mtip_hw_show_status(struct device *dev,
2265 struct device_attribute *attr,
2266 char *buf)
2267{
2268 struct driver_data *dd = dev_to_disk(dev)->private_data;
2269 int size = 0;
2270
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002271 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002272 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002273 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002274 size += sprintf(buf, "%s", "write_protect\n");
2275 else
2276 size += sprintf(buf, "%s", "online\n");
2277
2278 return size;
2279}
2280
Joe Perches5657a812018-05-24 13:38:59 -06002281static DEVICE_ATTR(status, 0444, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002282
Asai Thambi S P0caff002013-04-03 19:56:21 +05302283/* debugsfs entries */
2284
2285static ssize_t show_device_status(struct device_driver *drv, char *buf)
2286{
2287 int size = 0;
2288 struct driver_data *dd, *tmp;
2289 unsigned long flags;
2290 char id_buf[42];
2291 u16 status = 0;
2292
2293 spin_lock_irqsave(&dev_lock, flags);
2294 size += sprintf(&buf[size], "Devices Present:\n");
2295 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002296 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302297 if (dd->port &&
2298 dd->port->identify &&
2299 dd->port->identify_valid) {
2300 strlcpy(id_buf,
2301 (char *) (dd->port->identify + 10), 21);
2302 status = *(dd->port->identify + 141);
2303 } else {
2304 memset(id_buf, 0, 42);
2305 status = 0;
2306 }
2307
2308 if (dd->port &&
2309 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2310 size += sprintf(&buf[size],
2311 " device %s %s (ftl rebuild %d %%)\n",
2312 dev_name(&dd->pdev->dev),
2313 id_buf,
2314 status);
2315 } else {
2316 size += sprintf(&buf[size],
2317 " device %s %s\n",
2318 dev_name(&dd->pdev->dev),
2319 id_buf);
2320 }
2321 }
2322 }
2323
2324 size += sprintf(&buf[size], "Devices Being Removed:\n");
2325 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002326 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302327 if (dd->port &&
2328 dd->port->identify &&
2329 dd->port->identify_valid) {
2330 strlcpy(id_buf,
2331 (char *) (dd->port->identify+10), 21);
2332 status = *(dd->port->identify + 141);
2333 } else {
2334 memset(id_buf, 0, 42);
2335 status = 0;
2336 }
2337
2338 if (dd->port &&
2339 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2340 size += sprintf(&buf[size],
2341 " device %s %s (ftl rebuild %d %%)\n",
2342 dev_name(&dd->pdev->dev),
2343 id_buf,
2344 status);
2345 } else {
2346 size += sprintf(&buf[size],
2347 " device %s %s\n",
2348 dev_name(&dd->pdev->dev),
2349 id_buf);
2350 }
2351 }
2352 }
2353 spin_unlock_irqrestore(&dev_lock, flags);
2354
2355 return size;
2356}
2357
2358static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2359 size_t len, loff_t *offset)
2360{
David Milburnc8afd0d2013-05-23 16:23:45 -05002361 struct driver_data *dd = (struct driver_data *)f->private_data;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302362 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002363 char *buf;
2364 int rv = 0;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302365
2366 if (!len || *offset)
2367 return 0;
2368
David Milburnc8afd0d2013-05-23 16:23:45 -05002369 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2370 if (!buf) {
2371 dev_err(&dd->pdev->dev,
2372 "Memory allocation: status buffer\n");
2373 return -ENOMEM;
2374 }
2375
Asai Thambi S P0caff002013-04-03 19:56:21 +05302376 size += show_device_status(NULL, buf);
2377
2378 *offset = size <= len ? size : len;
2379 size = copy_to_user(ubuf, buf, *offset);
2380 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002381 rv = -EFAULT;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302382
David Milburnc8afd0d2013-05-23 16:23:45 -05002383 kfree(buf);
2384 return rv ? rv : *offset;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302385}
2386
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002387static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2388 size_t len, loff_t *offset)
2389{
2390 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002391 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002392 u32 group_allocated;
2393 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002394 int n, rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002395
2396 if (!len || size)
2397 return 0;
2398
David Milburnc8afd0d2013-05-23 16:23:45 -05002399 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2400 if (!buf) {
2401 dev_err(&dd->pdev->dev,
2402 "Memory allocation: register buffer\n");
2403 return -ENOMEM;
2404 }
2405
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002406 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2407
2408 for (n = dd->slot_groups-1; n >= 0; n--)
2409 size += sprintf(&buf[size], "%08X ",
2410 readl(dd->port->s_active[n]));
2411
2412 size += sprintf(&buf[size], "]\n");
2413 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2414
2415 for (n = dd->slot_groups-1; n >= 0; n--)
2416 size += sprintf(&buf[size], "%08X ",
2417 readl(dd->port->cmd_issue[n]));
2418
2419 size += sprintf(&buf[size], "]\n");
2420 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2421
2422 for (n = dd->slot_groups-1; n >= 0; n--)
2423 size += sprintf(&buf[size], "%08X ",
2424 readl(dd->port->completed[n]));
2425
2426 size += sprintf(&buf[size], "]\n");
2427 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2428 readl(dd->port->mmio + PORT_IRQ_STAT));
2429 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2430 readl(dd->mmio + HOST_IRQ_STAT));
2431 size += sprintf(&buf[size], "\n");
2432
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002433 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2434
2435 for (n = dd->slot_groups-1; n >= 0; n--) {
2436 if (sizeof(long) > sizeof(u32))
2437 group_allocated =
2438 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2439 else
2440 group_allocated = dd->port->cmds_to_issue[n];
2441 size += sprintf(&buf[size], "%08X ", group_allocated);
2442 }
2443 size += sprintf(&buf[size], "]\n");
2444
2445 *offset = size <= len ? size : len;
2446 size = copy_to_user(ubuf, buf, *offset);
2447 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002448 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002449
David Milburnc8afd0d2013-05-23 16:23:45 -05002450 kfree(buf);
2451 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002452}
2453
2454static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2455 size_t len, loff_t *offset)
2456{
2457 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002458 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002459 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002460 int rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002461
2462 if (!len || size)
2463 return 0;
2464
David Milburnc8afd0d2013-05-23 16:23:45 -05002465 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2466 if (!buf) {
2467 dev_err(&dd->pdev->dev,
2468 "Memory allocation: flag buffer\n");
2469 return -ENOMEM;
2470 }
2471
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002472 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2473 dd->port->flags);
2474 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2475 dd->dd_flag);
2476
2477 *offset = size <= len ? size : len;
2478 size = copy_to_user(ubuf, buf, *offset);
2479 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002480 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002481
David Milburnc8afd0d2013-05-23 16:23:45 -05002482 kfree(buf);
2483 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002484}
2485
Asai Thambi S P0caff002013-04-03 19:56:21 +05302486static const struct file_operations mtip_device_status_fops = {
2487 .owner = THIS_MODULE,
2488 .open = simple_open,
2489 .read = mtip_hw_read_device_status,
2490 .llseek = no_llseek,
2491};
2492
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002493static const struct file_operations mtip_regs_fops = {
2494 .owner = THIS_MODULE,
2495 .open = simple_open,
2496 .read = mtip_hw_read_registers,
2497 .llseek = no_llseek,
2498};
2499
2500static const struct file_operations mtip_flags_fops = {
2501 .owner = THIS_MODULE,
2502 .open = simple_open,
2503 .read = mtip_hw_read_flags,
2504 .llseek = no_llseek,
2505};
2506
Sam Bradshaw88523a62011-08-30 08:34:26 -06002507/*
2508 * Create the sysfs related attributes.
2509 *
2510 * @dd Pointer to the driver data structure.
2511 * @kobj Pointer to the kobj for the block device.
2512 *
2513 * return value
2514 * 0 Operation completed successfully.
2515 * -EINVAL Invalid parameter.
2516 */
Jens Axboe63166682011-09-27 21:27:43 -06002517static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002518{
2519 if (!kobj || !dd)
2520 return -EINVAL;
2521
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002522 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2523 dev_warn(&dd->pdev->dev,
2524 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002525 return 0;
2526}
2527
2528/*
2529 * Remove the sysfs related attributes.
2530 *
2531 * @dd Pointer to the driver data structure.
2532 * @kobj Pointer to the kobj for the block device.
2533 *
2534 * return value
2535 * 0 Operation completed successfully.
2536 * -EINVAL Invalid parameter.
2537 */
Jens Axboe63166682011-09-27 21:27:43 -06002538static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002539{
2540 if (!kobj || !dd)
2541 return -EINVAL;
2542
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002543 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002544
2545 return 0;
2546}
2547
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002548static int mtip_hw_debugfs_init(struct driver_data *dd)
2549{
2550 if (!dfs_parent)
2551 return -1;
2552
2553 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2554 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2555 dev_warn(&dd->pdev->dev,
2556 "Error creating node %s under debugfs\n",
2557 dd->disk->disk_name);
2558 dd->dfs_node = NULL;
2559 return -1;
2560 }
2561
Joe Perches5657a812018-05-24 13:38:59 -06002562 debugfs_create_file("flags", 0444, dd->dfs_node, dd, &mtip_flags_fops);
2563 debugfs_create_file("registers", 0444, dd->dfs_node, dd,
2564 &mtip_regs_fops);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002565
2566 return 0;
2567}
2568
2569static void mtip_hw_debugfs_exit(struct driver_data *dd)
2570{
zhong jiangd5fcc4e2018-08-08 22:58:33 +08002571 debugfs_remove_recursive(dd->dfs_node);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002572}
2573
Sam Bradshaw88523a62011-08-30 08:34:26 -06002574/*
2575 * Perform any init/resume time hardware setup
2576 *
2577 * @dd Pointer to the driver data structure.
2578 *
2579 * return value
2580 * None
2581 */
2582static inline void hba_setup(struct driver_data *dd)
2583{
2584 u32 hwdata;
2585 hwdata = readl(dd->mmio + HOST_HSORG);
2586
2587 /* interrupt bug workaround: use only 1 IS bit.*/
2588 writel(hwdata |
2589 HSORG_DISABLE_SLOTGRP_INTR |
2590 HSORG_DISABLE_SLOTGRP_PXIS,
2591 dd->mmio + HOST_HSORG);
2592}
2593
Asai Thambi S P2077d942013-04-29 21:19:49 +02002594static int mtip_device_unaligned_constrained(struct driver_data *dd)
2595{
2596 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
2597}
2598
Sam Bradshaw88523a62011-08-30 08:34:26 -06002599/*
2600 * Detect the details of the product, and store anything needed
2601 * into the driver data structure. This includes product type and
2602 * version and number of slot groups.
2603 *
2604 * @dd Pointer to the driver data structure.
2605 *
2606 * return value
2607 * None
2608 */
2609static void mtip_detect_product(struct driver_data *dd)
2610{
2611 u32 hwdata;
2612 unsigned int rev, slotgroups;
2613
2614 /*
2615 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2616 * info register:
2617 * [15:8] hardware/software interface rev#
2618 * [ 3] asic-style interface
2619 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2620 */
2621 hwdata = readl(dd->mmio + HOST_HSORG);
2622
2623 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2624 dd->slot_groups = 1;
2625
2626 if (hwdata & 0x8) {
2627 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2628 rev = (hwdata & HSORG_HWREV) >> 8;
2629 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2630 dev_info(&dd->pdev->dev,
2631 "ASIC-FPGA design, HS rev 0x%x, "
2632 "%i slot groups [%i slots]\n",
2633 rev,
2634 slotgroups,
2635 slotgroups * 32);
2636
2637 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2638 dev_warn(&dd->pdev->dev,
2639 "Warning: driver only supports "
2640 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2641 slotgroups = MTIP_MAX_SLOT_GROUPS;
2642 }
2643 dd->slot_groups = slotgroups;
2644 return;
2645 }
2646
2647 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2648}
2649
2650/*
2651 * Blocking wait for FTL rebuild to complete
2652 *
2653 * @dd Pointer to the DRIVER_DATA structure.
2654 *
2655 * return value
2656 * 0 FTL rebuild completed successfully
2657 * -EFAULT FTL rebuild error/timeout/interruption
2658 */
2659static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2660{
2661 unsigned long timeout, cnt = 0, start;
2662
2663 dev_warn(&dd->pdev->dev,
2664 "FTL rebuild in progress. Polling for completion.\n");
2665
2666 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002667 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2668
2669 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002670 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002671 &dd->dd_flag)))
2672 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002673 if (mtip_check_surprise_removal(dd->pdev))
2674 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002675
Sam Bradshaw88523a62011-08-30 08:34:26 -06002676 if (mtip_get_identify(dd->port, NULL) < 0)
2677 return -EFAULT;
2678
2679 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2680 MTIP_FTL_REBUILD_MAGIC) {
2681 ssleep(1);
2682 /* Print message every 3 minutes */
2683 if (cnt++ >= 180) {
2684 dev_warn(&dd->pdev->dev,
2685 "FTL rebuild in progress (%d secs).\n",
2686 jiffies_to_msecs(jiffies - start) / 1000);
2687 cnt = 0;
2688 }
2689 } else {
2690 dev_warn(&dd->pdev->dev,
2691 "FTL rebuild complete (%d secs).\n",
2692 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002693 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002694 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002695 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002696 } while (time_before(jiffies, timeout));
2697
2698 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002699 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002700 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2701 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002702 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002703}
2704
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002705static void mtip_softirq_done_fn(struct request *rq)
2706{
2707 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
2708 struct driver_data *dd = rq->q->queuedata;
2709
2710 /* Unmap the DMA scatter list entries */
2711 dma_unmap_sg(&dd->pdev->dev, cmd->sg, cmd->scatter_ents,
2712 cmd->direction);
2713
2714 if (unlikely(cmd->unaligned))
2715 up(&dd->port->cmd_slot_unal);
2716
Christoph Hellwig4dda4732017-04-20 16:03:07 +02002717 blk_mq_end_request(rq, cmd->status);
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002718}
2719
Jens Axboe7baa8572018-11-08 10:24:07 -07002720static bool mtip_abort_cmd(struct request *req, void *data, bool reserved)
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002721{
Christoph Hellwig4dda4732017-04-20 16:03:07 +02002722 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(req);
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002723 struct driver_data *dd = data;
2724
2725 dbg_printk(MTIP_DRV_NAME " Aborting request, tag = %d\n", req->tag);
2726
2727 clear_bit(req->tag, dd->port->cmds_to_issue);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002728 cmd->status = BLK_STS_IOERR;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002729 mtip_softirq_done_fn(req);
Jens Axboe7baa8572018-11-08 10:24:07 -07002730 return true;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002731}
2732
Jens Axboe7baa8572018-11-08 10:24:07 -07002733static bool mtip_queue_cmd(struct request *req, void *data, bool reserved)
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002734{
2735 struct driver_data *dd = data;
2736
2737 set_bit(req->tag, dd->port->cmds_to_issue);
2738 blk_abort_request(req);
Jens Axboe7baa8572018-11-08 10:24:07 -07002739 return true;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002740}
2741
Sam Bradshaw88523a62011-08-30 08:34:26 -06002742/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002743 * service thread to issue queued commands
2744 *
2745 * @data Pointer to the driver data structure.
2746 *
2747 * return value
2748 * 0
2749 */
2750
2751static int mtip_service_thread(void *data)
2752{
2753 struct driver_data *dd = (struct driver_data *)data;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002754 unsigned long slot, slot_start, slot_wrap, to;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002755 unsigned int num_cmd_slots = dd->slot_groups * 32;
2756 struct mtip_port *port = dd->port;
2757
2758 while (1) {
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002759 if (kthread_should_stop() ||
2760 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2761 goto st_out;
2762 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2763
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002764 /*
2765 * the condition is to check neither an internal command is
2766 * is in progress nor error handling is active
2767 */
2768 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi SPcfc05bd2016-02-24 21:16:00 -08002769 (port->flags & MTIP_PF_SVC_THD_WORK));
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002770
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002771 if (kthread_should_stop() ||
2772 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2773 goto st_out;
2774
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002775 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002776 &dd->dd_flag)))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002777 goto st_out;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002778
Asai Thambi SPcfc05bd2016-02-24 21:16:00 -08002779 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2780
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002781restart_eh:
2782 /* Demux bits: start with error handling */
2783 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) {
2784 mtip_handle_tfe(dd);
2785 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
2786 }
2787
2788 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags))
2789 goto restart_eh;
2790
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002791 if (test_bit(MTIP_PF_TO_ACTIVE_BIT, &port->flags)) {
2792 to = jiffies + msecs_to_jiffies(5000);
2793
2794 do {
2795 mdelay(100);
2796 } while (atomic_read(&dd->irq_workers_active) != 0 &&
2797 time_before(jiffies, to));
2798
2799 if (atomic_read(&dd->irq_workers_active) != 0)
2800 dev_warn(&dd->pdev->dev,
2801 "Completion workers still active!");
2802
Sagi Grimberg436c15a2017-07-04 10:00:41 +03002803 blk_mq_quiesce_queue(dd->queue);
2804
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002805 spin_lock(dd->queue->queue_lock);
Keith Busch6d125de2016-03-10 13:58:48 +02002806 blk_mq_tagset_busy_iter(&dd->tags,
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002807 mtip_queue_cmd, dd);
2808 spin_unlock(dd->queue->queue_lock);
2809
2810 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &dd->port->flags);
2811
2812 if (mtip_device_reset(dd))
Keith Busch6d125de2016-03-10 13:58:48 +02002813 blk_mq_tagset_busy_iter(&dd->tags,
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002814 mtip_abort_cmd, dd);
2815
2816 clear_bit(MTIP_PF_TO_ACTIVE_BIT, &dd->port->flags);
Sagi Grimberg436c15a2017-07-04 10:00:41 +03002817
2818 blk_mq_unquiesce_queue(dd->queue);
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002819 }
2820
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002821 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002822 slot = 1;
2823 /* used to restrict the loop to one iteration */
2824 slot_start = num_cmd_slots;
2825 slot_wrap = 0;
2826 while (1) {
2827 slot = find_next_bit(port->cmds_to_issue,
2828 num_cmd_slots, slot);
2829 if (slot_wrap == 1) {
2830 if ((slot_start >= slot) ||
2831 (slot >= num_cmd_slots))
2832 break;
2833 }
2834 if (unlikely(slot_start == num_cmd_slots))
2835 slot_start = slot;
2836
2837 if (unlikely(slot == num_cmd_slots)) {
2838 slot = 1;
2839 slot_wrap = 1;
2840 continue;
2841 }
2842
2843 /* Issue the command to the hardware */
2844 mtip_issue_ncq_command(port, slot);
2845
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002846 clear_bit(slot, port->cmds_to_issue);
2847 }
2848
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002849 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002850 }
2851
2852 if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08002853 if (mtip_ftl_rebuild_poll(dd) == 0)
2854 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002855 }
2856 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002857
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002858st_out:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002859 return 0;
2860}
2861
2862/*
Sam Bradshaw188b9f42014-01-15 10:14:57 -08002863 * DMA region teardown
2864 *
2865 * @dd Pointer to driver_data structure
2866 *
2867 * return value
2868 * None
2869 */
2870static void mtip_dma_free(struct driver_data *dd)
2871{
Sam Bradshaw188b9f42014-01-15 10:14:57 -08002872 struct mtip_port *port = dd->port;
2873
2874 if (port->block1)
2875 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
2876 port->block1, port->block1_dma);
2877
2878 if (port->command_list) {
2879 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
2880 port->command_list, port->command_list_dma);
2881 }
Sam Bradshaw188b9f42014-01-15 10:14:57 -08002882}
2883
2884/*
2885 * DMA region setup
2886 *
2887 * @dd Pointer to driver_data structure
2888 *
2889 * return value
2890 * -ENOMEM Not enough free DMA region space to initialize driver
2891 */
2892static int mtip_dma_alloc(struct driver_data *dd)
2893{
2894 struct mtip_port *port = dd->port;
Sam Bradshaw188b9f42014-01-15 10:14:57 -08002895
2896 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
2897 port->block1 =
2898 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
2899 &port->block1_dma, GFP_KERNEL);
2900 if (!port->block1)
2901 return -ENOMEM;
2902 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
2903
2904 /* Allocate dma memory for command list */
2905 port->command_list =
2906 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
2907 &port->command_list_dma, GFP_KERNEL);
2908 if (!port->command_list) {
2909 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
2910 port->block1, port->block1_dma);
2911 port->block1 = NULL;
2912 port->block1_dma = 0;
2913 return -ENOMEM;
2914 }
2915 memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
2916
2917 /* Setup all pointers into first DMA region */
2918 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
2919 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
2920 port->identify = port->block1 + AHCI_IDFY_OFFSET;
2921 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
2922 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
2923 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
2924 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
2925 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
2926
Sam Bradshaw188b9f42014-01-15 10:14:57 -08002927 return 0;
2928}
2929
Jens Axboeffc771b2014-05-09 09:42:02 -06002930static int mtip_hw_get_identify(struct driver_data *dd)
2931{
2932 struct smart_attr attr242;
2933 unsigned char *buf;
2934 int rv;
2935
2936 if (mtip_get_identify(dd->port, NULL) < 0)
2937 return -EFAULT;
2938
2939 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2940 MTIP_FTL_REBUILD_MAGIC) {
2941 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
2942 return MTIP_FTL_REBUILD_MAGIC;
2943 }
2944 mtip_dump_identify(dd->port);
2945
2946 /* check write protect, over temp and rebuild statuses */
2947 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
2948 dd->port->log_buf,
2949 dd->port->log_buf_dma, 1);
2950 if (rv) {
2951 dev_warn(&dd->pdev->dev,
2952 "Error in READ LOG EXT (10h) command\n");
2953 /* non-critical error, don't fail the load */
2954 } else {
2955 buf = (unsigned char *)dd->port->log_buf;
2956 if (buf[259] & 0x1) {
2957 dev_info(&dd->pdev->dev,
2958 "Write protect bit is set.\n");
2959 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
2960 }
2961 if (buf[288] == 0xF7) {
2962 dev_info(&dd->pdev->dev,
2963 "Exceeded Tmax, drive in thermal shutdown.\n");
2964 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
2965 }
2966 if (buf[288] == 0xBF) {
2967 dev_info(&dd->pdev->dev,
2968 "Drive indicates rebuild has failed.\n");
Asai Thambi SPaae4a032016-02-24 21:18:20 -08002969 set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag);
Jens Axboeffc771b2014-05-09 09:42:02 -06002970 }
2971 }
2972
2973 /* get write protect progess */
2974 memset(&attr242, 0, sizeof(struct smart_attr));
2975 if (mtip_get_smart_attr(dd->port, 242, &attr242))
2976 dev_warn(&dd->pdev->dev,
2977 "Unable to check write protect progress\n");
2978 else
2979 dev_info(&dd->pdev->dev,
2980 "Write protect progress: %u%% (%u blocks)\n",
2981 attr242.cur, le32_to_cpu(attr242.data));
2982
2983 return rv;
2984}
2985
Sam Bradshaw188b9f42014-01-15 10:14:57 -08002986/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06002987 * Called once for each card.
2988 *
2989 * @dd Pointer to the driver data structure.
2990 *
2991 * return value
2992 * 0 on success, else an error code.
2993 */
Jens Axboe63166682011-09-27 21:27:43 -06002994static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002995{
2996 int i;
2997 int rv;
Asai Thambi S P45038362012-04-09 08:35:38 +02002998 unsigned long timeout, timetaken;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002999
3000 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3001
3002 mtip_detect_product(dd);
3003 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3004 rv = -EIO;
3005 goto out1;
3006 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003007
3008 hba_setup(dd);
3009
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003010 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3011 dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003012 if (!dd->port) {
3013 dev_err(&dd->pdev->dev,
3014 "Memory allocation: port structure\n");
3015 return -ENOMEM;
3016 }
3017
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003018 /* Continue workqueue setup */
3019 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3020 dd->work[i].port = dd->port;
3021
Asai Thambi S P2077d942013-04-29 21:19:49 +02003022 /* Enable unaligned IO constraints for some devices */
3023 if (mtip_device_unaligned_constrained(dd))
3024 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3025 else
3026 dd->unal_qdepth = 0;
3027
Asai Thambi S P2077d942013-04-29 21:19:49 +02003028 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003029
3030 /* Spinlock to prevent concurrent issue */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003031 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3032 spin_lock_init(&dd->port->cmd_issue_lock[i]);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003033
3034 /* Set the port mmio base address. */
3035 dd->port->mmio = dd->mmio + PORT_OFFSET;
3036 dd->port->dd = dd;
3037
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003038 /* DMA allocations */
3039 rv = mtip_dma_alloc(dd);
3040 if (rv < 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003041 goto out1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003042
3043 /* Setup the pointers to the extended s_active and CI registers. */
3044 for (i = 0; i < dd->slot_groups; i++) {
3045 dd->port->s_active[i] =
3046 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3047 dd->port->cmd_issue[i] =
3048 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3049 dd->port->completed[i] =
3050 dd->port->mmio + i*0x80 + PORT_SDBV;
3051 }
3052
Asai Thambi S P45038362012-04-09 08:35:38 +02003053 timetaken = jiffies;
3054 timeout = jiffies + msecs_to_jiffies(30000);
3055 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3056 time_before(jiffies, timeout)) {
3057 mdelay(100);
3058 }
3059 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3060 timetaken = jiffies - timetaken;
3061 dev_warn(&dd->pdev->dev,
3062 "Surprise removal detected at %u ms\n",
3063 jiffies_to_msecs(timetaken));
3064 rv = -ENODEV;
3065 goto out2 ;
3066 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003067 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003068 timetaken = jiffies - timetaken;
3069 dev_warn(&dd->pdev->dev,
3070 "Removal detected at %u ms\n",
3071 jiffies_to_msecs(timetaken));
3072 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003073 goto out2;
3074 }
3075
Asai Thambi S P45038362012-04-09 08:35:38 +02003076 /* Conditionally reset the HBA. */
3077 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3078 if (mtip_hba_reset(dd) < 0) {
3079 dev_err(&dd->pdev->dev,
3080 "Card did not reset within timeout\n");
3081 rv = -EIO;
3082 goto out2;
3083 }
3084 } else {
3085 /* Clear any pending interrupts on the HBA */
3086 writel(readl(dd->mmio + HOST_IRQ_STAT),
3087 dd->mmio + HOST_IRQ_STAT);
3088 }
3089
Sam Bradshaw88523a62011-08-30 08:34:26 -06003090 mtip_init_port(dd->port);
3091 mtip_start_port(dd->port);
3092
3093 /* Setup the ISR and enable interrupts. */
3094 rv = devm_request_irq(&dd->pdev->dev,
3095 dd->pdev->irq,
3096 mtip_irq_handler,
3097 IRQF_SHARED,
3098 dev_driver_string(&dd->pdev->dev),
3099 dd);
3100
3101 if (rv) {
3102 dev_err(&dd->pdev->dev,
3103 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3104 goto out2;
3105 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003106 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003107
3108 /* Enable interrupts on the HBA. */
3109 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3110 dd->mmio + HOST_CTL);
3111
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003112 init_waitqueue_head(&dd->port->svc_wait);
3113
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003114 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003115 rv = -EFAULT;
3116 goto out3;
3117 }
3118
Sam Bradshaw88523a62011-08-30 08:34:26 -06003119 return rv;
3120
3121out3:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003122 /* Disable interrupts on the HBA. */
3123 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3124 dd->mmio + HOST_CTL);
3125
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003126 /* Release the IRQ. */
3127 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003128 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3129
3130out2:
3131 mtip_deinit_port(dd->port);
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003132 mtip_dma_free(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003133
Sam Bradshaw88523a62011-08-30 08:34:26 -06003134out1:
3135 /* Free the memory allocated for the for structure. */
3136 kfree(dd->port);
3137
3138 return rv;
3139}
3140
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003141static int mtip_standby_drive(struct driver_data *dd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003142{
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003143 int rv = 0;
Jens Axboeffc771b2014-05-09 09:42:02 -06003144
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003145 if (dd->sr || !dd->port)
3146 return -ENODEV;
Jens Axboeffc771b2014-05-09 09:42:02 -06003147 /*
3148 * Send standby immediate (E0h) to the drive so that it
3149 * saves its state.
3150 */
3151 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003152 !test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag) &&
3153 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)) {
3154 rv = mtip_standby_immediate(dd->port);
3155 if (rv)
Jens Axboeffc771b2014-05-09 09:42:02 -06003156 dev_warn(&dd->pdev->dev,
3157 "STANDBY IMMEDIATE failed\n");
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003158 }
3159 return rv;
Jens Axboeffc771b2014-05-09 09:42:02 -06003160}
3161
Sam Bradshaw88523a62011-08-30 08:34:26 -06003162/*
3163 * Called to deinitialize an interface.
3164 *
3165 * @dd Pointer to the driver data structure.
3166 *
3167 * return value
3168 * 0
3169 */
Jens Axboe63166682011-09-27 21:27:43 -06003170static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003171{
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003172 if (!dd->sr) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003173 /* de-initialize the port. */
3174 mtip_deinit_port(dd->port);
3175
3176 /* Disable interrupts on the HBA. */
3177 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3178 dd->mmio + HOST_CTL);
3179 }
3180
Sam Bradshaw88523a62011-08-30 08:34:26 -06003181 /* Release the IRQ. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003182 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003183 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
Asai Thambi SP2132a542015-05-11 15:53:18 -07003184 msleep(1000);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003185
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003186 /* Free dma regions */
3187 mtip_dma_free(dd);
3188
Sam Bradshaw88523a62011-08-30 08:34:26 -06003189 /* Free the memory allocated for the for structure. */
3190 kfree(dd->port);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003191 dd->port = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003192
3193 return 0;
3194}
3195
3196/*
3197 * Issue a Standby Immediate command to the device.
3198 *
3199 * This function is called by the Block Layer just before the
3200 * system powers off during a shutdown.
3201 *
3202 * @dd Pointer to the driver data structure.
3203 *
3204 * return value
3205 * 0
3206 */
Jens Axboe63166682011-09-27 21:27:43 -06003207static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003208{
3209 /*
3210 * Send standby immediate (E0h) to the drive so that it
3211 * saves its state.
3212 */
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003213 mtip_standby_drive(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003214
3215 return 0;
3216}
3217
3218/*
3219 * Suspend function
3220 *
3221 * This function is called by the Block Layer just before the
3222 * system hibernates.
3223 *
3224 * @dd Pointer to the driver data structure.
3225 *
3226 * return value
3227 * 0 Suspend was successful
3228 * -EFAULT Suspend was not successful
3229 */
Jens Axboe63166682011-09-27 21:27:43 -06003230static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003231{
3232 /*
3233 * Send standby immediate (E0h) to the drive
3234 * so that it saves its state.
3235 */
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003236 if (mtip_standby_drive(dd) != 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003237 dev_err(&dd->pdev->dev,
3238 "Failed standby-immediate command\n");
3239 return -EFAULT;
3240 }
3241
3242 /* Disable interrupts on the HBA.*/
3243 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3244 dd->mmio + HOST_CTL);
3245 mtip_deinit_port(dd->port);
3246
3247 return 0;
3248}
3249
3250/*
3251 * Resume function
3252 *
3253 * This function is called by the Block Layer as the
3254 * system resumes.
3255 *
3256 * @dd Pointer to the driver data structure.
3257 *
3258 * return value
3259 * 0 Resume was successful
3260 * -EFAULT Resume was not successful
3261 */
Jens Axboe63166682011-09-27 21:27:43 -06003262static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003263{
3264 /* Perform any needed hardware setup steps */
3265 hba_setup(dd);
3266
3267 /* Reset the HBA */
3268 if (mtip_hba_reset(dd) != 0) {
3269 dev_err(&dd->pdev->dev,
3270 "Unable to reset the HBA\n");
3271 return -EFAULT;
3272 }
3273
3274 /*
3275 * Enable the port, DMA engine, and FIS reception specific
3276 * h/w in controller.
3277 */
3278 mtip_init_port(dd->port);
3279 mtip_start_port(dd->port);
3280
3281 /* Enable interrupts on the HBA.*/
3282 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3283 dd->mmio + HOST_CTL);
3284
3285 return 0;
3286}
3287
3288/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003289 * Helper function for reusing disk name
3290 * upon hot insertion.
3291 */
3292static int rssd_disk_name_format(char *prefix,
3293 int index,
3294 char *buf,
3295 int buflen)
3296{
3297 const int base = 'z' - 'a' + 1;
3298 char *begin = buf + strlen(prefix);
3299 char *end = buf + buflen;
3300 char *p;
3301 int unit;
3302
3303 p = end - 1;
3304 *p = '\0';
3305 unit = base;
3306 do {
3307 if (p == begin)
3308 return -EINVAL;
3309 *--p = 'a' + (index % unit);
3310 index = (index / unit) - 1;
3311 } while (index >= 0);
3312
3313 memmove(begin, p, end - p);
3314 memcpy(buf, prefix, strlen(prefix));
3315
3316 return 0;
3317}
3318
3319/*
3320 * Block layer IOCTL handler.
3321 *
3322 * @dev Pointer to the block_device structure.
3323 * @mode ignored
3324 * @cmd IOCTL command passed from the user application.
3325 * @arg Argument passed from the user application.
3326 *
3327 * return value
3328 * 0 IOCTL completed successfully.
3329 * -ENOTTY IOCTL not supported or invalid driver data
3330 * structure pointer.
3331 */
3332static int mtip_block_ioctl(struct block_device *dev,
3333 fmode_t mode,
3334 unsigned cmd,
3335 unsigned long arg)
3336{
3337 struct driver_data *dd = dev->bd_disk->private_data;
3338
3339 if (!capable(CAP_SYS_ADMIN))
3340 return -EACCES;
3341
3342 if (!dd)
3343 return -ENOTTY;
3344
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003345 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003346 return -ENOTTY;
3347
Sam Bradshaw88523a62011-08-30 08:34:26 -06003348 switch (cmd) {
3349 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003350 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003351 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003352 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003353 }
3354}
3355
Jens Axboe16d02c02011-09-27 15:50:01 -06003356#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003357/*
3358 * Block layer compat IOCTL handler.
3359 *
3360 * @dev Pointer to the block_device structure.
3361 * @mode ignored
3362 * @cmd IOCTL command passed from the user application.
3363 * @arg Argument passed from the user application.
3364 *
3365 * return value
3366 * 0 IOCTL completed successfully.
3367 * -ENOTTY IOCTL not supported or invalid driver data
3368 * structure pointer.
3369 */
3370static int mtip_block_compat_ioctl(struct block_device *dev,
3371 fmode_t mode,
3372 unsigned cmd,
3373 unsigned long arg)
3374{
3375 struct driver_data *dd = dev->bd_disk->private_data;
3376
3377 if (!capable(CAP_SYS_ADMIN))
3378 return -EACCES;
3379
3380 if (!dd)
3381 return -ENOTTY;
3382
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003383 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003384 return -ENOTTY;
3385
Sam Bradshaw88523a62011-08-30 08:34:26 -06003386 switch (cmd) {
3387 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003388 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003389 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003390 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003391 ide_task_request_t req_task;
3392 int compat_tasksize, outtotal, ret;
3393
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003394 compat_tasksize =
3395 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003396
3397 compat_req_task =
3398 (struct mtip_compat_ide_task_request_s __user *) arg;
3399
3400 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003401 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003402 return -EFAULT;
3403
3404 if (get_user(req_task.out_size, &compat_req_task->out_size))
3405 return -EFAULT;
3406
3407 if (get_user(req_task.in_size, &compat_req_task->in_size))
3408 return -EFAULT;
3409
3410 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3411
3412 ret = exec_drive_taskfile(dd, (void __user *) arg,
3413 &req_task, outtotal);
3414
3415 if (copy_to_user((void __user *) arg, &req_task,
3416 compat_tasksize -
3417 (2 * sizeof(compat_long_t))))
3418 return -EFAULT;
3419
3420 if (put_user(req_task.out_size, &compat_req_task->out_size))
3421 return -EFAULT;
3422
3423 if (put_user(req_task.in_size, &compat_req_task->in_size))
3424 return -EFAULT;
3425
3426 return ret;
3427 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003428 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003429 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003430 }
3431}
Jens Axboe16d02c02011-09-27 15:50:01 -06003432#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003433
3434/*
3435 * Obtain the geometry of the device.
3436 *
3437 * You may think that this function is obsolete, but some applications,
3438 * fdisk for example still used CHS values. This function describes the
3439 * device as having 224 heads and 56 sectors per cylinder. These values are
3440 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3441 * partition is described in terms of a start and end cylinder this means
3442 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3443 * affects performance.
3444 *
3445 * @dev Pointer to the block_device strucutre.
3446 * @geo Pointer to a hd_geometry structure.
3447 *
3448 * return value
3449 * 0 Operation completed successfully.
3450 * -ENOTTY An error occurred while reading the drive capacity.
3451 */
3452static int mtip_block_getgeo(struct block_device *dev,
3453 struct hd_geometry *geo)
3454{
3455 struct driver_data *dd = dev->bd_disk->private_data;
3456 sector_t capacity;
3457
3458 if (!dd)
3459 return -ENOTTY;
3460
3461 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3462 dev_warn(&dd->pdev->dev,
3463 "Could not get drive capacity.\n");
3464 return -ENOTTY;
3465 }
3466
3467 geo->heads = 224;
3468 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003469 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003470 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003471 return 0;
3472}
3473
Asai Thambi SP51c65702016-02-24 21:18:10 -08003474static int mtip_block_open(struct block_device *dev, fmode_t mode)
3475{
3476 struct driver_data *dd;
3477
3478 if (dev && dev->bd_disk) {
3479 dd = (struct driver_data *) dev->bd_disk->private_data;
3480
3481 if (dd) {
3482 if (test_bit(MTIP_DDF_REMOVAL_BIT,
3483 &dd->dd_flag)) {
3484 return -ENODEV;
3485 }
3486 return 0;
3487 }
3488 }
3489 return -ENODEV;
3490}
3491
Baoyou Xie99e6b872016-08-26 14:08:53 +08003492static void mtip_block_release(struct gendisk *disk, fmode_t mode)
Asai Thambi SP51c65702016-02-24 21:18:10 -08003493{
3494}
3495
Sam Bradshaw88523a62011-08-30 08:34:26 -06003496/*
3497 * Block device operation function.
3498 *
3499 * This structure contains pointers to the functions required by the block
3500 * layer.
3501 */
3502static const struct block_device_operations mtip_block_ops = {
Asai Thambi SP51c65702016-02-24 21:18:10 -08003503 .open = mtip_block_open,
3504 .release = mtip_block_release,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003505 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003506#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003507 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003508#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003509 .getgeo = mtip_block_getgeo,
3510 .owner = THIS_MODULE
3511};
3512
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003513static inline bool is_se_active(struct driver_data *dd)
3514{
3515 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT, &dd->port->flags))) {
3516 if (dd->port->ic_pause_timer) {
3517 unsigned long to = dd->port->ic_pause_timer +
3518 msecs_to_jiffies(1000);
3519 if (time_after(jiffies, to)) {
3520 clear_bit(MTIP_PF_SE_ACTIVE_BIT,
3521 &dd->port->flags);
3522 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
3523 dd->port->ic_pause_timer = 0;
3524 wake_up_interruptible(&dd->port->svc_wait);
3525 return false;
3526 }
3527 }
3528 return true;
3529 }
3530 return false;
3531}
3532
Christoph Hellwig10966fa2018-11-09 14:48:55 +01003533static inline bool is_stopped(struct driver_data *dd, struct request *rq)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003534{
Christoph Hellwig10966fa2018-11-09 14:48:55 +01003535 if (likely(!(dd->dd_flag & MTIP_DDF_STOP_IO)))
3536 return false;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003537
Christoph Hellwig10966fa2018-11-09 14:48:55 +01003538 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
3539 return true;
3540 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
3541 return true;
3542 if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag) &&
3543 rq_data_dir(rq))
3544 return true;
3545 if (test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag))
3546 return true;
3547 if (test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag))
3548 return true;
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003549
Christoph Hellwig10966fa2018-11-09 14:48:55 +01003550 return false;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003551}
3552
Jens Axboeffc771b2014-05-09 09:42:02 -06003553static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx,
3554 struct request *rq)
3555{
3556 struct driver_data *dd = hctx->queue->queuedata;
3557 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3558
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003559 if (rq_data_dir(rq) == READ || !dd->unal_qdepth)
Jens Axboeffc771b2014-05-09 09:42:02 -06003560 return false;
3561
3562 /*
3563 * If unaligned depth must be limited on this controller, mark it
3564 * as unaligned if the IO isn't on a 4k boundary (start of length).
3565 */
3566 if (blk_rq_sectors(rq) <= 64) {
3567 if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7))
3568 cmd->unaligned = 1;
3569 }
3570
3571 if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal))
3572 return true;
3573
3574 return false;
3575}
3576
Christoph Hellwigfc17b652017-06-03 09:38:05 +02003577static blk_status_t mtip_issue_reserved_cmd(struct blk_mq_hw_ctx *hctx,
3578 struct request *rq)
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003579{
3580 struct driver_data *dd = hctx->queue->queuedata;
Jens Axboe6f63503c2017-05-02 09:56:00 -06003581 struct mtip_int_cmd *icmd = rq->special;
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003582 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3583 struct mtip_cmd_sg *command_sg;
3584
3585 if (mtip_commands_active(dd->port))
Christoph Hellwigfc17b652017-06-03 09:38:05 +02003586 return BLK_STS_RESOURCE;
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003587
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003588 /* Populate the SG list */
3589 cmd->command_header->opts =
3590 __force_bit2int cpu_to_le32(icmd->opts | icmd->fis_len);
3591 if (icmd->buf_len) {
3592 command_sg = cmd->command + AHCI_CMD_TBL_HDR_SZ;
3593
3594 command_sg->info =
3595 __force_bit2int cpu_to_le32((icmd->buf_len-1) & 0x3FFFFF);
3596 command_sg->dba =
3597 __force_bit2int cpu_to_le32(icmd->buffer & 0xFFFFFFFF);
3598 command_sg->dba_upper =
3599 __force_bit2int cpu_to_le32((icmd->buffer >> 16) >> 16);
3600
3601 cmd->command_header->opts |=
3602 __force_bit2int cpu_to_le32((1 << 16));
3603 }
3604
3605 /* Populate the command header */
3606 cmd->command_header->byte_count = 0;
3607
3608 blk_mq_start_request(rq);
3609 mtip_issue_non_ncq_command(dd->port, rq->tag);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02003610 return 0;
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003611}
3612
Christoph Hellwigfc17b652017-06-03 09:38:05 +02003613static blk_status_t mtip_queue_rq(struct blk_mq_hw_ctx *hctx,
Jens Axboe74c45052014-10-29 11:14:52 -06003614 const struct blk_mq_queue_data *bd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003615{
Christoph Hellwig10966fa2018-11-09 14:48:55 +01003616 struct driver_data *dd = hctx->queue->queuedata;
Jens Axboe74c45052014-10-29 11:14:52 -06003617 struct request *rq = bd->rq;
Christoph Hellwig10966fa2018-11-09 14:48:55 +01003618 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
Jens Axboeffc771b2014-05-09 09:42:02 -06003619
Ming Leia4e84aa2017-04-27 07:45:18 -06003620 mtip_init_cmd_header(rq);
3621
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003622 if (blk_rq_is_passthrough(rq))
3623 return mtip_issue_reserved_cmd(hctx, rq);
3624
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003625 if (unlikely(mtip_check_unal_depth(hctx, rq)))
Christoph Hellwigfc17b652017-06-03 09:38:05 +02003626 return BLK_STS_RESOURCE;
Jens Axboeffc771b2014-05-09 09:42:02 -06003627
Christoph Hellwig10966fa2018-11-09 14:48:55 +01003628 if (is_se_active(dd) || is_stopped(dd, rq))
3629 return BLK_STS_IOERR;
3630
Christoph Hellwige2490072014-09-13 16:40:09 -07003631 blk_mq_start_request(rq);
3632
Christoph Hellwig81e66172018-11-09 14:48:56 +01003633 if (req_op(rq) == REQ_OP_DISCARD)
3634 return mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq));
3635 mtip_hw_submit_io(dd, rq, cmd, hctx);
Christoph Hellwig10966fa2018-11-09 14:48:55 +01003636 return BLK_STS_OK;
Jens Axboeffc771b2014-05-09 09:42:02 -06003637}
3638
Christoph Hellwigd6296d392017-05-01 10:19:08 -06003639static void mtip_free_cmd(struct blk_mq_tag_set *set, struct request *rq,
3640 unsigned int hctx_idx)
Jens Axboeffc771b2014-05-09 09:42:02 -06003641{
Christoph Hellwigd6296d392017-05-01 10:19:08 -06003642 struct driver_data *dd = set->driver_data;
Jens Axboeffc771b2014-05-09 09:42:02 -06003643 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3644
3645 if (!cmd->command)
3646 return;
3647
3648 dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3649 cmd->command, cmd->command_dma);
3650}
3651
Christoph Hellwigd6296d392017-05-01 10:19:08 -06003652static int mtip_init_cmd(struct blk_mq_tag_set *set, struct request *rq,
3653 unsigned int hctx_idx, unsigned int numa_node)
Jens Axboeffc771b2014-05-09 09:42:02 -06003654{
Christoph Hellwigd6296d392017-05-01 10:19:08 -06003655 struct driver_data *dd = set->driver_data;
Jens Axboeffc771b2014-05-09 09:42:02 -06003656 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
Jens Axboeffc771b2014-05-09 09:42:02 -06003657
3658 cmd->command = dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3659 &cmd->command_dma, GFP_KERNEL);
3660 if (!cmd->command)
3661 return -ENOMEM;
3662
3663 memset(cmd->command, 0, CMD_DMA_ALLOC_SZ);
3664
Jens Axboeffc771b2014-05-09 09:42:02 -06003665 sg_init_table(cmd->sg, MTIP_MAX_SG);
3666 return 0;
3667}
3668
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003669static enum blk_eh_timer_return mtip_cmd_timeout(struct request *req,
3670 bool reserved)
3671{
3672 struct driver_data *dd = req->q->queuedata;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003673
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003674 if (reserved) {
3675 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(req);
3676
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02003677 cmd->status = BLK_STS_TIMEOUT;
Christoph Hellwigc5fb85b2018-05-29 15:52:32 +02003678 blk_mq_complete_request(req);
3679 return BLK_EH_DONE;
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003680 }
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003681
3682 if (test_bit(req->tag, dd->port->cmds_to_issue))
3683 goto exit_handler;
3684
3685 if (test_and_set_bit(MTIP_PF_TO_ACTIVE_BIT, &dd->port->flags))
3686 goto exit_handler;
3687
3688 wake_up_interruptible(&dd->port->svc_wait);
3689exit_handler:
Jens Axboe90beb2e2016-03-04 08:15:48 -07003690 return BLK_EH_RESET_TIMER;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003691}
3692
Eric Biggersf363b082017-03-30 13:39:16 -07003693static const struct blk_mq_ops mtip_mq_ops = {
Jens Axboeffc771b2014-05-09 09:42:02 -06003694 .queue_rq = mtip_queue_rq,
Jens Axboeffc771b2014-05-09 09:42:02 -06003695 .init_request = mtip_init_cmd,
3696 .exit_request = mtip_free_cmd,
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003697 .complete = mtip_softirq_done_fn,
3698 .timeout = mtip_cmd_timeout,
Jens Axboeffc771b2014-05-09 09:42:02 -06003699};
3700
Sam Bradshaw88523a62011-08-30 08:34:26 -06003701/*
3702 * Block layer initialization function.
3703 *
3704 * This function is called once by the PCI layer for each P320
3705 * device that is connected to the system.
3706 *
3707 * @dd Pointer to the driver data structure.
3708 *
3709 * return value
3710 * 0 on success else an error code.
3711 */
Jens Axboe63166682011-09-27 21:27:43 -06003712static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003713{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003714 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003715 sector_t capacity;
3716 unsigned int index = 0;
3717 struct kobject *kobj;
3718
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003719 if (dd->disk)
3720 goto skip_create_disk; /* hw init done, before rebuild */
3721
Jens Axboeffc771b2014-05-09 09:42:02 -06003722 if (mtip_hw_init(dd)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003723 rv = -EINVAL;
3724 goto protocol_init_error;
3725 }
3726
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003727 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003728 if (dd->disk == NULL) {
3729 dev_err(&dd->pdev->dev,
3730 "Unable to allocate gendisk structure\n");
3731 rv = -EINVAL;
3732 goto alloc_disk_error;
3733 }
3734
Matthew Wilcox3aed4bc2018-03-21 16:49:06 -04003735 rv = ida_alloc(&rssd_index_ida, GFP_KERNEL);
3736 if (rv < 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003737 goto ida_get_error;
Matthew Wilcox3aed4bc2018-03-21 16:49:06 -04003738 index = rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003739
3740 rv = rssd_disk_name_format("rssd",
3741 index,
3742 dd->disk->disk_name,
3743 DISK_NAME_LEN);
3744 if (rv)
3745 goto disk_index_error;
3746
Sam Bradshaw88523a62011-08-30 08:34:26 -06003747 dd->disk->major = dd->major;
Asai Thambi SP75787262015-05-11 15:55:26 -07003748 dd->disk->first_minor = index * MTIP_MAX_MINORS;
3749 dd->disk->minors = MTIP_MAX_MINORS;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003750 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003751 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003752 dd->index = index;
3753
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003754 mtip_hw_debugfs_init(dd);
3755
Jens Axboeffc771b2014-05-09 09:42:02 -06003756 memset(&dd->tags, 0, sizeof(dd->tags));
3757 dd->tags.ops = &mtip_mq_ops;
3758 dd->tags.nr_hw_queues = 1;
3759 dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS;
3760 dd->tags.reserved_tags = 1;
3761 dd->tags.cmd_size = sizeof(struct mtip_cmd);
3762 dd->tags.numa_node = dd->numa_node;
Jens Axboea800ce82017-04-27 16:46:02 -06003763 dd->tags.flags = BLK_MQ_F_SHOULD_MERGE;
Jens Axboeffc771b2014-05-09 09:42:02 -06003764 dd->tags.driver_data = dd;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003765 dd->tags.timeout = MTIP_NCQ_CMD_TIMEOUT_MS;
Jens Axboeffc771b2014-05-09 09:42:02 -06003766
3767 rv = blk_mq_alloc_tag_set(&dd->tags);
3768 if (rv) {
3769 dev_err(&dd->pdev->dev,
3770 "Unable to allocate request queue\n");
Asai Thambi SP008e56d2016-02-24 21:21:20 -08003771 goto block_queue_alloc_tag_error;
Jens Axboeffc771b2014-05-09 09:42:02 -06003772 }
3773
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003774 /* Allocate the request queue. */
Jens Axboeffc771b2014-05-09 09:42:02 -06003775 dd->queue = blk_mq_init_queue(&dd->tags);
Dan Carpentera8a642c2014-05-14 15:54:18 +03003776 if (IS_ERR(dd->queue)) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003777 dev_err(&dd->pdev->dev,
3778 "Unable to allocate request queue\n");
3779 rv = -ENOMEM;
3780 goto block_queue_alloc_init_error;
3781 }
3782
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003783 dd->disk->queue = dd->queue;
3784 dd->queue->queuedata = dd;
3785
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08003786skip_create_disk:
Jens Axboeffc771b2014-05-09 09:42:02 -06003787 /* Initialize the protocol layer. */
3788 wait_for_rebuild = mtip_hw_get_identify(dd);
3789 if (wait_for_rebuild < 0) {
3790 dev_err(&dd->pdev->dev,
3791 "Protocol layer initialization failed\n");
3792 rv = -EINVAL;
3793 goto init_hw_cmds_error;
3794 }
3795
3796 /*
3797 * if rebuild pending, start the service thread, and delay the block
Dan Williams0d52c7562016-06-15 19:44:20 -07003798 * queue creation and device_add_disk()
Jens Axboeffc771b2014-05-09 09:42:02 -06003799 */
3800 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3801 goto start_service_thread;
3802
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003803 /* Set device limits. */
Bart Van Assche4e699cb2018-03-07 17:10:06 -08003804 blk_queue_flag_set(QUEUE_FLAG_NONROT, dd->queue);
3805 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, dd->queue);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003806 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3807 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003808 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3809 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003810 blk_queue_io_min(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003811
Asai Thambi S P15283462013-01-11 14:41:34 +01003812 /* Signal trim support */
3813 if (dd->trim_supp == true) {
Bart Van Assche4e699cb2018-03-07 17:10:06 -08003814 blk_queue_flag_set(QUEUE_FLAG_DISCARD, dd->queue);
Asai Thambi S P15283462013-01-11 14:41:34 +01003815 dd->queue->limits.discard_granularity = 4096;
3816 blk_queue_max_discard_sectors(dd->queue,
3817 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
Asai Thambi S P15283462013-01-11 14:41:34 +01003818 }
3819
Sam Bradshaw88523a62011-08-30 08:34:26 -06003820 /* Set the capacity of the device in 512 byte sectors. */
3821 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3822 dev_warn(&dd->pdev->dev,
3823 "Could not read drive capacity\n");
3824 rv = -EIO;
3825 goto read_capacity_error;
3826 }
3827 set_capacity(dd->disk, capacity);
3828
3829 /* Enable the block device and add it to /dev */
Hannes Reineckefef912b2018-09-28 08:17:19 +02003830 device_add_disk(&dd->pdev->dev, dd->disk, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003831
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003832 dd->bdev = bdget_disk(dd->disk, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003833 /*
3834 * Now that the disk is active, initialize any sysfs attributes
3835 * managed by the protocol layer.
3836 */
3837 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3838 if (kobj) {
3839 mtip_hw_sysfs_init(dd, kobj);
3840 kobject_put(kobj);
3841 }
3842
Asai Thambi S P45038362012-04-09 08:35:38 +02003843 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003844 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003845 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02003846 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003847
3848start_service_thread:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003849 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
Rasmus Villemoes8aeea032015-11-20 10:46:49 +01003850 dd, dd->numa_node,
3851 "mtip_svc_thd_%02d", index);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003852
3853 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003854 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003855 dd->mtip_svc_handler = NULL;
3856 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003857 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003858 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003859 wake_up_process(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02003860 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3861 rv = wait_for_rebuild;
3862
Sam Bradshaw88523a62011-08-30 08:34:26 -06003863 return rv;
3864
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003865kthread_run_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003866 bdput(dd->bdev);
3867 dd->bdev = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07003868
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003869 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003870 del_gendisk(dd->disk);
3871
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003872read_capacity_error:
Jens Axboeffc771b2014-05-09 09:42:02 -06003873init_hw_cmds_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003874 blk_cleanup_queue(dd->queue);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003875block_queue_alloc_init_error:
Asai Thambi SP008e56d2016-02-24 21:21:20 -08003876 blk_mq_free_tag_set(&dd->tags);
3877block_queue_alloc_tag_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003878 mtip_hw_debugfs_exit(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003879disk_index_error:
Matthew Wilcox3aed4bc2018-03-21 16:49:06 -04003880 ida_free(&rssd_index_ida, index);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003881
3882ida_get_error:
3883 put_disk(dd->disk);
3884
3885alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003886 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003887
3888protocol_init_error:
3889 return rv;
3890}
3891
Jens Axboe7baa8572018-11-08 10:24:07 -07003892static bool mtip_no_dev_cleanup(struct request *rq, void *data, bool reserv)
Asai Thambi SP008e56d2016-02-24 21:21:20 -08003893{
Jens Axboe6f63503c2017-05-02 09:56:00 -06003894 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
Asai Thambi SP008e56d2016-02-24 21:21:20 -08003895
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02003896 cmd->status = BLK_STS_IOERR;
Jens Axboe6f63503c2017-05-02 09:56:00 -06003897 blk_mq_complete_request(rq);
Jens Axboe7baa8572018-11-08 10:24:07 -07003898 return true;
Asai Thambi SP008e56d2016-02-24 21:21:20 -08003899}
3900
Sam Bradshaw88523a62011-08-30 08:34:26 -06003901/*
3902 * Block layer deinitialization function.
3903 *
3904 * Called by the PCI layer as each P320 device is removed.
3905 *
3906 * @dd Pointer to the driver data structure.
3907 *
3908 * return value
3909 * 0
3910 */
Jens Axboe63166682011-09-27 21:27:43 -06003911static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003912{
3913 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003914
Asai Thambi SP2132a542015-05-11 15:53:18 -07003915 mtip_hw_debugfs_exit(dd);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003916
Asai Thambi SP2132a542015-05-11 15:53:18 -07003917 if (dd->mtip_svc_handler) {
3918 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
3919 wake_up_interruptible(&dd->port->svc_wait);
3920 kthread_stop(dd->mtip_svc_handler);
3921 }
3922
3923 /* Clean up the sysfs attributes, if created */
3924 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
3925 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3926 if (kobj) {
3927 mtip_hw_sysfs_exit(dd, kobj);
3928 kobject_put(kobj);
Asai Thambi S P45038362012-04-09 08:35:38 +02003929 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07003930 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003931
Asai Thambi SP008e56d2016-02-24 21:21:20 -08003932 if (!dd->sr) {
3933 /*
3934 * Explicitly wait here for IOs to quiesce,
3935 * as mtip_standby_drive usually won't wait for IOs.
3936 */
Jens Axboe8afdd942017-04-27 16:32:41 -06003937 if (!mtip_quiesce_io(dd->port, MTIP_QUIESCE_IO_TIMEOUT_MS))
Asai Thambi SP008e56d2016-02-24 21:21:20 -08003938 mtip_standby_drive(dd);
3939 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07003940 else
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003941 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
3942 dd->disk->disk_name);
Asai Thambi SP2132a542015-05-11 15:53:18 -07003943
Ming Lei1671d522017-03-27 20:06:57 +08003944 blk_freeze_queue_start(dd->queue);
Sagi Grimberg436c15a2017-07-04 10:00:41 +03003945 blk_mq_quiesce_queue(dd->queue);
Keith Busch6d125de2016-03-10 13:58:48 +02003946 blk_mq_tagset_busy_iter(&dd->tags, mtip_no_dev_cleanup, dd);
Sagi Grimberg436c15a2017-07-04 10:00:41 +03003947 blk_mq_unquiesce_queue(dd->queue);
Asai Thambi SP008e56d2016-02-24 21:21:20 -08003948
Asai Thambi SP2132a542015-05-11 15:53:18 -07003949 /*
3950 * Delete our gendisk structure. This also removes the device
3951 * from /dev
3952 */
3953 if (dd->bdev) {
3954 bdput(dd->bdev);
3955 dd->bdev = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003956 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07003957 if (dd->disk) {
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08003958 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
3959 del_gendisk(dd->disk);
Asai Thambi SP2132a542015-05-11 15:53:18 -07003960 if (dd->disk->queue) {
3961 blk_cleanup_queue(dd->queue);
3962 blk_mq_free_tag_set(&dd->tags);
3963 dd->queue = NULL;
3964 }
3965 put_disk(dd->disk);
3966 }
3967 dd->disk = NULL;
3968
Matthew Wilcox3aed4bc2018-03-21 16:49:06 -04003969 ida_free(&rssd_index_ida, dd->index);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003970
3971 /* De-initialize the protocol layer. */
3972 mtip_hw_exit(dd);
3973
3974 return 0;
3975}
3976
3977/*
3978 * Function called by the PCI layer when just before the
3979 * machine shuts down.
3980 *
3981 * If a protocol layer shutdown function is present it will be called
3982 * by this function.
3983 *
3984 * @dd Pointer to the driver data structure.
3985 *
3986 * return value
3987 * 0
3988 */
Jens Axboe63166682011-09-27 21:27:43 -06003989static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003990{
Jens Axboeffc771b2014-05-09 09:42:02 -06003991 mtip_hw_shutdown(dd);
3992
Sam Bradshaw88523a62011-08-30 08:34:26 -06003993 /* Delete our gendisk structure, and cleanup the blk queue. */
Asai Thambi S P58c49df2013-01-11 18:47:12 +05303994 if (dd->disk) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05303995 dev_info(&dd->pdev->dev,
3996 "Shutting down %s ...\n", dd->disk->disk_name);
Asai Thambi S P58c49df2013-01-11 18:47:12 +05303997
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08003998 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
3999 del_gendisk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304000 if (dd->disk->queue) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304001 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004002 blk_mq_free_tag_set(&dd->tags);
Asai Thambi SP02b48262015-05-11 15:48:00 -07004003 }
4004 put_disk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304005 dd->disk = NULL;
4006 dd->queue = NULL;
4007 }
Asai Thambi S P8182b492012-04-09 08:35:38 +02004008
Matthew Wilcox3aed4bc2018-03-21 16:49:06 -04004009 ida_free(&rssd_index_ida, dd->index);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004010 return 0;
4011}
4012
Jens Axboe63166682011-09-27 21:27:43 -06004013static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004014{
4015 dev_info(&dd->pdev->dev,
4016 "Suspending %s ...\n", dd->disk->disk_name);
4017 mtip_hw_suspend(dd);
4018 return 0;
4019}
4020
Jens Axboe63166682011-09-27 21:27:43 -06004021static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004022{
4023 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4024 dd->disk->disk_name);
4025 mtip_hw_resume(dd);
4026 return 0;
4027}
4028
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004029static void drop_cpu(int cpu)
4030{
4031 cpu_use[cpu]--;
4032}
4033
4034static int get_least_used_cpu_on_node(int node)
4035{
4036 int cpu, least_used_cpu, least_cnt;
4037 const struct cpumask *node_mask;
4038
4039 node_mask = cpumask_of_node(node);
4040 least_used_cpu = cpumask_first(node_mask);
4041 least_cnt = cpu_use[least_used_cpu];
4042 cpu = least_used_cpu;
4043
4044 for_each_cpu(cpu, node_mask) {
4045 if (cpu_use[cpu] < least_cnt) {
4046 least_used_cpu = cpu;
4047 least_cnt = cpu_use[cpu];
4048 }
4049 }
4050 cpu_use[least_used_cpu]++;
4051 return least_used_cpu;
4052}
4053
4054/* Helper for selecting a node in round robin mode */
4055static inline int mtip_get_next_rr_node(void)
4056{
4057 static int next_node = -1;
4058
4059 if (next_node == -1) {
4060 next_node = first_online_node;
4061 return next_node;
4062 }
4063
4064 next_node = next_online_node(next_node);
4065 if (next_node == MAX_NUMNODES)
4066 next_node = first_online_node;
4067 return next_node;
4068}
4069
Fengguang Wu25bac122013-01-12 15:31:40 +08004070static DEFINE_HANDLER(0);
4071static DEFINE_HANDLER(1);
4072static DEFINE_HANDLER(2);
4073static DEFINE_HANDLER(3);
4074static DEFINE_HANDLER(4);
4075static DEFINE_HANDLER(5);
4076static DEFINE_HANDLER(6);
4077static DEFINE_HANDLER(7);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004078
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004079static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
4080{
4081 int pos;
4082 unsigned short pcie_dev_ctrl;
4083
4084 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
4085 if (pos) {
4086 pci_read_config_word(pdev,
4087 pos + PCI_EXP_DEVCTL,
4088 &pcie_dev_ctrl);
4089 if (pcie_dev_ctrl & (1 << 11) ||
4090 pcie_dev_ctrl & (1 << 4)) {
4091 dev_info(&dd->pdev->dev,
4092 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4093 pdev->vendor, pdev->device);
4094 pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
4095 PCI_EXP_DEVCTL_RELAX_EN);
4096 pci_write_config_word(pdev,
4097 pos + PCI_EXP_DEVCTL,
4098 pcie_dev_ctrl);
4099 }
4100 }
4101}
4102
4103static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev)
4104{
4105 /*
4106 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4107 * device with device id 0x5aXX
4108 */
4109 if (pdev->bus && pdev->bus->self) {
4110 if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI &&
4111 ((pdev->bus->self->device & 0xff00) == 0x5a00)) {
4112 mtip_disable_link_opts(dd, pdev->bus->self);
4113 } else {
4114 /* Check further up the topology */
4115 struct pci_dev *parent_dev = pdev->bus->self;
4116 if (parent_dev->bus &&
4117 parent_dev->bus->parent &&
4118 parent_dev->bus->parent->self &&
4119 parent_dev->bus->parent->self->vendor ==
4120 PCI_VENDOR_ID_ATI &&
4121 (parent_dev->bus->parent->self->device &
4122 0xff00) == 0x5a00) {
4123 mtip_disable_link_opts(dd,
4124 parent_dev->bus->parent->self);
4125 }
4126 }
4127 }
4128}
4129
Sam Bradshaw88523a62011-08-30 08:34:26 -06004130/*
4131 * Called for each supported PCI device detected.
4132 *
4133 * This function allocates the private data structure, enables the
4134 * PCI device and then calls the block layer initialization function.
4135 *
4136 * return value
4137 * 0 on success else an error code.
4138 */
4139static int mtip_pci_probe(struct pci_dev *pdev,
4140 const struct pci_device_id *ent)
4141{
4142 int rv = 0;
4143 struct driver_data *dd = NULL;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004144 char cpu_list[256];
4145 const struct cpumask *node_mask;
4146 int cpu, i = 0, j = 0;
4147 int my_node = NUMA_NO_NODE;
Asai Thambi S P0caff002013-04-03 19:56:21 +05304148 unsigned long flags;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004149
4150 /* Allocate memory for this devices private data. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004151 my_node = pcibus_to_node(pdev->bus);
4152 if (my_node != NUMA_NO_NODE) {
4153 if (!node_online(my_node))
4154 my_node = mtip_get_next_rr_node();
4155 } else {
4156 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4157 my_node = mtip_get_next_rr_node();
4158 }
4159 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4160 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
Jens Axboe7f328902014-03-10 14:29:37 -06004161 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004162
4163 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004164 if (dd == NULL) {
4165 dev_err(&pdev->dev,
4166 "Unable to allocate memory for driver data\n");
4167 return -ENOMEM;
4168 }
4169
Sam Bradshaw88523a62011-08-30 08:34:26 -06004170 /* Attach the private data to this PCI device. */
4171 pci_set_drvdata(pdev, dd);
4172
4173 rv = pcim_enable_device(pdev);
4174 if (rv < 0) {
4175 dev_err(&pdev->dev, "Unable to enable device\n");
4176 goto iomap_err;
4177 }
4178
4179 /* Map BAR5 to memory. */
4180 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4181 if (rv < 0) {
4182 dev_err(&pdev->dev, "Unable to map regions\n");
4183 goto iomap_err;
4184 }
4185
Christoph Hellwigee75fa22018-10-18 15:15:16 +02004186 rv = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
4187 if (rv) {
4188 dev_warn(&pdev->dev, "64-bit DMA enable failed\n");
4189 goto setmask_err;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004190 }
4191
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004192 /* Copy the info we may need later into the private data structure. */
4193 dd->major = mtip_major;
4194 dd->instance = instance;
4195 dd->pdev = pdev;
4196 dd->numa_node = my_node;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004197
Asai Thambi S P0caff002013-04-03 19:56:21 +05304198 INIT_LIST_HEAD(&dd->online_list);
4199 INIT_LIST_HEAD(&dd->remove_list);
4200
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004201 memset(dd->workq_name, 0, 32);
4202 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4203
4204 dd->isr_workq = create_workqueue(dd->workq_name);
4205 if (!dd->isr_workq) {
4206 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
Wei Yongjund137c832013-03-22 08:58:23 -06004207 rv = -ENOMEM;
Christophe JAILLET8e3c2832018-05-10 09:27:31 +02004208 goto setmask_err;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004209 }
4210
4211 memset(cpu_list, 0, sizeof(cpu_list));
4212
4213 node_mask = cpumask_of_node(dd->numa_node);
4214 if (!cpumask_empty(node_mask)) {
4215 for_each_cpu(cpu, node_mask)
4216 {
4217 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4218 j = strlen(cpu_list);
4219 }
4220
4221 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4222 dd->numa_node,
4223 topology_physical_package_id(cpumask_first(node_mask)),
4224 nr_cpus_node(dd->numa_node),
4225 cpu_list);
4226 } else
4227 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4228
4229 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4230 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4231 cpu_to_node(dd->isr_binding), dd->isr_binding);
4232
4233 /* first worker context always runs in ISR */
4234 dd->work[0].cpu_binding = dd->isr_binding;
4235 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4236 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4237 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4238 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4239 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4240 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4241 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4242
4243 /* Log the bindings */
4244 for_each_present_cpu(cpu) {
4245 memset(cpu_list, 0, sizeof(cpu_list));
4246 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4247 if (dd->work[i].cpu_binding == cpu) {
4248 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4249 j = strlen(cpu_list);
4250 }
4251 }
4252 if (j)
4253 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4254 }
4255
4256 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4257 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4258 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4259 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4260 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4261 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4262 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4263 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4264
4265 pci_set_master(pdev);
Wei Yongjund137c832013-03-22 08:58:23 -06004266 rv = pci_enable_msi(pdev);
4267 if (rv) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004268 dev_warn(&pdev->dev,
4269 "Unable to enable MSI interrupt.\n");
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004270 goto msi_initialize_err;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004271 }
4272
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004273 mtip_fix_ero_nosnoop(dd, pdev);
4274
Sam Bradshaw88523a62011-08-30 08:34:26 -06004275 /* Initialize the block layer. */
4276 rv = mtip_block_initialize(dd);
4277 if (rv < 0) {
4278 dev_err(&pdev->dev,
4279 "Unable to initialize block layer\n");
4280 goto block_initialize_err;
4281 }
4282
4283 /*
4284 * Increment the instance count so that each device has a unique
4285 * instance number.
4286 */
4287 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02004288 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004289 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P6b06d352013-04-03 19:54:35 +05304290 else
4291 rv = 0; /* device in rebuild state, return 0 from probe */
Asai Thambi S P0caff002013-04-03 19:56:21 +05304292
4293 /* Add to online list even if in ftl rebuild */
4294 spin_lock_irqsave(&dev_lock, flags);
4295 list_add(&dd->online_list, &online_list);
4296 spin_unlock_irqrestore(&dev_lock, flags);
4297
Sam Bradshaw88523a62011-08-30 08:34:26 -06004298 goto done;
4299
4300block_initialize_err:
4301 pci_disable_msi(pdev);
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004302
4303msi_initialize_err:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004304 if (dd->isr_workq) {
4305 flush_workqueue(dd->isr_workq);
4306 destroy_workqueue(dd->isr_workq);
4307 drop_cpu(dd->work[0].cpu_binding);
4308 drop_cpu(dd->work[1].cpu_binding);
4309 drop_cpu(dd->work[2].cpu_binding);
4310 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004311setmask_err:
4312 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4313
4314iomap_err:
4315 kfree(dd);
4316 pci_set_drvdata(pdev, NULL);
4317 return rv;
4318done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06004319 return rv;
4320}
4321
4322/*
4323 * Called for each probed device when the device is removed or the
4324 * driver is unloaded.
4325 *
4326 * return value
4327 * None
4328 */
4329static void mtip_pci_remove(struct pci_dev *pdev)
4330{
4331 struct driver_data *dd = pci_get_drvdata(pdev);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004332 unsigned long flags, to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004333
Asai Thambi SP51c65702016-02-24 21:18:10 -08004334 set_bit(MTIP_DDF_REMOVAL_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02004335
Asai Thambi S P0caff002013-04-03 19:56:21 +05304336 spin_lock_irqsave(&dev_lock, flags);
4337 list_del_init(&dd->online_list);
4338 list_add(&dd->remove_list, &removing_list);
4339 spin_unlock_irqrestore(&dev_lock, flags);
4340
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004341 mtip_check_surprise_removal(pdev);
4342 synchronize_irq(dd->pdev->irq);
4343
4344 /* Spin until workers are done */
4345 to = jiffies + msecs_to_jiffies(4000);
4346 do {
4347 msleep(20);
4348 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4349 time_before(jiffies, to));
4350
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004351 if (!dd->sr)
4352 fsync_bdev(dd->bdev);
Asai Thambi SP51c65702016-02-24 21:18:10 -08004353
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004354 if (atomic_read(&dd->irq_workers_active) != 0) {
4355 dev_warn(&dd->pdev->dev,
4356 "Completion workers still active!\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004357 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004358
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004359 blk_set_queue_dying(dd->queue);
Asai Thambi SP51c65702016-02-24 21:18:10 -08004360 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
4361
Sam Bradshaw88523a62011-08-30 08:34:26 -06004362 /* Clean up the block layer. */
4363 mtip_block_remove(dd);
4364
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004365 if (dd->isr_workq) {
4366 flush_workqueue(dd->isr_workq);
4367 destroy_workqueue(dd->isr_workq);
4368 drop_cpu(dd->work[0].cpu_binding);
4369 drop_cpu(dd->work[1].cpu_binding);
4370 drop_cpu(dd->work[2].cpu_binding);
4371 }
4372
Sam Bradshaw88523a62011-08-30 08:34:26 -06004373 pci_disable_msi(pdev);
4374
Asai Thambi S P0caff002013-04-03 19:56:21 +05304375 spin_lock_irqsave(&dev_lock, flags);
4376 list_del_init(&dd->remove_list);
4377 spin_unlock_irqrestore(&dev_lock, flags);
4378
Asai Thambi SP2132a542015-05-11 15:53:18 -07004379 kfree(dd);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004380
Sam Bradshaw88523a62011-08-30 08:34:26 -06004381 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004382 pci_set_drvdata(pdev, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004383}
4384
4385/*
4386 * Called for each probed device when the device is suspended.
4387 *
4388 * return value
4389 * 0 Success
4390 * <0 Error
4391 */
4392static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4393{
4394 int rv = 0;
4395 struct driver_data *dd = pci_get_drvdata(pdev);
4396
4397 if (!dd) {
4398 dev_err(&pdev->dev,
4399 "Driver private datastructure is NULL\n");
4400 return -EFAULT;
4401 }
4402
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004403 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004404
4405 /* Disable ports & interrupts then send standby immediate */
4406 rv = mtip_block_suspend(dd);
4407 if (rv < 0) {
4408 dev_err(&pdev->dev,
4409 "Failed to suspend controller\n");
4410 return rv;
4411 }
4412
4413 /*
4414 * Save the pci config space to pdev structure &
4415 * disable the device
4416 */
4417 pci_save_state(pdev);
4418 pci_disable_device(pdev);
4419
4420 /* Move to Low power state*/
4421 pci_set_power_state(pdev, PCI_D3hot);
4422
4423 return rv;
4424}
4425
4426/*
4427 * Called for each probed device when the device is resumed.
4428 *
4429 * return value
4430 * 0 Success
4431 * <0 Error
4432 */
4433static int mtip_pci_resume(struct pci_dev *pdev)
4434{
4435 int rv = 0;
4436 struct driver_data *dd;
4437
4438 dd = pci_get_drvdata(pdev);
4439 if (!dd) {
4440 dev_err(&pdev->dev,
4441 "Driver private datastructure is NULL\n");
4442 return -EFAULT;
4443 }
4444
4445 /* Move the device to active State */
4446 pci_set_power_state(pdev, PCI_D0);
4447
4448 /* Restore PCI configuration space */
4449 pci_restore_state(pdev);
4450
4451 /* Enable the PCI device*/
4452 rv = pcim_enable_device(pdev);
4453 if (rv < 0) {
4454 dev_err(&pdev->dev,
4455 "Failed to enable card during resume\n");
4456 goto err;
4457 }
4458 pci_set_master(pdev);
4459
4460 /*
4461 * Calls hbaReset, initPort, & startPort function
4462 * then enables interrupts
4463 */
4464 rv = mtip_block_resume(dd);
4465 if (rv < 0)
4466 dev_err(&pdev->dev, "Unable to resume\n");
4467
4468err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004469 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004470
4471 return rv;
4472}
4473
4474/*
4475 * Shutdown routine
4476 *
4477 * return value
4478 * None
4479 */
4480static void mtip_pci_shutdown(struct pci_dev *pdev)
4481{
4482 struct driver_data *dd = pci_get_drvdata(pdev);
4483 if (dd)
4484 mtip_block_shutdown(dd);
4485}
4486
Sam Bradshaw88523a62011-08-30 08:34:26 -06004487/* Table of device ids supported by this driver. */
Benoit Taine9baa3c32014-08-08 15:56:03 +02004488static const struct pci_device_id mtip_pci_tbl[] = {
Asai Thambi S P1a131452012-09-05 22:00:38 +05304489 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4490 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4491 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4492 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4493 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4494 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4495 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
Sam Bradshaw88523a62011-08-30 08:34:26 -06004496 { 0 }
4497};
4498
4499/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004500static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004501 .name = MTIP_DRV_NAME,
4502 .id_table = mtip_pci_tbl,
4503 .probe = mtip_pci_probe,
4504 .remove = mtip_pci_remove,
4505 .suspend = mtip_pci_suspend,
4506 .resume = mtip_pci_resume,
4507 .shutdown = mtip_pci_shutdown,
4508};
4509
4510MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4511
4512/*
4513 * Module initialization function.
4514 *
4515 * Called once when the module is loaded. This function allocates a major
4516 * block device number to the Cyclone devices and registers the PCI layer
4517 * of the driver.
4518 *
4519 * Return value
4520 * 0 on success else error code.
4521 */
4522static int __init mtip_init(void)
4523{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004524 int error;
4525
Asai Thambi S P45422e72012-09-05 22:03:56 +05304526 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004527
Asai Thambi S P0caff002013-04-03 19:56:21 +05304528 spin_lock_init(&dev_lock);
4529
4530 INIT_LIST_HEAD(&online_list);
4531 INIT_LIST_HEAD(&removing_list);
4532
Sam Bradshaw88523a62011-08-30 08:34:26 -06004533 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004534 error = register_blkdev(0, MTIP_DRV_NAME);
4535 if (error <= 0) {
Asai Thambi S P45422e72012-09-05 22:03:56 +05304536 pr_err("Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004537 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004538 return -EBUSY;
4539 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004540 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004541
Asai Thambi S P0caff002013-04-03 19:56:21 +05304542 dfs_parent = debugfs_create_dir("rssd", NULL);
4543 if (IS_ERR_OR_NULL(dfs_parent)) {
4544 pr_warn("Error creating debugfs parent\n");
4545 dfs_parent = NULL;
4546 }
4547 if (dfs_parent) {
4548 dfs_device_status = debugfs_create_file("device_status",
Joe Perches5657a812018-05-24 13:38:59 -06004549 0444, dfs_parent, NULL,
Asai Thambi S P0caff002013-04-03 19:56:21 +05304550 &mtip_device_status_fops);
4551 if (IS_ERR_OR_NULL(dfs_device_status)) {
4552 pr_err("Error creating device_status node\n");
4553 dfs_device_status = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004554 }
4555 }
4556
Sam Bradshaw88523a62011-08-30 08:34:26 -06004557 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004558 error = pci_register_driver(&mtip_pci_driver);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004559 if (error) {
4560 debugfs_remove(dfs_parent);
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004561 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004562 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004563
4564 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004565}
4566
4567/*
4568 * Module de-initialization function.
4569 *
4570 * Called once when the module is unloaded. This function deallocates
4571 * the major block device number allocated by mtip_init() and
4572 * unregisters the PCI layer of the driver.
4573 *
4574 * Return value
4575 * none
4576 */
4577static void __exit mtip_exit(void)
4578{
Sam Bradshaw88523a62011-08-30 08:34:26 -06004579 /* Release the allocated major block device number. */
4580 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4581
4582 /* Unregister the PCI driver. */
4583 pci_unregister_driver(&mtip_pci_driver);
Asai Thambi S Paf5ded82014-04-16 20:30:16 -07004584
4585 debugfs_remove_recursive(dfs_parent);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004586}
4587
4588MODULE_AUTHOR("Micron Technology, Inc");
4589MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4590MODULE_LICENSE("GPL");
4591MODULE_VERSION(MTIP_DRV_VERSION);
4592
4593module_init(mtip_init);
4594module_exit(mtip_exit);