blob: e873daca9d13a6eaada0a4c75b905c13bf21f268 [file] [log] [blame]
Sam Bradshaw88523a62011-08-30 08:34:26 -06001/*
2 * Driver for the Micron P320 SSD
3 * Copyright (C) 2011 Micron Technology, Inc.
4 *
5 * Portions of this code were derived from works subjected to the
6 * following copyright:
7 * Copyright (C) 2009 Integrated Device Technology, Inc.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 */
20
21#include <linux/pci.h>
22#include <linux/interrupt.h>
23#include <linux/ata.h>
24#include <linux/delay.h>
25#include <linux/hdreg.h>
26#include <linux/uaccess.h>
27#include <linux/random.h>
28#include <linux/smp.h>
29#include <linux/compat.h>
30#include <linux/fs.h>
Jens Axboe0e838c62011-09-28 07:35:40 -060031#include <linux/module.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060032#include <linux/genhd.h>
33#include <linux/blkdev.h>
Jens Axboeffc771b2014-05-09 09:42:02 -060034#include <linux/blk-mq.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060035#include <linux/bio.h>
36#include <linux/dma-mapping.h>
37#include <linux/idr.h>
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +010038#include <linux/kthread.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060039#include <../drivers/ata/ahci.h>
Asai Thambi S P45038362012-04-09 08:35:38 +020040#include <linux/export.h>
Asai Thambi S P7b421d22012-06-04 12:44:02 -070041#include <linux/debugfs.h>
Sam Bradshawf45c40a2014-06-06 13:28:48 -060042#include <linux/prefetch.h>
Sam Bradshaw88523a62011-08-30 08:34:26 -060043#include "mtip32xx.h"
44
45#define HW_CMD_SLOT_SZ (MTIP_MAX_COMMAND_SLOTS * 32)
Sam Bradshaw188b9f42014-01-15 10:14:57 -080046
47/* DMA region containing RX Fis, Identify, RLE10, and SMART buffers */
48#define AHCI_RX_FIS_SZ 0x100
49#define AHCI_RX_FIS_OFFSET 0x0
50#define AHCI_IDFY_SZ ATA_SECT_SIZE
51#define AHCI_IDFY_OFFSET 0x400
52#define AHCI_SECTBUF_SZ ATA_SECT_SIZE
53#define AHCI_SECTBUF_OFFSET 0x800
54#define AHCI_SMARTBUF_SZ ATA_SECT_SIZE
55#define AHCI_SMARTBUF_OFFSET 0xC00
56/* 0x100 + 0x200 + 0x200 + 0x200 is smaller than 4k but we pad it out */
57#define BLOCK_DMA_ALLOC_SZ 4096
58
59/* DMA region containing command table (should be 8192 bytes) */
60#define AHCI_CMD_SLOT_SZ sizeof(struct mtip_cmd_hdr)
61#define AHCI_CMD_TBL_SZ (MTIP_MAX_COMMAND_SLOTS * AHCI_CMD_SLOT_SZ)
62#define AHCI_CMD_TBL_OFFSET 0x0
63
64/* DMA region per command (contains header and SGL) */
65#define AHCI_CMD_TBL_HDR_SZ 0x80
66#define AHCI_CMD_TBL_HDR_OFFSET 0x0
67#define AHCI_CMD_TBL_SGL_SZ (MTIP_MAX_SG * sizeof(struct mtip_cmd_sg))
68#define AHCI_CMD_TBL_SGL_OFFSET AHCI_CMD_TBL_HDR_SZ
69#define CMD_DMA_ALLOC_SZ (AHCI_CMD_TBL_SGL_SZ + AHCI_CMD_TBL_HDR_SZ)
70
Sam Bradshaw88523a62011-08-30 08:34:26 -060071
Asai Thambi S P45038362012-04-09 08:35:38 +020072#define HOST_CAP_NZDMA (1 << 19)
Sam Bradshaw88523a62011-08-30 08:34:26 -060073#define HOST_HSORG 0xFC
74#define HSORG_DISABLE_SLOTGRP_INTR (1<<24)
75#define HSORG_DISABLE_SLOTGRP_PXIS (1<<16)
76#define HSORG_HWREV 0xFF00
77#define HSORG_STYLE 0x8
78#define HSORG_SLOTGROUPS 0x7
79
80#define PORT_COMMAND_ISSUE 0x38
81#define PORT_SDBV 0x7C
82
83#define PORT_OFFSET 0x100
84#define PORT_MEM_SIZE 0x80
85
86#define PORT_IRQ_ERR \
87 (PORT_IRQ_HBUS_ERR | PORT_IRQ_IF_ERR | PORT_IRQ_CONNECT | \
88 PORT_IRQ_PHYRDY | PORT_IRQ_UNK_FIS | PORT_IRQ_BAD_PMP | \
89 PORT_IRQ_TF_ERR | PORT_IRQ_HBUS_DATA_ERR | PORT_IRQ_IF_NONFATAL | \
90 PORT_IRQ_OVERFLOW)
91#define PORT_IRQ_LEGACY \
92 (PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS)
93#define PORT_IRQ_HANDLED \
94 (PORT_IRQ_SDB_FIS | PORT_IRQ_LEGACY | \
95 PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR | \
96 PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)
97#define DEF_PORT_IRQ \
98 (PORT_IRQ_ERR | PORT_IRQ_LEGACY | PORT_IRQ_SDB_FIS)
99
100/* product numbers */
101#define MTIP_PRODUCT_UNKNOWN 0x00
102#define MTIP_PRODUCT_ASICFPGA 0x11
103
104/* Device instance number, incremented each time a device is probed. */
105static int instance;
106
Zhu Yanjun9e35fdc2016-01-05 18:39:04 +0800107static struct list_head online_list;
108static struct list_head removing_list;
109static spinlock_t dev_lock;
Asai Thambi S P0caff002013-04-03 19:56:21 +0530110
Sam Bradshaw88523a62011-08-30 08:34:26 -0600111/*
112 * Global variable used to hold the major block device number
113 * allocated in mtip_init().
114 */
Jens Axboe3ff147d2011-09-27 21:33:53 -0600115static int mtip_major;
Asai Thambi S P7b421d22012-06-04 12:44:02 -0700116static struct dentry *dfs_parent;
Asai Thambi S P0caff002013-04-03 19:56:21 +0530117static struct dentry *dfs_device_status;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600118
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800119static u32 cpu_use[NR_CPUS];
120
Sam Bradshaw88523a62011-08-30 08:34:26 -0600121static DEFINE_SPINLOCK(rssd_index_lock);
122static DEFINE_IDA(rssd_index_ida);
123
Asai Thambi S P62ee8c12012-01-04 22:01:32 +0100124static int mtip_block_initialize(struct driver_data *dd);
125
Jens Axboe16d02c02011-09-27 15:50:01 -0600126#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -0600127struct mtip_compat_ide_task_request_s {
128 __u8 io_ports[8];
129 __u8 hob_ports[8];
130 ide_reg_valid_t out_flags;
131 ide_reg_valid_t in_flags;
132 int data_phase;
133 int req_cmd;
134 compat_ulong_t out_size;
135 compat_ulong_t in_size;
136};
Jens Axboe16d02c02011-09-27 15:50:01 -0600137#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -0600138
Sam Bradshaw88523a62011-08-30 08:34:26 -0600139/*
Jens Axboe63166682011-09-27 21:27:43 -0600140 * This function check_for_surprise_removal is called
141 * while card is removed from the system and it will
142 * read the vendor id from the configration space
143 *
144 * @pdev Pointer to the pci_dev structure.
145 *
146 * return value
147 * true if device removed, else false
148 */
149static bool mtip_check_surprise_removal(struct pci_dev *pdev)
150{
151 u16 vendor_id = 0;
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600152 struct driver_data *dd = pci_get_drvdata(pdev);
153
154 if (dd->sr)
155 return true;
Jens Axboe63166682011-09-27 21:27:43 -0600156
157 /* Read the vendorID from the configuration space */
158 pci_read_config_word(pdev, 0x00, &vendor_id);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600159 if (vendor_id == 0xFFFF) {
160 dd->sr = true;
161 if (dd->queue)
Bart Van Assche4e699cb2018-03-07 17:10:06 -0800162 blk_queue_flag_set(QUEUE_FLAG_DEAD, dd->queue);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600163 else
164 dev_warn(&dd->pdev->dev,
165 "%s: dd->queue is NULL\n", __func__);
Jens Axboe63166682011-09-27 21:27:43 -0600166 return true; /* device removed */
Jens Axboe63166682011-09-27 21:27:43 -0600167 }
168
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600169 return false; /* device present */
Jens Axboe63166682011-09-27 21:27:43 -0600170}
171
Ming Leia4e84aa2017-04-27 07:45:18 -0600172/* we have to use runtime tag to setup command header */
173static void mtip_init_cmd_header(struct request *rq)
174{
175 struct driver_data *dd = rq->q->queuedata;
176 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
Ming Leia4e84aa2017-04-27 07:45:18 -0600177
178 /* Point the command headers at the command tables. */
179 cmd->command_header = dd->port->command_list +
180 (sizeof(struct mtip_cmd_hdr) * rq->tag);
181 cmd->command_header_dma = dd->port->command_list_dma +
182 (sizeof(struct mtip_cmd_hdr) * rq->tag);
183
Ming Lei8a05aa42017-07-05 12:14:27 +0800184 if (test_bit(MTIP_PF_HOST_CAP_64, &dd->port->flags))
Ming Leia4e84aa2017-04-27 07:45:18 -0600185 cmd->command_header->ctbau = __force_bit2int cpu_to_le32((cmd->command_dma >> 16) >> 16);
186
187 cmd->command_header->ctba = __force_bit2int cpu_to_le32(cmd->command_dma & 0xFFFFFFFF);
188}
189
Jens Axboeffc771b2014-05-09 09:42:02 -0600190static struct mtip_cmd *mtip_get_int_command(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600191{
Jens Axboeffc771b2014-05-09 09:42:02 -0600192 struct request *rq;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600193
Asai Thambi SP008e56d2016-02-24 21:21:20 -0800194 if (mtip_check_surprise_removal(dd->pdev))
195 return NULL;
196
Jens Axboe3f5e6a32017-04-28 10:45:08 -0600197 rq = blk_mq_alloc_request(dd->queue, REQ_OP_DRV_IN, BLK_MQ_REQ_RESERVED);
Asai Thambi SP008e56d2016-02-24 21:21:20 -0800198 if (IS_ERR(rq))
199 return NULL;
200
Ming Leia4e84aa2017-04-27 07:45:18 -0600201 /* Internal cmd isn't submitted via .queue_rq */
202 mtip_init_cmd_header(rq);
203
Jens Axboeffc771b2014-05-09 09:42:02 -0600204 return blk_mq_rq_to_pdu(rq);
205}
Sam Bradshaw88523a62011-08-30 08:34:26 -0600206
Jens Axboeffc771b2014-05-09 09:42:02 -0600207static struct mtip_cmd *mtip_cmd_from_tag(struct driver_data *dd,
208 unsigned int tag)
209{
Jens Axboe6f63503c2017-05-02 09:56:00 -0600210 struct blk_mq_hw_ctx *hctx = dd->queue->queue_hw_ctx[0];
Jens Axboeffc771b2014-05-09 09:42:02 -0600211
Jens Axboe6f63503c2017-05-02 09:56:00 -0600212 return blk_mq_rq_to_pdu(blk_mq_tag_to_rq(hctx->tags, tag));
Asai Thambi S P8f8b8992013-09-11 13:14:42 -0600213}
214
215/*
Jens Axboe63166682011-09-27 21:27:43 -0600216 * Reset the HBA (without sleeping)
217 *
Jens Axboe63166682011-09-27 21:27:43 -0600218 * @dd Pointer to the driver data structure.
219 *
220 * return value
221 * 0 The reset was successful.
222 * -1 The HBA Reset bit did not clear.
223 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530224static int mtip_hba_reset(struct driver_data *dd)
Jens Axboe63166682011-09-27 21:27:43 -0600225{
226 unsigned long timeout;
227
Jens Axboe63166682011-09-27 21:27:43 -0600228 /* Set the reset bit */
229 writel(HOST_RESET, dd->mmio + HOST_CTL);
230
231 /* Flush */
232 readl(dd->mmio + HOST_CTL);
233
Asai Thambi SP2f17d712015-05-11 15:57:16 -0700234 /*
235 * Spin for up to 10 seconds waiting for reset acknowledgement. Spec
236 * is 1 sec but in LUN failure conditions, up to 10 secs are required
237 */
238 timeout = jiffies + msecs_to_jiffies(10000);
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530239 do {
240 mdelay(10);
241 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))
242 return -1;
Jens Axboe63166682011-09-27 21:27:43 -0600243
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530244 } while ((readl(dd->mmio + HOST_CTL) & HOST_RESET)
245 && time_before(jiffies, timeout));
Asai Thambi S P45038362012-04-09 08:35:38 +0200246
Jens Axboe63166682011-09-27 21:27:43 -0600247 if (readl(dd->mmio + HOST_CTL) & HOST_RESET)
248 return -1;
249
250 return 0;
251}
252
253/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600254 * Issue a command to the hardware.
255 *
256 * Set the appropriate bit in the s_active and Command Issue hardware
257 * registers, causing hardware command processing to begin.
258 *
259 * @port Pointer to the port structure.
260 * @tag The tag of the command to be issued.
261 *
262 * return value
263 * None
264 */
265static inline void mtip_issue_ncq_command(struct mtip_port *port, int tag)
266{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800267 int group = tag >> 5;
268
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800269 /* guard SACT and CI registers */
270 spin_lock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600271 writel((1 << MTIP_TAG_BIT(tag)),
272 port->s_active[MTIP_TAG_INDEX(tag)]);
273 writel((1 << MTIP_TAG_BIT(tag)),
274 port->cmd_issue[MTIP_TAG_INDEX(tag)]);
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800275 spin_unlock(&port->cmd_issue_lock[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600276}
277
278/*
Jens Axboe63166682011-09-27 21:27:43 -0600279 * Enable/disable the reception of FIS
280 *
281 * @port Pointer to the port data structure
282 * @enable 1 to enable, 0 to disable
283 *
284 * return value
285 * Previous state: 1 enabled, 0 disabled
286 */
287static int mtip_enable_fis(struct mtip_port *port, int enable)
288{
289 u32 tmp;
290
291 /* enable FIS reception */
292 tmp = readl(port->mmio + PORT_CMD);
293 if (enable)
294 writel(tmp | PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
295 else
296 writel(tmp & ~PORT_CMD_FIS_RX, port->mmio + PORT_CMD);
297
298 /* Flush */
299 readl(port->mmio + PORT_CMD);
300
301 return (((tmp & PORT_CMD_FIS_RX) == PORT_CMD_FIS_RX));
302}
303
304/*
305 * Enable/disable the DMA engine
306 *
307 * @port Pointer to the port data structure
308 * @enable 1 to enable, 0 to disable
309 *
310 * return value
311 * Previous state: 1 enabled, 0 disabled.
312 */
313static int mtip_enable_engine(struct mtip_port *port, int enable)
314{
315 u32 tmp;
316
317 /* enable FIS reception */
318 tmp = readl(port->mmio + PORT_CMD);
319 if (enable)
320 writel(tmp | PORT_CMD_START, port->mmio + PORT_CMD);
321 else
322 writel(tmp & ~PORT_CMD_START, port->mmio + PORT_CMD);
323
324 readl(port->mmio + PORT_CMD);
325 return (((tmp & PORT_CMD_START) == PORT_CMD_START));
326}
327
328/*
329 * Enables the port DMA engine and FIS reception.
330 *
331 * return value
332 * None
333 */
334static inline void mtip_start_port(struct mtip_port *port)
335{
336 /* Enable FIS reception */
337 mtip_enable_fis(port, 1);
338
339 /* Enable the DMA engine */
340 mtip_enable_engine(port, 1);
341}
342
343/*
344 * Deinitialize a port by disabling port interrupts, the DMA engine,
345 * and FIS reception.
346 *
347 * @port Pointer to the port structure
348 *
349 * return value
350 * None
351 */
352static inline void mtip_deinit_port(struct mtip_port *port)
353{
354 /* Disable interrupts on this port */
355 writel(0, port->mmio + PORT_IRQ_MASK);
356
357 /* Disable the DMA engine */
358 mtip_enable_engine(port, 0);
359
360 /* Disable FIS reception */
361 mtip_enable_fis(port, 0);
362}
363
364/*
365 * Initialize a port.
366 *
367 * This function deinitializes the port by calling mtip_deinit_port() and
368 * then initializes it by setting the command header and RX FIS addresses,
369 * clearing the SError register and any pending port interrupts before
370 * re-enabling the default set of port interrupts.
371 *
372 * @port Pointer to the port structure.
373 *
374 * return value
375 * None
376 */
377static void mtip_init_port(struct mtip_port *port)
378{
379 int i;
380 mtip_deinit_port(port);
381
382 /* Program the command list base and FIS base addresses */
383 if (readl(port->dd->mmio + HOST_CAP) & HOST_CAP_64) {
384 writel((port->command_list_dma >> 16) >> 16,
385 port->mmio + PORT_LST_ADDR_HI);
386 writel((port->rxfis_dma >> 16) >> 16,
387 port->mmio + PORT_FIS_ADDR_HI);
Ming Lei8a05aa42017-07-05 12:14:27 +0800388 set_bit(MTIP_PF_HOST_CAP_64, &port->flags);
Jens Axboe63166682011-09-27 21:27:43 -0600389 }
390
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100391 writel(port->command_list_dma & 0xFFFFFFFF,
Jens Axboe63166682011-09-27 21:27:43 -0600392 port->mmio + PORT_LST_ADDR);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100393 writel(port->rxfis_dma & 0xFFFFFFFF, port->mmio + PORT_FIS_ADDR);
Jens Axboe63166682011-09-27 21:27:43 -0600394
395 /* Clear SError */
396 writel(readl(port->mmio + PORT_SCR_ERR), port->mmio + PORT_SCR_ERR);
397
398 /* reset the completed registers.*/
399 for (i = 0; i < port->dd->slot_groups; i++)
400 writel(0xFFFFFFFF, port->completed[i]);
401
402 /* Clear any pending interrupts for this port */
Asai Thambi S P6bb688c2012-05-29 18:40:45 -0700403 writel(readl(port->mmio + PORT_IRQ_STAT), port->mmio + PORT_IRQ_STAT);
Jens Axboe63166682011-09-27 21:27:43 -0600404
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100405 /* Clear any pending interrupts on the HBA. */
406 writel(readl(port->dd->mmio + HOST_IRQ_STAT),
407 port->dd->mmio + HOST_IRQ_STAT);
408
Jens Axboe63166682011-09-27 21:27:43 -0600409 /* Enable port interrupts */
410 writel(DEF_PORT_IRQ, port->mmio + PORT_IRQ_MASK);
411}
412
413/*
414 * Restart a port
415 *
416 * @port Pointer to the port data structure.
417 *
418 * return value
419 * None
420 */
421static void mtip_restart_port(struct mtip_port *port)
422{
423 unsigned long timeout;
424
425 /* Disable the DMA engine */
426 mtip_enable_engine(port, 0);
427
428 /* Chip quirk: wait up to 500ms for PxCMD.CR == 0 */
429 timeout = jiffies + msecs_to_jiffies(500);
430 while ((readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON)
431 && time_before(jiffies, timeout))
432 ;
433
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200434 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200435 return;
436
Jens Axboe63166682011-09-27 21:27:43 -0600437 /*
438 * Chip quirk: escalate to hba reset if
439 * PxCMD.CR not clear after 500 ms
440 */
441 if (readl(port->mmio + PORT_CMD) & PORT_CMD_LIST_ON) {
442 dev_warn(&port->dd->pdev->dev,
443 "PxCMD.CR not clear, escalating reset\n");
444
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530445 if (mtip_hba_reset(port->dd))
Jens Axboe63166682011-09-27 21:27:43 -0600446 dev_err(&port->dd->pdev->dev,
447 "HBA reset escalation failed.\n");
448
449 /* 30 ms delay before com reset to quiesce chip */
450 mdelay(30);
451 }
452
453 dev_warn(&port->dd->pdev->dev, "Issuing COM reset\n");
454
455 /* Set PxSCTL.DET */
456 writel(readl(port->mmio + PORT_SCR_CTL) |
457 1, port->mmio + PORT_SCR_CTL);
458 readl(port->mmio + PORT_SCR_CTL);
459
460 /* Wait 1 ms to quiesce chip function */
461 timeout = jiffies + msecs_to_jiffies(1);
462 while (time_before(jiffies, timeout))
463 ;
464
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200465 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200466 return;
467
Jens Axboe63166682011-09-27 21:27:43 -0600468 /* Clear PxSCTL.DET */
469 writel(readl(port->mmio + PORT_SCR_CTL) & ~1,
470 port->mmio + PORT_SCR_CTL);
471 readl(port->mmio + PORT_SCR_CTL);
472
473 /* Wait 500 ms for bit 0 of PORT_SCR_STS to be set */
474 timeout = jiffies + msecs_to_jiffies(500);
475 while (((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
476 && time_before(jiffies, timeout))
477 ;
478
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200479 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +0200480 return;
481
Jens Axboe63166682011-09-27 21:27:43 -0600482 if ((readl(port->mmio + PORT_SCR_STAT) & 0x01) == 0)
483 dev_warn(&port->dd->pdev->dev,
484 "COM reset failed\n");
485
Asai Thambi S P22be2e62012-03-23 12:33:03 +0100486 mtip_init_port(port);
487 mtip_start_port(port);
Jens Axboe63166682011-09-27 21:27:43 -0600488
Jens Axboe63166682011-09-27 21:27:43 -0600489}
490
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +0530491static int mtip_device_reset(struct driver_data *dd)
492{
493 int rv = 0;
494
495 if (mtip_check_surprise_removal(dd->pdev))
496 return 0;
497
498 if (mtip_hba_reset(dd) < 0)
499 rv = -EFAULT;
500
501 mdelay(1);
502 mtip_init_port(dd->port);
503 mtip_start_port(dd->port);
504
505 /* Enable interrupts on the HBA. */
506 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
507 dd->mmio + HOST_CTL);
508 return rv;
509}
510
Jens Axboe63166682011-09-27 21:27:43 -0600511/*
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200512 * Helper function for tag logging
513 */
514static void print_tags(struct driver_data *dd,
515 char *msg,
516 unsigned long *tagbits,
517 int cnt)
518{
519 unsigned char tagmap[128];
520 int group, tagmap_len = 0;
521
522 memset(tagmap, 0, sizeof(tagmap));
523 for (group = SLOTBITS_IN_LONGS; group > 0; group--)
Jens Axboeffc771b2014-05-09 09:42:02 -0600524 tagmap_len += sprintf(tagmap + tagmap_len, "%016lX ",
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200525 tagbits[group-1]);
526 dev_warn(&dd->pdev->dev,
527 "%d command(s) %s: tagmap [%s]", cnt, msg, tagmap);
528}
529
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200530static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
531 dma_addr_t buffer_dma, unsigned int sectors);
532static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
533 struct smart_attr *attrib);
Jens Axboe6f63503c2017-05-02 09:56:00 -0600534
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200535static void mtip_complete_command(struct mtip_cmd *cmd, blk_status_t status)
Jens Axboe6f63503c2017-05-02 09:56:00 -0600536{
537 struct request *req = blk_mq_rq_from_pdu(cmd);
538
539 cmd->status = status;
540 blk_mq_complete_request(req);
541}
542
Sam Bradshaw88523a62011-08-30 08:34:26 -0600543/*
544 * Handle an error.
545 *
546 * @dd Pointer to the DRIVER_DATA structure.
547 *
548 * return value
549 * None
550 */
551static void mtip_handle_tfe(struct driver_data *dd)
552{
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200553 int group, tag, bit, reissue, rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600554 struct mtip_port *port;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200555 struct mtip_cmd *cmd;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600556 u32 completed;
557 struct host_to_dev_fis *fis;
558 unsigned long tagaccum[SLOTBITS_IN_LONGS];
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200559 unsigned int cmd_cnt = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200560 unsigned char *buf;
561 char *fail_reason = NULL;
562 int fail_all_ncq_write = 0, fail_all_ncq_cmds = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600563
564 dev_warn(&dd->pdev->dev, "Taskfile error\n");
565
566 port = dd->port;
567
Asai Thambi SPa7806fa2015-05-11 15:49:28 -0700568 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags)) {
Jens Axboeffc771b2014-05-09 09:42:02 -0600569 cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700570 dbg_printk(MTIP_DRV_NAME " TFE for the internal command\n");
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200571 mtip_complete_command(cmd, BLK_STS_IOERR);
Asai Thambi SPe35b9472016-02-24 21:16:21 -0800572 return;
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -0700573 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600574
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200575 /* clear the tag accumulator */
576 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
577
Sam Bradshaw88523a62011-08-30 08:34:26 -0600578 /* Loop through all the groups */
579 for (group = 0; group < dd->slot_groups; group++) {
580 completed = readl(port->completed[group]);
581
Jens Axboeffc771b2014-05-09 09:42:02 -0600582 dev_warn(&dd->pdev->dev, "g=%u, comp=%x\n", group, completed);
583
Sam Bradshaw88523a62011-08-30 08:34:26 -0600584 /* clear completed status register in the hardware.*/
585 writel(completed, port->completed[group]);
586
Sam Bradshaw88523a62011-08-30 08:34:26 -0600587 /* Process successfully completed commands */
588 for (bit = 0; bit < 32 && completed; bit++) {
589 if (!(completed & (1<<bit)))
590 continue;
591 tag = (group << 5) + bit;
592
593 /* Skip the internal command slot */
594 if (tag == MTIP_TAG_INTERNAL)
595 continue;
596
Jens Axboeffc771b2014-05-09 09:42:02 -0600597 cmd = mtip_cmd_from_tag(dd, tag);
Jens Axboe6f63503c2017-05-02 09:56:00 -0600598 mtip_complete_command(cmd, 0);
599 set_bit(tag, tagaccum);
600 cmd_cnt++;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600601 }
602 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200603
604 print_tags(dd, "completed (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600605
606 /* Restart the port */
607 mdelay(20);
608 mtip_restart_port(port);
609
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200610 /* Trying to determine the cause of the error */
611 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
612 dd->port->log_buf,
613 dd->port->log_buf_dma, 1);
614 if (rv) {
615 dev_warn(&dd->pdev->dev,
616 "Error in READ LOG EXT (10h) command\n");
617 /* non-critical error, don't fail the load */
618 } else {
619 buf = (unsigned char *)dd->port->log_buf;
620 if (buf[259] & 0x1) {
621 dev_info(&dd->pdev->dev,
622 "Write protect bit is set.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200623 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200624 fail_all_ncq_write = 1;
625 fail_reason = "write protect";
626 }
627 if (buf[288] == 0xF7) {
628 dev_info(&dd->pdev->dev,
629 "Exceeded Tmax, drive in thermal shutdown.\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200630 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200631 fail_all_ncq_cmds = 1;
632 fail_reason = "thermal shutdown";
633 }
634 if (buf[288] == 0xBF) {
Asai Thambi SPaae4a032016-02-24 21:18:20 -0800635 set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200636 dev_info(&dd->pdev->dev,
Sam Bradshaw26d58052013-10-03 10:18:05 -0700637 "Drive indicates rebuild has failed. Secure erase required.\n");
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200638 fail_all_ncq_cmds = 1;
639 fail_reason = "rebuild failed";
640 }
641 }
642
Sam Bradshaw88523a62011-08-30 08:34:26 -0600643 /* clear the tag accumulator */
644 memset(tagaccum, 0, SLOTBITS_IN_LONGS * sizeof(long));
645
646 /* Loop through all the groups */
647 for (group = 0; group < dd->slot_groups; group++) {
648 for (bit = 0; bit < 32; bit++) {
649 reissue = 1;
650 tag = (group << 5) + bit;
Jens Axboeffc771b2014-05-09 09:42:02 -0600651 cmd = mtip_cmd_from_tag(dd, tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600652
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200653 fis = (struct host_to_dev_fis *)cmd->command;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600654
655 /* Should re-issue? */
656 if (tag == MTIP_TAG_INTERNAL ||
657 fis->command == ATA_CMD_SET_FEATURES)
658 reissue = 0;
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200659 else {
660 if (fail_all_ncq_cmds ||
661 (fail_all_ncq_write &&
662 fis->command == ATA_CMD_FPDMA_WRITE)) {
663 dev_warn(&dd->pdev->dev,
664 " Fail: %s w/tag %d [%s].\n",
665 fis->command == ATA_CMD_FPDMA_WRITE ?
666 "write" : "read",
667 tag,
668 fail_reason != NULL ?
669 fail_reason : "unknown");
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200670 mtip_complete_command(cmd, BLK_STS_MEDIUM);
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200671 continue;
672 }
673 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600674
675 /*
676 * First check if this command has
677 * exceeded its retries.
678 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +0200679 if (reissue && (cmd->retries-- > 0)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600680
681 set_bit(tag, tagaccum);
682
Sam Bradshaw88523a62011-08-30 08:34:26 -0600683 /* Re-issue the command. */
684 mtip_issue_ncq_command(port, tag);
685
686 continue;
687 }
688
689 /* Retire a command that will not be reissued */
690 dev_warn(&port->dd->pdev->dev,
691 "retiring tag %d\n", tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600692
Christoph Hellwig2a842ac2017-06-03 09:38:04 +0200693 mtip_complete_command(cmd, BLK_STS_IOERR);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600694 }
695 }
Asai Thambi S P95fea2f2012-04-09 08:35:39 +0200696 print_tags(dd, "reissued (TFE)", tagaccum, cmd_cnt);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600697}
698
699/*
700 * Handle a set device bits interrupt
701 */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800702static inline void mtip_workq_sdbfx(struct mtip_port *port, int group,
703 u32 completed)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600704{
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800705 struct driver_data *dd = port->dd;
706 int tag, bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600707 struct mtip_cmd *command;
708
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800709 if (!completed) {
710 WARN_ON_ONCE(!completed);
711 return;
712 }
713 /* clear completed status register in the hardware.*/
714 writel(completed, port->completed[group]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600715
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800716 /* Process completed commands. */
717 for (bit = 0; (bit < 32) && completed; bit++) {
718 if (completed & 0x01) {
719 tag = (group << 5) | bit;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600720
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800721 /* skip internal command slot. */
722 if (unlikely(tag == MTIP_TAG_INTERNAL))
723 continue;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600724
Jens Axboeffc771b2014-05-09 09:42:02 -0600725 command = mtip_cmd_from_tag(dd, tag);
Jens Axboe6f63503c2017-05-02 09:56:00 -0600726 mtip_complete_command(command, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600727 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800728 completed >>= 1;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600729 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800730
731 /* If last, re-enable interrupts */
732 if (atomic_dec_return(&dd->irq_workers_active) == 0)
733 writel(0xffffffff, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600734}
735
736/*
737 * Process legacy pio and d2h interrupts
738 */
739static inline void mtip_process_legacy(struct driver_data *dd, u32 port_stat)
740{
741 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -0600742 struct mtip_cmd *cmd = mtip_cmd_from_tag(dd, MTIP_TAG_INTERNAL);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600743
Jens Axboe994ff072017-05-02 09:53:04 -0600744 if (test_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags) && cmd) {
745 int group = MTIP_TAG_INDEX(MTIP_TAG_INTERNAL);
746 int status = readl(port->cmd_issue[group]);
747
Jens Axboe6f63503c2017-05-02 09:56:00 -0600748 if (!(status & (1 << MTIP_TAG_BIT(MTIP_TAG_INTERNAL))))
749 mtip_complete_command(cmd, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600750 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600751}
752
753/*
754 * Demux and handle errors
755 */
756static inline void mtip_process_errors(struct driver_data *dd, u32 port_stat)
757{
Sam Bradshaw88523a62011-08-30 08:34:26 -0600758 if (unlikely(port_stat & PORT_IRQ_CONNECT)) {
759 dev_warn(&dd->pdev->dev,
760 "Clearing PxSERR.DIAG.x\n");
761 writel((1 << 26), dd->port->mmio + PORT_SCR_ERR);
762 }
763
764 if (unlikely(port_stat & PORT_IRQ_PHYRDY)) {
765 dev_warn(&dd->pdev->dev,
766 "Clearing PxSERR.DIAG.n\n");
767 writel((1 << 16), dd->port->mmio + PORT_SCR_ERR);
768 }
769
770 if (unlikely(port_stat & ~PORT_IRQ_HANDLED)) {
771 dev_warn(&dd->pdev->dev,
772 "Port stat errors %x unhandled\n",
773 (port_stat & ~PORT_IRQ_HANDLED));
Asai Thambi S P9b204fb2014-05-20 10:48:56 -0700774 if (mtip_check_surprise_removal(dd->pdev))
775 return;
776 }
777 if (likely(port_stat & (PORT_IRQ_TF_ERR | PORT_IRQ_IF_ERR))) {
778 set_bit(MTIP_PF_EH_ACTIVE_BIT, &dd->port->flags);
779 wake_up_interruptible(&dd->port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600780 }
781}
782
783static inline irqreturn_t mtip_handle_irq(struct driver_data *data)
784{
785 struct driver_data *dd = (struct driver_data *) data;
786 struct mtip_port *port = dd->port;
787 u32 hba_stat, port_stat;
788 int rv = IRQ_NONE;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800789 int do_irq_enable = 1, i, workers;
790 struct mtip_work *twork;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600791
792 hba_stat = readl(dd->mmio + HOST_IRQ_STAT);
793 if (hba_stat) {
794 rv = IRQ_HANDLED;
795
796 /* Acknowledge the interrupt status on the port.*/
797 port_stat = readl(port->mmio + PORT_IRQ_STAT);
Asai Thambi SP2132a542015-05-11 15:53:18 -0700798 if (unlikely(port_stat == 0xFFFFFFFF)) {
799 mtip_check_surprise_removal(dd->pdev);
800 return IRQ_HANDLED;
801 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600802 writel(port_stat, port->mmio + PORT_IRQ_STAT);
803
804 /* Demux port status */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800805 if (likely(port_stat & PORT_IRQ_SDB_FIS)) {
806 do_irq_enable = 0;
807 WARN_ON_ONCE(atomic_read(&dd->irq_workers_active) != 0);
808
809 /* Start at 1: group zero is always local? */
810 for (i = 0, workers = 0; i < MTIP_MAX_SLOT_GROUPS;
811 i++) {
812 twork = &dd->work[i];
813 twork->completed = readl(port->completed[i]);
814 if (twork->completed)
815 workers++;
816 }
817
818 atomic_set(&dd->irq_workers_active, workers);
819 if (workers) {
820 for (i = 1; i < MTIP_MAX_SLOT_GROUPS; i++) {
821 twork = &dd->work[i];
822 if (twork->completed)
823 queue_work_on(
824 twork->cpu_binding,
825 dd->isr_workq,
826 &twork->work);
827 }
828
829 if (likely(dd->work[0].completed))
830 mtip_workq_sdbfx(port, 0,
831 dd->work[0].completed);
832
833 } else {
834 /*
835 * Chip quirk: SDB interrupt but nothing
836 * to complete
837 */
838 do_irq_enable = 1;
839 }
840 }
Sam Bradshaw88523a62011-08-30 08:34:26 -0600841
842 if (unlikely(port_stat & PORT_IRQ_ERR)) {
843 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
Sam Bradshaw88523a62011-08-30 08:34:26 -0600844 /* don't proceed further */
845 return IRQ_HANDLED;
846 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200847 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +0200848 &dd->dd_flag))
849 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600850
851 mtip_process_errors(dd, port_stat & PORT_IRQ_ERR);
852 }
853
854 if (unlikely(port_stat & PORT_IRQ_LEGACY))
855 mtip_process_legacy(dd, port_stat & PORT_IRQ_LEGACY);
856 }
857
858 /* acknowledge interrupt */
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800859 if (unlikely(do_irq_enable))
860 writel(hba_stat, dd->mmio + HOST_IRQ_STAT);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600861
862 return rv;
863}
864
865/*
Sam Bradshaw88523a62011-08-30 08:34:26 -0600866 * HBA interrupt subroutine.
867 *
868 * @irq IRQ number.
869 * @instance Pointer to the driver data structure.
870 *
871 * return value
872 * IRQ_HANDLED A HBA interrupt was pending and handled.
873 * IRQ_NONE This interrupt was not for the HBA.
874 */
875static irqreturn_t mtip_irq_handler(int irq, void *instance)
876{
877 struct driver_data *dd = instance;
Asai Thambi S P16c906e52012-12-20 07:46:25 -0800878
879 return mtip_handle_irq(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600880}
881
882static void mtip_issue_non_ncq_command(struct mtip_port *port, int tag)
883{
Jens Axboe6f63503c2017-05-02 09:56:00 -0600884 writel(1 << MTIP_TAG_BIT(tag), port->cmd_issue[MTIP_TAG_INDEX(tag)]);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600885}
886
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200887static bool mtip_pause_ncq(struct mtip_port *port,
888 struct host_to_dev_fis *fis)
889{
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200890 unsigned long task_file_data;
891
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200892 task_file_data = readl(port->mmio+PORT_TFDATA);
Asai Thambi S P12a166c2012-09-05 22:01:36 +0530893 if ((task_file_data & 1))
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200894 return false;
895
896 if (fis->command == ATA_CMD_SEC_ERASE_PREP) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200897 port->ic_pause_timer = jiffies;
898 return true;
899 } else if ((fis->command == ATA_CMD_DOWNLOAD_MICRO) &&
900 (fis->features == 0x03)) {
901 set_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
902 port->ic_pause_timer = jiffies;
903 return true;
904 } else if ((fis->command == ATA_CMD_SEC_ERASE_UNIT) ||
905 ((fis->command == 0xFC) &&
906 (fis->features == 0x27 || fis->features == 0x72 ||
907 fis->features == 0x62 || fis->features == 0x26))) {
Asai Thambi SPee04bed2015-05-11 15:50:50 -0700908 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
Asai Thambi SPaae4a032016-02-24 21:18:20 -0800909 clear_bit(MTIP_DDF_REBUILD_FAILED_BIT, &port->dd->dd_flag);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200910 /* Com reset after secure erase or lowlevel format */
911 mtip_restart_port(port);
Asai Thambi SP686d8e02015-05-11 15:51:27 -0700912 clear_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +0200913 return false;
914 }
915
916 return false;
917}
918
Jens Axboebaed5482017-04-28 07:54:06 -0600919static bool mtip_commands_active(struct mtip_port *port)
920{
921 unsigned int active;
922 unsigned int n;
923
924 /*
925 * Ignore s_active bit 0 of array element 0.
926 * This bit will always be set
927 */
928 active = readl(port->s_active[0]) & 0xFFFFFFFE;
929 for (n = 1; n < port->dd->slot_groups; n++)
930 active |= readl(port->s_active[n]);
931
932 return active != 0;
933}
934
Sam Bradshaw88523a62011-08-30 08:34:26 -0600935/*
936 * Wait for port to quiesce
937 *
938 * @port Pointer to port data structure
939 * @timeout Max duration to wait (ms)
940 *
941 * return value
942 * 0 Success
943 * -EBUSY Commands still active
944 */
Jens Axboe8afdd942017-04-27 16:32:41 -0600945static int mtip_quiesce_io(struct mtip_port *port, unsigned long timeout)
Sam Bradshaw88523a62011-08-30 08:34:26 -0600946{
947 unsigned long to;
Jens Axboebaed5482017-04-28 07:54:06 -0600948 bool active = true;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600949
Sagi Grimberg436c15a2017-07-04 10:00:41 +0300950 blk_mq_quiesce_queue(port->dd->queue);
Jens Axboe9acf03c2014-05-14 08:22:56 -0600951
Sam Bradshaw88523a62011-08-30 08:34:26 -0600952 to = jiffies + msecs_to_jiffies(timeout);
953 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +0200954 if (test_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags) &&
Jens Axboe8afdd942017-04-27 16:32:41 -0600955 test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100956 msleep(20);
957 continue; /* svc thd is actively issuing commands */
958 }
Asai Thambi S P9b204fb2014-05-20 10:48:56 -0700959
Jens Axboe8afdd942017-04-27 16:32:41 -0600960 msleep(100);
Asai Thambi SP008e56d2016-02-24 21:21:20 -0800961
Asai Thambi S P9b204fb2014-05-20 10:48:56 -0700962 if (mtip_check_surprise_removal(port->dd->pdev))
963 goto err_fault;
Asai Thambi S P9b204fb2014-05-20 10:48:56 -0700964
Jens Axboebaed5482017-04-28 07:54:06 -0600965 active = mtip_commands_active(port);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600966 if (!active)
967 break;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600968 } while (time_before(jiffies, to));
969
Sagi Grimberg436c15a2017-07-04 10:00:41 +0300970 blk_mq_unquiesce_queue(port->dd->queue);
Sam Bradshaw88523a62011-08-30 08:34:26 -0600971 return active ? -EBUSY : 0;
Jens Axboe9acf03c2014-05-14 08:22:56 -0600972err_fault:
Sagi Grimberg436c15a2017-07-04 10:00:41 +0300973 blk_mq_unquiesce_queue(port->dd->queue);
Jens Axboe9acf03c2014-05-14 08:22:56 -0600974 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -0600975}
976
Jens Axboe3f5e6a32017-04-28 10:45:08 -0600977struct mtip_int_cmd {
978 int fis_len;
979 dma_addr_t buffer;
980 int buf_len;
981 u32 opts;
982};
983
Sam Bradshaw88523a62011-08-30 08:34:26 -0600984/*
985 * Execute an internal command and wait for the completion.
986 *
987 * @port Pointer to the port data structure.
988 * @fis Pointer to the FIS that describes the command.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100989 * @fis_len Length in WORDS of the FIS.
Sam Bradshaw88523a62011-08-30 08:34:26 -0600990 * @buffer DMA accessible for command data.
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +0100991 * @buf_len Length, in bytes, of the data buffer.
Sam Bradshaw88523a62011-08-30 08:34:26 -0600992 * @opts Command header options, excluding the FIS length
993 * and the number of PRD entries.
994 * @timeout Time in ms to wait for the command to complete.
995 *
996 * return value
997 * 0 Command completed successfully.
998 * -EFAULT The buffer address is not correctly aligned.
999 * -EBUSY Internal command or other IO in progress.
1000 * -EAGAIN Time out waiting for command to complete.
1001 */
1002static int mtip_exec_internal_command(struct mtip_port *port,
Asai Thambi S P8182b492012-04-09 08:35:38 +02001003 struct host_to_dev_fis *fis,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001004 int fis_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001005 dma_addr_t buffer,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001006 int buf_len,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001007 u32 opts,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001008 unsigned long timeout)
1009{
Jens Axboeffc771b2014-05-09 09:42:02 -06001010 struct mtip_cmd *int_cmd;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301011 struct driver_data *dd = port->dd;
Jens Axboe3f5e6a32017-04-28 10:45:08 -06001012 struct request *rq;
1013 struct mtip_int_cmd icmd = {
1014 .fis_len = fis_len,
1015 .buffer = buffer,
1016 .buf_len = buf_len,
1017 .opts = opts
1018 };
Jens Axboeffc771b2014-05-09 09:42:02 -06001019 int rv = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001020
1021 /* Make sure the buffer is 8 byte aligned. This is asic specific. */
1022 if (buffer & 0x00000007) {
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301023 dev_err(&dd->pdev->dev, "SG buffer is not 8 byte aligned\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001024 return -EFAULT;
1025 }
1026
Jens Axboeffc771b2014-05-09 09:42:02 -06001027 int_cmd = mtip_get_int_command(dd);
Asai Thambi SP008e56d2016-02-24 21:21:20 -08001028 if (!int_cmd) {
1029 dbg_printk(MTIP_DRV_NAME "Unable to allocate tag for PIO cmd\n");
1030 return -EFAULT;
1031 }
Jens Axboe3f5e6a32017-04-28 10:45:08 -06001032 rq = blk_mq_rq_from_pdu(int_cmd);
Jens Axboe6f63503c2017-05-02 09:56:00 -06001033 rq->special = &icmd;
Jens Axboeffc771b2014-05-09 09:42:02 -06001034
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001035 set_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001036
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001037 if (fis->command == ATA_CMD_SEC_ERASE_PREP)
1038 set_bit(MTIP_PF_SE_ACTIVE_BIT, &port->flags);
1039
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301040 clear_bit(MTIP_PF_DM_ACTIVE_BIT, &port->flags);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001041
Jens Axboe0f6422a2017-04-27 16:29:26 -06001042 if (fis->command != ATA_CMD_STANDBYNOW1) {
1043 /* wait for io to complete if non atomic */
Jens Axboe8afdd942017-04-27 16:32:41 -06001044 if (mtip_quiesce_io(port, MTIP_QUIESCE_IO_TIMEOUT_MS) < 0) {
Jens Axboe0f6422a2017-04-27 16:29:26 -06001045 dev_warn(&dd->pdev->dev, "Failed to quiesce IO\n");
Jens Axboe6f63503c2017-05-02 09:56:00 -06001046 blk_mq_free_request(rq);
Jens Axboe0f6422a2017-04-27 16:29:26 -06001047 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
1048 wake_up_interruptible(&port->svc_wait);
1049 return -EBUSY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001050 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001051 }
1052
1053 /* Copy the command to the command table */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001054 memcpy(int_cmd->command, fis, fis_len*4);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001055
Jens Axboe3f5e6a32017-04-28 10:45:08 -06001056 rq->timeout = timeout;
Asai Thambi SP5b7e0a82016-02-24 21:16:38 -08001057
Jens Axboe3f5e6a32017-04-28 10:45:08 -06001058 /* insert request and run queue */
Jens Axboe6f63503c2017-05-02 09:56:00 -06001059 blk_execute_rq(rq->q, NULL, rq, true);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001060
Jens Axboe8c66ac6a2017-06-23 09:18:54 -06001061 if (int_cmd->status) {
1062 dev_err(&dd->pdev->dev, "Internal command [%02X] failed %d\n",
1063 fis->command, int_cmd->status);
1064 rv = -EIO;
Asai Thambi SPaae4a032016-02-24 21:18:20 -08001065
Jens Axboe0f6422a2017-04-27 16:29:26 -06001066 if (mtip_check_surprise_removal(dd->pdev) ||
1067 test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
1068 &dd->dd_flag)) {
1069 dev_err(&dd->pdev->dev,
1070 "Internal command [%02X] wait returned due to SR\n",
1071 fis->command);
1072 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301073 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001074 }
Jens Axboe0f6422a2017-04-27 16:29:26 -06001075 mtip_device_reset(dd); /* recover from timeout issue */
1076 rv = -EAGAIN;
1077 goto exec_ic_exit;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001078 }
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001079
Jens Axboe994ff072017-05-02 09:53:04 -06001080 if (readl(port->cmd_issue[MTIP_TAG_INDEX(MTIP_TAG_INTERNAL)])
1081 & (1 << MTIP_TAG_BIT(MTIP_TAG_INTERNAL))) {
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001082 rv = -ENXIO;
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301083 if (!test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
1084 mtip_device_reset(dd);
Asai Thambi S Pd02e1f02012-05-29 18:42:27 -07001085 rv = -EAGAIN;
1086 }
1087 }
Asai Thambi S P45038362012-04-09 08:35:38 +02001088exec_ic_exit:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001089 /* Clear the allocated and active bits for the internal command. */
Jens Axboe6f63503c2017-05-02 09:56:00 -06001090 blk_mq_free_request(rq);
Asai Thambi SP686d8e02015-05-11 15:51:27 -07001091 clear_bit(MTIP_PF_IC_ACTIVE_BIT, &port->flags);
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001092 if (rv >= 0 && mtip_pause_ncq(port, fis)) {
1093 /* NCQ paused */
1094 return rv;
1095 }
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001096 wake_up_interruptible(&port->svc_wait);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001097
1098 return rv;
1099}
1100
1101/*
1102 * Byte-swap ATA ID strings.
1103 *
1104 * ATA identify data contains strings in byte-swapped 16-bit words.
1105 * They must be swapped (on all architectures) to be usable as C strings.
1106 * This function swaps bytes in-place.
1107 *
1108 * @buf The buffer location of the string
1109 * @len The number of bytes to swap
1110 *
1111 * return value
1112 * None
1113 */
1114static inline void ata_swap_string(u16 *buf, unsigned int len)
1115{
1116 int i;
1117 for (i = 0; i < (len/2); i++)
1118 be16_to_cpus(&buf[i]);
1119}
1120
Asai Thambi S P670a6412014-04-16 20:27:54 -07001121static void mtip_set_timeout(struct driver_data *dd,
1122 struct host_to_dev_fis *fis,
1123 unsigned int *timeout, u8 erasemode)
1124{
1125 switch (fis->command) {
1126 case ATA_CMD_DOWNLOAD_MICRO:
1127 *timeout = 120000; /* 2 minutes */
1128 break;
1129 case ATA_CMD_SEC_ERASE_UNIT:
1130 case 0xFC:
1131 if (erasemode)
1132 *timeout = ((*(dd->port->identify + 90) * 2) * 60000);
1133 else
1134 *timeout = ((*(dd->port->identify + 89) * 2) * 60000);
1135 break;
1136 case ATA_CMD_STANDBYNOW1:
1137 *timeout = 120000; /* 2 minutes */
1138 break;
1139 case 0xF7:
1140 case 0xFA:
1141 *timeout = 60000; /* 60 seconds */
1142 break;
1143 case ATA_CMD_SMART:
1144 *timeout = 15000; /* 15 seconds */
1145 break;
1146 default:
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001147 *timeout = MTIP_IOCTL_CMD_TIMEOUT_MS;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001148 break;
1149 }
1150}
1151
Sam Bradshaw88523a62011-08-30 08:34:26 -06001152/*
1153 * Request the device identity information.
1154 *
1155 * If a user space buffer is not specified, i.e. is NULL, the
1156 * identify information is still read from the drive and placed
1157 * into the identify data buffer (@e port->identify) in the
1158 * port data structure.
1159 * When the identify buffer contains valid identify information @e
1160 * port->identify_valid is non-zero.
1161 *
1162 * @port Pointer to the port structure.
1163 * @user_buffer A user space buffer where the identify data should be
1164 * copied.
1165 *
1166 * return value
1167 * 0 Command completed successfully.
1168 * -EFAULT An error occurred while coping data to the user buffer.
1169 * -1 Command failed.
1170 */
1171static int mtip_get_identify(struct mtip_port *port, void __user *user_buffer)
1172{
1173 int rv = 0;
1174 struct host_to_dev_fis fis;
1175
Asai Thambi S P8a857a82012-04-09 08:35:38 +02001176 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &port->dd->dd_flag))
Asai Thambi S P45038362012-04-09 08:35:38 +02001177 return -EFAULT;
1178
Sam Bradshaw88523a62011-08-30 08:34:26 -06001179 /* Build the FIS. */
1180 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1181 fis.type = 0x27;
1182 fis.opts = 1 << 7;
1183 fis.command = ATA_CMD_ID_ATA;
1184
1185 /* Set the identify information as invalid. */
1186 port->identify_valid = 0;
1187
1188 /* Clear the identify information. */
1189 memset(port->identify, 0, sizeof(u16) * ATA_ID_WORDS);
1190
1191 /* Execute the command. */
1192 if (mtip_exec_internal_command(port,
1193 &fis,
1194 5,
1195 port->identify_dma,
1196 sizeof(u16) * ATA_ID_WORDS,
1197 0,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001198 MTIP_INT_CMD_TIMEOUT_MS)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001199 < 0) {
1200 rv = -1;
1201 goto out;
1202 }
1203
1204 /*
1205 * Perform any necessary byte-swapping. Yes, the kernel does in fact
1206 * perform field-sensitive swapping on the string fields.
1207 * See the kernel use of ata_id_string() for proof of this.
1208 */
1209#ifdef __LITTLE_ENDIAN
1210 ata_swap_string(port->identify + 27, 40); /* model string*/
1211 ata_swap_string(port->identify + 23, 8); /* firmware string*/
1212 ata_swap_string(port->identify + 10, 20); /* serial# string*/
1213#else
1214 {
1215 int i;
1216 for (i = 0; i < ATA_ID_WORDS; i++)
1217 port->identify[i] = le16_to_cpu(port->identify[i]);
1218 }
1219#endif
1220
Sam Bradshaw26d58052013-10-03 10:18:05 -07001221 /* Check security locked state */
1222 if (port->identify[128] & 0x4)
1223 set_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1224 else
1225 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &port->dd->dd_flag);
1226
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301227#ifdef MTIP_TRIM /* Disabling TRIM support temporarily */
Asai Thambi S P15283462013-01-11 14:41:34 +01001228 /* Demux ID.DRAT & ID.RZAT to determine trim support */
1229 if (port->identify[69] & (1 << 14) && port->identify[69] & (1 << 5))
1230 port->dd->trim_supp = true;
1231 else
Asai Thambi S P68466cb2013-04-12 23:58:02 +05301232#endif
Asai Thambi S P15283462013-01-11 14:41:34 +01001233 port->dd->trim_supp = false;
1234
Sam Bradshaw88523a62011-08-30 08:34:26 -06001235 /* Set the identify buffer as valid. */
1236 port->identify_valid = 1;
1237
1238 if (user_buffer) {
1239 if (copy_to_user(
1240 user_buffer,
1241 port->identify,
1242 ATA_ID_WORDS * sizeof(u16))) {
1243 rv = -EFAULT;
1244 goto out;
1245 }
1246 }
1247
1248out:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001249 return rv;
1250}
1251
1252/*
1253 * Issue a standby immediate command to the device.
1254 *
1255 * @port Pointer to the port structure.
1256 *
1257 * return value
1258 * 0 Command was executed successfully.
1259 * -1 An error occurred while executing the command.
1260 */
1261static int mtip_standby_immediate(struct mtip_port *port)
1262{
1263 int rv;
1264 struct host_to_dev_fis fis;
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001265 unsigned long start;
Asai Thambi S P670a6412014-04-16 20:27:54 -07001266 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001267
Sam Bradshaw88523a62011-08-30 08:34:26 -06001268 /* Build the FIS. */
1269 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1270 fis.type = 0x27;
1271 fis.opts = 1 << 7;
1272 fis.command = ATA_CMD_STANDBYNOW1;
1273
Asai Thambi S P670a6412014-04-16 20:27:54 -07001274 mtip_set_timeout(port->dd, &fis, &timeout, 0);
1275
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001276 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001277 rv = mtip_exec_internal_command(port,
1278 &fis,
1279 5,
1280 0,
1281 0,
1282 0,
Asai Thambi S P670a6412014-04-16 20:27:54 -07001283 timeout);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001284 dbg_printk(MTIP_DRV_NAME "Time taken to complete standby cmd: %d ms\n",
1285 jiffies_to_msecs(jiffies - start));
1286 if (rv)
1287 dev_warn(&port->dd->pdev->dev,
1288 "STANDBY IMMEDIATE command failed.\n");
1289
1290 return rv;
1291}
1292
1293/*
1294 * Issue a READ LOG EXT command to the device.
1295 *
1296 * @port pointer to the port structure.
1297 * @page page number to fetch
1298 * @buffer pointer to buffer
1299 * @buffer_dma dma address corresponding to @buffer
1300 * @sectors page length to fetch, in sectors
1301 *
1302 * return value
1303 * @rv return value from mtip_exec_internal_command()
1304 */
1305static int mtip_read_log_page(struct mtip_port *port, u8 page, u16 *buffer,
1306 dma_addr_t buffer_dma, unsigned int sectors)
1307{
1308 struct host_to_dev_fis fis;
1309
1310 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1311 fis.type = 0x27;
1312 fis.opts = 1 << 7;
1313 fis.command = ATA_CMD_READ_LOG_EXT;
1314 fis.sect_count = sectors & 0xFF;
1315 fis.sect_cnt_ex = (sectors >> 8) & 0xFF;
1316 fis.lba_low = page;
1317 fis.lba_mid = 0;
1318 fis.device = ATA_DEVICE_OBS;
1319
1320 memset(buffer, 0, sectors * ATA_SECT_SIZE);
1321
1322 return mtip_exec_internal_command(port,
1323 &fis,
1324 5,
1325 buffer_dma,
1326 sectors * ATA_SECT_SIZE,
1327 0,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001328 MTIP_INT_CMD_TIMEOUT_MS);
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001329}
1330
1331/*
1332 * Issue a SMART READ DATA command to the device.
1333 *
1334 * @port pointer to the port structure.
1335 * @buffer pointer to buffer
1336 * @buffer_dma dma address corresponding to @buffer
1337 *
1338 * return value
1339 * @rv return value from mtip_exec_internal_command()
1340 */
1341static int mtip_get_smart_data(struct mtip_port *port, u8 *buffer,
1342 dma_addr_t buffer_dma)
1343{
1344 struct host_to_dev_fis fis;
1345
1346 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1347 fis.type = 0x27;
1348 fis.opts = 1 << 7;
1349 fis.command = ATA_CMD_SMART;
1350 fis.features = 0xD0;
1351 fis.sect_count = 1;
1352 fis.lba_mid = 0x4F;
1353 fis.lba_hi = 0xC2;
1354 fis.device = ATA_DEVICE_OBS;
1355
1356 return mtip_exec_internal_command(port,
1357 &fis,
1358 5,
1359 buffer_dma,
1360 ATA_SECT_SIZE,
1361 0,
Asai Thambi S Pf6587212012-04-09 08:35:38 +02001362 15000);
1363}
1364
1365/*
1366 * Get the value of a smart attribute
1367 *
1368 * @port pointer to the port structure
1369 * @id attribute number
1370 * @attrib pointer to return attrib information corresponding to @id
1371 *
1372 * return value
1373 * -EINVAL NULL buffer passed or unsupported attribute @id.
1374 * -EPERM Identify data not valid, SMART not supported or not enabled
1375 */
1376static int mtip_get_smart_attr(struct mtip_port *port, unsigned int id,
1377 struct smart_attr *attrib)
1378{
1379 int rv, i;
1380 struct smart_attr *pattr;
1381
1382 if (!attrib)
1383 return -EINVAL;
1384
1385 if (!port->identify_valid) {
1386 dev_warn(&port->dd->pdev->dev, "IDENTIFY DATA not valid\n");
1387 return -EPERM;
1388 }
1389 if (!(port->identify[82] & 0x1)) {
1390 dev_warn(&port->dd->pdev->dev, "SMART not supported\n");
1391 return -EPERM;
1392 }
1393 if (!(port->identify[85] & 0x1)) {
1394 dev_warn(&port->dd->pdev->dev, "SMART not enabled\n");
1395 return -EPERM;
1396 }
1397
1398 memset(port->smart_buf, 0, ATA_SECT_SIZE);
1399 rv = mtip_get_smart_data(port, port->smart_buf, port->smart_buf_dma);
1400 if (rv) {
1401 dev_warn(&port->dd->pdev->dev, "Failed to ge SMART data\n");
1402 return rv;
1403 }
1404
1405 pattr = (struct smart_attr *)(port->smart_buf + 2);
1406 for (i = 0; i < 29; i++, pattr++)
1407 if (pattr->attr_id == id) {
1408 memcpy(attrib, pattr, sizeof(struct smart_attr));
1409 break;
1410 }
1411
1412 if (i == 29) {
1413 dev_warn(&port->dd->pdev->dev,
1414 "Query for invalid SMART attribute ID\n");
1415 rv = -EINVAL;
1416 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001417
Sam Bradshaw88523a62011-08-30 08:34:26 -06001418 return rv;
1419}
1420
1421/*
Asai Thambi S P15283462013-01-11 14:41:34 +01001422 * Trim unused sectors
1423 *
1424 * @dd pointer to driver_data structure
1425 * @lba starting lba
1426 * @len # of 512b sectors to trim
1427 *
1428 * return value
1429 * -ENOMEM Out of dma memory
1430 * -EINVAL Invalid parameters passed in, trim not supported
1431 * -EIO Error submitting trim request to hw
1432 */
Asai Thambi S Pd0d096b2013-04-03 19:53:07 +05301433static int mtip_send_trim(struct driver_data *dd, unsigned int lba,
1434 unsigned int len)
Asai Thambi S P15283462013-01-11 14:41:34 +01001435{
1436 int i, rv = 0;
1437 u64 tlba, tlen, sect_left;
1438 struct mtip_trim_entry *buf;
1439 dma_addr_t dma_addr;
1440 struct host_to_dev_fis fis;
1441
1442 if (!len || dd->trim_supp == false)
1443 return -EINVAL;
1444
1445 /* Trim request too big */
1446 WARN_ON(len > (MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES));
1447
1448 /* Trim request not aligned on 4k boundary */
1449 WARN_ON(len % 8 != 0);
1450
1451 /* Warn if vu_trim structure is too big */
1452 WARN_ON(sizeof(struct mtip_trim) > ATA_SECT_SIZE);
1453
1454 /* Allocate a DMA buffer for the trim structure */
1455 buf = dmam_alloc_coherent(&dd->pdev->dev, ATA_SECT_SIZE, &dma_addr,
1456 GFP_KERNEL);
1457 if (!buf)
1458 return -ENOMEM;
1459 memset(buf, 0, ATA_SECT_SIZE);
1460
1461 for (i = 0, sect_left = len, tlba = lba;
1462 i < MTIP_MAX_TRIM_ENTRIES && sect_left;
1463 i++) {
1464 tlen = (sect_left >= MTIP_MAX_TRIM_ENTRY_LEN ?
1465 MTIP_MAX_TRIM_ENTRY_LEN :
1466 sect_left);
1467 buf[i].lba = __force_bit2int cpu_to_le32(tlba);
1468 buf[i].range = __force_bit2int cpu_to_le16(tlen);
1469 tlba += tlen;
1470 sect_left -= tlen;
1471 }
1472 WARN_ON(sect_left != 0);
1473
1474 /* Build the fis */
1475 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1476 fis.type = 0x27;
1477 fis.opts = 1 << 7;
1478 fis.command = 0xfb;
1479 fis.features = 0x60;
1480 fis.sect_count = 1;
1481 fis.device = ATA_DEVICE_OBS;
1482
1483 if (mtip_exec_internal_command(dd->port,
1484 &fis,
1485 5,
1486 dma_addr,
1487 ATA_SECT_SIZE,
1488 0,
Asai Thambi S P15283462013-01-11 14:41:34 +01001489 MTIP_TRIM_TIMEOUT_MS) < 0)
1490 rv = -EIO;
1491
1492 dmam_free_coherent(&dd->pdev->dev, ATA_SECT_SIZE, buf, dma_addr);
1493 return rv;
1494}
1495
1496/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001497 * Get the drive capacity.
1498 *
1499 * @dd Pointer to the device data structure.
1500 * @sectors Pointer to the variable that will receive the sector count.
1501 *
1502 * return value
1503 * 1 Capacity was returned successfully.
1504 * 0 The identify information is invalid.
1505 */
Jens Axboe63166682011-09-27 21:27:43 -06001506static bool mtip_hw_get_capacity(struct driver_data *dd, sector_t *sectors)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001507{
1508 struct mtip_port *port = dd->port;
1509 u64 total, raw0, raw1, raw2, raw3;
1510 raw0 = port->identify[100];
1511 raw1 = port->identify[101];
1512 raw2 = port->identify[102];
1513 raw3 = port->identify[103];
1514 total = raw0 | raw1<<16 | raw2<<32 | raw3<<48;
1515 *sectors = total;
1516 return (bool) !!port->identify_valid;
1517}
1518
1519/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06001520 * Display the identify command data.
1521 *
1522 * @port Pointer to the port data structure.
1523 *
1524 * return value
1525 * None
1526 */
1527static void mtip_dump_identify(struct mtip_port *port)
1528{
1529 sector_t sectors;
1530 unsigned short revid;
1531 char cbuf[42];
1532
1533 if (!port->identify_valid)
1534 return;
1535
1536 strlcpy(cbuf, (char *)(port->identify+10), 21);
1537 dev_info(&port->dd->pdev->dev,
1538 "Serial No.: %s\n", cbuf);
1539
1540 strlcpy(cbuf, (char *)(port->identify+23), 9);
1541 dev_info(&port->dd->pdev->dev,
1542 "Firmware Ver.: %s\n", cbuf);
1543
1544 strlcpy(cbuf, (char *)(port->identify+27), 41);
1545 dev_info(&port->dd->pdev->dev, "Model: %s\n", cbuf);
1546
Sam Bradshaw26d58052013-10-03 10:18:05 -07001547 dev_info(&port->dd->pdev->dev, "Security: %04x %s\n",
1548 port->identify[128],
1549 port->identify[128] & 0x4 ? "(LOCKED)" : "");
1550
Sam Bradshaw88523a62011-08-30 08:34:26 -06001551 if (mtip_hw_get_capacity(port->dd, &sectors))
1552 dev_info(&port->dd->pdev->dev,
1553 "Capacity: %llu sectors (%llu MB)\n",
1554 (u64)sectors,
1555 ((u64)sectors) * ATA_SECT_SIZE >> 20);
1556
1557 pci_read_config_word(port->dd->pdev, PCI_REVISION_ID, &revid);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001558 switch (revid & 0xFF) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001559 case 0x1:
1560 strlcpy(cbuf, "A0", 3);
1561 break;
1562 case 0x3:
1563 strlcpy(cbuf, "A2", 3);
1564 break;
1565 default:
1566 strlcpy(cbuf, "?", 2);
1567 break;
1568 }
1569 dev_info(&port->dd->pdev->dev,
1570 "Card Type: %s\n", cbuf);
1571}
1572
1573/*
1574 * Map the commands scatter list into the command table.
1575 *
1576 * @command Pointer to the command.
1577 * @nents Number of scatter list entries.
1578 *
1579 * return value
1580 * None
1581 */
1582static inline void fill_command_sg(struct driver_data *dd,
1583 struct mtip_cmd *command,
1584 int nents)
1585{
1586 int n;
1587 unsigned int dma_len;
1588 struct mtip_cmd_sg *command_sg;
1589 struct scatterlist *sg = command->sg;
1590
1591 command_sg = command->command + AHCI_CMD_TBL_HDR_SZ;
1592
1593 for (n = 0; n < nents; n++) {
1594 dma_len = sg_dma_len(sg);
1595 if (dma_len > 0x400000)
1596 dev_err(&dd->pdev->dev,
1597 "DMA segment length truncated\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001598 command_sg->info = __force_bit2int
1599 cpu_to_le32((dma_len-1) & 0x3FFFFF);
1600 command_sg->dba = __force_bit2int
1601 cpu_to_le32(sg_dma_address(sg));
1602 command_sg->dba_upper = __force_bit2int
1603 cpu_to_le32((sg_dma_address(sg) >> 16) >> 16);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001604 command_sg++;
1605 sg++;
1606 }
1607}
1608
1609/*
1610 * @brief Execute a drive command.
1611 *
1612 * return value 0 The command completed successfully.
1613 * return value -1 An error occurred while executing the command.
1614 */
Jens Axboe63166682011-09-27 21:27:43 -06001615static int exec_drive_task(struct mtip_port *port, u8 *command)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001616{
1617 struct host_to_dev_fis fis;
1618 struct host_to_dev_fis *reply = (port->rxfis + RX_FIS_D2H_REG);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001619 unsigned int to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001620
Sam Bradshaw88523a62011-08-30 08:34:26 -06001621 /* Build the FIS. */
1622 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1623 fis.type = 0x27;
1624 fis.opts = 1 << 7;
1625 fis.command = command[0];
1626 fis.features = command[1];
1627 fis.sect_count = command[2];
1628 fis.sector = command[3];
1629 fis.cyl_low = command[4];
1630 fis.cyl_hi = command[5];
1631 fis.device = command[6] & ~0x10; /* Clear the dev bit*/
1632
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001633 mtip_set_timeout(port->dd, &fis, &to, 0);
1634
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001635 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 -06001636 __func__,
1637 command[0],
1638 command[1],
1639 command[2],
1640 command[3],
1641 command[4],
1642 command[5],
1643 command[6]);
1644
1645 /* Execute the command. */
1646 if (mtip_exec_internal_command(port,
1647 &fis,
1648 5,
1649 0,
1650 0,
1651 0,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001652 to) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001653 return -1;
1654 }
1655
1656 command[0] = reply->command; /* Status*/
1657 command[1] = reply->features; /* Error*/
1658 command[4] = reply->cyl_low;
1659 command[5] = reply->cyl_hi;
1660
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001661 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 -06001662 __func__,
1663 command[0],
1664 command[1],
1665 command[4],
1666 command[5]);
1667
Sam Bradshaw88523a62011-08-30 08:34:26 -06001668 return 0;
1669}
1670
1671/*
1672 * @brief Execute a drive command.
1673 *
1674 * @param port Pointer to the port data structure.
1675 * @param command Pointer to the user specified command parameters.
1676 * @param user_buffer Pointer to the user space buffer where read sector
1677 * data should be copied.
1678 *
1679 * return value 0 The command completed successfully.
1680 * return value -EFAULT An error occurred while copying the completion
1681 * data to the user space buffer.
1682 * return value -1 An error occurred while executing the command.
1683 */
Jens Axboe63166682011-09-27 21:27:43 -06001684static int exec_drive_command(struct mtip_port *port, u8 *command,
1685 void __user *user_buffer)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001686{
1687 struct host_to_dev_fis fis;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001688 struct host_to_dev_fis *reply;
1689 u8 *buf = NULL;
1690 dma_addr_t dma_addr = 0;
1691 int rv = 0, xfer_sz = command[3];
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001692 unsigned int to;
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001693
1694 if (xfer_sz) {
David Milburn97651ea2012-09-12 14:06:12 -05001695 if (!user_buffer)
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001696 return -EFAULT;
1697
1698 buf = dmam_alloc_coherent(&port->dd->pdev->dev,
1699 ATA_SECT_SIZE * xfer_sz,
1700 &dma_addr,
1701 GFP_KERNEL);
1702 if (!buf) {
1703 dev_err(&port->dd->pdev->dev,
1704 "Memory allocation failed (%d bytes)\n",
1705 ATA_SECT_SIZE * xfer_sz);
1706 return -ENOMEM;
1707 }
1708 memset(buf, 0, ATA_SECT_SIZE * xfer_sz);
1709 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06001710
Sam Bradshaw88523a62011-08-30 08:34:26 -06001711 /* Build the FIS. */
1712 memset(&fis, 0, sizeof(struct host_to_dev_fis));
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001713 fis.type = 0x27;
1714 fis.opts = 1 << 7;
1715 fis.command = command[0];
Sam Bradshaw88523a62011-08-30 08:34:26 -06001716 fis.features = command[2];
1717 fis.sect_count = command[3];
1718 if (fis.command == ATA_CMD_SMART) {
1719 fis.sector = command[1];
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001720 fis.cyl_low = 0x4F;
1721 fis.cyl_hi = 0xC2;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001722 }
1723
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001724 mtip_set_timeout(port->dd, &fis, &to, 0);
1725
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001726 if (xfer_sz)
1727 reply = (port->rxfis + RX_FIS_PIO_SETUP);
1728 else
1729 reply = (port->rxfis + RX_FIS_D2H_REG);
1730
Sam Bradshaw88523a62011-08-30 08:34:26 -06001731 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001732 " %s: User Command: cmd %x, sect %x, "
Sam Bradshaw88523a62011-08-30 08:34:26 -06001733 "feat %x, sectcnt %x\n",
1734 __func__,
1735 command[0],
1736 command[1],
1737 command[2],
1738 command[3]);
1739
Sam Bradshaw88523a62011-08-30 08:34:26 -06001740 /* Execute the command. */
1741 if (mtip_exec_internal_command(port,
1742 &fis,
1743 5,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001744 (xfer_sz ? dma_addr : 0),
1745 (xfer_sz ? ATA_SECT_SIZE * xfer_sz : 0),
Sam Bradshaw88523a62011-08-30 08:34:26 -06001746 0,
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07001747 to)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001748 < 0) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001749 rv = -EFAULT;
1750 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001751 }
1752
1753 /* Collect the completion status. */
1754 command[0] = reply->command; /* Status*/
1755 command[1] = reply->features; /* Error*/
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001756 command[2] = reply->sect_count;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001757
1758 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001759 " %s: Completion Status: stat %x, "
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001760 "err %x, nsect %x\n",
Sam Bradshaw88523a62011-08-30 08:34:26 -06001761 __func__,
1762 command[0],
1763 command[1],
1764 command[2]);
1765
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001766 if (xfer_sz) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001767 if (copy_to_user(user_buffer,
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001768 buf,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001769 ATA_SECT_SIZE * command[3])) {
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001770 rv = -EFAULT;
1771 goto exit_drive_command;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001772 }
1773 }
Asai Thambi S Pe602878f2012-05-29 18:43:31 -07001774exit_drive_command:
1775 if (buf)
1776 dmam_free_coherent(&port->dd->pdev->dev,
1777 ATA_SECT_SIZE * xfer_sz, buf, dma_addr);
1778 return rv;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001779}
1780
1781/*
1782 * Indicates whether a command has a single sector payload.
1783 *
1784 * @command passed to the device to perform the certain event.
1785 * @features passed to the device to perform the certain event.
1786 *
1787 * return value
1788 * 1 command is one that always has a single sector payload,
1789 * regardless of the value in the Sector Count field.
1790 * 0 otherwise
1791 *
1792 */
1793static unsigned int implicit_sector(unsigned char command,
1794 unsigned char features)
1795{
1796 unsigned int rv = 0;
1797
1798 /* list of commands that have an implicit sector count of 1 */
1799 switch (command) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001800 case ATA_CMD_SEC_SET_PASS:
1801 case ATA_CMD_SEC_UNLOCK:
1802 case ATA_CMD_SEC_ERASE_PREP:
1803 case ATA_CMD_SEC_ERASE_UNIT:
1804 case ATA_CMD_SEC_FREEZE_LOCK:
1805 case ATA_CMD_SEC_DISABLE_PASS:
1806 case ATA_CMD_PMP_READ:
1807 case ATA_CMD_PMP_WRITE:
Sam Bradshaw88523a62011-08-30 08:34:26 -06001808 rv = 1;
1809 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001810 case ATA_CMD_SET_MAX:
1811 if (features == ATA_SET_MAX_UNLOCK)
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_SMART:
1815 if ((features == ATA_SMART_READ_VALUES) ||
1816 (features == ATA_SMART_READ_THRESHOLDS))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001817 rv = 1;
1818 break;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01001819 case ATA_CMD_CONF_OVERLAY:
1820 if ((features == ATA_DCO_IDENTIFY) ||
1821 (features == ATA_DCO_SET))
Sam Bradshaw88523a62011-08-30 08:34:26 -06001822 rv = 1;
1823 break;
1824 }
1825 return rv;
1826}
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07001827
Sam Bradshaw88523a62011-08-30 08:34:26 -06001828/*
1829 * Executes a taskfile
1830 * See ide_taskfile_ioctl() for derivation
1831 */
1832static int exec_drive_taskfile(struct driver_data *dd,
Jens Axboeef0f1582011-09-27 21:19:53 -06001833 void __user *buf,
1834 ide_task_request_t *req_task,
1835 int outtotal)
Sam Bradshaw88523a62011-08-30 08:34:26 -06001836{
1837 struct host_to_dev_fis fis;
1838 struct host_to_dev_fis *reply;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001839 u8 *outbuf = NULL;
1840 u8 *inbuf = NULL;
Jens Axboe16d02c02011-09-27 15:50:01 -06001841 dma_addr_t outbuf_dma = 0;
1842 dma_addr_t inbuf_dma = 0;
1843 dma_addr_t dma_buffer = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001844 int err = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001845 unsigned int taskin = 0;
1846 unsigned int taskout = 0;
1847 u8 nsect = 0;
Asai Thambi S P2df7aa92012-05-29 18:41:23 -07001848 unsigned int timeout;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001849 unsigned int force_single_sector;
1850 unsigned int transfer_size;
1851 unsigned long task_file_data;
Jens Axboeef0f1582011-09-27 21:19:53 -06001852 int intotal = outtotal + req_task->out_size;
Selvan Mani4453bc82012-09-27 14:36:43 +02001853 int erasemode = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001854
1855 taskout = req_task->out_size;
1856 taskin = req_task->in_size;
1857 /* 130560 = 512 * 0xFF*/
Sachin Shuklab425b022016-11-11 14:34:51 +05301858 if (taskin > 130560 || taskout > 130560)
1859 return -EINVAL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001860
1861 if (taskout) {
Al Viro8ed60102016-01-02 14:56:33 -05001862 outbuf = memdup_user(buf + outtotal, taskout);
Sachin Shuklab425b022016-11-11 14:34:51 +05301863 if (IS_ERR(outbuf))
1864 return PTR_ERR(outbuf);
1865
Sam Bradshaw88523a62011-08-30 08:34:26 -06001866 outbuf_dma = pci_map_single(dd->pdev,
1867 outbuf,
1868 taskout,
1869 DMA_TO_DEVICE);
Alexey Khoroshilov5173cb82016-03-19 01:35:54 +03001870 if (pci_dma_mapping_error(dd->pdev, outbuf_dma)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001871 err = -ENOMEM;
1872 goto abort;
1873 }
1874 dma_buffer = outbuf_dma;
1875 }
1876
1877 if (taskin) {
Al Viro8ed60102016-01-02 14:56:33 -05001878 inbuf = memdup_user(buf + intotal, taskin);
1879 if (IS_ERR(inbuf)) {
1880 err = PTR_ERR(inbuf);
1881 inbuf = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06001882 goto abort;
1883 }
1884 inbuf_dma = pci_map_single(dd->pdev,
1885 inbuf,
1886 taskin, DMA_FROM_DEVICE);
Alexey Khoroshilov5173cb82016-03-19 01:35:54 +03001887 if (pci_dma_mapping_error(dd->pdev, inbuf_dma)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001888 err = -ENOMEM;
1889 goto abort;
1890 }
1891 dma_buffer = inbuf_dma;
1892 }
1893
1894 /* only supports PIO and non-data commands from this ioctl. */
1895 switch (req_task->data_phase) {
1896 case TASKFILE_OUT:
1897 nsect = taskout / ATA_SECT_SIZE;
1898 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1899 break;
1900 case TASKFILE_IN:
1901 reply = (dd->port->rxfis + RX_FIS_PIO_SETUP);
1902 break;
1903 case TASKFILE_NO_DATA:
1904 reply = (dd->port->rxfis + RX_FIS_D2H_REG);
1905 break;
1906 default:
1907 err = -EINVAL;
1908 goto abort;
1909 }
1910
Sam Bradshaw88523a62011-08-30 08:34:26 -06001911 /* Build the FIS. */
1912 memset(&fis, 0, sizeof(struct host_to_dev_fis));
1913
1914 fis.type = 0x27;
1915 fis.opts = 1 << 7;
1916 fis.command = req_task->io_ports[7];
1917 fis.features = req_task->io_ports[1];
1918 fis.sect_count = req_task->io_ports[2];
1919 fis.lba_low = req_task->io_ports[3];
1920 fis.lba_mid = req_task->io_ports[4];
1921 fis.lba_hi = req_task->io_ports[5];
1922 /* Clear the dev bit*/
1923 fis.device = req_task->io_ports[6] & ~0x10;
1924
1925 if ((req_task->in_flags.all == 0) && (req_task->out_flags.all & 1)) {
1926 req_task->in_flags.all =
1927 IDE_TASKFILE_STD_IN_FLAGS |
1928 (IDE_HOB_STD_IN_FLAGS << 8);
1929 fis.lba_low_ex = req_task->hob_ports[3];
1930 fis.lba_mid_ex = req_task->hob_ports[4];
1931 fis.lba_hi_ex = req_task->hob_ports[5];
1932 fis.features_ex = req_task->hob_ports[1];
1933 fis.sect_cnt_ex = req_task->hob_ports[2];
1934
1935 } else {
1936 req_task->in_flags.all = IDE_TASKFILE_STD_IN_FLAGS;
1937 }
1938
1939 force_single_sector = implicit_sector(fis.command, fis.features);
1940
1941 if ((taskin || taskout) && (!fis.sect_count)) {
1942 if (nsect)
1943 fis.sect_count = nsect;
1944 else {
1945 if (!force_single_sector) {
1946 dev_warn(&dd->pdev->dev,
1947 "data movement but "
1948 "sect_count is 0\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06001949 err = -EINVAL;
1950 goto abort;
1951 }
1952 }
1953 }
1954
1955 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001956 " %s: cmd %x, feat %x, nsect %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06001957 " sect/lbal %x, lcyl/lbam %x, hcyl/lbah %x,"
1958 " head/dev %x\n",
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02001959 __func__,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001960 fis.command,
1961 fis.features,
1962 fis.sect_count,
1963 fis.lba_low,
1964 fis.lba_mid,
1965 fis.lba_hi,
1966 fis.device);
1967
Selvan Mani4453bc82012-09-27 14:36:43 +02001968 /* check for erase mode support during secure erase.*/
Selvan Mani32087952012-11-07 06:03:37 -07001969 if ((fis.command == ATA_CMD_SEC_ERASE_UNIT) && outbuf &&
1970 (outbuf[0] & MTIP_SEC_ERASE_MODE)) {
Selvan Mani4453bc82012-09-27 14:36:43 +02001971 erasemode = 1;
1972 }
1973
1974 mtip_set_timeout(dd, &fis, &timeout, erasemode);
Sam Bradshaw88523a62011-08-30 08:34:26 -06001975
1976 /* Determine the correct transfer size.*/
1977 if (force_single_sector)
1978 transfer_size = ATA_SECT_SIZE;
1979 else
1980 transfer_size = ATA_SECT_SIZE * fis.sect_count;
1981
1982 /* Execute the command.*/
1983 if (mtip_exec_internal_command(dd->port,
1984 &fis,
1985 5,
1986 dma_buffer,
1987 transfer_size,
1988 0,
Sam Bradshaw88523a62011-08-30 08:34:26 -06001989 timeout) < 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06001990 err = -EIO;
1991 goto abort;
1992 }
1993
1994 task_file_data = readl(dd->port->mmio+PORT_TFDATA);
1995
1996 if ((req_task->data_phase == TASKFILE_IN) && !(task_file_data & 1)) {
1997 reply = dd->port->rxfis + RX_FIS_PIO_SETUP;
1998 req_task->io_ports[7] = reply->control;
1999 } else {
2000 reply = dd->port->rxfis + RX_FIS_D2H_REG;
2001 req_task->io_ports[7] = reply->command;
2002 }
2003
2004 /* reclaim the DMA buffers.*/
2005 if (inbuf_dma)
2006 pci_unmap_single(dd->pdev, inbuf_dma,
2007 taskin, DMA_FROM_DEVICE);
2008 if (outbuf_dma)
2009 pci_unmap_single(dd->pdev, outbuf_dma,
2010 taskout, DMA_TO_DEVICE);
Jens Axboe16d02c02011-09-27 15:50:01 -06002011 inbuf_dma = 0;
2012 outbuf_dma = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002013
2014 /* return the ATA registers to the caller.*/
2015 req_task->io_ports[1] = reply->features;
2016 req_task->io_ports[2] = reply->sect_count;
2017 req_task->io_ports[3] = reply->lba_low;
2018 req_task->io_ports[4] = reply->lba_mid;
2019 req_task->io_ports[5] = reply->lba_hi;
2020 req_task->io_ports[6] = reply->device;
2021
2022 if (req_task->out_flags.all & 1) {
2023
2024 req_task->hob_ports[3] = reply->lba_low_ex;
2025 req_task->hob_ports[4] = reply->lba_mid_ex;
2026 req_task->hob_ports[5] = reply->lba_hi_ex;
2027 req_task->hob_ports[1] = reply->features_ex;
2028 req_task->hob_ports[2] = reply->sect_cnt_ex;
2029 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002030 dbg_printk(MTIP_DRV_NAME
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002031 " %s: Completion: stat %x,"
Sam Bradshaw88523a62011-08-30 08:34:26 -06002032 "err %x, sect_cnt %x, lbalo %x,"
2033 "lbamid %x, lbahi %x, dev %x\n",
2034 __func__,
2035 req_task->io_ports[7],
2036 req_task->io_ports[1],
2037 req_task->io_ports[2],
2038 req_task->io_ports[3],
2039 req_task->io_ports[4],
2040 req_task->io_ports[5],
2041 req_task->io_ports[6]);
2042
Sam Bradshaw88523a62011-08-30 08:34:26 -06002043 if (taskout) {
2044 if (copy_to_user(buf + outtotal, outbuf, taskout)) {
2045 err = -EFAULT;
2046 goto abort;
2047 }
2048 }
2049 if (taskin) {
2050 if (copy_to_user(buf + intotal, inbuf, taskin)) {
2051 err = -EFAULT;
2052 goto abort;
2053 }
2054 }
2055abort:
2056 if (inbuf_dma)
2057 pci_unmap_single(dd->pdev, inbuf_dma,
2058 taskin, DMA_FROM_DEVICE);
2059 if (outbuf_dma)
2060 pci_unmap_single(dd->pdev, outbuf_dma,
2061 taskout, DMA_TO_DEVICE);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002062 kfree(outbuf);
2063 kfree(inbuf);
2064
2065 return err;
2066}
2067
2068/*
2069 * Handle IOCTL calls from the Block Layer.
2070 *
2071 * This function is called by the Block Layer when it receives an IOCTL
2072 * command that it does not understand. If the IOCTL command is not supported
2073 * this function returns -ENOTTY.
2074 *
2075 * @dd Pointer to the driver data structure.
2076 * @cmd IOCTL command passed from the Block Layer.
2077 * @arg IOCTL argument passed from the Block Layer.
2078 *
2079 * return value
2080 * 0 The IOCTL completed successfully.
2081 * -ENOTTY The specified command is not supported.
2082 * -EFAULT An error occurred copying data to a user space buffer.
2083 * -EIO An error occurred while executing the command.
2084 */
Jens Axboeef0f1582011-09-27 21:19:53 -06002085static int mtip_hw_ioctl(struct driver_data *dd, unsigned int cmd,
2086 unsigned long arg)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002087{
2088 switch (cmd) {
2089 case HDIO_GET_IDENTITY:
Asai Thambi S P971890f2012-05-29 18:41:47 -07002090 {
2091 if (copy_to_user((void __user *)arg, dd->port->identify,
2092 sizeof(u16) * ATA_ID_WORDS))
2093 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002094 break;
Asai Thambi S P971890f2012-05-29 18:41:47 -07002095 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002096 case HDIO_DRIVE_CMD:
2097 {
2098 u8 drive_command[4];
2099
2100 /* Copy the user command info to our buffer. */
2101 if (copy_from_user(drive_command,
2102 (void __user *) arg,
2103 sizeof(drive_command)))
2104 return -EFAULT;
2105
2106 /* Execute the drive command. */
2107 if (exec_drive_command(dd->port,
2108 drive_command,
2109 (void __user *) (arg+4)))
2110 return -EIO;
2111
2112 /* Copy the status back to the users buffer. */
2113 if (copy_to_user((void __user *) arg,
2114 drive_command,
2115 sizeof(drive_command)))
2116 return -EFAULT;
2117
2118 break;
2119 }
2120 case HDIO_DRIVE_TASK:
2121 {
2122 u8 drive_command[7];
2123
2124 /* Copy the user command info to our buffer. */
2125 if (copy_from_user(drive_command,
2126 (void __user *) arg,
2127 sizeof(drive_command)))
2128 return -EFAULT;
2129
2130 /* Execute the drive command. */
2131 if (exec_drive_task(dd->port, drive_command))
2132 return -EIO;
2133
2134 /* Copy the status back to the users buffer. */
2135 if (copy_to_user((void __user *) arg,
2136 drive_command,
2137 sizeof(drive_command)))
2138 return -EFAULT;
2139
2140 break;
2141 }
Jens Axboeef0f1582011-09-27 21:19:53 -06002142 case HDIO_DRIVE_TASKFILE: {
2143 ide_task_request_t req_task;
2144 int ret, outtotal;
2145
2146 if (copy_from_user(&req_task, (void __user *) arg,
2147 sizeof(req_task)))
2148 return -EFAULT;
2149
2150 outtotal = sizeof(req_task);
2151
2152 ret = exec_drive_taskfile(dd, (void __user *) arg,
2153 &req_task, outtotal);
2154
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002155 if (copy_to_user((void __user *) arg, &req_task,
2156 sizeof(req_task)))
Jens Axboeef0f1582011-09-27 21:19:53 -06002157 return -EFAULT;
2158
2159 return ret;
2160 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002161
2162 default:
2163 return -EINVAL;
2164 }
2165 return 0;
2166}
2167
2168/*
2169 * Submit an IO to the hw
2170 *
2171 * This function is called by the block layer to issue an io
2172 * to the device. Upon completion, the callback function will
2173 * be called with the data parameter passed as the callback data.
2174 *
2175 * @dd Pointer to the driver data structure.
2176 * @start First sector to read.
2177 * @nsect Number of sectors to read.
2178 * @nents Number of entries in scatter list for the read command.
2179 * @tag The tag of this read command.
2180 * @callback Pointer to the function that should be called
2181 * when the read completes.
2182 * @data Callback data passed to the callback function
2183 * when the read completes.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002184 * @dir Direction (read or write)
2185 *
2186 * return value
2187 * None
2188 */
Jens Axboeffc771b2014-05-09 09:42:02 -06002189static void mtip_hw_submit_io(struct driver_data *dd, struct request *rq,
2190 struct mtip_cmd *command, int nents,
2191 struct blk_mq_hw_ctx *hctx)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002192{
2193 struct host_to_dev_fis *fis;
2194 struct mtip_port *port = dd->port;
Jens Axboeffc771b2014-05-09 09:42:02 -06002195 int dma_dir = rq_data_dir(rq) == READ ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
2196 u64 start = blk_rq_pos(rq);
2197 unsigned int nsect = blk_rq_sectors(rq);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002198
2199 /* Map the scatter list for DMA access */
Asai Thambi S P45038362012-04-09 08:35:38 +02002200 nents = dma_map_sg(&dd->pdev->dev, command->sg, nents, dma_dir);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002201
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002202 prefetch(&port->flags);
2203
Sam Bradshaw88523a62011-08-30 08:34:26 -06002204 command->scatter_ents = nents;
2205
2206 /*
2207 * The number of retries for this command before it is
2208 * reported as a failure to the upper layers.
2209 */
2210 command->retries = MTIP_MAX_RETRIES;
2211
2212 /* Fill out fis */
2213 fis = command->command;
2214 fis->type = 0x27;
2215 fis->opts = 1 << 7;
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002216 if (dma_dir == DMA_FROM_DEVICE)
Jens Axboeffc771b2014-05-09 09:42:02 -06002217 fis->command = ATA_CMD_FPDMA_READ;
2218 else
2219 fis->command = ATA_CMD_FPDMA_WRITE;
Selvan Manieda45312012-11-07 06:03:53 -07002220 fis->lba_low = start & 0xFF;
2221 fis->lba_mid = (start >> 8) & 0xFF;
2222 fis->lba_hi = (start >> 16) & 0xFF;
2223 fis->lba_low_ex = (start >> 24) & 0xFF;
2224 fis->lba_mid_ex = (start >> 32) & 0xFF;
2225 fis->lba_hi_ex = (start >> 40) & 0xFF;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002226 fis->device = 1 << 6;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002227 fis->features = nsect & 0xFF;
2228 fis->features_ex = (nsect >> 8) & 0xFF;
Jens Axboeffc771b2014-05-09 09:42:02 -06002229 fis->sect_count = ((rq->tag << 3) | (rq->tag >> 5));
Sam Bradshaw88523a62011-08-30 08:34:26 -06002230 fis->sect_cnt_ex = 0;
2231 fis->control = 0;
2232 fis->res2 = 0;
2233 fis->res3 = 0;
2234 fill_command_sg(dd, command, nents);
2235
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002236 if (unlikely(command->unaligned))
Asai Thambi S P2077d942013-04-29 21:19:49 +02002237 fis->device |= 1 << 7;
2238
Sam Bradshaw88523a62011-08-30 08:34:26 -06002239 /* Populate the command header */
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002240 command->command_header->opts =
2241 __force_bit2int cpu_to_le32(
2242 (nents << 16) | 5 | AHCI_CMD_PREFETCH);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002243 command->command_header->byte_count = 0;
2244
Asai Thambi S P45038362012-04-09 08:35:38 +02002245 command->direction = dma_dir;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002246
2247 /*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002248 * To prevent this command from being issued
2249 * if an internal command is in progress or error handling is active.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002250 */
Sam Bradshawf45c40a2014-06-06 13:28:48 -06002251 if (unlikely(port->flags & MTIP_PF_PAUSE_IO)) {
Jens Axboeffc771b2014-05-09 09:42:02 -06002252 set_bit(rq->tag, port->cmds_to_issue);
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002253 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002254 return;
2255 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002256
2257 /* Issue the command to the hardware */
Jens Axboeffc771b2014-05-09 09:42:02 -06002258 mtip_issue_ncq_command(port, rq->tag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002259}
2260
2261/*
Asai Thambi S P7412ff12012-06-04 12:43:03 -07002262 * Sysfs status dump.
Sam Bradshaw88523a62011-08-30 08:34:26 -06002263 *
2264 * @dev Pointer to the device structure, passed by the kernrel.
2265 * @attr Pointer to the device_attribute structure passed by the kernel.
2266 * @buf Pointer to the char buffer that will receive the stats info.
2267 *
2268 * return value
2269 * The size, in bytes, of the data copied into buf.
2270 */
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002271static ssize_t mtip_hw_show_status(struct device *dev,
2272 struct device_attribute *attr,
2273 char *buf)
2274{
2275 struct driver_data *dd = dev_to_disk(dev)->private_data;
2276 int size = 0;
2277
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002278 if (test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002279 size += sprintf(buf, "%s", "thermal_shutdown\n");
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002280 else if (test_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag))
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002281 size += sprintf(buf, "%s", "write_protect\n");
2282 else
2283 size += sprintf(buf, "%s", "online\n");
2284
2285 return size;
2286}
2287
Joe Perches5657a812018-05-24 13:38:59 -06002288static DEVICE_ATTR(status, 0444, mtip_hw_show_status, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002289
Asai Thambi S P0caff002013-04-03 19:56:21 +05302290/* debugsfs entries */
2291
2292static ssize_t show_device_status(struct device_driver *drv, char *buf)
2293{
2294 int size = 0;
2295 struct driver_data *dd, *tmp;
2296 unsigned long flags;
2297 char id_buf[42];
2298 u16 status = 0;
2299
2300 spin_lock_irqsave(&dev_lock, flags);
2301 size += sprintf(&buf[size], "Devices Present:\n");
2302 list_for_each_entry_safe(dd, tmp, &online_list, online_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002303 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302304 if (dd->port &&
2305 dd->port->identify &&
2306 dd->port->identify_valid) {
2307 strlcpy(id_buf,
2308 (char *) (dd->port->identify + 10), 21);
2309 status = *(dd->port->identify + 141);
2310 } else {
2311 memset(id_buf, 0, 42);
2312 status = 0;
2313 }
2314
2315 if (dd->port &&
2316 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2317 size += sprintf(&buf[size],
2318 " device %s %s (ftl rebuild %d %%)\n",
2319 dev_name(&dd->pdev->dev),
2320 id_buf,
2321 status);
2322 } else {
2323 size += sprintf(&buf[size],
2324 " device %s %s\n",
2325 dev_name(&dd->pdev->dev),
2326 id_buf);
2327 }
2328 }
2329 }
2330
2331 size += sprintf(&buf[size], "Devices Being Removed:\n");
2332 list_for_each_entry_safe(dd, tmp, &removing_list, remove_list) {
Jens Axboec66bb3f2013-04-04 09:03:41 +02002333 if (dd->pdev) {
Asai Thambi S P0caff002013-04-03 19:56:21 +05302334 if (dd->port &&
2335 dd->port->identify &&
2336 dd->port->identify_valid) {
2337 strlcpy(id_buf,
2338 (char *) (dd->port->identify+10), 21);
2339 status = *(dd->port->identify + 141);
2340 } else {
2341 memset(id_buf, 0, 42);
2342 status = 0;
2343 }
2344
2345 if (dd->port &&
2346 test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags)) {
2347 size += sprintf(&buf[size],
2348 " device %s %s (ftl rebuild %d %%)\n",
2349 dev_name(&dd->pdev->dev),
2350 id_buf,
2351 status);
2352 } else {
2353 size += sprintf(&buf[size],
2354 " device %s %s\n",
2355 dev_name(&dd->pdev->dev),
2356 id_buf);
2357 }
2358 }
2359 }
2360 spin_unlock_irqrestore(&dev_lock, flags);
2361
2362 return size;
2363}
2364
2365static ssize_t mtip_hw_read_device_status(struct file *f, char __user *ubuf,
2366 size_t len, loff_t *offset)
2367{
David Milburnc8afd0d2013-05-23 16:23:45 -05002368 struct driver_data *dd = (struct driver_data *)f->private_data;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302369 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002370 char *buf;
2371 int rv = 0;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302372
2373 if (!len || *offset)
2374 return 0;
2375
David Milburnc8afd0d2013-05-23 16:23:45 -05002376 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2377 if (!buf) {
2378 dev_err(&dd->pdev->dev,
2379 "Memory allocation: status buffer\n");
2380 return -ENOMEM;
2381 }
2382
Asai Thambi S P0caff002013-04-03 19:56:21 +05302383 size += show_device_status(NULL, buf);
2384
2385 *offset = size <= len ? size : len;
2386 size = copy_to_user(ubuf, buf, *offset);
2387 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002388 rv = -EFAULT;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302389
David Milburnc8afd0d2013-05-23 16:23:45 -05002390 kfree(buf);
2391 return rv ? rv : *offset;
Asai Thambi S P0caff002013-04-03 19:56:21 +05302392}
2393
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002394static ssize_t mtip_hw_read_registers(struct file *f, char __user *ubuf,
2395 size_t len, loff_t *offset)
2396{
2397 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002398 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002399 u32 group_allocated;
2400 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002401 int n, rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002402
2403 if (!len || size)
2404 return 0;
2405
David Milburnc8afd0d2013-05-23 16:23:45 -05002406 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2407 if (!buf) {
2408 dev_err(&dd->pdev->dev,
2409 "Memory allocation: register buffer\n");
2410 return -ENOMEM;
2411 }
2412
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002413 size += sprintf(&buf[size], "H/ S ACTive : [ 0x");
2414
2415 for (n = dd->slot_groups-1; n >= 0; n--)
2416 size += sprintf(&buf[size], "%08X ",
2417 readl(dd->port->s_active[n]));
2418
2419 size += sprintf(&buf[size], "]\n");
2420 size += sprintf(&buf[size], "H/ Command Issue : [ 0x");
2421
2422 for (n = dd->slot_groups-1; n >= 0; n--)
2423 size += sprintf(&buf[size], "%08X ",
2424 readl(dd->port->cmd_issue[n]));
2425
2426 size += sprintf(&buf[size], "]\n");
2427 size += sprintf(&buf[size], "H/ Completed : [ 0x");
2428
2429 for (n = dd->slot_groups-1; n >= 0; n--)
2430 size += sprintf(&buf[size], "%08X ",
2431 readl(dd->port->completed[n]));
2432
2433 size += sprintf(&buf[size], "]\n");
2434 size += sprintf(&buf[size], "H/ PORT IRQ STAT : [ 0x%08X ]\n",
2435 readl(dd->port->mmio + PORT_IRQ_STAT));
2436 size += sprintf(&buf[size], "H/ HOST IRQ STAT : [ 0x%08X ]\n",
2437 readl(dd->mmio + HOST_IRQ_STAT));
2438 size += sprintf(&buf[size], "\n");
2439
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002440 size += sprintf(&buf[size], "L/ Commands in Q : [ 0x");
2441
2442 for (n = dd->slot_groups-1; n >= 0; n--) {
2443 if (sizeof(long) > sizeof(u32))
2444 group_allocated =
2445 dd->port->cmds_to_issue[n/2] >> (32*(n&1));
2446 else
2447 group_allocated = dd->port->cmds_to_issue[n];
2448 size += sprintf(&buf[size], "%08X ", group_allocated);
2449 }
2450 size += sprintf(&buf[size], "]\n");
2451
2452 *offset = size <= len ? size : len;
2453 size = copy_to_user(ubuf, buf, *offset);
2454 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002455 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002456
David Milburnc8afd0d2013-05-23 16:23:45 -05002457 kfree(buf);
2458 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002459}
2460
2461static ssize_t mtip_hw_read_flags(struct file *f, char __user *ubuf,
2462 size_t len, loff_t *offset)
2463{
2464 struct driver_data *dd = (struct driver_data *)f->private_data;
David Milburnc8afd0d2013-05-23 16:23:45 -05002465 char *buf;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002466 int size = *offset;
David Milburnc8afd0d2013-05-23 16:23:45 -05002467 int rv = 0;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002468
2469 if (!len || size)
2470 return 0;
2471
David Milburnc8afd0d2013-05-23 16:23:45 -05002472 buf = kzalloc(MTIP_DFS_MAX_BUF_SIZE, GFP_KERNEL);
2473 if (!buf) {
2474 dev_err(&dd->pdev->dev,
2475 "Memory allocation: flag buffer\n");
2476 return -ENOMEM;
2477 }
2478
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002479 size += sprintf(&buf[size], "Flag-port : [ %08lX ]\n",
2480 dd->port->flags);
2481 size += sprintf(&buf[size], "Flag-dd : [ %08lX ]\n",
2482 dd->dd_flag);
2483
2484 *offset = size <= len ? size : len;
2485 size = copy_to_user(ubuf, buf, *offset);
2486 if (size)
David Milburnc8afd0d2013-05-23 16:23:45 -05002487 rv = -EFAULT;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002488
David Milburnc8afd0d2013-05-23 16:23:45 -05002489 kfree(buf);
2490 return rv ? rv : *offset;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002491}
2492
Asai Thambi S P0caff002013-04-03 19:56:21 +05302493static const struct file_operations mtip_device_status_fops = {
2494 .owner = THIS_MODULE,
2495 .open = simple_open,
2496 .read = mtip_hw_read_device_status,
2497 .llseek = no_llseek,
2498};
2499
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002500static const struct file_operations mtip_regs_fops = {
2501 .owner = THIS_MODULE,
2502 .open = simple_open,
2503 .read = mtip_hw_read_registers,
2504 .llseek = no_llseek,
2505};
2506
2507static const struct file_operations mtip_flags_fops = {
2508 .owner = THIS_MODULE,
2509 .open = simple_open,
2510 .read = mtip_hw_read_flags,
2511 .llseek = no_llseek,
2512};
2513
Sam Bradshaw88523a62011-08-30 08:34:26 -06002514/*
2515 * Create the sysfs related attributes.
2516 *
2517 * @dd Pointer to the driver data structure.
2518 * @kobj Pointer to the kobj for the block device.
2519 *
2520 * return value
2521 * 0 Operation completed successfully.
2522 * -EINVAL Invalid parameter.
2523 */
Jens Axboe63166682011-09-27 21:27:43 -06002524static int mtip_hw_sysfs_init(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002525{
2526 if (!kobj || !dd)
2527 return -EINVAL;
2528
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002529 if (sysfs_create_file(kobj, &dev_attr_status.attr))
2530 dev_warn(&dd->pdev->dev,
2531 "Error creating 'status' sysfs entry\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06002532 return 0;
2533}
2534
2535/*
2536 * Remove the sysfs related attributes.
2537 *
2538 * @dd Pointer to the driver data structure.
2539 * @kobj Pointer to the kobj for the block device.
2540 *
2541 * return value
2542 * 0 Operation completed successfully.
2543 * -EINVAL Invalid parameter.
2544 */
Jens Axboe63166682011-09-27 21:27:43 -06002545static int mtip_hw_sysfs_exit(struct driver_data *dd, struct kobject *kobj)
Sam Bradshaw88523a62011-08-30 08:34:26 -06002546{
2547 if (!kobj || !dd)
2548 return -EINVAL;
2549
Asai Thambi S Pf6587212012-04-09 08:35:38 +02002550 sysfs_remove_file(kobj, &dev_attr_status.attr);
Sam Bradshaw88523a62011-08-30 08:34:26 -06002551
2552 return 0;
2553}
2554
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002555static int mtip_hw_debugfs_init(struct driver_data *dd)
2556{
2557 if (!dfs_parent)
2558 return -1;
2559
2560 dd->dfs_node = debugfs_create_dir(dd->disk->disk_name, dfs_parent);
2561 if (IS_ERR_OR_NULL(dd->dfs_node)) {
2562 dev_warn(&dd->pdev->dev,
2563 "Error creating node %s under debugfs\n",
2564 dd->disk->disk_name);
2565 dd->dfs_node = NULL;
2566 return -1;
2567 }
2568
Joe Perches5657a812018-05-24 13:38:59 -06002569 debugfs_create_file("flags", 0444, dd->dfs_node, dd, &mtip_flags_fops);
2570 debugfs_create_file("registers", 0444, dd->dfs_node, dd,
2571 &mtip_regs_fops);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002572
2573 return 0;
2574}
2575
2576static void mtip_hw_debugfs_exit(struct driver_data *dd)
2577{
Sam Bradshaw974a51a2013-05-15 10:04:34 +02002578 if (dd->dfs_node)
2579 debugfs_remove_recursive(dd->dfs_node);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07002580}
2581
Sam Bradshaw88523a62011-08-30 08:34:26 -06002582/*
2583 * Perform any init/resume time hardware setup
2584 *
2585 * @dd Pointer to the driver data structure.
2586 *
2587 * return value
2588 * None
2589 */
2590static inline void hba_setup(struct driver_data *dd)
2591{
2592 u32 hwdata;
2593 hwdata = readl(dd->mmio + HOST_HSORG);
2594
2595 /* interrupt bug workaround: use only 1 IS bit.*/
2596 writel(hwdata |
2597 HSORG_DISABLE_SLOTGRP_INTR |
2598 HSORG_DISABLE_SLOTGRP_PXIS,
2599 dd->mmio + HOST_HSORG);
2600}
2601
Asai Thambi S P2077d942013-04-29 21:19:49 +02002602static int mtip_device_unaligned_constrained(struct driver_data *dd)
2603{
2604 return (dd->pdev->device == P420M_DEVICE_ID ? 1 : 0);
2605}
2606
Sam Bradshaw88523a62011-08-30 08:34:26 -06002607/*
2608 * Detect the details of the product, and store anything needed
2609 * into the driver data structure. This includes product type and
2610 * version and number of slot groups.
2611 *
2612 * @dd Pointer to the driver data structure.
2613 *
2614 * return value
2615 * None
2616 */
2617static void mtip_detect_product(struct driver_data *dd)
2618{
2619 u32 hwdata;
2620 unsigned int rev, slotgroups;
2621
2622 /*
2623 * HBA base + 0xFC [15:0] - vendor-specific hardware interface
2624 * info register:
2625 * [15:8] hardware/software interface rev#
2626 * [ 3] asic-style interface
2627 * [ 2:0] number of slot groups, minus 1 (only valid for asic-style).
2628 */
2629 hwdata = readl(dd->mmio + HOST_HSORG);
2630
2631 dd->product_type = MTIP_PRODUCT_UNKNOWN;
2632 dd->slot_groups = 1;
2633
2634 if (hwdata & 0x8) {
2635 dd->product_type = MTIP_PRODUCT_ASICFPGA;
2636 rev = (hwdata & HSORG_HWREV) >> 8;
2637 slotgroups = (hwdata & HSORG_SLOTGROUPS) + 1;
2638 dev_info(&dd->pdev->dev,
2639 "ASIC-FPGA design, HS rev 0x%x, "
2640 "%i slot groups [%i slots]\n",
2641 rev,
2642 slotgroups,
2643 slotgroups * 32);
2644
2645 if (slotgroups > MTIP_MAX_SLOT_GROUPS) {
2646 dev_warn(&dd->pdev->dev,
2647 "Warning: driver only supports "
2648 "%i slot groups.\n", MTIP_MAX_SLOT_GROUPS);
2649 slotgroups = MTIP_MAX_SLOT_GROUPS;
2650 }
2651 dd->slot_groups = slotgroups;
2652 return;
2653 }
2654
2655 dev_warn(&dd->pdev->dev, "Unrecognized product id\n");
2656}
2657
2658/*
2659 * Blocking wait for FTL rebuild to complete
2660 *
2661 * @dd Pointer to the DRIVER_DATA structure.
2662 *
2663 * return value
2664 * 0 FTL rebuild completed successfully
2665 * -EFAULT FTL rebuild error/timeout/interruption
2666 */
2667static int mtip_ftl_rebuild_poll(struct driver_data *dd)
2668{
2669 unsigned long timeout, cnt = 0, start;
2670
2671 dev_warn(&dd->pdev->dev,
2672 "FTL rebuild in progress. Polling for completion.\n");
2673
2674 start = jiffies;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002675 timeout = jiffies + msecs_to_jiffies(MTIP_FTL_REBUILD_TIMEOUT_MS);
2676
2677 do {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002678 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002679 &dd->dd_flag)))
2680 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002681 if (mtip_check_surprise_removal(dd->pdev))
2682 return -EFAULT;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002683
Sam Bradshaw88523a62011-08-30 08:34:26 -06002684 if (mtip_get_identify(dd->port, NULL) < 0)
2685 return -EFAULT;
2686
2687 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2688 MTIP_FTL_REBUILD_MAGIC) {
2689 ssleep(1);
2690 /* Print message every 3 minutes */
2691 if (cnt++ >= 180) {
2692 dev_warn(&dd->pdev->dev,
2693 "FTL rebuild in progress (%d secs).\n",
2694 jiffies_to_msecs(jiffies - start) / 1000);
2695 cnt = 0;
2696 }
2697 } else {
2698 dev_warn(&dd->pdev->dev,
2699 "FTL rebuild complete (%d secs).\n",
2700 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01002701 mtip_block_initialize(dd);
Asai Thambi S P45038362012-04-09 08:35:38 +02002702 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002703 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06002704 } while (time_before(jiffies, timeout));
2705
2706 /* Check for timeout */
Asai Thambi S P45038362012-04-09 08:35:38 +02002707 dev_err(&dd->pdev->dev,
Sam Bradshaw88523a62011-08-30 08:34:26 -06002708 "Timed out waiting for FTL rebuild to complete (%d secs).\n",
2709 jiffies_to_msecs(jiffies - start) / 1000);
Asai Thambi S P45038362012-04-09 08:35:38 +02002710 return -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06002711}
2712
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002713static void mtip_softirq_done_fn(struct request *rq)
2714{
2715 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
2716 struct driver_data *dd = rq->q->queuedata;
2717
2718 /* Unmap the DMA scatter list entries */
2719 dma_unmap_sg(&dd->pdev->dev, cmd->sg, cmd->scatter_ents,
2720 cmd->direction);
2721
2722 if (unlikely(cmd->unaligned))
2723 up(&dd->port->cmd_slot_unal);
2724
Christoph Hellwig4dda4732017-04-20 16:03:07 +02002725 blk_mq_end_request(rq, cmd->status);
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002726}
2727
2728static void mtip_abort_cmd(struct request *req, void *data,
2729 bool reserved)
2730{
Christoph Hellwig4dda4732017-04-20 16:03:07 +02002731 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(req);
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002732 struct driver_data *dd = data;
2733
Sagi Grimberg436c15a2017-07-04 10:00:41 +03002734 if (!blk_mq_request_started(req))
2735 return;
2736
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002737 dbg_printk(MTIP_DRV_NAME " Aborting request, tag = %d\n", req->tag);
2738
2739 clear_bit(req->tag, dd->port->cmds_to_issue);
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02002740 cmd->status = BLK_STS_IOERR;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002741 mtip_softirq_done_fn(req);
2742}
2743
2744static void mtip_queue_cmd(struct request *req, void *data,
2745 bool reserved)
2746{
2747 struct driver_data *dd = data;
2748
Sagi Grimberg436c15a2017-07-04 10:00:41 +03002749 if (!blk_mq_request_started(req))
2750 return;
2751
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002752 set_bit(req->tag, dd->port->cmds_to_issue);
2753 blk_abort_request(req);
2754}
2755
Sam Bradshaw88523a62011-08-30 08:34:26 -06002756/*
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002757 * service thread to issue queued commands
2758 *
2759 * @data Pointer to the driver data structure.
2760 *
2761 * return value
2762 * 0
2763 */
2764
2765static int mtip_service_thread(void *data)
2766{
2767 struct driver_data *dd = (struct driver_data *)data;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002768 unsigned long slot, slot_start, slot_wrap, to;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002769 unsigned int num_cmd_slots = dd->slot_groups * 32;
2770 struct mtip_port *port = dd->port;
2771
2772 while (1) {
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002773 if (kthread_should_stop() ||
2774 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2775 goto st_out;
2776 clear_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2777
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002778 /*
2779 * the condition is to check neither an internal command is
2780 * is in progress nor error handling is active
2781 */
2782 wait_event_interruptible(port->svc_wait, (port->flags) &&
Asai Thambi SPcfc05bd2016-02-24 21:16:00 -08002783 (port->flags & MTIP_PF_SVC_THD_WORK));
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002784
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002785 if (kthread_should_stop() ||
2786 test_bit(MTIP_PF_SVC_THD_STOP_BIT, &port->flags))
2787 goto st_out;
2788
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002789 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
Asai Thambi S P45038362012-04-09 08:35:38 +02002790 &dd->dd_flag)))
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002791 goto st_out;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02002792
Asai Thambi SPcfc05bd2016-02-24 21:16:00 -08002793 set_bit(MTIP_PF_SVC_THD_ACTIVE_BIT, &port->flags);
2794
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002795restart_eh:
2796 /* Demux bits: start with error handling */
2797 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags)) {
2798 mtip_handle_tfe(dd);
2799 clear_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags);
2800 }
2801
2802 if (test_bit(MTIP_PF_EH_ACTIVE_BIT, &port->flags))
2803 goto restart_eh;
2804
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002805 if (test_bit(MTIP_PF_TO_ACTIVE_BIT, &port->flags)) {
2806 to = jiffies + msecs_to_jiffies(5000);
2807
2808 do {
2809 mdelay(100);
2810 } while (atomic_read(&dd->irq_workers_active) != 0 &&
2811 time_before(jiffies, to));
2812
2813 if (atomic_read(&dd->irq_workers_active) != 0)
2814 dev_warn(&dd->pdev->dev,
2815 "Completion workers still active!");
2816
Sagi Grimberg436c15a2017-07-04 10:00:41 +03002817 blk_mq_quiesce_queue(dd->queue);
2818
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002819 spin_lock(dd->queue->queue_lock);
Keith Busch6d125de2016-03-10 13:58:48 +02002820 blk_mq_tagset_busy_iter(&dd->tags,
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002821 mtip_queue_cmd, dd);
2822 spin_unlock(dd->queue->queue_lock);
2823
2824 set_bit(MTIP_PF_ISSUE_CMDS_BIT, &dd->port->flags);
2825
2826 if (mtip_device_reset(dd))
Keith Busch6d125de2016-03-10 13:58:48 +02002827 blk_mq_tagset_busy_iter(&dd->tags,
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002828 mtip_abort_cmd, dd);
2829
2830 clear_bit(MTIP_PF_TO_ACTIVE_BIT, &dd->port->flags);
Sagi Grimberg436c15a2017-07-04 10:00:41 +03002831
2832 blk_mq_unquiesce_queue(dd->queue);
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08002833 }
2834
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002835 if (test_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags)) {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002836 slot = 1;
2837 /* used to restrict the loop to one iteration */
2838 slot_start = num_cmd_slots;
2839 slot_wrap = 0;
2840 while (1) {
2841 slot = find_next_bit(port->cmds_to_issue,
2842 num_cmd_slots, slot);
2843 if (slot_wrap == 1) {
2844 if ((slot_start >= slot) ||
2845 (slot >= num_cmd_slots))
2846 break;
2847 }
2848 if (unlikely(slot_start == num_cmd_slots))
2849 slot_start = slot;
2850
2851 if (unlikely(slot == num_cmd_slots)) {
2852 slot = 1;
2853 slot_wrap = 1;
2854 continue;
2855 }
2856
2857 /* Issue the command to the hardware */
2858 mtip_issue_ncq_command(port, slot);
2859
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002860 clear_bit(slot, port->cmds_to_issue);
2861 }
2862
Asai Thambi S P8a857a82012-04-09 08:35:38 +02002863 clear_bit(MTIP_PF_ISSUE_CMDS_BIT, &port->flags);
Asai Thambi S P9b204fb2014-05-20 10:48:56 -07002864 }
2865
2866 if (test_bit(MTIP_PF_REBUILD_BIT, &port->flags)) {
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08002867 if (mtip_ftl_rebuild_poll(dd) == 0)
2868 clear_bit(MTIP_PF_REBUILD_BIT, &port->flags);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002869 }
2870 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002871
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06002872st_out:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01002873 return 0;
2874}
2875
2876/*
Sam Bradshaw188b9f42014-01-15 10:14:57 -08002877 * DMA region teardown
2878 *
2879 * @dd Pointer to driver_data structure
2880 *
2881 * return value
2882 * None
2883 */
2884static void mtip_dma_free(struct driver_data *dd)
2885{
Sam Bradshaw188b9f42014-01-15 10:14:57 -08002886 struct mtip_port *port = dd->port;
2887
2888 if (port->block1)
2889 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
2890 port->block1, port->block1_dma);
2891
2892 if (port->command_list) {
2893 dmam_free_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
2894 port->command_list, port->command_list_dma);
2895 }
Sam Bradshaw188b9f42014-01-15 10:14:57 -08002896}
2897
2898/*
2899 * DMA region setup
2900 *
2901 * @dd Pointer to driver_data structure
2902 *
2903 * return value
2904 * -ENOMEM Not enough free DMA region space to initialize driver
2905 */
2906static int mtip_dma_alloc(struct driver_data *dd)
2907{
2908 struct mtip_port *port = dd->port;
Sam Bradshaw188b9f42014-01-15 10:14:57 -08002909
2910 /* Allocate dma memory for RX Fis, Identify, and Sector Bufffer */
2911 port->block1 =
2912 dmam_alloc_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
2913 &port->block1_dma, GFP_KERNEL);
2914 if (!port->block1)
2915 return -ENOMEM;
2916 memset(port->block1, 0, BLOCK_DMA_ALLOC_SZ);
2917
2918 /* Allocate dma memory for command list */
2919 port->command_list =
2920 dmam_alloc_coherent(&dd->pdev->dev, AHCI_CMD_TBL_SZ,
2921 &port->command_list_dma, GFP_KERNEL);
2922 if (!port->command_list) {
2923 dmam_free_coherent(&dd->pdev->dev, BLOCK_DMA_ALLOC_SZ,
2924 port->block1, port->block1_dma);
2925 port->block1 = NULL;
2926 port->block1_dma = 0;
2927 return -ENOMEM;
2928 }
2929 memset(port->command_list, 0, AHCI_CMD_TBL_SZ);
2930
2931 /* Setup all pointers into first DMA region */
2932 port->rxfis = port->block1 + AHCI_RX_FIS_OFFSET;
2933 port->rxfis_dma = port->block1_dma + AHCI_RX_FIS_OFFSET;
2934 port->identify = port->block1 + AHCI_IDFY_OFFSET;
2935 port->identify_dma = port->block1_dma + AHCI_IDFY_OFFSET;
2936 port->log_buf = port->block1 + AHCI_SECTBUF_OFFSET;
2937 port->log_buf_dma = port->block1_dma + AHCI_SECTBUF_OFFSET;
2938 port->smart_buf = port->block1 + AHCI_SMARTBUF_OFFSET;
2939 port->smart_buf_dma = port->block1_dma + AHCI_SMARTBUF_OFFSET;
2940
Sam Bradshaw188b9f42014-01-15 10:14:57 -08002941 return 0;
2942}
2943
Jens Axboeffc771b2014-05-09 09:42:02 -06002944static int mtip_hw_get_identify(struct driver_data *dd)
2945{
2946 struct smart_attr attr242;
2947 unsigned char *buf;
2948 int rv;
2949
2950 if (mtip_get_identify(dd->port, NULL) < 0)
2951 return -EFAULT;
2952
2953 if (*(dd->port->identify + MTIP_FTL_REBUILD_OFFSET) ==
2954 MTIP_FTL_REBUILD_MAGIC) {
2955 set_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags);
2956 return MTIP_FTL_REBUILD_MAGIC;
2957 }
2958 mtip_dump_identify(dd->port);
2959
2960 /* check write protect, over temp and rebuild statuses */
2961 rv = mtip_read_log_page(dd->port, ATA_LOG_SATA_NCQ,
2962 dd->port->log_buf,
2963 dd->port->log_buf_dma, 1);
2964 if (rv) {
2965 dev_warn(&dd->pdev->dev,
2966 "Error in READ LOG EXT (10h) command\n");
2967 /* non-critical error, don't fail the load */
2968 } else {
2969 buf = (unsigned char *)dd->port->log_buf;
2970 if (buf[259] & 0x1) {
2971 dev_info(&dd->pdev->dev,
2972 "Write protect bit is set.\n");
2973 set_bit(MTIP_DDF_WRITE_PROTECT_BIT, &dd->dd_flag);
2974 }
2975 if (buf[288] == 0xF7) {
2976 dev_info(&dd->pdev->dev,
2977 "Exceeded Tmax, drive in thermal shutdown.\n");
2978 set_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag);
2979 }
2980 if (buf[288] == 0xBF) {
2981 dev_info(&dd->pdev->dev,
2982 "Drive indicates rebuild has failed.\n");
Asai Thambi SPaae4a032016-02-24 21:18:20 -08002983 set_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag);
Jens Axboeffc771b2014-05-09 09:42:02 -06002984 }
2985 }
2986
2987 /* get write protect progess */
2988 memset(&attr242, 0, sizeof(struct smart_attr));
2989 if (mtip_get_smart_attr(dd->port, 242, &attr242))
2990 dev_warn(&dd->pdev->dev,
2991 "Unable to check write protect progress\n");
2992 else
2993 dev_info(&dd->pdev->dev,
2994 "Write protect progress: %u%% (%u blocks)\n",
2995 attr242.cur, le32_to_cpu(attr242.data));
2996
2997 return rv;
2998}
2999
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003000/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003001 * Called once for each card.
3002 *
3003 * @dd Pointer to the driver data structure.
3004 *
3005 * return value
3006 * 0 on success, else an error code.
3007 */
Jens Axboe63166682011-09-27 21:27:43 -06003008static int mtip_hw_init(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003009{
3010 int i;
3011 int rv;
Asai Thambi S P45038362012-04-09 08:35:38 +02003012 unsigned long timeout, timetaken;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003013
3014 dd->mmio = pcim_iomap_table(dd->pdev)[MTIP_ABAR];
3015
3016 mtip_detect_product(dd);
3017 if (dd->product_type == MTIP_PRODUCT_UNKNOWN) {
3018 rv = -EIO;
3019 goto out1;
3020 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003021
3022 hba_setup(dd);
3023
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003024 dd->port = kzalloc_node(sizeof(struct mtip_port), GFP_KERNEL,
3025 dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003026 if (!dd->port) {
3027 dev_err(&dd->pdev->dev,
3028 "Memory allocation: port structure\n");
3029 return -ENOMEM;
3030 }
3031
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003032 /* Continue workqueue setup */
3033 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3034 dd->work[i].port = dd->port;
3035
Asai Thambi S P2077d942013-04-29 21:19:49 +02003036 /* Enable unaligned IO constraints for some devices */
3037 if (mtip_device_unaligned_constrained(dd))
3038 dd->unal_qdepth = MTIP_MAX_UNALIGNED_SLOTS;
3039 else
3040 dd->unal_qdepth = 0;
3041
Asai Thambi S P2077d942013-04-29 21:19:49 +02003042 sema_init(&dd->port->cmd_slot_unal, dd->unal_qdepth);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003043
3044 /* Spinlock to prevent concurrent issue */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003045 for (i = 0; i < MTIP_MAX_SLOT_GROUPS; i++)
3046 spin_lock_init(&dd->port->cmd_issue_lock[i]);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003047
3048 /* Set the port mmio base address. */
3049 dd->port->mmio = dd->mmio + PORT_OFFSET;
3050 dd->port->dd = dd;
3051
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003052 /* DMA allocations */
3053 rv = mtip_dma_alloc(dd);
3054 if (rv < 0)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003055 goto out1;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003056
3057 /* Setup the pointers to the extended s_active and CI registers. */
3058 for (i = 0; i < dd->slot_groups; i++) {
3059 dd->port->s_active[i] =
3060 dd->port->mmio + i*0x80 + PORT_SCR_ACT;
3061 dd->port->cmd_issue[i] =
3062 dd->port->mmio + i*0x80 + PORT_COMMAND_ISSUE;
3063 dd->port->completed[i] =
3064 dd->port->mmio + i*0x80 + PORT_SDBV;
3065 }
3066
Asai Thambi S P45038362012-04-09 08:35:38 +02003067 timetaken = jiffies;
3068 timeout = jiffies + msecs_to_jiffies(30000);
3069 while (((readl(dd->port->mmio + PORT_SCR_STAT) & 0x0F) != 0x03) &&
3070 time_before(jiffies, timeout)) {
3071 mdelay(100);
3072 }
3073 if (unlikely(mtip_check_surprise_removal(dd->pdev))) {
3074 timetaken = jiffies - timetaken;
3075 dev_warn(&dd->pdev->dev,
3076 "Surprise removal detected at %u ms\n",
3077 jiffies_to_msecs(timetaken));
3078 rv = -ENODEV;
3079 goto out2 ;
3080 }
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003081 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag))) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003082 timetaken = jiffies - timetaken;
3083 dev_warn(&dd->pdev->dev,
3084 "Removal detected at %u ms\n",
3085 jiffies_to_msecs(timetaken));
3086 rv = -EFAULT;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003087 goto out2;
3088 }
3089
Asai Thambi S P45038362012-04-09 08:35:38 +02003090 /* Conditionally reset the HBA. */
3091 if (!(readl(dd->mmio + HOST_CAP) & HOST_CAP_NZDMA)) {
3092 if (mtip_hba_reset(dd) < 0) {
3093 dev_err(&dd->pdev->dev,
3094 "Card did not reset within timeout\n");
3095 rv = -EIO;
3096 goto out2;
3097 }
3098 } else {
3099 /* Clear any pending interrupts on the HBA */
3100 writel(readl(dd->mmio + HOST_IRQ_STAT),
3101 dd->mmio + HOST_IRQ_STAT);
3102 }
3103
Sam Bradshaw88523a62011-08-30 08:34:26 -06003104 mtip_init_port(dd->port);
3105 mtip_start_port(dd->port);
3106
3107 /* Setup the ISR and enable interrupts. */
3108 rv = devm_request_irq(&dd->pdev->dev,
3109 dd->pdev->irq,
3110 mtip_irq_handler,
3111 IRQF_SHARED,
3112 dev_driver_string(&dd->pdev->dev),
3113 dd);
3114
3115 if (rv) {
3116 dev_err(&dd->pdev->dev,
3117 "Unable to allocate IRQ %d\n", dd->pdev->irq);
3118 goto out2;
3119 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003120 irq_set_affinity_hint(dd->pdev->irq, get_cpu_mask(dd->isr_binding));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003121
3122 /* Enable interrupts on the HBA. */
3123 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3124 dd->mmio + HOST_CTL);
3125
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003126 init_waitqueue_head(&dd->port->svc_wait);
3127
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003128 if (test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)) {
Asai Thambi S P45038362012-04-09 08:35:38 +02003129 rv = -EFAULT;
3130 goto out3;
3131 }
3132
Sam Bradshaw88523a62011-08-30 08:34:26 -06003133 return rv;
3134
3135out3:
Sam Bradshaw88523a62011-08-30 08:34:26 -06003136 /* Disable interrupts on the HBA. */
3137 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3138 dd->mmio + HOST_CTL);
3139
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003140 /* Release the IRQ. */
3141 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003142 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
3143
3144out2:
3145 mtip_deinit_port(dd->port);
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003146 mtip_dma_free(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003147
Sam Bradshaw88523a62011-08-30 08:34:26 -06003148out1:
3149 /* Free the memory allocated for the for structure. */
3150 kfree(dd->port);
3151
3152 return rv;
3153}
3154
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003155static int mtip_standby_drive(struct driver_data *dd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003156{
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003157 int rv = 0;
Jens Axboeffc771b2014-05-09 09:42:02 -06003158
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003159 if (dd->sr || !dd->port)
3160 return -ENODEV;
Jens Axboeffc771b2014-05-09 09:42:02 -06003161 /*
3162 * Send standby immediate (E0h) to the drive so that it
3163 * saves its state.
3164 */
3165 if (!test_bit(MTIP_PF_REBUILD_BIT, &dd->port->flags) &&
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003166 !test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag) &&
3167 !test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag)) {
3168 rv = mtip_standby_immediate(dd->port);
3169 if (rv)
Jens Axboeffc771b2014-05-09 09:42:02 -06003170 dev_warn(&dd->pdev->dev,
3171 "STANDBY IMMEDIATE failed\n");
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003172 }
3173 return rv;
Jens Axboeffc771b2014-05-09 09:42:02 -06003174}
3175
Sam Bradshaw88523a62011-08-30 08:34:26 -06003176/*
3177 * Called to deinitialize an interface.
3178 *
3179 * @dd Pointer to the driver data structure.
3180 *
3181 * return value
3182 * 0
3183 */
Jens Axboe63166682011-09-27 21:27:43 -06003184static int mtip_hw_exit(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003185{
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003186 if (!dd->sr) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003187 /* de-initialize the port. */
3188 mtip_deinit_port(dd->port);
3189
3190 /* Disable interrupts on the HBA. */
3191 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3192 dd->mmio + HOST_CTL);
3193 }
3194
Sam Bradshaw88523a62011-08-30 08:34:26 -06003195 /* Release the IRQ. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003196 irq_set_affinity_hint(dd->pdev->irq, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003197 devm_free_irq(&dd->pdev->dev, dd->pdev->irq, dd);
Asai Thambi SP2132a542015-05-11 15:53:18 -07003198 msleep(1000);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003199
Sam Bradshaw188b9f42014-01-15 10:14:57 -08003200 /* Free dma regions */
3201 mtip_dma_free(dd);
3202
Sam Bradshaw88523a62011-08-30 08:34:26 -06003203 /* Free the memory allocated for the for structure. */
3204 kfree(dd->port);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003205 dd->port = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003206
3207 return 0;
3208}
3209
3210/*
3211 * Issue a Standby Immediate command to the device.
3212 *
3213 * This function is called by the Block Layer just before the
3214 * system powers off during a shutdown.
3215 *
3216 * @dd Pointer to the driver data structure.
3217 *
3218 * return value
3219 * 0
3220 */
Jens Axboe63166682011-09-27 21:27:43 -06003221static int mtip_hw_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003222{
3223 /*
3224 * Send standby immediate (E0h) to the drive so that it
3225 * saves its state.
3226 */
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003227 mtip_standby_drive(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003228
3229 return 0;
3230}
3231
3232/*
3233 * Suspend function
3234 *
3235 * This function is called by the Block Layer just before the
3236 * system hibernates.
3237 *
3238 * @dd Pointer to the driver data structure.
3239 *
3240 * return value
3241 * 0 Suspend was successful
3242 * -EFAULT Suspend was not successful
3243 */
Jens Axboe63166682011-09-27 21:27:43 -06003244static int mtip_hw_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003245{
3246 /*
3247 * Send standby immediate (E0h) to the drive
3248 * so that it saves its state.
3249 */
Asai Thambi SPd8a18d22016-02-24 21:17:32 -08003250 if (mtip_standby_drive(dd) != 0) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003251 dev_err(&dd->pdev->dev,
3252 "Failed standby-immediate command\n");
3253 return -EFAULT;
3254 }
3255
3256 /* Disable interrupts on the HBA.*/
3257 writel(readl(dd->mmio + HOST_CTL) & ~HOST_IRQ_EN,
3258 dd->mmio + HOST_CTL);
3259 mtip_deinit_port(dd->port);
3260
3261 return 0;
3262}
3263
3264/*
3265 * Resume function
3266 *
3267 * This function is called by the Block Layer as the
3268 * system resumes.
3269 *
3270 * @dd Pointer to the driver data structure.
3271 *
3272 * return value
3273 * 0 Resume was successful
3274 * -EFAULT Resume was not successful
3275 */
Jens Axboe63166682011-09-27 21:27:43 -06003276static int mtip_hw_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003277{
3278 /* Perform any needed hardware setup steps */
3279 hba_setup(dd);
3280
3281 /* Reset the HBA */
3282 if (mtip_hba_reset(dd) != 0) {
3283 dev_err(&dd->pdev->dev,
3284 "Unable to reset the HBA\n");
3285 return -EFAULT;
3286 }
3287
3288 /*
3289 * Enable the port, DMA engine, and FIS reception specific
3290 * h/w in controller.
3291 */
3292 mtip_init_port(dd->port);
3293 mtip_start_port(dd->port);
3294
3295 /* Enable interrupts on the HBA.*/
3296 writel(readl(dd->mmio + HOST_CTL) | HOST_IRQ_EN,
3297 dd->mmio + HOST_CTL);
3298
3299 return 0;
3300}
3301
3302/*
Sam Bradshaw88523a62011-08-30 08:34:26 -06003303 * Helper function for reusing disk name
3304 * upon hot insertion.
3305 */
3306static int rssd_disk_name_format(char *prefix,
3307 int index,
3308 char *buf,
3309 int buflen)
3310{
3311 const int base = 'z' - 'a' + 1;
3312 char *begin = buf + strlen(prefix);
3313 char *end = buf + buflen;
3314 char *p;
3315 int unit;
3316
3317 p = end - 1;
3318 *p = '\0';
3319 unit = base;
3320 do {
3321 if (p == begin)
3322 return -EINVAL;
3323 *--p = 'a' + (index % unit);
3324 index = (index / unit) - 1;
3325 } while (index >= 0);
3326
3327 memmove(begin, p, end - p);
3328 memcpy(buf, prefix, strlen(prefix));
3329
3330 return 0;
3331}
3332
3333/*
3334 * Block layer IOCTL handler.
3335 *
3336 * @dev Pointer to the block_device structure.
3337 * @mode ignored
3338 * @cmd IOCTL command passed from the user application.
3339 * @arg Argument passed from the user application.
3340 *
3341 * return value
3342 * 0 IOCTL completed successfully.
3343 * -ENOTTY IOCTL not supported or invalid driver data
3344 * structure pointer.
3345 */
3346static int mtip_block_ioctl(struct block_device *dev,
3347 fmode_t mode,
3348 unsigned cmd,
3349 unsigned long arg)
3350{
3351 struct driver_data *dd = dev->bd_disk->private_data;
3352
3353 if (!capable(CAP_SYS_ADMIN))
3354 return -EACCES;
3355
3356 if (!dd)
3357 return -ENOTTY;
3358
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003359 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003360 return -ENOTTY;
3361
Sam Bradshaw88523a62011-08-30 08:34:26 -06003362 switch (cmd) {
3363 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003364 return -ENOTTY;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003365 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003366 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003367 }
3368}
3369
Jens Axboe16d02c02011-09-27 15:50:01 -06003370#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003371/*
3372 * Block layer compat IOCTL handler.
3373 *
3374 * @dev Pointer to the block_device structure.
3375 * @mode ignored
3376 * @cmd IOCTL command passed from the user application.
3377 * @arg Argument passed from the user application.
3378 *
3379 * return value
3380 * 0 IOCTL completed successfully.
3381 * -ENOTTY IOCTL not supported or invalid driver data
3382 * structure pointer.
3383 */
3384static int mtip_block_compat_ioctl(struct block_device *dev,
3385 fmode_t mode,
3386 unsigned cmd,
3387 unsigned long arg)
3388{
3389 struct driver_data *dd = dev->bd_disk->private_data;
3390
3391 if (!capable(CAP_SYS_ADMIN))
3392 return -EACCES;
3393
3394 if (!dd)
3395 return -ENOTTY;
3396
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003397 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag)))
Asai Thambi S P45038362012-04-09 08:35:38 +02003398 return -ENOTTY;
3399
Sam Bradshaw88523a62011-08-30 08:34:26 -06003400 switch (cmd) {
3401 case BLKFLSBUF:
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003402 return -ENOTTY;
Jens Axboeef0f1582011-09-27 21:19:53 -06003403 case HDIO_DRIVE_TASKFILE: {
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003404 struct mtip_compat_ide_task_request_s __user *compat_req_task;
Jens Axboeef0f1582011-09-27 21:19:53 -06003405 ide_task_request_t req_task;
3406 int compat_tasksize, outtotal, ret;
3407
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003408 compat_tasksize =
3409 sizeof(struct mtip_compat_ide_task_request_s);
Jens Axboeef0f1582011-09-27 21:19:53 -06003410
3411 compat_req_task =
3412 (struct mtip_compat_ide_task_request_s __user *) arg;
3413
3414 if (copy_from_user(&req_task, (void __user *) arg,
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003415 compat_tasksize - (2 * sizeof(compat_long_t))))
Jens Axboeef0f1582011-09-27 21:19:53 -06003416 return -EFAULT;
3417
3418 if (get_user(req_task.out_size, &compat_req_task->out_size))
3419 return -EFAULT;
3420
3421 if (get_user(req_task.in_size, &compat_req_task->in_size))
3422 return -EFAULT;
3423
3424 outtotal = sizeof(struct mtip_compat_ide_task_request_s);
3425
3426 ret = exec_drive_taskfile(dd, (void __user *) arg,
3427 &req_task, outtotal);
3428
3429 if (copy_to_user((void __user *) arg, &req_task,
3430 compat_tasksize -
3431 (2 * sizeof(compat_long_t))))
3432 return -EFAULT;
3433
3434 if (put_user(req_task.out_size, &compat_req_task->out_size))
3435 return -EFAULT;
3436
3437 if (put_user(req_task.in_size, &compat_req_task->in_size))
3438 return -EFAULT;
3439
3440 return ret;
3441 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003442 default:
Jens Axboeef0f1582011-09-27 21:19:53 -06003443 return mtip_hw_ioctl(dd, cmd, arg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003444 }
3445}
Jens Axboe16d02c02011-09-27 15:50:01 -06003446#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003447
3448/*
3449 * Obtain the geometry of the device.
3450 *
3451 * You may think that this function is obsolete, but some applications,
3452 * fdisk for example still used CHS values. This function describes the
3453 * device as having 224 heads and 56 sectors per cylinder. These values are
3454 * chosen so that each cylinder is aligned on a 4KB boundary. Since a
3455 * partition is described in terms of a start and end cylinder this means
3456 * that each partition is also 4KB aligned. Non-aligned partitions adversely
3457 * affects performance.
3458 *
3459 * @dev Pointer to the block_device strucutre.
3460 * @geo Pointer to a hd_geometry structure.
3461 *
3462 * return value
3463 * 0 Operation completed successfully.
3464 * -ENOTTY An error occurred while reading the drive capacity.
3465 */
3466static int mtip_block_getgeo(struct block_device *dev,
3467 struct hd_geometry *geo)
3468{
3469 struct driver_data *dd = dev->bd_disk->private_data;
3470 sector_t capacity;
3471
3472 if (!dd)
3473 return -ENOTTY;
3474
3475 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3476 dev_warn(&dd->pdev->dev,
3477 "Could not get drive capacity.\n");
3478 return -ENOTTY;
3479 }
3480
3481 geo->heads = 224;
3482 geo->sectors = 56;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003483 sector_div(capacity, (geo->heads * geo->sectors));
Sam Bradshaw88523a62011-08-30 08:34:26 -06003484 geo->cylinders = capacity;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003485 return 0;
3486}
3487
Asai Thambi SP51c65702016-02-24 21:18:10 -08003488static int mtip_block_open(struct block_device *dev, fmode_t mode)
3489{
3490 struct driver_data *dd;
3491
3492 if (dev && dev->bd_disk) {
3493 dd = (struct driver_data *) dev->bd_disk->private_data;
3494
3495 if (dd) {
3496 if (test_bit(MTIP_DDF_REMOVAL_BIT,
3497 &dd->dd_flag)) {
3498 return -ENODEV;
3499 }
3500 return 0;
3501 }
3502 }
3503 return -ENODEV;
3504}
3505
Baoyou Xie99e6b872016-08-26 14:08:53 +08003506static void mtip_block_release(struct gendisk *disk, fmode_t mode)
Asai Thambi SP51c65702016-02-24 21:18:10 -08003507{
3508}
3509
Sam Bradshaw88523a62011-08-30 08:34:26 -06003510/*
3511 * Block device operation function.
3512 *
3513 * This structure contains pointers to the functions required by the block
3514 * layer.
3515 */
3516static const struct block_device_operations mtip_block_ops = {
Asai Thambi SP51c65702016-02-24 21:18:10 -08003517 .open = mtip_block_open,
3518 .release = mtip_block_release,
Sam Bradshaw88523a62011-08-30 08:34:26 -06003519 .ioctl = mtip_block_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003520#ifdef CONFIG_COMPAT
Sam Bradshaw88523a62011-08-30 08:34:26 -06003521 .compat_ioctl = mtip_block_compat_ioctl,
Jens Axboe16d02c02011-09-27 15:50:01 -06003522#endif
Sam Bradshaw88523a62011-08-30 08:34:26 -06003523 .getgeo = mtip_block_getgeo,
3524 .owner = THIS_MODULE
3525};
3526
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003527static inline bool is_se_active(struct driver_data *dd)
3528{
3529 if (unlikely(test_bit(MTIP_PF_SE_ACTIVE_BIT, &dd->port->flags))) {
3530 if (dd->port->ic_pause_timer) {
3531 unsigned long to = dd->port->ic_pause_timer +
3532 msecs_to_jiffies(1000);
3533 if (time_after(jiffies, to)) {
3534 clear_bit(MTIP_PF_SE_ACTIVE_BIT,
3535 &dd->port->flags);
3536 clear_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag);
3537 dd->port->ic_pause_timer = 0;
3538 wake_up_interruptible(&dd->port->svc_wait);
3539 return false;
3540 }
3541 }
3542 return true;
3543 }
3544 return false;
3545}
3546
Sam Bradshaw88523a62011-08-30 08:34:26 -06003547/*
3548 * Block layer make request function.
3549 *
3550 * This function is called by the kernel to process a BIO for
3551 * the P320 device.
3552 *
3553 * @queue Pointer to the request queue. Unused other than to obtain
3554 * the driver data structure.
Jens Axboeffc771b2014-05-09 09:42:02 -06003555 * @rq Pointer to the request.
Sam Bradshaw88523a62011-08-30 08:34:26 -06003556 *
Sam Bradshaw88523a62011-08-30 08:34:26 -06003557 */
Jens Axboeffc771b2014-05-09 09:42:02 -06003558static int mtip_submit_request(struct blk_mq_hw_ctx *hctx, struct request *rq)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003559{
Jens Axboeffc771b2014-05-09 09:42:02 -06003560 struct driver_data *dd = hctx->queue->queuedata;
3561 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3562 unsigned int nents;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003563
Asai Thambi SP686d8e02015-05-11 15:51:27 -07003564 if (is_se_active(dd))
3565 return -ENODATA;
3566
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003567 if (unlikely(dd->dd_flag & MTIP_DDF_STOP_IO)) {
3568 if (unlikely(test_bit(MTIP_DDF_REMOVE_PENDING_BIT,
3569 &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003570 return -ENXIO;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003571 }
3572 if (unlikely(test_bit(MTIP_DDF_OVER_TEMP_BIT, &dd->dd_flag))) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003573 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003574 }
3575 if (unlikely(test_bit(MTIP_DDF_WRITE_PROTECT_BIT,
3576 &dd->dd_flag) &&
Jens Axboeffc771b2014-05-09 09:42:02 -06003577 rq_data_dir(rq))) {
3578 return -ENODATA;
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003579 }
Asai Thambi SPaae4a032016-02-24 21:18:20 -08003580 if (unlikely(test_bit(MTIP_DDF_SEC_LOCK_BIT, &dd->dd_flag) ||
3581 test_bit(MTIP_DDF_REBUILD_FAILED_BIT, &dd->dd_flag)))
Jens Axboeffc771b2014-05-09 09:42:02 -06003582 return -ENODATA;
Asai Thambi S P45038362012-04-09 08:35:38 +02003583 }
3584
Mike Christiec2df40d2016-06-05 14:32:17 -05003585 if (req_op(rq) == REQ_OP_DISCARD) {
Jens Axboeffc771b2014-05-09 09:42:02 -06003586 int err;
3587
3588 err = mtip_send_trim(dd, blk_rq_pos(rq), blk_rq_sectors(rq));
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02003589 blk_mq_end_request(rq, err ? BLK_STS_IOERR : BLK_STS_OK);
Jens Axboeffc771b2014-05-09 09:42:02 -06003590 return 0;
Asai Thambi S P15283462013-01-11 14:41:34 +01003591 }
3592
Jens Axboeffc771b2014-05-09 09:42:02 -06003593 /* Create the scatter list for this request. */
3594 nents = blk_rq_map_sg(hctx->queue, rq, cmd->sg);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003595
Jens Axboeffc771b2014-05-09 09:42:02 -06003596 /* Issue the read/write. */
3597 mtip_hw_submit_io(dd, rq, cmd, nents, hctx);
3598 return 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003599}
3600
Jens Axboeffc771b2014-05-09 09:42:02 -06003601static bool mtip_check_unal_depth(struct blk_mq_hw_ctx *hctx,
3602 struct request *rq)
3603{
3604 struct driver_data *dd = hctx->queue->queuedata;
3605 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3606
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003607 if (rq_data_dir(rq) == READ || !dd->unal_qdepth)
Jens Axboeffc771b2014-05-09 09:42:02 -06003608 return false;
3609
3610 /*
3611 * If unaligned depth must be limited on this controller, mark it
3612 * as unaligned if the IO isn't on a 4k boundary (start of length).
3613 */
3614 if (blk_rq_sectors(rq) <= 64) {
3615 if ((blk_rq_pos(rq) & 7) || (blk_rq_sectors(rq) & 7))
3616 cmd->unaligned = 1;
3617 }
3618
3619 if (cmd->unaligned && down_trylock(&dd->port->cmd_slot_unal))
3620 return true;
3621
3622 return false;
3623}
3624
Christoph Hellwigfc17b652017-06-03 09:38:05 +02003625static blk_status_t mtip_issue_reserved_cmd(struct blk_mq_hw_ctx *hctx,
3626 struct request *rq)
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003627{
3628 struct driver_data *dd = hctx->queue->queuedata;
Jens Axboe6f63503c2017-05-02 09:56:00 -06003629 struct mtip_int_cmd *icmd = rq->special;
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003630 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3631 struct mtip_cmd_sg *command_sg;
3632
3633 if (mtip_commands_active(dd->port))
Christoph Hellwigfc17b652017-06-03 09:38:05 +02003634 return BLK_STS_RESOURCE;
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003635
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003636 /* Populate the SG list */
3637 cmd->command_header->opts =
3638 __force_bit2int cpu_to_le32(icmd->opts | icmd->fis_len);
3639 if (icmd->buf_len) {
3640 command_sg = cmd->command + AHCI_CMD_TBL_HDR_SZ;
3641
3642 command_sg->info =
3643 __force_bit2int cpu_to_le32((icmd->buf_len-1) & 0x3FFFFF);
3644 command_sg->dba =
3645 __force_bit2int cpu_to_le32(icmd->buffer & 0xFFFFFFFF);
3646 command_sg->dba_upper =
3647 __force_bit2int cpu_to_le32((icmd->buffer >> 16) >> 16);
3648
3649 cmd->command_header->opts |=
3650 __force_bit2int cpu_to_le32((1 << 16));
3651 }
3652
3653 /* Populate the command header */
3654 cmd->command_header->byte_count = 0;
3655
3656 blk_mq_start_request(rq);
3657 mtip_issue_non_ncq_command(dd->port, rq->tag);
Christoph Hellwigfc17b652017-06-03 09:38:05 +02003658 return 0;
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003659}
3660
Christoph Hellwigfc17b652017-06-03 09:38:05 +02003661static blk_status_t mtip_queue_rq(struct blk_mq_hw_ctx *hctx,
Jens Axboe74c45052014-10-29 11:14:52 -06003662 const struct blk_mq_queue_data *bd)
Jens Axboeffc771b2014-05-09 09:42:02 -06003663{
Jens Axboe74c45052014-10-29 11:14:52 -06003664 struct request *rq = bd->rq;
Jens Axboeffc771b2014-05-09 09:42:02 -06003665 int ret;
3666
Ming Leia4e84aa2017-04-27 07:45:18 -06003667 mtip_init_cmd_header(rq);
3668
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003669 if (blk_rq_is_passthrough(rq))
3670 return mtip_issue_reserved_cmd(hctx, rq);
3671
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003672 if (unlikely(mtip_check_unal_depth(hctx, rq)))
Christoph Hellwigfc17b652017-06-03 09:38:05 +02003673 return BLK_STS_RESOURCE;
Jens Axboeffc771b2014-05-09 09:42:02 -06003674
Christoph Hellwige2490072014-09-13 16:40:09 -07003675 blk_mq_start_request(rq);
3676
Jens Axboeffc771b2014-05-09 09:42:02 -06003677 ret = mtip_submit_request(hctx, rq);
Sam Bradshawf45c40a2014-06-06 13:28:48 -06003678 if (likely(!ret))
Christoph Hellwigfc17b652017-06-03 09:38:05 +02003679 return BLK_STS_OK;
3680 return BLK_STS_IOERR;
Jens Axboeffc771b2014-05-09 09:42:02 -06003681}
3682
Christoph Hellwigd6296d392017-05-01 10:19:08 -06003683static void mtip_free_cmd(struct blk_mq_tag_set *set, struct request *rq,
3684 unsigned int hctx_idx)
Jens Axboeffc771b2014-05-09 09:42:02 -06003685{
Christoph Hellwigd6296d392017-05-01 10:19:08 -06003686 struct driver_data *dd = set->driver_data;
Jens Axboeffc771b2014-05-09 09:42:02 -06003687 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
3688
3689 if (!cmd->command)
3690 return;
3691
3692 dmam_free_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3693 cmd->command, cmd->command_dma);
3694}
3695
Christoph Hellwigd6296d392017-05-01 10:19:08 -06003696static int mtip_init_cmd(struct blk_mq_tag_set *set, struct request *rq,
3697 unsigned int hctx_idx, unsigned int numa_node)
Jens Axboeffc771b2014-05-09 09:42:02 -06003698{
Christoph Hellwigd6296d392017-05-01 10:19:08 -06003699 struct driver_data *dd = set->driver_data;
Jens Axboeffc771b2014-05-09 09:42:02 -06003700 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
Jens Axboeffc771b2014-05-09 09:42:02 -06003701
3702 cmd->command = dmam_alloc_coherent(&dd->pdev->dev, CMD_DMA_ALLOC_SZ,
3703 &cmd->command_dma, GFP_KERNEL);
3704 if (!cmd->command)
3705 return -ENOMEM;
3706
3707 memset(cmd->command, 0, CMD_DMA_ALLOC_SZ);
3708
Jens Axboeffc771b2014-05-09 09:42:02 -06003709 sg_init_table(cmd->sg, MTIP_MAX_SG);
3710 return 0;
3711}
3712
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003713static enum blk_eh_timer_return mtip_cmd_timeout(struct request *req,
3714 bool reserved)
3715{
3716 struct driver_data *dd = req->q->queuedata;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003717
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003718 if (reserved) {
3719 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(req);
3720
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02003721 cmd->status = BLK_STS_TIMEOUT;
Jens Axboe6f63503c2017-05-02 09:56:00 -06003722 return BLK_EH_HANDLED;
Jens Axboe3f5e6a32017-04-28 10:45:08 -06003723 }
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003724
3725 if (test_bit(req->tag, dd->port->cmds_to_issue))
3726 goto exit_handler;
3727
3728 if (test_and_set_bit(MTIP_PF_TO_ACTIVE_BIT, &dd->port->flags))
3729 goto exit_handler;
3730
3731 wake_up_interruptible(&dd->port->svc_wait);
3732exit_handler:
Jens Axboe90beb2e2016-03-04 08:15:48 -07003733 return BLK_EH_RESET_TIMER;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003734}
3735
Eric Biggersf363b082017-03-30 13:39:16 -07003736static const struct blk_mq_ops mtip_mq_ops = {
Jens Axboeffc771b2014-05-09 09:42:02 -06003737 .queue_rq = mtip_queue_rq,
Jens Axboeffc771b2014-05-09 09:42:02 -06003738 .init_request = mtip_init_cmd,
3739 .exit_request = mtip_free_cmd,
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003740 .complete = mtip_softirq_done_fn,
3741 .timeout = mtip_cmd_timeout,
Jens Axboeffc771b2014-05-09 09:42:02 -06003742};
3743
Sam Bradshaw88523a62011-08-30 08:34:26 -06003744/*
3745 * Block layer initialization function.
3746 *
3747 * This function is called once by the PCI layer for each P320
3748 * device that is connected to the system.
3749 *
3750 * @dd Pointer to the driver data structure.
3751 *
3752 * return value
3753 * 0 on success else an error code.
3754 */
Jens Axboe63166682011-09-27 21:27:43 -06003755static int mtip_block_initialize(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003756{
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003757 int rv = 0, wait_for_rebuild = 0;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003758 sector_t capacity;
3759 unsigned int index = 0;
3760 struct kobject *kobj;
3761
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003762 if (dd->disk)
3763 goto skip_create_disk; /* hw init done, before rebuild */
3764
Jens Axboeffc771b2014-05-09 09:42:02 -06003765 if (mtip_hw_init(dd)) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06003766 rv = -EINVAL;
3767 goto protocol_init_error;
3768 }
3769
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003770 dd->disk = alloc_disk_node(MTIP_MAX_MINORS, dd->numa_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003771 if (dd->disk == NULL) {
3772 dev_err(&dd->pdev->dev,
3773 "Unable to allocate gendisk structure\n");
3774 rv = -EINVAL;
3775 goto alloc_disk_error;
3776 }
3777
3778 /* Generate the disk name, implemented same as in sd.c */
3779 do {
Pan Bian5b0e34e2016-12-01 10:10:46 +08003780 if (!ida_pre_get(&rssd_index_ida, GFP_KERNEL)) {
3781 rv = -ENOMEM;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003782 goto ida_get_error;
Pan Bian5b0e34e2016-12-01 10:10:46 +08003783 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06003784
3785 spin_lock(&rssd_index_lock);
3786 rv = ida_get_new(&rssd_index_ida, &index);
3787 spin_unlock(&rssd_index_lock);
3788 } while (rv == -EAGAIN);
3789
3790 if (rv)
3791 goto ida_get_error;
3792
3793 rv = rssd_disk_name_format("rssd",
3794 index,
3795 dd->disk->disk_name,
3796 DISK_NAME_LEN);
3797 if (rv)
3798 goto disk_index_error;
3799
Sam Bradshaw88523a62011-08-30 08:34:26 -06003800 dd->disk->major = dd->major;
Asai Thambi SP75787262015-05-11 15:55:26 -07003801 dd->disk->first_minor = index * MTIP_MAX_MINORS;
3802 dd->disk->minors = MTIP_MAX_MINORS;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003803 dd->disk->fops = &mtip_block_ops;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003804 dd->disk->private_data = dd;
Sam Bradshaw88523a62011-08-30 08:34:26 -06003805 dd->index = index;
3806
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003807 mtip_hw_debugfs_init(dd);
3808
Jens Axboeffc771b2014-05-09 09:42:02 -06003809 memset(&dd->tags, 0, sizeof(dd->tags));
3810 dd->tags.ops = &mtip_mq_ops;
3811 dd->tags.nr_hw_queues = 1;
3812 dd->tags.queue_depth = MTIP_MAX_COMMAND_SLOTS;
3813 dd->tags.reserved_tags = 1;
3814 dd->tags.cmd_size = sizeof(struct mtip_cmd);
3815 dd->tags.numa_node = dd->numa_node;
Jens Axboea800ce82017-04-27 16:46:02 -06003816 dd->tags.flags = BLK_MQ_F_SHOULD_MERGE;
Jens Axboeffc771b2014-05-09 09:42:02 -06003817 dd->tags.driver_data = dd;
Asai Thambi SPabb0ccd2016-02-24 21:21:13 -08003818 dd->tags.timeout = MTIP_NCQ_CMD_TIMEOUT_MS;
Jens Axboeffc771b2014-05-09 09:42:02 -06003819
3820 rv = blk_mq_alloc_tag_set(&dd->tags);
3821 if (rv) {
3822 dev_err(&dd->pdev->dev,
3823 "Unable to allocate request queue\n");
Asai Thambi SP008e56d2016-02-24 21:21:20 -08003824 goto block_queue_alloc_tag_error;
Jens Axboeffc771b2014-05-09 09:42:02 -06003825 }
3826
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003827 /* Allocate the request queue. */
Jens Axboeffc771b2014-05-09 09:42:02 -06003828 dd->queue = blk_mq_init_queue(&dd->tags);
Dan Carpentera8a642c2014-05-14 15:54:18 +03003829 if (IS_ERR(dd->queue)) {
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003830 dev_err(&dd->pdev->dev,
3831 "Unable to allocate request queue\n");
3832 rv = -ENOMEM;
3833 goto block_queue_alloc_init_error;
3834 }
3835
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003836 dd->disk->queue = dd->queue;
3837 dd->queue->queuedata = dd;
3838
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08003839skip_create_disk:
Jens Axboeffc771b2014-05-09 09:42:02 -06003840 /* Initialize the protocol layer. */
3841 wait_for_rebuild = mtip_hw_get_identify(dd);
3842 if (wait_for_rebuild < 0) {
3843 dev_err(&dd->pdev->dev,
3844 "Protocol layer initialization failed\n");
3845 rv = -EINVAL;
3846 goto init_hw_cmds_error;
3847 }
3848
3849 /*
3850 * if rebuild pending, start the service thread, and delay the block
Dan Williams0d52c7562016-06-15 19:44:20 -07003851 * queue creation and device_add_disk()
Jens Axboeffc771b2014-05-09 09:42:02 -06003852 */
3853 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3854 goto start_service_thread;
3855
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003856 /* Set device limits. */
Bart Van Assche4e699cb2018-03-07 17:10:06 -08003857 blk_queue_flag_set(QUEUE_FLAG_NONROT, dd->queue);
3858 blk_queue_flag_clear(QUEUE_FLAG_ADD_RANDOM, dd->queue);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003859 blk_queue_max_segments(dd->queue, MTIP_MAX_SG);
3860 blk_queue_physical_block_size(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003861 blk_queue_max_hw_sectors(dd->queue, 0xffff);
3862 blk_queue_max_segment_size(dd->queue, 0x400000);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003863 blk_queue_io_min(dd->queue, 4096);
Asai Thambi S P6c8ab692012-05-29 18:42:51 -07003864
Asai Thambi S P15283462013-01-11 14:41:34 +01003865 /* Signal trim support */
3866 if (dd->trim_supp == true) {
Bart Van Assche4e699cb2018-03-07 17:10:06 -08003867 blk_queue_flag_set(QUEUE_FLAG_DISCARD, dd->queue);
Asai Thambi S P15283462013-01-11 14:41:34 +01003868 dd->queue->limits.discard_granularity = 4096;
3869 blk_queue_max_discard_sectors(dd->queue,
3870 MTIP_MAX_TRIM_ENTRY_LEN * MTIP_MAX_TRIM_ENTRIES);
Asai Thambi S P15283462013-01-11 14:41:34 +01003871 }
3872
Sam Bradshaw88523a62011-08-30 08:34:26 -06003873 /* Set the capacity of the device in 512 byte sectors. */
3874 if (!(mtip_hw_get_capacity(dd, &capacity))) {
3875 dev_warn(&dd->pdev->dev,
3876 "Could not read drive capacity\n");
3877 rv = -EIO;
3878 goto read_capacity_error;
3879 }
3880 set_capacity(dd->disk, capacity);
3881
3882 /* Enable the block device and add it to /dev */
Dan Williams0d52c7562016-06-15 19:44:20 -07003883 device_add_disk(&dd->pdev->dev, dd->disk);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003884
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003885 dd->bdev = bdget_disk(dd->disk, 0);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003886 /*
3887 * Now that the disk is active, initialize any sysfs attributes
3888 * managed by the protocol layer.
3889 */
3890 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3891 if (kobj) {
3892 mtip_hw_sysfs_init(dd, kobj);
3893 kobject_put(kobj);
3894 }
3895
Asai Thambi S P45038362012-04-09 08:35:38 +02003896 if (dd->mtip_svc_handler) {
Asai Thambi S P8a857a82012-04-09 08:35:38 +02003897 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003898 return rv; /* service thread created for handling rebuild */
Asai Thambi S P45038362012-04-09 08:35:38 +02003899 }
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003900
3901start_service_thread:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003902 dd->mtip_svc_handler = kthread_create_on_node(mtip_service_thread,
Rasmus Villemoes8aeea032015-11-20 10:46:49 +01003903 dd, dd->numa_node,
3904 "mtip_svc_thd_%02d", index);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003905
3906 if (IS_ERR(dd->mtip_svc_handler)) {
Asai Thambi S Pc74b0f52012-04-09 08:35:39 +02003907 dev_err(&dd->pdev->dev, "service thread failed to start\n");
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003908 dd->mtip_svc_handler = NULL;
3909 rv = -EFAULT;
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003910 goto kthread_run_error;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003911 }
Asai Thambi S P16c906e52012-12-20 07:46:25 -08003912 wake_up_process(dd->mtip_svc_handler);
Asai Thambi S P45038362012-04-09 08:35:38 +02003913 if (wait_for_rebuild == MTIP_FTL_REBUILD_MAGIC)
3914 rv = wait_for_rebuild;
3915
Sam Bradshaw88523a62011-08-30 08:34:26 -06003916 return rv;
3917
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003918kthread_run_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003919 bdput(dd->bdev);
3920 dd->bdev = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07003921
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003922 /* Delete our gendisk. This also removes the device from /dev */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003923 del_gendisk(dd->disk);
3924
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003925read_capacity_error:
Jens Axboeffc771b2014-05-09 09:42:02 -06003926init_hw_cmds_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003927 blk_cleanup_queue(dd->queue);
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003928block_queue_alloc_init_error:
Asai Thambi SP008e56d2016-02-24 21:21:20 -08003929 blk_mq_free_tag_set(&dd->tags);
3930block_queue_alloc_tag_error:
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003931 mtip_hw_debugfs_exit(dd);
Sam Bradshaw88523a62011-08-30 08:34:26 -06003932disk_index_error:
3933 spin_lock(&rssd_index_lock);
3934 ida_remove(&rssd_index_ida, index);
3935 spin_unlock(&rssd_index_lock);
3936
3937ida_get_error:
3938 put_disk(dd->disk);
3939
3940alloc_disk_error:
Asai Thambi S P62ee8c12012-01-04 22:01:32 +01003941 mtip_hw_exit(dd); /* De-initialize the protocol layer. */
Sam Bradshaw88523a62011-08-30 08:34:26 -06003942
3943protocol_init_error:
3944 return rv;
3945}
3946
Asai Thambi SP008e56d2016-02-24 21:21:20 -08003947static void mtip_no_dev_cleanup(struct request *rq, void *data, bool reserv)
3948{
Jens Axboe6f63503c2017-05-02 09:56:00 -06003949 struct mtip_cmd *cmd = blk_mq_rq_to_pdu(rq);
Asai Thambi SP008e56d2016-02-24 21:21:20 -08003950
Christoph Hellwig2a842ac2017-06-03 09:38:04 +02003951 cmd->status = BLK_STS_IOERR;
Jens Axboe6f63503c2017-05-02 09:56:00 -06003952 blk_mq_complete_request(rq);
Asai Thambi SP008e56d2016-02-24 21:21:20 -08003953}
3954
Sam Bradshaw88523a62011-08-30 08:34:26 -06003955/*
3956 * Block layer deinitialization function.
3957 *
3958 * Called by the PCI layer as each P320 device is removed.
3959 *
3960 * @dd Pointer to the driver data structure.
3961 *
3962 * return value
3963 * 0
3964 */
Jens Axboe63166682011-09-27 21:27:43 -06003965static int mtip_block_remove(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06003966{
3967 struct kobject *kobj;
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003968
Asai Thambi SP2132a542015-05-11 15:53:18 -07003969 mtip_hw_debugfs_exit(dd);
Asai Thambi S P60ec0ee2011-11-23 08:29:24 +01003970
Asai Thambi SP2132a542015-05-11 15:53:18 -07003971 if (dd->mtip_svc_handler) {
3972 set_bit(MTIP_PF_SVC_THD_STOP_BIT, &dd->port->flags);
3973 wake_up_interruptible(&dd->port->svc_wait);
3974 kthread_stop(dd->mtip_svc_handler);
3975 }
3976
3977 /* Clean up the sysfs attributes, if created */
3978 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag)) {
3979 kobj = kobject_get(&disk_to_dev(dd->disk)->kobj);
3980 if (kobj) {
3981 mtip_hw_sysfs_exit(dd, kobj);
3982 kobject_put(kobj);
Asai Thambi S P45038362012-04-09 08:35:38 +02003983 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07003984 }
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003985
Asai Thambi SP008e56d2016-02-24 21:21:20 -08003986 if (!dd->sr) {
3987 /*
3988 * Explicitly wait here for IOs to quiesce,
3989 * as mtip_standby_drive usually won't wait for IOs.
3990 */
Jens Axboe8afdd942017-04-27 16:32:41 -06003991 if (!mtip_quiesce_io(dd->port, MTIP_QUIESCE_IO_TIMEOUT_MS))
Asai Thambi SP008e56d2016-02-24 21:21:20 -08003992 mtip_standby_drive(dd);
3993 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07003994 else
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06003995 dev_info(&dd->pdev->dev, "device %s surprise removal\n",
3996 dd->disk->disk_name);
Asai Thambi SP2132a542015-05-11 15:53:18 -07003997
Ming Lei1671d522017-03-27 20:06:57 +08003998 blk_freeze_queue_start(dd->queue);
Sagi Grimberg436c15a2017-07-04 10:00:41 +03003999 blk_mq_quiesce_queue(dd->queue);
Keith Busch6d125de2016-03-10 13:58:48 +02004000 blk_mq_tagset_busy_iter(&dd->tags, mtip_no_dev_cleanup, dd);
Sagi Grimberg436c15a2017-07-04 10:00:41 +03004001 blk_mq_unquiesce_queue(dd->queue);
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004002
Asai Thambi SP2132a542015-05-11 15:53:18 -07004003 /*
4004 * Delete our gendisk structure. This also removes the device
4005 * from /dev
4006 */
4007 if (dd->bdev) {
4008 bdput(dd->bdev);
4009 dd->bdev = NULL;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004010 }
Asai Thambi SP2132a542015-05-11 15:53:18 -07004011 if (dd->disk) {
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08004012 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
4013 del_gendisk(dd->disk);
Asai Thambi SP2132a542015-05-11 15:53:18 -07004014 if (dd->disk->queue) {
4015 blk_cleanup_queue(dd->queue);
4016 blk_mq_free_tag_set(&dd->tags);
4017 dd->queue = NULL;
4018 }
4019 put_disk(dd->disk);
4020 }
4021 dd->disk = NULL;
4022
4023 spin_lock(&rssd_index_lock);
4024 ida_remove(&rssd_index_ida, dd->index);
4025 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004026
4027 /* De-initialize the protocol layer. */
4028 mtip_hw_exit(dd);
4029
4030 return 0;
4031}
4032
4033/*
4034 * Function called by the PCI layer when just before the
4035 * machine shuts down.
4036 *
4037 * If a protocol layer shutdown function is present it will be called
4038 * by this function.
4039 *
4040 * @dd Pointer to the driver data structure.
4041 *
4042 * return value
4043 * 0
4044 */
Jens Axboe63166682011-09-27 21:27:43 -06004045static int mtip_block_shutdown(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004046{
Jens Axboeffc771b2014-05-09 09:42:02 -06004047 mtip_hw_shutdown(dd);
4048
Sam Bradshaw88523a62011-08-30 08:34:26 -06004049 /* Delete our gendisk structure, and cleanup the blk queue. */
Asai Thambi S P58c49df2013-01-11 18:47:12 +05304050 if (dd->disk) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304051 dev_info(&dd->pdev->dev,
4052 "Shutting down %s ...\n", dd->disk->disk_name);
Asai Thambi S P58c49df2013-01-11 18:47:12 +05304053
Asai Thambi SP59cf70e2016-02-24 21:17:47 -08004054 if (test_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag))
4055 del_gendisk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304056 if (dd->disk->queue) {
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304057 blk_cleanup_queue(dd->queue);
Jens Axboeffc771b2014-05-09 09:42:02 -06004058 blk_mq_free_tag_set(&dd->tags);
Asai Thambi SP02b48262015-05-11 15:48:00 -07004059 }
4060 put_disk(dd->disk);
Asai Thambi S P5a79e1a2013-04-12 23:57:17 +05304061 dd->disk = NULL;
4062 dd->queue = NULL;
4063 }
Asai Thambi S P8182b492012-04-09 08:35:38 +02004064
4065 spin_lock(&rssd_index_lock);
4066 ida_remove(&rssd_index_ida, dd->index);
4067 spin_unlock(&rssd_index_lock);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004068 return 0;
4069}
4070
Jens Axboe63166682011-09-27 21:27:43 -06004071static int mtip_block_suspend(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004072{
4073 dev_info(&dd->pdev->dev,
4074 "Suspending %s ...\n", dd->disk->disk_name);
4075 mtip_hw_suspend(dd);
4076 return 0;
4077}
4078
Jens Axboe63166682011-09-27 21:27:43 -06004079static int mtip_block_resume(struct driver_data *dd)
Sam Bradshaw88523a62011-08-30 08:34:26 -06004080{
4081 dev_info(&dd->pdev->dev, "Resuming %s ...\n",
4082 dd->disk->disk_name);
4083 mtip_hw_resume(dd);
4084 return 0;
4085}
4086
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004087static void drop_cpu(int cpu)
4088{
4089 cpu_use[cpu]--;
4090}
4091
4092static int get_least_used_cpu_on_node(int node)
4093{
4094 int cpu, least_used_cpu, least_cnt;
4095 const struct cpumask *node_mask;
4096
4097 node_mask = cpumask_of_node(node);
4098 least_used_cpu = cpumask_first(node_mask);
4099 least_cnt = cpu_use[least_used_cpu];
4100 cpu = least_used_cpu;
4101
4102 for_each_cpu(cpu, node_mask) {
4103 if (cpu_use[cpu] < least_cnt) {
4104 least_used_cpu = cpu;
4105 least_cnt = cpu_use[cpu];
4106 }
4107 }
4108 cpu_use[least_used_cpu]++;
4109 return least_used_cpu;
4110}
4111
4112/* Helper for selecting a node in round robin mode */
4113static inline int mtip_get_next_rr_node(void)
4114{
4115 static int next_node = -1;
4116
4117 if (next_node == -1) {
4118 next_node = first_online_node;
4119 return next_node;
4120 }
4121
4122 next_node = next_online_node(next_node);
4123 if (next_node == MAX_NUMNODES)
4124 next_node = first_online_node;
4125 return next_node;
4126}
4127
Fengguang Wu25bac122013-01-12 15:31:40 +08004128static DEFINE_HANDLER(0);
4129static DEFINE_HANDLER(1);
4130static DEFINE_HANDLER(2);
4131static DEFINE_HANDLER(3);
4132static DEFINE_HANDLER(4);
4133static DEFINE_HANDLER(5);
4134static DEFINE_HANDLER(6);
4135static DEFINE_HANDLER(7);
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004136
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004137static void mtip_disable_link_opts(struct driver_data *dd, struct pci_dev *pdev)
4138{
4139 int pos;
4140 unsigned short pcie_dev_ctrl;
4141
4142 pos = pci_find_capability(pdev, PCI_CAP_ID_EXP);
4143 if (pos) {
4144 pci_read_config_word(pdev,
4145 pos + PCI_EXP_DEVCTL,
4146 &pcie_dev_ctrl);
4147 if (pcie_dev_ctrl & (1 << 11) ||
4148 pcie_dev_ctrl & (1 << 4)) {
4149 dev_info(&dd->pdev->dev,
4150 "Disabling ERO/No-Snoop on bridge device %04x:%04x\n",
4151 pdev->vendor, pdev->device);
4152 pcie_dev_ctrl &= ~(PCI_EXP_DEVCTL_NOSNOOP_EN |
4153 PCI_EXP_DEVCTL_RELAX_EN);
4154 pci_write_config_word(pdev,
4155 pos + PCI_EXP_DEVCTL,
4156 pcie_dev_ctrl);
4157 }
4158 }
4159}
4160
4161static void mtip_fix_ero_nosnoop(struct driver_data *dd, struct pci_dev *pdev)
4162{
4163 /*
4164 * This workaround is specific to AMD/ATI chipset with a PCI upstream
4165 * device with device id 0x5aXX
4166 */
4167 if (pdev->bus && pdev->bus->self) {
4168 if (pdev->bus->self->vendor == PCI_VENDOR_ID_ATI &&
4169 ((pdev->bus->self->device & 0xff00) == 0x5a00)) {
4170 mtip_disable_link_opts(dd, pdev->bus->self);
4171 } else {
4172 /* Check further up the topology */
4173 struct pci_dev *parent_dev = pdev->bus->self;
4174 if (parent_dev->bus &&
4175 parent_dev->bus->parent &&
4176 parent_dev->bus->parent->self &&
4177 parent_dev->bus->parent->self->vendor ==
4178 PCI_VENDOR_ID_ATI &&
4179 (parent_dev->bus->parent->self->device &
4180 0xff00) == 0x5a00) {
4181 mtip_disable_link_opts(dd,
4182 parent_dev->bus->parent->self);
4183 }
4184 }
4185 }
4186}
4187
Sam Bradshaw88523a62011-08-30 08:34:26 -06004188/*
4189 * Called for each supported PCI device detected.
4190 *
4191 * This function allocates the private data structure, enables the
4192 * PCI device and then calls the block layer initialization function.
4193 *
4194 * return value
4195 * 0 on success else an error code.
4196 */
4197static int mtip_pci_probe(struct pci_dev *pdev,
4198 const struct pci_device_id *ent)
4199{
4200 int rv = 0;
4201 struct driver_data *dd = NULL;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004202 char cpu_list[256];
4203 const struct cpumask *node_mask;
4204 int cpu, i = 0, j = 0;
4205 int my_node = NUMA_NO_NODE;
Asai Thambi S P0caff002013-04-03 19:56:21 +05304206 unsigned long flags;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004207
4208 /* Allocate memory for this devices private data. */
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004209 my_node = pcibus_to_node(pdev->bus);
4210 if (my_node != NUMA_NO_NODE) {
4211 if (!node_online(my_node))
4212 my_node = mtip_get_next_rr_node();
4213 } else {
4214 dev_info(&pdev->dev, "Kernel not reporting proximity, choosing a node\n");
4215 my_node = mtip_get_next_rr_node();
4216 }
4217 dev_info(&pdev->dev, "NUMA node %d (closest: %d,%d, probe on %d:%d)\n",
4218 my_node, pcibus_to_node(pdev->bus), dev_to_node(&pdev->dev),
Jens Axboe7f328902014-03-10 14:29:37 -06004219 cpu_to_node(raw_smp_processor_id()), raw_smp_processor_id());
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004220
4221 dd = kzalloc_node(sizeof(struct driver_data), GFP_KERNEL, my_node);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004222 if (dd == NULL) {
4223 dev_err(&pdev->dev,
4224 "Unable to allocate memory for driver data\n");
4225 return -ENOMEM;
4226 }
4227
Sam Bradshaw88523a62011-08-30 08:34:26 -06004228 /* Attach the private data to this PCI device. */
4229 pci_set_drvdata(pdev, dd);
4230
4231 rv = pcim_enable_device(pdev);
4232 if (rv < 0) {
4233 dev_err(&pdev->dev, "Unable to enable device\n");
4234 goto iomap_err;
4235 }
4236
4237 /* Map BAR5 to memory. */
4238 rv = pcim_iomap_regions(pdev, 1 << MTIP_ABAR, MTIP_DRV_NAME);
4239 if (rv < 0) {
4240 dev_err(&pdev->dev, "Unable to map regions\n");
4241 goto iomap_err;
4242 }
4243
4244 if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
4245 rv = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
4246
4247 if (rv) {
4248 rv = pci_set_consistent_dma_mask(pdev,
4249 DMA_BIT_MASK(32));
4250 if (rv) {
4251 dev_warn(&pdev->dev,
4252 "64-bit DMA enable failed\n");
4253 goto setmask_err;
4254 }
4255 }
4256 }
4257
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004258 /* Copy the info we may need later into the private data structure. */
4259 dd->major = mtip_major;
4260 dd->instance = instance;
4261 dd->pdev = pdev;
4262 dd->numa_node = my_node;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004263
Asai Thambi S P0caff002013-04-03 19:56:21 +05304264 INIT_LIST_HEAD(&dd->online_list);
4265 INIT_LIST_HEAD(&dd->remove_list);
4266
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004267 memset(dd->workq_name, 0, 32);
4268 snprintf(dd->workq_name, 31, "mtipq%d", dd->instance);
4269
4270 dd->isr_workq = create_workqueue(dd->workq_name);
4271 if (!dd->isr_workq) {
4272 dev_warn(&pdev->dev, "Can't create wq %d\n", dd->instance);
Wei Yongjund137c832013-03-22 08:58:23 -06004273 rv = -ENOMEM;
Christophe JAILLET8e3c2832018-05-10 09:27:31 +02004274 goto setmask_err;
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004275 }
4276
4277 memset(cpu_list, 0, sizeof(cpu_list));
4278
4279 node_mask = cpumask_of_node(dd->numa_node);
4280 if (!cpumask_empty(node_mask)) {
4281 for_each_cpu(cpu, node_mask)
4282 {
4283 snprintf(&cpu_list[j], 256 - j, "%d ", cpu);
4284 j = strlen(cpu_list);
4285 }
4286
4287 dev_info(&pdev->dev, "Node %d on package %d has %d cpu(s): %s\n",
4288 dd->numa_node,
4289 topology_physical_package_id(cpumask_first(node_mask)),
4290 nr_cpus_node(dd->numa_node),
4291 cpu_list);
4292 } else
4293 dev_dbg(&pdev->dev, "mtip32xx: node_mask empty\n");
4294
4295 dd->isr_binding = get_least_used_cpu_on_node(dd->numa_node);
4296 dev_info(&pdev->dev, "Initial IRQ binding node:cpu %d:%d\n",
4297 cpu_to_node(dd->isr_binding), dd->isr_binding);
4298
4299 /* first worker context always runs in ISR */
4300 dd->work[0].cpu_binding = dd->isr_binding;
4301 dd->work[1].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4302 dd->work[2].cpu_binding = get_least_used_cpu_on_node(dd->numa_node);
4303 dd->work[3].cpu_binding = dd->work[0].cpu_binding;
4304 dd->work[4].cpu_binding = dd->work[1].cpu_binding;
4305 dd->work[5].cpu_binding = dd->work[2].cpu_binding;
4306 dd->work[6].cpu_binding = dd->work[2].cpu_binding;
4307 dd->work[7].cpu_binding = dd->work[1].cpu_binding;
4308
4309 /* Log the bindings */
4310 for_each_present_cpu(cpu) {
4311 memset(cpu_list, 0, sizeof(cpu_list));
4312 for (i = 0, j = 0; i < MTIP_MAX_SLOT_GROUPS; i++) {
4313 if (dd->work[i].cpu_binding == cpu) {
4314 snprintf(&cpu_list[j], 256 - j, "%d ", i);
4315 j = strlen(cpu_list);
4316 }
4317 }
4318 if (j)
4319 dev_info(&pdev->dev, "CPU %d: WQs %s\n", cpu, cpu_list);
4320 }
4321
4322 INIT_WORK(&dd->work[0].work, mtip_workq_sdbf0);
4323 INIT_WORK(&dd->work[1].work, mtip_workq_sdbf1);
4324 INIT_WORK(&dd->work[2].work, mtip_workq_sdbf2);
4325 INIT_WORK(&dd->work[3].work, mtip_workq_sdbf3);
4326 INIT_WORK(&dd->work[4].work, mtip_workq_sdbf4);
4327 INIT_WORK(&dd->work[5].work, mtip_workq_sdbf5);
4328 INIT_WORK(&dd->work[6].work, mtip_workq_sdbf6);
4329 INIT_WORK(&dd->work[7].work, mtip_workq_sdbf7);
4330
4331 pci_set_master(pdev);
Wei Yongjund137c832013-03-22 08:58:23 -06004332 rv = pci_enable_msi(pdev);
4333 if (rv) {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004334 dev_warn(&pdev->dev,
4335 "Unable to enable MSI interrupt.\n");
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004336 goto msi_initialize_err;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004337 }
4338
Asai Thambi S Pd1e714d2014-03-13 18:45:15 -07004339 mtip_fix_ero_nosnoop(dd, pdev);
4340
Sam Bradshaw88523a62011-08-30 08:34:26 -06004341 /* Initialize the block layer. */
4342 rv = mtip_block_initialize(dd);
4343 if (rv < 0) {
4344 dev_err(&pdev->dev,
4345 "Unable to initialize block layer\n");
4346 goto block_initialize_err;
4347 }
4348
4349 /*
4350 * Increment the instance count so that each device has a unique
4351 * instance number.
4352 */
4353 instance++;
Asai Thambi S P45038362012-04-09 08:35:38 +02004354 if (rv != MTIP_FTL_REBUILD_MAGIC)
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004355 set_bit(MTIP_DDF_INIT_DONE_BIT, &dd->dd_flag);
Asai Thambi S P6b06d352013-04-03 19:54:35 +05304356 else
4357 rv = 0; /* device in rebuild state, return 0 from probe */
Asai Thambi S P0caff002013-04-03 19:56:21 +05304358
4359 /* Add to online list even if in ftl rebuild */
4360 spin_lock_irqsave(&dev_lock, flags);
4361 list_add(&dd->online_list, &online_list);
4362 spin_unlock_irqrestore(&dev_lock, flags);
4363
Sam Bradshaw88523a62011-08-30 08:34:26 -06004364 goto done;
4365
4366block_initialize_err:
4367 pci_disable_msi(pdev);
Alexander Gordeevcf91f392014-02-19 09:58:15 +01004368
4369msi_initialize_err:
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004370 if (dd->isr_workq) {
4371 flush_workqueue(dd->isr_workq);
4372 destroy_workqueue(dd->isr_workq);
4373 drop_cpu(dd->work[0].cpu_binding);
4374 drop_cpu(dd->work[1].cpu_binding);
4375 drop_cpu(dd->work[2].cpu_binding);
4376 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004377setmask_err:
4378 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
4379
4380iomap_err:
4381 kfree(dd);
4382 pci_set_drvdata(pdev, NULL);
4383 return rv;
4384done:
Sam Bradshaw88523a62011-08-30 08:34:26 -06004385 return rv;
4386}
4387
4388/*
4389 * Called for each probed device when the device is removed or the
4390 * driver is unloaded.
4391 *
4392 * return value
4393 * None
4394 */
4395static void mtip_pci_remove(struct pci_dev *pdev)
4396{
4397 struct driver_data *dd = pci_get_drvdata(pdev);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004398 unsigned long flags, to;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004399
Asai Thambi SP51c65702016-02-24 21:18:10 -08004400 set_bit(MTIP_DDF_REMOVAL_BIT, &dd->dd_flag);
Asai Thambi S P45038362012-04-09 08:35:38 +02004401
Asai Thambi S P0caff002013-04-03 19:56:21 +05304402 spin_lock_irqsave(&dev_lock, flags);
4403 list_del_init(&dd->online_list);
4404 list_add(&dd->remove_list, &removing_list);
4405 spin_unlock_irqrestore(&dev_lock, flags);
4406
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004407 mtip_check_surprise_removal(pdev);
4408 synchronize_irq(dd->pdev->irq);
4409
4410 /* Spin until workers are done */
4411 to = jiffies + msecs_to_jiffies(4000);
4412 do {
4413 msleep(20);
4414 } while (atomic_read(&dd->irq_workers_active) != 0 &&
4415 time_before(jiffies, to));
4416
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004417 if (!dd->sr)
4418 fsync_bdev(dd->bdev);
Asai Thambi SP51c65702016-02-24 21:18:10 -08004419
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004420 if (atomic_read(&dd->irq_workers_active) != 0) {
4421 dev_warn(&dd->pdev->dev,
4422 "Completion workers still active!\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004423 }
Sam Bradshaw88523a62011-08-30 08:34:26 -06004424
Asai Thambi SP008e56d2016-02-24 21:21:20 -08004425 blk_set_queue_dying(dd->queue);
Asai Thambi SP51c65702016-02-24 21:18:10 -08004426 set_bit(MTIP_DDF_REMOVE_PENDING_BIT, &dd->dd_flag);
4427
Sam Bradshaw88523a62011-08-30 08:34:26 -06004428 /* Clean up the block layer. */
4429 mtip_block_remove(dd);
4430
Asai Thambi S P16c906e52012-12-20 07:46:25 -08004431 if (dd->isr_workq) {
4432 flush_workqueue(dd->isr_workq);
4433 destroy_workqueue(dd->isr_workq);
4434 drop_cpu(dd->work[0].cpu_binding);
4435 drop_cpu(dd->work[1].cpu_binding);
4436 drop_cpu(dd->work[2].cpu_binding);
4437 }
4438
Sam Bradshaw88523a62011-08-30 08:34:26 -06004439 pci_disable_msi(pdev);
4440
Asai Thambi S P0caff002013-04-03 19:56:21 +05304441 spin_lock_irqsave(&dev_lock, flags);
4442 list_del_init(&dd->remove_list);
4443 spin_unlock_irqrestore(&dev_lock, flags);
4444
Asai Thambi SP2132a542015-05-11 15:53:18 -07004445 kfree(dd);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004446
Sam Bradshaw88523a62011-08-30 08:34:26 -06004447 pcim_iounmap_regions(pdev, 1 << MTIP_ABAR);
Asai Thambi S P8f8b8992013-09-11 13:14:42 -06004448 pci_set_drvdata(pdev, NULL);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004449}
4450
4451/*
4452 * Called for each probed device when the device is suspended.
4453 *
4454 * return value
4455 * 0 Success
4456 * <0 Error
4457 */
4458static int mtip_pci_suspend(struct pci_dev *pdev, pm_message_t mesg)
4459{
4460 int rv = 0;
4461 struct driver_data *dd = pci_get_drvdata(pdev);
4462
4463 if (!dd) {
4464 dev_err(&pdev->dev,
4465 "Driver private datastructure is NULL\n");
4466 return -EFAULT;
4467 }
4468
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004469 set_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004470
4471 /* Disable ports & interrupts then send standby immediate */
4472 rv = mtip_block_suspend(dd);
4473 if (rv < 0) {
4474 dev_err(&pdev->dev,
4475 "Failed to suspend controller\n");
4476 return rv;
4477 }
4478
4479 /*
4480 * Save the pci config space to pdev structure &
4481 * disable the device
4482 */
4483 pci_save_state(pdev);
4484 pci_disable_device(pdev);
4485
4486 /* Move to Low power state*/
4487 pci_set_power_state(pdev, PCI_D3hot);
4488
4489 return rv;
4490}
4491
4492/*
4493 * Called for each probed device when the device is resumed.
4494 *
4495 * return value
4496 * 0 Success
4497 * <0 Error
4498 */
4499static int mtip_pci_resume(struct pci_dev *pdev)
4500{
4501 int rv = 0;
4502 struct driver_data *dd;
4503
4504 dd = pci_get_drvdata(pdev);
4505 if (!dd) {
4506 dev_err(&pdev->dev,
4507 "Driver private datastructure is NULL\n");
4508 return -EFAULT;
4509 }
4510
4511 /* Move the device to active State */
4512 pci_set_power_state(pdev, PCI_D0);
4513
4514 /* Restore PCI configuration space */
4515 pci_restore_state(pdev);
4516
4517 /* Enable the PCI device*/
4518 rv = pcim_enable_device(pdev);
4519 if (rv < 0) {
4520 dev_err(&pdev->dev,
4521 "Failed to enable card during resume\n");
4522 goto err;
4523 }
4524 pci_set_master(pdev);
4525
4526 /*
4527 * Calls hbaReset, initPort, & startPort function
4528 * then enables interrupts
4529 */
4530 rv = mtip_block_resume(dd);
4531 if (rv < 0)
4532 dev_err(&pdev->dev, "Unable to resume\n");
4533
4534err:
Asai Thambi S P8a857a82012-04-09 08:35:38 +02004535 clear_bit(MTIP_DDF_RESUME_BIT, &dd->dd_flag);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004536
4537 return rv;
4538}
4539
4540/*
4541 * Shutdown routine
4542 *
4543 * return value
4544 * None
4545 */
4546static void mtip_pci_shutdown(struct pci_dev *pdev)
4547{
4548 struct driver_data *dd = pci_get_drvdata(pdev);
4549 if (dd)
4550 mtip_block_shutdown(dd);
4551}
4552
Sam Bradshaw88523a62011-08-30 08:34:26 -06004553/* Table of device ids supported by this driver. */
Benoit Taine9baa3c32014-08-08 15:56:03 +02004554static const struct pci_device_id mtip_pci_tbl[] = {
Asai Thambi S P1a131452012-09-05 22:00:38 +05304555 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320H_DEVICE_ID) },
4556 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320M_DEVICE_ID) },
4557 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P320S_DEVICE_ID) },
4558 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P325M_DEVICE_ID) },
4559 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420H_DEVICE_ID) },
4560 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P420M_DEVICE_ID) },
4561 { PCI_DEVICE(PCI_VENDOR_ID_MICRON, P425M_DEVICE_ID) },
Sam Bradshaw88523a62011-08-30 08:34:26 -06004562 { 0 }
4563};
4564
4565/* Structure that describes the PCI driver functions. */
Jens Axboe3ff147d2011-09-27 21:33:53 -06004566static struct pci_driver mtip_pci_driver = {
Sam Bradshaw88523a62011-08-30 08:34:26 -06004567 .name = MTIP_DRV_NAME,
4568 .id_table = mtip_pci_tbl,
4569 .probe = mtip_pci_probe,
4570 .remove = mtip_pci_remove,
4571 .suspend = mtip_pci_suspend,
4572 .resume = mtip_pci_resume,
4573 .shutdown = mtip_pci_shutdown,
4574};
4575
4576MODULE_DEVICE_TABLE(pci, mtip_pci_tbl);
4577
4578/*
4579 * Module initialization function.
4580 *
4581 * Called once when the module is loaded. This function allocates a major
4582 * block device number to the Cyclone devices and registers the PCI layer
4583 * of the driver.
4584 *
4585 * Return value
4586 * 0 on success else error code.
4587 */
4588static int __init mtip_init(void)
4589{
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004590 int error;
4591
Asai Thambi S P45422e72012-09-05 22:03:56 +05304592 pr_info(MTIP_DRV_NAME " Version " MTIP_DRV_VERSION "\n");
Sam Bradshaw88523a62011-08-30 08:34:26 -06004593
Asai Thambi S P0caff002013-04-03 19:56:21 +05304594 spin_lock_init(&dev_lock);
4595
4596 INIT_LIST_HEAD(&online_list);
4597 INIT_LIST_HEAD(&removing_list);
4598
Sam Bradshaw88523a62011-08-30 08:34:26 -06004599 /* Allocate a major block device number to use with this driver. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004600 error = register_blkdev(0, MTIP_DRV_NAME);
4601 if (error <= 0) {
Asai Thambi S P45422e72012-09-05 22:03:56 +05304602 pr_err("Unable to register block device (%d)\n",
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004603 error);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004604 return -EBUSY;
4605 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004606 mtip_major = error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004607
Asai Thambi S P0caff002013-04-03 19:56:21 +05304608 dfs_parent = debugfs_create_dir("rssd", NULL);
4609 if (IS_ERR_OR_NULL(dfs_parent)) {
4610 pr_warn("Error creating debugfs parent\n");
4611 dfs_parent = NULL;
4612 }
4613 if (dfs_parent) {
4614 dfs_device_status = debugfs_create_file("device_status",
Joe Perches5657a812018-05-24 13:38:59 -06004615 0444, dfs_parent, NULL,
Asai Thambi S P0caff002013-04-03 19:56:21 +05304616 &mtip_device_status_fops);
4617 if (IS_ERR_OR_NULL(dfs_device_status)) {
4618 pr_err("Error creating device_status node\n");
4619 dfs_device_status = NULL;
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004620 }
4621 }
4622
Sam Bradshaw88523a62011-08-30 08:34:26 -06004623 /* Register our PCI operations. */
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004624 error = pci_register_driver(&mtip_pci_driver);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004625 if (error) {
4626 debugfs_remove(dfs_parent);
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004627 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
Asai Thambi S P7b421d22012-06-04 12:44:02 -07004628 }
Ryosuke Saito6d27f092012-04-05 08:09:34 -06004629
4630 return error;
Sam Bradshaw88523a62011-08-30 08:34:26 -06004631}
4632
4633/*
4634 * Module de-initialization function.
4635 *
4636 * Called once when the module is unloaded. This function deallocates
4637 * the major block device number allocated by mtip_init() and
4638 * unregisters the PCI layer of the driver.
4639 *
4640 * Return value
4641 * none
4642 */
4643static void __exit mtip_exit(void)
4644{
Sam Bradshaw88523a62011-08-30 08:34:26 -06004645 /* Release the allocated major block device number. */
4646 unregister_blkdev(mtip_major, MTIP_DRV_NAME);
4647
4648 /* Unregister the PCI driver. */
4649 pci_unregister_driver(&mtip_pci_driver);
Asai Thambi S Paf5ded82014-04-16 20:30:16 -07004650
4651 debugfs_remove_recursive(dfs_parent);
Sam Bradshaw88523a62011-08-30 08:34:26 -06004652}
4653
4654MODULE_AUTHOR("Micron Technology, Inc");
4655MODULE_DESCRIPTION("Micron RealSSD PCIe Block Driver");
4656MODULE_LICENSE("GPL");
4657MODULE_VERSION(MTIP_DRV_VERSION);
4658
4659module_init(mtip_init);
4660module_exit(mtip_exit);